blob: b79b3846e71be283e54d62eb071870a8b345e0bf [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
7#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110014#include "xfs_acl.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_attr.h"
Dave Chinner239880e2013-10-23 10:50:10 +110017#include "xfs_trans.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000018#include "xfs_trace.h"
Brian Foster27b52862012-11-06 09:50:38 -050019#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100020#include "xfs_symlink.h"
Dave Chinner1b767ee2014-12-04 09:43:17 +110021#include "xfs_dir2.h"
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +100022#include "xfs_iomap.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070023#include "xfs_error.h"
Miklos Szeredi9fefd5d2021-04-07 14:36:43 +020024#include "xfs_ioctl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020026#include <linux/posix_acl.h>
Nathan Scott446ada42006-01-11 15:35:44 +110027#include <linux/security.h>
Christoph Hellwigc3b1b132018-03-06 17:04:00 -080028#include <linux/iversion.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020029#include <linux/fiemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Dave Chinner93a86142014-02-27 16:51:39 +110031/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070032 * Directories have different lock order w.r.t. mmap_lock compared to regular
Dave Chinner93a86142014-02-27 16:51:39 +110033 * 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 Lespinassec1e8d7c2020-06-08 21:33:54 -070036 * mmap_lock is taken during the page fault, and then we lock the ilock to do
Dave Chinner93a86142014-02-27 16:51:39 +110037 * block mapping. Hence we need a different class for the directory ilock so
38 * that lockdep can tell them apart.
39 */
40static struct lock_class_key xfs_nondir_ilock_class;
41static struct lock_class_key xfs_dir_ilock_class;
42
Dave Chinner8d2a5e62012-03-07 04:50:19 +000043static int
44xfs_initxattrs(
45 struct inode *inode,
46 const struct xattr *xattr_array,
47 void *fs_info)
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040048{
Dave Chinner8d2a5e62012-03-07 04:50:19 +000049 const struct xattr *xattr;
50 struct xfs_inode *ip = XFS_I(inode);
51 int error = 0;
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040052
53 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Christoph Hellwiga2544622020-02-26 17:30:33 -080054 struct xfs_da_args args = {
55 .dp = ip,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080056 .attr_filter = XFS_ATTR_SECURE,
Christoph Hellwiga2544622020-02-26 17:30:33 -080057 .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 Zohar9d8f13b2011-06-06 15:29:25 -040063 if (error < 0)
64 break;
65 }
66 return error;
67}
68
David Chinner5d51eff2007-11-23 16:29:18 +110069/*
Nathan Scott446ada42006-01-11 15:35:44 +110070 * 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 Zohar9d8f13b2011-06-06 15:29:25 -040075
Nathan Scott446ada42006-01-11 15:35:44 +110076STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +110077xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +110078 struct inode *inode,
Eric Paris2a7dba32011-02-01 11:05:39 -050079 struct inode *dir,
80 const struct qstr *qstr)
Nathan Scott446ada42006-01-11 15:35:44 +110081{
Dave Chinner24513372014-06-25 14:58:08 +100082 return security_inode_init_security(inode, dir, qstr,
Dave Chinnera5a14de2014-05-15 09:22:21 +100083 &xfs_initxattrs, NULL);
Nathan Scott446ada42006-01-11 15:35:44 +110084}
85
Barry Naujok556b8b12008-04-10 12:22:07 +100086static void
87xfs_dentry_to_name(
88 struct xfs_name *namep,
Amir Goldsteinfab8eef2017-01-17 11:41:44 -080089 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
96static int
97xfs_dentry_mode_to_name(
98 struct xfs_name *namep,
Dave Chinner0cb97762013-08-12 20:50:09 +100099 struct dentry *dentry,
100 int mode)
Barry Naujok556b8b12008-04-10 12:22:07 +1000101{
102 namep->name = dentry->d_name.name;
103 namep->len = dentry->d_name.len;
Amir Goldstein1fc4d332017-01-17 11:41:43 -0800104 namep->type = xfs_mode_to_ftype(mode);
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800105
106 if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
107 return -EFSCORRUPTED;
108
109 return 0;
Barry Naujok556b8b12008-04-10 12:22:07 +1000110}
111
David Chinner7989cb82007-02-10 18:34:56 +1100112STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100113xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000114 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100115 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000116 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100117{
Barry Naujok556b8b12008-04-10 12:22:07 +1000118 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100119
120 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100121 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100122 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100123 * ENOSPC can hit here, among other things.
124 */
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800125 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100126
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000127 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100128}
129
Dave Chinnere6a688c2021-03-22 09:52:03 -0700130/*
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 */
143static inline bool
144xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700161STATIC int
Brian Fosterd540e432014-05-06 07:34:28 +1000162xfs_generic_create(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100163 struct user_namespace *mnt_userns,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct inode *dir,
165 struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400166 umode_t mode,
Brian Fosterd540e432014-05-06 07:34:28 +1000167 dev_t rdev,
168 bool tmpfile) /* unnamed file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100170 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100171 struct xfs_inode *ip = NULL;
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800172 struct posix_acl *default_acl, *acl;
Barry Naujok556b8b12008-04-10 12:22:07 +1000173 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 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 Hellwig517b5e82009-02-09 08:38:02 +0100180 if (S_ISCHR(mode) || S_ISBLK(mode)) {
181 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
182 return -EINVAL;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100183 } else {
184 rdev = 0;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800187 error = posix_acl_create(dir, &mode, &default_acl, &acl);
188 if (error)
189 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800191 /* 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 Fosterd540e432014-05-06 07:34:28 +1000196 if (!tmpfile) {
Christoph Hellwigf736d932021-01-21 14:19:58 +0100197 error = xfs_create(mnt_userns, XFS_I(dir), &name, mode, rdev,
Dave Chinnere6a688c2021-03-22 09:52:03 -0700198 xfs_create_need_xattr(dir, default_acl, acl),
199 &ip);
Brian Fosterd540e432014-05-06 07:34:28 +1000200 } else {
Christoph Hellwigf736d932021-01-21 14:19:58 +0100201 error = xfs_create_tmpfile(mnt_userns, XFS_I(dir), mode, &ip);
Brian Fosterd540e432014-05-06 07:34:28 +1000202 }
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100203 if (unlikely(error))
204 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100205
David Chinner01651642008-08-13 15:45:15 +1000206 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100207
Eric Paris2a7dba32011-02-01 11:05:39 -0500208 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100209 if (unlikely(error))
210 goto out_cleanup_inode;
211
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800212#ifdef CONFIG_XFS_POSIX_ACL
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100213 if (default_acl) {
Jan Kara8ba35872017-06-26 08:48:18 -0700214 error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800215 if (error)
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100216 goto out_cleanup_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800218 if (acl) {
Jan Kara8ba35872017-06-26 08:48:18 -0700219 error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800220 if (error)
221 goto out_cleanup_inode;
222 }
223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +1000225 xfs_setup_iops(ip);
226
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -0800227 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 Fosterd540e432014-05-06 07:34:28 +1000237 d_tmpfile(dentry, inode);
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -0800238 } else
Brian Fosterd540e432014-05-06 07:34:28 +1000239 d_instantiate(dentry, inode);
240
Dave Chinner58c90472015-02-23 22:38:08 +1100241 xfs_finish_inode_setup(ip);
242
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800243 out_free_acl:
Kaixu Xia88269b82020-12-03 16:43:19 -0800244 posix_acl_release(default_acl);
245 posix_acl_release(acl);
Dave Chinner24513372014-06-25 14:58:08 +1000246 return error;
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100247
248 out_cleanup_inode:
Dave Chinner58c90472015-02-23 22:38:08 +1100249 xfs_finish_inode_setup(ip);
Brian Fosterd540e432014-05-06 07:34:28 +1000250 if (!tmpfile)
251 xfs_cleanup_inode(dir, inode, dentry);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700252 xfs_irele(ip);
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800253 goto out_free_acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256STATIC int
Brian Fosterd540e432014-05-06 07:34:28 +1000257xfs_vn_mknod(
Christian Brauner549c7292021-01-21 14:19:43 +0100258 struct user_namespace *mnt_userns,
259 struct inode *dir,
260 struct dentry *dentry,
261 umode_t mode,
262 dev_t rdev)
Brian Fosterd540e432014-05-06 07:34:28 +1000263{
Christoph Hellwigf736d932021-01-21 14:19:58 +0100264 return xfs_generic_create(mnt_userns, dir, dentry, mode, rdev, false);
Brian Fosterd540e432014-05-06 07:34:28 +1000265}
266
267STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100268xfs_vn_create(
Christian Brauner549c7292021-01-21 14:19:43 +0100269 struct user_namespace *mnt_userns,
270 struct inode *dir,
271 struct dentry *dentry,
272 umode_t mode,
273 bool flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Christoph Hellwigf736d932021-01-21 14:19:58 +0100275 return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100279xfs_vn_mkdir(
Christian Brauner549c7292021-01-21 14:19:43 +0100280 struct user_namespace *mnt_userns,
281 struct inode *dir,
282 struct dentry *dentry,
283 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Christoph Hellwigf736d932021-01-21 14:19:58 +0100285 return xfs_generic_create(mnt_userns, dir, dentry, mode | S_IFDIR, 0,
286 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100290xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct inode *dir,
292 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400293 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Al Virob113a6d2018-04-30 23:04:19 -0400295 struct inode *inode;
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100296 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000297 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int error;
299
300 if (dentry->d_name.len >= MAXNAMELEN)
301 return ERR_PTR(-ENAMETOOLONG);
302
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800303 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000304 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Al Virob113a6d2018-04-30 23:04:19 -0400305 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 Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Barry Naujok384f3ce2008-05-21 16:58:22 +1000314STATIC struct dentry *
315xfs_vn_ci_lookup(
316 struct inode *dir,
317 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400318 unsigned int flags)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000319{
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 Goldsteinfab8eef2017-01-17 11:41:44 -0800329 xfs_dentry_to_name(&xname, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000330 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
331 if (unlikely(error)) {
Dave Chinner24513372014-06-25 14:58:08 +1000332 if (unlikely(error != -ENOENT))
333 return ERR_PTR(error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000334 /*
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 Naujok384f3ce2008-05-21 16:58:22 +1000339 return NULL;
340 }
341
342 /* if exact match, just splice and exit */
343 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000344 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000345
346 /* else case-insensitive match... */
347 dname.name = ci_name.name;
348 dname.len = ci_name.len;
Christoph Hellwige45b5902008-08-07 23:49:07 +0200349 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000350 kmem_free(ci_name.name);
351 return dentry;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100355xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct dentry *old_dentry,
357 struct inode *dir,
358 struct dentry *dentry)
359{
David Howells2b0143b2015-03-17 22:25:59 +0000360 struct inode *inode = d_inode(old_dentry);
Barry Naujok556b8b12008-04-10 12:22:07 +1000361 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int error;
363
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800364 error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
365 if (unlikely(error))
366 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Barry Naujok556b8b12008-04-10 12:22:07 +1000368 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100369 if (unlikely(error))
Dave Chinner24513372014-06-25 14:58:08 +1000370 return error;
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100371
Al Viro7de9c6ee2010-10-23 11:11:40 -0400372 ihold(inode);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100373 d_instantiate(dentry, inode);
374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100378xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct inode *dir,
380 struct dentry *dentry)
381{
Barry Naujok556b8b12008-04-10 12:22:07 +1000382 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int error;
384
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800385 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
David Howells2b0143b2015-03-17 22:25:59 +0000387 error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
Christoph Hellwige5700702008-06-23 13:25:25 +1000388 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 Chinner38c26bf2021-08-18 18:46:37 -0700396 if (xfs_has_asciici(XFS_M(dir->i_sb)))
Christoph Hellwige5700702008-06-23 13:25:25 +1000397 d_invalidate(dentry);
398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100402xfs_vn_symlink(
Christian Brauner549c7292021-01-21 14:19:43 +0100403 struct user_namespace *mnt_userns,
404 struct inode *dir,
405 struct dentry *dentry,
406 const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100408 struct inode *inode;
409 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000410 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int error;
Al Viro576b1d62011-07-26 02:50:15 -0400412 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000414 mode = S_IFLNK |
Al Viroce3b0f82009-03-29 19:08:22 -0400415 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800416 error = xfs_dentry_mode_to_name(&name, dentry, mode);
417 if (unlikely(error))
418 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Christoph Hellwigf736d932021-01-21 14:19:58 +0100420 error = xfs_symlink(mnt_userns, XFS_I(dir), &name, symname, mode, &cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100421 if (unlikely(error))
422 goto out;
423
David Chinner01651642008-08-13 15:45:15 +1000424 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100425
Eric Paris2a7dba32011-02-01 11:05:39 -0500426 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100427 if (unlikely(error))
428 goto out_cleanup_inode;
429
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +1000430 xfs_setup_iops(cip);
431
Christoph Hellwig3937be52008-03-06 13:46:19 +1100432 d_instantiate(dentry, inode);
Dave Chinner58c90472015-02-23 22:38:08 +1100433 xfs_finish_inode_setup(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100434 return 0;
435
436 out_cleanup_inode:
Dave Chinner58c90472015-02-23 22:38:08 +1100437 xfs_finish_inode_setup(cip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000438 xfs_cleanup_inode(dir, inode, dentry);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700439 xfs_irele(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100440 out:
Dave Chinner24513372014-06-25 14:58:08 +1000441 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100445xfs_vn_rename(
Christian Brauner549c7292021-01-21 14:19:43 +0100446 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 Torvalds1da177e2005-04-16 15:20:36 -0700452{
David Howells2b0143b2015-03-17 22:25:59 +0000453 struct inode *new_inode = d_inode(ndentry);
Carlos Maiolinod31a1822014-12-24 08:51:42 +1100454 int omode = 0;
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800455 int error;
Barry Naujok556b8b12008-04-10 12:22:07 +1000456 struct xfs_name oname;
457 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dave Chinner7dcf5c32015-03-25 14:08:08 +1100459 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Carlos Maiolinodbe1b5c2014-12-24 08:51:38 +1100460 return -EINVAL;
461
Carlos Maiolinod31a1822014-12-24 08:51:42 +1100462 /* if we are exchanging files, we need to set i_mode of both files */
463 if (flags & RENAME_EXCHANGE)
David Howells2b0143b2015-03-17 22:25:59 +0000464 omode = d_inode(ndentry)->i_mode;
Carlos Maiolinod31a1822014-12-24 08:51:42 +1100465
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800466 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 Naujok556b8b12008-04-10 12:22:07 +1000474
Christoph Hellwigf736d932021-01-21 14:19:58 +0100475 return xfs_rename(mnt_userns, XFS_I(odir), &oname,
476 XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
Carlos Maiolinod31a1822014-12-24 08:51:42 +1100477 new_inode ? XFS_I(new_inode) : NULL, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
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 Viro680baac2015-05-02 13:32:22 -0400485STATIC const char *
Al Viro6b255392015-11-17 10:20:54 -0500486xfs_vn_get_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct dentry *dentry,
Al Viro6b255392015-11-17 10:20:54 -0500488 struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -0500489 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000492 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Al Viro6b255392015-11-17 10:20:54 -0500494 if (!dentry)
495 return ERR_PTR(-ECHILD);
496
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700497 link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000498 if (!link)
499 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
David Howells2b0143b2015-03-17 22:25:59 +0000501 error = xfs_readlink(XFS_I(d_inode(dentry)), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000502 if (unlikely(error))
503 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Al Virofceef392015-12-29 15:58:39 -0500505 set_delayed_call(done, kfree_link, link);
506 return link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000507
508 out_kfree:
509 kfree(link);
510 out_err:
Al Viro680baac2015-05-02 13:32:22 -0400511 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700514static uint32_t
515xfs_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. Wonga7bcb142021-05-31 11:31:56 -0700525 return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip));
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700526
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 Chinner0560f312021-08-18 18:46:52 -0700540 if (xfs_has_large_iosize(mp)) {
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700541 if (mp->m_swidth)
Darrick J. Wonga7bcb142021-05-31 11:31:56 -0700542 return XFS_FSB_TO_B(mp, mp->m_swidth);
Dave Chinner0560f312021-08-18 18:46:52 -0700543 if (xfs_has_allocsize(mp))
Christoph Hellwig5da8a072019-10-28 08:41:44 -0700544 return 1U << mp->m_allocsize_log;
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700545 }
546
547 return PAGE_SIZE;
548}
549
Christoph Hellwig45767582008-02-05 12:13:24 +1100550STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100551xfs_vn_getattr(
Christian Brauner549c7292021-01-21 14:19:43 +0100552 struct user_namespace *mnt_userns,
David Howellsa528d352017-01-31 16:46:22 +0000553 const struct path *path,
554 struct kstat *stat,
555 u32 request_mask,
556 unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
David Howellsa528d352017-01-31 16:46:22 +0000558 struct inode *inode = d_inode(path->dentry);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000559 struct xfs_inode *ip = XFS_I(inode);
560 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000562 trace_xfs_getattr(ip);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000563
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700564 if (xfs_is_shutdown(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000565 return -EIO;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000566
567 stat->size = XFS_ISIZE(ip);
568 stat->dev = inode->i_sb->s_dev;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100569 stat->mode = inode->i_mode;
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100570 stat->nlink = inode->i_nlink;
Christoph Hellwigf736d932021-01-21 14:19:58 +0100571 stat->uid = i_uid_into_mnt(mnt_userns, inode);
572 stat->gid = i_gid_into_mnt(mnt_userns, inode);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000573 stat->ino = ip->i_ino;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000574 stat->atime = inode->i_atime;
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000575 stat->mtime = inode->i_mtime;
576 stat->ctime = inode->i_ctime;
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700577 stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000578
Dave Chinner38c26bf2021-08-18 18:46:37 -0700579 if (xfs_has_v3inodes(mp)) {
Darrick J. Wong5f955f22017-03-31 18:32:03 +0100580 if (request_mask & STATX_BTIME) {
581 stat->result_mask |= STATX_BTIME;
Christoph Hellwige98d5e82021-03-29 11:11:45 -0700582 stat->btime = ip->i_crtime;
Darrick J. Wong5f955f22017-03-31 18:32:03 +0100583 }
584 }
585
Luis R. Rodriguez1b9598c2019-03-01 08:14:57 -0800586 /*
587 * Note: If you add another clause to set an attribute flag, please
588 * update attributes_mask below.
589 */
Christoph Hellwigdb073492021-03-29 11:11:44 -0700590 if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
Darrick J. Wong5f955f22017-03-31 18:32:03 +0100591 stat->attributes |= STATX_ATTR_IMMUTABLE;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700592 if (ip->i_diflags & XFS_DIFLAG_APPEND)
Darrick J. Wong5f955f22017-03-31 18:32:03 +0100593 stat->attributes |= STATX_ATTR_APPEND;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700594 if (ip->i_diflags & XFS_DIFLAG_NODUMP)
Darrick J. Wong5f955f22017-03-31 18:32:03 +0100595 stat->attributes |= STATX_ATTR_NODUMP;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000596
Luis R. Rodriguez1b9598c2019-03-01 08:14:57 -0800597 stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
598 STATX_ATTR_APPEND |
599 STATX_ATTR_NODUMP);
600
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000601 switch (inode->i_mode & S_IFMT) {
602 case S_IFBLK:
603 case S_IFCHR:
604 stat->blksize = BLKDEV_IOSIZE;
Christoph Hellwig66f36462017-10-19 11:07:09 -0700605 stat->rdev = inode->i_rdev;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000606 break;
607 default:
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700608 stat->blksize = xfs_stat_blksize(ip);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000609 stat->rdev = 0;
610 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000611 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000612
613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Dave Chinner56c19e82013-05-27 16:38:25 +1000616static void
617xfs_setattr_mode(
Dave Chinner56c19e82013-05-27 16:38:25 +1000618 struct xfs_inode *ip,
619 struct iattr *iattr)
620{
Christoph Hellwig0c3d88d2013-11-18 05:10:40 -0800621 struct inode *inode = VFS_I(ip);
622 umode_t mode = iattr->ia_mode;
Dave Chinner56c19e82013-05-27 16:38:25 +1000623
Dave Chinner56c19e82013-05-27 16:38:25 +1000624 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
625
Dave Chinner56c19e82013-05-27 16:38:25 +1000626 inode->i_mode &= S_IFMT;
627 inode->i_mode |= mode & ~S_IFMT;
628}
629
Christoph Hellwig52785112015-02-16 11:49:23 +1100630void
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800631xfs_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 Chinner39878482016-02-09 16:54:58 +1100639 if (iattr->ia_valid & ATTR_ATIME)
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800640 inode->i_atime = iattr->ia_atime;
Dave Chinner39878482016-02-09 16:54:58 +1100641 if (iattr->ia_valid & ATTR_CTIME)
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800642 inode->i_ctime = iattr->ia_ctime;
Dave Chinner39878482016-02-09 16:54:58 +1100643 if (iattr->ia_valid & ATTR_MTIME)
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800644 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800645}
646
Jan Kara69bca802016-05-26 14:46:43 +0200647static int
648xfs_vn_change_ok(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100649 struct user_namespace *mnt_userns,
650 struct dentry *dentry,
651 struct iattr *iattr)
Jan Kara69bca802016-05-26 14:46:43 +0200652{
Jan Kara31051c82016-05-26 16:55:18 +0200653 struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
Jan Kara69bca802016-05-26 14:46:43 +0200654
Dave Chinner2e973b22021-08-18 18:46:52 -0700655 if (xfs_is_readonly(mp))
Jan Kara69bca802016-05-26 14:46:43 +0200656 return -EROFS;
657
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700658 if (xfs_is_shutdown(mp))
Jan Kara69bca802016-05-26 14:46:43 +0200659 return -EIO;
660
Christoph Hellwigf736d932021-01-21 14:19:58 +0100661 return setattr_prepare(mnt_userns, dentry, iattr);
Jan Kara69bca802016-05-26 14:46:43 +0200662}
663
664/*
665 * Set non-size attributes of an inode.
666 *
667 * Caution: The caller of this function is responsible for calling
Jan Kara31051c82016-05-26 16:55:18 +0200668 * setattr_prepare() or otherwise verifying the change is fine.
Jan Kara69bca802016-05-26 14:46:43 +0200669 */
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -0800670static int
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200671xfs_setattr_nonsize(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100672 struct user_namespace *mnt_userns,
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200673 struct xfs_inode *ip,
Christoph Hellwig5d24ec4c2020-12-10 20:00:39 -0800674 struct iattr *iattr)
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200675{
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 Engen7aab1b22013-08-15 14:08:01 -0400681 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
682 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200683 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
684 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
685
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200686 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 Engen7aab1b22013-08-15 14:08:01 -0400703 uid = inode->i_uid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200704 }
705 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
706 gid = iattr->ia_gid;
707 qflags |= XFS_QMOPT_GQUOTA;
708 } else {
Dwight Engen7aab1b22013-08-15 14:08:01 -0400709 gid = inode->i_gid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200710 }
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 Hellwigceaf6032021-03-29 11:11:39 -0700719 error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid,
Dwight Engen7aab1b22013-08-15 14:08:01 -0400720 qflags, &udqp, &gdqp, NULL);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200721 if (error)
722 return error;
723 }
724
Darrick J. Wong7317a032021-01-29 11:32:09 -0800725 error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
726 capable(CAP_FOWNER), &tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200727 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000728 goto out_dqrele;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200729
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200730 /*
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 Engen7aab1b22013-08-15 14:08:01 -0400740 iuid = inode->i_uid;
741 igid = inode->i_gid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200742 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
743 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
744
745 /*
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200746 * 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 Chinnerc19b3b052016-02-09 16:54:58 +1100751 if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200752 !capable(CAP_FSETID))
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100753 inode->i_mode &= ~(S_ISUID|S_ISGID);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200754
755 /*
756 * Change the ownerships and register quota modifications
757 * in the transaction.
758 */
Dwight Engen7aab1b22013-08-15 14:08:01 -0400759 if (!uid_eq(iuid, uid)) {
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700760 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200761 ASSERT(mask & ATTR_UID);
762 ASSERT(udqp);
763 olddquot1 = xfs_qm_vop_chown(tp, ip,
764 &ip->i_udquot, udqp);
765 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200766 inode->i_uid = uid;
767 }
Dwight Engen7aab1b22013-08-15 14:08:01 -0400768 if (!gid_eq(igid, gid)) {
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700769 if (XFS_IS_GQUOTA_ON(mp)) {
Dave Chinner38c26bf2021-08-18 18:46:37 -0700770 ASSERT(xfs_has_pquotino(mp) ||
Jie Liu5a01dd52013-11-26 21:38:34 +0800771 !XFS_IS_PQUOTA_ON(mp));
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200772 ASSERT(mask & ATTR_GID);
773 ASSERT(gdqp);
774 olddquot2 = xfs_qm_vop_chown(tp, ip,
775 &ip->i_gdquot, gdqp);
776 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200777 inode->i_gid = gid;
778 }
779 }
780
Dave Chinner56c19e82013-05-27 16:38:25 +1000781 if (mask & ATTR_MODE)
Christoph Hellwig0c3d88d2013-11-18 05:10:40 -0800782 xfs_setattr_mode(ip, iattr);
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800783 if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
784 xfs_setattr_time(ip, iattr);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200785
786 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
787
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100788 XFS_STATS_INC(mp, xs_ig_attrchg);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200789
Dave Chinner0560f312021-08-18 18:46:52 -0700790 if (xfs_has_wsync(mp))
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200791 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +1000792 error = xfs_trans_commit(tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200793
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200794 /*
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 Sandeenb474c7a2014-06-22 15:04:54 +1000803 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200804
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 Hellwig5d24ec4c2020-12-10 20:00:39 -0800812 if (mask & ATTR_MODE) {
Christoph Hellwigf736d932021-01-21 14:19:58 +0100813 error = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200814 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000815 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200816 }
817
818 return 0;
819
Christoph Hellwig253f4912016-04-06 09:19:55 +1000820out_dqrele:
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200821 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 Kara69bca802016-05-26 14:46:43 +0200828 *
829 * Caution: The caller of this function is responsible for calling
Jan Kara31051c82016-05-26 16:55:18 +0200830 * setattr_prepare() or otherwise verifying the change is fine.
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200831 */
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700832STATIC int
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200833xfs_setattr_size(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100834 struct user_namespace *mnt_userns,
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200835 struct xfs_inode *ip,
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700836 struct iattr *iattr)
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200837{
838 struct xfs_mount *mp = ip->i_mount;
839 struct inode *inode = VFS_I(ip);
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000840 xfs_off_t oldsize, newsize;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200841 struct xfs_trans *tp;
842 int error;
Christoph Hellwigf38996f2012-03-27 10:34:48 -0400843 uint lock_flags = 0;
Dave Chinner5885ebd2015-02-23 22:37:08 +1100844 bool did_zeroing = false;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200845
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700846 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Dave Chinnere8e9ad42015-02-23 21:45:32 +1100847 ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100848 ASSERT(S_ISREG(inode->i_mode));
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +1100849 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
Yumei Huang88a9e032021-01-22 16:48:19 -0800850 ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200851
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000852 oldsize = inode->i_size;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000853 newsize = iattr->ia_size;
854
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200855 /*
856 * Short circuit the truncate case for zero length files.
857 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700858 if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +1100859 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700860 return 0;
Christoph Hellwig681b1202011-07-08 14:34:26 +0200861
862 /*
863 * Use the regular setattr path to update the timestamps.
864 */
Christoph Hellwig681b1202011-07-08 14:34:26 +0200865 iattr->ia_valid &= ~ATTR_SIZE;
Christoph Hellwigf736d932021-01-21 14:19:58 +0100866 return xfs_setattr_nonsize(mnt_userns, ip, iattr);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200867 }
868
869 /*
870 * Make sure that the dquots are attached to the inode.
871 */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700872 error = xfs_qm_dqattach(ip);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200873 if (error)
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700874 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200875
876 /*
Christoph Hellwigf0c6bcb2016-06-21 09:52:47 +1000877 * Wait for all direct I/O to complete.
878 */
879 inode_dio_wait(inode);
880
881 /*
Dave Chinner5885ebd2015-02-23 22:37:08 +1100882 * 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 Hellwigf0c6bcb2016-06-21 09:52:47 +1000887 * 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 Hellwigc4ed4242011-07-08 14:34:23 +0200890 */
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000891 if (newsize > oldsize) {
Christoph Hellwigf5c547172018-03-13 23:15:32 -0700892 trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
Shiyang Ruanf1ba5fa2021-11-29 11:21:49 +0100893 error = xfs_zero_range(ip, oldsize, newsize - oldsize,
894 &did_zeroing);
Christoph Hellwigf0c6bcb2016-06-21 09:52:47 +1000895 } else {
Brian Foster869ae852020-10-29 14:30:48 -0700896 /*
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 Ruanf1ba5fa2021-11-29 11:21:49 +0100906 error = xfs_truncate_page(ip, newsize, &did_zeroing);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200907 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200908
Christoph Hellwigf0c6bcb2016-06-21 09:52:47 +1000909 if (error)
910 return error;
911
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200912 /*
Dave Chinner0f9160b2015-02-23 21:46:58 +1100913 * 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 Chinner49abc3a2014-05-07 08:05:45 +1000919 *
Dave Chinner0f9160b2015-02-23 21:46:58 +1100920 * 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 Hellwig253f4912016-04-06 09:19:55 +1000923 * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
Dave Chinner0f9160b2015-02-23 21:46:58 +1100924 * 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 Guan350976ae2017-11-01 21:43:50 -0700928 *
929 * And we update in-core i_size and truncate page cache beyond newsize
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700930 * 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 Chinner49abc3a2014-05-07 08:05:45 +1000932 */
Dave Chinner49abc3a2014-05-07 08:05:45 +1000933 truncate_setsize(inode, newsize);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200934
Eryu Guan350976ae2017-11-01 21:43:50 -0700935 /*
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 Hellwig13d2c102021-03-29 11:11:40 -0700944 (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) {
Eryu Guan350976ae2017-11-01 21:43:50 -0700945 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700946 ip->i_disk_size, newsize - 1);
Eryu Guan350976ae2017-11-01 21:43:50 -0700947 if (error)
948 return error;
949 }
950
Christoph Hellwig253f4912016-04-06 09:19:55 +1000951 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200952 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000953 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200954
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200955 lock_flags |= XFS_ILOCK_EXCL;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200956 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000957 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200958
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 Hellwigfe60a8a2014-02-10 10:35:22 +1100969 if (newsize != oldsize &&
970 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200971 iattr->ia_ctime = iattr->ia_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700972 current_time(inode);
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +1100973 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200974 }
975
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000976 /*
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 Hellwig13d2c102021-03-29 11:11:40 -0700988 ip->i_disk_size = newsize;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000989 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 Hellwigc4ed4242011-07-08 14:34:23 +0200993 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +1000994 goto out_trans_cancel;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200995
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 Foster27b52862012-11-06 09:50:38 -05001004
1005 /* A truncate down always removes post-EOF blocks. */
1006 xfs_inode_clear_eofblocks_tag(ip);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001007 }
1008
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +11001009 if (iattr->ia_valid & ATTR_MODE)
Christoph Hellwig0c3d88d2013-11-18 05:10:40 -08001010 xfs_setattr_mode(ip, iattr);
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +11001011 if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
Christoph Hellwigc91c46c2013-11-18 05:10:52 -08001012 xfs_setattr_time(ip, iattr);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001013
1014 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1015
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001016 XFS_STATS_INC(mp, xs_ig_attrchg);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001017
Dave Chinner0560f312021-08-18 18:46:52 -07001018 if (xfs_has_wsync(mp))
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001019 xfs_trans_set_sync(tp);
1020
Christoph Hellwig70393312015-06-04 13:48:08 +10001021 error = xfs_trans_commit(tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001022out_unlock:
1023 if (lock_flags)
1024 xfs_iunlock(ip, lock_flags);
1025 return error;
1026
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001027out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001028 xfs_trans_cancel(tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001029 goto out_unlock;
1030}
1031
Jan Kara69bca802016-05-26 14:46:43 +02001032int
1033xfs_vn_setattr_size(
Christoph Hellwigf736d932021-01-21 14:19:58 +01001034 struct user_namespace *mnt_userns,
Jan Kara69bca802016-05-26 14:46:43 +02001035 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 Hellwigf736d932021-01-21 14:19:58 +01001043 error = xfs_vn_change_ok(mnt_userns, dentry, iattr);
Jan Kara69bca802016-05-26 14:46:43 +02001044 if (error)
1045 return error;
Christoph Hellwigf736d932021-01-21 14:19:58 +01001046 return xfs_setattr_size(mnt_userns, ip, iattr);
Jan Kara69bca802016-05-26 14:46:43 +02001047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +11001050xfs_vn_setattr(
Christian Brauner549c7292021-01-21 14:19:43 +01001051 struct user_namespace *mnt_userns,
Christoph Hellwig76ca4c22013-10-14 07:09:35 -07001052 struct dentry *dentry,
1053 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Christoph Hellwig26f88362020-12-10 20:00:38 -08001055 struct inode *inode = d_inode(dentry);
1056 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig76ca4c22013-10-14 07:09:35 -07001057 int error;
1058
1059 if (iattr->ia_valid & ATTR_SIZE) {
Dan Williamsc63a8ea2018-03-12 14:12:29 -07001060 uint iolock;
Dave Chinnere8e9ad42015-02-23 21:45:32 +11001061
Christoph Hellwig65523212016-11-30 14:33:25 +11001062 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
Dan Williamsc63a8ea2018-03-12 14:12:29 -07001063 iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
1064
Dan Williams69eb5fa2018-03-20 14:42:38 -07001065 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
Dan Williamsc63a8ea2018-03-12 14:12:29 -07001066 if (error) {
1067 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1068 return error;
1069 }
1070
Christoph Hellwigf736d932021-01-21 14:19:58 +01001071 error = xfs_vn_setattr_size(mnt_userns, dentry, iattr);
Christoph Hellwig65523212016-11-30 14:33:25 +11001072 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
Christoph Hellwig76ca4c22013-10-14 07:09:35 -07001073 } else {
Christoph Hellwig26f88362020-12-10 20:00:38 -08001074 trace_xfs_setattr(ip);
1075
Christoph Hellwigf736d932021-01-21 14:19:58 +01001076 error = xfs_vn_change_ok(mnt_userns, dentry, iattr);
Christoph Hellwig26f88362020-12-10 20:00:38 -08001077 if (!error)
Christoph Hellwigf736d932021-01-21 14:19:58 +01001078 error = xfs_setattr_nonsize(mnt_userns, ip, iattr);
Christoph Hellwig76ca4c22013-10-14 07:09:35 -07001079 }
1080
Dave Chinner24513372014-06-25 14:58:08 +10001081 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001084STATIC int
1085xfs_vn_update_time(
1086 struct inode *inode,
Deepa Dinamani95582b02018-05-08 19:36:02 -07001087 struct timespec64 *now,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001088 int flags)
1089{
1090 struct xfs_inode *ip = XFS_I(inode);
1091 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigc3b1b132018-03-06 17:04:00 -08001092 int log_flags = XFS_ILOG_TIMESTAMP;
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001093 struct xfs_trans *tp;
1094 int error;
1095
1096 trace_xfs_update_time(ip);
1097
Christoph Hellwigc3b1b132018-03-06 17:04:00 -08001098 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 Hellwig253f4912016-04-06 09:19:55 +10001107 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1108 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10001109 return error;
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001110
1111 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner39878482016-02-09 16:54:58 +11001112 if (flags & S_CTIME)
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001113 inode->i_ctime = *now;
Dave Chinner39878482016-02-09 16:54:58 +11001114 if (flags & S_MTIME)
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001115 inode->i_mtime = *now;
Dave Chinner39878482016-02-09 16:54:58 +11001116 if (flags & S_ATIME)
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001117 inode->i_atime = *now;
Dave Chinner39878482016-02-09 16:54:58 +11001118
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001119 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Christoph Hellwigc3b1b132018-03-06 17:04:00 -08001120 xfs_trans_log_inode(tp, ip, log_flags);
Christoph Hellwig70393312015-06-04 13:48:08 +10001121 return xfs_trans_commit(tp);
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001122}
1123
Eric Sandeenf35642e2008-11-28 14:23:35 +11001124STATIC int
1125xfs_vn_fiemap(
1126 struct inode *inode,
1127 struct fiemap_extent_info *fieinfo,
1128 u64 start,
1129 u64 length)
1130{
Eric Sandeenf35642e2008-11-28 14:23:35 +11001131 int error;
1132
Christoph Hellwigd2bb1402016-06-21 09:54:53 +10001133 xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001134 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 Hellwig690c2a32019-10-19 09:09:45 -07001140 &xfs_read_iomap_ops);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001141 }
Christoph Hellwigd2bb1402016-06-21 09:54:53 +10001142 xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
Eric Sandeenf35642e2008-11-28 14:23:35 +11001143
Christoph Hellwigd2bb1402016-06-21 09:54:53 +10001144 return error;
Eric Sandeenf35642e2008-11-28 14:23:35 +11001145}
1146
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001147STATIC int
1148xfs_vn_tmpfile(
Christian Brauner549c7292021-01-21 14:19:43 +01001149 struct user_namespace *mnt_userns,
1150 struct inode *dir,
1151 struct dentry *dentry,
1152 umode_t mode)
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001153{
Christoph Hellwigf736d932021-01-21 14:19:58 +01001154 return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, true);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001155}
1156
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001157static const struct inode_operations xfs_inode_operations = {
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001158 .get_acl = xfs_get_acl,
Christoph Hellwig2401dc22013-12-20 05:16:50 -08001159 .set_acl = xfs_set_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +11001160 .getattr = xfs_vn_getattr,
1161 .setattr = xfs_vn_setattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001162 .listxattr = xfs_vn_listxattr,
Eric Sandeenf35642e2008-11-28 14:23:35 +11001163 .fiemap = xfs_vn_fiemap,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001164 .update_time = xfs_vn_update_time,
Miklos Szeredi9fefd5d2021-04-07 14:36:43 +02001165 .fileattr_get = xfs_fileattr_get,
1166 .fileattr_set = xfs_fileattr_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167};
1168
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001169static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +11001170 .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 Hellwig8f112e32008-06-23 13:25:17 +10001176 /*
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 Scott416c6d52006-03-14 14:00:51 +11001183 .mknod = xfs_vn_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001184 .rename = xfs_vn_rename,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001185 .get_acl = xfs_get_acl,
Christoph Hellwig2401dc22013-12-20 05:16:50 -08001186 .set_acl = xfs_set_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +11001187 .getattr = xfs_vn_getattr,
1188 .setattr = xfs_vn_setattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001189 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001190 .update_time = xfs_vn_update_time,
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001191 .tmpfile = xfs_vn_tmpfile,
Miklos Szeredi9fefd5d2021-04-07 14:36:43 +02001192 .fileattr_get = xfs_fileattr_get,
1193 .fileattr_set = xfs_fileattr_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194};
1195
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001196static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +10001197 .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 Hellwig8f112e32008-06-23 13:25:17 +10001203 /*
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 Naujok384f3ce2008-05-21 16:58:22 +10001210 .mknod = xfs_vn_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001211 .rename = xfs_vn_rename,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001212 .get_acl = xfs_get_acl,
Christoph Hellwig2401dc22013-12-20 05:16:50 -08001213 .set_acl = xfs_set_acl,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001214 .getattr = xfs_vn_getattr,
1215 .setattr = xfs_vn_setattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001216 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001217 .update_time = xfs_vn_update_time,
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001218 .tmpfile = xfs_vn_tmpfile,
Miklos Szeredi9fefd5d2021-04-07 14:36:43 +02001219 .fileattr_get = xfs_fileattr_get,
1220 .fileattr_set = xfs_fileattr_set,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001221};
1222
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001223static const struct inode_operations xfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001224 .get_link = xfs_vn_get_link,
Nathan Scott416c6d52006-03-14 14:00:51 +11001225 .getattr = xfs_vn_getattr,
1226 .setattr = xfs_vn_setattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001227 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001228 .update_time = xfs_vn_update_time,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229};
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001230
Darrick J. Wongba23cba2018-05-30 13:03:45 -07001231/* Figure out if this file actually supports DAX. */
1232static bool
1233xfs_inode_supports_dax(
1234 struct xfs_inode *ip)
1235{
1236 struct xfs_mount *mp = ip->i_mount;
1237
Ira Weiny32dbc562020-05-04 09:02:42 -07001238 /* Only supported on regular files. */
1239 if (!S_ISREG(VFS_I(ip)->i_mode))
Darrick J. Wongba23cba2018-05-30 13:03:45 -07001240 return false;
1241
Ira Weiny32dbc562020-05-04 09:02:42 -07001242 /* Only supported on non-reflinked files. */
1243 if (xfs_is_reflink_inode(ip))
Darrick J. Wongba23cba2018-05-30 13:03:45 -07001244 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 Hellwig30fa5292019-10-24 22:25:38 -07001251 return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
Darrick J. Wongba23cba2018-05-30 13:03:45 -07001252}
1253
Ira Weiny32dbc562020-05-04 09:02:42 -07001254static bool
1255xfs_inode_should_enable_dax(
1256 struct xfs_inode *ip)
1257{
1258 if (!IS_ENABLED(CONFIG_FS_DAX))
1259 return false;
Dave Chinner0560f312021-08-18 18:46:52 -07001260 if (xfs_has_dax_never(ip->i_mount))
Ira Weiny32dbc562020-05-04 09:02:42 -07001261 return false;
1262 if (!xfs_inode_supports_dax(ip))
1263 return false;
Dave Chinner0560f312021-08-18 18:46:52 -07001264 if (xfs_has_dax_always(ip->i_mount))
Ira Weiny32dbc562020-05-04 09:02:42 -07001265 return true;
Christoph Hellwig3e09ab82021-03-29 11:11:45 -07001266 if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
Ira Weiny32dbc562020-05-04 09:02:42 -07001267 return true;
1268 return false;
1269}
1270
Ira Weiny840d4932020-05-04 09:02:43 -07001271void
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001272xfs_diflags_to_iflags(
Ira Weiny840d4932020-05-04 09:02:43 -07001273 struct xfs_inode *ip,
1274 bool init)
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001275{
Ira Weiny840d4932020-05-04 09:02:43 -07001276 struct inode *inode = VFS_I(ip);
1277 unsigned int xflags = xfs_ip2xflags(ip);
1278 unsigned int flags = 0;
Dave Chinnercbe4dab2015-06-04 09:19:18 +10001279
Ira Weiny840d4932020-05-04 09:02:43 -07001280 ASSERT(!(IS_DAX(inode) && init));
Dave Chinnercbe4dab2015-06-04 09:19:18 +10001281
Ira Weiny840d4932020-05-04 09:02:43 -07001282 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 Hellwig41be8be2008-08-13 16:23:13 +10001299}
1300
1301/*
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001302 * Initialize the Linux inode.
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001303 *
Dave Chinner58c90472015-02-23 22:38:08 +11001304 * When reading existing inodes from disk this is called directly from xfs_iget,
Yang Xu132c4602021-12-21 09:38:19 -08001305 * 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 Hellwig41be8be2008-08-13 16:23:13 +10001308 */
1309void
1310xfs_setup_inode(
1311 struct xfs_inode *ip)
1312{
David Chinnerbf904242008-10-30 17:36:14 +11001313 struct inode *inode = &ip->i_vnode;
Dave Chinnerad22c7a2013-10-29 22:11:57 +11001314 gfp_t gfp_mask;
David Chinnerbf904242008-10-30 17:36:14 +11001315
1316 inode->i_ino = ip->i_ino;
Dave Chinnerf38a0322021-08-24 19:13:04 -07001317 inode->i_state |= I_NEW;
Christoph Hellwig646ec462010-10-23 07:15:32 -04001318
1319 inode_sb_list_add(inode);
Christoph Hellwigc6f6cd02010-11-06 11:43:08 +00001320 /* make the inode look hashed for the writeback code */
Al Viro5bef9152018-06-29 19:36:57 -04001321 inode_fake_hash(inode);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001322
Christoph Hellwig13d2c102021-03-29 11:11:40 -07001323 i_size_write(inode, ip->i_disk_size);
Ira Weiny840d4932020-05-04 09:02:43 -07001324 xfs_diflags_to_iflags(ip, true);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001325
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001326 if (S_ISDIR(inode->i_mode)) {
Dave Chinneref215e32018-06-07 07:36:08 -07001327 /*
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 Chinner93a86142014-02-27 16:51:39 +11001335 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001336 } else {
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001337 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001338 }
1339
Christoph Hellwig510792e2011-07-26 15:07:29 +00001340 /*
Dave Chinnerad22c7a2013-10-29 22:11:57 +11001341 * 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 Hellwig510792e2011-07-26 15:07:29 +00001349 * 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 Hellwig6311b102011-07-23 17:36:50 +02001354 cache_no_acl(inode);
Christoph Hellwig510792e2011-07-26 15:07:29 +00001355 }
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001356}
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001357
1358void
1359xfs_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 Williams6e2608d2018-03-07 15:26:44 -08001368 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 Hellwig2b3d1d42016-04-06 07:48:27 +10001372 break;
1373 case S_IFDIR:
Dave Chinner38c26bf2021-08-18 18:46:37 -07001374 if (xfs_has_asciici(XFS_M(inode->i_sb)))
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001375 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. Wong7b7820b2021-12-15 12:07:41 -08001381 inode->i_op = &xfs_symlink_inode_operations;
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001382 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}