xref: /freebsd/sys/contrib/openzfs/cmd/zdb/zdb_il.c (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
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 https://opensource.org/licenses/CDDL-1.0.
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  * Copyright (c) 2012 Cyril Plisko. All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
29  */
30 
31 /*
32  * Print intent log header and statistics.
33  */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <sys/zfs_context.h>
39 #include <sys/spa.h>
40 #include <sys/dmu.h>
41 #include <sys/stat.h>
42 #include <sys/resource.h>
43 #include <sys/zil.h>
44 #include <sys/zil_impl.h>
45 #include <sys/spa_impl.h>
46 #include <sys/abd.h>
47 
48 #include "zdb.h"
49 
50 extern uint8_t dump_opt[256];
51 
52 static char tab_prefix[4] = "\t\t\t";
53 
54 static void
55 print_log_bp(const blkptr_t *bp, const char *prefix)
56 {
57 	char blkbuf[BP_SPRINTF_LEN];
58 
59 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
60 	(void) printf("%s%s\n", prefix, blkbuf);
61 }
62 
63 static void
64 zil_prt_rec_create(zilog_t *zilog, int txtype, const void *arg)
65 {
66 	(void) zilog;
67 	const lr_create_t *lrc = arg;
68 	const _lr_create_t *lr = &lrc->lr_create;
69 	time_t crtime = lr->lr_crtime[0];
70 	char *name, *link;
71 	lr_attr_t *lrattr;
72 
73 	name = (char *)(lr + 1);
74 
75 	if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
76 	    lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
77 		lrattr = (lr_attr_t *)(lr + 1);
78 		name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
79 	}
80 
81 	if (txtype == TX_SYMLINK) {
82 		link = name + strlen(name) + 1;
83 		(void) printf("%s%s -> %s\n", tab_prefix, name, link);
84 	} else if (txtype != TX_MKXATTR) {
85 		(void) printf("%s%s\n", tab_prefix, name);
86 	}
87 
88 	(void) printf("%s%s", tab_prefix, ctime(&crtime));
89 	(void) printf("%sdoid %llu, foid %llu, slots %llu, mode %llo\n",
90 	    tab_prefix, (u_longlong_t)lr->lr_doid,
91 	    (u_longlong_t)LR_FOID_GET_OBJ(lr->lr_foid),
92 	    (u_longlong_t)LR_FOID_GET_SLOTS(lr->lr_foid),
93 	    (longlong_t)lr->lr_mode);
94 	(void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n",
95 	    tab_prefix,
96 	    (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
97 	    (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
98 }
99 
100 static void
101 zil_prt_rec_remove(zilog_t *zilog, int txtype, const void *arg)
102 {
103 	(void) zilog, (void) txtype;
104 	const lr_remove_t *lr = arg;
105 
106 	(void) printf("%sdoid %llu, name %s\n", tab_prefix,
107 	    (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
108 }
109 
110 static void
111 zil_prt_rec_link(zilog_t *zilog, int txtype, const void *arg)
112 {
113 	(void) zilog, (void) txtype;
114 	const lr_link_t *lr = arg;
115 
116 	(void) printf("%sdoid %llu, link_obj %llu, name %s\n", tab_prefix,
117 	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
118 	    (char *)(lr + 1));
119 }
120 
121 static void
122 zil_prt_rec_rename(zilog_t *zilog, int txtype, const void *arg)
123 {
124 	(void) zilog, (void) txtype;
125 	const lr_rename_t *lrr = arg;
126 	const _lr_rename_t *lr = &lrr->lr_rename;
127 	char *snm = (char *)(lr + 1);
128 	char *tnm = snm + strlen(snm) + 1;
129 
130 	(void) printf("%ssdoid %llu, tdoid %llu\n", tab_prefix,
131 	    (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
132 	(void) printf("%ssrc %s tgt %s\n", tab_prefix, snm, tnm);
133 	switch (txtype) {
134 	case TX_RENAME_EXCHANGE:
135 		(void) printf("%sflags RENAME_EXCHANGE\n", tab_prefix);
136 		break;
137 	case TX_RENAME_WHITEOUT:
138 		(void) printf("%sflags RENAME_WHITEOUT\n", tab_prefix);
139 		break;
140 	}
141 }
142 
143 static int
144 zil_prt_rec_write_cb(void *data, size_t len, void *unused)
145 {
146 	(void) unused;
147 	char *cdata = data;
148 
149 	for (size_t i = 0; i < len; i++) {
150 		if (isprint(*cdata))
151 			(void) printf("%c ", *cdata);
152 		else
153 			(void) printf("%2X", *cdata);
154 		cdata++;
155 	}
156 	return (0);
157 }
158 
159 static void
160 zil_prt_rec_write(zilog_t *zilog, int txtype, const void *arg)
161 {
162 	const lr_write_t *lr = arg;
163 	abd_t *data;
164 	const blkptr_t *bp = &lr->lr_blkptr;
165 	zbookmark_phys_t zb;
166 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
167 	int error;
168 
169 	(void) printf("%sfoid %llu, offset %llx, length %llx\n", tab_prefix,
170 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
171 	    (u_longlong_t)lr->lr_length);
172 
173 	if (txtype == TX_WRITE2 || verbose < 4)
174 		return;
175 
176 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
177 		(void) printf("%shas blkptr, %s\n", tab_prefix,
178 		    !BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) >=
179 		    spa_min_claim_txg(zilog->zl_spa) ?
180 		    "will claim" : "won't claim");
181 		print_log_bp(bp, tab_prefix);
182 
183 		if (verbose < 5)
184 			return;
185 		if (BP_IS_HOLE(bp)) {
186 			(void) printf("\t\t\tLSIZE 0x%llx\n",
187 			    (u_longlong_t)BP_GET_LSIZE(bp));
188 			(void) printf("%s<hole>\n", tab_prefix);
189 			return;
190 		}
191 		if (BP_GET_LOGICAL_BIRTH(bp) < zilog->zl_header->zh_claim_txg) {
192 			(void) printf("%s<block already committed>\n",
193 			    tab_prefix);
194 			return;
195 		}
196 
197 		ASSERT3U(BP_GET_LSIZE(bp), !=, 0);
198 		SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
199 		    lr->lr_foid, ZB_ZIL_LEVEL,
200 		    lr->lr_offset / BP_GET_LSIZE(bp));
201 
202 		data = abd_alloc(BP_GET_LSIZE(bp), B_FALSE);
203 		error = zio_wait(zio_read(NULL, zilog->zl_spa,
204 		    bp, data, BP_GET_LSIZE(bp), NULL, NULL,
205 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
206 		if (error)
207 			goto out;
208 	} else {
209 		if (verbose < 5)
210 			return;
211 
212 		/* data is stored after the end of the lr_write record */
213 		data = abd_alloc(lr->lr_length, B_FALSE);
214 		abd_copy_from_buf(data, lr + 1, lr->lr_length);
215 	}
216 
217 	(void) printf("%s", tab_prefix);
218 	(void) abd_iterate_func(data,
219 	    0, MIN(lr->lr_length, (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE)),
220 	    zil_prt_rec_write_cb, NULL);
221 	(void) printf("\n");
222 
223 out:
224 	abd_free(data);
225 }
226 
227 static void
228 zil_prt_rec_write_enc(zilog_t *zilog, int txtype, const void *arg)
229 {
230 	(void) txtype;
231 	const lr_write_t *lr = arg;
232 	const blkptr_t *bp = &lr->lr_blkptr;
233 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
234 
235 	(void) printf("%s(encrypted)\n", tab_prefix);
236 
237 	if (verbose < 4)
238 		return;
239 
240 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
241 		(void) printf("%shas blkptr, %s\n", tab_prefix,
242 		    !BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) >=
243 		    spa_min_claim_txg(zilog->zl_spa) ?
244 		    "will claim" : "won't claim");
245 		print_log_bp(bp, tab_prefix);
246 	}
247 }
248 
249 static void
250 zil_prt_rec_truncate(zilog_t *zilog, int txtype, const void *arg)
251 {
252 	(void) zilog, (void) txtype;
253 	const lr_truncate_t *lr = arg;
254 
255 	(void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", tab_prefix,
256 	    (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
257 	    (u_longlong_t)lr->lr_length);
258 }
259 
260 static void
261 zil_prt_rec_setattr(zilog_t *zilog, int txtype, const void *arg)
262 {
263 	(void) zilog, (void) txtype;
264 	const lr_setattr_t *lr = arg;
265 	time_t atime = (time_t)lr->lr_atime[0];
266 	time_t mtime = (time_t)lr->lr_mtime[0];
267 
268 	(void) printf("%sfoid %llu, mask 0x%llx\n", tab_prefix,
269 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
270 
271 	if (lr->lr_mask & AT_MODE) {
272 		(void) printf("%sAT_MODE  %llo\n", tab_prefix,
273 		    (longlong_t)lr->lr_mode);
274 	}
275 
276 	if (lr->lr_mask & AT_UID) {
277 		(void) printf("%sAT_UID   %llu\n", tab_prefix,
278 		    (u_longlong_t)lr->lr_uid);
279 	}
280 
281 	if (lr->lr_mask & AT_GID) {
282 		(void) printf("%sAT_GID   %llu\n", tab_prefix,
283 		    (u_longlong_t)lr->lr_gid);
284 	}
285 
286 	if (lr->lr_mask & AT_SIZE) {
287 		(void) printf("%sAT_SIZE  %llu\n", tab_prefix,
288 		    (u_longlong_t)lr->lr_size);
289 	}
290 
291 	if (lr->lr_mask & AT_ATIME) {
292 		(void) printf("%sAT_ATIME %llu.%09llu %s", tab_prefix,
293 		    (u_longlong_t)lr->lr_atime[0],
294 		    (u_longlong_t)lr->lr_atime[1],
295 		    ctime(&atime));
296 	}
297 
298 	if (lr->lr_mask & AT_MTIME) {
299 		(void) printf("%sAT_MTIME %llu.%09llu %s", tab_prefix,
300 		    (u_longlong_t)lr->lr_mtime[0],
301 		    (u_longlong_t)lr->lr_mtime[1],
302 		    ctime(&mtime));
303 	}
304 }
305 
306 static void
307 zil_prt_rec_setsaxattr(zilog_t *zilog, int txtype, const void *arg)
308 {
309 	(void) zilog, (void) txtype;
310 	const lr_setsaxattr_t *lr = arg;
311 
312 	char *name = (char *)(lr + 1);
313 	(void) printf("%sfoid %llu\n", tab_prefix,
314 	    (u_longlong_t)lr->lr_foid);
315 
316 	(void) printf("%sXAT_NAME  %s\n", tab_prefix, name);
317 	if (lr->lr_size == 0) {
318 		(void) printf("%sXAT_VALUE  NULL\n", tab_prefix);
319 	} else {
320 		(void) printf("%sXAT_VALUE  ", tab_prefix);
321 		char *val = name + (strlen(name) + 1);
322 		for (int i = 0; i < lr->lr_size; i++) {
323 			(void) printf("%c", *val);
324 			val++;
325 		}
326 	}
327 }
328 
329 static void
330 zil_prt_rec_acl(zilog_t *zilog, int txtype, const void *arg)
331 {
332 	(void) zilog, (void) txtype;
333 	const lr_acl_t *lr = arg;
334 
335 	(void) printf("%sfoid %llu, aclcnt %llu\n", tab_prefix,
336 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
337 }
338 
339 static void
340 zil_prt_rec_clone_range(zilog_t *zilog, int txtype, const void *arg)
341 {
342 	(void) zilog, (void) txtype;
343 	const lr_clone_range_t *lr = arg;
344 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
345 
346 	(void) printf("%sfoid %llu, offset %llx, length %llx, blksize %llx\n",
347 	    tab_prefix, (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
348 	    (u_longlong_t)lr->lr_length, (u_longlong_t)lr->lr_blksz);
349 
350 	if (verbose < 4)
351 		return;
352 
353 	for (unsigned int i = 0; i < lr->lr_nbps; i++) {
354 		(void) printf("%s[%u/%llu] ", tab_prefix, i + 1,
355 		    (u_longlong_t)lr->lr_nbps);
356 		print_log_bp(&lr->lr_bps[i], "");
357 	}
358 }
359 
360 static void
361 zil_prt_rec_clone_range_enc(zilog_t *zilog, int txtype, const void *arg)
362 {
363 	(void) zilog, (void) txtype;
364 	const lr_clone_range_t *lr = arg;
365 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
366 
367 	(void) printf("%s(encrypted)\n", tab_prefix);
368 
369 	if (verbose < 4)
370 		return;
371 
372 	for (unsigned int i = 0; i < lr->lr_nbps; i++) {
373 		(void) printf("%s[%u/%llu] ", tab_prefix, i + 1,
374 		    (u_longlong_t)lr->lr_nbps);
375 		print_log_bp(&lr->lr_bps[i], "");
376 	}
377 }
378 
379 typedef void (*zil_prt_rec_func_t)(zilog_t *, int, const void *);
380 typedef struct zil_rec_info {
381 	zil_prt_rec_func_t	zri_print;
382 	zil_prt_rec_func_t	zri_print_enc;
383 	const char		*zri_name;
384 	uint64_t		zri_count;
385 } zil_rec_info_t;
386 
387 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
388 	{.zri_print = NULL,		    .zri_name = "Total              "},
389 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_CREATE          "},
390 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKDIR           "},
391 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKXATTR         "},
392 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_SYMLINK         "},
393 	{.zri_print = zil_prt_rec_remove,   .zri_name = "TX_REMOVE          "},
394 	{.zri_print = zil_prt_rec_remove,   .zri_name = "TX_RMDIR           "},
395 	{.zri_print = zil_prt_rec_link,	    .zri_name = "TX_LINK            "},
396 	{.zri_print = zil_prt_rec_rename,   .zri_name = "TX_RENAME          "},
397 	{.zri_print = zil_prt_rec_write,
398 	    .zri_print_enc = zil_prt_rec_write_enc,
399 	    .zri_name = "TX_WRITE           "},
400 	{.zri_print = zil_prt_rec_truncate, .zri_name = "TX_TRUNCATE        "},
401 	{.zri_print = zil_prt_rec_setattr,  .zri_name = "TX_SETATTR         "},
402 	{.zri_print = zil_prt_rec_acl,	    .zri_name = "TX_ACL_V0          "},
403 	{.zri_print = zil_prt_rec_acl,	    .zri_name = "TX_ACL_ACL         "},
404 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_CREATE_ACL      "},
405 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_CREATE_ATTR     "},
406 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_CREATE_ACL_ATTR "},
407 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKDIR_ACL       "},
408 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKDIR_ATTR      "},
409 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKDIR_ACL_ATTR  "},
410 	{.zri_print = zil_prt_rec_write,    .zri_name = "TX_WRITE2          "},
411 	{.zri_print = zil_prt_rec_setsaxattr,
412 	    .zri_name = "TX_SETSAXATTR      "},
413 	{.zri_print = zil_prt_rec_rename,   .zri_name = "TX_RENAME_EXCHANGE "},
414 	{.zri_print = zil_prt_rec_rename,   .zri_name = "TX_RENAME_WHITEOUT "},
415 	{.zri_print = zil_prt_rec_clone_range,
416 	    .zri_print_enc = zil_prt_rec_clone_range_enc,
417 	    .zri_name = "TX_CLONE_RANGE     "},
418 };
419 
420 static int
421 print_log_record(zilog_t *zilog, const lr_t *lr, void *arg, uint64_t claim_txg)
422 {
423 	(void) arg, (void) claim_txg;
424 	int txtype;
425 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
426 
427 	/* reduce size of txtype to strip off TX_CI bit */
428 	txtype = lr->lrc_txtype;
429 
430 	ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
431 	ASSERT(lr->lrc_txg);
432 
433 	(void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
434 	    (lr->lrc_txtype & TX_CI) ? "CI-" : "",
435 	    zil_rec_info[txtype].zri_name,
436 	    (u_longlong_t)lr->lrc_reclen,
437 	    (u_longlong_t)lr->lrc_txg,
438 	    (u_longlong_t)lr->lrc_seq);
439 
440 	if (txtype && verbose >= 3) {
441 		if (!zilog->zl_os->os_encrypted) {
442 			zil_rec_info[txtype].zri_print(zilog, txtype, lr);
443 		} else if (zil_rec_info[txtype].zri_print_enc) {
444 			zil_rec_info[txtype].zri_print_enc(zilog, txtype, lr);
445 		} else {
446 			(void) printf("%s(encrypted)\n", tab_prefix);
447 		}
448 	}
449 
450 	zil_rec_info[txtype].zri_count++;
451 	zil_rec_info[0].zri_count++;
452 
453 	return (0);
454 }
455 
456 static int
457 print_log_block(zilog_t *zilog, const blkptr_t *bp, void *arg,
458     uint64_t claim_txg)
459 {
460 	(void) arg;
461 	char blkbuf[BP_SPRINTF_LEN + 10];
462 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
463 	const char *claim;
464 
465 	if (verbose <= 3)
466 		return (0);
467 
468 	if (verbose >= 5) {
469 		(void) strcpy(blkbuf, ", ");
470 		snprintf_blkptr(blkbuf + strlen(blkbuf),
471 		    sizeof (blkbuf) - strlen(blkbuf), bp);
472 	} else {
473 		blkbuf[0] = '\0';
474 	}
475 
476 	if (claim_txg != 0)
477 		claim = "already claimed";
478 	else if (BP_GET_LOGICAL_BIRTH(bp) >= spa_min_claim_txg(zilog->zl_spa))
479 		claim = "will claim";
480 	else
481 		claim = "won't claim";
482 
483 	(void) printf("\tBlock seqno %llu, %s%s\n",
484 	    (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
485 
486 	return (0);
487 }
488 
489 static void
490 print_log_stats(int verbose)
491 {
492 	unsigned i, w, p10;
493 
494 	if (verbose > 3)
495 		(void) printf("\n");
496 
497 	if (zil_rec_info[0].zri_count == 0)
498 		return;
499 
500 	for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
501 		w++;
502 
503 	for (i = 0; i < TX_MAX_TYPE; i++)
504 		if (zil_rec_info[i].zri_count || verbose >= 3)
505 			(void) printf("\t\t%s %*llu\n",
506 			    zil_rec_info[i].zri_name, w,
507 			    (u_longlong_t)zil_rec_info[i].zri_count);
508 	(void) printf("\n");
509 }
510 
511 void
512 dump_intent_log(zilog_t *zilog)
513 {
514 	const zil_header_t *zh = zilog->zl_header;
515 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
516 	int i;
517 
518 	if (BP_IS_HOLE(&zh->zh_log) || verbose < 1)
519 		return;
520 
521 	(void) printf("\n    ZIL header: claim_txg %llu, "
522 	    "claim_blk_seq %llu, claim_lr_seq %llu",
523 	    (u_longlong_t)zh->zh_claim_txg,
524 	    (u_longlong_t)zh->zh_claim_blk_seq,
525 	    (u_longlong_t)zh->zh_claim_lr_seq);
526 	(void) printf(" replay_seq %llu, flags 0x%llx\n",
527 	    (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
528 
529 	for (i = 0; i < TX_MAX_TYPE; i++)
530 		zil_rec_info[i].zri_count = 0;
531 
532 	/* see comment in zil_claim() or zil_check_log_chain() */
533 	if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
534 	    zh->zh_claim_txg == 0)
535 		return;
536 
537 	if (verbose >= 2) {
538 		(void) printf("\n");
539 		(void) zil_parse(zilog, print_log_block, print_log_record, NULL,
540 		    zh->zh_claim_txg, B_FALSE);
541 		print_log_stats(verbose);
542 	}
543 }
544