xref: /dragonfly/sbin/hammer/cmd_show.c (revision cc93b0eb)
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  * $DragonFly: src/sbin/hammer/cmd_show.c,v 1.15 2008/07/07 00:27:22 dillon Exp $
35  */
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 
44 static void print_btree_node(hammer_off_t node_offset, int depth, int spike,
45 			hammer_base_elm_t left_bound,
46 			hammer_base_elm_t right_bound);
47 static void print_record(hammer_btree_elm_t elm);
48 static void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
49 			int flags, const char *label);
50 static int print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
51 			hammer_btree_elm_t elm, u_int8_t btype,
52 			hammer_base_elm_t left_bound,
53 			hammer_base_elm_t right_bound);
54 static void print_bigblock_fill(hammer_off_t offset);
55 
56 void
57 hammer_cmd_show(hammer_off_t node_offset, int depth,
58 		hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
59 {
60 	struct volume_info *volume;
61 
62 	if (node_offset == (hammer_off_t)-1) {
63 		volume = get_volume(RootVolNo);
64 		node_offset = volume->ondisk->vol0_btree_root;
65 		if (VerboseOpt) {
66 			printf("Volume header\trecords=%lld next_tid=%016llx\n",
67 			       volume->ondisk->vol0_stat_records,
68 			       volume->ondisk->vol0_next_tid);
69 		}
70 		rel_volume(volume);
71 	}
72 	printf("show %016llx depth %d\n", node_offset, depth);
73 	print_btree_node(node_offset, depth, 0, left_bound, right_bound);
74 	print_btree_node(node_offset, depth, 1, left_bound, right_bound);
75 }
76 
77 static void
78 print_btree_node(hammer_off_t node_offset, int depth, int spike,
79 		 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
80 {
81 	struct buffer_info *buffer = NULL;
82 	hammer_node_ondisk_t node;
83 	hammer_btree_elm_t elm;
84 	int i;
85 	int flags;
86 	int maxcount;
87 	char badc;
88 
89 	node = get_node(node_offset, &buffer);
90 
91 	if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
92 		badc = ' ';
93 	else
94 		badc = 'B';
95 
96 	if (spike == 0) {
97 		printf("%c   NODE %016llx cnt=%d p=%016llx "
98 		       "type=%c depth=%d",
99 		       badc,
100 		       node_offset, node->count, node->parent,
101 		       (node->type ? node->type : '?'), depth);
102 		printf(" mirror %016llx", node->mirror_tid);
103 		if (VerboseOpt) {
104 			printf(" fill=");
105 			print_bigblock_fill(node_offset);
106 		}
107 		printf(" {\n");
108 
109 		maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
110 			   HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
111 
112 		for (i = 0; i < node->count && i < maxcount; ++i) {
113 			elm = &node->elms[i];
114 			flags = print_elm_flags(node, node_offset,
115 						elm, elm->base.btype,
116 						left_bound, right_bound);
117 			print_btree_elm(elm, i, node->type, flags, "ELM");
118 		}
119 		if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
120 			elm = &node->elms[i];
121 			flags = print_elm_flags(node, node_offset,
122 						elm, 'I',
123 						left_bound, right_bound);
124 			print_btree_elm(elm, i, node->type, flags, "RBN");
125 		}
126 		printf("    }\n");
127 	}
128 
129 	for (i = 0; i < node->count; ++i) {
130 		elm = &node->elms[i];
131 
132 		switch(node->type) {
133 		case HAMMER_BTREE_TYPE_INTERNAL:
134 			if (elm->internal.subtree_offset) {
135 				print_btree_node(elm->internal.subtree_offset,
136 						 depth + 1, spike,
137 						 &elm[0].base, &elm[1].base);
138 			}
139 			break;
140 		default:
141 			break;
142 		}
143 	}
144 	rel_buffer(buffer);
145 }
146 
147 static
148 void
149 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
150 		int flags, const char *label)
151 {
152 	char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
153 
154 	flagstr[0] = flags ? 'B' : 'G';
155 	if (flags & FLAG_TOOFARLEFT)
156 		flagstr[2] = 'L';
157 	if (flags & FLAG_TOOFARRIGHT)
158 		flagstr[3] = 'R';
159 	if (flags & FLAG_BADTYPE)
160 		flagstr[4] = 'T';
161 	if (flags & FLAG_BADCHILDPARENT)
162 		flagstr[4] = 'C';
163 
164 	printf("%s\t%s %2d %c ",
165 	       flagstr, label, i,
166 	       (elm->base.btype ? elm->base.btype : '?'));
167 	printf("obj=%016llx key=%016llx lo=%08x rt=%02x ot=%02x\n",
168 	       elm->base.obj_id,
169 	       elm->base.key,
170 	       elm->base.localization,
171 	       elm->base.rec_type,
172 	       elm->base.obj_type);
173 	printf("\t       %c tids %016llx:%016llx ",
174 		(elm->base.delete_tid ? 'd' : ' '),
175 	       elm->base.create_tid,
176 	       elm->base.delete_tid);
177 
178 	switch(type) {
179 	case HAMMER_BTREE_TYPE_INTERNAL:
180 		printf("suboff=%016llx", elm->internal.subtree_offset);
181 		if (VerboseOpt)
182 			printf(" mirror %016llx", elm->internal.mirror_tid);
183 		break;
184 	case HAMMER_BTREE_TYPE_LEAF:
185 		switch(elm->base.btype) {
186 		case HAMMER_BTREE_TYPE_RECORD:
187 			printf("\n\t         ");
188 			printf(" dataoff=%016llx/%d",
189 				elm->leaf.data_offset, elm->leaf.data_len);
190 			if (VerboseOpt) {
191 				printf("\n\t         fills=");
192 				print_bigblock_fill(elm->leaf.data_offset);
193 			}
194 			if (VerboseOpt > 1)
195 				print_record(elm);
196 			break;
197 		}
198 		break;
199 	default:
200 		break;
201 	}
202 	printf("\n");
203 }
204 
205 static
206 int
207 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
208 		hammer_btree_elm_t elm, u_int8_t btype,
209 		hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
210 {
211 	int flags = 0;
212 
213 	switch(node->type) {
214 	case HAMMER_BTREE_TYPE_INTERNAL:
215 		if (elm->internal.subtree_offset) {
216 			struct buffer_info *buffer = NULL;
217 			hammer_node_ondisk_t subnode;
218 
219 			subnode = get_node(elm->internal.subtree_offset,
220 					   &buffer);
221 			if (subnode->parent != node_offset)
222 				flags |= FLAG_BADCHILDPARENT;
223 			rel_buffer(buffer);
224 		}
225 
226 		switch(btype) {
227 		case HAMMER_BTREE_TYPE_INTERNAL:
228 			if (left_bound == NULL || right_bound == NULL)
229 				break;
230 			if (hammer_btree_cmp(&elm->base, left_bound) < 0)
231 				flags |= FLAG_TOOFARLEFT;
232 			if (hammer_btree_cmp(&elm->base, right_bound) > 0)
233 				flags |= FLAG_TOOFARRIGHT;
234 			break;
235 		case HAMMER_BTREE_TYPE_LEAF:
236 			if (left_bound == NULL || right_bound == NULL)
237 				break;
238 			if (hammer_btree_cmp(&elm->base, left_bound) < 0)
239 				flags |= FLAG_TOOFARLEFT;
240 			if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
241 				flags |= FLAG_TOOFARRIGHT;
242 			break;
243 		default:
244 			flags |= FLAG_BADTYPE;
245 			break;
246 		}
247 		break;
248 	case HAMMER_BTREE_TYPE_LEAF:
249 		switch(btype) {
250 		case HAMMER_BTREE_TYPE_RECORD:
251 			if (left_bound == NULL || right_bound == NULL)
252 				break;
253 			if (hammer_btree_cmp(&elm->base, left_bound) < 0)
254 				flags |= FLAG_TOOFARLEFT;
255 			if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
256 				flags |= FLAG_TOOFARRIGHT;
257 			break;
258 		default:
259 			flags |= FLAG_BADTYPE;
260 			break;
261 		}
262 		break;
263 	default:
264 		flags |= FLAG_BADTYPE;
265 		break;
266 	}
267 	return(flags);
268 }
269 
270 static
271 void
272 print_bigblock_fill(hammer_off_t offset)
273 {
274 	struct hammer_blockmap_layer1 layer1;
275 	struct hammer_blockmap_layer2 layer2;
276 	int fill;
277 
278 	blockmap_lookup(offset, &layer1, &layer2);
279 	fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
280 	fill = 100 - fill;
281 
282 	printf("z%d:%lld=%d%%",
283 		HAMMER_ZONE_DECODE(offset),
284 		(offset & ~HAMMER_OFF_ZONE_MASK) / HAMMER_LARGEBLOCK_SIZE,
285 		fill
286 	);
287 }
288 
289 static
290 void
291 print_record(hammer_btree_elm_t elm)
292 {
293 	struct buffer_info *data_buffer;
294 	hammer_off_t data_offset;
295 	int32_t data_len;
296 	hammer_data_ondisk_t data;
297 
298 	data_offset = elm->leaf.data_offset;
299 	data_len = elm->leaf.data_len;
300 	data_buffer = NULL;
301 
302 	if (data_offset)
303 		data = get_buffer_data(data_offset, &data_buffer, 0);
304 	else
305 		data = NULL;
306 
307 	switch(elm->leaf.base.rec_type) {
308 	case HAMMER_RECTYPE_INODE:
309 		printf("\n%17s", "");
310 		printf("size=%lld nlinks=%lld",
311 		       data->inode.size, data->inode.nlinks);
312 		if (VerboseOpt > 2) {
313 			printf(" mode=%05o uflags=%08x\n",
314 				data->inode.mode,
315 				data->inode.uflags);
316 			printf("%17s", "");
317 			printf("ctime=%016llx pobjid=%016llx obj_type=%d\n",
318 				data->inode.ctime, data->inode.parent_obj_id,
319 				data->inode.obj_type);
320 			printf("%17s", "");
321 			printf("mtime=%016llx", data->inode.mtime);
322 		}
323 		break;
324 	case HAMMER_RECTYPE_DIRENTRY:
325 		printf("\n%17s", "");
326 		data_len -= HAMMER_ENTRY_NAME_OFF;
327 		printf("dir-entry ino=%016llx lo=%08x name=\"%*.*s\"",
328 		       data->entry.obj_id,
329 		       data->entry.localization,
330 		       data_len, data_len, data->entry.name);
331 		break;
332 	case HAMMER_RECTYPE_FIX:
333 		switch(elm->leaf.base.key) {
334 		case HAMMER_FIXKEY_SYMLINK:
335 			data_len -= HAMMER_SYMLINK_NAME_OFF;
336 			printf("\n%17s", "");
337 			printf("symlink=\"%*.*s\"", data_len, data_len,
338 				data->symlink.name);
339 			break;
340 		default:
341 			break;
342 		}
343 		break;
344 	default:
345 		break;
346 	}
347 	if (data_buffer)
348 		rel_buffer(data_buffer);
349 }
350 
351