cnode.c 4.8 KB

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