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 /* Get write access to upper mnt - may fail if upper sb was remounted ro */
21 int ovl_get_write_access(struct dentry *dentry)
23 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
24 return mnt_get_write_access(ovl_upper_mnt(ofs));
27 /* Get write access to upper sb - may block if upper sb is frozen */
28 void ovl_start_write(struct dentry *dentry)
30 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
31 sb_start_write(ovl_upper_mnt(ofs)->mnt_sb);
34 int ovl_want_write(struct dentry *dentry)
36 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
37 return mnt_want_write(ovl_upper_mnt(ofs));
40 void ovl_put_write_access(struct dentry *dentry)
42 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
43 mnt_put_write_access(ovl_upper_mnt(ofs));
46 void ovl_end_write(struct dentry *dentry)
48 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
49 sb_end_write(ovl_upper_mnt(ofs)->mnt_sb);
52 void ovl_drop_write(struct dentry *dentry)
54 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
55 mnt_drop_write(ovl_upper_mnt(ofs));
58 struct dentry *ovl_workdir(struct dentry *dentry)
60 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
64 const struct cred *ovl_override_creds(struct super_block *sb)
66 struct ovl_fs *ofs = OVL_FS(sb);
68 return override_creds(ofs->creator_cred);
72 * Check if underlying fs supports file handles and try to determine encoding
73 * type, in order to deduce maximum inode number used by fs.
75 * Return 0 if file handles are not supported.
76 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
77 * Return -1 if fs uses a non default encoding with unknown inode size.
79 int ovl_can_decode_fh(struct super_block *sb)
81 if (!capable(CAP_DAC_READ_SEARCH))
84 if (!exportfs_can_decode_fh(sb->s_export_op))
87 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
90 struct dentry *ovl_indexdir(struct super_block *sb)
92 struct ovl_fs *ofs = OVL_FS(sb);
97 /* Index all files on copy up. For now only enabled for NFS export */
98 bool ovl_index_all(struct super_block *sb)
100 struct ovl_fs *ofs = OVL_FS(sb);
102 return ofs->config.nfs_export && ofs->config.index;
105 /* Verify lower origin on lookup. For now only enabled for NFS export */
106 bool ovl_verify_lower(struct super_block *sb)
108 struct ovl_fs *ofs = OVL_FS(sb);
110 return ofs->config.nfs_export && ofs->config.index;
113 struct ovl_path *ovl_stack_alloc(unsigned int n)
115 return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL);
118 void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
122 memcpy(dst, src, sizeof(struct ovl_path) * n);
123 for (i = 0; i < n; i++)
127 void ovl_stack_put(struct ovl_path *stack, unsigned int n)
131 for (i = 0; stack && i < n; i++)
132 dput(stack[i].dentry);
135 void ovl_stack_free(struct ovl_path *stack, unsigned int n)
137 ovl_stack_put(stack, n);
141 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
143 size_t size = offsetof(struct ovl_entry, __lowerstack[numlower]);
144 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
147 oe->__numlower = numlower;
152 void ovl_free_entry(struct ovl_entry *oe)
154 ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
158 #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
160 bool ovl_dentry_remote(struct dentry *dentry)
162 return dentry->d_flags & OVL_D_REVALIDATE;
165 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
167 if (!ovl_dentry_remote(realdentry))
170 spin_lock(&dentry->d_lock);
171 dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
172 spin_unlock(&dentry->d_lock);
175 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
176 struct ovl_entry *oe)
178 return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
181 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
182 struct ovl_entry *oe, unsigned int mask)
184 struct ovl_path *lowerstack = ovl_lowerstack(oe);
185 unsigned int i, flags = 0;
188 flags |= upperdentry->d_flags;
189 for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
190 flags |= lowerstack[i].dentry->d_flags;
192 spin_lock(&dentry->d_lock);
193 dentry->d_flags &= ~mask;
194 dentry->d_flags |= flags & mask;
195 spin_unlock(&dentry->d_lock);
198 bool ovl_dentry_weird(struct dentry *dentry)
200 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
201 DCACHE_MANAGE_TRANSIT |
206 enum ovl_path_type ovl_path_type(struct dentry *dentry)
208 struct ovl_entry *oe = OVL_E(dentry);
209 enum ovl_path_type type = 0;
211 if (ovl_dentry_upper(dentry)) {
212 type = __OVL_PATH_UPPER;
215 * Non-dir dentry can hold lower dentry of its copy up origin.
217 if (ovl_numlower(oe)) {
218 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
219 type |= __OVL_PATH_ORIGIN;
220 if (d_is_dir(dentry) ||
221 !ovl_has_upperdata(d_inode(dentry)))
222 type |= __OVL_PATH_MERGE;
225 if (ovl_numlower(oe) > 1)
226 type |= __OVL_PATH_MERGE;
231 void ovl_path_upper(struct dentry *dentry, struct path *path)
233 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
235 path->mnt = ovl_upper_mnt(ofs);
236 path->dentry = ovl_dentry_upper(dentry);
239 void ovl_path_lower(struct dentry *dentry, struct path *path)
241 struct ovl_entry *oe = OVL_E(dentry);
242 struct ovl_path *lowerpath = ovl_lowerstack(oe);
244 if (ovl_numlower(oe)) {
245 path->mnt = lowerpath->layer->mnt;
246 path->dentry = lowerpath->dentry;
248 *path = (struct path) { };
252 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
254 struct ovl_entry *oe = OVL_E(dentry);
255 struct ovl_path *lowerdata = ovl_lowerdata(oe);
256 struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
258 if (lowerdata_dentry) {
259 path->dentry = lowerdata_dentry;
261 * Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
262 * Make sure that if lowerdata->dentry is visible, then
263 * datapath->layer is visible as well.
266 path->mnt = READ_ONCE(lowerdata->layer)->mnt;
268 *path = (struct path) { };
272 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
274 enum ovl_path_type type = ovl_path_type(dentry);
276 if (!OVL_TYPE_UPPER(type))
277 ovl_path_lower(dentry, path);
279 ovl_path_upper(dentry, path);
284 enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
286 enum ovl_path_type type = ovl_path_type(dentry);
288 WARN_ON_ONCE(d_is_dir(dentry));
290 if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
291 ovl_path_lowerdata(dentry, path);
293 ovl_path_upper(dentry, path);
298 struct dentry *ovl_dentry_upper(struct dentry *dentry)
300 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
303 struct dentry *ovl_dentry_lower(struct dentry *dentry)
305 struct ovl_entry *oe = OVL_E(dentry);
307 return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
310 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
312 struct ovl_entry *oe = OVL_E(dentry);
314 return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
318 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
319 * depending on what is stored in lowerstack[0]. At times we need to find
320 * lower dentry which has data (and not metacopy dentry). This helper
321 * returns the lower data dentry.
323 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
325 return ovl_lowerdata_dentry(OVL_E(dentry));
328 int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
330 struct ovl_entry *oe = OVL_E(dentry);
331 struct ovl_path *lowerdata = ovl_lowerdata(oe);
332 struct dentry *datadentry = datapath->dentry;
334 if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
337 WRITE_ONCE(lowerdata->layer, datapath->layer);
339 * Pairs with smp_rmb() in ovl_path_lowerdata().
340 * Make sure that if lowerdata->dentry is visible, then
341 * lowerdata->layer is visible as well.
344 WRITE_ONCE(lowerdata->dentry, dget(datadentry));
346 ovl_dentry_update_reval(dentry, datadentry);
351 struct dentry *ovl_dentry_real(struct dentry *dentry)
353 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
356 struct dentry *ovl_i_dentry_upper(struct inode *inode)
358 return ovl_upperdentry_dereference(OVL_I(inode));
361 struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
363 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
365 path->dentry = ovl_i_dentry_upper(inode);
367 path->dentry = lowerpath->dentry;
368 path->mnt = lowerpath->layer->mnt;
370 path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
373 return path->dentry ? d_inode_rcu(path->dentry) : NULL;
376 struct inode *ovl_inode_upper(struct inode *inode)
378 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
380 return upperdentry ? d_inode(upperdentry) : NULL;
383 struct inode *ovl_inode_lower(struct inode *inode)
385 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
387 return lowerpath ? d_inode(lowerpath->dentry) : NULL;
390 struct inode *ovl_inode_real(struct inode *inode)
392 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
395 /* Return inode which contains lower data. Do not return metacopy */
396 struct inode *ovl_inode_lowerdata(struct inode *inode)
398 struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode));
400 if (WARN_ON(!S_ISREG(inode->i_mode)))
403 return lowerdata ? d_inode(lowerdata) : NULL;
406 /* Return real inode which contains data. Does not return metacopy inode */
407 struct inode *ovl_inode_realdata(struct inode *inode)
409 struct inode *upperinode;
411 upperinode = ovl_inode_upper(inode);
412 if (upperinode && ovl_has_upperdata(inode))
415 return ovl_inode_lowerdata(inode);
418 const char *ovl_lowerdata_redirect(struct inode *inode)
420 return inode && S_ISREG(inode->i_mode) ?
421 OVL_I(inode)->lowerdata_redirect : NULL;
424 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
426 return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL;
429 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
431 OVL_I(inode)->cache = cache;
434 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
436 set_bit(flag, OVL_E_FLAGS(dentry));
439 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
441 clear_bit(flag, OVL_E_FLAGS(dentry));
444 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
446 return test_bit(flag, OVL_E_FLAGS(dentry));
449 bool ovl_dentry_is_opaque(struct dentry *dentry)
451 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
454 bool ovl_dentry_is_whiteout(struct dentry *dentry)
456 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
459 void ovl_dentry_set_opaque(struct dentry *dentry)
461 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
465 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
466 * to return positive, while there's no actual upper alias for the inode.
467 * Copy up code needs to know about the existence of the upper alias, so it
468 * can't use ovl_dentry_upper().
470 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
472 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
475 void ovl_dentry_set_upper_alias(struct dentry *dentry)
477 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
480 static bool ovl_should_check_upperdata(struct inode *inode)
482 if (!S_ISREG(inode->i_mode))
485 if (!ovl_inode_lower(inode))
491 bool ovl_has_upperdata(struct inode *inode)
493 if (!ovl_should_check_upperdata(inode))
496 if (!ovl_test_flag(OVL_UPPERDATA, inode))
499 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
500 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
501 * if setting of OVL_UPPERDATA is visible, then effects of writes
502 * before that are visible too.
508 void ovl_set_upperdata(struct inode *inode)
511 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
512 * if OVL_UPPERDATA flag is visible, then effects of write operations
513 * before it are visible as well.
516 ovl_set_flag(OVL_UPPERDATA, inode);
519 /* Caller should hold ovl_inode->lock */
520 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
522 if (!ovl_open_flags_need_copy_up(flags))
525 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
528 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
530 if (!ovl_open_flags_need_copy_up(flags))
533 return !ovl_has_upperdata(d_inode(dentry));
536 const char *ovl_dentry_get_redirect(struct dentry *dentry)
538 return OVL_I(d_inode(dentry))->redirect;
541 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
543 struct ovl_inode *oi = OVL_I(d_inode(dentry));
546 oi->redirect = redirect;
549 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
551 struct inode *upperinode = d_inode(upperdentry);
553 WARN_ON(OVL_I(inode)->__upperdentry);
556 * Make sure upperdentry is consistent before making it visible
559 OVL_I(inode)->__upperdentry = upperdentry;
560 if (inode_unhashed(inode)) {
561 inode->i_private = upperinode;
562 __insert_inode_hash(inode, (unsigned long) upperinode);
566 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
568 struct inode *inode = d_inode(dentry);
570 WARN_ON(!inode_is_locked(inode));
571 WARN_ON(!d_is_dir(dentry));
573 * Version is used by readdir code to keep cache consistent.
574 * For merge dirs (or dirs with origin) all changes need to be noted.
575 * For non-merge dirs, cache contains only impure entries (i.e. ones
576 * which have been copied up and have origins), so only need to note
577 * changes to impure entries.
579 if (!ovl_dir_is_real(inode) || impurity)
580 OVL_I(inode)->version++;
583 void ovl_dir_modified(struct dentry *dentry, bool impurity)
585 /* Copy mtime/ctime */
586 ovl_copyattr(d_inode(dentry));
588 ovl_dir_version_inc(dentry, impurity);
591 u64 ovl_inode_version_get(struct inode *inode)
593 WARN_ON(!inode_is_locked(inode));
594 return OVL_I(inode)->version;
597 bool ovl_is_whiteout(struct dentry *dentry)
599 struct inode *inode = dentry->d_inode;
601 return inode && IS_WHITEOUT(inode);
605 * Use this over ovl_is_whiteout for upper and lower files, as it also
606 * handles overlay.whiteout xattr whiteout files.
608 bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path)
610 return ovl_is_whiteout(path->dentry) ||
611 ovl_path_check_xwhiteout_xattr(ofs, path);
614 struct file *ovl_path_open(const struct path *path, int flags)
616 struct inode *inode = d_inode(path->dentry);
617 struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
620 if (flags & ~(O_ACCMODE | O_LARGEFILE))
623 switch (flags & O_ACCMODE) {
628 acc_mode = MAY_WRITE;
634 err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
638 /* O_NOATIME is an optimization, don't fail if not permitted */
639 if (inode_owner_or_capable(real_idmap, inode))
642 return dentry_open(path, flags, current_cred());
645 /* Caller should hold ovl_inode->lock */
646 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
648 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
650 if (ovl_dentry_upper(dentry) &&
651 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
652 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
658 bool ovl_already_copied_up(struct dentry *dentry, int flags)
660 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
663 * Check if copy-up has happened as well as for upper alias (in
664 * case of hard links) is there.
666 * Both checks are lockless:
667 * - false negatives: will recheck under oi->lock
669 * + ovl_dentry_upper() uses memory barriers to ensure the
670 * upper dentry is up-to-date
671 * + ovl_dentry_has_upper_alias() relies on locking of
672 * upper parent i_rwsem to prevent reordering copy-up
675 if (ovl_dentry_upper(dentry) &&
676 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
677 !ovl_dentry_needs_data_copy_up(dentry, flags))
684 * The copy up "transaction" keeps an elevated mnt write count on upper mnt,
685 * but leaves taking freeze protection on upper sb to lower level helpers.
687 int ovl_copy_up_start(struct dentry *dentry, int flags)
689 struct inode *inode = d_inode(dentry);
692 err = ovl_inode_lock_interruptible(inode);
696 if (ovl_already_copied_up_locked(dentry, flags))
697 err = 1; /* Already copied up */
699 err = ovl_get_write_access(dentry);
706 ovl_inode_unlock(inode);
710 void ovl_copy_up_end(struct dentry *dentry)
712 ovl_put_write_access(dentry);
713 ovl_inode_unlock(d_inode(dentry));
716 bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
720 res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
722 /* Zero size value means "copied up but origin unknown" */
729 bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path)
731 struct dentry *dentry = path->dentry;
734 /* xattr.whiteout must be a zero size regular file */
735 if (!d_is_reg(dentry) || i_size_read(d_inode(dentry)) != 0)
738 res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUT, NULL, 0);
742 bool ovl_path_check_xwhiteouts_xattr(struct ovl_fs *ofs, const struct path *path)
744 struct dentry *dentry = path->dentry;
747 /* xattr.whiteouts must be a directory */
748 if (!d_is_dir(dentry))
751 res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUTS, NULL, 0);
756 * Load persistent uuid from xattr into s_uuid if found, or store a new
757 * random generated value in s_uuid and in xattr.
759 bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
760 const struct path *upperpath)
765 /* Try to load existing persistent uuid */
766 res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, sb->s_uuid.b,
768 if (res == UUID_SIZE)
775 * With uuid=auto, if uuid xattr is found, it will be used.
776 * If uuid xattrs is not found, generate a persistent uuid only on mount
777 * of new overlays where upper root dir is not yet marked as impure.
778 * An upper dir is marked as impure on copy up or lookup of its subdirs.
780 if (ofs->config.uuid == OVL_UUID_AUTO) {
781 res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_IMPURE, NULL,
784 /* Any mount of old overlay - downgrade to uuid=null */
785 ofs->config.uuid = OVL_UUID_NULL;
787 } else if (res == -ENODATA) {
788 /* First mount of new overlay - upgrade to uuid=on */
789 ofs->config.uuid = OVL_UUID_ON;
790 } else if (res < 0) {
796 /* Generate overlay instance uuid */
797 uuid_gen(&sb->s_uuid);
799 /* Try to store persistent uuid */
801 res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, sb->s_uuid.b,
807 memset(sb->s_uuid.b, 0, UUID_SIZE);
808 ofs->config.uuid = OVL_UUID_NULL;
809 pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n",
810 set ? "set" : "get", upperpath->dentry, res);
814 bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
820 if (!d_is_dir(path->dentry))
823 res = ovl_path_getxattr(ofs, path, ox, &val, 1);
824 if (res == 1 && val == 'y')
830 #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
831 #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
832 #define OVL_XATTR_ORIGIN_POSTFIX "origin"
833 #define OVL_XATTR_IMPURE_POSTFIX "impure"
834 #define OVL_XATTR_NLINK_POSTFIX "nlink"
835 #define OVL_XATTR_UPPER_POSTFIX "upper"
836 #define OVL_XATTR_UUID_POSTFIX "uuid"
837 #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
838 #define OVL_XATTR_PROTATTR_POSTFIX "protattr"
839 #define OVL_XATTR_XWHITEOUT_POSTFIX "whiteout"
840 #define OVL_XATTR_XWHITEOUTS_POSTFIX "whiteouts"
842 #define OVL_XATTR_TAB_ENTRY(x) \
843 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
844 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
846 const char *const ovl_xattr_table[][2] = {
847 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
848 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
849 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
850 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
851 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
852 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
853 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID),
854 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
855 OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
856 OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUT),
857 OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUTS),
860 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
861 enum ovl_xattr ox, const void *value, size_t size,
869 err = ovl_setxattr(ofs, upperdentry, ox, value, size);
871 if (err == -EOPNOTSUPP) {
872 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
880 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
882 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
885 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
889 * Do not fail when upper doesn't support xattrs.
890 * Upper inodes won't have origin nor redirect xattr anyway.
892 err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
894 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
900 #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
902 void ovl_check_protattr(struct inode *inode, struct dentry *upper)
904 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
905 u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
906 char buf[OVL_PROTATTR_MAX+1];
909 res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
915 * Initialize inode flags from overlay.protattr xattr and upper inode
916 * flags. If upper inode has those fileattr flags set (i.e. from old
917 * kernel), we do not clear them on ovl_get_inode(), but we will clear
918 * them on next fileattr_set().
920 for (n = 0; n < res; n++) {
923 else if (buf[n] == 'i')
924 iflags |= S_IMMUTABLE;
929 if (!res || n < res) {
930 pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
933 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
937 int ovl_set_protattr(struct inode *inode, struct dentry *upper,
940 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
941 char buf[OVL_PROTATTR_MAX];
942 int len = 0, err = 0;
945 BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
947 if (fa->flags & FS_APPEND_FL) {
951 if (fa->flags & FS_IMMUTABLE_FL) {
953 iflags |= S_IMMUTABLE;
957 * Do not allow to set protection flags when upper doesn't support
958 * xattrs, because we do not set those fileattr flags on upper inode.
959 * Remove xattr if it exist and all protection flags are cleared.
962 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
964 } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
965 err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
966 if (err == -EOPNOTSUPP || err == -ENODATA)
972 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
974 /* Mask out the fileattr flags that should not be set in upper inode */
975 fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
976 fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
982 * Caller must hold a reference to inode to prevent it from being freed while
983 * it is marked inuse.
985 bool ovl_inuse_trylock(struct dentry *dentry)
987 struct inode *inode = d_inode(dentry);
990 spin_lock(&inode->i_lock);
991 if (!(inode->i_state & I_OVL_INUSE)) {
992 inode->i_state |= I_OVL_INUSE;
995 spin_unlock(&inode->i_lock);
1000 void ovl_inuse_unlock(struct dentry *dentry)
1003 struct inode *inode = d_inode(dentry);
1005 spin_lock(&inode->i_lock);
1006 WARN_ON(!(inode->i_state & I_OVL_INUSE));
1007 inode->i_state &= ~I_OVL_INUSE;
1008 spin_unlock(&inode->i_lock);
1012 bool ovl_is_inuse(struct dentry *dentry)
1014 struct inode *inode = d_inode(dentry);
1017 spin_lock(&inode->i_lock);
1018 inuse = (inode->i_state & I_OVL_INUSE);
1019 spin_unlock(&inode->i_lock);
1025 * Does this overlay dentry need to be indexed on copy up?
1027 bool ovl_need_index(struct dentry *dentry)
1029 struct dentry *lower = ovl_dentry_lower(dentry);
1031 if (!lower || !ovl_indexdir(dentry->d_sb))
1034 /* Index all files for NFS export and consistency verification */
1035 if (ovl_index_all(dentry->d_sb))
1038 /* Index only lower hardlinks on copy up */
1039 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
1045 /* Caller must hold OVL_I(inode)->lock */
1046 static void ovl_cleanup_index(struct dentry *dentry)
1048 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1049 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
1050 struct inode *dir = indexdir->d_inode;
1051 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
1052 struct dentry *upperdentry = ovl_dentry_upper(dentry);
1053 struct dentry *index = NULL;
1054 struct inode *inode;
1055 struct qstr name = { };
1056 bool got_write = false;
1059 err = ovl_get_index_name(ofs, lowerdentry, &name);
1063 err = ovl_want_write(dentry);
1068 inode = d_inode(upperdentry);
1069 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
1070 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
1071 upperdentry, inode->i_ino, inode->i_nlink);
1073 * We either have a bug with persistent union nlink or a lower
1074 * hardlink was added while overlay is mounted. Adding a lower
1075 * hardlink and then unlinking all overlay hardlinks would drop
1076 * overlay nlink to zero before all upper inodes are unlinked.
1077 * As a safety measure, when that situation is detected, set
1078 * the overlay nlink to the index inode nlink minus one for the
1079 * index entry itself.
1081 set_nlink(d_inode(dentry), inode->i_nlink - 1);
1082 ovl_set_nlink_upper(dentry);
1086 inode_lock_nested(dir, I_MUTEX_PARENT);
1087 index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
1088 err = PTR_ERR(index);
1089 if (IS_ERR(index)) {
1091 } else if (ovl_index_all(dentry->d_sb)) {
1092 /* Whiteout orphan index to block future open by handle */
1093 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
1096 /* Cleanup orphan index entries */
1097 err = ovl_cleanup(ofs, dir, index);
1106 ovl_drop_write(dentry);
1112 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
1117 * Operations that change overlay inode and upper inode nlink need to be
1118 * synchronized with copy up for persistent nlink accounting.
1120 int ovl_nlink_start(struct dentry *dentry)
1122 struct inode *inode = d_inode(dentry);
1123 const struct cred *old_cred;
1126 if (WARN_ON(!inode))
1130 * With inodes index is enabled, we store the union overlay nlink
1131 * in an xattr on the index inode. When whiting out an indexed lower,
1132 * we need to decrement the overlay persistent nlink, but before the
1133 * first copy up, we have no upper index inode to store the xattr.
1135 * As a workaround, before whiteout/rename over an indexed lower,
1136 * copy up to create the upper index. Creating the upper index will
1137 * initialize the overlay nlink, so it could be dropped if unlink
1138 * or rename succeeds.
1140 * TODO: implement metadata only index copy up when called with
1141 * ovl_copy_up_flags(dentry, O_PATH).
1143 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
1144 err = ovl_copy_up(dentry);
1149 err = ovl_inode_lock_interruptible(inode);
1153 err = ovl_want_write(dentry);
1157 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
1160 old_cred = ovl_override_creds(dentry->d_sb);
1162 * The overlay inode nlink should be incremented/decremented IFF the
1163 * upper operation succeeds, along with nlink change of upper inode.
1164 * Therefore, before link/unlink/rename, we store the union nlink
1165 * value relative to the upper inode nlink in an upper inode xattr.
1167 err = ovl_set_nlink_upper(dentry);
1168 revert_creds(old_cred);
1170 goto out_drop_write;
1175 ovl_drop_write(dentry);
1177 ovl_inode_unlock(inode);
1182 void ovl_nlink_end(struct dentry *dentry)
1184 struct inode *inode = d_inode(dentry);
1186 ovl_drop_write(dentry);
1188 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
1189 const struct cred *old_cred;
1191 old_cred = ovl_override_creds(dentry->d_sb);
1192 ovl_cleanup_index(dentry);
1193 revert_creds(old_cred);
1196 ovl_inode_unlock(inode);
1199 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
1201 struct dentry *trap;
1203 /* Workdir should not be the same as upperdir */
1204 if (workdir == upperdir)
1207 /* Workdir should not be subdir of upperdir and vice versa */
1208 trap = lock_rename(workdir, upperdir);
1217 unlock_rename(workdir, upperdir);
1219 pr_err("failed to lock workdir+upperdir\n");
1224 * err < 0, 0 if no metacopy xattr, metacopy data size if xattr found.
1225 * an empty xattr returns OVL_METACOPY_MIN_SIZE to distinguish from no xattr value.
1227 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
1228 struct ovl_metacopy *data)
1232 /* Only regular files can have metacopy xattr */
1233 if (!S_ISREG(d_inode(path->dentry)->i_mode))
1236 res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY,
1237 data, data ? OVL_METACOPY_MAX_SIZE : 0);
1239 if (res == -ENODATA || res == -EOPNOTSUPP)
1242 * getxattr on user.* may fail with EACCES in case there's no
1243 * read permission on the inode. Not much we can do, other than
1244 * tell the caller that this is not a metacopy inode.
1246 if (ofs->config.userxattr && res == -EACCES)
1252 /* Emulate empty data for zero size metacopy xattr */
1253 res = OVL_METACOPY_MIN_SIZE;
1255 memset(data, 0, res);
1258 } else if (res < OVL_METACOPY_MIN_SIZE) {
1259 pr_warn_ratelimited("metacopy file '%pd' has too small xattr\n",
1263 if (data->version != 0) {
1264 pr_warn_ratelimited("metacopy file '%pd' has unsupported version\n",
1268 if (res != data->len) {
1269 pr_warn_ratelimited("metacopy file '%pd' has invalid xattr size\n",
1277 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
1281 int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, struct ovl_metacopy *metacopy)
1283 size_t len = metacopy->len;
1285 /* If no flags or digest fall back to empty metacopy file */
1286 if (metacopy->version == 0 && metacopy->flags == 0 && metacopy->digest_algo == 0)
1289 return ovl_check_setxattr(ofs, d, OVL_XATTR_METACOPY,
1290 metacopy, len, -EOPNOTSUPP);
1293 bool ovl_is_metacopy_dentry(struct dentry *dentry)
1295 struct ovl_entry *oe = OVL_E(dentry);
1297 if (!d_is_reg(dentry))
1300 if (ovl_dentry_upper(dentry)) {
1301 if (!ovl_has_upperdata(d_inode(dentry)))
1306 return (ovl_numlower(oe) > 1);
1309 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
1312 char *s, *next, *buf = NULL;
1314 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
1315 if (res == -ENODATA || res == -EOPNOTSUPP)
1322 buf = kzalloc(res + padding + 1, GFP_KERNEL);
1324 return ERR_PTR(-ENOMEM);
1326 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
1332 if (buf[0] == '/') {
1333 for (s = buf; *s++ == '/'; s = next) {
1334 next = strchrnul(s, '/');
1339 if (strchr(buf, '/') != NULL)
1345 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
1349 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1352 return ERR_PTR(res);
1355 /* Call with mounter creds as it may open the file */
1356 int ovl_ensure_verity_loaded(struct path *datapath)
1358 struct inode *inode = d_inode(datapath->dentry);
1361 if (!fsverity_active(inode) && IS_VERITY(inode)) {
1363 * If this inode was not yet opened, the verity info hasn't been
1364 * loaded yet, so we need to do that here to force it into memory.
1366 filp = kernel_file_open(datapath, O_RDONLY, inode, current_cred());
1368 return PTR_ERR(filp);
1375 int ovl_validate_verity(struct ovl_fs *ofs,
1376 struct path *metapath,
1377 struct path *datapath)
1379 struct ovl_metacopy metacopy_data;
1380 u8 actual_digest[FS_VERITY_MAX_DIGEST_SIZE];
1381 int xattr_digest_size, digest_size;
1382 int xattr_size, err;
1385 if (!ofs->config.verity_mode ||
1386 /* Verity only works on regular files */
1387 !S_ISREG(d_inode(metapath->dentry)->i_mode))
1390 xattr_size = ovl_check_metacopy_xattr(ofs, metapath, &metacopy_data);
1394 if (!xattr_size || !metacopy_data.digest_algo) {
1395 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1396 pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n",
1403 xattr_digest_size = ovl_metadata_digest_size(&metacopy_data);
1405 err = ovl_ensure_verity_loaded(datapath);
1407 pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1412 digest_size = fsverity_get_digest(d_inode(datapath->dentry), actual_digest,
1413 &verity_algo, NULL);
1414 if (digest_size == 0) {
1415 pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", datapath->dentry);
1419 if (xattr_digest_size != digest_size ||
1420 metacopy_data.digest_algo != verity_algo ||
1421 memcmp(metacopy_data.digest, actual_digest, xattr_digest_size) != 0) {
1422 pr_warn_ratelimited("lower file '%pd' has the wrong fs-verity digest\n",
1430 int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src,
1431 struct ovl_metacopy *metacopy)
1433 int err, digest_size;
1435 if (!ofs->config.verity_mode || !S_ISREG(d_inode(src->dentry)->i_mode))
1438 err = ovl_ensure_verity_loaded(src);
1440 pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1445 digest_size = fsverity_get_digest(d_inode(src->dentry),
1446 metacopy->digest, &metacopy->digest_algo, NULL);
1447 if (digest_size == 0 ||
1448 WARN_ON_ONCE(digest_size > FS_VERITY_MAX_DIGEST_SIZE)) {
1449 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1450 pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n",
1457 metacopy->len += digest_size;
1462 * ovl_sync_status() - Check fs sync status for volatile mounts
1464 * Returns 1 if this is not a volatile mount and a real sync is required.
1466 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1467 * have occurred on the upperdir since the mount.
1469 * Returns -errno if it is a volatile mount, and the error that occurred since
1470 * the last mount. If the error code changes, it'll return the latest error
1474 int ovl_sync_status(struct ovl_fs *ofs)
1476 struct vfsmount *mnt;
1478 if (ovl_should_sync(ofs))
1481 mnt = ovl_upper_mnt(ofs);
1485 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1489 * ovl_copyattr() - copy inode attributes from layer to ovl inode
1491 * When overlay copies inode information from an upper or lower layer to the
1492 * relevant overlay inode it will apply the idmapping of the upper or lower
1493 * layer when doing so ensuring that the ovl inode ownership will correctly
1494 * reflect the ownership of the idmapped upper or lower layer. For example, an
1495 * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1496 * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1497 * helpers are nops when the relevant layer isn't idmapped.
1499 void ovl_copyattr(struct inode *inode)
1501 struct path realpath;
1502 struct inode *realinode;
1503 struct mnt_idmap *real_idmap;
1507 realinode = ovl_i_path_real(inode, &realpath);
1508 real_idmap = mnt_idmap(realpath.mnt);
1510 spin_lock(&inode->i_lock);
1511 vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1512 vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
1514 inode->i_uid = vfsuid_into_kuid(vfsuid);
1515 inode->i_gid = vfsgid_into_kgid(vfsgid);
1516 inode->i_mode = realinode->i_mode;
1517 inode_set_atime_to_ts(inode, inode_get_atime(realinode));
1518 inode_set_mtime_to_ts(inode, inode_get_mtime(realinode));
1519 inode_set_ctime_to_ts(inode, inode_get_ctime(realinode));
1520 i_size_write(inode, i_size_read(realinode));
1521 spin_unlock(&inode->i_lock);