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_da_format.h"
14 #include "xfs_da_btree.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_inode_item.h"
19 #include "xfs_bmap_util.h"
20 #include "xfs_error.h"
22 #include "xfs_dir2_priv.h"
23 #include "xfs_ioctl.h"
24 #include "xfs_trace.h"
26 #include "xfs_icache.h"
28 #include "xfs_iomap.h"
29 #include "xfs_reflink.h"
31 #include <linux/dcache.h>
32 #include <linux/falloc.h>
33 #include <linux/pagevec.h>
34 #include <linux/backing-dev.h>
35 #include <linux/mman.h>
37 static const struct vm_operations_struct xfs_file_vm_ops;
40 xfs_update_prealloc_flags(
42 enum xfs_prealloc_flags flags)
47 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
52 xfs_ilock(ip, XFS_ILOCK_EXCL);
53 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
55 if (!(flags & XFS_PREALLOC_INVISIBLE)) {
56 VFS_I(ip)->i_mode &= ~S_ISUID;
57 if (VFS_I(ip)->i_mode & S_IXGRP)
58 VFS_I(ip)->i_mode &= ~S_ISGID;
59 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
62 if (flags & XFS_PREALLOC_SET)
63 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
64 if (flags & XFS_PREALLOC_CLEAR)
65 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
67 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
68 if (flags & XFS_PREALLOC_SYNC)
69 xfs_trans_set_sync(tp);
70 return xfs_trans_commit(tp);
74 * Fsync operations on directories are much simpler than on regular files,
75 * as there is no file data to flush, and thus also no need for explicit
76 * cache flush operations, and there are no non-transaction metadata updates
77 * on directories either.
86 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
87 struct xfs_mount *mp = ip->i_mount;
90 trace_xfs_dir_fsync(ip);
92 xfs_ilock(ip, XFS_ILOCK_SHARED);
93 if (xfs_ipincount(ip))
94 lsn = ip->i_itemp->ili_last_lsn;
95 xfs_iunlock(ip, XFS_ILOCK_SHARED);
99 return xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
109 struct inode *inode = file->f_mapping->host;
110 struct xfs_inode *ip = XFS_I(inode);
111 struct xfs_mount *mp = ip->i_mount;
116 trace_xfs_file_fsync(ip);
118 error = file_write_and_wait_range(file, start, end);
122 if (XFS_FORCED_SHUTDOWN(mp))
125 xfs_iflags_clear(ip, XFS_ITRUNCATED);
128 * If we have an RT and/or log subvolume we need to make sure to flush
129 * the write cache the device used for file data first. This is to
130 * ensure newly written file data make it to disk before logging the new
131 * inode size in case of an extending write.
133 if (XFS_IS_REALTIME_INODE(ip))
134 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
135 else if (mp->m_logdev_targp != mp->m_ddev_targp)
136 xfs_blkdev_issue_flush(mp->m_ddev_targp);
139 * All metadata updates are logged, which means that we just have to
140 * flush the log up to the latest LSN that touched the inode. If we have
141 * concurrent fsync/fdatasync() calls, we need them to all block on the
142 * log force before we clear the ili_fsync_fields field. This ensures
143 * that we don't get a racing sync operation that does not wait for the
144 * metadata to hit the journal before returning. If we race with
145 * clearing the ili_fsync_fields, then all that will happen is the log
146 * force will do nothing as the lsn will already be on disk. We can't
147 * race with setting ili_fsync_fields because that is done under
148 * XFS_ILOCK_EXCL, and that can't happen because we hold the lock shared
149 * until after the ili_fsync_fields is cleared.
151 xfs_ilock(ip, XFS_ILOCK_SHARED);
152 if (xfs_ipincount(ip)) {
154 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
155 lsn = ip->i_itemp->ili_last_lsn;
159 error = xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
160 ip->i_itemp->ili_fsync_fields = 0;
162 xfs_iunlock(ip, XFS_ILOCK_SHARED);
165 * If we only have a single device, and the log force about was
166 * a no-op we might have to flush the data device cache here.
167 * This can only happen for fdatasync/O_DSYNC if we were overwriting
168 * an already allocated file and thus do not have any metadata to
171 if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
172 mp->m_logdev_targp == mp->m_ddev_targp)
173 xfs_blkdev_issue_flush(mp->m_ddev_targp);
179 xfs_file_dio_aio_read(
183 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
184 size_t count = iov_iter_count(to);
187 trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
190 return 0; /* skip atime */
192 file_accessed(iocb->ki_filp);
194 xfs_ilock(ip, XFS_IOLOCK_SHARED);
195 ret = iomap_dio_rw(iocb, to, &xfs_iomap_ops, NULL);
196 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
201 static noinline ssize_t
206 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
207 size_t count = iov_iter_count(to);
210 trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
213 return 0; /* skip atime */
215 if (iocb->ki_flags & IOCB_NOWAIT) {
216 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
219 xfs_ilock(ip, XFS_IOLOCK_SHARED);
222 ret = dax_iomap_rw(iocb, to, &xfs_iomap_ops);
223 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
225 file_accessed(iocb->ki_filp);
230 xfs_file_buffered_aio_read(
234 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
237 trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
239 if (iocb->ki_flags & IOCB_NOWAIT) {
240 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
243 xfs_ilock(ip, XFS_IOLOCK_SHARED);
245 ret = generic_file_read_iter(iocb, to);
246 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
256 struct inode *inode = file_inode(iocb->ki_filp);
257 struct xfs_mount *mp = XFS_I(inode)->i_mount;
260 XFS_STATS_INC(mp, xs_read_calls);
262 if (XFS_FORCED_SHUTDOWN(mp))
266 ret = xfs_file_dax_read(iocb, to);
267 else if (iocb->ki_flags & IOCB_DIRECT)
268 ret = xfs_file_dio_aio_read(iocb, to);
270 ret = xfs_file_buffered_aio_read(iocb, to);
273 XFS_STATS_ADD(mp, xs_read_bytes, ret);
278 * Common pre-write limit and setup checks.
280 * Called with the iolocked held either shared and exclusive according to
281 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
282 * if called for a direct write beyond i_size.
285 xfs_file_aio_write_checks(
287 struct iov_iter *from,
290 struct file *file = iocb->ki_filp;
291 struct inode *inode = file->f_mapping->host;
292 struct xfs_inode *ip = XFS_I(inode);
294 size_t count = iov_iter_count(from);
295 bool drained_dio = false;
299 error = generic_write_checks(iocb, from);
303 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
308 * For changing security info in file_remove_privs() we need i_rwsem
311 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
312 xfs_iunlock(ip, *iolock);
313 *iolock = XFS_IOLOCK_EXCL;
314 xfs_ilock(ip, *iolock);
318 * If the offset is beyond the size of the file, we need to zero any
319 * blocks that fall between the existing EOF and the start of this
320 * write. If zeroing is needed and we are currently holding the
321 * iolock shared, we need to update it to exclusive which implies
322 * having to redo all checks before.
324 * We need to serialise against EOF updates that occur in IO
325 * completions here. We want to make sure that nobody is changing the
326 * size while we do this check until we have placed an IO barrier (i.e.
327 * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
328 * The spinlock effectively forms a memory barrier once we have the
329 * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
330 * and hence be able to correctly determine if we need to run zeroing.
332 spin_lock(&ip->i_flags_lock);
333 isize = i_size_read(inode);
334 if (iocb->ki_pos > isize) {
335 spin_unlock(&ip->i_flags_lock);
337 if (*iolock == XFS_IOLOCK_SHARED) {
338 xfs_iunlock(ip, *iolock);
339 *iolock = XFS_IOLOCK_EXCL;
340 xfs_ilock(ip, *iolock);
341 iov_iter_reexpand(from, count);
344 * We now have an IO submission barrier in place, but
345 * AIO can do EOF updates during IO completion and hence
346 * we now need to wait for all of them to drain. Non-AIO
347 * DIO will have drained before we are given the
348 * XFS_IOLOCK_EXCL, and so for most cases this wait is a
351 inode_dio_wait(inode);
356 trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
357 error = iomap_zero_range(inode, isize, iocb->ki_pos - isize,
358 NULL, &xfs_iomap_ops);
362 spin_unlock(&ip->i_flags_lock);
365 * Updating the timestamps will grab the ilock again from
366 * xfs_fs_dirty_inode, so we have to call it after dropping the
367 * lock above. Eventually we should look into a way to avoid
368 * the pointless lock roundtrip.
370 if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
371 error = file_update_time(file);
377 * If we're writing the file then make sure to clear the setuid and
378 * setgid bits if the process is not being run by root. This keeps
379 * people from modifying setuid and setgid binaries.
381 if (!IS_NOSEC(inode))
382 return file_remove_privs(file);
387 xfs_dio_write_end_io(
392 struct inode *inode = file_inode(iocb->ki_filp);
393 struct xfs_inode *ip = XFS_I(inode);
394 loff_t offset = iocb->ki_pos;
397 trace_xfs_end_io_direct_write(ip, offset, size);
399 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
406 * Capture amount written on completion as we can't reliably account
407 * for it on submission.
409 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
411 if (flags & IOMAP_DIO_COW) {
412 error = xfs_reflink_end_cow(ip, offset, size);
418 * Unwritten conversion updates the in-core isize after extent
419 * conversion but before updating the on-disk size. Updating isize any
420 * earlier allows a racing dio read to find unwritten extents before
421 * they are converted.
423 if (flags & IOMAP_DIO_UNWRITTEN)
424 return xfs_iomap_write_unwritten(ip, offset, size, true);
427 * We need to update the in-core inode size here so that we don't end up
428 * with the on-disk inode size being outside the in-core inode size. We
429 * have no other method of updating EOF for AIO, so always do it here
432 * We need to lock the test/set EOF update as we can be racing with
433 * other IO completions here to update the EOF. Failing to serialise
434 * here can result in EOF moving backwards and Bad Things Happen when
437 spin_lock(&ip->i_flags_lock);
438 if (offset + size > i_size_read(inode)) {
439 i_size_write(inode, offset + size);
440 spin_unlock(&ip->i_flags_lock);
441 error = xfs_setfilesize(ip, offset, size);
443 spin_unlock(&ip->i_flags_lock);
450 * xfs_file_dio_aio_write - handle direct IO writes
452 * Lock the inode appropriately to prepare for and issue a direct IO write.
453 * By separating it from the buffered write path we remove all the tricky to
454 * follow locking changes and looping.
456 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
457 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
458 * pages are flushed out.
460 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
461 * allowing them to be done in parallel with reads and other direct IO writes.
462 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
463 * needs to do sub-block zeroing and that requires serialisation against other
464 * direct IOs to the same block. In this case we need to serialise the
465 * submission of the unaligned IOs so that we don't get racing block zeroing in
466 * the dio layer. To avoid the problem with aio, we also need to wait for
467 * outstanding IOs to complete so that unwritten extent conversion is completed
468 * before we try to map the overlapping block. This is currently implemented by
469 * hitting it with a big hammer (i.e. inode_dio_wait()).
471 * Returns with locks held indicated by @iolock and errors indicated by
472 * negative return values.
475 xfs_file_dio_aio_write(
477 struct iov_iter *from)
479 struct file *file = iocb->ki_filp;
480 struct address_space *mapping = file->f_mapping;
481 struct inode *inode = mapping->host;
482 struct xfs_inode *ip = XFS_I(inode);
483 struct xfs_mount *mp = ip->i_mount;
485 int unaligned_io = 0;
487 size_t count = iov_iter_count(from);
488 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
489 mp->m_rtdev_targp : mp->m_ddev_targp;
491 /* DIO must be aligned to device logical sector size */
492 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
496 * Don't take the exclusive iolock here unless the I/O is unaligned to
497 * the file system block size. We don't need to consider the EOF
498 * extension case here because xfs_file_aio_write_checks() will relock
499 * the inode as necessary for EOF zeroing cases and fill out the new
500 * inode size as appropriate.
502 if ((iocb->ki_pos & mp->m_blockmask) ||
503 ((iocb->ki_pos + count) & mp->m_blockmask)) {
507 * We can't properly handle unaligned direct I/O to reflink
508 * files yet, as we can't unshare a partial block.
510 if (xfs_is_cow_inode(ip)) {
511 trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
514 iolock = XFS_IOLOCK_EXCL;
516 iolock = XFS_IOLOCK_SHARED;
519 if (iocb->ki_flags & IOCB_NOWAIT) {
520 /* unaligned dio always waits, bail */
523 if (!xfs_ilock_nowait(ip, iolock))
526 xfs_ilock(ip, iolock);
529 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
532 count = iov_iter_count(from);
535 * If we are doing unaligned IO, we can't allow any other overlapping IO
536 * in-flight at the same time or we risk data corruption. Wait for all
537 * other IO to drain before we submit. If the IO is aligned, demote the
538 * iolock if we had to take the exclusive lock in
539 * xfs_file_aio_write_checks() for other reasons.
542 inode_dio_wait(inode);
543 } else if (iolock == XFS_IOLOCK_EXCL) {
544 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
545 iolock = XFS_IOLOCK_SHARED;
548 trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
549 ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io);
552 * If unaligned, this is the only IO in-flight. If it has not yet
553 * completed, wait on it before we release the iolock to prevent
554 * subsequent overlapping IO.
556 if (ret == -EIOCBQUEUED && unaligned_io)
557 inode_dio_wait(inode);
559 xfs_iunlock(ip, iolock);
562 * No fallback to buffered IO on errors for XFS, direct IO will either
563 * complete fully or fail.
565 ASSERT(ret < 0 || ret == count);
569 static noinline ssize_t
572 struct iov_iter *from)
574 struct inode *inode = iocb->ki_filp->f_mapping->host;
575 struct xfs_inode *ip = XFS_I(inode);
576 int iolock = XFS_IOLOCK_EXCL;
577 ssize_t ret, error = 0;
581 if (iocb->ki_flags & IOCB_NOWAIT) {
582 if (!xfs_ilock_nowait(ip, iolock))
585 xfs_ilock(ip, iolock);
588 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
593 count = iov_iter_count(from);
595 trace_xfs_file_dax_write(ip, count, pos);
596 ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
597 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
598 i_size_write(inode, iocb->ki_pos);
599 error = xfs_setfilesize(ip, pos, ret);
602 xfs_iunlock(ip, iolock);
607 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
609 /* Handle various SYNC-type writes */
610 ret = generic_write_sync(iocb, ret);
616 xfs_file_buffered_aio_write(
618 struct iov_iter *from)
620 struct file *file = iocb->ki_filp;
621 struct address_space *mapping = file->f_mapping;
622 struct inode *inode = mapping->host;
623 struct xfs_inode *ip = XFS_I(inode);
628 if (iocb->ki_flags & IOCB_NOWAIT)
632 iolock = XFS_IOLOCK_EXCL;
633 xfs_ilock(ip, iolock);
635 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
639 /* We can write back this queue in page reclaim */
640 current->backing_dev_info = inode_to_bdi(inode);
642 trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
643 ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
644 if (likely(ret >= 0))
648 * If we hit a space limit, try to free up some lingering preallocated
649 * space before returning an error. In the case of ENOSPC, first try to
650 * write back all dirty inodes to free up some of the excess reserved
651 * metadata space. This reduces the chances that the eofblocks scan
652 * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
653 * also behaves as a filter to prevent too many eofblocks scans from
654 * running at the same time.
656 if (ret == -EDQUOT && !enospc) {
657 xfs_iunlock(ip, iolock);
658 enospc = xfs_inode_free_quota_eofblocks(ip);
661 enospc = xfs_inode_free_quota_cowblocks(ip);
665 } else if (ret == -ENOSPC && !enospc) {
666 struct xfs_eofblocks eofb = {0};
669 xfs_flush_inodes(ip->i_mount);
671 xfs_iunlock(ip, iolock);
672 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
673 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
674 xfs_icache_free_cowblocks(ip->i_mount, &eofb);
678 current->backing_dev_info = NULL;
681 xfs_iunlock(ip, iolock);
684 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
685 /* Handle various SYNC-type writes */
686 ret = generic_write_sync(iocb, ret);
694 struct iov_iter *from)
696 struct file *file = iocb->ki_filp;
697 struct address_space *mapping = file->f_mapping;
698 struct inode *inode = mapping->host;
699 struct xfs_inode *ip = XFS_I(inode);
701 size_t ocount = iov_iter_count(from);
703 XFS_STATS_INC(ip->i_mount, xs_write_calls);
708 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
712 return xfs_file_dax_write(iocb, from);
714 if (iocb->ki_flags & IOCB_DIRECT) {
716 * Allow a directio write to fall back to a buffered
717 * write *only* in the case that we're doing a reflink
718 * CoW. In all other directio scenarios we do not
719 * allow an operation to fall back to buffered mode.
721 ret = xfs_file_dio_aio_write(iocb, from);
726 return xfs_file_buffered_aio_write(iocb, from);
733 struct xfs_inode *ip = XFS_I(inode);
735 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
737 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
741 xfs_break_dax_layouts(
747 ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
749 page = dax_layout_busy_page(inode->i_mapping);
754 return ___wait_var_event(&page->_refcount,
755 atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
756 0, 0, xfs_wait_dax_page(inode));
763 enum layout_break_reason reason)
768 ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
774 error = xfs_break_dax_layouts(inode, &retry);
779 error = xfs_break_leased_layouts(inode, iolock, &retry);
785 } while (error == 0 && retry);
790 #define XFS_FALLOC_FL_SUPPORTED \
791 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
792 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
793 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
802 struct inode *inode = file_inode(file);
803 struct xfs_inode *ip = XFS_I(inode);
805 enum xfs_prealloc_flags flags = 0;
806 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
808 bool do_file_insert = false;
810 if (!S_ISREG(inode->i_mode))
812 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
815 xfs_ilock(ip, iolock);
816 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
820 if (mode & FALLOC_FL_PUNCH_HOLE) {
821 error = xfs_free_file_space(ip, offset, len);
824 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
825 unsigned int blksize_mask = i_blocksize(inode) - 1;
827 if (offset & blksize_mask || len & blksize_mask) {
833 * There is no need to overlap collapse range with EOF,
834 * in which case it is effectively a truncate operation
836 if (offset + len >= i_size_read(inode)) {
841 new_size = i_size_read(inode) - len;
843 error = xfs_collapse_file_space(ip, offset, len);
846 } else if (mode & FALLOC_FL_INSERT_RANGE) {
847 unsigned int blksize_mask = i_blocksize(inode) - 1;
848 loff_t isize = i_size_read(inode);
850 if (offset & blksize_mask || len & blksize_mask) {
856 * New inode size must not exceed ->s_maxbytes, accounting for
857 * possible signed overflow.
859 if (inode->i_sb->s_maxbytes - isize < len) {
863 new_size = isize + len;
865 /* Offset should be less than i_size */
866 if (offset >= isize) {
870 do_file_insert = true;
872 flags |= XFS_PREALLOC_SET;
874 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
875 offset + len > i_size_read(inode)) {
876 new_size = offset + len;
877 error = inode_newsize_ok(inode, new_size);
882 if (mode & FALLOC_FL_ZERO_RANGE) {
883 error = xfs_zero_file_space(ip, offset, len);
884 } else if (mode & FALLOC_FL_UNSHARE_RANGE) {
885 error = xfs_reflink_unshare(ip, offset, len);
889 if (!xfs_is_always_cow_inode(ip)) {
890 error = xfs_alloc_file_space(ip, offset, len,
895 * If always_cow mode we can't use preallocations and
896 * thus should not create them.
898 if (xfs_is_always_cow_inode(ip)) {
903 error = xfs_alloc_file_space(ip, offset, len,
910 if (file->f_flags & O_DSYNC)
911 flags |= XFS_PREALLOC_SYNC;
913 error = xfs_update_prealloc_flags(ip, flags);
917 /* Change file size if needed */
921 iattr.ia_valid = ATTR_SIZE;
922 iattr.ia_size = new_size;
923 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
929 * Perform hole insertion now that the file size has been
930 * updated so that if we crash during the operation we don't
931 * leave shifted extents past EOF and hence losing access to
932 * the data that is contained within them.
935 error = xfs_insert_file_space(ip, offset, len);
938 xfs_iunlock(ip, iolock);
944 xfs_file_remap_range(
945 struct file *file_in,
947 struct file *file_out,
950 unsigned int remap_flags)
952 struct inode *inode_in = file_inode(file_in);
953 struct xfs_inode *src = XFS_I(inode_in);
954 struct inode *inode_out = file_inode(file_out);
955 struct xfs_inode *dest = XFS_I(inode_out);
956 struct xfs_mount *mp = src->i_mount;
958 xfs_extlen_t cowextsize;
961 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
964 if (!xfs_sb_version_hasreflink(&mp->m_sb))
967 if (XFS_FORCED_SHUTDOWN(mp))
970 /* Prepare and then clone file data. */
971 ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
973 if (ret < 0 || len == 0)
976 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
978 ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
984 * Carry the cowextsize hint from src to dest if we're sharing the
985 * entire source file to the entire destination file, the source file
986 * has a cowextsize hint, and the destination file does not.
989 if (pos_in == 0 && len == i_size_read(inode_in) &&
990 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
991 pos_out == 0 && len >= i_size_read(inode_out) &&
992 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
993 cowextsize = src->i_d.di_cowextsize;
995 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
999 xfs_reflink_remap_unlock(file_in, file_out);
1001 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1002 return remapped > 0 ? remapped : ret;
1007 struct inode *inode,
1010 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1012 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1014 file->f_mode |= FMODE_NOWAIT;
1020 struct inode *inode,
1023 struct xfs_inode *ip = XFS_I(inode);
1027 error = xfs_file_open(inode, file);
1032 * If there are any blocks, read-ahead block 0 as we're almost
1033 * certain to have the next operation be a read there.
1035 mode = xfs_ilock_data_map_shared(ip);
1036 if (ip->i_d.di_nextents > 0)
1037 error = xfs_dir3_data_readahead(ip, 0, -1);
1038 xfs_iunlock(ip, mode);
1044 struct inode *inode,
1047 return xfs_release(XFS_I(inode));
1053 struct dir_context *ctx)
1055 struct inode *inode = file_inode(file);
1056 xfs_inode_t *ip = XFS_I(inode);
1060 * The Linux API doesn't pass down the total size of the buffer
1061 * we read into down to the filesystem. With the filldir concept
1062 * it's not needed for correct information, but the XFS dir2 leaf
1063 * code wants an estimate of the buffer size to calculate it's
1064 * readahead window and size the buffers used for mapping to
1067 * Try to give it an estimate that's good enough, maybe at some
1068 * point we can change the ->readdir prototype to include the
1069 * buffer size. For now we use the current glibc buffer size.
1071 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
1073 return xfs_readdir(NULL, ip, ctx, bufsize);
1082 struct inode *inode = file->f_mapping->host;
1084 if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
1089 return generic_file_llseek(file, offset, whence);
1091 offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
1094 offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
1100 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1104 * Locking for serialisation of IO during page faults. This results in a lock
1108 * sb_start_pagefault(vfs, freeze)
1109 * i_mmaplock (XFS - truncate serialisation)
1111 * i_lock (XFS - extent map serialisation)
1114 __xfs_filemap_fault(
1115 struct vm_fault *vmf,
1116 enum page_entry_size pe_size,
1119 struct inode *inode = file_inode(vmf->vma->vm_file);
1120 struct xfs_inode *ip = XFS_I(inode);
1123 trace_xfs_filemap_fault(ip, pe_size, write_fault);
1126 sb_start_pagefault(inode->i_sb);
1127 file_update_time(vmf->vma->vm_file);
1130 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1131 if (IS_DAX(inode)) {
1134 ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL, &xfs_iomap_ops);
1135 if (ret & VM_FAULT_NEEDDSYNC)
1136 ret = dax_finish_sync_fault(vmf, pe_size, pfn);
1139 ret = iomap_page_mkwrite(vmf, &xfs_iomap_ops);
1141 ret = filemap_fault(vmf);
1143 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1146 sb_end_pagefault(inode->i_sb);
1152 struct vm_fault *vmf)
1154 /* DAX can shortcut the normal fault path on write faults! */
1155 return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
1156 IS_DAX(file_inode(vmf->vma->vm_file)) &&
1157 (vmf->flags & FAULT_FLAG_WRITE));
1161 xfs_filemap_huge_fault(
1162 struct vm_fault *vmf,
1163 enum page_entry_size pe_size)
1165 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1166 return VM_FAULT_FALLBACK;
1168 /* DAX can shortcut the normal fault path on write faults! */
1169 return __xfs_filemap_fault(vmf, pe_size,
1170 (vmf->flags & FAULT_FLAG_WRITE));
1174 xfs_filemap_page_mkwrite(
1175 struct vm_fault *vmf)
1177 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1181 * pfn_mkwrite was originally intended to ensure we capture time stamp updates
1182 * on write faults. In reality, it needs to serialise against truncate and
1183 * prepare memory for writing so handle is as standard write fault.
1186 xfs_filemap_pfn_mkwrite(
1187 struct vm_fault *vmf)
1190 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1193 static const struct vm_operations_struct xfs_file_vm_ops = {
1194 .fault = xfs_filemap_fault,
1195 .huge_fault = xfs_filemap_huge_fault,
1196 .map_pages = filemap_map_pages,
1197 .page_mkwrite = xfs_filemap_page_mkwrite,
1198 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
1204 struct vm_area_struct *vma)
1207 * We don't support synchronous mappings for non-DAX files. At least
1208 * until someone comes with a sensible use case.
1210 if (!IS_DAX(file_inode(filp)) && (vma->vm_flags & VM_SYNC))
1213 file_accessed(filp);
1214 vma->vm_ops = &xfs_file_vm_ops;
1215 if (IS_DAX(file_inode(filp)))
1216 vma->vm_flags |= VM_HUGEPAGE;
1220 const struct file_operations xfs_file_operations = {
1221 .llseek = xfs_file_llseek,
1222 .read_iter = xfs_file_read_iter,
1223 .write_iter = xfs_file_write_iter,
1224 .splice_read = generic_file_splice_read,
1225 .splice_write = iter_file_splice_write,
1226 .iopoll = iomap_dio_iopoll,
1227 .unlocked_ioctl = xfs_file_ioctl,
1228 #ifdef CONFIG_COMPAT
1229 .compat_ioctl = xfs_file_compat_ioctl,
1231 .mmap = xfs_file_mmap,
1232 .mmap_supported_flags = MAP_SYNC,
1233 .open = xfs_file_open,
1234 .release = xfs_file_release,
1235 .fsync = xfs_file_fsync,
1236 .get_unmapped_area = thp_get_unmapped_area,
1237 .fallocate = xfs_file_fallocate,
1238 .remap_file_range = xfs_file_remap_range,
1241 const struct file_operations xfs_dir_file_operations = {
1242 .open = xfs_dir_open,
1243 .read = generic_read_dir,
1244 .iterate_shared = xfs_file_readdir,
1245 .llseek = generic_file_llseek,
1246 .unlocked_ioctl = xfs_file_ioctl,
1247 #ifdef CONFIG_COMPAT
1248 .compat_ioctl = xfs_file_compat_ioctl,
1250 .fsync = xfs_dir_fsync,