xref: /dflybsd-src/sbin/hammer2/cmd_debug.c (revision 513a5bc47eb6dbacacf9572911d8a1b2461e6679)
1 /*
2  * Copyright (c) 2011-2012 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  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "hammer2.h"
37 
38 #include <openssl/sha.h>
39 
40 #define GIG	(1024LL*1024*1024)
41 #define arysize(ary)	(sizeof(ary) / sizeof((ary)[0]))
42 
43 static int show_tab = 2;
44 
45 static void shell_msghandler(dmsg_msg_t *msg, int unmanaged);
46 static void shell_ttymsg(dmsg_iocom_t *iocom);
47 static void CountBlocks(hammer2_bmap_data_t *bmap, int value,
48 		hammer2_off_t *accum16, hammer2_off_t *accum64);
49 
50 /************************************************************************
51  *				    SHELL				*
52  ************************************************************************/
53 
54 int
55 cmd_shell(const char *hostname)
56 {
57 	dmsg_master_service_info_t *info;
58 	pthread_t thread;
59 	int fd;
60 
61 	fd = dmsg_connect(hostname);
62 	if (fd < 0)
63 		return 1;
64 
65 	info = malloc(sizeof(*info));
66 	bzero(info, sizeof(*info));
67 	info->fd = fd;
68 	info->detachme = 0;
69 	info->usrmsg_callback = shell_msghandler;
70 	info->altmsg_callback = shell_ttymsg;
71 	info->label = strdup("debug");
72 	pthread_create(&thread, NULL, dmsg_master_service, info);
73 	pthread_join(thread, NULL);
74 
75 	return 0;
76 }
77 
78 #if 0
79 int
80 cmd_shell(const char *hostname)
81 {
82 	struct dmsg_iocom iocom;
83 	dmsg_msg_t *msg;
84 	int fd;
85 
86 	/*
87 	 * Connect to the target
88 	 */
89 	fd = dmsg_connect(hostname);
90 	if (fd < 0)
91 		return 1;
92 
93 	/*
94 	 * Initialize the session and transmit an empty DMSG_DBG_SHELL
95 	 * to cause the remote end to generate a prompt.
96 	 */
97 	dmsg_iocom_init(&iocom, fd, 0,
98 			NULL,
99 			shell_rcvmsg,
100 			hammer2_shell_parse,
101 			shell_ttymsg);
102 	fcntl(0, F_SETFL, O_NONBLOCK);
103 	printf("debug: connected\n");
104 
105 	msg = dmsg_msg_alloc(&iocom.state0, 0, DMSG_DBG_SHELL, NULL, NULL);
106 	dmsg_msg_write(msg);
107 	dmsg_iocom_core(&iocom);
108 	fprintf(stderr, "debug: disconnected\n");
109 	close(fd);
110 	return 0;
111 }
112 #endif
113 
114 /*
115  * Debug session front-end
116  *
117  * Callback from dmsg_iocom_core() when messages might be present
118  * on the socket.
119  */
120 static
121 void
122 shell_msghandler(dmsg_msg_t *msg, int unmanaged)
123 {
124 	dmsg_msg_t *nmsg;
125 
126 	switch(msg->tcmd) {
127 #if 0
128 	case DMSG_LNK_ERROR:
129 	case DMSG_LNK_ERROR | DMSGF_REPLY:
130 		/*
131 		 * One-way non-transactional LNK_ERROR messages typically
132 		 * indicate a connection failure.  Error code 0 is used by
133 		 * the debug shell to indicate no more results from last cmd.
134 		 */
135 		if (msg->any.head.error) {
136 			fprintf(stderr, "Stream failure: %s\n",
137 				dmsg_msg_str(msg));
138 		} else {
139 			write(1, "debug> ", 7);
140 		}
141 		break;
142 	case DMSG_LNK_ERROR | DMSGF_DELETE:
143 		/* ignore termination of LNK_CONN */
144 		break;
145 #endif
146 	case DMSG_DBG_SHELL:
147 		/*
148 		 * We send the commands, not accept them.
149 		 * (one-way message, not transactional)
150 		 */
151 		if (unmanaged)
152 			dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
153 		break;
154 	case DMSG_DBG_SHELL | DMSGF_REPLY:
155 		/*
156 		 * A reply from the remote is data we copy to stdout.
157 		 * (one-way message, not transactional)
158 		 */
159 		if (msg->aux_size) {
160 			msg->aux_data[msg->aux_size - 1] = 0;
161 			write(1, msg->aux_data, strlen(msg->aux_data));
162 		}
163 		break;
164 #if 1
165 	case DMSG_LNK_CONN | DMSGF_CREATE:
166 		fprintf(stderr, "Debug Shell received LNK_CONN\n");
167 		nmsg = dmsg_msg_alloc(&msg->state->iocom->state0, 0,
168 				      DMSG_DBG_SHELL,
169 				      NULL, NULL);
170 		dmsg_msg_write(nmsg);
171 		dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
172 		break;
173 	case DMSG_LNK_CONN | DMSGF_DELETE:
174 		break;
175 #endif
176 	default:
177 		/*
178 		 * Ignore any unknown messages, Terminate any unknown
179 		 * transactions with an error.
180 		 */
181 		fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
182 		if (unmanaged) {
183 			if (msg->any.head.cmd & DMSGF_CREATE)
184 				dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
185 			if (msg->any.head.cmd & DMSGF_DELETE)
186 				dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
187 		}
188 		break;
189 	}
190 }
191 
192 /*
193  * Debug session front-end
194  */
195 static
196 void
197 shell_ttymsg(dmsg_iocom_t *iocom)
198 {
199 	dmsg_state_t *pstate;
200 	dmsg_msg_t *msg;
201 	char buf[256];
202 	char *cmd;
203 	size_t len;
204 
205 	if (fgets(buf, sizeof(buf), stdin) != NULL) {
206 		if (buf[0] == '@') {
207 			pstate = dmsg_findspan(strtok(buf + 1, " \t\n"));
208 			cmd = strtok(NULL, "\n");
209 		} else {
210 			pstate = &iocom->state0;
211 			cmd = strtok(buf, "\n");
212 		}
213 		if (cmd && pstate) {
214 			len = strlen(cmd) + 1;
215 			msg = dmsg_msg_alloc(pstate, len, DMSG_DBG_SHELL,
216 					     NULL, NULL);
217 			bcopy(cmd, msg->aux_data, len);
218 			dmsg_msg_write(msg);
219 		} else if (cmd) {
220 			fprintf(stderr, "@msgid not found\n");
221 		} else {
222 			/*
223 			 * This should cause the remote end to generate
224 			 * a debug> prompt (and thus shows that there is
225 			 * connectivity).
226 			 */
227 			msg = dmsg_msg_alloc(pstate, 0, DMSG_DBG_SHELL,
228 					     NULL, NULL);
229 			dmsg_msg_write(msg);
230 		}
231 	} else if (feof(stdin)) {
232 		/*
233 		 * Set EOF flag without setting any error code for normal
234 		 * EOF.
235 		 */
236 		iocom->flags |= DMSG_IOCOMF_EOF;
237 	} else {
238 		clearerr(stdin);
239 	}
240 }
241 
242 /*
243  * Debug session back-end (on remote side)
244  */
245 static void shell_span(dmsg_msg_t *msg, char *cmdbuf);
246 static void shell_ping(dmsg_msg_t *msg, char *cmdbuf);
247 
248 void
249 hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged)
250 {
251 	dmsg_iocom_t *iocom = msg->state->iocom;
252 	char *cmdbuf;
253 	char *cmdp;
254 	uint32_t cmd;
255 
256 	/*
257 	 * Filter on debug shell commands and ping responses only
258 	 */
259 	cmd = msg->any.head.cmd;
260 	if ((cmd & DMSGF_CMDSWMASK) == (DMSG_LNK_PING | DMSGF_REPLY)) {
261 		dmsg_printf(iocom, "ping reply\n");
262 		return;
263 	}
264 
265 	if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) {
266 		if (unmanaged)
267 			dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
268 		return;
269 	}
270 	if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) {
271 		if (unmanaged)
272 			dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
273 		return;
274 	}
275 
276 	/*
277 	 * Debug shell command
278 	 */
279 	cmdbuf = msg->aux_data;
280 	cmdp = strsep(&cmdbuf, " \t");
281 
282 	if (cmdp == NULL || *cmdp == 0) {
283 		;
284 	} else if (strcmp(cmdp, "ping") == 0) {
285 		shell_ping(msg, cmdbuf);
286 	} else if (strcmp(cmdp, "span") == 0) {
287 		shell_span(msg, cmdbuf);
288 	} else if (strcmp(cmdp, "tree") == 0) {
289 		dmsg_shell_tree(iocom, cmdbuf); /* dump spanning tree */
290 	} else if (strcmp(cmdp, "help") == 0 || strcmp(cmdp, "?") == 0) {
291 		dmsg_printf(iocom, "help            Command help\n");
292 		dmsg_printf(iocom, "span <host>     Span to target host\n");
293 		dmsg_printf(iocom, "tree            Dump spanning tree\n");
294 		dmsg_printf(iocom, "@span <cmd>     Issue via circuit\n");
295 	} else {
296 		dmsg_printf(iocom, "Unrecognized command: %s\n", cmdp);
297 	}
298 	dmsg_printf(iocom, "debug> ");
299 }
300 
301 static void
302 shell_ping(dmsg_msg_t *msg, char *cmdbuf __unused)
303 {
304 	dmsg_iocom_t *iocom = msg->state->iocom;
305 	dmsg_msg_t *m2;
306 
307 	dmsg_printf(iocom, "sending ping\n");
308 	m2 = dmsg_msg_alloc(msg->state, 0, DMSG_LNK_PING, NULL, NULL);
309 	dmsg_msg_write(m2);
310 }
311 
312 static void
313 shell_span(dmsg_msg_t *msg, char *cmdbuf)
314 {
315 	dmsg_iocom_t *iocom = msg->state->iocom;
316 	dmsg_master_service_info_t *info;
317 	const char *hostname = strsep(&cmdbuf, " \t");
318 	pthread_t thread;
319 	int fd;
320 
321 	/*
322 	 * Connect to the target
323 	 */
324 	if (hostname == NULL) {
325 		fd = -1;
326 	} else {
327 		fd = dmsg_connect(hostname);
328 	}
329 
330 	/*
331 	 * Start master service
332 	 */
333 	if (fd < 0) {
334 		dmsg_printf(iocom, "Connection to %s failed\n", hostname);
335 	} else {
336 		dmsg_printf(iocom, "Connected to %s\n", hostname);
337 
338 		info = malloc(sizeof(*info));
339 		bzero(info, sizeof(*info));
340 		info->fd = fd;
341 		info->detachme = 1;
342 		info->usrmsg_callback = hammer2_shell_parse;
343 		info->label = strdup("client");
344 
345 		pthread_create(&thread, NULL, dmsg_master_service, info);
346 		/*pthread_join(thread, &res);*/
347 	}
348 }
349 
350 /************************************************************************
351  *				DEBUGSPAN				*
352  ************************************************************************
353  *
354  * Connect to the target manually (not via the cluster list embedded in
355  * a hammer2 filesystem) and initiate the SPAN protocol.
356  */
357 int
358 cmd_debugspan(const char *hostname)
359 {
360 	pthread_t thread;
361 	int fd;
362 	void *res;
363 
364 	/*
365 	 * Connect to the target
366 	 */
367 	fd = dmsg_connect(hostname);
368 	if (fd < 0)
369 		return 1;
370 
371 	printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
372 	pthread_create(&thread, NULL,
373 		       dmsg_master_service, (void *)(intptr_t)fd);
374 	pthread_join(thread, &res);
375 	return(0);
376 }
377 
378 /************************************************************************
379  *				    SHOW				*
380  ************************************************************************/
381 
382 static void show_volhdr(hammer2_volume_data_t *voldata, int fd, int bi);
383 static void show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
384 			int bi, hammer2_blockref_t *bref, int norecurse);
385 static void tabprintf(int tab, const char *ctl, ...);
386 
387 static hammer2_off_t TotalAccum16[4]; /* includes TotalAccum64 */
388 static hammer2_off_t TotalAccum64[4];
389 static hammer2_off_t TotalUnavail;
390 static hammer2_off_t TotalFreemap;
391 
392 int
393 cmd_show(const char *devpath, int which)
394 {
395 	hammer2_blockref_t broot;
396 	hammer2_blockref_t best;
397 	hammer2_media_data_t media;
398 	hammer2_media_data_t best_media;
399 	int fd;
400 	int i;
401 	int best_i;
402 	char *env;
403 
404 	memset(TotalAccum16, 0, sizeof(TotalAccum16));
405 	memset(TotalAccum64, 0, sizeof(TotalAccum64));
406 	TotalUnavail = TotalFreemap = 0;
407 
408 	env = getenv("HAMMER2_SHOW_TAB");
409 	if (env != NULL) {
410 		show_tab = (int)strtol(env, NULL, 0);
411 		if (errno || show_tab < 0 || show_tab > 8)
412 			show_tab = 2;
413 	}
414 
415 	fd = open(devpath, O_RDONLY);
416 	if (fd < 0) {
417 		perror("open");
418 		return 1;
419 	}
420 
421 	/*
422 	 * Show the tree using the best volume header.
423 	 * -vvv will show the tree for all four volume headers.
424 	 */
425 	best_i = -1;
426 	bzero(&best, sizeof(best));
427 	bzero(&best_media, sizeof(best_media));
428 	for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
429 		bzero(&broot, sizeof(broot));
430 		broot.data_off = (i * HAMMER2_ZONE_BYTES64) | HAMMER2_PBUFRADIX;
431 		lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, SEEK_SET);
432 		if (read(fd, &media, HAMMER2_PBUFSIZE) ==
433 		    (ssize_t)HAMMER2_PBUFSIZE) {
434 			broot.mirror_tid = media.voldata.mirror_tid;
435 			if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
436 				best_i = i;
437 				best = broot;
438 				best_media = media;
439 			}
440 			printf("Volume header %d: mirror_tid=%016jx\n",
441 			       i, (intmax_t)broot.mirror_tid);
442 
443 			if (VerboseOpt >= 3) {
444 				switch(which) {
445 				case 0:
446 					broot.type = HAMMER2_BREF_TYPE_VOLUME;
447 					show_bref(&media.voldata, fd, 0,
448 						  i, &broot, 0);
449 					break;
450 				case 1:
451 					broot.type = HAMMER2_BREF_TYPE_FREEMAP;
452 					show_bref(&media.voldata, fd, 0,
453 						  i, &broot, 0);
454 					break;
455 				default:
456 					show_volhdr(&media.voldata, fd, i);
457 					break;
458 				}
459 				printf("\n");
460 			}
461 		}
462 	}
463 	if (VerboseOpt < 3) {
464 		switch(which) {
465 		case 0:
466 			best.type = HAMMER2_BREF_TYPE_VOLUME;
467 			show_bref(&best_media.voldata, fd, 0, best_i, &best, 0);
468 			break;
469 		case 1:
470 			best.type = HAMMER2_BREF_TYPE_FREEMAP;
471 			show_bref(&best_media.voldata, fd, 0, best_i, &best, 0);
472 			break;
473 		default:
474 			show_volhdr(&best_media.voldata, fd, best_i);
475 			break;
476 		}
477 	}
478 	close(fd);
479 
480 	if (which == 1 && VerboseOpt < 3) {
481 		printf("Total unallocated storage:   %6.3fGiB (%6.3fGiB in 64KB chunks)\n",
482 		       (double)TotalAccum16[0] / GIG,
483 		       (double)TotalAccum64[0] / GIG);
484 		printf("Total possibly free storage: %6.3fGiB (%6.3fGiB in 64KB chunks)\n",
485 		       (double)TotalAccum16[2] / GIG,
486 		       (double)TotalAccum64[2] / GIG);
487 		printf("Total allocated storage:     %6.3fGiB (%6.3fGiB in 64KB chunks)\n",
488 		       (double)TotalAccum16[3] / GIG,
489 		       (double)TotalAccum64[3] / GIG);
490 		printf("Total unavailable storage:   %6.3fGiB\n",
491 		       (double)TotalUnavail / GIG);
492 		printf("Total freemap storage:       %6.3fGiB\n",
493 		       (double)TotalFreemap / GIG);
494 	}
495 
496 	return 0;
497 }
498 
499 static void
500 show_volhdr(hammer2_volume_data_t *voldata, int fd, int bi)
501 {
502 	uint32_t status;
503 	uint32_t i;
504 	char *str;
505 	char *name;
506 
507 	printf("\nVolume header %d {\n", bi);
508 	printf("    magic          0x%016jx\n", (intmax_t)voldata->magic);
509 	printf("    boot_beg       0x%016jx\n", (intmax_t)voldata->boot_beg);
510 	printf("    boot_end       0x%016jx (%6.2fMB)\n",
511 	       (intmax_t)voldata->boot_end,
512 	       (double)(voldata->boot_end - voldata->boot_beg) /
513 	       (1024.0*1024.0));
514 	printf("    aux_beg        0x%016jx\n", (intmax_t)voldata->aux_beg);
515 	printf("    aux_end        0x%016jx (%6.2fMB)\n",
516 	       (intmax_t)voldata->aux_end,
517 	       (double)(voldata->aux_end - voldata->aux_beg) /
518 	       (1024.0*1024.0));
519 	printf("    volu_size      0x%016jx (%6.2fGiB)\n",
520 	       (intmax_t)voldata->volu_size,
521 	       (double)voldata->volu_size / GIG);
522 	printf("    version        %d\n", voldata->version);
523 	printf("    flags          0x%08x\n", voldata->flags);
524 	printf("    copyid         %d\n", voldata->copyid);
525 	printf("    freemap_vers   %d\n", voldata->freemap_version);
526 	printf("    peer_type      %d\n", voldata->peer_type);
527 
528 	str = NULL;
529 	hammer2_uuid_to_str(&voldata->fsid, &str);
530 	printf("    fsid           %s\n", str);
531 	free(str);
532 
533 	str = NULL;
534 	name = NULL;
535 	hammer2_uuid_to_str(&voldata->fstype, &str);
536 	printf("    fstype         %s\n", str);
537 	uuid_addr_lookup(&voldata->fstype, &name, &status);
538 	if (name == NULL)
539 		name = strdup("?");
540 	printf("                   (%s)\n", name);
541 	free(name);
542 	free(str);
543 
544 	printf("    allocator_size 0x%016jx (%6.2fGiB)\n",
545 	       voldata->allocator_size,
546 	       (double)voldata->allocator_size / GIG);
547 	printf("    allocator_free 0x%016jx (%6.2fGiB)\n",
548 	       voldata->allocator_free,
549 	       (double)voldata->allocator_free / GIG);
550 	printf("    allocator_beg  0x%016jx (%6.2fGiB)\n",
551 	       voldata->allocator_beg,
552 	       (double)voldata->allocator_beg / GIG);
553 
554 	printf("    mirror_tid     0x%016jx\n", voldata->mirror_tid);
555 	printf("    reserved0080   0x%016jx\n", voldata->reserved0080);
556 	printf("    reserved0088   0x%016jx\n", voldata->reserved0088);
557 	printf("    freemap_tid    0x%016jx\n", voldata->freemap_tid);
558 	for (i = 0; i < arysize(voldata->reserved00A0); ++i) {
559 		printf("    reserved00A0/%u 0x%016jx\n",
560 		       i, voldata->reserved00A0[0]);
561 	}
562 
563 	printf("    copyexists    ");
564 	for (i = 0; i < arysize(voldata->copyexists); ++i)
565 		printf(" 0x%02x", voldata->copyexists[i]);
566 	printf("\n");
567 
568 	/*
569 	 * NOTE: Index numbers and ICRC_SECTn definitions are not matched,
570 	 *	 the ICRC for sector 0 actually uses the last index, for
571 	 *	 example.
572 	 *
573 	 * NOTE: The whole voldata CRC does not have to match critically
574 	 *	 as certain sub-areas of the volume header have their own
575 	 *	 CRCs.
576 	 */
577 	printf("\n");
578 	for (i = 0; i < arysize(voldata->icrc_sects); ++i) {
579 		printf("    icrc_sects[%u]  ", i);
580 		switch(i) {
581 		case HAMMER2_VOL_ICRC_SECT0:
582 			printf("0x%08x/0x%08x",
583 			       hammer2_icrc32((char *)voldata +
584 					      HAMMER2_VOLUME_ICRC0_OFF,
585 					      HAMMER2_VOLUME_ICRC0_SIZE),
586 			       voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT0]);
587 			if (hammer2_icrc32((char *)voldata +
588 					   HAMMER2_VOLUME_ICRC0_OFF,
589 					   HAMMER2_VOLUME_ICRC0_SIZE) ==
590 			       voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT0]) {
591 				printf(" (OK)");
592 			} else {
593 				printf(" (FAILED)");
594 			}
595 			break;
596 		case HAMMER2_VOL_ICRC_SECT1:
597 			printf("0x%08x/0x%08x",
598 			       hammer2_icrc32((char *)voldata +
599 					      HAMMER2_VOLUME_ICRC1_OFF,
600 					      HAMMER2_VOLUME_ICRC1_SIZE),
601 			       voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT1]);
602 			if (hammer2_icrc32((char *)voldata +
603 					   HAMMER2_VOLUME_ICRC1_OFF,
604 					   HAMMER2_VOLUME_ICRC1_SIZE) ==
605 			       voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT1]) {
606 				printf(" (OK)");
607 			} else {
608 				printf(" (FAILED)");
609 			}
610 
611 			break;
612 		default:
613 			printf("0x%08x (reserved)", voldata->icrc_sects[i]);
614 			break;
615 		}
616 		printf("\n");
617 	}
618 	printf("    icrc_volhdr    0x%08x/0x%08x",
619 	       hammer2_icrc32((char *)voldata + HAMMER2_VOLUME_ICRCVH_OFF,
620 			      HAMMER2_VOLUME_ICRCVH_SIZE),
621 	       voldata->icrc_volheader);
622 	if (hammer2_icrc32((char *)voldata + HAMMER2_VOLUME_ICRCVH_OFF,
623 			   HAMMER2_VOLUME_ICRCVH_SIZE) ==
624 	    voldata->icrc_volheader) {
625 		printf(" (OK)\n");
626 	} else {
627 		printf(" (FAILED - not a critical error)\n");
628 	}
629 
630 	/*
631 	 * The super-root and freemap blocksets (not recursed)
632 	 */
633 	printf("\n");
634 	printf("    sroot_blockset {\n");
635 	for (i = 0; i < HAMMER2_SET_COUNT; ++i) {
636 		show_bref(voldata, fd, 16, i,
637 			  &voldata->sroot_blockset.blockref[i], 2);
638 	        printf("\n");
639 	}
640 	printf("    }\n");
641 
642 	printf("    freemap_blockset {\n");
643 	for (i = 0; i < HAMMER2_SET_COUNT; ++i) {
644 		show_bref(voldata, fd, 16, i,
645 			  &voldata->freemap_blockset.blockref[i], 2);
646 	        printf("\n");
647 	}
648 	printf("    }\n");
649 
650 	printf("}\n");
651 }
652 
653 static void
654 show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
655 	  int bi, hammer2_blockref_t *bref, int norecurse)
656 {
657 	hammer2_media_data_t media;
658 	hammer2_blockref_t *bscan;
659 	hammer2_off_t tmp;
660 	int i, bcount, namelen, failed, obrace;
661 	int type_pad;
662 	size_t bytes;
663 	const char *type_str;
664 	char *str = NULL;
665 	uint32_t cv;
666 	uint64_t cv64;
667 
668 	SHA256_CTX hash_ctx;
669 	union {
670 		uint8_t digest[SHA256_DIGEST_LENGTH];
671 		uint64_t digest64[SHA256_DIGEST_LENGTH/8];
672 	} u;
673 
674 	bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX);
675 	if (bytes)
676 		bytes = (size_t)1 << bytes;
677 	if (bytes) {
678 		hammer2_off_t io_off;
679 		hammer2_off_t io_base;
680 		size_t io_bytes;
681 		size_t boff;
682 
683 		io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
684 		io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
685 		boff = io_off - io_base;
686 
687 		io_bytes = HAMMER2_MINIOSIZE;
688 		while (io_bytes + boff < bytes)
689 			io_bytes <<= 1;
690 
691 		if (io_bytes > sizeof(media)) {
692 			printf("(bad block size %zd)\n", bytes);
693 			return;
694 		}
695 		if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
696 			lseek(fd, io_base, SEEK_SET);
697 			if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
698 				printf("(media read failed)\n");
699 				return;
700 			}
701 			if (boff)
702 				bcopy((char *)&media + boff, &media, bytes);
703 		}
704 	}
705 
706 	bscan = NULL;
707 	bcount = 0;
708 	namelen = 0;
709 	failed = 0;
710 	obrace = 1;
711 
712 	switch(bref->type) {
713 	case HAMMER2_BREF_TYPE_EMPTY:
714 		type_str = "empty";
715 		break;
716 	case HAMMER2_BREF_TYPE_DIRENT:
717 		type_str = "dirent";
718 		break;
719 	case HAMMER2_BREF_TYPE_INODE:
720 		type_str = "inode";
721 		break;
722 	case HAMMER2_BREF_TYPE_INDIRECT:
723 		type_str = "indblk";
724 		break;
725 	case HAMMER2_BREF_TYPE_DATA:
726 		type_str = "data";
727 		break;
728 	case HAMMER2_BREF_TYPE_VOLUME:
729 		type_str = "volume";
730 		break;
731 	case HAMMER2_BREF_TYPE_FREEMAP:
732 		type_str = "freemap";
733 		break;
734 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
735 		type_str = "fmapnode";
736 		break;
737 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
738 		type_str = "fbitmap";
739 		break;
740 	default:
741 		type_str = "unknown";
742 		break;
743 	}
744 	type_pad = 8 - strlen(type_str);
745 	if (type_pad < 0)
746 		type_pad = 0;
747 
748 	switch(bref->type) {
749 	case HAMMER2_BREF_TYPE_INODE:
750 		assert(bytes);
751 		if (!(media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) {
752 			bscan = &media.ipdata.u.blockset.blockref[0];
753 			bcount = HAMMER2_SET_COUNT;
754 		}
755 		break;
756 	case HAMMER2_BREF_TYPE_INDIRECT:
757 		assert(bytes);
758 		bscan = &media.npdata[0];
759 		bcount = bytes / sizeof(hammer2_blockref_t);
760 		break;
761 	case HAMMER2_BREF_TYPE_VOLUME:
762 		bscan = &media.voldata.sroot_blockset.blockref[0];
763 		bcount = HAMMER2_SET_COUNT;
764 		break;
765 	case HAMMER2_BREF_TYPE_FREEMAP:
766 		bscan = &media.voldata.freemap_blockset.blockref[0];
767 		bcount = HAMMER2_SET_COUNT;
768 		break;
769 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
770 		assert(bytes);
771 		bscan = &media.npdata[0];
772 		bcount = bytes / sizeof(hammer2_blockref_t);
773 		break;
774 	}
775 
776 	if (QuietOpt > 0) {
777 		tabprintf(tab,
778 			  "%s.%-3d %016jx %016jx/%-2d "
779 			  "mir=%016jx mod=%016jx leafcnt=%d ",
780 			  type_str, bi, (intmax_t)bref->data_off,
781 			  (intmax_t)bref->key, (intmax_t)bref->keybits,
782 			  (intmax_t)bref->mirror_tid,
783 			  (intmax_t)bref->modify_tid,
784 			  bref->leaf_count);
785 		tab += show_tab;
786 	} else {
787 		tabprintf(tab, "%s.%-3d%*.*s 0x%016jx 0x%016jx/%-2d ",
788 			  type_str, bi, type_pad, type_pad, "",
789 			  (intmax_t)bref->data_off,
790 			  (intmax_t)bref->key, (intmax_t)bref->keybits);
791 		/*if (norecurse > 1)*/ {
792 			printf("\n");
793 			tabprintf(tab + 13, "");
794 		}
795 		printf("mir=%016jx mod=%016jx lfcnt=%d ",
796 		       (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid,
797 		       bref->leaf_count);
798 		if (/*norecurse > 1 && */ (bcount || bref->flags ||
799 		    bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
800 		    bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)) {
801 			printf("\n");
802 			tabprintf(tab + 13, "");
803 		}
804 	}
805 
806 	if (bcount)
807 		printf("bcnt=%d ", bcount);
808 	if (bref->flags)
809 		printf("flags=%02x ", bref->flags);
810 	if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
811 	    bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
812 		printf("bigmask=%08x avail=%ld ",
813 			bref->check.freemap.bigmask, bref->check.freemap.avail);
814 	}
815 
816 	/*
817 	 * Check data integrity in verbose mode, otherwise we are just doing
818 	 * a quick meta-data scan.  Meta-data integrity is always checked.
819 	 * (Also see the check above that ensures the media data is loaded,
820 	 * otherwise there's no data to check!).
821 	 *
822 	 * WARNING! bref->check state may be used for other things when
823 	 *	    bref has no data (bytes == 0).
824 	 */
825 	if (bytes &&
826 	    (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1)) {
827 		if (!(QuietOpt > 0)) {
828 			/*if (norecurse > 1)*/ {
829 				printf("\n");
830 				tabprintf(tab + 13, "");
831 			}
832 		}
833 
834 		switch(HAMMER2_DEC_CHECK(bref->methods)) {
835 		case HAMMER2_CHECK_NONE:
836 			printf("meth=%02x ", bref->methods);
837 			break;
838 		case HAMMER2_CHECK_DISABLED:
839 			printf("meth=%02x ", bref->methods);
840 			break;
841 		case HAMMER2_CHECK_ISCSI32:
842 			cv = hammer2_icrc32(&media, bytes);
843 			if (bref->check.iscsi32.value != cv) {
844 				printf("(icrc %02x:%08x/%08x failed) ",
845 				       bref->methods,
846 				       bref->check.iscsi32.value,
847 				       cv);
848 				failed = 1;
849 			} else {
850 				printf("meth=%02x, iscsi32=%08x ",
851 				       bref->methods, cv);
852 			}
853 			break;
854 		case HAMMER2_CHECK_XXHASH64:
855 			cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED);
856 			if (bref->check.xxhash64.value != cv64) {
857 				printf("(xxhash64 %02x:%016jx/%016jx failed) ",
858 				       bref->methods,
859 				       bref->check.xxhash64.value,
860 				       cv64);
861 				failed = 1;
862 			} else {
863 				printf("meth=%02x, xxh=%016jx ",
864 				       bref->methods, cv64);
865 			}
866 			break;
867 		case HAMMER2_CHECK_SHA192:
868 			SHA256_Init(&hash_ctx);
869 			SHA256_Update(&hash_ctx, &media, bytes);
870 			SHA256_Final(u.digest, &hash_ctx);
871 			u.digest64[2] ^= u.digest64[3];
872 			if (memcmp(u.digest, bref->check.sha192.data,
873 			    sizeof(bref->check.sha192.data))) {
874 				printf("(sha192 failed) ");
875 				failed = 1;
876 			} else {
877 				printf("meth=%02x ", bref->methods);
878 			}
879 			break;
880 		case HAMMER2_CHECK_FREEMAP:
881 			cv = hammer2_icrc32(&media, bytes);
882 			if (bref->check.freemap.icrc32 != cv) {
883 				printf("(fcrc %02x:%08x/%08x failed) ",
884 					bref->methods,
885 					bref->check.freemap.icrc32,
886 					cv);
887 				failed = 1;
888 			} else {
889 				printf("meth=%02x, fcrc=%08x ",
890 					bref->methods, cv);
891 			}
892 			break;
893 		}
894 	}
895 
896 	tab += show_tab;
897 
898 	if (QuietOpt > 0) {
899 		obrace = 0;
900 		printf("\n");
901 		goto skip_data;
902 	}
903 
904 	switch(bref->type) {
905 	case HAMMER2_BREF_TYPE_EMPTY:
906 		obrace = 0;
907 		break;
908 	case HAMMER2_BREF_TYPE_DIRENT:
909 		printf("{\n");
910 		if (bref->embed.dirent.namlen <= sizeof(bref->check.buf)) {
911 			tabprintf(tab, "filename \"%*.*s\"\n",
912 				bref->embed.dirent.namlen,
913 				bref->embed.dirent.namlen,
914 				bref->check.buf);
915 		} else {
916 			tabprintf(tab, "filename \"%*.*s\"\n",
917 				bref->embed.dirent.namlen,
918 				bref->embed.dirent.namlen,
919 				media.buf);
920 		}
921 		tabprintf(tab, "inum 0x%016jx\n",
922 			  (uintmax_t)bref->embed.dirent.inum);
923 		tabprintf(tab, "type     %s\n",
924 			  hammer2_iptype_to_str(bref->embed.dirent.type));
925 		break;
926 	case HAMMER2_BREF_TYPE_INODE:
927 		printf("{\n");
928 		namelen = media.ipdata.meta.name_len;
929 		if (namelen > HAMMER2_INODE_MAXNAME)
930 			namelen = 0;
931 		tabprintf(tab, "filename \"%*.*s\"\n",
932 			  namelen, namelen, media.ipdata.filename);
933 		tabprintf(tab, "version  %d\n", media.ipdata.meta.version);
934 		tabprintf(tab, "pfs_st   %d\n", media.ipdata.meta.pfs_subtype);
935 		tabprintf(tab, "uflags   0x%08x\n",
936 			  media.ipdata.meta.uflags);
937 		if (media.ipdata.meta.rmajor || media.ipdata.meta.rminor) {
938 			tabprintf(tab, "rmajor   %d\n",
939 				  media.ipdata.meta.rmajor);
940 			tabprintf(tab, "rminor   %d\n",
941 				  media.ipdata.meta.rminor);
942 		}
943 		tabprintf(tab, "ctime    %s\n",
944 			  hammer2_time64_to_str(media.ipdata.meta.ctime, &str));
945 		tabprintf(tab, "mtime    %s\n",
946 			  hammer2_time64_to_str(media.ipdata.meta.mtime, &str));
947 		tabprintf(tab, "atime    %s\n",
948 			  hammer2_time64_to_str(media.ipdata.meta.atime, &str));
949 		tabprintf(tab, "btime    %s\n",
950 			  hammer2_time64_to_str(media.ipdata.meta.btime, &str));
951 		tabprintf(tab, "uid      %s\n",
952 			  hammer2_uuid_to_str(&media.ipdata.meta.uid, &str));
953 		tabprintf(tab, "gid      %s\n",
954 			  hammer2_uuid_to_str(&media.ipdata.meta.gid, &str));
955 		tabprintf(tab, "type     %s\n",
956 			  hammer2_iptype_to_str(media.ipdata.meta.type));
957 		tabprintf(tab, "opflgs   0x%02x\n",
958 			  media.ipdata.meta.op_flags);
959 		tabprintf(tab, "capflgs  0x%04x\n",
960 			  media.ipdata.meta.cap_flags);
961 		tabprintf(tab, "mode     %-7o\n",
962 			  media.ipdata.meta.mode);
963 		tabprintf(tab, "inum     0x%016jx\n",
964 			  media.ipdata.meta.inum);
965 		tabprintf(tab, "size     %ju ",
966 			  (uintmax_t)media.ipdata.meta.size);
967 		if (media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA &&
968 		    media.ipdata.meta.size <= HAMMER2_EMBEDDED_BYTES)
969 			printf("(embedded data)\n");
970 		else
971 			printf("\n");
972 		tabprintf(tab, "nlinks   %ju\n",
973 			  (uintmax_t)media.ipdata.meta.nlinks);
974 		tabprintf(tab, "iparent  0x%016jx\n",
975 			  (uintmax_t)media.ipdata.meta.iparent);
976 		tabprintf(tab, "name_key 0x%016jx\n",
977 			  (uintmax_t)media.ipdata.meta.name_key);
978 		tabprintf(tab, "name_len %u\n",
979 			  media.ipdata.meta.name_len);
980 		tabprintf(tab, "ncopies  %u\n",
981 			  media.ipdata.meta.ncopies);
982 		tabprintf(tab, "compalg  %u\n",
983 			  media.ipdata.meta.comp_algo);
984 		tabprintf(tab, "target_t %u\n",
985 			  media.ipdata.meta.target_type);
986 		tabprintf(tab, "checkalg %u\n",
987 			  media.ipdata.meta.check_algo);
988 		if ((media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) ||
989 		    media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) {
990 			tabprintf(tab, "pfs_nmas %u\n",
991 				  media.ipdata.meta.pfs_nmasters);
992 			tabprintf(tab, "pfs_type %u (%s)\n",
993 				  media.ipdata.meta.pfs_type,
994 				  hammer2_pfstype_to_str(media.ipdata.meta.pfs_type));
995 			tabprintf(tab, "pfs_inum 0x%016jx\n",
996 				  (uintmax_t)media.ipdata.meta.pfs_inum);
997 			tabprintf(tab, "pfs_clid %s\n",
998 				  hammer2_uuid_to_str(&media.ipdata.meta.pfs_clid,
999 						      &str));
1000 			tabprintf(tab, "pfs_fsid %s\n",
1001 				  hammer2_uuid_to_str(&media.ipdata.meta.pfs_fsid,
1002 						      &str));
1003 			tabprintf(tab, "pfs_lsnap_tid 0x%016jx\n",
1004 				  (uintmax_t)media.ipdata.meta.pfs_lsnap_tid);
1005 		}
1006 		tabprintf(tab, "data_quota  %ju\n",
1007 			  (uintmax_t)media.ipdata.meta.data_quota);
1008 		tabprintf(tab, "data_count  %ju\n",
1009 			  (uintmax_t)bref->embed.stats.data_count);
1010 		tabprintf(tab, "inode_quota %ju\n",
1011 			  (uintmax_t)media.ipdata.meta.inode_quota);
1012 		tabprintf(tab, "inode_count %ju\n",
1013 			  (uintmax_t)bref->embed.stats.inode_count);
1014 		break;
1015 	case HAMMER2_BREF_TYPE_INDIRECT:
1016 		printf("{\n");
1017 		break;
1018 	case HAMMER2_BREF_TYPE_DATA:
1019 		printf("\n");
1020 		obrace = 0;
1021 		break;
1022 	case HAMMER2_BREF_TYPE_VOLUME:
1023 		printf("mirror_tid=%016jx freemap_tid=%016jx ",
1024 			media.voldata.mirror_tid,
1025 			media.voldata.freemap_tid);
1026 		printf("{\n");
1027 		break;
1028 	case HAMMER2_BREF_TYPE_FREEMAP:
1029 		printf("mirror_tid=%016jx freemap_tid=%016jx ",
1030 			media.voldata.mirror_tid,
1031 			media.voldata.freemap_tid);
1032 		printf("{\n");
1033 		break;
1034 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1035 		printf("{\n");
1036 		tmp = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
1037 		tmp &= HAMMER2_SEGMASK;
1038 		tmp /= HAMMER2_PBUFSIZE;
1039 		assert(tmp >= HAMMER2_ZONE_FREEMAP_00);
1040 		assert(tmp < HAMMER2_ZONE_FREEMAP_END);
1041 		tmp -= HAMMER2_ZONE_FREEMAP_00;
1042 		tmp /= HAMMER2_ZONE_FREEMAP_INC;
1043 		tabprintf(tab, "rotation=%d\n", (int)tmp);
1044 
1045 		for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
1046 			hammer2_off_t data_off = bref->key +
1047 				i * HAMMER2_FREEMAP_LEVEL0_SIZE;
1048 #if HAMMER2_BMAP_ELEMENTS != 8
1049 #error "cmd_debug.c: HAMMER2_BMAP_ELEMENTS expected to be 8"
1050 #endif
1051 			tabprintf(tab + 4, "%016jx %04d.%04x (avail=%7d) "
1052 				  "%016jx %016jx %016jx %016jx "
1053 				  "%016jx %016jx %016jx %016jx\n",
1054 				  data_off, i, media.bmdata[i].class,
1055 				  media.bmdata[i].avail,
1056 				  media.bmdata[i].bitmapq[0],
1057 				  media.bmdata[i].bitmapq[1],
1058 				  media.bmdata[i].bitmapq[2],
1059 				  media.bmdata[i].bitmapq[3],
1060 				  media.bmdata[i].bitmapq[4],
1061 				  media.bmdata[i].bitmapq[5],
1062 				  media.bmdata[i].bitmapq[6],
1063 				  media.bmdata[i].bitmapq[7]);
1064 		}
1065 		tabprintf(tab, "}\n");
1066 		break;
1067 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1068 		printf("{\n");
1069 		tmp = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
1070 		tmp &= HAMMER2_SEGMASK;
1071 		tmp /= HAMMER2_PBUFSIZE;
1072 		assert(tmp >= HAMMER2_ZONE_FREEMAP_00);
1073 		assert(tmp < HAMMER2_ZONE_FREEMAP_END);
1074 		tmp -= HAMMER2_ZONE_FREEMAP_00;
1075 		tmp /= HAMMER2_ZONE_FREEMAP_INC;
1076 		tabprintf(tab, "rotation=%d\n", (int)tmp);
1077 		break;
1078 	default:
1079 		printf("\n");
1080 		obrace = 0;
1081 		break;
1082 	}
1083 	if (str)
1084 		free(str);
1085 
1086 skip_data:
1087 	/*
1088 	 * Update statistics.
1089 	 */
1090 	switch(bref->type) {
1091 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1092 		for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
1093 			hammer2_off_t data_off = bref->key +
1094 				i * HAMMER2_FREEMAP_LEVEL0_SIZE;
1095 			if (data_off >= voldata->aux_end &&
1096 			    data_off < voldata->volu_size) {
1097 				int j;
1098 				for (j = 0; j < 4; ++j)
1099 					CountBlocks(&media.bmdata[i], j,
1100 						    &TotalAccum16[j],
1101 						    &TotalAccum64[j]);
1102 			} else
1103 				TotalUnavail += HAMMER2_FREEMAP_LEVEL0_SIZE;
1104 		}
1105 		TotalFreemap += HAMMER2_FREEMAP_LEVEL1_SIZE;
1106 		break;
1107 	default:
1108 		break;
1109 	}
1110 
1111 	/*
1112 	 * Recurse if norecurse == 0.  If the CRC failed, pass norecurse = 1.
1113 	 * That is, if an indirect or inode fails we still try to list its
1114 	 * direct children to help with debugging, but go no further than
1115 	 * that because they are probably garbage.
1116 	 */
1117 	for (i = 0; norecurse == 0 && i < bcount; ++i) {
1118 		if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
1119 			show_bref(voldata, fd, tab, i, &bscan[i], failed);
1120 		}
1121 	}
1122 	tab -= show_tab;
1123 	if (obrace) {
1124 		if (bref->type == HAMMER2_BREF_TYPE_INODE)
1125 			tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
1126 				  type_str, bi, namelen, namelen,
1127 				  media.ipdata.filename);
1128 		else
1129 			tabprintf(tab, "} (%s.%d)\n", type_str, bi);
1130 	}
1131 }
1132 
1133 static
1134 void
1135 CountBlocks(hammer2_bmap_data_t *bmap, int value,
1136 	    hammer2_off_t *accum16, hammer2_off_t *accum64)
1137 {
1138 	int i, j, bits;
1139 	hammer2_bitmap_t value16, value64;
1140 
1141 	bits = (int)sizeof(hammer2_bitmap_t) * 8;
1142 	assert(bits == 64);
1143 
1144 	value16 = value;
1145 	assert(value16 < 4);
1146 	value64 = (value16 << 6) | (value16 << 4) | (value16 << 2) | value16;
1147 	assert(value64 < 256);
1148 
1149 	for (i = 0; i < HAMMER2_BMAP_ELEMENTS; ++i) {
1150 		hammer2_bitmap_t bm = bmap->bitmapq[i];
1151 		hammer2_bitmap_t bm_save = bm;
1152 		hammer2_bitmap_t mask;
1153 
1154 		mask = 0x03; /* 2 bits per 16KB */
1155 		for (j = 0; j < bits; j += 2) {
1156 			if ((bm & mask) == value16)
1157 				*accum16 += 16384;
1158 			bm >>= 2;
1159 		}
1160 
1161 		bm = bm_save;
1162 		mask = 0xFF; /* 8 bits per 64KB chunk */
1163 		for (j = 0; j < bits; j += 8) {
1164 			if ((bm & mask) == value64)
1165 				*accum64 += 65536;
1166 			bm >>= 8;
1167 		}
1168 	}
1169 }
1170 
1171 int
1172 cmd_hash(int ac, const char **av)
1173 {
1174 	int i;
1175 
1176 	for (i = 0; i < ac; ++i) {
1177 		printf("%016jx %s\n",
1178 		       dirhash((const unsigned char*)av[i], strlen(av[i])),
1179 		       av[i]);
1180 	}
1181 	return(0);
1182 }
1183 
1184 int
1185 cmd_dhash(int ac, const char **av)
1186 {
1187 	char buf[1024];		/* 1K extended directory record */
1188 	uint64_t hash;
1189 	int i;
1190 
1191 	for (i = 0; i < ac; ++i) {
1192 		bzero(buf, sizeof(buf));
1193 		snprintf(buf, sizeof(buf), "%s", av[i]);
1194 		hash = XXH64(buf, sizeof(buf), XXH_HAMMER2_SEED);
1195 		printf("%016jx %s\n", hash, av[i]);
1196 	}
1197 	return(0);
1198 }
1199 
1200 int
1201 cmd_dumpchain(const char *path, u_int flags)
1202 {
1203 	int dummy = (int)flags;
1204 	int fd;
1205 
1206 	fd = open(path, O_RDONLY);
1207 	if (fd >= 0) {
1208 		ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
1209 		close(fd);
1210 	} else {
1211 		fprintf(stderr, "unable to open %s\n", path);
1212 	}
1213 	return 0;
1214 }
1215 
1216 
1217 static
1218 void
1219 tabprintf(int tab, const char *ctl, ...)
1220 {
1221 	va_list va;
1222 
1223 	printf("%*.*s", tab, tab, "");
1224 	va_start(va, ctl);
1225 	vprintf(ctl, va);
1226 	va_end(va);
1227 }
1228