1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
51544Seschrock * Common Development and Distribution License (the "License").
61544Seschrock * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
21789Sahrens /*
228989SNeil.Perrin@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23789Sahrens * Use is subject to license terms.
24789Sahrens */
25789Sahrens
26789Sahrens /*
27789Sahrens * Print intent log header and statistics.
28789Sahrens */
29789Sahrens
30789Sahrens #include <stdio.h>
31789Sahrens #include <stdlib.h>
32789Sahrens #include <ctype.h>
33789Sahrens #include <sys/zfs_context.h>
34789Sahrens #include <sys/spa.h>
35789Sahrens #include <sys/dmu.h>
36789Sahrens #include <sys/stat.h>
37789Sahrens #include <sys/resource.h>
38789Sahrens #include <sys/zil.h>
39789Sahrens #include <sys/zil_impl.h>
40789Sahrens
41789Sahrens extern uint8_t dump_opt[256];
42789Sahrens
4310922SJeff.Bonwick@Sun.COM static char prefix[4] = "\t\t\t";
4410922SJeff.Bonwick@Sun.COM
45789Sahrens static void
print_log_bp(const blkptr_t * bp,const char * prefix)461807Sbonwick print_log_bp(const blkptr_t *bp, const char *prefix)
47789Sahrens {
48896Smaybee char blkbuf[BP_SPRINTF_LEN];
49789Sahrens
5010922SJeff.Bonwick@Sun.COM sprintf_blkptr(blkbuf, bp);
51789Sahrens (void) printf("%s%s\n", prefix, blkbuf);
52789Sahrens }
53789Sahrens
54789Sahrens /* ARGSUSED */
55789Sahrens static void
zil_prt_rec_create(zilog_t * zilog,int txtype,lr_create_t * lr)56789Sahrens zil_prt_rec_create(zilog_t *zilog, int txtype, lr_create_t *lr)
57789Sahrens {
58789Sahrens time_t crtime = lr->lr_crtime[0];
59*10960SEric.Schrock@Sun.COM char *name, *link;
60*10960SEric.Schrock@Sun.COM lr_attr_t *lrattr;
61*10960SEric.Schrock@Sun.COM
62*10960SEric.Schrock@Sun.COM name = (char *)(lr + 1);
63789Sahrens
64*10960SEric.Schrock@Sun.COM if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
65*10960SEric.Schrock@Sun.COM lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
66*10960SEric.Schrock@Sun.COM lrattr = (lr_attr_t *)(lr + 1);
67*10960SEric.Schrock@Sun.COM name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
68*10960SEric.Schrock@Sun.COM }
69*10960SEric.Schrock@Sun.COM
70*10960SEric.Schrock@Sun.COM if (txtype == TX_SYMLINK) {
71*10960SEric.Schrock@Sun.COM link = name + strlen(name) + 1;
7210922SJeff.Bonwick@Sun.COM (void) printf("%s%s -> %s\n", prefix, name, link);
73*10960SEric.Schrock@Sun.COM } else if (txtype != TX_MKXATTR) {
7410922SJeff.Bonwick@Sun.COM (void) printf("%s%s\n", prefix, name);
75*10960SEric.Schrock@Sun.COM }
76789Sahrens
7710922SJeff.Bonwick@Sun.COM (void) printf("%s%s", prefix, ctime(&crtime));
7810922SJeff.Bonwick@Sun.COM (void) printf("%sdoid %llu, foid %llu, mode %llo\n", prefix,
79789Sahrens (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_foid,
80789Sahrens (longlong_t)lr->lr_mode);
8110922SJeff.Bonwick@Sun.COM (void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n", prefix,
82789Sahrens (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
83789Sahrens (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
84789Sahrens }
85789Sahrens
86789Sahrens /* ARGSUSED */
87789Sahrens static void
zil_prt_rec_remove(zilog_t * zilog,int txtype,lr_remove_t * lr)88789Sahrens zil_prt_rec_remove(zilog_t *zilog, int txtype, lr_remove_t *lr)
89789Sahrens {
9010922SJeff.Bonwick@Sun.COM (void) printf("%sdoid %llu, name %s\n", prefix,
91789Sahrens (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
92789Sahrens }
93789Sahrens
94789Sahrens /* ARGSUSED */
95789Sahrens static void
zil_prt_rec_link(zilog_t * zilog,int txtype,lr_link_t * lr)96789Sahrens zil_prt_rec_link(zilog_t *zilog, int txtype, lr_link_t *lr)
97789Sahrens {
9810922SJeff.Bonwick@Sun.COM (void) printf("%sdoid %llu, link_obj %llu, name %s\n", prefix,
99789Sahrens (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
100789Sahrens (char *)(lr + 1));
101789Sahrens }
102789Sahrens
103789Sahrens /* ARGSUSED */
104789Sahrens static void
zil_prt_rec_rename(zilog_t * zilog,int txtype,lr_rename_t * lr)105789Sahrens zil_prt_rec_rename(zilog_t *zilog, int txtype, lr_rename_t *lr)
106789Sahrens {
107789Sahrens char *snm = (char *)(lr + 1);
108789Sahrens char *tnm = snm + strlen(snm) + 1;
109789Sahrens
11010922SJeff.Bonwick@Sun.COM (void) printf("%ssdoid %llu, tdoid %llu\n", prefix,
111789Sahrens (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
11210922SJeff.Bonwick@Sun.COM (void) printf("%ssrc %s tgt %s\n", prefix, snm, tnm);
113789Sahrens }
114789Sahrens
115789Sahrens /* ARGSUSED */
116789Sahrens static void
zil_prt_rec_write(zilog_t * zilog,int txtype,lr_write_t * lr)117789Sahrens zil_prt_rec_write(zilog_t *zilog, int txtype, lr_write_t *lr)
118789Sahrens {
119789Sahrens char *data, *dlimit;
120789Sahrens blkptr_t *bp = &lr->lr_blkptr;
12110922SJeff.Bonwick@Sun.COM zbookmark_t zb;
122789Sahrens char buf[SPA_MAXBLOCKSIZE];
123789Sahrens int verbose = MAX(dump_opt['d'], dump_opt['i']);
124789Sahrens int error;
125789Sahrens
12610922SJeff.Bonwick@Sun.COM (void) printf("%sfoid %llu, offset %llx, length %llx\n", prefix,
12710922SJeff.Bonwick@Sun.COM (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
12810922SJeff.Bonwick@Sun.COM (u_longlong_t)lr->lr_length);
129789Sahrens
13010800SNeil.Perrin@Sun.COM if (txtype == TX_WRITE2 || verbose < 5)
131789Sahrens return;
132789Sahrens
133789Sahrens if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
13410922SJeff.Bonwick@Sun.COM (void) printf("%shas blkptr, %s\n", prefix,
135789Sahrens bp->blk_birth >= spa_first_txg(zilog->zl_spa) ?
136789Sahrens "will claim" : "won't claim");
13710922SJeff.Bonwick@Sun.COM print_log_bp(bp, prefix);
13810922SJeff.Bonwick@Sun.COM
13910800SNeil.Perrin@Sun.COM if (BP_IS_HOLE(bp)) {
14010800SNeil.Perrin@Sun.COM (void) printf("\t\t\tLSIZE 0x%llx\n",
14110800SNeil.Perrin@Sun.COM (u_longlong_t)BP_GET_LSIZE(bp));
14210800SNeil.Perrin@Sun.COM }
143789Sahrens if (bp->blk_birth == 0) {
144789Sahrens bzero(buf, sizeof (buf));
14510922SJeff.Bonwick@Sun.COM (void) printf("%s<hole>\n", prefix);
14610922SJeff.Bonwick@Sun.COM return;
14710922SJeff.Bonwick@Sun.COM }
14810922SJeff.Bonwick@Sun.COM if (bp->blk_birth < zilog->zl_header->zh_claim_txg) {
14910922SJeff.Bonwick@Sun.COM (void) printf("%s<block already committed>\n", prefix);
15010922SJeff.Bonwick@Sun.COM return;
15110922SJeff.Bonwick@Sun.COM }
1521544Seschrock
15310922SJeff.Bonwick@Sun.COM SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
15410922SJeff.Bonwick@Sun.COM lr->lr_foid, ZB_ZIL_LEVEL,
15510922SJeff.Bonwick@Sun.COM lr->lr_offset / BP_GET_LSIZE(bp));
15610922SJeff.Bonwick@Sun.COM
15710922SJeff.Bonwick@Sun.COM error = zio_wait(zio_read(NULL, zilog->zl_spa,
15810922SJeff.Bonwick@Sun.COM bp, buf, BP_GET_LSIZE(bp), NULL, NULL,
15910922SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
16010922SJeff.Bonwick@Sun.COM if (error)
16110922SJeff.Bonwick@Sun.COM return;
16210922SJeff.Bonwick@Sun.COM data = buf;
163789Sahrens } else {
164789Sahrens data = (char *)(lr + 1);
165789Sahrens }
166789Sahrens
167789Sahrens dlimit = data + MIN(lr->lr_length,
168789Sahrens (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE));
169789Sahrens
17010922SJeff.Bonwick@Sun.COM (void) printf("%s", prefix);
171789Sahrens while (data < dlimit) {
172789Sahrens if (isprint(*data))
173789Sahrens (void) printf("%c ", *data);
174789Sahrens else
175789Sahrens (void) printf("%2X", *data);
176789Sahrens data++;
177789Sahrens }
178789Sahrens (void) printf("\n");
179789Sahrens }
180789Sahrens
181789Sahrens /* ARGSUSED */
182789Sahrens static void
zil_prt_rec_truncate(zilog_t * zilog,int txtype,lr_truncate_t * lr)183789Sahrens zil_prt_rec_truncate(zilog_t *zilog, int txtype, lr_truncate_t *lr)
184789Sahrens {
18510922SJeff.Bonwick@Sun.COM (void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", prefix,
186789Sahrens (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
187789Sahrens (u_longlong_t)lr->lr_length);
188789Sahrens }
189789Sahrens
190789Sahrens /* ARGSUSED */
191789Sahrens static void
zil_prt_rec_setattr(zilog_t * zilog,int txtype,lr_setattr_t * lr)192789Sahrens zil_prt_rec_setattr(zilog_t *zilog, int txtype, lr_setattr_t *lr)
193789Sahrens {
194789Sahrens time_t atime = (time_t)lr->lr_atime[0];
195789Sahrens time_t mtime = (time_t)lr->lr_mtime[0];
196789Sahrens
19710922SJeff.Bonwick@Sun.COM (void) printf("%sfoid %llu, mask 0x%llx\n", prefix,
198789Sahrens (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
199789Sahrens
200789Sahrens if (lr->lr_mask & AT_MODE) {
20110922SJeff.Bonwick@Sun.COM (void) printf("%sAT_MODE %llo\n", prefix,
202789Sahrens (longlong_t)lr->lr_mode);
203789Sahrens }
204789Sahrens
205789Sahrens if (lr->lr_mask & AT_UID) {
20610922SJeff.Bonwick@Sun.COM (void) printf("%sAT_UID %llu\n", prefix,
207789Sahrens (u_longlong_t)lr->lr_uid);
208789Sahrens }
209789Sahrens
210789Sahrens if (lr->lr_mask & AT_GID) {
21110922SJeff.Bonwick@Sun.COM (void) printf("%sAT_GID %llu\n", prefix,
212789Sahrens (u_longlong_t)lr->lr_gid);
213789Sahrens }
214789Sahrens
215789Sahrens if (lr->lr_mask & AT_SIZE) {
21610922SJeff.Bonwick@Sun.COM (void) printf("%sAT_SIZE %llu\n", prefix,
217789Sahrens (u_longlong_t)lr->lr_size);
218789Sahrens }
219789Sahrens
220789Sahrens if (lr->lr_mask & AT_ATIME) {
22110922SJeff.Bonwick@Sun.COM (void) printf("%sAT_ATIME %llu.%09llu %s", prefix,
222789Sahrens (u_longlong_t)lr->lr_atime[0],
223789Sahrens (u_longlong_t)lr->lr_atime[1],
224789Sahrens ctime(&atime));
225789Sahrens }
226789Sahrens
227789Sahrens if (lr->lr_mask & AT_MTIME) {
22810922SJeff.Bonwick@Sun.COM (void) printf("%sAT_MTIME %llu.%09llu %s", prefix,
229789Sahrens (u_longlong_t)lr->lr_mtime[0],
230789Sahrens (u_longlong_t)lr->lr_mtime[1],
231789Sahrens ctime(&mtime));
232789Sahrens }
233789Sahrens }
234789Sahrens
235789Sahrens /* ARGSUSED */
236789Sahrens static void
zil_prt_rec_acl(zilog_t * zilog,int txtype,lr_acl_t * lr)237789Sahrens zil_prt_rec_acl(zilog_t *zilog, int txtype, lr_acl_t *lr)
238789Sahrens {
23910922SJeff.Bonwick@Sun.COM (void) printf("%sfoid %llu, aclcnt %llu\n", prefix,
240789Sahrens (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
241789Sahrens }
242789Sahrens
243789Sahrens typedef void (*zil_prt_rec_func_t)();
244789Sahrens typedef struct zil_rec_info {
245789Sahrens zil_prt_rec_func_t zri_print;
246789Sahrens char *zri_name;
247789Sahrens uint64_t zri_count;
248789Sahrens } zil_rec_info_t;
249789Sahrens
250789Sahrens static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
2515331Samw { NULL, "Total " },
2525331Samw { zil_prt_rec_create, "TX_CREATE " },
2535331Samw { zil_prt_rec_create, "TX_MKDIR " },
2545331Samw { zil_prt_rec_create, "TX_MKXATTR " },
2555331Samw { zil_prt_rec_create, "TX_SYMLINK " },
2565331Samw { zil_prt_rec_remove, "TX_REMOVE " },
2575331Samw { zil_prt_rec_remove, "TX_RMDIR " },
2585331Samw { zil_prt_rec_link, "TX_LINK " },
2595331Samw { zil_prt_rec_rename, "TX_RENAME " },
2605331Samw { zil_prt_rec_write, "TX_WRITE " },
2615331Samw { zil_prt_rec_truncate, "TX_TRUNCATE " },
2625331Samw { zil_prt_rec_setattr, "TX_SETATTR " },
2635331Samw { zil_prt_rec_acl, "TX_ACL_V0 " },
2645331Samw { zil_prt_rec_acl, "TX_ACL_ACL " },
2655331Samw { zil_prt_rec_create, "TX_CREATE_ACL " },
2665331Samw { zil_prt_rec_create, "TX_CREATE_ATTR " },
2675331Samw { zil_prt_rec_create, "TX_CREATE_ACL_ATTR " },
2685331Samw { zil_prt_rec_create, "TX_MKDIR_ACL " },
2695331Samw { zil_prt_rec_create, "TX_MKDIR_ATTR " },
2705331Samw { zil_prt_rec_create, "TX_MKDIR_ACL_ATTR " },
27110800SNeil.Perrin@Sun.COM { zil_prt_rec_write, "TX_WRITE2 " },
272789Sahrens };
273789Sahrens
274789Sahrens /* ARGSUSED */
27510922SJeff.Bonwick@Sun.COM static int
print_log_record(zilog_t * zilog,lr_t * lr,void * arg,uint64_t claim_txg)2761601Sbonwick print_log_record(zilog_t *zilog, lr_t *lr, void *arg, uint64_t claim_txg)
277789Sahrens {
278789Sahrens int txtype;
279789Sahrens int verbose = MAX(dump_opt['d'], dump_opt['i']);
280789Sahrens
2815331Samw /* reduce size of txtype to strip off TX_CI bit */
282789Sahrens txtype = lr->lrc_txtype;
283789Sahrens
284789Sahrens ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
285789Sahrens ASSERT(lr->lrc_txg);
286789Sahrens
2875331Samw (void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
2885331Samw (lr->lrc_txtype & TX_CI) ? "CI-" : "",
289789Sahrens zil_rec_info[txtype].zri_name,
290789Sahrens (u_longlong_t)lr->lrc_reclen,
291789Sahrens (u_longlong_t)lr->lrc_txg,
292789Sahrens (u_longlong_t)lr->lrc_seq);
293789Sahrens
294789Sahrens if (txtype && verbose >= 3)
295789Sahrens zil_rec_info[txtype].zri_print(zilog, txtype, lr);
296789Sahrens
297789Sahrens zil_rec_info[txtype].zri_count++;
298789Sahrens zil_rec_info[0].zri_count++;
29910922SJeff.Bonwick@Sun.COM
30010922SJeff.Bonwick@Sun.COM return (0);
301789Sahrens }
302789Sahrens
303789Sahrens /* ARGSUSED */
30410922SJeff.Bonwick@Sun.COM static int
print_log_block(zilog_t * zilog,blkptr_t * bp,void * arg,uint64_t claim_txg)3051601Sbonwick print_log_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
306789Sahrens {
30710922SJeff.Bonwick@Sun.COM char blkbuf[BP_SPRINTF_LEN + 10];
308789Sahrens int verbose = MAX(dump_opt['d'], dump_opt['i']);
3091601Sbonwick char *claim;
310789Sahrens
311789Sahrens if (verbose <= 3)
31210922SJeff.Bonwick@Sun.COM return (0);
313789Sahrens
314789Sahrens if (verbose >= 5) {
315789Sahrens (void) strcpy(blkbuf, ", ");
31610922SJeff.Bonwick@Sun.COM sprintf_blkptr(blkbuf + strlen(blkbuf), bp);
317789Sahrens } else {
318789Sahrens blkbuf[0] = '\0';
319789Sahrens }
320789Sahrens
3211601Sbonwick if (claim_txg != 0)
3221601Sbonwick claim = "already claimed";
3231601Sbonwick else if (bp->blk_birth >= spa_first_txg(zilog->zl_spa))
3241601Sbonwick claim = "will claim";
3251601Sbonwick else
3261601Sbonwick claim = "won't claim";
3271601Sbonwick
328789Sahrens (void) printf("\tBlock seqno %llu, %s%s\n",
3291807Sbonwick (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
33010922SJeff.Bonwick@Sun.COM
33110922SJeff.Bonwick@Sun.COM return (0);
332789Sahrens }
333789Sahrens
334789Sahrens static void
print_log_stats(int verbose)335789Sahrens print_log_stats(int verbose)
336789Sahrens {
337789Sahrens int i, w, p10;
338789Sahrens
339789Sahrens if (verbose > 3)
340789Sahrens (void) printf("\n");
341789Sahrens
342789Sahrens if (zil_rec_info[0].zri_count == 0)
343789Sahrens return;
344789Sahrens
345789Sahrens for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
346789Sahrens w++;
347789Sahrens
348789Sahrens for (i = 0; i < TX_MAX_TYPE; i++)
349789Sahrens if (zil_rec_info[i].zri_count || verbose >= 3)
350789Sahrens (void) printf("\t\t%s %*llu\n",
351789Sahrens zil_rec_info[i].zri_name, w,
352789Sahrens (u_longlong_t)zil_rec_info[i].zri_count);
353789Sahrens (void) printf("\n");
354789Sahrens }
355789Sahrens
356789Sahrens /* ARGSUSED */
357789Sahrens void
dump_intent_log(zilog_t * zilog)358789Sahrens dump_intent_log(zilog_t *zilog)
359789Sahrens {
3601807Sbonwick const zil_header_t *zh = zilog->zl_header;
361789Sahrens int verbose = MAX(dump_opt['d'], dump_opt['i']);
362789Sahrens int i;
363789Sahrens
36410922SJeff.Bonwick@Sun.COM if (zh->zh_log.blk_birth == 0 || verbose < 1)
365789Sahrens return;
366789Sahrens
36710922SJeff.Bonwick@Sun.COM (void) printf("\n ZIL header: claim_txg %llu, "
36810922SJeff.Bonwick@Sun.COM "claim_blk_seq %llu, claim_lr_seq %llu",
36910922SJeff.Bonwick@Sun.COM (u_longlong_t)zh->zh_claim_txg,
37010922SJeff.Bonwick@Sun.COM (u_longlong_t)zh->zh_claim_blk_seq,
37110922SJeff.Bonwick@Sun.COM (u_longlong_t)zh->zh_claim_lr_seq);
3728989SNeil.Perrin@Sun.COM (void) printf(" replay_seq %llu, flags 0x%llx\n",
3738989SNeil.Perrin@Sun.COM (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
374789Sahrens
375789Sahrens for (i = 0; i < TX_MAX_TYPE; i++)
376789Sahrens zil_rec_info[i].zri_count = 0;
377789Sahrens
378789Sahrens if (verbose >= 2) {
379789Sahrens (void) printf("\n");
3801807Sbonwick (void) zil_parse(zilog, print_log_block, print_log_record, NULL,
3811601Sbonwick zh->zh_claim_txg);
382789Sahrens print_log_stats(verbose);
383789Sahrens }
384789Sahrens }
385