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