ext4_getfsmap_dev_compare() returns the difference of two u32 encoded device numbers as an int. new_encode_dev() places bits 8-19 of the minor number in bits 20-31, so any device with a minor number of 0x80000 or higher encodes to a value with bit 31 set, and the difference of such a value and a smaller one does not fit in an int. The comparison then has the wrong sign. With an external journal on a low-numbered device and the filesystem on such a high-minor device, sort() orders the handlers as [data device, log device]. ext4_getfsmap() relies on the handlers being sorted in ascending device order: a query covering only the log device hits "fmh_keys[1].fmr_device < handlers[i].gfd_dev" on the data device first and breaks out of the loop, so no mappings are returned for the journal device, and a full query reports the devices in descending order, which breaks pagination with the last returned record as the next low key. Use cmp_int() so that the comparison cannot overflow. Add a KUnit test with a high-minor data device and an external journal device. Fixes: 0c9ec4beecac ("ext4: support GETFSMAP ioctls") Signed-off-by: Theodore Ts'o --- fs/ext4/fsmap-test.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/fsmap.c | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/fs/ext4/fsmap-test.c b/fs/ext4/fsmap-test.c index 63a1061b413a..ef7681cf9964 100644 --- a/fs/ext4/fsmap-test.c +++ b/fs/ext4/fsmap-test.c @@ -765,6 +765,61 @@ static void test_getfsmap_external_logdev_and_multi_device(struct kunit *test) KUNIT_EXPECT_EQ(test, head.fmh_entries, 0U); } +/* + * A data device whose encoded dev_t has bit 31 set (minor >= 0x80000) must + * still sort after a lower-numbered external journal device, so that devices + * are reported in ascending order and log-device-only queries work. + */ +static void test_getfsmap_high_encoded_datadev_order(struct kunit *test) +{ + struct fsmap_test_ctx *ctx; + struct ext4_fsmap_head head; + struct fsmap_rec_sink *sink; + u32 datadev, logdev = new_encode_dev(FSMAP_TEST_LOGDEV); + int err; + + sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, sink); + ctx = fsmap_create_ctx(test, 12, 0, 1, 128); + fsmap_set_group_tables(ctx, 0, 2, 3, 4); + fsmap_mark_clusters_used(ctx, 0, 0, 12); + + ctx->data_bdev.bdev.bd_dev = MKDEV(259, 0x80000); + datadev = new_encode_dev(ctx->data_bdev.bdev.bd_dev); + KUNIT_ASSERT_GE(test, datadev, 0x80000000U); + KUNIT_ASSERT_LT(test, logdev, datadev); + + ctx->mock_journal.j_blk_offset = 16; + ctx->mock_journal.j_total_len = 64; + ctx->sbi.s_journal = &ctx->mock_journal; + ctx->sbi.s_journal_bdev_file = &ctx->log_bdev_file; + + /* Multi-device query reports logdev (2 recs) before datadev (6) */ + fsmap_init_full_range_head(&head, FSMAP_TEST_MAX_RECS); + err = ext4_getfsmap(ctx->sb, &head, fsmap_collect_cb, sink); + KUNIT_ASSERT_EQ(test, err, 0); + KUNIT_ASSERT_EQ(test, head.fmh_entries, 8U); + fsmap_expect_rec(test, &sink->recs[0], logdev, 0, 16, + EXT4_FMR_OWN_UNKNOWN); + fsmap_expect_rec(test, &sink->recs[1], logdev, 16, 64, + EXT4_FMR_OWN_LOG); + KUNIT_EXPECT_EQ(test, (u32)sink->recs[2].fmr_device, datadev); + KUNIT_EXPECT_EQ(test, (u32)sink->recs[7].fmr_device, datadev); + + /* Targeted query against only logdev */ + memset(sink, 0, sizeof(*sink)); + fsmap_init_full_range_head(&head, FSMAP_TEST_MAX_RECS); + head.fmh_keys[0].fmr_device = logdev; + head.fmh_keys[1].fmr_device = logdev; + err = ext4_getfsmap(ctx->sb, &head, fsmap_collect_cb, sink); + KUNIT_ASSERT_EQ(test, err, 0); + KUNIT_ASSERT_EQ(test, head.fmh_entries, 2U); + fsmap_expect_rec(test, &sink->recs[0], logdev, 0, 16, + EXT4_FMR_OWN_UNKNOWN); + fsmap_expect_rec(test, &sink->recs[1], logdev, 16, 64, + EXT4_FMR_OWN_LOG); +} + static struct kunit_case fsmap_test_cases[] = { KUNIT_CASE(test_fsmap_unit_conversions), KUNIT_CASE(test_getfsmap_invalid_flags_devices_and_keys), @@ -773,6 +828,7 @@ static struct kunit_case fsmap_test_cases[] = { KUNIT_CASE(test_getfsmap_1k_bofs_bigalloc_and_meta_bg), KUNIT_CASE(test_getfsmap_count_only_pagination_and_abort), KUNIT_CASE(test_getfsmap_external_logdev_and_multi_device), + KUNIT_CASE(test_getfsmap_high_encoded_datadev_order), {} }; diff --git a/fs/ext4/fsmap.c b/fs/ext4/fsmap.c index f41c2298d196..dd465b27f879 100644 --- a/fs/ext4/fsmap.c +++ b/fs/ext4/fsmap.c @@ -69,7 +69,7 @@ static int ext4_getfsmap_dev_compare(const void *p1, const void *p2) const struct ext4_getfsmap_dev *d1 = p1; const struct ext4_getfsmap_dev *d2 = p2; - return d1->gfd_dev - d2->gfd_dev; + return cmp_int(d1->gfd_dev, d2->gfd_dev); } /* Compare a record against our starting point */ -- 2.56.0.542.g3ef89b7020-goog