1 /*
2  * Copyright (c) 2013-2019 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/proc.h>
38 #include <sys/mount.h>
39 #include <vm/vm_kern.h>
40 #include <vm/vm_extern.h>
41 
42 #include "hammer2.h"
43 
44 /*
45  * breadth-first search
46  */
47 typedef struct hammer2_chain_save {
48 	TAILQ_ENTRY(hammer2_chain_save)	entry;
49 	hammer2_chain_t	*chain;
50 } hammer2_chain_save_t;
51 
52 TAILQ_HEAD(hammer2_chain_save_list, hammer2_chain_save);
53 typedef struct hammer2_chain_save_list hammer2_chain_save_list_t;
54 
55 typedef struct hammer2_bulkfree_info {
56 	hammer2_dev_t		*hmp;
57 	kmem_anon_desc_t	kp;
58 	hammer2_off_t		sbase;		/* sub-loop iteration */
59 	hammer2_off_t		sstop;
60 	hammer2_bmap_data_t	*bmap;
61 	int			depth;
62 	long			count_10_00;	/* staged->free	     */
63 	long			count_11_10;	/* allocated->staged */
64 	long			count_00_11;	/* (should not happen) */
65 	long			count_01_11;	/* (should not happen) */
66 	long			count_10_11;	/* staged->allocated */
67 	long			count_l0cleans;
68 	long			count_linadjusts;
69 	long			count_inodes_scanned;
70 	long			count_dirents_scanned;
71 	long			count_dedup_factor;
72 	long			count_bytes_scanned;
73 	long			count_chains_scanned;
74 	long			count_chains_reported;
75 	long			bulkfree_calls;
76 	int			bulkfree_ticks;
77 	hammer2_off_t		adj_free;
78 	hammer2_tid_t		mtid;
79 	time_t			save_time;
80 	hammer2_chain_save_list_t list;
81 	hammer2_dedup_t		*dedup;
82 	int			pri;
83 } hammer2_bulkfree_info_t;
84 
85 static int h2_bulkfree_test(hammer2_bulkfree_info_t *info,
86 			hammer2_blockref_t *bref, int pri, int saved_error);
87 
88 /*
89  * General bulk scan function with callback.  Called with a referenced
90  * but UNLOCKED parent.  The parent is returned in the same state.
91  */
92 static
93 int
94 hammer2_bulkfree_scan(hammer2_chain_t *parent,
95 		  int (*func)(hammer2_bulkfree_info_t *info,
96 			      hammer2_blockref_t *bref),
97 		  hammer2_bulkfree_info_t *info)
98 {
99 	hammer2_blockref_t bref;
100 	hammer2_chain_t *chain;
101 	hammer2_chain_save_t *tail;
102 	hammer2_chain_save_t *save;
103 	int first = 1;
104 	int rup_error;
105 	int error;
106 	int e2;
107 
108 	++info->pri;
109 
110 	chain = NULL;
111 	rup_error = 0;
112 	error = 0;
113 
114 	hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
115 				   HAMMER2_RESOLVE_SHARED);
116 	tail = TAILQ_LAST(&info->list, hammer2_chain_save_list);
117 
118 	/*
119 	 * The parent was previously retrieved NODATA and thus has not
120 	 * tested the CRC.  Now that we have locked it normally, check
121 	 * for a CRC problem and skip it if we found one.  The bulk scan
122 	 * cannot safely traverse invalid block tables (we could end up
123 	 * in an endless loop or cause a panic).
124 	 */
125 	if (parent->error & HAMMER2_ERROR_CHECK) {
126 		error = parent->error;
127 		goto done;
128 	}
129 
130 	/*
131 	 * Report which PFS is being scanned
132 	 */
133 	if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
134 	    (parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT)) {
135 		kprintf("hammer2_bulkfree: Scanning %s\n",
136 			parent->data->ipdata.filename);
137 	}
138 
139 	/*
140 	 * Generally loop on the contents if we have not been flagged
141 	 * for abort.
142 	 *
143 	 * Remember that these chains are completely isolated from
144 	 * the frontend, so we can release locks temporarily without
145 	 * imploding.
146 	 */
147 	for (;;) {
148 		error |= hammer2_chain_scan(parent, &chain, &bref, &first,
149 					    HAMMER2_LOOKUP_NODATA |
150 					    HAMMER2_LOOKUP_SHARED);
151 
152 		/*
153 		 * Handle EOF or other error at current level.  This stops
154 		 * the bulkfree scan.
155 		 */
156 		if (error & ~HAMMER2_ERROR_CHECK)
157 			break;
158 
159 		/*
160 		 * Account for dirents before thre data_off test, since most
161 		 * dirents do not need a data reference.
162 		 */
163 		if (bref.type == HAMMER2_BREF_TYPE_DIRENT)
164 			++info->count_dirents_scanned;
165 
166 		/*
167 		 * Ignore brefs without data (typically dirents)
168 		 */
169 		if ((bref.data_off & ~HAMMER2_OFF_MASK_RADIX) == 0)
170 			continue;
171 
172 		/*
173 		 * Process bref, chain is only non-NULL if the bref
174 		 * might be recursable (its possible that we sometimes get
175 		 * a non-NULL chain where the bref cannot be recursed).
176 		 *
177 		 * If we already ran down this tree we do not have to do it
178 		 * again, but we must still recover any cumulative error
179 		 * recorded from the time we did.
180 		 */
181 		++info->pri;
182 		e2 = h2_bulkfree_test(info, &bref, 1, 0);
183 		if (e2) {
184 			error |= e2 & ~HAMMER2_ERROR_EOF;
185 			continue;
186 		}
187 
188 		if (bref.type == HAMMER2_BREF_TYPE_INODE)
189 			++info->count_inodes_scanned;
190 
191 		error |= func(info, &bref);
192 		if (error & ~HAMMER2_ERROR_CHECK)
193 			break;
194 
195 		/*
196 		 * A non-null chain is always returned if it is
197 		 * recursive, otherwise a non-null chain might be
198 		 * returned but usually is not when not recursive.
199 		 */
200 		if (chain == NULL)
201 			continue;
202 
203 		if (chain) {
204 			info->count_bytes_scanned += chain->bytes;
205 			++info->count_chains_scanned;
206 
207 			if (info->count_chains_scanned >=
208 			    info->count_chains_reported + 1000000 ||
209 			    (info->count_chains_scanned < 1000000 &&
210 			     info->count_chains_scanned >=
211 			     info->count_chains_reported + 100000)) {
212 				kprintf(" chains %-7ld inodes %-7ld "
213 					"dirents %-7ld bytes %5ldMB\n",
214 					info->count_chains_scanned,
215 					info->count_inodes_scanned,
216 					info->count_dirents_scanned,
217 					info->count_bytes_scanned / 1000000);
218 				info->count_chains_reported =
219 					info->count_chains_scanned;
220 			}
221 		}
222 
223 		/*
224 		 * Else check type and setup depth-first scan.
225 		 *
226 		 * Account for bytes actually read.
227 		 */
228 		switch(chain->bref.type) {
229 		case HAMMER2_BREF_TYPE_INODE:
230 		case HAMMER2_BREF_TYPE_FREEMAP_NODE:
231 		case HAMMER2_BREF_TYPE_INDIRECT:
232 		case HAMMER2_BREF_TYPE_VOLUME:
233 		case HAMMER2_BREF_TYPE_FREEMAP:
234 			++info->depth;
235 			if (chain->error & HAMMER2_ERROR_CHECK) {
236 				/*
237 				 * Cannot safely recurse chains with crc
238 				 * errors, even in emergency mode.
239 				 */
240 				/* NOP */
241 			} else if (info->depth > 16) {
242 				save = kmalloc(sizeof(*save), M_HAMMER2,
243 					       M_WAITOK | M_ZERO);
244 				save->chain = chain;
245 				hammer2_chain_ref(chain);
246 				TAILQ_INSERT_TAIL(&info->list, save, entry);
247 
248 				/* guess */
249 				info->pri += 10;
250 			} else {
251 				int savepri = info->pri;
252 
253 				hammer2_chain_unlock(chain);
254 				hammer2_chain_unlock(parent);
255 				info->pri = 0;
256 				rup_error |= hammer2_bulkfree_scan(chain,
257 								   func, info);
258 				info->pri += savepri;
259 				hammer2_chain_lock(parent,
260 						   HAMMER2_RESOLVE_ALWAYS |
261 						   HAMMER2_RESOLVE_SHARED);
262 				hammer2_chain_lock(chain,
263 						   HAMMER2_RESOLVE_ALWAYS |
264 						   HAMMER2_RESOLVE_SHARED);
265 			}
266 			--info->depth;
267 			break;
268 		case HAMMER2_BREF_TYPE_DATA:
269 			break;
270 		default:
271 			/* does not recurse */
272 			break;
273 		}
274 		if (rup_error & HAMMER2_ERROR_ABORTED)
275 			break;
276 	}
277 	if (chain) {
278 		hammer2_chain_unlock(chain);
279 		hammer2_chain_drop(chain);
280 	}
281 
282 	/*
283 	 * If this is a PFSROOT, also re-run any defered elements
284 	 * added during our scan so we can report any cumulative errors
285 	 * for the PFS.
286 	 */
287 	if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
288 	    (parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT)) {
289 		for (;;) {
290 			int opri;
291 
292 			save = tail ? TAILQ_NEXT(tail, entry) :
293 				      TAILQ_FIRST(&info->list);
294 			if (save == NULL)
295 				break;
296 
297 			TAILQ_REMOVE(&info->list, save, entry);
298 			opri = info->pri;
299 			info->pri = 0;
300 			rup_error |= hammer2_bulkfree_scan(save->chain, func, info);
301 			hammer2_chain_drop(save->chain);
302 			kfree(save, M_HAMMER2);
303 			info->pri = opri;
304 		}
305 	}
306 
307 	error |= rup_error;
308 
309 	/*
310 	 * Report which PFS the errors were encountered in.
311 	 */
312 	if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
313 	    (parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) &&
314 	    (error & ~HAMMER2_ERROR_EOF)) {
315 		kprintf("hammer2_bulkfree: Encountered errors (%08x) "
316 			"while scanning \"%s\"\n",
317 			error, parent->data->ipdata.filename);
318 	}
319 
320 	/*
321 	 * Save with higher pri now that we know what it is.
322 	 */
323 	h2_bulkfree_test(info, &parent->bref, info->pri + 1,
324 			 (error & ~HAMMER2_ERROR_EOF));
325 
326 done:
327 	hammer2_chain_unlock(parent);
328 
329 	return (error & ~HAMMER2_ERROR_EOF);
330 }
331 
332 /*
333  * Bulkfree algorithm
334  *
335  * Repeat {
336  *	Chain flush (partial synchronization) XXX removed
337  *	Scan the whole topology - build in-memory freemap (mark 11)
338  *	Reconcile the in-memory freemap against the on-disk freemap.
339  *		ondisk xx -> ondisk 11 (if allocated)
340  *		ondisk 11 -> ondisk 10 (if free in-memory)
341  *		ondisk 10 -> ondisk 00 (if free in-memory) - on next pass
342  * }
343  *
344  * The topology scan may have to be performed multiple times to window
345  * freemaps which are too large to fit in kernel memory.
346  *
347  * Races are handled using a double-transition (11->10, 10->00).  The bulkfree
348  * scan snapshots the volume root's blockset and thus can run concurrent with
349  * normal operations, as long as a full flush is made between each pass to
350  * synchronize any modified chains (otherwise their blocks might be improperly
351  * freed).
352  *
353  * Temporary memory in multiples of 32KB is required to reconstruct the leaf
354  * hammer2_bmap_data blocks so they can later be compared against the live
355  * freemap.  Each 32KB represents 256 x 16KB x 256 = ~1 GB of storage.
356  * A 32MB save area thus represents around ~1 TB.  The temporary memory
357  * allocated can be specified.  If it is not sufficient multiple topology
358  * passes will be made.
359  */
360 
361 /*
362  * Bulkfree callback info
363  */
364 static void hammer2_bulkfree_thread(void *arg __unused);
365 static void cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size);
366 static int h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo,
367 			hammer2_blockref_t *bref);
368 static int h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo);
369 static void h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
370 			hammer2_off_t data_off, hammer2_bmap_data_t *live,
371 			hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base);
372 
373 void
374 hammer2_bulkfree_init(hammer2_dev_t *hmp)
375 {
376 	hammer2_thr_create(&hmp->bfthr, NULL, hmp,
377 			   hmp->devrepname, -1, -1,
378 			   hammer2_bulkfree_thread);
379 }
380 
381 void
382 hammer2_bulkfree_uninit(hammer2_dev_t *hmp)
383 {
384 	hammer2_thr_delete(&hmp->bfthr);
385 }
386 
387 static void
388 hammer2_bulkfree_thread(void *arg)
389 {
390 	hammer2_thread_t *thr = arg;
391 	hammer2_ioc_bulkfree_t bfi;
392 	uint32_t flags;
393 
394 	for (;;) {
395 		hammer2_thr_wait_any(thr,
396 				     HAMMER2_THREAD_STOP |
397 				     HAMMER2_THREAD_FREEZE |
398 				     HAMMER2_THREAD_UNFREEZE |
399 				     HAMMER2_THREAD_REMASTER,
400 				     hz * 60);
401 
402 		flags = thr->flags;
403 		cpu_ccfence();
404 		if (flags & HAMMER2_THREAD_STOP)
405 			break;
406 		if (flags & HAMMER2_THREAD_FREEZE) {
407 			hammer2_thr_signal2(thr, HAMMER2_THREAD_FROZEN,
408 						 HAMMER2_THREAD_FREEZE);
409 			continue;
410 		}
411 		if (flags & HAMMER2_THREAD_UNFREEZE) {
412 			hammer2_thr_signal2(thr, 0,
413 						 HAMMER2_THREAD_FROZEN |
414 						 HAMMER2_THREAD_UNFREEZE);
415 			continue;
416 		}
417 		if (flags & HAMMER2_THREAD_FROZEN)
418 			continue;
419 		if (flags & HAMMER2_THREAD_REMASTER) {
420 			hammer2_thr_signal2(thr, 0, HAMMER2_THREAD_REMASTER);
421 			bzero(&bfi, sizeof(bfi));
422 			bfi.size = 8192 * 1024;
423 			/* hammer2_bulkfree_pass(thr->hmp, &bfi); */
424 		}
425 	}
426 	thr->td = NULL;
427 	hammer2_thr_signal(thr, HAMMER2_THREAD_STOPPED);
428 	/* structure can go invalid at this point */
429 }
430 
431 int
432 hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
433 		      hammer2_ioc_bulkfree_t *bfi)
434 {
435 	hammer2_bulkfree_info_t cbinfo;
436 	hammer2_chain_save_t *save;
437 	hammer2_off_t incr;
438 	size_t size;
439 	int error;
440 
441 	/*
442 	 * We have to clear the live dedup cache as it might have entries
443 	 * that are freeable as of now.  Any new entries in the dedup cache
444 	 * made after this point, even if they become freeable, will have
445 	 * previously been fully allocated and will be protected by the
446 	 * 2-stage bulkfree.
447 	 */
448 	hammer2_dedup_clear(hmp);
449 
450 	/*
451 	 * Setup for free pass using the buffer size specified by the
452 	 * hammer2 utility, 32K-aligned.
453 	 */
454 	bzero(&cbinfo, sizeof(cbinfo));
455 	size = (bfi->size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
456 	       ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
457 
458 	/*
459 	 * Cap at 1/4 physical memory (hammer2 utility will not normally
460 	 * ever specify a buffer this big, but leave the option available).
461 	 */
462 	if (size > kmem_lim_size() * 1024 * 1024 / 4) {
463 		size = kmem_lim_size() * 1024 * 1024 / 4;
464 		kprintf("hammer2: Warning: capping bulkfree buffer at %jdM\n",
465 			(intmax_t)size / (1024 * 1024));
466 	}
467 
468 #define HAMMER2_FREEMAP_SIZEDIV	\
469 	(HAMMER2_FREEMAP_LEVEL1_SIZE / HAMMER2_FREEMAP_LEVELN_PSIZE)
470 #define HAMMER2_FREEMAP_SIZEMASK	(HAMMER2_FREEMAP_SIZEDIV - 1)
471 
472 	/*
473 	 * Cap at the size needed to cover the whole volume to avoid
474 	 * making an unnecessarily large allocation.
475 	 */
476 	if (size > hmp->total_size / HAMMER2_FREEMAP_SIZEDIV) {
477 		size = (hmp->total_size + HAMMER2_FREEMAP_SIZEMASK) /
478 			HAMMER2_FREEMAP_SIZEDIV;
479 	}
480 
481 	/*
482 	 * Minimum bitmap buffer size, then align to a LEVELN_PSIZE (32K)
483 	 * boundary.
484 	 */
485 	if (size < 1024 * 1024)
486 		size = 1024 * 1024;
487 	size = (size + HAMMER2_FREEMAP_LEVELN_PSIZE - 1) &
488 	       ~(size_t)(HAMMER2_FREEMAP_LEVELN_PSIZE - 1);
489 
490 	cbinfo.hmp = hmp;
491 	cbinfo.bmap = kmem_alloc_swapbacked(&cbinfo.kp, size, VM_SUBSYS_HAMMER);
492 	cbinfo.dedup = kmalloc(sizeof(*cbinfo.dedup) * HAMMER2_DEDUP_HEUR_SIZE,
493 			       M_HAMMER2, M_WAITOK | M_ZERO);
494 
495 	kprintf("hammer2: bulkfree buf=%jdM\n",
496 		(intmax_t)size / (1024 * 1024));
497 
498 	/*
499 	 * Normalize start point to a 1GB boundary.  We operate on a
500 	 * 32KB leaf bitmap boundary which represents 1GB of storage.
501 	 */
502 	cbinfo.sbase = bfi->sbase;
503 	if (cbinfo.sbase > hmp->total_size)
504 		cbinfo.sbase = hmp->total_size;
505 	cbinfo.sbase &= ~HAMMER2_FREEMAP_LEVEL1_MASK;
506 	TAILQ_INIT(&cbinfo.list);
507 
508 	cbinfo.bulkfree_ticks = ticks;
509 
510 	/*
511 	 * Loop on a full meta-data scan as many times as required to
512 	 * get through all available storage.
513 	 */
514 	error = 0;
515 	while (cbinfo.sbase < hmp->total_size) {
516 		/*
517 		 * We have enough ram to represent (incr) bytes of storage.
518 		 * Each 32KB of ram represents 1GB of storage.
519 		 *
520 		 * We must also clean out our de-duplication heuristic for
521 		 * each (incr) bytes of storage, otherwise we wind up not
522 		 * scanning meta-data for later areas of storage because
523 		 * they had already been scanned in earlier areas of storage.
524 		 * Since the ranging is different, we have to restart
525 		 * the dedup heuristic too.
526 		 */
527 		int allmedia;
528 
529 		cbinfo_bmap_init(&cbinfo, size);
530 		bzero(cbinfo.dedup, sizeof(*cbinfo.dedup) *
531 				    HAMMER2_DEDUP_HEUR_SIZE);
532 		cbinfo.count_inodes_scanned = 0;
533 		cbinfo.count_dirents_scanned = 0;
534 		cbinfo.count_bytes_scanned = 0;
535 		cbinfo.count_chains_scanned = 0;
536 		cbinfo.count_chains_reported = 0;
537 
538 		incr = size / HAMMER2_FREEMAP_LEVELN_PSIZE *
539 		       HAMMER2_FREEMAP_LEVEL1_SIZE;
540 		if (hmp->total_size - cbinfo.sbase <= incr) {
541 			cbinfo.sstop = hmp->total_size;
542 			allmedia = 1;
543 		} else {
544 			cbinfo.sstop = cbinfo.sbase + incr;
545 			allmedia = 0;
546 		}
547 		kprintf("hammer2: pass %016jx-%016jx ",
548 			(intmax_t)cbinfo.sbase,
549 			(intmax_t)cbinfo.sstop);
550 		if (allmedia && cbinfo.sbase == 0)
551 			kprintf("(all media)\n");
552 		else if (allmedia)
553 			kprintf("(remaining media)\n");
554 		else
555 			kprintf("(%jdGB of media)\n",
556 				(intmax_t)incr / (1024L*1024*1024));
557 
558 		/*
559 		 * Scan topology for stuff inside this range.
560 		 *
561 		 * NOTE - By not using a transaction the operation can
562 		 *	  run concurrent with the frontend as well as
563 		 *	  with flushes.
564 		 *
565 		 *	  We cannot safely set a mtid without a transaction,
566 		 *	  and in fact we don't want to set one anyway.  We
567 		 *	  want the bulkfree to be passive and no interfere
568 		 *	  with crash recovery.
569 		 */
570 #undef HAMMER2_BULKFREE_TRANS	/* undef - don't use transaction */
571 #ifdef HAMMER2_BULKFREE_TRANS
572 		hammer2_trans_init(hmp->spmp, 0);
573 		cbinfo.mtid = hammer2_trans_sub(hmp->spmp);
574 #else
575 		cbinfo.mtid = 0;
576 #endif
577 		cbinfo.pri = 0;
578 		error |= hammer2_bulkfree_scan(vchain,
579 					       h2_bulkfree_callback, &cbinfo);
580 
581 		while ((save = TAILQ_FIRST(&cbinfo.list)) != NULL &&
582 		       (error & ~HAMMER2_ERROR_CHECK) == 0) {
583 			TAILQ_REMOVE(&cbinfo.list, save, entry);
584 			cbinfo.pri = 0;
585 			error |= hammer2_bulkfree_scan(save->chain,
586 						       h2_bulkfree_callback,
587 						       &cbinfo);
588 			hammer2_chain_drop(save->chain);
589 			kfree(save, M_HAMMER2);
590 		}
591 		while (save) {
592 			TAILQ_REMOVE(&cbinfo.list, save, entry);
593 			hammer2_chain_drop(save->chain);
594 			kfree(save, M_HAMMER2);
595 			save = TAILQ_FIRST(&cbinfo.list);
596 		}
597 
598 		/*
599 		 * If the complete scan succeeded we can synchronize our
600 		 * in-memory freemap against live storage.  If an abort
601 		 * occured we cannot safely synchronize our partially
602 		 * filled-out in-memory freemap.
603 		 *
604 		 * We still synchronize on CHECK failures.  That is, we still
605 		 * want bulkfree to operate even if the filesystem has defects.
606 		 */
607 		if (error & ~HAMMER2_ERROR_CHECK) {
608 			kprintf("bulkfree lastdrop %d %d error=0x%04x\n",
609 				vchain->refs, vchain->core.chain_count, error);
610 		} else {
611 			if (error & HAMMER2_ERROR_CHECK) {
612 				kprintf("bulkfree lastdrop %d %d "
613 					"(with check errors)\n",
614 					vchain->refs, vchain->core.chain_count);
615 			} else {
616 				kprintf("bulkfree lastdrop %d %d\n",
617 					vchain->refs, vchain->core.chain_count);
618 			}
619 
620 			error = h2_bulkfree_sync(&cbinfo);
621 
622 			hammer2_voldata_lock(hmp);
623 			hammer2_voldata_modify(hmp);
624 			hmp->voldata.allocator_free += cbinfo.adj_free;
625 			hammer2_voldata_unlock(hmp);
626 		}
627 
628 		/*
629 		 * Cleanup for next loop.
630 		 */
631 #ifdef HAMMER2_BULKFREE_TRANS
632 		hammer2_trans_done(hmp->spmp, 0);
633 #endif
634 		if (error & ~HAMMER2_ERROR_CHECK)
635 			break;
636 		cbinfo.sbase = cbinfo.sstop;
637 		cbinfo.adj_free = 0;
638 	}
639 	kmem_free_swapbacked(&cbinfo.kp);
640 	kfree(cbinfo.dedup, M_HAMMER2);
641 	cbinfo.dedup = NULL;
642 
643 	bfi->sstop = cbinfo.sbase;
644 
645 	incr = bfi->sstop / (hmp->total_size / 10000);
646 	if (incr > 10000)
647 		incr = 10000;
648 
649 	kprintf("bulkfree pass statistics (%d.%02d%% storage processed):\n",
650 		(int)incr / 100,
651 		(int)incr % 100);
652 
653 	if (error & ~HAMMER2_ERROR_CHECK) {
654 		kprintf("    bulkfree was aborted\n");
655 	} else {
656 		if (error & HAMMER2_ERROR_CHECK) {
657 			kprintf("    WARNING: bulkfree "
658 				"encountered CRC errors\n");
659 		}
660 		kprintf("    transition->free   %ld\n", cbinfo.count_10_00);
661 		kprintf("    transition->staged %ld\n", cbinfo.count_11_10);
662 		kprintf("    ERR(00)->allocated %ld\n", cbinfo.count_00_11);
663 		kprintf("    ERR(01)->allocated %ld\n", cbinfo.count_01_11);
664 		kprintf("    staged->allocated  %ld\n", cbinfo.count_10_11);
665 		kprintf("    ~4MB segs cleaned  %ld\n", cbinfo.count_l0cleans);
666 		kprintf("    linear adjusts     %ld\n",
667 			cbinfo.count_linadjusts);
668 		kprintf("    dedup factor       %ld\n",
669 			cbinfo.count_dedup_factor);
670 	}
671 
672 	return error;
673 }
674 
675 static void
676 cbinfo_bmap_init(hammer2_bulkfree_info_t *cbinfo, size_t size)
677 {
678 	hammer2_bmap_data_t *bmap = cbinfo->bmap;
679 	hammer2_key_t key = cbinfo->sbase;
680 	hammer2_key_t lokey;
681 	hammer2_key_t hikey;
682 
683 	lokey = (cbinfo->hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
684 		~HAMMER2_SEGMASK64;
685 	hikey = cbinfo->hmp->total_size & ~HAMMER2_SEGMASK64;
686 
687 	bzero(bmap, size);
688 	while (size) {
689 		bzero(bmap, sizeof(*bmap));
690 		if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX))
691 			lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX);
692 		if (lokey < H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64)
693 			lokey = H2FMZONEBASE(key) + HAMMER2_ZONE_SEG64;
694 		if (key < lokey || key >= hikey) {
695 			memset(bmap->bitmapq, -1,
696 			       sizeof(bmap->bitmapq));
697 			bmap->avail = 0;
698 			bmap->linear = HAMMER2_SEGSIZE;
699 		} else {
700 			bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
701 		}
702 		size -= sizeof(*bmap);
703 		key += HAMMER2_FREEMAP_LEVEL0_SIZE;
704 		++bmap;
705 	}
706 }
707 
708 static int
709 h2_bulkfree_callback(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref)
710 {
711 	hammer2_bmap_data_t *bmap;
712 	hammer2_off_t data_off;
713 	uint16_t class;
714 	size_t bytes;
715 	int radix;
716 
717 	/*
718 	 * Check for signal and allow yield to userland during scan.
719 	 */
720 	if (hammer2_signal_check(&cbinfo->save_time))
721 		return HAMMER2_ERROR_ABORTED;
722 
723 	/*
724 	 * Deal with kernel thread cpu or I/O hogging by limiting the
725 	 * number of chains scanned per second to hammer2_bulkfree_tps.
726 	 * Ignore leaf records (DIRENT and DATA), no per-record I/O is
727 	 * involved for those since we don't load their data.
728 	 */
729 	if (bref->type != HAMMER2_BREF_TYPE_DATA &&
730 	    bref->type != HAMMER2_BREF_TYPE_DIRENT) {
731 		++cbinfo->bulkfree_calls;
732 		if (cbinfo->bulkfree_calls > hammer2_bulkfree_tps) {
733 			int dticks = ticks - cbinfo->bulkfree_ticks;
734 			if (dticks < 0)
735 				dticks = 0;
736 			if (dticks < hz) {
737 				tsleep(&cbinfo->bulkfree_ticks, 0,
738 				       "h2bw", hz - dticks);
739 			}
740 			cbinfo->bulkfree_calls = 0;
741 			cbinfo->bulkfree_ticks = ticks;
742 		}
743 	}
744 
745 	/*
746 	 * Calculate the data offset and determine if it is within
747 	 * the current freemap range being gathered.
748 	 */
749 	data_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
750 	if (data_off < cbinfo->sbase || data_off >= cbinfo->sstop)
751 		return 0;
752 	if (data_off < cbinfo->hmp->voldata.allocator_beg)
753 		return 0;
754 	if (data_off >= cbinfo->hmp->total_size)
755 		return 0;
756 
757 	/*
758 	 * Calculate the information needed to generate the in-memory
759 	 * freemap record.
760 	 *
761 	 * Hammer2 does not allow allocations to cross the L1 (1GB) boundary,
762 	 * it's a problem if it does.  (Or L0 (4MB) for that matter).
763 	 */
764 	radix = (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
765 	KKASSERT(radix != 0);
766 	bytes = (size_t)1 << radix;
767 	class = (bref->type << 8) | HAMMER2_PBUFRADIX;
768 
769 	if (data_off + bytes > cbinfo->sstop) {
770 		kprintf("hammer2_bulkfree_scan: illegal 1GB boundary "
771 			"%016jx %016jx/%d\n",
772 			(intmax_t)bref->data_off,
773 			(intmax_t)bref->key,
774 			bref->keybits);
775 		bytes = cbinfo->sstop - data_off;	/* XXX */
776 	}
777 
778 	/*
779 	 * Convert to a storage offset relative to the beginning of the
780 	 * storage range we are collecting.  Then lookup the level0 bmap entry.
781 	 */
782 	data_off -= cbinfo->sbase;
783 	bmap = cbinfo->bmap + (data_off >> HAMMER2_FREEMAP_LEVEL0_RADIX);
784 
785 	/*
786 	 * Convert data_off to a bmap-relative value (~4MB storage range).
787 	 * Adjust linear, class, and avail.
788 	 *
789 	 * Hammer2 does not allow allocations to cross the L0 (4MB) boundary,
790 	 */
791 	data_off &= HAMMER2_FREEMAP_LEVEL0_MASK;
792 	if (data_off + bytes > HAMMER2_FREEMAP_LEVEL0_SIZE) {
793 		kprintf("hammer2_bulkfree_scan: illegal 4MB boundary "
794 			"%016jx %016jx/%d\n",
795 			(intmax_t)bref->data_off,
796 			(intmax_t)bref->key,
797 			bref->keybits);
798 		bytes = HAMMER2_FREEMAP_LEVEL0_SIZE - data_off;
799 	}
800 
801 	if (bmap->class == 0) {
802 		bmap->class = class;
803 		bmap->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
804 	}
805 
806 	/*
807 	 * NOTE: bmap->class does not have to match class.  Classification
808 	 *	 is relaxed when free space is low, so some mixing can occur.
809 	 */
810 #if 0
811 	/*
812 	 * XXX removed
813 	 */
814 	if (bmap->class != class) {
815 		kprintf("hammer2_bulkfree_scan: illegal mixed class "
816 			"%016jx %016jx/%d (%04x vs %04x)\n",
817 			(intmax_t)bref->data_off,
818 			(intmax_t)bref->key,
819 			bref->keybits,
820 			class, bmap->class);
821 	}
822 #endif
823 
824 	/*
825 	 * Just record the highest byte-granular offset for now.  Do not
826 	 * match against allocations which are in multiples of whole blocks.
827 	 *
828 	 * Make sure that any in-block linear offset at least covers the
829 	 * data range.  This can cause bmap->linear to become block-aligned.
830 	 */
831 	if (bytes & HAMMER2_FREEMAP_BLOCK_MASK) {
832 		if (bmap->linear < (int32_t)data_off + (int32_t)bytes)
833 			bmap->linear = (int32_t)data_off + (int32_t)bytes;
834 	} else if (bmap->linear >= (int32_t)data_off &&
835 		   bmap->linear < (int32_t)data_off + (int32_t)bytes) {
836 		bmap->linear = (int32_t)data_off + (int32_t)bytes;
837 	}
838 
839 	/*
840 	 * Adjust the hammer2_bitmap_t bitmap[HAMMER2_BMAP_ELEMENTS].
841 	 * 64-bit entries, 2 bits per entry, to code 11.
842 	 *
843 	 * NOTE: data_off mask to 524288, shift right by 14 (radix for 16384),
844 	 *	 and multiply shift amount by 2 for sets of 2 bits.
845 	 *
846 	 * NOTE: The allocation can be smaller than HAMMER2_FREEMAP_BLOCK_SIZE.
847 	 *	 also, data_off may not be FREEMAP_BLOCK_SIZE aligned.
848 	 */
849 	while (bytes > 0) {
850 		hammer2_bitmap_t bmask;
851 		int bindex;
852 
853 		bindex = (int)data_off >> (HAMMER2_FREEMAP_BLOCK_RADIX +
854 					   HAMMER2_BMAP_INDEX_RADIX);
855 		bmask = (hammer2_bitmap_t)3 <<
856 			((((int)data_off & HAMMER2_BMAP_INDEX_MASK) >>
857 			 HAMMER2_FREEMAP_BLOCK_RADIX) << 1);
858 
859 		/*
860 		 * NOTE! The (avail) calculation is bitmap-granular.  Multiple
861 		 *	 sub-granular records can wind up at the same bitmap
862 		 *	 position.
863 		 */
864 		if ((bmap->bitmapq[bindex] & bmask) == 0) {
865 			if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE) {
866 				bmap->avail -= HAMMER2_FREEMAP_BLOCK_SIZE;
867 			} else {
868 				bmap->avail -= bytes;
869 			}
870 			bmap->bitmapq[bindex] |= bmask;
871 		}
872 		data_off += HAMMER2_FREEMAP_BLOCK_SIZE;
873 		if (bytes < HAMMER2_FREEMAP_BLOCK_SIZE)
874 			bytes = 0;
875 		else
876 			bytes -= HAMMER2_FREEMAP_BLOCK_SIZE;
877 	}
878 	return 0;
879 }
880 
881 /*
882  * Synchronize the in-memory bitmap with the live freemap.  This is not a
883  * direct copy.  Instead the bitmaps must be compared:
884  *
885  *	In-memory	Live-freemap
886  *	   00		  11 -> 10	(do nothing if live modified)
887  *			  10 -> 00	(do nothing if live modified)
888  *	   11		  10 -> 11	handles race against live
889  *			  ** -> 11	nominally warn of corruption
890  *
891  * We must also fixup the hints in HAMMER2_BREF_TYPE_FREEMAP_LEAF.
892  */
893 static int
894 h2_bulkfree_sync(hammer2_bulkfree_info_t *cbinfo)
895 {
896 	hammer2_off_t data_off;
897 	hammer2_key_t key;
898 	hammer2_key_t key_dummy;
899 	hammer2_bmap_data_t *bmap;
900 	hammer2_bmap_data_t *live;
901 	hammer2_chain_t *live_parent;
902 	hammer2_chain_t *live_chain;
903 	int bmapindex;
904 	int error;
905 
906 	kprintf("hammer2_bulkfree - range ");
907 
908 	if (cbinfo->sbase < cbinfo->hmp->voldata.allocator_beg)
909 		kprintf("%016jx-",
910 			(intmax_t)cbinfo->hmp->voldata.allocator_beg);
911 	else
912 		kprintf("%016jx-",
913 			(intmax_t)cbinfo->sbase);
914 
915 	if (cbinfo->sstop > cbinfo->hmp->total_size)
916 		kprintf("%016jx\n",
917 			(intmax_t)cbinfo->hmp->total_size);
918 	else
919 		kprintf("%016jx\n",
920 			(intmax_t)cbinfo->sstop);
921 
922 	data_off = cbinfo->sbase;
923 	bmap = cbinfo->bmap;
924 
925 	live_parent = &cbinfo->hmp->fchain;
926 	hammer2_chain_ref(live_parent);
927 	hammer2_chain_lock(live_parent, HAMMER2_RESOLVE_ALWAYS);
928 	live_chain = NULL;
929 	error = 0;
930 
931 	/*
932 	 * Iterate each hammer2_bmap_data_t line (128 bytes) managing
933 	 * 4MB of storage.
934 	 */
935 	while (data_off < cbinfo->sstop) {
936 		/*
937 		 * The freemap is not used below allocator_beg or beyond
938 		 * total_size.
939 		 */
940 
941 		if (data_off < cbinfo->hmp->voldata.allocator_beg)
942 			goto next;
943 		if (data_off >= cbinfo->hmp->total_size)
944 			goto next;
945 
946 		/*
947 		 * Locate the freemap leaf on the live filesystem
948 		 */
949 		key = (data_off & ~HAMMER2_FREEMAP_LEVEL1_MASK);
950 
951 		if (live_chain == NULL || live_chain->bref.key != key) {
952 			if (live_chain) {
953 				hammer2_chain_unlock(live_chain);
954 				hammer2_chain_drop(live_chain);
955 			}
956 			live_chain = hammer2_chain_lookup(
957 					    &live_parent,
958 					    &key_dummy,
959 					    key,
960 					    key + HAMMER2_FREEMAP_LEVEL1_MASK,
961 					    &error,
962 					    HAMMER2_LOOKUP_ALWAYS);
963 			if (error) {
964 				kprintf("hammer2_bulkfree: freemap lookup "
965 					"error near %016jx, error %s\n",
966 					(intmax_t)data_off,
967 					hammer2_error_str(live_chain->error));
968 				break;
969 			}
970 		}
971 		if (live_chain == NULL) {
972 			/*
973 			 * XXX if we implement a full recovery mode we need
974 			 * to create/recreate missing freemap chains if our
975 			 * bmap has any allocated blocks.
976 			 */
977 			if (bmap->class &&
978 			    bmap->avail != HAMMER2_FREEMAP_LEVEL0_SIZE) {
979 				kprintf("hammer2_bulkfree: cannot locate "
980 					"live leaf for allocated data "
981 					"near %016jx\n",
982 					(intmax_t)data_off);
983 			}
984 			goto next;
985 		}
986 		if (live_chain->error) {
987 			kprintf("hammer2_bulkfree: unable to access freemap "
988 				"near %016jx, error %s\n",
989 				(intmax_t)data_off,
990 				hammer2_error_str(live_chain->error));
991 			hammer2_chain_unlock(live_chain);
992 			hammer2_chain_drop(live_chain);
993 			live_chain = NULL;
994 			goto next;
995 		}
996 
997 		bmapindex = (data_off & HAMMER2_FREEMAP_LEVEL1_MASK) >>
998 			    HAMMER2_FREEMAP_LEVEL0_RADIX;
999 		live = &live_chain->data->bmdata[bmapindex];
1000 
1001 		/*
1002 		 * Shortcut if the bitmaps match and the live linear
1003 		 * indicator is sane.  We can't do a perfect check of
1004 		 * live->linear because the only real requirement is that
1005 		 * if it is not block-aligned, that it not cover the space
1006 		 * within its current block which overlaps one of the data
1007 		 * ranges we scan.  We don't retain enough fine-grained
1008 		 * data in our scan to be able to set it exactly.
1009 		 *
1010 		 * TODO - we could shortcut this by testing that both
1011 		 * live->class and bmap->class are 0, and both avails are
1012 		 * set to HAMMER2_FREEMAP_LEVEL0_SIZE (4MB).
1013 		 */
1014 		if (bcmp(live->bitmapq, bmap->bitmapq,
1015 			 sizeof(bmap->bitmapq)) == 0 &&
1016 		    live->linear >= bmap->linear) {
1017 			goto next;
1018 		}
1019 		if (hammer2_debug & 1) {
1020 			kprintf("live %016jx %04d.%04x (avail=%d)\n",
1021 				data_off, bmapindex, live->class, live->avail);
1022 		}
1023 
1024 		hammer2_chain_modify(live_chain, cbinfo->mtid, 0, 0);
1025 		live_chain->bref.check.freemap.bigmask = -1;
1026 		cbinfo->hmp->freemap_relaxed = 0;	/* reset heuristic */
1027 		live = &live_chain->data->bmdata[bmapindex];
1028 
1029 		h2_bulkfree_sync_adjust(cbinfo, data_off, live, bmap,
1030 					live_chain->bref.key +
1031 					bmapindex *
1032 					HAMMER2_FREEMAP_LEVEL0_SIZE);
1033 next:
1034 		data_off += HAMMER2_FREEMAP_LEVEL0_SIZE;
1035 		++bmap;
1036 	}
1037 	if (live_chain) {
1038 		hammer2_chain_unlock(live_chain);
1039 		hammer2_chain_drop(live_chain);
1040 	}
1041 	if (live_parent) {
1042 		hammer2_chain_unlock(live_parent);
1043 		hammer2_chain_drop(live_parent);
1044 	}
1045 	return error;
1046 }
1047 
1048 /*
1049  * Merge the bulkfree bitmap against the existing bitmap.
1050  */
1051 static
1052 void
1053 h2_bulkfree_sync_adjust(hammer2_bulkfree_info_t *cbinfo,
1054 			hammer2_off_t data_off, hammer2_bmap_data_t *live,
1055 			hammer2_bmap_data_t *bmap, hammer2_key_t alloc_base)
1056 {
1057 	int bindex;
1058 	int scount;
1059 	hammer2_off_t tmp_off;
1060 	hammer2_bitmap_t lmask;
1061 	hammer2_bitmap_t mmask;
1062 
1063 	tmp_off = data_off;
1064 
1065 	for (bindex = 0; bindex < HAMMER2_BMAP_ELEMENTS; ++bindex) {
1066 		lmask = live->bitmapq[bindex];	/* live */
1067 		mmask = bmap->bitmapq[bindex];	/* snapshotted bulkfree */
1068 		if (lmask == mmask) {
1069 			tmp_off += HAMMER2_BMAP_INDEX_SIZE;
1070 			continue;
1071 		}
1072 
1073 		for (scount = 0;
1074 		     scount < HAMMER2_BMAP_BITS_PER_ELEMENT;
1075 		     scount += 2) {
1076 			if ((mmask & 3) == 0) {
1077 				/*
1078 				 * in-memory 00		live 11 -> 10
1079 				 *			live 10 -> 00
1080 				 *
1081 				 * Storage might be marked allocated or
1082 				 * staged and must be remarked staged or
1083 				 * free.
1084 				 */
1085 				switch (lmask & 3) {
1086 				case 0:	/* 00 */
1087 					break;
1088 				case 1:	/* 01 */
1089 					kprintf("hammer2_bulkfree: cannot "
1090 						"transition m=00/l=01\n");
1091 					break;
1092 				case 2:	/* 10 -> 00 */
1093 					live->bitmapq[bindex] &=
1094 					    ~((hammer2_bitmap_t)2 << scount);
1095 					live->avail +=
1096 						HAMMER2_FREEMAP_BLOCK_SIZE;
1097 					if (live->avail >
1098 					    HAMMER2_FREEMAP_LEVEL0_SIZE) {
1099 						live->avail =
1100 						    HAMMER2_FREEMAP_LEVEL0_SIZE;
1101 					}
1102 					cbinfo->adj_free +=
1103 						HAMMER2_FREEMAP_BLOCK_SIZE;
1104 					++cbinfo->count_10_00;
1105 					hammer2_io_dedup_assert(
1106 						cbinfo->hmp,
1107 						tmp_off |
1108 						HAMMER2_FREEMAP_BLOCK_RADIX,
1109 						HAMMER2_FREEMAP_BLOCK_SIZE);
1110 					break;
1111 				case 3:	/* 11 -> 10 */
1112 					live->bitmapq[bindex] &=
1113 					    ~((hammer2_bitmap_t)1 << scount);
1114 					++cbinfo->count_11_10;
1115 					hammer2_io_dedup_delete(
1116 						cbinfo->hmp,
1117 						HAMMER2_BREF_TYPE_DATA,
1118 						tmp_off |
1119 						HAMMER2_FREEMAP_BLOCK_RADIX,
1120 						HAMMER2_FREEMAP_BLOCK_SIZE);
1121 					break;
1122 				}
1123 			} else if ((mmask & 3) == 3) {
1124 				/*
1125 				 * in-memory 11		live 10 -> 11
1126 				 *			live ** -> 11
1127 				 *
1128 				 * Storage might be incorrectly marked free
1129 				 * or staged and must be remarked fully
1130 				 * allocated.
1131 				 */
1132 				switch (lmask & 3) {
1133 				case 0:	/* 00 */
1134 					++cbinfo->count_00_11;
1135 					cbinfo->adj_free -=
1136 						HAMMER2_FREEMAP_BLOCK_SIZE;
1137 					live->avail -=
1138 						HAMMER2_FREEMAP_BLOCK_SIZE;
1139 					if ((int32_t)live->avail < 0)
1140 						live->avail = 0;
1141 					break;
1142 				case 1:	/* 01 */
1143 					++cbinfo->count_01_11;
1144 					break;
1145 				case 2:	/* 10 -> 11 */
1146 					++cbinfo->count_10_11;
1147 					break;
1148 				case 3:	/* 11 */
1149 					break;
1150 				}
1151 				live->bitmapq[bindex] |=
1152 					((hammer2_bitmap_t)3 << scount);
1153 			}
1154 			mmask >>= 2;
1155 			lmask >>= 2;
1156 			tmp_off += HAMMER2_FREEMAP_BLOCK_SIZE;
1157 		}
1158 	}
1159 
1160 	/*
1161 	 * Determine if the live bitmap is completely free and reset its
1162 	 * fields if so.  Otherwise check to see if we can reduce the linear
1163 	 * offset.
1164 	 */
1165 	for (bindex = HAMMER2_BMAP_ELEMENTS - 1; bindex >= 0; --bindex) {
1166 		if (live->bitmapq[bindex] != 0)
1167 			break;
1168 	}
1169 	if (bindex < 0) {
1170 		/*
1171 		 * Completely empty, reset entire segment
1172 		 */
1173 #if 0
1174 		kprintf("hammer2: cleanseg %016jx.%04x (%d)\n",
1175 			alloc_base, live->class, live->avail);
1176 #endif
1177 		live->avail = HAMMER2_FREEMAP_LEVEL0_SIZE;
1178 		live->class = 0;
1179 		live->linear = 0;
1180 		++cbinfo->count_l0cleans;
1181 	} else if (bindex < 7) {
1182 		/*
1183 		 * Partially full, bitmapq[bindex] != 0.  Our bulkfree pass
1184 		 * does not record enough information to set live->linear
1185 		 * exactly.
1186 		 *
1187 		 * NOTE: Setting live->linear to a sub-block (16K) boundary
1188 		 *	 forces the live code to iterate to the next fully
1189 		 *	 free block.  It does NOT mean that all blocks above
1190 		 *	 live->linear are available.
1191 		 *
1192 		 *	 Setting live->linear to a fragmentary (less than
1193 		 *	 16K) boundary allows allocations to iterate within
1194 		 *	 that sub-block.
1195 		 */
1196 		if (live->linear < bmap->linear &&
1197 		    ((live->linear ^ bmap->linear) &
1198 		     ~HAMMER2_FREEMAP_BLOCK_MASK) == 0) {
1199 			/*
1200 			 * If greater than but still within the same
1201 			 * sub-block as live we can adjust linear upward.
1202 			 */
1203 			live->linear = bmap->linear;
1204 			++cbinfo->count_linadjusts;
1205 		} else {
1206 			/*
1207 			 * Otherwise adjust to the nearest higher or same
1208 			 * sub-block boundary.  The live system may have
1209 			 * bounced live->linear around so we cannot make any
1210 			 * assumptions with regards to available fragmentary
1211 			 * allocations.
1212 			 */
1213 			live->linear =
1214 				(bmap->linear + HAMMER2_FREEMAP_BLOCK_MASK) &
1215 				~HAMMER2_FREEMAP_BLOCK_MASK;
1216 			++cbinfo->count_linadjusts;
1217 		}
1218 	} else {
1219 		/*
1220 		 * Completely full, effectively disable the linear iterator
1221 		 */
1222 		live->linear = HAMMER2_SEGSIZE;
1223 	}
1224 
1225 #if 0
1226 	if (bmap->class) {
1227 		kprintf("%016jx %04d.%04x (avail=%7d) "
1228 			"%08x %08x %08x %08x %08x %08x %08x %08x\n",
1229 			(intmax_t)data_off,
1230 			(int)((data_off &
1231 			       HAMMER2_FREEMAP_LEVEL1_MASK) >>
1232 			      HAMMER2_FREEMAP_LEVEL0_RADIX),
1233 			bmap->class,
1234 			bmap->avail,
1235 			bmap->bitmap[0], bmap->bitmap[1],
1236 			bmap->bitmap[2], bmap->bitmap[3],
1237 			bmap->bitmap[4], bmap->bitmap[5],
1238 			bmap->bitmap[6], bmap->bitmap[7]);
1239 	}
1240 #endif
1241 }
1242 
1243 /*
1244  * BULKFREE DEDUP HEURISTIC
1245  *
1246  * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1247  *	    All fields must be loaded into locals and validated.
1248  */
1249 static
1250 int
1251 h2_bulkfree_test(hammer2_bulkfree_info_t *cbinfo, hammer2_blockref_t *bref,
1252 		 int pri, int saved_error)
1253 {
1254 	hammer2_dedup_t *dedup;
1255 	int best;
1256 	int n;
1257 	int i;
1258 
1259 	n = hammer2_icrc32(&bref->data_off, sizeof(bref->data_off));
1260 	dedup = cbinfo->dedup + (n & (HAMMER2_DEDUP_HEUR_MASK & ~7));
1261 
1262 	for (i = best = 0; i < 8; ++i) {
1263 		if (dedup[i].data_off == bref->data_off) {
1264 			if (dedup[i].ticks < pri)
1265 				dedup[i].ticks = pri;
1266 			if (pri == 1)
1267 				cbinfo->count_dedup_factor += dedup[i].ticks;
1268 			return (dedup[i].saved_error | HAMMER2_ERROR_EOF);
1269 		}
1270 		if (dedup[i].ticks < dedup[best].ticks)
1271 			best = i;
1272 	}
1273 	dedup[best].data_off = bref->data_off;
1274 	dedup[best].ticks = pri;
1275 	dedup[best].saved_error = saved_error;
1276 
1277 	return 0;
1278 }
1279