blob: a7ceae90110eded646f13acf4314131574d46a69 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +00007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110013#include "xfs_da_format.h"
14#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110016#include "xfs_trans.h"
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000017#include "xfs_inode_item.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000018#include "xfs_bmap.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100019#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100021#include "xfs_dir2.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100022#include "xfs_dir2_priv.h"
Christoph Hellwigddcd8562008-12-03 07:55:34 -050023#include "xfs_ioctl.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000024#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110025#include "xfs_log.h"
Brian Fosterdc06f3982014-07-24 19:49:28 +100026#include "xfs_icache.h"
Christoph Hellwig781355c2015-02-16 11:59:50 +110027#include "xfs_pnfs.h"
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +100028#include "xfs_iomap.h"
Darrick J. Wong0613f162016-10-03 09:11:37 -070029#include "xfs_reflink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <linux/dcache.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010032#include <linux/falloc.h>
Jeff Liud126d432012-08-21 17:11:57 +080033#include <linux/pagevec.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040034#include <linux/backing-dev.h>
Christoph Hellwiga39e5962017-11-01 16:36:47 +010035#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +040037static const struct vm_operations_struct xfs_file_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Christoph Hellwig8add71c2015-02-02 09:53:56 +110039int
40xfs_update_prealloc_flags(
41 struct xfs_inode *ip,
42 enum xfs_prealloc_flags flags)
43{
44 struct xfs_trans *tp;
45 int error;
46
Christoph Hellwig253f4912016-04-06 09:19:55 +100047 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
48 0, 0, 0, &tp);
49 if (error)
Christoph Hellwig8add71c2015-02-02 09:53:56 +110050 return error;
Christoph Hellwig8add71c2015-02-02 09:53:56 +110051
52 xfs_ilock(ip, XFS_ILOCK_EXCL);
53 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
54
55 if (!(flags & XFS_PREALLOC_INVISIBLE)) {
Dave Chinnerc19b3b052016-02-09 16:54:58 +110056 VFS_I(ip)->i_mode &= ~S_ISUID;
57 if (VFS_I(ip)->i_mode & S_IXGRP)
58 VFS_I(ip)->i_mode &= ~S_ISGID;
Christoph Hellwig8add71c2015-02-02 09:53:56 +110059 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
60 }
61
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;
66
67 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
68 if (flags & XFS_PREALLOC_SYNC)
69 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +100070 return xfs_trans_commit(tp);
Christoph Hellwig8add71c2015-02-02 09:53:56 +110071}
72
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +000073/*
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.
78 */
79STATIC int
80xfs_dir_fsync(
81 struct file *file,
82 loff_t start,
83 loff_t end,
84 int datasync)
85{
86 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
87 struct xfs_mount *mp = ip->i_mount;
88 xfs_lsn_t lsn = 0;
89
90 trace_xfs_dir_fsync(ip);
91
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);
96
97 if (!lsn)
98 return 0;
Christoph Hellwig656de4f2018-03-13 23:15:28 -070099 return xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +0000100}
101
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000102STATIC int
103xfs_file_fsync(
104 struct file *file,
Josef Bacik02c24a82011-07-16 20:44:56 -0400105 loff_t start,
106 loff_t end,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000107 int datasync)
108{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200109 struct inode *inode = file->f_mapping->host;
110 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000111 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000112 int error = 0;
113 int log_flushed = 0;
Christoph Hellwigb1037052011-09-19 14:55:51 +0000114 xfs_lsn_t lsn = 0;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000115
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000116 trace_xfs_file_fsync(ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000117
Jeff Layton1b180272017-07-06 07:02:30 -0400118 error = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -0400119 if (error)
120 return error;
121
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000122 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000123 return -EIO;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000124
125 xfs_iflags_clear(ip, XFS_ITRUNCATED);
126
Dave Chinner2291dab2016-12-09 16:49:54 +1100127 /*
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.
132 */
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);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000137
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000138 /*
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100139 * 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.
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000150 */
151 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000152 if (xfs_ipincount(ip)) {
153 if (!datasync ||
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100154 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000155 lsn = ip->i_itemp->ili_last_lsn;
156 }
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000157
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100158 if (lsn) {
Christoph Hellwig656de4f2018-03-13 23:15:28 -0700159 error = xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100160 ip->i_itemp->ili_fsync_fields = 0;
161 }
162 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigb1037052011-09-19 14:55:51 +0000163
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000164 /*
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
169 * commit.
170 */
Dave Chinner2291dab2016-12-09 16:49:54 +1100171 if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
172 mp->m_logdev_targp == mp->m_ddev_targp)
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000173 xfs_blkdev_issue_flush(mp->m_ddev_targp);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000174
Dave Chinner24513372014-06-25 14:58:08 +1000175 return error;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000176}
177
Christoph Hellwig00258e32010-02-15 09:44:47 +0000178STATIC ssize_t
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000179xfs_file_dio_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000180 struct kiocb *iocb,
Al Virob4f5d2c2014-04-02 14:37:59 -0400181 struct iov_iter *to)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000182{
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100183 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000184 size_t count = iov_iter_count(to);
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100185 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000186
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000187 trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000188
Christoph Hellwigf1285ff2016-07-20 11:36:57 +1000189 if (!count)
190 return 0; /* skip atime */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000191
Christoph Hellwiga447d7c2016-10-03 09:47:34 +1100192 file_accessed(iocb->ki_filp);
193
Christoph Hellwig65523212016-11-30 14:33:25 +1100194 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100195 ret = iomap_dio_rw(iocb, to, &xfs_iomap_ops, NULL);
Christoph Hellwig65523212016-11-30 14:33:25 +1100196 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100197
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000198 return ret;
199}
200
Arnd Bergmannf021bd02016-07-22 09:50:55 +1000201static noinline ssize_t
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000202xfs_file_dax_read(
203 struct kiocb *iocb,
204 struct iov_iter *to)
205{
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000206 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000207 size_t count = iov_iter_count(to);
208 ssize_t ret = 0;
209
210 trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
211
212 if (!count)
213 return 0; /* skip atime */
214
Christoph Hellwig942491c2017-10-23 18:31:50 -0700215 if (iocb->ki_flags & IOCB_NOWAIT) {
216 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500217 return -EAGAIN;
Christoph Hellwig942491c2017-10-23 18:31:50 -0700218 } else {
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500219 xfs_ilock(ip, XFS_IOLOCK_SHARED);
220 }
Christoph Hellwig942491c2017-10-23 18:31:50 -0700221
Ross Zwisler11c59c92016-11-08 11:32:46 +1100222 ret = dax_iomap_rw(iocb, to, &xfs_iomap_ops);
Christoph Hellwig65523212016-11-30 14:33:25 +1100223 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000224
Christoph Hellwigf1285ff2016-07-20 11:36:57 +1000225 file_accessed(iocb->ki_filp);
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000226 return ret;
227}
228
229STATIC ssize_t
230xfs_file_buffered_aio_read(
231 struct kiocb *iocb,
232 struct iov_iter *to)
233{
234 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
235 ssize_t ret;
236
237 trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
238
Christoph Hellwig942491c2017-10-23 18:31:50 -0700239 if (iocb->ki_flags & IOCB_NOWAIT) {
240 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
Christoph Hellwig91f99432017-08-29 16:13:20 +0200241 return -EAGAIN;
Christoph Hellwig942491c2017-10-23 18:31:50 -0700242 } else {
Christoph Hellwig91f99432017-08-29 16:13:20 +0200243 xfs_ilock(ip, XFS_IOLOCK_SHARED);
244 }
Al Virob4f5d2c2014-04-02 14:37:59 -0400245 ret = generic_file_read_iter(iocb, to);
Christoph Hellwig65523212016-11-30 14:33:25 +1100246 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000247
248 return ret;
249}
250
251STATIC ssize_t
252xfs_file_read_iter(
253 struct kiocb *iocb,
254 struct iov_iter *to)
255{
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000256 struct inode *inode = file_inode(iocb->ki_filp);
257 struct xfs_mount *mp = XFS_I(inode)->i_mount;
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000258 ssize_t ret = 0;
259
260 XFS_STATS_INC(mp, xs_read_calls);
261
262 if (XFS_FORCED_SHUTDOWN(mp))
263 return -EIO;
264
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000265 if (IS_DAX(inode))
266 ret = xfs_file_dax_read(iocb, to);
267 else if (iocb->ki_flags & IOCB_DIRECT)
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000268 ret = xfs_file_dio_aio_read(iocb, to);
269 else
270 ret = xfs_file_buffered_aio_read(iocb, to);
271
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000272 if (ret > 0)
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100273 XFS_STATS_ADD(mp, xs_read_bytes, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000274 return ret;
275}
276
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100277/*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100278 * Common pre-write limit and setup checks.
279 *
Christoph Hellwig5bf1f262011-12-18 20:00:13 +0000280 * 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.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100283 */
284STATIC ssize_t
285xfs_file_aio_write_checks(
Al Viro99733fa2015-04-07 14:25:18 -0400286 struct kiocb *iocb,
287 struct iov_iter *from,
Dave Chinner4d8d1582011-01-11 10:23:42 +1100288 int *iolock)
289{
Al Viro99733fa2015-04-07 14:25:18 -0400290 struct file *file = iocb->ki_filp;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100291 struct inode *inode = file->f_mapping->host;
292 struct xfs_inode *ip = XFS_I(inode);
Al Viro3309dd02015-04-09 12:55:47 -0400293 ssize_t error = 0;
Al Viro99733fa2015-04-07 14:25:18 -0400294 size_t count = iov_iter_count(from);
Brian Foster3136e8b2015-10-12 16:02:05 +1100295 bool drained_dio = false;
Christoph Hellwigf5c547172018-03-13 23:15:32 -0700296 loff_t isize;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100297
Dave Chinner7271d242011-08-25 07:17:02 +0000298restart:
Al Viro3309dd02015-04-09 12:55:47 -0400299 error = generic_write_checks(iocb, from);
300 if (error <= 0)
Dave Chinner4d8d1582011-01-11 10:23:42 +1100301 return error;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100302
Dan Williams69eb5fa2018-03-20 14:42:38 -0700303 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100304 if (error)
305 return error;
306
Christoph Hellwig65523212016-11-30 14:33:25 +1100307 /*
308 * For changing security info in file_remove_privs() we need i_rwsem
309 * exclusively.
310 */
Jan Karaa6de82c2015-05-21 16:05:56 +0200311 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100312 xfs_iunlock(ip, *iolock);
Jan Karaa6de82c2015-05-21 16:05:56 +0200313 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig65523212016-11-30 14:33:25 +1100314 xfs_ilock(ip, *iolock);
Jan Karaa6de82c2015-05-21 16:05:56 +0200315 goto restart;
316 }
Dave Chinner4d8d1582011-01-11 10:23:42 +1100317 /*
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
Christoph Hellwig2813d682011-12-18 20:00:12 +0000320 * write. If zeroing is needed and we are currently holding the
Christoph Hellwig467f7892012-03-27 10:34:47 -0400321 * iolock shared, we need to update it to exclusive which implies
322 * having to redo all checks before.
Dave Chinnerb9d59842015-04-16 22:03:07 +1000323 *
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.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100331 */
Dave Chinnerb9d59842015-04-16 22:03:07 +1000332 spin_lock(&ip->i_flags_lock);
Christoph Hellwigf5c547172018-03-13 23:15:32 -0700333 isize = i_size_read(inode);
334 if (iocb->ki_pos > isize) {
Dave Chinnerb9d59842015-04-16 22:03:07 +1000335 spin_unlock(&ip->i_flags_lock);
Brian Foster3136e8b2015-10-12 16:02:05 +1100336 if (!drained_dio) {
337 if (*iolock == XFS_IOLOCK_SHARED) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100338 xfs_iunlock(ip, *iolock);
Brian Foster3136e8b2015-10-12 16:02:05 +1100339 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig65523212016-11-30 14:33:25 +1100340 xfs_ilock(ip, *iolock);
Brian Foster3136e8b2015-10-12 16:02:05 +1100341 iov_iter_reexpand(from, count);
342 }
Dave Chinner40c63fb2015-04-16 22:03:17 +1000343 /*
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
349 * no-op.
350 */
351 inode_dio_wait(inode);
Brian Foster3136e8b2015-10-12 16:02:05 +1100352 drained_dio = true;
Dave Chinner7271d242011-08-25 07:17:02 +0000353 goto restart;
354 }
Christoph Hellwigf5c547172018-03-13 23:15:32 -0700355
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);
Christoph Hellwig467f7892012-03-27 10:34:47 -0400359 if (error)
360 return error;
Dave Chinnerb9d59842015-04-16 22:03:07 +1000361 } else
362 spin_unlock(&ip->i_flags_lock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100363
364 /*
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000365 * 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.
369 */
Josef Bacikc3b2da32012-03-26 09:59:21 -0400370 if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
371 error = file_update_time(file);
372 if (error)
373 return error;
374 }
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000375
376 /*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100377 * 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.
380 */
Jan Karaa6de82c2015-05-21 16:05:56 +0200381 if (!IS_NOSEC(inode))
382 return file_remove_privs(file);
383 return 0;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100384}
385
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100386static int
387xfs_dio_write_end_io(
388 struct kiocb *iocb,
389 ssize_t size,
390 unsigned flags)
391{
392 struct inode *inode = file_inode(iocb->ki_filp);
393 struct xfs_inode *ip = XFS_I(inode);
394 loff_t offset = iocb->ki_pos;
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100395 int error = 0;
396
397 trace_xfs_end_io_direct_write(ip, offset, size);
398
399 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
400 return -EIO;
401
402 if (size <= 0)
403 return size;
404
Dave Chinnered5c3e62018-05-02 12:54:52 -0700405 /*
406 * Capture amount written on completion as we can't reliably account
407 * for it on submission.
408 */
409 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
410
Eryu Guanee70daa2017-09-21 11:26:18 -0700411 if (flags & IOMAP_DIO_COW) {
412 error = xfs_reflink_end_cow(ip, offset, size);
413 if (error)
414 return error;
415 }
416
417 /*
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.
422 */
423 if (flags & IOMAP_DIO_UNWRITTEN)
424 return xfs_iomap_write_unwritten(ip, offset, size, true);
425
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100426 /*
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
430 * if necessary.
431 *
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
435 * that occurs.
436 */
437 spin_lock(&ip->i_flags_lock);
438 if (offset + size > i_size_read(inode)) {
439 i_size_write(inode, offset + size);
Eryu Guanee70daa2017-09-21 11:26:18 -0700440 spin_unlock(&ip->i_flags_lock);
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100441 error = xfs_setfilesize(ip, offset, size);
Eryu Guanee70daa2017-09-21 11:26:18 -0700442 } else {
443 spin_unlock(&ip->i_flags_lock);
444 }
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100445
446 return error;
447}
448
Dave Chinner4d8d1582011-01-11 10:23:42 +1100449/*
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100450 * xfs_file_dio_aio_write - handle direct IO writes
451 *
452 * Lock the inode appropriately to prepare for and issue a direct IO write.
Dave Chinnereda77982011-01-11 10:22:40 +1100453 * By separating it from the buffered write path we remove all the tricky to
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100454 * follow locking changes and looping.
455 *
Dave Chinnereda77982011-01-11 10:22:40 +1100456 * 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.
459 *
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
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000469 * hitting it with a big hammer (i.e. inode_dio_wait()).
Dave Chinnereda77982011-01-11 10:22:40 +1100470 *
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100471 * Returns with locks held indicated by @iolock and errors indicated by
472 * negative return values.
473 */
474STATIC ssize_t
475xfs_file_dio_aio_write(
476 struct kiocb *iocb,
Al Virob3188912014-04-02 07:06:30 -0400477 struct iov_iter *from)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100478{
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;
484 ssize_t ret = 0;
Dave Chinnereda77982011-01-11 10:22:40 +1100485 int unaligned_io = 0;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000486 int iolock;
Al Virob3188912014-04-02 07:06:30 -0400487 size_t count = iov_iter_count(from);
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100488 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100489 mp->m_rtdev_targp : mp->m_ddev_targp;
490
Eric Sandeen7c71ee72014-01-21 16:46:23 -0600491 /* DIO must be aligned to device logical sector size */
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000492 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000493 return -EINVAL;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100494
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100495 /*
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.
501 */
Christoph Hellwig13712712016-04-07 08:51:57 -0700502 if ((iocb->ki_pos & mp->m_blockmask) ||
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100503 ((iocb->ki_pos + count) & mp->m_blockmask)) {
Dave Chinnereda77982011-01-11 10:22:40 +1100504 unaligned_io = 1;
Christoph Hellwig54a4ef82017-02-06 13:00:54 -0800505
506 /*
507 * We can't properly handle unaligned direct I/O to reflink
508 * files yet, as we can't unshare a partial block.
509 */
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800510 if (xfs_is_cow_inode(ip)) {
Christoph Hellwig54a4ef82017-02-06 13:00:54 -0800511 trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
512 return -EREMCHG;
513 }
Christoph Hellwigd0606462011-12-18 20:00:14 +0000514 iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100515 } else {
Christoph Hellwigd0606462011-12-18 20:00:14 +0000516 iolock = XFS_IOLOCK_SHARED;
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000517 }
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100518
Christoph Hellwig942491c2017-10-23 18:31:50 -0700519 if (iocb->ki_flags & IOCB_NOWAIT) {
520 if (!xfs_ilock_nowait(ip, iolock))
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500521 return -EAGAIN;
Christoph Hellwig942491c2017-10-23 18:31:50 -0700522 } else {
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500523 xfs_ilock(ip, iolock);
524 }
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100525
Al Viro99733fa2015-04-07 14:25:18 -0400526 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100527 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000528 goto out;
Al Viro99733fa2015-04-07 14:25:18 -0400529 count = iov_iter_count(from);
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100530
Dave Chinnereda77982011-01-11 10:22:40 +1100531 /*
Brian Foster2032a8a2019-03-25 17:01:45 -0700532 * If we are doing unaligned IO, we can't allow any other overlapping IO
533 * in-flight at the same time or we risk data corruption. Wait for all
534 * other IO to drain before we submit. If the IO is aligned, demote the
535 * iolock if we had to take the exclusive lock in
536 * xfs_file_aio_write_checks() for other reasons.
Dave Chinnereda77982011-01-11 10:22:40 +1100537 */
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500538 if (unaligned_io) {
Brian Foster2032a8a2019-03-25 17:01:45 -0700539 /* unaligned dio always waits, bail */
540 if (iocb->ki_flags & IOCB_NOWAIT)
541 return -EAGAIN;
542 inode_dio_wait(inode);
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500543 } else if (iolock == XFS_IOLOCK_EXCL) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100544 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000545 iolock = XFS_IOLOCK_SHARED;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100546 }
547
Christoph Hellwig3176c3e2016-07-20 11:31:42 +1000548 trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100549 ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io);
Brian Foster2032a8a2019-03-25 17:01:45 -0700550
551 /*
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.
555 */
556 if (ret == -EIOCBQUEUED && unaligned_io)
557 inode_dio_wait(inode);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000558out:
Christoph Hellwig65523212016-11-30 14:33:25 +1100559 xfs_iunlock(ip, iolock);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000560
Dave Chinner6b698ed2015-06-04 09:18:53 +1000561 /*
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000562 * No fallback to buffered IO on errors for XFS, direct IO will either
563 * complete fully or fail.
Dave Chinner6b698ed2015-06-04 09:18:53 +1000564 */
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000565 ASSERT(ret < 0 || ret == count);
566 return ret;
567}
568
Arnd Bergmannf021bd02016-07-22 09:50:55 +1000569static noinline ssize_t
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000570xfs_file_dax_write(
571 struct kiocb *iocb,
572 struct iov_iter *from)
573{
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000574 struct inode *inode = iocb->ki_filp->f_mapping->host;
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000575 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig17879e82016-09-19 11:24:50 +1000576 int iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000577 ssize_t ret, error = 0;
578 size_t count;
579 loff_t pos;
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000580
Christoph Hellwig942491c2017-10-23 18:31:50 -0700581 if (iocb->ki_flags & IOCB_NOWAIT) {
582 if (!xfs_ilock_nowait(ip, iolock))
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500583 return -EAGAIN;
Christoph Hellwig942491c2017-10-23 18:31:50 -0700584 } else {
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500585 xfs_ilock(ip, iolock);
586 }
587
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000588 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
589 if (ret)
590 goto out;
591
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000592 pos = iocb->ki_pos;
593 count = iov_iter_count(from);
Dave Chinner8b2180b2016-08-17 08:31:33 +1000594
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000595 trace_xfs_file_dax_write(ip, count, pos);
Ross Zwisler11c59c92016-11-08 11:32:46 +1100596 ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000597 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);
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000600 }
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000601out:
Christoph Hellwig65523212016-11-30 14:33:25 +1100602 xfs_iunlock(ip, iolock);
Dave Chinnered5c3e62018-05-02 12:54:52 -0700603 if (error)
604 return error;
605
606 if (ret > 0) {
607 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
608
609 /* Handle various SYNC-type writes */
610 ret = generic_write_sync(iocb, ret);
611 }
612 return ret;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100613}
614
Christoph Hellwig00258e32010-02-15 09:44:47 +0000615STATIC ssize_t
Dave Chinner637bbc72011-01-11 10:17:30 +1100616xfs_file_buffered_aio_write(
617 struct kiocb *iocb,
Al Virob3188912014-04-02 07:06:30 -0400618 struct iov_iter *from)
Dave Chinner637bbc72011-01-11 10:17:30 +1100619{
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);
624 ssize_t ret;
625 int enospc = 0;
Brian Fosterc3155092017-01-27 23:22:56 -0800626 int iolock;
Dave Chinner637bbc72011-01-11 10:17:30 +1100627
Christoph Hellwig91f99432017-08-29 16:13:20 +0200628 if (iocb->ki_flags & IOCB_NOWAIT)
629 return -EOPNOTSUPP;
630
Brian Fosterc3155092017-01-27 23:22:56 -0800631write_retry:
632 iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig65523212016-11-30 14:33:25 +1100633 xfs_ilock(ip, iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100634
Al Viro99733fa2015-04-07 14:25:18 -0400635 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100636 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000637 goto out;
Dave Chinner637bbc72011-01-11 10:17:30 +1100638
639 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100640 current->backing_dev_info = inode_to_bdi(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100641
Christoph Hellwig3176c3e2016-07-20 11:31:42 +1000642 trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000643 ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
Al Viro0a64bc22014-02-11 22:25:22 -0500644 if (likely(ret >= 0))
Al Viro99733fa2015-04-07 14:25:18 -0400645 iocb->ki_pos += ret;
Brian Fosterdc06f3982014-07-24 19:49:28 +1000646
Dave Chinner637bbc72011-01-11 10:17:30 +1100647 /*
Brian Fosterdc06f3982014-07-24 19:49:28 +1000648 * 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.
Dave Chinner637bbc72011-01-11 10:17:30 +1100655 */
Brian Fosterdc06f3982014-07-24 19:49:28 +1000656 if (ret == -EDQUOT && !enospc) {
Brian Fosterc3155092017-01-27 23:22:56 -0800657 xfs_iunlock(ip, iolock);
Brian Fosterdc06f3982014-07-24 19:49:28 +1000658 enospc = xfs_inode_free_quota_eofblocks(ip);
659 if (enospc)
660 goto write_retry;
Darrick J. Wong83104d42016-10-03 09:11:46 -0700661 enospc = xfs_inode_free_quota_cowblocks(ip);
662 if (enospc)
663 goto write_retry;
Brian Fosterc3155092017-01-27 23:22:56 -0800664 iolock = 0;
Brian Fosterdc06f3982014-07-24 19:49:28 +1000665 } else if (ret == -ENOSPC && !enospc) {
666 struct xfs_eofblocks eofb = {0};
667
Dave Chinner637bbc72011-01-11 10:17:30 +1100668 enospc = 1;
Dave Chinner9aa05002012-10-08 21:56:04 +1100669 xfs_flush_inodes(ip->i_mount);
Brian Fosterc3155092017-01-27 23:22:56 -0800670
671 xfs_iunlock(ip, iolock);
Brian Fosterdc06f3982014-07-24 19:49:28 +1000672 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
673 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
Brian Fostercf2cb782017-06-20 14:36:19 -0700674 xfs_icache_free_cowblocks(ip->i_mount, &eofb);
Dave Chinner9aa05002012-10-08 21:56:04 +1100675 goto write_retry;
Dave Chinner637bbc72011-01-11 10:17:30 +1100676 }
Christoph Hellwigd0606462011-12-18 20:00:14 +0000677
Dave Chinner637bbc72011-01-11 10:17:30 +1100678 current->backing_dev_info = NULL;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000679out:
Brian Fosterc3155092017-01-27 23:22:56 -0800680 if (iolock)
681 xfs_iunlock(ip, iolock);
Dave Chinnered5c3e62018-05-02 12:54:52 -0700682
683 if (ret > 0) {
684 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
685 /* Handle various SYNC-type writes */
686 ret = generic_write_sync(iocb, ret);
687 }
Dave Chinner637bbc72011-01-11 10:17:30 +1100688 return ret;
689}
690
691STATIC ssize_t
Al Virobf97f3bc2014-04-03 14:20:23 -0400692xfs_file_write_iter(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000693 struct kiocb *iocb,
Al Virobf97f3bc2014-04-03 14:20:23 -0400694 struct iov_iter *from)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000695{
696 struct file *file = iocb->ki_filp;
697 struct address_space *mapping = file->f_mapping;
698 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000699 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100700 ssize_t ret;
Al Virobf97f3bc2014-04-03 14:20:23 -0400701 size_t ocount = iov_iter_count(from);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000702
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100703 XFS_STATS_INC(ip->i_mount, xs_write_calls);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000704
Dave Chinner637bbc72011-01-11 10:17:30 +1100705 if (ocount == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000706 return 0;
707
Al Virobf97f3bc2014-04-03 14:20:23 -0400708 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
709 return -EIO;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000710
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000711 if (IS_DAX(inode))
Dave Chinnered5c3e62018-05-02 12:54:52 -0700712 return xfs_file_dax_write(iocb, from);
713
714 if (iocb->ki_flags & IOCB_DIRECT) {
Darrick J. Wong0613f162016-10-03 09:11:37 -0700715 /*
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.
720 */
Al Virobf97f3bc2014-04-03 14:20:23 -0400721 ret = xfs_file_dio_aio_write(iocb, from);
Dave Chinnered5c3e62018-05-02 12:54:52 -0700722 if (ret != -EREMCHG)
723 return ret;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700724 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000725
Dave Chinnered5c3e62018-05-02 12:54:52 -0700726 return xfs_file_buffered_aio_write(iocb, from);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000727}
728
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700729static void
730xfs_wait_dax_page(
Dave Jiange25ff832018-08-10 08:48:18 -0700731 struct inode *inode)
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700732{
733 struct xfs_inode *ip = XFS_I(inode);
734
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700735 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
736 schedule();
737 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
738}
739
740static int
741xfs_break_dax_layouts(
742 struct inode *inode,
Dave Jiange25ff832018-08-10 08:48:18 -0700743 bool *retry)
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700744{
745 struct page *page;
746
747 ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
748
749 page = dax_layout_busy_page(inode->i_mapping);
750 if (!page)
751 return 0;
752
Dave Jiange25ff832018-08-10 08:48:18 -0700753 *retry = true;
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700754 return ___wait_var_event(&page->_refcount,
755 atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
Dave Jiange25ff832018-08-10 08:48:18 -0700756 0, 0, xfs_wait_dax_page(inode));
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700757}
758
Dan Williams69eb5fa2018-03-20 14:42:38 -0700759int
760xfs_break_layouts(
761 struct inode *inode,
762 uint *iolock,
763 enum layout_break_reason reason)
764{
765 bool retry;
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700766 int error;
Dan Williams69eb5fa2018-03-20 14:42:38 -0700767
768 ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
769
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700770 do {
771 retry = false;
772 switch (reason) {
773 case BREAK_UNMAP:
Eric Sandeena4722a62018-07-11 22:26:36 -0700774 error = xfs_break_dax_layouts(inode, &retry);
Dan Williamsd6dc57e2018-05-09 15:47:49 -0700775 if (error || retry)
776 break;
777 /* fall through */
778 case BREAK_WRITE:
779 error = xfs_break_leased_layouts(inode, iolock, &retry);
780 break;
781 default:
782 WARN_ON_ONCE(1);
783 error = -EINVAL;
784 }
785 } while (error == 0 && retry);
786
787 return error;
Dan Williams69eb5fa2018-03-20 14:42:38 -0700788}
789
Namjae Jeona904b1c2015-03-25 15:08:56 +1100790#define XFS_FALLOC_FL_SUPPORTED \
791 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
792 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
Darrick J. Wong98cc2db2016-10-03 09:11:43 -0700793 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
Namjae Jeona904b1c2015-03-25 15:08:56 +1100794
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100795STATIC long
796xfs_file_fallocate(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700797 struct file *file,
798 int mode,
799 loff_t offset,
800 loff_t len)
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100801{
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700802 struct inode *inode = file_inode(file);
803 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700804 long error;
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100805 enum xfs_prealloc_flags flags = 0;
Dan Williamsc63a8ea2018-03-12 14:12:29 -0700806 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700807 loff_t new_size = 0;
Thomas Meyer749f24f2017-10-09 11:38:54 -0700808 bool do_file_insert = false;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100809
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700810 if (!S_ISREG(inode->i_mode))
811 return -EINVAL;
Namjae Jeona904b1c2015-03-25 15:08:56 +1100812 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100813 return -EOPNOTSUPP;
814
Christoph Hellwig781355c2015-02-16 11:59:50 +1100815 xfs_ilock(ip, iolock);
Dan Williams69eb5fa2018-03-20 14:42:38 -0700816 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100817 if (error)
818 goto out_unlock;
819
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700820 if (mode & FALLOC_FL_PUNCH_HOLE) {
821 error = xfs_free_file_space(ip, offset, len);
822 if (error)
823 goto out_unlock;
Namjae Jeone1d8fb82014-02-24 10:58:19 +1100824 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
Fabian Frederick93407472017-02-27 14:28:32 -0800825 unsigned int blksize_mask = i_blocksize(inode) - 1;
Namjae Jeone1d8fb82014-02-24 10:58:19 +1100826
827 if (offset & blksize_mask || len & blksize_mask) {
Dave Chinner24513372014-06-25 14:58:08 +1000828 error = -EINVAL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +1100829 goto out_unlock;
830 }
831
Lukas Czerner23fffa92014-04-12 09:56:41 -0400832 /*
833 * There is no need to overlap collapse range with EOF,
834 * in which case it is effectively a truncate operation
835 */
836 if (offset + len >= i_size_read(inode)) {
Dave Chinner24513372014-06-25 14:58:08 +1000837 error = -EINVAL;
Lukas Czerner23fffa92014-04-12 09:56:41 -0400838 goto out_unlock;
839 }
840
Namjae Jeone1d8fb82014-02-24 10:58:19 +1100841 new_size = i_size_read(inode) - len;
842
843 error = xfs_collapse_file_space(ip, offset, len);
844 if (error)
845 goto out_unlock;
Namjae Jeona904b1c2015-03-25 15:08:56 +1100846 } else if (mode & FALLOC_FL_INSERT_RANGE) {
Darrick J. Wong7d83fb12018-04-16 23:07:45 -0700847 unsigned int blksize_mask = i_blocksize(inode) - 1;
848 loff_t isize = i_size_read(inode);
Namjae Jeona904b1c2015-03-25 15:08:56 +1100849
Namjae Jeona904b1c2015-03-25 15:08:56 +1100850 if (offset & blksize_mask || len & blksize_mask) {
851 error = -EINVAL;
852 goto out_unlock;
853 }
854
Darrick J. Wong7d83fb12018-04-16 23:07:45 -0700855 /*
856 * New inode size must not exceed ->s_maxbytes, accounting for
857 * possible signed overflow.
858 */
859 if (inode->i_sb->s_maxbytes - isize < len) {
Namjae Jeona904b1c2015-03-25 15:08:56 +1100860 error = -EFBIG;
861 goto out_unlock;
862 }
Darrick J. Wong7d83fb12018-04-16 23:07:45 -0700863 new_size = isize + len;
Namjae Jeona904b1c2015-03-25 15:08:56 +1100864
865 /* Offset should be less than i_size */
Darrick J. Wong7d83fb12018-04-16 23:07:45 -0700866 if (offset >= isize) {
Namjae Jeona904b1c2015-03-25 15:08:56 +1100867 error = -EINVAL;
868 goto out_unlock;
869 }
Thomas Meyer749f24f2017-10-09 11:38:54 -0700870 do_file_insert = true;
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700871 } else {
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100872 flags |= XFS_PREALLOC_SET;
873
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700874 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
875 offset + len > i_size_read(inode)) {
876 new_size = offset + len;
Dave Chinner24513372014-06-25 14:58:08 +1000877 error = inode_newsize_ok(inode, new_size);
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700878 if (error)
879 goto out_unlock;
880 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100881
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800882 if (mode & FALLOC_FL_ZERO_RANGE) {
Lukas Czerner376ba312014-03-13 19:07:58 +1100883 error = xfs_zero_file_space(ip, offset, len);
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800884 } else if (mode & FALLOC_FL_UNSHARE_RANGE) {
885 error = xfs_reflink_unshare(ip, offset, len);
886 if (error)
887 goto out_unlock;
888
889 if (!xfs_is_always_cow_inode(ip)) {
890 error = xfs_alloc_file_space(ip, offset, len,
891 XFS_BMAPI_PREALLOC);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -0700892 }
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800893 } else {
894 /*
895 * If always_cow mode we can't use preallocations and
896 * thus should not create them.
897 */
898 if (xfs_is_always_cow_inode(ip)) {
899 error = -EOPNOTSUPP;
900 goto out_unlock;
901 }
902
Lukas Czerner376ba312014-03-13 19:07:58 +1100903 error = xfs_alloc_file_space(ip, offset, len,
904 XFS_BMAPI_PREALLOC);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -0700905 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100906 if (error)
907 goto out_unlock;
908 }
909
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700910 if (file->f_flags & O_DSYNC)
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100911 flags |= XFS_PREALLOC_SYNC;
912
913 error = xfs_update_prealloc_flags(ip, flags);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100914 if (error)
915 goto out_unlock;
916
917 /* Change file size if needed */
918 if (new_size) {
919 struct iattr iattr;
920
921 iattr.ia_valid = ATTR_SIZE;
922 iattr.ia_size = new_size;
Jan Kara69bca802016-05-26 14:46:43 +0200923 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
Namjae Jeona904b1c2015-03-25 15:08:56 +1100924 if (error)
925 goto out_unlock;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100926 }
927
Namjae Jeona904b1c2015-03-25 15:08:56 +1100928 /*
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.
933 */
934 if (do_file_insert)
935 error = xfs_insert_file_space(ip, offset, len);
936
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100937out_unlock:
Christoph Hellwig781355c2015-02-16 11:59:50 +1100938 xfs_iunlock(ip, iolock);
Dave Chinner24513372014-06-25 14:58:08 +1000939 return error;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100940}
941
Darrick J. Wong3fc9f5e2018-10-30 10:47:26 +1100942
Eric Biggersda034bc2018-11-14 21:48:18 -0800943STATIC loff_t
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100944xfs_file_remap_range(
Darrick J. Wong3fc9f5e2018-10-30 10:47:26 +1100945 struct file *file_in,
946 loff_t pos_in,
947 struct file *file_out,
948 loff_t pos_out,
949 loff_t len,
950 unsigned int remap_flags)
Darrick J. Wong9fe26042016-10-03 09:11:40 -0700951{
Darrick J. Wong3fc9f5e2018-10-30 10:47:26 +1100952 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;
957 loff_t remapped = 0;
958 xfs_extlen_t cowextsize;
959 int ret;
960
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100961 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
962 return -EINVAL;
Darrick J. Wongcc714662016-10-03 09:11:41 -0700963
Darrick J. Wong3fc9f5e2018-10-30 10:47:26 +1100964 if (!xfs_sb_version_hasreflink(&mp->m_sb))
965 return -EOPNOTSUPP;
966
967 if (XFS_FORCED_SHUTDOWN(mp))
968 return -EIO;
969
970 /* Prepare and then clone file data. */
971 ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
972 &len, remap_flags);
973 if (ret < 0 || len == 0)
974 return ret;
975
976 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
977
978 ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
979 &remapped);
980 if (ret)
981 goto out_unlock;
982
983 /*
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.
987 */
988 cowextsize = 0;
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;
994
995 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
996 remap_flags);
997
998out_unlock:
999 xfs_reflink_remap_unlock(file_in, file_out);
1000 if (ret)
1001 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1002 return remapped > 0 ? remapped : ret;
Darrick J. Wong9fe26042016-10-03 09:11:40 -07001003}
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001006xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001008 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001010 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001012 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1013 return -EIO;
Christoph Hellwig91f99432017-08-29 16:13:20 +02001014 file->f_mode |= FMODE_NOWAIT;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001015 return 0;
1016}
1017
1018STATIC int
1019xfs_dir_open(
1020 struct inode *inode,
1021 struct file *file)
1022{
1023 struct xfs_inode *ip = XFS_I(inode);
1024 int mode;
1025 int error;
1026
1027 error = xfs_file_open(inode, file);
1028 if (error)
1029 return error;
1030
1031 /*
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.
1034 */
Christoph Hellwig309ecac82013-12-06 12:30:09 -08001035 mode = xfs_ilock_data_map_shared(ip);
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001036 if (ip->i_d.di_nextents > 0)
Darrick J. Wong7a652bb2017-02-02 15:13:58 -08001037 error = xfs_dir3_data_readahead(ip, 0, -1);
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001038 xfs_iunlock(ip, mode);
Darrick J. Wong7a652bb2017-02-02 15:13:58 -08001039 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001043xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct inode *inode,
1045 struct file *filp)
1046{
Dave Chinner24513372014-06-25 14:58:08 +10001047 return xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001051xfs_file_readdir(
Al Virob8227552013-05-22 17:07:56 -04001052 struct file *file,
1053 struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Al Virob8227552013-05-22 17:07:56 -04001055 struct inode *inode = file_inode(file);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001056 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001057 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001059 /*
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
1065 * physical blocks.
1066 *
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
Eric Sandeena9cc7992010-02-03 17:50:13 +00001069 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001070 */
Darrick J. Wonga5c46e52017-10-17 21:37:44 -07001071 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Darrick J. Wongacb95532017-06-16 11:00:14 -07001073 return xfs_readdir(NULL, ip, ctx, bufsize);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001074}
1075
1076STATIC loff_t
1077xfs_file_llseek(
1078 struct file *file,
1079 loff_t offset,
Eric Sandeen59f9c002014-09-09 11:57:10 +10001080 int whence)
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001081{
Christoph Hellwig9b2970a2017-06-29 11:43:21 -07001082 struct inode *inode = file->f_mapping->host;
1083
1084 if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
1085 return -EIO;
1086
Eric Sandeen59f9c002014-09-09 11:57:10 +10001087 switch (whence) {
Christoph Hellwig9b2970a2017-06-29 11:43:21 -07001088 default:
Eric Sandeen59f9c002014-09-09 11:57:10 +10001089 return generic_file_llseek(file, offset, whence);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001090 case SEEK_HOLE:
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001091 offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
Christoph Hellwig9b2970a2017-06-29 11:43:21 -07001092 break;
Eric Sandeen49c69592014-09-09 11:56:48 +10001093 case SEEK_DATA:
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001094 offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
Christoph Hellwig9b2970a2017-06-29 11:43:21 -07001095 break;
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001096 }
Christoph Hellwig9b2970a2017-06-29 11:43:21 -07001097
1098 if (offset < 0)
1099 return offset;
1100 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001101}
1102
Dave Chinnerde0e8c22015-02-23 21:44:19 +11001103/*
1104 * Locking for serialisation of IO during page faults. This results in a lock
1105 * ordering of:
1106 *
1107 * mmap_sem (MM)
Dave Chinner6b698ed2015-06-04 09:18:53 +10001108 * sb_start_pagefault(vfs, freeze)
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001109 * i_mmaplock (XFS - truncate serialisation)
Dave Chinner6b698ed2015-06-04 09:18:53 +10001110 * page_lock (MM)
1111 * i_lock (XFS - extent map serialisation)
Dave Chinnerde0e8c22015-02-23 21:44:19 +11001112 */
Souptick Joarder05edd882018-05-29 10:39:03 -07001113static vm_fault_t
Christoph Hellwigd522d562017-08-29 10:08:41 -07001114__xfs_filemap_fault(
Dave Jiangc791ace2017-02-24 14:57:08 -08001115 struct vm_fault *vmf,
Christoph Hellwigd522d562017-08-29 10:08:41 -07001116 enum page_entry_size pe_size,
1117 bool write_fault)
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001118{
Dave Jiangf4200392017-02-22 15:40:06 -08001119 struct inode *inode = file_inode(vmf->vma->vm_file);
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001120 struct xfs_inode *ip = XFS_I(inode);
Souptick Joarder05edd882018-05-29 10:39:03 -07001121 vm_fault_t ret;
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001122
Christoph Hellwigd522d562017-08-29 10:08:41 -07001123 trace_xfs_filemap_fault(ip, pe_size, write_fault);
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001124
Christoph Hellwigd522d562017-08-29 10:08:41 -07001125 if (write_fault) {
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001126 sb_start_pagefault(inode->i_sb);
Dave Jiangf4200392017-02-22 15:40:06 -08001127 file_update_time(vmf->vma->vm_file);
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001128 }
1129
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001130 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
Christoph Hellwigd522d562017-08-29 10:08:41 -07001131 if (IS_DAX(inode)) {
Christoph Hellwiga39e5962017-11-01 16:36:47 +01001132 pfn_t pfn;
1133
Jan Karac0b24622018-01-07 16:38:43 -05001134 ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL, &xfs_iomap_ops);
Christoph Hellwiga39e5962017-11-01 16:36:47 +01001135 if (ret & VM_FAULT_NEEDDSYNC)
1136 ret = dax_finish_sync_fault(vmf, pe_size, pfn);
Christoph Hellwigd522d562017-08-29 10:08:41 -07001137 } else {
1138 if (write_fault)
1139 ret = iomap_page_mkwrite(vmf, &xfs_iomap_ops);
1140 else
1141 ret = filemap_fault(vmf);
1142 }
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001143 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001144
Christoph Hellwigd522d562017-08-29 10:08:41 -07001145 if (write_fault)
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001146 sb_end_pagefault(inode->i_sb);
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001147 return ret;
1148}
1149
Souptick Joarder05edd882018-05-29 10:39:03 -07001150static vm_fault_t
Christoph Hellwigd522d562017-08-29 10:08:41 -07001151xfs_filemap_fault(
1152 struct vm_fault *vmf)
1153{
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));
1158}
1159
Souptick Joarder05edd882018-05-29 10:39:03 -07001160static vm_fault_t
Christoph Hellwigd522d562017-08-29 10:08:41 -07001161xfs_filemap_huge_fault(
1162 struct vm_fault *vmf,
1163 enum page_entry_size pe_size)
1164{
1165 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1166 return VM_FAULT_FALLBACK;
1167
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));
1171}
1172
Souptick Joarder05edd882018-05-29 10:39:03 -07001173static vm_fault_t
Christoph Hellwigd522d562017-08-29 10:08:41 -07001174xfs_filemap_page_mkwrite(
1175 struct vm_fault *vmf)
1176{
1177 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1178}
1179
Dave Chinner3af49282015-11-03 12:37:02 +11001180/*
Jan Kara7b565c92017-11-01 16:36:46 +01001181 * 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.
Dave Chinner3af49282015-11-03 12:37:02 +11001184 */
Souptick Joarder05edd882018-05-29 10:39:03 -07001185static vm_fault_t
Dave Chinner3af49282015-11-03 12:37:02 +11001186xfs_filemap_pfn_mkwrite(
Dave Chinner3af49282015-11-03 12:37:02 +11001187 struct vm_fault *vmf)
1188{
1189
Jan Kara7b565c92017-11-01 16:36:46 +01001190 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
Dave Chinner3af49282015-11-03 12:37:02 +11001191}
1192
Dave Chinner6b698ed2015-06-04 09:18:53 +10001193static const struct vm_operations_struct xfs_file_vm_ops = {
1194 .fault = xfs_filemap_fault,
Dave Jianga2d58162017-02-24 14:56:59 -08001195 .huge_fault = xfs_filemap_huge_fault,
Dave Chinner6b698ed2015-06-04 09:18:53 +10001196 .map_pages = filemap_map_pages,
1197 .page_mkwrite = xfs_filemap_page_mkwrite,
Dave Chinner3af49282015-11-03 12:37:02 +11001198 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
Dave Chinner6b698ed2015-06-04 09:18:53 +10001199};
1200
1201STATIC int
1202xfs_file_mmap(
1203 struct file *filp,
1204 struct vm_area_struct *vma)
1205{
Christoph Hellwiga39e5962017-11-01 16:36:47 +01001206 /*
1207 * We don't support synchronous mappings for non-DAX files. At least
1208 * until someone comes with a sensible use case.
1209 */
1210 if (!IS_DAX(file_inode(filp)) && (vma->vm_flags & VM_SYNC))
1211 return -EOPNOTSUPP;
1212
Dave Chinner6b698ed2015-06-04 09:18:53 +10001213 file_accessed(filp);
1214 vma->vm_ops = &xfs_file_vm_ops;
1215 if (IS_DAX(file_inode(filp)))
Dave Jiange1fb4a02018-08-17 15:43:40 -07001216 vma->vm_flags |= VM_HUGEPAGE;
Dave Chinner6b698ed2015-06-04 09:18:53 +10001217 return 0;
Dave Chinner075a9242015-02-23 21:44:54 +11001218}
1219
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001220const struct file_operations xfs_file_operations = {
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001221 .llseek = xfs_file_llseek,
Al Virob4f5d2c2014-04-02 14:37:59 -04001222 .read_iter = xfs_file_read_iter,
Al Virobf97f3bc2014-04-03 14:20:23 -04001223 .write_iter = xfs_file_write_iter,
Al Viro82c156f2016-09-22 23:35:42 -04001224 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001225 .splice_write = iter_file_splice_write,
Christoph Hellwig81214ba2018-12-04 11:12:08 -07001226 .iopoll = iomap_dio_iopoll,
Nathan Scott3562fd42006-03-14 14:00:35 +11001227 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001229 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001231 .mmap = xfs_file_mmap,
Christoph Hellwiga39e5962017-11-01 16:36:47 +01001232 .mmap_supported_flags = MAP_SYNC,
Nathan Scott3562fd42006-03-14 14:00:35 +11001233 .open = xfs_file_open,
1234 .release = xfs_file_release,
1235 .fsync = xfs_file_fsync,
Toshi Kanidbe6ec82016-10-07 16:59:59 -07001236 .get_unmapped_area = thp_get_unmapped_area,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001237 .fallocate = xfs_file_fallocate,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001238 .remap_file_range = xfs_file_remap_range,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239};
1240
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001241const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001242 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -04001244 .iterate_shared = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -04001245 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +11001246 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001247#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001248 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001249#endif
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +00001250 .fsync = xfs_dir_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251};