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>
50 * XXX I made a mistake and made the reserved area begin at each LEVEL1 zone,
51 * which is on a 1GB demark. This will eat a little more space but for
52 * now we retain compatibility and make FMZONEBASE every 1GB
54 #define H2FMZONEBASE(key) ((key) & ~HAMMER2_FREEMAP_LEVEL1_MASK)
55 #define H2FMBASE(key, radix) ((key) & ~(((hammer2_off_t)1 << (radix)) - 1))
56 #define H2FMSHIFT(radix) ((hammer2_off_t)1 << (radix))
59 * breadth-first search
61 typedef struct hammer2_chain_save {
62 TAILQ_ENTRY(hammer2_chain_save) entry;
63 hammer2_chain_t *chain;
65 } hammer2_chain_save_t;
67 TAILQ_HEAD(hammer2_chain_save_list, hammer2_chain_save);
68 typedef struct hammer2_chain_save_list hammer2_chain_save_list_t;
70 typedef struct hammer2_bulkfree_info {
73 hammer2_off_t sbase; /* sub-loop iteration */
75 hammer2_bmap_data_t *bmap;
77 long count_10_00; /* staged->free */
78 long count_11_10; /* allocated->staged */
79 long count_00_11; /* (should not happen) */
80 long count_01_11; /* (should not happen) */
81 long count_10_11; /* staged->allocated */
83 long count_linadjusts;
84 long count_inodes_scanned;
85 long count_dirents_scanned;
86 long count_dedup_factor;
87 long count_bytes_scanned;
88 long count_chains_scanned;
89 long count_chains_reported;
90 hammer2_off_t adj_free;
92 hammer2_tid_t saved_mirror_tid;
94 hammer2_chain_save_list_t list;
95 hammer2_dedup_t *dedup;
97 } hammer2_bulkfree_info_t;
99 static int h2_bulkfree_test(hammer2_bulkfree_info_t *info,
100 hammer2_blockref_t *bref, int pri);
103 * General bulk scan function with callback. Called with a referenced
104 * but UNLOCKED parent. The parent is returned in the same state.
108 hammer2_bulk_scan(hammer2_chain_t *parent,
109 int (*func)(hammer2_bulkfree_info_t *info,
110 hammer2_blockref_t *bref),
111 hammer2_bulkfree_info_t *info)
113 hammer2_blockref_t bref;
114 hammer2_chain_t *chain;
121 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
122 HAMMER2_RESOLVE_SHARED);
128 * Generally loop on the contents if we have not been flagged
131 * Remember that these chains are completely isolated from
132 * the frontend, so we can release locks temporarily without
136 error |= hammer2_chain_scan(parent, &chain, &bref, &first,
137 HAMMER2_LOOKUP_NODATA |
138 HAMMER2_LOOKUP_SHARED);
141 * Handle EOF or other error at current level. This stops
147 if (bref.type == HAMMER2_BREF_TYPE_DIRENT)
148 ++info->count_dirents_scanned;
151 * Ignore brefs without data (typically dirents)
153 if ((bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0)
157 * Process bref, chain is only non-NULL if the bref
158 * might be recursable (its possible that we sometimes get
159 * a non-NULL chain where the bref cannot be recursed).
162 if (h2_bulkfree_test(info, &bref, 1))
165 if (bref.type == HAMMER2_BREF_TYPE_INODE)
166 ++info->count_inodes_scanned;
168 error |= func(info, &bref);
173 * A non-null chain is always returned if it is
174 * recursive, otherwise a non-null chain might be
175 * returned but usually is not when not recursive.
181 info->count_bytes_scanned += chain->bytes;
182 ++info->count_chains_scanned;
184 if (info->count_chains_scanned >=
185 info->count_chains_reported + 50000) {
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 += 50000;
198 * Else check type and setup depth-first scan.
200 * Account for bytes actually read.
202 switch(chain->bref.type) {
203 case HAMMER2_BREF_TYPE_INODE:
204 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
205 case HAMMER2_BREF_TYPE_INDIRECT:
206 case HAMMER2_BREF_TYPE_VOLUME:
207 case HAMMER2_BREF_TYPE_FREEMAP:
209 if (info->depth > 16) {
210 hammer2_chain_save_t *save;
211 save = kmalloc(sizeof(*save), M_HAMMER2,
214 hammer2_chain_ref(chain);
215 TAILQ_INSERT_TAIL(&info->list, save, entry);
220 int savepri = info->pri;
222 hammer2_chain_unlock(chain);
223 hammer2_chain_unlock(parent);
226 hammer2_bulk_scan(chain, func, info);
227 info->pri += savepri;
228 hammer2_chain_lock(parent,
229 HAMMER2_RESOLVE_ALWAYS |
230 HAMMER2_RESOLVE_SHARED);
231 hammer2_chain_lock(chain,
232 HAMMER2_RESOLVE_ALWAYS |
233 HAMMER2_RESOLVE_SHARED);
237 case HAMMER2_BREF_TYPE_DATA:
240 /* does not recurse */
243 if (rup_error & HAMMER2_ERROR_ABORTED)
247 hammer2_chain_unlock(chain);
248 hammer2_chain_drop(chain);
252 * Save with higher pri now that we know what it is.
254 h2_bulkfree_test(info, &parent->bref, info->pri + 1);
256 hammer2_chain_unlock(parent);
258 return ((error | rup_error) & ~HAMMER2_ERROR_EOF);
265 * Chain flush (partial synchronization) XXX removed
266 * Scan the whole topology - build in-memory freemap (mark 11)
267 * Reconcile the in-memory freemap against the on-disk freemap.
268 * ondisk xx -> ondisk 11 (if allocated)
269 * ondisk 11 -> ondisk 10 (if free in-memory)
270 * ondisk 10 -> ondisk 00 (if free in-memory) - on next pass
273 * The topology scan may have to be performed multiple times to window
274 * freemaps which are too large to fit in kernel memory.
276 * Races are handled using a double-transition (11->10, 10->00). The bulkfree
277 * scan snapshots the volume root's blockset and thus can run concurrent with
278 * normal operations, as long as a full flush is made between each pass to
279 * synchronize any modified chains (otherwise their blocks might be improperly
282 * Temporary memory in multiples of 64KB is required to reconstruct the leaf
283 * hammer2_bmap_data blocks so they can later be compared against the live
284 * freemap. Each 64KB block represents 128 x 16KB x 1024 = ~2 GB of storage.
285 * A 32MB save area thus represents around ~1 TB. The temporary memory
286 * allocated can be specified. If it is not sufficient multiple topology
287 * passes will be made.
291 * Bulkfree callback info
293 static void hammer2_bulkfree_thread(void *arg __unused);
294 static void cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size);
295 static int h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo,
296 hammer2_blockref_t *bref);
297 static int h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
298 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
299 hammer2_off_t data_off, hammer2_bmap_data_t *live,
300 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base);
303 hammer2_bulkfree_init(hammer2_dev_t *hmp)
305 hammer2_thr_create(&hmp->bfthr, NULL, hmp,
306 hmp->devrepname, -1, -1,
307 hammer2_bulkfree_thread);
311 hammer2_bulkfree_uninit(hammer2_dev_t *hmp)
313 hammer2_thr_delete(&hmp->bfthr);
317 hammer2_bulkfree_thread(void *arg)
319 hammer2_thread_t *thr = arg;
320 hammer2_ioc_bulkfree_t bfi;
324 hammer2_thr_wait_any(thr,
325 HAMMER2_THREAD_STOP |
326 HAMMER2_THREAD_FREEZE |
327 HAMMER2_THREAD_UNFREEZE |
328 HAMMER2_THREAD_REMASTER,
333 if (flags & HAMMER2_THREAD_STOP)
335 if (flags & HAMMER2_THREAD_FREEZE) {
336 hammer2_thr_signal2(thr, HAMMER2_THREAD_FROZEN,
337 HAMMER2_THREAD_FREEZE);
340 if (flags & HAMMER2_THREAD_UNFREEZE) {
341 hammer2_thr_signal2(thr, 0,
342 HAMMER2_THREAD_FROZEN |
343 HAMMER2_THREAD_UNFREEZE);
346 if (flags & HAMMER2_THREAD_FROZEN)
348 if (flags & HAMMER2_THREAD_REMASTER) {
349 hammer2_thr_signal2(thr, 0, HAMMER2_THREAD_REMASTER);
350 bzero(&bfi, sizeof(bfi));
351 bfi.size = 8192 * 1024;
352 /* hammer2_bulkfree_pass(thr->hmp, &bfi); */
356 hammer2_thr_signal(thr, HAMMER2_THREAD_STOPPED);
357 /* structure can go invalid at this point */
361 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
362 hammer2_ioc_bulkfree_t *bfi)
364 hammer2_bulkfree_info_t cbinfo;
365 hammer2_chain_save_t *save;
371 * We have to clear the live dedup cache as it might have entries
372 * that are freeable as of now. Any new entries in the dedup cache
373 * made after this point, even if they become freeable, will have
374 * previously been fully allocated and will be protected by the
377 hammer2_dedup_clear(hmp);
380 * Setup for free pass
382 bzero(&cbinfo, sizeof(cbinfo));
383 size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
384 ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
385 if (size < 1024 * 1024)
387 if (size > 64 * 1024 * 1024)
388 size = 64 * 1024 * 1024;
391 cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size, VM_SUBSYS_HAMMER);
392 cbinfo.saved_mirror_tid = hmp->voldata.mirror_tid;
394 cbinfo.dedup = kmalloc(sizeof(*cbinfo.dedup) * HAMMER2_DEDUP_HEUR_SIZE,
395 M_HAMMER2, M_WAITOK | M_ZERO);
398 * Normalize start point to a 2GB boundary. We operate on a
399 * 64KB leaf bitmap boundary which represents 2GB of storage.
401 cbinfo.sbase = bfi->sbase;
402 if (cbinfo.sbase > hmp->voldata.volu_size)
403 cbinfo.sbase = hmp->voldata.volu_size;
404 cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
405 TAILQ_INIT(&cbinfo.list);
408 * Loop on a full meta-data scan as many times as required to
409 * get through all available storage.
412 while (cbinfo.sbase < hmp->voldata.volu_size) {
414 * We have enough ram to represent (incr) bytes of storage.
415 * Each 64KB of ram represents 2GB of storage.
417 * We must also clean out our de-duplication heuristic for
418 * each (incr) bytes of storage, otherwise we wind up not
419 * scanning meta-data for later areas of storage because
420 * they had already been scanned in earlier areas of storage.
421 * Since the ranging is different, we have to restart
422 * the dedup heuristic too.
424 cbinfo_bmap_init(&cbinfo, size);
425 bzero(cbinfo.dedup, sizeof(*cbinfo.dedup) *
426 HAMMER2_DEDUP_HEUR_SIZE);
427 cbinfo.count_inodes_scanned = 0;
428 cbinfo.count_dirents_scanned = 0;
429 cbinfo.count_bytes_scanned = 0;
430 cbinfo.count_chains_scanned = 0;
431 cbinfo.count_chains_reported = 0;
433 incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
434 HAMMER2_FREEMAP_LEVEL1_SIZE;
435 if (hmp->voldata.volu_size - cbinfo.sbase < incr)
436 cbinfo.sstop = hmp->voldata.volu_size;
438 cbinfo.sstop = cbinfo.sbase + incr;
439 if (hammer2_debug & 1) {
440 kprintf("bulkfree pass %016jx/%jdGB\n",
441 (intmax_t)cbinfo.sbase,
442 (intmax_t)incr / HAMMER2_FREEMAP_LEVEL1_SIZE);
446 * Scan topology for stuff inside this range.
448 * NOTE - By not using a transaction the operation can
449 * run concurrent with the frontend as well as
452 * We cannot safely set a mtid without a transaction,
453 * and in fact we don't want to set one anyway. We
454 * want the bulkfree to be passive and no interfere
455 * with crash recovery.
457 #undef HAMMER2_BULKFREE_TRANS /* undef - don't use transaction */
458 #ifdef HAMMER2_BULKFREE_TRANS
459 hammer2_trans_init(hmp->spmp, 0);
460 cbinfo.mtid = hammer2_trans_sub(hmp->spmp);
465 error |= hammer2_bulk_scan(vchain, h2_bulkfree_callback,
468 while ((save = TAILQ_FIRST(&cbinfo.list)) != NULL &&
470 TAILQ_REMOVE(&cbinfo.list, save, entry);
472 error |= hammer2_bulk_scan(save->chain,
473 h2_bulkfree_callback,
475 hammer2_chain_drop(save->chain);
476 kfree(save, M_HAMMER2);
479 TAILQ_REMOVE(&cbinfo.list, save, entry);
480 hammer2_chain_drop(save->chain);
481 kfree(save, M_HAMMER2);
482 save = TAILQ_FIRST(&cbinfo.list);
485 kprintf("bulkfree lastdrop %d %d error=0x%04x\n",
486 vchain->refs, vchain->core.chain_count, error);
489 * If complete scan succeeded we can synchronize our
490 * in-memory freemap against live storage. If an abort
491 * did occur we cannot safely synchronize our partially
492 * filled-out in-memory freemap.
495 error = h2_bulkfree_sync(&cbinfo);
497 hammer2_voldata_lock(hmp);
498 hammer2_voldata_modify(hmp);
499 hmp->voldata.allocator_free += cbinfo.adj_free;
500 hammer2_voldata_unlock(hmp);
504 * Cleanup for next loop.
506 #ifdef HAMMER2_BULKFREE_TRANS
507 hammer2_trans_done(hmp->spmp);
511 cbinfo.sbase = cbinfo.sstop;
514 kmem_free_swapbacked(&cbinfo.kp);
515 kfree(cbinfo.dedup, M_HAMMER2);
518 bfi->sstop = cbinfo.sbase;
520 incr = bfi->sstop / (hmp->voldata.volu_size / 10000);
524 kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
529 kprintf(" bulkfree was aborted\n");
531 kprintf(" transition->free %ld\n", cbinfo.count_10_00);
532 kprintf(" transition->staged %ld\n", cbinfo.count_11_10);
533 kprintf(" ERR(00)->allocated %ld\n", cbinfo.count_00_11);
534 kprintf(" ERR(01)->allocated %ld\n", cbinfo.count_01_11);
535 kprintf(" staged->allocated %ld\n", cbinfo.count_10_11);
536 kprintf(" ~2MB segs cleaned %ld\n", cbinfo.count_l0cleans);
537 kprintf(" linear adjusts %ld\n",
538 cbinfo.count_linadjusts);
539 kprintf(" dedup factor %ld\n",
540 cbinfo.count_dedup_factor);
547 cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size)
549 hammer2_bmap_data_t *bmap = cbinfo->bmap;
550 hammer2_key_t key = cbinfo->sbase;
554 lokey = (cbinfo->hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
556 hikey = cbinfo->hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
560 bzero(bmap, sizeof(*bmap));
561 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX))
562 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX);
563 if (lokey < H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64)
564 lokey = H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64;
565 if (key < lokey || key >= hikey) {
566 memset(bmap->bitmapq, -1,
567 sizeof(bmap->bitmapq));
569 bmap->linear = HAMMER2_SEGSIZE;
571 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
573 size -= sizeof(*bmap);
574 key += HAMMER2_FREEMAP_LEVEL0_SIZE;
580 h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref)
582 hammer2_bmap_data_t *bmap;
583 hammer2_off_t data_off;
589 * Check for signal and allow yield to userland during scan
591 if (hammer2_signal_check(&cbinfo->save_time))
592 return HAMMER2_ERROR_ABORTED;
595 * Calculate the data offset and determine if it is within
596 * the current freemap range being gathered.
598 data_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
599 if (data_off < cbinfo->sbase || data_off >= cbinfo->sstop)
601 if (data_off < cbinfo->hmp->voldata.allocator_beg)
603 if (data_off >= cbinfo->hmp->voldata.volu_size)
607 * Calculate the information needed to generate the in-memory
610 * Hammer2 does not allow allocations to cross the L1 (2GB) boundary,
611 * it's a problem if it does. (Or L0 (2MB) for that matter).
613 radix = (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
614 KKASSERT(radix != 0);
615 bytes = (size_t)1 << radix;
616 class = (bref->type << 8) | hammer2_devblkradix(radix);
618 if (data_off + bytes > cbinfo->sstop) {
619 kprintf("hammer2_bulkfree_scan: illegal 2GB boundary "
620 "%016jx %016jx/%d\n",
621 (intmax_t)bref->data_off,
624 bytes = cbinfo->sstop - data_off; /* XXX */
628 * Convert to a storage offset relative to the beginning of the
629 * storage range we are collecting. Then lookup the level0 bmap entry.
631 data_off -= cbinfo->sbase;
632 bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
635 * Convert data_off to a bmap-relative value (~4MB storage range).
636 * Adjust linear, class, and avail.
638 * Hammer2 does not allow allocations to cross the L0 (4MB) boundary,
640 data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
641 if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
642 kprintf("hammer2_bulkfree_scan: illegal 4MB boundary "
643 "%016jx %016jx/%d\n",
644 (intmax_t)bref->data_off,
647 bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
650 if (bmap->class == 0) {
652 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
656 * NOTE: bmap->class does not have to match class. Classification
657 * is relaxed when free space is low, so some mixing can occur.
663 if (bmap->class != class) {
664 kprintf("hammer2_bulkfree_scan: illegal mixed class "
665 "%016jx %016jx/%d (%04x vs %04x)\n",
666 (intmax_t)bref->data_off,
674 * Just record the highest byte-granular offset for now. Do not
675 * match against allocations which are in multiples of whole blocks.
677 * Make sure that any in-block linear offset at least covers the
678 * data range. This can cause bmap->linear to become block-aligned.
680 if (bytes & HAMMER2_FREEMAP_BLOCK_MASK) {
681 if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
682 bmap->linear = (int32_t)data_off + (int32_t)bytes;
683 } else if (bmap->linear >= (int32_t)data_off &&
684 bmap->linear < (int32_t)data_off + (int32_t)bytes) {
685 bmap->linear = (int32_t)data_off + (int32_t)bytes;
689 * Adjust the hammer2_bitmap_t bitmap[HAMMER2_BMAP_ELEMENTS].
690 * 64-bit entries, 2 bits per entry, to code 11.
692 * NOTE: data_off mask to 524288, shift right by 14 (radix for 16384),
693 * and multiply shift amount by 2 for sets of 2 bits.
695 * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
696 * also, data_off may not be FREEMAP_BLOCK_SIZE aligned.
699 hammer2_bitmap_t bmask;
702 bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
703 HAMMER2_BMAP_INDEX_RADIX);
704 bmask = (hammer2_bitmap_t)3 <<
705 ((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
706 HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
709 * NOTE! The (avail) calculation is bitmap-granular. Multiple
710 * sub-granular records can wind up at the same bitmap
713 if ((bmap->bitmapq[bindex] & bmask) == 0) {
714 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
715 bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
717 bmap->avail -= bytes;
719 bmap->bitmapq[bindex] |= bmask;
721 data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
722 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
725 bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
731 * Synchronize the in-memory bitmap with the live freemap. This is not a
732 * direct copy. Instead the bitmaps must be compared:
734 * In-memory Live-freemap
735 * 00 11 -> 10 (do nothing if live modified)
736 * 10 -> 00 (do nothing if live modified)
737 * 11 10 -> 11 handles race against live
738 * ** -> 11 nominally warn of corruption
740 * We must also fixup the hints in HAMMER2_BREF_TYPE_FREEMAP_LEAF.
743 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
745 hammer2_off_t data_off;
747 hammer2_key_t key_dummy;
748 hammer2_bmap_data_t *bmap;
749 hammer2_bmap_data_t *live;
750 hammer2_chain_t *live_parent;
751 hammer2_chain_t *live_chain;
755 kprintf("hammer2_bulkfree - range ");
757 if (cbinfo->sbase < cbinfo->hmp->voldata.allocator_beg)
759 (intmax_t)cbinfo->hmp->voldata.allocator_beg);
762 (intmax_t)cbinfo->sbase);
764 if (cbinfo->sstop > cbinfo->hmp->voldata.volu_size)
766 (intmax_t)cbinfo->hmp->voldata.volu_size);
769 (intmax_t)cbinfo->sstop);
771 data_off = cbinfo->sbase;
774 live_parent = &cbinfo->hmp->fchain;
775 hammer2_chain_ref(live_parent);
776 hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
781 * Iterate each hammer2_bmap_data_t line (128 bytes) managing
784 while (data_off < cbinfo->sstop) {
786 * The freemap is not used below allocator_beg or beyond
790 if (data_off < cbinfo->hmp->voldata.allocator_beg)
792 if (data_off >= cbinfo->hmp->voldata.volu_size)
796 * Locate the freemap leaf on the live filesystem
798 key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
800 if (live_chain == NULL || live_chain->bref.key != key) {
802 hammer2_chain_unlock(live_chain);
803 hammer2_chain_drop(live_chain);
805 live_chain = hammer2_chain_lookup(
809 key + HAMMER2_FREEMAP_LEVEL1_MASK,
811 HAMMER2_LOOKUP_ALWAYS);
813 kprintf("hammer2_bulkfree: freemap lookup "
814 "error near %016jx, error %s\n",
816 hammer2_error_str(live_chain->error));
820 if (live_chain == NULL) {
822 * XXX if we implement a full recovery mode we need
823 * to create/recreate missing freemap chains if our
824 * bmap has any allocated blocks.
827 bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
828 kprintf("hammer2_bulkfree: cannot locate "
829 "live leaf for allocated data "
835 if (live_chain->error) {
836 kprintf("hammer2_bulkfree: unable to access freemap "
837 "near %016jx, error %s\n",
839 hammer2_error_str(live_chain->error));
840 hammer2_chain_unlock(live_chain);
841 hammer2_chain_drop(live_chain);
846 bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
847 HAMMER2_FREEMAP_LEVEL0_RADIX;
848 live = &live_chain->data->bmdata[bmapindex];
851 * Shortcut if the bitmaps match and the live linear
852 * indicator is sane. We can't do a perfect check of
853 * live->linear because the only real requirement is that
854 * if it is not block-aligned, that it not cover the space
855 * within its current block which overlaps one of the data
856 * ranges we scan. We don't retain enough fine-grained
857 * data in our scan to be able to set it exactly.
859 * TODO - we could shortcut this by testing that both
860 * live->class and bmap->class are 0, and both avails are
861 * set to HAMMER2_FREEMAP_LEVEL0_SIZE (4MB).
863 if (bcmp(live->bitmapq, bmap->bitmapq,
864 sizeof(bmap->bitmapq)) == 0 &&
865 live->linear >= bmap->linear) {
868 if (hammer2_debug & 1) {
869 kprintf("live %016jx %04d.%04x (avail=%d)\n",
870 data_off, bmapindex, live->class, live->avail);
873 hammer2_chain_modify(live_chain, cbinfo->mtid, 0, 0);
874 live_chain->bref.check.freemap.bigmask = -1;
875 cbinfo->hmp->freemap_relaxed = 0; /* reset heuristic */
876 live = &live_chain->data->bmdata[bmapindex];
878 h2_bulkfree_sync_adjust(cbinfo, data_off, live, bmap,
879 live_chain->bref.key +
881 HAMMER2_FREEMAP_LEVEL0_SIZE);
883 data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
887 hammer2_chain_unlock(live_chain);
888 hammer2_chain_drop(live_chain);
891 hammer2_chain_unlock(live_parent);
892 hammer2_chain_drop(live_parent);
898 * Merge the bulkfree bitmap against the existing bitmap.
902 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
903 hammer2_off_t data_off, hammer2_bmap_data_t *live,
904 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base)
908 hammer2_off_t tmp_off;
909 hammer2_bitmap_t lmask;
910 hammer2_bitmap_t mmask;
914 for (bindex = 0; bindex < HAMMER2_BMAP_ELEMENTS; ++bindex) {
915 lmask = live->bitmapq[bindex]; /* live */
916 mmask = bmap->bitmapq[bindex]; /* snapshotted bulkfree */
917 if (lmask == mmask) {
918 tmp_off += HAMMER2_BMAP_INDEX_SIZE;
923 scount < HAMMER2_BMAP_BITS_PER_ELEMENT;
925 if ((mmask & 3) == 0) {
927 * in-memory 00 live 11 -> 10
930 * Storage might be marked allocated or
931 * staged and must be remarked staged or
938 kprintf("hammer2_bulkfree: cannot "
939 "transition m=00/l=01\n");
941 case 2: /* 10 -> 00 */
942 live->bitmapq[bindex] &=
943 ~((hammer2_bitmap_t)2 << scount);
945 HAMMER2_FREEMAP_BLOCK_SIZE;
947 HAMMER2_FREEMAP_LEVEL0_SIZE) {
949 HAMMER2_FREEMAP_LEVEL0_SIZE;
952 HAMMER2_FREEMAP_BLOCK_SIZE;
953 ++cbinfo->count_10_00;
954 hammer2_io_dedup_assert(
957 HAMMER2_FREEMAP_BLOCK_RADIX,
958 HAMMER2_FREEMAP_BLOCK_SIZE);
960 case 3: /* 11 -> 10 */
961 live->bitmapq[bindex] &=
962 ~((hammer2_bitmap_t)1 << scount);
963 ++cbinfo->count_11_10;
964 hammer2_io_dedup_delete(
966 HAMMER2_BREF_TYPE_DATA,
968 HAMMER2_FREEMAP_BLOCK_RADIX,
969 HAMMER2_FREEMAP_BLOCK_SIZE);
972 } else if ((mmask & 3) == 3) {
974 * in-memory 11 live 10 -> 11
977 * Storage might be incorrectly marked free
978 * or staged and must be remarked fully
983 ++cbinfo->count_00_11;
985 HAMMER2_FREEMAP_BLOCK_SIZE;
987 HAMMER2_FREEMAP_BLOCK_SIZE;
988 if ((int32_t)live->avail < 0)
992 ++cbinfo->count_01_11;
994 case 2: /* 10 -> 11 */
995 ++cbinfo->count_10_11;
1000 live->bitmapq[bindex] |=
1001 ((hammer2_bitmap_t)3 << scount);
1005 tmp_off += HAMMER2_FREEMAP_BLOCK_SIZE;
1010 * Determine if the live bitmap is completely free and reset its
1011 * fields if so. Otherwise check to see if we can reduce the linear
1014 for (bindex = HAMMER2_BMAP_ELEMENTS - 1; bindex >= 0; --bindex) {
1015 if (live->bitmapq[bindex] != 0)
1020 * Completely empty, reset entire segment
1023 kprintf("hammer2: cleanseg %016jx.%04x (%d)\n",
1024 alloc_base, live->class, live->avail);
1026 live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
1029 ++cbinfo->count_l0cleans;
1030 } else if (bindex < 7) {
1032 * Partially full, bitmapq[bindex] != 0. The live->linear
1033 * offset can legitimately be just about anything, but
1034 * our bulkfree pass doesn't record enough information to
1035 * set it exactly. Just make sure that it is set to a
1036 * safe value that also works in our match code above (the
1037 * bcmp and linear test).
1039 * We cannot safely leave live->linear at a sub-block offset
1040 * unless it is already in the same block as bmap->linear.
1042 * If it is not in the same block, we cannot assume that
1043 * we can set it to bmap->linear on a sub-block boundary,
1044 * because the live system could have bounced it around.
1045 * In that situation we satisfy our bcmp/skip requirement
1046 * above by setting it to the nearest higher block boundary.
1047 * This alignment effectively kills any partial allocation it
1048 * might have been tracking before.
1050 if (live->linear < bmap->linear &&
1051 ((live->linear ^ bmap->linear) &
1052 ~HAMMER2_FREEMAP_BLOCK_MASK) == 0) {
1053 live->linear = bmap->linear;
1054 ++cbinfo->count_linadjusts;
1057 (bmap->linear + HAMMER2_FREEMAP_BLOCK_MASK) &
1058 ~HAMMER2_FREEMAP_BLOCK_MASK;
1059 ++cbinfo->count_linadjusts;
1063 * Completely full, effectively disable the linear iterator
1065 live->linear = HAMMER2_SEGSIZE;
1070 kprintf("%016jx %04d.%04x (avail=%7d) "
1071 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
1074 HAMMER2_FREEMAP_LEVEL1_MASK) >>
1075 HAMMER2_FREEMAP_LEVEL0_RADIX),
1078 bmap->bitmap[0], bmap->bitmap[1],
1079 bmap->bitmap[2], bmap->bitmap[3],
1080 bmap->bitmap[4], bmap->bitmap[5],
1081 bmap->bitmap[6], bmap->bitmap[7]);
1087 * BULKFREE DEDUP HEURISTIC
1089 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1090 * All fields must be loaded into locals and validated.
1094 h2_bulkfree_test(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref,
1097 hammer2_dedup_t *dedup;
1102 n = hammer2_icrc32(&bref->data_off, sizeof(bref->data_off));
1103 dedup = cbinfo->dedup + (n & (HAMMER2_DEDUP_HEUR_MASK & ~7));
1105 for (i = best = 0; i < 8; ++i) {
1106 if (dedup[i].data_off == bref->data_off) {
1107 if (dedup[i].ticks < pri)
1108 dedup[i].ticks = pri;
1110 cbinfo->count_dedup_factor += dedup[i].ticks;
1113 if (dedup[i].ticks < dedup[best].ticks)
1116 dedup[best].data_off = bref->data_off;
1117 dedup[best].ticks = pri;