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