cnode.c 4.7 KB

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