git: df2b8b494412 - stable/14 - vfs_vnops.c: Use va_bytes >= va_size hint to avoid SEEK_DATA/SEEKHOLE

From: Rick Macklem <rmacklem_at_FreeBSD.org>
Date: Fri, 29 Mar 2024 01:16:39 UTC
The branch stable/14 has been updated by rmacklem:

URL: https://cgit.FreeBSD.org/src/commit/?id=df2b8b494412259050254e04cb4d2e13ab815783

commit df2b8b494412259050254e04cb4d2e13ab815783
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2024-03-15 00:35:32 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2024-03-29 01:15:09 +0000

    vfs_vnops.c: Use va_bytes >= va_size hint to avoid SEEK_DATA/SEEKHOLE
    
    vn_generic_copy_file_range() tries to maintain holes
    in file ranges being copied, using SEEK_DATA/SEEK_HOLE
    where possible,
    
    Unfortunately SEEK_DATA/SEEK_HOLE operations can take
    a long time under certain circumstances.
    Although it is not currently possible to know if a file has
    unallocated data regions, the case where va_bytes >= va_size
    is a strong hint that there are no unallocated data regions.
    This hint does not work well for file systems doing compression,
    but since it is only a hint, it is still useful.
    
    For the case of va_bytes >= va_size, avoid doing SEEK_DATA/SEEK_HOLE.
    
    (cherry picked from commit 89f1dcb3eb468e4cbaebd1ccde9a643d85f1282e)
---
 sys/kern/vfs_vnops.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index a16e137755b1..51386d0e9581 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -3336,14 +3336,15 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp,
     struct vnode *outvp, off_t *outoffp, size_t *lenp, unsigned int flags,
     struct ucred *incred, struct ucred *outcred, struct thread *fsize_td)
 {
+	struct vattr inva;
 	struct mount *mp;
 	off_t startoff, endoff, xfer, xfer2;
 	u_long blksize;
 	int error, interrupted;
-	bool cantseek, readzeros, eof, lastblock, holetoeof;
+	bool cantseek, readzeros, eof, lastblock, holetoeof, sparse;
 	ssize_t aresid, r = 0;
 	size_t copylen, len, savlen;
-	off_t insize, outsize;
+	off_t outsize;
 	char *dat;
 	long holein, holeout;
 	struct timespec curts, endts;
@@ -3359,11 +3360,26 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp,
 		goto out;
 	if (VOP_PATHCONF(invp, _PC_MIN_HOLE_SIZE, &holein) != 0)
 		holein = 0;
-	error = vn_getsize_locked(invp, &insize, incred);
+	error = VOP_GETATTR(invp, &inva, incred);
+	if (error == 0 && inva.va_size > OFF_MAX)
+		error = EFBIG;
 	VOP_UNLOCK(invp);
 	if (error != 0)
 		goto out;
 
+	/*
+	 * Use va_bytes >= va_size as a hint that the file does not have
+	 * sufficient holes to justify the overhead of doing FIOSEEKHOLE.
+	 * This hint does not work well for file systems doing compression
+	 * and may fail when allocations for extended attributes increases
+	 * the value of va_bytes to >= va_size.
+	 */
+	sparse = true;
+	if (holein != 0 && inva.va_bytes >= inva.va_size) {
+		holein = 0;
+		sparse = false;
+	}
+
 	mp = NULL;
 	error = vn_start_write(outvp, &mp, V_WAIT);
 	if (error == 0)
@@ -3397,9 +3413,9 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp,
 			error = vn_getsize_locked(outvp, &outsize, outcred);
 		if (error == 0 && outsize > *outoffp &&
 		    *outoffp <= OFF_MAX - len && outsize <= *outoffp + len &&
-		    *inoffp < insize &&
-		    *outoffp <= OFF_MAX - (insize - *inoffp) &&
-		    outsize <= *outoffp + (insize - *inoffp)) {
+		    *inoffp < inva.va_size &&
+		    *outoffp <= OFF_MAX - (inva.va_size - *inoffp) &&
+		    outsize <= *outoffp + (inva.va_size - *inoffp)) {
 #ifdef MAC
 			error = mac_vnode_check_write(curthread->td_ucred,
 			    outcred, outvp);
@@ -3417,7 +3433,7 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp,
 	if (error != 0)
 		goto out;
 
-	if (holein == 0 && holeout > 0) {
+	if (sparse && holein == 0 && holeout > 0) {
 		/*
 		 * For this special case, the input data will be scanned
 		 * for blocks of all 0 bytes.  For these blocks, the
@@ -3488,7 +3504,7 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp,
 			error = VOP_IOCTL(invp, FIOSEEKDATA, &startoff, 0,
 			    incred, curthread);
 			if (error == ENXIO) {
-				startoff = endoff = insize;
+				startoff = endoff = inva.va_size;
 				eof = holetoeof = true;
 				error = 0;
 			}
@@ -3551,6 +3567,8 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp,
 			cantseek = false;
 		} else {
 			cantseek = true;
+			if (!sparse)
+				cantseek = false;
 			startoff = *inoffp;
 			copylen = len;
 			error = 0;