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)
44 kmem_cache_create("ntfs3_enode_cache", 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);
54 static inline u32 wnd_bits(const struct wnd_bitmap *wnd, size_t i)
56 return i + 1 == wnd->nwnd ? wnd->bits_last : wnd->sb->s_blocksize * 8;
62 * b_pos + b_len - biggest fragment.
63 * Scan range [wpos wbits) window @buf.
65 * Return: -1 if not found.
67 static size_t wnd_scan(const ulong *buf, size_t wbit, u32 wpos, u32 wend,
68 size_t to_alloc, size_t *prev_tail, size_t *b_pos,
74 u32 used = find_next_zero_bit(buf, wend, wpos);
77 if (*b_len < *prev_tail) {
78 *b_pos = wbit - *prev_tail;
88 if (*b_len < *prev_tail) {
89 *b_pos = wbit - *prev_tail;
97 * Now we have a fragment [wpos, wend) staring with 0.
99 end = wpos + to_alloc - *prev_tail;
100 free_bits = find_next_bit(buf, min(end, wend), wpos);
102 free_len = *prev_tail + free_bits - wpos;
104 if (*b_len < free_len) {
105 *b_pos = wbit + wpos - *prev_tail;
109 if (free_len >= to_alloc)
110 return wbit + wpos - *prev_tail;
112 if (free_bits >= wend) {
113 *prev_tail += free_bits - wpos;
117 wpos = free_bits + 1;
126 * wnd_close - Frees all resources.
128 void wnd_close(struct wnd_bitmap *wnd)
130 struct rb_node *node, *next;
132 kfree(wnd->free_bits);
133 run_close(&wnd->run);
135 node = rb_first(&wnd->start_tree);
138 next = rb_next(node);
139 rb_erase(node, &wnd->start_tree);
140 kmem_cache_free(ntfs_enode_cachep,
141 rb_entry(node, struct e_node, start.node));
146 static struct rb_node *rb_lookup(struct rb_root *root, size_t v)
148 struct rb_node **p = &root->rb_node;
149 struct rb_node *r = NULL;
152 struct rb_node_key *k;
154 k = rb_entry(*p, struct rb_node_key, node);
157 } else if (v > k->key) {
169 * rb_insert_count - Helper function to insert special kind of 'count' tree.
171 static inline bool rb_insert_count(struct rb_root *root, struct e_node *e)
173 struct rb_node **p = &root->rb_node;
174 struct rb_node *parent = NULL;
175 size_t e_ckey = e->count.key;
176 size_t e_skey = e->start.key;
180 rb_entry(parent = *p, struct e_node, count.node);
182 if (e_ckey > k->count.key) {
184 } else if (e_ckey < k->count.key) {
186 } else if (e_skey < k->start.key) {
188 } else if (e_skey > k->start.key) {
196 rb_link_node(&e->count.node, parent, p);
197 rb_insert_color(&e->count.node, root);
202 * rb_insert_start - Helper function to insert special kind of 'count' tree.
204 static inline bool rb_insert_start(struct rb_root *root, struct e_node *e)
206 struct rb_node **p = &root->rb_node;
207 struct rb_node *parent = NULL;
208 size_t e_skey = e->start.key;
215 k = rb_entry(parent, struct e_node, start.node);
216 if (e_skey < k->start.key) {
218 } else if (e_skey > k->start.key) {
226 rb_link_node(&e->start.node, parent, p);
227 rb_insert_color(&e->start.node, root);
232 * wnd_add_free_ext - Adds a new extent of free space.
233 * @build: 1 when building tree.
235 static void wnd_add_free_ext(struct wnd_bitmap *wnd, size_t bit, size_t len,
238 struct e_node *e, *e0 = NULL;
239 size_t ib, end_in = bit + len;
243 /* Use extent_min to filter too short extents. */
244 if (wnd->count >= NTFS_MAX_WND_EXTENTS &&
245 len <= wnd->extent_min) {
250 /* Try to find extent before 'bit'. */
251 n = rb_lookup(&wnd->start_tree, bit);
254 n = rb_first(&wnd->start_tree);
256 e = rb_entry(n, struct e_node, start.node);
258 if (e->start.key + e->count.key == bit) {
262 rb_erase(&e->start.node, &wnd->start_tree);
263 rb_erase(&e->count.node, &wnd->count_tree);
272 e = rb_entry(n, struct e_node, start.node);
273 next_end = e->start.key + e->count.key;
274 if (e->start.key > end_in)
279 len += next_end - end_in;
281 rb_erase(&e->start.node, &wnd->start_tree);
282 rb_erase(&e->count.node, &wnd->count_tree);
288 kmem_cache_free(ntfs_enode_cachep, e);
291 if (wnd->uptodated != 1) {
292 /* Check bits before 'bit'. */
293 ib = wnd->zone_bit == wnd->zone_end ||
298 while (bit > ib && wnd_is_free_hlp(wnd, bit - 1, 1)) {
303 /* Check bits after 'end_in'. */
304 ib = wnd->zone_bit == wnd->zone_end ||
305 end_in > wnd->zone_bit
309 while (end_in < ib && wnd_is_free_hlp(wnd, end_in, 1)) {
315 /* Insert new fragment. */
316 if (wnd->count >= NTFS_MAX_WND_EXTENTS) {
318 kmem_cache_free(ntfs_enode_cachep, e0);
322 /* Compare with smallest fragment. */
323 n = rb_last(&wnd->count_tree);
324 e = rb_entry(n, struct e_node, count.node);
325 if (len <= e->count.key)
326 goto out; /* Do not insert small fragments. */
332 e2 = rb_entry(n, struct e_node, count.node);
333 /* Smallest fragment will be 'e2->count.key'. */
334 wnd->extent_min = e2->count.key;
337 /* Replace smallest fragment by new one. */
338 rb_erase(&e->start.node, &wnd->start_tree);
339 rb_erase(&e->count.node, &wnd->count_tree);
342 e = e0 ? e0 : kmem_cache_alloc(ntfs_enode_cachep, GFP_ATOMIC);
348 if (build && len <= wnd->extent_min)
349 wnd->extent_min = len;
353 if (len > wnd->extent_max)
354 wnd->extent_max = len;
356 rb_insert_start(&wnd->start_tree, e);
357 rb_insert_count(&wnd->count_tree, e);
364 * wnd_remove_free_ext - Remove a run from the cached free space.
366 static void wnd_remove_free_ext(struct wnd_bitmap *wnd, size_t bit, size_t len)
368 struct rb_node *n, *n3;
369 struct e_node *e, *e3;
370 size_t end_in = bit + len;
371 size_t end3, end, new_key, new_len, max_new_len;
373 /* Try to find extent before 'bit'. */
374 n = rb_lookup(&wnd->start_tree, bit);
379 e = rb_entry(n, struct e_node, start.node);
380 end = e->start.key + e->count.key;
382 new_key = new_len = 0;
385 /* Range [bit,end_in) must be inside 'e' or outside 'e' and 'n'. */
386 if (e->start.key > bit)
388 else if (end_in <= end) {
389 /* Range [bit,end_in) inside 'e'. */
391 new_len = end - end_in;
392 len = bit - e->start.key;
393 } else if (bit > end) {
399 e3 = rb_entry(n3, struct e_node, start.node);
400 if (e3->start.key >= end_in)
403 if (e3->count.key == wnd->extent_max)
406 end3 = e3->start.key + e3->count.key;
408 e3->start.key = end_in;
409 rb_erase(&e3->count.node, &wnd->count_tree);
410 e3->count.key = end3 - end_in;
411 rb_insert_count(&wnd->count_tree, e3);
416 rb_erase(&e3->start.node, &wnd->start_tree);
417 rb_erase(&e3->count.node, &wnd->count_tree);
419 kmem_cache_free(ntfs_enode_cachep, e3);
423 n3 = rb_first(&wnd->count_tree);
425 n3 ? rb_entry(n3, struct e_node, count.node)->count.key
430 if (e->count.key != wnd->extent_max) {
432 } else if (rb_prev(&e->count.node)) {
435 n3 = rb_next(&e->count.node);
436 max_new_len = max(len, new_len);
438 wnd->extent_max = max_new_len;
440 e3 = rb_entry(n3, struct e_node, count.node);
441 wnd->extent_max = max(e3->count.key, max_new_len);
447 e->start.key = new_key;
448 rb_erase(&e->count.node, &wnd->count_tree);
449 e->count.key = new_len;
450 rb_insert_count(&wnd->count_tree, e);
452 rb_erase(&e->start.node, &wnd->start_tree);
453 rb_erase(&e->count.node, &wnd->count_tree);
455 kmem_cache_free(ntfs_enode_cachep, e);
459 rb_erase(&e->count.node, &wnd->count_tree);
461 rb_insert_count(&wnd->count_tree, e);
466 if (wnd->count >= NTFS_MAX_WND_EXTENTS) {
469 /* Get minimal extent. */
470 e = rb_entry(rb_last(&wnd->count_tree), struct e_node,
472 if (e->count.key > new_len)
475 /* Replace minimum. */
476 rb_erase(&e->start.node, &wnd->start_tree);
477 rb_erase(&e->count.node, &wnd->count_tree);
480 e = kmem_cache_alloc(ntfs_enode_cachep, GFP_ATOMIC);
486 e->start.key = new_key;
487 e->count.key = new_len;
488 rb_insert_start(&wnd->start_tree, e);
489 rb_insert_count(&wnd->count_tree, e);
494 if (!wnd->count && 1 != wnd->uptodated)
499 * wnd_rescan - Scan all bitmap. Used while initialization.
501 static int wnd_rescan(struct wnd_bitmap *wnd)
504 size_t prev_tail = 0;
505 struct super_block *sb = wnd->sb;
506 struct ntfs_sb_info *sbi = sb->s_fs_info;
508 u32 blocksize = sb->s_blocksize;
509 u8 cluster_bits = sbi->cluster_bits;
510 u32 wbits = 8 * sb->s_blocksize;
513 size_t wpos, wbit, iw, vbo;
514 struct buffer_head *bh = NULL;
519 wnd->extent_min = MINUS_ONE_T;
520 wnd->total_zeroes = 0;
524 for (iw = 0; iw < wnd->nwnd; iw++) {
525 if (iw + 1 == wnd->nwnd)
526 wbits = wnd->bits_last;
529 if (!wnd->free_bits[iw]) {
532 wnd_add_free_ext(wnd,
539 if (wbits == wnd->free_bits[iw]) {
542 wnd->total_zeroes += wbits;
548 u32 off = vbo & sbi->cluster_mask;
550 if (!run_lookup_entry(&wnd->run, vbo >> cluster_bits,
551 &lcn, &clen, NULL)) {
556 lbo = ((u64)lcn << cluster_bits) + off;
557 len = ((u64)clen << cluster_bits) - off;
560 bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
566 buf = (ulong *)bh->b_data;
568 used = __bitmap_weight(buf, wbits);
571 wnd->free_bits[iw] = frb;
572 wnd->total_zeroes += frb;
578 if (wbit + wbits > wnd->nbits)
579 wbits = wnd->nbits - wbit;
582 used = find_next_zero_bit(buf, wbits, wpos);
584 if (used > wpos && prev_tail) {
585 wnd_add_free_ext(wnd, wbit + wpos - prev_tail,
593 /* No free blocks. */
598 frb = find_next_bit(buf, wbits, wpos);
600 /* Keep last free block. */
601 prev_tail += frb - wpos;
605 wnd_add_free_ext(wnd, wbit + wpos - prev_tail,
606 frb + prev_tail - wpos, true);
608 /* Skip free block and first '1'. */
610 /* Reset previous tail. */
612 } while (wpos < wbits);
627 /* Add last block. */
629 wnd_add_free_ext(wnd, wnd->nbits - prev_tail, prev_tail, true);
632 * Before init cycle wnd->uptodated was 0.
633 * If any errors or limits occurs while initialization then
634 * wnd->uptodated will be -1.
635 * If 'uptodated' is still 0 then Tree is really updated.
640 if (wnd->zone_bit != wnd->zone_end) {
641 size_t zlen = wnd->zone_end - wnd->zone_bit;
643 wnd->zone_end = wnd->zone_bit;
644 wnd_zone_set(wnd, wnd->zone_bit, zlen);
651 int wnd_init(struct wnd_bitmap *wnd, struct super_block *sb, size_t nbits)
654 u32 blocksize = sb->s_blocksize;
655 u32 wbits = blocksize * 8;
657 init_rwsem(&wnd->rw_lock);
661 wnd->total_zeroes = nbits;
662 wnd->extent_max = MINUS_ONE_T;
663 wnd->zone_bit = wnd->zone_end = 0;
664 wnd->nwnd = bytes_to_block(sb, bitmap_size(nbits));
665 wnd->bits_last = nbits & (wbits - 1);
667 wnd->bits_last = wbits;
669 wnd->free_bits = kcalloc(wnd->nwnd, sizeof(u16), GFP_NOFS);
673 err = wnd_rescan(wnd);
683 * wnd_map - Call sb_bread for requested window.
685 static struct buffer_head *wnd_map(struct wnd_bitmap *wnd, size_t iw)
689 struct super_block *sb = wnd->sb;
690 struct ntfs_sb_info *sbi;
691 struct buffer_head *bh;
695 vbo = (u64)iw << sb->s_blocksize_bits;
697 if (!run_lookup_entry(&wnd->run, vbo >> sbi->cluster_bits, &lcn, &clen,
699 return ERR_PTR(-ENOENT);
702 lbo = ((u64)lcn << sbi->cluster_bits) + (vbo & sbi->cluster_mask);
704 bh = ntfs_bread(wnd->sb, lbo >> sb->s_blocksize_bits);
706 return ERR_PTR(-EIO);
712 * wnd_set_free - Mark the bits range from bit to bit + bits as free.
714 int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits)
717 struct super_block *sb = wnd->sb;
719 u32 wbits = 8 * sb->s_blocksize;
720 size_t iw = bit >> (sb->s_blocksize_bits + 3);
721 u32 wbit = bit & (wbits - 1);
722 struct buffer_head *bh;
724 while (iw < wnd->nwnd && bits) {
728 if (iw + 1 == wnd->nwnd)
729 wbits = wnd->bits_last;
732 op = min_t(u32, tail, bits);
734 bh = wnd_map(wnd, iw);
740 buf = (ulong *)bh->b_data;
744 __bitmap_clear(buf, wbit, op);
746 wnd->free_bits[iw] += op;
748 set_buffer_uptodate(bh);
749 mark_buffer_dirty(bh);
753 wnd->total_zeroes += op;
759 wnd_add_free_ext(wnd, bit, bits0, false);
765 * wnd_set_used - Mark the bits range from bit to bit + bits as used.
767 int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits)
770 struct super_block *sb = wnd->sb;
772 size_t iw = bit >> (sb->s_blocksize_bits + 3);
773 u32 wbits = 8 * sb->s_blocksize;
774 u32 wbit = bit & (wbits - 1);
775 struct buffer_head *bh;
777 while (iw < wnd->nwnd && bits) {
781 if (unlikely(iw + 1 == wnd->nwnd))
782 wbits = wnd->bits_last;
785 op = min_t(u32, tail, bits);
787 bh = wnd_map(wnd, iw);
792 buf = (ulong *)bh->b_data;
796 __bitmap_set(buf, wbit, op);
797 wnd->free_bits[iw] -= op;
799 set_buffer_uptodate(bh);
800 mark_buffer_dirty(bh);
804 wnd->total_zeroes -= op;
810 if (!RB_EMPTY_ROOT(&wnd->start_tree))
811 wnd_remove_free_ext(wnd, bit, bits0);
819 * Return: True if all clusters [bit, bit+bits) are free (bitmap only).
821 static bool wnd_is_free_hlp(struct wnd_bitmap *wnd, size_t bit, size_t bits)
823 struct super_block *sb = wnd->sb;
824 size_t iw = bit >> (sb->s_blocksize_bits + 3);
825 u32 wbits = 8 * sb->s_blocksize;
826 u32 wbit = bit & (wbits - 1);
828 while (iw < wnd->nwnd && bits) {
831 if (unlikely(iw + 1 == wnd->nwnd))
832 wbits = wnd->bits_last;
835 op = min_t(u32, tail, bits);
837 if (wbits != wnd->free_bits[iw]) {
839 struct buffer_head *bh = wnd_map(wnd, iw);
844 ret = are_bits_clear((ulong *)bh->b_data, wbit, op);
862 * Return: True if all clusters [bit, bit+bits) are free.
864 bool wnd_is_free(struct wnd_bitmap *wnd, size_t bit, size_t bits)
871 if (RB_EMPTY_ROOT(&wnd->start_tree))
874 n = rb_lookup(&wnd->start_tree, bit);
878 e = rb_entry(n, struct e_node, start.node);
880 end = e->start.key + e->count.key;
882 if (bit < end && bit + bits <= end)
886 ret = wnd_is_free_hlp(wnd, bit, bits);
894 * Return: True if all clusters [bit, bit+bits) are used.
896 bool wnd_is_used(struct wnd_bitmap *wnd, size_t bit, size_t bits)
899 struct super_block *sb = wnd->sb;
900 size_t iw = bit >> (sb->s_blocksize_bits + 3);
901 u32 wbits = 8 * sb->s_blocksize;
902 u32 wbit = bit & (wbits - 1);
907 if (RB_EMPTY_ROOT(&wnd->start_tree))
911 n = rb_lookup(&wnd->start_tree, end - 1);
915 e = rb_entry(n, struct e_node, start.node);
916 if (e->start.key + e->count.key > bit)
920 while (iw < wnd->nwnd && bits) {
923 if (unlikely(iw + 1 == wnd->nwnd))
924 wbits = wnd->bits_last;
927 op = min_t(u32, tail, bits);
929 if (wnd->free_bits[iw]) {
931 struct buffer_head *bh = wnd_map(wnd, iw);
936 ret = are_bits_set((ulong *)bh->b_data, wbit, op);
953 * wnd_find - Look for free space.
955 * - flags - BITMAP_FIND_XXX flags
957 * Return: 0 if not found.
959 size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
960 size_t flags, size_t *allocated)
962 struct super_block *sb;
963 u32 wbits, wpos, wzbit, wzend;
964 size_t fnd, max_alloc, b_len, b_pos;
965 size_t iw, prev_tail, nwnd, wbit, ebit, zbit, zend;
966 size_t to_alloc0 = to_alloc;
968 const struct e_node *e;
969 const struct rb_node *pr, *cr;
972 struct buffer_head *bh;
974 /* Fast checking for available free space. */
975 if (flags & BITMAP_FIND_FULL) {
976 size_t zeroes = wnd_zeroes(wnd);
978 zeroes -= wnd->zone_end - wnd->zone_bit;
979 if (zeroes < to_alloc0)
982 if (to_alloc0 > wnd->extent_max)
985 if (to_alloc > wnd->extent_max)
986 to_alloc = wnd->extent_max;
989 if (wnd->zone_bit <= hint && hint < wnd->zone_end)
990 hint = wnd->zone_end;
992 max_alloc = wnd->nbits;
995 if (hint >= max_alloc)
998 if (RB_EMPTY_ROOT(&wnd->start_tree)) {
999 if (wnd->uptodated == 1) {
1000 /* Extents tree is updated -> No free space. */
1008 goto allocate_biggest;
1010 /* Use hint: Enumerate extents by start >= hint. */
1012 cr = wnd->start_tree.rb_node;
1015 e = rb_entry(cr, struct e_node, start.node);
1017 if (e->start.key == hint)
1020 if (e->start.key < hint) {
1030 e = pr ? rb_entry(pr, struct e_node, start.node) : NULL;
1036 goto allocate_biggest;
1038 if (e->start.key + e->count.key > hint) {
1039 /* We have found extension with 'hint' inside. */
1040 size_t len = e->start.key + e->count.key - hint;
1042 if (len >= to_alloc && hint + to_alloc <= max_alloc) {
1047 if (!(flags & BITMAP_FIND_FULL)) {
1051 if (hint + len <= max_alloc) {
1060 /* Allocate from biggest free extent. */
1061 e = rb_entry(rb_first(&wnd->count_tree), struct e_node, count.node);
1062 if (e->count.key != wnd->extent_max)
1063 wnd->extent_max = e->count.key;
1065 if (e->count.key < max_alloc) {
1066 if (e->count.key >= to_alloc) {
1068 } else if (flags & BITMAP_FIND_FULL) {
1069 if (e->count.key < to_alloc0) {
1070 /* Biggest free block is less then requested. */
1073 to_alloc = e->count.key;
1074 } else if (-1 != wnd->uptodated) {
1075 to_alloc = e->count.key;
1077 /* Check if we can use more bits. */
1078 size_t op, max_check;
1079 struct rb_root start_tree;
1081 memcpy(&start_tree, &wnd->start_tree,
1082 sizeof(struct rb_root));
1083 memset(&wnd->start_tree, 0, sizeof(struct rb_root));
1085 max_check = e->start.key + to_alloc;
1086 if (max_check > max_alloc)
1087 max_check = max_alloc;
1088 for (op = e->start.key + e->count.key; op < max_check;
1090 if (!wnd_is_free(wnd, op, 1))
1093 memcpy(&wnd->start_tree, &start_tree,
1094 sizeof(struct rb_root));
1095 to_alloc = op - e->start.key;
1098 /* Prepare to return. */
1100 if (e->start.key + to_alloc > max_alloc)
1101 to_alloc = max_alloc - e->start.key;
1105 if (wnd->uptodated == 1) {
1106 /* Extents tree is updated -> no free space. */
1110 b_len = e->count.key;
1111 b_pos = e->start.key;
1115 log2_bits = sb->s_blocksize_bits + 3;
1117 /* At most two ranges [hint, max_alloc) + [0, hint). */
1120 /* TODO: Optimize request for case nbits > wbits. */
1121 iw = hint >> log2_bits;
1122 wbits = sb->s_blocksize * 8;
1123 wpos = hint & (wbits - 1);
1127 if (max_alloc == wnd->nbits) {
1130 size_t t = max_alloc + wbits - 1;
1132 nwnd = likely(t > max_alloc) ? (t >> log2_bits) : wnd->nwnd;
1135 /* Enumerate all windows. */
1136 for (; iw < nwnd; iw++) {
1137 wbit = iw << log2_bits;
1139 if (!wnd->free_bits[iw]) {
1140 if (prev_tail > b_len) {
1141 b_pos = wbit - prev_tail;
1145 /* Skip full used window. */
1151 if (unlikely(iw + 1 == nwnd)) {
1152 if (max_alloc == wnd->nbits) {
1153 wbits = wnd->bits_last;
1155 size_t t = max_alloc & (wbits - 1);
1159 fbits_valid = false;
1164 if (wnd->zone_end > wnd->zone_bit) {
1165 ebit = wbit + wbits;
1166 zbit = max(wnd->zone_bit, wbit);
1167 zend = min(wnd->zone_end, ebit);
1169 /* Here we have a window [wbit, ebit) and zone [zbit, zend). */
1171 /* Zone does not overlap window. */
1173 wzbit = zbit - wbit;
1174 wzend = zend - wbit;
1176 /* Zone overlaps window. */
1177 if (wnd->free_bits[iw] == wzend - wzbit) {
1183 /* Scan two ranges window: [wbit, zbit) and [zend, ebit). */
1184 bh = wnd_map(wnd, iw);
1193 buf = (ulong *)bh->b_data;
1195 /* Scan range [wbit, zbit). */
1197 /* Scan range [wpos, zbit). */
1198 fnd = wnd_scan(buf, wbit, wpos, wzbit,
1199 to_alloc, &prev_tail,
1201 if (fnd != MINUS_ONE_T) {
1209 /* Scan range [zend, ebit). */
1210 if (wzend < wbits) {
1211 fnd = wnd_scan(buf, wbit,
1212 max(wzend, wpos), wbits,
1213 to_alloc, &prev_tail,
1215 if (fnd != MINUS_ONE_T) {
1227 /* Current window does not overlap zone. */
1228 if (!wpos && fbits_valid && wnd->free_bits[iw] == wbits) {
1229 /* Window is empty. */
1230 if (prev_tail + wbits >= to_alloc) {
1231 fnd = wbit + wpos - prev_tail;
1235 /* Increase 'prev_tail' and process next window. */
1242 bh = wnd_map(wnd, iw);
1250 buf = (ulong *)bh->b_data;
1252 /* Scan range [wpos, eBits). */
1253 fnd = wnd_scan(buf, wbit, wpos, wbits, to_alloc, &prev_tail,
1256 if (fnd != MINUS_ONE_T)
1260 if (b_len < prev_tail) {
1261 /* The last fragment. */
1263 b_pos = max_alloc - prev_tail;
1268 * We have scanned range [hint max_alloc).
1269 * Prepare to scan range [0 hint + to_alloc).
1271 size_t nextmax = hint + to_alloc;
1273 if (likely(nextmax >= hint) && nextmax < max_alloc)
1274 max_alloc = nextmax;
1282 wnd->extent_max = b_len;
1284 if (flags & BITMAP_FIND_FULL)
1291 if (flags & BITMAP_FIND_MARK_AS_USED) {
1292 /* TODO: Optimize remove extent (pass 'e'?). */
1293 if (wnd_set_used(wnd, fnd, to_alloc))
1295 } else if (wnd->extent_max != MINUS_ONE_T &&
1296 to_alloc > wnd->extent_max) {
1297 wnd->extent_max = to_alloc;
1308 * wnd_extend - Extend bitmap ($MFT bitmap).
1310 int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits)
1313 struct super_block *sb = wnd->sb;
1314 struct ntfs_sb_info *sbi = sb->s_fs_info;
1315 u32 blocksize = sb->s_blocksize;
1316 u32 wbits = blocksize * 8;
1318 size_t bits, iw, new_wnd;
1319 size_t old_bits = wnd->nbits;
1322 if (new_bits <= old_bits)
1325 /* Align to 8 byte boundary. */
1326 new_wnd = bytes_to_block(sb, bitmap_size(new_bits));
1327 new_last = new_bits & (wbits - 1);
1331 if (new_wnd != wnd->nwnd) {
1332 new_free = kmalloc(new_wnd * sizeof(u16), GFP_NOFS);
1336 if (new_free != wnd->free_bits)
1337 memcpy(new_free, wnd->free_bits,
1338 wnd->nwnd * sizeof(short));
1339 memset(new_free + wnd->nwnd, 0,
1340 (new_wnd - wnd->nwnd) * sizeof(short));
1341 kfree(wnd->free_bits);
1342 wnd->free_bits = new_free;
1345 /* Zero bits [old_bits,new_bits). */
1346 bits = new_bits - old_bits;
1347 b0 = old_bits & (wbits - 1);
1349 for (iw = old_bits >> (sb->s_blocksize_bits + 3); bits; iw += 1) {
1352 u64 vbo, lbo, bytes;
1353 struct buffer_head *bh;
1356 if (iw + 1 == new_wnd)
1359 op = b0 + bits > wbits ? wbits - b0 : bits;
1360 vbo = (u64)iw * blocksize;
1362 err = ntfs_vbo_to_lbo(sbi, &wnd->run, vbo, &lbo, &bytes);
1366 bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
1371 buf = (ulong *)bh->b_data;
1373 __bitmap_clear(buf, b0, blocksize * 8 - b0);
1374 frb = wbits - __bitmap_weight(buf, wbits);
1375 wnd->total_zeroes += frb - wnd->free_bits[iw];
1376 wnd->free_bits[iw] = frb;
1378 set_buffer_uptodate(bh);
1379 mark_buffer_dirty(bh);
1381 /* err = sync_dirty_buffer(bh); */
1387 wnd->nbits = new_bits;
1388 wnd->nwnd = new_wnd;
1389 wnd->bits_last = new_last;
1391 wnd_add_free_ext(wnd, old_bits, new_bits - old_bits, false);
1396 void wnd_zone_set(struct wnd_bitmap *wnd, size_t lcn, size_t len)
1400 zlen = wnd->zone_end - wnd->zone_bit;
1402 wnd_add_free_ext(wnd, wnd->zone_bit, zlen, false);
1404 if (!RB_EMPTY_ROOT(&wnd->start_tree) && len)
1405 wnd_remove_free_ext(wnd, lcn, len);
1407 wnd->zone_bit = lcn;
1408 wnd->zone_end = lcn + len;
1411 int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range)
1414 struct super_block *sb = sbi->sb;
1415 struct wnd_bitmap *wnd = &sbi->used.bitmap;
1416 u32 wbits = 8 * sb->s_blocksize;
1417 CLST len = 0, lcn = 0, done = 0;
1418 CLST minlen = bytes_to_cluster(sbi, range->minlen);
1419 CLST lcn_from = bytes_to_cluster(sbi, range->start);
1420 size_t iw = lcn_from >> (sb->s_blocksize_bits + 3);
1421 u32 wbit = lcn_from & (wbits - 1);
1428 if (range->len == (u64)-1)
1429 lcn_to = wnd->nbits;
1431 lcn_to = bytes_to_cluster(sbi, range->start + range->len);
1433 down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
1435 for (; iw < wnd->nbits; iw++, wbit = 0) {
1436 CLST lcn_wnd = iw * wbits;
1437 struct buffer_head *bh;
1439 if (lcn_wnd > lcn_to)
1442 if (!wnd->free_bits[iw])
1445 if (iw + 1 == wnd->nwnd)
1446 wbits = wnd->bits_last;
1448 if (lcn_wnd + wbits > lcn_to)
1449 wbits = lcn_to - lcn_wnd;
1451 bh = wnd_map(wnd, iw);
1457 buf = (ulong *)bh->b_data;
1459 for (; wbit < wbits; wbit++) {
1460 if (!test_bit(wbit, buf)) {
1462 lcn = lcn_wnd + wbit;
1466 if (len >= minlen) {
1467 err = ntfs_discard(sbi, lcn, len);
1477 /* Process the last fragment. */
1478 if (len >= minlen) {
1479 err = ntfs_discard(sbi, lcn, len);
1486 range->len = (u64)done << sbi->cluster_bits;
1488 up_read(&wnd->rw_lock);