Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gen_init_cpio: attempt copy_file_range for file data

The copy_file_range syscall can improve copy performance by cloning
extents between cpio archive source and destination files.
Existing read / write based copy logic is retained for fallback in case
the copy_file_range syscall is unsupported or unavailable due to
cross-filesystem EXDEV, etc.

Clone or reflink, as opposed to copy, of source file extents into the
output cpio archive may (e.g. on Btrfs and XFS) require alignment of the
output to the filesystem block size. This could be achieved by inserting
padding entries into the cpio archive manifest.

Signed-off-by: David Disseldorp <ddiss@suse.de>
Reviewed-by: Nicolas Schier <nsc@kernel.org>
Link: https://lore.kernel.org/r/20250819032607.28727-4-ddiss@suse.de
Signed-off-by: Nathan Chancellor <nathan@kernel.org>

authored by

David Disseldorp and committed by
Nathan Chancellor
97169cd6 ae18b940

+12 -1
+12 -1
usr/gen_init_cpio.c
··· 354 354 int namesize; 355 355 unsigned int i; 356 356 uint32_t csum = 0; 357 + ssize_t this_read; 357 358 358 359 mode |= S_IFREG; 359 360 ··· 430 429 push_pad(padlen(offset, 4)) < 0) 431 430 goto error; 432 431 432 + if (size) { 433 + this_read = copy_file_range(file, NULL, outfd, NULL, size, 0); 434 + if (this_read > 0) { 435 + if (this_read > size) 436 + goto error; 437 + offset += this_read; 438 + size -= this_read; 439 + } 440 + /* short or failed copy falls back to read/write... */ 441 + } 442 + 433 443 while (size) { 434 444 unsigned char filebuf[65536]; 435 - ssize_t this_read; 436 445 size_t this_size = MIN(size, sizeof(filebuf)); 437 446 438 447 this_read = read(file, filebuf, this_size);