0001
0002
0003
0004
0005
0006
0007 #include <linux/fs.h>
0008 #include <linux/cred.h>
0009 #include <linux/ctype.h>
0010 #include <linux/namei.h>
0011 #include <linux/xattr.h>
0012 #include <linux/ratelimit.h>
0013 #include <linux/mount.h>
0014 #include <linux/exportfs.h>
0015 #include "overlayfs.h"
0016
0017 struct ovl_lookup_data {
0018 struct super_block *sb;
0019 struct vfsmount *mnt;
0020 struct qstr name;
0021 bool is_dir;
0022 bool opaque;
0023 bool stop;
0024 bool last;
0025 char *redirect;
0026 bool metacopy;
0027 };
0028
0029 static int ovl_check_redirect(struct path *path, struct ovl_lookup_data *d,
0030 size_t prelen, const char *post)
0031 {
0032 int res;
0033 char *buf;
0034 struct ovl_fs *ofs = OVL_FS(d->sb);
0035
0036 buf = ovl_get_redirect_xattr(ofs, path, prelen + strlen(post));
0037 if (IS_ERR_OR_NULL(buf))
0038 return PTR_ERR(buf);
0039
0040 if (buf[0] == '/') {
0041
0042
0043
0044
0045
0046
0047
0048
0049 d->stop = false;
0050 } else {
0051 res = strlen(buf) + 1;
0052 memmove(buf + prelen, buf, res);
0053 memcpy(buf, d->name.name, prelen);
0054 }
0055
0056 strcat(buf, post);
0057 kfree(d->redirect);
0058 d->redirect = buf;
0059 d->name.name = d->redirect;
0060 d->name.len = strlen(d->redirect);
0061
0062 return 0;
0063 }
0064
0065 static int ovl_acceptable(void *ctx, struct dentry *dentry)
0066 {
0067
0068
0069
0070
0071 if (!d_is_dir(dentry))
0072 return 1;
0073
0074
0075 if (d_unhashed(dentry))
0076 return 0;
0077
0078
0079 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
0080 }
0081
0082
0083
0084
0085
0086
0087
0088
0089 int ovl_check_fb_len(struct ovl_fb *fb, int fb_len)
0090 {
0091 if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len)
0092 return -EINVAL;
0093
0094 if (fb->magic != OVL_FH_MAGIC)
0095 return -EINVAL;
0096
0097
0098 if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL)
0099 return -ENODATA;
0100
0101
0102 if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
0103 (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
0104 return -ENODATA;
0105
0106 return 0;
0107 }
0108
0109 static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *upperdentry,
0110 enum ovl_xattr ox)
0111 {
0112 int res, err;
0113 struct ovl_fh *fh = NULL;
0114
0115 res = ovl_getxattr_upper(ofs, upperdentry, ox, NULL, 0);
0116 if (res < 0) {
0117 if (res == -ENODATA || res == -EOPNOTSUPP)
0118 return NULL;
0119 goto fail;
0120 }
0121
0122 if (res == 0)
0123 return NULL;
0124
0125 fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
0126 if (!fh)
0127 return ERR_PTR(-ENOMEM);
0128
0129 res = ovl_getxattr_upper(ofs, upperdentry, ox, fh->buf, res);
0130 if (res < 0)
0131 goto fail;
0132
0133 err = ovl_check_fb_len(&fh->fb, res);
0134 if (err < 0) {
0135 if (err == -ENODATA)
0136 goto out;
0137 goto invalid;
0138 }
0139
0140 return fh;
0141
0142 out:
0143 kfree(fh);
0144 return NULL;
0145
0146 fail:
0147 pr_warn_ratelimited("failed to get origin (%i)\n", res);
0148 goto out;
0149 invalid:
0150 pr_warn_ratelimited("invalid origin (%*phN)\n", res, fh);
0151 goto out;
0152 }
0153
0154 struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
0155 struct vfsmount *mnt, bool connected)
0156 {
0157 struct dentry *real;
0158 int bytes;
0159
0160 if (!capable(CAP_DAC_READ_SEARCH))
0161 return NULL;
0162
0163
0164
0165
0166
0167
0168 if (ofs->config.uuid ? !uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid) :
0169 !uuid_is_null(&fh->fb.uuid))
0170 return NULL;
0171
0172 bytes = (fh->fb.len - offsetof(struct ovl_fb, fid));
0173 real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid,
0174 bytes >> 2, (int)fh->fb.type,
0175 connected ? ovl_acceptable : NULL, mnt);
0176 if (IS_ERR(real)) {
0177
0178
0179
0180
0181
0182
0183 if (real == ERR_PTR(-ESTALE) &&
0184 !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER))
0185 real = NULL;
0186 return real;
0187 }
0188
0189 if (ovl_dentry_weird(real)) {
0190 dput(real);
0191 return NULL;
0192 }
0193
0194 return real;
0195 }
0196
0197 static bool ovl_is_opaquedir(struct ovl_fs *ofs, struct path *path)
0198 {
0199 return ovl_path_check_dir_xattr(ofs, path, OVL_XATTR_OPAQUE);
0200 }
0201
0202 static struct dentry *ovl_lookup_positive_unlocked(struct ovl_lookup_data *d,
0203 const char *name,
0204 struct dentry *base, int len,
0205 bool drop_negative)
0206 {
0207 struct dentry *ret = lookup_one_unlocked(mnt_user_ns(d->mnt), name, base, len);
0208
0209 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
0210 if (drop_negative && ret->d_lockref.count == 1) {
0211 spin_lock(&ret->d_lock);
0212
0213 if (d_is_negative(ret) && ret->d_lockref.count == 1)
0214 __d_drop(ret);
0215 spin_unlock(&ret->d_lock);
0216 }
0217 dput(ret);
0218 ret = ERR_PTR(-ENOENT);
0219 }
0220 return ret;
0221 }
0222
0223 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
0224 const char *name, unsigned int namelen,
0225 size_t prelen, const char *post,
0226 struct dentry **ret, bool drop_negative)
0227 {
0228 struct dentry *this;
0229 struct path path;
0230 int err;
0231 bool last_element = !post[0];
0232
0233 this = ovl_lookup_positive_unlocked(d, name, base, namelen, drop_negative);
0234 if (IS_ERR(this)) {
0235 err = PTR_ERR(this);
0236 this = NULL;
0237 if (err == -ENOENT || err == -ENAMETOOLONG)
0238 goto out;
0239 goto out_err;
0240 }
0241
0242 if (ovl_dentry_weird(this)) {
0243
0244 err = -EREMOTE;
0245 goto out_err;
0246 }
0247 if (ovl_is_whiteout(this)) {
0248 d->stop = d->opaque = true;
0249 goto put_and_out;
0250 }
0251
0252
0253
0254
0255 if (last_element && d->metacopy && !d_is_reg(this)) {
0256 d->stop = true;
0257 goto put_and_out;
0258 }
0259
0260 path.dentry = this;
0261 path.mnt = d->mnt;
0262 if (!d_can_lookup(this)) {
0263 if (d->is_dir || !last_element) {
0264 d->stop = true;
0265 goto put_and_out;
0266 }
0267 err = ovl_check_metacopy_xattr(OVL_FS(d->sb), &path);
0268 if (err < 0)
0269 goto out_err;
0270
0271 d->metacopy = err;
0272 d->stop = !d->metacopy;
0273 if (!d->metacopy || d->last)
0274 goto out;
0275 } else {
0276 if (ovl_lookup_trap_inode(d->sb, this)) {
0277
0278 err = -ELOOP;
0279 goto out_err;
0280 }
0281
0282 if (last_element)
0283 d->is_dir = true;
0284 if (d->last)
0285 goto out;
0286
0287 if (ovl_is_opaquedir(OVL_FS(d->sb), &path)) {
0288 d->stop = true;
0289 if (last_element)
0290 d->opaque = true;
0291 goto out;
0292 }
0293 }
0294 err = ovl_check_redirect(&path, d, prelen, post);
0295 if (err)
0296 goto out_err;
0297 out:
0298 *ret = this;
0299 return 0;
0300
0301 put_and_out:
0302 dput(this);
0303 this = NULL;
0304 goto out;
0305
0306 out_err:
0307 dput(this);
0308 return err;
0309 }
0310
0311 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
0312 struct dentry **ret, bool drop_negative)
0313 {
0314
0315 size_t rem = d->name.len - 1;
0316 struct dentry *dentry = NULL;
0317 int err;
0318
0319 if (d->name.name[0] != '/')
0320 return ovl_lookup_single(base, d, d->name.name, d->name.len,
0321 0, "", ret, drop_negative);
0322
0323 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
0324 const char *s = d->name.name + d->name.len - rem;
0325 const char *next = strchrnul(s, '/');
0326 size_t thislen = next - s;
0327 bool end = !next[0];
0328
0329
0330 if (WARN_ON(s[-1] != '/'))
0331 return -EIO;
0332
0333 err = ovl_lookup_single(base, d, s, thislen,
0334 d->name.len - rem, next, &base,
0335 drop_negative);
0336 dput(dentry);
0337 if (err)
0338 return err;
0339 dentry = base;
0340 if (end)
0341 break;
0342
0343 rem -= thislen + 1;
0344
0345 if (WARN_ON(rem >= d->name.len))
0346 return -EIO;
0347 }
0348 *ret = dentry;
0349 return 0;
0350 }
0351
0352
0353 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
0354 struct dentry *upperdentry, struct ovl_path **stackp)
0355 {
0356 struct dentry *origin = NULL;
0357 int i;
0358
0359 for (i = 1; i < ofs->numlayer; i++) {
0360
0361
0362
0363
0364 if (ofs->layers[i].fsid &&
0365 ofs->layers[i].fs->bad_uuid)
0366 continue;
0367
0368 origin = ovl_decode_real_fh(ofs, fh, ofs->layers[i].mnt,
0369 connected);
0370 if (origin)
0371 break;
0372 }
0373
0374 if (!origin)
0375 return -ESTALE;
0376 else if (IS_ERR(origin))
0377 return PTR_ERR(origin);
0378
0379 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
0380 inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode))
0381 goto invalid;
0382
0383 if (!*stackp)
0384 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
0385 if (!*stackp) {
0386 dput(origin);
0387 return -ENOMEM;
0388 }
0389 **stackp = (struct ovl_path){
0390 .dentry = origin,
0391 .layer = &ofs->layers[i]
0392 };
0393
0394 return 0;
0395
0396 invalid:
0397 pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
0398 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
0399 d_inode(origin)->i_mode & S_IFMT);
0400 dput(origin);
0401 return -ESTALE;
0402 }
0403
0404 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
0405 struct ovl_path **stackp)
0406 {
0407 struct ovl_fh *fh = ovl_get_fh(ofs, upperdentry, OVL_XATTR_ORIGIN);
0408 int err;
0409
0410 if (IS_ERR_OR_NULL(fh))
0411 return PTR_ERR(fh);
0412
0413 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
0414 kfree(fh);
0415
0416 if (err) {
0417 if (err == -ESTALE)
0418 return 0;
0419 return err;
0420 }
0421
0422 return 0;
0423 }
0424
0425
0426
0427
0428
0429 static int ovl_verify_fh(struct ovl_fs *ofs, struct dentry *dentry,
0430 enum ovl_xattr ox, const struct ovl_fh *fh)
0431 {
0432 struct ovl_fh *ofh = ovl_get_fh(ofs, dentry, ox);
0433 int err = 0;
0434
0435 if (!ofh)
0436 return -ENODATA;
0437
0438 if (IS_ERR(ofh))
0439 return PTR_ERR(ofh);
0440
0441 if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len))
0442 err = -ESTALE;
0443
0444 kfree(ofh);
0445 return err;
0446 }
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456 int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
0457 enum ovl_xattr ox, struct dentry *real, bool is_upper,
0458 bool set)
0459 {
0460 struct inode *inode;
0461 struct ovl_fh *fh;
0462 int err;
0463
0464 fh = ovl_encode_real_fh(ofs, real, is_upper);
0465 err = PTR_ERR(fh);
0466 if (IS_ERR(fh)) {
0467 fh = NULL;
0468 goto fail;
0469 }
0470
0471 err = ovl_verify_fh(ofs, dentry, ox, fh);
0472 if (set && err == -ENODATA)
0473 err = ovl_setxattr(ofs, dentry, ox, fh->buf, fh->fb.len);
0474 if (err)
0475 goto fail;
0476
0477 out:
0478 kfree(fh);
0479 return err;
0480
0481 fail:
0482 inode = d_inode(real);
0483 pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n",
0484 is_upper ? "upper" : "origin", real,
0485 inode ? inode->i_ino : 0, err);
0486 goto out;
0487 }
0488
0489
0490 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
0491 {
0492 struct ovl_fh *fh;
0493 struct dentry *upper;
0494
0495 if (!d_is_dir(index))
0496 return dget(index);
0497
0498 fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER);
0499 if (IS_ERR_OR_NULL(fh))
0500 return ERR_CAST(fh);
0501
0502 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true);
0503 kfree(fh);
0504
0505 if (IS_ERR_OR_NULL(upper))
0506 return upper ?: ERR_PTR(-ESTALE);
0507
0508 if (!d_is_dir(upper)) {
0509 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
0510 index, upper);
0511 dput(upper);
0512 return ERR_PTR(-EIO);
0513 }
0514
0515 return upper;
0516 }
0517
0518
0519
0520
0521
0522
0523 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
0524 {
0525 struct ovl_fh *fh = NULL;
0526 size_t len;
0527 struct ovl_path origin = { };
0528 struct ovl_path *stack = &origin;
0529 struct dentry *upper = NULL;
0530 int err;
0531
0532 if (!d_inode(index))
0533 return 0;
0534
0535 err = -EINVAL;
0536 if (index->d_name.len < sizeof(struct ovl_fb)*2)
0537 goto fail;
0538
0539 err = -ENOMEM;
0540 len = index->d_name.len / 2;
0541 fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
0542 if (!fh)
0543 goto fail;
0544
0545 err = -EINVAL;
0546 if (hex2bin(fh->buf, index->d_name.name, len))
0547 goto fail;
0548
0549 err = ovl_check_fb_len(&fh->fb, len);
0550 if (err)
0551 goto fail;
0552
0553
0554
0555
0556
0557
0558 if (ovl_is_whiteout(index))
0559 goto out;
0560
0561
0562
0563
0564
0565 if (d_is_dir(index) && !ofs->config.nfs_export)
0566 goto out;
0567
0568
0569
0570
0571
0572
0573
0574
0575 upper = ovl_index_upper(ofs, index);
0576 if (IS_ERR_OR_NULL(upper)) {
0577 err = PTR_ERR(upper);
0578
0579
0580
0581
0582
0583
0584 if (err == -ESTALE)
0585 goto orphan;
0586 else if (!err)
0587 err = -ESTALE;
0588 goto fail;
0589 }
0590
0591 err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh);
0592 dput(upper);
0593 if (err)
0594 goto fail;
0595
0596
0597 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
0598 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
0599 if (err)
0600 goto fail;
0601
0602 if (ovl_get_nlink(ofs, origin.dentry, index, 0) == 0)
0603 goto orphan;
0604 }
0605
0606 out:
0607 dput(origin.dentry);
0608 kfree(fh);
0609 return err;
0610
0611 fail:
0612 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
0613 index, d_inode(index)->i_mode & S_IFMT, err);
0614 goto out;
0615
0616 orphan:
0617 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
0618 index, d_inode(index)->i_mode & S_IFMT,
0619 d_inode(index)->i_nlink);
0620 err = -ENOENT;
0621 goto out;
0622 }
0623
0624 static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
0625 {
0626 char *n, *s;
0627
0628 n = kcalloc(fh->fb.len, 2, GFP_KERNEL);
0629 if (!n)
0630 return -ENOMEM;
0631
0632 s = bin2hex(n, fh->buf, fh->fb.len);
0633 *name = (struct qstr) QSTR_INIT(n, s - n);
0634
0635 return 0;
0636
0637 }
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654 int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
0655 struct qstr *name)
0656 {
0657 struct ovl_fh *fh;
0658 int err;
0659
0660 fh = ovl_encode_real_fh(ofs, origin, false);
0661 if (IS_ERR(fh))
0662 return PTR_ERR(fh);
0663
0664 err = ovl_get_index_name_fh(fh, name);
0665
0666 kfree(fh);
0667 return err;
0668 }
0669
0670
0671 struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
0672 {
0673 struct dentry *index;
0674 struct qstr name;
0675 int err;
0676
0677 err = ovl_get_index_name_fh(fh, &name);
0678 if (err)
0679 return ERR_PTR(err);
0680
0681 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
0682 kfree(name.name);
0683 if (IS_ERR(index)) {
0684 if (PTR_ERR(index) == -ENOENT)
0685 index = NULL;
0686 return index;
0687 }
0688
0689 if (ovl_is_whiteout(index))
0690 err = -ESTALE;
0691 else if (ovl_dentry_weird(index))
0692 err = -EIO;
0693 else
0694 return index;
0695
0696 dput(index);
0697 return ERR_PTR(err);
0698 }
0699
0700 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
0701 struct dentry *origin, bool verify)
0702 {
0703 struct dentry *index;
0704 struct inode *inode;
0705 struct qstr name;
0706 bool is_dir = d_is_dir(origin);
0707 int err;
0708
0709 err = ovl_get_index_name(ofs, origin, &name);
0710 if (err)
0711 return ERR_PTR(err);
0712
0713 index = lookup_one_positive_unlocked(ovl_upper_mnt_userns(ofs), name.name,
0714 ofs->indexdir, name.len);
0715 if (IS_ERR(index)) {
0716 err = PTR_ERR(index);
0717 if (err == -ENOENT) {
0718 index = NULL;
0719 goto out;
0720 }
0721 pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
0722 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
0723 d_inode(origin)->i_ino, name.len, name.name,
0724 err);
0725 goto out;
0726 }
0727
0728 inode = d_inode(index);
0729 if (ovl_is_whiteout(index) && !verify) {
0730
0731
0732
0733
0734
0735
0736 dput(index);
0737 index = ERR_PTR(-ESTALE);
0738 goto out;
0739 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
0740 inode_wrong_type(inode, d_inode(origin)->i_mode)) {
0741
0742
0743
0744
0745
0746
0747
0748 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
0749 index, d_inode(index)->i_mode & S_IFMT,
0750 d_inode(origin)->i_mode & S_IFMT);
0751 goto fail;
0752 } else if (is_dir && verify) {
0753 if (!upper) {
0754 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
0755 origin, index);
0756 goto fail;
0757 }
0758
0759
0760 err = ovl_verify_upper(ofs, index, upper, false);
0761 if (err) {
0762 if (err == -ESTALE) {
0763 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
0764 upper, origin, index);
0765 }
0766 goto fail;
0767 }
0768 } else if (upper && d_inode(upper) != inode) {
0769 goto out_dput;
0770 }
0771 out:
0772 kfree(name.name);
0773 return index;
0774
0775 out_dput:
0776 dput(index);
0777 index = NULL;
0778 goto out;
0779
0780 fail:
0781 dput(index);
0782 index = ERR_PTR(-EIO);
0783 goto out;
0784 }
0785
0786
0787
0788
0789
0790 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
0791 {
0792 struct ovl_entry *oe = dentry->d_fsdata;
0793
0794 BUG_ON(idx < 0);
0795 if (idx == 0) {
0796 ovl_path_upper(dentry, path);
0797 if (path->dentry)
0798 return oe->numlower ? 1 : -1;
0799 idx++;
0800 }
0801 BUG_ON(idx > oe->numlower);
0802 path->dentry = oe->lowerstack[idx - 1].dentry;
0803 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
0804
0805 return (idx < oe->numlower) ? idx + 1 : -1;
0806 }
0807
0808
0809 static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
0810 struct dentry *lower, struct dentry *upper)
0811 {
0812 int err;
0813
0814 if (ovl_check_origin_xattr(ofs, upper))
0815 return 0;
0816
0817 err = ovl_want_write(dentry);
0818 if (err)
0819 return err;
0820
0821 err = ovl_set_origin(ofs, lower, upper);
0822 if (!err)
0823 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
0824
0825 ovl_drop_write(dentry);
0826 return err;
0827 }
0828
0829 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
0830 unsigned int flags)
0831 {
0832 struct ovl_entry *oe;
0833 const struct cred *old_cred;
0834 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
0835 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
0836 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
0837 struct ovl_path *stack = NULL, *origin_path = NULL;
0838 struct dentry *upperdir, *upperdentry = NULL;
0839 struct dentry *origin = NULL;
0840 struct dentry *index = NULL;
0841 unsigned int ctr = 0;
0842 struct inode *inode = NULL;
0843 bool upperopaque = false;
0844 char *upperredirect = NULL;
0845 struct dentry *this;
0846 unsigned int i;
0847 int err;
0848 bool uppermetacopy = false;
0849 struct ovl_lookup_data d = {
0850 .sb = dentry->d_sb,
0851 .name = dentry->d_name,
0852 .is_dir = false,
0853 .opaque = false,
0854 .stop = false,
0855 .last = ofs->config.redirect_follow ? false : !poe->numlower,
0856 .redirect = NULL,
0857 .metacopy = false,
0858 };
0859
0860 if (dentry->d_name.len > ofs->namelen)
0861 return ERR_PTR(-ENAMETOOLONG);
0862
0863 old_cred = ovl_override_creds(dentry->d_sb);
0864 upperdir = ovl_dentry_upper(dentry->d_parent);
0865 if (upperdir) {
0866 d.mnt = ovl_upper_mnt(ofs);
0867 err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
0868 if (err)
0869 goto out;
0870
0871 if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) {
0872 dput(upperdentry);
0873 err = -EREMOTE;
0874 goto out;
0875 }
0876 if (upperdentry && !d.is_dir) {
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886
0887 err = ovl_check_origin(ofs, upperdentry, &origin_path);
0888 if (err)
0889 goto out_put_upper;
0890
0891 if (d.metacopy)
0892 uppermetacopy = true;
0893 }
0894
0895 if (d.redirect) {
0896 err = -ENOMEM;
0897 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
0898 if (!upperredirect)
0899 goto out_put_upper;
0900 if (d.redirect[0] == '/')
0901 poe = roe;
0902 }
0903 upperopaque = d.opaque;
0904 }
0905
0906 if (!d.stop && poe->numlower) {
0907 err = -ENOMEM;
0908 stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path),
0909 GFP_KERNEL);
0910 if (!stack)
0911 goto out_put_upper;
0912 }
0913
0914 for (i = 0; !d.stop && i < poe->numlower; i++) {
0915 struct ovl_path lower = poe->lowerstack[i];
0916
0917 if (!ofs->config.redirect_follow)
0918 d.last = i == poe->numlower - 1;
0919 else
0920 d.last = lower.layer->idx == roe->numlower;
0921
0922 d.mnt = lower.layer->mnt;
0923 err = ovl_lookup_layer(lower.dentry, &d, &this, false);
0924 if (err)
0925 goto out_put;
0926
0927 if (!this)
0928 continue;
0929
0930 if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) {
0931 dput(this);
0932 err = -EPERM;
0933 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry);
0934 goto out_put;
0935 }
0936
0937
0938
0939
0940
0941 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
0942 err = ovl_fix_origin(ofs, dentry, this, upperdentry);
0943 if (err) {
0944 dput(this);
0945 goto out_put;
0946 }
0947 }
0948
0949
0950
0951
0952
0953
0954
0955
0956
0957
0958 if (upperdentry && !ctr &&
0959 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
0960 (!d.is_dir && ofs->config.index && origin_path))) {
0961 err = ovl_verify_origin(ofs, upperdentry, this, false);
0962 if (err) {
0963 dput(this);
0964 if (d.is_dir)
0965 break;
0966 goto out_put;
0967 }
0968 origin = this;
0969 }
0970
0971 if (d.metacopy && ctr) {
0972
0973
0974
0975
0976
0977
0978 dput(this);
0979 this = NULL;
0980 } else {
0981 stack[ctr].dentry = this;
0982 stack[ctr].layer = lower.layer;
0983 ctr++;
0984 }
0985
0986
0987
0988
0989
0990
0991
0992
0993
0994
0995
0996 err = -EPERM;
0997 if (d.redirect && !ofs->config.redirect_follow) {
0998 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
0999 dentry);
1000 goto out_put;
1001 }
1002
1003 if (d.stop)
1004 break;
1005
1006 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
1007 poe = roe;
1008
1009 i = lower.layer->idx - 1;
1010 }
1011 }
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021 if (d.metacopy || (uppermetacopy && !ctr)) {
1022 pr_warn_ratelimited("metacopy with no lower data found - abort lookup (%pd2)\n",
1023 dentry);
1024 err = -EIO;
1025 goto out_put;
1026 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1027 if (WARN_ON(stack != NULL)) {
1028 err = -EIO;
1029 goto out_put;
1030 }
1031 stack = origin_path;
1032 ctr = 1;
1033 origin = origin_path->dentry;
1034 origin_path = NULL;
1035 }
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056 if (!upperdentry && ctr)
1057 origin = stack[0].dentry;
1058
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);
1064 index = NULL;
1065 goto out_put;
1066 }
1067 }
1068
1069 oe = ovl_alloc_entry(ctr);
1070 err = -ENOMEM;
1071 if (!oe)
1072 goto out_put;
1073
1074 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
1075 dentry->d_fsdata = oe;
1076
1077 if (upperopaque)
1078 ovl_dentry_set_opaque(dentry);
1079
1080 if (upperdentry)
1081 ovl_dentry_set_upper_alias(dentry);
1082 else if (index) {
1083 struct path upperpath = {
1084 .dentry = upperdentry = dget(index),
1085 .mnt = ovl_upper_mnt(ofs),
1086 };
1087
1088 upperredirect = ovl_get_redirect_xattr(ofs, &upperpath, 0);
1089 if (IS_ERR(upperredirect)) {
1090 err = PTR_ERR(upperredirect);
1091 upperredirect = NULL;
1092 goto out_free_oe;
1093 }
1094 err = ovl_check_metacopy_xattr(ofs, &upperpath);
1095 if (err < 0)
1096 goto out_free_oe;
1097 uppermetacopy = err;
1098 }
1099
1100 if (upperdentry || ctr) {
1101 struct ovl_inode_params oip = {
1102 .upperdentry = upperdentry,
1103 .lowerpath = stack,
1104 .index = index,
1105 .numlower = ctr,
1106 .redirect = upperredirect,
1107 .lowerdata = (ctr > 1 && !d.is_dir) ?
1108 stack[ctr - 1].dentry : NULL,
1109 };
1110
1111 inode = ovl_get_inode(dentry->d_sb, &oip);
1112 err = PTR_ERR(inode);
1113 if (IS_ERR(inode))
1114 goto out_free_oe;
1115 if (upperdentry && !uppermetacopy)
1116 ovl_set_flag(OVL_UPPERDATA, inode);
1117 }
1118
1119 ovl_dentry_update_reval(dentry, upperdentry,
1120 DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
1121
1122 revert_creds(old_cred);
1123 if (origin_path) {
1124 dput(origin_path->dentry);
1125 kfree(origin_path);
1126 }
1127 dput(index);
1128 kfree(stack);
1129 kfree(d.redirect);
1130 return d_splice_alias(inode, dentry);
1131
1132 out_free_oe:
1133 dentry->d_fsdata = NULL;
1134 kfree(oe);
1135 out_put:
1136 dput(index);
1137 for (i = 0; i < ctr; i++)
1138 dput(stack[i].dentry);
1139 kfree(stack);
1140 out_put_upper:
1141 if (origin_path) {
1142 dput(origin_path->dentry);
1143 kfree(origin_path);
1144 }
1145 dput(upperdentry);
1146 kfree(upperredirect);
1147 out:
1148 kfree(d.redirect);
1149 revert_creds(old_cred);
1150 return ERR_PTR(err);
1151 }
1152
1153 bool ovl_lower_positive(struct dentry *dentry)
1154 {
1155 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1156 const struct qstr *name = &dentry->d_name;
1157 const struct cred *old_cred;
1158 unsigned int i;
1159 bool positive = false;
1160 bool done = false;
1161
1162
1163
1164
1165
1166 if (!dentry->d_inode)
1167 return ovl_dentry_is_opaque(dentry);
1168
1169
1170 if (!ovl_dentry_upper(dentry))
1171 return true;
1172
1173 old_cred = ovl_override_creds(dentry->d_sb);
1174
1175 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1176 struct dentry *this;
1177 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1178
1179 this = lookup_one_positive_unlocked(mnt_user_ns(poe->lowerstack[i].layer->mnt),
1180 name->name, lowerdir, name->len);
1181 if (IS_ERR(this)) {
1182 switch (PTR_ERR(this)) {
1183 case -ENOENT:
1184 case -ENAMETOOLONG:
1185 break;
1186
1187 default:
1188
1189
1190
1191
1192 positive = true;
1193 break;
1194 }
1195 } else {
1196 positive = !ovl_is_whiteout(this);
1197 done = true;
1198 dput(this);
1199 }
1200 }
1201 revert_creds(old_cred);
1202
1203 return positive;
1204 }