cnode.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* cnode related routines for the coda kernel code
  3. (C) 1996 Peter Braam
  4. */
  5. #include <linux/types.h>
  6. #include <linux/string.h>
  7. #include <linux/time.h>
  8. #include <linux/coda.h>
  9. #include <linux/pagemap.h>
  10. #include "coda_psdev.h"
  11. #include "coda_linux.h"
  12. static inline int coda_fideq(struct CodaFid *fid1, struct CodaFid *fid2)
  13. {
  14. return memcmp(fid1, fid2, sizeof(*fid1)) == 0;
  15. }
  16. static const struct inode_operations coda_symlink_inode_operations = {
  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. umode_t inode_type = coda_inode_type(attr);
  58. retry:
  59. inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
  60. if (!inode)
  61. return ERR_PTR(-ENOMEM);
  62. if (inode->i_state & I_NEW) {
  63. cii = ITOC(inode);
  64. /* we still need to set i_ino for things like stat(2) */
  65. inode->i_ino = hash;
  66. /* inode is locked and unique, no need to grab cii->c_lock */
  67. cii->c_mapcount = 0;
  68. coda_fill_inode(inode, attr);
  69. unlock_new_inode(inode);
  70. } else if ((inode->i_mode & S_IFMT) != inode_type) {
  71. /* Inode has changed type, mark bad and grab a new one */
  72. remove_inode_hash(inode);
  73. coda_flag_inode(inode, C_PURGE);
  74. iput(inode);
  75. goto retry;
  76. }
  77. return inode;
  78. }
  79. /* this is effectively coda_iget:
  80. - get attributes (might be cached)
  81. - get the inode for the fid using vfs iget
  82. - link the two up if this is needed
  83. - fill in the attributes
  84. */
  85. struct inode *coda_cnode_make(struct CodaFid *fid, struct super_block *sb)
  86. {
  87. struct coda_vattr attr;
  88. struct inode *inode;
  89. int error;
  90. /* We get inode numbers from Venus -- see venus source */
  91. error = venus_getattr(sb, fid, &attr);
  92. if (error)
  93. return ERR_PTR(error);
  94. inode = coda_iget(sb, fid, &attr);
  95. if (IS_ERR(inode))
  96. pr_warn("%s: coda_iget failed\n", __func__);
  97. return inode;
  98. }
  99. /* Although we treat Coda file identifiers as immutable, there is one
  100. * special case for files created during a disconnection where they may
  101. * not be globally unique. When an identifier collision is detected we
  102. * first try to flush the cached inode from the kernel and finally
  103. * resort to renaming/rehashing in-place. Userspace remembers both old
  104. * and new values of the identifier to handle any in-flight upcalls.
  105. * The real solution is to use globally unique UUIDs as identifiers, but
  106. * retrofitting the existing userspace code for this is non-trivial. */
  107. void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
  108. struct CodaFid *newfid)
  109. {
  110. struct coda_inode_info *cii = ITOC(inode);
  111. unsigned long hash = coda_f2i(newfid);
  112. BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
  113. /* replace fid and rehash inode */
  114. /* XXX we probably need to hold some lock here! */
  115. remove_inode_hash(inode);
  116. cii->c_fid = *newfid;
  117. inode->i_ino = hash;
  118. __insert_inode_hash(inode, hash);
  119. }
  120. /* convert a fid to an inode. */
  121. struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb)
  122. {
  123. struct inode *inode;
  124. unsigned long hash = coda_f2i(fid);
  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. struct coda_file_info *coda_ftoc(struct file *file)
  134. {
  135. struct coda_file_info *cfi = file->private_data;
  136. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  137. return cfi;
  138. }
  139. /* the CONTROL inode is made without asking attributes from Venus */
  140. struct inode *coda_cnode_makectl(struct super_block *sb)
  141. {
  142. struct inode *inode = new_inode(sb);
  143. if (inode) {
  144. inode->i_ino = CTL_INO;
  145. inode->i_op = &coda_ioctl_inode_operations;
  146. inode->i_fop = &coda_ioctl_operations;
  147. inode->i_mode = 0444;
  148. return inode;
  149. }
  150. return ERR_PTR(-ENOMEM);
  151. }