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)
277 if (!d_is_dir(dentry))
280 res = vfs_getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
281 if (res == 1 && val == 'y')
287 static void ovl_dentry_release(struct dentry *dentry)
289 struct ovl_entry *oe = dentry->d_fsdata;
294 dput(oe->__upperdentry);
295 for (i = 0; i < oe->numlower; i++)
296 dput(oe->lowerstack[i].dentry);
301 static struct dentry *ovl_d_real(struct dentry *dentry,
302 const struct inode *inode,
303 unsigned int open_flags)
307 if (d_is_dir(dentry)) {
308 if (!inode || inode == d_inode(dentry))
313 if (d_is_negative(dentry))
317 int err = ovl_open_maybe_copy_up(dentry, open_flags);
323 real = ovl_dentry_upper(dentry);
324 if (real && (!inode || inode == d_inode(real)))
327 real = ovl_dentry_lower(dentry);
331 /* Handle recursion */
332 real = d_real(real, inode, open_flags);
334 if (!inode || inode == d_inode(real))
337 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
338 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
342 static int ovl_dentry_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_REVALIDATE) {
352 ret = d->d_op->d_revalidate(d, flags);
356 if (!(flags & LOOKUP_RCU))
365 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
367 struct ovl_entry *oe = dentry->d_fsdata;
371 for (i = 0; i < oe->numlower; i++) {
372 struct dentry *d = oe->lowerstack[i].dentry;
374 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
375 ret = d->d_op->d_weak_revalidate(d, flags);
383 static const struct dentry_operations ovl_dentry_operations = {
384 .d_release = ovl_dentry_release,
385 .d_real = ovl_d_real,
388 static const struct dentry_operations ovl_reval_dentry_operations = {
389 .d_release = ovl_dentry_release,
390 .d_real = ovl_d_real,
391 .d_revalidate = ovl_dentry_revalidate,
392 .d_weak_revalidate = ovl_dentry_weak_revalidate,
395 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
397 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
398 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
401 oe->numlower = numlower;
406 static bool ovl_dentry_remote(struct dentry *dentry)
408 return dentry->d_flags &
409 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
413 static bool ovl_dentry_weird(struct dentry *dentry)
415 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
416 DCACHE_MANAGE_TRANSIT |
421 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
422 const struct qstr *name)
424 struct dentry *dentry;
426 dentry = lookup_one_len_unlocked(name->name, dir, name->len);
428 if (IS_ERR(dentry)) {
429 if (PTR_ERR(dentry) == -ENOENT)
431 } else if (!dentry->d_inode) {
434 } else if (ovl_dentry_weird(dentry)) {
436 /* Don't support traversing automounts and other weirdness */
437 dentry = ERR_PTR(-EREMOTE);
443 * Returns next layer in stack starting from top.
444 * Returns -1 if this is the last layer.
446 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
448 struct ovl_entry *oe = dentry->d_fsdata;
452 ovl_path_upper(dentry, path);
454 return oe->numlower ? 1 : -1;
457 BUG_ON(idx > oe->numlower);
458 *path = oe->lowerstack[idx - 1];
460 return (idx < oe->numlower) ? idx + 1 : -1;
463 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
466 struct ovl_entry *oe;
467 const struct cred *old_cred;
468 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
469 struct path *stack = NULL;
470 struct dentry *upperdir, *upperdentry = NULL;
471 unsigned int ctr = 0;
472 struct inode *inode = NULL;
473 bool upperopaque = false;
474 struct dentry *this, *prev = NULL;
478 old_cred = ovl_override_creds(dentry->d_sb);
479 upperdir = ovl_upperdentry_dereference(poe);
481 this = ovl_lookup_real(upperdir, &dentry->d_name);
487 if (unlikely(ovl_dentry_remote(this))) {
492 if (ovl_is_whiteout(this)) {
496 } else if (poe->numlower && ovl_is_opaquedir(this)) {
500 upperdentry = prev = this;
503 if (!upperopaque && poe->numlower) {
505 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
510 for (i = 0; !upperopaque && i < poe->numlower; i++) {
512 struct path lowerpath = poe->lowerstack[i];
514 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
518 * If it's positive, then treat ENAMETOOLONG as ENOENT.
520 if (err == -ENAMETOOLONG && (upperdentry || ctr))
526 if (ovl_is_whiteout(this)) {
531 * Only makes sense to check opaque dir if this is not the
534 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
537 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
538 !S_ISDIR(this->d_inode->i_mode))) {
540 * FIXME: check for upper-opaqueness maybe better done
543 if (prev == upperdentry)
549 * If this is a non-directory then stop here.
551 if (!S_ISDIR(this->d_inode->i_mode))
554 stack[ctr].dentry = this;
555 stack[ctr].mnt = lowerpath.mnt;
562 oe = ovl_alloc_entry(ctr);
567 if (upperdentry || ctr) {
568 struct dentry *realdentry;
569 struct inode *realinode;
571 realdentry = upperdentry ? upperdentry : stack[0].dentry;
572 realinode = d_inode(realdentry);
575 if (upperdentry && !d_is_dir(upperdentry)) {
576 inode = ovl_get_inode(dentry->d_sb, realinode);
578 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode);
580 ovl_inode_init(inode, realinode, !!upperdentry);
584 ovl_copyattr(realdentry->d_inode, inode);
587 revert_creds(old_cred);
588 oe->opaque = upperopaque;
589 oe->__upperdentry = upperdentry;
590 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
592 dentry->d_fsdata = oe;
593 d_add(dentry, inode);
600 for (i = 0; i < ctr; i++)
601 dput(stack[i].dentry);
606 revert_creds(old_cred);
610 struct file *ovl_path_open(struct path *path, int flags)
612 return dentry_open(path, flags | O_NOATIME, current_cred());
615 static void ovl_put_super(struct super_block *sb)
617 struct ovl_fs *ufs = sb->s_fs_info;
621 mntput(ufs->upper_mnt);
622 for (i = 0; i < ufs->numlower; i++)
623 mntput(ufs->lower_mnt[i]);
624 kfree(ufs->lower_mnt);
626 kfree(ufs->config.lowerdir);
627 kfree(ufs->config.upperdir);
628 kfree(ufs->config.workdir);
629 put_cred(ufs->creator_cred);
635 * @sb: The overlayfs super block
636 * @buf: The struct kstatfs to fill in with stats
638 * Get the filesystem statistics. As writes always target the upper layer
639 * filesystem pass the statfs to the upper filesystem (if it exists)
641 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
643 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
644 struct dentry *root_dentry = dentry->d_sb->s_root;
648 ovl_path_real(root_dentry, &path);
650 err = vfs_statfs(&path, buf);
652 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
653 buf->f_type = OVERLAYFS_SUPER_MAGIC;
662 * Prints the mount options for a given superblock.
663 * Returns zero; does not fail.
665 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
667 struct super_block *sb = dentry->d_sb;
668 struct ovl_fs *ufs = sb->s_fs_info;
670 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
671 if (ufs->config.upperdir) {
672 seq_show_option(m, "upperdir", ufs->config.upperdir);
673 seq_show_option(m, "workdir", ufs->config.workdir);
675 if (ufs->config.default_permissions)
676 seq_puts(m, ",default_permissions");
680 static int ovl_remount(struct super_block *sb, int *flags, char *data)
682 struct ovl_fs *ufs = sb->s_fs_info;
684 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
690 static const struct super_operations ovl_super_operations = {
691 .put_super = ovl_put_super,
692 .statfs = ovl_statfs,
693 .show_options = ovl_show_options,
694 .remount_fs = ovl_remount,
695 .drop_inode = generic_delete_inode,
702 OPT_DEFAULT_PERMISSIONS,
706 static const match_table_t ovl_tokens = {
707 {OPT_LOWERDIR, "lowerdir=%s"},
708 {OPT_UPPERDIR, "upperdir=%s"},
709 {OPT_WORKDIR, "workdir=%s"},
710 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
714 static char *ovl_next_opt(char **s)
722 for (p = sbegin; *p; p++) {
727 } else if (*p == ',') {
737 static int ovl_parse_opt(char *opt, struct ovl_config *config)
741 while ((p = ovl_next_opt(&opt)) != NULL) {
743 substring_t args[MAX_OPT_ARGS];
748 token = match_token(p, ovl_tokens, args);
751 kfree(config->upperdir);
752 config->upperdir = match_strdup(&args[0]);
753 if (!config->upperdir)
758 kfree(config->lowerdir);
759 config->lowerdir = match_strdup(&args[0]);
760 if (!config->lowerdir)
765 kfree(config->workdir);
766 config->workdir = match_strdup(&args[0]);
767 if (!config->workdir)
771 case OPT_DEFAULT_PERMISSIONS:
772 config->default_permissions = true;
776 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
781 /* Workdir is useless in non-upper mount */
782 if (!config->upperdir && config->workdir) {
783 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
785 kfree(config->workdir);
786 config->workdir = NULL;
792 #define OVL_WORKDIR_NAME "work"
794 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
795 struct dentry *dentry)
797 struct inode *dir = dentry->d_inode;
800 bool retried = false;
802 err = mnt_want_write(mnt);
806 inode_lock_nested(dir, I_MUTEX_PARENT);
808 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
809 strlen(OVL_WORKDIR_NAME));
812 struct kstat stat = {
815 struct iattr attr = {
816 .ia_valid = ATTR_MODE,
817 .ia_mode = stat.mode,
826 ovl_workdir_cleanup(dir, mnt, work, 0);
831 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
836 * Try to remove POSIX ACL xattrs from workdir. We are good if:
838 * a) success (there was a POSIX ACL xattr and was removed)
839 * b) -ENODATA (there was no POSIX ACL xattr)
840 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
842 * There are various other error values that could effectively
843 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
844 * if the xattr name is too long), but the set of filesystems
845 * allowed as upper are limited to "normal" ones, where checking
846 * for the above two errors is sufficient.
848 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
849 if (err && err != -ENODATA && err != -EOPNOTSUPP)
852 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
853 if (err && err != -ENODATA && err != -EOPNOTSUPP)
856 /* Clear any inherited mode bits */
857 inode_lock(work->d_inode);
858 err = notify_change(work, &attr, NULL);
859 inode_unlock(work->d_inode);
875 static void ovl_unescape(char *s)
888 static int ovl_mount_dir_noesc(const char *name, struct path *path)
893 pr_err("overlayfs: empty lowerdir\n");
896 err = kern_path(name, LOOKUP_FOLLOW, path);
898 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
902 if (ovl_dentry_weird(path->dentry)) {
903 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
906 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
907 pr_err("overlayfs: '%s' not a directory\n", name);
918 static int ovl_mount_dir(const char *name, struct path *path)
921 char *tmp = kstrdup(name, GFP_KERNEL);
925 err = ovl_mount_dir_noesc(tmp, path);
928 if (ovl_dentry_remote(path->dentry)) {
929 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
939 static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
940 int *stack_depth, bool *remote)
943 struct kstatfs statfs;
945 err = ovl_mount_dir_noesc(name, path);
949 err = vfs_statfs(path, &statfs);
951 pr_err("overlayfs: statfs failed on '%s'\n", name);
954 *namelen = max(*namelen, statfs.f_namelen);
955 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
957 if (ovl_dentry_remote(path->dentry))
968 /* Workdir should not be subdir of upperdir and vice versa */
969 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
973 if (workdir != upperdir) {
974 ok = (lock_rename(workdir, upperdir) == NULL);
975 unlock_rename(workdir, upperdir);
980 static unsigned int ovl_split_lowerdirs(char *str)
982 unsigned int ctr = 1;
985 for (s = d = str;; s++, d++) {
988 } else if (*s == ':') {
1000 static int __maybe_unused
1001 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
1002 struct dentry *dentry, struct inode *inode,
1003 const char *name, void *buffer, size_t size)
1005 return ovl_xattr_get(dentry, handler->name, buffer, size);
1008 static int __maybe_unused
1009 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
1010 struct dentry *dentry, struct inode *inode,
1011 const char *name, const void *value,
1012 size_t size, int flags)
1014 struct dentry *workdir = ovl_workdir(dentry);
1015 struct inode *realinode = ovl_inode_real(inode, NULL);
1016 struct posix_acl *acl = NULL;
1019 /* Check that everything is OK before copy-up */
1021 acl = posix_acl_from_xattr(&init_user_ns, value, size);
1023 return PTR_ERR(acl);
1026 if (!IS_POSIXACL(d_inode(workdir)))
1027 goto out_acl_release;
1028 if (!realinode->i_op->set_acl)
1029 goto out_acl_release;
1030 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
1031 err = acl ? -EACCES : 0;
1032 goto out_acl_release;
1035 if (!inode_owner_or_capable(inode))
1036 goto out_acl_release;
1038 posix_acl_release(acl);
1041 * Check if sgid bit needs to be cleared (actual setacl operation will
1042 * be done with mounter's capabilities and so that won't do it for us).
1044 if (unlikely(inode->i_mode & S_ISGID) &&
1045 handler->flags == ACL_TYPE_ACCESS &&
1046 !in_group_p(inode->i_gid) &&
1047 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
1048 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
1050 err = ovl_setattr(dentry, &iattr);
1055 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
1057 ovl_copyattr(ovl_inode_real(inode, NULL), inode);
1062 posix_acl_release(acl);
1066 static int ovl_own_xattr_get(const struct xattr_handler *handler,
1067 struct dentry *dentry, struct inode *inode,
1068 const char *name, void *buffer, size_t size)
1073 static int ovl_own_xattr_set(const struct xattr_handler *handler,
1074 struct dentry *dentry, struct inode *inode,
1075 const char *name, const void *value,
1076 size_t size, int flags)
1081 static int ovl_other_xattr_get(const struct xattr_handler *handler,
1082 struct dentry *dentry, struct inode *inode,
1083 const char *name, void *buffer, size_t size)
1085 return ovl_xattr_get(dentry, name, buffer, size);
1088 static int ovl_other_xattr_set(const struct xattr_handler *handler,
1089 struct dentry *dentry, struct inode *inode,
1090 const char *name, const void *value,
1091 size_t size, int flags)
1093 return ovl_xattr_set(dentry, name, value, size, flags);
1096 static const struct xattr_handler __maybe_unused
1097 ovl_posix_acl_access_xattr_handler = {
1098 .name = XATTR_NAME_POSIX_ACL_ACCESS,
1099 .flags = ACL_TYPE_ACCESS,
1100 .get = ovl_posix_acl_xattr_get,
1101 .set = ovl_posix_acl_xattr_set,
1104 static const struct xattr_handler __maybe_unused
1105 ovl_posix_acl_default_xattr_handler = {
1106 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1107 .flags = ACL_TYPE_DEFAULT,
1108 .get = ovl_posix_acl_xattr_get,
1109 .set = ovl_posix_acl_xattr_set,
1112 static const struct xattr_handler ovl_own_xattr_handler = {
1113 .prefix = OVL_XATTR_PREFIX,
1114 .get = ovl_own_xattr_get,
1115 .set = ovl_own_xattr_set,
1118 static const struct xattr_handler ovl_other_xattr_handler = {
1119 .prefix = "", /* catch all */
1120 .get = ovl_other_xattr_get,
1121 .set = ovl_other_xattr_set,
1124 static const struct xattr_handler *ovl_xattr_handlers[] = {
1125 #ifdef CONFIG_FS_POSIX_ACL
1126 &ovl_posix_acl_access_xattr_handler,
1127 &ovl_posix_acl_default_xattr_handler,
1129 &ovl_own_xattr_handler,
1130 &ovl_other_xattr_handler,
1134 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1136 struct path upperpath = { NULL, NULL };
1137 struct path workpath = { NULL, NULL };
1138 struct dentry *root_dentry;
1139 struct inode *realinode;
1140 struct ovl_entry *oe;
1142 struct path *stack = NULL;
1145 unsigned int numlower;
1146 unsigned int stacklen = 0;
1148 bool remote = false;
1153 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1157 err = ovl_parse_opt((char *) data, &ufs->config);
1159 goto out_free_config;
1162 if (!ufs->config.lowerdir) {
1164 pr_err("overlayfs: missing 'lowerdir'\n");
1165 goto out_free_config;
1168 sb->s_stack_depth = 0;
1169 sb->s_maxbytes = MAX_LFS_FILESIZE;
1170 if (ufs->config.upperdir) {
1171 if (!ufs->config.workdir) {
1172 pr_err("overlayfs: missing 'workdir'\n");
1173 goto out_free_config;
1176 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
1178 goto out_free_config;
1180 /* Upper fs should not be r/o */
1181 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
1182 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1184 goto out_put_upperpath;
1187 err = ovl_mount_dir(ufs->config.workdir, &workpath);
1189 goto out_put_upperpath;
1192 if (upperpath.mnt != workpath.mnt) {
1193 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1194 goto out_put_workpath;
1196 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1197 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1198 goto out_put_workpath;
1200 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
1203 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1205 goto out_put_workpath;
1208 stacklen = ovl_split_lowerdirs(lowertmp);
1209 if (stacklen > OVL_MAX_STACK) {
1210 pr_err("overlayfs: too many lower directories, limit is %d\n",
1212 goto out_free_lowertmp;
1213 } else if (!ufs->config.upperdir && stacklen == 1) {
1214 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1215 goto out_free_lowertmp;
1218 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1220 goto out_free_lowertmp;
1223 for (numlower = 0; numlower < stacklen; numlower++) {
1224 err = ovl_lower_dir(lower, &stack[numlower],
1225 &ufs->lower_namelen, &sb->s_stack_depth,
1228 goto out_put_lowerpath;
1230 lower = strchr(lower, '\0') + 1;
1234 sb->s_stack_depth++;
1235 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1236 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1237 goto out_put_lowerpath;
1240 if (ufs->config.upperdir) {
1241 ufs->upper_mnt = clone_private_mount(&upperpath);
1242 err = PTR_ERR(ufs->upper_mnt);
1243 if (IS_ERR(ufs->upper_mnt)) {
1244 pr_err("overlayfs: failed to clone upperpath\n");
1245 goto out_put_lowerpath;
1247 /* Don't inherit atime flags */
1248 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1250 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
1252 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1253 err = PTR_ERR(ufs->workdir);
1254 if (IS_ERR(ufs->workdir)) {
1255 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1256 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1257 sb->s_flags |= MS_RDONLY;
1258 ufs->workdir = NULL;
1262 * Upper should support d_type, else whiteouts are visible.
1263 * Given workdir and upper are on same fs, we can do
1264 * iterate_dir() on workdir. This check requires successful
1265 * creation of workdir in previous step.
1268 err = ovl_check_d_type_supported(&workpath);
1270 goto out_put_workdir;
1273 * We allowed this configuration and don't want to
1274 * break users over kernel upgrade. So warn instead
1278 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1283 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1284 if (ufs->lower_mnt == NULL)
1285 goto out_put_workdir;
1286 for (i = 0; i < numlower; i++) {
1287 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1291 pr_err("overlayfs: failed to clone lowerpath\n");
1292 goto out_put_lower_mnt;
1295 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1296 * will fail instead of modifying lower fs.
1298 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1300 ufs->lower_mnt[ufs->numlower] = mnt;
1304 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1305 if (!ufs->upper_mnt)
1306 sb->s_flags |= MS_RDONLY;
1309 sb->s_d_op = &ovl_reval_dentry_operations;
1311 sb->s_d_op = &ovl_dentry_operations;
1314 ufs->creator_cred = cred = prepare_creds();
1316 goto out_put_lower_mnt;
1318 /* Never override disk quota limits or use reserved space */
1319 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1322 oe = ovl_alloc_entry(numlower);
1326 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1327 sb->s_op = &ovl_super_operations;
1328 sb->s_xattr = ovl_xattr_handlers;
1329 sb->s_fs_info = ufs;
1330 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1332 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR));
1336 mntput(upperpath.mnt);
1337 for (i = 0; i < numlower; i++)
1338 mntput(stack[i].mnt);
1339 path_put(&workpath);
1342 oe->__upperdentry = upperpath.dentry;
1343 for (i = 0; i < numlower; i++) {
1344 oe->lowerstack[i].dentry = stack[i].dentry;
1345 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1349 root_dentry->d_fsdata = oe;
1351 realinode = d_inode(ovl_dentry_real(root_dentry));
1352 ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry);
1353 ovl_copyattr(realinode, d_inode(root_dentry));
1355 sb->s_root = root_dentry;
1362 put_cred(ufs->creator_cred);
1364 for (i = 0; i < ufs->numlower; i++)
1365 mntput(ufs->lower_mnt[i]);
1366 kfree(ufs->lower_mnt);
1369 mntput(ufs->upper_mnt);
1371 for (i = 0; i < numlower; i++)
1372 path_put(&stack[i]);
1377 path_put(&workpath);
1379 path_put(&upperpath);
1381 kfree(ufs->config.lowerdir);
1382 kfree(ufs->config.upperdir);
1383 kfree(ufs->config.workdir);
1389 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1390 const char *dev_name, void *raw_data)
1392 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1395 static struct file_system_type ovl_fs_type = {
1396 .owner = THIS_MODULE,
1399 .kill_sb = kill_anon_super,
1401 MODULE_ALIAS_FS("overlay");
1403 static int __init ovl_init(void)
1405 return register_filesystem(&ovl_fs_type);
1408 static void __exit ovl_exit(void)
1410 unregister_filesystem(&ovl_fs_type);
1413 module_init(ovl_init);
1414 module_exit(ovl_exit);