kernel/acpi: Remove the old APM emulation code.
[dragonfly.git] / sbin / hammer / cmd_show.c
blobd2d02374167dece1521844259f8fcb64f8adc471
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 "hammer.h"
37 #include <sys/tree.h>
38 #include <libutil.h>
40 #define FLAG_TOOFARLEFT 0x0001
41 #define FLAG_TOOFARRIGHT 0x0002
42 #define FLAG_BADTYPE 0x0004
43 #define FLAG_BADCHILDPARENT 0x0008
44 #define FLAG_BADMIRRORTID 0x0010
46 struct {
47 struct hammer_base_elm base;
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 */
52 zone_stat_t stats;
53 } opt;
55 static __inline void print_btree(hammer_off_t node_offset);
56 static __inline void print_subtree(hammer_btree_elm_t elm);
57 static void print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
58 hammer_btree_elm_t lbe);
59 static int test_node_count(hammer_node_ondisk_t node, char *badmp);
60 static void print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
61 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext);
62 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
63 hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
64 static int test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
65 static int test_rbn_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
66 static void print_bigblock_fill(hammer_off_t offset);
67 static const char *check_data_crc(hammer_btree_elm_t elm, const char **whichp);
68 static hammer_crc_t get_inode_crc(hammer_btree_leaf_elm_t leaf,
69 const char **whichp);
70 static hammer_crc_t get_buf_crc(hammer_btree_leaf_elm_t leaf,
71 const char **whichp);
72 static void print_record(hammer_btree_elm_t elm);
73 static int init_btree_search(const char *arg);
74 static int test_btree_search(hammer_btree_elm_t elm);
75 static int test_btree_match(hammer_btree_elm_t elm);
76 static int test_btree_out_of_range(hammer_btree_elm_t elm);
77 static void hexdump_record(const void *ptr, int length, const char *hdr);
79 static int num_bad_node = 0;
80 static int num_bad_elm = 0;
81 static int num_bad_rec = 0;
82 static int depth;
84 #define _X "\t"
85 static const char* _indents[] = {
86 "",
87 _X,
88 _X _X,
89 _X _X _X,
90 _X _X _X _X,
91 _X _X _X _X _X,
92 _X _X _X _X _X _X,
93 _X _X _X _X _X _X _X,
94 _X _X _X _X _X _X _X _X,
95 _X _X _X _X _X _X _X _X _X,
96 _X _X _X _X _X _X _X _X _X _X,
97 _X _X _X _X _X _X _X _X _X _X _X,
98 _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,
100 _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,
102 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
103 /* deep enough */
105 #define INDENT _indents[opt.indent ? depth : 0]
107 void
108 hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent)
110 volume_info_t volume;
111 hammer_volume_ondisk_t ondisk;
112 zone_stat_t stats = NULL;
114 volume = get_root_volume();
115 ondisk = volume->ondisk;
117 print_blockmap(volume);
119 if (VerboseOpt)
120 stats = hammer_init_zone_stat_bits();
122 bzero(&opt, sizeof(opt));
123 opt.filter = filter;
124 opt.obfuscate = obfuscate;
125 opt.indent = indent;
126 opt.stats = stats;
128 if (init_btree_search(arg) > 0) {
129 printf("arg=\"%s\"", arg);
130 if (opt.limit > 0)
131 printf(" lo=%08x", opt.base.localization);
132 if (opt.limit > 1)
133 printf(" objid=%016jx", (uintmax_t)opt.base.obj_id);
134 if (opt.limit > 2)
135 printf(" rt=%02x", opt.base.rec_type);
136 if (opt.limit > 3)
137 printf(" key=%016jx", (uintmax_t)opt.base.key);
138 if (opt.limit > 4)
139 printf(" tid=%016jx", (uintmax_t)opt.base.create_tid);
140 printf("\n");
142 print_btree(ondisk->vol0_btree_root);
144 if (stats) {
145 hammer_print_zone_stat(stats);
146 hammer_cleanup_zone_stat(stats);
149 if (num_bad_node || VerboseOpt)
150 printf("%d bad nodes\n", num_bad_node);
151 if (num_bad_elm || VerboseOpt)
152 printf("%d bad elms\n", num_bad_elm);
153 if (num_bad_rec || VerboseOpt)
154 printf("%d bad records\n", num_bad_rec);
157 static __inline
158 void
159 print_btree(hammer_off_t node_offset)
161 depth = -1;
162 print_btree_node(node_offset, HAMMER_MAX_TID, NULL);
163 assert(depth == -1);
166 static __inline
167 void
168 print_subtree(hammer_btree_elm_t elm)
170 hammer_btree_internal_elm_t i = &elm->internal;
171 print_btree_node(i->subtree_offset, i->mirror_tid, elm);
174 static
175 void
176 print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
177 hammer_btree_elm_t lbe)
179 buffer_info_t buffer = NULL;
180 hammer_node_ondisk_t node;
181 hammer_btree_elm_t elm;
182 int i;
183 char badc = ' '; /* good */
184 char badm = ' '; /* good */
185 const char *ext;
187 depth++;
188 node = get_buffer_data(node_offset, &buffer, 0);
190 if (node == NULL) {
191 badc = 'B';
192 badm = 'I';
193 } else {
194 if (!hammer_crc_test_btree(HammerVersion, node))
195 badc = 'B';
196 if (node->mirror_tid > mirror_tid) {
197 badc = 'B';
198 badm = 'M';
200 if (test_node_count(node, &badm) == -1) {
201 badc = 'B';
202 assert(badm != ' ');
206 if (badm != ' ' || badc != ' ') /* not good */
207 ++num_bad_node;
209 printf("%s%c%c NODE %016jx ",
210 INDENT, badc, badm, (uintmax_t)node_offset);
211 printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx",
212 node->count,
213 (uintmax_t)node->parent,
214 (node->type ? node->type : '?'),
215 depth,
216 (uintmax_t)node->mirror_tid);
217 printf(" fill=");
218 print_bigblock_fill(node_offset);
219 printf(" {\n");
221 if (opt.stats)
222 hammer_add_zone_stat(opt.stats, node_offset, sizeof(*node));
224 for (i = 0; i < node->count; ++i) {
225 elm = &node->elms[i];
226 ext = NULL;
227 if (opt.limit) {
228 switch (node->type) {
229 case HAMMER_BTREE_TYPE_INTERNAL:
230 if (!test_btree_out_of_range(elm))
231 ext = "*";
232 break;
233 case HAMMER_BTREE_TYPE_LEAF:
234 if (test_btree_match(elm))
235 ext = "*";
236 break;
239 print_btree_elm(node, node_offset, elm, lbe, ext);
241 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
242 assert(i == node->count); /* boundary */
243 elm = &node->elms[i];
244 print_btree_elm(node, node_offset, elm, lbe, NULL);
246 printf("%s }\n", INDENT);
248 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
249 for (i = 0; i < node->count; ++i) {
250 elm = &node->elms[i];
251 if (opt.limit && opt.filter) {
252 if (test_btree_out_of_range(elm))
253 continue;
255 if (elm->internal.subtree_offset) {
256 print_subtree(elm);
258 * Cause show to do normal iteration after
259 * seeking to the lo:objid:rt:key:tid
260 * by default
262 if (opt.limit && opt.filter == -1) /* default */
263 opt.filter = 0;
267 rel_buffer(buffer);
268 depth--;
271 static
273 test_node_count(hammer_node_ondisk_t node, char *badmp)
275 hammer_node_ondisk_t parent_node;
276 buffer_info_t buffer = NULL;
277 int maxcount;
279 maxcount = hammer_node_max_elements(node->type);
281 if (maxcount == -1) {
282 *badmp = 'U';
283 return(-1);
284 } else if (node->count > maxcount) {
285 *badmp = 'C';
286 return(-1);
287 } else if (node->count == 0) {
288 parent_node = get_buffer_data(node->parent, &buffer, 0);
289 if (parent_node->count != 1) {
290 *badmp = 'C';
291 rel_buffer(buffer);
292 return(-1);
294 rel_buffer(buffer);
297 return(0);
300 static __inline
302 is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm)
305 * elm->base.btype depends on what the original node had
306 * so it could be anything but HAMMER_BTREE_TYPE_NONE.
308 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
309 i == 0 &&
310 elm->base.localization == HAMMER_MIN_ONDISK_LOCALIZATION &&
311 elm->base.obj_id == (int64_t)HAMMER_MIN_OBJID &&
312 elm->base.key == (int64_t)HAMMER_MIN_KEY &&
313 elm->base.create_tid == 1 &&
314 elm->base.delete_tid == 1 &&
315 elm->base.rec_type == HAMMER_MIN_RECTYPE &&
316 elm->base.obj_type == 0 &&
317 elm->base.btype != HAMMER_BTREE_TYPE_NONE);
320 static __inline
322 is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm)
324 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
325 i != 0 &&
326 elm->base.localization == HAMMER_MAX_ONDISK_LOCALIZATION &&
327 elm->base.obj_id == HAMMER_MAX_OBJID &&
328 elm->base.key == HAMMER_MAX_KEY &&
329 elm->base.create_tid == HAMMER_MAX_TID &&
330 elm->base.delete_tid == 0 &&
331 elm->base.rec_type == HAMMER_MAX_RECTYPE &&
332 elm->base.obj_type == 0 &&
333 elm->base.btype == HAMMER_BTREE_TYPE_NONE);
336 static
337 void
338 print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
339 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext)
341 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
342 char deleted;
343 char rootelm;
344 const char *label;
345 const char *p;
346 const char *which;
347 int flags;
348 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
350 flags = get_elm_flags(node, node_offset, elm, lbe);
351 flagstr[0] = flags ? 'B' : 'G';
352 if (flags & FLAG_TOOFARLEFT)
353 flagstr[2] = 'L';
354 if (flags & FLAG_TOOFARRIGHT)
355 flagstr[3] = 'R';
356 if (flags & FLAG_BADTYPE)
357 flagstr[4] = 'T';
358 if (flags & FLAG_BADCHILDPARENT)
359 flagstr[5] = 'C';
360 if (flags & FLAG_BADMIRRORTID)
361 flagstr[6] = 'M';
362 if (flagstr[0] == 'B')
363 ++num_bad_elm;
366 * Check if elm is derived from root split
368 if (is_root_btree_beg(node->type, i, elm))
369 rootelm = '>';
370 else if (is_root_btree_end(node->type, i, elm))
371 rootelm = '<';
372 else
373 rootelm = ' ';
375 if (elm->base.delete_tid)
376 deleted = 'd';
377 else
378 deleted = ' ';
380 if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
381 label = "RBN";
382 else
383 label = "ELM";
385 printf("%s%s %s %2d %c ",
386 INDENT, flagstr, label, i, hammer_elm_btype(elm));
387 printf("lo=%08x objid=%016jx rt=%02x key=%016jx tid=%016jx\n",
388 elm->base.localization,
389 (uintmax_t)elm->base.obj_id,
390 elm->base.rec_type,
391 (uintmax_t)elm->base.key,
392 (uintmax_t)elm->base.create_tid);
393 printf("%s %c del=%016jx ot=%02x",
394 INDENT,
395 (rootelm == ' ' ? deleted : rootelm),
396 (uintmax_t)elm->base.delete_tid,
397 elm->base.obj_type);
399 switch(node->type) {
400 case HAMMER_BTREE_TYPE_INTERNAL:
401 printf(" suboff=%016jx mirror=%016jx",
402 (uintmax_t)elm->internal.subtree_offset,
403 (uintmax_t)elm->internal.mirror_tid);
404 if (ext)
405 printf(" %s", ext);
406 break;
407 case HAMMER_BTREE_TYPE_LEAF:
408 switch(elm->base.btype) {
409 case HAMMER_BTREE_TYPE_RECORD:
410 printf(" dataoff=%016jx/%d",
411 (uintmax_t)elm->leaf.data_offset,
412 elm->leaf.data_len);
413 p = check_data_crc(elm, &which);
414 printf(" %scrc=%08x", which, elm->leaf.data_crc);
415 if (p) {
416 printf(" error=%s", p);
417 ++num_bad_rec;
419 printf(" fill=");
420 print_bigblock_fill(elm->leaf.data_offset);
421 if (QuietOpt < 2)
422 print_record(elm);
423 if (opt.stats) {
424 hammer_add_zone_stat(opt.stats,
425 elm->leaf.data_offset,
426 elm->leaf.data_len);
428 break;
429 default:
430 printf(" badtype=%d", elm->base.btype);
431 break;
433 if (ext)
434 printf(" %s", ext);
435 break;
437 printf("\n");
440 static
442 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
443 hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
445 hammer_off_t child_offset;
446 int flags = 0;
447 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
449 switch(node->type) {
450 case HAMMER_BTREE_TYPE_INTERNAL:
451 child_offset = elm->internal.subtree_offset;
452 if (elm->internal.mirror_tid > node->mirror_tid)
453 flags |= FLAG_BADMIRRORTID;
455 if (i == node->count) {
456 if (child_offset != 0)
457 flags |= FLAG_BADCHILDPARENT;
458 switch(elm->base.btype) {
459 case HAMMER_BTREE_TYPE_NONE:
460 flags |= test_rbn_lr(elm, lbe);
461 break;
462 default:
463 flags |= FLAG_BADTYPE;
464 break;
466 } else {
467 if (child_offset == 0) {
468 flags |= FLAG_BADCHILDPARENT;
469 } else {
470 buffer_info_t buffer = NULL;
471 hammer_node_ondisk_t subnode;
472 subnode = get_buffer_data(child_offset, &buffer, 0);
473 if (subnode == NULL)
474 flags |= FLAG_BADCHILDPARENT;
475 else if (subnode->parent != node_offset)
476 flags |= FLAG_BADCHILDPARENT;
477 rel_buffer(buffer);
479 switch(elm->base.btype) {
480 case HAMMER_BTREE_TYPE_INTERNAL:
481 case HAMMER_BTREE_TYPE_LEAF:
482 flags |= test_lr(elm, lbe);
483 break;
484 default:
485 flags |= FLAG_BADTYPE;
486 break;
489 break;
490 case HAMMER_BTREE_TYPE_LEAF:
491 if (elm->leaf.data_offset == 0)
492 flags |= FLAG_BADCHILDPARENT;
493 if (elm->leaf.data_len == 0)
494 flags |= FLAG_BADCHILDPARENT;
496 if (node->mirror_tid == 0 &&
497 !(node->parent == 0 && node->count == 2)) {
498 flags |= FLAG_BADMIRRORTID;
500 if (elm->base.create_tid && node->mirror_tid &&
501 elm->base.create_tid > node->mirror_tid) {
502 flags |= FLAG_BADMIRRORTID;
504 if (elm->base.delete_tid && node->mirror_tid &&
505 elm->base.delete_tid > node->mirror_tid) {
506 flags |= FLAG_BADMIRRORTID;
508 switch(elm->base.btype) {
509 case HAMMER_BTREE_TYPE_RECORD:
510 flags |= test_lr(elm, lbe);
511 break;
512 default:
513 flags |= FLAG_BADTYPE;
514 break;
516 break;
517 default:
518 flags |= FLAG_BADTYPE;
519 break;
521 return(flags);
525 * Taken from /usr/src/sys/vfs/hammer/hammer_btree.c.
527 static
529 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
531 if (key1->localization < key2->localization)
532 return(-5);
533 if (key1->localization > key2->localization)
534 return(5);
536 if (key1->obj_id < key2->obj_id)
537 return(-4);
538 if (key1->obj_id > key2->obj_id)
539 return(4);
541 if (key1->rec_type < key2->rec_type)
542 return(-3);
543 if (key1->rec_type > key2->rec_type)
544 return(3);
546 if (key1->key < key2->key)
547 return(-2);
548 if (key1->key > key2->key)
549 return(2);
551 if (key1->create_tid == 0) {
552 if (key2->create_tid == 0)
553 return(0);
554 return(1);
556 if (key2->create_tid == 0)
557 return(-1);
558 if (key1->create_tid < key2->create_tid)
559 return(-1);
560 if (key1->create_tid > key2->create_tid)
561 return(1);
562 return(0);
565 static
567 test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
569 if (lbe) {
570 hammer_btree_elm_t rbe = lbe + 1;
571 if (hammer_btree_cmp(&elm->base, &lbe->base) < 0)
572 return(FLAG_TOOFARLEFT);
573 if (hammer_btree_cmp(&elm->base, &rbe->base) >= 0)
574 return(FLAG_TOOFARRIGHT);
576 return(0);
579 static
581 test_rbn_lr(hammer_btree_elm_t rbn, hammer_btree_elm_t lbe)
583 if (lbe) {
584 hammer_btree_elm_t rbe = lbe + 1;
585 if (hammer_btree_cmp(&rbn->base, &lbe->base) < 0)
586 return(FLAG_TOOFARLEFT);
587 if (hammer_btree_cmp(&rbn->base, &rbe->base) > 0)
588 return(FLAG_TOOFARRIGHT);
590 return(0);
593 static
594 void
595 print_bigblock_fill(hammer_off_t offset)
597 struct hammer_blockmap_layer1 layer1;
598 struct hammer_blockmap_layer2 layer2;
599 int fill;
600 int error;
602 blockmap_lookup_save(offset, &layer1, &layer2, &error);
603 printf("z%d:v%d:%d:%d:%lu=",
604 HAMMER_ZONE_DECODE(offset),
605 HAMMER_VOL_DECODE(offset),
606 HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
607 HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
608 offset & HAMMER_BIGBLOCK_MASK64);
610 if (error) {
611 printf("B%d", error);
612 } else {
613 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
614 printf("%d%%", 100 - fill);
619 * Check the generic crc on a data element. Inodes record types are
620 * special in that some of their fields are not CRCed.
622 * Also check that the zone is valid.
624 static
625 const char *
626 check_data_crc(hammer_btree_elm_t elm, const char **whichp)
628 hammer_crc_t crc;
630 *whichp = "";
631 if (elm->leaf.data_offset == 0 || elm->leaf.data_len == 0)
632 return("ZO"); /* zero offset or length */
634 switch (elm->leaf.base.rec_type) {
635 case HAMMER_RECTYPE_INODE:
636 if (elm->leaf.data_len != sizeof(struct hammer_inode_data))
637 return("BI"); /* bad inode size */
638 crc = get_inode_crc(&elm->leaf, whichp);
639 break;
640 default:
641 crc = get_buf_crc(&elm->leaf, whichp);
642 break;
645 if (crc == 0)
646 return("Bx"); /* bad crc */
647 if (crc != elm->leaf.data_crc)
648 return("BX"); /* bad crc */
649 return(NULL); /* success */
652 static
653 hammer_crc_t
654 get_inode_crc(hammer_btree_leaf_elm_t leaf, const char **whichp)
656 buffer_info_t data_buffer = NULL;
657 hammer_crc_t crc;
658 char *ptr;
660 ptr = get_buffer_data(leaf->data_offset, &data_buffer, 0);
662 if (HammerVersion >= HAMMER_VOL_VERSION_SEVEN) {
663 crc = iscsi_crc32(ptr, HAMMER_INODE_CRCSIZE);
664 if (crc == leaf->data_crc) {
665 *whichp = "i";
666 goto end;
670 crc = crc32(ptr, HAMMER_INODE_CRCSIZE);
671 if (crc == leaf->data_crc) {
672 *whichp = "o";
673 goto end;
676 *whichp = "";
677 end:
678 rel_buffer(data_buffer);
679 return(crc);
682 typedef uint32_t (*crc32_ext_fn)(const void *, size_t, uint32_t);
684 static
685 hammer_crc_t
686 __get_buf_crc(hammer_btree_leaf_elm_t leaf, crc32_ext_fn f)
688 buffer_info_t data_buffer = NULL;
689 hammer_off_t buf_offset;
690 hammer_crc_t crc = 0;
691 int32_t buf_len, len;
692 char *ptr;
694 buf_offset = leaf->data_offset;
695 buf_len = leaf->data_len;
697 while (buf_len) {
698 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
699 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
700 if (len > buf_len)
701 len = (int)buf_len;
702 assert(len <= HAMMER_BUFSIZE);
703 crc = f(ptr, len, crc);
704 buf_len -= len;
705 buf_offset += len;
707 rel_buffer(data_buffer);
709 return(crc);
712 static
713 hammer_crc_t
714 get_buf_crc(hammer_btree_leaf_elm_t leaf, const char **whichp)
716 hammer_crc_t crc;
718 if (HammerVersion >= HAMMER_VOL_VERSION_SEVEN) {
719 crc = __get_buf_crc(leaf, iscsi_crc32_ext);
720 if (crc == leaf->data_crc) {
721 *whichp = "i";
722 goto end;
726 crc = __get_buf_crc(leaf, crc32_ext);
727 if (crc == leaf->data_crc) {
728 *whichp = "o";
729 goto end;
732 *whichp = "";
733 end:
734 return(crc);
737 static
738 void
739 print_config(char *cfgtxt)
741 char *token;
743 printf("\n%s%17s", INDENT, "");
744 printf("config text=\"\n");
745 if (cfgtxt != NULL) {
746 while((token = strsep(&cfgtxt, "\r\n")) != NULL) {
747 if (strlen(token)) {
748 printf("%s%17s %s\n",
749 INDENT, "", token);
753 printf("%s%17s \"", INDENT, "");
756 static
757 void
758 print_record(hammer_btree_elm_t elm)
760 buffer_info_t data_buffer;
761 hammer_off_t data_offset;
762 int32_t data_len;
763 hammer_data_ondisk_t data;
764 char *str1 = NULL;
765 char *str2 = NULL;
767 data_offset = elm->leaf.data_offset;
768 data_len = elm->leaf.data_len;
769 assert(data_offset != 0);
770 assert(data_len != 0);
772 data_buffer = NULL;
773 data = get_buffer_data(data_offset, &data_buffer, 0);
774 assert(data != NULL);
776 switch(elm->leaf.base.rec_type) {
777 case HAMMER_RECTYPE_UNKNOWN:
778 printf("\n%s%17s", INDENT, "");
779 printf("unknown");
780 break;
781 case HAMMER_RECTYPE_INODE:
782 printf("\n%s%17s", INDENT, "");
783 printf("inode size=%jd nlinks=%jd",
784 (intmax_t)data->inode.size,
785 (intmax_t)data->inode.nlinks);
786 printf(" mode=%05o uflags=%08x caps=%02x",
787 data->inode.mode,
788 data->inode.uflags,
789 data->inode.cap_flags);
790 printf(" pobjid=%016jx ot=%02x\n",
791 (uintmax_t)data->inode.parent_obj_id,
792 data->inode.obj_type);
793 printf("%s%17s", INDENT, "");
794 printf(" ctime=%016jx mtime=%016jx atime=%016jx",
795 (uintmax_t)data->inode.ctime,
796 (uintmax_t)data->inode.mtime,
797 (uintmax_t)data->inode.atime);
798 if (data->inode.ext.symlink[0]) {
799 printf(" symlink=\"%s\"",
800 data->inode.ext.symlink);
802 break;
803 case HAMMER_RECTYPE_DIRENTRY:
804 data_len -= HAMMER_ENTRY_NAME_OFF;
805 printf("\n%s%17s", INDENT, "");
806 printf("dir-entry objid=%016jx lo=%08x",
807 (uintmax_t)data->entry.obj_id,
808 data->entry.localization);
809 if (!opt.obfuscate) {
810 printf(" name=\"%*.*s\"",
811 data_len, data_len, data->entry.name);
813 break;
814 case HAMMER_RECTYPE_FIX:
815 switch(elm->leaf.base.key) {
816 case HAMMER_FIXKEY_SYMLINK:
817 data_len -= HAMMER_SYMLINK_NAME_OFF;
818 printf("\n%s%17s", INDENT, "");
819 printf("fix-symlink name=\"%*.*s\"",
820 data_len, data_len, data->symlink.name);
821 break;
823 break;
824 case HAMMER_RECTYPE_PFS:
825 printf("\n%s%17s", INDENT, "");
826 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
827 (intmax_t)data->pfsd.sync_beg_tid,
828 (intmax_t)data->pfsd.sync_end_tid);
829 hammer_uuid_to_string(&data->pfsd.shared_uuid, &str1);
830 hammer_uuid_to_string(&data->pfsd.unique_uuid, &str2);
831 printf("%17s", "");
832 printf(" shared_uuid=%s\n", str1);
833 printf("%17s", "");
834 printf(" unique_uuid=%s\n", str2);
835 printf("%17s", "");
836 printf(" mirror_flags=%08x label=\"%s\"",
837 data->pfsd.mirror_flags, data->pfsd.label);
838 if (data->pfsd.snapshots[0])
839 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
840 free(str1);
841 free(str2);
842 break;
843 case HAMMER_RECTYPE_SNAPSHOT:
844 printf("\n%s%17s", INDENT, "");
845 printf("snapshot tid=%016jx label=\"%s\"",
846 (intmax_t)data->snap.tid, data->snap.label);
847 break;
848 case HAMMER_RECTYPE_CONFIG:
849 if (VerboseOpt > 2) {
850 char *p = strdup(data->config.text);
851 print_config(p);
852 free(p);
854 break;
855 case HAMMER_RECTYPE_DATA:
856 if (VerboseOpt > 3) {
857 printf("\n");
858 hexdump_record(data, data_len, "\t\t ");
860 break;
861 case HAMMER_RECTYPE_EXT:
862 case HAMMER_RECTYPE_DB:
863 if (VerboseOpt > 2) {
864 printf("\n");
865 hexdump_record(data, data_len, "\t\t ");
867 break;
868 default:
869 assert(0);
870 break;
872 rel_buffer(data_buffer);
876 * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
877 * which is 16KB. Passing record data length larger than 16KB to
878 * hexdump(3) is invalid even if the leaf node elm says >16KB data.
880 static
881 void
882 hexdump_record(const void *ptr, int length, const char *hdr)
884 int data_len = length;
886 if (data_len > HAMMER_BUFSIZE) /* XXX */
887 data_len = HAMMER_BUFSIZE;
888 hexdump(ptr, data_len, hdr, 0);
890 if (length > data_len)
891 printf("%s....\n", hdr);
894 static __inline __always_inline
895 unsigned long
896 _strtoul(const char *p, int base)
898 unsigned long retval;
900 errno = 0; /* clear */
901 retval = strtoul(p, NULL, base);
902 if (errno == ERANGE && retval == ULONG_MAX) {
903 err(1, "strtoul");
904 /* not reached */
906 return retval;
909 static __inline __always_inline
910 unsigned long long
911 _strtoull(const char *p, int base)
913 unsigned long long retval;
915 errno = 0; /* clear */
916 retval = strtoull(p, NULL, base);
917 if (errno == ERANGE && retval == ULLONG_MAX) {
918 err(1, "strtoull");
919 /* not reached */
921 return retval;
924 static
926 init_btree_search(const char *arg)
928 char *s, *p;
929 int i = 0;
931 bzero(&opt.base, sizeof(opt.base));
932 opt.limit = 0;
934 if (arg == NULL)
935 return(-1);
936 if (strcmp(arg, "none") == 0)
937 return(-1);
939 s = strdup(arg);
940 if (s == NULL)
941 return(-1);
943 while ((p = s) != NULL) {
944 if ((s = strchr(s, ':')) != NULL)
945 *s++ = 0;
946 if (++i == 1) {
947 opt.base.localization = _strtoul(p, 16);
948 } else if (i == 2) {
949 opt.base.obj_id = _strtoull(p, 16);
950 } else if (i == 3) {
951 opt.base.rec_type = _strtoul(p, 16);
952 } else if (i == 4) {
953 opt.base.key = _strtoull(p, 16);
954 } else if (i == 5) {
955 opt.base.create_tid = _strtoull(p, 16);
956 break;
959 opt.limit = i;
960 free(s);
962 return(i);
965 static
967 test_btree_search(hammer_btree_elm_t elm)
969 hammer_base_elm_t base1 = &elm->base;
970 hammer_base_elm_t base2 = &opt.base;
971 int limit = opt.limit;
973 if (base1->localization < base2->localization)
974 return(-5);
975 if (base1->localization > base2->localization)
976 return(5);
977 if (limit == 1)
978 return(0); /* ignore below */
980 if (base1->obj_id < base2->obj_id)
981 return(-4);
982 if (base1->obj_id > base2->obj_id)
983 return(4);
984 if (limit == 2)
985 return(0); /* ignore below */
987 if (base1->rec_type < base2->rec_type)
988 return(-3);
989 if (base1->rec_type > base2->rec_type)
990 return(3);
991 if (limit == 3)
992 return(0); /* ignore below */
994 if (base1->key < base2->key)
995 return(-2);
996 if (base1->key > base2->key)
997 return(2);
998 if (limit == 4)
999 return(0); /* ignore below */
1001 if (base1->create_tid == 0) {
1002 if (base2->create_tid == 0)
1003 return(0);
1004 return(1);
1006 if (base2->create_tid == 0)
1007 return(-1);
1008 if (base1->create_tid < base2->create_tid)
1009 return(-1);
1010 if (base1->create_tid > base2->create_tid)
1011 return(1);
1012 return(0);
1015 static __inline
1017 test_btree_match(hammer_btree_elm_t elm)
1019 if (test_btree_search(elm) == 0)
1020 return(1);
1021 return(0);
1024 static
1026 test_btree_out_of_range(hammer_btree_elm_t elm)
1028 if (test_btree_search(elm) > 0)
1029 return(1); /* conditions < this elm */
1031 if (opt.limit >= 5) {
1032 if (test_btree_search(elm + 1) <= 0)
1033 return(1); /* next elm <= conditions */
1034 } else {
1035 if (test_btree_search(elm + 1) < 0)
1036 return(1); /* next elm < conditions */
1038 return(0);
1042 * Dump the UNDO FIFO
1044 void
1045 hammer_cmd_show_undo(void)
1047 volume_info_t volume;
1048 hammer_blockmap_t rootmap;
1049 hammer_off_t scan_offset;
1050 hammer_fifo_any_t head;
1051 hammer_fifo_head_t hdr;
1052 buffer_info_t data_buffer = NULL;
1053 zone_stat_t stats = NULL;
1055 volume = get_root_volume();
1056 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
1058 print_blockmap(volume);
1060 if (VerboseOpt)
1061 stats = hammer_init_zone_stat_bits();
1063 scan_offset = HAMMER_ENCODE_UNDO(0);
1064 while (scan_offset < rootmap->alloc_offset) {
1065 head = get_buffer_data(scan_offset, &data_buffer, 0);
1066 hdr = &head->head;
1067 printf("%016jx ", scan_offset);
1069 switch(hdr->hdr_type) {
1070 case HAMMER_HEAD_TYPE_PAD:
1071 printf("PAD(%d)", hdr->hdr_size);
1072 break;
1073 case HAMMER_HEAD_TYPE_DUMMY:
1074 printf("DUMMY(%d)\tseq=%08x",
1075 hdr->hdr_size, hdr->hdr_seq);
1076 break;
1077 case HAMMER_HEAD_TYPE_UNDO:
1078 printf("UNDO(%d)\tseq=%08x offset=%016jx bytes=%d",
1079 hdr->hdr_size, hdr->hdr_seq,
1080 (intmax_t)head->undo.undo_offset,
1081 head->undo.undo_data_bytes);
1082 break;
1083 case HAMMER_HEAD_TYPE_REDO:
1084 printf("REDO(%d)\tseq=%08x offset=%016jx bytes=%d "
1085 "objid=%016jx flags=%08x lo=%08x",
1086 hdr->hdr_size, hdr->hdr_seq,
1087 (intmax_t)head->redo.redo_offset,
1088 head->redo.redo_data_bytes,
1089 (intmax_t)head->redo.redo_objid,
1090 head->redo.redo_flags,
1091 head->redo.redo_localization);
1092 break;
1093 default:
1094 printf("%04x(%d)\tseq=%08x",
1095 hdr->hdr_type, hdr->hdr_size, hdr->hdr_seq);
1096 break;
1099 if (scan_offset == rootmap->first_offset)
1100 printf(" >");
1101 if (scan_offset == rootmap->next_offset)
1102 printf(" <");
1103 printf("\n");
1105 if (stats)
1106 hammer_add_zone_stat(stats, scan_offset, hdr->hdr_size);
1108 if ((hdr->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1109 hdr->hdr_size == 0 ||
1110 hdr->hdr_size > HAMMER_UNDO_ALIGN -
1111 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1112 printf("Illegal size field, skipping to "
1113 "next boundary\n");
1114 scan_offset = HAMMER_UNDO_DOALIGN(scan_offset);
1115 } else {
1116 scan_offset += hdr->hdr_size;
1119 rel_buffer(data_buffer);
1121 if (stats) {
1122 hammer_print_zone_stat(stats);
1123 hammer_cleanup_zone_stat(stats);