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