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