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