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