kernel/netgraph7: Fix panic
[dragonfly.git] / sbin / hammer2 / cmd_debug.c
CommitLineData
9ab15106
MD
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
bd878fc9
MD
38#define SHOW_TAB 2
39
1b8eded1 40static void shell_msghandler(dmsg_msg_t *msg, int unmanaged);
0c3a8cd0 41static void shell_ttymsg(dmsg_iocom_t *iocom);
5c37bf12
MD
42static void CountFreeBlocks(hammer2_bmap_data_t *bmap,
43 hammer2_off_t *accum16, hammer2_off_t *accum64);
bd878fc9
MD
44
45/************************************************************************
46 * SHELL *
47 ************************************************************************/
9ab15106 48
1b8eded1
MD
49int
50cmd_shell(const char *hostname)
51{
52 dmsg_master_service_info_t *info;
53 pthread_t thread;
54 int fd;
55
56 fd = dmsg_connect(hostname);
57 if (fd < 0)
58 return 1;
59
60 info = malloc(sizeof(*info));
61 bzero(info, sizeof(*info));
62 info->fd = fd;
63 info->detachme = 0;
64 info->usrmsg_callback = shell_msghandler;
65 info->altmsg_callback = shell_ttymsg;
1b8eded1
MD
66 info->label = strdup("debug");
67 pthread_create(&thread, NULL, dmsg_master_service, info);
68 pthread_join(thread, NULL);
69
70 return 0;
71}
72
73#if 0
9ab15106 74int
bd878fc9 75cmd_shell(const char *hostname)
9ab15106 76{
0c3a8cd0
MD
77 struct dmsg_iocom iocom;
78 dmsg_msg_t *msg;
9ab15106
MD
79 int fd;
80
9ab15106
MD
81 /*
82 * Connect to the target
83 */
0c3a8cd0 84 fd = dmsg_connect(hostname);
02454b3e
MD
85 if (fd < 0)
86 return 1;
9ab15106
MD
87
88 /*
1b8eded1
MD
89 * Initialize the session and transmit an empty DMSG_DBG_SHELL
90 * to cause the remote end to generate a prompt.
9ab15106 91 */
11f7caf4
MD
92 dmsg_iocom_init(&iocom, fd, 0,
93 NULL,
94 shell_rcvmsg,
95 hammer2_shell_parse,
96 shell_ttymsg);
02454b3e 97 fcntl(0, F_SETFL, O_NONBLOCK);
9ab15106
MD
98 printf("debug: connected\n");
99
1b8eded1 100 msg = dmsg_msg_alloc(&iocom.state0, 0, DMSG_DBG_SHELL, NULL, NULL);
0c3a8cd0
MD
101 dmsg_msg_write(msg);
102 dmsg_iocom_core(&iocom);
9ab15106
MD
103 fprintf(stderr, "debug: disconnected\n");
104 close(fd);
105 return 0;
106}
1b8eded1 107#endif
9ab15106
MD
108
109/*
1b8eded1
MD
110 * Debug session front-end
111 *
0c3a8cd0 112 * Callback from dmsg_iocom_core() when messages might be present
9ab15106
MD
113 * on the socket.
114 */
115static
116void
1b8eded1 117shell_msghandler(dmsg_msg_t *msg, int unmanaged)
9ab15106 118{
1b8eded1
MD
119 dmsg_msg_t *nmsg;
120
063639ba 121 switch(msg->tcmd) {
1b8eded1 122#if 0
5bc5bca2
MD
123 case DMSG_LNK_ERROR:
124 case DMSG_LNK_ERROR | DMSGF_REPLY:
81666e1b
MD
125 /*
126 * One-way non-transactional LNK_ERROR messages typically
127 * indicate a connection failure. Error code 0 is used by
128 * the debug shell to indicate no more results from last cmd.
129 */
5903497c 130 if (msg->any.head.error) {
81666e1b 131 fprintf(stderr, "Stream failure: %s\n",
0c3a8cd0 132 dmsg_msg_str(msg));
5903497c
MD
133 } else {
134 write(1, "debug> ", 7);
9ab15106 135 }
5903497c 136 break;
5bc5bca2 137 case DMSG_LNK_ERROR | DMSGF_DELETE:
81666e1b
MD
138 /* ignore termination of LNK_CONN */
139 break;
1b8eded1 140#endif
5bc5bca2 141 case DMSG_DBG_SHELL:
5903497c
MD
142 /*
143 * We send the commands, not accept them.
81666e1b 144 * (one-way message, not transactional)
5903497c 145 */
1b8eded1
MD
146 if (unmanaged)
147 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
5903497c 148 break;
5bc5bca2 149 case DMSG_DBG_SHELL | DMSGF_REPLY:
5903497c
MD
150 /*
151 * A reply from the remote is data we copy to stdout.
81666e1b 152 * (one-way message, not transactional)
5903497c
MD
153 */
154 if (msg->aux_size) {
155 msg->aux_data[msg->aux_size - 1] = 0;
156 write(1, msg->aux_data, strlen(msg->aux_data));
157 }
158 break;
1b8eded1 159#if 1
5bc5bca2 160 case DMSG_LNK_CONN | DMSGF_CREATE:
1b8eded1
MD
161 fprintf(stderr, "Debug Shell received LNK_CONN\n");
162 nmsg = dmsg_msg_alloc(&msg->state->iocom->state0, 0,
163 DMSG_DBG_SHELL,
164 NULL, NULL);
165 dmsg_msg_write(nmsg);
0c3a8cd0 166 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
81666e1b 167 break;
5bc5bca2 168 case DMSG_LNK_CONN | DMSGF_DELETE:
81666e1b 169 break;
1b8eded1 170#endif
5903497c 171 default:
81666e1b
MD
172 /*
173 * Ignore any unknown messages, Terminate any unknown
174 * transactions with an error.
175 */
0c3a8cd0 176 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
1b8eded1
MD
177 if (unmanaged) {
178 if (msg->any.head.cmd & DMSGF_CREATE)
179 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
180 if (msg->any.head.cmd & DMSGF_DELETE)
181 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
182 }
5903497c 183 break;
9ab15106
MD
184 }
185}
186
1b8eded1
MD
187/*
188 * Debug session front-end
189 */
9ab15106
MD
190static
191void
0c3a8cd0 192shell_ttymsg(dmsg_iocom_t *iocom)
9ab15106 193{
1b8eded1 194 dmsg_state_t *pstate;
0c3a8cd0 195 dmsg_msg_t *msg;
9ab15106 196 char buf[256];
1b8eded1 197 char *cmd;
9ab15106
MD
198 size_t len;
199
200 if (fgets(buf, sizeof(buf), stdin) != NULL) {
1b8eded1
MD
201 if (buf[0] == '@') {
202 pstate = dmsg_findspan(strtok(buf + 1, " \t\n"));
203 cmd = strtok(NULL, "\n");
204 } else {
205 pstate = &iocom->state0;
206 cmd = strtok(buf, "\n");
207 }
208 if (cmd && pstate) {
209 len = strlen(cmd) + 1;
210 msg = dmsg_msg_alloc(pstate, len, DMSG_DBG_SHELL,
211 NULL, NULL);
212 bcopy(cmd, msg->aux_data, len);
213 dmsg_msg_write(msg);
214 } else if (cmd) {
215 fprintf(stderr, "@msgid not found\n");
216 } else {
217 /*
218 * This should cause the remote end to generate
219 * a debug> prompt (and thus shows that there is
220 * connectivity).
221 */
222 msg = dmsg_msg_alloc(pstate, 0, DMSG_DBG_SHELL,
223 NULL, NULL);
224 dmsg_msg_write(msg);
225 }
02454b3e 226 } else if (feof(stdin)) {
9ab15106
MD
227 /*
228 * Set EOF flag without setting any error code for normal
229 * EOF.
230 */
0c3a8cd0 231 iocom->flags |= DMSG_IOCOMF_EOF;
02454b3e
MD
232 } else {
233 clearerr(stdin);
9ab15106
MD
234 }
235}
236
1b8eded1
MD
237/*
238 * Debug session back-end (on remote side)
239 */
240static void shell_span(dmsg_msg_t *msg, char *cmdbuf);
5ab1caed 241static void shell_ping(dmsg_msg_t *msg, char *cmdbuf);
81666e1b 242
11f7caf4 243void
b53e7c4f 244hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged)
9ab15106 245{
1b8eded1 246 dmsg_iocom_t *iocom = msg->state->iocom;
b53e7c4f
MD
247 char *cmdbuf;
248 char *cmdp;
249 uint32_t cmd;
9ab15106 250
b53e7c4f 251 /*
5ab1caed 252 * Filter on debug shell commands and ping responses only
b53e7c4f
MD
253 */
254 cmd = msg->any.head.cmd;
5ab1caed
MD
255 if ((cmd & DMSGF_CMDSWMASK) == (DMSG_LNK_PING | DMSGF_REPLY)) {
256 dmsg_printf(iocom, "ping reply\n");
257 return;
258 }
259
b53e7c4f
MD
260 if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) {
261 if (unmanaged)
262 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
263 return;
264 }
265 if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) {
266 if (unmanaged)
267 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
268 return;
269 }
270
271 /*
272 * Debug shell command
273 */
b53e7c4f
MD
274 cmdbuf = msg->aux_data;
275 cmdp = strsep(&cmdbuf, " \t");
276
277 if (cmdp == NULL || *cmdp == 0) {
9ab15106 278 ;
5ab1caed
MD
279 } else if (strcmp(cmdp, "ping") == 0) {
280 shell_ping(msg, cmdbuf);
b53e7c4f 281 } else if (strcmp(cmdp, "span") == 0) {
1b8eded1 282 shell_span(msg, cmdbuf);
b53e7c4f 283 } else if (strcmp(cmdp, "tree") == 0) {
1b8eded1 284 dmsg_shell_tree(iocom, cmdbuf); /* dump spanning tree */
b53e7c4f 285 } else if (strcmp(cmdp, "help") == 0 || strcmp(cmdp, "?") == 0) {
1b8eded1
MD
286 dmsg_printf(iocom, "help Command help\n");
287 dmsg_printf(iocom, "span <host> Span to target host\n");
288 dmsg_printf(iocom, "tree Dump spanning tree\n");
289 dmsg_printf(iocom, "@span <cmd> Issue via circuit\n");
81666e1b 290 } else {
1b8eded1 291 dmsg_printf(iocom, "Unrecognized command: %s\n", cmdp);
81666e1b 292 }
1b8eded1 293 dmsg_printf(iocom, "debug> ");
81666e1b 294}
02454b3e 295
5ab1caed
MD
296static void
297shell_ping(dmsg_msg_t *msg, char *cmdbuf __unused)
298{
299 dmsg_iocom_t *iocom = msg->state->iocom;
300 dmsg_msg_t *m2;
301
302 dmsg_printf(iocom, "sending ping\n");
303 m2 = dmsg_msg_alloc(msg->state, 0, DMSG_LNK_PING, NULL, NULL);
304 dmsg_msg_write(m2);
305}
306
81666e1b 307static void
1b8eded1 308shell_span(dmsg_msg_t *msg, char *cmdbuf)
81666e1b 309{
1b8eded1 310 dmsg_iocom_t *iocom = msg->state->iocom;
ddfbb283 311 dmsg_master_service_info_t *info;
81666e1b
MD
312 const char *hostname = strsep(&cmdbuf, " \t");
313 pthread_t thread;
314 int fd;
02454b3e 315
81666e1b
MD
316 /*
317 * Connect to the target
318 */
319 if (hostname == NULL) {
320 fd = -1;
321 } else {
0c3a8cd0 322 fd = dmsg_connect(hostname);
81666e1b
MD
323 }
324
325 /*
326 * Start master service
327 */
328 if (fd < 0) {
1b8eded1 329 dmsg_printf(iocom, "Connection to %s failed\n", hostname);
9ab15106 330 } else {
1b8eded1 331 dmsg_printf(iocom, "Connected to %s\n", hostname);
ddfbb283
MD
332
333 info = malloc(sizeof(*info));
334 bzero(info, sizeof(*info));
335 info->fd = fd;
336 info->detachme = 1;
b53e7c4f 337 info->usrmsg_callback = hammer2_shell_parse;
c3711753 338 info->label = strdup("client");
ddfbb283
MD
339
340 pthread_create(&thread, NULL, dmsg_master_service, info);
81666e1b 341 /*pthread_join(thread, &res);*/
9ab15106
MD
342 }
343}
344
02454b3e
MD
345/************************************************************************
346 * DEBUGSPAN *
347 ************************************************************************
348 *
349 * Connect to the target manually (not via the cluster list embedded in
350 * a hammer2 filesystem) and initiate the SPAN protocol.
351 */
352int
353cmd_debugspan(const char *hostname)
354{
355 pthread_t thread;
356 int fd;
357 void *res;
358
359 /*
360 * Connect to the target
361 */
0c3a8cd0 362 fd = dmsg_connect(hostname);
02454b3e
MD
363 if (fd < 0)
364 return 1;
365
366 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
0c3a8cd0
MD
367 pthread_create(&thread, NULL,
368 dmsg_master_service, (void *)(intptr_t)fd);
02454b3e
MD
369 pthread_join(thread, &res);
370 return(0);
371}
372
bd878fc9
MD
373/************************************************************************
374 * SHOW *
375 ************************************************************************/
376
5c37bf12
MD
377static void show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
378 int bi, hammer2_blockref_t *bref,
62a2c422 379 int dofreemap, int norecurse);
bd878fc9
MD
380static void tabprintf(int tab, const char *ctl, ...);
381
5c37bf12
MD
382int64_t TotalFreeAccum16;
383int64_t TotalFreeAccum64;
384
bd878fc9 385int
78ac5385 386cmd_show(const char *devpath, int dofreemap)
bd878fc9
MD
387{
388 hammer2_blockref_t broot;
351eb452
MD
389 hammer2_blockref_t best;
390 hammer2_media_data_t media;
bd878fc9 391 int fd;
9061bde5 392 int i;
351eb452 393 int best_i;
bd878fc9 394
5c37bf12
MD
395 TotalFreeAccum16 = 0; /* includes TotalFreeAccum64 */
396 TotalFreeAccum64 = 0;
bd878fc9
MD
397 fd = open(devpath, O_RDONLY);
398 if (fd < 0) {
399 perror("open");
400 return 1;
401 }
351eb452
MD
402
403 /*
404 * Show the tree using the best volume header.
405 * -vvv will show the tree for all four volume headers.
406 */
407 best_i = -1;
408 bzero(&best, sizeof(best));
9061bde5
MD
409 for (i = 0; i < 4; ++i) {
410 bzero(&broot, sizeof(broot));
411 broot.type = HAMMER2_BREF_TYPE_VOLUME;
351eb452
MD
412 broot.data_off = (i * HAMMER2_ZONE_BYTES64) |
413 HAMMER2_PBUFRADIX;
414 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
415 if (read(fd, &media, HAMMER2_PBUFSIZE) ==
416 (ssize_t)HAMMER2_PBUFSIZE) {
417 broot.mirror_tid = media.voldata.mirror_tid;
418 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
419 best_i = i;
420 best = broot;
421 }
422 if (VerboseOpt >= 3)
5c37bf12 423 show_bref(&media.voldata, fd, 0, i, &broot, dofreemap, 0);
351eb452 424 }
9061bde5 425 }
351eb452 426 if (VerboseOpt < 3)
5c37bf12 427 show_bref(&media.voldata, fd, 0, best_i, &best, dofreemap, 0);
bd878fc9
MD
428 close(fd);
429
5c37bf12 430 if (dofreemap && VerboseOpt < 3) {
7350c065 431 printf("Total free storage: %6.3fGB (%6.3fGB in 64KB chunks)\n",
5c37bf12
MD
432 (double)TotalFreeAccum16 / (1024.0 * 1024.0 * 1024.0),
433 (double)TotalFreeAccum64 / (1024.0 * 1024.0 * 1024.0));
434 }
435
bd878fc9
MD
436 return 0;
437}
438
5a790ddf 439extern uint32_t iscsi_crc32(const void *buf, size_t size);
bd878fc9 440static void
5c37bf12
MD
441show_bref(hammer2_volume_data_t *voldata, int fd, int tab,
442 int bi, hammer2_blockref_t *bref, int dofreemap, int norecurse)
bd878fc9
MD
443{
444 hammer2_media_data_t media;
445 hammer2_blockref_t *bscan;
446 int bcount;
447 int i;
448 int didnl;
ad7cf8ea 449 int namelen;
bd878fc9 450 int obrace = 1;
62a2c422 451 int failed;
bd878fc9
MD
452 size_t bytes;
453 const char *type_str;
454 char *str = NULL;
5a790ddf 455 uint32_t cv;
7d565a4f 456 uint64_t cv64;
bd878fc9
MD
457
458 switch(bref->type) {
459 case HAMMER2_BREF_TYPE_EMPTY:
460 type_str = "empty";
461 break;
da0cdd33
MD
462 case HAMMER2_BREF_TYPE_DIRENT:
463 type_str = "dirent";
464 break;
bd878fc9
MD
465 case HAMMER2_BREF_TYPE_INODE:
466 type_str = "inode";
467 break;
468 case HAMMER2_BREF_TYPE_INDIRECT:
469 type_str = "indblk";
470 break;
471 case HAMMER2_BREF_TYPE_DATA:
472 type_str = "data";
473 break;
474 case HAMMER2_BREF_TYPE_VOLUME:
475 type_str = "volume";
476 break;
78ac5385
MD
477 case HAMMER2_BREF_TYPE_FREEMAP:
478 type_str = "freemap";
479 break;
480 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
481 type_str = "fmapnode";
482 break;
483 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
484 type_str = "fbitmap";
485 break;
bd878fc9
MD
486 default:
487 type_str = "unknown";
488 break;
489 }
490
4e0d25fd
MD
491 tabprintf(tab,
492 "%s.%-3d %016jx %016jx/%-2d "
493 "mir=%016jx mod=%016jx leafcnt=%d ",
ad7cf8ea
MD
494 type_str, bi, (intmax_t)bref->data_off,
495 (intmax_t)bref->key, (intmax_t)bref->keybits,
4e0d25fd
MD
496 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid,
497 bref->leaf_count);
bd878fc9 498 tab += SHOW_TAB;
22dffc51
MD
499 if (bref->flags)
500 printf("flags=%02x ", bref->flags);
65cacacf
MD
501 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
502 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
503 printf("bigmask=%08x avail=%ld ",
504 bref->check.freemap.bigmask, bref->check.freemap.avail);
505 }
bd878fc9 506
da0cdd33
MD
507 bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX);
508 if (bytes)
509 bytes = (size_t)1 << bytes;
510 if (bytes) {
78ac5385
MD
511 hammer2_off_t io_off;
512 hammer2_off_t io_base;
513 size_t io_bytes;
514 size_t boff;
515
516 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
517 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
518 io_bytes = bytes;
519 boff = io_off - io_base;
520
521 io_bytes = HAMMER2_MINIOSIZE;
522 while (io_bytes + boff < bytes)
523 io_bytes <<= 1;
524
525 if (io_bytes > sizeof(media)) {
526 printf("(bad block size %zd)\n", bytes);
bd878fc9
MD
527 return;
528 }
78ac5385
MD
529 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
530 lseek(fd, io_base, 0);
531 if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
532 printf("(media read failed)\n");
533 return;
534 }
535 if (boff)
536 bcopy((char *)&media + boff, &media, bytes);
537 }
bd878fc9
MD
538 }
539
540 bscan = NULL;
541 bcount = 0;
78ac5385 542 didnl = 1;
1fca819a 543 namelen = 0;
62a2c422 544 failed = 0;
bd878fc9 545
5a790ddf
MD
546 /*
547 * Check data integrity in verbose mode, otherwise we are just doing
548 * a quick meta-data scan. Meta-data integrity is always checked.
549 * (Also see the check above that ensures the media data is loaded,
550 * otherwise there's no data to check!).
da0cdd33
MD
551 *
552 * WARNING! bref->check state may be used for other things when
553 * bref has no data (bytes == 0).
5a790ddf 554 */
da0cdd33
MD
555 if (bytes &&
556 (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1)) {
5a790ddf
MD
557 switch(HAMMER2_DEC_CHECK(bref->methods)) {
558 case HAMMER2_CHECK_NONE:
8adee7de
MD
559 printf("(meth %02x) ", bref->methods);
560 break;
561 case HAMMER2_CHECK_DISABLED:
562 printf("(meth %02x) ", bref->methods);
5a790ddf
MD
563 break;
564 case HAMMER2_CHECK_ISCSI32:
565 cv = hammer2_icrc32(&media, bytes);
566 if (bref->check.iscsi32.value != cv) {
8b2625c5 567 printf("(icrc %02x:%08x/%08x failed) ",
5a790ddf
MD
568 bref->methods,
569 bref->check.iscsi32.value,
570 cv);
62a2c422 571 failed = 1;
8adee7de 572 } else {
8b2625c5
MD
573 printf("(meth %02x, iscsi32=%08x) ",
574 bref->methods, cv);
5a790ddf
MD
575 }
576 break;
7d565a4f
MD
577 case HAMMER2_CHECK_XXHASH64:
578 cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED);
579 if (bref->check.xxhash64.value != cv64) {
8b2625c5 580 printf("(xxhash64 %02x:%016jx/%016jx failed) ",
7d565a4f
MD
581 bref->methods,
582 bref->check.xxhash64.value,
583 cv64);
62a2c422 584 failed = 1;
7d565a4f 585 } else {
8b2625c5
MD
586 printf("(meth %02x, xxh=%016jx) ",
587 bref->methods, cv64);
7d565a4f 588 }
5a790ddf
MD
589 break;
590 case HAMMER2_CHECK_SHA192:
8adee7de 591 printf("(meth %02x) ", bref->methods);
5a790ddf
MD
592 break;
593 case HAMMER2_CHECK_FREEMAP:
594 cv = hammer2_icrc32(&media, bytes);
595 if (bref->check.freemap.icrc32 != cv) {
8b2625c5 596 printf("(fcrc %02x:%08x/%08x failed) ",
5a790ddf
MD
597 bref->methods,
598 bref->check.freemap.icrc32,
599 cv);
62a2c422 600 failed = 1;
8adee7de 601 } else {
8b2625c5
MD
602 printf("(meth %02x, fcrc=%08x) ",
603 bref->methods, cv);
5a790ddf
MD
604 }
605 break;
606 }
607 }
608
bd878fc9
MD
609 switch(bref->type) {
610 case HAMMER2_BREF_TYPE_EMPTY:
611 obrace = 0;
612 break;
da0cdd33
MD
613 case HAMMER2_BREF_TYPE_DIRENT:
614 printf("{\n");
615 if (bref->embed.dirent.namlen <= sizeof(bref->check.buf)) {
616 tabprintf(tab, "filename \"%*.*s\"\n",
617 bref->embed.dirent.namlen,
618 bref->embed.dirent.namlen,
619 bref->check.buf);
620 } else {
621 tabprintf(tab, "filename \"%*.*s\"\n",
622 bref->embed.dirent.namlen,
623 bref->embed.dirent.namlen,
624 media.buf);
625 }
626 tabprintf(tab, "inum 0x%016jx\n",
627 (uintmax_t)bref->embed.dirent.inum);
628 tabprintf(tab, "type %s\n",
629 hammer2_iptype_to_str(bref->embed.dirent.type));
630 break;
bd878fc9
MD
631 case HAMMER2_BREF_TYPE_INODE:
632 printf("{\n");
b0f58de8 633 if (media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
bd878fc9
MD
634 /* no blockrefs */
635 } else {
636 bscan = &media.ipdata.u.blockset.blockref[0];
637 bcount = HAMMER2_SET_COUNT;
638 }
b0f58de8 639 namelen = media.ipdata.meta.name_len;
ad7cf8ea
MD
640 if (namelen > HAMMER2_INODE_MAXNAME)
641 namelen = 0;
642 tabprintf(tab, "filename \"%*.*s\"\n",
643 namelen, namelen, media.ipdata.filename);
b0f58de8 644 tabprintf(tab, "version %d\n", media.ipdata.meta.version);
bd878fc9 645 tabprintf(tab, "uflags 0x%08x\n",
b0f58de8
MD
646 media.ipdata.meta.uflags);
647 if (media.ipdata.meta.rmajor || media.ipdata.meta.rminor) {
bd878fc9 648 tabprintf(tab, "rmajor %d\n",
b0f58de8 649 media.ipdata.meta.rmajor);
bd878fc9 650 tabprintf(tab, "rminor %d\n",
b0f58de8 651 media.ipdata.meta.rminor);
bd878fc9
MD
652 }
653 tabprintf(tab, "ctime %s\n",
b0f58de8 654 hammer2_time64_to_str(media.ipdata.meta.ctime, &str));
bd878fc9 655 tabprintf(tab, "mtime %s\n",
b0f58de8 656 hammer2_time64_to_str(media.ipdata.meta.mtime, &str));
bd878fc9 657 tabprintf(tab, "atime %s\n",
b0f58de8 658 hammer2_time64_to_str(media.ipdata.meta.atime, &str));
bd878fc9 659 tabprintf(tab, "btime %s\n",
b0f58de8 660 hammer2_time64_to_str(media.ipdata.meta.btime, &str));
bd878fc9 661 tabprintf(tab, "uid %s\n",
b0f58de8 662 hammer2_uuid_to_str(&media.ipdata.meta.uid, &str));
bd878fc9 663 tabprintf(tab, "gid %s\n",
b0f58de8 664 hammer2_uuid_to_str(&media.ipdata.meta.gid, &str));
da0cdd33
MD
665 tabprintf(tab, "type %s\n",
666 hammer2_iptype_to_str(media.ipdata.meta.type));
bd878fc9 667 tabprintf(tab, "opflgs 0x%02x\n",
b0f58de8 668 media.ipdata.meta.op_flags);
bd878fc9 669 tabprintf(tab, "capflgs 0x%04x\n",
b0f58de8 670 media.ipdata.meta.cap_flags);
bd878fc9 671 tabprintf(tab, "mode %-7o\n",
b0f58de8 672 media.ipdata.meta.mode);
bd878fc9 673 tabprintf(tab, "inum 0x%016jx\n",
b0f58de8 674 media.ipdata.meta.inum);
bd878fc9 675 tabprintf(tab, "size %ju\n",
b0f58de8 676 (uintmax_t)media.ipdata.meta.size);
bd878fc9 677 tabprintf(tab, "nlinks %ju\n",
b0f58de8 678 (uintmax_t)media.ipdata.meta.nlinks);
bd878fc9 679 tabprintf(tab, "iparent 0x%016jx\n",
b0f58de8 680 (uintmax_t)media.ipdata.meta.iparent);
bd878fc9 681 tabprintf(tab, "name_key 0x%016jx\n",
b0f58de8 682 (uintmax_t)media.ipdata.meta.name_key);
bd878fc9 683 tabprintf(tab, "name_len %u\n",
b0f58de8 684 media.ipdata.meta.name_len);
bd878fc9 685 tabprintf(tab, "ncopies %u\n",
b0f58de8 686 media.ipdata.meta.ncopies);
bd878fc9 687 tabprintf(tab, "compalg %u\n",
b0f58de8 688 media.ipdata.meta.comp_algo);
8adee7de 689 tabprintf(tab, "checkalg %u\n",
b0f58de8 690 media.ipdata.meta.check_algo);
b7add675
MD
691 if ((media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) ||
692 media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) {
bd878fc9 693 tabprintf(tab, "pfs_type %u (%s)\n",
b0f58de8
MD
694 media.ipdata.meta.pfs_type,
695 hammer2_pfstype_to_str(media.ipdata.meta.pfs_type));
bd878fc9 696 tabprintf(tab, "pfs_inum 0x%016jx\n",
b0f58de8 697 (uintmax_t)media.ipdata.meta.pfs_inum);
8c280d5d 698 tabprintf(tab, "pfs_clid %s\n",
b0f58de8 699 hammer2_uuid_to_str(&media.ipdata.meta.pfs_clid,
bd878fc9
MD
700 &str));
701 tabprintf(tab, "pfs_fsid %s\n",
b0f58de8 702 hammer2_uuid_to_str(&media.ipdata.meta.pfs_fsid,
bd878fc9 703 &str));
7866a0e7
MD
704 tabprintf(tab, "pfs_lsnap_tid 0x%016jx\n",
705 (uintmax_t)media.ipdata.meta.pfs_lsnap_tid);
bd878fc9
MD
706 }
707 tabprintf(tab, "data_quota %ju\n",
b0f58de8 708 (uintmax_t)media.ipdata.meta.data_quota);
bd878fc9 709 tabprintf(tab, "data_count %ju\n",
da0cdd33 710 (uintmax_t)bref->embed.stats.data_count);
bd878fc9 711 tabprintf(tab, "inode_quota %ju\n",
b0f58de8 712 (uintmax_t)media.ipdata.meta.inode_quota);
bd878fc9 713 tabprintf(tab, "inode_count %ju\n",
da0cdd33 714 (uintmax_t)bref->embed.stats.inode_count);
bd878fc9
MD
715 break;
716 case HAMMER2_BREF_TYPE_INDIRECT:
93f3933a 717 bscan = &media.npdata[0];
bd878fc9
MD
718 bcount = bytes / sizeof(hammer2_blockref_t);
719 didnl = 1;
720 printf("{\n");
721 break;
722 case HAMMER2_BREF_TYPE_DATA:
29cbd7e5 723#if 0
bd878fc9
MD
724 if (VerboseOpt >= 2) {
725 printf("{\n");
29cbd7e5
MD
726 } else
727#endif
728 {
bd878fc9
MD
729 printf("\n");
730 obrace = 0;
731 }
732 break;
733 case HAMMER2_BREF_TYPE_VOLUME:
50456506
MD
734 printf("mirror_tid=%016jx freemap_tid=%016jx ",
735 media.voldata.mirror_tid,
10136ab6 736 media.voldata.freemap_tid);
78ac5385
MD
737 if (dofreemap) {
738 bscan = &media.voldata.freemap_blockset.blockref[0];
739 bcount = HAMMER2_SET_COUNT;
740 } else {
741 bscan = &media.voldata.sroot_blockset.blockref[0];
742 bcount = HAMMER2_SET_COUNT;
743 }
744 printf("{\n");
745 break;
746 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
93f3933a
MD
747 printf("{\n");
748 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
5c37bf12
MD
749 hammer2_off_t data_off;
750
751 data_off = bref->key +
752 i * 256 * HAMMER2_FREEMAP_BLOCK_SIZE;
470dad14 753 /*
de936870 754 if (media.bmdata[i].class == 0 &&
93f3933a
MD
755 media.bmdata[i].avail == 0) {
756 continue;
757 }
470dad14 758 */
5cebbe36
MD
759#if HAMMER2_BMAP_ELEMENTS != 8
760#error "cmd_debug.c: HAMMER2_BMAP_ELEMENTS expected to be 8"
761#endif
762
8b2625c5 763 tabprintf(tab + 4, "%016jx %04d.%04x (avail=%7d) "
5cebbe36
MD
764 "%016jx %016jx %016jx %016jx "
765 "%016jx %016jx %016jx %016jx\n",
5c37bf12 766 data_off,
de936870 767 i, media.bmdata[i].class,
93f3933a 768 media.bmdata[i].avail,
5cebbe36
MD
769 media.bmdata[i].bitmapq[0],
770 media.bmdata[i].bitmapq[1],
771 media.bmdata[i].bitmapq[2],
772 media.bmdata[i].bitmapq[3],
773 media.bmdata[i].bitmapq[4],
774 media.bmdata[i].bitmapq[5],
775 media.bmdata[i].bitmapq[6],
776 media.bmdata[i].bitmapq[7]);
5c37bf12
MD
777 if (data_off >= voldata->aux_end &&
778 data_off < voldata->volu_size) {
779 CountFreeBlocks(&media.bmdata[i],
780 &TotalFreeAccum16,
781 &TotalFreeAccum64);
782 }
78ac5385 783 }
93f3933a 784 tabprintf(tab, "}\n");
78ac5385
MD
785 break;
786 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
bd878fc9 787 printf("{\n");
93f3933a 788 bscan = &media.npdata[0];
78ac5385 789 bcount = bytes / sizeof(hammer2_blockref_t);
bd878fc9
MD
790 break;
791 default:
78ac5385
MD
792 printf("\n");
793 obrace = 0;
bd878fc9
MD
794 break;
795 }
796 if (str)
797 free(str);
62a2c422
MD
798
799 /*
800 * Recurse if norecurse == 0. If the CRC failed, pass norecurse = 1.
801 * That is, if an indirect or inode fails we still try to list its
802 * direct children to help with debugging, but go no further than
803 * that because they are probably garbage.
804 */
805 for (i = 0; norecurse == 0 && i < bcount; ++i) {
bd878fc9
MD
806 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
807 if (didnl == 0) {
808 printf("\n");
809 didnl = 1;
810 }
5c37bf12
MD
811 show_bref(voldata, fd, tab,
812 i, &bscan[i], dofreemap, failed);
bd878fc9
MD
813 }
814 }
815 tab -= SHOW_TAB;
816 if (obrace) {
817 if (bref->type == HAMMER2_BREF_TYPE_INODE)
1fca819a
MD
818 tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
819 type_str, bi,
820 namelen, namelen, media.ipdata.filename);
bd878fc9
MD
821 else
822 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
823 }
824}
825
5c37bf12
MD
826static
827void
828CountFreeBlocks(hammer2_bmap_data_t *bmap,
829 hammer2_off_t *accum16, hammer2_off_t *accum64)
830{
831 int i;
832 int j;
833
834 for (i = 0; i < 8; ++i) {
835 uint64_t bm = bmap->bitmapq[i];
836 uint64_t mask;
837
838 mask = 0x03;
839 for (j = 0; j < 64; j += 2) {
840 if ((bm & mask) == 0)
841 *accum16 += 16384;
842 }
843 mask = 0xFF;
844 for (j = 0; j < 64; j += 8) {
845 if ((bm & mask) == 0)
846 *accum64 += 65536;
847 }
848 }
849}
850
775153c2
MD
851int
852cmd_hash(int ac, const char **av)
853{
854 int i;
855
856 for (i = 0; i < ac; ++i) {
857 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
858 }
859 return(0);
860}
861
8f579e46
MD
862int
863cmd_chaindump(const char *path)
864{
865 int dummy = 0;
866 int fd;
867
868 fd = open(path, O_RDONLY);
869 if (fd >= 0) {
870 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
871 close(fd);
872 } else {
873 fprintf(stderr, "unable to open %s\n", path);
874 }
875 return 0;
876}
877
775153c2 878
bd878fc9
MD
879static
880void
881tabprintf(int tab, const char *ctl, ...)
882{
883 va_list va;
884
885 printf("%*.*s", tab, tab, "");
886 va_start(va, ctl);
887 vprintf(ctl, va);
888 va_end(va);
889}