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