489b686425cc509a2859812a5f18ec3d5d327f32
[dragonfly.git] / sys / vfs / hammer2 / hammer2_bulkfree.c
1 /*
2  * Copyright (c) 2013-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  */
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>
46
47 #include "hammer2.h"
48
49 #define H2FMBASE(key, radix)    ((key) & ~(((hammer2_off_t)1 << (radix)) - 1))
50 #define H2FMSHIFT(radix)        ((hammer2_off_t)1 << (radix))
51
52 /*
53  * breadth-first search
54  */
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;
60
61 TAILQ_HEAD(hammer2_chain_save_list, hammer2_chain_save);
62 typedef struct hammer2_chain_save_list hammer2_chain_save_list_t;
63
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;
89
90 static int h2_bulkfree_test(hammer2_bulkfree_info_t *info,
91                         hammer2_blockref_t *bref, int pri);
92
93 /*
94  * General bulk scan function with callback.  Called with a referenced
95  * but UNLOCKED parent.  The parent is returned in the same state.
96  */
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)
103 {
104         hammer2_blockref_t bref;
105         hammer2_chain_t *chain;
106         int cache_index = -1;
107         int doabort = 0;
108         int first = 1;
109
110         ++info->pri;
111
112         hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
113                                    HAMMER2_RESOLVE_SHARED);
114         chain = NULL;
115
116         /*
117          * Generally loop on the contents if we have not been flagged
118          * for abort.
119          *
120          * Remember that these chains are completely isolated from
121          * the frontend, so we can release locks temporarily without
122          * imploding.
123          */
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) {
129                 /*
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).
133                  */
134 #if 0
135                 kprintf("SCAN %p/%p %016jx.%02x\n",
136                         parent, chain, bref.data_off, bref.type);
137                 int xerr = tsleep(&info->pri, PCATCH, "slp", hz / 10);
138                 if (xerr == EINTR || xerr == ERESTART) {
139                         doabort |= HAMMER2_BULK_ABORT;
140                 }
141 #endif
142                 ++info->pri;
143                 if (h2_bulkfree_test(info, &bref, 1))
144                         continue;
145
146                 doabort |= func(info, &bref);
147
148                 if (doabort & HAMMER2_BULK_ABORT)
149                         break;
150
151                 /*
152                  * A non-null chain is always returned if it is
153                  * recursive, otherwise a non-null chain might be
154                  * returned but usually is not when not recursive.
155                  */
156                 if (chain == NULL)
157                         continue;
158
159                 /*
160                  * Else check type and setup depth-first scan.
161                  *
162                  * Account for bytes actually read.
163                  */
164                 info->bytes_scanned += chain->bytes;
165
166                 switch(chain->bref.type) {
167                 case HAMMER2_BREF_TYPE_INODE:
168                 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
169                 case HAMMER2_BREF_TYPE_INDIRECT:
170                 case HAMMER2_BREF_TYPE_VOLUME:
171                 case HAMMER2_BREF_TYPE_FREEMAP:
172                         ++info->depth;
173                         if (info->depth > 16) {
174                                 hammer2_chain_save_t *save;
175                                 save = kmalloc(sizeof(*save), M_HAMMER2,
176                                                M_WAITOK | M_ZERO);
177                                 save->chain = chain;
178                                 hammer2_chain_ref(chain);
179                                 TAILQ_INSERT_TAIL(&info->list, save, entry);
180
181                                 /* guess */
182                                 info->pri += 10;
183                         } else {
184                                 int savepri = info->pri;
185
186                                 hammer2_chain_unlock(chain);
187                                 info->pri = 0;
188                                 doabort |= hammer2_bulk_scan(chain, func, info);
189                                 info->pri += savepri;
190                                 hammer2_chain_lock(chain,
191                                                    HAMMER2_RESOLVE_ALWAYS |
192                                                    HAMMER2_RESOLVE_SHARED);
193                         }
194                         --info->depth;
195                         break;
196                 default:
197                         /* does not recurse */
198                         break;
199                 }
200         }
201         if (chain) {
202                 hammer2_chain_unlock(chain);
203                 hammer2_chain_drop(chain);
204         }
205
206         /*
207          * Save with higher pri now that we know what it is.
208          */
209         h2_bulkfree_test(info, &parent->bref, info->pri + 1);
210
211         hammer2_chain_unlock(parent);
212
213         return doabort;
214 }
215
216 /*
217  * Bulkfree algorithm
218  *
219  * Repeat {
220  *      Chain flush (partial synchronization) XXX removed
221  *      Scan the whole topology - build in-memory freemap (mark 11)
222  *      Reconcile the in-memory freemap against the on-disk freemap.
223  *              ondisk xx -> ondisk 11 (if allocated)
224  *              ondisk 11 -> ondisk 10 (if free in-memory)
225  *              ondisk 10 -> ondisk 00 (if free in-memory) - on next pass
226  * }
227  *
228  * The topology scan may have to be performed multiple times to window
229  * freemaps which are too large to fit in kernel memory.
230  *
231  * Races are handled using a double-transition (11->10, 10->00).  The bulkfree
232  * scan snapshots the volume root's blockset and thus can run concurrent with
233  * normal operations, as long as a full flush is made between each pass to
234  * synchronize any modified chains (otherwise their blocks might be improperly
235  * freed).
236  *
237  * Temporary memory in multiples of 64KB is required to reconstruct the leaf
238  * hammer2_bmap_data blocks so they can later be compared against the live
239  * freemap.  Each 64KB block represents 128 x 16KB x 1024 = ~2 GB of storage.
240  * A 32MB save area thus represents around ~1 TB.  The temporary memory
241  * allocated can be specified.  If it is not sufficient multiple topology
242  * passes will be made.
243  */
244
245 /*
246  * Bulkfree callback info
247  */
248 static void hammer2_bulkfree_thread(void *arg __unused);
249 static void cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size);
250 static int h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo,
251                         hammer2_blockref_t *bref);
252 static void h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
253 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
254                         hammer2_off_t data_off, hammer2_bmap_data_t *live,
255                         hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base);
256
257 void
258 hammer2_bulkfree_init(hammer2_dev_t *hmp)
259 {
260         hammer2_thr_create(&hmp->bfthr, NULL, hmp,
261                            hmp->devrepname, -1, -1,
262                            hammer2_bulkfree_thread);
263 }
264
265 void
266 hammer2_bulkfree_uninit(hammer2_dev_t *hmp)
267 {
268         hammer2_thr_delete(&hmp->bfthr);
269 }
270
271 static void
272 hammer2_bulkfree_thread(void *arg)
273 {
274         hammer2_thread_t *thr = arg;
275         hammer2_ioc_bulkfree_t bfi;
276         uint32_t flags;
277
278         for (;;) {
279                 hammer2_thr_wait_any(thr,
280                                      HAMMER2_THREAD_STOP |
281                                      HAMMER2_THREAD_FREEZE |
282                                      HAMMER2_THREAD_UNFREEZE |
283                                      HAMMER2_THREAD_REMASTER,
284                                      hz * 60);
285
286                 flags = thr->flags;
287                 cpu_ccfence();
288                 if (flags & HAMMER2_THREAD_STOP)
289                         break;
290                 if (flags & HAMMER2_THREAD_FREEZE) {
291                         hammer2_thr_signal2(thr, HAMMER2_THREAD_FROZEN,
292                                                  HAMMER2_THREAD_FREEZE);
293                         continue;
294                 }
295                 if (flags & HAMMER2_THREAD_UNFREEZE) {
296                         hammer2_thr_signal2(thr, 0,
297                                                  HAMMER2_THREAD_FROZEN |
298                                                  HAMMER2_THREAD_UNFREEZE);
299                         continue;
300                 }
301                 if (flags & HAMMER2_THREAD_FROZEN)
302                         continue;
303                 if (flags & HAMMER2_THREAD_REMASTER) {
304                         hammer2_thr_signal2(thr, 0, HAMMER2_THREAD_REMASTER);
305                         bzero(&bfi, sizeof(bfi));
306                         bfi.size = 8192 * 1024;
307                         /* hammer2_bulkfree_pass(thr->hmp, &bfi); */
308                 }
309         }
310         thr->td = NULL;
311         hammer2_thr_signal(thr, HAMMER2_THREAD_STOPPED);
312         /* structure can go invalid at this point */
313 }
314
315 int
316 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
317                       hammer2_ioc_bulkfree_t *bfi)
318 {
319         hammer2_bulkfree_info_t cbinfo;
320         hammer2_chain_save_t *save;
321         hammer2_off_t incr;
322         size_t size;
323         int doabort = 0;
324
325         /*
326          * We have to clear the live dedup cache as it might have entries
327          * that are freeable as of now.  Any new entries in the dedup cache
328          * made after this point, even if they become freeable, will have
329          * previously been fully allocated and will be protected by the
330          * 2-stage bulkfree.
331          */
332         hammer2_dedup_clear(hmp);
333
334         /*
335          * Setup for free pass
336          */
337         bzero(&cbinfo, sizeof(cbinfo));
338         size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
339                ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
340         cbinfo.hmp = hmp;
341         cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size, VM_SUBSYS_HAMMER);
342         cbinfo.saved_mirror_tid = hmp->voldata.mirror_tid;
343
344         cbinfo.dedup = kmalloc(sizeof(*cbinfo.dedup) * HAMMER2_DEDUP_HEUR_SIZE,
345                                M_HAMMER2, M_WAITOK | M_ZERO);
346
347         /*
348          * Normalize start point to a 2GB boundary.  We operate on a
349          * 64KB leaf bitmap boundary which represents 2GB of storage.
350          */
351         cbinfo.sbase = bfi->sbase;
352         if (cbinfo.sbase > hmp->voldata.volu_size)
353                 cbinfo.sbase = hmp->voldata.volu_size;
354         cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
355         TAILQ_INIT(&cbinfo.list);
356
357         /*
358          * Loop on a full meta-data scan as many times as required to
359          * get through all available storage.
360          */
361         while (cbinfo.sbase < hmp->voldata.volu_size) {
362                 /*
363                  * We have enough ram to represent (incr) bytes of storage.
364                  * Each 64KB of ram represents 2GB of storage.
365                  *
366                  * We must also clean out our de-duplication heuristic for
367                  * each (incr) bytes of storage, otherwise we wind up not
368                  * scanning meta-data for later areas of storage because
369                  * they had already been scanned in earlier areas of storage.
370                  * Since the ranging is different, we have to restart
371                  * the dedup heuristic too.
372                  */
373                 cbinfo_bmap_init(&cbinfo, size);
374                 bzero(cbinfo.dedup, sizeof(*cbinfo.dedup) *
375                                     HAMMER2_DEDUP_HEUR_SIZE);
376                 incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
377                        HAMMER2_FREEMAP_LEVEL1_SIZE;
378                 if (hmp->voldata.volu_size - cbinfo.sbase < incr)
379                         cbinfo.sstop = hmp->voldata.volu_size;
380                 else
381                         cbinfo.sstop = cbinfo.sbase + incr;
382                 if (hammer2_debug & 1) {
383                         kprintf("bulkfree pass %016jx/%jdGB\n",
384                                 (intmax_t)cbinfo.sbase,
385                                 (intmax_t)incr / HAMMER2_FREEMAP_LEVEL1_SIZE);
386                 }
387
388                 /*
389                  * Scan topology for stuff inside this range.
390                  */
391                 hammer2_trans_init(hmp->spmp, 0);
392                 cbinfo.mtid = hammer2_trans_sub(hmp->spmp);
393                 cbinfo.pri = 0;
394                 doabort |= hammer2_bulk_scan(vchain, h2_bulkfree_callback,
395                                              &cbinfo);
396
397                 while ((save = TAILQ_FIRST(&cbinfo.list)) != NULL &&
398                        doabort == 0) {
399                         TAILQ_REMOVE(&cbinfo.list, save, entry);
400                         cbinfo.pri = 0;
401                         doabort |= hammer2_bulk_scan(save->chain,
402                                                      h2_bulkfree_callback,
403                                                      &cbinfo);
404                         hammer2_chain_drop(save->chain);
405                         kfree(save, M_HAMMER2);
406                 }
407                 while (save) {
408                         TAILQ_REMOVE(&cbinfo.list, save, entry);
409                         hammer2_chain_drop(save->chain);
410                         kfree(save, M_HAMMER2);
411                         save = TAILQ_FIRST(&cbinfo.list);
412                 }
413
414                 kprintf("bulkfree lastdrop %d %d doabort=%d\n",
415                         vchain->refs, vchain->core.chain_count, doabort);
416
417                 /*
418                  * If complete scan succeeded we can synchronize our
419                  * in-memory freemap against live storage.  If an abort
420                  * did occur we cannot safely synchronize our partially
421                  * filled-out in-memory freemap.
422                  */
423                 if (doabort == 0) {
424                         h2_bulkfree_sync(&cbinfo);
425
426                         hammer2_voldata_lock(hmp);
427                         hammer2_voldata_modify(hmp);
428                         hmp->voldata.allocator_free += cbinfo.adj_free;
429                         hammer2_voldata_unlock(hmp);
430                 }
431
432                 /*
433                  * Cleanup for next loop.
434                  */
435                 hammer2_trans_done(hmp->spmp);
436                 if (doabort)
437                         break;
438                 cbinfo.sbase = cbinfo.sstop;
439                 cbinfo.adj_free = 0;
440         }
441         kmem_free_swapbacked(&cbinfo.kp);
442         kfree(cbinfo.dedup, M_HAMMER2);
443         cbinfo.dedup = NULL;
444
445         bfi->sstop = cbinfo.sbase;
446
447         incr = bfi->sstop / (hmp->voldata.volu_size / 10000);
448         if (incr > 10000)
449                 incr = 10000;
450
451         kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
452                 (int)incr / 100,
453                 (int)incr % 100);
454
455         kprintf("    transition->free   %ld\n", cbinfo.count_10_00);
456         kprintf("    transition->staged %ld\n", cbinfo.count_11_10);
457         kprintf("    ERR(00)->allocated %ld\n", cbinfo.count_00_11);
458         kprintf("    ERR(01)->allocated %ld\n", cbinfo.count_01_11);
459         kprintf("    staged->allocated  %ld\n", cbinfo.count_10_11);
460         kprintf("    ~2MB segs cleaned  %ld\n", cbinfo.count_l0cleans);
461         kprintf("    linear adjusts     %ld\n", cbinfo.count_linadjusts);
462         kprintf("    dedup factor       %ld\n", cbinfo.count_dedup_factor);
463
464         return doabort;
465 }
466
467 static void
468 cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size)
469 {
470         hammer2_bmap_data_t *bmap = cbinfo->bmap;
471         hammer2_key_t key = cbinfo->sbase;
472         hammer2_key_t lokey;
473         hammer2_key_t hikey;
474
475         lokey = (cbinfo->hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
476                 ~HAMMER2_SEGMASK64;
477         hikey = cbinfo->hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
478
479         bzero(bmap, size);
480         while (size) {
481                 if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
482                             HAMMER2_ZONE_SEG64) {
483                         lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
484                                 HAMMER2_ZONE_SEG64;
485                 }
486                 if (key < lokey || key >= hikey) {
487                         memset(bmap->bitmapq, -1,
488                                sizeof(bmap->bitmapq));
489                         bmap->avail = 0;
490                         bmap->linear = HAMMER2_SEGSIZE;
491                 } else {
492                         bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
493                 }
494                 size -= sizeof(*bmap);
495                 key += HAMMER2_FREEMAP_LEVEL0_SIZE;
496                 ++bmap;
497         }
498 }
499
500 static int
501 h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref)
502 {
503         hammer2_bmap_data_t *bmap;
504         hammer2_off_t data_off;
505         uint16_t class;
506         size_t bytes;
507         int radix;
508         int error;
509
510         /*
511          * Check for signal and allow yield to userland during scan
512          */
513         if (hammer2_signal_check(&cbinfo->save_time))
514                 return HAMMER2_BULK_ABORT;
515         if (bref->type == HAMMER2_BREF_TYPE_INODE) {
516                 ++cbinfo->count_inodes_scanned;
517                 if ((cbinfo->count_inodes_scanned & 65535) == 0)
518                         kprintf(" inodes %6ld bytes %9ld\n",
519                                 cbinfo->count_inodes_scanned,
520                                 cbinfo->bytes_scanned);
521         }
522
523         /*
524          * Calculate the data offset and determine if it is within
525          * the current freemap range being gathered.
526          */
527         error = 0;
528         data_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
529         if (data_off < cbinfo->sbase || data_off >= cbinfo->sstop)
530                 return 0;
531         if (data_off < cbinfo->hmp->voldata.allocator_beg)
532                 return 0;
533         if (data_off >= cbinfo->hmp->voldata.volu_size)
534                 return 0;
535
536         /*
537          * Calculate the information needed to generate the in-memory
538          * freemap record.
539          *
540          * Hammer2 does not allow allocations to cross the L1 (2GB) boundary,
541          * it's a problem if it does.  (Or L0 (2MB) for that matter).
542          */
543         radix = (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
544         KKASSERT(radix != 0);
545         bytes = (size_t)1 << radix;
546         class = (bref->type << 8) | hammer2_devblkradix(radix);
547
548         if (data_off + bytes >= cbinfo->sstop) {
549                 kprintf("hammer2_bulkfree_scan: illegal 2GB boundary "
550                         "%016jx %016jx/%d\n",
551                         (intmax_t)bref->data_off,
552                         (intmax_t)bref->key,
553                         bref->keybits);
554                 bytes = cbinfo->sstop - data_off;       /* XXX */
555         }
556
557         /*
558          * Convert to a storage offset relative to the beginning of the
559          * storage range we are collecting.  Then lookup the level0 bmap entry.
560          */
561         data_off -= cbinfo->sbase;
562         bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
563
564         /*
565          * Convert data_off to a bmap-relative value (~4MB storage range).
566          * Adjust linear, class, and avail.
567          *
568          * Hammer2 does not allow allocations to cross the L0 (4MB) boundary,
569          */
570         data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
571         if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
572                 kprintf("hammer2_bulkfree_scan: illegal 4MB boundary "
573                         "%016jx %016jx/%d\n",
574                         (intmax_t)bref->data_off,
575                         (intmax_t)bref->key,
576                         bref->keybits);
577                 bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
578         }
579
580         if (bmap->class == 0) {
581                 bmap->class = class;
582                 bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
583         }
584         if (bmap->class != class) {
585                 kprintf("hammer2_bulkfree_scan: illegal mixed class "
586                         "%016jx %016jx/%d (%04x vs %04x)\n",
587                         (intmax_t)bref->data_off,
588                         (intmax_t)bref->key,
589                         bref->keybits,
590                         class, bmap->class);
591         }
592
593         /*
594          * Just record the highest byte-granular offset for now.  Do not
595          * match against allocations which are in multiples of whole blocks.
596          *
597          * Make sure that any in-block linear offset at least covers the
598          * data range.  This can cause bmap->linear to become block-aligned.
599          */
600         if (bytes & HAMMER2_FREEMAP_BLOCK_MASK) {
601                 if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
602                         bmap->linear = (int32_t)data_off + (int32_t)bytes;
603         } else if (bmap->linear >= (int32_t)data_off &&
604                    bmap->linear < (int32_t)data_off + (int32_t)bytes) {
605                 bmap->linear = (int32_t)data_off + (int32_t)bytes;
606         }
607
608         /*
609          * Adjust the hammer2_bitmap_t bitmap[HAMMER2_BMAP_ELEMENTS].
610          * 64-bit entries, 2 bits per entry, to code 11.
611          *
612          * NOTE: data_off mask to 524288, shift right by 14 (radix for 16384),
613          *       and multiply shift amount by 2 for sets of 2 bits.
614          *
615          * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
616          *       also, data_off may not be FREEMAP_BLOCK_SIZE aligned.
617          */
618         while (bytes > 0) {
619                 hammer2_bitmap_t bmask;
620                 int bindex;
621
622                 bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
623                                            HAMMER2_BMAP_INDEX_RADIX);
624                 bmask = (hammer2_bitmap_t)3 <<
625                         ((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
626                          HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
627
628                 /*
629                  * NOTE! The (avail) calculation is bitmap-granular.  Multiple
630                  *       sub-granular records can wind up at the same bitmap
631                  *       position.
632                  */
633                 if ((bmap->bitmapq[bindex] & bmask) == 0) {
634                         if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
635                                 bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
636                         } else {
637                                 bmap->avail -= bytes;
638                         }
639                         bmap->bitmapq[bindex] |= bmask;
640                 }
641                 data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
642                 if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
643                         bytes = 0;
644                 else
645                         bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
646         }
647         return error;
648 }
649
650 /*
651  * Synchronize the in-memory bitmap with the live freemap.  This is not a
652  * direct copy.  Instead the bitmaps must be compared:
653  *
654  *      In-memory       Live-freemap
655  *         00             11 -> 10      (do nothing if live modified)
656  *                        10 -> 00      (do nothing if live modified)
657  *         11             10 -> 11      handles race against live
658  *                        ** -> 11      nominally warn of corruption
659  * 
660  */
661 static void
662 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
663 {
664         hammer2_off_t data_off;
665         hammer2_key_t key;
666         hammer2_key_t key_dummy;
667         hammer2_bmap_data_t *bmap;
668         hammer2_bmap_data_t *live;
669         hammer2_chain_t *live_parent;
670         hammer2_chain_t *live_chain;
671         int cache_index = -1;
672         int bmapindex;
673
674         kprintf("hammer2_bulkfree - range ");
675
676         if (cbinfo->sbase < cbinfo->hmp->voldata.allocator_beg)
677                 kprintf("%016jx-",
678                         (intmax_t)cbinfo->hmp->voldata.allocator_beg);
679         else
680                 kprintf("%016jx-",
681                         (intmax_t)cbinfo->sbase);
682
683         if (cbinfo->sstop > cbinfo->hmp->voldata.volu_size)
684                 kprintf("%016jx\n",
685                         (intmax_t)cbinfo->hmp->voldata.volu_size);
686         else
687                 kprintf("%016jx\n",
688                         (intmax_t)cbinfo->sstop);
689                 
690         data_off = cbinfo->sbase;
691         bmap = cbinfo->bmap;
692
693         live_parent = &cbinfo->hmp->fchain;
694         hammer2_chain_ref(live_parent);
695         hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
696         live_chain = NULL;
697
698         /*
699          * Iterate each hammer2_bmap_data_t line (128 bytes) managing
700          * 4MB of storage.
701          */
702         while (data_off < cbinfo->sstop) {
703                 /*
704                  * The freemap is not used below allocator_beg or beyond
705                  * volu_size.
706                  */
707
708                 if (data_off < cbinfo->hmp->voldata.allocator_beg)
709                         goto next;
710                 if (data_off >= cbinfo->hmp->voldata.volu_size)
711                         goto next;
712
713                 /*
714                  * Locate the freemap leaf on the live filesystem
715                  */
716                 key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
717
718                 if (live_chain == NULL || live_chain->bref.key != key) {
719                         if (live_chain) {
720                                 hammer2_chain_unlock(live_chain);
721                                 hammer2_chain_drop(live_chain);
722                         }
723                         live_chain = hammer2_chain_lookup(
724                                             &live_parent,
725                                             &key_dummy,
726                                             key,
727                                             key + HAMMER2_FREEMAP_LEVEL1_MASK,
728                                             &cache_index,
729                                             HAMMER2_LOOKUP_ALWAYS);
730
731 #if 0
732                         /*
733                          * If recent allocations were made we avoid races by
734                          * not staging or freeing any blocks.  We can still
735                          * remark blocks as fully allocated.
736                          */
737                         if (live_chain) {
738                                 if (hammer2_debug & 1) {
739                                         kprintf("live_chain %016jx\n",
740                                                 (intmax_t)key);
741                                 }
742                                 if (live_chain->bref.mirror_tid >
743                                     cbinfo->saved_mirror_tid) {
744                                         kprintf("hammer2_bulkfree: "
745                                                 "avoid %016jx\n",
746                                                 data_off);
747                                         nofree = 1;
748                                 } else {
749                                         nofree = 0;
750                                 }
751                         }
752 #endif
753                 }
754                 if (live_chain == NULL) {
755                         /*
756                          * XXX if we implement a full recovery mode we need
757                          * to create/recreate missing freemap chains if our
758                          * bmap has any allocated blocks.
759                          */
760                         if (bmap->class &&
761                             bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
762                                 kprintf("hammer2_bulkfree: cannot locate "
763                                         "live leaf for allocated data "
764                                         "near %016jx\n",
765                                         (intmax_t)data_off);
766                         }
767                         goto next;
768                 }
769                 if (live_chain->error) {
770                         kprintf("hammer2_bulkfree: error %s looking up "
771                                 "live leaf for allocated data near %016jx\n",
772                                 hammer2_error_str(live_chain->error),
773                                 (intmax_t)data_off);
774                         hammer2_chain_unlock(live_chain);
775                         hammer2_chain_drop(live_chain);
776                         live_chain = NULL;
777                         goto next;
778                 }
779
780                 bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
781                             HAMMER2_FREEMAP_LEVEL0_RADIX;
782                 live = &live_chain->data->bmdata[bmapindex];
783
784                 /*
785                  * Shortcut if the bitmaps match and the live linear
786                  * indicator is sane.  We can't do a perfect check of
787                  * live->linear because the only real requirement is that
788                  * if it is not block-aligned, that it not cover the space
789                  * within its current block which overlaps one of the data
790                  * ranges we scan.  We don't retain enough fine-grained
791                  * data in our scan to be able to set it exactly.
792                  *
793                  * TODO - we could shortcut this by testing that both
794                  * live->class and bmap->class are 0, and both avails are
795                  * set to HAMMER2_FREEMAP_LEVEL0_SIZE (4MB).
796                  */
797                 if (bcmp(live->bitmapq, bmap->bitmapq,
798                          sizeof(bmap->bitmapq)) == 0 &&
799                     live->linear >= bmap->linear) {
800                         goto next;
801                 }
802                 if (hammer2_debug & 1) {
803                         kprintf("live %016jx %04d.%04x (avail=%d)\n",
804                                 data_off, bmapindex, live->class, live->avail);
805                 }
806
807                 hammer2_chain_modify(live_chain, cbinfo->mtid, 0, 0);
808                 live = &live_chain->data->bmdata[bmapindex];
809
810                 h2_bulkfree_sync_adjust(cbinfo, data_off, live, bmap,
811                                         live_chain->bref.key +
812                                         bmapindex *
813                                         HAMMER2_FREEMAP_LEVEL0_SIZE);
814 next:
815                 data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
816                 ++bmap;
817         }
818         if (live_chain) {
819                 hammer2_chain_unlock(live_chain);
820                 hammer2_chain_drop(live_chain);
821         }
822         if (live_parent) {
823                 hammer2_chain_unlock(live_parent);
824                 hammer2_chain_drop(live_parent);
825         }
826 }
827
828 /*
829  * Merge the bulkfree bitmap against the existing bitmap.
830  */
831 static
832 void
833 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
834                         hammer2_off_t data_off, hammer2_bmap_data_t *live,
835                         hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base)
836 {
837         int bindex;
838         int scount;
839         hammer2_off_t tmp_off;
840         hammer2_bitmap_t lmask;
841         hammer2_bitmap_t mmask;
842
843         tmp_off = data_off;
844
845         for (bindex = 0; bindex < HAMMER2_BMAP_ELEMENTS; ++bindex) {
846                 lmask = live->bitmapq[bindex];  /* live */
847                 mmask = bmap->bitmapq[bindex];  /* snapshotted bulkfree */
848                 if (lmask == mmask) {
849                         tmp_off += HAMMER2_BMAP_INDEX_SIZE;
850                         continue;
851                 }
852
853                 for (scount = 0;
854                      scount < HAMMER2_BMAP_BITS_PER_ELEMENT;
855                      scount += 2) {
856                         if ((mmask & 3) == 0) {
857                                 /*
858                                  * in-memory 00         live 11 -> 10
859                                  *                      live 10 -> 00
860                                  *
861                                  * Storage might be marked allocated or
862                                  * staged and must be remarked staged or
863                                  * free.
864                                  */
865                                 switch (lmask & 3) {
866                                 case 0: /* 00 */
867                                         break;
868                                 case 1: /* 01 */
869                                         kprintf("hammer2_bulkfree: cannot "
870                                                 "transition m=00/l=01\n");
871                                         break;
872                                 case 2: /* 10 -> 00 */
873                                         live->bitmapq[bindex] &=
874                                             ~((hammer2_bitmap_t)2 << scount);
875                                         live->avail +=
876                                                 HAMMER2_FREEMAP_BLOCK_SIZE;
877                                         if (live->avail >
878                                             HAMMER2_FREEMAP_LEVEL0_SIZE) {
879                                                 live->avail =
880                                                     HAMMER2_FREEMAP_LEVEL0_SIZE;
881                                         }
882                                         cbinfo->adj_free +=
883                                                 HAMMER2_FREEMAP_BLOCK_SIZE;
884                                         ++cbinfo->count_10_00;
885                                         hammer2_io_dedup_assert(
886                                                 cbinfo->hmp,
887                                                 tmp_off |
888                                                 HAMMER2_FREEMAP_BLOCK_RADIX,
889                                                 HAMMER2_FREEMAP_BLOCK_SIZE);
890                                         break;
891                                 case 3: /* 11 -> 10 */
892                                         live->bitmapq[bindex] &=
893                                             ~((hammer2_bitmap_t)1 << scount);
894                                         ++cbinfo->count_11_10;
895                                         hammer2_io_dedup_delete(
896                                                 cbinfo->hmp,
897                                                 HAMMER2_BREF_TYPE_DATA,
898                                                 tmp_off |
899                                                 HAMMER2_FREEMAP_BLOCK_RADIX,
900                                                 HAMMER2_FREEMAP_BLOCK_SIZE);
901                                         break;
902                                 }
903                         } else if ((mmask & 3) == 3) {
904                                 /*
905                                  * in-memory 11         live 10 -> 11
906                                  *                      live ** -> 11
907                                  *
908                                  * Storage might be incorrectly marked free
909                                  * or staged and must be remarked fully
910                                  * allocated.
911                                  */
912                                 switch (lmask & 3) {
913                                 case 0: /* 00 */
914                                         ++cbinfo->count_00_11;
915                                         cbinfo->adj_free -=
916                                                 HAMMER2_FREEMAP_BLOCK_SIZE;
917                                         live->avail -=
918                                                 HAMMER2_FREEMAP_BLOCK_SIZE;
919                                         if ((int32_t)live->avail < 0)
920                                                 live->avail = 0;
921                                         break;
922                                 case 1: /* 01 */
923                                         ++cbinfo->count_01_11;
924                                         break;
925                                 case 2: /* 10 -> 11 */
926                                         ++cbinfo->count_10_11;
927                                         break;
928                                 case 3: /* 11 */
929                                         break;
930                                 }
931                                 live->bitmapq[bindex] |=
932                                         ((hammer2_bitmap_t)3 << scount);
933                         }
934                         mmask >>= 2;
935                         lmask >>= 2;
936                         tmp_off += HAMMER2_FREEMAP_BLOCK_SIZE;
937                 }
938         }
939
940         /*
941          * Determine if the live bitmap is completely free and reset its
942          * fields if so.  Otherwise check to see if we can reduce the linear
943          * offset.
944          */
945         for (bindex = HAMMER2_BMAP_ELEMENTS - 1; bindex >= 0; --bindex) {
946                 if (live->bitmapq[bindex] != 0)
947                         break;
948         }
949         if (bindex < 0) {
950                 /*
951                  * Completely empty, reset entire segment
952                  */
953 #if 0
954                 kprintf("hammer2: cleanseg %016jx.%04x (%d)\n",
955                         alloc_base, live->class, live->avail);
956 #endif
957                 live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
958                 live->class = 0;
959                 live->linear = 0;
960                 ++cbinfo->count_l0cleans;
961         } else if (bindex < 7) {
962                 /*
963                  * Partially full, bitmapq[bindex] != 0.  The live->linear
964                  * offset can legitimately be just about anything, but
965                  * our bulkfree pass doesn't record enough information to
966                  * set it exactly.  Just make sure that it is set to a
967                  * safe value that also works in our match code above (the
968                  * bcmp and linear test).
969                  *
970                  * We cannot safely leave live->linear at a sub-block offset
971                  * unless it is already in the same block as bmap->linear.
972                  *
973                  * If it is not in the same block, we cannot assume that
974                  * we can set it to bmap->linear on a sub-block boundary,
975                  * because the live system could have bounced it around.
976                  * In that situation we satisfy our bcmp/skip requirement
977                  * above by setting it to the nearest higher block boundary.
978                  * This alignment effectively kills any partial allocation it
979                  * might have been tracking before.
980                  */
981                 if (live->linear < bmap->linear &&
982                     ((live->linear ^ bmap->linear) &
983                      ~HAMMER2_FREEMAP_BLOCK_MASK) == 0) {
984                         live->linear = bmap->linear;
985                         ++cbinfo->count_linadjusts;
986                 } else {
987                         live->linear =
988                                 (bmap->linear + HAMMER2_FREEMAP_BLOCK_MASK) &
989                                 ~HAMMER2_FREEMAP_BLOCK_MASK;
990                         ++cbinfo->count_linadjusts;
991                 }
992         } else {
993                 /*
994                  * Completely full, effectively disable the linear iterator
995                  */
996                 live->linear = HAMMER2_SEGSIZE;
997         }
998
999 #if 0
1000         if (bmap->class) {
1001                 kprintf("%016jx %04d.%04x (avail=%7d) "
1002                         "%08x %08x %08x %08x %08x %08x %08x %08x\n",
1003                         (intmax_t)data_off,
1004                         (int)((data_off &
1005                                HAMMER2_FREEMAP_LEVEL1_MASK) >>
1006                               HAMMER2_FREEMAP_LEVEL0_RADIX),
1007                         bmap->class,
1008                         bmap->avail,
1009                         bmap->bitmap[0], bmap->bitmap[1],
1010                         bmap->bitmap[2], bmap->bitmap[3],
1011                         bmap->bitmap[4], bmap->bitmap[5],
1012                         bmap->bitmap[6], bmap->bitmap[7]);
1013         }
1014 #endif
1015 }
1016
1017 /*
1018  * BULKFREE DEDUP HEURISTIC
1019  *
1020  * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1021  *          All fields must be loaded into locals and validated.
1022  */
1023 static
1024 int
1025 h2_bulkfree_test(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref,
1026                  int pri)
1027 {
1028         hammer2_dedup_t *dedup;
1029         int best;
1030         int n;
1031         int i;
1032
1033         n = hammer2_icrc32(&bref->data_off, sizeof(bref->data_off));
1034         dedup = cbinfo->dedup + (n & (HAMMER2_DEDUP_HEUR_MASK & ~7));
1035
1036         for (i = best = 0; i < 8; ++i) {
1037                 if (dedup[i].data_off == bref->data_off) {
1038                         if (dedup[i].ticks < pri)
1039                                 dedup[i].ticks = pri;
1040                         if (pri == 1)
1041                                 cbinfo->count_dedup_factor += dedup[i].ticks;
1042                         return 1;
1043                 }
1044                 if (dedup[i].ticks < dedup[best].ticks)
1045                         best = i;
1046         }
1047         dedup[best].data_off = bref->data_off;
1048         dedup[best].ticks = pri;
1049
1050         return 0;
1051 }