3 * Copyright (C) 2011 Novell Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
11 #include <linux/namei.h>
12 #include <linux/pagemap.h>
13 #include <linux/xattr.h>
14 #include <linux/security.h>
15 #include <linux/mount.h>
16 #include <linux/slab.h>
17 #include <linux/parser.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/statfs.h>
22 #include <linux/seq_file.h>
23 #include "overlayfs.h"
25 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
26 MODULE_DESCRIPTION("Overlay filesystem");
27 MODULE_LICENSE("GPL");
33 bool default_permissions
;
36 /* private information held for overlayfs's superblock */
38 struct vfsmount
*upper_mnt
;
40 struct vfsmount
**lower_mnt
;
41 struct dentry
*workdir
;
43 /* pathnames of lower and upper dirs, for show_options */
44 struct ovl_config config
;
45 /* creds of process who forced instantiation of super block */
46 const struct cred
*creator_cred
;
51 /* private information held for every overlayfs dentry */
53 struct dentry
*__upperdentry
;
54 struct ovl_dir_cache
*cache
;
63 struct path lowerstack
[];
66 #define OVL_MAX_STACK 500
68 static struct dentry
*__ovl_dentry_lower(struct ovl_entry
*oe
)
70 return oe
->numlower
? oe
->lowerstack
[0].dentry
: NULL
;
73 enum ovl_path_type
ovl_path_type(struct dentry
*dentry
)
75 struct ovl_entry
*oe
= dentry
->d_fsdata
;
76 enum ovl_path_type type
= 0;
78 if (oe
->__upperdentry
) {
79 type
= __OVL_PATH_UPPER
;
82 * Non-dir dentry can hold lower dentry from previous
83 * location. Its purity depends only on opaque flag.
85 if (oe
->numlower
&& S_ISDIR(dentry
->d_inode
->i_mode
))
86 type
|= __OVL_PATH_MERGE
;
88 type
|= __OVL_PATH_PURE
;
91 type
|= __OVL_PATH_MERGE
;
96 static struct dentry
*ovl_upperdentry_dereference(struct ovl_entry
*oe
)
98 return lockless_dereference(oe
->__upperdentry
);
101 void ovl_path_upper(struct dentry
*dentry
, struct path
*path
)
103 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
104 struct ovl_entry
*oe
= dentry
->d_fsdata
;
106 path
->mnt
= ofs
->upper_mnt
;
107 path
->dentry
= ovl_upperdentry_dereference(oe
);
110 enum ovl_path_type
ovl_path_real(struct dentry
*dentry
, struct path
*path
)
112 enum ovl_path_type type
= ovl_path_type(dentry
);
114 if (!OVL_TYPE_UPPER(type
))
115 ovl_path_lower(dentry
, path
);
117 ovl_path_upper(dentry
, path
);
122 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
124 struct ovl_entry
*oe
= dentry
->d_fsdata
;
126 return ovl_upperdentry_dereference(oe
);
129 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
131 struct ovl_entry
*oe
= dentry
->d_fsdata
;
133 return __ovl_dentry_lower(oe
);
136 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
138 struct ovl_entry
*oe
= dentry
->d_fsdata
;
139 struct dentry
*realdentry
;
141 realdentry
= ovl_upperdentry_dereference(oe
);
143 realdentry
= __ovl_dentry_lower(oe
);
148 struct dentry
*ovl_entry_real(struct ovl_entry
*oe
, bool *is_upper
)
150 struct dentry
*realdentry
;
152 realdentry
= ovl_upperdentry_dereference(oe
);
156 realdentry
= __ovl_dentry_lower(oe
);
162 struct vfsmount
*ovl_entry_mnt_real(struct ovl_entry
*oe
, struct inode
*inode
,
166 struct ovl_fs
*ofs
= inode
->i_sb
->s_fs_info
;
168 return ofs
->upper_mnt
;
170 return oe
->numlower
? oe
->lowerstack
[0].mnt
: NULL
;
174 struct ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
176 struct ovl_entry
*oe
= dentry
->d_fsdata
;
181 bool ovl_is_default_permissions(struct inode
*inode
)
183 struct ovl_fs
*ofs
= inode
->i_sb
->s_fs_info
;
185 return ofs
->config
.default_permissions
;
188 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
190 struct ovl_entry
*oe
= dentry
->d_fsdata
;
195 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
197 struct ovl_entry
*oe
= dentry
->d_fsdata
;
199 *path
= oe
->numlower
? oe
->lowerstack
[0] : (struct path
) { NULL
, NULL
};
202 int ovl_want_write(struct dentry
*dentry
)
204 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
205 return mnt_want_write(ofs
->upper_mnt
);
208 void ovl_drop_write(struct dentry
*dentry
)
210 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
211 mnt_drop_write(ofs
->upper_mnt
);
214 struct dentry
*ovl_workdir(struct dentry
*dentry
)
216 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
220 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
222 struct ovl_entry
*oe
= dentry
->d_fsdata
;
226 void ovl_dentry_set_opaque(struct dentry
*dentry
, bool opaque
)
228 struct ovl_entry
*oe
= dentry
->d_fsdata
;
232 void ovl_dentry_update(struct dentry
*dentry
, struct dentry
*upperdentry
)
234 struct ovl_entry
*oe
= dentry
->d_fsdata
;
236 WARN_ON(!inode_is_locked(upperdentry
->d_parent
->d_inode
));
237 WARN_ON(oe
->__upperdentry
);
238 BUG_ON(!upperdentry
->d_inode
);
240 * Make sure upperdentry is consistent before making it visible to
241 * ovl_upperdentry_dereference().
244 oe
->__upperdentry
= upperdentry
;
247 void ovl_dentry_version_inc(struct dentry
*dentry
)
249 struct ovl_entry
*oe
= dentry
->d_fsdata
;
251 WARN_ON(!inode_is_locked(dentry
->d_inode
));
255 u64
ovl_dentry_version_get(struct dentry
*dentry
)
257 struct ovl_entry
*oe
= dentry
->d_fsdata
;
259 WARN_ON(!inode_is_locked(dentry
->d_inode
));
263 bool ovl_is_whiteout(struct dentry
*dentry
)
265 struct inode
*inode
= dentry
->d_inode
;
267 return inode
&& IS_WHITEOUT(inode
);
270 const struct cred
*ovl_override_creds(struct super_block
*sb
)
272 struct ovl_fs
*ofs
= sb
->s_fs_info
;
274 return override_creds(ofs
->creator_cred
);
277 static bool ovl_is_opaquedir(struct dentry
*dentry
)
281 struct inode
*inode
= dentry
->d_inode
;
283 if (!S_ISDIR(inode
->i_mode
) || !inode
->i_op
->getxattr
)
286 res
= inode
->i_op
->getxattr(dentry
, inode
, OVL_XATTR_OPAQUE
, &val
, 1);
287 if (res
== 1 && val
== 'y')
293 static void ovl_dentry_release(struct dentry
*dentry
)
295 struct ovl_entry
*oe
= dentry
->d_fsdata
;
300 dput(oe
->__upperdentry
);
301 for (i
= 0; i
< oe
->numlower
; i
++)
302 dput(oe
->lowerstack
[i
].dentry
);
307 static struct dentry
*ovl_d_real(struct dentry
*dentry
, struct inode
*inode
)
311 if (d_is_dir(dentry
)) {
312 if (!inode
|| inode
== d_inode(dentry
))
317 real
= ovl_dentry_upper(dentry
);
318 if (real
&& (!inode
|| inode
== d_inode(real
)))
321 real
= ovl_dentry_lower(dentry
);
325 if (!inode
|| inode
== d_inode(real
))
328 /* Handle recursion */
329 if (real
->d_flags
& DCACHE_OP_REAL
)
330 return real
->d_op
->d_real(real
, inode
);
333 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry
,
334 inode
? inode
->i_sb
->s_id
: "NULL", inode
? inode
->i_ino
: 0);
338 static int ovl_dentry_revalidate(struct dentry
*dentry
, unsigned int flags
)
340 struct ovl_entry
*oe
= dentry
->d_fsdata
;
344 for (i
= 0; i
< oe
->numlower
; i
++) {
345 struct dentry
*d
= oe
->lowerstack
[i
].dentry
;
347 if (d
->d_flags
& DCACHE_OP_REVALIDATE
) {
348 ret
= d
->d_op
->d_revalidate(d
, flags
);
352 if (!(flags
& LOOKUP_RCU
))
361 static int ovl_dentry_weak_revalidate(struct dentry
*dentry
, unsigned int flags
)
363 struct ovl_entry
*oe
= dentry
->d_fsdata
;
367 for (i
= 0; i
< oe
->numlower
; i
++) {
368 struct dentry
*d
= oe
->lowerstack
[i
].dentry
;
370 if (d
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
) {
371 ret
= d
->d_op
->d_weak_revalidate(d
, flags
);
379 static const struct dentry_operations ovl_dentry_operations
= {
380 .d_release
= ovl_dentry_release
,
381 .d_select_inode
= ovl_d_select_inode
,
382 .d_real
= ovl_d_real
,
385 static const struct dentry_operations ovl_reval_dentry_operations
= {
386 .d_release
= ovl_dentry_release
,
387 .d_select_inode
= ovl_d_select_inode
,
388 .d_real
= ovl_d_real
,
389 .d_revalidate
= ovl_dentry_revalidate
,
390 .d_weak_revalidate
= ovl_dentry_weak_revalidate
,
393 static struct ovl_entry
*ovl_alloc_entry(unsigned int numlower
)
395 size_t size
= offsetof(struct ovl_entry
, lowerstack
[numlower
]);
396 struct ovl_entry
*oe
= kzalloc(size
, GFP_KERNEL
);
399 oe
->numlower
= numlower
;
404 static bool ovl_dentry_remote(struct dentry
*dentry
)
406 return dentry
->d_flags
&
407 (DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
);
410 static bool ovl_dentry_weird(struct dentry
*dentry
)
412 return dentry
->d_flags
& (DCACHE_NEED_AUTOMOUNT
|
413 DCACHE_MANAGE_TRANSIT
|
418 static inline struct dentry
*ovl_lookup_real(struct dentry
*dir
,
421 struct dentry
*dentry
;
423 dentry
= lookup_hash(name
, dir
);
425 if (IS_ERR(dentry
)) {
426 if (PTR_ERR(dentry
) == -ENOENT
)
428 } else if (!dentry
->d_inode
) {
431 } else if (ovl_dentry_weird(dentry
)) {
433 /* Don't support traversing automounts and other weirdness */
434 dentry
= ERR_PTR(-EREMOTE
);
440 * Returns next layer in stack starting from top.
441 * Returns -1 if this is the last layer.
443 int ovl_path_next(int idx
, struct dentry
*dentry
, struct path
*path
)
445 struct ovl_entry
*oe
= dentry
->d_fsdata
;
449 ovl_path_upper(dentry
, path
);
451 return oe
->numlower
? 1 : -1;
454 BUG_ON(idx
> oe
->numlower
);
455 *path
= oe
->lowerstack
[idx
- 1];
457 return (idx
< oe
->numlower
) ? idx
+ 1 : -1;
460 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
463 struct ovl_entry
*oe
;
464 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
465 struct path
*stack
= NULL
;
466 struct dentry
*upperdir
, *upperdentry
= NULL
;
467 unsigned int ctr
= 0;
468 struct inode
*inode
= NULL
;
469 bool upperopaque
= false;
470 struct dentry
*this, *prev
= NULL
;
474 upperdir
= ovl_upperdentry_dereference(poe
);
476 this = ovl_lookup_real(upperdir
, &dentry
->d_name
);
482 if (unlikely(ovl_dentry_remote(this))) {
487 if (ovl_is_whiteout(this)) {
491 } else if (poe
->numlower
&& ovl_is_opaquedir(this)) {
495 upperdentry
= prev
= this;
498 if (!upperopaque
&& poe
->numlower
) {
500 stack
= kcalloc(poe
->numlower
, sizeof(struct path
), GFP_KERNEL
);
505 for (i
= 0; !upperopaque
&& i
< poe
->numlower
; i
++) {
507 struct path lowerpath
= poe
->lowerstack
[i
];
509 this = ovl_lookup_real(lowerpath
.dentry
, &dentry
->d_name
);
513 * If it's positive, then treat ENAMETOOLONG as ENOENT.
515 if (err
== -ENAMETOOLONG
&& (upperdentry
|| ctr
))
521 if (ovl_is_whiteout(this)) {
526 * Only makes sense to check opaque dir if this is not the
529 if (i
< poe
->numlower
- 1 && ovl_is_opaquedir(this))
532 if (prev
&& (!S_ISDIR(prev
->d_inode
->i_mode
) ||
533 !S_ISDIR(this->d_inode
->i_mode
))) {
535 * FIXME: check for upper-opaqueness maybe better done
538 if (prev
== upperdentry
)
544 * If this is a non-directory then stop here.
546 if (!S_ISDIR(this->d_inode
->i_mode
))
549 stack
[ctr
].dentry
= this;
550 stack
[ctr
].mnt
= lowerpath
.mnt
;
557 oe
= ovl_alloc_entry(ctr
);
562 if (upperdentry
|| ctr
) {
563 struct dentry
*realdentry
;
565 realdentry
= upperdentry
? upperdentry
: stack
[0].dentry
;
568 inode
= ovl_new_inode(dentry
->d_sb
, realdentry
->d_inode
->i_mode
,
572 ovl_copyattr(realdentry
->d_inode
, inode
);
575 oe
->opaque
= upperopaque
;
576 oe
->__upperdentry
= upperdentry
;
577 memcpy(oe
->lowerstack
, stack
, sizeof(struct path
) * ctr
);
579 dentry
->d_fsdata
= oe
;
580 d_add(dentry
, inode
);
587 for (i
= 0; i
< ctr
; i
++)
588 dput(stack
[i
].dentry
);
596 struct file
*ovl_path_open(struct path
*path
, int flags
)
598 return dentry_open(path
, flags
, current_cred());
601 static void ovl_put_super(struct super_block
*sb
)
603 struct ovl_fs
*ufs
= sb
->s_fs_info
;
607 mntput(ufs
->upper_mnt
);
608 for (i
= 0; i
< ufs
->numlower
; i
++)
609 mntput(ufs
->lower_mnt
[i
]);
610 kfree(ufs
->lower_mnt
);
612 kfree(ufs
->config
.lowerdir
);
613 kfree(ufs
->config
.upperdir
);
614 kfree(ufs
->config
.workdir
);
615 put_cred(ufs
->creator_cred
);
621 * @sb: The overlayfs super block
622 * @buf: The struct kstatfs to fill in with stats
624 * Get the filesystem statistics. As writes always target the upper layer
625 * filesystem pass the statfs to the upper filesystem (if it exists)
627 static int ovl_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
629 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
630 struct dentry
*root_dentry
= dentry
->d_sb
->s_root
;
634 ovl_path_real(root_dentry
, &path
);
636 err
= vfs_statfs(&path
, buf
);
638 buf
->f_namelen
= max(buf
->f_namelen
, ofs
->lower_namelen
);
639 buf
->f_type
= OVERLAYFS_SUPER_MAGIC
;
648 * Prints the mount options for a given superblock.
649 * Returns zero; does not fail.
651 static int ovl_show_options(struct seq_file
*m
, struct dentry
*dentry
)
653 struct super_block
*sb
= dentry
->d_sb
;
654 struct ovl_fs
*ufs
= sb
->s_fs_info
;
656 seq_show_option(m
, "lowerdir", ufs
->config
.lowerdir
);
657 if (ufs
->config
.upperdir
) {
658 seq_show_option(m
, "upperdir", ufs
->config
.upperdir
);
659 seq_show_option(m
, "workdir", ufs
->config
.workdir
);
661 if (ufs
->config
.default_permissions
)
662 seq_puts(m
, ",default_permissions");
666 static int ovl_remount(struct super_block
*sb
, int *flags
, char *data
)
668 struct ovl_fs
*ufs
= sb
->s_fs_info
;
670 if (!(*flags
& MS_RDONLY
) && (!ufs
->upper_mnt
|| !ufs
->workdir
))
676 static const struct super_operations ovl_super_operations
= {
677 .put_super
= ovl_put_super
,
678 .statfs
= ovl_statfs
,
679 .show_options
= ovl_show_options
,
680 .remount_fs
= ovl_remount
,
687 OPT_DEFAULT_PERMISSIONS
,
691 static const match_table_t ovl_tokens
= {
692 {OPT_LOWERDIR
, "lowerdir=%s"},
693 {OPT_UPPERDIR
, "upperdir=%s"},
694 {OPT_WORKDIR
, "workdir=%s"},
695 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
699 static char *ovl_next_opt(char **s
)
707 for (p
= sbegin
; *p
; p
++) {
712 } else if (*p
== ',') {
722 static int ovl_parse_opt(char *opt
, struct ovl_config
*config
)
726 while ((p
= ovl_next_opt(&opt
)) != NULL
) {
728 substring_t args
[MAX_OPT_ARGS
];
733 token
= match_token(p
, ovl_tokens
, args
);
736 kfree(config
->upperdir
);
737 config
->upperdir
= match_strdup(&args
[0]);
738 if (!config
->upperdir
)
743 kfree(config
->lowerdir
);
744 config
->lowerdir
= match_strdup(&args
[0]);
745 if (!config
->lowerdir
)
750 kfree(config
->workdir
);
751 config
->workdir
= match_strdup(&args
[0]);
752 if (!config
->workdir
)
756 case OPT_DEFAULT_PERMISSIONS
:
757 config
->default_permissions
= true;
761 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p
);
766 /* Workdir is useless in non-upper mount */
767 if (!config
->upperdir
&& config
->workdir
) {
768 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
770 kfree(config
->workdir
);
771 config
->workdir
= NULL
;
777 #define OVL_WORKDIR_NAME "work"
779 static struct dentry
*ovl_workdir_create(struct vfsmount
*mnt
,
780 struct dentry
*dentry
)
782 struct inode
*dir
= dentry
->d_inode
;
785 bool retried
= false;
787 err
= mnt_want_write(mnt
);
791 inode_lock_nested(dir
, I_MUTEX_PARENT
);
793 work
= lookup_one_len(OVL_WORKDIR_NAME
, dentry
,
794 strlen(OVL_WORKDIR_NAME
));
797 struct kstat stat
= {
807 ovl_cleanup(dir
, work
);
812 err
= ovl_create_real(dir
, work
, &stat
, NULL
, NULL
, true);
828 static void ovl_unescape(char *s
)
841 static int ovl_mount_dir_noesc(const char *name
, struct path
*path
)
846 pr_err("overlayfs: empty lowerdir\n");
849 err
= kern_path(name
, LOOKUP_FOLLOW
, path
);
851 pr_err("overlayfs: failed to resolve '%s': %i\n", name
, err
);
855 if (ovl_dentry_weird(path
->dentry
)) {
856 pr_err("overlayfs: filesystem on '%s' not supported\n", name
);
859 if (!S_ISDIR(path
->dentry
->d_inode
->i_mode
)) {
860 pr_err("overlayfs: '%s' not a directory\n", name
);
871 static int ovl_mount_dir(const char *name
, struct path
*path
)
874 char *tmp
= kstrdup(name
, GFP_KERNEL
);
878 err
= ovl_mount_dir_noesc(tmp
, path
);
881 if (ovl_dentry_remote(path
->dentry
)) {
882 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
892 static int ovl_lower_dir(const char *name
, struct path
*path
, long *namelen
,
893 int *stack_depth
, bool *remote
)
896 struct kstatfs statfs
;
898 err
= ovl_mount_dir_noesc(name
, path
);
902 err
= vfs_statfs(path
, &statfs
);
904 pr_err("overlayfs: statfs failed on '%s'\n", name
);
907 *namelen
= max(*namelen
, statfs
.f_namelen
);
908 *stack_depth
= max(*stack_depth
, path
->mnt
->mnt_sb
->s_stack_depth
);
910 if (ovl_dentry_remote(path
->dentry
))
921 /* Workdir should not be subdir of upperdir and vice versa */
922 static bool ovl_workdir_ok(struct dentry
*workdir
, struct dentry
*upperdir
)
926 if (workdir
!= upperdir
) {
927 ok
= (lock_rename(workdir
, upperdir
) == NULL
);
928 unlock_rename(workdir
, upperdir
);
933 static unsigned int ovl_split_lowerdirs(char *str
)
935 unsigned int ctr
= 1;
938 for (s
= d
= str
;; s
++, d
++) {
941 } else if (*s
== ':') {
953 static int ovl_fill_super(struct super_block
*sb
, void *data
, int silent
)
955 struct path upperpath
= { NULL
, NULL
};
956 struct path workpath
= { NULL
, NULL
};
957 struct dentry
*root_dentry
;
958 struct ovl_entry
*oe
;
960 struct path
*stack
= NULL
;
963 unsigned int numlower
;
964 unsigned int stacklen
= 0;
970 ufs
= kzalloc(sizeof(struct ovl_fs
), GFP_KERNEL
);
974 err
= ovl_parse_opt((char *) data
, &ufs
->config
);
976 goto out_free_config
;
979 if (!ufs
->config
.lowerdir
) {
981 pr_err("overlayfs: missing 'lowerdir'\n");
982 goto out_free_config
;
985 sb
->s_stack_depth
= 0;
986 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
987 if (ufs
->config
.upperdir
) {
988 if (!ufs
->config
.workdir
) {
989 pr_err("overlayfs: missing 'workdir'\n");
990 goto out_free_config
;
993 err
= ovl_mount_dir(ufs
->config
.upperdir
, &upperpath
);
995 goto out_free_config
;
997 /* Upper fs should not be r/o */
998 if (upperpath
.mnt
->mnt_sb
->s_flags
& MS_RDONLY
) {
999 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1001 goto out_put_upperpath
;
1004 err
= ovl_mount_dir(ufs
->config
.workdir
, &workpath
);
1006 goto out_put_upperpath
;
1009 if (upperpath
.mnt
!= workpath
.mnt
) {
1010 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1011 goto out_put_workpath
;
1013 if (!ovl_workdir_ok(workpath
.dentry
, upperpath
.dentry
)) {
1014 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1015 goto out_put_workpath
;
1017 sb
->s_stack_depth
= upperpath
.mnt
->mnt_sb
->s_stack_depth
;
1020 lowertmp
= kstrdup(ufs
->config
.lowerdir
, GFP_KERNEL
);
1022 goto out_put_workpath
;
1025 stacklen
= ovl_split_lowerdirs(lowertmp
);
1026 if (stacklen
> OVL_MAX_STACK
) {
1027 pr_err("overlayfs: too many lower directries, limit is %d\n",
1029 goto out_free_lowertmp
;
1030 } else if (!ufs
->config
.upperdir
&& stacklen
== 1) {
1031 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1032 goto out_free_lowertmp
;
1035 stack
= kcalloc(stacklen
, sizeof(struct path
), GFP_KERNEL
);
1037 goto out_free_lowertmp
;
1040 for (numlower
= 0; numlower
< stacklen
; numlower
++) {
1041 err
= ovl_lower_dir(lower
, &stack
[numlower
],
1042 &ufs
->lower_namelen
, &sb
->s_stack_depth
,
1045 goto out_put_lowerpath
;
1047 lower
= strchr(lower
, '\0') + 1;
1051 sb
->s_stack_depth
++;
1052 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
1053 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1054 goto out_put_lowerpath
;
1057 if (ufs
->config
.upperdir
) {
1058 ufs
->upper_mnt
= clone_private_mount(&upperpath
);
1059 err
= PTR_ERR(ufs
->upper_mnt
);
1060 if (IS_ERR(ufs
->upper_mnt
)) {
1061 pr_err("overlayfs: failed to clone upperpath\n");
1062 goto out_put_lowerpath
;
1065 ufs
->workdir
= ovl_workdir_create(ufs
->upper_mnt
, workpath
.dentry
);
1066 err
= PTR_ERR(ufs
->workdir
);
1067 if (IS_ERR(ufs
->workdir
)) {
1068 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1069 ufs
->config
.workdir
, OVL_WORKDIR_NAME
, -err
);
1070 sb
->s_flags
|= MS_RDONLY
;
1071 ufs
->workdir
= NULL
;
1075 * Upper should support d_type, else whiteouts are visible.
1076 * Given workdir and upper are on same fs, we can do
1077 * iterate_dir() on workdir. This check requires successful
1078 * creation of workdir in previous step.
1081 err
= ovl_check_d_type_supported(&workpath
);
1083 goto out_put_workdir
;
1086 pr_err("overlayfs: upper fs needs to support d_type.\n");
1088 goto out_put_workdir
;
1094 ufs
->lower_mnt
= kcalloc(numlower
, sizeof(struct vfsmount
*), GFP_KERNEL
);
1095 if (ufs
->lower_mnt
== NULL
)
1096 goto out_put_workdir
;
1097 for (i
= 0; i
< numlower
; i
++) {
1098 struct vfsmount
*mnt
= clone_private_mount(&stack
[i
]);
1102 pr_err("overlayfs: failed to clone lowerpath\n");
1103 goto out_put_lower_mnt
;
1106 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1107 * will fail instead of modifying lower fs.
1109 mnt
->mnt_flags
|= MNT_READONLY
;
1111 ufs
->lower_mnt
[ufs
->numlower
] = mnt
;
1115 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1116 if (!ufs
->upper_mnt
)
1117 sb
->s_flags
|= MS_RDONLY
;
1120 sb
->s_d_op
= &ovl_reval_dentry_operations
;
1122 sb
->s_d_op
= &ovl_dentry_operations
;
1124 ufs
->creator_cred
= prepare_creds();
1125 if (!ufs
->creator_cred
)
1126 goto out_put_lower_mnt
;
1129 oe
= ovl_alloc_entry(numlower
);
1133 root_dentry
= d_make_root(ovl_new_inode(sb
, S_IFDIR
, oe
));
1137 mntput(upperpath
.mnt
);
1138 for (i
= 0; i
< numlower
; i
++)
1139 mntput(stack
[i
].mnt
);
1140 path_put(&workpath
);
1143 oe
->__upperdentry
= upperpath
.dentry
;
1144 for (i
= 0; i
< numlower
; i
++) {
1145 oe
->lowerstack
[i
].dentry
= stack
[i
].dentry
;
1146 oe
->lowerstack
[i
].mnt
= ufs
->lower_mnt
[i
];
1150 root_dentry
->d_fsdata
= oe
;
1152 ovl_copyattr(ovl_dentry_real(root_dentry
)->d_inode
,
1153 root_dentry
->d_inode
);
1155 sb
->s_magic
= OVERLAYFS_SUPER_MAGIC
;
1156 sb
->s_op
= &ovl_super_operations
;
1157 sb
->s_root
= root_dentry
;
1158 sb
->s_fs_info
= ufs
;
1165 put_cred(ufs
->creator_cred
);
1167 for (i
= 0; i
< ufs
->numlower
; i
++)
1168 mntput(ufs
->lower_mnt
[i
]);
1169 kfree(ufs
->lower_mnt
);
1172 mntput(ufs
->upper_mnt
);
1174 for (i
= 0; i
< numlower
; i
++)
1175 path_put(&stack
[i
]);
1180 path_put(&workpath
);
1182 path_put(&upperpath
);
1184 kfree(ufs
->config
.lowerdir
);
1185 kfree(ufs
->config
.upperdir
);
1186 kfree(ufs
->config
.workdir
);
1192 static struct dentry
*ovl_mount(struct file_system_type
*fs_type
, int flags
,
1193 const char *dev_name
, void *raw_data
)
1195 return mount_nodev(fs_type
, flags
, raw_data
, ovl_fill_super
);
1198 static struct file_system_type ovl_fs_type
= {
1199 .owner
= THIS_MODULE
,
1202 .kill_sb
= kill_anon_super
,
1204 MODULE_ALIAS_FS("overlay");
1206 static int __init
ovl_init(void)
1208 return register_filesystem(&ovl_fs_type
);
1211 static void __exit
ovl_exit(void)
1213 unregister_filesystem(&ovl_fs_type
);
1216 module_init(ovl_init
);
1217 module_exit(ovl_exit
);