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"
28 #include <linux/falloc.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mman.h>
31 #include <linux/fadvise.h>
33 static const struct vm_operations_struct xfs_file_vm_ops;
36 * Decide if the given file range is aligned to the size of the fundamental
37 * allocation unit for the file.
40 xfs_is_falloc_aligned(
45 struct xfs_mount *mp = ip->i_mount;
48 if (XFS_IS_REALTIME_INODE(ip)) {
49 if (!is_power_of_2(mp->m_sb.sb_rextsize)) {
53 rextbytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
54 div_u64_rem(pos, rextbytes, &mod);
57 div_u64_rem(len, rextbytes, &mod);
60 mask = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize) - 1;
62 mask = mp->m_sb.sb_blocksize - 1;
65 return !((pos | len) & mask);
69 xfs_update_prealloc_flags(
71 enum xfs_prealloc_flags flags)
76 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
81 xfs_ilock(ip, XFS_ILOCK_EXCL);
82 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
84 if (!(flags & XFS_PREALLOC_INVISIBLE)) {
85 VFS_I(ip)->i_mode &= ~S_ISUID;
86 if (VFS_I(ip)->i_mode & S_IXGRP)
87 VFS_I(ip)->i_mode &= ~S_ISGID;
88 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
91 if (flags & XFS_PREALLOC_SET)
92 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
93 if (flags & XFS_PREALLOC_CLEAR)
94 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
96 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
97 if (flags & XFS_PREALLOC_SYNC)
98 xfs_trans_set_sync(tp);
99 return xfs_trans_commit(tp);
103 * Fsync operations on directories are much simpler than on regular files,
104 * as there is no file data to flush, and thus also no need for explicit
105 * cache flush operations, and there are no non-transaction metadata updates
106 * on directories either.
115 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
117 trace_xfs_dir_fsync(ip);
118 return xfs_log_force_inode(ip);
128 struct inode *inode = file->f_mapping->host;
129 struct xfs_inode *ip = XFS_I(inode);
130 struct xfs_inode_log_item *iip = ip->i_itemp;
131 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_FORCED_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 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
155 else if (mp->m_logdev_targp != mp->m_ddev_targp)
156 xfs_blkdev_issue_flush(mp->m_ddev_targp);
159 * All metadata updates are logged, which means that we just have to
160 * flush the log up to the latest LSN that touched the inode. If we have
161 * concurrent fsync/fdatasync() calls, we need them to all block on the
162 * log force before we clear the ili_fsync_fields field. This ensures
163 * that we don't get a racing sync operation that does not wait for the
164 * metadata to hit the journal before returning. If we race with
165 * clearing the ili_fsync_fields, then all that will happen is the log
166 * force will do nothing as the lsn will already be on disk. We can't
167 * race with setting ili_fsync_fields because that is done under
168 * XFS_ILOCK_EXCL, and that can't happen because we hold the lock shared
169 * until after the ili_fsync_fields is cleared.
171 xfs_ilock(ip, XFS_ILOCK_SHARED);
172 if (xfs_ipincount(ip)) {
174 (iip->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
175 lsn = iip->ili_last_lsn;
179 error = xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
180 spin_lock(&iip->ili_lock);
181 iip->ili_fsync_fields = 0;
182 spin_unlock(&iip->ili_lock);
184 xfs_iunlock(ip, XFS_ILOCK_SHARED);
187 * If we only have a single device, and the log force about was
188 * a no-op we might have to flush the data device cache here.
189 * This can only happen for fdatasync/O_DSYNC if we were overwriting
190 * an already allocated file and thus do not have any metadata to
193 if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
194 mp->m_logdev_targp == mp->m_ddev_targp)
195 xfs_blkdev_issue_flush(mp->m_ddev_targp);
201 xfs_file_dio_aio_read(
205 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
206 size_t count = iov_iter_count(to);
209 trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
212 return 0; /* skip atime */
214 file_accessed(iocb->ki_filp);
216 if (iocb->ki_flags & IOCB_NOWAIT) {
217 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
220 xfs_ilock(ip, XFS_IOLOCK_SHARED);
222 ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
223 is_sync_kiocb(iocb));
224 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
229 static noinline ssize_t
234 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
235 size_t count = iov_iter_count(to);
238 trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
241 return 0; /* skip atime */
243 if (iocb->ki_flags & IOCB_NOWAIT) {
244 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
247 xfs_ilock(ip, XFS_IOLOCK_SHARED);
250 ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
251 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
253 file_accessed(iocb->ki_filp);
258 xfs_file_buffered_aio_read(
262 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
265 trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
267 if (iocb->ki_flags & IOCB_NOWAIT) {
268 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
271 xfs_ilock(ip, XFS_IOLOCK_SHARED);
273 ret = generic_file_read_iter(iocb, to);
274 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
284 struct inode *inode = file_inode(iocb->ki_filp);
285 struct xfs_mount *mp = XFS_I(inode)->i_mount;
288 XFS_STATS_INC(mp, xs_read_calls);
290 if (XFS_FORCED_SHUTDOWN(mp))
294 ret = xfs_file_dax_read(iocb, to);
295 else if (iocb->ki_flags & IOCB_DIRECT)
296 ret = xfs_file_dio_aio_read(iocb, to);
298 ret = xfs_file_buffered_aio_read(iocb, to);
301 XFS_STATS_ADD(mp, xs_read_bytes, ret);
306 * Common pre-write limit and setup checks.
308 * Called with the iolocked held either shared and exclusive according to
309 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
310 * if called for a direct write beyond i_size.
313 xfs_file_aio_write_checks(
315 struct iov_iter *from,
318 struct file *file = iocb->ki_filp;
319 struct inode *inode = file->f_mapping->host;
320 struct xfs_inode *ip = XFS_I(inode);
322 size_t count = iov_iter_count(from);
323 bool drained_dio = false;
327 error = generic_write_checks(iocb, from);
331 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
336 * For changing security info in file_remove_privs() we need i_rwsem
339 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
340 xfs_iunlock(ip, *iolock);
341 *iolock = XFS_IOLOCK_EXCL;
342 xfs_ilock(ip, *iolock);
346 * If the offset is beyond the size of the file, we need to zero any
347 * blocks that fall between the existing EOF and the start of this
348 * write. If zeroing is needed and we are currently holding the
349 * iolock shared, we need to update it to exclusive which implies
350 * having to redo all checks before.
352 * We need to serialise against EOF updates that occur in IO
353 * completions here. We want to make sure that nobody is changing the
354 * size while we do this check until we have placed an IO barrier (i.e.
355 * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
356 * The spinlock effectively forms a memory barrier once we have the
357 * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
358 * and hence be able to correctly determine if we need to run zeroing.
360 spin_lock(&ip->i_flags_lock);
361 isize = i_size_read(inode);
362 if (iocb->ki_pos > isize) {
363 spin_unlock(&ip->i_flags_lock);
365 if (*iolock == XFS_IOLOCK_SHARED) {
366 xfs_iunlock(ip, *iolock);
367 *iolock = XFS_IOLOCK_EXCL;
368 xfs_ilock(ip, *iolock);
369 iov_iter_reexpand(from, count);
372 * We now have an IO submission barrier in place, but
373 * AIO can do EOF updates during IO completion and hence
374 * we now need to wait for all of them to drain. Non-AIO
375 * DIO will have drained before we are given the
376 * XFS_IOLOCK_EXCL, and so for most cases this wait is a
379 inode_dio_wait(inode);
384 trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
385 error = iomap_zero_range(inode, isize, iocb->ki_pos - isize,
386 NULL, &xfs_buffered_write_iomap_ops);
390 spin_unlock(&ip->i_flags_lock);
393 * Updating the timestamps will grab the ilock again from
394 * xfs_fs_dirty_inode, so we have to call it after dropping the
395 * lock above. Eventually we should look into a way to avoid
396 * the pointless lock roundtrip.
398 return file_modified(file);
402 xfs_dio_write_end_io(
408 struct inode *inode = file_inode(iocb->ki_filp);
409 struct xfs_inode *ip = XFS_I(inode);
410 loff_t offset = iocb->ki_pos;
411 unsigned int nofs_flag;
413 trace_xfs_end_io_direct_write(ip, offset, size);
415 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
424 * Capture amount written on completion as we can't reliably account
425 * for it on submission.
427 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
430 * We can allocate memory here while doing writeback on behalf of
431 * memory reclaim. To avoid memory allocation deadlocks set the
432 * task-wide nofs context for the following operations.
434 nofs_flag = memalloc_nofs_save();
436 if (flags & IOMAP_DIO_COW) {
437 error = xfs_reflink_end_cow(ip, offset, size);
443 * Unwritten conversion updates the in-core isize after extent
444 * conversion but before updating the on-disk size. Updating isize any
445 * earlier allows a racing dio read to find unwritten extents before
446 * they are converted.
448 if (flags & IOMAP_DIO_UNWRITTEN) {
449 error = xfs_iomap_write_unwritten(ip, offset, size, true);
454 * We need to update the in-core inode size here so that we don't end up
455 * with the on-disk inode size being outside the in-core inode size. We
456 * have no other method of updating EOF for AIO, so always do it here
459 * We need to lock the test/set EOF update as we can be racing with
460 * other IO completions here to update the EOF. Failing to serialise
461 * here can result in EOF moving backwards and Bad Things Happen when
464 spin_lock(&ip->i_flags_lock);
465 if (offset + size > i_size_read(inode)) {
466 i_size_write(inode, offset + size);
467 spin_unlock(&ip->i_flags_lock);
468 error = xfs_setfilesize(ip, offset, size);
470 spin_unlock(&ip->i_flags_lock);
474 memalloc_nofs_restore(nofs_flag);
478 static const struct iomap_dio_ops xfs_dio_write_ops = {
479 .end_io = xfs_dio_write_end_io,
483 * xfs_file_dio_aio_write - handle direct IO writes
485 * Lock the inode appropriately to prepare for and issue a direct IO write.
486 * By separating it from the buffered write path we remove all the tricky to
487 * follow locking changes and looping.
489 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
490 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
491 * pages are flushed out.
493 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
494 * allowing them to be done in parallel with reads and other direct IO writes.
495 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
496 * needs to do sub-block zeroing and that requires serialisation against other
497 * direct IOs to the same block. In this case we need to serialise the
498 * submission of the unaligned IOs so that we don't get racing block zeroing in
499 * the dio layer. To avoid the problem with aio, we also need to wait for
500 * outstanding IOs to complete so that unwritten extent conversion is completed
501 * before we try to map the overlapping block. This is currently implemented by
502 * hitting it with a big hammer (i.e. inode_dio_wait()).
504 * Returns with locks held indicated by @iolock and errors indicated by
505 * negative return values.
508 xfs_file_dio_aio_write(
510 struct iov_iter *from)
512 struct file *file = iocb->ki_filp;
513 struct address_space *mapping = file->f_mapping;
514 struct inode *inode = mapping->host;
515 struct xfs_inode *ip = XFS_I(inode);
516 struct xfs_mount *mp = ip->i_mount;
518 int unaligned_io = 0;
520 size_t count = iov_iter_count(from);
521 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
523 /* DIO must be aligned to device logical sector size */
524 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
528 * Don't take the exclusive iolock here unless the I/O is unaligned to
529 * the file system block size. We don't need to consider the EOF
530 * extension case here because xfs_file_aio_write_checks() will relock
531 * the inode as necessary for EOF zeroing cases and fill out the new
532 * inode size as appropriate.
534 if ((iocb->ki_pos & mp->m_blockmask) ||
535 ((iocb->ki_pos + count) & mp->m_blockmask)) {
539 * We can't properly handle unaligned direct I/O to reflink
540 * files yet, as we can't unshare a partial block.
542 if (xfs_is_cow_inode(ip)) {
543 trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
546 iolock = XFS_IOLOCK_EXCL;
548 iolock = XFS_IOLOCK_SHARED;
551 if (iocb->ki_flags & IOCB_NOWAIT) {
552 /* unaligned dio always waits, bail */
555 if (!xfs_ilock_nowait(ip, iolock))
558 xfs_ilock(ip, iolock);
561 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
564 count = iov_iter_count(from);
567 * If we are doing unaligned IO, we can't allow any other overlapping IO
568 * in-flight at the same time or we risk data corruption. Wait for all
569 * other IO to drain before we submit. If the IO is aligned, demote the
570 * iolock if we had to take the exclusive lock in
571 * xfs_file_aio_write_checks() for other reasons.
574 inode_dio_wait(inode);
575 } else if (iolock == XFS_IOLOCK_EXCL) {
576 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
577 iolock = XFS_IOLOCK_SHARED;
580 trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
582 * If unaligned, this is the only IO in-flight. Wait on it before we
583 * release the iolock to prevent subsequent overlapping IO.
585 ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
587 is_sync_kiocb(iocb) || unaligned_io);
589 xfs_iunlock(ip, iolock);
592 * No fallback to buffered IO after short writes for XFS, direct I/O
593 * will either complete fully or return an error.
595 ASSERT(ret < 0 || ret == count);
599 static noinline ssize_t
602 struct iov_iter *from)
604 struct inode *inode = iocb->ki_filp->f_mapping->host;
605 struct xfs_inode *ip = XFS_I(inode);
606 int iolock = XFS_IOLOCK_EXCL;
607 ssize_t ret, error = 0;
611 if (iocb->ki_flags & IOCB_NOWAIT) {
612 if (!xfs_ilock_nowait(ip, iolock))
615 xfs_ilock(ip, iolock);
618 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
623 count = iov_iter_count(from);
625 trace_xfs_file_dax_write(ip, count, pos);
626 ret = dax_iomap_rw(iocb, from, &xfs_direct_write_iomap_ops);
627 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
628 i_size_write(inode, iocb->ki_pos);
629 error = xfs_setfilesize(ip, pos, ret);
632 xfs_iunlock(ip, iolock);
637 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
639 /* Handle various SYNC-type writes */
640 ret = generic_write_sync(iocb, ret);
646 xfs_file_buffered_aio_write(
648 struct iov_iter *from)
650 struct file *file = iocb->ki_filp;
651 struct address_space *mapping = file->f_mapping;
652 struct inode *inode = mapping->host;
653 struct xfs_inode *ip = XFS_I(inode);
658 if (iocb->ki_flags & IOCB_NOWAIT)
662 iolock = XFS_IOLOCK_EXCL;
663 xfs_ilock(ip, iolock);
665 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
669 /* We can write back this queue in page reclaim */
670 current->backing_dev_info = inode_to_bdi(inode);
672 trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
673 ret = iomap_file_buffered_write(iocb, from,
674 &xfs_buffered_write_iomap_ops);
675 if (likely(ret >= 0))
679 * If we hit a space limit, try to free up some lingering preallocated
680 * space before returning an error. In the case of ENOSPC, first try to
681 * write back all dirty inodes to free up some of the excess reserved
682 * metadata space. This reduces the chances that the eofblocks scan
683 * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
684 * also behaves as a filter to prevent too many eofblocks scans from
685 * running at the same time.
687 if (ret == -EDQUOT && !enospc) {
688 xfs_iunlock(ip, iolock);
689 enospc = xfs_inode_free_quota_eofblocks(ip);
692 enospc = xfs_inode_free_quota_cowblocks(ip);
696 } else if (ret == -ENOSPC && !enospc) {
697 struct xfs_eofblocks eofb = {0};
700 xfs_flush_inodes(ip->i_mount);
702 xfs_iunlock(ip, iolock);
703 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
704 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
705 xfs_icache_free_cowblocks(ip->i_mount, &eofb);
709 current->backing_dev_info = NULL;
712 xfs_iunlock(ip, iolock);
715 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
716 /* Handle various SYNC-type writes */
717 ret = generic_write_sync(iocb, ret);
725 struct iov_iter *from)
727 struct file *file = iocb->ki_filp;
728 struct address_space *mapping = file->f_mapping;
729 struct inode *inode = mapping->host;
730 struct xfs_inode *ip = XFS_I(inode);
732 size_t ocount = iov_iter_count(from);
734 XFS_STATS_INC(ip->i_mount, xs_write_calls);
739 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
743 return xfs_file_dax_write(iocb, from);
745 if (iocb->ki_flags & IOCB_DIRECT) {
747 * Allow a directio write to fall back to a buffered
748 * write *only* in the case that we're doing a reflink
749 * CoW. In all other directio scenarios we do not
750 * allow an operation to fall back to buffered mode.
752 ret = xfs_file_dio_aio_write(iocb, from);
757 return xfs_file_buffered_aio_write(iocb, from);
764 struct xfs_inode *ip = XFS_I(inode);
766 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
768 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
772 xfs_break_dax_layouts(
778 ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
780 page = dax_layout_busy_page(inode->i_mapping);
785 return ___wait_var_event(&page->_refcount,
786 atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
787 0, 0, xfs_wait_dax_page(inode));
794 enum layout_break_reason reason)
799 ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
805 error = xfs_break_dax_layouts(inode, &retry);
810 error = xfs_break_leased_layouts(inode, iolock, &retry);
816 } while (error == 0 && retry);
821 #define XFS_FALLOC_FL_SUPPORTED \
822 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
823 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
824 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
833 struct inode *inode = file_inode(file);
834 struct xfs_inode *ip = XFS_I(inode);
836 enum xfs_prealloc_flags flags = 0;
837 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
839 bool do_file_insert = false;
841 if (!S_ISREG(inode->i_mode))
843 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
846 xfs_ilock(ip, iolock);
847 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
852 * Must wait for all AIO to complete before we continue as AIO can
853 * change the file size on completion without holding any locks we
854 * currently hold. We must do this first because AIO can update both
855 * the on disk and in memory inode sizes, and the operations that follow
856 * require the in-memory size to be fully up-to-date.
858 inode_dio_wait(inode);
861 * Now AIO and DIO has drained we flush and (if necessary) invalidate
862 * the cached range over the first operation we are about to run.
864 * We care about zero and collapse here because they both run a hole
865 * punch over the range first. Because that can zero data, and the range
866 * of invalidation for the shift operations is much larger, we still do
867 * the required flush for collapse in xfs_prepare_shift().
869 * Insert has the same range requirements as collapse, and we extend the
870 * file first which can zero data. Hence insert has the same
871 * flush/invalidate requirements as collapse and so they are both
872 * handled at the right time by xfs_prepare_shift().
874 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE |
875 FALLOC_FL_COLLAPSE_RANGE)) {
876 error = xfs_flush_unmap_range(ip, offset, len);
881 if (mode & FALLOC_FL_PUNCH_HOLE) {
882 error = xfs_free_file_space(ip, offset, len);
885 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
886 if (!xfs_is_falloc_aligned(ip, offset, len)) {
892 * There is no need to overlap collapse range with EOF,
893 * in which case it is effectively a truncate operation
895 if (offset + len >= i_size_read(inode)) {
900 new_size = i_size_read(inode) - len;
902 error = xfs_collapse_file_space(ip, offset, len);
905 } else if (mode & FALLOC_FL_INSERT_RANGE) {
906 loff_t isize = i_size_read(inode);
908 if (!xfs_is_falloc_aligned(ip, offset, len)) {
914 * New inode size must not exceed ->s_maxbytes, accounting for
915 * possible signed overflow.
917 if (inode->i_sb->s_maxbytes - isize < len) {
921 new_size = isize + len;
923 /* Offset should be less than i_size */
924 if (offset >= isize) {
928 do_file_insert = true;
930 flags |= XFS_PREALLOC_SET;
932 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
933 offset + len > i_size_read(inode)) {
934 new_size = offset + len;
935 error = inode_newsize_ok(inode, new_size);
940 if (mode & FALLOC_FL_ZERO_RANGE) {
942 * Punch a hole and prealloc the range. We use a hole
943 * punch rather than unwritten extent conversion for two
946 * 1.) Hole punch handles partial block zeroing for us.
947 * 2.) If prealloc returns ENOSPC, the file range is
948 * still zero-valued by virtue of the hole punch.
950 unsigned int blksize = i_blocksize(inode);
952 trace_xfs_zero_file_space(ip);
954 error = xfs_free_file_space(ip, offset, len);
958 len = round_up(offset + len, blksize) -
959 round_down(offset, blksize);
960 offset = round_down(offset, blksize);
961 } else if (mode & FALLOC_FL_UNSHARE_RANGE) {
962 error = xfs_reflink_unshare(ip, offset, len);
967 * If always_cow mode we can't use preallocations and
968 * thus should not create them.
970 if (xfs_is_always_cow_inode(ip)) {
976 if (!xfs_is_always_cow_inode(ip)) {
977 error = xfs_alloc_file_space(ip, offset, len,
984 if (file->f_flags & O_DSYNC)
985 flags |= XFS_PREALLOC_SYNC;
987 error = xfs_update_prealloc_flags(ip, flags);
991 /* Change file size if needed */
995 iattr.ia_valid = ATTR_SIZE;
996 iattr.ia_size = new_size;
997 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
1003 * Perform hole insertion now that the file size has been
1004 * updated so that if we crash during the operation we don't
1005 * leave shifted extents past EOF and hence losing access to
1006 * the data that is contained within them.
1009 error = xfs_insert_file_space(ip, offset, len);
1012 xfs_iunlock(ip, iolock);
1023 struct xfs_inode *ip = XFS_I(file_inode(file));
1028 * Operations creating pages in page cache need protection from hole
1029 * punching and similar ops
1031 if (advice == POSIX_FADV_WILLNEED) {
1032 lockflags = XFS_IOLOCK_SHARED;
1033 xfs_ilock(ip, lockflags);
1035 ret = generic_fadvise(file, start, end, advice);
1037 xfs_iunlock(ip, lockflags);
1041 /* Does this file, inode, or mount want synchronous writes? */
1042 static inline bool xfs_file_sync_writes(struct file *filp)
1044 struct xfs_inode *ip = XFS_I(file_inode(filp));
1046 if (ip->i_mount->m_flags & XFS_MOUNT_WSYNC)
1048 if (filp->f_flags & (__O_SYNC | O_DSYNC))
1050 if (IS_SYNC(file_inode(filp)))
1057 xfs_file_remap_range(
1058 struct file *file_in,
1060 struct file *file_out,
1063 unsigned int remap_flags)
1065 struct inode *inode_in = file_inode(file_in);
1066 struct xfs_inode *src = XFS_I(inode_in);
1067 struct inode *inode_out = file_inode(file_out);
1068 struct xfs_inode *dest = XFS_I(inode_out);
1069 struct xfs_mount *mp = src->i_mount;
1070 loff_t remapped = 0;
1071 xfs_extlen_t cowextsize;
1074 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1077 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1080 if (XFS_FORCED_SHUTDOWN(mp))
1083 /* Prepare and then clone file data. */
1084 ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
1086 if (ret || len == 0)
1089 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
1091 ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
1097 * Carry the cowextsize hint from src to dest if we're sharing the
1098 * entire source file to the entire destination file, the source file
1099 * has a cowextsize hint, and the destination file does not.
1102 if (pos_in == 0 && len == i_size_read(inode_in) &&
1103 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1104 pos_out == 0 && len >= i_size_read(inode_out) &&
1105 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1106 cowextsize = src->i_d.di_cowextsize;
1108 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1113 if (xfs_file_sync_writes(file_in) || xfs_file_sync_writes(file_out))
1114 xfs_log_force_inode(dest);
1116 xfs_iunlock2_io_mmap(src, dest);
1118 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1119 return remapped > 0 ? remapped : ret;
1124 struct inode *inode,
1127 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1129 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1131 file->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
1137 struct inode *inode,
1140 struct xfs_inode *ip = XFS_I(inode);
1144 error = xfs_file_open(inode, file);
1149 * If there are any blocks, read-ahead block 0 as we're almost
1150 * certain to have the next operation be a read there.
1152 mode = xfs_ilock_data_map_shared(ip);
1153 if (ip->i_df.if_nextents > 0)
1154 error = xfs_dir3_data_readahead(ip, 0, 0);
1155 xfs_iunlock(ip, mode);
1161 struct inode *inode,
1164 return xfs_release(XFS_I(inode));
1170 struct dir_context *ctx)
1172 struct inode *inode = file_inode(file);
1173 xfs_inode_t *ip = XFS_I(inode);
1177 * The Linux API doesn't pass down the total size of the buffer
1178 * we read into down to the filesystem. With the filldir concept
1179 * it's not needed for correct information, but the XFS dir2 leaf
1180 * code wants an estimate of the buffer size to calculate it's
1181 * readahead window and size the buffers used for mapping to
1184 * Try to give it an estimate that's good enough, maybe at some
1185 * point we can change the ->readdir prototype to include the
1186 * buffer size. For now we use the current glibc buffer size.
1188 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
1190 return xfs_readdir(NULL, ip, ctx, bufsize);
1199 struct inode *inode = file->f_mapping->host;
1201 if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
1206 return generic_file_llseek(file, offset, whence);
1208 offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
1211 offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
1217 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1221 * Locking for serialisation of IO during page faults. This results in a lock
1225 * sb_start_pagefault(vfs, freeze)
1226 * i_mmaplock (XFS - truncate serialisation)
1228 * i_lock (XFS - extent map serialisation)
1231 __xfs_filemap_fault(
1232 struct vm_fault *vmf,
1233 enum page_entry_size pe_size,
1236 struct inode *inode = file_inode(vmf->vma->vm_file);
1237 struct xfs_inode *ip = XFS_I(inode);
1240 trace_xfs_filemap_fault(ip, pe_size, write_fault);
1243 sb_start_pagefault(inode->i_sb);
1244 file_update_time(vmf->vma->vm_file);
1247 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1248 if (IS_DAX(inode)) {
1251 ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL,
1252 (write_fault && !vmf->cow_page) ?
1253 &xfs_direct_write_iomap_ops :
1254 &xfs_read_iomap_ops);
1255 if (ret & VM_FAULT_NEEDDSYNC)
1256 ret = dax_finish_sync_fault(vmf, pe_size, pfn);
1259 ret = iomap_page_mkwrite(vmf,
1260 &xfs_buffered_write_iomap_ops);
1262 ret = filemap_fault(vmf);
1264 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1267 sb_end_pagefault(inode->i_sb);
1273 struct vm_fault *vmf)
1275 return (vmf->flags & FAULT_FLAG_WRITE) &&
1276 (vmf->vma->vm_flags & VM_SHARED);
1281 struct vm_fault *vmf)
1283 /* DAX can shortcut the normal fault path on write faults! */
1284 return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
1285 IS_DAX(file_inode(vmf->vma->vm_file)) &&
1286 xfs_is_write_fault(vmf));
1290 xfs_filemap_huge_fault(
1291 struct vm_fault *vmf,
1292 enum page_entry_size pe_size)
1294 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1295 return VM_FAULT_FALLBACK;
1297 /* DAX can shortcut the normal fault path on write faults! */
1298 return __xfs_filemap_fault(vmf, pe_size,
1299 xfs_is_write_fault(vmf));
1303 xfs_filemap_page_mkwrite(
1304 struct vm_fault *vmf)
1306 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1310 * pfn_mkwrite was originally intended to ensure we capture time stamp updates
1311 * on write faults. In reality, it needs to serialise against truncate and
1312 * prepare memory for writing so handle is as standard write fault.
1315 xfs_filemap_pfn_mkwrite(
1316 struct vm_fault *vmf)
1319 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1323 xfs_filemap_map_pages(
1324 struct vm_fault *vmf,
1325 pgoff_t start_pgoff,
1328 struct inode *inode = file_inode(vmf->vma->vm_file);
1330 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1331 filemap_map_pages(vmf, start_pgoff, end_pgoff);
1332 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1335 static const struct vm_operations_struct xfs_file_vm_ops = {
1336 .fault = xfs_filemap_fault,
1337 .huge_fault = xfs_filemap_huge_fault,
1338 .map_pages = xfs_filemap_map_pages,
1339 .page_mkwrite = xfs_filemap_page_mkwrite,
1340 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
1346 struct vm_area_struct *vma)
1348 struct inode *inode = file_inode(file);
1349 struct xfs_buftarg *target = xfs_inode_buftarg(XFS_I(inode));
1352 * We don't support synchronous mappings for non-DAX files and
1353 * for DAX files if underneath dax_device is not synchronous.
1355 if (!daxdev_mapping_supported(vma, target->bt_daxdev))
1358 file_accessed(file);
1359 vma->vm_ops = &xfs_file_vm_ops;
1361 vma->vm_flags |= VM_HUGEPAGE;
1365 const struct file_operations xfs_file_operations = {
1366 .llseek = xfs_file_llseek,
1367 .read_iter = xfs_file_read_iter,
1368 .write_iter = xfs_file_write_iter,
1369 .splice_read = generic_file_splice_read,
1370 .splice_write = iter_file_splice_write,
1371 .iopoll = iomap_dio_iopoll,
1372 .unlocked_ioctl = xfs_file_ioctl,
1373 #ifdef CONFIG_COMPAT
1374 .compat_ioctl = xfs_file_compat_ioctl,
1376 .mmap = xfs_file_mmap,
1377 .mmap_supported_flags = MAP_SYNC,
1378 .open = xfs_file_open,
1379 .release = xfs_file_release,
1380 .fsync = xfs_file_fsync,
1381 .get_unmapped_area = thp_get_unmapped_area,
1382 .fallocate = xfs_file_fallocate,
1383 .fadvise = xfs_file_fadvise,
1384 .remap_file_range = xfs_file_remap_range,
1387 const struct file_operations xfs_dir_file_operations = {
1388 .open = xfs_dir_open,
1389 .read = generic_read_dir,
1390 .iterate_shared = xfs_file_readdir,
1391 .llseek = generic_file_llseek,
1392 .unlocked_ioctl = xfs_file_ioctl,
1393 #ifdef CONFIG_COMPAT
1394 .compat_ioctl = xfs_file_compat_ioctl,
1396 .fsync = xfs_dir_fsync,