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