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