i386-resolve-dependency-of-asm-i386-pgtableh-on-highmemh
[linux-2.6/linux-trees-mm.git] / fs / unionfs / main.c
blobffb0da1c4df24cd29228a61ce515c7fdb7baded5
1 /*
2 * Copyright (c) 2003-2007 Erez Zadok
3 * Copyright (c) 2003-2006 Charles P. Wright
4 * Copyright (c) 2005-2007 Josef 'Jeff' Sipek
5 * Copyright (c) 2005-2006 Junjiro Okajima
6 * Copyright (c) 2005 Arun M. Krishnakumar
7 * Copyright (c) 2004-2006 David P. Quigley
8 * Copyright (c) 2003-2004 Mohammad Nayyer Zubair
9 * Copyright (c) 2003 Puja Gupta
10 * Copyright (c) 2003 Harikesavan Krishnan
11 * Copyright (c) 2003-2007 Stony Brook University
12 * Copyright (c) 2003-2007 The Research Foundation of SUNY
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include "union.h"
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
23 static void unionfs_fill_inode(struct dentry *dentry,
24 struct inode *inode)
26 struct inode *lower_inode;
27 struct dentry *lower_dentry;
28 int bindex, bstart, bend;
30 bstart = dbstart(dentry);
31 bend = dbend(dentry);
33 for (bindex = bstart; bindex <= bend; bindex++) {
34 lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
35 if (!lower_dentry) {
36 unionfs_set_lower_inode_idx(inode, bindex, NULL);
37 continue;
40 /* Initialize the lower inode to the new lower inode. */
41 if (!lower_dentry->d_inode)
42 continue;
44 unionfs_set_lower_inode_idx(inode, bindex,
45 igrab(lower_dentry->d_inode));
48 ibstart(inode) = dbstart(dentry);
49 ibend(inode) = dbend(dentry);
51 /* Use attributes from the first branch. */
52 lower_inode = unionfs_lower_inode(inode);
54 /* Use different set of inode ops for symlinks & directories */
55 if (S_ISLNK(lower_inode->i_mode))
56 inode->i_op = &unionfs_symlink_iops;
57 else if (S_ISDIR(lower_inode->i_mode))
58 inode->i_op = &unionfs_dir_iops;
60 /* Use different set of file ops for directories */
61 if (S_ISDIR(lower_inode->i_mode))
62 inode->i_fop = &unionfs_dir_fops;
64 /* properly initialize special inodes */
65 if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
66 S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
67 init_special_inode(inode, lower_inode->i_mode,
68 lower_inode->i_rdev);
70 /* all well, copy inode attributes */
71 unionfs_copy_attr_all(inode, lower_inode);
72 fsstack_copy_inode_size(inode, lower_inode);
76 * Connect a unionfs inode dentry/inode with several lower ones. This is
77 * the classic stackable file system "vnode interposition" action.
79 * @sb: unionfs's super_block
81 struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb,
82 int flag)
84 int err = 0;
85 struct inode *inode;
86 int is_negative_dentry = 1;
87 int bindex, bstart, bend;
88 int need_fill_inode = 1;
89 struct dentry *spliced = NULL;
91 verify_locked(dentry);
93 bstart = dbstart(dentry);
94 bend = dbend(dentry);
96 /* Make sure that we didn't get a negative dentry. */
97 for (bindex = bstart; bindex <= bend; bindex++) {
98 if (unionfs_lower_dentry_idx(dentry, bindex) &&
99 unionfs_lower_dentry_idx(dentry, bindex)->d_inode) {
100 is_negative_dentry = 0;
101 break;
104 BUG_ON(is_negative_dentry);
107 * We allocate our new inode below, by calling iget.
108 * iget will call our read_inode which will initialize some
109 * of the new inode's fields
113 * On revalidate we've already got our own inode and just need
114 * to fix it up.
116 if (flag == INTERPOSE_REVAL) {
117 inode = dentry->d_inode;
118 UNIONFS_I(inode)->bstart = -1;
119 UNIONFS_I(inode)->bend = -1;
120 atomic_set(&UNIONFS_I(inode)->generation,
121 atomic_read(&UNIONFS_SB(sb)->generation));
123 UNIONFS_I(inode)->lower_inodes =
124 kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL);
125 if (unlikely(!UNIONFS_I(inode)->lower_inodes)) {
126 err = -ENOMEM;
127 goto out;
129 } else {
130 /* get unique inode number for unionfs */
131 inode = iget(sb, iunique(sb, UNIONFS_ROOT_INO));
132 if (!inode) {
133 err = -EACCES;
134 goto out;
136 if (atomic_read(&inode->i_count) > 1)
137 goto skip;
140 need_fill_inode = 0;
141 unionfs_fill_inode(dentry, inode);
143 skip:
144 /* only (our) lookup wants to do a d_add */
145 switch (flag) {
146 case INTERPOSE_DEFAULT:
147 case INTERPOSE_REVAL_NEG:
148 d_instantiate(dentry, inode);
149 break;
150 case INTERPOSE_LOOKUP:
151 spliced = d_splice_alias(inode, dentry);
152 if (IS_ERR(spliced)) {
153 err = PTR_ERR(spliced);
154 } else if (spliced && spliced != dentry) {
156 * d_splice can return a dentry if it was
157 * disconnected and had to be moved. We must ensure
158 * that the private data of the new dentry is
159 * correct and that the inode info was filled
160 * properly. Finally we must return this new
161 * dentry.
163 spliced->d_op = &unionfs_dops;
164 spliced->d_fsdata = dentry->d_fsdata;
165 dentry->d_fsdata = NULL;
166 dentry = spliced;
167 if (need_fill_inode) {
168 need_fill_inode = 0;
169 unionfs_fill_inode(dentry, inode);
171 goto out_spliced;
173 break;
174 case INTERPOSE_REVAL:
175 /* Do nothing. */
176 break;
177 default:
178 printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n");
179 BUG();
181 goto out;
183 out_spliced:
184 if (!err)
185 return spliced;
186 out:
187 return ERR_PTR(err);
190 /* like interpose above, but for an already existing dentry */
191 void unionfs_reinterpose(struct dentry *dentry)
193 struct dentry *lower_dentry;
194 struct inode *inode;
195 int bindex, bstart, bend;
197 verify_locked(dentry);
199 /* This is pre-allocated inode */
200 inode = dentry->d_inode;
202 bstart = dbstart(dentry);
203 bend = dbend(dentry);
204 for (bindex = bstart; bindex <= bend; bindex++) {
205 lower_dentry = unionfs_lower_dentry_idx(dentry, bindex);
206 if (!lower_dentry)
207 continue;
209 if (!lower_dentry->d_inode)
210 continue;
211 if (unionfs_lower_inode_idx(inode, bindex))
212 continue;
213 unionfs_set_lower_inode_idx(inode, bindex,
214 igrab(lower_dentry->d_inode));
216 ibstart(inode) = dbstart(dentry);
217 ibend(inode) = dbend(dentry);
221 * make sure the branch we just looked up (nd) makes sense:
223 * 1) we're not trying to stack unionfs on top of unionfs
224 * 2) it exists
225 * 3) is a directory
227 int check_branch(struct nameidata *nd)
229 /* XXX: remove in ODF code -- stacking unions allowed there */
230 if (!strcmp(nd->dentry->d_sb->s_type->name, UNIONFS_NAME))
231 return -EINVAL;
232 if (!nd->dentry->d_inode)
233 return -ENOENT;
234 if (!S_ISDIR(nd->dentry->d_inode->i_mode))
235 return -ENOTDIR;
236 return 0;
239 /* checks if two lower_dentries have overlapping branches */
240 static int is_branch_overlap(struct dentry *dent1, struct dentry *dent2)
242 struct dentry *dent = NULL;
244 dent = dent1;
245 while ((dent != dent2) && (dent->d_parent != dent))
246 dent = dent->d_parent;
248 if (dent == dent2)
249 return 1;
251 dent = dent2;
252 while ((dent != dent1) && (dent->d_parent != dent))
253 dent = dent->d_parent;
255 return (dent == dent1);
259 * Parse branch mode helper function
261 int __parse_branch_mode(const char *name)
263 if (!name)
264 return 0;
265 if (!strcmp(name, "ro"))
266 return MAY_READ;
267 if (!strcmp(name, "rw"))
268 return (MAY_READ | MAY_WRITE);
269 return 0;
273 * Parse "ro" or "rw" options, but default to "rw" of no mode options
274 * was specified.
276 int parse_branch_mode(const char *name)
278 int perms = __parse_branch_mode(name);
280 if (perms == 0)
281 perms = MAY_READ | MAY_WRITE;
282 return perms;
286 * parse the dirs= mount argument
288 * We don't need to lock the superblock private data's rwsem, as we get
289 * called only by unionfs_read_super - it is still a long time before anyone
290 * can even get a reference to us.
292 static int parse_dirs_option(struct super_block *sb, struct unionfs_dentry_info
293 *lower_root_info, char *options)
295 struct nameidata nd;
296 char *name;
297 int err = 0;
298 int branches = 1;
299 int bindex = 0;
300 int i = 0;
301 int j = 0;
302 struct dentry *dent1;
303 struct dentry *dent2;
305 if (options[0] == '\0') {
306 printk(KERN_ERR "unionfs: no branches specified\n");
307 err = -EINVAL;
308 goto out;
312 * Each colon means we have a separator, this is really just a rough
313 * guess, since strsep will handle empty fields for us.
315 for (i = 0; options[i]; i++)
316 if (options[i] == ':')
317 branches++;
319 /* allocate space for underlying pointers to lower dentry */
320 UNIONFS_SB(sb)->data =
321 kcalloc(branches, sizeof(struct unionfs_data), GFP_KERNEL);
322 if (unlikely(!UNIONFS_SB(sb)->data)) {
323 err = -ENOMEM;
324 goto out;
327 lower_root_info->lower_paths =
328 kcalloc(branches, sizeof(struct path), GFP_KERNEL);
329 if (unlikely(!lower_root_info->lower_paths)) {
330 err = -ENOMEM;
331 goto out;
334 /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
335 branches = 0;
336 while ((name = strsep(&options, ":")) != NULL) {
337 int perms;
338 char *mode = strchr(name, '=');
340 if (!name)
341 continue;
342 if (!*name) { /* bad use of ':' (extra colons) */
343 err = -EINVAL;
344 goto out;
347 branches++;
349 /* strip off '=' if any */
350 if (mode)
351 *mode++ = '\0';
353 perms = parse_branch_mode(mode);
354 if (!bindex && !(perms & MAY_WRITE)) {
355 err = -EINVAL;
356 goto out;
359 err = path_lookup(name, LOOKUP_FOLLOW, &nd);
360 if (err) {
361 printk(KERN_ERR "unionfs: error accessing "
362 "lower directory '%s' (error %d)\n",
363 name, err);
364 goto out;
367 err = check_branch(&nd);
368 if (err) {
369 printk(KERN_ERR "unionfs: lower directory "
370 "'%s' is not a valid branch\n", name);
371 path_release(&nd);
372 goto out;
375 lower_root_info->lower_paths[bindex].dentry = nd.dentry;
376 lower_root_info->lower_paths[bindex].mnt = nd.mnt;
378 set_branchperms(sb, bindex, perms);
379 set_branch_count(sb, bindex, 0);
380 new_branch_id(sb, bindex);
382 if (lower_root_info->bstart < 0)
383 lower_root_info->bstart = bindex;
384 lower_root_info->bend = bindex;
385 bindex++;
388 if (branches == 0) {
389 printk(KERN_ERR "unionfs: no branches specified\n");
390 err = -EINVAL;
391 goto out;
394 BUG_ON(branches != (lower_root_info->bend + 1));
397 * Ensure that no overlaps exist in the branches.
399 * This test is required because the Linux kernel has no support
400 * currently for ensuring coherency between stackable layers and
401 * branches. If we were to allow overlapping branches, it would be
402 * possible, for example, to delete a file via one branch, which
403 * would not be reflected in another branch. Such incoherency could
404 * lead to inconsistencies and even kernel oopses. Rather than
405 * implement hacks to work around some of these cache-coherency
406 * problems, we prevent branch overlapping, for now. A complete
407 * solution will involve proper kernel/VFS support for cache
408 * coherency, at which time we could safely remove this
409 * branch-overlapping test.
411 for (i = 0; i < branches; i++) {
412 dent1 = lower_root_info->lower_paths[i].dentry;
413 for (j = i + 1; j < branches; j++) {
414 dent2 = lower_root_info->lower_paths[j].dentry;
415 if (is_branch_overlap(dent1, dent2)) {
416 printk(KERN_ERR "unionfs: branches %d and "
417 "%d overlap\n", i, j);
418 err = -EINVAL;
419 goto out;
424 out:
425 if (err) {
426 for (i = 0; i < branches; i++)
427 if (lower_root_info->lower_paths[i].dentry) {
428 dput(lower_root_info->lower_paths[i].dentry);
429 /* initialize: can't use unionfs_mntput here */
430 mntput(lower_root_info->lower_paths[i].mnt);
433 kfree(lower_root_info->lower_paths);
434 kfree(UNIONFS_SB(sb)->data);
437 * MUST clear the pointers to prevent potential double free if
438 * the caller dies later on
440 lower_root_info->lower_paths = NULL;
441 UNIONFS_SB(sb)->data = NULL;
443 return err;
447 * Parse mount options. See the manual page for usage instructions.
449 * Returns the dentry object of the lower-level (lower) directory;
450 * We want to mount our stackable file system on top of that lower directory.
452 static struct unionfs_dentry_info *unionfs_parse_options(
453 struct super_block *sb,
454 char *options)
456 struct unionfs_dentry_info *lower_root_info;
457 char *optname;
458 int err = 0;
459 int bindex;
460 int dirsfound = 0;
462 /* allocate private data area */
463 err = -ENOMEM;
464 lower_root_info =
465 kzalloc(sizeof(struct unionfs_dentry_info), GFP_KERNEL);
466 if (unlikely(!lower_root_info))
467 goto out_error;
468 lower_root_info->bstart = -1;
469 lower_root_info->bend = -1;
470 lower_root_info->bopaque = -1;
472 while ((optname = strsep(&options, ",")) != NULL) {
473 char *optarg;
474 char *endptr;
475 int intval;
477 if (!optname || !*optname)
478 continue;
480 optarg = strchr(optname, '=');
481 if (optarg)
482 *optarg++ = '\0';
485 * All of our options take an argument now. Insert ones that
486 * don't, above this check.
488 if (!optarg) {
489 printk(KERN_ERR "unionfs: %s requires an argument\n",
490 optname);
491 err = -EINVAL;
492 goto out_error;
495 if (!strcmp("dirs", optname)) {
496 if (++dirsfound > 1) {
497 printk(KERN_ERR
498 "unionfs: multiple dirs specified\n");
499 err = -EINVAL;
500 goto out_error;
502 err = parse_dirs_option(sb, lower_root_info, optarg);
503 if (err)
504 goto out_error;
505 continue;
508 /* All of these options require an integer argument. */
509 intval = simple_strtoul(optarg, &endptr, 0);
510 if (*endptr) {
511 printk(KERN_ERR
512 "unionfs: invalid %s option '%s'\n",
513 optname, optarg);
514 err = -EINVAL;
515 goto out_error;
518 err = -EINVAL;
519 printk(KERN_ERR
520 "unionfs: unrecognized option '%s'\n", optname);
521 goto out_error;
523 if (dirsfound != 1) {
524 printk(KERN_ERR "unionfs: dirs option required\n");
525 err = -EINVAL;
526 goto out_error;
528 goto out;
530 out_error:
531 if (lower_root_info && lower_root_info->lower_paths) {
532 for (bindex = lower_root_info->bstart;
533 bindex >= 0 && bindex <= lower_root_info->bend;
534 bindex++) {
535 struct dentry *d;
536 struct vfsmount *m;
538 d = lower_root_info->lower_paths[bindex].dentry;
539 m = lower_root_info->lower_paths[bindex].mnt;
541 dput(d);
542 /* initializing: can't use unionfs_mntput here */
543 mntput(m);
547 kfree(lower_root_info->lower_paths);
548 kfree(lower_root_info);
550 kfree(UNIONFS_SB(sb)->data);
551 UNIONFS_SB(sb)->data = NULL;
553 lower_root_info = ERR_PTR(err);
554 out:
555 return lower_root_info;
559 * our custom d_alloc_root work-alike
561 * we can't use d_alloc_root if we want to use our own interpose function
562 * unchanged, so we simply call our own "fake" d_alloc_root
564 static struct dentry *unionfs_d_alloc_root(struct super_block *sb)
566 struct dentry *ret = NULL;
568 if (sb) {
569 static const struct qstr name = {
570 .name = "/",
571 .len = 1
574 ret = d_alloc(NULL, &name);
575 if (likely(ret)) {
576 ret->d_op = &unionfs_dops;
577 ret->d_sb = sb;
578 ret->d_parent = ret;
581 return ret;
585 * There is no need to lock the unionfs_super_info's rwsem as there is no
586 * way anyone can have a reference to the superblock at this point in time.
588 static int unionfs_read_super(struct super_block *sb, void *raw_data,
589 int silent)
591 int err = 0;
592 struct unionfs_dentry_info *lower_root_info = NULL;
593 int bindex, bstart, bend;
595 if (!raw_data) {
596 printk(KERN_ERR
597 "unionfs: read_super: missing data argument\n");
598 err = -EINVAL;
599 goto out;
602 /* Allocate superblock private data */
603 sb->s_fs_info = kzalloc(sizeof(struct unionfs_sb_info), GFP_KERNEL);
604 if (unlikely(!UNIONFS_SB(sb))) {
605 printk(KERN_CRIT "unionfs: read_super: out of memory\n");
606 err = -ENOMEM;
607 goto out;
610 UNIONFS_SB(sb)->bend = -1;
611 atomic_set(&UNIONFS_SB(sb)->generation, 1);
612 init_rwsem(&UNIONFS_SB(sb)->rwsem);
613 UNIONFS_SB(sb)->high_branch_id = -1; /* -1 == invalid branch ID */
615 lower_root_info = unionfs_parse_options(sb, raw_data);
616 if (IS_ERR(lower_root_info)) {
617 printk(KERN_ERR
618 "unionfs: read_super: error while parsing options "
619 "(err = %ld)\n", PTR_ERR(lower_root_info));
620 err = PTR_ERR(lower_root_info);
621 lower_root_info = NULL;
622 goto out_free;
624 if (lower_root_info->bstart == -1) {
625 err = -ENOENT;
626 goto out_free;
629 /* set the lower superblock field of upper superblock */
630 bstart = lower_root_info->bstart;
631 BUG_ON(bstart != 0);
632 sbend(sb) = bend = lower_root_info->bend;
633 for (bindex = bstart; bindex <= bend; bindex++) {
634 struct dentry *d = lower_root_info->lower_paths[bindex].dentry;
635 unionfs_set_lower_super_idx(sb, bindex, d->d_sb);
638 /* max Bytes is the maximum bytes from highest priority branch */
639 sb->s_maxbytes = unionfs_lower_super_idx(sb, 0)->s_maxbytes;
641 sb->s_op = &unionfs_sops;
643 /* See comment next to the definition of unionfs_d_alloc_root */
644 sb->s_root = unionfs_d_alloc_root(sb);
645 if (unlikely(!sb->s_root)) {
646 err = -ENOMEM;
647 goto out_dput;
650 /* link the upper and lower dentries */
651 sb->s_root->d_fsdata = NULL;
652 err = new_dentry_private_data(sb->s_root);
653 if (unlikely(err))
654 goto out_freedpd;
656 /* Set the lower dentries for s_root */
657 for (bindex = bstart; bindex <= bend; bindex++) {
658 struct dentry *d;
659 struct vfsmount *m;
661 d = lower_root_info->lower_paths[bindex].dentry;
662 m = lower_root_info->lower_paths[bindex].mnt;
664 unionfs_set_lower_dentry_idx(sb->s_root, bindex, d);
665 unionfs_set_lower_mnt_idx(sb->s_root, bindex, m);
667 set_dbstart(sb->s_root, bstart);
668 set_dbend(sb->s_root, bend);
670 /* Set the generation number to one, since this is for the mount. */
671 atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
674 * Call interpose to create the upper level inode. Only
675 * INTERPOSE_LOOKUP can return a value other than 0 on err.
677 err = PTR_ERR(unionfs_interpose(sb->s_root, sb, 0));
678 unionfs_unlock_dentry(sb->s_root);
679 if (!err)
680 goto out;
681 /* else fall through */
683 out_freedpd:
684 if (UNIONFS_D(sb->s_root)) {
685 kfree(UNIONFS_D(sb->s_root)->lower_paths);
686 free_dentry_private_data(sb->s_root);
688 dput(sb->s_root);
690 out_dput:
691 if (lower_root_info && !IS_ERR(lower_root_info)) {
692 for (bindex = lower_root_info->bstart;
693 bindex <= lower_root_info->bend; bindex++) {
694 struct dentry *d;
695 struct vfsmount *m;
697 d = lower_root_info->lower_paths[bindex].dentry;
698 m = lower_root_info->lower_paths[bindex].mnt;
700 dput(d);
701 /* initializing: can't use unionfs_mntput here */
702 mntput(m);
704 kfree(lower_root_info->lower_paths);
705 kfree(lower_root_info);
706 lower_root_info = NULL;
709 out_free:
710 kfree(UNIONFS_SB(sb)->data);
711 kfree(UNIONFS_SB(sb));
712 sb->s_fs_info = NULL;
714 out:
715 if (lower_root_info && !IS_ERR(lower_root_info)) {
716 kfree(lower_root_info->lower_paths);
717 kfree(lower_root_info);
719 return err;
722 static int unionfs_get_sb(struct file_system_type *fs_type,
723 int flags, const char *dev_name,
724 void *raw_data, struct vfsmount *mnt)
726 return get_sb_nodev(fs_type, flags, raw_data, unionfs_read_super, mnt);
729 static struct file_system_type unionfs_fs_type = {
730 .owner = THIS_MODULE,
731 .name = UNIONFS_NAME,
732 .get_sb = unionfs_get_sb,
733 .kill_sb = generic_shutdown_super,
734 .fs_flags = FS_REVAL_DOT,
737 static int __init init_unionfs_fs(void)
739 int err;
741 pr_info("Registering unionfs " UNIONFS_VERSION "\n");
743 err = unionfs_init_filldir_cache();
744 if (unlikely(err))
745 goto out;
746 err = unionfs_init_inode_cache();
747 if (unlikely(err))
748 goto out;
749 err = unionfs_init_dentry_cache();
750 if (unlikely(err))
751 goto out;
752 err = init_sioq();
753 if (unlikely(err))
754 goto out;
755 err = register_filesystem(&unionfs_fs_type);
756 out:
757 if (unlikely(err)) {
758 stop_sioq();
759 unionfs_destroy_filldir_cache();
760 unionfs_destroy_inode_cache();
761 unionfs_destroy_dentry_cache();
763 return err;
766 static void __exit exit_unionfs_fs(void)
768 stop_sioq();
769 unionfs_destroy_filldir_cache();
770 unionfs_destroy_inode_cache();
771 unionfs_destroy_dentry_cache();
772 unregister_filesystem(&unionfs_fs_type);
773 pr_info("Completed unionfs module unload\n");
776 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
777 " (http://www.fsl.cs.sunysb.edu)");
778 MODULE_DESCRIPTION("Unionfs " UNIONFS_VERSION
779 " (http://unionfs.filesystems.org)");
780 MODULE_LICENSE("GPL");
782 module_init(init_unionfs_fs);
783 module_exit(exit_unionfs_fs);