1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
8 #include <linux/mount.h>
9 #include <linux/slab.h>
10 #include <linux/cred.h>
11 #include <linux/xattr.h>
12 #include <linux/exportfs.h>
13 #include <linux/uuid.h>
14 #include <linux/namei.h>
15 #include <linux/ratelimit.h>
16 #include "overlayfs.h"
18 int ovl_want_write(struct dentry *dentry)
20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
21 return mnt_want_write(ofs->upper_mnt);
24 void ovl_drop_write(struct dentry *dentry)
26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
27 mnt_drop_write(ofs->upper_mnt);
30 struct dentry *ovl_workdir(struct dentry *dentry)
32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
36 const struct cred *ovl_override_creds(struct super_block *sb)
38 struct ovl_fs *ofs = sb->s_fs_info;
40 return override_creds(ofs->creator_cred);
43 struct super_block *ovl_same_sb(struct super_block *sb)
45 struct ovl_fs *ofs = sb->s_fs_info;
48 return ofs->upper_mnt->mnt_sb;
49 else if (ofs->numlowerfs == 1 && !ofs->upper_mnt)
50 return ofs->lower_fs[0].sb;
56 * Check if underlying fs supports file handles and try to determine encoding
57 * type, in order to deduce maximum inode number used by fs.
59 * Return 0 if file handles are not supported.
60 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
61 * Return -1 if fs uses a non default encoding with unknown inode size.
63 int ovl_can_decode_fh(struct super_block *sb)
65 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
68 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
71 struct dentry *ovl_indexdir(struct super_block *sb)
73 struct ovl_fs *ofs = sb->s_fs_info;
78 /* Index all files on copy up. For now only enabled for NFS export */
79 bool ovl_index_all(struct super_block *sb)
81 struct ovl_fs *ofs = sb->s_fs_info;
83 return ofs->config.nfs_export && ofs->config.index;
86 /* Verify lower origin on lookup. For now only enabled for NFS export */
87 bool ovl_verify_lower(struct super_block *sb)
89 struct ovl_fs *ofs = sb->s_fs_info;
91 return ofs->config.nfs_export && ofs->config.index;
94 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
96 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
97 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
100 oe->numlower = numlower;
105 bool ovl_dentry_remote(struct dentry *dentry)
107 return dentry->d_flags &
108 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
112 bool ovl_dentry_weird(struct dentry *dentry)
114 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
115 DCACHE_MANAGE_TRANSIT |
120 enum ovl_path_type ovl_path_type(struct dentry *dentry)
122 struct ovl_entry *oe = dentry->d_fsdata;
123 enum ovl_path_type type = 0;
125 if (ovl_dentry_upper(dentry)) {
126 type = __OVL_PATH_UPPER;
129 * Non-dir dentry can hold lower dentry of its copy up origin.
132 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
133 type |= __OVL_PATH_ORIGIN;
134 if (d_is_dir(dentry) ||
135 !ovl_has_upperdata(d_inode(dentry)))
136 type |= __OVL_PATH_MERGE;
139 if (oe->numlower > 1)
140 type |= __OVL_PATH_MERGE;
145 void ovl_path_upper(struct dentry *dentry, struct path *path)
147 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
149 path->mnt = ofs->upper_mnt;
150 path->dentry = ovl_dentry_upper(dentry);
153 void ovl_path_lower(struct dentry *dentry, struct path *path)
155 struct ovl_entry *oe = dentry->d_fsdata;
158 path->mnt = oe->lowerstack[0].layer->mnt;
159 path->dentry = oe->lowerstack[0].dentry;
161 *path = (struct path) { };
165 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
167 struct ovl_entry *oe = dentry->d_fsdata;
170 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
171 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
173 *path = (struct path) { };
177 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
179 enum ovl_path_type type = ovl_path_type(dentry);
181 if (!OVL_TYPE_UPPER(type))
182 ovl_path_lower(dentry, path);
184 ovl_path_upper(dentry, path);
189 struct dentry *ovl_dentry_upper(struct dentry *dentry)
191 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
194 struct dentry *ovl_dentry_lower(struct dentry *dentry)
196 struct ovl_entry *oe = dentry->d_fsdata;
198 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
201 struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
203 struct ovl_entry *oe = dentry->d_fsdata;
205 return oe->numlower ? oe->lowerstack[0].layer : NULL;
209 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
210 * dependig on what is stored in lowerstack[0]. At times we need to find
211 * lower dentry which has data (and not metacopy dentry). This helper
212 * returns the lower data dentry.
214 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
216 struct ovl_entry *oe = dentry->d_fsdata;
218 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
221 struct dentry *ovl_dentry_real(struct dentry *dentry)
223 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
226 struct dentry *ovl_i_dentry_upper(struct inode *inode)
228 return ovl_upperdentry_dereference(OVL_I(inode));
231 struct inode *ovl_inode_upper(struct inode *inode)
233 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
235 return upperdentry ? d_inode(upperdentry) : NULL;
238 struct inode *ovl_inode_lower(struct inode *inode)
240 return OVL_I(inode)->lower;
243 struct inode *ovl_inode_real(struct inode *inode)
245 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
248 /* Return inode which contains lower data. Do not return metacopy */
249 struct inode *ovl_inode_lowerdata(struct inode *inode)
251 if (WARN_ON(!S_ISREG(inode->i_mode)))
254 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
257 /* Return real inode which contains data. Does not return metacopy inode */
258 struct inode *ovl_inode_realdata(struct inode *inode)
260 struct inode *upperinode;
262 upperinode = ovl_inode_upper(inode);
263 if (upperinode && ovl_has_upperdata(inode))
266 return ovl_inode_lowerdata(inode);
269 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
271 return OVL_I(inode)->cache;
274 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
276 OVL_I(inode)->cache = cache;
279 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
281 set_bit(flag, &OVL_E(dentry)->flags);
284 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
286 clear_bit(flag, &OVL_E(dentry)->flags);
289 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
291 return test_bit(flag, &OVL_E(dentry)->flags);
294 bool ovl_dentry_is_opaque(struct dentry *dentry)
296 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
299 bool ovl_dentry_is_whiteout(struct dentry *dentry)
301 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
304 void ovl_dentry_set_opaque(struct dentry *dentry)
306 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
310 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
311 * to return positive, while there's no actual upper alias for the inode.
312 * Copy up code needs to know about the existence of the upper alias, so it
313 * can't use ovl_dentry_upper().
315 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
317 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
320 void ovl_dentry_set_upper_alias(struct dentry *dentry)
322 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
325 static bool ovl_should_check_upperdata(struct inode *inode)
327 if (!S_ISREG(inode->i_mode))
330 if (!ovl_inode_lower(inode))
336 bool ovl_has_upperdata(struct inode *inode)
338 if (!ovl_should_check_upperdata(inode))
341 if (!ovl_test_flag(OVL_UPPERDATA, inode))
344 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
345 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
346 * if setting of OVL_UPPERDATA is visible, then effects of writes
347 * before that are visible too.
353 void ovl_set_upperdata(struct inode *inode)
356 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
357 * if OVL_UPPERDATA flag is visible, then effects of write operations
358 * before it are visible as well.
361 ovl_set_flag(OVL_UPPERDATA, inode);
364 /* Caller should hold ovl_inode->lock */
365 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
367 if (!ovl_open_flags_need_copy_up(flags))
370 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
373 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
375 if (!ovl_open_flags_need_copy_up(flags))
378 return !ovl_has_upperdata(d_inode(dentry));
381 bool ovl_redirect_dir(struct super_block *sb)
383 struct ovl_fs *ofs = sb->s_fs_info;
385 return ofs->config.redirect_dir && !ofs->noxattr;
388 const char *ovl_dentry_get_redirect(struct dentry *dentry)
390 return OVL_I(d_inode(dentry))->redirect;
393 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
395 struct ovl_inode *oi = OVL_I(d_inode(dentry));
398 oi->redirect = redirect;
401 void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
402 struct dentry *lowerdentry, struct dentry *lowerdata)
404 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
407 OVL_I(inode)->__upperdentry = upperdentry;
409 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
411 OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
413 ovl_copyattr(realinode, inode);
414 ovl_copyflags(realinode, inode);
416 inode->i_ino = realinode->i_ino;
419 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
421 struct inode *upperinode = d_inode(upperdentry);
423 WARN_ON(OVL_I(inode)->__upperdentry);
426 * Make sure upperdentry is consistent before making it visible
429 OVL_I(inode)->__upperdentry = upperdentry;
430 if (inode_unhashed(inode)) {
432 inode->i_ino = upperinode->i_ino;
433 inode->i_private = upperinode;
434 __insert_inode_hash(inode, (unsigned long) upperinode);
438 static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
440 struct inode *inode = d_inode(dentry);
442 WARN_ON(!inode_is_locked(inode));
444 * Version is used by readdir code to keep cache consistent. For merge
445 * dirs all changes need to be noted. For non-merge dirs, cache only
446 * contains impure (ones which have been copied up and have origins)
447 * entries, so only need to note changes to impure entries.
449 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
450 OVL_I(inode)->version++;
453 void ovl_dir_modified(struct dentry *dentry, bool impurity)
455 /* Copy mtime/ctime */
456 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
458 ovl_dentry_version_inc(dentry, impurity);
461 u64 ovl_dentry_version_get(struct dentry *dentry)
463 struct inode *inode = d_inode(dentry);
465 WARN_ON(!inode_is_locked(inode));
466 return OVL_I(inode)->version;
469 bool ovl_is_whiteout(struct dentry *dentry)
471 struct inode *inode = dentry->d_inode;
473 return inode && IS_WHITEOUT(inode);
476 struct file *ovl_path_open(struct path *path, int flags)
478 return dentry_open(path, flags | O_NOATIME, current_cred());
481 /* Caller should hold ovl_inode->lock */
482 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
484 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
486 if (ovl_dentry_upper(dentry) &&
487 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
488 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
494 bool ovl_already_copied_up(struct dentry *dentry, int flags)
496 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
499 * Check if copy-up has happened as well as for upper alias (in
500 * case of hard links) is there.
502 * Both checks are lockless:
503 * - false negatives: will recheck under oi->lock
505 * + ovl_dentry_upper() uses memory barriers to ensure the
506 * upper dentry is up-to-date
507 * + ovl_dentry_has_upper_alias() relies on locking of
508 * upper parent i_rwsem to prevent reordering copy-up
511 if (ovl_dentry_upper(dentry) &&
512 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
513 !ovl_dentry_needs_data_copy_up(dentry, flags))
519 int ovl_copy_up_start(struct dentry *dentry, int flags)
521 struct inode *inode = d_inode(dentry);
524 err = ovl_inode_lock(inode);
525 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
526 err = 1; /* Already copied up */
527 ovl_inode_unlock(inode);
533 void ovl_copy_up_end(struct dentry *dentry)
535 ovl_inode_unlock(d_inode(dentry));
538 bool ovl_check_origin_xattr(struct dentry *dentry)
542 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
544 /* Zero size value means "copied up but origin unknown" */
551 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
556 if (!d_is_dir(dentry))
559 res = vfs_getxattr(dentry, name, &val, 1);
560 if (res == 1 && val == 'y')
566 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
567 const char *name, const void *value, size_t size,
571 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
576 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
578 if (err == -EOPNOTSUPP) {
579 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
587 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
591 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
595 * Do not fail when upper doesn't support xattrs.
596 * Upper inodes won't have origin nor redirect xattr anyway.
598 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
601 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
606 void ovl_set_flag(unsigned long flag, struct inode *inode)
608 set_bit(flag, &OVL_I(inode)->flags);
611 void ovl_clear_flag(unsigned long flag, struct inode *inode)
613 clear_bit(flag, &OVL_I(inode)->flags);
616 bool ovl_test_flag(unsigned long flag, struct inode *inode)
618 return test_bit(flag, &OVL_I(inode)->flags);
622 * Caller must hold a reference to inode to prevent it from being freed while
623 * it is marked inuse.
625 bool ovl_inuse_trylock(struct dentry *dentry)
627 struct inode *inode = d_inode(dentry);
630 spin_lock(&inode->i_lock);
631 if (!(inode->i_state & I_OVL_INUSE)) {
632 inode->i_state |= I_OVL_INUSE;
635 spin_unlock(&inode->i_lock);
640 void ovl_inuse_unlock(struct dentry *dentry)
643 struct inode *inode = d_inode(dentry);
645 spin_lock(&inode->i_lock);
646 WARN_ON(!(inode->i_state & I_OVL_INUSE));
647 inode->i_state &= ~I_OVL_INUSE;
648 spin_unlock(&inode->i_lock);
652 bool ovl_is_inuse(struct dentry *dentry)
654 struct inode *inode = d_inode(dentry);
657 spin_lock(&inode->i_lock);
658 inuse = (inode->i_state & I_OVL_INUSE);
659 spin_unlock(&inode->i_lock);
665 * Does this overlay dentry need to be indexed on copy up?
667 bool ovl_need_index(struct dentry *dentry)
669 struct dentry *lower = ovl_dentry_lower(dentry);
671 if (!lower || !ovl_indexdir(dentry->d_sb))
674 /* Index all files for NFS export and consistency verification */
675 if (ovl_index_all(dentry->d_sb))
678 /* Index only lower hardlinks on copy up */
679 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
685 /* Caller must hold OVL_I(inode)->lock */
686 static void ovl_cleanup_index(struct dentry *dentry)
688 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
689 struct inode *dir = indexdir->d_inode;
690 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
691 struct dentry *upperdentry = ovl_dentry_upper(dentry);
692 struct dentry *index = NULL;
694 struct qstr name = { };
697 err = ovl_get_index_name(lowerdentry, &name);
701 inode = d_inode(upperdentry);
702 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
703 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
704 upperdentry, inode->i_ino, inode->i_nlink);
706 * We either have a bug with persistent union nlink or a lower
707 * hardlink was added while overlay is mounted. Adding a lower
708 * hardlink and then unlinking all overlay hardlinks would drop
709 * overlay nlink to zero before all upper inodes are unlinked.
710 * As a safety measure, when that situation is detected, set
711 * the overlay nlink to the index inode nlink minus one for the
712 * index entry itself.
714 set_nlink(d_inode(dentry), inode->i_nlink - 1);
715 ovl_set_nlink_upper(dentry);
719 inode_lock_nested(dir, I_MUTEX_PARENT);
720 index = lookup_one_len(name.name, indexdir, name.len);
721 err = PTR_ERR(index);
724 } else if (ovl_index_all(dentry->d_sb)) {
725 /* Whiteout orphan index to block future open by handle */
726 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
728 /* Cleanup orphan index entries */
729 err = ovl_cleanup(dir, index);
742 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
747 * Operations that change overlay inode and upper inode nlink need to be
748 * synchronized with copy up for persistent nlink accounting.
750 int ovl_nlink_start(struct dentry *dentry)
752 struct inode *inode = d_inode(dentry);
753 const struct cred *old_cred;
760 * With inodes index is enabled, we store the union overlay nlink
761 * in an xattr on the index inode. When whiting out an indexed lower,
762 * we need to decrement the overlay persistent nlink, but before the
763 * first copy up, we have no upper index inode to store the xattr.
765 * As a workaround, before whiteout/rename over an indexed lower,
766 * copy up to create the upper index. Creating the upper index will
767 * initialize the overlay nlink, so it could be dropped if unlink
768 * or rename succeeds.
770 * TODO: implement metadata only index copy up when called with
771 * ovl_copy_up_flags(dentry, O_PATH).
773 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
774 err = ovl_copy_up(dentry);
779 err = ovl_inode_lock(inode);
783 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
786 old_cred = ovl_override_creds(dentry->d_sb);
788 * The overlay inode nlink should be incremented/decremented IFF the
789 * upper operation succeeds, along with nlink change of upper inode.
790 * Therefore, before link/unlink/rename, we store the union nlink
791 * value relative to the upper inode nlink in an upper inode xattr.
793 err = ovl_set_nlink_upper(dentry);
794 revert_creds(old_cred);
798 ovl_inode_unlock(inode);
803 void ovl_nlink_end(struct dentry *dentry)
805 struct inode *inode = d_inode(dentry);
807 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
808 const struct cred *old_cred;
810 old_cred = ovl_override_creds(dentry->d_sb);
811 ovl_cleanup_index(dentry);
812 revert_creds(old_cred);
815 ovl_inode_unlock(inode);
818 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
820 /* Workdir should not be the same as upperdir */
821 if (workdir == upperdir)
824 /* Workdir should not be subdir of upperdir and vice versa */
825 if (lock_rename(workdir, upperdir) != NULL)
831 unlock_rename(workdir, upperdir);
833 pr_err("overlayfs: failed to lock workdir+upperdir\n");
837 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
838 int ovl_check_metacopy_xattr(struct dentry *dentry)
842 /* Only regular files can have metacopy xattr */
843 if (!S_ISREG(d_inode(dentry)->i_mode))
846 res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
848 if (res == -ENODATA || res == -EOPNOTSUPP)
855 pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res);
859 bool ovl_is_metacopy_dentry(struct dentry *dentry)
861 struct ovl_entry *oe = dentry->d_fsdata;
863 if (!d_is_reg(dentry))
866 if (ovl_dentry_upper(dentry)) {
867 if (!ovl_has_upperdata(d_inode(dentry)))
872 return (oe->numlower > 1);
875 ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
881 res = vfs_getxattr(dentry, name, NULL, 0);
883 if (res == -ENODATA || res == -EOPNOTSUPP)
889 buf = kzalloc(res + padding, GFP_KERNEL);
893 res = vfs_getxattr(dentry, name, buf, res);
902 pr_warn_ratelimited("overlayfs: failed to get xattr %s: err=%zi)\n",
908 char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
911 char *s, *next, *buf = NULL;
913 res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
922 for (s = buf; *s++ == '/'; s = next) {
923 next = strchrnul(s, '/');
928 if (strchr(buf, '/') != NULL)
934 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);