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