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/sched.h>
20 #include <linux/statfs.h>
21 #include <linux/seq_file.h>
22 #include "overlayfs.h"
24 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
25 MODULE_DESCRIPTION("Overlay filesystem");
26 MODULE_LICENSE("GPL");
28 #define OVERLAYFS_SUPER_MAGIC 0x794c7630
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 ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
164 struct ovl_entry
*oe
= dentry
->d_fsdata
;
169 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
171 struct ovl_entry
*oe
= dentry
->d_fsdata
;
176 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
178 struct ovl_entry
*oe
= dentry
->d_fsdata
;
180 *path
= oe
->numlower
? oe
->lowerstack
[0] : (struct path
) { NULL
, NULL
};
183 int ovl_want_write(struct dentry
*dentry
)
185 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
186 return mnt_want_write(ofs
->upper_mnt
);
189 void ovl_drop_write(struct dentry
*dentry
)
191 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
192 mnt_drop_write(ofs
->upper_mnt
);
195 struct dentry
*ovl_workdir(struct dentry
*dentry
)
197 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
201 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
203 struct ovl_entry
*oe
= dentry
->d_fsdata
;
207 void ovl_dentry_set_opaque(struct dentry
*dentry
, bool opaque
)
209 struct ovl_entry
*oe
= dentry
->d_fsdata
;
213 void ovl_dentry_update(struct dentry
*dentry
, struct dentry
*upperdentry
)
215 struct ovl_entry
*oe
= dentry
->d_fsdata
;
217 WARN_ON(!mutex_is_locked(&upperdentry
->d_parent
->d_inode
->i_mutex
));
218 WARN_ON(oe
->__upperdentry
);
219 BUG_ON(!upperdentry
->d_inode
);
221 * Make sure upperdentry is consistent before making it visible to
222 * ovl_upperdentry_dereference().
225 oe
->__upperdentry
= upperdentry
;
228 void ovl_dentry_version_inc(struct dentry
*dentry
)
230 struct ovl_entry
*oe
= dentry
->d_fsdata
;
232 WARN_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
236 u64
ovl_dentry_version_get(struct dentry
*dentry
)
238 struct ovl_entry
*oe
= dentry
->d_fsdata
;
240 WARN_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
244 bool ovl_is_whiteout(struct dentry
*dentry
)
246 struct inode
*inode
= dentry
->d_inode
;
248 return inode
&& IS_WHITEOUT(inode
);
251 const struct cred
*ovl_override_creds(struct super_block
*sb
)
253 struct ovl_fs
*ofs
= sb
->s_fs_info
;
255 return override_creds(ofs
->creator_cred
);
258 static bool ovl_is_opaquedir(struct dentry
*dentry
)
262 struct inode
*inode
= dentry
->d_inode
;
264 if (!S_ISDIR(inode
->i_mode
) || !inode
->i_op
->getxattr
)
267 res
= inode
->i_op
->getxattr(dentry
, OVL_XATTR_OPAQUE
, &val
, 1);
268 if (res
== 1 && val
== 'y')
274 static void ovl_dentry_release(struct dentry
*dentry
)
276 struct ovl_entry
*oe
= dentry
->d_fsdata
;
281 dput(oe
->__upperdentry
);
282 for (i
= 0; i
< oe
->numlower
; i
++)
283 dput(oe
->lowerstack
[i
].dentry
);
288 static struct dentry
*ovl_d_real(struct dentry
*dentry
, struct inode
*inode
)
292 if (d_is_dir(dentry
)) {
293 if (!inode
|| inode
== d_inode(dentry
))
298 real
= ovl_dentry_upper(dentry
);
299 if (real
&& (!inode
|| inode
== d_inode(real
)))
302 real
= ovl_dentry_lower(dentry
);
306 if (!inode
|| inode
== d_inode(real
))
309 /* Handle recursion */
310 if (real
->d_flags
& DCACHE_OP_REAL
)
311 return real
->d_op
->d_real(real
, inode
);
314 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry
,
315 inode
? inode
->i_sb
->s_id
: "NULL", inode
? inode
->i_ino
: 0);
319 static int ovl_dentry_revalidate(struct dentry
*dentry
, unsigned int flags
)
321 struct ovl_entry
*oe
= dentry
->d_fsdata
;
325 for (i
= 0; i
< oe
->numlower
; i
++) {
326 struct dentry
*d
= oe
->lowerstack
[i
].dentry
;
328 if (d
->d_flags
& DCACHE_OP_REVALIDATE
) {
329 ret
= d
->d_op
->d_revalidate(d
, flags
);
333 if (!(flags
& LOOKUP_RCU
))
342 static int ovl_dentry_weak_revalidate(struct dentry
*dentry
, unsigned int flags
)
344 struct ovl_entry
*oe
= dentry
->d_fsdata
;
348 for (i
= 0; i
< oe
->numlower
; i
++) {
349 struct dentry
*d
= oe
->lowerstack
[i
].dentry
;
351 if (d
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
) {
352 ret
= d
->d_op
->d_weak_revalidate(d
, flags
);
360 static const struct dentry_operations ovl_dentry_operations
= {
361 .d_release
= ovl_dentry_release
,
362 .d_select_inode
= ovl_d_select_inode
,
363 .d_real
= ovl_d_real
,
366 static const struct dentry_operations ovl_reval_dentry_operations
= {
367 .d_release
= ovl_dentry_release
,
368 .d_select_inode
= ovl_d_select_inode
,
369 .d_real
= ovl_d_real
,
370 .d_revalidate
= ovl_dentry_revalidate
,
371 .d_weak_revalidate
= ovl_dentry_weak_revalidate
,
374 static struct ovl_entry
*ovl_alloc_entry(unsigned int numlower
)
376 size_t size
= offsetof(struct ovl_entry
, lowerstack
[numlower
]);
377 struct ovl_entry
*oe
= kzalloc(size
, GFP_KERNEL
);
380 oe
->numlower
= numlower
;
385 static bool ovl_dentry_remote(struct dentry
*dentry
)
387 return dentry
->d_flags
&
388 (DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
|
392 static bool ovl_dentry_weird(struct dentry
*dentry
)
394 return dentry
->d_flags
& (DCACHE_NEED_AUTOMOUNT
|
395 DCACHE_MANAGE_TRANSIT
|
400 static inline struct dentry
*ovl_lookup_real(struct dentry
*dir
,
403 struct dentry
*dentry
;
405 mutex_lock(&dir
->d_inode
->i_mutex
);
406 dentry
= lookup_one_len(name
->name
, dir
, name
->len
);
407 mutex_unlock(&dir
->d_inode
->i_mutex
);
409 if (IS_ERR(dentry
)) {
410 if (PTR_ERR(dentry
) == -ENOENT
)
412 } else if (!dentry
->d_inode
) {
415 } else if (ovl_dentry_weird(dentry
)) {
417 /* Don't support traversing automounts and other weirdness */
418 dentry
= ERR_PTR(-EREMOTE
);
424 * Returns next layer in stack starting from top.
425 * Returns -1 if this is the last layer.
427 int ovl_path_next(int idx
, struct dentry
*dentry
, struct path
*path
)
429 struct ovl_entry
*oe
= dentry
->d_fsdata
;
433 ovl_path_upper(dentry
, path
);
435 return oe
->numlower
? 1 : -1;
438 BUG_ON(idx
> oe
->numlower
);
439 *path
= oe
->lowerstack
[idx
- 1];
441 return (idx
< oe
->numlower
) ? idx
+ 1 : -1;
444 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
447 struct ovl_entry
*oe
;
448 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
449 struct path
*stack
= NULL
;
450 struct dentry
*upperdir
, *upperdentry
= NULL
;
451 unsigned int ctr
= 0;
452 struct inode
*inode
= NULL
;
453 bool upperopaque
= false;
454 struct dentry
*this, *prev
= NULL
;
458 upperdir
= ovl_upperdentry_dereference(poe
);
460 this = ovl_lookup_real(upperdir
, &dentry
->d_name
);
466 if (unlikely(ovl_dentry_remote(this))) {
471 if (ovl_is_whiteout(this)) {
475 } else if (poe
->numlower
&& ovl_is_opaquedir(this)) {
479 upperdentry
= prev
= this;
482 if (!upperopaque
&& poe
->numlower
) {
484 stack
= kcalloc(poe
->numlower
, sizeof(struct path
), GFP_KERNEL
);
489 for (i
= 0; !upperopaque
&& i
< poe
->numlower
; i
++) {
491 struct path lowerpath
= poe
->lowerstack
[i
];
493 this = ovl_lookup_real(lowerpath
.dentry
, &dentry
->d_name
);
497 * If it's positive, then treat ENAMETOOLONG as ENOENT.
499 if (err
== -ENAMETOOLONG
&& (upperdentry
|| ctr
))
505 if (ovl_is_whiteout(this)) {
510 * Only makes sense to check opaque dir if this is not the
513 if (i
< poe
->numlower
- 1 && ovl_is_opaquedir(this))
516 if (prev
&& (!S_ISDIR(prev
->d_inode
->i_mode
) ||
517 !S_ISDIR(this->d_inode
->i_mode
))) {
519 * FIXME: check for upper-opaqueness maybe better done
522 if (prev
== upperdentry
)
528 * If this is a non-directory then stop here.
530 if (!S_ISDIR(this->d_inode
->i_mode
))
533 stack
[ctr
].dentry
= this;
534 stack
[ctr
].mnt
= lowerpath
.mnt
;
541 oe
= ovl_alloc_entry(ctr
);
546 if (upperdentry
|| ctr
) {
547 struct dentry
*realdentry
;
549 realdentry
= upperdentry
? upperdentry
: stack
[0].dentry
;
552 inode
= ovl_new_inode(dentry
->d_sb
, realdentry
->d_inode
->i_mode
,
556 ovl_copyattr(realdentry
->d_inode
, inode
);
559 oe
->opaque
= upperopaque
;
560 oe
->__upperdentry
= upperdentry
;
561 memcpy(oe
->lowerstack
, stack
, sizeof(struct path
) * ctr
);
563 dentry
->d_fsdata
= oe
;
564 d_add(dentry
, inode
);
571 for (i
= 0; i
< ctr
; i
++)
572 dput(stack
[i
].dentry
);
580 struct file
*ovl_path_open(struct path
*path
, int flags
)
582 return dentry_open(path
, flags
, current_cred());
585 static void ovl_put_super(struct super_block
*sb
)
587 struct ovl_fs
*ufs
= sb
->s_fs_info
;
591 mntput(ufs
->upper_mnt
);
592 for (i
= 0; i
< ufs
->numlower
; i
++)
593 mntput(ufs
->lower_mnt
[i
]);
594 kfree(ufs
->lower_mnt
);
596 kfree(ufs
->config
.lowerdir
);
597 kfree(ufs
->config
.upperdir
);
598 kfree(ufs
->config
.workdir
);
599 put_cred(ufs
->creator_cred
);
605 * @sb: The overlayfs super block
606 * @buf: The struct kstatfs to fill in with stats
608 * Get the filesystem statistics. As writes always target the upper layer
609 * filesystem pass the statfs to the upper filesystem (if it exists)
611 static int ovl_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
613 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
614 struct dentry
*root_dentry
= dentry
->d_sb
->s_root
;
618 ovl_path_real(root_dentry
, &path
);
620 err
= vfs_statfs(&path
, buf
);
622 buf
->f_namelen
= max(buf
->f_namelen
, ofs
->lower_namelen
);
623 buf
->f_type
= OVERLAYFS_SUPER_MAGIC
;
632 * Prints the mount options for a given superblock.
633 * Returns zero; does not fail.
635 static int ovl_show_options(struct seq_file
*m
, struct dentry
*dentry
)
637 struct super_block
*sb
= dentry
->d_sb
;
638 struct ovl_fs
*ufs
= sb
->s_fs_info
;
640 seq_show_option(m
, "lowerdir", ufs
->config
.lowerdir
);
641 if (ufs
->config
.upperdir
) {
642 seq_show_option(m
, "upperdir", ufs
->config
.upperdir
);
643 seq_show_option(m
, "workdir", ufs
->config
.workdir
);
648 static int ovl_remount(struct super_block
*sb
, int *flags
, char *data
)
650 struct ovl_fs
*ufs
= sb
->s_fs_info
;
652 if (!(*flags
& MS_RDONLY
) && (!ufs
->upper_mnt
|| !ufs
->workdir
))
658 static const struct super_operations ovl_super_operations
= {
659 .put_super
= ovl_put_super
,
660 .statfs
= ovl_statfs
,
661 .show_options
= ovl_show_options
,
662 .remount_fs
= ovl_remount
,
672 static const match_table_t ovl_tokens
= {
673 {OPT_LOWERDIR
, "lowerdir=%s"},
674 {OPT_UPPERDIR
, "upperdir=%s"},
675 {OPT_WORKDIR
, "workdir=%s"},
679 static char *ovl_next_opt(char **s
)
687 for (p
= sbegin
; *p
; p
++) {
692 } else if (*p
== ',') {
702 static int ovl_parse_opt(char *opt
, struct ovl_config
*config
)
706 while ((p
= ovl_next_opt(&opt
)) != NULL
) {
708 substring_t args
[MAX_OPT_ARGS
];
713 token
= match_token(p
, ovl_tokens
, args
);
716 kfree(config
->upperdir
);
717 config
->upperdir
= match_strdup(&args
[0]);
718 if (!config
->upperdir
)
723 kfree(config
->lowerdir
);
724 config
->lowerdir
= match_strdup(&args
[0]);
725 if (!config
->lowerdir
)
730 kfree(config
->workdir
);
731 config
->workdir
= match_strdup(&args
[0]);
732 if (!config
->workdir
)
737 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p
);
742 /* Workdir is useless in non-upper mount */
743 if (!config
->upperdir
&& config
->workdir
) {
744 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
746 kfree(config
->workdir
);
747 config
->workdir
= NULL
;
753 #define OVL_WORKDIR_NAME "work"
755 static struct dentry
*ovl_workdir_create(struct vfsmount
*mnt
,
756 struct dentry
*dentry
)
758 struct inode
*dir
= dentry
->d_inode
;
761 bool retried
= false;
763 err
= mnt_want_write(mnt
);
767 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
769 work
= lookup_one_len(OVL_WORKDIR_NAME
, dentry
,
770 strlen(OVL_WORKDIR_NAME
));
773 struct kstat stat
= {
776 struct iattr attr
= {
777 .ia_valid
= ATTR_MODE
,
778 .ia_mode
= stat
.mode
,
787 ovl_workdir_cleanup(dir
, mnt
, work
, 0);
792 err
= ovl_create_real(dir
, work
, &stat
, NULL
, NULL
, true);
796 err
= vfs_removexattr(work
, XATTR_NAME_POSIX_ACL_DEFAULT
);
797 if (err
&& err
!= -ENODATA
&& err
!= -EOPNOTSUPP
)
800 err
= vfs_removexattr(work
, XATTR_NAME_POSIX_ACL_ACCESS
);
801 if (err
&& err
!= -ENODATA
&& err
!= -EOPNOTSUPP
)
804 /* Clear any inherited mode bits */
805 inode_lock(work
->d_inode
);
806 err
= notify_change(work
, &attr
, NULL
);
807 inode_unlock(work
->d_inode
);
812 mutex_unlock(&dir
->i_mutex
);
823 static void ovl_unescape(char *s
)
836 static int ovl_mount_dir_noesc(const char *name
, struct path
*path
)
841 pr_err("overlayfs: empty lowerdir\n");
844 err
= kern_path(name
, LOOKUP_FOLLOW
, path
);
846 pr_err("overlayfs: failed to resolve '%s': %i\n", name
, err
);
850 if (ovl_dentry_weird(path
->dentry
)) {
851 pr_err("overlayfs: filesystem on '%s' not supported\n", name
);
854 if (!S_ISDIR(path
->dentry
->d_inode
->i_mode
)) {
855 pr_err("overlayfs: '%s' not a directory\n", name
);
866 static int ovl_mount_dir(const char *name
, struct path
*path
)
869 char *tmp
= kstrdup(name
, GFP_KERNEL
);
873 err
= ovl_mount_dir_noesc(tmp
, path
);
876 if (ovl_dentry_remote(path
->dentry
)) {
877 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
887 static int ovl_lower_dir(const char *name
, struct path
*path
, long *namelen
,
888 int *stack_depth
, bool *remote
)
891 struct kstatfs statfs
;
893 err
= ovl_mount_dir_noesc(name
, path
);
897 err
= vfs_statfs(path
, &statfs
);
899 pr_err("overlayfs: statfs failed on '%s'\n", name
);
902 *namelen
= max(*namelen
, statfs
.f_namelen
);
903 *stack_depth
= max(*stack_depth
, path
->mnt
->mnt_sb
->s_stack_depth
);
905 if (ovl_dentry_remote(path
->dentry
))
916 /* Workdir should not be subdir of upperdir and vice versa */
917 static bool ovl_workdir_ok(struct dentry
*workdir
, struct dentry
*upperdir
)
921 if (workdir
!= upperdir
) {
922 ok
= (lock_rename(workdir
, upperdir
) == NULL
);
923 unlock_rename(workdir
, upperdir
);
928 static unsigned int ovl_split_lowerdirs(char *str
)
930 unsigned int ctr
= 1;
933 for (s
= d
= str
;; s
++, d
++) {
936 } else if (*s
== ':') {
948 static int ovl_fill_super(struct super_block
*sb
, void *data
, int silent
)
950 struct path upperpath
= { NULL
, NULL
};
951 struct path workpath
= { NULL
, NULL
};
952 struct dentry
*root_dentry
;
953 struct ovl_entry
*oe
;
955 struct path
*stack
= NULL
;
958 unsigned int numlower
;
959 unsigned int stacklen
= 0;
965 ufs
= kzalloc(sizeof(struct ovl_fs
), GFP_KERNEL
);
969 err
= ovl_parse_opt((char *) data
, &ufs
->config
);
971 goto out_free_config
;
974 if (!ufs
->config
.lowerdir
) {
975 pr_err("overlayfs: missing 'lowerdir'\n");
976 goto out_free_config
;
979 sb
->s_stack_depth
= 0;
980 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
981 if (ufs
->config
.upperdir
) {
982 if (!ufs
->config
.workdir
) {
983 pr_err("overlayfs: missing 'workdir'\n");
984 goto out_free_config
;
987 err
= ovl_mount_dir(ufs
->config
.upperdir
, &upperpath
);
989 goto out_free_config
;
991 /* Upper fs should not be r/o */
992 if (upperpath
.mnt
->mnt_sb
->s_flags
& MS_RDONLY
) {
993 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
995 goto out_put_upperpath
;
998 err
= ovl_mount_dir(ufs
->config
.workdir
, &workpath
);
1000 goto out_put_upperpath
;
1003 if (upperpath
.mnt
!= workpath
.mnt
) {
1004 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1005 goto out_put_workpath
;
1007 if (!ovl_workdir_ok(workpath
.dentry
, upperpath
.dentry
)) {
1008 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1009 goto out_put_workpath
;
1011 sb
->s_stack_depth
= upperpath
.mnt
->mnt_sb
->s_stack_depth
;
1014 lowertmp
= kstrdup(ufs
->config
.lowerdir
, GFP_KERNEL
);
1016 goto out_put_workpath
;
1019 stacklen
= ovl_split_lowerdirs(lowertmp
);
1020 if (stacklen
> OVL_MAX_STACK
) {
1021 pr_err("overlayfs: too many lower directries, limit is %d\n",
1023 goto out_free_lowertmp
;
1024 } else if (!ufs
->config
.upperdir
&& stacklen
== 1) {
1025 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1026 goto out_free_lowertmp
;
1029 stack
= kcalloc(stacklen
, sizeof(struct path
), GFP_KERNEL
);
1031 goto out_free_lowertmp
;
1034 for (numlower
= 0; numlower
< stacklen
; numlower
++) {
1035 err
= ovl_lower_dir(lower
, &stack
[numlower
],
1036 &ufs
->lower_namelen
, &sb
->s_stack_depth
,
1039 goto out_put_lowerpath
;
1041 lower
= strchr(lower
, '\0') + 1;
1045 sb
->s_stack_depth
++;
1046 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
1047 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1048 goto out_put_lowerpath
;
1051 if (ufs
->config
.upperdir
) {
1052 ufs
->upper_mnt
= clone_private_mount(&upperpath
);
1053 err
= PTR_ERR(ufs
->upper_mnt
);
1054 if (IS_ERR(ufs
->upper_mnt
)) {
1055 pr_err("overlayfs: failed to clone upperpath\n");
1056 goto out_put_lowerpath
;
1059 ufs
->workdir
= ovl_workdir_create(ufs
->upper_mnt
, workpath
.dentry
);
1060 err
= PTR_ERR(ufs
->workdir
);
1061 if (IS_ERR(ufs
->workdir
)) {
1062 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1063 ufs
->config
.workdir
, OVL_WORKDIR_NAME
, -err
);
1064 sb
->s_flags
|= MS_RDONLY
;
1065 ufs
->workdir
= NULL
;
1069 * Upper should support d_type, else whiteouts are visible.
1070 * Given workdir and upper are on same fs, we can do
1071 * iterate_dir() on workdir. This check requires successful
1072 * creation of workdir in previous step.
1075 err
= ovl_check_d_type_supported(&workpath
);
1077 goto out_put_workdir
;
1080 * We allowed this configuration and don't want to
1081 * break users over kernel upgrade. So warn instead
1085 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1090 ufs
->lower_mnt
= kcalloc(numlower
, sizeof(struct vfsmount
*), GFP_KERNEL
);
1091 if (ufs
->lower_mnt
== NULL
)
1092 goto out_put_workdir
;
1093 for (i
= 0; i
< numlower
; i
++) {
1094 struct vfsmount
*mnt
= clone_private_mount(&stack
[i
]);
1098 pr_err("overlayfs: failed to clone lowerpath\n");
1099 goto out_put_lower_mnt
;
1102 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1103 * will fail instead of modifying lower fs.
1105 mnt
->mnt_flags
|= MNT_READONLY
;
1107 ufs
->lower_mnt
[ufs
->numlower
] = mnt
;
1111 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1112 if (!ufs
->upper_mnt
)
1113 sb
->s_flags
|= MS_RDONLY
;
1116 sb
->s_d_op
= &ovl_reval_dentry_operations
;
1118 sb
->s_d_op
= &ovl_dentry_operations
;
1120 ufs
->creator_cred
= prepare_creds();
1121 if (!ufs
->creator_cred
)
1122 goto out_put_lower_mnt
;
1125 oe
= ovl_alloc_entry(numlower
);
1129 root_dentry
= d_make_root(ovl_new_inode(sb
, S_IFDIR
, oe
));
1133 mntput(upperpath
.mnt
);
1134 for (i
= 0; i
< numlower
; i
++)
1135 mntput(stack
[i
].mnt
);
1136 path_put(&workpath
);
1139 oe
->__upperdentry
= upperpath
.dentry
;
1140 for (i
= 0; i
< numlower
; i
++) {
1141 oe
->lowerstack
[i
].dentry
= stack
[i
].dentry
;
1142 oe
->lowerstack
[i
].mnt
= ufs
->lower_mnt
[i
];
1146 root_dentry
->d_fsdata
= oe
;
1148 ovl_copyattr(ovl_dentry_real(root_dentry
)->d_inode
,
1149 root_dentry
->d_inode
);
1151 sb
->s_magic
= OVERLAYFS_SUPER_MAGIC
;
1152 sb
->s_op
= &ovl_super_operations
;
1153 sb
->s_root
= root_dentry
;
1154 sb
->s_fs_info
= ufs
;
1161 put_cred(ufs
->creator_cred
);
1163 for (i
= 0; i
< ufs
->numlower
; i
++)
1164 mntput(ufs
->lower_mnt
[i
]);
1165 kfree(ufs
->lower_mnt
);
1168 mntput(ufs
->upper_mnt
);
1170 for (i
= 0; i
< numlower
; i
++)
1171 path_put(&stack
[i
]);
1176 path_put(&workpath
);
1178 path_put(&upperpath
);
1180 kfree(ufs
->config
.lowerdir
);
1181 kfree(ufs
->config
.upperdir
);
1182 kfree(ufs
->config
.workdir
);
1188 static struct dentry
*ovl_mount(struct file_system_type
*fs_type
, int flags
,
1189 const char *dev_name
, void *raw_data
)
1191 return mount_nodev(fs_type
, flags
, raw_data
, ovl_fill_super
);
1194 static struct file_system_type ovl_fs_type
= {
1195 .owner
= THIS_MODULE
,
1198 .kill_sb
= kill_anon_super
,
1200 MODULE_ALIAS_FS("overlay");
1202 static int __init
ovl_init(void)
1204 return register_filesystem(&ovl_fs_type
);
1207 static void __exit
ovl_exit(void)
1209 unregister_filesystem(&ovl_fs_type
);
1212 module_init(ovl_init
);
1213 module_exit(ovl_exit
);