FreeBSD: ignore some includes when not building kernel
[zfs.git] / cmd / zdb / zdb_il.c
blob80d81c1154ae8adc7114c035ea248fd43fb38690
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 https://opensource.org/licenses/CDDL-1.0.
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 static void
64 zil_prt_rec_create(zilog_t *zilog, int txtype, const void *arg)
66 (void) zilog;
67 const lr_create_t *lrc = arg;
68 const _lr_create_t *lr = &lrc->lr_create;
69 time_t crtime = lr->lr_crtime[0];
70 char *name, *link;
71 lr_attr_t *lrattr;
73 name = (char *)(lr + 1);
75 if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
76 lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
77 lrattr = (lr_attr_t *)(lr + 1);
78 name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
81 if (txtype == TX_SYMLINK) {
82 link = name + strlen(name) + 1;
83 (void) printf("%s%s -> %s\n", tab_prefix, name, link);
84 } else if (txtype != TX_MKXATTR) {
85 (void) printf("%s%s\n", tab_prefix, name);
88 (void) printf("%s%s", tab_prefix, ctime(&crtime));
89 (void) printf("%sdoid %llu, foid %llu, slots %llu, mode %llo\n",
90 tab_prefix, (u_longlong_t)lr->lr_doid,
91 (u_longlong_t)LR_FOID_GET_OBJ(lr->lr_foid),
92 (u_longlong_t)LR_FOID_GET_SLOTS(lr->lr_foid),
93 (longlong_t)lr->lr_mode);
94 (void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n",
95 tab_prefix,
96 (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
97 (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
100 static void
101 zil_prt_rec_remove(zilog_t *zilog, int txtype, const void *arg)
103 (void) zilog, (void) txtype;
104 const lr_remove_t *lr = arg;
106 (void) printf("%sdoid %llu, name %s\n", tab_prefix,
107 (u_longlong_t)lr->lr_doid, (char *)(lr + 1));
110 static void
111 zil_prt_rec_link(zilog_t *zilog, int txtype, const void *arg)
113 (void) zilog, (void) txtype;
114 const lr_link_t *lr = arg;
116 (void) printf("%sdoid %llu, link_obj %llu, name %s\n", tab_prefix,
117 (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
118 (char *)(lr + 1));
121 static void
122 zil_prt_rec_rename(zilog_t *zilog, int txtype, const void *arg)
124 (void) zilog, (void) txtype;
125 const lr_rename_t *lrr = arg;
126 const _lr_rename_t *lr = &lrr->lr_rename;
127 char *snm = (char *)(lr + 1);
128 char *tnm = snm + strlen(snm) + 1;
130 (void) printf("%ssdoid %llu, tdoid %llu\n", tab_prefix,
131 (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
132 (void) printf("%ssrc %s tgt %s\n", tab_prefix, snm, tnm);
133 switch (txtype) {
134 case TX_RENAME_EXCHANGE:
135 (void) printf("%sflags RENAME_EXCHANGE\n", tab_prefix);
136 break;
137 case TX_RENAME_WHITEOUT:
138 (void) printf("%sflags RENAME_WHITEOUT\n", tab_prefix);
139 break;
143 static int
144 zil_prt_rec_write_cb(void *data, size_t len, void *unused)
146 (void) unused;
147 char *cdata = data;
149 for (size_t i = 0; i < len; i++) {
150 if (isprint(*cdata))
151 (void) printf("%c ", *cdata);
152 else
153 (void) printf("%2X", *cdata);
154 cdata++;
156 return (0);
159 static void
160 zil_prt_rec_write(zilog_t *zilog, int txtype, const void *arg)
162 const lr_write_t *lr = arg;
163 abd_t *data;
164 const blkptr_t *bp = &lr->lr_blkptr;
165 zbookmark_phys_t zb;
166 int verbose = MAX(dump_opt['d'], dump_opt['i']);
167 int error;
169 (void) printf("%sfoid %llu, offset %llx, length %llx\n", tab_prefix,
170 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
171 (u_longlong_t)lr->lr_length);
173 if (txtype == TX_WRITE2 || verbose < 4)
174 return;
176 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
177 (void) printf("%shas blkptr, %s\n", tab_prefix,
178 !BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) >=
179 spa_min_claim_txg(zilog->zl_spa) ?
180 "will claim" : "won't claim");
181 print_log_bp(bp, tab_prefix);
183 if (verbose < 5)
184 return;
185 if (BP_IS_HOLE(bp)) {
186 (void) printf("\t\t\tLSIZE 0x%llx\n",
187 (u_longlong_t)BP_GET_LSIZE(bp));
188 (void) printf("%s<hole>\n", tab_prefix);
189 return;
191 if (BP_GET_LOGICAL_BIRTH(bp) < zilog->zl_header->zh_claim_txg) {
192 (void) printf("%s<block already committed>\n",
193 tab_prefix);
194 return;
197 ASSERT3U(BP_GET_LSIZE(bp), !=, 0);
198 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
199 lr->lr_foid, ZB_ZIL_LEVEL,
200 lr->lr_offset / BP_GET_LSIZE(bp));
202 data = abd_alloc(BP_GET_LSIZE(bp), B_FALSE);
203 error = zio_wait(zio_read(NULL, zilog->zl_spa,
204 bp, data, BP_GET_LSIZE(bp), NULL, NULL,
205 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
206 if (error)
207 goto out;
208 } else {
209 if (verbose < 5)
210 return;
212 /* data is stored after the end of the lr_write record */
213 data = abd_alloc(lr->lr_length, B_FALSE);
214 abd_copy_from_buf(data, lr + 1, lr->lr_length);
217 (void) printf("%s", tab_prefix);
218 (void) abd_iterate_func(data,
219 0, MIN(lr->lr_length, (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE)),
220 zil_prt_rec_write_cb, NULL);
221 (void) printf("\n");
223 out:
224 abd_free(data);
227 static void
228 zil_prt_rec_write_enc(zilog_t *zilog, int txtype, const void *arg)
230 (void) txtype;
231 const lr_write_t *lr = arg;
232 const blkptr_t *bp = &lr->lr_blkptr;
233 int verbose = MAX(dump_opt['d'], dump_opt['i']);
235 (void) printf("%s(encrypted)\n", tab_prefix);
237 if (verbose < 4)
238 return;
240 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
241 (void) printf("%shas blkptr, %s\n", tab_prefix,
242 !BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) >=
243 spa_min_claim_txg(zilog->zl_spa) ?
244 "will claim" : "won't claim");
245 print_log_bp(bp, tab_prefix);
249 static void
250 zil_prt_rec_truncate(zilog_t *zilog, int txtype, const void *arg)
252 (void) zilog, (void) txtype;
253 const lr_truncate_t *lr = arg;
255 (void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", tab_prefix,
256 (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
257 (u_longlong_t)lr->lr_length);
260 static void
261 zil_prt_rec_setattr(zilog_t *zilog, int txtype, const void *arg)
263 (void) zilog, (void) txtype;
264 const lr_setattr_t *lr = arg;
265 time_t atime = (time_t)lr->lr_atime[0];
266 time_t mtime = (time_t)lr->lr_mtime[0];
268 (void) printf("%sfoid %llu, mask 0x%llx\n", tab_prefix,
269 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
271 if (lr->lr_mask & AT_MODE) {
272 (void) printf("%sAT_MODE %llo\n", tab_prefix,
273 (longlong_t)lr->lr_mode);
276 if (lr->lr_mask & AT_UID) {
277 (void) printf("%sAT_UID %llu\n", tab_prefix,
278 (u_longlong_t)lr->lr_uid);
281 if (lr->lr_mask & AT_GID) {
282 (void) printf("%sAT_GID %llu\n", tab_prefix,
283 (u_longlong_t)lr->lr_gid);
286 if (lr->lr_mask & AT_SIZE) {
287 (void) printf("%sAT_SIZE %llu\n", tab_prefix,
288 (u_longlong_t)lr->lr_size);
291 if (lr->lr_mask & AT_ATIME) {
292 (void) printf("%sAT_ATIME %llu.%09llu %s", tab_prefix,
293 (u_longlong_t)lr->lr_atime[0],
294 (u_longlong_t)lr->lr_atime[1],
295 ctime(&atime));
298 if (lr->lr_mask & AT_MTIME) {
299 (void) printf("%sAT_MTIME %llu.%09llu %s", tab_prefix,
300 (u_longlong_t)lr->lr_mtime[0],
301 (u_longlong_t)lr->lr_mtime[1],
302 ctime(&mtime));
306 static void
307 zil_prt_rec_setsaxattr(zilog_t *zilog, int txtype, const void *arg)
309 (void) zilog, (void) txtype;
310 const lr_setsaxattr_t *lr = arg;
312 char *name = (char *)(lr + 1);
313 (void) printf("%sfoid %llu\n", tab_prefix,
314 (u_longlong_t)lr->lr_foid);
316 (void) printf("%sXAT_NAME %s\n", tab_prefix, name);
317 if (lr->lr_size == 0) {
318 (void) printf("%sXAT_VALUE NULL\n", tab_prefix);
319 } else {
320 (void) printf("%sXAT_VALUE ", tab_prefix);
321 char *val = name + (strlen(name) + 1);
322 for (int i = 0; i < lr->lr_size; i++) {
323 (void) printf("%c", *val);
324 val++;
329 static void
330 zil_prt_rec_acl(zilog_t *zilog, int txtype, const void *arg)
332 (void) zilog, (void) txtype;
333 const lr_acl_t *lr = arg;
335 (void) printf("%sfoid %llu, aclcnt %llu\n", tab_prefix,
336 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
339 static void
340 zil_prt_rec_clone_range(zilog_t *zilog, int txtype, const void *arg)
342 (void) zilog, (void) txtype;
343 const lr_clone_range_t *lr = arg;
344 int verbose = MAX(dump_opt['d'], dump_opt['i']);
346 (void) printf("%sfoid %llu, offset %llx, length %llx, blksize %llx\n",
347 tab_prefix, (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
348 (u_longlong_t)lr->lr_length, (u_longlong_t)lr->lr_blksz);
350 if (verbose < 4)
351 return;
353 for (unsigned int i = 0; i < lr->lr_nbps; i++) {
354 (void) printf("%s[%u/%llu] ", tab_prefix, i + 1,
355 (u_longlong_t)lr->lr_nbps);
356 print_log_bp(&lr->lr_bps[i], "");
360 static void
361 zil_prt_rec_clone_range_enc(zilog_t *zilog, int txtype, const void *arg)
363 (void) zilog, (void) txtype;
364 const lr_clone_range_t *lr = arg;
365 int verbose = MAX(dump_opt['d'], dump_opt['i']);
367 (void) printf("%s(encrypted)\n", tab_prefix);
369 if (verbose < 4)
370 return;
372 for (unsigned int i = 0; i < lr->lr_nbps; i++) {
373 (void) printf("%s[%u/%llu] ", tab_prefix, i + 1,
374 (u_longlong_t)lr->lr_nbps);
375 print_log_bp(&lr->lr_bps[i], "");
379 typedef void (*zil_prt_rec_func_t)(zilog_t *, int, const void *);
380 typedef struct zil_rec_info {
381 zil_prt_rec_func_t zri_print;
382 zil_prt_rec_func_t zri_print_enc;
383 const char *zri_name;
384 uint64_t zri_count;
385 } zil_rec_info_t;
387 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
388 {.zri_print = NULL, .zri_name = "Total "},
389 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE "},
390 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR "},
391 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKXATTR "},
392 {.zri_print = zil_prt_rec_create, .zri_name = "TX_SYMLINK "},
393 {.zri_print = zil_prt_rec_remove, .zri_name = "TX_REMOVE "},
394 {.zri_print = zil_prt_rec_remove, .zri_name = "TX_RMDIR "},
395 {.zri_print = zil_prt_rec_link, .zri_name = "TX_LINK "},
396 {.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME "},
397 {.zri_print = zil_prt_rec_write,
398 .zri_print_enc = zil_prt_rec_write_enc,
399 .zri_name = "TX_WRITE "},
400 {.zri_print = zil_prt_rec_truncate, .zri_name = "TX_TRUNCATE "},
401 {.zri_print = zil_prt_rec_setattr, .zri_name = "TX_SETATTR "},
402 {.zri_print = zil_prt_rec_acl, .zri_name = "TX_ACL_V0 "},
403 {.zri_print = zil_prt_rec_acl, .zri_name = "TX_ACL_ACL "},
404 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ACL "},
405 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ATTR "},
406 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ACL_ATTR "},
407 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ACL "},
408 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ATTR "},
409 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ACL_ATTR "},
410 {.zri_print = zil_prt_rec_write, .zri_name = "TX_WRITE2 "},
411 {.zri_print = zil_prt_rec_setsaxattr,
412 .zri_name = "TX_SETSAXATTR "},
413 {.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME_EXCHANGE "},
414 {.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME_WHITEOUT "},
415 {.zri_print = zil_prt_rec_clone_range,
416 .zri_print_enc = zil_prt_rec_clone_range_enc,
417 .zri_name = "TX_CLONE_RANGE "},
420 static int
421 print_log_record(zilog_t *zilog, const lr_t *lr, void *arg, uint64_t claim_txg)
423 (void) arg, (void) claim_txg;
424 int txtype;
425 int verbose = MAX(dump_opt['d'], dump_opt['i']);
427 /* reduce size of txtype to strip off TX_CI bit */
428 txtype = lr->lrc_txtype;
430 ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
431 ASSERT(lr->lrc_txg);
433 (void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
434 (lr->lrc_txtype & TX_CI) ? "CI-" : "",
435 zil_rec_info[txtype].zri_name,
436 (u_longlong_t)lr->lrc_reclen,
437 (u_longlong_t)lr->lrc_txg,
438 (u_longlong_t)lr->lrc_seq);
440 if (txtype && verbose >= 3) {
441 if (!zilog->zl_os->os_encrypted) {
442 zil_rec_info[txtype].zri_print(zilog, txtype, lr);
443 } else if (zil_rec_info[txtype].zri_print_enc) {
444 zil_rec_info[txtype].zri_print_enc(zilog, txtype, lr);
445 } else {
446 (void) printf("%s(encrypted)\n", tab_prefix);
450 zil_rec_info[txtype].zri_count++;
451 zil_rec_info[0].zri_count++;
453 return (0);
456 static int
457 print_log_block(zilog_t *zilog, const blkptr_t *bp, void *arg,
458 uint64_t claim_txg)
460 (void) arg;
461 char blkbuf[BP_SPRINTF_LEN + 10];
462 int verbose = MAX(dump_opt['d'], dump_opt['i']);
463 const char *claim;
465 if (verbose <= 3)
466 return (0);
468 if (verbose >= 5) {
469 (void) strcpy(blkbuf, ", ");
470 snprintf_blkptr(blkbuf + strlen(blkbuf),
471 sizeof (blkbuf) - strlen(blkbuf), bp);
472 } else {
473 blkbuf[0] = '\0';
476 if (claim_txg != 0)
477 claim = "already claimed";
478 else if (BP_GET_LOGICAL_BIRTH(bp) >= spa_min_claim_txg(zilog->zl_spa))
479 claim = "will claim";
480 else
481 claim = "won't claim";
483 (void) printf("\tBlock seqno %llu, %s%s\n",
484 (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
486 return (0);
489 static void
490 print_log_stats(int verbose)
492 unsigned i, w, p10;
494 if (verbose > 3)
495 (void) printf("\n");
497 if (zil_rec_info[0].zri_count == 0)
498 return;
500 for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
501 w++;
503 for (i = 0; i < TX_MAX_TYPE; i++)
504 if (zil_rec_info[i].zri_count || verbose >= 3)
505 (void) printf("\t\t%s %*llu\n",
506 zil_rec_info[i].zri_name, w,
507 (u_longlong_t)zil_rec_info[i].zri_count);
508 (void) printf("\n");
511 void
512 dump_intent_log(zilog_t *zilog)
514 const zil_header_t *zh = zilog->zl_header;
515 int verbose = MAX(dump_opt['d'], dump_opt['i']);
516 int i;
518 if (BP_IS_HOLE(&zh->zh_log) || verbose < 1)
519 return;
521 (void) printf("\n ZIL header: claim_txg %llu, "
522 "claim_blk_seq %llu, claim_lr_seq %llu",
523 (u_longlong_t)zh->zh_claim_txg,
524 (u_longlong_t)zh->zh_claim_blk_seq,
525 (u_longlong_t)zh->zh_claim_lr_seq);
526 (void) printf(" replay_seq %llu, flags 0x%llx\n",
527 (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
529 for (i = 0; i < TX_MAX_TYPE; i++)
530 zil_rec_info[i].zri_count = 0;
532 /* see comment in zil_claim() or zil_check_log_chain() */
533 if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
534 zh->zh_claim_txg == 0)
535 return;
537 if (verbose >= 2) {
538 (void) printf("\n");
539 (void) zil_parse(zilog, print_log_block, print_log_record, NULL,
540 zh->zh_claim_txg, B_FALSE);
541 print_log_stats(verbose);