1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2013 Fusion IO. All rights reserved. 4 */ 5 6 #include <linux/types.h> 7 #include "btrfs-tests.h" 8 #include "../ctree.h" 9 #include "../btrfs_inode.h" 10 #include "../disk-io.h" 11 #include "../extent_io.h" 12 #include "../volumes.h" 13 #include "../compression.h" 14 15 static void insert_extent(struct btrfs_root *root, u64 start, u64 len, 16 u64 ram_bytes, u64 offset, u64 disk_bytenr, 17 u64 disk_len, u32 type, u8 compression, int slot) 18 { 19 struct btrfs_path path; 20 struct btrfs_file_extent_item *fi; 21 struct extent_buffer *leaf = root->node; 22 struct btrfs_key key; 23 u32 value_len = sizeof(struct btrfs_file_extent_item); 24 25 if (type == BTRFS_FILE_EXTENT_INLINE) 26 value_len += len; 27 memset(&path, 0, sizeof(path)); 28 29 path.nodes[0] = leaf; 30 path.slots[0] = slot; 31 32 key.objectid = BTRFS_FIRST_FREE_OBJECTID; 33 key.type = BTRFS_EXTENT_DATA_KEY; 34 key.offset = start; 35 36 setup_items_for_insert(root, &path, &key, &value_len, value_len, 37 value_len + sizeof(struct btrfs_item), 1); 38 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 39 btrfs_set_file_extent_generation(leaf, fi, 1); 40 btrfs_set_file_extent_type(leaf, fi, type); 41 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); 42 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len); 43 btrfs_set_file_extent_offset(leaf, fi, offset); 44 btrfs_set_file_extent_num_bytes(leaf, fi, len); 45 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); 46 btrfs_set_file_extent_compression(leaf, fi, compression); 47 btrfs_set_file_extent_encryption(leaf, fi, 0); 48 btrfs_set_file_extent_other_encoding(leaf, fi, 0); 49 } 50 51 static void insert_inode_item_key(struct btrfs_root *root) 52 { 53 struct btrfs_path path; 54 struct extent_buffer *leaf = root->node; 55 struct btrfs_key key; 56 u32 value_len = 0; 57 58 memset(&path, 0, sizeof(path)); 59 60 path.nodes[0] = leaf; 61 path.slots[0] = 0; 62 63 key.objectid = BTRFS_INODE_ITEM_KEY; 64 key.type = BTRFS_INODE_ITEM_KEY; 65 key.offset = 0; 66 67 setup_items_for_insert(root, &path, &key, &value_len, value_len, 68 value_len + sizeof(struct btrfs_item), 1); 69 } 70 71 /* 72 * Build the most complicated map of extents the earth has ever seen. We want 73 * this so we can test all of the corner cases of btrfs_get_extent. Here is a 74 * diagram of how the extents will look though this may not be possible we still 75 * want to make sure everything acts normally (the last number is not inclusive) 76 * 77 * [0 - 5][5 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291] 78 * [hole ][inline][hole but no extent][ hole ][ regular ][regular1 split] 79 * 80 * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ] 81 * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written] 82 * 83 * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635] 84 * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1] 85 * 86 * [69635-73731][ 73731 - 86019 ][86019-90115] 87 * [ regular ][ hole but no extent][ regular ] 88 */ 89 static void setup_file_extents(struct btrfs_root *root, u32 sectorsize) 90 { 91 int slot = 0; 92 u64 disk_bytenr = SZ_1M; 93 u64 offset = 0; 94 95 /* First we want a hole */ 96 insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0, 97 slot); 98 slot++; 99 offset += 5; 100 101 /* 102 * Now we want an inline extent, I don't think this is possible but hey 103 * why not? Also keep in mind if we have an inline extent it counts as 104 * the whole first page. If we were to expand it we would have to cow 105 * and we wouldn't have an inline extent anymore. 106 */ 107 insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0, 108 slot); 109 slot++; 110 offset = sectorsize; 111 112 /* Now another hole */ 113 insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0, 114 slot); 115 slot++; 116 offset += 4; 117 118 /* Now for a regular extent */ 119 insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0, 120 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); 121 slot++; 122 disk_bytenr += sectorsize; 123 offset += sectorsize - 1; 124 125 /* 126 * Now for 3 extents that were split from a hole punch so we test 127 * offsets properly. 128 */ 129 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, 130 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); 131 slot++; 132 offset += sectorsize; 133 insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0, 134 BTRFS_FILE_EXTENT_REG, 0, slot); 135 slot++; 136 offset += sectorsize; 137 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, 138 2 * sectorsize, disk_bytenr, 4 * sectorsize, 139 BTRFS_FILE_EXTENT_REG, 0, slot); 140 slot++; 141 offset += 2 * sectorsize; 142 disk_bytenr += 4 * sectorsize; 143 144 /* Now for a unwritten prealloc extent */ 145 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, 146 sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot); 147 slot++; 148 offset += sectorsize; 149 150 /* 151 * We want to jack up disk_bytenr a little more so the em stuff doesn't 152 * merge our records. 153 */ 154 disk_bytenr += 2 * sectorsize; 155 156 /* 157 * Now for a partially written prealloc extent, basically the same as 158 * the hole punch example above. Ram_bytes never changes when you mark 159 * extents written btw. 160 */ 161 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, 162 4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot); 163 slot++; 164 offset += sectorsize; 165 insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize, 166 disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, 167 slot); 168 slot++; 169 offset += sectorsize; 170 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, 171 2 * sectorsize, disk_bytenr, 4 * sectorsize, 172 BTRFS_FILE_EXTENT_PREALLOC, 0, slot); 173 slot++; 174 offset += 2 * sectorsize; 175 disk_bytenr += 4 * sectorsize; 176 177 /* Now a normal compressed extent */ 178 insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0, 179 disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 180 BTRFS_COMPRESS_ZLIB, slot); 181 slot++; 182 offset += 2 * sectorsize; 183 /* No merges */ 184 disk_bytenr += 2 * sectorsize; 185 186 /* Now a split compressed extent */ 187 insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr, 188 sectorsize, BTRFS_FILE_EXTENT_REG, 189 BTRFS_COMPRESS_ZLIB, slot); 190 slot++; 191 offset += sectorsize; 192 insert_extent(root, offset, sectorsize, sectorsize, 0, 193 disk_bytenr + sectorsize, sectorsize, 194 BTRFS_FILE_EXTENT_REG, 0, slot); 195 slot++; 196 offset += sectorsize; 197 insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize, 198 2 * sectorsize, disk_bytenr, sectorsize, 199 BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot); 200 slot++; 201 offset += 2 * sectorsize; 202 disk_bytenr += 2 * sectorsize; 203 204 /* Now extents that have a hole but no hole extent */ 205 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, 206 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); 207 slot++; 208 offset += 4 * sectorsize; 209 disk_bytenr += sectorsize; 210 insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr, 211 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot); 212 } 213 214 static unsigned long prealloc_only = 0; 215 static unsigned long compressed_only = 0; 216 static unsigned long vacancy_only = 0; 217 218 static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) 219 { 220 struct btrfs_fs_info *fs_info = NULL; 221 struct inode *inode = NULL; 222 struct btrfs_root *root = NULL; 223 struct extent_map *em = NULL; 224 u64 orig_start; 225 u64 disk_bytenr; 226 u64 offset; 227 int ret = -ENOMEM; 228 229 test_msg("running btrfs_get_extent tests"); 230 231 inode = btrfs_new_test_inode(); 232 if (!inode) { 233 test_std_err(TEST_ALLOC_INODE); 234 return ret; 235 } 236 237 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; 238 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID; 239 BTRFS_I(inode)->location.offset = 0; 240 241 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); 242 if (!fs_info) { 243 test_std_err(TEST_ALLOC_FS_INFO); 244 goto out; 245 } 246 247 root = btrfs_alloc_dummy_root(fs_info); 248 if (IS_ERR(root)) { 249 test_std_err(TEST_ALLOC_ROOT); 250 goto out; 251 } 252 253 root->node = alloc_dummy_extent_buffer(fs_info, nodesize); 254 if (!root->node) { 255 test_std_err(TEST_ALLOC_ROOT); 256 goto out; 257 } 258 259 btrfs_set_header_nritems(root->node, 0); 260 btrfs_set_header_level(root->node, 0); 261 ret = -EINVAL; 262 263 /* First with no extents */ 264 BTRFS_I(inode)->root = root; 265 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize, 0); 266 if (IS_ERR(em)) { 267 em = NULL; 268 test_err("got an error when we shouldn't have"); 269 goto out; 270 } 271 if (em->block_start != EXTENT_MAP_HOLE) { 272 test_err("expected a hole, got %llu", em->block_start); 273 goto out; 274 } 275 free_extent_map(em); 276 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); 277 278 /* 279 * All of the magic numbers are based on the mapping setup in 280 * setup_file_extents, so if you change anything there you need to 281 * update the comment and update the expected values below. 282 */ 283 setup_file_extents(root, sectorsize); 284 285 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1, 0); 286 if (IS_ERR(em)) { 287 test_err("got an error when we shouldn't have"); 288 goto out; 289 } 290 if (em->block_start != EXTENT_MAP_HOLE) { 291 test_err("expected a hole, got %llu", em->block_start); 292 goto out; 293 } 294 if (em->start != 0 || em->len != 5) { 295 test_err( 296 "unexpected extent wanted start 0 len 5, got start %llu len %llu", 297 em->start, em->len); 298 goto out; 299 } 300 if (em->flags != 0) { 301 test_err("unexpected flags set, want 0 have %lu", em->flags); 302 goto out; 303 } 304 offset = em->start + em->len; 305 free_extent_map(em); 306 307 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 308 if (IS_ERR(em)) { 309 test_err("got an error when we shouldn't have"); 310 goto out; 311 } 312 if (em->block_start != EXTENT_MAP_INLINE) { 313 test_err("expected an inline, got %llu", em->block_start); 314 goto out; 315 } 316 317 if (em->start != offset || em->len != (sectorsize - 5)) { 318 test_err( 319 "unexpected extent wanted start %llu len 1, got start %llu len %llu", 320 offset, em->start, em->len); 321 goto out; 322 } 323 if (em->flags != 0) { 324 test_err("unexpected flags set, want 0 have %lu", em->flags); 325 goto out; 326 } 327 /* 328 * We don't test anything else for inline since it doesn't get set 329 * unless we have a page for it to write into. Maybe we should change 330 * this? 331 */ 332 offset = em->start + em->len; 333 free_extent_map(em); 334 335 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 336 if (IS_ERR(em)) { 337 test_err("got an error when we shouldn't have"); 338 goto out; 339 } 340 if (em->block_start != EXTENT_MAP_HOLE) { 341 test_err("expected a hole, got %llu", em->block_start); 342 goto out; 343 } 344 if (em->start != offset || em->len != 4) { 345 test_err( 346 "unexpected extent wanted start %llu len 4, got start %llu len %llu", 347 offset, em->start, em->len); 348 goto out; 349 } 350 if (em->flags != 0) { 351 test_err("unexpected flags set, want 0 have %lu", em->flags); 352 goto out; 353 } 354 offset = em->start + em->len; 355 free_extent_map(em); 356 357 /* Regular extent */ 358 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 359 if (IS_ERR(em)) { 360 test_err("got an error when we shouldn't have"); 361 goto out; 362 } 363 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 364 test_err("expected a real extent, got %llu", em->block_start); 365 goto out; 366 } 367 if (em->start != offset || em->len != sectorsize - 1) { 368 test_err( 369 "unexpected extent wanted start %llu len 4095, got start %llu len %llu", 370 offset, em->start, em->len); 371 goto out; 372 } 373 if (em->flags != 0) { 374 test_err("unexpected flags set, want 0 have %lu", em->flags); 375 goto out; 376 } 377 if (em->orig_start != em->start) { 378 test_err("wrong orig offset, want %llu, have %llu", em->start, 379 em->orig_start); 380 goto out; 381 } 382 offset = em->start + em->len; 383 free_extent_map(em); 384 385 /* The next 3 are split extents */ 386 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 387 if (IS_ERR(em)) { 388 test_err("got an error when we shouldn't have"); 389 goto out; 390 } 391 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 392 test_err("expected a real extent, got %llu", em->block_start); 393 goto out; 394 } 395 if (em->start != offset || em->len != sectorsize) { 396 test_err( 397 "unexpected extent start %llu len %u, got start %llu len %llu", 398 offset, sectorsize, em->start, em->len); 399 goto out; 400 } 401 if (em->flags != 0) { 402 test_err("unexpected flags set, want 0 have %lu", em->flags); 403 goto out; 404 } 405 if (em->orig_start != em->start) { 406 test_err("wrong orig offset, want %llu, have %llu", em->start, 407 em->orig_start); 408 goto out; 409 } 410 disk_bytenr = em->block_start; 411 orig_start = em->start; 412 offset = em->start + em->len; 413 free_extent_map(em); 414 415 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 416 if (IS_ERR(em)) { 417 test_err("got an error when we shouldn't have"); 418 goto out; 419 } 420 if (em->block_start != EXTENT_MAP_HOLE) { 421 test_err("expected a hole, got %llu", em->block_start); 422 goto out; 423 } 424 if (em->start != offset || em->len != sectorsize) { 425 test_err( 426 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 427 offset, sectorsize, em->start, em->len); 428 goto out; 429 } 430 if (em->flags != 0) { 431 test_err("unexpected flags set, want 0 have %lu", em->flags); 432 goto out; 433 } 434 offset = em->start + em->len; 435 free_extent_map(em); 436 437 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 438 if (IS_ERR(em)) { 439 test_err("got an error when we shouldn't have"); 440 goto out; 441 } 442 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 443 test_err("expected a real extent, got %llu", em->block_start); 444 goto out; 445 } 446 if (em->start != offset || em->len != 2 * sectorsize) { 447 test_err( 448 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 449 offset, 2 * sectorsize, em->start, em->len); 450 goto out; 451 } 452 if (em->flags != 0) { 453 test_err("unexpected flags set, want 0 have %lu", em->flags); 454 goto out; 455 } 456 if (em->orig_start != orig_start) { 457 test_err("wrong orig offset, want %llu, have %llu", 458 orig_start, em->orig_start); 459 goto out; 460 } 461 disk_bytenr += (em->start - orig_start); 462 if (em->block_start != disk_bytenr) { 463 test_err("wrong block start, want %llu, have %llu", 464 disk_bytenr, em->block_start); 465 goto out; 466 } 467 offset = em->start + em->len; 468 free_extent_map(em); 469 470 /* Prealloc extent */ 471 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 472 if (IS_ERR(em)) { 473 test_err("got an error when we shouldn't have"); 474 goto out; 475 } 476 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 477 test_err("expected a real extent, got %llu", em->block_start); 478 goto out; 479 } 480 if (em->start != offset || em->len != sectorsize) { 481 test_err( 482 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 483 offset, sectorsize, em->start, em->len); 484 goto out; 485 } 486 if (em->flags != prealloc_only) { 487 test_err("unexpected flags set, want %lu have %lu", 488 prealloc_only, em->flags); 489 goto out; 490 } 491 if (em->orig_start != em->start) { 492 test_err("wrong orig offset, want %llu, have %llu", em->start, 493 em->orig_start); 494 goto out; 495 } 496 offset = em->start + em->len; 497 free_extent_map(em); 498 499 /* The next 3 are a half written prealloc extent */ 500 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 501 if (IS_ERR(em)) { 502 test_err("got an error when we shouldn't have"); 503 goto out; 504 } 505 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 506 test_err("expected a real extent, got %llu", em->block_start); 507 goto out; 508 } 509 if (em->start != offset || em->len != sectorsize) { 510 test_err( 511 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 512 offset, sectorsize, em->start, em->len); 513 goto out; 514 } 515 if (em->flags != prealloc_only) { 516 test_err("unexpected flags set, want %lu have %lu", 517 prealloc_only, em->flags); 518 goto out; 519 } 520 if (em->orig_start != em->start) { 521 test_err("wrong orig offset, want %llu, have %llu", em->start, 522 em->orig_start); 523 goto out; 524 } 525 disk_bytenr = em->block_start; 526 orig_start = em->start; 527 offset = em->start + em->len; 528 free_extent_map(em); 529 530 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 531 if (IS_ERR(em)) { 532 test_err("got an error when we shouldn't have"); 533 goto out; 534 } 535 if (em->block_start >= EXTENT_MAP_HOLE) { 536 test_err("expected a real extent, got %llu", em->block_start); 537 goto out; 538 } 539 if (em->start != offset || em->len != sectorsize) { 540 test_err( 541 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 542 offset, sectorsize, em->start, em->len); 543 goto out; 544 } 545 if (em->flags != 0) { 546 test_err("unexpected flags set, want 0 have %lu", em->flags); 547 goto out; 548 } 549 if (em->orig_start != orig_start) { 550 test_err("unexpected orig offset, wanted %llu, have %llu", 551 orig_start, em->orig_start); 552 goto out; 553 } 554 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) { 555 test_err("unexpected block start, wanted %llu, have %llu", 556 disk_bytenr + (em->start - em->orig_start), 557 em->block_start); 558 goto out; 559 } 560 offset = em->start + em->len; 561 free_extent_map(em); 562 563 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 564 if (IS_ERR(em)) { 565 test_err("got an error when we shouldn't have"); 566 goto out; 567 } 568 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 569 test_err("expected a real extent, got %llu", em->block_start); 570 goto out; 571 } 572 if (em->start != offset || em->len != 2 * sectorsize) { 573 test_err( 574 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 575 offset, 2 * sectorsize, em->start, em->len); 576 goto out; 577 } 578 if (em->flags != prealloc_only) { 579 test_err("unexpected flags set, want %lu have %lu", 580 prealloc_only, em->flags); 581 goto out; 582 } 583 if (em->orig_start != orig_start) { 584 test_err("wrong orig offset, want %llu, have %llu", orig_start, 585 em->orig_start); 586 goto out; 587 } 588 if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) { 589 test_err("unexpected block start, wanted %llu, have %llu", 590 disk_bytenr + (em->start - em->orig_start), 591 em->block_start); 592 goto out; 593 } 594 offset = em->start + em->len; 595 free_extent_map(em); 596 597 /* Now for the compressed extent */ 598 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 599 if (IS_ERR(em)) { 600 test_err("got an error when we shouldn't have"); 601 goto out; 602 } 603 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 604 test_err("expected a real extent, got %llu", em->block_start); 605 goto out; 606 } 607 if (em->start != offset || em->len != 2 * sectorsize) { 608 test_err( 609 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 610 offset, 2 * sectorsize, em->start, em->len); 611 goto out; 612 } 613 if (em->flags != compressed_only) { 614 test_err("unexpected flags set, want %lu have %lu", 615 compressed_only, em->flags); 616 goto out; 617 } 618 if (em->orig_start != em->start) { 619 test_err("wrong orig offset, want %llu, have %llu", 620 em->start, em->orig_start); 621 goto out; 622 } 623 if (em->compress_type != BTRFS_COMPRESS_ZLIB) { 624 test_err("unexpected compress type, wanted %d, got %d", 625 BTRFS_COMPRESS_ZLIB, em->compress_type); 626 goto out; 627 } 628 offset = em->start + em->len; 629 free_extent_map(em); 630 631 /* Split compressed extent */ 632 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 633 if (IS_ERR(em)) { 634 test_err("got an error when we shouldn't have"); 635 goto out; 636 } 637 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 638 test_err("expected a real extent, got %llu", em->block_start); 639 goto out; 640 } 641 if (em->start != offset || em->len != sectorsize) { 642 test_err( 643 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 644 offset, sectorsize, em->start, em->len); 645 goto out; 646 } 647 if (em->flags != compressed_only) { 648 test_err("unexpected flags set, want %lu have %lu", 649 compressed_only, em->flags); 650 goto out; 651 } 652 if (em->orig_start != em->start) { 653 test_err("wrong orig offset, want %llu, have %llu", 654 em->start, em->orig_start); 655 goto out; 656 } 657 if (em->compress_type != BTRFS_COMPRESS_ZLIB) { 658 test_err("unexpected compress type, wanted %d, got %d", 659 BTRFS_COMPRESS_ZLIB, em->compress_type); 660 goto out; 661 } 662 disk_bytenr = em->block_start; 663 orig_start = em->start; 664 offset = em->start + em->len; 665 free_extent_map(em); 666 667 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 668 if (IS_ERR(em)) { 669 test_err("got an error when we shouldn't have"); 670 goto out; 671 } 672 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 673 test_err("expected a real extent, got %llu", em->block_start); 674 goto out; 675 } 676 if (em->start != offset || em->len != sectorsize) { 677 test_err( 678 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 679 offset, sectorsize, em->start, em->len); 680 goto out; 681 } 682 if (em->flags != 0) { 683 test_err("unexpected flags set, want 0 have %lu", em->flags); 684 goto out; 685 } 686 if (em->orig_start != em->start) { 687 test_err("wrong orig offset, want %llu, have %llu", em->start, 688 em->orig_start); 689 goto out; 690 } 691 offset = em->start + em->len; 692 free_extent_map(em); 693 694 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 695 if (IS_ERR(em)) { 696 test_err("got an error when we shouldn't have"); 697 goto out; 698 } 699 if (em->block_start != disk_bytenr) { 700 test_err("block start does not match, want %llu got %llu", 701 disk_bytenr, em->block_start); 702 goto out; 703 } 704 if (em->start != offset || em->len != 2 * sectorsize) { 705 test_err( 706 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 707 offset, 2 * sectorsize, em->start, em->len); 708 goto out; 709 } 710 if (em->flags != compressed_only) { 711 test_err("unexpected flags set, want %lu have %lu", 712 compressed_only, em->flags); 713 goto out; 714 } 715 if (em->orig_start != orig_start) { 716 test_err("wrong orig offset, want %llu, have %llu", 717 em->start, orig_start); 718 goto out; 719 } 720 if (em->compress_type != BTRFS_COMPRESS_ZLIB) { 721 test_err("unexpected compress type, wanted %d, got %d", 722 BTRFS_COMPRESS_ZLIB, em->compress_type); 723 goto out; 724 } 725 offset = em->start + em->len; 726 free_extent_map(em); 727 728 /* A hole between regular extents but no hole extent */ 729 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6, 730 sectorsize, 0); 731 if (IS_ERR(em)) { 732 test_err("got an error when we shouldn't have"); 733 goto out; 734 } 735 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 736 test_err("expected a real extent, got %llu", em->block_start); 737 goto out; 738 } 739 if (em->start != offset || em->len != sectorsize) { 740 test_err( 741 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 742 offset, sectorsize, em->start, em->len); 743 goto out; 744 } 745 if (em->flags != 0) { 746 test_err("unexpected flags set, want 0 have %lu", em->flags); 747 goto out; 748 } 749 if (em->orig_start != em->start) { 750 test_err("wrong orig offset, want %llu, have %llu", em->start, 751 em->orig_start); 752 goto out; 753 } 754 offset = em->start + em->len; 755 free_extent_map(em); 756 757 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M, 0); 758 if (IS_ERR(em)) { 759 test_err("got an error when we shouldn't have"); 760 goto out; 761 } 762 if (em->block_start != EXTENT_MAP_HOLE) { 763 test_err("expected a hole extent, got %llu", em->block_start); 764 goto out; 765 } 766 /* 767 * Currently we just return a length that we requested rather than the 768 * length of the actual hole, if this changes we'll have to change this 769 * test. 770 */ 771 if (em->start != offset || em->len != 3 * sectorsize) { 772 test_err( 773 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 774 offset, 3 * sectorsize, em->start, em->len); 775 goto out; 776 } 777 if (em->flags != vacancy_only) { 778 test_err("unexpected flags set, want %lu have %lu", 779 vacancy_only, em->flags); 780 goto out; 781 } 782 if (em->orig_start != em->start) { 783 test_err("wrong orig offset, want %llu, have %llu", em->start, 784 em->orig_start); 785 goto out; 786 } 787 offset = em->start + em->len; 788 free_extent_map(em); 789 790 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0); 791 if (IS_ERR(em)) { 792 test_err("got an error when we shouldn't have"); 793 goto out; 794 } 795 if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 796 test_err("expected a real extent, got %llu", em->block_start); 797 goto out; 798 } 799 if (em->start != offset || em->len != sectorsize) { 800 test_err( 801 "unexpected extent wanted start %llu len %u, got start %llu len %llu", 802 offset, sectorsize, em->start, em->len); 803 goto out; 804 } 805 if (em->flags != 0) { 806 test_err("unexpected flags set, want 0 have %lu", em->flags); 807 goto out; 808 } 809 if (em->orig_start != em->start) { 810 test_err("wrong orig offset, want %llu, have %llu", em->start, 811 em->orig_start); 812 goto out; 813 } 814 ret = 0; 815 out: 816 if (!IS_ERR(em)) 817 free_extent_map(em); 818 iput(inode); 819 btrfs_free_dummy_root(root); 820 btrfs_free_dummy_fs_info(fs_info); 821 return ret; 822 } 823 824 static int test_hole_first(u32 sectorsize, u32 nodesize) 825 { 826 struct btrfs_fs_info *fs_info = NULL; 827 struct inode *inode = NULL; 828 struct btrfs_root *root = NULL; 829 struct extent_map *em = NULL; 830 int ret = -ENOMEM; 831 832 test_msg("running hole first btrfs_get_extent test"); 833 834 inode = btrfs_new_test_inode(); 835 if (!inode) { 836 test_std_err(TEST_ALLOC_INODE); 837 return ret; 838 } 839 840 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; 841 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID; 842 BTRFS_I(inode)->location.offset = 0; 843 844 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); 845 if (!fs_info) { 846 test_std_err(TEST_ALLOC_FS_INFO); 847 goto out; 848 } 849 850 root = btrfs_alloc_dummy_root(fs_info); 851 if (IS_ERR(root)) { 852 test_std_err(TEST_ALLOC_ROOT); 853 goto out; 854 } 855 856 root->node = alloc_dummy_extent_buffer(fs_info, nodesize); 857 if (!root->node) { 858 test_std_err(TEST_ALLOC_ROOT); 859 goto out; 860 } 861 862 btrfs_set_header_nritems(root->node, 0); 863 btrfs_set_header_level(root->node, 0); 864 BTRFS_I(inode)->root = root; 865 ret = -EINVAL; 866 867 /* 868 * Need a blank inode item here just so we don't confuse 869 * btrfs_get_extent. 870 */ 871 insert_inode_item_key(root); 872 insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize, 873 sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1); 874 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize, 0); 875 if (IS_ERR(em)) { 876 test_err("got an error when we shouldn't have"); 877 goto out; 878 } 879 if (em->block_start != EXTENT_MAP_HOLE) { 880 test_err("expected a hole, got %llu", em->block_start); 881 goto out; 882 } 883 if (em->start != 0 || em->len != sectorsize) { 884 test_err( 885 "unexpected extent wanted start 0 len %u, got start %llu len %llu", 886 sectorsize, em->start, em->len); 887 goto out; 888 } 889 if (em->flags != vacancy_only) { 890 test_err("wrong flags, wanted %lu, have %lu", vacancy_only, 891 em->flags); 892 goto out; 893 } 894 free_extent_map(em); 895 896 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize, 897 2 * sectorsize, 0); 898 if (IS_ERR(em)) { 899 test_err("got an error when we shouldn't have"); 900 goto out; 901 } 902 if (em->block_start != sectorsize) { 903 test_err("expected a real extent, got %llu", em->block_start); 904 goto out; 905 } 906 if (em->start != sectorsize || em->len != sectorsize) { 907 test_err( 908 "unexpected extent wanted start %u len %u, got start %llu len %llu", 909 sectorsize, sectorsize, em->start, em->len); 910 goto out; 911 } 912 if (em->flags != 0) { 913 test_err("unexpected flags set, wanted 0 got %lu", 914 em->flags); 915 goto out; 916 } 917 ret = 0; 918 out: 919 if (!IS_ERR(em)) 920 free_extent_map(em); 921 iput(inode); 922 btrfs_free_dummy_root(root); 923 btrfs_free_dummy_fs_info(fs_info); 924 return ret; 925 } 926 927 static int test_extent_accounting(u32 sectorsize, u32 nodesize) 928 { 929 struct btrfs_fs_info *fs_info = NULL; 930 struct inode *inode = NULL; 931 struct btrfs_root *root = NULL; 932 int ret = -ENOMEM; 933 934 test_msg("running outstanding_extents tests"); 935 936 inode = btrfs_new_test_inode(); 937 if (!inode) { 938 test_std_err(TEST_ALLOC_INODE); 939 return ret; 940 } 941 942 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); 943 if (!fs_info) { 944 test_std_err(TEST_ALLOC_FS_INFO); 945 goto out; 946 } 947 948 root = btrfs_alloc_dummy_root(fs_info); 949 if (IS_ERR(root)) { 950 test_std_err(TEST_ALLOC_ROOT); 951 goto out; 952 } 953 954 BTRFS_I(inode)->root = root; 955 btrfs_test_inode_set_ops(inode); 956 957 /* [BTRFS_MAX_EXTENT_SIZE] */ 958 ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1, 0, 959 NULL, 0); 960 if (ret) { 961 test_err("btrfs_set_extent_delalloc returned %d", ret); 962 goto out; 963 } 964 if (BTRFS_I(inode)->outstanding_extents != 1) { 965 ret = -EINVAL; 966 test_err("miscount, wanted 1, got %u", 967 BTRFS_I(inode)->outstanding_extents); 968 goto out; 969 } 970 971 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ 972 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE, 973 BTRFS_MAX_EXTENT_SIZE + sectorsize - 1, 974 0, NULL, 0); 975 if (ret) { 976 test_err("btrfs_set_extent_delalloc returned %d", ret); 977 goto out; 978 } 979 if (BTRFS_I(inode)->outstanding_extents != 2) { 980 ret = -EINVAL; 981 test_err("miscount, wanted 2, got %u", 982 BTRFS_I(inode)->outstanding_extents); 983 goto out; 984 } 985 986 /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */ 987 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 988 BTRFS_MAX_EXTENT_SIZE >> 1, 989 (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1, 990 EXTENT_DELALLOC | EXTENT_DIRTY | 991 EXTENT_UPTODATE, 0, 0, NULL); 992 if (ret) { 993 test_err("clear_extent_bit returned %d", ret); 994 goto out; 995 } 996 if (BTRFS_I(inode)->outstanding_extents != 2) { 997 ret = -EINVAL; 998 test_err("miscount, wanted 2, got %u", 999 BTRFS_I(inode)->outstanding_extents); 1000 goto out; 1001 } 1002 1003 /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ 1004 ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1, 1005 (BTRFS_MAX_EXTENT_SIZE >> 1) 1006 + sectorsize - 1, 1007 0, NULL, 0); 1008 if (ret) { 1009 test_err("btrfs_set_extent_delalloc returned %d", ret); 1010 goto out; 1011 } 1012 if (BTRFS_I(inode)->outstanding_extents != 2) { 1013 ret = -EINVAL; 1014 test_err("miscount, wanted 2, got %u", 1015 BTRFS_I(inode)->outstanding_extents); 1016 goto out; 1017 } 1018 1019 /* 1020 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize] 1021 */ 1022 ret = btrfs_set_extent_delalloc(inode, 1023 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize, 1024 (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1, 1025 0, NULL, 0); 1026 if (ret) { 1027 test_err("btrfs_set_extent_delalloc returned %d", ret); 1028 goto out; 1029 } 1030 if (BTRFS_I(inode)->outstanding_extents != 4) { 1031 ret = -EINVAL; 1032 test_err("miscount, wanted 4, got %u", 1033 BTRFS_I(inode)->outstanding_extents); 1034 goto out; 1035 } 1036 1037 /* 1038 * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize] 1039 */ 1040 ret = btrfs_set_extent_delalloc(inode, 1041 BTRFS_MAX_EXTENT_SIZE + sectorsize, 1042 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0); 1043 if (ret) { 1044 test_err("btrfs_set_extent_delalloc returned %d", ret); 1045 goto out; 1046 } 1047 if (BTRFS_I(inode)->outstanding_extents != 3) { 1048 ret = -EINVAL; 1049 test_err("miscount, wanted 3, got %u", 1050 BTRFS_I(inode)->outstanding_extents); 1051 goto out; 1052 } 1053 1054 /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */ 1055 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 1056 BTRFS_MAX_EXTENT_SIZE + sectorsize, 1057 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 1058 EXTENT_DIRTY | EXTENT_DELALLOC | 1059 EXTENT_UPTODATE, 0, 0, NULL); 1060 if (ret) { 1061 test_err("clear_extent_bit returned %d", ret); 1062 goto out; 1063 } 1064 if (BTRFS_I(inode)->outstanding_extents != 4) { 1065 ret = -EINVAL; 1066 test_err("miscount, wanted 4, got %u", 1067 BTRFS_I(inode)->outstanding_extents); 1068 goto out; 1069 } 1070 1071 /* 1072 * Refill the hole again just for good measure, because I thought it 1073 * might fail and I'd rather satisfy my paranoia at this point. 1074 */ 1075 ret = btrfs_set_extent_delalloc(inode, 1076 BTRFS_MAX_EXTENT_SIZE + sectorsize, 1077 BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0); 1078 if (ret) { 1079 test_err("btrfs_set_extent_delalloc returned %d", ret); 1080 goto out; 1081 } 1082 if (BTRFS_I(inode)->outstanding_extents != 3) { 1083 ret = -EINVAL; 1084 test_err("miscount, wanted 3, got %u", 1085 BTRFS_I(inode)->outstanding_extents); 1086 goto out; 1087 } 1088 1089 /* Empty */ 1090 ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1, 1091 EXTENT_DIRTY | EXTENT_DELALLOC | 1092 EXTENT_UPTODATE, 0, 0, NULL); 1093 if (ret) { 1094 test_err("clear_extent_bit returned %d", ret); 1095 goto out; 1096 } 1097 if (BTRFS_I(inode)->outstanding_extents) { 1098 ret = -EINVAL; 1099 test_err("miscount, wanted 0, got %u", 1100 BTRFS_I(inode)->outstanding_extents); 1101 goto out; 1102 } 1103 ret = 0; 1104 out: 1105 if (ret) 1106 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1, 1107 EXTENT_DIRTY | EXTENT_DELALLOC | 1108 EXTENT_UPTODATE, 0, 0, NULL); 1109 iput(inode); 1110 btrfs_free_dummy_root(root); 1111 btrfs_free_dummy_fs_info(fs_info); 1112 return ret; 1113 } 1114 1115 int btrfs_test_inodes(u32 sectorsize, u32 nodesize) 1116 { 1117 int ret; 1118 1119 test_msg("running inode tests"); 1120 1121 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only); 1122 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only); 1123 1124 ret = test_btrfs_get_extent(sectorsize, nodesize); 1125 if (ret) 1126 return ret; 1127 ret = test_hole_first(sectorsize, nodesize); 1128 if (ret) 1129 return ret; 1130 return test_extent_accounting(sectorsize, nodesize); 1131 } 1132