0001
0002
0003
0004
0005
0006
0007 #include <linux/fs.h>
0008 #include <linux/mount.h>
0009 #include <linux/slab.h>
0010 #include <linux/cred.h>
0011 #include <linux/xattr.h>
0012 #include <linux/exportfs.h>
0013 #include <linux/fileattr.h>
0014 #include <linux/uuid.h>
0015 #include <linux/namei.h>
0016 #include <linux/ratelimit.h>
0017 #include "overlayfs.h"
0018
0019 int ovl_want_write(struct dentry *dentry)
0020 {
0021 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
0022 return mnt_want_write(ovl_upper_mnt(ofs));
0023 }
0024
0025 void ovl_drop_write(struct dentry *dentry)
0026 {
0027 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
0028 mnt_drop_write(ovl_upper_mnt(ofs));
0029 }
0030
0031 struct dentry *ovl_workdir(struct dentry *dentry)
0032 {
0033 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
0034 return ofs->workdir;
0035 }
0036
0037 const struct cred *ovl_override_creds(struct super_block *sb)
0038 {
0039 struct ovl_fs *ofs = sb->s_fs_info;
0040
0041 return override_creds(ofs->creator_cred);
0042 }
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 int ovl_can_decode_fh(struct super_block *sb)
0053 {
0054 if (!capable(CAP_DAC_READ_SEARCH))
0055 return 0;
0056
0057 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
0058 return 0;
0059
0060 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
0061 }
0062
0063 struct dentry *ovl_indexdir(struct super_block *sb)
0064 {
0065 struct ovl_fs *ofs = sb->s_fs_info;
0066
0067 return ofs->indexdir;
0068 }
0069
0070
0071 bool ovl_index_all(struct super_block *sb)
0072 {
0073 struct ovl_fs *ofs = sb->s_fs_info;
0074
0075 return ofs->config.nfs_export && ofs->config.index;
0076 }
0077
0078
0079 bool ovl_verify_lower(struct super_block *sb)
0080 {
0081 struct ovl_fs *ofs = sb->s_fs_info;
0082
0083 return ofs->config.nfs_export && ofs->config.index;
0084 }
0085
0086 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
0087 {
0088 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
0089 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
0090
0091 if (oe)
0092 oe->numlower = numlower;
0093
0094 return oe;
0095 }
0096
0097 bool ovl_dentry_remote(struct dentry *dentry)
0098 {
0099 return dentry->d_flags &
0100 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
0101 }
0102
0103 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
0104 unsigned int mask)
0105 {
0106 struct ovl_entry *oe = OVL_E(dentry);
0107 unsigned int i, flags = 0;
0108
0109 if (upperdentry)
0110 flags |= upperdentry->d_flags;
0111 for (i = 0; i < oe->numlower; i++)
0112 flags |= oe->lowerstack[i].dentry->d_flags;
0113
0114 spin_lock(&dentry->d_lock);
0115 dentry->d_flags &= ~mask;
0116 dentry->d_flags |= flags & mask;
0117 spin_unlock(&dentry->d_lock);
0118 }
0119
0120 bool ovl_dentry_weird(struct dentry *dentry)
0121 {
0122 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
0123 DCACHE_MANAGE_TRANSIT |
0124 DCACHE_OP_HASH |
0125 DCACHE_OP_COMPARE);
0126 }
0127
0128 enum ovl_path_type ovl_path_type(struct dentry *dentry)
0129 {
0130 struct ovl_entry *oe = dentry->d_fsdata;
0131 enum ovl_path_type type = 0;
0132
0133 if (ovl_dentry_upper(dentry)) {
0134 type = __OVL_PATH_UPPER;
0135
0136
0137
0138
0139 if (oe->numlower) {
0140 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
0141 type |= __OVL_PATH_ORIGIN;
0142 if (d_is_dir(dentry) ||
0143 !ovl_has_upperdata(d_inode(dentry)))
0144 type |= __OVL_PATH_MERGE;
0145 }
0146 } else {
0147 if (oe->numlower > 1)
0148 type |= __OVL_PATH_MERGE;
0149 }
0150 return type;
0151 }
0152
0153 void ovl_path_upper(struct dentry *dentry, struct path *path)
0154 {
0155 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
0156
0157 path->mnt = ovl_upper_mnt(ofs);
0158 path->dentry = ovl_dentry_upper(dentry);
0159 }
0160
0161 void ovl_path_lower(struct dentry *dentry, struct path *path)
0162 {
0163 struct ovl_entry *oe = dentry->d_fsdata;
0164
0165 if (oe->numlower) {
0166 path->mnt = oe->lowerstack[0].layer->mnt;
0167 path->dentry = oe->lowerstack[0].dentry;
0168 } else {
0169 *path = (struct path) { };
0170 }
0171 }
0172
0173 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
0174 {
0175 struct ovl_entry *oe = dentry->d_fsdata;
0176
0177 if (oe->numlower) {
0178 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
0179 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
0180 } else {
0181 *path = (struct path) { };
0182 }
0183 }
0184
0185 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
0186 {
0187 enum ovl_path_type type = ovl_path_type(dentry);
0188
0189 if (!OVL_TYPE_UPPER(type))
0190 ovl_path_lower(dentry, path);
0191 else
0192 ovl_path_upper(dentry, path);
0193
0194 return type;
0195 }
0196
0197 enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
0198 {
0199 enum ovl_path_type type = ovl_path_type(dentry);
0200
0201 WARN_ON_ONCE(d_is_dir(dentry));
0202
0203 if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
0204 ovl_path_lowerdata(dentry, path);
0205 else
0206 ovl_path_upper(dentry, path);
0207
0208 return type;
0209 }
0210
0211 struct dentry *ovl_dentry_upper(struct dentry *dentry)
0212 {
0213 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
0214 }
0215
0216 struct dentry *ovl_dentry_lower(struct dentry *dentry)
0217 {
0218 struct ovl_entry *oe = dentry->d_fsdata;
0219
0220 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
0221 }
0222
0223 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
0224 {
0225 struct ovl_entry *oe = dentry->d_fsdata;
0226
0227 return oe->numlower ? oe->lowerstack[0].layer : NULL;
0228 }
0229
0230
0231
0232
0233
0234
0235
0236 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
0237 {
0238 struct ovl_entry *oe = dentry->d_fsdata;
0239
0240 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
0241 }
0242
0243 struct dentry *ovl_dentry_real(struct dentry *dentry)
0244 {
0245 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
0246 }
0247
0248 struct dentry *ovl_i_dentry_upper(struct inode *inode)
0249 {
0250 return ovl_upperdentry_dereference(OVL_I(inode));
0251 }
0252
0253 void ovl_i_path_real(struct inode *inode, struct path *path)
0254 {
0255 path->dentry = ovl_i_dentry_upper(inode);
0256 if (!path->dentry) {
0257 path->dentry = OVL_I(inode)->lowerpath.dentry;
0258 path->mnt = OVL_I(inode)->lowerpath.layer->mnt;
0259 } else {
0260 path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
0261 }
0262 }
0263
0264 struct inode *ovl_inode_upper(struct inode *inode)
0265 {
0266 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
0267
0268 return upperdentry ? d_inode(upperdentry) : NULL;
0269 }
0270
0271 struct inode *ovl_inode_lower(struct inode *inode)
0272 {
0273 struct dentry *lowerdentry = OVL_I(inode)->lowerpath.dentry;
0274
0275 return lowerdentry ? d_inode(lowerdentry) : NULL;
0276 }
0277
0278 struct inode *ovl_inode_real(struct inode *inode)
0279 {
0280 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
0281 }
0282
0283
0284 struct inode *ovl_inode_lowerdata(struct inode *inode)
0285 {
0286 if (WARN_ON(!S_ISREG(inode->i_mode)))
0287 return NULL;
0288
0289 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
0290 }
0291
0292
0293 struct inode *ovl_inode_realdata(struct inode *inode)
0294 {
0295 struct inode *upperinode;
0296
0297 upperinode = ovl_inode_upper(inode);
0298 if (upperinode && ovl_has_upperdata(inode))
0299 return upperinode;
0300
0301 return ovl_inode_lowerdata(inode);
0302 }
0303
0304 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
0305 {
0306 return OVL_I(inode)->cache;
0307 }
0308
0309 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
0310 {
0311 OVL_I(inode)->cache = cache;
0312 }
0313
0314 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
0315 {
0316 set_bit(flag, &OVL_E(dentry)->flags);
0317 }
0318
0319 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
0320 {
0321 clear_bit(flag, &OVL_E(dentry)->flags);
0322 }
0323
0324 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
0325 {
0326 return test_bit(flag, &OVL_E(dentry)->flags);
0327 }
0328
0329 bool ovl_dentry_is_opaque(struct dentry *dentry)
0330 {
0331 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
0332 }
0333
0334 bool ovl_dentry_is_whiteout(struct dentry *dentry)
0335 {
0336 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
0337 }
0338
0339 void ovl_dentry_set_opaque(struct dentry *dentry)
0340 {
0341 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
0342 }
0343
0344
0345
0346
0347
0348
0349
0350 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
0351 {
0352 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
0353 }
0354
0355 void ovl_dentry_set_upper_alias(struct dentry *dentry)
0356 {
0357 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
0358 }
0359
0360 static bool ovl_should_check_upperdata(struct inode *inode)
0361 {
0362 if (!S_ISREG(inode->i_mode))
0363 return false;
0364
0365 if (!ovl_inode_lower(inode))
0366 return false;
0367
0368 return true;
0369 }
0370
0371 bool ovl_has_upperdata(struct inode *inode)
0372 {
0373 if (!ovl_should_check_upperdata(inode))
0374 return true;
0375
0376 if (!ovl_test_flag(OVL_UPPERDATA, inode))
0377 return false;
0378
0379
0380
0381
0382
0383
0384 smp_rmb();
0385 return true;
0386 }
0387
0388 void ovl_set_upperdata(struct inode *inode)
0389 {
0390
0391
0392
0393
0394
0395 smp_wmb();
0396 ovl_set_flag(OVL_UPPERDATA, inode);
0397 }
0398
0399
0400 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
0401 {
0402 if (!ovl_open_flags_need_copy_up(flags))
0403 return false;
0404
0405 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
0406 }
0407
0408 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
0409 {
0410 if (!ovl_open_flags_need_copy_up(flags))
0411 return false;
0412
0413 return !ovl_has_upperdata(d_inode(dentry));
0414 }
0415
0416 bool ovl_redirect_dir(struct super_block *sb)
0417 {
0418 struct ovl_fs *ofs = sb->s_fs_info;
0419
0420 return ofs->config.redirect_dir && !ofs->noxattr;
0421 }
0422
0423 const char *ovl_dentry_get_redirect(struct dentry *dentry)
0424 {
0425 return OVL_I(d_inode(dentry))->redirect;
0426 }
0427
0428 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
0429 {
0430 struct ovl_inode *oi = OVL_I(d_inode(dentry));
0431
0432 kfree(oi->redirect);
0433 oi->redirect = redirect;
0434 }
0435
0436 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
0437 {
0438 struct inode *upperinode = d_inode(upperdentry);
0439
0440 WARN_ON(OVL_I(inode)->__upperdentry);
0441
0442
0443
0444
0445 smp_wmb();
0446 OVL_I(inode)->__upperdentry = upperdentry;
0447 if (inode_unhashed(inode)) {
0448 inode->i_private = upperinode;
0449 __insert_inode_hash(inode, (unsigned long) upperinode);
0450 }
0451 }
0452
0453 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
0454 {
0455 struct inode *inode = d_inode(dentry);
0456
0457 WARN_ON(!inode_is_locked(inode));
0458 WARN_ON(!d_is_dir(dentry));
0459
0460
0461
0462
0463
0464
0465
0466 if (!ovl_dir_is_real(dentry) || impurity)
0467 OVL_I(inode)->version++;
0468 }
0469
0470 void ovl_dir_modified(struct dentry *dentry, bool impurity)
0471 {
0472
0473 ovl_copyattr(d_inode(dentry));
0474
0475 ovl_dir_version_inc(dentry, impurity);
0476 }
0477
0478 u64 ovl_dentry_version_get(struct dentry *dentry)
0479 {
0480 struct inode *inode = d_inode(dentry);
0481
0482 WARN_ON(!inode_is_locked(inode));
0483 return OVL_I(inode)->version;
0484 }
0485
0486 bool ovl_is_whiteout(struct dentry *dentry)
0487 {
0488 struct inode *inode = dentry->d_inode;
0489
0490 return inode && IS_WHITEOUT(inode);
0491 }
0492
0493 struct file *ovl_path_open(struct path *path, int flags)
0494 {
0495 struct inode *inode = d_inode(path->dentry);
0496 struct user_namespace *real_mnt_userns = mnt_user_ns(path->mnt);
0497 int err, acc_mode;
0498
0499 if (flags & ~(O_ACCMODE | O_LARGEFILE))
0500 BUG();
0501
0502 switch (flags & O_ACCMODE) {
0503 case O_RDONLY:
0504 acc_mode = MAY_READ;
0505 break;
0506 case O_WRONLY:
0507 acc_mode = MAY_WRITE;
0508 break;
0509 default:
0510 BUG();
0511 }
0512
0513 err = inode_permission(real_mnt_userns, inode, acc_mode | MAY_OPEN);
0514 if (err)
0515 return ERR_PTR(err);
0516
0517
0518 if (inode_owner_or_capable(real_mnt_userns, inode))
0519 flags |= O_NOATIME;
0520
0521 return dentry_open(path, flags, current_cred());
0522 }
0523
0524
0525 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
0526 {
0527 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
0528
0529 if (ovl_dentry_upper(dentry) &&
0530 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
0531 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
0532 return true;
0533
0534 return false;
0535 }
0536
0537 bool ovl_already_copied_up(struct dentry *dentry, int flags)
0538 {
0539 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554 if (ovl_dentry_upper(dentry) &&
0555 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
0556 !ovl_dentry_needs_data_copy_up(dentry, flags))
0557 return true;
0558
0559 return false;
0560 }
0561
0562 int ovl_copy_up_start(struct dentry *dentry, int flags)
0563 {
0564 struct inode *inode = d_inode(dentry);
0565 int err;
0566
0567 err = ovl_inode_lock_interruptible(inode);
0568 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
0569 err = 1;
0570 ovl_inode_unlock(inode);
0571 }
0572
0573 return err;
0574 }
0575
0576 void ovl_copy_up_end(struct dentry *dentry)
0577 {
0578 ovl_inode_unlock(d_inode(dentry));
0579 }
0580
0581 bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, struct path *path)
0582 {
0583 int res;
0584
0585 res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
0586
0587
0588 if (res >= 0)
0589 return true;
0590
0591 return false;
0592 }
0593
0594 bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, struct path *path,
0595 enum ovl_xattr ox)
0596 {
0597 int res;
0598 char val;
0599
0600 if (!d_is_dir(path->dentry))
0601 return false;
0602
0603 res = ovl_path_getxattr(ofs, path, ox, &val, 1);
0604 if (res == 1 && val == 'y')
0605 return true;
0606
0607 return false;
0608 }
0609
0610 #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
0611 #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
0612 #define OVL_XATTR_ORIGIN_POSTFIX "origin"
0613 #define OVL_XATTR_IMPURE_POSTFIX "impure"
0614 #define OVL_XATTR_NLINK_POSTFIX "nlink"
0615 #define OVL_XATTR_UPPER_POSTFIX "upper"
0616 #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
0617 #define OVL_XATTR_PROTATTR_POSTFIX "protattr"
0618
0619 #define OVL_XATTR_TAB_ENTRY(x) \
0620 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
0621 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
0622
0623 const char *const ovl_xattr_table[][2] = {
0624 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
0625 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
0626 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
0627 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
0628 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
0629 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
0630 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
0631 OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
0632 };
0633
0634 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
0635 enum ovl_xattr ox, const void *value, size_t size,
0636 int xerr)
0637 {
0638 int err;
0639
0640 if (ofs->noxattr)
0641 return xerr;
0642
0643 err = ovl_setxattr(ofs, upperdentry, ox, value, size);
0644
0645 if (err == -EOPNOTSUPP) {
0646 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
0647 ofs->noxattr = true;
0648 return xerr;
0649 }
0650
0651 return err;
0652 }
0653
0654 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
0655 {
0656 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
0657 int err;
0658
0659 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
0660 return 0;
0661
0662
0663
0664
0665
0666 err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
0667 if (!err)
0668 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
0669
0670 return err;
0671 }
0672
0673
0674 #define OVL_PROTATTR_MAX 32
0675
0676 void ovl_check_protattr(struct inode *inode, struct dentry *upper)
0677 {
0678 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
0679 u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
0680 char buf[OVL_PROTATTR_MAX+1];
0681 int res, n;
0682
0683 res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
0684 OVL_PROTATTR_MAX);
0685 if (res < 0)
0686 return;
0687
0688
0689
0690
0691
0692
0693
0694 for (n = 0; n < res; n++) {
0695 if (buf[n] == 'a')
0696 iflags |= S_APPEND;
0697 else if (buf[n] == 'i')
0698 iflags |= S_IMMUTABLE;
0699 else
0700 break;
0701 }
0702
0703 if (!res || n < res) {
0704 pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
0705 upper, res);
0706 } else {
0707 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
0708 }
0709 }
0710
0711 int ovl_set_protattr(struct inode *inode, struct dentry *upper,
0712 struct fileattr *fa)
0713 {
0714 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
0715 char buf[OVL_PROTATTR_MAX];
0716 int len = 0, err = 0;
0717 u32 iflags = 0;
0718
0719 BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
0720
0721 if (fa->flags & FS_APPEND_FL) {
0722 buf[len++] = 'a';
0723 iflags |= S_APPEND;
0724 }
0725 if (fa->flags & FS_IMMUTABLE_FL) {
0726 buf[len++] = 'i';
0727 iflags |= S_IMMUTABLE;
0728 }
0729
0730
0731
0732
0733
0734
0735 if (len) {
0736 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
0737 buf, len, -EPERM);
0738 } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
0739 err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
0740 if (err == -EOPNOTSUPP || err == -ENODATA)
0741 err = 0;
0742 }
0743 if (err)
0744 return err;
0745
0746 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
0747
0748
0749 fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
0750 fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
0751
0752 return 0;
0753 }
0754
0755
0756
0757
0758
0759 bool ovl_inuse_trylock(struct dentry *dentry)
0760 {
0761 struct inode *inode = d_inode(dentry);
0762 bool locked = false;
0763
0764 spin_lock(&inode->i_lock);
0765 if (!(inode->i_state & I_OVL_INUSE)) {
0766 inode->i_state |= I_OVL_INUSE;
0767 locked = true;
0768 }
0769 spin_unlock(&inode->i_lock);
0770
0771 return locked;
0772 }
0773
0774 void ovl_inuse_unlock(struct dentry *dentry)
0775 {
0776 if (dentry) {
0777 struct inode *inode = d_inode(dentry);
0778
0779 spin_lock(&inode->i_lock);
0780 WARN_ON(!(inode->i_state & I_OVL_INUSE));
0781 inode->i_state &= ~I_OVL_INUSE;
0782 spin_unlock(&inode->i_lock);
0783 }
0784 }
0785
0786 bool ovl_is_inuse(struct dentry *dentry)
0787 {
0788 struct inode *inode = d_inode(dentry);
0789 bool inuse;
0790
0791 spin_lock(&inode->i_lock);
0792 inuse = (inode->i_state & I_OVL_INUSE);
0793 spin_unlock(&inode->i_lock);
0794
0795 return inuse;
0796 }
0797
0798
0799
0800
0801 bool ovl_need_index(struct dentry *dentry)
0802 {
0803 struct dentry *lower = ovl_dentry_lower(dentry);
0804
0805 if (!lower || !ovl_indexdir(dentry->d_sb))
0806 return false;
0807
0808
0809 if (ovl_index_all(dentry->d_sb))
0810 return true;
0811
0812
0813 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
0814 return true;
0815
0816 return false;
0817 }
0818
0819
0820 static void ovl_cleanup_index(struct dentry *dentry)
0821 {
0822 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
0823 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
0824 struct inode *dir = indexdir->d_inode;
0825 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
0826 struct dentry *upperdentry = ovl_dentry_upper(dentry);
0827 struct dentry *index = NULL;
0828 struct inode *inode;
0829 struct qstr name = { };
0830 int err;
0831
0832 err = ovl_get_index_name(ofs, lowerdentry, &name);
0833 if (err)
0834 goto fail;
0835
0836 inode = d_inode(upperdentry);
0837 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
0838 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
0839 upperdentry, inode->i_ino, inode->i_nlink);
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849 set_nlink(d_inode(dentry), inode->i_nlink - 1);
0850 ovl_set_nlink_upper(dentry);
0851 goto out;
0852 }
0853
0854 inode_lock_nested(dir, I_MUTEX_PARENT);
0855 index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
0856 err = PTR_ERR(index);
0857 if (IS_ERR(index)) {
0858 index = NULL;
0859 } else if (ovl_index_all(dentry->d_sb)) {
0860
0861 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
0862 dir, index);
0863 } else {
0864
0865 err = ovl_cleanup(ofs, dir, index);
0866 }
0867
0868 inode_unlock(dir);
0869 if (err)
0870 goto fail;
0871
0872 out:
0873 kfree(name.name);
0874 dput(index);
0875 return;
0876
0877 fail:
0878 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
0879 goto out;
0880 }
0881
0882
0883
0884
0885
0886 int ovl_nlink_start(struct dentry *dentry)
0887 {
0888 struct inode *inode = d_inode(dentry);
0889 const struct cred *old_cred;
0890 int err;
0891
0892 if (WARN_ON(!inode))
0893 return -ENOENT;
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
0910 err = ovl_copy_up(dentry);
0911 if (err)
0912 return err;
0913 }
0914
0915 err = ovl_inode_lock_interruptible(inode);
0916 if (err)
0917 return err;
0918
0919 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
0920 goto out;
0921
0922 old_cred = ovl_override_creds(dentry->d_sb);
0923
0924
0925
0926
0927
0928
0929 err = ovl_set_nlink_upper(dentry);
0930 revert_creds(old_cred);
0931
0932 out:
0933 if (err)
0934 ovl_inode_unlock(inode);
0935
0936 return err;
0937 }
0938
0939 void ovl_nlink_end(struct dentry *dentry)
0940 {
0941 struct inode *inode = d_inode(dentry);
0942
0943 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
0944 const struct cred *old_cred;
0945
0946 old_cred = ovl_override_creds(dentry->d_sb);
0947 ovl_cleanup_index(dentry);
0948 revert_creds(old_cred);
0949 }
0950
0951 ovl_inode_unlock(inode);
0952 }
0953
0954 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
0955 {
0956
0957 if (workdir == upperdir)
0958 goto err;
0959
0960
0961 if (lock_rename(workdir, upperdir) != NULL)
0962 goto err_unlock;
0963
0964 return 0;
0965
0966 err_unlock:
0967 unlock_rename(workdir, upperdir);
0968 err:
0969 pr_err("failed to lock workdir+upperdir\n");
0970 return -EIO;
0971 }
0972
0973
0974 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct path *path)
0975 {
0976 int res;
0977
0978
0979 if (!S_ISREG(d_inode(path->dentry)->i_mode))
0980 return 0;
0981
0982 res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0);
0983 if (res < 0) {
0984 if (res == -ENODATA || res == -EOPNOTSUPP)
0985 return 0;
0986
0987
0988
0989
0990
0991 if (ofs->config.userxattr && res == -EACCES)
0992 return 0;
0993 goto out;
0994 }
0995
0996 return 1;
0997 out:
0998 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
0999 return res;
1000 }
1001
1002 bool ovl_is_metacopy_dentry(struct dentry *dentry)
1003 {
1004 struct ovl_entry *oe = dentry->d_fsdata;
1005
1006 if (!d_is_reg(dentry))
1007 return false;
1008
1009 if (ovl_dentry_upper(dentry)) {
1010 if (!ovl_has_upperdata(d_inode(dentry)))
1011 return true;
1012 return false;
1013 }
1014
1015 return (oe->numlower > 1);
1016 }
1017
1018 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct path *path, int padding)
1019 {
1020 int res;
1021 char *s, *next, *buf = NULL;
1022
1023 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
1024 if (res == -ENODATA || res == -EOPNOTSUPP)
1025 return NULL;
1026 if (res < 0)
1027 goto fail;
1028 if (res == 0)
1029 goto invalid;
1030
1031 buf = kzalloc(res + padding + 1, GFP_KERNEL);
1032 if (!buf)
1033 return ERR_PTR(-ENOMEM);
1034
1035 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
1036 if (res < 0)
1037 goto fail;
1038 if (res == 0)
1039 goto invalid;
1040
1041 if (buf[0] == '/') {
1042 for (s = buf; *s++ == '/'; s = next) {
1043 next = strchrnul(s, '/');
1044 if (s == next)
1045 goto invalid;
1046 }
1047 } else {
1048 if (strchr(buf, '/') != NULL)
1049 goto invalid;
1050 }
1051
1052 return buf;
1053 invalid:
1054 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
1055 res = -EINVAL;
1056 goto err_free;
1057 fail:
1058 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1059 err_free:
1060 kfree(buf);
1061 return ERR_PTR(res);
1062 }
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077 int ovl_sync_status(struct ovl_fs *ofs)
1078 {
1079 struct vfsmount *mnt;
1080
1081 if (ovl_should_sync(ofs))
1082 return 1;
1083
1084 mnt = ovl_upper_mnt(ofs);
1085 if (!mnt)
1086 return 0;
1087
1088 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1089 }
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102 void ovl_copyattr(struct inode *inode)
1103 {
1104 struct path realpath;
1105 struct inode *realinode;
1106 struct user_namespace *real_mnt_userns;
1107
1108 ovl_i_path_real(inode, &realpath);
1109 realinode = d_inode(realpath.dentry);
1110 real_mnt_userns = mnt_user_ns(realpath.mnt);
1111
1112 inode->i_uid = i_uid_into_mnt(real_mnt_userns, realinode);
1113 inode->i_gid = i_gid_into_mnt(real_mnt_userns, realinode);
1114 inode->i_mode = realinode->i_mode;
1115 inode->i_atime = realinode->i_atime;
1116 inode->i_mtime = realinode->i_mtime;
1117 inode->i_ctime = realinode->i_ctime;
1118 i_size_write(inode, i_size_read(realinode));
1119 }