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