hammer2 - Clean DIO invalidation flags in more cases
[dragonfly.git] / sys / vfs / hammer2 / hammer2_bulkfree.c
blobe6aca816c1dd6fb402e5e8b68242fe111839c6b0
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"
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;
58 int pri;
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 {
65 hammer2_dev_t *hmp;
66 kmem_anon_desc_t kp;
67 hammer2_off_t sbase; /* sub-loop iteration */
68 hammer2_off_t sstop;
69 hammer2_bmap_data_t *bmap;
70 int depth;
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 */
76 long count_l0cleans;
77 long count_linadjusts;
78 long count_inodes_scanned;
79 long count_dedup_factor;
80 long bytes_scanned;
81 hammer2_off_t adj_free;
82 hammer2_tid_t mtid;
83 hammer2_tid_t saved_mirror_tid;
84 time_t save_time;
85 hammer2_chain_save_list_t list;
86 hammer2_dedup_t *dedup;
87 int pri;
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.
97 static
98 int
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;
107 int doabort = 0;
108 int first = 1;
110 ++info->pri;
112 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
113 HAMMER2_RESOLVE_SHARED);
114 chain = NULL;
117 * Generally loop on the contents if we have not been flagged
118 * for abort.
120 * Remember that these chains are completely isolated from
121 * the frontend, so we can release locks temporarily without
122 * imploding.
124 while ((doabort & HAMMER2_BULK_ABORT) == 0 &&
125 hammer2_chain_scan(parent, &chain, &bref, &first,
126 &cache_index,
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).
134 #if 0
135 kprintf("SCAN %016jx\n", bref.data_off);
136 int xerr = tsleep(&info->pri, PCATCH, "slp", hz / 10);
137 if (xerr == EINTR || xerr == ERESTART) {
138 doabort |= HAMMER2_BULK_ABORT;
140 #endif
141 ++info->pri;
142 if (h2_bulkfree_test(info, &bref, 1))
143 continue;
145 doabort |= func(info, &bref);
147 if (doabort & HAMMER2_BULK_ABORT)
148 break;
151 * A non-null chain is always returned if it is
152 * recursive, otherwise a non-null chain might be
153 * returned but usually is not when not recursive.
155 if (chain == NULL)
156 continue;
159 * Else check type and setup depth-first scan.
161 * Account for bytes actually read.
163 info->bytes_scanned += chain->bytes;
165 switch(chain->bref.type) {
166 case HAMMER2_BREF_TYPE_INODE:
167 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
168 case HAMMER2_BREF_TYPE_INDIRECT:
169 case HAMMER2_BREF_TYPE_VOLUME:
170 case HAMMER2_BREF_TYPE_FREEMAP:
171 ++info->depth;
172 if (info->depth > 16) {
173 hammer2_chain_save_t *save;
174 save = kmalloc(sizeof(*save), M_HAMMER2,
175 M_WAITOK | M_ZERO);
176 save->chain = chain;
177 hammer2_chain_ref(chain);
178 TAILQ_INSERT_TAIL(&info->list, save, entry);
180 /* guess */
181 info->pri += 10;
182 } else {
183 int savepri = info->pri;
185 hammer2_chain_unlock(chain);
186 info->pri = 0;
187 doabort |= hammer2_bulk_scan(chain, func, info);
188 info->pri += savepri;
189 hammer2_chain_lock(chain,
190 HAMMER2_RESOLVE_ALWAYS |
191 HAMMER2_RESOLVE_SHARED);
193 --info->depth;
194 break;
195 default:
196 /* does not recurse */
197 break;
200 if (chain) {
201 hammer2_chain_unlock(chain);
202 hammer2_chain_drop(chain);
206 * Save with higher pri now that we know what it is.
208 h2_bulkfree_test(info, &parent->bref, info->pri + 1);
210 hammer2_chain_unlock(parent);
212 return doabort;
216 * Bulkfree algorithm
218 * Repeat {
219 * Chain flush (partial synchronization) XXX removed
220 * Scan the whole topology - build in-memory freemap (mark 11)
221 * Reconcile the in-memory freemap against the on-disk freemap.
222 * ondisk xx -> ondisk 11 (if allocated)
223 * ondisk 11 -> ondisk 10 (if free in-memory)
224 * ondisk 10 -> ondisk 00 (if free in-memory) - on next pass
227 * The topology scan may have to be performed multiple times to window
228 * freemaps which are too large to fit in kernel memory.
230 * Races are handled using a double-transition (11->10, 10->00). The bulkfree
231 * scan snapshots the volume root's blockset and thus can run concurrent with
232 * normal operations, as long as a full flush is made between each pass to
233 * synchronize any modified chains (otherwise their blocks might be improperly
234 * freed).
236 * Temporary memory in multiples of 64KB is required to reconstruct the leaf
237 * hammer2_bmap_data blocks so they can later be compared against the live
238 * freemap. Each 64KB block represents 128 x 16KB x 1024 = ~2 GB of storage.
239 * A 32MB save area thus represents around ~1 TB. The temporary memory
240 * allocated can be specified. If it is not sufficient multiple topology
241 * passes will be made.
245 * Bulkfree callback info
247 static void hammer2_bulkfree_thread(void *arg __unused);
248 static void cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size);
249 static int h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo,
250 hammer2_blockref_t *bref);
251 static void h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
252 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
253 hammer2_off_t data_off, hammer2_bmap_data_t *live,
254 hammer2_bmap_data_t *bmap, int nofree);
256 void
257 hammer2_bulkfree_init(hammer2_dev_t *hmp)
259 hammer2_thr_create(&hmp->bfthr, NULL, hmp,
260 hmp->devrepname, -1, -1,
261 hammer2_bulkfree_thread);
264 void
265 hammer2_bulkfree_uninit(hammer2_dev_t *hmp)
267 hammer2_thr_delete(&hmp->bfthr);
270 static void
271 hammer2_bulkfree_thread(void *arg)
273 hammer2_thread_t *thr = arg;
274 hammer2_ioc_bulkfree_t bfi;
275 uint32_t flags;
277 for (;;) {
278 hammer2_thr_wait_any(thr,
279 HAMMER2_THREAD_STOP |
280 HAMMER2_THREAD_FREEZE |
281 HAMMER2_THREAD_UNFREEZE |
282 HAMMER2_THREAD_REMASTER,
283 hz * 60);
285 flags = thr->flags;
286 cpu_ccfence();
287 if (flags & HAMMER2_THREAD_STOP)
288 break;
289 if (flags & HAMMER2_THREAD_FREEZE) {
290 hammer2_thr_signal2(thr, HAMMER2_THREAD_FROZEN,
291 HAMMER2_THREAD_FREEZE);
292 continue;
294 if (flags & HAMMER2_THREAD_UNFREEZE) {
295 hammer2_thr_signal2(thr, 0,
296 HAMMER2_THREAD_FROZEN |
297 HAMMER2_THREAD_UNFREEZE);
298 continue;
300 if (flags & HAMMER2_THREAD_FROZEN)
301 continue;
302 if (flags & HAMMER2_THREAD_REMASTER) {
303 hammer2_thr_signal2(thr, 0, HAMMER2_THREAD_REMASTER);
304 bzero(&bfi, sizeof(bfi));
305 bfi.size = 8192 * 1024;
306 /* hammer2_bulkfree_pass(thr->hmp, &bfi); */
309 thr->td = NULL;
310 hammer2_thr_signal(thr, HAMMER2_THREAD_STOPPED);
311 /* structure can go invalid at this point */
315 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
316 hammer2_ioc_bulkfree_t *bfi)
318 hammer2_bulkfree_info_t cbinfo;
319 hammer2_chain_save_t *save;
320 hammer2_off_t incr;
321 size_t size;
322 int doabort = 0;
325 * We have to clear the live dedup cache as it might have entries
326 * that are freeable as of now. Any new entries in the dedup cache
327 * made after this point, even if they become freeable, will have
328 * previously been fully allocated and will be protected by the
329 * 2-stage bulkfree.
331 hammer2_dedup_clear(hmp);
334 * Setup for free pass
336 bzero(&cbinfo, sizeof(cbinfo));
337 size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
338 ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
339 cbinfo.hmp = hmp;
340 cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size, VM_SUBSYS_HAMMER);
341 cbinfo.saved_mirror_tid = hmp->voldata.mirror_tid;
343 cbinfo.dedup = kmalloc(sizeof(*cbinfo.dedup) * HAMMER2_DEDUP_HEUR_SIZE,
344 M_HAMMER2, M_WAITOK | M_ZERO);
347 * Normalize start point to a 2GB boundary. We operate on a
348 * 64KB leaf bitmap boundary which represents 2GB of storage.
350 cbinfo.sbase = bfi->sbase;
351 if (cbinfo.sbase > hmp->voldata.volu_size)
352 cbinfo.sbase = hmp->voldata.volu_size;
353 cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
354 TAILQ_INIT(&cbinfo.list);
357 * Loop on a full meta-data scan as many times as required to
358 * get through all available storage.
360 while (cbinfo.sbase < hmp->voldata.volu_size) {
362 * We have enough ram to represent (incr) bytes of storage.
363 * Each 64KB of ram represents 2GB of storage.
365 cbinfo_bmap_init(&cbinfo, size);
366 incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
367 HAMMER2_FREEMAP_LEVEL1_SIZE;
368 if (hmp->voldata.volu_size - cbinfo.sbase < incr)
369 cbinfo.sstop = hmp->voldata.volu_size;
370 else
371 cbinfo.sstop = cbinfo.sbase + incr;
372 if (hammer2_debug & 1) {
373 kprintf("bulkfree pass %016jx/%jdGB\n",
374 (intmax_t)cbinfo.sbase,
375 (intmax_t)incr / HAMMER2_FREEMAP_LEVEL1_SIZE);
379 * Scan topology for stuff inside this range.
381 hammer2_trans_init(hmp->spmp, 0);
382 cbinfo.mtid = hammer2_trans_sub(hmp->spmp);
383 cbinfo.pri = 0;
384 doabort |= hammer2_bulk_scan(vchain, h2_bulkfree_callback,
385 &cbinfo);
387 while ((save = TAILQ_FIRST(&cbinfo.list)) != NULL &&
388 doabort == 0) {
389 TAILQ_REMOVE(&cbinfo.list, save, entry);
390 cbinfo.pri = 0;
391 doabort |= hammer2_bulk_scan(save->chain,
392 h2_bulkfree_callback,
393 &cbinfo);
394 hammer2_chain_drop(save->chain);
395 kfree(save, M_HAMMER2);
397 while (save) {
398 TAILQ_REMOVE(&cbinfo.list, save, entry);
399 hammer2_chain_drop(save->chain);
400 kfree(save, M_HAMMER2);
401 save = TAILQ_FIRST(&cbinfo.list);
404 kprintf("bulkfree lastdrop %d %d doabort=%d\n",
405 vchain->refs, vchain->core.chain_count, doabort);
408 * If complete scan succeeded we can synchronize our
409 * in-memory freemap against live storage. If an abort
410 * did occur we cannot safely synchronize our partially
411 * filled-out in-memory freemap.
413 if (doabort == 0) {
414 h2_bulkfree_sync(&cbinfo);
416 hammer2_voldata_lock(hmp);
417 hammer2_voldata_modify(hmp);
418 hmp->voldata.allocator_free += cbinfo.adj_free;
419 hammer2_voldata_unlock(hmp);
423 * Cleanup for next loop.
425 hammer2_trans_done(hmp->spmp);
426 if (doabort)
427 break;
428 cbinfo.sbase = cbinfo.sstop;
429 cbinfo.adj_free = 0;
431 kmem_free_swapbacked(&cbinfo.kp);
432 kfree(cbinfo.dedup, M_HAMMER2);
433 cbinfo.dedup = NULL;
435 bfi->sstop = cbinfo.sbase;
437 incr = bfi->sstop / (hmp->voldata.volu_size / 10000);
438 if (incr > 10000)
439 incr = 10000;
441 kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
442 (int)incr / 100,
443 (int)incr % 100);
445 kprintf(" transition->free %ld\n", cbinfo.count_10_00);
446 kprintf(" transition->staged %ld\n", cbinfo.count_11_10);
447 kprintf(" ERR(00)->allocated %ld\n", cbinfo.count_00_11);
448 kprintf(" ERR(01)->allocated %ld\n", cbinfo.count_01_11);
449 kprintf(" staged->allocated %ld\n", cbinfo.count_10_11);
450 kprintf(" ~2MB segs cleaned %ld\n", cbinfo.count_l0cleans);
451 kprintf(" linear adjusts %ld\n", cbinfo.count_linadjusts);
452 kprintf(" dedup factor %ld\n", cbinfo.count_dedup_factor);
454 return doabort;
457 static void
458 cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size)
460 hammer2_bmap_data_t *bmap = cbinfo->bmap;
461 hammer2_key_t key = cbinfo->sbase;
462 hammer2_key_t lokey;
463 hammer2_key_t hikey;
465 lokey = (cbinfo->hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
466 ~HAMMER2_SEGMASK64;
467 hikey = cbinfo->hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
469 bzero(bmap, size);
470 while (size) {
471 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
472 HAMMER2_ZONE_SEG64) {
473 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
474 HAMMER2_ZONE_SEG64;
476 if (key < lokey || key >= hikey) {
477 memset(bmap->bitmapq, -1,
478 sizeof(bmap->bitmapq));
479 bmap->avail = 0;
480 bmap->linear = HAMMER2_SEGSIZE;
481 } else {
482 bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
484 size -= sizeof(*bmap);
485 key += HAMMER2_FREEMAP_LEVEL0_SIZE;
486 ++bmap;
490 static int
491 h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref)
493 hammer2_bmap_data_t *bmap;
494 hammer2_off_t data_off;
495 uint16_t class;
496 size_t bytes;
497 int radix;
498 int error;
501 * Check for signal and allow yield to userland during scan
503 if (hammer2_signal_check(&cbinfo->save_time))
504 return HAMMER2_BULK_ABORT;
505 if (bref->type == HAMMER2_BREF_TYPE_INODE) {
506 ++cbinfo->count_inodes_scanned;
507 if ((cbinfo->count_inodes_scanned & 65535) == 0)
508 kprintf(" inodes %6ld bytes %9ld\n",
509 cbinfo->count_inodes_scanned,
510 cbinfo->bytes_scanned);
514 * Calculate the data offset and determine if it is within
515 * the current freemap range being gathered.
517 error = 0;
518 data_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
519 if (data_off < cbinfo->sbase || data_off >= cbinfo->sstop)
520 return 0;
521 if (data_off < cbinfo->hmp->voldata.allocator_beg)
522 return 0;
523 if (data_off >= cbinfo->hmp->voldata.volu_size)
524 return 0;
527 * Calculate the information needed to generate the in-memory
528 * freemap record.
530 * Hammer2 does not allow allocations to cross the L1 (2GB) boundary,
531 * it's a problem if it does. (Or L0 (2MB) for that matter).
533 radix = (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
534 KKASSERT(radix != 0);
535 bytes = (size_t)1 << radix;
536 class = (bref->type << 8) | hammer2_devblkradix(radix);
538 if (data_off + bytes >= cbinfo->sstop) {
539 kprintf("hammer2_bulkfree_scan: illegal 2GB boundary "
540 "%016jx %016jx/%d\n",
541 (intmax_t)bref->data_off,
542 (intmax_t)bref->key,
543 bref->keybits);
544 bytes = cbinfo->sstop - data_off; /* XXX */
548 * Convert to a storage offset relative to the beginning of the
549 * storage range we are collecting. Then lookup the level0 bmap entry.
551 data_off -= cbinfo->sbase;
552 bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
555 * Convert data_off to a bmap-relative value (~4MB storage range).
556 * Adjust linear, class, and avail.
558 * Hammer2 does not allow allocations to cross the L0 (4MB) boundary,
560 data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
561 if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
562 kprintf("hammer2_bulkfree_scan: illegal 4MB boundary "
563 "%016jx %016jx/%d\n",
564 (intmax_t)bref->data_off,
565 (intmax_t)bref->key,
566 bref->keybits);
567 bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
570 if (bmap->class == 0) {
571 bmap->class = class;
572 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
574 if (bmap->class != class) {
575 kprintf("hammer2_bulkfree_scan: illegal mixed class "
576 "%016jx %016jx/%d (%04x vs %04x)\n",
577 (intmax_t)bref->data_off,
578 (intmax_t)bref->key,
579 bref->keybits,
580 class, bmap->class);
582 if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
583 bmap->linear = (int32_t)data_off + (int32_t)bytes;
586 * Adjust the hammer2_bitmap_t bitmap[HAMMER2_BMAP_ELEMENTS].
587 * 64-bit entries, 2 bits per entry, to code 11.
589 * NOTE: data_off mask to 524288, shift right by 14 (radix for 16384),
590 * and multiply shift amount by 2 for sets of 2 bits.
592 * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
593 * also, data_off may not be FREEMAP_BLOCK_SIZE aligned.
595 while (bytes > 0) {
596 hammer2_bitmap_t bmask;
597 int bindex;
599 bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
600 HAMMER2_BMAP_INDEX_RADIX);
601 bmask = (hammer2_bitmap_t)3 <<
602 ((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
603 HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
606 * NOTE! The (avail) calculation is bitmap-granular. Multiple
607 * sub-granular records can wind up at the same bitmap
608 * position.
610 if ((bmap->bitmapq[bindex] & bmask) == 0) {
611 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
612 bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
613 } else {
614 bmap->avail -= bytes;
616 bmap->bitmapq[bindex] |= bmask;
618 data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
619 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
620 bytes = 0;
621 else
622 bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
624 return error;
628 * Synchronize the in-memory bitmap with the live freemap. This is not a
629 * direct copy. Instead the bitmaps must be compared:
631 * In-memory Live-freemap
632 * 00 11 -> 10 (do nothing if live modified)
633 * 10 -> 00 (do nothing if live modified)
634 * 11 10 -> 11 handles race against live
635 * ** -> 11 nominally warn of corruption
638 static void
639 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
641 hammer2_off_t data_off;
642 hammer2_key_t key;
643 hammer2_key_t key_dummy;
644 hammer2_bmap_data_t *bmap;
645 hammer2_bmap_data_t *live;
646 hammer2_chain_t *live_parent;
647 hammer2_chain_t *live_chain;
648 int cache_index = -1;
649 int bmapindex;
651 kprintf("hammer2_bulkfree - range ");
653 if (cbinfo->sbase < cbinfo->hmp->voldata.allocator_beg)
654 kprintf("%016jx-",
655 (intmax_t)cbinfo->hmp->voldata.allocator_beg);
656 else
657 kprintf("%016jx-",
658 (intmax_t)cbinfo->sbase);
660 if (cbinfo->sstop > cbinfo->hmp->voldata.volu_size)
661 kprintf("%016jx\n",
662 (intmax_t)cbinfo->hmp->voldata.volu_size);
663 else
664 kprintf("%016jx\n",
665 (intmax_t)cbinfo->sstop);
667 data_off = cbinfo->sbase;
668 bmap = cbinfo->bmap;
670 live_parent = &cbinfo->hmp->fchain;
671 hammer2_chain_ref(live_parent);
672 hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
673 live_chain = NULL;
676 * Iterate each hammer2_bmap_data_t line (128 bytes) managing
677 * 4MB of storage.
679 while (data_off < cbinfo->sstop) {
681 * The freemap is not used below allocator_beg or beyond
682 * volu_size.
684 int nofree;
686 if (data_off < cbinfo->hmp->voldata.allocator_beg)
687 goto next;
688 if (data_off >= cbinfo->hmp->voldata.volu_size)
689 goto next;
692 * Locate the freemap leaf on the live filesystem
694 key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
695 nofree = 0;
697 if (live_chain == NULL || live_chain->bref.key != key) {
698 if (live_chain) {
699 hammer2_chain_unlock(live_chain);
700 hammer2_chain_drop(live_chain);
702 live_chain = hammer2_chain_lookup(
703 &live_parent,
704 &key_dummy,
705 key,
706 key + HAMMER2_FREEMAP_LEVEL1_MASK,
707 &cache_index,
708 HAMMER2_LOOKUP_ALWAYS);
710 #if 0
712 * If recent allocations were made we avoid races by
713 * not staging or freeing any blocks. We can still
714 * remark blocks as fully allocated.
716 if (live_chain) {
717 if (hammer2_debug & 1) {
718 kprintf("live_chain %016jx\n",
719 (intmax_t)key);
721 if (live_chain->bref.mirror_tid >
722 cbinfo->saved_mirror_tid) {
723 kprintf("hammer2_bulkfree: "
724 "avoid %016jx\n",
725 data_off);
726 nofree = 1;
727 } else {
728 nofree = 0;
731 #endif
733 if (live_chain == NULL) {
735 * XXX if we implement a full recovery mode we need
736 * to create/recreate missing freemap chains if our
737 * bmap has any allocated blocks.
739 if (bmap->class &&
740 bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
741 kprintf("hammer2_bulkfree: cannot locate "
742 "live leaf for allocated data "
743 "near %016jx\n",
744 (intmax_t)data_off);
746 goto next;
748 if (live_chain->error) {
749 kprintf("hammer2_bulkfree: error %s looking up "
750 "live leaf for allocated data near %016jx\n",
751 hammer2_error_str(live_chain->error),
752 (intmax_t)data_off);
753 hammer2_chain_unlock(live_chain);
754 hammer2_chain_drop(live_chain);
755 live_chain = NULL;
756 goto next;
759 bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
760 HAMMER2_FREEMAP_LEVEL0_RADIX;
761 live = &live_chain->data->bmdata[bmapindex];
764 * TODO - we could shortcut this by testing that both
765 * live->class and bmap->class are 0, and both avails are
766 * set to HAMMER2_FREEMAP_LEVEL0_SIZE (4MB).
768 if (bcmp(live->bitmapq, bmap->bitmapq,
769 sizeof(bmap->bitmapq)) == 0) {
770 goto next;
772 if (hammer2_debug & 1) {
773 kprintf("live %016jx %04d.%04x (avail=%d)\n",
774 data_off, bmapindex, live->class, live->avail);
777 hammer2_chain_modify(live_chain, cbinfo->mtid, 0, 0);
778 live = &live_chain->data->bmdata[bmapindex];
780 h2_bulkfree_sync_adjust(cbinfo, data_off, live, bmap, nofree);
781 next:
782 data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
783 ++bmap;
785 if (live_chain) {
786 hammer2_chain_unlock(live_chain);
787 hammer2_chain_drop(live_chain);
789 if (live_parent) {
790 hammer2_chain_unlock(live_parent);
791 hammer2_chain_drop(live_parent);
796 * When bulkfree is finally able to free a block it must make sure that
797 * the INVALOK bit in any cached DIO is cleared prior to the block being
798 * reused.
800 static
801 void
802 fixup_dio(hammer2_dev_t *hmp, hammer2_off_t data_off, int bindex, int scount)
804 data_off += (scount >> 1) * HAMMER2_FREEMAP_BLOCK_SIZE;
805 data_off += bindex *
806 (HAMMER2_FREEMAP_BLOCK_SIZE * HAMMER2_BMAP_BLOCKS_PER_ELEMENT);
807 hammer2_io_resetinval(hmp, data_off);
811 * Merge the bulkfree bitmap against the existing bitmap.
813 * If nofree is non-zero the merge will only mark free blocks as allocated
814 * and will refuse to free any blocks.
816 static
817 void
818 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
819 hammer2_off_t data_off, hammer2_bmap_data_t *live,
820 hammer2_bmap_data_t *bmap, int nofree)
822 int bindex;
823 int scount;
824 hammer2_bitmap_t lmask;
825 hammer2_bitmap_t mmask;
827 for (bindex = 0; bindex < HAMMER2_BMAP_ELEMENTS; ++bindex) {
828 lmask = live->bitmapq[bindex]; /* live */
829 mmask = bmap->bitmapq[bindex]; /* snapshotted bulkfree */
830 if (lmask == mmask)
831 continue;
833 for (scount = 0;
834 scount < HAMMER2_BMAP_BITS_PER_ELEMENT;
835 scount += 2) {
836 if ((mmask & 3) == 0) {
838 * in-memory 00 live 11 -> 10
839 * live 10 -> 00
841 * Storage might be marked allocated or
842 * staged and must be remarked staged or
843 * free.
845 switch (lmask & 3) {
846 case 0: /* 00 */
847 break;
848 case 1: /* 01 */
849 kprintf("hammer2_bulkfree: cannot "
850 "transition m=00/l=01\n");
851 break;
852 case 2: /* 10 -> 00 */
853 if (nofree)
854 break;
855 live->bitmapq[bindex] &=
856 ~((hammer2_bitmap_t)2 << scount);
857 live->avail +=
858 HAMMER2_FREEMAP_BLOCK_SIZE;
859 if (live->avail >
860 HAMMER2_FREEMAP_LEVEL0_SIZE) {
861 live->avail =
862 HAMMER2_FREEMAP_LEVEL0_SIZE;
864 cbinfo->adj_free +=
865 HAMMER2_FREEMAP_BLOCK_SIZE;
866 ++cbinfo->count_10_00;
867 fixup_dio(cbinfo->hmp, data_off,
868 bindex, scount);
869 break;
870 case 3: /* 11 -> 10 */
871 live->bitmapq[bindex] &=
872 ~((hammer2_bitmap_t)1 << scount);
873 ++cbinfo->count_11_10;
874 fixup_dio(cbinfo->hmp, data_off,
875 bindex, scount);
876 break;
878 } else if ((mmask & 3) == 3) {
880 * in-memory 11 live 10 -> 11
881 * live ** -> 11
883 * Storage might be incorrectly marked free
884 * or staged and must be remarked fully
885 * allocated.
887 switch (lmask & 3) {
888 case 0: /* 00 */
889 ++cbinfo->count_00_11;
890 cbinfo->adj_free -=
891 HAMMER2_FREEMAP_BLOCK_SIZE;
892 live->avail -=
893 HAMMER2_FREEMAP_BLOCK_SIZE;
894 if ((int32_t)live->avail < 0)
895 live->avail = 0;
896 break;
897 case 1: /* 01 */
898 ++cbinfo->count_01_11;
899 break;
900 case 2: /* 10 -> 11 */
901 ++cbinfo->count_10_11;
902 break;
903 case 3: /* 11 */
904 break;
906 live->bitmapq[bindex] |=
907 ((hammer2_bitmap_t)3 << scount);
908 fixup_dio(cbinfo->hmp, data_off,
909 bindex, scount);
911 mmask >>= 2;
912 lmask >>= 2;
917 * Determine if the live bitmap is completely free and reset its
918 * fields if so. Otherwise check to see if we can reduce the linear
919 * offset.
921 for (bindex = HAMMER2_BMAP_ELEMENTS - 1; bindex >= 0; --bindex) {
922 if (live->bitmapq[bindex] != 0)
923 break;
925 if (nofree) {
926 /* do nothing */
927 } else if (bindex < 0) {
928 live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
929 live->class = 0;
930 live->linear = 0;
931 ++cbinfo->count_l0cleans;
932 } else if (bindex < 7) {
933 ++bindex;
934 if (live->linear > bindex * HAMMER2_FREEMAP_BLOCK_SIZE) {
935 live->linear = bindex * HAMMER2_FREEMAP_BLOCK_SIZE;
936 ++cbinfo->count_linadjusts;
940 * XXX this fine-grained measure still has some issues.
942 if (live->linear < bindex * HAMMER2_FREEMAP_BLOCK_SIZE) {
943 live->linear = bindex * HAMMER2_FREEMAP_BLOCK_SIZE;
944 ++cbinfo->count_linadjusts;
946 } else {
947 live->linear = HAMMER2_SEGSIZE;
950 #if 0
951 if (bmap->class) {
952 kprintf("%016jx %04d.%04x (avail=%7d) "
953 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
954 (intmax_t)data_off,
955 (int)((data_off &
956 HAMMER2_FREEMAP_LEVEL1_MASK) >>
957 HAMMER2_FREEMAP_LEVEL0_RADIX),
958 bmap->class,
959 bmap->avail,
960 bmap->bitmap[0], bmap->bitmap[1],
961 bmap->bitmap[2], bmap->bitmap[3],
962 bmap->bitmap[4], bmap->bitmap[5],
963 bmap->bitmap[6], bmap->bitmap[7]);
965 #endif
969 * BULKFREE DEDUP HEURISTIC
971 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
972 * All fields must be loaded into locals and validated.
974 static
976 h2_bulkfree_test(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref,
977 int pri)
979 hammer2_dedup_t *dedup;
980 int best;
981 int n;
982 int i;
984 n = hammer2_icrc32(&bref->data_off, sizeof(bref->data_off));
985 dedup = cbinfo->dedup + (n & (HAMMER2_DEDUP_HEUR_MASK & ~7));
987 for (i = best = 0; i < 8; ++i) {
988 if (dedup[i].data_off == bref->data_off) {
989 if (dedup[i].ticks < pri)
990 dedup[i].ticks = pri;
991 if (pri == 1)
992 cbinfo->count_dedup_factor += dedup[i].ticks;
993 return 1;
995 if (dedup[i].ticks < dedup[best].ticks)
996 best = i;
998 dedup[best].data_off = bref->data_off;
999 dedup[best].ticks = pri;
1001 return 0;