1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (C) 2011 Novell Inc. 5 */ 6 7 #include <linux/module.h> 8 #include <linux/fs.h> 9 #include <linux/slab.h> 10 #include <linux/file.h> 11 #include <linux/fileattr.h> 12 #include <linux/splice.h> 13 #include <linux/xattr.h> 14 #include <linux/security.h> 15 #include <linux/uaccess.h> 16 #include <linux/sched/signal.h> 17 #include <linux/cred.h> 18 #include <linux/namei.h> 19 #include <linux/fdtable.h> 20 #include <linux/ratelimit.h> 21 #include <linux/exportfs.h> 22 #include "overlayfs.h" 23 24 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) 25 26 static int ovl_ccup_set(const char *buf, const struct kernel_param *param) 27 { 28 pr_warn("\"check_copy_up\" module option is obsolete\n"); 29 return 0; 30 } 31 32 static int ovl_ccup_get(char *buf, const struct kernel_param *param) 33 { 34 return sprintf(buf, "N\n"); 35 } 36 37 module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644); 38 MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing"); 39 40 static bool ovl_must_copy_xattr(const char *name) 41 { 42 return !strcmp(name, XATTR_POSIX_ACL_ACCESS) || 43 !strcmp(name, XATTR_POSIX_ACL_DEFAULT) || 44 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN); 45 } 46 47 int ovl_copy_xattr(struct super_block *sb, struct dentry *old, 48 struct dentry *new) 49 { 50 ssize_t list_size, size, value_size = 0; 51 char *buf, *name, *value = NULL; 52 int error = 0; 53 size_t slen; 54 55 if (!(old->d_inode->i_opflags & IOP_XATTR) || 56 !(new->d_inode->i_opflags & IOP_XATTR)) 57 return 0; 58 59 list_size = vfs_listxattr(old, NULL, 0); 60 if (list_size <= 0) { 61 if (list_size == -EOPNOTSUPP) 62 return 0; 63 return list_size; 64 } 65 66 buf = kvzalloc(list_size, GFP_KERNEL); 67 if (!buf) 68 return -ENOMEM; 69 70 list_size = vfs_listxattr(old, buf, list_size); 71 if (list_size <= 0) { 72 error = list_size; 73 goto out; 74 } 75 76 for (name = buf; list_size; name += slen) { 77 slen = strnlen(name, list_size) + 1; 78 79 /* underlying fs providing us with an broken xattr list? */ 80 if (WARN_ON(slen > list_size)) { 81 error = -EIO; 82 break; 83 } 84 list_size -= slen; 85 86 if (ovl_is_private_xattr(sb, name)) 87 continue; 88 89 error = security_inode_copy_up_xattr(name); 90 if (error < 0 && error != -EOPNOTSUPP) 91 break; 92 if (error == 1) { 93 error = 0; 94 continue; /* Discard */ 95 } 96 retry: 97 size = vfs_getxattr(&init_user_ns, old, name, value, value_size); 98 if (size == -ERANGE) 99 size = vfs_getxattr(&init_user_ns, old, name, NULL, 0); 100 101 if (size < 0) { 102 error = size; 103 break; 104 } 105 106 if (size > value_size) { 107 void *new; 108 109 new = kvmalloc(size, GFP_KERNEL); 110 if (!new) { 111 error = -ENOMEM; 112 break; 113 } 114 kvfree(value); 115 value = new; 116 value_size = size; 117 goto retry; 118 } 119 120 error = ovl_do_setxattr(OVL_FS(sb), new, name, value, size, 0); 121 if (error) { 122 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name)) 123 break; 124 125 /* Ignore failure to copy unknown xattrs */ 126 error = 0; 127 } 128 } 129 kvfree(value); 130 out: 131 kvfree(buf); 132 return error; 133 } 134 135 static int ovl_copy_fileattr(struct inode *inode, struct path *old, 136 struct path *new) 137 { 138 struct fileattr oldfa = { .flags_valid = true }; 139 struct fileattr newfa = { .flags_valid = true }; 140 int err; 141 142 err = ovl_real_fileattr_get(old, &oldfa); 143 if (err) { 144 /* Ntfs-3g returns -EINVAL for "no fileattr support" */ 145 if (err == -ENOTTY || err == -EINVAL) 146 return 0; 147 pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n", 148 old->dentry, err); 149 return err; 150 } 151 152 /* 153 * We cannot set immutable and append-only flags on upper inode, 154 * because we would not be able to link upper inode to upper dir 155 * not set overlay private xattr on upper inode. 156 * Store these flags in overlay.protattr xattr instead. 157 */ 158 if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) { 159 err = ovl_set_protattr(inode, new->dentry, &oldfa); 160 if (err == -EPERM) 161 pr_warn_once("copying fileattr: no xattr on upper\n"); 162 else if (err) 163 return err; 164 } 165 166 /* Don't bother copying flags if none are set */ 167 if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK)) 168 return 0; 169 170 err = ovl_real_fileattr_get(new, &newfa); 171 if (err) { 172 /* 173 * Returning an error if upper doesn't support fileattr will 174 * result in a regression, so revert to the old behavior. 175 */ 176 if (err == -ENOTTY || err == -EINVAL) { 177 pr_warn_once("copying fileattr: no support on upper\n"); 178 return 0; 179 } 180 pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n", 181 new->dentry, err); 182 return err; 183 } 184 185 BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL); 186 newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK; 187 newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK); 188 189 BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON); 190 newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK; 191 newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK); 192 193 return ovl_real_fileattr_set(new, &newfa); 194 } 195 196 static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old, 197 struct path *new, loff_t len) 198 { 199 struct file *old_file; 200 struct file *new_file; 201 loff_t old_pos = 0; 202 loff_t new_pos = 0; 203 loff_t cloned; 204 loff_t data_pos = -1; 205 loff_t hole_len; 206 bool skip_hole = false; 207 int error = 0; 208 209 if (len == 0) 210 return 0; 211 212 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); 213 if (IS_ERR(old_file)) 214 return PTR_ERR(old_file); 215 216 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); 217 if (IS_ERR(new_file)) { 218 error = PTR_ERR(new_file); 219 goto out_fput; 220 } 221 222 /* Try to use clone_file_range to clone up within the same fs */ 223 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0); 224 if (cloned == len) 225 goto out; 226 /* Couldn't clone, so now we try to copy the data */ 227 228 /* Check if lower fs supports seek operation */ 229 if (old_file->f_mode & FMODE_LSEEK && 230 old_file->f_op->llseek) 231 skip_hole = true; 232 233 while (len) { 234 size_t this_len = OVL_COPY_UP_CHUNK_SIZE; 235 long bytes; 236 237 if (len < this_len) 238 this_len = len; 239 240 if (signal_pending_state(TASK_KILLABLE, current)) { 241 error = -EINTR; 242 break; 243 } 244 245 /* 246 * Fill zero for hole will cost unnecessary disk space 247 * and meanwhile slow down the copy-up speed, so we do 248 * an optimization for hole during copy-up, it relies 249 * on SEEK_DATA implementation in lower fs so if lower 250 * fs does not support it, copy-up will behave as before. 251 * 252 * Detail logic of hole detection as below: 253 * When we detect next data position is larger than current 254 * position we will skip that hole, otherwise we copy 255 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually, 256 * it may not recognize all kind of holes and sometimes 257 * only skips partial of hole area. However, it will be 258 * enough for most of the use cases. 259 */ 260 261 if (skip_hole && data_pos < old_pos) { 262 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA); 263 if (data_pos > old_pos) { 264 hole_len = data_pos - old_pos; 265 len -= hole_len; 266 old_pos = new_pos = data_pos; 267 continue; 268 } else if (data_pos == -ENXIO) { 269 break; 270 } else if (data_pos < 0) { 271 skip_hole = false; 272 } 273 } 274 275 bytes = do_splice_direct(old_file, &old_pos, 276 new_file, &new_pos, 277 this_len, SPLICE_F_MOVE); 278 if (bytes <= 0) { 279 error = bytes; 280 break; 281 } 282 WARN_ON(old_pos != new_pos); 283 284 len -= bytes; 285 } 286 out: 287 if (!error && ovl_should_sync(ofs)) 288 error = vfs_fsync(new_file, 0); 289 fput(new_file); 290 out_fput: 291 fput(old_file); 292 return error; 293 } 294 295 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat) 296 { 297 struct iattr attr = { 298 .ia_valid = ATTR_SIZE, 299 .ia_size = stat->size, 300 }; 301 302 return notify_change(&init_user_ns, upperdentry, &attr, NULL); 303 } 304 305 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) 306 { 307 struct iattr attr = { 308 .ia_valid = 309 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, 310 .ia_atime = stat->atime, 311 .ia_mtime = stat->mtime, 312 }; 313 314 return notify_change(&init_user_ns, upperdentry, &attr, NULL); 315 } 316 317 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) 318 { 319 int err = 0; 320 321 if (!S_ISLNK(stat->mode)) { 322 struct iattr attr = { 323 .ia_valid = ATTR_MODE, 324 .ia_mode = stat->mode, 325 }; 326 err = notify_change(&init_user_ns, upperdentry, &attr, NULL); 327 } 328 if (!err) { 329 struct iattr attr = { 330 .ia_valid = ATTR_UID | ATTR_GID, 331 .ia_uid = stat->uid, 332 .ia_gid = stat->gid, 333 }; 334 err = notify_change(&init_user_ns, upperdentry, &attr, NULL); 335 } 336 if (!err) 337 ovl_set_timestamps(upperdentry, stat); 338 339 return err; 340 } 341 342 struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real, 343 bool is_upper) 344 { 345 struct ovl_fh *fh; 346 int fh_type, dwords; 347 int buflen = MAX_HANDLE_SZ; 348 uuid_t *uuid = &real->d_sb->s_uuid; 349 int err; 350 351 /* Make sure the real fid stays 32bit aligned */ 352 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4); 353 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255); 354 355 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL); 356 if (!fh) 357 return ERR_PTR(-ENOMEM); 358 359 /* 360 * We encode a non-connectable file handle for non-dir, because we 361 * only need to find the lower inode number and we don't want to pay 362 * the price or reconnecting the dentry. 363 */ 364 dwords = buflen >> 2; 365 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0); 366 buflen = (dwords << 2); 367 368 err = -EIO; 369 if (WARN_ON(fh_type < 0) || 370 WARN_ON(buflen > MAX_HANDLE_SZ) || 371 WARN_ON(fh_type == FILEID_INVALID)) 372 goto out_err; 373 374 fh->fb.version = OVL_FH_VERSION; 375 fh->fb.magic = OVL_FH_MAGIC; 376 fh->fb.type = fh_type; 377 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN; 378 /* 379 * When we will want to decode an overlay dentry from this handle 380 * and all layers are on the same fs, if we get a disconncted real 381 * dentry when we decode fid, the only way to tell if we should assign 382 * it to upperdentry or to lowerstack is by checking this flag. 383 */ 384 if (is_upper) 385 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER; 386 fh->fb.len = sizeof(fh->fb) + buflen; 387 if (ofs->config.uuid) 388 fh->fb.uuid = *uuid; 389 390 return fh; 391 392 out_err: 393 kfree(fh); 394 return ERR_PTR(err); 395 } 396 397 int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower, 398 struct dentry *upper) 399 { 400 const struct ovl_fh *fh = NULL; 401 int err; 402 403 /* 404 * When lower layer doesn't support export operations store a 'null' fh, 405 * so we can use the overlay.origin xattr to distignuish between a copy 406 * up and a pure upper inode. 407 */ 408 if (ovl_can_decode_fh(lower->d_sb)) { 409 fh = ovl_encode_real_fh(ofs, lower, false); 410 if (IS_ERR(fh)) 411 return PTR_ERR(fh); 412 } 413 414 /* 415 * Do not fail when upper doesn't support xattrs. 416 */ 417 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf, 418 fh ? fh->fb.len : 0, 0); 419 kfree(fh); 420 421 /* Ignore -EPERM from setting "user.*" on symlink/special */ 422 return err == -EPERM ? 0 : err; 423 } 424 425 /* Store file handle of @upper dir in @index dir entry */ 426 static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper, 427 struct dentry *index) 428 { 429 const struct ovl_fh *fh; 430 int err; 431 432 fh = ovl_encode_real_fh(ofs, upper, true); 433 if (IS_ERR(fh)) 434 return PTR_ERR(fh); 435 436 err = ovl_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len); 437 438 kfree(fh); 439 return err; 440 } 441 442 /* 443 * Create and install index entry. 444 * 445 * Caller must hold i_mutex on indexdir. 446 */ 447 static int ovl_create_index(struct dentry *dentry, struct dentry *origin, 448 struct dentry *upper) 449 { 450 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 451 struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 452 struct inode *dir = d_inode(indexdir); 453 struct dentry *index = NULL; 454 struct dentry *temp = NULL; 455 struct qstr name = { }; 456 int err; 457 458 /* 459 * For now this is only used for creating index entry for directories, 460 * because non-dir are copied up directly to index and then hardlinked 461 * to upper dir. 462 * 463 * TODO: implement create index for non-dir, so we can call it when 464 * encoding file handle for non-dir in case index does not exist. 465 */ 466 if (WARN_ON(!d_is_dir(dentry))) 467 return -EIO; 468 469 /* Directory not expected to be indexed before copy up */ 470 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) 471 return -EIO; 472 473 err = ovl_get_index_name(ofs, origin, &name); 474 if (err) 475 return err; 476 477 temp = ovl_create_temp(ofs, indexdir, OVL_CATTR(S_IFDIR | 0)); 478 err = PTR_ERR(temp); 479 if (IS_ERR(temp)) 480 goto free_name; 481 482 err = ovl_set_upper_fh(ofs, upper, temp); 483 if (err) 484 goto out; 485 486 index = lookup_one_len(name.name, indexdir, name.len); 487 if (IS_ERR(index)) { 488 err = PTR_ERR(index); 489 } else { 490 err = ovl_do_rename(ofs, dir, temp, dir, index, 0); 491 dput(index); 492 } 493 out: 494 if (err) 495 ovl_cleanup(ofs, dir, temp); 496 dput(temp); 497 free_name: 498 kfree(name.name); 499 return err; 500 } 501 502 struct ovl_copy_up_ctx { 503 struct dentry *parent; 504 struct dentry *dentry; 505 struct path lowerpath; 506 struct kstat stat; 507 struct kstat pstat; 508 const char *link; 509 struct dentry *destdir; 510 struct qstr destname; 511 struct dentry *workdir; 512 bool origin; 513 bool indexed; 514 bool metacopy; 515 }; 516 517 static int ovl_link_up(struct ovl_copy_up_ctx *c) 518 { 519 int err; 520 struct dentry *upper; 521 struct dentry *upperdir = ovl_dentry_upper(c->parent); 522 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 523 struct inode *udir = d_inode(upperdir); 524 525 /* Mark parent "impure" because it may now contain non-pure upper */ 526 err = ovl_set_impure(c->parent, upperdir); 527 if (err) 528 return err; 529 530 err = ovl_set_nlink_lower(c->dentry); 531 if (err) 532 return err; 533 534 inode_lock_nested(udir, I_MUTEX_PARENT); 535 upper = lookup_one_len(c->dentry->d_name.name, upperdir, 536 c->dentry->d_name.len); 537 err = PTR_ERR(upper); 538 if (!IS_ERR(upper)) { 539 err = ovl_do_link(ofs, ovl_dentry_upper(c->dentry), udir, upper); 540 dput(upper); 541 542 if (!err) { 543 /* Restore timestamps on parent (best effort) */ 544 ovl_set_timestamps(upperdir, &c->pstat); 545 ovl_dentry_set_upper_alias(c->dentry); 546 } 547 } 548 inode_unlock(udir); 549 if (err) 550 return err; 551 552 err = ovl_set_nlink_upper(c->dentry); 553 554 return err; 555 } 556 557 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp) 558 { 559 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 560 struct inode *inode = d_inode(c->dentry); 561 struct path upperpath, datapath; 562 int err; 563 564 ovl_path_upper(c->dentry, &upperpath); 565 if (WARN_ON(upperpath.dentry != NULL)) 566 return -EIO; 567 568 upperpath.dentry = temp; 569 570 /* 571 * Copy up data first and then xattrs. Writing data after 572 * xattrs will remove security.capability xattr automatically. 573 */ 574 if (S_ISREG(c->stat.mode) && !c->metacopy) { 575 ovl_path_lowerdata(c->dentry, &datapath); 576 err = ovl_copy_up_data(ofs, &datapath, &upperpath, 577 c->stat.size); 578 if (err) 579 return err; 580 } 581 582 err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp); 583 if (err) 584 return err; 585 586 if (inode->i_flags & OVL_COPY_I_FLAGS_MASK) { 587 /* 588 * Copy the fileattr inode flags that are the source of already 589 * copied i_flags 590 */ 591 err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath); 592 if (err) 593 return err; 594 } 595 596 /* 597 * Store identifier of lower inode in upper inode xattr to 598 * allow lookup of the copy up origin inode. 599 * 600 * Don't set origin when we are breaking the association with a lower 601 * hard link. 602 */ 603 if (c->origin) { 604 err = ovl_set_origin(ofs, c->lowerpath.dentry, temp); 605 if (err) 606 return err; 607 } 608 609 if (c->metacopy) { 610 err = ovl_check_setxattr(ofs, temp, OVL_XATTR_METACOPY, 611 NULL, 0, -EOPNOTSUPP); 612 if (err) 613 return err; 614 } 615 616 inode_lock(temp->d_inode); 617 if (S_ISREG(c->stat.mode)) 618 err = ovl_set_size(temp, &c->stat); 619 if (!err) 620 err = ovl_set_attr(temp, &c->stat); 621 inode_unlock(temp->d_inode); 622 623 return err; 624 } 625 626 struct ovl_cu_creds { 627 const struct cred *old; 628 struct cred *new; 629 }; 630 631 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc) 632 { 633 int err; 634 635 cc->old = cc->new = NULL; 636 err = security_inode_copy_up(dentry, &cc->new); 637 if (err < 0) 638 return err; 639 640 if (cc->new) 641 cc->old = override_creds(cc->new); 642 643 return 0; 644 } 645 646 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc) 647 { 648 if (cc->new) { 649 revert_creds(cc->old); 650 put_cred(cc->new); 651 } 652 } 653 654 /* 655 * Copyup using workdir to prepare temp file. Used when copying up directories, 656 * special files or when upper fs doesn't support O_TMPFILE. 657 */ 658 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) 659 { 660 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 661 struct inode *inode; 662 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir); 663 struct dentry *temp, *upper; 664 struct ovl_cu_creds cc; 665 int err; 666 struct ovl_cattr cattr = { 667 /* Can't properly set mode on creation because of the umask */ 668 .mode = c->stat.mode & S_IFMT, 669 .rdev = c->stat.rdev, 670 .link = c->link 671 }; 672 673 /* workdir and destdir could be the same when copying up to indexdir */ 674 err = -EIO; 675 if (lock_rename(c->workdir, c->destdir) != NULL) 676 goto unlock; 677 678 err = ovl_prep_cu_creds(c->dentry, &cc); 679 if (err) 680 goto unlock; 681 682 temp = ovl_create_temp(ofs, c->workdir, &cattr); 683 ovl_revert_cu_creds(&cc); 684 685 err = PTR_ERR(temp); 686 if (IS_ERR(temp)) 687 goto unlock; 688 689 err = ovl_copy_up_inode(c, temp); 690 if (err) 691 goto cleanup; 692 693 if (S_ISDIR(c->stat.mode) && c->indexed) { 694 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp); 695 if (err) 696 goto cleanup; 697 } 698 699 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); 700 err = PTR_ERR(upper); 701 if (IS_ERR(upper)) 702 goto cleanup; 703 704 err = ovl_do_rename(ofs, wdir, temp, udir, upper, 0); 705 dput(upper); 706 if (err) 707 goto cleanup; 708 709 if (!c->metacopy) 710 ovl_set_upperdata(d_inode(c->dentry)); 711 inode = d_inode(c->dentry); 712 ovl_inode_update(inode, temp); 713 if (S_ISDIR(inode->i_mode)) 714 ovl_set_flag(OVL_WHITEOUTS, inode); 715 unlock: 716 unlock_rename(c->workdir, c->destdir); 717 718 return err; 719 720 cleanup: 721 ovl_cleanup(ofs, wdir, temp); 722 dput(temp); 723 goto unlock; 724 } 725 726 /* Copyup using O_TMPFILE which does not require cross dir locking */ 727 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) 728 { 729 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 730 struct inode *udir = d_inode(c->destdir); 731 struct dentry *temp, *upper; 732 struct ovl_cu_creds cc; 733 int err; 734 735 err = ovl_prep_cu_creds(c->dentry, &cc); 736 if (err) 737 return err; 738 739 temp = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode); 740 ovl_revert_cu_creds(&cc); 741 742 if (IS_ERR(temp)) 743 return PTR_ERR(temp); 744 745 err = ovl_copy_up_inode(c, temp); 746 if (err) 747 goto out_dput; 748 749 inode_lock_nested(udir, I_MUTEX_PARENT); 750 751 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); 752 err = PTR_ERR(upper); 753 if (!IS_ERR(upper)) { 754 err = ovl_do_link(ofs, temp, udir, upper); 755 dput(upper); 756 } 757 inode_unlock(udir); 758 759 if (err) 760 goto out_dput; 761 762 if (!c->metacopy) 763 ovl_set_upperdata(d_inode(c->dentry)); 764 ovl_inode_update(d_inode(c->dentry), temp); 765 766 return 0; 767 768 out_dput: 769 dput(temp); 770 return err; 771 } 772 773 /* 774 * Copy up a single dentry 775 * 776 * All renames start with copy up of source if necessary. The actual 777 * rename will only proceed once the copy up was successful. Copy up uses 778 * upper parent i_mutex for exclusion. Since rename can change d_parent it 779 * is possible that the copy up will lock the old parent. At that point 780 * the file will have already been copied up anyway. 781 */ 782 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) 783 { 784 int err; 785 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 786 bool to_index = false; 787 788 /* 789 * Indexed non-dir is copied up directly to the index entry and then 790 * hardlinked to upper dir. Indexed dir is copied up to indexdir, 791 * then index entry is created and then copied up dir installed. 792 * Copying dir up to indexdir instead of workdir simplifies locking. 793 */ 794 if (ovl_need_index(c->dentry)) { 795 c->indexed = true; 796 if (S_ISDIR(c->stat.mode)) 797 c->workdir = ovl_indexdir(c->dentry->d_sb); 798 else 799 to_index = true; 800 } 801 802 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) 803 c->origin = true; 804 805 if (to_index) { 806 c->destdir = ovl_indexdir(c->dentry->d_sb); 807 err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname); 808 if (err) 809 return err; 810 } else if (WARN_ON(!c->parent)) { 811 /* Disconnected dentry must be copied up to index dir */ 812 return -EIO; 813 } else { 814 /* 815 * Mark parent "impure" because it may now contain non-pure 816 * upper 817 */ 818 err = ovl_set_impure(c->parent, c->destdir); 819 if (err) 820 return err; 821 } 822 823 /* Should we copyup with O_TMPFILE or with workdir? */ 824 if (S_ISREG(c->stat.mode) && ofs->tmpfile) 825 err = ovl_copy_up_tmpfile(c); 826 else 827 err = ovl_copy_up_workdir(c); 828 if (err) 829 goto out; 830 831 if (c->indexed) 832 ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); 833 834 if (to_index) { 835 /* Initialize nlink for copy up of disconnected dentry */ 836 err = ovl_set_nlink_upper(c->dentry); 837 } else { 838 struct inode *udir = d_inode(c->destdir); 839 840 /* Restore timestamps on parent (best effort) */ 841 inode_lock(udir); 842 ovl_set_timestamps(c->destdir, &c->pstat); 843 inode_unlock(udir); 844 845 ovl_dentry_set_upper_alias(c->dentry); 846 } 847 848 out: 849 if (to_index) 850 kfree(c->destname.name); 851 return err; 852 } 853 854 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode, 855 int flags) 856 { 857 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 858 859 if (!ofs->config.metacopy) 860 return false; 861 862 if (!S_ISREG(mode)) 863 return false; 864 865 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC))) 866 return false; 867 868 return true; 869 } 870 871 static ssize_t ovl_getxattr_value(struct ovl_fs *ofs, struct dentry *dentry, 872 char *name, char **value) 873 { 874 ssize_t res; 875 char *buf; 876 877 res = ovl_do_getxattr(ofs, dentry, name, NULL, 0); 878 if (res == -ENODATA || res == -EOPNOTSUPP) 879 res = 0; 880 881 if (res > 0) { 882 buf = kzalloc(res, GFP_KERNEL); 883 if (!buf) 884 return -ENOMEM; 885 886 res = ovl_do_getxattr(ofs, dentry, name, buf, res); 887 if (res < 0) 888 kfree(buf); 889 else 890 *value = buf; 891 } 892 return res; 893 } 894 895 /* Copy up data of an inode which was copied up metadata only in the past. */ 896 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) 897 { 898 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 899 struct path upperpath, datapath; 900 int err; 901 char *capability = NULL; 902 ssize_t cap_size; 903 904 ovl_path_upper(c->dentry, &upperpath); 905 if (WARN_ON(upperpath.dentry == NULL)) 906 return -EIO; 907 908 ovl_path_lowerdata(c->dentry, &datapath); 909 if (WARN_ON(datapath.dentry == NULL)) 910 return -EIO; 911 912 if (c->stat.size) { 913 err = cap_size = ovl_getxattr_value(ofs, upperpath.dentry, 914 XATTR_NAME_CAPS, &capability); 915 if (cap_size < 0) 916 goto out; 917 } 918 919 err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size); 920 if (err) 921 goto out_free; 922 923 /* 924 * Writing to upper file will clear security.capability xattr. We 925 * don't want that to happen for normal copy-up operation. 926 */ 927 if (capability) { 928 err = ovl_do_setxattr(ofs, upperpath.dentry, XATTR_NAME_CAPS, 929 capability, cap_size, 0); 930 if (err) 931 goto out_free; 932 } 933 934 935 err = ovl_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY); 936 if (err) 937 goto out_free; 938 939 ovl_set_upperdata(d_inode(c->dentry)); 940 out_free: 941 kfree(capability); 942 out: 943 return err; 944 } 945 946 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, 947 int flags) 948 { 949 int err; 950 DEFINE_DELAYED_CALL(done); 951 struct path parentpath; 952 struct ovl_copy_up_ctx ctx = { 953 .parent = parent, 954 .dentry = dentry, 955 .workdir = ovl_workdir(dentry), 956 }; 957 958 if (WARN_ON(!ctx.workdir)) 959 return -EROFS; 960 961 ovl_path_lower(dentry, &ctx.lowerpath); 962 err = vfs_getattr(&ctx.lowerpath, &ctx.stat, 963 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); 964 if (err) 965 return err; 966 967 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags); 968 969 if (parent) { 970 ovl_path_upper(parent, &parentpath); 971 ctx.destdir = parentpath.dentry; 972 ctx.destname = dentry->d_name; 973 974 err = vfs_getattr(&parentpath, &ctx.pstat, 975 STATX_ATIME | STATX_MTIME, 976 AT_STATX_SYNC_AS_STAT); 977 if (err) 978 return err; 979 } 980 981 /* maybe truncate regular file. this has no effect on dirs */ 982 if (flags & O_TRUNC) 983 ctx.stat.size = 0; 984 985 if (S_ISLNK(ctx.stat.mode)) { 986 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); 987 if (IS_ERR(ctx.link)) 988 return PTR_ERR(ctx.link); 989 } 990 991 err = ovl_copy_up_start(dentry, flags); 992 /* err < 0: interrupted, err > 0: raced with another copy-up */ 993 if (unlikely(err)) { 994 if (err > 0) 995 err = 0; 996 } else { 997 if (!ovl_dentry_upper(dentry)) 998 err = ovl_do_copy_up(&ctx); 999 if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) 1000 err = ovl_link_up(&ctx); 1001 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 1002 err = ovl_copy_up_meta_inode_data(&ctx); 1003 ovl_copy_up_end(dentry); 1004 } 1005 do_delayed_call(&done); 1006 1007 return err; 1008 } 1009 1010 static int ovl_copy_up_flags(struct dentry *dentry, int flags) 1011 { 1012 int err = 0; 1013 const struct cred *old_cred; 1014 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); 1015 1016 /* 1017 * With NFS export, copy up can get called for a disconnected non-dir. 1018 * In this case, we will copy up lower inode to index dir without 1019 * linking it to upper dir. 1020 */ 1021 if (WARN_ON(disconnected && d_is_dir(dentry))) 1022 return -EIO; 1023 1024 old_cred = ovl_override_creds(dentry->d_sb); 1025 while (!err) { 1026 struct dentry *next; 1027 struct dentry *parent = NULL; 1028 1029 if (ovl_already_copied_up(dentry, flags)) 1030 break; 1031 1032 next = dget(dentry); 1033 /* find the topmost dentry not yet copied up */ 1034 for (; !disconnected;) { 1035 parent = dget_parent(next); 1036 1037 if (ovl_dentry_upper(parent)) 1038 break; 1039 1040 dput(next); 1041 next = parent; 1042 } 1043 1044 err = ovl_copy_up_one(parent, next, flags); 1045 1046 dput(parent); 1047 dput(next); 1048 } 1049 revert_creds(old_cred); 1050 1051 return err; 1052 } 1053 1054 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) 1055 { 1056 /* Copy up of disconnected dentry does not set upper alias */ 1057 if (ovl_already_copied_up(dentry, flags)) 1058 return false; 1059 1060 if (special_file(d_inode(dentry)->i_mode)) 1061 return false; 1062 1063 if (!ovl_open_flags_need_copy_up(flags)) 1064 return false; 1065 1066 return true; 1067 } 1068 1069 int ovl_maybe_copy_up(struct dentry *dentry, int flags) 1070 { 1071 int err = 0; 1072 1073 if (ovl_open_need_copy_up(dentry, flags)) { 1074 err = ovl_want_write(dentry); 1075 if (!err) { 1076 err = ovl_copy_up_flags(dentry, flags); 1077 ovl_drop_write(dentry); 1078 } 1079 } 1080 1081 return err; 1082 } 1083 1084 int ovl_copy_up_with_data(struct dentry *dentry) 1085 { 1086 return ovl_copy_up_flags(dentry, O_WRONLY); 1087 } 1088 1089 int ovl_copy_up(struct dentry *dentry) 1090 { 1091 return ovl_copy_up_flags(dentry, 0); 1092 } 1093