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 * breadth-first search
52 typedef struct hammer2_chain_save {
53 TAILQ_ENTRY(hammer2_chain_save) entry;
54 hammer2_chain_t *parent;
55 } hammer2_chain_save_t;
57 TAILQ_HEAD(hammer2_chain_save_list, hammer2_chain_save);
58 typedef struct hammer2_chain_save_list hammer2_chain_save_list_t;
61 * General bulk scan function with callback. Called with a referenced
62 * but UNLOCKED parent. The original parent is returned in the same state.
65 hammer2_bulk_scan(hammer2_trans_t *trans, hammer2_chain_t *parent,
66 int (*func)(hammer2_chain_t *chain, void *info),
69 hammer2_chain_save_list_t list;
70 hammer2_chain_save_t *save;
74 hammer2_chain_ref(parent);
75 save = kmalloc(sizeof(*save), M_HAMMER2, M_WAITOK | M_ZERO);
76 save->parent = parent;
77 TAILQ_INSERT_TAIL(&list, save, entry);
79 while ((save = TAILQ_FIRST(&list)) != NULL && doabort == 0) {
80 hammer2_chain_t *chain;
83 TAILQ_REMOVE(&list, save, entry);
85 parent = save->parent;
91 * lock the parent, the lock eats the ref.
93 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
94 HAMMER2_RESOLVE_SHARED |
95 HAMMER2_RESOLVE_NOREF);
98 * Generally loop on the contents if we have not been flagged
101 while ((doabort & HAMMER2_BULK_ABORT) == 0) {
102 chain = hammer2_chain_scan(parent, chain, &cache_index,
103 HAMMER2_LOOKUP_NODATA |
104 HAMMER2_LOOKUP_SHARED);
107 doabort |= func(chain, info);
109 if (doabort & HAMMER2_BULK_ABORT) {
110 hammer2_chain_unlock(chain);
114 switch(chain->bref.type) {
115 case HAMMER2_BREF_TYPE_INODE:
116 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
117 case HAMMER2_BREF_TYPE_INDIRECT:
118 case HAMMER2_BREF_TYPE_VOLUME:
119 case HAMMER2_BREF_TYPE_FREEMAP:
121 * Breadth-first scan. Chain is referenced
122 * to save for later and will be unlocked on
123 * our loop (so it isn't left locked while on
127 save = kmalloc(sizeof(*save),
131 hammer2_chain_ref(chain);
132 save->parent = chain;
133 TAILQ_INSERT_TAIL(&list, save, entry);
137 /* does not recurse */
143 * Releases the lock and the ref the lock inherited. Free
144 * save structure if we didn't recycle it above.
146 hammer2_chain_unlock(parent);
148 kfree(save, M_HAMMER2);
152 * Cleanup anything left undone due to an abort
154 while ((save = TAILQ_FIRST(&list)) != NULL) {
155 TAILQ_REMOVE(&list, save, entry);
156 hammer2_chain_drop(save->parent);
157 kfree(save, M_HAMMER2);
164 * Bulkfree algorithm -
168 * Scan the whole topology and build the freemap
169 * ** -> 11 during scan for all elements scanned (and thus not free)
170 * 11 -> 10 after scan if allocated in-topo and free in-memory, mark 10
171 * 10 -> 00 after scan if possibly-free in-topo and free in-memory mark 00
174 * Adjustment of the freemap ->10 and ->00 cannot occur until the topology
175 * scan is complete. The scan runs concurrentlyt with normal filesystem
176 * operations and any allocation will also remark the freemap bitmap 11.
177 * We handle races by performing two scans and only changing the map to
178 * fully free (00) if both passes believe it is free.
180 * Temporary memory in multiples of 64KB is required to reconstruct leaf
181 * hammer2_bmap_data blocks so they can later be compared against the live
182 * freemap. Each 64KB block represents 128 x 16KB x 1024 = ~2 GB of storage.
183 * A 32MB save area thus represents around ~1 TB. The temporary memory
184 * allocated can be specified. If it is not sufficient multiple topology
185 * passes will be made.
189 * Bulkfree callback info
191 typedef struct hammer2_bulkfree_info {
193 hammer2_trans_t *trans;
195 hammer2_off_t sbase; /* sub-loop iteration */
197 hammer2_bmap_data_t *bmap;
202 long count_linadjusts;
203 hammer2_off_t adj_free;
205 } hammer2_bulkfree_info_t;
207 static int h2_bulkfree_callback(hammer2_chain_t *chain, void *info);
208 static void h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
209 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
210 hammer2_bmap_data_t *live, hammer2_bmap_data_t *bmap);
213 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_ioc_bulkfree_t *bfi)
215 hammer2_trans_t trans;
216 hammer2_bulkfree_info_t cbinfo;
221 /* hammer2_vfs_sync(hmp->mp, MNT_WAIT); XXX */
223 bzero(&cbinfo, sizeof(cbinfo));
224 size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
225 ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
226 cbinfo.trans = &trans;
228 cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size);
231 * Normalize start point to a 2GB boundary. We operate on a
232 * 64KB leaf bitmap boundary which represents 2GB of storage.
234 cbinfo.sbase = bfi->sbase;
235 if (cbinfo.sbase > hmp->voldata.volu_size)
236 cbinfo.sbase = hmp->voldata.volu_size;
237 cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
240 * Loop on a full meta-data scan as many times as required to
241 * get through all available storage.
243 while (cbinfo.sbase < hmp->voldata.volu_size) {
245 * We have enough ram to represent (incr) bytes of storage.
246 * Each 64KB of ram represents 2GB of storage.
248 bzero(cbinfo.bmap, size);
249 incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
250 HAMMER2_FREEMAP_LEVEL1_SIZE;
251 if (hmp->voldata.volu_size - cbinfo.sbase < incr)
252 cbinfo.sstop = hmp->voldata.volu_size;
254 cbinfo.sstop = cbinfo.sbase + incr;
255 kprintf("bulkfree pass %016jx/%jdGB\n",
256 (intmax_t)cbinfo.sbase,
257 (intmax_t)incr / HAMMER2_FREEMAP_LEVEL1_SIZE);
259 hammer2_trans_init(&trans, hmp->spmp, 0);
260 doabort |= hammer2_bulk_scan(&trans, &hmp->vchain,
261 h2_bulkfree_callback, &cbinfo);
264 * If complete scan succeeded we can synchronize our
265 * in-memory freemap against live storage. If an abort
266 * did occur we cannot safely synchronize our partially
267 * filled-out in-memory freemap.
270 h2_bulkfree_sync(&cbinfo);
272 hammer2_voldata_lock(hmp);
273 hammer2_voldata_modify(hmp);
274 hmp->voldata.allocator_free += cbinfo.adj_free;
275 hammer2_voldata_unlock(hmp);
279 * Cleanup for next loop.
281 hammer2_trans_done(&trans);
284 cbinfo.sbase = cbinfo.sstop;
286 kmem_free_swapbacked(&cbinfo.kp);
288 bfi->sstop = cbinfo.sbase;
290 incr = bfi->sstop / (hmp->voldata.volu_size / 10000);
294 kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
298 kprintf(" transition->free %ld\n", cbinfo.count_10_00);
299 kprintf(" transition->staged %ld\n", cbinfo.count_11_10);
300 kprintf(" raced on %ld\n", cbinfo.count_10_11);
301 kprintf(" ~2MB segs cleaned %ld\n", cbinfo.count_l0cleans);
302 kprintf(" linear adjusts %ld\n", cbinfo.count_linadjusts);
308 h2_bulkfree_callback(hammer2_chain_t *chain, void *info)
310 hammer2_bulkfree_info_t *cbinfo = info;
311 hammer2_bmap_data_t *bmap;
312 hammer2_off_t data_off;
319 * Check for signal and allow yield to userland during scan
321 if (hammer2_signal_check(&cbinfo->save_time))
322 return HAMMER2_BULK_ABORT;
325 kprintf("scan chain %016jx %016jx/%-2d type=%02x\n",
326 (intmax_t)chain->bref.data_off,
327 (intmax_t)chain->bref.key,
333 * Calculate the data offset and determine if it is within
334 * the current freemap range being gathered.
337 data_off = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
338 if (data_off < cbinfo->sbase || data_off > cbinfo->sstop)
340 if (data_off < chain->hmp->voldata.allocator_beg)
342 if (data_off > chain->hmp->voldata.volu_size)
346 * Calculate the information needed to generate the in-memory
349 * Hammer2 does not allow allocations to cross the L1 (2GB) boundary,
350 * it's a problem if it does. (Or L0 (2MB) for that matter).
352 radix = (int)(chain->bref.data_off & HAMMER2_OFF_MASK_RADIX);
353 bytes = (size_t)1 << radix;
354 class = (chain->bref.type << 8) | hammer2_devblkradix(radix);
356 if (data_off + bytes > cbinfo->sstop) {
357 kprintf("hammer2_bulkfree_scan: illegal 2GB boundary "
358 "%016jx %016jx/%d\n",
359 (intmax_t)chain->bref.data_off,
360 (intmax_t)chain->bref.key,
361 chain->bref.keybits);
362 bytes = cbinfo->sstop - data_off; /* XXX */
366 * Convert to a storage offset relative to the beginning of the
367 * storage range we are collecting. Then lookup the level0 bmap entry.
369 data_off -= cbinfo->sbase;
370 bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
373 * Convert data_off to a bmap-relative value (~2MB storage range).
374 * Adjust linear, class, and avail.
376 * Hammer2 does not allow allocations to cross the L0 (2MB) boundary,
378 data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
379 if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
380 kprintf("hammer2_bulkfree_scan: illegal 2MB boundary "
381 "%016jx %016jx/%d\n",
382 (intmax_t)chain->bref.data_off,
383 (intmax_t)chain->bref.key,
384 chain->bref.keybits);
385 bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
388 if (bmap->class == 0) {
390 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
392 if (bmap->class != class) {
393 kprintf("hammer2_bulkfree_scan: illegal mixed class "
394 "%016jx %016jx/%d (%04x vs %04x)\n",
395 (intmax_t)chain->bref.data_off,
396 (intmax_t)chain->bref.key,
400 if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
401 bmap->linear = (int32_t)data_off + (int32_t)bytes;
404 * Adjust the uint32_t bitmap[8]. 2 bits per entry, to code 11.
405 * Shortcut aligned 64KB allocations.
407 * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
413 bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
414 HAMMER2_BMAP_INDEX_RADIX);
415 bmask = 3 << ((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
416 HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
419 * NOTE! The (avail) calculation is bitmap-granular. Multiple
420 * sub-granular records can wind up at the same bitmap
423 if ((bmap->bitmap[bindex] & bmask) == 0) {
424 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
425 bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
427 bmap->avail -= bytes;
429 bmap->bitmap[bindex] |= bmask;
431 data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
432 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
435 bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
441 * Synchronize the in-memory bitmap with the live freemap. This is not a
442 * direct copy. Instead the bitmaps must be compared:
444 * In-memory Live-freemap
447 * 11 10 -> 11 handles race against live
448 * ** -> 11 nominally warn of corruption
452 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
454 hammer2_off_t data_off;
456 hammer2_key_t key_dummy;
457 hammer2_bmap_data_t *bmap;
458 hammer2_bmap_data_t *live;
459 hammer2_chain_t *live_parent;
460 hammer2_chain_t *live_chain;
461 int cache_index = -1;
464 kprintf("hammer2_bulkfree - range %016jx-%016jx\n",
465 (intmax_t)cbinfo->sbase,
466 (intmax_t)cbinfo->sstop);
468 data_off = cbinfo->sbase;
471 live_parent = &cbinfo->hmp->fchain;
472 hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
475 while (data_off < cbinfo->sstop) {
477 * The freemap is not used below allocator_beg or beyond
480 if (data_off < cbinfo->hmp->voldata.allocator_beg)
482 if (data_off > cbinfo->hmp->voldata.volu_size)
486 * Locate the freemap leaf on the live filesystem
488 key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
489 if (live_chain == NULL || live_chain->bref.key != key) {
491 hammer2_chain_unlock(live_chain);
492 live_chain = hammer2_chain_lookup(
496 key + HAMMER2_FREEMAP_LEVEL1_MASK,
498 HAMMER2_LOOKUP_ALWAYS);
500 kprintf("live_chain %016jx\n", (intmax_t)key);
503 if (live_chain == NULL) {
505 bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
506 kprintf("hammer2_bulkfree: cannot locate "
507 "live leaf for allocated data "
514 bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
515 HAMMER2_FREEMAP_LEVEL0_RADIX;
516 live = &live_chain->data->bmdata[bmapindex];
519 * For now just handle the 11->10, 10->00, and 10->11
522 if (live->class == 0 ||
523 live->avail == HAMMER2_FREEMAP_LEVEL0_SIZE) {
526 if (bcmp(live->bitmap, bmap->bitmap, sizeof(bmap->bitmap)) == 0)
528 kprintf("live %016jx %04d.%04x (avail=%d)\n",
529 data_off, bmapindex, live->class, live->avail);
531 hammer2_chain_modify(cbinfo->trans, live_chain, 0);
532 h2_bulkfree_sync_adjust(cbinfo, live, bmap);
534 data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
538 hammer2_chain_unlock(live_chain);
540 hammer2_chain_unlock(live_parent);
545 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
546 hammer2_bmap_data_t *live, hammer2_bmap_data_t *bmap)
553 for (bindex = 0; bindex < 8; ++bindex) {
554 lmask = live->bitmap[bindex];
555 mmask = bmap->bitmap[bindex];
559 for (scount = 0; scount < 32; scount += 2) {
560 if ((mmask & 3) == 0) {
562 * in-memory 00 live 11 -> 10
569 kprintf("hammer2_bulkfree: cannot "
570 "transition m=00/l=01\n");
572 case 2: /* 10 -> 00 */
573 live->bitmap[bindex] &= ~(2 << scount);
575 HAMMER2_FREEMAP_BLOCK_SIZE;
577 HAMMER2_FREEMAP_BLOCK_SIZE;
578 ++cbinfo->count_10_00;
580 case 3: /* 11 -> 10 */
581 live->bitmap[bindex] &= ~(1 << scount);
582 ++cbinfo->count_11_10;
585 } else if ((lmask & 3) == 3) {
587 * in-memory 11 live 10 -> 11
592 kprintf("hammer2_bulkfree: cannot "
593 "transition m=11/l=00\n");
596 kprintf("hammer2_bulkfree: cannot "
597 "transition m=11/l=01\n");
599 case 2: /* 10 -> 11 */
600 live->bitmap[bindex] |= (1 << scount);
601 ++cbinfo->count_10_11;
613 * Determine if the live bitmap is completely free and reset its
614 * fields if so. Otherwise check to see if we can reduce the linear
617 for (bindex = 7; bindex >= 0; --bindex) {
618 if (live->bitmap[bindex] != 0)
622 live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
625 ++cbinfo->count_l0cleans;
626 } else if (bindex < 7) {
628 if (live->linear > bindex * HAMMER2_FREEMAP_BLOCK_SIZE) {
629 live->linear = bindex * HAMMER2_FREEMAP_BLOCK_SIZE;
630 ++cbinfo->count_linadjusts;
636 kprintf("%016jx %04d.%04x (avail=%7d) "
637 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
640 HAMMER2_FREEMAP_LEVEL1_MASK) >>
641 HAMMER2_FREEMAP_LEVEL0_RADIX),
644 bmap->bitmap[0], bmap->bitmap[1],
645 bmap->bitmap[2], bmap->bitmap[3],
646 bmap->bitmap[4], bmap->bitmap[5],
647 bmap->bitmap[6], bmap->bitmap[7]);