10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0 2c59d87c4SChristoph Hellwig /* 3c59d87c4SChristoph Hellwig * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4c59d87c4SChristoph Hellwig * All Rights Reserved. 5c59d87c4SChristoph Hellwig */ 6c59d87c4SChristoph Hellwig #include "xfs.h" 7c59d87c4SChristoph Hellwig #include "xfs_fs.h" 870a9883cSDave Chinner #include "xfs_shared.h" 9239880efSDave Chinner #include "xfs_format.h" 10239880efSDave Chinner #include "xfs_log_format.h" 11239880efSDave Chinner #include "xfs_trans_resv.h" 12c59d87c4SChristoph Hellwig #include "xfs_mount.h" 13c59d87c4SChristoph Hellwig #include "xfs_inode.h" 14239880efSDave Chinner #include "xfs_acl.h" 15239880efSDave Chinner #include "xfs_quota.h" 16fd920008SAllison Henderson #include "xfs_da_format.h" 17fd920008SAllison Henderson #include "xfs_da_btree.h" 18c59d87c4SChristoph Hellwig #include "xfs_attr.h" 19239880efSDave Chinner #include "xfs_trans.h" 20c59d87c4SChristoph Hellwig #include "xfs_trace.h" 2127b52867SBrian Foster #include "xfs_icache.h" 22c24b5dfaSDave Chinner #include "xfs_symlink.h" 231b767ee3SDave Chinner #include "xfs_dir2.h" 2468a9f5e7SChristoph Hellwig #include "xfs_iomap.h" 25a5155b87SDarrick J. Wong #include "xfs_error.h" 269fefd5dbSMiklos Szeredi #include "xfs_ioctl.h" 27efc2efebSDarrick J. Wong #include "xfs_xattr.h" 28c59d87c4SChristoph Hellwig 29c59d87c4SChristoph Hellwig #include <linux/posix_acl.h> 30c59d87c4SChristoph Hellwig #include <linux/security.h> 31c3b1b131SChristoph Hellwig #include <linux/iversion.h> 3210c5db28SChristoph Hellwig #include <linux/fiemap.h> 33c59d87c4SChristoph Hellwig 3493a8614eSDave Chinner /* 35c1e8d7c6SMichel Lespinasse * Directories have different lock order w.r.t. mmap_lock compared to regular 3693a8614eSDave Chinner * files. This is due to readdir potentially triggering page faults on a user 3793a8614eSDave Chinner * buffer inside filldir(), and this happens with the ilock on the directory 3893a8614eSDave Chinner * held. For regular files, the lock order is the other way around - the 39c1e8d7c6SMichel Lespinasse * mmap_lock is taken during the page fault, and then we lock the ilock to do 4093a8614eSDave Chinner * block mapping. Hence we need a different class for the directory ilock so 4193a8614eSDave Chinner * that lockdep can tell them apart. 4293a8614eSDave Chinner */ 4393a8614eSDave Chinner static struct lock_class_key xfs_nondir_ilock_class; 4493a8614eSDave Chinner static struct lock_class_key xfs_dir_ilock_class; 4593a8614eSDave Chinner 468d2a5e6eSDave Chinner static int 478d2a5e6eSDave Chinner xfs_initxattrs( 488d2a5e6eSDave Chinner struct inode *inode, 498d2a5e6eSDave Chinner const struct xattr *xattr_array, 5036b8d186SLinus Torvalds void *fs_info) 5136b8d186SLinus Torvalds { 5236b8d186SLinus Torvalds const struct xattr *xattr; 5336b8d186SLinus Torvalds struct xfs_inode *ip = XFS_I(inode); 5436b8d186SLinus Torvalds int error = 0; 5536b8d186SLinus Torvalds 5636b8d186SLinus Torvalds for (xattr = xattr_array; xattr->name != NULL; xattr++) { 57a2544622SChristoph Hellwig struct xfs_da_args args = { 58a2544622SChristoph Hellwig .dp = ip, 59d5f0f49aSChristoph Hellwig .attr_filter = XFS_ATTR_SECURE, 60a2544622SChristoph Hellwig .name = xattr->name, 61a2544622SChristoph Hellwig .namelen = strlen(xattr->name), 62a2544622SChristoph Hellwig .value = xattr->value, 63a2544622SChristoph Hellwig .valuelen = xattr->value_len, 64a2544622SChristoph Hellwig }; 65efc2efebSDarrick J. Wong error = xfs_attr_change(&args); 6636b8d186SLinus Torvalds if (error < 0) 6736b8d186SLinus Torvalds break; 6836b8d186SLinus Torvalds } 6936b8d186SLinus Torvalds return error; 7036b8d186SLinus Torvalds } 7136b8d186SLinus Torvalds 72c59d87c4SChristoph Hellwig /* 73c59d87c4SChristoph Hellwig * Hook in SELinux. This is not quite correct yet, what we really need 74c59d87c4SChristoph Hellwig * here (as we do for default ACLs) is a mechanism by which creation of 75c59d87c4SChristoph Hellwig * these attrs can be journalled at inode creation time (along with the 76c59d87c4SChristoph Hellwig * inode, of course, such that log replay can't cause these to be lost). 77c59d87c4SChristoph Hellwig */ 7870b589a3SEric Sandeen int 7970b589a3SEric Sandeen xfs_inode_init_security( 80c59d87c4SChristoph Hellwig struct inode *inode, 81c59d87c4SChristoph Hellwig struct inode *dir, 82c59d87c4SChristoph Hellwig const struct qstr *qstr) 83c59d87c4SChristoph Hellwig { 842451337dSDave Chinner return security_inode_init_security(inode, dir, qstr, 8536b8d186SLinus Torvalds &xfs_initxattrs, NULL); 86c59d87c4SChristoph Hellwig } 87c59d87c4SChristoph Hellwig 88c59d87c4SChristoph Hellwig static void 89c59d87c4SChristoph Hellwig xfs_dentry_to_name( 90c59d87c4SChristoph Hellwig struct xfs_name *namep, 91fab8eef8SAmir Goldstein struct dentry *dentry) 92fab8eef8SAmir Goldstein { 93fab8eef8SAmir Goldstein namep->name = dentry->d_name.name; 94fab8eef8SAmir Goldstein namep->len = dentry->d_name.len; 95fab8eef8SAmir Goldstein namep->type = XFS_DIR3_FT_UNKNOWN; 96fab8eef8SAmir Goldstein } 97fab8eef8SAmir Goldstein 98fab8eef8SAmir Goldstein static int 99fab8eef8SAmir Goldstein xfs_dentry_mode_to_name( 100fab8eef8SAmir Goldstein struct xfs_name *namep, 1010cb97766SDave Chinner struct dentry *dentry, 1020cb97766SDave Chinner int mode) 103c59d87c4SChristoph Hellwig { 104c59d87c4SChristoph Hellwig namep->name = dentry->d_name.name; 105c59d87c4SChristoph Hellwig namep->len = dentry->d_name.len; 1061fc4d33fSAmir Goldstein namep->type = xfs_mode_to_ftype(mode); 107fab8eef8SAmir Goldstein 108fab8eef8SAmir Goldstein if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN)) 109fab8eef8SAmir Goldstein return -EFSCORRUPTED; 110fab8eef8SAmir Goldstein 111fab8eef8SAmir Goldstein return 0; 112c59d87c4SChristoph Hellwig } 113c59d87c4SChristoph Hellwig 114c59d87c4SChristoph Hellwig STATIC void 115c59d87c4SChristoph Hellwig xfs_cleanup_inode( 116c59d87c4SChristoph Hellwig struct inode *dir, 117c59d87c4SChristoph Hellwig struct inode *inode, 118c59d87c4SChristoph Hellwig struct dentry *dentry) 119c59d87c4SChristoph Hellwig { 120c59d87c4SChristoph Hellwig struct xfs_name teardown; 121c59d87c4SChristoph Hellwig 122c59d87c4SChristoph Hellwig /* Oh, the horror. 123c59d87c4SChristoph Hellwig * If we can't add the ACL or we fail in 12470b589a3SEric Sandeen * xfs_inode_init_security we must back out. 125c59d87c4SChristoph Hellwig * ENOSPC can hit here, among other things. 126c59d87c4SChristoph Hellwig */ 127fab8eef8SAmir Goldstein xfs_dentry_to_name(&teardown, dentry); 128c59d87c4SChristoph Hellwig 129c59d87c4SChristoph Hellwig xfs_remove(XFS_I(dir), &teardown, XFS_I(inode)); 130c59d87c4SChristoph Hellwig } 131c59d87c4SChristoph Hellwig 132e6a688c3SDave Chinner /* 133e6a688c3SDave Chinner * Check to see if we are likely to need an extended attribute to be added to 134e6a688c3SDave Chinner * the inode we are about to allocate. This allows the attribute fork to be 135e6a688c3SDave Chinner * created during the inode allocation, reducing the number of transactions we 136e6a688c3SDave Chinner * need to do in this fast path. 137e6a688c3SDave Chinner * 138e6a688c3SDave Chinner * The security checks are optimistic, but not guaranteed. The two LSMs that 139e6a688c3SDave Chinner * require xattrs to be added here (selinux and smack) are also the only two 140e6a688c3SDave Chinner * LSMs that add a sb->s_security structure to the superblock. Hence if security 141e6a688c3SDave Chinner * is enabled and sb->s_security is set, we have a pretty good idea that we are 142e6a688c3SDave Chinner * going to be asked to add a security xattr immediately after allocating the 143e6a688c3SDave Chinner * xfs inode and instantiating the VFS inode. 144e6a688c3SDave Chinner */ 145e6a688c3SDave Chinner static inline bool 146e6a688c3SDave Chinner xfs_create_need_xattr( 147e6a688c3SDave Chinner struct inode *dir, 148e6a688c3SDave Chinner struct posix_acl *default_acl, 149e6a688c3SDave Chinner struct posix_acl *acl) 150e6a688c3SDave Chinner { 151e6a688c3SDave Chinner if (acl) 152e6a688c3SDave Chinner return true; 153e6a688c3SDave Chinner if (default_acl) 154e6a688c3SDave Chinner return true; 155e6a688c3SDave Chinner #if IS_ENABLED(CONFIG_SECURITY) 156e6a688c3SDave Chinner if (dir->i_sb->s_security) 157e6a688c3SDave Chinner return true; 158e6a688c3SDave Chinner #endif 159e6a688c3SDave Chinner return false; 160e6a688c3SDave Chinner } 161e6a688c3SDave Chinner 162e6a688c3SDave Chinner 163c59d87c4SChristoph Hellwig STATIC int 164d540e43bSBrian Foster xfs_generic_create( 165f2d40141SChristian Brauner struct mnt_idmap *idmap, 166c59d87c4SChristoph Hellwig struct inode *dir, 167c59d87c4SChristoph Hellwig struct dentry *dentry, 1681a67aafbSAl Viro umode_t mode, 169d540e43bSBrian Foster dev_t rdev, 170863f144fSMiklos Szeredi struct file *tmpfile) /* unnamed file */ 171c59d87c4SChristoph Hellwig { 172c59d87c4SChristoph Hellwig struct inode *inode; 173c59d87c4SChristoph Hellwig struct xfs_inode *ip = NULL; 1742401dc29SChristoph Hellwig struct posix_acl *default_acl, *acl; 175c59d87c4SChristoph Hellwig struct xfs_name name; 176c59d87c4SChristoph Hellwig int error; 177c59d87c4SChristoph Hellwig 178c59d87c4SChristoph Hellwig /* 179c59d87c4SChristoph Hellwig * Irix uses Missed'em'V split, but doesn't want to see 180c59d87c4SChristoph Hellwig * the upper 5 bits of (14bit) major. 181c59d87c4SChristoph Hellwig */ 182c59d87c4SChristoph Hellwig if (S_ISCHR(mode) || S_ISBLK(mode)) { 183c59d87c4SChristoph Hellwig if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) 184c59d87c4SChristoph Hellwig return -EINVAL; 185c59d87c4SChristoph Hellwig } else { 186c59d87c4SChristoph Hellwig rdev = 0; 187c59d87c4SChristoph Hellwig } 188c59d87c4SChristoph Hellwig 1892401dc29SChristoph Hellwig error = posix_acl_create(dir, &mode, &default_acl, &acl); 1902401dc29SChristoph Hellwig if (error) 1912401dc29SChristoph Hellwig return error; 192c59d87c4SChristoph Hellwig 193fab8eef8SAmir Goldstein /* Verify mode is valid also for tmpfile case */ 194fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&name, dentry, mode); 195fab8eef8SAmir Goldstein if (unlikely(error)) 196fab8eef8SAmir Goldstein goto out_free_acl; 197fab8eef8SAmir Goldstein 198d540e43bSBrian Foster if (!tmpfile) { 199f2d40141SChristian Brauner error = xfs_create(idmap, XFS_I(dir), &name, mode, rdev, 200e6a688c3SDave Chinner xfs_create_need_xattr(dir, default_acl, acl), 201f736d93dSChristoph Hellwig &ip); 202d540e43bSBrian Foster } else { 203f2d40141SChristian Brauner error = xfs_create_tmpfile(idmap, XFS_I(dir), mode, &ip); 204d540e43bSBrian Foster } 205c59d87c4SChristoph Hellwig if (unlikely(error)) 206c59d87c4SChristoph Hellwig goto out_free_acl; 207c59d87c4SChristoph Hellwig 208c59d87c4SChristoph Hellwig inode = VFS_I(ip); 209c59d87c4SChristoph Hellwig 21070b589a3SEric Sandeen error = xfs_inode_init_security(inode, dir, &dentry->d_name); 211c59d87c4SChristoph Hellwig if (unlikely(error)) 212c59d87c4SChristoph Hellwig goto out_cleanup_inode; 213c59d87c4SChristoph Hellwig 214c59d87c4SChristoph Hellwig if (default_acl) { 2158ba35875SJan Kara error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); 2162401dc29SChristoph Hellwig if (error) 217c59d87c4SChristoph Hellwig goto out_cleanup_inode; 218c59d87c4SChristoph Hellwig } 2192401dc29SChristoph Hellwig if (acl) { 2208ba35875SJan Kara error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS); 2212401dc29SChristoph Hellwig if (error) 2222401dc29SChristoph Hellwig goto out_cleanup_inode; 2232401dc29SChristoph Hellwig } 224c59d87c4SChristoph Hellwig 2252b3d1d41SChristoph Hellwig xfs_setup_iops(ip); 2262b3d1d41SChristoph Hellwig 227c4a6bf7fSDarrick J. Wong if (tmpfile) { 228c4a6bf7fSDarrick J. Wong /* 229c4a6bf7fSDarrick J. Wong * The VFS requires that any inode fed to d_tmpfile must have 230c4a6bf7fSDarrick J. Wong * nlink == 1 so that it can decrement the nlink in d_tmpfile. 231c4a6bf7fSDarrick J. Wong * However, we created the temp file with nlink == 0 because 232c4a6bf7fSDarrick J. Wong * we're not allowed to put an inode with nlink > 0 on the 233c4a6bf7fSDarrick J. Wong * unlinked list. Therefore we have to set nlink to 1 so that 234c4a6bf7fSDarrick J. Wong * d_tmpfile can immediately set it back to zero. 235c4a6bf7fSDarrick J. Wong */ 236c4a6bf7fSDarrick J. Wong set_nlink(inode, 1); 237863f144fSMiklos Szeredi d_tmpfile(tmpfile, inode); 238c4a6bf7fSDarrick J. Wong } else 239c59d87c4SChristoph Hellwig d_instantiate(dentry, inode); 240d540e43bSBrian Foster 24158c90473SDave Chinner xfs_finish_inode_setup(ip); 24258c90473SDave Chinner 2432401dc29SChristoph Hellwig out_free_acl: 2442401dc29SChristoph Hellwig posix_acl_release(default_acl); 2452401dc29SChristoph Hellwig posix_acl_release(acl); 2462451337dSDave Chinner return error; 247c59d87c4SChristoph Hellwig 248c59d87c4SChristoph Hellwig out_cleanup_inode: 24958c90473SDave Chinner xfs_finish_inode_setup(ip); 250d540e43bSBrian Foster if (!tmpfile) 251c59d87c4SChristoph Hellwig xfs_cleanup_inode(dir, inode, dentry); 25244a8736bSDarrick J. Wong xfs_irele(ip); 2532401dc29SChristoph Hellwig goto out_free_acl; 254c59d87c4SChristoph Hellwig } 255c59d87c4SChristoph Hellwig 256c59d87c4SChristoph Hellwig STATIC int 257d540e43bSBrian Foster xfs_vn_mknod( 2585ebb29beSChristian Brauner struct mnt_idmap *idmap, 259d540e43bSBrian Foster struct inode *dir, 260d540e43bSBrian Foster struct dentry *dentry, 261d540e43bSBrian Foster umode_t mode, 262d540e43bSBrian Foster dev_t rdev) 263d540e43bSBrian Foster { 264f2d40141SChristian Brauner return xfs_generic_create(idmap, dir, dentry, mode, rdev, NULL); 265d540e43bSBrian Foster } 266d540e43bSBrian Foster 267d540e43bSBrian Foster STATIC int 268c59d87c4SChristoph Hellwig xfs_vn_create( 2696c960e68SChristian Brauner struct mnt_idmap *idmap, 270c59d87c4SChristoph Hellwig struct inode *dir, 271c59d87c4SChristoph Hellwig struct dentry *dentry, 2724acdaf27SAl Viro umode_t mode, 273ebfc3b49SAl Viro bool flags) 274c59d87c4SChristoph Hellwig { 275f2d40141SChristian Brauner return xfs_generic_create(idmap, dir, dentry, mode, 0, NULL); 276c59d87c4SChristoph Hellwig } 277c59d87c4SChristoph Hellwig 278c59d87c4SChristoph Hellwig STATIC int 279c59d87c4SChristoph Hellwig xfs_vn_mkdir( 280c54bd91eSChristian Brauner struct mnt_idmap *idmap, 281c59d87c4SChristoph Hellwig struct inode *dir, 282c59d87c4SChristoph Hellwig struct dentry *dentry, 28318bb1db3SAl Viro umode_t mode) 284c59d87c4SChristoph Hellwig { 285f2d40141SChristian Brauner return xfs_generic_create(idmap, dir, dentry, mode | S_IFDIR, 0, NULL); 286c59d87c4SChristoph Hellwig } 287c59d87c4SChristoph Hellwig 288c59d87c4SChristoph Hellwig STATIC struct dentry * 289c59d87c4SChristoph Hellwig xfs_vn_lookup( 290c59d87c4SChristoph Hellwig struct inode *dir, 291c59d87c4SChristoph Hellwig struct dentry *dentry, 29200cd8dd3SAl Viro unsigned int flags) 293c59d87c4SChristoph Hellwig { 294b113a6d3SAl Viro struct inode *inode; 295c59d87c4SChristoph Hellwig struct xfs_inode *cip; 296c59d87c4SChristoph Hellwig struct xfs_name name; 297c59d87c4SChristoph Hellwig int error; 298c59d87c4SChristoph Hellwig 299c59d87c4SChristoph Hellwig if (dentry->d_name.len >= MAXNAMELEN) 300c59d87c4SChristoph Hellwig return ERR_PTR(-ENAMETOOLONG); 301c59d87c4SChristoph Hellwig 302fab8eef8SAmir Goldstein xfs_dentry_to_name(&name, dentry); 303c59d87c4SChristoph Hellwig error = xfs_lookup(XFS_I(dir), &name, &cip, NULL); 304b113a6d3SAl Viro if (likely(!error)) 305b113a6d3SAl Viro inode = VFS_I(cip); 306b113a6d3SAl Viro else if (likely(error == -ENOENT)) 307b113a6d3SAl Viro inode = NULL; 308b113a6d3SAl Viro else 309b113a6d3SAl Viro inode = ERR_PTR(error); 310b113a6d3SAl Viro return d_splice_alias(inode, dentry); 311c59d87c4SChristoph Hellwig } 312c59d87c4SChristoph Hellwig 313c59d87c4SChristoph Hellwig STATIC struct dentry * 314c59d87c4SChristoph Hellwig xfs_vn_ci_lookup( 315c59d87c4SChristoph Hellwig struct inode *dir, 316c59d87c4SChristoph Hellwig struct dentry *dentry, 31700cd8dd3SAl Viro unsigned int flags) 318c59d87c4SChristoph Hellwig { 319c59d87c4SChristoph Hellwig struct xfs_inode *ip; 320c59d87c4SChristoph Hellwig struct xfs_name xname; 321c59d87c4SChristoph Hellwig struct xfs_name ci_name; 322c59d87c4SChristoph Hellwig struct qstr dname; 323c59d87c4SChristoph Hellwig int error; 324c59d87c4SChristoph Hellwig 325c59d87c4SChristoph Hellwig if (dentry->d_name.len >= MAXNAMELEN) 326c59d87c4SChristoph Hellwig return ERR_PTR(-ENAMETOOLONG); 327c59d87c4SChristoph Hellwig 328fab8eef8SAmir Goldstein xfs_dentry_to_name(&xname, dentry); 329c59d87c4SChristoph Hellwig error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name); 330c59d87c4SChristoph Hellwig if (unlikely(error)) { 3312451337dSDave Chinner if (unlikely(error != -ENOENT)) 3322451337dSDave Chinner return ERR_PTR(error); 333c59d87c4SChristoph Hellwig /* 334c59d87c4SChristoph Hellwig * call d_add(dentry, NULL) here when d_drop_negative_children 335c59d87c4SChristoph Hellwig * is called in xfs_vn_mknod (ie. allow negative dentries 336c59d87c4SChristoph Hellwig * with CI filesystems). 337c59d87c4SChristoph Hellwig */ 338c59d87c4SChristoph Hellwig return NULL; 339c59d87c4SChristoph Hellwig } 340c59d87c4SChristoph Hellwig 341c59d87c4SChristoph Hellwig /* if exact match, just splice and exit */ 342c59d87c4SChristoph Hellwig if (!ci_name.name) 343c59d87c4SChristoph Hellwig return d_splice_alias(VFS_I(ip), dentry); 344c59d87c4SChristoph Hellwig 345c59d87c4SChristoph Hellwig /* else case-insensitive match... */ 346c59d87c4SChristoph Hellwig dname.name = ci_name.name; 347c59d87c4SChristoph Hellwig dname.len = ci_name.len; 348c59d87c4SChristoph Hellwig dentry = d_add_ci(dentry, VFS_I(ip), &dname); 349c59d87c4SChristoph Hellwig kmem_free(ci_name.name); 350c59d87c4SChristoph Hellwig return dentry; 351c59d87c4SChristoph Hellwig } 352c59d87c4SChristoph Hellwig 353c59d87c4SChristoph Hellwig STATIC int 354c59d87c4SChristoph Hellwig xfs_vn_link( 355c59d87c4SChristoph Hellwig struct dentry *old_dentry, 356c59d87c4SChristoph Hellwig struct inode *dir, 357c59d87c4SChristoph Hellwig struct dentry *dentry) 358c59d87c4SChristoph Hellwig { 3592b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry); 360c59d87c4SChristoph Hellwig struct xfs_name name; 361c59d87c4SChristoph Hellwig int error; 362c59d87c4SChristoph Hellwig 363fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode); 364fab8eef8SAmir Goldstein if (unlikely(error)) 365fab8eef8SAmir Goldstein return error; 366c59d87c4SChristoph Hellwig 367c59d87c4SChristoph Hellwig error = xfs_link(XFS_I(dir), XFS_I(inode), &name); 368c59d87c4SChristoph Hellwig if (unlikely(error)) 3692451337dSDave Chinner return error; 370c59d87c4SChristoph Hellwig 371c59d87c4SChristoph Hellwig ihold(inode); 372c59d87c4SChristoph Hellwig d_instantiate(dentry, inode); 373c59d87c4SChristoph Hellwig return 0; 374c59d87c4SChristoph Hellwig } 375c59d87c4SChristoph Hellwig 376c59d87c4SChristoph Hellwig STATIC int 377c59d87c4SChristoph Hellwig xfs_vn_unlink( 378c59d87c4SChristoph Hellwig struct inode *dir, 379c59d87c4SChristoph Hellwig struct dentry *dentry) 380c59d87c4SChristoph Hellwig { 381c59d87c4SChristoph Hellwig struct xfs_name name; 382c59d87c4SChristoph Hellwig int error; 383c59d87c4SChristoph Hellwig 384fab8eef8SAmir Goldstein xfs_dentry_to_name(&name, dentry); 385c59d87c4SChristoph Hellwig 3862b0143b5SDavid Howells error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry))); 387c59d87c4SChristoph Hellwig if (error) 388c59d87c4SChristoph Hellwig return error; 389c59d87c4SChristoph Hellwig 390c59d87c4SChristoph Hellwig /* 391c59d87c4SChristoph Hellwig * With unlink, the VFS makes the dentry "negative": no inode, 392c59d87c4SChristoph Hellwig * but still hashed. This is incompatible with case-insensitive 393c59d87c4SChristoph Hellwig * mode, so invalidate (unhash) the dentry in CI-mode. 394c59d87c4SChristoph Hellwig */ 39538c26bfdSDave Chinner if (xfs_has_asciici(XFS_M(dir->i_sb))) 396c59d87c4SChristoph Hellwig d_invalidate(dentry); 397c59d87c4SChristoph Hellwig return 0; 398c59d87c4SChristoph Hellwig } 399c59d87c4SChristoph Hellwig 400c59d87c4SChristoph Hellwig STATIC int 401c59d87c4SChristoph Hellwig xfs_vn_symlink( 4027a77db95SChristian Brauner struct mnt_idmap *idmap, 403c59d87c4SChristoph Hellwig struct inode *dir, 404c59d87c4SChristoph Hellwig struct dentry *dentry, 405c59d87c4SChristoph Hellwig const char *symname) 406c59d87c4SChristoph Hellwig { 407c59d87c4SChristoph Hellwig struct inode *inode; 408c59d87c4SChristoph Hellwig struct xfs_inode *cip = NULL; 409c59d87c4SChristoph Hellwig struct xfs_name name; 410c59d87c4SChristoph Hellwig int error; 411576b1d67SAl Viro umode_t mode; 412c59d87c4SChristoph Hellwig 413c59d87c4SChristoph Hellwig mode = S_IFLNK | 414c59d87c4SChristoph Hellwig (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO); 415fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&name, dentry, mode); 416fab8eef8SAmir Goldstein if (unlikely(error)) 417fab8eef8SAmir Goldstein goto out; 418c59d87c4SChristoph Hellwig 419f2d40141SChristian Brauner error = xfs_symlink(idmap, XFS_I(dir), &name, symname, mode, &cip); 420c59d87c4SChristoph Hellwig if (unlikely(error)) 421c59d87c4SChristoph Hellwig goto out; 422c59d87c4SChristoph Hellwig 423c59d87c4SChristoph Hellwig inode = VFS_I(cip); 424c59d87c4SChristoph Hellwig 42570b589a3SEric Sandeen error = xfs_inode_init_security(inode, dir, &dentry->d_name); 426c59d87c4SChristoph Hellwig if (unlikely(error)) 427c59d87c4SChristoph Hellwig goto out_cleanup_inode; 428c59d87c4SChristoph Hellwig 4292b3d1d41SChristoph Hellwig xfs_setup_iops(cip); 4302b3d1d41SChristoph Hellwig 431c59d87c4SChristoph Hellwig d_instantiate(dentry, inode); 43258c90473SDave Chinner xfs_finish_inode_setup(cip); 433c59d87c4SChristoph Hellwig return 0; 434c59d87c4SChristoph Hellwig 435c59d87c4SChristoph Hellwig out_cleanup_inode: 43658c90473SDave Chinner xfs_finish_inode_setup(cip); 437c59d87c4SChristoph Hellwig xfs_cleanup_inode(dir, inode, dentry); 43844a8736bSDarrick J. Wong xfs_irele(cip); 439c59d87c4SChristoph Hellwig out: 4402451337dSDave Chinner return error; 441c59d87c4SChristoph Hellwig } 442c59d87c4SChristoph Hellwig 443c59d87c4SChristoph Hellwig STATIC int 444c59d87c4SChristoph Hellwig xfs_vn_rename( 445e18275aeSChristian Brauner struct mnt_idmap *idmap, 446c59d87c4SChristoph Hellwig struct inode *odir, 447c59d87c4SChristoph Hellwig struct dentry *odentry, 448c59d87c4SChristoph Hellwig struct inode *ndir, 449dbe1b5caSCarlos Maiolino struct dentry *ndentry, 450dbe1b5caSCarlos Maiolino unsigned int flags) 451c59d87c4SChristoph Hellwig { 4522b0143b5SDavid Howells struct inode *new_inode = d_inode(ndentry); 453d31a1825SCarlos Maiolino int omode = 0; 454fab8eef8SAmir Goldstein int error; 455c59d87c4SChristoph Hellwig struct xfs_name oname; 456c59d87c4SChristoph Hellwig struct xfs_name nname; 457c59d87c4SChristoph Hellwig 4587dcf5c3eSDave Chinner if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 459dbe1b5caSCarlos Maiolino return -EINVAL; 460dbe1b5caSCarlos Maiolino 461d31a1825SCarlos Maiolino /* if we are exchanging files, we need to set i_mode of both files */ 462d31a1825SCarlos Maiolino if (flags & RENAME_EXCHANGE) 4632b0143b5SDavid Howells omode = d_inode(ndentry)->i_mode; 464d31a1825SCarlos Maiolino 465fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&oname, odentry, omode); 466fab8eef8SAmir Goldstein if (omode && unlikely(error)) 467fab8eef8SAmir Goldstein return error; 468fab8eef8SAmir Goldstein 469fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&nname, ndentry, 470fab8eef8SAmir Goldstein d_inode(odentry)->i_mode); 471fab8eef8SAmir Goldstein if (unlikely(error)) 472fab8eef8SAmir Goldstein return error; 473c59d87c4SChristoph Hellwig 474f2d40141SChristian Brauner return xfs_rename(idmap, XFS_I(odir), &oname, 475f736d93dSChristoph Hellwig XFS_I(d_inode(odentry)), XFS_I(ndir), &nname, 476d31a1825SCarlos Maiolino new_inode ? XFS_I(new_inode) : NULL, flags); 477c59d87c4SChristoph Hellwig } 478c59d87c4SChristoph Hellwig 479c59d87c4SChristoph Hellwig /* 480c59d87c4SChristoph Hellwig * careful here - this function can get called recursively, so 481c59d87c4SChristoph Hellwig * we need to be very careful about how much stack we use. 482c59d87c4SChristoph Hellwig * uio is kmalloced for this reason... 483c59d87c4SChristoph Hellwig */ 484680baacbSAl Viro STATIC const char * 4856b255391SAl Viro xfs_vn_get_link( 486c59d87c4SChristoph Hellwig struct dentry *dentry, 4876b255391SAl Viro struct inode *inode, 488fceef393SAl Viro struct delayed_call *done) 489c59d87c4SChristoph Hellwig { 490c59d87c4SChristoph Hellwig char *link; 491c59d87c4SChristoph Hellwig int error = -ENOMEM; 492c59d87c4SChristoph Hellwig 4936b255391SAl Viro if (!dentry) 4946b255391SAl Viro return ERR_PTR(-ECHILD); 4956b255391SAl Viro 4966eb0b8dfSDarrick J. Wong link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL); 497c59d87c4SChristoph Hellwig if (!link) 498c59d87c4SChristoph Hellwig goto out_err; 499c59d87c4SChristoph Hellwig 5002b0143b5SDavid Howells error = xfs_readlink(XFS_I(d_inode(dentry)), link); 501c59d87c4SChristoph Hellwig if (unlikely(error)) 502c59d87c4SChristoph Hellwig goto out_kfree; 503c59d87c4SChristoph Hellwig 504fceef393SAl Viro set_delayed_call(done, kfree_link, link); 505fceef393SAl Viro return link; 506c59d87c4SChristoph Hellwig 507c59d87c4SChristoph Hellwig out_kfree: 508c59d87c4SChristoph Hellwig kfree(link); 509c59d87c4SChristoph Hellwig out_err: 510680baacbSAl Viro return ERR_PTR(error); 511c59d87c4SChristoph Hellwig } 512c59d87c4SChristoph Hellwig 513dd2d535eSChristoph Hellwig static uint32_t 514dd2d535eSChristoph Hellwig xfs_stat_blksize( 515dd2d535eSChristoph Hellwig struct xfs_inode *ip) 516dd2d535eSChristoph Hellwig { 517dd2d535eSChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 518dd2d535eSChristoph Hellwig 519dd2d535eSChristoph Hellwig /* 520dd2d535eSChristoph Hellwig * If the file blocks are being allocated from a realtime volume, then 521dd2d535eSChristoph Hellwig * always return the realtime extent size. 522dd2d535eSChristoph Hellwig */ 523dd2d535eSChristoph Hellwig if (XFS_IS_REALTIME_INODE(ip)) 524a7bcb147SDarrick J. Wong return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip)); 525dd2d535eSChristoph Hellwig 526dd2d535eSChristoph Hellwig /* 527dd2d535eSChristoph Hellwig * Allow large block sizes to be reported to userspace programs if the 528dd2d535eSChristoph Hellwig * "largeio" mount option is used. 529dd2d535eSChristoph Hellwig * 530dd2d535eSChristoph Hellwig * If compatibility mode is specified, simply return the basic unit of 531dd2d535eSChristoph Hellwig * caching so that we don't get inefficient read/modify/write I/O from 532dd2d535eSChristoph Hellwig * user apps. Otherwise.... 533dd2d535eSChristoph Hellwig * 534dd2d535eSChristoph Hellwig * If the underlying volume is a stripe, then return the stripe width in 535dd2d535eSChristoph Hellwig * bytes as the recommended I/O size. It is not a stripe and we've set a 536dd2d535eSChristoph Hellwig * default buffered I/O size, return that, otherwise return the compat 537dd2d535eSChristoph Hellwig * default. 538dd2d535eSChristoph Hellwig */ 5390560f31aSDave Chinner if (xfs_has_large_iosize(mp)) { 540dd2d535eSChristoph Hellwig if (mp->m_swidth) 541a7bcb147SDarrick J. Wong return XFS_FSB_TO_B(mp, mp->m_swidth); 5420560f31aSDave Chinner if (xfs_has_allocsize(mp)) 5435da8a07cSChristoph Hellwig return 1U << mp->m_allocsize_log; 544dd2d535eSChristoph Hellwig } 545dd2d535eSChristoph Hellwig 546dd2d535eSChristoph Hellwig return PAGE_SIZE; 547dd2d535eSChristoph Hellwig } 548dd2d535eSChristoph Hellwig 549c59d87c4SChristoph Hellwig STATIC int 550c59d87c4SChristoph Hellwig xfs_vn_getattr( 551b74d24f7SChristian Brauner struct mnt_idmap *idmap, 552a528d35eSDavid Howells const struct path *path, 553a528d35eSDavid Howells struct kstat *stat, 554a528d35eSDavid Howells u32 request_mask, 555a528d35eSDavid Howells unsigned int query_flags) 556c59d87c4SChristoph Hellwig { 557a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 558c59d87c4SChristoph Hellwig struct xfs_inode *ip = XFS_I(inode); 559c59d87c4SChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 560e67fe633SChristian Brauner vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 561e67fe633SChristian Brauner vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 562c59d87c4SChristoph Hellwig 563c59d87c4SChristoph Hellwig trace_xfs_getattr(ip); 564c59d87c4SChristoph Hellwig 56575c8c50fSDave Chinner if (xfs_is_shutdown(mp)) 566b474c7aeSEric Sandeen return -EIO; 567c59d87c4SChristoph Hellwig 568c59d87c4SChristoph Hellwig stat->size = XFS_ISIZE(ip); 569c59d87c4SChristoph Hellwig stat->dev = inode->i_sb->s_dev; 570c19b3b05SDave Chinner stat->mode = inode->i_mode; 57154d7b5c1SDave Chinner stat->nlink = inode->i_nlink; 57242b7cc11SChristian Brauner stat->uid = vfsuid_into_kuid(vfsuid); 57342b7cc11SChristian Brauner stat->gid = vfsgid_into_kgid(vfsgid); 574c59d87c4SChristoph Hellwig stat->ino = ip->i_ino; 575c59d87c4SChristoph Hellwig stat->atime = inode->i_atime; 576c59d87c4SChristoph Hellwig stat->mtime = inode->i_mtime; 577a0a415e3SJeff Layton stat->ctime = inode_get_ctime(inode); 5786e73a545SChristoph Hellwig stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks); 579c59d87c4SChristoph Hellwig 58038c26bfdSDave Chinner if (xfs_has_v3inodes(mp)) { 5815f955f26SDarrick J. Wong if (request_mask & STATX_BTIME) { 5825f955f26SDarrick J. Wong stat->result_mask |= STATX_BTIME; 583e98d5e88SChristoph Hellwig stat->btime = ip->i_crtime; 5845f955f26SDarrick J. Wong } 5855f955f26SDarrick J. Wong } 5865f955f26SDarrick J. Wong 5871b9598c8SLuis R. Rodriguez /* 5881b9598c8SLuis R. Rodriguez * Note: If you add another clause to set an attribute flag, please 5891b9598c8SLuis R. Rodriguez * update attributes_mask below. 5901b9598c8SLuis R. Rodriguez */ 591db07349dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE) 5925f955f26SDarrick J. Wong stat->attributes |= STATX_ATTR_IMMUTABLE; 593db07349dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_APPEND) 5945f955f26SDarrick J. Wong stat->attributes |= STATX_ATTR_APPEND; 595db07349dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_NODUMP) 5965f955f26SDarrick J. Wong stat->attributes |= STATX_ATTR_NODUMP; 597c59d87c4SChristoph Hellwig 5981b9598c8SLuis R. Rodriguez stat->attributes_mask |= (STATX_ATTR_IMMUTABLE | 5991b9598c8SLuis R. Rodriguez STATX_ATTR_APPEND | 6001b9598c8SLuis R. Rodriguez STATX_ATTR_NODUMP); 6011b9598c8SLuis R. Rodriguez 602c59d87c4SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 603c59d87c4SChristoph Hellwig case S_IFBLK: 604c59d87c4SChristoph Hellwig case S_IFCHR: 605c59d87c4SChristoph Hellwig stat->blksize = BLKDEV_IOSIZE; 60666f36464SChristoph Hellwig stat->rdev = inode->i_rdev; 607c59d87c4SChristoph Hellwig break; 60861a223dfSEric Biggers case S_IFREG: 60961a223dfSEric Biggers if (request_mask & STATX_DIOALIGN) { 61061a223dfSEric Biggers struct xfs_buftarg *target = xfs_inode_buftarg(ip); 61161a223dfSEric Biggers struct block_device *bdev = target->bt_bdev; 61261a223dfSEric Biggers 61361a223dfSEric Biggers stat->result_mask |= STATX_DIOALIGN; 61461a223dfSEric Biggers stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; 61561a223dfSEric Biggers stat->dio_offset_align = bdev_logical_block_size(bdev); 61661a223dfSEric Biggers } 61761a223dfSEric Biggers fallthrough; 618c59d87c4SChristoph Hellwig default: 619dd2d535eSChristoph Hellwig stat->blksize = xfs_stat_blksize(ip); 620c59d87c4SChristoph Hellwig stat->rdev = 0; 621c59d87c4SChristoph Hellwig break; 622c59d87c4SChristoph Hellwig } 623c59d87c4SChristoph Hellwig 624c59d87c4SChristoph Hellwig return 0; 625c59d87c4SChristoph Hellwig } 626c59d87c4SChristoph Hellwig 62769bca807SJan Kara static int 62869bca807SJan Kara xfs_vn_change_ok( 629c1632a0fSChristian Brauner struct mnt_idmap *idmap, 63069bca807SJan Kara struct dentry *dentry, 63169bca807SJan Kara struct iattr *iattr) 63269bca807SJan Kara { 63331051c85SJan Kara struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount; 63469bca807SJan Kara 6352e973b2cSDave Chinner if (xfs_is_readonly(mp)) 63669bca807SJan Kara return -EROFS; 63769bca807SJan Kara 63875c8c50fSDave Chinner if (xfs_is_shutdown(mp)) 63969bca807SJan Kara return -EIO; 64069bca807SJan Kara 641c1632a0fSChristian Brauner return setattr_prepare(idmap, dentry, iattr); 64269bca807SJan Kara } 64369bca807SJan Kara 64469bca807SJan Kara /* 64569bca807SJan Kara * Set non-size attributes of an inode. 64669bca807SJan Kara * 64769bca807SJan Kara * Caution: The caller of this function is responsible for calling 64831051c85SJan Kara * setattr_prepare() or otherwise verifying the change is fine. 64969bca807SJan Kara */ 6505d24ec4cSChristoph Hellwig static int 651c59d87c4SChristoph Hellwig xfs_setattr_nonsize( 652c1632a0fSChristian Brauner struct mnt_idmap *idmap, 653138060baSChristian Brauner struct dentry *dentry, 654c59d87c4SChristoph Hellwig struct xfs_inode *ip, 6555d24ec4cSChristoph Hellwig struct iattr *iattr) 656c59d87c4SChristoph Hellwig { 657c59d87c4SChristoph Hellwig xfs_mount_t *mp = ip->i_mount; 658c59d87c4SChristoph Hellwig struct inode *inode = VFS_I(ip); 659c59d87c4SChristoph Hellwig int mask = iattr->ia_valid; 660c59d87c4SChristoph Hellwig xfs_trans_t *tp; 661c59d87c4SChristoph Hellwig int error; 662dd3b015dSDarrick J. Wong kuid_t uid = GLOBAL_ROOT_UID; 663dd3b015dSDarrick J. Wong kgid_t gid = GLOBAL_ROOT_GID; 664c59d87c4SChristoph Hellwig struct xfs_dquot *udqp = NULL, *gdqp = NULL; 665dd3b015dSDarrick J. Wong struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL; 666c59d87c4SChristoph Hellwig 667c59d87c4SChristoph Hellwig ASSERT((mask & ATTR_SIZE) == 0); 668c59d87c4SChristoph Hellwig 669c59d87c4SChristoph Hellwig /* 670c59d87c4SChristoph Hellwig * If disk quotas is on, we make sure that the dquots do exist on disk, 671c59d87c4SChristoph Hellwig * before we start any other transactions. Trying to do this later 672c59d87c4SChristoph Hellwig * is messy. We don't care to take a readlock to look at the ids 673c59d87c4SChristoph Hellwig * in inode here, because we can't hold it across the trans_reserve. 674c59d87c4SChristoph Hellwig * If the IDs do change before we take the ilock, we're covered 675c59d87c4SChristoph Hellwig * because the i_*dquot fields will get updated anyway. 676c59d87c4SChristoph Hellwig */ 677c59d87c4SChristoph Hellwig if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) { 678c59d87c4SChristoph Hellwig uint qflags = 0; 679c59d87c4SChristoph Hellwig 680c59d87c4SChristoph Hellwig if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) { 6814d7ca409SChristian Brauner uid = from_vfsuid(idmap, i_user_ns(inode), 682b27c82e1SChristian Brauner iattr->ia_vfsuid); 683c59d87c4SChristoph Hellwig qflags |= XFS_QMOPT_UQUOTA; 684c59d87c4SChristoph Hellwig } else { 6857aab1b28SDwight Engen uid = inode->i_uid; 686c59d87c4SChristoph Hellwig } 687c59d87c4SChristoph Hellwig if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) { 6884d7ca409SChristian Brauner gid = from_vfsgid(idmap, i_user_ns(inode), 689b27c82e1SChristian Brauner iattr->ia_vfsgid); 690c59d87c4SChristoph Hellwig qflags |= XFS_QMOPT_GQUOTA; 691c59d87c4SChristoph Hellwig } else { 6927aab1b28SDwight Engen gid = inode->i_gid; 693c59d87c4SChristoph Hellwig } 694c59d87c4SChristoph Hellwig 695c59d87c4SChristoph Hellwig /* 696c59d87c4SChristoph Hellwig * We take a reference when we initialize udqp and gdqp, 697c59d87c4SChristoph Hellwig * so it is important that we never blindly double trip on 698c59d87c4SChristoph Hellwig * the same variable. See xfs_create() for an example. 699c59d87c4SChristoph Hellwig */ 700c59d87c4SChristoph Hellwig ASSERT(udqp == NULL); 701c59d87c4SChristoph Hellwig ASSERT(gdqp == NULL); 702ceaf603cSChristoph Hellwig error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid, 70392f8ff73SChandra Seetharaman qflags, &udqp, &gdqp, NULL); 704c59d87c4SChristoph Hellwig if (error) 705c59d87c4SChristoph Hellwig return error; 706c59d87c4SChristoph Hellwig } 707c59d87c4SChristoph Hellwig 7087317a03dSDarrick J. Wong error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL, 709eba0549bSDarrick J. Wong has_capability_noaudit(current, CAP_FOWNER), &tp); 710c59d87c4SChristoph Hellwig if (error) 711253f4911SChristoph Hellwig goto out_dqrele; 712c59d87c4SChristoph Hellwig 713c59d87c4SChristoph Hellwig /* 714dd3b015dSDarrick J. Wong * Register quota modifications in the transaction. Must be the owner 715dd3b015dSDarrick J. Wong * or privileged. These IDs could have changed since we last looked at 716dd3b015dSDarrick J. Wong * them. But, we're assured that if the ownership did change while we 717dd3b015dSDarrick J. Wong * didn't have the inode locked, inode's dquot(s) would have changed 718dd3b015dSDarrick J. Wong * also. 719c59d87c4SChristoph Hellwig */ 72035faf310SChristian Brauner if (XFS_IS_UQUOTA_ON(mp) && 7210dbe12f2SChristian Brauner i_uid_needs_update(idmap, iattr, inode)) { 722c59d87c4SChristoph Hellwig ASSERT(udqp); 723dd3b015dSDarrick J. Wong old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp); 724c59d87c4SChristoph Hellwig } 72535faf310SChristian Brauner if (XFS_IS_GQUOTA_ON(mp) && 7260dbe12f2SChristian Brauner i_gid_needs_update(idmap, iattr, inode)) { 727dd3b015dSDarrick J. Wong ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp)); 728c59d87c4SChristoph Hellwig ASSERT(gdqp); 729dd3b015dSDarrick J. Wong old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp); 730c59d87c4SChristoph Hellwig } 731c59d87c4SChristoph Hellwig 732c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr); 733c59d87c4SChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 734c59d87c4SChristoph Hellwig 735ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_ig_attrchg); 736c59d87c4SChristoph Hellwig 7370560f31aSDave Chinner if (xfs_has_wsync(mp)) 738c59d87c4SChristoph Hellwig xfs_trans_set_sync(tp); 73970393313SChristoph Hellwig error = xfs_trans_commit(tp); 740c59d87c4SChristoph Hellwig 741c59d87c4SChristoph Hellwig /* 742c59d87c4SChristoph Hellwig * Release any dquot(s) the inode had kept before chown. 743c59d87c4SChristoph Hellwig */ 744dd3b015dSDarrick J. Wong xfs_qm_dqrele(old_udqp); 745dd3b015dSDarrick J. Wong xfs_qm_dqrele(old_gdqp); 746c59d87c4SChristoph Hellwig xfs_qm_dqrele(udqp); 747c59d87c4SChristoph Hellwig xfs_qm_dqrele(gdqp); 748c59d87c4SChristoph Hellwig 749c59d87c4SChristoph Hellwig if (error) 750b474c7aeSEric Sandeen return error; 751c59d87c4SChristoph Hellwig 752c59d87c4SChristoph Hellwig /* 753c59d87c4SChristoph Hellwig * XXX(hch): Updating the ACL entries is not atomic vs the i_mode 754c59d87c4SChristoph Hellwig * update. We could avoid this with linked transactions 755c59d87c4SChristoph Hellwig * and passing down the transaction pointer all the way 756c59d87c4SChristoph Hellwig * to attr_set. No previous user of the generic 757c59d87c4SChristoph Hellwig * Posix ACL code seems to care about this issue either. 758c59d87c4SChristoph Hellwig */ 7595d24ec4cSChristoph Hellwig if (mask & ATTR_MODE) { 76013e83a49SChristian Brauner error = posix_acl_chmod(idmap, dentry, inode->i_mode); 761c59d87c4SChristoph Hellwig if (error) 762b474c7aeSEric Sandeen return error; 763c59d87c4SChristoph Hellwig } 764c59d87c4SChristoph Hellwig 765c59d87c4SChristoph Hellwig return 0; 766c59d87c4SChristoph Hellwig 767253f4911SChristoph Hellwig out_dqrele: 768c59d87c4SChristoph Hellwig xfs_qm_dqrele(udqp); 769c59d87c4SChristoph Hellwig xfs_qm_dqrele(gdqp); 770c59d87c4SChristoph Hellwig return error; 771c59d87c4SChristoph Hellwig } 772c59d87c4SChristoph Hellwig 773c59d87c4SChristoph Hellwig /* 774c59d87c4SChristoph Hellwig * Truncate file. Must have write permission and not be a directory. 77569bca807SJan Kara * 77669bca807SJan Kara * Caution: The caller of this function is responsible for calling 77731051c85SJan Kara * setattr_prepare() or otherwise verifying the change is fine. 778c59d87c4SChristoph Hellwig */ 7797bf7a193SDarrick J. Wong STATIC int 780c59d87c4SChristoph Hellwig xfs_setattr_size( 781c1632a0fSChristian Brauner struct mnt_idmap *idmap, 782138060baSChristian Brauner struct dentry *dentry, 783c59d87c4SChristoph Hellwig struct xfs_inode *ip, 78476ca4c23SChristoph Hellwig struct iattr *iattr) 785c59d87c4SChristoph Hellwig { 786c59d87c4SChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 787c59d87c4SChristoph Hellwig struct inode *inode = VFS_I(ip); 788673e8e59SChristoph Hellwig xfs_off_t oldsize, newsize; 789c59d87c4SChristoph Hellwig struct xfs_trans *tp; 790c59d87c4SChristoph Hellwig int error; 791f38996f5SChristoph Hellwig uint lock_flags = 0; 7925885ebdaSDave Chinner bool did_zeroing = false; 793c59d87c4SChristoph Hellwig 79476ca4c23SChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); 795e8e9ad42SDave Chinner ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL)); 796c19b3b05SDave Chinner ASSERT(S_ISREG(inode->i_mode)); 797fe60a8a0SChristoph Hellwig ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET| 79888a9e03bSYumei Huang ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0); 799c59d87c4SChristoph Hellwig 800ce7ae151SChristoph Hellwig oldsize = inode->i_size; 801673e8e59SChristoph Hellwig newsize = iattr->ia_size; 802673e8e59SChristoph Hellwig 803c59d87c4SChristoph Hellwig /* 804c59d87c4SChristoph Hellwig * Short circuit the truncate case for zero length files. 805c59d87c4SChristoph Hellwig */ 806daf83964SChristoph Hellwig if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) { 807fe60a8a0SChristoph Hellwig if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME))) 80876ca4c23SChristoph Hellwig return 0; 809c59d87c4SChristoph Hellwig 810c59d87c4SChristoph Hellwig /* 811c59d87c4SChristoph Hellwig * Use the regular setattr path to update the timestamps. 812c59d87c4SChristoph Hellwig */ 813c59d87c4SChristoph Hellwig iattr->ia_valid &= ~ATTR_SIZE; 814c1632a0fSChristian Brauner return xfs_setattr_nonsize(idmap, dentry, ip, iattr); 815c59d87c4SChristoph Hellwig } 816c59d87c4SChristoph Hellwig 817c59d87c4SChristoph Hellwig /* 818c59d87c4SChristoph Hellwig * Make sure that the dquots are attached to the inode. 819c59d87c4SChristoph Hellwig */ 820c14cfccaSDarrick J. Wong error = xfs_qm_dqattach(ip); 821c59d87c4SChristoph Hellwig if (error) 82276ca4c23SChristoph Hellwig return error; 823c59d87c4SChristoph Hellwig 824c59d87c4SChristoph Hellwig /* 825f0c6bcbaSChristoph Hellwig * Wait for all direct I/O to complete. 826f0c6bcbaSChristoph Hellwig */ 827f0c6bcbaSChristoph Hellwig inode_dio_wait(inode); 828f0c6bcbaSChristoph Hellwig 829f0c6bcbaSChristoph Hellwig /* 8305885ebdaSDave Chinner * File data changes must be complete before we start the transaction to 8315885ebdaSDave Chinner * modify the inode. This needs to be done before joining the inode to 8325885ebdaSDave Chinner * the transaction because the inode cannot be unlocked once it is a 8335885ebdaSDave Chinner * part of the transaction. 8345885ebdaSDave Chinner * 835f0c6bcbaSChristoph Hellwig * Start with zeroing any data beyond EOF that we may expose on file 836f0c6bcbaSChristoph Hellwig * extension, or zeroing out the rest of the block on a downward 837f0c6bcbaSChristoph Hellwig * truncate. 838c59d87c4SChristoph Hellwig */ 839673e8e59SChristoph Hellwig if (newsize > oldsize) { 840f5c54717SChristoph Hellwig trace_xfs_zero_eof(ip, oldsize, newsize - oldsize); 841f1ba5fafSShiyang Ruan error = xfs_zero_range(ip, oldsize, newsize - oldsize, 842f1ba5fafSShiyang Ruan &did_zeroing); 843f0c6bcbaSChristoph Hellwig } else { 844869ae85dSBrian Foster /* 845869ae85dSBrian Foster * iomap won't detect a dirty page over an unwritten block (or a 846869ae85dSBrian Foster * cow block over a hole) and subsequently skips zeroing the 847869ae85dSBrian Foster * newly post-EOF portion of the page. Flush the new EOF to 848869ae85dSBrian Foster * convert the block before the pagecache truncate. 849869ae85dSBrian Foster */ 850869ae85dSBrian Foster error = filemap_write_and_wait_range(inode->i_mapping, newsize, 851869ae85dSBrian Foster newsize); 852869ae85dSBrian Foster if (error) 853869ae85dSBrian Foster return error; 854f1ba5fafSShiyang Ruan error = xfs_truncate_page(ip, newsize, &did_zeroing); 855f0c6bcbaSChristoph Hellwig } 856f0c6bcbaSChristoph Hellwig 857c59d87c4SChristoph Hellwig if (error) 85876ca4c23SChristoph Hellwig return error; 859c59d87c4SChristoph Hellwig 860c59d87c4SChristoph Hellwig /* 8610f9160b4SDave Chinner * We've already locked out new page faults, so now we can safely remove 8620f9160b4SDave Chinner * pages from the page cache knowing they won't get refaulted until we 8630f9160b4SDave Chinner * drop the XFS_MMAP_EXCL lock after the extent manipulations are 8640f9160b4SDave Chinner * complete. The truncate_setsize() call also cleans partial EOF page 8650f9160b4SDave Chinner * PTEs on extending truncates and hence ensures sub-page block size 8660f9160b4SDave Chinner * filesystems are correctly handled, too. 86749abc3a8SDave Chinner * 8680f9160b4SDave Chinner * We have to do all the page cache truncate work outside the 8690f9160b4SDave Chinner * transaction context as the "lock" order is page lock->log space 8700f9160b4SDave Chinner * reservation as defined by extent allocation in the writeback path. 871253f4911SChristoph Hellwig * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but 8720f9160b4SDave Chinner * having already truncated the in-memory version of the file (i.e. made 8730f9160b4SDave Chinner * user visible changes). There's not much we can do about this, except 8740f9160b4SDave Chinner * to hope that the caller sees ENOMEM and retries the truncate 8750f9160b4SDave Chinner * operation. 876350976aeSEryu Guan * 877350976aeSEryu Guan * And we update in-core i_size and truncate page cache beyond newsize 87813d2c10bSChristoph Hellwig * before writeback the [i_disk_size, newsize] range, so we're 87913d2c10bSChristoph Hellwig * guaranteed not to write stale data past the new EOF on truncate down. 88049abc3a8SDave Chinner */ 88149abc3a8SDave Chinner truncate_setsize(inode, newsize); 882c59d87c4SChristoph Hellwig 883350976aeSEryu Guan /* 884350976aeSEryu Guan * We are going to log the inode size change in this transaction so 885350976aeSEryu Guan * any previous writes that are beyond the on disk EOF and the new 886350976aeSEryu Guan * EOF that have not been written out need to be written here. If we 887350976aeSEryu Guan * do not write the data out, we expose ourselves to the null files 888350976aeSEryu Guan * problem. Note that this includes any block zeroing we did above; 889350976aeSEryu Guan * otherwise those blocks may not be zeroed after a crash. 890350976aeSEryu Guan */ 891350976aeSEryu Guan if (did_zeroing || 89213d2c10bSChristoph Hellwig (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) { 893350976aeSEryu Guan error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, 89413d2c10bSChristoph Hellwig ip->i_disk_size, newsize - 1); 895350976aeSEryu Guan if (error) 896350976aeSEryu Guan return error; 897350976aeSEryu Guan } 898350976aeSEryu Guan 899253f4911SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); 900c59d87c4SChristoph Hellwig if (error) 901253f4911SChristoph Hellwig return error; 902c59d87c4SChristoph Hellwig 903c59d87c4SChristoph Hellwig lock_flags |= XFS_ILOCK_EXCL; 904c59d87c4SChristoph Hellwig xfs_ilock(ip, XFS_ILOCK_EXCL); 905ddc3415aSChristoph Hellwig xfs_trans_ijoin(tp, ip, 0); 906c59d87c4SChristoph Hellwig 907c59d87c4SChristoph Hellwig /* 908c59d87c4SChristoph Hellwig * Only change the c/mtime if we are changing the size or we are 909c59d87c4SChristoph Hellwig * explicitly asked to change it. This handles the semantic difference 910c59d87c4SChristoph Hellwig * between truncate() and ftruncate() as implemented in the VFS. 911c59d87c4SChristoph Hellwig * 912c59d87c4SChristoph Hellwig * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a 913c59d87c4SChristoph Hellwig * special case where we need to update the times despite not having 914c59d87c4SChristoph Hellwig * these flags set. For all other operations the VFS set these flags 915c59d87c4SChristoph Hellwig * explicitly if it wants a timestamp update. 916c59d87c4SChristoph Hellwig */ 917fe60a8a0SChristoph Hellwig if (newsize != oldsize && 918fe60a8a0SChristoph Hellwig !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) { 919c59d87c4SChristoph Hellwig iattr->ia_ctime = iattr->ia_mtime = 920c2050a45SDeepa Dinamani current_time(inode); 921fe60a8a0SChristoph Hellwig iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME; 922c59d87c4SChristoph Hellwig } 923c59d87c4SChristoph Hellwig 924673e8e59SChristoph Hellwig /* 925673e8e59SChristoph Hellwig * The first thing we do is set the size to new_size permanently on 926673e8e59SChristoph Hellwig * disk. This way we don't have to worry about anyone ever being able 927673e8e59SChristoph Hellwig * to look at the data being freed even in the face of a crash. 928673e8e59SChristoph Hellwig * What we're getting around here is the case where we free a block, it 929673e8e59SChristoph Hellwig * is allocated to another file, it is written to, and then we crash. 930673e8e59SChristoph Hellwig * If the new data gets written to the file but the log buffers 931673e8e59SChristoph Hellwig * containing the free and reallocation don't, then we'd end up with 932673e8e59SChristoph Hellwig * garbage in the blocks being freed. As long as we make the new size 933673e8e59SChristoph Hellwig * permanent before actually freeing any blocks it doesn't matter if 934673e8e59SChristoph Hellwig * they get written to. 935673e8e59SChristoph Hellwig */ 93613d2c10bSChristoph Hellwig ip->i_disk_size = newsize; 937673e8e59SChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 938673e8e59SChristoph Hellwig 939673e8e59SChristoph Hellwig if (newsize <= oldsize) { 940673e8e59SChristoph Hellwig error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize); 941c59d87c4SChristoph Hellwig if (error) 9424906e215SChristoph Hellwig goto out_trans_cancel; 943c59d87c4SChristoph Hellwig 944c59d87c4SChristoph Hellwig /* 945c59d87c4SChristoph Hellwig * Truncated "down", so we're removing references to old data 946c59d87c4SChristoph Hellwig * here - if we delay flushing for a long time, we expose 947c59d87c4SChristoph Hellwig * ourselves unduly to the notorious NULL files problem. So, 948c59d87c4SChristoph Hellwig * we mark this inode and flush it when the file is closed, 949c59d87c4SChristoph Hellwig * and do not wait the usual (long) time for writeout. 950c59d87c4SChristoph Hellwig */ 951c59d87c4SChristoph Hellwig xfs_iflags_set(ip, XFS_ITRUNCATED); 95227b52867SBrian Foster 95327b52867SBrian Foster /* A truncate down always removes post-EOF blocks. */ 95427b52867SBrian Foster xfs_inode_clear_eofblocks_tag(ip); 955c59d87c4SChristoph Hellwig } 956c59d87c4SChristoph Hellwig 957e014f37dSDarrick J. Wong ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID))); 958c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr); 959c59d87c4SChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 960c59d87c4SChristoph Hellwig 961ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_ig_attrchg); 962c59d87c4SChristoph Hellwig 9630560f31aSDave Chinner if (xfs_has_wsync(mp)) 964c59d87c4SChristoph Hellwig xfs_trans_set_sync(tp); 965c59d87c4SChristoph Hellwig 96670393313SChristoph Hellwig error = xfs_trans_commit(tp); 967c59d87c4SChristoph Hellwig out_unlock: 968c59d87c4SChristoph Hellwig if (lock_flags) 969c59d87c4SChristoph Hellwig xfs_iunlock(ip, lock_flags); 970c59d87c4SChristoph Hellwig return error; 971c59d87c4SChristoph Hellwig 972c59d87c4SChristoph Hellwig out_trans_cancel: 9734906e215SChristoph Hellwig xfs_trans_cancel(tp); 974c59d87c4SChristoph Hellwig goto out_unlock; 975c59d87c4SChristoph Hellwig } 976c59d87c4SChristoph Hellwig 97769bca807SJan Kara int 97869bca807SJan Kara xfs_vn_setattr_size( 979c1632a0fSChristian Brauner struct mnt_idmap *idmap, 980c59d87c4SChristoph Hellwig struct dentry *dentry, 981c59d87c4SChristoph Hellwig struct iattr *iattr) 982c59d87c4SChristoph Hellwig { 9832b0143b5SDavid Howells struct xfs_inode *ip = XFS_I(d_inode(dentry)); 98476ca4c23SChristoph Hellwig int error; 98576ca4c23SChristoph Hellwig 98669bca807SJan Kara trace_xfs_setattr(ip); 98769bca807SJan Kara 988c1632a0fSChristian Brauner error = xfs_vn_change_ok(idmap, dentry, iattr); 98969bca807SJan Kara if (error) 99069bca807SJan Kara return error; 991c1632a0fSChristian Brauner return xfs_setattr_size(idmap, dentry, ip, iattr); 99269bca807SJan Kara } 99369bca807SJan Kara 99469bca807SJan Kara STATIC int 99569bca807SJan Kara xfs_vn_setattr( 996c1632a0fSChristian Brauner struct mnt_idmap *idmap, 99769bca807SJan Kara struct dentry *dentry, 99869bca807SJan Kara struct iattr *iattr) 99969bca807SJan Kara { 100026f88363SChristoph Hellwig struct inode *inode = d_inode(dentry); 100126f88363SChristoph Hellwig struct xfs_inode *ip = XFS_I(inode); 100269bca807SJan Kara int error; 100369bca807SJan Kara 100476ca4c23SChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) { 1005c63a8eaeSDan Williams uint iolock; 1006e8e9ad42SDave Chinner 100765523218SChristoph Hellwig xfs_ilock(ip, XFS_MMAPLOCK_EXCL); 1008c63a8eaeSDan Williams iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL; 1009c63a8eaeSDan Williams 101069eb5fa1SDan Williams error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP); 1011c63a8eaeSDan Williams if (error) { 1012c63a8eaeSDan Williams xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); 1013c63a8eaeSDan Williams return error; 1014c63a8eaeSDan Williams } 1015c63a8eaeSDan Williams 1016c1632a0fSChristian Brauner error = xfs_vn_setattr_size(idmap, dentry, iattr); 101765523218SChristoph Hellwig xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); 101876ca4c23SChristoph Hellwig } else { 101926f88363SChristoph Hellwig trace_xfs_setattr(ip); 102026f88363SChristoph Hellwig 1021c1632a0fSChristian Brauner error = xfs_vn_change_ok(idmap, dentry, iattr); 102226f88363SChristoph Hellwig if (!error) 1023c1632a0fSChristian Brauner error = xfs_setattr_nonsize(idmap, dentry, ip, iattr); 102476ca4c23SChristoph Hellwig } 102576ca4c23SChristoph Hellwig 10262451337dSDave Chinner return error; 1027c59d87c4SChristoph Hellwig } 1028c59d87c4SChristoph Hellwig 102969ff2826SChristoph Hellwig STATIC int 103069ff2826SChristoph Hellwig xfs_vn_update_time( 103169ff2826SChristoph Hellwig struct inode *inode, 1032*51b0f3ebSJeff Layton struct timespec64 *time, 103369ff2826SChristoph Hellwig int flags) 103469ff2826SChristoph Hellwig { 103569ff2826SChristoph Hellwig struct xfs_inode *ip = XFS_I(inode); 103669ff2826SChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 1037c3b1b131SChristoph Hellwig int log_flags = XFS_ILOG_TIMESTAMP; 103869ff2826SChristoph Hellwig struct xfs_trans *tp; 103969ff2826SChristoph Hellwig int error; 1040*51b0f3ebSJeff Layton struct timespec64 now; 104169ff2826SChristoph Hellwig 104269ff2826SChristoph Hellwig trace_xfs_update_time(ip); 104369ff2826SChristoph Hellwig 1044c3b1b131SChristoph Hellwig if (inode->i_sb->s_flags & SB_LAZYTIME) { 1045c3b1b131SChristoph Hellwig if (!((flags & S_VERSION) && 1046541d4c79SJeff Layton inode_maybe_inc_iversion(inode, false))) { 1047541d4c79SJeff Layton generic_update_time(inode, flags); 1048541d4c79SJeff Layton return 0; 1049541d4c79SJeff Layton } 1050c3b1b131SChristoph Hellwig 1051c3b1b131SChristoph Hellwig /* Capture the iversion update that just occurred */ 1052c3b1b131SChristoph Hellwig log_flags |= XFS_ILOG_CORE; 1053c3b1b131SChristoph Hellwig } 1054c3b1b131SChristoph Hellwig 1055253f4911SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); 1056253f4911SChristoph Hellwig if (error) 10572451337dSDave Chinner return error; 105869ff2826SChristoph Hellwig 105969ff2826SChristoph Hellwig xfs_ilock(ip, XFS_ILOCK_EXCL); 1060*51b0f3ebSJeff Layton if (flags & (S_CTIME|S_MTIME)) 1061*51b0f3ebSJeff Layton now = inode_set_ctime_current(inode); 1062*51b0f3ebSJeff Layton else 1063*51b0f3ebSJeff Layton now = current_time(inode); 1064*51b0f3ebSJeff Layton 10653987848cSDave Chinner if (flags & S_MTIME) 1066*51b0f3ebSJeff Layton inode->i_mtime = now; 10673987848cSDave Chinner if (flags & S_ATIME) 1068*51b0f3ebSJeff Layton inode->i_atime = now; 10693987848cSDave Chinner 107069ff2826SChristoph Hellwig xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 1071c3b1b131SChristoph Hellwig xfs_trans_log_inode(tp, ip, log_flags); 107270393313SChristoph Hellwig return xfs_trans_commit(tp); 107369ff2826SChristoph Hellwig } 107469ff2826SChristoph Hellwig 1075c59d87c4SChristoph Hellwig STATIC int 1076c59d87c4SChristoph Hellwig xfs_vn_fiemap( 1077c59d87c4SChristoph Hellwig struct inode *inode, 1078c59d87c4SChristoph Hellwig struct fiemap_extent_info *fieinfo, 1079c59d87c4SChristoph Hellwig u64 start, 1080c59d87c4SChristoph Hellwig u64 length) 1081c59d87c4SChristoph Hellwig { 1082c59d87c4SChristoph Hellwig int error; 1083c59d87c4SChristoph Hellwig 1084d2bb140eSChristoph Hellwig xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED); 10851d4795e7SChristoph Hellwig if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 10861d4795e7SChristoph Hellwig fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; 10871d4795e7SChristoph Hellwig error = iomap_fiemap(inode, fieinfo, start, length, 10881d4795e7SChristoph Hellwig &xfs_xattr_iomap_ops); 10891d4795e7SChristoph Hellwig } else { 10901d4795e7SChristoph Hellwig error = iomap_fiemap(inode, fieinfo, start, length, 1091690c2a38SChristoph Hellwig &xfs_read_iomap_ops); 10921d4795e7SChristoph Hellwig } 1093d2bb140eSChristoph Hellwig xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED); 1094d2bb140eSChristoph Hellwig 1095c59d87c4SChristoph Hellwig return error; 1096c59d87c4SChristoph Hellwig } 1097c59d87c4SChristoph Hellwig 109899b6436bSZhi Yong Wu STATIC int 109999b6436bSZhi Yong Wu xfs_vn_tmpfile( 1100011e2b71SChristian Brauner struct mnt_idmap *idmap, 110199b6436bSZhi Yong Wu struct inode *dir, 1102863f144fSMiklos Szeredi struct file *file, 110399b6436bSZhi Yong Wu umode_t mode) 110499b6436bSZhi Yong Wu { 1105f2d40141SChristian Brauner int err = xfs_generic_create(idmap, dir, file->f_path.dentry, mode, 0, file); 1106863f144fSMiklos Szeredi 1107863f144fSMiklos Szeredi return finish_open_simple(file, err); 110899b6436bSZhi Yong Wu } 110999b6436bSZhi Yong Wu 1110c59d87c4SChristoph Hellwig static const struct inode_operations xfs_inode_operations = { 1111cac2f8b8SChristian Brauner .get_inode_acl = xfs_get_acl, 11122401dc29SChristoph Hellwig .set_acl = xfs_set_acl, 1113c59d87c4SChristoph Hellwig .getattr = xfs_vn_getattr, 1114c59d87c4SChristoph Hellwig .setattr = xfs_vn_setattr, 1115c59d87c4SChristoph Hellwig .listxattr = xfs_vn_listxattr, 1116c59d87c4SChristoph Hellwig .fiemap = xfs_vn_fiemap, 111769ff2826SChristoph Hellwig .update_time = xfs_vn_update_time, 11189fefd5dbSMiklos Szeredi .fileattr_get = xfs_fileattr_get, 11199fefd5dbSMiklos Szeredi .fileattr_set = xfs_fileattr_set, 1120c59d87c4SChristoph Hellwig }; 1121c59d87c4SChristoph Hellwig 1122c59d87c4SChristoph Hellwig static const struct inode_operations xfs_dir_inode_operations = { 1123c59d87c4SChristoph Hellwig .create = xfs_vn_create, 1124c59d87c4SChristoph Hellwig .lookup = xfs_vn_lookup, 1125c59d87c4SChristoph Hellwig .link = xfs_vn_link, 1126c59d87c4SChristoph Hellwig .unlink = xfs_vn_unlink, 1127c59d87c4SChristoph Hellwig .symlink = xfs_vn_symlink, 1128c59d87c4SChristoph Hellwig .mkdir = xfs_vn_mkdir, 1129c59d87c4SChristoph Hellwig /* 1130c59d87c4SChristoph Hellwig * Yes, XFS uses the same method for rmdir and unlink. 1131c59d87c4SChristoph Hellwig * 1132c59d87c4SChristoph Hellwig * There are some subtile differences deeper in the code, 1133c59d87c4SChristoph Hellwig * but we use S_ISDIR to check for those. 1134c59d87c4SChristoph Hellwig */ 1135c59d87c4SChristoph Hellwig .rmdir = xfs_vn_unlink, 1136c59d87c4SChristoph Hellwig .mknod = xfs_vn_mknod, 11372773bf00SMiklos Szeredi .rename = xfs_vn_rename, 1138cac2f8b8SChristian Brauner .get_inode_acl = xfs_get_acl, 11392401dc29SChristoph Hellwig .set_acl = xfs_set_acl, 1140c59d87c4SChristoph Hellwig .getattr = xfs_vn_getattr, 1141c59d87c4SChristoph Hellwig .setattr = xfs_vn_setattr, 1142c59d87c4SChristoph Hellwig .listxattr = xfs_vn_listxattr, 114369ff2826SChristoph Hellwig .update_time = xfs_vn_update_time, 114499b6436bSZhi Yong Wu .tmpfile = xfs_vn_tmpfile, 11459fefd5dbSMiklos Szeredi .fileattr_get = xfs_fileattr_get, 11469fefd5dbSMiklos Szeredi .fileattr_set = xfs_fileattr_set, 1147c59d87c4SChristoph Hellwig }; 1148c59d87c4SChristoph Hellwig 1149c59d87c4SChristoph Hellwig static const struct inode_operations xfs_dir_ci_inode_operations = { 1150c59d87c4SChristoph Hellwig .create = xfs_vn_create, 1151c59d87c4SChristoph Hellwig .lookup = xfs_vn_ci_lookup, 1152c59d87c4SChristoph Hellwig .link = xfs_vn_link, 1153c59d87c4SChristoph Hellwig .unlink = xfs_vn_unlink, 1154c59d87c4SChristoph Hellwig .symlink = xfs_vn_symlink, 1155c59d87c4SChristoph Hellwig .mkdir = xfs_vn_mkdir, 1156c59d87c4SChristoph Hellwig /* 1157c59d87c4SChristoph Hellwig * Yes, XFS uses the same method for rmdir and unlink. 1158c59d87c4SChristoph Hellwig * 1159c59d87c4SChristoph Hellwig * There are some subtile differences deeper in the code, 1160c59d87c4SChristoph Hellwig * but we use S_ISDIR to check for those. 1161c59d87c4SChristoph Hellwig */ 1162c59d87c4SChristoph Hellwig .rmdir = xfs_vn_unlink, 1163c59d87c4SChristoph Hellwig .mknod = xfs_vn_mknod, 11642773bf00SMiklos Szeredi .rename = xfs_vn_rename, 1165cac2f8b8SChristian Brauner .get_inode_acl = xfs_get_acl, 11662401dc29SChristoph Hellwig .set_acl = xfs_set_acl, 1167c59d87c4SChristoph Hellwig .getattr = xfs_vn_getattr, 1168c59d87c4SChristoph Hellwig .setattr = xfs_vn_setattr, 1169c59d87c4SChristoph Hellwig .listxattr = xfs_vn_listxattr, 117069ff2826SChristoph Hellwig .update_time = xfs_vn_update_time, 117199b6436bSZhi Yong Wu .tmpfile = xfs_vn_tmpfile, 11729fefd5dbSMiklos Szeredi .fileattr_get = xfs_fileattr_get, 11739fefd5dbSMiklos Szeredi .fileattr_set = xfs_fileattr_set, 1174c59d87c4SChristoph Hellwig }; 1175c59d87c4SChristoph Hellwig 1176c59d87c4SChristoph Hellwig static const struct inode_operations xfs_symlink_inode_operations = { 11776b255391SAl Viro .get_link = xfs_vn_get_link, 1178c59d87c4SChristoph Hellwig .getattr = xfs_vn_getattr, 1179c59d87c4SChristoph Hellwig .setattr = xfs_vn_setattr, 1180c59d87c4SChristoph Hellwig .listxattr = xfs_vn_listxattr, 118169ff2826SChristoph Hellwig .update_time = xfs_vn_update_time, 1182c59d87c4SChristoph Hellwig }; 1183c59d87c4SChristoph Hellwig 1184ba23cba9SDarrick J. Wong /* Figure out if this file actually supports DAX. */ 1185ba23cba9SDarrick J. Wong static bool 1186ba23cba9SDarrick J. Wong xfs_inode_supports_dax( 1187ba23cba9SDarrick J. Wong struct xfs_inode *ip) 1188ba23cba9SDarrick J. Wong { 1189ba23cba9SDarrick J. Wong struct xfs_mount *mp = ip->i_mount; 1190ba23cba9SDarrick J. Wong 119132dbc565SIra Weiny /* Only supported on regular files. */ 119232dbc565SIra Weiny if (!S_ISREG(VFS_I(ip)->i_mode)) 1193ba23cba9SDarrick J. Wong return false; 1194ba23cba9SDarrick J. Wong 1195ba23cba9SDarrick J. Wong /* Block size must match page size */ 1196ba23cba9SDarrick J. Wong if (mp->m_sb.sb_blocksize != PAGE_SIZE) 1197ba23cba9SDarrick J. Wong return false; 1198ba23cba9SDarrick J. Wong 1199ba23cba9SDarrick J. Wong /* Device has to support DAX too. */ 120030fa529eSChristoph Hellwig return xfs_inode_buftarg(ip)->bt_daxdev != NULL; 1201ba23cba9SDarrick J. Wong } 1202ba23cba9SDarrick J. Wong 120332dbc565SIra Weiny static bool 120432dbc565SIra Weiny xfs_inode_should_enable_dax( 120532dbc565SIra Weiny struct xfs_inode *ip) 120632dbc565SIra Weiny { 120732dbc565SIra Weiny if (!IS_ENABLED(CONFIG_FS_DAX)) 120832dbc565SIra Weiny return false; 12090560f31aSDave Chinner if (xfs_has_dax_never(ip->i_mount)) 121032dbc565SIra Weiny return false; 121132dbc565SIra Weiny if (!xfs_inode_supports_dax(ip)) 121232dbc565SIra Weiny return false; 12130560f31aSDave Chinner if (xfs_has_dax_always(ip->i_mount)) 121432dbc565SIra Weiny return true; 12153e09ab8fSChristoph Hellwig if (ip->i_diflags2 & XFS_DIFLAG2_DAX) 121632dbc565SIra Weiny return true; 121732dbc565SIra Weiny return false; 121832dbc565SIra Weiny } 121932dbc565SIra Weiny 1220840d493dSIra Weiny void 1221c59d87c4SChristoph Hellwig xfs_diflags_to_iflags( 1222840d493dSIra Weiny struct xfs_inode *ip, 1223840d493dSIra Weiny bool init) 1224c59d87c4SChristoph Hellwig { 1225840d493dSIra Weiny struct inode *inode = VFS_I(ip); 1226840d493dSIra Weiny unsigned int xflags = xfs_ip2xflags(ip); 1227840d493dSIra Weiny unsigned int flags = 0; 1228cbe4dab1SDave Chinner 1229840d493dSIra Weiny ASSERT(!(IS_DAX(inode) && init)); 1230cbe4dab1SDave Chinner 1231840d493dSIra Weiny if (xflags & FS_XFLAG_IMMUTABLE) 1232840d493dSIra Weiny flags |= S_IMMUTABLE; 1233840d493dSIra Weiny if (xflags & FS_XFLAG_APPEND) 1234840d493dSIra Weiny flags |= S_APPEND; 1235840d493dSIra Weiny if (xflags & FS_XFLAG_SYNC) 1236840d493dSIra Weiny flags |= S_SYNC; 1237840d493dSIra Weiny if (xflags & FS_XFLAG_NOATIME) 1238840d493dSIra Weiny flags |= S_NOATIME; 1239840d493dSIra Weiny if (init && xfs_inode_should_enable_dax(ip)) 1240840d493dSIra Weiny flags |= S_DAX; 1241840d493dSIra Weiny 1242840d493dSIra Weiny /* 1243840d493dSIra Weiny * S_DAX can only be set during inode initialization and is never set by 1244840d493dSIra Weiny * the VFS, so we cannot mask off S_DAX in i_flags. 1245840d493dSIra Weiny */ 1246840d493dSIra Weiny inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME); 1247840d493dSIra Weiny inode->i_flags |= flags; 1248c59d87c4SChristoph Hellwig } 1249c59d87c4SChristoph Hellwig 1250c59d87c4SChristoph Hellwig /* 12512b3d1d41SChristoph Hellwig * Initialize the Linux inode. 1252c59d87c4SChristoph Hellwig * 125358c90473SDave Chinner * When reading existing inodes from disk this is called directly from xfs_iget, 1254132c460eSYang Xu * when creating a new inode it is called from xfs_init_new_inode after setting 1255132c460eSYang Xu * up the inode. These callers have different criteria for clearing XFS_INEW, so 1256132c460eSYang Xu * leave it up to the caller to deal with unlocking the inode appropriately. 1257c59d87c4SChristoph Hellwig */ 1258c59d87c4SChristoph Hellwig void 1259c59d87c4SChristoph Hellwig xfs_setup_inode( 1260c59d87c4SChristoph Hellwig struct xfs_inode *ip) 1261c59d87c4SChristoph Hellwig { 1262c59d87c4SChristoph Hellwig struct inode *inode = &ip->i_vnode; 1263ad22c7a0SDave Chinner gfp_t gfp_mask; 1264c59d87c4SChristoph Hellwig 1265c59d87c4SChristoph Hellwig inode->i_ino = ip->i_ino; 1266f38a032bSDave Chinner inode->i_state |= I_NEW; 1267c59d87c4SChristoph Hellwig 1268c59d87c4SChristoph Hellwig inode_sb_list_add(inode); 1269c59d87c4SChristoph Hellwig /* make the inode look hashed for the writeback code */ 12705bef9151SAl Viro inode_fake_hash(inode); 1271c59d87c4SChristoph Hellwig 127213d2c10bSChristoph Hellwig i_size_write(inode, ip->i_disk_size); 1273840d493dSIra Weiny xfs_diflags_to_iflags(ip, true); 1274c59d87c4SChristoph Hellwig 12752b3d1d41SChristoph Hellwig if (S_ISDIR(inode->i_mode)) { 1276ef215e39SDave Chinner /* 1277ef215e39SDave Chinner * We set the i_rwsem class here to avoid potential races with 1278ef215e39SDave Chinner * lockdep_annotate_inode_mutex_key() reinitialising the lock 1279ef215e39SDave Chinner * after a filehandle lookup has already found the inode in 1280ef215e39SDave Chinner * cache before it has been unlocked via unlock_new_inode(). 1281ef215e39SDave Chinner */ 1282ef215e39SDave Chinner lockdep_set_class(&inode->i_rwsem, 1283ef215e39SDave Chinner &inode->i_sb->s_type->i_mutex_dir_key); 12842b3d1d41SChristoph Hellwig lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class); 12852b3d1d41SChristoph Hellwig } else { 128693a8614eSDave Chinner lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class); 1287c59d87c4SChristoph Hellwig } 1288c59d87c4SChristoph Hellwig 1289c59d87c4SChristoph Hellwig /* 1290ad22c7a0SDave Chinner * Ensure all page cache allocations are done from GFP_NOFS context to 1291ad22c7a0SDave Chinner * prevent direct reclaim recursion back into the filesystem and blowing 1292ad22c7a0SDave Chinner * stacks or deadlocking. 1293ad22c7a0SDave Chinner */ 1294ad22c7a0SDave Chinner gfp_mask = mapping_gfp_mask(inode->i_mapping); 1295ad22c7a0SDave Chinner mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS))); 1296ad22c7a0SDave Chinner 1297ad22c7a0SDave Chinner /* 1298c59d87c4SChristoph Hellwig * If there is no attribute fork no ACL can exist on this inode, 1299c59d87c4SChristoph Hellwig * and it can't have any file capabilities attached to it either. 1300c59d87c4SChristoph Hellwig */ 1301932b42c6SDarrick J. Wong if (!xfs_inode_has_attr_fork(ip)) { 1302c59d87c4SChristoph Hellwig inode_has_no_xattr(inode); 1303c59d87c4SChristoph Hellwig cache_no_acl(inode); 1304c59d87c4SChristoph Hellwig } 1305c59d87c4SChristoph Hellwig } 13062b3d1d41SChristoph Hellwig 13072b3d1d41SChristoph Hellwig void 13082b3d1d41SChristoph Hellwig xfs_setup_iops( 13092b3d1d41SChristoph Hellwig struct xfs_inode *ip) 13102b3d1d41SChristoph Hellwig { 13112b3d1d41SChristoph Hellwig struct inode *inode = &ip->i_vnode; 13122b3d1d41SChristoph Hellwig 13132b3d1d41SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 13142b3d1d41SChristoph Hellwig case S_IFREG: 13152b3d1d41SChristoph Hellwig inode->i_op = &xfs_inode_operations; 13162b3d1d41SChristoph Hellwig inode->i_fop = &xfs_file_operations; 13176e2608dfSDan Williams if (IS_DAX(inode)) 13186e2608dfSDan Williams inode->i_mapping->a_ops = &xfs_dax_aops; 13196e2608dfSDan Williams else 13202b3d1d41SChristoph Hellwig inode->i_mapping->a_ops = &xfs_address_space_operations; 13212b3d1d41SChristoph Hellwig break; 13222b3d1d41SChristoph Hellwig case S_IFDIR: 132338c26bfdSDave Chinner if (xfs_has_asciici(XFS_M(inode->i_sb))) 13242b3d1d41SChristoph Hellwig inode->i_op = &xfs_dir_ci_inode_operations; 13252b3d1d41SChristoph Hellwig else 13262b3d1d41SChristoph Hellwig inode->i_op = &xfs_dir_inode_operations; 13272b3d1d41SChristoph Hellwig inode->i_fop = &xfs_dir_file_operations; 13282b3d1d41SChristoph Hellwig break; 13292b3d1d41SChristoph Hellwig case S_IFLNK: 13302b3d1d41SChristoph Hellwig inode->i_op = &xfs_symlink_inode_operations; 13312b3d1d41SChristoph Hellwig break; 13322b3d1d41SChristoph Hellwig default: 13332b3d1d41SChristoph Hellwig inode->i_op = &xfs_inode_operations; 13342b3d1d41SChristoph Hellwig init_special_inode(inode, inode->i_mode, inode->i_rdev); 13352b3d1d41SChristoph Hellwig break; 13362b3d1d41SChristoph Hellwig } 13372b3d1d41SChristoph Hellwig } 1338