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