cnode.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* cnode related routines for the coda kernel code
  2. (C) 1996 Peter Braam
  3. */
  4. #include <linux/types.h>
  5. #include <linux/string.h>
  6. #include <linux/time.h>
  7. #include <linux/coda.h>
  8. #include <linux/coda_psdev.h>
  9. #include "coda_linux.h"
  10. static inline int coda_fideq(struct CodaFid *fid1, struct CodaFid *fid2)
  11. {
  12. return memcmp(fid1, fid2, sizeof(*fid1)) == 0;
  13. }
  14. static const struct inode_operations coda_symlink_inode_operations = {
  15. .readlink = generic_readlink,
  16. .follow_link = page_follow_link_light,
  17. .put_link = page_put_link,
  18. .setattr = coda_setattr,
  19. };
  20. /* cnode.c */
  21. static void coda_fill_inode(struct inode *inode, struct coda_vattr *attr)
  22. {
  23. coda_vattr_to_iattr(inode, attr);
  24. if (S_ISREG(inode->i_mode)) {
  25. inode->i_op = &coda_file_inode_operations;
  26. inode->i_fop = &coda_file_operations;
  27. } else if (S_ISDIR(inode->i_mode)) {
  28. inode->i_op = &coda_dir_inode_operations;
  29. inode->i_fop = &coda_dir_operations;
  30. } else if (S_ISLNK(inode->i_mode)) {
  31. inode->i_op = &coda_symlink_inode_operations;
  32. inode->i_data.a_ops = &coda_symlink_aops;
  33. inode->i_mapping = &inode->i_data;
  34. } else
  35. init_special_inode(inode, inode->i_mode, huge_decode_dev(attr->va_rdev));
  36. }
  37. static int coda_test_inode(struct inode *inode, void *data)
  38. {
  39. struct CodaFid *fid = (struct CodaFid *)data;
  40. struct coda_inode_info *cii = ITOC(inode);
  41. return coda_fideq(&cii->c_fid, fid);
  42. }
  43. static int coda_set_inode(struct inode *inode, void *data)
  44. {
  45. struct CodaFid *fid = (struct CodaFid *)data;
  46. struct coda_inode_info *cii = ITOC(inode);
  47. cii->c_fid = *fid;
  48. return 0;
  49. }
  50. struct inode * coda_iget(struct super_block * sb, struct CodaFid * fid,
  51. struct coda_vattr * attr)
  52. {
  53. struct inode *inode;
  54. struct coda_inode_info *cii;
  55. unsigned long hash = coda_f2i(fid);
  56. inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
  57. if (!inode)
  58. return ERR_PTR(-ENOMEM);
  59. if (inode->i_state & I_NEW) {
  60. cii = ITOC(inode);
  61. /* we still need to set i_ino for things like stat(2) */
  62. inode->i_ino = hash;
  63. /* inode is locked and unique, no need to grab cii->c_lock */
  64. cii->c_mapcount = 0;
  65. unlock_new_inode(inode);
  66. }
  67. /* always replace the attributes, type might have changed */
  68. coda_fill_inode(inode, attr);
  69. return inode;
  70. }
  71. /* this is effectively coda_iget:
  72. - get attributes (might be cached)
  73. - get the inode for the fid using vfs iget
  74. - link the two up if this is needed
  75. - fill in the attributes
  76. */
  77. struct inode *coda_cnode_make(struct CodaFid *fid, struct super_block *sb)
  78. {
  79. struct coda_vattr attr;
  80. struct inode *inode;
  81. int error;
  82. /* We get inode numbers from Venus -- see venus source */
  83. error = venus_getattr(sb, fid, &attr);
  84. if (error)
  85. return ERR_PTR(error);
  86. inode = coda_iget(sb, fid, &attr);
  87. if (IS_ERR(inode))
  88. pr_warn("%s: coda_iget failed\n", __func__);
  89. return inode;
  90. }
  91. /* Although we treat Coda file identifiers as immutable, there is one
  92. * special case for files created during a disconnection where they may
  93. * not be globally unique. When an identifier collision is detected we
  94. * first try to flush the cached inode from the kernel and finally
  95. * resort to renaming/rehashing in-place. Userspace remembers both old
  96. * and new values of the identifier to handle any in-flight upcalls.
  97. * The real solution is to use globally unique UUIDs as identifiers, but
  98. * retrofitting the existing userspace code for this is non-trivial. */
  99. void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
  100. struct CodaFid *newfid)
  101. {
  102. struct coda_inode_info *cii = ITOC(inode);
  103. unsigned long hash = coda_f2i(newfid);
  104. BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
  105. /* replace fid and rehash inode */
  106. /* XXX we probably need to hold some lock here! */
  107. remove_inode_hash(inode);
  108. cii->c_fid = *newfid;
  109. inode->i_ino = hash;
  110. __insert_inode_hash(inode, hash);
  111. }
  112. /* convert a fid to an inode. */
  113. struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb)
  114. {
  115. struct inode *inode;
  116. unsigned long hash = coda_f2i(fid);
  117. if ( !sb ) {
  118. pr_warn("%s: no sb!\n", __func__);
  119. return NULL;
  120. }
  121. inode = ilookup5(sb, hash, coda_test_inode, fid);
  122. if ( !inode )
  123. return NULL;
  124. /* we should never see newly created inodes because we intentionally
  125. * fail in the initialization callback */
  126. BUG_ON(inode->i_state & I_NEW);
  127. return inode;
  128. }
  129. /* the CONTROL inode is made without asking attributes from Venus */
  130. struct inode *coda_cnode_makectl(struct super_block *sb)
  131. {
  132. struct inode *inode = new_inode(sb);
  133. if (inode) {
  134. inode->i_ino = CTL_INO;
  135. inode->i_op = &coda_ioctl_inode_operations;
  136. inode->i_fop = &coda_ioctl_operations;
  137. inode->i_mode = 0444;
  138. return inode;
  139. }
  140. return ERR_PTR(-ENOMEM);
  141. }