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