2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
11 #include <linux/cred.h>
12 #include <linux/ctype.h>
13 #include <linux/namei.h>
14 #include <linux/xattr.h>
15 #include <linux/ratelimit.h>
16 #include <linux/mount.h>
17 #include <linux/exportfs.h>
18 #include "overlayfs.h"
20 struct ovl_lookup_data
{
21 struct super_block
*sb
;
31 static int ovl_check_redirect(struct dentry
*dentry
, struct ovl_lookup_data
*d
,
32 size_t prelen
, const char *post
)
37 buf
= ovl_get_redirect_xattr(dentry
, prelen
+ strlen(post
));
38 if (IS_ERR_OR_NULL(buf
))
43 * One of the ancestor path elements in an absolute path
44 * lookup in ovl_lookup_layer() could have been opaque and
45 * that will stop further lookup in lower layers (d->stop=true)
46 * But we have found an absolute redirect in decendant path
47 * element and that should force continue lookup in lower
48 * layers (reset d->stop).
52 res
= strlen(buf
) + 1;
53 memmove(buf
+ prelen
, buf
, res
);
54 memcpy(buf
, d
->name
.name
, prelen
);
60 d
->name
.name
= d
->redirect
;
61 d
->name
.len
= strlen(d
->redirect
);
66 static int ovl_acceptable(void *ctx
, struct dentry
*dentry
)
69 * A non-dir origin may be disconnected, which is fine, because
70 * we only need it for its unique inode number.
72 if (!d_is_dir(dentry
))
75 /* Don't decode a deleted empty directory */
76 if (d_unhashed(dentry
))
79 /* Check if directory belongs to the layer we are decoding from */
80 return is_subdir(dentry
, ((struct vfsmount
*)ctx
)->mnt_root
);
84 * Check validity of an overlay file handle buffer.
86 * Return 0 for a valid file handle.
87 * Return -ENODATA for "origin unknown".
88 * Return <0 for an invalid file handle.
90 int ovl_check_fh_len(struct ovl_fh
*fh
, int fh_len
)
92 if (fh_len
< sizeof(struct ovl_fh
) || fh_len
< fh
->len
)
95 if (fh
->magic
!= OVL_FH_MAGIC
)
98 /* Treat larger version and unknown flags as "origin unknown" */
99 if (fh
->version
> OVL_FH_VERSION
|| fh
->flags
& ~OVL_FH_FLAG_ALL
)
102 /* Treat endianness mismatch as "origin unknown" */
103 if (!(fh
->flags
& OVL_FH_FLAG_ANY_ENDIAN
) &&
104 (fh
->flags
& OVL_FH_FLAG_BIG_ENDIAN
) != OVL_FH_FLAG_CPU_ENDIAN
)
110 static struct ovl_fh
*ovl_get_fh(struct dentry
*dentry
, const char *name
)
113 struct ovl_fh
*fh
= NULL
;
115 res
= vfs_getxattr(dentry
, name
, NULL
, 0);
117 if (res
== -ENODATA
|| res
== -EOPNOTSUPP
)
121 /* Zero size value means "copied up but origin unknown" */
125 fh
= kzalloc(res
, GFP_KERNEL
);
127 return ERR_PTR(-ENOMEM
);
129 res
= vfs_getxattr(dentry
, name
, fh
, res
);
133 err
= ovl_check_fh_len(fh
, res
);
147 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res
);
150 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res
, fh
);
154 struct dentry
*ovl_decode_real_fh(struct ovl_fh
*fh
, struct vfsmount
*mnt
,
161 * Make sure that the stored uuid matches the uuid of the lower
162 * layer where file handle will be decoded.
164 if (!uuid_equal(&fh
->uuid
, &mnt
->mnt_sb
->s_uuid
))
167 bytes
= (fh
->len
- offsetof(struct ovl_fh
, fid
));
168 real
= exportfs_decode_fh(mnt
, (struct fid
*)fh
->fid
,
169 bytes
>> 2, (int)fh
->type
,
170 connected
? ovl_acceptable
: NULL
, mnt
);
173 * Treat stale file handle to lower file as "origin unknown".
174 * upper file handle could become stale when upper file is
175 * unlinked and this information is needed to handle stale
176 * index entries correctly.
178 if (real
== ERR_PTR(-ESTALE
) &&
179 !(fh
->flags
& OVL_FH_FLAG_PATH_UPPER
))
184 if (ovl_dentry_weird(real
)) {
192 static bool ovl_is_opaquedir(struct dentry
*dentry
)
194 return ovl_check_dir_xattr(dentry
, OVL_XATTR_OPAQUE
);
197 static int ovl_lookup_single(struct dentry
*base
, struct ovl_lookup_data
*d
,
198 const char *name
, unsigned int namelen
,
199 size_t prelen
, const char *post
,
204 bool last_element
= !post
[0];
206 this = lookup_one_len_unlocked(name
, base
, namelen
);
210 if (err
== -ENOENT
|| err
== -ENAMETOOLONG
)
217 if (ovl_dentry_weird(this)) {
218 /* Don't support traversing automounts and other weirdness */
222 if (ovl_is_whiteout(this)) {
223 d
->stop
= d
->opaque
= true;
227 * This dentry should be a regular file if previous layer lookup
228 * found a metacopy dentry.
230 if (last_element
&& d
->metacopy
&& !d_is_reg(this)) {
234 if (!d_can_lookup(this)) {
235 if (d
->is_dir
|| !last_element
) {
239 err
= ovl_check_metacopy_xattr(this);
244 d
->stop
= !d
->metacopy
;
245 if (!d
->metacopy
|| d
->last
)
248 if (ovl_lookup_trap_inode(d
->sb
, this)) {
249 /* Caught in a trap of overlapping layers */
259 if (ovl_is_opaquedir(this)) {
266 err
= ovl_check_redirect(this, d
, prelen
, post
);
283 static int ovl_lookup_layer(struct dentry
*base
, struct ovl_lookup_data
*d
,
286 /* Counting down from the end, since the prefix can change */
287 size_t rem
= d
->name
.len
- 1;
288 struct dentry
*dentry
= NULL
;
291 if (d
->name
.name
[0] != '/')
292 return ovl_lookup_single(base
, d
, d
->name
.name
, d
->name
.len
,
295 while (!IS_ERR_OR_NULL(base
) && d_can_lookup(base
)) {
296 const char *s
= d
->name
.name
+ d
->name
.len
- rem
;
297 const char *next
= strchrnul(s
, '/');
298 size_t thislen
= next
- s
;
301 /* Verify we did not go off the rails */
302 if (WARN_ON(s
[-1] != '/'))
305 err
= ovl_lookup_single(base
, d
, s
, thislen
,
306 d
->name
.len
- rem
, next
, &base
);
316 if (WARN_ON(rem
>= d
->name
.len
))
324 int ovl_check_origin_fh(struct ovl_fs
*ofs
, struct ovl_fh
*fh
, bool connected
,
325 struct dentry
*upperdentry
, struct ovl_path
**stackp
)
327 struct dentry
*origin
= NULL
;
330 for (i
= 0; i
< ofs
->numlower
; i
++) {
331 origin
= ovl_decode_real_fh(fh
, ofs
->lower_layers
[i
].mnt
,
339 else if (IS_ERR(origin
))
340 return PTR_ERR(origin
);
342 if (upperdentry
&& !ovl_is_whiteout(upperdentry
) &&
343 ((d_inode(origin
)->i_mode
^ d_inode(upperdentry
)->i_mode
) & S_IFMT
))
347 *stackp
= kmalloc(sizeof(struct ovl_path
), GFP_KERNEL
);
352 **stackp
= (struct ovl_path
){
354 .layer
= &ofs
->lower_layers
[i
]
360 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
361 upperdentry
, d_inode(upperdentry
)->i_mode
& S_IFMT
,
362 d_inode(origin
)->i_mode
& S_IFMT
);
367 static int ovl_check_origin(struct ovl_fs
*ofs
, struct dentry
*upperdentry
,
368 struct ovl_path
**stackp
, unsigned int *ctrp
)
370 struct ovl_fh
*fh
= ovl_get_fh(upperdentry
, OVL_XATTR_ORIGIN
);
373 if (IS_ERR_OR_NULL(fh
))
376 err
= ovl_check_origin_fh(ofs
, fh
, false, upperdentry
, stackp
);
393 * Verify that @fh matches the file handle stored in xattr @name.
394 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
396 static int ovl_verify_fh(struct dentry
*dentry
, const char *name
,
397 const struct ovl_fh
*fh
)
399 struct ovl_fh
*ofh
= ovl_get_fh(dentry
, name
);
408 if (fh
->len
!= ofh
->len
|| memcmp(fh
, ofh
, fh
->len
))
416 * Verify that @real dentry matches the file handle stored in xattr @name.
418 * If @set is true and there is no stored file handle, encode @real and store
419 * file handle in xattr @name.
421 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
423 int ovl_verify_set_fh(struct dentry
*dentry
, const char *name
,
424 struct dentry
*real
, bool is_upper
, bool set
)
430 fh
= ovl_encode_real_fh(real
, is_upper
);
437 err
= ovl_verify_fh(dentry
, name
, fh
);
438 if (set
&& err
== -ENODATA
)
439 err
= ovl_do_setxattr(dentry
, name
, fh
, fh
->len
, 0);
448 inode
= d_inode(real
);
449 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
450 is_upper
? "upper" : "origin", real
,
451 inode
? inode
->i_ino
: 0, err
);
455 /* Get upper dentry from index */
456 struct dentry
*ovl_index_upper(struct ovl_fs
*ofs
, struct dentry
*index
)
459 struct dentry
*upper
;
461 if (!d_is_dir(index
))
464 fh
= ovl_get_fh(index
, OVL_XATTR_UPPER
);
465 if (IS_ERR_OR_NULL(fh
))
468 upper
= ovl_decode_real_fh(fh
, ofs
->upper_mnt
, true);
471 if (IS_ERR_OR_NULL(upper
))
472 return upper
?: ERR_PTR(-ESTALE
);
474 if (!d_is_dir(upper
)) {
475 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
478 return ERR_PTR(-EIO
);
484 /* Is this a leftover from create/whiteout of directory index entry? */
485 static bool ovl_is_temp_index(struct dentry
*index
)
487 return index
->d_name
.name
[0] == '#';
491 * Verify that an index entry name matches the origin file handle stored in
492 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
493 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
495 int ovl_verify_index(struct ovl_fs
*ofs
, struct dentry
*index
)
497 struct ovl_fh
*fh
= NULL
;
499 struct ovl_path origin
= { };
500 struct ovl_path
*stack
= &origin
;
501 struct dentry
*upper
= NULL
;
507 /* Cleanup leftover from index create/cleanup attempt */
509 if (ovl_is_temp_index(index
))
513 if (index
->d_name
.len
< sizeof(struct ovl_fh
)*2)
517 len
= index
->d_name
.len
/ 2;
518 fh
= kzalloc(len
, GFP_KERNEL
);
523 if (hex2bin((u8
*)fh
, index
->d_name
.name
, len
))
526 err
= ovl_check_fh_len(fh
, len
);
531 * Whiteout index entries are used as an indication that an exported
532 * overlay file handle should be treated as stale (i.e. after unlink
533 * of the overlay inode). These entries contain no origin xattr.
535 if (ovl_is_whiteout(index
))
539 * Verifying directory index entries are not stale is expensive, so
540 * only verify stale dir index if NFS export is enabled.
542 if (d_is_dir(index
) && !ofs
->config
.nfs_export
)
546 * Directory index entries should have 'upper' xattr pointing to the
547 * real upper dir. Non-dir index entries are hardlinks to the upper
548 * real inode. For non-dir index, we can read the copy up origin xattr
549 * directly from the index dentry, but for dir index we first need to
550 * decode the upper directory.
552 upper
= ovl_index_upper(ofs
, index
);
553 if (IS_ERR_OR_NULL(upper
)) {
554 err
= PTR_ERR(upper
);
556 * Directory index entries with no 'upper' xattr need to be
557 * removed. When dir index entry has a stale 'upper' xattr,
558 * we assume that upper dir was removed and we treat the dir
559 * index as orphan entry that needs to be whited out.
568 err
= ovl_verify_fh(upper
, OVL_XATTR_ORIGIN
, fh
);
573 /* Check if non-dir index is orphan and don't warn before cleaning it */
574 if (!d_is_dir(index
) && d_inode(index
)->i_nlink
== 1) {
575 err
= ovl_check_origin_fh(ofs
, fh
, false, index
, &stack
);
579 if (ovl_get_nlink(origin
.dentry
, index
, 0) == 0)
589 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
590 index
, d_inode(index
)->i_mode
& S_IFMT
, err
);
594 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
595 index
, d_inode(index
)->i_mode
& S_IFMT
,
596 d_inode(index
)->i_nlink
);
601 static int ovl_get_index_name_fh(struct ovl_fh
*fh
, struct qstr
*name
)
605 n
= kcalloc(fh
->len
, 2, GFP_KERNEL
);
609 s
= bin2hex(n
, fh
, fh
->len
);
610 *name
= (struct qstr
) QSTR_INIT(n
, s
- n
);
617 * Lookup in indexdir for the index entry of a lower real inode or a copy up
618 * origin inode. The index entry name is the hex representation of the lower
621 * If the index dentry in negative, then either no lower aliases have been
622 * copied up yet, or aliases have been copied up in older kernels and are
625 * If the index dentry for a copy up origin inode is positive, but points
626 * to an inode different than the upper inode, then either the upper inode
627 * has been copied up and not indexed or it was indexed, but since then
628 * index dir was cleared. Either way, that index cannot be used to indentify
631 int ovl_get_index_name(struct dentry
*origin
, struct qstr
*name
)
636 fh
= ovl_encode_real_fh(origin
, false);
640 err
= ovl_get_index_name_fh(fh
, name
);
646 /* Lookup index by file handle for NFS export */
647 struct dentry
*ovl_get_index_fh(struct ovl_fs
*ofs
, struct ovl_fh
*fh
)
649 struct dentry
*index
;
653 err
= ovl_get_index_name_fh(fh
, &name
);
657 index
= lookup_one_len_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
660 if (PTR_ERR(index
) == -ENOENT
)
665 if (d_is_negative(index
))
667 else if (ovl_is_whiteout(index
))
669 else if (ovl_dentry_weird(index
))
678 struct dentry
*ovl_lookup_index(struct ovl_fs
*ofs
, struct dentry
*upper
,
679 struct dentry
*origin
, bool verify
)
681 struct dentry
*index
;
684 bool is_dir
= d_is_dir(origin
);
687 err
= ovl_get_index_name(origin
, &name
);
691 index
= lookup_one_len_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
693 err
= PTR_ERR(index
);
694 if (err
== -ENOENT
) {
698 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
699 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
700 d_inode(origin
)->i_ino
, name
.len
, name
.name
,
705 inode
= d_inode(index
);
706 if (d_is_negative(index
)) {
708 } else if (ovl_is_whiteout(index
) && !verify
) {
710 * When index lookup is called with !verify for decoding an
711 * overlay file handle, a whiteout index implies that decode
712 * should treat file handle as stale and no need to print a
716 index
= ERR_PTR(-ESTALE
);
718 } else if (ovl_dentry_weird(index
) || ovl_is_whiteout(index
) ||
719 ((inode
->i_mode
^ d_inode(origin
)->i_mode
) & S_IFMT
)) {
721 * Index should always be of the same file type as origin
722 * except for the case of a whiteout index. A whiteout
723 * index should only exist if all lower aliases have been
724 * unlinked, which means that finding a lower origin on lookup
725 * whose index is a whiteout should be treated as an error.
727 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
728 index
, d_inode(index
)->i_mode
& S_IFMT
,
729 d_inode(origin
)->i_mode
& S_IFMT
);
731 } else if (is_dir
&& verify
) {
733 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
738 /* Verify that dir index 'upper' xattr points to upper dir */
739 err
= ovl_verify_upper(index
, upper
, false);
741 if (err
== -ESTALE
) {
742 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
743 upper
, origin
, index
);
747 } else if (upper
&& d_inode(upper
) != inode
) {
761 index
= ERR_PTR(-EIO
);
766 * Returns next layer in stack starting from top.
767 * Returns -1 if this is the last layer.
769 int ovl_path_next(int idx
, struct dentry
*dentry
, struct path
*path
)
771 struct ovl_entry
*oe
= dentry
->d_fsdata
;
775 ovl_path_upper(dentry
, path
);
777 return oe
->numlower
? 1 : -1;
780 BUG_ON(idx
> oe
->numlower
);
781 path
->dentry
= oe
->lowerstack
[idx
- 1].dentry
;
782 path
->mnt
= oe
->lowerstack
[idx
- 1].layer
->mnt
;
784 return (idx
< oe
->numlower
) ? idx
+ 1 : -1;
787 /* Fix missing 'origin' xattr */
788 static int ovl_fix_origin(struct dentry
*dentry
, struct dentry
*lower
,
789 struct dentry
*upper
)
793 if (ovl_check_origin_xattr(upper
))
796 err
= ovl_want_write(dentry
);
800 err
= ovl_set_origin(dentry
, lower
, upper
);
802 err
= ovl_set_impure(dentry
->d_parent
, upper
->d_parent
);
804 ovl_drop_write(dentry
);
808 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
811 struct ovl_entry
*oe
;
812 const struct cred
*old_cred
;
813 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
814 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
815 struct ovl_entry
*roe
= dentry
->d_sb
->s_root
->d_fsdata
;
816 struct ovl_path
*stack
= NULL
, *origin_path
= NULL
;
817 struct dentry
*upperdir
, *upperdentry
= NULL
;
818 struct dentry
*origin
= NULL
;
819 struct dentry
*index
= NULL
;
820 unsigned int ctr
= 0;
821 struct inode
*inode
= NULL
;
822 bool upperopaque
= false;
823 char *upperredirect
= NULL
;
827 bool metacopy
= false;
828 struct ovl_lookup_data d
= {
830 .name
= dentry
->d_name
,
834 .last
= ofs
->config
.redirect_follow
? false : !poe
->numlower
,
839 if (dentry
->d_name
.len
> ofs
->namelen
)
840 return ERR_PTR(-ENAMETOOLONG
);
842 old_cred
= ovl_override_creds(dentry
->d_sb
);
843 upperdir
= ovl_dentry_upper(dentry
->d_parent
);
845 err
= ovl_lookup_layer(upperdir
, &d
, &upperdentry
);
849 if (upperdentry
&& unlikely(ovl_dentry_remote(upperdentry
))) {
854 if (upperdentry
&& !d
.is_dir
) {
855 unsigned int origin_ctr
= 0;
858 * Lookup copy up origin by decoding origin file handle.
859 * We may get a disconnected dentry, which is fine,
860 * because we only need to hold the origin inode in
861 * cache and use its inode number. We may even get a
862 * connected dentry, that is not under any of the lower
863 * layers root. That is also fine for using it's inode
864 * number - it's the same as if we held a reference
865 * to a dentry in lower layer that was moved under us.
867 err
= ovl_check_origin(ofs
, upperdentry
, &origin_path
,
878 upperredirect
= kstrdup(d
.redirect
, GFP_KERNEL
);
881 if (d
.redirect
[0] == '/')
884 upperopaque
= d
.opaque
;
887 if (!d
.stop
&& poe
->numlower
) {
889 stack
= kcalloc(ofs
->numlower
, sizeof(struct ovl_path
),
895 for (i
= 0; !d
.stop
&& i
< poe
->numlower
; i
++) {
896 struct ovl_path lower
= poe
->lowerstack
[i
];
898 if (!ofs
->config
.redirect_follow
)
899 d
.last
= i
== poe
->numlower
- 1;
901 d
.last
= lower
.layer
->idx
== roe
->numlower
;
903 err
= ovl_lookup_layer(lower
.dentry
, &d
, &this);
911 * If no origin fh is stored in upper of a merge dir, store fh
912 * of lower dir and set upper parent "impure".
914 if (upperdentry
&& !ctr
&& !ofs
->noxattr
&& d
.is_dir
) {
915 err
= ovl_fix_origin(dentry
, this, upperdentry
);
923 * When "verify_lower" feature is enabled, do not merge with a
924 * lower dir that does not match a stored origin xattr. In any
925 * case, only verified origin is used for index lookup.
927 * For non-dir dentry, if index=on, then ensure origin
928 * matches the dentry found using path based lookup,
929 * otherwise error out.
931 if (upperdentry
&& !ctr
&&
932 ((d
.is_dir
&& ovl_verify_lower(dentry
->d_sb
)) ||
933 (!d
.is_dir
&& ofs
->config
.index
&& origin_path
))) {
934 err
= ovl_verify_origin(upperdentry
, this, false);
947 * Do not store intermediate metacopy dentries in chain,
948 * except top most lower metacopy dentry
950 if (d
.metacopy
&& ctr
) {
955 stack
[ctr
].dentry
= this;
956 stack
[ctr
].layer
= lower
.layer
;
960 * Following redirects can have security consequences: it's like
961 * a symlink into the lower layer without the permission checks.
962 * This is only a problem if the upper layer is untrusted (e.g
963 * comes from an USB drive). This can allow a non-readable file
964 * or directory to become readable.
966 * Only following redirects when redirects are enabled disables
967 * this attack vector when not necessary.
970 if (d
.redirect
&& !ofs
->config
.redirect_follow
) {
971 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
979 if (d
.redirect
&& d
.redirect
[0] == '/' && poe
!= roe
) {
981 /* Find the current layer on the root dentry */
982 i
= lower
.layer
->idx
- 1;
988 * Found a metacopy dentry but did not find corresponding
997 if (!ofs
->config
.metacopy
) {
998 pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
1002 } else if (!d
.is_dir
&& upperdentry
&& !ctr
&& origin_path
) {
1003 if (WARN_ON(stack
!= NULL
)) {
1007 stack
= origin_path
;
1013 * Lookup index by lower inode and verify it matches upper inode.
1014 * We only trust dir index if we verified that lower dir matches
1015 * origin, otherwise dir index entries may be inconsistent and we
1018 * For non-dir upper metacopy dentry, we already set "origin" if we
1019 * verified that lower matched upper origin. If upper origin was
1020 * not present (because lower layer did not support fh encode/decode),
1021 * or indexing is not enabled, do not set "origin" and skip looking up
1022 * index. This case should be handled in same way as a non-dir upper
1023 * without ORIGIN is handled.
1025 * Always lookup index of non-dir non-metacopy and non-upper.
1027 if (ctr
&& (!upperdentry
|| (!d
.is_dir
&& !metacopy
)))
1028 origin
= stack
[0].dentry
;
1030 if (origin
&& ovl_indexdir(dentry
->d_sb
) &&
1031 (!d
.is_dir
|| ovl_index_all(dentry
->d_sb
))) {
1032 index
= ovl_lookup_index(ofs
, upperdentry
, origin
, true);
1033 if (IS_ERR(index
)) {
1034 err
= PTR_ERR(index
);
1040 oe
= ovl_alloc_entry(ctr
);
1045 memcpy(oe
->lowerstack
, stack
, sizeof(struct ovl_path
) * ctr
);
1046 dentry
->d_fsdata
= oe
;
1049 ovl_dentry_set_opaque(dentry
);
1052 ovl_dentry_set_upper_alias(dentry
);
1054 upperdentry
= dget(index
);
1055 upperredirect
= ovl_get_redirect_xattr(upperdentry
, 0);
1056 if (IS_ERR(upperredirect
)) {
1057 err
= PTR_ERR(upperredirect
);
1058 upperredirect
= NULL
;
1063 if (upperdentry
|| ctr
) {
1064 struct ovl_inode_params oip
= {
1065 .upperdentry
= upperdentry
,
1069 .redirect
= upperredirect
,
1070 .lowerdata
= (ctr
> 1 && !d
.is_dir
) ?
1071 stack
[ctr
- 1].dentry
: NULL
,
1074 inode
= ovl_get_inode(dentry
->d_sb
, &oip
);
1075 err
= PTR_ERR(inode
);
1080 revert_creds(old_cred
);
1082 dput(origin_path
->dentry
);
1088 return d_splice_alias(inode
, dentry
);
1091 dentry
->d_fsdata
= NULL
;
1095 for (i
= 0; i
< ctr
; i
++)
1096 dput(stack
[i
].dentry
);
1100 dput(origin_path
->dentry
);
1104 kfree(upperredirect
);
1107 revert_creds(old_cred
);
1108 return ERR_PTR(err
);
1111 bool ovl_lower_positive(struct dentry
*dentry
)
1113 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
1114 const struct qstr
*name
= &dentry
->d_name
;
1115 const struct cred
*old_cred
;
1117 bool positive
= false;
1121 * If dentry is negative, then lower is positive iff this is a
1124 if (!dentry
->d_inode
)
1125 return ovl_dentry_is_opaque(dentry
);
1127 /* Negative upper -> positive lower */
1128 if (!ovl_dentry_upper(dentry
))
1131 old_cred
= ovl_override_creds(dentry
->d_sb
);
1132 /* Positive upper -> have to look up lower to see whether it exists */
1133 for (i
= 0; !done
&& !positive
&& i
< poe
->numlower
; i
++) {
1134 struct dentry
*this;
1135 struct dentry
*lowerdir
= poe
->lowerstack
[i
].dentry
;
1137 this = lookup_one_len_unlocked(name
->name
, lowerdir
,
1140 switch (PTR_ERR(this)) {
1147 * Assume something is there, we just couldn't
1154 if (this->d_inode
) {
1155 positive
= !ovl_is_whiteout(this);
1161 revert_creds(old_cred
);