2 * Copyright (c) 2013-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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/fcntl.h>
40 #include <sys/namei.h>
41 #include <sys/mount.h>
42 #include <sys/vnode.h>
43 #include <sys/mountctl.h>
44 #include <vm/vm_kern.h>
45 #include <vm/vm_extern.h>
50 * breadth-first search
52 typedef struct hammer2_chain_save {
53 TAILQ_ENTRY(hammer2_chain_save) entry;
54 hammer2_chain_t *chain;
56 } hammer2_chain_save_t;
58 TAILQ_HEAD(hammer2_chain_save_list, hammer2_chain_save);
59 typedef struct hammer2_chain_save_list hammer2_chain_save_list_t;
61 typedef struct hammer2_bulkfree_info {
64 hammer2_off_t sbase; /* sub-loop iteration */
66 hammer2_bmap_data_t *bmap;
68 long count_10_00; /* staged->free */
69 long count_11_10; /* allocated->staged */
70 long count_00_11; /* (should not happen) */
71 long count_01_11; /* (should not happen) */
72 long count_10_11; /* staged->allocated */
74 long count_linadjusts;
75 long count_inodes_scanned;
76 long count_dirents_scanned;
77 long count_dedup_factor;
78 long count_bytes_scanned;
79 long count_chains_scanned;
80 long count_chains_reported;
83 hammer2_off_t adj_free;
85 hammer2_tid_t saved_mirror_tid;
87 hammer2_chain_save_list_t list;
88 hammer2_dedup_t *dedup;
90 } hammer2_bulkfree_info_t;
92 static int h2_bulkfree_test(hammer2_bulkfree_info_t *info,
93 hammer2_blockref_t *bref, int pri);
96 * General bulk scan function with callback. Called with a referenced
97 * but UNLOCKED parent. The parent is returned in the same state.
101 hammer2_bulk_scan(hammer2_chain_t *parent,
102 int (*func)(hammer2_bulkfree_info_t *info,
103 hammer2_blockref_t *bref),
104 hammer2_bulkfree_info_t *info)
106 hammer2_blockref_t bref;
107 hammer2_chain_t *chain;
114 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
115 HAMMER2_RESOLVE_SHARED);
121 * Generally loop on the contents if we have not been flagged
124 * Remember that these chains are completely isolated from
125 * the frontend, so we can release locks temporarily without
129 error |= hammer2_chain_scan(parent, &chain, &bref, &first,
130 HAMMER2_LOOKUP_NODATA |
131 HAMMER2_LOOKUP_SHARED);
134 * Handle EOF or other error at current level. This stops
141 * Account for dirents before thre data_off test, since most
142 * dirents do not need a data reference.
144 if (bref.type == HAMMER2_BREF_TYPE_DIRENT)
145 ++info->count_dirents_scanned;
148 * Ignore brefs without data (typically dirents)
150 if ((bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0)
154 * Process bref, chain is only non-NULL if the bref
155 * might be recursable (its possible that we sometimes get
156 * a non-NULL chain where the bref cannot be recursed).
159 if (h2_bulkfree_test(info, &bref, 1))
162 if (bref.type == HAMMER2_BREF_TYPE_INODE)
163 ++info->count_inodes_scanned;
165 error |= func(info, &bref);
170 * A non-null chain is always returned if it is
171 * recursive, otherwise a non-null chain might be
172 * returned but usually is not when not recursive.
178 info->count_bytes_scanned += chain->bytes;
179 ++info->count_chains_scanned;
181 if (info->count_chains_scanned >=
182 info->count_chains_reported + 1000000 ||
183 (info->count_chains_scanned < 1000000 &&
184 info->count_chains_scanned >=
185 info->count_chains_reported + 100000)) {
186 kprintf(" chains %-7ld inodes %-7ld "
187 "dirents %-7ld bytes %5ldMB\n",
188 info->count_chains_scanned,
189 info->count_inodes_scanned,
190 info->count_dirents_scanned,
191 info->count_bytes_scanned / 1000000);
192 info->count_chains_reported =
193 info->count_chains_scanned;
199 * Else check type and setup depth-first scan.
201 * Account for bytes actually read.
203 switch(chain->bref.type) {
204 case HAMMER2_BREF_TYPE_INODE:
205 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
206 case HAMMER2_BREF_TYPE_INDIRECT:
207 case HAMMER2_BREF_TYPE_VOLUME:
208 case HAMMER2_BREF_TYPE_FREEMAP:
210 if (info->depth > 16) {
211 hammer2_chain_save_t *save;
212 save = kmalloc(sizeof(*save), M_HAMMER2,
215 hammer2_chain_ref(chain);
216 TAILQ_INSERT_TAIL(&info->list, save, entry);
221 int savepri = info->pri;
223 hammer2_chain_unlock(chain);
224 hammer2_chain_unlock(parent);
227 hammer2_bulk_scan(chain, func, info);
228 info->pri += savepri;
229 hammer2_chain_lock(parent,
230 HAMMER2_RESOLVE_ALWAYS |
231 HAMMER2_RESOLVE_SHARED);
232 hammer2_chain_lock(chain,
233 HAMMER2_RESOLVE_ALWAYS |
234 HAMMER2_RESOLVE_SHARED);
238 case HAMMER2_BREF_TYPE_DATA:
241 /* does not recurse */
244 if (rup_error & HAMMER2_ERROR_ABORTED)
248 hammer2_chain_unlock(chain);
249 hammer2_chain_drop(chain);
253 * Save with higher pri now that we know what it is.
255 h2_bulkfree_test(info, &parent->bref, info->pri + 1);
257 hammer2_chain_unlock(parent);
259 return ((error | rup_error) & ~HAMMER2_ERROR_EOF);
266 * Chain flush (partial synchronization) XXX removed
267 * Scan the whole topology - build in-memory freemap (mark 11)
268 * Reconcile the in-memory freemap against the on-disk freemap.
269 * ondisk xx -> ondisk 11 (if allocated)
270 * ondisk 11 -> ondisk 10 (if free in-memory)
271 * ondisk 10 -> ondisk 00 (if free in-memory) - on next pass
274 * The topology scan may have to be performed multiple times to window
275 * freemaps which are too large to fit in kernel memory.
277 * Races are handled using a double-transition (11->10, 10->00). The bulkfree
278 * scan snapshots the volume root's blockset and thus can run concurrent with
279 * normal operations, as long as a full flush is made between each pass to
280 * synchronize any modified chains (otherwise their blocks might be improperly
283 * Temporary memory in multiples of 64KB is required to reconstruct the leaf
284 * hammer2_bmap_data blocks so they can later be compared against the live
285 * freemap. Each 64KB block represents 128 x 16KB x 1024 = ~2 GB of storage.
286 * A 32MB save area thus represents around ~1 TB. The temporary memory
287 * allocated can be specified. If it is not sufficient multiple topology
288 * passes will be made.
292 * Bulkfree callback info
294 static void hammer2_bulkfree_thread(void *arg __unused);
295 static void cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size);
296 static int h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo,
297 hammer2_blockref_t *bref);
298 static int h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
299 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
300 hammer2_off_t data_off, hammer2_bmap_data_t *live,
301 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base);
304 hammer2_bulkfree_init(hammer2_dev_t *hmp)
306 hammer2_thr_create(&hmp->bfthr, NULL, hmp,
307 hmp->devrepname, -1, -1,
308 hammer2_bulkfree_thread);
312 hammer2_bulkfree_uninit(hammer2_dev_t *hmp)
314 hammer2_thr_delete(&hmp->bfthr);
318 hammer2_bulkfree_thread(void *arg)
320 hammer2_thread_t *thr = arg;
321 hammer2_ioc_bulkfree_t bfi;
325 hammer2_thr_wait_any(thr,
326 HAMMER2_THREAD_STOP |
327 HAMMER2_THREAD_FREEZE |
328 HAMMER2_THREAD_UNFREEZE |
329 HAMMER2_THREAD_REMASTER,
334 if (flags & HAMMER2_THREAD_STOP)
336 if (flags & HAMMER2_THREAD_FREEZE) {
337 hammer2_thr_signal2(thr, HAMMER2_THREAD_FROZEN,
338 HAMMER2_THREAD_FREEZE);
341 if (flags & HAMMER2_THREAD_UNFREEZE) {
342 hammer2_thr_signal2(thr, 0,
343 HAMMER2_THREAD_FROZEN |
344 HAMMER2_THREAD_UNFREEZE);
347 if (flags & HAMMER2_THREAD_FROZEN)
349 if (flags & HAMMER2_THREAD_REMASTER) {
350 hammer2_thr_signal2(thr, 0, HAMMER2_THREAD_REMASTER);
351 bzero(&bfi, sizeof(bfi));
352 bfi.size = 8192 * 1024;
353 /* hammer2_bulkfree_pass(thr->hmp, &bfi); */
357 hammer2_thr_signal(thr, HAMMER2_THREAD_STOPPED);
358 /* structure can go invalid at this point */
362 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
363 hammer2_ioc_bulkfree_t *bfi)
365 hammer2_bulkfree_info_t cbinfo;
366 hammer2_chain_save_t *save;
372 * We have to clear the live dedup cache as it might have entries
373 * that are freeable as of now. Any new entries in the dedup cache
374 * made after this point, even if they become freeable, will have
375 * previously been fully allocated and will be protected by the
378 hammer2_dedup_clear(hmp);
381 * Setup for free pass using the buffer size specified by the
382 * hammer2 utility, 32K-aligned.
384 bzero(&cbinfo, sizeof(cbinfo));
385 size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
386 ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
389 * Cap at 1/4 physical memory (hammer2 utility will not normally
390 * ever specify a buffer this big, but leave the option available).
392 if (size > kmem_lim_size() * 1024 * 1024 / 4) {
393 size = kmem_lim_size() * 1024 * 1024 / 4;
394 kprintf("hammer2: Warning: capping bulkfree buffer at %jdM\n",
395 (intmax_t)size / (1024 * 1024));
398 #define HAMMER2_FREEMAP_SIZEDIV \
399 (HAMMER2_FREEMAP_LEVEL1_SIZE / HAMMER2_FREEMAP_LEVELN_PSIZE)
400 #define HAMMER2_FREEMAP_SIZEMASK (HAMMER2_FREEMAP_SIZEDIV - 1)
403 * Cap at the size needed to cover the whole volume to avoid
404 * making an unnecessarily large allocation.
406 if (size > hmp->voldata.volu_size / HAMMER2_FREEMAP_SIZEDIV) {
407 size = (hmp->voldata.volu_size + HAMMER2_FREEMAP_SIZEMASK) /
408 HAMMER2_FREEMAP_SIZEDIV;
412 * Minimum bitmap buffer size, then align to a LEVELN_PSIZE (32K)
415 if (size < 1024 * 1024)
417 size = (size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
418 ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
421 cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size, VM_SUBSYS_HAMMER);
422 cbinfo.saved_mirror_tid = hmp->voldata.mirror_tid;
424 cbinfo.dedup = kmalloc(sizeof(*cbinfo.dedup) * HAMMER2_DEDUP_HEUR_SIZE,
425 M_HAMMER2, M_WAITOK | M_ZERO);
427 kprintf("hammer2: bulkfree buf=%jdM\n",
428 (intmax_t)size / (1024 * 1024));
431 * Normalize start point to a 2GB boundary. We operate on a
432 * 64KB leaf bitmap boundary which represents 2GB of storage.
434 cbinfo.sbase = bfi->sbase;
435 if (cbinfo.sbase > hmp->voldata.volu_size)
436 cbinfo.sbase = hmp->voldata.volu_size;
437 cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
438 TAILQ_INIT(&cbinfo.list);
440 cbinfo.bulkfree_ticks = ticks;
443 * Loop on a full meta-data scan as many times as required to
444 * get through all available storage.
447 while (cbinfo.sbase < hmp->voldata.volu_size) {
449 * We have enough ram to represent (incr) bytes of storage.
450 * Each 64KB of ram represents 2GB of storage.
452 * We must also clean out our de-duplication heuristic for
453 * each (incr) bytes of storage, otherwise we wind up not
454 * scanning meta-data for later areas of storage because
455 * they had already been scanned in earlier areas of storage.
456 * Since the ranging is different, we have to restart
457 * the dedup heuristic too.
461 cbinfo_bmap_init(&cbinfo, size);
462 bzero(cbinfo.dedup, sizeof(*cbinfo.dedup) *
463 HAMMER2_DEDUP_HEUR_SIZE);
464 cbinfo.count_inodes_scanned = 0;
465 cbinfo.count_dirents_scanned = 0;
466 cbinfo.count_bytes_scanned = 0;
467 cbinfo.count_chains_scanned = 0;
468 cbinfo.count_chains_reported = 0;
470 incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
471 HAMMER2_FREEMAP_LEVEL1_SIZE;
472 if (hmp->voldata.volu_size - cbinfo.sbase <= incr) {
473 cbinfo.sstop = hmp->voldata.volu_size;
476 cbinfo.sstop = cbinfo.sbase + incr;
479 kprintf("hammer2: pass %016jx-%016jx ",
480 (intmax_t)cbinfo.sbase,
481 (intmax_t)cbinfo.sstop);
482 if (allmedia && cbinfo.sbase == 0)
483 kprintf("(all media)\n");
485 kprintf("(remaining media)\n");
487 kprintf("(%jdGB of media)\n",
488 (intmax_t)incr / (1024L*1024*1024));
491 * Scan topology for stuff inside this range.
493 * NOTE - By not using a transaction the operation can
494 * run concurrent with the frontend as well as
497 * We cannot safely set a mtid without a transaction,
498 * and in fact we don't want to set one anyway. We
499 * want the bulkfree to be passive and no interfere
500 * with crash recovery.
502 #undef HAMMER2_BULKFREE_TRANS /* undef - don't use transaction */
503 #ifdef HAMMER2_BULKFREE_TRANS
504 hammer2_trans_init(hmp->spmp, 0);
505 cbinfo.mtid = hammer2_trans_sub(hmp->spmp);
510 error |= hammer2_bulk_scan(vchain, h2_bulkfree_callback,
513 while ((save = TAILQ_FIRST(&cbinfo.list)) != NULL &&
515 TAILQ_REMOVE(&cbinfo.list, save, entry);
517 error |= hammer2_bulk_scan(save->chain,
518 h2_bulkfree_callback,
520 hammer2_chain_drop(save->chain);
521 kfree(save, M_HAMMER2);
524 TAILQ_REMOVE(&cbinfo.list, save, entry);
525 hammer2_chain_drop(save->chain);
526 kfree(save, M_HAMMER2);
527 save = TAILQ_FIRST(&cbinfo.list);
531 * If the complete scan succeeded we can synchronize our
532 * in-memory freemap against live storage. If an abort
533 * occured we cannot safely synchronize our partially
534 * filled-out in-memory freemap.
537 kprintf("bulkfree lastdrop %d %d error=0x%04x\n",
538 vchain->refs, vchain->core.chain_count, error);
540 kprintf("bulkfree lastdrop %d %d\n",
541 vchain->refs, vchain->core.chain_count);
543 error = h2_bulkfree_sync(&cbinfo);
545 hammer2_voldata_lock(hmp);
546 hammer2_voldata_modify(hmp);
547 hmp->voldata.allocator_free += cbinfo.adj_free;
548 hammer2_voldata_unlock(hmp);
552 * Cleanup for next loop.
554 #ifdef HAMMER2_BULKFREE_TRANS
555 hammer2_trans_done(hmp->spmp, 0);
559 cbinfo.sbase = cbinfo.sstop;
562 kmem_free_swapbacked(&cbinfo.kp);
563 kfree(cbinfo.dedup, M_HAMMER2);
566 bfi->sstop = cbinfo.sbase;
568 incr = bfi->sstop / (hmp->voldata.volu_size / 10000);
572 kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
577 kprintf(" bulkfree was aborted\n");
579 kprintf(" transition->free %ld\n", cbinfo.count_10_00);
580 kprintf(" transition->staged %ld\n", cbinfo.count_11_10);
581 kprintf(" ERR(00)->allocated %ld\n", cbinfo.count_00_11);
582 kprintf(" ERR(01)->allocated %ld\n", cbinfo.count_01_11);
583 kprintf(" staged->allocated %ld\n", cbinfo.count_10_11);
584 kprintf(" ~2MB segs cleaned %ld\n", cbinfo.count_l0cleans);
585 kprintf(" linear adjusts %ld\n",
586 cbinfo.count_linadjusts);
587 kprintf(" dedup factor %ld\n",
588 cbinfo.count_dedup_factor);
595 cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size)
597 hammer2_bmap_data_t *bmap = cbinfo->bmap;
598 hammer2_key_t key = cbinfo->sbase;
602 lokey = (cbinfo->hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
604 hikey = cbinfo->hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
608 bzero(bmap, sizeof(*bmap));
609 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX))
610 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX);
611 if (lokey < H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64)
612 lokey = H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64;
613 if (key < lokey || key >= hikey) {
614 memset(bmap->bitmapq, -1,
615 sizeof(bmap->bitmapq));
617 bmap->linear = HAMMER2_SEGSIZE;
619 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
621 size -= sizeof(*bmap);
622 key += HAMMER2_FREEMAP_LEVEL0_SIZE;
628 h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref)
630 hammer2_bmap_data_t *bmap;
631 hammer2_off_t data_off;
637 * Check for signal and allow yield to userland during scan.
639 if (hammer2_signal_check(&cbinfo->save_time))
640 return HAMMER2_ERROR_ABORTED;
643 * Deal with kernel thread cpu or I/O hogging by limiting the
644 * number of chains scanned per second to hammer2_bulkfree_tps.
645 * Ignore leaf records (DIRENT and DATA), no per-record I/O is
646 * involved for those since we don't load their data.
648 if (bref->type != HAMMER2_BREF_TYPE_DATA &&
649 bref->type != HAMMER2_BREF_TYPE_DIRENT) {
650 ++cbinfo->bulkfree_calls;
651 if (cbinfo->bulkfree_calls > hammer2_bulkfree_tps) {
652 int dticks = ticks - cbinfo->bulkfree_ticks;
656 tsleep(&cbinfo->bulkfree_ticks, 0,
657 "h2bw", hz - dticks);
659 cbinfo->bulkfree_calls = 0;
660 cbinfo->bulkfree_ticks = ticks;
665 * Calculate the data offset and determine if it is within
666 * the current freemap range being gathered.
668 data_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
669 if (data_off < cbinfo->sbase || data_off >= cbinfo->sstop)
671 if (data_off < cbinfo->hmp->voldata.allocator_beg)
673 if (data_off >= cbinfo->hmp->voldata.volu_size)
677 * Calculate the information needed to generate the in-memory
680 * Hammer2 does not allow allocations to cross the L1 (2GB) boundary,
681 * it's a problem if it does. (Or L0 (2MB) for that matter).
683 radix = (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
684 KKASSERT(radix != 0);
685 bytes = (size_t)1 << radix;
686 class = (bref->type << 8) | hammer2_devblkradix(radix);
688 if (data_off + bytes > cbinfo->sstop) {
689 kprintf("hammer2_bulkfree_scan: illegal 2GB boundary "
690 "%016jx %016jx/%d\n",
691 (intmax_t)bref->data_off,
694 bytes = cbinfo->sstop - data_off; /* XXX */
698 * Convert to a storage offset relative to the beginning of the
699 * storage range we are collecting. Then lookup the level0 bmap entry.
701 data_off -= cbinfo->sbase;
702 bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
705 * Convert data_off to a bmap-relative value (~4MB storage range).
706 * Adjust linear, class, and avail.
708 * Hammer2 does not allow allocations to cross the L0 (4MB) boundary,
710 data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
711 if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
712 kprintf("hammer2_bulkfree_scan: illegal 4MB boundary "
713 "%016jx %016jx/%d\n",
714 (intmax_t)bref->data_off,
717 bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
720 if (bmap->class == 0) {
722 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
726 * NOTE: bmap->class does not have to match class. Classification
727 * is relaxed when free space is low, so some mixing can occur.
733 if (bmap->class != class) {
734 kprintf("hammer2_bulkfree_scan: illegal mixed class "
735 "%016jx %016jx/%d (%04x vs %04x)\n",
736 (intmax_t)bref->data_off,
744 * Just record the highest byte-granular offset for now. Do not
745 * match against allocations which are in multiples of whole blocks.
747 * Make sure that any in-block linear offset at least covers the
748 * data range. This can cause bmap->linear to become block-aligned.
750 if (bytes & HAMMER2_FREEMAP_BLOCK_MASK) {
751 if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
752 bmap->linear = (int32_t)data_off + (int32_t)bytes;
753 } else if (bmap->linear >= (int32_t)data_off &&
754 bmap->linear < (int32_t)data_off + (int32_t)bytes) {
755 bmap->linear = (int32_t)data_off + (int32_t)bytes;
759 * Adjust the hammer2_bitmap_t bitmap[HAMMER2_BMAP_ELEMENTS].
760 * 64-bit entries, 2 bits per entry, to code 11.
762 * NOTE: data_off mask to 524288, shift right by 14 (radix for 16384),
763 * and multiply shift amount by 2 for sets of 2 bits.
765 * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
766 * also, data_off may not be FREEMAP_BLOCK_SIZE aligned.
769 hammer2_bitmap_t bmask;
772 bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
773 HAMMER2_BMAP_INDEX_RADIX);
774 bmask = (hammer2_bitmap_t)3 <<
775 ((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
776 HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
779 * NOTE! The (avail) calculation is bitmap-granular. Multiple
780 * sub-granular records can wind up at the same bitmap
783 if ((bmap->bitmapq[bindex] & bmask) == 0) {
784 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
785 bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
787 bmap->avail -= bytes;
789 bmap->bitmapq[bindex] |= bmask;
791 data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
792 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
795 bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
801 * Synchronize the in-memory bitmap with the live freemap. This is not a
802 * direct copy. Instead the bitmaps must be compared:
804 * In-memory Live-freemap
805 * 00 11 -> 10 (do nothing if live modified)
806 * 10 -> 00 (do nothing if live modified)
807 * 11 10 -> 11 handles race against live
808 * ** -> 11 nominally warn of corruption
810 * We must also fixup the hints in HAMMER2_BREF_TYPE_FREEMAP_LEAF.
813 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
815 hammer2_off_t data_off;
817 hammer2_key_t key_dummy;
818 hammer2_bmap_data_t *bmap;
819 hammer2_bmap_data_t *live;
820 hammer2_chain_t *live_parent;
821 hammer2_chain_t *live_chain;
825 kprintf("hammer2_bulkfree - range ");
827 if (cbinfo->sbase < cbinfo->hmp->voldata.allocator_beg)
829 (intmax_t)cbinfo->hmp->voldata.allocator_beg);
832 (intmax_t)cbinfo->sbase);
834 if (cbinfo->sstop > cbinfo->hmp->voldata.volu_size)
836 (intmax_t)cbinfo->hmp->voldata.volu_size);
839 (intmax_t)cbinfo->sstop);
841 data_off = cbinfo->sbase;
844 live_parent = &cbinfo->hmp->fchain;
845 hammer2_chain_ref(live_parent);
846 hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
851 * Iterate each hammer2_bmap_data_t line (128 bytes) managing
854 while (data_off < cbinfo->sstop) {
856 * The freemap is not used below allocator_beg or beyond
860 if (data_off < cbinfo->hmp->voldata.allocator_beg)
862 if (data_off >= cbinfo->hmp->voldata.volu_size)
866 * Locate the freemap leaf on the live filesystem
868 key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
870 if (live_chain == NULL || live_chain->bref.key != key) {
872 hammer2_chain_unlock(live_chain);
873 hammer2_chain_drop(live_chain);
875 live_chain = hammer2_chain_lookup(
879 key + HAMMER2_FREEMAP_LEVEL1_MASK,
881 HAMMER2_LOOKUP_ALWAYS);
883 kprintf("hammer2_bulkfree: freemap lookup "
884 "error near %016jx, error %s\n",
886 hammer2_error_str(live_chain->error));
890 if (live_chain == NULL) {
892 * XXX if we implement a full recovery mode we need
893 * to create/recreate missing freemap chains if our
894 * bmap has any allocated blocks.
897 bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
898 kprintf("hammer2_bulkfree: cannot locate "
899 "live leaf for allocated data "
905 if (live_chain->error) {
906 kprintf("hammer2_bulkfree: unable to access freemap "
907 "near %016jx, error %s\n",
909 hammer2_error_str(live_chain->error));
910 hammer2_chain_unlock(live_chain);
911 hammer2_chain_drop(live_chain);
916 bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
917 HAMMER2_FREEMAP_LEVEL0_RADIX;
918 live = &live_chain->data->bmdata[bmapindex];
921 * Shortcut if the bitmaps match and the live linear
922 * indicator is sane. We can't do a perfect check of
923 * live->linear because the only real requirement is that
924 * if it is not block-aligned, that it not cover the space
925 * within its current block which overlaps one of the data
926 * ranges we scan. We don't retain enough fine-grained
927 * data in our scan to be able to set it exactly.
929 * TODO - we could shortcut this by testing that both
930 * live->class and bmap->class are 0, and both avails are
931 * set to HAMMER2_FREEMAP_LEVEL0_SIZE (4MB).
933 if (bcmp(live->bitmapq, bmap->bitmapq,
934 sizeof(bmap->bitmapq)) == 0 &&
935 live->linear >= bmap->linear) {
938 if (hammer2_debug & 1) {
939 kprintf("live %016jx %04d.%04x (avail=%d)\n",
940 data_off, bmapindex, live->class, live->avail);
943 hammer2_chain_modify(live_chain, cbinfo->mtid, 0, 0);
944 live_chain->bref.check.freemap.bigmask = -1;
945 cbinfo->hmp->freemap_relaxed = 0; /* reset heuristic */
946 live = &live_chain->data->bmdata[bmapindex];
948 h2_bulkfree_sync_adjust(cbinfo, data_off, live, bmap,
949 live_chain->bref.key +
951 HAMMER2_FREEMAP_LEVEL0_SIZE);
953 data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
957 hammer2_chain_unlock(live_chain);
958 hammer2_chain_drop(live_chain);
961 hammer2_chain_unlock(live_parent);
962 hammer2_chain_drop(live_parent);
968 * Merge the bulkfree bitmap against the existing bitmap.
972 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
973 hammer2_off_t data_off, hammer2_bmap_data_t *live,
974 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base)
978 hammer2_off_t tmp_off;
979 hammer2_bitmap_t lmask;
980 hammer2_bitmap_t mmask;
984 for (bindex = 0; bindex < HAMMER2_BMAP_ELEMENTS; ++bindex) {
985 lmask = live->bitmapq[bindex]; /* live */
986 mmask = bmap->bitmapq[bindex]; /* snapshotted bulkfree */
987 if (lmask == mmask) {
988 tmp_off += HAMMER2_BMAP_INDEX_SIZE;
993 scount < HAMMER2_BMAP_BITS_PER_ELEMENT;
995 if ((mmask & 3) == 0) {
997 * in-memory 00 live 11 -> 10
1000 * Storage might be marked allocated or
1001 * staged and must be remarked staged or
1004 switch (lmask & 3) {
1008 kprintf("hammer2_bulkfree: cannot "
1009 "transition m=00/l=01\n");
1011 case 2: /* 10 -> 00 */
1012 live->bitmapq[bindex] &=
1013 ~((hammer2_bitmap_t)2 << scount);
1015 HAMMER2_FREEMAP_BLOCK_SIZE;
1017 HAMMER2_FREEMAP_LEVEL0_SIZE) {
1019 HAMMER2_FREEMAP_LEVEL0_SIZE;
1022 HAMMER2_FREEMAP_BLOCK_SIZE;
1023 ++cbinfo->count_10_00;
1024 hammer2_io_dedup_assert(
1027 HAMMER2_FREEMAP_BLOCK_RADIX,
1028 HAMMER2_FREEMAP_BLOCK_SIZE);
1030 case 3: /* 11 -> 10 */
1031 live->bitmapq[bindex] &=
1032 ~((hammer2_bitmap_t)1 << scount);
1033 ++cbinfo->count_11_10;
1034 hammer2_io_dedup_delete(
1036 HAMMER2_BREF_TYPE_DATA,
1038 HAMMER2_FREEMAP_BLOCK_RADIX,
1039 HAMMER2_FREEMAP_BLOCK_SIZE);
1042 } else if ((mmask & 3) == 3) {
1044 * in-memory 11 live 10 -> 11
1047 * Storage might be incorrectly marked free
1048 * or staged and must be remarked fully
1051 switch (lmask & 3) {
1053 ++cbinfo->count_00_11;
1055 HAMMER2_FREEMAP_BLOCK_SIZE;
1057 HAMMER2_FREEMAP_BLOCK_SIZE;
1058 if ((int32_t)live->avail < 0)
1062 ++cbinfo->count_01_11;
1064 case 2: /* 10 -> 11 */
1065 ++cbinfo->count_10_11;
1070 live->bitmapq[bindex] |=
1071 ((hammer2_bitmap_t)3 << scount);
1075 tmp_off += HAMMER2_FREEMAP_BLOCK_SIZE;
1080 * Determine if the live bitmap is completely free and reset its
1081 * fields if so. Otherwise check to see if we can reduce the linear
1084 for (bindex = HAMMER2_BMAP_ELEMENTS - 1; bindex >= 0; --bindex) {
1085 if (live->bitmapq[bindex] != 0)
1090 * Completely empty, reset entire segment
1093 kprintf("hammer2: cleanseg %016jx.%04x (%d)\n",
1094 alloc_base, live->class, live->avail);
1096 live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
1099 ++cbinfo->count_l0cleans;
1100 } else if (bindex < 7) {
1102 * Partially full, bitmapq[bindex] != 0. Our bulkfree pass
1103 * does not record enough information to set live->linear
1106 * NOTE: Setting live->linear to a sub-block (16K) boundary
1107 * forces the live code to iterate to the next fully
1108 * free block. It does NOT mean that all blocks above
1109 * live->linear are available.
1111 * Setting live->linear to a fragmentary (less than
1112 * 16K) boundary allows allocations to iterate within
1115 if (live->linear < bmap->linear &&
1116 ((live->linear ^ bmap->linear) &
1117 ~HAMMER2_FREEMAP_BLOCK_MASK) == 0) {
1119 * If greater than but still within the same
1120 * sub-block as live we can adjust linear upward.
1122 live->linear = bmap->linear;
1123 ++cbinfo->count_linadjusts;
1126 * Otherwise adjust to the nearest higher or same
1127 * sub-block boundary. The live system may have
1128 * bounced live->linear around so we cannot make any
1129 * assumptions with regards to available fragmentary
1133 (bmap->linear + HAMMER2_FREEMAP_BLOCK_MASK) &
1134 ~HAMMER2_FREEMAP_BLOCK_MASK;
1135 ++cbinfo->count_linadjusts;
1139 * Completely full, effectively disable the linear iterator
1141 live->linear = HAMMER2_SEGSIZE;
1146 kprintf("%016jx %04d.%04x (avail=%7d) "
1147 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
1150 HAMMER2_FREEMAP_LEVEL1_MASK) >>
1151 HAMMER2_FREEMAP_LEVEL0_RADIX),
1154 bmap->bitmap[0], bmap->bitmap[1],
1155 bmap->bitmap[2], bmap->bitmap[3],
1156 bmap->bitmap[4], bmap->bitmap[5],
1157 bmap->bitmap[6], bmap->bitmap[7]);
1163 * BULKFREE DEDUP HEURISTIC
1165 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1166 * All fields must be loaded into locals and validated.
1170 h2_bulkfree_test(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref,
1173 hammer2_dedup_t *dedup;
1178 n = hammer2_icrc32(&bref->data_off, sizeof(bref->data_off));
1179 dedup = cbinfo->dedup + (n & (HAMMER2_DEDUP_HEUR_MASK & ~7));
1181 for (i = best = 0; i < 8; ++i) {
1182 if (dedup[i].data_off == bref->data_off) {
1183 if (dedup[i].ticks < pri)
1184 dedup[i].ticks = pri;
1186 cbinfo->count_dedup_factor += dedup[i].ticks;
1189 if (dedup[i].ticks < dedup[best].ticks)
1192 dedup[best].data_off = bref->data_off;
1193 dedup[best].ticks = pri;