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