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