nfs.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* fs/fat/nfs.c
  3. */
  4. #include <linux/exportfs.h>
  5. #include "fat.h"
  6. struct fat_fid {
  7. u32 i_gen;
  8. u32 i_pos_low;
  9. u16 i_pos_hi;
  10. u16 parent_i_pos_hi;
  11. u32 parent_i_pos_low;
  12. u32 parent_i_gen;
  13. };
  14. #define FAT_FID_SIZE_WITHOUT_PARENT 3
  15. #define FAT_FID_SIZE_WITH_PARENT (sizeof(struct fat_fid)/sizeof(u32))
  16. /*
  17. * Look up a directory inode given its starting cluster.
  18. */
  19. static struct inode *fat_dget(struct super_block *sb, int i_logstart)
  20. {
  21. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  22. struct hlist_head *head;
  23. struct msdos_inode_info *i;
  24. struct inode *inode = NULL;
  25. head = sbi->dir_hashtable + fat_dir_hash(i_logstart);
  26. spin_lock(&sbi->dir_hash_lock);
  27. hlist_for_each_entry(i, head, i_dir_hash) {
  28. BUG_ON(i->vfs_inode.i_sb != sb);
  29. if (i->i_logstart != i_logstart)
  30. continue;
  31. inode = igrab(&i->vfs_inode);
  32. if (inode)
  33. break;
  34. }
  35. spin_unlock(&sbi->dir_hash_lock);
  36. return inode;
  37. }
  38. static struct inode *fat_ilookup(struct super_block *sb, u64 ino, loff_t i_pos)
  39. {
  40. if (MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO)
  41. return fat_iget(sb, i_pos);
  42. else {
  43. if ((ino < MSDOS_ROOT_INO) || (ino == MSDOS_FSINFO_INO))
  44. return NULL;
  45. return ilookup(sb, ino);
  46. }
  47. }
  48. static struct inode *__fat_nfs_get_inode(struct super_block *sb,
  49. u64 ino, u32 generation, loff_t i_pos)
  50. {
  51. struct inode *inode = fat_ilookup(sb, ino, i_pos);
  52. if (inode && generation && (inode->i_generation != generation)) {
  53. iput(inode);
  54. inode = NULL;
  55. }
  56. if (inode == NULL && MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
  57. struct buffer_head *bh = NULL;
  58. struct msdos_dir_entry *de ;
  59. sector_t blocknr;
  60. int offset;
  61. fat_get_blknr_offset(MSDOS_SB(sb), i_pos, &blocknr, &offset);
  62. bh = sb_bread(sb, blocknr);
  63. if (!bh) {
  64. fat_msg(sb, KERN_ERR,
  65. "unable to read block(%llu) for building NFS inode",
  66. (llu)blocknr);
  67. return inode;
  68. }
  69. de = (struct msdos_dir_entry *)bh->b_data;
  70. /* If a file is deleted on server and client is not updated
  71. * yet, we must not build the inode upon a lookup call.
  72. */
  73. if (IS_FREE(de[offset].name))
  74. inode = NULL;
  75. else
  76. inode = fat_build_inode(sb, &de[offset], i_pos);
  77. brelse(bh);
  78. }
  79. return inode;
  80. }
  81. static struct inode *fat_nfs_get_inode(struct super_block *sb,
  82. u64 ino, u32 generation)
  83. {
  84. return __fat_nfs_get_inode(sb, ino, generation, 0);
  85. }
  86. static int
  87. fat_encode_fh_nostale(struct inode *inode, __u32 *fh, int *lenp,
  88. struct inode *parent)
  89. {
  90. int len = *lenp;
  91. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  92. struct fat_fid *fid = (struct fat_fid *) fh;
  93. loff_t i_pos;
  94. int type = FILEID_FAT_WITHOUT_PARENT;
  95. if (parent) {
  96. if (len < FAT_FID_SIZE_WITH_PARENT) {
  97. *lenp = FAT_FID_SIZE_WITH_PARENT;
  98. return FILEID_INVALID;
  99. }
  100. } else {
  101. if (len < FAT_FID_SIZE_WITHOUT_PARENT) {
  102. *lenp = FAT_FID_SIZE_WITHOUT_PARENT;
  103. return FILEID_INVALID;
  104. }
  105. }
  106. i_pos = fat_i_pos_read(sbi, inode);
  107. *lenp = FAT_FID_SIZE_WITHOUT_PARENT;
  108. fid->i_gen = inode->i_generation;
  109. fid->i_pos_low = i_pos & 0xFFFFFFFF;
  110. fid->i_pos_hi = (i_pos >> 32) & 0xFFFF;
  111. if (parent) {
  112. i_pos = fat_i_pos_read(sbi, parent);
  113. fid->parent_i_pos_hi = (i_pos >> 32) & 0xFFFF;
  114. fid->parent_i_pos_low = i_pos & 0xFFFFFFFF;
  115. fid->parent_i_gen = parent->i_generation;
  116. type = FILEID_FAT_WITH_PARENT;
  117. *lenp = FAT_FID_SIZE_WITH_PARENT;
  118. } else {
  119. /*
  120. * We need to initialize this field because the fh is actually
  121. * 12 bytes long
  122. */
  123. fid->parent_i_pos_hi = 0;
  124. }
  125. return type;
  126. }
  127. /*
  128. * Map a NFS file handle to a corresponding dentry.
  129. * The dentry may or may not be connected to the filesystem root.
  130. */
  131. static struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
  132. int fh_len, int fh_type)
  133. {
  134. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  135. fat_nfs_get_inode);
  136. }
  137. static struct dentry *fat_fh_to_dentry_nostale(struct super_block *sb,
  138. struct fid *fh, int fh_len,
  139. int fh_type)
  140. {
  141. struct inode *inode = NULL;
  142. struct fat_fid *fid = (struct fat_fid *)fh;
  143. loff_t i_pos;
  144. switch (fh_type) {
  145. case FILEID_FAT_WITHOUT_PARENT:
  146. if (fh_len < FAT_FID_SIZE_WITHOUT_PARENT)
  147. return NULL;
  148. break;
  149. case FILEID_FAT_WITH_PARENT:
  150. if (fh_len < FAT_FID_SIZE_WITH_PARENT)
  151. return NULL;
  152. break;
  153. default:
  154. return NULL;
  155. }
  156. i_pos = fid->i_pos_hi;
  157. i_pos = (i_pos << 32) | (fid->i_pos_low);
  158. inode = __fat_nfs_get_inode(sb, 0, fid->i_gen, i_pos);
  159. return d_obtain_alias(inode);
  160. }
  161. /*
  162. * Find the parent for a file specified by NFS handle.
  163. * This requires that the handle contain the i_ino of the parent.
  164. */
  165. static struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid,
  166. int fh_len, int fh_type)
  167. {
  168. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  169. fat_nfs_get_inode);
  170. }
  171. static struct dentry *fat_fh_to_parent_nostale(struct super_block *sb,
  172. struct fid *fh, int fh_len,
  173. int fh_type)
  174. {
  175. struct inode *inode = NULL;
  176. struct fat_fid *fid = (struct fat_fid *)fh;
  177. loff_t i_pos;
  178. if (fh_len < FAT_FID_SIZE_WITH_PARENT)
  179. return NULL;
  180. switch (fh_type) {
  181. case FILEID_FAT_WITH_PARENT:
  182. i_pos = fid->parent_i_pos_hi;
  183. i_pos = (i_pos << 32) | (fid->parent_i_pos_low);
  184. inode = __fat_nfs_get_inode(sb, 0, fid->parent_i_gen, i_pos);
  185. break;
  186. }
  187. return d_obtain_alias(inode);
  188. }
  189. /*
  190. * Rebuild the parent for a directory that is not connected
  191. * to the filesystem root
  192. */
  193. static
  194. struct inode *fat_rebuild_parent(struct super_block *sb, int parent_logstart)
  195. {
  196. int search_clus, clus_to_match;
  197. struct msdos_dir_entry *de;
  198. struct inode *parent = NULL;
  199. struct inode *dummy_grand_parent = NULL;
  200. struct fat_slot_info sinfo;
  201. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  202. sector_t blknr = fat_clus_to_blknr(sbi, parent_logstart);
  203. struct buffer_head *parent_bh = sb_bread(sb, blknr);
  204. if (!parent_bh) {
  205. fat_msg(sb, KERN_ERR,
  206. "unable to read cluster of parent directory");
  207. return NULL;
  208. }
  209. de = (struct msdos_dir_entry *) parent_bh->b_data;
  210. clus_to_match = fat_get_start(sbi, &de[0]);
  211. search_clus = fat_get_start(sbi, &de[1]);
  212. dummy_grand_parent = fat_dget(sb, search_clus);
  213. if (!dummy_grand_parent) {
  214. dummy_grand_parent = new_inode(sb);
  215. if (!dummy_grand_parent) {
  216. brelse(parent_bh);
  217. return parent;
  218. }
  219. dummy_grand_parent->i_ino = iunique(sb, MSDOS_ROOT_INO);
  220. fat_fill_inode(dummy_grand_parent, &de[1]);
  221. MSDOS_I(dummy_grand_parent)->i_pos = -1;
  222. }
  223. if (!fat_scan_logstart(dummy_grand_parent, clus_to_match, &sinfo))
  224. parent = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  225. brelse(parent_bh);
  226. iput(dummy_grand_parent);
  227. return parent;
  228. }
  229. /*
  230. * Find the parent for a directory that is not currently connected to
  231. * the filesystem root.
  232. *
  233. * On entry, the caller holds d_inode(child_dir)->i_mutex.
  234. */
  235. static struct dentry *fat_get_parent(struct dentry *child_dir)
  236. {
  237. struct super_block *sb = child_dir->d_sb;
  238. struct buffer_head *bh = NULL;
  239. struct msdos_dir_entry *de;
  240. struct inode *parent_inode = NULL;
  241. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  242. if (!fat_get_dotdot_entry(d_inode(child_dir), &bh, &de)) {
  243. int parent_logstart = fat_get_start(sbi, de);
  244. parent_inode = fat_dget(sb, parent_logstart);
  245. if (!parent_inode && sbi->options.nfs == FAT_NFS_NOSTALE_RO)
  246. parent_inode = fat_rebuild_parent(sb, parent_logstart);
  247. }
  248. brelse(bh);
  249. return d_obtain_alias(parent_inode);
  250. }
  251. const struct export_operations fat_export_ops = {
  252. .encode_fh = generic_encode_ino32_fh,
  253. .fh_to_dentry = fat_fh_to_dentry,
  254. .fh_to_parent = fat_fh_to_parent,
  255. .get_parent = fat_get_parent,
  256. };
  257. const struct export_operations fat_export_ops_nostale = {
  258. .encode_fh = fat_encode_fh_nostale,
  259. .fh_to_dentry = fat_fh_to_dentry_nostale,
  260. .fh_to_parent = fat_fh_to_parent_nostale,
  261. .get_parent = fat_get_parent,
  262. };