2 * Copyright (c) 2011-2014 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
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
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
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
41 #include <sys/namei.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/mountctl.h>
48 #define FREEMAP_DEBUG 0
50 struct hammer2_fiterate {
56 typedef struct hammer2_fiterate hammer2_fiterate_t;
58 static int hammer2_freemap_try_alloc(hammer2_trans_t *trans,
59 hammer2_chain_t **parentp, hammer2_blockref_t *bref,
60 int radix, hammer2_fiterate_t *iter);
61 static void hammer2_freemap_init(hammer2_trans_t *trans, hammer2_dev_t *hmp,
62 hammer2_key_t key, hammer2_chain_t *chain);
63 static int hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_dev_t *hmp,
64 hammer2_bmap_data_t *bmap, uint16_t class,
65 int n, int radix, hammer2_key_t *basep);
66 static int hammer2_freemap_iterate(hammer2_trans_t *trans,
67 hammer2_chain_t **parentp, hammer2_chain_t **chainp,
68 hammer2_fiterate_t *iter);
72 hammer2_freemapradix(int radix)
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
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
87 #define H2FMBASE(key, radix) ((key) & ~(((hammer2_off_t)1 << (radix)) - 1))
88 #define H2FMSHIFT(radix) ((hammer2_off_t)1 << (radix))
92 hammer2_freemap_reserve(hammer2_trans_t *trans, hammer2_chain_t *chain,
95 hammer2_blockref_t *bref = &chain->bref;
101 * Physical allocation size -> radix. Typically either 256 for
102 * a level 0 freemap leaf or 65536 for a level N freemap node.
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.
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
115 if ((bref->data_off & ~HAMMER2_OFF_MASK_RADIX) == 0) {
118 off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX &
119 (((hammer2_off_t)1 <<
120 HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
121 off = off / HAMMER2_PBUFSIZE;
122 KKASSERT(off >= HAMMER2_ZONE_FREEMAP_00 &&
123 off < HAMMER2_ZONE_FREEMAP_END);
124 index = (int)(off - HAMMER2_ZONE_FREEMAP_00) / 4;
125 KKASSERT(index >= 0 && index < HAMMER2_NFREEMAPS);
126 if (++index == HAMMER2_NFREEMAPS)
131 * Calculate the block offset of the reserved block. This will
132 * point into the 4MB reserved area at the base of the appropriate
133 * 2GB zone, once added to the FREEMAP_x selection above.
135 switch(bref->keybits) {
136 /* case HAMMER2_FREEMAP_LEVEL5_RADIX: not applicable */
137 case HAMMER2_FREEMAP_LEVEL4_RADIX: /* 2EB */
138 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
139 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
140 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL4_RADIX) +
141 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
142 HAMMER2_ZONEFM_LEVEL4) * HAMMER2_PBUFSIZE;
144 case HAMMER2_FREEMAP_LEVEL3_RADIX: /* 2PB */
145 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
146 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
147 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL3_RADIX) +
148 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
149 HAMMER2_ZONEFM_LEVEL3) * HAMMER2_PBUFSIZE;
151 case HAMMER2_FREEMAP_LEVEL2_RADIX: /* 2TB */
152 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
153 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
154 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL2_RADIX) +
155 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
156 HAMMER2_ZONEFM_LEVEL2) * HAMMER2_PBUFSIZE;
158 case HAMMER2_FREEMAP_LEVEL1_RADIX: /* 2GB */
159 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
160 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
161 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
162 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
163 HAMMER2_ZONEFM_LEVEL1) * HAMMER2_PBUFSIZE;
166 panic("freemap: bad radix(2) %p %d\n", bref, bref->keybits);
168 off = (hammer2_off_t)-1;
171 bref->data_off = off | radix;
173 kprintf("FREEMAP BLOCK TYPE %d %016jx/%d DATA_OFF=%016jx\n",
174 bref->type, bref->key, bref->keybits, bref->data_off);
180 * Normal freemap allocator
182 * Use available hints to allocate space using the freemap. Create missing
183 * freemap infrastructure on-the-fly as needed (including marking initial
184 * allocations using the iterator as allocated, instantiating new 2GB zones,
185 * and dealing with the end-of-media edge case).
187 * ip and bpref are only used as a heuristic to determine locality of
188 * reference. bref->key may also be used heuristically.
191 hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_chain_t *chain,
194 hammer2_dev_t *hmp = chain->hmp;
195 hammer2_blockref_t *bref = &chain->bref;
196 hammer2_chain_t *parent;
200 hammer2_fiterate_t iter;
203 * Validate the allocation size. It must be a power of 2.
205 * For now require that the caller be aware of the minimum
208 radix = hammer2_getradix(bytes);
209 KKASSERT((size_t)1 << radix == bytes);
211 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
212 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
214 * Freemap blocks themselves are assigned from the reserve
215 * area, not allocated from the freemap.
217 error = hammer2_freemap_reserve(trans, chain, radix);
221 KKASSERT(bytes >= HAMMER2_ALLOC_MIN && bytes <= HAMMER2_ALLOC_MAX);
223 if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
227 * Calculate the starting point for our allocation search.
229 * Each freemap leaf is dedicated to a specific freemap_radix.
230 * The freemap_radix can be more fine-grained than the device buffer
231 * radix which results in inodes being grouped together in their
232 * own segment, terminal-data (16K or less) and initial indirect
233 * block being grouped together, and then full-indirect and full-data
234 * blocks (64K) being grouped together.
236 * The single most important aspect of this is the inode grouping
237 * because that is what allows 'find' and 'ls' and other filesystem
238 * topology operations to run fast.
241 if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX)
242 bpref = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
243 else if (trans->tmp_bpref)
244 bpref = trans->tmp_bpref;
245 else if (trans->tmp_ip)
246 bpref = trans->tmp_ip->chain->bref.data_off;
250 * Heuristic tracking index. We would like one for each distinct
251 * bref type if possible. heur_freemap[] has room for two classes
252 * for each type. At a minimum we have to break-up our heuristic
253 * by device block sizes.
255 hindex = hammer2_devblkradix(radix) - HAMMER2_MINIORADIX;
256 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR_NRADIX);
257 hindex += bref->type * HAMMER2_FREEMAP_HEUR_NRADIX;
258 hindex &= HAMMER2_FREEMAP_HEUR_TYPES * HAMMER2_FREEMAP_HEUR_NRADIX - 1;
259 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR);
261 iter.bpref = hmp->heur_freemap[hindex];
264 * Make sure bpref is in-bounds. It's ok if bpref covers a zone's
265 * reserved area, the try code will iterate past it.
267 if (iter.bpref > hmp->voldata.volu_size)
268 iter.bpref = hmp->voldata.volu_size - 1;
271 * Iterate the freemap looking for free space before and after.
273 parent = &hmp->fchain;
274 hammer2_chain_ref(parent);
275 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
277 iter.bnext = iter.bpref;
280 while (error == EAGAIN) {
281 error = hammer2_freemap_try_alloc(trans, &parent, bref,
284 hmp->heur_freemap[hindex] = iter.bnext;
285 hammer2_chain_unlock(parent);
286 hammer2_chain_drop(parent);
288 if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
295 hammer2_freemap_try_alloc(hammer2_trans_t *trans, hammer2_chain_t **parentp,
296 hammer2_blockref_t *bref, int radix,
297 hammer2_fiterate_t *iter)
299 hammer2_dev_t *hmp = (*parentp)->hmp;
300 hammer2_off_t l0size;
301 hammer2_off_t l1size;
302 hammer2_off_t l1mask;
303 hammer2_key_t key_dummy;
304 hammer2_chain_t *chain;
309 int cache_index = -1;
312 * Calculate the number of bytes being allocated, the number
313 * of contiguous bits of bitmap being allocated, and the bitmap
316 * WARNING! cpu hardware may mask bits == 64 -> 0 and blow up the
319 bytes = (size_t)1 << radix;
320 class = (bref->type << 8) | hammer2_devblkradix(radix);
323 * Lookup the level1 freemap chain, creating and initializing one
324 * if necessary. Intermediate levels will be created automatically
325 * when necessary by hammer2_chain_create().
327 key = H2FMBASE(iter->bnext, HAMMER2_FREEMAP_LEVEL1_RADIX);
328 l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
329 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
332 chain = hammer2_chain_lookup(parentp, &key_dummy, key, key + l1mask,
334 HAMMER2_LOOKUP_ALWAYS |
335 HAMMER2_LOOKUP_MATCHIND);
339 * Create the missing leaf, be sure to initialize
340 * the auxillary freemap tracking information in
341 * the bref.check.freemap structure.
344 kprintf("freemap create L1 @ %016jx bpref %016jx\n",
347 error = hammer2_chain_create(trans, parentp, &chain, hmp->spmp,
348 key, HAMMER2_FREEMAP_LEVEL1_RADIX,
349 HAMMER2_BREF_TYPE_FREEMAP_LEAF,
350 HAMMER2_FREEMAP_LEVELN_PSIZE,
352 KKASSERT(error == 0);
354 hammer2_chain_modify(trans, chain, 0);
355 bzero(&chain->data->bmdata[0],
356 HAMMER2_FREEMAP_LEVELN_PSIZE);
357 chain->bref.check.freemap.bigmask = (uint32_t)-1;
358 chain->bref.check.freemap.avail = l1size;
359 /* bref.methods should already be inherited */
361 hammer2_freemap_init(trans, hmp, key, chain);
363 } else if (chain->error) {
365 * Error during lookup.
367 kprintf("hammer2_freemap_try_alloc: %016jx: error %s\n",
368 (intmax_t)bref->data_off,
369 hammer2_error_str(chain->error));
371 } else if ((chain->bref.check.freemap.bigmask & (1 << radix)) == 0) {
373 * Already flagged as not having enough space
378 * Modify existing chain to setup for adjustment.
380 hammer2_chain_modify(trans, chain, 0);
387 hammer2_bmap_data_t *bmap;
388 hammer2_key_t base_key;
393 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
394 start = (int)((iter->bnext - key) >>
395 HAMMER2_FREEMAP_LEVEL0_RADIX);
396 KKASSERT(start >= 0 && start < HAMMER2_FREEMAP_COUNT);
397 hammer2_chain_modify(trans, chain, 0);
400 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
403 if (start + count >= HAMMER2_FREEMAP_COUNT &&
409 * Calculate bmap pointer
411 * NOTE: bmap pointer is invalid if n >= FREEMAP_COUNT.
414 bmap = &chain->data->bmdata[n];
416 if (n >= HAMMER2_FREEMAP_COUNT) {
418 } else if (bmap->avail) {
420 } else if (radix < HAMMER2_FREEMAP_BLOCK_RADIX &&
421 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK)) {
428 (bmap->class == 0 || bmap->class == class)) {
429 base_key = key + n * l0size;
430 error = hammer2_bmap_alloc(trans, hmp, bmap,
433 if (error != ENOSPC) {
440 * Must recalculate after potentially having called
441 * hammer2_bmap_alloc() above in case chain was
444 * NOTE: bmap pointer is invalid if n < 0.
447 bmap = &chain->data->bmdata[n];
450 } else if (bmap->avail) {
452 } else if (radix < HAMMER2_FREEMAP_BLOCK_RADIX &&
453 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK)) {
460 (bmap->class == 0 || bmap->class == class)) {
461 base_key = key + n * l0size;
462 error = hammer2_bmap_alloc(trans, hmp, bmap,
465 if (error != ENOSPC) {
472 chain->bref.check.freemap.bigmask &= ~(1 << radix);
473 /* XXX also scan down from original count */
478 * Assert validity. Must be beyond the static allocator used
479 * by newfs_hammer2 (and thus also beyond the aux area),
480 * not go past the volume size, and must not be in the
481 * reserved segment area for a zone.
483 KKASSERT(key >= hmp->voldata.allocator_beg &&
484 key + bytes <= hmp->voldata.volu_size);
485 KKASSERT((key & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
486 bref->data_off = key | radix;
489 kprintf("alloc cp=%p %016jx %016jx using %016jx\n",
491 bref->key, bref->data_off, chain->bref.data_off);
493 } else if (error == ENOSPC) {
495 * Return EAGAIN with next iteration in iter->bnext, or
496 * return ENOSPC if the allocation map has been exhausted.
498 error = hammer2_freemap_iterate(trans, parentp, &chain, iter);
505 hammer2_chain_unlock(chain);
506 hammer2_chain_drop(chain);
512 * Allocate (1<<radix) bytes from the bmap whos base data offset is (*basep).
514 * If the linear iterator is mid-block we use it directly (the bitmap should
515 * already be marked allocated), otherwise we search for a block in the bitmap
516 * that fits the allocation request.
518 * A partial bitmap allocation sets the minimum bitmap granularity (16KB)
519 * to fully allocated and adjusts the linear allocator to allow the
520 * remaining space to be allocated.
524 hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_dev_t *hmp,
525 hammer2_bmap_data_t *bmap,
526 uint16_t class, int n, int radix, hammer2_key_t *basep)
539 * Take into account 2-bits per block when calculating bmradix.
541 size = (size_t)1 << radix;
543 if (radix <= HAMMER2_FREEMAP_BLOCK_RADIX) {
545 /* (16K) 2 bits per allocation block */
547 bmradix = 2 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
548 /* (32K-256K) 4, 8, 16, 32 bits per allocation block */
552 * Use the linear iterator to pack small allocations, otherwise
553 * fall-back to finding a free 16KB chunk. The linear iterator
554 * is only valid when *NOT* on a freemap chunking boundary (16KB).
555 * If it is the bitmap must be scanned. It can become invalid
556 * once we pack to the boundary. We adjust it after a bitmap
557 * allocation only for sub-16KB allocations (so the perfectly good
558 * previous value can still be used for fragments when 16KB+
559 * allocations are made).
561 * Beware of hardware artifacts when bmradix == 32 (intermediate
562 * result can wind up being '1' instead of '0' if hardware masks
565 * NOTE: j needs to be even in the j= calculation. As an artifact
566 * of the /2 division, our bitmask has to clear bit 0.
568 * NOTE: TODO this can leave little unallocatable fragments lying
571 if (((uint32_t)bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) + size <=
572 HAMMER2_FREEMAP_BLOCK_SIZE &&
573 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) &&
574 bmap->linear < HAMMER2_SEGSIZE) {
575 KKASSERT(bmap->linear >= 0 &&
576 bmap->linear + size <= HAMMER2_SEGSIZE &&
577 (bmap->linear & (HAMMER2_ALLOC_MIN - 1)) == 0);
578 offset = bmap->linear;
579 i = offset / (HAMMER2_SEGSIZE / 8);
580 j = (offset / (HAMMER2_FREEMAP_BLOCK_SIZE / 2)) & 30;
581 bmmask = (bmradix == 32) ?
582 0xFFFFFFFFU : (1 << bmradix) - 1;
584 bmap->linear = offset + size;
586 for (i = 0; i < 8; ++i) {
587 bmmask = (bmradix == 32) ?
588 0xFFFFFFFFU : (1 << bmradix) - 1;
589 for (j = 0; j < 32; j += bmradix) {
590 if ((bmap->bitmap[i] & bmmask) == 0)
595 /*fragments might remain*/
596 /*KKASSERT(bmap->avail == 0);*/
599 offset = i * (HAMMER2_SEGSIZE / 8) +
600 (j * (HAMMER2_FREEMAP_BLOCK_SIZE / 2));
601 if (size & HAMMER2_FREEMAP_BLOCK_MASK)
602 bmap->linear = offset + size;
605 KKASSERT(i >= 0 && i < 8); /* 8 x 16 -> 128 x 16K -> 2MB */
608 * Optimize the buffer cache to avoid unnecessary read-before-write
611 * The device block size could be larger than the allocation size
612 * so the actual bitmap test is somewhat more involved. We have
613 * to use a compatible buffer size for this operation.
615 if ((bmap->bitmap[i] & bmmask) == 0 &&
616 hammer2_devblksize(size) != size) {
617 size_t psize = hammer2_devblksize(size);
618 hammer2_off_t pmask = (hammer2_off_t)psize - 1;
619 int pbmradix = 2 << (hammer2_devblkradix(radix) -
620 HAMMER2_FREEMAP_BLOCK_RADIX);
622 int pradix = hammer2_getradix(psize);
624 pbmmask = (pbmradix == 32) ? 0xFFFFFFFFU : (1 << pbmradix) - 1;
625 while ((pbmmask & bmmask) == 0)
626 pbmmask <<= pbmradix;
629 kprintf("%016jx mask %08x %08x %08x (%zd/%zd)\n",
630 *basep + offset, bmap->bitmap[i],
631 pbmmask, bmmask, size, psize);
634 if ((bmap->bitmap[i] & pbmmask) == 0) {
635 error = hammer2_io_newq(hmp,
636 (*basep + (offset & ~pmask)) |
639 hammer2_io_bqrelse(&dio);
645 * When initializing a new inode segment also attempt to initialize
646 * an adjacent segment. Be careful not to index beyond the array
649 * We do this to try to localize inode accesses to improve
650 * directory scan rates. XXX doesn't improve scan rates.
652 if (size == HAMMER2_INODE_BYTES) {
654 if (bmap[-1].radix == 0 && bmap[-1].avail)
655 bmap[-1].radix = radix;
657 if (bmap[1].radix == 0 && bmap[1].avail)
658 bmap[1].radix = radix;
663 * Calculate the bitmap-granular change in bgsize for the volume
664 * header. We cannot use the fine-grained change here because
665 * the bulkfree code can't undo it. If the bitmap element is already
666 * marked allocated it has already been accounted for.
668 if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) {
669 if (bmap->bitmap[i] & bmmask)
672 bgsize = HAMMER2_FREEMAP_BLOCK_SIZE;
678 * Adjust the bitmap, set the class (it might have been 0),
679 * and available bytes, update the allocation offset (*basep)
680 * from the L0 base to the actual offset.
682 * avail must reflect the bitmap-granular availability. The allocator
683 * tests will also check the linear iterator.
685 bmap->bitmap[i] |= bmmask;
687 bmap->avail -= bgsize;
691 * Adjust the volume header's allocator_free parameter. This
692 * parameter has to be fixed up by bulkfree which has no way to
693 * figure out sub-16K chunking, so it must be adjusted by the
694 * bitmap-granular size.
697 hammer2_voldata_lock(hmp);
698 hammer2_voldata_modify(hmp);
699 hmp->voldata.allocator_free -= bgsize;
700 hammer2_voldata_unlock(hmp);
708 hammer2_freemap_init(hammer2_trans_t *trans, hammer2_dev_t *hmp,
709 hammer2_key_t key, hammer2_chain_t *chain)
711 hammer2_off_t l1size;
714 hammer2_bmap_data_t *bmap;
717 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
720 * Calculate the portion of the 2GB map that should be initialized
721 * as free. Portions below or after will be initialized as allocated.
722 * SEGMASK-align the areas so we don't have to worry about sub-scans
723 * or endianess when using memset.
725 * (1) Ensure that all statically allocated space from newfs_hammer2
726 * is marked allocated.
728 * (2) Ensure that the reserved area is marked allocated (typically
729 * the first 4MB of the 2GB area being represented).
731 * (3) Ensure that any trailing space at the end-of-volume is marked
734 * WARNING! It is possible for lokey to be larger than hikey if the
735 * entire 2GB segment is within the static allocation.
737 lokey = (hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
740 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
741 HAMMER2_ZONE_SEG64) {
742 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
746 hikey = key + H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
747 if (hikey > hmp->voldata.volu_size) {
748 hikey = hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
751 chain->bref.check.freemap.avail =
752 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
753 bmap = &chain->data->bmdata[0];
755 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
756 if (key < lokey || key >= hikey) {
757 memset(bmap->bitmap, -1,
758 sizeof(bmap->bitmap));
760 bmap->linear = HAMMER2_SEGSIZE;
761 chain->bref.check.freemap.avail -=
762 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
764 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
766 key += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
772 * The current Level 1 freemap has been exhausted, iterate to the next
773 * one, return ENOSPC if no freemaps remain.
775 * XXX this should rotate back to the beginning to handle freed-up space
776 * XXX or use intermediate entries to locate free space. TODO
779 hammer2_freemap_iterate(hammer2_trans_t *trans, hammer2_chain_t **parentp,
780 hammer2_chain_t **chainp, hammer2_fiterate_t *iter)
782 hammer2_dev_t *hmp = (*parentp)->hmp;
784 iter->bnext &= ~(H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
785 iter->bnext += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
786 if (iter->bnext >= hmp->voldata.volu_size) {
788 if (++iter->loops == 2)
795 * Adjust the bit-pattern for data in the freemap bitmap according to
796 * (how). This code is called from on-mount recovery to fixup (mark
797 * as allocated) blocks whos freemap upates might not have been committed
798 * in the last crash and is used by the bulk freemap scan to stage frees.
800 * XXX currently disabled when how == 0 (the normal real-time case). At
801 * the moment we depend on the bulk freescan to actually free blocks. It
802 * will still call this routine with a non-zero how to stage possible frees
803 * and to do the actual free.
806 hammer2_freemap_adjust(hammer2_trans_t *trans, hammer2_dev_t *hmp,
807 hammer2_blockref_t *bref, int how)
809 hammer2_off_t data_off = bref->data_off;
810 hammer2_chain_t *chain;
811 hammer2_chain_t *parent;
812 hammer2_bmap_data_t *bmap;
814 hammer2_key_t key_dummy;
815 hammer2_off_t l0size;
816 hammer2_off_t l1size;
817 hammer2_off_t l1mask;
819 const uint32_t bmmask00 = 0;
829 int cache_index = -1;
832 KKASSERT(how == HAMMER2_FREEMAP_DORECOVER);
834 radix = (int)data_off & HAMMER2_OFF_MASK_RADIX;
835 data_off &= ~HAMMER2_OFF_MASK_RADIX;
836 KKASSERT(radix <= HAMMER2_RADIX_MAX);
838 bytes = (size_t)1 << radix;
839 class = (bref->type << 8) | hammer2_devblkradix(radix);
842 * We can't adjust thre freemap for data allocations made by
845 if (data_off < hmp->voldata.allocator_beg)
848 KKASSERT((data_off & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
851 * Lookup the level1 freemap chain. The chain must exist.
853 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL1_RADIX);
854 l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
855 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
858 parent = &hmp->fchain;
859 hammer2_chain_ref(parent);
860 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
862 chain = hammer2_chain_lookup(&parent, &key_dummy, key, key + l1mask,
864 HAMMER2_LOOKUP_ALWAYS |
865 HAMMER2_LOOKUP_MATCHIND);
868 * Stop early if we are trying to free something but no leaf exists.
870 if (chain == NULL && how != HAMMER2_FREEMAP_DORECOVER) {
871 kprintf("hammer2_freemap_adjust: %016jx: no chain\n",
872 (intmax_t)bref->data_off);
876 kprintf("hammer2_freemap_adjust: %016jx: error %s\n",
877 (intmax_t)bref->data_off,
878 hammer2_error_str(chain->error));
879 hammer2_chain_unlock(chain);
880 hammer2_chain_drop(chain);
886 * Create any missing leaf(s) if we are doing a recovery (marking
887 * the block(s) as being allocated instead of being freed). Be sure
888 * to initialize the auxillary freemap tracking info in the
889 * bref.check.freemap structure.
891 if (chain == NULL && how == HAMMER2_FREEMAP_DORECOVER) {
892 error = hammer2_chain_create(trans, &parent, &chain, hmp->spmp,
893 key, HAMMER2_FREEMAP_LEVEL1_RADIX,
894 HAMMER2_BREF_TYPE_FREEMAP_LEAF,
895 HAMMER2_FREEMAP_LEVELN_PSIZE,
898 if (hammer2_debug & 0x0040) {
899 kprintf("fixup create chain %p %016jx:%d\n",
900 chain, chain->bref.key, chain->bref.keybits);
904 hammer2_chain_modify(trans, chain, 0);
905 bzero(&chain->data->bmdata[0],
906 HAMMER2_FREEMAP_LEVELN_PSIZE);
907 chain->bref.check.freemap.bigmask = (uint32_t)-1;
908 chain->bref.check.freemap.avail = l1size;
909 /* bref.methods should already be inherited */
911 hammer2_freemap_init(trans, hmp, key, chain);
913 /* XXX handle error */
917 kprintf("FREEMAP ADJUST TYPE %d %016jx/%d DATA_OFF=%016jx\n",
918 chain->bref.type, chain->bref.key,
919 chain->bref.keybits, chain->bref.data_off);
923 * Calculate the bitmask (runs in 2-bit pairs).
925 start = ((int)(data_off >> HAMMER2_FREEMAP_BLOCK_RADIX) & 15) * 2;
926 bmmask01 = 1 << start;
927 bmmask10 = 2 << start;
928 bmmask11 = 3 << start;
931 * Fixup the bitmap. Partial blocks cannot be fully freed unless
932 * a bulk scan is able to roll them up.
934 if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) {
936 if (how == HAMMER2_FREEMAP_DOREALFREE)
937 how = HAMMER2_FREEMAP_DOMAYFREE;
939 count = 1 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
943 * [re]load the bmap and bitmap pointers. Each bmap entry covers
944 * a 2MB swath. The bmap itself (LEVEL1) covers 2GB.
946 * Be sure to reset the linear iterator to ensure that the adjustment
950 bmap = &chain->data->bmdata[(int)(data_off >> HAMMER2_SEGRADIX) &
951 (HAMMER2_FREEMAP_COUNT - 1)];
952 bitmap = &bmap->bitmap[(int)(data_off >> (HAMMER2_SEGRADIX - 3)) & 7];
959 if (how == HAMMER2_FREEMAP_DORECOVER) {
961 * Recovery request, mark as allocated.
963 if ((*bitmap & bmmask11) != bmmask11) {
965 hammer2_chain_modify(trans, chain, 0);
969 if ((*bitmap & bmmask11) == bmmask00) {
971 HAMMER2_FREEMAP_BLOCK_SIZE;
973 if (bmap->class == 0)
976 if (hammer2_debug & 0x0040) {
977 kprintf("hammer2_freemap_recover: "
979 "block=%016jx/%zd\n",
980 bref->type, data_off, bytes);
984 kprintf("hammer2_freemap_recover: good "
985 "type=%02x block=%016jx/%zd\n",
986 bref->type, data_off, bytes);
992 * XXX this stuff doesn't work, avail is miscalculated and
993 * code 10 means something else now.
995 else if ((*bitmap & bmmask11) == bmmask11) {
997 * Mayfree/Realfree request and bitmap is currently
998 * marked as being fully allocated.
1001 hammer2_chain_modify(trans, chain, 0);
1005 if (how == HAMMER2_FREEMAP_DOREALFREE)
1006 *bitmap &= ~bmmask11;
1008 *bitmap = (*bitmap & ~bmmask11) | bmmask10;
1009 } else if ((*bitmap & bmmask11) == bmmask10) {
1011 * Mayfree/Realfree request and bitmap is currently
1012 * marked as being possibly freeable.
1014 if (how == HAMMER2_FREEMAP_DOREALFREE) {
1016 hammer2_chain_modify(trans, chain, 0);
1020 *bitmap &= ~bmmask11;
1024 * 01 - Not implemented, currently illegal state
1025 * 00 - Not allocated at all, illegal free.
1027 panic("hammer2_freemap_adjust: "
1028 "Illegal state %08x(%08x)",
1029 *bitmap, *bitmap & bmmask11);
1037 if (how == HAMMER2_FREEMAP_DOREALFREE && modified) {
1038 bmap->avail += 1 << radix;
1039 KKASSERT(bmap->avail <= HAMMER2_SEGSIZE);
1040 if (bmap->avail == HAMMER2_SEGSIZE &&
1041 bmap->bitmap[0] == 0 &&
1042 bmap->bitmap[1] == 0 &&
1043 bmap->bitmap[2] == 0 &&
1044 bmap->bitmap[3] == 0 &&
1045 bmap->bitmap[4] == 0 &&
1046 bmap->bitmap[5] == 0 &&
1047 bmap->bitmap[6] == 0 &&
1048 bmap->bitmap[7] == 0) {
1049 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL0_RADIX);
1050 kprintf("Freeseg %016jx\n", (intmax_t)key);
1056 * chain->bref.check.freemap.bigmask (XXX)
1058 * Setting bigmask is a hint to the allocation code that there might
1059 * be something allocatable. We also set this in recovery... it
1060 * doesn't hurt and we might want to use the hint for other validation
1061 * operations later on.
1064 chain->bref.check.freemap.bigmask |= 1 << radix;
1066 hammer2_chain_unlock(chain);
1067 hammer2_chain_drop(chain);
1069 hammer2_chain_unlock(parent);
1070 hammer2_chain_drop(parent);
1074 * Validate the freemap, in three stages.
1076 * stage-1 ALLOCATED -> POSSIBLY FREE
1077 * POSSIBLY FREE -> POSSIBLY FREE (type corrected)
1079 * This transitions bitmap entries from ALLOCATED to POSSIBLY FREE.
1080 * The POSSIBLY FREE state does not mean that a block is actually free
1081 * and may be transitioned back to ALLOCATED in stage-2.
1083 * This is typically done during normal filesystem operations when
1084 * something is deleted or a block is replaced.
1086 * This is done by bulkfree in-bulk after a memory-bounded meta-data
1087 * scan to try to determine what might be freeable.
1089 * This can be done unconditionally through a freemap scan when the
1090 * intention is to brute-force recover the proper state of the freemap.
1092 * stage-2 POSSIBLY FREE -> ALLOCATED (scan metadata topology)
1094 * This is done by bulkfree during a meta-data scan to ensure that
1095 * all blocks still actually allocated by the filesystem are marked
1098 * NOTE! Live filesystem transitions to POSSIBLY FREE can occur while
1099 * the bulkfree stage-2 and stage-3 is running. The live filesystem
1100 * will use the alternative POSSIBLY FREE type (2) to prevent
1101 * stage-3 from improperly transitioning unvetted possibly-free
1104 * stage-3 POSSIBLY FREE (type 1) -> FREE (scan freemap)
1106 * This is done by bulkfree to finalize POSSIBLY FREE states.