]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | |
a5694255 | 2 | #include <linux/exportfs.h> |
1da177e4 LT |
3 | #include <linux/fs.h> |
4 | #include <linux/file.h> | |
5 | #include <linux/module.h> | |
d37065cd | 6 | #include <linux/mount.h> |
1da177e4 LT |
7 | #include <linux/namei.h> |
8 | ||
10f11c34 | 9 | #define dprintk(fmt, args...) do{}while(0) |
1da177e4 | 10 | |
1da177e4 | 11 | |
10f11c34 CH |
12 | static int get_name(struct dentry *dentry, char *name, |
13 | struct dentry *child); | |
14 | ||
15 | ||
10f11c34 CH |
16 | static int exportfs_get_name(struct dentry *dir, char *name, |
17 | struct dentry *child) | |
18 | { | |
39655164 | 19 | const struct export_operations *nop = dir->d_sb->s_export_op; |
10f11c34 CH |
20 | |
21 | if (nop->get_name) | |
22 | return nop->get_name(dir, name, child); | |
23 | else | |
24 | return get_name(dir, name, child); | |
25 | } | |
1da177e4 | 26 | |
fb66a198 CH |
27 | /* |
28 | * Check if the dentry or any of it's aliases is acceptable. | |
29 | */ | |
e2f99018 CH |
30 | static struct dentry * |
31 | find_acceptable_alias(struct dentry *result, | |
32 | int (*acceptable)(void *context, struct dentry *dentry), | |
33 | void *context) | |
34 | { | |
35 | struct dentry *dentry, *toput = NULL; | |
36 | ||
fb66a198 CH |
37 | if (acceptable(context, result)) |
38 | return result; | |
39 | ||
e2f99018 CH |
40 | spin_lock(&dcache_lock); |
41 | list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) { | |
42 | dget_locked(dentry); | |
43 | spin_unlock(&dcache_lock); | |
44 | if (toput) | |
45 | dput(toput); | |
46 | if (dentry != result && acceptable(context, dentry)) { | |
47 | dput(result); | |
48 | return dentry; | |
49 | } | |
50 | spin_lock(&dcache_lock); | |
51 | toput = dentry; | |
52 | } | |
53 | spin_unlock(&dcache_lock); | |
54 | ||
55 | if (toput) | |
56 | dput(toput); | |
57 | return NULL; | |
58 | } | |
59 | ||
dd90b509 CH |
60 | /* |
61 | * Find root of a disconnected subtree and return a reference to it. | |
62 | */ | |
63 | static struct dentry * | |
64 | find_disconnected_root(struct dentry *dentry) | |
65 | { | |
66 | dget(dentry); | |
67 | spin_lock(&dentry->d_lock); | |
68 | while (!IS_ROOT(dentry) && | |
69 | (dentry->d_parent->d_flags & DCACHE_DISCONNECTED)) { | |
70 | struct dentry *parent = dentry->d_parent; | |
71 | dget(parent); | |
72 | spin_unlock(&dentry->d_lock); | |
73 | dput(dentry); | |
74 | dentry = parent; | |
75 | spin_lock(&dentry->d_lock); | |
76 | } | |
77 | spin_unlock(&dentry->d_lock); | |
78 | return dentry; | |
79 | } | |
80 | ||
019ab801 CH |
81 | |
82 | /* | |
83 | * Make sure target_dir is fully connected to the dentry tree. | |
1da177e4 | 84 | * |
019ab801 | 85 | * It may already be, as the flag isn't always updated when connection happens. |
1da177e4 | 86 | */ |
019ab801 CH |
87 | static int |
88 | reconnect_path(struct super_block *sb, struct dentry *target_dir) | |
1da177e4 | 89 | { |
1da177e4 | 90 | char nbuf[NAME_MAX+1]; |
019ab801 CH |
91 | int noprogress = 0; |
92 | int err = -ESTALE; | |
1da177e4 LT |
93 | |
94 | /* | |
019ab801 | 95 | * It is possible that a confused file system might not let us complete |
1da177e4 LT |
96 | * the path to the root. For example, if get_parent returns a directory |
97 | * in which we cannot find a name for the child. While this implies a | |
98 | * very sick filesystem we don't want it to cause knfsd to spin. Hence | |
99 | * the noprogress counter. If we go through the loop 10 times (2 is | |
100 | * probably enough) without getting anywhere, we just give up | |
101 | */ | |
1da177e4 | 102 | while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) { |
dd90b509 | 103 | struct dentry *pd = find_disconnected_root(target_dir); |
1da177e4 LT |
104 | |
105 | if (!IS_ROOT(pd)) { | |
106 | /* must have found a connected parent - great */ | |
107 | spin_lock(&pd->d_lock); | |
108 | pd->d_flags &= ~DCACHE_DISCONNECTED; | |
109 | spin_unlock(&pd->d_lock); | |
110 | noprogress = 0; | |
111 | } else if (pd == sb->s_root) { | |
112 | printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n"); | |
113 | spin_lock(&pd->d_lock); | |
114 | pd->d_flags &= ~DCACHE_DISCONNECTED; | |
115 | spin_unlock(&pd->d_lock); | |
116 | noprogress = 0; | |
117 | } else { | |
10f11c34 CH |
118 | /* |
119 | * We have hit the top of a disconnected path, try to | |
120 | * find parent and connect. | |
121 | * | |
122 | * Racing with some other process renaming a directory | |
123 | * isn't much of a problem here. If someone renames | |
124 | * the directory, it will end up properly connected, | |
125 | * which is what we want | |
126 | * | |
127 | * Getting the parent can't be supported generically, | |
128 | * the locking is too icky. | |
129 | * | |
130 | * Instead we just return EACCES. If server reboots | |
131 | * or inodes get flushed, you lose | |
1da177e4 | 132 | */ |
10f11c34 | 133 | struct dentry *ppd = ERR_PTR(-EACCES); |
1da177e4 LT |
134 | struct dentry *npd; |
135 | ||
1b1dcc1b | 136 | mutex_lock(&pd->d_inode->i_mutex); |
019ab801 CH |
137 | if (sb->s_export_op->get_parent) |
138 | ppd = sb->s_export_op->get_parent(pd); | |
1b1dcc1b | 139 | mutex_unlock(&pd->d_inode->i_mutex); |
1da177e4 LT |
140 | |
141 | if (IS_ERR(ppd)) { | |
142 | err = PTR_ERR(ppd); | |
019ab801 CH |
143 | dprintk("%s: get_parent of %ld failed, err %d\n", |
144 | __FUNCTION__, pd->d_inode->i_ino, err); | |
1da177e4 LT |
145 | dput(pd); |
146 | break; | |
147 | } | |
019ab801 CH |
148 | |
149 | dprintk("%s: find name of %lu in %lu\n", __FUNCTION__, | |
150 | pd->d_inode->i_ino, ppd->d_inode->i_ino); | |
10f11c34 | 151 | err = exportfs_get_name(ppd, nbuf, pd); |
1da177e4 LT |
152 | if (err) { |
153 | dput(ppd); | |
154 | dput(pd); | |
155 | if (err == -ENOENT) | |
156 | /* some race between get_parent and | |
157 | * get_name? just try again | |
158 | */ | |
159 | continue; | |
160 | break; | |
161 | } | |
019ab801 | 162 | dprintk("%s: found name: %s\n", __FUNCTION__, nbuf); |
1b1dcc1b | 163 | mutex_lock(&ppd->d_inode->i_mutex); |
1da177e4 | 164 | npd = lookup_one_len(nbuf, ppd, strlen(nbuf)); |
1b1dcc1b | 165 | mutex_unlock(&ppd->d_inode->i_mutex); |
1da177e4 LT |
166 | if (IS_ERR(npd)) { |
167 | err = PTR_ERR(npd); | |
019ab801 CH |
168 | dprintk("%s: lookup failed: %d\n", |
169 | __FUNCTION__, err); | |
1da177e4 LT |
170 | dput(ppd); |
171 | dput(pd); | |
172 | break; | |
173 | } | |
174 | /* we didn't really want npd, we really wanted | |
175 | * a side-effect of the lookup. | |
176 | * hopefully, npd == pd, though it isn't really | |
177 | * a problem if it isn't | |
178 | */ | |
179 | if (npd == pd) | |
180 | noprogress = 0; | |
181 | else | |
019ab801 | 182 | printk("%s: npd != pd\n", __FUNCTION__); |
1da177e4 LT |
183 | dput(npd); |
184 | dput(ppd); | |
185 | if (IS_ROOT(pd)) { | |
186 | /* something went wrong, we have to give up */ | |
187 | dput(pd); | |
188 | break; | |
189 | } | |
190 | } | |
191 | dput(pd); | |
192 | } | |
193 | ||
194 | if (target_dir->d_flags & DCACHE_DISCONNECTED) { | |
195 | /* something went wrong - oh-well */ | |
196 | if (!err) | |
197 | err = -ESTALE; | |
019ab801 | 198 | return err; |
1da177e4 | 199 | } |
019ab801 CH |
200 | |
201 | return 0; | |
202 | } | |
203 | ||
1da177e4 LT |
204 | struct getdents_callback { |
205 | char *name; /* name that was found. It already points to a | |
206 | buffer NAME_MAX+1 is size */ | |
207 | unsigned long ino; /* the inum we are looking for */ | |
208 | int found; /* inode matched? */ | |
209 | int sequence; /* sequence counter */ | |
210 | }; | |
211 | ||
212 | /* | |
213 | * A rather strange filldir function to capture | |
214 | * the name matching the specified inode number. | |
215 | */ | |
216 | static int filldir_one(void * __buf, const char * name, int len, | |
afefdbb2 | 217 | loff_t pos, u64 ino, unsigned int d_type) |
1da177e4 LT |
218 | { |
219 | struct getdents_callback *buf = __buf; | |
220 | int result = 0; | |
221 | ||
222 | buf->sequence++; | |
223 | if (buf->ino == ino) { | |
224 | memcpy(buf->name, name, len); | |
225 | buf->name[len] = '\0'; | |
226 | buf->found = 1; | |
227 | result = -1; | |
228 | } | |
229 | return result; | |
230 | } | |
231 | ||
232 | /** | |
233 | * get_name - default export_operations->get_name function | |
234 | * @dentry: the directory in which to find a name | |
235 | * @name: a pointer to a %NAME_MAX+1 char buffer to store the name | |
236 | * @child: the dentry for the child directory. | |
237 | * | |
238 | * calls readdir on the parent until it finds an entry with | |
239 | * the same inode number as the child, and returns that. | |
240 | */ | |
241 | static int get_name(struct dentry *dentry, char *name, | |
242 | struct dentry *child) | |
243 | { | |
244 | struct inode *dir = dentry->d_inode; | |
245 | int error; | |
246 | struct file *file; | |
247 | struct getdents_callback buffer; | |
248 | ||
249 | error = -ENOTDIR; | |
250 | if (!dir || !S_ISDIR(dir->i_mode)) | |
251 | goto out; | |
252 | error = -EINVAL; | |
253 | if (!dir->i_fop) | |
254 | goto out; | |
255 | /* | |
256 | * Open the directory ... | |
257 | */ | |
258 | file = dentry_open(dget(dentry), NULL, O_RDONLY); | |
259 | error = PTR_ERR(file); | |
260 | if (IS_ERR(file)) | |
261 | goto out; | |
262 | ||
263 | error = -EINVAL; | |
264 | if (!file->f_op->readdir) | |
265 | goto out_close; | |
266 | ||
267 | buffer.name = name; | |
268 | buffer.ino = child->d_inode->i_ino; | |
269 | buffer.found = 0; | |
270 | buffer.sequence = 0; | |
271 | while (1) { | |
272 | int old_seq = buffer.sequence; | |
273 | ||
274 | error = vfs_readdir(file, filldir_one, &buffer); | |
275 | ||
276 | if (error < 0) | |
277 | break; | |
278 | ||
279 | error = 0; | |
280 | if (buffer.found) | |
281 | break; | |
282 | error = -ENOENT; | |
283 | if (old_seq == buffer.sequence) | |
284 | break; | |
285 | } | |
286 | ||
287 | out_close: | |
288 | fput(file); | |
289 | out: | |
290 | return error; | |
291 | } | |
292 | ||
1da177e4 LT |
293 | /** |
294 | * export_encode_fh - default export_operations->encode_fh function | |
295 | * @dentry: the dentry to encode | |
296 | * @fh: where to store the file handle fragment | |
297 | * @max_len: maximum length to store there | |
298 | * @connectable: whether to store parent information | |
299 | * | |
300 | * This default encode_fh function assumes that the 32 inode number | |
301 | * is suitable for locating an inode, and that the generation number | |
302 | * can be used to check that it is still valid. It places them in the | |
303 | * filehandle fragment where export_decode_fh expects to find them. | |
304 | */ | |
6e91ea2b CH |
305 | static int export_encode_fh(struct dentry *dentry, struct fid *fid, |
306 | int *max_len, int connectable) | |
1da177e4 LT |
307 | { |
308 | struct inode * inode = dentry->d_inode; | |
309 | int len = *max_len; | |
6e91ea2b | 310 | int type = FILEID_INO32_GEN; |
1da177e4 LT |
311 | |
312 | if (len < 2 || (connectable && len < 4)) | |
313 | return 255; | |
314 | ||
315 | len = 2; | |
6e91ea2b CH |
316 | fid->i32.ino = inode->i_ino; |
317 | fid->i32.gen = inode->i_generation; | |
1da177e4 LT |
318 | if (connectable && !S_ISDIR(inode->i_mode)) { |
319 | struct inode *parent; | |
320 | ||
321 | spin_lock(&dentry->d_lock); | |
322 | parent = dentry->d_parent->d_inode; | |
6e91ea2b CH |
323 | fid->i32.parent_ino = parent->i_ino; |
324 | fid->i32.parent_gen = parent->i_generation; | |
1da177e4 LT |
325 | spin_unlock(&dentry->d_lock); |
326 | len = 4; | |
6e91ea2b | 327 | type = FILEID_INO32_GEN_PARENT; |
1da177e4 LT |
328 | } |
329 | *max_len = len; | |
330 | return type; | |
331 | } | |
332 | ||
6e91ea2b | 333 | int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len, |
d37065cd CH |
334 | int connectable) |
335 | { | |
39655164 | 336 | const struct export_operations *nop = dentry->d_sb->s_export_op; |
10f11c34 | 337 | int error; |
d37065cd | 338 | |
10f11c34 | 339 | if (nop->encode_fh) |
6e91ea2b | 340 | error = nop->encode_fh(dentry, fid->raw, max_len, connectable); |
10f11c34 | 341 | else |
6e91ea2b | 342 | error = export_encode_fh(dentry, fid, max_len, connectable); |
10f11c34 CH |
343 | |
344 | return error; | |
d37065cd CH |
345 | } |
346 | EXPORT_SYMBOL_GPL(exportfs_encode_fh); | |
347 | ||
6e91ea2b CH |
348 | struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid, |
349 | int fh_len, int fileid_type, | |
350 | int (*acceptable)(void *, struct dentry *), void *context) | |
d37065cd | 351 | { |
39655164 | 352 | const struct export_operations *nop = mnt->mnt_sb->s_export_op; |
2596110a CH |
353 | struct dentry *result, *alias; |
354 | int err; | |
d37065cd | 355 | |
2596110a CH |
356 | /* |
357 | * Try to get any dentry for the given file handle from the filesystem. | |
358 | */ | |
359 | result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type); | |
360 | if (!result) | |
361 | result = ERR_PTR(-ESTALE); | |
362 | if (IS_ERR(result)) | |
363 | return result; | |
364 | ||
365 | if (S_ISDIR(result->d_inode->i_mode)) { | |
366 | /* | |
367 | * This request is for a directory. | |
368 | * | |
369 | * On the positive side there is only one dentry for each | |
370 | * directory inode. On the negative side this implies that we | |
371 | * to ensure our dentry is connected all the way up to the | |
372 | * filesystem root. | |
373 | */ | |
374 | if (result->d_flags & DCACHE_DISCONNECTED) { | |
375 | err = reconnect_path(mnt->mnt_sb, result); | |
376 | if (err) | |
377 | goto err_result; | |
378 | } | |
379 | ||
380 | if (!acceptable(context, result)) { | |
381 | err = -EACCES; | |
382 | goto err_result; | |
383 | } | |
384 | ||
385 | return result; | |
10f11c34 | 386 | } else { |
2596110a CH |
387 | /* |
388 | * It's not a directory. Life is a little more complicated. | |
389 | */ | |
390 | struct dentry *target_dir, *nresult; | |
391 | char nbuf[NAME_MAX+1]; | |
392 | ||
393 | /* | |
394 | * See if either the dentry we just got from the filesystem | |
395 | * or any alias for it is acceptable. This is always true | |
396 | * if this filesystem is exported without the subtreecheck | |
397 | * option. If the filesystem is exported with the subtree | |
398 | * check option there's a fair chance we need to look at | |
399 | * the parent directory in the file handle and make sure | |
400 | * it's connected to the filesystem root. | |
401 | */ | |
402 | alias = find_acceptable_alias(result, acceptable, context); | |
403 | if (alias) | |
404 | return alias; | |
405 | ||
406 | /* | |
407 | * Try to extract a dentry for the parent directory from the | |
408 | * file handle. If this fails we'll have to give up. | |
409 | */ | |
410 | err = -ESTALE; | |
411 | if (!nop->fh_to_parent) | |
412 | goto err_result; | |
413 | ||
414 | target_dir = nop->fh_to_parent(mnt->mnt_sb, fid, | |
415 | fh_len, fileid_type); | |
416 | if (!target_dir) | |
417 | goto err_result; | |
418 | err = PTR_ERR(target_dir); | |
419 | if (IS_ERR(target_dir)) | |
420 | goto err_result; | |
421 | ||
422 | /* | |
423 | * And as usual we need to make sure the parent directory is | |
424 | * connected to the filesystem root. The VFS really doesn't | |
425 | * like disconnected directories.. | |
426 | */ | |
427 | err = reconnect_path(mnt->mnt_sb, target_dir); | |
428 | if (err) { | |
429 | dput(target_dir); | |
430 | goto err_result; | |
431 | } | |
432 | ||
433 | /* | |
434 | * Now that we've got both a well-connected parent and a | |
435 | * dentry for the inode we're after, make sure that our | |
436 | * inode is actually connected to the parent. | |
437 | */ | |
438 | err = exportfs_get_name(target_dir, nbuf, result); | |
439 | if (!err) { | |
440 | mutex_lock(&target_dir->d_inode->i_mutex); | |
441 | nresult = lookup_one_len(nbuf, target_dir, | |
442 | strlen(nbuf)); | |
443 | mutex_unlock(&target_dir->d_inode->i_mutex); | |
444 | if (!IS_ERR(nresult)) { | |
445 | if (nresult->d_inode) { | |
446 | dput(result); | |
447 | result = nresult; | |
448 | } else | |
449 | dput(nresult); | |
450 | } | |
451 | } | |
452 | ||
453 | /* | |
454 | * At this point we are done with the parent, but it's pinned | |
455 | * by the child dentry anyway. | |
456 | */ | |
457 | dput(target_dir); | |
458 | ||
459 | /* | |
460 | * And finally make sure the dentry is actually acceptable | |
461 | * to NFSD. | |
462 | */ | |
463 | alias = find_acceptable_alias(result, acceptable, context); | |
464 | if (!alias) { | |
465 | err = -EACCES; | |
466 | goto err_result; | |
467 | } | |
468 | ||
469 | return alias; | |
10f11c34 CH |
470 | } |
471 | ||
2596110a CH |
472 | err_result: |
473 | dput(result); | |
474 | return ERR_PTR(err); | |
d37065cd CH |
475 | } |
476 | EXPORT_SYMBOL_GPL(exportfs_decode_fh); | |
477 | ||
1da177e4 | 478 | MODULE_LICENSE("GPL"); |