From: David Disseldorp <ddiss@suse.de>
To: linux-kbuild@vger.kernel.org, linux-fsdevel@vger.kernel.org
Cc: linux-next@vger.kernel.org, ddiss@suse.de, nsc@kernel.org
Subject: [PATCH v3 8/8] initramfs_test: add filename padding test case
Date: Tue, 19 Aug 2025 13:05:51 +1000 [thread overview]
Message-ID: <20250819032607.28727-9-ddiss@suse.de> (raw)
In-Reply-To: <20250819032607.28727-1-ddiss@suse.de>
Confirm that cpio filenames with multiple trailing zeros (accounted for
in namesize) extract successfully.
Signed-off-by: David Disseldorp <ddiss@suse.de>
---
init/initramfs_test.c | 68 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 67 insertions(+), 1 deletion(-)
diff --git a/init/initramfs_test.c b/init/initramfs_test.c
index 517e5e04e5ccf..da16b012322b9 100644
--- a/init/initramfs_test.c
+++ b/init/initramfs_test.c
@@ -45,8 +45,11 @@ static size_t fill_cpio(struct initramfs_test_cpio *cs, size_t csz, char *out)
c->mtime, c->filesize, c->devmajor, c->devminor,
c->rdevmajor, c->rdevminor, c->namesize, c->csum,
c->fname) + 1;
+
pr_debug("packing (%zu): %.*s\n", thislen, (int)thislen, pos);
- off += thislen;
+ if (thislen != CPIO_HDRLEN + c->namesize)
+ pr_debug("padded to: %u\n", CPIO_HDRLEN + c->namesize);
+ off += CPIO_HDRLEN + c->namesize;
while (off & 3)
out[off++] = '\0';
@@ -383,6 +386,68 @@ static void __init initramfs_test_many(struct kunit *test)
kfree(cpio_srcbuf);
}
+/*
+ * An initramfs filename is namesize in length, including the zero-terminator.
+ * A filename can be zero-terminated prior to namesize, with the remainder used
+ * as padding. This can be useful for e.g. alignment of file data segments with
+ * a 4KB filesystem block, allowing for extent sharing (reflinks) between cpio
+ * source and destination. This hack works with both GNU cpio and initramfs, as
+ * long as PATH_MAX isn't exceeded.
+ */
+static void __init initramfs_test_fname_pad(struct kunit *test)
+{
+ char *err;
+ size_t len;
+ struct file *file;
+ char fdata[] = "this file data is aligned at 4K in the archive";
+ struct test_fname_pad {
+ char padded_fname[4096 - CPIO_HDRLEN];
+ char cpio_srcbuf[CPIO_HDRLEN + PATH_MAX + 3 + sizeof(fdata)];
+ } *tbufs = kzalloc(sizeof(struct test_fname_pad), GFP_KERNEL);
+ struct initramfs_test_cpio c[] = { {
+ .magic = "070701",
+ .ino = 1,
+ .mode = S_IFREG | 0777,
+ .uid = 0,
+ .gid = 0,
+ .nlink = 1,
+ .mtime = 1,
+ .filesize = 0,
+ .devmajor = 0,
+ .devminor = 1,
+ .rdevmajor = 0,
+ .rdevminor = 0,
+ /* align file data at 4K archive offset via padded fname */
+ .namesize = 4096 - CPIO_HDRLEN,
+ .csum = 0,
+ .fname = tbufs->padded_fname,
+ .data = fdata,
+ .filesize = sizeof(fdata),
+ } };
+
+ memcpy(tbufs->padded_fname, "padded_fname", sizeof("padded_fname"));
+ len = fill_cpio(c, ARRAY_SIZE(c), tbufs->cpio_srcbuf);
+
+ err = unpack_to_rootfs(tbufs->cpio_srcbuf, len);
+ KUNIT_EXPECT_NULL(test, err);
+
+ file = filp_open(c[0].fname, O_RDONLY, 0);
+ if (IS_ERR(file)) {
+ KUNIT_FAIL(test, "open failed");
+ goto out;
+ }
+
+ /* read back file contents into @cpio_srcbuf and confirm match */
+ len = kernel_read(file, tbufs->cpio_srcbuf, c[0].filesize, NULL);
+ KUNIT_EXPECT_EQ(test, len, c[0].filesize);
+ KUNIT_EXPECT_MEMEQ(test, tbufs->cpio_srcbuf, c[0].data, len);
+
+ fput(file);
+ KUNIT_EXPECT_EQ(test, init_unlink(c[0].fname), 0);
+out:
+ kfree(tbufs);
+}
+
/*
* The kunit_case/_suite struct cannot be marked as __initdata as this will be
* used in debugfs to retrieve results after test has run.
@@ -394,6 +459,7 @@ static struct kunit_case __refdata initramfs_test_cases[] = {
KUNIT_CASE(initramfs_test_csum),
KUNIT_CASE(initramfs_test_hardlink),
KUNIT_CASE(initramfs_test_many),
+ KUNIT_CASE(initramfs_test_fname_pad),
{},
};
--
2.43.0
next prev parent reply other threads:[~2025-08-19 3:50 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-19 3:05 [PATCH v3 0/8] gen_init_cpio: add copy_file_range / reflink support David Disseldorp
2025-08-19 3:05 ` [PATCH v3 1/8] gen_init_cpio: write to fd instead of stdout stream David Disseldorp
2025-08-19 3:05 ` [PATCH v3 2/8] gen_init_cpio: support -o <output_file> parameter David Disseldorp
2025-08-19 3:05 ` [PATCH v3 3/8] gen_init_cpio: attempt copy_file_range for file data David Disseldorp
2025-08-19 3:05 ` [PATCH v3 4/8] gen_init_cpio: avoid duplicate strlen calls David Disseldorp
2025-08-19 3:05 ` [PATCH v3 5/8] gen_initramfs.sh: use gen_init_cpio -o parameter David Disseldorp
2025-08-19 3:05 ` [PATCH v3 6/8] docs: initramfs: file data alignment via name padding David Disseldorp
2025-08-19 3:05 ` [PATCH v3 7/8] gen_init_cpio: add -a <data_align> as reflink optimization David Disseldorp
2025-08-19 3:05 ` David Disseldorp [this message]
2025-08-19 20:16 ` [PATCH v3 8/8] initramfs_test: add filename padding test case kernel test robot
2025-08-20 1:13 ` David Disseldorp
2025-08-20 21:02 ` Nicolas Schier
2025-08-21 5:04 ` David Disseldorp
2025-08-21 5:40 ` Nicolas Schier
2025-08-21 19:09 ` [PATCH v3 0/8] gen_init_cpio: add copy_file_range / reflink support Nathan Chancellor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250819032607.28727-9-ddiss@suse.de \
--to=ddiss@suse.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-next@vger.kernel.org \
--cc=nsc@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).