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_mount_t *hmp,
62 hammer2_key_t key, hammer2_chain_t *chain);
63 static int hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_mount_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.
112 if ((bref->data_off & ~HAMMER2_OFF_MASK_RADIX) == 0) {
115 off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX &
116 (((hammer2_off_t)1 << HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
117 off = off / HAMMER2_PBUFSIZE;
118 KKASSERT(off >= HAMMER2_ZONE_FREEMAP_00 &&
119 off < HAMMER2_ZONE_FREEMAP_END);
120 index = (int)(off - HAMMER2_ZONE_FREEMAP_00) / 4;
121 KKASSERT(index >= 0 && index < HAMMER2_ZONE_FREEMAP_COPIES);
125 * Calculate new index (our 'allocation'). We have to be careful
126 * here as there can be two different transaction ids running
127 * concurrently when a flush is in-progress.
129 * We also want to make sure, for algorithmic repeatability, that
130 * the index sequences are monotonic with transaction ids. Some
131 * skipping is allowed as long as we ensure that all four volume
132 * header backups have consistent freemaps.
134 * FLUSH NORMAL FLUSH NORMAL FLUSH NORMAL FLUSH NORMAL
136 * (0->1) (1->3) (3->4) (4->6) (6->7) (7->9) (9->10) (10->12)
138 * [-concurrent-][-concurrent-][-concurrent-][-concurrent-]
140 * (alternative first NORMAL might be 0->2 if flush had not yet
141 * modified the chain, this is the worst case).
143 if ((trans->flags & HAMMER2_TRANS_ISFLUSH) == 0) {
145 * Normal transactions always run with the highest TID.
146 * But if a flush is in-progress we want to reserve a slot
147 * for the flush with a lower TID running concurrently to
148 * do a delete-duplicate.
150 index = (index + 2) % HAMMER2_ZONE_FREEMAP_COPIES;
151 } else if (trans->flags & HAMMER2_TRANS_ISALLOCATING) {
153 * Flush transaction, hammer2_freemap.c itself is doing a
154 * delete-duplicate during an allocation within the freemap.
156 index = (index + 1) % HAMMER2_ZONE_FREEMAP_COPIES;
159 * Flush transaction, hammer2_flush.c is doing a
160 * delete-duplicate on the freemap while flushing
163 index = (index + 1) % HAMMER2_ZONE_FREEMAP_COPIES;
167 * Calculate the block offset of the reserved block. This will
168 * point into the 4MB reserved area at the base of the appropriate
169 * 2GB zone, once added to the FREEMAP_x selection above.
171 switch(bref->keybits) {
172 /* case HAMMER2_FREEMAP_LEVEL5_RADIX: not applicable */
173 case HAMMER2_FREEMAP_LEVEL4_RADIX: /* 2EB */
174 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
175 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
176 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL4_RADIX) +
177 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
178 HAMMER2_ZONEFM_LEVEL4) * HAMMER2_PBUFSIZE;
180 case HAMMER2_FREEMAP_LEVEL3_RADIX: /* 2PB */
181 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
182 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
183 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL3_RADIX) +
184 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
185 HAMMER2_ZONEFM_LEVEL3) * HAMMER2_PBUFSIZE;
187 case HAMMER2_FREEMAP_LEVEL2_RADIX: /* 2TB */
188 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
189 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
190 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL2_RADIX) +
191 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
192 HAMMER2_ZONEFM_LEVEL2) * HAMMER2_PBUFSIZE;
194 case HAMMER2_FREEMAP_LEVEL1_RADIX: /* 2GB */
195 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
196 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
197 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
198 (index * 4 + HAMMER2_ZONE_FREEMAP_00 +
199 HAMMER2_ZONEFM_LEVEL1) * HAMMER2_PBUFSIZE;
202 panic("freemap: bad radix(2) %p %d\n", bref, bref->keybits);
204 off = (hammer2_off_t)-1;
207 bref->data_off = off | radix;
209 kprintf("FREEMAP BLOCK TYPE %d %016jx/%d DATA_OFF=%016jx\n",
210 bref->type, bref->key, bref->keybits, bref->data_off);
216 * Normal freemap allocator
218 * Use available hints to allocate space using the freemap. Create missing
219 * freemap infrastructure on-the-fly as needed (including marking initial
220 * allocations using the iterator as allocated, instantiating new 2GB zones,
221 * and dealing with the end-of-media edge case).
223 * ip and bpref are only used as a heuristic to determine locality of
224 * reference. bref->key may also be used heuristically.
226 * WARNING! When called from a flush we have to use the 'live' sync_tid
227 * and not the flush sync_tid. The live sync_tid is the flush
228 * sync_tid + 1. That is, freemap allocations which occur during
229 * a flush are not part of the flush. Crash-recovery will restore
230 * any lost allocations.
233 hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_chain_t *chain,
236 hammer2_mount_t *hmp = chain->hmp;
237 hammer2_blockref_t *bref = &chain->bref;
238 hammer2_chain_t *parent;
242 hammer2_fiterate_t iter;
245 * Validate the allocation size. It must be a power of 2.
247 * For now require that the caller be aware of the minimum
250 radix = hammer2_getradix(bytes);
251 KKASSERT((size_t)1 << radix == bytes);
254 * Freemap blocks themselves are simply assigned from the reserve
255 * area, not allocated from the freemap.
257 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
258 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
259 return (hammer2_freemap_reserve(trans, chain, radix));
264 * (this mechanic is no longer used, DOMAYFREE is used only by
265 * the bulk freemap scan now).
267 * Mark previously allocated block as possibly freeable. There might
268 * be snapshots and other races so we can't just mark it fully free.
269 * (XXX optimize this for the current-transaction create+delete case)
271 if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX) {
272 hammer2_freemap_adjust(trans, hmp, bref,
273 HAMMER2_FREEMAP_DOMAYFREE);
278 * Setting ISALLOCATING ensures correct operation even when the
279 * flusher itself is making allocations.
281 KKASSERT(bytes >= HAMMER2_MIN_ALLOC && bytes <= HAMMER2_MAX_ALLOC);
282 KKASSERT((trans->flags & HAMMER2_TRANS_ISALLOCATING) == 0);
283 atomic_set_int(&trans->flags, HAMMER2_TRANS_ISALLOCATING);
284 if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
288 * Calculate the starting point for our allocation search.
290 * Each freemap leaf is dedicated to a specific freemap_radix.
291 * The freemap_radix can be more fine-grained than the device buffer
292 * radix which results in inodes being grouped together in their
293 * own segment, terminal-data (16K or less) and initial indirect
294 * block being grouped together, and then full-indirect and full-data
295 * blocks (64K) being grouped together.
297 * The single most important aspect of this is the inode grouping
298 * because that is what allows 'find' and 'ls' and other filesystem
299 * topology operations to run fast.
302 if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX)
303 bpref = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
304 else if (trans->tmp_bpref)
305 bpref = trans->tmp_bpref;
306 else if (trans->tmp_ip)
307 bpref = trans->tmp_ip->chain->bref.data_off;
311 * Heuristic tracking index. We would like one for each distinct
312 * bref type if possible. heur_freemap[] has room for two classes
313 * for each type. At a minimum we have to break-up our heuristic
314 * by device block sizes.
316 hindex = hammer2_devblkradix(radix) - HAMMER2_MINIORADIX;
317 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR_NRADIX);
318 hindex += bref->type * HAMMER2_FREEMAP_HEUR_NRADIX;
319 hindex &= HAMMER2_FREEMAP_HEUR_TYPES * HAMMER2_FREEMAP_HEUR_NRADIX - 1;
320 KKASSERT(hindex < HAMMER2_FREEMAP_HEUR);
322 iter.bpref = hmp->heur_freemap[hindex];
325 * Make sure bpref is in-bounds. It's ok if bpref covers a zone's
326 * reserved area, the try code will iterate past it.
328 if (iter.bpref > hmp->voldata.volu_size)
329 iter.bpref = hmp->voldata.volu_size - 1;
332 * Iterate the freemap looking for free space before and after.
334 parent = &hmp->fchain;
335 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
337 iter.bnext = iter.bpref;
340 while (error == EAGAIN) {
341 error = hammer2_freemap_try_alloc(trans, &parent, bref,
344 hmp->heur_freemap[hindex] = iter.bnext;
345 hammer2_chain_unlock(parent);
347 atomic_clear_int(&trans->flags, HAMMER2_TRANS_ISALLOCATING);
348 if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
355 hammer2_freemap_try_alloc(hammer2_trans_t *trans, hammer2_chain_t **parentp,
356 hammer2_blockref_t *bref, int radix,
357 hammer2_fiterate_t *iter)
359 hammer2_mount_t *hmp = (*parentp)->hmp;
360 hammer2_off_t l0size;
361 hammer2_off_t l1size;
362 hammer2_off_t l1mask;
363 hammer2_key_t key_dummy;
364 hammer2_chain_t *chain;
369 int cache_index = -1;
374 * Calculate the number of bytes being allocated, the number
375 * of contiguous bits of bitmap being allocated, and the bitmap
378 * WARNING! cpu hardware may mask bits == 64 -> 0 and blow up the
381 bytes = (size_t)1 << radix;
382 class = (bref->type << 8) | hammer2_devblkradix(radix);
385 * Lookup the level1 freemap chain, creating and initializing one
386 * if necessary. Intermediate levels will be created automatically
387 * when necessary by hammer2_chain_create().
389 key = H2FMBASE(iter->bnext, HAMMER2_FREEMAP_LEVEL1_RADIX);
390 l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
391 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
394 chain = hammer2_chain_lookup(parentp, &key_dummy, key, key + l1mask,
396 HAMMER2_LOOKUP_ALWAYS |
397 HAMMER2_LOOKUP_MATCHIND, &ddflag);
401 * Create the missing leaf, be sure to initialize
402 * the auxillary freemap tracking information in
403 * the bref.check.freemap structure.
406 kprintf("freemap create L1 @ %016jx bpref %016jx\n",
409 error = hammer2_chain_create(trans, parentp, &chain,
410 key, HAMMER2_FREEMAP_LEVEL1_RADIX,
411 HAMMER2_BREF_TYPE_FREEMAP_LEAF,
412 HAMMER2_FREEMAP_LEVELN_PSIZE);
413 KKASSERT(error == 0);
415 hammer2_chain_modify(trans, &chain, 0);
416 bzero(&chain->data->bmdata[0],
417 HAMMER2_FREEMAP_LEVELN_PSIZE);
418 chain->bref.check.freemap.bigmask = (uint32_t)-1;
419 chain->bref.check.freemap.avail = l1size;
420 /* bref.methods should already be inherited */
422 hammer2_freemap_init(trans, hmp, key, chain);
424 } else if ((chain->bref.check.freemap.bigmask & (1 << radix)) == 0) {
426 * Already flagged as not having enough space
431 * Modify existing chain to setup for adjustment.
433 hammer2_chain_modify(trans, &chain, 0);
440 hammer2_bmap_data_t *bmap;
441 hammer2_key_t base_key;
446 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
447 start = (int)((iter->bnext - key) >>
448 HAMMER2_FREEMAP_LEVEL0_RADIX);
449 KKASSERT(start >= 0 && start < HAMMER2_FREEMAP_COUNT);
450 hammer2_chain_modify(trans, &chain, 0);
453 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
454 if (start + count >= HAMMER2_FREEMAP_COUNT &&
459 bmap = &chain->data->bmdata[n];
460 if (n < HAMMER2_FREEMAP_COUNT && bmap->avail &&
461 (bmap->class == 0 || bmap->class == class)) {
462 base_key = key + n * l0size;
463 error = hammer2_bmap_alloc(trans, hmp, bmap,
466 if (error != ENOSPC) {
472 bmap = &chain->data->bmdata[n];
473 if (n >= 0 && bmap->avail &&
474 (bmap->class == 0 || bmap->class == class)) {
475 base_key = key + n * l0size;
476 error = hammer2_bmap_alloc(trans, hmp, bmap,
479 if (error != ENOSPC) {
486 chain->bref.check.freemap.bigmask &= ~(1 << radix);
487 /* XXX also scan down from original count */
492 * Assert validity. Must be beyond the static allocator used
493 * by newfs_hammer2 (and thus also beyond the aux area),
494 * not go past the volume size, and must not be in the
495 * reserved segment area for a zone.
497 KKASSERT(key >= hmp->voldata.allocator_beg &&
498 key + bytes <= hmp->voldata.volu_size);
499 KKASSERT((key & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
500 bref->data_off = key | radix;
503 kprintf("alloc cp=%p %016jx %016jx using %016jx\n",
505 bref->key, bref->data_off, chain->bref.data_off);
507 } else if (error == ENOSPC) {
509 * Return EAGAIN with next iteration in iter->bnext, or
510 * return ENOSPC if the allocation map has been exhausted.
512 error = hammer2_freemap_iterate(trans, parentp, &chain, iter);
519 hammer2_chain_unlock(chain);
524 * Allocate (1<<radix) bytes from the bmap whos base data offset is (*basep).
526 * If the linear iterator is mid-block we use it directly (the bitmap should
527 * already be marked allocated), otherwise we search for a block in the bitmap
528 * that fits the allocation request.
530 * A partial bitmap allocation sets the minimum bitmap granularity (16KB)
531 * to fully allocated and adjusts the linear allocator to allow the
532 * remaining space to be allocated.
536 hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_mount_t *hmp,
537 hammer2_bmap_data_t *bmap,
538 uint16_t class, int n, int radix, hammer2_key_t *basep)
551 * Take into account 2-bits per block when calculating bmradix.
553 size = (size_t)1 << radix;
555 if (radix <= HAMMER2_FREEMAP_BLOCK_RADIX) {
557 bsize = HAMMER2_FREEMAP_BLOCK_SIZE;
558 /* (16K) 2 bits per allocation block */
560 bmradix = 2 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
562 /* (32K-256K) 4, 8, 16, 32 bits per allocation block */
566 * Use the linear iterator to pack small allocations, otherwise
567 * fall-back to finding a free 16KB chunk. The linear iterator
568 * is only valid when *NOT* on a freemap chunking boundary (16KB).
569 * If it is the bitmap must be scanned. It can become invalid
570 * once we pack to the boundary. We adjust it after a bitmap
571 * allocation only for sub-16KB allocations (so the perfectly good
572 * previous value can still be used for fragments when 16KB+
573 * allocations are made).
575 * Beware of hardware artifacts when bmradix == 32 (intermediate
576 * result can wind up being '1' instead of '0' if hardware masks
579 * NOTE: j needs to be even in the j= calculation. As an artifact
580 * of the /2 division, our bitmask has to clear bit 0.
582 * NOTE: TODO this can leave little unallocatable fragments lying
585 if (((uint32_t)bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) + size <=
586 HAMMER2_FREEMAP_BLOCK_SIZE &&
587 (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) &&
588 bmap->linear < HAMMER2_SEGSIZE) {
589 KKASSERT(bmap->linear >= 0 &&
590 bmap->linear + size <= HAMMER2_SEGSIZE &&
591 (bmap->linear & (HAMMER2_MIN_ALLOC - 1)) == 0);
592 offset = bmap->linear;
593 i = offset / (HAMMER2_SEGSIZE / 8);
594 j = (offset / (HAMMER2_FREEMAP_BLOCK_SIZE / 2)) & 30;
595 bmmask = (bmradix == 32) ?
596 0xFFFFFFFFU : (1 << bmradix) - 1;
598 bmap->linear = offset + size;
600 for (i = 0; i < 8; ++i) {
601 bmmask = (bmradix == 32) ?
602 0xFFFFFFFFU : (1 << bmradix) - 1;
603 for (j = 0; j < 32; j += bmradix) {
604 if ((bmap->bitmap[i] & bmmask) == 0)
609 /*fragments might remain*/
610 /*KKASSERT(bmap->avail == 0);*/
613 offset = i * (HAMMER2_SEGSIZE / 8) +
614 (j * (HAMMER2_FREEMAP_BLOCK_SIZE / 2));
615 if (size & HAMMER2_FREEMAP_BLOCK_MASK)
616 bmap->linear = offset + size;
619 KKASSERT(i >= 0 && i < 8); /* 8 x 16 -> 128 x 16K -> 2MB */
622 * Optimize the buffer cache to avoid unnecessary read-before-write
625 * The device block size could be larger than the allocation size
626 * so the actual bitmap test is somewhat more involved. We have
627 * to use a compatible buffer size for this operation.
629 if ((bmap->bitmap[i] & bmmask) == 0 &&
630 hammer2_devblksize(size) != size) {
631 size_t psize = hammer2_devblksize(size);
632 hammer2_off_t pmask = (hammer2_off_t)psize - 1;
633 int pbmradix = 2 << (hammer2_devblkradix(radix) -
634 HAMMER2_FREEMAP_BLOCK_RADIX);
636 int pradix = hammer2_getradix(psize);
638 pbmmask = (pbmradix == 32) ? 0xFFFFFFFFU : (1 << pbmradix) - 1;
639 while ((pbmmask & bmmask) == 0)
640 pbmmask <<= pbmradix;
643 kprintf("%016jx mask %08x %08x %08x (%zd/%zd)\n",
644 *basep + offset, bmap->bitmap[i],
645 pbmmask, bmmask, size, psize);
648 if ((bmap->bitmap[i] & pbmmask) == 0) {
649 error = hammer2_io_newq(hmp,
650 (*basep + (offset & ~pmask)) |
653 hammer2_io_bqrelse(&dio);
659 * When initializing a new inode segment also attempt to initialize
660 * an adjacent segment. Be careful not to index beyond the array
663 * We do this to try to localize inode accesses to improve
664 * directory scan rates. XXX doesn't improve scan rates.
666 if (size == HAMMER2_INODE_BYTES) {
668 if (bmap[-1].radix == 0 && bmap[-1].avail)
669 bmap[-1].radix = radix;
671 if (bmap[1].radix == 0 && bmap[1].avail)
672 bmap[1].radix = radix;
678 * Adjust the linear iterator, set the radix if necessary (might as
679 * well just set it unconditionally), adjust *basep to return the
680 * allocated data offset.
682 bmap->bitmap[i] |= bmmask;
687 hammer2_voldata_lock(hmp);
688 hmp->voldata.allocator_free -= size; /* XXX */
689 hammer2_voldata_unlock(hmp, 1);
696 hammer2_freemap_init(hammer2_trans_t *trans, hammer2_mount_t *hmp,
697 hammer2_key_t key, hammer2_chain_t *chain)
699 hammer2_off_t l1size;
702 hammer2_bmap_data_t *bmap;
705 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
708 * Calculate the portion of the 2GB map that should be initialized
709 * as free. Portions below or after will be initialized as allocated.
710 * SEGMASK-align the areas so we don't have to worry about sub-scans
711 * or endianess when using memset.
713 * (1) Ensure that all statically allocated space from newfs_hammer2
714 * is marked allocated.
716 * (2) Ensure that the reserved area is marked allocated (typically
717 * the first 4MB of the 2GB area being represented).
719 * (3) Ensure that any trailing space at the end-of-volume is marked
722 * WARNING! It is possible for lokey to be larger than hikey if the
723 * entire 2GB segment is within the static allocation.
725 lokey = (hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
728 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
729 HAMMER2_ZONE_SEG64) {
730 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
734 hikey = key + H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
735 if (hikey > hmp->voldata.volu_size) {
736 hikey = hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
739 chain->bref.check.freemap.avail =
740 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
741 bmap = &chain->data->bmdata[0];
743 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
744 if (key < lokey || key >= hikey) {
745 memset(bmap->bitmap, -1,
746 sizeof(bmap->bitmap));
748 bmap->linear = HAMMER2_SEGSIZE;
749 chain->bref.check.freemap.avail -=
750 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
752 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
754 key += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
760 * The current Level 1 freemap has been exhausted, iterate to the next
761 * one, return ENOSPC if no freemaps remain.
763 * XXX this should rotate back to the beginning to handle freed-up space
764 * XXX or use intermediate entries to locate free space. TODO
767 hammer2_freemap_iterate(hammer2_trans_t *trans, hammer2_chain_t **parentp,
768 hammer2_chain_t **chainp, hammer2_fiterate_t *iter)
770 hammer2_mount_t *hmp = (*parentp)->hmp;
772 iter->bnext &= ~(H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
773 iter->bnext += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
774 if (iter->bnext >= hmp->voldata.volu_size) {
776 if (++iter->loops == 2)
783 * Adjust the bit-pattern for data in the freemap bitmap according to
784 * (how). This code is called from on-mount recovery to fixup (mark
785 * as allocated) blocks whos freemap upates might not have been committed
786 * in the last crash and is used by the bulk freemap scan to stage frees.
788 * XXX currently disabled when how == 0 (the normal real-time case). At
789 * the moment we depend on the bulk freescan to actually free blocks. It
790 * will still call this routine with a non-zero how to stage possible frees
791 * and to do the actual free.
793 * WARNING! When called from a flush we have to use the 'live' sync_tid
794 * and not the flush sync_tid. The live sync_tid is the flush
795 * sync_tid + 1. That is, freemap allocations which occur during
796 * a flush are not part of the flush. Crash-recovery will restore
797 * any lost allocations.
800 hammer2_freemap_adjust(hammer2_trans_t *trans, hammer2_mount_t *hmp,
801 hammer2_blockref_t *bref, int how)
803 hammer2_off_t data_off = bref->data_off;
804 hammer2_chain_t *chain;
805 hammer2_chain_t *parent;
806 hammer2_bmap_data_t *bmap;
808 hammer2_key_t key_dummy;
809 hammer2_off_t l0size;
810 hammer2_off_t l1size;
811 hammer2_off_t l1mask;
813 const uint32_t bmmask00 = 0;
823 int cache_index = -1;
827 radix = (int)data_off & HAMMER2_OFF_MASK_RADIX;
828 data_off &= ~HAMMER2_OFF_MASK_RADIX;
829 KKASSERT(radix <= HAMMER2_MAX_RADIX);
831 bytes = (size_t)1 << radix;
832 class = (bref->type << 8) | hammer2_devblkradix(radix);
835 * We can't adjust thre freemap for data allocations made by
838 if (data_off < hmp->voldata.allocator_beg)
841 KKASSERT((data_off & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
842 KKASSERT((trans->flags & HAMMER2_TRANS_ISALLOCATING) == 0);
843 atomic_set_int(&trans->flags, HAMMER2_TRANS_ISALLOCATING);
844 if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
848 * Lookup the level1 freemap chain. The chain must exist.
850 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL1_RADIX);
851 l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
852 l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
855 parent = &hmp->fchain;
856 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
858 chain = hammer2_chain_lookup(&parent, &key_dummy, key, key + l1mask,
860 HAMMER2_LOOKUP_ALWAYS |
861 HAMMER2_LOOKUP_MATCHIND, &ddflag);
864 * Stop early if we are trying to free something but no leaf exists.
866 if (chain == NULL && how != HAMMER2_FREEMAP_DORECOVER) {
867 kprintf("hammer2_freemap_adjust: %016jx: no chain\n",
868 (intmax_t)bref->data_off);
873 * Create any missing leaf(s) if we are doing a recovery (marking
874 * the block(s) as being allocated instead of being freed). Be sure
875 * to initialize the auxillary freemap tracking info in the
876 * bref.check.freemap structure.
878 if (chain == NULL && how == HAMMER2_FREEMAP_DORECOVER) {
879 error = hammer2_chain_create(trans, &parent, &chain,
880 key, HAMMER2_FREEMAP_LEVEL1_RADIX,
881 HAMMER2_BREF_TYPE_FREEMAP_LEAF,
882 HAMMER2_FREEMAP_LEVELN_PSIZE);
884 if (hammer2_debug & 0x0040) {
885 kprintf("fixup create chain %p %016jx:%d\n",
886 chain, chain->bref.key, chain->bref.keybits);
890 hammer2_chain_modify(trans, &chain, 0);
891 bzero(&chain->data->bmdata[0],
892 HAMMER2_FREEMAP_LEVELN_PSIZE);
893 chain->bref.check.freemap.bigmask = (uint32_t)-1;
894 chain->bref.check.freemap.avail = l1size;
895 /* bref.methods should already be inherited */
897 hammer2_freemap_init(trans, hmp, key, chain);
899 /* XXX handle error */
903 kprintf("FREEMAP ADJUST TYPE %d %016jx/%d DATA_OFF=%016jx\n",
904 chain->bref.type, chain->bref.key,
905 chain->bref.keybits, chain->bref.data_off);
909 * Calculate the bitmask (runs in 2-bit pairs).
911 start = ((int)(data_off >> HAMMER2_FREEMAP_BLOCK_RADIX) & 15) * 2;
912 bmmask01 = 1 << start;
913 bmmask10 = 2 << start;
914 bmmask11 = 3 << start;
917 * Fixup the bitmap. Partial blocks cannot be fully freed unless
918 * a bulk scan is able to roll them up.
920 if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) {
922 if (how == HAMMER2_FREEMAP_DOREALFREE)
923 how = HAMMER2_FREEMAP_DOMAYFREE;
925 count = 1 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
929 * [re]load the bmap and bitmap pointers. Each bmap entry covers
930 * a 2MB swath. The bmap itself (LEVEL1) covers 2GB.
932 * Be sure to reset the linear iterator to ensure that the adjustment
936 bmap = &chain->data->bmdata[(int)(data_off >> HAMMER2_SEGRADIX) &
937 (HAMMER2_FREEMAP_COUNT - 1)];
938 bitmap = &bmap->bitmap[(int)(data_off >> (HAMMER2_SEGRADIX - 3)) & 7];
943 if (how == HAMMER2_FREEMAP_DORECOVER) {
945 * Recovery request, mark as allocated.
947 if ((*bitmap & bmmask11) != bmmask11) {
949 hammer2_chain_modify(trans, &chain, 0);
953 if ((*bitmap & bmmask11) == bmmask00)
954 bmap->avail -= 1 << radix;
955 if (bmap->class == 0)
958 if (hammer2_debug & 0x0040) {
959 kprintf("hammer2_freemap_recover: "
961 "block=%016jx/%zd\n",
962 bref->type, data_off, bytes);
966 kprintf("hammer2_freemap_recover: good "
967 "type=%02x block=%016jx/%zd\n",
968 bref->type, data_off, bytes);
971 } else if ((*bitmap & bmmask11) == bmmask11) {
973 * Mayfree/Realfree request and bitmap is currently
974 * marked as being fully allocated.
977 hammer2_chain_modify(trans, &chain, 0);
981 if (how == HAMMER2_FREEMAP_DOREALFREE)
982 *bitmap &= ~bmmask11;
984 *bitmap = (*bitmap & ~bmmask11) | bmmask10;
985 } else if ((*bitmap & bmmask11) == bmmask10) {
987 * Mayfree/Realfree request and bitmap is currently
988 * marked as being possibly freeable.
990 if (how == HAMMER2_FREEMAP_DOREALFREE) {
992 hammer2_chain_modify(trans, &chain, 0);
996 *bitmap &= ~bmmask11;
1000 * 01 - Not implemented, currently illegal state
1001 * 00 - Not allocated at all, illegal free.
1003 panic("hammer2_freemap_adjust: "
1004 "Illegal state %08x(%08x)",
1005 *bitmap, *bitmap & bmmask11);
1012 if (how == HAMMER2_FREEMAP_DOREALFREE && modified) {
1013 bmap->avail += 1 << radix;
1014 KKASSERT(bmap->avail <= HAMMER2_SEGSIZE);
1015 if (bmap->avail == HAMMER2_SEGSIZE &&
1016 bmap->bitmap[0] == 0 &&
1017 bmap->bitmap[1] == 0 &&
1018 bmap->bitmap[2] == 0 &&
1019 bmap->bitmap[3] == 0 &&
1020 bmap->bitmap[4] == 0 &&
1021 bmap->bitmap[5] == 0 &&
1022 bmap->bitmap[6] == 0 &&
1023 bmap->bitmap[7] == 0) {
1024 key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL0_RADIX);
1025 kprintf("Freeseg %016jx\n", (intmax_t)key);
1031 * chain->bref.check.freemap.bigmask (XXX)
1033 * Setting bigmask is a hint to the allocation code that there might
1034 * be something allocatable. We also set this in recovery... it
1035 * doesn't hurt and we might want to use the hint for other validation
1036 * operations later on.
1039 chain->bref.check.freemap.bigmask |= 1 << radix;
1041 hammer2_chain_unlock(chain);
1043 hammer2_chain_unlock(parent);
1044 atomic_clear_int(&trans->flags, HAMMER2_TRANS_ISALLOCATING);
1045 if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))