Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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