1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. 5 * 6 * This code builds two trees of free clusters extents. 7 * Trees are sorted by start of extent and by length of extent. 8 * NTFS_MAX_WND_EXTENTS defines the maximum number of elements in trees. 9 * In extreme case code reads on-disk bitmap to find free clusters. 10 * 11 */ 12 13 #include <linux/buffer_head.h> 14 #include <linux/fs.h> 15 16 #include "ntfs.h" 17 #include "ntfs_fs.h" 18 19 /* 20 * Maximum number of extents in tree. 21 */ 22 #define NTFS_MAX_WND_EXTENTS (32u * 1024u) 23 24 struct rb_node_key { 25 struct rb_node node; 26 size_t key; 27 }; 28 29 struct e_node { 30 struct rb_node_key start; /* Tree sorted by start. */ 31 struct rb_node_key count; /* Tree sorted by len. */ 32 }; 33 34 static int wnd_rescan(struct wnd_bitmap *wnd); 35 static struct buffer_head *wnd_map(struct wnd_bitmap *wnd, size_t iw); 36 static bool wnd_is_free_hlp(struct wnd_bitmap *wnd, size_t bit, size_t bits); 37 38 static struct kmem_cache *ntfs_enode_cachep; 39 40 int __init ntfs3_init_bitmap(void) 41 { 42 ntfs_enode_cachep = 43 kmem_cache_create("ntfs3_enode_cache", sizeof(struct e_node), 0, 44 SLAB_RECLAIM_ACCOUNT, NULL); 45 return ntfs_enode_cachep ? 0 : -ENOMEM; 46 } 47 48 void ntfs3_exit_bitmap(void) 49 { 50 kmem_cache_destroy(ntfs_enode_cachep); 51 } 52 53 static inline u32 wnd_bits(const struct wnd_bitmap *wnd, size_t i) 54 { 55 return i + 1 == wnd->nwnd ? wnd->bits_last : wnd->sb->s_blocksize * 8; 56 } 57 58 /* 59 * wnd_scan 60 * 61 * b_pos + b_len - biggest fragment. 62 * Scan range [wpos wbits) window @buf. 63 * 64 * Return: -1 if not found. 65 */ 66 static size_t wnd_scan(const ulong *buf, size_t wbit, u32 wpos, u32 wend, 67 size_t to_alloc, size_t *prev_tail, size_t *b_pos, 68 size_t *b_len) 69 { 70 while (wpos < wend) { 71 size_t free_len; 72 u32 free_bits, end; 73 u32 used = find_next_zero_bit(buf, wend, wpos); 74 75 if (used >= wend) { 76 if (*b_len < *prev_tail) { 77 *b_pos = wbit - *prev_tail; 78 *b_len = *prev_tail; 79 } 80 81 *prev_tail = 0; 82 return -1; 83 } 84 85 if (used > wpos) { 86 wpos = used; 87 if (*b_len < *prev_tail) { 88 *b_pos = wbit - *prev_tail; 89 *b_len = *prev_tail; 90 } 91 92 *prev_tail = 0; 93 } 94 95 /* 96 * Now we have a fragment [wpos, wend) staring with 0. 97 */ 98 end = wpos + to_alloc - *prev_tail; 99 free_bits = find_next_bit(buf, min(end, wend), wpos); 100 101 free_len = *prev_tail + free_bits - wpos; 102 103 if (*b_len < free_len) { 104 *b_pos = wbit + wpos - *prev_tail; 105 *b_len = free_len; 106 } 107 108 if (free_len >= to_alloc) 109 return wbit + wpos - *prev_tail; 110 111 if (free_bits >= wend) { 112 *prev_tail += free_bits - wpos; 113 return -1; 114 } 115 116 wpos = free_bits + 1; 117 118 *prev_tail = 0; 119 } 120 121 return -1; 122 } 123 124 /* 125 * wnd_close - Frees all resources. 126 */ 127 void wnd_close(struct wnd_bitmap *wnd) 128 { 129 struct rb_node *node, *next; 130 131 kfree(wnd->free_bits); 132 run_close(&wnd->run); 133 134 node = rb_first(&wnd->start_tree); 135 136 while (node) { 137 next = rb_next(node); 138 rb_erase(node, &wnd->start_tree); 139 kmem_cache_free(ntfs_enode_cachep, 140 rb_entry(node, struct e_node, start.node)); 141 node = next; 142 } 143 } 144 145 static struct rb_node *rb_lookup(struct rb_root *root, size_t v) 146 { 147 struct rb_node **p = &root->rb_node; 148 struct rb_node *r = NULL; 149 150 while (*p) { 151 struct rb_node_key *k; 152 153 k = rb_entry(*p, struct rb_node_key, node); 154 if (v < k->key) { 155 p = &(*p)->rb_left; 156 } else if (v > k->key) { 157 r = &k->node; 158 p = &(*p)->rb_right; 159 } else { 160 return &k->node; 161 } 162 } 163 164 return r; 165 } 166 167 /* 168 * rb_insert_count - Helper function to insert special kind of 'count' tree. 169 */ 170 static inline bool rb_insert_count(struct rb_root *root, struct e_node *e) 171 { 172 struct rb_node **p = &root->rb_node; 173 struct rb_node *parent = NULL; 174 size_t e_ckey = e->count.key; 175 size_t e_skey = e->start.key; 176 177 while (*p) { 178 struct e_node *k = 179 rb_entry(parent = *p, struct e_node, count.node); 180 181 if (e_ckey > k->count.key) { 182 p = &(*p)->rb_left; 183 } else if (e_ckey < k->count.key) { 184 p = &(*p)->rb_right; 185 } else if (e_skey < k->start.key) { 186 p = &(*p)->rb_left; 187 } else if (e_skey > k->start.key) { 188 p = &(*p)->rb_right; 189 } else { 190 WARN_ON(1); 191 return false; 192 } 193 } 194 195 rb_link_node(&e->count.node, parent, p); 196 rb_insert_color(&e->count.node, root); 197 return true; 198 } 199 200 /* 201 * rb_insert_start - Helper function to insert special kind of 'count' tree. 202 */ 203 static inline bool rb_insert_start(struct rb_root *root, struct e_node *e) 204 { 205 struct rb_node **p = &root->rb_node; 206 struct rb_node *parent = NULL; 207 size_t e_skey = e->start.key; 208 209 while (*p) { 210 struct e_node *k; 211 212 parent = *p; 213 214 k = rb_entry(parent, struct e_node, start.node); 215 if (e_skey < k->start.key) { 216 p = &(*p)->rb_left; 217 } else if (e_skey > k->start.key) { 218 p = &(*p)->rb_right; 219 } else { 220 WARN_ON(1); 221 return false; 222 } 223 } 224 225 rb_link_node(&e->start.node, parent, p); 226 rb_insert_color(&e->start.node, root); 227 return true; 228 } 229 230 /* 231 * wnd_add_free_ext - Adds a new extent of free space. 232 * @build: 1 when building tree. 233 */ 234 static void wnd_add_free_ext(struct wnd_bitmap *wnd, size_t bit, size_t len, 235 bool build) 236 { 237 struct e_node *e, *e0 = NULL; 238 size_t ib, end_in = bit + len; 239 struct rb_node *n; 240 241 if (build) { 242 /* Use extent_min to filter too short extents. */ 243 if (wnd->count >= NTFS_MAX_WND_EXTENTS && 244 len <= wnd->extent_min) { 245 wnd->uptodated = -1; 246 return; 247 } 248 } else { 249 /* Try to find extent before 'bit'. */ 250 n = rb_lookup(&wnd->start_tree, bit); 251 252 if (!n) { 253 n = rb_first(&wnd->start_tree); 254 } else { 255 e = rb_entry(n, struct e_node, start.node); 256 n = rb_next(n); 257 if (e->start.key + e->count.key == bit) { 258 /* Remove left. */ 259 bit = e->start.key; 260 len += e->count.key; 261 rb_erase(&e->start.node, &wnd->start_tree); 262 rb_erase(&e->count.node, &wnd->count_tree); 263 wnd->count -= 1; 264 e0 = e; 265 } 266 } 267 268 while (n) { 269 size_t next_end; 270 271 e = rb_entry(n, struct e_node, start.node); 272 next_end = e->start.key + e->count.key; 273 if (e->start.key > end_in) 274 break; 275 276 /* Remove right. */ 277 n = rb_next(n); 278 len += next_end - end_in; 279 end_in = next_end; 280 rb_erase(&e->start.node, &wnd->start_tree); 281 rb_erase(&e->count.node, &wnd->count_tree); 282 wnd->count -= 1; 283 284 if (!e0) 285 e0 = e; 286 else 287 kmem_cache_free(ntfs_enode_cachep, e); 288 } 289 290 if (wnd->uptodated != 1) { 291 /* Check bits before 'bit'. */ 292 ib = wnd->zone_bit == wnd->zone_end || 293 bit < wnd->zone_end 294 ? 0 295 : wnd->zone_end; 296 297 while (bit > ib && wnd_is_free_hlp(wnd, bit - 1, 1)) { 298 bit -= 1; 299 len += 1; 300 } 301 302 /* Check bits after 'end_in'. */ 303 ib = wnd->zone_bit == wnd->zone_end || 304 end_in > wnd->zone_bit 305 ? wnd->nbits 306 : wnd->zone_bit; 307 308 while (end_in < ib && wnd_is_free_hlp(wnd, end_in, 1)) { 309 end_in += 1; 310 len += 1; 311 } 312 } 313 } 314 /* Insert new fragment. */ 315 if (wnd->count >= NTFS_MAX_WND_EXTENTS) { 316 if (e0) 317 kmem_cache_free(ntfs_enode_cachep, e0); 318 319 wnd->uptodated = -1; 320 321 /* Compare with smallest fragment. */ 322 n = rb_last(&wnd->count_tree); 323 e = rb_entry(n, struct e_node, count.node); 324 if (len <= e->count.key) 325 goto out; /* Do not insert small fragments. */ 326 327 if (build) { 328 struct e_node *e2; 329 330 n = rb_prev(n); 331 e2 = rb_entry(n, struct e_node, count.node); 332 /* Smallest fragment will be 'e2->count.key'. */ 333 wnd->extent_min = e2->count.key; 334 } 335 336 /* Replace smallest fragment by new one. */ 337 rb_erase(&e->start.node, &wnd->start_tree); 338 rb_erase(&e->count.node, &wnd->count_tree); 339 wnd->count -= 1; 340 } else { 341 e = e0 ? e0 : kmem_cache_alloc(ntfs_enode_cachep, GFP_ATOMIC); 342 if (!e) { 343 wnd->uptodated = -1; 344 goto out; 345 } 346 347 if (build && len <= wnd->extent_min) 348 wnd->extent_min = len; 349 } 350 e->start.key = bit; 351 e->count.key = len; 352 if (len > wnd->extent_max) 353 wnd->extent_max = len; 354 355 rb_insert_start(&wnd->start_tree, e); 356 rb_insert_count(&wnd->count_tree, e); 357 wnd->count += 1; 358 359 out:; 360 } 361 362 /* 363 * wnd_remove_free_ext - Remove a run from the cached free space. 364 */ 365 static void wnd_remove_free_ext(struct wnd_bitmap *wnd, size_t bit, size_t len) 366 { 367 struct rb_node *n, *n3; 368 struct e_node *e, *e3; 369 size_t end_in = bit + len; 370 size_t end3, end, new_key, new_len, max_new_len; 371 372 /* Try to find extent before 'bit'. */ 373 n = rb_lookup(&wnd->start_tree, bit); 374 375 if (!n) 376 return; 377 378 e = rb_entry(n, struct e_node, start.node); 379 end = e->start.key + e->count.key; 380 381 new_key = new_len = 0; 382 len = e->count.key; 383 384 /* Range [bit,end_in) must be inside 'e' or outside 'e' and 'n'. */ 385 if (e->start.key > bit) 386 ; 387 else if (end_in <= end) { 388 /* Range [bit,end_in) inside 'e'. */ 389 new_key = end_in; 390 new_len = end - end_in; 391 len = bit - e->start.key; 392 } else if (bit > end) { 393 bool bmax = false; 394 395 n3 = rb_next(n); 396 397 while (n3) { 398 e3 = rb_entry(n3, struct e_node, start.node); 399 if (e3->start.key >= end_in) 400 break; 401 402 if (e3->count.key == wnd->extent_max) 403 bmax = true; 404 405 end3 = e3->start.key + e3->count.key; 406 if (end3 > end_in) { 407 e3->start.key = end_in; 408 rb_erase(&e3->count.node, &wnd->count_tree); 409 e3->count.key = end3 - end_in; 410 rb_insert_count(&wnd->count_tree, e3); 411 break; 412 } 413 414 n3 = rb_next(n3); 415 rb_erase(&e3->start.node, &wnd->start_tree); 416 rb_erase(&e3->count.node, &wnd->count_tree); 417 wnd->count -= 1; 418 kmem_cache_free(ntfs_enode_cachep, e3); 419 } 420 if (!bmax) 421 return; 422 n3 = rb_first(&wnd->count_tree); 423 wnd->extent_max = 424 n3 ? rb_entry(n3, struct e_node, count.node)->count.key 425 : 0; 426 return; 427 } 428 429 if (e->count.key != wnd->extent_max) { 430 ; 431 } else if (rb_prev(&e->count.node)) { 432 ; 433 } else { 434 n3 = rb_next(&e->count.node); 435 max_new_len = len > new_len ? len : new_len; 436 if (!n3) { 437 wnd->extent_max = max_new_len; 438 } else { 439 e3 = rb_entry(n3, struct e_node, count.node); 440 wnd->extent_max = max(e3->count.key, max_new_len); 441 } 442 } 443 444 if (!len) { 445 if (new_len) { 446 e->start.key = new_key; 447 rb_erase(&e->count.node, &wnd->count_tree); 448 e->count.key = new_len; 449 rb_insert_count(&wnd->count_tree, e); 450 } else { 451 rb_erase(&e->start.node, &wnd->start_tree); 452 rb_erase(&e->count.node, &wnd->count_tree); 453 wnd->count -= 1; 454 kmem_cache_free(ntfs_enode_cachep, e); 455 } 456 goto out; 457 } 458 rb_erase(&e->count.node, &wnd->count_tree); 459 e->count.key = len; 460 rb_insert_count(&wnd->count_tree, e); 461 462 if (!new_len) 463 goto out; 464 465 if (wnd->count >= NTFS_MAX_WND_EXTENTS) { 466 wnd->uptodated = -1; 467 468 /* Get minimal extent. */ 469 e = rb_entry(rb_last(&wnd->count_tree), struct e_node, 470 count.node); 471 if (e->count.key > new_len) 472 goto out; 473 474 /* Replace minimum. */ 475 rb_erase(&e->start.node, &wnd->start_tree); 476 rb_erase(&e->count.node, &wnd->count_tree); 477 wnd->count -= 1; 478 } else { 479 e = kmem_cache_alloc(ntfs_enode_cachep, GFP_ATOMIC); 480 if (!e) 481 wnd->uptodated = -1; 482 } 483 484 if (e) { 485 e->start.key = new_key; 486 e->count.key = new_len; 487 rb_insert_start(&wnd->start_tree, e); 488 rb_insert_count(&wnd->count_tree, e); 489 wnd->count += 1; 490 } 491 492 out: 493 if (!wnd->count && 1 != wnd->uptodated) 494 wnd_rescan(wnd); 495 } 496 497 /* 498 * wnd_rescan - Scan all bitmap. Used while initialization. 499 */ 500 static int wnd_rescan(struct wnd_bitmap *wnd) 501 { 502 int err = 0; 503 size_t prev_tail = 0; 504 struct super_block *sb = wnd->sb; 505 struct ntfs_sb_info *sbi = sb->s_fs_info; 506 u64 lbo, len = 0; 507 u32 blocksize = sb->s_blocksize; 508 u8 cluster_bits = sbi->cluster_bits; 509 u32 wbits = 8 * sb->s_blocksize; 510 u32 used, frb; 511 const ulong *buf; 512 size_t wpos, wbit, iw, vbo; 513 struct buffer_head *bh = NULL; 514 CLST lcn, clen; 515 516 wnd->uptodated = 0; 517 wnd->extent_max = 0; 518 wnd->extent_min = MINUS_ONE_T; 519 wnd->total_zeroes = 0; 520 521 vbo = 0; 522 523 for (iw = 0; iw < wnd->nwnd; iw++) { 524 if (iw + 1 == wnd->nwnd) 525 wbits = wnd->bits_last; 526 527 if (wnd->inited) { 528 if (!wnd->free_bits[iw]) { 529 /* All ones. */ 530 if (prev_tail) { 531 wnd_add_free_ext(wnd, 532 vbo * 8 - prev_tail, 533 prev_tail, true); 534 prev_tail = 0; 535 } 536 goto next_wnd; 537 } 538 if (wbits == wnd->free_bits[iw]) { 539 /* All zeroes. */ 540 prev_tail += wbits; 541 wnd->total_zeroes += wbits; 542 goto next_wnd; 543 } 544 } 545 546 if (!len) { 547 u32 off = vbo & sbi->cluster_mask; 548 549 if (!run_lookup_entry(&wnd->run, vbo >> cluster_bits, 550 &lcn, &clen, NULL)) { 551 err = -ENOENT; 552 goto out; 553 } 554 555 lbo = ((u64)lcn << cluster_bits) + off; 556 len = ((u64)clen << cluster_bits) - off; 557 } 558 559 bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits); 560 if (!bh) { 561 err = -EIO; 562 goto out; 563 } 564 565 buf = (ulong *)bh->b_data; 566 567 used = __bitmap_weight(buf, wbits); 568 if (used < wbits) { 569 frb = wbits - used; 570 wnd->free_bits[iw] = frb; 571 wnd->total_zeroes += frb; 572 } 573 574 wpos = 0; 575 wbit = vbo * 8; 576 577 if (wbit + wbits > wnd->nbits) 578 wbits = wnd->nbits - wbit; 579 580 do { 581 used = find_next_zero_bit(buf, wbits, wpos); 582 583 if (used > wpos && prev_tail) { 584 wnd_add_free_ext(wnd, wbit + wpos - prev_tail, 585 prev_tail, true); 586 prev_tail = 0; 587 } 588 589 wpos = used; 590 591 if (wpos >= wbits) { 592 /* No free blocks. */ 593 prev_tail = 0; 594 break; 595 } 596 597 frb = find_next_bit(buf, wbits, wpos); 598 if (frb >= wbits) { 599 /* Keep last free block. */ 600 prev_tail += frb - wpos; 601 break; 602 } 603 604 wnd_add_free_ext(wnd, wbit + wpos - prev_tail, 605 frb + prev_tail - wpos, true); 606 607 /* Skip free block and first '1'. */ 608 wpos = frb + 1; 609 /* Reset previous tail. */ 610 prev_tail = 0; 611 } while (wpos < wbits); 612 613 next_wnd: 614 615 if (bh) 616 put_bh(bh); 617 bh = NULL; 618 619 vbo += blocksize; 620 if (len) { 621 len -= blocksize; 622 lbo += blocksize; 623 } 624 } 625 626 /* Add last block. */ 627 if (prev_tail) 628 wnd_add_free_ext(wnd, wnd->nbits - prev_tail, prev_tail, true); 629 630 /* 631 * Before init cycle wnd->uptodated was 0. 632 * If any errors or limits occurs while initialization then 633 * wnd->uptodated will be -1. 634 * If 'uptodated' is still 0 then Tree is really updated. 635 */ 636 if (!wnd->uptodated) 637 wnd->uptodated = 1; 638 639 if (wnd->zone_bit != wnd->zone_end) { 640 size_t zlen = wnd->zone_end - wnd->zone_bit; 641 642 wnd->zone_end = wnd->zone_bit; 643 wnd_zone_set(wnd, wnd->zone_bit, zlen); 644 } 645 646 out: 647 return err; 648 } 649 650 int wnd_init(struct wnd_bitmap *wnd, struct super_block *sb, size_t nbits) 651 { 652 int err; 653 u32 blocksize = sb->s_blocksize; 654 u32 wbits = blocksize * 8; 655 656 init_rwsem(&wnd->rw_lock); 657 658 wnd->sb = sb; 659 wnd->nbits = nbits; 660 wnd->total_zeroes = nbits; 661 wnd->extent_max = MINUS_ONE_T; 662 wnd->zone_bit = wnd->zone_end = 0; 663 wnd->nwnd = bytes_to_block(sb, bitmap_size(nbits)); 664 wnd->bits_last = nbits & (wbits - 1); 665 if (!wnd->bits_last) 666 wnd->bits_last = wbits; 667 668 wnd->free_bits = kcalloc(wnd->nwnd, sizeof(u16), GFP_NOFS); 669 if (!wnd->free_bits) 670 return -ENOMEM; 671 672 err = wnd_rescan(wnd); 673 if (err) 674 return err; 675 676 wnd->inited = true; 677 678 return 0; 679 } 680 681 /* 682 * wnd_map - Call sb_bread for requested window. 683 */ 684 static struct buffer_head *wnd_map(struct wnd_bitmap *wnd, size_t iw) 685 { 686 size_t vbo; 687 CLST lcn, clen; 688 struct super_block *sb = wnd->sb; 689 struct ntfs_sb_info *sbi; 690 struct buffer_head *bh; 691 u64 lbo; 692 693 sbi = sb->s_fs_info; 694 vbo = (u64)iw << sb->s_blocksize_bits; 695 696 if (!run_lookup_entry(&wnd->run, vbo >> sbi->cluster_bits, &lcn, &clen, 697 NULL)) { 698 return ERR_PTR(-ENOENT); 699 } 700 701 lbo = ((u64)lcn << sbi->cluster_bits) + (vbo & sbi->cluster_mask); 702 703 bh = ntfs_bread(wnd->sb, lbo >> sb->s_blocksize_bits); 704 if (!bh) 705 return ERR_PTR(-EIO); 706 707 return bh; 708 } 709 710 /* 711 * wnd_set_free - Mark the bits range from bit to bit + bits as free. 712 */ 713 int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits) 714 { 715 int err = 0; 716 struct super_block *sb = wnd->sb; 717 size_t bits0 = bits; 718 u32 wbits = 8 * sb->s_blocksize; 719 size_t iw = bit >> (sb->s_blocksize_bits + 3); 720 u32 wbit = bit & (wbits - 1); 721 struct buffer_head *bh; 722 723 while (iw < wnd->nwnd && bits) { 724 u32 tail, op; 725 ulong *buf; 726 727 if (iw + 1 == wnd->nwnd) 728 wbits = wnd->bits_last; 729 730 tail = wbits - wbit; 731 op = tail < bits ? tail : bits; 732 733 bh = wnd_map(wnd, iw); 734 if (IS_ERR(bh)) { 735 err = PTR_ERR(bh); 736 break; 737 } 738 739 buf = (ulong *)bh->b_data; 740 741 lock_buffer(bh); 742 743 __bitmap_clear(buf, wbit, op); 744 745 wnd->free_bits[iw] += op; 746 747 set_buffer_uptodate(bh); 748 mark_buffer_dirty(bh); 749 unlock_buffer(bh); 750 put_bh(bh); 751 752 wnd->total_zeroes += op; 753 bits -= op; 754 wbit = 0; 755 iw += 1; 756 } 757 758 wnd_add_free_ext(wnd, bit, bits0, false); 759 760 return err; 761 } 762 763 /* 764 * wnd_set_used - Mark the bits range from bit to bit + bits as used. 765 */ 766 int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits) 767 { 768 int err = 0; 769 struct super_block *sb = wnd->sb; 770 size_t bits0 = bits; 771 size_t iw = bit >> (sb->s_blocksize_bits + 3); 772 u32 wbits = 8 * sb->s_blocksize; 773 u32 wbit = bit & (wbits - 1); 774 struct buffer_head *bh; 775 776 while (iw < wnd->nwnd && bits) { 777 u32 tail, op; 778 ulong *buf; 779 780 if (unlikely(iw + 1 == wnd->nwnd)) 781 wbits = wnd->bits_last; 782 783 tail = wbits - wbit; 784 op = tail < bits ? tail : bits; 785 786 bh = wnd_map(wnd, iw); 787 if (IS_ERR(bh)) { 788 err = PTR_ERR(bh); 789 break; 790 } 791 buf = (ulong *)bh->b_data; 792 793 lock_buffer(bh); 794 795 __bitmap_set(buf, wbit, op); 796 wnd->free_bits[iw] -= op; 797 798 set_buffer_uptodate(bh); 799 mark_buffer_dirty(bh); 800 unlock_buffer(bh); 801 put_bh(bh); 802 803 wnd->total_zeroes -= op; 804 bits -= op; 805 wbit = 0; 806 iw += 1; 807 } 808 809 if (!RB_EMPTY_ROOT(&wnd->start_tree)) 810 wnd_remove_free_ext(wnd, bit, bits0); 811 812 return err; 813 } 814 815 /* 816 * wnd_is_free_hlp 817 * 818 * Return: True if all clusters [bit, bit+bits) are free (bitmap only). 819 */ 820 static bool wnd_is_free_hlp(struct wnd_bitmap *wnd, size_t bit, size_t bits) 821 { 822 struct super_block *sb = wnd->sb; 823 size_t iw = bit >> (sb->s_blocksize_bits + 3); 824 u32 wbits = 8 * sb->s_blocksize; 825 u32 wbit = bit & (wbits - 1); 826 827 while (iw < wnd->nwnd && bits) { 828 u32 tail, op; 829 830 if (unlikely(iw + 1 == wnd->nwnd)) 831 wbits = wnd->bits_last; 832 833 tail = wbits - wbit; 834 op = tail < bits ? tail : bits; 835 836 if (wbits != wnd->free_bits[iw]) { 837 bool ret; 838 struct buffer_head *bh = wnd_map(wnd, iw); 839 840 if (IS_ERR(bh)) 841 return false; 842 843 ret = are_bits_clear((ulong *)bh->b_data, wbit, op); 844 845 put_bh(bh); 846 if (!ret) 847 return false; 848 } 849 850 bits -= op; 851 wbit = 0; 852 iw += 1; 853 } 854 855 return true; 856 } 857 858 /* 859 * wnd_is_free 860 * 861 * Return: True if all clusters [bit, bit+bits) are free. 862 */ 863 bool wnd_is_free(struct wnd_bitmap *wnd, size_t bit, size_t bits) 864 { 865 bool ret; 866 struct rb_node *n; 867 size_t end; 868 struct e_node *e; 869 870 if (RB_EMPTY_ROOT(&wnd->start_tree)) 871 goto use_wnd; 872 873 n = rb_lookup(&wnd->start_tree, bit); 874 if (!n) 875 goto use_wnd; 876 877 e = rb_entry(n, struct e_node, start.node); 878 879 end = e->start.key + e->count.key; 880 881 if (bit < end && bit + bits <= end) 882 return true; 883 884 use_wnd: 885 ret = wnd_is_free_hlp(wnd, bit, bits); 886 887 return ret; 888 } 889 890 /* 891 * wnd_is_used 892 * 893 * Return: True if all clusters [bit, bit+bits) are used. 894 */ 895 bool wnd_is_used(struct wnd_bitmap *wnd, size_t bit, size_t bits) 896 { 897 bool ret = false; 898 struct super_block *sb = wnd->sb; 899 size_t iw = bit >> (sb->s_blocksize_bits + 3); 900 u32 wbits = 8 * sb->s_blocksize; 901 u32 wbit = bit & (wbits - 1); 902 size_t end; 903 struct rb_node *n; 904 struct e_node *e; 905 906 if (RB_EMPTY_ROOT(&wnd->start_tree)) 907 goto use_wnd; 908 909 end = bit + bits; 910 n = rb_lookup(&wnd->start_tree, end - 1); 911 if (!n) 912 goto use_wnd; 913 914 e = rb_entry(n, struct e_node, start.node); 915 if (e->start.key + e->count.key > bit) 916 return false; 917 918 use_wnd: 919 while (iw < wnd->nwnd && bits) { 920 u32 tail, op; 921 922 if (unlikely(iw + 1 == wnd->nwnd)) 923 wbits = wnd->bits_last; 924 925 tail = wbits - wbit; 926 op = tail < bits ? tail : bits; 927 928 if (wnd->free_bits[iw]) { 929 bool ret; 930 struct buffer_head *bh = wnd_map(wnd, iw); 931 932 if (IS_ERR(bh)) 933 goto out; 934 935 ret = are_bits_set((ulong *)bh->b_data, wbit, op); 936 put_bh(bh); 937 if (!ret) 938 goto out; 939 } 940 941 bits -= op; 942 wbit = 0; 943 iw += 1; 944 } 945 ret = true; 946 947 out: 948 return ret; 949 } 950 951 /* 952 * wnd_find - Look for free space. 953 * 954 * - flags - BITMAP_FIND_XXX flags 955 * 956 * Return: 0 if not found. 957 */ 958 size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint, 959 size_t flags, size_t *allocated) 960 { 961 struct super_block *sb; 962 u32 wbits, wpos, wzbit, wzend; 963 size_t fnd, max_alloc, b_len, b_pos; 964 size_t iw, prev_tail, nwnd, wbit, ebit, zbit, zend; 965 size_t to_alloc0 = to_alloc; 966 const ulong *buf; 967 const struct e_node *e; 968 const struct rb_node *pr, *cr; 969 u8 log2_bits; 970 bool fbits_valid; 971 struct buffer_head *bh; 972 973 /* Fast checking for available free space. */ 974 if (flags & BITMAP_FIND_FULL) { 975 size_t zeroes = wnd_zeroes(wnd); 976 977 zeroes -= wnd->zone_end - wnd->zone_bit; 978 if (zeroes < to_alloc0) 979 goto no_space; 980 981 if (to_alloc0 > wnd->extent_max) 982 goto no_space; 983 } else { 984 if (to_alloc > wnd->extent_max) 985 to_alloc = wnd->extent_max; 986 } 987 988 if (wnd->zone_bit <= hint && hint < wnd->zone_end) 989 hint = wnd->zone_end; 990 991 max_alloc = wnd->nbits; 992 b_len = b_pos = 0; 993 994 if (hint >= max_alloc) 995 hint = 0; 996 997 if (RB_EMPTY_ROOT(&wnd->start_tree)) { 998 if (wnd->uptodated == 1) { 999 /* Extents tree is updated -> No free space. */ 1000 goto no_space; 1001 } 1002 goto scan_bitmap; 1003 } 1004 1005 e = NULL; 1006 if (!hint) 1007 goto allocate_biggest; 1008 1009 /* Use hint: Enumerate extents by start >= hint. */ 1010 pr = NULL; 1011 cr = wnd->start_tree.rb_node; 1012 1013 for (;;) { 1014 e = rb_entry(cr, struct e_node, start.node); 1015 1016 if (e->start.key == hint) 1017 break; 1018 1019 if (e->start.key < hint) { 1020 pr = cr; 1021 cr = cr->rb_right; 1022 if (!cr) 1023 break; 1024 continue; 1025 } 1026 1027 cr = cr->rb_left; 1028 if (!cr) { 1029 e = pr ? rb_entry(pr, struct e_node, start.node) : NULL; 1030 break; 1031 } 1032 } 1033 1034 if (!e) 1035 goto allocate_biggest; 1036 1037 if (e->start.key + e->count.key > hint) { 1038 /* We have found extension with 'hint' inside. */ 1039 size_t len = e->start.key + e->count.key - hint; 1040 1041 if (len >= to_alloc && hint + to_alloc <= max_alloc) { 1042 fnd = hint; 1043 goto found; 1044 } 1045 1046 if (!(flags & BITMAP_FIND_FULL)) { 1047 if (len > to_alloc) 1048 len = to_alloc; 1049 1050 if (hint + len <= max_alloc) { 1051 fnd = hint; 1052 to_alloc = len; 1053 goto found; 1054 } 1055 } 1056 } 1057 1058 allocate_biggest: 1059 /* Allocate from biggest free extent. */ 1060 e = rb_entry(rb_first(&wnd->count_tree), struct e_node, count.node); 1061 if (e->count.key != wnd->extent_max) 1062 wnd->extent_max = e->count.key; 1063 1064 if (e->count.key < max_alloc) { 1065 if (e->count.key >= to_alloc) { 1066 ; 1067 } else if (flags & BITMAP_FIND_FULL) { 1068 if (e->count.key < to_alloc0) { 1069 /* Biggest free block is less then requested. */ 1070 goto no_space; 1071 } 1072 to_alloc = e->count.key; 1073 } else if (-1 != wnd->uptodated) { 1074 to_alloc = e->count.key; 1075 } else { 1076 /* Check if we can use more bits. */ 1077 size_t op, max_check; 1078 struct rb_root start_tree; 1079 1080 memcpy(&start_tree, &wnd->start_tree, 1081 sizeof(struct rb_root)); 1082 memset(&wnd->start_tree, 0, sizeof(struct rb_root)); 1083 1084 max_check = e->start.key + to_alloc; 1085 if (max_check > max_alloc) 1086 max_check = max_alloc; 1087 for (op = e->start.key + e->count.key; op < max_check; 1088 op++) { 1089 if (!wnd_is_free(wnd, op, 1)) 1090 break; 1091 } 1092 memcpy(&wnd->start_tree, &start_tree, 1093 sizeof(struct rb_root)); 1094 to_alloc = op - e->start.key; 1095 } 1096 1097 /* Prepare to return. */ 1098 fnd = e->start.key; 1099 if (e->start.key + to_alloc > max_alloc) 1100 to_alloc = max_alloc - e->start.key; 1101 goto found; 1102 } 1103 1104 if (wnd->uptodated == 1) { 1105 /* Extents tree is updated -> no free space. */ 1106 goto no_space; 1107 } 1108 1109 b_len = e->count.key; 1110 b_pos = e->start.key; 1111 1112 scan_bitmap: 1113 sb = wnd->sb; 1114 log2_bits = sb->s_blocksize_bits + 3; 1115 1116 /* At most two ranges [hint, max_alloc) + [0, hint). */ 1117 Again: 1118 1119 /* TODO: Optimize request for case nbits > wbits. */ 1120 iw = hint >> log2_bits; 1121 wbits = sb->s_blocksize * 8; 1122 wpos = hint & (wbits - 1); 1123 prev_tail = 0; 1124 fbits_valid = true; 1125 1126 if (max_alloc == wnd->nbits) { 1127 nwnd = wnd->nwnd; 1128 } else { 1129 size_t t = max_alloc + wbits - 1; 1130 1131 nwnd = likely(t > max_alloc) ? (t >> log2_bits) : wnd->nwnd; 1132 } 1133 1134 /* Enumerate all windows. */ 1135 for (; iw < nwnd; iw++) { 1136 wbit = iw << log2_bits; 1137 1138 if (!wnd->free_bits[iw]) { 1139 if (prev_tail > b_len) { 1140 b_pos = wbit - prev_tail; 1141 b_len = prev_tail; 1142 } 1143 1144 /* Skip full used window. */ 1145 prev_tail = 0; 1146 wpos = 0; 1147 continue; 1148 } 1149 1150 if (unlikely(iw + 1 == nwnd)) { 1151 if (max_alloc == wnd->nbits) { 1152 wbits = wnd->bits_last; 1153 } else { 1154 size_t t = max_alloc & (wbits - 1); 1155 1156 if (t) { 1157 wbits = t; 1158 fbits_valid = false; 1159 } 1160 } 1161 } 1162 1163 if (wnd->zone_end > wnd->zone_bit) { 1164 ebit = wbit + wbits; 1165 zbit = max(wnd->zone_bit, wbit); 1166 zend = min(wnd->zone_end, ebit); 1167 1168 /* Here we have a window [wbit, ebit) and zone [zbit, zend). */ 1169 if (zend <= zbit) { 1170 /* Zone does not overlap window. */ 1171 } else { 1172 wzbit = zbit - wbit; 1173 wzend = zend - wbit; 1174 1175 /* Zone overlaps window. */ 1176 if (wnd->free_bits[iw] == wzend - wzbit) { 1177 prev_tail = 0; 1178 wpos = 0; 1179 continue; 1180 } 1181 1182 /* Scan two ranges window: [wbit, zbit) and [zend, ebit). */ 1183 bh = wnd_map(wnd, iw); 1184 1185 if (IS_ERR(bh)) { 1186 /* TODO: Error */ 1187 prev_tail = 0; 1188 wpos = 0; 1189 continue; 1190 } 1191 1192 buf = (ulong *)bh->b_data; 1193 1194 /* Scan range [wbit, zbit). */ 1195 if (wpos < wzbit) { 1196 /* Scan range [wpos, zbit). */ 1197 fnd = wnd_scan(buf, wbit, wpos, wzbit, 1198 to_alloc, &prev_tail, 1199 &b_pos, &b_len); 1200 if (fnd != MINUS_ONE_T) { 1201 put_bh(bh); 1202 goto found; 1203 } 1204 } 1205 1206 prev_tail = 0; 1207 1208 /* Scan range [zend, ebit). */ 1209 if (wzend < wbits) { 1210 fnd = wnd_scan(buf, wbit, 1211 max(wzend, wpos), wbits, 1212 to_alloc, &prev_tail, 1213 &b_pos, &b_len); 1214 if (fnd != MINUS_ONE_T) { 1215 put_bh(bh); 1216 goto found; 1217 } 1218 } 1219 1220 wpos = 0; 1221 put_bh(bh); 1222 continue; 1223 } 1224 } 1225 1226 /* Current window does not overlap zone. */ 1227 if (!wpos && fbits_valid && wnd->free_bits[iw] == wbits) { 1228 /* Window is empty. */ 1229 if (prev_tail + wbits >= to_alloc) { 1230 fnd = wbit + wpos - prev_tail; 1231 goto found; 1232 } 1233 1234 /* Increase 'prev_tail' and process next window. */ 1235 prev_tail += wbits; 1236 wpos = 0; 1237 continue; 1238 } 1239 1240 /* Read window. */ 1241 bh = wnd_map(wnd, iw); 1242 if (IS_ERR(bh)) { 1243 // TODO: Error. 1244 prev_tail = 0; 1245 wpos = 0; 1246 continue; 1247 } 1248 1249 buf = (ulong *)bh->b_data; 1250 1251 /* Scan range [wpos, eBits). */ 1252 fnd = wnd_scan(buf, wbit, wpos, wbits, to_alloc, &prev_tail, 1253 &b_pos, &b_len); 1254 put_bh(bh); 1255 if (fnd != MINUS_ONE_T) 1256 goto found; 1257 } 1258 1259 if (b_len < prev_tail) { 1260 /* The last fragment. */ 1261 b_len = prev_tail; 1262 b_pos = max_alloc - prev_tail; 1263 } 1264 1265 if (hint) { 1266 /* 1267 * We have scanned range [hint max_alloc). 1268 * Prepare to scan range [0 hint + to_alloc). 1269 */ 1270 size_t nextmax = hint + to_alloc; 1271 1272 if (likely(nextmax >= hint) && nextmax < max_alloc) 1273 max_alloc = nextmax; 1274 hint = 0; 1275 goto Again; 1276 } 1277 1278 if (!b_len) 1279 goto no_space; 1280 1281 wnd->extent_max = b_len; 1282 1283 if (flags & BITMAP_FIND_FULL) 1284 goto no_space; 1285 1286 fnd = b_pos; 1287 to_alloc = b_len; 1288 1289 found: 1290 if (flags & BITMAP_FIND_MARK_AS_USED) { 1291 /* TODO: Optimize remove extent (pass 'e'?). */ 1292 if (wnd_set_used(wnd, fnd, to_alloc)) 1293 goto no_space; 1294 } else if (wnd->extent_max != MINUS_ONE_T && 1295 to_alloc > wnd->extent_max) { 1296 wnd->extent_max = to_alloc; 1297 } 1298 1299 *allocated = fnd; 1300 return to_alloc; 1301 1302 no_space: 1303 return 0; 1304 } 1305 1306 /* 1307 * wnd_extend - Extend bitmap ($MFT bitmap). 1308 */ 1309 int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits) 1310 { 1311 int err; 1312 struct super_block *sb = wnd->sb; 1313 struct ntfs_sb_info *sbi = sb->s_fs_info; 1314 u32 blocksize = sb->s_blocksize; 1315 u32 wbits = blocksize * 8; 1316 u32 b0, new_last; 1317 size_t bits, iw, new_wnd; 1318 size_t old_bits = wnd->nbits; 1319 u16 *new_free; 1320 1321 if (new_bits <= old_bits) 1322 return -EINVAL; 1323 1324 /* Align to 8 byte boundary. */ 1325 new_wnd = bytes_to_block(sb, bitmap_size(new_bits)); 1326 new_last = new_bits & (wbits - 1); 1327 if (!new_last) 1328 new_last = wbits; 1329 1330 if (new_wnd != wnd->nwnd) { 1331 new_free = kmalloc(new_wnd * sizeof(u16), GFP_NOFS); 1332 if (!new_free) 1333 return -ENOMEM; 1334 1335 if (new_free != wnd->free_bits) 1336 memcpy(new_free, wnd->free_bits, 1337 wnd->nwnd * sizeof(short)); 1338 memset(new_free + wnd->nwnd, 0, 1339 (new_wnd - wnd->nwnd) * sizeof(short)); 1340 kfree(wnd->free_bits); 1341 wnd->free_bits = new_free; 1342 } 1343 1344 /* Zero bits [old_bits,new_bits). */ 1345 bits = new_bits - old_bits; 1346 b0 = old_bits & (wbits - 1); 1347 1348 for (iw = old_bits >> (sb->s_blocksize_bits + 3); bits; iw += 1) { 1349 u32 op; 1350 size_t frb; 1351 u64 vbo, lbo, bytes; 1352 struct buffer_head *bh; 1353 ulong *buf; 1354 1355 if (iw + 1 == new_wnd) 1356 wbits = new_last; 1357 1358 op = b0 + bits > wbits ? wbits - b0 : bits; 1359 vbo = (u64)iw * blocksize; 1360 1361 err = ntfs_vbo_to_lbo(sbi, &wnd->run, vbo, &lbo, &bytes); 1362 if (err) 1363 break; 1364 1365 bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits); 1366 if (!bh) 1367 return -EIO; 1368 1369 lock_buffer(bh); 1370 buf = (ulong *)bh->b_data; 1371 1372 __bitmap_clear(buf, b0, blocksize * 8 - b0); 1373 frb = wbits - __bitmap_weight(buf, wbits); 1374 wnd->total_zeroes += frb - wnd->free_bits[iw]; 1375 wnd->free_bits[iw] = frb; 1376 1377 set_buffer_uptodate(bh); 1378 mark_buffer_dirty(bh); 1379 unlock_buffer(bh); 1380 /* err = sync_dirty_buffer(bh); */ 1381 1382 b0 = 0; 1383 bits -= op; 1384 } 1385 1386 wnd->nbits = new_bits; 1387 wnd->nwnd = new_wnd; 1388 wnd->bits_last = new_last; 1389 1390 wnd_add_free_ext(wnd, old_bits, new_bits - old_bits, false); 1391 1392 return 0; 1393 } 1394 1395 void wnd_zone_set(struct wnd_bitmap *wnd, size_t lcn, size_t len) 1396 { 1397 size_t zlen; 1398 1399 zlen = wnd->zone_end - wnd->zone_bit; 1400 if (zlen) 1401 wnd_add_free_ext(wnd, wnd->zone_bit, zlen, false); 1402 1403 if (!RB_EMPTY_ROOT(&wnd->start_tree) && len) 1404 wnd_remove_free_ext(wnd, lcn, len); 1405 1406 wnd->zone_bit = lcn; 1407 wnd->zone_end = lcn + len; 1408 } 1409 1410 int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range) 1411 { 1412 int err = 0; 1413 struct super_block *sb = sbi->sb; 1414 struct wnd_bitmap *wnd = &sbi->used.bitmap; 1415 u32 wbits = 8 * sb->s_blocksize; 1416 CLST len = 0, lcn = 0, done = 0; 1417 CLST minlen = bytes_to_cluster(sbi, range->minlen); 1418 CLST lcn_from = bytes_to_cluster(sbi, range->start); 1419 size_t iw = lcn_from >> (sb->s_blocksize_bits + 3); 1420 u32 wbit = lcn_from & (wbits - 1); 1421 const ulong *buf; 1422 CLST lcn_to; 1423 1424 if (!minlen) 1425 minlen = 1; 1426 1427 if (range->len == (u64)-1) 1428 lcn_to = wnd->nbits; 1429 else 1430 lcn_to = bytes_to_cluster(sbi, range->start + range->len); 1431 1432 down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS); 1433 1434 for (; iw < wnd->nbits; iw++, wbit = 0) { 1435 CLST lcn_wnd = iw * wbits; 1436 struct buffer_head *bh; 1437 1438 if (lcn_wnd > lcn_to) 1439 break; 1440 1441 if (!wnd->free_bits[iw]) 1442 continue; 1443 1444 if (iw + 1 == wnd->nwnd) 1445 wbits = wnd->bits_last; 1446 1447 if (lcn_wnd + wbits > lcn_to) 1448 wbits = lcn_to - lcn_wnd; 1449 1450 bh = wnd_map(wnd, iw); 1451 if (IS_ERR(bh)) { 1452 err = PTR_ERR(bh); 1453 break; 1454 } 1455 1456 buf = (ulong *)bh->b_data; 1457 1458 for (; wbit < wbits; wbit++) { 1459 if (!test_bit(wbit, buf)) { 1460 if (!len) 1461 lcn = lcn_wnd + wbit; 1462 len += 1; 1463 continue; 1464 } 1465 if (len >= minlen) { 1466 err = ntfs_discard(sbi, lcn, len); 1467 if (err) 1468 goto out; 1469 done += len; 1470 } 1471 len = 0; 1472 } 1473 put_bh(bh); 1474 } 1475 1476 /* Process the last fragment. */ 1477 if (len >= minlen) { 1478 err = ntfs_discard(sbi, lcn, len); 1479 if (err) 1480 goto out; 1481 done += len; 1482 } 1483 1484 out: 1485 range->len = (u64)done << sbi->cluster_bits; 1486 1487 up_read(&wnd->rw_lock); 1488 1489 return err; 1490 } 1491