1
2
3
4
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_da_format.h"
14#include "xfs_da_btree.h"
15#include "xfs_inode.h"
16#include "xfs_trans.h"
17#include "xfs_inode_item.h"
18#include "xfs_bmap.h"
19#include "xfs_bmap_util.h"
20#include "xfs_error.h"
21#include "xfs_dir2.h"
22#include "xfs_dir2_priv.h"
23#include "xfs_ioctl.h"
24#include "xfs_trace.h"
25#include "xfs_log.h"
26#include "xfs_icache.h"
27#include "xfs_pnfs.h"
28#include "xfs_iomap.h"
29#include "xfs_reflink.h"
30
31#include <linux/dcache.h>
32#include <linux/falloc.h>
33#include <linux/pagevec.h>
34#include <linux/backing-dev.h>
35#include <linux/mman.h>
36
37static const struct vm_operations_struct xfs_file_vm_ops;
38
39int
40xfs_update_prealloc_flags(
41 struct xfs_inode *ip,
42 enum xfs_prealloc_flags flags)
43{
44 struct xfs_trans *tp;
45 int error;
46
47 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
48 0, 0, 0, &tp);
49 if (error)
50 return error;
51
52 xfs_ilock(ip, XFS_ILOCK_EXCL);
53 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
54
55 if (!(flags & XFS_PREALLOC_INVISIBLE)) {
56 VFS_I(ip)->i_mode &= ~S_ISUID;
57 if (VFS_I(ip)->i_mode & S_IXGRP)
58 VFS_I(ip)->i_mode &= ~S_ISGID;
59 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
60 }
61
62 if (flags & XFS_PREALLOC_SET)
63 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
64 if (flags & XFS_PREALLOC_CLEAR)
65 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
66
67 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
68 if (flags & XFS_PREALLOC_SYNC)
69 xfs_trans_set_sync(tp);
70 return xfs_trans_commit(tp);
71}
72
73
74
75
76
77
78
79STATIC int
80xfs_dir_fsync(
81 struct file *file,
82 loff_t start,
83 loff_t end,
84 int datasync)
85{
86 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
87 struct xfs_mount *mp = ip->i_mount;
88 xfs_lsn_t lsn = 0;
89
90 trace_xfs_dir_fsync(ip);
91
92 xfs_ilock(ip, XFS_ILOCK_SHARED);
93 if (xfs_ipincount(ip))
94 lsn = ip->i_itemp->ili_last_lsn;
95 xfs_iunlock(ip, XFS_ILOCK_SHARED);
96
97 if (!lsn)
98 return 0;
99 return xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
100}
101
102STATIC int
103xfs_file_fsync(
104 struct file *file,
105 loff_t start,
106 loff_t end,
107 int datasync)
108{
109 struct inode *inode = file->f_mapping->host;
110 struct xfs_inode *ip = XFS_I(inode);
111 struct xfs_mount *mp = ip->i_mount;
112 int error = 0;
113 int log_flushed = 0;
114 xfs_lsn_t lsn = 0;
115
116 trace_xfs_file_fsync(ip);
117
118 error = file_write_and_wait_range(file, start, end);
119 if (error)
120 return error;
121
122 if (XFS_FORCED_SHUTDOWN(mp))
123 return -EIO;
124
125 xfs_iflags_clear(ip, XFS_ITRUNCATED);
126
127
128
129
130
131
132
133 if (XFS_IS_REALTIME_INODE(ip))
134 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
135 else if (mp->m_logdev_targp != mp->m_ddev_targp)
136 xfs_blkdev_issue_flush(mp->m_ddev_targp);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 xfs_ilock(ip, XFS_ILOCK_SHARED);
152 if (xfs_ipincount(ip)) {
153 if (!datasync ||
154 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
155 lsn = ip->i_itemp->ili_last_lsn;
156 }
157
158 if (lsn) {
159 error = xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
160 ip->i_itemp->ili_fsync_fields = 0;
161 }
162 xfs_iunlock(ip, XFS_ILOCK_SHARED);
163
164
165
166
167
168
169
170
171 if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
172 mp->m_logdev_targp == mp->m_ddev_targp)
173 xfs_blkdev_issue_flush(mp->m_ddev_targp);
174
175 return error;
176}
177
178STATIC ssize_t
179xfs_file_dio_aio_read(
180 struct kiocb *iocb,
181 struct iov_iter *to)
182{
183 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
184 size_t count = iov_iter_count(to);
185 ssize_t ret;
186
187 trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
188
189 if (!count)
190 return 0;
191
192 file_accessed(iocb->ki_filp);
193
194 xfs_ilock(ip, XFS_IOLOCK_SHARED);
195 ret = iomap_dio_rw(iocb, to, &xfs_iomap_ops, NULL);
196 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
197
198 return ret;
199}
200
201static noinline ssize_t
202xfs_file_dax_read(
203 struct kiocb *iocb,
204 struct iov_iter *to)
205{
206 struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
207 size_t count = iov_iter_count(to);
208 ssize_t ret = 0;
209
210 trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
211
212 if (!count)
213 return 0;
214
215 if (iocb->ki_flags & IOCB_NOWAIT) {
216 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
217 return -EAGAIN;
218 } else {
219 xfs_ilock(ip, XFS_IOLOCK_SHARED);
220 }
221
222 ret = dax_iomap_rw(iocb, to, &xfs_iomap_ops);
223 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
224
225 file_accessed(iocb->ki_filp);
226 return ret;
227}
228
229STATIC ssize_t
230xfs_file_buffered_aio_read(
231 struct kiocb *iocb,
232 struct iov_iter *to)
233{
234 struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
235 ssize_t ret;
236
237 trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
238
239 if (iocb->ki_flags & IOCB_NOWAIT) {
240 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
241 return -EAGAIN;
242 } else {
243 xfs_ilock(ip, XFS_IOLOCK_SHARED);
244 }
245 ret = generic_file_read_iter(iocb, to);
246 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
247
248 return ret;
249}
250
251STATIC ssize_t
252xfs_file_read_iter(
253 struct kiocb *iocb,
254 struct iov_iter *to)
255{
256 struct inode *inode = file_inode(iocb->ki_filp);
257 struct xfs_mount *mp = XFS_I(inode)->i_mount;
258 ssize_t ret = 0;
259
260 XFS_STATS_INC(mp, xs_read_calls);
261
262 if (XFS_FORCED_SHUTDOWN(mp))
263 return -EIO;
264
265 if (IS_DAX(inode))
266 ret = xfs_file_dax_read(iocb, to);
267 else if (iocb->ki_flags & IOCB_DIRECT)
268 ret = xfs_file_dio_aio_read(iocb, to);
269 else
270 ret = xfs_file_buffered_aio_read(iocb, to);
271
272 if (ret > 0)
273 XFS_STATS_ADD(mp, xs_read_bytes, ret);
274 return ret;
275}
276
277
278
279
280
281
282
283
284STATIC ssize_t
285xfs_file_aio_write_checks(
286 struct kiocb *iocb,
287 struct iov_iter *from,
288 int *iolock)
289{
290 struct file *file = iocb->ki_filp;
291 struct inode *inode = file->f_mapping->host;
292 struct xfs_inode *ip = XFS_I(inode);
293 ssize_t error = 0;
294 size_t count = iov_iter_count(from);
295 bool drained_dio = false;
296 loff_t isize;
297
298restart:
299 error = generic_write_checks(iocb, from);
300 if (error <= 0)
301 return error;
302
303 error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
304 if (error)
305 return error;
306
307
308
309
310
311 if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
312 xfs_iunlock(ip, *iolock);
313 *iolock = XFS_IOLOCK_EXCL;
314 xfs_ilock(ip, *iolock);
315 goto restart;
316 }
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332 spin_lock(&ip->i_flags_lock);
333 isize = i_size_read(inode);
334 if (iocb->ki_pos > isize) {
335 spin_unlock(&ip->i_flags_lock);
336 if (!drained_dio) {
337 if (*iolock == XFS_IOLOCK_SHARED) {
338 xfs_iunlock(ip, *iolock);
339 *iolock = XFS_IOLOCK_EXCL;
340 xfs_ilock(ip, *iolock);
341 iov_iter_reexpand(from, count);
342 }
343
344
345
346
347
348
349
350
351 inode_dio_wait(inode);
352 drained_dio = true;
353 goto restart;
354 }
355
356 trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
357 error = iomap_zero_range(inode, isize, iocb->ki_pos - isize,
358 NULL, &xfs_iomap_ops);
359 if (error)
360 return error;
361 } else
362 spin_unlock(&ip->i_flags_lock);
363
364
365
366
367
368
369
370 if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
371 error = file_update_time(file);
372 if (error)
373 return error;
374 }
375
376
377
378
379
380
381 if (!IS_NOSEC(inode))
382 return file_remove_privs(file);
383 return 0;
384}
385
386static int
387xfs_dio_write_end_io(
388 struct kiocb *iocb,
389 ssize_t size,
390 unsigned flags)
391{
392 struct inode *inode = file_inode(iocb->ki_filp);
393 struct xfs_inode *ip = XFS_I(inode);
394 loff_t offset = iocb->ki_pos;
395 int error = 0;
396
397 trace_xfs_end_io_direct_write(ip, offset, size);
398
399 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
400 return -EIO;
401
402 if (size <= 0)
403 return size;
404
405
406
407
408
409 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
410
411 if (flags & IOMAP_DIO_COW) {
412 error = xfs_reflink_end_cow(ip, offset, size);
413 if (error)
414 return error;
415 }
416
417
418
419
420
421
422
423 if (flags & IOMAP_DIO_UNWRITTEN)
424 return xfs_iomap_write_unwritten(ip, offset, size, true);
425
426
427
428
429
430
431
432
433
434
435
436
437 spin_lock(&ip->i_flags_lock);
438 if (offset + size > i_size_read(inode)) {
439 i_size_write(inode, offset + size);
440 spin_unlock(&ip->i_flags_lock);
441 error = xfs_setfilesize(ip, offset, size);
442 } else {
443 spin_unlock(&ip->i_flags_lock);
444 }
445
446 return error;
447}
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474STATIC ssize_t
475xfs_file_dio_aio_write(
476 struct kiocb *iocb,
477 struct iov_iter *from)
478{
479 struct file *file = iocb->ki_filp;
480 struct address_space *mapping = file->f_mapping;
481 struct inode *inode = mapping->host;
482 struct xfs_inode *ip = XFS_I(inode);
483 struct xfs_mount *mp = ip->i_mount;
484 ssize_t ret = 0;
485 int unaligned_io = 0;
486 int iolock;
487 size_t count = iov_iter_count(from);
488 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
489 mp->m_rtdev_targp : mp->m_ddev_targp;
490
491
492 if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
493 return -EINVAL;
494
495
496
497
498
499
500
501
502 if ((iocb->ki_pos & mp->m_blockmask) ||
503 ((iocb->ki_pos + count) & mp->m_blockmask)) {
504 unaligned_io = 1;
505
506
507
508
509
510 if (xfs_is_reflink_inode(ip)) {
511 trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
512 return -EREMCHG;
513 }
514 iolock = XFS_IOLOCK_EXCL;
515 } else {
516 iolock = XFS_IOLOCK_SHARED;
517 }
518
519 if (iocb->ki_flags & IOCB_NOWAIT) {
520 if (!xfs_ilock_nowait(ip, iolock))
521 return -EAGAIN;
522 } else {
523 xfs_ilock(ip, iolock);
524 }
525
526 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
527 if (ret)
528 goto out;
529 count = iov_iter_count(from);
530
531
532
533
534
535
536 if (unaligned_io) {
537
538 if (iocb->ki_flags & IOCB_NOWAIT) {
539 if (atomic_read(&inode->i_dio_count))
540 return -EAGAIN;
541 } else {
542 inode_dio_wait(inode);
543 }
544 } else if (iolock == XFS_IOLOCK_EXCL) {
545 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
546 iolock = XFS_IOLOCK_SHARED;
547 }
548
549 trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
550 ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io);
551out:
552 xfs_iunlock(ip, iolock);
553
554
555
556
557
558 ASSERT(ret < 0 || ret == count);
559 return ret;
560}
561
562static noinline ssize_t
563xfs_file_dax_write(
564 struct kiocb *iocb,
565 struct iov_iter *from)
566{
567 struct inode *inode = iocb->ki_filp->f_mapping->host;
568 struct xfs_inode *ip = XFS_I(inode);
569 int iolock = XFS_IOLOCK_EXCL;
570 ssize_t ret, error = 0;
571 size_t count;
572 loff_t pos;
573
574 if (iocb->ki_flags & IOCB_NOWAIT) {
575 if (!xfs_ilock_nowait(ip, iolock))
576 return -EAGAIN;
577 } else {
578 xfs_ilock(ip, iolock);
579 }
580
581 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
582 if (ret)
583 goto out;
584
585 pos = iocb->ki_pos;
586 count = iov_iter_count(from);
587
588 trace_xfs_file_dax_write(ip, count, pos);
589 ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
590 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
591 i_size_write(inode, iocb->ki_pos);
592 error = xfs_setfilesize(ip, pos, ret);
593 }
594out:
595 xfs_iunlock(ip, iolock);
596 if (error)
597 return error;
598
599 if (ret > 0) {
600 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
601
602
603 ret = generic_write_sync(iocb, ret);
604 }
605 return ret;
606}
607
608STATIC ssize_t
609xfs_file_buffered_aio_write(
610 struct kiocb *iocb,
611 struct iov_iter *from)
612{
613 struct file *file = iocb->ki_filp;
614 struct address_space *mapping = file->f_mapping;
615 struct inode *inode = mapping->host;
616 struct xfs_inode *ip = XFS_I(inode);
617 ssize_t ret;
618 int enospc = 0;
619 int iolock;
620
621 if (iocb->ki_flags & IOCB_NOWAIT)
622 return -EOPNOTSUPP;
623
624write_retry:
625 iolock = XFS_IOLOCK_EXCL;
626 xfs_ilock(ip, iolock);
627
628 ret = xfs_file_aio_write_checks(iocb, from, &iolock);
629 if (ret)
630 goto out;
631
632
633 current->backing_dev_info = inode_to_bdi(inode);
634
635 trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
636 ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
637 if (likely(ret >= 0))
638 iocb->ki_pos += ret;
639
640
641
642
643
644
645
646
647
648
649 if (ret == -EDQUOT && !enospc) {
650 xfs_iunlock(ip, iolock);
651 enospc = xfs_inode_free_quota_eofblocks(ip);
652 if (enospc)
653 goto write_retry;
654 enospc = xfs_inode_free_quota_cowblocks(ip);
655 if (enospc)
656 goto write_retry;
657 iolock = 0;
658 } else if (ret == -ENOSPC && !enospc) {
659 struct xfs_eofblocks eofb = {0};
660
661 enospc = 1;
662 xfs_flush_inodes(ip->i_mount);
663
664 xfs_iunlock(ip, iolock);
665 eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
666 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
667 xfs_icache_free_cowblocks(ip->i_mount, &eofb);
668 goto write_retry;
669 }
670
671 current->backing_dev_info = NULL;
672out:
673 if (iolock)
674 xfs_iunlock(ip, iolock);
675
676 if (ret > 0) {
677 XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
678
679 ret = generic_write_sync(iocb, ret);
680 }
681 return ret;
682}
683
684STATIC ssize_t
685xfs_file_write_iter(
686 struct kiocb *iocb,
687 struct iov_iter *from)
688{
689 struct file *file = iocb->ki_filp;
690 struct address_space *mapping = file->f_mapping;
691 struct inode *inode = mapping->host;
692 struct xfs_inode *ip = XFS_I(inode);
693 ssize_t ret;
694 size_t ocount = iov_iter_count(from);
695
696 XFS_STATS_INC(ip->i_mount, xs_write_calls);
697
698 if (ocount == 0)
699 return 0;
700
701 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
702 return -EIO;
703
704 if (IS_DAX(inode))
705 return xfs_file_dax_write(iocb, from);
706
707 if (iocb->ki_flags & IOCB_DIRECT) {
708
709
710
711
712
713
714 ret = xfs_file_dio_aio_write(iocb, from);
715 if (ret != -EREMCHG)
716 return ret;
717 }
718
719 return xfs_file_buffered_aio_write(iocb, from);
720}
721
722static void
723xfs_wait_dax_page(
724 struct inode *inode)
725{
726 struct xfs_inode *ip = XFS_I(inode);
727
728 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
729 schedule();
730 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
731}
732
733static int
734xfs_break_dax_layouts(
735 struct inode *inode,
736 bool *retry)
737{
738 struct page *page;
739
740 ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
741
742 page = dax_layout_busy_page(inode->i_mapping);
743 if (!page)
744 return 0;
745
746 *retry = true;
747 return ___wait_var_event(&page->_refcount,
748 atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
749 0, 0, xfs_wait_dax_page(inode));
750}
751
752int
753xfs_break_layouts(
754 struct inode *inode,
755 uint *iolock,
756 enum layout_break_reason reason)
757{
758 bool retry;
759 int error;
760
761 ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
762
763 do {
764 retry = false;
765 switch (reason) {
766 case BREAK_UNMAP:
767 error = xfs_break_dax_layouts(inode, &retry);
768 if (error || retry)
769 break;
770
771 case BREAK_WRITE:
772 error = xfs_break_leased_layouts(inode, iolock, &retry);
773 break;
774 default:
775 WARN_ON_ONCE(1);
776 error = -EINVAL;
777 }
778 } while (error == 0 && retry);
779
780 return error;
781}
782
783#define XFS_FALLOC_FL_SUPPORTED \
784 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
785 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
786 FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
787
788STATIC long
789xfs_file_fallocate(
790 struct file *file,
791 int mode,
792 loff_t offset,
793 loff_t len)
794{
795 struct inode *inode = file_inode(file);
796 struct xfs_inode *ip = XFS_I(inode);
797 long error;
798 enum xfs_prealloc_flags flags = 0;
799 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
800 loff_t new_size = 0;
801 bool do_file_insert = false;
802
803 if (!S_ISREG(inode->i_mode))
804 return -EINVAL;
805 if (mode & ~XFS_FALLOC_FL_SUPPORTED)
806 return -EOPNOTSUPP;
807
808 xfs_ilock(ip, iolock);
809 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
810 if (error)
811 goto out_unlock;
812
813 if (mode & FALLOC_FL_PUNCH_HOLE) {
814 error = xfs_free_file_space(ip, offset, len);
815 if (error)
816 goto out_unlock;
817 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
818 unsigned int blksize_mask = i_blocksize(inode) - 1;
819
820 if (offset & blksize_mask || len & blksize_mask) {
821 error = -EINVAL;
822 goto out_unlock;
823 }
824
825
826
827
828
829 if (offset + len >= i_size_read(inode)) {
830 error = -EINVAL;
831 goto out_unlock;
832 }
833
834 new_size = i_size_read(inode) - len;
835
836 error = xfs_collapse_file_space(ip, offset, len);
837 if (error)
838 goto out_unlock;
839 } else if (mode & FALLOC_FL_INSERT_RANGE) {
840 unsigned int blksize_mask = i_blocksize(inode) - 1;
841 loff_t isize = i_size_read(inode);
842
843 if (offset & blksize_mask || len & blksize_mask) {
844 error = -EINVAL;
845 goto out_unlock;
846 }
847
848
849
850
851
852 if (inode->i_sb->s_maxbytes - isize < len) {
853 error = -EFBIG;
854 goto out_unlock;
855 }
856 new_size = isize + len;
857
858
859 if (offset >= isize) {
860 error = -EINVAL;
861 goto out_unlock;
862 }
863 do_file_insert = true;
864 } else {
865 flags |= XFS_PREALLOC_SET;
866
867 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
868 offset + len > i_size_read(inode)) {
869 new_size = offset + len;
870 error = inode_newsize_ok(inode, new_size);
871 if (error)
872 goto out_unlock;
873 }
874
875 if (mode & FALLOC_FL_ZERO_RANGE)
876 error = xfs_zero_file_space(ip, offset, len);
877 else {
878 if (mode & FALLOC_FL_UNSHARE_RANGE) {
879 error = xfs_reflink_unshare(ip, offset, len);
880 if (error)
881 goto out_unlock;
882 }
883 error = xfs_alloc_file_space(ip, offset, len,
884 XFS_BMAPI_PREALLOC);
885 }
886 if (error)
887 goto out_unlock;
888 }
889
890 if (file->f_flags & O_DSYNC)
891 flags |= XFS_PREALLOC_SYNC;
892
893 error = xfs_update_prealloc_flags(ip, flags);
894 if (error)
895 goto out_unlock;
896
897
898 if (new_size) {
899 struct iattr iattr;
900
901 iattr.ia_valid = ATTR_SIZE;
902 iattr.ia_size = new_size;
903 error = xfs_vn_setattr_size(file_dentry(file), &iattr);
904 if (error)
905 goto out_unlock;
906 }
907
908
909
910
911
912
913
914 if (do_file_insert)
915 error = xfs_insert_file_space(ip, offset, len);
916
917out_unlock:
918 xfs_iunlock(ip, iolock);
919 return error;
920}
921
922STATIC int
923xfs_file_clone_range(
924 struct file *file_in,
925 loff_t pos_in,
926 struct file *file_out,
927 loff_t pos_out,
928 u64 len)
929{
930 return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
931 len, false);
932}
933
934STATIC int
935xfs_file_dedupe_range(
936 struct file *file_in,
937 loff_t pos_in,
938 struct file *file_out,
939 loff_t pos_out,
940 u64 len)
941{
942 return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
943 len, true);
944}
945
946STATIC int
947xfs_file_open(
948 struct inode *inode,
949 struct file *file)
950{
951 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
952 return -EFBIG;
953 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
954 return -EIO;
955 file->f_mode |= FMODE_NOWAIT;
956 return 0;
957}
958
959STATIC int
960xfs_dir_open(
961 struct inode *inode,
962 struct file *file)
963{
964 struct xfs_inode *ip = XFS_I(inode);
965 int mode;
966 int error;
967
968 error = xfs_file_open(inode, file);
969 if (error)
970 return error;
971
972
973
974
975
976 mode = xfs_ilock_data_map_shared(ip);
977 if (ip->i_d.di_nextents > 0)
978 error = xfs_dir3_data_readahead(ip, 0, -1);
979 xfs_iunlock(ip, mode);
980 return error;
981}
982
983STATIC int
984xfs_file_release(
985 struct inode *inode,
986 struct file *filp)
987{
988 return xfs_release(XFS_I(inode));
989}
990
991STATIC int
992xfs_file_readdir(
993 struct file *file,
994 struct dir_context *ctx)
995{
996 struct inode *inode = file_inode(file);
997 xfs_inode_t *ip = XFS_I(inode);
998 size_t bufsize;
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
1013
1014 return xfs_readdir(NULL, ip, ctx, bufsize);
1015}
1016
1017STATIC loff_t
1018xfs_file_llseek(
1019 struct file *file,
1020 loff_t offset,
1021 int whence)
1022{
1023 struct inode *inode = file->f_mapping->host;
1024
1025 if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
1026 return -EIO;
1027
1028 switch (whence) {
1029 default:
1030 return generic_file_llseek(file, offset, whence);
1031 case SEEK_HOLE:
1032 offset = iomap_seek_hole(inode, offset, &xfs_iomap_ops);
1033 break;
1034 case SEEK_DATA:
1035 offset = iomap_seek_data(inode, offset, &xfs_iomap_ops);
1036 break;
1037 }
1038
1039 if (offset < 0)
1040 return offset;
1041 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1042}
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054static vm_fault_t
1055__xfs_filemap_fault(
1056 struct vm_fault *vmf,
1057 enum page_entry_size pe_size,
1058 bool write_fault)
1059{
1060 struct inode *inode = file_inode(vmf->vma->vm_file);
1061 struct xfs_inode *ip = XFS_I(inode);
1062 vm_fault_t ret;
1063
1064 trace_xfs_filemap_fault(ip, pe_size, write_fault);
1065
1066 if (write_fault) {
1067 sb_start_pagefault(inode->i_sb);
1068 file_update_time(vmf->vma->vm_file);
1069 }
1070
1071 xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1072 if (IS_DAX(inode)) {
1073 pfn_t pfn;
1074
1075 ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL, &xfs_iomap_ops);
1076 if (ret & VM_FAULT_NEEDDSYNC)
1077 ret = dax_finish_sync_fault(vmf, pe_size, pfn);
1078 } else {
1079 if (write_fault)
1080 ret = iomap_page_mkwrite(vmf, &xfs_iomap_ops);
1081 else
1082 ret = filemap_fault(vmf);
1083 }
1084 xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
1085
1086 if (write_fault)
1087 sb_end_pagefault(inode->i_sb);
1088 return ret;
1089}
1090
1091static vm_fault_t
1092xfs_filemap_fault(
1093 struct vm_fault *vmf)
1094{
1095
1096 return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
1097 IS_DAX(file_inode(vmf->vma->vm_file)) &&
1098 (vmf->flags & FAULT_FLAG_WRITE));
1099}
1100
1101static vm_fault_t
1102xfs_filemap_huge_fault(
1103 struct vm_fault *vmf,
1104 enum page_entry_size pe_size)
1105{
1106 if (!IS_DAX(file_inode(vmf->vma->vm_file)))
1107 return VM_FAULT_FALLBACK;
1108
1109
1110 return __xfs_filemap_fault(vmf, pe_size,
1111 (vmf->flags & FAULT_FLAG_WRITE));
1112}
1113
1114static vm_fault_t
1115xfs_filemap_page_mkwrite(
1116 struct vm_fault *vmf)
1117{
1118 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1119}
1120
1121
1122
1123
1124
1125
1126static vm_fault_t
1127xfs_filemap_pfn_mkwrite(
1128 struct vm_fault *vmf)
1129{
1130
1131 return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
1132}
1133
1134static const struct vm_operations_struct xfs_file_vm_ops = {
1135 .fault = xfs_filemap_fault,
1136 .huge_fault = xfs_filemap_huge_fault,
1137 .map_pages = filemap_map_pages,
1138 .page_mkwrite = xfs_filemap_page_mkwrite,
1139 .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
1140};
1141
1142STATIC int
1143xfs_file_mmap(
1144 struct file *filp,
1145 struct vm_area_struct *vma)
1146{
1147
1148
1149
1150
1151 if (!IS_DAX(file_inode(filp)) && (vma->vm_flags & VM_SYNC))
1152 return -EOPNOTSUPP;
1153
1154 file_accessed(filp);
1155 vma->vm_ops = &xfs_file_vm_ops;
1156 if (IS_DAX(file_inode(filp)))
1157 vma->vm_flags |= VM_HUGEPAGE;
1158 return 0;
1159}
1160
1161const struct file_operations xfs_file_operations = {
1162 .llseek = xfs_file_llseek,
1163 .read_iter = xfs_file_read_iter,
1164 .write_iter = xfs_file_write_iter,
1165 .splice_read = generic_file_splice_read,
1166 .splice_write = iter_file_splice_write,
1167 .unlocked_ioctl = xfs_file_ioctl,
1168#ifdef CONFIG_COMPAT
1169 .compat_ioctl = xfs_file_compat_ioctl,
1170#endif
1171 .mmap = xfs_file_mmap,
1172 .mmap_supported_flags = MAP_SYNC,
1173 .open = xfs_file_open,
1174 .release = xfs_file_release,
1175 .fsync = xfs_file_fsync,
1176 .get_unmapped_area = thp_get_unmapped_area,
1177 .fallocate = xfs_file_fallocate,
1178 .clone_file_range = xfs_file_clone_range,
1179 .dedupe_file_range = xfs_file_dedupe_range,
1180};
1181
1182const struct file_operations xfs_dir_file_operations = {
1183 .open = xfs_dir_open,
1184 .read = generic_read_dir,
1185 .iterate_shared = xfs_file_readdir,
1186 .llseek = generic_file_llseek,
1187 .unlocked_ioctl = xfs_file_ioctl,
1188#ifdef CONFIG_COMPAT
1189 .compat_ioctl = xfs_file_compat_ioctl,
1190#endif
1191 .fsync = xfs_dir_fsync,
1192};
1193