1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
8 #include <linux/cred.h>
9 #include <linux/ctype.h>
10 #include <linux/namei.h>
11 #include <linux/xattr.h>
12 #include <linux/ratelimit.h>
13 #include <linux/mount.h>
14 #include <linux/exportfs.h>
15 #include "overlayfs.h"
17 struct ovl_lookup_data
{
18 struct super_block
*sb
;
28 static int ovl_check_redirect(struct dentry
*dentry
, struct ovl_lookup_data
*d
,
29 size_t prelen
, const char *post
)
34 buf
= ovl_get_redirect_xattr(dentry
, prelen
+ strlen(post
));
35 if (IS_ERR_OR_NULL(buf
))
40 * One of the ancestor path elements in an absolute path
41 * lookup in ovl_lookup_layer() could have been opaque and
42 * that will stop further lookup in lower layers (d->stop=true)
43 * But we have found an absolute redirect in decendant path
44 * element and that should force continue lookup in lower
45 * layers (reset d->stop).
49 res
= strlen(buf
) + 1;
50 memmove(buf
+ prelen
, buf
, res
);
51 memcpy(buf
, d
->name
.name
, prelen
);
57 d
->name
.name
= d
->redirect
;
58 d
->name
.len
= strlen(d
->redirect
);
63 static int ovl_acceptable(void *ctx
, struct dentry
*dentry
)
66 * A non-dir origin may be disconnected, which is fine, because
67 * we only need it for its unique inode number.
69 if (!d_is_dir(dentry
))
72 /* Don't decode a deleted empty directory */
73 if (d_unhashed(dentry
))
76 /* Check if directory belongs to the layer we are decoding from */
77 return is_subdir(dentry
, ((struct vfsmount
*)ctx
)->mnt_root
);
81 * Check validity of an overlay file handle buffer.
83 * Return 0 for a valid file handle.
84 * Return -ENODATA for "origin unknown".
85 * Return <0 for an invalid file handle.
87 int ovl_check_fb_len(struct ovl_fb
*fb
, int fb_len
)
89 if (fb_len
< sizeof(struct ovl_fb
) || fb_len
< fb
->len
)
92 if (fb
->magic
!= OVL_FH_MAGIC
)
95 /* Treat larger version and unknown flags as "origin unknown" */
96 if (fb
->version
> OVL_FH_VERSION
|| fb
->flags
& ~OVL_FH_FLAG_ALL
)
99 /* Treat endianness mismatch as "origin unknown" */
100 if (!(fb
->flags
& OVL_FH_FLAG_ANY_ENDIAN
) &&
101 (fb
->flags
& OVL_FH_FLAG_BIG_ENDIAN
) != OVL_FH_FLAG_CPU_ENDIAN
)
107 static struct ovl_fh
*ovl_get_fh(struct dentry
*dentry
, const char *name
)
110 struct ovl_fh
*fh
= NULL
;
112 res
= vfs_getxattr(dentry
, name
, NULL
, 0);
114 if (res
== -ENODATA
|| res
== -EOPNOTSUPP
)
118 /* Zero size value means "copied up but origin unknown" */
122 fh
= kzalloc(res
+ OVL_FH_WIRE_OFFSET
, GFP_KERNEL
);
124 return ERR_PTR(-ENOMEM
);
126 res
= vfs_getxattr(dentry
, name
, fh
->buf
, res
);
130 err
= ovl_check_fb_len(&fh
->fb
, res
);
144 pr_warn_ratelimited("failed to get origin (%i)\n", res
);
147 pr_warn_ratelimited("invalid origin (%*phN)\n", res
, fh
);
151 struct dentry
*ovl_decode_real_fh(struct ovl_fh
*fh
, struct vfsmount
*mnt
,
158 * Make sure that the stored uuid matches the uuid of the lower
159 * layer where file handle will be decoded.
161 if (!uuid_equal(&fh
->fb
.uuid
, &mnt
->mnt_sb
->s_uuid
))
164 bytes
= (fh
->fb
.len
- offsetof(struct ovl_fb
, fid
));
165 real
= exportfs_decode_fh(mnt
, (struct fid
*)fh
->fb
.fid
,
166 bytes
>> 2, (int)fh
->fb
.type
,
167 connected
? ovl_acceptable
: NULL
, mnt
);
170 * Treat stale file handle to lower file as "origin unknown".
171 * upper file handle could become stale when upper file is
172 * unlinked and this information is needed to handle stale
173 * index entries correctly.
175 if (real
== ERR_PTR(-ESTALE
) &&
176 !(fh
->fb
.flags
& OVL_FH_FLAG_PATH_UPPER
))
181 if (ovl_dentry_weird(real
)) {
189 static bool ovl_is_opaquedir(struct dentry
*dentry
)
191 return ovl_check_dir_xattr(dentry
, OVL_XATTR_OPAQUE
);
194 static int ovl_lookup_single(struct dentry
*base
, struct ovl_lookup_data
*d
,
195 const char *name
, unsigned int namelen
,
196 size_t prelen
, const char *post
,
201 bool last_element
= !post
[0];
203 this = lookup_positive_unlocked(name
, base
, namelen
);
207 if (err
== -ENOENT
|| err
== -ENAMETOOLONG
)
212 if (ovl_dentry_weird(this)) {
213 /* Don't support traversing automounts and other weirdness */
217 if (ovl_is_whiteout(this)) {
218 d
->stop
= d
->opaque
= true;
222 * This dentry should be a regular file if previous layer lookup
223 * found a metacopy dentry.
225 if (last_element
&& d
->metacopy
&& !d_is_reg(this)) {
229 if (!d_can_lookup(this)) {
230 if (d
->is_dir
|| !last_element
) {
234 err
= ovl_check_metacopy_xattr(this);
239 d
->stop
= !d
->metacopy
;
240 if (!d
->metacopy
|| d
->last
)
243 if (ovl_lookup_trap_inode(d
->sb
, this)) {
244 /* Caught in a trap of overlapping layers */
254 if (ovl_is_opaquedir(this)) {
261 err
= ovl_check_redirect(this, d
, prelen
, post
);
278 static int ovl_lookup_layer(struct dentry
*base
, struct ovl_lookup_data
*d
,
281 /* Counting down from the end, since the prefix can change */
282 size_t rem
= d
->name
.len
- 1;
283 struct dentry
*dentry
= NULL
;
286 if (d
->name
.name
[0] != '/')
287 return ovl_lookup_single(base
, d
, d
->name
.name
, d
->name
.len
,
290 while (!IS_ERR_OR_NULL(base
) && d_can_lookup(base
)) {
291 const char *s
= d
->name
.name
+ d
->name
.len
- rem
;
292 const char *next
= strchrnul(s
, '/');
293 size_t thislen
= next
- s
;
296 /* Verify we did not go off the rails */
297 if (WARN_ON(s
[-1] != '/'))
300 err
= ovl_lookup_single(base
, d
, s
, thislen
,
301 d
->name
.len
- rem
, next
, &base
);
311 if (WARN_ON(rem
>= d
->name
.len
))
319 int ovl_check_origin_fh(struct ovl_fs
*ofs
, struct ovl_fh
*fh
, bool connected
,
320 struct dentry
*upperdentry
, struct ovl_path
**stackp
)
322 struct dentry
*origin
= NULL
;
325 for (i
= 1; i
< ofs
->numlayer
; i
++) {
327 * If lower fs uuid is not unique among lower fs we cannot match
330 if (ofs
->layers
[i
].fsid
&&
331 ofs
->layers
[i
].fs
->bad_uuid
)
334 origin
= ovl_decode_real_fh(fh
, ofs
->layers
[i
].mnt
,
342 else if (IS_ERR(origin
))
343 return PTR_ERR(origin
);
345 if (upperdentry
&& !ovl_is_whiteout(upperdentry
) &&
346 ((d_inode(origin
)->i_mode
^ d_inode(upperdentry
)->i_mode
) & S_IFMT
))
350 *stackp
= kmalloc(sizeof(struct ovl_path
), GFP_KERNEL
);
355 **stackp
= (struct ovl_path
){
357 .layer
= &ofs
->layers
[i
]
363 pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
364 upperdentry
, d_inode(upperdentry
)->i_mode
& S_IFMT
,
365 d_inode(origin
)->i_mode
& S_IFMT
);
370 static int ovl_check_origin(struct ovl_fs
*ofs
, struct dentry
*upperdentry
,
371 struct ovl_path
**stackp
, unsigned int *ctrp
)
373 struct ovl_fh
*fh
= ovl_get_fh(upperdentry
, OVL_XATTR_ORIGIN
);
376 if (IS_ERR_OR_NULL(fh
))
379 err
= ovl_check_origin_fh(ofs
, fh
, false, upperdentry
, stackp
);
396 * Verify that @fh matches the file handle stored in xattr @name.
397 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
399 static int ovl_verify_fh(struct dentry
*dentry
, const char *name
,
400 const struct ovl_fh
*fh
)
402 struct ovl_fh
*ofh
= ovl_get_fh(dentry
, name
);
411 if (fh
->fb
.len
!= ofh
->fb
.len
|| memcmp(&fh
->fb
, &ofh
->fb
, fh
->fb
.len
))
419 * Verify that @real dentry matches the file handle stored in xattr @name.
421 * If @set is true and there is no stored file handle, encode @real and store
422 * file handle in xattr @name.
424 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
426 int ovl_verify_set_fh(struct dentry
*dentry
, const char *name
,
427 struct dentry
*real
, bool is_upper
, bool set
)
433 fh
= ovl_encode_real_fh(real
, is_upper
);
440 err
= ovl_verify_fh(dentry
, name
, fh
);
441 if (set
&& err
== -ENODATA
)
442 err
= ovl_do_setxattr(dentry
, name
, fh
->buf
, fh
->fb
.len
, 0);
451 inode
= d_inode(real
);
452 pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n",
453 is_upper
? "upper" : "origin", real
,
454 inode
? inode
->i_ino
: 0, err
);
458 /* Get upper dentry from index */
459 struct dentry
*ovl_index_upper(struct ovl_fs
*ofs
, struct dentry
*index
)
462 struct dentry
*upper
;
464 if (!d_is_dir(index
))
467 fh
= ovl_get_fh(index
, OVL_XATTR_UPPER
);
468 if (IS_ERR_OR_NULL(fh
))
471 upper
= ovl_decode_real_fh(fh
, ofs
->upper_mnt
, true);
474 if (IS_ERR_OR_NULL(upper
))
475 return upper
?: ERR_PTR(-ESTALE
);
477 if (!d_is_dir(upper
)) {
478 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
481 return ERR_PTR(-EIO
);
487 /* Is this a leftover from create/whiteout of directory index entry? */
488 static bool ovl_is_temp_index(struct dentry
*index
)
490 return index
->d_name
.name
[0] == '#';
494 * Verify that an index entry name matches the origin file handle stored in
495 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
496 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
498 int ovl_verify_index(struct ovl_fs
*ofs
, struct dentry
*index
)
500 struct ovl_fh
*fh
= NULL
;
502 struct ovl_path origin
= { };
503 struct ovl_path
*stack
= &origin
;
504 struct dentry
*upper
= NULL
;
510 /* Cleanup leftover from index create/cleanup attempt */
512 if (ovl_is_temp_index(index
))
516 if (index
->d_name
.len
< sizeof(struct ovl_fb
)*2)
520 len
= index
->d_name
.len
/ 2;
521 fh
= kzalloc(len
+ OVL_FH_WIRE_OFFSET
, GFP_KERNEL
);
526 if (hex2bin(fh
->buf
, index
->d_name
.name
, len
))
529 err
= ovl_check_fb_len(&fh
->fb
, len
);
534 * Whiteout index entries are used as an indication that an exported
535 * overlay file handle should be treated as stale (i.e. after unlink
536 * of the overlay inode). These entries contain no origin xattr.
538 if (ovl_is_whiteout(index
))
542 * Verifying directory index entries are not stale is expensive, so
543 * only verify stale dir index if NFS export is enabled.
545 if (d_is_dir(index
) && !ofs
->config
.nfs_export
)
549 * Directory index entries should have 'upper' xattr pointing to the
550 * real upper dir. Non-dir index entries are hardlinks to the upper
551 * real inode. For non-dir index, we can read the copy up origin xattr
552 * directly from the index dentry, but for dir index we first need to
553 * decode the upper directory.
555 upper
= ovl_index_upper(ofs
, index
);
556 if (IS_ERR_OR_NULL(upper
)) {
557 err
= PTR_ERR(upper
);
559 * Directory index entries with no 'upper' xattr need to be
560 * removed. When dir index entry has a stale 'upper' xattr,
561 * we assume that upper dir was removed and we treat the dir
562 * index as orphan entry that needs to be whited out.
571 err
= ovl_verify_fh(upper
, OVL_XATTR_ORIGIN
, fh
);
576 /* Check if non-dir index is orphan and don't warn before cleaning it */
577 if (!d_is_dir(index
) && d_inode(index
)->i_nlink
== 1) {
578 err
= ovl_check_origin_fh(ofs
, fh
, false, index
, &stack
);
582 if (ovl_get_nlink(origin
.dentry
, index
, 0) == 0)
592 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
593 index
, d_inode(index
)->i_mode
& S_IFMT
, err
);
597 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
598 index
, d_inode(index
)->i_mode
& S_IFMT
,
599 d_inode(index
)->i_nlink
);
604 static int ovl_get_index_name_fh(struct ovl_fh
*fh
, struct qstr
*name
)
608 n
= kcalloc(fh
->fb
.len
, 2, GFP_KERNEL
);
612 s
= bin2hex(n
, fh
->buf
, fh
->fb
.len
);
613 *name
= (struct qstr
) QSTR_INIT(n
, s
- n
);
620 * Lookup in indexdir for the index entry of a lower real inode or a copy up
621 * origin inode. The index entry name is the hex representation of the lower
624 * If the index dentry in negative, then either no lower aliases have been
625 * copied up yet, or aliases have been copied up in older kernels and are
628 * If the index dentry for a copy up origin inode is positive, but points
629 * to an inode different than the upper inode, then either the upper inode
630 * has been copied up and not indexed or it was indexed, but since then
631 * index dir was cleared. Either way, that index cannot be used to indentify
634 int ovl_get_index_name(struct dentry
*origin
, struct qstr
*name
)
639 fh
= ovl_encode_real_fh(origin
, false);
643 err
= ovl_get_index_name_fh(fh
, name
);
649 /* Lookup index by file handle for NFS export */
650 struct dentry
*ovl_get_index_fh(struct ovl_fs
*ofs
, struct ovl_fh
*fh
)
652 struct dentry
*index
;
656 err
= ovl_get_index_name_fh(fh
, &name
);
660 index
= lookup_positive_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
663 if (PTR_ERR(index
) == -ENOENT
)
668 if (ovl_is_whiteout(index
))
670 else if (ovl_dentry_weird(index
))
679 struct dentry
*ovl_lookup_index(struct ovl_fs
*ofs
, struct dentry
*upper
,
680 struct dentry
*origin
, bool verify
)
682 struct dentry
*index
;
685 bool is_dir
= d_is_dir(origin
);
688 err
= ovl_get_index_name(origin
, &name
);
692 index
= lookup_positive_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
694 err
= PTR_ERR(index
);
695 if (err
== -ENOENT
) {
699 pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
700 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
701 d_inode(origin
)->i_ino
, name
.len
, name
.name
,
706 inode
= d_inode(index
);
707 if (ovl_is_whiteout(index
) && !verify
) {
709 * When index lookup is called with !verify for decoding an
710 * overlay file handle, a whiteout index implies that decode
711 * should treat file handle as stale and no need to print a
715 index
= ERR_PTR(-ESTALE
);
717 } else if (ovl_dentry_weird(index
) || ovl_is_whiteout(index
) ||
718 ((inode
->i_mode
^ d_inode(origin
)->i_mode
) & S_IFMT
)) {
720 * Index should always be of the same file type as origin
721 * except for the case of a whiteout index. A whiteout
722 * index should only exist if all lower aliases have been
723 * unlinked, which means that finding a lower origin on lookup
724 * whose index is a whiteout should be treated as an error.
726 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
727 index
, d_inode(index
)->i_mode
& S_IFMT
,
728 d_inode(origin
)->i_mode
& S_IFMT
);
730 } else if (is_dir
&& verify
) {
732 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
737 /* Verify that dir index 'upper' xattr points to upper dir */
738 err
= ovl_verify_upper(index
, upper
, false);
740 if (err
== -ESTALE
) {
741 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
742 upper
, origin
, index
);
746 } else if (upper
&& d_inode(upper
) != inode
) {
760 index
= ERR_PTR(-EIO
);
765 * Returns next layer in stack starting from top.
766 * Returns -1 if this is the last layer.
768 int ovl_path_next(int idx
, struct dentry
*dentry
, struct path
*path
)
770 struct ovl_entry
*oe
= dentry
->d_fsdata
;
774 ovl_path_upper(dentry
, path
);
776 return oe
->numlower
? 1 : -1;
779 BUG_ON(idx
> oe
->numlower
);
780 path
->dentry
= oe
->lowerstack
[idx
- 1].dentry
;
781 path
->mnt
= oe
->lowerstack
[idx
- 1].layer
->mnt
;
783 return (idx
< oe
->numlower
) ? idx
+ 1 : -1;
786 /* Fix missing 'origin' xattr */
787 static int ovl_fix_origin(struct dentry
*dentry
, struct dentry
*lower
,
788 struct dentry
*upper
)
792 if (ovl_check_origin_xattr(upper
))
795 err
= ovl_want_write(dentry
);
799 err
= ovl_set_origin(dentry
, lower
, upper
);
801 err
= ovl_set_impure(dentry
->d_parent
, upper
->d_parent
);
803 ovl_drop_write(dentry
);
807 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
810 struct ovl_entry
*oe
;
811 const struct cred
*old_cred
;
812 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
813 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
814 struct ovl_entry
*roe
= dentry
->d_sb
->s_root
->d_fsdata
;
815 struct ovl_path
*stack
= NULL
, *origin_path
= NULL
;
816 struct dentry
*upperdir
, *upperdentry
= NULL
;
817 struct dentry
*origin
= NULL
;
818 struct dentry
*index
= NULL
;
819 unsigned int ctr
= 0;
820 struct inode
*inode
= NULL
;
821 bool upperopaque
= false;
822 char *upperredirect
= NULL
;
826 bool metacopy
= false;
827 struct ovl_lookup_data d
= {
829 .name
= dentry
->d_name
,
833 .last
= ofs
->config
.redirect_follow
? false : !poe
->numlower
,
838 if (dentry
->d_name
.len
> ofs
->namelen
)
839 return ERR_PTR(-ENAMETOOLONG
);
841 old_cred
= ovl_override_creds(dentry
->d_sb
);
842 upperdir
= ovl_dentry_upper(dentry
->d_parent
);
844 err
= ovl_lookup_layer(upperdir
, &d
, &upperdentry
);
848 if (upperdentry
&& upperdentry
->d_flags
& DCACHE_OP_REAL
) {
853 if (upperdentry
&& !d
.is_dir
) {
854 unsigned int origin_ctr
= 0;
857 * Lookup copy up origin by decoding origin file handle.
858 * We may get a disconnected dentry, which is fine,
859 * because we only need to hold the origin inode in
860 * cache and use its inode number. We may even get a
861 * connected dentry, that is not under any of the lower
862 * layers root. That is also fine for using it's inode
863 * number - it's the same as if we held a reference
864 * to a dentry in lower layer that was moved under us.
866 err
= ovl_check_origin(ofs
, upperdentry
, &origin_path
,
877 upperredirect
= kstrdup(d
.redirect
, GFP_KERNEL
);
880 if (d
.redirect
[0] == '/')
883 upperopaque
= d
.opaque
;
886 if (!d
.stop
&& poe
->numlower
) {
888 stack
= kcalloc(ofs
->numlayer
- 1, sizeof(struct ovl_path
),
894 for (i
= 0; !d
.stop
&& i
< poe
->numlower
; i
++) {
895 struct ovl_path lower
= poe
->lowerstack
[i
];
897 if (!ofs
->config
.redirect_follow
)
898 d
.last
= i
== poe
->numlower
- 1;
900 d
.last
= lower
.layer
->idx
== roe
->numlower
;
902 err
= ovl_lookup_layer(lower
.dentry
, &d
, &this);
910 * If no origin fh is stored in upper of a merge dir, store fh
911 * of lower dir and set upper parent "impure".
913 if (upperdentry
&& !ctr
&& !ofs
->noxattr
&& d
.is_dir
) {
914 err
= ovl_fix_origin(dentry
, this, upperdentry
);
922 * When "verify_lower" feature is enabled, do not merge with a
923 * lower dir that does not match a stored origin xattr. In any
924 * case, only verified origin is used for index lookup.
926 * For non-dir dentry, if index=on, then ensure origin
927 * matches the dentry found using path based lookup,
928 * otherwise error out.
930 if (upperdentry
&& !ctr
&&
931 ((d
.is_dir
&& ovl_verify_lower(dentry
->d_sb
)) ||
932 (!d
.is_dir
&& ofs
->config
.index
&& origin_path
))) {
933 err
= ovl_verify_origin(upperdentry
, this, false);
946 * Do not store intermediate metacopy dentries in chain,
947 * except top most lower metacopy dentry
949 if (d
.metacopy
&& ctr
) {
954 stack
[ctr
].dentry
= this;
955 stack
[ctr
].layer
= lower
.layer
;
959 * Following redirects can have security consequences: it's like
960 * a symlink into the lower layer without the permission checks.
961 * This is only a problem if the upper layer is untrusted (e.g
962 * comes from an USB drive). This can allow a non-readable file
963 * or directory to become readable.
965 * Only following redirects when redirects are enabled disables
966 * this attack vector when not necessary.
969 if (d
.redirect
&& !ofs
->config
.redirect_follow
) {
970 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
978 if (d
.redirect
&& d
.redirect
[0] == '/' && poe
!= roe
) {
980 /* Find the current layer on the root dentry */
981 i
= lower
.layer
->idx
- 1;
987 * Found a metacopy dentry but did not find corresponding
996 if (!ofs
->config
.metacopy
) {
997 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n",
1001 } else if (!d
.is_dir
&& upperdentry
&& !ctr
&& origin_path
) {
1002 if (WARN_ON(stack
!= NULL
)) {
1006 stack
= origin_path
;
1012 * Lookup index by lower inode and verify it matches upper inode.
1013 * We only trust dir index if we verified that lower dir matches
1014 * origin, otherwise dir index entries may be inconsistent and we
1017 * For non-dir upper metacopy dentry, we already set "origin" if we
1018 * verified that lower matched upper origin. If upper origin was
1019 * not present (because lower layer did not support fh encode/decode),
1020 * or indexing is not enabled, do not set "origin" and skip looking up
1021 * index. This case should be handled in same way as a non-dir upper
1022 * without ORIGIN is handled.
1024 * Always lookup index of non-dir non-metacopy and non-upper.
1026 if (ctr
&& (!upperdentry
|| (!d
.is_dir
&& !metacopy
)))
1027 origin
= stack
[0].dentry
;
1029 if (origin
&& ovl_indexdir(dentry
->d_sb
) &&
1030 (!d
.is_dir
|| ovl_index_all(dentry
->d_sb
))) {
1031 index
= ovl_lookup_index(ofs
, upperdentry
, origin
, true);
1032 if (IS_ERR(index
)) {
1033 err
= PTR_ERR(index
);
1039 oe
= ovl_alloc_entry(ctr
);
1044 memcpy(oe
->lowerstack
, stack
, sizeof(struct ovl_path
) * ctr
);
1045 dentry
->d_fsdata
= oe
;
1048 ovl_dentry_set_opaque(dentry
);
1051 ovl_dentry_set_upper_alias(dentry
);
1053 upperdentry
= dget(index
);
1054 upperredirect
= ovl_get_redirect_xattr(upperdentry
, 0);
1055 if (IS_ERR(upperredirect
)) {
1056 err
= PTR_ERR(upperredirect
);
1057 upperredirect
= NULL
;
1062 if (upperdentry
|| ctr
) {
1063 struct ovl_inode_params oip
= {
1064 .upperdentry
= upperdentry
,
1068 .redirect
= upperredirect
,
1069 .lowerdata
= (ctr
> 1 && !d
.is_dir
) ?
1070 stack
[ctr
- 1].dentry
: NULL
,
1073 inode
= ovl_get_inode(dentry
->d_sb
, &oip
);
1074 err
= PTR_ERR(inode
);
1079 ovl_dentry_update_reval(dentry
, upperdentry
,
1080 DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
);
1082 revert_creds(old_cred
);
1084 dput(origin_path
->dentry
);
1090 return d_splice_alias(inode
, dentry
);
1093 dentry
->d_fsdata
= NULL
;
1097 for (i
= 0; i
< ctr
; i
++)
1098 dput(stack
[i
].dentry
);
1102 dput(origin_path
->dentry
);
1106 kfree(upperredirect
);
1109 revert_creds(old_cred
);
1110 return ERR_PTR(err
);
1113 bool ovl_lower_positive(struct dentry
*dentry
)
1115 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
1116 const struct qstr
*name
= &dentry
->d_name
;
1117 const struct cred
*old_cred
;
1119 bool positive
= false;
1123 * If dentry is negative, then lower is positive iff this is a
1126 if (!dentry
->d_inode
)
1127 return ovl_dentry_is_opaque(dentry
);
1129 /* Negative upper -> positive lower */
1130 if (!ovl_dentry_upper(dentry
))
1133 old_cred
= ovl_override_creds(dentry
->d_sb
);
1134 /* Positive upper -> have to look up lower to see whether it exists */
1135 for (i
= 0; !done
&& !positive
&& i
< poe
->numlower
; i
++) {
1136 struct dentry
*this;
1137 struct dentry
*lowerdir
= poe
->lowerstack
[i
].dentry
;
1139 this = lookup_positive_unlocked(name
->name
, lowerdir
,
1142 switch (PTR_ERR(this)) {
1149 * Assume something is there, we just couldn't
1156 positive
= !ovl_is_whiteout(this);
1161 revert_creds(old_cred
);