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