Add -p switch to "zpool get"
[zfs.git] / cmd / zdb / zdb_il.c
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  * Print intent log header and statistics.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <sys/zfs_context.h>
35 #include <sys/spa.h>
36 #include <sys/dmu.h>
37 #include <sys/stat.h>
38 #include <sys/resource.h>
39 #include <sys/zil.h>
40 #include <sys/zil_impl.h>
41
42 extern uint8_t dump_opt[256];
43
44 static char prefix[4] = "\t\t\t";
45
46 static void
47 print_log_bp(const blkptr_t *bp, const char *prefix)
48 {
49         char blkbuf[BP_SPRINTF_LEN];
50
51         sprintf_blkptr(blkbuf, bp);
52         (void) printf("%s%s\n", prefix, blkbuf);
53 }
54
55 /* ARGSUSED */
56 static void
57 zil_prt_rec_create(zilog_t *zilog, int txtype, lr_create_t *lr)
58 {
59         time_t crtime = lr->lr_crtime[0];
60         char *name, *link;
61         lr_attr_t *lrattr;
62
63         name = (char *)(lr + 1);
64
65         if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
66             lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
67                 lrattr = (lr_attr_t *)(lr + 1);
68                 name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
69         }
70
71         if (txtype == TX_SYMLINK) {
72                 link = name + strlen(name) + 1;
73                 (void) printf("%s%s -> %s\n", prefix, name, link);
74         } else if (txtype != TX_MKXATTR) {
75                 (void) printf("%s%s\n", prefix, name);
76         }
77
78         (void) printf("%s%s", prefix, ctime(&crtime));
79         (void) printf("%sdoid %llu, foid %llu, mode %llo\n", prefix,
80             (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_foid,
81             (longlong_t)lr->lr_mode);
82         (void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n", prefix,
83             (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
84             (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
85 }
86
87 /* ARGSUSED */
88 static void
89 zil_prt_rec_remove(zilog_t *zilog, int txtype, lr_remove_t *lr)
90 {
91         (void) printf("%sdoid %llu, name %s\n", prefix,
92             (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
93 }
94
95 /* ARGSUSED */
96 static void
97 zil_prt_rec_link(zilog_t *zilog, int txtype, lr_link_t *lr)
98 {
99         (void) printf("%sdoid %llu, link_obj %llu, name %s\n", prefix,
100             (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
101             (char *)(lr + 1));
102 }
103
104 /* ARGSUSED */
105 static void
106 zil_prt_rec_rename(zilog_t *zilog, int txtype, lr_rename_t *lr)
107 {
108         char *snm = (char *)(lr + 1);
109         char *tnm = snm + strlen(snm) + 1;
110
111         (void) printf("%ssdoid %llu, tdoid %llu\n", prefix,
112             (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
113         (void) printf("%ssrc %s tgt %s\n", prefix, snm, tnm);
114 }
115
116 /* ARGSUSED */
117 static void
118 zil_prt_rec_write(zilog_t *zilog, int txtype, lr_write_t *lr)
119 {
120         char *data, *dlimit;
121         blkptr_t *bp = &lr->lr_blkptr;
122         zbookmark_t zb;
123         char buf[SPA_MAXBLOCKSIZE];
124         int verbose = MAX(dump_opt['d'], dump_opt['i']);
125         int error;
126
127         (void) printf("%sfoid %llu, offset %llx, length %llx\n", prefix,
128             (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
129             (u_longlong_t)lr->lr_length);
130
131         if (txtype == TX_WRITE2 || verbose < 5)
132                 return;
133
134         if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
135                 (void) printf("%shas blkptr, %s\n", prefix,
136                     bp->blk_birth >= spa_first_txg(zilog->zl_spa) ?
137                     "will claim" : "won't claim");
138                 print_log_bp(bp, prefix);
139
140                 if (BP_IS_HOLE(bp)) {
141                         (void) printf("\t\t\tLSIZE 0x%llx\n",
142                             (u_longlong_t)BP_GET_LSIZE(bp));
143                 }
144                 if (bp->blk_birth == 0) {
145                         bzero(buf, sizeof (buf));
146                         (void) printf("%s<hole>\n", prefix);
147                         return;
148                 }
149                 if (bp->blk_birth < zilog->zl_header->zh_claim_txg) {
150                         (void) printf("%s<block already committed>\n", prefix);
151                         return;
152                 }
153
154                 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
155                     lr->lr_foid, ZB_ZIL_LEVEL,
156                     lr->lr_offset / BP_GET_LSIZE(bp));
157
158                 error = zio_wait(zio_read(NULL, zilog->zl_spa,
159                     bp, buf, BP_GET_LSIZE(bp), NULL, NULL,
160                     ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
161                 if (error)
162                         return;
163                 data = buf;
164         } else {
165                 data = (char *)(lr + 1);
166         }
167
168         dlimit = data + MIN(lr->lr_length,
169             (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE));
170
171         (void) printf("%s", prefix);
172         while (data < dlimit) {
173                 if (isprint(*data))
174                         (void) printf("%c ", *data);
175                 else
176                         (void) printf("%2hhX", *data);
177                 data++;
178         }
179         (void) printf("\n");
180 }
181
182 /* ARGSUSED */
183 static void
184 zil_prt_rec_truncate(zilog_t *zilog, int txtype, lr_truncate_t *lr)
185 {
186         (void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", prefix,
187             (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
188             (u_longlong_t)lr->lr_length);
189 }
190
191 /* ARGSUSED */
192 static void
193 zil_prt_rec_setattr(zilog_t *zilog, int txtype, lr_setattr_t *lr)
194 {
195         time_t atime = (time_t)lr->lr_atime[0];
196         time_t mtime = (time_t)lr->lr_mtime[0];
197
198         (void) printf("%sfoid %llu, mask 0x%llx\n", prefix,
199             (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
200
201         if (lr->lr_mask & AT_MODE) {
202                 (void) printf("%sAT_MODE  %llo\n", prefix,
203                     (longlong_t)lr->lr_mode);
204         }
205
206         if (lr->lr_mask & AT_UID) {
207                 (void) printf("%sAT_UID   %llu\n", prefix,
208                     (u_longlong_t)lr->lr_uid);
209         }
210
211         if (lr->lr_mask & AT_GID) {
212                 (void) printf("%sAT_GID   %llu\n", prefix,
213                     (u_longlong_t)lr->lr_gid);
214         }
215
216         if (lr->lr_mask & AT_SIZE) {
217                 (void) printf("%sAT_SIZE  %llu\n", prefix,
218                     (u_longlong_t)lr->lr_size);
219         }
220
221         if (lr->lr_mask & AT_ATIME) {
222                 (void) printf("%sAT_ATIME %llu.%09llu %s", prefix,
223                     (u_longlong_t)lr->lr_atime[0],
224                     (u_longlong_t)lr->lr_atime[1],
225                     ctime(&atime));
226         }
227
228         if (lr->lr_mask & AT_MTIME) {
229                 (void) printf("%sAT_MTIME %llu.%09llu %s", prefix,
230                     (u_longlong_t)lr->lr_mtime[0],
231                     (u_longlong_t)lr->lr_mtime[1],
232                     ctime(&mtime));
233         }
234 }
235
236 /* ARGSUSED */
237 static void
238 zil_prt_rec_acl(zilog_t *zilog, int txtype, lr_acl_t *lr)
239 {
240         (void) printf("%sfoid %llu, aclcnt %llu\n", prefix,
241             (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
242 }
243
244 typedef void (*zil_prt_rec_func_t)(zilog_t *, int, void *);
245 typedef struct zil_rec_info {
246         zil_prt_rec_func_t      zri_print;
247         char                    *zri_name;
248         uint64_t                zri_count;
249 } zil_rec_info_t;
250
251 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
252         { NULL,                 "Total              " },
253         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_CREATE          " },
254         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_MKDIR           " },
255         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_MKXATTR         " },
256         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_SYMLINK         " },
257         { (zil_prt_rec_func_t)zil_prt_rec_remove,       "TX_REMOVE          " },
258         { (zil_prt_rec_func_t)zil_prt_rec_remove,       "TX_RMDIR           " },
259         { (zil_prt_rec_func_t)zil_prt_rec_link,         "TX_LINK            " },
260         { (zil_prt_rec_func_t)zil_prt_rec_rename,       "TX_RENAME          " },
261         { (zil_prt_rec_func_t)zil_prt_rec_write,        "TX_WRITE           " },
262         { (zil_prt_rec_func_t)zil_prt_rec_truncate,     "TX_TRUNCATE        " },
263         { (zil_prt_rec_func_t)zil_prt_rec_setattr,      "TX_SETATTR         " },
264         { (zil_prt_rec_func_t)zil_prt_rec_acl,          "TX_ACL_V0          " },
265         { (zil_prt_rec_func_t)zil_prt_rec_acl,          "TX_ACL_ACL         " },
266         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_CREATE_ACL      " },
267         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_CREATE_ATTR     " },
268         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_CREATE_ACL_ATTR " },
269         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_MKDIR_ACL       " },
270         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_MKDIR_ATTR      " },
271         { (zil_prt_rec_func_t)zil_prt_rec_create,       "TX_MKDIR_ACL_ATTR  " },
272         { (zil_prt_rec_func_t)zil_prt_rec_write,        "TX_WRITE2          " },
273 };
274
275 /* ARGSUSED */
276 static int
277 print_log_record(zilog_t *zilog, lr_t *lr, void *arg, uint64_t claim_txg)
278 {
279         int txtype;
280         int verbose = MAX(dump_opt['d'], dump_opt['i']);
281
282         /* reduce size of txtype to strip off TX_CI bit */
283         txtype = lr->lrc_txtype;
284
285         ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
286         ASSERT(lr->lrc_txg);
287
288         (void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
289             (lr->lrc_txtype & TX_CI) ? "CI-" : "",
290             zil_rec_info[txtype].zri_name,
291             (u_longlong_t)lr->lrc_reclen,
292             (u_longlong_t)lr->lrc_txg,
293             (u_longlong_t)lr->lrc_seq);
294
295         if (txtype && verbose >= 3)
296                 zil_rec_info[txtype].zri_print(zilog, txtype, lr);
297
298         zil_rec_info[txtype].zri_count++;
299         zil_rec_info[0].zri_count++;
300
301         return (0);
302 }
303
304 /* ARGSUSED */
305 static int
306 print_log_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
307 {
308         char blkbuf[BP_SPRINTF_LEN + 10];
309         int verbose = MAX(dump_opt['d'], dump_opt['i']);
310         char *claim;
311
312         if (verbose <= 3)
313                 return (0);
314
315         if (verbose >= 5) {
316                 (void) strcpy(blkbuf, ", ");
317                 sprintf_blkptr(blkbuf + strlen(blkbuf), bp);
318         } else {
319                 blkbuf[0] = '\0';
320         }
321
322         if (claim_txg != 0)
323                 claim = "already claimed";
324         else if (bp->blk_birth >= spa_first_txg(zilog->zl_spa))
325                 claim = "will claim";
326         else
327                 claim = "won't claim";
328
329         (void) printf("\tBlock seqno %llu, %s%s\n",
330             (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
331
332         return (0);
333 }
334
335 static void
336 print_log_stats(int verbose)
337 {
338         int i, w, p10;
339
340         if (verbose > 3)
341                 (void) printf("\n");
342
343         if (zil_rec_info[0].zri_count == 0)
344                 return;
345
346         for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
347                 w++;
348
349         for (i = 0; i < TX_MAX_TYPE; i++)
350                 if (zil_rec_info[i].zri_count || verbose >= 3)
351                         (void) printf("\t\t%s %*llu\n",
352                             zil_rec_info[i].zri_name, w,
353                             (u_longlong_t)zil_rec_info[i].zri_count);
354         (void) printf("\n");
355 }
356
357 /* ARGSUSED */
358 void
359 dump_intent_log(zilog_t *zilog)
360 {
361         const zil_header_t *zh = zilog->zl_header;
362         int verbose = MAX(dump_opt['d'], dump_opt['i']);
363         int i;
364
365         if (zh->zh_log.blk_birth == 0 || verbose < 1)
366                 return;
367
368         (void) printf("\n    ZIL header: claim_txg %llu, "
369             "claim_blk_seq %llu, claim_lr_seq %llu",
370             (u_longlong_t)zh->zh_claim_txg,
371             (u_longlong_t)zh->zh_claim_blk_seq,
372             (u_longlong_t)zh->zh_claim_lr_seq);
373         (void) printf(" replay_seq %llu, flags 0x%llx\n",
374             (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
375
376         for (i = 0; i < TX_MAX_TYPE; i++)
377                 zil_rec_info[i].zri_count = 0;
378
379         if (verbose >= 2) {
380                 (void) printf("\n");
381                 (void) zil_parse(zilog, print_log_block, print_log_record, NULL,
382                     zh->zh_claim_txg);
383                 print_log_stats(verbose);
384         }
385 }