xref: /freebsd/sys/contrib/openzfs/cmd/zdb/zdb_il.c (revision 1323ec57)
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, 2017 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/spa_impl.h>
46 #include <sys/abd.h>
47 
48 #include "zdb.h"
49 
50 extern uint8_t dump_opt[256];
51 
52 static char tab_prefix[4] = "\t\t\t";
53 
54 static void
55 print_log_bp(const blkptr_t *bp, const char *prefix)
56 {
57 	char blkbuf[BP_SPRINTF_LEN];
58 
59 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
60 	(void) printf("%s%s\n", prefix, blkbuf);
61 }
62 
63 static void
64 zil_prt_rec_create(zilog_t *zilog, int txtype, const void *arg)
65 {
66 	(void) zilog;
67 	const lr_create_t *lr = arg;
68 	time_t crtime = lr->lr_crtime[0];
69 	char *name, *link;
70 	lr_attr_t *lrattr;
71 
72 	name = (char *)(lr + 1);
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 *)(lr + 1);
77 		name += ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
78 	}
79 
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);
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
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, (char *)(lr + 1));
107 }
108 
109 static void
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 	    (char *)(lr + 1));
118 }
119 
120 static void
121 zil_prt_rec_rename(zilog_t *zilog, int txtype, const void *arg)
122 {
123 	(void) zilog, (void) txtype;
124 	const lr_rename_t *lr = arg;
125 	char *snm = (char *)(lr + 1);
126 	char *tnm = snm + strlen(snm) + 1;
127 
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);
131 }
132 
133 static int
134 zil_prt_rec_write_cb(void *data, size_t len, void *unused)
135 {
136 	(void) unused;
137 	char *cdata = data;
138 
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++;
145 	}
146 	return (0);
147 }
148 
149 static void
150 zil_prt_rec_write(zilog_t *zilog, int txtype, const void *arg)
151 {
152 	const lr_write_t *lr = arg;
153 	abd_t *data;
154 	const blkptr_t *bp = &lr->lr_blkptr;
155 	zbookmark_phys_t zb;
156 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
157 	int error;
158 
159 	(void) printf("%sfoid %llu, offset %llx, length %llx\n", tab_prefix,
160 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset,
161 	    (u_longlong_t)lr->lr_length);
162 
163 	if (txtype == TX_WRITE2 || verbose < 5)
164 		return;
165 
166 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
167 		(void) printf("%shas blkptr, %s\n", tab_prefix,
168 		    !BP_IS_HOLE(bp) &&
169 		    bp->blk_birth >= spa_min_claim_txg(zilog->zl_spa) ?
170 		    "will claim" : "won't claim");
171 		print_log_bp(bp, tab_prefix);
172 
173 		if (BP_IS_HOLE(bp)) {
174 			(void) printf("\t\t\tLSIZE 0x%llx\n",
175 			    (u_longlong_t)BP_GET_LSIZE(bp));
176 			(void) printf("%s<hole>\n", tab_prefix);
177 			return;
178 		}
179 		if (bp->blk_birth < zilog->zl_header->zh_claim_txg) {
180 			(void) printf("%s<block already committed>\n",
181 			    tab_prefix);
182 			return;
183 		}
184 
185 		SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os),
186 		    lr->lr_foid, ZB_ZIL_LEVEL,
187 		    lr->lr_offset / BP_GET_LSIZE(bp));
188 
189 		data = abd_alloc(BP_GET_LSIZE(bp), B_FALSE);
190 		error = zio_wait(zio_read(NULL, zilog->zl_spa,
191 		    bp, data, BP_GET_LSIZE(bp), NULL, NULL,
192 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb));
193 		if (error)
194 			goto out;
195 	} else {
196 		/* data is stored after the end of the lr_write record */
197 		data = abd_alloc(lr->lr_length, B_FALSE);
198 		abd_copy_from_buf(data, lr + 1, lr->lr_length);
199 	}
200 
201 	(void) printf("%s", tab_prefix);
202 	(void) abd_iterate_func(data,
203 	    0, MIN(lr->lr_length, (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE)),
204 	    zil_prt_rec_write_cb, NULL);
205 	(void) printf("\n");
206 
207 out:
208 	abd_free(data);
209 }
210 
211 static void
212 zil_prt_rec_truncate(zilog_t *zilog, int txtype, const void *arg)
213 {
214 	(void) zilog, (void) txtype;
215 	const lr_truncate_t *lr = arg;
216 
217 	(void) printf("%sfoid %llu, offset 0x%llx, length 0x%llx\n", tab_prefix,
218 	    (u_longlong_t)lr->lr_foid, (longlong_t)lr->lr_offset,
219 	    (u_longlong_t)lr->lr_length);
220 }
221 
222 static void
223 zil_prt_rec_setattr(zilog_t *zilog, int txtype, const void *arg)
224 {
225 	(void) zilog, (void) txtype;
226 	const lr_setattr_t *lr = arg;
227 	time_t atime = (time_t)lr->lr_atime[0];
228 	time_t mtime = (time_t)lr->lr_mtime[0];
229 
230 	(void) printf("%sfoid %llu, mask 0x%llx\n", tab_prefix,
231 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_mask);
232 
233 	if (lr->lr_mask & AT_MODE) {
234 		(void) printf("%sAT_MODE  %llo\n", tab_prefix,
235 		    (longlong_t)lr->lr_mode);
236 	}
237 
238 	if (lr->lr_mask & AT_UID) {
239 		(void) printf("%sAT_UID   %llu\n", tab_prefix,
240 		    (u_longlong_t)lr->lr_uid);
241 	}
242 
243 	if (lr->lr_mask & AT_GID) {
244 		(void) printf("%sAT_GID   %llu\n", tab_prefix,
245 		    (u_longlong_t)lr->lr_gid);
246 	}
247 
248 	if (lr->lr_mask & AT_SIZE) {
249 		(void) printf("%sAT_SIZE  %llu\n", tab_prefix,
250 		    (u_longlong_t)lr->lr_size);
251 	}
252 
253 	if (lr->lr_mask & AT_ATIME) {
254 		(void) printf("%sAT_ATIME %llu.%09llu %s", tab_prefix,
255 		    (u_longlong_t)lr->lr_atime[0],
256 		    (u_longlong_t)lr->lr_atime[1],
257 		    ctime(&atime));
258 	}
259 
260 	if (lr->lr_mask & AT_MTIME) {
261 		(void) printf("%sAT_MTIME %llu.%09llu %s", tab_prefix,
262 		    (u_longlong_t)lr->lr_mtime[0],
263 		    (u_longlong_t)lr->lr_mtime[1],
264 		    ctime(&mtime));
265 	}
266 }
267 
268 static void
269 zil_prt_rec_setsaxattr(zilog_t *zilog, int txtype, const void *arg)
270 {
271 	(void) zilog, (void) txtype;
272 	const lr_setsaxattr_t *lr = arg;
273 
274 	char *name = (char *)(lr + 1);
275 	(void) printf("%sfoid %llu\n", tab_prefix,
276 	    (u_longlong_t)lr->lr_foid);
277 
278 	(void) printf("%sXAT_NAME  %s\n", tab_prefix, name);
279 	if (lr->lr_size == 0) {
280 		(void) printf("%sXAT_VALUE  NULL\n", tab_prefix);
281 	} else {
282 		(void) printf("%sXAT_VALUE  ", tab_prefix);
283 		char *val = name + (strlen(name) + 1);
284 		for (int i = 0; i < lr->lr_size; i++) {
285 			(void) printf("%c", *val);
286 			val++;
287 		}
288 	}
289 }
290 
291 static void
292 zil_prt_rec_acl(zilog_t *zilog, int txtype, const void *arg)
293 {
294 	(void) zilog, (void) txtype;
295 	const lr_acl_t *lr = arg;
296 
297 	(void) printf("%sfoid %llu, aclcnt %llu\n", tab_prefix,
298 	    (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_aclcnt);
299 }
300 
301 typedef void (*zil_prt_rec_func_t)(zilog_t *, int, const void *);
302 typedef struct zil_rec_info {
303 	zil_prt_rec_func_t	zri_print;
304 	const char		*zri_name;
305 	uint64_t		zri_count;
306 } zil_rec_info_t;
307 
308 static zil_rec_info_t zil_rec_info[TX_MAX_TYPE] = {
309 	{.zri_print = NULL,		    .zri_name = "Total              "},
310 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_CREATE          "},
311 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKDIR           "},
312 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKXATTR         "},
313 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_SYMLINK         "},
314 	{.zri_print = zil_prt_rec_remove,   .zri_name = "TX_REMOVE          "},
315 	{.zri_print = zil_prt_rec_remove,   .zri_name = "TX_RMDIR           "},
316 	{.zri_print = zil_prt_rec_link,	    .zri_name = "TX_LINK            "},
317 	{.zri_print = zil_prt_rec_rename,   .zri_name = "TX_RENAME          "},
318 	{.zri_print = zil_prt_rec_write,    .zri_name = "TX_WRITE           "},
319 	{.zri_print = zil_prt_rec_truncate, .zri_name = "TX_TRUNCATE        "},
320 	{.zri_print = zil_prt_rec_setattr,  .zri_name = "TX_SETATTR         "},
321 	{.zri_print = zil_prt_rec_acl,	    .zri_name = "TX_ACL_V0          "},
322 	{.zri_print = zil_prt_rec_acl,	    .zri_name = "TX_ACL_ACL         "},
323 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_CREATE_ACL      "},
324 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_CREATE_ATTR     "},
325 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_CREATE_ACL_ATTR "},
326 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKDIR_ACL       "},
327 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKDIR_ATTR      "},
328 	{.zri_print = zil_prt_rec_create,   .zri_name = "TX_MKDIR_ACL_ATTR  "},
329 	{.zri_print = zil_prt_rec_write,    .zri_name = "TX_WRITE2          "},
330 	{.zri_print = zil_prt_rec_setsaxattr,
331 	    .zri_name = "TX_SETSAXATTR      "},
332 };
333 
334 static int
335 print_log_record(zilog_t *zilog, const lr_t *lr, void *arg, uint64_t claim_txg)
336 {
337 	(void) arg, (void) claim_txg;
338 	int txtype;
339 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
340 
341 	/* reduce size of txtype to strip off TX_CI bit */
342 	txtype = lr->lrc_txtype;
343 
344 	ASSERT(txtype != 0 && (uint_t)txtype < TX_MAX_TYPE);
345 	ASSERT(lr->lrc_txg);
346 
347 	(void) printf("\t\t%s%s len %6llu, txg %llu, seq %llu\n",
348 	    (lr->lrc_txtype & TX_CI) ? "CI-" : "",
349 	    zil_rec_info[txtype].zri_name,
350 	    (u_longlong_t)lr->lrc_reclen,
351 	    (u_longlong_t)lr->lrc_txg,
352 	    (u_longlong_t)lr->lrc_seq);
353 
354 	if (txtype && verbose >= 3) {
355 		if (!zilog->zl_os->os_encrypted) {
356 			zil_rec_info[txtype].zri_print(zilog, txtype, lr);
357 		} else {
358 			(void) printf("%s(encrypted)\n", tab_prefix);
359 		}
360 	}
361 
362 	zil_rec_info[txtype].zri_count++;
363 	zil_rec_info[0].zri_count++;
364 
365 	return (0);
366 }
367 
368 static int
369 print_log_block(zilog_t *zilog, const blkptr_t *bp, void *arg,
370     uint64_t claim_txg)
371 {
372 	(void) arg;
373 	char blkbuf[BP_SPRINTF_LEN + 10];
374 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
375 	const char *claim;
376 
377 	if (verbose <= 3)
378 		return (0);
379 
380 	if (verbose >= 5) {
381 		(void) strcpy(blkbuf, ", ");
382 		snprintf_blkptr(blkbuf + strlen(blkbuf),
383 		    sizeof (blkbuf) - strlen(blkbuf), bp);
384 	} else {
385 		blkbuf[0] = '\0';
386 	}
387 
388 	if (claim_txg != 0)
389 		claim = "already claimed";
390 	else if (bp->blk_birth >= spa_min_claim_txg(zilog->zl_spa))
391 		claim = "will claim";
392 	else
393 		claim = "won't claim";
394 
395 	(void) printf("\tBlock seqno %llu, %s%s\n",
396 	    (u_longlong_t)bp->blk_cksum.zc_word[ZIL_ZC_SEQ], claim, blkbuf);
397 
398 	return (0);
399 }
400 
401 static void
402 print_log_stats(int verbose)
403 {
404 	unsigned i, w, p10;
405 
406 	if (verbose > 3)
407 		(void) printf("\n");
408 
409 	if (zil_rec_info[0].zri_count == 0)
410 		return;
411 
412 	for (w = 1, p10 = 10; zil_rec_info[0].zri_count >= p10; p10 *= 10)
413 		w++;
414 
415 	for (i = 0; i < TX_MAX_TYPE; i++)
416 		if (zil_rec_info[i].zri_count || verbose >= 3)
417 			(void) printf("\t\t%s %*llu\n",
418 			    zil_rec_info[i].zri_name, w,
419 			    (u_longlong_t)zil_rec_info[i].zri_count);
420 	(void) printf("\n");
421 }
422 
423 void
424 dump_intent_log(zilog_t *zilog)
425 {
426 	const zil_header_t *zh = zilog->zl_header;
427 	int verbose = MAX(dump_opt['d'], dump_opt['i']);
428 	int i;
429 
430 	if (BP_IS_HOLE(&zh->zh_log) || verbose < 1)
431 		return;
432 
433 	(void) printf("\n    ZIL header: claim_txg %llu, "
434 	    "claim_blk_seq %llu, claim_lr_seq %llu",
435 	    (u_longlong_t)zh->zh_claim_txg,
436 	    (u_longlong_t)zh->zh_claim_blk_seq,
437 	    (u_longlong_t)zh->zh_claim_lr_seq);
438 	(void) printf(" replay_seq %llu, flags 0x%llx\n",
439 	    (u_longlong_t)zh->zh_replay_seq, (u_longlong_t)zh->zh_flags);
440 
441 	for (i = 0; i < TX_MAX_TYPE; i++)
442 		zil_rec_info[i].zri_count = 0;
443 
444 	/* see comment in zil_claim() or zil_check_log_chain() */
445 	if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
446 	    zh->zh_claim_txg == 0)
447 		return;
448 
449 	if (verbose >= 2) {
450 		(void) printf("\n");
451 		(void) zil_parse(zilog, print_log_block, print_log_record, NULL,
452 		    zh->zh_claim_txg, B_FALSE);
453 		print_log_stats(verbose);
454 	}
455 }
456