]>
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 | ||
16 | static struct dentry *exportfs_get_dentry(struct super_block *sb, void *obj) | |
17 | { | |
18 | struct dentry *result = ERR_PTR(-ESTALE); | |
19 | ||
20 | if (sb->s_export_op->get_dentry) { | |
21 | result = sb->s_export_op->get_dentry(sb, obj); | |
22 | if (!result) | |
23 | result = ERR_PTR(-ESTALE); | |
24 | } | |
25 | ||
26 | return result; | |
27 | } | |
28 | ||
29 | static int exportfs_get_name(struct dentry *dir, char *name, | |
30 | struct dentry *child) | |
31 | { | |
32 | struct export_operations *nop = dir->d_sb->s_export_op; | |
33 | ||
34 | if (nop->get_name) | |
35 | return nop->get_name(dir, name, child); | |
36 | else | |
37 | return get_name(dir, name, child); | |
38 | } | |
1da177e4 | 39 | |
fb66a198 CH |
40 | /* |
41 | * Check if the dentry or any of it's aliases is acceptable. | |
42 | */ | |
e2f99018 CH |
43 | static struct dentry * |
44 | find_acceptable_alias(struct dentry *result, | |
45 | int (*acceptable)(void *context, struct dentry *dentry), | |
46 | void *context) | |
47 | { | |
48 | struct dentry *dentry, *toput = NULL; | |
49 | ||
fb66a198 CH |
50 | if (acceptable(context, result)) |
51 | return result; | |
52 | ||
e2f99018 CH |
53 | spin_lock(&dcache_lock); |
54 | list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) { | |
55 | dget_locked(dentry); | |
56 | spin_unlock(&dcache_lock); | |
57 | if (toput) | |
58 | dput(toput); | |
59 | if (dentry != result && acceptable(context, dentry)) { | |
60 | dput(result); | |
61 | return dentry; | |
62 | } | |
63 | spin_lock(&dcache_lock); | |
64 | toput = dentry; | |
65 | } | |
66 | spin_unlock(&dcache_lock); | |
67 | ||
68 | if (toput) | |
69 | dput(toput); | |
70 | return NULL; | |
71 | } | |
72 | ||
dd90b509 CH |
73 | /* |
74 | * Find root of a disconnected subtree and return a reference to it. | |
75 | */ | |
76 | static struct dentry * | |
77 | find_disconnected_root(struct dentry *dentry) | |
78 | { | |
79 | dget(dentry); | |
80 | spin_lock(&dentry->d_lock); | |
81 | while (!IS_ROOT(dentry) && | |
82 | (dentry->d_parent->d_flags & DCACHE_DISCONNECTED)) { | |
83 | struct dentry *parent = dentry->d_parent; | |
84 | dget(parent); | |
85 | spin_unlock(&dentry->d_lock); | |
86 | dput(dentry); | |
87 | dentry = parent; | |
88 | spin_lock(&dentry->d_lock); | |
89 | } | |
90 | spin_unlock(&dentry->d_lock); | |
91 | return dentry; | |
92 | } | |
93 | ||
1da177e4 LT |
94 | /** |
95 | * find_exported_dentry - helper routine to implement export_operations->decode_fh | |
96 | * @sb: The &super_block identifying the filesystem | |
97 | * @obj: An opaque identifier of the object to be found - passed to | |
98 | * get_inode | |
99 | * @parent: An optional opqaue identifier of the parent of the object. | |
100 | * @acceptable: A function used to test possible &dentries to see if they are | |
101 | * acceptable | |
102 | * @context: A parameter to @acceptable so that it knows on what basis to | |
103 | * judge. | |
104 | * | |
105 | * find_exported_dentry is the central helper routine to enable file systems | |
106 | * to provide the decode_fh() export_operation. It's main task is to take | |
107 | * an &inode, find or create an appropriate &dentry structure, and possibly | |
108 | * splice this into the dcache in the correct place. | |
109 | * | |
110 | * The decode_fh() operation provided by the filesystem should call | |
111 | * find_exported_dentry() with the same parameters that it received except | |
112 | * that instead of the file handle fragment, pointers to opaque identifiers | |
113 | * for the object and optionally its parent are passed. The default decode_fh | |
114 | * routine passes one pointer to the start of the filehandle fragment, and | |
115 | * one 8 bytes into the fragment. It is expected that most filesystems will | |
116 | * take this approach, though the offset to the parent identifier may well be | |
117 | * different. | |
118 | * | |
119 | * find_exported_dentry() will call get_dentry to get an dentry pointer from | |
120 | * the file system. If any &dentry in the d_alias list is acceptable, it will | |
121 | * be returned. Otherwise find_exported_dentry() will attempt to splice a new | |
122 | * &dentry into the dcache using get_name() and get_parent() to find the | |
123 | * appropriate place. | |
124 | */ | |
125 | ||
126 | struct dentry * | |
127 | find_exported_dentry(struct super_block *sb, void *obj, void *parent, | |
128 | int (*acceptable)(void *context, struct dentry *de), | |
129 | void *context) | |
130 | { | |
131 | struct dentry *result = NULL; | |
132 | struct dentry *target_dir; | |
10f11c34 | 133 | int err = -ESTALE; |
1da177e4 | 134 | struct export_operations *nops = sb->s_export_op; |
e2f99018 | 135 | struct dentry *alias; |
1da177e4 LT |
136 | int noprogress; |
137 | char nbuf[NAME_MAX+1]; | |
138 | ||
139 | /* | |
140 | * Attempt to find the inode. | |
141 | */ | |
10f11c34 CH |
142 | result = exportfs_get_dentry(sb, obj); |
143 | if (IS_ERR(result)) | |
144 | return result; | |
145 | ||
d7dd618a CH |
146 | if (S_ISDIR(result->d_inode->i_mode)) { |
147 | if (!(result->d_flags & DCACHE_DISCONNECTED)) { | |
148 | if (acceptable(context, result)) | |
149 | return result; | |
8c7b389e | 150 | err = -EACCES; |
1da177e4 LT |
151 | goto err_result; |
152 | } | |
e2f99018 | 153 | |
d7dd618a CH |
154 | target_dir = dget(result); |
155 | } else { | |
e2f99018 CH |
156 | alias = find_acceptable_alias(result, acceptable, context); |
157 | if (alias) | |
158 | return alias; | |
1da177e4 | 159 | |
1da177e4 LT |
160 | if (parent == NULL) |
161 | goto err_result; | |
162 | ||
10f11c34 CH |
163 | target_dir = exportfs_get_dentry(sb,parent); |
164 | if (IS_ERR(target_dir)) { | |
1da177e4 | 165 | err = PTR_ERR(target_dir); |
1da177e4 | 166 | goto err_result; |
10f11c34 | 167 | } |
1da177e4 | 168 | } |
d7dd618a | 169 | |
1da177e4 LT |
170 | /* |
171 | * Now we need to make sure that target_dir is properly connected. | |
172 | * It may already be, as the flag isn't always updated when connection | |
173 | * happens. | |
174 | * So, we walk up parent links until we find a connected directory, | |
175 | * or we run out of directories. Then we find the parent, find | |
176 | * the name of the child in that parent, and do a lookup. | |
177 | * This should connect the child into the parent | |
178 | * We then repeat. | |
179 | */ | |
180 | ||
181 | /* it is possible that a confused file system might not let us complete | |
182 | * the path to the root. For example, if get_parent returns a directory | |
183 | * in which we cannot find a name for the child. While this implies a | |
184 | * very sick filesystem we don't want it to cause knfsd to spin. Hence | |
185 | * the noprogress counter. If we go through the loop 10 times (2 is | |
186 | * probably enough) without getting anywhere, we just give up | |
187 | */ | |
dd90b509 | 188 | noprogress = 0; |
1da177e4 | 189 | while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) { |
dd90b509 | 190 | struct dentry *pd = find_disconnected_root(target_dir); |
1da177e4 LT |
191 | |
192 | if (!IS_ROOT(pd)) { | |
193 | /* must have found a connected parent - great */ | |
194 | spin_lock(&pd->d_lock); | |
195 | pd->d_flags &= ~DCACHE_DISCONNECTED; | |
196 | spin_unlock(&pd->d_lock); | |
197 | noprogress = 0; | |
198 | } else if (pd == sb->s_root) { | |
199 | printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n"); | |
200 | spin_lock(&pd->d_lock); | |
201 | pd->d_flags &= ~DCACHE_DISCONNECTED; | |
202 | spin_unlock(&pd->d_lock); | |
203 | noprogress = 0; | |
204 | } else { | |
10f11c34 CH |
205 | /* |
206 | * We have hit the top of a disconnected path, try to | |
207 | * find parent and connect. | |
208 | * | |
209 | * Racing with some other process renaming a directory | |
210 | * isn't much of a problem here. If someone renames | |
211 | * the directory, it will end up properly connected, | |
212 | * which is what we want | |
213 | * | |
214 | * Getting the parent can't be supported generically, | |
215 | * the locking is too icky. | |
216 | * | |
217 | * Instead we just return EACCES. If server reboots | |
218 | * or inodes get flushed, you lose | |
1da177e4 | 219 | */ |
10f11c34 | 220 | struct dentry *ppd = ERR_PTR(-EACCES); |
1da177e4 LT |
221 | struct dentry *npd; |
222 | ||
1b1dcc1b | 223 | mutex_lock(&pd->d_inode->i_mutex); |
10f11c34 CH |
224 | if (nops->get_parent) |
225 | ppd = nops->get_parent(pd); | |
1b1dcc1b | 226 | mutex_unlock(&pd->d_inode->i_mutex); |
1da177e4 LT |
227 | |
228 | if (IS_ERR(ppd)) { | |
229 | err = PTR_ERR(ppd); | |
230 | dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n", | |
231 | pd->d_inode->i_ino, err); | |
232 | dput(pd); | |
233 | break; | |
234 | } | |
235 | dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino); | |
10f11c34 | 236 | err = exportfs_get_name(ppd, nbuf, pd); |
1da177e4 LT |
237 | if (err) { |
238 | dput(ppd); | |
239 | dput(pd); | |
240 | if (err == -ENOENT) | |
241 | /* some race between get_parent and | |
242 | * get_name? just try again | |
243 | */ | |
244 | continue; | |
245 | break; | |
246 | } | |
247 | dprintk("find_exported_dentry: found name: %s\n", nbuf); | |
1b1dcc1b | 248 | mutex_lock(&ppd->d_inode->i_mutex); |
1da177e4 | 249 | npd = lookup_one_len(nbuf, ppd, strlen(nbuf)); |
1b1dcc1b | 250 | mutex_unlock(&ppd->d_inode->i_mutex); |
1da177e4 LT |
251 | if (IS_ERR(npd)) { |
252 | err = PTR_ERR(npd); | |
253 | dprintk("find_exported_dentry: lookup failed: %d\n", err); | |
254 | dput(ppd); | |
255 | dput(pd); | |
256 | break; | |
257 | } | |
258 | /* we didn't really want npd, we really wanted | |
259 | * a side-effect of the lookup. | |
260 | * hopefully, npd == pd, though it isn't really | |
261 | * a problem if it isn't | |
262 | */ | |
263 | if (npd == pd) | |
264 | noprogress = 0; | |
265 | else | |
266 | printk("find_exported_dentry: npd != pd\n"); | |
267 | dput(npd); | |
268 | dput(ppd); | |
269 | if (IS_ROOT(pd)) { | |
270 | /* something went wrong, we have to give up */ | |
271 | dput(pd); | |
272 | break; | |
273 | } | |
274 | } | |
275 | dput(pd); | |
276 | } | |
277 | ||
278 | if (target_dir->d_flags & DCACHE_DISCONNECTED) { | |
279 | /* something went wrong - oh-well */ | |
280 | if (!err) | |
281 | err = -ESTALE; | |
282 | goto err_target; | |
283 | } | |
284 | /* if we weren't after a directory, have one more step to go */ | |
285 | if (result != target_dir) { | |
286 | struct dentry *nresult; | |
10f11c34 | 287 | err = exportfs_get_name(target_dir, nbuf, result); |
1da177e4 | 288 | if (!err) { |
1b1dcc1b | 289 | mutex_lock(&target_dir->d_inode->i_mutex); |
1da177e4 | 290 | nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf)); |
1b1dcc1b | 291 | mutex_unlock(&target_dir->d_inode->i_mutex); |
1da177e4 LT |
292 | if (!IS_ERR(nresult)) { |
293 | if (nresult->d_inode) { | |
294 | dput(result); | |
295 | result = nresult; | |
296 | } else | |
297 | dput(nresult); | |
298 | } | |
299 | } | |
300 | } | |
301 | dput(target_dir); | |
e2f99018 CH |
302 | |
303 | alias = find_acceptable_alias(result, acceptable, context); | |
304 | if (alias) | |
305 | return alias; | |
1da177e4 LT |
306 | |
307 | /* drat - I just cannot find anything acceptable */ | |
308 | dput(result); | |
309 | /* It might be justifiable to return ESTALE here, | |
310 | * but the filehandle at-least looks reasonable good | |
311 | * and it just be a permission problem, so returning | |
312 | * -EACCESS is safer | |
313 | */ | |
314 | return ERR_PTR(-EACCES); | |
315 | ||
316 | err_target: | |
317 | dput(target_dir); | |
318 | err_result: | |
319 | dput(result); | |
1da177e4 LT |
320 | return ERR_PTR(err); |
321 | } | |
322 | ||
1da177e4 LT |
323 | struct getdents_callback { |
324 | char *name; /* name that was found. It already points to a | |
325 | buffer NAME_MAX+1 is size */ | |
326 | unsigned long ino; /* the inum we are looking for */ | |
327 | int found; /* inode matched? */ | |
328 | int sequence; /* sequence counter */ | |
329 | }; | |
330 | ||
331 | /* | |
332 | * A rather strange filldir function to capture | |
333 | * the name matching the specified inode number. | |
334 | */ | |
335 | static int filldir_one(void * __buf, const char * name, int len, | |
afefdbb2 | 336 | loff_t pos, u64 ino, unsigned int d_type) |
1da177e4 LT |
337 | { |
338 | struct getdents_callback *buf = __buf; | |
339 | int result = 0; | |
340 | ||
341 | buf->sequence++; | |
342 | if (buf->ino == ino) { | |
343 | memcpy(buf->name, name, len); | |
344 | buf->name[len] = '\0'; | |
345 | buf->found = 1; | |
346 | result = -1; | |
347 | } | |
348 | return result; | |
349 | } | |
350 | ||
351 | /** | |
352 | * get_name - default export_operations->get_name function | |
353 | * @dentry: the directory in which to find a name | |
354 | * @name: a pointer to a %NAME_MAX+1 char buffer to store the name | |
355 | * @child: the dentry for the child directory. | |
356 | * | |
357 | * calls readdir on the parent until it finds an entry with | |
358 | * the same inode number as the child, and returns that. | |
359 | */ | |
360 | static int get_name(struct dentry *dentry, char *name, | |
361 | struct dentry *child) | |
362 | { | |
363 | struct inode *dir = dentry->d_inode; | |
364 | int error; | |
365 | struct file *file; | |
366 | struct getdents_callback buffer; | |
367 | ||
368 | error = -ENOTDIR; | |
369 | if (!dir || !S_ISDIR(dir->i_mode)) | |
370 | goto out; | |
371 | error = -EINVAL; | |
372 | if (!dir->i_fop) | |
373 | goto out; | |
374 | /* | |
375 | * Open the directory ... | |
376 | */ | |
377 | file = dentry_open(dget(dentry), NULL, O_RDONLY); | |
378 | error = PTR_ERR(file); | |
379 | if (IS_ERR(file)) | |
380 | goto out; | |
381 | ||
382 | error = -EINVAL; | |
383 | if (!file->f_op->readdir) | |
384 | goto out_close; | |
385 | ||
386 | buffer.name = name; | |
387 | buffer.ino = child->d_inode->i_ino; | |
388 | buffer.found = 0; | |
389 | buffer.sequence = 0; | |
390 | while (1) { | |
391 | int old_seq = buffer.sequence; | |
392 | ||
393 | error = vfs_readdir(file, filldir_one, &buffer); | |
394 | ||
395 | if (error < 0) | |
396 | break; | |
397 | ||
398 | error = 0; | |
399 | if (buffer.found) | |
400 | break; | |
401 | error = -ENOENT; | |
402 | if (old_seq == buffer.sequence) | |
403 | break; | |
404 | } | |
405 | ||
406 | out_close: | |
407 | fput(file); | |
408 | out: | |
409 | return error; | |
410 | } | |
411 | ||
1da177e4 LT |
412 | /** |
413 | * export_encode_fh - default export_operations->encode_fh function | |
414 | * @dentry: the dentry to encode | |
415 | * @fh: where to store the file handle fragment | |
416 | * @max_len: maximum length to store there | |
417 | * @connectable: whether to store parent information | |
418 | * | |
419 | * This default encode_fh function assumes that the 32 inode number | |
420 | * is suitable for locating an inode, and that the generation number | |
421 | * can be used to check that it is still valid. It places them in the | |
422 | * filehandle fragment where export_decode_fh expects to find them. | |
423 | */ | |
424 | static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len, | |
425 | int connectable) | |
426 | { | |
427 | struct inode * inode = dentry->d_inode; | |
428 | int len = *max_len; | |
429 | int type = 1; | |
430 | ||
431 | if (len < 2 || (connectable && len < 4)) | |
432 | return 255; | |
433 | ||
434 | len = 2; | |
435 | fh[0] = inode->i_ino; | |
436 | fh[1] = inode->i_generation; | |
437 | if (connectable && !S_ISDIR(inode->i_mode)) { | |
438 | struct inode *parent; | |
439 | ||
440 | spin_lock(&dentry->d_lock); | |
441 | parent = dentry->d_parent->d_inode; | |
442 | fh[2] = parent->i_ino; | |
443 | fh[3] = parent->i_generation; | |
444 | spin_unlock(&dentry->d_lock); | |
445 | len = 4; | |
446 | type = 2; | |
447 | } | |
448 | *max_len = len; | |
449 | return type; | |
450 | } | |
451 | ||
452 | ||
453 | /** | |
454 | * export_decode_fh - default export_operations->decode_fh function | |
455 | * @sb: The superblock | |
456 | * @fh: pointer to the file handle fragment | |
457 | * @fh_len: length of file handle fragment | |
458 | * @acceptable: function for testing acceptability of dentrys | |
459 | * @context: context for @acceptable | |
460 | * | |
461 | * This is the default decode_fh() function. | |
462 | * a fileid_type of 1 indicates that the filehandlefragment | |
463 | * just contains an object identifier understood by get_dentry. | |
464 | * a fileid_type of 2 says that there is also a directory | |
465 | * identifier 8 bytes in to the filehandlefragement. | |
466 | */ | |
467 | static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len, | |
468 | int fileid_type, | |
469 | int (*acceptable)(void *context, struct dentry *de), | |
470 | void *context) | |
471 | { | |
472 | __u32 parent[2]; | |
473 | parent[0] = parent[1] = 0; | |
474 | if (fh_len < 2 || fileid_type > 2) | |
475 | return NULL; | |
476 | if (fileid_type == 2) { | |
477 | if (fh_len > 2) parent[0] = fh[2]; | |
478 | if (fh_len > 3) parent[1] = fh[3]; | |
479 | } | |
480 | return find_exported_dentry(sb, fh, parent, | |
481 | acceptable, context); | |
482 | } | |
483 | ||
d37065cd CH |
484 | int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len, |
485 | int connectable) | |
486 | { | |
10f11c34 CH |
487 | struct export_operations *nop = dentry->d_sb->s_export_op; |
488 | int error; | |
d37065cd | 489 | |
10f11c34 CH |
490 | if (nop->encode_fh) |
491 | error = nop->encode_fh(dentry, fh, max_len, connectable); | |
492 | else | |
493 | error = export_encode_fh(dentry, fh, max_len, connectable); | |
494 | ||
495 | return error; | |
d37065cd CH |
496 | } |
497 | EXPORT_SYMBOL_GPL(exportfs_encode_fh); | |
498 | ||
499 | struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len, | |
500 | int fileid_type, int (*acceptable)(void *, struct dentry *), | |
501 | void *context) | |
502 | { | |
503 | struct export_operations *nop = mnt->mnt_sb->s_export_op; | |
10f11c34 | 504 | struct dentry *result; |
d37065cd | 505 | |
10f11c34 CH |
506 | if (nop->decode_fh) { |
507 | result = nop->decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type, | |
d37065cd | 508 | acceptable, context); |
10f11c34 CH |
509 | } else { |
510 | result = export_decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type, | |
511 | acceptable, context); | |
512 | } | |
513 | ||
514 | return result; | |
d37065cd CH |
515 | } |
516 | EXPORT_SYMBOL_GPL(exportfs_decode_fh); | |
517 | ||
1da177e4 LT |
518 | EXPORT_SYMBOL(find_exported_dentry); |
519 | ||
520 | MODULE_LICENSE("GPL"); |