Blame view

fs/fat/nfs.c 7.42 KB
9c92ab619   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
21b6633d5   Steven J. Magnani   fat (exportfs): m...
2
  /* fs/fat/nfs.c
21b6633d5   Steven J. Magnani   fat (exportfs): m...
3
4
5
6
   */
  
  #include <linux/exportfs.h>
  #include "fat.h"
ea3983ace   Namjae Jeon   fat: restructure ...
7
8
9
10
11
12
13
14
15
16
17
  struct fat_fid {
  	u32 i_gen;
  	u32 i_pos_low;
  	u16 i_pos_hi;
  	u16 parent_i_pos_hi;
  	u32 parent_i_pos_low;
  	u32 parent_i_gen;
  };
  
  #define FAT_FID_SIZE_WITHOUT_PARENT 3
  #define FAT_FID_SIZE_WITH_PARENT (sizeof(struct fat_fid)/sizeof(u32))
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
18
19
  /**
   * Look up a directory inode given its starting cluster.
21b6633d5   Steven J. Magnani   fat (exportfs): m...
20
   */
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
21
  static struct inode *fat_dget(struct super_block *sb, int i_logstart)
21b6633d5   Steven J. Magnani   fat (exportfs): m...
22
  {
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
23
24
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
  	struct hlist_head *head;
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
25
26
  	struct msdos_inode_info *i;
  	struct inode *inode = NULL;
21b6633d5   Steven J. Magnani   fat (exportfs): m...
27

7669e8fb0   Steven J. Magnani   fat (exportfs): f...
28
29
  	head = sbi->dir_hashtable + fat_dir_hash(i_logstart);
  	spin_lock(&sbi->dir_hash_lock);
b67bfe0d4   Sasha Levin   hlist: drop the n...
30
  	hlist_for_each_entry(i, head, i_dir_hash) {
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
31
32
33
34
35
36
  		BUG_ON(i->vfs_inode.i_sb != sb);
  		if (i->i_logstart != i_logstart)
  			continue;
  		inode = igrab(&i->vfs_inode);
  		if (inode)
  			break;
21b6633d5   Steven J. Magnani   fat (exportfs): m...
37
  	}
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
38
39
  	spin_unlock(&sbi->dir_hash_lock);
  	return inode;
21b6633d5   Steven J. Magnani   fat (exportfs): m...
40
  }
8fceb4e01   Namjae Jeon   fat (exportfs): r...
41
42
43
44
45
46
47
48
49
50
51
  static struct inode *fat_ilookup(struct super_block *sb, u64 ino, loff_t i_pos)
  {
  	if (MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO)
  		return fat_iget(sb, i_pos);
  
  	else {
  		if ((ino < MSDOS_ROOT_INO) || (ino == MSDOS_FSINFO_INO))
  			return NULL;
  		return ilookup(sb, ino);
  	}
  }
ea3983ace   Namjae Jeon   fat: restructure ...
52
53
  static struct inode *__fat_nfs_get_inode(struct super_block *sb,
  				       u64 ino, u32 generation, loff_t i_pos)
21b6633d5   Steven J. Magnani   fat (exportfs): m...
54
  {
8fceb4e01   Namjae Jeon   fat (exportfs): r...
55
  	struct inode *inode = fat_ilookup(sb, ino, i_pos);
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
56

7669e8fb0   Steven J. Magnani   fat (exportfs): f...
57
58
59
60
  	if (inode && generation && (inode->i_generation != generation)) {
  		iput(inode);
  		inode = NULL;
  	}
8fceb4e01   Namjae Jeon   fat (exportfs): r...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  	if (inode == NULL && MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
  		struct buffer_head *bh = NULL;
  		struct msdos_dir_entry *de ;
  		sector_t blocknr;
  		int offset;
  		fat_get_blknr_offset(MSDOS_SB(sb), i_pos, &blocknr, &offset);
  		bh = sb_bread(sb, blocknr);
  		if (!bh) {
  			fat_msg(sb, KERN_ERR,
  				"unable to read block(%llu) for building NFS inode",
  				(llu)blocknr);
  			return inode;
  		}
  		de = (struct msdos_dir_entry *)bh->b_data;
  		/* If a file is deleted on server and client is not updated
  		 * yet, we must not build the inode upon a lookup call.
  		 */
  		if (IS_FREE(de[offset].name))
  			inode = NULL;
  		else
  			inode = fat_build_inode(sb, &de[offset], i_pos);
  		brelse(bh);
  	}
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
84
85
  
  	return inode;
21b6633d5   Steven J. Magnani   fat (exportfs): m...
86
  }
ea3983ace   Namjae Jeon   fat: restructure ...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  static struct inode *fat_nfs_get_inode(struct super_block *sb,
  				       u64 ino, u32 generation)
  {
  
  	return __fat_nfs_get_inode(sb, ino, generation, 0);
  }
  
  static int
  fat_encode_fh_nostale(struct inode *inode, __u32 *fh, int *lenp,
  		      struct inode *parent)
  {
  	int len = *lenp;
  	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  	struct fat_fid *fid = (struct fat_fid *) fh;
  	loff_t i_pos;
  	int type = FILEID_FAT_WITHOUT_PARENT;
  
  	if (parent) {
  		if (len < FAT_FID_SIZE_WITH_PARENT) {
  			*lenp = FAT_FID_SIZE_WITH_PARENT;
  			return FILEID_INVALID;
  		}
  	} else {
  		if (len < FAT_FID_SIZE_WITHOUT_PARENT) {
  			*lenp = FAT_FID_SIZE_WITHOUT_PARENT;
  			return FILEID_INVALID;
  		}
  	}
  
  	i_pos = fat_i_pos_read(sbi, inode);
  	*lenp = FAT_FID_SIZE_WITHOUT_PARENT;
  	fid->i_gen = inode->i_generation;
  	fid->i_pos_low = i_pos & 0xFFFFFFFF;
  	fid->i_pos_hi = (i_pos >> 32) & 0xFFFF;
  	if (parent) {
  		i_pos = fat_i_pos_read(sbi, parent);
  		fid->parent_i_pos_hi = (i_pos >> 32) & 0xFFFF;
  		fid->parent_i_pos_low = i_pos & 0xFFFFFFFF;
  		fid->parent_i_gen = parent->i_generation;
  		type = FILEID_FAT_WITH_PARENT;
  		*lenp = FAT_FID_SIZE_WITH_PARENT;
  	}
  
  	return type;
  }
21b6633d5   Steven J. Magnani   fat (exportfs): m...
132
133
134
135
  /**
   * Map a NFS file handle to a corresponding dentry.
   * The dentry may or may not be connected to the filesystem root.
   */
ea3983ace   Namjae Jeon   fat: restructure ...
136
  static struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
21b6633d5   Steven J. Magnani   fat (exportfs): m...
137
138
  				int fh_len, int fh_type)
  {
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
139
140
141
  	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  				    fat_nfs_get_inode);
  }
21b6633d5   Steven J. Magnani   fat (exportfs): m...
142

ea3983ace   Namjae Jeon   fat: restructure ...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  static struct dentry *fat_fh_to_dentry_nostale(struct super_block *sb,
  					       struct fid *fh, int fh_len,
  					       int fh_type)
  {
  	struct inode *inode = NULL;
  	struct fat_fid *fid = (struct fat_fid *)fh;
  	loff_t i_pos;
  
  	switch (fh_type) {
  	case FILEID_FAT_WITHOUT_PARENT:
  		if (fh_len < FAT_FID_SIZE_WITHOUT_PARENT)
  			return NULL;
  		break;
  	case FILEID_FAT_WITH_PARENT:
  		if (fh_len < FAT_FID_SIZE_WITH_PARENT)
  			return NULL;
  		break;
  	default:
  		return NULL;
  	}
  	i_pos = fid->i_pos_hi;
  	i_pos = (i_pos << 32) | (fid->i_pos_low);
  	inode = __fat_nfs_get_inode(sb, 0, fid->i_gen, i_pos);
  
  	return d_obtain_alias(inode);
  }
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
169
170
171
172
  /*
   * Find the parent for a file specified by NFS handle.
   * This requires that the handle contain the i_ino of the parent.
   */
ea3983ace   Namjae Jeon   fat: restructure ...
173
  static struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid,
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
174
175
176
177
  				int fh_len, int fh_type)
  {
  	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  				    fat_nfs_get_inode);
21b6633d5   Steven J. Magnani   fat (exportfs): m...
178
  }
ea3983ace   Namjae Jeon   fat: restructure ...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  static struct dentry *fat_fh_to_parent_nostale(struct super_block *sb,
  					       struct fid *fh, int fh_len,
  					       int fh_type)
  {
  	struct inode *inode = NULL;
  	struct fat_fid *fid = (struct fat_fid *)fh;
  	loff_t i_pos;
  
  	if (fh_len < FAT_FID_SIZE_WITH_PARENT)
  		return NULL;
  
  	switch (fh_type) {
  	case FILEID_FAT_WITH_PARENT:
  		i_pos = fid->parent_i_pos_hi;
  		i_pos = (i_pos << 32) | (fid->parent_i_pos_low);
  		inode = __fat_nfs_get_inode(sb, 0, fid->parent_i_gen, i_pos);
  		break;
  	}
  
  	return d_obtain_alias(inode);
  }
21b6633d5   Steven J. Magnani   fat (exportfs): m...
200
  /*
f1e6fb0ab   Namjae Jeon   fat (exportfs): r...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
   * Rebuild the parent for a directory that is not connected
   *  to the filesystem root
   */
  static
  struct inode *fat_rebuild_parent(struct super_block *sb, int parent_logstart)
  {
  	int search_clus, clus_to_match;
  	struct msdos_dir_entry *de;
  	struct inode *parent = NULL;
  	struct inode *dummy_grand_parent = NULL;
  	struct fat_slot_info sinfo;
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
  	sector_t blknr = fat_clus_to_blknr(sbi, parent_logstart);
  	struct buffer_head *parent_bh = sb_bread(sb, blknr);
  	if (!parent_bh) {
  		fat_msg(sb, KERN_ERR,
  			"unable to read cluster of parent directory");
  		return NULL;
  	}
  
  	de = (struct msdos_dir_entry *) parent_bh->b_data;
  	clus_to_match = fat_get_start(sbi, &de[0]);
  	search_clus = fat_get_start(sbi, &de[1]);
  
  	dummy_grand_parent = fat_dget(sb, search_clus);
  	if (!dummy_grand_parent) {
  		dummy_grand_parent = new_inode(sb);
  		if (!dummy_grand_parent) {
  			brelse(parent_bh);
  			return parent;
  		}
  
  		dummy_grand_parent->i_ino = iunique(sb, MSDOS_ROOT_INO);
  		fat_fill_inode(dummy_grand_parent, &de[1]);
  		MSDOS_I(dummy_grand_parent)->i_pos = -1;
  	}
  
  	if (!fat_scan_logstart(dummy_grand_parent, clus_to_match, &sinfo))
  		parent = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
  
  	brelse(parent_bh);
  	iput(dummy_grand_parent);
  
  	return parent;
  }
  
  /*
21b6633d5   Steven J. Magnani   fat (exportfs): m...
248
249
250
   * Find the parent for a directory that is not currently connected to
   * the filesystem root.
   *
2b0143b5c   David Howells   VFS: normal files...
251
   * On entry, the caller holds d_inode(child_dir)->i_mutex.
21b6633d5   Steven J. Magnani   fat (exportfs): m...
252
   */
ea3983ace   Namjae Jeon   fat: restructure ...
253
  static struct dentry *fat_get_parent(struct dentry *child_dir)
21b6633d5   Steven J. Magnani   fat (exportfs): m...
254
255
256
257
  {
  	struct super_block *sb = child_dir->d_sb;
  	struct buffer_head *bh = NULL;
  	struct msdos_dir_entry *de;
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
258
  	struct inode *parent_inode = NULL;
f1e6fb0ab   Namjae Jeon   fat (exportfs): r...
259
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
21b6633d5   Steven J. Magnani   fat (exportfs): m...
260

2b0143b5c   David Howells   VFS: normal files...
261
  	if (!fat_get_dotdot_entry(d_inode(child_dir), &bh, &de)) {
f1e6fb0ab   Namjae Jeon   fat (exportfs): r...
262
  		int parent_logstart = fat_get_start(sbi, de);
7669e8fb0   Steven J. Magnani   fat (exportfs): f...
263
  		parent_inode = fat_dget(sb, parent_logstart);
f1e6fb0ab   Namjae Jeon   fat (exportfs): r...
264
265
  		if (!parent_inode && sbi->options.nfs == FAT_NFS_NOSTALE_RO)
  			parent_inode = fat_rebuild_parent(sb, parent_logstart);
21b6633d5   Steven J. Magnani   fat (exportfs): m...
266
  	}
21b6633d5   Steven J. Magnani   fat (exportfs): m...
267
  	brelse(bh);
21b6633d5   Steven J. Magnani   fat (exportfs): m...
268

7669e8fb0   Steven J. Magnani   fat (exportfs): f...
269
  	return d_obtain_alias(parent_inode);
21b6633d5   Steven J. Magnani   fat (exportfs): m...
270
  }
ea3983ace   Namjae Jeon   fat: restructure ...
271
272
273
274
275
276
277
278
279
280
281
282
283
  
  const struct export_operations fat_export_ops = {
  	.fh_to_dentry   = fat_fh_to_dentry,
  	.fh_to_parent   = fat_fh_to_parent,
  	.get_parent     = fat_get_parent,
  };
  
  const struct export_operations fat_export_ops_nostale = {
  	.encode_fh      = fat_encode_fh_nostale,
  	.fh_to_dentry   = fat_fh_to_dentry_nostale,
  	.fh_to_parent   = fat_fh_to_parent_nostale,
  	.get_parent     = fat_get_parent,
  };