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
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>
39 #include <sys/mount.h>
43 #define FREEMAP_DEBUG 0
45 struct hammer2_fiterate
{
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
78 hammer2_freemap_reserve(hammer2_chain_t
*chain
, int radix
)
80 hammer2_blockref_t
*bref
= &chain
->bref
;
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
97 if ((bref
->data_off
& ~HAMMER2_OFF_MASK_RADIX
) == 0) {
100 off
= bref
->data_off
& ~HAMMER2_OFF_MASK_RADIX
&
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
)
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
;
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
;
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
;
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
;
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
;
157 panic("freemap: bad radix(2) %p %d\n", bref
, bref
->keybits
);
159 off
= (hammer2_off_t
)-1;
162 bref
->data_off
= off
| radix
;
164 kprintf("FREEMAP BLOCK TYPE %d %016jx/%d DATA_OFF=%016jx\n",
165 bref
->type
, bref
->key
, bref
->keybits
, bref
->data_off
);
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
;
193 hammer2_fiterate_t iter
;
196 * If allocating or downsizing to zero we just get rid of whatever
200 chain
->bref
.data_off
= 0;
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
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
);
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
;
261 while (error
== HAMMER2_ERROR_EAGAIN
) {
262 error
= hammer2_freemap_try_alloc(&parent
, bref
, radix
,
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
);
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
;
290 * Calculate the number of bytes being allocated, the number
291 * of contiguous bits of bitmap being allocated, and the bitmap
294 * WARNING! cpu hardware may mask bits == 64 -> 0 and blow up the
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
;
310 chain
= hammer2_chain_lookup(parentp
, &key_dummy
, key
, key
+ l1mask
,
312 HAMMER2_LOOKUP_ALWAYS
|
313 HAMMER2_LOOKUP_MATCHIND
);
317 * Create the missing leaf, be sure to initialize
318 * the auxillary freemap tracking information in
319 * the bref.check.freemap structure.
322 kprintf("freemap create L1 @ %016jx bpref %016jx\n",
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
,
331 KKASSERT(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
;
358 * Modify existing chain to setup for adjustment.
360 hammer2_chain_modify(chain
, mtid
, 0, 0);
367 hammer2_bmap_data_t
*bmap
;
368 hammer2_key_t base_key
;
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
) {
383 if (start
+ count
>= HAMMER2_FREEMAP_COUNT
&&
389 * Calculate bmap pointer from thart starting index
392 * NOTE: bmap pointer is invalid if n >= FREEMAP_COUNT.
395 bmap
= &chain
->data
->bmdata
[n
];
397 if (n
>= HAMMER2_FREEMAP_COUNT
) {
399 } else if (bmap
->avail
) {
401 } else if (radix
< HAMMER2_FREEMAP_BLOCK_RADIX
&&
402 (bmap
->linear
& HAMMER2_FREEMAP_BLOCK_MASK
)) {
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.
414 (bmap
->class == 0 || bmap
->class == class ||
416 base_key
= key
+ n
* l0size
;
417 error
= hammer2_bmap_alloc(hmp
, bmap
,
422 if (error
!= HAMMER2_ERROR_ENOSPC
) {
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
436 * NOTE: bmap pointer is invalid if n < 0.
439 bmap
= &chain
->data
->bmdata
[n
];
442 } else if (bmap
->avail
) {
444 } else if (radix
< HAMMER2_FREEMAP_BLOCK_RADIX
&&
445 (bmap
->linear
& HAMMER2_FREEMAP_BLOCK_MASK
)) {
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.
457 (bmap
->class == 0 || bmap
->class == class ||
459 base_key
= key
+ n
* l0size
;
460 error
= hammer2_bmap_alloc(hmp
, bmap
,
465 if (error
!= HAMMER2_ERROR_ENOSPC
) {
473 * We only know for sure that we can clear the bitmap bit
474 * if we scanned the entire array (start == 0) in relaxed
477 if (error
== HAMMER2_ERROR_ENOSPC
&&
481 chain
->bref
.check
.freemap
.bigmask
&=
482 (uint32_t)~((size_t)1 << radix
);
484 /* XXX also scan down from original count */
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
508 if (bref
->type
== HAMMER2_BREF_TYPE_DATA
)
509 hammer2_io_dedup_set(hmp
, bref
);
511 kprintf("alloc cp=%p %016jx %016jx using %016jx\n",
513 bref
->key
, bref
->data_off
, chain
->bref
.data_off
);
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
);
527 hammer2_chain_unlock(chain
);
528 hammer2_chain_drop(chain
);
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.
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
)
558 hammer2_bitmap_t bmmask
;
564 * Take into account 2-bits per block when calculating bmradix.
566 size
= (size_t)1 << radix
;
568 if (radix
<= HAMMER2_FREEMAP_BLOCK_RADIX
) {
570 /* (16K) 2 bits per allocation block */
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
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
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
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;
622 bmap
->linear
= offset
+ size
;
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
630 * i - Index bitmapq[], full data range represented is
633 * j - Index within bitmapq[i], full data range represented is
634 * HAMMER2_BMAP_INDEX_SIZE.
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
);
652 case HAMMER2_BREF_TYPE_INODE
:
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;
666 if ((bmap
->bitmapq
[i
] & bmmask
) == 0)
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;
680 j
< HAMMER2_BMAP_BITS_PER_ELEMENT
;
682 if ((bmap
->bitmapq
[i
] & bmmask
) == 0)
687 /*fragments might remain*/
688 /*KKASSERT(bmap->avail == 0);*/
689 return (HAMMER2_ERROR_ENOSPC
);
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
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 <<
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
;
725 kprintf("%016jx mask %016jx %016jx %016jx (%zd/%zd)\n",
726 *basep
+ offset
, bmap
->bitmapq
[i
],
727 pbmmask
, bmmask
, size
, psize
);
730 if ((bmap
->bitmapq
[i
] & pbmmask
) == 0) {
733 hammer2_io_newnz(hmp
, class >> 8,
734 (*basep
+ (offset
& ~pmask
)) |
735 pradix
, psize
, &dio
);
736 hammer2_io_putblk(&dio
);
742 * When initializing a new inode segment also attempt to initialize
743 * an adjacent segment. Be careful not to index beyond the array
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
) {
751 if (bmap
[-1].radix
== 0 && bmap
[-1].avail
)
752 bmap
[-1].radix
= radix
;
754 if (bmap
[1].radix
== 0 && bmap
[1].avail
)
755 bmap
[1].radix
= radix
;
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
)
769 bgsize
= HAMMER2_FREEMAP_BLOCK_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)
787 bmap
->avail
-= bgsize
;
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.
797 hammer2_voldata_lock(hmp
);
798 hammer2_voldata_modify(hmp
);
799 hmp
->voldata
.allocator_free
-= bgsize
;
800 hammer2_voldata_unlock(hmp
);
807 * Initialize a freemap for the storage area (in bytes) that begins at (key).
811 hammer2_freemap_init(hammer2_dev_t
*hmp
, hammer2_key_t key
,
812 hammer2_chain_t
*chain
)
816 hammer2_bmap_data_t
*bmap
;
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
833 lokey
= (hmp
->voldata
.allocator_beg
+ 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
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
));
870 bmap
->linear
= HAMMER2_SEGSIZE
;
871 chain
->bref
.check
.freemap
.avail
-=
872 HAMMER2_FREEMAP_LEVEL0_SIZE
;
874 bmap
->avail
= HAMMER2_FREEMAP_LEVEL0_SIZE
;
876 key
+= HAMMER2_FREEMAP_LEVEL0_SIZE
;
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
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
) {
903 if (++iter
->loops
>= 2) {
904 if (iter
->relaxed
== 0)
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.
927 hammer2_freemap_adjust(hammer2_dev_t
*hmp
, hammer2_blockref_t
*bref
,
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
;
935 hammer2_key_t key_dummy
;
936 hammer2_off_t l1size
;
937 hammer2_off_t l1mask
;
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
;
953 KKASSERT(how
== HAMMER2_FREEMAP_DORECOVER
);
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
);
964 bytes
= (size_t)1 << radix
;
967 class = (bref
->type
<< 8) | HAMMER2_PBUFRADIX
;
970 * We can't adjust the freemap for data allocations made by
973 if (data_off
< hmp
->voldata
.allocator_beg
)
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
;
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
,
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
);
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
);
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
,
1026 if (hammer2_debug
& 0x0040) {
1027 kprintf("fixup create chain %p %016jx:%d\n",
1028 chain
, chain
->bref
.key
, chain
->bref
.keybits
);
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 */
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
);
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
) {
1066 if (how
== HAMMER2_FREEMAP_DOREALFREE
)
1067 how
= HAMMER2_FREEMAP_DOMAYFREE
;
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
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];
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);
1100 if ((*bitmap
& bmmask11
) == bmmask00
) {
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: "
1111 "block=%016jx/%zd\n",
1112 bref
->type
, data_off
, bytes
);
1116 kprintf("hammer2_freemap_adjust: good "
1117 "type=%02x block=%016jx/%zd\n",
1118 bref->type, data_off, bytes);
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.
1133 hammer2_chain_modify(chain
, 0);
1137 if (how
== HAMMER2_FREEMAP_DOREALFREE
)
1138 *bitmap
&= ~bmmask11
;
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
) {
1148 hammer2_chain_modify(chain
, 0);
1152 *bitmap
&= ~bmmask11
;
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
);
1170 #if HAMMER2_BMAP_ELEMENTS != 8
1171 #error "hammer2_freemap.c: HAMMER2_BMAP_ELEMENTS expected to be 8"
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
);
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.
1204 chain
->bref
.check
.freemap
.bigmask
= -1;
1205 hmp
->freemap_relaxed
= 0; /* reset heuristic */
1208 hammer2_chain_unlock(chain
);
1209 hammer2_chain_drop(chain
);
1211 hammer2_chain_unlock(parent
);
1212 hammer2_chain_drop(parent
);
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
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
1253 * stage-3 POSSIBLY FREE (type 1) -> FREE (scan freemap)
1255 * This is done by bulkfree to finalize POSSIBLY FREE states.