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 return xfs_trans_commit(tp);
101 * Fsync operations on directories are much simpler than on regular files,
102 * as there is no file data to flush, and thus also no need for explicit
103 * cache flush operations, and there are no non-transaction metadata updates
104 * on directories either.
113 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
115 trace_xfs_dir_fsync(ip);
116 return xfs_log_force_inode(ip);
121 struct xfs_inode *ip,
124 if (!xfs_ipincount(ip))
126 if (datasync && !(ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
128 return ip->i_itemp->ili_commit_seq;
132 * All metadata updates are logged, which means that we just have to flush the
133 * log up to the latest LSN that touched the inode.
135 * If we have concurrent fsync/fdatasync() calls, we need them to all block on
136 * the log force before we clear the ili_fsync_fields field. This ensures that
137 * we don't get a racing sync operation that does not wait for the metadata to
138 * hit the journal before returning. If we race with clearing ili_fsync_fields,
139 * then all that will happen is the log force will do nothing as the lsn will
140 * already be on disk. We can't race with setting ili_fsync_fields because that
141 * is done under XFS_ILOCK_EXCL, and that can't happen because we hold the lock
142 * shared until after the ili_fsync_fields is cleared.
146 struct xfs_inode *ip,
153 xfs_ilock(ip, XFS_ILOCK_SHARED);
154 seq = xfs_fsync_seq(ip, datasync);
156 error = xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC,
159 spin_lock(&ip->i_itemp->ili_lock);
160 ip->i_itemp->ili_fsync_fields = 0;
161 spin_unlock(&ip->i_itemp->ili_lock);
163 xfs_iunlock(ip, XFS_ILOCK_SHARED);
174 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
175 struct xfs_mount *mp = ip->i_mount;
179 trace_xfs_file_fsync(ip);
181 error = file_write_and_wait_range(file, start, end);
185 if (XFS_FORCED_SHUTDOWN(mp))
188 xfs_iflags_clear(ip, XFS_ITRUNCATED);
191 * If we have an RT and/or log subvolume we need to make sure to flush
192 * the write cache the device used for file data first. This is to
193 * ensure newly written file data make it to disk before logging the new
194 * inode size in case of an extending write.
196 if (XFS_IS_REALTIME_INODE(ip))
197 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
198 else if (mp->m_logdev_targp != mp->m_ddev_targp)
199 xfs_blkdev_issue_flush(mp->m_ddev_targp);
201 error = xfs_fsync_flush_log(ip, datasync, &log_flushed);
204 * If we only have a single device, and the log force about was
205 * a no-op we might have to flush the data device cache here.
206 * This can only happen for fdatasync/O_DSYNC if we were overwriting
207 * an already allocated file and thus do not have any metadata to
210 if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
211 mp->m_logdev_targp == mp->m_ddev_targp)
212 xfs_blkdev_issue_flush(mp->m_ddev_targp);
218 xfs_file_dio_aio_read(
222 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
223 size_t count = iov_iter_count(to);
226 trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
229 return 0; /* skip atime */
231 file_accessed(iocb->ki_filp);
233 if (iocb->ki_flags & IOCB_NOWAIT) {
234 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
237 xfs_ilock(ip, XFS_IOLOCK_SHARED);
239 ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
240 is_sync_kiocb(iocb));
241 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
246 static noinline ssize_t
251 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
252 size_t count = iov_iter_count(to);
255 trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
258 return 0; /* skip atime */
260 if (iocb->ki_flags & IOCB_NOWAIT) {
261 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
264 xfs_ilock(ip, XFS_IOLOCK_SHARED);
267 ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
268 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
270 file_accessed(iocb->ki_filp);
275 xfs_file_buffered_aio_read(
279 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
282 trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
284 if (iocb->ki_flags & IOCB_NOWAIT) {
285 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
288 xfs_ilock(ip, XFS_IOLOCK_SHARED);
290 ret = generic_file_read_iter(iocb, to);
291 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
301 struct inode *inode = file_inode(iocb->ki_filp);
302 struct xfs_mount *mp = XFS_I(inode)->i_mount;
305 XFS_STATS_INC(mp, xs_read_calls);
307 if (XFS_FORCED_SHUTDOWN(mp))
311 ret = xfs_file_dax_read(iocb, to);
312 else if (iocb->ki_flags & IOCB_DIRECT)
313 ret = xfs_file_dio_aio_read(iocb, to);
315 ret = xfs_file_buffered_aio_read(iocb, to);
318 XFS_STATS_ADD(mp, xs_read_bytes, ret);
323 * Common pre-write limit and setup checks.
325 * Called with the iolocked held either shared and exclusive according to
326 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
327 * if called for a direct write beyond i_size.
330 xfs_file_aio_write_checks(
332 struct iov_iter *from,
335 struct file *file = iocb->ki_filp;
336 struct inode *inode = file->f_mapping->host;
337 struct xfs_inode *ip = XFS_I(inode);
339 size_t count = iov_iter_count(from);
340 bool drained_dio = false;
344 error = generic_write_checks(iocb, from);
348 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
353 * For changing security info in file_remove_privs() we need i_rwsem
356 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
357 xfs_iunlock(ip, *iolock);
358 *iolock = XFS_IOLOCK_EXCL;
359 xfs_ilock(ip, *iolock);
363 * If the offset is beyond the size of the file, we need to zero any
364 * blocks that fall between the existing EOF and the start of this
365 * write. If zeroing is needed and we are currently holding the
366 * iolock shared, we need to update it to exclusive which implies
367 * having to redo all checks before.
369 * We need to serialise against EOF updates that occur in IO
370 * completions here. We want to make sure that nobody is changing the
371 * size while we do this check until we have placed an IO barrier (i.e.
372 * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
373 * The spinlock effectively forms a memory barrier once we have the
374 * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
375 * and hence be able to correctly determine if we need to run zeroing.
377 spin_lock(&ip->i_flags_lock);
378 isize = i_size_read(inode);
379 if (iocb->ki_pos > isize) {
380 spin_unlock(&ip->i_flags_lock);
382 if (*iolock == XFS_IOLOCK_SHARED) {
383 xfs_iunlock(ip, *iolock);
384 *iolock = XFS_IOLOCK_EXCL;
385 xfs_ilock(ip, *iolock);
386 iov_iter_reexpand(from, count);
389 * We now have an IO submission barrier in place, but
390 * AIO can do EOF updates during IO completion and hence
391 * we now need to wait for all of them to drain. Non-AIO
392 * DIO will have drained before we are given the
393 * XFS_IOLOCK_EXCL, and so for most cases this wait is a
396 inode_dio_wait(inode);
401 trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
402 error = iomap_zero_range(inode, isize, iocb->ki_pos - isize,
403 NULL, &xfs_buffered_write_iomap_ops);
407 spin_unlock(&ip->i_flags_lock);
410 * Updating the timestamps will grab the ilock again from
411 * xfs_fs_dirty_inode, so we have to call it after dropping the
412 * lock above. Eventually we should look into a way to avoid
413 * the pointless lock roundtrip.
415 return file_modified(file);
419 xfs_dio_write_end_io(
425 struct inode *inode = file_inode(iocb->ki_filp);
426 struct xfs_inode *ip = XFS_I(inode);
427 loff_t offset = iocb->ki_pos;
428 unsigned int nofs_flag;
430 trace_xfs_end_io_direct_write(ip, offset, size);
432 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
441 * Capture amount written on completion as we can't reliably account
442 * for it on submission.
444 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
447 * We can allocate memory here while doing writeback on behalf of
448 * memory reclaim. To avoid memory allocation deadlocks set the
449 * task-wide nofs context for the following operations.
451 nofs_flag = memalloc_nofs_save();
453 if (flags & IOMAP_DIO_COW) {
454 error = xfs_reflink_end_cow(ip, offset, size);
460 * Unwritten conversion updates the in-core isize after extent
461 * conversion but before updating the on-disk size. Updating isize any
462 * earlier allows a racing dio read to find unwritten extents before
463 * they are converted.
465 if (flags & IOMAP_DIO_UNWRITTEN) {
466 error = xfs_iomap_write_unwritten(ip, offset, size, true);
471 * We need to update the in-core inode size here so that we don't end up
472 * with the on-disk inode size being outside the in-core inode size. We
473 * have no other method of updating EOF for AIO, so always do it here
476 * We need to lock the test/set EOF update as we can be racing with
477 * other IO completions here to update the EOF. Failing to serialise
478 * here can result in EOF moving backwards and Bad Things Happen when
481 spin_lock(&ip->i_flags_lock);
482 if (offset + size > i_size_read(inode)) {
483 i_size_write(inode, offset + size);
484 spin_unlock(&ip->i_flags_lock);
485 error = xfs_setfilesize(ip, offset, size);
487 spin_unlock(&ip->i_flags_lock);
491 memalloc_nofs_restore(nofs_flag);
495 static const struct iomap_dio_ops xfs_dio_write_ops = {
496 .end_io = xfs_dio_write_end_io,
500 * xfs_file_dio_aio_write - handle direct IO writes
502 * Lock the inode appropriately to prepare for and issue a direct IO write.
503 * By separating it from the buffered write path we remove all the tricky to
504 * follow locking changes and looping.
506 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
507 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
508 * pages are flushed out.
510 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
511 * allowing them to be done in parallel with reads and other direct IO writes.
512 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
513 * needs to do sub-block zeroing and that requires serialisation against other
514 * direct IOs to the same block. In this case we need to serialise the
515 * submission of the unaligned IOs so that we don't get racing block zeroing in
516 * the dio layer. To avoid the problem with aio, we also need to wait for
517 * outstanding IOs to complete so that unwritten extent conversion is completed
518 * before we try to map the overlapping block. This is currently implemented by
519 * hitting it with a big hammer (i.e. inode_dio_wait()).
521 * Returns with locks held indicated by @iolock and errors indicated by
522 * negative return values.
525 xfs_file_dio_aio_write(
527 struct iov_iter *from)
529 struct file *file = iocb->ki_filp;
530 struct address_space *mapping = file->f_mapping;
531 struct inode *inode = mapping->host;
532 struct xfs_inode *ip = XFS_I(inode);
533 struct xfs_mount *mp = ip->i_mount;
535 int unaligned_io = 0;
537 size_t count = iov_iter_count(from);
538 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
540 /* DIO must be aligned to device logical sector size */
541 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
545 * Don't take the exclusive iolock here unless the I/O is unaligned to
546 * the file system block size. We don't need to consider the EOF
547 * extension case here because xfs_file_aio_write_checks() will relock
548 * the inode as necessary for EOF zeroing cases and fill out the new
549 * inode size as appropriate.
551 if ((iocb->ki_pos & mp->m_blockmask) ||
552 ((iocb->ki_pos + count) & mp->m_blockmask)) {
556 * We can't properly handle unaligned direct I/O to reflink
557 * files yet, as we can't unshare a partial block.
559 if (xfs_is_cow_inode(ip)) {
560 trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
563 iolock = XFS_IOLOCK_EXCL;
565 iolock = XFS_IOLOCK_SHARED;
568 if (iocb->ki_flags & IOCB_NOWAIT) {
569 /* unaligned dio always waits, bail */
572 if (!xfs_ilock_nowait(ip, iolock))
575 xfs_ilock(ip, iolock);
578 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
581 count = iov_iter_count(from);
584 * If we are doing unaligned IO, we can't allow any other overlapping IO
585 * in-flight at the same time or we risk data corruption. Wait for all
586 * other IO to drain before we submit. If the IO is aligned, demote the
587 * iolock if we had to take the exclusive lock in
588 * xfs_file_aio_write_checks() for other reasons.
591 inode_dio_wait(inode);
592 } else if (iolock == XFS_IOLOCK_EXCL) {
593 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
594 iolock = XFS_IOLOCK_SHARED;
597 trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
599 * If unaligned, this is the only IO in-flight. Wait on it before we
600 * release the iolock to prevent subsequent overlapping IO.
602 ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
604 is_sync_kiocb(iocb) || unaligned_io);
606 xfs_iunlock(ip, iolock);
609 * No fallback to buffered IO after short writes for XFS, direct I/O
610 * will either complete fully or return an error.
612 ASSERT(ret < 0 || ret == count);
616 static noinline ssize_t
619 struct iov_iter *from)
621 struct inode *inode = iocb->ki_filp->f_mapping->host;
622 struct xfs_inode *ip = XFS_I(inode);
623 int iolock = XFS_IOLOCK_EXCL;
624 ssize_t ret, error = 0;
628 if (iocb->ki_flags & IOCB_NOWAIT) {
629 if (!xfs_ilock_nowait(ip, iolock))
632 xfs_ilock(ip, iolock);
635 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
640 count = iov_iter_count(from);
642 trace_xfs_file_dax_write(ip, count, pos);
643 ret = dax_iomap_rw(iocb, from, &xfs_direct_write_iomap_ops);
644 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
645 i_size_write(inode, iocb->ki_pos);
646 error = xfs_setfilesize(ip, pos, ret);
649 xfs_iunlock(ip, iolock);
654 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
656 /* Handle various SYNC-type writes */
657 ret = generic_write_sync(iocb, ret);
663 xfs_file_buffered_aio_write(
665 struct iov_iter *from)
667 struct file *file = iocb->ki_filp;
668 struct address_space *mapping = file->f_mapping;
669 struct inode *inode = mapping->host;
670 struct xfs_inode *ip = XFS_I(inode);
675 if (iocb->ki_flags & IOCB_NOWAIT)
679 iolock = XFS_IOLOCK_EXCL;
680 xfs_ilock(ip, iolock);
682 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
686 /* We can write back this queue in page reclaim */
687 current->backing_dev_info = inode_to_bdi(inode);
689 trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
690 ret = iomap_file_buffered_write(iocb, from,
691 &xfs_buffered_write_iomap_ops);
692 if (likely(ret >= 0))
696 * If we hit a space limit, try to free up some lingering preallocated
697 * space before returning an error. In the case of ENOSPC, first try to
698 * write back all dirty inodes to free up some of the excess reserved
699 * metadata space. This reduces the chances that the eofblocks scan
700 * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
701 * also behaves as a filter to prevent too many eofblocks scans from
702 * running at the same time.
704 if (ret == -EDQUOT && !enospc) {
705 xfs_iunlock(ip, iolock);
706 enospc = xfs_inode_free_quota_eofblocks(ip);
709 enospc = xfs_inode_free_quota_cowblocks(ip);
713 } else if (ret == -ENOSPC && !enospc) {
714 struct xfs_eofblocks eofb = {0};
717 xfs_flush_inodes(ip->i_mount);
719 xfs_iunlock(ip, iolock);
720 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
721 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
722 xfs_icache_free_cowblocks(ip->i_mount, &eofb);
726 current->backing_dev_info = NULL;
729 xfs_iunlock(ip, iolock);
732 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
733 /* Handle various SYNC-type writes */
734 ret = generic_write_sync(iocb, ret);
742 struct iov_iter *from)
744 struct file *file = iocb->ki_filp;
745 struct address_space *mapping = file->f_mapping;
746 struct inode *inode = mapping->host;
747 struct xfs_inode *ip = XFS_I(inode);
749 size_t ocount = iov_iter_count(from);
751 XFS_STATS_INC(ip->i_mount, xs_write_calls);
756 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
760 return xfs_file_dax_write(iocb, from);
762 if (iocb->ki_flags & IOCB_DIRECT) {
764 * Allow a directio write to fall back to a buffered
765 * write *only* in the case that we're doing a reflink
766 * CoW. In all other directio scenarios we do not
767 * allow an operation to fall back to buffered mode.
769 ret = xfs_file_dio_aio_write(iocb, from);
774 return xfs_file_buffered_aio_write(iocb, from);
781 struct xfs_inode *ip = XFS_I(inode);
783 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
785 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
789 xfs_break_dax_layouts(
795 ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
797 page = dax_layout_busy_page(inode->i_mapping);
802 return ___wait_var_event(&page->_refcount,
803 atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
804 0, 0, xfs_wait_dax_page(inode));
811 enum layout_break_reason reason)
816 ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
822 error = xfs_break_dax_layouts(inode, &retry);
827 error = xfs_break_leased_layouts(inode, iolock, &retry);
833 } while (error == 0 && retry);
838 #define XFS_FALLOC_FL_SUPPORTED \
839 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
840 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
841 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
850 struct inode *inode = file_inode(file);
851 struct xfs_inode *ip = XFS_I(inode);
853 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
855 bool do_file_insert = false;
857 if (!S_ISREG(inode->i_mode))
859 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
862 xfs_ilock(ip, iolock);
863 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
868 * Must wait for all AIO to complete before we continue as AIO can
869 * change the file size on completion without holding any locks we
870 * currently hold. We must do this first because AIO can update both
871 * the on disk and in memory inode sizes, and the operations that follow
872 * require the in-memory size to be fully up-to-date.
874 inode_dio_wait(inode);
877 * Now AIO and DIO has drained we flush and (if necessary) invalidate
878 * the cached range over the first operation we are about to run.
880 * We care about zero and collapse here because they both run a hole
881 * punch over the range first. Because that can zero data, and the range
882 * of invalidation for the shift operations is much larger, we still do
883 * the required flush for collapse in xfs_prepare_shift().
885 * Insert has the same range requirements as collapse, and we extend the
886 * file first which can zero data. Hence insert has the same
887 * flush/invalidate requirements as collapse and so they are both
888 * handled at the right time by xfs_prepare_shift().
890 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE |
891 FALLOC_FL_COLLAPSE_RANGE)) {
892 error = xfs_flush_unmap_range(ip, offset, len);
897 error = file_modified(file);
901 if (mode & FALLOC_FL_PUNCH_HOLE) {
902 error = xfs_free_file_space(ip, offset, len);
905 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
906 if (!xfs_is_falloc_aligned(ip, offset, len)) {
912 * There is no need to overlap collapse range with EOF,
913 * in which case it is effectively a truncate operation
915 if (offset + len >= i_size_read(inode)) {
920 new_size = i_size_read(inode) - len;
922 error = xfs_collapse_file_space(ip, offset, len);
925 } else if (mode & FALLOC_FL_INSERT_RANGE) {
926 loff_t isize = i_size_read(inode);
928 if (!xfs_is_falloc_aligned(ip, offset, len)) {
934 * New inode size must not exceed ->s_maxbytes, accounting for
935 * possible signed overflow.
937 if (inode->i_sb->s_maxbytes - isize < len) {
941 new_size = isize + len;
943 /* Offset should be less than i_size */
944 if (offset >= isize) {
948 do_file_insert = true;
950 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
951 offset + len > i_size_read(inode)) {
952 new_size = offset + len;
953 error = inode_newsize_ok(inode, new_size);
958 if (mode & FALLOC_FL_ZERO_RANGE) {
960 * Punch a hole and prealloc the range. We use a hole
961 * punch rather than unwritten extent conversion for two
964 * 1.) Hole punch handles partial block zeroing for us.
965 * 2.) If prealloc returns ENOSPC, the file range is
966 * still zero-valued by virtue of the hole punch.
968 unsigned int blksize = i_blocksize(inode);
970 trace_xfs_zero_file_space(ip);
972 error = xfs_free_file_space(ip, offset, len);
976 len = round_up(offset + len, blksize) -
977 round_down(offset, blksize);
978 offset = round_down(offset, blksize);
979 } else if (mode & FALLOC_FL_UNSHARE_RANGE) {
980 error = xfs_reflink_unshare(ip, offset, len);
985 * If always_cow mode we can't use preallocations and
986 * thus should not create them.
988 if (xfs_is_always_cow_inode(ip)) {
994 if (!xfs_is_always_cow_inode(ip)) {
995 error = xfs_alloc_file_space(ip, offset, len,
1002 /* Change file size if needed */
1006 iattr.ia_valid = ATTR_SIZE;
1007 iattr.ia_size = new_size;
1008 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
1014 * Perform hole insertion now that the file size has been
1015 * updated so that if we crash during the operation we don't
1016 * leave shifted extents past EOF and hence losing access to
1017 * the data that is contained within them.
1019 if (do_file_insert) {
1020 error = xfs_insert_file_space(ip, offset, len);
1025 if (file->f_flags & O_DSYNC)
1026 error = xfs_log_force_inode(ip);
1029 xfs_iunlock(ip, iolock);
1040 struct xfs_inode *ip = XFS_I(file_inode(file));
1045 * Operations creating pages in page cache need protection from hole
1046 * punching and similar ops
1048 if (advice == POSIX_FADV_WILLNEED) {
1049 lockflags = XFS_IOLOCK_SHARED;
1050 xfs_ilock(ip, lockflags);
1052 ret = generic_fadvise(file, start, end, advice);
1054 xfs_iunlock(ip, lockflags);
1058 /* Does this file, inode, or mount want synchronous writes? */
1059 static inline bool xfs_file_sync_writes(struct file *filp)
1061 struct xfs_inode *ip = XFS_I(file_inode(filp));
1063 if (ip->i_mount->m_flags & XFS_MOUNT_WSYNC)
1065 if (filp->f_flags & (__O_SYNC | O_DSYNC))
1067 if (IS_SYNC(file_inode(filp)))
1074 xfs_file_remap_range(
1075 struct file *file_in,
1077 struct file *file_out,
1080 unsigned int remap_flags)
1082 struct inode *inode_in = file_inode(file_in);
1083 struct xfs_inode *src = XFS_I(inode_in);
1084 struct inode *inode_out = file_inode(file_out);
1085 struct xfs_inode *dest = XFS_I(inode_out);
1086 struct xfs_mount *mp = src->i_mount;
1087 loff_t remapped = 0;
1088 xfs_extlen_t cowextsize;
1091 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1094 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1097 if (XFS_FORCED_SHUTDOWN(mp))
1100 /* Prepare and then clone file data. */
1101 ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
1103 if (ret || len == 0)
1106 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
1108 ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
1114 * Carry the cowextsize hint from src to dest if we're sharing the
1115 * entire source file to the entire destination file, the source file
1116 * has a cowextsize hint, and the destination file does not.
1119 if (pos_in == 0 && len == i_size_read(inode_in) &&
1120 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1121 pos_out == 0 && len >= i_size_read(inode_out) &&
1122 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1123 cowextsize = src->i_d.di_cowextsize;
1125 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1130 if (xfs_file_sync_writes(file_in) || xfs_file_sync_writes(file_out))
1131 xfs_log_force_inode(dest);
1133 xfs_iunlock2_io_mmap(src, dest);
1135 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1136 return remapped > 0 ? remapped : ret;
1141 struct inode *inode,
1144 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1146 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1148 file->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
1154 struct inode *inode,
1157 struct xfs_inode *ip = XFS_I(inode);
1161 error = xfs_file_open(inode, file);
1166 * If there are any blocks, read-ahead block 0 as we're almost
1167 * certain to have the next operation be a read there.
1169 mode = xfs_ilock_data_map_shared(ip);
1170 if (ip->i_df.if_nextents > 0)
1171 error = xfs_dir3_data_readahead(ip, 0, 0);
1172 xfs_iunlock(ip, mode);
1178 struct inode *inode,
1181 return xfs_release(XFS_I(inode));
1187 struct dir_context *ctx)
1189 struct inode *inode = file_inode(file);
1190 xfs_inode_t *ip = XFS_I(inode);
1194 * The Linux API doesn't pass down the total size of the buffer
1195 * we read into down to the filesystem. With the filldir concept
1196 * it's not needed for correct information, but the XFS dir2 leaf
1197 * code wants an estimate of the buffer size to calculate it's
1198 * readahead window and size the buffers used for mapping to
1201 * Try to give it an estimate that's good enough, maybe at some
1202 * point we can change the ->readdir prototype to include the
1203 * buffer size. For now we use the current glibc buffer size.
1205 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
1207 return xfs_readdir(NULL, ip, ctx, bufsize);
1216 struct inode *inode = file->f_mapping->host;
1218 if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
1223 return generic_file_llseek(file, offset, whence);
1225 offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
1228 offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
1234 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1238 * Locking for serialisation of IO during page faults. This results in a lock
1242 * sb_start_pagefault(vfs, freeze)
1243 * i_mmaplock (XFS - truncate serialisation)
1245 * i_lock (XFS - extent map serialisation)
1248 __xfs_filemap_fault(
1249 struct vm_fault *vmf,
1250 enum page_entry_size pe_size,
1253 struct inode *inode = file_inode(vmf->vma->vm_file);
1254 struct xfs_inode *ip = XFS_I(inode);
1257 trace_xfs_filemap_fault(ip, pe_size, write_fault);
1260 sb_start_pagefault(inode->i_sb);
1261 file_update_time(vmf->vma->vm_file);
1264 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1265 if (IS_DAX(inode)) {
1268 ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL,
1269 (write_fault && !vmf->cow_page) ?
1270 &xfs_direct_write_iomap_ops :
1271 &xfs_read_iomap_ops);
1272 if (ret & VM_FAULT_NEEDDSYNC)
1273 ret = dax_finish_sync_fault(vmf, pe_size, pfn);
1276 ret = iomap_page_mkwrite(vmf,
1277 &xfs_buffered_write_iomap_ops);
1279 ret = filemap_fault(vmf);
1281 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1284 sb_end_pagefault(inode->i_sb);
1290 struct vm_fault *vmf)
1292 return (vmf->flags & FAULT_FLAG_WRITE) &&
1293 (vmf->vma->vm_flags & VM_SHARED);
1298 struct vm_fault *vmf)
1300 /* DAX can shortcut the normal fault path on write faults! */
1301 return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
1302 IS_DAX(file_inode(vmf->vma->vm_file)) &&
1303 xfs_is_write_fault(vmf));
1307 xfs_filemap_huge_fault(
1308 struct vm_fault *vmf,
1309 enum page_entry_size pe_size)
1311 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1312 return VM_FAULT_FALLBACK;
1314 /* DAX can shortcut the normal fault path on write faults! */
1315 return __xfs_filemap_fault(vmf, pe_size,
1316 xfs_is_write_fault(vmf));
1320 xfs_filemap_page_mkwrite(
1321 struct vm_fault *vmf)
1323 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1327 * pfn_mkwrite was originally intended to ensure we capture time stamp updates
1328 * on write faults. In reality, it needs to serialise against truncate and
1329 * prepare memory for writing so handle is as standard write fault.
1332 xfs_filemap_pfn_mkwrite(
1333 struct vm_fault *vmf)
1336 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1340 xfs_filemap_map_pages(
1341 struct vm_fault *vmf,
1342 pgoff_t start_pgoff,
1345 struct inode *inode = file_inode(vmf->vma->vm_file);
1347 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1348 filemap_map_pages(vmf, start_pgoff, end_pgoff);
1349 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1352 static const struct vm_operations_struct xfs_file_vm_ops = {
1353 .fault = xfs_filemap_fault,
1354 .huge_fault = xfs_filemap_huge_fault,
1355 .map_pages = xfs_filemap_map_pages,
1356 .page_mkwrite = xfs_filemap_page_mkwrite,
1357 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
1363 struct vm_area_struct *vma)
1365 struct inode *inode = file_inode(file);
1366 struct xfs_buftarg *target = xfs_inode_buftarg(XFS_I(inode));
1369 * We don't support synchronous mappings for non-DAX files and
1370 * for DAX files if underneath dax_device is not synchronous.
1372 if (!daxdev_mapping_supported(vma, target->bt_daxdev))
1375 file_accessed(file);
1376 vma->vm_ops = &xfs_file_vm_ops;
1378 vma->vm_flags |= VM_HUGEPAGE;
1382 const struct file_operations xfs_file_operations = {
1383 .llseek = xfs_file_llseek,
1384 .read_iter = xfs_file_read_iter,
1385 .write_iter = xfs_file_write_iter,
1386 .splice_read = generic_file_splice_read,
1387 .splice_write = iter_file_splice_write,
1388 .iopoll = iomap_dio_iopoll,
1389 .unlocked_ioctl = xfs_file_ioctl,
1390 #ifdef CONFIG_COMPAT
1391 .compat_ioctl = xfs_file_compat_ioctl,
1393 .mmap = xfs_file_mmap,
1394 .mmap_supported_flags = MAP_SYNC,
1395 .open = xfs_file_open,
1396 .release = xfs_file_release,
1397 .fsync = xfs_file_fsync,
1398 .get_unmapped_area = thp_get_unmapped_area,
1399 .fallocate = xfs_file_fallocate,
1400 .fadvise = xfs_file_fadvise,
1401 .remap_file_range = xfs_file_remap_range,
1404 const struct file_operations xfs_dir_file_operations = {
1405 .open = xfs_dir_open,
1406 .read = generic_read_dir,
1407 .iterate_shared = xfs_file_readdir,
1408 .llseek = generic_file_llseek,
1409 .unlocked_ioctl = xfs_file_ioctl,
1410 #ifdef CONFIG_COMPAT
1411 .compat_ioctl = xfs_file_compat_ioctl,
1413 .fsync = xfs_dir_fsync,