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