git: 471d0cb24c6d - main - pax: Use copy_file_range

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 14:45:57 UTC
The branch main has been updated by des:

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

commit 471d0cb24c6d1252f33dd1cdb6c907dd3b6b5ea4
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-16 14:44:24 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-16 14:44:24 +0000

    pax: Use copy_file_range
    
    Use copy_file_range(2) instead of our own inefficient and unreliable
    attempt at detecting holes.  If copy_file_range(2) is not available,
    which in theory should be never, we fall back to a simple read(2) /
    write(2) loop which makes no attempt to copy holes.  This should
    speed up pax copy mode and cpio passthrough mode significantly.
    
    This includes refactoring much of the existing I/O code to use the
    size_t and ssize_t instead of int where appropriate.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    obiwac
    Differential Revision:  https://reviews.freebsd.org/D58470
---
 bin/pax/ar_io.c    |  35 ++++++------
 bin/pax/buf_subs.c | 163 +++++++++++++++++++++++++++++------------------------
 bin/pax/extern.h   |  12 ++--
 3 files changed, 114 insertions(+), 96 deletions(-)

diff --git a/bin/pax/ar_io.c b/bin/pax/ar_io.c
index c595870aaa5e..686c211b301f 100644
--- a/bin/pax/ar_io.c
+++ b/bin/pax/ar_io.c
@@ -484,8 +484,8 @@ ar_app_ok(void)
 
 	if (!invld_rec)
 		return(0);
-	paxwarn(1,"Cannot append, device record size %d does not support %s spec",
-		rdblksz, argv0);
+	paxwarn(1, "Cannot append, device record size %zu "
+	    "does not support %s spec", rdblksz, argv0);
 	return(-1);
 }
 
@@ -498,10 +498,10 @@ ar_app_ok(void)
  *	Number of bytes in buffer. 0 for end of file, -1 for a read error.
  */
 
-int
-ar_read(char *buf, int cnt)
+ssize_t
+ar_read(char *buf, size_t cnt)
 {
-	int res = 0;
+	ssize_t res = 0;
 
 	/*
 	 * if last i/o was in error, no more reads until reset or new volume
@@ -523,7 +523,7 @@ ar_read(char *buf, int cnt)
 			 * manner.
 			 */
 			io_ok = 1;
-			if (res != rdblksz) {
+			if ((size_t)res != rdblksz) {
 				/*
 				 * Record size changed. If this happens on
 				 * any record after the first, we probably have
@@ -581,8 +581,8 @@ ar_read(char *buf, int cnt)
  *	error in the archive occurred.
  */
 
-int
-ar_write(char *buf, int bsz)
+ssize_t
+ar_write(char *buf, size_t bsz)
 {
 	int res;
 	off_t cpos;
@@ -594,20 +594,21 @@ ar_write(char *buf, int bsz)
 	if (lstrval <= 0)
 		return(lstrval);
 
-	if ((res = write(arfd, buf, bsz)) == bsz) {
-		wr_trail = 1;
-		io_ok = 1;
-		return(bsz);
+	if ((res = write(arfd, buf, bsz)) < 0) {
+		lstrval = -1;
+	} else {
+		lstrval = 0;
+		if ((size_t)res == bsz) {
+			wr_trail = 1;
+			io_ok = 1;
+			return(bsz);
+		}
 	}
+
 	/*
 	 * write broke, see what we can do with it. We try to send any partial
 	 * writes that may violate pax spec to the next archive volume.
 	 */
-	if (res < 0)
-		lstrval = res;
-	else
-		lstrval = 0;
-
 	switch (artyp) {
 	case ISREG:
 		if ((res > 0) && (res % BLKMULT)) {
diff --git a/bin/pax/buf_subs.c b/bin/pax/buf_subs.c
index 7f9b31881d56..0ccd8e77af99 100644
--- a/bin/pax/buf_subs.c
+++ b/bin/pax/buf_subs.c
@@ -35,10 +35,13 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+
 #include <errno.h>
-#include <unistd.h>
+#include <limits.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
+
 #include "pax.h"
 #include "extern.h"
 
@@ -54,14 +57,14 @@
  * limit on blocking size is removed (though that will violate pax spec)
  * MAXBLK define and tests will also need to be updated.
  */
-static char bufmem[MAXBLK+BLKMULT];	/* i/o buffer + pushback id space */
+static char bufmem[MAXBLK + BLKMULT];	/* i/o buffer + pushback id space */
 static char *buf;			/* normal start of i/o buffer */
 static char *bufend;			/* end or last char in i/o buffer */
 static char *bufpt;			/* read/write point in i/o buffer */
-int blksz = MAXBLK;			/* block input/output size in bytes */
-int wrblksz;				/* user spec output size in bytes */
+size_t blksz = MAXBLK;			/* block input/output size in bytes */
+size_t wrblksz;				/* user spec output size in bytes */
 int maxflt = MAXFLT;			/* MAX consecutive media errors */
-int rdblksz;				/* first read blksize (tapes only) */
+size_t rdblksz;				/* first read blksize (tapes only) */
 off_t wrlimit;				/* # of bytes written per archive vol */
 off_t wrcnt;				/* # of bytes written on current vol */
 off_t rdcnt;				/* # of bytes read on current vol */
@@ -87,18 +90,18 @@ wr_start(void)
 	if (!wrblksz)
 		wrblksz = frmt->bsz;
 	if (wrblksz > MAXBLK) {
-		paxwarn(1, "Write block size of %d too large, maximum is: %d",
-			wrblksz, MAXBLK);
+		paxwarn(1, "Write block size of %zu too large, maximum is: %d",
+		    wrblksz, MAXBLK);
 		return(-1);
 	}
 	if (wrblksz % BLKMULT) {
-		paxwarn(1, "Write block size of %d is not a %d byte multiple",
+		paxwarn(1, "Write block size of %zu is not a %d byte multiple",
 		    wrblksz, BLKMULT);
 		return(-1);
 	}
 	if (wrblksz > MAXBLK_POSIX) {
-		paxwarn(0, "Write block size of %d larger than POSIX max %d, archive may not be portable",
-			wrblksz, MAXBLK_POSIX);
+		paxwarn(0, "Write block size of %zu larger than POSIX max %d, "
+		    "archive may not be portable", wrblksz, MAXBLK_POSIX);
 		return(-1);
 	}
 
@@ -132,13 +135,13 @@ rd_start(void)
 	buf = &(bufmem[BLKMULT]);
 	if ((act == APPND) && wrblksz) {
 		if (wrblksz > MAXBLK) {
-			paxwarn(1,"Write block size %d too large, maximum is: %d",
-				wrblksz, MAXBLK);
+			paxwarn(1, "Write block size %zu too large, "
+			    "maximum is: %d", wrblksz, MAXBLK);
 			return(-1);
 		}
 		if (wrblksz % BLKMULT) {
-			paxwarn(1, "Write block size %d is not a %d byte multiple",
-			wrblksz, BLKMULT);
+			paxwarn(1, "Write block size %zu is not a "
+			    "%d byte multiple", wrblksz, BLKMULT);
 			return(-1);
 		}
 	}
@@ -735,82 +738,95 @@ rd_wrfile(ARCHD *arcn, int ofd, off_t *left)
 	return(0);
 }
 
+/*
+ * Copy up to len (limited to SSIZE_MAX) bytes from infd to outfd with
+ * similar semantics to copy_file_range(2), for use when the latter does
+ * not work.  Just like copy_file_range(2), this may terminate early for a
+ * variety of reasons, and should therefore be called in a loop until it
+ * returns zero or an error.
+ */
+
+static ssize_t
+copy_file_fallback(int infd, int outfd, size_t len)
+{
+	char *pos;
+	ssize_t rlen = 0, wlen = 0;
+	size_t total = 0;
+
+	if (len > SSIZE_MAX)
+		len = SSIZE_MAX;
+	if (len == 0)
+		return (0);
+	while (total < len) {
+		if ((rlen = read(infd, buf, MIN(blksz, len))) <= 0)
+			break;
+		for (pos = buf; rlen > 0; pos += wlen, rlen -= wlen)
+			if ((wlen = write(outfd, pos, rlen)) < 0)
+				break;
+		total += pos - buf;
+		if (wlen < 0)
+			break;
+	}
+	/* report a partial success as success */
+	if (total > 0)
+		return (total);
+	if (rlen < 0 || wlen < 0)
+		return (-1);
+	return (0);
+}
+
 /*
  * cp_file()
- *	copy the contents of one file to another. used during -rw phase of pax
- *	just as in rd_wrfile() we use a special write function to write the
- *	destination file so we can properly copy files with holes.
+ *	copy the contents of one file to another, used during -rw phase of pax.
  */
 
 void
 cp_file(ARCHD *arcn, int fd1, int fd2)
 {
-	int cnt;
-	off_t cpcnt = 0L;
-	int res = 0;
-	char *fnm = arcn->name;
-	int no_hole = 0;
-	int isem = 1;
-	int rem;
-	int sz = MINFBSZ;
 	struct stat sb;
+	ssize_t res, total = 0;
 
 	/*
-	 * check for holes in the source file. If none, we will use regular
-	 * write instead of file write.
-	 */
-	 if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size)
-		++no_hole;
-
-	/*
-	 * pass the blocksize of the file being written to the write routine,
-	 * if the size is zero, use the default MINFBSZ
+	 * Try copy_file_range(2) first.  On FreeBSD, it works in almost
+	 * all situations, even across file systems, and even when the
+	 * underlying file system does not implement it.
 	 */
-	if (fstat(fd2, &sb) == 0) {
-		if (sb.st_blksize > 0)
-			sz = sb.st_blksize;
-	} else
-		syswarn(0,errno,"Unable to obtain block size for file %s",fnm);
-	rem = sz;
+	for (;;) {
+		res = copy_file_range(fd1, NULL, fd2, NULL, SSIZE_MAX, 0);
+		if (res <= 0)
+			break;
+		total += res;
+	}
 
 	/*
-	 * read the source file and copy to destination file until EOF
+	 * Fall back to a standard copy loop.
 	 */
-	for(;;) {
-		if ((cnt = read(fd1, buf, blksz)) <= 0)
-			break;
-		if (no_hole)
-			res = write(fd2, buf, cnt);
-		else
-			res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm);
-		if (res != cnt)
-			break;
-		cpcnt += cnt;
+	if (res < 0 && errno == EINVAL) {
+		for (;;) {
+			res = copy_file_fallback(fd1, fd2, SSIZE_MAX);
+			if (res <= 0)
+				break;
+			total += res;
+		}
 	}
 
 	/*
 	 * check to make sure the copy is valid.
 	 */
-	if (res < 0)
+	if (res < 0) {
 		syswarn(1, errno, "Failed write during copy of %s to %s",
-			arcn->org_name, arcn->name);
-	else if (cpcnt != arcn->sb.st_size)
+		    arcn->org_name, arcn->name);
+	} else if (total != arcn->sb.st_size) {
 		paxwarn(1, "File %s changed size during copy to %s",
-			arcn->org_name, arcn->name);
-	else if (fstat(fd1, &sb) < 0)
+		    arcn->org_name, arcn->name);
+	} else if (fstat(fd1, &sb) < 0) {
 		syswarn(1, errno, "Failed stat of %s", arcn->org_name);
-	else if (arcn->sb.st_mtime != sb.st_mtime)
+	} else if (arcn->sb.st_mtime != sb.st_mtime ||
+	    arcn->sb.st_size != sb.st_size) {
 		paxwarn(1, "File %s was modified during copy to %s",
-			arcn->org_name, arcn->name);
+		    arcn->org_name, arcn->name);
+	}
 
-	/*
-	 * if the last block has a file hole (all zero), we must make sure this
-	 * gets updated in the file. We force the last block of zeros to be
-	 * written. just closing with the file offset moved forward may not put
-	 * a hole at the end of the file.
-	 */
-	if (!no_hole && isem && (arcn->sb.st_size > 0L))
-		file_flush(fd2, fnm, isem);
 	return;
 }
 
@@ -872,11 +888,11 @@ buf_fill(void)
  */
 
 int
-buf_flush(int bufcnt)
+buf_flush(size_t bufcnt)
 {
-	int cnt;
-	int push = 0;
-	int totcnt = 0;
+	ssize_t cnt;
+	size_t push = 0;
+	size_t totcnt = 0;
 
 	/*
 	 * if we have reached the user specified byte count for each archive
@@ -916,8 +932,9 @@ buf_flush(int bufcnt)
 		/*
 		 * write a block and check if it all went out ok
 		 */
-		cnt = ar_write(buf, blksz);
-		if (cnt == blksz) {
+		if ((cnt = ar_write(buf, blksz)) < 0) {
+			break;
+		} else if ((size_t)cnt == blksz) {
 			/*
 			 * the write went ok
 			 */
@@ -937,7 +954,7 @@ buf_flush(int bufcnt)
 			} else
 				bufpt = buf;
 			return(totcnt);
-		} else if (cnt > 0) {
+		} else {
 			/*
 			 * Oh drat we got a partial write!
 			 * if format doesn't care about alignment let it go,
diff --git a/bin/pax/extern.h b/bin/pax/extern.h
index 26e5f6b6151b..fa203b73f58c 100644
--- a/bin/pax/extern.h
+++ b/bin/pax/extern.h
@@ -49,8 +49,8 @@ void ar_close(void);
 void ar_drain(void);
 int ar_set_wr(void);
 int ar_app_ok(void);
-int ar_read(char *, int);
-int ar_write(char *, int);
+ssize_t ar_read(char *, size_t);
+ssize_t ar_write(char *, size_t);
 int ar_rdsync(void);
 int ar_fow(off_t, off_t *);
 int ar_rev(off_t );
@@ -69,10 +69,10 @@ void copy(void);
 /*
  * buf_subs.c
  */
-extern int blksz;
-extern int wrblksz;
+extern size_t blksz;
+extern size_t wrblksz;
 extern int maxflt;
-extern int rdblksz;
+extern size_t rdblksz;
 extern off_t wrlimit;
 extern off_t rdcnt;
 extern off_t wrcnt;
@@ -91,7 +91,7 @@ int wr_rdfile(ARCHD *, int, off_t *);
 int rd_wrfile(ARCHD *, int, off_t *);
 void cp_file(ARCHD *, int, int);
 int buf_fill(void);
-int buf_flush(int);
+int buf_flush(size_t);
 
 /*
  * cache.c