Add an ext4_dirdata_test suite next to ext4_namei_test in fs/ext4/namei-test.c. It uses the same mock superblock, inodes and directory blocks, and covers: - record lengths: ext4_dirent_get_data_len(), ext4_dir_entry_len(), ext4_dirent_data_corrupt() for records that run past rec_len or have length 0, and get_dtype() with the dirdata flags set; - __ext4_check_dir_entry() on entries whose rec_len does not cover their records; - dx_get_dx_info() with dirdata on '.' and '..', malformed '..' records, and dx_root_limit() for the resulting offsets; - adding entries with and without a LUFID, and without the feature; - converting a directory whose '..' carries a LUFID into an htree and splitting leaves whose entries all carry LUFIDs, then finding every entry and its LUFID again and reading the directory back; - rename within and across directories and RENAME_EXCHANGE, which keep each LUFID with its inode; - ext4_dirdata_set_lufid(), behind EXT4_IOC_SET_LUFID; - the casefold+fscrypt hash from a CFHASH record, from the fixed place used before dirdata, and from entries too short to hold it; - the two htree index node limits accepted for casefolded and encrypted directories. dx_get_dx_info() gets a test wrapper, and ext4_dirdata_get(), ext4_dirent_hash_get() and ext4_dirdata_set_lufid() are exported to the test module. Run with: ./tools/testing/kunit/kunit.py run --kunitconfig=fs/ext4 Signed-off-by: Artem Blagodarenko Assisted-by: Claude:claude-opus-5-5 --- fs/ext4/namei-test.c | 754 ++++++++++++++++++++++++++++++++++++++++++- fs/ext4/namei.c | 9 + fs/ext4/namei.h | 1 + 3 files changed, 763 insertions(+), 1 deletion(-) diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c index b45f5ad3b7af..ba1bb52d2442 100644 --- a/fs/ext4/namei-test.c +++ b/fs/ext4/namei-test.c @@ -2315,6 +2315,737 @@ static void test_namei_casefold_lookup_and_strict(struct kunit *test) } #endif /* CONFIG_UNICODE */ +/* + * Dirdata tests. + * + * With the dirdata feature the high four bits of file_type flag records + * stored after the name and its NUL byte: a LUFID (a 128-bit file + * identifier, used by Lustre), a reserved INO64 record and CFHASH, the + * casefold+fscrypt hash. Each record starts with its own length byte. + */ + +#define TEST_LUFID_LEN (sizeof(struct ext4_dirent_data_header) + \ + sizeof(struct ext4_fid)) +#define TEST_CFHASH_LEN sizeof(struct ext4_dirent_hash) + +static void dirdata_enable(struct namei_test_ctx *ctx) +{ + ctx->es.s_feature_incompat |= + cpu_to_le32(EXT4_FEATURE_INCOMPAT_DIRDATA); +} + +static void dirdata_fill_fid(u8 *rec, u8 seed) +{ + int i; + + rec[0] = TEST_LUFID_LEN; + for (i = 0; i < sizeof(struct ext4_fid); i++) + rec[1 + i] = seed + i; +} + +/* + * Write a directory entry at @buf: @name, its NUL byte, then a LUFID + * record made from @fid_seed (if @lufid) and a CFHASH record (if + * @cfhash). Returns the number of bytes used, before rounding. + */ +static unsigned int dirdata_make_de(char *buf, u32 ino, const char *name, + u8 ft, bool lufid, u8 fid_seed, + bool cfhash, u32 hash, u32 minor_hash, + unsigned int rec_len) +{ + struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)buf; + unsigned int len = strlen(name); + unsigned int off = EXT4_BASE_DIR_LEN + len + 1; + + memset(buf, 0, rec_len); + de->inode = cpu_to_le32(ino); + de->rec_len = ext4_rec_len_to_disk(rec_len, TEST_BLK_SIZE); + de->name_len = len; + de->file_type = ft; + memcpy(de->name, name, len); + if (lufid) { + dirdata_fill_fid((u8 *)buf + off, fid_seed); + de->file_type |= EXT4_DIRENT_LUFID; + off += TEST_LUFID_LEN; + } + if (cfhash) { + struct ext4_dirent_hash *dh = (void *)(buf + off); + + dh->dh_header.ddh_length = TEST_CFHASH_LEN; + dh->dh_hash = cpu_to_le32(hash); + dh->dh_minor_hash = cpu_to_le32(minor_hash); + de->file_type |= EXT4_DIRENT_CFHASH; + off += TEST_CFHASH_LEN; + } + return (lufid || cfhash) ? off : EXT4_BASE_DIR_LEN + len; +} + +/* Return true if @de carries the LUFID made from @seed */ +static bool dirdata_has_fid(struct inode *dir, struct ext4_dir_entry_2 *de, + u8 seed) +{ + u8 want[TEST_LUFID_LEN]; + union { + struct ext4_dirent_fid fid; + u8 raw[TEST_LUFID_LEN + EXT4_LUFID_MAX_DATA_LEN]; + } got = {}; + + if (!(ext4_dirdata_get(de, dir, TEST_BLK_SIZE, &got.fid, NULL) & + EXT4_DIRENT_LUFID)) + return false; + dirdata_fill_fid(want, seed); + return !memcmp(got.raw, want, TEST_LUFID_LEN); +} + +static struct ext4_dentry_param *dirdata_make_edp(struct kunit *test, u8 seed) +{ + struct ext4_dentry_param *edp; + + edp = kunit_kzalloc(test, sizeof(*edp) + TEST_LUFID_LEN, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, edp); + edp->edp_magic = EXT4_LUFID_MAGIC; + dirdata_fill_fid(edp->edp_dfid, seed); + return edp; +} + +/* Find @name in @dir; the caller releases the returned buffer */ +static struct buffer_head *dirdata_find(struct kunit *test, struct inode *dir, + const char *name, + struct ext4_dir_entry_2 **de) +{ + struct qstr q = QSTR_INIT(name, strlen(name)); + struct buffer_head *bh; + + *de = NULL; + bh = ext4_find_entry_test(dir, &q, de, NULL); + KUNIT_ASSERT_FALSE_MSG(test, IS_ERR_OR_NULL(bh), "missing %s", name); + KUNIT_ASSERT_NOT_NULL(test, *de); + return bh; +} + +/* + * Rewrite '..' of a freshly initialized directory block so that it carries + * a LUFID, as Lustre does. '..' keeps the rest of the block. + */ +static void dirdata_set_dotdot_fid(struct kunit *test, struct inode *dir, + struct buffer_head *bh, u8 seed) +{ + struct ext4_dir_entry_2 *dot = (struct ext4_dir_entry_2 *)bh->b_data; + struct ext4_dir_entry_2 *dotdot; + unsigned int rlen; + char *rec; + + dotdot = ext4_next_entry(dot, TEST_BLK_SIZE); + KUNIT_ASSERT_EQ(test, dotdot->name_len, 2); + rlen = ext4_rec_len_from_disk(dotdot->rec_len, TEST_BLK_SIZE); + KUNIT_ASSERT_GE(test, rlen, 64U); + rec = (char *)dotdot + EXT4_BASE_DIR_LEN + 2; + rec[0] = 0; + dirdata_fill_fid((u8 *)rec + 1, seed); + dotdot->file_type |= EXT4_DIRENT_LUFID; + KUNIT_ASSERT_EQ(test, ext4_handle_dirty_dirblock(NULL, dir, bh), 0); +} + +/* + * Dirdata 1: record lengths (ext4_dirent_get_data_len, ext4_dir_entry_len), + * records that do not fit (ext4_dirent_data_corrupt) and the file type + * reported to readdir (get_dtype). + */ +static void test_dirdata_record_lengths(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO); + struct ext4_dir_entry_2 *de; + struct buffer_head *bh; + char *buf; + + buf = kunit_kzalloc(test, TEST_BLK_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, buf); + de = (struct ext4_dir_entry_2 *)buf; + dirdata_enable(ctx); + + /* No dirdata: 8 + 3 rounded up */ + dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE, + false, 0, false, 0, 0, 64); + KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 64), 0); + KUNIT_EXPECT_EQ(test, ext4_dir_entry_len(de, TEST_BLK_SIZE, NULL), 12U); + KUNIT_EXPECT_FALSE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 64)); + + /* LUFID: the NUL byte and the 17-byte record */ + dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE, + true, 1, false, 0, 0, 64); + KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 64), + (int)(1 + TEST_LUFID_LEN)); + KUNIT_EXPECT_EQ(test, ext4_dir_entry_len(de, TEST_BLK_SIZE, NULL), + round_up(EXT4_BASE_DIR_LEN + 3 + 1 + TEST_LUFID_LEN, + 4)); + KUNIT_EXPECT_FALSE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 64)); + + /* LUFID and CFHASH: the NUL byte is counted once */ + dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE, + true, 1, true, 0x1234, 0x5678, 64); + KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 64), + (int)(1 + TEST_LUFID_LEN + TEST_CFHASH_LEN)); + KUNIT_EXPECT_EQ(test, ext4_dir_entry_len(de, TEST_BLK_SIZE, NULL), 40U); + + /* The flags do not change the type readdir reports */ + KUNIT_EXPECT_EQ(test, get_dtype(&ctx->sb, de->file_type), + (unsigned char)DT_REG); + + /* A LUFID record that runs past rec_len is corruption */ + dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE, + true, 1, false, 0, 0, 24); + KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 24), 0); + KUNIT_EXPECT_TRUE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 24)); + + /* So is a record of length 0 */ + dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE, + true, 1, false, 0, 0, 64); + buf[EXT4_BASE_DIR_LEN + 3 + 1] = 0; + KUNIT_EXPECT_TRUE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 64)); + + /* Without the feature the flags are not interpreted */ + ctx->es.s_feature_incompat &= + ~cpu_to_le32(EXT4_FEATURE_INCOMPAT_DIRDATA); + KUNIT_EXPECT_FALSE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 64)); + dirdata_enable(ctx); + + /* The checksum tail is never parsed for dirdata */ + bh = map_and_get_dir_bh(ctx, dir, 0); + KUNIT_ASSERT_NOT_NULL(test, bh); + ext4_initialize_dirent_tail(bh, TEST_BLK_SIZE); + de = (struct ext4_dir_entry_2 *)EXT4_DIRENT_TAIL(bh->b_data, + TEST_BLK_SIZE); + KUNIT_EXPECT_TRUE(test, ext4_dir_entry_is_tail(de)); + KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 12), 0); + KUNIT_EXPECT_FALSE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 12)); +} + +/* + * Dirdata 2: __ext4_check_dir_entry() accepts an entry with well-formed + * records and rejects one whose record overruns it or whose rec_len does + * not cover its records. + */ +static void test_dirdata_check_dir_entry(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO); + struct ext4_dir_entry_2 *de; + struct buffer_head *bh; + + bh = map_and_get_dir_bh(ctx, dir, 0); + KUNIT_ASSERT_NOT_NULL(test, bh); + de = (struct ext4_dir_entry_2 *)bh->b_data; + dirdata_enable(ctx); + + dirdata_make_de(bh->b_data, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE, + true, 1, true, 1, 2, TEST_BLK_SIZE); + KUNIT_EXPECT_EQ(test, ext4_check_dir_entry(dir, NULL, de, bh, + bh->b_data, TEST_BLK_SIZE, + 0), 0); + + /* rec_len 28: the LUFID record ends at byte 29 */ + dirdata_make_de(bh->b_data, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE, + true, 1, false, 0, 0, TEST_BLK_SIZE); + de->rec_len = ext4_rec_len_to_disk(28, TEST_BLK_SIZE); + KUNIT_EXPECT_NE(test, ext4_check_dir_entry(dir, NULL, de, bh, + bh->b_data, TEST_BLK_SIZE, + 0), 0); + + /* rec_len 32 holds the LUFID but not a CFHASH after it */ + dirdata_make_de(bh->b_data, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE, + true, 1, true, 1, 2, TEST_BLK_SIZE); + de->rec_len = ext4_rec_len_to_disk(32, TEST_BLK_SIZE); + KUNIT_EXPECT_NE(test, ext4_check_dir_entry(dir, NULL, de, bh, + bh->b_data, TEST_BLK_SIZE, + 0), 0); +} + +/* + * Dirdata 3: dx_get_dx_info() finds dx_root_info after '.' and '..' of any + * length, rejects a root whose '..' records are malformed, and + * dx_root_limit() sizes the dx_entry array from where it starts. + */ +static void test_dirdata_dx_root_info(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO); + struct dx_root_info *info; + struct buffer_head *bh; + unsigned int dotdot_len; + char *b; + + bh = map_and_get_dir_bh(ctx, dir, 0); + KUNIT_ASSERT_NOT_NULL(test, bh); + b = bh->b_data; + dirdata_enable(ctx); + + /* Plain root: dx_root_info at the offset struct dx_root gives it */ + dirdata_make_de(b, dir->i_ino, ".", EXT4_FT_DIR, false, 0, false, + 0, 0, 12); + dirdata_make_de(b + 12, TEST_ROOT_INO, "..", EXT4_FT_DIR, false, 0, + false, 0, 0, TEST_BLK_SIZE - 12); + info = dx_get_dx_info_test(dir, b); + KUNIT_ASSERT_FALSE(test, IS_ERR(info)); + KUNIT_EXPECT_EQ(test, (char *)info - b, + (long)offsetof(struct dx_root, info)); + KUNIT_EXPECT_EQ(test, dx_root_limit(dir, + offsetof(struct dx_root, entries)), + 124U); + + /* '..' with a LUFID moves dx_root_info by the record */ + dotdot_len = round_up(EXT4_BASE_DIR_LEN + 2 + 1 + TEST_LUFID_LEN, 4); + dirdata_make_de(b + 12, TEST_ROOT_INO, "..", EXT4_FT_DIR, true, 7, + false, 0, 0, TEST_BLK_SIZE - 12); + info = dx_get_dx_info_test(dir, b); + KUNIT_ASSERT_FALSE(test, IS_ERR(info)); + KUNIT_EXPECT_EQ(test, (char *)info - b, (long)(12 + dotdot_len)); + KUNIT_EXPECT_EQ(test, dx_root_limit(dir, 12 + dotdot_len + + sizeof(struct dx_root_info)), + (TEST_BLK_SIZE - 12 - dotdot_len - + (unsigned int)sizeof(struct dx_root_info)) / + (unsigned int)sizeof(struct dx_entry)); + + /* '.' with a LUFID too: '..' is found through the rec_len of '.' */ + dirdata_make_de(b, dir->i_ino, ".", EXT4_FT_DIR, true, 3, false, 0, 0, + 28); + dirdata_make_de(b + 28, TEST_ROOT_INO, "..", EXT4_FT_DIR, true, 7, + false, 0, 0, TEST_BLK_SIZE - 28); + info = dx_get_dx_info_test(dir, b); + KUNIT_ASSERT_FALSE(test, IS_ERR(info)); + KUNIT_EXPECT_EQ(test, (char *)info - b, (long)(28 + dotdot_len)); + + /* A zero-length record in '..' is corruption, not "no dirdata" */ + b[28 + EXT4_BASE_DIR_LEN + 2 + 1] = 0; + KUNIT_EXPECT_TRUE(test, IS_ERR(dx_get_dx_info_test(dir, b))); + + /* So is a rec_len of '.' that leaves no room for '..' */ + dirdata_make_de(b, dir->i_ino, ".", EXT4_FT_DIR, false, 0, false, + 0, 0, TEST_BLK_SIZE - 8); + KUNIT_EXPECT_TRUE(test, IS_ERR(dx_get_dx_info_test(dir, b))); +} + +/* + * Dirdata 4: entries added with a LUFID carry it, entries added without + * one do not, and without the feature the LUFID is ignored. + */ +static void test_dirdata_add_entry_lufid(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *root_dir = get_mock_inode(ctx, TEST_ROOT_INO); + struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO); + struct inode *file1 = get_mock_inode(ctx, TEST_FILE1_INO); + struct inode *file2 = get_mock_inode(ctx, TEST_FILE2_INO); + struct dentry *dentries, *parent, *child; + struct ext4_dir_entry_2 *de; + struct buffer_head *bh; + + dentries = kunit_kcalloc(test, 2, sizeof(*dentries), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dentries); + parent = &dentries[0]; + child = &dentries[1]; + ctx->es.s_feature_ro_compat |= + cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM); + dirdata_enable(ctx); + + KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir), 0); + init_mock_dentry(parent, NULL, "dir", dir, &ctx->sb); + + init_mock_dentry(child, parent, "with_fid", file1, &ctx->sb); + child->d_fsdata = dirdata_make_edp(test, 0x40); + KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, child, file1), 0); + + init_mock_dentry(child, parent, "no_fid", file2, &ctx->sb); + KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, child, file2), 0); + + bh = dirdata_find(test, dir, "with_fid", &de); + KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO); + KUNIT_EXPECT_EQ(test, de->file_type & ~EXT4_FT_MASK, + EXT4_DIRENT_LUFID); + KUNIT_EXPECT_EQ(test, de->file_type & EXT4_FT_MASK, EXT4_FT_REG_FILE); + KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir, de, 0x40)); + KUNIT_EXPECT_EQ(test, ext4_dir_entry_len(de, TEST_BLK_SIZE, dir), + round_up(EXT4_BASE_DIR_LEN + 8 + 1 + TEST_LUFID_LEN, + 4)); + brelse(bh); + + bh = dirdata_find(test, dir, "no_fid", &de); + KUNIT_EXPECT_EQ(test, de->file_type, EXT4_FT_REG_FILE); + KUNIT_EXPECT_FALSE(test, dirdata_has_fid(dir, de, 0x40)); + brelse(bh); + + KUNIT_EXPECT_EQ(test, ext4_dirblock_csum_verify(dir, + map_and_get_dir_bh(ctx, dir, 0)), 1); + + /* Without the feature the LUFID passed in is not stored */ + ctx->es.s_feature_incompat &= + ~cpu_to_le32(EXT4_FEATURE_INCOMPAT_DIRDATA); + init_mock_dentry(child, parent, "ignored_fid", file1, &ctx->sb); + child->d_fsdata = dirdata_make_edp(test, 0x50); + KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, child, file1), 0); + bh = dirdata_find(test, dir, "ignored_fid", &de); + KUNIT_EXPECT_EQ(test, de->file_type, EXT4_FT_REG_FILE); + brelse(bh); +} + +/* + * Dirdata 5: a one-block directory whose '..' carries a LUFID becomes an + * htree (make_indexed_dir): '..' keeps its LUFID, dx_root_info follows it, + * the root limit accounts for it, and every entry, each with its own + * LUFID, survives the leaf splits and is found again. + */ +static void test_dirdata_htree_with_lufids(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *root_dir = get_mock_inode(ctx, TEST_ROOT_INO); + struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO); + struct inode *file1 = get_mock_inode(ctx, TEST_FILE1_INO); + struct dentry *dentries, *parent, *child; + struct ext4_dir_entry_2 *de, *dotdot; + struct dir_private_info *dir_info; + struct dx_root_info *info; + struct dx_entry *entries; + struct buffer_head *bh; + struct file *dir_file; + unsigned int dotdot_len, info_off; + __u32 curr_hash = 0, next_hash = 0; + int i, total = 0; + + dentries = kunit_kcalloc(test, 2, sizeof(*dentries), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dentries); + parent = &dentries[0]; + child = &dentries[1]; + dir_file = kunit_kzalloc(test, sizeof(*dir_file), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dir_file); + ctx->es.s_feature_ro_compat |= + cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM); + dirdata_enable(ctx); + + KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir), 0); + bh = map_and_get_dir_bh(ctx, dir, 0); + KUNIT_ASSERT_NOT_NULL(test, bh); + dirdata_set_dotdot_fid(test, dir, bh, 0x20); + init_mock_dentry(parent, NULL, "dir", dir, &ctx->sb); + + /* 40 bytes per entry: ~24 fit in a block, so the leaves split */ + for (i = 0; i < 60; i++) { + char name[16]; + + snprintf(name, sizeof(name), "file_%05d", i); + init_mock_dentry(child, parent, name, file1, &ctx->sb); + child->d_fsdata = dirdata_make_edp(test, i); + KUNIT_ASSERT_EQ_MSG(test, + ext4_add_entry_test(NULL, child, file1), 0, + "failed at entry %d", i); + } + KUNIT_EXPECT_TRUE(test, ext4_test_inode_flag(dir, EXT4_INODE_INDEX)); + KUNIT_EXPECT_GE(test, dir->i_size, (loff_t)(4 * TEST_BLK_SIZE)); + + /* '..' kept its LUFID and now ends where dx_root_info starts */ + dotdot = ext4_next_entry((struct ext4_dir_entry_2 *)bh->b_data, + TEST_BLK_SIZE); + KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir, dotdot, 0x20)); + dotdot_len = round_up(EXT4_BASE_DIR_LEN + 2 + 1 + TEST_LUFID_LEN, 4); + KUNIT_EXPECT_EQ(test, ext4_rec_len_from_disk(dotdot->rec_len, + TEST_BLK_SIZE), + TEST_BLK_SIZE - 12); + info = dx_get_dx_info_test(dir, bh->b_data); + KUNIT_ASSERT_FALSE(test, IS_ERR(info)); + info_off = (char *)info - bh->b_data; + KUNIT_EXPECT_EQ(test, info_off, 12 + dotdot_len); + KUNIT_EXPECT_EQ(test, info->info_length, + (u8)sizeof(struct dx_root_info)); + entries = (struct dx_entry *)((char *)info + info->info_length); + KUNIT_EXPECT_EQ(test, dx_get_limit(entries), + dx_root_limit(dir, info_off + info->info_length)); + KUNIT_EXPECT_EQ(test, dx_get_limit(entries), + (TEST_BLK_SIZE - info_off - info->info_length - + (unsigned int)sizeof(struct dx_tail)) / + (unsigned int)sizeof(struct dx_entry)); + KUNIT_EXPECT_GT(test, dx_get_count(entries), 2U); + + /* Every entry is found through the index, with its own LUFID */ + for (i = 0; i < 60; i++) { + char name[16]; + struct buffer_head *ebh; + + snprintf(name, sizeof(name), "file_%05d", i); + ebh = dirdata_find(test, dir, name, &de); + KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO); + KUNIT_EXPECT_TRUE_MSG(test, dirdata_has_fid(dir, de, i), + "wrong LUFID on %s", name); + brelse(ebh); + } + + /* readdir sees 60 files, '.' and '..' */ + dir_file->f_inode = dir; + while (1) { + int cnt; + + dir_info = kzalloc_obj(*dir_info); + KUNIT_ASSERT_NOT_NULL(test, dir_info); + dir_file->private_data = dir_info; + cnt = ext4_htree_fill_tree(dir_file, curr_hash, 0, &next_hash); + KUNIT_ASSERT_GE(test, cnt, 0); + total += cnt; + ext4_htree_free_dir_info(dir_info); + if (next_hash == ~0U) + break; + curr_hash = next_hash; + } + KUNIT_EXPECT_EQ(test, total, 62); +} + +/* The rename tests look the target up through ext4_iget() */ +static struct inode *dirdata_iget_stub(struct super_block *sb, + unsigned long ino, + ext4_iget_flags flags, + const char *function, + unsigned int line) +{ + struct kunit *test = kunit_get_current_test(); + struct namei_test_ctx *ctx = test->priv; + struct mock_inode_slot *slot = find_inode_slot_by_ino(ctx, ino); + + ctx->last_iget_ino = ino; + if (!slot) + return ERR_PTR(-ESTALE); + ihold(&slot->ei.vfs_inode); + return &slot->ei.vfs_inode; +} + +/* + * Dirdata 6: rename keeps the LUFID of the entry it moves, within a + * directory and across directories, and RENAME_EXCHANGE swaps the LUFIDs + * together with the inodes. + */ +static void test_dirdata_rename_keeps_lufid(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *root_dir = get_mock_inode(ctx, TEST_ROOT_INO); + struct inode *dir1 = get_mock_inode(ctx, TEST_DIR_INO); + struct inode *dir2 = get_mock_inode(ctx, TEST_DIR2_INO); + struct inode *file1 = get_mock_inode(ctx, TEST_FILE1_INO); + struct inode *file2 = get_mock_inode(ctx, TEST_FILE2_INO); + struct dentry *dentries, *d1, *d2, *old_dentry, *new_dentry; + struct ext4_dir_entry_2 *de; + struct buffer_head *bh; + + dentries = kunit_kcalloc(test, 4, sizeof(*dentries), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dentries); + d1 = &dentries[0]; + d2 = &dentries[1]; + old_dentry = &dentries[2]; + new_dentry = &dentries[3]; + ctx->es.s_feature_ro_compat |= + cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM); + dirdata_enable(ctx); + + KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir1), 0); + KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir2), 0); + init_mock_dentry(d1, NULL, "dir1", dir1, &ctx->sb); + init_mock_dentry(d2, NULL, "dir2", dir2, &ctx->sb); + + init_mock_dentry(old_dentry, d1, "f1", file1, &ctx->sb); + old_dentry->d_fsdata = dirdata_make_edp(test, 0x11); + KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, old_dentry, file1), 0); + old_dentry->d_fsdata = NULL; + + /* Within the directory */ + init_mock_dentry(new_dentry, d1, "f1_renamed", NULL, &ctx->sb); + KUNIT_ASSERT_EQ(test, + ext4_dir_inode_operations.rename(&nop_mnt_idmap, dir1, + old_dentry, dir1, new_dentry, 0), 0); + bh = dirdata_find(test, dir1, "f1_renamed", &de); + KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir1, de, 0x11)); + brelse(bh); + + /* Across directories */ + init_mock_dentry(old_dentry, d1, "f1_renamed", file1, &ctx->sb); + init_mock_dentry(new_dentry, d2, "f1_moved", NULL, &ctx->sb); + KUNIT_ASSERT_EQ(test, + ext4_dir_inode_operations.rename(&nop_mnt_idmap, dir1, + old_dentry, dir2, new_dentry, 0), 0); + bh = dirdata_find(test, dir2, "f1_moved", &de); + KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO); + KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir2, de, 0x11)); + brelse(bh); + + /* RENAME_EXCHANGE: each LUFID follows its inode */ + init_mock_dentry(old_dentry, d1, "f2", file2, &ctx->sb); + old_dentry->d_fsdata = dirdata_make_edp(test, 0x22); + KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, old_dentry, file2), 0); + old_dentry->d_fsdata = NULL; + init_mock_dentry(new_dentry, d2, "f1_moved", file1, &ctx->sb); + KUNIT_ASSERT_EQ(test, + ext4_dir_inode_operations.rename(&nop_mnt_idmap, dir1, + old_dentry, dir2, new_dentry, RENAME_EXCHANGE), 0); + bh = dirdata_find(test, dir1, "f2", &de); + KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO); + KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir1, de, 0x11)); + brelse(bh); + bh = dirdata_find(test, dir2, "f1_moved", &de); + KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE2_INO); + KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir2, de, 0x22)); + brelse(bh); +} + +/* + * Dirdata 7: ext4_dirdata_set_lufid(), behind EXT4_IOC_SET_LUFID, adds a + * LUFID to an existing entry and replaces it, and refuses a missing name + * and a filesystem without the feature. + */ +static void test_dirdata_set_lufid(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *root_dir = get_mock_inode(ctx, TEST_ROOT_INO); + struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO); + struct inode *file1 = get_mock_inode(ctx, TEST_FILE1_INO); + struct dentry *dentries, *parent, *child; + struct ext4_dir_entry_2 *de; + struct buffer_head *bh; + + kunit_activate_static_stub(test, __ext4_iget, dirdata_iget_stub); + dentries = kunit_kcalloc(test, 2, sizeof(*dentries), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dentries); + parent = &dentries[0]; + child = &dentries[1]; + ctx->es.s_feature_ro_compat |= + cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM); + dirdata_enable(ctx); + + KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir), 0); + init_mock_dentry(parent, NULL, "dir", dir, &ctx->sb); + init_mock_dentry(child, parent, "target", file1, &ctx->sb); + KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, child, file1), 0); + + KUNIT_EXPECT_EQ(test, + ext4_dirdata_set_lufid(&nop_mnt_idmap, dir, "target", 6, + dirdata_make_edp(test, 0x30)), 0); + bh = dirdata_find(test, dir, "target", &de); + KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO); + KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir, de, 0x30)); + brelse(bh); + + KUNIT_EXPECT_EQ(test, + ext4_dirdata_set_lufid(&nop_mnt_idmap, dir, "target", 6, + dirdata_make_edp(test, 0x60)), 0); + bh = dirdata_find(test, dir, "target", &de); + KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir, de, 0x60)); + brelse(bh); + KUNIT_EXPECT_EQ(test, ext4_dirblock_csum_verify(dir, + map_and_get_dir_bh(ctx, dir, 0)), 1); + + KUNIT_EXPECT_EQ(test, + ext4_dirdata_set_lufid(&nop_mnt_idmap, dir, "missing", 7, + dirdata_make_edp(test, 0x30)), -ENOENT); + + ctx->es.s_feature_incompat &= + ~cpu_to_le32(EXT4_FEATURE_INCOMPAT_DIRDATA); + KUNIT_EXPECT_EQ(test, + ext4_dirdata_set_lufid(&nop_mnt_idmap, dir, "target", 6, + dirdata_make_edp(test, 0x30)), + -EOPNOTSUPP); +} + +/* + * Dirdata 8: in a casefolded and encrypted directory the hash is read from + * the CFHASH record, wherever it is, or from its fixed place after the name + * of an entry written before dirdata; an entry too short for the hash, such + * as '.', or with other records at that place, yields none. + */ +static void test_dirdata_hash_get(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO); + struct dx_hash_info hinfo; + struct ext4_dir_entry_2 *de; + char *buf; + + buf = kunit_kzalloc(test, TEST_BLK_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, buf); + de = (struct ext4_dir_entry_2 *)buf; + dir->i_flags |= S_CASEFOLD | S_ENCRYPTED; + KUNIT_ASSERT_TRUE(test, ext4_hash_in_dirent(dir)); + dirdata_enable(ctx); + + /* CFHASH record */ + dirdata_make_de(buf, TEST_FILE1_INO, "abcd", EXT4_FT_REG_FILE, + false, 0, true, 0xaaaa0000, 0x0000bbbb, 64); + memset(&hinfo, 0, sizeof(hinfo)); + KUNIT_EXPECT_TRUE(test, + ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo)); + KUNIT_EXPECT_EQ(test, hinfo.hash, 0xaaaa0000U); + KUNIT_EXPECT_EQ(test, hinfo.minor_hash, 0x0000bbbbU); + + /* CFHASH after a LUFID */ + dirdata_make_de(buf, TEST_FILE1_INO, "abcd", EXT4_FT_REG_FILE, + true, 9, true, 0xcccc0000, 0x0000dddd, 64); + memset(&hinfo, 0, sizeof(hinfo)); + KUNIT_EXPECT_TRUE(test, + ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo)); + KUNIT_EXPECT_EQ(test, hinfo.hash, 0xcccc0000U); + KUNIT_EXPECT_EQ(test, hinfo.minor_hash, 0x0000ddddU); + + /* A LUFID without CFHASH: the LUFID sits where the hash would be */ + dirdata_make_de(buf, TEST_FILE1_INO, "abcd", EXT4_FT_REG_FILE, + true, 9, false, 0, 0, 64); + KUNIT_EXPECT_FALSE(test, + ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo)); + + /* An entry written before dirdata: hash at its fixed offset */ + dirdata_make_de(buf, TEST_FILE1_INO, "abcd", EXT4_FT_REG_FILE, + false, 0, false, 0, 0, 20); + EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(0x12340000); + EXT4_DIRENT_HASHES(de)->minor_hash = cpu_to_le32(0x00005678); + memset(&hinfo, 0, sizeof(hinfo)); + KUNIT_EXPECT_TRUE(test, + ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo)); + KUNIT_EXPECT_EQ(test, hinfo.hash, 0x12340000U); + KUNIT_EXPECT_EQ(test, hinfo.minor_hash, 0x00005678U); + + /* '.' is 12 bytes long and has no room for a hash */ + dirdata_make_de(buf, dir->i_ino, ".", EXT4_FT_DIR, false, 0, false, + 0, 0, 12); + KUNIT_EXPECT_FALSE(test, + ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo)); + + dir->i_flags &= ~(S_CASEFOLD | S_ENCRYPTED); +} + +/* + * Dirdata 9: an htree index node of a casefolded and encrypted directory + * may have the limit that the kernel computes or the one that e2fsck + * computes, which reserves room for the hash in the fake dirent. + */ +static void test_dirdata_dx_node_limit(struct kunit *test) +{ + struct namei_test_ctx *ctx = test->priv; + struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO); + unsigned int limit = dx_node_limit(dir); + + /* Plain directory: one valid limit */ + KUNIT_EXPECT_EQ(test, limit, 127U); + KUNIT_EXPECT_TRUE(test, dx_node_limit_ok(dir, limit)); + KUNIT_EXPECT_FALSE(test, dx_node_limit_ok(dir, limit - 1)); + + dir->i_flags |= S_CASEFOLD | S_ENCRYPTED; + KUNIT_EXPECT_EQ(test, dx_node_limit(dir), limit); + KUNIT_EXPECT_TRUE(test, dx_node_limit_ok(dir, limit)); + KUNIT_EXPECT_TRUE(test, dx_node_limit_ok(dir, limit - 1)); + KUNIT_EXPECT_FALSE(test, dx_node_limit_ok(dir, limit - 2)); + KUNIT_EXPECT_FALSE(test, dx_node_limit_ok(dir, limit + 1)); + + /* With metadata_csum the dx_tail comes off both */ + ctx->es.s_feature_ro_compat |= + cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM); + limit = dx_node_limit(dir); + KUNIT_EXPECT_EQ(test, limit, 126U); + KUNIT_EXPECT_TRUE(test, dx_node_limit_ok(dir, limit - 1)); + KUNIT_EXPECT_FALSE(test, dx_node_limit_ok(dir, limit - 2)); + dir->i_flags &= ~(S_CASEFOLD | S_ENCRYPTED); +} + static struct kunit_case namei_test_cases[] = { KUNIT_CASE(test_namei_dirent_tail_and_csum), KUNIT_CASE(test_namei_dx_helpers_and_csum), @@ -2344,6 +3075,27 @@ static struct kunit_suite namei_test_suite = { .test_cases = namei_test_cases, }; -kunit_test_suites(&namei_test_suite); +static struct kunit_case dirdata_test_cases[] = { + KUNIT_CASE(test_dirdata_record_lengths), + KUNIT_CASE(test_dirdata_check_dir_entry), + KUNIT_CASE(test_dirdata_dx_root_info), + KUNIT_CASE(test_dirdata_add_entry_lufid), + KUNIT_CASE(test_dirdata_htree_with_lufids), + KUNIT_CASE(test_dirdata_rename_keeps_lufid), + KUNIT_CASE(test_dirdata_set_lufid), + KUNIT_CASE(test_dirdata_hash_get), + KUNIT_CASE(test_dirdata_dx_node_limit), + {} +}; + +/* Same fixture as ext4_namei_test */ +static struct kunit_suite dirdata_test_suite = { + .name = "ext4_dirdata_test", + .init = namei_test_init, + .exit = namei_test_exit, + .test_cases = dirdata_test_cases, +}; + +kunit_test_suites(&namei_test_suite, &dirdata_test_suite); MODULE_LICENSE("GPL"); diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index f9250407cedb..b8a5bd803603 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1323,6 +1323,7 @@ unsigned char ext4_dirdata_get(struct ext4_dir_entry_2 *de, struct inode *dir, return ret; } +EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirdata_get); /* * ext4_dirent_hash_get() - read the hash stored in a casefold+fscrypt entry @@ -1355,6 +1356,7 @@ bool ext4_dirent_hash_get(struct ext4_dir_entry_2 *de, struct inode *dir, hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de); return true; } +EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirent_hash_get); /* * ext4_dirdata_set() - Write dirdata fields into a directory entry. @@ -4991,6 +4993,7 @@ int ext4_dirdata_set_lufid(const struct mnt_idmap *idmap, struct inode *dir, return err; } +EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirdata_set_lufid); /* * directories can handle most operations... @@ -5132,6 +5135,12 @@ bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root) } EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_check_dx_root_test); +struct dx_root_info *dx_get_dx_info_test(struct inode *dir, void *de_buf) +{ + return dx_get_dx_info(dir, de_buf); +} +EXPORT_SYMBOL_FOR_EXT4_TEST(dx_get_dx_info_test); + struct buffer_head *ext4_find_entry_test(struct inode *dir, const struct qstr *d_name, struct ext4_dir_entry_2 **res_dir, diff --git a/fs/ext4/namei.h b/fs/ext4/namei.h index 946a5f705f4d..3a7560a27720 100644 --- a/fs/ext4/namei.h +++ b/fs/ext4/namei.h @@ -227,6 +227,7 @@ struct ext4_dir_entry_2 *do_split_test(handle_t *handle, struct inode *dir, struct dx_frame *frame, struct dx_hash_info *hinfo); bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root); +struct dx_root_info *dx_get_dx_info_test(struct inode *dir, void *de_buf); struct buffer_head *ext4_find_entry_test(struct inode *dir, const struct qstr *d_name, struct ext4_dir_entry_2 **res_dir, -- 2.52.0