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_path *ovl_stack_alloc(unsigned int n)
88 return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL);
91 void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
95 memcpy(dst, src, sizeof(struct ovl_path) * n);
96 for (i = 0; i < n; i++)
100 void ovl_stack_put(struct ovl_path *stack, unsigned int n)
104 for (i = 0; stack && i < n; i++)
105 dput(stack[i].dentry);
108 void ovl_stack_free(struct ovl_path *stack, unsigned int n)
110 ovl_stack_put(stack, n);
114 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
116 size_t size = offsetof(struct ovl_entry, __lowerstack[numlower]);
117 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
120 oe->__numlower = numlower;
125 void ovl_free_entry(struct ovl_entry *oe)
127 ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
131 #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
133 bool ovl_dentry_remote(struct dentry *dentry)
135 return dentry->d_flags & OVL_D_REVALIDATE;
138 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
140 if (!ovl_dentry_remote(realdentry))
143 spin_lock(&dentry->d_lock);
144 dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
145 spin_unlock(&dentry->d_lock);
148 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
149 struct ovl_entry *oe)
151 return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
154 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
155 struct ovl_entry *oe, unsigned int mask)
157 struct ovl_path *lowerstack = ovl_lowerstack(oe);
158 unsigned int i, flags = 0;
161 flags |= upperdentry->d_flags;
162 for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
163 flags |= lowerstack[i].dentry->d_flags;
165 spin_lock(&dentry->d_lock);
166 dentry->d_flags &= ~mask;
167 dentry->d_flags |= flags & mask;
168 spin_unlock(&dentry->d_lock);
171 bool ovl_dentry_weird(struct dentry *dentry)
173 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
174 DCACHE_MANAGE_TRANSIT |
179 enum ovl_path_type ovl_path_type(struct dentry *dentry)
181 struct ovl_entry *oe = OVL_E(dentry);
182 enum ovl_path_type type = 0;
184 if (ovl_dentry_upper(dentry)) {
185 type = __OVL_PATH_UPPER;
188 * Non-dir dentry can hold lower dentry of its copy up origin.
190 if (ovl_numlower(oe)) {
191 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
192 type |= __OVL_PATH_ORIGIN;
193 if (d_is_dir(dentry) ||
194 !ovl_has_upperdata(d_inode(dentry)))
195 type |= __OVL_PATH_MERGE;
198 if (ovl_numlower(oe) > 1)
199 type |= __OVL_PATH_MERGE;
204 void ovl_path_upper(struct dentry *dentry, struct path *path)
206 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
208 path->mnt = ovl_upper_mnt(ofs);
209 path->dentry = ovl_dentry_upper(dentry);
212 void ovl_path_lower(struct dentry *dentry, struct path *path)
214 struct ovl_entry *oe = OVL_E(dentry);
215 struct ovl_path *lowerpath = ovl_lowerstack(oe);
217 if (ovl_numlower(oe)) {
218 path->mnt = lowerpath->layer->mnt;
219 path->dentry = lowerpath->dentry;
221 *path = (struct path) { };
225 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
227 struct ovl_entry *oe = OVL_E(dentry);
228 struct ovl_path *lowerdata = ovl_lowerdata(oe);
229 struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
231 if (lowerdata_dentry) {
232 path->dentry = lowerdata_dentry;
234 * Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
235 * Make sure that if lowerdata->dentry is visible, then
236 * datapath->layer is visible as well.
239 path->mnt = READ_ONCE(lowerdata->layer)->mnt;
241 *path = (struct path) { };
245 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
247 enum ovl_path_type type = ovl_path_type(dentry);
249 if (!OVL_TYPE_UPPER(type))
250 ovl_path_lower(dentry, path);
252 ovl_path_upper(dentry, path);
257 enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
259 enum ovl_path_type type = ovl_path_type(dentry);
261 WARN_ON_ONCE(d_is_dir(dentry));
263 if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
264 ovl_path_lowerdata(dentry, path);
266 ovl_path_upper(dentry, path);
271 struct dentry *ovl_dentry_upper(struct dentry *dentry)
273 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
276 struct dentry *ovl_dentry_lower(struct dentry *dentry)
278 struct ovl_entry *oe = OVL_E(dentry);
280 return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
283 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
285 struct ovl_entry *oe = OVL_E(dentry);
287 return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
291 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
292 * depending on what is stored in lowerstack[0]. At times we need to find
293 * lower dentry which has data (and not metacopy dentry). This helper
294 * returns the lower data dentry.
296 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
298 return ovl_lowerdata_dentry(OVL_E(dentry));
301 int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
303 struct ovl_entry *oe = OVL_E(dentry);
304 struct ovl_path *lowerdata = ovl_lowerdata(oe);
305 struct dentry *datadentry = datapath->dentry;
307 if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
310 WRITE_ONCE(lowerdata->layer, datapath->layer);
312 * Pairs with smp_rmb() in ovl_path_lowerdata().
313 * Make sure that if lowerdata->dentry is visible, then
314 * lowerdata->layer is visible as well.
317 WRITE_ONCE(lowerdata->dentry, dget(datadentry));
319 ovl_dentry_update_reval(dentry, datadentry);
324 struct dentry *ovl_dentry_real(struct dentry *dentry)
326 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
329 struct dentry *ovl_i_dentry_upper(struct inode *inode)
331 return ovl_upperdentry_dereference(OVL_I(inode));
334 struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
336 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
338 path->dentry = ovl_i_dentry_upper(inode);
340 path->dentry = lowerpath->dentry;
341 path->mnt = lowerpath->layer->mnt;
343 path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
346 return path->dentry ? d_inode_rcu(path->dentry) : NULL;
349 struct inode *ovl_inode_upper(struct inode *inode)
351 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
353 return upperdentry ? d_inode(upperdentry) : NULL;
356 struct inode *ovl_inode_lower(struct inode *inode)
358 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
360 return lowerpath ? d_inode(lowerpath->dentry) : NULL;
363 struct inode *ovl_inode_real(struct inode *inode)
365 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
368 /* Return inode which contains lower data. Do not return metacopy */
369 struct inode *ovl_inode_lowerdata(struct inode *inode)
371 struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode));
373 if (WARN_ON(!S_ISREG(inode->i_mode)))
376 return lowerdata ? d_inode(lowerdata) : NULL;
379 /* Return real inode which contains data. Does not return metacopy inode */
380 struct inode *ovl_inode_realdata(struct inode *inode)
382 struct inode *upperinode;
384 upperinode = ovl_inode_upper(inode);
385 if (upperinode && ovl_has_upperdata(inode))
388 return ovl_inode_lowerdata(inode);
391 const char *ovl_lowerdata_redirect(struct inode *inode)
393 return inode && S_ISREG(inode->i_mode) ?
394 OVL_I(inode)->lowerdata_redirect : NULL;
397 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
399 return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL;
402 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
404 OVL_I(inode)->cache = cache;
407 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
409 set_bit(flag, OVL_E_FLAGS(dentry));
412 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
414 clear_bit(flag, OVL_E_FLAGS(dentry));
417 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
419 return test_bit(flag, OVL_E_FLAGS(dentry));
422 bool ovl_dentry_is_opaque(struct dentry *dentry)
424 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
427 bool ovl_dentry_is_whiteout(struct dentry *dentry)
429 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
432 void ovl_dentry_set_opaque(struct dentry *dentry)
434 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
438 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
439 * to return positive, while there's no actual upper alias for the inode.
440 * Copy up code needs to know about the existence of the upper alias, so it
441 * can't use ovl_dentry_upper().
443 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
445 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
448 void ovl_dentry_set_upper_alias(struct dentry *dentry)
450 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
453 static bool ovl_should_check_upperdata(struct inode *inode)
455 if (!S_ISREG(inode->i_mode))
458 if (!ovl_inode_lower(inode))
464 bool ovl_has_upperdata(struct inode *inode)
466 if (!ovl_should_check_upperdata(inode))
469 if (!ovl_test_flag(OVL_UPPERDATA, inode))
472 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
473 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
474 * if setting of OVL_UPPERDATA is visible, then effects of writes
475 * before that are visible too.
481 void ovl_set_upperdata(struct inode *inode)
484 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
485 * if OVL_UPPERDATA flag is visible, then effects of write operations
486 * before it are visible as well.
489 ovl_set_flag(OVL_UPPERDATA, inode);
492 /* Caller should hold ovl_inode->lock */
493 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
495 if (!ovl_open_flags_need_copy_up(flags))
498 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
501 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
503 if (!ovl_open_flags_need_copy_up(flags))
506 return !ovl_has_upperdata(d_inode(dentry));
509 const char *ovl_dentry_get_redirect(struct dentry *dentry)
511 return OVL_I(d_inode(dentry))->redirect;
514 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
516 struct ovl_inode *oi = OVL_I(d_inode(dentry));
519 oi->redirect = redirect;
522 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
524 struct inode *upperinode = d_inode(upperdentry);
526 WARN_ON(OVL_I(inode)->__upperdentry);
529 * Make sure upperdentry is consistent before making it visible
532 OVL_I(inode)->__upperdentry = upperdentry;
533 if (inode_unhashed(inode)) {
534 inode->i_private = upperinode;
535 __insert_inode_hash(inode, (unsigned long) upperinode);
539 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
541 struct inode *inode = d_inode(dentry);
543 WARN_ON(!inode_is_locked(inode));
544 WARN_ON(!d_is_dir(dentry));
546 * Version is used by readdir code to keep cache consistent.
547 * For merge dirs (or dirs with origin) all changes need to be noted.
548 * For non-merge dirs, cache contains only impure entries (i.e. ones
549 * which have been copied up and have origins), so only need to note
550 * changes to impure entries.
552 if (!ovl_dir_is_real(inode) || impurity)
553 OVL_I(inode)->version++;
556 void ovl_dir_modified(struct dentry *dentry, bool impurity)
558 /* Copy mtime/ctime */
559 ovl_copyattr(d_inode(dentry));
561 ovl_dir_version_inc(dentry, impurity);
564 u64 ovl_inode_version_get(struct inode *inode)
566 WARN_ON(!inode_is_locked(inode));
567 return OVL_I(inode)->version;
570 bool ovl_is_whiteout(struct dentry *dentry)
572 struct inode *inode = dentry->d_inode;
574 return inode && IS_WHITEOUT(inode);
577 struct file *ovl_path_open(const struct path *path, int flags)
579 struct inode *inode = d_inode(path->dentry);
580 struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
583 if (flags & ~(O_ACCMODE | O_LARGEFILE))
586 switch (flags & O_ACCMODE) {
591 acc_mode = MAY_WRITE;
597 err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
601 /* O_NOATIME is an optimization, don't fail if not permitted */
602 if (inode_owner_or_capable(real_idmap, inode))
605 return dentry_open(path, flags, current_cred());
608 /* Caller should hold ovl_inode->lock */
609 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
611 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
613 if (ovl_dentry_upper(dentry) &&
614 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
615 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
621 bool ovl_already_copied_up(struct dentry *dentry, int flags)
623 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
626 * Check if copy-up has happened as well as for upper alias (in
627 * case of hard links) is there.
629 * Both checks are lockless:
630 * - false negatives: will recheck under oi->lock
632 * + ovl_dentry_upper() uses memory barriers to ensure the
633 * upper dentry is up-to-date
634 * + ovl_dentry_has_upper_alias() relies on locking of
635 * upper parent i_rwsem to prevent reordering copy-up
638 if (ovl_dentry_upper(dentry) &&
639 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
640 !ovl_dentry_needs_data_copy_up(dentry, flags))
646 int ovl_copy_up_start(struct dentry *dentry, int flags)
648 struct inode *inode = d_inode(dentry);
651 err = ovl_inode_lock_interruptible(inode);
652 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
653 err = 1; /* Already copied up */
654 ovl_inode_unlock(inode);
660 void ovl_copy_up_end(struct dentry *dentry)
662 ovl_inode_unlock(d_inode(dentry));
665 bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
669 res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
671 /* Zero size value means "copied up but origin unknown" */
678 bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
684 if (!d_is_dir(path->dentry))
687 res = ovl_path_getxattr(ofs, path, ox, &val, 1);
688 if (res == 1 && val == 'y')
694 #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
695 #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
696 #define OVL_XATTR_ORIGIN_POSTFIX "origin"
697 #define OVL_XATTR_IMPURE_POSTFIX "impure"
698 #define OVL_XATTR_NLINK_POSTFIX "nlink"
699 #define OVL_XATTR_UPPER_POSTFIX "upper"
700 #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
701 #define OVL_XATTR_PROTATTR_POSTFIX "protattr"
703 #define OVL_XATTR_TAB_ENTRY(x) \
704 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
705 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
707 const char *const ovl_xattr_table[][2] = {
708 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
709 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
710 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
711 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
712 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
713 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
714 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
715 OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
718 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
719 enum ovl_xattr ox, const void *value, size_t size,
727 err = ovl_setxattr(ofs, upperdentry, ox, value, size);
729 if (err == -EOPNOTSUPP) {
730 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
738 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
740 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
743 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
747 * Do not fail when upper doesn't support xattrs.
748 * Upper inodes won't have origin nor redirect xattr anyway.
750 err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
752 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
758 #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
760 void ovl_check_protattr(struct inode *inode, struct dentry *upper)
762 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
763 u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
764 char buf[OVL_PROTATTR_MAX+1];
767 res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
773 * Initialize inode flags from overlay.protattr xattr and upper inode
774 * flags. If upper inode has those fileattr flags set (i.e. from old
775 * kernel), we do not clear them on ovl_get_inode(), but we will clear
776 * them on next fileattr_set().
778 for (n = 0; n < res; n++) {
781 else if (buf[n] == 'i')
782 iflags |= S_IMMUTABLE;
787 if (!res || n < res) {
788 pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
791 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
795 int ovl_set_protattr(struct inode *inode, struct dentry *upper,
798 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
799 char buf[OVL_PROTATTR_MAX];
800 int len = 0, err = 0;
803 BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
805 if (fa->flags & FS_APPEND_FL) {
809 if (fa->flags & FS_IMMUTABLE_FL) {
811 iflags |= S_IMMUTABLE;
815 * Do not allow to set protection flags when upper doesn't support
816 * xattrs, because we do not set those fileattr flags on upper inode.
817 * Remove xattr if it exist and all protection flags are cleared.
820 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
822 } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
823 err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
824 if (err == -EOPNOTSUPP || err == -ENODATA)
830 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
832 /* Mask out the fileattr flags that should not be set in upper inode */
833 fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
834 fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
840 * Caller must hold a reference to inode to prevent it from being freed while
841 * it is marked inuse.
843 bool ovl_inuse_trylock(struct dentry *dentry)
845 struct inode *inode = d_inode(dentry);
848 spin_lock(&inode->i_lock);
849 if (!(inode->i_state & I_OVL_INUSE)) {
850 inode->i_state |= I_OVL_INUSE;
853 spin_unlock(&inode->i_lock);
858 void ovl_inuse_unlock(struct dentry *dentry)
861 struct inode *inode = d_inode(dentry);
863 spin_lock(&inode->i_lock);
864 WARN_ON(!(inode->i_state & I_OVL_INUSE));
865 inode->i_state &= ~I_OVL_INUSE;
866 spin_unlock(&inode->i_lock);
870 bool ovl_is_inuse(struct dentry *dentry)
872 struct inode *inode = d_inode(dentry);
875 spin_lock(&inode->i_lock);
876 inuse = (inode->i_state & I_OVL_INUSE);
877 spin_unlock(&inode->i_lock);
883 * Does this overlay dentry need to be indexed on copy up?
885 bool ovl_need_index(struct dentry *dentry)
887 struct dentry *lower = ovl_dentry_lower(dentry);
889 if (!lower || !ovl_indexdir(dentry->d_sb))
892 /* Index all files for NFS export and consistency verification */
893 if (ovl_index_all(dentry->d_sb))
896 /* Index only lower hardlinks on copy up */
897 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
903 /* Caller must hold OVL_I(inode)->lock */
904 static void ovl_cleanup_index(struct dentry *dentry)
906 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
907 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
908 struct inode *dir = indexdir->d_inode;
909 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
910 struct dentry *upperdentry = ovl_dentry_upper(dentry);
911 struct dentry *index = NULL;
913 struct qstr name = { };
916 err = ovl_get_index_name(ofs, lowerdentry, &name);
920 inode = d_inode(upperdentry);
921 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
922 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
923 upperdentry, inode->i_ino, inode->i_nlink);
925 * We either have a bug with persistent union nlink or a lower
926 * hardlink was added while overlay is mounted. Adding a lower
927 * hardlink and then unlinking all overlay hardlinks would drop
928 * overlay nlink to zero before all upper inodes are unlinked.
929 * As a safety measure, when that situation is detected, set
930 * the overlay nlink to the index inode nlink minus one for the
931 * index entry itself.
933 set_nlink(d_inode(dentry), inode->i_nlink - 1);
934 ovl_set_nlink_upper(dentry);
938 inode_lock_nested(dir, I_MUTEX_PARENT);
939 index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
940 err = PTR_ERR(index);
943 } else if (ovl_index_all(dentry->d_sb)) {
944 /* Whiteout orphan index to block future open by handle */
945 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
948 /* Cleanup orphan index entries */
949 err = ovl_cleanup(ofs, dir, index);
962 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
967 * Operations that change overlay inode and upper inode nlink need to be
968 * synchronized with copy up for persistent nlink accounting.
970 int ovl_nlink_start(struct dentry *dentry)
972 struct inode *inode = d_inode(dentry);
973 const struct cred *old_cred;
980 * With inodes index is enabled, we store the union overlay nlink
981 * in an xattr on the index inode. When whiting out an indexed lower,
982 * we need to decrement the overlay persistent nlink, but before the
983 * first copy up, we have no upper index inode to store the xattr.
985 * As a workaround, before whiteout/rename over an indexed lower,
986 * copy up to create the upper index. Creating the upper index will
987 * initialize the overlay nlink, so it could be dropped if unlink
988 * or rename succeeds.
990 * TODO: implement metadata only index copy up when called with
991 * ovl_copy_up_flags(dentry, O_PATH).
993 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
994 err = ovl_copy_up(dentry);
999 err = ovl_inode_lock_interruptible(inode);
1003 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
1006 old_cred = ovl_override_creds(dentry->d_sb);
1008 * The overlay inode nlink should be incremented/decremented IFF the
1009 * upper operation succeeds, along with nlink change of upper inode.
1010 * Therefore, before link/unlink/rename, we store the union nlink
1011 * value relative to the upper inode nlink in an upper inode xattr.
1013 err = ovl_set_nlink_upper(dentry);
1014 revert_creds(old_cred);
1018 ovl_inode_unlock(inode);
1023 void ovl_nlink_end(struct dentry *dentry)
1025 struct inode *inode = d_inode(dentry);
1027 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
1028 const struct cred *old_cred;
1030 old_cred = ovl_override_creds(dentry->d_sb);
1031 ovl_cleanup_index(dentry);
1032 revert_creds(old_cred);
1035 ovl_inode_unlock(inode);
1038 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
1040 /* Workdir should not be the same as upperdir */
1041 if (workdir == upperdir)
1044 /* Workdir should not be subdir of upperdir and vice versa */
1045 if (lock_rename(workdir, upperdir) != NULL)
1051 unlock_rename(workdir, upperdir);
1053 pr_err("failed to lock workdir+upperdir\n");
1057 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
1058 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path)
1062 /* Only regular files can have metacopy xattr */
1063 if (!S_ISREG(d_inode(path->dentry)->i_mode))
1066 res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0);
1068 if (res == -ENODATA || res == -EOPNOTSUPP)
1071 * getxattr on user.* may fail with EACCES in case there's no
1072 * read permission on the inode. Not much we can do, other than
1073 * tell the caller that this is not a metacopy inode.
1075 if (ofs->config.userxattr && res == -EACCES)
1082 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
1086 bool ovl_is_metacopy_dentry(struct dentry *dentry)
1088 struct ovl_entry *oe = OVL_E(dentry);
1090 if (!d_is_reg(dentry))
1093 if (ovl_dentry_upper(dentry)) {
1094 if (!ovl_has_upperdata(d_inode(dentry)))
1099 return (ovl_numlower(oe) > 1);
1102 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
1105 char *s, *next, *buf = NULL;
1107 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
1108 if (res == -ENODATA || res == -EOPNOTSUPP)
1115 buf = kzalloc(res + padding + 1, GFP_KERNEL);
1117 return ERR_PTR(-ENOMEM);
1119 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
1125 if (buf[0] == '/') {
1126 for (s = buf; *s++ == '/'; s = next) {
1127 next = strchrnul(s, '/');
1132 if (strchr(buf, '/') != NULL)
1138 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
1142 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1145 return ERR_PTR(res);
1149 * ovl_sync_status() - Check fs sync status for volatile mounts
1151 * Returns 1 if this is not a volatile mount and a real sync is required.
1153 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1154 * have occurred on the upperdir since the mount.
1156 * Returns -errno if it is a volatile mount, and the error that occurred since
1157 * the last mount. If the error code changes, it'll return the latest error
1161 int ovl_sync_status(struct ovl_fs *ofs)
1163 struct vfsmount *mnt;
1165 if (ovl_should_sync(ofs))
1168 mnt = ovl_upper_mnt(ofs);
1172 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1176 * ovl_copyattr() - copy inode attributes from layer to ovl inode
1178 * When overlay copies inode information from an upper or lower layer to the
1179 * relevant overlay inode it will apply the idmapping of the upper or lower
1180 * layer when doing so ensuring that the ovl inode ownership will correctly
1181 * reflect the ownership of the idmapped upper or lower layer. For example, an
1182 * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1183 * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1184 * helpers are nops when the relevant layer isn't idmapped.
1186 void ovl_copyattr(struct inode *inode)
1188 struct path realpath;
1189 struct inode *realinode;
1190 struct mnt_idmap *real_idmap;
1194 realinode = ovl_i_path_real(inode, &realpath);
1195 real_idmap = mnt_idmap(realpath.mnt);
1197 vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1198 vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
1200 inode->i_uid = vfsuid_into_kuid(vfsuid);
1201 inode->i_gid = vfsgid_into_kgid(vfsgid);
1202 inode->i_mode = realinode->i_mode;
1203 inode->i_atime = realinode->i_atime;
1204 inode->i_mtime = realinode->i_mtime;
1205 inode->i_ctime = realinode->i_ctime;
1206 i_size_write(inode, i_size_read(realinode));