1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Written by Ojaswin Mujoo <ojaswin@linux.ibm.com> (IBM) 4 * 5 * These Kunit tests are designed to test the functionality of 6 * extent split and conversion in ext4. 7 * 8 * Currently, ext4 can split extents in 2 ways: 9 * 1. By splitting the extents in the extent tree and optionally converting them 10 * to written or unwritten based on flags passed. 11 * 2. In case 1 encounters an error, ext4 instead zerooes out the unwritten 12 * areas of the extent and marks the complete extent written. 13 * 14 * The primary function that handles this is ext4_split_convert_extents(). 15 * 16 * We test both of the methods of split. The behavior we try to enforce is: 17 * 1. When passing EXT4_GET_BLOCKS_CONVERT flag to ext4_split_convert_extents(), 18 * the split extent should be converted to initialized. 19 * 2. When passing EXT4_GET_BLOCKS_CONVERT_UNWRITTEN flag to 20 * ext4_split_convert_extents(), the split extent should be converted to 21 * uninitialized. 22 * 3. In case we use the zeroout method, then we should correctly write zeroes 23 * to the unwritten areas of the extent and we should not corrupt/leak any 24 * data. 25 * 26 * Enforcing 1 and 2 is straight forward, we just setup a minimal inode with 27 * extent tree, call ext4_split_convert_extents() and check the final state of 28 * the extent tree. 29 * 30 * For zeroout testing, we maintain a separate buffer which represents the disk 31 * data corresponding to the extents. We then override ext4's zeroout functions 32 * to instead write zeroes to our buffer. Then, we override 33 * ext4_ext_insert_extent() to return -ENOSPC, which triggers the zeroout. 34 * Finally, we check the state of the extent tree and zeroout buffer to confirm 35 * everything went well. 36 */ 37 38 #include <kunit/test.h> 39 #include <kunit/static_stub.h> 40 #include <linux/gfp_types.h> 41 #include <linux/stddef.h> 42 43 #include "ext4.h" 44 #include "ext4_extents.h" 45 46 #define EXT_DATA_PBLK 100 47 #define EXT_DATA_LBLK 10 48 #define EXT_DATA_LEN 3 49 50 struct kunit_ctx { 51 /* 52 * Ext4 inode which has only 1 unwrit extent 53 */ 54 struct ext4_inode_info *k_ei; 55 /* 56 * Represents the underlying data area (used for zeroout testing) 57 */ 58 char *k_data; 59 } k_ctx; 60 61 /* 62 * describes the state of an expected extent in extent tree. 63 */ 64 struct kunit_ext_state { 65 ext4_lblk_t ex_lblk; 66 ext4_lblk_t ex_len; 67 bool is_unwrit; 68 }; 69 70 /* 71 * describes the state of the data area of a writ extent. Used for testing 72 * correctness of zeroout. 73 */ 74 struct kunit_ext_data_state { 75 char exp_char; 76 ext4_lblk_t off_blk; 77 ext4_lblk_t len_blk; 78 }; 79 80 struct kunit_ext_test_param { 81 /* description of test */ 82 char *desc; 83 84 /* is extent unwrit at beginning of test */ 85 bool is_unwrit_at_start; 86 87 /* flags to pass while splitting */ 88 int split_flags; 89 90 /* map describing range to split */ 91 struct ext4_map_blocks split_map; 92 93 /* no of extents expected after split */ 94 int nr_exp_ext; 95 96 /* 97 * expected state of extents after split. We will never split into more 98 * than 3 extents 99 */ 100 struct kunit_ext_state exp_ext_state[3]; 101 102 /* Below fields used for zeroout tests */ 103 104 bool is_zeroout_test; 105 /* 106 * no of expected data segments (zeroout tests). Example, if we expect 107 * data to be 4kb 0s, followed by 8kb non-zero, then nr_exp_data_segs==2 108 */ 109 int nr_exp_data_segs; 110 111 /* 112 * expected state of data area after zeroout. 113 */ 114 struct kunit_ext_data_state exp_data_state[3]; 115 }; 116 117 static void ext_kill_sb(struct super_block *sb) 118 { 119 generic_shutdown_super(sb); 120 } 121 122 static int ext_set(struct super_block *sb, void *data) 123 { 124 return 0; 125 } 126 127 static struct file_system_type ext_fs_type = { 128 .name = "extents test", 129 .kill_sb = ext_kill_sb, 130 }; 131 132 static void extents_kunit_exit(struct kunit *test) 133 { 134 kfree(k_ctx.k_ei); 135 kfree(k_ctx.k_data); 136 } 137 138 static void ext4_cache_extents_stub(struct inode *inode, 139 struct ext4_extent_header *eh) 140 { 141 return; 142 } 143 144 static int __ext4_ext_dirty_stub(const char *where, unsigned int line, 145 handle_t *handle, struct inode *inode, 146 struct ext4_ext_path *path) 147 { 148 return 0; 149 } 150 151 static struct ext4_ext_path * 152 ext4_ext_insert_extent_stub(handle_t *handle, struct inode *inode, 153 struct ext4_ext_path *path, 154 struct ext4_extent *newext, int gb_flags) 155 { 156 return ERR_PTR(-ENOSPC); 157 } 158 159 static void ext4_es_remove_extent_stub(struct inode *inode, ext4_lblk_t lblk, 160 ext4_lblk_t len) 161 { 162 return; 163 } 164 165 static void ext4_zeroout_es_stub(struct inode *inode, struct ext4_extent *ex) 166 { 167 return; 168 } 169 170 /* 171 * We will zeroout the equivalent range in the data area 172 */ 173 static int ext4_ext_zeroout_stub(struct inode *inode, struct ext4_extent *ex) 174 { 175 ext4_lblk_t ee_block, off_blk; 176 loff_t ee_len; 177 loff_t off_bytes; 178 struct kunit *test = kunit_get_current_test(); 179 180 ee_block = le32_to_cpu(ex->ee_block); 181 ee_len = ext4_ext_get_actual_len(ex); 182 183 KUNIT_EXPECT_EQ_MSG(test, 1, ee_block >= EXT_DATA_LBLK, "ee_block=%d", 184 ee_block); 185 KUNIT_EXPECT_EQ(test, 1, 186 ee_block + ee_len <= EXT_DATA_LBLK + EXT_DATA_LEN); 187 188 off_blk = ee_block - EXT_DATA_LBLK; 189 off_bytes = off_blk << inode->i_sb->s_blocksize_bits; 190 memset(k_ctx.k_data + off_bytes, 0, 191 ee_len << inode->i_sb->s_blocksize_bits); 192 193 return 0; 194 } 195 196 static int ext4_issue_zeroout_stub(struct inode *inode, ext4_lblk_t lblk, 197 ext4_fsblk_t pblk, ext4_lblk_t len) 198 { 199 ext4_lblk_t off_blk; 200 loff_t off_bytes; 201 struct kunit *test = kunit_get_current_test(); 202 203 kunit_log(KERN_ALERT, test, 204 "%s: lblk=%u pblk=%llu len=%u", __func__, lblk, pblk, len); 205 KUNIT_EXPECT_EQ(test, 1, lblk >= EXT_DATA_LBLK); 206 KUNIT_EXPECT_EQ(test, 1, lblk + len <= EXT_DATA_LBLK + EXT_DATA_LEN); 207 KUNIT_EXPECT_EQ(test, 1, lblk - EXT_DATA_LBLK == pblk - EXT_DATA_PBLK); 208 209 off_blk = lblk - EXT_DATA_LBLK; 210 off_bytes = off_blk << inode->i_sb->s_blocksize_bits; 211 memset(k_ctx.k_data + off_bytes, 0, 212 len << inode->i_sb->s_blocksize_bits); 213 214 return 0; 215 } 216 217 static int extents_kunit_init(struct kunit *test) 218 { 219 struct ext4_extent_header *eh = NULL; 220 struct ext4_inode_info *ei; 221 struct inode *inode; 222 struct super_block *sb; 223 struct kunit_ext_test_param *param = 224 (struct kunit_ext_test_param *)(test->param_value); 225 226 /* setup the mock inode */ 227 k_ctx.k_ei = kzalloc(sizeof(struct ext4_inode_info), GFP_KERNEL); 228 if (k_ctx.k_ei == NULL) 229 return -ENOMEM; 230 ei = k_ctx.k_ei; 231 inode = &ei->vfs_inode; 232 233 sb = sget(&ext_fs_type, NULL, ext_set, 0, NULL); 234 if (IS_ERR(sb)) 235 return PTR_ERR(sb); 236 237 sb->s_blocksize = 4096; 238 sb->s_blocksize_bits = 12; 239 240 ei->i_disksize = (EXT_DATA_LBLK + EXT_DATA_LEN + 10) << sb->s_blocksize_bits; 241 inode->i_sb = sb; 242 243 k_ctx.k_data = kzalloc(EXT_DATA_LEN * 4096, GFP_KERNEL); 244 if (k_ctx.k_data == NULL) 245 return -ENOMEM; 246 247 /* 248 * set the data area to a junk value 249 */ 250 memset(k_ctx.k_data, 'X', EXT_DATA_LEN * 4096); 251 252 /* create a tree with depth 0 */ 253 eh = (struct ext4_extent_header *)k_ctx.k_ei->i_data; 254 255 /* Fill extent header */ 256 eh = ext_inode_hdr(&k_ctx.k_ei->vfs_inode); 257 eh->eh_depth = 0; 258 eh->eh_entries = cpu_to_le16(1); 259 eh->eh_magic = EXT4_EXT_MAGIC; 260 eh->eh_max = 261 cpu_to_le16(ext4_ext_space_root_idx(&k_ctx.k_ei->vfs_inode, 0)); 262 eh->eh_generation = 0; 263 264 /* 265 * add 1 extent in leaf node covering lblks [10,13) and pblk [100,103) 266 */ 267 EXT_FIRST_EXTENT(eh)->ee_block = cpu_to_le32(EXT_DATA_LBLK); 268 EXT_FIRST_EXTENT(eh)->ee_len = cpu_to_le16(EXT_DATA_LEN); 269 ext4_ext_store_pblock(EXT_FIRST_EXTENT(eh), EXT_DATA_PBLK); 270 if (!param || param->is_unwrit_at_start) 271 ext4_ext_mark_unwritten(EXT_FIRST_EXTENT(eh)); 272 273 /* Add stubs */ 274 kunit_activate_static_stub(test, ext4_cache_extents, 275 ext4_cache_extents_stub); 276 kunit_activate_static_stub(test, __ext4_ext_dirty, 277 __ext4_ext_dirty_stub); 278 kunit_activate_static_stub(test, ext4_es_remove_extent, 279 ext4_es_remove_extent_stub); 280 kunit_activate_static_stub(test, ext4_zeroout_es, ext4_zeroout_es_stub); 281 kunit_activate_static_stub(test, ext4_ext_zeroout, ext4_ext_zeroout_stub); 282 kunit_activate_static_stub(test, ext4_issue_zeroout, 283 ext4_issue_zeroout_stub); 284 return 0; 285 } 286 287 /* 288 * Return 1 if all bytes in the buf equal to c, else return the offset of first mismatch 289 */ 290 static int check_buffer(char *buf, int c, int size) 291 { 292 void *ret = NULL; 293 294 ret = memchr_inv(buf, c, size); 295 if (ret == NULL) 296 return 0; 297 298 kunit_log(KERN_ALERT, kunit_get_current_test(), 299 "# %s: wrong char found at offset %u (expected:%d got:%d)", __func__, 300 (u32)((char *)ret - buf), c, *((char *)ret)); 301 return 1; 302 } 303 304 static void test_split_convert(struct kunit *test) 305 { 306 struct ext4_ext_path *path; 307 struct inode *inode = &k_ctx.k_ei->vfs_inode; 308 struct ext4_extent *ex; 309 struct ext4_map_blocks map; 310 const struct kunit_ext_test_param *param = 311 (const struct kunit_ext_test_param *)(test->param_value); 312 int blkbits = inode->i_sb->s_blocksize_bits; 313 314 if (param->is_zeroout_test) 315 /* 316 * Force zeroout by making ext4_ext_insert_extent return ENOSPC 317 */ 318 kunit_activate_static_stub(test, ext4_ext_insert_extent, 319 ext4_ext_insert_extent_stub); 320 321 path = ext4_find_extent(inode, EXT_DATA_LBLK, NULL, 0); 322 ex = path->p_ext; 323 KUNIT_EXPECT_EQ(test, EXT_DATA_LBLK, le32_to_cpu(ex->ee_block)); 324 KUNIT_EXPECT_EQ(test, EXT_DATA_LEN, ext4_ext_get_actual_len(ex)); 325 KUNIT_EXPECT_EQ(test, param->is_unwrit_at_start, 326 ext4_ext_is_unwritten(ex)); 327 if (param->is_zeroout_test) 328 KUNIT_EXPECT_EQ(test, 0, 329 check_buffer(k_ctx.k_data, 'X', 330 EXT_DATA_LEN << blkbits)); 331 332 map.m_lblk = param->split_map.m_lblk; 333 map.m_len = param->split_map.m_len; 334 ext4_split_convert_extents(NULL, inode, &map, path, 335 param->split_flags, NULL); 336 337 path = ext4_find_extent(inode, EXT_DATA_LBLK, NULL, 0); 338 ex = path->p_ext; 339 340 for (int i = 0; i < param->nr_exp_ext; i++) { 341 struct kunit_ext_state exp_ext = param->exp_ext_state[i]; 342 343 KUNIT_EXPECT_EQ(test, exp_ext.ex_lblk, 344 le32_to_cpu(ex->ee_block)); 345 KUNIT_EXPECT_EQ(test, exp_ext.ex_len, 346 ext4_ext_get_actual_len(ex)); 347 KUNIT_EXPECT_EQ(test, exp_ext.is_unwrit, 348 ext4_ext_is_unwritten(ex)); 349 350 /* Only printed on failure */ 351 kunit_log(KERN_INFO, test, 352 "# [extent %d] exp: lblk:%d len:%d unwrit:%d \n", i, 353 exp_ext.ex_lblk, exp_ext.ex_len, exp_ext.is_unwrit); 354 kunit_log(KERN_INFO, test, 355 "# [extent %d] got: lblk:%d len:%d unwrit:%d\n", i, 356 le32_to_cpu(ex->ee_block), 357 ext4_ext_get_actual_len(ex), 358 ext4_ext_is_unwritten(ex)); 359 kunit_log(KERN_INFO, test, "------------------\n"); 360 361 ex = ex + 1; 362 } 363 364 if (!param->is_zeroout_test) 365 return; 366 367 /* 368 * Check that then data area has been zeroed out correctly 369 */ 370 for (int i = 0; i < param->nr_exp_data_segs; i++) { 371 loff_t off, len; 372 struct kunit_ext_data_state exp_data_seg = param->exp_data_state[i]; 373 374 off = exp_data_seg.off_blk << blkbits; 375 len = exp_data_seg.len_blk << blkbits; 376 KUNIT_EXPECT_EQ_MSG(test, 0, 377 check_buffer(k_ctx.k_data + off, 378 exp_data_seg.exp_char, len), 379 "# corruption in byte range [%lld, %lld)", 380 off, len); 381 } 382 383 return; 384 } 385 386 static const struct kunit_ext_test_param test_split_convert_params[] = { 387 /* unwrit to writ splits */ 388 { .desc = "split unwrit extent to 2 extents and convert 1st half writ", 389 .is_unwrit_at_start = 1, 390 .split_flags = EXT4_GET_BLOCKS_CONVERT, 391 .split_map = { .m_lblk = EXT_DATA_LBLK, .m_len = 1 }, 392 .nr_exp_ext = 2, 393 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 394 .ex_len = 1, 395 .is_unwrit = 0 }, 396 { .ex_lblk = EXT_DATA_LBLK + 1, 397 .ex_len = EXT_DATA_LEN - 1, 398 .is_unwrit = 1 } }, 399 .is_zeroout_test = 0 }, 400 { .desc = "split unwrit extent to 2 extents and convert 2nd half writ", 401 .is_unwrit_at_start = 1, 402 .split_flags = EXT4_GET_BLOCKS_CONVERT, 403 .split_map = { .m_lblk = EXT_DATA_LBLK + 1, .m_len = EXT_DATA_LEN - 1 }, 404 .nr_exp_ext = 2, 405 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 406 .ex_len = 1, 407 .is_unwrit = 1 }, 408 { .ex_lblk = EXT_DATA_LBLK + 1, 409 .ex_len = EXT_DATA_LEN - 1, 410 .is_unwrit = 0 } }, 411 .is_zeroout_test = 0 }, 412 { .desc = "split unwrit extent to 3 extents and convert 2nd half to writ", 413 .is_unwrit_at_start = 1, 414 .split_flags = EXT4_GET_BLOCKS_CONVERT, 415 .split_map = { .m_lblk = EXT_DATA_LBLK + 1, .m_len = EXT_DATA_LEN - 2 }, 416 .nr_exp_ext = 3, 417 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 418 .ex_len = 1, 419 .is_unwrit = 1 }, 420 { .ex_lblk = EXT_DATA_LBLK + 1, 421 .ex_len = EXT_DATA_LEN - 2, 422 .is_unwrit = 0 }, 423 { .ex_lblk = EXT_DATA_LBLK + 1 + (EXT_DATA_LEN - 2), 424 .ex_len = 1, 425 .is_unwrit = 1 } }, 426 .is_zeroout_test = 0 }, 427 428 /* writ to unwrit splits */ 429 { .desc = "split writ extent to 2 extents and convert 1st half unwrit", 430 .is_unwrit_at_start = 0, 431 .split_flags = EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, 432 .split_map = { .m_lblk = EXT_DATA_LBLK, .m_len = 1 }, 433 .nr_exp_ext = 2, 434 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 435 .ex_len = 1, 436 .is_unwrit = 1 }, 437 { .ex_lblk = EXT_DATA_LBLK + 1, 438 .ex_len = EXT_DATA_LEN - 1, 439 .is_unwrit = 0 } }, 440 .is_zeroout_test = 0 }, 441 { .desc = "split writ extent to 2 extents and convert 2nd half unwrit", 442 .is_unwrit_at_start = 0, 443 .split_flags = EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, 444 .split_map = { .m_lblk = EXT_DATA_LBLK + 1, .m_len = EXT_DATA_LEN - 1 }, 445 .nr_exp_ext = 2, 446 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 447 .ex_len = 1, 448 .is_unwrit = 0 }, 449 { .ex_lblk = EXT_DATA_LBLK + 1, 450 .ex_len = EXT_DATA_LEN - 1, 451 .is_unwrit = 1 } }, 452 .is_zeroout_test = 0 }, 453 { .desc = "split writ extent to 3 extents and convert 2nd half to unwrit", 454 .is_unwrit_at_start = 0, 455 .split_flags = EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, 456 .split_map = { .m_lblk = EXT_DATA_LBLK + 1, .m_len = EXT_DATA_LEN - 2 }, 457 .nr_exp_ext = 3, 458 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 459 .ex_len = 1, 460 .is_unwrit = 0 }, 461 { .ex_lblk = EXT_DATA_LBLK + 1, 462 .ex_len = EXT_DATA_LEN - 2, 463 .is_unwrit = 1 }, 464 { .ex_lblk = EXT_DATA_LBLK + 1 + (EXT_DATA_LEN - 2), 465 .ex_len = 1, 466 .is_unwrit = 0 } }, 467 .is_zeroout_test = 0 }, 468 469 /* 470 * ***** zeroout tests ***** 471 */ 472 /* unwrit to writ splits */ 473 { .desc = "split unwrit extent to 2 extents and convert 1st half writ (zeroout)", 474 .is_unwrit_at_start = 1, 475 .split_flags = EXT4_GET_BLOCKS_CONVERT, 476 .split_map = { .m_lblk = EXT_DATA_LBLK, .m_len = 1 }, 477 .nr_exp_ext = 1, 478 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 479 .ex_len = EXT_DATA_LEN, 480 .is_unwrit = 0 } }, 481 .is_zeroout_test = 1, 482 .nr_exp_data_segs = 2, 483 .exp_data_state = { { .exp_char = 'X', .off_blk = 0, .len_blk = 1 }, 484 { .exp_char = 0, 485 .off_blk = 1, 486 .len_blk = EXT_DATA_LEN - 1 } } }, 487 { .desc = "split unwrit extent to 2 extents and convert 2nd half writ (zeroout)", 488 .is_unwrit_at_start = 1, 489 .split_flags = EXT4_GET_BLOCKS_CONVERT, 490 .split_map = { .m_lblk = EXT_DATA_LBLK + 1, .m_len = EXT_DATA_LEN - 1 }, 491 .nr_exp_ext = 1, 492 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 493 .ex_len = EXT_DATA_LEN, 494 .is_unwrit = 0 } }, 495 .is_zeroout_test = 1, 496 .nr_exp_data_segs = 2, 497 .exp_data_state = { { .exp_char = 0, .off_blk = 0, .len_blk = 1 }, 498 { .exp_char = 'X', 499 .off_blk = 1, 500 .len_blk = EXT_DATA_LEN - 1 } } }, 501 { .desc = "split unwrit extent to 3 extents and convert 2nd half writ (zeroout)", 502 .is_unwrit_at_start = 1, 503 .split_flags = EXT4_GET_BLOCKS_CONVERT, 504 .split_map = { .m_lblk = EXT_DATA_LBLK + 1, .m_len = EXT_DATA_LEN - 2 }, 505 .nr_exp_ext = 1, 506 .exp_ext_state = { { .ex_lblk = EXT_DATA_LBLK, 507 .ex_len = EXT_DATA_LEN, 508 .is_unwrit = 0 } }, 509 .is_zeroout_test = 1, 510 .nr_exp_data_segs = 3, 511 .exp_data_state = { { .exp_char = 0, .off_blk = 0, .len_blk = 1 }, 512 { .exp_char = 'X', 513 .off_blk = 1, 514 .len_blk = EXT_DATA_LEN - 2 }, 515 { .exp_char = 0, 516 .off_blk = EXT_DATA_LEN - 1, 517 .len_blk = 1 } } }, 518 }; 519 520 static void ext_get_desc(struct kunit *test, const void *p, char *desc) 521 522 { 523 struct kunit_ext_test_param *param = (struct kunit_ext_test_param *)p; 524 525 snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s\n", param->desc); 526 } 527 528 static int test_split_convert_param_init(struct kunit *test) 529 { 530 size_t arr_size = ARRAY_SIZE(test_split_convert_params); 531 532 kunit_register_params_array(test, test_split_convert_params, arr_size, 533 ext_get_desc); 534 return 0; 535 } 536 537 /* 538 * Note that we use KUNIT_CASE_PARAM_WITH_INIT() instead of the more compact 539 * KUNIT_ARRAY_PARAM() because the later currently has a limitation causing the 540 * output parsing to be prone to error. For more context: 541 * 542 * https://lore.kernel.org/linux-kselftest/aULJpTvJDw9ctUDe@li-dc0c254c-257c-11b2-a85c-98b6c1322444.ibm.com/ 543 */ 544 static struct kunit_case extents_test_cases[] = { 545 KUNIT_CASE_PARAM_WITH_INIT(test_split_convert, kunit_array_gen_params, 546 test_split_convert_param_init, NULL), 547 {} 548 }; 549 550 static struct kunit_suite extents_test_suite = { 551 .name = "ext4_extents_test", 552 .init = extents_kunit_init, 553 .exit = extents_kunit_exit, 554 .test_cases = extents_test_cases, 555 }; 556 557 kunit_test_suites(&extents_test_suite); 558 559 MODULE_LICENSE("GPL"); 560