blob: d054b73b56fbbaee27721f34627823819c6db086 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110025#include "xfs_da_format.h"
26#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_trans.h"
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000029#include "xfs_inode_item.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000030#include "xfs_bmap.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100031#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100033#include "xfs_dir2.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100034#include "xfs_dir2_priv.h"
Christoph Hellwigddcd8562008-12-03 07:55:34 -050035#include "xfs_ioctl.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000036#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110037#include "xfs_log.h"
Brian Fosterdc06f3982014-07-24 19:49:28 +100038#include "xfs_icache.h"
Christoph Hellwig781355c2015-02-16 11:59:50 +110039#include "xfs_pnfs.h"
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +100040#include "xfs_iomap.h"
Darrick J. Wong0613f162016-10-03 09:11:37 -070041#include "xfs_reflink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <linux/dcache.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010044#include <linux/falloc.h>
Jeff Liud126d432012-08-21 17:11:57 +080045#include <linux/pagevec.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040046#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040048static const struct vm_operations_struct xfs_file_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Christoph Hellwigdda35b82010-02-15 09:44:46 +000050/*
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +100051 * Clear the specified ranges to zero through either the pagecache or DAX.
52 * Holes and unwritten extents will be left as-is as they already are zeroed.
Christoph Hellwigdda35b82010-02-15 09:44:46 +000053 */
Dave Chinneref9d8732012-11-29 15:26:33 +110054int
Christoph Hellwig7bb41db2016-06-21 09:56:26 +100055xfs_zero_range(
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +100056 struct xfs_inode *ip,
Christoph Hellwig7bb41db2016-06-21 09:56:26 +100057 xfs_off_t pos,
58 xfs_off_t count,
59 bool *did_zero)
Christoph Hellwigdda35b82010-02-15 09:44:46 +000060{
Christoph Hellwig459f0fb2016-06-21 09:55:18 +100061 return iomap_zero_range(VFS_I(ip), pos, count, NULL, &xfs_iomap_ops);
Christoph Hellwigdda35b82010-02-15 09:44:46 +000062}
63
Christoph Hellwig8add71c2015-02-02 09:53:56 +110064int
65xfs_update_prealloc_flags(
66 struct xfs_inode *ip,
67 enum xfs_prealloc_flags flags)
68{
69 struct xfs_trans *tp;
70 int error;
71
Christoph Hellwig253f4912016-04-06 09:19:55 +100072 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
73 0, 0, 0, &tp);
74 if (error)
Christoph Hellwig8add71c2015-02-02 09:53:56 +110075 return error;
Christoph Hellwig8add71c2015-02-02 09:53:56 +110076
77 xfs_ilock(ip, XFS_ILOCK_EXCL);
78 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
79
80 if (!(flags & XFS_PREALLOC_INVISIBLE)) {
Dave Chinnerc19b3b052016-02-09 16:54:58 +110081 VFS_I(ip)->i_mode &= ~S_ISUID;
82 if (VFS_I(ip)->i_mode & S_IXGRP)
83 VFS_I(ip)->i_mode &= ~S_ISGID;
Christoph Hellwig8add71c2015-02-02 09:53:56 +110084 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
85 }
86
87 if (flags & XFS_PREALLOC_SET)
88 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
89 if (flags & XFS_PREALLOC_CLEAR)
90 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
91
92 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
93 if (flags & XFS_PREALLOC_SYNC)
94 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +100095 return xfs_trans_commit(tp);
Christoph Hellwig8add71c2015-02-02 09:53:56 +110096}
97
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +000098/*
99 * Fsync operations on directories are much simpler than on regular files,
100 * as there is no file data to flush, and thus also no need for explicit
101 * cache flush operations, and there are no non-transaction metadata updates
102 * on directories either.
103 */
104STATIC int
105xfs_dir_fsync(
106 struct file *file,
107 loff_t start,
108 loff_t end,
109 int datasync)
110{
111 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
112 struct xfs_mount *mp = ip->i_mount;
113 xfs_lsn_t lsn = 0;
114
115 trace_xfs_dir_fsync(ip);
116
117 xfs_ilock(ip, XFS_ILOCK_SHARED);
118 if (xfs_ipincount(ip))
119 lsn = ip->i_itemp->ili_last_lsn;
120 xfs_iunlock(ip, XFS_ILOCK_SHARED);
121
122 if (!lsn)
123 return 0;
Dave Chinner24513372014-06-25 14:58:08 +1000124 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +0000125}
126
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000127STATIC int
128xfs_file_fsync(
129 struct file *file,
Josef Bacik02c24a82011-07-16 20:44:56 -0400130 loff_t start,
131 loff_t end,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000132 int datasync)
133{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200134 struct inode *inode = file->f_mapping->host;
135 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000136 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000137 int error = 0;
138 int log_flushed = 0;
Christoph Hellwigb1037052011-09-19 14:55:51 +0000139 xfs_lsn_t lsn = 0;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000140
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000141 trace_xfs_file_fsync(ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000142
Josef Bacik02c24a82011-07-16 20:44:56 -0400143 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
144 if (error)
145 return error;
146
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000147 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000148 return -EIO;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000149
150 xfs_iflags_clear(ip, XFS_ITRUNCATED);
151
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000152 if (mp->m_flags & XFS_MOUNT_BARRIER) {
153 /*
154 * If we have an RT and/or log subvolume we need to make sure
155 * to flush the write cache the device used for file data
156 * first. This is to ensure newly written file data make
157 * it to disk before logging the new inode size in case of
158 * an extending write.
159 */
160 if (XFS_IS_REALTIME_INODE(ip))
161 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
162 else if (mp->m_logdev_targp != mp->m_ddev_targp)
163 xfs_blkdev_issue_flush(mp->m_ddev_targp);
164 }
165
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000166 /*
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100167 * All metadata updates are logged, which means that we just have to
168 * flush the log up to the latest LSN that touched the inode. If we have
169 * concurrent fsync/fdatasync() calls, we need them to all block on the
170 * log force before we clear the ili_fsync_fields field. This ensures
171 * that we don't get a racing sync operation that does not wait for the
172 * metadata to hit the journal before returning. If we race with
173 * clearing the ili_fsync_fields, then all that will happen is the log
174 * force will do nothing as the lsn will already be on disk. We can't
175 * race with setting ili_fsync_fields because that is done under
176 * XFS_ILOCK_EXCL, and that can't happen because we hold the lock shared
177 * until after the ili_fsync_fields is cleared.
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000178 */
179 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000180 if (xfs_ipincount(ip)) {
181 if (!datasync ||
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100182 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000183 lsn = ip->i_itemp->ili_last_lsn;
184 }
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000185
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100186 if (lsn) {
Christoph Hellwigb1037052011-09-19 14:55:51 +0000187 error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
Dave Chinnerfc0561c2015-11-03 13:14:59 +1100188 ip->i_itemp->ili_fsync_fields = 0;
189 }
190 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigb1037052011-09-19 14:55:51 +0000191
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000192 /*
193 * If we only have a single device, and the log force about was
194 * a no-op we might have to flush the data device cache here.
195 * This can only happen for fdatasync/O_DSYNC if we were overwriting
196 * an already allocated file and thus do not have any metadata to
197 * commit.
198 */
199 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
200 mp->m_logdev_targp == mp->m_ddev_targp &&
201 !XFS_IS_REALTIME_INODE(ip) &&
202 !log_flushed)
203 xfs_blkdev_issue_flush(mp->m_ddev_targp);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000204
Dave Chinner24513372014-06-25 14:58:08 +1000205 return error;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000206}
207
Christoph Hellwig00258e32010-02-15 09:44:47 +0000208STATIC ssize_t
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000209xfs_file_dio_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000210 struct kiocb *iocb,
Al Virob4f5d2c2014-04-02 14:37:59 -0400211 struct iov_iter *to)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000212{
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000213 struct address_space *mapping = iocb->ki_filp->f_mapping;
214 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000215 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwigf1285ff2016-07-20 11:36:57 +1000216 loff_t isize = i_size_read(inode);
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000217 size_t count = iov_iter_count(to);
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100218 loff_t end = iocb->ki_pos + count - 1;
Christoph Hellwigf1285ff2016-07-20 11:36:57 +1000219 struct iov_iter data;
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000220 struct xfs_buftarg *target;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000221 ssize_t ret = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000222
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000223 trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000224
Christoph Hellwigf1285ff2016-07-20 11:36:57 +1000225 if (!count)
226 return 0; /* skip atime */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000227
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000228 if (XFS_IS_REALTIME_INODE(ip))
229 target = ip->i_mount->m_rtdev_targp;
230 else
231 target = ip->i_mount->m_ddev_targp;
232
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000233 /* DIO must be aligned to device logical sector size */
234 if ((iocb->ki_pos | count) & target->bt_logical_sectormask) {
235 if (iocb->ki_pos == isize)
236 return 0;
237 return -EINVAL;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000238 }
239
Christoph Hellwiga447d7c2016-10-03 09:47:34 +1100240 file_accessed(iocb->ki_filp);
241
Christoph Hellwig65523212016-11-30 14:33:25 +1100242 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000243 if (mapping->nrpages) {
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100244 ret = filemap_write_and_wait_range(mapping, iocb->ki_pos, end);
245 if (ret)
246 goto out_unlock;
Dave Chinner487f84f2011-01-12 11:37:10 +1100247
Brian Foster3d751af2015-08-19 10:35:04 +1000248 /*
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100249 * Invalidate whole pages. This can return an error if we fail
250 * to invalidate a page, but this should never happen on XFS.
251 * Warn if it does fail.
Brian Foster3d751af2015-08-19 10:35:04 +1000252 */
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100253 ret = invalidate_inode_pages2_range(mapping,
254 iocb->ki_pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
255 WARN_ON_ONCE(ret);
256 ret = 0;
Dave Chinner0c38a252011-08-25 07:17:01 +0000257 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000258
Christoph Hellwigf1285ff2016-07-20 11:36:57 +1000259 data = *to;
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000260 ret = __blockdev_direct_IO(iocb, inode, target->bt_bdev, &data,
261 xfs_get_blocks_direct, NULL, NULL, 0);
Al Viroc3a69022016-10-10 13:26:27 -0400262 if (ret >= 0) {
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000263 iocb->ki_pos += ret;
264 iov_iter_advance(to, ret);
Christoph Hellwigfa8d9722016-07-20 11:38:01 +1000265 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000266
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100267out_unlock:
Christoph Hellwig65523212016-11-30 14:33:25 +1100268 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000269 return ret;
270}
271
Arnd Bergmannf021bd02016-07-22 09:50:55 +1000272static noinline ssize_t
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000273xfs_file_dax_read(
274 struct kiocb *iocb,
275 struct iov_iter *to)
276{
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000277 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000278 size_t count = iov_iter_count(to);
279 ssize_t ret = 0;
280
281 trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
282
283 if (!count)
284 return 0; /* skip atime */
285
Christoph Hellwig65523212016-11-30 14:33:25 +1100286 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Ross Zwisler11c59c92016-11-08 11:32:46 +1100287 ret = dax_iomap_rw(iocb, to, &xfs_iomap_ops);
Christoph Hellwig65523212016-11-30 14:33:25 +1100288 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000289
Christoph Hellwigf1285ff2016-07-20 11:36:57 +1000290 file_accessed(iocb->ki_filp);
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000291 return ret;
292}
293
294STATIC ssize_t
295xfs_file_buffered_aio_read(
296 struct kiocb *iocb,
297 struct iov_iter *to)
298{
299 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
300 ssize_t ret;
301
302 trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
303
Christoph Hellwig65523212016-11-30 14:33:25 +1100304 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Al Virob4f5d2c2014-04-02 14:37:59 -0400305 ret = generic_file_read_iter(iocb, to);
Christoph Hellwig65523212016-11-30 14:33:25 +1100306 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000307
308 return ret;
309}
310
311STATIC ssize_t
312xfs_file_read_iter(
313 struct kiocb *iocb,
314 struct iov_iter *to)
315{
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000316 struct inode *inode = file_inode(iocb->ki_filp);
317 struct xfs_mount *mp = XFS_I(inode)->i_mount;
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000318 ssize_t ret = 0;
319
320 XFS_STATS_INC(mp, xs_read_calls);
321
322 if (XFS_FORCED_SHUTDOWN(mp))
323 return -EIO;
324
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000325 if (IS_DAX(inode))
326 ret = xfs_file_dax_read(iocb, to);
327 else if (iocb->ki_flags & IOCB_DIRECT)
Christoph Hellwigbbc5a742016-07-20 11:35:42 +1000328 ret = xfs_file_dio_aio_read(iocb, to);
329 else
330 ret = xfs_file_buffered_aio_read(iocb, to);
331
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000332 if (ret > 0)
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100333 XFS_STATS_ADD(mp, xs_read_bytes, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000334 return ret;
335}
336
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100337/*
Christoph Hellwig193aec12012-03-27 10:34:49 -0400338 * Zero any on disk space between the current EOF and the new, larger EOF.
339 *
340 * This handles the normal case of zeroing the remainder of the last block in
341 * the file and the unusual case of zeroing blocks out beyond the size of the
342 * file. This second case only happens with fixed size extents and when the
343 * system crashes before the inode size was updated but after blocks were
344 * allocated.
345 *
346 * Expects the iolock to be held exclusive, and will take the ilock internally.
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000347 */
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000348int /* error (positive) */
349xfs_zero_eof(
Christoph Hellwig193aec12012-03-27 10:34:49 -0400350 struct xfs_inode *ip,
351 xfs_off_t offset, /* starting I/O offset */
Dave Chinner5885ebd2015-02-23 22:37:08 +1100352 xfs_fsize_t isize, /* current inode size */
353 bool *did_zeroing)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000354{
Christoph Hellwig193aec12012-03-27 10:34:49 -0400355 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000356 ASSERT(offset > isize);
357
Brian Foster0a50f162015-10-12 16:02:08 +1100358 trace_xfs_zero_eof(ip, isize, offset - isize);
Christoph Hellwig570b6212016-06-21 09:57:26 +1000359 return xfs_zero_range(ip, isize, offset - isize, did_zeroing);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000360}
361
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100362/*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100363 * Common pre-write limit and setup checks.
364 *
Christoph Hellwig5bf1f262011-12-18 20:00:13 +0000365 * Called with the iolocked held either shared and exclusive according to
366 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
367 * if called for a direct write beyond i_size.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100368 */
369STATIC ssize_t
370xfs_file_aio_write_checks(
Al Viro99733fa2015-04-07 14:25:18 -0400371 struct kiocb *iocb,
372 struct iov_iter *from,
Dave Chinner4d8d1582011-01-11 10:23:42 +1100373 int *iolock)
374{
Al Viro99733fa2015-04-07 14:25:18 -0400375 struct file *file = iocb->ki_filp;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100376 struct inode *inode = file->f_mapping->host;
377 struct xfs_inode *ip = XFS_I(inode);
Al Viro3309dd02015-04-09 12:55:47 -0400378 ssize_t error = 0;
Al Viro99733fa2015-04-07 14:25:18 -0400379 size_t count = iov_iter_count(from);
Brian Foster3136e8b2015-10-12 16:02:05 +1100380 bool drained_dio = false;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100381
Dave Chinner7271d242011-08-25 07:17:02 +0000382restart:
Al Viro3309dd02015-04-09 12:55:47 -0400383 error = generic_write_checks(iocb, from);
384 if (error <= 0)
Dave Chinner4d8d1582011-01-11 10:23:42 +1100385 return error;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100386
Christoph Hellwig65523212016-11-30 14:33:25 +1100387 error = xfs_break_layouts(inode, iolock);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100388 if (error)
389 return error;
390
Christoph Hellwig65523212016-11-30 14:33:25 +1100391 /*
392 * For changing security info in file_remove_privs() we need i_rwsem
393 * exclusively.
394 */
Jan Karaa6de82c2015-05-21 16:05:56 +0200395 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100396 xfs_iunlock(ip, *iolock);
Jan Karaa6de82c2015-05-21 16:05:56 +0200397 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig65523212016-11-30 14:33:25 +1100398 xfs_ilock(ip, *iolock);
Jan Karaa6de82c2015-05-21 16:05:56 +0200399 goto restart;
400 }
Dave Chinner4d8d1582011-01-11 10:23:42 +1100401 /*
402 * If the offset is beyond the size of the file, we need to zero any
403 * blocks that fall between the existing EOF and the start of this
Christoph Hellwig2813d682011-12-18 20:00:12 +0000404 * write. If zeroing is needed and we are currently holding the
Christoph Hellwig467f7892012-03-27 10:34:47 -0400405 * iolock shared, we need to update it to exclusive which implies
406 * having to redo all checks before.
Dave Chinnerb9d59842015-04-16 22:03:07 +1000407 *
408 * We need to serialise against EOF updates that occur in IO
409 * completions here. We want to make sure that nobody is changing the
410 * size while we do this check until we have placed an IO barrier (i.e.
411 * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
412 * The spinlock effectively forms a memory barrier once we have the
413 * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
414 * and hence be able to correctly determine if we need to run zeroing.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100415 */
Dave Chinnerb9d59842015-04-16 22:03:07 +1000416 spin_lock(&ip->i_flags_lock);
Al Viro99733fa2015-04-07 14:25:18 -0400417 if (iocb->ki_pos > i_size_read(inode)) {
Dave Chinner5885ebd2015-02-23 22:37:08 +1100418 bool zero = false;
419
Dave Chinnerb9d59842015-04-16 22:03:07 +1000420 spin_unlock(&ip->i_flags_lock);
Brian Foster3136e8b2015-10-12 16:02:05 +1100421 if (!drained_dio) {
422 if (*iolock == XFS_IOLOCK_SHARED) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100423 xfs_iunlock(ip, *iolock);
Brian Foster3136e8b2015-10-12 16:02:05 +1100424 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig65523212016-11-30 14:33:25 +1100425 xfs_ilock(ip, *iolock);
Brian Foster3136e8b2015-10-12 16:02:05 +1100426 iov_iter_reexpand(from, count);
427 }
Dave Chinner40c63fb2015-04-16 22:03:17 +1000428 /*
429 * We now have an IO submission barrier in place, but
430 * AIO can do EOF updates during IO completion and hence
431 * we now need to wait for all of them to drain. Non-AIO
432 * DIO will have drained before we are given the
433 * XFS_IOLOCK_EXCL, and so for most cases this wait is a
434 * no-op.
435 */
436 inode_dio_wait(inode);
Brian Foster3136e8b2015-10-12 16:02:05 +1100437 drained_dio = true;
Dave Chinner7271d242011-08-25 07:17:02 +0000438 goto restart;
439 }
Al Viro99733fa2015-04-07 14:25:18 -0400440 error = xfs_zero_eof(ip, iocb->ki_pos, i_size_read(inode), &zero);
Christoph Hellwig467f7892012-03-27 10:34:47 -0400441 if (error)
442 return error;
Dave Chinnerb9d59842015-04-16 22:03:07 +1000443 } else
444 spin_unlock(&ip->i_flags_lock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100445
446 /*
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000447 * Updating the timestamps will grab the ilock again from
448 * xfs_fs_dirty_inode, so we have to call it after dropping the
449 * lock above. Eventually we should look into a way to avoid
450 * the pointless lock roundtrip.
451 */
Josef Bacikc3b2da32012-03-26 09:59:21 -0400452 if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
453 error = file_update_time(file);
454 if (error)
455 return error;
456 }
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000457
458 /*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100459 * If we're writing the file then make sure to clear the setuid and
460 * setgid bits if the process is not being run by root. This keeps
461 * people from modifying setuid and setgid binaries.
462 */
Jan Karaa6de82c2015-05-21 16:05:56 +0200463 if (!IS_NOSEC(inode))
464 return file_remove_privs(file);
465 return 0;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100466}
467
468/*
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100469 * xfs_file_dio_aio_write - handle direct IO writes
470 *
471 * Lock the inode appropriately to prepare for and issue a direct IO write.
Dave Chinnereda77982011-01-11 10:22:40 +1100472 * By separating it from the buffered write path we remove all the tricky to
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100473 * follow locking changes and looping.
474 *
Dave Chinnereda77982011-01-11 10:22:40 +1100475 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
476 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
477 * pages are flushed out.
478 *
479 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
480 * allowing them to be done in parallel with reads and other direct IO writes.
481 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
482 * needs to do sub-block zeroing and that requires serialisation against other
483 * direct IOs to the same block. In this case we need to serialise the
484 * submission of the unaligned IOs so that we don't get racing block zeroing in
485 * the dio layer. To avoid the problem with aio, we also need to wait for
486 * outstanding IOs to complete so that unwritten extent conversion is completed
487 * before we try to map the overlapping block. This is currently implemented by
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000488 * hitting it with a big hammer (i.e. inode_dio_wait()).
Dave Chinnereda77982011-01-11 10:22:40 +1100489 *
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100490 * Returns with locks held indicated by @iolock and errors indicated by
491 * negative return values.
492 */
493STATIC ssize_t
494xfs_file_dio_aio_write(
495 struct kiocb *iocb,
Al Virob3188912014-04-02 07:06:30 -0400496 struct iov_iter *from)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100497{
498 struct file *file = iocb->ki_filp;
499 struct address_space *mapping = file->f_mapping;
500 struct inode *inode = mapping->host;
501 struct xfs_inode *ip = XFS_I(inode);
502 struct xfs_mount *mp = ip->i_mount;
503 ssize_t ret = 0;
Dave Chinnereda77982011-01-11 10:22:40 +1100504 int unaligned_io = 0;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000505 int iolock;
Al Virob3188912014-04-02 07:06:30 -0400506 size_t count = iov_iter_count(from);
Dave Chinner0cefb292015-04-16 22:03:27 +1000507 loff_t end;
508 struct iov_iter data;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100509 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
510 mp->m_rtdev_targp : mp->m_ddev_targp;
511
Eric Sandeen7c71ee72014-01-21 16:46:23 -0600512 /* DIO must be aligned to device logical sector size */
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000513 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000514 return -EINVAL;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100515
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100516 /*
517 * Don't take the exclusive iolock here unless the I/O is unaligned to
518 * the file system block size. We don't need to consider the EOF
519 * extension case here because xfs_file_aio_write_checks() will relock
520 * the inode as necessary for EOF zeroing cases and fill out the new
521 * inode size as appropriate.
522 */
Christoph Hellwig13712712016-04-07 08:51:57 -0700523 if ((iocb->ki_pos & mp->m_blockmask) ||
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100524 ((iocb->ki_pos + count) & mp->m_blockmask)) {
Dave Chinnereda77982011-01-11 10:22:40 +1100525 unaligned_io = 1;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000526 iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100527 } else {
Christoph Hellwigd0606462011-12-18 20:00:14 +0000528 iolock = XFS_IOLOCK_SHARED;
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000529 }
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100530
Christoph Hellwig65523212016-11-30 14:33:25 +1100531 xfs_ilock(ip, iolock);
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100532
Al Viro99733fa2015-04-07 14:25:18 -0400533 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100534 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000535 goto out;
Al Viro99733fa2015-04-07 14:25:18 -0400536 count = iov_iter_count(from);
Christoph Hellwig13712712016-04-07 08:51:57 -0700537 end = iocb->ki_pos + count - 1;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100538
539 if (mapping->nrpages) {
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100540 ret = filemap_write_and_wait_range(mapping, iocb->ki_pos, end);
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100541 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000542 goto out;
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100543
Dave Chinner834ffca2014-09-02 12:12:52 +1000544 /*
Brian Foster3d751af2015-08-19 10:35:04 +1000545 * Invalidate whole pages. This can return an error if we fail
546 * to invalidate a page, but this should never happen on XFS.
547 * Warn if it does fail.
Dave Chinner834ffca2014-09-02 12:12:52 +1000548 */
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100549 ret = invalidate_inode_pages2_range(mapping,
550 iocb->ki_pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
Dave Chinner834ffca2014-09-02 12:12:52 +1000551 WARN_ON_ONCE(ret);
552 ret = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100553 }
554
Dave Chinnereda77982011-01-11 10:22:40 +1100555 /*
556 * If we are doing unaligned IO, wait for all other IO to drain,
Christoph Hellwig0ee7a3f2016-10-20 15:44:14 +1100557 * otherwise demote the lock if we had to take the exclusive lock
558 * for other reasons in xfs_file_aio_write_checks.
Dave Chinnereda77982011-01-11 10:22:40 +1100559 */
560 if (unaligned_io)
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000561 inode_dio_wait(inode);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000562 else if (iolock == XFS_IOLOCK_EXCL) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100563 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000564 iolock = XFS_IOLOCK_SHARED;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100565 }
566
Christoph Hellwig3176c3e2016-07-20 11:31:42 +1000567 trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100568
Darrick J. Wong0613f162016-10-03 09:11:37 -0700569 /* If this is a block-aligned directio CoW, remap immediately. */
570 if (xfs_is_reflink_inode(ip) && !unaligned_io) {
571 ret = xfs_reflink_allocate_cow_range(ip, iocb->ki_pos, count);
572 if (ret)
573 goto out;
574 }
575
Dave Chinner0cefb292015-04-16 22:03:27 +1000576 data = *from;
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000577 ret = __blockdev_direct_IO(iocb, inode, target->bt_bdev, &data,
578 xfs_get_blocks_direct, xfs_end_io_direct_write,
579 NULL, DIO_ASYNC_EXTEND);
Dave Chinner0cefb292015-04-16 22:03:27 +1000580
581 /* see generic_file_direct_write() for why this is necessary */
582 if (mapping->nrpages) {
583 invalidate_inode_pages2_range(mapping,
Christoph Hellwig13712712016-04-07 08:51:57 -0700584 iocb->ki_pos >> PAGE_SHIFT,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300585 end >> PAGE_SHIFT);
Dave Chinner0cefb292015-04-16 22:03:27 +1000586 }
587
588 if (ret > 0) {
Christoph Hellwig13712712016-04-07 08:51:57 -0700589 iocb->ki_pos += ret;
Dave Chinner0cefb292015-04-16 22:03:27 +1000590 iov_iter_advance(from, ret);
Dave Chinner0cefb292015-04-16 22:03:27 +1000591 }
Christoph Hellwigd0606462011-12-18 20:00:14 +0000592out:
Christoph Hellwig65523212016-11-30 14:33:25 +1100593 xfs_iunlock(ip, iolock);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000594
Dave Chinner6b698ed2015-06-04 09:18:53 +1000595 /*
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000596 * No fallback to buffered IO on errors for XFS, direct IO will either
597 * complete fully or fail.
Dave Chinner6b698ed2015-06-04 09:18:53 +1000598 */
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000599 ASSERT(ret < 0 || ret == count);
600 return ret;
601}
602
Arnd Bergmannf021bd02016-07-22 09:50:55 +1000603static noinline ssize_t
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000604xfs_file_dax_write(
605 struct kiocb *iocb,
606 struct iov_iter *from)
607{
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000608 struct inode *inode = iocb->ki_filp->f_mapping->host;
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000609 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig17879e82016-09-19 11:24:50 +1000610 int iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000611 ssize_t ret, error = 0;
612 size_t count;
613 loff_t pos;
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000614
Christoph Hellwig65523212016-11-30 14:33:25 +1100615 xfs_ilock(ip, iolock);
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000616 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
617 if (ret)
618 goto out;
619
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000620 pos = iocb->ki_pos;
621 count = iov_iter_count(from);
Dave Chinner8b2180b2016-08-17 08:31:33 +1000622
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000623 trace_xfs_file_dax_write(ip, count, pos);
Ross Zwisler11c59c92016-11-08 11:32:46 +1100624 ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000625 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
626 i_size_write(inode, iocb->ki_pos);
627 error = xfs_setfilesize(ip, pos, ret);
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000628 }
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000629out:
Christoph Hellwig65523212016-11-30 14:33:25 +1100630 xfs_iunlock(ip, iolock);
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000631 return error ? error : ret;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100632}
633
Christoph Hellwig00258e32010-02-15 09:44:47 +0000634STATIC ssize_t
Dave Chinner637bbc72011-01-11 10:17:30 +1100635xfs_file_buffered_aio_write(
636 struct kiocb *iocb,
Al Virob3188912014-04-02 07:06:30 -0400637 struct iov_iter *from)
Dave Chinner637bbc72011-01-11 10:17:30 +1100638{
639 struct file *file = iocb->ki_filp;
640 struct address_space *mapping = file->f_mapping;
641 struct inode *inode = mapping->host;
642 struct xfs_inode *ip = XFS_I(inode);
643 ssize_t ret;
644 int enospc = 0;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000645 int iolock = XFS_IOLOCK_EXCL;
Dave Chinner637bbc72011-01-11 10:17:30 +1100646
Christoph Hellwig65523212016-11-30 14:33:25 +1100647 xfs_ilock(ip, iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100648
Al Viro99733fa2015-04-07 14:25:18 -0400649 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100650 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000651 goto out;
Dave Chinner637bbc72011-01-11 10:17:30 +1100652
653 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100654 current->backing_dev_info = inode_to_bdi(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100655
656write_retry:
Christoph Hellwig3176c3e2016-07-20 11:31:42 +1000657 trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000658 ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
Al Viro0a64bc22014-02-11 22:25:22 -0500659 if (likely(ret >= 0))
Al Viro99733fa2015-04-07 14:25:18 -0400660 iocb->ki_pos += ret;
Brian Fosterdc06f3982014-07-24 19:49:28 +1000661
Dave Chinner637bbc72011-01-11 10:17:30 +1100662 /*
Brian Fosterdc06f3982014-07-24 19:49:28 +1000663 * If we hit a space limit, try to free up some lingering preallocated
664 * space before returning an error. In the case of ENOSPC, first try to
665 * write back all dirty inodes to free up some of the excess reserved
666 * metadata space. This reduces the chances that the eofblocks scan
667 * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
668 * also behaves as a filter to prevent too many eofblocks scans from
669 * running at the same time.
Dave Chinner637bbc72011-01-11 10:17:30 +1100670 */
Brian Fosterdc06f3982014-07-24 19:49:28 +1000671 if (ret == -EDQUOT && !enospc) {
672 enospc = xfs_inode_free_quota_eofblocks(ip);
673 if (enospc)
674 goto write_retry;
Darrick J. Wong83104d42016-10-03 09:11:46 -0700675 enospc = xfs_inode_free_quota_cowblocks(ip);
676 if (enospc)
677 goto write_retry;
Brian Fosterdc06f3982014-07-24 19:49:28 +1000678 } else if (ret == -ENOSPC && !enospc) {
679 struct xfs_eofblocks eofb = {0};
680
Dave Chinner637bbc72011-01-11 10:17:30 +1100681 enospc = 1;
Dave Chinner9aa05002012-10-08 21:56:04 +1100682 xfs_flush_inodes(ip->i_mount);
Brian Fosterdc06f3982014-07-24 19:49:28 +1000683 eofb.eof_scan_owner = ip->i_ino; /* for locking */
684 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
685 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
Dave Chinner9aa05002012-10-08 21:56:04 +1100686 goto write_retry;
Dave Chinner637bbc72011-01-11 10:17:30 +1100687 }
Christoph Hellwigd0606462011-12-18 20:00:14 +0000688
Dave Chinner637bbc72011-01-11 10:17:30 +1100689 current->backing_dev_info = NULL;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000690out:
Christoph Hellwig65523212016-11-30 14:33:25 +1100691 xfs_iunlock(ip, iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100692 return ret;
693}
694
695STATIC ssize_t
Al Virobf97f3bc2014-04-03 14:20:23 -0400696xfs_file_write_iter(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000697 struct kiocb *iocb,
Al Virobf97f3bc2014-04-03 14:20:23 -0400698 struct iov_iter *from)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000699{
700 struct file *file = iocb->ki_filp;
701 struct address_space *mapping = file->f_mapping;
702 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000703 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100704 ssize_t ret;
Al Virobf97f3bc2014-04-03 14:20:23 -0400705 size_t ocount = iov_iter_count(from);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000706
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100707 XFS_STATS_INC(ip->i_mount, xs_write_calls);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000708
Dave Chinner637bbc72011-01-11 10:17:30 +1100709 if (ocount == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000710 return 0;
711
Al Virobf97f3bc2014-04-03 14:20:23 -0400712 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
713 return -EIO;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000714
Christoph Hellwig16d4d432016-07-20 11:38:55 +1000715 if (IS_DAX(inode))
716 ret = xfs_file_dax_write(iocb, from);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700717 else if (iocb->ki_flags & IOCB_DIRECT) {
718 /*
719 * Allow a directio write to fall back to a buffered
720 * write *only* in the case that we're doing a reflink
721 * CoW. In all other directio scenarios we do not
722 * allow an operation to fall back to buffered mode.
723 */
Al Virobf97f3bc2014-04-03 14:20:23 -0400724 ret = xfs_file_dio_aio_write(iocb, from);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700725 if (ret == -EREMCHG)
726 goto buffered;
727 } else {
728buffered:
Al Virobf97f3bc2014-04-03 14:20:23 -0400729 ret = xfs_file_buffered_aio_write(iocb, from);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700730 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000731
Christoph Hellwigd0606462011-12-18 20:00:14 +0000732 if (ret > 0) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100733 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000734
Christoph Hellwigd0606462011-12-18 20:00:14 +0000735 /* Handle various SYNC-type writes */
Christoph Hellwige2592212016-04-07 08:52:01 -0700736 ret = generic_write_sync(iocb, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000737 }
Dave Chinnera363f0c2011-01-11 10:13:53 +1100738 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000739}
740
Namjae Jeona904b1c2015-03-25 15:08:56 +1100741#define XFS_FALLOC_FL_SUPPORTED \
742 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
743 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
Darrick J. Wong98cc2db2016-10-03 09:11:43 -0700744 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
Namjae Jeona904b1c2015-03-25 15:08:56 +1100745
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100746STATIC long
747xfs_file_fallocate(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700748 struct file *file,
749 int mode,
750 loff_t offset,
751 loff_t len)
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100752{
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700753 struct inode *inode = file_inode(file);
754 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700755 long error;
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100756 enum xfs_prealloc_flags flags = 0;
Christoph Hellwig781355c2015-02-16 11:59:50 +1100757 uint iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700758 loff_t new_size = 0;
Namjae Jeona904b1c2015-03-25 15:08:56 +1100759 bool do_file_insert = 0;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100760
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700761 if (!S_ISREG(inode->i_mode))
762 return -EINVAL;
Namjae Jeona904b1c2015-03-25 15:08:56 +1100763 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100764 return -EOPNOTSUPP;
765
Christoph Hellwig781355c2015-02-16 11:59:50 +1100766 xfs_ilock(ip, iolock);
Christoph Hellwig65523212016-11-30 14:33:25 +1100767 error = xfs_break_layouts(inode, &iolock);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100768 if (error)
769 goto out_unlock;
770
Dave Chinnere8e9ad42015-02-23 21:45:32 +1100771 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
772 iolock |= XFS_MMAPLOCK_EXCL;
773
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700774 if (mode & FALLOC_FL_PUNCH_HOLE) {
775 error = xfs_free_file_space(ip, offset, len);
776 if (error)
777 goto out_unlock;
Namjae Jeone1d8fb82014-02-24 10:58:19 +1100778 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
779 unsigned blksize_mask = (1 << inode->i_blkbits) - 1;
780
781 if (offset & blksize_mask || len & blksize_mask) {
Dave Chinner24513372014-06-25 14:58:08 +1000782 error = -EINVAL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +1100783 goto out_unlock;
784 }
785
Lukas Czerner23fffa92014-04-12 09:56:41 -0400786 /*
787 * There is no need to overlap collapse range with EOF,
788 * in which case it is effectively a truncate operation
789 */
790 if (offset + len >= i_size_read(inode)) {
Dave Chinner24513372014-06-25 14:58:08 +1000791 error = -EINVAL;
Lukas Czerner23fffa92014-04-12 09:56:41 -0400792 goto out_unlock;
793 }
794
Namjae Jeone1d8fb82014-02-24 10:58:19 +1100795 new_size = i_size_read(inode) - len;
796
797 error = xfs_collapse_file_space(ip, offset, len);
798 if (error)
799 goto out_unlock;
Namjae Jeona904b1c2015-03-25 15:08:56 +1100800 } else if (mode & FALLOC_FL_INSERT_RANGE) {
801 unsigned blksize_mask = (1 << inode->i_blkbits) - 1;
802
803 new_size = i_size_read(inode) + len;
804 if (offset & blksize_mask || len & blksize_mask) {
805 error = -EINVAL;
806 goto out_unlock;
807 }
808
809 /* check the new inode size does not wrap through zero */
810 if (new_size > inode->i_sb->s_maxbytes) {
811 error = -EFBIG;
812 goto out_unlock;
813 }
814
815 /* Offset should be less than i_size */
816 if (offset >= i_size_read(inode)) {
817 error = -EINVAL;
818 goto out_unlock;
819 }
820 do_file_insert = 1;
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700821 } else {
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100822 flags |= XFS_PREALLOC_SET;
823
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700824 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
825 offset + len > i_size_read(inode)) {
826 new_size = offset + len;
Dave Chinner24513372014-06-25 14:58:08 +1000827 error = inode_newsize_ok(inode, new_size);
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700828 if (error)
829 goto out_unlock;
830 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100831
Lukas Czerner376ba312014-03-13 19:07:58 +1100832 if (mode & FALLOC_FL_ZERO_RANGE)
833 error = xfs_zero_file_space(ip, offset, len);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -0700834 else {
835 if (mode & FALLOC_FL_UNSHARE_RANGE) {
836 error = xfs_reflink_unshare(ip, offset, len);
837 if (error)
838 goto out_unlock;
839 }
Lukas Czerner376ba312014-03-13 19:07:58 +1100840 error = xfs_alloc_file_space(ip, offset, len,
841 XFS_BMAPI_PREALLOC);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -0700842 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100843 if (error)
844 goto out_unlock;
845 }
846
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700847 if (file->f_flags & O_DSYNC)
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100848 flags |= XFS_PREALLOC_SYNC;
849
850 error = xfs_update_prealloc_flags(ip, flags);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100851 if (error)
852 goto out_unlock;
853
854 /* Change file size if needed */
855 if (new_size) {
856 struct iattr iattr;
857
858 iattr.ia_valid = ATTR_SIZE;
859 iattr.ia_size = new_size;
Jan Kara69bca802016-05-26 14:46:43 +0200860 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
Namjae Jeona904b1c2015-03-25 15:08:56 +1100861 if (error)
862 goto out_unlock;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100863 }
864
Namjae Jeona904b1c2015-03-25 15:08:56 +1100865 /*
866 * Perform hole insertion now that the file size has been
867 * updated so that if we crash during the operation we don't
868 * leave shifted extents past EOF and hence losing access to
869 * the data that is contained within them.
870 */
871 if (do_file_insert)
872 error = xfs_insert_file_space(ip, offset, len);
873
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100874out_unlock:
Christoph Hellwig781355c2015-02-16 11:59:50 +1100875 xfs_iunlock(ip, iolock);
Dave Chinner24513372014-06-25 14:58:08 +1000876 return error;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100877}
878
Darrick J. Wong9fe26042016-10-03 09:11:40 -0700879STATIC ssize_t
880xfs_file_copy_range(
881 struct file *file_in,
882 loff_t pos_in,
883 struct file *file_out,
884 loff_t pos_out,
885 size_t len,
886 unsigned int flags)
887{
888 int error;
889
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +1100890 error = xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
Darrick J. Wongcc714662016-10-03 09:11:41 -0700891 len, false);
Darrick J. Wong9fe26042016-10-03 09:11:40 -0700892 if (error)
893 return error;
894 return len;
895}
896
897STATIC int
898xfs_file_clone_range(
899 struct file *file_in,
900 loff_t pos_in,
901 struct file *file_out,
902 loff_t pos_out,
903 u64 len)
904{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +1100905 return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
Darrick J. Wongcc714662016-10-03 09:11:41 -0700906 len, false);
907}
908
909#define XFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
910STATIC ssize_t
911xfs_file_dedupe_range(
912 struct file *src_file,
913 u64 loff,
914 u64 len,
915 struct file *dst_file,
916 u64 dst_loff)
917{
918 int error;
919
920 /*
921 * Limit the total length we will dedupe for each operation.
922 * This is intended to bound the total time spent in this
923 * ioctl to something sane.
924 */
925 if (len > XFS_MAX_DEDUPE_LEN)
926 len = XFS_MAX_DEDUPE_LEN;
927
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +1100928 error = xfs_reflink_remap_range(src_file, loff, dst_file, dst_loff,
Darrick J. Wongcc714662016-10-03 09:11:41 -0700929 len, true);
930 if (error)
931 return error;
932 return len;
Darrick J. Wong9fe26042016-10-03 09:11:40 -0700933}
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100936xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100938 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100940 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100942 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
943 return -EIO;
944 return 0;
945}
946
947STATIC int
948xfs_dir_open(
949 struct inode *inode,
950 struct file *file)
951{
952 struct xfs_inode *ip = XFS_I(inode);
953 int mode;
954 int error;
955
956 error = xfs_file_open(inode, file);
957 if (error)
958 return error;
959
960 /*
961 * If there are any blocks, read-ahead block 0 as we're almost
962 * certain to have the next operation be a read there.
963 */
Christoph Hellwig309ecac82013-12-06 12:30:09 -0800964 mode = xfs_ilock_data_map_shared(ip);
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100965 if (ip->i_d.di_nextents > 0)
Eric Sandeen9df2dd02014-04-14 19:01:59 +1000966 xfs_dir3_data_readahead(ip, 0, -1);
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100967 xfs_iunlock(ip, mode);
968 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100972xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct inode *inode,
974 struct file *filp)
975{
Dave Chinner24513372014-06-25 14:58:08 +1000976 return xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100980xfs_file_readdir(
Al Virob8227552013-05-22 17:07:56 -0400981 struct file *file,
982 struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Al Virob8227552013-05-22 17:07:56 -0400984 struct inode *inode = file_inode(file);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000985 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000986 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000988 /*
989 * The Linux API doesn't pass down the total size of the buffer
990 * we read into down to the filesystem. With the filldir concept
991 * it's not needed for correct information, but the XFS dir2 leaf
992 * code wants an estimate of the buffer size to calculate it's
993 * readahead window and size the buffers used for mapping to
994 * physical blocks.
995 *
996 * Try to give it an estimate that's good enough, maybe at some
997 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +0000998 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000999 */
Eric Sandeena9cc7992010-02-03 17:50:13 +00001000 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
kbuild test robot83004752014-12-01 08:25:28 +11001002 return xfs_readdir(ip, ctx, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
David Chinner4f57dbc2007-07-19 16:28:17 +10001005/*
Jeff Liud126d432012-08-21 17:11:57 +08001006 * This type is designed to indicate the type of offset we would like
Eric Sandeen49c69592014-09-09 11:56:48 +10001007 * to search from page cache for xfs_seek_hole_data().
Jeff Liud126d432012-08-21 17:11:57 +08001008 */
1009enum {
1010 HOLE_OFF = 0,
1011 DATA_OFF,
1012};
1013
1014/*
1015 * Lookup the desired type of offset from the given page.
1016 *
1017 * On success, return true and the offset argument will point to the
1018 * start of the region that was found. Otherwise this function will
1019 * return false and keep the offset argument unchanged.
1020 */
1021STATIC bool
1022xfs_lookup_buffer_offset(
1023 struct page *page,
1024 loff_t *offset,
1025 unsigned int type)
1026{
1027 loff_t lastoff = page_offset(page);
1028 bool found = false;
1029 struct buffer_head *bh, *head;
1030
1031 bh = head = page_buffers(page);
1032 do {
1033 /*
1034 * Unwritten extents that have data in the page
1035 * cache covering them can be identified by the
1036 * BH_Unwritten state flag. Pages with multiple
1037 * buffers might have a mix of holes, data and
1038 * unwritten extents - any buffer with valid
1039 * data in it should have BH_Uptodate flag set
1040 * on it.
1041 */
1042 if (buffer_unwritten(bh) ||
1043 buffer_uptodate(bh)) {
1044 if (type == DATA_OFF)
1045 found = true;
1046 } else {
1047 if (type == HOLE_OFF)
1048 found = true;
1049 }
1050
1051 if (found) {
1052 *offset = lastoff;
1053 break;
1054 }
1055 lastoff += bh->b_size;
1056 } while ((bh = bh->b_this_page) != head);
1057
1058 return found;
1059}
1060
1061/*
1062 * This routine is called to find out and return a data or hole offset
1063 * from the page cache for unwritten extents according to the desired
Eric Sandeen49c69592014-09-09 11:56:48 +10001064 * type for xfs_seek_hole_data().
Jeff Liud126d432012-08-21 17:11:57 +08001065 *
1066 * The argument offset is used to tell where we start to search from the
1067 * page cache. Map is used to figure out the end points of the range to
1068 * lookup pages.
1069 *
1070 * Return true if the desired type of offset was found, and the argument
1071 * offset is filled with that address. Otherwise, return false and keep
1072 * offset unchanged.
1073 */
1074STATIC bool
1075xfs_find_get_desired_pgoff(
1076 struct inode *inode,
1077 struct xfs_bmbt_irec *map,
1078 unsigned int type,
1079 loff_t *offset)
1080{
1081 struct xfs_inode *ip = XFS_I(inode);
1082 struct xfs_mount *mp = ip->i_mount;
1083 struct pagevec pvec;
1084 pgoff_t index;
1085 pgoff_t end;
1086 loff_t endoff;
1087 loff_t startoff = *offset;
1088 loff_t lastoff = startoff;
1089 bool found = false;
1090
1091 pagevec_init(&pvec, 0);
1092
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001093 index = startoff >> PAGE_SHIFT;
Jeff Liud126d432012-08-21 17:11:57 +08001094 endoff = XFS_FSB_TO_B(mp, map->br_startoff + map->br_blockcount);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001095 end = endoff >> PAGE_SHIFT;
Jeff Liud126d432012-08-21 17:11:57 +08001096 do {
1097 int want;
1098 unsigned nr_pages;
1099 unsigned int i;
1100
1101 want = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
1102 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
1103 want);
1104 /*
1105 * No page mapped into given range. If we are searching holes
1106 * and if this is the first time we got into the loop, it means
1107 * that the given offset is landed in a hole, return it.
1108 *
1109 * If we have already stepped through some block buffers to find
1110 * holes but they all contains data. In this case, the last
1111 * offset is already updated and pointed to the end of the last
1112 * mapped page, if it does not reach the endpoint to search,
1113 * that means there should be a hole between them.
1114 */
1115 if (nr_pages == 0) {
1116 /* Data search found nothing */
1117 if (type == DATA_OFF)
1118 break;
1119
1120 ASSERT(type == HOLE_OFF);
1121 if (lastoff == startoff || lastoff < endoff) {
1122 found = true;
1123 *offset = lastoff;
1124 }
1125 break;
1126 }
1127
1128 /*
1129 * At lease we found one page. If this is the first time we
1130 * step into the loop, and if the first page index offset is
1131 * greater than the given search offset, a hole was found.
1132 */
1133 if (type == HOLE_OFF && lastoff == startoff &&
1134 lastoff < page_offset(pvec.pages[0])) {
1135 found = true;
1136 break;
1137 }
1138
1139 for (i = 0; i < nr_pages; i++) {
1140 struct page *page = pvec.pages[i];
1141 loff_t b_offset;
1142
1143 /*
1144 * At this point, the page may be truncated or
1145 * invalidated (changing page->mapping to NULL),
1146 * or even swizzled back from swapper_space to tmpfs
1147 * file mapping. However, page->index will not change
1148 * because we have a reference on the page.
1149 *
1150 * Searching done if the page index is out of range.
1151 * If the current offset is not reaches the end of
1152 * the specified search range, there should be a hole
1153 * between them.
1154 */
1155 if (page->index > end) {
1156 if (type == HOLE_OFF && lastoff < endoff) {
1157 *offset = lastoff;
1158 found = true;
1159 }
1160 goto out;
1161 }
1162
1163 lock_page(page);
1164 /*
1165 * Page truncated or invalidated(page->mapping == NULL).
1166 * We can freely skip it and proceed to check the next
1167 * page.
1168 */
1169 if (unlikely(page->mapping != inode->i_mapping)) {
1170 unlock_page(page);
1171 continue;
1172 }
1173
1174 if (!page_has_buffers(page)) {
1175 unlock_page(page);
1176 continue;
1177 }
1178
1179 found = xfs_lookup_buffer_offset(page, &b_offset, type);
1180 if (found) {
1181 /*
1182 * The found offset may be less than the start
1183 * point to search if this is the first time to
1184 * come here.
1185 */
1186 *offset = max_t(loff_t, startoff, b_offset);
1187 unlock_page(page);
1188 goto out;
1189 }
1190
1191 /*
1192 * We either searching data but nothing was found, or
1193 * searching hole but found a data buffer. In either
1194 * case, probably the next page contains the desired
1195 * things, update the last offset to it so.
1196 */
1197 lastoff = page_offset(page) + PAGE_SIZE;
1198 unlock_page(page);
1199 }
1200
1201 /*
1202 * The number of returned pages less than our desired, search
1203 * done. In this case, nothing was found for searching data,
1204 * but we found a hole behind the last offset.
1205 */
1206 if (nr_pages < want) {
1207 if (type == HOLE_OFF) {
1208 *offset = lastoff;
1209 found = true;
1210 }
1211 break;
1212 }
1213
1214 index = pvec.pages[i - 1]->index + 1;
1215 pagevec_release(&pvec);
1216 } while (index <= end);
1217
1218out:
1219 pagevec_release(&pvec);
1220 return found;
1221}
1222
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001223/*
1224 * caller must lock inode with xfs_ilock_data_map_shared,
1225 * can we craft an appropriate ASSERT?
1226 *
1227 * end is because the VFS-level lseek interface is defined such that any
1228 * offset past i_size shall return -ENXIO, but we use this for quota code
1229 * which does not maintain i_size, and we want to SEEK_DATA past i_size.
1230 */
1231loff_t
1232__xfs_seek_hole_data(
1233 struct inode *inode,
Eric Sandeen49c69592014-09-09 11:56:48 +10001234 loff_t start,
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001235 loff_t end,
Eric Sandeen49c69592014-09-09 11:56:48 +10001236 int whence)
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001237{
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001238 struct xfs_inode *ip = XFS_I(inode);
1239 struct xfs_mount *mp = ip->i_mount;
1240 loff_t uninitialized_var(offset);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001241 xfs_fileoff_t fsbno;
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001242 xfs_filblks_t lastbno;
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001243 int error;
1244
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001245 if (start >= end) {
Dave Chinner24513372014-06-25 14:58:08 +10001246 error = -ENXIO;
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001247 goto out_error;
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001248 }
1249
Eric Sandeen49c69592014-09-09 11:56:48 +10001250 /*
1251 * Try to read extents from the first block indicated
1252 * by fsbno to the end block of the file.
1253 */
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001254 fsbno = XFS_B_TO_FSBT(mp, start);
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001255 lastbno = XFS_B_TO_FSB(mp, end);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001256
Jeff Liub686d1f2012-08-21 17:12:18 +08001257 for (;;) {
1258 struct xfs_bmbt_irec map[2];
1259 int nmap = 2;
1260 unsigned int i;
1261
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001262 error = xfs_bmapi_read(ip, fsbno, lastbno - fsbno, map, &nmap,
Jeff Liub686d1f2012-08-21 17:12:18 +08001263 XFS_BMAPI_ENTIRE);
1264 if (error)
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001265 goto out_error;
Jeff Liub686d1f2012-08-21 17:12:18 +08001266
1267 /* No extents at given offset, must be beyond EOF */
1268 if (nmap == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001269 error = -ENXIO;
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001270 goto out_error;
Jeff Liub686d1f2012-08-21 17:12:18 +08001271 }
1272
1273 for (i = 0; i < nmap; i++) {
1274 offset = max_t(loff_t, start,
1275 XFS_FSB_TO_B(mp, map[i].br_startoff));
1276
Eric Sandeen49c69592014-09-09 11:56:48 +10001277 /* Landed in the hole we wanted? */
1278 if (whence == SEEK_HOLE &&
1279 map[i].br_startblock == HOLESTARTBLOCK)
1280 goto out;
1281
1282 /* Landed in the data extent we wanted? */
1283 if (whence == SEEK_DATA &&
1284 (map[i].br_startblock == DELAYSTARTBLOCK ||
1285 (map[i].br_state == XFS_EXT_NORM &&
1286 !isnullstartblock(map[i].br_startblock))))
Jeff Liub686d1f2012-08-21 17:12:18 +08001287 goto out;
1288
1289 /*
Eric Sandeen49c69592014-09-09 11:56:48 +10001290 * Landed in an unwritten extent, try to search
1291 * for hole or data from page cache.
Jeff Liub686d1f2012-08-21 17:12:18 +08001292 */
1293 if (map[i].br_state == XFS_EXT_UNWRITTEN) {
1294 if (xfs_find_get_desired_pgoff(inode, &map[i],
Eric Sandeen49c69592014-09-09 11:56:48 +10001295 whence == SEEK_HOLE ? HOLE_OFF : DATA_OFF,
1296 &offset))
Jeff Liub686d1f2012-08-21 17:12:18 +08001297 goto out;
1298 }
1299 }
1300
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001301 /*
Eric Sandeen49c69592014-09-09 11:56:48 +10001302 * We only received one extent out of the two requested. This
1303 * means we've hit EOF and didn't find what we are looking for.
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001304 */
Jeff Liub686d1f2012-08-21 17:12:18 +08001305 if (nmap == 1) {
Eric Sandeen49c69592014-09-09 11:56:48 +10001306 /*
1307 * If we were looking for a hole, set offset to
1308 * the end of the file (i.e., there is an implicit
1309 * hole at the end of any file).
1310 */
1311 if (whence == SEEK_HOLE) {
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001312 offset = end;
Eric Sandeen49c69592014-09-09 11:56:48 +10001313 break;
1314 }
1315 /*
1316 * If we were looking for data, it's nowhere to be found
1317 */
1318 ASSERT(whence == SEEK_DATA);
1319 error = -ENXIO;
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001320 goto out_error;
Jeff Liub686d1f2012-08-21 17:12:18 +08001321 }
1322
1323 ASSERT(i > 1);
1324
1325 /*
Eric Sandeen49c69592014-09-09 11:56:48 +10001326 * Nothing was found, proceed to the next round of search
1327 * if the next reading offset is not at or beyond EOF.
Jeff Liub686d1f2012-08-21 17:12:18 +08001328 */
1329 fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
1330 start = XFS_FSB_TO_B(mp, fsbno);
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001331 if (start >= end) {
Eric Sandeen49c69592014-09-09 11:56:48 +10001332 if (whence == SEEK_HOLE) {
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001333 offset = end;
Eric Sandeen49c69592014-09-09 11:56:48 +10001334 break;
1335 }
1336 ASSERT(whence == SEEK_DATA);
1337 error = -ENXIO;
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001338 goto out_error;
Jeff Liub686d1f2012-08-21 17:12:18 +08001339 }
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001340 }
1341
Jeff Liub686d1f2012-08-21 17:12:18 +08001342out:
1343 /*
Eric Sandeen49c69592014-09-09 11:56:48 +10001344 * If at this point we have found the hole we wanted, the returned
Jeff Liub686d1f2012-08-21 17:12:18 +08001345 * offset may be bigger than the file size as it may be aligned to
Eric Sandeen49c69592014-09-09 11:56:48 +10001346 * page boundary for unwritten extents. We need to deal with this
Jeff Liub686d1f2012-08-21 17:12:18 +08001347 * situation in particular.
1348 */
Eric Sandeen49c69592014-09-09 11:56:48 +10001349 if (whence == SEEK_HOLE)
Eric Sandeen8aa7d372016-02-08 11:25:16 +11001350 offset = min_t(loff_t, offset, end);
1351
1352 return offset;
1353
1354out_error:
1355 return error;
1356}
1357
1358STATIC loff_t
1359xfs_seek_hole_data(
1360 struct file *file,
1361 loff_t start,
1362 int whence)
1363{
1364 struct inode *inode = file->f_mapping->host;
1365 struct xfs_inode *ip = XFS_I(inode);
1366 struct xfs_mount *mp = ip->i_mount;
1367 uint lock;
1368 loff_t offset, end;
1369 int error = 0;
1370
1371 if (XFS_FORCED_SHUTDOWN(mp))
1372 return -EIO;
1373
1374 lock = xfs_ilock_data_map_shared(ip);
1375
1376 end = i_size_read(inode);
1377 offset = __xfs_seek_hole_data(inode, start, end, whence);
1378 if (offset < 0) {
1379 error = offset;
1380 goto out_unlock;
1381 }
1382
Jie Liu46a1c2c2013-06-25 12:02:13 +08001383 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001384
1385out_unlock:
Christoph Hellwig01f4f322013-12-06 12:30:08 -08001386 xfs_iunlock(ip, lock);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001387
1388 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10001389 return error;
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001390 return offset;
1391}
1392
1393STATIC loff_t
1394xfs_file_llseek(
1395 struct file *file,
1396 loff_t offset,
Eric Sandeen59f9c002014-09-09 11:57:10 +10001397 int whence)
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001398{
Eric Sandeen59f9c002014-09-09 11:57:10 +10001399 switch (whence) {
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001400 case SEEK_END:
1401 case SEEK_CUR:
1402 case SEEK_SET:
Eric Sandeen59f9c002014-09-09 11:57:10 +10001403 return generic_file_llseek(file, offset, whence);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001404 case SEEK_HOLE:
Eric Sandeen49c69592014-09-09 11:56:48 +10001405 case SEEK_DATA:
Eric Sandeen59f9c002014-09-09 11:57:10 +10001406 return xfs_seek_hole_data(file, offset, whence);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001407 default:
1408 return -EINVAL;
1409 }
1410}
1411
Dave Chinnerde0e8c22015-02-23 21:44:19 +11001412/*
1413 * Locking for serialisation of IO during page faults. This results in a lock
1414 * ordering of:
1415 *
1416 * mmap_sem (MM)
Dave Chinner6b698ed2015-06-04 09:18:53 +10001417 * sb_start_pagefault(vfs, freeze)
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001418 * i_mmaplock (XFS - truncate serialisation)
Dave Chinner6b698ed2015-06-04 09:18:53 +10001419 * page_lock (MM)
1420 * i_lock (XFS - extent map serialisation)
Dave Chinnerde0e8c22015-02-23 21:44:19 +11001421 */
Dave Chinnerde0e8c22015-02-23 21:44:19 +11001422
Dave Chinner075a9242015-02-23 21:44:54 +11001423/*
1424 * mmap()d file has taken write protection fault and is being made writable. We
1425 * can set the page state up correctly for a writable page, which means we can
1426 * do correct delalloc accounting (ENOSPC checking!) and unwritten extent
1427 * mapping.
1428 */
1429STATIC int
1430xfs_filemap_page_mkwrite(
1431 struct vm_area_struct *vma,
1432 struct vm_fault *vmf)
1433{
Dave Chinner6b698ed2015-06-04 09:18:53 +10001434 struct inode *inode = file_inode(vma->vm_file);
Dave Chinnerec56b1f2015-06-04 09:18:18 +10001435 int ret;
Dave Chinner075a9242015-02-23 21:44:54 +11001436
Dave Chinner6b698ed2015-06-04 09:18:53 +10001437 trace_xfs_filemap_page_mkwrite(XFS_I(inode));
Dave Chinner075a9242015-02-23 21:44:54 +11001438
Dave Chinner6b698ed2015-06-04 09:18:53 +10001439 sb_start_pagefault(inode->i_sb);
Dave Chinnerec56b1f2015-06-04 09:18:18 +10001440 file_update_time(vma->vm_file);
Dave Chinner6b698ed2015-06-04 09:18:53 +10001441 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1442
1443 if (IS_DAX(inode)) {
Ross Zwisler11c59c92016-11-08 11:32:46 +11001444 ret = dax_iomap_fault(vma, vmf, &xfs_iomap_ops);
Dave Chinner6b698ed2015-06-04 09:18:53 +10001445 } else {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001446 ret = iomap_page_mkwrite(vma, vmf, &xfs_iomap_ops);
Dave Chinner6b698ed2015-06-04 09:18:53 +10001447 ret = block_page_mkwrite_return(ret);
1448 }
1449
1450 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1451 sb_end_pagefault(inode->i_sb);
1452
1453 return ret;
1454}
1455
1456STATIC int
1457xfs_filemap_fault(
1458 struct vm_area_struct *vma,
1459 struct vm_fault *vmf)
1460{
Dave Chinnerb2442c52015-07-29 11:48:00 +10001461 struct inode *inode = file_inode(vma->vm_file);
Dave Chinner6b698ed2015-06-04 09:18:53 +10001462 int ret;
1463
Dave Chinnerb2442c52015-07-29 11:48:00 +10001464 trace_xfs_filemap_fault(XFS_I(inode));
Dave Chinner6b698ed2015-06-04 09:18:53 +10001465
1466 /* DAX can shortcut the normal fault path on write faults! */
Dave Chinnerb2442c52015-07-29 11:48:00 +10001467 if ((vmf->flags & FAULT_FLAG_WRITE) && IS_DAX(inode))
Dave Chinner6b698ed2015-06-04 09:18:53 +10001468 return xfs_filemap_page_mkwrite(vma, vmf);
Dave Chinner075a9242015-02-23 21:44:54 +11001469
Dave Chinnerb2442c52015-07-29 11:48:00 +10001470 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1471 if (IS_DAX(inode)) {
1472 /*
1473 * we do not want to trigger unwritten extent conversion on read
1474 * faults - that is unnecessary overhead and would also require
1475 * changes to xfs_get_blocks_direct() to map unwritten extent
1476 * ioend for conversion on read-only mappings.
1477 */
Ross Zwisler11c59c92016-11-08 11:32:46 +11001478 ret = dax_iomap_fault(vma, vmf, &xfs_iomap_ops);
Dave Chinnerb2442c52015-07-29 11:48:00 +10001479 } else
1480 ret = filemap_fault(vma, vmf);
1481 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
Dave Chinner075a9242015-02-23 21:44:54 +11001482
Dave Chinner6b698ed2015-06-04 09:18:53 +10001483 return ret;
1484}
1485
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001486/*
1487 * Similar to xfs_filemap_fault(), the DAX fault path can call into here on
1488 * both read and write faults. Hence we need to handle both cases. There is no
1489 * ->pmd_mkwrite callout for huge pages, so we have a single function here to
1490 * handle both cases here. @flags carries the information on the type of fault
1491 * occuring.
1492 */
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001493STATIC int
1494xfs_filemap_pmd_fault(
1495 struct vm_area_struct *vma,
1496 unsigned long addr,
1497 pmd_t *pmd,
1498 unsigned int flags)
1499{
1500 struct inode *inode = file_inode(vma->vm_file);
1501 struct xfs_inode *ip = XFS_I(inode);
1502 int ret;
1503
1504 if (!IS_DAX(inode))
1505 return VM_FAULT_FALLBACK;
1506
1507 trace_xfs_filemap_pmd_fault(ip);
1508
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001509 if (flags & FAULT_FLAG_WRITE) {
1510 sb_start_pagefault(inode->i_sb);
1511 file_update_time(vma->vm_file);
1512 }
1513
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001514 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
Ross Zwisler862f1b92016-11-08 11:35:02 +11001515 ret = dax_iomap_pmd_fault(vma, addr, pmd, flags, &xfs_iomap_ops);
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001516 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
Dave Chinner13ad4fe2015-11-03 12:37:02 +11001517
1518 if (flags & FAULT_FLAG_WRITE)
1519 sb_end_pagefault(inode->i_sb);
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001520
1521 return ret;
1522}
1523
Dave Chinner3af49282015-11-03 12:37:02 +11001524/*
1525 * pfn_mkwrite was originally inteneded to ensure we capture time stamp
1526 * updates on write faults. In reality, it's need to serialise against
Ross Zwisler5eb88dc2016-01-22 15:10:56 -08001527 * truncate similar to page_mkwrite. Hence we cycle the XFS_MMAPLOCK_SHARED
1528 * to ensure we serialise the fault barrier in place.
Dave Chinner3af49282015-11-03 12:37:02 +11001529 */
1530static int
1531xfs_filemap_pfn_mkwrite(
1532 struct vm_area_struct *vma,
1533 struct vm_fault *vmf)
1534{
1535
1536 struct inode *inode = file_inode(vma->vm_file);
1537 struct xfs_inode *ip = XFS_I(inode);
1538 int ret = VM_FAULT_NOPAGE;
1539 loff_t size;
1540
1541 trace_xfs_filemap_pfn_mkwrite(ip);
1542
1543 sb_start_pagefault(inode->i_sb);
1544 file_update_time(vma->vm_file);
1545
1546 /* check if the faulting page hasn't raced with truncate */
1547 xfs_ilock(ip, XFS_MMAPLOCK_SHARED);
1548 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1549 if (vmf->pgoff >= size)
1550 ret = VM_FAULT_SIGBUS;
Ross Zwisler5eb88dc2016-01-22 15:10:56 -08001551 else if (IS_DAX(inode))
1552 ret = dax_pfn_mkwrite(vma, vmf);
Dave Chinner3af49282015-11-03 12:37:02 +11001553 xfs_iunlock(ip, XFS_MMAPLOCK_SHARED);
1554 sb_end_pagefault(inode->i_sb);
1555 return ret;
1556
1557}
1558
Dave Chinner6b698ed2015-06-04 09:18:53 +10001559static const struct vm_operations_struct xfs_file_vm_ops = {
1560 .fault = xfs_filemap_fault,
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001561 .pmd_fault = xfs_filemap_pmd_fault,
Dave Chinner6b698ed2015-06-04 09:18:53 +10001562 .map_pages = filemap_map_pages,
1563 .page_mkwrite = xfs_filemap_page_mkwrite,
Dave Chinner3af49282015-11-03 12:37:02 +11001564 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
Dave Chinner6b698ed2015-06-04 09:18:53 +10001565};
1566
1567STATIC int
1568xfs_file_mmap(
1569 struct file *filp,
1570 struct vm_area_struct *vma)
1571{
1572 file_accessed(filp);
1573 vma->vm_ops = &xfs_file_vm_ops;
1574 if (IS_DAX(file_inode(filp)))
Matthew Wilcoxacd76e72015-09-08 14:59:06 -07001575 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
Dave Chinner6b698ed2015-06-04 09:18:53 +10001576 return 0;
Dave Chinner075a9242015-02-23 21:44:54 +11001577}
1578
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001579const struct file_operations xfs_file_operations = {
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001580 .llseek = xfs_file_llseek,
Al Virob4f5d2c2014-04-02 14:37:59 -04001581 .read_iter = xfs_file_read_iter,
Al Virobf97f3bc2014-04-03 14:20:23 -04001582 .write_iter = xfs_file_write_iter,
Al Viro82c156f2016-09-22 23:35:42 -04001583 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001584 .splice_write = iter_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001585 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001587 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001589 .mmap = xfs_file_mmap,
1590 .open = xfs_file_open,
1591 .release = xfs_file_release,
1592 .fsync = xfs_file_fsync,
Toshi Kanidbe6ec82016-10-07 16:59:59 -07001593 .get_unmapped_area = thp_get_unmapped_area,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001594 .fallocate = xfs_file_fallocate,
Darrick J. Wong9fe26042016-10-03 09:11:40 -07001595 .copy_file_range = xfs_file_copy_range,
1596 .clone_file_range = xfs_file_clone_range,
Darrick J. Wongcc714662016-10-03 09:11:41 -07001597 .dedupe_file_range = xfs_file_dedupe_range,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598};
1599
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001600const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001601 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -04001603 .iterate_shared = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -04001604 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +11001605 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001606#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001607 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001608#endif
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +00001609 .fsync = xfs_dir_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610};