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 <linux/posix_acl_xattr.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 static void ovl_inode_init(struct inode *inode, struct inode *realinode,
151 WRITE_ONCE(inode->i_private, (unsigned long) realinode |
152 (is_upper ? OVL_ISUPPER_MASK : 0));
155 struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
159 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
161 return ofs->upper_mnt;
163 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
167 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
169 struct ovl_entry *oe = dentry->d_fsdata;
174 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
176 struct ovl_entry *oe = dentry->d_fsdata;
181 void ovl_path_lower(struct dentry *dentry, struct path *path)
183 struct ovl_entry *oe = dentry->d_fsdata;
185 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
188 int ovl_want_write(struct dentry *dentry)
190 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
191 return mnt_want_write(ofs->upper_mnt);
194 void ovl_drop_write(struct dentry *dentry)
196 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
197 mnt_drop_write(ofs->upper_mnt);
200 struct dentry *ovl_workdir(struct dentry *dentry)
202 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
206 bool ovl_dentry_is_opaque(struct dentry *dentry)
208 struct ovl_entry *oe = dentry->d_fsdata;
212 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
214 struct ovl_entry *oe = dentry->d_fsdata;
218 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
220 struct ovl_entry *oe = dentry->d_fsdata;
222 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
223 WARN_ON(oe->__upperdentry);
225 * Make sure upperdentry is consistent before making it visible to
226 * ovl_upperdentry_dereference().
229 oe->__upperdentry = upperdentry;
232 void ovl_inode_update(struct inode *inode, struct inode *upperinode)
234 WARN_ON(!upperinode);
235 WARN_ON(!inode_unhashed(inode));
236 WRITE_ONCE(inode->i_private,
237 (unsigned long) upperinode | OVL_ISUPPER_MASK);
238 if (!S_ISDIR(upperinode->i_mode))
239 __insert_inode_hash(inode, (unsigned long) upperinode);
242 void ovl_dentry_version_inc(struct dentry *dentry)
244 struct ovl_entry *oe = dentry->d_fsdata;
246 WARN_ON(!inode_is_locked(dentry->d_inode));
250 u64 ovl_dentry_version_get(struct dentry *dentry)
252 struct ovl_entry *oe = dentry->d_fsdata;
254 WARN_ON(!inode_is_locked(dentry->d_inode));
258 bool ovl_is_whiteout(struct dentry *dentry)
260 struct inode *inode = dentry->d_inode;
262 return inode && IS_WHITEOUT(inode);
265 const struct cred *ovl_override_creds(struct super_block *sb)
267 struct ovl_fs *ofs = sb->s_fs_info;
269 return override_creds(ofs->creator_cred);
272 static bool ovl_is_opaquedir(struct dentry *dentry)
276 struct inode *inode = dentry->d_inode;
278 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
281 res = inode->i_op->getxattr(dentry, inode, OVL_XATTR_OPAQUE, &val, 1);
282 if (res == 1 && val == 'y')
288 static void ovl_dentry_release(struct dentry *dentry)
290 struct ovl_entry *oe = dentry->d_fsdata;
295 dput(oe->__upperdentry);
296 for (i = 0; i < oe->numlower; i++)
297 dput(oe->lowerstack[i].dentry);
302 static struct dentry *ovl_d_real(struct dentry *dentry,
303 const struct inode *inode,
304 unsigned int open_flags)
308 if (d_is_dir(dentry)) {
309 if (!inode || inode == d_inode(dentry))
314 if (d_is_negative(dentry))
318 int err = ovl_open_maybe_copy_up(dentry, open_flags);
324 real = ovl_dentry_upper(dentry);
325 if (real && (!inode || inode == d_inode(real)))
328 real = ovl_dentry_lower(dentry);
332 if (!inode || inode == d_inode(real))
335 /* Handle recursion */
336 return d_real(real, inode, open_flags);
338 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
339 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
343 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
345 struct ovl_entry *oe = dentry->d_fsdata;
349 for (i = 0; i < oe->numlower; i++) {
350 struct dentry *d = oe->lowerstack[i].dentry;
352 if (d->d_flags & DCACHE_OP_REVALIDATE) {
353 ret = d->d_op->d_revalidate(d, flags);
357 if (!(flags & LOOKUP_RCU))
366 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
368 struct ovl_entry *oe = dentry->d_fsdata;
372 for (i = 0; i < oe->numlower; i++) {
373 struct dentry *d = oe->lowerstack[i].dentry;
375 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
376 ret = d->d_op->d_weak_revalidate(d, flags);
384 static const struct dentry_operations ovl_dentry_operations = {
385 .d_release = ovl_dentry_release,
386 .d_real = ovl_d_real,
389 static const struct dentry_operations ovl_reval_dentry_operations = {
390 .d_release = ovl_dentry_release,
391 .d_real = ovl_d_real,
392 .d_revalidate = ovl_dentry_revalidate,
393 .d_weak_revalidate = ovl_dentry_weak_revalidate,
396 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
398 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
399 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
402 oe->numlower = numlower;
407 static bool ovl_dentry_remote(struct dentry *dentry)
409 return dentry->d_flags &
410 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
414 static bool ovl_dentry_weird(struct dentry *dentry)
416 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
417 DCACHE_MANAGE_TRANSIT |
422 static inline struct dentry *ovl_lookup_real(struct super_block *ovl_sb,
424 const struct qstr *name)
426 const struct cred *old_cred;
427 struct dentry *dentry;
429 old_cred = ovl_override_creds(ovl_sb);
430 dentry = lookup_one_len_unlocked(name->name, dir, name->len);
431 revert_creds(old_cred);
433 if (IS_ERR(dentry)) {
434 if (PTR_ERR(dentry) == -ENOENT)
436 } else if (!dentry->d_inode) {
439 } else if (ovl_dentry_weird(dentry)) {
441 /* Don't support traversing automounts and other weirdness */
442 dentry = ERR_PTR(-EREMOTE);
448 * Returns next layer in stack starting from top.
449 * Returns -1 if this is the last layer.
451 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
453 struct ovl_entry *oe = dentry->d_fsdata;
457 ovl_path_upper(dentry, path);
459 return oe->numlower ? 1 : -1;
462 BUG_ON(idx > oe->numlower);
463 *path = oe->lowerstack[idx - 1];
465 return (idx < oe->numlower) ? idx + 1 : -1;
468 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
471 struct ovl_entry *oe;
472 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
473 struct path *stack = NULL;
474 struct dentry *upperdir, *upperdentry = NULL;
475 unsigned int ctr = 0;
476 struct inode *inode = NULL;
477 bool upperopaque = false;
478 struct dentry *this, *prev = NULL;
482 upperdir = ovl_upperdentry_dereference(poe);
484 this = ovl_lookup_real(dentry->d_sb, upperdir, &dentry->d_name);
490 if (unlikely(ovl_dentry_remote(this))) {
495 if (ovl_is_whiteout(this)) {
499 } else if (poe->numlower && ovl_is_opaquedir(this)) {
503 upperdentry = prev = this;
506 if (!upperopaque && poe->numlower) {
508 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
513 for (i = 0; !upperopaque && i < poe->numlower; i++) {
515 struct path lowerpath = poe->lowerstack[i];
517 this = ovl_lookup_real(dentry->d_sb,
518 lowerpath.dentry, &dentry->d_name);
522 * If it's positive, then treat ENAMETOOLONG as ENOENT.
524 if (err == -ENAMETOOLONG && (upperdentry || ctr))
530 if (ovl_is_whiteout(this)) {
535 * Only makes sense to check opaque dir if this is not the
538 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
541 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
542 !S_ISDIR(this->d_inode->i_mode))) {
544 * FIXME: check for upper-opaqueness maybe better done
547 if (prev == upperdentry)
553 * If this is a non-directory then stop here.
555 if (!S_ISDIR(this->d_inode->i_mode))
558 stack[ctr].dentry = this;
559 stack[ctr].mnt = lowerpath.mnt;
566 oe = ovl_alloc_entry(ctr);
571 if (upperdentry || ctr) {
572 struct dentry *realdentry;
573 struct inode *realinode;
575 realdentry = upperdentry ? upperdentry : stack[0].dentry;
576 realinode = d_inode(realdentry);
579 if (upperdentry && !d_is_dir(upperdentry)) {
580 inode = ovl_get_inode(dentry->d_sb, realinode);
582 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode);
584 ovl_inode_init(inode, realinode, !!upperdentry);
588 ovl_copyattr(realdentry->d_inode, inode);
591 oe->opaque = upperopaque;
592 oe->__upperdentry = upperdentry;
593 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
595 dentry->d_fsdata = oe;
596 d_add(dentry, inode);
603 for (i = 0; i < ctr; i++)
604 dput(stack[i].dentry);
612 struct file *ovl_path_open(struct path *path, int flags)
614 return dentry_open(path, flags | O_NOATIME, current_cred());
617 static void ovl_put_super(struct super_block *sb)
619 struct ovl_fs *ufs = sb->s_fs_info;
623 mntput(ufs->upper_mnt);
624 for (i = 0; i < ufs->numlower; i++)
625 mntput(ufs->lower_mnt[i]);
626 kfree(ufs->lower_mnt);
628 kfree(ufs->config.lowerdir);
629 kfree(ufs->config.upperdir);
630 kfree(ufs->config.workdir);
631 put_cred(ufs->creator_cred);
637 * @sb: The overlayfs super block
638 * @buf: The struct kstatfs to fill in with stats
640 * Get the filesystem statistics. As writes always target the upper layer
641 * filesystem pass the statfs to the upper filesystem (if it exists)
643 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
645 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
646 struct dentry *root_dentry = dentry->d_sb->s_root;
650 ovl_path_real(root_dentry, &path);
652 err = vfs_statfs(&path, buf);
654 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
655 buf->f_type = OVERLAYFS_SUPER_MAGIC;
664 * Prints the mount options for a given superblock.
665 * Returns zero; does not fail.
667 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
669 struct super_block *sb = dentry->d_sb;
670 struct ovl_fs *ufs = sb->s_fs_info;
672 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
673 if (ufs->config.upperdir) {
674 seq_show_option(m, "upperdir", ufs->config.upperdir);
675 seq_show_option(m, "workdir", ufs->config.workdir);
677 if (ufs->config.default_permissions)
678 seq_puts(m, ",default_permissions");
682 static int ovl_remount(struct super_block *sb, int *flags, char *data)
684 struct ovl_fs *ufs = sb->s_fs_info;
686 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
692 static const struct super_operations ovl_super_operations = {
693 .put_super = ovl_put_super,
694 .statfs = ovl_statfs,
695 .show_options = ovl_show_options,
696 .remount_fs = ovl_remount,
697 .drop_inode = generic_delete_inode,
704 OPT_DEFAULT_PERMISSIONS,
708 static const match_table_t ovl_tokens = {
709 {OPT_LOWERDIR, "lowerdir=%s"},
710 {OPT_UPPERDIR, "upperdir=%s"},
711 {OPT_WORKDIR, "workdir=%s"},
712 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
716 static char *ovl_next_opt(char **s)
724 for (p = sbegin; *p; p++) {
729 } else if (*p == ',') {
739 static int ovl_parse_opt(char *opt, struct ovl_config *config)
743 while ((p = ovl_next_opt(&opt)) != NULL) {
745 substring_t args[MAX_OPT_ARGS];
750 token = match_token(p, ovl_tokens, args);
753 kfree(config->upperdir);
754 config->upperdir = match_strdup(&args[0]);
755 if (!config->upperdir)
760 kfree(config->lowerdir);
761 config->lowerdir = match_strdup(&args[0]);
762 if (!config->lowerdir)
767 kfree(config->workdir);
768 config->workdir = match_strdup(&args[0]);
769 if (!config->workdir)
773 case OPT_DEFAULT_PERMISSIONS:
774 config->default_permissions = true;
778 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
783 /* Workdir is useless in non-upper mount */
784 if (!config->upperdir && config->workdir) {
785 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
787 kfree(config->workdir);
788 config->workdir = NULL;
794 #define OVL_WORKDIR_NAME "work"
796 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
797 struct dentry *dentry)
799 struct inode *dir = dentry->d_inode;
802 bool retried = false;
804 err = mnt_want_write(mnt);
808 inode_lock_nested(dir, I_MUTEX_PARENT);
810 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
811 strlen(OVL_WORKDIR_NAME));
814 struct kstat stat = {
824 ovl_cleanup(dir, work);
829 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
845 static void ovl_unescape(char *s)
858 static int ovl_mount_dir_noesc(const char *name, struct path *path)
863 pr_err("overlayfs: empty lowerdir\n");
866 err = kern_path(name, LOOKUP_FOLLOW, path);
868 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
872 if (ovl_dentry_weird(path->dentry)) {
873 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
876 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
877 pr_err("overlayfs: '%s' not a directory\n", name);
888 static int ovl_mount_dir(const char *name, struct path *path)
891 char *tmp = kstrdup(name, GFP_KERNEL);
895 err = ovl_mount_dir_noesc(tmp, path);
898 if (ovl_dentry_remote(path->dentry)) {
899 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
909 static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
910 int *stack_depth, bool *remote)
913 struct kstatfs statfs;
915 err = ovl_mount_dir_noesc(name, path);
919 err = vfs_statfs(path, &statfs);
921 pr_err("overlayfs: statfs failed on '%s'\n", name);
924 *namelen = max(*namelen, statfs.f_namelen);
925 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
927 if (ovl_dentry_remote(path->dentry))
938 /* Workdir should not be subdir of upperdir and vice versa */
939 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
943 if (workdir != upperdir) {
944 ok = (lock_rename(workdir, upperdir) == NULL);
945 unlock_rename(workdir, upperdir);
950 static unsigned int ovl_split_lowerdirs(char *str)
952 unsigned int ctr = 1;
955 for (s = d = str;; s++, d++) {
958 } else if (*s == ':') {
970 static int ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
971 struct dentry *dentry, struct inode *inode,
972 const char *name, const void *value,
973 size_t size, int flags)
975 struct dentry *workdir = ovl_workdir(dentry);
976 struct inode *realinode = ovl_inode_real(inode, NULL);
977 struct posix_acl *acl = NULL;
980 /* Check that everything is OK before copy-up */
982 acl = posix_acl_from_xattr(&init_user_ns, value, size);
987 if (!IS_POSIXACL(d_inode(workdir)))
988 goto out_acl_release;
989 if (!realinode->i_op->set_acl)
990 goto out_acl_release;
991 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
992 err = acl ? -EACCES : 0;
993 goto out_acl_release;
996 if (!inode_owner_or_capable(inode))
997 goto out_acl_release;
999 posix_acl_release(acl);
1001 return ovl_setxattr(dentry, inode, handler->name, value, size, flags);
1004 posix_acl_release(acl);
1008 static int ovl_other_xattr_set(const struct xattr_handler *handler,
1009 struct dentry *dentry, struct inode *inode,
1010 const char *name, const void *value,
1011 size_t size, int flags)
1013 return ovl_setxattr(dentry, inode, name, value, size, flags);
1016 static int ovl_own_xattr_set(const struct xattr_handler *handler,
1017 struct dentry *dentry, struct inode *inode,
1018 const char *name, const void *value,
1019 size_t size, int flags)
1024 static const struct xattr_handler ovl_posix_acl_access_xattr_handler = {
1025 .name = XATTR_NAME_POSIX_ACL_ACCESS,
1026 .flags = ACL_TYPE_ACCESS,
1027 .set = ovl_posix_acl_xattr_set,
1030 static const struct xattr_handler ovl_posix_acl_default_xattr_handler = {
1031 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1032 .flags = ACL_TYPE_DEFAULT,
1033 .set = ovl_posix_acl_xattr_set,
1036 static const struct xattr_handler ovl_own_xattr_handler = {
1037 .prefix = OVL_XATTR_PREFIX,
1038 .set = ovl_own_xattr_set,
1041 static const struct xattr_handler ovl_other_xattr_handler = {
1042 .prefix = "", /* catch all */
1043 .set = ovl_other_xattr_set,
1046 static const struct xattr_handler *ovl_xattr_handlers[] = {
1047 &ovl_posix_acl_access_xattr_handler,
1048 &ovl_posix_acl_default_xattr_handler,
1049 &ovl_own_xattr_handler,
1050 &ovl_other_xattr_handler,
1054 static const struct xattr_handler *ovl_xattr_noacl_handlers[] = {
1055 &ovl_own_xattr_handler,
1056 &ovl_other_xattr_handler,
1060 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1062 struct path upperpath = { NULL, NULL };
1063 struct path workpath = { NULL, NULL };
1064 struct dentry *root_dentry;
1065 struct inode *realinode;
1066 struct ovl_entry *oe;
1068 struct path *stack = NULL;
1071 unsigned int numlower;
1072 unsigned int stacklen = 0;
1074 bool remote = false;
1078 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1082 err = ovl_parse_opt((char *) data, &ufs->config);
1084 goto out_free_config;
1087 if (!ufs->config.lowerdir) {
1089 pr_err("overlayfs: missing 'lowerdir'\n");
1090 goto out_free_config;
1093 sb->s_stack_depth = 0;
1094 sb->s_maxbytes = MAX_LFS_FILESIZE;
1095 if (ufs->config.upperdir) {
1096 if (!ufs->config.workdir) {
1097 pr_err("overlayfs: missing 'workdir'\n");
1098 goto out_free_config;
1101 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
1103 goto out_free_config;
1105 /* Upper fs should not be r/o */
1106 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
1107 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1109 goto out_put_upperpath;
1112 err = ovl_mount_dir(ufs->config.workdir, &workpath);
1114 goto out_put_upperpath;
1117 if (upperpath.mnt != workpath.mnt) {
1118 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1119 goto out_put_workpath;
1121 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1122 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1123 goto out_put_workpath;
1125 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
1128 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1130 goto out_put_workpath;
1133 stacklen = ovl_split_lowerdirs(lowertmp);
1134 if (stacklen > OVL_MAX_STACK) {
1135 pr_err("overlayfs: too many lower directries, limit is %d\n",
1137 goto out_free_lowertmp;
1138 } else if (!ufs->config.upperdir && stacklen == 1) {
1139 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1140 goto out_free_lowertmp;
1143 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1145 goto out_free_lowertmp;
1148 for (numlower = 0; numlower < stacklen; numlower++) {
1149 err = ovl_lower_dir(lower, &stack[numlower],
1150 &ufs->lower_namelen, &sb->s_stack_depth,
1153 goto out_put_lowerpath;
1155 lower = strchr(lower, '\0') + 1;
1159 sb->s_stack_depth++;
1160 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1161 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1162 goto out_put_lowerpath;
1165 if (ufs->config.upperdir) {
1166 ufs->upper_mnt = clone_private_mount(&upperpath);
1167 err = PTR_ERR(ufs->upper_mnt);
1168 if (IS_ERR(ufs->upper_mnt)) {
1169 pr_err("overlayfs: failed to clone upperpath\n");
1170 goto out_put_lowerpath;
1172 /* Don't inherit atime flags */
1173 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1175 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
1177 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1178 err = PTR_ERR(ufs->workdir);
1179 if (IS_ERR(ufs->workdir)) {
1180 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1181 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1182 sb->s_flags |= MS_RDONLY;
1183 ufs->workdir = NULL;
1187 * Upper should support d_type, else whiteouts are visible.
1188 * Given workdir and upper are on same fs, we can do
1189 * iterate_dir() on workdir. This check requires successful
1190 * creation of workdir in previous step.
1193 err = ovl_check_d_type_supported(&workpath);
1195 goto out_put_workdir;
1198 * We allowed this configuration and don't want to
1199 * break users over kernel upgrade. So warn instead
1203 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1208 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1209 if (ufs->lower_mnt == NULL)
1210 goto out_put_workdir;
1211 for (i = 0; i < numlower; i++) {
1212 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1216 pr_err("overlayfs: failed to clone lowerpath\n");
1217 goto out_put_lower_mnt;
1220 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1221 * will fail instead of modifying lower fs.
1223 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1225 ufs->lower_mnt[ufs->numlower] = mnt;
1229 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1230 if (!ufs->upper_mnt)
1231 sb->s_flags |= MS_RDONLY;
1234 sb->s_d_op = &ovl_reval_dentry_operations;
1236 sb->s_d_op = &ovl_dentry_operations;
1238 ufs->creator_cred = prepare_creds();
1239 if (!ufs->creator_cred)
1240 goto out_put_lower_mnt;
1243 oe = ovl_alloc_entry(numlower);
1247 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR));
1251 mntput(upperpath.mnt);
1252 for (i = 0; i < numlower; i++)
1253 mntput(stack[i].mnt);
1254 path_put(&workpath);
1257 oe->__upperdentry = upperpath.dentry;
1258 for (i = 0; i < numlower; i++) {
1259 oe->lowerstack[i].dentry = stack[i].dentry;
1260 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1264 root_dentry->d_fsdata = oe;
1266 realinode = d_inode(ovl_dentry_real(root_dentry));
1267 ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry);
1268 ovl_copyattr(realinode, d_inode(root_dentry));
1270 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1271 sb->s_op = &ovl_super_operations;
1272 if (IS_ENABLED(CONFIG_FS_POSIX_ACL))
1273 sb->s_xattr = ovl_xattr_handlers;
1275 sb->s_xattr = ovl_xattr_noacl_handlers;
1276 sb->s_root = root_dentry;
1277 sb->s_fs_info = ufs;
1278 sb->s_flags |= MS_POSIXACL;
1285 put_cred(ufs->creator_cred);
1287 for (i = 0; i < ufs->numlower; i++)
1288 mntput(ufs->lower_mnt[i]);
1289 kfree(ufs->lower_mnt);
1292 mntput(ufs->upper_mnt);
1294 for (i = 0; i < numlower; i++)
1295 path_put(&stack[i]);
1300 path_put(&workpath);
1302 path_put(&upperpath);
1304 kfree(ufs->config.lowerdir);
1305 kfree(ufs->config.upperdir);
1306 kfree(ufs->config.workdir);
1312 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1313 const char *dev_name, void *raw_data)
1315 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1318 static struct file_system_type ovl_fs_type = {
1319 .owner = THIS_MODULE,
1322 .kill_sb = kill_anon_super,
1324 MODULE_ALIAS_FS("overlay");
1326 static int __init ovl_init(void)
1328 return register_filesystem(&ovl_fs_type);
1331 static void __exit ovl_exit(void)
1333 unregister_filesystem(&ovl_fs_type);
1336 module_init(ovl_init);
1337 module_exit(ovl_exit);