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);
123 struct xfs_inode *ip,
126 if (!xfs_ipincount(ip))
128 if (datasync && !(ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
130 return ip->i_itemp->ili_commit_seq;
134 * All metadata updates are logged, which means that we just have to flush the
135 * log up to the latest LSN that touched the inode.
137 * If we have concurrent fsync/fdatasync() calls, we need them to all block on
138 * the log force before we clear the ili_fsync_fields field. This ensures that
139 * we don't get a racing sync operation that does not wait for the metadata to
140 * hit the journal before returning. If we race with clearing ili_fsync_fields,
141 * then all that will happen is the log force will do nothing as the lsn will
142 * already be on disk. We can't race with setting ili_fsync_fields because that
143 * is done under XFS_ILOCK_EXCL, and that can't happen because we hold the lock
144 * shared until after the ili_fsync_fields is cleared.
148 struct xfs_inode *ip,
155 xfs_ilock(ip, XFS_ILOCK_SHARED);
156 seq = xfs_fsync_seq(ip, datasync);
158 error = xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC,
161 spin_lock(&ip->i_itemp->ili_lock);
162 ip->i_itemp->ili_fsync_fields = 0;
163 spin_unlock(&ip->i_itemp->ili_lock);
165 xfs_iunlock(ip, XFS_ILOCK_SHARED);
176 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
177 struct xfs_mount *mp = ip->i_mount;
181 trace_xfs_file_fsync(ip);
183 error = file_write_and_wait_range(file, start, end);
187 if (XFS_FORCED_SHUTDOWN(mp))
190 xfs_iflags_clear(ip, XFS_ITRUNCATED);
193 * If we have an RT and/or log subvolume we need to make sure to flush
194 * the write cache the device used for file data first. This is to
195 * ensure newly written file data make it to disk before logging the new
196 * inode size in case of an extending write.
198 if (XFS_IS_REALTIME_INODE(ip))
199 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
200 else if (mp->m_logdev_targp != mp->m_ddev_targp)
201 xfs_blkdev_issue_flush(mp->m_ddev_targp);
203 error = xfs_fsync_flush_log(ip, datasync, &log_flushed);
206 * If we only have a single device, and the log force about was
207 * a no-op we might have to flush the data device cache here.
208 * This can only happen for fdatasync/O_DSYNC if we were overwriting
209 * an already allocated file and thus do not have any metadata to
212 if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
213 mp->m_logdev_targp == mp->m_ddev_targp)
214 xfs_blkdev_issue_flush(mp->m_ddev_targp);
220 xfs_file_dio_aio_read(
224 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
225 size_t count = iov_iter_count(to);
228 trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
231 return 0; /* skip atime */
233 file_accessed(iocb->ki_filp);
235 if (iocb->ki_flags & IOCB_NOWAIT) {
236 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
239 xfs_ilock(ip, XFS_IOLOCK_SHARED);
241 ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
242 is_sync_kiocb(iocb));
243 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
248 static noinline ssize_t
253 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
254 size_t count = iov_iter_count(to);
257 trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
260 return 0; /* skip atime */
262 if (iocb->ki_flags & IOCB_NOWAIT) {
263 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
266 xfs_ilock(ip, XFS_IOLOCK_SHARED);
269 ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
270 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
272 file_accessed(iocb->ki_filp);
277 xfs_file_buffered_aio_read(
281 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
284 trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
286 if (iocb->ki_flags & IOCB_NOWAIT) {
287 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
290 xfs_ilock(ip, XFS_IOLOCK_SHARED);
292 ret = generic_file_read_iter(iocb, to);
293 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
303 struct inode *inode = file_inode(iocb->ki_filp);
304 struct xfs_mount *mp = XFS_I(inode)->i_mount;
307 XFS_STATS_INC(mp, xs_read_calls);
309 if (XFS_FORCED_SHUTDOWN(mp))
313 ret = xfs_file_dax_read(iocb, to);
314 else if (iocb->ki_flags & IOCB_DIRECT)
315 ret = xfs_file_dio_aio_read(iocb, to);
317 ret = xfs_file_buffered_aio_read(iocb, to);
320 XFS_STATS_ADD(mp, xs_read_bytes, ret);
325 * Common pre-write limit and setup checks.
327 * Called with the iolocked held either shared and exclusive according to
328 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
329 * if called for a direct write beyond i_size.
332 xfs_file_aio_write_checks(
334 struct iov_iter *from,
337 struct file *file = iocb->ki_filp;
338 struct inode *inode = file->f_mapping->host;
339 struct xfs_inode *ip = XFS_I(inode);
341 size_t count = iov_iter_count(from);
342 bool drained_dio = false;
346 error = generic_write_checks(iocb, from);
350 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
355 * For changing security info in file_remove_privs() we need i_rwsem
358 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
359 xfs_iunlock(ip, *iolock);
360 *iolock = XFS_IOLOCK_EXCL;
361 xfs_ilock(ip, *iolock);
365 * If the offset is beyond the size of the file, we need to zero any
366 * blocks that fall between the existing EOF and the start of this
367 * write. If zeroing is needed and we are currently holding the
368 * iolock shared, we need to update it to exclusive which implies
369 * having to redo all checks before.
371 * We need to serialise against EOF updates that occur in IO
372 * completions here. We want to make sure that nobody is changing the
373 * size while we do this check until we have placed an IO barrier (i.e.
374 * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
375 * The spinlock effectively forms a memory barrier once we have the
376 * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
377 * and hence be able to correctly determine if we need to run zeroing.
379 spin_lock(&ip->i_flags_lock);
380 isize = i_size_read(inode);
381 if (iocb->ki_pos > isize) {
382 spin_unlock(&ip->i_flags_lock);
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 * xfs_file_dio_aio_write - handle direct IO writes
504 * Lock the inode appropriately to prepare for and issue a direct IO write.
505 * By separating it from the buffered write path we remove all the tricky to
506 * follow locking changes and looping.
508 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
509 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
510 * pages are flushed out.
512 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
513 * allowing them to be done in parallel with reads and other direct IO writes.
514 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
515 * needs to do sub-block zeroing and that requires serialisation against other
516 * direct IOs to the same block. In this case we need to serialise the
517 * submission of the unaligned IOs so that we don't get racing block zeroing in
518 * the dio layer. To avoid the problem with aio, we also need to wait for
519 * outstanding IOs to complete so that unwritten extent conversion is completed
520 * before we try to map the overlapping block. This is currently implemented by
521 * hitting it with a big hammer (i.e. inode_dio_wait()).
523 * Returns with locks held indicated by @iolock and errors indicated by
524 * negative return values.
527 xfs_file_dio_aio_write(
529 struct iov_iter *from)
531 struct file *file = iocb->ki_filp;
532 struct address_space *mapping = file->f_mapping;
533 struct inode *inode = mapping->host;
534 struct xfs_inode *ip = XFS_I(inode);
535 struct xfs_mount *mp = ip->i_mount;
537 int unaligned_io = 0;
539 size_t count = iov_iter_count(from);
540 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
542 /* DIO must be aligned to device logical sector size */
543 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
547 * Don't take the exclusive iolock here unless the I/O is unaligned to
548 * the file system block size. We don't need to consider the EOF
549 * extension case here because xfs_file_aio_write_checks() will relock
550 * the inode as necessary for EOF zeroing cases and fill out the new
551 * inode size as appropriate.
553 if ((iocb->ki_pos & mp->m_blockmask) ||
554 ((iocb->ki_pos + count) & mp->m_blockmask)) {
558 * We can't properly handle unaligned direct I/O to reflink
559 * files yet, as we can't unshare a partial block.
561 if (xfs_is_cow_inode(ip)) {
562 trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
565 iolock = XFS_IOLOCK_EXCL;
567 iolock = XFS_IOLOCK_SHARED;
570 if (iocb->ki_flags & IOCB_NOWAIT) {
571 /* unaligned dio always waits, bail */
574 if (!xfs_ilock_nowait(ip, iolock))
577 xfs_ilock(ip, iolock);
580 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
583 count = iov_iter_count(from);
586 * If we are doing unaligned IO, we can't allow any other overlapping IO
587 * in-flight at the same time or we risk data corruption. Wait for all
588 * other IO to drain before we submit. If the IO is aligned, demote the
589 * iolock if we had to take the exclusive lock in
590 * xfs_file_aio_write_checks() for other reasons.
593 inode_dio_wait(inode);
594 } else if (iolock == XFS_IOLOCK_EXCL) {
595 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
596 iolock = XFS_IOLOCK_SHARED;
599 trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
601 * If unaligned, this is the only IO in-flight. Wait on it before we
602 * release the iolock to prevent subsequent overlapping IO.
604 ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
606 is_sync_kiocb(iocb) || unaligned_io);
608 xfs_iunlock(ip, iolock);
611 * No fallback to buffered IO after short writes for XFS, direct I/O
612 * will either complete fully or return an error.
614 ASSERT(ret < 0 || ret == count);
618 static noinline ssize_t
621 struct iov_iter *from)
623 struct inode *inode = iocb->ki_filp->f_mapping->host;
624 struct xfs_inode *ip = XFS_I(inode);
625 int iolock = XFS_IOLOCK_EXCL;
626 ssize_t ret, error = 0;
630 if (iocb->ki_flags & IOCB_NOWAIT) {
631 if (!xfs_ilock_nowait(ip, iolock))
634 xfs_ilock(ip, iolock);
637 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
642 count = iov_iter_count(from);
644 trace_xfs_file_dax_write(ip, count, pos);
645 ret = dax_iomap_rw(iocb, from, &xfs_direct_write_iomap_ops);
646 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
647 i_size_write(inode, iocb->ki_pos);
648 error = xfs_setfilesize(ip, pos, ret);
651 xfs_iunlock(ip, iolock);
656 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
658 /* Handle various SYNC-type writes */
659 ret = generic_write_sync(iocb, ret);
665 xfs_file_buffered_aio_write(
667 struct iov_iter *from)
669 struct file *file = iocb->ki_filp;
670 struct address_space *mapping = file->f_mapping;
671 struct inode *inode = mapping->host;
672 struct xfs_inode *ip = XFS_I(inode);
677 if (iocb->ki_flags & IOCB_NOWAIT)
681 iolock = XFS_IOLOCK_EXCL;
682 xfs_ilock(ip, iolock);
684 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
688 /* We can write back this queue in page reclaim */
689 current->backing_dev_info = inode_to_bdi(inode);
691 trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
692 ret = iomap_file_buffered_write(iocb, from,
693 &xfs_buffered_write_iomap_ops);
694 if (likely(ret >= 0))
698 * If we hit a space limit, try to free up some lingering preallocated
699 * space before returning an error. In the case of ENOSPC, first try to
700 * write back all dirty inodes to free up some of the excess reserved
701 * metadata space. This reduces the chances that the eofblocks scan
702 * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
703 * also behaves as a filter to prevent too many eofblocks scans from
704 * running at the same time.
706 if (ret == -EDQUOT && !enospc) {
707 xfs_iunlock(ip, iolock);
708 enospc = xfs_inode_free_quota_eofblocks(ip);
711 enospc = xfs_inode_free_quota_cowblocks(ip);
715 } else if (ret == -ENOSPC && !enospc) {
716 struct xfs_eofblocks eofb = {0};
719 xfs_flush_inodes(ip->i_mount);
721 xfs_iunlock(ip, iolock);
722 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
723 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
724 xfs_icache_free_cowblocks(ip->i_mount, &eofb);
728 current->backing_dev_info = NULL;
731 xfs_iunlock(ip, iolock);
734 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
735 /* Handle various SYNC-type writes */
736 ret = generic_write_sync(iocb, ret);
744 struct iov_iter *from)
746 struct file *file = iocb->ki_filp;
747 struct address_space *mapping = file->f_mapping;
748 struct inode *inode = mapping->host;
749 struct xfs_inode *ip = XFS_I(inode);
751 size_t ocount = iov_iter_count(from);
753 XFS_STATS_INC(ip->i_mount, xs_write_calls);
758 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
762 return xfs_file_dax_write(iocb, from);
764 if (iocb->ki_flags & IOCB_DIRECT) {
766 * Allow a directio write to fall back to a buffered
767 * write *only* in the case that we're doing a reflink
768 * CoW. In all other directio scenarios we do not
769 * allow an operation to fall back to buffered mode.
771 ret = xfs_file_dio_aio_write(iocb, from);
776 return xfs_file_buffered_aio_write(iocb, from);
783 struct xfs_inode *ip = XFS_I(inode);
785 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
787 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
791 xfs_break_dax_layouts(
797 ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
799 page = dax_layout_busy_page(inode->i_mapping);
804 return ___wait_var_event(&page->_refcount,
805 atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
806 0, 0, xfs_wait_dax_page(inode));
813 enum layout_break_reason reason)
818 ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
824 error = xfs_break_dax_layouts(inode, &retry);
829 error = xfs_break_leased_layouts(inode, iolock, &retry);
835 } while (error == 0 && retry);
840 #define XFS_FALLOC_FL_SUPPORTED \
841 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
842 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
843 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
852 struct inode *inode = file_inode(file);
853 struct xfs_inode *ip = XFS_I(inode);
855 enum xfs_prealloc_flags flags = 0;
856 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
858 bool do_file_insert = false;
860 if (!S_ISREG(inode->i_mode))
862 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
865 xfs_ilock(ip, iolock);
866 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
871 * Must wait for all AIO to complete before we continue as AIO can
872 * change the file size on completion without holding any locks we
873 * currently hold. We must do this first because AIO can update both
874 * the on disk and in memory inode sizes, and the operations that follow
875 * require the in-memory size to be fully up-to-date.
877 inode_dio_wait(inode);
880 * Now AIO and DIO has drained we flush and (if necessary) invalidate
881 * the cached range over the first operation we are about to run.
883 * We care about zero and collapse here because they both run a hole
884 * punch over the range first. Because that can zero data, and the range
885 * of invalidation for the shift operations is much larger, we still do
886 * the required flush for collapse in xfs_prepare_shift().
888 * Insert has the same range requirements as collapse, and we extend the
889 * file first which can zero data. Hence insert has the same
890 * flush/invalidate requirements as collapse and so they are both
891 * handled at the right time by xfs_prepare_shift().
893 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE |
894 FALLOC_FL_COLLAPSE_RANGE)) {
895 error = xfs_flush_unmap_range(ip, offset, len);
900 if (mode & FALLOC_FL_PUNCH_HOLE) {
901 error = xfs_free_file_space(ip, offset, len);
904 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
905 if (!xfs_is_falloc_aligned(ip, offset, len)) {
911 * There is no need to overlap collapse range with EOF,
912 * in which case it is effectively a truncate operation
914 if (offset + len >= i_size_read(inode)) {
919 new_size = i_size_read(inode) - len;
921 error = xfs_collapse_file_space(ip, offset, len);
924 } else if (mode & FALLOC_FL_INSERT_RANGE) {
925 loff_t isize = i_size_read(inode);
927 if (!xfs_is_falloc_aligned(ip, offset, len)) {
933 * New inode size must not exceed ->s_maxbytes, accounting for
934 * possible signed overflow.
936 if (inode->i_sb->s_maxbytes - isize < len) {
940 new_size = isize + len;
942 /* Offset should be less than i_size */
943 if (offset >= isize) {
947 do_file_insert = true;
949 flags |= XFS_PREALLOC_SET;
951 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
952 offset + len > i_size_read(inode)) {
953 new_size = offset + len;
954 error = inode_newsize_ok(inode, new_size);
959 if (mode & FALLOC_FL_ZERO_RANGE) {
961 * Punch a hole and prealloc the range. We use a hole
962 * punch rather than unwritten extent conversion for two
965 * 1.) Hole punch handles partial block zeroing for us.
966 * 2.) If prealloc returns ENOSPC, the file range is
967 * still zero-valued by virtue of the hole punch.
969 unsigned int blksize = i_blocksize(inode);
971 trace_xfs_zero_file_space(ip);
973 error = xfs_free_file_space(ip, offset, len);
977 len = round_up(offset + len, blksize) -
978 round_down(offset, blksize);
979 offset = round_down(offset, blksize);
980 } else if (mode & FALLOC_FL_UNSHARE_RANGE) {
981 error = xfs_reflink_unshare(ip, offset, len);
986 * If always_cow mode we can't use preallocations and
987 * thus should not create them.
989 if (xfs_is_always_cow_inode(ip)) {
995 if (!xfs_is_always_cow_inode(ip)) {
996 error = xfs_alloc_file_space(ip, offset, len,
1003 if (file->f_flags & O_DSYNC)
1004 flags |= XFS_PREALLOC_SYNC;
1006 error = xfs_update_prealloc_flags(ip, flags);
1010 /* Change file size if needed */
1014 iattr.ia_valid = ATTR_SIZE;
1015 iattr.ia_size = new_size;
1016 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
1022 * Perform hole insertion now that the file size has been
1023 * updated so that if we crash during the operation we don't
1024 * leave shifted extents past EOF and hence losing access to
1025 * the data that is contained within them.
1028 error = xfs_insert_file_space(ip, offset, len);
1031 xfs_iunlock(ip, iolock);
1042 struct xfs_inode *ip = XFS_I(file_inode(file));
1047 * Operations creating pages in page cache need protection from hole
1048 * punching and similar ops
1050 if (advice == POSIX_FADV_WILLNEED) {
1051 lockflags = XFS_IOLOCK_SHARED;
1052 xfs_ilock(ip, lockflags);
1054 ret = generic_fadvise(file, start, end, advice);
1056 xfs_iunlock(ip, lockflags);
1060 /* Does this file, inode, or mount want synchronous writes? */
1061 static inline bool xfs_file_sync_writes(struct file *filp)
1063 struct xfs_inode *ip = XFS_I(file_inode(filp));
1065 if (ip->i_mount->m_flags & XFS_MOUNT_WSYNC)
1067 if (filp->f_flags & (__O_SYNC | O_DSYNC))
1069 if (IS_SYNC(file_inode(filp)))
1076 xfs_file_remap_range(
1077 struct file *file_in,
1079 struct file *file_out,
1082 unsigned int remap_flags)
1084 struct inode *inode_in = file_inode(file_in);
1085 struct xfs_inode *src = XFS_I(inode_in);
1086 struct inode *inode_out = file_inode(file_out);
1087 struct xfs_inode *dest = XFS_I(inode_out);
1088 struct xfs_mount *mp = src->i_mount;
1089 loff_t remapped = 0;
1090 xfs_extlen_t cowextsize;
1093 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1096 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1099 if (XFS_FORCED_SHUTDOWN(mp))
1102 /* Prepare and then clone file data. */
1103 ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
1105 if (ret || len == 0)
1108 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
1110 ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
1116 * Carry the cowextsize hint from src to dest if we're sharing the
1117 * entire source file to the entire destination file, the source file
1118 * has a cowextsize hint, and the destination file does not.
1121 if (pos_in == 0 && len == i_size_read(inode_in) &&
1122 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1123 pos_out == 0 && len >= i_size_read(inode_out) &&
1124 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1125 cowextsize = src->i_d.di_cowextsize;
1127 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1132 if (xfs_file_sync_writes(file_in) || xfs_file_sync_writes(file_out))
1133 xfs_log_force_inode(dest);
1135 xfs_iunlock2_io_mmap(src, dest);
1137 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1138 return remapped > 0 ? remapped : ret;
1143 struct inode *inode,
1146 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1148 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1150 file->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
1156 struct inode *inode,
1159 struct xfs_inode *ip = XFS_I(inode);
1163 error = xfs_file_open(inode, file);
1168 * If there are any blocks, read-ahead block 0 as we're almost
1169 * certain to have the next operation be a read there.
1171 mode = xfs_ilock_data_map_shared(ip);
1172 if (ip->i_df.if_nextents > 0)
1173 error = xfs_dir3_data_readahead(ip, 0, 0);
1174 xfs_iunlock(ip, mode);
1180 struct inode *inode,
1183 return xfs_release(XFS_I(inode));
1189 struct dir_context *ctx)
1191 struct inode *inode = file_inode(file);
1192 xfs_inode_t *ip = XFS_I(inode);
1196 * The Linux API doesn't pass down the total size of the buffer
1197 * we read into down to the filesystem. With the filldir concept
1198 * it's not needed for correct information, but the XFS dir2 leaf
1199 * code wants an estimate of the buffer size to calculate it's
1200 * readahead window and size the buffers used for mapping to
1203 * Try to give it an estimate that's good enough, maybe at some
1204 * point we can change the ->readdir prototype to include the
1205 * buffer size. For now we use the current glibc buffer size.
1207 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
1209 return xfs_readdir(NULL, ip, ctx, bufsize);
1218 struct inode *inode = file->f_mapping->host;
1220 if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
1225 return generic_file_llseek(file, offset, whence);
1227 offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
1230 offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
1236 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1240 * Locking for serialisation of IO during page faults. This results in a lock
1244 * sb_start_pagefault(vfs, freeze)
1245 * i_mmaplock (XFS - truncate serialisation)
1247 * i_lock (XFS - extent map serialisation)
1250 __xfs_filemap_fault(
1251 struct vm_fault *vmf,
1252 enum page_entry_size pe_size,
1255 struct inode *inode = file_inode(vmf->vma->vm_file);
1256 struct xfs_inode *ip = XFS_I(inode);
1259 trace_xfs_filemap_fault(ip, pe_size, write_fault);
1262 sb_start_pagefault(inode->i_sb);
1263 file_update_time(vmf->vma->vm_file);
1266 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1267 if (IS_DAX(inode)) {
1270 ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL,
1271 (write_fault && !vmf->cow_page) ?
1272 &xfs_direct_write_iomap_ops :
1273 &xfs_read_iomap_ops);
1274 if (ret & VM_FAULT_NEEDDSYNC)
1275 ret = dax_finish_sync_fault(vmf, pe_size, pfn);
1278 ret = iomap_page_mkwrite(vmf,
1279 &xfs_buffered_write_iomap_ops);
1281 ret = filemap_fault(vmf);
1283 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1286 sb_end_pagefault(inode->i_sb);
1292 struct vm_fault *vmf)
1294 return (vmf->flags & FAULT_FLAG_WRITE) &&
1295 (vmf->vma->vm_flags & VM_SHARED);
1300 struct vm_fault *vmf)
1302 /* DAX can shortcut the normal fault path on write faults! */
1303 return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
1304 IS_DAX(file_inode(vmf->vma->vm_file)) &&
1305 xfs_is_write_fault(vmf));
1309 xfs_filemap_huge_fault(
1310 struct vm_fault *vmf,
1311 enum page_entry_size pe_size)
1313 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1314 return VM_FAULT_FALLBACK;
1316 /* DAX can shortcut the normal fault path on write faults! */
1317 return __xfs_filemap_fault(vmf, pe_size,
1318 xfs_is_write_fault(vmf));
1322 xfs_filemap_page_mkwrite(
1323 struct vm_fault *vmf)
1325 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1329 * pfn_mkwrite was originally intended to ensure we capture time stamp updates
1330 * on write faults. In reality, it needs to serialise against truncate and
1331 * prepare memory for writing so handle is as standard write fault.
1334 xfs_filemap_pfn_mkwrite(
1335 struct vm_fault *vmf)
1338 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1342 xfs_filemap_map_pages(
1343 struct vm_fault *vmf,
1344 pgoff_t start_pgoff,
1347 struct inode *inode = file_inode(vmf->vma->vm_file);
1349 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1350 filemap_map_pages(vmf, start_pgoff, end_pgoff);
1351 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1354 static const struct vm_operations_struct xfs_file_vm_ops = {
1355 .fault = xfs_filemap_fault,
1356 .huge_fault = xfs_filemap_huge_fault,
1357 .map_pages = xfs_filemap_map_pages,
1358 .page_mkwrite = xfs_filemap_page_mkwrite,
1359 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
1365 struct vm_area_struct *vma)
1367 struct inode *inode = file_inode(file);
1368 struct xfs_buftarg *target = xfs_inode_buftarg(XFS_I(inode));
1371 * We don't support synchronous mappings for non-DAX files and
1372 * for DAX files if underneath dax_device is not synchronous.
1374 if (!daxdev_mapping_supported(vma, target->bt_daxdev))
1377 file_accessed(file);
1378 vma->vm_ops = &xfs_file_vm_ops;
1380 vma->vm_flags |= VM_HUGEPAGE;
1384 const struct file_operations xfs_file_operations = {
1385 .llseek = xfs_file_llseek,
1386 .read_iter = xfs_file_read_iter,
1387 .write_iter = xfs_file_write_iter,
1388 .splice_read = generic_file_splice_read,
1389 .splice_write = iter_file_splice_write,
1390 .iopoll = iomap_dio_iopoll,
1391 .unlocked_ioctl = xfs_file_ioctl,
1392 #ifdef CONFIG_COMPAT
1393 .compat_ioctl = xfs_file_compat_ioctl,
1395 .mmap = xfs_file_mmap,
1396 .mmap_supported_flags = MAP_SYNC,
1397 .open = xfs_file_open,
1398 .release = xfs_file_release,
1399 .fsync = xfs_file_fsync,
1400 .get_unmapped_area = thp_get_unmapped_area,
1401 .fallocate = xfs_file_fallocate,
1402 .fadvise = xfs_file_fadvise,
1403 .remap_file_range = xfs_file_remap_range,
1406 const struct file_operations xfs_dir_file_operations = {
1407 .open = xfs_dir_open,
1408 .read = generic_read_dir,
1409 .iterate_shared = xfs_file_readdir,
1410 .llseek = generic_file_llseek,
1411 .unlocked_ioctl = xfs_file_ioctl,
1412 #ifdef CONFIG_COMPAT
1413 .compat_ioctl = xfs_file_compat_ioctl,
1415 .fsync = xfs_dir_fsync,