1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Copyright (c) 2012 Cyril Plisko. All rights reserved.
25 * Use is subject to license terms.
26 */
27
28 /*
29 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
30 */
31
32 /*
33 * Print intent log header and statistics.
34 */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <ctype.h>
39 #include <sys/zfs_context.h>
40 #include <sys/spa.h>
41 #include <sys/dmu.h>
42 #include <sys/stat.h>
43 #include <sys/resource.h>
44 #include <sys/zil.h>
45 #include <sys/zil_impl.h>
46 #include <sys/spa_impl.h>
47 #include <sys/abd.h>
48
49 #include "zdb.h"
50
51 extern uint8_t dump_opt[256];
52
53 static char tab_prefix[4] = "\t\t\t";
54
55 static void
print_log_bp(const blkptr_t * bp,const char * prefix)56 print_log_bp(const blkptr_t *bp, const char *prefix)
57 {
58 char blkbuf[BP_SPRINTF_LEN];
59
60 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
61 (void) printf("%s%s\n", prefix, blkbuf);
62 }
63
64 static void
zil_prt_rec_create(zilog_t * zilog,int txtype,const void * arg)65 zil_prt_rec_create(zilog_t *zilog, int txtype, const void *arg)
66 {
67 (void) zilog;
68 const lr_create_t *lrc = arg;
69 const _lr_create_t *lr = &lrc->lr_create;
70 time_t crtime = lr->lr_crtime[0];
71 const char *name, *link;
72 lr_attr_t *lrattr;
73
74 name = (const char *)&lrc->lr_data[0];
75
76 if (lr->lr_common.lrc_txtype == TX_CREATE_ATTR ||
77 lr->lr_common.lrc_txtype == TX_MKDIR_ATTR) {
78 lrattr = (lr_attr_t *)&lrc->lr_data[0];
79 name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
80 }
81
82 if (txtype == TX_SYMLINK) {
83 link = (const char *)&lrc->lr_data[strlen(name) + 1];
84 (void) printf("%s%s -> %s\n", tab_prefix, name, link);
85 } else if (txtype != TX_MKXATTR) {
86 (void) printf("%s%s\n", tab_prefix, name);
87 }
88
89 (void) printf("%s%s", tab_prefix, ctime(&crtime));
90 (void) printf("%sdoid %llu, foid %llu, slots %llu, mode %llo\n",
91 tab_prefix, (u_longlong_t)lr->lr_doid,
92 (u_longlong_t)LR_FOID_GET_OBJ(lr->lr_foid),
93 (u_longlong_t)LR_FOID_GET_SLOTS(lr->lr_foid),
94 (longlong_t)lr->lr_mode);
95 (void) printf("%suid %llu, gid %llu, gen %llu, rdev 0x%llx\n",
96 tab_prefix,
97 (u_longlong_t)lr->lr_uid, (u_longlong_t)lr->lr_gid,
98 (u_longlong_t)lr->lr_gen, (u_longlong_t)lr->lr_rdev);
99 }
100
101 static void
zil_prt_rec_remove(zilog_t * zilog,int txtype,const void * arg)102 zil_prt_rec_remove(zilog_t *zilog, int txtype, const void *arg)
103 {
104 (void) zilog, (void) txtype;
105 const lr_remove_t *lr = arg;
106
107 (void) printf("%sdoid %llu, name %s\n", tab_prefix,
108 (u_longlong_t)lr->lr_doid, (const char *)&lr->lr_data[0]);
109 }
110
111 static void
zil_prt_rec_link(zilog_t * zilog,int txtype,const void * arg)112 zil_prt_rec_link(zilog_t *zilog, int txtype, const void *arg)
113 {
114 (void) zilog, (void) txtype;
115 const lr_link_t *lr = arg;
116
117 (void) printf("%sdoid %llu, link_obj %llu, name %s\n", tab_prefix,
118 (u_longlong_t)lr->lr_doid, (u_longlong_t)lr->lr_link_obj,
119 (const char *)&lr->lr_data[0]);
120 }
121
122 static void
zil_prt_rec_rename(zilog_t * zilog,int txtype,const void * arg)123 zil_prt_rec_rename(zilog_t *zilog, int txtype, const void *arg)
124 {
125 (void) zilog, (void) txtype;
126 const lr_rename_t *lrr = arg;
127 const _lr_rename_t *lr = &lrr->lr_rename;
128 const char *snm = (const char *)&lrr->lr_data[0];
129 const char *tnm = (const char *)&lrr->lr_data[strlen(snm) + 1];
130
131 (void) printf("%ssdoid %llu, tdoid %llu\n", tab_prefix,
132 (u_longlong_t)lr->lr_sdoid, (u_longlong_t)lr->lr_tdoid);
133 (void) printf("%ssrc %s tgt %s\n", tab_prefix, snm, tnm);
134 switch (txtype) {
135 case TX_RENAME_EXCHANGE:
136 (void) printf("%sflags RENAME_EXCHANGE\n", tab_prefix);
137 break;
138 case TX_RENAME_WHITEOUT:
139 (void) printf("%sflags RENAME_WHITEOUT\n", tab_prefix);
140 break;
141 }
142 }
143
144 static int
zil_prt_rec_write_cb(void * data,size_t len,void * unused)145 zil_prt_rec_write_cb(void *data, size_t len, void *unused)
146 {
147 (void) unused;
148 char *cdata = data;
149
150 for (size_t i = 0; i < len; i++) {
151 if (isprint(*cdata))
152 (void) printf("%c ", *cdata);
153 else
154 (void) printf("%2X", *cdata);
155 cdata++;
156 }
157 return (0);
158 }
159
160 static void
zil_prt_rec_write(zilog_t * zilog,int txtype,const void * arg)161 zil_prt_rec_write(zilog_t *zilog, int txtype, const void *arg)
162 {
163 const lr_write_t *lr = arg;
164 abd_t *data;
165 const blkptr_t *bp = &lr->lr_blkptr;
166 zbookmark_phys_t zb;
167 int verbose = MAX(dump_opt['d'], dump_opt['i']);
168 int error;
169
170 (void) printf("%sfoid %llu, offset %llx, length %llx\n", tab_prefix,
171 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
172 (u_longlong_t)lr->lr_length);
173
174 if (txtype == TX_WRITE2 || verbose < 4)
175 return;
176
177 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
178 (void) printf("%shas blkptr, %s\n", tab_prefix,
179 !BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) >=
180 spa_min_claim_txg(zilog->zl_spa) ?
181 "will claim" : "won't claim");
182 print_log_bp(bp, tab_prefix);
183
184 if (verbose < 5)
185 return;
186 if (BP_IS_HOLE(bp)) {
187 (void) printf("\t\t\tLSIZE 0x%llx\n",
188 (u_longlong_t)BP_GET_LSIZE(bp));
189 (void) printf("%s<hole>\n", tab_prefix);
190 return;
191 }
192 if (BP_GET_LOGICAL_BIRTH(bp) < zilog->zl_header->zh_claim_txg) {
193 (void) printf("%s<block already committed>\n",
194 tab_prefix);
195 return;
196 }
197
198 ASSERT3U(BP_GET_LSIZE(bp), !=, 0);
199 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
200 lr->lr_foid, ZB_ZIL_LEVEL,
201 lr->lr_offset / BP_GET_LSIZE(bp));
202
203 data = abd_alloc(BP_GET_LSIZE(bp), B_FALSE);
204 error = zio_wait(zio_read(NULL, zilog->zl_spa,
205 bp, data, BP_GET_LSIZE(bp), NULL, NULL,
206 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
207 if (error)
208 goto out;
209 } else {
210 if (verbose < 5)
211 return;
212
213 /* data is stored after the end of the lr_write record */
214 data = abd_alloc(lr->lr_length, B_FALSE);
215 abd_copy_from_buf(data, &lr->lr_data[0], lr->lr_length);
216 }
217
218 (void) printf("%s", tab_prefix);
219 (void) abd_iterate_func(data,
220 0, MIN(lr->lr_length, (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE)),
221 zil_prt_rec_write_cb, NULL);
222 (void) printf("\n");
223
224 out:
225 abd_free(data);
226 }
227
228 static void
zil_prt_rec_write_enc(zilog_t * zilog,int txtype,const void * arg)229 zil_prt_rec_write_enc(zilog_t *zilog, int txtype, const void *arg)
230 {
231 (void) txtype;
232 const lr_write_t *lr = arg;
233 const blkptr_t *bp = &lr->lr_blkptr;
234 int verbose = MAX(dump_opt['d'], dump_opt['i']);
235
236 (void) printf("%s(encrypted)\n", tab_prefix);
237
238 if (verbose < 4)
239 return;
240
241 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
242 (void) printf("%shas blkptr, %s\n", tab_prefix,
243 !BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) >=
244 spa_min_claim_txg(zilog->zl_spa) ?
245 "will claim" : "won't claim");
246 print_log_bp(bp, tab_prefix);
247 }
248 }
249
250 static void
zil_prt_rec_truncate(zilog_t * zilog,int txtype,const void * arg)251 zil_prt_rec_truncate(zilog_t *zilog, int txtype, const void *arg)
252 {
253 (void) zilog, (void) txtype;
254 const lr_truncate_t *lr = arg;
255
256 (void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", tab_prefix,
257 (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
258 (u_longlong_t)lr->lr_length);
259 }
260
261 static void
zil_prt_rec_setattr(zilog_t * zilog,int txtype,const void * arg)262 zil_prt_rec_setattr(zilog_t *zilog, int txtype, const void *arg)
263 {
264 (void) zilog, (void) txtype;
265 const lr_setattr_t *lr = arg;
266 time_t atime = (time_t)lr->lr_atime[0];
267 time_t mtime = (time_t)lr->lr_mtime[0];
268
269 (void) printf("%sfoid %llu, mask 0x%llx\n", tab_prefix,
270 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
271
272 if (lr->lr_mask & AT_MODE) {
273 (void) printf("%sAT_MODE %llo\n", tab_prefix,
274 (longlong_t)lr->lr_mode);
275 }
276
277 if (lr->lr_mask & AT_UID) {
278 (void) printf("%sAT_UID %llu\n", tab_prefix,
279 (u_longlong_t)lr->lr_uid);
280 }
281
282 if (lr->lr_mask & AT_GID) {
283 (void) printf("%sAT_GID %llu\n", tab_prefix,
284 (u_longlong_t)lr->lr_gid);
285 }
286
287 if (lr->lr_mask & AT_SIZE) {
288 (void) printf("%sAT_SIZE %llu\n", tab_prefix,
289 (u_longlong_t)lr->lr_size);
290 }
291
292 if (lr->lr_mask & AT_ATIME) {
293 (void) printf("%sAT_ATIME %llu.%09llu %s", tab_prefix,
294 (u_longlong_t)lr->lr_atime[0],
295 (u_longlong_t)lr->lr_atime[1],
296 ctime(&atime));
297 }
298
299 if (lr->lr_mask & AT_MTIME) {
300 (void) printf("%sAT_MTIME %llu.%09llu %s", tab_prefix,
301 (u_longlong_t)lr->lr_mtime[0],
302 (u_longlong_t)lr->lr_mtime[1],
303 ctime(&mtime));
304 }
305 }
306
307 static void
zil_prt_rec_setsaxattr(zilog_t * zilog,int txtype,const void * arg)308 zil_prt_rec_setsaxattr(zilog_t *zilog, int txtype, const void *arg)
309 {
310 (void) zilog, (void) txtype;
311 const lr_setsaxattr_t *lr = arg;
312
313 const char *name = (const char *)&lr->lr_data[0];
314 (void) printf("%sfoid %llu\n", tab_prefix,
315 (u_longlong_t)lr->lr_foid);
316
317 (void) printf("%sXAT_NAME %s\n", tab_prefix, name);
318 if (lr->lr_size == 0) {
319 (void) printf("%sXAT_VALUE NULL\n", tab_prefix);
320 } else {
321 (void) printf("%sXAT_VALUE ", tab_prefix);
322 const char *val = (const char *)&lr->lr_data[strlen(name) + 1];
323 for (int i = 0; i < lr->lr_size; i++) {
324 (void) printf("%c", *val);
325 val++;
326 }
327 }
328 }
329
330 static void
zil_prt_rec_acl(zilog_t * zilog,int txtype,const void * arg)331 zil_prt_rec_acl(zilog_t *zilog, int txtype, const void *arg)
332 {
333 (void) zilog, (void) txtype;
334 const lr_acl_t *lr = arg;
335
336 (void) printf("%sfoid %llu, aclcnt %llu\n", tab_prefix,
337 (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
338 }
339
340 static void
zil_prt_rec_clone_range(zilog_t * zilog,int txtype,const void * arg)341 zil_prt_rec_clone_range(zilog_t *zilog, int txtype, const void *arg)
342 {
343 (void) zilog, (void) txtype;
344 const lr_clone_range_t *lr = arg;
345 int verbose = MAX(dump_opt['d'], dump_opt['i']);
346
347 (void) printf("%sfoid %llu, offset %llx, length %llx, blksize %llx\n",
348 tab_prefix, (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
349 (u_longlong_t)lr->lr_length, (u_longlong_t)lr->lr_blksz);
350
351 if (verbose < 4)
352 return;
353
354 for (unsigned int i = 0; i < lr->lr_nbps; i++) {
355 (void) printf("%s[%u/%llu] ", tab_prefix, i + 1,
356 (u_longlong_t)lr->lr_nbps);
357 print_log_bp(&lr->lr_bps[i], "");
358 }
359 }
360
361 static void
zil_prt_rec_clone_range_enc(zilog_t * zilog,int txtype,const void * arg)362 zil_prt_rec_clone_range_enc(zilog_t *zilog, int txtype, const void *arg)
363 {
364 (void) zilog, (void) txtype;
365 const lr_clone_range_t *lr = arg;
366 int verbose = MAX(dump_opt['d'], dump_opt['i']);
367
368 (void) printf("%s(encrypted)\n", tab_prefix);
369
370 if (verbose < 4)
371 return;
372
373 for (unsigned int i = 0; i < lr->lr_nbps; i++) {
374 (void) printf("%s[%u/%llu] ", tab_prefix, i + 1,
375 (u_longlong_t)lr->lr_nbps);
376 print_log_bp(&lr->lr_bps[i], "");
377 }
378 }
379
380 typedef void (*zil_prt_rec_func_t)(zilog_t *, int, const void *);
381 typedef struct zil_rec_info {
382 zil_prt_rec_func_t zri_print;
383 zil_prt_rec_func_t zri_print_enc;
384 const char *zri_name;
385 uint64_t zri_count;
386 } zil_rec_info_t;
387
388 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
389 {.zri_print = NULL, .zri_name = "Total "},
390 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE "},
391 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR "},
392 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKXATTR "},
393 {.zri_print = zil_prt_rec_create, .zri_name = "TX_SYMLINK "},
394 {.zri_print = zil_prt_rec_remove, .zri_name = "TX_REMOVE "},
395 {.zri_print = zil_prt_rec_remove, .zri_name = "TX_RMDIR "},
396 {.zri_print = zil_prt_rec_link, .zri_name = "TX_LINK "},
397 {.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME "},
398 {.zri_print = zil_prt_rec_write,
399 .zri_print_enc = zil_prt_rec_write_enc,
400 .zri_name = "TX_WRITE "},
401 {.zri_print = zil_prt_rec_truncate, .zri_name = "TX_TRUNCATE "},
402 {.zri_print = zil_prt_rec_setattr, .zri_name = "TX_SETATTR "},
403 {.zri_print = zil_prt_rec_acl, .zri_name = "TX_ACL_V0 "},
404 {.zri_print = zil_prt_rec_acl, .zri_name = "TX_ACL_ACL "},
405 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ACL "},
406 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ATTR "},
407 {.zri_print = zil_prt_rec_create, .zri_name = "TX_CREATE_ACL_ATTR "},
408 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ACL "},
409 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ATTR "},
410 {.zri_print = zil_prt_rec_create, .zri_name = "TX_MKDIR_ACL_ATTR "},
411 {.zri_print = zil_prt_rec_write, .zri_name = "TX_WRITE2 "},
412 {.zri_print = zil_prt_rec_setsaxattr,
413 .zri_name = "TX_SETSAXATTR "},
414 {.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME_EXCHANGE "},
415 {.zri_print = zil_prt_rec_rename, .zri_name = "TX_RENAME_WHITEOUT "},
416 {.zri_print = zil_prt_rec_clone_range,
417 .zri_print_enc = zil_prt_rec_clone_range_enc,
418 .zri_name = "TX_CLONE_RANGE "},
419 };
420
421 static int
print_log_record(zilog_t * zilog,const lr_t * lr,void * arg,uint64_t claim_txg)422 print_log_record(zilog_t *zilog, const lr_t *lr, void *arg, uint64_t claim_txg)
423 {
424 (void) arg, (void) claim_txg;
425 int txtype;
426 int verbose = MAX(dump_opt['d'], dump_opt['i']);
427
428 /* reduce size of txtype to strip off TX_CI bit */
429 txtype = lr->lrc_txtype;
430
431 ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
432 ASSERT(lr->lrc_txg);
433
434 (void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
435 (lr->lrc_txtype & TX_CI) ? "CI-" : "",
436 zil_rec_info[txtype].zri_name,
437 (u_longlong_t)lr->lrc_reclen,
438 (u_longlong_t)lr->lrc_txg,
439 (u_longlong_t)lr->lrc_seq);
440
441 if (txtype && verbose >= 3) {
442 if (!zilog->zl_os->os_encrypted) {
443 zil_rec_info[txtype].zri_print(zilog, txtype, lr);
444 } else if (zil_rec_info[txtype].zri_print_enc) {
445 zil_rec_info[txtype].zri_print_enc(zilog, txtype, lr);
446 } else {
447 (void) printf("%s(encrypted)\n", tab_prefix);
448 }
449 }
450
451 zil_rec_info[txtype].zri_count++;
452 zil_rec_info[0].zri_count++;
453
454 return (0);
455 }
456
457 static int
print_log_block(zilog_t * zilog,const blkptr_t * bp,void * arg,uint64_t claim_txg)458 print_log_block(zilog_t *zilog, const blkptr_t *bp, void *arg,
459 uint64_t claim_txg)
460 {
461 (void) arg;
462 char blkbuf[BP_SPRINTF_LEN + 10];
463 int verbose = MAX(dump_opt['d'], dump_opt['i']);
464 const char *claim;
465
466 if (verbose <= 3)
467 return (0);
468
469 if (verbose >= 5) {
470 (void) strcpy(blkbuf, ", ");
471 snprintf_blkptr(blkbuf + strlen(blkbuf),
472 sizeof (blkbuf) - strlen(blkbuf), bp);
473 } else {
474 blkbuf[0] = '\0';
475 }
476
477 if (claim_txg != 0)
478 claim = "already claimed";
479 else if (BP_GET_LOGICAL_BIRTH(bp) >= spa_min_claim_txg(zilog->zl_spa))
480 claim = "will claim";
481 else
482 claim = "won't claim";
483
484 (void) printf("\tBlock seqno %llu, %s%s\n",
485 (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
486
487 return (0);
488 }
489
490 static void
print_log_stats(int verbose)491 print_log_stats(int verbose)
492 {
493 unsigned i, w, p10;
494
495 if (verbose > 3)
496 (void) printf("\n");
497
498 if (zil_rec_info[0].zri_count == 0)
499 return;
500
501 for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
502 w++;
503
504 for (i = 0; i < TX_MAX_TYPE; i++)
505 if (zil_rec_info[i].zri_count || verbose >= 3)
506 (void) printf("\t\t%s %*llu\n",
507 zil_rec_info[i].zri_name, w,
508 (u_longlong_t)zil_rec_info[i].zri_count);
509 (void) printf("\n");
510 }
511
512 void
dump_intent_log(zilog_t * zilog)513 dump_intent_log(zilog_t *zilog)
514 {
515 const zil_header_t *zh = zilog->zl_header;
516 int verbose = MAX(dump_opt['d'], dump_opt['i']);
517 int i;
518
519 if (BP_IS_HOLE(&zh->zh_log) || verbose < 1)
520 return;
521
522 (void) printf("\n ZIL header: claim_txg %llu, "
523 "claim_blk_seq %llu, claim_lr_seq %llu",
524 (u_longlong_t)zh->zh_claim_txg,
525 (u_longlong_t)zh->zh_claim_blk_seq,
526 (u_longlong_t)zh->zh_claim_lr_seq);
527 (void) printf(" replay_seq %llu, flags 0x%llx\n",
528 (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
529
530 for (i = 0; i < TX_MAX_TYPE; i++)
531 zil_rec_info[i].zri_count = 0;
532
533 /* see comment in zil_claim() or zil_check_log_chain() */
534 if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
535 zh->zh_claim_txg == 0)
536 return;
537
538 if (verbose >= 2) {
539 (void) printf("\n");
540 (void) zil_parse(zilog, print_log_block, print_log_record, NULL,
541 zh->zh_claim_txg, B_FALSE);
542 print_log_stats(verbose);
543 }
544 }
545