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_fh_len(struct ovl_fh
*fh
, int fh_len
)
89 if (fh_len
< sizeof(struct ovl_fh
) || fh_len
< fh
->len
)
92 if (fh
->magic
!= OVL_FH_MAGIC
)
95 /* Treat larger version and unknown flags as "origin unknown" */
96 if (fh
->version
> OVL_FH_VERSION
|| fh
->flags
& ~OVL_FH_FLAG_ALL
)
99 /* Treat endianness mismatch as "origin unknown" */
100 if (!(fh
->flags
& OVL_FH_FLAG_ANY_ENDIAN
) &&
101 (fh
->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
, GFP_KERNEL
);
124 return ERR_PTR(-ENOMEM
);
126 res
= vfs_getxattr(dentry
, name
, fh
, res
);
130 err
= ovl_check_fh_len(fh
, res
);
144 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res
);
147 pr_warn_ratelimited("overlayfs: 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
->uuid
, &mnt
->mnt_sb
->s_uuid
))
164 bytes
= (fh
->len
- offsetof(struct ovl_fh
, fid
));
165 real
= exportfs_decode_fh(mnt
, (struct fid
*)fh
->fid
,
166 bytes
>> 2, (int)fh
->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
->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_one_len_unlocked(name
, base
, namelen
);
207 if (err
== -ENOENT
|| err
== -ENAMETOOLONG
)
214 if (ovl_dentry_weird(this)) {
215 /* Don't support traversing automounts and other weirdness */
219 if (ovl_is_whiteout(this)) {
220 d
->stop
= d
->opaque
= true;
224 * This dentry should be a regular file if previous layer lookup
225 * found a metacopy dentry.
227 if (last_element
&& d
->metacopy
&& !d_is_reg(this)) {
231 if (!d_can_lookup(this)) {
232 if (d
->is_dir
|| !last_element
) {
236 err
= ovl_check_metacopy_xattr(this);
241 d
->stop
= !d
->metacopy
;
242 if (!d
->metacopy
|| d
->last
)
245 if (ovl_lookup_trap_inode(d
->sb
, this)) {
246 /* Caught in a trap of overlapping layers */
256 if (ovl_is_opaquedir(this)) {
263 err
= ovl_check_redirect(this, d
, prelen
, post
);
280 static int ovl_lookup_layer(struct dentry
*base
, struct ovl_lookup_data
*d
,
283 /* Counting down from the end, since the prefix can change */
284 size_t rem
= d
->name
.len
- 1;
285 struct dentry
*dentry
= NULL
;
288 if (d
->name
.name
[0] != '/')
289 return ovl_lookup_single(base
, d
, d
->name
.name
, d
->name
.len
,
292 while (!IS_ERR_OR_NULL(base
) && d_can_lookup(base
)) {
293 const char *s
= d
->name
.name
+ d
->name
.len
- rem
;
294 const char *next
= strchrnul(s
, '/');
295 size_t thislen
= next
- s
;
298 /* Verify we did not go off the rails */
299 if (WARN_ON(s
[-1] != '/'))
302 err
= ovl_lookup_single(base
, d
, s
, thislen
,
303 d
->name
.len
- rem
, next
, &base
);
313 if (WARN_ON(rem
>= d
->name
.len
))
321 int ovl_check_origin_fh(struct ovl_fs
*ofs
, struct ovl_fh
*fh
, bool connected
,
322 struct dentry
*upperdentry
, struct ovl_path
**stackp
)
324 struct dentry
*origin
= NULL
;
327 for (i
= 0; i
< ofs
->numlower
; i
++) {
329 * If lower fs uuid is not unique among lower fs we cannot match
332 if (ofs
->lower_layers
[i
].fsid
&&
333 ofs
->lower_layers
[i
].fs
->bad_uuid
)
336 origin
= ovl_decode_real_fh(fh
, ofs
->lower_layers
[i
].mnt
,
344 else if (IS_ERR(origin
))
345 return PTR_ERR(origin
);
347 if (upperdentry
&& !ovl_is_whiteout(upperdentry
) &&
348 ((d_inode(origin
)->i_mode
^ d_inode(upperdentry
)->i_mode
) & S_IFMT
))
352 *stackp
= kmalloc(sizeof(struct ovl_path
), GFP_KERNEL
);
357 **stackp
= (struct ovl_path
){
359 .layer
= &ofs
->lower_layers
[i
]
365 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
366 upperdentry
, d_inode(upperdentry
)->i_mode
& S_IFMT
,
367 d_inode(origin
)->i_mode
& S_IFMT
);
372 static int ovl_check_origin(struct ovl_fs
*ofs
, struct dentry
*upperdentry
,
373 struct ovl_path
**stackp
, unsigned int *ctrp
)
375 struct ovl_fh
*fh
= ovl_get_fh(upperdentry
, OVL_XATTR_ORIGIN
);
378 if (IS_ERR_OR_NULL(fh
))
381 err
= ovl_check_origin_fh(ofs
, fh
, false, upperdentry
, stackp
);
398 * Verify that @fh matches the file handle stored in xattr @name.
399 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
401 static int ovl_verify_fh(struct dentry
*dentry
, const char *name
,
402 const struct ovl_fh
*fh
)
404 struct ovl_fh
*ofh
= ovl_get_fh(dentry
, name
);
413 if (fh
->len
!= ofh
->len
|| memcmp(fh
, ofh
, fh
->len
))
421 * Verify that @real dentry matches the file handle stored in xattr @name.
423 * If @set is true and there is no stored file handle, encode @real and store
424 * file handle in xattr @name.
426 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
428 int ovl_verify_set_fh(struct dentry
*dentry
, const char *name
,
429 struct dentry
*real
, bool is_upper
, bool set
)
435 fh
= ovl_encode_real_fh(real
, is_upper
);
442 err
= ovl_verify_fh(dentry
, name
, fh
);
443 if (set
&& err
== -ENODATA
)
444 err
= ovl_do_setxattr(dentry
, name
, fh
, fh
->len
, 0);
453 inode
= d_inode(real
);
454 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
455 is_upper
? "upper" : "origin", real
,
456 inode
? inode
->i_ino
: 0, err
);
460 /* Get upper dentry from index */
461 struct dentry
*ovl_index_upper(struct ovl_fs
*ofs
, struct dentry
*index
)
464 struct dentry
*upper
;
466 if (!d_is_dir(index
))
469 fh
= ovl_get_fh(index
, OVL_XATTR_UPPER
);
470 if (IS_ERR_OR_NULL(fh
))
473 upper
= ovl_decode_real_fh(fh
, ofs
->upper_mnt
, true);
476 if (IS_ERR_OR_NULL(upper
))
477 return upper
?: ERR_PTR(-ESTALE
);
479 if (!d_is_dir(upper
)) {
480 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
483 return ERR_PTR(-EIO
);
489 /* Is this a leftover from create/whiteout of directory index entry? */
490 static bool ovl_is_temp_index(struct dentry
*index
)
492 return index
->d_name
.name
[0] == '#';
496 * Verify that an index entry name matches the origin file handle stored in
497 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
498 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
500 int ovl_verify_index(struct ovl_fs
*ofs
, struct dentry
*index
)
502 struct ovl_fh
*fh
= NULL
;
504 struct ovl_path origin
= { };
505 struct ovl_path
*stack
= &origin
;
506 struct dentry
*upper
= NULL
;
512 /* Cleanup leftover from index create/cleanup attempt */
514 if (ovl_is_temp_index(index
))
518 if (index
->d_name
.len
< sizeof(struct ovl_fh
)*2)
522 len
= index
->d_name
.len
/ 2;
523 fh
= kzalloc(len
, GFP_KERNEL
);
528 if (hex2bin((u8
*)fh
, index
->d_name
.name
, len
))
531 err
= ovl_check_fh_len(fh
, len
);
536 * Whiteout index entries are used as an indication that an exported
537 * overlay file handle should be treated as stale (i.e. after unlink
538 * of the overlay inode). These entries contain no origin xattr.
540 if (ovl_is_whiteout(index
))
544 * Verifying directory index entries are not stale is expensive, so
545 * only verify stale dir index if NFS export is enabled.
547 if (d_is_dir(index
) && !ofs
->config
.nfs_export
)
551 * Directory index entries should have 'upper' xattr pointing to the
552 * real upper dir. Non-dir index entries are hardlinks to the upper
553 * real inode. For non-dir index, we can read the copy up origin xattr
554 * directly from the index dentry, but for dir index we first need to
555 * decode the upper directory.
557 upper
= ovl_index_upper(ofs
, index
);
558 if (IS_ERR_OR_NULL(upper
)) {
559 err
= PTR_ERR(upper
);
561 * Directory index entries with no 'upper' xattr need to be
562 * removed. When dir index entry has a stale 'upper' xattr,
563 * we assume that upper dir was removed and we treat the dir
564 * index as orphan entry that needs to be whited out.
573 err
= ovl_verify_fh(upper
, OVL_XATTR_ORIGIN
, fh
);
578 /* Check if non-dir index is orphan and don't warn before cleaning it */
579 if (!d_is_dir(index
) && d_inode(index
)->i_nlink
== 1) {
580 err
= ovl_check_origin_fh(ofs
, fh
, false, index
, &stack
);
584 if (ovl_get_nlink(origin
.dentry
, index
, 0) == 0)
594 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
595 index
, d_inode(index
)->i_mode
& S_IFMT
, err
);
599 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
600 index
, d_inode(index
)->i_mode
& S_IFMT
,
601 d_inode(index
)->i_nlink
);
606 static int ovl_get_index_name_fh(struct ovl_fh
*fh
, struct qstr
*name
)
610 n
= kcalloc(fh
->len
, 2, GFP_KERNEL
);
614 s
= bin2hex(n
, fh
, fh
->len
);
615 *name
= (struct qstr
) QSTR_INIT(n
, s
- n
);
622 * Lookup in indexdir for the index entry of a lower real inode or a copy up
623 * origin inode. The index entry name is the hex representation of the lower
626 * If the index dentry in negative, then either no lower aliases have been
627 * copied up yet, or aliases have been copied up in older kernels and are
630 * If the index dentry for a copy up origin inode is positive, but points
631 * to an inode different than the upper inode, then either the upper inode
632 * has been copied up and not indexed or it was indexed, but since then
633 * index dir was cleared. Either way, that index cannot be used to indentify
636 int ovl_get_index_name(struct dentry
*origin
, struct qstr
*name
)
641 fh
= ovl_encode_real_fh(origin
, false);
645 err
= ovl_get_index_name_fh(fh
, name
);
651 /* Lookup index by file handle for NFS export */
652 struct dentry
*ovl_get_index_fh(struct ovl_fs
*ofs
, struct ovl_fh
*fh
)
654 struct dentry
*index
;
658 err
= ovl_get_index_name_fh(fh
, &name
);
662 index
= lookup_one_len_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
665 if (PTR_ERR(index
) == -ENOENT
)
670 if (d_is_negative(index
))
672 else if (ovl_is_whiteout(index
))
674 else if (ovl_dentry_weird(index
))
683 struct dentry
*ovl_lookup_index(struct ovl_fs
*ofs
, struct dentry
*upper
,
684 struct dentry
*origin
, bool verify
)
686 struct dentry
*index
;
689 bool is_dir
= d_is_dir(origin
);
692 err
= ovl_get_index_name(origin
, &name
);
696 index
= lookup_one_len_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
698 err
= PTR_ERR(index
);
699 if (err
== -ENOENT
) {
703 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
704 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
705 d_inode(origin
)->i_ino
, name
.len
, name
.name
,
710 inode
= d_inode(index
);
711 if (d_is_negative(index
)) {
713 } else if (ovl_is_whiteout(index
) && !verify
) {
715 * When index lookup is called with !verify for decoding an
716 * overlay file handle, a whiteout index implies that decode
717 * should treat file handle as stale and no need to print a
721 index
= ERR_PTR(-ESTALE
);
723 } else if (ovl_dentry_weird(index
) || ovl_is_whiteout(index
) ||
724 ((inode
->i_mode
^ d_inode(origin
)->i_mode
) & S_IFMT
)) {
726 * Index should always be of the same file type as origin
727 * except for the case of a whiteout index. A whiteout
728 * index should only exist if all lower aliases have been
729 * unlinked, which means that finding a lower origin on lookup
730 * whose index is a whiteout should be treated as an error.
732 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
733 index
, d_inode(index
)->i_mode
& S_IFMT
,
734 d_inode(origin
)->i_mode
& S_IFMT
);
736 } else if (is_dir
&& verify
) {
738 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
743 /* Verify that dir index 'upper' xattr points to upper dir */
744 err
= ovl_verify_upper(index
, upper
, false);
746 if (err
== -ESTALE
) {
747 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
748 upper
, origin
, index
);
752 } else if (upper
&& d_inode(upper
) != inode
) {
766 index
= ERR_PTR(-EIO
);
771 * Returns next layer in stack starting from top.
772 * Returns -1 if this is the last layer.
774 int ovl_path_next(int idx
, struct dentry
*dentry
, struct path
*path
)
776 struct ovl_entry
*oe
= dentry
->d_fsdata
;
780 ovl_path_upper(dentry
, path
);
782 return oe
->numlower
? 1 : -1;
785 BUG_ON(idx
> oe
->numlower
);
786 path
->dentry
= oe
->lowerstack
[idx
- 1].dentry
;
787 path
->mnt
= oe
->lowerstack
[idx
- 1].layer
->mnt
;
789 return (idx
< oe
->numlower
) ? idx
+ 1 : -1;
792 /* Fix missing 'origin' xattr */
793 static int ovl_fix_origin(struct dentry
*dentry
, struct dentry
*lower
,
794 struct dentry
*upper
)
798 if (ovl_check_origin_xattr(upper
))
801 err
= ovl_want_write(dentry
);
805 err
= ovl_set_origin(dentry
, lower
, upper
);
807 err
= ovl_set_impure(dentry
->d_parent
, upper
->d_parent
);
809 ovl_drop_write(dentry
);
813 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
816 struct ovl_entry
*oe
;
817 const struct cred
*old_cred
;
818 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
819 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
820 struct ovl_entry
*roe
= dentry
->d_sb
->s_root
->d_fsdata
;
821 struct ovl_path
*stack
= NULL
, *origin_path
= NULL
;
822 struct dentry
*upperdir
, *upperdentry
= NULL
;
823 struct dentry
*origin
= NULL
;
824 struct dentry
*index
= NULL
;
825 unsigned int ctr
= 0;
826 struct inode
*inode
= NULL
;
827 bool upperopaque
= false;
828 char *upperredirect
= NULL
;
832 bool metacopy
= false;
833 struct ovl_lookup_data d
= {
835 .name
= dentry
->d_name
,
839 .last
= ofs
->config
.redirect_follow
? false : !poe
->numlower
,
844 if (dentry
->d_name
.len
> ofs
->namelen
)
845 return ERR_PTR(-ENAMETOOLONG
);
847 old_cred
= ovl_override_creds(dentry
->d_sb
);
848 upperdir
= ovl_dentry_upper(dentry
->d_parent
);
850 err
= ovl_lookup_layer(upperdir
, &d
, &upperdentry
);
854 if (upperdentry
&& unlikely(ovl_dentry_remote(upperdentry
))) {
859 if (upperdentry
&& !d
.is_dir
) {
860 unsigned int origin_ctr
= 0;
863 * Lookup copy up origin by decoding origin file handle.
864 * We may get a disconnected dentry, which is fine,
865 * because we only need to hold the origin inode in
866 * cache and use its inode number. We may even get a
867 * connected dentry, that is not under any of the lower
868 * layers root. That is also fine for using it's inode
869 * number - it's the same as if we held a reference
870 * to a dentry in lower layer that was moved under us.
872 err
= ovl_check_origin(ofs
, upperdentry
, &origin_path
,
883 upperredirect
= kstrdup(d
.redirect
, GFP_KERNEL
);
886 if (d
.redirect
[0] == '/')
889 upperopaque
= d
.opaque
;
892 if (!d
.stop
&& poe
->numlower
) {
894 stack
= kcalloc(ofs
->numlower
, sizeof(struct ovl_path
),
900 for (i
= 0; !d
.stop
&& i
< poe
->numlower
; i
++) {
901 struct ovl_path lower
= poe
->lowerstack
[i
];
903 if (!ofs
->config
.redirect_follow
)
904 d
.last
= i
== poe
->numlower
- 1;
906 d
.last
= lower
.layer
->idx
== roe
->numlower
;
908 err
= ovl_lookup_layer(lower
.dentry
, &d
, &this);
916 * If no origin fh is stored in upper of a merge dir, store fh
917 * of lower dir and set upper parent "impure".
919 if (upperdentry
&& !ctr
&& !ofs
->noxattr
&& d
.is_dir
) {
920 err
= ovl_fix_origin(dentry
, this, upperdentry
);
928 * When "verify_lower" feature is enabled, do not merge with a
929 * lower dir that does not match a stored origin xattr. In any
930 * case, only verified origin is used for index lookup.
932 * For non-dir dentry, if index=on, then ensure origin
933 * matches the dentry found using path based lookup,
934 * otherwise error out.
936 if (upperdentry
&& !ctr
&&
937 ((d
.is_dir
&& ovl_verify_lower(dentry
->d_sb
)) ||
938 (!d
.is_dir
&& ofs
->config
.index
&& origin_path
))) {
939 err
= ovl_verify_origin(upperdentry
, this, false);
952 * Do not store intermediate metacopy dentries in chain,
953 * except top most lower metacopy dentry
955 if (d
.metacopy
&& ctr
) {
960 stack
[ctr
].dentry
= this;
961 stack
[ctr
].layer
= lower
.layer
;
965 * Following redirects can have security consequences: it's like
966 * a symlink into the lower layer without the permission checks.
967 * This is only a problem if the upper layer is untrusted (e.g
968 * comes from an USB drive). This can allow a non-readable file
969 * or directory to become readable.
971 * Only following redirects when redirects are enabled disables
972 * this attack vector when not necessary.
975 if (d
.redirect
&& !ofs
->config
.redirect_follow
) {
976 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
984 if (d
.redirect
&& d
.redirect
[0] == '/' && poe
!= roe
) {
986 /* Find the current layer on the root dentry */
987 i
= lower
.layer
->idx
- 1;
993 * Found a metacopy dentry but did not find corresponding
1002 if (!ofs
->config
.metacopy
) {
1003 pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
1007 } else if (!d
.is_dir
&& upperdentry
&& !ctr
&& origin_path
) {
1008 if (WARN_ON(stack
!= NULL
)) {
1012 stack
= origin_path
;
1018 * Lookup index by lower inode and verify it matches upper inode.
1019 * We only trust dir index if we verified that lower dir matches
1020 * origin, otherwise dir index entries may be inconsistent and we
1023 * For non-dir upper metacopy dentry, we already set "origin" if we
1024 * verified that lower matched upper origin. If upper origin was
1025 * not present (because lower layer did not support fh encode/decode),
1026 * or indexing is not enabled, do not set "origin" and skip looking up
1027 * index. This case should be handled in same way as a non-dir upper
1028 * without ORIGIN is handled.
1030 * Always lookup index of non-dir non-metacopy and non-upper.
1032 if (ctr
&& (!upperdentry
|| (!d
.is_dir
&& !metacopy
)))
1033 origin
= stack
[0].dentry
;
1035 if (origin
&& ovl_indexdir(dentry
->d_sb
) &&
1036 (!d
.is_dir
|| ovl_index_all(dentry
->d_sb
))) {
1037 index
= ovl_lookup_index(ofs
, upperdentry
, origin
, true);
1038 if (IS_ERR(index
)) {
1039 err
= PTR_ERR(index
);
1045 oe
= ovl_alloc_entry(ctr
);
1050 memcpy(oe
->lowerstack
, stack
, sizeof(struct ovl_path
) * ctr
);
1051 dentry
->d_fsdata
= oe
;
1054 ovl_dentry_set_opaque(dentry
);
1057 ovl_dentry_set_upper_alias(dentry
);
1059 upperdentry
= dget(index
);
1060 upperredirect
= ovl_get_redirect_xattr(upperdentry
, 0);
1061 if (IS_ERR(upperredirect
)) {
1062 err
= PTR_ERR(upperredirect
);
1063 upperredirect
= NULL
;
1068 if (upperdentry
|| ctr
) {
1069 struct ovl_inode_params oip
= {
1070 .upperdentry
= upperdentry
,
1074 .redirect
= upperredirect
,
1075 .lowerdata
= (ctr
> 1 && !d
.is_dir
) ?
1076 stack
[ctr
- 1].dentry
: NULL
,
1079 inode
= ovl_get_inode(dentry
->d_sb
, &oip
);
1080 err
= PTR_ERR(inode
);
1085 revert_creds(old_cred
);
1087 dput(origin_path
->dentry
);
1093 return d_splice_alias(inode
, dentry
);
1096 dentry
->d_fsdata
= NULL
;
1100 for (i
= 0; i
< ctr
; i
++)
1101 dput(stack
[i
].dentry
);
1105 dput(origin_path
->dentry
);
1109 kfree(upperredirect
);
1112 revert_creds(old_cred
);
1113 return ERR_PTR(err
);
1116 bool ovl_lower_positive(struct dentry
*dentry
)
1118 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
1119 const struct qstr
*name
= &dentry
->d_name
;
1120 const struct cred
*old_cred
;
1122 bool positive
= false;
1126 * If dentry is negative, then lower is positive iff this is a
1129 if (!dentry
->d_inode
)
1130 return ovl_dentry_is_opaque(dentry
);
1132 /* Negative upper -> positive lower */
1133 if (!ovl_dentry_upper(dentry
))
1136 old_cred
= ovl_override_creds(dentry
->d_sb
);
1137 /* Positive upper -> have to look up lower to see whether it exists */
1138 for (i
= 0; !done
&& !positive
&& i
< poe
->numlower
; i
++) {
1139 struct dentry
*this;
1140 struct dentry
*lowerdir
= poe
->lowerstack
[i
].dentry
;
1142 this = lookup_one_len_unlocked(name
->name
, lowerdir
,
1145 switch (PTR_ERR(this)) {
1152 * Assume something is there, we just couldn't
1159 if (this->d_inode
) {
1160 positive
= !ovl_is_whiteout(this);
1166 revert_creds(old_cred
);