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
{
30 static int ovl_check_redirect(struct dentry
*dentry
, struct ovl_lookup_data
*d
,
31 size_t prelen
, const char *post
)
36 buf
= ovl_get_redirect_xattr(dentry
, prelen
+ strlen(post
));
37 if (IS_ERR_OR_NULL(buf
))
42 * One of the ancestor path elements in an absolute path
43 * lookup in ovl_lookup_layer() could have been opaque and
44 * that will stop further lookup in lower layers (d->stop=true)
45 * But we have found an absolute redirect in decendant path
46 * element and that should force continue lookup in lower
47 * layers (reset d->stop).
51 res
= strlen(buf
) + 1;
52 memmove(buf
+ prelen
, buf
, res
);
53 memcpy(buf
, d
->name
.name
, prelen
);
59 d
->name
.name
= d
->redirect
;
60 d
->name
.len
= strlen(d
->redirect
);
65 static int ovl_acceptable(void *ctx
, struct dentry
*dentry
)
68 * A non-dir origin may be disconnected, which is fine, because
69 * we only need it for its unique inode number.
71 if (!d_is_dir(dentry
))
74 /* Don't decode a deleted empty directory */
75 if (d_unhashed(dentry
))
78 /* Check if directory belongs to the layer we are decoding from */
79 return is_subdir(dentry
, ((struct vfsmount
*)ctx
)->mnt_root
);
83 * Check validity of an overlay file handle buffer.
85 * Return 0 for a valid file handle.
86 * Return -ENODATA for "origin unknown".
87 * Return <0 for an invalid file handle.
89 int ovl_check_fh_len(struct ovl_fh
*fh
, int fh_len
)
91 if (fh_len
< sizeof(struct ovl_fh
) || fh_len
< fh
->len
)
94 if (fh
->magic
!= OVL_FH_MAGIC
)
97 /* Treat larger version and unknown flags as "origin unknown" */
98 if (fh
->version
> OVL_FH_VERSION
|| fh
->flags
& ~OVL_FH_FLAG_ALL
)
101 /* Treat endianness mismatch as "origin unknown" */
102 if (!(fh
->flags
& OVL_FH_FLAG_ANY_ENDIAN
) &&
103 (fh
->flags
& OVL_FH_FLAG_BIG_ENDIAN
) != OVL_FH_FLAG_CPU_ENDIAN
)
109 static struct ovl_fh
*ovl_get_fh(struct dentry
*dentry
, const char *name
)
112 struct ovl_fh
*fh
= NULL
;
114 res
= vfs_getxattr(dentry
, name
, NULL
, 0);
116 if (res
== -ENODATA
|| res
== -EOPNOTSUPP
)
120 /* Zero size value means "copied up but origin unknown" */
124 fh
= kzalloc(res
, GFP_KERNEL
);
126 return ERR_PTR(-ENOMEM
);
128 res
= vfs_getxattr(dentry
, name
, fh
, res
);
132 err
= ovl_check_fh_len(fh
, res
);
146 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res
);
149 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res
, fh
);
153 struct dentry
*ovl_decode_real_fh(struct ovl_fh
*fh
, struct vfsmount
*mnt
,
160 * Make sure that the stored uuid matches the uuid of the lower
161 * layer where file handle will be decoded.
163 if (!uuid_equal(&fh
->uuid
, &mnt
->mnt_sb
->s_uuid
))
166 bytes
= (fh
->len
- offsetof(struct ovl_fh
, fid
));
167 real
= exportfs_decode_fh(mnt
, (struct fid
*)fh
->fid
,
168 bytes
>> 2, (int)fh
->type
,
169 connected
? ovl_acceptable
: NULL
, mnt
);
172 * Treat stale file handle to lower file as "origin unknown".
173 * upper file handle could become stale when upper file is
174 * unlinked and this information is needed to handle stale
175 * index entries correctly.
177 if (real
== ERR_PTR(-ESTALE
) &&
178 !(fh
->flags
& OVL_FH_FLAG_PATH_UPPER
))
183 if (ovl_dentry_weird(real
)) {
191 static bool ovl_is_opaquedir(struct dentry
*dentry
)
193 return ovl_check_dir_xattr(dentry
, OVL_XATTR_OPAQUE
);
196 static int ovl_lookup_single(struct dentry
*base
, struct ovl_lookup_data
*d
,
197 const char *name
, unsigned int namelen
,
198 size_t prelen
, const char *post
,
203 bool last_element
= !post
[0];
205 this = lookup_one_len_unlocked(name
, base
, namelen
);
209 if (err
== -ENOENT
|| err
== -ENAMETOOLONG
)
216 if (ovl_dentry_weird(this)) {
217 /* Don't support traversing automounts and other weirdness */
221 if (ovl_is_whiteout(this)) {
222 d
->stop
= d
->opaque
= true;
226 * This dentry should be a regular file if previous layer lookup
227 * found a metacopy dentry.
229 if (last_element
&& d
->metacopy
&& !d_is_reg(this)) {
233 if (!d_can_lookup(this)) {
234 if (d
->is_dir
|| !last_element
) {
238 err
= ovl_check_metacopy_xattr(this);
243 d
->stop
= !d
->metacopy
;
244 if (!d
->metacopy
|| d
->last
)
252 if (ovl_is_opaquedir(this)) {
259 err
= ovl_check_redirect(this, d
, prelen
, post
);
276 static int ovl_lookup_layer(struct dentry
*base
, struct ovl_lookup_data
*d
,
279 /* Counting down from the end, since the prefix can change */
280 size_t rem
= d
->name
.len
- 1;
281 struct dentry
*dentry
= NULL
;
284 if (d
->name
.name
[0] != '/')
285 return ovl_lookup_single(base
, d
, d
->name
.name
, d
->name
.len
,
288 while (!IS_ERR_OR_NULL(base
) && d_can_lookup(base
)) {
289 const char *s
= d
->name
.name
+ d
->name
.len
- rem
;
290 const char *next
= strchrnul(s
, '/');
291 size_t thislen
= next
- s
;
294 /* Verify we did not go off the rails */
295 if (WARN_ON(s
[-1] != '/'))
298 err
= ovl_lookup_single(base
, d
, s
, thislen
,
299 d
->name
.len
- rem
, next
, &base
);
309 if (WARN_ON(rem
>= d
->name
.len
))
317 int ovl_check_origin_fh(struct ovl_fs
*ofs
, struct ovl_fh
*fh
, bool connected
,
318 struct dentry
*upperdentry
, struct ovl_path
**stackp
)
320 struct dentry
*origin
= NULL
;
323 for (i
= 0; i
< ofs
->numlower
; i
++) {
324 origin
= ovl_decode_real_fh(fh
, ofs
->lower_layers
[i
].mnt
,
332 else if (IS_ERR(origin
))
333 return PTR_ERR(origin
);
335 if (upperdentry
&& !ovl_is_whiteout(upperdentry
) &&
336 ((d_inode(origin
)->i_mode
^ d_inode(upperdentry
)->i_mode
) & S_IFMT
))
340 *stackp
= kmalloc(sizeof(struct ovl_path
), GFP_KERNEL
);
345 **stackp
= (struct ovl_path
){
347 .layer
= &ofs
->lower_layers
[i
]
353 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
354 upperdentry
, d_inode(upperdentry
)->i_mode
& S_IFMT
,
355 d_inode(origin
)->i_mode
& S_IFMT
);
360 static int ovl_check_origin(struct ovl_fs
*ofs
, struct dentry
*upperdentry
,
361 struct ovl_path
**stackp
, unsigned int *ctrp
)
363 struct ovl_fh
*fh
= ovl_get_fh(upperdentry
, OVL_XATTR_ORIGIN
);
366 if (IS_ERR_OR_NULL(fh
))
369 err
= ovl_check_origin_fh(ofs
, fh
, false, upperdentry
, stackp
);
386 * Verify that @fh matches the file handle stored in xattr @name.
387 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
389 static int ovl_verify_fh(struct dentry
*dentry
, const char *name
,
390 const struct ovl_fh
*fh
)
392 struct ovl_fh
*ofh
= ovl_get_fh(dentry
, name
);
401 if (fh
->len
!= ofh
->len
|| memcmp(fh
, ofh
, fh
->len
))
409 * Verify that @real dentry matches the file handle stored in xattr @name.
411 * If @set is true and there is no stored file handle, encode @real and store
412 * file handle in xattr @name.
414 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
416 int ovl_verify_set_fh(struct dentry
*dentry
, const char *name
,
417 struct dentry
*real
, bool is_upper
, bool set
)
423 fh
= ovl_encode_real_fh(real
, is_upper
);
428 err
= ovl_verify_fh(dentry
, name
, fh
);
429 if (set
&& err
== -ENODATA
)
430 err
= ovl_do_setxattr(dentry
, name
, fh
, fh
->len
, 0);
439 inode
= d_inode(real
);
440 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
441 is_upper
? "upper" : "origin", real
,
442 inode
? inode
->i_ino
: 0, err
);
446 /* Get upper dentry from index */
447 struct dentry
*ovl_index_upper(struct ovl_fs
*ofs
, struct dentry
*index
)
450 struct dentry
*upper
;
452 if (!d_is_dir(index
))
455 fh
= ovl_get_fh(index
, OVL_XATTR_UPPER
);
456 if (IS_ERR_OR_NULL(fh
))
459 upper
= ovl_decode_real_fh(fh
, ofs
->upper_mnt
, true);
462 if (IS_ERR_OR_NULL(upper
))
463 return upper
?: ERR_PTR(-ESTALE
);
465 if (!d_is_dir(upper
)) {
466 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
469 return ERR_PTR(-EIO
);
475 /* Is this a leftover from create/whiteout of directory index entry? */
476 static bool ovl_is_temp_index(struct dentry
*index
)
478 return index
->d_name
.name
[0] == '#';
482 * Verify that an index entry name matches the origin file handle stored in
483 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
484 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
486 int ovl_verify_index(struct ovl_fs
*ofs
, struct dentry
*index
)
488 struct ovl_fh
*fh
= NULL
;
490 struct ovl_path origin
= { };
491 struct ovl_path
*stack
= &origin
;
492 struct dentry
*upper
= NULL
;
498 /* Cleanup leftover from index create/cleanup attempt */
500 if (ovl_is_temp_index(index
))
504 if (index
->d_name
.len
< sizeof(struct ovl_fh
)*2)
508 len
= index
->d_name
.len
/ 2;
509 fh
= kzalloc(len
, GFP_KERNEL
);
514 if (hex2bin((u8
*)fh
, index
->d_name
.name
, len
))
517 err
= ovl_check_fh_len(fh
, len
);
522 * Whiteout index entries are used as an indication that an exported
523 * overlay file handle should be treated as stale (i.e. after unlink
524 * of the overlay inode). These entries contain no origin xattr.
526 if (ovl_is_whiteout(index
))
530 * Verifying directory index entries are not stale is expensive, so
531 * only verify stale dir index if NFS export is enabled.
533 if (d_is_dir(index
) && !ofs
->config
.nfs_export
)
537 * Directory index entries should have 'upper' xattr pointing to the
538 * real upper dir. Non-dir index entries are hardlinks to the upper
539 * real inode. For non-dir index, we can read the copy up origin xattr
540 * directly from the index dentry, but for dir index we first need to
541 * decode the upper directory.
543 upper
= ovl_index_upper(ofs
, index
);
544 if (IS_ERR_OR_NULL(upper
)) {
545 err
= PTR_ERR(upper
);
547 * Directory index entries with no 'upper' xattr need to be
548 * removed. When dir index entry has a stale 'upper' xattr,
549 * we assume that upper dir was removed and we treat the dir
550 * index as orphan entry that needs to be whited out.
559 err
= ovl_verify_fh(upper
, OVL_XATTR_ORIGIN
, fh
);
564 /* Check if non-dir index is orphan and don't warn before cleaning it */
565 if (!d_is_dir(index
) && d_inode(index
)->i_nlink
== 1) {
566 err
= ovl_check_origin_fh(ofs
, fh
, false, index
, &stack
);
570 if (ovl_get_nlink(origin
.dentry
, index
, 0) == 0)
580 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
581 index
, d_inode(index
)->i_mode
& S_IFMT
, err
);
585 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
586 index
, d_inode(index
)->i_mode
& S_IFMT
,
587 d_inode(index
)->i_nlink
);
592 static int ovl_get_index_name_fh(struct ovl_fh
*fh
, struct qstr
*name
)
596 n
= kcalloc(fh
->len
, 2, GFP_KERNEL
);
600 s
= bin2hex(n
, fh
, fh
->len
);
601 *name
= (struct qstr
) QSTR_INIT(n
, s
- n
);
608 * Lookup in indexdir for the index entry of a lower real inode or a copy up
609 * origin inode. The index entry name is the hex representation of the lower
612 * If the index dentry in negative, then either no lower aliases have been
613 * copied up yet, or aliases have been copied up in older kernels and are
616 * If the index dentry for a copy up origin inode is positive, but points
617 * to an inode different than the upper inode, then either the upper inode
618 * has been copied up and not indexed or it was indexed, but since then
619 * index dir was cleared. Either way, that index cannot be used to indentify
622 int ovl_get_index_name(struct dentry
*origin
, struct qstr
*name
)
627 fh
= ovl_encode_real_fh(origin
, false);
631 err
= ovl_get_index_name_fh(fh
, name
);
637 /* Lookup index by file handle for NFS export */
638 struct dentry
*ovl_get_index_fh(struct ovl_fs
*ofs
, struct ovl_fh
*fh
)
640 struct dentry
*index
;
644 err
= ovl_get_index_name_fh(fh
, &name
);
648 index
= lookup_one_len_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
651 if (PTR_ERR(index
) == -ENOENT
)
656 if (d_is_negative(index
))
658 else if (ovl_is_whiteout(index
))
660 else if (ovl_dentry_weird(index
))
669 struct dentry
*ovl_lookup_index(struct ovl_fs
*ofs
, struct dentry
*upper
,
670 struct dentry
*origin
, bool verify
)
672 struct dentry
*index
;
675 bool is_dir
= d_is_dir(origin
);
678 err
= ovl_get_index_name(origin
, &name
);
682 index
= lookup_one_len_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
684 err
= PTR_ERR(index
);
685 if (err
== -ENOENT
) {
689 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
690 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
691 d_inode(origin
)->i_ino
, name
.len
, name
.name
,
696 inode
= d_inode(index
);
697 if (d_is_negative(index
)) {
699 } else if (ovl_is_whiteout(index
) && !verify
) {
701 * When index lookup is called with !verify for decoding an
702 * overlay file handle, a whiteout index implies that decode
703 * should treat file handle as stale and no need to print a
707 index
= ERR_PTR(-ESTALE
);
709 } else if (ovl_dentry_weird(index
) || ovl_is_whiteout(index
) ||
710 ((inode
->i_mode
^ d_inode(origin
)->i_mode
) & S_IFMT
)) {
712 * Index should always be of the same file type as origin
713 * except for the case of a whiteout index. A whiteout
714 * index should only exist if all lower aliases have been
715 * unlinked, which means that finding a lower origin on lookup
716 * whose index is a whiteout should be treated as an error.
718 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
719 index
, d_inode(index
)->i_mode
& S_IFMT
,
720 d_inode(origin
)->i_mode
& S_IFMT
);
722 } else if (is_dir
&& verify
) {
724 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
729 /* Verify that dir index 'upper' xattr points to upper dir */
730 err
= ovl_verify_upper(index
, upper
, false);
732 if (err
== -ESTALE
) {
733 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
734 upper
, origin
, index
);
738 } else if (upper
&& d_inode(upper
) != inode
) {
752 index
= ERR_PTR(-EIO
);
757 * Returns next layer in stack starting from top.
758 * Returns -1 if this is the last layer.
760 int ovl_path_next(int idx
, struct dentry
*dentry
, struct path
*path
)
762 struct ovl_entry
*oe
= dentry
->d_fsdata
;
766 ovl_path_upper(dentry
, path
);
768 return oe
->numlower
? 1 : -1;
771 BUG_ON(idx
> oe
->numlower
);
772 path
->dentry
= oe
->lowerstack
[idx
- 1].dentry
;
773 path
->mnt
= oe
->lowerstack
[idx
- 1].layer
->mnt
;
775 return (idx
< oe
->numlower
) ? idx
+ 1 : -1;
778 /* Fix missing 'origin' xattr */
779 static int ovl_fix_origin(struct dentry
*dentry
, struct dentry
*lower
,
780 struct dentry
*upper
)
784 if (ovl_check_origin_xattr(upper
))
787 err
= ovl_want_write(dentry
);
791 err
= ovl_set_origin(dentry
, lower
, upper
);
793 err
= ovl_set_impure(dentry
->d_parent
, upper
->d_parent
);
795 ovl_drop_write(dentry
);
799 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
802 struct ovl_entry
*oe
;
803 const struct cred
*old_cred
;
804 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
805 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
806 struct ovl_entry
*roe
= dentry
->d_sb
->s_root
->d_fsdata
;
807 struct ovl_path
*stack
= NULL
, *origin_path
= NULL
;
808 struct dentry
*upperdir
, *upperdentry
= NULL
;
809 struct dentry
*origin
= NULL
;
810 struct dentry
*index
= NULL
;
811 unsigned int ctr
= 0;
812 struct inode
*inode
= NULL
;
813 bool upperopaque
= false;
814 char *upperredirect
= NULL
;
818 bool metacopy
= false;
819 struct ovl_lookup_data d
= {
820 .name
= dentry
->d_name
,
824 .last
= ofs
->config
.redirect_follow
? false : !poe
->numlower
,
829 if (dentry
->d_name
.len
> ofs
->namelen
)
830 return ERR_PTR(-ENAMETOOLONG
);
832 old_cred
= ovl_override_creds(dentry
->d_sb
);
833 upperdir
= ovl_dentry_upper(dentry
->d_parent
);
835 err
= ovl_lookup_layer(upperdir
, &d
, &upperdentry
);
839 if (upperdentry
&& unlikely(ovl_dentry_remote(upperdentry
))) {
844 if (upperdentry
&& !d
.is_dir
) {
845 unsigned int origin_ctr
= 0;
848 * Lookup copy up origin by decoding origin file handle.
849 * We may get a disconnected dentry, which is fine,
850 * because we only need to hold the origin inode in
851 * cache and use its inode number. We may even get a
852 * connected dentry, that is not under any of the lower
853 * layers root. That is also fine for using it's inode
854 * number - it's the same as if we held a reference
855 * to a dentry in lower layer that was moved under us.
857 err
= ovl_check_origin(ofs
, upperdentry
, &origin_path
,
868 upperredirect
= kstrdup(d
.redirect
, GFP_KERNEL
);
871 if (d
.redirect
[0] == '/')
874 upperopaque
= d
.opaque
;
877 if (!d
.stop
&& poe
->numlower
) {
879 stack
= kcalloc(ofs
->numlower
, sizeof(struct ovl_path
),
885 for (i
= 0; !d
.stop
&& i
< poe
->numlower
; i
++) {
886 struct ovl_path lower
= poe
->lowerstack
[i
];
888 if (!ofs
->config
.redirect_follow
)
889 d
.last
= i
== poe
->numlower
- 1;
891 d
.last
= lower
.layer
->idx
== roe
->numlower
;
893 err
= ovl_lookup_layer(lower
.dentry
, &d
, &this);
901 * If no origin fh is stored in upper of a merge dir, store fh
902 * of lower dir and set upper parent "impure".
904 if (upperdentry
&& !ctr
&& !ofs
->noxattr
&& d
.is_dir
) {
905 err
= ovl_fix_origin(dentry
, this, upperdentry
);
913 * When "verify_lower" feature is enabled, do not merge with a
914 * lower dir that does not match a stored origin xattr. In any
915 * case, only verified origin is used for index lookup.
917 * For non-dir dentry, if index=on, then ensure origin
918 * matches the dentry found using path based lookup,
919 * otherwise error out.
921 if (upperdentry
&& !ctr
&&
922 ((d
.is_dir
&& ovl_verify_lower(dentry
->d_sb
)) ||
923 (!d
.is_dir
&& ofs
->config
.index
&& origin_path
))) {
924 err
= ovl_verify_origin(upperdentry
, this, false);
937 * Do not store intermediate metacopy dentries in chain,
938 * except top most lower metacopy dentry
940 if (d
.metacopy
&& ctr
) {
945 stack
[ctr
].dentry
= this;
946 stack
[ctr
].layer
= lower
.layer
;
950 * Following redirects can have security consequences: it's like
951 * a symlink into the lower layer without the permission checks.
952 * This is only a problem if the upper layer is untrusted (e.g
953 * comes from an USB drive). This can allow a non-readable file
954 * or directory to become readable.
956 * Only following redirects when redirects are enabled disables
957 * this attack vector when not necessary.
960 if (d
.redirect
&& !ofs
->config
.redirect_follow
) {
961 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
969 if (d
.redirect
&& d
.redirect
[0] == '/' && poe
!= roe
) {
971 /* Find the current layer on the root dentry */
972 i
= lower
.layer
->idx
- 1;
978 * Found a metacopy dentry but did not find corresponding
987 if (!ofs
->config
.metacopy
) {
988 pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
992 } else if (!d
.is_dir
&& upperdentry
&& !ctr
&& origin_path
) {
993 if (WARN_ON(stack
!= NULL
)) {
1003 * Lookup index by lower inode and verify it matches upper inode.
1004 * We only trust dir index if we verified that lower dir matches
1005 * origin, otherwise dir index entries may be inconsistent and we
1008 * For non-dir upper metacopy dentry, we already set "origin" if we
1009 * verified that lower matched upper origin. If upper origin was
1010 * not present (because lower layer did not support fh encode/decode),
1011 * or indexing is not enabled, do not set "origin" and skip looking up
1012 * index. This case should be handled in same way as a non-dir upper
1013 * without ORIGIN is handled.
1015 * Always lookup index of non-dir non-metacopy and non-upper.
1017 if (ctr
&& (!upperdentry
|| (!d
.is_dir
&& !metacopy
)))
1018 origin
= stack
[0].dentry
;
1020 if (origin
&& ovl_indexdir(dentry
->d_sb
) &&
1021 (!d
.is_dir
|| ovl_index_all(dentry
->d_sb
))) {
1022 index
= ovl_lookup_index(ofs
, upperdentry
, origin
, true);
1023 if (IS_ERR(index
)) {
1024 err
= PTR_ERR(index
);
1030 oe
= ovl_alloc_entry(ctr
);
1035 memcpy(oe
->lowerstack
, stack
, sizeof(struct ovl_path
) * ctr
);
1036 dentry
->d_fsdata
= oe
;
1039 ovl_dentry_set_opaque(dentry
);
1042 ovl_dentry_set_upper_alias(dentry
);
1044 upperdentry
= dget(index
);
1045 upperredirect
= ovl_get_redirect_xattr(upperdentry
, 0);
1046 if (IS_ERR(upperredirect
)) {
1047 err
= PTR_ERR(upperredirect
);
1048 upperredirect
= NULL
;
1053 if (upperdentry
|| ctr
) {
1054 struct ovl_inode_params oip
= {
1055 .upperdentry
= upperdentry
,
1059 .redirect
= upperredirect
,
1060 .lowerdata
= (ctr
> 1 && !d
.is_dir
) ?
1061 stack
[ctr
- 1].dentry
: NULL
,
1064 inode
= ovl_get_inode(dentry
->d_sb
, &oip
);
1065 err
= PTR_ERR(inode
);
1070 revert_creds(old_cred
);
1072 dput(origin_path
->dentry
);
1078 return d_splice_alias(inode
, dentry
);
1081 dentry
->d_fsdata
= NULL
;
1085 for (i
= 0; i
< ctr
; i
++)
1086 dput(stack
[i
].dentry
);
1090 dput(origin_path
->dentry
);
1094 kfree(upperredirect
);
1097 revert_creds(old_cred
);
1098 return ERR_PTR(err
);
1101 bool ovl_lower_positive(struct dentry
*dentry
)
1103 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
1104 const struct qstr
*name
= &dentry
->d_name
;
1105 const struct cred
*old_cred
;
1107 bool positive
= false;
1111 * If dentry is negative, then lower is positive iff this is a
1114 if (!dentry
->d_inode
)
1115 return ovl_dentry_is_opaque(dentry
);
1117 /* Negative upper -> positive lower */
1118 if (!ovl_dentry_upper(dentry
))
1121 old_cred
= ovl_override_creds(dentry
->d_sb
);
1122 /* Positive upper -> have to look up lower to see whether it exists */
1123 for (i
= 0; !done
&& !positive
&& i
< poe
->numlower
; i
++) {
1124 struct dentry
*this;
1125 struct dentry
*lowerdir
= poe
->lowerstack
[i
].dentry
;
1127 this = lookup_one_len_unlocked(name
->name
, lowerdir
,
1130 switch (PTR_ERR(this)) {
1137 * Assume something is there, we just couldn't
1144 if (this->d_inode
) {
1145 positive
= !ovl_is_whiteout(this);
1151 revert_creds(old_cred
);