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