sbin/hammer: Don't modify buffer_info for config data for no reason
[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         if (cfgtxt != NULL) {
638                 while((token = strsep(&cfgtxt, "\r\n")) != NULL)
639                         printf("%17s  %s\n", "", token);
640         }
641         printf("%17s\"", "");
642 }
643
644 static
645 void
646 print_record(hammer_btree_elm_t elm)
647 {
648         struct buffer_info *data_buffer;
649         hammer_off_t data_offset;
650         int32_t data_len;
651         hammer_data_ondisk_t data;
652         uint32_t status;
653         char *str1 = NULL;
654         char *str2 = NULL;
655
656         data_offset = elm->leaf.data_offset;
657         data_len = elm->leaf.data_len;
658         assert(data_offset != 0);
659         assert(data_len != 0);
660
661         data_buffer = NULL;
662         data = get_buffer_data(data_offset, &data_buffer, 0);
663         assert(data != NULL);
664
665         switch(elm->leaf.base.rec_type) {
666         case HAMMER_RECTYPE_UNKNOWN:
667                 printf("\n%17s", "");
668                 printf("unknown");
669                 break;
670         case HAMMER_RECTYPE_INODE:
671                 printf("\n%17s", "");
672                 printf("inode size=%jd nlinks=%jd",
673                        (intmax_t)data->inode.size,
674                        (intmax_t)data->inode.nlinks);
675                 if (QuietOpt < 1) {
676                         printf(" mode=%05o uflags=%08x caps=%02x",
677                                 data->inode.mode,
678                                 data->inode.uflags,
679                                 data->inode.cap_flags);
680                         printf(" pobjid=%016jx ot=%02x\n",
681                                 (uintmax_t)data->inode.parent_obj_id,
682                                 data->inode.obj_type);
683                         printf("%17s", "");
684                         printf("      ctime=%016jx mtime=%016jx atime=%016jx",
685                                 (uintmax_t)data->inode.ctime,
686                                 (uintmax_t)data->inode.mtime,
687                                 (uintmax_t)data->inode.atime);
688                         if (data->inode.ext.symlink[0])
689                                 printf(" symlink=\"%s\"",
690                                         data->inode.ext.symlink);
691                 }
692                 break;
693         case HAMMER_RECTYPE_DIRENTRY:
694                 data_len -= HAMMER_ENTRY_NAME_OFF;
695                 printf("\n%17s", "");
696                 printf("dir-entry ino=%016jx lo=%08x",
697                        (uintmax_t)data->entry.obj_id,
698                        data->entry.localization);
699                 if (!do_obfuscate)
700                         printf(" name=\"%*.*s\"",
701                                data_len, data_len, data->entry.name);
702                 break;
703         case HAMMER_RECTYPE_FIX:
704                 switch(elm->leaf.base.key) {
705                 case HAMMER_FIXKEY_SYMLINK:
706                         data_len -= HAMMER_SYMLINK_NAME_OFF;
707                         printf("\n%17s", "");
708                         printf("fix-symlink name=\"%*.*s\"",
709                                 data_len, data_len, data->symlink.name);
710                         break;
711                 }
712                 break;
713         case HAMMER_RECTYPE_PFS:
714                 printf("\n%17s", "");
715                 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
716                         (intmax_t)data->pfsd.sync_beg_tid,
717                         (intmax_t)data->pfsd.sync_end_tid);
718                 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
719                 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
720                 printf("%17s", "");
721                 printf("    shared_uuid=%s\n", str1);
722                 printf("%17s", "");
723                 printf("    unique_uuid=%s\n", str2);
724                 printf("%17s", "");
725                 printf("    mirror_flags=%08x label=\"%s\"",
726                         data->pfsd.mirror_flags, data->pfsd.label);
727                 if (data->pfsd.snapshots[0])
728                         printf(" snapshots=\"%s\"", data->pfsd.snapshots);
729                 free(str1);
730                 free(str2);
731                 break;
732         case HAMMER_RECTYPE_SNAPSHOT:
733                 printf("\n%17s", "");
734                 printf("snapshot tid=%016jx label=\"%s\"",
735                         (intmax_t)data->snap.tid, data->snap.label);
736                 break;
737         case HAMMER_RECTYPE_CONFIG:
738                 if (VerboseOpt > 2) {
739                         char *p = strdup(data->config.text);
740                         print_config(p);
741                         free(p);
742                 }
743                 break;
744         case HAMMER_RECTYPE_DATA:
745                 if (VerboseOpt > 3) {
746                         printf("\n");
747                         hexdump(data, data_len, "\t\t  ", 0);
748                 }
749                 break;
750         case HAMMER_RECTYPE_EXT:
751         case HAMMER_RECTYPE_DB:
752                 if (VerboseOpt > 2) {
753                         printf("\n");
754                         hexdump(data, data_len, "\t\t  ", 0);
755                 }
756                 break;
757         default:
758                 assert(0);
759                 break;
760         }
761         rel_buffer(data_buffer);
762 }
763
764 static __inline
765 unsigned long
766 _strtoul(const char *p, int base)
767 {
768         unsigned long retval;
769
770         errno = 0;  /* clear */
771         retval = strtoul(p, NULL, base);
772         if (errno == ERANGE && retval == ULONG_MAX)
773                 err(1, "strtoul");
774         return retval;
775 }
776
777 static __inline
778 unsigned long long
779 _strtoull(const char *p, int base)
780 {
781         unsigned long long retval;
782
783         errno = 0;  /* clear */
784         retval = strtoull(p, NULL, base);
785         if (errno == ERANGE && retval == ULLONG_MAX)
786                 err(1, "strtoull");
787         return retval;
788 }
789
790 static int
791 init_btree_search(const char *arg, int filter, btree_search_t search)
792 {
793         char *s, *p;
794         int i = 0;
795
796         bzero(&search->base, sizeof(search->base));
797         search->limit = 0;
798         search->filter = filter;
799
800         if (arg == NULL)
801                 return(-1);
802         if (strcmp(arg, "none") == 0)
803                 return(-1);
804
805         s = strdup(arg);
806         if (s == NULL)
807                 return(-1);
808
809         while ((p = s) != NULL) {
810                 if ((s = strchr(s, ':')) != NULL)
811                         *s++ = 0;
812                 if (++i == 1) {
813                         search->base.localization = _strtoul(p, 16);
814                 } else if (i == 2) {
815                         search->base.obj_id = _strtoull(p, 16);
816                 } else if (i == 3) {
817                         search->base.rec_type = _strtoul(p, 16);
818                 } else if (i == 4) {
819                         search->base.key = _strtoull(p, 16);
820                 } else if (i == 5) {
821                         search->base.create_tid = _strtoull(p, 16);
822                         break;
823                 }
824         }
825         search->limit = i;
826         free(s);
827
828         return(i);
829 }
830
831 static int
832 test_btree_search(hammer_btree_elm_t elm, btree_search_t search)
833 {
834         hammer_base_elm_t base1 = &elm->base;
835         hammer_base_elm_t base2 = &search->base;
836         assert(search);
837
838         if (base1->localization < base2->localization)
839                 return(-1);
840         if (base1->localization > base2->localization)
841                 return(1);
842         if (search->limit == 1)
843                 return(0);  /* ignore below */
844
845         if (base1->obj_id < base2->obj_id)
846                 return(-2);
847         if (base1->obj_id > base2->obj_id)
848                 return(2);
849         if (search->limit == 2)
850                 return(0);  /* ignore below */
851
852         if (base1->rec_type < base2->rec_type)
853                 return(-3);
854         if (base1->rec_type > base2->rec_type)
855                 return(3);
856         if (search->limit == 3)
857                 return(0);  /* ignore below */
858
859         if (base1->key < base2->key)
860                 return(-4);
861         if (base1->key > base2->key)
862                 return(4);
863         if (search->limit == 4)
864                 return(0);  /* ignore below */
865
866         if (base1->create_tid == 0) {
867                 if (base2->create_tid == 0)
868                         return(0);
869                 return(5);
870         }
871         if (base2->create_tid == 0)
872                 return(-5);
873         if (base1->create_tid < base2->create_tid)
874                 return(-5);
875         if (base1->create_tid > base2->create_tid)
876                 return(5);
877         return(0);
878 }
879
880 static __inline
881 int
882 test_btree_match(hammer_btree_elm_t elm, btree_search_t search)
883 {
884         if (test_btree_search(elm, search) == 0)
885                 return(1);
886         return(0);
887 }
888
889 static
890 int
891 test_btree_out_of_range(hammer_btree_elm_t elm, btree_search_t search)
892 {
893         if (test_btree_search(elm, search) > 0)
894                 return(1);  /* search < this elm */
895
896         if (search->limit >= 5) {
897                 if (test_btree_search(elm + 1, search) <= 0)
898                         return(1);  /* next elm <= search */
899         } else {
900                 if (test_btree_search(elm + 1, search) < 0)
901                         return(1);  /* next elm < search */
902         }
903         return(0);
904 }
905
906 /*
907  * Dump the UNDO FIFO
908  */
909 void
910 hammer_cmd_show_undo(void)
911 {
912         struct volume_info *volume;
913         hammer_blockmap_t rootmap;
914         hammer_off_t scan_offset;
915         hammer_fifo_any_t head;
916         struct buffer_info *data_buffer = NULL;
917         int64_t bytes;
918
919         volume = get_volume(RootVolNo);
920         rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
921         if (rootmap->first_offset <= rootmap->next_offset)
922                 bytes = rootmap->next_offset - rootmap->first_offset;
923         else
924                 bytes = rootmap->alloc_offset - rootmap->first_offset +
925                         (rootmap->next_offset & HAMMER_OFF_LONG_MASK);
926
927         printf("Volume header UNDO %016jx-%016jx/%016jx\n",
928                 (intmax_t)rootmap->first_offset,
929                 (intmax_t)rootmap->next_offset,
930                 (intmax_t)rootmap->alloc_offset);
931         printf("UNDO map is %jdMB\n",
932                 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
933                            (1024 * 1024)));
934         printf("UNDO being used is %jdB\n", (intmax_t)bytes);
935
936         scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
937         while (scan_offset < rootmap->alloc_offset) {
938                 head = get_buffer_data(scan_offset, &data_buffer, 0);
939                 printf("%016jx ", scan_offset);
940
941                 switch(head->head.hdr_type) {
942                 case HAMMER_HEAD_TYPE_PAD:
943                         printf("PAD(%04x)", head->head.hdr_size);
944                         break;
945                 case HAMMER_HEAD_TYPE_DUMMY:
946                         printf("DUMMY(%04x) seq=%08x",
947                                 head->head.hdr_size, head->head.hdr_seq);
948                         break;
949                 case HAMMER_HEAD_TYPE_UNDO:
950                         printf("UNDO(%04x) seq=%08x "
951                                "dataoff=%016jx bytes=%d",
952                                 head->head.hdr_size, head->head.hdr_seq,
953                                 (intmax_t)head->undo.undo_offset,
954                                 head->undo.undo_data_bytes);
955                         break;
956                 case HAMMER_HEAD_TYPE_REDO:
957                         printf("REDO(%04x) seq=%08x flags=%08x "
958                                "objid=%016jx logoff=%016jx bytes=%d",
959                                 head->head.hdr_size, head->head.hdr_seq,
960                                 head->redo.redo_flags,
961                                 (intmax_t)head->redo.redo_objid,
962                                 (intmax_t)head->redo.redo_offset,
963                                 head->redo.redo_data_bytes);
964                         break;
965                 default:
966                         printf("UNKNOWN(%04x,%04x) seq=%08x",
967                                 head->head.hdr_type,
968                                 head->head.hdr_size,
969                                 head->head.hdr_seq);
970                         break;
971                 }
972
973                 if (scan_offset == rootmap->first_offset)
974                         printf(" >");
975                 if (scan_offset == rootmap->next_offset)
976                         printf(" <");
977                 printf("\n");
978
979                 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
980                     head->head.hdr_size == 0 ||
981                     head->head.hdr_size > HAMMER_UNDO_ALIGN -
982                                     ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
983                         printf("Illegal size field, skipping to "
984                                "next boundary\n");
985                         scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
986                                         ~HAMMER_UNDO_MASK64;
987                 } else {
988                         scan_offset += head->head.hdr_size;
989                 }
990         }
991         rel_buffer(data_buffer);
992 }