sys/vfs/hammer2: Remove obsolete code/comment in freemap
[dragonfly.git] / sys / vfs / hammer2 / hammer2_freemap.c
blob842e41e43cf1f1b0e79b5cdbbb39b787ae78df72
1 /*
2 * Copyright (c) 2011-2018 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
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.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/mount.h>
41 #include "hammer2.h"
43 #define FREEMAP_DEBUG 0
45 struct hammer2_fiterate {
46 hammer2_off_t bpref;
47 hammer2_off_t bnext;
48 int loops;
49 int relaxed;
52 typedef struct hammer2_fiterate hammer2_fiterate_t;
54 static int hammer2_freemap_try_alloc(hammer2_chain_t **parentp,
55 hammer2_blockref_t *bref, int radix,
56 hammer2_fiterate_t *iter, hammer2_tid_t mtid);
57 static void hammer2_freemap_init(hammer2_dev_t *hmp,
58 hammer2_key_t key, hammer2_chain_t *chain);
59 static int hammer2_bmap_alloc(hammer2_dev_t *hmp,
60 hammer2_bmap_data_t *bmap, uint16_t class,
61 int n, int sub_key, int radix, hammer2_key_t *basep);
62 static int hammer2_freemap_iterate(hammer2_chain_t **parentp,
63 hammer2_chain_t **chainp,
64 hammer2_fiterate_t *iter);
67 * Calculate the device offset for the specified FREEMAP_NODE or FREEMAP_LEAF
68 * bref. Return a combined media offset and physical size radix. Freemap
69 * chains use fixed storage offsets in the 4MB reserved area at the
70 * beginning of each 1GB zone.
72 * Rotate between eight possibilities. Theoretically this means we have seven
73 * good freemaps in case of a crash which we can use as a base for the fixup
74 * scan at mount-time.
76 static
77 int
78 hammer2_freemap_reserve(hammer2_chain_t *chain, int radix)
80 hammer2_blockref_t *bref = &chain->bref;
81 hammer2_off_t off;
82 int index;
83 int index_inc;
84 size_t bytes;
87 * Physical allocation size.
89 bytes = (size_t)1 << radix;
92 * Calculate block selection index 0..7 of current block. If this
93 * is the first allocation of the block (verses a modification of an
94 * existing block), we use index 0, otherwise we use the next rotating
95 * index.
97 if ((bref->data_off & ~HAMMER2_OFF_MASK_RADIX) == 0) {
98 index = 0;
99 } else {
100 off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX &
101 HAMMER2_SEGMASK;
102 off = off / HAMMER2_PBUFSIZE;
103 KKASSERT(off >= HAMMER2_ZONE_FREEMAP_00 &&
104 off < HAMMER2_ZONE_FREEMAP_END);
105 index = (int)(off - HAMMER2_ZONE_FREEMAP_00) /
106 HAMMER2_ZONE_FREEMAP_INC;
107 KKASSERT(index >= 0 && index < HAMMER2_NFREEMAPS);
108 if (++index == HAMMER2_NFREEMAPS)
109 index = 0;
113 * Calculate the block offset of the reserved block. This will
114 * point into the 4MB reserved area at the base of the appropriate
115 * 2GB zone, once added to the FREEMAP_x selection above.
117 index_inc = index * HAMMER2_ZONE_FREEMAP_INC;
119 switch(bref->keybits) {
120 /* case HAMMER2_FREEMAP_LEVEL6_RADIX: not applicable */
121 case HAMMER2_FREEMAP_LEVEL5_RADIX: /* 4EB */
122 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
123 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
124 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL5_RADIX) +
125 (index_inc + HAMMER2_ZONE_FREEMAP_00 +
126 HAMMER2_ZONEFM_LEVEL5) * HAMMER2_PBUFSIZE;
127 break;
128 case HAMMER2_FREEMAP_LEVEL4_RADIX: /* 16PB */
129 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
130 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
131 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL4_RADIX) +
132 (index_inc + HAMMER2_ZONE_FREEMAP_00 +
133 HAMMER2_ZONEFM_LEVEL4) * HAMMER2_PBUFSIZE;
134 break;
135 case HAMMER2_FREEMAP_LEVEL3_RADIX: /* 64TB */
136 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
137 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
138 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL3_RADIX) +
139 (index_inc + HAMMER2_ZONE_FREEMAP_00 +
140 HAMMER2_ZONEFM_LEVEL3) * HAMMER2_PBUFSIZE;
141 break;
142 case HAMMER2_FREEMAP_LEVEL2_RADIX: /* 256GB */
143 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
144 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
145 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL2_RADIX) +
146 (index_inc + HAMMER2_ZONE_FREEMAP_00 +
147 HAMMER2_ZONEFM_LEVEL2) * HAMMER2_PBUFSIZE;
148 break;
149 case HAMMER2_FREEMAP_LEVEL1_RADIX: /* 1GB */
150 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
151 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
152 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
153 (index_inc + HAMMER2_ZONE_FREEMAP_00 +
154 HAMMER2_ZONEFM_LEVEL1) * HAMMER2_PBUFSIZE;
155 break;
156 default:
157 panic("freemap: bad radix(2) %p %d\n", bref, bref->keybits);
158 /* NOT REACHED */
159 off = (hammer2_off_t)-1;
160 break;
162 bref->data_off = off | radix;
163 #if FREEMAP_DEBUG
164 kprintf("FREEMAP BLOCK TYPE %d %016jx/%d DATA_OFF=%016jx\n",
165 bref->type, bref->key, bref->keybits, bref->data_off);
166 #endif
167 return (0);
171 * Normal freemap allocator
173 * Use available hints to allocate space using the freemap. Create missing
174 * freemap infrastructure on-the-fly as needed (including marking initial
175 * allocations using the iterator as allocated, instantiating new 2GB zones,
176 * and dealing with the end-of-media edge case).
178 * ip and bpref are only used as a heuristic to determine locality of
179 * reference. bref->key may also be used heuristically.
181 * This function is a NOP if bytes is 0.
184 hammer2_freemap_alloc(hammer2_chain_t *chain, size_t bytes)
186 hammer2_dev_t *hmp = chain->hmp;
187 hammer2_blockref_t *bref = &chain->bref;
188 hammer2_chain_t *parent;
189 hammer2_tid_t mtid;
190 int radix;
191 int error;
192 unsigned int hindex;
193 hammer2_fiterate_t iter;
196 * If allocating or downsizing to zero we just get rid of whatever
197 * data_off we had.
199 if (bytes == 0) {
200 chain->bref.data_off = 0;
201 return 0;
204 KKASSERT(hmp->spmp);
205 mtid = hammer2_trans_sub(hmp->spmp);
208 * Validate the allocation size. It must be a power of 2.
210 * For now require that the caller be aware of the minimum
211 * allocation (1K).
213 radix = hammer2_getradix(bytes);
214 KKASSERT((size_t)1 << radix == bytes);
216 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
217 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
219 * Freemap blocks themselves are assigned from the reserve
220 * area, not allocated from the freemap.
222 error = hammer2_freemap_reserve(chain, radix);
224 return error;
227 KKASSERT(bytes >= HAMMER2_ALLOC_MIN && bytes <= HAMMER2_ALLOC_MAX);
230 * Heuristic tracking index. We would like one for each distinct
231 * bref type if possible. heur_freemap[] has room for two classes
232 * for each type. At a minimum we have to break-up our heuristic
233 * by device block sizes.
235 hindex = HAMMER2_PBUFRADIX - HAMMER2_LBUFRADIX;
236 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR_NRADIX);
237 hindex += bref->type * HAMMER2_FREEMAP_HEUR_NRADIX;
238 hindex &= HAMMER2_FREEMAP_HEUR_TYPES * HAMMER2_FREEMAP_HEUR_NRADIX - 1;
239 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR_SIZE);
241 iter.bpref = hmp->heur_freemap[hindex];
242 iter.relaxed = hmp->freemap_relaxed;
245 * Make sure bpref is in-bounds. It's ok if bpref covers a zone's
246 * reserved area, the try code will iterate past it.
248 if (iter.bpref > hmp->total_size)
249 iter.bpref = hmp->total_size - 1;
252 * Iterate the freemap looking for free space before and after.
254 parent = &hmp->fchain;
255 hammer2_chain_ref(parent);
256 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
257 error = HAMMER2_ERROR_EAGAIN;
258 iter.bnext = iter.bpref;
259 iter.loops = 0;
261 while (error == HAMMER2_ERROR_EAGAIN) {
262 error = hammer2_freemap_try_alloc(&parent, bref, radix,
263 &iter, mtid);
265 hmp->freemap_relaxed |= iter.relaxed; /* heuristical, SMP race ok */
266 hmp->heur_freemap[hindex] = iter.bnext;
267 hammer2_chain_unlock(parent);
268 hammer2_chain_drop(parent);
270 return (error);
273 static int
274 hammer2_freemap_try_alloc(hammer2_chain_t **parentp,
275 hammer2_blockref_t *bref, int radix,
276 hammer2_fiterate_t *iter, hammer2_tid_t mtid)
278 hammer2_dev_t *hmp = (*parentp)->hmp;
279 hammer2_off_t l0size;
280 hammer2_off_t l1size;
281 hammer2_off_t l1mask;
282 hammer2_key_t key_dummy;
283 hammer2_chain_t *chain;
284 hammer2_off_t key;
285 size_t bytes;
286 uint16_t class;
287 int error;
290 * Calculate the number of bytes being allocated, the number
291 * of contiguous bits of bitmap being allocated, and the bitmap
292 * mask.
294 * WARNING! cpu hardware may mask bits == 64 -> 0 and blow up the
295 * mask calculation.
297 bytes = (size_t)1 << radix;
298 class = (bref->type << 8) | HAMMER2_PBUFRADIX;
301 * Lookup the level1 freemap chain, creating and initializing one
302 * if necessary. Intermediate levels will be created automatically
303 * when necessary by hammer2_chain_create().
305 key = H2FMBASE(iter->bnext, HAMMER2_FREEMAP_LEVEL1_RADIX);
306 l0size = HAMMER2_FREEMAP_LEVEL0_SIZE;
307 l1size = HAMMER2_FREEMAP_LEVEL1_SIZE;
308 l1mask = l1size - 1;
310 chain = hammer2_chain_lookup(parentp, &key_dummy, key, key + l1mask,
311 &error,
312 HAMMER2_LOOKUP_ALWAYS |
313 HAMMER2_LOOKUP_MATCHIND);
315 if (chain == NULL) {
317 * Create the missing leaf, be sure to initialize
318 * the auxillary freemap tracking information in
319 * the bref.check.freemap structure.
321 #if 0
322 kprintf("freemap create L1 @ %016jx bpref %016jx\n",
323 key, iter->bpref);
324 #endif
325 error = hammer2_chain_create(parentp, &chain, NULL, hmp->spmp,
326 HAMMER2_METH_DEFAULT,
327 key, HAMMER2_FREEMAP_LEVEL1_RADIX,
328 HAMMER2_BREF_TYPE_FREEMAP_LEAF,
329 HAMMER2_FREEMAP_LEVELN_PSIZE,
330 mtid, 0, 0);
331 KKASSERT(error == 0);
332 if (error == 0) {
333 hammer2_chain_modify(chain, mtid, 0, 0);
334 bzero(&chain->data->bmdata[0],
335 HAMMER2_FREEMAP_LEVELN_PSIZE);
336 chain->bref.check.freemap.bigmask = (uint32_t)-1;
337 chain->bref.check.freemap.avail = l1size;
338 /* bref.methods should already be inherited */
340 hammer2_freemap_init(hmp, key, chain);
342 } else if (chain->error) {
344 * Error during lookup.
346 kprintf("hammer2_freemap_try_alloc: %016jx: error %s\n",
347 (intmax_t)bref->data_off,
348 hammer2_error_str(chain->error));
349 error = HAMMER2_ERROR_EIO;
350 } else if ((chain->bref.check.freemap.bigmask &
351 ((size_t)1 << radix)) == 0) {
353 * Already flagged as not having enough space
355 error = HAMMER2_ERROR_ENOSPC;
356 } else {
358 * Modify existing chain to setup for adjustment.
360 hammer2_chain_modify(chain, mtid, 0, 0);
364 * Scan 4MB entries.
366 if (error == 0) {
367 hammer2_bmap_data_t *bmap;
368 hammer2_key_t base_key;
369 int count;
370 int start;
371 int n;
373 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
374 start = (int)((iter->bnext - key) >>
375 HAMMER2_FREEMAP_LEVEL0_RADIX);
376 KKASSERT(start >= 0 && start < HAMMER2_FREEMAP_COUNT);
377 hammer2_chain_modify(chain, mtid, 0, 0);
379 error = HAMMER2_ERROR_ENOSPC;
380 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
381 int availchk;
383 if (start + count >= HAMMER2_FREEMAP_COUNT &&
384 start - count < 0) {
385 break;
389 * Calculate bmap pointer from thart starting index
390 * forwards.
392 * NOTE: bmap pointer is invalid if n >= FREEMAP_COUNT.
394 n = start + count;
395 bmap = &chain->data->bmdata[n];
397 if (n >= HAMMER2_FREEMAP_COUNT) {
398 availchk = 0;
399 } else if (bmap->avail) {
400 availchk = 1;
401 } else if (radix < HAMMER2_FREEMAP_BLOCK_RADIX &&
402 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK)) {
403 availchk = 1;
404 } else {
405 availchk = 0;
409 * Try to allocate from a matching freemap class
410 * superblock. If we are in relaxed mode we allocate
411 * from any freemap class superblock.
413 if (availchk &&
414 (bmap->class == 0 || bmap->class == class ||
415 iter->relaxed)) {
416 base_key = key + n * l0size;
417 error = hammer2_bmap_alloc(hmp, bmap,
418 class, n,
419 (int)bref->key,
420 radix,
421 &base_key);
422 if (error != HAMMER2_ERROR_ENOSPC) {
423 key = base_key;
424 break;
429 * Calculate bmap pointer from the starting index
430 * backwards (locality).
432 * Must recalculate after potentially having called
433 * hammer2_bmap_alloc() above in case chain was
434 * reallocated.
436 * NOTE: bmap pointer is invalid if n < 0.
438 n = start - count;
439 bmap = &chain->data->bmdata[n];
440 if (n < 0) {
441 availchk = 0;
442 } else if (bmap->avail) {
443 availchk = 1;
444 } else if (radix < HAMMER2_FREEMAP_BLOCK_RADIX &&
445 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK)) {
446 availchk = 1;
447 } else {
448 availchk = 0;
452 * Try to allocate from a matching freemap class
453 * superblock. If we are in relaxed mode we allocate
454 * from any freemap class superblock.
456 if (availchk &&
457 (bmap->class == 0 || bmap->class == class ||
458 iter->relaxed)) {
459 base_key = key + n * l0size;
460 error = hammer2_bmap_alloc(hmp, bmap,
461 class, n,
462 (int)bref->key,
463 radix,
464 &base_key);
465 if (error != HAMMER2_ERROR_ENOSPC) {
466 key = base_key;
467 break;
473 * We only know for sure that we can clear the bitmap bit
474 * if we scanned the entire array (start == 0) in relaxed
475 * mode.
477 if (error == HAMMER2_ERROR_ENOSPC &&
478 start == 0 &&
479 iter->relaxed)
481 chain->bref.check.freemap.bigmask &=
482 (uint32_t)~((size_t)1 << radix);
484 /* XXX also scan down from original count */
487 if (error == 0) {
489 * Assert validity. Must be beyond the static allocator used
490 * by newfs_hammer2 (and thus also beyond the aux area),
491 * not go past the volume size, and must not be in the
492 * reserved segment area for a zone.
494 KKASSERT(key >= hmp->voldata.allocator_beg &&
495 key + bytes <= hmp->total_size);
496 KKASSERT((key & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
497 bref->data_off = key | radix;
500 * Record dedupability. The dedup bits are cleared
501 * when bulkfree transitions the freemap from 11->10,
502 * and asserted to be clear on the 10->00 transition.
504 * We must record the bitmask with the chain locked
505 * at the time we set the allocation bits to avoid
506 * racing a bulkfree.
508 if (bref->type == HAMMER2_BREF_TYPE_DATA)
509 hammer2_io_dedup_set(hmp, bref);
510 #if 0
511 kprintf("alloc cp=%p %016jx %016jx using %016jx\n",
512 chain,
513 bref->key, bref->data_off, chain->bref.data_off);
514 #endif
515 } else if (error == HAMMER2_ERROR_ENOSPC) {
517 * Return EAGAIN with next iteration in iter->bnext, or
518 * return ENOSPC if the allocation map has been exhausted.
520 error = hammer2_freemap_iterate(parentp, &chain, iter);
524 * Cleanup
526 if (chain) {
527 hammer2_chain_unlock(chain);
528 hammer2_chain_drop(chain);
530 return (error);
534 * Allocate (1<<radix) bytes from the bmap whos base data offset is (*basep).
536 * If the linear iterator is mid-block we use it directly (the bitmap should
537 * already be marked allocated), otherwise we search for a block in the
538 * bitmap that fits the allocation request.
540 * A partial bitmap allocation sets the minimum bitmap granularity (16KB)
541 * to fully allocated and adjusts the linear allocator to allow the
542 * remaining space to be allocated.
544 * sub_key is the lower 32 bits of the chain->bref.key for the chain whos
545 * bref is being allocated. If the radix represents an allocation >= 16KB
546 * (aka HAMMER2_FREEMAP_BLOCK_RADIX) we try to use this key to select the
547 * blocks directly out of the bmap.
549 static
551 hammer2_bmap_alloc(hammer2_dev_t *hmp, hammer2_bmap_data_t *bmap,
552 uint16_t class, int n, int sub_key,
553 int radix, hammer2_key_t *basep)
555 size_t size;
556 size_t bgsize;
557 int bmradix;
558 hammer2_bitmap_t bmmask;
559 int offset;
560 int i;
561 int j;
564 * Take into account 2-bits per block when calculating bmradix.
566 size = (size_t)1 << radix;
568 if (radix <= HAMMER2_FREEMAP_BLOCK_RADIX) {
569 bmradix = 2;
570 /* (16K) 2 bits per allocation block */
571 } else {
572 bmradix = (hammer2_bitmap_t)2 <<
573 (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
574 /* (32K-256K) 4, 8, 16, 32 bits per allocation block */
578 * Use the linear iterator to pack small allocations, otherwise
579 * fall-back to finding a free 16KB chunk. The linear iterator
580 * is only valid when *NOT* on a freemap chunking boundary (16KB).
581 * If it is the bitmap must be scanned. It can become invalid
582 * once we pack to the boundary. We adjust it after a bitmap
583 * allocation only for sub-16KB allocations (so the perfectly good
584 * previous value can still be used for fragments when 16KB+
585 * allocations are made inbetween fragmentary allocations).
587 * Beware of hardware artifacts when bmradix == 64 (intermediate
588 * result can wind up being '1' instead of '0' if hardware masks
589 * bit-count & 63).
591 * NOTE: j needs to be even in the j= calculation. As an artifact
592 * of the /2 division, our bitmask has to clear bit 0.
594 * NOTE: TODO this can leave little unallocatable fragments lying
595 * around.
597 if (((uint32_t)bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) + size <=
598 HAMMER2_FREEMAP_BLOCK_SIZE &&
599 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) &&
600 bmap->linear < HAMMER2_SEGSIZE) {
602 * Use linear iterator if it is not block-aligned to avoid
603 * wasting space.
605 * Calculate the bitmapq[] index (i) and calculate the
606 * shift count within the 64-bit bitmapq[] entry.
608 * The freemap block size is 16KB, but each bitmap
609 * entry is two bits so use a little trick to get
610 * a (j) shift of 0, 2, 4, ... 62 in 16KB chunks.
612 KKASSERT(bmap->linear >= 0 &&
613 bmap->linear + size <= HAMMER2_SEGSIZE &&
614 (bmap->linear & (HAMMER2_ALLOC_MIN - 1)) == 0);
615 offset = bmap->linear;
616 i = offset / (HAMMER2_SEGSIZE / HAMMER2_BMAP_ELEMENTS);
617 j = (offset / (HAMMER2_FREEMAP_BLOCK_SIZE / 2)) & 62;
618 bmmask = (bmradix == HAMMER2_BMAP_BITS_PER_ELEMENT) ?
619 HAMMER2_BMAP_ALLONES :
620 ((hammer2_bitmap_t)1 << bmradix) - 1;
621 bmmask <<= j;
622 bmap->linear = offset + size;
623 } else {
625 * Try to index a starting point based on sub_key. This
626 * attempts to restore sequential block ordering on-disk
627 * whenever possible, even if data is committed out of
628 * order.
630 * i - Index bitmapq[], full data range represented is
631 * HAMMER2_BMAP_SIZE.
633 * j - Index within bitmapq[i], full data range represented is
634 * HAMMER2_BMAP_INDEX_SIZE.
636 * WARNING!
638 i = -1;
639 j = -1;
641 switch(class >> 8) {
642 case HAMMER2_BREF_TYPE_DATA:
643 if (radix >= HAMMER2_FREEMAP_BLOCK_RADIX) {
644 i = (sub_key & HAMMER2_BMAP_MASK) /
645 (HAMMER2_BMAP_SIZE / HAMMER2_BMAP_ELEMENTS);
646 j = (sub_key & HAMMER2_BMAP_INDEX_MASK) /
647 (HAMMER2_BMAP_INDEX_SIZE /
648 HAMMER2_BMAP_BLOCKS_PER_ELEMENT);
649 j = j * 2;
651 break;
652 case HAMMER2_BREF_TYPE_INODE:
653 break;
654 default:
655 break;
657 if (i >= 0) {
658 KKASSERT(i < HAMMER2_BMAP_ELEMENTS &&
659 j < 2 * HAMMER2_BMAP_BLOCKS_PER_ELEMENT);
660 KKASSERT(j + bmradix <= HAMMER2_BMAP_BITS_PER_ELEMENT);
661 bmmask = (bmradix == HAMMER2_BMAP_BITS_PER_ELEMENT) ?
662 HAMMER2_BMAP_ALLONES :
663 ((hammer2_bitmap_t)1 << bmradix) - 1;
664 bmmask <<= j;
666 if ((bmap->bitmapq[i] & bmmask) == 0)
667 goto success;
671 * General element scan.
673 * WARNING: (j) is iterating a bit index (by 2's)
675 for (i = 0; i < HAMMER2_BMAP_ELEMENTS; ++i) {
676 bmmask = (bmradix == HAMMER2_BMAP_BITS_PER_ELEMENT) ?
677 HAMMER2_BMAP_ALLONES :
678 ((hammer2_bitmap_t)1 << bmradix) - 1;
679 for (j = 0;
680 j < HAMMER2_BMAP_BITS_PER_ELEMENT;
681 j += bmradix) {
682 if ((bmap->bitmapq[i] & bmmask) == 0)
683 goto success;
684 bmmask <<= bmradix;
687 /*fragments might remain*/
688 /*KKASSERT(bmap->avail == 0);*/
689 return (HAMMER2_ERROR_ENOSPC);
690 success:
691 offset = i * (HAMMER2_SEGSIZE / HAMMER2_BMAP_ELEMENTS) +
692 (j * (HAMMER2_FREEMAP_BLOCK_SIZE / 2));
693 if (size & HAMMER2_FREEMAP_BLOCK_MASK)
694 bmap->linear = offset + size;
697 /* 8 x (64/2) -> 256 x 16K -> 4MB */
698 KKASSERT(i >= 0 && i < HAMMER2_BMAP_ELEMENTS);
701 * Optimize the buffer cache to avoid unnecessary read-before-write
702 * operations.
704 * The device block size could be larger than the allocation size
705 * so the actual bitmap test is somewhat more involved. We have
706 * to use a compatible buffer size for this operation.
708 if ((bmap->bitmapq[i] & bmmask) == 0 &&
709 HAMMER2_PBUFSIZE != size) {
710 size_t psize = HAMMER2_PBUFSIZE;
711 hammer2_off_t pmask = (hammer2_off_t)psize - 1;
712 int pbmradix = (hammer2_bitmap_t)2 <<
713 (HAMMER2_PBUFRADIX -
714 HAMMER2_FREEMAP_BLOCK_RADIX);
715 hammer2_bitmap_t pbmmask;
716 int pradix = hammer2_getradix(psize);
718 pbmmask = (pbmradix == HAMMER2_BMAP_BITS_PER_ELEMENT) ?
719 HAMMER2_BMAP_ALLONES :
720 ((hammer2_bitmap_t)1 << pbmradix) - 1;
721 while ((pbmmask & bmmask) == 0)
722 pbmmask <<= pbmradix;
724 #if 0
725 kprintf("%016jx mask %016jx %016jx %016jx (%zd/%zd)\n",
726 *basep + offset, bmap->bitmapq[i],
727 pbmmask, bmmask, size, psize);
728 #endif
730 if ((bmap->bitmapq[i] & pbmmask) == 0) {
731 hammer2_io_t *dio;
733 hammer2_io_newnz(hmp, class >> 8,
734 (*basep + (offset & ~pmask)) |
735 pradix, psize, &dio);
736 hammer2_io_putblk(&dio);
740 #if 0
742 * When initializing a new inode segment also attempt to initialize
743 * an adjacent segment. Be careful not to index beyond the array
744 * bounds.
746 * We do this to try to localize inode accesses to improve
747 * directory scan rates. XXX doesn't improve scan rates.
749 if (size == HAMMER2_INODE_BYTES) {
750 if (n & 1) {
751 if (bmap[-1].radix == 0 && bmap[-1].avail)
752 bmap[-1].radix = radix;
753 } else {
754 if (bmap[1].radix == 0 && bmap[1].avail)
755 bmap[1].radix = radix;
758 #endif
760 * Calculate the bitmap-granular change in bgsize for the volume
761 * header. We cannot use the fine-grained change here because
762 * the bulkfree code can't undo it. If the bitmap element is already
763 * marked allocated it has already been accounted for.
765 if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) {
766 if (bmap->bitmapq[i] & bmmask)
767 bgsize = 0;
768 else
769 bgsize = HAMMER2_FREEMAP_BLOCK_SIZE;
770 } else {
771 bgsize = size;
775 * Adjust the bitmap, set the class (it might have been 0),
776 * and available bytes, update the allocation offset (*basep)
777 * from the L0 base to the actual offset.
779 * Do not override the class if doing a relaxed class allocation.
781 * avail must reflect the bitmap-granular availability. The allocator
782 * tests will also check the linear iterator.
784 bmap->bitmapq[i] |= bmmask;
785 if (bmap->class == 0)
786 bmap->class = class;
787 bmap->avail -= bgsize;
788 *basep += offset;
791 * Adjust the volume header's allocator_free parameter. This
792 * parameter has to be fixed up by bulkfree which has no way to
793 * figure out sub-16K chunking, so it must be adjusted by the
794 * bitmap-granular size.
796 if (bgsize) {
797 hammer2_voldata_lock(hmp);
798 hammer2_voldata_modify(hmp);
799 hmp->voldata.allocator_free -= bgsize;
800 hammer2_voldata_unlock(hmp);
803 return(0);
807 * Initialize a freemap for the storage area (in bytes) that begins at (key).
809 static
810 void
811 hammer2_freemap_init(hammer2_dev_t *hmp, hammer2_key_t key,
812 hammer2_chain_t *chain)
814 hammer2_off_t lokey;
815 hammer2_off_t hikey;
816 hammer2_bmap_data_t *bmap;
817 int count;
820 * Calculate the portion of the 1GB map that should be initialized
821 * as free. Portions below or after will be initialized as allocated.
822 * SEGMASK-align the areas so we don't have to worry about sub-scans
823 * or endianess when using memset.
825 * WARNING! It is possible for lokey to be larger than hikey if the
826 * entire 2GB segment is within the static allocation.
829 * (1) Ensure that all statically allocated space from newfs_hammer2
830 * is marked allocated, and take it up to the level1 base for
831 * this key.
833 lokey = (hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
834 ~HAMMER2_SEGMASK64;
835 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX))
836 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX);
839 * (2) Ensure that the reserved area is marked allocated (typically
840 * the first 4MB of each 2GB area being represented). Since
841 * each LEAF represents 1GB of storage and the zone is 2GB, we
842 * have to adjust lowkey upward every other LEAF sequentially.
844 if (lokey < H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64)
845 lokey = H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64;
848 * (3) Ensure that any trailing space at the end-of-volume is marked
849 * allocated.
851 hikey = key + HAMMER2_FREEMAP_LEVEL1_SIZE;
852 if (hikey > hmp->total_size) {
853 hikey = hmp->total_size & ~HAMMER2_SEGMASK64;
857 * Heuristic highest possible value
859 chain->bref.check.freemap.avail = HAMMER2_FREEMAP_LEVEL1_SIZE;
860 bmap = &chain->data->bmdata[0];
863 * Initialize bitmap (bzero'd by caller)
865 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
866 if (key < lokey || key >= hikey) {
867 memset(bmap->bitmapq, -1,
868 sizeof(bmap->bitmapq));
869 bmap->avail = 0;
870 bmap->linear = HAMMER2_SEGSIZE;
871 chain->bref.check.freemap.avail -=
872 HAMMER2_FREEMAP_LEVEL0_SIZE;
873 } else {
874 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
876 key += HAMMER2_FREEMAP_LEVEL0_SIZE;
877 ++bmap;
882 * The current Level 1 freemap has been exhausted, iterate to the next
883 * one, return ENOSPC if no freemaps remain.
885 * At least two loops are required. If we are not in relaxed mode and
886 * we run out of storage we enter relaxed mode and do a third loop.
887 * The relaxed mode is recorded back in the hmp so once we enter the mode
888 * we remain relaxed until stuff begins to get freed and only do 2 loops.
890 * XXX this should rotate back to the beginning to handle freed-up space
891 * XXX or use intermediate entries to locate free space. TODO
893 static int
894 hammer2_freemap_iterate(hammer2_chain_t **parentp, hammer2_chain_t **chainp,
895 hammer2_fiterate_t *iter)
897 hammer2_dev_t *hmp = (*parentp)->hmp;
899 iter->bnext &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
900 iter->bnext += HAMMER2_FREEMAP_LEVEL1_SIZE;
901 if (iter->bnext >= hmp->total_size) {
902 iter->bnext = 0;
903 if (++iter->loops >= 2) {
904 if (iter->relaxed == 0)
905 iter->relaxed = 1;
906 else
907 return (HAMMER2_ERROR_ENOSPC);
910 return(HAMMER2_ERROR_EAGAIN);
914 * Adjust the bit-pattern for data in the freemap bitmap according to
915 * (how). This code is called from on-mount recovery to fixup (mark
916 * as allocated) blocks whos freemap upates might not have been committed
917 * in the last crash and is used by the bulk freemap scan to stage frees.
919 * WARNING! Cannot be called with a empty-data bref (radix == 0).
921 * XXX currently disabled when how == 0 (the normal real-time case). At
922 * the moment we depend on the bulk freescan to actually free blocks. It
923 * will still call this routine with a non-zero how to stage possible frees
924 * and to do the actual free.
926 void
927 hammer2_freemap_adjust(hammer2_dev_t *hmp, hammer2_blockref_t *bref,
928 int how)
930 hammer2_off_t data_off = bref->data_off;
931 hammer2_chain_t *chain;
932 hammer2_chain_t *parent;
933 hammer2_bmap_data_t *bmap;
934 hammer2_key_t key;
935 hammer2_key_t key_dummy;
936 hammer2_off_t l1size;
937 hammer2_off_t l1mask;
938 hammer2_tid_t mtid;
939 hammer2_bitmap_t *bitmap;
940 const hammer2_bitmap_t bmmask00 = 0;
941 //hammer2_bitmap_t bmmask01;
942 //hammer2_bitmap_t bmmask10;
943 hammer2_bitmap_t bmmask11;
944 size_t bytes;
945 uint16_t class;
946 int radix;
947 int start;
948 int count;
949 int modified = 0;
950 int error;
951 size_t bgsize = 0;
953 KKASSERT(how == HAMMER2_FREEMAP_DORECOVER);
955 KKASSERT(hmp->spmp);
956 mtid = hammer2_trans_sub(hmp->spmp);
958 radix = (int)data_off & HAMMER2_OFF_MASK_RADIX;
959 KKASSERT(radix != 0);
960 data_off &= ~HAMMER2_OFF_MASK_RADIX;
961 KKASSERT(radix <= HAMMER2_RADIX_MAX);
963 if (radix)
964 bytes = (size_t)1 << radix;
965 else
966 bytes = 0;
967 class = (bref->type << 8) | HAMMER2_PBUFRADIX;
970 * We can't adjust the freemap for data allocations made by
971 * newfs_hammer2.
973 if (data_off < hmp->voldata.allocator_beg)
974 return;
976 KKASSERT((data_off & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
979 * Lookup the level1 freemap chain. The chain must exist.
981 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL1_RADIX);
982 l1size = HAMMER2_FREEMAP_LEVEL1_SIZE;
983 l1mask = l1size - 1;
985 parent = &hmp->fchain;
986 hammer2_chain_ref(parent);
987 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
989 chain = hammer2_chain_lookup(&parent, &key_dummy, key, key + l1mask,
990 &error,
991 HAMMER2_LOOKUP_ALWAYS |
992 HAMMER2_LOOKUP_MATCHIND);
995 * Stop early if we are trying to free something but no leaf exists.
997 if (chain == NULL && how != HAMMER2_FREEMAP_DORECOVER) {
998 kprintf("hammer2_freemap_adjust: %016jx: no chain\n",
999 (intmax_t)bref->data_off);
1000 goto done;
1002 if (chain->error) {
1003 kprintf("hammer2_freemap_adjust: %016jx: error %s\n",
1004 (intmax_t)bref->data_off,
1005 hammer2_error_str(chain->error));
1006 hammer2_chain_unlock(chain);
1007 hammer2_chain_drop(chain);
1008 chain = NULL;
1009 goto done;
1013 * Create any missing leaf(s) if we are doing a recovery (marking
1014 * the block(s) as being allocated instead of being freed). Be sure
1015 * to initialize the auxillary freemap tracking info in the
1016 * bref.check.freemap structure.
1018 if (chain == NULL && how == HAMMER2_FREEMAP_DORECOVER) {
1019 error = hammer2_chain_create(&parent, &chain, NULL, hmp->spmp,
1020 HAMMER2_METH_DEFAULT,
1021 key, HAMMER2_FREEMAP_LEVEL1_RADIX,
1022 HAMMER2_BREF_TYPE_FREEMAP_LEAF,
1023 HAMMER2_FREEMAP_LEVELN_PSIZE,
1024 mtid, 0, 0);
1026 if (hammer2_debug & 0x0040) {
1027 kprintf("fixup create chain %p %016jx:%d\n",
1028 chain, chain->bref.key, chain->bref.keybits);
1031 if (error == 0) {
1032 error = hammer2_chain_modify(chain, mtid, 0, 0);
1033 KKASSERT(error == 0);
1034 bzero(&chain->data->bmdata[0],
1035 HAMMER2_FREEMAP_LEVELN_PSIZE);
1036 chain->bref.check.freemap.bigmask = (uint32_t)-1;
1037 chain->bref.check.freemap.avail = l1size;
1038 /* bref.methods should already be inherited */
1040 hammer2_freemap_init(hmp, key, chain);
1042 /* XXX handle error */
1045 #if FREEMAP_DEBUG
1046 kprintf("FREEMAP ADJUST TYPE %d %016jx/%d DATA_OFF=%016jx\n",
1047 chain->bref.type, chain->bref.key,
1048 chain->bref.keybits, chain->bref.data_off);
1049 #endif
1052 * Calculate the bitmask (runs in 2-bit pairs).
1054 start = ((int)(data_off >> HAMMER2_FREEMAP_BLOCK_RADIX) & 15) * 2;
1055 //bmmask01 = (hammer2_bitmap_t)1 << start;
1056 //bmmask10 = (hammer2_bitmap_t)2 << start;
1057 bmmask11 = (hammer2_bitmap_t)3 << start;
1060 * Fixup the bitmap. Partial blocks cannot be fully freed unless
1061 * a bulk scan is able to roll them up.
1063 if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) {
1064 count = 1;
1065 #if 0
1066 if (how == HAMMER2_FREEMAP_DOREALFREE)
1067 how = HAMMER2_FREEMAP_DOMAYFREE;
1068 #endif
1069 } else {
1070 count = 1 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
1074 * [re]load the bmap and bitmap pointers. Each bmap entry covers
1075 * a 4MB swath. The bmap itself (LEVEL1) covers 2GB.
1077 * Be sure to reset the linear iterator to ensure that the adjustment
1078 * is not ignored.
1080 again:
1081 bmap = &chain->data->bmdata[(int)(data_off >> HAMMER2_SEGRADIX) &
1082 (HAMMER2_FREEMAP_COUNT - 1)];
1083 bitmap = &bmap->bitmapq[(int)(data_off >> (HAMMER2_SEGRADIX - 3)) & 7];
1085 if (modified)
1086 bmap->linear = 0;
1088 while (count) {
1089 KKASSERT(bmmask11);
1090 if (how == HAMMER2_FREEMAP_DORECOVER) {
1092 * Recovery request, mark as allocated.
1094 if ((*bitmap & bmmask11) != bmmask11) {
1095 if (modified == 0) {
1096 hammer2_chain_modify(chain, mtid, 0, 0);
1097 modified = 1;
1098 goto again;
1100 if ((*bitmap & bmmask11) == bmmask00) {
1101 bmap->avail -=
1102 HAMMER2_FREEMAP_BLOCK_SIZE;
1103 bgsize += HAMMER2_FREEMAP_BLOCK_SIZE;
1105 if (bmap->class == 0)
1106 bmap->class = class;
1107 *bitmap |= bmmask11;
1108 if (hammer2_debug & 0x0040) {
1109 kprintf("hammer2_freemap_adjust: "
1110 "fixup type=%02x "
1111 "block=%016jx/%zd\n",
1112 bref->type, data_off, bytes);
1114 } else {
1116 kprintf("hammer2_freemap_adjust: good "
1117 "type=%02x block=%016jx/%zd\n",
1118 bref->type, data_off, bytes);
1122 #if 0
1124 * XXX this stuff doesn't work, avail is miscalculated and
1125 * code 10 means something else now.
1127 else if ((*bitmap & bmmask11) == bmmask11) {
1129 * Mayfree/Realfree request and bitmap is currently
1130 * marked as being fully allocated.
1132 if (!modified) {
1133 hammer2_chain_modify(chain, 0);
1134 modified = 1;
1135 goto again;
1137 if (how == HAMMER2_FREEMAP_DOREALFREE)
1138 *bitmap &= ~bmmask11;
1139 else
1140 *bitmap = (*bitmap & ~bmmask11) | bmmask10;
1141 } else if ((*bitmap & bmmask11) == bmmask10) {
1143 * Mayfree/Realfree request and bitmap is currently
1144 * marked as being possibly freeable.
1146 if (how == HAMMER2_FREEMAP_DOREALFREE) {
1147 if (!modified) {
1148 hammer2_chain_modify(chain, 0);
1149 modified = 1;
1150 goto again;
1152 *bitmap &= ~bmmask11;
1154 } else {
1156 * 01 - Not implemented, currently illegal state
1157 * 00 - Not allocated at all, illegal free.
1159 panic("hammer2_freemap_adjust: "
1160 "Illegal state %08x(%08x)",
1161 *bitmap, *bitmap & bmmask11);
1163 #endif
1164 --count;
1165 //bmmask01 <<= 2;
1166 //bmmask10 <<= 2;
1167 bmmask11 <<= 2;
1169 #if 0
1170 #if HAMMER2_BMAP_ELEMENTS != 8
1171 #error "hammer2_freemap.c: HAMMER2_BMAP_ELEMENTS expected to be 8"
1172 #endif
1173 if (how == HAMMER2_FREEMAP_DOREALFREE && modified) {
1174 bmap->avail += 1 << radix;
1175 KKASSERT(bmap->avail <= HAMMER2_SEGSIZE);
1176 if (bmap->avail == HAMMER2_SEGSIZE &&
1177 bmap->bitmapq[0] == 0 &&
1178 bmap->bitmapq[1] == 0 &&
1179 bmap->bitmapq[2] == 0 &&
1180 bmap->bitmapq[3] == 0 &&
1181 bmap->bitmapq[4] == 0 &&
1182 bmap->bitmapq[5] == 0 &&
1183 bmap->bitmapq[6] == 0 &&
1184 bmap->bitmapq[7] == 0) {
1185 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL0_RADIX);
1186 kprintf("Freeseg %016jx\n", (intmax_t)key);
1187 bmap->class = 0;
1190 #endif
1193 * chain->bref.check.freemap.bigmask (XXX)
1195 * Setting bigmask is a hint to the allocation code that there might
1196 * be something allocatable. We also set this in recovery... it
1197 * doesn't hurt and we might want to use the hint for other validation
1198 * operations later on.
1200 * We could calculate the largest possible allocation and set the
1201 * radixes that could fit, but its easier just to set bigmask to -1.
1203 if (modified) {
1204 chain->bref.check.freemap.bigmask = -1;
1205 hmp->freemap_relaxed = 0; /* reset heuristic */
1208 hammer2_chain_unlock(chain);
1209 hammer2_chain_drop(chain);
1210 done:
1211 hammer2_chain_unlock(parent);
1212 hammer2_chain_drop(parent);
1214 if (bgsize) {
1215 hammer2_voldata_lock(hmp);
1216 hammer2_voldata_modify(hmp);
1217 hmp->voldata.allocator_free -= bgsize;
1218 hammer2_voldata_unlock(hmp);
1223 * Validate the freemap, in three stages.
1225 * stage-1 ALLOCATED -> POSSIBLY FREE
1226 * POSSIBLY FREE -> POSSIBLY FREE (type corrected)
1228 * This transitions bitmap entries from ALLOCATED to POSSIBLY FREE.
1229 * The POSSIBLY FREE state does not mean that a block is actually free
1230 * and may be transitioned back to ALLOCATED in stage-2.
1232 * This is typically done during normal filesystem operations when
1233 * something is deleted or a block is replaced.
1235 * This is done by bulkfree in-bulk after a memory-bounded meta-data
1236 * scan to try to determine what might be freeable.
1238 * This can be done unconditionally through a freemap scan when the
1239 * intention is to brute-force recover the proper state of the freemap.
1241 * stage-2 POSSIBLY FREE -> ALLOCATED (scan metadata topology)
1243 * This is done by bulkfree during a meta-data scan to ensure that
1244 * all blocks still actually allocated by the filesystem are marked
1245 * as such.
1247 * NOTE! Live filesystem transitions to POSSIBLY FREE can occur while
1248 * the bulkfree stage-2 and stage-3 is running. The live filesystem
1249 * will use the alternative POSSIBLY FREE type (2) to prevent
1250 * stage-3 from improperly transitioning unvetted possibly-free
1251 * blocks to FREE.
1253 * stage-3 POSSIBLY FREE (type 1) -> FREE (scan freemap)
1255 * This is done by bulkfree to finalize POSSIBLY FREE states.