hammer2 - Do not use transaction for bulkfree pass
[dragonfly.git] / sys / vfs / hammer2 / hammer2_bulkfree.c
blob535742804809316fd1a25b9af4370f9350dc9855
1 /*
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
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/fcntl.h>
38 #include <sys/buf.h>
39 #include <sys/proc.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>
47 #include "hammer2.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;
64 int pri;
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 {
71 hammer2_dev_t *hmp;
72 kmem_anon_desc_t kp;
73 hammer2_off_t sbase; /* sub-loop iteration */
74 hammer2_off_t sstop;
75 hammer2_bmap_data_t *bmap;
76 int depth;
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 */
82 long count_l0cleans;
83 long count_linadjusts;
84 long count_inodes_scanned;
85 long count_dedup_factor;
86 long bytes_scanned;
87 hammer2_off_t adj_free;
88 hammer2_tid_t mtid;
89 hammer2_tid_t saved_mirror_tid;
90 time_t save_time;
91 hammer2_chain_save_list_t list;
92 hammer2_dedup_t *dedup;
93 int pri;
94 } hammer2_bulkfree_info_t;
96 static int h2_bulkfree_test(hammer2_bulkfree_info_t *info,
97 hammer2_blockref_t *bref, int pri);
100 * General bulk scan function with callback. Called with a referenced
101 * but UNLOCKED parent. The parent is returned in the same state.
103 static
105 hammer2_bulk_scan(hammer2_chain_t *parent,
106 int (*func)(hammer2_bulkfree_info_t *info,
107 hammer2_blockref_t *bref),
108 hammer2_bulkfree_info_t *info)
110 hammer2_blockref_t bref;
111 hammer2_chain_t *chain;
112 int first = 1;
113 int rup_error;
114 int error;
116 ++info->pri;
118 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
119 HAMMER2_RESOLVE_SHARED);
120 chain = NULL;
121 rup_error = 0;
122 error = 0;
125 * Generally loop on the contents if we have not been flagged
126 * for abort.
128 * Remember that these chains are completely isolated from
129 * the frontend, so we can release locks temporarily without
130 * imploding.
132 for (;;) {
133 error |= hammer2_chain_scan(parent, &chain, &bref, &first,
134 HAMMER2_LOOKUP_NODATA |
135 HAMMER2_LOOKUP_SHARED);
138 * Handle EOF or other error at current level. This stops
139 * the bulkfree scan.
141 if (error)
142 break;
145 * Process bref, chain is only non-NULL if the bref
146 * might be recursable (its possible that we sometimes get
147 * a non-NULL chain where the bref cannot be recursed).
149 ++info->pri;
150 if (h2_bulkfree_test(info, &bref, 1))
151 continue;
153 error |= func(info, &bref);
154 if (error)
155 break;
158 * A non-null chain is always returned if it is
159 * recursive, otherwise a non-null chain might be
160 * returned but usually is not when not recursive.
162 if (chain == NULL)
163 continue;
166 * Else check type and setup depth-first scan.
168 * Account for bytes actually read.
170 info->bytes_scanned += chain->bytes;
172 switch(chain->bref.type) {
173 case HAMMER2_BREF_TYPE_INODE:
174 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
175 case HAMMER2_BREF_TYPE_INDIRECT:
176 case HAMMER2_BREF_TYPE_VOLUME:
177 case HAMMER2_BREF_TYPE_FREEMAP:
178 ++info->depth;
179 if (info->depth > 16) {
180 hammer2_chain_save_t *save;
181 save = kmalloc(sizeof(*save), M_HAMMER2,
182 M_WAITOK | M_ZERO);
183 save->chain = chain;
184 hammer2_chain_ref(chain);
185 TAILQ_INSERT_TAIL(&info->list, save, entry);
187 /* guess */
188 info->pri += 10;
189 } else {
190 int savepri = info->pri;
192 hammer2_chain_unlock(chain);
193 hammer2_chain_unlock(parent);
194 info->pri = 0;
195 rup_error |=
196 hammer2_bulk_scan(chain, func, info);
197 info->pri += savepri;
198 hammer2_chain_lock(parent,
199 HAMMER2_RESOLVE_ALWAYS |
200 HAMMER2_RESOLVE_SHARED);
201 hammer2_chain_lock(chain,
202 HAMMER2_RESOLVE_ALWAYS |
203 HAMMER2_RESOLVE_SHARED);
205 --info->depth;
206 break;
207 default:
208 /* does not recurse */
209 break;
211 if (rup_error & HAMMER2_ERROR_ABORTED)
212 break;
214 if (chain) {
215 hammer2_chain_unlock(chain);
216 hammer2_chain_drop(chain);
220 * Save with higher pri now that we know what it is.
222 h2_bulkfree_test(info, &parent->bref, info->pri + 1);
224 hammer2_chain_unlock(parent);
226 return ((error | rup_error) & ~HAMMER2_ERROR_EOF);
230 * Bulkfree algorithm
232 * Repeat {
233 * Chain flush (partial synchronization) XXX removed
234 * Scan the whole topology - build in-memory freemap (mark 11)
235 * Reconcile the in-memory freemap against the on-disk freemap.
236 * ondisk xx -> ondisk 11 (if allocated)
237 * ondisk 11 -> ondisk 10 (if free in-memory)
238 * ondisk 10 -> ondisk 00 (if free in-memory) - on next pass
241 * The topology scan may have to be performed multiple times to window
242 * freemaps which are too large to fit in kernel memory.
244 * Races are handled using a double-transition (11->10, 10->00). The bulkfree
245 * scan snapshots the volume root's blockset and thus can run concurrent with
246 * normal operations, as long as a full flush is made between each pass to
247 * synchronize any modified chains (otherwise their blocks might be improperly
248 * freed).
250 * Temporary memory in multiples of 64KB is required to reconstruct the leaf
251 * hammer2_bmap_data blocks so they can later be compared against the live
252 * freemap. Each 64KB block represents 128 x 16KB x 1024 = ~2 GB of storage.
253 * A 32MB save area thus represents around ~1 TB. The temporary memory
254 * allocated can be specified. If it is not sufficient multiple topology
255 * passes will be made.
259 * Bulkfree callback info
261 static void hammer2_bulkfree_thread(void *arg __unused);
262 static void cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size);
263 static int h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo,
264 hammer2_blockref_t *bref);
265 static int h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
266 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
267 hammer2_off_t data_off, hammer2_bmap_data_t *live,
268 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base);
270 void
271 hammer2_bulkfree_init(hammer2_dev_t *hmp)
273 hammer2_thr_create(&hmp->bfthr, NULL, hmp,
274 hmp->devrepname, -1, -1,
275 hammer2_bulkfree_thread);
278 void
279 hammer2_bulkfree_uninit(hammer2_dev_t *hmp)
281 hammer2_thr_delete(&hmp->bfthr);
284 static void
285 hammer2_bulkfree_thread(void *arg)
287 hammer2_thread_t *thr = arg;
288 hammer2_ioc_bulkfree_t bfi;
289 uint32_t flags;
291 for (;;) {
292 hammer2_thr_wait_any(thr,
293 HAMMER2_THREAD_STOP |
294 HAMMER2_THREAD_FREEZE |
295 HAMMER2_THREAD_UNFREEZE |
296 HAMMER2_THREAD_REMASTER,
297 hz * 60);
299 flags = thr->flags;
300 cpu_ccfence();
301 if (flags & HAMMER2_THREAD_STOP)
302 break;
303 if (flags & HAMMER2_THREAD_FREEZE) {
304 hammer2_thr_signal2(thr, HAMMER2_THREAD_FROZEN,
305 HAMMER2_THREAD_FREEZE);
306 continue;
308 if (flags & HAMMER2_THREAD_UNFREEZE) {
309 hammer2_thr_signal2(thr, 0,
310 HAMMER2_THREAD_FROZEN |
311 HAMMER2_THREAD_UNFREEZE);
312 continue;
314 if (flags & HAMMER2_THREAD_FROZEN)
315 continue;
316 if (flags & HAMMER2_THREAD_REMASTER) {
317 hammer2_thr_signal2(thr, 0, HAMMER2_THREAD_REMASTER);
318 bzero(&bfi, sizeof(bfi));
319 bfi.size = 8192 * 1024;
320 /* hammer2_bulkfree_pass(thr->hmp, &bfi); */
323 thr->td = NULL;
324 hammer2_thr_signal(thr, HAMMER2_THREAD_STOPPED);
325 /* structure can go invalid at this point */
329 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
330 hammer2_ioc_bulkfree_t *bfi)
332 hammer2_bulkfree_info_t cbinfo;
333 hammer2_chain_save_t *save;
334 hammer2_off_t incr;
335 size_t size;
336 int error;
339 * We have to clear the live dedup cache as it might have entries
340 * that are freeable as of now. Any new entries in the dedup cache
341 * made after this point, even if they become freeable, will have
342 * previously been fully allocated and will be protected by the
343 * 2-stage bulkfree.
345 hammer2_dedup_clear(hmp);
348 * Setup for free pass
350 bzero(&cbinfo, sizeof(cbinfo));
351 size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
352 ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
353 cbinfo.hmp = hmp;
354 cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size, VM_SUBSYS_HAMMER);
355 cbinfo.saved_mirror_tid = hmp->voldata.mirror_tid;
357 cbinfo.dedup = kmalloc(sizeof(*cbinfo.dedup) * HAMMER2_DEDUP_HEUR_SIZE,
358 M_HAMMER2, M_WAITOK | M_ZERO);
361 * Normalize start point to a 2GB boundary. We operate on a
362 * 64KB leaf bitmap boundary which represents 2GB of storage.
364 cbinfo.sbase = bfi->sbase;
365 if (cbinfo.sbase > hmp->voldata.volu_size)
366 cbinfo.sbase = hmp->voldata.volu_size;
367 cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
368 TAILQ_INIT(&cbinfo.list);
371 * Loop on a full meta-data scan as many times as required to
372 * get through all available storage.
374 error = 0;
375 while (cbinfo.sbase < hmp->voldata.volu_size) {
377 * We have enough ram to represent (incr) bytes of storage.
378 * Each 64KB of ram represents 2GB of storage.
380 * We must also clean out our de-duplication heuristic for
381 * each (incr) bytes of storage, otherwise we wind up not
382 * scanning meta-data for later areas of storage because
383 * they had already been scanned in earlier areas of storage.
384 * Since the ranging is different, we have to restart
385 * the dedup heuristic too.
387 cbinfo_bmap_init(&cbinfo, size);
388 bzero(cbinfo.dedup, sizeof(*cbinfo.dedup) *
389 HAMMER2_DEDUP_HEUR_SIZE);
390 incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
391 HAMMER2_FREEMAP_LEVEL1_SIZE;
392 if (hmp->voldata.volu_size - cbinfo.sbase < incr)
393 cbinfo.sstop = hmp->voldata.volu_size;
394 else
395 cbinfo.sstop = cbinfo.sbase + incr;
396 if (hammer2_debug & 1) {
397 kprintf("bulkfree pass %016jx/%jdGB\n",
398 (intmax_t)cbinfo.sbase,
399 (intmax_t)incr / HAMMER2_FREEMAP_LEVEL1_SIZE);
403 * Scan topology for stuff inside this range.
405 * NOTE - By not using a transaction the operation can
406 * run concurrent with the frontend as well as
407 * with flushes.
409 * We cannot safely set a mtid without a transaction,
410 * and in fact we don't want to set one anyway. We
411 * want the bulkfree to be passive and no interfere
412 * with crash recovery.
414 #undef HAMMER2_BULKFREE_TRANS /* undef - don't use transaction */
415 #ifdef HAMMER2_BULKFREE_TRANS
416 hammer2_trans_init(hmp->spmp, 0);
417 cbinfo.mtid = hammer2_trans_sub(hmp->spmp);
418 #else
419 cbinfo.mtid = 0;
420 #endif
421 cbinfo.pri = 0;
422 error |= hammer2_bulk_scan(vchain, h2_bulkfree_callback,
423 &cbinfo);
425 while ((save = TAILQ_FIRST(&cbinfo.list)) != NULL &&
426 error == 0) {
427 TAILQ_REMOVE(&cbinfo.list, save, entry);
428 cbinfo.pri = 0;
429 error |= hammer2_bulk_scan(save->chain,
430 h2_bulkfree_callback,
431 &cbinfo);
432 hammer2_chain_drop(save->chain);
433 kfree(save, M_HAMMER2);
435 while (save) {
436 TAILQ_REMOVE(&cbinfo.list, save, entry);
437 hammer2_chain_drop(save->chain);
438 kfree(save, M_HAMMER2);
439 save = TAILQ_FIRST(&cbinfo.list);
442 kprintf("bulkfree lastdrop %d %d error=0x%04x\n",
443 vchain->refs, vchain->core.chain_count, error);
446 * If complete scan succeeded we can synchronize our
447 * in-memory freemap against live storage. If an abort
448 * did occur we cannot safely synchronize our partially
449 * filled-out in-memory freemap.
451 if (error == 0) {
452 error = h2_bulkfree_sync(&cbinfo);
454 hammer2_voldata_lock(hmp);
455 hammer2_voldata_modify(hmp);
456 hmp->voldata.allocator_free += cbinfo.adj_free;
457 hammer2_voldata_unlock(hmp);
461 * Cleanup for next loop.
463 #ifdef HAMMER2_BULKFREE_TRANS
464 hammer2_trans_done(hmp->spmp);
465 #endif
466 if (error)
467 break;
468 cbinfo.sbase = cbinfo.sstop;
469 cbinfo.adj_free = 0;
471 kmem_free_swapbacked(&cbinfo.kp);
472 kfree(cbinfo.dedup, M_HAMMER2);
473 cbinfo.dedup = NULL;
475 bfi->sstop = cbinfo.sbase;
477 incr = bfi->sstop / (hmp->voldata.volu_size / 10000);
478 if (incr > 10000)
479 incr = 10000;
481 kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
482 (int)incr / 100,
483 (int)incr % 100);
485 if (error) {
486 kprintf(" bulkfree was aborted\n");
487 } else {
488 kprintf(" transition->free %ld\n", cbinfo.count_10_00);
489 kprintf(" transition->staged %ld\n", cbinfo.count_11_10);
490 kprintf(" ERR(00)->allocated %ld\n", cbinfo.count_00_11);
491 kprintf(" ERR(01)->allocated %ld\n", cbinfo.count_01_11);
492 kprintf(" staged->allocated %ld\n", cbinfo.count_10_11);
493 kprintf(" ~2MB segs cleaned %ld\n", cbinfo.count_l0cleans);
494 kprintf(" linear adjusts %ld\n",
495 cbinfo.count_linadjusts);
496 kprintf(" dedup factor %ld\n",
497 cbinfo.count_dedup_factor);
500 return error;
503 static void
504 cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size)
506 hammer2_bmap_data_t *bmap = cbinfo->bmap;
507 hammer2_key_t key = cbinfo->sbase;
508 hammer2_key_t lokey;
509 hammer2_key_t hikey;
511 lokey = (cbinfo->hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
512 ~HAMMER2_SEGMASK64;
513 hikey = cbinfo->hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
515 bzero(bmap, size);
516 while (size) {
517 bzero(bmap, sizeof(*bmap));
518 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX))
519 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX);
520 if (lokey < H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64)
521 lokey = H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64;
522 if (key < lokey || key >= hikey) {
523 memset(bmap->bitmapq, -1,
524 sizeof(bmap->bitmapq));
525 bmap->avail = 0;
526 bmap->linear = HAMMER2_SEGSIZE;
527 } else {
528 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
530 size -= sizeof(*bmap);
531 key += HAMMER2_FREEMAP_LEVEL0_SIZE;
532 ++bmap;
536 static int
537 h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref)
539 hammer2_bmap_data_t *bmap;
540 hammer2_off_t data_off;
541 uint16_t class;
542 size_t bytes;
543 int radix;
546 * Check for signal and allow yield to userland during scan
548 if (hammer2_signal_check(&cbinfo->save_time))
549 return HAMMER2_ERROR_ABORTED;
551 if (bref->type == HAMMER2_BREF_TYPE_INODE) {
552 ++cbinfo->count_inodes_scanned;
553 if ((cbinfo->count_inodes_scanned & 65535) == 0)
554 kprintf(" inodes %6ld bytes %9ld\n",
555 cbinfo->count_inodes_scanned,
556 cbinfo->bytes_scanned);
560 * Calculate the data offset and determine if it is within
561 * the current freemap range being gathered.
563 data_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
564 if (data_off < cbinfo->sbase || data_off >= cbinfo->sstop)
565 return 0;
566 if (data_off < cbinfo->hmp->voldata.allocator_beg)
567 return 0;
568 if (data_off >= cbinfo->hmp->voldata.volu_size)
569 return 0;
572 * Calculate the information needed to generate the in-memory
573 * freemap record.
575 * Hammer2 does not allow allocations to cross the L1 (2GB) boundary,
576 * it's a problem if it does. (Or L0 (2MB) for that matter).
578 radix = (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
579 KKASSERT(radix != 0);
580 bytes = (size_t)1 << radix;
581 class = (bref->type << 8) | hammer2_devblkradix(radix);
583 if (data_off + bytes > cbinfo->sstop) {
584 kprintf("hammer2_bulkfree_scan: illegal 2GB boundary "
585 "%016jx %016jx/%d\n",
586 (intmax_t)bref->data_off,
587 (intmax_t)bref->key,
588 bref->keybits);
589 bytes = cbinfo->sstop - data_off; /* XXX */
593 * Convert to a storage offset relative to the beginning of the
594 * storage range we are collecting. Then lookup the level0 bmap entry.
596 data_off -= cbinfo->sbase;
597 bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
600 * Convert data_off to a bmap-relative value (~4MB storage range).
601 * Adjust linear, class, and avail.
603 * Hammer2 does not allow allocations to cross the L0 (4MB) boundary,
605 data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
606 if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
607 kprintf("hammer2_bulkfree_scan: illegal 4MB boundary "
608 "%016jx %016jx/%d\n",
609 (intmax_t)bref->data_off,
610 (intmax_t)bref->key,
611 bref->keybits);
612 bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
615 if (bmap->class == 0) {
616 bmap->class = class;
617 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
621 * NOTE: bmap->class does not have to match class. Classification
622 * is relaxed when free space is low, so some mixing can occur.
624 #if 0
626 * XXX removed
628 if (bmap->class != class) {
629 kprintf("hammer2_bulkfree_scan: illegal mixed class "
630 "%016jx %016jx/%d (%04x vs %04x)\n",
631 (intmax_t)bref->data_off,
632 (intmax_t)bref->key,
633 bref->keybits,
634 class, bmap->class);
636 #endif
639 * Just record the highest byte-granular offset for now. Do not
640 * match against allocations which are in multiples of whole blocks.
642 * Make sure that any in-block linear offset at least covers the
643 * data range. This can cause bmap->linear to become block-aligned.
645 if (bytes & HAMMER2_FREEMAP_BLOCK_MASK) {
646 if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
647 bmap->linear = (int32_t)data_off + (int32_t)bytes;
648 } else if (bmap->linear >= (int32_t)data_off &&
649 bmap->linear < (int32_t)data_off + (int32_t)bytes) {
650 bmap->linear = (int32_t)data_off + (int32_t)bytes;
654 * Adjust the hammer2_bitmap_t bitmap[HAMMER2_BMAP_ELEMENTS].
655 * 64-bit entries, 2 bits per entry, to code 11.
657 * NOTE: data_off mask to 524288, shift right by 14 (radix for 16384),
658 * and multiply shift amount by 2 for sets of 2 bits.
660 * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
661 * also, data_off may not be FREEMAP_BLOCK_SIZE aligned.
663 while (bytes > 0) {
664 hammer2_bitmap_t bmask;
665 int bindex;
667 bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
668 HAMMER2_BMAP_INDEX_RADIX);
669 bmask = (hammer2_bitmap_t)3 <<
670 ((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
671 HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
674 * NOTE! The (avail) calculation is bitmap-granular. Multiple
675 * sub-granular records can wind up at the same bitmap
676 * position.
678 if ((bmap->bitmapq[bindex] & bmask) == 0) {
679 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
680 bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
681 } else {
682 bmap->avail -= bytes;
684 bmap->bitmapq[bindex] |= bmask;
686 data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
687 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
688 bytes = 0;
689 else
690 bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
692 return 0;
696 * Synchronize the in-memory bitmap with the live freemap. This is not a
697 * direct copy. Instead the bitmaps must be compared:
699 * In-memory Live-freemap
700 * 00 11 -> 10 (do nothing if live modified)
701 * 10 -> 00 (do nothing if live modified)
702 * 11 10 -> 11 handles race against live
703 * ** -> 11 nominally warn of corruption
705 * We must also fixup the hints in HAMMER2_BREF_TYPE_FREEMAP_LEAF.
707 static int
708 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
710 hammer2_off_t data_off;
711 hammer2_key_t key;
712 hammer2_key_t key_dummy;
713 hammer2_bmap_data_t *bmap;
714 hammer2_bmap_data_t *live;
715 hammer2_chain_t *live_parent;
716 hammer2_chain_t *live_chain;
717 int bmapindex;
718 int error;
720 kprintf("hammer2_bulkfree - range ");
722 if (cbinfo->sbase < cbinfo->hmp->voldata.allocator_beg)
723 kprintf("%016jx-",
724 (intmax_t)cbinfo->hmp->voldata.allocator_beg);
725 else
726 kprintf("%016jx-",
727 (intmax_t)cbinfo->sbase);
729 if (cbinfo->sstop > cbinfo->hmp->voldata.volu_size)
730 kprintf("%016jx\n",
731 (intmax_t)cbinfo->hmp->voldata.volu_size);
732 else
733 kprintf("%016jx\n",
734 (intmax_t)cbinfo->sstop);
736 data_off = cbinfo->sbase;
737 bmap = cbinfo->bmap;
739 live_parent = &cbinfo->hmp->fchain;
740 hammer2_chain_ref(live_parent);
741 hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
742 live_chain = NULL;
743 error = 0;
746 * Iterate each hammer2_bmap_data_t line (128 bytes) managing
747 * 4MB of storage.
749 while (data_off < cbinfo->sstop) {
751 * The freemap is not used below allocator_beg or beyond
752 * volu_size.
755 if (data_off < cbinfo->hmp->voldata.allocator_beg)
756 goto next;
757 if (data_off >= cbinfo->hmp->voldata.volu_size)
758 goto next;
761 * Locate the freemap leaf on the live filesystem
763 key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
765 if (live_chain == NULL || live_chain->bref.key != key) {
766 if (live_chain) {
767 hammer2_chain_unlock(live_chain);
768 hammer2_chain_drop(live_chain);
770 live_chain = hammer2_chain_lookup(
771 &live_parent,
772 &key_dummy,
773 key,
774 key + HAMMER2_FREEMAP_LEVEL1_MASK,
775 &error,
776 HAMMER2_LOOKUP_ALWAYS);
777 if (error) {
778 kprintf("hammer2_bulkfree: freemap lookup "
779 "error near %016jx, error %s\n",
780 (intmax_t)data_off,
781 hammer2_error_str(live_chain->error));
782 break;
785 if (live_chain == NULL) {
787 * XXX if we implement a full recovery mode we need
788 * to create/recreate missing freemap chains if our
789 * bmap has any allocated blocks.
791 if (bmap->class &&
792 bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
793 kprintf("hammer2_bulkfree: cannot locate "
794 "live leaf for allocated data "
795 "near %016jx\n",
796 (intmax_t)data_off);
798 goto next;
800 if (live_chain->error) {
801 kprintf("hammer2_bulkfree: unable to access freemap "
802 "near %016jx, error %s\n",
803 (intmax_t)data_off,
804 hammer2_error_str(live_chain->error));
805 hammer2_chain_unlock(live_chain);
806 hammer2_chain_drop(live_chain);
807 live_chain = NULL;
808 goto next;
811 bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
812 HAMMER2_FREEMAP_LEVEL0_RADIX;
813 live = &live_chain->data->bmdata[bmapindex];
816 * Shortcut if the bitmaps match and the live linear
817 * indicator is sane. We can't do a perfect check of
818 * live->linear because the only real requirement is that
819 * if it is not block-aligned, that it not cover the space
820 * within its current block which overlaps one of the data
821 * ranges we scan. We don't retain enough fine-grained
822 * data in our scan to be able to set it exactly.
824 * TODO - we could shortcut this by testing that both
825 * live->class and bmap->class are 0, and both avails are
826 * set to HAMMER2_FREEMAP_LEVEL0_SIZE (4MB).
828 if (bcmp(live->bitmapq, bmap->bitmapq,
829 sizeof(bmap->bitmapq)) == 0 &&
830 live->linear >= bmap->linear) {
831 goto next;
833 if (hammer2_debug & 1) {
834 kprintf("live %016jx %04d.%04x (avail=%d)\n",
835 data_off, bmapindex, live->class, live->avail);
838 hammer2_chain_modify(live_chain, cbinfo->mtid, 0, 0);
839 live_chain->bref.check.freemap.bigmask = -1;
840 cbinfo->hmp->freemap_relaxed = 0; /* reset heuristic */
841 live = &live_chain->data->bmdata[bmapindex];
843 h2_bulkfree_sync_adjust(cbinfo, data_off, live, bmap,
844 live_chain->bref.key +
845 bmapindex *
846 HAMMER2_FREEMAP_LEVEL0_SIZE);
847 next:
848 data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
849 ++bmap;
851 if (live_chain) {
852 hammer2_chain_unlock(live_chain);
853 hammer2_chain_drop(live_chain);
855 if (live_parent) {
856 hammer2_chain_unlock(live_parent);
857 hammer2_chain_drop(live_parent);
859 return error;
863 * Merge the bulkfree bitmap against the existing bitmap.
865 static
866 void
867 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
868 hammer2_off_t data_off, hammer2_bmap_data_t *live,
869 hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base)
871 int bindex;
872 int scount;
873 hammer2_off_t tmp_off;
874 hammer2_bitmap_t lmask;
875 hammer2_bitmap_t mmask;
877 tmp_off = data_off;
879 for (bindex = 0; bindex < HAMMER2_BMAP_ELEMENTS; ++bindex) {
880 lmask = live->bitmapq[bindex]; /* live */
881 mmask = bmap->bitmapq[bindex]; /* snapshotted bulkfree */
882 if (lmask == mmask) {
883 tmp_off += HAMMER2_BMAP_INDEX_SIZE;
884 continue;
887 for (scount = 0;
888 scount < HAMMER2_BMAP_BITS_PER_ELEMENT;
889 scount += 2) {
890 if ((mmask & 3) == 0) {
892 * in-memory 00 live 11 -> 10
893 * live 10 -> 00
895 * Storage might be marked allocated or
896 * staged and must be remarked staged or
897 * free.
899 switch (lmask & 3) {
900 case 0: /* 00 */
901 break;
902 case 1: /* 01 */
903 kprintf("hammer2_bulkfree: cannot "
904 "transition m=00/l=01\n");
905 break;
906 case 2: /* 10 -> 00 */
907 live->bitmapq[bindex] &=
908 ~((hammer2_bitmap_t)2 << scount);
909 live->avail +=
910 HAMMER2_FREEMAP_BLOCK_SIZE;
911 if (live->avail >
912 HAMMER2_FREEMAP_LEVEL0_SIZE) {
913 live->avail =
914 HAMMER2_FREEMAP_LEVEL0_SIZE;
916 cbinfo->adj_free +=
917 HAMMER2_FREEMAP_BLOCK_SIZE;
918 ++cbinfo->count_10_00;
919 hammer2_io_dedup_assert(
920 cbinfo->hmp,
921 tmp_off |
922 HAMMER2_FREEMAP_BLOCK_RADIX,
923 HAMMER2_FREEMAP_BLOCK_SIZE);
924 break;
925 case 3: /* 11 -> 10 */
926 live->bitmapq[bindex] &=
927 ~((hammer2_bitmap_t)1 << scount);
928 ++cbinfo->count_11_10;
929 hammer2_io_dedup_delete(
930 cbinfo->hmp,
931 HAMMER2_BREF_TYPE_DATA,
932 tmp_off |
933 HAMMER2_FREEMAP_BLOCK_RADIX,
934 HAMMER2_FREEMAP_BLOCK_SIZE);
935 break;
937 } else if ((mmask & 3) == 3) {
939 * in-memory 11 live 10 -> 11
940 * live ** -> 11
942 * Storage might be incorrectly marked free
943 * or staged and must be remarked fully
944 * allocated.
946 switch (lmask & 3) {
947 case 0: /* 00 */
948 ++cbinfo->count_00_11;
949 cbinfo->adj_free -=
950 HAMMER2_FREEMAP_BLOCK_SIZE;
951 live->avail -=
952 HAMMER2_FREEMAP_BLOCK_SIZE;
953 if ((int32_t)live->avail < 0)
954 live->avail = 0;
955 break;
956 case 1: /* 01 */
957 ++cbinfo->count_01_11;
958 break;
959 case 2: /* 10 -> 11 */
960 ++cbinfo->count_10_11;
961 break;
962 case 3: /* 11 */
963 break;
965 live->bitmapq[bindex] |=
966 ((hammer2_bitmap_t)3 << scount);
968 mmask >>= 2;
969 lmask >>= 2;
970 tmp_off += HAMMER2_FREEMAP_BLOCK_SIZE;
975 * Determine if the live bitmap is completely free and reset its
976 * fields if so. Otherwise check to see if we can reduce the linear
977 * offset.
979 for (bindex = HAMMER2_BMAP_ELEMENTS - 1; bindex >= 0; --bindex) {
980 if (live->bitmapq[bindex] != 0)
981 break;
983 if (bindex < 0) {
985 * Completely empty, reset entire segment
987 #if 0
988 kprintf("hammer2: cleanseg %016jx.%04x (%d)\n",
989 alloc_base, live->class, live->avail);
990 #endif
991 live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
992 live->class = 0;
993 live->linear = 0;
994 ++cbinfo->count_l0cleans;
995 } else if (bindex < 7) {
997 * Partially full, bitmapq[bindex] != 0. The live->linear
998 * offset can legitimately be just about anything, but
999 * our bulkfree pass doesn't record enough information to
1000 * set it exactly. Just make sure that it is set to a
1001 * safe value that also works in our match code above (the
1002 * bcmp and linear test).
1004 * We cannot safely leave live->linear at a sub-block offset
1005 * unless it is already in the same block as bmap->linear.
1007 * If it is not in the same block, we cannot assume that
1008 * we can set it to bmap->linear on a sub-block boundary,
1009 * because the live system could have bounced it around.
1010 * In that situation we satisfy our bcmp/skip requirement
1011 * above by setting it to the nearest higher block boundary.
1012 * This alignment effectively kills any partial allocation it
1013 * might have been tracking before.
1015 if (live->linear < bmap->linear &&
1016 ((live->linear ^ bmap->linear) &
1017 ~HAMMER2_FREEMAP_BLOCK_MASK) == 0) {
1018 live->linear = bmap->linear;
1019 ++cbinfo->count_linadjusts;
1020 } else {
1021 live->linear =
1022 (bmap->linear + HAMMER2_FREEMAP_BLOCK_MASK) &
1023 ~HAMMER2_FREEMAP_BLOCK_MASK;
1024 ++cbinfo->count_linadjusts;
1026 } else {
1028 * Completely full, effectively disable the linear iterator
1030 live->linear = HAMMER2_SEGSIZE;
1033 #if 0
1034 if (bmap->class) {
1035 kprintf("%016jx %04d.%04x (avail=%7d) "
1036 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
1037 (intmax_t)data_off,
1038 (int)((data_off &
1039 HAMMER2_FREEMAP_LEVEL1_MASK) >>
1040 HAMMER2_FREEMAP_LEVEL0_RADIX),
1041 bmap->class,
1042 bmap->avail,
1043 bmap->bitmap[0], bmap->bitmap[1],
1044 bmap->bitmap[2], bmap->bitmap[3],
1045 bmap->bitmap[4], bmap->bitmap[5],
1046 bmap->bitmap[6], bmap->bitmap[7]);
1048 #endif
1052 * BULKFREE DEDUP HEURISTIC
1054 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1055 * All fields must be loaded into locals and validated.
1057 static
1059 h2_bulkfree_test(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref,
1060 int pri)
1062 hammer2_dedup_t *dedup;
1063 int best;
1064 int n;
1065 int i;
1067 n = hammer2_icrc32(&bref->data_off, sizeof(bref->data_off));
1068 dedup = cbinfo->dedup + (n & (HAMMER2_DEDUP_HEUR_MASK & ~7));
1070 for (i = best = 0; i < 8; ++i) {
1071 if (dedup[i].data_off == bref->data_off) {
1072 if (dedup[i].ticks < pri)
1073 dedup[i].ticks = pri;
1074 if (pri == 1)
1075 cbinfo->count_dedup_factor += dedup[i].ticks;
1076 return 1;
1078 if (dedup[i].ticks < dedup[best].ticks)
1079 best = i;
1081 dedup[best].data_off = bref->data_off;
1082 dedup[best].ticks = pri;
1084 return 0;