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/fileattr.h>
14 #include <linux/uuid.h>
15 #include <linux/namei.h>
16 #include <linux/ratelimit.h>
17 #include "overlayfs.h"
19 int ovl_want_write(struct dentry *dentry)
21 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
22 return mnt_want_write(ovl_upper_mnt(ofs));
25 void ovl_drop_write(struct dentry *dentry)
27 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
28 mnt_drop_write(ovl_upper_mnt(ofs));
31 struct dentry *ovl_workdir(struct dentry *dentry)
33 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
37 const struct cred *ovl_override_creds(struct super_block *sb)
39 struct ovl_fs *ofs = sb->s_fs_info;
41 return override_creds(ofs->creator_cred);
45 * Check if underlying fs supports file handles and try to determine encoding
46 * type, in order to deduce maximum inode number used by fs.
48 * Return 0 if file handles are not supported.
49 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
50 * Return -1 if fs uses a non default encoding with unknown inode size.
52 int ovl_can_decode_fh(struct super_block *sb)
54 if (!capable(CAP_DAC_READ_SEARCH))
57 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
60 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
63 struct dentry *ovl_indexdir(struct super_block *sb)
65 struct ovl_fs *ofs = sb->s_fs_info;
70 /* Index all files on copy up. For now only enabled for NFS export */
71 bool ovl_index_all(struct super_block *sb)
73 struct ovl_fs *ofs = sb->s_fs_info;
75 return ofs->config.nfs_export && ofs->config.index;
78 /* Verify lower origin on lookup. For now only enabled for NFS export */
79 bool ovl_verify_lower(struct super_block *sb)
81 struct ovl_fs *ofs = sb->s_fs_info;
83 return ofs->config.nfs_export && ofs->config.index;
86 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
88 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
89 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
92 oe->numlower = numlower;
97 #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
99 bool ovl_dentry_remote(struct dentry *dentry)
101 return dentry->d_flags & OVL_D_REVALIDATE;
104 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
106 if (!ovl_dentry_remote(realdentry))
109 spin_lock(&dentry->d_lock);
110 dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
111 spin_unlock(&dentry->d_lock);
114 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry)
116 return ovl_dentry_init_flags(dentry, upperdentry, OVL_D_REVALIDATE);
119 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
122 struct ovl_entry *oe = OVL_E(dentry);
123 unsigned int i, flags = 0;
126 flags |= upperdentry->d_flags;
127 for (i = 0; i < oe->numlower; i++)
128 flags |= oe->lowerstack[i].dentry->d_flags;
130 spin_lock(&dentry->d_lock);
131 dentry->d_flags &= ~mask;
132 dentry->d_flags |= flags & mask;
133 spin_unlock(&dentry->d_lock);
136 bool ovl_dentry_weird(struct dentry *dentry)
138 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
139 DCACHE_MANAGE_TRANSIT |
144 enum ovl_path_type ovl_path_type(struct dentry *dentry)
146 struct ovl_entry *oe = dentry->d_fsdata;
147 enum ovl_path_type type = 0;
149 if (ovl_dentry_upper(dentry)) {
150 type = __OVL_PATH_UPPER;
153 * Non-dir dentry can hold lower dentry of its copy up origin.
156 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
157 type |= __OVL_PATH_ORIGIN;
158 if (d_is_dir(dentry) ||
159 !ovl_has_upperdata(d_inode(dentry)))
160 type |= __OVL_PATH_MERGE;
163 if (oe->numlower > 1)
164 type |= __OVL_PATH_MERGE;
169 void ovl_path_upper(struct dentry *dentry, struct path *path)
171 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
173 path->mnt = ovl_upper_mnt(ofs);
174 path->dentry = ovl_dentry_upper(dentry);
177 void ovl_path_lower(struct dentry *dentry, struct path *path)
179 struct ovl_entry *oe = dentry->d_fsdata;
182 path->mnt = oe->lowerstack[0].layer->mnt;
183 path->dentry = oe->lowerstack[0].dentry;
185 *path = (struct path) { };
189 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
191 struct ovl_entry *oe = dentry->d_fsdata;
194 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
195 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
197 *path = (struct path) { };
201 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
203 enum ovl_path_type type = ovl_path_type(dentry);
205 if (!OVL_TYPE_UPPER(type))
206 ovl_path_lower(dentry, path);
208 ovl_path_upper(dentry, path);
213 struct dentry *ovl_dentry_upper(struct dentry *dentry)
215 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
218 struct dentry *ovl_dentry_lower(struct dentry *dentry)
220 struct ovl_entry *oe = dentry->d_fsdata;
222 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
225 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
227 struct ovl_entry *oe = dentry->d_fsdata;
229 return oe->numlower ? oe->lowerstack[0].layer : NULL;
233 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
234 * depending on what is stored in lowerstack[0]. At times we need to find
235 * lower dentry which has data (and not metacopy dentry). This helper
236 * returns the lower data dentry.
238 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
240 struct ovl_entry *oe = dentry->d_fsdata;
242 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
245 struct dentry *ovl_dentry_real(struct dentry *dentry)
247 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
250 struct dentry *ovl_i_dentry_upper(struct inode *inode)
252 return ovl_upperdentry_dereference(OVL_I(inode));
255 void ovl_i_path_real(struct inode *inode, struct path *path)
257 path->dentry = ovl_i_dentry_upper(inode);
259 path->dentry = OVL_I(inode)->lowerpath.dentry;
260 path->mnt = OVL_I(inode)->lowerpath.layer->mnt;
262 path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
266 struct inode *ovl_inode_upper(struct inode *inode)
268 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
270 return upperdentry ? d_inode(upperdentry) : NULL;
273 struct inode *ovl_inode_lower(struct inode *inode)
275 struct dentry *lowerdentry = OVL_I(inode)->lowerpath.dentry;
277 return lowerdentry ? d_inode(lowerdentry) : NULL;
280 struct inode *ovl_inode_real(struct inode *inode)
282 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
285 /* Return inode which contains lower data. Do not return metacopy */
286 struct inode *ovl_inode_lowerdata(struct inode *inode)
288 if (WARN_ON(!S_ISREG(inode->i_mode)))
291 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
294 /* Return real inode which contains data. Does not return metacopy inode */
295 struct inode *ovl_inode_realdata(struct inode *inode)
297 struct inode *upperinode;
299 upperinode = ovl_inode_upper(inode);
300 if (upperinode && ovl_has_upperdata(inode))
303 return ovl_inode_lowerdata(inode);
306 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
308 return OVL_I(inode)->cache;
311 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
313 OVL_I(inode)->cache = cache;
316 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
318 set_bit(flag, &OVL_E(dentry)->flags);
321 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
323 clear_bit(flag, &OVL_E(dentry)->flags);
326 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
328 return test_bit(flag, &OVL_E(dentry)->flags);
331 bool ovl_dentry_is_opaque(struct dentry *dentry)
333 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
336 bool ovl_dentry_is_whiteout(struct dentry *dentry)
338 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
341 void ovl_dentry_set_opaque(struct dentry *dentry)
343 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
347 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
348 * to return positive, while there's no actual upper alias for the inode.
349 * Copy up code needs to know about the existence of the upper alias, so it
350 * can't use ovl_dentry_upper().
352 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
354 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
357 void ovl_dentry_set_upper_alias(struct dentry *dentry)
359 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
362 static bool ovl_should_check_upperdata(struct inode *inode)
364 if (!S_ISREG(inode->i_mode))
367 if (!ovl_inode_lower(inode))
373 bool ovl_has_upperdata(struct inode *inode)
375 if (!ovl_should_check_upperdata(inode))
378 if (!ovl_test_flag(OVL_UPPERDATA, inode))
381 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
382 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
383 * if setting of OVL_UPPERDATA is visible, then effects of writes
384 * before that are visible too.
390 void ovl_set_upperdata(struct inode *inode)
393 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
394 * if OVL_UPPERDATA flag is visible, then effects of write operations
395 * before it are visible as well.
398 ovl_set_flag(OVL_UPPERDATA, inode);
401 /* Caller should hold ovl_inode->lock */
402 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
404 if (!ovl_open_flags_need_copy_up(flags))
407 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
410 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
412 if (!ovl_open_flags_need_copy_up(flags))
415 return !ovl_has_upperdata(d_inode(dentry));
418 bool ovl_redirect_dir(struct super_block *sb)
420 struct ovl_fs *ofs = sb->s_fs_info;
422 return ofs->config.redirect_dir && !ofs->noxattr;
425 const char *ovl_dentry_get_redirect(struct dentry *dentry)
427 return OVL_I(d_inode(dentry))->redirect;
430 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
432 struct ovl_inode *oi = OVL_I(d_inode(dentry));
435 oi->redirect = redirect;
438 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
440 struct inode *upperinode = d_inode(upperdentry);
442 WARN_ON(OVL_I(inode)->__upperdentry);
445 * Make sure upperdentry is consistent before making it visible
448 OVL_I(inode)->__upperdentry = upperdentry;
449 if (inode_unhashed(inode)) {
450 inode->i_private = upperinode;
451 __insert_inode_hash(inode, (unsigned long) upperinode);
455 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
457 struct inode *inode = d_inode(dentry);
459 WARN_ON(!inode_is_locked(inode));
460 WARN_ON(!d_is_dir(dentry));
462 * Version is used by readdir code to keep cache consistent.
463 * For merge dirs (or dirs with origin) all changes need to be noted.
464 * For non-merge dirs, cache contains only impure entries (i.e. ones
465 * which have been copied up and have origins), so only need to note
466 * changes to impure entries.
468 if (!ovl_dir_is_real(dentry) || impurity)
469 OVL_I(inode)->version++;
472 void ovl_dir_modified(struct dentry *dentry, bool impurity)
474 /* Copy mtime/ctime */
475 ovl_copyattr(d_inode(dentry));
477 ovl_dir_version_inc(dentry, impurity);
480 u64 ovl_dentry_version_get(struct dentry *dentry)
482 struct inode *inode = d_inode(dentry);
484 WARN_ON(!inode_is_locked(inode));
485 return OVL_I(inode)->version;
488 bool ovl_is_whiteout(struct dentry *dentry)
490 struct inode *inode = dentry->d_inode;
492 return inode && IS_WHITEOUT(inode);
495 struct file *ovl_path_open(struct path *path, int flags)
497 struct inode *inode = d_inode(path->dentry);
500 if (flags & ~(O_ACCMODE | O_LARGEFILE))
503 switch (flags & O_ACCMODE) {
508 acc_mode = MAY_WRITE;
514 err = inode_permission(&init_user_ns, inode, acc_mode | MAY_OPEN);
518 /* O_NOATIME is an optimization, don't fail if not permitted */
519 if (inode_owner_or_capable(&init_user_ns, inode))
522 return dentry_open(path, flags, current_cred());
525 /* Caller should hold ovl_inode->lock */
526 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
528 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
530 if (ovl_dentry_upper(dentry) &&
531 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
532 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
538 bool ovl_already_copied_up(struct dentry *dentry, int flags)
540 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
543 * Check if copy-up has happened as well as for upper alias (in
544 * case of hard links) is there.
546 * Both checks are lockless:
547 * - false negatives: will recheck under oi->lock
549 * + ovl_dentry_upper() uses memory barriers to ensure the
550 * upper dentry is up-to-date
551 * + ovl_dentry_has_upper_alias() relies on locking of
552 * upper parent i_rwsem to prevent reordering copy-up
555 if (ovl_dentry_upper(dentry) &&
556 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
557 !ovl_dentry_needs_data_copy_up(dentry, flags))
563 int ovl_copy_up_start(struct dentry *dentry, int flags)
565 struct inode *inode = d_inode(dentry);
568 err = ovl_inode_lock_interruptible(inode);
569 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
570 err = 1; /* Already copied up */
571 ovl_inode_unlock(inode);
577 void ovl_copy_up_end(struct dentry *dentry)
579 ovl_inode_unlock(d_inode(dentry));
582 bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
586 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
588 /* Zero size value means "copied up but origin unknown" */
595 bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
601 if (!d_is_dir(dentry))
604 res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
605 if (res == 1 && val == 'y')
611 #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
612 #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
613 #define OVL_XATTR_ORIGIN_POSTFIX "origin"
614 #define OVL_XATTR_IMPURE_POSTFIX "impure"
615 #define OVL_XATTR_NLINK_POSTFIX "nlink"
616 #define OVL_XATTR_UPPER_POSTFIX "upper"
617 #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
618 #define OVL_XATTR_PROTATTR_POSTFIX "protattr"
620 #define OVL_XATTR_TAB_ENTRY(x) \
621 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
622 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
624 const char *const ovl_xattr_table[][2] = {
625 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
626 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
627 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
628 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
629 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
630 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
631 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
632 OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
635 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
636 enum ovl_xattr ox, const void *value, size_t size,
644 err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
646 if (err == -EOPNOTSUPP) {
647 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
655 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
657 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
660 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
664 * Do not fail when upper doesn't support xattrs.
665 * Upper inodes won't have origin nor redirect xattr anyway.
667 err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
669 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
675 #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
677 void ovl_check_protattr(struct inode *inode, struct dentry *upper)
679 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
680 u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
681 char buf[OVL_PROTATTR_MAX+1];
684 res = ovl_do_getxattr(ofs, upper, OVL_XATTR_PROTATTR, buf,
690 * Initialize inode flags from overlay.protattr xattr and upper inode
691 * flags. If upper inode has those fileattr flags set (i.e. from old
692 * kernel), we do not clear them on ovl_get_inode(), but we will clear
693 * them on next fileattr_set().
695 for (n = 0; n < res; n++) {
698 else if (buf[n] == 'i')
699 iflags |= S_IMMUTABLE;
704 if (!res || n < res) {
705 pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
708 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
712 int ovl_set_protattr(struct inode *inode, struct dentry *upper,
715 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
716 char buf[OVL_PROTATTR_MAX];
717 int len = 0, err = 0;
720 BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
722 if (fa->flags & FS_APPEND_FL) {
726 if (fa->flags & FS_IMMUTABLE_FL) {
728 iflags |= S_IMMUTABLE;
732 * Do not allow to set protection flags when upper doesn't support
733 * xattrs, because we do not set those fileattr flags on upper inode.
734 * Remove xattr if it exist and all protection flags are cleared.
737 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
739 } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
740 err = ovl_do_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
741 if (err == -EOPNOTSUPP || err == -ENODATA)
747 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
749 /* Mask out the fileattr flags that should not be set in upper inode */
750 fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
751 fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
757 * Caller must hold a reference to inode to prevent it from being freed while
758 * it is marked inuse.
760 bool ovl_inuse_trylock(struct dentry *dentry)
762 struct inode *inode = d_inode(dentry);
765 spin_lock(&inode->i_lock);
766 if (!(inode->i_state & I_OVL_INUSE)) {
767 inode->i_state |= I_OVL_INUSE;
770 spin_unlock(&inode->i_lock);
775 void ovl_inuse_unlock(struct dentry *dentry)
778 struct inode *inode = d_inode(dentry);
780 spin_lock(&inode->i_lock);
781 WARN_ON(!(inode->i_state & I_OVL_INUSE));
782 inode->i_state &= ~I_OVL_INUSE;
783 spin_unlock(&inode->i_lock);
787 bool ovl_is_inuse(struct dentry *dentry)
789 struct inode *inode = d_inode(dentry);
792 spin_lock(&inode->i_lock);
793 inuse = (inode->i_state & I_OVL_INUSE);
794 spin_unlock(&inode->i_lock);
800 * Does this overlay dentry need to be indexed on copy up?
802 bool ovl_need_index(struct dentry *dentry)
804 struct dentry *lower = ovl_dentry_lower(dentry);
806 if (!lower || !ovl_indexdir(dentry->d_sb))
809 /* Index all files for NFS export and consistency verification */
810 if (ovl_index_all(dentry->d_sb))
813 /* Index only lower hardlinks on copy up */
814 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
820 /* Caller must hold OVL_I(inode)->lock */
821 static void ovl_cleanup_index(struct dentry *dentry)
823 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
824 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
825 struct inode *dir = indexdir->d_inode;
826 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
827 struct dentry *upperdentry = ovl_dentry_upper(dentry);
828 struct dentry *index = NULL;
830 struct qstr name = { };
833 err = ovl_get_index_name(ofs, lowerdentry, &name);
837 inode = d_inode(upperdentry);
838 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
839 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
840 upperdentry, inode->i_ino, inode->i_nlink);
842 * We either have a bug with persistent union nlink or a lower
843 * hardlink was added while overlay is mounted. Adding a lower
844 * hardlink and then unlinking all overlay hardlinks would drop
845 * overlay nlink to zero before all upper inodes are unlinked.
846 * As a safety measure, when that situation is detected, set
847 * the overlay nlink to the index inode nlink minus one for the
848 * index entry itself.
850 set_nlink(d_inode(dentry), inode->i_nlink - 1);
851 ovl_set_nlink_upper(dentry);
855 inode_lock_nested(dir, I_MUTEX_PARENT);
856 index = lookup_one_len(name.name, indexdir, name.len);
857 err = PTR_ERR(index);
860 } else if (ovl_index_all(dentry->d_sb)) {
861 /* Whiteout orphan index to block future open by handle */
862 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
865 /* Cleanup orphan index entries */
866 err = ovl_cleanup(dir, index);
879 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
884 * Operations that change overlay inode and upper inode nlink need to be
885 * synchronized with copy up for persistent nlink accounting.
887 int ovl_nlink_start(struct dentry *dentry)
889 struct inode *inode = d_inode(dentry);
890 const struct cred *old_cred;
897 * With inodes index is enabled, we store the union overlay nlink
898 * in an xattr on the index inode. When whiting out an indexed lower,
899 * we need to decrement the overlay persistent nlink, but before the
900 * first copy up, we have no upper index inode to store the xattr.
902 * As a workaround, before whiteout/rename over an indexed lower,
903 * copy up to create the upper index. Creating the upper index will
904 * initialize the overlay nlink, so it could be dropped if unlink
905 * or rename succeeds.
907 * TODO: implement metadata only index copy up when called with
908 * ovl_copy_up_flags(dentry, O_PATH).
910 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
911 err = ovl_copy_up(dentry);
916 err = ovl_inode_lock_interruptible(inode);
920 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
923 old_cred = ovl_override_creds(dentry->d_sb);
925 * The overlay inode nlink should be incremented/decremented IFF the
926 * upper operation succeeds, along with nlink change of upper inode.
927 * Therefore, before link/unlink/rename, we store the union nlink
928 * value relative to the upper inode nlink in an upper inode xattr.
930 err = ovl_set_nlink_upper(dentry);
931 revert_creds(old_cred);
935 ovl_inode_unlock(inode);
940 void ovl_nlink_end(struct dentry *dentry)
942 struct inode *inode = d_inode(dentry);
944 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
945 const struct cred *old_cred;
947 old_cred = ovl_override_creds(dentry->d_sb);
948 ovl_cleanup_index(dentry);
949 revert_creds(old_cred);
952 ovl_inode_unlock(inode);
955 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
957 /* Workdir should not be the same as upperdir */
958 if (workdir == upperdir)
961 /* Workdir should not be subdir of upperdir and vice versa */
962 if (lock_rename(workdir, upperdir) != NULL)
968 unlock_rename(workdir, upperdir);
970 pr_err("failed to lock workdir+upperdir\n");
974 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
975 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
979 /* Only regular files can have metacopy xattr */
980 if (!S_ISREG(d_inode(dentry)->i_mode))
983 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
985 if (res == -ENODATA || res == -EOPNOTSUPP)
988 * getxattr on user.* may fail with EACCES in case there's no
989 * read permission on the inode. Not much we can do, other than
990 * tell the caller that this is not a metacopy inode.
992 if (ofs->config.userxattr && res == -EACCES)
999 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
1003 bool ovl_is_metacopy_dentry(struct dentry *dentry)
1005 struct ovl_entry *oe = dentry->d_fsdata;
1007 if (!d_is_reg(dentry))
1010 if (ovl_dentry_upper(dentry)) {
1011 if (!ovl_has_upperdata(d_inode(dentry)))
1016 return (oe->numlower > 1);
1019 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
1023 char *s, *next, *buf = NULL;
1025 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
1026 if (res == -ENODATA || res == -EOPNOTSUPP)
1033 buf = kzalloc(res + padding + 1, GFP_KERNEL);
1035 return ERR_PTR(-ENOMEM);
1037 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
1043 if (buf[0] == '/') {
1044 for (s = buf; *s++ == '/'; s = next) {
1045 next = strchrnul(s, '/');
1050 if (strchr(buf, '/') != NULL)
1056 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
1060 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1063 return ERR_PTR(res);
1067 * ovl_sync_status() - Check fs sync status for volatile mounts
1069 * Returns 1 if this is not a volatile mount and a real sync is required.
1071 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1072 * have occurred on the upperdir since the mount.
1074 * Returns -errno if it is a volatile mount, and the error that occurred since
1075 * the last mount. If the error code changes, it'll return the latest error
1079 int ovl_sync_status(struct ovl_fs *ofs)
1081 struct vfsmount *mnt;
1083 if (ovl_should_sync(ofs))
1086 mnt = ovl_upper_mnt(ofs);
1090 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1094 * ovl_copyattr() - copy inode attributes from layer to ovl inode
1096 * When overlay copies inode information from an upper or lower layer to the
1097 * relevant overlay inode it will apply the idmapping of the upper or lower
1098 * layer when doing so ensuring that the ovl inode ownership will correctly
1099 * reflect the ownership of the idmapped upper or lower layer. For example, an
1100 * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1101 * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1102 * helpers are nops when the relevant layer isn't idmapped.
1104 void ovl_copyattr(struct inode *inode)
1106 struct path realpath;
1107 struct inode *realinode;
1108 struct user_namespace *real_mnt_userns;
1110 ovl_i_path_real(inode, &realpath);
1111 realinode = d_inode(realpath.dentry);
1112 real_mnt_userns = mnt_user_ns(realpath.mnt);
1114 inode->i_uid = i_uid_into_mnt(real_mnt_userns, realinode);
1115 inode->i_gid = i_gid_into_mnt(real_mnt_userns, realinode);
1116 inode->i_mode = realinode->i_mode;
1117 inode->i_atime = realinode->i_atime;
1118 inode->i_mtime = realinode->i_mtime;
1119 inode->i_ctime = realinode->i_ctime;
1120 i_size_write(inode, i_size_read(realinode));