1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (C) 2011 Novell Inc. 5 */ 6 7 #include <linux/fs.h> 8 #include <linux/slab.h> 9 #include <linux/cred.h> 10 #include <linux/xattr.h> 11 #include <linux/posix_acl.h> 12 #include <linux/ratelimit.h> 13 #include <linux/fiemap.h> 14 #include <linux/fileattr.h> 15 #include <linux/security.h> 16 #include "overlayfs.h" 17 18 19 int ovl_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 20 struct iattr *attr) 21 { 22 int err; 23 bool full_copy_up = false; 24 struct dentry *upperdentry; 25 const struct cred *old_cred; 26 27 err = setattr_prepare(&init_user_ns, dentry, attr); 28 if (err) 29 return err; 30 31 err = ovl_want_write(dentry); 32 if (err) 33 goto out; 34 35 if (attr->ia_valid & ATTR_SIZE) { 36 struct inode *realinode = d_inode(ovl_dentry_real(dentry)); 37 38 err = -ETXTBSY; 39 if (atomic_read(&realinode->i_writecount) < 0) 40 goto out_drop_write; 41 42 /* Truncate should trigger data copy up as well */ 43 full_copy_up = true; 44 } 45 46 if (!full_copy_up) 47 err = ovl_copy_up(dentry); 48 else 49 err = ovl_copy_up_with_data(dentry); 50 if (!err) { 51 struct inode *winode = NULL; 52 53 upperdentry = ovl_dentry_upper(dentry); 54 55 if (attr->ia_valid & ATTR_SIZE) { 56 winode = d_inode(upperdentry); 57 err = get_write_access(winode); 58 if (err) 59 goto out_drop_write; 60 } 61 62 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) 63 attr->ia_valid &= ~ATTR_MODE; 64 65 /* 66 * We might have to translate ovl file into real file object 67 * once use cases emerge. For now, simply don't let underlying 68 * filesystem rely on attr->ia_file 69 */ 70 attr->ia_valid &= ~ATTR_FILE; 71 72 /* 73 * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN 74 * set. Overlayfs does not pass O_TRUNC flag to underlying 75 * filesystem during open -> do not pass ATTR_OPEN. This 76 * disables optimization in fuse which assumes open(O_TRUNC) 77 * already set file size to 0. But we never passed O_TRUNC to 78 * fuse. So by clearing ATTR_OPEN, fuse will be forced to send 79 * setattr request to server. 80 */ 81 attr->ia_valid &= ~ATTR_OPEN; 82 83 inode_lock(upperdentry->d_inode); 84 old_cred = ovl_override_creds(dentry->d_sb); 85 err = notify_change(&init_user_ns, upperdentry, attr, NULL); 86 revert_creds(old_cred); 87 if (!err) 88 ovl_copyattr(upperdentry->d_inode, dentry->d_inode); 89 inode_unlock(upperdentry->d_inode); 90 91 if (winode) 92 put_write_access(winode); 93 } 94 out_drop_write: 95 ovl_drop_write(dentry); 96 out: 97 return err; 98 } 99 100 static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid) 101 { 102 bool samefs = ovl_same_fs(dentry->d_sb); 103 unsigned int xinobits = ovl_xino_bits(dentry->d_sb); 104 unsigned int xinoshift = 64 - xinobits; 105 106 if (samefs) { 107 /* 108 * When all layers are on the same fs, all real inode 109 * number are unique, so we use the overlay st_dev, 110 * which is friendly to du -x. 111 */ 112 stat->dev = dentry->d_sb->s_dev; 113 return 0; 114 } else if (xinobits) { 115 /* 116 * All inode numbers of underlying fs should not be using the 117 * high xinobits, so we use high xinobits to partition the 118 * overlay st_ino address space. The high bits holds the fsid 119 * (upper fsid is 0). The lowest xinobit is reserved for mapping 120 * the non-peresistent inode numbers range in case of overflow. 121 * This way all overlay inode numbers are unique and use the 122 * overlay st_dev. 123 */ 124 if (likely(!(stat->ino >> xinoshift))) { 125 stat->ino |= ((u64)fsid) << (xinoshift + 1); 126 stat->dev = dentry->d_sb->s_dev; 127 return 0; 128 } else if (ovl_xino_warn(dentry->d_sb)) { 129 pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n", 130 dentry, stat->ino, xinobits); 131 } 132 } 133 134 /* The inode could not be mapped to a unified st_ino address space */ 135 if (S_ISDIR(dentry->d_inode->i_mode)) { 136 /* 137 * Always use the overlay st_dev for directories, so 'find 138 * -xdev' will scan the entire overlay mount and won't cross the 139 * overlay mount boundaries. 140 * 141 * If not all layers are on the same fs the pair {real st_ino; 142 * overlay st_dev} is not unique, so use the non persistent 143 * overlay st_ino for directories. 144 */ 145 stat->dev = dentry->d_sb->s_dev; 146 stat->ino = dentry->d_inode->i_ino; 147 } else { 148 /* 149 * For non-samefs setup, if we cannot map all layers st_ino 150 * to a unified address space, we need to make sure that st_dev 151 * is unique per underlying fs, so we use the unique anonymous 152 * bdev assigned to the underlying fs. 153 */ 154 stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].pseudo_dev; 155 } 156 157 return 0; 158 } 159 160 int ovl_getattr(struct user_namespace *mnt_userns, const struct path *path, 161 struct kstat *stat, u32 request_mask, unsigned int flags) 162 { 163 struct dentry *dentry = path->dentry; 164 enum ovl_path_type type; 165 struct path realpath; 166 const struct cred *old_cred; 167 bool is_dir = S_ISDIR(dentry->d_inode->i_mode); 168 int fsid = 0; 169 int err; 170 bool metacopy_blocks = false; 171 172 metacopy_blocks = ovl_is_metacopy_dentry(dentry); 173 174 type = ovl_path_real(dentry, &realpath); 175 old_cred = ovl_override_creds(dentry->d_sb); 176 err = vfs_getattr(&realpath, stat, request_mask, flags); 177 if (err) 178 goto out; 179 180 /* 181 * For non-dir or same fs, we use st_ino of the copy up origin. 182 * This guaranties constant st_dev/st_ino across copy up. 183 * With xino feature and non-samefs, we use st_ino of the copy up 184 * origin masked with high bits that represent the layer id. 185 * 186 * If lower filesystem supports NFS file handles, this also guaranties 187 * persistent st_ino across mount cycle. 188 */ 189 if (!is_dir || ovl_same_dev(dentry->d_sb)) { 190 if (!OVL_TYPE_UPPER(type)) { 191 fsid = ovl_layer_lower(dentry)->fsid; 192 } else if (OVL_TYPE_ORIGIN(type)) { 193 struct kstat lowerstat; 194 u32 lowermask = STATX_INO | STATX_BLOCKS | 195 (!is_dir ? STATX_NLINK : 0); 196 197 ovl_path_lower(dentry, &realpath); 198 err = vfs_getattr(&realpath, &lowerstat, 199 lowermask, flags); 200 if (err) 201 goto out; 202 203 /* 204 * Lower hardlinks may be broken on copy up to different 205 * upper files, so we cannot use the lower origin st_ino 206 * for those different files, even for the same fs case. 207 * 208 * Similarly, several redirected dirs can point to the 209 * same dir on a lower layer. With the "verify_lower" 210 * feature, we do not use the lower origin st_ino, if 211 * we haven't verified that this redirect is unique. 212 * 213 * With inodes index enabled, it is safe to use st_ino 214 * of an indexed origin. The index validates that the 215 * upper hardlink is not broken and that a redirected 216 * dir is the only redirect to that origin. 217 */ 218 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) || 219 (!ovl_verify_lower(dentry->d_sb) && 220 (is_dir || lowerstat.nlink == 1))) { 221 fsid = ovl_layer_lower(dentry)->fsid; 222 stat->ino = lowerstat.ino; 223 } 224 225 /* 226 * If we are querying a metacopy dentry and lower 227 * dentry is data dentry, then use the blocks we 228 * queried just now. We don't have to do additional 229 * vfs_getattr(). If lower itself is metacopy, then 230 * additional vfs_getattr() is unavoidable. 231 */ 232 if (metacopy_blocks && 233 realpath.dentry == ovl_dentry_lowerdata(dentry)) { 234 stat->blocks = lowerstat.blocks; 235 metacopy_blocks = false; 236 } 237 } 238 239 if (metacopy_blocks) { 240 /* 241 * If lower is not same as lowerdata or if there was 242 * no origin on upper, we can end up here. 243 */ 244 struct kstat lowerdatastat; 245 u32 lowermask = STATX_BLOCKS; 246 247 ovl_path_lowerdata(dentry, &realpath); 248 err = vfs_getattr(&realpath, &lowerdatastat, 249 lowermask, flags); 250 if (err) 251 goto out; 252 stat->blocks = lowerdatastat.blocks; 253 } 254 } 255 256 err = ovl_map_dev_ino(dentry, stat, fsid); 257 if (err) 258 goto out; 259 260 /* 261 * It's probably not worth it to count subdirs to get the 262 * correct link count. nlink=1 seems to pacify 'find' and 263 * other utilities. 264 */ 265 if (is_dir && OVL_TYPE_MERGE(type)) 266 stat->nlink = 1; 267 268 /* 269 * Return the overlay inode nlinks for indexed upper inodes. 270 * Overlay inode nlink counts the union of the upper hardlinks 271 * and non-covered lower hardlinks. It does not include the upper 272 * index hardlink. 273 */ 274 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) 275 stat->nlink = dentry->d_inode->i_nlink; 276 277 out: 278 revert_creds(old_cred); 279 280 return err; 281 } 282 283 int ovl_permission(struct user_namespace *mnt_userns, 284 struct inode *inode, int mask) 285 { 286 struct inode *upperinode = ovl_inode_upper(inode); 287 struct inode *realinode = upperinode ?: ovl_inode_lower(inode); 288 const struct cred *old_cred; 289 int err; 290 291 /* Careful in RCU walk mode */ 292 if (!realinode) { 293 WARN_ON(!(mask & MAY_NOT_BLOCK)); 294 return -ECHILD; 295 } 296 297 /* 298 * Check overlay inode with the creds of task and underlying inode 299 * with creds of mounter 300 */ 301 err = generic_permission(&init_user_ns, inode, mask); 302 if (err) 303 return err; 304 305 old_cred = ovl_override_creds(inode->i_sb); 306 if (!upperinode && 307 !special_file(realinode->i_mode) && mask & MAY_WRITE) { 308 mask &= ~(MAY_WRITE | MAY_APPEND); 309 /* Make sure mounter can read file for copy up later */ 310 mask |= MAY_READ; 311 } 312 err = inode_permission(&init_user_ns, realinode, mask); 313 revert_creds(old_cred); 314 315 return err; 316 } 317 318 static const char *ovl_get_link(struct dentry *dentry, 319 struct inode *inode, 320 struct delayed_call *done) 321 { 322 const struct cred *old_cred; 323 const char *p; 324 325 if (!dentry) 326 return ERR_PTR(-ECHILD); 327 328 old_cred = ovl_override_creds(dentry->d_sb); 329 p = vfs_get_link(ovl_dentry_real(dentry), done); 330 revert_creds(old_cred); 331 return p; 332 } 333 334 bool ovl_is_private_xattr(struct super_block *sb, const char *name) 335 { 336 struct ovl_fs *ofs = sb->s_fs_info; 337 338 if (ofs->config.userxattr) 339 return strncmp(name, OVL_XATTR_USER_PREFIX, 340 sizeof(OVL_XATTR_USER_PREFIX) - 1) == 0; 341 else 342 return strncmp(name, OVL_XATTR_TRUSTED_PREFIX, 343 sizeof(OVL_XATTR_TRUSTED_PREFIX) - 1) == 0; 344 } 345 346 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, 347 const void *value, size_t size, int flags) 348 { 349 int err; 350 struct dentry *upperdentry = ovl_i_dentry_upper(inode); 351 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); 352 const struct cred *old_cred; 353 354 err = ovl_want_write(dentry); 355 if (err) 356 goto out; 357 358 if (!value && !upperdentry) { 359 old_cred = ovl_override_creds(dentry->d_sb); 360 err = vfs_getxattr(&init_user_ns, realdentry, name, NULL, 0); 361 revert_creds(old_cred); 362 if (err < 0) 363 goto out_drop_write; 364 } 365 366 if (!upperdentry) { 367 err = ovl_copy_up(dentry); 368 if (err) 369 goto out_drop_write; 370 371 realdentry = ovl_dentry_upper(dentry); 372 } 373 374 old_cred = ovl_override_creds(dentry->d_sb); 375 if (value) 376 err = vfs_setxattr(&init_user_ns, realdentry, name, value, size, 377 flags); 378 else { 379 WARN_ON(flags != XATTR_REPLACE); 380 err = vfs_removexattr(&init_user_ns, realdentry, name); 381 } 382 revert_creds(old_cred); 383 384 /* copy c/mtime */ 385 ovl_copyattr(d_inode(realdentry), inode); 386 387 out_drop_write: 388 ovl_drop_write(dentry); 389 out: 390 return err; 391 } 392 393 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, 394 void *value, size_t size) 395 { 396 ssize_t res; 397 const struct cred *old_cred; 398 struct dentry *realdentry = 399 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry); 400 401 old_cred = ovl_override_creds(dentry->d_sb); 402 res = vfs_getxattr(&init_user_ns, realdentry, name, value, size); 403 revert_creds(old_cred); 404 return res; 405 } 406 407 static bool ovl_can_list(struct super_block *sb, const char *s) 408 { 409 /* Never list private (.overlay) */ 410 if (ovl_is_private_xattr(sb, s)) 411 return false; 412 413 /* List all non-trusted xatts */ 414 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) 415 return true; 416 417 /* list other trusted for superuser only */ 418 return ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN); 419 } 420 421 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) 422 { 423 struct dentry *realdentry = ovl_dentry_real(dentry); 424 ssize_t res; 425 size_t len; 426 char *s; 427 const struct cred *old_cred; 428 429 old_cred = ovl_override_creds(dentry->d_sb); 430 res = vfs_listxattr(realdentry, list, size); 431 revert_creds(old_cred); 432 if (res <= 0 || size == 0) 433 return res; 434 435 /* filter out private xattrs */ 436 for (s = list, len = res; len;) { 437 size_t slen = strnlen(s, len) + 1; 438 439 /* underlying fs providing us with an broken xattr list? */ 440 if (WARN_ON(slen > len)) 441 return -EIO; 442 443 len -= slen; 444 if (!ovl_can_list(dentry->d_sb, s)) { 445 res -= slen; 446 memmove(s, s + slen, len); 447 } else { 448 s += slen; 449 } 450 } 451 452 return res; 453 } 454 455 struct posix_acl *ovl_get_acl(struct inode *inode, int type) 456 { 457 struct inode *realinode = ovl_inode_real(inode); 458 const struct cred *old_cred; 459 struct posix_acl *acl; 460 461 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) 462 return NULL; 463 464 old_cred = ovl_override_creds(inode->i_sb); 465 acl = get_acl(realinode, type); 466 revert_creds(old_cred); 467 468 return acl; 469 } 470 471 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags) 472 { 473 if (flags & S_ATIME) { 474 struct ovl_fs *ofs = inode->i_sb->s_fs_info; 475 struct path upperpath = { 476 .mnt = ovl_upper_mnt(ofs), 477 .dentry = ovl_upperdentry_dereference(OVL_I(inode)), 478 }; 479 480 if (upperpath.dentry) { 481 touch_atime(&upperpath); 482 inode->i_atime = d_inode(upperpath.dentry)->i_atime; 483 } 484 } 485 return 0; 486 } 487 488 static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 489 u64 start, u64 len) 490 { 491 int err; 492 struct inode *realinode = ovl_inode_realdata(inode); 493 const struct cred *old_cred; 494 495 if (!realinode->i_op->fiemap) 496 return -EOPNOTSUPP; 497 498 old_cred = ovl_override_creds(inode->i_sb); 499 err = realinode->i_op->fiemap(realinode, fieinfo, start, len); 500 revert_creds(old_cred); 501 502 return err; 503 } 504 505 /* 506 * Work around the fact that security_file_ioctl() takes a file argument. 507 * Introducing security_inode_fileattr_get/set() hooks would solve this issue 508 * properly. 509 */ 510 static int ovl_security_fileattr(struct dentry *dentry, struct fileattr *fa, 511 bool set) 512 { 513 struct path realpath; 514 struct file *file; 515 unsigned int cmd; 516 int err; 517 518 ovl_path_real(dentry, &realpath); 519 file = dentry_open(&realpath, O_RDONLY, current_cred()); 520 if (IS_ERR(file)) 521 return PTR_ERR(file); 522 523 if (set) 524 cmd = fa->fsx_valid ? FS_IOC_FSSETXATTR : FS_IOC_SETFLAGS; 525 else 526 cmd = fa->fsx_valid ? FS_IOC_FSGETXATTR : FS_IOC_GETFLAGS; 527 528 err = security_file_ioctl(file, cmd, 0); 529 fput(file); 530 531 return err; 532 } 533 534 int ovl_fileattr_set(struct user_namespace *mnt_userns, 535 struct dentry *dentry, struct fileattr *fa) 536 { 537 struct inode *inode = d_inode(dentry); 538 struct dentry *upperdentry; 539 const struct cred *old_cred; 540 int err; 541 542 err = ovl_want_write(dentry); 543 if (err) 544 goto out; 545 546 err = ovl_copy_up(dentry); 547 if (!err) { 548 upperdentry = ovl_dentry_upper(dentry); 549 550 old_cred = ovl_override_creds(inode->i_sb); 551 err = ovl_security_fileattr(dentry, fa, true); 552 if (!err) 553 err = vfs_fileattr_set(&init_user_ns, upperdentry, fa); 554 revert_creds(old_cred); 555 ovl_copyflags(ovl_inode_real(inode), inode); 556 } 557 ovl_drop_write(dentry); 558 out: 559 return err; 560 } 561 562 int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa) 563 { 564 struct inode *inode = d_inode(dentry); 565 struct dentry *realdentry = ovl_dentry_real(dentry); 566 const struct cred *old_cred; 567 int err; 568 569 old_cred = ovl_override_creds(inode->i_sb); 570 err = ovl_security_fileattr(dentry, fa, false); 571 if (!err) 572 err = vfs_fileattr_get(realdentry, fa); 573 revert_creds(old_cred); 574 575 return err; 576 } 577 578 static const struct inode_operations ovl_file_inode_operations = { 579 .setattr = ovl_setattr, 580 .permission = ovl_permission, 581 .getattr = ovl_getattr, 582 .listxattr = ovl_listxattr, 583 .get_acl = ovl_get_acl, 584 .update_time = ovl_update_time, 585 .fiemap = ovl_fiemap, 586 .fileattr_get = ovl_fileattr_get, 587 .fileattr_set = ovl_fileattr_set, 588 }; 589 590 static const struct inode_operations ovl_symlink_inode_operations = { 591 .setattr = ovl_setattr, 592 .get_link = ovl_get_link, 593 .getattr = ovl_getattr, 594 .listxattr = ovl_listxattr, 595 .update_time = ovl_update_time, 596 }; 597 598 static const struct inode_operations ovl_special_inode_operations = { 599 .setattr = ovl_setattr, 600 .permission = ovl_permission, 601 .getattr = ovl_getattr, 602 .listxattr = ovl_listxattr, 603 .get_acl = ovl_get_acl, 604 .update_time = ovl_update_time, 605 }; 606 607 static const struct address_space_operations ovl_aops = { 608 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */ 609 .direct_IO = noop_direct_IO, 610 }; 611 612 /* 613 * It is possible to stack overlayfs instance on top of another 614 * overlayfs instance as lower layer. We need to annotate the 615 * stackable i_mutex locks according to stack level of the super 616 * block instance. An overlayfs instance can never be in stack 617 * depth 0 (there is always a real fs below it). An overlayfs 618 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth]. 619 * 620 * For example, here is a snip from /proc/lockdep_chains after 621 * dir_iterate of nested overlayfs: 622 * 623 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) 624 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) 625 * [...] &type->i_mutex_dir_key (stack_depth=0) 626 * 627 * Locking order w.r.t ovl_want_write() is important for nested overlayfs. 628 * 629 * This chain is valid: 630 * - inode->i_rwsem (inode_lock[2]) 631 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) 632 * - OVL_I(inode)->lock (ovl_inode_lock[2]) 633 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) 634 * 635 * And this chain is valid: 636 * - inode->i_rwsem (inode_lock[2]) 637 * - OVL_I(inode)->lock (ovl_inode_lock[2]) 638 * - lowerinode->i_rwsem (inode_lock[1]) 639 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) 640 * 641 * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is 642 * held, because it is in reverse order of the non-nested case using the same 643 * upper fs: 644 * - inode->i_rwsem (inode_lock[1]) 645 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) 646 * - OVL_I(inode)->lock (ovl_inode_lock[1]) 647 */ 648 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH 649 650 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) 651 { 652 #ifdef CONFIG_LOCKDEP 653 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; 654 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; 655 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; 656 657 int depth = inode->i_sb->s_stack_depth - 1; 658 659 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) 660 depth = 0; 661 662 if (S_ISDIR(inode->i_mode)) 663 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); 664 else 665 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); 666 667 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); 668 #endif 669 } 670 671 static void ovl_next_ino(struct inode *inode) 672 { 673 struct ovl_fs *ofs = inode->i_sb->s_fs_info; 674 675 inode->i_ino = atomic_long_inc_return(&ofs->last_ino); 676 if (unlikely(!inode->i_ino)) 677 inode->i_ino = atomic_long_inc_return(&ofs->last_ino); 678 } 679 680 static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid) 681 { 682 int xinobits = ovl_xino_bits(inode->i_sb); 683 unsigned int xinoshift = 64 - xinobits; 684 685 /* 686 * When d_ino is consistent with st_ino (samefs or i_ino has enough 687 * bits to encode layer), set the same value used for st_ino to i_ino, 688 * so inode number exposed via /proc/locks and a like will be 689 * consistent with d_ino and st_ino values. An i_ino value inconsistent 690 * with d_ino also causes nfsd readdirplus to fail. 691 */ 692 inode->i_ino = ino; 693 if (ovl_same_fs(inode->i_sb)) { 694 return; 695 } else if (xinobits && likely(!(ino >> xinoshift))) { 696 inode->i_ino |= (unsigned long)fsid << (xinoshift + 1); 697 return; 698 } 699 700 /* 701 * For directory inodes on non-samefs with xino disabled or xino 702 * overflow, we allocate a non-persistent inode number, to be used for 703 * resolving st_ino collisions in ovl_map_dev_ino(). 704 * 705 * To avoid ino collision with legitimate xino values from upper 706 * layer (fsid 0), use the lowest xinobit to map the non 707 * persistent inode numbers to the unified st_ino address space. 708 */ 709 if (S_ISDIR(inode->i_mode)) { 710 ovl_next_ino(inode); 711 if (xinobits) { 712 inode->i_ino &= ~0UL >> xinobits; 713 inode->i_ino |= 1UL << xinoshift; 714 } 715 } 716 } 717 718 void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, 719 unsigned long ino, int fsid) 720 { 721 struct inode *realinode; 722 723 if (oip->upperdentry) 724 OVL_I(inode)->__upperdentry = oip->upperdentry; 725 if (oip->lowerpath && oip->lowerpath->dentry) 726 OVL_I(inode)->lower = igrab(d_inode(oip->lowerpath->dentry)); 727 if (oip->lowerdata) 728 OVL_I(inode)->lowerdata = igrab(d_inode(oip->lowerdata)); 729 730 realinode = ovl_inode_real(inode); 731 ovl_copyattr(realinode, inode); 732 ovl_copyflags(realinode, inode); 733 ovl_map_ino(inode, ino, fsid); 734 } 735 736 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) 737 { 738 inode->i_mode = mode; 739 inode->i_flags |= S_NOCMTIME; 740 #ifdef CONFIG_FS_POSIX_ACL 741 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; 742 #endif 743 744 ovl_lockdep_annotate_inode_mutex_key(inode); 745 746 switch (mode & S_IFMT) { 747 case S_IFREG: 748 inode->i_op = &ovl_file_inode_operations; 749 inode->i_fop = &ovl_file_operations; 750 inode->i_mapping->a_ops = &ovl_aops; 751 break; 752 753 case S_IFDIR: 754 inode->i_op = &ovl_dir_inode_operations; 755 inode->i_fop = &ovl_dir_operations; 756 break; 757 758 case S_IFLNK: 759 inode->i_op = &ovl_symlink_inode_operations; 760 break; 761 762 default: 763 inode->i_op = &ovl_special_inode_operations; 764 init_special_inode(inode, mode, rdev); 765 break; 766 } 767 } 768 769 /* 770 * With inodes index enabled, an overlay inode nlink counts the union of upper 771 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure 772 * upper inode, the following nlink modifying operations can happen: 773 * 774 * 1. Lower hardlink copy up 775 * 2. Upper hardlink created, unlinked or renamed over 776 * 3. Lower hardlink whiteout or renamed over 777 * 778 * For the first, copy up case, the union nlink does not change, whether the 779 * operation succeeds or fails, but the upper inode nlink may change. 780 * Therefore, before copy up, we store the union nlink value relative to the 781 * lower inode nlink in the index inode xattr .overlay.nlink. 782 * 783 * For the second, upper hardlink case, the union nlink should be incremented 784 * or decremented IFF the operation succeeds, aligned with nlink change of the 785 * upper inode. Therefore, before link/unlink/rename, we store the union nlink 786 * value relative to the upper inode nlink in the index inode. 787 * 788 * For the last, lower cover up case, we simplify things by preceding the 789 * whiteout or cover up with copy up. This makes sure that there is an index 790 * upper inode where the nlink xattr can be stored before the copied up upper 791 * entry is unlink. 792 */ 793 #define OVL_NLINK_ADD_UPPER (1 << 0) 794 795 /* 796 * On-disk format for indexed nlink: 797 * 798 * nlink relative to the upper inode - "U[+-]NUM" 799 * nlink relative to the lower inode - "L[+-]NUM" 800 */ 801 802 static int ovl_set_nlink_common(struct dentry *dentry, 803 struct dentry *realdentry, const char *format) 804 { 805 struct inode *inode = d_inode(dentry); 806 struct inode *realinode = d_inode(realdentry); 807 char buf[13]; 808 int len; 809 810 len = snprintf(buf, sizeof(buf), format, 811 (int) (inode->i_nlink - realinode->i_nlink)); 812 813 if (WARN_ON(len >= sizeof(buf))) 814 return -EIO; 815 816 return ovl_do_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry), 817 OVL_XATTR_NLINK, buf, len); 818 } 819 820 int ovl_set_nlink_upper(struct dentry *dentry) 821 { 822 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); 823 } 824 825 int ovl_set_nlink_lower(struct dentry *dentry) 826 { 827 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); 828 } 829 830 unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry, 831 struct dentry *upperdentry, 832 unsigned int fallback) 833 { 834 int nlink_diff; 835 int nlink; 836 char buf[13]; 837 int err; 838 839 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) 840 return fallback; 841 842 err = ovl_do_getxattr(ofs, upperdentry, OVL_XATTR_NLINK, 843 &buf, sizeof(buf) - 1); 844 if (err < 0) 845 goto fail; 846 847 buf[err] = '\0'; 848 if ((buf[0] != 'L' && buf[0] != 'U') || 849 (buf[1] != '+' && buf[1] != '-')) 850 goto fail; 851 852 err = kstrtoint(buf + 1, 10, &nlink_diff); 853 if (err < 0) 854 goto fail; 855 856 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; 857 nlink += nlink_diff; 858 859 if (nlink <= 0) 860 goto fail; 861 862 return nlink; 863 864 fail: 865 pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n", 866 upperdentry, err); 867 return fallback; 868 } 869 870 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) 871 { 872 struct inode *inode; 873 874 inode = new_inode(sb); 875 if (inode) 876 ovl_fill_inode(inode, mode, rdev); 877 878 return inode; 879 } 880 881 static int ovl_inode_test(struct inode *inode, void *data) 882 { 883 return inode->i_private == data; 884 } 885 886 static int ovl_inode_set(struct inode *inode, void *data) 887 { 888 inode->i_private = data; 889 return 0; 890 } 891 892 static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, 893 struct dentry *upperdentry, bool strict) 894 { 895 /* 896 * For directories, @strict verify from lookup path performs consistency 897 * checks, so NULL lower/upper in dentry must match NULL lower/upper in 898 * inode. Non @strict verify from NFS handle decode path passes NULL for 899 * 'unknown' lower/upper. 900 */ 901 if (S_ISDIR(inode->i_mode) && strict) { 902 /* Real lower dir moved to upper layer under us? */ 903 if (!lowerdentry && ovl_inode_lower(inode)) 904 return false; 905 906 /* Lookup of an uncovered redirect origin? */ 907 if (!upperdentry && ovl_inode_upper(inode)) 908 return false; 909 } 910 911 /* 912 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. 913 * This happens when finding a copied up overlay inode for a renamed 914 * or hardlinked overlay dentry and lower dentry cannot be followed 915 * by origin because lower fs does not support file handles. 916 */ 917 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) 918 return false; 919 920 /* 921 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. 922 * This happens when finding a lower alias for a copied up hard link. 923 */ 924 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) 925 return false; 926 927 return true; 928 } 929 930 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 931 bool is_upper) 932 { 933 struct inode *inode, *key = d_inode(real); 934 935 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 936 if (!inode) 937 return NULL; 938 939 if (!ovl_verify_inode(inode, is_upper ? NULL : real, 940 is_upper ? real : NULL, false)) { 941 iput(inode); 942 return ERR_PTR(-ESTALE); 943 } 944 945 return inode; 946 } 947 948 bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir) 949 { 950 struct inode *key = d_inode(dir); 951 struct inode *trap; 952 bool res; 953 954 trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 955 if (!trap) 956 return false; 957 958 res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) && 959 !ovl_inode_lower(trap); 960 961 iput(trap); 962 return res; 963 } 964 965 /* 966 * Create an inode cache entry for layer root dir, that will intentionally 967 * fail ovl_verify_inode(), so any lookup that will find some layer root 968 * will fail. 969 */ 970 struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir) 971 { 972 struct inode *key = d_inode(dir); 973 struct inode *trap; 974 975 if (!d_is_dir(dir)) 976 return ERR_PTR(-ENOTDIR); 977 978 trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test, 979 ovl_inode_set, key); 980 if (!trap) 981 return ERR_PTR(-ENOMEM); 982 983 if (!(trap->i_state & I_NEW)) { 984 /* Conflicting layer roots? */ 985 iput(trap); 986 return ERR_PTR(-ELOOP); 987 } 988 989 trap->i_mode = S_IFDIR; 990 trap->i_flags = S_DEAD; 991 unlock_new_inode(trap); 992 993 return trap; 994 } 995 996 /* 997 * Does overlay inode need to be hashed by lower inode? 998 */ 999 static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, 1000 struct dentry *lower, bool index) 1001 { 1002 struct ovl_fs *ofs = sb->s_fs_info; 1003 1004 /* No, if pure upper */ 1005 if (!lower) 1006 return false; 1007 1008 /* Yes, if already indexed */ 1009 if (index) 1010 return true; 1011 1012 /* Yes, if won't be copied up */ 1013 if (!ovl_upper_mnt(ofs)) 1014 return true; 1015 1016 /* No, if lower hardlink is or will be broken on copy up */ 1017 if ((upper || !ovl_indexdir(sb)) && 1018 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 1019 return false; 1020 1021 /* No, if non-indexed upper with NFS export */ 1022 if (sb->s_export_op && upper) 1023 return false; 1024 1025 /* Otherwise, hash by lower inode for fsnotify */ 1026 return true; 1027 } 1028 1029 static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode, 1030 struct inode *key) 1031 { 1032 return newinode ? inode_insert5(newinode, (unsigned long) key, 1033 ovl_inode_test, ovl_inode_set, key) : 1034 iget5_locked(sb, (unsigned long) key, 1035 ovl_inode_test, ovl_inode_set, key); 1036 } 1037 1038 struct inode *ovl_get_inode(struct super_block *sb, 1039 struct ovl_inode_params *oip) 1040 { 1041 struct ovl_fs *ofs = OVL_FS(sb); 1042 struct dentry *upperdentry = oip->upperdentry; 1043 struct ovl_path *lowerpath = oip->lowerpath; 1044 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; 1045 struct inode *inode; 1046 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL; 1047 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, 1048 oip->index); 1049 int fsid = bylower ? lowerpath->layer->fsid : 0; 1050 bool is_dir; 1051 unsigned long ino = 0; 1052 int err = oip->newinode ? -EEXIST : -ENOMEM; 1053 1054 if (!realinode) 1055 realinode = d_inode(lowerdentry); 1056 1057 /* 1058 * Copy up origin (lower) may exist for non-indexed upper, but we must 1059 * not use lower as hash key if this is a broken hardlink. 1060 */ 1061 is_dir = S_ISDIR(realinode->i_mode); 1062 if (upperdentry || bylower) { 1063 struct inode *key = d_inode(bylower ? lowerdentry : 1064 upperdentry); 1065 unsigned int nlink = is_dir ? 1 : realinode->i_nlink; 1066 1067 inode = ovl_iget5(sb, oip->newinode, key); 1068 if (!inode) 1069 goto out_err; 1070 if (!(inode->i_state & I_NEW)) { 1071 /* 1072 * Verify that the underlying files stored in the inode 1073 * match those in the dentry. 1074 */ 1075 if (!ovl_verify_inode(inode, lowerdentry, upperdentry, 1076 true)) { 1077 iput(inode); 1078 err = -ESTALE; 1079 goto out_err; 1080 } 1081 1082 dput(upperdentry); 1083 kfree(oip->redirect); 1084 goto out; 1085 } 1086 1087 /* Recalculate nlink for non-dir due to indexing */ 1088 if (!is_dir) 1089 nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry, 1090 nlink); 1091 set_nlink(inode, nlink); 1092 ino = key->i_ino; 1093 } else { 1094 /* Lower hardlink that will be broken on copy up */ 1095 inode = new_inode(sb); 1096 if (!inode) { 1097 err = -ENOMEM; 1098 goto out_err; 1099 } 1100 ino = realinode->i_ino; 1101 fsid = lowerpath->layer->fsid; 1102 } 1103 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); 1104 ovl_inode_init(inode, oip, ino, fsid); 1105 1106 if (upperdentry && ovl_is_impuredir(sb, upperdentry)) 1107 ovl_set_flag(OVL_IMPURE, inode); 1108 1109 if (oip->index) 1110 ovl_set_flag(OVL_INDEX, inode); 1111 1112 OVL_I(inode)->redirect = oip->redirect; 1113 1114 if (bylower) 1115 ovl_set_flag(OVL_CONST_INO, inode); 1116 1117 /* Check for non-merge dir that may have whiteouts */ 1118 if (is_dir) { 1119 if (((upperdentry && lowerdentry) || oip->numlower > 1) || 1120 ovl_check_origin_xattr(ofs, upperdentry ?: lowerdentry)) { 1121 ovl_set_flag(OVL_WHITEOUTS, inode); 1122 } 1123 } 1124 1125 if (inode->i_state & I_NEW) 1126 unlock_new_inode(inode); 1127 out: 1128 return inode; 1129 1130 out_err: 1131 pr_warn_ratelimited("failed to get inode (%i)\n", err); 1132 inode = ERR_PTR(err); 1133 goto out; 1134 } 1135