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);
203 unsigned int lock_mode)
205 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
207 if (iocb->ki_flags & IOCB_NOWAIT) {
208 if (!xfs_ilock_nowait(ip, lock_mode))
211 xfs_ilock(ip, lock_mode);
222 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
225 trace_xfs_file_direct_read(iocb, to);
227 if (!iov_iter_count(to))
228 return 0; /* skip atime */
230 file_accessed(iocb->ki_filp);
232 ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
235 ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL, 0);
236 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
241 static noinline ssize_t
246 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
249 trace_xfs_file_dax_read(iocb, to);
251 if (!iov_iter_count(to))
252 return 0; /* skip atime */
254 ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
257 ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
258 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
260 file_accessed(iocb->ki_filp);
265 xfs_file_buffered_read(
269 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
272 trace_xfs_file_buffered_read(iocb, to);
274 ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
277 ret = generic_file_read_iter(iocb, to);
278 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
288 struct inode *inode = file_inode(iocb->ki_filp);
289 struct xfs_mount *mp = XFS_I(inode)->i_mount;
292 XFS_STATS_INC(mp, xs_read_calls);
294 if (XFS_FORCED_SHUTDOWN(mp))
298 ret = xfs_file_dax_read(iocb, to);
299 else if (iocb->ki_flags & IOCB_DIRECT)
300 ret = xfs_file_dio_read(iocb, to);
302 ret = xfs_file_buffered_read(iocb, to);
305 XFS_STATS_ADD(mp, xs_read_bytes, ret);
310 * Common pre-write limit and setup checks.
312 * Called with the iolocked held either shared and exclusive according to
313 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
314 * if called for a direct write beyond i_size.
317 xfs_file_write_checks(
319 struct iov_iter *from,
322 struct file *file = iocb->ki_filp;
323 struct inode *inode = file->f_mapping->host;
324 struct xfs_inode *ip = XFS_I(inode);
326 size_t count = iov_iter_count(from);
327 bool drained_dio = false;
331 error = generic_write_checks(iocb, from);
335 if (iocb->ki_flags & IOCB_NOWAIT) {
336 error = break_layout(inode, false);
337 if (error == -EWOULDBLOCK)
340 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
347 * For changing security info in file_remove_privs() we need i_rwsem
350 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
351 xfs_iunlock(ip, *iolock);
352 *iolock = XFS_IOLOCK_EXCL;
353 error = xfs_ilock_iocb(iocb, *iolock);
361 * If the offset is beyond the size of the file, we need to zero any
362 * blocks that fall between the existing EOF and the start of this
363 * write. If zeroing is needed and we are currently holding the
364 * iolock shared, we need to update it to exclusive which implies
365 * having to redo all checks before.
367 * We need to serialise against EOF updates that occur in IO
368 * completions here. We want to make sure that nobody is changing the
369 * size while we do this check until we have placed an IO barrier (i.e.
370 * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
371 * The spinlock effectively forms a memory barrier once we have the
372 * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
373 * and hence be able to correctly determine if we need to run zeroing.
375 spin_lock(&ip->i_flags_lock);
376 isize = i_size_read(inode);
377 if (iocb->ki_pos > isize) {
378 spin_unlock(&ip->i_flags_lock);
380 if (iocb->ki_flags & IOCB_NOWAIT)
384 if (*iolock == XFS_IOLOCK_SHARED) {
385 xfs_iunlock(ip, *iolock);
386 *iolock = XFS_IOLOCK_EXCL;
387 xfs_ilock(ip, *iolock);
388 iov_iter_reexpand(from, count);
391 * We now have an IO submission barrier in place, but
392 * AIO can do EOF updates during IO completion and hence
393 * we now need to wait for all of them to drain. Non-AIO
394 * DIO will have drained before we are given the
395 * XFS_IOLOCK_EXCL, and so for most cases this wait is a
398 inode_dio_wait(inode);
403 trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
404 error = iomap_zero_range(inode, isize, iocb->ki_pos - isize,
405 NULL, &xfs_buffered_write_iomap_ops);
409 spin_unlock(&ip->i_flags_lock);
412 * Updating the timestamps will grab the ilock again from
413 * xfs_fs_dirty_inode, so we have to call it after dropping the
414 * lock above. Eventually we should look into a way to avoid
415 * the pointless lock roundtrip.
417 return file_modified(file);
421 xfs_dio_write_end_io(
427 struct inode *inode = file_inode(iocb->ki_filp);
428 struct xfs_inode *ip = XFS_I(inode);
429 loff_t offset = iocb->ki_pos;
430 unsigned int nofs_flag;
432 trace_xfs_end_io_direct_write(ip, offset, size);
434 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
443 * Capture amount written on completion as we can't reliably account
444 * for it on submission.
446 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
449 * We can allocate memory here while doing writeback on behalf of
450 * memory reclaim. To avoid memory allocation deadlocks set the
451 * task-wide nofs context for the following operations.
453 nofs_flag = memalloc_nofs_save();
455 if (flags & IOMAP_DIO_COW) {
456 error = xfs_reflink_end_cow(ip, offset, size);
462 * Unwritten conversion updates the in-core isize after extent
463 * conversion but before updating the on-disk size. Updating isize any
464 * earlier allows a racing dio read to find unwritten extents before
465 * they are converted.
467 if (flags & IOMAP_DIO_UNWRITTEN) {
468 error = xfs_iomap_write_unwritten(ip, offset, size, true);
473 * We need to update the in-core inode size here so that we don't end up
474 * with the on-disk inode size being outside the in-core inode size. We
475 * have no other method of updating EOF for AIO, so always do it here
478 * We need to lock the test/set EOF update as we can be racing with
479 * other IO completions here to update the EOF. Failing to serialise
480 * here can result in EOF moving backwards and Bad Things Happen when
483 spin_lock(&ip->i_flags_lock);
484 if (offset + size > i_size_read(inode)) {
485 i_size_write(inode, offset + size);
486 spin_unlock(&ip->i_flags_lock);
487 error = xfs_setfilesize(ip, offset, size);
489 spin_unlock(&ip->i_flags_lock);
493 memalloc_nofs_restore(nofs_flag);
497 static const struct iomap_dio_ops xfs_dio_write_ops = {
498 .end_io = xfs_dio_write_end_io,
502 * Handle block aligned direct I/O writes
504 static noinline ssize_t
505 xfs_file_dio_write_aligned(
506 struct xfs_inode *ip,
508 struct iov_iter *from)
510 int iolock = XFS_IOLOCK_SHARED;
513 ret = xfs_ilock_iocb(iocb, iolock);
516 ret = xfs_file_write_checks(iocb, from, &iolock);
521 * We don't need to hold the IOLOCK exclusively across the IO, so demote
522 * the iolock back to shared if we had to take the exclusive lock in
523 * xfs_file_write_checks() for other reasons.
525 if (iolock == XFS_IOLOCK_EXCL) {
526 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
527 iolock = XFS_IOLOCK_SHARED;
529 trace_xfs_file_direct_write(iocb, from);
530 ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
531 &xfs_dio_write_ops, 0);
534 xfs_iunlock(ip, iolock);
539 * Handle block unaligned direct I/O writes
541 * In most cases direct I/O writes will be done holding IOLOCK_SHARED, allowing
542 * them to be done in parallel with reads and other direct I/O writes. However,
543 * if the I/O is not aligned to filesystem blocks, the direct I/O layer may need
544 * to do sub-block zeroing and that requires serialisation against other direct
545 * I/O to the same block. In this case we need to serialise the submission of
546 * the unaligned I/O so that we don't get racing block zeroing in the dio layer.
548 * This means that unaligned dio writes always block. There is no "nowait" fast
549 * path in this code - if IOCB_NOWAIT is set we simply return -EAGAIN up front
550 * and we don't have to worry about that anymore.
552 static noinline ssize_t
553 xfs_file_dio_write_unaligned(
554 struct xfs_inode *ip,
556 struct iov_iter *from)
558 int iolock = XFS_IOLOCK_EXCL;
561 /* unaligned dio always waits, bail */
562 if (iocb->ki_flags & IOCB_NOWAIT)
564 xfs_ilock(ip, iolock);
567 * We can't properly handle unaligned direct I/O to reflink files yet,
568 * as we can't unshare a partial block.
570 if (xfs_is_cow_inode(ip)) {
571 trace_xfs_reflink_bounce_dio_write(iocb, from);
576 ret = xfs_file_write_checks(iocb, from, &iolock);
581 * If we are doing unaligned I/O, this must be the only I/O in-flight.
582 * Otherwise we risk data corruption due to unwritten extent conversions
583 * from the AIO end_io handler. Wait for all other I/O to drain first.
585 inode_dio_wait(VFS_I(ip));
587 trace_xfs_file_direct_write(iocb, from);
588 ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
589 &xfs_dio_write_ops, IOMAP_DIO_FORCE_WAIT);
592 xfs_iunlock(ip, iolock);
599 struct iov_iter *from)
601 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
602 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
603 size_t count = iov_iter_count(from);
605 /* direct I/O must be aligned to device logical sector size */
606 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
608 if ((iocb->ki_pos | count) & ip->i_mount->m_blockmask)
609 return xfs_file_dio_write_unaligned(ip, iocb, from);
610 return xfs_file_dio_write_aligned(ip, iocb, from);
613 static noinline ssize_t
616 struct iov_iter *from)
618 struct inode *inode = iocb->ki_filp->f_mapping->host;
619 struct xfs_inode *ip = XFS_I(inode);
620 int iolock = XFS_IOLOCK_EXCL;
621 ssize_t ret, error = 0;
624 ret = xfs_ilock_iocb(iocb, iolock);
627 ret = xfs_file_write_checks(iocb, from, &iolock);
633 trace_xfs_file_dax_write(iocb, from);
634 ret = dax_iomap_rw(iocb, from, &xfs_direct_write_iomap_ops);
635 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
636 i_size_write(inode, iocb->ki_pos);
637 error = xfs_setfilesize(ip, pos, ret);
641 xfs_iunlock(ip, iolock);
646 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
648 /* Handle various SYNC-type writes */
649 ret = generic_write_sync(iocb, ret);
655 xfs_file_buffered_write(
657 struct iov_iter *from)
659 struct file *file = iocb->ki_filp;
660 struct address_space *mapping = file->f_mapping;
661 struct inode *inode = mapping->host;
662 struct xfs_inode *ip = XFS_I(inode);
667 if (iocb->ki_flags & IOCB_NOWAIT)
671 iolock = XFS_IOLOCK_EXCL;
672 xfs_ilock(ip, iolock);
674 ret = xfs_file_write_checks(iocb, from, &iolock);
678 /* We can write back this queue in page reclaim */
679 current->backing_dev_info = inode_to_bdi(inode);
681 trace_xfs_file_buffered_write(iocb, from);
682 ret = iomap_file_buffered_write(iocb, from,
683 &xfs_buffered_write_iomap_ops);
684 if (likely(ret >= 0))
688 * If we hit a space limit, try to free up some lingering preallocated
689 * space before returning an error. In the case of ENOSPC, first try to
690 * write back all dirty inodes to free up some of the excess reserved
691 * metadata space. This reduces the chances that the eofblocks scan
692 * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
693 * also behaves as a filter to prevent too many eofblocks scans from
694 * running at the same time.
696 if (ret == -EDQUOT && !enospc) {
697 xfs_iunlock(ip, iolock);
698 enospc = xfs_inode_free_quota_eofblocks(ip);
701 enospc = xfs_inode_free_quota_cowblocks(ip);
705 } else if (ret == -ENOSPC && !enospc) {
706 struct xfs_eofblocks eofb = {0};
709 xfs_flush_inodes(ip->i_mount);
711 xfs_iunlock(ip, iolock);
712 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
713 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
714 xfs_icache_free_cowblocks(ip->i_mount, &eofb);
718 current->backing_dev_info = NULL;
721 xfs_iunlock(ip, iolock);
724 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
725 /* Handle various SYNC-type writes */
726 ret = generic_write_sync(iocb, ret);
734 struct iov_iter *from)
736 struct file *file = iocb->ki_filp;
737 struct address_space *mapping = file->f_mapping;
738 struct inode *inode = mapping->host;
739 struct xfs_inode *ip = XFS_I(inode);
741 size_t ocount = iov_iter_count(from);
743 XFS_STATS_INC(ip->i_mount, xs_write_calls);
748 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
752 return xfs_file_dax_write(iocb, from);
754 if (iocb->ki_flags & IOCB_DIRECT) {
756 * Allow a directio write to fall back to a buffered
757 * write *only* in the case that we're doing a reflink
758 * CoW. In all other directio scenarios we do not
759 * allow an operation to fall back to buffered mode.
761 ret = xfs_file_dio_write(iocb, from);
766 return xfs_file_buffered_write(iocb, from);
773 struct xfs_inode *ip = XFS_I(inode);
775 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
777 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
781 xfs_break_dax_layouts(
787 ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
789 page = dax_layout_busy_page(inode->i_mapping);
794 return ___wait_var_event(&page->_refcount,
795 atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
796 0, 0, xfs_wait_dax_page(inode));
803 enum layout_break_reason reason)
808 ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
814 error = xfs_break_dax_layouts(inode, &retry);
819 error = xfs_break_leased_layouts(inode, iolock, &retry);
825 } while (error == 0 && retry);
830 #define XFS_FALLOC_FL_SUPPORTED \
831 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
832 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
833 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
842 struct inode *inode = file_inode(file);
843 struct xfs_inode *ip = XFS_I(inode);
845 enum xfs_prealloc_flags flags = 0;
846 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
848 bool do_file_insert = false;
850 if (!S_ISREG(inode->i_mode))
852 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
855 xfs_ilock(ip, iolock);
856 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
861 * Must wait for all AIO to complete before we continue as AIO can
862 * change the file size on completion without holding any locks we
863 * currently hold. We must do this first because AIO can update both
864 * the on disk and in memory inode sizes, and the operations that follow
865 * require the in-memory size to be fully up-to-date.
867 inode_dio_wait(inode);
870 * Now AIO and DIO has drained we flush and (if necessary) invalidate
871 * the cached range over the first operation we are about to run.
873 * We care about zero and collapse here because they both run a hole
874 * punch over the range first. Because that can zero data, and the range
875 * of invalidation for the shift operations is much larger, we still do
876 * the required flush for collapse in xfs_prepare_shift().
878 * Insert has the same range requirements as collapse, and we extend the
879 * file first which can zero data. Hence insert has the same
880 * flush/invalidate requirements as collapse and so they are both
881 * handled at the right time by xfs_prepare_shift().
883 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE |
884 FALLOC_FL_COLLAPSE_RANGE)) {
885 error = xfs_flush_unmap_range(ip, offset, len);
890 if (mode & FALLOC_FL_PUNCH_HOLE) {
891 error = xfs_free_file_space(ip, offset, len);
894 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
895 if (!xfs_is_falloc_aligned(ip, offset, len)) {
901 * There is no need to overlap collapse range with EOF,
902 * in which case it is effectively a truncate operation
904 if (offset + len >= i_size_read(inode)) {
909 new_size = i_size_read(inode) - len;
911 error = xfs_collapse_file_space(ip, offset, len);
914 } else if (mode & FALLOC_FL_INSERT_RANGE) {
915 loff_t isize = i_size_read(inode);
917 if (!xfs_is_falloc_aligned(ip, offset, len)) {
923 * New inode size must not exceed ->s_maxbytes, accounting for
924 * possible signed overflow.
926 if (inode->i_sb->s_maxbytes - isize < len) {
930 new_size = isize + len;
932 /* Offset should be less than i_size */
933 if (offset >= isize) {
937 do_file_insert = true;
939 flags |= XFS_PREALLOC_SET;
941 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
942 offset + len > i_size_read(inode)) {
943 new_size = offset + len;
944 error = inode_newsize_ok(inode, new_size);
949 if (mode & FALLOC_FL_ZERO_RANGE) {
951 * Punch a hole and prealloc the range. We use a hole
952 * punch rather than unwritten extent conversion for two
955 * 1.) Hole punch handles partial block zeroing for us.
956 * 2.) If prealloc returns ENOSPC, the file range is
957 * still zero-valued by virtue of the hole punch.
959 unsigned int blksize = i_blocksize(inode);
961 trace_xfs_zero_file_space(ip);
963 error = xfs_free_file_space(ip, offset, len);
967 len = round_up(offset + len, blksize) -
968 round_down(offset, blksize);
969 offset = round_down(offset, blksize);
970 } else if (mode & FALLOC_FL_UNSHARE_RANGE) {
971 error = xfs_reflink_unshare(ip, offset, len);
976 * If always_cow mode we can't use preallocations and
977 * thus should not create them.
979 if (xfs_is_always_cow_inode(ip)) {
985 if (!xfs_is_always_cow_inode(ip)) {
986 error = xfs_alloc_file_space(ip, offset, len,
993 if (file->f_flags & O_DSYNC)
994 flags |= XFS_PREALLOC_SYNC;
996 error = xfs_update_prealloc_flags(ip, flags);
1000 /* Change file size if needed */
1004 iattr.ia_valid = ATTR_SIZE;
1005 iattr.ia_size = new_size;
1006 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
1012 * Perform hole insertion now that the file size has been
1013 * updated so that if we crash during the operation we don't
1014 * leave shifted extents past EOF and hence losing access to
1015 * the data that is contained within them.
1018 error = xfs_insert_file_space(ip, offset, len);
1021 xfs_iunlock(ip, iolock);
1032 struct xfs_inode *ip = XFS_I(file_inode(file));
1037 * Operations creating pages in page cache need protection from hole
1038 * punching and similar ops
1040 if (advice == POSIX_FADV_WILLNEED) {
1041 lockflags = XFS_IOLOCK_SHARED;
1042 xfs_ilock(ip, lockflags);
1044 ret = generic_fadvise(file, start, end, advice);
1046 xfs_iunlock(ip, lockflags);
1050 /* Does this file, inode, or mount want synchronous writes? */
1051 static inline bool xfs_file_sync_writes(struct file *filp)
1053 struct xfs_inode *ip = XFS_I(file_inode(filp));
1055 if (ip->i_mount->m_flags & XFS_MOUNT_WSYNC)
1057 if (filp->f_flags & (__O_SYNC | O_DSYNC))
1059 if (IS_SYNC(file_inode(filp)))
1066 xfs_file_remap_range(
1067 struct file *file_in,
1069 struct file *file_out,
1072 unsigned int remap_flags)
1074 struct inode *inode_in = file_inode(file_in);
1075 struct xfs_inode *src = XFS_I(inode_in);
1076 struct inode *inode_out = file_inode(file_out);
1077 struct xfs_inode *dest = XFS_I(inode_out);
1078 struct xfs_mount *mp = src->i_mount;
1079 loff_t remapped = 0;
1080 xfs_extlen_t cowextsize;
1083 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1086 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1089 if (XFS_FORCED_SHUTDOWN(mp))
1092 /* Prepare and then clone file data. */
1093 ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
1095 if (ret || len == 0)
1098 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
1100 ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
1106 * Carry the cowextsize hint from src to dest if we're sharing the
1107 * entire source file to the entire destination file, the source file
1108 * has a cowextsize hint, and the destination file does not.
1111 if (pos_in == 0 && len == i_size_read(inode_in) &&
1112 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1113 pos_out == 0 && len >= i_size_read(inode_out) &&
1114 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1115 cowextsize = src->i_d.di_cowextsize;
1117 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1122 if (xfs_file_sync_writes(file_in) || xfs_file_sync_writes(file_out))
1123 xfs_log_force_inode(dest);
1125 xfs_iunlock2_io_mmap(src, dest);
1127 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1128 return remapped > 0 ? remapped : ret;
1133 struct inode *inode,
1136 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1138 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1140 file->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
1146 struct inode *inode,
1149 struct xfs_inode *ip = XFS_I(inode);
1153 error = xfs_file_open(inode, file);
1158 * If there are any blocks, read-ahead block 0 as we're almost
1159 * certain to have the next operation be a read there.
1161 mode = xfs_ilock_data_map_shared(ip);
1162 if (ip->i_df.if_nextents > 0)
1163 error = xfs_dir3_data_readahead(ip, 0, 0);
1164 xfs_iunlock(ip, mode);
1170 struct inode *inode,
1173 return xfs_release(XFS_I(inode));
1179 struct dir_context *ctx)
1181 struct inode *inode = file_inode(file);
1182 xfs_inode_t *ip = XFS_I(inode);
1186 * The Linux API doesn't pass down the total size of the buffer
1187 * we read into down to the filesystem. With the filldir concept
1188 * it's not needed for correct information, but the XFS dir2 leaf
1189 * code wants an estimate of the buffer size to calculate it's
1190 * readahead window and size the buffers used for mapping to
1193 * Try to give it an estimate that's good enough, maybe at some
1194 * point we can change the ->readdir prototype to include the
1195 * buffer size. For now we use the current glibc buffer size.
1197 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
1199 return xfs_readdir(NULL, ip, ctx, bufsize);
1208 struct inode *inode = file->f_mapping->host;
1210 if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
1215 return generic_file_llseek(file, offset, whence);
1217 offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
1220 offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
1226 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1230 * Locking for serialisation of IO during page faults. This results in a lock
1234 * sb_start_pagefault(vfs, freeze)
1235 * i_mmaplock (XFS - truncate serialisation)
1237 * i_lock (XFS - extent map serialisation)
1240 __xfs_filemap_fault(
1241 struct vm_fault *vmf,
1242 enum page_entry_size pe_size,
1245 struct inode *inode = file_inode(vmf->vma->vm_file);
1246 struct xfs_inode *ip = XFS_I(inode);
1249 trace_xfs_filemap_fault(ip, pe_size, write_fault);
1252 sb_start_pagefault(inode->i_sb);
1253 file_update_time(vmf->vma->vm_file);
1256 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1257 if (IS_DAX(inode)) {
1260 ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL,
1261 (write_fault && !vmf->cow_page) ?
1262 &xfs_direct_write_iomap_ops :
1263 &xfs_read_iomap_ops);
1264 if (ret & VM_FAULT_NEEDDSYNC)
1265 ret = dax_finish_sync_fault(vmf, pe_size, pfn);
1268 ret = iomap_page_mkwrite(vmf,
1269 &xfs_buffered_write_iomap_ops);
1271 ret = filemap_fault(vmf);
1273 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1276 sb_end_pagefault(inode->i_sb);
1282 struct vm_fault *vmf)
1284 return (vmf->flags & FAULT_FLAG_WRITE) &&
1285 (vmf->vma->vm_flags & VM_SHARED);
1290 struct vm_fault *vmf)
1292 /* DAX can shortcut the normal fault path on write faults! */
1293 return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
1294 IS_DAX(file_inode(vmf->vma->vm_file)) &&
1295 xfs_is_write_fault(vmf));
1299 xfs_filemap_huge_fault(
1300 struct vm_fault *vmf,
1301 enum page_entry_size pe_size)
1303 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1304 return VM_FAULT_FALLBACK;
1306 /* DAX can shortcut the normal fault path on write faults! */
1307 return __xfs_filemap_fault(vmf, pe_size,
1308 xfs_is_write_fault(vmf));
1312 xfs_filemap_page_mkwrite(
1313 struct vm_fault *vmf)
1315 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1319 * pfn_mkwrite was originally intended to ensure we capture time stamp updates
1320 * on write faults. In reality, it needs to serialise against truncate and
1321 * prepare memory for writing so handle is as standard write fault.
1324 xfs_filemap_pfn_mkwrite(
1325 struct vm_fault *vmf)
1328 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1332 xfs_filemap_map_pages(
1333 struct vm_fault *vmf,
1334 pgoff_t start_pgoff,
1337 struct inode *inode = file_inode(vmf->vma->vm_file);
1339 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1340 filemap_map_pages(vmf, start_pgoff, end_pgoff);
1341 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1344 static const struct vm_operations_struct xfs_file_vm_ops = {
1345 .fault = xfs_filemap_fault,
1346 .huge_fault = xfs_filemap_huge_fault,
1347 .map_pages = xfs_filemap_map_pages,
1348 .page_mkwrite = xfs_filemap_page_mkwrite,
1349 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
1355 struct vm_area_struct *vma)
1357 struct inode *inode = file_inode(file);
1358 struct xfs_buftarg *target = xfs_inode_buftarg(XFS_I(inode));
1361 * We don't support synchronous mappings for non-DAX files and
1362 * for DAX files if underneath dax_device is not synchronous.
1364 if (!daxdev_mapping_supported(vma, target->bt_daxdev))
1367 file_accessed(file);
1368 vma->vm_ops = &xfs_file_vm_ops;
1370 vma->vm_flags |= VM_HUGEPAGE;
1374 const struct file_operations xfs_file_operations = {
1375 .llseek = xfs_file_llseek,
1376 .read_iter = xfs_file_read_iter,
1377 .write_iter = xfs_file_write_iter,
1378 .splice_read = generic_file_splice_read,
1379 .splice_write = iter_file_splice_write,
1380 .iopoll = iomap_dio_iopoll,
1381 .unlocked_ioctl = xfs_file_ioctl,
1382 #ifdef CONFIG_COMPAT
1383 .compat_ioctl = xfs_file_compat_ioctl,
1385 .mmap = xfs_file_mmap,
1386 .mmap_supported_flags = MAP_SYNC,
1387 .open = xfs_file_open,
1388 .release = xfs_file_release,
1389 .fsync = xfs_file_fsync,
1390 .get_unmapped_area = thp_get_unmapped_area,
1391 .fallocate = xfs_file_fallocate,
1392 .fadvise = xfs_file_fadvise,
1393 .remap_file_range = xfs_file_remap_range,
1396 const struct file_operations xfs_dir_file_operations = {
1397 .open = xfs_dir_open,
1398 .read = generic_read_dir,
1399 .iterate_shared = xfs_file_readdir,
1400 .llseek = generic_file_llseek,
1401 .unlocked_ioctl = xfs_file_ioctl,
1402 #ifdef CONFIG_COMPAT
1403 .compat_ioctl = xfs_file_compat_ioctl,
1405 .fsync = xfs_dir_fsync,