1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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"
17 #include "xfs_bmap_util.h"
19 #include "xfs_dir2_priv.h"
20 #include "xfs_ioctl.h"
21 #include "xfs_trace.h"
23 #include "xfs_icache.h"
25 #include "xfs_iomap.h"
26 #include "xfs_reflink.h"
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>
36 static const struct vm_operations_struct xfs_file_vm_ops;
39 * Decide if the given file range is aligned to the size of the fundamental
40 * allocation unit for the file.
43 xfs_is_falloc_aligned(
48 unsigned int alloc_unit = xfs_inode_alloc_unitsize(ip);
50 if (!is_power_of_2(alloc_unit))
51 return isaligned_64(pos, alloc_unit) &&
52 isaligned_64(len, alloc_unit);
54 return !((pos | len) & (alloc_unit - 1));
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.
70 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
72 trace_xfs_dir_fsync(ip);
73 return xfs_log_force_inode(ip);
81 if (!xfs_ipincount(ip))
83 if (datasync && !(ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
85 return ip->i_itemp->ili_commit_seq;
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.
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.
103 struct xfs_inode *ip,
110 xfs_ilock(ip, XFS_ILOCK_SHARED);
111 seq = xfs_fsync_seq(ip, datasync);
113 error = xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC,
116 spin_lock(&ip->i_itemp->ili_lock);
117 ip->i_itemp->ili_fsync_fields = 0;
118 spin_unlock(&ip->i_itemp->ili_lock);
120 xfs_iunlock(ip, XFS_ILOCK_SHARED);
131 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
132 struct xfs_mount *mp = ip->i_mount;
136 trace_xfs_file_fsync(ip);
138 error = file_write_and_wait_range(file, start, end);
142 if (xfs_is_shutdown(mp))
145 xfs_iflags_clear(ip, XFS_ITRUNCATED);
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.
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);
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.
164 if (xfs_ipincount(ip)) {
165 err2 = xfs_fsync_flush_log(ip, datasync, &log_flushed);
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
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);
190 unsigned int lock_mode)
192 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
194 if (iocb->ki_flags & IOCB_NOWAIT) {
195 if (!xfs_ilock_nowait(ip, lock_mode))
198 xfs_ilock(ip, lock_mode);
205 xfs_ilock_iocb_for_write(
207 unsigned int *lock_mode)
210 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
212 ret = xfs_ilock_iocb(iocb, *lock_mode);
216 if (*lock_mode == XFS_IOLOCK_EXCL)
218 if (!xfs_iflags_test(ip, XFS_IREMAPPING))
221 xfs_iunlock(ip, *lock_mode);
222 *lock_mode = XFS_IOLOCK_EXCL;
223 return xfs_ilock_iocb(iocb, *lock_mode);
227 xfs_ilock_for_write_fault(
228 struct xfs_inode *ip)
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;
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;
246 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
249 trace_xfs_file_direct_read(iocb, to);
251 if (!iov_iter_count(to))
252 return 0; /* skip atime */
254 file_accessed(iocb->ki_filp);
256 ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
259 ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL, 0, NULL, 0);
260 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
265 static noinline ssize_t
270 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
273 trace_xfs_file_dax_read(iocb, to);
275 if (!iov_iter_count(to))
276 return 0; /* skip atime */
278 ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
281 ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
282 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
284 file_accessed(iocb->ki_filp);
289 xfs_file_buffered_read(
293 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
296 trace_xfs_file_buffered_read(iocb, to);
298 ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
301 ret = generic_file_read_iter(iocb, to);
302 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
312 struct inode *inode = file_inode(iocb->ki_filp);
313 struct xfs_mount *mp = XFS_I(inode)->i_mount;
316 XFS_STATS_INC(mp, xs_read_calls);
318 if (xfs_is_shutdown(mp))
322 ret = xfs_file_dax_read(iocb, to);
323 else if (iocb->ki_flags & IOCB_DIRECT)
324 ret = xfs_file_dio_read(iocb, to);
326 ret = xfs_file_buffered_read(iocb, to);
329 XFS_STATS_ADD(mp, xs_read_bytes, ret);
334 xfs_file_splice_read(
337 struct pipe_inode_info *pipe,
341 struct inode *inode = file_inode(in);
342 struct xfs_inode *ip = XFS_I(inode);
343 struct xfs_mount *mp = ip->i_mount;
346 XFS_STATS_INC(mp, xs_read_calls);
348 if (xfs_is_shutdown(mp))
351 trace_xfs_file_splice_read(ip, *ppos, len);
353 xfs_ilock(ip, XFS_IOLOCK_SHARED);
354 ret = filemap_splice_read(in, ppos, pipe, len, flags);
355 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
357 XFS_STATS_ADD(mp, xs_read_bytes, ret);
362 * Common pre-write limit and setup checks.
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.
369 xfs_file_write_checks(
371 struct iov_iter *from,
372 unsigned int *iolock)
374 struct file *file = iocb->ki_filp;
375 struct inode *inode = file->f_mapping->host;
376 struct xfs_inode *ip = XFS_I(inode);
378 size_t count = iov_iter_count(from);
379 bool drained_dio = false;
383 error = generic_write_checks(iocb, from);
387 if (iocb->ki_flags & IOCB_NOWAIT) {
388 error = break_layout(inode, false);
389 if (error == -EWOULDBLOCK)
392 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
399 * For changing security info in file_remove_privs() we need i_rwsem
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);
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.
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.
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.
433 if (iocb->ki_pos <= i_size_read(inode))
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);
441 if (iocb->ki_flags & IOCB_NOWAIT)
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);
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
459 inode_dio_wait(inode);
464 trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
465 error = xfs_zero_range(ip, isize, iocb->ki_pos - isize, NULL);
469 spin_unlock(&ip->i_flags_lock);
472 return kiocb_modified(iocb);
476 xfs_dio_write_end_io(
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;
487 trace_xfs_end_io_direct_write(ip, offset, size);
489 if (xfs_is_shutdown(ip->i_mount))
498 * Capture amount written on completion as we can't reliably account
499 * for it on submission.
501 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
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.
508 nofs_flag = memalloc_nofs_save();
510 if (flags & IOMAP_DIO_COW) {
511 error = xfs_reflink_end_cow(ip, offset, size);
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.
522 if (flags & IOMAP_DIO_UNWRITTEN) {
523 error = xfs_iomap_write_unwritten(ip, offset, size, true);
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
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
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.
545 if (offset + size <= i_size_read(inode))
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);
554 spin_unlock(&ip->i_flags_lock);
558 memalloc_nofs_restore(nofs_flag);
562 static const struct iomap_dio_ops xfs_dio_write_ops = {
563 .end_io = xfs_dio_write_end_io,
567 * Handle block aligned direct I/O writes
569 static noinline ssize_t
570 xfs_file_dio_write_aligned(
571 struct xfs_inode *ip,
573 struct iov_iter *from)
575 unsigned int iolock = XFS_IOLOCK_SHARED;
578 ret = xfs_ilock_iocb_for_write(iocb, &iolock);
581 ret = xfs_file_write_checks(iocb, from, &iolock);
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.
590 if (iolock == XFS_IOLOCK_EXCL) {
591 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
592 iolock = XFS_IOLOCK_SHARED;
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);
599 xfs_iunlock(ip, iolock);
604 * Handle block unaligned direct I/O writes
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.
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.
620 static noinline ssize_t
621 xfs_file_dio_write_unaligned(
622 struct xfs_inode *ip,
624 struct iov_iter *from)
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;
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.
637 if (iocb->ki_pos > isize || iocb->ki_pos + count >= isize) {
638 if (iocb->ki_flags & IOCB_NOWAIT)
641 iolock = XFS_IOLOCK_EXCL;
642 flags = IOMAP_DIO_FORCE_WAIT;
645 ret = xfs_ilock_iocb_for_write(iocb, &iolock);
650 * We can't properly handle unaligned direct I/O to reflink files yet,
651 * as we can't unshare a partial block.
653 if (xfs_is_cow_inode(ip)) {
654 trace_xfs_reflink_bounce_dio_write(iocb, from);
659 ret = xfs_file_write_checks(iocb, from, &iolock);
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
669 if (flags & IOMAP_DIO_FORCE_WAIT)
670 inode_dio_wait(VFS_I(ip));
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);
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.
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;
689 xfs_iunlock(ip, iolock);
696 struct iov_iter *from)
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);
702 /* direct I/O must be aligned to device logical sector size */
703 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
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);
710 static noinline ssize_t
713 struct iov_iter *from)
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;
721 ret = xfs_ilock_iocb(iocb, iolock);
724 ret = xfs_file_write_checks(iocb, from, &iolock);
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);
738 xfs_iunlock(ip, iolock);
743 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
745 /* Handle various SYNC-type writes */
746 ret = generic_write_sync(iocb, ret);
752 xfs_file_buffered_write(
754 struct iov_iter *from)
756 struct inode *inode = iocb->ki_filp->f_mapping->host;
757 struct xfs_inode *ip = XFS_I(inode);
759 bool cleared_space = false;
763 iolock = XFS_IOLOCK_EXCL;
764 ret = xfs_ilock_iocb(iocb, iolock);
768 ret = xfs_file_write_checks(iocb, from, &iolock);
772 trace_xfs_file_buffered_write(iocb, from);
773 ret = iomap_file_buffered_write(iocb, from,
774 &xfs_buffered_write_iomap_ops);
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.
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;
791 } else if (ret == -ENOSPC && !cleared_space) {
792 struct xfs_icwalk icw = {0};
794 cleared_space = true;
795 xfs_flush_inodes(ip->i_mount);
797 xfs_iunlock(ip, iolock);
798 icw.icw_flags = XFS_ICWALK_FLAG_SYNC;
799 xfs_blockgc_free_space(ip->i_mount, &icw);
805 xfs_iunlock(ip, iolock);
808 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
809 /* Handle various SYNC-type writes */
810 ret = generic_write_sync(iocb, ret);
818 struct iov_iter *from)
820 struct inode *inode = iocb->ki_filp->f_mapping->host;
821 struct xfs_inode *ip = XFS_I(inode);
823 size_t ocount = iov_iter_count(from);
825 XFS_STATS_INC(ip->i_mount, xs_write_calls);
830 if (xfs_is_shutdown(ip->i_mount))
834 return xfs_file_dax_write(iocb, from);
836 if (iocb->ki_flags & IOCB_DIRECT) {
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.
843 ret = xfs_file_dio_write(iocb, from);
848 return xfs_file_buffered_write(iocb, from);
851 /* Does this file, inode, or mount want synchronous writes? */
852 static inline bool xfs_file_sync_writes(struct file *filp)
854 struct xfs_inode *ip = XFS_I(file_inode(filp));
856 if (xfs_has_wsync(ip->i_mount))
858 if (filp->f_flags & (__O_SYNC | O_DSYNC))
860 if (IS_SYNC(file_inode(filp)))
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)
878 struct inode *inode = file_inode(file);
879 struct xfs_inode *ip = XFS_I(inode);
881 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
883 bool do_file_insert = false;
885 if (!S_ISREG(inode->i_mode))
887 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
890 xfs_ilock(ip, iolock);
891 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
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.
902 inode_dio_wait(inode);
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.
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().
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().
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);
925 error = file_modified(file);
929 if (mode & FALLOC_FL_PUNCH_HOLE) {
930 error = xfs_free_file_space(ip, offset, len);
933 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
934 if (!xfs_is_falloc_aligned(ip, offset, len)) {
940 * There is no need to overlap collapse range with EOF,
941 * in which case it is effectively a truncate operation
943 if (offset + len >= i_size_read(inode)) {
948 new_size = i_size_read(inode) - len;
950 error = xfs_collapse_file_space(ip, offset, len);
953 } else if (mode & FALLOC_FL_INSERT_RANGE) {
954 loff_t isize = i_size_read(inode);
956 if (!xfs_is_falloc_aligned(ip, offset, len)) {
962 * New inode size must not exceed ->s_maxbytes, accounting for
963 * possible signed overflow.
965 if (inode->i_sb->s_maxbytes - isize < len) {
969 new_size = isize + len;
971 /* Offset should be less than i_size */
972 if (offset >= isize) {
976 do_file_insert = true;
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);
986 if (mode & FALLOC_FL_ZERO_RANGE) {
988 * Punch a hole and prealloc the range. We use a hole
989 * punch rather than unwritten extent conversion for two
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.
996 unsigned int blksize = i_blocksize(inode);
998 trace_xfs_zero_file_space(ip);
1000 error = xfs_free_file_space(ip, offset, len);
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);
1013 * If always_cow mode we can't use preallocations and
1014 * thus should not create them.
1016 if (xfs_is_always_cow_inode(ip)) {
1017 error = -EOPNOTSUPP;
1022 if (!xfs_is_always_cow_inode(ip)) {
1023 error = xfs_alloc_file_space(ip, offset, len);
1029 /* Change file size if needed */
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);
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.
1047 if (do_file_insert) {
1048 error = xfs_insert_file_space(ip, offset, len);
1053 if (xfs_file_sync_writes(file))
1054 error = xfs_log_force_inode(ip);
1057 xfs_iunlock(ip, iolock);
1068 struct xfs_inode *ip = XFS_I(file_inode(file));
1073 * Operations creating pages in page cache need protection from hole
1074 * punching and similar ops
1076 if (advice == POSIX_FADV_WILLNEED) {
1077 lockflags = XFS_IOLOCK_SHARED;
1078 xfs_ilock(ip, lockflags);
1080 ret = generic_fadvise(file, start, end, advice);
1082 xfs_iunlock(ip, lockflags);
1087 xfs_file_remap_range(
1088 struct file *file_in,
1090 struct file *file_out,
1093 unsigned int remap_flags)
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;
1104 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1107 if (!xfs_has_reflink(mp))
1110 if (xfs_is_shutdown(mp))
1113 /* Prepare and then clone file data. */
1114 ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
1116 if (ret || len == 0)
1119 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
1121 ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
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.
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;
1138 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1143 if (xfs_file_sync_writes(file_in) || xfs_file_sync_writes(file_out))
1144 xfs_log_force_inode(dest);
1146 xfs_iunlock2_remapping(src, dest);
1148 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1149 return remapped > 0 ? remapped : ret;
1154 struct inode *inode,
1157 if (xfs_is_shutdown(XFS_M(inode->i_sb)))
1159 file->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
1160 return generic_file_open(inode, file);
1165 struct inode *inode,
1168 struct xfs_inode *ip = XFS_I(inode);
1172 if (xfs_is_shutdown(ip->i_mount))
1174 error = generic_file_open(inode, file);
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.
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);
1191 struct inode *inode,
1194 return xfs_release(XFS_I(inode));
1200 struct dir_context *ctx)
1202 struct inode *inode = file_inode(file);
1203 xfs_inode_t *ip = XFS_I(inode);
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
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.
1218 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_disk_size);
1220 return xfs_readdir(NULL, ip, ctx, bufsize);
1229 struct inode *inode = file->f_mapping->host;
1231 if (xfs_is_shutdown(XFS_I(inode)->i_mount))
1236 return generic_file_llseek(file, offset, whence);
1238 offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
1241 offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
1247 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1250 #ifdef CONFIG_FS_DAX
1251 static inline vm_fault_t
1253 struct vm_fault *vmf,
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);
1264 static inline vm_fault_t
1266 struct vm_fault *vmf,
1272 return VM_FAULT_SIGBUS;
1277 * Locking for serialisation of IO during page faults. This results in a lock
1281 * sb_start_pagefault(vfs, freeze)
1282 * invalidate_lock (vfs/XFS_MMAPLOCK - truncate serialisation)
1284 * i_lock (XFS - extent map serialisation)
1287 __xfs_filemap_fault(
1288 struct vm_fault *vmf,
1292 struct inode *inode = file_inode(vmf->vma->vm_file);
1293 struct xfs_inode *ip = XFS_I(inode);
1295 unsigned int lock_mode = 0;
1297 trace_xfs_filemap_fault(ip, order, write_fault);
1300 sb_start_pagefault(inode->i_sb);
1301 file_update_time(vmf->vma->vm_file);
1304 if (IS_DAX(inode) || write_fault)
1305 lock_mode = xfs_ilock_for_write_fault(XFS_I(inode));
1307 if (IS_DAX(inode)) {
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);
1316 ret = filemap_fault(vmf);
1320 xfs_iunlock(XFS_I(inode), lock_mode);
1323 sb_end_pagefault(inode->i_sb);
1329 struct vm_fault *vmf)
1331 return (vmf->flags & FAULT_FLAG_WRITE) &&
1332 (vmf->vma->vm_flags & VM_SHARED);
1337 struct vm_fault *vmf)
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));
1346 xfs_filemap_huge_fault(
1347 struct vm_fault *vmf,
1350 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1351 return VM_FAULT_FALLBACK;
1353 /* DAX can shortcut the normal fault path on write faults! */
1354 return __xfs_filemap_fault(vmf, order,
1355 xfs_is_write_fault(vmf));
1359 xfs_filemap_page_mkwrite(
1360 struct vm_fault *vmf)
1362 return __xfs_filemap_fault(vmf, 0, true);
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.
1371 xfs_filemap_pfn_mkwrite(
1372 struct vm_fault *vmf)
1375 return __xfs_filemap_fault(vmf, 0, true);
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,
1389 struct vm_area_struct *vma)
1391 struct inode *inode = file_inode(file);
1392 struct xfs_buftarg *target = xfs_inode_buftarg(XFS_I(inode));
1395 * We don't support synchronous mappings for non-DAX files and
1396 * for DAX files if underneath dax_device is not synchronous.
1398 if (!daxdev_mapping_supported(vma, target->bt_daxdev))
1401 file_accessed(file);
1402 vma->vm_ops = &xfs_file_vm_ops;
1404 vm_flags_set(vma, VM_HUGEPAGE);
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,
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,
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,
1440 .fsync = xfs_dir_fsync,