xref: /linux/fs/xfs/xfs_file.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
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_trans.h"
15 #include "xfs_inode_item.h"
16 #include "xfs_bmap.h"
17 #include "xfs_bmap_util.h"
18 #include "xfs_dir2.h"
19 #include "xfs_dir2_priv.h"
20 #include "xfs_ioctl.h"
21 #include "xfs_trace.h"
22 #include "xfs_log.h"
23 #include "xfs_icache.h"
24 #include "xfs_pnfs.h"
25 #include "xfs_iomap.h"
26 #include "xfs_reflink.h"
27 #include "xfs_file.h"
28 
29 #include <linux/dax.h>
30 #include <linux/falloc.h>
31 #include <linux/backing-dev.h>
32 #include <linux/mman.h>
33 #include <linux/fadvise.h>
34 #include <linux/mount.h>
35 
36 static const struct vm_operations_struct xfs_file_vm_ops;
37 
38 /*
39  * Decide if the given file range is aligned to the size of the fundamental
40  * allocation unit for the file.
41  */
42 bool
43 xfs_is_falloc_aligned(
44 	struct xfs_inode	*ip,
45 	loff_t			pos,
46 	long long int		len)
47 {
48 	unsigned int		alloc_unit = xfs_inode_alloc_unitsize(ip);
49 
50 	if (!is_power_of_2(alloc_unit))
51 		return isaligned_64(pos, alloc_unit) &&
52 		       isaligned_64(len, alloc_unit);
53 
54 	return !((pos | len) & (alloc_unit - 1));
55 }
56 
57 /*
58  * Fsync operations on directories are much simpler than on regular files,
59  * as there is no file data to flush, and thus also no need for explicit
60  * cache flush operations, and there are no non-transaction metadata updates
61  * on directories either.
62  */
63 STATIC int
64 xfs_dir_fsync(
65 	struct file		*file,
66 	loff_t			start,
67 	loff_t			end,
68 	int			datasync)
69 {
70 	struct xfs_inode	*ip = XFS_I(file->f_mapping->host);
71 
72 	trace_xfs_dir_fsync(ip);
73 	return xfs_log_force_inode(ip);
74 }
75 
76 static xfs_csn_t
77 xfs_fsync_seq(
78 	struct xfs_inode	*ip,
79 	bool			datasync)
80 {
81 	if (!xfs_ipincount(ip))
82 		return 0;
83 	if (datasync && !(ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
84 		return 0;
85 	return ip->i_itemp->ili_commit_seq;
86 }
87 
88 /*
89  * All metadata updates are logged, which means that we just have to flush the
90  * log up to the latest LSN that touched the inode.
91  *
92  * If we have concurrent fsync/fdatasync() calls, we need them to all block on
93  * the log force before we clear the ili_fsync_fields field. This ensures that
94  * we don't get a racing sync operation that does not wait for the metadata to
95  * hit the journal before returning.  If we race with clearing ili_fsync_fields,
96  * then all that will happen is the log force will do nothing as the lsn will
97  * already be on disk.  We can't race with setting ili_fsync_fields because that
98  * is done under XFS_ILOCK_EXCL, and that can't happen because we hold the lock
99  * shared until after the ili_fsync_fields is cleared.
100  */
101 static  int
102 xfs_fsync_flush_log(
103 	struct xfs_inode	*ip,
104 	bool			datasync,
105 	int			*log_flushed)
106 {
107 	int			error = 0;
108 	xfs_csn_t		seq;
109 
110 	xfs_ilock(ip, XFS_ILOCK_SHARED);
111 	seq = xfs_fsync_seq(ip, datasync);
112 	if (seq) {
113 		error = xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC,
114 					  log_flushed);
115 
116 		spin_lock(&ip->i_itemp->ili_lock);
117 		ip->i_itemp->ili_fsync_fields = 0;
118 		spin_unlock(&ip->i_itemp->ili_lock);
119 	}
120 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
121 	return error;
122 }
123 
124 STATIC int
125 xfs_file_fsync(
126 	struct file		*file,
127 	loff_t			start,
128 	loff_t			end,
129 	int			datasync)
130 {
131 	struct xfs_inode	*ip = XFS_I(file->f_mapping->host);
132 	struct xfs_mount	*mp = ip->i_mount;
133 	int			error, err2;
134 	int			log_flushed = 0;
135 
136 	trace_xfs_file_fsync(ip);
137 
138 	error = file_write_and_wait_range(file, start, end);
139 	if (error)
140 		return error;
141 
142 	if (xfs_is_shutdown(mp))
143 		return -EIO;
144 
145 	xfs_iflags_clear(ip, XFS_ITRUNCATED);
146 
147 	/*
148 	 * If we have an RT and/or log subvolume we need to make sure to flush
149 	 * the write cache the device used for file data first.  This is to
150 	 * ensure newly written file data make it to disk before logging the new
151 	 * inode size in case of an extending write.
152 	 */
153 	if (XFS_IS_REALTIME_INODE(ip))
154 		error = blkdev_issue_flush(mp->m_rtdev_targp->bt_bdev);
155 	else if (mp->m_logdev_targp != mp->m_ddev_targp)
156 		error = blkdev_issue_flush(mp->m_ddev_targp->bt_bdev);
157 
158 	/*
159 	 * Any inode that has dirty modifications in the log is pinned.  The
160 	 * racy check here for a pinned inode will not catch modifications
161 	 * that happen concurrently to the fsync call, but fsync semantics
162 	 * only require to sync previously completed I/O.
163 	 */
164 	if (xfs_ipincount(ip)) {
165 		err2 = xfs_fsync_flush_log(ip, datasync, &log_flushed);
166 		if (err2 && !error)
167 			error = err2;
168 	}
169 
170 	/*
171 	 * If we only have a single device, and the log force about was
172 	 * a no-op we might have to flush the data device cache here.
173 	 * This can only happen for fdatasync/O_DSYNC if we were overwriting
174 	 * an already allocated file and thus do not have any metadata to
175 	 * commit.
176 	 */
177 	if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
178 	    mp->m_logdev_targp == mp->m_ddev_targp) {
179 		err2 = blkdev_issue_flush(mp->m_ddev_targp->bt_bdev);
180 		if (err2 && !error)
181 			error = err2;
182 	}
183 
184 	return error;
185 }
186 
187 static int
188 xfs_ilock_iocb(
189 	struct kiocb		*iocb,
190 	unsigned int		lock_mode)
191 {
192 	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
193 
194 	if (iocb->ki_flags & IOCB_NOWAIT) {
195 		if (!xfs_ilock_nowait(ip, lock_mode))
196 			return -EAGAIN;
197 	} else {
198 		xfs_ilock(ip, lock_mode);
199 	}
200 
201 	return 0;
202 }
203 
204 static int
205 xfs_ilock_iocb_for_write(
206 	struct kiocb		*iocb,
207 	unsigned int		*lock_mode)
208 {
209 	ssize_t			ret;
210 	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
211 
212 	ret = xfs_ilock_iocb(iocb, *lock_mode);
213 	if (ret)
214 		return ret;
215 
216 	if (*lock_mode == XFS_IOLOCK_EXCL)
217 		return 0;
218 	if (!xfs_iflags_test(ip, XFS_IREMAPPING))
219 		return 0;
220 
221 	xfs_iunlock(ip, *lock_mode);
222 	*lock_mode = XFS_IOLOCK_EXCL;
223 	return xfs_ilock_iocb(iocb, *lock_mode);
224 }
225 
226 static unsigned int
227 xfs_ilock_for_write_fault(
228 	struct xfs_inode	*ip)
229 {
230 	/* get a shared lock if no remapping in progress */
231 	xfs_ilock(ip, XFS_MMAPLOCK_SHARED);
232 	if (!xfs_iflags_test(ip, XFS_IREMAPPING))
233 		return XFS_MMAPLOCK_SHARED;
234 
235 	/* wait for remapping to complete */
236 	xfs_iunlock(ip, XFS_MMAPLOCK_SHARED);
237 	xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
238 	return XFS_MMAPLOCK_EXCL;
239 }
240 
241 STATIC ssize_t
242 xfs_file_dio_read(
243 	struct kiocb		*iocb,
244 	struct iov_iter		*to)
245 {
246 	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
247 	ssize_t			ret;
248 
249 	trace_xfs_file_direct_read(iocb, to);
250 
251 	if (!iov_iter_count(to))
252 		return 0; /* skip atime */
253 
254 	file_accessed(iocb->ki_filp);
255 
256 	ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
257 	if (ret)
258 		return ret;
259 	ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL, 0, NULL, 0);
260 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
261 
262 	return ret;
263 }
264 
265 static noinline ssize_t
266 xfs_file_dax_read(
267 	struct kiocb		*iocb,
268 	struct iov_iter		*to)
269 {
270 	struct xfs_inode	*ip = XFS_I(iocb->ki_filp->f_mapping->host);
271 	ssize_t			ret = 0;
272 
273 	trace_xfs_file_dax_read(iocb, to);
274 
275 	if (!iov_iter_count(to))
276 		return 0; /* skip atime */
277 
278 	ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
279 	if (ret)
280 		return ret;
281 	ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
282 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
283 
284 	file_accessed(iocb->ki_filp);
285 	return ret;
286 }
287 
288 STATIC ssize_t
289 xfs_file_buffered_read(
290 	struct kiocb		*iocb,
291 	struct iov_iter		*to)
292 {
293 	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
294 	ssize_t			ret;
295 
296 	trace_xfs_file_buffered_read(iocb, to);
297 
298 	ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
299 	if (ret)
300 		return ret;
301 	ret = generic_file_read_iter(iocb, to);
302 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
303 
304 	return ret;
305 }
306 
307 STATIC ssize_t
308 xfs_file_read_iter(
309 	struct kiocb		*iocb,
310 	struct iov_iter		*to)
311 {
312 	struct inode		*inode = file_inode(iocb->ki_filp);
313 	struct xfs_mount	*mp = XFS_I(inode)->i_mount;
314 	ssize_t			ret = 0;
315 
316 	XFS_STATS_INC(mp, xs_read_calls);
317 
318 	if (xfs_is_shutdown(mp))
319 		return -EIO;
320 
321 	if (IS_DAX(inode))
322 		ret = xfs_file_dax_read(iocb, to);
323 	else if (iocb->ki_flags & IOCB_DIRECT)
324 		ret = xfs_file_dio_read(iocb, to);
325 	else
326 		ret = xfs_file_buffered_read(iocb, to);
327 
328 	if (ret > 0)
329 		XFS_STATS_ADD(mp, xs_read_bytes, ret);
330 	return ret;
331 }
332 
333 STATIC ssize_t
334 xfs_file_splice_read(
335 	struct file		*in,
336 	loff_t			*ppos,
337 	struct pipe_inode_info	*pipe,
338 	size_t			len,
339 	unsigned int		flags)
340 {
341 	struct inode		*inode = file_inode(in);
342 	struct xfs_inode	*ip = XFS_I(inode);
343 	struct xfs_mount	*mp = ip->i_mount;
344 	ssize_t			ret = 0;
345 
346 	XFS_STATS_INC(mp, xs_read_calls);
347 
348 	if (xfs_is_shutdown(mp))
349 		return -EIO;
350 
351 	trace_xfs_file_splice_read(ip, *ppos, len);
352 
353 	xfs_ilock(ip, XFS_IOLOCK_SHARED);
354 	ret = filemap_splice_read(in, ppos, pipe, len, flags);
355 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
356 	if (ret > 0)
357 		XFS_STATS_ADD(mp, xs_read_bytes, ret);
358 	return ret;
359 }
360 
361 /*
362  * Common pre-write limit and setup checks.
363  *
364  * Called with the iolocked held either shared and exclusive according to
365  * @iolock, and returns with it held.  Might upgrade the iolock to exclusive
366  * if called for a direct write beyond i_size.
367  */
368 STATIC ssize_t
369 xfs_file_write_checks(
370 	struct kiocb		*iocb,
371 	struct iov_iter		*from,
372 	unsigned int		*iolock)
373 {
374 	struct file		*file = iocb->ki_filp;
375 	struct inode		*inode = file->f_mapping->host;
376 	struct xfs_inode	*ip = XFS_I(inode);
377 	ssize_t			error = 0;
378 	size_t			count = iov_iter_count(from);
379 	bool			drained_dio = false;
380 	loff_t			isize;
381 
382 restart:
383 	error = generic_write_checks(iocb, from);
384 	if (error <= 0)
385 		return error;
386 
387 	if (iocb->ki_flags & IOCB_NOWAIT) {
388 		error = break_layout(inode, false);
389 		if (error == -EWOULDBLOCK)
390 			error = -EAGAIN;
391 	} else {
392 		error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
393 	}
394 
395 	if (error)
396 		return error;
397 
398 	/*
399 	 * For changing security info in file_remove_privs() we need i_rwsem
400 	 * exclusively.
401 	 */
402 	if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
403 		xfs_iunlock(ip, *iolock);
404 		*iolock = XFS_IOLOCK_EXCL;
405 		error = xfs_ilock_iocb(iocb, *iolock);
406 		if (error) {
407 			*iolock = 0;
408 			return error;
409 		}
410 		goto restart;
411 	}
412 
413 	/*
414 	 * If the offset is beyond the size of the file, we need to zero any
415 	 * blocks that fall between the existing EOF and the start of this
416 	 * write.  If zeroing is needed and we are currently holding the iolock
417 	 * shared, we need to update it to exclusive which implies having to
418 	 * redo all checks before.
419 	 *
420 	 * We need to serialise against EOF updates that occur in IO completions
421 	 * here. We want to make sure that nobody is changing the size while we
422 	 * do this check until we have placed an IO barrier (i.e.  hold the
423 	 * XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.  The
424 	 * spinlock effectively forms a memory barrier once we have the
425 	 * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value and
426 	 * hence be able to correctly determine if we need to run zeroing.
427 	 *
428 	 * We can do an unlocked check here safely as IO completion can only
429 	 * extend EOF. Truncate is locked out at this point, so the EOF can
430 	 * not move backwards, only forwards. Hence we only need to take the
431 	 * slow path and spin locks when we are at or beyond the current EOF.
432 	 */
433 	if (iocb->ki_pos <= i_size_read(inode))
434 		goto out;
435 
436 	spin_lock(&ip->i_flags_lock);
437 	isize = i_size_read(inode);
438 	if (iocb->ki_pos > isize) {
439 		spin_unlock(&ip->i_flags_lock);
440 
441 		if (iocb->ki_flags & IOCB_NOWAIT)
442 			return -EAGAIN;
443 
444 		if (!drained_dio) {
445 			if (*iolock == XFS_IOLOCK_SHARED) {
446 				xfs_iunlock(ip, *iolock);
447 				*iolock = XFS_IOLOCK_EXCL;
448 				xfs_ilock(ip, *iolock);
449 				iov_iter_reexpand(from, count);
450 			}
451 			/*
452 			 * We now have an IO submission barrier in place, but
453 			 * AIO can do EOF updates during IO completion and hence
454 			 * we now need to wait for all of them to drain. Non-AIO
455 			 * DIO will have drained before we are given the
456 			 * XFS_IOLOCK_EXCL, and so for most cases this wait is a
457 			 * no-op.
458 			 */
459 			inode_dio_wait(inode);
460 			drained_dio = true;
461 			goto restart;
462 		}
463 
464 		trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
465 		error = xfs_zero_range(ip, isize, iocb->ki_pos - isize, NULL);
466 		if (error)
467 			return error;
468 	} else
469 		spin_unlock(&ip->i_flags_lock);
470 
471 out:
472 	return kiocb_modified(iocb);
473 }
474 
475 static int
476 xfs_dio_write_end_io(
477 	struct kiocb		*iocb,
478 	ssize_t			size,
479 	int			error,
480 	unsigned		flags)
481 {
482 	struct inode		*inode = file_inode(iocb->ki_filp);
483 	struct xfs_inode	*ip = XFS_I(inode);
484 	loff_t			offset = iocb->ki_pos;
485 	unsigned int		nofs_flag;
486 
487 	trace_xfs_end_io_direct_write(ip, offset, size);
488 
489 	if (xfs_is_shutdown(ip->i_mount))
490 		return -EIO;
491 
492 	if (error)
493 		return error;
494 	if (!size)
495 		return 0;
496 
497 	/*
498 	 * Capture amount written on completion as we can't reliably account
499 	 * for it on submission.
500 	 */
501 	XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
502 
503 	/*
504 	 * We can allocate memory here while doing writeback on behalf of
505 	 * memory reclaim.  To avoid memory allocation deadlocks set the
506 	 * task-wide nofs context for the following operations.
507 	 */
508 	nofs_flag = memalloc_nofs_save();
509 
510 	if (flags & IOMAP_DIO_COW) {
511 		error = xfs_reflink_end_cow(ip, offset, size);
512 		if (error)
513 			goto out;
514 	}
515 
516 	/*
517 	 * Unwritten conversion updates the in-core isize after extent
518 	 * conversion but before updating the on-disk size. Updating isize any
519 	 * earlier allows a racing dio read to find unwritten extents before
520 	 * they are converted.
521 	 */
522 	if (flags & IOMAP_DIO_UNWRITTEN) {
523 		error = xfs_iomap_write_unwritten(ip, offset, size, true);
524 		goto out;
525 	}
526 
527 	/*
528 	 * We need to update the in-core inode size here so that we don't end up
529 	 * with the on-disk inode size being outside the in-core inode size. We
530 	 * have no other method of updating EOF for AIO, so always do it here
531 	 * if necessary.
532 	 *
533 	 * We need to lock the test/set EOF update as we can be racing with
534 	 * other IO completions here to update the EOF. Failing to serialise
535 	 * here can result in EOF moving backwards and Bad Things Happen when
536 	 * that occurs.
537 	 *
538 	 * As IO completion only ever extends EOF, we can do an unlocked check
539 	 * here to avoid taking the spinlock. If we land within the current EOF,
540 	 * then we do not need to do an extending update at all, and we don't
541 	 * need to take the lock to check this. If we race with an update moving
542 	 * EOF, then we'll either still be beyond EOF and need to take the lock,
543 	 * or we'll be within EOF and we don't need to take it at all.
544 	 */
545 	if (offset + size <= i_size_read(inode))
546 		goto out;
547 
548 	spin_lock(&ip->i_flags_lock);
549 	if (offset + size > i_size_read(inode)) {
550 		i_size_write(inode, offset + size);
551 		spin_unlock(&ip->i_flags_lock);
552 		error = xfs_setfilesize(ip, offset, size);
553 	} else {
554 		spin_unlock(&ip->i_flags_lock);
555 	}
556 
557 out:
558 	memalloc_nofs_restore(nofs_flag);
559 	return error;
560 }
561 
562 static const struct iomap_dio_ops xfs_dio_write_ops = {
563 	.end_io		= xfs_dio_write_end_io,
564 };
565 
566 /*
567  * Handle block aligned direct I/O writes
568  */
569 static noinline ssize_t
570 xfs_file_dio_write_aligned(
571 	struct xfs_inode	*ip,
572 	struct kiocb		*iocb,
573 	struct iov_iter		*from)
574 {
575 	unsigned int		iolock = XFS_IOLOCK_SHARED;
576 	ssize_t			ret;
577 
578 	ret = xfs_ilock_iocb_for_write(iocb, &iolock);
579 	if (ret)
580 		return ret;
581 	ret = xfs_file_write_checks(iocb, from, &iolock);
582 	if (ret)
583 		goto out_unlock;
584 
585 	/*
586 	 * We don't need to hold the IOLOCK exclusively across the IO, so demote
587 	 * the iolock back to shared if we had to take the exclusive lock in
588 	 * xfs_file_write_checks() for other reasons.
589 	 */
590 	if (iolock == XFS_IOLOCK_EXCL) {
591 		xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
592 		iolock = XFS_IOLOCK_SHARED;
593 	}
594 	trace_xfs_file_direct_write(iocb, from);
595 	ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
596 			   &xfs_dio_write_ops, 0, NULL, 0);
597 out_unlock:
598 	if (iolock)
599 		xfs_iunlock(ip, iolock);
600 	return ret;
601 }
602 
603 /*
604  * Handle block unaligned direct I/O writes
605  *
606  * In most cases direct I/O writes will be done holding IOLOCK_SHARED, allowing
607  * them to be done in parallel with reads and other direct I/O writes.  However,
608  * if the I/O is not aligned to filesystem blocks, the direct I/O layer may need
609  * to do sub-block zeroing and that requires serialisation against other direct
610  * I/O to the same block.  In this case we need to serialise the submission of
611  * the unaligned I/O so that we don't get racing block zeroing in the dio layer.
612  * In the case where sub-block zeroing is not required, we can do concurrent
613  * sub-block dios to the same block successfully.
614  *
615  * Optimistically submit the I/O using the shared lock first, but use the
616  * IOMAP_DIO_OVERWRITE_ONLY flag to tell the lower layers to return -EAGAIN
617  * if block allocation or partial block zeroing would be required.  In that case
618  * we try again with the exclusive lock.
619  */
620 static noinline ssize_t
621 xfs_file_dio_write_unaligned(
622 	struct xfs_inode	*ip,
623 	struct kiocb		*iocb,
624 	struct iov_iter		*from)
625 {
626 	size_t			isize = i_size_read(VFS_I(ip));
627 	size_t			count = iov_iter_count(from);
628 	unsigned int		iolock = XFS_IOLOCK_SHARED;
629 	unsigned int		flags = IOMAP_DIO_OVERWRITE_ONLY;
630 	ssize_t			ret;
631 
632 	/*
633 	 * Extending writes need exclusivity because of the sub-block zeroing
634 	 * that the DIO code always does for partial tail blocks beyond EOF, so
635 	 * don't even bother trying the fast path in this case.
636 	 */
637 	if (iocb->ki_pos > isize || iocb->ki_pos + count >= isize) {
638 		if (iocb->ki_flags & IOCB_NOWAIT)
639 			return -EAGAIN;
640 retry_exclusive:
641 		iolock = XFS_IOLOCK_EXCL;
642 		flags = IOMAP_DIO_FORCE_WAIT;
643 	}
644 
645 	ret = xfs_ilock_iocb_for_write(iocb, &iolock);
646 	if (ret)
647 		return ret;
648 
649 	/*
650 	 * We can't properly handle unaligned direct I/O to reflink files yet,
651 	 * as we can't unshare a partial block.
652 	 */
653 	if (xfs_is_cow_inode(ip)) {
654 		trace_xfs_reflink_bounce_dio_write(iocb, from);
655 		ret = -ENOTBLK;
656 		goto out_unlock;
657 	}
658 
659 	ret = xfs_file_write_checks(iocb, from, &iolock);
660 	if (ret)
661 		goto out_unlock;
662 
663 	/*
664 	 * If we are doing exclusive unaligned I/O, this must be the only I/O
665 	 * in-flight.  Otherwise we risk data corruption due to unwritten extent
666 	 * conversions from the AIO end_io handler.  Wait for all other I/O to
667 	 * drain first.
668 	 */
669 	if (flags & IOMAP_DIO_FORCE_WAIT)
670 		inode_dio_wait(VFS_I(ip));
671 
672 	trace_xfs_file_direct_write(iocb, from);
673 	ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
674 			   &xfs_dio_write_ops, flags, NULL, 0);
675 
676 	/*
677 	 * Retry unaligned I/O with exclusive blocking semantics if the DIO
678 	 * layer rejected it for mapping or locking reasons. If we are doing
679 	 * nonblocking user I/O, propagate the error.
680 	 */
681 	if (ret == -EAGAIN && !(iocb->ki_flags & IOCB_NOWAIT)) {
682 		ASSERT(flags & IOMAP_DIO_OVERWRITE_ONLY);
683 		xfs_iunlock(ip, iolock);
684 		goto retry_exclusive;
685 	}
686 
687 out_unlock:
688 	if (iolock)
689 		xfs_iunlock(ip, iolock);
690 	return ret;
691 }
692 
693 static ssize_t
694 xfs_file_dio_write(
695 	struct kiocb		*iocb,
696 	struct iov_iter		*from)
697 {
698 	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
699 	struct xfs_buftarg      *target = xfs_inode_buftarg(ip);
700 	size_t			count = iov_iter_count(from);
701 
702 	/* direct I/O must be aligned to device logical sector size */
703 	if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
704 		return -EINVAL;
705 	if ((iocb->ki_pos | count) & ip->i_mount->m_blockmask)
706 		return xfs_file_dio_write_unaligned(ip, iocb, from);
707 	return xfs_file_dio_write_aligned(ip, iocb, from);
708 }
709 
710 static noinline ssize_t
711 xfs_file_dax_write(
712 	struct kiocb		*iocb,
713 	struct iov_iter		*from)
714 {
715 	struct inode		*inode = iocb->ki_filp->f_mapping->host;
716 	struct xfs_inode	*ip = XFS_I(inode);
717 	unsigned int		iolock = XFS_IOLOCK_EXCL;
718 	ssize_t			ret, error = 0;
719 	loff_t			pos;
720 
721 	ret = xfs_ilock_iocb(iocb, iolock);
722 	if (ret)
723 		return ret;
724 	ret = xfs_file_write_checks(iocb, from, &iolock);
725 	if (ret)
726 		goto out;
727 
728 	pos = iocb->ki_pos;
729 
730 	trace_xfs_file_dax_write(iocb, from);
731 	ret = dax_iomap_rw(iocb, from, &xfs_dax_write_iomap_ops);
732 	if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
733 		i_size_write(inode, iocb->ki_pos);
734 		error = xfs_setfilesize(ip, pos, ret);
735 	}
736 out:
737 	if (iolock)
738 		xfs_iunlock(ip, iolock);
739 	if (error)
740 		return error;
741 
742 	if (ret > 0) {
743 		XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
744 
745 		/* Handle various SYNC-type writes */
746 		ret = generic_write_sync(iocb, ret);
747 	}
748 	return ret;
749 }
750 
751 STATIC ssize_t
752 xfs_file_buffered_write(
753 	struct kiocb		*iocb,
754 	struct iov_iter		*from)
755 {
756 	struct inode		*inode = iocb->ki_filp->f_mapping->host;
757 	struct xfs_inode	*ip = XFS_I(inode);
758 	ssize_t			ret;
759 	bool			cleared_space = false;
760 	unsigned int		iolock;
761 
762 write_retry:
763 	iolock = XFS_IOLOCK_EXCL;
764 	ret = xfs_ilock_iocb(iocb, iolock);
765 	if (ret)
766 		return ret;
767 
768 	ret = xfs_file_write_checks(iocb, from, &iolock);
769 	if (ret)
770 		goto out;
771 
772 	trace_xfs_file_buffered_write(iocb, from);
773 	ret = iomap_file_buffered_write(iocb, from,
774 			&xfs_buffered_write_iomap_ops);
775 
776 	/*
777 	 * If we hit a space limit, try to free up some lingering preallocated
778 	 * space before returning an error. In the case of ENOSPC, first try to
779 	 * write back all dirty inodes to free up some of the excess reserved
780 	 * metadata space. This reduces the chances that the eofblocks scan
781 	 * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
782 	 * also behaves as a filter to prevent too many eofblocks scans from
783 	 * running at the same time.  Use a synchronous scan to increase the
784 	 * effectiveness of the scan.
785 	 */
786 	if (ret == -EDQUOT && !cleared_space) {
787 		xfs_iunlock(ip, iolock);
788 		xfs_blockgc_free_quota(ip, XFS_ICWALK_FLAG_SYNC);
789 		cleared_space = true;
790 		goto write_retry;
791 	} else if (ret == -ENOSPC && !cleared_space) {
792 		struct xfs_icwalk	icw = {0};
793 
794 		cleared_space = true;
795 		xfs_flush_inodes(ip->i_mount);
796 
797 		xfs_iunlock(ip, iolock);
798 		icw.icw_flags = XFS_ICWALK_FLAG_SYNC;
799 		xfs_blockgc_free_space(ip->i_mount, &icw);
800 		goto write_retry;
801 	}
802 
803 out:
804 	if (iolock)
805 		xfs_iunlock(ip, iolock);
806 
807 	if (ret > 0) {
808 		XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
809 		/* Handle various SYNC-type writes */
810 		ret = generic_write_sync(iocb, ret);
811 	}
812 	return ret;
813 }
814 
815 STATIC ssize_t
816 xfs_file_write_iter(
817 	struct kiocb		*iocb,
818 	struct iov_iter		*from)
819 {
820 	struct inode		*inode = iocb->ki_filp->f_mapping->host;
821 	struct xfs_inode	*ip = XFS_I(inode);
822 	ssize_t			ret;
823 	size_t			ocount = iov_iter_count(from);
824 
825 	XFS_STATS_INC(ip->i_mount, xs_write_calls);
826 
827 	if (ocount == 0)
828 		return 0;
829 
830 	if (xfs_is_shutdown(ip->i_mount))
831 		return -EIO;
832 
833 	if (IS_DAX(inode))
834 		return xfs_file_dax_write(iocb, from);
835 
836 	if (iocb->ki_flags & IOCB_DIRECT) {
837 		/*
838 		 * Allow a directio write to fall back to a buffered
839 		 * write *only* in the case that we're doing a reflink
840 		 * CoW.  In all other directio scenarios we do not
841 		 * allow an operation to fall back to buffered mode.
842 		 */
843 		ret = xfs_file_dio_write(iocb, from);
844 		if (ret != -ENOTBLK)
845 			return ret;
846 	}
847 
848 	return xfs_file_buffered_write(iocb, from);
849 }
850 
851 /* Does this file, inode, or mount want synchronous writes? */
852 static inline bool xfs_file_sync_writes(struct file *filp)
853 {
854 	struct xfs_inode	*ip = XFS_I(file_inode(filp));
855 
856 	if (xfs_has_wsync(ip->i_mount))
857 		return true;
858 	if (filp->f_flags & (__O_SYNC | O_DSYNC))
859 		return true;
860 	if (IS_SYNC(file_inode(filp)))
861 		return true;
862 
863 	return false;
864 }
865 
866 #define	XFS_FALLOC_FL_SUPPORTED						\
867 		(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |		\
868 		 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |	\
869 		 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
870 
871 STATIC long
872 xfs_file_fallocate(
873 	struct file		*file,
874 	int			mode,
875 	loff_t			offset,
876 	loff_t			len)
877 {
878 	struct inode		*inode = file_inode(file);
879 	struct xfs_inode	*ip = XFS_I(inode);
880 	long			error;
881 	uint			iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
882 	loff_t			new_size = 0;
883 	bool			do_file_insert = false;
884 
885 	if (!S_ISREG(inode->i_mode))
886 		return -EINVAL;
887 	if (mode & ~XFS_FALLOC_FL_SUPPORTED)
888 		return -EOPNOTSUPP;
889 
890 	xfs_ilock(ip, iolock);
891 	error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
892 	if (error)
893 		goto out_unlock;
894 
895 	/*
896 	 * Must wait for all AIO to complete before we continue as AIO can
897 	 * change the file size on completion without holding any locks we
898 	 * currently hold. We must do this first because AIO can update both
899 	 * the on disk and in memory inode sizes, and the operations that follow
900 	 * require the in-memory size to be fully up-to-date.
901 	 */
902 	inode_dio_wait(inode);
903 
904 	/*
905 	 * Now AIO and DIO has drained we flush and (if necessary) invalidate
906 	 * the cached range over the first operation we are about to run.
907 	 *
908 	 * We care about zero and collapse here because they both run a hole
909 	 * punch over the range first. Because that can zero data, and the range
910 	 * of invalidation for the shift operations is much larger, we still do
911 	 * the required flush for collapse in xfs_prepare_shift().
912 	 *
913 	 * Insert has the same range requirements as collapse, and we extend the
914 	 * file first which can zero data. Hence insert has the same
915 	 * flush/invalidate requirements as collapse and so they are both
916 	 * handled at the right time by xfs_prepare_shift().
917 	 */
918 	if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE |
919 		    FALLOC_FL_COLLAPSE_RANGE)) {
920 		error = xfs_flush_unmap_range(ip, offset, len);
921 		if (error)
922 			goto out_unlock;
923 	}
924 
925 	error = file_modified(file);
926 	if (error)
927 		goto out_unlock;
928 
929 	if (mode & FALLOC_FL_PUNCH_HOLE) {
930 		error = xfs_free_file_space(ip, offset, len);
931 		if (error)
932 			goto out_unlock;
933 	} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
934 		if (!xfs_is_falloc_aligned(ip, offset, len)) {
935 			error = -EINVAL;
936 			goto out_unlock;
937 		}
938 
939 		/*
940 		 * There is no need to overlap collapse range with EOF,
941 		 * in which case it is effectively a truncate operation
942 		 */
943 		if (offset + len >= i_size_read(inode)) {
944 			error = -EINVAL;
945 			goto out_unlock;
946 		}
947 
948 		new_size = i_size_read(inode) - len;
949 
950 		error = xfs_collapse_file_space(ip, offset, len);
951 		if (error)
952 			goto out_unlock;
953 	} else if (mode & FALLOC_FL_INSERT_RANGE) {
954 		loff_t		isize = i_size_read(inode);
955 
956 		if (!xfs_is_falloc_aligned(ip, offset, len)) {
957 			error = -EINVAL;
958 			goto out_unlock;
959 		}
960 
961 		/*
962 		 * New inode size must not exceed ->s_maxbytes, accounting for
963 		 * possible signed overflow.
964 		 */
965 		if (inode->i_sb->s_maxbytes - isize < len) {
966 			error = -EFBIG;
967 			goto out_unlock;
968 		}
969 		new_size = isize + len;
970 
971 		/* Offset should be less than i_size */
972 		if (offset >= isize) {
973 			error = -EINVAL;
974 			goto out_unlock;
975 		}
976 		do_file_insert = true;
977 	} else {
978 		if (!(mode & FALLOC_FL_KEEP_SIZE) &&
979 		    offset + len > i_size_read(inode)) {
980 			new_size = offset + len;
981 			error = inode_newsize_ok(inode, new_size);
982 			if (error)
983 				goto out_unlock;
984 		}
985 
986 		if (mode & FALLOC_FL_ZERO_RANGE) {
987 			/*
988 			 * Punch a hole and prealloc the range.  We use a hole
989 			 * punch rather than unwritten extent conversion for two
990 			 * reasons:
991 			 *
992 			 *   1.) Hole punch handles partial block zeroing for us.
993 			 *   2.) If prealloc returns ENOSPC, the file range is
994 			 *       still zero-valued by virtue of the hole punch.
995 			 */
996 			unsigned int blksize = i_blocksize(inode);
997 
998 			trace_xfs_zero_file_space(ip);
999 
1000 			error = xfs_free_file_space(ip, offset, len);
1001 			if (error)
1002 				goto out_unlock;
1003 
1004 			len = round_up(offset + len, blksize) -
1005 			      round_down(offset, blksize);
1006 			offset = round_down(offset, blksize);
1007 		} else if (mode & FALLOC_FL_UNSHARE_RANGE) {
1008 			error = xfs_reflink_unshare(ip, offset, len);
1009 			if (error)
1010 				goto out_unlock;
1011 		} else {
1012 			/*
1013 			 * If always_cow mode we can't use preallocations and
1014 			 * thus should not create them.
1015 			 */
1016 			if (xfs_is_always_cow_inode(ip)) {
1017 				error = -EOPNOTSUPP;
1018 				goto out_unlock;
1019 			}
1020 		}
1021 
1022 		if (!xfs_is_always_cow_inode(ip)) {
1023 			error = xfs_alloc_file_space(ip, offset, len);
1024 			if (error)
1025 				goto out_unlock;
1026 		}
1027 	}
1028 
1029 	/* Change file size if needed */
1030 	if (new_size) {
1031 		struct iattr iattr;
1032 
1033 		iattr.ia_valid = ATTR_SIZE;
1034 		iattr.ia_size = new_size;
1035 		error = xfs_vn_setattr_size(file_mnt_idmap(file),
1036 					    file_dentry(file), &iattr);
1037 		if (error)
1038 			goto out_unlock;
1039 	}
1040 
1041 	/*
1042 	 * Perform hole insertion now that the file size has been
1043 	 * updated so that if we crash during the operation we don't
1044 	 * leave shifted extents past EOF and hence losing access to
1045 	 * the data that is contained within them.
1046 	 */
1047 	if (do_file_insert) {
1048 		error = xfs_insert_file_space(ip, offset, len);
1049 		if (error)
1050 			goto out_unlock;
1051 	}
1052 
1053 	if (xfs_file_sync_writes(file))
1054 		error = xfs_log_force_inode(ip);
1055 
1056 out_unlock:
1057 	xfs_iunlock(ip, iolock);
1058 	return error;
1059 }
1060 
1061 STATIC int
1062 xfs_file_fadvise(
1063 	struct file	*file,
1064 	loff_t		start,
1065 	loff_t		end,
1066 	int		advice)
1067 {
1068 	struct xfs_inode *ip = XFS_I(file_inode(file));
1069 	int ret;
1070 	int lockflags = 0;
1071 
1072 	/*
1073 	 * Operations creating pages in page cache need protection from hole
1074 	 * punching and similar ops
1075 	 */
1076 	if (advice == POSIX_FADV_WILLNEED) {
1077 		lockflags = XFS_IOLOCK_SHARED;
1078 		xfs_ilock(ip, lockflags);
1079 	}
1080 	ret = generic_fadvise(file, start, end, advice);
1081 	if (lockflags)
1082 		xfs_iunlock(ip, lockflags);
1083 	return ret;
1084 }
1085 
1086 STATIC loff_t
1087 xfs_file_remap_range(
1088 	struct file		*file_in,
1089 	loff_t			pos_in,
1090 	struct file		*file_out,
1091 	loff_t			pos_out,
1092 	loff_t			len,
1093 	unsigned int		remap_flags)
1094 {
1095 	struct inode		*inode_in = file_inode(file_in);
1096 	struct xfs_inode	*src = XFS_I(inode_in);
1097 	struct inode		*inode_out = file_inode(file_out);
1098 	struct xfs_inode	*dest = XFS_I(inode_out);
1099 	struct xfs_mount	*mp = src->i_mount;
1100 	loff_t			remapped = 0;
1101 	xfs_extlen_t		cowextsize;
1102 	int			ret;
1103 
1104 	if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1105 		return -EINVAL;
1106 
1107 	if (!xfs_has_reflink(mp))
1108 		return -EOPNOTSUPP;
1109 
1110 	if (xfs_is_shutdown(mp))
1111 		return -EIO;
1112 
1113 	/* Prepare and then clone file data. */
1114 	ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
1115 			&len, remap_flags);
1116 	if (ret || len == 0)
1117 		return ret;
1118 
1119 	trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
1120 
1121 	ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
1122 			&remapped);
1123 	if (ret)
1124 		goto out_unlock;
1125 
1126 	/*
1127 	 * Carry the cowextsize hint from src to dest if we're sharing the
1128 	 * entire source file to the entire destination file, the source file
1129 	 * has a cowextsize hint, and the destination file does not.
1130 	 */
1131 	cowextsize = 0;
1132 	if (pos_in == 0 && len == i_size_read(inode_in) &&
1133 	    (src->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1134 	    pos_out == 0 && len >= i_size_read(inode_out) &&
1135 	    !(dest->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE))
1136 		cowextsize = src->i_cowextsize;
1137 
1138 	ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1139 			remap_flags);
1140 	if (ret)
1141 		goto out_unlock;
1142 
1143 	if (xfs_file_sync_writes(file_in) || xfs_file_sync_writes(file_out))
1144 		xfs_log_force_inode(dest);
1145 out_unlock:
1146 	xfs_iunlock2_remapping(src, dest);
1147 	if (ret)
1148 		trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1149 	return remapped > 0 ? remapped : ret;
1150 }
1151 
1152 STATIC int
1153 xfs_file_open(
1154 	struct inode	*inode,
1155 	struct file	*file)
1156 {
1157 	if (xfs_is_shutdown(XFS_M(inode->i_sb)))
1158 		return -EIO;
1159 	file->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
1160 	return generic_file_open(inode, file);
1161 }
1162 
1163 STATIC int
1164 xfs_dir_open(
1165 	struct inode	*inode,
1166 	struct file	*file)
1167 {
1168 	struct xfs_inode *ip = XFS_I(inode);
1169 	unsigned int	mode;
1170 	int		error;
1171 
1172 	if (xfs_is_shutdown(ip->i_mount))
1173 		return -EIO;
1174 	error = generic_file_open(inode, file);
1175 	if (error)
1176 		return error;
1177 
1178 	/*
1179 	 * If there are any blocks, read-ahead block 0 as we're almost
1180 	 * certain to have the next operation be a read there.
1181 	 */
1182 	mode = xfs_ilock_data_map_shared(ip);
1183 	if (ip->i_df.if_nextents > 0)
1184 		error = xfs_dir3_data_readahead(ip, 0, 0);
1185 	xfs_iunlock(ip, mode);
1186 	return error;
1187 }
1188 
1189 STATIC int
1190 xfs_file_release(
1191 	struct inode	*inode,
1192 	struct file	*filp)
1193 {
1194 	return xfs_release(XFS_I(inode));
1195 }
1196 
1197 STATIC int
1198 xfs_file_readdir(
1199 	struct file	*file,
1200 	struct dir_context *ctx)
1201 {
1202 	struct inode	*inode = file_inode(file);
1203 	xfs_inode_t	*ip = XFS_I(inode);
1204 	size_t		bufsize;
1205 
1206 	/*
1207 	 * The Linux API doesn't pass down the total size of the buffer
1208 	 * we read into down to the filesystem.  With the filldir concept
1209 	 * it's not needed for correct information, but the XFS dir2 leaf
1210 	 * code wants an estimate of the buffer size to calculate it's
1211 	 * readahead window and size the buffers used for mapping to
1212 	 * physical blocks.
1213 	 *
1214 	 * Try to give it an estimate that's good enough, maybe at some
1215 	 * point we can change the ->readdir prototype to include the
1216 	 * buffer size.  For now we use the current glibc buffer size.
1217 	 */
1218 	bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_disk_size);
1219 
1220 	return xfs_readdir(NULL, ip, ctx, bufsize);
1221 }
1222 
1223 STATIC loff_t
1224 xfs_file_llseek(
1225 	struct file	*file,
1226 	loff_t		offset,
1227 	int		whence)
1228 {
1229 	struct inode		*inode = file->f_mapping->host;
1230 
1231 	if (xfs_is_shutdown(XFS_I(inode)->i_mount))
1232 		return -EIO;
1233 
1234 	switch (whence) {
1235 	default:
1236 		return generic_file_llseek(file, offset, whence);
1237 	case SEEK_HOLE:
1238 		offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
1239 		break;
1240 	case SEEK_DATA:
1241 		offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
1242 		break;
1243 	}
1244 
1245 	if (offset < 0)
1246 		return offset;
1247 	return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1248 }
1249 
1250 #ifdef CONFIG_FS_DAX
1251 static inline vm_fault_t
1252 xfs_dax_fault(
1253 	struct vm_fault		*vmf,
1254 	unsigned int		order,
1255 	bool			write_fault,
1256 	pfn_t			*pfn)
1257 {
1258 	return dax_iomap_fault(vmf, order, pfn, NULL,
1259 			(write_fault && !vmf->cow_page) ?
1260 				&xfs_dax_write_iomap_ops :
1261 				&xfs_read_iomap_ops);
1262 }
1263 #else
1264 static inline vm_fault_t
1265 xfs_dax_fault(
1266 	struct vm_fault		*vmf,
1267 	unsigned int		order,
1268 	bool			write_fault,
1269 	pfn_t			*pfn)
1270 {
1271 	ASSERT(0);
1272 	return VM_FAULT_SIGBUS;
1273 }
1274 #endif
1275 
1276 /*
1277  * Locking for serialisation of IO during page faults. This results in a lock
1278  * ordering of:
1279  *
1280  * mmap_lock (MM)
1281  *   sb_start_pagefault(vfs, freeze)
1282  *     invalidate_lock (vfs/XFS_MMAPLOCK - truncate serialisation)
1283  *       page_lock (MM)
1284  *         i_lock (XFS - extent map serialisation)
1285  */
1286 static vm_fault_t
1287 __xfs_filemap_fault(
1288 	struct vm_fault		*vmf,
1289 	unsigned int		order,
1290 	bool			write_fault)
1291 {
1292 	struct inode		*inode = file_inode(vmf->vma->vm_file);
1293 	struct xfs_inode	*ip = XFS_I(inode);
1294 	vm_fault_t		ret;
1295 	unsigned int		lock_mode = 0;
1296 
1297 	trace_xfs_filemap_fault(ip, order, write_fault);
1298 
1299 	if (write_fault) {
1300 		sb_start_pagefault(inode->i_sb);
1301 		file_update_time(vmf->vma->vm_file);
1302 	}
1303 
1304 	if (IS_DAX(inode) || write_fault)
1305 		lock_mode = xfs_ilock_for_write_fault(XFS_I(inode));
1306 
1307 	if (IS_DAX(inode)) {
1308 		pfn_t pfn;
1309 
1310 		ret = xfs_dax_fault(vmf, order, write_fault, &pfn);
1311 		if (ret & VM_FAULT_NEEDDSYNC)
1312 			ret = dax_finish_sync_fault(vmf, order, pfn);
1313 	} else if (write_fault) {
1314 		ret = iomap_page_mkwrite(vmf, &xfs_page_mkwrite_iomap_ops);
1315 	} else {
1316 		ret = filemap_fault(vmf);
1317 	}
1318 
1319 	if (lock_mode)
1320 		xfs_iunlock(XFS_I(inode), lock_mode);
1321 
1322 	if (write_fault)
1323 		sb_end_pagefault(inode->i_sb);
1324 	return ret;
1325 }
1326 
1327 static inline bool
1328 xfs_is_write_fault(
1329 	struct vm_fault		*vmf)
1330 {
1331 	return (vmf->flags & FAULT_FLAG_WRITE) &&
1332 	       (vmf->vma->vm_flags & VM_SHARED);
1333 }
1334 
1335 static vm_fault_t
1336 xfs_filemap_fault(
1337 	struct vm_fault		*vmf)
1338 {
1339 	/* DAX can shortcut the normal fault path on write faults! */
1340 	return __xfs_filemap_fault(vmf, 0,
1341 			IS_DAX(file_inode(vmf->vma->vm_file)) &&
1342 			xfs_is_write_fault(vmf));
1343 }
1344 
1345 static vm_fault_t
1346 xfs_filemap_huge_fault(
1347 	struct vm_fault		*vmf,
1348 	unsigned int		order)
1349 {
1350 	if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1351 		return VM_FAULT_FALLBACK;
1352 
1353 	/* DAX can shortcut the normal fault path on write faults! */
1354 	return __xfs_filemap_fault(vmf, order,
1355 			xfs_is_write_fault(vmf));
1356 }
1357 
1358 static vm_fault_t
1359 xfs_filemap_page_mkwrite(
1360 	struct vm_fault		*vmf)
1361 {
1362 	return __xfs_filemap_fault(vmf, 0, true);
1363 }
1364 
1365 /*
1366  * pfn_mkwrite was originally intended to ensure we capture time stamp updates
1367  * on write faults. In reality, it needs to serialise against truncate and
1368  * prepare memory for writing so handle is as standard write fault.
1369  */
1370 static vm_fault_t
1371 xfs_filemap_pfn_mkwrite(
1372 	struct vm_fault		*vmf)
1373 {
1374 
1375 	return __xfs_filemap_fault(vmf, 0, true);
1376 }
1377 
1378 static const struct vm_operations_struct xfs_file_vm_ops = {
1379 	.fault		= xfs_filemap_fault,
1380 	.huge_fault	= xfs_filemap_huge_fault,
1381 	.map_pages	= filemap_map_pages,
1382 	.page_mkwrite	= xfs_filemap_page_mkwrite,
1383 	.pfn_mkwrite	= xfs_filemap_pfn_mkwrite,
1384 };
1385 
1386 STATIC int
1387 xfs_file_mmap(
1388 	struct file		*file,
1389 	struct vm_area_struct	*vma)
1390 {
1391 	struct inode		*inode = file_inode(file);
1392 	struct xfs_buftarg	*target = xfs_inode_buftarg(XFS_I(inode));
1393 
1394 	/*
1395 	 * We don't support synchronous mappings for non-DAX files and
1396 	 * for DAX files if underneath dax_device is not synchronous.
1397 	 */
1398 	if (!daxdev_mapping_supported(vma, target->bt_daxdev))
1399 		return -EOPNOTSUPP;
1400 
1401 	file_accessed(file);
1402 	vma->vm_ops = &xfs_file_vm_ops;
1403 	if (IS_DAX(inode))
1404 		vm_flags_set(vma, VM_HUGEPAGE);
1405 	return 0;
1406 }
1407 
1408 const struct file_operations xfs_file_operations = {
1409 	.llseek		= xfs_file_llseek,
1410 	.read_iter	= xfs_file_read_iter,
1411 	.write_iter	= xfs_file_write_iter,
1412 	.splice_read	= xfs_file_splice_read,
1413 	.splice_write	= iter_file_splice_write,
1414 	.iopoll		= iocb_bio_iopoll,
1415 	.unlocked_ioctl	= xfs_file_ioctl,
1416 #ifdef CONFIG_COMPAT
1417 	.compat_ioctl	= xfs_file_compat_ioctl,
1418 #endif
1419 	.mmap		= xfs_file_mmap,
1420 	.open		= xfs_file_open,
1421 	.release	= xfs_file_release,
1422 	.fsync		= xfs_file_fsync,
1423 	.get_unmapped_area = thp_get_unmapped_area,
1424 	.fallocate	= xfs_file_fallocate,
1425 	.fadvise	= xfs_file_fadvise,
1426 	.remap_file_range = xfs_file_remap_range,
1427 	.fop_flags	= FOP_MMAP_SYNC | FOP_BUFFER_RASYNC |
1428 			  FOP_BUFFER_WASYNC | FOP_DIO_PARALLEL_WRITE,
1429 };
1430 
1431 const struct file_operations xfs_dir_file_operations = {
1432 	.open		= xfs_dir_open,
1433 	.read		= generic_read_dir,
1434 	.iterate_shared	= xfs_file_readdir,
1435 	.llseek		= generic_file_llseek,
1436 	.unlocked_ioctl	= xfs_file_ioctl,
1437 #ifdef CONFIG_COMPAT
1438 	.compat_ioctl	= xfs_file_compat_ioctl,
1439 #endif
1440 	.fsync		= xfs_dir_fsync,
1441 };
1442