1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs_platform.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_mount.h" 13 #include "xfs_inode.h" 14 #include "xfs_acl.h" 15 #include "xfs_quota.h" 16 #include "xfs_da_format.h" 17 #include "xfs_da_btree.h" 18 #include "xfs_attr.h" 19 #include "xfs_trans.h" 20 #include "xfs_trans_space.h" 21 #include "xfs_bmap_btree.h" 22 #include "xfs_trace.h" 23 #include "xfs_icache.h" 24 #include "xfs_symlink.h" 25 #include "xfs_dir2.h" 26 #include "xfs_iomap.h" 27 #include "xfs_error.h" 28 #include "xfs_ioctl.h" 29 #include "xfs_xattr.h" 30 #include "xfs_file.h" 31 #include "xfs_bmap.h" 32 #include "xfs_zone_alloc.h" 33 34 #include <linux/posix_acl.h> 35 #include <linux/security.h> 36 #include <linux/iversion.h> 37 #include <linux/fiemap.h> 38 39 /* 40 * Directories have different lock order w.r.t. mmap_lock compared to regular 41 * files. This is due to readdir potentially triggering page faults on a user 42 * buffer inside filldir(), and this happens with the ilock on the directory 43 * held. For regular files, the lock order is the other way around - the 44 * mmap_lock is taken during the page fault, and then we lock the ilock to do 45 * block mapping. Hence we need a different class for the directory ilock so 46 * that lockdep can tell them apart. Directories in the metadata directory 47 * tree get a separate class so that lockdep reports will warn us if someone 48 * ever tries to lock regular directories after locking metadata directories. 49 */ 50 static struct lock_class_key xfs_nondir_ilock_class; 51 static struct lock_class_key xfs_dir_ilock_class; 52 53 static int 54 xfs_initxattrs( 55 struct inode *inode, 56 const struct xattr *xattr_array, 57 void *fs_info) 58 { 59 const struct xattr *xattr; 60 struct xfs_inode *ip = XFS_I(inode); 61 int error = 0; 62 63 for (xattr = xattr_array; xattr->name != NULL; xattr++) { 64 struct xfs_da_args args = { 65 .dp = ip, 66 .attr_filter = XFS_ATTR_SECURE, 67 .name = xattr->name, 68 .namelen = strlen(xattr->name), 69 .value = xattr->value, 70 .valuelen = xattr->value_len, 71 }; 72 error = xfs_attr_change(&args, XFS_ATTRUPDATE_UPSERT); 73 if (error < 0) 74 break; 75 } 76 return error; 77 } 78 79 /* 80 * Hook in SELinux. This is not quite correct yet, what we really need 81 * here (as we do for default ACLs) is a mechanism by which creation of 82 * these attrs can be journalled at inode creation time (along with the 83 * inode, of course, such that log replay can't cause these to be lost). 84 */ 85 int 86 xfs_inode_init_security( 87 struct inode *inode, 88 struct inode *dir, 89 const struct qstr *qstr) 90 { 91 return security_inode_init_security(inode, dir, qstr, 92 &xfs_initxattrs, NULL); 93 } 94 95 static void 96 xfs_dentry_to_name( 97 struct xfs_name *namep, 98 struct dentry *dentry) 99 { 100 namep->name = dentry->d_name.name; 101 namep->len = dentry->d_name.len; 102 namep->type = XFS_DIR3_FT_UNKNOWN; 103 } 104 105 static int 106 xfs_dentry_mode_to_name( 107 struct xfs_name *namep, 108 struct dentry *dentry, 109 int mode) 110 { 111 namep->name = dentry->d_name.name; 112 namep->len = dentry->d_name.len; 113 namep->type = xfs_mode_to_ftype(mode); 114 115 if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN)) 116 return -EFSCORRUPTED; 117 118 return 0; 119 } 120 121 STATIC void 122 xfs_cleanup_inode( 123 struct inode *dir, 124 struct inode *inode, 125 struct dentry *dentry) 126 { 127 struct xfs_name teardown; 128 129 /* Oh, the horror. 130 * If we can't add the ACL or we fail in 131 * xfs_inode_init_security we must back out. 132 * ENOSPC can hit here, among other things. 133 */ 134 xfs_dentry_to_name(&teardown, dentry); 135 136 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode)); 137 } 138 139 /* 140 * Check to see if we are likely to need an extended attribute to be added to 141 * the inode we are about to allocate. This allows the attribute fork to be 142 * created during the inode allocation, reducing the number of transactions we 143 * need to do in this fast path. 144 * 145 * The security checks are optimistic, but not guaranteed. The two LSMs that 146 * require xattrs to be added here (selinux and smack) are also the only two 147 * LSMs that add a sb->s_security structure to the superblock. Hence if security 148 * is enabled and sb->s_security is set, we have a pretty good idea that we are 149 * going to be asked to add a security xattr immediately after allocating the 150 * xfs inode and instantiating the VFS inode. 151 */ 152 static inline bool 153 xfs_create_need_xattr( 154 struct inode *dir, 155 struct posix_acl *default_acl, 156 struct posix_acl *acl) 157 { 158 if (acl) 159 return true; 160 if (default_acl) 161 return true; 162 #if IS_ENABLED(CONFIG_SECURITY) 163 if (dir->i_sb->s_security) 164 return true; 165 #endif 166 return false; 167 } 168 169 170 STATIC int 171 xfs_generic_create( 172 const struct mnt_idmap *idmap, 173 struct inode *dir, 174 struct dentry *dentry, 175 umode_t mode, 176 dev_t rdev, 177 struct file *tmpfile) /* unnamed file */ 178 { 179 struct xfs_icreate_args args = { 180 .idmap = idmap, 181 .pip = XFS_I(dir), 182 .rdev = rdev, 183 .mode = mode, 184 }; 185 struct inode *inode; 186 struct xfs_inode *ip = NULL; 187 struct posix_acl *default_acl, *acl; 188 struct xfs_name name; 189 int error; 190 191 /* 192 * Irix uses Missed'em'V split, but doesn't want to see 193 * the upper 5 bits of (14bit) major. 194 */ 195 if (S_ISCHR(args.mode) || S_ISBLK(args.mode)) { 196 if (unlikely(!sysv_valid_dev(args.rdev) || 197 MAJOR(args.rdev) & ~0x1ff)) 198 return -EINVAL; 199 } else { 200 args.rdev = 0; 201 } 202 203 error = posix_acl_create(dir, &args.mode, &default_acl, &acl); 204 if (error) 205 return error; 206 207 /* Verify mode is valid also for tmpfile case */ 208 error = xfs_dentry_mode_to_name(&name, dentry, args.mode); 209 if (unlikely(error)) 210 goto out_free_acl; 211 212 if (!tmpfile) { 213 if (xfs_create_need_xattr(dir, default_acl, acl)) 214 args.flags |= XFS_ICREATE_INIT_XATTRS; 215 216 error = xfs_create(&args, &name, &ip); 217 } else { 218 args.flags |= XFS_ICREATE_TMPFILE; 219 220 /* 221 * If this temporary file will not be linkable, don't bother 222 * creating an attr fork to receive a parent pointer. 223 */ 224 if (tmpfile->f_flags & O_EXCL) 225 args.flags |= XFS_ICREATE_UNLINKABLE; 226 227 error = xfs_create_tmpfile(&args, &ip); 228 } 229 if (unlikely(error)) 230 goto out_free_acl; 231 232 inode = VFS_I(ip); 233 234 error = xfs_inode_init_security(inode, dir, &dentry->d_name); 235 if (unlikely(error)) 236 goto out_cleanup_inode; 237 238 if (default_acl) { 239 error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); 240 if (error) 241 goto out_cleanup_inode; 242 } 243 if (acl) { 244 error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS); 245 if (error) 246 goto out_cleanup_inode; 247 } 248 249 xfs_setup_iops(ip); 250 251 if (tmpfile) { 252 /* 253 * The VFS requires that any inode fed to d_tmpfile must have 254 * nlink == 1 so that it can decrement the nlink in d_tmpfile. 255 * However, we created the temp file with nlink == 0 because 256 * we're not allowed to put an inode with nlink > 0 on the 257 * unlinked list. Therefore we have to set nlink to 1 so that 258 * d_tmpfile can immediately set it back to zero. 259 */ 260 set_nlink(inode, 1); 261 d_tmpfile(tmpfile, inode); 262 } else 263 d_instantiate(dentry, inode); 264 265 xfs_finish_inode_setup(ip); 266 267 out_free_acl: 268 posix_acl_release(default_acl); 269 posix_acl_release(acl); 270 return error; 271 272 out_cleanup_inode: 273 xfs_finish_inode_setup(ip); 274 if (!tmpfile) 275 xfs_cleanup_inode(dir, inode, dentry); 276 xfs_irele(ip); 277 goto out_free_acl; 278 } 279 280 STATIC int 281 xfs_vn_mknod( 282 const struct mnt_idmap *idmap, 283 struct inode *dir, 284 struct dentry *dentry, 285 umode_t mode, 286 dev_t rdev) 287 { 288 return xfs_generic_create(idmap, dir, dentry, mode, rdev, NULL); 289 } 290 291 STATIC int 292 xfs_vn_create( 293 const struct mnt_idmap *idmap, 294 struct inode *dir, 295 struct dentry *dentry, 296 umode_t mode) 297 { 298 return xfs_generic_create(idmap, dir, dentry, mode, 0, NULL); 299 } 300 301 STATIC struct dentry * 302 xfs_vn_mkdir( 303 const struct mnt_idmap *idmap, 304 struct inode *dir, 305 struct dentry *dentry, 306 umode_t mode) 307 { 308 return ERR_PTR(xfs_generic_create(idmap, dir, dentry, mode, 0, NULL)); 309 } 310 311 STATIC struct dentry * 312 xfs_vn_lookup( 313 struct inode *dir, 314 struct dentry *dentry, 315 unsigned int flags) 316 { 317 struct inode *inode; 318 struct xfs_inode *cip; 319 struct xfs_name name; 320 int error; 321 322 if (dentry->d_name.len >= MAXNAMELEN) 323 return ERR_PTR(-ENAMETOOLONG); 324 325 xfs_dentry_to_name(&name, dentry); 326 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL); 327 if (likely(!error)) 328 inode = VFS_I(cip); 329 else if (likely(error == -ENOENT)) 330 inode = NULL; 331 else 332 inode = ERR_PTR(error); 333 return d_splice_alias(inode, dentry); 334 } 335 336 STATIC struct dentry * 337 xfs_vn_ci_lookup( 338 struct inode *dir, 339 struct dentry *dentry, 340 unsigned int flags) 341 { 342 struct xfs_inode *ip; 343 struct xfs_name xname; 344 struct xfs_name ci_name; 345 struct qstr dname; 346 int error; 347 348 if (dentry->d_name.len >= MAXNAMELEN) 349 return ERR_PTR(-ENAMETOOLONG); 350 351 xfs_dentry_to_name(&xname, dentry); 352 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name); 353 if (unlikely(error)) { 354 if (unlikely(error != -ENOENT)) 355 return ERR_PTR(error); 356 /* 357 * call d_add(dentry, NULL) here when d_drop_negative_children 358 * is called in xfs_vn_mknod (ie. allow negative dentries 359 * with CI filesystems). 360 */ 361 return NULL; 362 } 363 364 /* if exact match, just splice and exit */ 365 if (!ci_name.name) 366 return d_splice_alias(VFS_I(ip), dentry); 367 368 /* else case-insensitive match... */ 369 dname.name = ci_name.name; 370 dname.len = ci_name.len; 371 dentry = d_add_ci(dentry, VFS_I(ip), &dname); 372 kfree(ci_name.name); 373 return dentry; 374 } 375 376 STATIC int 377 xfs_vn_link( 378 struct dentry *old_dentry, 379 struct inode *dir, 380 struct dentry *dentry) 381 { 382 struct inode *inode = d_inode(old_dentry); 383 struct xfs_name name; 384 int error; 385 386 error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode); 387 if (unlikely(error)) 388 return error; 389 390 if (IS_PRIVATE(inode)) 391 return -EPERM; 392 393 error = xfs_link(XFS_I(dir), XFS_I(inode), &name); 394 if (unlikely(error)) 395 return error; 396 397 ihold(inode); 398 d_instantiate(dentry, inode); 399 return 0; 400 } 401 402 STATIC int 403 xfs_vn_unlink( 404 struct inode *dir, 405 struct dentry *dentry) 406 { 407 struct xfs_name name; 408 int error; 409 410 xfs_dentry_to_name(&name, dentry); 411 412 error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry))); 413 if (error) 414 return error; 415 416 /* 417 * With unlink, the VFS makes the dentry "negative": no inode, 418 * but still hashed. This is incompatible with case-insensitive 419 * mode, so invalidate (unhash) the dentry in CI-mode. 420 */ 421 if (xfs_has_asciici(XFS_M(dir->i_sb))) 422 d_invalidate(dentry); 423 return 0; 424 } 425 426 STATIC int 427 xfs_vn_symlink( 428 const struct mnt_idmap *idmap, 429 struct inode *dir, 430 struct dentry *dentry, 431 const char *symname) 432 { 433 struct inode *inode; 434 struct xfs_inode *cip = NULL; 435 struct xfs_name name; 436 int error; 437 umode_t mode = S_IFLNK | S_IRWXUGO; 438 439 error = xfs_dentry_mode_to_name(&name, dentry, mode); 440 if (unlikely(error)) 441 goto out; 442 443 error = xfs_symlink(idmap, XFS_I(dir), &name, symname, mode, &cip); 444 if (unlikely(error)) 445 goto out; 446 447 inode = VFS_I(cip); 448 449 error = xfs_inode_init_security(inode, dir, &dentry->d_name); 450 if (unlikely(error)) 451 goto out_cleanup_inode; 452 453 xfs_setup_iops(cip); 454 455 d_instantiate(dentry, inode); 456 xfs_finish_inode_setup(cip); 457 return 0; 458 459 out_cleanup_inode: 460 xfs_finish_inode_setup(cip); 461 xfs_cleanup_inode(dir, inode, dentry); 462 xfs_irele(cip); 463 out: 464 return error; 465 } 466 467 STATIC int 468 xfs_vn_rename( 469 const struct mnt_idmap *idmap, 470 struct inode *odir, 471 struct dentry *odentry, 472 struct inode *ndir, 473 struct dentry *ndentry, 474 unsigned int flags) 475 { 476 struct inode *new_inode = d_inode(ndentry); 477 int omode = 0; 478 int error; 479 struct xfs_name oname; 480 struct xfs_name nname; 481 482 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 483 return -EINVAL; 484 485 /* if we are exchanging files, we need to set i_mode of both files */ 486 if (flags & RENAME_EXCHANGE) 487 omode = d_inode(ndentry)->i_mode; 488 489 error = xfs_dentry_mode_to_name(&oname, odentry, omode); 490 if (omode && unlikely(error)) 491 return error; 492 493 error = xfs_dentry_mode_to_name(&nname, ndentry, 494 d_inode(odentry)->i_mode); 495 if (unlikely(error)) 496 return error; 497 498 return xfs_rename(idmap, XFS_I(odir), &oname, 499 XFS_I(d_inode(odentry)), XFS_I(ndir), &nname, 500 new_inode ? XFS_I(new_inode) : NULL, flags); 501 } 502 503 /* 504 * careful here - this function can get called recursively, so 505 * we need to be very careful about how much stack we use. 506 * uio is kmalloced for this reason... 507 */ 508 STATIC const char * 509 xfs_vn_get_link( 510 struct dentry *dentry, 511 struct inode *inode, 512 struct delayed_call *done) 513 { 514 char *link; 515 int error = -ENOMEM; 516 517 if (!dentry) 518 return ERR_PTR(-ECHILD); 519 520 link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL); 521 if (!link) 522 goto out_err; 523 524 error = xfs_readlink(XFS_I(d_inode(dentry)), link); 525 if (unlikely(error)) 526 goto out_kfree; 527 528 set_delayed_call(done, kfree_link, link); 529 return link; 530 531 out_kfree: 532 kfree(link); 533 out_err: 534 return ERR_PTR(error); 535 } 536 537 static uint32_t 538 xfs_stat_blksize( 539 struct xfs_inode *ip) 540 { 541 struct xfs_mount *mp = ip->i_mount; 542 543 /* 544 * If the file blocks are being allocated from a realtime volume, then 545 * always return the realtime extent size. 546 */ 547 if (XFS_IS_REALTIME_INODE(ip)) 548 return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip) ? : 1); 549 550 /* 551 * Allow large block sizes to be reported to userspace programs if the 552 * "largeio" mount option is used. 553 * 554 * If compatibility mode is specified, simply return the basic unit of 555 * caching so that we don't get inefficient read/modify/write I/O from 556 * user apps. Otherwise.... 557 * 558 * If the underlying volume is a stripe, then return the stripe width in 559 * bytes as the recommended I/O size. It is not a stripe and we've set a 560 * default buffered I/O size, return that, otherwise return the compat 561 * default. 562 */ 563 if (xfs_has_large_iosize(mp)) { 564 if (mp->m_swidth) 565 return XFS_FSB_TO_B(mp, mp->m_swidth); 566 if (xfs_has_allocsize(mp)) 567 return 1U << mp->m_allocsize_log; 568 } 569 570 return max_t(uint32_t, PAGE_SIZE, mp->m_sb.sb_blocksize); 571 } 572 573 static void 574 xfs_report_dioalign( 575 struct xfs_inode *ip, 576 struct kstat *stat) 577 { 578 struct xfs_buftarg *target = xfs_inode_buftarg(ip); 579 struct block_device *bdev = target->bt_bdev; 580 581 stat->result_mask |= STATX_DIOALIGN | STATX_DIO_READ_ALIGN; 582 stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; 583 584 /* 585 * For COW inodes, we can only perform out of place writes of entire 586 * allocation units (blocks or RT extents). 587 * For writes smaller than the allocation unit, we must fall back to 588 * buffered I/O to perform read-modify-write cycles. At best this is 589 * highly inefficient; at worst it leads to page cache invalidation 590 * races. Tell applications to avoid this by reporting the larger write 591 * alignment in dio_offset_align, and the smaller read alignment in 592 * dio_read_offset_align. 593 */ 594 stat->dio_read_offset_align = bdev_logical_block_size(bdev); 595 if (xfs_is_cow_inode(ip)) 596 stat->dio_offset_align = xfs_inode_alloc_unitsize(ip); 597 else 598 stat->dio_offset_align = stat->dio_read_offset_align; 599 } 600 601 unsigned int 602 xfs_get_atomic_write_min( 603 struct xfs_inode *ip) 604 { 605 struct xfs_mount *mp = ip->i_mount; 606 607 /* 608 * If we can complete an atomic write via atomic out of place writes, 609 * then advertise a minimum size of one fsblock. Without this 610 * mechanism, we can only guarantee atomic writes up to a single LBA. 611 * 612 * If out of place writes are not available, we can guarantee an atomic 613 * write of exactly one single fsblock if the bdev will make that 614 * guarantee for us. 615 */ 616 if (xfs_inode_can_hw_atomic_write(ip) || 617 xfs_inode_can_sw_atomic_write(ip)) 618 return mp->m_sb.sb_blocksize; 619 620 return 0; 621 } 622 623 unsigned int 624 xfs_get_atomic_write_max( 625 struct xfs_inode *ip) 626 { 627 struct xfs_mount *mp = ip->i_mount; 628 629 /* 630 * If out of place writes are not available, we can guarantee an atomic 631 * write of exactly one single fsblock if the bdev will make that 632 * guarantee for us. 633 */ 634 if (!xfs_inode_can_sw_atomic_write(ip)) { 635 if (xfs_inode_can_hw_atomic_write(ip)) 636 return mp->m_sb.sb_blocksize; 637 return 0; 638 } 639 640 /* 641 * If we can complete an atomic write via atomic out of place writes, 642 * then advertise a maximum size of whatever we can complete through 643 * that means. Hardware support is reported via max_opt, not here. 644 */ 645 return XFS_FSB_TO_B(mp, mp->m_groups[xfs_inode_group_type(ip)].awu_max); 646 } 647 648 static inline xfs_fsize_t xfs_get_atomic_hw_write_align( 649 const struct xfs_inode *ip) 650 { 651 struct xfs_mount *mp = ip->i_mount; 652 653 return XFS_FSB_TO_B( 654 mp, xfs_calc_group_awu_align_max(mp, xfs_inode_group_type(ip))); 655 } 656 657 unsigned int 658 xfs_get_atomic_write_max_opt( 659 struct xfs_inode *ip) 660 { 661 struct xfs_mount *mp = ip->i_mount; 662 unsigned int max_opt; 663 const unsigned int sw_awu_max = xfs_get_atomic_write_max(ip); 664 const unsigned int hw_awu_max = xfs_inode_buftarg(ip)->bt_awu_max; 665 const xfs_fsize_t hw_align_max = xfs_get_atomic_hw_write_align(ip); 666 667 /* if the max is 1x block, then just keep behaviour that opt is 0 */ 668 if (sw_awu_max <= mp->m_sb.sb_blocksize) 669 return 0; 670 671 /* 672 * Advertise the maximum size of an atomic write that we can tell the 673 * block device to perform for us. In general the bdev limit will be 674 * less than our out of place write limit, but we don't want to exceed 675 * the awu_max. 676 */ 677 max_opt = min(sw_awu_max, hw_awu_max); 678 679 /* 680 * HW atomic writes also have to be naturally aligned on disk, so we 681 * cannot promise more than the largest extent that the allocator is 682 * able to align within a group. 683 */ 684 return min_t(xfs_fsize_t, max_opt, hw_align_max); 685 } 686 687 static void 688 xfs_report_atomic_write( 689 struct xfs_inode *ip, 690 struct kstat *stat) 691 { 692 generic_fill_statx_atomic_writes(stat, 693 xfs_get_atomic_write_min(ip), 694 xfs_get_atomic_write_max(ip), 695 xfs_get_atomic_write_max_opt(ip)); 696 } 697 698 STATIC int 699 xfs_vn_getattr( 700 const struct mnt_idmap *idmap, 701 const struct path *path, 702 struct kstat *stat, 703 u32 request_mask, 704 unsigned int query_flags) 705 { 706 struct inode *inode = d_inode(path->dentry); 707 struct xfs_inode *ip = XFS_I(inode); 708 struct xfs_mount *mp = ip->i_mount; 709 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 710 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 711 712 trace_xfs_getattr(ip); 713 714 if (xfs_is_shutdown(mp)) 715 return -EIO; 716 717 stat->size = XFS_ISIZE(ip); 718 stat->dev = inode->i_sb->s_dev; 719 stat->mode = inode->i_mode; 720 stat->nlink = inode->i_nlink; 721 stat->uid = vfsuid_into_kuid(vfsuid); 722 stat->gid = vfsgid_into_kgid(vfsgid); 723 stat->ino = inode->i_ino; 724 stat->atime = inode_get_atime(inode); 725 726 fill_mg_cmtime(stat, request_mask, inode); 727 728 stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks); 729 730 if (xfs_has_v3inodes(mp)) { 731 if (request_mask & STATX_BTIME) { 732 stat->result_mask |= STATX_BTIME; 733 stat->btime = ip->i_crtime; 734 } 735 } 736 737 /* 738 * Note: If you add another clause to set an attribute flag, please 739 * update attributes_mask below. 740 */ 741 if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE) 742 stat->attributes |= STATX_ATTR_IMMUTABLE; 743 if (ip->i_diflags & XFS_DIFLAG_APPEND) 744 stat->attributes |= STATX_ATTR_APPEND; 745 if (ip->i_diflags & XFS_DIFLAG_NODUMP) 746 stat->attributes |= STATX_ATTR_NODUMP; 747 748 stat->attributes_mask |= (STATX_ATTR_IMMUTABLE | 749 STATX_ATTR_APPEND | 750 STATX_ATTR_NODUMP); 751 752 switch (inode->i_mode & S_IFMT) { 753 case S_IFBLK: 754 case S_IFCHR: 755 stat->blksize = BLKDEV_IOSIZE; 756 stat->rdev = inode->i_rdev; 757 break; 758 case S_IFREG: 759 if (request_mask & (STATX_DIOALIGN | STATX_DIO_READ_ALIGN)) 760 xfs_report_dioalign(ip, stat); 761 if (request_mask & STATX_WRITE_ATOMIC) 762 xfs_report_atomic_write(ip, stat); 763 fallthrough; 764 default: 765 stat->blksize = xfs_stat_blksize(ip); 766 stat->rdev = 0; 767 break; 768 } 769 770 return 0; 771 } 772 773 static int 774 xfs_vn_change_ok( 775 const struct mnt_idmap *idmap, 776 struct dentry *dentry, 777 struct iattr *iattr) 778 { 779 struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount; 780 781 if (xfs_is_readonly(mp)) 782 return -EROFS; 783 784 if (xfs_is_shutdown(mp)) 785 return -EIO; 786 787 return setattr_prepare(idmap, dentry, iattr); 788 } 789 790 /* 791 * Set non-size attributes of an inode. 792 * 793 * Caution: The caller of this function is responsible for calling 794 * setattr_prepare() or otherwise verifying the change is fine. 795 */ 796 static int 797 xfs_setattr_nonsize( 798 const struct mnt_idmap *idmap, 799 struct dentry *dentry, 800 struct xfs_inode *ip, 801 struct iattr *iattr) 802 { 803 xfs_mount_t *mp = ip->i_mount; 804 struct inode *inode = VFS_I(ip); 805 int mask = iattr->ia_valid; 806 xfs_trans_t *tp; 807 int error; 808 kuid_t uid = GLOBAL_ROOT_UID; 809 kgid_t gid = GLOBAL_ROOT_GID; 810 struct xfs_dquot *udqp = NULL, *gdqp = NULL; 811 struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL; 812 813 ASSERT((mask & ATTR_SIZE) == 0); 814 815 /* 816 * If disk quotas is on, we make sure that the dquots do exist on disk, 817 * before we start any other transactions. Trying to do this later 818 * is messy. We don't care to take a readlock to look at the ids 819 * in inode here, because we can't hold it across the trans_reserve. 820 * If the IDs do change before we take the ilock, we're covered 821 * because the i_*dquot fields will get updated anyway. 822 */ 823 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) { 824 uint qflags = 0; 825 826 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) { 827 uid = from_vfsuid(idmap, i_user_ns(inode), 828 iattr->ia_vfsuid); 829 qflags |= XFS_QMOPT_UQUOTA; 830 } else { 831 uid = inode->i_uid; 832 } 833 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) { 834 gid = from_vfsgid(idmap, i_user_ns(inode), 835 iattr->ia_vfsgid); 836 qflags |= XFS_QMOPT_GQUOTA; 837 } else { 838 gid = inode->i_gid; 839 } 840 841 /* 842 * We take a reference when we initialize udqp and gdqp, 843 * so it is important that we never blindly double trip on 844 * the same variable. See xfs_create() for an example. 845 */ 846 ASSERT(udqp == NULL); 847 ASSERT(gdqp == NULL); 848 error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid, 849 qflags, &udqp, &gdqp, NULL); 850 if (error) 851 return error; 852 } 853 854 error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL, 855 capable_noaudit(CAP_FOWNER), &tp); 856 if (error) 857 goto out_dqrele; 858 859 /* 860 * Register quota modifications in the transaction. Must be the owner 861 * or privileged. These IDs could have changed since we last looked at 862 * them. But, we're assured that if the ownership did change while we 863 * didn't have the inode locked, inode's dquot(s) would have changed 864 * also. 865 */ 866 if (XFS_IS_UQUOTA_ON(mp) && 867 i_uid_needs_update(idmap, iattr, inode)) { 868 ASSERT(udqp); 869 old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp); 870 } 871 if (XFS_IS_GQUOTA_ON(mp) && 872 i_gid_needs_update(idmap, iattr, inode)) { 873 ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp)); 874 ASSERT(gdqp); 875 old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp); 876 } 877 878 setattr_copy(idmap, inode, iattr); 879 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 880 881 XFS_STATS_INC(mp, xs_ig_attrchg); 882 883 if (xfs_has_wsync(mp)) 884 xfs_trans_set_sync(tp); 885 error = xfs_trans_commit(tp); 886 887 /* 888 * Release any dquot(s) the inode had kept before chown. 889 */ 890 xfs_qm_dqrele(old_udqp); 891 xfs_qm_dqrele(old_gdqp); 892 xfs_qm_dqrele(udqp); 893 xfs_qm_dqrele(gdqp); 894 895 if (error) 896 return error; 897 898 /* 899 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode 900 * update. We could avoid this with linked transactions 901 * and passing down the transaction pointer all the way 902 * to attr_set. No previous user of the generic 903 * Posix ACL code seems to care about this issue either. 904 */ 905 if (mask & ATTR_MODE) { 906 error = posix_acl_chmod(idmap, dentry, inode->i_mode); 907 if (error) 908 return error; 909 } 910 911 return 0; 912 913 out_dqrele: 914 xfs_qm_dqrele(udqp); 915 xfs_qm_dqrele(gdqp); 916 return error; 917 } 918 919 /* 920 * Truncate file. Must have write permission and not be a directory. 921 */ 922 int 923 xfs_vn_setattr_size( 924 const struct mnt_idmap *idmap, 925 struct dentry *dentry, 926 struct iattr *iattr) 927 { 928 struct inode *inode = d_inode(dentry); 929 struct xfs_inode *ip = XFS_I(inode); 930 struct xfs_mount *mp = ip->i_mount; 931 xfs_off_t oldsize = inode->i_size; 932 xfs_off_t newsize = iattr->ia_size; 933 struct xfs_trans *tp; 934 int error; 935 uint lock_flags = 0; 936 uint resblks = 0; 937 bool did_zeroing = false; 938 struct xfs_zone_alloc_ctx ac = { }; 939 940 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL); 941 ASSERT(S_ISREG(inode->i_mode)); 942 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET| 943 ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0); 944 945 trace_xfs_setattr(ip); 946 947 error = xfs_vn_change_ok(idmap, dentry, iattr); 948 if (error) 949 return error; 950 951 /* 952 * Short circuit the truncate case for zero length files. 953 */ 954 if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) { 955 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME))) 956 return 0; 957 958 /* 959 * Use the regular setattr path to update the timestamps. 960 */ 961 iattr->ia_valid &= ~ATTR_SIZE; 962 return xfs_setattr_nonsize(idmap, dentry, ip, iattr); 963 } 964 965 /* 966 * Make sure that the dquots are attached to the inode. 967 */ 968 error = xfs_qm_dqattach(ip); 969 if (error) 970 return error; 971 972 /* 973 * Wait for all direct I/O to complete. 974 */ 975 inode_dio_wait(inode); 976 977 /* 978 * Normally xfs_zoned_space_reserve is supposed to be called outside the 979 * IOLOCK. For truncate we can't do that since ->setattr is called with 980 * it already held by the VFS. So for now chicken out and try to 981 * allocate space under it. 982 * 983 * To avoid deadlocks this means we can't block waiting for space, which 984 * can lead to spurious -ENOSPC if there are no directly available 985 * blocks. We mitigate this a bit by allowing zeroing to dip into the 986 * reserved pool, but eventually the VFS calling convention needs to 987 * change. 988 */ 989 if (xfs_is_zoned_inode(ip)) { 990 error = xfs_zoned_space_reserve(mp, 1, 991 XFS_ZR_NOWAIT | XFS_ZR_RESERVED, &ac); 992 if (error) { 993 if (error == -EAGAIN) 994 return -ENOSPC; 995 return error; 996 } 997 } 998 999 /* 1000 * File data changes must be complete before we start the transaction to 1001 * modify the inode. This needs to be done before joining the inode to 1002 * the transaction because the inode cannot be unlocked once it is a 1003 * part of the transaction. 1004 * 1005 * Start with zeroing any data beyond EOF that we may expose on file 1006 * extension, or zeroing out the rest of the block on a downward 1007 * truncate. 1008 */ 1009 if (newsize > oldsize) { 1010 trace_xfs_zero_eof(ip, oldsize, newsize - oldsize); 1011 error = xfs_zero_range(ip, oldsize, newsize - oldsize, 1012 &ac, &did_zeroing); 1013 } else { 1014 error = xfs_truncate_page(ip, newsize, &ac, &did_zeroing); 1015 } 1016 1017 if (xfs_is_zoned_inode(ip)) 1018 xfs_zoned_space_unreserve(mp, &ac); 1019 1020 if (error) 1021 return error; 1022 1023 /* 1024 * We've already locked out new page faults, so now we can safely remove 1025 * pages from the page cache knowing they won't get refaulted until we 1026 * drop the XFS_MMAP_EXCL lock after the extent manipulations are 1027 * complete. The truncate_setsize() call also cleans partial EOF page 1028 * PTEs on extending truncates and hence ensures sub-page block size 1029 * filesystems are correctly handled, too. 1030 * 1031 * We have to do all the page cache truncate work outside the 1032 * transaction context as the "lock" order is page lock->log space 1033 * reservation as defined by extent allocation in the writeback path. 1034 * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but 1035 * having already truncated the in-memory version of the file (i.e. made 1036 * user visible changes). There's not much we can do about this, except 1037 * to hope that the caller sees ENOMEM and retries the truncate 1038 * operation. 1039 * 1040 * And we update in-core i_size and truncate page cache beyond newsize 1041 * before writeback the [i_disk_size, newsize] range, so we're 1042 * guaranteed not to write stale data past the new EOF on truncate down. 1043 */ 1044 truncate_setsize(inode, newsize); 1045 1046 /* 1047 * We are going to log the inode size change in this transaction so 1048 * any previous writes that are beyond the on disk EOF and the new 1049 * EOF that have not been written out need to be written here. If we 1050 * do not write the data out, we expose ourselves to the null files 1051 * problem. Note that this includes any block zeroing we did above; 1052 * otherwise those blocks may not be zeroed after a crash. 1053 */ 1054 if (did_zeroing || 1055 (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) { 1056 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, 1057 ip->i_disk_size, newsize - 1); 1058 if (error) 1059 return error; 1060 } 1061 1062 /* 1063 * For realtime inode with more than one block rtextsize, we need the 1064 * block reservation for bmap btree block allocations/splits that can 1065 * happen since it could split the tail written extent and convert the 1066 * right beyond EOF one to unwritten. 1067 */ 1068 if (xfs_inode_has_bigrtalloc(ip)) 1069 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 1070 1071 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks, 1072 0, 0, &tp); 1073 if (error) 1074 return error; 1075 1076 lock_flags |= XFS_ILOCK_EXCL; 1077 xfs_ilock(ip, XFS_ILOCK_EXCL); 1078 xfs_trans_ijoin(tp, ip, 0); 1079 1080 /* 1081 * Only change the c/mtime if we are changing the size or we are 1082 * explicitly asked to change it. This handles the semantic difference 1083 * between truncate() and ftruncate() as implemented in the VFS. 1084 * 1085 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a 1086 * special case where we need to update the times despite not having 1087 * these flags set. For all other operations the VFS set these flags 1088 * explicitly if it wants a timestamp update. 1089 */ 1090 if (newsize != oldsize && 1091 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) { 1092 iattr->ia_ctime = iattr->ia_mtime = 1093 current_time(inode); 1094 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME; 1095 } 1096 1097 /* 1098 * The first thing we do is set the size to new_size permanently on 1099 * disk. This way we don't have to worry about anyone ever being able 1100 * to look at the data being freed even in the face of a crash. 1101 * What we're getting around here is the case where we free a block, it 1102 * is allocated to another file, it is written to, and then we crash. 1103 * If the new data gets written to the file but the log buffers 1104 * containing the free and reallocation don't, then we'd end up with 1105 * garbage in the blocks being freed. As long as we make the new size 1106 * permanent before actually freeing any blocks it doesn't matter if 1107 * they get written to. 1108 */ 1109 ip->i_disk_size = newsize; 1110 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1111 1112 if (newsize <= oldsize) { 1113 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize); 1114 if (error) 1115 goto out_trans_cancel; 1116 1117 /* 1118 * Truncated "down", so we're removing references to old data 1119 * here - if we delay flushing for a long time, we expose 1120 * ourselves unduly to the notorious NULL files problem. So, 1121 * we mark this inode and flush it when the file is closed, 1122 * and do not wait the usual (long) time for writeout. 1123 */ 1124 xfs_iflags_set(ip, XFS_ITRUNCATED); 1125 1126 /* A truncate down always removes post-EOF blocks. */ 1127 xfs_inode_clear_eofblocks_tag(ip); 1128 } 1129 1130 setattr_copy(idmap, inode, iattr); 1131 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1132 1133 XFS_STATS_INC(mp, xs_ig_attrchg); 1134 1135 if (xfs_has_wsync(mp)) 1136 xfs_trans_set_sync(tp); 1137 1138 error = xfs_trans_commit(tp); 1139 out_unlock: 1140 if (lock_flags) 1141 xfs_iunlock(ip, lock_flags); 1142 return error; 1143 1144 out_trans_cancel: 1145 xfs_trans_cancel(tp); 1146 goto out_unlock; 1147 } 1148 1149 STATIC int 1150 xfs_vn_setattr( 1151 const struct mnt_idmap *idmap, 1152 struct dentry *dentry, 1153 struct iattr *iattr) 1154 { 1155 struct inode *inode = d_inode(dentry); 1156 struct xfs_inode *ip = XFS_I(inode); 1157 int error; 1158 1159 if (iattr->ia_valid & ATTR_SIZE) { 1160 uint iolock; 1161 1162 xfs_ilock(ip, XFS_MMAPLOCK_EXCL); 1163 iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL; 1164 1165 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP); 1166 if (error) { 1167 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); 1168 return error; 1169 } 1170 1171 error = xfs_vn_setattr_size(idmap, dentry, iattr); 1172 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); 1173 } else { 1174 trace_xfs_setattr(ip); 1175 1176 error = xfs_vn_change_ok(idmap, dentry, iattr); 1177 if (!error) 1178 error = xfs_setattr_nonsize(idmap, dentry, ip, iattr); 1179 } 1180 1181 return error; 1182 } 1183 1184 STATIC int 1185 xfs_vn_update_time( 1186 struct inode *inode, 1187 enum fs_update_time type, 1188 unsigned int flags) 1189 { 1190 struct xfs_inode *ip = XFS_I(inode); 1191 struct xfs_mount *mp = ip->i_mount; 1192 int log_flags = XFS_ILOG_TIMESTAMP; 1193 struct xfs_trans *tp; 1194 int error; 1195 1196 trace_xfs_update_time(ip); 1197 1198 if (inode->i_sb->s_flags & SB_LAZYTIME) { 1199 int dirty; 1200 1201 dirty = inode_update_time(inode, type, flags); 1202 if (dirty <= 0) 1203 return dirty; 1204 if (dirty == I_DIRTY_TIME) { 1205 __mark_inode_dirty(inode, I_DIRTY_TIME); 1206 return 0; 1207 } 1208 1209 /* Capture the iversion update that just occurred */ 1210 log_flags |= XFS_ILOG_CORE; 1211 } else { 1212 if (flags & IOCB_NOWAIT) 1213 return -EAGAIN; 1214 } 1215 1216 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); 1217 if (error) 1218 return error; 1219 1220 xfs_ilock(ip, XFS_ILOCK_EXCL); 1221 if (type == FS_UPD_ATIME) 1222 inode_set_atime_to_ts(inode, current_time(inode)); 1223 else 1224 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 1225 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 1226 xfs_trans_log_inode(tp, ip, log_flags); 1227 return xfs_trans_commit(tp); 1228 } 1229 1230 static void 1231 xfs_vn_sync_lazytime( 1232 struct inode *inode) 1233 { 1234 struct xfs_inode *ip = XFS_I(inode); 1235 struct xfs_mount *mp = ip->i_mount; 1236 struct xfs_trans *tp; 1237 1238 if (xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp)) 1239 return; 1240 xfs_ilock(ip, XFS_ILOCK_EXCL); 1241 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 1242 xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP); 1243 xfs_trans_commit(tp); 1244 } 1245 1246 STATIC int 1247 xfs_vn_fiemap( 1248 struct inode *inode, 1249 struct fiemap_extent_info *fieinfo, 1250 u64 start, 1251 u64 length) 1252 { 1253 int error; 1254 1255 xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED); 1256 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 1257 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; 1258 error = iomap_fiemap(inode, fieinfo, start, length, 1259 &xfs_xattr_iomap_ops); 1260 } else { 1261 error = iomap_fiemap(inode, fieinfo, start, length, 1262 &xfs_read_iomap_ops); 1263 } 1264 xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED); 1265 1266 return error; 1267 } 1268 1269 STATIC int 1270 xfs_vn_tmpfile( 1271 const struct mnt_idmap *idmap, 1272 struct inode *dir, 1273 struct file *file, 1274 umode_t mode) 1275 { 1276 int err = xfs_generic_create(idmap, dir, file->f_path.dentry, mode, 0, file); 1277 1278 return finish_open_simple(file, err); 1279 } 1280 1281 static const struct inode_operations xfs_inode_operations = { 1282 .get_inode_acl = xfs_get_acl, 1283 .set_acl = xfs_set_acl, 1284 .getattr = xfs_vn_getattr, 1285 .setattr = xfs_vn_setattr, 1286 .listxattr = xfs_vn_listxattr, 1287 .fiemap = xfs_vn_fiemap, 1288 .update_time = xfs_vn_update_time, 1289 .sync_lazytime = xfs_vn_sync_lazytime, 1290 .fileattr_get = xfs_fileattr_get, 1291 .fileattr_set = xfs_fileattr_set, 1292 }; 1293 1294 static const struct inode_operations xfs_dir_inode_operations = { 1295 .create = xfs_vn_create, 1296 .lookup = xfs_vn_lookup, 1297 .link = xfs_vn_link, 1298 .unlink = xfs_vn_unlink, 1299 .symlink = xfs_vn_symlink, 1300 .mkdir = xfs_vn_mkdir, 1301 /* 1302 * Yes, XFS uses the same method for rmdir and unlink. 1303 * 1304 * There are some subtile differences deeper in the code, 1305 * but we use S_ISDIR to check for those. 1306 */ 1307 .rmdir = xfs_vn_unlink, 1308 .mknod = xfs_vn_mknod, 1309 .rename = xfs_vn_rename, 1310 .get_inode_acl = xfs_get_acl, 1311 .set_acl = xfs_set_acl, 1312 .getattr = xfs_vn_getattr, 1313 .setattr = xfs_vn_setattr, 1314 .listxattr = xfs_vn_listxattr, 1315 .update_time = xfs_vn_update_time, 1316 .sync_lazytime = xfs_vn_sync_lazytime, 1317 .tmpfile = xfs_vn_tmpfile, 1318 .fileattr_get = xfs_fileattr_get, 1319 .fileattr_set = xfs_fileattr_set, 1320 }; 1321 1322 static const struct inode_operations xfs_dir_ci_inode_operations = { 1323 .create = xfs_vn_create, 1324 .lookup = xfs_vn_ci_lookup, 1325 .link = xfs_vn_link, 1326 .unlink = xfs_vn_unlink, 1327 .symlink = xfs_vn_symlink, 1328 .mkdir = xfs_vn_mkdir, 1329 /* 1330 * Yes, XFS uses the same method for rmdir and unlink. 1331 * 1332 * There are some subtile differences deeper in the code, 1333 * but we use S_ISDIR to check for those. 1334 */ 1335 .rmdir = xfs_vn_unlink, 1336 .mknod = xfs_vn_mknod, 1337 .rename = xfs_vn_rename, 1338 .get_inode_acl = xfs_get_acl, 1339 .set_acl = xfs_set_acl, 1340 .getattr = xfs_vn_getattr, 1341 .setattr = xfs_vn_setattr, 1342 .listxattr = xfs_vn_listxattr, 1343 .update_time = xfs_vn_update_time, 1344 .sync_lazytime = xfs_vn_sync_lazytime, 1345 .tmpfile = xfs_vn_tmpfile, 1346 .fileattr_get = xfs_fileattr_get, 1347 .fileattr_set = xfs_fileattr_set, 1348 }; 1349 1350 static const struct inode_operations xfs_symlink_inode_operations = { 1351 .get_link = xfs_vn_get_link, 1352 .getattr = xfs_vn_getattr, 1353 .setattr = xfs_vn_setattr, 1354 .listxattr = xfs_vn_listxattr, 1355 .update_time = xfs_vn_update_time, 1356 .sync_lazytime = xfs_vn_sync_lazytime, 1357 .fileattr_get = xfs_fileattr_get, 1358 .fileattr_set = xfs_fileattr_set, 1359 }; 1360 1361 /* Figure out if this file actually supports DAX. */ 1362 static bool 1363 xfs_inode_supports_dax( 1364 struct xfs_inode *ip) 1365 { 1366 struct xfs_mount *mp = ip->i_mount; 1367 1368 /* Only supported on regular files. */ 1369 if (!S_ISREG(VFS_I(ip)->i_mode)) 1370 return false; 1371 1372 /* Block size must match page size */ 1373 if (mp->m_sb.sb_blocksize != PAGE_SIZE) 1374 return false; 1375 1376 /* Device has to support DAX too. */ 1377 return xfs_inode_buftarg(ip)->bt_daxdev != NULL; 1378 } 1379 1380 static bool 1381 xfs_inode_should_enable_dax( 1382 struct xfs_inode *ip) 1383 { 1384 if (!IS_ENABLED(CONFIG_FS_DAX)) 1385 return false; 1386 if (xfs_has_dax_never(ip->i_mount)) 1387 return false; 1388 if (!xfs_inode_supports_dax(ip)) 1389 return false; 1390 if (xfs_has_dax_always(ip->i_mount)) 1391 return true; 1392 if (ip->i_diflags2 & XFS_DIFLAG2_DAX) 1393 return true; 1394 return false; 1395 } 1396 1397 void 1398 xfs_diflags_to_iflags( 1399 struct xfs_inode *ip, 1400 bool init) 1401 { 1402 struct inode *inode = VFS_I(ip); 1403 unsigned int xflags = xfs_ip2xflags(ip); 1404 unsigned int flags = 0; 1405 1406 ASSERT(!(IS_DAX(inode) && init)); 1407 1408 if (xflags & FS_XFLAG_IMMUTABLE) 1409 flags |= S_IMMUTABLE; 1410 if (xflags & FS_XFLAG_APPEND) 1411 flags |= S_APPEND; 1412 if (xflags & FS_XFLAG_SYNC) 1413 flags |= S_SYNC; 1414 if (xflags & FS_XFLAG_NOATIME) 1415 flags |= S_NOATIME; 1416 if (init && xfs_inode_should_enable_dax(ip)) 1417 flags |= S_DAX; 1418 1419 /* 1420 * S_DAX can only be set during inode initialization and is never set by 1421 * the VFS, so we cannot mask off S_DAX in i_flags. 1422 */ 1423 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME); 1424 inode->i_flags |= flags; 1425 } 1426 1427 /* 1428 * Initialize the Linux inode. 1429 * 1430 * When reading existing inodes from disk this is called directly from xfs_iget, 1431 * when creating a new inode it is called from xfs_init_new_inode after setting 1432 * up the inode. These callers have different criteria for clearing XFS_INEW, so 1433 * leave it up to the caller to deal with unlocking the inode appropriately. 1434 */ 1435 void 1436 xfs_setup_inode( 1437 struct xfs_inode *ip) 1438 { 1439 struct inode *inode = &ip->i_vnode; 1440 gfp_t gfp_mask; 1441 bool is_meta = xfs_is_internal_inode(ip); 1442 1443 inode_state_set_raw(inode, I_NEW); 1444 1445 inode_sb_list_add(inode); 1446 /* make the inode look hashed for the writeback code */ 1447 inode_fake_hash(inode); 1448 1449 i_size_write(inode, ip->i_disk_size); 1450 xfs_diflags_to_iflags(ip, true); 1451 1452 /* 1453 * Mark our metadata files as private so that LSMs and the ACL code 1454 * don't try to add their own metadata or reason about these files, 1455 * and users cannot ever obtain file handles to them. 1456 */ 1457 if (is_meta) { 1458 inode->i_flags |= S_PRIVATE; 1459 inode->i_opflags &= ~IOP_XATTR; 1460 } 1461 1462 if (S_ISDIR(inode->i_mode)) { 1463 /* 1464 * We set the i_rwsem class here to avoid potential races with 1465 * lockdep_annotate_inode_mutex_key() reinitialising the lock 1466 * after a filehandle lookup has already found the inode in 1467 * cache before it has been unlocked via unlock_new_inode(). 1468 */ 1469 lockdep_set_class(&inode->i_rwsem, 1470 &inode->i_sb->s_type->i_mutex_dir_key); 1471 lockdep_set_class(&ip->i_lock, &xfs_dir_ilock_class); 1472 } else { 1473 lockdep_set_class(&ip->i_lock, &xfs_nondir_ilock_class); 1474 } 1475 1476 /* 1477 * Ensure all page cache allocations are done from GFP_NOFS context to 1478 * prevent direct reclaim recursion back into the filesystem and blowing 1479 * stacks or deadlocking. 1480 */ 1481 gfp_mask = mapping_gfp_mask(inode->i_mapping); 1482 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS))); 1483 1484 /* 1485 * For real-time inodes update the stable write flags to that of the RT 1486 * device instead of the data device. 1487 */ 1488 if (S_ISREG(inode->i_mode) && XFS_IS_REALTIME_INODE(ip)) 1489 xfs_update_stable_writes(ip); 1490 1491 /* 1492 * If there is no attribute fork no ACL can exist on this inode, 1493 * and it can't have any file capabilities attached to it either. 1494 */ 1495 if (!xfs_inode_has_attr_fork(ip)) { 1496 inode_has_no_xattr(inode); 1497 cache_no_acl(inode); 1498 } 1499 } 1500 1501 void 1502 xfs_setup_iops( 1503 struct xfs_inode *ip) 1504 { 1505 struct inode *inode = &ip->i_vnode; 1506 1507 switch (inode->i_mode & S_IFMT) { 1508 case S_IFREG: 1509 inode->i_op = &xfs_inode_operations; 1510 inode->i_fop = &xfs_file_operations; 1511 if (IS_DAX(inode)) 1512 inode->i_mapping->a_ops = &xfs_dax_aops; 1513 else 1514 inode->i_mapping->a_ops = &xfs_address_space_operations; 1515 break; 1516 case S_IFDIR: 1517 if (xfs_has_asciici(XFS_M(inode->i_sb))) 1518 inode->i_op = &xfs_dir_ci_inode_operations; 1519 else 1520 inode->i_op = &xfs_dir_inode_operations; 1521 inode->i_fop = &xfs_dir_file_operations; 1522 break; 1523 case S_IFLNK: 1524 inode->i_op = &xfs_symlink_inode_operations; 1525 break; 1526 default: 1527 inode->i_op = &xfs_inode_operations; 1528 init_special_inode(inode, inode->i_mode, inode->i_rdev); 1529 break; 1530 } 1531 } 1532