xref: /illumos-gate/usr/src/cmd/zdb/zdb_il.c (revision b24ab676)
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 = (char *)(lr + 1);
60 	char *link = name + strlen(name) + 1;
61 
62 	if (txtype == TX_SYMLINK)
63 		(void) printf("%s%s -> %s\n", prefix, name, link);
64 	else
65 		(void) printf("%s%s\n", prefix, name);
66 
67 	(void) printf("%s%s", prefix, ctime(&crtime));
68 	(void) printf("%sdoid %llu, foid %llu, mode %llo\n", prefix,
69 	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_foid,
70 	    (longlong_t)lr->lr_mode);
71 	(void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n", prefix,
72 	    (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
73 	    (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
74 }
75 
76 /* ARGSUSED */
77 static void
78 zil_prt_rec_remove(zilog_t *zilog, int txtype, lr_remove_t *lr)
79 {
80 	(void) printf("%sdoid %llu, name %s\n", prefix,
81 	    (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
82 }
83 
84 /* ARGSUSED */
85 static void
86 zil_prt_rec_link(zilog_t *zilog, int txtype, lr_link_t *lr)
87 {
88 	(void) printf("%sdoid %llu, link_obj %llu, name %s\n", prefix,
89 	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
90 	    (char *)(lr + 1));
91 }
92 
93 /* ARGSUSED */
94 static void
95 zil_prt_rec_rename(zilog_t *zilog, int txtype, lr_rename_t *lr)
96 {
97 	char *snm = (char *)(lr + 1);
98 	char *tnm = snm + strlen(snm) + 1;
99 
100 	(void) printf("%ssdoid %llu, tdoid %llu\n", prefix,
101 	    (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
102 	(void) printf("%ssrc %s tgt %s\n", prefix, snm, tnm);
103 }
104 
105 /* ARGSUSED */
106 static void
107 zil_prt_rec_write(zilog_t *zilog, int txtype, lr_write_t *lr)
108 {
109 	char *data, *dlimit;
110 	blkptr_t *bp = &lr->lr_blkptr;
111 	zbookmark_t zb;
112 	char buf[SPA_MAXBLOCKSIZE];
113 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
114 	int error;
115 
116 	(void) printf("%sfoid %llu, offset %llx, length %llx\n", prefix,
117 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
118 	    (u_longlong_t)lr->lr_length);
119 
120 	if (txtype == TX_WRITE2 || verbose < 5)
121 		return;
122 
123 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
124 		(void) printf("%shas blkptr, %s\n", prefix,
125 		    bp->blk_birth >= spa_first_txg(zilog->zl_spa) ?
126 		    "will claim" : "won't claim");
127 		print_log_bp(bp, prefix);
128 
129 		if (BP_IS_HOLE(bp)) {
130 			(void) printf("\t\t\tLSIZE 0x%llx\n",
131 			    (u_longlong_t)BP_GET_LSIZE(bp));
132 		}
133 		if (bp->blk_birth == 0) {
134 			bzero(buf, sizeof (buf));
135 			(void) printf("%s<hole>\n", prefix);
136 			return;
137 		}
138 		if (bp->blk_birth < zilog->zl_header->zh_claim_txg) {
139 			(void) printf("%s<block already committed>\n", prefix);
140 			return;
141 		}
142 
143 		SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
144 		    lr->lr_foid, ZB_ZIL_LEVEL,
145 		    lr->lr_offset / BP_GET_LSIZE(bp));
146 
147 		error = zio_wait(zio_read(NULL, zilog->zl_spa,
148 		    bp, buf, BP_GET_LSIZE(bp), NULL, NULL,
149 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
150 		if (error)
151 			return;
152 		data = buf;
153 	} else {
154 		data = (char *)(lr + 1);
155 	}
156 
157 	dlimit = data + MIN(lr->lr_length,
158 	    (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE));
159 
160 	(void) printf("%s", prefix);
161 	while (data < dlimit) {
162 		if (isprint(*data))
163 			(void) printf("%c ", *data);
164 		else
165 			(void) printf("%2X", *data);
166 		data++;
167 	}
168 	(void) printf("\n");
169 }
170 
171 /* ARGSUSED */
172 static void
173 zil_prt_rec_truncate(zilog_t *zilog, int txtype, lr_truncate_t *lr)
174 {
175 	(void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", prefix,
176 	    (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
177 	    (u_longlong_t)lr->lr_length);
178 }
179 
180 /* ARGSUSED */
181 static void
182 zil_prt_rec_setattr(zilog_t *zilog, int txtype, lr_setattr_t *lr)
183 {
184 	time_t atime = (time_t)lr->lr_atime[0];
185 	time_t mtime = (time_t)lr->lr_mtime[0];
186 
187 	(void) printf("%sfoid %llu, mask 0x%llx\n", prefix,
188 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
189 
190 	if (lr->lr_mask & AT_MODE) {
191 		(void) printf("%sAT_MODE  %llo\n", prefix,
192 		    (longlong_t)lr->lr_mode);
193 	}
194 
195 	if (lr->lr_mask & AT_UID) {
196 		(void) printf("%sAT_UID   %llu\n", prefix,
197 		    (u_longlong_t)lr->lr_uid);
198 	}
199 
200 	if (lr->lr_mask & AT_GID) {
201 		(void) printf("%sAT_GID   %llu\n", prefix,
202 		    (u_longlong_t)lr->lr_gid);
203 	}
204 
205 	if (lr->lr_mask & AT_SIZE) {
206 		(void) printf("%sAT_SIZE  %llu\n", prefix,
207 		    (u_longlong_t)lr->lr_size);
208 	}
209 
210 	if (lr->lr_mask & AT_ATIME) {
211 		(void) printf("%sAT_ATIME %llu.%09llu %s", prefix,
212 		    (u_longlong_t)lr->lr_atime[0],
213 		    (u_longlong_t)lr->lr_atime[1],
214 		    ctime(&atime));
215 	}
216 
217 	if (lr->lr_mask & AT_MTIME) {
218 		(void) printf("%sAT_MTIME %llu.%09llu %s", prefix,
219 		    (u_longlong_t)lr->lr_mtime[0],
220 		    (u_longlong_t)lr->lr_mtime[1],
221 		    ctime(&mtime));
222 	}
223 }
224 
225 /* ARGSUSED */
226 static void
227 zil_prt_rec_acl(zilog_t *zilog, int txtype, lr_acl_t *lr)
228 {
229 	(void) printf("%sfoid %llu, aclcnt %llu\n", prefix,
230 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
231 }
232 
233 typedef void (*zil_prt_rec_func_t)();
234 typedef struct zil_rec_info {
235 	zil_prt_rec_func_t	zri_print;
236 	char			*zri_name;
237 	uint64_t		zri_count;
238 } zil_rec_info_t;
239 
240 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
241 	{	NULL,			"Total              " },
242 	{	zil_prt_rec_create,	"TX_CREATE          " },
243 	{	zil_prt_rec_create,	"TX_MKDIR           " },
244 	{	zil_prt_rec_create,	"TX_MKXATTR         " },
245 	{	zil_prt_rec_create,	"TX_SYMLINK         " },
246 	{	zil_prt_rec_remove,	"TX_REMOVE          " },
247 	{	zil_prt_rec_remove,	"TX_RMDIR           " },
248 	{	zil_prt_rec_link,	"TX_LINK            " },
249 	{	zil_prt_rec_rename,	"TX_RENAME          " },
250 	{	zil_prt_rec_write,	"TX_WRITE           " },
251 	{	zil_prt_rec_truncate,	"TX_TRUNCATE        " },
252 	{	zil_prt_rec_setattr,	"TX_SETATTR         " },
253 	{	zil_prt_rec_acl,	"TX_ACL_V0          " },
254 	{	zil_prt_rec_acl,	"TX_ACL_ACL         " },
255 	{	zil_prt_rec_create,	"TX_CREATE_ACL      " },
256 	{	zil_prt_rec_create,	"TX_CREATE_ATTR     " },
257 	{	zil_prt_rec_create,	"TX_CREATE_ACL_ATTR " },
258 	{	zil_prt_rec_create,	"TX_MKDIR_ACL       " },
259 	{	zil_prt_rec_create,	"TX_MKDIR_ATTR      " },
260 	{	zil_prt_rec_create,	"TX_MKDIR_ACL_ATTR  " },
261 	{	zil_prt_rec_write,	"TX_WRITE2          " },
262 };
263 
264 /* ARGSUSED */
265 static int
266 print_log_record(zilog_t *zilog, lr_t *lr, void *arg, uint64_t claim_txg)
267 {
268 	int txtype;
269 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
270 
271 	/* reduce size of txtype to strip off TX_CI bit */
272 	txtype = lr->lrc_txtype;
273 
274 	ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
275 	ASSERT(lr->lrc_txg);
276 
277 	(void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
278 	    (lr->lrc_txtype & TX_CI) ? "CI-" : "",
279 	    zil_rec_info[txtype].zri_name,
280 	    (u_longlong_t)lr->lrc_reclen,
281 	    (u_longlong_t)lr->lrc_txg,
282 	    (u_longlong_t)lr->lrc_seq);
283 
284 	if (txtype && verbose >= 3)
285 		zil_rec_info[txtype].zri_print(zilog, txtype, lr);
286 
287 	zil_rec_info[txtype].zri_count++;
288 	zil_rec_info[0].zri_count++;
289 
290 	return (0);
291 }
292 
293 /* ARGSUSED */
294 static int
295 print_log_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
296 {
297 	char blkbuf[BP_SPRINTF_LEN + 10];
298 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
299 	char *claim;
300 
301 	if (verbose <= 3)
302 		return (0);
303 
304 	if (verbose >= 5) {
305 		(void) strcpy(blkbuf, ", ");
306 		sprintf_blkptr(blkbuf + strlen(blkbuf), bp);
307 	} else {
308 		blkbuf[0] = '\0';
309 	}
310 
311 	if (claim_txg != 0)
312 		claim = "already claimed";
313 	else if (bp->blk_birth >= spa_first_txg(zilog->zl_spa))
314 		claim = "will claim";
315 	else
316 		claim = "won't claim";
317 
318 	(void) printf("\tBlock seqno %llu, %s%s\n",
319 	    (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
320 
321 	return (0);
322 }
323 
324 static void
325 print_log_stats(int verbose)
326 {
327 	int i, w, p10;
328 
329 	if (verbose > 3)
330 		(void) printf("\n");
331 
332 	if (zil_rec_info[0].zri_count == 0)
333 		return;
334 
335 	for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
336 		w++;
337 
338 	for (i = 0; i < TX_MAX_TYPE; i++)
339 		if (zil_rec_info[i].zri_count || verbose >= 3)
340 			(void) printf("\t\t%s %*llu\n",
341 			    zil_rec_info[i].zri_name, w,
342 			    (u_longlong_t)zil_rec_info[i].zri_count);
343 	(void) printf("\n");
344 }
345 
346 /* ARGSUSED */
347 void
348 dump_intent_log(zilog_t *zilog)
349 {
350 	const zil_header_t *zh = zilog->zl_header;
351 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
352 	int i;
353 
354 	if (zh->zh_log.blk_birth == 0 || verbose < 1)
355 		return;
356 
357 	(void) printf("\n    ZIL header: claim_txg %llu, "
358 	    "claim_blk_seq %llu, claim_lr_seq %llu",
359 	    (u_longlong_t)zh->zh_claim_txg,
360 	    (u_longlong_t)zh->zh_claim_blk_seq,
361 	    (u_longlong_t)zh->zh_claim_lr_seq);
362 	(void) printf(" replay_seq %llu, flags 0x%llx\n",
363 	    (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
364 
365 	for (i = 0; i < TX_MAX_TYPE; i++)
366 		zil_rec_info[i].zri_count = 0;
367 
368 	if (verbose >= 2) {
369 		(void) printf("\n");
370 		(void) zil_parse(zilog, print_log_block, print_log_record, NULL,
371 		    zh->zh_claim_txg);
372 		print_log_stats(verbose);
373 	}
374 }
375