1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
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.
13 #include <linux/buffer_head.h>
15 #include <linux/kernel.h>
21 * Maximum number of extents in tree.
23 #define NTFS_MAX_WND_EXTENTS (32u * 1024u)
31 struct rb_node_key start; /* Tree sorted by start. */
32 struct rb_node_key count; /* Tree sorted by len. */
35 static int wnd_rescan(struct wnd_bitmap *wnd);
36 static struct buffer_head *wnd_map(struct wnd_bitmap *wnd, size_t iw);
37 static bool wnd_is_free_hlp(struct wnd_bitmap *wnd, size_t bit, size_t bits);
39 static struct kmem_cache *ntfs_enode_cachep;
41 int __init ntfs3_init_bitmap(void)
43 ntfs_enode_cachep = kmem_cache_create("ntfs3_enode_cache",
44 sizeof(struct e_node), 0,
45 SLAB_RECLAIM_ACCOUNT, NULL);
46 return ntfs_enode_cachep ? 0 : -ENOMEM;
49 void ntfs3_exit_bitmap(void)
51 kmem_cache_destroy(ntfs_enode_cachep);
57 * b_pos + b_len - biggest fragment.
58 * Scan range [wpos wbits) window @buf.
60 * Return: -1 if not found.
62 static size_t wnd_scan(const void *buf, size_t wbit, u32 wpos, u32 wend,
63 size_t to_alloc, size_t *prev_tail, size_t *b_pos,
69 u32 used = find_next_zero_bit_le(buf, wend, wpos);
72 if (*b_len < *prev_tail) {
73 *b_pos = wbit - *prev_tail;
83 if (*b_len < *prev_tail) {
84 *b_pos = wbit - *prev_tail;
92 * Now we have a fragment [wpos, wend) staring with 0.
94 end = wpos + to_alloc - *prev_tail;
95 free_bits = find_next_bit_le(buf, min(end, wend), wpos);
97 free_len = *prev_tail + free_bits - wpos;
99 if (*b_len < free_len) {
100 *b_pos = wbit + wpos - *prev_tail;
104 if (free_len >= to_alloc)
105 return wbit + wpos - *prev_tail;
107 if (free_bits >= wend) {
108 *prev_tail += free_bits - wpos;
112 wpos = free_bits + 1;
121 * wnd_close - Frees all resources.
123 void wnd_close(struct wnd_bitmap *wnd)
125 struct rb_node *node, *next;
127 kfree(wnd->free_bits);
128 wnd->free_bits = NULL;
129 run_close(&wnd->run);
131 node = rb_first(&wnd->start_tree);
134 next = rb_next(node);
135 rb_erase(node, &wnd->start_tree);
136 kmem_cache_free(ntfs_enode_cachep,
137 rb_entry(node, struct e_node, start.node));
142 static struct rb_node *rb_lookup(struct rb_root *root, size_t v)
144 struct rb_node **p = &root->rb_node;
145 struct rb_node *r = NULL;
148 struct rb_node_key *k;
150 k = rb_entry(*p, struct rb_node_key, node);
153 } else if (v > k->key) {
165 * rb_insert_count - Helper function to insert special kind of 'count' tree.
167 static inline bool rb_insert_count(struct rb_root *root, struct e_node *e)
169 struct rb_node **p = &root->rb_node;
170 struct rb_node *parent = NULL;
171 size_t e_ckey = e->count.key;
172 size_t e_skey = e->start.key;
176 rb_entry(parent = *p, struct e_node, count.node);
178 if (e_ckey > k->count.key) {
180 } else if (e_ckey < k->count.key) {
182 } else if (e_skey < k->start.key) {
184 } else if (e_skey > k->start.key) {
192 rb_link_node(&e->count.node, parent, p);
193 rb_insert_color(&e->count.node, root);
198 * rb_insert_start - Helper function to insert special kind of 'count' tree.
200 static inline bool rb_insert_start(struct rb_root *root, struct e_node *e)
202 struct rb_node **p = &root->rb_node;
203 struct rb_node *parent = NULL;
204 size_t e_skey = e->start.key;
211 k = rb_entry(parent, struct e_node, start.node);
212 if (e_skey < k->start.key) {
214 } else if (e_skey > k->start.key) {
222 rb_link_node(&e->start.node, parent, p);
223 rb_insert_color(&e->start.node, root);
228 * wnd_add_free_ext - Adds a new extent of free space.
229 * @build: 1 when building tree.
231 static void wnd_add_free_ext(struct wnd_bitmap *wnd, size_t bit, size_t len,
234 struct e_node *e, *e0 = NULL;
235 size_t ib, end_in = bit + len;
239 /* Use extent_min to filter too short extents. */
240 if (wnd->count >= NTFS_MAX_WND_EXTENTS &&
241 len <= wnd->extent_min) {
246 /* Try to find extent before 'bit'. */
247 n = rb_lookup(&wnd->start_tree, bit);
250 n = rb_first(&wnd->start_tree);
252 e = rb_entry(n, struct e_node, start.node);
254 if (e->start.key + e->count.key == bit) {
258 rb_erase(&e->start.node, &wnd->start_tree);
259 rb_erase(&e->count.node, &wnd->count_tree);
268 e = rb_entry(n, struct e_node, start.node);
269 next_end = e->start.key + e->count.key;
270 if (e->start.key > end_in)
275 len += next_end - end_in;
277 rb_erase(&e->start.node, &wnd->start_tree);
278 rb_erase(&e->count.node, &wnd->count_tree);
284 kmem_cache_free(ntfs_enode_cachep, e);
287 if (wnd->uptodated != 1) {
288 /* Check bits before 'bit'. */
289 ib = wnd->zone_bit == wnd->zone_end ||
290 bit < wnd->zone_end ?
294 while (bit > ib && wnd_is_free_hlp(wnd, bit - 1, 1)) {
299 /* Check bits after 'end_in'. */
300 ib = wnd->zone_bit == wnd->zone_end ||
301 end_in > wnd->zone_bit ?
305 while (end_in < ib && wnd_is_free_hlp(wnd, end_in, 1)) {
311 /* Insert new fragment. */
312 if (wnd->count >= NTFS_MAX_WND_EXTENTS) {
314 kmem_cache_free(ntfs_enode_cachep, e0);
318 /* Compare with smallest fragment. */
319 n = rb_last(&wnd->count_tree);
320 e = rb_entry(n, struct e_node, count.node);
321 if (len <= e->count.key)
322 goto out; /* Do not insert small fragments. */
328 e2 = rb_entry(n, struct e_node, count.node);
329 /* Smallest fragment will be 'e2->count.key'. */
330 wnd->extent_min = e2->count.key;
333 /* Replace smallest fragment by new one. */
334 rb_erase(&e->start.node, &wnd->start_tree);
335 rb_erase(&e->count.node, &wnd->count_tree);
338 e = e0 ? e0 : kmem_cache_alloc(ntfs_enode_cachep, GFP_ATOMIC);
344 if (build && len <= wnd->extent_min)
345 wnd->extent_min = len;
349 if (len > wnd->extent_max)
350 wnd->extent_max = len;
352 rb_insert_start(&wnd->start_tree, e);
353 rb_insert_count(&wnd->count_tree, e);
360 * wnd_remove_free_ext - Remove a run from the cached free space.
362 static void wnd_remove_free_ext(struct wnd_bitmap *wnd, size_t bit, size_t len)
364 struct rb_node *n, *n3;
365 struct e_node *e, *e3;
366 size_t end_in = bit + len;
367 size_t end3, end, new_key, new_len, max_new_len;
369 /* Try to find extent before 'bit'. */
370 n = rb_lookup(&wnd->start_tree, bit);
375 e = rb_entry(n, struct e_node, start.node);
376 end = e->start.key + e->count.key;
378 new_key = new_len = 0;
381 /* Range [bit,end_in) must be inside 'e' or outside 'e' and 'n'. */
382 if (e->start.key > bit)
384 else if (end_in <= end) {
385 /* Range [bit,end_in) inside 'e'. */
387 new_len = end - end_in;
388 len = bit - e->start.key;
389 } else if (bit > end) {
395 e3 = rb_entry(n3, struct e_node, start.node);
396 if (e3->start.key >= end_in)
399 if (e3->count.key == wnd->extent_max)
402 end3 = e3->start.key + e3->count.key;
404 e3->start.key = end_in;
405 rb_erase(&e3->count.node, &wnd->count_tree);
406 e3->count.key = end3 - end_in;
407 rb_insert_count(&wnd->count_tree, e3);
412 rb_erase(&e3->start.node, &wnd->start_tree);
413 rb_erase(&e3->count.node, &wnd->count_tree);
415 kmem_cache_free(ntfs_enode_cachep, e3);
419 n3 = rb_first(&wnd->count_tree);
421 n3 ? rb_entry(n3, struct e_node, count.node)->count.key :
426 if (e->count.key != wnd->extent_max) {
428 } else if (rb_prev(&e->count.node)) {
431 n3 = rb_next(&e->count.node);
432 max_new_len = max(len, new_len);
434 wnd->extent_max = max_new_len;
436 e3 = rb_entry(n3, struct e_node, count.node);
437 wnd->extent_max = max(e3->count.key, max_new_len);
443 e->start.key = new_key;
444 rb_erase(&e->count.node, &wnd->count_tree);
445 e->count.key = new_len;
446 rb_insert_count(&wnd->count_tree, e);
448 rb_erase(&e->start.node, &wnd->start_tree);
449 rb_erase(&e->count.node, &wnd->count_tree);
451 kmem_cache_free(ntfs_enode_cachep, e);
455 rb_erase(&e->count.node, &wnd->count_tree);
457 rb_insert_count(&wnd->count_tree, e);
462 if (wnd->count >= NTFS_MAX_WND_EXTENTS) {
465 /* Get minimal extent. */
466 e = rb_entry(rb_last(&wnd->count_tree), struct e_node,
468 if (e->count.key > new_len)
471 /* Replace minimum. */
472 rb_erase(&e->start.node, &wnd->start_tree);
473 rb_erase(&e->count.node, &wnd->count_tree);
476 e = kmem_cache_alloc(ntfs_enode_cachep, GFP_ATOMIC);
482 e->start.key = new_key;
483 e->count.key = new_len;
484 rb_insert_start(&wnd->start_tree, e);
485 rb_insert_count(&wnd->count_tree, e);
490 if (!wnd->count && 1 != wnd->uptodated)
495 * wnd_rescan - Scan all bitmap. Used while initialization.
497 static int wnd_rescan(struct wnd_bitmap *wnd)
500 size_t prev_tail = 0;
501 struct super_block *sb = wnd->sb;
502 struct ntfs_sb_info *sbi = sb->s_fs_info;
504 u32 blocksize = sb->s_blocksize;
505 u8 cluster_bits = sbi->cluster_bits;
506 u32 wbits = 8 * sb->s_blocksize;
508 size_t wpos, wbit, iw, vbo;
509 struct buffer_head *bh = NULL;
514 wnd->extent_min = MINUS_ONE_T;
515 wnd->total_zeroes = 0;
519 for (iw = 0; iw < wnd->nwnd; iw++) {
520 if (iw + 1 == wnd->nwnd)
521 wbits = wnd->bits_last;
524 if (!wnd->free_bits[iw]) {
527 wnd_add_free_ext(wnd,
534 if (wbits == wnd->free_bits[iw]) {
537 wnd->total_zeroes += wbits;
543 u32 off = vbo & sbi->cluster_mask;
545 if (!run_lookup_entry(&wnd->run, vbo >> cluster_bits,
546 &lcn, &clen, NULL)) {
551 lbo = ((u64)lcn << cluster_bits) + off;
552 len = ((u64)clen << cluster_bits) - off;
555 bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
561 used = ntfs_bitmap_weight_le(bh->b_data, wbits);
564 wnd->free_bits[iw] = frb;
565 wnd->total_zeroes += frb;
571 if (wbit + wbits > wnd->nbits)
572 wbits = wnd->nbits - wbit;
575 used = find_next_zero_bit_le(bh->b_data, wbits, wpos);
577 if (used > wpos && prev_tail) {
578 wnd_add_free_ext(wnd, wbit + wpos - prev_tail,
586 /* No free blocks. */
591 frb = find_next_bit_le(bh->b_data, wbits, wpos);
593 /* Keep last free block. */
594 prev_tail += frb - wpos;
598 wnd_add_free_ext(wnd, wbit + wpos - prev_tail,
599 frb + prev_tail - wpos, true);
601 /* Skip free block and first '1'. */
603 /* Reset previous tail. */
605 } while (wpos < wbits);
620 /* Add last block. */
622 wnd_add_free_ext(wnd, wnd->nbits - prev_tail, prev_tail, true);
625 * Before init cycle wnd->uptodated was 0.
626 * If any errors or limits occurs while initialization then
627 * wnd->uptodated will be -1.
628 * If 'uptodated' is still 0 then Tree is really updated.
633 if (wnd->zone_bit != wnd->zone_end) {
634 size_t zlen = wnd->zone_end - wnd->zone_bit;
636 wnd->zone_end = wnd->zone_bit;
637 wnd_zone_set(wnd, wnd->zone_bit, zlen);
644 int wnd_init(struct wnd_bitmap *wnd, struct super_block *sb, size_t nbits)
647 u32 blocksize = sb->s_blocksize;
648 u32 wbits = blocksize * 8;
650 init_rwsem(&wnd->rw_lock);
654 wnd->total_zeroes = nbits;
655 wnd->extent_max = MINUS_ONE_T;
656 wnd->zone_bit = wnd->zone_end = 0;
657 wnd->nwnd = bytes_to_block(sb, bitmap_size(nbits));
658 wnd->bits_last = nbits & (wbits - 1);
660 wnd->bits_last = wbits;
663 kvmalloc_array(wnd->nwnd, sizeof(u16), GFP_KERNEL | __GFP_ZERO);
668 err = wnd_rescan(wnd);
678 * wnd_map - Call sb_bread for requested window.
680 static struct buffer_head *wnd_map(struct wnd_bitmap *wnd, size_t iw)
684 struct super_block *sb = wnd->sb;
685 struct ntfs_sb_info *sbi;
686 struct buffer_head *bh;
690 vbo = (u64)iw << sb->s_blocksize_bits;
692 if (!run_lookup_entry(&wnd->run, vbo >> sbi->cluster_bits, &lcn, &clen,
694 return ERR_PTR(-ENOENT);
697 lbo = ((u64)lcn << sbi->cluster_bits) + (vbo & sbi->cluster_mask);
699 bh = ntfs_bread(wnd->sb, lbo >> sb->s_blocksize_bits);
701 return ERR_PTR(-EIO);
707 * wnd_set_free - Mark the bits range from bit to bit + bits as free.
709 int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits)
712 struct super_block *sb = wnd->sb;
714 u32 wbits = 8 * sb->s_blocksize;
715 size_t iw = bit >> (sb->s_blocksize_bits + 3);
716 u32 wbit = bit & (wbits - 1);
717 struct buffer_head *bh;
719 while (iw < wnd->nwnd && bits) {
722 if (iw + 1 == wnd->nwnd)
723 wbits = wnd->bits_last;
726 op = min_t(u32, tail, bits);
728 bh = wnd_map(wnd, iw);
736 ntfs_bitmap_clear_le(bh->b_data, wbit, op);
738 wnd->free_bits[iw] += op;
740 set_buffer_uptodate(bh);
741 mark_buffer_dirty(bh);
745 wnd->total_zeroes += op;
751 wnd_add_free_ext(wnd, bit, bits0, false);
757 * wnd_set_used - Mark the bits range from bit to bit + bits as used.
759 int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits)
762 struct super_block *sb = wnd->sb;
764 size_t iw = bit >> (sb->s_blocksize_bits + 3);
765 u32 wbits = 8 * sb->s_blocksize;
766 u32 wbit = bit & (wbits - 1);
767 struct buffer_head *bh;
769 while (iw < wnd->nwnd && bits) {
772 if (unlikely(iw + 1 == wnd->nwnd))
773 wbits = wnd->bits_last;
776 op = min_t(u32, tail, bits);
778 bh = wnd_map(wnd, iw);
786 ntfs_bitmap_set_le(bh->b_data, wbit, op);
787 wnd->free_bits[iw] -= op;
789 set_buffer_uptodate(bh);
790 mark_buffer_dirty(bh);
794 wnd->total_zeroes -= op;
800 if (!RB_EMPTY_ROOT(&wnd->start_tree))
801 wnd_remove_free_ext(wnd, bit, bits0);
807 * wnd_set_used_safe - Mark the bits range from bit to bit + bits as used.
809 * Unlikely wnd_set_used/wnd_set_free this function is not full trusted.
810 * It scans every bit in bitmap and marks free bit as used.
811 * @done - how many bits were marked as used.
813 * NOTE: normally *done should be 0.
815 int wnd_set_used_safe(struct wnd_bitmap *wnd, size_t bit, size_t bits,
818 size_t i, from = 0, len = 0;
822 for (i = 0; i < bits; i++) {
823 if (wnd_is_free(wnd, bit + i, 1)) {
828 err = wnd_set_used(wnd, from, len);
838 err = wnd_set_used(wnd, from, len);
847 * Return: True if all clusters [bit, bit+bits) are free (bitmap only).
849 static bool wnd_is_free_hlp(struct wnd_bitmap *wnd, size_t bit, size_t bits)
851 struct super_block *sb = wnd->sb;
852 size_t iw = bit >> (sb->s_blocksize_bits + 3);
853 u32 wbits = 8 * sb->s_blocksize;
854 u32 wbit = bit & (wbits - 1);
856 while (iw < wnd->nwnd && bits) {
859 if (unlikely(iw + 1 == wnd->nwnd))
860 wbits = wnd->bits_last;
863 op = min_t(u32, tail, bits);
865 if (wbits != wnd->free_bits[iw]) {
867 struct buffer_head *bh = wnd_map(wnd, iw);
872 ret = are_bits_clear(bh->b_data, wbit, op);
890 * Return: True if all clusters [bit, bit+bits) are free.
892 bool wnd_is_free(struct wnd_bitmap *wnd, size_t bit, size_t bits)
899 if (RB_EMPTY_ROOT(&wnd->start_tree))
902 n = rb_lookup(&wnd->start_tree, bit);
906 e = rb_entry(n, struct e_node, start.node);
908 end = e->start.key + e->count.key;
910 if (bit < end && bit + bits <= end)
914 ret = wnd_is_free_hlp(wnd, bit, bits);
922 * Return: True if all clusters [bit, bit+bits) are used.
924 bool wnd_is_used(struct wnd_bitmap *wnd, size_t bit, size_t bits)
927 struct super_block *sb = wnd->sb;
928 size_t iw = bit >> (sb->s_blocksize_bits + 3);
929 u32 wbits = 8 * sb->s_blocksize;
930 u32 wbit = bit & (wbits - 1);
935 if (RB_EMPTY_ROOT(&wnd->start_tree))
939 n = rb_lookup(&wnd->start_tree, end - 1);
943 e = rb_entry(n, struct e_node, start.node);
944 if (e->start.key + e->count.key > bit)
948 while (iw < wnd->nwnd && bits) {
951 if (unlikely(iw + 1 == wnd->nwnd))
952 wbits = wnd->bits_last;
955 op = min_t(u32, tail, bits);
957 if (wnd->free_bits[iw]) {
959 struct buffer_head *bh = wnd_map(wnd, iw);
964 ret = are_bits_set(bh->b_data, wbit, op);
981 * wnd_find - Look for free space.
983 * - flags - BITMAP_FIND_XXX flags
985 * Return: 0 if not found.
987 size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
988 size_t flags, size_t *allocated)
990 struct super_block *sb;
991 u32 wbits, wpos, wzbit, wzend;
992 size_t fnd, max_alloc, b_len, b_pos;
993 size_t iw, prev_tail, nwnd, wbit, ebit, zbit, zend;
994 size_t to_alloc0 = to_alloc;
995 const struct e_node *e;
996 const struct rb_node *pr, *cr;
999 struct buffer_head *bh;
1001 /* Fast checking for available free space. */
1002 if (flags & BITMAP_FIND_FULL) {
1003 size_t zeroes = wnd_zeroes(wnd);
1005 zeroes -= wnd->zone_end - wnd->zone_bit;
1006 if (zeroes < to_alloc0)
1009 if (to_alloc0 > wnd->extent_max)
1012 if (to_alloc > wnd->extent_max)
1013 to_alloc = wnd->extent_max;
1016 if (wnd->zone_bit <= hint && hint < wnd->zone_end)
1017 hint = wnd->zone_end;
1019 max_alloc = wnd->nbits;
1022 if (hint >= max_alloc)
1025 if (RB_EMPTY_ROOT(&wnd->start_tree)) {
1026 if (wnd->uptodated == 1) {
1027 /* Extents tree is updated -> No free space. */
1035 goto allocate_biggest;
1037 /* Use hint: Enumerate extents by start >= hint. */
1039 cr = wnd->start_tree.rb_node;
1042 e = rb_entry(cr, struct e_node, start.node);
1044 if (e->start.key == hint)
1047 if (e->start.key < hint) {
1057 e = pr ? rb_entry(pr, struct e_node, start.node) : NULL;
1063 goto allocate_biggest;
1065 if (e->start.key + e->count.key > hint) {
1066 /* We have found extension with 'hint' inside. */
1067 size_t len = e->start.key + e->count.key - hint;
1069 if (len >= to_alloc && hint + to_alloc <= max_alloc) {
1074 if (!(flags & BITMAP_FIND_FULL)) {
1078 if (hint + len <= max_alloc) {
1087 /* Allocate from biggest free extent. */
1088 e = rb_entry(rb_first(&wnd->count_tree), struct e_node, count.node);
1089 if (e->count.key != wnd->extent_max)
1090 wnd->extent_max = e->count.key;
1092 if (e->count.key < max_alloc) {
1093 if (e->count.key >= to_alloc) {
1095 } else if (flags & BITMAP_FIND_FULL) {
1096 if (e->count.key < to_alloc0) {
1097 /* Biggest free block is less then requested. */
1100 to_alloc = e->count.key;
1101 } else if (-1 != wnd->uptodated) {
1102 to_alloc = e->count.key;
1104 /* Check if we can use more bits. */
1105 size_t op, max_check;
1106 struct rb_root start_tree;
1108 memcpy(&start_tree, &wnd->start_tree,
1109 sizeof(struct rb_root));
1110 memset(&wnd->start_tree, 0, sizeof(struct rb_root));
1112 max_check = e->start.key + to_alloc;
1113 if (max_check > max_alloc)
1114 max_check = max_alloc;
1115 for (op = e->start.key + e->count.key; op < max_check;
1117 if (!wnd_is_free(wnd, op, 1))
1120 memcpy(&wnd->start_tree, &start_tree,
1121 sizeof(struct rb_root));
1122 to_alloc = op - e->start.key;
1125 /* Prepare to return. */
1127 if (e->start.key + to_alloc > max_alloc)
1128 to_alloc = max_alloc - e->start.key;
1132 if (wnd->uptodated == 1) {
1133 /* Extents tree is updated -> no free space. */
1137 b_len = e->count.key;
1138 b_pos = e->start.key;
1142 log2_bits = sb->s_blocksize_bits + 3;
1144 /* At most two ranges [hint, max_alloc) + [0, hint). */
1147 /* TODO: Optimize request for case nbits > wbits. */
1148 iw = hint >> log2_bits;
1149 wbits = sb->s_blocksize * 8;
1150 wpos = hint & (wbits - 1);
1154 if (max_alloc == wnd->nbits) {
1157 size_t t = max_alloc + wbits - 1;
1159 nwnd = likely(t > max_alloc) ? (t >> log2_bits) : wnd->nwnd;
1162 /* Enumerate all windows. */
1163 for (; iw < nwnd; iw++) {
1164 wbit = iw << log2_bits;
1166 if (!wnd->free_bits[iw]) {
1167 if (prev_tail > b_len) {
1168 b_pos = wbit - prev_tail;
1172 /* Skip full used window. */
1178 if (unlikely(iw + 1 == nwnd)) {
1179 if (max_alloc == wnd->nbits) {
1180 wbits = wnd->bits_last;
1182 size_t t = max_alloc & (wbits - 1);
1186 fbits_valid = false;
1191 if (wnd->zone_end > wnd->zone_bit) {
1192 ebit = wbit + wbits;
1193 zbit = max(wnd->zone_bit, wbit);
1194 zend = min(wnd->zone_end, ebit);
1196 /* Here we have a window [wbit, ebit) and zone [zbit, zend). */
1198 /* Zone does not overlap window. */
1200 wzbit = zbit - wbit;
1201 wzend = zend - wbit;
1203 /* Zone overlaps window. */
1204 if (wnd->free_bits[iw] == wzend - wzbit) {
1210 /* Scan two ranges window: [wbit, zbit) and [zend, ebit). */
1211 bh = wnd_map(wnd, iw);
1220 /* Scan range [wbit, zbit). */
1222 /* Scan range [wpos, zbit). */
1223 fnd = wnd_scan(bh->b_data, wbit, wpos,
1227 if (fnd != MINUS_ONE_T) {
1235 /* Scan range [zend, ebit). */
1236 if (wzend < wbits) {
1237 fnd = wnd_scan(bh->b_data, wbit,
1238 max(wzend, wpos), wbits,
1239 to_alloc, &prev_tail,
1241 if (fnd != MINUS_ONE_T) {
1253 /* Current window does not overlap zone. */
1254 if (!wpos && fbits_valid && wnd->free_bits[iw] == wbits) {
1255 /* Window is empty. */
1256 if (prev_tail + wbits >= to_alloc) {
1257 fnd = wbit + wpos - prev_tail;
1261 /* Increase 'prev_tail' and process next window. */
1268 bh = wnd_map(wnd, iw);
1276 /* Scan range [wpos, eBits). */
1277 fnd = wnd_scan(bh->b_data, wbit, wpos, wbits, to_alloc,
1278 &prev_tail, &b_pos, &b_len);
1280 if (fnd != MINUS_ONE_T)
1284 if (b_len < prev_tail) {
1285 /* The last fragment. */
1287 b_pos = max_alloc - prev_tail;
1292 * We have scanned range [hint max_alloc).
1293 * Prepare to scan range [0 hint + to_alloc).
1295 size_t nextmax = hint + to_alloc;
1297 if (likely(nextmax >= hint) && nextmax < max_alloc)
1298 max_alloc = nextmax;
1306 wnd->extent_max = b_len;
1308 if (flags & BITMAP_FIND_FULL)
1315 if (flags & BITMAP_FIND_MARK_AS_USED) {
1316 /* TODO: Optimize remove extent (pass 'e'?). */
1317 if (wnd_set_used(wnd, fnd, to_alloc))
1319 } else if (wnd->extent_max != MINUS_ONE_T &&
1320 to_alloc > wnd->extent_max) {
1321 wnd->extent_max = to_alloc;
1332 * wnd_extend - Extend bitmap ($MFT bitmap).
1334 int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits)
1337 struct super_block *sb = wnd->sb;
1338 struct ntfs_sb_info *sbi = sb->s_fs_info;
1339 u32 blocksize = sb->s_blocksize;
1340 u32 wbits = blocksize * 8;
1342 size_t bits, iw, new_wnd;
1343 size_t old_bits = wnd->nbits;
1346 if (new_bits <= old_bits)
1349 /* Align to 8 byte boundary. */
1350 new_wnd = bytes_to_block(sb, bitmap_size(new_bits));
1351 new_last = new_bits & (wbits - 1);
1355 if (new_wnd != wnd->nwnd) {
1356 new_free = kmalloc_array(new_wnd, sizeof(u16), GFP_NOFS);
1360 memcpy(new_free, wnd->free_bits, wnd->nwnd * sizeof(short));
1361 memset(new_free + wnd->nwnd, 0,
1362 (new_wnd - wnd->nwnd) * sizeof(short));
1363 kfree(wnd->free_bits);
1364 wnd->free_bits = new_free;
1367 /* Zero bits [old_bits,new_bits). */
1368 bits = new_bits - old_bits;
1369 b0 = old_bits & (wbits - 1);
1371 for (iw = old_bits >> (sb->s_blocksize_bits + 3); bits; iw += 1) {
1374 u64 vbo, lbo, bytes;
1375 struct buffer_head *bh;
1377 if (iw + 1 == new_wnd)
1380 op = b0 + bits > wbits ? wbits - b0 : bits;
1381 vbo = (u64)iw * blocksize;
1383 err = ntfs_vbo_to_lbo(sbi, &wnd->run, vbo, &lbo, &bytes);
1387 bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
1393 ntfs_bitmap_clear_le(bh->b_data, b0, blocksize * 8 - b0);
1394 frb = wbits - ntfs_bitmap_weight_le(bh->b_data, wbits);
1395 wnd->total_zeroes += frb - wnd->free_bits[iw];
1396 wnd->free_bits[iw] = frb;
1398 set_buffer_uptodate(bh);
1399 mark_buffer_dirty(bh);
1401 /* err = sync_dirty_buffer(bh); */
1407 wnd->nbits = new_bits;
1408 wnd->nwnd = new_wnd;
1409 wnd->bits_last = new_last;
1411 wnd_add_free_ext(wnd, old_bits, new_bits - old_bits, false);
1416 void wnd_zone_set(struct wnd_bitmap *wnd, size_t lcn, size_t len)
1418 size_t zlen = wnd->zone_end - wnd->zone_bit;
1421 wnd_add_free_ext(wnd, wnd->zone_bit, zlen, false);
1423 if (!RB_EMPTY_ROOT(&wnd->start_tree) && len)
1424 wnd_remove_free_ext(wnd, lcn, len);
1426 wnd->zone_bit = lcn;
1427 wnd->zone_end = lcn + len;
1430 int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range)
1433 struct super_block *sb = sbi->sb;
1434 struct wnd_bitmap *wnd = &sbi->used.bitmap;
1435 u32 wbits = 8 * sb->s_blocksize;
1436 CLST len = 0, lcn = 0, done = 0;
1437 CLST minlen = bytes_to_cluster(sbi, range->minlen);
1438 CLST lcn_from = bytes_to_cluster(sbi, range->start);
1439 size_t iw = lcn_from >> (sb->s_blocksize_bits + 3);
1440 u32 wbit = lcn_from & (wbits - 1);
1446 if (range->len == (u64)-1)
1447 lcn_to = wnd->nbits;
1449 lcn_to = bytes_to_cluster(sbi, range->start + range->len);
1451 down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
1453 for (; iw < wnd->nwnd; iw++, wbit = 0) {
1454 CLST lcn_wnd = iw * wbits;
1455 struct buffer_head *bh;
1457 if (lcn_wnd > lcn_to)
1460 if (!wnd->free_bits[iw])
1463 if (iw + 1 == wnd->nwnd)
1464 wbits = wnd->bits_last;
1466 if (lcn_wnd + wbits > lcn_to)
1467 wbits = lcn_to - lcn_wnd;
1469 bh = wnd_map(wnd, iw);
1475 for (; wbit < wbits; wbit++) {
1476 if (!test_bit_le(wbit, bh->b_data)) {
1478 lcn = lcn_wnd + wbit;
1482 if (len >= minlen) {
1483 err = ntfs_discard(sbi, lcn, len);
1493 /* Process the last fragment. */
1494 if (len >= minlen) {
1495 err = ntfs_discard(sbi, lcn, len);
1502 range->len = (u64)done << sbi->cluster_bits;
1504 up_read(&wnd->rw_lock);
1509 #if BITS_PER_LONG == 64
1510 typedef __le64 bitmap_ulong;
1511 #define cpu_to_ul(x) cpu_to_le64(x)
1512 #define ul_to_cpu(x) le64_to_cpu(x)
1514 typedef __le32 bitmap_ulong;
1515 #define cpu_to_ul(x) cpu_to_le32(x)
1516 #define ul_to_cpu(x) le32_to_cpu(x)
1519 void ntfs_bitmap_set_le(void *map, unsigned int start, int len)
1521 bitmap_ulong *p = (bitmap_ulong *)map + BIT_WORD(start);
1522 const unsigned int size = start + len;
1523 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
1524 bitmap_ulong mask_to_set = cpu_to_ul(BITMAP_FIRST_WORD_MASK(start));
1526 while (len - bits_to_set >= 0) {
1529 bits_to_set = BITS_PER_LONG;
1530 mask_to_set = cpu_to_ul(~0UL);
1534 mask_to_set &= cpu_to_ul(BITMAP_LAST_WORD_MASK(size));
1539 void ntfs_bitmap_clear_le(void *map, unsigned int start, int len)
1541 bitmap_ulong *p = (bitmap_ulong *)map + BIT_WORD(start);
1542 const unsigned int size = start + len;
1543 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
1544 bitmap_ulong mask_to_clear = cpu_to_ul(BITMAP_FIRST_WORD_MASK(start));
1546 while (len - bits_to_clear >= 0) {
1547 *p &= ~mask_to_clear;
1548 len -= bits_to_clear;
1549 bits_to_clear = BITS_PER_LONG;
1550 mask_to_clear = cpu_to_ul(~0UL);
1554 mask_to_clear &= cpu_to_ul(BITMAP_LAST_WORD_MASK(size));
1555 *p &= ~mask_to_clear;
1559 unsigned int ntfs_bitmap_weight_le(const void *bitmap, int bits)
1561 const ulong *bmp = bitmap;
1562 unsigned int k, lim = bits / BITS_PER_LONG;
1565 for (k = 0; k < lim; k++)
1566 w += hweight_long(bmp[k]);
1568 if (bits % BITS_PER_LONG) {
1569 w += hweight_long(ul_to_cpu(((bitmap_ulong *)bitmap)[k]) &
1570 BITMAP_LAST_WORD_MASK(bits));