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