LCOV - code coverage report
Current view: top level - fs/xfs - xfs_iops.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 471 504 93.5 %
Date: 2023-07-31 20:08:34 Functions: 27 28 96.4 %

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

Generated by: LCOV version 1.14