hammer2 - update documentation, begin working on callback I/O
[dragonfly.git] / sys / vfs / hammer2 / hammer2_freemap.c
CommitLineData
5c23d7f1 1/*
8138a154 2 * Copyright (c) 2011-2014 The DragonFly Project. All rights reserved.
5c23d7f1
MD
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/fcntl.h>
39#include <sys/buf.h>
40#include <sys/proc.h>
41#include <sys/namei.h>
42#include <sys/mount.h>
43#include <sys/vnode.h>
44#include <sys/mountctl.h>
45
46#include "hammer2.h"
47
8138a154
MD
48#define FREEMAP_DEBUG 0
49
91abd410
MD
50struct hammer2_fiterate {
51 hammer2_off_t bpref;
52 hammer2_off_t bnext;
53 int loops;
54};
55
56typedef struct hammer2_fiterate hammer2_fiterate_t;
57
1a7cfe5a
MD
58static int hammer2_freemap_try_alloc(hammer2_trans_t *trans,
59 hammer2_chain_t **parentp, hammer2_blockref_t *bref,
91abd410 60 int radix, hammer2_fiterate_t *iter);
a5913bdf
MD
61static void hammer2_freemap_init(hammer2_trans_t *trans, hammer2_mount_t *hmp,
62 hammer2_key_t key, hammer2_chain_t *chain);
63static int hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_mount_t *hmp,
91abd410 64 hammer2_bmap_data_t *bmap, uint16_t class,
a5913bdf 65 int n, int radix, hammer2_key_t *basep);
1a7cfe5a
MD
66static int hammer2_freemap_iterate(hammer2_trans_t *trans,
67 hammer2_chain_t **parentp, hammer2_chain_t **chainp,
91abd410 68 hammer2_fiterate_t *iter);
1a7cfe5a 69
a98aa0b0
MD
70static __inline
71int
72hammer2_freemapradix(int radix)
73{
74 return(radix);
75}
76
1a7cfe5a
MD
77/*
78 * Calculate the device offset for the specified FREEMAP_NODE or FREEMAP_LEAF
79 * bref. Return a combined media offset and physical size radix. Freemap
80 * chains use fixed storage offsets in the 4MB reserved area at the
81 * beginning of each 2GB zone
82 *
83 * Rotate between four possibilities. Theoretically this means we have three
84 * good freemaps in case of a crash which we can use as a base for the fixup
85 * scan at mount-time.
86 */
87#define H2FMBASE(key, radix) ((key) & ~(((hammer2_off_t)1 << (radix)) - 1))
88#define H2FMSHIFT(radix) ((hammer2_off_t)1 << (radix))
89
90static
91int
623d43d4 92hammer2_freemap_reserve(hammer2_trans_t *trans, hammer2_chain_t *chain,
1a7cfe5a
MD
93 int radix)
94{
623d43d4 95 hammer2_blockref_t *bref = &chain->bref;
1a7cfe5a 96 hammer2_off_t off;
a3fd5153 97 int index;
1a7cfe5a
MD
98 size_t bytes;
99
100 /*
101 * Physical allocation size -> radix. Typically either 256 for
102 * a level 0 freemap leaf or 65536 for a level N freemap node.
103 *
104 * NOTE: A 256 byte bitmap represents 256 x 8 x 1024 = 2MB of storage.
105 * Do not use hammer2_allocsize() here as it has a min cap.
106 */
107 bytes = 1 << radix;
108
109 /*
a71db85d
MD
110 * Calculate block selection index 0..7 of current block. If this
111 * is the first allocation of the block (verses a modification of an
112 * existing block), we use index 0, otherwise we use the next rotating
113 * index.
114 *
115 * NORMAL transactions use FREEMAP sections 0-5, while FREEBATCH
116 * transactions use sections 6 and 7. FREEBATCH transactions are
117 * used by the batch freeing code to spool-off in-memory structures
118 * used to track the batch free scan.
1a7cfe5a 119 */
a71db85d
MD
120 if (trans->flags & HAMMER2_TRANS_FREEBATCH) {
121 if ((bref->data_off & ~HAMMER2_OFF_MASK_RADIX) == 0) {
122 index = (HAMMER2_ZONE_FREEMAP_06 -
123 HAMMER2_ZONE_FREEMAP_00) / 4;
124 } else {
125 off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX &
126 (((hammer2_off_t)1 <<
127 HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
128 off = off / HAMMER2_PBUFSIZE;
129 KKASSERT(off >= HAMMER2_ZONE_FREEMAP_06 &&
130 off < HAMMER2_ZONE_FREEMAP_08);
131 index = (int)(off - HAMMER2_ZONE_FREEMAP_00) / 4;
132 KKASSERT(index >= 6 && index < 8);
133 if (++index == 8)
134 index = 6;
135 }
1a7cfe5a 136 } else {
a71db85d
MD
137 if ((bref->data_off & ~HAMMER2_OFF_MASK_RADIX) == 0) {
138 index = 0;
139 } else {
140 off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX &
141 (((hammer2_off_t)1 <<
142 HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
143 off = off / HAMMER2_PBUFSIZE;
144 KKASSERT(off >= HAMMER2_ZONE_FREEMAP_00 &&
145 off < HAMMER2_ZONE_FREEMAP_06);
146 index = (int)(off - HAMMER2_ZONE_FREEMAP_00) / 4;
147 KKASSERT(index >= 0 && index < 6);
148 if (++index == 6)
149 index = 0;
150 }
623d43d4 151 }
1a7cfe5a 152
1a7cfe5a
MD
153 /*
154 * Calculate the block offset of the reserved block. This will
155 * point into the 4MB reserved area at the base of the appropriate
156 * 2GB zone, once added to the FREEMAP_x selection above.
157 */
158 switch(bref->keybits) {
159 /* case HAMMER2_FREEMAP_LEVEL5_RADIX: not applicable */
160 case HAMMER2_FREEMAP_LEVEL4_RADIX: /* 2EB */
161 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
162 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
a3fd5153 163 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL4_RADIX) +
ba8a9be0
MD
164 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
165 HAMMER2_ZONEFM_LEVEL4) * HAMMER2_PBUFSIZE;
1a7cfe5a
MD
166 break;
167 case HAMMER2_FREEMAP_LEVEL3_RADIX: /* 2PB */
168 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
169 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
a3fd5153 170 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL3_RADIX) +
ba8a9be0
MD
171 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
172 HAMMER2_ZONEFM_LEVEL3) * HAMMER2_PBUFSIZE;
1a7cfe5a
MD
173 break;
174 case HAMMER2_FREEMAP_LEVEL2_RADIX: /* 2TB */
175 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
176 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
a3fd5153 177 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL2_RADIX) +
ba8a9be0
MD
178 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
179 HAMMER2_ZONEFM_LEVEL2) * HAMMER2_PBUFSIZE;
1a7cfe5a
MD
180 break;
181 case HAMMER2_FREEMAP_LEVEL1_RADIX: /* 2GB */
93f3933a 182 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
1a7cfe5a 183 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
a3fd5153 184 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
ba8a9be0
MD
185 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
186 HAMMER2_ZONEFM_LEVEL1) * HAMMER2_PBUFSIZE;
1a7cfe5a 187 break;
1a7cfe5a
MD
188 default:
189 panic("freemap: bad radix(2) %p %d\n", bref, bref->keybits);
190 /* NOT REACHED */
a3fd5153 191 off = (hammer2_off_t)-1;
1a7cfe5a
MD
192 break;
193 }
194 bref->data_off = off | radix;
8138a154
MD
195#if FREEMAP_DEBUG
196 kprintf("FREEMAP BLOCK TYPE %d %016jx/%d DATA_OFF=%016jx\n",
197 bref->type, bref->key, bref->keybits, bref->data_off);
623d43d4 198#endif
1a7cfe5a
MD
199 return (0);
200}
201
1a7cfe5a
MD
202/*
203 * Normal freemap allocator
204 *
205 * Use available hints to allocate space using the freemap. Create missing
206 * freemap infrastructure on-the-fly as needed (including marking initial
207 * allocations using the iterator as allocated, instantiating new 2GB zones,
208 * and dealing with the end-of-media edge case).
209 *
210 * ip and bpref are only used as a heuristic to determine locality of
211 * reference. bref->key may also be used heuristically.
212 */
213int
623d43d4
MD
214hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_chain_t *chain,
215 size_t bytes)
1a7cfe5a 216{
623d43d4
MD
217 hammer2_mount_t *hmp = chain->hmp;
218 hammer2_blockref_t *bref = &chain->bref;
1a7cfe5a 219 hammer2_chain_t *parent;
1a7cfe5a
MD
220 int radix;
221 int error;
91abd410
MD
222 unsigned int hindex;
223 hammer2_fiterate_t iter;
1a7cfe5a
MD
224
225 /*
226 * Validate the allocation size. It must be a power of 2.
93f3933a
MD
227 *
228 * For now require that the caller be aware of the minimum
229 * allocation (1K).
1a7cfe5a
MD
230 */
231 radix = hammer2_getradix(bytes);
232 KKASSERT((size_t)1 << radix == bytes);
233
1a7cfe5a
MD
234 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
235 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
50456506
MD
236 /*
237 * Freemap blocks themselves are assigned from the reserve
238 * area, not allocated from the freemap.
239 */
240 error = hammer2_freemap_reserve(trans, chain, radix);
241 return error;
1a7cfe5a 242 }
1a7cfe5a 243
50456506 244 KKASSERT(bytes >= HAMMER2_ALLOC_MIN && bytes <= HAMMER2_ALLOC_MAX);
91abd410 245
052e0aa0 246 if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
50456506 247 ++trans->sync_xid;
1a7cfe5a 248
01eabad4 249 /*
a98aa0b0
MD
250 * Calculate the starting point for our allocation search.
251 *
252 * Each freemap leaf is dedicated to a specific freemap_radix.
253 * The freemap_radix can be more fine-grained than the device buffer
254 * radix which results in inodes being grouped together in their
255 * own segment, terminal-data (16K or less) and initial indirect
256 * block being grouped together, and then full-indirect and full-data
257 * blocks (64K) being grouped together.
258 *
259 * The single most important aspect of this is the inode grouping
260 * because that is what allows 'find' and 'ls' and other filesystem
261 * topology operations to run fast.
1a7cfe5a 262 */
a98aa0b0 263#if 0
1a7cfe5a
MD
264 if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX)
265 bpref = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
266 else if (trans->tmp_bpref)
267 bpref = trans->tmp_bpref;
268 else if (trans->tmp_ip)
269 bpref = trans->tmp_ip->chain->bref.data_off;
270 else
271#endif
91abd410
MD
272 /*
273 * Heuristic tracking index. We would like one for each distinct
274 * bref type if possible. heur_freemap[] has room for two classes
275 * for each type. At a minimum we have to break-up our heuristic
276 * by device block sizes.
277 */
278 hindex = hammer2_devblkradix(radix) - HAMMER2_MINIORADIX;
279 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR_NRADIX);
280 hindex += bref->type * HAMMER2_FREEMAP_HEUR_NRADIX;
281 hindex &= HAMMER2_FREEMAP_HEUR_TYPES * HAMMER2_FREEMAP_HEUR_NRADIX - 1;
282 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR);
283
284 iter.bpref = hmp->heur_freemap[hindex];
1a7cfe5a
MD
285
286 /*
287 * Make sure bpref is in-bounds. It's ok if bpref covers a zone's
288 * reserved area, the try code will iterate past it.
289 */
91abd410
MD
290 if (iter.bpref > hmp->voldata.volu_size)
291 iter.bpref = hmp->voldata.volu_size - 1;
1a7cfe5a
MD
292
293 /*
294 * Iterate the freemap looking for free space before and after.
295 */
296 parent = &hmp->fchain;
297 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
298 error = EAGAIN;
91abd410
MD
299 iter.bnext = iter.bpref;
300 iter.loops = 0;
1a7cfe5a
MD
301
302 while (error == EAGAIN) {
303 error = hammer2_freemap_try_alloc(trans, &parent, bref,
91abd410 304 radix, &iter);
1a7cfe5a 305 }
91abd410 306 hmp->heur_freemap[hindex] = iter.bnext;
1a7cfe5a
MD
307 hammer2_chain_unlock(parent);
308
052e0aa0 309 if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
50456506 310 --trans->sync_xid;
a7720be7 311
1a7cfe5a 312 return (error);
1a7cfe5a
MD
313}
314
315static int
316hammer2_freemap_try_alloc(hammer2_trans_t *trans, hammer2_chain_t **parentp,
317 hammer2_blockref_t *bref, int radix,
91abd410 318 hammer2_fiterate_t *iter)
1a7cfe5a 319{
a5913bdf 320 hammer2_mount_t *hmp = (*parentp)->hmp;
1a7cfe5a 321 hammer2_off_t l0size;
93f3933a
MD
322 hammer2_off_t l1size;
323 hammer2_off_t l1mask;
1897c66e 324 hammer2_key_t key_dummy;
1a7cfe5a
MD
325 hammer2_chain_t *chain;
326 hammer2_off_t key;
1a7cfe5a 327 size_t bytes;
91abd410 328 uint16_t class;
1a7cfe5a 329 int error = 0;
1897c66e 330 int cache_index = -1;
278ab2b2 331 int ddflag;
93f3933a 332
1a7cfe5a
MD
333
334 /*
335 * Calculate the number of bytes being allocated, the number
336 * of contiguous bits of bitmap being allocated, and the bitmap
337 * mask.
01eabad4 338 *
1a7cfe5a
MD
339 * WARNING! cpu hardware may mask bits == 64 -> 0 and blow up the
340 * mask calculation.
341 */
342 bytes = (size_t)1 << radix;
91abd410 343 class = (bref->type << 8) | hammer2_devblkradix(radix);
a98aa0b0 344
1a7cfe5a 345 /*
93f3933a 346 * Lookup the level1 freemap chain, creating and initializing one
1a7cfe5a
MD
347 * if necessary. Intermediate levels will be created automatically
348 * when necessary by hammer2_chain_create().
349 */
91abd410 350 key = H2FMBASE(iter->bnext, HAMMER2_FREEMAP_LEVEL1_RADIX);
1a7cfe5a 351 l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
93f3933a
MD
352 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
353 l1mask = l1size - 1;
1a7cfe5a 354
1897c66e
MD
355 chain = hammer2_chain_lookup(parentp, &key_dummy, key, key + l1mask,
356 &cache_index,
1a7cfe5a 357 HAMMER2_LOOKUP_ALWAYS |
278ab2b2 358 HAMMER2_LOOKUP_MATCHIND, &ddflag);
623d43d4 359
1a7cfe5a
MD
360 if (chain == NULL) {
361 /*
362 * Create the missing leaf, be sure to initialize
363 * the auxillary freemap tracking information in
364 * the bref.check.freemap structure.
365 */
366#if 0
93f3933a 367 kprintf("freemap create L1 @ %016jx bpref %016jx\n",
91abd410 368 key, iter->bpref);
1a7cfe5a 369#endif
50456506 370 error = hammer2_chain_create(trans, parentp, &chain, hmp->spmp,
93f3933a 371 key, HAMMER2_FREEMAP_LEVEL1_RADIX,
1a7cfe5a 372 HAMMER2_BREF_TYPE_FREEMAP_LEAF,
b3659de2
MD
373 HAMMER2_FREEMAP_LEVELN_PSIZE,
374 0);
0924b3f8 375 KKASSERT(error == 0);
1a7cfe5a 376 if (error == 0) {
da6f36f4 377 hammer2_chain_modify(trans, chain, 0);
93f3933a
MD
378 bzero(&chain->data->bmdata[0],
379 HAMMER2_FREEMAP_LEVELN_PSIZE);
380 chain->bref.check.freemap.bigmask = (uint32_t)-1;
381 chain->bref.check.freemap.avail = l1size;
382 /* bref.methods should already be inherited */
1a7cfe5a 383
a5913bdf 384 hammer2_freemap_init(trans, hmp, key, chain);
1a7cfe5a 385 }
93f3933a 386 } else if ((chain->bref.check.freemap.bigmask & (1 << radix)) == 0) {
1a7cfe5a
MD
387 /*
388 * Already flagged as not having enough space
389 */
390 error = ENOSPC;
391 } else {
392 /*
393 * Modify existing chain to setup for adjustment.
394 */
da6f36f4 395 hammer2_chain_modify(trans, chain, 0);
1a7cfe5a 396 }
1a7cfe5a
MD
397
398 /*
93f3933a 399 * Scan 2MB entries.
1a7cfe5a 400 */
93f3933a
MD
401 if (error == 0) {
402 hammer2_bmap_data_t *bmap;
403 hammer2_key_t base_key;
404 int count;
405 int start;
406 int n;
407
10136ab6 408 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
91abd410
MD
409 start = (int)((iter->bnext - key) >>
410 HAMMER2_FREEMAP_LEVEL0_RADIX);
93f3933a 411 KKASSERT(start >= 0 && start < HAMMER2_FREEMAP_COUNT);
da6f36f4 412 hammer2_chain_modify(trans, chain, 0);
1a7cfe5a 413
93f3933a
MD
414 error = ENOSPC;
415 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
416 if (start + count >= HAMMER2_FREEMAP_COUNT &&
417 start - count < 0) {
1a7cfe5a 418 break;
93f3933a
MD
419 }
420 n = start + count;
421 bmap = &chain->data->bmdata[n];
422 if (n < HAMMER2_FREEMAP_COUNT && bmap->avail &&
91abd410 423 (bmap->class == 0 || bmap->class == class)) {
93f3933a 424 base_key = key + n * l0size;
91abd410
MD
425 error = hammer2_bmap_alloc(trans, hmp, bmap,
426 class, n, radix,
427 &base_key);
93f3933a
MD
428 if (error != ENOSPC) {
429 key = base_key;
430 break;
431 }
432 }
433 n = start - count;
434 bmap = &chain->data->bmdata[n];
435 if (n >= 0 && bmap->avail &&
91abd410 436 (bmap->class == 0 || bmap->class == class)) {
93f3933a 437 base_key = key + n * l0size;
91abd410
MD
438 error = hammer2_bmap_alloc(trans, hmp, bmap,
439 class, n, radix,
440 &base_key);
93f3933a
MD
441 if (error != ENOSPC) {
442 key = base_key;
443 break;
444 }
445 }
1a7cfe5a 446 }
93f3933a
MD
447 if (error == ENOSPC)
448 chain->bref.check.freemap.bigmask &= ~(1 << radix);
449 /* XXX also scan down from original count */
1a7cfe5a
MD
450 }
451
1a7cfe5a
MD
452 if (error == 0) {
453 /*
454 * Assert validity. Must be beyond the static allocator used
455 * by newfs_hammer2 (and thus also beyond the aux area),
456 * not go past the volume size, and must not be in the
457 * reserved segment area for a zone.
458 */
459 KKASSERT(key >= hmp->voldata.allocator_beg &&
460 key + bytes <= hmp->voldata.volu_size);
461 KKASSERT((key & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
1a7cfe5a
MD
462 bref->data_off = key | radix;
463
464#if 0
93f3933a 465 kprintf("alloc cp=%p %016jx %016jx using %016jx\n",
1a7cfe5a 466 chain,
93f3933a 467 bref->key, bref->data_off, chain->bref.data_off);
1a7cfe5a
MD
468#endif
469 } else if (error == ENOSPC) {
470 /*
91abd410 471 * Return EAGAIN with next iteration in iter->bnext, or
1a7cfe5a
MD
472 * return ENOSPC if the allocation map has been exhausted.
473 */
91abd410 474 error = hammer2_freemap_iterate(trans, parentp, &chain, iter);
1a7cfe5a
MD
475 }
476
477 /*
478 * Cleanup
479 */
480 if (chain)
481 hammer2_chain_unlock(chain);
482 return (error);
483}
484
93f3933a
MD
485/*
486 * Allocate (1<<radix) bytes from the bmap whos base data offset is (*basep).
487 *
488 * If the linear iterator is mid-block we use it directly (the bitmap should
489 * already be marked allocated), otherwise we search for a block in the bitmap
490 * that fits the allocation request.
491 *
492 * A partial bitmap allocation sets the minimum bitmap granularity (16KB)
493 * to fully allocated and adjusts the linear allocator to allow the
494 * remaining space to be allocated.
495 */
496static
497int
a5913bdf
MD
498hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_mount_t *hmp,
499 hammer2_bmap_data_t *bmap,
91abd410 500 uint16_t class, int n, int radix, hammer2_key_t *basep)
93f3933a 501{
fdf62707 502 hammer2_io_t *dio;
93f3933a 503 size_t size;
91abd410 504 size_t bsize;
93f3933a
MD
505 int bmradix;
506 uint32_t bmmask;
507 int offset;
fdf62707 508 int error;
93f3933a
MD
509 int i;
510 int j;
511
512 /*
513 * Take into account 2-bits per block when calculating bmradix.
514 */
515 size = (size_t)1 << radix;
91abd410 516
93f3933a
MD
517 if (radix <= HAMMER2_FREEMAP_BLOCK_RADIX) {
518 bmradix = 2;
91abd410 519 bsize = HAMMER2_FREEMAP_BLOCK_SIZE;
93f3933a
MD
520 /* (16K) 2 bits per allocation block */
521 } else {
522 bmradix = 2 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
91abd410 523 bsize = size;
93f3933a
MD
524 /* (32K-256K) 4, 8, 16, 32 bits per allocation block */
525 }
526
527 /*
91abd410
MD
528 * Use the linear iterator to pack small allocations, otherwise
529 * fall-back to finding a free 16KB chunk. The linear iterator
530 * is only valid when *NOT* on a freemap chunking boundary (16KB).
531 * If it is the bitmap must be scanned. It can become invalid
532 * once we pack to the boundary. We adjust it after a bitmap
533 * allocation only for sub-16KB allocations (so the perfectly good
534 * previous value can still be used for fragments when 16KB+
535 * allocations are made).
536 *
537 * Beware of hardware artifacts when bmradix == 32 (intermediate
538 * result can wind up being '1' instead of '0' if hardware masks
539 * bit-count & 31).
93f3933a
MD
540 *
541 * NOTE: j needs to be even in the j= calculation. As an artifact
542 * of the /2 division, our bitmask has to clear bit 0.
91abd410
MD
543 *
544 * NOTE: TODO this can leave little unallocatable fragments lying
545 * around.
93f3933a 546 */
91abd410
MD
547 if (((uint32_t)bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) + size <=
548 HAMMER2_FREEMAP_BLOCK_SIZE &&
2c6f594d 549 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) &&
91abd410 550 bmap->linear < HAMMER2_SEGSIZE) {
93f3933a 551 KKASSERT(bmap->linear >= 0 &&
91abd410 552 bmap->linear + size <= HAMMER2_SEGSIZE &&
50456506 553 (bmap->linear & (HAMMER2_ALLOC_MIN - 1)) == 0);
93f3933a
MD
554 offset = bmap->linear;
555 i = offset / (HAMMER2_SEGSIZE / 8);
556 j = (offset / (HAMMER2_FREEMAP_BLOCK_SIZE / 2)) & 30;
557 bmmask = (bmradix == 32) ?
558 0xFFFFFFFFU : (1 << bmradix) - 1;
559 bmmask <<= j;
91abd410 560 bmap->linear = offset + size;
93f3933a
MD
561 } else {
562 for (i = 0; i < 8; ++i) {
563 bmmask = (bmradix == 32) ?
564 0xFFFFFFFFU : (1 << bmradix) - 1;
565 for (j = 0; j < 32; j += bmradix) {
566 if ((bmap->bitmap[i] & bmmask) == 0)
567 goto success;
568 bmmask <<= bmradix;
569 }
570 }
91abd410
MD
571 /*fragments might remain*/
572 /*KKASSERT(bmap->avail == 0);*/
93f3933a
MD
573 return (ENOSPC);
574success:
575 offset = i * (HAMMER2_SEGSIZE / 8) +
576 (j * (HAMMER2_FREEMAP_BLOCK_SIZE / 2));
91abd410
MD
577 if (size & HAMMER2_FREEMAP_BLOCK_MASK)
578 bmap->linear = offset + size;
93f3933a
MD
579 }
580
581 KKASSERT(i >= 0 && i < 8); /* 8 x 16 -> 128 x 16K -> 2MB */
582
583 /*
584 * Optimize the buffer cache to avoid unnecessary read-before-write
585 * operations.
91abd410
MD
586 *
587 * The device block size could be larger than the allocation size
588 * so the actual bitmap test is somewhat more involved. We have
589 * to use a compatible buffer size for this operation.
93f3933a 590 */
2c6f594d
MD
591 if ((bmap->bitmap[i] & bmmask) == 0 &&
592 hammer2_devblksize(size) != size) {
91abd410
MD
593 size_t psize = hammer2_devblksize(size);
594 hammer2_off_t pmask = (hammer2_off_t)psize - 1;
595 int pbmradix = 2 << (hammer2_devblkradix(radix) -
596 HAMMER2_FREEMAP_BLOCK_RADIX);
597 uint32_t pbmmask;
fdf62707 598 int pradix = hammer2_getradix(psize);
91abd410
MD
599
600 pbmmask = (pbmradix == 32) ? 0xFFFFFFFFU : (1 << pbmradix) - 1;
601 while ((pbmmask & bmmask) == 0)
602 pbmmask <<= pbmradix;
603
2c6f594d
MD
604#if 0
605 kprintf("%016jx mask %08x %08x %08x (%zd/%zd)\n",
606 *basep + offset, bmap->bitmap[i],
607 pbmmask, bmmask, size, psize);
608#endif
609
91abd410 610 if ((bmap->bitmap[i] & pbmmask) == 0) {
fdf62707
MD
611 error = hammer2_io_newq(hmp,
612 (*basep + (offset & ~pmask)) |
613 pradix,
614 psize, &dio);
615 hammer2_io_bqrelse(&dio);
93f3933a
MD
616 }
617 }
618
619#if 0
620 /*
621 * When initializing a new inode segment also attempt to initialize
622 * an adjacent segment. Be careful not to index beyond the array
623 * bounds.
624 *
625 * We do this to try to localize inode accesses to improve
626 * directory scan rates. XXX doesn't improve scan rates.
627 */
628 if (size == HAMMER2_INODE_BYTES) {
629 if (n & 1) {
630 if (bmap[-1].radix == 0 && bmap[-1].avail)
631 bmap[-1].radix = radix;
632 } else {
633 if (bmap[1].radix == 0 && bmap[1].avail)
634 bmap[1].radix = radix;
635 }
636 }
637#endif
638
639 /*
640 * Adjust the linear iterator, set the radix if necessary (might as
641 * well just set it unconditionally), adjust *basep to return the
642 * allocated data offset.
643 */
644 bmap->bitmap[i] |= bmmask;
91abd410 645 bmap->class = class;
93f3933a
MD
646 bmap->avail -= size;
647 *basep += offset;
648
9f604b01 649 hammer2_voldata_lock(hmp);
50456506 650 hammer2_voldata_modify(hmp);
9f604b01 651 hmp->voldata.allocator_free -= size; /* XXX */
50456506 652 hammer2_voldata_unlock(hmp);
9f604b01 653
93f3933a
MD
654 return(0);
655}
656
657static
658void
a5913bdf
MD
659hammer2_freemap_init(hammer2_trans_t *trans, hammer2_mount_t *hmp,
660 hammer2_key_t key, hammer2_chain_t *chain)
93f3933a
MD
661{
662 hammer2_off_t l1size;
663 hammer2_off_t lokey;
664 hammer2_off_t hikey;
665 hammer2_bmap_data_t *bmap;
666 int count;
667
668 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
669
670 /*
671 * Calculate the portion of the 2GB map that should be initialized
672 * as free. Portions below or after will be initialized as allocated.
673 * SEGMASK-align the areas so we don't have to worry about sub-scans
674 * or endianess when using memset.
675 *
676 * (1) Ensure that all statically allocated space from newfs_hammer2
677 * is marked allocated.
678 *
679 * (2) Ensure that the reserved area is marked allocated (typically
680 * the first 4MB of the 2GB area being represented).
681 *
682 * (3) Ensure that any trailing space at the end-of-volume is marked
683 * allocated.
684 *
685 * WARNING! It is possible for lokey to be larger than hikey if the
686 * entire 2GB segment is within the static allocation.
687 */
a5913bdf 688 lokey = (hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
93f3933a
MD
689 ~HAMMER2_SEGMASK64;
690
691 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
692 HAMMER2_ZONE_SEG64) {
693 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
694 HAMMER2_ZONE_SEG64;
695 }
696
697 hikey = key + H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
a5913bdf
MD
698 if (hikey > hmp->voldata.volu_size) {
699 hikey = hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
93f3933a
MD
700 }
701
702 chain->bref.check.freemap.avail =
703 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
704 bmap = &chain->data->bmdata[0];
705
706 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
707 if (key < lokey || key >= hikey) {
708 memset(bmap->bitmap, -1,
709 sizeof(bmap->bitmap));
710 bmap->avail = 0;
2c6f594d 711 bmap->linear = HAMMER2_SEGSIZE;
93f3933a
MD
712 chain->bref.check.freemap.avail -=
713 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
714 } else {
715 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
716 }
717 key += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
718 ++bmap;
719 }
720}
721
722/*
723 * The current Level 1 freemap has been exhausted, iterate to the next
724 * one, return ENOSPC if no freemaps remain.
725 *
726 * XXX this should rotate back to the beginning to handle freed-up space
727 * XXX or use intermediate entries to locate free space. TODO
728 */
1a7cfe5a
MD
729static int
730hammer2_freemap_iterate(hammer2_trans_t *trans, hammer2_chain_t **parentp,
91abd410 731 hammer2_chain_t **chainp, hammer2_fiterate_t *iter)
1a7cfe5a 732{
a5913bdf
MD
733 hammer2_mount_t *hmp = (*parentp)->hmp;
734
91abd410
MD
735 iter->bnext &= ~(H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
736 iter->bnext += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
737 if (iter->bnext >= hmp->voldata.volu_size) {
738 iter->bnext = 0;
739 if (++iter->loops == 2)
740 return (ENOSPC);
741 }
1a7cfe5a 742 return(EAGAIN);
5c23d7f1
MD
743}
744
91abd410 745/*
877eacb6
MD
746 * Adjust the bit-pattern for data in the freemap bitmap according to
747 * (how). This code is called from on-mount recovery to fixup (mark
748 * as allocated) blocks whos freemap upates might not have been committed
749 * in the last crash and is used by the bulk freemap scan to stage frees.
91abd410
MD
750 *
751 * XXX currently disabled when how == 0 (the normal real-time case). At
752 * the moment we depend on the bulk freescan to actually free blocks. It
753 * will still call this routine with a non-zero how to stage possible frees
754 * and to do the actual free.
755 */
004f88b4 756void
10136ab6
MD
757hammer2_freemap_adjust(hammer2_trans_t *trans, hammer2_mount_t *hmp,
758 hammer2_blockref_t *bref, int how)
004f88b4 759{
91abd410
MD
760 hammer2_off_t data_off = bref->data_off;
761 hammer2_chain_t *chain;
762 hammer2_chain_t *parent;
763 hammer2_bmap_data_t *bmap;
764 hammer2_key_t key;
1897c66e 765 hammer2_key_t key_dummy;
91abd410
MD
766 hammer2_off_t l0size;
767 hammer2_off_t l1size;
768 hammer2_off_t l1mask;
769 uint32_t *bitmap;
770 const uint32_t bmmask00 = 0;
771 uint32_t bmmask01;
772 uint32_t bmmask10;
773 uint32_t bmmask11;
774 size_t bytes;
775 uint16_t class;
004f88b4 776 int radix;
91abd410
MD
777 int start;
778 int count;
779 int modified = 0;
1897c66e 780 int cache_index = -1;
10136ab6 781 int error;
278ab2b2 782 int ddflag;
004f88b4 783
004f88b4
MD
784 radix = (int)data_off & HAMMER2_OFF_MASK_RADIX;
785 data_off &= ~HAMMER2_OFF_MASK_RADIX;
50456506 786 KKASSERT(radix <= HAMMER2_RADIX_MAX);
91abd410
MD
787
788 bytes = (size_t)1 << radix;
789 class = (bref->type << 8) | hammer2_devblkradix(radix);
790
355d67fc 791 /*
10136ab6
MD
792 * We can't adjust thre freemap for data allocations made by
793 * newfs_hammer2.
355d67fc
MD
794 */
795 if (data_off < hmp->voldata.allocator_beg)
796 return;
355d67fc 797
10136ab6 798 KKASSERT((data_off & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
a7720be7 799
91abd410
MD
800 /*
801 * Lookup the level1 freemap chain. The chain must exist.
802 */
803 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL1_RADIX);
804 l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
805 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
806 l1mask = l1size - 1;
807
808 parent = &hmp->fchain;
809 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
810
1897c66e
MD
811 chain = hammer2_chain_lookup(&parent, &key_dummy, key, key + l1mask,
812 &cache_index,
91abd410 813 HAMMER2_LOOKUP_ALWAYS |
278ab2b2 814 HAMMER2_LOOKUP_MATCHIND, &ddflag);
10136ab6
MD
815
816 /*
817 * Stop early if we are trying to free something but no leaf exists.
818 */
819 if (chain == NULL && how != HAMMER2_FREEMAP_DORECOVER) {
820 kprintf("hammer2_freemap_adjust: %016jx: no chain\n",
91abd410 821 (intmax_t)bref->data_off);
623d43d4 822 goto done;
91abd410 823 }
91abd410
MD
824
825 /*
10136ab6
MD
826 * Create any missing leaf(s) if we are doing a recovery (marking
827 * the block(s) as being allocated instead of being freed). Be sure
828 * to initialize the auxillary freemap tracking info in the
829 * bref.check.freemap structure.
91abd410 830 */
10136ab6 831 if (chain == NULL && how == HAMMER2_FREEMAP_DORECOVER) {
50456506 832 error = hammer2_chain_create(trans, &parent, &chain, hmp->spmp,
10136ab6
MD
833 key, HAMMER2_FREEMAP_LEVEL1_RADIX,
834 HAMMER2_BREF_TYPE_FREEMAP_LEAF,
b3659de2
MD
835 HAMMER2_FREEMAP_LEVELN_PSIZE,
836 0);
1fca819a
MD
837
838 if (hammer2_debug & 0x0040) {
839 kprintf("fixup create chain %p %016jx:%d\n",
840 chain, chain->bref.key, chain->bref.keybits);
841 }
10136ab6
MD
842
843 if (error == 0) {
da6f36f4 844 hammer2_chain_modify(trans, chain, 0);
10136ab6
MD
845 bzero(&chain->data->bmdata[0],
846 HAMMER2_FREEMAP_LEVELN_PSIZE);
847 chain->bref.check.freemap.bigmask = (uint32_t)-1;
848 chain->bref.check.freemap.avail = l1size;
849 /* bref.methods should already be inherited */
91abd410 850
10136ab6
MD
851 hammer2_freemap_init(trans, hmp, key, chain);
852 }
853 /* XXX handle error */
854 }
855
8138a154
MD
856#if FREEMAP_DEBUG
857 kprintf("FREEMAP ADJUST TYPE %d %016jx/%d DATA_OFF=%016jx\n",
858 chain->bref.type, chain->bref.key,
859 chain->bref.keybits, chain->bref.data_off);
860#endif
861
10136ab6
MD
862 /*
863 * Calculate the bitmask (runs in 2-bit pairs).
864 */
91abd410
MD
865 start = ((int)(data_off >> HAMMER2_FREEMAP_BLOCK_RADIX) & 15) * 2;
866 bmmask01 = 1 << start;
867 bmmask10 = 2 << start;
868 bmmask11 = 3 << start;
869
870 /*
10136ab6
MD
871 * Fixup the bitmap. Partial blocks cannot be fully freed unless
872 * a bulk scan is able to roll them up.
91abd410
MD
873 */
874 if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) {
875 count = 1;
10136ab6
MD
876 if (how == HAMMER2_FREEMAP_DOREALFREE)
877 how = HAMMER2_FREEMAP_DOMAYFREE;
91abd410
MD
878 } else {
879 count = 1 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
880 }
004f88b4 881
10136ab6
MD
882 /*
883 * [re]load the bmap and bitmap pointers. Each bmap entry covers
884 * a 2MB swath. The bmap itself (LEVEL1) covers 2GB.
8138a154
MD
885 *
886 * Be sure to reset the linear iterator to ensure that the adjustment
887 * is not ignored.
10136ab6
MD
888 */
889again:
890 bmap = &chain->data->bmdata[(int)(data_off >> HAMMER2_SEGRADIX) &
891 (HAMMER2_FREEMAP_COUNT - 1)];
892 bitmap = &bmap->bitmap[(int)(data_off >> (HAMMER2_SEGRADIX - 3)) & 7];
a71db85d
MD
893
894 if (modified)
895 bmap->linear = 0;
10136ab6 896
91abd410
MD
897 while (count) {
898 KKASSERT(bmmask11);
10136ab6
MD
899 if (how == HAMMER2_FREEMAP_DORECOVER) {
900 /*
901 * Recovery request, mark as allocated.
902 */
903 if ((*bitmap & bmmask11) != bmmask11) {
904 if (modified == 0) {
da6f36f4 905 hammer2_chain_modify(trans, chain, 0);
10136ab6
MD
906 modified = 1;
907 goto again;
908 }
909 if ((*bitmap & bmmask11) == bmmask00)
910 bmap->avail -= 1 << radix;
911 if (bmap->class == 0)
912 bmap->class = class;
913 *bitmap |= bmmask11;
1fca819a
MD
914 if (hammer2_debug & 0x0040) {
915 kprintf("hammer2_freemap_recover: "
916 "fixup type=%02x "
917 "block=%016jx/%zd\n",
918 bref->type, data_off, bytes);
919 }
10136ab6
MD
920 } else {
921 /*
922 kprintf("hammer2_freemap_recover: good "
923 "type=%02x block=%016jx/%zd\n",
924 bref->type, data_off, bytes);
925 */
926 }
927 } else if ((*bitmap & bmmask11) == bmmask11) {
928 /*
929 * Mayfree/Realfree request and bitmap is currently
930 * marked as being fully allocated.
931 */
91abd410 932 if (!modified) {
da6f36f4 933 hammer2_chain_modify(trans, chain, 0);
91abd410 934 modified = 1;
10136ab6 935 goto again;
91abd410 936 }
10136ab6 937 if (how == HAMMER2_FREEMAP_DOREALFREE)
91abd410
MD
938 *bitmap &= ~bmmask11;
939 else
940 *bitmap = (*bitmap & ~bmmask11) | bmmask10;
941 } else if ((*bitmap & bmmask11) == bmmask10) {
10136ab6
MD
942 /*
943 * Mayfree/Realfree request and bitmap is currently
944 * marked as being possibly freeable.
945 */
946 if (how == HAMMER2_FREEMAP_DOREALFREE) {
91abd410 947 if (!modified) {
da6f36f4 948 hammer2_chain_modify(trans, chain, 0);
91abd410 949 modified = 1;
10136ab6 950 goto again;
91abd410
MD
951 }
952 *bitmap &= ~bmmask11;
953 }
10136ab6
MD
954 } else {
955 /*
956 * 01 - Not implemented, currently illegal state
957 * 00 - Not allocated at all, illegal free.
958 */
959 panic("hammer2_freemap_adjust: "
960 "Illegal state %08x(%08x)",
961 *bitmap, *bitmap & bmmask11);
91abd410
MD
962 }
963 --count;
964 bmmask01 <<= 2;
965 bmmask10 <<= 2;
966 bmmask11 <<= 2;
967 }
10136ab6 968 if (how == HAMMER2_FREEMAP_DOREALFREE && modified) {
91abd410
MD
969 bmap->avail += 1 << radix;
970 KKASSERT(bmap->avail <= HAMMER2_SEGSIZE);
971 if (bmap->avail == HAMMER2_SEGSIZE &&
972 bmap->bitmap[0] == 0 &&
973 bmap->bitmap[1] == 0 &&
974 bmap->bitmap[2] == 0 &&
975 bmap->bitmap[3] == 0 &&
976 bmap->bitmap[4] == 0 &&
977 bmap->bitmap[5] == 0 &&
978 bmap->bitmap[6] == 0 &&
979 bmap->bitmap[7] == 0) {
980 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL0_RADIX);
981 kprintf("Freeseg %016jx\n", (intmax_t)key);
982 bmap->class = 0;
983 }
004f88b4 984 }
004f88b4 985
91abd410
MD
986 /*
987 * chain->bref.check.freemap.bigmask (XXX)
10136ab6
MD
988 *
989 * Setting bigmask is a hint to the allocation code that there might
990 * be something allocatable. We also set this in recovery... it
991 * doesn't hurt and we might want to use the hint for other validation
992 * operations later on.
91abd410
MD
993 */
994 if (modified)
995 chain->bref.check.freemap.bigmask |= 1 << radix;
1a7cfe5a 996
91abd410 997 hammer2_chain_unlock(chain);
623d43d4 998done:
91abd410 999 hammer2_chain_unlock(parent);
5c23d7f1 1000}
bca9f8e6
MD
1001
1002/*
1003 * Validate the freemap, in three stages.
1004 *
1005 * stage-1 ALLOCATED -> POSSIBLY FREE
1006 * POSSIBLY FREE -> POSSIBLY FREE (type corrected)
1007 *
1008 * This transitions bitmap entries from ALLOCATED to POSSIBLY FREE.
1009 * The POSSIBLY FREE state does not mean that a block is actually free
1010 * and may be transitioned back to ALLOCATED in stage-2.
1011 *
1012 * This is typically done during normal filesystem operations when
1013 * something is deleted or a block is replaced.
1014 *
1015 * This is done by bulkfree in-bulk after a memory-bounded meta-data
1016 * scan to try to determine what might be freeable.
1017 *
1018 * This can be done unconditionally through a freemap scan when the
1019 * intention is to brute-force recover the proper state of the freemap.
1020 *
1021 * stage-2 POSSIBLY FREE -> ALLOCATED (scan metadata topology)
1022 *
1023 * This is done by bulkfree during a meta-data scan to ensure that
1024 * all blocks still actually allocated by the filesystem are marked
1025 * as such.
1026 *
1027 * NOTE! Live filesystem transitions to POSSIBLY FREE can occur while
1028 * the bulkfree stage-2 and stage-3 is running. The live filesystem
1029 * will use the alternative POSSIBLY FREE type (2) to prevent
1030 * stage-3 from improperly transitioning unvetted possibly-free
1031 * blocks to FREE.
1032 *
1033 * stage-3 POSSIBLY FREE (type 1) -> FREE (scan freemap)
1034 *
1035 * This is done by bulkfree to finalize POSSIBLY FREE states.
1036 *
1037 */