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