xref: /openbmc/linux/fs/xfs/xfs_iops.c (revision 55e43d6abd078ed6d219902ce8cb4d68e3c993ba)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
2c59d87c4SChristoph Hellwig /*
3c59d87c4SChristoph Hellwig  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4c59d87c4SChristoph Hellwig  * All Rights Reserved.
5c59d87c4SChristoph Hellwig  */
6c59d87c4SChristoph Hellwig #include "xfs.h"
7c59d87c4SChristoph Hellwig #include "xfs_fs.h"
870a9883cSDave Chinner #include "xfs_shared.h"
9239880efSDave Chinner #include "xfs_format.h"
10239880efSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
12c59d87c4SChristoph Hellwig #include "xfs_mount.h"
13c59d87c4SChristoph Hellwig #include "xfs_inode.h"
14239880efSDave Chinner #include "xfs_acl.h"
15239880efSDave Chinner #include "xfs_quota.h"
16fd920008SAllison Henderson #include "xfs_da_format.h"
17fd920008SAllison Henderson #include "xfs_da_btree.h"
18c59d87c4SChristoph Hellwig #include "xfs_attr.h"
19239880efSDave Chinner #include "xfs_trans.h"
20c59d87c4SChristoph Hellwig #include "xfs_trace.h"
2127b52867SBrian Foster #include "xfs_icache.h"
22c24b5dfaSDave Chinner #include "xfs_symlink.h"
231b767ee3SDave Chinner #include "xfs_dir2.h"
2468a9f5e7SChristoph Hellwig #include "xfs_iomap.h"
25a5155b87SDarrick J. Wong #include "xfs_error.h"
269fefd5dbSMiklos Szeredi #include "xfs_ioctl.h"
27efc2efebSDarrick J. Wong #include "xfs_xattr.h"
28*7531c9abSDarrick J. Wong #include "xfs_file.h"
29c59d87c4SChristoph Hellwig 
30c59d87c4SChristoph Hellwig #include <linux/posix_acl.h>
31c59d87c4SChristoph Hellwig #include <linux/security.h>
32c3b1b131SChristoph Hellwig #include <linux/iversion.h>
3310c5db28SChristoph Hellwig #include <linux/fiemap.h>
34c59d87c4SChristoph Hellwig 
3593a8614eSDave Chinner /*
36c1e8d7c6SMichel Lespinasse  * Directories have different lock order w.r.t. mmap_lock compared to regular
3793a8614eSDave Chinner  * files. This is due to readdir potentially triggering page faults on a user
3893a8614eSDave Chinner  * buffer inside filldir(), and this happens with the ilock on the directory
3993a8614eSDave Chinner  * held. For regular files, the lock order is the other way around - the
40c1e8d7c6SMichel Lespinasse  * mmap_lock is taken during the page fault, and then we lock the ilock to do
4193a8614eSDave Chinner  * block mapping. Hence we need a different class for the directory ilock so
4293a8614eSDave Chinner  * that lockdep can tell them apart.
4393a8614eSDave Chinner  */
4493a8614eSDave Chinner static struct lock_class_key xfs_nondir_ilock_class;
4593a8614eSDave Chinner static struct lock_class_key xfs_dir_ilock_class;
4693a8614eSDave Chinner 
478d2a5e6eSDave Chinner static int
xfs_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * fs_info)488d2a5e6eSDave Chinner xfs_initxattrs(
498d2a5e6eSDave Chinner 	struct inode		*inode,
508d2a5e6eSDave Chinner 	const struct xattr	*xattr_array,
5136b8d186SLinus Torvalds 	void			*fs_info)
5236b8d186SLinus Torvalds {
5336b8d186SLinus Torvalds 	const struct xattr	*xattr;
5436b8d186SLinus Torvalds 	struct xfs_inode	*ip = XFS_I(inode);
5536b8d186SLinus Torvalds 	int			error = 0;
5636b8d186SLinus Torvalds 
5736b8d186SLinus Torvalds 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
58a2544622SChristoph Hellwig 		struct xfs_da_args	args = {
59a2544622SChristoph Hellwig 			.dp		= ip,
60d5f0f49aSChristoph Hellwig 			.attr_filter	= XFS_ATTR_SECURE,
61a2544622SChristoph Hellwig 			.name		= xattr->name,
62a2544622SChristoph Hellwig 			.namelen	= strlen(xattr->name),
63a2544622SChristoph Hellwig 			.value		= xattr->value,
64a2544622SChristoph Hellwig 			.valuelen	= xattr->value_len,
65a2544622SChristoph Hellwig 		};
66efc2efebSDarrick J. Wong 		error = xfs_attr_change(&args);
6736b8d186SLinus Torvalds 		if (error < 0)
6836b8d186SLinus Torvalds 			break;
6936b8d186SLinus Torvalds 	}
7036b8d186SLinus Torvalds 	return error;
7136b8d186SLinus Torvalds }
7236b8d186SLinus Torvalds 
73c59d87c4SChristoph Hellwig /*
74c59d87c4SChristoph Hellwig  * Hook in SELinux.  This is not quite correct yet, what we really need
75c59d87c4SChristoph Hellwig  * here (as we do for default ACLs) is a mechanism by which creation of
76c59d87c4SChristoph Hellwig  * these attrs can be journalled at inode creation time (along with the
77c59d87c4SChristoph Hellwig  * inode, of course, such that log replay can't cause these to be lost).
78c59d87c4SChristoph Hellwig  */
7970b589a3SEric Sandeen int
xfs_inode_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr)8070b589a3SEric Sandeen xfs_inode_init_security(
81c59d87c4SChristoph Hellwig 	struct inode	*inode,
82c59d87c4SChristoph Hellwig 	struct inode	*dir,
83c59d87c4SChristoph Hellwig 	const struct qstr *qstr)
84c59d87c4SChristoph Hellwig {
852451337dSDave Chinner 	return security_inode_init_security(inode, dir, qstr,
8636b8d186SLinus Torvalds 					     &xfs_initxattrs, NULL);
87c59d87c4SChristoph Hellwig }
88c59d87c4SChristoph Hellwig 
89c59d87c4SChristoph Hellwig static void
xfs_dentry_to_name(struct xfs_name * namep,struct dentry * dentry)90c59d87c4SChristoph Hellwig xfs_dentry_to_name(
91c59d87c4SChristoph Hellwig 	struct xfs_name	*namep,
92fab8eef8SAmir Goldstein 	struct dentry	*dentry)
93fab8eef8SAmir Goldstein {
94fab8eef8SAmir Goldstein 	namep->name = dentry->d_name.name;
95fab8eef8SAmir Goldstein 	namep->len = dentry->d_name.len;
96fab8eef8SAmir Goldstein 	namep->type = XFS_DIR3_FT_UNKNOWN;
97fab8eef8SAmir Goldstein }
98fab8eef8SAmir Goldstein 
99fab8eef8SAmir Goldstein static int
xfs_dentry_mode_to_name(struct xfs_name * namep,struct dentry * dentry,int mode)100fab8eef8SAmir Goldstein xfs_dentry_mode_to_name(
101fab8eef8SAmir Goldstein 	struct xfs_name	*namep,
1020cb97766SDave Chinner 	struct dentry	*dentry,
1030cb97766SDave Chinner 	int		mode)
104c59d87c4SChristoph Hellwig {
105c59d87c4SChristoph Hellwig 	namep->name = dentry->d_name.name;
106c59d87c4SChristoph Hellwig 	namep->len = dentry->d_name.len;
1071fc4d33fSAmir Goldstein 	namep->type = xfs_mode_to_ftype(mode);
108fab8eef8SAmir Goldstein 
109fab8eef8SAmir Goldstein 	if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
110fab8eef8SAmir Goldstein 		return -EFSCORRUPTED;
111fab8eef8SAmir Goldstein 
112fab8eef8SAmir Goldstein 	return 0;
113c59d87c4SChristoph Hellwig }
114c59d87c4SChristoph Hellwig 
115c59d87c4SChristoph Hellwig STATIC void
xfs_cleanup_inode(struct inode * dir,struct inode * inode,struct dentry * dentry)116c59d87c4SChristoph Hellwig xfs_cleanup_inode(
117c59d87c4SChristoph Hellwig 	struct inode	*dir,
118c59d87c4SChristoph Hellwig 	struct inode	*inode,
119c59d87c4SChristoph Hellwig 	struct dentry	*dentry)
120c59d87c4SChristoph Hellwig {
121c59d87c4SChristoph Hellwig 	struct xfs_name	teardown;
122c59d87c4SChristoph Hellwig 
123c59d87c4SChristoph Hellwig 	/* Oh, the horror.
124c59d87c4SChristoph Hellwig 	 * If we can't add the ACL or we fail in
12570b589a3SEric Sandeen 	 * xfs_inode_init_security we must back out.
126c59d87c4SChristoph Hellwig 	 * ENOSPC can hit here, among other things.
127c59d87c4SChristoph Hellwig 	 */
128fab8eef8SAmir Goldstein 	xfs_dentry_to_name(&teardown, dentry);
129c59d87c4SChristoph Hellwig 
130c59d87c4SChristoph Hellwig 	xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
131c59d87c4SChristoph Hellwig }
132c59d87c4SChristoph Hellwig 
133e6a688c3SDave Chinner /*
134e6a688c3SDave Chinner  * Check to see if we are likely to need an extended attribute to be added to
135e6a688c3SDave Chinner  * the inode we are about to allocate. This allows the attribute fork to be
136e6a688c3SDave Chinner  * created during the inode allocation, reducing the number of transactions we
137e6a688c3SDave Chinner  * need to do in this fast path.
138e6a688c3SDave Chinner  *
139e6a688c3SDave Chinner  * The security checks are optimistic, but not guaranteed. The two LSMs that
140e6a688c3SDave Chinner  * require xattrs to be added here (selinux and smack) are also the only two
141e6a688c3SDave Chinner  * LSMs that add a sb->s_security structure to the superblock. Hence if security
142e6a688c3SDave Chinner  * is enabled and sb->s_security is set, we have a pretty good idea that we are
143e6a688c3SDave Chinner  * going to be asked to add a security xattr immediately after allocating the
144e6a688c3SDave Chinner  * xfs inode and instantiating the VFS inode.
145e6a688c3SDave Chinner  */
146e6a688c3SDave Chinner static inline bool
xfs_create_need_xattr(struct inode * dir,struct posix_acl * default_acl,struct posix_acl * acl)147e6a688c3SDave Chinner xfs_create_need_xattr(
148e6a688c3SDave Chinner 	struct inode	*dir,
149e6a688c3SDave Chinner 	struct posix_acl *default_acl,
150e6a688c3SDave Chinner 	struct posix_acl *acl)
151e6a688c3SDave Chinner {
152e6a688c3SDave Chinner 	if (acl)
153e6a688c3SDave Chinner 		return true;
154e6a688c3SDave Chinner 	if (default_acl)
155e6a688c3SDave Chinner 		return true;
156e6a688c3SDave Chinner #if IS_ENABLED(CONFIG_SECURITY)
157e6a688c3SDave Chinner 	if (dir->i_sb->s_security)
158e6a688c3SDave Chinner 		return true;
159e6a688c3SDave Chinner #endif
160e6a688c3SDave Chinner 	return false;
161e6a688c3SDave Chinner }
162e6a688c3SDave Chinner 
163e6a688c3SDave Chinner 
164c59d87c4SChristoph Hellwig STATIC int
xfs_generic_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev,struct file * tmpfile)165d540e43bSBrian Foster xfs_generic_create(
166f2d40141SChristian Brauner 	struct mnt_idmap	*idmap,
167c59d87c4SChristoph Hellwig 	struct inode		*dir,
168c59d87c4SChristoph Hellwig 	struct dentry		*dentry,
1691a67aafbSAl Viro 	umode_t			mode,
170d540e43bSBrian Foster 	dev_t			rdev,
171863f144fSMiklos Szeredi 	struct file		*tmpfile)	/* unnamed file */
172c59d87c4SChristoph Hellwig {
173c59d87c4SChristoph Hellwig 	struct inode	*inode;
174c59d87c4SChristoph Hellwig 	struct xfs_inode *ip = NULL;
1752401dc29SChristoph Hellwig 	struct posix_acl *default_acl, *acl;
176c59d87c4SChristoph Hellwig 	struct xfs_name	name;
177c59d87c4SChristoph Hellwig 	int		error;
178c59d87c4SChristoph Hellwig 
179c59d87c4SChristoph Hellwig 	/*
180c59d87c4SChristoph Hellwig 	 * Irix uses Missed'em'V split, but doesn't want to see
181c59d87c4SChristoph Hellwig 	 * the upper 5 bits of (14bit) major.
182c59d87c4SChristoph Hellwig 	 */
183c59d87c4SChristoph Hellwig 	if (S_ISCHR(mode) || S_ISBLK(mode)) {
184c59d87c4SChristoph Hellwig 		if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
185c59d87c4SChristoph Hellwig 			return -EINVAL;
186c59d87c4SChristoph Hellwig 	} else {
187c59d87c4SChristoph Hellwig 		rdev = 0;
188c59d87c4SChristoph Hellwig 	}
189c59d87c4SChristoph Hellwig 
1902401dc29SChristoph Hellwig 	error = posix_acl_create(dir, &mode, &default_acl, &acl);
1912401dc29SChristoph Hellwig 	if (error)
1922401dc29SChristoph Hellwig 		return error;
193c59d87c4SChristoph Hellwig 
194fab8eef8SAmir Goldstein 	/* Verify mode is valid also for tmpfile case */
195fab8eef8SAmir Goldstein 	error = xfs_dentry_mode_to_name(&name, dentry, mode);
196fab8eef8SAmir Goldstein 	if (unlikely(error))
197fab8eef8SAmir Goldstein 		goto out_free_acl;
198fab8eef8SAmir Goldstein 
199d540e43bSBrian Foster 	if (!tmpfile) {
200f2d40141SChristian Brauner 		error = xfs_create(idmap, XFS_I(dir), &name, mode, rdev,
201e6a688c3SDave Chinner 				xfs_create_need_xattr(dir, default_acl, acl),
202f736d93dSChristoph Hellwig 				&ip);
203d540e43bSBrian Foster 	} else {
204f2d40141SChristian Brauner 		error = xfs_create_tmpfile(idmap, XFS_I(dir), mode, &ip);
205d540e43bSBrian Foster 	}
206c59d87c4SChristoph Hellwig 	if (unlikely(error))
207c59d87c4SChristoph Hellwig 		goto out_free_acl;
208c59d87c4SChristoph Hellwig 
209c59d87c4SChristoph Hellwig 	inode = VFS_I(ip);
210c59d87c4SChristoph Hellwig 
21170b589a3SEric Sandeen 	error = xfs_inode_init_security(inode, dir, &dentry->d_name);
212c59d87c4SChristoph Hellwig 	if (unlikely(error))
213c59d87c4SChristoph Hellwig 		goto out_cleanup_inode;
214c59d87c4SChristoph Hellwig 
215c59d87c4SChristoph Hellwig 	if (default_acl) {
2168ba35875SJan Kara 		error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
2172401dc29SChristoph Hellwig 		if (error)
218c59d87c4SChristoph Hellwig 			goto out_cleanup_inode;
219c59d87c4SChristoph Hellwig 	}
2202401dc29SChristoph Hellwig 	if (acl) {
2218ba35875SJan Kara 		error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
2222401dc29SChristoph Hellwig 		if (error)
2232401dc29SChristoph Hellwig 			goto out_cleanup_inode;
2242401dc29SChristoph Hellwig 	}
225c59d87c4SChristoph Hellwig 
2262b3d1d41SChristoph Hellwig 	xfs_setup_iops(ip);
2272b3d1d41SChristoph Hellwig 
228c4a6bf7fSDarrick J. Wong 	if (tmpfile) {
229c4a6bf7fSDarrick J. Wong 		/*
230c4a6bf7fSDarrick J. Wong 		 * The VFS requires that any inode fed to d_tmpfile must have
231c4a6bf7fSDarrick J. Wong 		 * nlink == 1 so that it can decrement the nlink in d_tmpfile.
232c4a6bf7fSDarrick J. Wong 		 * However, we created the temp file with nlink == 0 because
233c4a6bf7fSDarrick J. Wong 		 * we're not allowed to put an inode with nlink > 0 on the
234c4a6bf7fSDarrick J. Wong 		 * unlinked list.  Therefore we have to set nlink to 1 so that
235c4a6bf7fSDarrick J. Wong 		 * d_tmpfile can immediately set it back to zero.
236c4a6bf7fSDarrick J. Wong 		 */
237c4a6bf7fSDarrick J. Wong 		set_nlink(inode, 1);
238863f144fSMiklos Szeredi 		d_tmpfile(tmpfile, inode);
239c4a6bf7fSDarrick J. Wong 	} else
240c59d87c4SChristoph Hellwig 		d_instantiate(dentry, inode);
241d540e43bSBrian Foster 
24258c90473SDave Chinner 	xfs_finish_inode_setup(ip);
24358c90473SDave Chinner 
2442401dc29SChristoph Hellwig  out_free_acl:
2452401dc29SChristoph Hellwig 	posix_acl_release(default_acl);
2462401dc29SChristoph Hellwig 	posix_acl_release(acl);
2472451337dSDave Chinner 	return error;
248c59d87c4SChristoph Hellwig 
249c59d87c4SChristoph Hellwig  out_cleanup_inode:
25058c90473SDave Chinner 	xfs_finish_inode_setup(ip);
251d540e43bSBrian Foster 	if (!tmpfile)
252c59d87c4SChristoph Hellwig 		xfs_cleanup_inode(dir, inode, dentry);
25344a8736bSDarrick J. Wong 	xfs_irele(ip);
2542401dc29SChristoph Hellwig 	goto out_free_acl;
255c59d87c4SChristoph Hellwig }
256c59d87c4SChristoph Hellwig 
257c59d87c4SChristoph Hellwig STATIC int
xfs_vn_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)258d540e43bSBrian Foster xfs_vn_mknod(
2595ebb29beSChristian Brauner 	struct mnt_idmap	*idmap,
260d540e43bSBrian Foster 	struct inode		*dir,
261d540e43bSBrian Foster 	struct dentry		*dentry,
262d540e43bSBrian Foster 	umode_t			mode,
263d540e43bSBrian Foster 	dev_t			rdev)
264d540e43bSBrian Foster {
265f2d40141SChristian Brauner 	return xfs_generic_create(idmap, dir, dentry, mode, rdev, NULL);
266d540e43bSBrian Foster }
267d540e43bSBrian Foster 
268d540e43bSBrian Foster STATIC int
xfs_vn_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool flags)269c59d87c4SChristoph Hellwig xfs_vn_create(
2706c960e68SChristian Brauner 	struct mnt_idmap	*idmap,
271c59d87c4SChristoph Hellwig 	struct inode		*dir,
272c59d87c4SChristoph Hellwig 	struct dentry		*dentry,
2734acdaf27SAl Viro 	umode_t			mode,
274ebfc3b49SAl Viro 	bool			flags)
275c59d87c4SChristoph Hellwig {
276f2d40141SChristian Brauner 	return xfs_generic_create(idmap, dir, dentry, mode, 0, NULL);
277c59d87c4SChristoph Hellwig }
278c59d87c4SChristoph Hellwig 
279c59d87c4SChristoph Hellwig STATIC int
xfs_vn_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)280c59d87c4SChristoph Hellwig xfs_vn_mkdir(
281c54bd91eSChristian Brauner 	struct mnt_idmap	*idmap,
282c59d87c4SChristoph Hellwig 	struct inode		*dir,
283c59d87c4SChristoph Hellwig 	struct dentry		*dentry,
28418bb1db3SAl Viro 	umode_t			mode)
285c59d87c4SChristoph Hellwig {
286f2d40141SChristian Brauner 	return xfs_generic_create(idmap, dir, dentry, mode | S_IFDIR, 0, NULL);
287c59d87c4SChristoph Hellwig }
288c59d87c4SChristoph Hellwig 
289c59d87c4SChristoph Hellwig STATIC struct dentry *
xfs_vn_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)290c59d87c4SChristoph Hellwig xfs_vn_lookup(
291c59d87c4SChristoph Hellwig 	struct inode	*dir,
292c59d87c4SChristoph Hellwig 	struct dentry	*dentry,
29300cd8dd3SAl Viro 	unsigned int flags)
294c59d87c4SChristoph Hellwig {
295b113a6d3SAl Viro 	struct inode *inode;
296c59d87c4SChristoph Hellwig 	struct xfs_inode *cip;
297c59d87c4SChristoph Hellwig 	struct xfs_name	name;
298c59d87c4SChristoph Hellwig 	int		error;
299c59d87c4SChristoph Hellwig 
300c59d87c4SChristoph Hellwig 	if (dentry->d_name.len >= MAXNAMELEN)
301c59d87c4SChristoph Hellwig 		return ERR_PTR(-ENAMETOOLONG);
302c59d87c4SChristoph Hellwig 
303fab8eef8SAmir Goldstein 	xfs_dentry_to_name(&name, dentry);
304c59d87c4SChristoph Hellwig 	error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
305b113a6d3SAl Viro 	if (likely(!error))
306b113a6d3SAl Viro 		inode = VFS_I(cip);
307b113a6d3SAl Viro 	else if (likely(error == -ENOENT))
308b113a6d3SAl Viro 		inode = NULL;
309b113a6d3SAl Viro 	else
310b113a6d3SAl Viro 		inode = ERR_PTR(error);
311b113a6d3SAl Viro 	return d_splice_alias(inode, dentry);
312c59d87c4SChristoph Hellwig }
313c59d87c4SChristoph Hellwig 
314c59d87c4SChristoph Hellwig STATIC struct dentry *
xfs_vn_ci_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)315c59d87c4SChristoph Hellwig xfs_vn_ci_lookup(
316c59d87c4SChristoph Hellwig 	struct inode	*dir,
317c59d87c4SChristoph Hellwig 	struct dentry	*dentry,
31800cd8dd3SAl Viro 	unsigned int flags)
319c59d87c4SChristoph Hellwig {
320c59d87c4SChristoph Hellwig 	struct xfs_inode *ip;
321c59d87c4SChristoph Hellwig 	struct xfs_name	xname;
322c59d87c4SChristoph Hellwig 	struct xfs_name ci_name;
323c59d87c4SChristoph Hellwig 	struct qstr	dname;
324c59d87c4SChristoph Hellwig 	int		error;
325c59d87c4SChristoph Hellwig 
326c59d87c4SChristoph Hellwig 	if (dentry->d_name.len >= MAXNAMELEN)
327c59d87c4SChristoph Hellwig 		return ERR_PTR(-ENAMETOOLONG);
328c59d87c4SChristoph Hellwig 
329fab8eef8SAmir Goldstein 	xfs_dentry_to_name(&xname, dentry);
330c59d87c4SChristoph Hellwig 	error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
331c59d87c4SChristoph Hellwig 	if (unlikely(error)) {
3322451337dSDave Chinner 		if (unlikely(error != -ENOENT))
3332451337dSDave Chinner 			return ERR_PTR(error);
334c59d87c4SChristoph Hellwig 		/*
335c59d87c4SChristoph Hellwig 		 * call d_add(dentry, NULL) here when d_drop_negative_children
336c59d87c4SChristoph Hellwig 		 * is called in xfs_vn_mknod (ie. allow negative dentries
337c59d87c4SChristoph Hellwig 		 * with CI filesystems).
338c59d87c4SChristoph Hellwig 		 */
339c59d87c4SChristoph Hellwig 		return NULL;
340c59d87c4SChristoph Hellwig 	}
341c59d87c4SChristoph Hellwig 
342c59d87c4SChristoph Hellwig 	/* if exact match, just splice and exit */
343c59d87c4SChristoph Hellwig 	if (!ci_name.name)
344c59d87c4SChristoph Hellwig 		return d_splice_alias(VFS_I(ip), dentry);
345c59d87c4SChristoph Hellwig 
346c59d87c4SChristoph Hellwig 	/* else case-insensitive match... */
347c59d87c4SChristoph Hellwig 	dname.name = ci_name.name;
348c59d87c4SChristoph Hellwig 	dname.len = ci_name.len;
349c59d87c4SChristoph Hellwig 	dentry = d_add_ci(dentry, VFS_I(ip), &dname);
350c59d87c4SChristoph Hellwig 	kmem_free(ci_name.name);
351c59d87c4SChristoph Hellwig 	return dentry;
352c59d87c4SChristoph Hellwig }
353c59d87c4SChristoph Hellwig 
354c59d87c4SChristoph Hellwig STATIC int
xfs_vn_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)355c59d87c4SChristoph Hellwig xfs_vn_link(
356c59d87c4SChristoph Hellwig 	struct dentry	*old_dentry,
357c59d87c4SChristoph Hellwig 	struct inode	*dir,
358c59d87c4SChristoph Hellwig 	struct dentry	*dentry)
359c59d87c4SChristoph Hellwig {
3602b0143b5SDavid Howells 	struct inode	*inode = d_inode(old_dentry);
361c59d87c4SChristoph Hellwig 	struct xfs_name	name;
362c59d87c4SChristoph Hellwig 	int		error;
363c59d87c4SChristoph Hellwig 
364fab8eef8SAmir Goldstein 	error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
365fab8eef8SAmir Goldstein 	if (unlikely(error))
366fab8eef8SAmir Goldstein 		return error;
367c59d87c4SChristoph Hellwig 
368c59d87c4SChristoph Hellwig 	error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
369c59d87c4SChristoph Hellwig 	if (unlikely(error))
3702451337dSDave Chinner 		return error;
371c59d87c4SChristoph Hellwig 
372c59d87c4SChristoph Hellwig 	ihold(inode);
373c59d87c4SChristoph Hellwig 	d_instantiate(dentry, inode);
374c59d87c4SChristoph Hellwig 	return 0;
375c59d87c4SChristoph Hellwig }
376c59d87c4SChristoph Hellwig 
377c59d87c4SChristoph Hellwig STATIC int
xfs_vn_unlink(struct inode * dir,struct dentry * dentry)378c59d87c4SChristoph Hellwig xfs_vn_unlink(
379c59d87c4SChristoph Hellwig 	struct inode	*dir,
380c59d87c4SChristoph Hellwig 	struct dentry	*dentry)
381c59d87c4SChristoph Hellwig {
382c59d87c4SChristoph Hellwig 	struct xfs_name	name;
383c59d87c4SChristoph Hellwig 	int		error;
384c59d87c4SChristoph Hellwig 
385fab8eef8SAmir Goldstein 	xfs_dentry_to_name(&name, dentry);
386c59d87c4SChristoph Hellwig 
3872b0143b5SDavid Howells 	error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
388c59d87c4SChristoph Hellwig 	if (error)
389c59d87c4SChristoph Hellwig 		return error;
390c59d87c4SChristoph Hellwig 
391c59d87c4SChristoph Hellwig 	/*
392c59d87c4SChristoph Hellwig 	 * With unlink, the VFS makes the dentry "negative": no inode,
393c59d87c4SChristoph Hellwig 	 * but still hashed. This is incompatible with case-insensitive
394c59d87c4SChristoph Hellwig 	 * mode, so invalidate (unhash) the dentry in CI-mode.
395c59d87c4SChristoph Hellwig 	 */
39638c26bfdSDave Chinner 	if (xfs_has_asciici(XFS_M(dir->i_sb)))
397c59d87c4SChristoph Hellwig 		d_invalidate(dentry);
398c59d87c4SChristoph Hellwig 	return 0;
399c59d87c4SChristoph Hellwig }
400c59d87c4SChristoph Hellwig 
401c59d87c4SChristoph Hellwig STATIC int
xfs_vn_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)402c59d87c4SChristoph Hellwig xfs_vn_symlink(
4037a77db95SChristian Brauner 	struct mnt_idmap	*idmap,
404c59d87c4SChristoph Hellwig 	struct inode		*dir,
405c59d87c4SChristoph Hellwig 	struct dentry		*dentry,
406c59d87c4SChristoph Hellwig 	const char		*symname)
407c59d87c4SChristoph Hellwig {
408c59d87c4SChristoph Hellwig 	struct inode	*inode;
409c59d87c4SChristoph Hellwig 	struct xfs_inode *cip = NULL;
410c59d87c4SChristoph Hellwig 	struct xfs_name	name;
411c59d87c4SChristoph Hellwig 	int		error;
412576b1d67SAl Viro 	umode_t		mode;
413c59d87c4SChristoph Hellwig 
414c59d87c4SChristoph Hellwig 	mode = S_IFLNK |
415c59d87c4SChristoph Hellwig 		(irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
416fab8eef8SAmir Goldstein 	error = xfs_dentry_mode_to_name(&name, dentry, mode);
417fab8eef8SAmir Goldstein 	if (unlikely(error))
418fab8eef8SAmir Goldstein 		goto out;
419c59d87c4SChristoph Hellwig 
420f2d40141SChristian Brauner 	error = xfs_symlink(idmap, XFS_I(dir), &name, symname, mode, &cip);
421c59d87c4SChristoph Hellwig 	if (unlikely(error))
422c59d87c4SChristoph Hellwig 		goto out;
423c59d87c4SChristoph Hellwig 
424c59d87c4SChristoph Hellwig 	inode = VFS_I(cip);
425c59d87c4SChristoph Hellwig 
42670b589a3SEric Sandeen 	error = xfs_inode_init_security(inode, dir, &dentry->d_name);
427c59d87c4SChristoph Hellwig 	if (unlikely(error))
428c59d87c4SChristoph Hellwig 		goto out_cleanup_inode;
429c59d87c4SChristoph Hellwig 
4302b3d1d41SChristoph Hellwig 	xfs_setup_iops(cip);
4312b3d1d41SChristoph Hellwig 
432c59d87c4SChristoph Hellwig 	d_instantiate(dentry, inode);
43358c90473SDave Chinner 	xfs_finish_inode_setup(cip);
434c59d87c4SChristoph Hellwig 	return 0;
435c59d87c4SChristoph Hellwig 
436c59d87c4SChristoph Hellwig  out_cleanup_inode:
43758c90473SDave Chinner 	xfs_finish_inode_setup(cip);
438c59d87c4SChristoph Hellwig 	xfs_cleanup_inode(dir, inode, dentry);
43944a8736bSDarrick J. Wong 	xfs_irele(cip);
440c59d87c4SChristoph Hellwig  out:
4412451337dSDave Chinner 	return error;
442c59d87c4SChristoph Hellwig }
443c59d87c4SChristoph Hellwig 
444c59d87c4SChristoph Hellwig STATIC int
xfs_vn_rename(struct mnt_idmap * idmap,struct inode * odir,struct dentry * odentry,struct inode * ndir,struct dentry * ndentry,unsigned int flags)445c59d87c4SChristoph Hellwig xfs_vn_rename(
446e18275aeSChristian Brauner 	struct mnt_idmap	*idmap,
447c59d87c4SChristoph Hellwig 	struct inode		*odir,
448c59d87c4SChristoph Hellwig 	struct dentry		*odentry,
449c59d87c4SChristoph Hellwig 	struct inode		*ndir,
450dbe1b5caSCarlos Maiolino 	struct dentry		*ndentry,
451dbe1b5caSCarlos Maiolino 	unsigned int		flags)
452c59d87c4SChristoph Hellwig {
4532b0143b5SDavid Howells 	struct inode	*new_inode = d_inode(ndentry);
454d31a1825SCarlos Maiolino 	int		omode = 0;
455fab8eef8SAmir Goldstein 	int		error;
456c59d87c4SChristoph Hellwig 	struct xfs_name	oname;
457c59d87c4SChristoph Hellwig 	struct xfs_name	nname;
458c59d87c4SChristoph Hellwig 
4597dcf5c3eSDave Chinner 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
460dbe1b5caSCarlos Maiolino 		return -EINVAL;
461dbe1b5caSCarlos Maiolino 
462d31a1825SCarlos Maiolino 	/* if we are exchanging files, we need to set i_mode of both files */
463d31a1825SCarlos Maiolino 	if (flags & RENAME_EXCHANGE)
4642b0143b5SDavid Howells 		omode = d_inode(ndentry)->i_mode;
465d31a1825SCarlos Maiolino 
466fab8eef8SAmir Goldstein 	error = xfs_dentry_mode_to_name(&oname, odentry, omode);
467fab8eef8SAmir Goldstein 	if (omode && unlikely(error))
468fab8eef8SAmir Goldstein 		return error;
469fab8eef8SAmir Goldstein 
470fab8eef8SAmir Goldstein 	error = xfs_dentry_mode_to_name(&nname, ndentry,
471fab8eef8SAmir Goldstein 					d_inode(odentry)->i_mode);
472fab8eef8SAmir Goldstein 	if (unlikely(error))
473fab8eef8SAmir Goldstein 		return error;
474c59d87c4SChristoph Hellwig 
475f2d40141SChristian Brauner 	return xfs_rename(idmap, XFS_I(odir), &oname,
476f736d93dSChristoph Hellwig 			  XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
477d31a1825SCarlos Maiolino 			  new_inode ? XFS_I(new_inode) : NULL, flags);
478c59d87c4SChristoph Hellwig }
479c59d87c4SChristoph Hellwig 
480c59d87c4SChristoph Hellwig /*
481c59d87c4SChristoph Hellwig  * careful here - this function can get called recursively, so
482c59d87c4SChristoph Hellwig  * we need to be very careful about how much stack we use.
483c59d87c4SChristoph Hellwig  * uio is kmalloced for this reason...
484c59d87c4SChristoph Hellwig  */
485680baacbSAl Viro STATIC const char *
xfs_vn_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)4866b255391SAl Viro xfs_vn_get_link(
487c59d87c4SChristoph Hellwig 	struct dentry		*dentry,
4886b255391SAl Viro 	struct inode		*inode,
489fceef393SAl Viro 	struct delayed_call	*done)
490c59d87c4SChristoph Hellwig {
491c59d87c4SChristoph Hellwig 	char			*link;
492c59d87c4SChristoph Hellwig 	int			error = -ENOMEM;
493c59d87c4SChristoph Hellwig 
4946b255391SAl Viro 	if (!dentry)
4956b255391SAl Viro 		return ERR_PTR(-ECHILD);
4966b255391SAl Viro 
4976eb0b8dfSDarrick J. Wong 	link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
498c59d87c4SChristoph Hellwig 	if (!link)
499c59d87c4SChristoph Hellwig 		goto out_err;
500c59d87c4SChristoph Hellwig 
5012b0143b5SDavid Howells 	error = xfs_readlink(XFS_I(d_inode(dentry)), link);
502c59d87c4SChristoph Hellwig 	if (unlikely(error))
503c59d87c4SChristoph Hellwig 		goto out_kfree;
504c59d87c4SChristoph Hellwig 
505fceef393SAl Viro 	set_delayed_call(done, kfree_link, link);
506fceef393SAl Viro 	return link;
507c59d87c4SChristoph Hellwig 
508c59d87c4SChristoph Hellwig  out_kfree:
509c59d87c4SChristoph Hellwig 	kfree(link);
510c59d87c4SChristoph Hellwig  out_err:
511680baacbSAl Viro 	return ERR_PTR(error);
512c59d87c4SChristoph Hellwig }
513c59d87c4SChristoph Hellwig 
514dd2d535eSChristoph Hellwig static uint32_t
xfs_stat_blksize(struct xfs_inode * ip)515dd2d535eSChristoph Hellwig xfs_stat_blksize(
516dd2d535eSChristoph Hellwig 	struct xfs_inode	*ip)
517dd2d535eSChristoph Hellwig {
518dd2d535eSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
519dd2d535eSChristoph Hellwig 
520dd2d535eSChristoph Hellwig 	/*
521dd2d535eSChristoph Hellwig 	 * If the file blocks are being allocated from a realtime volume, then
522dd2d535eSChristoph Hellwig 	 * always return the realtime extent size.
523dd2d535eSChristoph Hellwig 	 */
524dd2d535eSChristoph Hellwig 	if (XFS_IS_REALTIME_INODE(ip))
525a7bcb147SDarrick J. Wong 		return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip));
526dd2d535eSChristoph Hellwig 
527dd2d535eSChristoph Hellwig 	/*
528dd2d535eSChristoph Hellwig 	 * Allow large block sizes to be reported to userspace programs if the
529dd2d535eSChristoph Hellwig 	 * "largeio" mount option is used.
530dd2d535eSChristoph Hellwig 	 *
531dd2d535eSChristoph Hellwig 	 * If compatibility mode is specified, simply return the basic unit of
532dd2d535eSChristoph Hellwig 	 * caching so that we don't get inefficient read/modify/write I/O from
533dd2d535eSChristoph Hellwig 	 * user apps. Otherwise....
534dd2d535eSChristoph Hellwig 	 *
535dd2d535eSChristoph Hellwig 	 * If the underlying volume is a stripe, then return the stripe width in
536dd2d535eSChristoph Hellwig 	 * bytes as the recommended I/O size. It is not a stripe and we've set a
537dd2d535eSChristoph Hellwig 	 * default buffered I/O size, return that, otherwise return the compat
538dd2d535eSChristoph Hellwig 	 * default.
539dd2d535eSChristoph Hellwig 	 */
5400560f31aSDave Chinner 	if (xfs_has_large_iosize(mp)) {
541dd2d535eSChristoph Hellwig 		if (mp->m_swidth)
542a7bcb147SDarrick J. Wong 			return XFS_FSB_TO_B(mp, mp->m_swidth);
5430560f31aSDave Chinner 		if (xfs_has_allocsize(mp))
5445da8a07cSChristoph Hellwig 			return 1U << mp->m_allocsize_log;
545dd2d535eSChristoph Hellwig 	}
546dd2d535eSChristoph Hellwig 
547dd2d535eSChristoph Hellwig 	return PAGE_SIZE;
548dd2d535eSChristoph Hellwig }
549dd2d535eSChristoph Hellwig 
550c59d87c4SChristoph Hellwig STATIC int
xfs_vn_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)551c59d87c4SChristoph Hellwig xfs_vn_getattr(
552b74d24f7SChristian Brauner 	struct mnt_idmap	*idmap,
553a528d35eSDavid Howells 	const struct path	*path,
554a528d35eSDavid Howells 	struct kstat		*stat,
555a528d35eSDavid Howells 	u32			request_mask,
556a528d35eSDavid Howells 	unsigned int		query_flags)
557c59d87c4SChristoph Hellwig {
558a528d35eSDavid Howells 	struct inode		*inode = d_inode(path->dentry);
559c59d87c4SChristoph Hellwig 	struct xfs_inode	*ip = XFS_I(inode);
560c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
561e67fe633SChristian Brauner 	vfsuid_t		vfsuid = i_uid_into_vfsuid(idmap, inode);
562e67fe633SChristian Brauner 	vfsgid_t		vfsgid = i_gid_into_vfsgid(idmap, inode);
563c59d87c4SChristoph Hellwig 
564c59d87c4SChristoph Hellwig 	trace_xfs_getattr(ip);
565c59d87c4SChristoph Hellwig 
56675c8c50fSDave Chinner 	if (xfs_is_shutdown(mp))
567b474c7aeSEric Sandeen 		return -EIO;
568c59d87c4SChristoph Hellwig 
569c59d87c4SChristoph Hellwig 	stat->size = XFS_ISIZE(ip);
570c59d87c4SChristoph Hellwig 	stat->dev = inode->i_sb->s_dev;
571c19b3b05SDave Chinner 	stat->mode = inode->i_mode;
57254d7b5c1SDave Chinner 	stat->nlink = inode->i_nlink;
57342b7cc11SChristian Brauner 	stat->uid = vfsuid_into_kuid(vfsuid);
57442b7cc11SChristian Brauner 	stat->gid = vfsgid_into_kgid(vfsgid);
575c59d87c4SChristoph Hellwig 	stat->ino = ip->i_ino;
576c59d87c4SChristoph Hellwig 	stat->atime = inode->i_atime;
577f798accdSChristian Brauner 	stat->mtime = inode->i_mtime;
578f798accdSChristian Brauner 	stat->ctime = inode_get_ctime(inode);
5796e73a545SChristoph Hellwig 	stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks);
580c59d87c4SChristoph Hellwig 
58138c26bfdSDave Chinner 	if (xfs_has_v3inodes(mp)) {
5825f955f26SDarrick J. Wong 		if (request_mask & STATX_BTIME) {
5835f955f26SDarrick J. Wong 			stat->result_mask |= STATX_BTIME;
584e98d5e88SChristoph Hellwig 			stat->btime = ip->i_crtime;
5855f955f26SDarrick J. Wong 		}
5865f955f26SDarrick J. Wong 	}
5875f955f26SDarrick J. Wong 
588cbc06310SJeff Layton 	if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
589cbc06310SJeff Layton 		stat->change_cookie = inode_query_iversion(inode);
590cbc06310SJeff Layton 		stat->result_mask |= STATX_CHANGE_COOKIE;
591cbc06310SJeff Layton 	}
592cbc06310SJeff Layton 
5931b9598c8SLuis R. Rodriguez 	/*
5941b9598c8SLuis R. Rodriguez 	 * Note: If you add another clause to set an attribute flag, please
5951b9598c8SLuis R. Rodriguez 	 * update attributes_mask below.
5961b9598c8SLuis R. Rodriguez 	 */
597db07349dSChristoph Hellwig 	if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
5985f955f26SDarrick J. Wong 		stat->attributes |= STATX_ATTR_IMMUTABLE;
599db07349dSChristoph Hellwig 	if (ip->i_diflags & XFS_DIFLAG_APPEND)
6005f955f26SDarrick J. Wong 		stat->attributes |= STATX_ATTR_APPEND;
601db07349dSChristoph Hellwig 	if (ip->i_diflags & XFS_DIFLAG_NODUMP)
6025f955f26SDarrick J. Wong 		stat->attributes |= STATX_ATTR_NODUMP;
603c59d87c4SChristoph Hellwig 
6041b9598c8SLuis R. Rodriguez 	stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
6051b9598c8SLuis R. Rodriguez 				  STATX_ATTR_APPEND |
6061b9598c8SLuis R. Rodriguez 				  STATX_ATTR_NODUMP);
6071b9598c8SLuis R. Rodriguez 
608c59d87c4SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
609c59d87c4SChristoph Hellwig 	case S_IFBLK:
610c59d87c4SChristoph Hellwig 	case S_IFCHR:
611c59d87c4SChristoph Hellwig 		stat->blksize = BLKDEV_IOSIZE;
61266f36464SChristoph Hellwig 		stat->rdev = inode->i_rdev;
613c59d87c4SChristoph Hellwig 		break;
61461a223dfSEric Biggers 	case S_IFREG:
61561a223dfSEric Biggers 		if (request_mask & STATX_DIOALIGN) {
61661a223dfSEric Biggers 			struct xfs_buftarg	*target = xfs_inode_buftarg(ip);
61761a223dfSEric Biggers 			struct block_device	*bdev = target->bt_bdev;
61861a223dfSEric Biggers 
61961a223dfSEric Biggers 			stat->result_mask |= STATX_DIOALIGN;
62061a223dfSEric Biggers 			stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
62161a223dfSEric Biggers 			stat->dio_offset_align = bdev_logical_block_size(bdev);
62261a223dfSEric Biggers 		}
62361a223dfSEric Biggers 		fallthrough;
624c59d87c4SChristoph Hellwig 	default:
625dd2d535eSChristoph Hellwig 		stat->blksize = xfs_stat_blksize(ip);
626c59d87c4SChristoph Hellwig 		stat->rdev = 0;
627c59d87c4SChristoph Hellwig 		break;
628c59d87c4SChristoph Hellwig 	}
629c59d87c4SChristoph Hellwig 
630c59d87c4SChristoph Hellwig 	return 0;
631c59d87c4SChristoph Hellwig }
632c59d87c4SChristoph Hellwig 
63369bca807SJan Kara static int
xfs_vn_change_ok(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)63469bca807SJan Kara xfs_vn_change_ok(
635c1632a0fSChristian Brauner 	struct mnt_idmap	*idmap,
63669bca807SJan Kara 	struct dentry		*dentry,
63769bca807SJan Kara 	struct iattr		*iattr)
63869bca807SJan Kara {
63931051c85SJan Kara 	struct xfs_mount	*mp = XFS_I(d_inode(dentry))->i_mount;
64069bca807SJan Kara 
6412e973b2cSDave Chinner 	if (xfs_is_readonly(mp))
64269bca807SJan Kara 		return -EROFS;
64369bca807SJan Kara 
64475c8c50fSDave Chinner 	if (xfs_is_shutdown(mp))
64569bca807SJan Kara 		return -EIO;
64669bca807SJan Kara 
647c1632a0fSChristian Brauner 	return setattr_prepare(idmap, dentry, iattr);
64869bca807SJan Kara }
64969bca807SJan Kara 
65069bca807SJan Kara /*
65169bca807SJan Kara  * Set non-size attributes of an inode.
65269bca807SJan Kara  *
65369bca807SJan Kara  * Caution: The caller of this function is responsible for calling
65431051c85SJan Kara  * setattr_prepare() or otherwise verifying the change is fine.
65569bca807SJan Kara  */
6565d24ec4cSChristoph Hellwig static int
xfs_setattr_nonsize(struct mnt_idmap * idmap,struct dentry * dentry,struct xfs_inode * ip,struct iattr * iattr)657c59d87c4SChristoph Hellwig xfs_setattr_nonsize(
658c1632a0fSChristian Brauner 	struct mnt_idmap	*idmap,
659138060baSChristian Brauner 	struct dentry		*dentry,
660c59d87c4SChristoph Hellwig 	struct xfs_inode	*ip,
6615d24ec4cSChristoph Hellwig 	struct iattr		*iattr)
662c59d87c4SChristoph Hellwig {
663c59d87c4SChristoph Hellwig 	xfs_mount_t		*mp = ip->i_mount;
664c59d87c4SChristoph Hellwig 	struct inode		*inode = VFS_I(ip);
665c59d87c4SChristoph Hellwig 	int			mask = iattr->ia_valid;
666c59d87c4SChristoph Hellwig 	xfs_trans_t		*tp;
667c59d87c4SChristoph Hellwig 	int			error;
668dd3b015dSDarrick J. Wong 	kuid_t			uid = GLOBAL_ROOT_UID;
669dd3b015dSDarrick J. Wong 	kgid_t			gid = GLOBAL_ROOT_GID;
670c59d87c4SChristoph Hellwig 	struct xfs_dquot	*udqp = NULL, *gdqp = NULL;
671dd3b015dSDarrick J. Wong 	struct xfs_dquot	*old_udqp = NULL, *old_gdqp = NULL;
672c59d87c4SChristoph Hellwig 
673c59d87c4SChristoph Hellwig 	ASSERT((mask & ATTR_SIZE) == 0);
674c59d87c4SChristoph Hellwig 
675c59d87c4SChristoph Hellwig 	/*
676c59d87c4SChristoph Hellwig 	 * If disk quotas is on, we make sure that the dquots do exist on disk,
677c59d87c4SChristoph Hellwig 	 * before we start any other transactions. Trying to do this later
678c59d87c4SChristoph Hellwig 	 * is messy. We don't care to take a readlock to look at the ids
679c59d87c4SChristoph Hellwig 	 * in inode here, because we can't hold it across the trans_reserve.
680c59d87c4SChristoph Hellwig 	 * If the IDs do change before we take the ilock, we're covered
681c59d87c4SChristoph Hellwig 	 * because the i_*dquot fields will get updated anyway.
682c59d87c4SChristoph Hellwig 	 */
683c59d87c4SChristoph Hellwig 	if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
684c59d87c4SChristoph Hellwig 		uint	qflags = 0;
685c59d87c4SChristoph Hellwig 
686c59d87c4SChristoph Hellwig 		if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
6874d7ca409SChristian Brauner 			uid = from_vfsuid(idmap, i_user_ns(inode),
688b27c82e1SChristian Brauner 					  iattr->ia_vfsuid);
689c59d87c4SChristoph Hellwig 			qflags |= XFS_QMOPT_UQUOTA;
690c59d87c4SChristoph Hellwig 		} else {
6917aab1b28SDwight Engen 			uid = inode->i_uid;
692c59d87c4SChristoph Hellwig 		}
693c59d87c4SChristoph Hellwig 		if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
6944d7ca409SChristian Brauner 			gid = from_vfsgid(idmap, i_user_ns(inode),
695b27c82e1SChristian Brauner 					  iattr->ia_vfsgid);
696c59d87c4SChristoph Hellwig 			qflags |= XFS_QMOPT_GQUOTA;
697c59d87c4SChristoph Hellwig 		}  else {
6987aab1b28SDwight Engen 			gid = inode->i_gid;
699c59d87c4SChristoph Hellwig 		}
700c59d87c4SChristoph Hellwig 
701c59d87c4SChristoph Hellwig 		/*
702c59d87c4SChristoph Hellwig 		 * We take a reference when we initialize udqp and gdqp,
703c59d87c4SChristoph Hellwig 		 * so it is important that we never blindly double trip on
704c59d87c4SChristoph Hellwig 		 * the same variable. See xfs_create() for an example.
705c59d87c4SChristoph Hellwig 		 */
706c59d87c4SChristoph Hellwig 		ASSERT(udqp == NULL);
707c59d87c4SChristoph Hellwig 		ASSERT(gdqp == NULL);
708ceaf603cSChristoph Hellwig 		error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid,
70992f8ff73SChandra Seetharaman 					   qflags, &udqp, &gdqp, NULL);
710c59d87c4SChristoph Hellwig 		if (error)
711c59d87c4SChristoph Hellwig 			return error;
712c59d87c4SChristoph Hellwig 	}
713c59d87c4SChristoph Hellwig 
7147317a03dSDarrick J. Wong 	error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
715eba0549bSDarrick J. Wong 			has_capability_noaudit(current, CAP_FOWNER), &tp);
716c59d87c4SChristoph Hellwig 	if (error)
717253f4911SChristoph Hellwig 		goto out_dqrele;
718c59d87c4SChristoph Hellwig 
719c59d87c4SChristoph Hellwig 	/*
720dd3b015dSDarrick J. Wong 	 * Register quota modifications in the transaction.  Must be the owner
721dd3b015dSDarrick J. Wong 	 * or privileged.  These IDs could have changed since we last looked at
722dd3b015dSDarrick J. Wong 	 * them.  But, we're assured that if the ownership did change while we
723dd3b015dSDarrick J. Wong 	 * didn't have the inode locked, inode's dquot(s) would have changed
724dd3b015dSDarrick J. Wong 	 * also.
725c59d87c4SChristoph Hellwig 	 */
72635faf310SChristian Brauner 	if (XFS_IS_UQUOTA_ON(mp) &&
7270dbe12f2SChristian Brauner 	    i_uid_needs_update(idmap, iattr, inode)) {
728c59d87c4SChristoph Hellwig 		ASSERT(udqp);
729dd3b015dSDarrick J. Wong 		old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp);
730c59d87c4SChristoph Hellwig 	}
73135faf310SChristian Brauner 	if (XFS_IS_GQUOTA_ON(mp) &&
7320dbe12f2SChristian Brauner 	    i_gid_needs_update(idmap, iattr, inode)) {
733dd3b015dSDarrick J. Wong 		ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp));
734c59d87c4SChristoph Hellwig 		ASSERT(gdqp);
735dd3b015dSDarrick J. Wong 		old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp);
736c59d87c4SChristoph Hellwig 	}
737c59d87c4SChristoph Hellwig 
738c1632a0fSChristian Brauner 	setattr_copy(idmap, inode, iattr);
739c59d87c4SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
740c59d87c4SChristoph Hellwig 
741ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_ig_attrchg);
742c59d87c4SChristoph Hellwig 
7430560f31aSDave Chinner 	if (xfs_has_wsync(mp))
744c59d87c4SChristoph Hellwig 		xfs_trans_set_sync(tp);
74570393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
746c59d87c4SChristoph Hellwig 
747c59d87c4SChristoph Hellwig 	/*
748c59d87c4SChristoph Hellwig 	 * Release any dquot(s) the inode had kept before chown.
749c59d87c4SChristoph Hellwig 	 */
750dd3b015dSDarrick J. Wong 	xfs_qm_dqrele(old_udqp);
751dd3b015dSDarrick J. Wong 	xfs_qm_dqrele(old_gdqp);
752c59d87c4SChristoph Hellwig 	xfs_qm_dqrele(udqp);
753c59d87c4SChristoph Hellwig 	xfs_qm_dqrele(gdqp);
754c59d87c4SChristoph Hellwig 
755c59d87c4SChristoph Hellwig 	if (error)
756b474c7aeSEric Sandeen 		return error;
757c59d87c4SChristoph Hellwig 
758c59d87c4SChristoph Hellwig 	/*
759c59d87c4SChristoph Hellwig 	 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
760c59d87c4SChristoph Hellwig 	 * 	     update.  We could avoid this with linked transactions
761c59d87c4SChristoph Hellwig 	 * 	     and passing down the transaction pointer all the way
762c59d87c4SChristoph Hellwig 	 *	     to attr_set.  No previous user of the generic
763c59d87c4SChristoph Hellwig 	 * 	     Posix ACL code seems to care about this issue either.
764c59d87c4SChristoph Hellwig 	 */
7655d24ec4cSChristoph Hellwig 	if (mask & ATTR_MODE) {
76613e83a49SChristian Brauner 		error = posix_acl_chmod(idmap, dentry, inode->i_mode);
767c59d87c4SChristoph Hellwig 		if (error)
768b474c7aeSEric Sandeen 			return error;
769c59d87c4SChristoph Hellwig 	}
770c59d87c4SChristoph Hellwig 
771c59d87c4SChristoph Hellwig 	return 0;
772c59d87c4SChristoph Hellwig 
773253f4911SChristoph Hellwig out_dqrele:
774c59d87c4SChristoph Hellwig 	xfs_qm_dqrele(udqp);
775c59d87c4SChristoph Hellwig 	xfs_qm_dqrele(gdqp);
776c59d87c4SChristoph Hellwig 	return error;
777c59d87c4SChristoph Hellwig }
778c59d87c4SChristoph Hellwig 
779c59d87c4SChristoph Hellwig /*
780c59d87c4SChristoph Hellwig  * Truncate file.  Must have write permission and not be a directory.
78169bca807SJan Kara  *
78269bca807SJan Kara  * Caution: The caller of this function is responsible for calling
78331051c85SJan Kara  * setattr_prepare() or otherwise verifying the change is fine.
784c59d87c4SChristoph Hellwig  */
7857bf7a193SDarrick J. Wong STATIC int
xfs_setattr_size(struct mnt_idmap * idmap,struct dentry * dentry,struct xfs_inode * ip,struct iattr * iattr)786c59d87c4SChristoph Hellwig xfs_setattr_size(
787c1632a0fSChristian Brauner 	struct mnt_idmap	*idmap,
788138060baSChristian Brauner 	struct dentry		*dentry,
789c59d87c4SChristoph Hellwig 	struct xfs_inode	*ip,
79076ca4c23SChristoph Hellwig 	struct iattr		*iattr)
791c59d87c4SChristoph Hellwig {
792c59d87c4SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
793c59d87c4SChristoph Hellwig 	struct inode		*inode = VFS_I(ip);
794673e8e59SChristoph Hellwig 	xfs_off_t		oldsize, newsize;
795c59d87c4SChristoph Hellwig 	struct xfs_trans	*tp;
796c59d87c4SChristoph Hellwig 	int			error;
797f38996f5SChristoph Hellwig 	uint			lock_flags = 0;
7985885ebdaSDave Chinner 	bool			did_zeroing = false;
799c59d87c4SChristoph Hellwig 
80076ca4c23SChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
801e8e9ad42SDave Chinner 	ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
802c19b3b05SDave Chinner 	ASSERT(S_ISREG(inode->i_mode));
803fe60a8a0SChristoph Hellwig 	ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
80488a9e03bSYumei Huang 		ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
805c59d87c4SChristoph Hellwig 
806ce7ae151SChristoph Hellwig 	oldsize = inode->i_size;
807673e8e59SChristoph Hellwig 	newsize = iattr->ia_size;
808673e8e59SChristoph Hellwig 
809c59d87c4SChristoph Hellwig 	/*
810c59d87c4SChristoph Hellwig 	 * Short circuit the truncate case for zero length files.
811c59d87c4SChristoph Hellwig 	 */
812daf83964SChristoph Hellwig 	if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
813fe60a8a0SChristoph Hellwig 		if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
81476ca4c23SChristoph Hellwig 			return 0;
815c59d87c4SChristoph Hellwig 
816c59d87c4SChristoph Hellwig 		/*
817c59d87c4SChristoph Hellwig 		 * Use the regular setattr path to update the timestamps.
818c59d87c4SChristoph Hellwig 		 */
819c59d87c4SChristoph Hellwig 		iattr->ia_valid &= ~ATTR_SIZE;
820c1632a0fSChristian Brauner 		return xfs_setattr_nonsize(idmap, dentry, ip, iattr);
821c59d87c4SChristoph Hellwig 	}
822c59d87c4SChristoph Hellwig 
823c59d87c4SChristoph Hellwig 	/*
824c59d87c4SChristoph Hellwig 	 * Make sure that the dquots are attached to the inode.
825c59d87c4SChristoph Hellwig 	 */
826c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
827c59d87c4SChristoph Hellwig 	if (error)
82876ca4c23SChristoph Hellwig 		return error;
829c59d87c4SChristoph Hellwig 
830c59d87c4SChristoph Hellwig 	/*
831f0c6bcbaSChristoph Hellwig 	 * Wait for all direct I/O to complete.
832f0c6bcbaSChristoph Hellwig 	 */
833f0c6bcbaSChristoph Hellwig 	inode_dio_wait(inode);
834f0c6bcbaSChristoph Hellwig 
835f0c6bcbaSChristoph Hellwig 	/*
8365885ebdaSDave Chinner 	 * File data changes must be complete before we start the transaction to
8375885ebdaSDave Chinner 	 * modify the inode.  This needs to be done before joining the inode to
8385885ebdaSDave Chinner 	 * the transaction because the inode cannot be unlocked once it is a
8395885ebdaSDave Chinner 	 * part of the transaction.
8405885ebdaSDave Chinner 	 *
841f0c6bcbaSChristoph Hellwig 	 * Start with zeroing any data beyond EOF that we may expose on file
842f0c6bcbaSChristoph Hellwig 	 * extension, or zeroing out the rest of the block on a downward
843f0c6bcbaSChristoph Hellwig 	 * truncate.
844c59d87c4SChristoph Hellwig 	 */
845673e8e59SChristoph Hellwig 	if (newsize > oldsize) {
846f5c54717SChristoph Hellwig 		trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
847f1ba5fafSShiyang Ruan 		error = xfs_zero_range(ip, oldsize, newsize - oldsize,
848f1ba5fafSShiyang Ruan 				&did_zeroing);
849f0c6bcbaSChristoph Hellwig 	} else {
850869ae85dSBrian Foster 		/*
851869ae85dSBrian Foster 		 * iomap won't detect a dirty page over an unwritten block (or a
852869ae85dSBrian Foster 		 * cow block over a hole) and subsequently skips zeroing the
853869ae85dSBrian Foster 		 * newly post-EOF portion of the page. Flush the new EOF to
854869ae85dSBrian Foster 		 * convert the block before the pagecache truncate.
855869ae85dSBrian Foster 		 */
856869ae85dSBrian Foster 		error = filemap_write_and_wait_range(inode->i_mapping, newsize,
857869ae85dSBrian Foster 						     newsize);
858869ae85dSBrian Foster 		if (error)
859869ae85dSBrian Foster 			return error;
860f1ba5fafSShiyang Ruan 		error = xfs_truncate_page(ip, newsize, &did_zeroing);
861f0c6bcbaSChristoph Hellwig 	}
862f0c6bcbaSChristoph Hellwig 
863c59d87c4SChristoph Hellwig 	if (error)
86476ca4c23SChristoph Hellwig 		return error;
865c59d87c4SChristoph Hellwig 
866c59d87c4SChristoph Hellwig 	/*
8670f9160b4SDave Chinner 	 * We've already locked out new page faults, so now we can safely remove
8680f9160b4SDave Chinner 	 * pages from the page cache knowing they won't get refaulted until we
8690f9160b4SDave Chinner 	 * drop the XFS_MMAP_EXCL lock after the extent manipulations are
8700f9160b4SDave Chinner 	 * complete. The truncate_setsize() call also cleans partial EOF page
8710f9160b4SDave Chinner 	 * PTEs on extending truncates and hence ensures sub-page block size
8720f9160b4SDave Chinner 	 * filesystems are correctly handled, too.
87349abc3a8SDave Chinner 	 *
8740f9160b4SDave Chinner 	 * We have to do all the page cache truncate work outside the
8750f9160b4SDave Chinner 	 * transaction context as the "lock" order is page lock->log space
8760f9160b4SDave Chinner 	 * reservation as defined by extent allocation in the writeback path.
877253f4911SChristoph Hellwig 	 * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
8780f9160b4SDave Chinner 	 * having already truncated the in-memory version of the file (i.e. made
8790f9160b4SDave Chinner 	 * user visible changes). There's not much we can do about this, except
8800f9160b4SDave Chinner 	 * to hope that the caller sees ENOMEM and retries the truncate
8810f9160b4SDave Chinner 	 * operation.
882350976aeSEryu Guan 	 *
883350976aeSEryu Guan 	 * And we update in-core i_size and truncate page cache beyond newsize
88413d2c10bSChristoph Hellwig 	 * before writeback the [i_disk_size, newsize] range, so we're
88513d2c10bSChristoph Hellwig 	 * guaranteed not to write stale data past the new EOF on truncate down.
88649abc3a8SDave Chinner 	 */
88749abc3a8SDave Chinner 	truncate_setsize(inode, newsize);
888c59d87c4SChristoph Hellwig 
889350976aeSEryu Guan 	/*
890350976aeSEryu Guan 	 * We are going to log the inode size change in this transaction so
891350976aeSEryu Guan 	 * any previous writes that are beyond the on disk EOF and the new
892350976aeSEryu Guan 	 * EOF that have not been written out need to be written here.  If we
893350976aeSEryu Guan 	 * do not write the data out, we expose ourselves to the null files
894350976aeSEryu Guan 	 * problem. Note that this includes any block zeroing we did above;
895350976aeSEryu Guan 	 * otherwise those blocks may not be zeroed after a crash.
896350976aeSEryu Guan 	 */
897350976aeSEryu Guan 	if (did_zeroing ||
89813d2c10bSChristoph Hellwig 	    (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) {
899350976aeSEryu Guan 		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
90013d2c10bSChristoph Hellwig 						ip->i_disk_size, newsize - 1);
901350976aeSEryu Guan 		if (error)
902350976aeSEryu Guan 			return error;
903350976aeSEryu Guan 	}
904350976aeSEryu Guan 
905253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
906c59d87c4SChristoph Hellwig 	if (error)
907253f4911SChristoph Hellwig 		return error;
908c59d87c4SChristoph Hellwig 
909c59d87c4SChristoph Hellwig 	lock_flags |= XFS_ILOCK_EXCL;
910c59d87c4SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_EXCL);
911ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(tp, ip, 0);
912c59d87c4SChristoph Hellwig 
913c59d87c4SChristoph Hellwig 	/*
914c59d87c4SChristoph Hellwig 	 * Only change the c/mtime if we are changing the size or we are
915c59d87c4SChristoph Hellwig 	 * explicitly asked to change it.  This handles the semantic difference
916c59d87c4SChristoph Hellwig 	 * between truncate() and ftruncate() as implemented in the VFS.
917c59d87c4SChristoph Hellwig 	 *
918c59d87c4SChristoph Hellwig 	 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
919c59d87c4SChristoph Hellwig 	 * special case where we need to update the times despite not having
920c59d87c4SChristoph Hellwig 	 * these flags set.  For all other operations the VFS set these flags
921c59d87c4SChristoph Hellwig 	 * explicitly if it wants a timestamp update.
922c59d87c4SChristoph Hellwig 	 */
923fe60a8a0SChristoph Hellwig 	if (newsize != oldsize &&
924fe60a8a0SChristoph Hellwig 	    !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
925c59d87c4SChristoph Hellwig 		iattr->ia_ctime = iattr->ia_mtime =
926f798accdSChristian Brauner 			current_time(inode);
927fe60a8a0SChristoph Hellwig 		iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
928c59d87c4SChristoph Hellwig 	}
929c59d87c4SChristoph Hellwig 
930673e8e59SChristoph Hellwig 	/*
931673e8e59SChristoph Hellwig 	 * The first thing we do is set the size to new_size permanently on
932673e8e59SChristoph Hellwig 	 * disk.  This way we don't have to worry about anyone ever being able
933673e8e59SChristoph Hellwig 	 * to look at the data being freed even in the face of a crash.
934673e8e59SChristoph Hellwig 	 * What we're getting around here is the case where we free a block, it
935673e8e59SChristoph Hellwig 	 * is allocated to another file, it is written to, and then we crash.
936673e8e59SChristoph Hellwig 	 * If the new data gets written to the file but the log buffers
937673e8e59SChristoph Hellwig 	 * containing the free and reallocation don't, then we'd end up with
938673e8e59SChristoph Hellwig 	 * garbage in the blocks being freed.  As long as we make the new size
939673e8e59SChristoph Hellwig 	 * permanent before actually freeing any blocks it doesn't matter if
940673e8e59SChristoph Hellwig 	 * they get written to.
941673e8e59SChristoph Hellwig 	 */
94213d2c10bSChristoph Hellwig 	ip->i_disk_size = newsize;
943673e8e59SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
944673e8e59SChristoph Hellwig 
945673e8e59SChristoph Hellwig 	if (newsize <= oldsize) {
946673e8e59SChristoph Hellwig 		error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
947c59d87c4SChristoph Hellwig 		if (error)
9484906e215SChristoph Hellwig 			goto out_trans_cancel;
949c59d87c4SChristoph Hellwig 
950c59d87c4SChristoph Hellwig 		/*
951c59d87c4SChristoph Hellwig 		 * Truncated "down", so we're removing references to old data
952c59d87c4SChristoph Hellwig 		 * here - if we delay flushing for a long time, we expose
953c59d87c4SChristoph Hellwig 		 * ourselves unduly to the notorious NULL files problem.  So,
954c59d87c4SChristoph Hellwig 		 * we mark this inode and flush it when the file is closed,
955c59d87c4SChristoph Hellwig 		 * and do not wait the usual (long) time for writeout.
956c59d87c4SChristoph Hellwig 		 */
957c59d87c4SChristoph Hellwig 		xfs_iflags_set(ip, XFS_ITRUNCATED);
95827b52867SBrian Foster 
95927b52867SBrian Foster 		/* A truncate down always removes post-EOF blocks. */
96027b52867SBrian Foster 		xfs_inode_clear_eofblocks_tag(ip);
961c59d87c4SChristoph Hellwig 	}
962c59d87c4SChristoph Hellwig 
963e014f37dSDarrick J. Wong 	ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
964c1632a0fSChristian Brauner 	setattr_copy(idmap, inode, iattr);
965c59d87c4SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
966c59d87c4SChristoph Hellwig 
967ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_ig_attrchg);
968c59d87c4SChristoph Hellwig 
9690560f31aSDave Chinner 	if (xfs_has_wsync(mp))
970c59d87c4SChristoph Hellwig 		xfs_trans_set_sync(tp);
971c59d87c4SChristoph Hellwig 
97270393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
973c59d87c4SChristoph Hellwig out_unlock:
974c59d87c4SChristoph Hellwig 	if (lock_flags)
975c59d87c4SChristoph Hellwig 		xfs_iunlock(ip, lock_flags);
976c59d87c4SChristoph Hellwig 	return error;
977c59d87c4SChristoph Hellwig 
978c59d87c4SChristoph Hellwig out_trans_cancel:
9794906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
980c59d87c4SChristoph Hellwig 	goto out_unlock;
981c59d87c4SChristoph Hellwig }
982c59d87c4SChristoph Hellwig 
98369bca807SJan Kara int
xfs_vn_setattr_size(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)98469bca807SJan Kara xfs_vn_setattr_size(
985c1632a0fSChristian Brauner 	struct mnt_idmap	*idmap,
986c59d87c4SChristoph Hellwig 	struct dentry		*dentry,
987c59d87c4SChristoph Hellwig 	struct iattr		*iattr)
988c59d87c4SChristoph Hellwig {
9892b0143b5SDavid Howells 	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
99076ca4c23SChristoph Hellwig 	int error;
99176ca4c23SChristoph Hellwig 
99269bca807SJan Kara 	trace_xfs_setattr(ip);
99369bca807SJan Kara 
994c1632a0fSChristian Brauner 	error = xfs_vn_change_ok(idmap, dentry, iattr);
99569bca807SJan Kara 	if (error)
99669bca807SJan Kara 		return error;
997c1632a0fSChristian Brauner 	return xfs_setattr_size(idmap, dentry, ip, iattr);
99869bca807SJan Kara }
99969bca807SJan Kara 
100069bca807SJan Kara STATIC int
xfs_vn_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)100169bca807SJan Kara xfs_vn_setattr(
1002c1632a0fSChristian Brauner 	struct mnt_idmap	*idmap,
100369bca807SJan Kara 	struct dentry		*dentry,
100469bca807SJan Kara 	struct iattr		*iattr)
100569bca807SJan Kara {
100626f88363SChristoph Hellwig 	struct inode		*inode = d_inode(dentry);
100726f88363SChristoph Hellwig 	struct xfs_inode	*ip = XFS_I(inode);
100869bca807SJan Kara 	int			error;
100969bca807SJan Kara 
101076ca4c23SChristoph Hellwig 	if (iattr->ia_valid & ATTR_SIZE) {
1011c63a8eaeSDan Williams 		uint			iolock;
1012e8e9ad42SDave Chinner 
101365523218SChristoph Hellwig 		xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
1014c63a8eaeSDan Williams 		iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
1015c63a8eaeSDan Williams 
101669eb5fa1SDan Williams 		error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
1017c63a8eaeSDan Williams 		if (error) {
1018c63a8eaeSDan Williams 			xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1019c63a8eaeSDan Williams 			return error;
1020c63a8eaeSDan Williams 		}
1021c63a8eaeSDan Williams 
1022c1632a0fSChristian Brauner 		error = xfs_vn_setattr_size(idmap, dentry, iattr);
102365523218SChristoph Hellwig 		xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
102476ca4c23SChristoph Hellwig 	} else {
102526f88363SChristoph Hellwig 		trace_xfs_setattr(ip);
102626f88363SChristoph Hellwig 
1027c1632a0fSChristian Brauner 		error = xfs_vn_change_ok(idmap, dentry, iattr);
102826f88363SChristoph Hellwig 		if (!error)
1029c1632a0fSChristian Brauner 			error = xfs_setattr_nonsize(idmap, dentry, ip, iattr);
103076ca4c23SChristoph Hellwig 	}
103176ca4c23SChristoph Hellwig 
10322451337dSDave Chinner 	return error;
1033c59d87c4SChristoph Hellwig }
1034c59d87c4SChristoph Hellwig 
103569ff2826SChristoph Hellwig STATIC int
xfs_vn_update_time(struct inode * inode,int flags)103669ff2826SChristoph Hellwig xfs_vn_update_time(
103769ff2826SChristoph Hellwig 	struct inode		*inode,
103869ff2826SChristoph Hellwig 	int			flags)
103969ff2826SChristoph Hellwig {
104069ff2826SChristoph Hellwig 	struct xfs_inode	*ip = XFS_I(inode);
104169ff2826SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
1042c3b1b131SChristoph Hellwig 	int			log_flags = XFS_ILOG_TIMESTAMP;
104369ff2826SChristoph Hellwig 	struct xfs_trans	*tp;
104469ff2826SChristoph Hellwig 	int			error;
104551b0f3ebSJeff Layton 	struct timespec64	now;
104669ff2826SChristoph Hellwig 
104769ff2826SChristoph Hellwig 	trace_xfs_update_time(ip);
104869ff2826SChristoph Hellwig 
1049c3b1b131SChristoph Hellwig 	if (inode->i_sb->s_flags & SB_LAZYTIME) {
1050c3b1b131SChristoph Hellwig 		if (!((flags & S_VERSION) &&
1051541d4c79SJeff Layton 		      inode_maybe_inc_iversion(inode, false))) {
1052541d4c79SJeff Layton 			generic_update_time(inode, flags);
1053541d4c79SJeff Layton 			return 0;
1054541d4c79SJeff Layton 		}
1055c3b1b131SChristoph Hellwig 
1056c3b1b131SChristoph Hellwig 		/* Capture the iversion update that just occurred */
1057c3b1b131SChristoph Hellwig 		log_flags |= XFS_ILOG_CORE;
1058c3b1b131SChristoph Hellwig 	}
1059c3b1b131SChristoph Hellwig 
1060253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1061253f4911SChristoph Hellwig 	if (error)
10622451337dSDave Chinner 		return error;
106369ff2826SChristoph Hellwig 
106469ff2826SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_EXCL);
106551b0f3ebSJeff Layton 	if (flags & (S_CTIME|S_MTIME))
106651b0f3ebSJeff Layton 		now = inode_set_ctime_current(inode);
106751b0f3ebSJeff Layton 	else
106851b0f3ebSJeff Layton 		now = current_time(inode);
106951b0f3ebSJeff Layton 
10703987848cSDave Chinner 	if (flags & S_MTIME)
107151b0f3ebSJeff Layton 		inode->i_mtime = now;
10723987848cSDave Chinner 	if (flags & S_ATIME)
107351b0f3ebSJeff Layton 		inode->i_atime = now;
10743987848cSDave Chinner 
107569ff2826SChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1076c3b1b131SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, log_flags);
107770393313SChristoph Hellwig 	return xfs_trans_commit(tp);
107869ff2826SChristoph Hellwig }
107969ff2826SChristoph Hellwig 
1080c59d87c4SChristoph Hellwig STATIC int
xfs_vn_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 length)1081c59d87c4SChristoph Hellwig xfs_vn_fiemap(
1082c59d87c4SChristoph Hellwig 	struct inode		*inode,
1083c59d87c4SChristoph Hellwig 	struct fiemap_extent_info *fieinfo,
1084c59d87c4SChristoph Hellwig 	u64			start,
1085c59d87c4SChristoph Hellwig 	u64			length)
1086c59d87c4SChristoph Hellwig {
1087c59d87c4SChristoph Hellwig 	int			error;
1088c59d87c4SChristoph Hellwig 
1089d2bb140eSChristoph Hellwig 	xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
10901d4795e7SChristoph Hellwig 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
10911d4795e7SChristoph Hellwig 		fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
10921d4795e7SChristoph Hellwig 		error = iomap_fiemap(inode, fieinfo, start, length,
10931d4795e7SChristoph Hellwig 				&xfs_xattr_iomap_ops);
10941d4795e7SChristoph Hellwig 	} else {
10951d4795e7SChristoph Hellwig 		error = iomap_fiemap(inode, fieinfo, start, length,
1096690c2a38SChristoph Hellwig 				&xfs_read_iomap_ops);
10971d4795e7SChristoph Hellwig 	}
1098d2bb140eSChristoph Hellwig 	xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
1099d2bb140eSChristoph Hellwig 
1100c59d87c4SChristoph Hellwig 	return error;
1101c59d87c4SChristoph Hellwig }
1102c59d87c4SChristoph Hellwig 
110399b6436bSZhi Yong Wu STATIC int
xfs_vn_tmpfile(struct mnt_idmap * idmap,struct inode * dir,struct file * file,umode_t mode)110499b6436bSZhi Yong Wu xfs_vn_tmpfile(
1105011e2b71SChristian Brauner 	struct mnt_idmap	*idmap,
110699b6436bSZhi Yong Wu 	struct inode		*dir,
1107863f144fSMiklos Szeredi 	struct file		*file,
110899b6436bSZhi Yong Wu 	umode_t			mode)
110999b6436bSZhi Yong Wu {
1110f2d40141SChristian Brauner 	int err = xfs_generic_create(idmap, dir, file->f_path.dentry, mode, 0, file);
1111863f144fSMiklos Szeredi 
1112863f144fSMiklos Szeredi 	return finish_open_simple(file, err);
111399b6436bSZhi Yong Wu }
111499b6436bSZhi Yong Wu 
1115c59d87c4SChristoph Hellwig static const struct inode_operations xfs_inode_operations = {
1116cac2f8b8SChristian Brauner 	.get_inode_acl		= xfs_get_acl,
11172401dc29SChristoph Hellwig 	.set_acl		= xfs_set_acl,
1118c59d87c4SChristoph Hellwig 	.getattr		= xfs_vn_getattr,
1119c59d87c4SChristoph Hellwig 	.setattr		= xfs_vn_setattr,
1120c59d87c4SChristoph Hellwig 	.listxattr		= xfs_vn_listxattr,
1121c59d87c4SChristoph Hellwig 	.fiemap			= xfs_vn_fiemap,
112269ff2826SChristoph Hellwig 	.update_time		= xfs_vn_update_time,
11239fefd5dbSMiklos Szeredi 	.fileattr_get		= xfs_fileattr_get,
11249fefd5dbSMiklos Szeredi 	.fileattr_set		= xfs_fileattr_set,
1125c59d87c4SChristoph Hellwig };
1126c59d87c4SChristoph Hellwig 
1127c59d87c4SChristoph Hellwig static const struct inode_operations xfs_dir_inode_operations = {
1128c59d87c4SChristoph Hellwig 	.create			= xfs_vn_create,
1129c59d87c4SChristoph Hellwig 	.lookup			= xfs_vn_lookup,
1130c59d87c4SChristoph Hellwig 	.link			= xfs_vn_link,
1131c59d87c4SChristoph Hellwig 	.unlink			= xfs_vn_unlink,
1132c59d87c4SChristoph Hellwig 	.symlink		= xfs_vn_symlink,
1133c59d87c4SChristoph Hellwig 	.mkdir			= xfs_vn_mkdir,
1134c59d87c4SChristoph Hellwig 	/*
1135c59d87c4SChristoph Hellwig 	 * Yes, XFS uses the same method for rmdir and unlink.
1136c59d87c4SChristoph Hellwig 	 *
1137c59d87c4SChristoph Hellwig 	 * There are some subtile differences deeper in the code,
1138c59d87c4SChristoph Hellwig 	 * but we use S_ISDIR to check for those.
1139c59d87c4SChristoph Hellwig 	 */
1140c59d87c4SChristoph Hellwig 	.rmdir			= xfs_vn_unlink,
1141c59d87c4SChristoph Hellwig 	.mknod			= xfs_vn_mknod,
11422773bf00SMiklos Szeredi 	.rename			= xfs_vn_rename,
1143cac2f8b8SChristian Brauner 	.get_inode_acl		= xfs_get_acl,
11442401dc29SChristoph Hellwig 	.set_acl		= xfs_set_acl,
1145c59d87c4SChristoph Hellwig 	.getattr		= xfs_vn_getattr,
1146c59d87c4SChristoph Hellwig 	.setattr		= xfs_vn_setattr,
1147c59d87c4SChristoph Hellwig 	.listxattr		= xfs_vn_listxattr,
114869ff2826SChristoph Hellwig 	.update_time		= xfs_vn_update_time,
114999b6436bSZhi Yong Wu 	.tmpfile		= xfs_vn_tmpfile,
11509fefd5dbSMiklos Szeredi 	.fileattr_get		= xfs_fileattr_get,
11519fefd5dbSMiklos Szeredi 	.fileattr_set		= xfs_fileattr_set,
1152c59d87c4SChristoph Hellwig };
1153c59d87c4SChristoph Hellwig 
1154c59d87c4SChristoph Hellwig static const struct inode_operations xfs_dir_ci_inode_operations = {
1155c59d87c4SChristoph Hellwig 	.create			= xfs_vn_create,
1156c59d87c4SChristoph Hellwig 	.lookup			= xfs_vn_ci_lookup,
1157c59d87c4SChristoph Hellwig 	.link			= xfs_vn_link,
1158c59d87c4SChristoph Hellwig 	.unlink			= xfs_vn_unlink,
1159c59d87c4SChristoph Hellwig 	.symlink		= xfs_vn_symlink,
1160c59d87c4SChristoph Hellwig 	.mkdir			= xfs_vn_mkdir,
1161c59d87c4SChristoph Hellwig 	/*
1162c59d87c4SChristoph Hellwig 	 * Yes, XFS uses the same method for rmdir and unlink.
1163c59d87c4SChristoph Hellwig 	 *
1164c59d87c4SChristoph Hellwig 	 * There are some subtile differences deeper in the code,
1165c59d87c4SChristoph Hellwig 	 * but we use S_ISDIR to check for those.
1166c59d87c4SChristoph Hellwig 	 */
1167c59d87c4SChristoph Hellwig 	.rmdir			= xfs_vn_unlink,
1168c59d87c4SChristoph Hellwig 	.mknod			= xfs_vn_mknod,
11692773bf00SMiklos Szeredi 	.rename			= xfs_vn_rename,
1170cac2f8b8SChristian Brauner 	.get_inode_acl		= xfs_get_acl,
11712401dc29SChristoph Hellwig 	.set_acl		= xfs_set_acl,
1172c59d87c4SChristoph Hellwig 	.getattr		= xfs_vn_getattr,
1173c59d87c4SChristoph Hellwig 	.setattr		= xfs_vn_setattr,
1174c59d87c4SChristoph Hellwig 	.listxattr		= xfs_vn_listxattr,
117569ff2826SChristoph Hellwig 	.update_time		= xfs_vn_update_time,
117699b6436bSZhi Yong Wu 	.tmpfile		= xfs_vn_tmpfile,
11779fefd5dbSMiklos Szeredi 	.fileattr_get		= xfs_fileattr_get,
11789fefd5dbSMiklos Szeredi 	.fileattr_set		= xfs_fileattr_set,
1179c59d87c4SChristoph Hellwig };
1180c59d87c4SChristoph Hellwig 
1181c59d87c4SChristoph Hellwig static const struct inode_operations xfs_symlink_inode_operations = {
11826b255391SAl Viro 	.get_link		= xfs_vn_get_link,
1183c59d87c4SChristoph Hellwig 	.getattr		= xfs_vn_getattr,
1184c59d87c4SChristoph Hellwig 	.setattr		= xfs_vn_setattr,
1185c59d87c4SChristoph Hellwig 	.listxattr		= xfs_vn_listxattr,
118669ff2826SChristoph Hellwig 	.update_time		= xfs_vn_update_time,
1187c59d87c4SChristoph Hellwig };
1188c59d87c4SChristoph Hellwig 
1189ba23cba9SDarrick J. Wong /* Figure out if this file actually supports DAX. */
1190ba23cba9SDarrick J. Wong static bool
xfs_inode_supports_dax(struct xfs_inode * ip)1191ba23cba9SDarrick J. Wong xfs_inode_supports_dax(
1192ba23cba9SDarrick J. Wong 	struct xfs_inode	*ip)
1193ba23cba9SDarrick J. Wong {
1194ba23cba9SDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
1195ba23cba9SDarrick J. Wong 
119632dbc565SIra Weiny 	/* Only supported on regular files. */
119732dbc565SIra Weiny 	if (!S_ISREG(VFS_I(ip)->i_mode))
1198ba23cba9SDarrick J. Wong 		return false;
1199ba23cba9SDarrick J. Wong 
1200ba23cba9SDarrick J. Wong 	/* Block size must match page size */
1201ba23cba9SDarrick J. Wong 	if (mp->m_sb.sb_blocksize != PAGE_SIZE)
1202ba23cba9SDarrick J. Wong 		return false;
1203ba23cba9SDarrick J. Wong 
1204ba23cba9SDarrick J. Wong 	/* Device has to support DAX too. */
120530fa529eSChristoph Hellwig 	return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
1206ba23cba9SDarrick J. Wong }
1207ba23cba9SDarrick J. Wong 
120832dbc565SIra Weiny static bool
xfs_inode_should_enable_dax(struct xfs_inode * ip)120932dbc565SIra Weiny xfs_inode_should_enable_dax(
121032dbc565SIra Weiny 	struct xfs_inode *ip)
121132dbc565SIra Weiny {
121232dbc565SIra Weiny 	if (!IS_ENABLED(CONFIG_FS_DAX))
121332dbc565SIra Weiny 		return false;
12140560f31aSDave Chinner 	if (xfs_has_dax_never(ip->i_mount))
121532dbc565SIra Weiny 		return false;
121632dbc565SIra Weiny 	if (!xfs_inode_supports_dax(ip))
121732dbc565SIra Weiny 		return false;
12180560f31aSDave Chinner 	if (xfs_has_dax_always(ip->i_mount))
121932dbc565SIra Weiny 		return true;
12203e09ab8fSChristoph Hellwig 	if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
122132dbc565SIra Weiny 		return true;
122232dbc565SIra Weiny 	return false;
122332dbc565SIra Weiny }
122432dbc565SIra Weiny 
1225840d493dSIra Weiny void
xfs_diflags_to_iflags(struct xfs_inode * ip,bool init)1226c59d87c4SChristoph Hellwig xfs_diflags_to_iflags(
1227840d493dSIra Weiny 	struct xfs_inode	*ip,
1228840d493dSIra Weiny 	bool init)
1229c59d87c4SChristoph Hellwig {
1230840d493dSIra Weiny 	struct inode            *inode = VFS_I(ip);
1231840d493dSIra Weiny 	unsigned int            xflags = xfs_ip2xflags(ip);
1232840d493dSIra Weiny 	unsigned int            flags = 0;
1233cbe4dab1SDave Chinner 
1234840d493dSIra Weiny 	ASSERT(!(IS_DAX(inode) && init));
1235cbe4dab1SDave Chinner 
1236840d493dSIra Weiny 	if (xflags & FS_XFLAG_IMMUTABLE)
1237840d493dSIra Weiny 		flags |= S_IMMUTABLE;
1238840d493dSIra Weiny 	if (xflags & FS_XFLAG_APPEND)
1239840d493dSIra Weiny 		flags |= S_APPEND;
1240840d493dSIra Weiny 	if (xflags & FS_XFLAG_SYNC)
1241840d493dSIra Weiny 		flags |= S_SYNC;
1242840d493dSIra Weiny 	if (xflags & FS_XFLAG_NOATIME)
1243840d493dSIra Weiny 		flags |= S_NOATIME;
1244840d493dSIra Weiny 	if (init && xfs_inode_should_enable_dax(ip))
1245840d493dSIra Weiny 		flags |= S_DAX;
1246840d493dSIra Weiny 
1247840d493dSIra Weiny 	/*
1248840d493dSIra Weiny 	 * S_DAX can only be set during inode initialization and is never set by
1249840d493dSIra Weiny 	 * the VFS, so we cannot mask off S_DAX in i_flags.
1250840d493dSIra Weiny 	 */
1251840d493dSIra Weiny 	inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
1252840d493dSIra Weiny 	inode->i_flags |= flags;
1253c59d87c4SChristoph Hellwig }
1254c59d87c4SChristoph Hellwig 
1255c59d87c4SChristoph Hellwig /*
12562b3d1d41SChristoph Hellwig  * Initialize the Linux inode.
1257c59d87c4SChristoph Hellwig  *
125858c90473SDave Chinner  * When reading existing inodes from disk this is called directly from xfs_iget,
1259132c460eSYang Xu  * when creating a new inode it is called from xfs_init_new_inode after setting
1260132c460eSYang Xu  * up the inode. These callers have different criteria for clearing XFS_INEW, so
1261132c460eSYang Xu  * leave it up to the caller to deal with unlocking the inode appropriately.
1262c59d87c4SChristoph Hellwig  */
1263c59d87c4SChristoph Hellwig void
xfs_setup_inode(struct xfs_inode * ip)1264c59d87c4SChristoph Hellwig xfs_setup_inode(
1265c59d87c4SChristoph Hellwig 	struct xfs_inode	*ip)
1266c59d87c4SChristoph Hellwig {
1267c59d87c4SChristoph Hellwig 	struct inode		*inode = &ip->i_vnode;
1268ad22c7a0SDave Chinner 	gfp_t			gfp_mask;
1269c59d87c4SChristoph Hellwig 
1270c59d87c4SChristoph Hellwig 	inode->i_ino = ip->i_ino;
1271f38a032bSDave Chinner 	inode->i_state |= I_NEW;
1272c59d87c4SChristoph Hellwig 
1273c59d87c4SChristoph Hellwig 	inode_sb_list_add(inode);
1274c59d87c4SChristoph Hellwig 	/* make the inode look hashed for the writeback code */
12755bef9151SAl Viro 	inode_fake_hash(inode);
1276c59d87c4SChristoph Hellwig 
127713d2c10bSChristoph Hellwig 	i_size_write(inode, ip->i_disk_size);
1278840d493dSIra Weiny 	xfs_diflags_to_iflags(ip, true);
1279c59d87c4SChristoph Hellwig 
12802b3d1d41SChristoph Hellwig 	if (S_ISDIR(inode->i_mode)) {
1281ef215e39SDave Chinner 		/*
1282ef215e39SDave Chinner 		 * We set the i_rwsem class here to avoid potential races with
1283ef215e39SDave Chinner 		 * lockdep_annotate_inode_mutex_key() reinitialising the lock
1284ef215e39SDave Chinner 		 * after a filehandle lookup has already found the inode in
1285ef215e39SDave Chinner 		 * cache before it has been unlocked via unlock_new_inode().
1286ef215e39SDave Chinner 		 */
1287ef215e39SDave Chinner 		lockdep_set_class(&inode->i_rwsem,
1288ef215e39SDave Chinner 				  &inode->i_sb->s_type->i_mutex_dir_key);
12892b3d1d41SChristoph Hellwig 		lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
12902b3d1d41SChristoph Hellwig 	} else {
129193a8614eSDave Chinner 		lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
1292c59d87c4SChristoph Hellwig 	}
1293c59d87c4SChristoph Hellwig 
1294c59d87c4SChristoph Hellwig 	/*
1295ad22c7a0SDave Chinner 	 * Ensure all page cache allocations are done from GFP_NOFS context to
1296ad22c7a0SDave Chinner 	 * prevent direct reclaim recursion back into the filesystem and blowing
1297ad22c7a0SDave Chinner 	 * stacks or deadlocking.
1298ad22c7a0SDave Chinner 	 */
1299ad22c7a0SDave Chinner 	gfp_mask = mapping_gfp_mask(inode->i_mapping);
1300ad22c7a0SDave Chinner 	mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1301ad22c7a0SDave Chinner 
1302ad22c7a0SDave Chinner 	/*
130305955a70SChristoph Hellwig 	 * For real-time inodes update the stable write flags to that of the RT
130405955a70SChristoph Hellwig 	 * device instead of the data device.
130505955a70SChristoph Hellwig 	 */
130605955a70SChristoph Hellwig 	if (S_ISREG(inode->i_mode) && XFS_IS_REALTIME_INODE(ip))
130705955a70SChristoph Hellwig 		xfs_update_stable_writes(ip);
130805955a70SChristoph Hellwig 
130905955a70SChristoph Hellwig 	/*
1310c59d87c4SChristoph Hellwig 	 * If there is no attribute fork no ACL can exist on this inode,
1311c59d87c4SChristoph Hellwig 	 * and it can't have any file capabilities attached to it either.
1312c59d87c4SChristoph Hellwig 	 */
1313932b42c6SDarrick J. Wong 	if (!xfs_inode_has_attr_fork(ip)) {
1314c59d87c4SChristoph Hellwig 		inode_has_no_xattr(inode);
1315c59d87c4SChristoph Hellwig 		cache_no_acl(inode);
1316c59d87c4SChristoph Hellwig 	}
1317c59d87c4SChristoph Hellwig }
13182b3d1d41SChristoph Hellwig 
13192b3d1d41SChristoph Hellwig void
xfs_setup_iops(struct xfs_inode * ip)13202b3d1d41SChristoph Hellwig xfs_setup_iops(
13212b3d1d41SChristoph Hellwig 	struct xfs_inode	*ip)
13222b3d1d41SChristoph Hellwig {
13232b3d1d41SChristoph Hellwig 	struct inode		*inode = &ip->i_vnode;
13242b3d1d41SChristoph Hellwig 
13252b3d1d41SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
13262b3d1d41SChristoph Hellwig 	case S_IFREG:
13272b3d1d41SChristoph Hellwig 		inode->i_op = &xfs_inode_operations;
13282b3d1d41SChristoph Hellwig 		inode->i_fop = &xfs_file_operations;
13296e2608dfSDan Williams 		if (IS_DAX(inode))
13306e2608dfSDan Williams 			inode->i_mapping->a_ops = &xfs_dax_aops;
13316e2608dfSDan Williams 		else
13322b3d1d41SChristoph Hellwig 			inode->i_mapping->a_ops = &xfs_address_space_operations;
13332b3d1d41SChristoph Hellwig 		break;
13342b3d1d41SChristoph Hellwig 	case S_IFDIR:
133538c26bfdSDave Chinner 		if (xfs_has_asciici(XFS_M(inode->i_sb)))
13362b3d1d41SChristoph Hellwig 			inode->i_op = &xfs_dir_ci_inode_operations;
13372b3d1d41SChristoph Hellwig 		else
13382b3d1d41SChristoph Hellwig 			inode->i_op = &xfs_dir_inode_operations;
13392b3d1d41SChristoph Hellwig 		inode->i_fop = &xfs_dir_file_operations;
13402b3d1d41SChristoph Hellwig 		break;
13412b3d1d41SChristoph Hellwig 	case S_IFLNK:
13422b3d1d41SChristoph Hellwig 		inode->i_op = &xfs_symlink_inode_operations;
13432b3d1d41SChristoph Hellwig 		break;
13442b3d1d41SChristoph Hellwig 	default:
13452b3d1d41SChristoph Hellwig 		inode->i_op = &xfs_inode_operations;
13462b3d1d41SChristoph Hellwig 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
13472b3d1d41SChristoph Hellwig 		break;
13482b3d1d41SChristoph Hellwig 	}
13492b3d1d41SChristoph Hellwig }
1350