nfs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. }
  119. return type;
  120. }
  121. /**
  122. * Map a NFS file handle to a corresponding dentry.
  123. * The dentry may or may not be connected to the filesystem root.
  124. */
  125. static struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
  126. int fh_len, int fh_type)
  127. {
  128. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  129. fat_nfs_get_inode);
  130. }
  131. static struct dentry *fat_fh_to_dentry_nostale(struct super_block *sb,
  132. struct fid *fh, int fh_len,
  133. int fh_type)
  134. {
  135. struct inode *inode = NULL;
  136. struct fat_fid *fid = (struct fat_fid *)fh;
  137. loff_t i_pos;
  138. switch (fh_type) {
  139. case FILEID_FAT_WITHOUT_PARENT:
  140. if (fh_len < FAT_FID_SIZE_WITHOUT_PARENT)
  141. return NULL;
  142. break;
  143. case FILEID_FAT_WITH_PARENT:
  144. if (fh_len < FAT_FID_SIZE_WITH_PARENT)
  145. return NULL;
  146. break;
  147. default:
  148. return NULL;
  149. }
  150. i_pos = fid->i_pos_hi;
  151. i_pos = (i_pos << 32) | (fid->i_pos_low);
  152. inode = __fat_nfs_get_inode(sb, 0, fid->i_gen, i_pos);
  153. return d_obtain_alias(inode);
  154. }
  155. /*
  156. * Find the parent for a file specified by NFS handle.
  157. * This requires that the handle contain the i_ino of the parent.
  158. */
  159. static struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid,
  160. int fh_len, int fh_type)
  161. {
  162. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  163. fat_nfs_get_inode);
  164. }
  165. static struct dentry *fat_fh_to_parent_nostale(struct super_block *sb,
  166. struct fid *fh, int fh_len,
  167. int fh_type)
  168. {
  169. struct inode *inode = NULL;
  170. struct fat_fid *fid = (struct fat_fid *)fh;
  171. loff_t i_pos;
  172. if (fh_len < FAT_FID_SIZE_WITH_PARENT)
  173. return NULL;
  174. switch (fh_type) {
  175. case FILEID_FAT_WITH_PARENT:
  176. i_pos = fid->parent_i_pos_hi;
  177. i_pos = (i_pos << 32) | (fid->parent_i_pos_low);
  178. inode = __fat_nfs_get_inode(sb, 0, fid->parent_i_gen, i_pos);
  179. break;
  180. }
  181. return d_obtain_alias(inode);
  182. }
  183. /*
  184. * Rebuild the parent for a directory that is not connected
  185. * to the filesystem root
  186. */
  187. static
  188. struct inode *fat_rebuild_parent(struct super_block *sb, int parent_logstart)
  189. {
  190. int search_clus, clus_to_match;
  191. struct msdos_dir_entry *de;
  192. struct inode *parent = NULL;
  193. struct inode *dummy_grand_parent = NULL;
  194. struct fat_slot_info sinfo;
  195. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  196. sector_t blknr = fat_clus_to_blknr(sbi, parent_logstart);
  197. struct buffer_head *parent_bh = sb_bread(sb, blknr);
  198. if (!parent_bh) {
  199. fat_msg(sb, KERN_ERR,
  200. "unable to read cluster of parent directory");
  201. return NULL;
  202. }
  203. de = (struct msdos_dir_entry *) parent_bh->b_data;
  204. clus_to_match = fat_get_start(sbi, &de[0]);
  205. search_clus = fat_get_start(sbi, &de[1]);
  206. dummy_grand_parent = fat_dget(sb, search_clus);
  207. if (!dummy_grand_parent) {
  208. dummy_grand_parent = new_inode(sb);
  209. if (!dummy_grand_parent) {
  210. brelse(parent_bh);
  211. return parent;
  212. }
  213. dummy_grand_parent->i_ino = iunique(sb, MSDOS_ROOT_INO);
  214. fat_fill_inode(dummy_grand_parent, &de[1]);
  215. MSDOS_I(dummy_grand_parent)->i_pos = -1;
  216. }
  217. if (!fat_scan_logstart(dummy_grand_parent, clus_to_match, &sinfo))
  218. parent = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  219. brelse(parent_bh);
  220. iput(dummy_grand_parent);
  221. return parent;
  222. }
  223. /*
  224. * Find the parent for a directory that is not currently connected to
  225. * the filesystem root.
  226. *
  227. * On entry, the caller holds d_inode(child_dir)->i_mutex.
  228. */
  229. static struct dentry *fat_get_parent(struct dentry *child_dir)
  230. {
  231. struct super_block *sb = child_dir->d_sb;
  232. struct buffer_head *bh = NULL;
  233. struct msdos_dir_entry *de;
  234. struct inode *parent_inode = NULL;
  235. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  236. if (!fat_get_dotdot_entry(d_inode(child_dir), &bh, &de)) {
  237. int parent_logstart = fat_get_start(sbi, de);
  238. parent_inode = fat_dget(sb, parent_logstart);
  239. if (!parent_inode && sbi->options.nfs == FAT_NFS_NOSTALE_RO)
  240. parent_inode = fat_rebuild_parent(sb, parent_logstart);
  241. }
  242. brelse(bh);
  243. return d_obtain_alias(parent_inode);
  244. }
  245. const struct export_operations fat_export_ops = {
  246. .fh_to_dentry = fat_fh_to_dentry,
  247. .fh_to_parent = fat_fh_to_parent,
  248. .get_parent = fat_get_parent,
  249. };
  250. const struct export_operations fat_export_ops_nostale = {
  251. .encode_fh = fat_encode_fh_nostale,
  252. .fh_to_dentry = fat_fh_to_dentry_nostale,
  253. .fh_to_parent = fat_fh_to_parent_nostale,
  254. .get_parent = fat_get_parent,
  255. };