xref: /dragonfly/sbin/hammer2/cmd_debug.c (revision a31d3627)
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 
42 static int show_tab = 2;
43 static int show_depth = -1;
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 	env = getenv("HAMMER2_SHOW_DEPTH");
415 	if (env != NULL) {
416 		show_depth = (int)strtol(env, NULL, 0);
417 		if (errno || show_depth < 0)
418 			show_depth = -1;
419 	}
420 
421 	fd = open(devpath, O_RDONLY);
422 	if (fd < 0) {
423 		perror("open");
424 		return 1;
425 	}
426 
427 	/*
428 	 * Show the tree using the best volume header.
429 	 * -vvv will show the tree for all four volume headers.
430 	 */
431 	best_i = -1;
432 	bzero(&best, sizeof(best));
433 	bzero(&best_media, sizeof(best_media));
434 	for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
435 		bzero(&broot, sizeof(broot));
436 		broot.data_off = (i * HAMMER2_ZONE_BYTES64) | HAMMER2_PBUFRADIX;
437 		lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, SEEK_SET);
438 		if (read(fd, &media, HAMMER2_PBUFSIZE) ==
439 		    (ssize_t)HAMMER2_PBUFSIZE) {
440 			broot.mirror_tid = media.voldata.mirror_tid;
441 			if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
442 				best_i = i;
443 				best = broot;
444 				best_media = media;
445 			}
446 			printf("Volume header %d: mirror_tid=%016jx\n",
447 			       i, (intmax_t)broot.mirror_tid);
448 
449 			if (VerboseOpt >= 3) {
450 				switch(which) {
451 				case 0:
452 					broot.type = HAMMER2_BREF_TYPE_VOLUME;
453 					show_bref(&media.voldata, fd, 0,
454 						  i, &broot, 0);
455 					break;
456 				case 1:
457 					broot.type = HAMMER2_BREF_TYPE_FREEMAP;
458 					show_bref(&media.voldata, fd, 0,
459 						  i, &broot, 0);
460 					break;
461 				default:
462 					show_volhdr(&media.voldata, fd, i);
463 					break;
464 				}
465 				if (i != HAMMER2_NUM_VOLHDRS - 1)
466 					printf("\n");
467 			}
468 		}
469 	}
470 	if (VerboseOpt < 3) {
471 		switch(which) {
472 		case 0:
473 			best.type = HAMMER2_BREF_TYPE_VOLUME;
474 			show_bref(&best_media.voldata, fd, 0, best_i, &best, 0);
475 			break;
476 		case 1:
477 			best.type = HAMMER2_BREF_TYPE_FREEMAP;
478 			show_bref(&best_media.voldata, fd, 0, best_i, &best, 0);
479 			break;
480 		default:
481 			show_volhdr(&best_media.voldata, fd, best_i);
482 			break;
483 		}
484 	}
485 	close(fd);
486 
487 	if (which == 1 && VerboseOpt < 3) {
488 		printf("Total unallocated storage:   %6.3fGiB (%6.3fGiB in 64KB chunks)\n",
489 		       (double)TotalAccum16[0] / GIG,
490 		       (double)TotalAccum64[0] / GIG);
491 		printf("Total possibly free storage: %6.3fGiB (%6.3fGiB in 64KB chunks)\n",
492 		       (double)TotalAccum16[2] / GIG,
493 		       (double)TotalAccum64[2] / GIG);
494 		printf("Total allocated storage:     %6.3fGiB (%6.3fGiB in 64KB chunks)\n",
495 		       (double)TotalAccum16[3] / GIG,
496 		       (double)TotalAccum64[3] / GIG);
497 		printf("Total unavailable storage:   %6.3fGiB\n",
498 		       (double)TotalUnavail / GIG);
499 		printf("Total freemap storage:       %6.3fGiB\n",
500 		       (double)TotalFreemap / GIG);
501 	}
502 
503 	return 0;
504 }
505 
506 static void
507 show_volhdr(hammer2_volume_data_t *voldata, int fd, int bi)
508 {
509 	uint32_t status;
510 	uint32_t i;
511 	char *str;
512 	char *name;
513 
514 	printf("\nVolume header %d {\n", bi);
515 	printf("    magic          0x%016jx\n", (intmax_t)voldata->magic);
516 	printf("    boot_beg       0x%016jx\n", (intmax_t)voldata->boot_beg);
517 	printf("    boot_end       0x%016jx (%6.2fMB)\n",
518 	       (intmax_t)voldata->boot_end,
519 	       (double)(voldata->boot_end - voldata->boot_beg) /
520 	       (1024.0*1024.0));
521 	printf("    aux_beg        0x%016jx\n", (intmax_t)voldata->aux_beg);
522 	printf("    aux_end        0x%016jx (%6.2fMB)\n",
523 	       (intmax_t)voldata->aux_end,
524 	       (double)(voldata->aux_end - voldata->aux_beg) /
525 	       (1024.0*1024.0));
526 	printf("    volu_size      0x%016jx (%6.2fGiB)\n",
527 	       (intmax_t)voldata->volu_size,
528 	       (double)voldata->volu_size / GIG);
529 	printf("    version        %d\n", voldata->version);
530 	printf("    flags          0x%08x\n", voldata->flags);
531 	printf("    copyid         %d\n", voldata->copyid);
532 	printf("    freemap_vers   %d\n", voldata->freemap_version);
533 	printf("    peer_type      %d\n", voldata->peer_type);
534 
535 	str = NULL;
536 	hammer2_uuid_to_str(&voldata->fsid, &str);
537 	printf("    fsid           %s\n", str);
538 	free(str);
539 
540 	str = NULL;
541 	name = NULL;
542 	hammer2_uuid_to_str(&voldata->fstype, &str);
543 	printf("    fstype         %s\n", str);
544 	uuid_addr_lookup(&voldata->fstype, &name, &status);
545 	if (name == NULL)
546 		name = strdup("?");
547 	printf("                   (%s)\n", name);
548 	free(name);
549 	free(str);
550 
551 	printf("    allocator_size 0x%016jx (%6.2fGiB)\n",
552 	       voldata->allocator_size,
553 	       (double)voldata->allocator_size / GIG);
554 	printf("    allocator_free 0x%016jx (%6.2fGiB)\n",
555 	       voldata->allocator_free,
556 	       (double)voldata->allocator_free / GIG);
557 	printf("    allocator_beg  0x%016jx (%6.2fGiB)\n",
558 	       voldata->allocator_beg,
559 	       (double)voldata->allocator_beg / GIG);
560 
561 	printf("    mirror_tid     0x%016jx\n", voldata->mirror_tid);
562 	printf("    reserved0080   0x%016jx\n", voldata->reserved0080);
563 	printf("    reserved0088   0x%016jx\n", voldata->reserved0088);
564 	printf("    freemap_tid    0x%016jx\n", voldata->freemap_tid);
565 	for (i = 0; i < nitems(voldata->reserved00A0); ++i) {
566 		printf("    reserved00A0/%u 0x%016jx\n",
567 		       i, voldata->reserved00A0[0]);
568 	}
569 
570 	printf("    copyexists    ");
571 	for (i = 0; i < nitems(voldata->copyexists); ++i)
572 		printf(" 0x%02x", voldata->copyexists[i]);
573 	printf("\n");
574 
575 	/*
576 	 * NOTE: Index numbers and ICRC_SECTn definitions are not matched,
577 	 *	 the ICRC for sector 0 actually uses the last index, for
578 	 *	 example.
579 	 *
580 	 * NOTE: The whole voldata CRC does not have to match critically
581 	 *	 as certain sub-areas of the volume header have their own
582 	 *	 CRCs.
583 	 */
584 	printf("\n");
585 	for (i = 0; i < nitems(voldata->icrc_sects); ++i) {
586 		printf("    icrc_sects[%u]  ", i);
587 		switch(i) {
588 		case HAMMER2_VOL_ICRC_SECT0:
589 			printf("0x%08x/0x%08x",
590 			       hammer2_icrc32((char *)voldata +
591 					      HAMMER2_VOLUME_ICRC0_OFF,
592 					      HAMMER2_VOLUME_ICRC0_SIZE),
593 			       voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT0]);
594 			if (hammer2_icrc32((char *)voldata +
595 					   HAMMER2_VOLUME_ICRC0_OFF,
596 					   HAMMER2_VOLUME_ICRC0_SIZE) ==
597 			       voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT0]) {
598 				printf(" (OK)");
599 			} else {
600 				printf(" (FAILED)");
601 			}
602 			break;
603 		case HAMMER2_VOL_ICRC_SECT1:
604 			printf("0x%08x/0x%08x",
605 			       hammer2_icrc32((char *)voldata +
606 					      HAMMER2_VOLUME_ICRC1_OFF,
607 					      HAMMER2_VOLUME_ICRC1_SIZE),
608 			       voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT1]);
609 			if (hammer2_icrc32((char *)voldata +
610 					   HAMMER2_VOLUME_ICRC1_OFF,
611 					   HAMMER2_VOLUME_ICRC1_SIZE) ==
612 			       voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT1]) {
613 				printf(" (OK)");
614 			} else {
615 				printf(" (FAILED)");
616 			}
617 
618 			break;
619 		default:
620 			printf("0x%08x (reserved)", voldata->icrc_sects[i]);
621 			break;
622 		}
623 		printf("\n");
624 	}
625 	printf("    icrc_volhdr    0x%08x/0x%08x",
626 	       hammer2_icrc32((char *)voldata + HAMMER2_VOLUME_ICRCVH_OFF,
627 			      HAMMER2_VOLUME_ICRCVH_SIZE),
628 	       voldata->icrc_volheader);
629 	if (hammer2_icrc32((char *)voldata + HAMMER2_VOLUME_ICRCVH_OFF,
630 			   HAMMER2_VOLUME_ICRCVH_SIZE) ==
631 	    voldata->icrc_volheader) {
632 		printf(" (OK)\n");
633 	} else {
634 		printf(" (FAILED - not a critical error)\n");
635 	}
636 
637 	/*
638 	 * The super-root and freemap blocksets (not recursed)
639 	 */
640 	printf("\n");
641 	printf("    sroot_blockset {\n");
642 	for (i = 0; i < HAMMER2_SET_COUNT; ++i) {
643 		show_bref(voldata, fd, 16, i,
644 			  &voldata->sroot_blockset.blockref[i], 2);
645 	}
646 	printf("    }\n");
647 
648 	printf("    freemap_blockset {\n");
649 	for (i = 0; i < HAMMER2_SET_COUNT; ++i) {
650 		show_bref(voldata, fd, 16, i,
651 			  &voldata->freemap_blockset.blockref[i], 2);
652 	}
653 	printf("    }\n");
654 
655 	printf("}\n");
656 }
657 
658 static void
659 show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
660 	  int bi, hammer2_blockref_t *bref, int norecurse)
661 {
662 	hammer2_media_data_t media;
663 	hammer2_blockref_t *bscan;
664 	hammer2_off_t tmp;
665 	int i, bcount, namelen, failed, obrace;
666 	int type_pad;
667 	size_t bytes;
668 	const char *type_str;
669 	char *str = NULL;
670 	uint32_t cv;
671 	uint64_t cv64;
672 	static int init_tab = -1;
673 
674 	SHA256_CTX hash_ctx;
675 	union {
676 		uint8_t digest[SHA256_DIGEST_LENGTH];
677 		uint64_t digest64[SHA256_DIGEST_LENGTH/8];
678 	} u;
679 
680 	if (init_tab == -1)
681 		init_tab = tab;
682 
683 	bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX);
684 	if (bytes)
685 		bytes = (size_t)1 << bytes;
686 	if (bytes) {
687 		hammer2_off_t io_off;
688 		hammer2_off_t io_base;
689 		size_t io_bytes;
690 		size_t boff;
691 
692 		io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
693 		io_base = io_off & ~(hammer2_off_t)(HAMMER2_LBUFSIZE - 1);
694 		boff = io_off - io_base;
695 
696 		io_bytes = HAMMER2_LBUFSIZE;
697 		while (io_bytes + boff < bytes)
698 			io_bytes <<= 1;
699 
700 		if (io_bytes > sizeof(media)) {
701 			printf("(bad block size %zu)\n", bytes);
702 			return;
703 		}
704 		if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
705 			lseek(fd, io_base, SEEK_SET);
706 			if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
707 				printf("(media read failed)\n");
708 				return;
709 			}
710 			if (boff)
711 				bcopy((char *)&media + boff, &media, bytes);
712 		}
713 	}
714 
715 	bscan = NULL;
716 	bcount = 0;
717 	namelen = 0;
718 	failed = 0;
719 	obrace = 1;
720 
721 	type_str = hammer2_breftype_to_str(bref->type);
722 	type_pad = 8 - strlen(type_str);
723 	if (type_pad < 0)
724 		type_pad = 0;
725 
726 	switch(bref->type) {
727 	case HAMMER2_BREF_TYPE_INODE:
728 		assert(bytes);
729 		if (!(media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) {
730 			bscan = &media.ipdata.u.blockset.blockref[0];
731 			bcount = HAMMER2_SET_COUNT;
732 		}
733 		break;
734 	case HAMMER2_BREF_TYPE_INDIRECT:
735 		assert(bytes);
736 		bscan = &media.npdata[0];
737 		bcount = bytes / sizeof(hammer2_blockref_t);
738 		break;
739 	case HAMMER2_BREF_TYPE_VOLUME:
740 		bscan = &media.voldata.sroot_blockset.blockref[0];
741 		bcount = HAMMER2_SET_COUNT;
742 		break;
743 	case HAMMER2_BREF_TYPE_FREEMAP:
744 		bscan = &media.voldata.freemap_blockset.blockref[0];
745 		bcount = HAMMER2_SET_COUNT;
746 		break;
747 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
748 		assert(bytes);
749 		bscan = &media.npdata[0];
750 		bcount = bytes / sizeof(hammer2_blockref_t);
751 		break;
752 	}
753 
754 	if (QuietOpt > 0) {
755 		tabprintf(tab,
756 			  "%s.%-3d %016jx %016jx/%-2d "
757 			  "mir=%016jx mod=%016jx leafcnt=%d ",
758 			  type_str, bi, (intmax_t)bref->data_off,
759 			  (intmax_t)bref->key, (intmax_t)bref->keybits,
760 			  (intmax_t)bref->mirror_tid,
761 			  (intmax_t)bref->modify_tid,
762 			  bref->leaf_count);
763 	} else {
764 		tabprintf(tab, "%s.%-3d%*.*s 0x%016jx 0x%016jx/%-2d ",
765 			  type_str, bi, type_pad, type_pad, "",
766 			  (intmax_t)bref->data_off,
767 			  (intmax_t)bref->key, (intmax_t)bref->keybits);
768 		/*if (norecurse > 1)*/ {
769 			printf("\n");
770 			tabprintf(tab + 13, "");
771 		}
772 		printf("mir=%016jx mod=%016jx lfcnt=%d ",
773 		       (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid,
774 		       bref->leaf_count);
775 		if (/*norecurse > 1 && */ (bcount || bref->flags ||
776 		    bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
777 		    bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)) {
778 			printf("\n");
779 			tabprintf(tab + 13, "");
780 		}
781 	}
782 
783 	if (bcount)
784 		printf("bcnt=%d ", bcount);
785 	if (bref->flags)
786 		printf("flags=%02x ", bref->flags);
787 	if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
788 	    bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
789 		printf("bigmask=%08x avail=%ld ",
790 			bref->check.freemap.bigmask, bref->check.freemap.avail);
791 	}
792 
793 	/*
794 	 * Check data integrity in verbose mode, otherwise we are just doing
795 	 * a quick meta-data scan.  Meta-data integrity is always checked.
796 	 * (Also see the check above that ensures the media data is loaded,
797 	 * otherwise there's no data to check!).
798 	 *
799 	 * WARNING! bref->check state may be used for other things when
800 	 *	    bref has no data (bytes == 0).
801 	 */
802 	if (bytes &&
803 	    (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1)) {
804 		if (!(QuietOpt > 0)) {
805 			/*if (norecurse > 1)*/ {
806 				printf("\n");
807 				tabprintf(tab + 13, "");
808 			}
809 		}
810 
811 		switch(HAMMER2_DEC_CHECK(bref->methods)) {
812 		case HAMMER2_CHECK_NONE:
813 			printf("meth=%02x ", bref->methods);
814 			break;
815 		case HAMMER2_CHECK_DISABLED:
816 			printf("meth=%02x ", bref->methods);
817 			break;
818 		case HAMMER2_CHECK_ISCSI32:
819 			cv = hammer2_icrc32(&media, bytes);
820 			if (bref->check.iscsi32.value != cv) {
821 				printf("(icrc %02x:%08x/%08x failed) ",
822 				       bref->methods,
823 				       bref->check.iscsi32.value,
824 				       cv);
825 				failed = 1;
826 			} else {
827 				printf("meth=%02x iscsi32=%08x ",
828 				       bref->methods, cv);
829 			}
830 			break;
831 		case HAMMER2_CHECK_XXHASH64:
832 			cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED);
833 			if (bref->check.xxhash64.value != cv64) {
834 				printf("(xxhash64 %02x:%016jx/%016jx failed) ",
835 				       bref->methods,
836 				       bref->check.xxhash64.value,
837 				       cv64);
838 				failed = 1;
839 			} else {
840 				printf("meth=%02x xxh=%016jx ",
841 				       bref->methods, cv64);
842 			}
843 			break;
844 		case HAMMER2_CHECK_SHA192:
845 			SHA256_Init(&hash_ctx);
846 			SHA256_Update(&hash_ctx, &media, bytes);
847 			SHA256_Final(u.digest, &hash_ctx);
848 			u.digest64[2] ^= u.digest64[3];
849 			if (memcmp(u.digest, bref->check.sha192.data,
850 			    sizeof(bref->check.sha192.data))) {
851 				printf("(sha192 failed) ");
852 				failed = 1;
853 			} else {
854 				printf("meth=%02x ", bref->methods);
855 			}
856 			break;
857 		case HAMMER2_CHECK_FREEMAP:
858 			cv = hammer2_icrc32(&media, bytes);
859 			if (bref->check.freemap.icrc32 != cv) {
860 				printf("(fcrc %02x:%08x/%08x failed) ",
861 					bref->methods,
862 					bref->check.freemap.icrc32,
863 					cv);
864 				failed = 1;
865 			} else {
866 				printf("meth=%02x fcrc=%08x ",
867 					bref->methods, cv);
868 			}
869 			break;
870 		}
871 	}
872 
873 	tab += show_tab;
874 
875 	if (QuietOpt > 0) {
876 		obrace = 0;
877 		printf("\n");
878 		goto skip_data;
879 	}
880 
881 	switch(bref->type) {
882 	case HAMMER2_BREF_TYPE_EMPTY:
883 		if (norecurse)
884 			printf("\n");
885 		obrace = 0;
886 		break;
887 	case HAMMER2_BREF_TYPE_DIRENT:
888 		printf("{\n");
889 		if (bref->embed.dirent.namlen <= sizeof(bref->check.buf)) {
890 			tabprintf(tab, "filename \"%*.*s\"\n",
891 				bref->embed.dirent.namlen,
892 				bref->embed.dirent.namlen,
893 				bref->check.buf);
894 		} else {
895 			tabprintf(tab, "filename \"%*.*s\"\n",
896 				bref->embed.dirent.namlen,
897 				bref->embed.dirent.namlen,
898 				media.buf);
899 		}
900 		tabprintf(tab, "inum 0x%016jx\n",
901 			  (uintmax_t)bref->embed.dirent.inum);
902 		tabprintf(tab, "nlen %d\n", bref->embed.dirent.namlen);
903 		tabprintf(tab, "type %s\n",
904 			  hammer2_iptype_to_str(bref->embed.dirent.type));
905 		break;
906 	case HAMMER2_BREF_TYPE_INODE:
907 		printf("{\n");
908 		namelen = media.ipdata.meta.name_len;
909 		if (namelen > HAMMER2_INODE_MAXNAME)
910 			namelen = 0;
911 		tabprintf(tab, "filename \"%*.*s\"\n",
912 			  namelen, namelen, media.ipdata.filename);
913 		tabprintf(tab, "version  %d\n", media.ipdata.meta.version);
914 		if ((media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) ||
915 		    media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) {
916 			tabprintf(tab, "pfs_st   %d (%s)\n",
917 				  media.ipdata.meta.pfs_subtype,
918 				  hammer2_pfssubtype_to_str(media.ipdata.meta.pfs_subtype));
919 		}
920 		tabprintf(tab, "uflags   0x%08x\n",
921 			  media.ipdata.meta.uflags);
922 		if (media.ipdata.meta.rmajor || media.ipdata.meta.rminor) {
923 			tabprintf(tab, "rmajor   %d\n",
924 				  media.ipdata.meta.rmajor);
925 			tabprintf(tab, "rminor   %d\n",
926 				  media.ipdata.meta.rminor);
927 		}
928 		tabprintf(tab, "ctime    %s\n",
929 			  hammer2_time64_to_str(media.ipdata.meta.ctime, &str));
930 		tabprintf(tab, "mtime    %s\n",
931 			  hammer2_time64_to_str(media.ipdata.meta.mtime, &str));
932 		tabprintf(tab, "atime    %s\n",
933 			  hammer2_time64_to_str(media.ipdata.meta.atime, &str));
934 		tabprintf(tab, "btime    %s\n",
935 			  hammer2_time64_to_str(media.ipdata.meta.btime, &str));
936 		tabprintf(tab, "uid      %s\n",
937 			  hammer2_uuid_to_str(&media.ipdata.meta.uid, &str));
938 		tabprintf(tab, "gid      %s\n",
939 			  hammer2_uuid_to_str(&media.ipdata.meta.gid, &str));
940 		tabprintf(tab, "type     %s\n",
941 			  hammer2_iptype_to_str(media.ipdata.meta.type));
942 		tabprintf(tab, "opflgs   0x%02x\n",
943 			  media.ipdata.meta.op_flags);
944 		tabprintf(tab, "capflgs  0x%04x\n",
945 			  media.ipdata.meta.cap_flags);
946 		tabprintf(tab, "mode     %-7o\n",
947 			  media.ipdata.meta.mode);
948 		tabprintf(tab, "inum     0x%016jx\n",
949 			  media.ipdata.meta.inum);
950 		tabprintf(tab, "size     %ju ",
951 			  (uintmax_t)media.ipdata.meta.size);
952 		if (media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA &&
953 		    media.ipdata.meta.size <= HAMMER2_EMBEDDED_BYTES)
954 			printf("(embedded data)\n");
955 		else
956 			printf("\n");
957 		tabprintf(tab, "nlinks   %ju\n",
958 			  (uintmax_t)media.ipdata.meta.nlinks);
959 		tabprintf(tab, "iparent  0x%016jx\n",
960 			  (uintmax_t)media.ipdata.meta.iparent);
961 		tabprintf(tab, "name_key 0x%016jx\n",
962 			  (uintmax_t)media.ipdata.meta.name_key);
963 		tabprintf(tab, "name_len %u\n",
964 			  media.ipdata.meta.name_len);
965 		tabprintf(tab, "ncopies  %u\n",
966 			  media.ipdata.meta.ncopies);
967 		tabprintf(tab, "compalg  %u\n",
968 			  media.ipdata.meta.comp_algo);
969 		tabprintf(tab, "target_t %u\n",
970 			  media.ipdata.meta.target_type);
971 		tabprintf(tab, "checkalg %u\n",
972 			  media.ipdata.meta.check_algo);
973 		if ((media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) ||
974 		    media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) {
975 			tabprintf(tab, "pfs_nmas %u\n",
976 				  media.ipdata.meta.pfs_nmasters);
977 			tabprintf(tab, "pfs_type %u (%s)\n",
978 				  media.ipdata.meta.pfs_type,
979 				  hammer2_pfstype_to_str(media.ipdata.meta.pfs_type));
980 			tabprintf(tab, "pfs_inum 0x%016jx\n",
981 				  (uintmax_t)media.ipdata.meta.pfs_inum);
982 			tabprintf(tab, "pfs_clid %s\n",
983 				  hammer2_uuid_to_str(&media.ipdata.meta.pfs_clid,
984 						      &str));
985 			tabprintf(tab, "pfs_fsid %s\n",
986 				  hammer2_uuid_to_str(&media.ipdata.meta.pfs_fsid,
987 						      &str));
988 			tabprintf(tab, "pfs_lsnap_tid 0x%016jx\n",
989 				  (uintmax_t)media.ipdata.meta.pfs_lsnap_tid);
990 		}
991 		tabprintf(tab, "data_quota  %ju\n",
992 			  (uintmax_t)media.ipdata.meta.data_quota);
993 		tabprintf(tab, "data_count  %ju\n",
994 			  (uintmax_t)bref->embed.stats.data_count);
995 		tabprintf(tab, "inode_quota %ju\n",
996 			  (uintmax_t)media.ipdata.meta.inode_quota);
997 		tabprintf(tab, "inode_count %ju\n",
998 			  (uintmax_t)bref->embed.stats.inode_count);
999 		break;
1000 	case HAMMER2_BREF_TYPE_INDIRECT:
1001 		printf("{\n");
1002 		break;
1003 	case HAMMER2_BREF_TYPE_DATA:
1004 		printf("\n");
1005 		obrace = 0;
1006 		break;
1007 	case HAMMER2_BREF_TYPE_VOLUME:
1008 		printf("mirror_tid=%016jx freemap_tid=%016jx ",
1009 			media.voldata.mirror_tid,
1010 			media.voldata.freemap_tid);
1011 		printf("{\n");
1012 		break;
1013 	case HAMMER2_BREF_TYPE_FREEMAP:
1014 		printf("mirror_tid=%016jx freemap_tid=%016jx ",
1015 			media.voldata.mirror_tid,
1016 			media.voldata.freemap_tid);
1017 		printf("{\n");
1018 		break;
1019 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1020 		printf("{\n");
1021 		tmp = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
1022 		tmp &= HAMMER2_SEGMASK;
1023 		tmp /= HAMMER2_PBUFSIZE;
1024 		assert(tmp >= HAMMER2_ZONE_FREEMAP_00);
1025 		assert(tmp < HAMMER2_ZONE_FREEMAP_END);
1026 		tmp -= HAMMER2_ZONE_FREEMAP_00;
1027 		tmp /= HAMMER2_ZONE_FREEMAP_INC;
1028 		tabprintf(tab, "rotation=%d\n", (int)tmp);
1029 
1030 		for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
1031 			hammer2_off_t data_off = bref->key +
1032 				i * HAMMER2_FREEMAP_LEVEL0_SIZE;
1033 #if HAMMER2_BMAP_ELEMENTS != 8
1034 #error "cmd_debug.c: HAMMER2_BMAP_ELEMENTS expected to be 8"
1035 #endif
1036 			tabprintf(tab + 4, "%016jx %04d.%04x (avail=%7d) "
1037 				  "%016jx %016jx %016jx %016jx "
1038 				  "%016jx %016jx %016jx %016jx\n",
1039 				  data_off, i, media.bmdata[i].class,
1040 				  media.bmdata[i].avail,
1041 				  media.bmdata[i].bitmapq[0],
1042 				  media.bmdata[i].bitmapq[1],
1043 				  media.bmdata[i].bitmapq[2],
1044 				  media.bmdata[i].bitmapq[3],
1045 				  media.bmdata[i].bitmapq[4],
1046 				  media.bmdata[i].bitmapq[5],
1047 				  media.bmdata[i].bitmapq[6],
1048 				  media.bmdata[i].bitmapq[7]);
1049 		}
1050 		tabprintf(tab, "}\n");
1051 		break;
1052 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1053 		printf("{\n");
1054 		tmp = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
1055 		tmp &= HAMMER2_SEGMASK;
1056 		tmp /= HAMMER2_PBUFSIZE;
1057 		assert(tmp >= HAMMER2_ZONE_FREEMAP_00);
1058 		assert(tmp < HAMMER2_ZONE_FREEMAP_END);
1059 		tmp -= HAMMER2_ZONE_FREEMAP_00;
1060 		tmp /= HAMMER2_ZONE_FREEMAP_INC;
1061 		tabprintf(tab, "rotation=%d\n", (int)tmp);
1062 		break;
1063 	default:
1064 		printf("\n");
1065 		obrace = 0;
1066 		break;
1067 	}
1068 	if (str)
1069 		free(str);
1070 
1071 skip_data:
1072 	/*
1073 	 * Update statistics.
1074 	 */
1075 	switch(bref->type) {
1076 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1077 		for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
1078 			hammer2_off_t data_off = bref->key +
1079 				i * HAMMER2_FREEMAP_LEVEL0_SIZE;
1080 			if (data_off >= voldata->aux_end &&
1081 			    data_off < voldata->volu_size) {
1082 				int j;
1083 				for (j = 0; j < 4; ++j)
1084 					CountBlocks(&media.bmdata[i], j,
1085 						    &TotalAccum16[j],
1086 						    &TotalAccum64[j]);
1087 			} else
1088 				TotalUnavail += HAMMER2_FREEMAP_LEVEL0_SIZE;
1089 		}
1090 		TotalFreemap += HAMMER2_FREEMAP_LEVEL1_SIZE;
1091 		break;
1092 	default:
1093 		break;
1094 	}
1095 
1096 	/*
1097 	 * Recurse if norecurse == 0.  If the CRC failed, pass norecurse = 1.
1098 	 * That is, if an indirect or inode fails we still try to list its
1099 	 * direct children to help with debugging, but go no further than
1100 	 * that because they are probably garbage.
1101 	 */
1102 	if (show_depth == -1 || ((tab - init_tab) / show_tab) < show_depth) {
1103 		for (i = 0; norecurse == 0 && i < bcount; ++i) {
1104 			if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
1105 				show_bref(voldata, fd, tab, i, &bscan[i],
1106 				    failed);
1107 			}
1108 		}
1109 	}
1110 	tab -= show_tab;
1111 	if (obrace) {
1112 		if (bref->type == HAMMER2_BREF_TYPE_INODE)
1113 			tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
1114 				  type_str, bi, namelen, namelen,
1115 				  media.ipdata.filename);
1116 		else
1117 			tabprintf(tab, "} (%s.%d)\n", type_str, bi);
1118 	}
1119 }
1120 
1121 static
1122 void
1123 CountBlocks(hammer2_bmap_data_t *bmap, int value,
1124 	    hammer2_off_t *accum16, hammer2_off_t *accum64)
1125 {
1126 	int i, j, bits;
1127 	hammer2_bitmap_t value16, value64;
1128 
1129 	bits = (int)sizeof(hammer2_bitmap_t) * 8;
1130 	assert(bits == 64);
1131 
1132 	value16 = value;
1133 	assert(value16 < 4);
1134 	value64 = (value16 << 6) | (value16 << 4) | (value16 << 2) | value16;
1135 	assert(value64 < 256);
1136 
1137 	for (i = 0; i < HAMMER2_BMAP_ELEMENTS; ++i) {
1138 		hammer2_bitmap_t bm = bmap->bitmapq[i];
1139 		hammer2_bitmap_t bm_save = bm;
1140 		hammer2_bitmap_t mask;
1141 
1142 		mask = 0x03; /* 2 bits per 16KB */
1143 		for (j = 0; j < bits; j += 2) {
1144 			if ((bm & mask) == value16)
1145 				*accum16 += 16384;
1146 			bm >>= 2;
1147 		}
1148 
1149 		bm = bm_save;
1150 		mask = 0xFF; /* 8 bits per 64KB chunk */
1151 		for (j = 0; j < bits; j += 8) {
1152 			if ((bm & mask) == value64)
1153 				*accum64 += 65536;
1154 			bm >>= 8;
1155 		}
1156 	}
1157 }
1158 
1159 int
1160 cmd_hash(int ac, const char **av)
1161 {
1162 	int i;
1163 
1164 	for (i = 0; i < ac; ++i) {
1165 		printf("%016jx %s\n",
1166 		       dirhash((const unsigned char*)av[i], strlen(av[i])),
1167 		       av[i]);
1168 	}
1169 	return(0);
1170 }
1171 
1172 int
1173 cmd_dhash(int ac, const char **av)
1174 {
1175 	char buf[1024];		/* 1K extended directory record */
1176 	uint64_t hash;
1177 	int i;
1178 
1179 	for (i = 0; i < ac; ++i) {
1180 		bzero(buf, sizeof(buf));
1181 		snprintf(buf, sizeof(buf), "%s", av[i]);
1182 		hash = XXH64(buf, sizeof(buf), XXH_HAMMER2_SEED);
1183 		printf("%016jx %s\n", hash, av[i]);
1184 	}
1185 	return(0);
1186 }
1187 
1188 int
1189 cmd_dumpchain(const char *path, u_int flags)
1190 {
1191 	int dummy = (int)flags;
1192 	int ecode = 0;
1193 	int fd;
1194 
1195 	fd = open(path, O_RDONLY);
1196 	if (fd >= 0) {
1197 		if (ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy) < 0) {
1198 			fprintf(stderr, "%s: %s\n", path, strerror(errno));
1199 			ecode = 1;
1200 		}
1201 		close(fd);
1202 	} else {
1203 		fprintf(stderr, "unable to open %s\n", path);
1204 		ecode = 1;
1205 	}
1206 	return ecode;
1207 }
1208 
1209 static
1210 void
1211 tabprintf(int tab, const char *ctl, ...)
1212 {
1213 	va_list va;
1214 
1215 	printf("%*.*s", tab, tab, "");
1216 	va_start(va, ctl);
1217 	vprintf(ctl, va);
1218 	va_end(va);
1219 }
1220