0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/fs.h>
0011 #include <linux/cred.h>
0012 #include <linux/mount.h>
0013 #include <linux/namei.h>
0014 #include <linux/xattr.h>
0015 #include <linux/exportfs.h>
0016 #include <linux/ratelimit.h>
0017 #include "overlayfs.h"
0018
0019 static int ovl_encode_maybe_copy_up(struct dentry *dentry)
0020 {
0021 int err;
0022
0023 if (ovl_dentry_upper(dentry))
0024 return 0;
0025
0026 err = ovl_want_write(dentry);
0027 if (!err) {
0028 err = ovl_copy_up(dentry);
0029 ovl_drop_write(dentry);
0030 }
0031
0032 if (err) {
0033 pr_warn_ratelimited("failed to copy up on encode (%pd2, err=%i)\n",
0034 dentry, err);
0035 }
0036
0037 return err;
0038 }
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077 static int ovl_connectable_layer(struct dentry *dentry)
0078 {
0079 struct ovl_entry *oe = OVL_E(dentry);
0080
0081
0082 if (dentry == dentry->d_sb->s_root)
0083 return oe->numlower;
0084
0085
0086
0087
0088
0089 if (ovl_dentry_upper(dentry) &&
0090 !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
0091 return 0;
0092
0093
0094 return oe->lowerstack[0].layer->idx;
0095 }
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 static int ovl_connect_layer(struct dentry *dentry)
0106 {
0107 struct dentry *next, *parent = NULL;
0108 int origin_layer;
0109 int err = 0;
0110
0111 if (WARN_ON(dentry == dentry->d_sb->s_root) ||
0112 WARN_ON(!ovl_dentry_lower(dentry)))
0113 return -EIO;
0114
0115 origin_layer = OVL_E(dentry)->lowerstack[0].layer->idx;
0116 if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry))
0117 return origin_layer;
0118
0119
0120 next = dget(dentry);
0121 for (;;) {
0122 parent = dget_parent(next);
0123 if (WARN_ON(parent == next)) {
0124 err = -EIO;
0125 break;
0126 }
0127
0128
0129
0130
0131
0132 if (ovl_connectable_layer(parent) < origin_layer) {
0133 err = ovl_encode_maybe_copy_up(next);
0134 break;
0135 }
0136
0137
0138 if (ovl_dentry_test_flag(OVL_E_CONNECTED, parent) ||
0139 ovl_test_flag(OVL_INDEX, d_inode(parent)))
0140 break;
0141
0142 dput(next);
0143 next = parent;
0144 }
0145
0146 dput(parent);
0147 dput(next);
0148
0149 if (!err)
0150 ovl_dentry_set_flag(OVL_E_CONNECTED, dentry);
0151
0152 return err ?: origin_layer;
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 static int ovl_check_encode_origin(struct dentry *dentry)
0184 {
0185 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
0186
0187
0188 if (!ovl_dentry_lower(dentry))
0189 return 0;
0190
0191
0192
0193
0194
0195
0196
0197 if (ovl_dentry_upper(dentry) &&
0198 !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
0199 return 0;
0200
0201
0202
0203
0204
0205
0206
0207 if (d_is_dir(dentry) && ovl_upper_mnt(ofs))
0208 return ovl_connect_layer(dentry);
0209
0210
0211 return 1;
0212 }
0213
0214 static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct dentry *dentry,
0215 u32 *fid, int buflen)
0216 {
0217 struct ovl_fh *fh = NULL;
0218 int err, enc_lower;
0219 int len;
0220
0221
0222
0223
0224
0225 err = enc_lower = ovl_check_encode_origin(dentry);
0226 if (enc_lower < 0)
0227 goto fail;
0228
0229
0230 fh = ovl_encode_real_fh(ofs, enc_lower ? ovl_dentry_lower(dentry) :
0231 ovl_dentry_upper(dentry), !enc_lower);
0232 if (IS_ERR(fh))
0233 return PTR_ERR(fh);
0234
0235 len = OVL_FH_LEN(fh);
0236 if (len <= buflen)
0237 memcpy(fid, fh, len);
0238 err = len;
0239
0240 out:
0241 kfree(fh);
0242 return err;
0243
0244 fail:
0245 pr_warn_ratelimited("failed to encode file handle (%pd2, err=%i)\n",
0246 dentry, err);
0247 goto out;
0248 }
0249
0250 static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len,
0251 struct inode *parent)
0252 {
0253 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
0254 struct dentry *dentry;
0255 int bytes, buflen = *max_len << 2;
0256
0257
0258 if (parent)
0259 return FILEID_INVALID;
0260
0261 dentry = d_find_any_alias(inode);
0262 if (!dentry)
0263 return FILEID_INVALID;
0264
0265 bytes = ovl_dentry_to_fid(ofs, dentry, fid, buflen);
0266 dput(dentry);
0267 if (bytes <= 0)
0268 return FILEID_INVALID;
0269
0270 *max_len = bytes >> 2;
0271 if (bytes > buflen)
0272 return FILEID_INVALID;
0273
0274 return OVL_FILEID_V1;
0275 }
0276
0277
0278
0279
0280 static struct dentry *ovl_obtain_alias(struct super_block *sb,
0281 struct dentry *upper_alias,
0282 struct ovl_path *lowerpath,
0283 struct dentry *index)
0284 {
0285 struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
0286 struct dentry *upper = upper_alias ?: index;
0287 struct dentry *dentry;
0288 struct inode *inode;
0289 struct ovl_entry *oe;
0290 struct ovl_inode_params oip = {
0291 .lowerpath = lowerpath,
0292 .index = index,
0293 .numlower = !!lower
0294 };
0295
0296
0297 if (d_is_dir(upper ?: lower))
0298 return ERR_PTR(-EIO);
0299
0300 oip.upperdentry = dget(upper);
0301 inode = ovl_get_inode(sb, &oip);
0302 if (IS_ERR(inode)) {
0303 dput(upper);
0304 return ERR_CAST(inode);
0305 }
0306
0307 if (upper)
0308 ovl_set_flag(OVL_UPPERDATA, inode);
0309
0310 dentry = d_find_any_alias(inode);
0311 if (dentry)
0312 goto out_iput;
0313
0314 dentry = d_alloc_anon(inode->i_sb);
0315 if (unlikely(!dentry))
0316 goto nomem;
0317 oe = ovl_alloc_entry(lower ? 1 : 0);
0318 if (!oe)
0319 goto nomem;
0320
0321 if (lower) {
0322 oe->lowerstack->dentry = dget(lower);
0323 oe->lowerstack->layer = lowerpath->layer;
0324 }
0325 dentry->d_fsdata = oe;
0326 if (upper_alias)
0327 ovl_dentry_set_upper_alias(dentry);
0328
0329 ovl_dentry_update_reval(dentry, upper,
0330 DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
0331
0332 return d_instantiate_anon(dentry, inode);
0333
0334 nomem:
0335 dput(dentry);
0336 dentry = ERR_PTR(-ENOMEM);
0337 out_iput:
0338 iput(inode);
0339 return dentry;
0340 }
0341
0342
0343 static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
0344 {
0345 struct ovl_entry *oe = dentry->d_fsdata;
0346 int i;
0347
0348 if (!idx)
0349 return ovl_dentry_upper(dentry);
0350
0351 for (i = 0; i < oe->numlower; i++) {
0352 if (oe->lowerstack[i].layer->idx == idx)
0353 return oe->lowerstack[i].dentry;
0354 }
0355
0356 return NULL;
0357 }
0358
0359
0360
0361
0362
0363
0364
0365 static struct dentry *ovl_lookup_real_one(struct dentry *connected,
0366 struct dentry *real,
0367 const struct ovl_layer *layer)
0368 {
0369 struct inode *dir = d_inode(connected);
0370 struct dentry *this, *parent = NULL;
0371 struct name_snapshot name;
0372 int err;
0373
0374
0375
0376
0377
0378
0379
0380
0381 inode_lock_nested(dir, I_MUTEX_PARENT);
0382 err = -ECHILD;
0383 parent = dget_parent(real);
0384 if (ovl_dentry_real_at(connected, layer->idx) != parent)
0385 goto fail;
0386
0387
0388
0389
0390
0391
0392
0393 take_dentry_name_snapshot(&name, real);
0394
0395
0396
0397
0398
0399 this = lookup_one_len(name.name.name, connected, name.name.len);
0400 release_dentry_name_snapshot(&name);
0401 err = PTR_ERR(this);
0402 if (IS_ERR(this)) {
0403 goto fail;
0404 } else if (!this || !this->d_inode) {
0405 dput(this);
0406 err = -ENOENT;
0407 goto fail;
0408 } else if (ovl_dentry_real_at(this, layer->idx) != real) {
0409 dput(this);
0410 err = -ESTALE;
0411 goto fail;
0412 }
0413
0414 out:
0415 dput(parent);
0416 inode_unlock(dir);
0417 return this;
0418
0419 fail:
0420 pr_warn_ratelimited("failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
0421 real, layer->idx, connected, err);
0422 this = ERR_PTR(err);
0423 goto out;
0424 }
0425
0426 static struct dentry *ovl_lookup_real(struct super_block *sb,
0427 struct dentry *real,
0428 const struct ovl_layer *layer);
0429
0430
0431
0432
0433 static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
0434 struct dentry *real,
0435 const struct ovl_layer *layer)
0436 {
0437 struct ovl_fs *ofs = sb->s_fs_info;
0438 struct dentry *index = NULL;
0439 struct dentry *this = NULL;
0440 struct inode *inode;
0441
0442
0443
0444
0445
0446 inode = ovl_lookup_inode(sb, real, !layer->idx);
0447 if (IS_ERR(inode))
0448 return ERR_CAST(inode);
0449 if (inode) {
0450 this = d_find_any_alias(inode);
0451 iput(inode);
0452 }
0453
0454
0455
0456
0457
0458 if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) {
0459 index = ovl_lookup_index(ofs, NULL, real, false);
0460 if (IS_ERR(index))
0461 return index;
0462 }
0463
0464
0465 if (index) {
0466 struct dentry *upper = ovl_index_upper(ofs, index);
0467
0468 dput(index);
0469 if (IS_ERR_OR_NULL(upper))
0470 return upper;
0471
0472
0473
0474
0475
0476
0477
0478
0479 this = ovl_lookup_real(sb, upper, &ofs->layers[0]);
0480 dput(upper);
0481 }
0482
0483 if (IS_ERR_OR_NULL(this))
0484 return this;
0485
0486 if (ovl_dentry_real_at(this, layer->idx) != real) {
0487 dput(this);
0488 this = ERR_PTR(-EIO);
0489 }
0490
0491 return this;
0492 }
0493
0494
0495
0496
0497
0498 static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
0499 struct dentry *real,
0500 const struct ovl_layer *layer)
0501 {
0502 struct dentry *next, *parent = NULL;
0503 struct dentry *ancestor = ERR_PTR(-EIO);
0504
0505 if (real == layer->mnt->mnt_root)
0506 return dget(sb->s_root);
0507
0508
0509 next = dget(real);
0510 for (;;) {
0511 parent = dget_parent(next);
0512
0513
0514
0515
0516
0517 ancestor = ovl_lookup_real_inode(sb, next, layer);
0518 if (ancestor)
0519 break;
0520
0521 if (parent == layer->mnt->mnt_root) {
0522 ancestor = dget(sb->s_root);
0523 break;
0524 }
0525
0526
0527
0528
0529
0530
0531 if (parent == next) {
0532 ancestor = ERR_PTR(-EXDEV);
0533 break;
0534 }
0535
0536 dput(next);
0537 next = parent;
0538 }
0539
0540 dput(parent);
0541 dput(next);
0542
0543 return ancestor;
0544 }
0545
0546
0547
0548
0549
0550
0551 static struct dentry *ovl_lookup_real(struct super_block *sb,
0552 struct dentry *real,
0553 const struct ovl_layer *layer)
0554 {
0555 struct dentry *connected;
0556 int err = 0;
0557
0558 connected = ovl_lookup_real_ancestor(sb, real, layer);
0559 if (IS_ERR(connected))
0560 return connected;
0561
0562 while (!err) {
0563 struct dentry *next, *this;
0564 struct dentry *parent = NULL;
0565 struct dentry *real_connected = ovl_dentry_real_at(connected,
0566 layer->idx);
0567
0568 if (real_connected == real)
0569 break;
0570
0571
0572 next = dget(real);
0573 for (;;) {
0574 parent = dget_parent(next);
0575
0576 if (parent == real_connected)
0577 break;
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587 if (parent == layer->mnt->mnt_root) {
0588 dput(connected);
0589 connected = dget(sb->s_root);
0590 break;
0591 }
0592
0593
0594
0595
0596
0597
0598
0599 if (parent == next) {
0600 err = -EXDEV;
0601 break;
0602 }
0603
0604 dput(next);
0605 next = parent;
0606 }
0607
0608 if (!err) {
0609 this = ovl_lookup_real_one(connected, next, layer);
0610 if (IS_ERR(this))
0611 err = PTR_ERR(this);
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623 if (err == -ECHILD) {
0624 this = ovl_lookup_real_ancestor(sb, real,
0625 layer);
0626 err = PTR_ERR_OR_ZERO(this);
0627 }
0628 if (!err) {
0629 dput(connected);
0630 connected = this;
0631 }
0632 }
0633
0634 dput(parent);
0635 dput(next);
0636 }
0637
0638 if (err)
0639 goto fail;
0640
0641 return connected;
0642
0643 fail:
0644 pr_warn_ratelimited("failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
0645 real, layer->idx, connected, err);
0646 dput(connected);
0647 return ERR_PTR(err);
0648 }
0649
0650
0651
0652
0653 static struct dentry *ovl_get_dentry(struct super_block *sb,
0654 struct dentry *upper,
0655 struct ovl_path *lowerpath,
0656 struct dentry *index)
0657 {
0658 struct ovl_fs *ofs = sb->s_fs_info;
0659 const struct ovl_layer *layer = upper ? &ofs->layers[0] : lowerpath->layer;
0660 struct dentry *real = upper ?: (index ?: lowerpath->dentry);
0661
0662
0663
0664
0665
0666 if (!d_is_dir(real))
0667 return ovl_obtain_alias(sb, upper, lowerpath, index);
0668
0669
0670 if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
0671 return ERR_PTR(-ENOENT);
0672
0673
0674
0675
0676
0677 return ovl_lookup_real(sb, real, layer);
0678 }
0679
0680 static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
0681 struct ovl_fh *fh)
0682 {
0683 struct ovl_fs *ofs = sb->s_fs_info;
0684 struct dentry *dentry;
0685 struct dentry *upper;
0686
0687 if (!ovl_upper_mnt(ofs))
0688 return ERR_PTR(-EACCES);
0689
0690 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true);
0691 if (IS_ERR_OR_NULL(upper))
0692 return upper;
0693
0694 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
0695 dput(upper);
0696
0697 return dentry;
0698 }
0699
0700 static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
0701 struct ovl_fh *fh)
0702 {
0703 struct ovl_fs *ofs = sb->s_fs_info;
0704 struct ovl_path origin = { };
0705 struct ovl_path *stack = &origin;
0706 struct dentry *dentry = NULL;
0707 struct dentry *index = NULL;
0708 struct inode *inode;
0709 int err;
0710
0711
0712 err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
0713 if (err)
0714 return ERR_PTR(err);
0715
0716 if (!d_is_dir(origin.dentry) ||
0717 !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
0718 inode = ovl_lookup_inode(sb, origin.dentry, false);
0719 err = PTR_ERR(inode);
0720 if (IS_ERR(inode))
0721 goto out_err;
0722 if (inode) {
0723 dentry = d_find_any_alias(inode);
0724 iput(inode);
0725 if (dentry)
0726 goto out;
0727 }
0728 }
0729
0730
0731 if (ofs->indexdir) {
0732 index = ovl_get_index_fh(ofs, fh);
0733 err = PTR_ERR(index);
0734 if (IS_ERR(index)) {
0735 index = NULL;
0736 goto out_err;
0737 }
0738 }
0739
0740
0741 if (index && d_is_dir(index)) {
0742 struct dentry *upper = ovl_index_upper(ofs, index);
0743
0744 err = PTR_ERR(upper);
0745 if (IS_ERR_OR_NULL(upper))
0746 goto out_err;
0747
0748 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
0749 dput(upper);
0750 goto out;
0751 }
0752
0753
0754 if (d_is_dir(origin.dentry)) {
0755 dput(origin.dentry);
0756 origin.dentry = NULL;
0757 err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack);
0758 if (err)
0759 goto out_err;
0760 }
0761 if (index) {
0762 err = ovl_verify_origin(ofs, index, origin.dentry, false);
0763 if (err)
0764 goto out_err;
0765 }
0766
0767
0768 dentry = ovl_get_dentry(sb, NULL, &origin, index);
0769
0770 out:
0771 dput(origin.dentry);
0772 dput(index);
0773 return dentry;
0774
0775 out_err:
0776 dentry = ERR_PTR(err);
0777 goto out;
0778 }
0779
0780 static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type)
0781 {
0782 struct ovl_fh *fh;
0783
0784
0785 if (fh_type == OVL_FILEID_V1)
0786 return (struct ovl_fh *)fid;
0787
0788 if (fh_type != OVL_FILEID_V0)
0789 return ERR_PTR(-EINVAL);
0790
0791 if (buflen <= OVL_FH_WIRE_OFFSET)
0792 return ERR_PTR(-EINVAL);
0793
0794 fh = kzalloc(buflen, GFP_KERNEL);
0795 if (!fh)
0796 return ERR_PTR(-ENOMEM);
0797
0798
0799 memcpy(&fh->fb, fid, buflen - OVL_FH_WIRE_OFFSET);
0800 return fh;
0801 }
0802
0803 static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
0804 int fh_len, int fh_type)
0805 {
0806 struct dentry *dentry = NULL;
0807 struct ovl_fh *fh = NULL;
0808 int len = fh_len << 2;
0809 unsigned int flags = 0;
0810 int err;
0811
0812 fh = ovl_fid_to_fh(fid, len, fh_type);
0813 err = PTR_ERR(fh);
0814 if (IS_ERR(fh))
0815 goto out_err;
0816
0817 err = ovl_check_fh_len(fh, len);
0818 if (err)
0819 goto out_err;
0820
0821 flags = fh->fb.flags;
0822 dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
0823 ovl_upper_fh_to_d(sb, fh) :
0824 ovl_lower_fh_to_d(sb, fh);
0825 err = PTR_ERR(dentry);
0826 if (IS_ERR(dentry) && err != -ESTALE)
0827 goto out_err;
0828
0829 out:
0830
0831 if (!IS_ERR_OR_NULL(fh) && fh != (void *)fid)
0832 kfree(fh);
0833
0834 return dentry;
0835
0836 out_err:
0837 pr_warn_ratelimited("failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
0838 fh_len, fh_type, flags, err);
0839 dentry = ERR_PTR(err);
0840 goto out;
0841 }
0842
0843 static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
0844 int fh_len, int fh_type)
0845 {
0846 pr_warn_ratelimited("connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
0847 return ERR_PTR(-EACCES);
0848 }
0849
0850 static int ovl_get_name(struct dentry *parent, char *name,
0851 struct dentry *child)
0852 {
0853
0854
0855
0856
0857 WARN_ON_ONCE(1);
0858 return -EIO;
0859 }
0860
0861 static struct dentry *ovl_get_parent(struct dentry *dentry)
0862 {
0863
0864
0865
0866
0867 WARN_ON_ONCE(1);
0868 return ERR_PTR(-EIO);
0869 }
0870
0871 const struct export_operations ovl_export_operations = {
0872 .encode_fh = ovl_encode_fh,
0873 .fh_to_dentry = ovl_fh_to_dentry,
0874 .fh_to_parent = ovl_fh_to_parent,
0875 .get_name = ovl_get_name,
0876 .get_parent = ovl_get_parent,
0877 };