fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / fs / exportfs / expfs.c
blob8adb32a9387a3e6cb76ba0cf80b9873d51dad3bd
2 #include <linux/exportfs.h>
3 #include <linux/fs.h>
4 #include <linux/file.h>
5 #include <linux/module.h>
6 #include <linux/mount.h>
7 #include <linux/namei.h>
9 #define dprintk(fmt, args...) do{}while(0)
12 static int get_name(struct dentry *dentry, char *name,
13 struct dentry *child);
16 static struct dentry *exportfs_get_dentry(struct super_block *sb, void *obj)
18 struct dentry *result = ERR_PTR(-ESTALE);
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);
26 return result;
29 static int exportfs_get_name(struct dentry *dir, char *name,
30 struct dentry *child)
32 struct export_operations *nop = dir->d_sb->s_export_op;
34 if (nop->get_name)
35 return nop->get_name(dir, name, child);
36 else
37 return get_name(dir, name, child);
41 * Check if the dentry or any of it's aliases is acceptable.
43 static struct dentry *
44 find_acceptable_alias(struct dentry *result,
45 int (*acceptable)(void *context, struct dentry *dentry),
46 void *context)
48 struct dentry *dentry, *toput = NULL;
50 if (acceptable(context, result))
51 return result;
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;
63 spin_lock(&dcache_lock);
64 toput = dentry;
66 spin_unlock(&dcache_lock);
68 if (toput)
69 dput(toput);
70 return NULL;
74 * Find root of a disconnected subtree and return a reference to it.
76 static struct dentry *
77 find_disconnected_root(struct dentry *dentry)
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);
90 spin_unlock(&dentry->d_lock);
91 return dentry;
96 * Make sure target_dir is fully connected to the dentry tree.
98 * It may already be, as the flag isn't always updated when connection happens.
100 static int
101 reconnect_path(struct super_block *sb, struct dentry *target_dir)
103 char nbuf[NAME_MAX+1];
104 int noprogress = 0;
105 int err = -ESTALE;
108 * It is possible that a confused file system might not let us complete
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
115 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
116 struct dentry *pd = find_disconnected_root(target_dir);
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 {
132 * We have hit the top of a disconnected path, try to
133 * find parent and connect.
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
140 * Getting the parent can't be supported generically,
141 * the locking is too icky.
143 * Instead we just return EACCES. If server reboots
144 * or inodes get flushed, you lose
146 struct dentry *ppd = ERR_PTR(-EACCES);
147 struct dentry *npd;
149 mutex_lock(&pd->d_inode->i_mutex);
150 if (sb->s_export_op->get_parent)
151 ppd = sb->s_export_op->get_parent(pd);
152 mutex_unlock(&pd->d_inode->i_mutex);
154 if (IS_ERR(ppd)) {
155 err = PTR_ERR(ppd);
156 dprintk("%s: get_parent of %ld failed, err %d\n",
157 __FUNCTION__, pd->d_inode->i_ino, err);
158 dput(pd);
159 break;
162 dprintk("%s: find name of %lu in %lu\n", __FUNCTION__,
163 pd->d_inode->i_ino, ppd->d_inode->i_ino);
164 err = exportfs_get_name(ppd, nbuf, pd);
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
172 continue;
173 break;
175 dprintk("%s: found name: %s\n", __FUNCTION__, nbuf);
176 mutex_lock(&ppd->d_inode->i_mutex);
177 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
178 mutex_unlock(&ppd->d_inode->i_mutex);
179 if (IS_ERR(npd)) {
180 err = PTR_ERR(npd);
181 dprintk("%s: lookup failed: %d\n",
182 __FUNCTION__, err);
183 dput(ppd);
184 dput(pd);
185 break;
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
192 if (npd == pd)
193 noprogress = 0;
194 else
195 printk("%s: npd != pd\n", __FUNCTION__);
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;
204 dput(pd);
207 if (target_dir->d_flags & DCACHE_DISCONNECTED) {
208 /* something went wrong - oh-well */
209 if (!err)
210 err = -ESTALE;
211 return err;
214 return 0;
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.
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.
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.
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.
249 struct dentry *
250 find_exported_dentry(struct super_block *sb, void *obj, void *parent,
251 int (*acceptable)(void *context, struct dentry *de),
252 void *context)
254 struct dentry *result, *alias;
255 int err = -ESTALE;
258 * Attempt to find the inode.
260 result = exportfs_get_dentry(sb, obj);
261 if (IS_ERR(result))
262 return result;
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;
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];
279 alias = find_acceptable_alias(result, acceptable, context);
280 if (alias)
281 return alias;
283 if (parent == NULL)
284 goto err_result;
286 target_dir = exportfs_get_dentry(sb,parent);
287 if (IS_ERR(target_dir)) {
288 err = PTR_ERR(target_dir);
289 goto err_result;
292 err = reconnect_path(sb, target_dir);
293 if (err) {
294 dput(target_dir);
295 goto err_result;
299 * As we weren't after a directory, have one more step to go.
301 err = exportfs_get_name(target_dir, nbuf, result);
302 if (!err) {
303 mutex_lock(&target_dir->d_inode->i_mutex);
304 nresult = lookup_one_len(nbuf, target_dir,
305 strlen(nbuf));
306 mutex_unlock(&target_dir->d_inode->i_mutex);
307 if (!IS_ERR(nresult)) {
308 if (nresult->d_inode) {
309 dput(result);
310 result = nresult;
311 } else
312 dput(nresult);
315 dput(target_dir);
318 alias = find_acceptable_alias(result, acceptable, context);
319 if (alias)
320 return alias;
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
326 * and it may just be a permission problem, so returning
327 * -EACCESS is safer
329 return ERR_PTR(-EACCES);
331 err_result:
332 dput(result);
333 return ERR_PTR(err);
336 struct 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 */
345 * A rather strange filldir function to capture
346 * the name matching the specified inode number.
348 static int filldir_one(void * __buf, const char * name, int len,
349 loff_t pos, u64 ino, unsigned int d_type)
351 struct getdents_callback *buf = __buf;
352 int result = 0;
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;
361 return result;
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.
370 * calls readdir on the parent until it finds an entry with
371 * the same inode number as the child, and returns that.
373 static int get_name(struct dentry *dentry, char *name,
374 struct dentry *child)
376 struct inode *dir = dentry->d_inode;
377 int error;
378 struct file *file;
379 struct getdents_callback buffer;
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;
388 * Open the directory ...
390 file = dentry_open(dget(dentry), NULL, O_RDONLY);
391 error = PTR_ERR(file);
392 if (IS_ERR(file))
393 goto out;
395 error = -EINVAL;
396 if (!file->f_op->readdir)
397 goto out_close;
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;
406 error = vfs_readdir(file, filldir_one, &buffer);
408 if (error < 0)
409 break;
411 error = 0;
412 if (buffer.found)
413 break;
414 error = -ENOENT;
415 if (old_seq == buffer.sequence)
416 break;
419 out_close:
420 fput(file);
421 out:
422 return error;
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
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.
437 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
438 int connectable)
440 struct inode * inode = dentry->d_inode;
441 int len = *max_len;
442 int type = 1;
444 if (len < 2 || (connectable && len < 4))
445 return 255;
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;
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;
461 *max_len = len;
462 return type;
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
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.
480 static 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)
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];
493 return find_exported_dentry(sb, fh, parent,
494 acceptable, context);
497 int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
498 int connectable)
500 struct export_operations *nop = dentry->d_sb->s_export_op;
501 int error;
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);
508 return error;
510 EXPORT_SYMBOL_GPL(exportfs_encode_fh);
512 struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
513 int fileid_type, int (*acceptable)(void *, struct dentry *),
514 void *context)
516 struct export_operations *nop = mnt->mnt_sb->s_export_op;
517 struct dentry *result;
519 if (nop->decode_fh) {
520 result = nop->decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
521 acceptable, context);
522 } else {
523 result = export_decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
524 acceptable, context);
527 return result;
529 EXPORT_SYMBOL_GPL(exportfs_decode_fh);
531 EXPORT_SYMBOL(find_exported_dentry);
533 MODULE_LICENSE("GPL");