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(const 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, const 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,
494 struct dentry *upper;
496 if (!d_is_dir(index))
499 fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER);
500 if (IS_ERR_OR_NULL(fh))
503 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), connected);
506 if (IS_ERR_OR_NULL(upper))
507 return upper ?: ERR_PTR(-ESTALE);
509 if (!d_is_dir(upper)) {
510 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
513 return ERR_PTR(-EIO);
520 * Verify that an index entry name matches the origin file handle stored in
521 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
522 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
524 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
526 struct ovl_fh *fh = NULL;
528 struct ovl_path origin = { };
529 struct ovl_path *stack = &origin;
530 struct dentry *upper = NULL;
537 if (index->d_name.len < sizeof(struct ovl_fb)*2)
541 len = index->d_name.len / 2;
542 fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
547 if (hex2bin(fh->buf, index->d_name.name, len))
550 err = ovl_check_fb_len(&fh->fb, len);
555 * Whiteout index entries are used as an indication that an exported
556 * overlay file handle should be treated as stale (i.e. after unlink
557 * of the overlay inode). These entries contain no origin xattr.
559 if (ovl_is_whiteout(index))
563 * Verifying directory index entries are not stale is expensive, so
564 * only verify stale dir index if NFS export is enabled.
566 if (d_is_dir(index) && !ofs->config.nfs_export)
570 * Directory index entries should have 'upper' xattr pointing to the
571 * real upper dir. Non-dir index entries are hardlinks to the upper
572 * real inode. For non-dir index, we can read the copy up origin xattr
573 * directly from the index dentry, but for dir index we first need to
574 * decode the upper directory.
576 upper = ovl_index_upper(ofs, index, false);
577 if (IS_ERR_OR_NULL(upper)) {
578 err = PTR_ERR(upper);
580 * Directory index entries with no 'upper' xattr need to be
581 * removed. When dir index entry has a stale 'upper' xattr,
582 * we assume that upper dir was removed and we treat the dir
583 * index as orphan entry that needs to be whited out.
592 err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh);
597 /* Check if non-dir index is orphan and don't warn before cleaning it */
598 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
599 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
603 if (ovl_get_nlink(ofs, origin.dentry, index, 0) == 0)
613 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
614 index, d_inode(index)->i_mode & S_IFMT, err);
618 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
619 index, d_inode(index)->i_mode & S_IFMT,
620 d_inode(index)->i_nlink);
625 static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
629 n = kcalloc(fh->fb.len, 2, GFP_KERNEL);
633 s = bin2hex(n, fh->buf, fh->fb.len);
634 *name = (struct qstr) QSTR_INIT(n, s - n);
641 * Lookup in indexdir for the index entry of a lower real inode or a copy up
642 * origin inode. The index entry name is the hex representation of the lower
645 * If the index dentry in negative, then either no lower aliases have been
646 * copied up yet, or aliases have been copied up in older kernels and are
649 * If the index dentry for a copy up origin inode is positive, but points
650 * to an inode different than the upper inode, then either the upper inode
651 * has been copied up and not indexed or it was indexed, but since then
652 * index dir was cleared. Either way, that index cannot be used to identify
655 int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
661 fh = ovl_encode_real_fh(ofs, origin, false);
665 err = ovl_get_index_name_fh(fh, name);
671 /* Lookup index by file handle for NFS export */
672 struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
674 struct dentry *index;
678 err = ovl_get_index_name_fh(fh, &name);
682 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
685 if (PTR_ERR(index) == -ENOENT)
690 if (ovl_is_whiteout(index))
692 else if (ovl_dentry_weird(index))
701 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
702 struct dentry *origin, bool verify)
704 struct dentry *index;
707 bool is_dir = d_is_dir(origin);
710 err = ovl_get_index_name(ofs, origin, &name);
714 index = lookup_one_positive_unlocked(ovl_upper_mnt_userns(ofs), name.name,
715 ofs->indexdir, name.len);
717 err = PTR_ERR(index);
718 if (err == -ENOENT) {
722 pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
723 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
724 d_inode(origin)->i_ino, name.len, name.name,
729 inode = d_inode(index);
730 if (ovl_is_whiteout(index) && !verify) {
732 * When index lookup is called with !verify for decoding an
733 * overlay file handle, a whiteout index implies that decode
734 * should treat file handle as stale and no need to print a
738 index = ERR_PTR(-ESTALE);
740 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
741 inode_wrong_type(inode, d_inode(origin)->i_mode)) {
743 * Index should always be of the same file type as origin
744 * except for the case of a whiteout index. A whiteout
745 * index should only exist if all lower aliases have been
746 * unlinked, which means that finding a lower origin on lookup
747 * whose index is a whiteout should be treated as an error.
749 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
750 index, d_inode(index)->i_mode & S_IFMT,
751 d_inode(origin)->i_mode & S_IFMT);
753 } else if (is_dir && verify) {
755 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
760 /* Verify that dir index 'upper' xattr points to upper dir */
761 err = ovl_verify_upper(ofs, index, upper, false);
763 if (err == -ESTALE) {
764 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
765 upper, origin, index);
769 } else if (upper && d_inode(upper) != inode) {
783 index = ERR_PTR(-EIO);
788 * Returns next layer in stack starting from top.
789 * Returns -1 if this is the last layer.
791 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
793 struct ovl_entry *oe = dentry->d_fsdata;
797 ovl_path_upper(dentry, path);
799 return oe->numlower ? 1 : -1;
802 BUG_ON(idx > oe->numlower);
803 path->dentry = oe->lowerstack[idx - 1].dentry;
804 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
806 return (idx < oe->numlower) ? idx + 1 : -1;
809 /* Fix missing 'origin' xattr */
810 static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
811 struct dentry *lower, struct dentry *upper)
815 if (ovl_check_origin_xattr(ofs, upper))
818 err = ovl_want_write(dentry);
822 err = ovl_set_origin(ofs, lower, upper);
824 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
826 ovl_drop_write(dentry);
830 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
833 struct ovl_entry *oe;
834 const struct cred *old_cred;
835 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
836 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
837 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
838 struct ovl_path *stack = NULL, *origin_path = NULL;
839 struct dentry *upperdir, *upperdentry = NULL;
840 struct dentry *origin = NULL;
841 struct dentry *index = NULL;
842 unsigned int ctr = 0;
843 struct inode *inode = NULL;
844 bool upperopaque = false;
845 char *upperredirect = NULL;
849 bool uppermetacopy = false;
850 struct ovl_lookup_data d = {
852 .name = dentry->d_name,
856 .last = ofs->config.redirect_follow ? false : !poe->numlower,
861 if (dentry->d_name.len > ofs->namelen)
862 return ERR_PTR(-ENAMETOOLONG);
864 old_cred = ovl_override_creds(dentry->d_sb);
865 upperdir = ovl_dentry_upper(dentry->d_parent);
867 d.mnt = ovl_upper_mnt(ofs);
868 err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
872 if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) {
877 if (upperdentry && !d.is_dir) {
879 * Lookup copy up origin by decoding origin file handle.
880 * We may get a disconnected dentry, which is fine,
881 * because we only need to hold the origin inode in
882 * cache and use its inode number. We may even get a
883 * connected dentry, that is not under any of the lower
884 * layers root. That is also fine for using it's inode
885 * number - it's the same as if we held a reference
886 * to a dentry in lower layer that was moved under us.
888 err = ovl_check_origin(ofs, upperdentry, &origin_path);
893 uppermetacopy = true;
898 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
901 if (d.redirect[0] == '/')
904 upperopaque = d.opaque;
907 if (!d.stop && poe->numlower) {
909 stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path),
915 for (i = 0; !d.stop && i < poe->numlower; i++) {
916 struct ovl_path lower = poe->lowerstack[i];
918 if (!ofs->config.redirect_follow)
919 d.last = i == poe->numlower - 1;
921 d.last = lower.layer->idx == roe->numlower;
923 d.mnt = lower.layer->mnt;
924 err = ovl_lookup_layer(lower.dentry, &d, &this, false);
931 if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) {
934 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry);
939 * If no origin fh is stored in upper of a merge dir, store fh
940 * of lower dir and set upper parent "impure".
942 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
943 err = ovl_fix_origin(ofs, dentry, this, upperdentry);
951 * When "verify_lower" feature is enabled, do not merge with a
952 * lower dir that does not match a stored origin xattr. In any
953 * case, only verified origin is used for index lookup.
955 * For non-dir dentry, if index=on, then ensure origin
956 * matches the dentry found using path based lookup,
957 * otherwise error out.
959 if (upperdentry && !ctr &&
960 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
961 (!d.is_dir && ofs->config.index && origin_path))) {
962 err = ovl_verify_origin(ofs, upperdentry, this, false);
972 if (d.metacopy && ctr) {
974 * Do not store intermediate metacopy dentries in
975 * lower chain, except top most lower metacopy dentry.
976 * Continue the loop so that if there is an absolute
977 * redirect on this dentry, poe can be reset to roe.
982 stack[ctr].dentry = this;
983 stack[ctr].layer = lower.layer;
988 * Following redirects can have security consequences: it's like
989 * a symlink into the lower layer without the permission checks.
990 * This is only a problem if the upper layer is untrusted (e.g
991 * comes from an USB drive). This can allow a non-readable file
992 * or directory to become readable.
994 * Only following redirects when redirects are enabled disables
995 * this attack vector when not necessary.
998 if (d.redirect && !ofs->config.redirect_follow) {
999 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
1007 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
1009 /* Find the current layer on the root dentry */
1010 i = lower.layer->idx - 1;
1015 * For regular non-metacopy upper dentries, there is no lower
1016 * path based lookup, hence ctr will be zero. If a dentry is found
1017 * using ORIGIN xattr on upper, install it in stack.
1019 * For metacopy dentry, path based lookup will find lower dentries.
1020 * Just make sure a corresponding data dentry has been found.
1022 if (d.metacopy || (uppermetacopy && !ctr)) {
1023 pr_warn_ratelimited("metacopy with no lower data found - abort lookup (%pd2)\n",
1027 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1028 if (WARN_ON(stack != NULL)) {
1032 stack = origin_path;
1034 origin = origin_path->dentry;
1039 * Always lookup index if there is no-upperdentry.
1041 * For the case of upperdentry, we have set origin by now if it
1042 * needed to be set. There are basically three cases.
1044 * For directories, lookup index by lower inode and verify it matches
1045 * upper inode. We only trust dir index if we verified that lower dir
1046 * matches origin, otherwise dir index entries may be inconsistent
1047 * and we ignore them.
1049 * For regular upper, we already set origin if upper had ORIGIN
1050 * xattr. There is no verification though as there is no path
1051 * based dentry lookup in lower in this case.
1053 * For metacopy upper, we set a verified origin already if index
1054 * is enabled and if upper had an ORIGIN xattr.
1057 if (!upperdentry && ctr)
1058 origin = stack[0].dentry;
1060 if (origin && ovl_indexdir(dentry->d_sb) &&
1061 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
1062 index = ovl_lookup_index(ofs, upperdentry, origin, true);
1063 if (IS_ERR(index)) {
1064 err = PTR_ERR(index);
1070 oe = ovl_alloc_entry(ctr);
1075 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
1076 dentry->d_fsdata = oe;
1079 ovl_dentry_set_opaque(dentry);
1082 ovl_dentry_set_upper_alias(dentry);
1084 struct path upperpath = {
1085 .dentry = upperdentry = dget(index),
1086 .mnt = ovl_upper_mnt(ofs),
1090 * It's safe to assign upperredirect here: the previous
1091 * assignment of happens only if upperdentry is non-NULL, and
1092 * this one only if upperdentry is NULL.
1094 upperredirect = ovl_get_redirect_xattr(ofs, &upperpath, 0);
1095 if (IS_ERR(upperredirect)) {
1096 err = PTR_ERR(upperredirect);
1097 upperredirect = NULL;
1100 err = ovl_check_metacopy_xattr(ofs, &upperpath);
1103 uppermetacopy = err;
1106 if (upperdentry || ctr) {
1107 struct ovl_inode_params oip = {
1108 .upperdentry = upperdentry,
1112 .redirect = upperredirect,
1113 .lowerdata = (ctr > 1 && !d.is_dir) ?
1114 stack[ctr - 1].dentry : NULL,
1117 inode = ovl_get_inode(dentry->d_sb, &oip);
1118 err = PTR_ERR(inode);
1121 if (upperdentry && !uppermetacopy)
1122 ovl_set_flag(OVL_UPPERDATA, inode);
1125 ovl_dentry_update_reval(dentry, upperdentry,
1126 DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
1128 revert_creds(old_cred);
1130 dput(origin_path->dentry);
1136 return d_splice_alias(inode, dentry);
1139 dentry->d_fsdata = NULL;
1143 for (i = 0; i < ctr; i++)
1144 dput(stack[i].dentry);
1148 dput(origin_path->dentry);
1152 kfree(upperredirect);
1155 revert_creds(old_cred);
1156 return ERR_PTR(err);
1159 bool ovl_lower_positive(struct dentry *dentry)
1161 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1162 const struct qstr *name = &dentry->d_name;
1163 const struct cred *old_cred;
1165 bool positive = false;
1169 * If dentry is negative, then lower is positive iff this is a
1172 if (!dentry->d_inode)
1173 return ovl_dentry_is_opaque(dentry);
1175 /* Negative upper -> positive lower */
1176 if (!ovl_dentry_upper(dentry))
1179 old_cred = ovl_override_creds(dentry->d_sb);
1180 /* Positive upper -> have to look up lower to see whether it exists */
1181 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1182 struct dentry *this;
1183 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1185 this = lookup_one_positive_unlocked(mnt_user_ns(poe->lowerstack[i].layer->mnt),
1186 name->name, lowerdir, name->len);
1188 switch (PTR_ERR(this)) {
1195 * Assume something is there, we just couldn't
1202 positive = !ovl_is_whiteout(this);
1207 revert_creds(old_cred);