1dfa549a897c8ed2746bf901c15925dc3dacc9c3
[dragonfly.git] / sbin / hammer / cmd_show.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  */
34
35 #include <libutil.h>
36
37 #include "hammer.h"
38
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
44
45 typedef struct btree_search {
46         struct hammer_base_elm base;
47         int             limit;   /* # of fields to test */
48         int             filter;  /* filter type (default -1) */
49 } *btree_search_t;
50
51 static void print_btree_node(hammer_off_t node_offset, btree_search_t search,
52                         int depth, hammer_tid_t mirror_tid,
53                         hammer_base_elm_t left_bound,
54                         hammer_base_elm_t right_bound,
55                         struct zone_stat *stats);
56 static const char *check_data_crc(hammer_btree_elm_t elm);
57 static void print_record(hammer_btree_elm_t elm);
58 static void print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
59                         hammer_btree_elm_t elm,
60                         hammer_base_elm_t left_bound,
61                         hammer_base_elm_t right_bound,
62                         const char *ext, struct zone_stat *stats);
63 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
64                         hammer_btree_elm_t elm,
65                         hammer_base_elm_t left_bound,
66                         hammer_base_elm_t right_bound);
67 static int test_lr(hammer_btree_elm_t elm,
68                         hammer_base_elm_t left_bound,
69                         hammer_base_elm_t right_bound);
70 static int test_rbn_lr(hammer_btree_elm_t elm,
71                         hammer_base_elm_t left_bound,
72                         hammer_base_elm_t right_bound);
73 static void print_bigblock_fill(hammer_off_t offset);
74 static int init_btree_search(const char *arg, int filter,
75                         btree_search_t search);
76 static int test_btree_search(hammer_btree_elm_t elm, btree_search_t search);
77 static int test_btree_match(hammer_btree_elm_t elm, btree_search_t search);
78 static int test_btree_out_of_range(hammer_btree_elm_t elm, btree_search_t search);
79
80 static int num_bad_node = 0;
81 static int num_bad_elm = 0;
82 static int do_obfuscate = 0;
83
84 void
85 hammer_cmd_show(hammer_off_t node_offset, const char *arg,
86                 int filter, int obfuscate, int depth,
87                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
88 {
89         struct volume_info *volume;
90         struct btree_search search;
91         struct zone_stat *stats = NULL;
92         int zone;
93
94         AssertOnFailure = (DebugOpt != 0);
95
96         if (VerboseOpt)
97                 stats = hammer_init_zone_stat_bits();
98
99         if (node_offset == HAMMER_OFF_BAD) {
100                 volume = get_volume(RootVolNo);
101                 node_offset = volume->ondisk->vol0_btree_root;
102                 if (QuietOpt < 3) {
103                         printf("Volume header\trecords=%jd next_tid=%016jx\n",
104                                (intmax_t)volume->ondisk->vol0_stat_records,
105                                (uintmax_t)volume->ondisk->vol0_next_tid);
106                         printf("\t\tbufoffset=%016jx\n",
107                                (uintmax_t)volume->ondisk->vol_buf_beg);
108                         for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
109                                 printf("\t\tzone %d\tnext_offset=%016jx\n",
110                                         zone,
111                                         (uintmax_t)volume->ondisk->vol0_blockmap[zone].next_offset
112                                 );
113                         }
114                 }
115                 rel_volume(volume);
116         }
117
118         printf("show %016jx", (uintmax_t)node_offset);
119
120         do_obfuscate = obfuscate;
121         init_btree_search(arg, filter, &search);
122         if (arg) {
123                 if (search.limit >= 1)
124                         printf(" lo %08x", search.base.localization);
125                 if (search.limit >= 2)
126                         printf(" obj_id %016jx", (uintmax_t)search.base.obj_id);
127                 if (search.limit >= 3)
128                         printf(" rec_type %02x", search.base.rec_type);
129                 if (search.limit >= 4)
130                         printf(" key %016jx", (uintmax_t)search.base.key);
131                 if (search.limit == 5)
132                         printf(" create_tid %016jx\n",
133                                 (uintmax_t)search.base.create_tid);
134         }
135         printf(" depth %d\n", depth);
136         print_btree_node(node_offset, &search, depth, HAMMER_MAX_TID,
137                          left_bound, right_bound, stats);
138
139         AssertOnFailure = 1;
140
141         if (VerboseOpt) {
142                 hammer_print_zone_stat(stats);
143                 hammer_cleanup_zone_stat(stats);
144         }
145
146         if (num_bad_node || VerboseOpt) {
147                 printf("%d bad nodes\n", num_bad_node);
148         }
149         if (num_bad_elm || VerboseOpt) {
150                 printf("%d bad elms\n", num_bad_elm);
151         }
152 }
153
154 static void
155 print_btree_node(hammer_off_t node_offset, btree_search_t search,
156                 int depth, hammer_tid_t mirror_tid,
157                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound,
158                 struct zone_stat *stats)
159 {
160         struct buffer_info *buffer = NULL;
161         hammer_node_ondisk_t node;
162         hammer_btree_elm_t elm;
163         int i;
164         int maxcount;
165         char badc = ' ';  /* good */
166         char badm = ' ';  /* good */
167         const char *ext;
168
169         node = get_node(node_offset, &buffer);
170
171         if (node == NULL) {
172                 badc = 'B';
173                 badm = 'I';
174         } else {
175                 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) != node->crc)
176                         badc = 'B';
177                 if (node->mirror_tid > mirror_tid) {
178                         badc = 'B';
179                         badm = 'M';
180                 }
181                 maxcount = hammer_node_max_elements(node->type);
182                 if (maxcount == -1) {
183                         badc = 'B';
184                         badm = 'U';
185                 } else if (node->count == 0 || node->count > maxcount) {
186                         badc = 'B';
187                         badm = 'C';
188                 }
189         }
190
191         if (badm != ' ' || badc != ' ')  /* not good */
192                 ++num_bad_node;
193
194         printf("%c%c   NODE %016jx ", badc, badm, (uintmax_t)node_offset);
195         if (node == NULL) {
196                 printf("(IO ERROR)\n");
197                 rel_buffer(buffer);
198                 return;
199         }
200
201         printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx",
202                node->count,
203                (uintmax_t)node->parent,
204                (node->type ? node->type : '?'),
205                depth,
206                (uintmax_t)node->mirror_tid);
207         if (QuietOpt < 3) {
208                 printf(" fill=");
209                 print_bigblock_fill(node_offset);
210         }
211         printf(" {\n");
212
213         if (VerboseOpt)
214                 hammer_add_zone_stat(stats, node_offset, sizeof(*node));
215
216         for (i = 0; i < node->count; ++i) {
217                 elm = &node->elms[i];
218                 ext = NULL;
219
220                 if (search->limit) {
221                         switch (node->type) {
222                         case HAMMER_BTREE_TYPE_INTERNAL:
223                                 if (!test_btree_out_of_range(elm, search))
224                                         ext = "*";
225                                 break;
226                         case HAMMER_BTREE_TYPE_LEAF:
227                                 if (test_btree_match(elm, search))
228                                         ext = "*";
229                                 break;
230                         }
231                 }
232                 print_btree_elm(node, node_offset,
233                                 elm, left_bound, right_bound,
234                                 ext, stats);
235         }
236         if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
237                 assert(i == node->count);  /* boundary */
238                 elm = &node->elms[i];
239                 print_btree_elm(node, node_offset,
240                                 elm, left_bound, right_bound,
241                                 NULL, stats);
242         }
243         printf("     }\n");
244
245         if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
246                 for (i = 0; i < node->count; ++i) {
247                         elm = &node->elms[i];
248                         if (search->limit && search->filter) {
249                                 if (test_btree_out_of_range(elm, search))
250                                         continue;
251                         }
252                         if (elm->internal.subtree_offset) {
253                                 print_btree_node(elm->internal.subtree_offset,
254                                                  search, depth + 1,
255                                                  elm->internal.mirror_tid,
256                                                  &elm[0].base, &elm[1].base,
257                                                  stats);
258                                 /*
259                                  * Cause show to do normal iteration after
260                                  * seeking to the lo:objid:rectype:key:tid
261                                  * by default
262                                  */
263                                 if (search->limit && search->filter == -1)  /* default */
264                                         search->filter = 0;
265                         }
266                 }
267         }
268         rel_buffer(buffer);
269 }
270
271 static __inline
272 int
273 is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm)
274 {
275         /*
276          * elm->base.btype depends on what the original node had
277          * so it could be anything but HAMMER_BTREE_TYPE_NONE.
278          */
279         return (type == HAMMER_BTREE_TYPE_INTERNAL &&
280                 i == 0 &&
281                 elm->base.localization == 0 &&
282                 elm->base.obj_id == (int64_t)-0x8000000000000000LL &&
283                 elm->base.key == (int64_t)-0x8000000000000000LL &&
284                 elm->base.create_tid == 1 &&
285                 elm->base.delete_tid == 1 &&
286                 elm->base.rec_type == 0 &&
287                 elm->base.obj_type == 0 &&
288                 elm->base.btype != HAMMER_BTREE_TYPE_NONE);
289 }
290
291 static __inline
292 int
293 is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm)
294 {
295         return (type == HAMMER_BTREE_TYPE_INTERNAL &&
296                 i != 0 &&
297                 elm->base.localization == 0xFFFFFFFFU &&
298                 elm->base.obj_id == 0x7FFFFFFFFFFFFFFFLL &&
299                 elm->base.key == 0x7FFFFFFFFFFFFFFFLL &&
300                 elm->base.create_tid == 0xFFFFFFFFFFFFFFFFULL &&
301                 elm->base.delete_tid == 0 &&
302                 elm->base.rec_type == 0xFFFFU &&
303                 elm->base.obj_type == 0 &&
304                 elm->base.btype == HAMMER_BTREE_TYPE_NONE);
305 }
306
307 static
308 void
309 print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
310                 hammer_btree_elm_t elm,
311                 hammer_base_elm_t left_bound,
312                 hammer_base_elm_t right_bound,
313                 const char *ext, struct zone_stat *stats)
314 {
315         char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
316         char deleted;
317         char rootelm;
318         const char *label;
319         int flags;
320         int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
321
322         flags = get_elm_flags(node, node_offset, elm, left_bound, right_bound);
323         flagstr[0] = flags ? 'B' : 'G';
324         if (flags & FLAG_TOOFARLEFT)
325                 flagstr[2] = 'L';
326         if (flags & FLAG_TOOFARRIGHT)
327                 flagstr[3] = 'R';
328         if (flags & FLAG_BADTYPE)
329                 flagstr[4] = 'T';
330         if (flags & FLAG_BADCHILDPARENT)
331                 flagstr[5] = 'C';
332         if (flags & FLAG_BADMIRRORTID)
333                 flagstr[6] = 'M';
334         if (flagstr[0] == 'B')
335                 ++num_bad_elm;
336
337         /*
338          * Check if elm is derived from root split
339          */
340         if (is_root_btree_beg(node->type, i, elm))
341                 rootelm = '>';
342         else if (is_root_btree_end(node->type, i, elm))
343                 rootelm = '<';
344         else
345                 rootelm = ' ';
346
347         if (elm->base.delete_tid)
348                 deleted = 'd';
349         else
350                 deleted = ' ';
351
352         if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
353                 label = "RBN";
354         else
355                 label = "ELM";
356
357         printf("%s\t%s %2d %c ", flagstr, label, i, hammer_elm_btype(elm));
358         printf("lo=%08x obj=%016jx rt=%02x key=%016jx tid=%016jx\n",
359                elm->base.localization,
360                (uintmax_t)elm->base.obj_id,
361                elm->base.rec_type,
362                (uintmax_t)elm->base.key,
363                (uintmax_t)elm->base.create_tid);
364         printf("\t       %c del=%016jx ot=%02x",
365                (rootelm == ' ' ? deleted : rootelm),
366                (uintmax_t)elm->base.delete_tid,
367                elm->base.obj_type);
368
369         switch(node->type) {
370         case HAMMER_BTREE_TYPE_INTERNAL:
371                 printf(" suboff=%016jx",
372                        (uintmax_t)elm->internal.subtree_offset);
373                 if (QuietOpt < 3) {
374                         printf(" mirror=%016jx",
375                                (uintmax_t)elm->internal.mirror_tid);
376                 }
377                 if (ext)
378                         printf(" %s", ext);
379                 break;
380         case HAMMER_BTREE_TYPE_LEAF:
381                 switch(elm->base.btype) {
382                 case HAMMER_BTREE_TYPE_RECORD:
383                         printf(" dataoff=%016jx/%d",
384                                (uintmax_t)elm->leaf.data_offset,
385                                elm->leaf.data_len);
386                         if (QuietOpt < 3) {
387                                 const char *p = check_data_crc(elm);
388                                 printf(" crc=%08x", elm->leaf.data_crc);
389                                 if (p)
390                                         printf(" %s", p);
391                                 printf(" fill=");
392                                 print_bigblock_fill(elm->leaf.data_offset);
393                         }
394                         if (QuietOpt < 2)
395                                 print_record(elm);
396                         if (VerboseOpt)
397                                 hammer_add_zone_stat(stats,
398                                         elm->leaf.data_offset,
399                                         elm->leaf.data_len);
400                         break;
401                 default:
402                         printf("\n\t         ");
403                         printf("badtype=%d", elm->base.btype);
404                         break;
405                 }
406                 if (ext)
407                         printf(" %s", ext);
408                 break;
409         }
410         printf("\n");
411 }
412
413 static
414 int
415 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
416                 hammer_btree_elm_t elm,
417                 hammer_base_elm_t left_bound,
418                 hammer_base_elm_t right_bound)
419 {
420         hammer_off_t child_offset;
421         int flags = 0;
422         int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
423
424         switch(node->type) {
425         case HAMMER_BTREE_TYPE_INTERNAL:
426                 child_offset = elm->internal.subtree_offset;
427                 if (elm->internal.mirror_tid > node->mirror_tid)
428                         flags |= FLAG_BADMIRRORTID;
429
430                 if (i == node->count) {
431                         if (child_offset != 0)
432                                 flags |= FLAG_BADCHILDPARENT;
433                         switch(elm->base.btype) {
434                         case HAMMER_BTREE_TYPE_NONE:
435                                 flags |= test_rbn_lr(elm, left_bound, right_bound);
436                                 break;
437                         default:
438                                 flags |= FLAG_BADTYPE;
439                                 break;
440                         }
441                 } else {
442                         if (child_offset == 0) {
443                                 flags |= FLAG_BADCHILDPARENT;
444                         } else {
445                                 struct buffer_info *buffer = NULL;
446                                 hammer_node_ondisk_t subnode;
447                                 subnode = get_node(child_offset, &buffer);
448                                 if (subnode == NULL)
449                                         flags |= FLAG_BADCHILDPARENT;
450                                 else if (subnode->parent != node_offset)
451                                         flags |= FLAG_BADCHILDPARENT;
452                                 rel_buffer(buffer);
453                         }
454                         switch(elm->base.btype) {
455                         case HAMMER_BTREE_TYPE_INTERNAL:
456                         case HAMMER_BTREE_TYPE_LEAF:
457                                 flags |= test_lr(elm, left_bound, right_bound);
458                                 break;
459                         default:
460                                 flags |= FLAG_BADTYPE;
461                                 break;
462                         }
463                 }
464                 break;
465         case HAMMER_BTREE_TYPE_LEAF:
466                 if (elm->leaf.data_offset == 0) {
467                         flags |= FLAG_BADCHILDPARENT;
468                 }
469                 if (elm->leaf.data_len == 0) {
470                         flags |= FLAG_BADCHILDPARENT;
471                 }
472
473                 if (node->mirror_tid == 0 &&
474                     !(node->parent == 0 && node->count == 2)) {
475                         flags |= FLAG_BADMIRRORTID;
476                 }
477                 if (elm->base.create_tid && node->mirror_tid &&
478                     elm->base.create_tid > node->mirror_tid) {
479                         flags |= FLAG_BADMIRRORTID;
480                 }
481                 if (elm->base.delete_tid && node->mirror_tid &&
482                     elm->base.delete_tid > node->mirror_tid) {
483                         flags |= FLAG_BADMIRRORTID;
484                 }
485                 switch(elm->base.btype) {
486                 case HAMMER_BTREE_TYPE_RECORD:
487                         flags |= test_lr(elm, left_bound, right_bound);
488                         break;
489                 default:
490                         flags |= FLAG_BADTYPE;
491                         break;
492                 }
493                 break;
494         default:
495                 flags |= FLAG_BADTYPE;
496                 break;
497         }
498         return(flags);
499 }
500
501 static
502 int
503 test_lr(hammer_btree_elm_t elm,
504         hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
505 {
506         if (left_bound == NULL || right_bound == NULL)
507                 return(0);
508         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
509                 return(FLAG_TOOFARLEFT);
510         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
511                 return(FLAG_TOOFARRIGHT);
512         return(0);
513 }
514
515 static
516 int
517 test_rbn_lr(hammer_btree_elm_t rbn,
518         hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
519 {
520         if (left_bound == NULL || right_bound == NULL)
521                 return(0);
522         if (hammer_btree_cmp(&rbn->base, left_bound) < 0)
523                 return(FLAG_TOOFARLEFT);
524         if (hammer_btree_cmp(&rbn->base, right_bound) > 0)
525                 return(FLAG_TOOFARRIGHT);
526         return(0);
527 }
528
529 static
530 void
531 print_bigblock_fill(hammer_off_t offset)
532 {
533         struct hammer_blockmap_layer1 layer1;
534         struct hammer_blockmap_layer2 layer2;
535         int fill;
536         int error;
537
538         blockmap_lookup(offset, &layer1, &layer2, &error);
539         printf("z%d:v%d:%d:%d:%lu=",
540                 HAMMER_ZONE_DECODE(offset),
541                 HAMMER_VOL_DECODE(offset),
542                 HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
543                 HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
544                 offset & HAMMER_BIGBLOCK_MASK64);
545
546         if (error) {
547                 printf("B%d", error);
548         } else {
549                 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
550                 fill = 100 - fill;
551                 printf("%d%%", fill);
552         }
553 }
554
555 /*
556  * Check the generic crc on a data element.  Inodes record types are
557  * special in that some of their fields are not CRCed.
558  *
559  * Also check that the zone is valid.
560  */
561 static
562 const char *
563 check_data_crc(hammer_btree_elm_t elm)
564 {
565         struct buffer_info *data_buffer;
566         hammer_off_t data_offset;
567         int32_t data_len;
568         int32_t len;
569         uint32_t crc;
570         int error;
571         char *ptr;
572
573         data_offset = elm->leaf.data_offset;
574         data_len = elm->leaf.data_len;
575         data_buffer = NULL;
576         if (data_offset == 0 || data_len == 0)
577                 return("Z");
578
579         crc = 0;
580         error = 0;
581         while (data_len) {
582                 blockmap_lookup(data_offset, NULL, NULL, &error);
583                 if (error)
584                         break;
585
586                 ptr = get_buffer_data(data_offset, &data_buffer, 0);
587                 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
588                 if (len > data_len)
589                         len = (int)data_len;
590                 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
591                     data_len == sizeof(struct hammer_inode_data)) {
592                         crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
593                 } else {
594                         crc = crc32_ext(ptr, len, crc);
595                 }
596                 data_len -= len;
597                 data_offset += len;
598         }
599         rel_buffer(data_buffer);
600         if (error) {
601                 switch (error) {        /* bad offset */
602                 case -1:
603                         return("BO-ZL");
604                 case -2:
605                         return("BO-ZG");
606                 case -3:
607                         return("BO-RV");
608                 case -4:
609                         return("BO-AO");
610                 case -5:
611                         return("BO-DE");
612                 case -6:
613                         return("BO-L1");
614                 case -7:
615                         return("BO-LU");
616                 case -8:
617                         return("BO-L2");
618                 case -9:
619                         return("BO-LZ");
620                 default:
621                         return("BO-??");
622                 }
623         }
624         if (crc == elm->leaf.data_crc)
625                 return(NULL);
626         return("BX");                   /* bad crc */
627 }
628
629 static
630 void
631 print_config(char *cfgtxt)
632 {
633         char *token;
634
635         printf("\n%17s", "");
636         printf("config stext=\"\n");
637         while((token = strsep(&cfgtxt, "\r\n")) != NULL) {
638                 printf("%17s  %s\n", "", token);
639         }
640         printf("%17s\"", "");
641 }
642
643 static
644 void
645 print_record(hammer_btree_elm_t elm)
646 {
647         struct buffer_info *data_buffer;
648         hammer_off_t data_offset;
649         int32_t data_len;
650         hammer_data_ondisk_t data;
651         uint32_t status;
652         char *str1 = NULL;
653         char *str2 = NULL;
654
655         data_offset = elm->leaf.data_offset;
656         data_len = elm->leaf.data_len;
657         assert(data_offset != 0);
658         assert(data_len != 0);
659
660         data_buffer = NULL;
661         data = get_buffer_data(data_offset, &data_buffer, 0);
662         assert(data != NULL);
663
664         switch(elm->leaf.base.rec_type) {
665         case HAMMER_RECTYPE_UNKNOWN:
666                 printf("\n%17s", "");
667                 printf("unknown");
668                 break;
669         case HAMMER_RECTYPE_INODE:
670                 printf("\n%17s", "");
671                 printf("inode size=%jd nlinks=%jd",
672                        (intmax_t)data->inode.size,
673                        (intmax_t)data->inode.nlinks);
674                 if (QuietOpt < 1) {
675                         printf(" mode=%05o uflags=%08x caps=%02x",
676                                 data->inode.mode,
677                                 data->inode.uflags,
678                                 data->inode.cap_flags);
679                         printf(" pobjid=%016jx ot=%02x\n",
680                                 (uintmax_t)data->inode.parent_obj_id,
681                                 data->inode.obj_type);
682                         printf("%17s", "");
683                         printf("      ctime=%016jx mtime=%016jx atime=%016jx",
684                                 (uintmax_t)data->inode.ctime,
685                                 (uintmax_t)data->inode.mtime,
686                                 (uintmax_t)data->inode.atime);
687                         if (data->inode.ext.symlink[0])
688                                 printf(" symlink=\"%s\"",
689                                         data->inode.ext.symlink);
690                 }
691                 break;
692         case HAMMER_RECTYPE_DIRENTRY:
693                 data_len -= HAMMER_ENTRY_NAME_OFF;
694                 printf("\n%17s", "");
695                 printf("dir-entry ino=%016jx lo=%08x",
696                        (uintmax_t)data->entry.obj_id,
697                        data->entry.localization);
698                 if (!do_obfuscate)
699                         printf(" name=\"%*.*s\"",
700                                data_len, data_len, data->entry.name);
701                 break;
702         case HAMMER_RECTYPE_FIX:
703                 switch(elm->leaf.base.key) {
704                 case HAMMER_FIXKEY_SYMLINK:
705                         data_len -= HAMMER_SYMLINK_NAME_OFF;
706                         printf("\n%17s", "");
707                         printf("fix-symlink name=\"%*.*s\"",
708                                 data_len, data_len, data->symlink.name);
709                         break;
710                 }
711                 break;
712         case HAMMER_RECTYPE_PFS:
713                 printf("\n%17s", "");
714                 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
715                         (intmax_t)data->pfsd.sync_beg_tid,
716                         (intmax_t)data->pfsd.sync_end_tid);
717                 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
718                 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
719                 printf("%17s", "");
720                 printf("    shared_uuid=%s\n", str1);
721                 printf("%17s", "");
722                 printf("    unique_uuid=%s\n", str2);
723                 printf("%17s", "");
724                 printf("    mirror_flags=%08x label=\"%s\"",
725                         data->pfsd.mirror_flags, data->pfsd.label);
726                 if (data->pfsd.snapshots[0])
727                         printf(" snapshots=\"%s\"", data->pfsd.snapshots);
728                 free(str1);
729                 free(str2);
730                 break;
731         case HAMMER_RECTYPE_SNAPSHOT:
732                 printf("\n%17s", "");
733                 printf("snapshot tid=%016jx label=\"%s\"",
734                         (intmax_t)data->snap.tid, data->snap.label);
735                 break;
736         case HAMMER_RECTYPE_CONFIG:
737                 if (VerboseOpt > 2) {
738                         print_config(data->config.text);
739                 }
740                 break;
741         case HAMMER_RECTYPE_DATA:
742                 if (VerboseOpt > 3) {
743                         printf("\n");
744                         hexdump(data, data_len, "\t\t  ", 0);
745                 }
746                 break;
747         case HAMMER_RECTYPE_EXT:
748         case HAMMER_RECTYPE_DB:
749                 if (VerboseOpt > 2) {
750                         printf("\n");
751                         hexdump(data, data_len, "\t\t  ", 0);
752                 }
753                 break;
754         default:
755                 assert(0);
756                 break;
757         }
758         rel_buffer(data_buffer);
759 }
760
761 static __inline
762 unsigned long
763 _strtoul(const char *p, int base)
764 {
765         unsigned long retval;
766
767         errno = 0;  /* clear */
768         retval = strtoul(p, NULL, base);
769         if (errno == ERANGE && retval == ULONG_MAX)
770                 err(1, "strtoul");
771         return retval;
772 }
773
774 static __inline
775 unsigned long long
776 _strtoull(const char *p, int base)
777 {
778         unsigned long long retval;
779
780         errno = 0;  /* clear */
781         retval = strtoull(p, NULL, base);
782         if (errno == ERANGE && retval == ULLONG_MAX)
783                 err(1, "strtoull");
784         return retval;
785 }
786
787 static int
788 init_btree_search(const char *arg, int filter, btree_search_t search)
789 {
790         char *s, *p;
791         int i = 0;
792
793         bzero(&search->base, sizeof(search->base));
794         search->limit = 0;
795         search->filter = filter;
796
797         if (arg == NULL)
798                 return(-1);
799         if (strcmp(arg, "none") == 0)
800                 return(-1);
801
802         s = strdup(arg);
803         if (s == NULL)
804                 return(-1);
805
806         while ((p = s) != NULL) {
807                 if ((s = strchr(s, ':')) != NULL)
808                         *s++ = 0;
809                 if (++i == 1) {
810                         search->base.localization = _strtoul(p, 16);
811                 } else if (i == 2) {
812                         search->base.obj_id = _strtoull(p, 16);
813                 } else if (i == 3) {
814                         search->base.rec_type = _strtoul(p, 16);
815                 } else if (i == 4) {
816                         search->base.key = _strtoull(p, 16);
817                 } else if (i == 5) {
818                         search->base.create_tid = _strtoull(p, 16);
819                         break;
820                 }
821         }
822         search->limit = i;
823         free(s);
824
825         return(i);
826 }
827
828 static int
829 test_btree_search(hammer_btree_elm_t elm, btree_search_t search)
830 {
831         hammer_base_elm_t base1 = &elm->base;
832         hammer_base_elm_t base2 = &search->base;
833         assert(search);
834
835         if (base1->localization < base2->localization)
836                 return(-1);
837         if (base1->localization > base2->localization)
838                 return(1);
839         if (search->limit == 1)
840                 return(0);  /* ignore below */
841
842         if (base1->obj_id < base2->obj_id)
843                 return(-2);
844         if (base1->obj_id > base2->obj_id)
845                 return(2);
846         if (search->limit == 2)
847                 return(0);  /* ignore below */
848
849         if (base1->rec_type < base2->rec_type)
850                 return(-3);
851         if (base1->rec_type > base2->rec_type)
852                 return(3);
853         if (search->limit == 3)
854                 return(0);  /* ignore below */
855
856         if (base1->key < base2->key)
857                 return(-4);
858         if (base1->key > base2->key)
859                 return(4);
860         if (search->limit == 4)
861                 return(0);  /* ignore below */
862
863         if (base1->create_tid == 0) {
864                 if (base2->create_tid == 0)
865                         return(0);
866                 return(5);
867         }
868         if (base2->create_tid == 0)
869                 return(-5);
870         if (base1->create_tid < base2->create_tid)
871                 return(-5);
872         if (base1->create_tid > base2->create_tid)
873                 return(5);
874         return(0);
875 }
876
877 static __inline
878 int
879 test_btree_match(hammer_btree_elm_t elm, btree_search_t search)
880 {
881         if (test_btree_search(elm, search) == 0)
882                 return(1);
883         return(0);
884 }
885
886 static
887 int
888 test_btree_out_of_range(hammer_btree_elm_t elm, btree_search_t search)
889 {
890         if (test_btree_search(elm, search) > 0)
891                 return(1);  /* search < this elm */
892
893         if (search->limit >= 5) {
894                 if (test_btree_search(elm + 1, search) <= 0)
895                         return(1);  /* next elm <= search */
896         } else {
897                 if (test_btree_search(elm + 1, search) < 0)
898                         return(1);  /* next elm < search */
899         }
900         return(0);
901 }
902
903 /*
904  * Dump the UNDO FIFO
905  */
906 void
907 hammer_cmd_show_undo(void)
908 {
909         struct volume_info *volume;
910         hammer_blockmap_t rootmap;
911         hammer_off_t scan_offset;
912         hammer_fifo_any_t head;
913         struct buffer_info *data_buffer = NULL;
914         int64_t bytes;
915
916         volume = get_volume(RootVolNo);
917         rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
918         if (rootmap->first_offset <= rootmap->next_offset)
919                 bytes = rootmap->next_offset - rootmap->first_offset;
920         else
921                 bytes = rootmap->alloc_offset - rootmap->first_offset +
922                         (rootmap->next_offset & HAMMER_OFF_LONG_MASK);
923
924         printf("Volume header UNDO %016jx-%016jx/%016jx\n",
925                 (intmax_t)rootmap->first_offset,
926                 (intmax_t)rootmap->next_offset,
927                 (intmax_t)rootmap->alloc_offset);
928         printf("UNDO map is %jdMB\n",
929                 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
930                            (1024 * 1024)));
931         printf("UNDO being used is %jdB\n", (intmax_t)bytes);
932
933         scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
934         while (scan_offset < rootmap->alloc_offset) {
935                 head = get_buffer_data(scan_offset, &data_buffer, 0);
936                 printf("%016jx ", scan_offset);
937
938                 switch(head->head.hdr_type) {
939                 case HAMMER_HEAD_TYPE_PAD:
940                         printf("PAD(%04x)", head->head.hdr_size);
941                         break;
942                 case HAMMER_HEAD_TYPE_DUMMY:
943                         printf("DUMMY(%04x) seq=%08x",
944                                 head->head.hdr_size, head->head.hdr_seq);
945                         break;
946                 case HAMMER_HEAD_TYPE_UNDO:
947                         printf("UNDO(%04x) seq=%08x "
948                                "dataoff=%016jx bytes=%d",
949                                 head->head.hdr_size, head->head.hdr_seq,
950                                 (intmax_t)head->undo.undo_offset,
951                                 head->undo.undo_data_bytes);
952                         break;
953                 case HAMMER_HEAD_TYPE_REDO:
954                         printf("REDO(%04x) seq=%08x flags=%08x "
955                                "objid=%016jx logoff=%016jx bytes=%d",
956                                 head->head.hdr_size, head->head.hdr_seq,
957                                 head->redo.redo_flags,
958                                 (intmax_t)head->redo.redo_objid,
959                                 (intmax_t)head->redo.redo_offset,
960                                 head->redo.redo_data_bytes);
961                         break;
962                 default:
963                         printf("UNKNOWN(%04x,%04x) seq=%08x",
964                                 head->head.hdr_type,
965                                 head->head.hdr_size,
966                                 head->head.hdr_seq);
967                         break;
968                 }
969
970                 if (scan_offset == rootmap->first_offset)
971                         printf(" >");
972                 if (scan_offset == rootmap->next_offset)
973                         printf(" <");
974                 printf("\n");
975
976                 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
977                     head->head.hdr_size == 0 ||
978                     head->head.hdr_size > HAMMER_UNDO_ALIGN -
979                                     ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
980                         printf("Illegal size field, skipping to "
981                                "next boundary\n");
982                         scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
983                                         ~HAMMER_UNDO_MASK64;
984                 } else {
985                         scan_offset += head->head.hdr_size;
986                 }
987         }
988         rel_buffer(data_buffer);
989 }