Print "(repairing)" in zpool status again
[zfs.git] / cmd / zdb / zdb_il.c
blobc12178effae0db50425645e1029841a003f195b9
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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.
28 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
32 * Print intent log header and statistics.
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/spa_impl.h>
46 #include <sys/abd.h>
48 #include "zdb.h"
50 extern uint8_t dump_opt[256];
52 static char tab_prefix[4] = "\t\t\t";
54 static void
55 print_log_bp(const blkptr_t *bp, const char *prefix)
57 char blkbuf[BP_SPRINTF_LEN];
59 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
60 (void) printf("%s%s\n", prefix, blkbuf);
63 /* ARGSUSED */
64 static void
65 zil_prt_rec_create(zilog_t *zilog, int txtype, void *arg)
67 lr_create_t *lr = arg;
68 time_t crtime = lr->lr_crtime[0];
69 char *name, *link;
70 lr_attr_t *lrattr;
72 name = (char *)(lr + 1);
74 if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
75 lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
76 lrattr = (lr_attr_t *)(lr + 1);
77 name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
80 if (txtype == TX_SYMLINK) {
81 link = name + strlen(name) + 1;
82 (void) printf("%s%s -> %s\n", tab_prefix, name, link);
83 } else if (txtype != TX_MKXATTR) {
84 (void) printf("%s%s\n", tab_prefix, name);
87 (void) printf("%s%s", tab_prefix, ctime(&crtime));
88 (void) printf("%sdoid %llu, foid %llu, slots %llu, mode %llo\n",
89 tab_prefix, (u_longlong_t)lr->lr_doid,
90 (u_longlong_t)LR_FOID_GET_OBJ(lr->lr_foid),
91 (u_longlong_t)LR_FOID_GET_SLOTS(lr->lr_foid),
92 (longlong_t)lr->lr_mode);
93 (void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n",
94 tab_prefix,
95 (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
96 (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
99 /* ARGSUSED */
100 static void
101 zil_prt_rec_remove(zilog_t *zilog, int txtype, void *arg)
103 lr_remove_t *lr = arg;
105 (void) printf("%sdoid %llu, name %s\n", tab_prefix,
106 (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
109 /* ARGSUSED */
110 static void
111 zil_prt_rec_link(zilog_t *zilog, int txtype, void *arg)
113 lr_link_t *lr = arg;
115 (void) printf("%sdoid %llu, link_obj %llu, name %s\n", tab_prefix,
116 (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
117 (char *)(lr + 1));
120 /* ARGSUSED */
121 static void
122 zil_prt_rec_rename(zilog_t *zilog, int txtype, void *arg)
124 lr_rename_t *lr = arg;
125 char *snm = (char *)(lr + 1);
126 char *tnm = snm + strlen(snm) + 1;
128 (void) printf("%ssdoid %llu, tdoid %llu\n", tab_prefix,
129 (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
130 (void) printf("%ssrc %s tgt %s\n", tab_prefix, snm, tnm);
133 /* ARGSUSED */
134 static int
135 zil_prt_rec_write_cb(void *data, size_t len, void *unused)
137 char *cdata = data;
139 for (size_t i = 0; i < len; i++) {
140 if (isprint(*cdata))
141 (void) printf("%c ", *cdata);
142 else
143 (void) printf("%2X", *cdata);
144 cdata++;
146 return (0);
149 /* ARGSUSED */
150 static void
151 zil_prt_rec_write(zilog_t *zilog, int txtype, void *arg)
153 lr_write_t *lr = arg;
154 abd_t *data;
155 blkptr_t *bp = &lr->lr_blkptr;
156 zbookmark_phys_t zb;
157 int verbose = MAX(dump_opt['d'], dump_opt['i']);
158 int error;
160 (void) printf("%sfoid %llu, offset %llx, length %llx\n", tab_prefix,
161 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
162 (u_longlong_t)lr->lr_length);
164 if (txtype == TX_WRITE2 || verbose < 5)
165 return;
167 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
168 (void) printf("%shas blkptr, %s\n", tab_prefix,
169 !BP_IS_HOLE(bp) &&
170 bp->blk_birth >= spa_min_claim_txg(zilog->zl_spa) ?
171 "will claim" : "won't claim");
172 print_log_bp(bp, tab_prefix);
174 if (BP_IS_HOLE(bp)) {
175 (void) printf("\t\t\tLSIZE 0x%llx\n",
176 (u_longlong_t)BP_GET_LSIZE(bp));
177 (void) printf("%s<hole>\n", tab_prefix);
178 return;
180 if (bp->blk_birth < zilog->zl_header->zh_claim_txg) {
181 (void) printf("%s<block already committed>\n",
182 tab_prefix);
183 return;
186 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
187 lr->lr_foid, ZB_ZIL_LEVEL,
188 lr->lr_offset / BP_GET_LSIZE(bp));
190 data = abd_alloc(BP_GET_LSIZE(bp), B_FALSE);
191 error = zio_wait(zio_read(NULL, zilog->zl_spa,
192 bp, data, BP_GET_LSIZE(bp), NULL, NULL,
193 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
194 if (error)
195 goto out;
196 } else {
197 /* data is stored after the end of the lr_write record */
198 data = abd_alloc(lr->lr_length, B_FALSE);
199 abd_copy_from_buf(data, lr + 1, lr->lr_length);
202 (void) printf("%s", tab_prefix);
203 (void) abd_iterate_func(data,
204 0, MIN(lr->lr_length, (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE)),
205 zil_prt_rec_write_cb, NULL);
206 (void) printf("\n");
208 out:
209 abd_free(data);
212 /* ARGSUSED */
213 static void
214 zil_prt_rec_truncate(zilog_t *zilog, int txtype, void *arg)
216 lr_truncate_t *lr = arg;
218 (void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", tab_prefix,
219 (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
220 (u_longlong_t)lr->lr_length);
223 /* ARGSUSED */
224 static void
225 zil_prt_rec_setattr(zilog_t *zilog, int txtype, void *arg)
227 lr_setattr_t *lr = arg;
228 time_t atime = (time_t)lr->lr_atime[0];
229 time_t mtime = (time_t)lr->lr_mtime[0];
231 (void) printf("%sfoid %llu, mask 0x%llx\n", tab_prefix,
232 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
234 if (lr->lr_mask & AT_MODE) {
235 (void) printf("%sAT_MODE %llo\n", tab_prefix,
236 (longlong_t)lr->lr_mode);
239 if (lr->lr_mask & AT_UID) {
240 (void) printf("%sAT_UID %llu\n", tab_prefix,
241 (u_longlong_t)lr->lr_uid);
244 if (lr->lr_mask & AT_GID) {
245 (void) printf("%sAT_GID %llu\n", tab_prefix,
246 (u_longlong_t)lr->lr_gid);
249 if (lr->lr_mask & AT_SIZE) {
250 (void) printf("%sAT_SIZE %llu\n", tab_prefix,
251 (u_longlong_t)lr->lr_size);
254 if (lr->lr_mask & AT_ATIME) {
255 (void) printf("%sAT_ATIME %llu.%09llu %s", tab_prefix,
256 (u_longlong_t)lr->lr_atime[0],
257 (u_longlong_t)lr->lr_atime[1],
258 ctime(&atime));
261 if (lr->lr_mask & AT_MTIME) {
262 (void) printf("%sAT_MTIME %llu.%09llu %s", tab_prefix,
263 (u_longlong_t)lr->lr_mtime[0],
264 (u_longlong_t)lr->lr_mtime[1],
265 ctime(&mtime));
269 /* ARGSUSED */
270 static void
271 zil_prt_rec_acl(zilog_t *zilog, int txtype, void *arg)
273 lr_acl_t *lr = arg;
275 (void) printf("%sfoid %llu, aclcnt %llu\n", tab_prefix,
276 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
279 typedef void (*zil_prt_rec_func_t)(zilog_t *, int, void *);
280 typedef struct zil_rec_info {
281 zil_prt_rec_func_t zri_print;
282 const char *zri_name;
283 uint64_t zri_count;
284 } zil_rec_info_t;
286 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
287 {.zri_print = NULL, .zri_name = "Total "},
288 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE "},
289 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR "},
290 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKXATTR "},
291 {.zri_print = zil_prt_rec_create, .zri_name = "TX_SYMLINK "},
292 {.zri_print = zil_prt_rec_remove, .zri_name = "TX_REMOVE "},
293 {.zri_print = zil_prt_rec_remove, .zri_name = "TX_RMDIR "},
294 {.zri_print = zil_prt_rec_link, .zri_name = "TX_LINK "},
295 {.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME "},
296 {.zri_print = zil_prt_rec_write, .zri_name = "TX_WRITE "},
297 {.zri_print = zil_prt_rec_truncate, .zri_name = "TX_TRUNCATE "},
298 {.zri_print = zil_prt_rec_setattr, .zri_name = "TX_SETATTR "},
299 {.zri_print = zil_prt_rec_acl, .zri_name = "TX_ACL_V0 "},
300 {.zri_print = zil_prt_rec_acl, .zri_name = "TX_ACL_ACL "},
301 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ACL "},
302 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ATTR "},
303 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ACL_ATTR "},
304 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ACL "},
305 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ATTR "},
306 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ACL_ATTR "},
307 {.zri_print = zil_prt_rec_write, .zri_name = "TX_WRITE2 "},
310 /* ARGSUSED */
311 static int
312 print_log_record(zilog_t *zilog, lr_t *lr, void *arg, uint64_t claim_txg)
314 int txtype;
315 int verbose = MAX(dump_opt['d'], dump_opt['i']);
317 /* reduce size of txtype to strip off TX_CI bit */
318 txtype = lr->lrc_txtype;
320 ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
321 ASSERT(lr->lrc_txg);
323 (void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
324 (lr->lrc_txtype & TX_CI) ? "CI-" : "",
325 zil_rec_info[txtype].zri_name,
326 (u_longlong_t)lr->lrc_reclen,
327 (u_longlong_t)lr->lrc_txg,
328 (u_longlong_t)lr->lrc_seq);
330 if (txtype && verbose >= 3) {
331 if (!zilog->zl_os->os_encrypted) {
332 zil_rec_info[txtype].zri_print(zilog, txtype, lr);
333 } else {
334 (void) printf("%s(encrypted)\n", tab_prefix);
338 zil_rec_info[txtype].zri_count++;
339 zil_rec_info[0].zri_count++;
341 return (0);
344 /* ARGSUSED */
345 static int
346 print_log_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
348 char blkbuf[BP_SPRINTF_LEN + 10];
349 int verbose = MAX(dump_opt['d'], dump_opt['i']);
350 const char *claim;
352 if (verbose <= 3)
353 return (0);
355 if (verbose >= 5) {
356 (void) strcpy(blkbuf, ", ");
357 snprintf_blkptr(blkbuf + strlen(blkbuf),
358 sizeof (blkbuf) - strlen(blkbuf), bp);
359 } else {
360 blkbuf[0] = '\0';
363 if (claim_txg != 0)
364 claim = "already claimed";
365 else if (bp->blk_birth >= spa_min_claim_txg(zilog->zl_spa))
366 claim = "will claim";
367 else
368 claim = "won't claim";
370 (void) printf("\tBlock seqno %llu, %s%s\n",
371 (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
373 return (0);
376 static void
377 print_log_stats(int verbose)
379 unsigned i, w, p10;
381 if (verbose > 3)
382 (void) printf("\n");
384 if (zil_rec_info[0].zri_count == 0)
385 return;
387 for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
388 w++;
390 for (i = 0; i < TX_MAX_TYPE; i++)
391 if (zil_rec_info[i].zri_count || verbose >= 3)
392 (void) printf("\t\t%s %*llu\n",
393 zil_rec_info[i].zri_name, w,
394 (u_longlong_t)zil_rec_info[i].zri_count);
395 (void) printf("\n");
398 /* ARGSUSED */
399 void
400 dump_intent_log(zilog_t *zilog)
402 const zil_header_t *zh = zilog->zl_header;
403 int verbose = MAX(dump_opt['d'], dump_opt['i']);
404 int i;
406 if (BP_IS_HOLE(&zh->zh_log) || verbose < 1)
407 return;
409 (void) printf("\n ZIL header: claim_txg %llu, "
410 "claim_blk_seq %llu, claim_lr_seq %llu",
411 (u_longlong_t)zh->zh_claim_txg,
412 (u_longlong_t)zh->zh_claim_blk_seq,
413 (u_longlong_t)zh->zh_claim_lr_seq);
414 (void) printf(" replay_seq %llu, flags 0x%llx\n",
415 (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
417 for (i = 0; i < TX_MAX_TYPE; i++)
418 zil_rec_info[i].zri_count = 0;
420 /* see comment in zil_claim() or zil_check_log_chain() */
421 if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
422 zh->zh_claim_txg == 0)
423 return;
425 if (verbose >= 2) {
426 (void) printf("\n");
427 (void) zil_parse(zilog, print_log_block, print_log_record, NULL,
428 zh->zh_claim_txg, B_FALSE);
429 print_log_stats(verbose);