xref: /illumos-gate/usr/src/cmd/zdb/zdb_il.c (revision 1a220b56b93ff1dc80855691548503117af4cc10)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Print intent log header and statistics.
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <sys/zfs_context.h>
36 #include <sys/spa.h>
37 #include <sys/dmu.h>
38 #include <sys/stat.h>
39 #include <sys/resource.h>
40 #include <sys/zil.h>
41 #include <sys/zil_impl.h>
42 
43 extern uint8_t dump_opt[256];
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_SPRINTF_LEN, 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("\t\t\t%s -> %s\n", name, link);
64 	else
65 		(void) printf("\t\t\t%s\n", name);
66 
67 	(void) printf("\t\t\t%s", ctime(&crtime));
68 	(void) printf("\t\t\tdoid %llu, foid %llu, mode %llo\n",
69 	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_foid,
70 	    (longlong_t)lr->lr_mode);
71 	(void) printf("\t\t\tuid %llu, gid %llu, gen %llu, rdev 0x%llx\n",
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("\t\t\tdoid %llu, name %s\n",
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("\t\t\tdoid %llu, link_obj %llu, name %s\n",
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("\t\t\tsdoid %llu, tdoid %llu\n",
101 	    (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
102 	(void) printf("\t\t\tsrc %s tgt %s\n", 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 	char buf[SPA_MAXBLOCKSIZE];
112 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
113 	int error;
114 
115 	(void) printf("\t\t\tfoid %llu, offset 0x%llx,"
116 	    " length 0x%llx, blkoff 0x%llx\n",
117 	    (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
118 	    (u_longlong_t)lr->lr_length, (u_longlong_t)lr->lr_blkoff);
119 
120 	if (verbose < 5)
121 		return;
122 
123 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
124 		(void) printf("\t\t\thas blkptr, %s\n",
125 		    bp->blk_birth >= spa_first_txg(zilog->zl_spa) ?
126 		    "will claim" : "won't claim");
127 		print_log_bp(bp, "\t\t\t");
128 		if (bp->blk_birth == 0) {
129 			bzero(buf, sizeof (buf));
130 		} else {
131 			zbookmark_t zb;
132 
133 			ASSERT3U(bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], ==,
134 			    dmu_objset_id(zilog->zl_os));
135 
136 			zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
137 			zb.zb_object = 0;
138 			zb.zb_level = -1;
139 			zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
140 
141 			error = zio_wait(zio_read(NULL, zilog->zl_spa,
142 			    bp, buf, BP_GET_LSIZE(bp), NULL, NULL,
143 			    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
144 			if (error)
145 				return;
146 		}
147 		data = buf + lr->lr_blkoff;
148 	} else {
149 		data = (char *)(lr + 1);
150 	}
151 
152 	dlimit = data + MIN(lr->lr_length,
153 	    (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE));
154 
155 	(void) printf("\t\t\t");
156 	while (data < dlimit) {
157 		if (isprint(*data))
158 			(void) printf("%c ", *data);
159 		else
160 			(void) printf("%2X", *data);
161 		data++;
162 	}
163 	(void) printf("\n");
164 }
165 
166 /* ARGSUSED */
167 static void
168 zil_prt_rec_truncate(zilog_t *zilog, int txtype, lr_truncate_t *lr)
169 {
170 	(void) printf("\t\t\tfoid %llu, offset 0x%llx, length 0x%llx\n",
171 	    (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
172 	    (u_longlong_t)lr->lr_length);
173 }
174 
175 /* ARGSUSED */
176 static void
177 zil_prt_rec_setattr(zilog_t *zilog, int txtype, lr_setattr_t *lr)
178 {
179 	time_t atime = (time_t)lr->lr_atime[0];
180 	time_t mtime = (time_t)lr->lr_mtime[0];
181 
182 	(void) printf("\t\t\tfoid %llu, mask 0x%llx\n",
183 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
184 
185 	if (lr->lr_mask & AT_MODE) {
186 		(void) printf("\t\t\tAT_MODE  %llo\n",
187 		    (longlong_t)lr->lr_mode);
188 	}
189 
190 	if (lr->lr_mask & AT_UID) {
191 		(void) printf("\t\t\tAT_UID   %llu\n",
192 		    (u_longlong_t)lr->lr_uid);
193 	}
194 
195 	if (lr->lr_mask & AT_GID) {
196 		(void) printf("\t\t\tAT_GID   %llu\n",
197 		    (u_longlong_t)lr->lr_gid);
198 	}
199 
200 	if (lr->lr_mask & AT_SIZE) {
201 		(void) printf("\t\t\tAT_SIZE  %llu\n",
202 		    (u_longlong_t)lr->lr_size);
203 	}
204 
205 	if (lr->lr_mask & AT_ATIME) {
206 		(void) printf("\t\t\tAT_ATIME %llu.%09llu %s",
207 		    (u_longlong_t)lr->lr_atime[0],
208 		    (u_longlong_t)lr->lr_atime[1],
209 		    ctime(&atime));
210 	}
211 
212 	if (lr->lr_mask & AT_MTIME) {
213 		(void) printf("\t\t\tAT_MTIME %llu.%09llu %s",
214 		    (u_longlong_t)lr->lr_mtime[0],
215 		    (u_longlong_t)lr->lr_mtime[1],
216 		    ctime(&mtime));
217 	}
218 }
219 
220 /* ARGSUSED */
221 static void
222 zil_prt_rec_acl(zilog_t *zilog, int txtype, lr_acl_t *lr)
223 {
224 	(void) printf("\t\t\tfoid %llu, aclcnt %llu\n",
225 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
226 }
227 
228 typedef void (*zil_prt_rec_func_t)();
229 typedef struct zil_rec_info {
230 	zil_prt_rec_func_t	zri_print;
231 	char			*zri_name;
232 	uint64_t		zri_count;
233 } zil_rec_info_t;
234 
235 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
236 	{	NULL,			"Total      "	},
237 	{	zil_prt_rec_create,	"TX_CREATE  "	},
238 	{	zil_prt_rec_create,	"TX_MKDIR   "	},
239 	{	zil_prt_rec_create,	"TX_MKXATTR "	},
240 	{	zil_prt_rec_create,	"TX_SYMLINK "	},
241 	{	zil_prt_rec_remove,	"TX_REMOVE  "	},
242 	{	zil_prt_rec_remove,	"TX_RMDIR   "	},
243 	{	zil_prt_rec_link,	"TX_LINK    "	},
244 	{	zil_prt_rec_rename,	"TX_RENAME  "	},
245 	{	zil_prt_rec_write,	"TX_WRITE   "	},
246 	{	zil_prt_rec_truncate,	"TX_TRUNCATE"	},
247 	{	zil_prt_rec_setattr,	"TX_SETATTR "	},
248 	{	zil_prt_rec_acl,	"TX_ACL     "	},
249 };
250 
251 /* ARGSUSED */
252 static void
253 print_log_record(zilog_t *zilog, lr_t *lr, void *arg, uint64_t claim_txg)
254 {
255 	int txtype;
256 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
257 
258 	txtype = lr->lrc_txtype;
259 
260 	ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
261 	ASSERT(lr->lrc_txg);
262 
263 	(void) printf("\t\t%s len %6llu, txg %llu, seq %llu\n",
264 	    zil_rec_info[txtype].zri_name,
265 	    (u_longlong_t)lr->lrc_reclen,
266 	    (u_longlong_t)lr->lrc_txg,
267 	    (u_longlong_t)lr->lrc_seq);
268 
269 	if (txtype && verbose >= 3)
270 		zil_rec_info[txtype].zri_print(zilog, txtype, lr);
271 
272 	zil_rec_info[txtype].zri_count++;
273 	zil_rec_info[0].zri_count++;
274 }
275 
276 /* ARGSUSED */
277 static void
278 print_log_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
279 {
280 	char blkbuf[BP_SPRINTF_LEN];
281 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
282 	char *claim;
283 
284 	if (verbose <= 3)
285 		return;
286 
287 	if (verbose >= 5) {
288 		(void) strcpy(blkbuf, ", ");
289 		sprintf_blkptr(blkbuf + strlen(blkbuf),
290 		    BP_SPRINTF_LEN - strlen(blkbuf), bp);
291 	} else {
292 		blkbuf[0] = '\0';
293 	}
294 
295 	if (claim_txg != 0)
296 		claim = "already claimed";
297 	else if (bp->blk_birth >= spa_first_txg(zilog->zl_spa))
298 		claim = "will claim";
299 	else
300 		claim = "won't claim";
301 
302 	(void) printf("\tBlock seqno %llu, %s%s\n",
303 	    (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
304 }
305 
306 static void
307 print_log_stats(int verbose)
308 {
309 	int i, w, p10;
310 
311 	if (verbose > 3)
312 		(void) printf("\n");
313 
314 	if (zil_rec_info[0].zri_count == 0)
315 		return;
316 
317 	for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
318 		w++;
319 
320 	for (i = 0; i < TX_MAX_TYPE; i++)
321 		if (zil_rec_info[i].zri_count || verbose >= 3)
322 			(void) printf("\t\t%s %*llu\n",
323 			    zil_rec_info[i].zri_name, w,
324 			    (u_longlong_t)zil_rec_info[i].zri_count);
325 	(void) printf("\n");
326 }
327 
328 /* ARGSUSED */
329 void
330 dump_intent_log(zilog_t *zilog)
331 {
332 	const zil_header_t *zh = zilog->zl_header;
333 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
334 	int i;
335 
336 	if (zh->zh_log.blk_birth == 0 || verbose < 2)
337 		return;
338 
339 	(void) printf("\n    ZIL header: claim_txg %llu, seq %llu\n",
340 	    (u_longlong_t)zh->zh_claim_txg, (u_longlong_t)zh->zh_replay_seq);
341 
342 	if (verbose >= 4)
343 		print_log_bp(&zh->zh_log, "\n\tfirst block: ");
344 
345 	for (i = 0; i < TX_MAX_TYPE; i++)
346 		zil_rec_info[i].zri_count = 0;
347 
348 	if (verbose >= 2) {
349 		(void) printf("\n");
350 		(void) zil_parse(zilog, print_log_block, print_log_record, NULL,
351 		    zh->zh_claim_txg);
352 		print_log_stats(verbose);
353 	}
354 }
355