zdb_il.c revision 219089
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/*
27168404Spjd * Print intent log header and statistics.
28168404Spjd */
29168404Spjd
30168404Spjd#include <stdio.h>
31168404Spjd#include <stdlib.h>
32168404Spjd#include <ctype.h>
33168404Spjd#include <sys/zfs_context.h>
34168404Spjd#include <sys/spa.h>
35168404Spjd#include <sys/dmu.h>
36168404Spjd#include <sys/stat.h>
37168404Spjd#include <sys/resource.h>
38168404Spjd#include <sys/zil.h>
39168404Spjd#include <sys/zil_impl.h>
40168404Spjd
41168404Spjdextern uint8_t dump_opt[256];
42168404Spjd
43219089Spjdstatic char prefix[4] = "\t\t\t";
44219089Spjd
45168404Spjdstatic void
46168404Spjdprint_log_bp(const blkptr_t *bp, const char *prefix)
47168404Spjd{
48168404Spjd	char blkbuf[BP_SPRINTF_LEN];
49168404Spjd
50219089Spjd	sprintf_blkptr(blkbuf, bp);
51168404Spjd	(void) printf("%s%s\n", prefix, blkbuf);
52168404Spjd}
53168404Spjd
54168404Spjd/* ARGSUSED */
55168404Spjdstatic void
56168404Spjdzil_prt_rec_create(zilog_t *zilog, int txtype, lr_create_t *lr)
57168404Spjd{
58168404Spjd	time_t crtime = lr->lr_crtime[0];
59219089Spjd	char *name, *link;
60219089Spjd	lr_attr_t *lrattr;
61168404Spjd
62219089Spjd	name = (char *)(lr + 1);
63168404Spjd
64219089Spjd	if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
65219089Spjd	    lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
66219089Spjd		lrattr = (lr_attr_t *)(lr + 1);
67219089Spjd		name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
68219089Spjd	}
69219089Spjd
70219089Spjd	if (txtype == TX_SYMLINK) {
71219089Spjd		link = name + strlen(name) + 1;
72219089Spjd		(void) printf("%s%s -> %s\n", prefix, name, link);
73219089Spjd	} else if (txtype != TX_MKXATTR) {
74219089Spjd		(void) printf("%s%s\n", prefix, name);
75219089Spjd	}
76219089Spjd
77219089Spjd	(void) printf("%s%s", prefix, ctime(&crtime));
78219089Spjd	(void) printf("%sdoid %llu, foid %llu, mode %llo\n", prefix,
79168404Spjd	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_foid,
80168404Spjd	    (longlong_t)lr->lr_mode);
81219089Spjd	(void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n", prefix,
82168404Spjd	    (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
83168404Spjd	    (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
84168404Spjd}
85168404Spjd
86168404Spjd/* ARGSUSED */
87168404Spjdstatic void
88168404Spjdzil_prt_rec_remove(zilog_t *zilog, int txtype, lr_remove_t *lr)
89168404Spjd{
90219089Spjd	(void) printf("%sdoid %llu, name %s\n", prefix,
91168404Spjd	    (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
92168404Spjd}
93168404Spjd
94168404Spjd/* ARGSUSED */
95168404Spjdstatic void
96168404Spjdzil_prt_rec_link(zilog_t *zilog, int txtype, lr_link_t *lr)
97168404Spjd{
98219089Spjd	(void) printf("%sdoid %llu, link_obj %llu, name %s\n", prefix,
99168404Spjd	    (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
100168404Spjd	    (char *)(lr + 1));
101168404Spjd}
102168404Spjd
103168404Spjd/* ARGSUSED */
104168404Spjdstatic void
105168404Spjdzil_prt_rec_rename(zilog_t *zilog, int txtype, lr_rename_t *lr)
106168404Spjd{
107168404Spjd	char *snm = (char *)(lr + 1);
108168404Spjd	char *tnm = snm + strlen(snm) + 1;
109168404Spjd
110219089Spjd	(void) printf("%ssdoid %llu, tdoid %llu\n", prefix,
111168404Spjd	    (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
112219089Spjd	(void) printf("%ssrc %s tgt %s\n", prefix, snm, tnm);
113168404Spjd}
114168404Spjd
115168404Spjd/* ARGSUSED */
116168404Spjdstatic void
117168404Spjdzil_prt_rec_write(zilog_t *zilog, int txtype, lr_write_t *lr)
118168404Spjd{
119168404Spjd	char *data, *dlimit;
120168404Spjd	blkptr_t *bp = &lr->lr_blkptr;
121219089Spjd	zbookmark_t zb;
122168404Spjd	char buf[SPA_MAXBLOCKSIZE];
123168404Spjd	int verbose = MAX(dump_opt['d'], dump_opt['i']);
124168404Spjd	int error;
125168404Spjd
126219089Spjd	(void) printf("%sfoid %llu, offset %llx, length %llx\n", prefix,
127219089Spjd	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
128219089Spjd	    (u_longlong_t)lr->lr_length);
129168404Spjd
130209962Smm	if (txtype == TX_WRITE2 || verbose < 5)
131168404Spjd		return;
132168404Spjd
133168404Spjd	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
134219089Spjd		(void) printf("%shas blkptr, %s\n", prefix,
135168404Spjd		    bp->blk_birth >= spa_first_txg(zilog->zl_spa) ?
136168404Spjd		    "will claim" : "won't claim");
137219089Spjd		print_log_bp(bp, prefix);
138219089Spjd
139209962Smm		if (BP_IS_HOLE(bp)) {
140209962Smm			(void) printf("\t\t\tLSIZE 0x%llx\n",
141209962Smm			    (u_longlong_t)BP_GET_LSIZE(bp));
142209962Smm		}
143168404Spjd		if (bp->blk_birth == 0) {
144168404Spjd			bzero(buf, sizeof (buf));
145219089Spjd			(void) printf("%s<hole>\n", prefix);
146219089Spjd			return;
147219089Spjd		}
148219089Spjd		if (bp->blk_birth < zilog->zl_header->zh_claim_txg) {
149219089Spjd			(void) printf("%s<block already committed>\n", prefix);
150219089Spjd			return;
151219089Spjd		}
152168404Spjd
153219089Spjd		SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
154219089Spjd		    lr->lr_foid, ZB_ZIL_LEVEL,
155219089Spjd		    lr->lr_offset / BP_GET_LSIZE(bp));
156168404Spjd
157219089Spjd		error = zio_wait(zio_read(NULL, zilog->zl_spa,
158219089Spjd		    bp, buf, BP_GET_LSIZE(bp), NULL, NULL,
159219089Spjd		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
160219089Spjd		if (error)
161219089Spjd			return;
162219089Spjd		data = buf;
163168404Spjd	} else {
164168404Spjd		data = (char *)(lr + 1);
165168404Spjd	}
166168404Spjd
167168404Spjd	dlimit = data + MIN(lr->lr_length,
168168404Spjd	    (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE));
169168404Spjd
170219089Spjd	(void) printf("%s", prefix);
171168404Spjd	while (data < dlimit) {
172168404Spjd		if (isprint(*data))
173168404Spjd			(void) printf("%c ", *data);
174168404Spjd		else
175168404Spjd			(void) printf("%2X", *data);
176168404Spjd		data++;
177168404Spjd	}
178168404Spjd	(void) printf("\n");
179168404Spjd}
180168404Spjd
181168404Spjd/* ARGSUSED */
182168404Spjdstatic void
183168404Spjdzil_prt_rec_truncate(zilog_t *zilog, int txtype, lr_truncate_t *lr)
184168404Spjd{
185219089Spjd	(void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", prefix,
186168404Spjd	    (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
187168404Spjd	    (u_longlong_t)lr->lr_length);
188168404Spjd}
189168404Spjd
190168404Spjd/* ARGSUSED */
191168404Spjdstatic void
192168404Spjdzil_prt_rec_setattr(zilog_t *zilog, int txtype, lr_setattr_t *lr)
193168404Spjd{
194168404Spjd	time_t atime = (time_t)lr->lr_atime[0];
195168404Spjd	time_t mtime = (time_t)lr->lr_mtime[0];
196168404Spjd
197219089Spjd	(void) printf("%sfoid %llu, mask 0x%llx\n", prefix,
198168404Spjd	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
199168404Spjd
200168404Spjd	if (lr->lr_mask & AT_MODE) {
201219089Spjd		(void) printf("%sAT_MODE  %llo\n", prefix,
202168404Spjd		    (longlong_t)lr->lr_mode);
203168404Spjd	}
204168404Spjd
205168404Spjd	if (lr->lr_mask & AT_UID) {
206219089Spjd		(void) printf("%sAT_UID   %llu\n", prefix,
207168404Spjd		    (u_longlong_t)lr->lr_uid);
208168404Spjd	}
209168404Spjd
210168404Spjd	if (lr->lr_mask & AT_GID) {
211219089Spjd		(void) printf("%sAT_GID   %llu\n", prefix,
212168404Spjd		    (u_longlong_t)lr->lr_gid);
213168404Spjd	}
214168404Spjd
215168404Spjd	if (lr->lr_mask & AT_SIZE) {
216219089Spjd		(void) printf("%sAT_SIZE  %llu\n", prefix,
217168404Spjd		    (u_longlong_t)lr->lr_size);
218168404Spjd	}
219168404Spjd
220168404Spjd	if (lr->lr_mask & AT_ATIME) {
221219089Spjd		(void) printf("%sAT_ATIME %llu.%09llu %s", prefix,
222168404Spjd		    (u_longlong_t)lr->lr_atime[0],
223168404Spjd		    (u_longlong_t)lr->lr_atime[1],
224168404Spjd		    ctime(&atime));
225168404Spjd	}
226168404Spjd
227168404Spjd	if (lr->lr_mask & AT_MTIME) {
228219089Spjd		(void) printf("%sAT_MTIME %llu.%09llu %s", prefix,
229168404Spjd		    (u_longlong_t)lr->lr_mtime[0],
230168404Spjd		    (u_longlong_t)lr->lr_mtime[1],
231168404Spjd		    ctime(&mtime));
232168404Spjd	}
233168404Spjd}
234168404Spjd
235168404Spjd/* ARGSUSED */
236168404Spjdstatic void
237168404Spjdzil_prt_rec_acl(zilog_t *zilog, int txtype, lr_acl_t *lr)
238168404Spjd{
239219089Spjd	(void) printf("%sfoid %llu, aclcnt %llu\n", prefix,
240168404Spjd	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
241168404Spjd}
242168404Spjd
243168404Spjdtypedef void (*zil_prt_rec_func_t)();
244168404Spjdtypedef struct zil_rec_info {
245168404Spjd	zil_prt_rec_func_t	zri_print;
246168404Spjd	char			*zri_name;
247168404Spjd	uint64_t		zri_count;
248168404Spjd} zil_rec_info_t;
249168404Spjd
250168404Spjdstatic zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
251185029Spjd	{	NULL,			"Total              " },
252185029Spjd	{	zil_prt_rec_create,	"TX_CREATE          " },
253185029Spjd	{	zil_prt_rec_create,	"TX_MKDIR           " },
254185029Spjd	{	zil_prt_rec_create,	"TX_MKXATTR         " },
255185029Spjd	{	zil_prt_rec_create,	"TX_SYMLINK         " },
256185029Spjd	{	zil_prt_rec_remove,	"TX_REMOVE          " },
257185029Spjd	{	zil_prt_rec_remove,	"TX_RMDIR           " },
258185029Spjd	{	zil_prt_rec_link,	"TX_LINK            " },
259185029Spjd	{	zil_prt_rec_rename,	"TX_RENAME          " },
260185029Spjd	{	zil_prt_rec_write,	"TX_WRITE           " },
261185029Spjd	{	zil_prt_rec_truncate,	"TX_TRUNCATE        " },
262185029Spjd	{	zil_prt_rec_setattr,	"TX_SETATTR         " },
263185029Spjd	{	zil_prt_rec_acl,	"TX_ACL_V0          " },
264185029Spjd	{	zil_prt_rec_acl,	"TX_ACL_ACL         " },
265185029Spjd	{	zil_prt_rec_create,	"TX_CREATE_ACL      " },
266185029Spjd	{	zil_prt_rec_create,	"TX_CREATE_ATTR     " },
267185029Spjd	{	zil_prt_rec_create,	"TX_CREATE_ACL_ATTR " },
268185029Spjd	{	zil_prt_rec_create,	"TX_MKDIR_ACL       " },
269185029Spjd	{	zil_prt_rec_create,	"TX_MKDIR_ATTR      " },
270185029Spjd	{	zil_prt_rec_create,	"TX_MKDIR_ACL_ATTR  " },
271209962Smm	{	zil_prt_rec_write,	"TX_WRITE2          " },
272168404Spjd};
273168404Spjd
274168404Spjd/* ARGSUSED */
275219089Spjdstatic int
276168404Spjdprint_log_record(zilog_t *zilog, lr_t *lr, void *arg, uint64_t claim_txg)
277168404Spjd{
278168404Spjd	int txtype;
279168404Spjd	int verbose = MAX(dump_opt['d'], dump_opt['i']);
280168404Spjd
281185029Spjd	/* reduce size of txtype to strip off TX_CI bit */
282168404Spjd	txtype = lr->lrc_txtype;
283168404Spjd
284168404Spjd	ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
285168404Spjd	ASSERT(lr->lrc_txg);
286168404Spjd
287185029Spjd	(void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
288185029Spjd	    (lr->lrc_txtype & TX_CI) ? "CI-" : "",
289168404Spjd	    zil_rec_info[txtype].zri_name,
290168404Spjd	    (u_longlong_t)lr->lrc_reclen,
291168404Spjd	    (u_longlong_t)lr->lrc_txg,
292168404Spjd	    (u_longlong_t)lr->lrc_seq);
293168404Spjd
294168404Spjd	if (txtype && verbose >= 3)
295168404Spjd		zil_rec_info[txtype].zri_print(zilog, txtype, lr);
296168404Spjd
297168404Spjd	zil_rec_info[txtype].zri_count++;
298168404Spjd	zil_rec_info[0].zri_count++;
299219089Spjd
300219089Spjd	return (0);
301168404Spjd}
302168404Spjd
303168404Spjd/* ARGSUSED */
304219089Spjdstatic int
305168404Spjdprint_log_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
306168404Spjd{
307219089Spjd	char blkbuf[BP_SPRINTF_LEN + 10];
308168404Spjd	int verbose = MAX(dump_opt['d'], dump_opt['i']);
309168404Spjd	char *claim;
310168404Spjd
311168404Spjd	if (verbose <= 3)
312219089Spjd		return (0);
313168404Spjd
314168404Spjd	if (verbose >= 5) {
315168404Spjd		(void) strcpy(blkbuf, ", ");
316219089Spjd		sprintf_blkptr(blkbuf + strlen(blkbuf), bp);
317168404Spjd	} else {
318168404Spjd		blkbuf[0] = '\0';
319168404Spjd	}
320168404Spjd
321168404Spjd	if (claim_txg != 0)
322168404Spjd		claim = "already claimed";
323168404Spjd	else if (bp->blk_birth >= spa_first_txg(zilog->zl_spa))
324168404Spjd		claim = "will claim";
325168404Spjd	else
326168404Spjd		claim = "won't claim";
327168404Spjd
328168404Spjd	(void) printf("\tBlock seqno %llu, %s%s\n",
329168404Spjd	    (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
330219089Spjd
331219089Spjd	return (0);
332168404Spjd}
333168404Spjd
334168404Spjdstatic void
335168404Spjdprint_log_stats(int verbose)
336168404Spjd{
337168404Spjd	int i, w, p10;
338168404Spjd
339168404Spjd	if (verbose > 3)
340168404Spjd		(void) printf("\n");
341168404Spjd
342168404Spjd	if (zil_rec_info[0].zri_count == 0)
343168404Spjd		return;
344168404Spjd
345168404Spjd	for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
346168404Spjd		w++;
347168404Spjd
348168404Spjd	for (i = 0; i < TX_MAX_TYPE; i++)
349168404Spjd		if (zil_rec_info[i].zri_count || verbose >= 3)
350168404Spjd			(void) printf("\t\t%s %*llu\n",
351168404Spjd			    zil_rec_info[i].zri_name, w,
352168404Spjd			    (u_longlong_t)zil_rec_info[i].zri_count);
353168404Spjd	(void) printf("\n");
354168404Spjd}
355168404Spjd
356168404Spjd/* ARGSUSED */
357168404Spjdvoid
358168404Spjddump_intent_log(zilog_t *zilog)
359168404Spjd{
360168404Spjd	const zil_header_t *zh = zilog->zl_header;
361168404Spjd	int verbose = MAX(dump_opt['d'], dump_opt['i']);
362168404Spjd	int i;
363168404Spjd
364219089Spjd	if (zh->zh_log.blk_birth == 0 || verbose < 1)
365168404Spjd		return;
366168404Spjd
367219089Spjd	(void) printf("\n    ZIL header: claim_txg %llu, "
368219089Spjd	    "claim_blk_seq %llu, claim_lr_seq %llu",
369219089Spjd	    (u_longlong_t)zh->zh_claim_txg,
370219089Spjd	    (u_longlong_t)zh->zh_claim_blk_seq,
371219089Spjd	    (u_longlong_t)zh->zh_claim_lr_seq);
372200724Sdelphij	(void) printf(" replay_seq %llu, flags 0x%llx\n",
373200724Sdelphij	    (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
374168404Spjd
375168404Spjd	for (i = 0; i < TX_MAX_TYPE; i++)
376168404Spjd		zil_rec_info[i].zri_count = 0;
377168404Spjd
378168404Spjd	if (verbose >= 2) {
379168404Spjd		(void) printf("\n");
380168404Spjd		(void) zil_parse(zilog, print_log_block, print_log_record, NULL,
381168404Spjd		    zh->zh_claim_txg);
382168404Spjd		print_log_stats(verbose);
383168404Spjd	}
384168404Spjd}
385