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