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