Correct some casts in printf arguments in various utilities.
[dragonfly.git] / sbin / hammer / cmd_show.c
CommitLineData
d38ab092
MD
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
84e57c2c 3 *
d38ab092
MD
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
84e57c2c 6 *
d38ab092
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
84e57c2c 10 *
d38ab092
MD
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
84e57c2c 20 *
d38ab092
MD
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
d38ab092
MD
33 */
34
b45803e3
TK
35#include "hammer.h"
36
41ae0862
TK
37#include <sys/tree.h>
38#include <libutil.h>
39
5a19cfc8
MD
40#define FLAG_TOOFARLEFT 0x0001
41#define FLAG_TOOFARRIGHT 0x0002
42#define FLAG_BADTYPE 0x0004
0006adae 43#define FLAG_BADCHILDPARENT 0x0008
39e88285 44#define FLAG_BADMIRRORTID 0x0010
5a19cfc8 45
53176238 46struct {
47fc3381 47 struct hammer_base_elm base;
00dd9c67
TK
48 int limit; /* # of fields to test */
49 int filter; /* filter type (default -1) */
50 int obfuscate; /* obfuscate direntry name */
51 int indent; /* use depth indentation */
64321baf 52 zone_stat_t stats;
686dde96 53} opt;
e7f926a5 54
7efa829c 55static __inline void print_btree(hammer_off_t node_offset);
db7212b1 56static __inline void print_subtree(hammer_btree_elm_t elm);
f3c08aaa
TK
57static void print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
58 hammer_btree_elm_t lbe);
8260ce22 59static int test_node_count(hammer_node_ondisk_t node, char *badmp);
400b9ca8 60static void print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
f3c08aaa 61 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext);
17958f92 62static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
f3c08aaa
TK
63 hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
64static int test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
65static int test_rbn_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
f03c9cf4 66static void print_bigblock_fill(hammer_off_t offset);
4c09d9c4 67static const char *check_data_crc(hammer_btree_elm_t elm, const char **whichp);
06f423f0
TK
68static hammer_crc_t get_inode_crc(hammer_btree_leaf_elm_t leaf,
69 const char **whichp);
2bcf3d94
TK
70static hammer_crc_t get_buf_crc(hammer_btree_leaf_elm_t leaf,
71 const char **whichp);
7efa829c 72static void print_record(hammer_btree_elm_t elm);
686dde96 73static int init_btree_search(const char *arg);
53176238 74static int test_btree_search(hammer_btree_elm_t elm);
94c6425e 75static __inline int test_btree_match(hammer_btree_elm_t elm);
53176238 76static int test_btree_out_of_range(hammer_btree_elm_t elm);
7d0dab48 77static void hexdump_record(const void *ptr, int length, const char *hdr);
d38ab092 78
4a08dcf4
TK
79static int num_bad_node = 0;
80static int num_bad_elm = 0;
2d600cc7 81static int num_bad_rec = 0;
db7212b1
TK
82static int depth;
83
db7212b1
TK
84static const char* _indents[] = {
85 "",
dcf5173c
TK
86 "\t",
87 "\t\t",
88 "\t\t\t",
89 "\t\t\t\t",
90 "\t\t\t\t\t",
91 "\t\t\t\t\t\t",
92 "\t\t\t\t\t\t\t",
93 "\t\t\t\t\t\t\t\t",
94 "\t\t\t\t\t\t\t\t\t", /* deep enough */
db7212b1
TK
95};
96#define INDENT _indents[opt.indent ? depth : 0]
4a08dcf4 97
d38ab092 98void
db7212b1 99hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent)
d38ab092 100{
2dba5fa7 101 volume_info_t volume;
b419d3ee 102 hammer_volume_ondisk_t ondisk;
64321baf 103 zone_stat_t stats = NULL;
d38ab092 104
daf400fa
TK
105 volume = get_root_volume();
106 ondisk = volume->ondisk;
dce7ae2c
TK
107
108 print_blockmap(volume);
e7f926a5 109
2615c079
TK
110 if (VerboseOpt)
111 stats = hammer_init_zone_stat_bits();
112
686dde96
TK
113 bzero(&opt, sizeof(opt));
114 opt.filter = filter;
115 opt.obfuscate = obfuscate;
db7212b1 116 opt.indent = indent;
686dde96
TK
117 opt.stats = stats;
118
119 if (init_btree_search(arg) > 0) {
db7212b1 120 printf("arg=\"%s\"", arg);
686dde96 121 if (opt.limit > 0)
db7212b1 122 printf(" lo=%08x", opt.base.localization);
686dde96 123 if (opt.limit > 1)
02971442 124 printf(" objid=%016jx", (uintmax_t)opt.base.obj_id);
686dde96
TK
125 if (opt.limit > 2)
126 printf(" rt=%02x", opt.base.rec_type);
127 if (opt.limit > 3)
128 printf(" key=%016jx", (uintmax_t)opt.base.key);
129 if (opt.limit > 4)
130 printf(" tid=%016jx", (uintmax_t)opt.base.create_tid);
131 printf("\n");
e7f926a5 132 }
db7212b1 133 print_btree(ondisk->vol0_btree_root);
b9107f58 134
dd96867b 135 if (stats) {
e9976f43
TK
136 hammer_print_zone_stat(stats);
137 hammer_cleanup_zone_stat(stats);
138 }
4a08dcf4 139
52e2f1b5 140 if (num_bad_node || VerboseOpt)
4a08dcf4 141 printf("%d bad nodes\n", num_bad_node);
52e2f1b5 142 if (num_bad_elm || VerboseOpt)
4a08dcf4 143 printf("%d bad elms\n", num_bad_elm);
52e2f1b5 144 if (num_bad_rec || VerboseOpt)
2d600cc7 145 printf("%d bad records\n", num_bad_rec);
d38ab092
MD
146}
147
7efa829c
TK
148static __inline
149void
150print_btree(hammer_off_t node_offset)
151{
db7212b1 152 depth = -1;
f3c08aaa 153 print_btree_node(node_offset, HAMMER_MAX_TID, NULL);
db7212b1 154 assert(depth == -1);
7efa829c
TK
155}
156
157static __inline
158void
db7212b1 159print_subtree(hammer_btree_elm_t elm)
7efa829c 160{
f3c08aaa
TK
161 hammer_btree_internal_elm_t i = &elm->internal;
162 print_btree_node(i->subtree_offset, i->mirror_tid, elm);
7efa829c
TK
163}
164
005a4da7
TK
165static
166void
f3c08aaa
TK
167print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
168 hammer_btree_elm_t lbe)
d38ab092 169{
c17fa600 170 buffer_info_t buffer = NULL;
d38ab092
MD
171 hammer_node_ondisk_t node;
172 hammer_btree_elm_t elm;
173 int i;
4a08dcf4
TK
174 char badc = ' '; /* good */
175 char badm = ' '; /* good */
e7f926a5 176 const char *ext;
d38ab092 177
db7212b1 178 depth++;
1ba4249f 179 node = get_buffer_data(node_offset, &buffer, 0);
d38ab092 180
b9107f58 181 if (node == NULL) {
4a08dcf4
TK
182 badc = 'B';
183 badm = 'I';
184 } else {
4c09d9c4 185 if (!hammer_crc_test_btree(HammerVersion, node))
4a08dcf4
TK
186 badc = 'B';
187 if (node->mirror_tid > mirror_tid) {
188 badc = 'B';
189 badm = 'M';
190 }
8260ce22 191 if (test_node_count(node, &badm) == -1) {
001e28bc 192 badc = 'B';
8260ce22 193 assert(badm != ' ');
001e28bc 194 }
b9107f58
MD
195 }
196
4a08dcf4
TK
197 if (badm != ' ' || badc != ' ') /* not good */
198 ++num_bad_node;
0cd134ab 199
db7212b1
TK
200 printf("%s%c%c NODE %016jx ",
201 INDENT, badc, badm, (uintmax_t)node_offset);
4a08dcf4
TK
202 printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx",
203 node->count,
20b5b42f 204 (uintmax_t)node->parent,
4a08dcf4
TK
205 (node->type ? node->type : '?'),
206 depth,
207 (uintmax_t)node->mirror_tid);
fc124603
TK
208 printf(" fill=");
209 print_bigblock_fill(node_offset);
20b5b42f 210 printf(" {\n");
d38ab092 211
dd96867b 212 if (opt.stats)
686dde96 213 hammer_add_zone_stat(opt.stats, node_offset, sizeof(*node));
e9976f43 214
001e28bc 215 for (i = 0; i < node->count; ++i) {
20b5b42f 216 elm = &node->elms[i];
f29b8e39 217 ext = NULL;
f254e677 218 if (opt.limit) {
f29b8e39
TK
219 switch (node->type) {
220 case HAMMER_BTREE_TYPE_INTERNAL:
53176238 221 if (!test_btree_out_of_range(elm))
dc75e798 222 ext = "*";
f29b8e39
TK
223 break;
224 case HAMMER_BTREE_TYPE_LEAF:
53176238 225 if (test_btree_match(elm))
dc75e798 226 ext = "*";
f29b8e39
TK
227 break;
228 }
f254e677 229 }
f3c08aaa 230 print_btree_elm(node, node_offset, elm, lbe, ext);
20b5b42f
TK
231 }
232 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
001e28bc 233 assert(i == node->count); /* boundary */
20b5b42f 234 elm = &node->elms[i];
f3c08aaa 235 print_btree_elm(node, node_offset, elm, lbe, NULL);
5a19cfc8 236 }
db7212b1 237 printf("%s }\n", INDENT);
d38ab092 238
f254e677 239 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
f1cf4f64
TK
240 for (i = 0; i < node->count; ++i) {
241 elm = &node->elms[i];
f254e677 242 if (opt.limit && opt.filter) {
53176238 243 if (test_btree_out_of_range(elm))
f1cf4f64 244 continue;
f254e677 245 }
d38ab092 246 if (elm->internal.subtree_offset) {
db7212b1 247 print_subtree(elm);
e7f926a5 248 /*
edcc8136 249 * Cause show to do normal iteration after
02971442 250 * seeking to the lo:objid:rt:key:tid
913b6663 251 * by default
e7f926a5 252 */
686dde96
TK
253 if (opt.limit && opt.filter == -1) /* default */
254 opt.filter = 0;
d38ab092 255 }
d38ab092 256 }
f254e677 257 }
d38ab092 258 rel_buffer(buffer);
db7212b1 259 depth--;
d38ab092
MD
260}
261
005a4da7
TK
262static
263int
8260ce22
TK
264test_node_count(hammer_node_ondisk_t node, char *badmp)
265{
266 hammer_node_ondisk_t parent_node;
c17fa600 267 buffer_info_t buffer = NULL;
8260ce22
TK
268 int maxcount;
269
270 maxcount = hammer_node_max_elements(node->type);
271
272 if (maxcount == -1) {
273 *badmp = 'U';
274 return(-1);
275 } else if (node->count > maxcount) {
276 *badmp = 'C';
277 return(-1);
278 } else if (node->count == 0) {
1ba4249f 279 parent_node = get_buffer_data(node->parent, &buffer, 0);
8260ce22
TK
280 if (parent_node->count != 1) {
281 *badmp = 'C';
282 rel_buffer(buffer);
283 return(-1);
284 }
285 rel_buffer(buffer);
286 }
287
288 return(0);
289}
290
04b9236f
TK
291static __inline
292int
46137e17 293is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm)
04b9236f 294{
bfbd8331
TK
295 /*
296 * elm->base.btype depends on what the original node had
297 * so it could be anything but HAMMER_BTREE_TYPE_NONE.
298 */
04b9236f
TK
299 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
300 i == 0 &&
6416eb03 301 elm->base.localization == HAMMER_MIN_ONDISK_LOCALIZATION &&
9615af80
TK
302 elm->base.obj_id == (int64_t)HAMMER_MIN_OBJID &&
303 elm->base.key == (int64_t)HAMMER_MIN_KEY &&
04b9236f
TK
304 elm->base.create_tid == 1 &&
305 elm->base.delete_tid == 1 &&
9615af80 306 elm->base.rec_type == HAMMER_MIN_RECTYPE &&
04b9236f 307 elm->base.obj_type == 0 &&
bfbd8331 308 elm->base.btype != HAMMER_BTREE_TYPE_NONE);
04b9236f
TK
309}
310
311static __inline
312int
46137e17 313is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm)
04b9236f
TK
314{
315 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
316 i != 0 &&
6416eb03 317 elm->base.localization == HAMMER_MAX_ONDISK_LOCALIZATION &&
9615af80
TK
318 elm->base.obj_id == HAMMER_MAX_OBJID &&
319 elm->base.key == HAMMER_MAX_KEY &&
320 elm->base.create_tid == HAMMER_MAX_TID &&
04b9236f 321 elm->base.delete_tid == 0 &&
9615af80 322 elm->base.rec_type == HAMMER_MAX_RECTYPE &&
04b9236f 323 elm->base.obj_type == 0 &&
bfbd8331 324 elm->base.btype == HAMMER_BTREE_TYPE_NONE);
04b9236f
TK
325}
326
d38ab092
MD
327static
328void
400b9ca8 329print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
f3c08aaa 330 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext)
d38ab092 331{
5a19cfc8 332 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
2f8c6a59 333 char deleted;
04b9236f 334 char rootelm;
a81bcfad 335 const char *label;
fc124603 336 const char *p;
4c09d9c4 337 const char *which;
400b9ca8 338 int flags;
19615386 339 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
5a19cfc8 340
f3c08aaa 341 flags = get_elm_flags(node, node_offset, elm, lbe);
5a19cfc8
MD
342 flagstr[0] = flags ? 'B' : 'G';
343 if (flags & FLAG_TOOFARLEFT)
344 flagstr[2] = 'L';
345 if (flags & FLAG_TOOFARRIGHT)
346 flagstr[3] = 'R';
347 if (flags & FLAG_BADTYPE)
348 flagstr[4] = 'T';
0006adae 349 if (flags & FLAG_BADCHILDPARENT)
39e88285
MD
350 flagstr[5] = 'C';
351 if (flags & FLAG_BADMIRRORTID)
352 flagstr[6] = 'M';
4a08dcf4
TK
353 if (flagstr[0] == 'B')
354 ++num_bad_elm;
5a19cfc8 355
2f8c6a59 356 /*
04b9236f 357 * Check if elm is derived from root split
2f8c6a59 358 */
a81bcfad 359 if (is_root_btree_beg(node->type, i, elm))
04b9236f 360 rootelm = '>';
a81bcfad 361 else if (is_root_btree_end(node->type, i, elm))
04b9236f
TK
362 rootelm = '<';
363 else
364 rootelm = ' ';
365
366 if (elm->base.delete_tid)
367 deleted = 'd';
368 else
2f8c6a59 369 deleted = ' ';
2f8c6a59 370
a81bcfad
TK
371 if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
372 label = "RBN";
373 else
374 label = "ELM";
375
db7212b1
TK
376 printf("%s%s %s %2d %c ",
377 INDENT, flagstr, label, i, hammer_elm_btype(elm));
02971442 378 printf("lo=%08x objid=%016jx rt=%02x key=%016jx tid=%016jx\n",
58c17893 379 elm->base.localization,
331e9727 380 (uintmax_t)elm->base.obj_id,
d38ab092 381 elm->base.rec_type,
331e9727 382 (uintmax_t)elm->base.key,
d9930283 383 (uintmax_t)elm->base.create_tid);
db7212b1
TK
384 printf("%s %c del=%016jx ot=%02x",
385 INDENT,
04b9236f 386 (rootelm == ' ' ? deleted : rootelm),
d9930283
TK
387 (uintmax_t)elm->base.delete_tid,
388 elm->base.obj_type);
d38ab092 389
a81bcfad 390 switch(node->type) {
d38ab092 391 case HAMMER_BTREE_TYPE_INTERNAL:
fc124603
TK
392 printf(" suboff=%016jx mirror=%016jx",
393 (uintmax_t)elm->internal.subtree_offset,
394 (uintmax_t)elm->internal.mirror_tid);
e7f926a5
MD
395 if (ext)
396 printf(" %s", ext);
d38ab092
MD
397 break;
398 case HAMMER_BTREE_TYPE_LEAF:
399 switch(elm->base.btype) {
400 case HAMMER_BTREE_TYPE_RECORD:
d63d1254 401 printf(" dataoff=%016jx/%d",
a276dc6b
MD
402 (uintmax_t)elm->leaf.data_offset,
403 elm->leaf.data_len);
4c09d9c4
MD
404 p = check_data_crc(elm, &which);
405 printf(" %scrc=%08x", which, elm->leaf.data_crc);
fc124603
TK
406 if (p) {
407 printf(" error=%s", p);
408 ++num_bad_rec;
f03c9cf4 409 }
fc124603
TK
410 printf(" fill=");
411 print_bigblock_fill(elm->leaf.data_offset);
e95314de 412 if (QuietOpt < 2)
0006adae 413 print_record(elm);
9930da22 414 if (opt.stats) {
686dde96 415 hammer_add_zone_stat(opt.stats,
e9976f43
TK
416 elm->leaf.data_offset,
417 elm->leaf.data_len);
9930da22 418 }
d38ab092 419 break;
ef5c9cdc 420 default:
6eb402e5 421 printf(" badtype=%d", elm->base.btype);
ef5c9cdc 422 break;
d38ab092 423 }
dc75e798
TK
424 if (ext)
425 printf(" %s", ext);
d38ab092 426 break;
d38ab092
MD
427 }
428 printf("\n");
429}
430
5a19cfc8
MD
431static
432int
17958f92 433get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
f3c08aaa 434 hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
5a19cfc8 435{
ef69cace 436 hammer_off_t child_offset;
5a19cfc8 437 int flags = 0;
19615386 438 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
5a19cfc8
MD
439
440 switch(node->type) {
441 case HAMMER_BTREE_TYPE_INTERNAL:
ef69cace 442 child_offset = elm->internal.subtree_offset;
39e88285
MD
443 if (elm->internal.mirror_tid > node->mirror_tid)
444 flags |= FLAG_BADMIRRORTID;
0006adae 445
6580f642 446 if (i == node->count) {
ef69cace
TK
447 if (child_offset != 0)
448 flags |= FLAG_BADCHILDPARENT;
b6361ed8
TK
449 switch(elm->base.btype) {
450 case HAMMER_BTREE_TYPE_NONE:
f3c08aaa 451 flags |= test_rbn_lr(elm, lbe);
b6361ed8
TK
452 break;
453 default:
454 flags |= FLAG_BADTYPE;
455 break;
456 }
6580f642 457 } else {
ef69cace
TK
458 if (child_offset == 0) {
459 flags |= FLAG_BADCHILDPARENT;
460 } else {
c17fa600 461 buffer_info_t buffer = NULL;
ef69cace 462 hammer_node_ondisk_t subnode;
1ba4249f 463 subnode = get_buffer_data(child_offset, &buffer, 0);
ef69cace
TK
464 if (subnode == NULL)
465 flags |= FLAG_BADCHILDPARENT;
466 else if (subnode->parent != node_offset)
467 flags |= FLAG_BADCHILDPARENT;
468 rel_buffer(buffer);
469 }
6580f642
TK
470 switch(elm->base.btype) {
471 case HAMMER_BTREE_TYPE_INTERNAL:
472 case HAMMER_BTREE_TYPE_LEAF:
f3c08aaa 473 flags |= test_lr(elm, lbe);
47197d71 474 break;
6580f642
TK
475 default:
476 flags |= FLAG_BADTYPE;
47197d71 477 break;
6580f642 478 }
5a19cfc8
MD
479 }
480 break;
481 case HAMMER_BTREE_TYPE_LEAF:
52e2f1b5 482 if (elm->leaf.data_offset == 0)
ef69cace 483 flags |= FLAG_BADCHILDPARENT;
52e2f1b5 484 if (elm->leaf.data_len == 0)
ef69cace 485 flags |= FLAG_BADCHILDPARENT;
ef69cace 486
44cd685c 487 if (node->mirror_tid == 0 &&
f254e677 488 !(node->parent == 0 && node->count == 2)) {
44cd685c 489 flags |= FLAG_BADMIRRORTID;
f254e677 490 }
44cd685c 491 if (elm->base.create_tid && node->mirror_tid &&
f254e677 492 elm->base.create_tid > node->mirror_tid) {
39e88285 493 flags |= FLAG_BADMIRRORTID;
f254e677 494 }
44cd685c 495 if (elm->base.delete_tid && node->mirror_tid &&
f254e677 496 elm->base.delete_tid > node->mirror_tid) {
39e88285 497 flags |= FLAG_BADMIRRORTID;
f254e677 498 }
6580f642 499 switch(elm->base.btype) {
5a19cfc8 500 case HAMMER_BTREE_TYPE_RECORD:
f3c08aaa 501 flags |= test_lr(elm, lbe);
5a19cfc8
MD
502 break;
503 default:
504 flags |= FLAG_BADTYPE;
505 break;
506 }
507 break;
508 default:
509 flags |= FLAG_BADTYPE;
510 break;
511 }
512 return(flags);
513}
514
28b9bac3
TK
515/*
516 * Taken from /usr/src/sys/vfs/hammer/hammer_btree.c.
517 */
518static
519int
520hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
521{
522 if (key1->localization < key2->localization)
523 return(-5);
524 if (key1->localization > key2->localization)
525 return(5);
526
527 if (key1->obj_id < key2->obj_id)
528 return(-4);
529 if (key1->obj_id > key2->obj_id)
530 return(4);
531
532 if (key1->rec_type < key2->rec_type)
533 return(-3);
534 if (key1->rec_type > key2->rec_type)
535 return(3);
536
537 if (key1->key < key2->key)
538 return(-2);
539 if (key1->key > key2->key)
540 return(2);
541
542 if (key1->create_tid == 0) {
543 if (key2->create_tid == 0)
544 return(0);
545 return(1);
546 }
547 if (key2->create_tid == 0)
548 return(-1);
549 if (key1->create_tid < key2->create_tid)
550 return(-1);
551 if (key1->create_tid > key2->create_tid)
552 return(1);
553 return(0);
554}
555
6580f642
TK
556static
557int
f3c08aaa 558test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
6580f642 559{
f3c08aaa
TK
560 if (lbe) {
561 hammer_btree_elm_t rbe = lbe + 1;
562 if (hammer_btree_cmp(&elm->base, &lbe->base) < 0)
563 return(FLAG_TOOFARLEFT);
564 if (hammer_btree_cmp(&elm->base, &rbe->base) >= 0)
565 return(FLAG_TOOFARRIGHT);
566 }
6580f642
TK
567 return(0);
568}
569
570static
571int
f3c08aaa 572test_rbn_lr(hammer_btree_elm_t rbn, hammer_btree_elm_t lbe)
6580f642 573{
f3c08aaa
TK
574 if (lbe) {
575 hammer_btree_elm_t rbe = lbe + 1;
576 if (hammer_btree_cmp(&rbn->base, &lbe->base) < 0)
577 return(FLAG_TOOFARLEFT);
578 if (hammer_btree_cmp(&rbn->base, &rbe->base) > 0)
579 return(FLAG_TOOFARRIGHT);
580 }
6580f642
TK
581 return(0);
582}
583
f03c9cf4
MD
584static
585void
586print_bigblock_fill(hammer_off_t offset)
587{
588 struct hammer_blockmap_layer1 layer1;
589 struct hammer_blockmap_layer2 layer2;
590 int fill;
6ed4c886
MD
591 int error;
592
8b916bc2 593 blockmap_lookup_save(offset, &layer1, &layer2, &error);
e2124556 594 printf("z%d:v%d:%d:%d:%lu=",
6291ccf8
TK
595 HAMMER_ZONE_DECODE(offset),
596 HAMMER_VOL_DECODE(offset),
597 HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
598 HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
599 offset & HAMMER_BIGBLOCK_MASK64);
600
6ed4c886 601 if (error) {
6291ccf8 602 printf("B%d", error);
6ed4c886 603 } else {
e04ee2de 604 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
00dd9c67 605 printf("%d%%", 100 - fill);
6ed4c886 606 }
f03c9cf4
MD
607}
608
b3465cda
MD
609/*
610 * Check the generic crc on a data element. Inodes record types are
611 * special in that some of their fields are not CRCed.
6ed4c886
MD
612 *
613 * Also check that the zone is valid.
b3465cda
MD
614 */
615static
616const char *
4c09d9c4 617check_data_crc(hammer_btree_elm_t elm, const char **whichp)
b3465cda 618{
17b150c6 619 hammer_crc_t crc;
b3465cda 620
4c09d9c4 621 *whichp = "";
06f423f0 622 if (elm->leaf.data_offset == 0 || elm->leaf.data_len == 0)
1c1d5eec 623 return("ZO"); /* zero offset or length */
b3465cda 624
3fd1e952
TK
625 switch (elm->leaf.base.rec_type) {
626 case HAMMER_RECTYPE_INODE:
06f423f0 627 if (elm->leaf.data_len != sizeof(struct hammer_inode_data))
e0c64b7d 628 return("BI"); /* bad inode size */
06f423f0 629 crc = get_inode_crc(&elm->leaf, whichp);
3fd1e952
TK
630 break;
631 default:
06f423f0 632 crc = get_buf_crc(&elm->leaf, whichp);
3fd1e952
TK
633 break;
634 }
635
636 if (crc == 0)
637 return("Bx"); /* bad crc */
1c1d5eec
TK
638 if (crc != elm->leaf.data_crc)
639 return("BX"); /* bad crc */
640 return(NULL); /* success */
b3465cda
MD
641}
642
06f423f0
TK
643static
644hammer_crc_t
645get_inode_crc(hammer_btree_leaf_elm_t leaf, const char **whichp)
646{
647 buffer_info_t data_buffer = NULL;
648 hammer_crc_t crc;
649 char *ptr;
650
651 ptr = get_buffer_data(leaf->data_offset, &data_buffer, 0);
652
653 if (HammerVersion >= HAMMER_VOL_VERSION_SEVEN) {
654 crc = iscsi_crc32(ptr, HAMMER_INODE_CRCSIZE);
655 if (crc == leaf->data_crc) {
656 *whichp = "i";
657 goto end;
658 }
659 }
660
661 crc = crc32(ptr, HAMMER_INODE_CRCSIZE);
662 if (crc == leaf->data_crc) {
663 *whichp = "o";
664 goto end;
665 }
666
667 *whichp = "";
668end:
669 rel_buffer(data_buffer);
670 return(crc);
671}
672
2bcf3d94
TK
673typedef uint32_t (*crc32_ext_fn)(const void *, size_t, uint32_t);
674
3fd1e952 675static
2bcf3d94
TK
676hammer_crc_t
677__get_buf_crc(hammer_btree_leaf_elm_t leaf, crc32_ext_fn f)
3fd1e952 678{
c17fa600 679 buffer_info_t data_buffer = NULL;
06f423f0 680 hammer_off_t buf_offset;
17b150c6 681 hammer_crc_t crc = 0;
06f423f0 682 int32_t buf_len, len;
3fd1e952
TK
683 char *ptr;
684
06f423f0
TK
685 buf_offset = leaf->data_offset;
686 buf_len = leaf->data_len;
687
3fd1e952
TK
688 while (buf_len) {
689 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
690 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
691 if (len > buf_len)
692 len = (int)buf_len;
693 assert(len <= HAMMER_BUFSIZE);
2bcf3d94 694 crc = f(ptr, len, crc);
3fd1e952
TK
695 buf_len -= len;
696 buf_offset += len;
697 }
698 rel_buffer(data_buffer);
699
2bcf3d94
TK
700 return(crc);
701}
702
703static
704hammer_crc_t
705get_buf_crc(hammer_btree_leaf_elm_t leaf, const char **whichp)
706{
2bcf3d94
TK
707 hammer_crc_t crc;
708
709 if (HammerVersion >= HAMMER_VOL_VERSION_SEVEN) {
710 crc = __get_buf_crc(leaf, iscsi_crc32_ext);
711 if (crc == leaf->data_crc) {
712 *whichp = "i";
713 goto end;
714 }
715 }
716
717 crc = __get_buf_crc(leaf, crc32_ext);
718 if (crc == leaf->data_crc) {
4c09d9c4 719 *whichp = "o";
2bcf3d94 720 goto end;
4c09d9c4 721 }
6c1845d8
TK
722
723 *whichp = "";
2bcf3d94 724end:
2bcf3d94 725 return(crc);
4c09d9c4
MD
726}
727
bc5af92e
AHJ
728static
729void
730print_config(char *cfgtxt)
731{
732 char *token;
733
db7212b1 734 printf("\n%s%17s", INDENT, "");
e24b3fc7 735 printf("config text=\"\n");
f254e677 736 if (cfgtxt != NULL) {
9930da22
TK
737 while((token = strsep(&cfgtxt, "\r\n")) != NULL) {
738 if (strlen(token)) {
db7212b1
TK
739 printf("%s%17s %s\n",
740 INDENT, "", token);
9930da22
TK
741 }
742 }
f254e677 743 }
db7212b1 744 printf("%s%17s \"", INDENT, "");
bc5af92e
AHJ
745}
746
0006adae
MD
747static
748void
749print_record(hammer_btree_elm_t elm)
750{
c17fa600 751 buffer_info_t data_buffer;
0006adae
MD
752 hammer_off_t data_offset;
753 int32_t data_len;
11ad5ade 754 hammer_data_ondisk_t data;
6541a74b
TK
755 char *str1 = NULL;
756 char *str2 = NULL;
0006adae 757
0006adae
MD
758 data_offset = elm->leaf.data_offset;
759 data_len = elm->leaf.data_len;
33ad9365
TK
760 assert(data_offset != 0);
761 assert(data_len != 0);
0006adae 762
33ad9365
TK
763 data_buffer = NULL;
764 data = get_buffer_data(data_offset, &data_buffer, 0);
765 assert(data != NULL);
0006adae 766
11ad5ade 767 switch(elm->leaf.base.rec_type) {
6541a74b 768 case HAMMER_RECTYPE_UNKNOWN:
db7212b1 769 printf("\n%s%17s", INDENT, "");
6541a74b
TK
770 printf("unknown");
771 break;
0006adae 772 case HAMMER_RECTYPE_INODE:
db7212b1 773 printf("\n%s%17s", INDENT, "");
1f173364 774 printf("inode size=%jd nlinks=%jd",
a276dc6b
MD
775 (intmax_t)data->inode.size,
776 (intmax_t)data->inode.nlinks);
fc124603
TK
777 printf(" mode=%05o uflags=%08x caps=%02x",
778 data->inode.mode,
779 data->inode.uflags,
780 data->inode.cap_flags);
781 printf(" pobjid=%016jx ot=%02x\n",
782 (uintmax_t)data->inode.parent_obj_id,
783 data->inode.obj_type);
784 printf("%s%17s", INDENT, "");
785 printf(" ctime=%016jx mtime=%016jx atime=%016jx",
786 (uintmax_t)data->inode.ctime,
787 (uintmax_t)data->inode.mtime,
788 (uintmax_t)data->inode.atime);
9930da22 789 if (data->inode.ext.symlink[0]) {
fc124603
TK
790 printf(" symlink=\"%s\"",
791 data->inode.ext.symlink);
9930da22 792 }
0006adae
MD
793 break;
794 case HAMMER_RECTYPE_DIRENTRY:
18bc5e7c 795 data_len -= HAMMER_ENTRY_NAME_OFF;
db7212b1 796 printf("\n%s%17s", INDENT, "");
02971442 797 printf("dir-entry objid=%016jx lo=%08x",
a276dc6b 798 (uintmax_t)data->entry.obj_id,
337ec5f8 799 data->entry.localization);
9930da22 800 if (!opt.obfuscate) {
337ec5f8
TK
801 printf(" name=\"%*.*s\"",
802 data_len, data_len, data->entry.name);
9930da22 803 }
0006adae
MD
804 break;
805 case HAMMER_RECTYPE_FIX:
11ad5ade 806 switch(elm->leaf.base.key) {
0006adae 807 case HAMMER_FIXKEY_SYMLINK:
18bc5e7c 808 data_len -= HAMMER_SYMLINK_NAME_OFF;
db7212b1 809 printf("\n%s%17s", INDENT, "");
1f173364
TK
810 printf("fix-symlink name=\"%*.*s\"",
811 data_len, data_len, data->symlink.name);
0006adae 812 break;
0006adae
MD
813 }
814 break;
6541a74b 815 case HAMMER_RECTYPE_PFS:
db7212b1 816 printf("\n%s%17s", INDENT, "");
1f173364 817 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
1f5e0b99
SW
818 (uintmax_t)data->pfsd.sync_beg_tid,
819 (uintmax_t)data->pfsd.sync_end_tid);
3cd578ed
TK
820 hammer_uuid_to_string(&data->pfsd.shared_uuid, &str1);
821 hammer_uuid_to_string(&data->pfsd.unique_uuid, &str2);
6541a74b 822 printf("%17s", "");
1f173364 823 printf(" shared_uuid=%s\n", str1);
6541a74b 824 printf("%17s", "");
1f173364 825 printf(" unique_uuid=%s\n", str2);
6541a74b 826 printf("%17s", "");
1f173364 827 printf(" mirror_flags=%08x label=\"%s\"",
6541a74b
TK
828 data->pfsd.mirror_flags, data->pfsd.label);
829 if (data->pfsd.snapshots[0])
830 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
831 free(str1);
832 free(str2);
833 break;
834 case HAMMER_RECTYPE_SNAPSHOT:
db7212b1 835 printf("\n%s%17s", INDENT, "");
1f173364 836 printf("snapshot tid=%016jx label=\"%s\"",
1f5e0b99 837 (uintmax_t)data->snap.tid, data->snap.label);
6541a74b
TK
838 break;
839 case HAMMER_RECTYPE_CONFIG:
840 if (VerboseOpt > 2) {
d604bad6
TK
841 char *p = strdup(data->config.text);
842 print_config(p);
843 free(p);
6541a74b
TK
844 }
845 break;
5d6e04f0 846 case HAMMER_RECTYPE_DATA:
01fdc736
AHJ
847 if (VerboseOpt > 3) {
848 printf("\n");
7d0dab48 849 hexdump_record(data, data_len, "\t\t ");
01fdc736
AHJ
850 }
851 break;
5d6e04f0
AHJ
852 case HAMMER_RECTYPE_EXT:
853 case HAMMER_RECTYPE_DB:
01fdc736 854 if (VerboseOpt > 2) {
5d6e04f0 855 printf("\n");
7d0dab48 856 hexdump_record(data, data_len, "\t\t ");
01fdc736 857 }
5d6e04f0 858 break;
0006adae 859 default:
33ad9365 860 assert(0);
0006adae
MD
861 break;
862 }
3c6df284 863 rel_buffer(data_buffer);
0006adae
MD
864}
865
7d0dab48
TK
866/*
867 * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
868 * which is 16KB. Passing record data length larger than 16KB to
869 * hexdump(3) is invalid even if the leaf node elm says >16KB data.
870 */
005a4da7
TK
871static
872void
7d0dab48
TK
873hexdump_record(const void *ptr, int length, const char *hdr)
874{
875 int data_len = length;
876
877 if (data_len > HAMMER_BUFSIZE) /* XXX */
878 data_len = HAMMER_BUFSIZE;
879 hexdump(ptr, data_len, hdr, 0);
880
881 if (length > data_len)
882 printf("%s....\n", hdr);
883}
884
522fafa8 885static __inline __always_inline
913b6663
TK
886unsigned long
887_strtoul(const char *p, int base)
888{
889 unsigned long retval;
890
891 errno = 0; /* clear */
892 retval = strtoul(p, NULL, base);
052fd72b 893 if (errno == ERANGE && retval == ULONG_MAX) {
913b6663 894 err(1, "strtoul");
052fd72b
TK
895 /* not reached */
896 }
913b6663
TK
897 return retval;
898}
899
522fafa8 900static __inline __always_inline
913b6663
TK
901unsigned long long
902_strtoull(const char *p, int base)
903{
904 unsigned long long retval;
905
906 errno = 0; /* clear */
907 retval = strtoull(p, NULL, base);
052fd72b 908 if (errno == ERANGE && retval == ULLONG_MAX) {
913b6663 909 err(1, "strtoull");
052fd72b
TK
910 /* not reached */
911 }
913b6663
TK
912 return retval;
913}
914
005a4da7
TK
915static
916int
686dde96 917init_btree_search(const char *arg)
913b6663
TK
918{
919 char *s, *p;
920 int i = 0;
921
686dde96
TK
922 bzero(&opt.base, sizeof(opt.base));
923 opt.limit = 0;
913b6663 924
47fc3381
TK
925 if (arg == NULL)
926 return(-1);
337ec5f8
TK
927 if (strcmp(arg, "none") == 0)
928 return(-1);
47fc3381 929
913b6663
TK
930 s = strdup(arg);
931 if (s == NULL)
932 return(-1);
933
934 while ((p = s) != NULL) {
935 if ((s = strchr(s, ':')) != NULL)
936 *s++ = 0;
937 if (++i == 1) {
686dde96 938 opt.base.localization = _strtoul(p, 16);
913b6663 939 } else if (i == 2) {
686dde96 940 opt.base.obj_id = _strtoull(p, 16);
913b6663 941 } else if (i == 3) {
686dde96 942 opt.base.rec_type = _strtoul(p, 16);
913b6663 943 } else if (i == 4) {
686dde96 944 opt.base.key = _strtoull(p, 16);
913b6663 945 } else if (i == 5) {
686dde96 946 opt.base.create_tid = _strtoull(p, 16);
913b6663
TK
947 break;
948 }
949 }
686dde96 950 opt.limit = i;
913b6663 951 free(s);
47fc3381 952
913b6663
TK
953 return(i);
954}
955
005a4da7
TK
956static
957int
53176238 958test_btree_search(hammer_btree_elm_t elm)
913b6663 959{
47fc3381 960 hammer_base_elm_t base1 = &elm->base;
686dde96
TK
961 hammer_base_elm_t base2 = &opt.base;
962 int limit = opt.limit;
913b6663 963
47fc3381 964 if (base1->localization < base2->localization)
9a4f151e 965 return(-5);
47fc3381 966 if (base1->localization > base2->localization)
9a4f151e 967 return(5);
53176238 968 if (limit == 1)
34909d8e 969 return(0); /* ignore below */
913b6663 970
47fc3381 971 if (base1->obj_id < base2->obj_id)
9a4f151e 972 return(-4);
47fc3381 973 if (base1->obj_id > base2->obj_id)
9a4f151e 974 return(4);
53176238 975 if (limit == 2)
913b6663
TK
976 return(0); /* ignore below */
977
47fc3381 978 if (base1->rec_type < base2->rec_type)
913b6663 979 return(-3);
47fc3381 980 if (base1->rec_type > base2->rec_type)
913b6663 981 return(3);
53176238 982 if (limit == 3)
913b6663
TK
983 return(0); /* ignore below */
984
47fc3381 985 if (base1->key < base2->key)
9a4f151e 986 return(-2);
47fc3381 987 if (base1->key > base2->key)
9a4f151e 988 return(2);
53176238 989 if (limit == 4)
913b6663
TK
990 return(0); /* ignore below */
991
47fc3381
TK
992 if (base1->create_tid == 0) {
993 if (base2->create_tid == 0)
913b6663 994 return(0);
9a4f151e 995 return(1);
913b6663 996 }
47fc3381 997 if (base2->create_tid == 0)
9a4f151e 998 return(-1);
47fc3381 999 if (base1->create_tid < base2->create_tid)
9a4f151e 1000 return(-1);
47fc3381 1001 if (base1->create_tid > base2->create_tid)
9a4f151e 1002 return(1);
913b6663
TK
1003 return(0);
1004}
1005
f29b8e39
TK
1006static __inline
1007int
53176238 1008test_btree_match(hammer_btree_elm_t elm)
f29b8e39 1009{
53176238 1010 if (test_btree_search(elm) == 0)
f29b8e39
TK
1011 return(1);
1012 return(0);
1013}
1014
798cfa06 1015static
f29b8e39 1016int
53176238 1017test_btree_out_of_range(hammer_btree_elm_t elm)
f29b8e39 1018{
53176238
TK
1019 if (test_btree_search(elm) > 0)
1020 return(1); /* conditions < this elm */
798cfa06 1021
686dde96 1022 if (opt.limit >= 5) {
53176238
TK
1023 if (test_btree_search(elm + 1) <= 0)
1024 return(1); /* next elm <= conditions */
798cfa06 1025 } else {
53176238
TK
1026 if (test_btree_search(elm + 1) < 0)
1027 return(1); /* next elm < conditions */
798cfa06 1028 }
f29b8e39
TK
1029 return(0);
1030}
1031
6aec797f
MD
1032/*
1033 * Dump the UNDO FIFO
1034 */
1035void
1036hammer_cmd_show_undo(void)
1037{
2dba5fa7 1038 volume_info_t volume;
6aec797f
MD
1039 hammer_blockmap_t rootmap;
1040 hammer_off_t scan_offset;
1041 hammer_fifo_any_t head;
2615c079 1042 hammer_fifo_head_t hdr;
c17fa600 1043 buffer_info_t data_buffer = NULL;
64321baf 1044 zone_stat_t stats = NULL;
6aec797f 1045
42700c9d 1046 volume = get_root_volume();
6aec797f 1047 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
dce7ae2c
TK
1048
1049 print_blockmap(volume);
9d8235ab 1050
5b334df8
TK
1051 if (VerboseOpt)
1052 stats = hammer_init_zone_stat_bits();
1053
459a6a40 1054 scan_offset = HAMMER_ENCODE_UNDO(0);
6aec797f
MD
1055 while (scan_offset < rootmap->alloc_offset) {
1056 head = get_buffer_data(scan_offset, &data_buffer, 0);
2615c079 1057 hdr = &head->head;
6aec797f
MD
1058 printf("%016jx ", scan_offset);
1059
2615c079 1060 switch(hdr->hdr_type) {
6aec797f 1061 case HAMMER_HEAD_TYPE_PAD:
b480bc95 1062 printf("PAD(%d)", hdr->hdr_size);
6aec797f
MD
1063 break;
1064 case HAMMER_HEAD_TYPE_DUMMY:
b480bc95 1065 printf("DUMMY(%d)\tseq=%08x",
2615c079 1066 hdr->hdr_size, hdr->hdr_seq);
6aec797f
MD
1067 break;
1068 case HAMMER_HEAD_TYPE_UNDO:
1f5e0b99 1069 printf("UNDO(%u)\tseq=%08x offset=%016jx bytes=%d",
2615c079 1070 hdr->hdr_size, hdr->hdr_seq,
1f5e0b99 1071 (uintmax_t)head->undo.undo_offset,
6aec797f
MD
1072 head->undo.undo_data_bytes);
1073 break;
1074 case HAMMER_HEAD_TYPE_REDO:
1f5e0b99 1075 printf("REDO(%u)\tseq=%08x offset=%016jx bytes=%d "
b157444a 1076 "objid=%016jx flags=%08x lo=%08x",
2615c079 1077 hdr->hdr_size, hdr->hdr_seq,
1f5e0b99 1078 (uintmax_t)head->redo.redo_offset,
b157444a 1079 head->redo.redo_data_bytes,
1f5e0b99 1080 (uintmax_t)head->redo.redo_objid,
b157444a
TK
1081 head->redo.redo_flags,
1082 head->redo.redo_localization);
6aec797f
MD
1083 break;
1084 default:
b480bc95 1085 printf("%04x(%d)\tseq=%08x",
2615c079 1086 hdr->hdr_type, hdr->hdr_size, hdr->hdr_seq);
6aec797f
MD
1087 break;
1088 }
9d8235ab
TK
1089
1090 if (scan_offset == rootmap->first_offset)
1091 printf(" >");
1092 if (scan_offset == rootmap->next_offset)
1093 printf(" <");
1094 printf("\n");
1095
dd96867b 1096 if (stats)
2615c079 1097 hammer_add_zone_stat(stats, scan_offset, hdr->hdr_size);
5b334df8 1098
2615c079
TK
1099 if ((hdr->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1100 hdr->hdr_size == 0 ||
1101 hdr->hdr_size > HAMMER_UNDO_ALIGN -
6aec797f
MD
1102 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1103 printf("Illegal size field, skipping to "
1104 "next boundary\n");
19836c70 1105 scan_offset = HAMMER_UNDO_DOALIGN(scan_offset);
6aec797f 1106 } else {
2615c079 1107 scan_offset += hdr->hdr_size;
6aec797f
MD
1108 }
1109 }
3c6df284 1110 rel_buffer(data_buffer);
5b334df8 1111
dd96867b 1112 if (stats) {
5b334df8
TK
1113 hammer_print_zone_stat(stats);
1114 hammer_cleanup_zone_stat(stats);
1115 }
6aec797f 1116}