sbin/hammer: Cleanup blocks with a single statement
[dragonfly.git] / sbin / hammer / cmd_show.c
bloba781bf0d1f4d5eda844efa6cd503784621c08b42
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
35 #include <libutil.h>
37 #include "hammer.h"
39 #define FLAG_TOOFARLEFT 0x0001
40 #define FLAG_TOOFARRIGHT 0x0002
41 #define FLAG_BADTYPE 0x0004
42 #define FLAG_BADCHILDPARENT 0x0008
43 #define FLAG_BADMIRRORTID 0x0010
45 struct {
46 struct hammer_base_elm base;
47 int limit; /* # of fields to test */
48 int filter; /* filter type (default -1) */
49 int obfuscate; /* obfuscate direntry name */
50 int indent; /* use depth indentation */
51 struct zone_stat *stats;
52 } opt;
54 static __inline void print_btree(hammer_off_t node_offset);
55 static __inline void print_subtree(hammer_btree_elm_t elm);
56 static void print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
57 hammer_btree_elm_t lbe);
58 static int test_node_count(hammer_node_ondisk_t node, char *badmp);
59 static void print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
60 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext);
61 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
62 hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
63 static int test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
64 static int test_rbn_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
65 static void print_bigblock_fill(hammer_off_t offset);
66 static const char *check_data_crc(hammer_btree_elm_t elm, const char **whichp);
67 static hammer_crc_t get_leaf_crc(uint32_t vol_version, void *data,
68 hammer_btree_leaf_elm_t leaf, const char **whichp);
69 static uint32_t get_buf_crc(hammer_off_t buf_offset, int32_t buf_len,
70 uint32_t leaf_crc, const char **whichp);
71 static void print_record(hammer_btree_elm_t elm);
72 static int init_btree_search(const char *arg);
73 static int test_btree_search(hammer_btree_elm_t elm);
74 static int test_btree_match(hammer_btree_elm_t elm);
75 static int test_btree_out_of_range(hammer_btree_elm_t elm);
76 static void hexdump_record(const void *ptr, int length, const char *hdr);
78 static int num_bad_node = 0;
79 static int num_bad_elm = 0;
80 static int num_bad_rec = 0;
81 static int depth;
83 #define _X "\t"
84 static const char* _indents[] = {
85 "",
86 _X,
87 _X _X,
88 _X _X _X,
89 _X _X _X _X,
90 _X _X _X _X _X,
91 _X _X _X _X _X _X,
92 _X _X _X _X _X _X _X,
93 _X _X _X _X _X _X _X _X,
94 _X _X _X _X _X _X _X _X _X,
95 _X _X _X _X _X _X _X _X _X _X,
96 _X _X _X _X _X _X _X _X _X _X _X,
97 _X _X _X _X _X _X _X _X _X _X _X _X,
98 _X _X _X _X _X _X _X _X _X _X _X _X _X,
99 _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
100 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
101 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
102 /* deep enough */
104 #define INDENT _indents[opt.indent ? depth : 0]
106 void
107 hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent)
109 struct volume_info *volume;
110 hammer_volume_ondisk_t ondisk;
111 struct zone_stat *stats = NULL;
113 volume = get_root_volume();
114 ondisk = volume->ondisk;
116 print_blockmap(volume);
118 if (VerboseOpt)
119 stats = hammer_init_zone_stat_bits();
121 bzero(&opt, sizeof(opt));
122 opt.filter = filter;
123 opt.obfuscate = obfuscate;
124 opt.indent = indent;
125 opt.stats = stats;
127 if (init_btree_search(arg) > 0) {
128 printf("arg=\"%s\"", arg);
129 if (opt.limit > 0)
130 printf(" lo=%08x", opt.base.localization);
131 if (opt.limit > 1)
132 printf(" objid=%016jx", (uintmax_t)opt.base.obj_id);
133 if (opt.limit > 2)
134 printf(" rt=%02x", opt.base.rec_type);
135 if (opt.limit > 3)
136 printf(" key=%016jx", (uintmax_t)opt.base.key);
137 if (opt.limit > 4)
138 printf(" tid=%016jx", (uintmax_t)opt.base.create_tid);
139 printf("\n");
141 print_btree(ondisk->vol0_btree_root);
143 if (stats) {
144 hammer_print_zone_stat(stats);
145 hammer_cleanup_zone_stat(stats);
148 if (num_bad_node || VerboseOpt)
149 printf("%d bad nodes\n", num_bad_node);
150 if (num_bad_elm || VerboseOpt)
151 printf("%d bad elms\n", num_bad_elm);
152 if (num_bad_rec || VerboseOpt)
153 printf("%d bad records\n", num_bad_rec);
156 static __inline
157 void
158 print_btree(hammer_off_t node_offset)
160 depth = -1;
161 print_btree_node(node_offset, HAMMER_MAX_TID, NULL);
162 assert(depth == -1);
165 static __inline
166 void
167 print_subtree(hammer_btree_elm_t elm)
169 hammer_btree_internal_elm_t i = &elm->internal;
170 print_btree_node(i->subtree_offset, i->mirror_tid, elm);
173 static void
174 print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
175 hammer_btree_elm_t lbe)
177 struct buffer_info *buffer = NULL;
178 hammer_node_ondisk_t node;
179 hammer_btree_elm_t elm;
180 int i;
181 char badc = ' '; /* good */
182 char badm = ' '; /* good */
183 const char *ext;
185 depth++;
186 node = get_buffer_data(node_offset, &buffer, 0);
188 if (node == NULL) {
189 badc = 'B';
190 badm = 'I';
191 } else {
192 if (!hammer_crc_test_btree(HammerVersion, node))
193 badc = 'B';
194 if (node->mirror_tid > mirror_tid) {
195 badc = 'B';
196 badm = 'M';
198 if (test_node_count(node, &badm) == -1) {
199 badc = 'B';
200 assert(badm != ' ');
204 if (badm != ' ' || badc != ' ') /* not good */
205 ++num_bad_node;
207 printf("%s%c%c NODE %016jx ",
208 INDENT, badc, badm, (uintmax_t)node_offset);
209 printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx",
210 node->count,
211 (uintmax_t)node->parent,
212 (node->type ? node->type : '?'),
213 depth,
214 (uintmax_t)node->mirror_tid);
215 printf(" fill=");
216 print_bigblock_fill(node_offset);
217 printf(" {\n");
219 if (opt.stats)
220 hammer_add_zone_stat(opt.stats, node_offset, sizeof(*node));
222 for (i = 0; i < node->count; ++i) {
223 elm = &node->elms[i];
224 ext = NULL;
225 if (opt.limit)
226 switch (node->type) {
227 case HAMMER_BTREE_TYPE_INTERNAL:
228 if (!test_btree_out_of_range(elm))
229 ext = "*";
230 break;
231 case HAMMER_BTREE_TYPE_LEAF:
232 if (test_btree_match(elm))
233 ext = "*";
234 break;
236 print_btree_elm(node, node_offset, elm, lbe, ext);
238 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
239 assert(i == node->count); /* boundary */
240 elm = &node->elms[i];
241 print_btree_elm(node, node_offset, elm, lbe, NULL);
243 printf("%s }\n", INDENT);
245 if (node->type == HAMMER_BTREE_TYPE_INTERNAL)
246 for (i = 0; i < node->count; ++i) {
247 elm = &node->elms[i];
248 if (opt.limit && opt.filter)
249 if (test_btree_out_of_range(elm))
250 continue;
251 if (elm->internal.subtree_offset) {
252 print_subtree(elm);
254 * Cause show to do normal iteration after
255 * seeking to the lo:objid:rt:key:tid
256 * by default
258 if (opt.limit && opt.filter == -1) /* default */
259 opt.filter = 0;
262 rel_buffer(buffer);
263 depth--;
266 static int
267 test_node_count(hammer_node_ondisk_t node, char *badmp)
269 hammer_node_ondisk_t parent_node;
270 struct buffer_info *buffer = NULL;
271 int maxcount;
273 maxcount = hammer_node_max_elements(node->type);
275 if (maxcount == -1) {
276 *badmp = 'U';
277 return(-1);
278 } else if (node->count > maxcount) {
279 *badmp = 'C';
280 return(-1);
281 } else if (node->count == 0) {
282 parent_node = get_buffer_data(node->parent, &buffer, 0);
283 if (parent_node->count != 1) {
284 *badmp = 'C';
285 rel_buffer(buffer);
286 return(-1);
288 rel_buffer(buffer);
291 return(0);
294 static __inline
296 is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm)
299 * elm->base.btype depends on what the original node had
300 * so it could be anything but HAMMER_BTREE_TYPE_NONE.
302 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
303 i == 0 &&
304 elm->base.localization == HAMMER_MIN_ONDISK_LOCALIZATION &&
305 elm->base.obj_id == (int64_t)HAMMER_MIN_OBJID &&
306 elm->base.key == (int64_t)HAMMER_MIN_KEY &&
307 elm->base.create_tid == 1 &&
308 elm->base.delete_tid == 1 &&
309 elm->base.rec_type == HAMMER_MIN_RECTYPE &&
310 elm->base.obj_type == 0 &&
311 elm->base.btype != HAMMER_BTREE_TYPE_NONE);
314 static __inline
316 is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm)
318 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
319 i != 0 &&
320 elm->base.localization == HAMMER_MAX_ONDISK_LOCALIZATION &&
321 elm->base.obj_id == HAMMER_MAX_OBJID &&
322 elm->base.key == HAMMER_MAX_KEY &&
323 elm->base.create_tid == HAMMER_MAX_TID &&
324 elm->base.delete_tid == 0 &&
325 elm->base.rec_type == HAMMER_MAX_RECTYPE &&
326 elm->base.obj_type == 0 &&
327 elm->base.btype == HAMMER_BTREE_TYPE_NONE);
330 static
331 void
332 print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
333 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext)
335 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
336 char deleted;
337 char rootelm;
338 const char *label;
339 const char *p;
340 const char *which;
341 int flags;
342 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
344 flags = get_elm_flags(node, node_offset, elm, lbe);
345 flagstr[0] = flags ? 'B' : 'G';
346 if (flags & FLAG_TOOFARLEFT)
347 flagstr[2] = 'L';
348 if (flags & FLAG_TOOFARRIGHT)
349 flagstr[3] = 'R';
350 if (flags & FLAG_BADTYPE)
351 flagstr[4] = 'T';
352 if (flags & FLAG_BADCHILDPARENT)
353 flagstr[5] = 'C';
354 if (flags & FLAG_BADMIRRORTID)
355 flagstr[6] = 'M';
356 if (flagstr[0] == 'B')
357 ++num_bad_elm;
360 * Check if elm is derived from root split
362 if (is_root_btree_beg(node->type, i, elm))
363 rootelm = '>';
364 else if (is_root_btree_end(node->type, i, elm))
365 rootelm = '<';
366 else
367 rootelm = ' ';
369 if (elm->base.delete_tid)
370 deleted = 'd';
371 else
372 deleted = ' ';
374 if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
375 label = "RBN";
376 else
377 label = "ELM";
379 printf("%s%s %s %2d %c ",
380 INDENT, flagstr, label, i, hammer_elm_btype(elm));
381 printf("lo=%08x objid=%016jx rt=%02x key=%016jx tid=%016jx\n",
382 elm->base.localization,
383 (uintmax_t)elm->base.obj_id,
384 elm->base.rec_type,
385 (uintmax_t)elm->base.key,
386 (uintmax_t)elm->base.create_tid);
387 printf("%s %c del=%016jx ot=%02x",
388 INDENT,
389 (rootelm == ' ' ? deleted : rootelm),
390 (uintmax_t)elm->base.delete_tid,
391 elm->base.obj_type);
393 switch(node->type) {
394 case HAMMER_BTREE_TYPE_INTERNAL:
395 printf(" suboff=%016jx mirror=%016jx",
396 (uintmax_t)elm->internal.subtree_offset,
397 (uintmax_t)elm->internal.mirror_tid);
398 if (ext)
399 printf(" %s", ext);
400 break;
401 case HAMMER_BTREE_TYPE_LEAF:
402 switch(elm->base.btype) {
403 case HAMMER_BTREE_TYPE_RECORD:
404 printf(" dataoff=%016jx/%d",
405 (uintmax_t)elm->leaf.data_offset,
406 elm->leaf.data_len);
407 p = check_data_crc(elm, &which);
408 printf(" %scrc=%08x", which, elm->leaf.data_crc);
409 if (p) {
410 printf(" error=%s", p);
411 ++num_bad_rec;
413 printf(" fill=");
414 print_bigblock_fill(elm->leaf.data_offset);
415 if (QuietOpt < 2)
416 print_record(elm);
417 if (opt.stats)
418 hammer_add_zone_stat(opt.stats,
419 elm->leaf.data_offset,
420 elm->leaf.data_len);
421 break;
422 default:
423 printf(" badtype=%d", elm->base.btype);
424 break;
426 if (ext)
427 printf(" %s", ext);
428 break;
430 printf("\n");
433 static
435 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
436 hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
438 hammer_off_t child_offset;
439 int flags = 0;
440 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
442 switch(node->type) {
443 case HAMMER_BTREE_TYPE_INTERNAL:
444 child_offset = elm->internal.subtree_offset;
445 if (elm->internal.mirror_tid > node->mirror_tid)
446 flags |= FLAG_BADMIRRORTID;
448 if (i == node->count) {
449 if (child_offset != 0)
450 flags |= FLAG_BADCHILDPARENT;
451 switch(elm->base.btype) {
452 case HAMMER_BTREE_TYPE_NONE:
453 flags |= test_rbn_lr(elm, lbe);
454 break;
455 default:
456 flags |= FLAG_BADTYPE;
457 break;
459 } else {
460 if (child_offset == 0) {
461 flags |= FLAG_BADCHILDPARENT;
462 } else {
463 struct buffer_info *buffer = NULL;
464 hammer_node_ondisk_t subnode;
465 subnode = get_buffer_data(child_offset, &buffer, 0);
466 if (subnode == NULL)
467 flags |= FLAG_BADCHILDPARENT;
468 else if (subnode->parent != node_offset)
469 flags |= FLAG_BADCHILDPARENT;
470 rel_buffer(buffer);
472 switch(elm->base.btype) {
473 case HAMMER_BTREE_TYPE_INTERNAL:
474 case HAMMER_BTREE_TYPE_LEAF:
475 flags |= test_lr(elm, lbe);
476 break;
477 default:
478 flags |= FLAG_BADTYPE;
479 break;
482 break;
483 case HAMMER_BTREE_TYPE_LEAF:
484 if (elm->leaf.data_offset == 0)
485 flags |= FLAG_BADCHILDPARENT;
486 if (elm->leaf.data_len == 0)
487 flags |= FLAG_BADCHILDPARENT;
489 if (node->mirror_tid == 0 &&
490 !(node->parent == 0 && node->count == 2))
491 flags |= FLAG_BADMIRRORTID;
492 if (elm->base.create_tid && node->mirror_tid &&
493 elm->base.create_tid > node->mirror_tid)
494 flags |= FLAG_BADMIRRORTID;
495 if (elm->base.delete_tid && node->mirror_tid &&
496 elm->base.delete_tid > node->mirror_tid)
497 flags |= FLAG_BADMIRRORTID;
498 switch(elm->base.btype) {
499 case HAMMER_BTREE_TYPE_RECORD:
500 flags |= test_lr(elm, lbe);
501 break;
502 default:
503 flags |= FLAG_BADTYPE;
504 break;
506 break;
507 default:
508 flags |= FLAG_BADTYPE;
509 break;
511 return(flags);
515 * Taken from /usr/src/sys/vfs/hammer/hammer_btree.c.
517 static
519 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
521 if (key1->localization < key2->localization)
522 return(-5);
523 if (key1->localization > key2->localization)
524 return(5);
526 if (key1->obj_id < key2->obj_id)
527 return(-4);
528 if (key1->obj_id > key2->obj_id)
529 return(4);
531 if (key1->rec_type < key2->rec_type)
532 return(-3);
533 if (key1->rec_type > key2->rec_type)
534 return(3);
536 if (key1->key < key2->key)
537 return(-2);
538 if (key1->key > key2->key)
539 return(2);
541 if (key1->create_tid == 0) {
542 if (key2->create_tid == 0)
543 return(0);
544 return(1);
546 if (key2->create_tid == 0)
547 return(-1);
548 if (key1->create_tid < key2->create_tid)
549 return(-1);
550 if (key1->create_tid > key2->create_tid)
551 return(1);
552 return(0);
555 static
557 test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
559 if (lbe) {
560 hammer_btree_elm_t rbe = lbe + 1;
561 if (hammer_btree_cmp(&elm->base, &lbe->base) < 0)
562 return(FLAG_TOOFARLEFT);
563 if (hammer_btree_cmp(&elm->base, &rbe->base) >= 0)
564 return(FLAG_TOOFARRIGHT);
566 return(0);
569 static
571 test_rbn_lr(hammer_btree_elm_t rbn, hammer_btree_elm_t lbe)
573 if (lbe) {
574 hammer_btree_elm_t rbe = lbe + 1;
575 if (hammer_btree_cmp(&rbn->base, &lbe->base) < 0)
576 return(FLAG_TOOFARLEFT);
577 if (hammer_btree_cmp(&rbn->base, &rbe->base) > 0)
578 return(FLAG_TOOFARRIGHT);
580 return(0);
583 static
584 void
585 print_bigblock_fill(hammer_off_t offset)
587 struct hammer_blockmap_layer1 layer1;
588 struct hammer_blockmap_layer2 layer2;
589 int fill;
590 int error;
592 blockmap_lookup_save(offset, &layer1, &layer2, &error);
593 printf("z%d:v%d:%d:%d:%lu=",
594 HAMMER_ZONE_DECODE(offset),
595 HAMMER_VOL_DECODE(offset),
596 HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
597 HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
598 offset & HAMMER_BIGBLOCK_MASK64);
600 if (error) {
601 printf("B%d", error);
602 } else {
603 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
604 printf("%d%%", 100 - fill);
609 * Check the generic crc on a data element. Inodes record types are
610 * special in that some of their fields are not CRCed.
612 * Also check that the zone is valid.
614 static
615 const char *
616 check_data_crc(hammer_btree_elm_t elm, const char **whichp)
618 struct buffer_info *data_buffer;
619 hammer_off_t data_offset;
620 int32_t data_len;
621 uint32_t crc;
622 char *ptr;
624 *whichp = "";
625 data_offset = elm->leaf.data_offset;
626 data_len = elm->leaf.data_len;
627 data_buffer = NULL;
628 if (data_offset == 0 || data_len == 0)
629 return("ZO"); /* zero offset or length */
631 crc = 0;
632 switch (elm->leaf.base.rec_type) {
633 case HAMMER_RECTYPE_INODE:
634 if (data_len != sizeof(struct hammer_inode_data))
635 return("BI"); /* bad inode size */
636 ptr = get_buffer_data(data_offset, &data_buffer, 0);
637 crc = get_leaf_crc(HammerVersion, ptr, &elm->leaf, whichp);
638 rel_buffer(data_buffer);
639 break;
640 default:
641 crc = get_buf_crc(data_offset, data_len, elm->leaf.data_crc,
642 whichp);
643 break;
646 if (crc == 0)
647 return("Bx"); /* bad crc */
648 if (crc != elm->leaf.data_crc)
649 return("BX"); /* bad crc */
650 return(NULL); /* success */
653 static
654 uint32_t
655 get_buf_crc(hammer_off_t buf_offset, int32_t buf_len, uint32_t leaf_crc,
656 const char **whichp)
658 struct buffer_info *data_buffer = NULL;
659 int32_t len;
660 uint32_t crc = 0;
661 uint32_t ncrc = 0;
662 char *ptr;
664 while (buf_len) {
665 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
666 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
667 if (len > buf_len)
668 len = (int)buf_len;
669 assert(len <= HAMMER_BUFSIZE);
670 crc = crc32_ext(ptr, len, crc);
671 ncrc = iscsi_crc32_ext(ptr, len, ncrc);
672 buf_len -= len;
673 buf_offset += len;
675 rel_buffer(data_buffer);
677 if (leaf_crc == crc) {
678 *whichp = "o";
679 return crc;
681 *whichp = "i";
682 return ncrc;
685 static hammer_crc_t
686 get_leaf_crc(uint32_t vol_version, void *data, hammer_btree_leaf_elm_t leaf,
687 const char **whichp)
689 hammer_crc_t crc;
691 *whichp = "";
692 if (leaf->data_len == 0)
693 return(0);
695 switch(leaf->base.rec_type) {
696 case HAMMER_RECTYPE_INODE:
697 if (leaf->data_len != sizeof(struct hammer_inode_data))
698 return(0); /* This shouldn't happen */
699 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
700 crc = iscsi_crc32(data, HAMMER_INODE_CRCSIZE);
701 if (crc == leaf->data_crc) {
702 *whichp = "i";
703 break;
706 crc = crc32(data, HAMMER_INODE_CRCSIZE);
707 if (crc == leaf->data_crc)
708 *whichp = "o";
709 break;
710 default:
711 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
712 crc = iscsi_crc32(data, leaf->data_len);
713 if (crc == leaf->data_crc) {
714 *whichp = "i";
715 break;
718 crc = crc32(data, leaf->data_len);
719 if (crc == leaf->data_crc)
720 *whichp = "o";
721 break;
723 return(crc);
726 static
727 void
728 print_config(char *cfgtxt)
730 char *token;
732 printf("\n%s%17s", INDENT, "");
733 printf("config text=\"\n");
734 if (cfgtxt != NULL)
735 while((token = strsep(&cfgtxt, "\r\n")) != NULL)
736 if (strlen(token))
737 printf("%s%17s %s\n",
738 INDENT, "", token);
739 printf("%s%17s \"", INDENT, "");
742 static
743 void
744 print_record(hammer_btree_elm_t elm)
746 struct buffer_info *data_buffer;
747 hammer_off_t data_offset;
748 int32_t data_len;
749 hammer_data_ondisk_t data;
750 uint32_t status;
751 char *str1 = NULL;
752 char *str2 = NULL;
754 data_offset = elm->leaf.data_offset;
755 data_len = elm->leaf.data_len;
756 assert(data_offset != 0);
757 assert(data_len != 0);
759 data_buffer = NULL;
760 data = get_buffer_data(data_offset, &data_buffer, 0);
761 assert(data != NULL);
763 switch(elm->leaf.base.rec_type) {
764 case HAMMER_RECTYPE_UNKNOWN:
765 printf("\n%s%17s", INDENT, "");
766 printf("unknown");
767 break;
768 case HAMMER_RECTYPE_INODE:
769 printf("\n%s%17s", INDENT, "");
770 printf("inode size=%jd nlinks=%jd",
771 (intmax_t)data->inode.size,
772 (intmax_t)data->inode.nlinks);
773 printf(" mode=%05o uflags=%08x caps=%02x",
774 data->inode.mode,
775 data->inode.uflags,
776 data->inode.cap_flags);
777 printf(" pobjid=%016jx ot=%02x\n",
778 (uintmax_t)data->inode.parent_obj_id,
779 data->inode.obj_type);
780 printf("%s%17s", INDENT, "");
781 printf(" ctime=%016jx mtime=%016jx atime=%016jx",
782 (uintmax_t)data->inode.ctime,
783 (uintmax_t)data->inode.mtime,
784 (uintmax_t)data->inode.atime);
785 if (data->inode.ext.symlink[0])
786 printf(" symlink=\"%s\"",
787 data->inode.ext.symlink);
788 break;
789 case HAMMER_RECTYPE_DIRENTRY:
790 data_len -= HAMMER_ENTRY_NAME_OFF;
791 printf("\n%s%17s", INDENT, "");
792 printf("dir-entry objid=%016jx lo=%08x",
793 (uintmax_t)data->entry.obj_id,
794 data->entry.localization);
795 if (!opt.obfuscate)
796 printf(" name=\"%*.*s\"",
797 data_len, data_len, data->entry.name);
798 break;
799 case HAMMER_RECTYPE_FIX:
800 switch(elm->leaf.base.key) {
801 case HAMMER_FIXKEY_SYMLINK:
802 data_len -= HAMMER_SYMLINK_NAME_OFF;
803 printf("\n%s%17s", INDENT, "");
804 printf("fix-symlink name=\"%*.*s\"",
805 data_len, data_len, data->symlink.name);
806 break;
808 break;
809 case HAMMER_RECTYPE_PFS:
810 printf("\n%s%17s", INDENT, "");
811 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
812 (intmax_t)data->pfsd.sync_beg_tid,
813 (intmax_t)data->pfsd.sync_end_tid);
814 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
815 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
816 printf("%17s", "");
817 printf(" shared_uuid=%s\n", str1);
818 printf("%17s", "");
819 printf(" unique_uuid=%s\n", str2);
820 printf("%17s", "");
821 printf(" mirror_flags=%08x label=\"%s\"",
822 data->pfsd.mirror_flags, data->pfsd.label);
823 if (data->pfsd.snapshots[0])
824 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
825 free(str1);
826 free(str2);
827 break;
828 case HAMMER_RECTYPE_SNAPSHOT:
829 printf("\n%s%17s", INDENT, "");
830 printf("snapshot tid=%016jx label=\"%s\"",
831 (intmax_t)data->snap.tid, data->snap.label);
832 break;
833 case HAMMER_RECTYPE_CONFIG:
834 if (VerboseOpt > 2) {
835 char *p = strdup(data->config.text);
836 print_config(p);
837 free(p);
839 break;
840 case HAMMER_RECTYPE_DATA:
841 if (VerboseOpt > 3) {
842 printf("\n");
843 hexdump_record(data, data_len, "\t\t ");
845 break;
846 case HAMMER_RECTYPE_EXT:
847 case HAMMER_RECTYPE_DB:
848 if (VerboseOpt > 2) {
849 printf("\n");
850 hexdump_record(data, data_len, "\t\t ");
852 break;
853 default:
854 assert(0);
855 break;
857 rel_buffer(data_buffer);
861 * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
862 * which is 16KB. Passing record data length larger than 16KB to
863 * hexdump(3) is invalid even if the leaf node elm says >16KB data.
865 static void
866 hexdump_record(const void *ptr, int length, const char *hdr)
868 int data_len = length;
870 if (data_len > HAMMER_BUFSIZE) /* XXX */
871 data_len = HAMMER_BUFSIZE;
872 hexdump(ptr, data_len, hdr, 0);
874 if (length > data_len)
875 printf("%s....\n", hdr);
878 static __inline __always_inline
879 unsigned long
880 _strtoul(const char *p, int base)
882 unsigned long retval;
884 errno = 0; /* clear */
885 retval = strtoul(p, NULL, base);
886 if (errno == ERANGE && retval == ULONG_MAX)
887 err(1, "strtoul");
888 return retval;
891 static __inline __always_inline
892 unsigned long long
893 _strtoull(const char *p, int base)
895 unsigned long long retval;
897 errno = 0; /* clear */
898 retval = strtoull(p, NULL, base);
899 if (errno == ERANGE && retval == ULLONG_MAX)
900 err(1, "strtoull");
901 return retval;
904 static int
905 init_btree_search(const char *arg)
907 char *s, *p;
908 int i = 0;
910 bzero(&opt.base, sizeof(opt.base));
911 opt.limit = 0;
913 if (arg == NULL)
914 return(-1);
915 if (strcmp(arg, "none") == 0)
916 return(-1);
918 s = strdup(arg);
919 if (s == NULL)
920 return(-1);
922 while ((p = s) != NULL) {
923 if ((s = strchr(s, ':')) != NULL)
924 *s++ = 0;
925 if (++i == 1) {
926 opt.base.localization = _strtoul(p, 16);
927 } else if (i == 2) {
928 opt.base.obj_id = _strtoull(p, 16);
929 } else if (i == 3) {
930 opt.base.rec_type = _strtoul(p, 16);
931 } else if (i == 4) {
932 opt.base.key = _strtoull(p, 16);
933 } else if (i == 5) {
934 opt.base.create_tid = _strtoull(p, 16);
935 break;
938 opt.limit = i;
939 free(s);
941 return(i);
944 static int
945 test_btree_search(hammer_btree_elm_t elm)
947 hammer_base_elm_t base1 = &elm->base;
948 hammer_base_elm_t base2 = &opt.base;
949 int limit = opt.limit;
951 if (base1->localization < base2->localization)
952 return(-5);
953 if (base1->localization > base2->localization)
954 return(5);
955 if (limit == 1)
956 return(0); /* ignore below */
958 if (base1->obj_id < base2->obj_id)
959 return(-4);
960 if (base1->obj_id > base2->obj_id)
961 return(4);
962 if (limit == 2)
963 return(0); /* ignore below */
965 if (base1->rec_type < base2->rec_type)
966 return(-3);
967 if (base1->rec_type > base2->rec_type)
968 return(3);
969 if (limit == 3)
970 return(0); /* ignore below */
972 if (base1->key < base2->key)
973 return(-2);
974 if (base1->key > base2->key)
975 return(2);
976 if (limit == 4)
977 return(0); /* ignore below */
979 if (base1->create_tid == 0) {
980 if (base2->create_tid == 0)
981 return(0);
982 return(1);
984 if (base2->create_tid == 0)
985 return(-1);
986 if (base1->create_tid < base2->create_tid)
987 return(-1);
988 if (base1->create_tid > base2->create_tid)
989 return(1);
990 return(0);
993 static __inline
995 test_btree_match(hammer_btree_elm_t elm)
997 if (test_btree_search(elm) == 0)
998 return(1);
999 return(0);
1002 static
1004 test_btree_out_of_range(hammer_btree_elm_t elm)
1006 if (test_btree_search(elm) > 0)
1007 return(1); /* conditions < this elm */
1009 if (opt.limit >= 5) {
1010 if (test_btree_search(elm + 1) <= 0)
1011 return(1); /* next elm <= conditions */
1012 } else {
1013 if (test_btree_search(elm + 1) < 0)
1014 return(1); /* next elm < conditions */
1016 return(0);
1020 * Dump the UNDO FIFO
1022 void
1023 hammer_cmd_show_undo(void)
1025 struct volume_info *volume;
1026 hammer_blockmap_t rootmap;
1027 hammer_off_t scan_offset;
1028 hammer_fifo_any_t head;
1029 hammer_fifo_head_t hdr;
1030 struct buffer_info *data_buffer = NULL;
1031 struct zone_stat *stats = NULL;
1033 volume = get_root_volume();
1034 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
1036 print_blockmap(volume);
1038 if (VerboseOpt)
1039 stats = hammer_init_zone_stat_bits();
1041 scan_offset = HAMMER_ENCODE_UNDO(0);
1042 while (scan_offset < rootmap->alloc_offset) {
1043 head = get_buffer_data(scan_offset, &data_buffer, 0);
1044 hdr = &head->head;
1045 printf("%016jx ", scan_offset);
1047 switch(hdr->hdr_type) {
1048 case HAMMER_HEAD_TYPE_PAD:
1049 printf("PAD(%d)", hdr->hdr_size);
1050 break;
1051 case HAMMER_HEAD_TYPE_DUMMY:
1052 printf("DUMMY(%d)\tseq=%08x",
1053 hdr->hdr_size, hdr->hdr_seq);
1054 break;
1055 case HAMMER_HEAD_TYPE_UNDO:
1056 printf("UNDO(%d)\tseq=%08x offset=%016jx bytes=%d",
1057 hdr->hdr_size, hdr->hdr_seq,
1058 (intmax_t)head->undo.undo_offset,
1059 head->undo.undo_data_bytes);
1060 break;
1061 case HAMMER_HEAD_TYPE_REDO:
1062 printf("REDO(%d)\tseq=%08x offset=%016jx bytes=%d "
1063 "objid=%016jx flags=%08x lo=%08x",
1064 hdr->hdr_size, hdr->hdr_seq,
1065 (intmax_t)head->redo.redo_offset,
1066 head->redo.redo_data_bytes,
1067 (intmax_t)head->redo.redo_objid,
1068 head->redo.redo_flags,
1069 head->redo.redo_localization);
1070 break;
1071 default:
1072 printf("%04x(%d)\tseq=%08x",
1073 hdr->hdr_type, hdr->hdr_size, hdr->hdr_seq);
1074 break;
1077 if (scan_offset == rootmap->first_offset)
1078 printf(" >");
1079 if (scan_offset == rootmap->next_offset)
1080 printf(" <");
1081 printf("\n");
1083 if (stats)
1084 hammer_add_zone_stat(stats, scan_offset, hdr->hdr_size);
1086 if ((hdr->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1087 hdr->hdr_size == 0 ||
1088 hdr->hdr_size > HAMMER_UNDO_ALIGN -
1089 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1090 printf("Illegal size field, skipping to "
1091 "next boundary\n");
1092 scan_offset = HAMMER_UNDO_DOALIGN(scan_offset);
1093 } else {
1094 scan_offset += hdr->hdr_size;
1097 rel_buffer(data_buffer);
1099 if (stats) {
1100 hammer_print_zone_stat(stats);
1101 hammer_cleanup_zone_stat(stats);