Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 14 | #include "xfs_acl.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 15 | #include "xfs_quota.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xfs_attr.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 17 | #include "xfs_trans.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 18 | #include "xfs_trace.h" |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 19 | #include "xfs_icache.h" |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 20 | #include "xfs_symlink.h" |
Dave Chinner | 1b767ee | 2014-12-04 09:43:17 +1100 | [diff] [blame] | 21 | #include "xfs_dir2.h" |
Christoph Hellwig | 68a9f5e | 2016-06-21 09:53:44 +1000 | [diff] [blame] | 22 | #include "xfs_iomap.h" |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 23 | #include "xfs_error.h" |
Miklos Szeredi | 9fefd5d | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 24 | #include "xfs_ioctl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 26 | #include <linux/posix_acl.h> |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 27 | #include <linux/security.h> |
Christoph Hellwig | c3b1b13 | 2018-03-06 17:04:00 -0800 | [diff] [blame] | 28 | #include <linux/iversion.h> |
Christoph Hellwig | 10c5db2 | 2020-05-23 09:30:11 +0200 | [diff] [blame] | 29 | #include <linux/fiemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Dave Chinner | 93a8614 | 2014-02-27 16:51:39 +1100 | [diff] [blame] | 31 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 32 | * Directories have different lock order w.r.t. mmap_lock compared to regular |
Dave Chinner | 93a8614 | 2014-02-27 16:51:39 +1100 | [diff] [blame] | 33 | * files. This is due to readdir potentially triggering page faults on a user |
| 34 | * buffer inside filldir(), and this happens with the ilock on the directory |
| 35 | * held. For regular files, the lock order is the other way around - the |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 36 | * mmap_lock is taken during the page fault, and then we lock the ilock to do |
Dave Chinner | 93a8614 | 2014-02-27 16:51:39 +1100 | [diff] [blame] | 37 | * block mapping. Hence we need a different class for the directory ilock so |
| 38 | * that lockdep can tell them apart. |
| 39 | */ |
| 40 | static struct lock_class_key xfs_nondir_ilock_class; |
| 41 | static struct lock_class_key xfs_dir_ilock_class; |
| 42 | |
Dave Chinner | 8d2a5e6 | 2012-03-07 04:50:19 +0000 | [diff] [blame] | 43 | static int |
| 44 | xfs_initxattrs( |
| 45 | struct inode *inode, |
| 46 | const struct xattr *xattr_array, |
| 47 | void *fs_info) |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 48 | { |
Dave Chinner | 8d2a5e6 | 2012-03-07 04:50:19 +0000 | [diff] [blame] | 49 | const struct xattr *xattr; |
| 50 | struct xfs_inode *ip = XFS_I(inode); |
| 51 | int error = 0; |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 52 | |
| 53 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 54 | struct xfs_da_args args = { |
| 55 | .dp = ip, |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 56 | .attr_filter = XFS_ATTR_SECURE, |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 57 | .name = xattr->name, |
| 58 | .namelen = strlen(xattr->name), |
| 59 | .value = xattr->value, |
| 60 | .valuelen = xattr->value_len, |
| 61 | }; |
| 62 | error = xfs_attr_set(&args); |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 63 | if (error < 0) |
| 64 | break; |
| 65 | } |
| 66 | return error; |
| 67 | } |
| 68 | |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 69 | /* |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 70 | * Hook in SELinux. This is not quite correct yet, what we really need |
| 71 | * here (as we do for default ACLs) is a mechanism by which creation of |
| 72 | * these attrs can be journalled at inode creation time (along with the |
| 73 | * inode, of course, such that log replay can't cause these to be lost). |
| 74 | */ |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 75 | |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 76 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 77 | xfs_init_security( |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 78 | struct inode *inode, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 79 | struct inode *dir, |
| 80 | const struct qstr *qstr) |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 81 | { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 82 | return security_inode_init_security(inode, dir, qstr, |
Dave Chinner | a5a14de | 2014-05-15 09:22:21 +1000 | [diff] [blame] | 83 | &xfs_initxattrs, NULL); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 84 | } |
| 85 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 86 | static void |
| 87 | xfs_dentry_to_name( |
| 88 | struct xfs_name *namep, |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 89 | struct dentry *dentry) |
| 90 | { |
| 91 | namep->name = dentry->d_name.name; |
| 92 | namep->len = dentry->d_name.len; |
| 93 | namep->type = XFS_DIR3_FT_UNKNOWN; |
| 94 | } |
| 95 | |
| 96 | static int |
| 97 | xfs_dentry_mode_to_name( |
| 98 | struct xfs_name *namep, |
Dave Chinner | 0cb9776 | 2013-08-12 20:50:09 +1000 | [diff] [blame] | 99 | struct dentry *dentry, |
| 100 | int mode) |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 101 | { |
| 102 | namep->name = dentry->d_name.name; |
| 103 | namep->len = dentry->d_name.len; |
Amir Goldstein | 1fc4d33 | 2017-01-17 11:41:43 -0800 | [diff] [blame] | 104 | namep->type = xfs_mode_to_ftype(mode); |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 105 | |
| 106 | if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN)) |
| 107 | return -EFSCORRUPTED; |
| 108 | |
| 109 | return 0; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 110 | } |
| 111 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 112 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 113 | xfs_cleanup_inode( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 114 | struct inode *dir, |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 115 | struct inode *inode, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 116 | struct dentry *dentry) |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 117 | { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 118 | struct xfs_name teardown; |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 119 | |
| 120 | /* Oh, the horror. |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 121 | * If we can't add the ACL or we fail in |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 122 | * xfs_init_security we must back out. |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 123 | * ENOSPC can hit here, among other things. |
| 124 | */ |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 125 | xfs_dentry_to_name(&teardown, dentry); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 126 | |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 127 | xfs_remove(XFS_I(dir), &teardown, XFS_I(inode)); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 128 | } |
| 129 | |
Dave Chinner | e6a688c | 2021-03-22 09:52:03 -0700 | [diff] [blame] | 130 | /* |
| 131 | * Check to see if we are likely to need an extended attribute to be added to |
| 132 | * the inode we are about to allocate. This allows the attribute fork to be |
| 133 | * created during the inode allocation, reducing the number of transactions we |
| 134 | * need to do in this fast path. |
| 135 | * |
| 136 | * The security checks are optimistic, but not guaranteed. The two LSMs that |
| 137 | * require xattrs to be added here (selinux and smack) are also the only two |
| 138 | * LSMs that add a sb->s_security structure to the superblock. Hence if security |
| 139 | * is enabled and sb->s_security is set, we have a pretty good idea that we are |
| 140 | * going to be asked to add a security xattr immediately after allocating the |
| 141 | * xfs inode and instantiating the VFS inode. |
| 142 | */ |
| 143 | static inline bool |
| 144 | xfs_create_need_xattr( |
| 145 | struct inode *dir, |
| 146 | struct posix_acl *default_acl, |
| 147 | struct posix_acl *acl) |
| 148 | { |
| 149 | if (acl) |
| 150 | return true; |
| 151 | if (default_acl) |
| 152 | return true; |
| 153 | #if IS_ENABLED(CONFIG_SECURITY) |
| 154 | if (dir->i_sb->s_security) |
| 155 | return true; |
| 156 | #endif |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | STATIC int |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 162 | xfs_generic_create( |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 163 | struct user_namespace *mnt_userns, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | struct inode *dir, |
| 165 | struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 166 | umode_t mode, |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 167 | dev_t rdev, |
| 168 | bool tmpfile) /* unnamed file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 170 | struct inode *inode; |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 171 | struct xfs_inode *ip = NULL; |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 172 | struct posix_acl *default_acl, *acl; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 173 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | int error; |
| 175 | |
| 176 | /* |
| 177 | * Irix uses Missed'em'V split, but doesn't want to see |
| 178 | * the upper 5 bits of (14bit) major. |
| 179 | */ |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 180 | if (S_ISCHR(mode) || S_ISBLK(mode)) { |
| 181 | if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) |
| 182 | return -EINVAL; |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 183 | } else { |
| 184 | rdev = 0; |
| 185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 187 | error = posix_acl_create(dir, &mode, &default_acl, &acl); |
| 188 | if (error) |
| 189 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 191 | /* Verify mode is valid also for tmpfile case */ |
| 192 | error = xfs_dentry_mode_to_name(&name, dentry, mode); |
| 193 | if (unlikely(error)) |
| 194 | goto out_free_acl; |
| 195 | |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 196 | if (!tmpfile) { |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 197 | error = xfs_create(mnt_userns, XFS_I(dir), &name, mode, rdev, |
Dave Chinner | e6a688c | 2021-03-22 09:52:03 -0700 | [diff] [blame] | 198 | xfs_create_need_xattr(dir, default_acl, acl), |
| 199 | &ip); |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 200 | } else { |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 201 | error = xfs_create_tmpfile(mnt_userns, XFS_I(dir), mode, &ip); |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 202 | } |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 203 | if (unlikely(error)) |
| 204 | goto out_free_acl; |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 205 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 206 | inode = VFS_I(ip); |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 207 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 208 | error = xfs_init_security(inode, dir, &dentry->d_name); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 209 | if (unlikely(error)) |
| 210 | goto out_cleanup_inode; |
| 211 | |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 212 | #ifdef CONFIG_XFS_POSIX_ACL |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 213 | if (default_acl) { |
Jan Kara | 8ba3587 | 2017-06-26 08:48:18 -0700 | [diff] [blame] | 214 | error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 215 | if (error) |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 216 | goto out_cleanup_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 218 | if (acl) { |
Jan Kara | 8ba3587 | 2017-06-26 08:48:18 -0700 | [diff] [blame] | 219 | error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS); |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 220 | if (error) |
| 221 | goto out_cleanup_inode; |
| 222 | } |
| 223 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 225 | xfs_setup_iops(ip); |
| 226 | |
Darrick J. Wong | c4a6bf7 | 2019-02-13 11:15:17 -0800 | [diff] [blame] | 227 | if (tmpfile) { |
| 228 | /* |
| 229 | * The VFS requires that any inode fed to d_tmpfile must have |
| 230 | * nlink == 1 so that it can decrement the nlink in d_tmpfile. |
| 231 | * However, we created the temp file with nlink == 0 because |
| 232 | * we're not allowed to put an inode with nlink > 0 on the |
| 233 | * unlinked list. Therefore we have to set nlink to 1 so that |
| 234 | * d_tmpfile can immediately set it back to zero. |
| 235 | */ |
| 236 | set_nlink(inode, 1); |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 237 | d_tmpfile(dentry, inode); |
Darrick J. Wong | c4a6bf7 | 2019-02-13 11:15:17 -0800 | [diff] [blame] | 238 | } else |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 239 | d_instantiate(dentry, inode); |
| 240 | |
Dave Chinner | 58c9047 | 2015-02-23 22:38:08 +1100 | [diff] [blame] | 241 | xfs_finish_inode_setup(ip); |
| 242 | |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 243 | out_free_acl: |
Kaixu Xia | 88269b8 | 2020-12-03 16:43:19 -0800 | [diff] [blame] | 244 | posix_acl_release(default_acl); |
| 245 | posix_acl_release(acl); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 246 | return error; |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 247 | |
| 248 | out_cleanup_inode: |
Dave Chinner | 58c9047 | 2015-02-23 22:38:08 +1100 | [diff] [blame] | 249 | xfs_finish_inode_setup(ip); |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 250 | if (!tmpfile) |
| 251 | xfs_cleanup_inode(dir, inode, dentry); |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 252 | xfs_irele(ip); |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 253 | goto out_free_acl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | STATIC int |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 257 | xfs_vn_mknod( |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 258 | struct user_namespace *mnt_userns, |
| 259 | struct inode *dir, |
| 260 | struct dentry *dentry, |
| 261 | umode_t mode, |
| 262 | dev_t rdev) |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 263 | { |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 264 | return xfs_generic_create(mnt_userns, dir, dentry, mode, rdev, false); |
Brian Foster | d540e43 | 2014-05-06 07:34:28 +1000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 268 | xfs_vn_create( |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 269 | struct user_namespace *mnt_userns, |
| 270 | struct inode *dir, |
| 271 | struct dentry *dentry, |
| 272 | umode_t mode, |
| 273 | bool flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 275 | return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 279 | xfs_vn_mkdir( |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 280 | struct user_namespace *mnt_userns, |
| 281 | struct inode *dir, |
| 282 | struct dentry *dentry, |
| 283 | umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 285 | return xfs_generic_create(mnt_userns, dir, dentry, mode | S_IFDIR, 0, |
| 286 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | STATIC struct dentry * |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 290 | xfs_vn_lookup( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | struct inode *dir, |
| 292 | struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 293 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Al Viro | b113a6d | 2018-04-30 23:04:19 -0400 | [diff] [blame] | 295 | struct inode *inode; |
Christoph Hellwig | ef1f5e7 | 2008-03-06 13:46:25 +1100 | [diff] [blame] | 296 | struct xfs_inode *cip; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 297 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | int error; |
| 299 | |
| 300 | if (dentry->d_name.len >= MAXNAMELEN) |
| 301 | return ERR_PTR(-ENAMETOOLONG); |
| 302 | |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 303 | xfs_dentry_to_name(&name, dentry); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 304 | error = xfs_lookup(XFS_I(dir), &name, &cip, NULL); |
Al Viro | b113a6d | 2018-04-30 23:04:19 -0400 | [diff] [blame] | 305 | if (likely(!error)) |
| 306 | inode = VFS_I(cip); |
| 307 | else if (likely(error == -ENOENT)) |
| 308 | inode = NULL; |
| 309 | else |
| 310 | inode = ERR_PTR(error); |
| 311 | return d_splice_alias(inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 314 | STATIC struct dentry * |
| 315 | xfs_vn_ci_lookup( |
| 316 | struct inode *dir, |
| 317 | struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 318 | unsigned int flags) |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 319 | { |
| 320 | struct xfs_inode *ip; |
| 321 | struct xfs_name xname; |
| 322 | struct xfs_name ci_name; |
| 323 | struct qstr dname; |
| 324 | int error; |
| 325 | |
| 326 | if (dentry->d_name.len >= MAXNAMELEN) |
| 327 | return ERR_PTR(-ENAMETOOLONG); |
| 328 | |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 329 | xfs_dentry_to_name(&xname, dentry); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 330 | error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name); |
| 331 | if (unlikely(error)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 332 | if (unlikely(error != -ENOENT)) |
| 333 | return ERR_PTR(error); |
Barry Naujok | 866d5dc | 2008-05-22 17:21:40 +1000 | [diff] [blame] | 334 | /* |
| 335 | * call d_add(dentry, NULL) here when d_drop_negative_children |
| 336 | * is called in xfs_vn_mknod (ie. allow negative dentries |
| 337 | * with CI filesystems). |
| 338 | */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | /* if exact match, just splice and exit */ |
| 343 | if (!ci_name.name) |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 344 | return d_splice_alias(VFS_I(ip), dentry); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 345 | |
| 346 | /* else case-insensitive match... */ |
| 347 | dname.name = ci_name.name; |
| 348 | dname.len = ci_name.len; |
Christoph Hellwig | e45b590 | 2008-08-07 23:49:07 +0200 | [diff] [blame] | 349 | dentry = d_add_ci(dentry, VFS_I(ip), &dname); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 350 | kmem_free(ci_name.name); |
| 351 | return dentry; |
| 352 | } |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 355 | xfs_vn_link( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | struct dentry *old_dentry, |
| 357 | struct inode *dir, |
| 358 | struct dentry *dentry) |
| 359 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 360 | struct inode *inode = d_inode(old_dentry); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 361 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | int error; |
| 363 | |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 364 | error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode); |
| 365 | if (unlikely(error)) |
| 366 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 368 | error = xfs_link(XFS_I(dir), XFS_I(inode), &name); |
Christoph Hellwig | d9424b3 | 2008-12-03 12:20:27 +0100 | [diff] [blame] | 369 | if (unlikely(error)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 370 | return error; |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 371 | |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 372 | ihold(inode); |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 373 | d_instantiate(dentry, inode); |
| 374 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 378 | xfs_vn_unlink( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | struct inode *dir, |
| 380 | struct dentry *dentry) |
| 381 | { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 382 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | int error; |
| 384 | |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 385 | xfs_dentry_to_name(&name, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 387 | error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry))); |
Christoph Hellwig | e570070 | 2008-06-23 13:25:25 +1000 | [diff] [blame] | 388 | if (error) |
| 389 | return error; |
| 390 | |
| 391 | /* |
| 392 | * With unlink, the VFS makes the dentry "negative": no inode, |
| 393 | * but still hashed. This is incompatible with case-insensitive |
| 394 | * mode, so invalidate (unhash) the dentry in CI-mode. |
| 395 | */ |
Dave Chinner | 38c26bf | 2021-08-18 18:46:37 -0700 | [diff] [blame] | 396 | if (xfs_has_asciici(XFS_M(dir->i_sb))) |
Christoph Hellwig | e570070 | 2008-06-23 13:25:25 +1000 | [diff] [blame] | 397 | d_invalidate(dentry); |
| 398 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 402 | xfs_vn_symlink( |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 403 | struct user_namespace *mnt_userns, |
| 404 | struct inode *dir, |
| 405 | struct dentry *dentry, |
| 406 | const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 408 | struct inode *inode; |
| 409 | struct xfs_inode *cip = NULL; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 410 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | int error; |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 412 | umode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 414 | mode = S_IFLNK | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 415 | (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO); |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 416 | error = xfs_dentry_mode_to_name(&name, dentry, mode); |
| 417 | if (unlikely(error)) |
| 418 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 420 | error = xfs_symlink(mnt_userns, XFS_I(dir), &name, symname, mode, &cip); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 421 | if (unlikely(error)) |
| 422 | goto out; |
| 423 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 424 | inode = VFS_I(cip); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 425 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 426 | error = xfs_init_security(inode, dir, &dentry->d_name); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 427 | if (unlikely(error)) |
| 428 | goto out_cleanup_inode; |
| 429 | |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 430 | xfs_setup_iops(cip); |
| 431 | |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 432 | d_instantiate(dentry, inode); |
Dave Chinner | 58c9047 | 2015-02-23 22:38:08 +1100 | [diff] [blame] | 433 | xfs_finish_inode_setup(cip); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 434 | return 0; |
| 435 | |
| 436 | out_cleanup_inode: |
Dave Chinner | 58c9047 | 2015-02-23 22:38:08 +1100 | [diff] [blame] | 437 | xfs_finish_inode_setup(cip); |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 438 | xfs_cleanup_inode(dir, inode, dentry); |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 439 | xfs_irele(cip); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 440 | out: |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 441 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 445 | xfs_vn_rename( |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 446 | struct user_namespace *mnt_userns, |
| 447 | struct inode *odir, |
| 448 | struct dentry *odentry, |
| 449 | struct inode *ndir, |
| 450 | struct dentry *ndentry, |
| 451 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 453 | struct inode *new_inode = d_inode(ndentry); |
Carlos Maiolino | d31a182 | 2014-12-24 08:51:42 +1100 | [diff] [blame] | 454 | int omode = 0; |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 455 | int error; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 456 | struct xfs_name oname; |
| 457 | struct xfs_name nname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Dave Chinner | 7dcf5c3 | 2015-03-25 14:08:08 +1100 | [diff] [blame] | 459 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Carlos Maiolino | dbe1b5c | 2014-12-24 08:51:38 +1100 | [diff] [blame] | 460 | return -EINVAL; |
| 461 | |
Carlos Maiolino | d31a182 | 2014-12-24 08:51:42 +1100 | [diff] [blame] | 462 | /* if we are exchanging files, we need to set i_mode of both files */ |
| 463 | if (flags & RENAME_EXCHANGE) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 464 | omode = d_inode(ndentry)->i_mode; |
Carlos Maiolino | d31a182 | 2014-12-24 08:51:42 +1100 | [diff] [blame] | 465 | |
Amir Goldstein | fab8eef | 2017-01-17 11:41:44 -0800 | [diff] [blame] | 466 | error = xfs_dentry_mode_to_name(&oname, odentry, omode); |
| 467 | if (omode && unlikely(error)) |
| 468 | return error; |
| 469 | |
| 470 | error = xfs_dentry_mode_to_name(&nname, ndentry, |
| 471 | d_inode(odentry)->i_mode); |
| 472 | if (unlikely(error)) |
| 473 | return error; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 474 | |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 475 | return xfs_rename(mnt_userns, XFS_I(odir), &oname, |
| 476 | XFS_I(d_inode(odentry)), XFS_I(ndir), &nname, |
Carlos Maiolino | d31a182 | 2014-12-24 08:51:42 +1100 | [diff] [blame] | 477 | new_inode ? XFS_I(new_inode) : NULL, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* |
| 481 | * careful here - this function can get called recursively, so |
| 482 | * we need to be very careful about how much stack we use. |
| 483 | * uio is kmalloced for this reason... |
| 484 | */ |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 485 | STATIC const char * |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 486 | xfs_vn_get_link( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | struct dentry *dentry, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 488 | struct inode *inode, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 489 | struct delayed_call *done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | char *link; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 492 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 494 | if (!dentry) |
| 495 | return ERR_PTR(-ECHILD); |
| 496 | |
Darrick J. Wong | 6eb0b8d | 2017-07-07 08:37:26 -0700 | [diff] [blame] | 497 | link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 498 | if (!link) |
| 499 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 501 | error = xfs_readlink(XFS_I(d_inode(dentry)), link); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 502 | if (unlikely(error)) |
| 503 | goto out_kfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 505 | set_delayed_call(done, kfree_link, link); |
| 506 | return link; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 507 | |
| 508 | out_kfree: |
| 509 | kfree(link); |
| 510 | out_err: |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 511 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Christoph Hellwig | dd2d535 | 2019-10-28 08:41:43 -0700 | [diff] [blame] | 514 | static uint32_t |
| 515 | xfs_stat_blksize( |
| 516 | struct xfs_inode *ip) |
| 517 | { |
| 518 | struct xfs_mount *mp = ip->i_mount; |
| 519 | |
| 520 | /* |
| 521 | * If the file blocks are being allocated from a realtime volume, then |
| 522 | * always return the realtime extent size. |
| 523 | */ |
| 524 | if (XFS_IS_REALTIME_INODE(ip)) |
Darrick J. Wong | a7bcb14 | 2021-05-31 11:31:56 -0700 | [diff] [blame] | 525 | return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip)); |
Christoph Hellwig | dd2d535 | 2019-10-28 08:41:43 -0700 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * Allow large block sizes to be reported to userspace programs if the |
| 529 | * "largeio" mount option is used. |
| 530 | * |
| 531 | * If compatibility mode is specified, simply return the basic unit of |
| 532 | * caching so that we don't get inefficient read/modify/write I/O from |
| 533 | * user apps. Otherwise.... |
| 534 | * |
| 535 | * If the underlying volume is a stripe, then return the stripe width in |
| 536 | * bytes as the recommended I/O size. It is not a stripe and we've set a |
| 537 | * default buffered I/O size, return that, otherwise return the compat |
| 538 | * default. |
| 539 | */ |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 540 | if (xfs_has_large_iosize(mp)) { |
Christoph Hellwig | dd2d535 | 2019-10-28 08:41:43 -0700 | [diff] [blame] | 541 | if (mp->m_swidth) |
Darrick J. Wong | a7bcb14 | 2021-05-31 11:31:56 -0700 | [diff] [blame] | 542 | return XFS_FSB_TO_B(mp, mp->m_swidth); |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 543 | if (xfs_has_allocsize(mp)) |
Christoph Hellwig | 5da8a07 | 2019-10-28 08:41:44 -0700 | [diff] [blame] | 544 | return 1U << mp->m_allocsize_log; |
Christoph Hellwig | dd2d535 | 2019-10-28 08:41:43 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return PAGE_SIZE; |
| 548 | } |
| 549 | |
Christoph Hellwig | 4576758 | 2008-02-05 12:13:24 +1100 | [diff] [blame] | 550 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 551 | xfs_vn_getattr( |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 552 | struct user_namespace *mnt_userns, |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 553 | const struct path *path, |
| 554 | struct kstat *stat, |
| 555 | u32 request_mask, |
| 556 | unsigned int query_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 558 | struct inode *inode = d_inode(path->dentry); |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 559 | struct xfs_inode *ip = XFS_I(inode); |
| 560 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 562 | trace_xfs_getattr(ip); |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 563 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 564 | if (xfs_is_shutdown(mp)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 565 | return -EIO; |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 566 | |
| 567 | stat->size = XFS_ISIZE(ip); |
| 568 | stat->dev = inode->i_sb->s_dev; |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 569 | stat->mode = inode->i_mode; |
Dave Chinner | 54d7b5c | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 570 | stat->nlink = inode->i_nlink; |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 571 | stat->uid = i_uid_into_mnt(mnt_userns, inode); |
| 572 | stat->gid = i_gid_into_mnt(mnt_userns, inode); |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 573 | stat->ino = ip->i_ino; |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 574 | stat->atime = inode->i_atime; |
Christoph Hellwig | f9581b1 | 2009-10-06 20:29:26 +0000 | [diff] [blame] | 575 | stat->mtime = inode->i_mtime; |
| 576 | stat->ctime = inode->i_ctime; |
Christoph Hellwig | 6e73a54 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 577 | stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks); |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 578 | |
Dave Chinner | 38c26bf | 2021-08-18 18:46:37 -0700 | [diff] [blame] | 579 | if (xfs_has_v3inodes(mp)) { |
Darrick J. Wong | 5f955f2 | 2017-03-31 18:32:03 +0100 | [diff] [blame] | 580 | if (request_mask & STATX_BTIME) { |
| 581 | stat->result_mask |= STATX_BTIME; |
Christoph Hellwig | e98d5e8 | 2021-03-29 11:11:45 -0700 | [diff] [blame] | 582 | stat->btime = ip->i_crtime; |
Darrick J. Wong | 5f955f2 | 2017-03-31 18:32:03 +0100 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Luis R. Rodriguez | 1b9598c | 2019-03-01 08:14:57 -0800 | [diff] [blame] | 586 | /* |
| 587 | * Note: If you add another clause to set an attribute flag, please |
| 588 | * update attributes_mask below. |
| 589 | */ |
Christoph Hellwig | db07349 | 2021-03-29 11:11:44 -0700 | [diff] [blame] | 590 | if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE) |
Darrick J. Wong | 5f955f2 | 2017-03-31 18:32:03 +0100 | [diff] [blame] | 591 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
Christoph Hellwig | db07349 | 2021-03-29 11:11:44 -0700 | [diff] [blame] | 592 | if (ip->i_diflags & XFS_DIFLAG_APPEND) |
Darrick J. Wong | 5f955f2 | 2017-03-31 18:32:03 +0100 | [diff] [blame] | 593 | stat->attributes |= STATX_ATTR_APPEND; |
Christoph Hellwig | db07349 | 2021-03-29 11:11:44 -0700 | [diff] [blame] | 594 | if (ip->i_diflags & XFS_DIFLAG_NODUMP) |
Darrick J. Wong | 5f955f2 | 2017-03-31 18:32:03 +0100 | [diff] [blame] | 595 | stat->attributes |= STATX_ATTR_NODUMP; |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 596 | |
Luis R. Rodriguez | 1b9598c | 2019-03-01 08:14:57 -0800 | [diff] [blame] | 597 | stat->attributes_mask |= (STATX_ATTR_IMMUTABLE | |
| 598 | STATX_ATTR_APPEND | |
| 599 | STATX_ATTR_NODUMP); |
| 600 | |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 601 | switch (inode->i_mode & S_IFMT) { |
| 602 | case S_IFBLK: |
| 603 | case S_IFCHR: |
| 604 | stat->blksize = BLKDEV_IOSIZE; |
Christoph Hellwig | 66f3646 | 2017-10-19 11:07:09 -0700 | [diff] [blame] | 605 | stat->rdev = inode->i_rdev; |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 606 | break; |
| 607 | default: |
Christoph Hellwig | dd2d535 | 2019-10-28 08:41:43 -0700 | [diff] [blame] | 608 | stat->blksize = xfs_stat_blksize(ip); |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 609 | stat->rdev = 0; |
| 610 | break; |
Nathan Scott | 69e23b9 | 2006-09-28 11:01:22 +1000 | [diff] [blame] | 611 | } |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 612 | |
| 613 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Dave Chinner | 56c19e8 | 2013-05-27 16:38:25 +1000 | [diff] [blame] | 616 | static void |
| 617 | xfs_setattr_mode( |
Dave Chinner | 56c19e8 | 2013-05-27 16:38:25 +1000 | [diff] [blame] | 618 | struct xfs_inode *ip, |
| 619 | struct iattr *iattr) |
| 620 | { |
Christoph Hellwig | 0c3d88d | 2013-11-18 05:10:40 -0800 | [diff] [blame] | 621 | struct inode *inode = VFS_I(ip); |
| 622 | umode_t mode = iattr->ia_mode; |
Dave Chinner | 56c19e8 | 2013-05-27 16:38:25 +1000 | [diff] [blame] | 623 | |
Dave Chinner | 56c19e8 | 2013-05-27 16:38:25 +1000 | [diff] [blame] | 624 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 625 | |
Dave Chinner | 56c19e8 | 2013-05-27 16:38:25 +1000 | [diff] [blame] | 626 | inode->i_mode &= S_IFMT; |
| 627 | inode->i_mode |= mode & ~S_IFMT; |
| 628 | } |
| 629 | |
Christoph Hellwig | 5278511 | 2015-02-16 11:49:23 +1100 | [diff] [blame] | 630 | void |
Christoph Hellwig | c91c46c | 2013-11-18 05:10:52 -0800 | [diff] [blame] | 631 | xfs_setattr_time( |
| 632 | struct xfs_inode *ip, |
| 633 | struct iattr *iattr) |
| 634 | { |
| 635 | struct inode *inode = VFS_I(ip); |
| 636 | |
| 637 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 638 | |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 639 | if (iattr->ia_valid & ATTR_ATIME) |
Christoph Hellwig | c91c46c | 2013-11-18 05:10:52 -0800 | [diff] [blame] | 640 | inode->i_atime = iattr->ia_atime; |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 641 | if (iattr->ia_valid & ATTR_CTIME) |
Christoph Hellwig | c91c46c | 2013-11-18 05:10:52 -0800 | [diff] [blame] | 642 | inode->i_ctime = iattr->ia_ctime; |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 643 | if (iattr->ia_valid & ATTR_MTIME) |
Christoph Hellwig | c91c46c | 2013-11-18 05:10:52 -0800 | [diff] [blame] | 644 | inode->i_mtime = iattr->ia_mtime; |
Christoph Hellwig | c91c46c | 2013-11-18 05:10:52 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 647 | static int |
| 648 | xfs_vn_change_ok( |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 649 | struct user_namespace *mnt_userns, |
| 650 | struct dentry *dentry, |
| 651 | struct iattr *iattr) |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 652 | { |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 653 | struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount; |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 654 | |
Dave Chinner | 2e973b2 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 655 | if (xfs_is_readonly(mp)) |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 656 | return -EROFS; |
| 657 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 658 | if (xfs_is_shutdown(mp)) |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 659 | return -EIO; |
| 660 | |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 661 | return setattr_prepare(mnt_userns, dentry, iattr); |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Set non-size attributes of an inode. |
| 666 | * |
| 667 | * Caution: The caller of this function is responsible for calling |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 668 | * setattr_prepare() or otherwise verifying the change is fine. |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 669 | */ |
Christoph Hellwig | 5d24ec4c | 2020-12-10 20:00:39 -0800 | [diff] [blame] | 670 | static int |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 671 | xfs_setattr_nonsize( |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 672 | struct user_namespace *mnt_userns, |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 673 | struct xfs_inode *ip, |
Christoph Hellwig | 5d24ec4c | 2020-12-10 20:00:39 -0800 | [diff] [blame] | 674 | struct iattr *iattr) |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 675 | { |
| 676 | xfs_mount_t *mp = ip->i_mount; |
| 677 | struct inode *inode = VFS_I(ip); |
| 678 | int mask = iattr->ia_valid; |
| 679 | xfs_trans_t *tp; |
| 680 | int error; |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 681 | kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID; |
| 682 | kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 683 | struct xfs_dquot *udqp = NULL, *gdqp = NULL; |
| 684 | struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL; |
| 685 | |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 686 | ASSERT((mask & ATTR_SIZE) == 0); |
| 687 | |
| 688 | /* |
| 689 | * If disk quotas is on, we make sure that the dquots do exist on disk, |
| 690 | * before we start any other transactions. Trying to do this later |
| 691 | * is messy. We don't care to take a readlock to look at the ids |
| 692 | * in inode here, because we can't hold it across the trans_reserve. |
| 693 | * If the IDs do change before we take the ilock, we're covered |
| 694 | * because the i_*dquot fields will get updated anyway. |
| 695 | */ |
| 696 | if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) { |
| 697 | uint qflags = 0; |
| 698 | |
| 699 | if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) { |
| 700 | uid = iattr->ia_uid; |
| 701 | qflags |= XFS_QMOPT_UQUOTA; |
| 702 | } else { |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 703 | uid = inode->i_uid; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 704 | } |
| 705 | if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) { |
| 706 | gid = iattr->ia_gid; |
| 707 | qflags |= XFS_QMOPT_GQUOTA; |
| 708 | } else { |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 709 | gid = inode->i_gid; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* |
| 713 | * We take a reference when we initialize udqp and gdqp, |
| 714 | * so it is important that we never blindly double trip on |
| 715 | * the same variable. See xfs_create() for an example. |
| 716 | */ |
| 717 | ASSERT(udqp == NULL); |
| 718 | ASSERT(gdqp == NULL); |
Christoph Hellwig | ceaf603 | 2021-03-29 11:11:39 -0700 | [diff] [blame] | 719 | error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid, |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 720 | qflags, &udqp, &gdqp, NULL); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 721 | if (error) |
| 722 | return error; |
| 723 | } |
| 724 | |
Darrick J. Wong | 7317a03 | 2021-01-29 11:32:09 -0800 | [diff] [blame] | 725 | error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL, |
| 726 | capable(CAP_FOWNER), &tp); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 727 | if (error) |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 728 | goto out_dqrele; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 729 | |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 730 | /* |
| 731 | * Change file ownership. Must be the owner or privileged. |
| 732 | */ |
| 733 | if (mask & (ATTR_UID|ATTR_GID)) { |
| 734 | /* |
| 735 | * These IDs could have changed since we last looked at them. |
| 736 | * But, we're assured that if the ownership did change |
| 737 | * while we didn't have the inode locked, inode's dquot(s) |
| 738 | * would have changed also. |
| 739 | */ |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 740 | iuid = inode->i_uid; |
| 741 | igid = inode->i_gid; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 742 | gid = (mask & ATTR_GID) ? iattr->ia_gid : igid; |
| 743 | uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid; |
| 744 | |
| 745 | /* |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 746 | * CAP_FSETID overrides the following restrictions: |
| 747 | * |
| 748 | * The set-user-ID and set-group-ID bits of a file will be |
| 749 | * cleared upon successful return from chown() |
| 750 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 751 | if ((inode->i_mode & (S_ISUID|S_ISGID)) && |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 752 | !capable(CAP_FSETID)) |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 753 | inode->i_mode &= ~(S_ISUID|S_ISGID); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 754 | |
| 755 | /* |
| 756 | * Change the ownerships and register quota modifications |
| 757 | * in the transaction. |
| 758 | */ |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 759 | if (!uid_eq(iuid, uid)) { |
Christoph Hellwig | 149e53a | 2021-08-06 11:05:37 -0700 | [diff] [blame] | 760 | if (XFS_IS_UQUOTA_ON(mp)) { |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 761 | ASSERT(mask & ATTR_UID); |
| 762 | ASSERT(udqp); |
| 763 | olddquot1 = xfs_qm_vop_chown(tp, ip, |
| 764 | &ip->i_udquot, udqp); |
| 765 | } |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 766 | inode->i_uid = uid; |
| 767 | } |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 768 | if (!gid_eq(igid, gid)) { |
Christoph Hellwig | 149e53a | 2021-08-06 11:05:37 -0700 | [diff] [blame] | 769 | if (XFS_IS_GQUOTA_ON(mp)) { |
Dave Chinner | 38c26bf | 2021-08-18 18:46:37 -0700 | [diff] [blame] | 770 | ASSERT(xfs_has_pquotino(mp) || |
Jie Liu | 5a01dd5 | 2013-11-26 21:38:34 +0800 | [diff] [blame] | 771 | !XFS_IS_PQUOTA_ON(mp)); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 772 | ASSERT(mask & ATTR_GID); |
| 773 | ASSERT(gdqp); |
| 774 | olddquot2 = xfs_qm_vop_chown(tp, ip, |
| 775 | &ip->i_gdquot, gdqp); |
| 776 | } |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 777 | inode->i_gid = gid; |
| 778 | } |
| 779 | } |
| 780 | |
Dave Chinner | 56c19e8 | 2013-05-27 16:38:25 +1000 | [diff] [blame] | 781 | if (mask & ATTR_MODE) |
Christoph Hellwig | 0c3d88d | 2013-11-18 05:10:40 -0800 | [diff] [blame] | 782 | xfs_setattr_mode(ip, iattr); |
Christoph Hellwig | c91c46c | 2013-11-18 05:10:52 -0800 | [diff] [blame] | 783 | if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME)) |
| 784 | xfs_setattr_time(ip, iattr); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 785 | |
| 786 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 787 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 788 | XFS_STATS_INC(mp, xs_ig_attrchg); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 789 | |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 790 | if (xfs_has_wsync(mp)) |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 791 | xfs_trans_set_sync(tp); |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 792 | error = xfs_trans_commit(tp); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 793 | |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 794 | /* |
| 795 | * Release any dquot(s) the inode had kept before chown. |
| 796 | */ |
| 797 | xfs_qm_dqrele(olddquot1); |
| 798 | xfs_qm_dqrele(olddquot2); |
| 799 | xfs_qm_dqrele(udqp); |
| 800 | xfs_qm_dqrele(gdqp); |
| 801 | |
| 802 | if (error) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 803 | return error; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 804 | |
| 805 | /* |
| 806 | * XXX(hch): Updating the ACL entries is not atomic vs the i_mode |
| 807 | * update. We could avoid this with linked transactions |
| 808 | * and passing down the transaction pointer all the way |
| 809 | * to attr_set. No previous user of the generic |
| 810 | * Posix ACL code seems to care about this issue either. |
| 811 | */ |
Christoph Hellwig | 5d24ec4c | 2020-12-10 20:00:39 -0800 | [diff] [blame] | 812 | if (mask & ATTR_MODE) { |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 813 | error = posix_acl_chmod(mnt_userns, inode, inode->i_mode); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 814 | if (error) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 815 | return error; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | return 0; |
| 819 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 820 | out_dqrele: |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 821 | xfs_qm_dqrele(udqp); |
| 822 | xfs_qm_dqrele(gdqp); |
| 823 | return error; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Truncate file. Must have write permission and not be a directory. |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 828 | * |
| 829 | * Caution: The caller of this function is responsible for calling |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 830 | * setattr_prepare() or otherwise verifying the change is fine. |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 831 | */ |
Darrick J. Wong | 7bf7a19 | 2017-08-31 15:11:06 -0700 | [diff] [blame] | 832 | STATIC int |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 833 | xfs_setattr_size( |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 834 | struct user_namespace *mnt_userns, |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 835 | struct xfs_inode *ip, |
Christoph Hellwig | 76ca4c2 | 2013-10-14 07:09:35 -0700 | [diff] [blame] | 836 | struct iattr *iattr) |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 837 | { |
| 838 | struct xfs_mount *mp = ip->i_mount; |
| 839 | struct inode *inode = VFS_I(ip); |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 840 | xfs_off_t oldsize, newsize; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 841 | struct xfs_trans *tp; |
| 842 | int error; |
Christoph Hellwig | f38996f | 2012-03-27 10:34:48 -0400 | [diff] [blame] | 843 | uint lock_flags = 0; |
Dave Chinner | 5885ebd | 2015-02-23 22:37:08 +1100 | [diff] [blame] | 844 | bool did_zeroing = false; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 845 | |
Christoph Hellwig | 76ca4c2 | 2013-10-14 07:09:35 -0700 | [diff] [blame] | 846 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Dave Chinner | e8e9ad4 | 2015-02-23 21:45:32 +1100 | [diff] [blame] | 847 | ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL)); |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 848 | ASSERT(S_ISREG(inode->i_mode)); |
Christoph Hellwig | fe60a8a | 2014-02-10 10:35:22 +1100 | [diff] [blame] | 849 | ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET| |
Yumei Huang | 88a9e03 | 2021-01-22 16:48:19 -0800 | [diff] [blame] | 850 | ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 851 | |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 852 | oldsize = inode->i_size; |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 853 | newsize = iattr->ia_size; |
| 854 | |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 855 | /* |
| 856 | * Short circuit the truncate case for zero length files. |
| 857 | */ |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 858 | if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) { |
Christoph Hellwig | fe60a8a | 2014-02-10 10:35:22 +1100 | [diff] [blame] | 859 | if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME))) |
Christoph Hellwig | 76ca4c2 | 2013-10-14 07:09:35 -0700 | [diff] [blame] | 860 | return 0; |
Christoph Hellwig | 681b120 | 2011-07-08 14:34:26 +0200 | [diff] [blame] | 861 | |
| 862 | /* |
| 863 | * Use the regular setattr path to update the timestamps. |
| 864 | */ |
Christoph Hellwig | 681b120 | 2011-07-08 14:34:26 +0200 | [diff] [blame] | 865 | iattr->ia_valid &= ~ATTR_SIZE; |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 866 | return xfs_setattr_nonsize(mnt_userns, ip, iattr); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Make sure that the dquots are attached to the inode. |
| 871 | */ |
Darrick J. Wong | c14cfcc | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 872 | error = xfs_qm_dqattach(ip); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 873 | if (error) |
Christoph Hellwig | 76ca4c2 | 2013-10-14 07:09:35 -0700 | [diff] [blame] | 874 | return error; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 875 | |
| 876 | /* |
Christoph Hellwig | f0c6bcb | 2016-06-21 09:52:47 +1000 | [diff] [blame] | 877 | * Wait for all direct I/O to complete. |
| 878 | */ |
| 879 | inode_dio_wait(inode); |
| 880 | |
| 881 | /* |
Dave Chinner | 5885ebd | 2015-02-23 22:37:08 +1100 | [diff] [blame] | 882 | * File data changes must be complete before we start the transaction to |
| 883 | * modify the inode. This needs to be done before joining the inode to |
| 884 | * the transaction because the inode cannot be unlocked once it is a |
| 885 | * part of the transaction. |
| 886 | * |
Christoph Hellwig | f0c6bcb | 2016-06-21 09:52:47 +1000 | [diff] [blame] | 887 | * Start with zeroing any data beyond EOF that we may expose on file |
| 888 | * extension, or zeroing out the rest of the block on a downward |
| 889 | * truncate. |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 890 | */ |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 891 | if (newsize > oldsize) { |
Christoph Hellwig | f5c54717 | 2018-03-13 23:15:32 -0700 | [diff] [blame] | 892 | trace_xfs_zero_eof(ip, oldsize, newsize - oldsize); |
Shiyang Ruan | f1ba5fa | 2021-11-29 11:21:49 +0100 | [diff] [blame] | 893 | error = xfs_zero_range(ip, oldsize, newsize - oldsize, |
| 894 | &did_zeroing); |
Christoph Hellwig | f0c6bcb | 2016-06-21 09:52:47 +1000 | [diff] [blame] | 895 | } else { |
Brian Foster | 869ae85 | 2020-10-29 14:30:48 -0700 | [diff] [blame] | 896 | /* |
| 897 | * iomap won't detect a dirty page over an unwritten block (or a |
| 898 | * cow block over a hole) and subsequently skips zeroing the |
| 899 | * newly post-EOF portion of the page. Flush the new EOF to |
| 900 | * convert the block before the pagecache truncate. |
| 901 | */ |
| 902 | error = filemap_write_and_wait_range(inode->i_mapping, newsize, |
| 903 | newsize); |
| 904 | if (error) |
| 905 | return error; |
Shiyang Ruan | f1ba5fa | 2021-11-29 11:21:49 +0100 | [diff] [blame] | 906 | error = xfs_truncate_page(ip, newsize, &did_zeroing); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 907 | } |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 908 | |
Christoph Hellwig | f0c6bcb | 2016-06-21 09:52:47 +1000 | [diff] [blame] | 909 | if (error) |
| 910 | return error; |
| 911 | |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 912 | /* |
Dave Chinner | 0f9160b | 2015-02-23 21:46:58 +1100 | [diff] [blame] | 913 | * We've already locked out new page faults, so now we can safely remove |
| 914 | * pages from the page cache knowing they won't get refaulted until we |
| 915 | * drop the XFS_MMAP_EXCL lock after the extent manipulations are |
| 916 | * complete. The truncate_setsize() call also cleans partial EOF page |
| 917 | * PTEs on extending truncates and hence ensures sub-page block size |
| 918 | * filesystems are correctly handled, too. |
Dave Chinner | 49abc3a | 2014-05-07 08:05:45 +1000 | [diff] [blame] | 919 | * |
Dave Chinner | 0f9160b | 2015-02-23 21:46:58 +1100 | [diff] [blame] | 920 | * We have to do all the page cache truncate work outside the |
| 921 | * transaction context as the "lock" order is page lock->log space |
| 922 | * reservation as defined by extent allocation in the writeback path. |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 923 | * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but |
Dave Chinner | 0f9160b | 2015-02-23 21:46:58 +1100 | [diff] [blame] | 924 | * having already truncated the in-memory version of the file (i.e. made |
| 925 | * user visible changes). There's not much we can do about this, except |
| 926 | * to hope that the caller sees ENOMEM and retries the truncate |
| 927 | * operation. |
Eryu Guan | 350976ae | 2017-11-01 21:43:50 -0700 | [diff] [blame] | 928 | * |
| 929 | * And we update in-core i_size and truncate page cache beyond newsize |
Christoph Hellwig | 13d2c10 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 930 | * before writeback the [i_disk_size, newsize] range, so we're |
| 931 | * guaranteed not to write stale data past the new EOF on truncate down. |
Dave Chinner | 49abc3a | 2014-05-07 08:05:45 +1000 | [diff] [blame] | 932 | */ |
Dave Chinner | 49abc3a | 2014-05-07 08:05:45 +1000 | [diff] [blame] | 933 | truncate_setsize(inode, newsize); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 934 | |
Eryu Guan | 350976ae | 2017-11-01 21:43:50 -0700 | [diff] [blame] | 935 | /* |
| 936 | * We are going to log the inode size change in this transaction so |
| 937 | * any previous writes that are beyond the on disk EOF and the new |
| 938 | * EOF that have not been written out need to be written here. If we |
| 939 | * do not write the data out, we expose ourselves to the null files |
| 940 | * problem. Note that this includes any block zeroing we did above; |
| 941 | * otherwise those blocks may not be zeroed after a crash. |
| 942 | */ |
| 943 | if (did_zeroing || |
Christoph Hellwig | 13d2c10 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 944 | (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) { |
Eryu Guan | 350976ae | 2017-11-01 21:43:50 -0700 | [diff] [blame] | 945 | error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, |
Christoph Hellwig | 13d2c10 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 946 | ip->i_disk_size, newsize - 1); |
Eryu Guan | 350976ae | 2017-11-01 21:43:50 -0700 | [diff] [blame] | 947 | if (error) |
| 948 | return error; |
| 949 | } |
| 950 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 951 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 952 | if (error) |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 953 | return error; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 954 | |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 955 | lock_flags |= XFS_ILOCK_EXCL; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 956 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 957 | xfs_trans_ijoin(tp, ip, 0); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 958 | |
| 959 | /* |
| 960 | * Only change the c/mtime if we are changing the size or we are |
| 961 | * explicitly asked to change it. This handles the semantic difference |
| 962 | * between truncate() and ftruncate() as implemented in the VFS. |
| 963 | * |
| 964 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 965 | * special case where we need to update the times despite not having |
| 966 | * these flags set. For all other operations the VFS set these flags |
| 967 | * explicitly if it wants a timestamp update. |
| 968 | */ |
Christoph Hellwig | fe60a8a | 2014-02-10 10:35:22 +1100 | [diff] [blame] | 969 | if (newsize != oldsize && |
| 970 | !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) { |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 971 | iattr->ia_ctime = iattr->ia_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 972 | current_time(inode); |
Christoph Hellwig | fe60a8a | 2014-02-10 10:35:22 +1100 | [diff] [blame] | 973 | iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 976 | /* |
| 977 | * The first thing we do is set the size to new_size permanently on |
| 978 | * disk. This way we don't have to worry about anyone ever being able |
| 979 | * to look at the data being freed even in the face of a crash. |
| 980 | * What we're getting around here is the case where we free a block, it |
| 981 | * is allocated to another file, it is written to, and then we crash. |
| 982 | * If the new data gets written to the file but the log buffers |
| 983 | * containing the free and reallocation don't, then we'd end up with |
| 984 | * garbage in the blocks being freed. As long as we make the new size |
| 985 | * permanent before actually freeing any blocks it doesn't matter if |
| 986 | * they get written to. |
| 987 | */ |
Christoph Hellwig | 13d2c10 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 988 | ip->i_disk_size = newsize; |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 989 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 990 | |
| 991 | if (newsize <= oldsize) { |
| 992 | error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 993 | if (error) |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 994 | goto out_trans_cancel; |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 995 | |
| 996 | /* |
| 997 | * Truncated "down", so we're removing references to old data |
| 998 | * here - if we delay flushing for a long time, we expose |
| 999 | * ourselves unduly to the notorious NULL files problem. So, |
| 1000 | * we mark this inode and flush it when the file is closed, |
| 1001 | * and do not wait the usual (long) time for writeout. |
| 1002 | */ |
| 1003 | xfs_iflags_set(ip, XFS_ITRUNCATED); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1004 | |
| 1005 | /* A truncate down always removes post-EOF blocks. */ |
| 1006 | xfs_inode_clear_eofblocks_tag(ip); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Christoph Hellwig | fe60a8a | 2014-02-10 10:35:22 +1100 | [diff] [blame] | 1009 | if (iattr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 0c3d88d | 2013-11-18 05:10:40 -0800 | [diff] [blame] | 1010 | xfs_setattr_mode(ip, iattr); |
Christoph Hellwig | fe60a8a | 2014-02-10 10:35:22 +1100 | [diff] [blame] | 1011 | if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME)) |
Christoph Hellwig | c91c46c | 2013-11-18 05:10:52 -0800 | [diff] [blame] | 1012 | xfs_setattr_time(ip, iattr); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 1013 | |
| 1014 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1015 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1016 | XFS_STATS_INC(mp, xs_ig_attrchg); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 1017 | |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 1018 | if (xfs_has_wsync(mp)) |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 1019 | xfs_trans_set_sync(tp); |
| 1020 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1021 | error = xfs_trans_commit(tp); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 1022 | out_unlock: |
| 1023 | if (lock_flags) |
| 1024 | xfs_iunlock(ip, lock_flags); |
| 1025 | return error; |
| 1026 | |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 1027 | out_trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1028 | xfs_trans_cancel(tp); |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 1029 | goto out_unlock; |
| 1030 | } |
| 1031 | |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 1032 | int |
| 1033 | xfs_vn_setattr_size( |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 1034 | struct user_namespace *mnt_userns, |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 1035 | struct dentry *dentry, |
| 1036 | struct iattr *iattr) |
| 1037 | { |
| 1038 | struct xfs_inode *ip = XFS_I(d_inode(dentry)); |
| 1039 | int error; |
| 1040 | |
| 1041 | trace_xfs_setattr(ip); |
| 1042 | |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 1043 | error = xfs_vn_change_ok(mnt_userns, dentry, iattr); |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 1044 | if (error) |
| 1045 | return error; |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 1046 | return xfs_setattr_size(mnt_userns, ip, iattr); |
Jan Kara | 69bca80 | 2016-05-26 14:46:43 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1050 | xfs_vn_setattr( |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 1051 | struct user_namespace *mnt_userns, |
Christoph Hellwig | 76ca4c2 | 2013-10-14 07:09:35 -0700 | [diff] [blame] | 1052 | struct dentry *dentry, |
| 1053 | struct iattr *iattr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { |
Christoph Hellwig | 26f8836 | 2020-12-10 20:00:38 -0800 | [diff] [blame] | 1055 | struct inode *inode = d_inode(dentry); |
| 1056 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 76ca4c2 | 2013-10-14 07:09:35 -0700 | [diff] [blame] | 1057 | int error; |
| 1058 | |
| 1059 | if (iattr->ia_valid & ATTR_SIZE) { |
Dan Williams | c63a8ea | 2018-03-12 14:12:29 -0700 | [diff] [blame] | 1060 | uint iolock; |
Dave Chinner | e8e9ad4 | 2015-02-23 21:45:32 +1100 | [diff] [blame] | 1061 | |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1062 | xfs_ilock(ip, XFS_MMAPLOCK_EXCL); |
Dan Williams | c63a8ea | 2018-03-12 14:12:29 -0700 | [diff] [blame] | 1063 | iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL; |
| 1064 | |
Dan Williams | 69eb5fa | 2018-03-20 14:42:38 -0700 | [diff] [blame] | 1065 | error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP); |
Dan Williams | c63a8ea | 2018-03-12 14:12:29 -0700 | [diff] [blame] | 1066 | if (error) { |
| 1067 | xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); |
| 1068 | return error; |
| 1069 | } |
| 1070 | |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 1071 | error = xfs_vn_setattr_size(mnt_userns, dentry, iattr); |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1072 | xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); |
Christoph Hellwig | 76ca4c2 | 2013-10-14 07:09:35 -0700 | [diff] [blame] | 1073 | } else { |
Christoph Hellwig | 26f8836 | 2020-12-10 20:00:38 -0800 | [diff] [blame] | 1074 | trace_xfs_setattr(ip); |
| 1075 | |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 1076 | error = xfs_vn_change_ok(mnt_userns, dentry, iattr); |
Christoph Hellwig | 26f8836 | 2020-12-10 20:00:38 -0800 | [diff] [blame] | 1077 | if (!error) |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 1078 | error = xfs_setattr_nonsize(mnt_userns, ip, iattr); |
Christoph Hellwig | 76ca4c2 | 2013-10-14 07:09:35 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1081 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1084 | STATIC int |
| 1085 | xfs_vn_update_time( |
| 1086 | struct inode *inode, |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 1087 | struct timespec64 *now, |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1088 | int flags) |
| 1089 | { |
| 1090 | struct xfs_inode *ip = XFS_I(inode); |
| 1091 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | c3b1b13 | 2018-03-06 17:04:00 -0800 | [diff] [blame] | 1092 | int log_flags = XFS_ILOG_TIMESTAMP; |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1093 | struct xfs_trans *tp; |
| 1094 | int error; |
| 1095 | |
| 1096 | trace_xfs_update_time(ip); |
| 1097 | |
Christoph Hellwig | c3b1b13 | 2018-03-06 17:04:00 -0800 | [diff] [blame] | 1098 | if (inode->i_sb->s_flags & SB_LAZYTIME) { |
| 1099 | if (!((flags & S_VERSION) && |
| 1100 | inode_maybe_inc_iversion(inode, false))) |
| 1101 | return generic_update_time(inode, now, flags); |
| 1102 | |
| 1103 | /* Capture the iversion update that just occurred */ |
| 1104 | log_flags |= XFS_ILOG_CORE; |
| 1105 | } |
| 1106 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1107 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); |
| 1108 | if (error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1109 | return error; |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1110 | |
| 1111 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1112 | if (flags & S_CTIME) |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1113 | inode->i_ctime = *now; |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1114 | if (flags & S_MTIME) |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1115 | inode->i_mtime = *now; |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1116 | if (flags & S_ATIME) |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1117 | inode->i_atime = *now; |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1118 | |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1119 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | c3b1b13 | 2018-03-06 17:04:00 -0800 | [diff] [blame] | 1120 | xfs_trans_log_inode(tp, ip, log_flags); |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1121 | return xfs_trans_commit(tp); |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1122 | } |
| 1123 | |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1124 | STATIC int |
| 1125 | xfs_vn_fiemap( |
| 1126 | struct inode *inode, |
| 1127 | struct fiemap_extent_info *fieinfo, |
| 1128 | u64 start, |
| 1129 | u64 length) |
| 1130 | { |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1131 | int error; |
| 1132 | |
Christoph Hellwig | d2bb140 | 2016-06-21 09:54:53 +1000 | [diff] [blame] | 1133 | xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED); |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1134 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 1135 | fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; |
| 1136 | error = iomap_fiemap(inode, fieinfo, start, length, |
| 1137 | &xfs_xattr_iomap_ops); |
| 1138 | } else { |
| 1139 | error = iomap_fiemap(inode, fieinfo, start, length, |
Christoph Hellwig | 690c2a3 | 2019-10-19 09:09:45 -0700 | [diff] [blame] | 1140 | &xfs_read_iomap_ops); |
Christoph Hellwig | 1d4795e | 2016-08-17 08:45:30 +1000 | [diff] [blame] | 1141 | } |
Christoph Hellwig | d2bb140 | 2016-06-21 09:54:53 +1000 | [diff] [blame] | 1142 | xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED); |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1143 | |
Christoph Hellwig | d2bb140 | 2016-06-21 09:54:53 +1000 | [diff] [blame] | 1144 | return error; |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1145 | } |
| 1146 | |
Zhi Yong Wu | 99b6436 | 2013-12-18 08:22:40 +0800 | [diff] [blame] | 1147 | STATIC int |
| 1148 | xfs_vn_tmpfile( |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 1149 | struct user_namespace *mnt_userns, |
| 1150 | struct inode *dir, |
| 1151 | struct dentry *dentry, |
| 1152 | umode_t mode) |
Zhi Yong Wu | 99b6436 | 2013-12-18 08:22:40 +0800 | [diff] [blame] | 1153 | { |
Christoph Hellwig | f736d93 | 2021-01-21 14:19:58 +0100 | [diff] [blame] | 1154 | return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, true); |
Zhi Yong Wu | 99b6436 | 2013-12-18 08:22:40 +0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1157 | static const struct inode_operations xfs_inode_operations = { |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 1158 | .get_acl = xfs_get_acl, |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 1159 | .set_acl = xfs_set_acl, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1160 | .getattr = xfs_vn_getattr, |
| 1161 | .setattr = xfs_vn_setattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1162 | .listxattr = xfs_vn_listxattr, |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1163 | .fiemap = xfs_vn_fiemap, |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1164 | .update_time = xfs_vn_update_time, |
Miklos Szeredi | 9fefd5d | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 1165 | .fileattr_get = xfs_fileattr_get, |
| 1166 | .fileattr_set = xfs_fileattr_set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | }; |
| 1168 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1169 | static const struct inode_operations xfs_dir_inode_operations = { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1170 | .create = xfs_vn_create, |
| 1171 | .lookup = xfs_vn_lookup, |
| 1172 | .link = xfs_vn_link, |
| 1173 | .unlink = xfs_vn_unlink, |
| 1174 | .symlink = xfs_vn_symlink, |
| 1175 | .mkdir = xfs_vn_mkdir, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1176 | /* |
| 1177 | * Yes, XFS uses the same method for rmdir and unlink. |
| 1178 | * |
| 1179 | * There are some subtile differences deeper in the code, |
| 1180 | * but we use S_ISDIR to check for those. |
| 1181 | */ |
| 1182 | .rmdir = xfs_vn_unlink, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1183 | .mknod = xfs_vn_mknod, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1184 | .rename = xfs_vn_rename, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 1185 | .get_acl = xfs_get_acl, |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 1186 | .set_acl = xfs_set_acl, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1187 | .getattr = xfs_vn_getattr, |
| 1188 | .setattr = xfs_vn_setattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1189 | .listxattr = xfs_vn_listxattr, |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1190 | .update_time = xfs_vn_update_time, |
Zhi Yong Wu | 99b6436 | 2013-12-18 08:22:40 +0800 | [diff] [blame] | 1191 | .tmpfile = xfs_vn_tmpfile, |
Miklos Szeredi | 9fefd5d | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 1192 | .fileattr_get = xfs_fileattr_get, |
| 1193 | .fileattr_set = xfs_fileattr_set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | }; |
| 1195 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1196 | static const struct inode_operations xfs_dir_ci_inode_operations = { |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1197 | .create = xfs_vn_create, |
| 1198 | .lookup = xfs_vn_ci_lookup, |
| 1199 | .link = xfs_vn_link, |
| 1200 | .unlink = xfs_vn_unlink, |
| 1201 | .symlink = xfs_vn_symlink, |
| 1202 | .mkdir = xfs_vn_mkdir, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 1203 | /* |
| 1204 | * Yes, XFS uses the same method for rmdir and unlink. |
| 1205 | * |
| 1206 | * There are some subtile differences deeper in the code, |
| 1207 | * but we use S_ISDIR to check for those. |
| 1208 | */ |
| 1209 | .rmdir = xfs_vn_unlink, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1210 | .mknod = xfs_vn_mknod, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1211 | .rename = xfs_vn_rename, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 1212 | .get_acl = xfs_get_acl, |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 1213 | .set_acl = xfs_set_acl, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1214 | .getattr = xfs_vn_getattr, |
| 1215 | .setattr = xfs_vn_setattr, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1216 | .listxattr = xfs_vn_listxattr, |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1217 | .update_time = xfs_vn_update_time, |
Zhi Yong Wu | 99b6436 | 2013-12-18 08:22:40 +0800 | [diff] [blame] | 1218 | .tmpfile = xfs_vn_tmpfile, |
Miklos Szeredi | 9fefd5d | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 1219 | .fileattr_get = xfs_fileattr_get, |
| 1220 | .fileattr_set = xfs_fileattr_set, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1221 | }; |
| 1222 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1223 | static const struct inode_operations xfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1224 | .get_link = xfs_vn_get_link, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1225 | .getattr = xfs_vn_getattr, |
| 1226 | .setattr = xfs_vn_setattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 1227 | .listxattr = xfs_vn_listxattr, |
Christoph Hellwig | 69ff282 | 2012-06-06 17:01:28 -0400 | [diff] [blame] | 1228 | .update_time = xfs_vn_update_time, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | }; |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1230 | |
Darrick J. Wong | ba23cba | 2018-05-30 13:03:45 -0700 | [diff] [blame] | 1231 | /* Figure out if this file actually supports DAX. */ |
| 1232 | static bool |
| 1233 | xfs_inode_supports_dax( |
| 1234 | struct xfs_inode *ip) |
| 1235 | { |
| 1236 | struct xfs_mount *mp = ip->i_mount; |
| 1237 | |
Ira Weiny | 32dbc56 | 2020-05-04 09:02:42 -0700 | [diff] [blame] | 1238 | /* Only supported on regular files. */ |
| 1239 | if (!S_ISREG(VFS_I(ip)->i_mode)) |
Darrick J. Wong | ba23cba | 2018-05-30 13:03:45 -0700 | [diff] [blame] | 1240 | return false; |
| 1241 | |
Ira Weiny | 32dbc56 | 2020-05-04 09:02:42 -0700 | [diff] [blame] | 1242 | /* Only supported on non-reflinked files. */ |
| 1243 | if (xfs_is_reflink_inode(ip)) |
Darrick J. Wong | ba23cba | 2018-05-30 13:03:45 -0700 | [diff] [blame] | 1244 | return false; |
| 1245 | |
| 1246 | /* Block size must match page size */ |
| 1247 | if (mp->m_sb.sb_blocksize != PAGE_SIZE) |
| 1248 | return false; |
| 1249 | |
| 1250 | /* Device has to support DAX too. */ |
Christoph Hellwig | 30fa529 | 2019-10-24 22:25:38 -0700 | [diff] [blame] | 1251 | return xfs_inode_buftarg(ip)->bt_daxdev != NULL; |
Darrick J. Wong | ba23cba | 2018-05-30 13:03:45 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Ira Weiny | 32dbc56 | 2020-05-04 09:02:42 -0700 | [diff] [blame] | 1254 | static bool |
| 1255 | xfs_inode_should_enable_dax( |
| 1256 | struct xfs_inode *ip) |
| 1257 | { |
| 1258 | if (!IS_ENABLED(CONFIG_FS_DAX)) |
| 1259 | return false; |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 1260 | if (xfs_has_dax_never(ip->i_mount)) |
Ira Weiny | 32dbc56 | 2020-05-04 09:02:42 -0700 | [diff] [blame] | 1261 | return false; |
| 1262 | if (!xfs_inode_supports_dax(ip)) |
| 1263 | return false; |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 1264 | if (xfs_has_dax_always(ip->i_mount)) |
Ira Weiny | 32dbc56 | 2020-05-04 09:02:42 -0700 | [diff] [blame] | 1265 | return true; |
Christoph Hellwig | 3e09ab8 | 2021-03-29 11:11:45 -0700 | [diff] [blame] | 1266 | if (ip->i_diflags2 & XFS_DIFLAG2_DAX) |
Ira Weiny | 32dbc56 | 2020-05-04 09:02:42 -0700 | [diff] [blame] | 1267 | return true; |
| 1268 | return false; |
| 1269 | } |
| 1270 | |
Ira Weiny | 840d493 | 2020-05-04 09:02:43 -0700 | [diff] [blame] | 1271 | void |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1272 | xfs_diflags_to_iflags( |
Ira Weiny | 840d493 | 2020-05-04 09:02:43 -0700 | [diff] [blame] | 1273 | struct xfs_inode *ip, |
| 1274 | bool init) |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1275 | { |
Ira Weiny | 840d493 | 2020-05-04 09:02:43 -0700 | [diff] [blame] | 1276 | struct inode *inode = VFS_I(ip); |
| 1277 | unsigned int xflags = xfs_ip2xflags(ip); |
| 1278 | unsigned int flags = 0; |
Dave Chinner | cbe4dab | 2015-06-04 09:19:18 +1000 | [diff] [blame] | 1279 | |
Ira Weiny | 840d493 | 2020-05-04 09:02:43 -0700 | [diff] [blame] | 1280 | ASSERT(!(IS_DAX(inode) && init)); |
Dave Chinner | cbe4dab | 2015-06-04 09:19:18 +1000 | [diff] [blame] | 1281 | |
Ira Weiny | 840d493 | 2020-05-04 09:02:43 -0700 | [diff] [blame] | 1282 | if (xflags & FS_XFLAG_IMMUTABLE) |
| 1283 | flags |= S_IMMUTABLE; |
| 1284 | if (xflags & FS_XFLAG_APPEND) |
| 1285 | flags |= S_APPEND; |
| 1286 | if (xflags & FS_XFLAG_SYNC) |
| 1287 | flags |= S_SYNC; |
| 1288 | if (xflags & FS_XFLAG_NOATIME) |
| 1289 | flags |= S_NOATIME; |
| 1290 | if (init && xfs_inode_should_enable_dax(ip)) |
| 1291 | 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 | inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME); |
| 1298 | inode->i_flags |= flags; |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /* |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 1302 | * Initialize the Linux inode. |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1303 | * |
Dave Chinner | 58c9047 | 2015-02-23 22:38:08 +1100 | [diff] [blame] | 1304 | * When reading existing inodes from disk this is called directly from xfs_iget, |
Yang Xu | 132c460 | 2021-12-21 09:38:19 -0800 | [diff] [blame] | 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. |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1308 | */ |
| 1309 | void |
| 1310 | xfs_setup_inode( |
| 1311 | struct xfs_inode *ip) |
| 1312 | { |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1313 | struct inode *inode = &ip->i_vnode; |
Dave Chinner | ad22c7a | 2013-10-29 22:11:57 +1100 | [diff] [blame] | 1314 | gfp_t gfp_mask; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1315 | |
| 1316 | inode->i_ino = ip->i_ino; |
Dave Chinner | f38a032 | 2021-08-24 19:13:04 -0700 | [diff] [blame] | 1317 | inode->i_state |= I_NEW; |
Christoph Hellwig | 646ec46 | 2010-10-23 07:15:32 -0400 | [diff] [blame] | 1318 | |
| 1319 | inode_sb_list_add(inode); |
Christoph Hellwig | c6f6cd0 | 2010-11-06 11:43:08 +0000 | [diff] [blame] | 1320 | /* make the inode look hashed for the writeback code */ |
Al Viro | 5bef915 | 2018-06-29 19:36:57 -0400 | [diff] [blame] | 1321 | inode_fake_hash(inode); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1322 | |
Christoph Hellwig | 13d2c10 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 1323 | i_size_write(inode, ip->i_disk_size); |
Ira Weiny | 840d493 | 2020-05-04 09:02:43 -0700 | [diff] [blame] | 1324 | xfs_diflags_to_iflags(ip, true); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1325 | |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 1326 | if (S_ISDIR(inode->i_mode)) { |
Dave Chinner | ef215e3 | 2018-06-07 07:36:08 -0700 | [diff] [blame] | 1327 | /* |
| 1328 | * We set the i_rwsem class here to avoid potential races with |
| 1329 | * lockdep_annotate_inode_mutex_key() reinitialising the lock |
| 1330 | * after a filehandle lookup has already found the inode in |
| 1331 | * cache before it has been unlocked via unlock_new_inode(). |
| 1332 | */ |
| 1333 | lockdep_set_class(&inode->i_rwsem, |
| 1334 | &inode->i_sb->s_type->i_mutex_dir_key); |
Dave Chinner | 93a8614 | 2014-02-27 16:51:39 +1100 | [diff] [blame] | 1335 | lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class); |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 1336 | } else { |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 1337 | lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1338 | } |
| 1339 | |
Christoph Hellwig | 510792e | 2011-07-26 15:07:29 +0000 | [diff] [blame] | 1340 | /* |
Dave Chinner | ad22c7a | 2013-10-29 22:11:57 +1100 | [diff] [blame] | 1341 | * Ensure all page cache allocations are done from GFP_NOFS context to |
| 1342 | * prevent direct reclaim recursion back into the filesystem and blowing |
| 1343 | * stacks or deadlocking. |
| 1344 | */ |
| 1345 | gfp_mask = mapping_gfp_mask(inode->i_mapping); |
| 1346 | mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS))); |
| 1347 | |
| 1348 | /* |
Christoph Hellwig | 510792e | 2011-07-26 15:07:29 +0000 | [diff] [blame] | 1349 | * If there is no attribute fork no ACL can exist on this inode, |
| 1350 | * and it can't have any file capabilities attached to it either. |
| 1351 | */ |
| 1352 | if (!XFS_IFORK_Q(ip)) { |
| 1353 | inode_has_no_xattr(inode); |
Christoph Hellwig | 6311b10 | 2011-07-23 17:36:50 +0200 | [diff] [blame] | 1354 | cache_no_acl(inode); |
Christoph Hellwig | 510792e | 2011-07-26 15:07:29 +0000 | [diff] [blame] | 1355 | } |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1356 | } |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 1357 | |
| 1358 | void |
| 1359 | xfs_setup_iops( |
| 1360 | struct xfs_inode *ip) |
| 1361 | { |
| 1362 | struct inode *inode = &ip->i_vnode; |
| 1363 | |
| 1364 | switch (inode->i_mode & S_IFMT) { |
| 1365 | case S_IFREG: |
| 1366 | inode->i_op = &xfs_inode_operations; |
| 1367 | inode->i_fop = &xfs_file_operations; |
Dan Williams | 6e2608d | 2018-03-07 15:26:44 -0800 | [diff] [blame] | 1368 | if (IS_DAX(inode)) |
| 1369 | inode->i_mapping->a_ops = &xfs_dax_aops; |
| 1370 | else |
| 1371 | inode->i_mapping->a_ops = &xfs_address_space_operations; |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 1372 | break; |
| 1373 | case S_IFDIR: |
Dave Chinner | 38c26bf | 2021-08-18 18:46:37 -0700 | [diff] [blame] | 1374 | if (xfs_has_asciici(XFS_M(inode->i_sb))) |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 1375 | inode->i_op = &xfs_dir_ci_inode_operations; |
| 1376 | else |
| 1377 | inode->i_op = &xfs_dir_inode_operations; |
| 1378 | inode->i_fop = &xfs_dir_file_operations; |
| 1379 | break; |
| 1380 | case S_IFLNK: |
Darrick J. Wong | 7b7820b | 2021-12-15 12:07:41 -0800 | [diff] [blame] | 1381 | inode->i_op = &xfs_symlink_inode_operations; |
Christoph Hellwig | 2b3d1d4 | 2016-04-06 07:48:27 +1000 | [diff] [blame] | 1382 | break; |
| 1383 | default: |
| 1384 | inode->i_op = &xfs_inode_operations; |
| 1385 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 1386 | break; |
| 1387 | } |
| 1388 | } |