1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
8 #include <linux/mount.h>
9 #include <linux/slab.h>
10 #include <linux/cred.h>
11 #include <linux/xattr.h>
12 #include <linux/exportfs.h>
13 #include <linux/uuid.h>
14 #include <linux/namei.h>
15 #include <linux/ratelimit.h>
16 #include "overlayfs.h"
18 int ovl_want_write(struct dentry
*dentry
)
20 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
21 return mnt_want_write(ovl_upper_mnt(ofs
));
24 void ovl_drop_write(struct dentry
*dentry
)
26 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
27 mnt_drop_write(ovl_upper_mnt(ofs
));
30 struct dentry
*ovl_workdir(struct dentry
*dentry
)
32 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
36 const struct cred
*ovl_override_creds(struct super_block
*sb
)
38 struct ovl_fs
*ofs
= sb
->s_fs_info
;
40 return override_creds(ofs
->creator_cred
);
44 * Check if underlying fs supports file handles and try to determine encoding
45 * type, in order to deduce maximum inode number used by fs.
47 * Return 0 if file handles are not supported.
48 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
49 * Return -1 if fs uses a non default encoding with unknown inode size.
51 int ovl_can_decode_fh(struct super_block
*sb
)
53 if (!capable(CAP_DAC_READ_SEARCH
))
56 if (!sb
->s_export_op
|| !sb
->s_export_op
->fh_to_dentry
)
59 return sb
->s_export_op
->encode_fh
? -1 : FILEID_INO32_GEN
;
62 struct dentry
*ovl_indexdir(struct super_block
*sb
)
64 struct ovl_fs
*ofs
= sb
->s_fs_info
;
69 /* Index all files on copy up. For now only enabled for NFS export */
70 bool ovl_index_all(struct super_block
*sb
)
72 struct ovl_fs
*ofs
= sb
->s_fs_info
;
74 return ofs
->config
.nfs_export
&& ofs
->config
.index
;
77 /* Verify lower origin on lookup. For now only enabled for NFS export */
78 bool ovl_verify_lower(struct super_block
*sb
)
80 struct ovl_fs
*ofs
= sb
->s_fs_info
;
82 return ofs
->config
.nfs_export
&& ofs
->config
.index
;
85 struct ovl_entry
*ovl_alloc_entry(unsigned int numlower
)
87 size_t size
= offsetof(struct ovl_entry
, lowerstack
[numlower
]);
88 struct ovl_entry
*oe
= kzalloc(size
, GFP_KERNEL
);
91 oe
->numlower
= numlower
;
96 bool ovl_dentry_remote(struct dentry
*dentry
)
98 return dentry
->d_flags
&
99 (DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
);
102 void ovl_dentry_update_reval(struct dentry
*dentry
, struct dentry
*upperdentry
,
105 struct ovl_entry
*oe
= OVL_E(dentry
);
106 unsigned int i
, flags
= 0;
109 flags
|= upperdentry
->d_flags
;
110 for (i
= 0; i
< oe
->numlower
; i
++)
111 flags
|= oe
->lowerstack
[i
].dentry
->d_flags
;
113 spin_lock(&dentry
->d_lock
);
114 dentry
->d_flags
&= ~mask
;
115 dentry
->d_flags
|= flags
& mask
;
116 spin_unlock(&dentry
->d_lock
);
119 bool ovl_dentry_weird(struct dentry
*dentry
)
121 return dentry
->d_flags
& (DCACHE_NEED_AUTOMOUNT
|
122 DCACHE_MANAGE_TRANSIT
|
127 enum ovl_path_type
ovl_path_type(struct dentry
*dentry
)
129 struct ovl_entry
*oe
= dentry
->d_fsdata
;
130 enum ovl_path_type type
= 0;
132 if (ovl_dentry_upper(dentry
)) {
133 type
= __OVL_PATH_UPPER
;
136 * Non-dir dentry can hold lower dentry of its copy up origin.
139 if (ovl_test_flag(OVL_CONST_INO
, d_inode(dentry
)))
140 type
|= __OVL_PATH_ORIGIN
;
141 if (d_is_dir(dentry
) ||
142 !ovl_has_upperdata(d_inode(dentry
)))
143 type
|= __OVL_PATH_MERGE
;
146 if (oe
->numlower
> 1)
147 type
|= __OVL_PATH_MERGE
;
152 void ovl_path_upper(struct dentry
*dentry
, struct path
*path
)
154 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
156 path
->mnt
= ovl_upper_mnt(ofs
);
157 path
->dentry
= ovl_dentry_upper(dentry
);
160 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
162 struct ovl_entry
*oe
= dentry
->d_fsdata
;
165 path
->mnt
= oe
->lowerstack
[0].layer
->mnt
;
166 path
->dentry
= oe
->lowerstack
[0].dentry
;
168 *path
= (struct path
) { };
172 void ovl_path_lowerdata(struct dentry
*dentry
, struct path
*path
)
174 struct ovl_entry
*oe
= dentry
->d_fsdata
;
177 path
->mnt
= oe
->lowerstack
[oe
->numlower
- 1].layer
->mnt
;
178 path
->dentry
= oe
->lowerstack
[oe
->numlower
- 1].dentry
;
180 *path
= (struct path
) { };
184 enum ovl_path_type
ovl_path_real(struct dentry
*dentry
, struct path
*path
)
186 enum ovl_path_type type
= ovl_path_type(dentry
);
188 if (!OVL_TYPE_UPPER(type
))
189 ovl_path_lower(dentry
, path
);
191 ovl_path_upper(dentry
, path
);
196 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
198 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry
)));
201 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
203 struct ovl_entry
*oe
= dentry
->d_fsdata
;
205 return oe
->numlower
? oe
->lowerstack
[0].dentry
: NULL
;
208 const struct ovl_layer
*ovl_layer_lower(struct dentry
*dentry
)
210 struct ovl_entry
*oe
= dentry
->d_fsdata
;
212 return oe
->numlower
? oe
->lowerstack
[0].layer
: NULL
;
216 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
217 * dependig on what is stored in lowerstack[0]. At times we need to find
218 * lower dentry which has data (and not metacopy dentry). This helper
219 * returns the lower data dentry.
221 struct dentry
*ovl_dentry_lowerdata(struct dentry
*dentry
)
223 struct ovl_entry
*oe
= dentry
->d_fsdata
;
225 return oe
->numlower
? oe
->lowerstack
[oe
->numlower
- 1].dentry
: NULL
;
228 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
230 return ovl_dentry_upper(dentry
) ?: ovl_dentry_lower(dentry
);
233 struct dentry
*ovl_i_dentry_upper(struct inode
*inode
)
235 return ovl_upperdentry_dereference(OVL_I(inode
));
238 struct inode
*ovl_inode_upper(struct inode
*inode
)
240 struct dentry
*upperdentry
= ovl_i_dentry_upper(inode
);
242 return upperdentry
? d_inode(upperdentry
) : NULL
;
245 struct inode
*ovl_inode_lower(struct inode
*inode
)
247 return OVL_I(inode
)->lower
;
250 struct inode
*ovl_inode_real(struct inode
*inode
)
252 return ovl_inode_upper(inode
) ?: ovl_inode_lower(inode
);
255 /* Return inode which contains lower data. Do not return metacopy */
256 struct inode
*ovl_inode_lowerdata(struct inode
*inode
)
258 if (WARN_ON(!S_ISREG(inode
->i_mode
)))
261 return OVL_I(inode
)->lowerdata
?: ovl_inode_lower(inode
);
264 /* Return real inode which contains data. Does not return metacopy inode */
265 struct inode
*ovl_inode_realdata(struct inode
*inode
)
267 struct inode
*upperinode
;
269 upperinode
= ovl_inode_upper(inode
);
270 if (upperinode
&& ovl_has_upperdata(inode
))
273 return ovl_inode_lowerdata(inode
);
276 struct ovl_dir_cache
*ovl_dir_cache(struct inode
*inode
)
278 return OVL_I(inode
)->cache
;
281 void ovl_set_dir_cache(struct inode
*inode
, struct ovl_dir_cache
*cache
)
283 OVL_I(inode
)->cache
= cache
;
286 void ovl_dentry_set_flag(unsigned long flag
, struct dentry
*dentry
)
288 set_bit(flag
, &OVL_E(dentry
)->flags
);
291 void ovl_dentry_clear_flag(unsigned long flag
, struct dentry
*dentry
)
293 clear_bit(flag
, &OVL_E(dentry
)->flags
);
296 bool ovl_dentry_test_flag(unsigned long flag
, struct dentry
*dentry
)
298 return test_bit(flag
, &OVL_E(dentry
)->flags
);
301 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
303 return ovl_dentry_test_flag(OVL_E_OPAQUE
, dentry
);
306 bool ovl_dentry_is_whiteout(struct dentry
*dentry
)
308 return !dentry
->d_inode
&& ovl_dentry_is_opaque(dentry
);
311 void ovl_dentry_set_opaque(struct dentry
*dentry
)
313 ovl_dentry_set_flag(OVL_E_OPAQUE
, dentry
);
317 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
318 * to return positive, while there's no actual upper alias for the inode.
319 * Copy up code needs to know about the existence of the upper alias, so it
320 * can't use ovl_dentry_upper().
322 bool ovl_dentry_has_upper_alias(struct dentry
*dentry
)
324 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS
, dentry
);
327 void ovl_dentry_set_upper_alias(struct dentry
*dentry
)
329 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS
, dentry
);
332 static bool ovl_should_check_upperdata(struct inode
*inode
)
334 if (!S_ISREG(inode
->i_mode
))
337 if (!ovl_inode_lower(inode
))
343 bool ovl_has_upperdata(struct inode
*inode
)
345 if (!ovl_should_check_upperdata(inode
))
348 if (!ovl_test_flag(OVL_UPPERDATA
, inode
))
351 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
352 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
353 * if setting of OVL_UPPERDATA is visible, then effects of writes
354 * before that are visible too.
360 void ovl_set_upperdata(struct inode
*inode
)
363 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
364 * if OVL_UPPERDATA flag is visible, then effects of write operations
365 * before it are visible as well.
368 ovl_set_flag(OVL_UPPERDATA
, inode
);
371 /* Caller should hold ovl_inode->lock */
372 bool ovl_dentry_needs_data_copy_up_locked(struct dentry
*dentry
, int flags
)
374 if (!ovl_open_flags_need_copy_up(flags
))
377 return !ovl_test_flag(OVL_UPPERDATA
, d_inode(dentry
));
380 bool ovl_dentry_needs_data_copy_up(struct dentry
*dentry
, int flags
)
382 if (!ovl_open_flags_need_copy_up(flags
))
385 return !ovl_has_upperdata(d_inode(dentry
));
388 bool ovl_redirect_dir(struct super_block
*sb
)
390 struct ovl_fs
*ofs
= sb
->s_fs_info
;
392 return ofs
->config
.redirect_dir
&& !ofs
->noxattr
;
395 const char *ovl_dentry_get_redirect(struct dentry
*dentry
)
397 return OVL_I(d_inode(dentry
))->redirect
;
400 void ovl_dentry_set_redirect(struct dentry
*dentry
, const char *redirect
)
402 struct ovl_inode
*oi
= OVL_I(d_inode(dentry
));
405 oi
->redirect
= redirect
;
408 void ovl_inode_update(struct inode
*inode
, struct dentry
*upperdentry
)
410 struct inode
*upperinode
= d_inode(upperdentry
);
412 WARN_ON(OVL_I(inode
)->__upperdentry
);
415 * Make sure upperdentry is consistent before making it visible
418 OVL_I(inode
)->__upperdentry
= upperdentry
;
419 if (inode_unhashed(inode
)) {
420 inode
->i_private
= upperinode
;
421 __insert_inode_hash(inode
, (unsigned long) upperinode
);
425 static void ovl_dentry_version_inc(struct dentry
*dentry
, bool impurity
)
427 struct inode
*inode
= d_inode(dentry
);
429 WARN_ON(!inode_is_locked(inode
));
431 * Version is used by readdir code to keep cache consistent. For merge
432 * dirs all changes need to be noted. For non-merge dirs, cache only
433 * contains impure (ones which have been copied up and have origins)
434 * entries, so only need to note changes to impure entries.
436 if (OVL_TYPE_MERGE(ovl_path_type(dentry
)) || impurity
)
437 OVL_I(inode
)->version
++;
440 void ovl_dir_modified(struct dentry
*dentry
, bool impurity
)
442 /* Copy mtime/ctime */
443 ovl_copyattr(d_inode(ovl_dentry_upper(dentry
)), d_inode(dentry
));
445 ovl_dentry_version_inc(dentry
, impurity
);
448 u64
ovl_dentry_version_get(struct dentry
*dentry
)
450 struct inode
*inode
= d_inode(dentry
);
452 WARN_ON(!inode_is_locked(inode
));
453 return OVL_I(inode
)->version
;
456 bool ovl_is_whiteout(struct dentry
*dentry
)
458 struct inode
*inode
= dentry
->d_inode
;
460 return inode
&& IS_WHITEOUT(inode
);
463 struct file
*ovl_path_open(struct path
*path
, int flags
)
465 struct inode
*inode
= d_inode(path
->dentry
);
468 if (flags
& ~(O_ACCMODE
| O_LARGEFILE
))
471 switch (flags
& O_ACCMODE
) {
476 acc_mode
= MAY_WRITE
;
482 err
= inode_permission(inode
, acc_mode
| MAY_OPEN
);
486 /* O_NOATIME is an optimization, don't fail if not permitted */
487 if (inode_owner_or_capable(inode
))
490 return dentry_open(path
, flags
, current_cred());
493 /* Caller should hold ovl_inode->lock */
494 static bool ovl_already_copied_up_locked(struct dentry
*dentry
, int flags
)
496 bool disconnected
= dentry
->d_flags
& DCACHE_DISCONNECTED
;
498 if (ovl_dentry_upper(dentry
) &&
499 (ovl_dentry_has_upper_alias(dentry
) || disconnected
) &&
500 !ovl_dentry_needs_data_copy_up_locked(dentry
, flags
))
506 bool ovl_already_copied_up(struct dentry
*dentry
, int flags
)
508 bool disconnected
= dentry
->d_flags
& DCACHE_DISCONNECTED
;
511 * Check if copy-up has happened as well as for upper alias (in
512 * case of hard links) is there.
514 * Both checks are lockless:
515 * - false negatives: will recheck under oi->lock
517 * + ovl_dentry_upper() uses memory barriers to ensure the
518 * upper dentry is up-to-date
519 * + ovl_dentry_has_upper_alias() relies on locking of
520 * upper parent i_rwsem to prevent reordering copy-up
523 if (ovl_dentry_upper(dentry
) &&
524 (ovl_dentry_has_upper_alias(dentry
) || disconnected
) &&
525 !ovl_dentry_needs_data_copy_up(dentry
, flags
))
531 int ovl_copy_up_start(struct dentry
*dentry
, int flags
)
533 struct inode
*inode
= d_inode(dentry
);
536 err
= ovl_inode_lock_interruptible(inode
);
537 if (!err
&& ovl_already_copied_up_locked(dentry
, flags
)) {
538 err
= 1; /* Already copied up */
539 ovl_inode_unlock(inode
);
545 void ovl_copy_up_end(struct dentry
*dentry
)
547 ovl_inode_unlock(d_inode(dentry
));
550 bool ovl_check_origin_xattr(struct ovl_fs
*ofs
, struct dentry
*dentry
)
554 res
= ovl_do_getxattr(ofs
, dentry
, OVL_XATTR_ORIGIN
, NULL
, 0);
556 /* Zero size value means "copied up but origin unknown" */
563 bool ovl_check_dir_xattr(struct super_block
*sb
, struct dentry
*dentry
,
569 if (!d_is_dir(dentry
))
572 res
= ovl_do_getxattr(OVL_FS(sb
), dentry
, ox
, &val
, 1);
573 if (res
== 1 && val
== 'y')
579 #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
580 #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
581 #define OVL_XATTR_ORIGIN_POSTFIX "origin"
582 #define OVL_XATTR_IMPURE_POSTFIX "impure"
583 #define OVL_XATTR_NLINK_POSTFIX "nlink"
584 #define OVL_XATTR_UPPER_POSTFIX "upper"
585 #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
587 #define OVL_XATTR_TAB_ENTRY(x) \
588 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
589 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
591 const char *const ovl_xattr_table
[][2] = {
592 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE
),
593 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT
),
594 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN
),
595 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE
),
596 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK
),
597 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER
),
598 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY
),
601 int ovl_check_setxattr(struct dentry
*dentry
, struct dentry
*upperdentry
,
602 enum ovl_xattr ox
, const void *value
, size_t size
,
606 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
611 err
= ovl_do_setxattr(ofs
, upperdentry
, ox
, value
, size
);
613 if (err
== -EOPNOTSUPP
) {
614 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs
, ox
));
622 int ovl_set_impure(struct dentry
*dentry
, struct dentry
*upperdentry
)
626 if (ovl_test_flag(OVL_IMPURE
, d_inode(dentry
)))
630 * Do not fail when upper doesn't support xattrs.
631 * Upper inodes won't have origin nor redirect xattr anyway.
633 err
= ovl_check_setxattr(dentry
, upperdentry
, OVL_XATTR_IMPURE
,
636 ovl_set_flag(OVL_IMPURE
, d_inode(dentry
));
641 void ovl_set_flag(unsigned long flag
, struct inode
*inode
)
643 set_bit(flag
, &OVL_I(inode
)->flags
);
646 void ovl_clear_flag(unsigned long flag
, struct inode
*inode
)
648 clear_bit(flag
, &OVL_I(inode
)->flags
);
651 bool ovl_test_flag(unsigned long flag
, struct inode
*inode
)
653 return test_bit(flag
, &OVL_I(inode
)->flags
);
657 * Caller must hold a reference to inode to prevent it from being freed while
658 * it is marked inuse.
660 bool ovl_inuse_trylock(struct dentry
*dentry
)
662 struct inode
*inode
= d_inode(dentry
);
665 spin_lock(&inode
->i_lock
);
666 if (!(inode
->i_state
& I_OVL_INUSE
)) {
667 inode
->i_state
|= I_OVL_INUSE
;
670 spin_unlock(&inode
->i_lock
);
675 void ovl_inuse_unlock(struct dentry
*dentry
)
678 struct inode
*inode
= d_inode(dentry
);
680 spin_lock(&inode
->i_lock
);
681 WARN_ON(!(inode
->i_state
& I_OVL_INUSE
));
682 inode
->i_state
&= ~I_OVL_INUSE
;
683 spin_unlock(&inode
->i_lock
);
687 bool ovl_is_inuse(struct dentry
*dentry
)
689 struct inode
*inode
= d_inode(dentry
);
692 spin_lock(&inode
->i_lock
);
693 inuse
= (inode
->i_state
& I_OVL_INUSE
);
694 spin_unlock(&inode
->i_lock
);
700 * Does this overlay dentry need to be indexed on copy up?
702 bool ovl_need_index(struct dentry
*dentry
)
704 struct dentry
*lower
= ovl_dentry_lower(dentry
);
706 if (!lower
|| !ovl_indexdir(dentry
->d_sb
))
709 /* Index all files for NFS export and consistency verification */
710 if (ovl_index_all(dentry
->d_sb
))
713 /* Index only lower hardlinks on copy up */
714 if (!d_is_dir(lower
) && d_inode(lower
)->i_nlink
> 1)
720 /* Caller must hold OVL_I(inode)->lock */
721 static void ovl_cleanup_index(struct dentry
*dentry
)
723 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
724 struct dentry
*indexdir
= ovl_indexdir(dentry
->d_sb
);
725 struct inode
*dir
= indexdir
->d_inode
;
726 struct dentry
*lowerdentry
= ovl_dentry_lower(dentry
);
727 struct dentry
*upperdentry
= ovl_dentry_upper(dentry
);
728 struct dentry
*index
= NULL
;
730 struct qstr name
= { };
733 err
= ovl_get_index_name(ofs
, lowerdentry
, &name
);
737 inode
= d_inode(upperdentry
);
738 if (!S_ISDIR(inode
->i_mode
) && inode
->i_nlink
!= 1) {
739 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
740 upperdentry
, inode
->i_ino
, inode
->i_nlink
);
742 * We either have a bug with persistent union nlink or a lower
743 * hardlink was added while overlay is mounted. Adding a lower
744 * hardlink and then unlinking all overlay hardlinks would drop
745 * overlay nlink to zero before all upper inodes are unlinked.
746 * As a safety measure, when that situation is detected, set
747 * the overlay nlink to the index inode nlink minus one for the
748 * index entry itself.
750 set_nlink(d_inode(dentry
), inode
->i_nlink
- 1);
751 ovl_set_nlink_upper(dentry
);
755 inode_lock_nested(dir
, I_MUTEX_PARENT
);
756 index
= lookup_one_len(name
.name
, indexdir
, name
.len
);
757 err
= PTR_ERR(index
);
760 } else if (ovl_index_all(dentry
->d_sb
)) {
761 /* Whiteout orphan index to block future open by handle */
762 err
= ovl_cleanup_and_whiteout(OVL_FS(dentry
->d_sb
),
765 /* Cleanup orphan index entries */
766 err
= ovl_cleanup(dir
, index
);
779 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry
, err
);
784 * Operations that change overlay inode and upper inode nlink need to be
785 * synchronized with copy up for persistent nlink accounting.
787 int ovl_nlink_start(struct dentry
*dentry
)
789 struct inode
*inode
= d_inode(dentry
);
790 const struct cred
*old_cred
;
797 * With inodes index is enabled, we store the union overlay nlink
798 * in an xattr on the index inode. When whiting out an indexed lower,
799 * we need to decrement the overlay persistent nlink, but before the
800 * first copy up, we have no upper index inode to store the xattr.
802 * As a workaround, before whiteout/rename over an indexed lower,
803 * copy up to create the upper index. Creating the upper index will
804 * initialize the overlay nlink, so it could be dropped if unlink
805 * or rename succeeds.
807 * TODO: implement metadata only index copy up when called with
808 * ovl_copy_up_flags(dentry, O_PATH).
810 if (ovl_need_index(dentry
) && !ovl_dentry_has_upper_alias(dentry
)) {
811 err
= ovl_copy_up(dentry
);
816 err
= ovl_inode_lock_interruptible(inode
);
820 if (d_is_dir(dentry
) || !ovl_test_flag(OVL_INDEX
, inode
))
823 old_cred
= ovl_override_creds(dentry
->d_sb
);
825 * The overlay inode nlink should be incremented/decremented IFF the
826 * upper operation succeeds, along with nlink change of upper inode.
827 * Therefore, before link/unlink/rename, we store the union nlink
828 * value relative to the upper inode nlink in an upper inode xattr.
830 err
= ovl_set_nlink_upper(dentry
);
831 revert_creds(old_cred
);
835 ovl_inode_unlock(inode
);
840 void ovl_nlink_end(struct dentry
*dentry
)
842 struct inode
*inode
= d_inode(dentry
);
844 if (ovl_test_flag(OVL_INDEX
, inode
) && inode
->i_nlink
== 0) {
845 const struct cred
*old_cred
;
847 old_cred
= ovl_override_creds(dentry
->d_sb
);
848 ovl_cleanup_index(dentry
);
849 revert_creds(old_cred
);
852 ovl_inode_unlock(inode
);
855 int ovl_lock_rename_workdir(struct dentry
*workdir
, struct dentry
*upperdir
)
857 /* Workdir should not be the same as upperdir */
858 if (workdir
== upperdir
)
861 /* Workdir should not be subdir of upperdir and vice versa */
862 if (lock_rename(workdir
, upperdir
) != NULL
)
868 unlock_rename(workdir
, upperdir
);
870 pr_err("failed to lock workdir+upperdir\n");
874 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
875 int ovl_check_metacopy_xattr(struct ovl_fs
*ofs
, struct dentry
*dentry
)
879 /* Only regular files can have metacopy xattr */
880 if (!S_ISREG(d_inode(dentry
)->i_mode
))
883 res
= ovl_do_getxattr(ofs
, dentry
, OVL_XATTR_METACOPY
, NULL
, 0);
885 if (res
== -ENODATA
|| res
== -EOPNOTSUPP
)
888 * getxattr on user.* may fail with EACCES in case there's no
889 * read permission on the inode. Not much we can do, other than
890 * tell the caller that this is not a metacopy inode.
892 if (ofs
->config
.userxattr
&& res
== -EACCES
)
899 pr_warn_ratelimited("failed to get metacopy (%i)\n", res
);
903 bool ovl_is_metacopy_dentry(struct dentry
*dentry
)
905 struct ovl_entry
*oe
= dentry
->d_fsdata
;
907 if (!d_is_reg(dentry
))
910 if (ovl_dentry_upper(dentry
)) {
911 if (!ovl_has_upperdata(d_inode(dentry
)))
916 return (oe
->numlower
> 1);
919 char *ovl_get_redirect_xattr(struct ovl_fs
*ofs
, struct dentry
*dentry
,
923 char *s
, *next
, *buf
= NULL
;
925 res
= ovl_do_getxattr(ofs
, dentry
, OVL_XATTR_REDIRECT
, NULL
, 0);
926 if (res
== -ENODATA
|| res
== -EOPNOTSUPP
)
933 buf
= kzalloc(res
+ padding
+ 1, GFP_KERNEL
);
935 return ERR_PTR(-ENOMEM
);
937 res
= ovl_do_getxattr(ofs
, dentry
, OVL_XATTR_REDIRECT
, buf
, res
);
944 for (s
= buf
; *s
++ == '/'; s
= next
) {
945 next
= strchrnul(s
, '/');
950 if (strchr(buf
, '/') != NULL
)
956 pr_warn_ratelimited("invalid redirect (%s)\n", buf
);
960 pr_warn_ratelimited("failed to get redirect (%i)\n", res
);