cnode.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_linux.h>
  9. #include <linux/coda_fs_i.h>
  10. #include <linux/coda_psdev.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. .follow_link = page_follow_link_light,
  18. .put_link = page_put_link,
  19. .setattr = coda_setattr,
  20. };
  21. /* cnode.c */
  22. static void coda_fill_inode(struct inode *inode, struct coda_vattr *attr)
  23. {
  24. coda_vattr_to_iattr(inode, attr);
  25. if (S_ISREG(inode->i_mode)) {
  26. inode->i_op = &coda_file_inode_operations;
  27. inode->i_fop = &coda_file_operations;
  28. } else if (S_ISDIR(inode->i_mode)) {
  29. inode->i_op = &coda_dir_inode_operations;
  30. inode->i_fop = &coda_dir_operations;
  31. } else if (S_ISLNK(inode->i_mode)) {
  32. inode->i_op = &coda_symlink_inode_operations;
  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. int coda_cnode_make(struct inode **inode, struct CodaFid *fid, struct super_block *sb)
  79. {
  80. struct coda_vattr attr;
  81. int error;
  82. /* We get inode numbers from Venus -- see venus source */
  83. error = venus_getattr(sb, fid, &attr);
  84. if ( error ) {
  85. *inode = NULL;
  86. return error;
  87. }
  88. *inode = coda_iget(sb, fid, &attr);
  89. if ( IS_ERR(*inode) ) {
  90. printk("coda_cnode_make: coda_iget failed\n");
  91. return PTR_ERR(*inode);
  92. }
  93. return 0;
  94. }
  95. /* Although we treat Coda file identifiers as immutable, there is one
  96. * special case for files created during a disconnection where they may
  97. * not be globally unique. When an identifier collision is detected we
  98. * first try to flush the cached inode from the kernel and finally
  99. * resort to renaming/rehashing in-place. Userspace remembers both old
  100. * and new values of the identifier to handle any in-flight upcalls.
  101. * The real solution is to use globally unique UUIDs as identifiers, but
  102. * retrofitting the existing userspace code for this is non-trivial. */
  103. void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
  104. struct CodaFid *newfid)
  105. {
  106. struct coda_inode_info *cii = ITOC(inode);
  107. unsigned long hash = coda_f2i(newfid);
  108. BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
  109. /* replace fid and rehash inode */
  110. /* XXX we probably need to hold some lock here! */
  111. remove_inode_hash(inode);
  112. cii->c_fid = *newfid;
  113. inode->i_ino = hash;
  114. __insert_inode_hash(inode, hash);
  115. }
  116. /* convert a fid to an inode. */
  117. struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb)
  118. {
  119. struct inode *inode;
  120. unsigned long hash = coda_f2i(fid);
  121. if ( !sb ) {
  122. printk("coda_fid_to_inode: no sb!\n");
  123. return NULL;
  124. }
  125. inode = ilookup5(sb, hash, coda_test_inode, fid);
  126. if ( !inode )
  127. return NULL;
  128. /* we should never see newly created inodes because we intentionally
  129. * fail in the initialization callback */
  130. BUG_ON(inode->i_state & I_NEW);
  131. return inode;
  132. }
  133. /* the CONTROL inode is made without asking attributes from Venus */
  134. int coda_cnode_makectl(struct inode **inode, struct super_block *sb)
  135. {
  136. int error = -ENOMEM;
  137. *inode = new_inode(sb);
  138. if (*inode) {
  139. (*inode)->i_ino = CTL_INO;
  140. (*inode)->i_op = &coda_ioctl_inode_operations;
  141. (*inode)->i_fop = &coda_ioctl_operations;
  142. (*inode)->i_mode = 0444;
  143. error = 0;
  144. }
  145. return error;
  146. }