sbin/hammer: remove obsolete "spike" code from hammer show command
[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
37 #define FLAG_TOOFARLEFT         0x0001
38 #define FLAG_TOOFARRIGHT        0x0002
39 #define FLAG_BADTYPE            0x0004
40 #define FLAG_BADCHILDPARENT     0x0008
41 #define FLAG_BADMIRRORTID       0x0010
42
43 typedef struct btree_search {
44         u_int32_t       lo;
45         int64_t         obj_id;
46 } *btree_search_t;
47
48 static void print_btree_node(hammer_off_t node_offset, btree_search_t search,
49                         int depth, hammer_tid_t mirror_tid,
50                         hammer_base_elm_t left_bound,
51                         hammer_base_elm_t right_bound);
52 static const char *check_data_crc(hammer_btree_elm_t elm);
53 static void print_record(hammer_btree_elm_t elm);
54 static void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
55                         int flags, const char *label, const char *ext);
56 static int print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
57                         hammer_btree_elm_t elm, u_int8_t btype,
58                         hammer_base_elm_t left_bound,
59                         hammer_base_elm_t right_bound);
60 static void print_bigblock_fill(hammer_off_t offset);
61
62 void
63 hammer_cmd_show(hammer_off_t node_offset, u_int32_t lo, int64_t obj_id,
64                 int depth,
65                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
66 {
67         struct volume_info *volume;
68         struct btree_search search;
69         btree_search_t searchp;
70         int zone;
71
72         AssertOnFailure = 0;
73
74         if (node_offset == (hammer_off_t)-1) {
75                 volume = get_volume(RootVolNo);
76                 node_offset = volume->ondisk->vol0_btree_root;
77                 if (QuietOpt < 3) {
78                         printf("Volume header\trecords=%jd next_tid=%016jx\n",
79                                (intmax_t)volume->ondisk->vol0_stat_records,
80                                (uintmax_t)volume->ondisk->vol0_next_tid);
81                         printf("\t\tbufoffset=%016jx\n",
82                                (uintmax_t)volume->ondisk->vol_buf_beg);
83                         for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
84                                 printf("\t\tzone %d\tnext_offset=%016jx\n",
85                                         zone,
86                                         (uintmax_t)volume->ondisk->vol0_blockmap[zone].next_offset
87                                 );
88                         }
89                 }
90                 rel_volume(volume);
91         }
92
93         if (lo == 0 && obj_id == (int64_t)HAMMER_MIN_OBJID) {
94                 searchp = NULL;
95                 printf("show %016jx depth %d\n", (uintmax_t)node_offset, depth);
96         } else {
97                 search.lo = lo;
98                 search.obj_id = obj_id;
99                 searchp = &search;
100                 printf("show %016jx lo %08x obj_id %016jx depth %d\n",
101                         (uintmax_t)node_offset, lo, (uintmax_t)obj_id, depth);
102         }
103         print_btree_node(node_offset, searchp, depth, HAMMER_MAX_TID,
104                          left_bound, right_bound);
105
106         AssertOnFailure = 1;
107 }
108
109 static void
110 print_btree_node(hammer_off_t node_offset, btree_search_t search,
111                 int depth, hammer_tid_t mirror_tid,
112                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
113 {
114         struct buffer_info *buffer = NULL;
115         hammer_node_ondisk_t node;
116         hammer_btree_elm_t elm;
117         int i;
118         int flags;
119         int maxcount;
120         char badc;
121         char badm;
122         const char *ext;
123
124         node = get_node(node_offset, &buffer);
125
126         if (node == NULL) {
127                 printf("BI   NODE %016jx (IO ERROR)\n",
128                        (uintmax_t)node_offset);
129                 return;
130         }
131
132         if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
133                 badc = ' ';
134         else
135                 badc = 'B';
136
137         if (node->mirror_tid <= mirror_tid) {
138                 badm = ' ';
139         } else {
140                 badm = 'M';
141                 badc = 'B';
142         }
143
144         printf("%c%c   NODE %016jx cnt=%02d p=%016jx "
145                "type=%c depth=%d",
146                badc,
147                badm,
148                (uintmax_t)node_offset, node->count,
149                (uintmax_t)node->parent,
150                (node->type ? node->type : '?'), depth);
151         printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
152         if (QuietOpt < 3) {
153                 printf(" fill=");
154                 print_bigblock_fill(node_offset);
155         }
156         printf(" {\n");
157
158         maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
159                    HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
160
161         for (i = 0; i < node->count && i < maxcount; ++i) {
162                 elm = &node->elms[i];
163
164                 if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
165                         ext = NULL;
166                         if (search &&
167                             elm->base.localization == search->lo &&
168                              elm->base.obj_id == search->obj_id) {
169                                 ext = " *";
170                         }
171                 } else if (search) {
172                         ext = " *";
173                         if (elm->base.localization > search->lo ||
174                             (elm->base.localization == search->lo &&
175                              elm->base.obj_id > search->obj_id)) {
176                                 ext = NULL;
177                         }
178                         if (elm[1].base.localization < search->lo ||
179                             (elm[1].base.localization == search->lo &&
180                              elm[1].base.obj_id < search->obj_id)) {
181                                 ext = NULL;
182                         }
183                 } else {
184                         ext = NULL;
185                 }
186
187                 flags = print_elm_flags(node, node_offset,
188                                         elm, elm->base.btype,
189                                         left_bound, right_bound);
190                 print_btree_elm(elm, i, node->type, flags, "ELM", ext);
191         }
192         if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
193                 elm = &node->elms[i];
194
195                 flags = print_elm_flags(node, node_offset,
196                                         elm, 'I',
197                                         left_bound, right_bound);
198                 print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
199         }
200         printf("     }\n");
201
202         for (i = 0; i < node->count; ++i) {
203                 elm = &node->elms[i];
204
205                 switch(node->type) {
206                 case HAMMER_BTREE_TYPE_INTERNAL:
207                         if (search) {
208                                 if (elm->base.localization > search->lo ||
209                                     (elm->base.localization == search->lo &&
210                                      elm->base.obj_id > search->obj_id)) {
211                                         break;
212                                 }
213                                 if (elm[1].base.localization < search->lo ||
214                                     (elm[1].base.localization == search->lo &&
215                                      elm[1].base.obj_id < search->obj_id)) {
216                                         break;
217                                 }
218                         }
219                         if (elm->internal.subtree_offset) {
220                                 print_btree_node(elm->internal.subtree_offset,
221                                                  search, depth + 1,
222                                                  elm->internal.mirror_tid,
223                                                  &elm[0].base, &elm[1].base);
224                                 /*
225                                  * Cause show to iterate after seeking to
226                                  * the lo:objid
227                                  */
228                                 search = NULL;
229                         }
230                         break;
231                 default:
232                         break;
233                 }
234         }
235         rel_buffer(buffer);
236 }
237
238 static
239 void
240 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
241                 int flags, const char *label, const char *ext)
242 {
243         char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
244
245         flagstr[0] = flags ? 'B' : 'G';
246         if (flags & FLAG_TOOFARLEFT)
247                 flagstr[2] = 'L';
248         if (flags & FLAG_TOOFARRIGHT)
249                 flagstr[3] = 'R';
250         if (flags & FLAG_BADTYPE)
251                 flagstr[4] = 'T';
252         if (flags & FLAG_BADCHILDPARENT)
253                 flagstr[5] = 'C';
254         if (flags & FLAG_BADMIRRORTID)
255                 flagstr[6] = 'M';
256
257         printf("%s\t%s %2d %c ",
258                flagstr, label, i,
259                (elm->base.btype ? elm->base.btype : '?'));
260         printf("lo=%08x obj=%016jx rt=%02x key=%016jx ot=%02x\n",
261                elm->base.localization,
262                (uintmax_t)elm->base.obj_id,
263                elm->base.rec_type,
264                (uintmax_t)elm->base.key,
265                elm->base.obj_type);
266         printf("\t       %c tids %016jx:%016jx ",
267                 (elm->base.delete_tid ? 'd' : ' '),
268                (uintmax_t)elm->base.create_tid,
269                (uintmax_t)elm->base.delete_tid);
270
271         switch(type) {
272         case HAMMER_BTREE_TYPE_INTERNAL:
273                 printf("suboff=%016jx",
274                        (uintmax_t)elm->internal.subtree_offset);
275                 if (QuietOpt < 3) {
276                         printf(" mirror %016jx",
277                                (uintmax_t)elm->internal.mirror_tid);
278                 }
279                 if (ext)
280                         printf(" %s", ext);
281                 break;
282         case HAMMER_BTREE_TYPE_LEAF:
283                 if (ext)
284                         printf(" %s", ext);
285                 switch(elm->base.btype) {
286                 case HAMMER_BTREE_TYPE_RECORD:
287                         if (QuietOpt < 3)
288                                 printf("\n%s\t         ", check_data_crc(elm));
289                         else
290                                 printf("\n\t         ");
291                         printf("dataoff=%016jx/%d",
292                                (uintmax_t)elm->leaf.data_offset,
293                                elm->leaf.data_len);
294                         if (QuietOpt < 3) {
295                                 printf(" crc=%04x", elm->leaf.data_crc);
296                                 printf("\n\t         fills=");
297                                 print_bigblock_fill(elm->leaf.data_offset);
298                         }
299                         if (QuietOpt < 2)
300                                 print_record(elm);
301                         break;
302                 }
303                 break;
304         default:
305                 break;
306         }
307         printf("\n");
308 }
309
310 static
311 int
312 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
313                 hammer_btree_elm_t elm, u_int8_t btype,
314                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
315 {
316         int flags = 0;
317
318         switch(node->type) {
319         case HAMMER_BTREE_TYPE_INTERNAL:
320                 if (elm->internal.subtree_offset) {
321                         struct buffer_info *buffer = NULL;
322                         hammer_node_ondisk_t subnode;
323
324                         subnode = get_node(elm->internal.subtree_offset,
325                                            &buffer);
326                         if (subnode == NULL)
327                                 flags |= FLAG_BADCHILDPARENT;
328                         else if (subnode->parent != node_offset)
329                                 flags |= FLAG_BADCHILDPARENT;
330                         rel_buffer(buffer);
331                 }
332                 if (elm->internal.mirror_tid > node->mirror_tid)
333                         flags |= FLAG_BADMIRRORTID;
334
335                 switch(btype) {
336                 case HAMMER_BTREE_TYPE_INTERNAL:
337                         if (left_bound == NULL || right_bound == NULL)
338                                 break;
339                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
340                                 flags |= FLAG_TOOFARLEFT;
341                         if (hammer_btree_cmp(&elm->base, right_bound) > 0)
342                                 flags |= FLAG_TOOFARRIGHT;
343                         break;
344                 case HAMMER_BTREE_TYPE_LEAF:
345                         if (left_bound == NULL || right_bound == NULL)
346                                 break;
347                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
348                                 flags |= FLAG_TOOFARLEFT;
349                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
350                                 flags |= FLAG_TOOFARRIGHT;
351                         break;
352                 default:
353                         flags |= FLAG_BADTYPE;
354                         break;
355                 }
356                 break;
357         case HAMMER_BTREE_TYPE_LEAF:
358                 if (elm->base.create_tid &&
359                     elm->base.create_tid > node->mirror_tid) {
360                         flags |= FLAG_BADMIRRORTID;
361                 }
362                 if (elm->base.delete_tid &&
363                     elm->base.delete_tid > node->mirror_tid) {
364                         flags |= FLAG_BADMIRRORTID;
365                 }
366                 switch(btype) {
367                 case HAMMER_BTREE_TYPE_RECORD:
368                         if (left_bound == NULL || right_bound == NULL)
369                                 break;
370                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
371                                 flags |= FLAG_TOOFARLEFT;
372                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
373                                 flags |= FLAG_TOOFARRIGHT;
374                         break;
375                 default:
376                         flags |= FLAG_BADTYPE;
377                         break;
378                 }
379                 break;
380         default:
381                 flags |= FLAG_BADTYPE;
382                 break;
383         }
384         return(flags);
385 }
386
387 static
388 void
389 print_bigblock_fill(hammer_off_t offset)
390 {
391         struct hammer_blockmap_layer1 layer1;
392         struct hammer_blockmap_layer2 layer2;
393         int fill;
394         int error;
395
396         blockmap_lookup(offset, &layer1, &layer2, &error);
397         if (error) {
398                 printf("z%d:%lld=BADZ",
399                         HAMMER_ZONE_DECODE(offset),
400                         (offset & ~HAMMER_OFF_ZONE_MASK) /
401                             HAMMER_LARGEBLOCK_SIZE
402                 );
403         } else {
404                 fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
405                 fill = 100 - fill;
406
407                 printf("z%d:%lld=%d%%",
408                         HAMMER_ZONE_DECODE(offset),
409                         (offset & ~HAMMER_OFF_ZONE_MASK) /
410                             HAMMER_LARGEBLOCK_SIZE,
411                         fill
412                 );
413         }
414 }
415
416 /*
417  * Check the generic crc on a data element.  Inodes record types are
418  * special in that some of their fields are not CRCed.
419  *
420  * Also check that the zone is valid.
421  */
422 static
423 const char *
424 check_data_crc(hammer_btree_elm_t elm)
425 {
426         struct buffer_info *data_buffer;
427         hammer_off_t data_offset;
428         int32_t data_len;
429         int32_t len;
430         u_int32_t crc;
431         int error;
432         char *ptr;
433
434         data_offset = elm->leaf.data_offset;
435         data_len = elm->leaf.data_len;
436         data_buffer = NULL;
437         if (data_offset == 0 || data_len == 0)
438                 return("Z");
439
440         crc = 0;
441         error = 0;
442         while (data_len) {
443                 blockmap_lookup(data_offset, NULL, NULL, &error);
444                 if (error)
445                         break;
446
447                 ptr = get_buffer_data(data_offset, &data_buffer, 0);
448                 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
449                 if (len > data_len)
450                         len = (int)data_len;
451                 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
452                     data_len == sizeof(struct hammer_inode_data)) {
453                         crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
454                 } else {
455                         crc = crc32_ext(ptr, len, crc);
456                 }
457                 data_len -= len;
458                 data_offset += len;
459         }
460         if (data_buffer)
461                 rel_buffer(data_buffer);
462         if (error)
463                 return("BO");           /* bad offset */
464         if (crc == elm->leaf.data_crc)
465                 return("");
466         return("BX");                   /* bad crc */
467 }
468
469 static
470 void
471 print_record(hammer_btree_elm_t elm)
472 {
473         struct buffer_info *data_buffer;
474         hammer_off_t data_offset;
475         int32_t data_len;
476         hammer_data_ondisk_t data;
477
478         data_offset = elm->leaf.data_offset;
479         data_len = elm->leaf.data_len;
480         data_buffer = NULL;
481
482         if (data_offset)
483                 data = get_buffer_data(data_offset, &data_buffer, 0);
484         else
485                 data = NULL;
486
487         switch(elm->leaf.base.rec_type) {
488         case HAMMER_RECTYPE_INODE:
489                 printf("\n%17s", "");
490                 printf("size=%jd nlinks=%jd",
491                        (intmax_t)data->inode.size,
492                        (intmax_t)data->inode.nlinks);
493                 if (QuietOpt < 1) {
494                         printf(" mode=%05o uflags=%08x\n",
495                                 data->inode.mode,
496                                 data->inode.uflags);
497                         printf("%17s", "");
498                         printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
499                                 (uintmax_t)data->inode.ctime,
500                                 (uintmax_t)data->inode.parent_obj_id,
501                                 data->inode.obj_type);
502                         printf("%17s", "");
503                         printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
504                         printf(" caps=%02x", data->inode.cap_flags);
505                 }
506                 break;
507         case HAMMER_RECTYPE_DIRENTRY:
508                 printf("\n%17s", "");
509                 data_len -= HAMMER_ENTRY_NAME_OFF;
510                 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
511                        (uintmax_t)data->entry.obj_id,
512                        data->entry.localization,
513                        data_len, data_len, data->entry.name);
514                 break;
515         case HAMMER_RECTYPE_FIX:
516                 switch(elm->leaf.base.key) {
517                 case HAMMER_FIXKEY_SYMLINK:
518                         data_len -= HAMMER_SYMLINK_NAME_OFF;
519                         printf("\n%17s", "");
520                         printf("symlink=\"%*.*s\"", data_len, data_len,
521                                 data->symlink.name);
522                         break;
523                 default:
524                         break;
525                 }
526                 break;
527         default:
528                 break;
529         }
530         if (data_buffer)
531                 rel_buffer(data_buffer);
532 }
533
534 /*
535  * Dump the UNDO FIFO
536  */
537 void
538 hammer_cmd_show_undo(void)
539 {
540         struct volume_info *volume;
541         hammer_blockmap_t rootmap;
542         hammer_off_t scan_offset;
543         hammer_fifo_any_t head;
544         struct buffer_info *data_buffer = NULL;
545
546         volume = get_volume(RootVolNo);
547         rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
548         printf("Volume header UNDO %016jx-%016jx/%016jx\n",
549                 (intmax_t)rootmap->first_offset,
550                 (intmax_t)rootmap->next_offset,
551                 (intmax_t)rootmap->alloc_offset);
552         printf("Undo map is %jdMB\n",
553                 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
554                            (1024 * 1024)));
555         scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
556         while (scan_offset < rootmap->alloc_offset) {
557                 head = get_buffer_data(scan_offset, &data_buffer, 0);
558                 printf("%016jx ", scan_offset);
559
560                 switch(head->head.hdr_type) {
561                 case HAMMER_HEAD_TYPE_PAD:
562                         printf("PAD(%04x)\n", head->head.hdr_size);
563                         break;
564                 case HAMMER_HEAD_TYPE_DUMMY:
565                         printf("DUMMY(%04x) seq=%08x\n",
566                                 head->head.hdr_size, head->head.hdr_seq);
567                         break;
568                 case HAMMER_HEAD_TYPE_UNDO:
569                         printf("UNDO(%04x) seq=%08x "
570                                "dataoff=%016jx bytes=%d\n",
571                                 head->head.hdr_size, head->head.hdr_seq,
572                                 (intmax_t)head->undo.undo_offset,
573                                 head->undo.undo_data_bytes);
574                         break;
575                 case HAMMER_HEAD_TYPE_REDO:
576                         printf("REDO(%04x) seq=%08x flags=%08x "
577                                "objid=%016jx logoff=%016jx bytes=%d\n",
578                                 head->head.hdr_size, head->head.hdr_seq,
579                                 head->redo.redo_flags,
580                                 (intmax_t)head->redo.redo_objid,
581                                 (intmax_t)head->redo.redo_offset,
582                                 head->redo.redo_data_bytes);
583                         break;
584                 default:
585                         printf("UNKNOWN(%04x,%04x) seq=%08x\n",
586                                 head->head.hdr_type,
587                                 head->head.hdr_size,
588                                 head->head.hdr_seq);
589                         break;
590                 }
591                 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
592                     head->head.hdr_size == 0 ||
593                     head->head.hdr_size > HAMMER_UNDO_ALIGN -
594                                     ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
595                         printf("Illegal size field, skipping to "
596                                "next boundary\n");
597                         scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
598                                         ~HAMMER_UNDO_MASK64;
599                 } else {
600                         scan_offset += head->head.hdr_size;
601                 }
602         }
603         if (data_buffer)
604                 rel_buffer(data_buffer);
605 }