]> Git Repo - linux.git/blame - fs/xfs/xfs_file.c
Merge tag 'kvmarm-fixes-5.11-2' into kvmarm-master/next
[linux.git] / fs / xfs / xfs_file.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769
NS
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4 6#include "xfs.h"
dda35b8f 7#include "xfs_fs.h"
70a9883c 8#include "xfs_shared.h"
a4fbe6ab 9#include "xfs_format.h"
239880ef
DC
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
1da177e4 12#include "xfs_mount.h"
1da177e4 13#include "xfs_inode.h"
239880ef 14#include "xfs_trans.h"
fd3200be 15#include "xfs_inode_item.h"
dda35b8f 16#include "xfs_bmap.h"
c24b5dfa 17#include "xfs_bmap_util.h"
2b9ab5ab 18#include "xfs_dir2.h"
c24b5dfa 19#include "xfs_dir2_priv.h"
ddcd856d 20#include "xfs_ioctl.h"
dda35b8f 21#include "xfs_trace.h"
239880ef 22#include "xfs_log.h"
dc06f398 23#include "xfs_icache.h"
781355c6 24#include "xfs_pnfs.h"
68a9f5e7 25#include "xfs_iomap.h"
0613f16c 26#include "xfs_reflink.h"
1da177e4 27
2fe17c10 28#include <linux/falloc.h>
66114cad 29#include <linux/backing-dev.h>
a39e596b 30#include <linux/mman.h>
40144e49 31#include <linux/fadvise.h>
1da177e4 32
f0f37e2f 33static const struct vm_operations_struct xfs_file_vm_ops;
1da177e4 34
25219dbf
DW
35/*
36 * Decide if the given file range is aligned to the size of the fundamental
37 * allocation unit for the file.
38 */
39static bool
40xfs_is_falloc_aligned(
41 struct xfs_inode *ip,
42 loff_t pos,
43 long long int len)
44{
45 struct xfs_mount *mp = ip->i_mount;
46 uint64_t mask;
47
48 if (XFS_IS_REALTIME_INODE(ip)) {
49 if (!is_power_of_2(mp->m_sb.sb_rextsize)) {
50 u64 rextbytes;
51 u32 mod;
52
53 rextbytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
54 div_u64_rem(pos, rextbytes, &mod);
55 if (mod)
56 return false;
57 div_u64_rem(len, rextbytes, &mod);
58 return mod == 0;
59 }
60 mask = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize) - 1;
61 } else {
62 mask = mp->m_sb.sb_blocksize - 1;
63 }
64
65 return !((pos | len) & mask);
66}
67
8add71ca
CH
68int
69xfs_update_prealloc_flags(
70 struct xfs_inode *ip,
71 enum xfs_prealloc_flags flags)
72{
73 struct xfs_trans *tp;
74 int error;
75
253f4911
CH
76 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
77 0, 0, 0, &tp);
78 if (error)
8add71ca 79 return error;
8add71ca
CH
80
81 xfs_ilock(ip, XFS_ILOCK_EXCL);
82 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
83
84 if (!(flags & XFS_PREALLOC_INVISIBLE)) {
c19b3b05
DC
85 VFS_I(ip)->i_mode &= ~S_ISUID;
86 if (VFS_I(ip)->i_mode & S_IXGRP)
87 VFS_I(ip)->i_mode &= ~S_ISGID;
8add71ca
CH
88 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
89 }
90
91 if (flags & XFS_PREALLOC_SET)
92 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
93 if (flags & XFS_PREALLOC_CLEAR)
94 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
95
96 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
97 if (flags & XFS_PREALLOC_SYNC)
98 xfs_trans_set_sync(tp);
70393313 99 return xfs_trans_commit(tp);
8add71ca
CH
100}
101
1da2f2db
CH
102/*
103 * Fsync operations on directories are much simpler than on regular files,
104 * as there is no file data to flush, and thus also no need for explicit
105 * cache flush operations, and there are no non-transaction metadata updates
106 * on directories either.
107 */
108STATIC int
109xfs_dir_fsync(
110 struct file *file,
111 loff_t start,
112 loff_t end,
113 int datasync)
114{
115 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
1da2f2db
CH
116
117 trace_xfs_dir_fsync(ip);
54fbdd10 118 return xfs_log_force_inode(ip);
1da2f2db
CH
119}
120
fd3200be
CH
121STATIC int
122xfs_file_fsync(
123 struct file *file,
02c24a82
JB
124 loff_t start,
125 loff_t end,
fd3200be
CH
126 int datasync)
127{
7ea80859
CH
128 struct inode *inode = file->f_mapping->host;
129 struct xfs_inode *ip = XFS_I(inode);
1319ebef 130 struct xfs_inode_log_item *iip = ip->i_itemp;
a27a263b 131 struct xfs_mount *mp = ip->i_mount;
fd3200be
CH
132 int error = 0;
133 int log_flushed = 0;
b1037058 134 xfs_lsn_t lsn = 0;
fd3200be 135
cca28fb8 136 trace_xfs_file_fsync(ip);
fd3200be 137
1b180274 138 error = file_write_and_wait_range(file, start, end);
02c24a82
JB
139 if (error)
140 return error;
141
a27a263b 142 if (XFS_FORCED_SHUTDOWN(mp))
b474c7ae 143 return -EIO;
fd3200be
CH
144
145 xfs_iflags_clear(ip, XFS_ITRUNCATED);
146
2291dab2
DC
147 /*
148 * If we have an RT and/or log subvolume we need to make sure to flush
149 * the write cache the device used for file data first. This is to
150 * ensure newly written file data make it to disk before logging the new
151 * inode size in case of an extending write.
152 */
153 if (XFS_IS_REALTIME_INODE(ip))
154 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
155 else if (mp->m_logdev_targp != mp->m_ddev_targp)
156 xfs_blkdev_issue_flush(mp->m_ddev_targp);
a27a263b 157
fd3200be 158 /*
fc0561ce
DC
159 * All metadata updates are logged, which means that we just have to
160 * flush the log up to the latest LSN that touched the inode. If we have
161 * concurrent fsync/fdatasync() calls, we need them to all block on the
162 * log force before we clear the ili_fsync_fields field. This ensures
163 * that we don't get a racing sync operation that does not wait for the
164 * metadata to hit the journal before returning. If we race with
165 * clearing the ili_fsync_fields, then all that will happen is the log
166 * force will do nothing as the lsn will already be on disk. We can't
167 * race with setting ili_fsync_fields because that is done under
168 * XFS_ILOCK_EXCL, and that can't happen because we hold the lock shared
169 * until after the ili_fsync_fields is cleared.
fd3200be
CH
170 */
171 xfs_ilock(ip, XFS_ILOCK_SHARED);
8f639dde
CH
172 if (xfs_ipincount(ip)) {
173 if (!datasync ||
1319ebef
DC
174 (iip->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
175 lsn = iip->ili_last_lsn;
8f639dde 176 }
fd3200be 177
fc0561ce 178 if (lsn) {
656de4ff 179 error = xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
1319ebef
DC
180 spin_lock(&iip->ili_lock);
181 iip->ili_fsync_fields = 0;
182 spin_unlock(&iip->ili_lock);
fc0561ce
DC
183 }
184 xfs_iunlock(ip, XFS_ILOCK_SHARED);
b1037058 185
a27a263b
CH
186 /*
187 * If we only have a single device, and the log force about was
188 * a no-op we might have to flush the data device cache here.
189 * This can only happen for fdatasync/O_DSYNC if we were overwriting
190 * an already allocated file and thus do not have any metadata to
191 * commit.
192 */
2291dab2
DC
193 if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
194 mp->m_logdev_targp == mp->m_ddev_targp)
a27a263b 195 xfs_blkdev_issue_flush(mp->m_ddev_targp);
fd3200be 196
2451337d 197 return error;
fd3200be
CH
198}
199
00258e36 200STATIC ssize_t
bbc5a740 201xfs_file_dio_aio_read(
dda35b8f 202 struct kiocb *iocb,
b4f5d2c6 203 struct iov_iter *to)
dda35b8f 204{
acdda3aa 205 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
bbc5a740 206 size_t count = iov_iter_count(to);
acdda3aa 207 ssize_t ret;
dda35b8f 208
bbc5a740 209 trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
dda35b8f 210
f1285ff0
CH
211 if (!count)
212 return 0; /* skip atime */
dda35b8f 213
a447d7cd
CH
214 file_accessed(iocb->ki_filp);
215
7b53b868
CH
216 if (iocb->ki_flags & IOCB_NOWAIT) {
217 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
218 return -EAGAIN;
219 } else {
220 xfs_ilock(ip, XFS_IOLOCK_SHARED);
221 }
690c2a38
CH
222 ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
223 is_sync_kiocb(iocb));
65523218 224 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
acdda3aa 225
16d4d435
CH
226 return ret;
227}
228
f021bd07 229static noinline ssize_t
16d4d435
CH
230xfs_file_dax_read(
231 struct kiocb *iocb,
232 struct iov_iter *to)
233{
6c31f495 234 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
16d4d435
CH
235 size_t count = iov_iter_count(to);
236 ssize_t ret = 0;
237
238 trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
239
240 if (!count)
241 return 0; /* skip atime */
242
942491c9
CH
243 if (iocb->ki_flags & IOCB_NOWAIT) {
244 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
29a5d29e 245 return -EAGAIN;
942491c9 246 } else {
29a5d29e
GR
247 xfs_ilock(ip, XFS_IOLOCK_SHARED);
248 }
942491c9 249
690c2a38 250 ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
65523218 251 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
bbc5a740 252
f1285ff0 253 file_accessed(iocb->ki_filp);
bbc5a740
CH
254 return ret;
255}
256
257STATIC ssize_t
258xfs_file_buffered_aio_read(
259 struct kiocb *iocb,
260 struct iov_iter *to)
261{
262 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
263 ssize_t ret;
264
265 trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
dda35b8f 266
942491c9
CH
267 if (iocb->ki_flags & IOCB_NOWAIT) {
268 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
91f9943e 269 return -EAGAIN;
942491c9 270 } else {
91f9943e
CH
271 xfs_ilock(ip, XFS_IOLOCK_SHARED);
272 }
b4f5d2c6 273 ret = generic_file_read_iter(iocb, to);
65523218 274 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
bbc5a740
CH
275
276 return ret;
277}
278
279STATIC ssize_t
280xfs_file_read_iter(
281 struct kiocb *iocb,
282 struct iov_iter *to)
283{
16d4d435
CH
284 struct inode *inode = file_inode(iocb->ki_filp);
285 struct xfs_mount *mp = XFS_I(inode)->i_mount;
bbc5a740
CH
286 ssize_t ret = 0;
287
288 XFS_STATS_INC(mp, xs_read_calls);
289
290 if (XFS_FORCED_SHUTDOWN(mp))
291 return -EIO;
292
16d4d435
CH
293 if (IS_DAX(inode))
294 ret = xfs_file_dax_read(iocb, to);
295 else if (iocb->ki_flags & IOCB_DIRECT)
bbc5a740 296 ret = xfs_file_dio_aio_read(iocb, to);
3176c3e0 297 else
bbc5a740 298 ret = xfs_file_buffered_aio_read(iocb, to);
dda35b8f 299
dda35b8f 300 if (ret > 0)
ff6d6af2 301 XFS_STATS_ADD(mp, xs_read_bytes, ret);
dda35b8f
CH
302 return ret;
303}
304
4d8d1581
DC
305/*
306 * Common pre-write limit and setup checks.
307 *
5bf1f262
CH
308 * Called with the iolocked held either shared and exclusive according to
309 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
310 * if called for a direct write beyond i_size.
4d8d1581
DC
311 */
312STATIC ssize_t
313xfs_file_aio_write_checks(
99733fa3
AV
314 struct kiocb *iocb,
315 struct iov_iter *from,
4d8d1581
DC
316 int *iolock)
317{
99733fa3 318 struct file *file = iocb->ki_filp;
4d8d1581
DC
319 struct inode *inode = file->f_mapping->host;
320 struct xfs_inode *ip = XFS_I(inode);
3309dd04 321 ssize_t error = 0;
99733fa3 322 size_t count = iov_iter_count(from);
3136e8bb 323 bool drained_dio = false;
f5c54717 324 loff_t isize;
4d8d1581 325
7271d243 326restart:
3309dd04
AV
327 error = generic_write_checks(iocb, from);
328 if (error <= 0)
4d8d1581 329 return error;
4d8d1581 330
69eb5fa1 331 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
781355c6
CH
332 if (error)
333 return error;
334
65523218
CH
335 /*
336 * For changing security info in file_remove_privs() we need i_rwsem
337 * exclusively.
338 */
a6de82ca 339 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
65523218 340 xfs_iunlock(ip, *iolock);
a6de82ca 341 *iolock = XFS_IOLOCK_EXCL;
65523218 342 xfs_ilock(ip, *iolock);
a6de82ca
JK
343 goto restart;
344 }
4d8d1581
DC
345 /*
346 * If the offset is beyond the size of the file, we need to zero any
347 * blocks that fall between the existing EOF and the start of this
2813d682 348 * write. If zeroing is needed and we are currently holding the
467f7899
CH
349 * iolock shared, we need to update it to exclusive which implies
350 * having to redo all checks before.
b9d59846
DC
351 *
352 * We need to serialise against EOF updates that occur in IO
353 * completions here. We want to make sure that nobody is changing the
354 * size while we do this check until we have placed an IO barrier (i.e.
355 * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
356 * The spinlock effectively forms a memory barrier once we have the
357 * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
358 * and hence be able to correctly determine if we need to run zeroing.
4d8d1581 359 */
b9d59846 360 spin_lock(&ip->i_flags_lock);
f5c54717
CH
361 isize = i_size_read(inode);
362 if (iocb->ki_pos > isize) {
b9d59846 363 spin_unlock(&ip->i_flags_lock);
3136e8bb
BF
364 if (!drained_dio) {
365 if (*iolock == XFS_IOLOCK_SHARED) {
65523218 366 xfs_iunlock(ip, *iolock);
3136e8bb 367 *iolock = XFS_IOLOCK_EXCL;
65523218 368 xfs_ilock(ip, *iolock);
3136e8bb
BF
369 iov_iter_reexpand(from, count);
370 }
40c63fbc
DC
371 /*
372 * We now have an IO submission barrier in place, but
373 * AIO can do EOF updates during IO completion and hence
374 * we now need to wait for all of them to drain. Non-AIO
375 * DIO will have drained before we are given the
376 * XFS_IOLOCK_EXCL, and so for most cases this wait is a
377 * no-op.
378 */
379 inode_dio_wait(inode);
3136e8bb 380 drained_dio = true;
7271d243
DC
381 goto restart;
382 }
f5c54717
CH
383
384 trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
385 error = iomap_zero_range(inode, isize, iocb->ki_pos - isize,
f150b423 386 NULL, &xfs_buffered_write_iomap_ops);
467f7899
CH
387 if (error)
388 return error;
b9d59846
DC
389 } else
390 spin_unlock(&ip->i_flags_lock);
4d8d1581 391
8a9c9980
CH
392 /*
393 * Updating the timestamps will grab the ilock again from
394 * xfs_fs_dirty_inode, so we have to call it after dropping the
395 * lock above. Eventually we should look into a way to avoid
396 * the pointless lock roundtrip.
397 */
8c3f406c 398 return file_modified(file);
4d8d1581
DC
399}
400
acdda3aa
CH
401static int
402xfs_dio_write_end_io(
403 struct kiocb *iocb,
404 ssize_t size,
6fe7b990 405 int error,
acdda3aa
CH
406 unsigned flags)
407{
408 struct inode *inode = file_inode(iocb->ki_filp);
409 struct xfs_inode *ip = XFS_I(inode);
410 loff_t offset = iocb->ki_pos;
73d30d48 411 unsigned int nofs_flag;
acdda3aa
CH
412
413 trace_xfs_end_io_direct_write(ip, offset, size);
414
415 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
416 return -EIO;
417
6fe7b990
MB
418 if (error)
419 return error;
420 if (!size)
421 return 0;
acdda3aa 422
ed5c3e66
DC
423 /*
424 * Capture amount written on completion as we can't reliably account
425 * for it on submission.
426 */
427 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
428
73d30d48
CH
429 /*
430 * We can allocate memory here while doing writeback on behalf of
431 * memory reclaim. To avoid memory allocation deadlocks set the
432 * task-wide nofs context for the following operations.
433 */
434 nofs_flag = memalloc_nofs_save();
435
ee70daab
EG
436 if (flags & IOMAP_DIO_COW) {
437 error = xfs_reflink_end_cow(ip, offset, size);
438 if (error)
73d30d48 439 goto out;
ee70daab
EG
440 }
441
442 /*
443 * Unwritten conversion updates the in-core isize after extent
444 * conversion but before updating the on-disk size. Updating isize any
445 * earlier allows a racing dio read to find unwritten extents before
446 * they are converted.
447 */
73d30d48
CH
448 if (flags & IOMAP_DIO_UNWRITTEN) {
449 error = xfs_iomap_write_unwritten(ip, offset, size, true);
450 goto out;
451 }
ee70daab 452
acdda3aa
CH
453 /*
454 * We need to update the in-core inode size here so that we don't end up
455 * with the on-disk inode size being outside the in-core inode size. We
456 * have no other method of updating EOF for AIO, so always do it here
457 * if necessary.
458 *
459 * We need to lock the test/set EOF update as we can be racing with
460 * other IO completions here to update the EOF. Failing to serialise
461 * here can result in EOF moving backwards and Bad Things Happen when
462 * that occurs.
463 */
464 spin_lock(&ip->i_flags_lock);
465 if (offset + size > i_size_read(inode)) {
466 i_size_write(inode, offset + size);
ee70daab 467 spin_unlock(&ip->i_flags_lock);
acdda3aa 468 error = xfs_setfilesize(ip, offset, size);
ee70daab
EG
469 } else {
470 spin_unlock(&ip->i_flags_lock);
471 }
acdda3aa 472
73d30d48
CH
473out:
474 memalloc_nofs_restore(nofs_flag);
acdda3aa
CH
475 return error;
476}
477
838c4f3d
CH
478static const struct iomap_dio_ops xfs_dio_write_ops = {
479 .end_io = xfs_dio_write_end_io,
480};
481
f0d26e86
DC
482/*
483 * xfs_file_dio_aio_write - handle direct IO writes
484 *
485 * Lock the inode appropriately to prepare for and issue a direct IO write.
eda77982 486 * By separating it from the buffered write path we remove all the tricky to
f0d26e86
DC
487 * follow locking changes and looping.
488 *
eda77982
DC
489 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
490 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
491 * pages are flushed out.
492 *
493 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
494 * allowing them to be done in parallel with reads and other direct IO writes.
495 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
496 * needs to do sub-block zeroing and that requires serialisation against other
497 * direct IOs to the same block. In this case we need to serialise the
498 * submission of the unaligned IOs so that we don't get racing block zeroing in
499 * the dio layer. To avoid the problem with aio, we also need to wait for
500 * outstanding IOs to complete so that unwritten extent conversion is completed
501 * before we try to map the overlapping block. This is currently implemented by
4a06fd26 502 * hitting it with a big hammer (i.e. inode_dio_wait()).
eda77982 503 *
f0d26e86
DC
504 * Returns with locks held indicated by @iolock and errors indicated by
505 * negative return values.
506 */
507STATIC ssize_t
508xfs_file_dio_aio_write(
509 struct kiocb *iocb,
b3188919 510 struct iov_iter *from)
f0d26e86
DC
511{
512 struct file *file = iocb->ki_filp;
513 struct address_space *mapping = file->f_mapping;
514 struct inode *inode = mapping->host;
515 struct xfs_inode *ip = XFS_I(inode);
516 struct xfs_mount *mp = ip->i_mount;
517 ssize_t ret = 0;
eda77982 518 int unaligned_io = 0;
d0606464 519 int iolock;
b3188919 520 size_t count = iov_iter_count(from);
f9acc19c 521 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
f0d26e86 522
7c71ee78 523 /* DIO must be aligned to device logical sector size */
16d4d435 524 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
b474c7ae 525 return -EINVAL;
f0d26e86 526
7271d243 527 /*
0ee7a3f6
CH
528 * Don't take the exclusive iolock here unless the I/O is unaligned to
529 * the file system block size. We don't need to consider the EOF
530 * extension case here because xfs_file_aio_write_checks() will relock
531 * the inode as necessary for EOF zeroing cases and fill out the new
532 * inode size as appropriate.
7271d243 533 */
0ee7a3f6
CH
534 if ((iocb->ki_pos & mp->m_blockmask) ||
535 ((iocb->ki_pos + count) & mp->m_blockmask)) {
536 unaligned_io = 1;
54a4ef8a
CH
537
538 /*
539 * We can't properly handle unaligned direct I/O to reflink
540 * files yet, as we can't unshare a partial block.
541 */
66ae56a5 542 if (xfs_is_cow_inode(ip)) {
54a4ef8a 543 trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
80e543ae 544 return -ENOTBLK;
54a4ef8a 545 }
d0606464 546 iolock = XFS_IOLOCK_EXCL;
0ee7a3f6 547 } else {
d0606464 548 iolock = XFS_IOLOCK_SHARED;
c58cb165 549 }
f0d26e86 550
942491c9 551 if (iocb->ki_flags & IOCB_NOWAIT) {
1fdeaea4
DW
552 /* unaligned dio always waits, bail */
553 if (unaligned_io)
554 return -EAGAIN;
942491c9 555 if (!xfs_ilock_nowait(ip, iolock))
29a5d29e 556 return -EAGAIN;
942491c9 557 } else {
29a5d29e
GR
558 xfs_ilock(ip, iolock);
559 }
0ee7a3f6 560
99733fa3 561 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
4d8d1581 562 if (ret)
d0606464 563 goto out;
99733fa3 564 count = iov_iter_count(from);
f0d26e86 565
eda77982 566 /*
2032a8a2
BF
567 * If we are doing unaligned IO, we can't allow any other overlapping IO
568 * in-flight at the same time or we risk data corruption. Wait for all
569 * other IO to drain before we submit. If the IO is aligned, demote the
570 * iolock if we had to take the exclusive lock in
571 * xfs_file_aio_write_checks() for other reasons.
eda77982 572 */
29a5d29e 573 if (unaligned_io) {
2032a8a2 574 inode_dio_wait(inode);
29a5d29e 575 } else if (iolock == XFS_IOLOCK_EXCL) {
65523218 576 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
d0606464 577 iolock = XFS_IOLOCK_SHARED;
f0d26e86
DC
578 }
579
3176c3e0 580 trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
2032a8a2 581 /*
906753be
JK
582 * If unaligned, this is the only IO in-flight. Wait on it before we
583 * release the iolock to prevent subsequent overlapping IO.
2032a8a2 584 */
f150b423
CH
585 ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
586 &xfs_dio_write_ops,
906753be 587 is_sync_kiocb(iocb) || unaligned_io);
d0606464 588out:
65523218 589 xfs_iunlock(ip, iolock);
d0606464 590
6b698ede 591 /*
60263d58
CH
592 * No fallback to buffered IO after short writes for XFS, direct I/O
593 * will either complete fully or return an error.
6b698ede 594 */
16d4d435
CH
595 ASSERT(ret < 0 || ret == count);
596 return ret;
597}
598
f021bd07 599static noinline ssize_t
16d4d435
CH
600xfs_file_dax_write(
601 struct kiocb *iocb,
602 struct iov_iter *from)
603{
6c31f495 604 struct inode *inode = iocb->ki_filp->f_mapping->host;
16d4d435 605 struct xfs_inode *ip = XFS_I(inode);
17879e8f 606 int iolock = XFS_IOLOCK_EXCL;
6c31f495
CH
607 ssize_t ret, error = 0;
608 size_t count;
609 loff_t pos;
16d4d435 610
942491c9
CH
611 if (iocb->ki_flags & IOCB_NOWAIT) {
612 if (!xfs_ilock_nowait(ip, iolock))
29a5d29e 613 return -EAGAIN;
942491c9 614 } else {
29a5d29e
GR
615 xfs_ilock(ip, iolock);
616 }
617
16d4d435
CH
618 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
619 if (ret)
620 goto out;
621
6c31f495
CH
622 pos = iocb->ki_pos;
623 count = iov_iter_count(from);
8b2180b3 624
6c31f495 625 trace_xfs_file_dax_write(ip, count, pos);
f150b423 626 ret = dax_iomap_rw(iocb, from, &xfs_direct_write_iomap_ops);
6c31f495
CH
627 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
628 i_size_write(inode, iocb->ki_pos);
629 error = xfs_setfilesize(ip, pos, ret);
16d4d435 630 }
16d4d435 631out:
65523218 632 xfs_iunlock(ip, iolock);
ed5c3e66
DC
633 if (error)
634 return error;
635
636 if (ret > 0) {
637 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
638
639 /* Handle various SYNC-type writes */
640 ret = generic_write_sync(iocb, ret);
641 }
642 return ret;
f0d26e86
DC
643}
644
00258e36 645STATIC ssize_t
637bbc75 646xfs_file_buffered_aio_write(
dda35b8f 647 struct kiocb *iocb,
b3188919 648 struct iov_iter *from)
dda35b8f
CH
649{
650 struct file *file = iocb->ki_filp;
651 struct address_space *mapping = file->f_mapping;
652 struct inode *inode = mapping->host;
00258e36 653 struct xfs_inode *ip = XFS_I(inode);
637bbc75
DC
654 ssize_t ret;
655 int enospc = 0;
c3155097 656 int iolock;
dda35b8f 657
91f9943e
CH
658 if (iocb->ki_flags & IOCB_NOWAIT)
659 return -EOPNOTSUPP;
660
c3155097
BF
661write_retry:
662 iolock = XFS_IOLOCK_EXCL;
65523218 663 xfs_ilock(ip, iolock);
dda35b8f 664
99733fa3 665 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
4d8d1581 666 if (ret)
d0606464 667 goto out;
dda35b8f
CH
668
669 /* We can write back this queue in page reclaim */
de1414a6 670 current->backing_dev_info = inode_to_bdi(inode);
dda35b8f 671
3176c3e0 672 trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
f150b423
CH
673 ret = iomap_file_buffered_write(iocb, from,
674 &xfs_buffered_write_iomap_ops);
0a64bc2c 675 if (likely(ret >= 0))
99733fa3 676 iocb->ki_pos += ret;
dc06f398 677
637bbc75 678 /*
dc06f398
BF
679 * If we hit a space limit, try to free up some lingering preallocated
680 * space before returning an error. In the case of ENOSPC, first try to
681 * write back all dirty inodes to free up some of the excess reserved
682 * metadata space. This reduces the chances that the eofblocks scan
683 * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
684 * also behaves as a filter to prevent too many eofblocks scans from
685 * running at the same time.
637bbc75 686 */
dc06f398 687 if (ret == -EDQUOT && !enospc) {
c3155097 688 xfs_iunlock(ip, iolock);
dc06f398
BF
689 enospc = xfs_inode_free_quota_eofblocks(ip);
690 if (enospc)
691 goto write_retry;
83104d44
DW
692 enospc = xfs_inode_free_quota_cowblocks(ip);
693 if (enospc)
694 goto write_retry;
c3155097 695 iolock = 0;
dc06f398
BF
696 } else if (ret == -ENOSPC && !enospc) {
697 struct xfs_eofblocks eofb = {0};
698
637bbc75 699 enospc = 1;
9aa05000 700 xfs_flush_inodes(ip->i_mount);
c3155097
BF
701
702 xfs_iunlock(ip, iolock);
dc06f398
BF
703 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
704 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
cf2cb784 705 xfs_icache_free_cowblocks(ip->i_mount, &eofb);
9aa05000 706 goto write_retry;
dda35b8f 707 }
d0606464 708
dda35b8f 709 current->backing_dev_info = NULL;
d0606464 710out:
c3155097
BF
711 if (iolock)
712 xfs_iunlock(ip, iolock);
ed5c3e66
DC
713
714 if (ret > 0) {
715 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
716 /* Handle various SYNC-type writes */
717 ret = generic_write_sync(iocb, ret);
718 }
637bbc75
DC
719 return ret;
720}
721
722STATIC ssize_t
bf97f3bc 723xfs_file_write_iter(
637bbc75 724 struct kiocb *iocb,
bf97f3bc 725 struct iov_iter *from)
637bbc75
DC
726{
727 struct file *file = iocb->ki_filp;
728 struct address_space *mapping = file->f_mapping;
729 struct inode *inode = mapping->host;
730 struct xfs_inode *ip = XFS_I(inode);
731 ssize_t ret;
bf97f3bc 732 size_t ocount = iov_iter_count(from);
637bbc75 733
ff6d6af2 734 XFS_STATS_INC(ip->i_mount, xs_write_calls);
637bbc75 735
637bbc75
DC
736 if (ocount == 0)
737 return 0;
738
bf97f3bc
AV
739 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
740 return -EIO;
637bbc75 741
16d4d435 742 if (IS_DAX(inode))
ed5c3e66
DC
743 return xfs_file_dax_write(iocb, from);
744
745 if (iocb->ki_flags & IOCB_DIRECT) {
0613f16c
DW
746 /*
747 * Allow a directio write to fall back to a buffered
748 * write *only* in the case that we're doing a reflink
749 * CoW. In all other directio scenarios we do not
750 * allow an operation to fall back to buffered mode.
751 */
bf97f3bc 752 ret = xfs_file_dio_aio_write(iocb, from);
80e543ae 753 if (ret != -ENOTBLK)
ed5c3e66 754 return ret;
0613f16c 755 }
dda35b8f 756
ed5c3e66 757 return xfs_file_buffered_aio_write(iocb, from);
dda35b8f
CH
758}
759
d6dc57e2
DW
760static void
761xfs_wait_dax_page(
e25ff835 762 struct inode *inode)
d6dc57e2
DW
763{
764 struct xfs_inode *ip = XFS_I(inode);
765
d6dc57e2
DW
766 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
767 schedule();
768 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
769}
770
771static int
772xfs_break_dax_layouts(
773 struct inode *inode,
e25ff835 774 bool *retry)
d6dc57e2
DW
775{
776 struct page *page;
777
778 ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
779
780 page = dax_layout_busy_page(inode->i_mapping);
781 if (!page)
782 return 0;
783
e25ff835 784 *retry = true;
d6dc57e2
DW
785 return ___wait_var_event(&page->_refcount,
786 atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
e25ff835 787 0, 0, xfs_wait_dax_page(inode));
d6dc57e2
DW
788}
789
69eb5fa1
DW
790int
791xfs_break_layouts(
792 struct inode *inode,
793 uint *iolock,
794 enum layout_break_reason reason)
795{
796 bool retry;
d6dc57e2 797 int error;
69eb5fa1
DW
798
799 ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
800
d6dc57e2
DW
801 do {
802 retry = false;
803 switch (reason) {
804 case BREAK_UNMAP:
a4722a64 805 error = xfs_break_dax_layouts(inode, &retry);
d6dc57e2
DW
806 if (error || retry)
807 break;
808 /* fall through */
809 case BREAK_WRITE:
810 error = xfs_break_leased_layouts(inode, iolock, &retry);
811 break;
812 default:
813 WARN_ON_ONCE(1);
814 error = -EINVAL;
815 }
816 } while (error == 0 && retry);
817
818 return error;
69eb5fa1
DW
819}
820
a904b1ca
NJ
821#define XFS_FALLOC_FL_SUPPORTED \
822 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
823 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
98cc2db5 824 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
a904b1ca 825
2fe17c10
CH
826STATIC long
827xfs_file_fallocate(
83aee9e4
CH
828 struct file *file,
829 int mode,
830 loff_t offset,
831 loff_t len)
2fe17c10 832{
83aee9e4
CH
833 struct inode *inode = file_inode(file);
834 struct xfs_inode *ip = XFS_I(inode);
83aee9e4 835 long error;
8add71ca 836 enum xfs_prealloc_flags flags = 0;
c63a8eae 837 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
83aee9e4 838 loff_t new_size = 0;
749f24f3 839 bool do_file_insert = false;
2fe17c10 840
83aee9e4
CH
841 if (!S_ISREG(inode->i_mode))
842 return -EINVAL;
a904b1ca 843 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
2fe17c10
CH
844 return -EOPNOTSUPP;
845
781355c6 846 xfs_ilock(ip, iolock);
69eb5fa1 847 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
781355c6
CH
848 if (error)
849 goto out_unlock;
850
249bd908
DC
851 /*
852 * Must wait for all AIO to complete before we continue as AIO can
853 * change the file size on completion without holding any locks we
854 * currently hold. We must do this first because AIO can update both
855 * the on disk and in memory inode sizes, and the operations that follow
856 * require the in-memory size to be fully up-to-date.
857 */
858 inode_dio_wait(inode);
859
860 /*
861 * Now AIO and DIO has drained we flush and (if necessary) invalidate
862 * the cached range over the first operation we are about to run.
863 *
864 * We care about zero and collapse here because they both run a hole
865 * punch over the range first. Because that can zero data, and the range
866 * of invalidation for the shift operations is much larger, we still do
867 * the required flush for collapse in xfs_prepare_shift().
868 *
869 * Insert has the same range requirements as collapse, and we extend the
870 * file first which can zero data. Hence insert has the same
871 * flush/invalidate requirements as collapse and so they are both
872 * handled at the right time by xfs_prepare_shift().
873 */
874 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE |
875 FALLOC_FL_COLLAPSE_RANGE)) {
876 error = xfs_flush_unmap_range(ip, offset, len);
877 if (error)
878 goto out_unlock;
879 }
880
83aee9e4
CH
881 if (mode & FALLOC_FL_PUNCH_HOLE) {
882 error = xfs_free_file_space(ip, offset, len);
883 if (error)
884 goto out_unlock;
e1d8fb88 885 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
25219dbf 886 if (!xfs_is_falloc_aligned(ip, offset, len)) {
2451337d 887 error = -EINVAL;
e1d8fb88
NJ
888 goto out_unlock;
889 }
890
23fffa92
LC
891 /*
892 * There is no need to overlap collapse range with EOF,
893 * in which case it is effectively a truncate operation
894 */
895 if (offset + len >= i_size_read(inode)) {
2451337d 896 error = -EINVAL;
23fffa92
LC
897 goto out_unlock;
898 }
899
e1d8fb88
NJ
900 new_size = i_size_read(inode) - len;
901
902 error = xfs_collapse_file_space(ip, offset, len);
903 if (error)
904 goto out_unlock;
a904b1ca 905 } else if (mode & FALLOC_FL_INSERT_RANGE) {
7d83fb14 906 loff_t isize = i_size_read(inode);
a904b1ca 907
25219dbf 908 if (!xfs_is_falloc_aligned(ip, offset, len)) {
a904b1ca
NJ
909 error = -EINVAL;
910 goto out_unlock;
911 }
912
7d83fb14
DW
913 /*
914 * New inode size must not exceed ->s_maxbytes, accounting for
915 * possible signed overflow.
916 */
917 if (inode->i_sb->s_maxbytes - isize < len) {
a904b1ca
NJ
918 error = -EFBIG;
919 goto out_unlock;
920 }
7d83fb14 921 new_size = isize + len;
a904b1ca
NJ
922
923 /* Offset should be less than i_size */
7d83fb14 924 if (offset >= isize) {
a904b1ca
NJ
925 error = -EINVAL;
926 goto out_unlock;
927 }
749f24f3 928 do_file_insert = true;
83aee9e4 929 } else {
8add71ca
CH
930 flags |= XFS_PREALLOC_SET;
931
83aee9e4
CH
932 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
933 offset + len > i_size_read(inode)) {
934 new_size = offset + len;
2451337d 935 error = inode_newsize_ok(inode, new_size);
83aee9e4
CH
936 if (error)
937 goto out_unlock;
938 }
2fe17c10 939
66ae56a5 940 if (mode & FALLOC_FL_ZERO_RANGE) {
360c09c0
CH
941 /*
942 * Punch a hole and prealloc the range. We use a hole
943 * punch rather than unwritten extent conversion for two
944 * reasons:
945 *
946 * 1.) Hole punch handles partial block zeroing for us.
947 * 2.) If prealloc returns ENOSPC, the file range is
948 * still zero-valued by virtue of the hole punch.
949 */
950 unsigned int blksize = i_blocksize(inode);
951
952 trace_xfs_zero_file_space(ip);
953
954 error = xfs_free_file_space(ip, offset, len);
955 if (error)
956 goto out_unlock;
957
958 len = round_up(offset + len, blksize) -
959 round_down(offset, blksize);
960 offset = round_down(offset, blksize);
66ae56a5
CH
961 } else if (mode & FALLOC_FL_UNSHARE_RANGE) {
962 error = xfs_reflink_unshare(ip, offset, len);
963 if (error)
964 goto out_unlock;
66ae56a5
CH
965 } else {
966 /*
967 * If always_cow mode we can't use preallocations and
968 * thus should not create them.
969 */
970 if (xfs_is_always_cow_inode(ip)) {
971 error = -EOPNOTSUPP;
972 goto out_unlock;
973 }
360c09c0 974 }
66ae56a5 975
360c09c0 976 if (!xfs_is_always_cow_inode(ip)) {
376ba313
LC
977 error = xfs_alloc_file_space(ip, offset, len,
978 XFS_BMAPI_PREALLOC);
360c09c0
CH
979 if (error)
980 goto out_unlock;
98cc2db5 981 }
2fe17c10
CH
982 }
983
83aee9e4 984 if (file->f_flags & O_DSYNC)
8add71ca
CH
985 flags |= XFS_PREALLOC_SYNC;
986
987 error = xfs_update_prealloc_flags(ip, flags);
2fe17c10
CH
988 if (error)
989 goto out_unlock;
990
991 /* Change file size if needed */
992 if (new_size) {
993 struct iattr iattr;
994
995 iattr.ia_valid = ATTR_SIZE;
996 iattr.ia_size = new_size;
69bca807 997 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
a904b1ca
NJ
998 if (error)
999 goto out_unlock;
2fe17c10
CH
1000 }
1001
a904b1ca
NJ
1002 /*
1003 * Perform hole insertion now that the file size has been
1004 * updated so that if we crash during the operation we don't
1005 * leave shifted extents past EOF and hence losing access to
1006 * the data that is contained within them.
1007 */
1008 if (do_file_insert)
1009 error = xfs_insert_file_space(ip, offset, len);
1010
2fe17c10 1011out_unlock:
781355c6 1012 xfs_iunlock(ip, iolock);
2451337d 1013 return error;
2fe17c10
CH
1014}
1015
40144e49
JK
1016STATIC int
1017xfs_file_fadvise(
1018 struct file *file,
1019 loff_t start,
1020 loff_t end,
1021 int advice)
1022{
1023 struct xfs_inode *ip = XFS_I(file_inode(file));
1024 int ret;
1025 int lockflags = 0;
1026
1027 /*
1028 * Operations creating pages in page cache need protection from hole
1029 * punching and similar ops
1030 */
1031 if (advice == POSIX_FADV_WILLNEED) {
1032 lockflags = XFS_IOLOCK_SHARED;
1033 xfs_ilock(ip, lockflags);
1034 }
1035 ret = generic_fadvise(file, start, end, advice);
1036 if (lockflags)
1037 xfs_iunlock(ip, lockflags);
1038 return ret;
1039}
3fc9f5e4 1040
5ffce3cc
DW
1041/* Does this file, inode, or mount want synchronous writes? */
1042static inline bool xfs_file_sync_writes(struct file *filp)
1043{
1044 struct xfs_inode *ip = XFS_I(file_inode(filp));
1045
1046 if (ip->i_mount->m_flags & XFS_MOUNT_WSYNC)
1047 return true;
1048 if (filp->f_flags & (__O_SYNC | O_DSYNC))
1049 return true;
1050 if (IS_SYNC(file_inode(filp)))
1051 return true;
1052
1053 return false;
1054}
1055
da034bcc 1056STATIC loff_t
2e5dfc99 1057xfs_file_remap_range(
3fc9f5e4
DW
1058 struct file *file_in,
1059 loff_t pos_in,
1060 struct file *file_out,
1061 loff_t pos_out,
1062 loff_t len,
1063 unsigned int remap_flags)
9fe26045 1064{
3fc9f5e4
DW
1065 struct inode *inode_in = file_inode(file_in);
1066 struct xfs_inode *src = XFS_I(inode_in);
1067 struct inode *inode_out = file_inode(file_out);
1068 struct xfs_inode *dest = XFS_I(inode_out);
1069 struct xfs_mount *mp = src->i_mount;
1070 loff_t remapped = 0;
1071 xfs_extlen_t cowextsize;
1072 int ret;
1073
2e5dfc99
DW
1074 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1075 return -EINVAL;
cc714660 1076
3fc9f5e4
DW
1077 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1078 return -EOPNOTSUPP;
1079
1080 if (XFS_FORCED_SHUTDOWN(mp))
1081 return -EIO;
1082
1083 /* Prepare and then clone file data. */
1084 ret = xfs_reflink_remap_prep(file_in, pos_in, file_out, pos_out,
1085 &len, remap_flags);
451d34ee 1086 if (ret || len == 0)
3fc9f5e4
DW
1087 return ret;
1088
1089 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
1090
1091 ret = xfs_reflink_remap_blocks(src, pos_in, dest, pos_out, len,
1092 &remapped);
1093 if (ret)
1094 goto out_unlock;
1095
1096 /*
1097 * Carry the cowextsize hint from src to dest if we're sharing the
1098 * entire source file to the entire destination file, the source file
1099 * has a cowextsize hint, and the destination file does not.
1100 */
1101 cowextsize = 0;
1102 if (pos_in == 0 && len == i_size_read(inode_in) &&
1103 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1104 pos_out == 0 && len >= i_size_read(inode_out) &&
1105 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1106 cowextsize = src->i_d.di_cowextsize;
1107
1108 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1109 remap_flags);
5833112d
CH
1110 if (ret)
1111 goto out_unlock;
3fc9f5e4 1112
5ffce3cc 1113 if (xfs_file_sync_writes(file_in) || xfs_file_sync_writes(file_out))
5833112d 1114 xfs_log_force_inode(dest);
3fc9f5e4 1115out_unlock:
e2aaee9c 1116 xfs_iunlock2_io_mmap(src, dest);
3fc9f5e4
DW
1117 if (ret)
1118 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1119 return remapped > 0 ? remapped : ret;
9fe26045 1120}
2fe17c10 1121
1da177e4 1122STATIC int
3562fd45 1123xfs_file_open(
1da177e4 1124 struct inode *inode,
f999a5bf 1125 struct file *file)
1da177e4 1126{
f999a5bf 1127 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1da177e4 1128 return -EFBIG;
f999a5bf
CH
1129 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1130 return -EIO;
f89fb730 1131 file->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
f999a5bf
CH
1132 return 0;
1133}
1134
1135STATIC int
1136xfs_dir_open(
1137 struct inode *inode,
1138 struct file *file)
1139{
1140 struct xfs_inode *ip = XFS_I(inode);
1141 int mode;
1142 int error;
1143
1144 error = xfs_file_open(inode, file);
1145 if (error)
1146 return error;
1147
1148 /*
1149 * If there are any blocks, read-ahead block 0 as we're almost
1150 * certain to have the next operation be a read there.
1151 */
309ecac8 1152 mode = xfs_ilock_data_map_shared(ip);
daf83964 1153 if (ip->i_df.if_nextents > 0)
06566fda 1154 error = xfs_dir3_data_readahead(ip, 0, 0);
f999a5bf 1155 xfs_iunlock(ip, mode);
7a652bbe 1156 return error;
1da177e4
LT
1157}
1158
1da177e4 1159STATIC int
3562fd45 1160xfs_file_release(
1da177e4
LT
1161 struct inode *inode,
1162 struct file *filp)
1163{
2451337d 1164 return xfs_release(XFS_I(inode));
1da177e4
LT
1165}
1166
1da177e4 1167STATIC int
3562fd45 1168xfs_file_readdir(
b8227554
AV
1169 struct file *file,
1170 struct dir_context *ctx)
1da177e4 1171{
b8227554 1172 struct inode *inode = file_inode(file);
739bfb2a 1173 xfs_inode_t *ip = XFS_I(inode);
051e7cd4
CH
1174 size_t bufsize;
1175
1176 /*
1177 * The Linux API doesn't pass down the total size of the buffer
1178 * we read into down to the filesystem. With the filldir concept
1179 * it's not needed for correct information, but the XFS dir2 leaf
1180 * code wants an estimate of the buffer size to calculate it's
1181 * readahead window and size the buffers used for mapping to
1182 * physical blocks.
1183 *
1184 * Try to give it an estimate that's good enough, maybe at some
1185 * point we can change the ->readdir prototype to include the
a9cc799e 1186 * buffer size. For now we use the current glibc buffer size.
051e7cd4 1187 */
a5c46e5e 1188 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
051e7cd4 1189
acb9553c 1190 return xfs_readdir(NULL, ip, ctx, bufsize);
3fe3e6b1
JL
1191}
1192
1193STATIC loff_t
1194xfs_file_llseek(
1195 struct file *file,
1196 loff_t offset,
59f9c004 1197 int whence)
3fe3e6b1 1198{
9b2970aa
CH
1199 struct inode *inode = file->f_mapping->host;
1200
1201 if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
1202 return -EIO;
1203
59f9c004 1204 switch (whence) {
9b2970aa 1205 default:
59f9c004 1206 return generic_file_llseek(file, offset, whence);
3fe3e6b1 1207 case SEEK_HOLE:
60271ab7 1208 offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops);
9b2970aa 1209 break;
49c69591 1210 case SEEK_DATA:
60271ab7 1211 offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops);
9b2970aa 1212 break;
3fe3e6b1 1213 }
9b2970aa
CH
1214
1215 if (offset < 0)
1216 return offset;
1217 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3fe3e6b1
JL
1218}
1219
de0e8c20
DC
1220/*
1221 * Locking for serialisation of IO during page faults. This results in a lock
1222 * ordering of:
1223 *
c1e8d7c6 1224 * mmap_lock (MM)
6b698ede 1225 * sb_start_pagefault(vfs, freeze)
13ad4fe3 1226 * i_mmaplock (XFS - truncate serialisation)
6b698ede
DC
1227 * page_lock (MM)
1228 * i_lock (XFS - extent map serialisation)
de0e8c20 1229 */
05edd888 1230static vm_fault_t
d522d569
CH
1231__xfs_filemap_fault(
1232 struct vm_fault *vmf,
1233 enum page_entry_size pe_size,
1234 bool write_fault)
de0e8c20 1235{
11bac800 1236 struct inode *inode = file_inode(vmf->vma->vm_file);
d522d569 1237 struct xfs_inode *ip = XFS_I(inode);
05edd888 1238 vm_fault_t ret;
de0e8c20 1239
d522d569 1240 trace_xfs_filemap_fault(ip, pe_size, write_fault);
de0e8c20 1241
d522d569
CH
1242 if (write_fault) {
1243 sb_start_pagefault(inode->i_sb);
1244 file_update_time(vmf->vma->vm_file);
1245 }
de0e8c20 1246
d522d569 1247 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
6b698ede 1248 if (IS_DAX(inode)) {
a39e596b
CH
1249 pfn_t pfn;
1250
690c2a38
CH
1251 ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL,
1252 (write_fault && !vmf->cow_page) ?
f150b423
CH
1253 &xfs_direct_write_iomap_ops :
1254 &xfs_read_iomap_ops);
a39e596b
CH
1255 if (ret & VM_FAULT_NEEDDSYNC)
1256 ret = dax_finish_sync_fault(vmf, pe_size, pfn);
6b698ede 1257 } else {
d522d569 1258 if (write_fault)
f150b423
CH
1259 ret = iomap_page_mkwrite(vmf,
1260 &xfs_buffered_write_iomap_ops);
d522d569
CH
1261 else
1262 ret = filemap_fault(vmf);
6b698ede 1263 }
6b698ede 1264 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
6b698ede 1265
d522d569
CH
1266 if (write_fault)
1267 sb_end_pagefault(inode->i_sb);
6b698ede 1268 return ret;
de0e8c20
DC
1269}
1270
b17164e2
MP
1271static inline bool
1272xfs_is_write_fault(
1273 struct vm_fault *vmf)
1274{
1275 return (vmf->flags & FAULT_FLAG_WRITE) &&
1276 (vmf->vma->vm_flags & VM_SHARED);
1277}
1278
05edd888 1279static vm_fault_t
6b698ede 1280xfs_filemap_fault(
075a924d
DC
1281 struct vm_fault *vmf)
1282{
6b698ede 1283 /* DAX can shortcut the normal fault path on write faults! */
d522d569
CH
1284 return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
1285 IS_DAX(file_inode(vmf->vma->vm_file)) &&
b17164e2 1286 xfs_is_write_fault(vmf));
6b698ede
DC
1287}
1288
05edd888 1289static vm_fault_t
a2d58167 1290xfs_filemap_huge_fault(
c791ace1
DJ
1291 struct vm_fault *vmf,
1292 enum page_entry_size pe_size)
acd76e74 1293{
d522d569 1294 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
acd76e74
MW
1295 return VM_FAULT_FALLBACK;
1296
d522d569
CH
1297 /* DAX can shortcut the normal fault path on write faults! */
1298 return __xfs_filemap_fault(vmf, pe_size,
b17164e2 1299 xfs_is_write_fault(vmf));
d522d569 1300}
acd76e74 1301
05edd888 1302static vm_fault_t
d522d569
CH
1303xfs_filemap_page_mkwrite(
1304 struct vm_fault *vmf)
1305{
1306 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
acd76e74
MW
1307}
1308
3af49285 1309/*
7b565c9f
JK
1310 * pfn_mkwrite was originally intended to ensure we capture time stamp updates
1311 * on write faults. In reality, it needs to serialise against truncate and
1312 * prepare memory for writing so handle is as standard write fault.
3af49285 1313 */
05edd888 1314static vm_fault_t
3af49285 1315xfs_filemap_pfn_mkwrite(
3af49285
DC
1316 struct vm_fault *vmf)
1317{
1318
7b565c9f 1319 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
acd76e74
MW
1320}
1321
cd647d56
DC
1322static void
1323xfs_filemap_map_pages(
1324 struct vm_fault *vmf,
1325 pgoff_t start_pgoff,
1326 pgoff_t end_pgoff)
1327{
1328 struct inode *inode = file_inode(vmf->vma->vm_file);
1329
1330 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1331 filemap_map_pages(vmf, start_pgoff, end_pgoff);
1332 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1333}
1334
6b698ede
DC
1335static const struct vm_operations_struct xfs_file_vm_ops = {
1336 .fault = xfs_filemap_fault,
a2d58167 1337 .huge_fault = xfs_filemap_huge_fault,
cd647d56 1338 .map_pages = xfs_filemap_map_pages,
6b698ede 1339 .page_mkwrite = xfs_filemap_page_mkwrite,
3af49285 1340 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
6b698ede
DC
1341};
1342
1343STATIC int
1344xfs_file_mmap(
30fa529e
CH
1345 struct file *file,
1346 struct vm_area_struct *vma)
6b698ede 1347{
30fa529e
CH
1348 struct inode *inode = file_inode(file);
1349 struct xfs_buftarg *target = xfs_inode_buftarg(XFS_I(inode));
b21fec41 1350
a39e596b 1351 /*
b21fec41
PG
1352 * We don't support synchronous mappings for non-DAX files and
1353 * for DAX files if underneath dax_device is not synchronous.
a39e596b 1354 */
30fa529e 1355 if (!daxdev_mapping_supported(vma, target->bt_daxdev))
a39e596b
CH
1356 return -EOPNOTSUPP;
1357
30fa529e 1358 file_accessed(file);
6b698ede 1359 vma->vm_ops = &xfs_file_vm_ops;
30fa529e 1360 if (IS_DAX(inode))
e1fb4a08 1361 vma->vm_flags |= VM_HUGEPAGE;
6b698ede 1362 return 0;
075a924d
DC
1363}
1364
4b6f5d20 1365const struct file_operations xfs_file_operations = {
3fe3e6b1 1366 .llseek = xfs_file_llseek,
b4f5d2c6 1367 .read_iter = xfs_file_read_iter,
bf97f3bc 1368 .write_iter = xfs_file_write_iter,
82c156f8 1369 .splice_read = generic_file_splice_read,
8d020765 1370 .splice_write = iter_file_splice_write,
81214bab 1371 .iopoll = iomap_dio_iopoll,
3562fd45 1372 .unlocked_ioctl = xfs_file_ioctl,
1da177e4 1373#ifdef CONFIG_COMPAT
3562fd45 1374 .compat_ioctl = xfs_file_compat_ioctl,
1da177e4 1375#endif
3562fd45 1376 .mmap = xfs_file_mmap,
a39e596b 1377 .mmap_supported_flags = MAP_SYNC,
3562fd45
NS
1378 .open = xfs_file_open,
1379 .release = xfs_file_release,
1380 .fsync = xfs_file_fsync,
dbe6ec81 1381 .get_unmapped_area = thp_get_unmapped_area,
2fe17c10 1382 .fallocate = xfs_file_fallocate,
40144e49 1383 .fadvise = xfs_file_fadvise,
2e5dfc99 1384 .remap_file_range = xfs_file_remap_range,
1da177e4
LT
1385};
1386
4b6f5d20 1387const struct file_operations xfs_dir_file_operations = {
f999a5bf 1388 .open = xfs_dir_open,
1da177e4 1389 .read = generic_read_dir,
3b0a3c1a 1390 .iterate_shared = xfs_file_readdir,
59af1584 1391 .llseek = generic_file_llseek,
3562fd45 1392 .unlocked_ioctl = xfs_file_ioctl,
d3870398 1393#ifdef CONFIG_COMPAT
3562fd45 1394 .compat_ioctl = xfs_file_compat_ioctl,
d3870398 1395#endif
1da2f2db 1396 .fsync = xfs_dir_fsync,
1da177e4 1397};
This page took 1.455821 seconds and 4 git commands to generate.