1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22200724Sdelphij * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23168404Spjd * Use is subject to license terms.
24168404Spjd */
25168404Spjd
26168404Spjd/*
27268657Sdelphij * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
28263397Sdelphij */
29263397Sdelphij
30263397Sdelphij/*
31168404Spjd * Print intent log header and statistics.
32168404Spjd */
33168404Spjd
34168404Spjd#include <stdio.h>
35168404Spjd#include <stdlib.h>
36168404Spjd#include <ctype.h>
37168404Spjd#include <sys/zfs_context.h>
38168404Spjd#include <sys/spa.h>
39168404Spjd#include <sys/dmu.h>
40168404Spjd#include <sys/stat.h>
41168404Spjd#include <sys/resource.h>
42168404Spjd#include <sys/zil.h>
43168404Spjd#include <sys/zil_impl.h>
44168404Spjd
45168404Spjdextern uint8_t dump_opt[256];
46168404Spjd
47219089Spjdstatic char prefix[4] = "\t\t\t";
48219089Spjd
49168404Spjdstatic void
50168404Spjdprint_log_bp(const blkptr_t *bp, const char *prefix)
51168404Spjd{
52168404Spjd	char blkbuf[BP_SPRINTF_LEN];
53168404Spjd
54263397Sdelphij	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
55168404Spjd	(void) printf("%s%s\n", prefix, blkbuf);
56168404Spjd}
57168404Spjd
58168404Spjd/* ARGSUSED */
59168404Spjdstatic void
60168404Spjdzil_prt_rec_create(zilog_t *zilog, int txtype, lr_create_t *lr)
61168404Spjd{
62168404Spjd	time_t crtime = lr->lr_crtime[0];
63219089Spjd	char *name, *link;
64219089Spjd	lr_attr_t *lrattr;
65168404Spjd
66219089Spjd	name = (char *)(lr + 1);
67168404Spjd
68219089Spjd	if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
69219089Spjd	    lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
70219089Spjd		lrattr = (lr_attr_t *)(lr + 1);
71219089Spjd		name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
72219089Spjd	}
73219089Spjd
74219089Spjd	if (txtype == TX_SYMLINK) {
75219089Spjd		link = name + strlen(name) + 1;
76219089Spjd		(void) printf("%s%s -> %s\n", prefix, name, link);
77219089Spjd	} else if (txtype != TX_MKXATTR) {
78219089Spjd		(void) printf("%s%s\n", prefix, name);
79219089Spjd	}
80219089Spjd
81219089Spjd	(void) printf("%s%s", prefix, ctime(&crtime));
82219089Spjd	(void) printf("%sdoid %llu, foid %llu, mode %llo\n", prefix,
83168404Spjd	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_foid,
84168404Spjd	    (longlong_t)lr->lr_mode);
85219089Spjd	(void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n", prefix,
86168404Spjd	    (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
87168404Spjd	    (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
88168404Spjd}
89168404Spjd
90168404Spjd/* ARGSUSED */
91168404Spjdstatic void
92168404Spjdzil_prt_rec_remove(zilog_t *zilog, int txtype, lr_remove_t *lr)
93168404Spjd{
94219089Spjd	(void) printf("%sdoid %llu, name %s\n", prefix,
95168404Spjd	    (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
96168404Spjd}
97168404Spjd
98168404Spjd/* ARGSUSED */
99168404Spjdstatic void
100168404Spjdzil_prt_rec_link(zilog_t *zilog, int txtype, lr_link_t *lr)
101168404Spjd{
102219089Spjd	(void) printf("%sdoid %llu, link_obj %llu, name %s\n", prefix,
103168404Spjd	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
104168404Spjd	    (char *)(lr + 1));
105168404Spjd}
106168404Spjd
107168404Spjd/* ARGSUSED */
108168404Spjdstatic void
109168404Spjdzil_prt_rec_rename(zilog_t *zilog, int txtype, lr_rename_t *lr)
110168404Spjd{
111168404Spjd	char *snm = (char *)(lr + 1);
112168404Spjd	char *tnm = snm + strlen(snm) + 1;
113168404Spjd
114219089Spjd	(void) printf("%ssdoid %llu, tdoid %llu\n", prefix,
115168404Spjd	    (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
116219089Spjd	(void) printf("%ssrc %s tgt %s\n", prefix, snm, tnm);
117168404Spjd}
118168404Spjd
119168404Spjd/* ARGSUSED */
120168404Spjdstatic void
121168404Spjdzil_prt_rec_write(zilog_t *zilog, int txtype, lr_write_t *lr)
122168404Spjd{
123168404Spjd	char *data, *dlimit;
124168404Spjd	blkptr_t *bp = &lr->lr_blkptr;
125268657Sdelphij	zbookmark_phys_t zb;
126168404Spjd	char buf[SPA_MAXBLOCKSIZE];
127168404Spjd	int verbose = MAX(dump_opt['d'], dump_opt['i']);
128168404Spjd	int error;
129168404Spjd
130219089Spjd	(void) printf("%sfoid %llu, offset %llx, length %llx\n", prefix,
131219089Spjd	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
132219089Spjd	    (u_longlong_t)lr->lr_length);
133168404Spjd
134209962Smm	if (txtype == TX_WRITE2 || verbose < 5)
135168404Spjd		return;
136168404Spjd
137168404Spjd	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
138219089Spjd		(void) printf("%shas blkptr, %s\n", prefix,
139263397Sdelphij		    !BP_IS_HOLE(bp) &&
140168404Spjd		    bp->blk_birth >= spa_first_txg(zilog->zl_spa) ?
141168404Spjd		    "will claim" : "won't claim");
142219089Spjd		print_log_bp(bp, prefix);
143219089Spjd
144209962Smm		if (BP_IS_HOLE(bp)) {
145209962Smm			(void) printf("\t\t\tLSIZE 0x%llx\n",
146209962Smm			    (u_longlong_t)BP_GET_LSIZE(bp));
147168404Spjd			bzero(buf, sizeof (buf));
148219089Spjd			(void) printf("%s<hole>\n", prefix);
149219089Spjd			return;
150219089Spjd		}
151219089Spjd		if (bp->blk_birth < zilog->zl_header->zh_claim_txg) {
152219089Spjd			(void) printf("%s<block already committed>\n", prefix);
153219089Spjd			return;
154219089Spjd		}
155168404Spjd
156219089Spjd		SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
157219089Spjd		    lr->lr_foid, ZB_ZIL_LEVEL,
158219089Spjd		    lr->lr_offset / BP_GET_LSIZE(bp));
159168404Spjd
160219089Spjd		error = zio_wait(zio_read(NULL, zilog->zl_spa,
161219089Spjd		    bp, buf, BP_GET_LSIZE(bp), NULL, NULL,
162219089Spjd		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
163219089Spjd		if (error)
164219089Spjd			return;
165219089Spjd		data = buf;
166168404Spjd	} else {
167168404Spjd		data = (char *)(lr + 1);
168168404Spjd	}
169168404Spjd
170168404Spjd	dlimit = data + MIN(lr->lr_length,
171168404Spjd	    (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE));
172168404Spjd
173219089Spjd	(void) printf("%s", prefix);
174168404Spjd	while (data < dlimit) {
175168404Spjd		if (isprint(*data))
176168404Spjd			(void) printf("%c ", *data);
177168404Spjd		else
178168404Spjd			(void) printf("%2X", *data);
179168404Spjd		data++;
180168404Spjd	}
181168404Spjd	(void) printf("\n");
182168404Spjd}
183168404Spjd
184168404Spjd/* ARGSUSED */
185168404Spjdstatic void
186168404Spjdzil_prt_rec_truncate(zilog_t *zilog, int txtype, lr_truncate_t *lr)
187168404Spjd{
188219089Spjd	(void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", prefix,
189168404Spjd	    (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
190168404Spjd	    (u_longlong_t)lr->lr_length);
191168404Spjd}
192168404Spjd
193168404Spjd/* ARGSUSED */
194168404Spjdstatic void
195168404Spjdzil_prt_rec_setattr(zilog_t *zilog, int txtype, lr_setattr_t *lr)
196168404Spjd{
197168404Spjd	time_t atime = (time_t)lr->lr_atime[0];
198168404Spjd	time_t mtime = (time_t)lr->lr_mtime[0];
199168404Spjd
200219089Spjd	(void) printf("%sfoid %llu, mask 0x%llx\n", prefix,
201168404Spjd	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
202168404Spjd
203168404Spjd	if (lr->lr_mask & AT_MODE) {
204219089Spjd		(void) printf("%sAT_MODE  %llo\n", prefix,
205168404Spjd		    (longlong_t)lr->lr_mode);
206168404Spjd	}
207168404Spjd
208168404Spjd	if (lr->lr_mask & AT_UID) {
209219089Spjd		(void) printf("%sAT_UID   %llu\n", prefix,
210168404Spjd		    (u_longlong_t)lr->lr_uid);
211168404Spjd	}
212168404Spjd
213168404Spjd	if (lr->lr_mask & AT_GID) {
214219089Spjd		(void) printf("%sAT_GID   %llu\n", prefix,
215168404Spjd		    (u_longlong_t)lr->lr_gid);
216168404Spjd	}
217168404Spjd
218168404Spjd	if (lr->lr_mask & AT_SIZE) {
219219089Spjd		(void) printf("%sAT_SIZE  %llu\n", prefix,
220168404Spjd		    (u_longlong_t)lr->lr_size);
221168404Spjd	}
222168404Spjd
223168404Spjd	if (lr->lr_mask & AT_ATIME) {
224219089Spjd		(void) printf("%sAT_ATIME %llu.%09llu %s", prefix,
225168404Spjd		    (u_longlong_t)lr->lr_atime[0],
226168404Spjd		    (u_longlong_t)lr->lr_atime[1],
227168404Spjd		    ctime(&atime));
228168404Spjd	}
229168404Spjd
230168404Spjd	if (lr->lr_mask & AT_MTIME) {
231219089Spjd		(void) printf("%sAT_MTIME %llu.%09llu %s", prefix,
232168404Spjd		    (u_longlong_t)lr->lr_mtime[0],
233168404Spjd		    (u_longlong_t)lr->lr_mtime[1],
234168404Spjd		    ctime(&mtime));
235168404Spjd	}
236168404Spjd}
237168404Spjd
238168404Spjd/* ARGSUSED */
239168404Spjdstatic void
240168404Spjdzil_prt_rec_acl(zilog_t *zilog, int txtype, lr_acl_t *lr)
241168404Spjd{
242219089Spjd	(void) printf("%sfoid %llu, aclcnt %llu\n", prefix,
243168404Spjd	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
244168404Spjd}
245168404Spjd
246168404Spjdtypedef void (*zil_prt_rec_func_t)();
247168404Spjdtypedef struct zil_rec_info {
248168404Spjd	zil_prt_rec_func_t	zri_print;
249168404Spjd	char			*zri_name;
250168404Spjd	uint64_t		zri_count;
251168404Spjd} zil_rec_info_t;
252168404Spjd
253168404Spjdstatic zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
254185029Spjd	{	NULL,			"Total              " },
255185029Spjd	{	zil_prt_rec_create,	"TX_CREATE          " },
256185029Spjd	{	zil_prt_rec_create,	"TX_MKDIR           " },
257185029Spjd	{	zil_prt_rec_create,	"TX_MKXATTR         " },
258185029Spjd	{	zil_prt_rec_create,	"TX_SYMLINK         " },
259185029Spjd	{	zil_prt_rec_remove,	"TX_REMOVE          " },
260185029Spjd	{	zil_prt_rec_remove,	"TX_RMDIR           " },
261185029Spjd	{	zil_prt_rec_link,	"TX_LINK            " },
262185029Spjd	{	zil_prt_rec_rename,	"TX_RENAME          " },
263185029Spjd	{	zil_prt_rec_write,	"TX_WRITE           " },
264185029Spjd	{	zil_prt_rec_truncate,	"TX_TRUNCATE        " },
265185029Spjd	{	zil_prt_rec_setattr,	"TX_SETATTR         " },
266185029Spjd	{	zil_prt_rec_acl,	"TX_ACL_V0          " },
267185029Spjd	{	zil_prt_rec_acl,	"TX_ACL_ACL         " },
268185029Spjd	{	zil_prt_rec_create,	"TX_CREATE_ACL      " },
269185029Spjd	{	zil_prt_rec_create,	"TX_CREATE_ATTR     " },
270185029Spjd	{	zil_prt_rec_create,	"TX_CREATE_ACL_ATTR " },
271185029Spjd	{	zil_prt_rec_create,	"TX_MKDIR_ACL       " },
272185029Spjd	{	zil_prt_rec_create,	"TX_MKDIR_ATTR      " },
273185029Spjd	{	zil_prt_rec_create,	"TX_MKDIR_ACL_ATTR  " },
274209962Smm	{	zil_prt_rec_write,	"TX_WRITE2          " },
275168404Spjd};
276168404Spjd
277168404Spjd/* ARGSUSED */
278219089Spjdstatic int
279168404Spjdprint_log_record(zilog_t *zilog, lr_t *lr, void *arg, uint64_t claim_txg)
280168404Spjd{
281168404Spjd	int txtype;
282168404Spjd	int verbose = MAX(dump_opt['d'], dump_opt['i']);
283168404Spjd
284185029Spjd	/* reduce size of txtype to strip off TX_CI bit */
285168404Spjd	txtype = lr->lrc_txtype;
286168404Spjd
287168404Spjd	ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
288168404Spjd	ASSERT(lr->lrc_txg);
289168404Spjd
290185029Spjd	(void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
291185029Spjd	    (lr->lrc_txtype & TX_CI) ? "CI-" : "",
292168404Spjd	    zil_rec_info[txtype].zri_name,
293168404Spjd	    (u_longlong_t)lr->lrc_reclen,
294168404Spjd	    (u_longlong_t)lr->lrc_txg,
295168404Spjd	    (u_longlong_t)lr->lrc_seq);
296168404Spjd
297168404Spjd	if (txtype && verbose >= 3)
298168404Spjd		zil_rec_info[txtype].zri_print(zilog, txtype, lr);
299168404Spjd
300168404Spjd	zil_rec_info[txtype].zri_count++;
301168404Spjd	zil_rec_info[0].zri_count++;
302219089Spjd
303219089Spjd	return (0);
304168404Spjd}
305168404Spjd
306168404Spjd/* ARGSUSED */
307219089Spjdstatic int
308168404Spjdprint_log_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
309168404Spjd{
310219089Spjd	char blkbuf[BP_SPRINTF_LEN + 10];
311168404Spjd	int verbose = MAX(dump_opt['d'], dump_opt['i']);
312168404Spjd	char *claim;
313168404Spjd
314168404Spjd	if (verbose <= 3)
315219089Spjd		return (0);
316168404Spjd
317168404Spjd	if (verbose >= 5) {
318168404Spjd		(void) strcpy(blkbuf, ", ");
319263397Sdelphij		snprintf_blkptr(blkbuf + strlen(blkbuf),
320263397Sdelphij		    sizeof (blkbuf) - strlen(blkbuf), bp);
321168404Spjd	} else {
322168404Spjd		blkbuf[0] = '\0';
323168404Spjd	}
324168404Spjd
325168404Spjd	if (claim_txg != 0)
326168404Spjd		claim = "already claimed";
327168404Spjd	else if (bp->blk_birth >= spa_first_txg(zilog->zl_spa))
328168404Spjd		claim = "will claim";
329168404Spjd	else
330168404Spjd		claim = "won't claim";
331168404Spjd
332168404Spjd	(void) printf("\tBlock seqno %llu, %s%s\n",
333168404Spjd	    (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
334219089Spjd
335219089Spjd	return (0);
336168404Spjd}
337168404Spjd
338168404Spjdstatic void
339168404Spjdprint_log_stats(int verbose)
340168404Spjd{
341168404Spjd	int i, w, p10;
342168404Spjd
343168404Spjd	if (verbose > 3)
344168404Spjd		(void) printf("\n");
345168404Spjd
346168404Spjd	if (zil_rec_info[0].zri_count == 0)
347168404Spjd		return;
348168404Spjd
349168404Spjd	for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
350168404Spjd		w++;
351168404Spjd
352168404Spjd	for (i = 0; i < TX_MAX_TYPE; i++)
353168404Spjd		if (zil_rec_info[i].zri_count || verbose >= 3)
354168404Spjd			(void) printf("\t\t%s %*llu\n",
355168404Spjd			    zil_rec_info[i].zri_name, w,
356168404Spjd			    (u_longlong_t)zil_rec_info[i].zri_count);
357168404Spjd	(void) printf("\n");
358168404Spjd}
359168404Spjd
360168404Spjd/* ARGSUSED */
361168404Spjdvoid
362168404Spjddump_intent_log(zilog_t *zilog)
363168404Spjd{
364168404Spjd	const zil_header_t *zh = zilog->zl_header;
365168404Spjd	int verbose = MAX(dump_opt['d'], dump_opt['i']);
366168404Spjd	int i;
367168404Spjd
368263397Sdelphij	if (BP_IS_HOLE(&zh->zh_log) || verbose < 1)
369168404Spjd		return;
370168404Spjd
371219089Spjd	(void) printf("\n    ZIL header: claim_txg %llu, "
372219089Spjd	    "claim_blk_seq %llu, claim_lr_seq %llu",
373219089Spjd	    (u_longlong_t)zh->zh_claim_txg,
374219089Spjd	    (u_longlong_t)zh->zh_claim_blk_seq,
375219089Spjd	    (u_longlong_t)zh->zh_claim_lr_seq);
376200724Sdelphij	(void) printf(" replay_seq %llu, flags 0x%llx\n",
377200724Sdelphij	    (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
378168404Spjd
379168404Spjd	for (i = 0; i < TX_MAX_TYPE; i++)
380168404Spjd		zil_rec_info[i].zri_count = 0;
381168404Spjd
382168404Spjd	if (verbose >= 2) {
383168404Spjd		(void) printf("\n");
384168404Spjd		(void) zil_parse(zilog, print_log_block, print_log_record, NULL,
385168404Spjd		    zh->zh_claim_txg);
386168404Spjd		print_log_stats(verbose);
387168404Spjd	}
388168404Spjd}
389