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;
112 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
113 HAMMER2_RESOLVE_SHARED);
117 * Generally loop on the contents if we have not been flagged
120 * Remember that these chains are completely isolated from
121 * the frontend, so we can release locks temporarily without
124 while ((doabort & HAMMER2_BULK_ABORT) == 0 &&
125 hammer2_chain_scan(parent, &chain, &bref, &first,
127 HAMMER2_LOOKUP_NODATA |
128 HAMMER2_LOOKUP_SHARED) != NULL) {
130 * Process bref, chain is only non-NULL if the bref
131 * might be recursable (its possible that we sometimes get
132 * a non-NULL chain where the bref cannot be recursed).
135 kprintf("SCAN %p/%p %016jx.%02x\n",
136 parent, chain, bref.data_off, bref.type);
137 int xerr = tsleep(&info->pri, PCATCH, "slp", hz / 10);
138 if (xerr == EINTR || xerr == ERESTART) {
139 doabort |= HAMMER2_BULK_ABORT;
143 if (h2_bulkfree_test(info, &bref, 1))
146 doabort |= func(info, &bref);
148 if (doabort & HAMMER2_BULK_ABORT)
152 * A non-null chain is always returned if it is
153 * recursive, otherwise a non-null chain might be
154 * returned but usually is not when not recursive.
160 * Else check type and setup depth-first scan.
162 * Account for bytes actually read.
164 info->bytes_scanned += chain->bytes;
166 switch(chain->bref.type) {
167 case HAMMER2_BREF_TYPE_INODE:
168 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
169 case HAMMER2_BREF_TYPE_INDIRECT:
170 case HAMMER2_BREF_TYPE_VOLUME:
171 case HAMMER2_BREF_TYPE_FREEMAP:
173 if (info->depth > 16) {
174 hammer2_chain_save_t *save;
175 save = kmalloc(sizeof(*save), M_HAMMER2,
178 hammer2_chain_ref(chain);
179 TAILQ_INSERT_TAIL(&info->list, save, entry);
184 int savepri = info->pri;
186 hammer2_chain_unlock(chain);
188 doabort |= hammer2_bulk_scan(chain, func, info);
189 info->pri += savepri;
190 hammer2_chain_lock(chain,
191 HAMMER2_RESOLVE_ALWAYS |
192 HAMMER2_RESOLVE_SHARED);
197 /* does not recurse */
202 hammer2_chain_unlock(chain);
203 hammer2_chain_drop(chain);
207 * Save with higher pri now that we know what it is.
209 h2_bulkfree_test(info, &parent->bref, info->pri + 1);
211 hammer2_chain_unlock(parent);
220 * Chain flush (partial synchronization) XXX removed
221 * Scan the whole topology - build in-memory freemap (mark 11)
222 * Reconcile the in-memory freemap against the on-disk freemap.
223 * ondisk xx -> ondisk 11 (if allocated)
224 * ondisk 11 -> ondisk 10 (if free in-memory)
225 * ondisk 10 -> ondisk 00 (if free in-memory) - on next pass
228 * The topology scan may have to be performed multiple times to window
229 * freemaps which are too large to fit in kernel memory.
231 * Races are handled using a double-transition (11->10, 10->00). The bulkfree
232 * scan snapshots the volume root's blockset and thus can run concurrent with
233 * normal operations, as long as a full flush is made between each pass to
234 * synchronize any modified chains (otherwise their blocks might be improperly
237 * Temporary memory in multiples of 64KB is required to reconstruct the leaf
238 * hammer2_bmap_data blocks so they can later be compared against the live
239 * freemap. Each 64KB block represents 128 x 16KB x 1024 = ~2 GB of storage.
240 * A 32MB save area thus represents around ~1 TB. The temporary memory
241 * allocated can be specified. If it is not sufficient multiple topology
242 * passes will be made.
246 * Bulkfree callback info
248 static void hammer2_bulkfree_thread(void *arg __unused);
249 static void cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size);
250 static int h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo,
251 hammer2_blockref_t *bref);
252 static void h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
253 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
254 hammer2_off_t data_off, hammer2_bmap_data_t *live,
255 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base);
258 hammer2_bulkfree_init(hammer2_dev_t *hmp)
260 hammer2_thr_create(&hmp->bfthr, NULL, hmp,
261 hmp->devrepname, -1, -1,
262 hammer2_bulkfree_thread);
266 hammer2_bulkfree_uninit(hammer2_dev_t *hmp)
268 hammer2_thr_delete(&hmp->bfthr);
272 hammer2_bulkfree_thread(void *arg)
274 hammer2_thread_t *thr = arg;
275 hammer2_ioc_bulkfree_t bfi;
279 hammer2_thr_wait_any(thr,
280 HAMMER2_THREAD_STOP |
281 HAMMER2_THREAD_FREEZE |
282 HAMMER2_THREAD_UNFREEZE |
283 HAMMER2_THREAD_REMASTER,
288 if (flags & HAMMER2_THREAD_STOP)
290 if (flags & HAMMER2_THREAD_FREEZE) {
291 hammer2_thr_signal2(thr, HAMMER2_THREAD_FROZEN,
292 HAMMER2_THREAD_FREEZE);
295 if (flags & HAMMER2_THREAD_UNFREEZE) {
296 hammer2_thr_signal2(thr, 0,
297 HAMMER2_THREAD_FROZEN |
298 HAMMER2_THREAD_UNFREEZE);
301 if (flags & HAMMER2_THREAD_FROZEN)
303 if (flags & HAMMER2_THREAD_REMASTER) {
304 hammer2_thr_signal2(thr, 0, HAMMER2_THREAD_REMASTER);
305 bzero(&bfi, sizeof(bfi));
306 bfi.size = 8192 * 1024;
307 /* hammer2_bulkfree_pass(thr->hmp, &bfi); */
311 hammer2_thr_signal(thr, HAMMER2_THREAD_STOPPED);
312 /* structure can go invalid at this point */
316 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
317 hammer2_ioc_bulkfree_t *bfi)
319 hammer2_bulkfree_info_t cbinfo;
320 hammer2_chain_save_t *save;
326 * We have to clear the live dedup cache as it might have entries
327 * that are freeable as of now. Any new entries in the dedup cache
328 * made after this point, even if they become freeable, will have
329 * previously been fully allocated and will be protected by the
332 hammer2_dedup_clear(hmp);
335 * Setup for free pass
337 bzero(&cbinfo, sizeof(cbinfo));
338 size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
339 ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
341 cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size, VM_SUBSYS_HAMMER);
342 cbinfo.saved_mirror_tid = hmp->voldata.mirror_tid;
344 cbinfo.dedup = kmalloc(sizeof(*cbinfo.dedup) * HAMMER2_DEDUP_HEUR_SIZE,
345 M_HAMMER2, M_WAITOK | M_ZERO);
348 * Normalize start point to a 2GB boundary. We operate on a
349 * 64KB leaf bitmap boundary which represents 2GB of storage.
351 cbinfo.sbase = bfi->sbase;
352 if (cbinfo.sbase > hmp->voldata.volu_size)
353 cbinfo.sbase = hmp->voldata.volu_size;
354 cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
355 TAILQ_INIT(&cbinfo.list);
358 * Loop on a full meta-data scan as many times as required to
359 * get through all available storage.
361 while (cbinfo.sbase < hmp->voldata.volu_size) {
363 * We have enough ram to represent (incr) bytes of storage.
364 * Each 64KB of ram represents 2GB of storage.
366 * We must also clean out our de-duplication heuristic for
367 * each (incr) bytes of storage, otherwise we wind up not
368 * scanning meta-data for later areas of storage because
369 * they had already been scanned in earlier areas of storage.
370 * Since the ranging is different, we have to restart
371 * the dedup heuristic too.
373 cbinfo_bmap_init(&cbinfo, size);
374 bzero(cbinfo.dedup, sizeof(*cbinfo.dedup));
375 incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
376 HAMMER2_FREEMAP_LEVEL1_SIZE;
377 if (hmp->voldata.volu_size - cbinfo.sbase < incr)
378 cbinfo.sstop = hmp->voldata.volu_size;
380 cbinfo.sstop = cbinfo.sbase + incr;
381 if (hammer2_debug & 1) {
382 kprintf("bulkfree pass %016jx/%jdGB\n",
383 (intmax_t)cbinfo.sbase,
384 (intmax_t)incr / HAMMER2_FREEMAP_LEVEL1_SIZE);
388 * Scan topology for stuff inside this range.
390 hammer2_trans_init(hmp->spmp, 0);
391 cbinfo.mtid = hammer2_trans_sub(hmp->spmp);
393 doabort |= hammer2_bulk_scan(vchain, h2_bulkfree_callback,
396 while ((save = TAILQ_FIRST(&cbinfo.list)) != NULL &&
398 TAILQ_REMOVE(&cbinfo.list, save, entry);
400 doabort |= hammer2_bulk_scan(save->chain,
401 h2_bulkfree_callback,
403 hammer2_chain_drop(save->chain);
404 kfree(save, M_HAMMER2);
407 TAILQ_REMOVE(&cbinfo.list, save, entry);
408 hammer2_chain_drop(save->chain);
409 kfree(save, M_HAMMER2);
410 save = TAILQ_FIRST(&cbinfo.list);
413 kprintf("bulkfree lastdrop %d %d doabort=%d\n",
414 vchain->refs, vchain->core.chain_count, doabort);
417 * If complete scan succeeded we can synchronize our
418 * in-memory freemap against live storage. If an abort
419 * did occur we cannot safely synchronize our partially
420 * filled-out in-memory freemap.
423 h2_bulkfree_sync(&cbinfo);
425 hammer2_voldata_lock(hmp);
426 hammer2_voldata_modify(hmp);
427 hmp->voldata.allocator_free += cbinfo.adj_free;
428 hammer2_voldata_unlock(hmp);
432 * Cleanup for next loop.
434 hammer2_trans_done(hmp->spmp);
437 cbinfo.sbase = cbinfo.sstop;
440 kmem_free_swapbacked(&cbinfo.kp);
441 kfree(cbinfo.dedup, M_HAMMER2);
444 bfi->sstop = cbinfo.sbase;
446 incr = bfi->sstop / (hmp->voldata.volu_size / 10000);
450 kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
454 kprintf(" transition->free %ld\n", cbinfo.count_10_00);
455 kprintf(" transition->staged %ld\n", cbinfo.count_11_10);
456 kprintf(" ERR(00)->allocated %ld\n", cbinfo.count_00_11);
457 kprintf(" ERR(01)->allocated %ld\n", cbinfo.count_01_11);
458 kprintf(" staged->allocated %ld\n", cbinfo.count_10_11);
459 kprintf(" ~2MB segs cleaned %ld\n", cbinfo.count_l0cleans);
460 kprintf(" linear adjusts %ld\n", cbinfo.count_linadjusts);
461 kprintf(" dedup factor %ld\n", cbinfo.count_dedup_factor);
467 cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size)
469 hammer2_bmap_data_t *bmap = cbinfo->bmap;
470 hammer2_key_t key = cbinfo->sbase;
474 lokey = (cbinfo->hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
476 hikey = cbinfo->hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
480 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
481 HAMMER2_ZONE_SEG64) {
482 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
485 if (key < lokey || key >= hikey) {
486 memset(bmap->bitmapq, -1,
487 sizeof(bmap->bitmapq));
489 bmap->linear = HAMMER2_SEGSIZE;
491 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
493 size -= sizeof(*bmap);
494 key += HAMMER2_FREEMAP_LEVEL0_SIZE;
500 h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref)
502 hammer2_bmap_data_t *bmap;
503 hammer2_off_t data_off;
510 * Check for signal and allow yield to userland during scan
512 if (hammer2_signal_check(&cbinfo->save_time))
513 return HAMMER2_BULK_ABORT;
514 if (bref->type == HAMMER2_BREF_TYPE_INODE) {
515 ++cbinfo->count_inodes_scanned;
516 if ((cbinfo->count_inodes_scanned & 65535) == 0)
517 kprintf(" inodes %6ld bytes %9ld\n",
518 cbinfo->count_inodes_scanned,
519 cbinfo->bytes_scanned);
523 * Calculate the data offset and determine if it is within
524 * the current freemap range being gathered.
527 data_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
528 if (data_off < cbinfo->sbase || data_off >= cbinfo->sstop)
530 if (data_off < cbinfo->hmp->voldata.allocator_beg)
532 if (data_off >= cbinfo->hmp->voldata.volu_size)
536 * Calculate the information needed to generate the in-memory
539 * Hammer2 does not allow allocations to cross the L1 (2GB) boundary,
540 * it's a problem if it does. (Or L0 (2MB) for that matter).
542 radix = (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
543 KKASSERT(radix != 0);
544 bytes = (size_t)1 << radix;
545 class = (bref->type << 8) | hammer2_devblkradix(radix);
547 if (data_off + bytes >= cbinfo->sstop) {
548 kprintf("hammer2_bulkfree_scan: illegal 2GB boundary "
549 "%016jx %016jx/%d\n",
550 (intmax_t)bref->data_off,
553 bytes = cbinfo->sstop - data_off; /* XXX */
557 * Convert to a storage offset relative to the beginning of the
558 * storage range we are collecting. Then lookup the level0 bmap entry.
560 data_off -= cbinfo->sbase;
561 bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
564 * Convert data_off to a bmap-relative value (~4MB storage range).
565 * Adjust linear, class, and avail.
567 * Hammer2 does not allow allocations to cross the L0 (4MB) boundary,
569 data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
570 if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
571 kprintf("hammer2_bulkfree_scan: illegal 4MB boundary "
572 "%016jx %016jx/%d\n",
573 (intmax_t)bref->data_off,
576 bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
579 if (bmap->class == 0) {
581 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
583 if (bmap->class != class) {
584 kprintf("hammer2_bulkfree_scan: illegal mixed class "
585 "%016jx %016jx/%d (%04x vs %04x)\n",
586 (intmax_t)bref->data_off,
593 * Just record the highest byte-granular offset for now. Do not
594 * match against allocations which are in multiples of whole blocks.
596 * Make sure that any in-block linear offset at least covers the
597 * data range. This can cause bmap->linear to become block-aligned.
599 if (bytes & HAMMER2_FREEMAP_BLOCK_MASK) {
600 if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
601 bmap->linear = (int32_t)data_off + (int32_t)bytes;
602 } else if (bmap->linear >= (int32_t)data_off &&
603 bmap->linear < (int32_t)data_off + (int32_t)bytes) {
604 bmap->linear = (int32_t)data_off + (int32_t)bytes;
608 * Adjust the hammer2_bitmap_t bitmap[HAMMER2_BMAP_ELEMENTS].
609 * 64-bit entries, 2 bits per entry, to code 11.
611 * NOTE: data_off mask to 524288, shift right by 14 (radix for 16384),
612 * and multiply shift amount by 2 for sets of 2 bits.
614 * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
615 * also, data_off may not be FREEMAP_BLOCK_SIZE aligned.
618 hammer2_bitmap_t bmask;
621 bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
622 HAMMER2_BMAP_INDEX_RADIX);
623 bmask = (hammer2_bitmap_t)3 <<
624 ((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
625 HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
628 * NOTE! The (avail) calculation is bitmap-granular. Multiple
629 * sub-granular records can wind up at the same bitmap
632 if ((bmap->bitmapq[bindex] & bmask) == 0) {
633 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
634 bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
636 bmap->avail -= bytes;
638 bmap->bitmapq[bindex] |= bmask;
640 data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
641 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
644 bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
650 * Synchronize the in-memory bitmap with the live freemap. This is not a
651 * direct copy. Instead the bitmaps must be compared:
653 * In-memory Live-freemap
654 * 00 11 -> 10 (do nothing if live modified)
655 * 10 -> 00 (do nothing if live modified)
656 * 11 10 -> 11 handles race against live
657 * ** -> 11 nominally warn of corruption
661 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
663 hammer2_off_t data_off;
665 hammer2_key_t key_dummy;
666 hammer2_bmap_data_t *bmap;
667 hammer2_bmap_data_t *live;
668 hammer2_chain_t *live_parent;
669 hammer2_chain_t *live_chain;
670 int cache_index = -1;
673 kprintf("hammer2_bulkfree - range ");
675 if (cbinfo->sbase < cbinfo->hmp->voldata.allocator_beg)
677 (intmax_t)cbinfo->hmp->voldata.allocator_beg);
680 (intmax_t)cbinfo->sbase);
682 if (cbinfo->sstop > cbinfo->hmp->voldata.volu_size)
684 (intmax_t)cbinfo->hmp->voldata.volu_size);
687 (intmax_t)cbinfo->sstop);
689 data_off = cbinfo->sbase;
692 live_parent = &cbinfo->hmp->fchain;
693 hammer2_chain_ref(live_parent);
694 hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
698 * Iterate each hammer2_bmap_data_t line (128 bytes) managing
701 while (data_off < cbinfo->sstop) {
703 * The freemap is not used below allocator_beg or beyond
707 if (data_off < cbinfo->hmp->voldata.allocator_beg)
709 if (data_off >= cbinfo->hmp->voldata.volu_size)
713 * Locate the freemap leaf on the live filesystem
715 key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
717 if (live_chain == NULL || live_chain->bref.key != key) {
719 hammer2_chain_unlock(live_chain);
720 hammer2_chain_drop(live_chain);
722 live_chain = hammer2_chain_lookup(
726 key + HAMMER2_FREEMAP_LEVEL1_MASK,
728 HAMMER2_LOOKUP_ALWAYS);
732 * If recent allocations were made we avoid races by
733 * not staging or freeing any blocks. We can still
734 * remark blocks as fully allocated.
737 if (hammer2_debug & 1) {
738 kprintf("live_chain %016jx\n",
741 if (live_chain->bref.mirror_tid >
742 cbinfo->saved_mirror_tid) {
743 kprintf("hammer2_bulkfree: "
753 if (live_chain == NULL) {
755 * XXX if we implement a full recovery mode we need
756 * to create/recreate missing freemap chains if our
757 * bmap has any allocated blocks.
760 bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
761 kprintf("hammer2_bulkfree: cannot locate "
762 "live leaf for allocated data "
768 if (live_chain->error) {
769 kprintf("hammer2_bulkfree: error %s looking up "
770 "live leaf for allocated data near %016jx\n",
771 hammer2_error_str(live_chain->error),
773 hammer2_chain_unlock(live_chain);
774 hammer2_chain_drop(live_chain);
779 bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
780 HAMMER2_FREEMAP_LEVEL0_RADIX;
781 live = &live_chain->data->bmdata[bmapindex];
784 * Shortcut if the bitmaps match and the live linear
785 * indicator is sane. We can't do a perfect check of
786 * live->linear because the only real requirement is that
787 * if it is not block-aligned, that it not cover the space
788 * within its current block which overlaps one of the data
789 * ranges we scan. We don't retain enough fine-grained
790 * data in our scan to be able to set it exactly.
792 * TODO - we could shortcut this by testing that both
793 * live->class and bmap->class are 0, and both avails are
794 * set to HAMMER2_FREEMAP_LEVEL0_SIZE (4MB).
796 if (bcmp(live->bitmapq, bmap->bitmapq,
797 sizeof(bmap->bitmapq)) == 0 &&
798 live->linear >= bmap->linear) {
801 if (hammer2_debug & 1) {
802 kprintf("live %016jx %04d.%04x (avail=%d)\n",
803 data_off, bmapindex, live->class, live->avail);
806 hammer2_chain_modify(live_chain, cbinfo->mtid, 0, 0);
807 live = &live_chain->data->bmdata[bmapindex];
809 h2_bulkfree_sync_adjust(cbinfo, data_off, live, bmap,
810 live_chain->bref.key +
812 HAMMER2_FREEMAP_LEVEL0_SIZE);
814 data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
818 hammer2_chain_unlock(live_chain);
819 hammer2_chain_drop(live_chain);
822 hammer2_chain_unlock(live_parent);
823 hammer2_chain_drop(live_parent);
828 * Merge the bulkfree bitmap against the existing bitmap.
832 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
833 hammer2_off_t data_off, hammer2_bmap_data_t *live,
834 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base)
838 hammer2_off_t tmp_off;
839 hammer2_bitmap_t lmask;
840 hammer2_bitmap_t mmask;
844 for (bindex = 0; bindex < HAMMER2_BMAP_ELEMENTS; ++bindex) {
845 lmask = live->bitmapq[bindex]; /* live */
846 mmask = bmap->bitmapq[bindex]; /* snapshotted bulkfree */
847 if (lmask == mmask) {
848 tmp_off += HAMMER2_BMAP_INDEX_SIZE;
853 scount < HAMMER2_BMAP_BITS_PER_ELEMENT;
855 if ((mmask & 3) == 0) {
857 * in-memory 00 live 11 -> 10
860 * Storage might be marked allocated or
861 * staged and must be remarked staged or
868 kprintf("hammer2_bulkfree: cannot "
869 "transition m=00/l=01\n");
871 case 2: /* 10 -> 00 */
872 live->bitmapq[bindex] &=
873 ~((hammer2_bitmap_t)2 << scount);
875 HAMMER2_FREEMAP_BLOCK_SIZE;
877 HAMMER2_FREEMAP_LEVEL0_SIZE) {
879 HAMMER2_FREEMAP_LEVEL0_SIZE;
882 HAMMER2_FREEMAP_BLOCK_SIZE;
883 ++cbinfo->count_10_00;
884 hammer2_io_dedup_assert(
887 HAMMER2_FREEMAP_BLOCK_RADIX,
888 HAMMER2_FREEMAP_BLOCK_SIZE);
890 case 3: /* 11 -> 10 */
891 live->bitmapq[bindex] &=
892 ~((hammer2_bitmap_t)1 << scount);
893 ++cbinfo->count_11_10;
894 hammer2_io_dedup_delete(
896 HAMMER2_BREF_TYPE_DATA,
898 HAMMER2_FREEMAP_BLOCK_RADIX,
899 HAMMER2_FREEMAP_BLOCK_SIZE);
902 } else if ((mmask & 3) == 3) {
904 * in-memory 11 live 10 -> 11
907 * Storage might be incorrectly marked free
908 * or staged and must be remarked fully
913 ++cbinfo->count_00_11;
915 HAMMER2_FREEMAP_BLOCK_SIZE;
917 HAMMER2_FREEMAP_BLOCK_SIZE;
918 if ((int32_t)live->avail < 0)
922 ++cbinfo->count_01_11;
924 case 2: /* 10 -> 11 */
925 ++cbinfo->count_10_11;
930 live->bitmapq[bindex] |=
931 ((hammer2_bitmap_t)3 << scount);
935 tmp_off += HAMMER2_FREEMAP_BLOCK_SIZE;
940 * Determine if the live bitmap is completely free and reset its
941 * fields if so. Otherwise check to see if we can reduce the linear
944 for (bindex = HAMMER2_BMAP_ELEMENTS - 1; bindex >= 0; --bindex) {
945 if (live->bitmapq[bindex] != 0)
950 * Completely empty, reset entire segment
953 kprintf("hammer2: cleanseg %016jx.%04x (%d)\n",
954 alloc_base, live->class, live->avail);
956 live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
959 ++cbinfo->count_l0cleans;
960 } else if (bindex < 7) {
962 * Partially full, bitmapq[bindex] != 0. The live->linear
963 * offset can legitimately be just about anything, but
964 * our bulkfree pass doesn't record enough information to
965 * set it exactly. Just make sure that it is set to a
966 * safe value that also works in our match code above (the
967 * bcmp and linear test).
969 * We cannot safely leave live->linear at a sub-block offset
970 * unless it is already in the same block as bmap->linear.
972 * If it is not in the same block, we cannot assume that
973 * we can set it to bmap->linear on a sub-block boundary,
974 * because the live system could have bounced it around.
975 * In that situation we satisfy our bcmp/skip requirement
976 * above by setting it to the nearest higher block boundary.
977 * This alignment effectively kills any partial allocation it
978 * might have been tracking before.
980 if (live->linear < bmap->linear &&
981 ((live->linear ^ bmap->linear) &
982 ~HAMMER2_FREEMAP_BLOCK_MASK) == 0) {
983 live->linear = bmap->linear;
984 ++cbinfo->count_linadjusts;
987 (bmap->linear + HAMMER2_FREEMAP_BLOCK_MASK) &
988 ~HAMMER2_FREEMAP_BLOCK_MASK;
989 ++cbinfo->count_linadjusts;
993 * Completely full, effectively disable the linear iterator
995 live->linear = HAMMER2_SEGSIZE;
1000 kprintf("%016jx %04d.%04x (avail=%7d) "
1001 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
1004 HAMMER2_FREEMAP_LEVEL1_MASK) >>
1005 HAMMER2_FREEMAP_LEVEL0_RADIX),
1008 bmap->bitmap[0], bmap->bitmap[1],
1009 bmap->bitmap[2], bmap->bitmap[3],
1010 bmap->bitmap[4], bmap->bitmap[5],
1011 bmap->bitmap[6], bmap->bitmap[7]);
1017 * BULKFREE DEDUP HEURISTIC
1019 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1020 * All fields must be loaded into locals and validated.
1024 h2_bulkfree_test(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref,
1027 hammer2_dedup_t *dedup;
1032 n = hammer2_icrc32(&bref->data_off, sizeof(bref->data_off));
1033 dedup = cbinfo->dedup + (n & (HAMMER2_DEDUP_HEUR_MASK & ~7));
1035 for (i = best = 0; i < 8; ++i) {
1036 if (dedup[i].data_off == bref->data_off) {
1037 if (dedup[i].ticks < pri)
1038 dedup[i].ticks = pri;
1040 cbinfo->count_dedup_factor += dedup[i].ticks;
1043 if (dedup[i].ticks < dedup[best].ticks)
1046 dedup[best].data_off = bref->data_off;
1047 dedup[best].ticks = pri;