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/file.h>
14 #include <linux/fileattr.h>
15 #include <linux/uuid.h>
16 #include <linux/namei.h>
17 #include <linux/ratelimit.h>
18 #include "overlayfs.h"
20 int ovl_want_write(struct dentry *dentry)
22 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
23 return mnt_want_write(ovl_upper_mnt(ofs));
26 void ovl_drop_write(struct dentry *dentry)
28 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
29 mnt_drop_write(ovl_upper_mnt(ofs));
32 struct dentry *ovl_workdir(struct dentry *dentry)
34 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
38 const struct cred *ovl_override_creds(struct super_block *sb)
40 struct ovl_fs *ofs = OVL_FS(sb);
42 return override_creds(ofs->creator_cred);
46 * Check if underlying fs supports file handles and try to determine encoding
47 * type, in order to deduce maximum inode number used by fs.
49 * Return 0 if file handles are not supported.
50 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
51 * Return -1 if fs uses a non default encoding with unknown inode size.
53 int ovl_can_decode_fh(struct super_block *sb)
55 if (!capable(CAP_DAC_READ_SEARCH))
58 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
61 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
64 struct dentry *ovl_indexdir(struct super_block *sb)
66 struct ovl_fs *ofs = OVL_FS(sb);
71 /* Index all files on copy up. For now only enabled for NFS export */
72 bool ovl_index_all(struct super_block *sb)
74 struct ovl_fs *ofs = OVL_FS(sb);
76 return ofs->config.nfs_export && ofs->config.index;
79 /* Verify lower origin on lookup. For now only enabled for NFS export */
80 bool ovl_verify_lower(struct super_block *sb)
82 struct ovl_fs *ofs = OVL_FS(sb);
84 return ofs->config.nfs_export && ofs->config.index;
87 struct ovl_path *ovl_stack_alloc(unsigned int n)
89 return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL);
92 void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
96 memcpy(dst, src, sizeof(struct ovl_path) * n);
97 for (i = 0; i < n; i++)
101 void ovl_stack_put(struct ovl_path *stack, unsigned int n)
105 for (i = 0; stack && i < n; i++)
106 dput(stack[i].dentry);
109 void ovl_stack_free(struct ovl_path *stack, unsigned int n)
111 ovl_stack_put(stack, n);
115 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
117 size_t size = offsetof(struct ovl_entry, __lowerstack[numlower]);
118 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
121 oe->__numlower = numlower;
126 void ovl_free_entry(struct ovl_entry *oe)
128 ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
132 #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
134 bool ovl_dentry_remote(struct dentry *dentry)
136 return dentry->d_flags & OVL_D_REVALIDATE;
139 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
141 if (!ovl_dentry_remote(realdentry))
144 spin_lock(&dentry->d_lock);
145 dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
146 spin_unlock(&dentry->d_lock);
149 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
150 struct ovl_entry *oe)
152 return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
155 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
156 struct ovl_entry *oe, unsigned int mask)
158 struct ovl_path *lowerstack = ovl_lowerstack(oe);
159 unsigned int i, flags = 0;
162 flags |= upperdentry->d_flags;
163 for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
164 flags |= lowerstack[i].dentry->d_flags;
166 spin_lock(&dentry->d_lock);
167 dentry->d_flags &= ~mask;
168 dentry->d_flags |= flags & mask;
169 spin_unlock(&dentry->d_lock);
172 bool ovl_dentry_weird(struct dentry *dentry)
174 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
175 DCACHE_MANAGE_TRANSIT |
180 enum ovl_path_type ovl_path_type(struct dentry *dentry)
182 struct ovl_entry *oe = OVL_E(dentry);
183 enum ovl_path_type type = 0;
185 if (ovl_dentry_upper(dentry)) {
186 type = __OVL_PATH_UPPER;
189 * Non-dir dentry can hold lower dentry of its copy up origin.
191 if (ovl_numlower(oe)) {
192 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
193 type |= __OVL_PATH_ORIGIN;
194 if (d_is_dir(dentry) ||
195 !ovl_has_upperdata(d_inode(dentry)))
196 type |= __OVL_PATH_MERGE;
199 if (ovl_numlower(oe) > 1)
200 type |= __OVL_PATH_MERGE;
205 void ovl_path_upper(struct dentry *dentry, struct path *path)
207 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
209 path->mnt = ovl_upper_mnt(ofs);
210 path->dentry = ovl_dentry_upper(dentry);
213 void ovl_path_lower(struct dentry *dentry, struct path *path)
215 struct ovl_entry *oe = OVL_E(dentry);
216 struct ovl_path *lowerpath = ovl_lowerstack(oe);
218 if (ovl_numlower(oe)) {
219 path->mnt = lowerpath->layer->mnt;
220 path->dentry = lowerpath->dentry;
222 *path = (struct path) { };
226 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
228 struct ovl_entry *oe = OVL_E(dentry);
229 struct ovl_path *lowerdata = ovl_lowerdata(oe);
230 struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
232 if (lowerdata_dentry) {
233 path->dentry = lowerdata_dentry;
235 * Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
236 * Make sure that if lowerdata->dentry is visible, then
237 * datapath->layer is visible as well.
240 path->mnt = READ_ONCE(lowerdata->layer)->mnt;
242 *path = (struct path) { };
246 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
248 enum ovl_path_type type = ovl_path_type(dentry);
250 if (!OVL_TYPE_UPPER(type))
251 ovl_path_lower(dentry, path);
253 ovl_path_upper(dentry, path);
258 enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
260 enum ovl_path_type type = ovl_path_type(dentry);
262 WARN_ON_ONCE(d_is_dir(dentry));
264 if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
265 ovl_path_lowerdata(dentry, path);
267 ovl_path_upper(dentry, path);
272 struct dentry *ovl_dentry_upper(struct dentry *dentry)
274 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
277 struct dentry *ovl_dentry_lower(struct dentry *dentry)
279 struct ovl_entry *oe = OVL_E(dentry);
281 return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
284 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
286 struct ovl_entry *oe = OVL_E(dentry);
288 return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
292 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
293 * depending on what is stored in lowerstack[0]. At times we need to find
294 * lower dentry which has data (and not metacopy dentry). This helper
295 * returns the lower data dentry.
297 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
299 return ovl_lowerdata_dentry(OVL_E(dentry));
302 int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
304 struct ovl_entry *oe = OVL_E(dentry);
305 struct ovl_path *lowerdata = ovl_lowerdata(oe);
306 struct dentry *datadentry = datapath->dentry;
308 if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
311 WRITE_ONCE(lowerdata->layer, datapath->layer);
313 * Pairs with smp_rmb() in ovl_path_lowerdata().
314 * Make sure that if lowerdata->dentry is visible, then
315 * lowerdata->layer is visible as well.
318 WRITE_ONCE(lowerdata->dentry, dget(datadentry));
320 ovl_dentry_update_reval(dentry, datadentry);
325 struct dentry *ovl_dentry_real(struct dentry *dentry)
327 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
330 struct dentry *ovl_i_dentry_upper(struct inode *inode)
332 return ovl_upperdentry_dereference(OVL_I(inode));
335 struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
337 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
339 path->dentry = ovl_i_dentry_upper(inode);
341 path->dentry = lowerpath->dentry;
342 path->mnt = lowerpath->layer->mnt;
344 path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
347 return path->dentry ? d_inode_rcu(path->dentry) : NULL;
350 struct inode *ovl_inode_upper(struct inode *inode)
352 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
354 return upperdentry ? d_inode(upperdentry) : NULL;
357 struct inode *ovl_inode_lower(struct inode *inode)
359 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
361 return lowerpath ? d_inode(lowerpath->dentry) : NULL;
364 struct inode *ovl_inode_real(struct inode *inode)
366 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
369 /* Return inode which contains lower data. Do not return metacopy */
370 struct inode *ovl_inode_lowerdata(struct inode *inode)
372 struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode));
374 if (WARN_ON(!S_ISREG(inode->i_mode)))
377 return lowerdata ? d_inode(lowerdata) : NULL;
380 /* Return real inode which contains data. Does not return metacopy inode */
381 struct inode *ovl_inode_realdata(struct inode *inode)
383 struct inode *upperinode;
385 upperinode = ovl_inode_upper(inode);
386 if (upperinode && ovl_has_upperdata(inode))
389 return ovl_inode_lowerdata(inode);
392 const char *ovl_lowerdata_redirect(struct inode *inode)
394 return inode && S_ISREG(inode->i_mode) ?
395 OVL_I(inode)->lowerdata_redirect : NULL;
398 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
400 return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL;
403 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
405 OVL_I(inode)->cache = cache;
408 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
410 set_bit(flag, OVL_E_FLAGS(dentry));
413 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
415 clear_bit(flag, OVL_E_FLAGS(dentry));
418 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
420 return test_bit(flag, OVL_E_FLAGS(dentry));
423 bool ovl_dentry_is_opaque(struct dentry *dentry)
425 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
428 bool ovl_dentry_is_whiteout(struct dentry *dentry)
430 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
433 void ovl_dentry_set_opaque(struct dentry *dentry)
435 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
439 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
440 * to return positive, while there's no actual upper alias for the inode.
441 * Copy up code needs to know about the existence of the upper alias, so it
442 * can't use ovl_dentry_upper().
444 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
446 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
449 void ovl_dentry_set_upper_alias(struct dentry *dentry)
451 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
454 static bool ovl_should_check_upperdata(struct inode *inode)
456 if (!S_ISREG(inode->i_mode))
459 if (!ovl_inode_lower(inode))
465 bool ovl_has_upperdata(struct inode *inode)
467 if (!ovl_should_check_upperdata(inode))
470 if (!ovl_test_flag(OVL_UPPERDATA, inode))
473 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
474 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
475 * if setting of OVL_UPPERDATA is visible, then effects of writes
476 * before that are visible too.
482 void ovl_set_upperdata(struct inode *inode)
485 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
486 * if OVL_UPPERDATA flag is visible, then effects of write operations
487 * before it are visible as well.
490 ovl_set_flag(OVL_UPPERDATA, inode);
493 /* Caller should hold ovl_inode->lock */
494 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
496 if (!ovl_open_flags_need_copy_up(flags))
499 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
502 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
504 if (!ovl_open_flags_need_copy_up(flags))
507 return !ovl_has_upperdata(d_inode(dentry));
510 const char *ovl_dentry_get_redirect(struct dentry *dentry)
512 return OVL_I(d_inode(dentry))->redirect;
515 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
517 struct ovl_inode *oi = OVL_I(d_inode(dentry));
520 oi->redirect = redirect;
523 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
525 struct inode *upperinode = d_inode(upperdentry);
527 WARN_ON(OVL_I(inode)->__upperdentry);
530 * Make sure upperdentry is consistent before making it visible
533 OVL_I(inode)->__upperdentry = upperdentry;
534 if (inode_unhashed(inode)) {
535 inode->i_private = upperinode;
536 __insert_inode_hash(inode, (unsigned long) upperinode);
540 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
542 struct inode *inode = d_inode(dentry);
544 WARN_ON(!inode_is_locked(inode));
545 WARN_ON(!d_is_dir(dentry));
547 * Version is used by readdir code to keep cache consistent.
548 * For merge dirs (or dirs with origin) all changes need to be noted.
549 * For non-merge dirs, cache contains only impure entries (i.e. ones
550 * which have been copied up and have origins), so only need to note
551 * changes to impure entries.
553 if (!ovl_dir_is_real(inode) || impurity)
554 OVL_I(inode)->version++;
557 void ovl_dir_modified(struct dentry *dentry, bool impurity)
559 /* Copy mtime/ctime */
560 ovl_copyattr(d_inode(dentry));
562 ovl_dir_version_inc(dentry, impurity);
565 u64 ovl_inode_version_get(struct inode *inode)
567 WARN_ON(!inode_is_locked(inode));
568 return OVL_I(inode)->version;
571 bool ovl_is_whiteout(struct dentry *dentry)
573 struct inode *inode = dentry->d_inode;
575 return inode && IS_WHITEOUT(inode);
578 struct file *ovl_path_open(const struct path *path, int flags)
580 struct inode *inode = d_inode(path->dentry);
581 struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
584 if (flags & ~(O_ACCMODE | O_LARGEFILE))
587 switch (flags & O_ACCMODE) {
592 acc_mode = MAY_WRITE;
598 err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
602 /* O_NOATIME is an optimization, don't fail if not permitted */
603 if (inode_owner_or_capable(real_idmap, inode))
606 return dentry_open(path, flags, current_cred());
609 /* Caller should hold ovl_inode->lock */
610 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
612 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
614 if (ovl_dentry_upper(dentry) &&
615 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
616 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
622 bool ovl_already_copied_up(struct dentry *dentry, int flags)
624 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
627 * Check if copy-up has happened as well as for upper alias (in
628 * case of hard links) is there.
630 * Both checks are lockless:
631 * - false negatives: will recheck under oi->lock
633 * + ovl_dentry_upper() uses memory barriers to ensure the
634 * upper dentry is up-to-date
635 * + ovl_dentry_has_upper_alias() relies on locking of
636 * upper parent i_rwsem to prevent reordering copy-up
639 if (ovl_dentry_upper(dentry) &&
640 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
641 !ovl_dentry_needs_data_copy_up(dentry, flags))
647 int ovl_copy_up_start(struct dentry *dentry, int flags)
649 struct inode *inode = d_inode(dentry);
652 err = ovl_inode_lock_interruptible(inode);
653 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
654 err = 1; /* Already copied up */
655 ovl_inode_unlock(inode);
661 void ovl_copy_up_end(struct dentry *dentry)
663 ovl_inode_unlock(d_inode(dentry));
666 bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
670 res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
672 /* Zero size value means "copied up but origin unknown" */
680 * Load persistent uuid from xattr into s_uuid if found, or store a new
681 * random generated value in s_uuid and in xattr.
683 bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
684 const struct path *upperpath)
689 /* Try to load existing persistent uuid */
690 res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, sb->s_uuid.b,
692 if (res == UUID_SIZE)
699 * With uuid=auto, if uuid xattr is found, it will be used.
700 * If uuid xattrs is not found, generate a persistent uuid only on mount
701 * of new overlays where upper root dir is not yet marked as impure.
702 * An upper dir is marked as impure on copy up or lookup of its subdirs.
704 if (ofs->config.uuid == OVL_UUID_AUTO) {
705 res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_IMPURE, NULL,
708 /* Any mount of old overlay - downgrade to uuid=null */
709 ofs->config.uuid = OVL_UUID_NULL;
711 } else if (res == -ENODATA) {
712 /* First mount of new overlay - upgrade to uuid=on */
713 ofs->config.uuid = OVL_UUID_ON;
714 } else if (res < 0) {
720 /* Generate overlay instance uuid */
721 uuid_gen(&sb->s_uuid);
723 /* Try to store persistent uuid */
725 res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, sb->s_uuid.b,
731 memset(sb->s_uuid.b, 0, UUID_SIZE);
732 ofs->config.uuid = OVL_UUID_NULL;
733 pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n",
734 set ? "set" : "get", upperpath->dentry, res);
738 bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
744 if (!d_is_dir(path->dentry))
747 res = ovl_path_getxattr(ofs, path, ox, &val, 1);
748 if (res == 1 && val == 'y')
754 #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
755 #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
756 #define OVL_XATTR_ORIGIN_POSTFIX "origin"
757 #define OVL_XATTR_IMPURE_POSTFIX "impure"
758 #define OVL_XATTR_NLINK_POSTFIX "nlink"
759 #define OVL_XATTR_UPPER_POSTFIX "upper"
760 #define OVL_XATTR_UUID_POSTFIX "uuid"
761 #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
762 #define OVL_XATTR_PROTATTR_POSTFIX "protattr"
764 #define OVL_XATTR_TAB_ENTRY(x) \
765 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
766 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
768 const char *const ovl_xattr_table[][2] = {
769 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
770 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
771 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
772 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
773 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
774 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
775 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID),
776 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
777 OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
780 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
781 enum ovl_xattr ox, const void *value, size_t size,
789 err = ovl_setxattr(ofs, upperdentry, ox, value, size);
791 if (err == -EOPNOTSUPP) {
792 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
800 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
802 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
805 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
809 * Do not fail when upper doesn't support xattrs.
810 * Upper inodes won't have origin nor redirect xattr anyway.
812 err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
814 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
820 #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
822 void ovl_check_protattr(struct inode *inode, struct dentry *upper)
824 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
825 u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
826 char buf[OVL_PROTATTR_MAX+1];
829 res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
835 * Initialize inode flags from overlay.protattr xattr and upper inode
836 * flags. If upper inode has those fileattr flags set (i.e. from old
837 * kernel), we do not clear them on ovl_get_inode(), but we will clear
838 * them on next fileattr_set().
840 for (n = 0; n < res; n++) {
843 else if (buf[n] == 'i')
844 iflags |= S_IMMUTABLE;
849 if (!res || n < res) {
850 pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
853 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
857 int ovl_set_protattr(struct inode *inode, struct dentry *upper,
860 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
861 char buf[OVL_PROTATTR_MAX];
862 int len = 0, err = 0;
865 BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
867 if (fa->flags & FS_APPEND_FL) {
871 if (fa->flags & FS_IMMUTABLE_FL) {
873 iflags |= S_IMMUTABLE;
877 * Do not allow to set protection flags when upper doesn't support
878 * xattrs, because we do not set those fileattr flags on upper inode.
879 * Remove xattr if it exist and all protection flags are cleared.
882 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
884 } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
885 err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
886 if (err == -EOPNOTSUPP || err == -ENODATA)
892 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
894 /* Mask out the fileattr flags that should not be set in upper inode */
895 fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
896 fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
902 * Caller must hold a reference to inode to prevent it from being freed while
903 * it is marked inuse.
905 bool ovl_inuse_trylock(struct dentry *dentry)
907 struct inode *inode = d_inode(dentry);
910 spin_lock(&inode->i_lock);
911 if (!(inode->i_state & I_OVL_INUSE)) {
912 inode->i_state |= I_OVL_INUSE;
915 spin_unlock(&inode->i_lock);
920 void ovl_inuse_unlock(struct dentry *dentry)
923 struct inode *inode = d_inode(dentry);
925 spin_lock(&inode->i_lock);
926 WARN_ON(!(inode->i_state & I_OVL_INUSE));
927 inode->i_state &= ~I_OVL_INUSE;
928 spin_unlock(&inode->i_lock);
932 bool ovl_is_inuse(struct dentry *dentry)
934 struct inode *inode = d_inode(dentry);
937 spin_lock(&inode->i_lock);
938 inuse = (inode->i_state & I_OVL_INUSE);
939 spin_unlock(&inode->i_lock);
945 * Does this overlay dentry need to be indexed on copy up?
947 bool ovl_need_index(struct dentry *dentry)
949 struct dentry *lower = ovl_dentry_lower(dentry);
951 if (!lower || !ovl_indexdir(dentry->d_sb))
954 /* Index all files for NFS export and consistency verification */
955 if (ovl_index_all(dentry->d_sb))
958 /* Index only lower hardlinks on copy up */
959 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
965 /* Caller must hold OVL_I(inode)->lock */
966 static void ovl_cleanup_index(struct dentry *dentry)
968 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
969 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
970 struct inode *dir = indexdir->d_inode;
971 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
972 struct dentry *upperdentry = ovl_dentry_upper(dentry);
973 struct dentry *index = NULL;
975 struct qstr name = { };
978 err = ovl_get_index_name(ofs, lowerdentry, &name);
982 inode = d_inode(upperdentry);
983 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
984 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
985 upperdentry, inode->i_ino, inode->i_nlink);
987 * We either have a bug with persistent union nlink or a lower
988 * hardlink was added while overlay is mounted. Adding a lower
989 * hardlink and then unlinking all overlay hardlinks would drop
990 * overlay nlink to zero before all upper inodes are unlinked.
991 * As a safety measure, when that situation is detected, set
992 * the overlay nlink to the index inode nlink minus one for the
993 * index entry itself.
995 set_nlink(d_inode(dentry), inode->i_nlink - 1);
996 ovl_set_nlink_upper(dentry);
1000 inode_lock_nested(dir, I_MUTEX_PARENT);
1001 index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
1002 err = PTR_ERR(index);
1003 if (IS_ERR(index)) {
1005 } else if (ovl_index_all(dentry->d_sb)) {
1006 /* Whiteout orphan index to block future open by handle */
1007 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
1010 /* Cleanup orphan index entries */
1011 err = ovl_cleanup(ofs, dir, index);
1024 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
1029 * Operations that change overlay inode and upper inode nlink need to be
1030 * synchronized with copy up for persistent nlink accounting.
1032 int ovl_nlink_start(struct dentry *dentry)
1034 struct inode *inode = d_inode(dentry);
1035 const struct cred *old_cred;
1038 if (WARN_ON(!inode))
1042 * With inodes index is enabled, we store the union overlay nlink
1043 * in an xattr on the index inode. When whiting out an indexed lower,
1044 * we need to decrement the overlay persistent nlink, but before the
1045 * first copy up, we have no upper index inode to store the xattr.
1047 * As a workaround, before whiteout/rename over an indexed lower,
1048 * copy up to create the upper index. Creating the upper index will
1049 * initialize the overlay nlink, so it could be dropped if unlink
1050 * or rename succeeds.
1052 * TODO: implement metadata only index copy up when called with
1053 * ovl_copy_up_flags(dentry, O_PATH).
1055 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
1056 err = ovl_copy_up(dentry);
1061 err = ovl_inode_lock_interruptible(inode);
1065 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
1068 old_cred = ovl_override_creds(dentry->d_sb);
1070 * The overlay inode nlink should be incremented/decremented IFF the
1071 * upper operation succeeds, along with nlink change of upper inode.
1072 * Therefore, before link/unlink/rename, we store the union nlink
1073 * value relative to the upper inode nlink in an upper inode xattr.
1075 err = ovl_set_nlink_upper(dentry);
1076 revert_creds(old_cred);
1080 ovl_inode_unlock(inode);
1085 void ovl_nlink_end(struct dentry *dentry)
1087 struct inode *inode = d_inode(dentry);
1089 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
1090 const struct cred *old_cred;
1092 old_cred = ovl_override_creds(dentry->d_sb);
1093 ovl_cleanup_index(dentry);
1094 revert_creds(old_cred);
1097 ovl_inode_unlock(inode);
1100 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
1102 /* Workdir should not be the same as upperdir */
1103 if (workdir == upperdir)
1106 /* Workdir should not be subdir of upperdir and vice versa */
1107 if (lock_rename(workdir, upperdir) != NULL)
1113 unlock_rename(workdir, upperdir);
1115 pr_err("failed to lock workdir+upperdir\n");
1120 * err < 0, 0 if no metacopy xattr, metacopy data size if xattr found.
1121 * an empty xattr returns OVL_METACOPY_MIN_SIZE to distinguish from no xattr value.
1123 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
1124 struct ovl_metacopy *data)
1128 /* Only regular files can have metacopy xattr */
1129 if (!S_ISREG(d_inode(path->dentry)->i_mode))
1132 res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY,
1133 data, data ? OVL_METACOPY_MAX_SIZE : 0);
1135 if (res == -ENODATA || res == -EOPNOTSUPP)
1138 * getxattr on user.* may fail with EACCES in case there's no
1139 * read permission on the inode. Not much we can do, other than
1140 * tell the caller that this is not a metacopy inode.
1142 if (ofs->config.userxattr && res == -EACCES)
1148 /* Emulate empty data for zero size metacopy xattr */
1149 res = OVL_METACOPY_MIN_SIZE;
1151 memset(data, 0, res);
1154 } else if (res < OVL_METACOPY_MIN_SIZE) {
1155 pr_warn_ratelimited("metacopy file '%pd' has too small xattr\n",
1159 if (data->version != 0) {
1160 pr_warn_ratelimited("metacopy file '%pd' has unsupported version\n",
1164 if (res != data->len) {
1165 pr_warn_ratelimited("metacopy file '%pd' has invalid xattr size\n",
1173 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
1177 int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, struct ovl_metacopy *metacopy)
1179 size_t len = metacopy->len;
1181 /* If no flags or digest fall back to empty metacopy file */
1182 if (metacopy->version == 0 && metacopy->flags == 0 && metacopy->digest_algo == 0)
1185 return ovl_check_setxattr(ofs, d, OVL_XATTR_METACOPY,
1186 metacopy, len, -EOPNOTSUPP);
1189 bool ovl_is_metacopy_dentry(struct dentry *dentry)
1191 struct ovl_entry *oe = OVL_E(dentry);
1193 if (!d_is_reg(dentry))
1196 if (ovl_dentry_upper(dentry)) {
1197 if (!ovl_has_upperdata(d_inode(dentry)))
1202 return (ovl_numlower(oe) > 1);
1205 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
1208 char *s, *next, *buf = NULL;
1210 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
1211 if (res == -ENODATA || res == -EOPNOTSUPP)
1218 buf = kzalloc(res + padding + 1, GFP_KERNEL);
1220 return ERR_PTR(-ENOMEM);
1222 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
1228 if (buf[0] == '/') {
1229 for (s = buf; *s++ == '/'; s = next) {
1230 next = strchrnul(s, '/');
1235 if (strchr(buf, '/') != NULL)
1241 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
1245 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1248 return ERR_PTR(res);
1251 /* Call with mounter creds as it may open the file */
1252 int ovl_ensure_verity_loaded(struct path *datapath)
1254 struct inode *inode = d_inode(datapath->dentry);
1257 if (!fsverity_active(inode) && IS_VERITY(inode)) {
1259 * If this inode was not yet opened, the verity info hasn't been
1260 * loaded yet, so we need to do that here to force it into memory.
1262 filp = kernel_file_open(datapath, O_RDONLY, inode, current_cred());
1264 return PTR_ERR(filp);
1271 int ovl_validate_verity(struct ovl_fs *ofs,
1272 struct path *metapath,
1273 struct path *datapath)
1275 struct ovl_metacopy metacopy_data;
1276 u8 actual_digest[FS_VERITY_MAX_DIGEST_SIZE];
1277 int xattr_digest_size, digest_size;
1278 int xattr_size, err;
1281 if (!ofs->config.verity_mode ||
1282 /* Verity only works on regular files */
1283 !S_ISREG(d_inode(metapath->dentry)->i_mode))
1286 xattr_size = ovl_check_metacopy_xattr(ofs, metapath, &metacopy_data);
1290 if (!xattr_size || !metacopy_data.digest_algo) {
1291 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1292 pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n",
1299 xattr_digest_size = ovl_metadata_digest_size(&metacopy_data);
1301 err = ovl_ensure_verity_loaded(datapath);
1303 pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1308 digest_size = fsverity_get_digest(d_inode(datapath->dentry), actual_digest,
1309 &verity_algo, NULL);
1310 if (digest_size == 0) {
1311 pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", datapath->dentry);
1315 if (xattr_digest_size != digest_size ||
1316 metacopy_data.digest_algo != verity_algo ||
1317 memcmp(metacopy_data.digest, actual_digest, xattr_digest_size) != 0) {
1318 pr_warn_ratelimited("lower file '%pd' has the wrong fs-verity digest\n",
1326 int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src,
1327 struct ovl_metacopy *metacopy)
1329 int err, digest_size;
1331 if (!ofs->config.verity_mode || !S_ISREG(d_inode(src->dentry)->i_mode))
1334 err = ovl_ensure_verity_loaded(src);
1336 pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1341 digest_size = fsverity_get_digest(d_inode(src->dentry),
1342 metacopy->digest, &metacopy->digest_algo, NULL);
1343 if (digest_size == 0 ||
1344 WARN_ON_ONCE(digest_size > FS_VERITY_MAX_DIGEST_SIZE)) {
1345 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1346 pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n",
1353 metacopy->len += digest_size;
1358 * ovl_sync_status() - Check fs sync status for volatile mounts
1360 * Returns 1 if this is not a volatile mount and a real sync is required.
1362 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1363 * have occurred on the upperdir since the mount.
1365 * Returns -errno if it is a volatile mount, and the error that occurred since
1366 * the last mount. If the error code changes, it'll return the latest error
1370 int ovl_sync_status(struct ovl_fs *ofs)
1372 struct vfsmount *mnt;
1374 if (ovl_should_sync(ofs))
1377 mnt = ovl_upper_mnt(ofs);
1381 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1385 * ovl_copyattr() - copy inode attributes from layer to ovl inode
1387 * When overlay copies inode information from an upper or lower layer to the
1388 * relevant overlay inode it will apply the idmapping of the upper or lower
1389 * layer when doing so ensuring that the ovl inode ownership will correctly
1390 * reflect the ownership of the idmapped upper or lower layer. For example, an
1391 * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1392 * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1393 * helpers are nops when the relevant layer isn't idmapped.
1395 void ovl_copyattr(struct inode *inode)
1397 struct path realpath;
1398 struct inode *realinode;
1399 struct mnt_idmap *real_idmap;
1403 realinode = ovl_i_path_real(inode, &realpath);
1404 real_idmap = mnt_idmap(realpath.mnt);
1406 vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1407 vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
1409 inode->i_uid = vfsuid_into_kuid(vfsuid);
1410 inode->i_gid = vfsgid_into_kgid(vfsgid);
1411 inode->i_mode = realinode->i_mode;
1412 inode->i_atime = realinode->i_atime;
1413 inode->i_mtime = realinode->i_mtime;
1414 inode_set_ctime_to_ts(inode, inode_get_ctime(realinode));
1415 i_size_write(inode, i_size_read(realinode));