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