2 * Copyright (c) 2013-2015 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>
49 #define H2FMBASE(key, radix) ((key) & ~(((hammer2_off_t)1 << (radix)) - 1))
50 #define H2FMSHIFT(radix) ((hammer2_off_t)1 << (radix))
53 * breadth-first search
55 typedef struct hammer2_chain_save {
56 TAILQ_ENTRY(hammer2_chain_save) entry;
57 hammer2_chain_t *chain;
59 } hammer2_chain_save_t;
61 TAILQ_HEAD(hammer2_chain_save_list, hammer2_chain_save);
62 typedef struct hammer2_chain_save_list hammer2_chain_save_list_t;
64 typedef struct hammer2_bulkfree_info {
67 hammer2_off_t sbase; /* sub-loop iteration */
69 hammer2_bmap_data_t *bmap;
71 long count_10_00; /* staged->free */
72 long count_11_10; /* allocated->staged */
73 long count_00_11; /* (should not happen) */
74 long count_01_11; /* (should not happen) */
75 long count_10_11; /* staged->allocated */
77 long count_linadjusts;
78 long count_inodes_scanned;
79 long count_dedup_factor;
81 hammer2_off_t adj_free;
83 hammer2_tid_t saved_mirror_tid;
85 hammer2_chain_save_list_t list;
86 hammer2_dedup_t *dedup;
88 } hammer2_bulkfree_info_t;
90 static int h2_bulkfree_test(hammer2_bulkfree_info_t *info,
91 hammer2_blockref_t *bref, int pri);
94 * General bulk scan function with callback. Called with a referenced
95 * but UNLOCKED parent. The parent is returned in the same state.
99 hammer2_bulk_scan(hammer2_chain_t *parent,
100 int (*func)(hammer2_bulkfree_info_t *info,
101 hammer2_blockref_t *bref),
102 hammer2_bulkfree_info_t *info)
104 hammer2_blockref_t bref;
105 hammer2_chain_t *chain;
106 int cache_index = -1;
113 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
114 HAMMER2_RESOLVE_SHARED);
120 * Generally loop on the contents if we have not been flagged
123 * Remember that these chains are completely isolated from
124 * the frontend, so we can release locks temporarily without
128 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 * Process bref, chain is only non-NULL if the bref
142 * might be recursable (its possible that we sometimes get
143 * a non-NULL chain where the bref cannot be recursed).
146 if (h2_bulkfree_test(info, &bref, 1))
149 error |= func(info, &bref);
154 * A non-null chain is always returned if it is
155 * recursive, otherwise a non-null chain might be
156 * returned but usually is not when not recursive.
162 * Else check type and setup depth-first scan.
164 * Account for bytes actually read.
166 info->bytes_scanned += chain->bytes;
168 switch(chain->bref.type) {
169 case HAMMER2_BREF_TYPE_INODE:
170 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
171 case HAMMER2_BREF_TYPE_INDIRECT:
172 case HAMMER2_BREF_TYPE_VOLUME:
173 case HAMMER2_BREF_TYPE_FREEMAP:
175 if (info->depth > 16) {
176 hammer2_chain_save_t *save;
177 save = kmalloc(sizeof(*save), M_HAMMER2,
180 hammer2_chain_ref(chain);
181 TAILQ_INSERT_TAIL(&info->list, save, entry);
186 int savepri = info->pri;
188 hammer2_chain_unlock(chain);
191 hammer2_bulk_scan(chain, func, info);
192 info->pri += savepri;
193 hammer2_chain_lock(chain,
194 HAMMER2_RESOLVE_ALWAYS |
195 HAMMER2_RESOLVE_SHARED);
200 /* does not recurse */
203 if (rup_error & HAMMER2_ERROR_ABORTED)
207 hammer2_chain_unlock(chain);
208 hammer2_chain_drop(chain);
212 * Save with higher pri now that we know what it is.
214 h2_bulkfree_test(info, &parent->bref, info->pri + 1);
216 hammer2_chain_unlock(parent);
218 return ((error | rup_error) & ~HAMMER2_ERROR_EOF);
225 * Chain flush (partial synchronization) XXX removed
226 * Scan the whole topology - build in-memory freemap (mark 11)
227 * Reconcile the in-memory freemap against the on-disk freemap.
228 * ondisk xx -> ondisk 11 (if allocated)
229 * ondisk 11 -> ondisk 10 (if free in-memory)
230 * ondisk 10 -> ondisk 00 (if free in-memory) - on next pass
233 * The topology scan may have to be performed multiple times to window
234 * freemaps which are too large to fit in kernel memory.
236 * Races are handled using a double-transition (11->10, 10->00). The bulkfree
237 * scan snapshots the volume root's blockset and thus can run concurrent with
238 * normal operations, as long as a full flush is made between each pass to
239 * synchronize any modified chains (otherwise their blocks might be improperly
242 * Temporary memory in multiples of 64KB is required to reconstruct the leaf
243 * hammer2_bmap_data blocks so they can later be compared against the live
244 * freemap. Each 64KB block represents 128 x 16KB x 1024 = ~2 GB of storage.
245 * A 32MB save area thus represents around ~1 TB. The temporary memory
246 * allocated can be specified. If it is not sufficient multiple topology
247 * passes will be made.
251 * Bulkfree callback info
253 static void hammer2_bulkfree_thread(void *arg __unused);
254 static void cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size);
255 static int h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo,
256 hammer2_blockref_t *bref);
257 static void h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
258 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
259 hammer2_off_t data_off, hammer2_bmap_data_t *live,
260 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base);
263 hammer2_bulkfree_init(hammer2_dev_t *hmp)
265 hammer2_thr_create(&hmp->bfthr, NULL, hmp,
266 hmp->devrepname, -1, -1,
267 hammer2_bulkfree_thread);
271 hammer2_bulkfree_uninit(hammer2_dev_t *hmp)
273 hammer2_thr_delete(&hmp->bfthr);
277 hammer2_bulkfree_thread(void *arg)
279 hammer2_thread_t *thr = arg;
280 hammer2_ioc_bulkfree_t bfi;
284 hammer2_thr_wait_any(thr,
285 HAMMER2_THREAD_STOP |
286 HAMMER2_THREAD_FREEZE |
287 HAMMER2_THREAD_UNFREEZE |
288 HAMMER2_THREAD_REMASTER,
293 if (flags & HAMMER2_THREAD_STOP)
295 if (flags & HAMMER2_THREAD_FREEZE) {
296 hammer2_thr_signal2(thr, HAMMER2_THREAD_FROZEN,
297 HAMMER2_THREAD_FREEZE);
300 if (flags & HAMMER2_THREAD_UNFREEZE) {
301 hammer2_thr_signal2(thr, 0,
302 HAMMER2_THREAD_FROZEN |
303 HAMMER2_THREAD_UNFREEZE);
306 if (flags & HAMMER2_THREAD_FROZEN)
308 if (flags & HAMMER2_THREAD_REMASTER) {
309 hammer2_thr_signal2(thr, 0, HAMMER2_THREAD_REMASTER);
310 bzero(&bfi, sizeof(bfi));
311 bfi.size = 8192 * 1024;
312 /* hammer2_bulkfree_pass(thr->hmp, &bfi); */
316 hammer2_thr_signal(thr, HAMMER2_THREAD_STOPPED);
317 /* structure can go invalid at this point */
321 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
322 hammer2_ioc_bulkfree_t *bfi)
324 hammer2_bulkfree_info_t cbinfo;
325 hammer2_chain_save_t *save;
331 * We have to clear the live dedup cache as it might have entries
332 * that are freeable as of now. Any new entries in the dedup cache
333 * made after this point, even if they become freeable, will have
334 * previously been fully allocated and will be protected by the
337 hammer2_dedup_clear(hmp);
340 * Setup for free pass
342 bzero(&cbinfo, sizeof(cbinfo));
343 size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
344 ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
346 cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size, VM_SUBSYS_HAMMER);
347 cbinfo.saved_mirror_tid = hmp->voldata.mirror_tid;
349 cbinfo.dedup = kmalloc(sizeof(*cbinfo.dedup) * HAMMER2_DEDUP_HEUR_SIZE,
350 M_HAMMER2, M_WAITOK | M_ZERO);
353 * Normalize start point to a 2GB boundary. We operate on a
354 * 64KB leaf bitmap boundary which represents 2GB of storage.
356 cbinfo.sbase = bfi->sbase;
357 if (cbinfo.sbase > hmp->voldata.volu_size)
358 cbinfo.sbase = hmp->voldata.volu_size;
359 cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
360 TAILQ_INIT(&cbinfo.list);
363 * Loop on a full meta-data scan as many times as required to
364 * get through all available storage.
367 while (cbinfo.sbase < hmp->voldata.volu_size) {
369 * We have enough ram to represent (incr) bytes of storage.
370 * Each 64KB of ram represents 2GB of storage.
372 * We must also clean out our de-duplication heuristic for
373 * each (incr) bytes of storage, otherwise we wind up not
374 * scanning meta-data for later areas of storage because
375 * they had already been scanned in earlier areas of storage.
376 * Since the ranging is different, we have to restart
377 * the dedup heuristic too.
379 cbinfo_bmap_init(&cbinfo, size);
380 bzero(cbinfo.dedup, sizeof(*cbinfo.dedup) *
381 HAMMER2_DEDUP_HEUR_SIZE);
382 incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
383 HAMMER2_FREEMAP_LEVEL1_SIZE;
384 if (hmp->voldata.volu_size - cbinfo.sbase < incr)
385 cbinfo.sstop = hmp->voldata.volu_size;
387 cbinfo.sstop = cbinfo.sbase + incr;
388 if (hammer2_debug & 1) {
389 kprintf("bulkfree pass %016jx/%jdGB\n",
390 (intmax_t)cbinfo.sbase,
391 (intmax_t)incr / HAMMER2_FREEMAP_LEVEL1_SIZE);
395 * Scan topology for stuff inside this range.
397 hammer2_trans_init(hmp->spmp, 0);
398 cbinfo.mtid = hammer2_trans_sub(hmp->spmp);
400 error |= hammer2_bulk_scan(vchain, h2_bulkfree_callback,
403 while ((save = TAILQ_FIRST(&cbinfo.list)) != NULL &&
405 TAILQ_REMOVE(&cbinfo.list, save, entry);
407 error |= hammer2_bulk_scan(save->chain,
408 h2_bulkfree_callback,
410 hammer2_chain_drop(save->chain);
411 kfree(save, M_HAMMER2);
414 TAILQ_REMOVE(&cbinfo.list, save, entry);
415 hammer2_chain_drop(save->chain);
416 kfree(save, M_HAMMER2);
417 save = TAILQ_FIRST(&cbinfo.list);
420 kprintf("bulkfree lastdrop %d %d error=0x%04x\n",
421 vchain->refs, vchain->core.chain_count, error);
424 * If complete scan succeeded we can synchronize our
425 * in-memory freemap against live storage. If an abort
426 * did occur we cannot safely synchronize our partially
427 * filled-out in-memory freemap.
430 h2_bulkfree_sync(&cbinfo);
432 hammer2_voldata_lock(hmp);
433 hammer2_voldata_modify(hmp);
434 hmp->voldata.allocator_free += cbinfo.adj_free;
435 hammer2_voldata_unlock(hmp);
439 * Cleanup for next loop.
441 hammer2_trans_done(hmp->spmp);
444 cbinfo.sbase = cbinfo.sstop;
447 kmem_free_swapbacked(&cbinfo.kp);
448 kfree(cbinfo.dedup, M_HAMMER2);
451 bfi->sstop = cbinfo.sbase;
453 incr = bfi->sstop / (hmp->voldata.volu_size / 10000);
457 kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
462 kprintf(" bulkfree was aborted\n");
464 kprintf(" transition->free %ld\n", cbinfo.count_10_00);
465 kprintf(" transition->staged %ld\n", cbinfo.count_11_10);
466 kprintf(" ERR(00)->allocated %ld\n", cbinfo.count_00_11);
467 kprintf(" ERR(01)->allocated %ld\n", cbinfo.count_01_11);
468 kprintf(" staged->allocated %ld\n", cbinfo.count_10_11);
469 kprintf(" ~2MB segs cleaned %ld\n", cbinfo.count_l0cleans);
470 kprintf(" linear adjusts %ld\n",
471 cbinfo.count_linadjusts);
472 kprintf(" dedup factor %ld\n",
473 cbinfo.count_dedup_factor);
480 cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size)
482 hammer2_bmap_data_t *bmap = cbinfo->bmap;
483 hammer2_key_t key = cbinfo->sbase;
487 lokey = (cbinfo->hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
489 hikey = cbinfo->hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
493 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
494 HAMMER2_ZONE_SEG64) {
495 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
498 if (key < lokey || key >= hikey) {
499 memset(bmap->bitmapq, -1,
500 sizeof(bmap->bitmapq));
502 bmap->linear = HAMMER2_SEGSIZE;
504 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
506 size -= sizeof(*bmap);
507 key += HAMMER2_FREEMAP_LEVEL0_SIZE;
513 h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref)
515 hammer2_bmap_data_t *bmap;
516 hammer2_off_t data_off;
522 * Check for signal and allow yield to userland during scan
524 if (hammer2_signal_check(&cbinfo->save_time))
525 return HAMMER2_ERROR_ABORTED;
527 if (bref->type == HAMMER2_BREF_TYPE_INODE) {
528 ++cbinfo->count_inodes_scanned;
529 if ((cbinfo->count_inodes_scanned & 65535) == 0)
530 kprintf(" inodes %6ld bytes %9ld\n",
531 cbinfo->count_inodes_scanned,
532 cbinfo->bytes_scanned);
536 * Calculate the data offset and determine if it is within
537 * the current freemap range being gathered.
539 data_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
540 if (data_off < cbinfo->sbase || data_off >= cbinfo->sstop)
542 if (data_off < cbinfo->hmp->voldata.allocator_beg)
544 if (data_off >= cbinfo->hmp->voldata.volu_size)
548 * Calculate the information needed to generate the in-memory
551 * Hammer2 does not allow allocations to cross the L1 (2GB) boundary,
552 * it's a problem if it does. (Or L0 (2MB) for that matter).
554 radix = (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
555 KKASSERT(radix != 0);
556 bytes = (size_t)1 << radix;
557 class = (bref->type << 8) | hammer2_devblkradix(radix);
559 if (data_off + bytes >= cbinfo->sstop) {
560 kprintf("hammer2_bulkfree_scan: illegal 2GB boundary "
561 "%016jx %016jx/%d\n",
562 (intmax_t)bref->data_off,
565 bytes = cbinfo->sstop - data_off; /* XXX */
569 * Convert to a storage offset relative to the beginning of the
570 * storage range we are collecting. Then lookup the level0 bmap entry.
572 data_off -= cbinfo->sbase;
573 bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
576 * Convert data_off to a bmap-relative value (~4MB storage range).
577 * Adjust linear, class, and avail.
579 * Hammer2 does not allow allocations to cross the L0 (4MB) boundary,
581 data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
582 if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
583 kprintf("hammer2_bulkfree_scan: illegal 4MB boundary "
584 "%016jx %016jx/%d\n",
585 (intmax_t)bref->data_off,
588 bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
591 if (bmap->class == 0) {
593 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
595 if (bmap->class != class) {
596 kprintf("hammer2_bulkfree_scan: illegal mixed class "
597 "%016jx %016jx/%d (%04x vs %04x)\n",
598 (intmax_t)bref->data_off,
605 * Just record the highest byte-granular offset for now. Do not
606 * match against allocations which are in multiples of whole blocks.
608 * Make sure that any in-block linear offset at least covers the
609 * data range. This can cause bmap->linear to become block-aligned.
611 if (bytes & HAMMER2_FREEMAP_BLOCK_MASK) {
612 if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
613 bmap->linear = (int32_t)data_off + (int32_t)bytes;
614 } else if (bmap->linear >= (int32_t)data_off &&
615 bmap->linear < (int32_t)data_off + (int32_t)bytes) {
616 bmap->linear = (int32_t)data_off + (int32_t)bytes;
620 * Adjust the hammer2_bitmap_t bitmap[HAMMER2_BMAP_ELEMENTS].
621 * 64-bit entries, 2 bits per entry, to code 11.
623 * NOTE: data_off mask to 524288, shift right by 14 (radix for 16384),
624 * and multiply shift amount by 2 for sets of 2 bits.
626 * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
627 * also, data_off may not be FREEMAP_BLOCK_SIZE aligned.
630 hammer2_bitmap_t bmask;
633 bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
634 HAMMER2_BMAP_INDEX_RADIX);
635 bmask = (hammer2_bitmap_t)3 <<
636 ((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
637 HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
640 * NOTE! The (avail) calculation is bitmap-granular. Multiple
641 * sub-granular records can wind up at the same bitmap
644 if ((bmap->bitmapq[bindex] & bmask) == 0) {
645 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
646 bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
648 bmap->avail -= bytes;
650 bmap->bitmapq[bindex] |= bmask;
652 data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
653 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
656 bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
662 * Synchronize the in-memory bitmap with the live freemap. This is not a
663 * direct copy. Instead the bitmaps must be compared:
665 * In-memory Live-freemap
666 * 00 11 -> 10 (do nothing if live modified)
667 * 10 -> 00 (do nothing if live modified)
668 * 11 10 -> 11 handles race against live
669 * ** -> 11 nominally warn of corruption
673 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
675 hammer2_off_t data_off;
677 hammer2_key_t key_dummy;
678 hammer2_bmap_data_t *bmap;
679 hammer2_bmap_data_t *live;
680 hammer2_chain_t *live_parent;
681 hammer2_chain_t *live_chain;
682 int cache_index = -1;
685 kprintf("hammer2_bulkfree - range ");
687 if (cbinfo->sbase < cbinfo->hmp->voldata.allocator_beg)
689 (intmax_t)cbinfo->hmp->voldata.allocator_beg);
692 (intmax_t)cbinfo->sbase);
694 if (cbinfo->sstop > cbinfo->hmp->voldata.volu_size)
696 (intmax_t)cbinfo->hmp->voldata.volu_size);
699 (intmax_t)cbinfo->sstop);
701 data_off = cbinfo->sbase;
704 live_parent = &cbinfo->hmp->fchain;
705 hammer2_chain_ref(live_parent);
706 hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
710 * Iterate each hammer2_bmap_data_t line (128 bytes) managing
713 while (data_off < cbinfo->sstop) {
715 * The freemap is not used below allocator_beg or beyond
719 if (data_off < cbinfo->hmp->voldata.allocator_beg)
721 if (data_off >= cbinfo->hmp->voldata.volu_size)
725 * Locate the freemap leaf on the live filesystem
727 key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
729 if (live_chain == NULL || live_chain->bref.key != key) {
731 hammer2_chain_unlock(live_chain);
732 hammer2_chain_drop(live_chain);
734 live_chain = hammer2_chain_lookup(
738 key + HAMMER2_FREEMAP_LEVEL1_MASK,
740 HAMMER2_LOOKUP_ALWAYS);
744 * If recent allocations were made we avoid races by
745 * not staging or freeing any blocks. We can still
746 * remark blocks as fully allocated.
749 if (hammer2_debug & 1) {
750 kprintf("live_chain %016jx\n",
753 if (live_chain->bref.mirror_tid >
754 cbinfo->saved_mirror_tid) {
755 kprintf("hammer2_bulkfree: "
765 if (live_chain == NULL) {
767 * XXX if we implement a full recovery mode we need
768 * to create/recreate missing freemap chains if our
769 * bmap has any allocated blocks.
772 bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
773 kprintf("hammer2_bulkfree: cannot locate "
774 "live leaf for allocated data "
780 if (live_chain->error) {
781 kprintf("hammer2_bulkfree: error %s looking up "
782 "live leaf for allocated data near %016jx\n",
783 hammer2_error_str(live_chain->error),
785 hammer2_chain_unlock(live_chain);
786 hammer2_chain_drop(live_chain);
791 bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
792 HAMMER2_FREEMAP_LEVEL0_RADIX;
793 live = &live_chain->data->bmdata[bmapindex];
796 * Shortcut if the bitmaps match and the live linear
797 * indicator is sane. We can't do a perfect check of
798 * live->linear because the only real requirement is that
799 * if it is not block-aligned, that it not cover the space
800 * within its current block which overlaps one of the data
801 * ranges we scan. We don't retain enough fine-grained
802 * data in our scan to be able to set it exactly.
804 * TODO - we could shortcut this by testing that both
805 * live->class and bmap->class are 0, and both avails are
806 * set to HAMMER2_FREEMAP_LEVEL0_SIZE (4MB).
808 if (bcmp(live->bitmapq, bmap->bitmapq,
809 sizeof(bmap->bitmapq)) == 0 &&
810 live->linear >= bmap->linear) {
813 if (hammer2_debug & 1) {
814 kprintf("live %016jx %04d.%04x (avail=%d)\n",
815 data_off, bmapindex, live->class, live->avail);
818 hammer2_chain_modify(live_chain, cbinfo->mtid, 0, 0);
819 live = &live_chain->data->bmdata[bmapindex];
821 h2_bulkfree_sync_adjust(cbinfo, data_off, live, bmap,
822 live_chain->bref.key +
824 HAMMER2_FREEMAP_LEVEL0_SIZE);
826 data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
830 hammer2_chain_unlock(live_chain);
831 hammer2_chain_drop(live_chain);
834 hammer2_chain_unlock(live_parent);
835 hammer2_chain_drop(live_parent);
840 * Merge the bulkfree bitmap against the existing bitmap.
844 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
845 hammer2_off_t data_off, hammer2_bmap_data_t *live,
846 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base)
850 hammer2_off_t tmp_off;
851 hammer2_bitmap_t lmask;
852 hammer2_bitmap_t mmask;
856 for (bindex = 0; bindex < HAMMER2_BMAP_ELEMENTS; ++bindex) {
857 lmask = live->bitmapq[bindex]; /* live */
858 mmask = bmap->bitmapq[bindex]; /* snapshotted bulkfree */
859 if (lmask == mmask) {
860 tmp_off += HAMMER2_BMAP_INDEX_SIZE;
865 scount < HAMMER2_BMAP_BITS_PER_ELEMENT;
867 if ((mmask & 3) == 0) {
869 * in-memory 00 live 11 -> 10
872 * Storage might be marked allocated or
873 * staged and must be remarked staged or
880 kprintf("hammer2_bulkfree: cannot "
881 "transition m=00/l=01\n");
883 case 2: /* 10 -> 00 */
884 live->bitmapq[bindex] &=
885 ~((hammer2_bitmap_t)2 << scount);
887 HAMMER2_FREEMAP_BLOCK_SIZE;
889 HAMMER2_FREEMAP_LEVEL0_SIZE) {
891 HAMMER2_FREEMAP_LEVEL0_SIZE;
894 HAMMER2_FREEMAP_BLOCK_SIZE;
895 ++cbinfo->count_10_00;
896 hammer2_io_dedup_assert(
899 HAMMER2_FREEMAP_BLOCK_RADIX,
900 HAMMER2_FREEMAP_BLOCK_SIZE);
902 case 3: /* 11 -> 10 */
903 live->bitmapq[bindex] &=
904 ~((hammer2_bitmap_t)1 << scount);
905 ++cbinfo->count_11_10;
906 hammer2_io_dedup_delete(
908 HAMMER2_BREF_TYPE_DATA,
910 HAMMER2_FREEMAP_BLOCK_RADIX,
911 HAMMER2_FREEMAP_BLOCK_SIZE);
914 } else if ((mmask & 3) == 3) {
916 * in-memory 11 live 10 -> 11
919 * Storage might be incorrectly marked free
920 * or staged and must be remarked fully
925 ++cbinfo->count_00_11;
927 HAMMER2_FREEMAP_BLOCK_SIZE;
929 HAMMER2_FREEMAP_BLOCK_SIZE;
930 if ((int32_t)live->avail < 0)
934 ++cbinfo->count_01_11;
936 case 2: /* 10 -> 11 */
937 ++cbinfo->count_10_11;
942 live->bitmapq[bindex] |=
943 ((hammer2_bitmap_t)3 << scount);
947 tmp_off += HAMMER2_FREEMAP_BLOCK_SIZE;
952 * Determine if the live bitmap is completely free and reset its
953 * fields if so. Otherwise check to see if we can reduce the linear
956 for (bindex = HAMMER2_BMAP_ELEMENTS - 1; bindex >= 0; --bindex) {
957 if (live->bitmapq[bindex] != 0)
962 * Completely empty, reset entire segment
965 kprintf("hammer2: cleanseg %016jx.%04x (%d)\n",
966 alloc_base, live->class, live->avail);
968 live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
971 ++cbinfo->count_l0cleans;
972 } else if (bindex < 7) {
974 * Partially full, bitmapq[bindex] != 0. The live->linear
975 * offset can legitimately be just about anything, but
976 * our bulkfree pass doesn't record enough information to
977 * set it exactly. Just make sure that it is set to a
978 * safe value that also works in our match code above (the
979 * bcmp and linear test).
981 * We cannot safely leave live->linear at a sub-block offset
982 * unless it is already in the same block as bmap->linear.
984 * If it is not in the same block, we cannot assume that
985 * we can set it to bmap->linear on a sub-block boundary,
986 * because the live system could have bounced it around.
987 * In that situation we satisfy our bcmp/skip requirement
988 * above by setting it to the nearest higher block boundary.
989 * This alignment effectively kills any partial allocation it
990 * might have been tracking before.
992 if (live->linear < bmap->linear &&
993 ((live->linear ^ bmap->linear) &
994 ~HAMMER2_FREEMAP_BLOCK_MASK) == 0) {
995 live->linear = bmap->linear;
996 ++cbinfo->count_linadjusts;
999 (bmap->linear + HAMMER2_FREEMAP_BLOCK_MASK) &
1000 ~HAMMER2_FREEMAP_BLOCK_MASK;
1001 ++cbinfo->count_linadjusts;
1005 * Completely full, effectively disable the linear iterator
1007 live->linear = HAMMER2_SEGSIZE;
1012 kprintf("%016jx %04d.%04x (avail=%7d) "
1013 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
1016 HAMMER2_FREEMAP_LEVEL1_MASK) >>
1017 HAMMER2_FREEMAP_LEVEL0_RADIX),
1020 bmap->bitmap[0], bmap->bitmap[1],
1021 bmap->bitmap[2], bmap->bitmap[3],
1022 bmap->bitmap[4], bmap->bitmap[5],
1023 bmap->bitmap[6], bmap->bitmap[7]);
1029 * BULKFREE DEDUP HEURISTIC
1031 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1032 * All fields must be loaded into locals and validated.
1036 h2_bulkfree_test(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref,
1039 hammer2_dedup_t *dedup;
1044 n = hammer2_icrc32(&bref->data_off, sizeof(bref->data_off));
1045 dedup = cbinfo->dedup + (n & (HAMMER2_DEDUP_HEUR_MASK & ~7));
1047 for (i = best = 0; i < 8; ++i) {
1048 if (dedup[i].data_off == bref->data_off) {
1049 if (dedup[i].ticks < pri)
1050 dedup[i].ticks = pri;
1052 cbinfo->count_dedup_factor += dedup[i].ticks;
1055 if (dedup[i].ticks < dedup[best].ticks)
1058 dedup[best].data_off = bref->data_off;
1059 dedup[best].ticks = pri;