git: 322412b83bef - stable/13 - vfs_vnops.c: Use va_bytes >= va_size hint to avoid SEEK_DATA/SEEKHOLE

From: Rick Macklem <rmacklem_at_FreeBSD.org>
Date: Sat, 30 Mar 2024 21:01:07 UTC
The branch stable/13 has been updated by rmacklem:

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

commit 322412b83bef927afb6c807636a37a95d415422e
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2024-03-15 00:35:32 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2024-03-30 20:56:17 +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 | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index e095fb9df13b..6591b543ddea 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -3296,7 +3296,7 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp,
 	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;
 	char *dat;
@@ -3315,10 +3315,25 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp,
 	if (VOP_PATHCONF(invp, _PC_MIN_HOLE_SIZE, &holein) != 0)
 		holein = 0;
 	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)
@@ -3372,7 +3387,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
@@ -3506,6 +3521,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;