Skip to content

Commit 5467e85

Browse files
ddissnathanchance
authored andcommitted
gen_init_cpio: add -a <data_align> as reflink optimization
As described in buffer-format.rst, the existing initramfs.c extraction logic works fine if the cpio filename field is padded out with trailing zeros, with a caveat that the padded namesize can't exceed PATH_MAX. Add filename zero-padding logic to gen_init_cpio, which can be triggered via the new -a <data_align> parameter. Performance and storage utilization is improved for Btrfs and XFS workloads, as copy_file_range can reflink the entire source file into a filesystem block-size aligned destination offset within the cpio archive. Btrfs benchmarks run on 6.15.8-1-default (Tumbleweed) x86_64 host: > truncate --size=2G /tmp/backing.img > /sbin/mkfs.btrfs /tmp/backing.img ... Sector size: 4096 (CPU page size: 4096) ... > sudo mount /tmp/backing.img mnt > sudo chown $USER mnt > cd mnt mnt> dd if=/dev/urandom of=foo bs=1M count=20 && cat foo >/dev/null ... mnt> echo "file /foo foo 0755 0 0" > list mnt> perf stat -r 10 gen_init_cpio -o unaligned_btrfs list ... 0.023496 +- 0.000472 seconds time elapsed ( +- 2.01% ) mnt> perf stat -r 10 gen_init_cpio -o aligned_btrfs -a 4096 list ... 0.0010010 +- 0.0000565 seconds time elapsed ( +- 5.65% ) mnt> /sbin/xfs_io -c "fiemap -v" unaligned_btrfs unaligned_btrfs: EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS 0: [0..40967]: 695040..736007 40968 0x1 mnt> /sbin/xfs_io -c "fiemap -v" aligned_btrfs aligned_btrfs: EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS 0: [0..7]: 26768..26775 8 0x0 1: [8..40967]: 269056..310015 40960 0x2000 2: [40968..40975]: 26776..26783 8 0x1 mnt> /sbin/btrfs fi du unaligned_btrfs aligned_btrfs Total Exclusive Set shared Filename 20.00MiB 20.00MiB 0.00B unaligned_btrfs 20.01MiB 8.00KiB 20.00MiB aligned_btrfs XFS benchmarks run on same host: > sudo umount mnt && rm /tmp/backing.img > truncate --size=2G /tmp/backing.img > /sbin/mkfs.xfs /tmp/backing.img ... = reflink=1 ... data = bsize=4096 blocks=524288, imaxpct=25 ... > sudo mount /tmp/backing.img mnt > sudo chown $USER mnt > cd mnt mnt> dd if=/dev/urandom of=foo bs=1M count=20 && cat foo >/dev/null ... mnt> echo "file /foo foo 0755 0 0" > list mnt> perf stat -r 10 gen_init_cpio -o unaligned_xfs list ... 0.011069 +- 0.000469 seconds time elapsed ( +- 4.24% ) mnt> perf stat -r 10 gen_init_cpio -o aligned_xfs -a 4096 list ... 0.001273 +- 0.000288 seconds time elapsed ( +- 22.60% ) mnt> /sbin/xfs_io -c "fiemap -v" unaligned_xfs unaligned_xfs: EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS 0: [0..40967]: 106176..147143 40968 0x0 1: [40968..65023]: 147144..171199 24056 0x801 mnt> /sbin/xfs_io -c "fiemap -v" aligned_xfs aligned_xfs: EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS 0: [0..7]: 120..127 8 0x0 1: [8..40967]: 192..41151 40960 0x2000 2: [40968..40975]: 236728..236735 8 0x0 3: [40976..106495]: 236736..302255 65520 0x801 The alignment is best-effort; a stderr message is printed if alignment can't be achieved due to PATH_MAX overrun, with fallback to non-padded filename. This allows it to still be useful for opportunistic alignment, e.g. on aarch64 Btrfs with 64K block-size. Alignment failure messages provide an indicator that reordering of the cpio-manifest may be beneficial. Archive read performance for reflinked initramfs images may suffer due to the effects of fragmentation, particularly on spinning disks. To mitigate excessive fragmentation, files with lengths less than data_align aren't padded. Signed-off-by: David Disseldorp <[email protected]> Reviewed-by: Nicolas Schier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Nathan Chancellor <[email protected]>
1 parent 7c1f14f commit 5467e85

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

usr/gen_init_cpio.c

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
#define CPIO_TRAILER "TRAILER!!!"
2929
#define padlen(_off, _align) (((_align) - ((_off) & ((_align) - 1))) % (_align))
3030

31-
static char padding[512];
31+
/* zero-padding the filename field for data alignment is limited by PATH_MAX */
32+
static char padding[PATH_MAX];
3233
static unsigned int offset;
3334
static unsigned int ino = 721;
3435
static time_t default_mtime;
3536
static bool do_file_mtime;
3637
static bool do_csum = false;
3738
static int outfd = STDOUT_FILENO;
39+
static unsigned int dalign;
3840

3941
struct file_handler {
4042
const char *type;
@@ -359,7 +361,7 @@ static int cpio_mkfile(const char *name, const char *location,
359361
int file, retval, len;
360362
int rc = -1;
361363
time_t mtime;
362-
int namesize;
364+
int namesize, namepadlen;
363365
unsigned int i;
364366
uint32_t csum = 0;
365367
ssize_t this_read;
@@ -407,14 +409,27 @@ static int cpio_mkfile(const char *name, const char *location,
407409
}
408410

409411
size = 0;
412+
namepadlen = 0;
410413
for (i = 1; i <= nlinks; i++) {
411-
/* data goes on last link */
412-
if (i == nlinks)
413-
size = buf.st_size;
414-
415414
if (name[0] == '/')
416415
name++;
417416
namesize = strlen(name) + 1;
417+
418+
/* data goes on last link, after any alignment padding */
419+
if (i == nlinks)
420+
size = buf.st_size;
421+
422+
if (dalign && size > dalign) {
423+
namepadlen = padlen(offset + CPIO_HDR_LEN + namesize,
424+
dalign);
425+
if (namesize + namepadlen > PATH_MAX) {
426+
fprintf(stderr,
427+
"%s: best-effort alignment %u missed\n",
428+
name, dalign);
429+
namepadlen = 0;
430+
}
431+
}
432+
418433
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
419434
"%08lX%08X%08X%08X%08X%08X%08X",
420435
do_csum ? "070702" : "070701", /* magic */
@@ -429,13 +444,13 @@ static int cpio_mkfile(const char *name, const char *location,
429444
1, /* minor */
430445
0, /* rmajor */
431446
0, /* rminor */
432-
namesize, /* namesize */
447+
namesize + namepadlen, /* namesize */
433448
size ? csum : 0); /* chksum */
434449
offset += len;
435450

436451
if (len != CPIO_HDR_LEN ||
437452
push_buf(name, namesize) < 0 ||
438-
push_pad(padlen(offset, 4)) < 0)
453+
push_pad(namepadlen ? namepadlen : padlen(offset, 4)) < 0)
439454
goto error;
440455

441456
if (size) {
@@ -552,7 +567,7 @@ static int cpio_mkfile_line(const char *line)
552567
static void usage(const char *prog)
553568
{
554569
fprintf(stderr, "Usage:\n"
555-
"\t%s [-t <timestamp>] [-c] [-o <output_file>] <cpio_list>\n"
570+
"\t%s [-t <timestamp>] [-c] [-o <output_file>] [-a <data_align>] <cpio_list>\n"
556571
"\n"
557572
"<cpio_list> is a file containing newline separated entries that\n"
558573
"describe the files to be included in the initramfs archive:\n"
@@ -590,7 +605,10 @@ static void usage(const char *prog)
590605
"The default is to use the current time for all files, but\n"
591606
"preserve modification time for regular files.\n"
592607
"-c: calculate and store 32-bit checksums for file data.\n"
593-
"<output_file>: write cpio to this file instead of stdout\n",
608+
"<output_file>: write cpio to this file instead of stdout\n"
609+
"<data_align>: attempt to align file data by zero-padding the\n"
610+
"filename field up to data_align. Must be a multiple of 4.\n"
611+
"Alignment is best-effort; PATH_MAX limits filename padding.\n",
594612
prog);
595613
}
596614

@@ -632,7 +650,7 @@ int main (int argc, char *argv[])
632650

633651
default_mtime = time(NULL);
634652
while (1) {
635-
int opt = getopt(argc, argv, "t:cho:");
653+
int opt = getopt(argc, argv, "t:cho:a:");
636654
char *invalid;
637655

638656
if (opt == -1)
@@ -661,6 +679,15 @@ int main (int argc, char *argv[])
661679
exit(1);
662680
}
663681
break;
682+
case 'a':
683+
dalign = strtoul(optarg, &invalid, 10);
684+
if (!*optarg || *invalid || (dalign & 3)) {
685+
fprintf(stderr, "Invalid data_align: %s\n",
686+
optarg);
687+
usage(argv[0]);
688+
exit(1);
689+
}
690+
break;
664691
case 'h':
665692
case '?':
666693
usage(argv[0]);

0 commit comments

Comments
 (0)