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.
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
23 static void unionfs_fill_inode(struct dentry
*dentry
,
26 struct inode
*lower_inode
;
27 struct dentry
*lower_dentry
;
28 int bindex
, bstart
, bend
;
30 bstart
= dbstart(dentry
);
33 for (bindex
= bstart
; bindex
<= bend
; bindex
++) {
34 lower_dentry
= unionfs_lower_dentry_idx(dentry
, bindex
);
36 unionfs_set_lower_inode_idx(inode
, bindex
, NULL
);
40 /* Initialize the lower inode to the new lower inode. */
41 if (!lower_dentry
->d_inode
)
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
,
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
,
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
);
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;
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
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
)) {
130 /* get unique inode number for unionfs */
131 inode
= iget(sb
, iunique(sb
, UNIONFS_ROOT_INO
));
136 if (atomic_read(&inode
->i_count
) > 1)
141 unionfs_fill_inode(dentry
, inode
);
144 /* only (our) lookup wants to do a d_add */
146 case INTERPOSE_DEFAULT
:
147 case INTERPOSE_REVAL_NEG
:
148 d_instantiate(dentry
, inode
);
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
163 spliced
->d_op
= &unionfs_dops
;
164 spliced
->d_fsdata
= dentry
->d_fsdata
;
165 dentry
->d_fsdata
= NULL
;
167 if (need_fill_inode
) {
169 unionfs_fill_inode(dentry
, inode
);
174 case INTERPOSE_REVAL
:
178 printk(KERN_CRIT
"unionfs: invalid interpose flag passed!\n");
190 /* like interpose above, but for an already existing dentry */
191 void unionfs_reinterpose(struct dentry
*dentry
)
193 struct dentry
*lower_dentry
;
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
);
209 if (!lower_dentry
->d_inode
)
211 if (unionfs_lower_inode_idx(inode
, bindex
))
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
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
))
232 if (!nd
->dentry
->d_inode
)
234 if (!S_ISDIR(nd
->dentry
->d_inode
->i_mode
))
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
;
245 while ((dent
!= dent2
) && (dent
->d_parent
!= dent
))
246 dent
= dent
->d_parent
;
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
)
265 if (!strcmp(name
, "ro"))
267 if (!strcmp(name
, "rw"))
268 return (MAY_READ
| MAY_WRITE
);
273 * Parse "ro" or "rw" options, but default to "rw" of no mode options
276 int parse_branch_mode(const char *name
)
278 int perms
= __parse_branch_mode(name
);
281 perms
= MAY_READ
| MAY_WRITE
;
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
)
302 struct dentry
*dent1
;
303 struct dentry
*dent2
;
305 if (options
[0] == '\0') {
306 printk(KERN_ERR
"unionfs: no branches specified\n");
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
] == ':')
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
)) {
327 lower_root_info
->lower_paths
=
328 kcalloc(branches
, sizeof(struct path
), GFP_KERNEL
);
329 if (unlikely(!lower_root_info
->lower_paths
)) {
334 /* now parsing a string such as "b1:b2=rw:b3=ro:b4" */
336 while ((name
= strsep(&options
, ":")) != NULL
) {
338 char *mode
= strchr(name
, '=');
342 if (!*name
) { /* bad use of ':' (extra colons) */
349 /* strip off '=' if any */
353 perms
= parse_branch_mode(mode
);
354 if (!bindex
&& !(perms
& MAY_WRITE
)) {
359 err
= path_lookup(name
, LOOKUP_FOLLOW
, &nd
);
361 printk(KERN_ERR
"unionfs: error accessing "
362 "lower directory '%s' (error %d)\n",
367 err
= check_branch(&nd
);
369 printk(KERN_ERR
"unionfs: lower directory "
370 "'%s' is not a valid branch\n", name
);
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
;
389 printk(KERN_ERR
"unionfs: no branches specified\n");
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
);
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
;
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
,
456 struct unionfs_dentry_info
*lower_root_info
;
462 /* allocate private data area */
465 kzalloc(sizeof(struct unionfs_dentry_info
), GFP_KERNEL
);
466 if (unlikely(!lower_root_info
))
468 lower_root_info
->bstart
= -1;
469 lower_root_info
->bend
= -1;
470 lower_root_info
->bopaque
= -1;
472 while ((optname
= strsep(&options
, ",")) != NULL
) {
477 if (!optname
|| !*optname
)
480 optarg
= strchr(optname
, '=');
485 * All of our options take an argument now. Insert ones that
486 * don't, above this check.
489 printk(KERN_ERR
"unionfs: %s requires an argument\n",
495 if (!strcmp("dirs", optname
)) {
496 if (++dirsfound
> 1) {
498 "unionfs: multiple dirs specified\n");
502 err
= parse_dirs_option(sb
, lower_root_info
, optarg
);
508 /* All of these options require an integer argument. */
509 intval
= simple_strtoul(optarg
, &endptr
, 0);
512 "unionfs: invalid %s option '%s'\n",
520 "unionfs: unrecognized option '%s'\n", optname
);
523 if (dirsfound
!= 1) {
524 printk(KERN_ERR
"unionfs: dirs option required\n");
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
;
538 d
= lower_root_info
->lower_paths
[bindex
].dentry
;
539 m
= lower_root_info
->lower_paths
[bindex
].mnt
;
542 /* initializing: can't use unionfs_mntput here */
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
);
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
;
569 static const struct qstr name
= {
574 ret
= d_alloc(NULL
, &name
);
576 ret
->d_op
= &unionfs_dops
;
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
,
592 struct unionfs_dentry_info
*lower_root_info
= NULL
;
593 int bindex
, bstart
, bend
;
597 "unionfs: read_super: missing data argument\n");
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");
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
)) {
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
;
624 if (lower_root_info
->bstart
== -1) {
629 /* set the lower superblock field of upper superblock */
630 bstart
= lower_root_info
->bstart
;
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
)) {
650 /* link the upper and lower dentries */
651 sb
->s_root
->d_fsdata
= NULL
;
652 err
= new_dentry_private_data(sb
->s_root
);
656 /* Set the lower dentries for s_root */
657 for (bindex
= bstart
; bindex
<= bend
; bindex
++) {
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
);
681 /* else fall through */
684 if (UNIONFS_D(sb
->s_root
)) {
685 kfree(UNIONFS_D(sb
->s_root
)->lower_paths
);
686 free_dentry_private_data(sb
->s_root
);
691 if (lower_root_info
&& !IS_ERR(lower_root_info
)) {
692 for (bindex
= lower_root_info
->bstart
;
693 bindex
<= lower_root_info
->bend
; bindex
++) {
697 d
= lower_root_info
->lower_paths
[bindex
].dentry
;
698 m
= lower_root_info
->lower_paths
[bindex
].mnt
;
701 /* initializing: can't use unionfs_mntput here */
704 kfree(lower_root_info
->lower_paths
);
705 kfree(lower_root_info
);
706 lower_root_info
= NULL
;
710 kfree(UNIONFS_SB(sb
)->data
);
711 kfree(UNIONFS_SB(sb
));
712 sb
->s_fs_info
= NULL
;
715 if (lower_root_info
&& !IS_ERR(lower_root_info
)) {
716 kfree(lower_root_info
->lower_paths
);
717 kfree(lower_root_info
);
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)
741 pr_info("Registering unionfs " UNIONFS_VERSION
"\n");
743 err
= unionfs_init_filldir_cache();
746 err
= unionfs_init_inode_cache();
749 err
= unionfs_init_dentry_cache();
755 err
= register_filesystem(&unionfs_fs_type
);
759 unionfs_destroy_filldir_cache();
760 unionfs_destroy_inode_cache();
761 unionfs_destroy_dentry_cache();
766 static void __exit
exit_unionfs_fs(void)
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
);