blob: e68f0279f4b07ccf57e2827a149ba18b7d926800 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
Christoph Hellwiga5694252007-07-17 04:04:28 -07002#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/fs.h>
4#include <linux/file.h>
5#include <linux/module.h>
Christoph Hellwigd37065c2007-07-17 04:04:30 -07006#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/namei.h>
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#define dprintk(fmt, args...) do{}while(0)
10
Christoph Hellwig10f11c32007-07-17 04:04:31 -070011
12static int get_name(struct dentry *dentry, char *name,
13 struct dentry *child);
14
15
Christoph Hellwig10f11c32007-07-17 04:04:31 -070016static int exportfs_get_name(struct dentry *dir, char *name,
17 struct dentry *child)
18{
19 struct export_operations *nop = dir->d_sb->s_export_op;
20
21 if (nop->get_name)
22 return nop->get_name(dir, name, child);
23 else
24 return get_name(dir, name, child);
25}
26
Christoph Hellwigfb66a192007-07-17 04:04:32 -070027/*
28 * Check if the dentry or any of it's aliases is acceptable.
29 */
Christoph Hellwige2f99012006-01-18 17:43:52 -080030static struct dentry *
31find_acceptable_alias(struct dentry *result,
32 int (*acceptable)(void *context, struct dentry *dentry),
33 void *context)
34{
35 struct dentry *dentry, *toput = NULL;
36
Christoph Hellwigfb66a192007-07-17 04:04:32 -070037 if (acceptable(context, result))
38 return result;
39
Christoph Hellwige2f99012006-01-18 17:43:52 -080040 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
Christoph Hellwigdd90b502007-07-17 04:04:32 -070060/*
61 * Find root of a disconnected subtree and return a reference to it.
62 */
63static struct dentry *
64find_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
Christoph Hellwig019ab802007-07-17 04:04:33 -070081
82/*
83 * Make sure target_dir is fully connected to the dentry tree.
84 *
85 * It may already be, as the flag isn't always updated when connection happens.
86 */
87static int
88reconnect_path(struct super_block *sb, struct dentry *target_dir)
89{
90 char nbuf[NAME_MAX+1];
91 int noprogress = 0;
92 int err = -ESTALE;
93
94 /*
95 * It is possible that a confused file system might not let us complete
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 */
102 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
103 struct dentry *pd = find_disconnected_root(target_dir);
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 {
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
132 */
133 struct dentry *ppd = ERR_PTR(-EACCES);
134 struct dentry *npd;
135
136 mutex_lock(&pd->d_inode->i_mutex);
137 if (sb->s_export_op->get_parent)
138 ppd = sb->s_export_op->get_parent(pd);
139 mutex_unlock(&pd->d_inode->i_mutex);
140
141 if (IS_ERR(ppd)) {
142 err = PTR_ERR(ppd);
143 dprintk("%s: get_parent of %ld failed, err %d\n",
144 __FUNCTION__, pd->d_inode->i_ino, err);
145 dput(pd);
146 break;
147 }
148
149 dprintk("%s: find name of %lu in %lu\n", __FUNCTION__,
150 pd->d_inode->i_ino, ppd->d_inode->i_ino);
151 err = exportfs_get_name(ppd, nbuf, pd);
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 }
162 dprintk("%s: found name: %s\n", __FUNCTION__, nbuf);
163 mutex_lock(&ppd->d_inode->i_mutex);
164 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
165 mutex_unlock(&ppd->d_inode->i_mutex);
166 if (IS_ERR(npd)) {
167 err = PTR_ERR(npd);
168 dprintk("%s: lookup failed: %d\n",
169 __FUNCTION__, err);
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
182 printk("%s: npd != pd\n", __FUNCTION__);
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;
198 return err;
199 }
200
201 return 0;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204struct 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 */
216static int filldir_one(void * __buf, const char * name, int len,
David Howellsafefdbb2006-10-03 01:13:46 -0700217 loff_t pos, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
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 */
241static 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
287out_close:
288 fput(file);
289out:
290 return error;
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/**
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 */
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700305static int export_encode_fh(struct dentry *dentry, struct fid *fid,
306 int *max_len, int connectable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct inode * inode = dentry->d_inode;
309 int len = *max_len;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700310 int type = FILEID_INO32_GEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 if (len < 2 || (connectable && len < 4))
313 return 255;
314
315 len = 2;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700316 fid->i32.ino = inode->i_ino;
317 fid->i32.gen = inode->i_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 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;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700323 fid->i32.parent_ino = parent->i_ino;
324 fid->i32.parent_gen = parent->i_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 spin_unlock(&dentry->d_lock);
326 len = 4;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700327 type = FILEID_INO32_GEN_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 *max_len = len;
330 return type;
331}
332
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700333int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700334 int connectable)
335{
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700336 struct export_operations *nop = dentry->d_sb->s_export_op;
337 int error;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700338
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700339 if (nop->encode_fh)
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700340 error = nop->encode_fh(dentry, fid->raw, max_len, connectable);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700341 else
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700342 error = export_encode_fh(dentry, fid, max_len, connectable);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700343
344 return error;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700345}
346EXPORT_SYMBOL_GPL(exportfs_encode_fh);
347
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700348struct 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)
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700351{
352 struct export_operations *nop = mnt->mnt_sb->s_export_op;
Christoph Hellwig25961102007-10-21 16:42:05 -0700353 struct dentry *result, *alias;
354 int err;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700355
Christoph Hellwig25961102007-10-21 16:42:05 -0700356 /*
Christoph Hellwig25961102007-10-21 16:42:05 -0700357 * 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;
386 } else {
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;
470 }
471
472 err_result:
473 dput(result);
474 return ERR_PTR(err);
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700475}
476EXPORT_SYMBOL_GPL(exportfs_decode_fh);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478MODULE_LICENSE("GPL");