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