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;
29 static int ovl_check_redirect(struct path *path, struct ovl_lookup_data *d,
30 size_t prelen, const char *post)
34 struct ovl_fs *ofs = OVL_FS(d->sb);
36 buf = ovl_get_redirect_xattr(ofs, path, 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 descendant 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_fb_len(struct ovl_fb *fb, int fb_len)
91 if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len)
94 if (fb->magic != OVL_FH_MAGIC)
97 /* Treat larger version and unknown flags as "origin unknown" */
98 if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL)
101 /* Treat endianness mismatch as "origin unknown" */
102 if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
103 (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
109 static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *upperdentry,
113 struct ovl_fh *fh = NULL;
115 res = ovl_getxattr_upper(ofs, upperdentry, ox, NULL, 0);
117 if (res == -ENODATA || res == -EOPNOTSUPP)
121 /* Zero size value means "copied up but origin unknown" */
125 fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
127 return ERR_PTR(-ENOMEM);
129 res = ovl_getxattr_upper(ofs, upperdentry, ox, fh->buf, res);
133 err = ovl_check_fb_len(&fh->fb, res);
147 pr_warn_ratelimited("failed to get origin (%i)\n", res);
150 pr_warn_ratelimited("invalid origin (%*phN)\n", res, fh);
154 struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
155 struct vfsmount *mnt, bool connected)
160 if (!capable(CAP_DAC_READ_SEARCH))
164 * Make sure that the stored uuid matches the uuid of the lower
165 * layer where file handle will be decoded.
166 * In case of uuid=off option just make sure that stored uuid is null.
168 if (ofs->config.uuid ? !uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid) :
169 !uuid_is_null(&fh->fb.uuid))
172 bytes = (fh->fb.len - offsetof(struct ovl_fb, fid));
173 real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid,
174 bytes >> 2, (int)fh->fb.type,
175 connected ? ovl_acceptable : NULL, mnt);
178 * Treat stale file handle to lower file as "origin unknown".
179 * upper file handle could become stale when upper file is
180 * unlinked and this information is needed to handle stale
181 * index entries correctly.
183 if (real == ERR_PTR(-ESTALE) &&
184 !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER))
189 if (ovl_dentry_weird(real)) {
197 static bool ovl_is_opaquedir(struct ovl_fs *ofs, struct path *path)
199 return ovl_path_check_dir_xattr(ofs, path, OVL_XATTR_OPAQUE);
202 static struct dentry *ovl_lookup_positive_unlocked(struct ovl_lookup_data *d,
204 struct dentry *base, int len,
207 struct dentry *ret = lookup_one_unlocked(mnt_user_ns(d->mnt), name, base, len);
209 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
210 if (drop_negative && ret->d_lockref.count == 1) {
211 spin_lock(&ret->d_lock);
212 /* Recheck condition under lock */
213 if (d_is_negative(ret) && ret->d_lockref.count == 1)
215 spin_unlock(&ret->d_lock);
218 ret = ERR_PTR(-ENOENT);
223 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
224 const char *name, unsigned int namelen,
225 size_t prelen, const char *post,
226 struct dentry **ret, bool drop_negative)
231 bool last_element = !post[0];
233 this = ovl_lookup_positive_unlocked(d, name, base, namelen, drop_negative);
237 if (err == -ENOENT || err == -ENAMETOOLONG)
242 if (ovl_dentry_weird(this)) {
243 /* Don't support traversing automounts and other weirdness */
247 if (ovl_is_whiteout(this)) {
248 d->stop = d->opaque = true;
252 * This dentry should be a regular file if previous layer lookup
253 * found a metacopy dentry.
255 if (last_element && d->metacopy && !d_is_reg(this)) {
262 if (!d_can_lookup(this)) {
263 if (d->is_dir || !last_element) {
267 err = ovl_check_metacopy_xattr(OVL_FS(d->sb), &path);
272 d->stop = !d->metacopy;
273 if (!d->metacopy || d->last)
276 if (ovl_lookup_trap_inode(d->sb, this)) {
277 /* Caught in a trap of overlapping layers */
287 if (ovl_is_opaquedir(OVL_FS(d->sb), &path)) {
294 err = ovl_check_redirect(&path, d, prelen, post);
311 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
312 struct dentry **ret, bool drop_negative)
314 /* Counting down from the end, since the prefix can change */
315 size_t rem = d->name.len - 1;
316 struct dentry *dentry = NULL;
319 if (d->name.name[0] != '/')
320 return ovl_lookup_single(base, d, d->name.name, d->name.len,
321 0, "", ret, drop_negative);
323 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
324 const char *s = d->name.name + d->name.len - rem;
325 const char *next = strchrnul(s, '/');
326 size_t thislen = next - s;
329 /* Verify we did not go off the rails */
330 if (WARN_ON(s[-1] != '/'))
333 err = ovl_lookup_single(base, d, s, thislen,
334 d->name.len - rem, next, &base,
345 if (WARN_ON(rem >= d->name.len))
353 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
354 struct dentry *upperdentry, struct ovl_path **stackp)
356 struct dentry *origin = NULL;
359 for (i = 1; i < ofs->numlayer; i++) {
361 * If lower fs uuid is not unique among lower fs we cannot match
364 if (ofs->layers[i].fsid &&
365 ofs->layers[i].fs->bad_uuid)
368 origin = ovl_decode_real_fh(ofs, fh, ofs->layers[i].mnt,
376 else if (IS_ERR(origin))
377 return PTR_ERR(origin);
379 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
380 inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode))
384 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
389 **stackp = (struct ovl_path){
391 .layer = &ofs->layers[i]
397 pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
398 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
399 d_inode(origin)->i_mode & S_IFMT);
404 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
405 struct ovl_path **stackp)
407 struct ovl_fh *fh = ovl_get_fh(ofs, upperdentry, OVL_XATTR_ORIGIN);
410 if (IS_ERR_OR_NULL(fh))
413 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
426 * Verify that @fh matches the file handle stored in xattr @name.
427 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
429 static int ovl_verify_fh(struct ovl_fs *ofs, struct dentry *dentry,
430 enum ovl_xattr ox, const struct ovl_fh *fh)
432 struct ovl_fh *ofh = ovl_get_fh(ofs, dentry, ox);
441 if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len))
449 * Verify that @real dentry matches the file handle stored in xattr @name.
451 * If @set is true and there is no stored file handle, encode @real and store
452 * file handle in xattr @name.
454 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
456 int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
457 enum ovl_xattr ox, struct dentry *real, bool is_upper,
464 fh = ovl_encode_real_fh(ofs, real, is_upper);
471 err = ovl_verify_fh(ofs, dentry, ox, fh);
472 if (set && err == -ENODATA)
473 err = ovl_setxattr(ofs, dentry, ox, fh->buf, fh->fb.len);
482 inode = d_inode(real);
483 pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n",
484 is_upper ? "upper" : "origin", real,
485 inode ? inode->i_ino : 0, err);
489 /* Get upper dentry from index */
490 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
493 struct dentry *upper;
495 if (!d_is_dir(index))
498 fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER);
499 if (IS_ERR_OR_NULL(fh))
502 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true);
505 if (IS_ERR_OR_NULL(upper))
506 return upper ?: ERR_PTR(-ESTALE);
508 if (!d_is_dir(upper)) {
509 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
512 return ERR_PTR(-EIO);
519 * Verify that an index entry name matches the origin file handle stored in
520 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
521 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
523 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
525 struct ovl_fh *fh = NULL;
527 struct ovl_path origin = { };
528 struct ovl_path *stack = &origin;
529 struct dentry *upper = NULL;
536 if (index->d_name.len < sizeof(struct ovl_fb)*2)
540 len = index->d_name.len / 2;
541 fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
546 if (hex2bin(fh->buf, index->d_name.name, len))
549 err = ovl_check_fb_len(&fh->fb, len);
554 * Whiteout index entries are used as an indication that an exported
555 * overlay file handle should be treated as stale (i.e. after unlink
556 * of the overlay inode). These entries contain no origin xattr.
558 if (ovl_is_whiteout(index))
562 * Verifying directory index entries are not stale is expensive, so
563 * only verify stale dir index if NFS export is enabled.
565 if (d_is_dir(index) && !ofs->config.nfs_export)
569 * Directory index entries should have 'upper' xattr pointing to the
570 * real upper dir. Non-dir index entries are hardlinks to the upper
571 * real inode. For non-dir index, we can read the copy up origin xattr
572 * directly from the index dentry, but for dir index we first need to
573 * decode the upper directory.
575 upper = ovl_index_upper(ofs, index);
576 if (IS_ERR_OR_NULL(upper)) {
577 err = PTR_ERR(upper);
579 * Directory index entries with no 'upper' xattr need to be
580 * removed. When dir index entry has a stale 'upper' xattr,
581 * we assume that upper dir was removed and we treat the dir
582 * index as orphan entry that needs to be whited out.
591 err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh);
596 /* Check if non-dir index is orphan and don't warn before cleaning it */
597 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
598 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
602 if (ovl_get_nlink(ofs, origin.dentry, index, 0) == 0)
612 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
613 index, d_inode(index)->i_mode & S_IFMT, err);
617 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
618 index, d_inode(index)->i_mode & S_IFMT,
619 d_inode(index)->i_nlink);
624 static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
628 n = kcalloc(fh->fb.len, 2, GFP_KERNEL);
632 s = bin2hex(n, fh->buf, fh->fb.len);
633 *name = (struct qstr) QSTR_INIT(n, s - n);
640 * Lookup in indexdir for the index entry of a lower real inode or a copy up
641 * origin inode. The index entry name is the hex representation of the lower
644 * If the index dentry in negative, then either no lower aliases have been
645 * copied up yet, or aliases have been copied up in older kernels and are
648 * If the index dentry for a copy up origin inode is positive, but points
649 * to an inode different than the upper inode, then either the upper inode
650 * has been copied up and not indexed or it was indexed, but since then
651 * index dir was cleared. Either way, that index cannot be used to identify
654 int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
660 fh = ovl_encode_real_fh(ofs, origin, false);
664 err = ovl_get_index_name_fh(fh, name);
670 /* Lookup index by file handle for NFS export */
671 struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
673 struct dentry *index;
677 err = ovl_get_index_name_fh(fh, &name);
681 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
684 if (PTR_ERR(index) == -ENOENT)
689 if (ovl_is_whiteout(index))
691 else if (ovl_dentry_weird(index))
700 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
701 struct dentry *origin, bool verify)
703 struct dentry *index;
706 bool is_dir = d_is_dir(origin);
709 err = ovl_get_index_name(ofs, origin, &name);
713 index = lookup_one_positive_unlocked(ovl_upper_mnt_userns(ofs), name.name,
714 ofs->indexdir, name.len);
716 err = PTR_ERR(index);
717 if (err == -ENOENT) {
721 pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
722 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
723 d_inode(origin)->i_ino, name.len, name.name,
728 inode = d_inode(index);
729 if (ovl_is_whiteout(index) && !verify) {
731 * When index lookup is called with !verify for decoding an
732 * overlay file handle, a whiteout index implies that decode
733 * should treat file handle as stale and no need to print a
737 index = ERR_PTR(-ESTALE);
739 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
740 inode_wrong_type(inode, d_inode(origin)->i_mode)) {
742 * Index should always be of the same file type as origin
743 * except for the case of a whiteout index. A whiteout
744 * index should only exist if all lower aliases have been
745 * unlinked, which means that finding a lower origin on lookup
746 * whose index is a whiteout should be treated as an error.
748 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
749 index, d_inode(index)->i_mode & S_IFMT,
750 d_inode(origin)->i_mode & S_IFMT);
752 } else if (is_dir && verify) {
754 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
759 /* Verify that dir index 'upper' xattr points to upper dir */
760 err = ovl_verify_upper(ofs, index, upper, false);
762 if (err == -ESTALE) {
763 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
764 upper, origin, index);
768 } else if (upper && d_inode(upper) != inode) {
782 index = ERR_PTR(-EIO);
787 * Returns next layer in stack starting from top.
788 * Returns -1 if this is the last layer.
790 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
792 struct ovl_entry *oe = dentry->d_fsdata;
796 ovl_path_upper(dentry, path);
798 return oe->numlower ? 1 : -1;
801 BUG_ON(idx > oe->numlower);
802 path->dentry = oe->lowerstack[idx - 1].dentry;
803 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
805 return (idx < oe->numlower) ? idx + 1 : -1;
808 /* Fix missing 'origin' xattr */
809 static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
810 struct dentry *lower, struct dentry *upper)
814 if (ovl_check_origin_xattr(ofs, upper))
817 err = ovl_want_write(dentry);
821 err = ovl_set_origin(ofs, lower, upper);
823 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
825 ovl_drop_write(dentry);
829 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
832 struct ovl_entry *oe;
833 const struct cred *old_cred;
834 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
835 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
836 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
837 struct ovl_path *stack = NULL, *origin_path = NULL;
838 struct dentry *upperdir, *upperdentry = NULL;
839 struct dentry *origin = NULL;
840 struct dentry *index = NULL;
841 unsigned int ctr = 0;
842 struct inode *inode = NULL;
843 bool upperopaque = false;
844 char *upperredirect = NULL;
848 bool uppermetacopy = false;
849 struct ovl_lookup_data d = {
851 .name = dentry->d_name,
855 .last = ofs->config.redirect_follow ? false : !poe->numlower,
860 if (dentry->d_name.len > ofs->namelen)
861 return ERR_PTR(-ENAMETOOLONG);
863 old_cred = ovl_override_creds(dentry->d_sb);
864 upperdir = ovl_dentry_upper(dentry->d_parent);
866 d.mnt = ovl_upper_mnt(ofs);
867 err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
871 if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) {
876 if (upperdentry && !d.is_dir) {
878 * Lookup copy up origin by decoding origin file handle.
879 * We may get a disconnected dentry, which is fine,
880 * because we only need to hold the origin inode in
881 * cache and use its inode number. We may even get a
882 * connected dentry, that is not under any of the lower
883 * layers root. That is also fine for using it's inode
884 * number - it's the same as if we held a reference
885 * to a dentry in lower layer that was moved under us.
887 err = ovl_check_origin(ofs, upperdentry, &origin_path);
892 uppermetacopy = true;
897 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
900 if (d.redirect[0] == '/')
903 upperopaque = d.opaque;
906 if (!d.stop && poe->numlower) {
908 stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path),
914 for (i = 0; !d.stop && i < poe->numlower; i++) {
915 struct ovl_path lower = poe->lowerstack[i];
917 if (!ofs->config.redirect_follow)
918 d.last = i == poe->numlower - 1;
920 d.last = lower.layer->idx == roe->numlower;
922 d.mnt = lower.layer->mnt;
923 err = ovl_lookup_layer(lower.dentry, &d, &this, false);
930 if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) {
933 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry);
938 * If no origin fh is stored in upper of a merge dir, store fh
939 * of lower dir and set upper parent "impure".
941 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
942 err = ovl_fix_origin(ofs, dentry, this, upperdentry);
950 * When "verify_lower" feature is enabled, do not merge with a
951 * lower dir that does not match a stored origin xattr. In any
952 * case, only verified origin is used for index lookup.
954 * For non-dir dentry, if index=on, then ensure origin
955 * matches the dentry found using path based lookup,
956 * otherwise error out.
958 if (upperdentry && !ctr &&
959 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
960 (!d.is_dir && ofs->config.index && origin_path))) {
961 err = ovl_verify_origin(ofs, upperdentry, this, false);
971 if (d.metacopy && ctr) {
973 * Do not store intermediate metacopy dentries in
974 * lower chain, except top most lower metacopy dentry.
975 * Continue the loop so that if there is an absolute
976 * redirect on this dentry, poe can be reset to roe.
981 stack[ctr].dentry = this;
982 stack[ctr].layer = lower.layer;
987 * Following redirects can have security consequences: it's like
988 * a symlink into the lower layer without the permission checks.
989 * This is only a problem if the upper layer is untrusted (e.g
990 * comes from an USB drive). This can allow a non-readable file
991 * or directory to become readable.
993 * Only following redirects when redirects are enabled disables
994 * this attack vector when not necessary.
997 if (d.redirect && !ofs->config.redirect_follow) {
998 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
1006 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
1008 /* Find the current layer on the root dentry */
1009 i = lower.layer->idx - 1;
1014 * For regular non-metacopy upper dentries, there is no lower
1015 * path based lookup, hence ctr will be zero. If a dentry is found
1016 * using ORIGIN xattr on upper, install it in stack.
1018 * For metacopy dentry, path based lookup will find lower dentries.
1019 * Just make sure a corresponding data dentry has been found.
1021 if (d.metacopy || (uppermetacopy && !ctr)) {
1022 pr_warn_ratelimited("metacopy with no lower data found - abort lookup (%pd2)\n",
1026 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1027 if (WARN_ON(stack != NULL)) {
1031 stack = origin_path;
1033 origin = origin_path->dentry;
1038 * Always lookup index if there is no-upperdentry.
1040 * For the case of upperdentry, we have set origin by now if it
1041 * needed to be set. There are basically three cases.
1043 * For directories, lookup index by lower inode and verify it matches
1044 * upper inode. We only trust dir index if we verified that lower dir
1045 * matches origin, otherwise dir index entries may be inconsistent
1046 * and we ignore them.
1048 * For regular upper, we already set origin if upper had ORIGIN
1049 * xattr. There is no verification though as there is no path
1050 * based dentry lookup in lower in this case.
1052 * For metacopy upper, we set a verified origin already if index
1053 * is enabled and if upper had an ORIGIN xattr.
1056 if (!upperdentry && ctr)
1057 origin = stack[0].dentry;
1059 if (origin && ovl_indexdir(dentry->d_sb) &&
1060 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
1061 index = ovl_lookup_index(ofs, upperdentry, origin, true);
1062 if (IS_ERR(index)) {
1063 err = PTR_ERR(index);
1069 oe = ovl_alloc_entry(ctr);
1074 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
1075 dentry->d_fsdata = oe;
1078 ovl_dentry_set_opaque(dentry);
1081 ovl_dentry_set_upper_alias(dentry);
1083 struct path upperpath = {
1084 .dentry = upperdentry = dget(index),
1085 .mnt = ovl_upper_mnt(ofs),
1088 upperredirect = ovl_get_redirect_xattr(ofs, &upperpath, 0);
1089 if (IS_ERR(upperredirect)) {
1090 err = PTR_ERR(upperredirect);
1091 upperredirect = NULL;
1094 err = ovl_check_metacopy_xattr(ofs, &upperpath);
1097 uppermetacopy = err;
1100 if (upperdentry || ctr) {
1101 struct ovl_inode_params oip = {
1102 .upperdentry = upperdentry,
1106 .redirect = upperredirect,
1107 .lowerdata = (ctr > 1 && !d.is_dir) ?
1108 stack[ctr - 1].dentry : NULL,
1111 inode = ovl_get_inode(dentry->d_sb, &oip);
1112 err = PTR_ERR(inode);
1115 if (upperdentry && !uppermetacopy)
1116 ovl_set_flag(OVL_UPPERDATA, inode);
1119 ovl_dentry_update_reval(dentry, upperdentry,
1120 DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
1122 revert_creds(old_cred);
1124 dput(origin_path->dentry);
1130 return d_splice_alias(inode, dentry);
1133 dentry->d_fsdata = NULL;
1137 for (i = 0; i < ctr; i++)
1138 dput(stack[i].dentry);
1142 dput(origin_path->dentry);
1146 kfree(upperredirect);
1149 revert_creds(old_cred);
1150 return ERR_PTR(err);
1153 bool ovl_lower_positive(struct dentry *dentry)
1155 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1156 const struct qstr *name = &dentry->d_name;
1157 const struct cred *old_cred;
1159 bool positive = false;
1163 * If dentry is negative, then lower is positive iff this is a
1166 if (!dentry->d_inode)
1167 return ovl_dentry_is_opaque(dentry);
1169 /* Negative upper -> positive lower */
1170 if (!ovl_dentry_upper(dentry))
1173 old_cred = ovl_override_creds(dentry->d_sb);
1174 /* Positive upper -> have to look up lower to see whether it exists */
1175 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1176 struct dentry *this;
1177 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1179 this = lookup_one_positive_unlocked(mnt_user_ns(poe->lowerstack[i].layer->mnt),
1180 name->name, lowerdir, name->len);
1182 switch (PTR_ERR(this)) {
1189 * Assume something is there, we just couldn't
1196 positive = !ovl_is_whiteout(this);
1201 revert_creds(old_cred);