xref: /illumos-gate/usr/src/cmd/zdb/zdb_il.c (revision faaa6415)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Print intent log header and statistics.
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <sys/zfs_context.h>
34 #include <sys/spa.h>
35 #include <sys/dmu.h>
36 #include <sys/stat.h>
37 #include <sys/resource.h>
38 #include <sys/zil.h>
39 #include <sys/zil_impl.h>
40 
41 extern uint8_t dump_opt[256];
42 
43 static char prefix[4] = "\t\t\t";
44 
45 static void
46 print_log_bp(const blkptr_t *bp, const char *prefix)
47 {
48 	char blkbuf[BP_SPRINTF_LEN];
49 
50 	sprintf_blkptr(blkbuf, bp);
51 	(void) printf("%s%s\n", prefix, blkbuf);
52 }
53 
54 /* ARGSUSED */
55 static void
56 zil_prt_rec_create(zilog_t *zilog, int txtype, lr_create_t *lr)
57 {
58 	time_t crtime = lr->lr_crtime[0];
59 	char *name, *link;
60 	lr_attr_t *lrattr;
61 
62 	name = (char *)(lr + 1);
63 
64 	if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
65 	    lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
66 		lrattr = (lr_attr_t *)(lr + 1);
67 		name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
68 	}
69 
70 	if (txtype == TX_SYMLINK) {
71 		link = name + strlen(name) + 1;
72 		(void) printf("%s%s -> %s\n", prefix, name, link);
73 	} else if (txtype != TX_MKXATTR) {
74 		(void) printf("%s%s\n", prefix, name);
75 	}
76 
77 	(void) printf("%s%s", prefix, ctime(&crtime));
78 	(void) printf("%sdoid %llu, foid %llu, mode %llo\n", prefix,
79 	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_foid,
80 	    (longlong_t)lr->lr_mode);
81 	(void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n", prefix,
82 	    (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
83 	    (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
84 }
85 
86 /* ARGSUSED */
87 static void
88 zil_prt_rec_remove(zilog_t *zilog, int txtype, lr_remove_t *lr)
89 {
90 	(void) printf("%sdoid %llu, name %s\n", prefix,
91 	    (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
92 }
93 
94 /* ARGSUSED */
95 static void
96 zil_prt_rec_link(zilog_t *zilog, int txtype, lr_link_t *lr)
97 {
98 	(void) printf("%sdoid %llu, link_obj %llu, name %s\n", prefix,
99 	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
100 	    (char *)(lr + 1));
101 }
102 
103 /* ARGSUSED */
104 static void
105 zil_prt_rec_rename(zilog_t *zilog, int txtype, lr_rename_t *lr)
106 {
107 	char *snm = (char *)(lr + 1);
108 	char *tnm = snm + strlen(snm) + 1;
109 
110 	(void) printf("%ssdoid %llu, tdoid %llu\n", prefix,
111 	    (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
112 	(void) printf("%ssrc %s tgt %s\n", prefix, snm, tnm);
113 }
114 
115 /* ARGSUSED */
116 static void
117 zil_prt_rec_write(zilog_t *zilog, int txtype, lr_write_t *lr)
118 {
119 	char *data, *dlimit;
120 	blkptr_t *bp = &lr->lr_blkptr;
121 	zbookmark_t zb;
122 	char buf[SPA_MAXBLOCKSIZE];
123 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
124 	int error;
125 
126 	(void) printf("%sfoid %llu, offset %llx, length %llx\n", prefix,
127 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
128 	    (u_longlong_t)lr->lr_length);
129 
130 	if (txtype == TX_WRITE2 || verbose < 5)
131 		return;
132 
133 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
134 		(void) printf("%shas blkptr, %s\n", prefix,
135 		    bp->blk_birth >= spa_first_txg(zilog->zl_spa) ?
136 		    "will claim" : "won't claim");
137 		print_log_bp(bp, prefix);
138 
139 		if (BP_IS_HOLE(bp)) {
140 			(void) printf("\t\t\tLSIZE 0x%llx\n",
141 			    (u_longlong_t)BP_GET_LSIZE(bp));
142 		}
143 		if (bp->blk_birth == 0) {
144 			bzero(buf, sizeof (buf));
145 			(void) printf("%s<hole>\n", prefix);
146 			return;
147 		}
148 		if (bp->blk_birth < zilog->zl_header->zh_claim_txg) {
149 			(void) printf("%s<block already committed>\n", prefix);
150 			return;
151 		}
152 
153 		SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
154 		    lr->lr_foid, ZB_ZIL_LEVEL,
155 		    lr->lr_offset / BP_GET_LSIZE(bp));
156 
157 		error = zio_wait(zio_read(NULL, zilog->zl_spa,
158 		    bp, buf, BP_GET_LSIZE(bp), NULL, NULL,
159 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
160 		if (error)
161 			return;
162 		data = buf;
163 	} else {
164 		data = (char *)(lr + 1);
165 	}
166 
167 	dlimit = data + MIN(lr->lr_length,
168 	    (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE));
169 
170 	(void) printf("%s", prefix);
171 	while (data < dlimit) {
172 		if (isprint(*data))
173 			(void) printf("%c ", *data);
174 		else
175 			(void) printf("%2X", *data);
176 		data++;
177 	}
178 	(void) printf("\n");
179 }
180 
181 /* ARGSUSED */
182 static void
183 zil_prt_rec_truncate(zilog_t *zilog, int txtype, lr_truncate_t *lr)
184 {
185 	(void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", prefix,
186 	    (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
187 	    (u_longlong_t)lr->lr_length);
188 }
189 
190 /* ARGSUSED */
191 static void
192 zil_prt_rec_setattr(zilog_t *zilog, int txtype, lr_setattr_t *lr)
193 {
194 	time_t atime = (time_t)lr->lr_atime[0];
195 	time_t mtime = (time_t)lr->lr_mtime[0];
196 
197 	(void) printf("%sfoid %llu, mask 0x%llx\n", prefix,
198 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
199 
200 	if (lr->lr_mask & AT_MODE) {
201 		(void) printf("%sAT_MODE  %llo\n", prefix,
202 		    (longlong_t)lr->lr_mode);
203 	}
204 
205 	if (lr->lr_mask & AT_UID) {
206 		(void) printf("%sAT_UID   %llu\n", prefix,
207 		    (u_longlong_t)lr->lr_uid);
208 	}
209 
210 	if (lr->lr_mask & AT_GID) {
211 		(void) printf("%sAT_GID   %llu\n", prefix,
212 		    (u_longlong_t)lr->lr_gid);
213 	}
214 
215 	if (lr->lr_mask & AT_SIZE) {
216 		(void) printf("%sAT_SIZE  %llu\n", prefix,
217 		    (u_longlong_t)lr->lr_size);
218 	}
219 
220 	if (lr->lr_mask & AT_ATIME) {
221 		(void) printf("%sAT_ATIME %llu.%09llu %s", prefix,
222 		    (u_longlong_t)lr->lr_atime[0],
223 		    (u_longlong_t)lr->lr_atime[1],
224 		    ctime(&atime));
225 	}
226 
227 	if (lr->lr_mask & AT_MTIME) {
228 		(void) printf("%sAT_MTIME %llu.%09llu %s", prefix,
229 		    (u_longlong_t)lr->lr_mtime[0],
230 		    (u_longlong_t)lr->lr_mtime[1],
231 		    ctime(&mtime));
232 	}
233 }
234 
235 /* ARGSUSED */
236 static void
237 zil_prt_rec_acl(zilog_t *zilog, int txtype, lr_acl_t *lr)
238 {
239 	(void) printf("%sfoid %llu, aclcnt %llu\n", prefix,
240 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
241 }
242 
243 typedef void (*zil_prt_rec_func_t)();
244 typedef struct zil_rec_info {
245 	zil_prt_rec_func_t	zri_print;
246 	char			*zri_name;
247 	uint64_t		zri_count;
248 } zil_rec_info_t;
249 
250 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
251 	{	NULL,			"Total              " },
252 	{	zil_prt_rec_create,	"TX_CREATE          " },
253 	{	zil_prt_rec_create,	"TX_MKDIR           " },
254 	{	zil_prt_rec_create,	"TX_MKXATTR         " },
255 	{	zil_prt_rec_create,	"TX_SYMLINK         " },
256 	{	zil_prt_rec_remove,	"TX_REMOVE          " },
257 	{	zil_prt_rec_remove,	"TX_RMDIR           " },
258 	{	zil_prt_rec_link,	"TX_LINK            " },
259 	{	zil_prt_rec_rename,	"TX_RENAME          " },
260 	{	zil_prt_rec_write,	"TX_WRITE           " },
261 	{	zil_prt_rec_truncate,	"TX_TRUNCATE        " },
262 	{	zil_prt_rec_setattr,	"TX_SETATTR         " },
263 	{	zil_prt_rec_acl,	"TX_ACL_V0          " },
264 	{	zil_prt_rec_acl,	"TX_ACL_ACL         " },
265 	{	zil_prt_rec_create,	"TX_CREATE_ACL      " },
266 	{	zil_prt_rec_create,	"TX_CREATE_ATTR     " },
267 	{	zil_prt_rec_create,	"TX_CREATE_ACL_ATTR " },
268 	{	zil_prt_rec_create,	"TX_MKDIR_ACL       " },
269 	{	zil_prt_rec_create,	"TX_MKDIR_ATTR      " },
270 	{	zil_prt_rec_create,	"TX_MKDIR_ACL_ATTR  " },
271 	{	zil_prt_rec_write,	"TX_WRITE2          " },
272 };
273 
274 /* ARGSUSED */
275 static int
276 print_log_record(zilog_t *zilog, lr_t *lr, void *arg, uint64_t claim_txg)
277 {
278 	int txtype;
279 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
280 
281 	/* reduce size of txtype to strip off TX_CI bit */
282 	txtype = lr->lrc_txtype;
283 
284 	ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
285 	ASSERT(lr->lrc_txg);
286 
287 	(void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
288 	    (lr->lrc_txtype & TX_CI) ? "CI-" : "",
289 	    zil_rec_info[txtype].zri_name,
290 	    (u_longlong_t)lr->lrc_reclen,
291 	    (u_longlong_t)lr->lrc_txg,
292 	    (u_longlong_t)lr->lrc_seq);
293 
294 	if (txtype && verbose >= 3)
295 		zil_rec_info[txtype].zri_print(zilog, txtype, lr);
296 
297 	zil_rec_info[txtype].zri_count++;
298 	zil_rec_info[0].zri_count++;
299 
300 	return (0);
301 }
302 
303 /* ARGSUSED */
304 static int
305 print_log_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
306 {
307 	char blkbuf[BP_SPRINTF_LEN + 10];
308 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
309 	char *claim;
310 
311 	if (verbose <= 3)
312 		return (0);
313 
314 	if (verbose >= 5) {
315 		(void) strcpy(blkbuf, ", ");
316 		sprintf_blkptr(blkbuf + strlen(blkbuf), bp);
317 	} else {
318 		blkbuf[0] = '\0';
319 	}
320 
321 	if (claim_txg != 0)
322 		claim = "already claimed";
323 	else if (bp->blk_birth >= spa_first_txg(zilog->zl_spa))
324 		claim = "will claim";
325 	else
326 		claim = "won't claim";
327 
328 	(void) printf("\tBlock seqno %llu, %s%s\n",
329 	    (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
330 
331 	return (0);
332 }
333 
334 static void
335 print_log_stats(int verbose)
336 {
337 	int i, w, p10;
338 
339 	if (verbose > 3)
340 		(void) printf("\n");
341 
342 	if (zil_rec_info[0].zri_count == 0)
343 		return;
344 
345 	for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
346 		w++;
347 
348 	for (i = 0; i < TX_MAX_TYPE; i++)
349 		if (zil_rec_info[i].zri_count || verbose >= 3)
350 			(void) printf("\t\t%s %*llu\n",
351 			    zil_rec_info[i].zri_name, w,
352 			    (u_longlong_t)zil_rec_info[i].zri_count);
353 	(void) printf("\n");
354 }
355 
356 /* ARGSUSED */
357 void
358 dump_intent_log(zilog_t *zilog)
359 {
360 	const zil_header_t *zh = zilog->zl_header;
361 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
362 	int i;
363 
364 	if (zh->zh_log.blk_birth == 0 || verbose < 1)
365 		return;
366 
367 	(void) printf("\n    ZIL header: claim_txg %llu, "
368 	    "claim_blk_seq %llu, claim_lr_seq %llu",
369 	    (u_longlong_t)zh->zh_claim_txg,
370 	    (u_longlong_t)zh->zh_claim_blk_seq,
371 	    (u_longlong_t)zh->zh_claim_lr_seq);
372 	(void) printf(" replay_seq %llu, flags 0x%llx\n",
373 	    (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
374 
375 	for (i = 0; i < TX_MAX_TYPE; i++)
376 		zil_rec_info[i].zri_count = 0;
377 
378 	if (verbose >= 2) {
379 		(void) printf("\n");
380 		(void) zil_parse(zilog, print_log_block, print_log_record, NULL,
381 		    zh->zh_claim_txg);
382 		print_log_stats(verbose);
383 	}
384 }
385