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