Print this page
4171 clean up spa_feature_*() interfaces
4172 implement extensible_dataset feature for use by other zpool features
Reviewed by: Max Grossman <max.grossman@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/zdb/zdb.c
+++ new/usr/src/cmd/zdb/zdb.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 25 */
26 26
27 27 #include <stdio.h>
28 28 #include <unistd.h>
29 29 #include <stdio_ext.h>
30 30 #include <stdlib.h>
31 31 #include <ctype.h>
32 32 #include <sys/zfs_context.h>
33 33 #include <sys/spa.h>
34 34 #include <sys/spa_impl.h>
35 35 #include <sys/dmu.h>
36 36 #include <sys/zap.h>
37 37 #include <sys/fs/zfs.h>
38 38 #include <sys/zfs_znode.h>
39 39 #include <sys/zfs_sa.h>
40 40 #include <sys/sa.h>
41 41 #include <sys/sa_impl.h>
42 42 #include <sys/vdev.h>
43 43 #include <sys/vdev_impl.h>
44 44 #include <sys/metaslab_impl.h>
45 45 #include <sys/dmu_objset.h>
46 46 #include <sys/dsl_dir.h>
47 47 #include <sys/dsl_dataset.h>
48 48 #include <sys/dsl_pool.h>
49 49 #include <sys/dbuf.h>
50 50 #include <sys/zil.h>
51 51 #include <sys/zil_impl.h>
52 52 #include <sys/stat.h>
53 53 #include <sys/resource.h>
54 54 #include <sys/dmu_traverse.h>
55 55 #include <sys/zio_checksum.h>
56 56 #include <sys/zio_compress.h>
57 57 #include <sys/zfs_fuid.h>
58 58 #include <sys/arc.h>
59 59 #include <sys/ddt.h>
60 60 #include <sys/zfeature.h>
61 61 #include <zfs_comutil.h>
62 62 #undef ZFS_MAXNAMELEN
63 63 #undef verify
64 64 #include <libzfs.h>
65 65
66 66 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
67 67 zio_compress_table[(idx)].ci_name : "UNKNOWN")
68 68 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
69 69 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70 70 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
71 71 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \
72 72 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73 73 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
74 74 (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \
75 75 DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
76 76
77 77 #ifndef lint
78 78 extern int zfs_recover;
79 79 #else
80 80 int zfs_recover;
81 81 #endif
82 82
83 83 const char cmdname[] = "zdb";
84 84 uint8_t dump_opt[256];
85 85
86 86 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
87 87
88 88 extern void dump_intent_log(zilog_t *);
89 89 uint64_t *zopt_object = NULL;
90 90 int zopt_objects = 0;
91 91 libzfs_handle_t *g_zfs;
92 92 uint64_t max_inflight = 200;
93 93
94 94 /*
95 95 * These libumem hooks provide a reasonable set of defaults for the allocator's
96 96 * debugging facilities.
97 97 */
98 98 const char *
99 99 _umem_debug_init()
100 100 {
101 101 return ("default,verbose"); /* $UMEM_DEBUG setting */
102 102 }
103 103
104 104 const char *
105 105 _umem_logging_init(void)
106 106 {
107 107 return ("fail,contents"); /* $UMEM_LOGGING setting */
108 108 }
109 109
110 110 static void
111 111 usage(void)
112 112 {
113 113 (void) fprintf(stderr,
114 114 "Usage: %s [-CumdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
115 115 "[-U config] [-M inflight I/Os] poolname [object...]\n"
116 116 " %s [-divPA] [-e -p path...] [-U config] dataset "
117 117 "[object...]\n"
118 118 " %s -m [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
119 119 "poolname [vdev [metaslab...]]\n"
120 120 " %s -R [-A] [-e [-p path...]] poolname "
121 121 "vdev:offset:size[:flags]\n"
122 122 " %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
123 123 " %s -l [-uA] device\n"
124 124 " %s -C [-A] [-U config]\n\n",
125 125 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
126 126
127 127 (void) fprintf(stderr, " Dataset name must include at least one "
128 128 "separator character '/' or '@'\n");
129 129 (void) fprintf(stderr, " If dataset name is specified, only that "
130 130 "dataset is dumped\n");
131 131 (void) fprintf(stderr, " If object numbers are specified, only "
132 132 "those objects are dumped\n\n");
133 133 (void) fprintf(stderr, " Options to control amount of output:\n");
134 134 (void) fprintf(stderr, " -u uberblock\n");
135 135 (void) fprintf(stderr, " -d dataset(s)\n");
136 136 (void) fprintf(stderr, " -i intent logs\n");
137 137 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
138 138 (void) fprintf(stderr, " -h pool history\n");
139 139 (void) fprintf(stderr, " -b block statistics\n");
140 140 (void) fprintf(stderr, " -m metaslabs\n");
141 141 (void) fprintf(stderr, " -c checksum all metadata (twice for "
142 142 "all data) blocks\n");
143 143 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
144 144 (void) fprintf(stderr, " -D dedup statistics\n");
145 145 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
146 146 (void) fprintf(stderr, " -v verbose (applies to all others)\n");
147 147 (void) fprintf(stderr, " -l dump label contents\n");
148 148 (void) fprintf(stderr, " -L disable leak tracking (do not "
149 149 "load spacemaps)\n");
150 150 (void) fprintf(stderr, " -R read and display block from a "
151 151 "device\n\n");
152 152 (void) fprintf(stderr, " Below options are intended for use "
153 153 "with other options (except -l):\n");
154 154 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
155 155 "panic recovery (-AA) or both (-AAA)\n");
156 156 (void) fprintf(stderr, " -F attempt automatic rewind within "
157 157 "safe range of transaction groups\n");
158 158 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
159 159 "cachefile\n");
160 160 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
161 161 "work with dataset)\n");
162 162 (void) fprintf(stderr, " -e pool is exported/destroyed/"
163 163 "has altroot/not in a cachefile\n");
164 164 (void) fprintf(stderr, " -p <path> -- use one or more with "
165 165 "-e to specify path to vdev dir\n");
166 166 (void) fprintf(stderr, " -P print numbers in parseable form\n");
167 167 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
168 168 "searching for uberblocks\n");
169 169 (void) fprintf(stderr, " -M <number of inflight I/Os> -- "
170 170 "specify the maximum number of checksumming I/Os [default is 200]");
171 171 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
172 172 "to make only that option verbose\n");
173 173 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
174 174 exit(1);
175 175 }
176 176
177 177 /*
178 178 * Called for usage errors that are discovered after a call to spa_open(),
179 179 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
180 180 */
181 181
182 182 static void
183 183 fatal(const char *fmt, ...)
184 184 {
185 185 va_list ap;
186 186
187 187 va_start(ap, fmt);
188 188 (void) fprintf(stderr, "%s: ", cmdname);
189 189 (void) vfprintf(stderr, fmt, ap);
190 190 va_end(ap);
191 191 (void) fprintf(stderr, "\n");
192 192
193 193 exit(1);
194 194 }
195 195
196 196 /* ARGSUSED */
197 197 static void
198 198 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
199 199 {
200 200 nvlist_t *nv;
201 201 size_t nvsize = *(uint64_t *)data;
202 202 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
203 203
204 204 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
205 205
206 206 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
207 207
208 208 umem_free(packed, nvsize);
209 209
210 210 dump_nvlist(nv, 8);
211 211
212 212 nvlist_free(nv);
213 213 }
214 214
215 215 /* ARGSUSED */
216 216 static void
217 217 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
218 218 {
219 219 spa_history_phys_t *shp = data;
220 220
221 221 if (shp == NULL)
222 222 return;
223 223
224 224 (void) printf("\t\tpool_create_len = %llu\n",
225 225 (u_longlong_t)shp->sh_pool_create_len);
226 226 (void) printf("\t\tphys_max_off = %llu\n",
227 227 (u_longlong_t)shp->sh_phys_max_off);
228 228 (void) printf("\t\tbof = %llu\n",
229 229 (u_longlong_t)shp->sh_bof);
230 230 (void) printf("\t\teof = %llu\n",
231 231 (u_longlong_t)shp->sh_eof);
232 232 (void) printf("\t\trecords_lost = %llu\n",
233 233 (u_longlong_t)shp->sh_records_lost);
234 234 }
235 235
236 236 static void
237 237 zdb_nicenum(uint64_t num, char *buf)
238 238 {
239 239 if (dump_opt['P'])
240 240 (void) sprintf(buf, "%llu", (longlong_t)num);
241 241 else
242 242 nicenum(num, buf);
243 243 }
244 244
245 245 const char histo_stars[] = "****************************************";
246 246 const int histo_width = sizeof (histo_stars) - 1;
247 247
248 248 static void
249 249 dump_histogram(const uint64_t *histo, int size, int offset)
250 250 {
251 251 int i;
252 252 int minidx = size - 1;
253 253 int maxidx = 0;
254 254 uint64_t max = 0;
255 255
256 256 for (i = 0; i < size; i++) {
257 257 if (histo[i] > max)
258 258 max = histo[i];
259 259 if (histo[i] > 0 && i > maxidx)
260 260 maxidx = i;
261 261 if (histo[i] > 0 && i < minidx)
262 262 minidx = i;
263 263 }
264 264
265 265 if (max < histo_width)
266 266 max = histo_width;
267 267
268 268 for (i = minidx; i <= maxidx; i++) {
269 269 (void) printf("\t\t\t%3u: %6llu %s\n",
270 270 i + offset, (u_longlong_t)histo[i],
271 271 &histo_stars[(max - histo[i]) * histo_width / max]);
272 272 }
273 273 }
274 274
275 275 static void
276 276 dump_zap_stats(objset_t *os, uint64_t object)
277 277 {
278 278 int error;
279 279 zap_stats_t zs;
280 280
281 281 error = zap_get_stats(os, object, &zs);
282 282 if (error)
283 283 return;
284 284
285 285 if (zs.zs_ptrtbl_len == 0) {
286 286 ASSERT(zs.zs_num_blocks == 1);
287 287 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
288 288 (u_longlong_t)zs.zs_blocksize,
289 289 (u_longlong_t)zs.zs_num_entries);
290 290 return;
291 291 }
292 292
293 293 (void) printf("\tFat ZAP stats:\n");
294 294
295 295 (void) printf("\t\tPointer table:\n");
296 296 (void) printf("\t\t\t%llu elements\n",
297 297 (u_longlong_t)zs.zs_ptrtbl_len);
298 298 (void) printf("\t\t\tzt_blk: %llu\n",
299 299 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
300 300 (void) printf("\t\t\tzt_numblks: %llu\n",
301 301 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
302 302 (void) printf("\t\t\tzt_shift: %llu\n",
303 303 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
304 304 (void) printf("\t\t\tzt_blks_copied: %llu\n",
305 305 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
306 306 (void) printf("\t\t\tzt_nextblk: %llu\n",
307 307 (u_longlong_t)zs.zs_ptrtbl_nextblk);
308 308
309 309 (void) printf("\t\tZAP entries: %llu\n",
310 310 (u_longlong_t)zs.zs_num_entries);
311 311 (void) printf("\t\tLeaf blocks: %llu\n",
312 312 (u_longlong_t)zs.zs_num_leafs);
313 313 (void) printf("\t\tTotal blocks: %llu\n",
314 314 (u_longlong_t)zs.zs_num_blocks);
315 315 (void) printf("\t\tzap_block_type: 0x%llx\n",
316 316 (u_longlong_t)zs.zs_block_type);
317 317 (void) printf("\t\tzap_magic: 0x%llx\n",
318 318 (u_longlong_t)zs.zs_magic);
319 319 (void) printf("\t\tzap_salt: 0x%llx\n",
320 320 (u_longlong_t)zs.zs_salt);
321 321
322 322 (void) printf("\t\tLeafs with 2^n pointers:\n");
323 323 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
324 324
325 325 (void) printf("\t\tBlocks with n*5 entries:\n");
326 326 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
327 327
328 328 (void) printf("\t\tBlocks n/10 full:\n");
329 329 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
330 330
331 331 (void) printf("\t\tEntries with n chunks:\n");
332 332 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
333 333
334 334 (void) printf("\t\tBuckets with n entries:\n");
335 335 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
336 336 }
337 337
338 338 /*ARGSUSED*/
339 339 static void
340 340 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
341 341 {
342 342 }
343 343
344 344 /*ARGSUSED*/
345 345 static void
346 346 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
347 347 {
348 348 (void) printf("\tUNKNOWN OBJECT TYPE\n");
349 349 }
350 350
351 351 /*ARGSUSED*/
352 352 void
353 353 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
354 354 {
355 355 }
356 356
357 357 /*ARGSUSED*/
358 358 static void
359 359 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
360 360 {
361 361 }
362 362
363 363 /*ARGSUSED*/
364 364 static void
365 365 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
366 366 {
367 367 zap_cursor_t zc;
368 368 zap_attribute_t attr;
369 369 void *prop;
370 370 int i;
371 371
372 372 dump_zap_stats(os, object);
373 373 (void) printf("\n");
374 374
375 375 for (zap_cursor_init(&zc, os, object);
376 376 zap_cursor_retrieve(&zc, &attr) == 0;
377 377 zap_cursor_advance(&zc)) {
378 378 (void) printf("\t\t%s = ", attr.za_name);
379 379 if (attr.za_num_integers == 0) {
380 380 (void) printf("\n");
381 381 continue;
382 382 }
383 383 prop = umem_zalloc(attr.za_num_integers *
384 384 attr.za_integer_length, UMEM_NOFAIL);
385 385 (void) zap_lookup(os, object, attr.za_name,
386 386 attr.za_integer_length, attr.za_num_integers, prop);
387 387 if (attr.za_integer_length == 1) {
388 388 (void) printf("%s", (char *)prop);
389 389 } else {
390 390 for (i = 0; i < attr.za_num_integers; i++) {
391 391 switch (attr.za_integer_length) {
392 392 case 2:
393 393 (void) printf("%u ",
394 394 ((uint16_t *)prop)[i]);
395 395 break;
396 396 case 4:
397 397 (void) printf("%u ",
398 398 ((uint32_t *)prop)[i]);
399 399 break;
400 400 case 8:
401 401 (void) printf("%lld ",
402 402 (u_longlong_t)((int64_t *)prop)[i]);
403 403 break;
404 404 }
405 405 }
406 406 }
407 407 (void) printf("\n");
408 408 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
409 409 }
410 410 zap_cursor_fini(&zc);
411 411 }
412 412
413 413 /*ARGSUSED*/
414 414 static void
415 415 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
416 416 {
417 417 dump_zap_stats(os, object);
418 418 /* contents are printed elsewhere, properly decoded */
419 419 }
420 420
421 421 /*ARGSUSED*/
422 422 static void
423 423 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
424 424 {
425 425 zap_cursor_t zc;
426 426 zap_attribute_t attr;
427 427
428 428 dump_zap_stats(os, object);
429 429 (void) printf("\n");
430 430
431 431 for (zap_cursor_init(&zc, os, object);
432 432 zap_cursor_retrieve(&zc, &attr) == 0;
433 433 zap_cursor_advance(&zc)) {
434 434 (void) printf("\t\t%s = ", attr.za_name);
435 435 if (attr.za_num_integers == 0) {
436 436 (void) printf("\n");
437 437 continue;
438 438 }
439 439 (void) printf(" %llx : [%d:%d:%d]\n",
440 440 (u_longlong_t)attr.za_first_integer,
441 441 (int)ATTR_LENGTH(attr.za_first_integer),
442 442 (int)ATTR_BSWAP(attr.za_first_integer),
443 443 (int)ATTR_NUM(attr.za_first_integer));
444 444 }
445 445 zap_cursor_fini(&zc);
446 446 }
447 447
448 448 /*ARGSUSED*/
449 449 static void
450 450 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
451 451 {
452 452 zap_cursor_t zc;
453 453 zap_attribute_t attr;
454 454 uint16_t *layout_attrs;
455 455 int i;
456 456
457 457 dump_zap_stats(os, object);
458 458 (void) printf("\n");
459 459
460 460 for (zap_cursor_init(&zc, os, object);
461 461 zap_cursor_retrieve(&zc, &attr) == 0;
462 462 zap_cursor_advance(&zc)) {
463 463 (void) printf("\t\t%s = [", attr.za_name);
464 464 if (attr.za_num_integers == 0) {
465 465 (void) printf("\n");
466 466 continue;
467 467 }
468 468
469 469 VERIFY(attr.za_integer_length == 2);
470 470 layout_attrs = umem_zalloc(attr.za_num_integers *
471 471 attr.za_integer_length, UMEM_NOFAIL);
472 472
473 473 VERIFY(zap_lookup(os, object, attr.za_name,
474 474 attr.za_integer_length,
475 475 attr.za_num_integers, layout_attrs) == 0);
476 476
477 477 for (i = 0; i != attr.za_num_integers; i++)
478 478 (void) printf(" %d ", (int)layout_attrs[i]);
479 479 (void) printf("]\n");
480 480 umem_free(layout_attrs,
481 481 attr.za_num_integers * attr.za_integer_length);
482 482 }
483 483 zap_cursor_fini(&zc);
484 484 }
485 485
486 486 /*ARGSUSED*/
487 487 static void
488 488 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
489 489 {
490 490 zap_cursor_t zc;
491 491 zap_attribute_t attr;
492 492 const char *typenames[] = {
493 493 /* 0 */ "not specified",
494 494 /* 1 */ "FIFO",
495 495 /* 2 */ "Character Device",
496 496 /* 3 */ "3 (invalid)",
497 497 /* 4 */ "Directory",
498 498 /* 5 */ "5 (invalid)",
499 499 /* 6 */ "Block Device",
500 500 /* 7 */ "7 (invalid)",
501 501 /* 8 */ "Regular File",
502 502 /* 9 */ "9 (invalid)",
503 503 /* 10 */ "Symbolic Link",
504 504 /* 11 */ "11 (invalid)",
505 505 /* 12 */ "Socket",
506 506 /* 13 */ "Door",
507 507 /* 14 */ "Event Port",
508 508 /* 15 */ "15 (invalid)",
509 509 };
510 510
511 511 dump_zap_stats(os, object);
512 512 (void) printf("\n");
513 513
514 514 for (zap_cursor_init(&zc, os, object);
515 515 zap_cursor_retrieve(&zc, &attr) == 0;
516 516 zap_cursor_advance(&zc)) {
517 517 (void) printf("\t\t%s = %lld (type: %s)\n",
518 518 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
519 519 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
520 520 }
521 521 zap_cursor_fini(&zc);
522 522 }
523 523
524 524 int
525 525 get_dtl_refcount(vdev_t *vd)
526 526 {
527 527 int refcount = 0;
528 528
529 529 if (vd->vdev_ops->vdev_op_leaf) {
530 530 space_map_t *sm = vd->vdev_dtl_sm;
531 531
532 532 if (sm != NULL &&
533 533 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
534 534 return (1);
535 535 return (0);
536 536 }
537 537
538 538 for (int c = 0; c < vd->vdev_children; c++)
539 539 refcount += get_dtl_refcount(vd->vdev_child[c]);
540 540 return (refcount);
541 541 }
542 542
543 543 int
544 544 get_metaslab_refcount(vdev_t *vd)
545 545 {
546 546 int refcount = 0;
547 547
548 548 if (vd->vdev_top == vd) {
549 549 for (int m = 0; m < vd->vdev_ms_count; m++) {
550 550 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
551 551
552 552 if (sm != NULL &&
553 553 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
554 554 refcount++;
555 555 }
↓ open down ↓ |
555 lines elided |
↑ open up ↑ |
556 556 }
557 557 for (int c = 0; c < vd->vdev_children; c++)
558 558 refcount += get_metaslab_refcount(vd->vdev_child[c]);
559 559
560 560 return (refcount);
561 561 }
562 562
563 563 static int
564 564 verify_spacemap_refcounts(spa_t *spa)
565 565 {
566 - int expected_refcount, actual_refcount;
566 + uint64_t expected_refcount = 0;
567 + uint64_t actual_refcount;
567 568
568 - expected_refcount = spa_feature_get_refcount(spa,
569 - &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM]);
569 + (void) feature_get_refcount(spa,
570 + &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
571 + &expected_refcount);
570 572 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
571 573 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
572 574
573 575 if (expected_refcount != actual_refcount) {
574 - (void) printf("space map refcount mismatch: expected %d != "
575 - "actual %d\n", expected_refcount, actual_refcount);
576 + (void) printf("space map refcount mismatch: expected %lld != "
577 + "actual %lld\n",
578 + (longlong_t)expected_refcount,
579 + (longlong_t)actual_refcount);
576 580 return (2);
577 581 }
578 582 return (0);
579 583 }
580 584
581 585 static void
582 586 dump_spacemap(objset_t *os, space_map_t *sm)
583 587 {
584 588 uint64_t alloc, offset, entry;
585 589 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
586 590 "INVALID", "INVALID", "INVALID", "INVALID" };
587 591
588 592 if (sm == NULL)
589 593 return;
590 594
591 595 /*
592 596 * Print out the freelist entries in both encoded and decoded form.
593 597 */
594 598 alloc = 0;
595 599 for (offset = 0; offset < space_map_length(sm);
596 600 offset += sizeof (entry)) {
597 601 uint8_t mapshift = sm->sm_shift;
598 602
599 603 VERIFY0(dmu_read(os, space_map_object(sm), offset,
600 604 sizeof (entry), &entry, DMU_READ_PREFETCH));
601 605 if (SM_DEBUG_DECODE(entry)) {
602 606
603 607 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
604 608 (u_longlong_t)(offset / sizeof (entry)),
605 609 ddata[SM_DEBUG_ACTION_DECODE(entry)],
606 610 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
607 611 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
608 612 } else {
609 613 (void) printf("\t [%6llu] %c range:"
610 614 " %010llx-%010llx size: %06llx\n",
611 615 (u_longlong_t)(offset / sizeof (entry)),
612 616 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
613 617 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
614 618 mapshift) + sm->sm_start),
615 619 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
616 620 mapshift) + sm->sm_start +
617 621 (SM_RUN_DECODE(entry) << mapshift)),
618 622 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
619 623 if (SM_TYPE_DECODE(entry) == SM_ALLOC)
620 624 alloc += SM_RUN_DECODE(entry) << mapshift;
621 625 else
622 626 alloc -= SM_RUN_DECODE(entry) << mapshift;
623 627 }
624 628 }
625 629 if (alloc != space_map_allocated(sm)) {
626 630 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
627 631 "with space map summary (%llu)\n",
628 632 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
629 633 }
630 634 }
631 635
632 636 static void
633 637 dump_metaslab_stats(metaslab_t *msp)
634 638 {
635 639 char maxbuf[32];
636 640 range_tree_t *rt = msp->ms_tree;
637 641 avl_tree_t *t = &msp->ms_size_tree;
638 642 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
639 643
640 644 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
641 645
642 646 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
643 647 "segments", avl_numnodes(t), "maxsize", maxbuf,
644 648 "freepct", free_pct);
645 649 (void) printf("\tIn-memory histogram:\n");
646 650 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
647 651 }
648 652
649 653 static void
650 654 dump_metaslab(metaslab_t *msp)
651 655 {
652 656 vdev_t *vd = msp->ms_group->mg_vd;
653 657 spa_t *spa = vd->vdev_spa;
654 658 space_map_t *sm = msp->ms_sm;
655 659 char freebuf[32];
656 660
657 661 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
658 662
659 663 (void) printf(
660 664 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
661 665 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
662 666 (u_longlong_t)space_map_object(sm), freebuf);
663 667
664 668 if (dump_opt['m'] > 2 && !dump_opt['L']) {
665 669 mutex_enter(&msp->ms_lock);
666 670 metaslab_load_wait(msp);
↓ open down ↓ |
81 lines elided |
↑ open up ↑ |
667 671 if (!msp->ms_loaded) {
668 672 VERIFY0(metaslab_load(msp));
669 673 range_tree_stat_verify(msp->ms_tree);
670 674 }
671 675 dump_metaslab_stats(msp);
672 676 metaslab_unload(msp);
673 677 mutex_exit(&msp->ms_lock);
674 678 }
675 679
676 680 if (dump_opt['m'] > 1 && sm != NULL &&
677 - spa_feature_is_active(spa,
678 - &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM])) {
681 + spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
679 682 /*
680 683 * The space map histogram represents free space in chunks
681 684 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
682 685 */
683 686 (void) printf("\tOn-disk histogram:\n");
684 687 dump_histogram(sm->sm_phys->smp_histogram,
685 688 SPACE_MAP_HISTOGRAM_SIZE(sm), sm->sm_shift);
686 689 }
687 690
688 691 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
689 692 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
690 693
691 694 mutex_enter(&msp->ms_lock);
692 695 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
693 696 mutex_exit(&msp->ms_lock);
694 697 }
695 698 }
696 699
697 700 static void
698 701 print_vdev_metaslab_header(vdev_t *vd)
699 702 {
700 703 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
701 704 (u_longlong_t)vd->vdev_id,
702 705 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
703 706 "offset", "spacemap", "free");
704 707 (void) printf("\t%15s %19s %15s %10s\n",
705 708 "---------------", "-------------------",
706 709 "---------------", "-------------");
707 710 }
708 711
709 712 static void
710 713 dump_metaslabs(spa_t *spa)
711 714 {
712 715 vdev_t *vd, *rvd = spa->spa_root_vdev;
713 716 uint64_t m, c = 0, children = rvd->vdev_children;
714 717
715 718 (void) printf("\nMetaslabs:\n");
716 719
717 720 if (!dump_opt['d'] && zopt_objects > 0) {
718 721 c = zopt_object[0];
719 722
720 723 if (c >= children)
721 724 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
722 725
723 726 if (zopt_objects > 1) {
724 727 vd = rvd->vdev_child[c];
725 728 print_vdev_metaslab_header(vd);
726 729
727 730 for (m = 1; m < zopt_objects; m++) {
728 731 if (zopt_object[m] < vd->vdev_ms_count)
729 732 dump_metaslab(
730 733 vd->vdev_ms[zopt_object[m]]);
731 734 else
732 735 (void) fprintf(stderr, "bad metaslab "
733 736 "number %llu\n",
734 737 (u_longlong_t)zopt_object[m]);
735 738 }
736 739 (void) printf("\n");
737 740 return;
738 741 }
739 742 children = c + 1;
740 743 }
741 744 for (; c < children; c++) {
742 745 vd = rvd->vdev_child[c];
743 746 print_vdev_metaslab_header(vd);
744 747
745 748 for (m = 0; m < vd->vdev_ms_count; m++)
746 749 dump_metaslab(vd->vdev_ms[m]);
747 750 (void) printf("\n");
748 751 }
749 752 }
750 753
751 754 static void
752 755 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
753 756 {
754 757 const ddt_phys_t *ddp = dde->dde_phys;
755 758 const ddt_key_t *ddk = &dde->dde_key;
756 759 char *types[4] = { "ditto", "single", "double", "triple" };
757 760 char blkbuf[BP_SPRINTF_LEN];
758 761 blkptr_t blk;
759 762
760 763 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
761 764 if (ddp->ddp_phys_birth == 0)
762 765 continue;
763 766 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
764 767 sprintf_blkptr(blkbuf, &blk);
765 768 (void) printf("index %llx refcnt %llu %s %s\n",
766 769 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
767 770 types[p], blkbuf);
768 771 }
769 772 }
770 773
771 774 static void
772 775 dump_dedup_ratio(const ddt_stat_t *dds)
773 776 {
774 777 double rL, rP, rD, D, dedup, compress, copies;
775 778
776 779 if (dds->dds_blocks == 0)
777 780 return;
778 781
779 782 rL = (double)dds->dds_ref_lsize;
780 783 rP = (double)dds->dds_ref_psize;
781 784 rD = (double)dds->dds_ref_dsize;
782 785 D = (double)dds->dds_dsize;
783 786
784 787 dedup = rD / D;
785 788 compress = rL / rP;
786 789 copies = rD / rP;
787 790
788 791 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
789 792 "dedup * compress / copies = %.2f\n\n",
790 793 dedup, compress, copies, dedup * compress / copies);
791 794 }
792 795
793 796 static void
794 797 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
795 798 {
796 799 char name[DDT_NAMELEN];
797 800 ddt_entry_t dde;
798 801 uint64_t walk = 0;
799 802 dmu_object_info_t doi;
800 803 uint64_t count, dspace, mspace;
801 804 int error;
802 805
803 806 error = ddt_object_info(ddt, type, class, &doi);
804 807
805 808 if (error == ENOENT)
806 809 return;
807 810 ASSERT(error == 0);
808 811
809 812 if ((count = ddt_object_count(ddt, type, class)) == 0)
810 813 return;
811 814
812 815 dspace = doi.doi_physical_blocks_512 << 9;
813 816 mspace = doi.doi_fill_count * doi.doi_data_block_size;
814 817
815 818 ddt_object_name(ddt, type, class, name);
816 819
817 820 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
818 821 name,
819 822 (u_longlong_t)count,
820 823 (u_longlong_t)(dspace / count),
821 824 (u_longlong_t)(mspace / count));
822 825
823 826 if (dump_opt['D'] < 3)
824 827 return;
825 828
826 829 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
827 830
828 831 if (dump_opt['D'] < 4)
829 832 return;
830 833
831 834 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
832 835 return;
833 836
834 837 (void) printf("%s contents:\n\n", name);
835 838
836 839 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
837 840 dump_dde(ddt, &dde, walk);
838 841
839 842 ASSERT(error == ENOENT);
840 843
841 844 (void) printf("\n");
842 845 }
843 846
844 847 static void
845 848 dump_all_ddts(spa_t *spa)
846 849 {
847 850 ddt_histogram_t ddh_total = { 0 };
848 851 ddt_stat_t dds_total = { 0 };
849 852
850 853 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
851 854 ddt_t *ddt = spa->spa_ddt[c];
852 855 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
853 856 for (enum ddt_class class = 0; class < DDT_CLASSES;
854 857 class++) {
855 858 dump_ddt(ddt, type, class);
856 859 }
857 860 }
858 861 }
859 862
860 863 ddt_get_dedup_stats(spa, &dds_total);
861 864
862 865 if (dds_total.dds_blocks == 0) {
863 866 (void) printf("All DDTs are empty\n");
864 867 return;
865 868 }
866 869
867 870 (void) printf("\n");
868 871
869 872 if (dump_opt['D'] > 1) {
870 873 (void) printf("DDT histogram (aggregated over all DDTs):\n");
871 874 ddt_get_dedup_histogram(spa, &ddh_total);
872 875 zpool_dump_ddt(&dds_total, &ddh_total);
873 876 }
874 877
875 878 dump_dedup_ratio(&dds_total);
876 879 }
877 880
878 881 static void
879 882 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
880 883 {
881 884 char *prefix = arg;
882 885
883 886 (void) printf("%s [%llu,%llu) length %llu\n",
884 887 prefix,
885 888 (u_longlong_t)start,
886 889 (u_longlong_t)(start + size),
887 890 (u_longlong_t)(size));
888 891 }
889 892
890 893 static void
891 894 dump_dtl(vdev_t *vd, int indent)
892 895 {
893 896 spa_t *spa = vd->vdev_spa;
894 897 boolean_t required;
895 898 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
896 899 char prefix[256];
897 900
898 901 spa_vdev_state_enter(spa, SCL_NONE);
899 902 required = vdev_dtl_required(vd);
900 903 (void) spa_vdev_state_exit(spa, NULL, 0);
901 904
902 905 if (indent == 0)
903 906 (void) printf("\nDirty time logs:\n\n");
904 907
905 908 (void) printf("\t%*s%s [%s]\n", indent, "",
906 909 vd->vdev_path ? vd->vdev_path :
907 910 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
908 911 required ? "DTL-required" : "DTL-expendable");
909 912
910 913 for (int t = 0; t < DTL_TYPES; t++) {
911 914 range_tree_t *rt = vd->vdev_dtl[t];
912 915 if (range_tree_space(rt) == 0)
913 916 continue;
914 917 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
915 918 indent + 2, "", name[t]);
916 919 mutex_enter(rt->rt_lock);
917 920 range_tree_walk(rt, dump_dtl_seg, prefix);
918 921 mutex_exit(rt->rt_lock);
919 922 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
920 923 dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
921 924 }
922 925
923 926 for (int c = 0; c < vd->vdev_children; c++)
924 927 dump_dtl(vd->vdev_child[c], indent + 4);
925 928 }
926 929
927 930 static void
928 931 dump_history(spa_t *spa)
929 932 {
930 933 nvlist_t **events = NULL;
931 934 char buf[SPA_MAXBLOCKSIZE];
932 935 uint64_t resid, len, off = 0;
933 936 uint_t num = 0;
934 937 int error;
935 938 time_t tsec;
936 939 struct tm t;
937 940 char tbuf[30];
938 941 char internalstr[MAXPATHLEN];
939 942
940 943 do {
941 944 len = sizeof (buf);
942 945
943 946 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
944 947 (void) fprintf(stderr, "Unable to read history: "
945 948 "error %d\n", error);
946 949 return;
947 950 }
948 951
949 952 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
950 953 break;
951 954
952 955 off -= resid;
953 956 } while (len != 0);
954 957
955 958 (void) printf("\nHistory:\n");
956 959 for (int i = 0; i < num; i++) {
957 960 uint64_t time, txg, ievent;
958 961 char *cmd, *intstr;
959 962 boolean_t printed = B_FALSE;
960 963
961 964 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
962 965 &time) != 0)
963 966 goto next;
964 967 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
965 968 &cmd) != 0) {
966 969 if (nvlist_lookup_uint64(events[i],
967 970 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
968 971 goto next;
969 972 verify(nvlist_lookup_uint64(events[i],
970 973 ZPOOL_HIST_TXG, &txg) == 0);
971 974 verify(nvlist_lookup_string(events[i],
972 975 ZPOOL_HIST_INT_STR, &intstr) == 0);
973 976 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
974 977 goto next;
975 978
976 979 (void) snprintf(internalstr,
977 980 sizeof (internalstr),
978 981 "[internal %s txg:%lld] %s",
979 982 zfs_history_event_names[ievent], txg,
980 983 intstr);
981 984 cmd = internalstr;
982 985 }
983 986 tsec = time;
984 987 (void) localtime_r(&tsec, &t);
985 988 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
986 989 (void) printf("%s %s\n", tbuf, cmd);
987 990 printed = B_TRUE;
988 991
989 992 next:
990 993 if (dump_opt['h'] > 1) {
991 994 if (!printed)
992 995 (void) printf("unrecognized record:\n");
993 996 dump_nvlist(events[i], 2);
994 997 }
995 998 }
996 999 }
997 1000
998 1001 /*ARGSUSED*/
999 1002 static void
1000 1003 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1001 1004 {
1002 1005 }
1003 1006
1004 1007 static uint64_t
1005 1008 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb)
1006 1009 {
1007 1010 if (dnp == NULL) {
1008 1011 ASSERT(zb->zb_level < 0);
1009 1012 if (zb->zb_object == 0)
1010 1013 return (zb->zb_blkid);
1011 1014 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1012 1015 }
1013 1016
1014 1017 ASSERT(zb->zb_level >= 0);
1015 1018
1016 1019 return ((zb->zb_blkid <<
1017 1020 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1018 1021 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1019 1022 }
1020 1023
1021 1024 static void
1022 1025 sprintf_blkptr_compact(char *blkbuf, const blkptr_t *bp)
1023 1026 {
1024 1027 const dva_t *dva = bp->blk_dva;
1025 1028 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1026 1029
1027 1030 if (dump_opt['b'] >= 6) {
1028 1031 sprintf_blkptr(blkbuf, bp);
1029 1032 return;
1030 1033 }
1031 1034
1032 1035 blkbuf[0] = '\0';
1033 1036
1034 1037 for (int i = 0; i < ndvas; i++)
1035 1038 (void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ",
1036 1039 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1037 1040 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1038 1041 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1039 1042
1040 1043 (void) sprintf(blkbuf + strlen(blkbuf),
1041 1044 "%llxL/%llxP F=%llu B=%llu/%llu",
1042 1045 (u_longlong_t)BP_GET_LSIZE(bp),
1043 1046 (u_longlong_t)BP_GET_PSIZE(bp),
1044 1047 (u_longlong_t)bp->blk_fill,
1045 1048 (u_longlong_t)bp->blk_birth,
1046 1049 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1047 1050 }
1048 1051
1049 1052 static void
1050 1053 print_indirect(blkptr_t *bp, const zbookmark_t *zb,
1051 1054 const dnode_phys_t *dnp)
1052 1055 {
1053 1056 char blkbuf[BP_SPRINTF_LEN];
1054 1057 int l;
1055 1058
1056 1059 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1057 1060 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1058 1061
1059 1062 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1060 1063
1061 1064 ASSERT(zb->zb_level >= 0);
1062 1065
1063 1066 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1064 1067 if (l == zb->zb_level) {
1065 1068 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1066 1069 } else {
1067 1070 (void) printf(" ");
1068 1071 }
1069 1072 }
1070 1073
1071 1074 sprintf_blkptr_compact(blkbuf, bp);
1072 1075 (void) printf("%s\n", blkbuf);
1073 1076 }
1074 1077
1075 1078 static int
1076 1079 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1077 1080 blkptr_t *bp, const zbookmark_t *zb)
1078 1081 {
1079 1082 int err = 0;
1080 1083
1081 1084 if (bp->blk_birth == 0)
1082 1085 return (0);
1083 1086
1084 1087 print_indirect(bp, zb, dnp);
1085 1088
1086 1089 if (BP_GET_LEVEL(bp) > 0) {
1087 1090 uint32_t flags = ARC_WAIT;
1088 1091 int i;
1089 1092 blkptr_t *cbp;
1090 1093 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1091 1094 arc_buf_t *buf;
1092 1095 uint64_t fill = 0;
1093 1096
1094 1097 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1095 1098 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1096 1099 if (err)
1097 1100 return (err);
1098 1101 ASSERT(buf->b_data);
1099 1102
1100 1103 /* recursively visit blocks below this */
1101 1104 cbp = buf->b_data;
1102 1105 for (i = 0; i < epb; i++, cbp++) {
1103 1106 zbookmark_t czb;
1104 1107
1105 1108 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1106 1109 zb->zb_level - 1,
1107 1110 zb->zb_blkid * epb + i);
1108 1111 err = visit_indirect(spa, dnp, cbp, &czb);
1109 1112 if (err)
1110 1113 break;
1111 1114 fill += cbp->blk_fill;
1112 1115 }
1113 1116 if (!err)
1114 1117 ASSERT3U(fill, ==, bp->blk_fill);
1115 1118 (void) arc_buf_remove_ref(buf, &buf);
1116 1119 }
1117 1120
1118 1121 return (err);
1119 1122 }
1120 1123
1121 1124 /*ARGSUSED*/
1122 1125 static void
1123 1126 dump_indirect(dnode_t *dn)
1124 1127 {
1125 1128 dnode_phys_t *dnp = dn->dn_phys;
1126 1129 int j;
1127 1130 zbookmark_t czb;
1128 1131
1129 1132 (void) printf("Indirect blocks:\n");
1130 1133
1131 1134 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1132 1135 dn->dn_object, dnp->dn_nlevels - 1, 0);
1133 1136 for (j = 0; j < dnp->dn_nblkptr; j++) {
1134 1137 czb.zb_blkid = j;
1135 1138 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1136 1139 &dnp->dn_blkptr[j], &czb);
1137 1140 }
1138 1141
1139 1142 (void) printf("\n");
1140 1143 }
1141 1144
1142 1145 /*ARGSUSED*/
1143 1146 static void
1144 1147 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1145 1148 {
1146 1149 dsl_dir_phys_t *dd = data;
1147 1150 time_t crtime;
1148 1151 char nice[32];
1149 1152
1150 1153 if (dd == NULL)
1151 1154 return;
1152 1155
1153 1156 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1154 1157
1155 1158 crtime = dd->dd_creation_time;
1156 1159 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1157 1160 (void) printf("\t\thead_dataset_obj = %llu\n",
1158 1161 (u_longlong_t)dd->dd_head_dataset_obj);
1159 1162 (void) printf("\t\tparent_dir_obj = %llu\n",
1160 1163 (u_longlong_t)dd->dd_parent_obj);
1161 1164 (void) printf("\t\torigin_obj = %llu\n",
1162 1165 (u_longlong_t)dd->dd_origin_obj);
1163 1166 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1164 1167 (u_longlong_t)dd->dd_child_dir_zapobj);
1165 1168 zdb_nicenum(dd->dd_used_bytes, nice);
1166 1169 (void) printf("\t\tused_bytes = %s\n", nice);
1167 1170 zdb_nicenum(dd->dd_compressed_bytes, nice);
1168 1171 (void) printf("\t\tcompressed_bytes = %s\n", nice);
1169 1172 zdb_nicenum(dd->dd_uncompressed_bytes, nice);
1170 1173 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
1171 1174 zdb_nicenum(dd->dd_quota, nice);
1172 1175 (void) printf("\t\tquota = %s\n", nice);
1173 1176 zdb_nicenum(dd->dd_reserved, nice);
1174 1177 (void) printf("\t\treserved = %s\n", nice);
1175 1178 (void) printf("\t\tprops_zapobj = %llu\n",
1176 1179 (u_longlong_t)dd->dd_props_zapobj);
1177 1180 (void) printf("\t\tdeleg_zapobj = %llu\n",
1178 1181 (u_longlong_t)dd->dd_deleg_zapobj);
1179 1182 (void) printf("\t\tflags = %llx\n",
1180 1183 (u_longlong_t)dd->dd_flags);
1181 1184
1182 1185 #define DO(which) \
1183 1186 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
1184 1187 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1185 1188 DO(HEAD);
1186 1189 DO(SNAP);
1187 1190 DO(CHILD);
1188 1191 DO(CHILD_RSRV);
1189 1192 DO(REFRSRV);
1190 1193 #undef DO
1191 1194 }
1192 1195
1193 1196 /*ARGSUSED*/
1194 1197 static void
1195 1198 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1196 1199 {
1197 1200 dsl_dataset_phys_t *ds = data;
1198 1201 time_t crtime;
1199 1202 char used[32], compressed[32], uncompressed[32], unique[32];
1200 1203 char blkbuf[BP_SPRINTF_LEN];
1201 1204
1202 1205 if (ds == NULL)
1203 1206 return;
1204 1207
1205 1208 ASSERT(size == sizeof (*ds));
1206 1209 crtime = ds->ds_creation_time;
1207 1210 zdb_nicenum(ds->ds_referenced_bytes, used);
1208 1211 zdb_nicenum(ds->ds_compressed_bytes, compressed);
1209 1212 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
1210 1213 zdb_nicenum(ds->ds_unique_bytes, unique);
1211 1214 sprintf_blkptr(blkbuf, &ds->ds_bp);
1212 1215
1213 1216 (void) printf("\t\tdir_obj = %llu\n",
1214 1217 (u_longlong_t)ds->ds_dir_obj);
1215 1218 (void) printf("\t\tprev_snap_obj = %llu\n",
1216 1219 (u_longlong_t)ds->ds_prev_snap_obj);
1217 1220 (void) printf("\t\tprev_snap_txg = %llu\n",
1218 1221 (u_longlong_t)ds->ds_prev_snap_txg);
1219 1222 (void) printf("\t\tnext_snap_obj = %llu\n",
1220 1223 (u_longlong_t)ds->ds_next_snap_obj);
1221 1224 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1222 1225 (u_longlong_t)ds->ds_snapnames_zapobj);
1223 1226 (void) printf("\t\tnum_children = %llu\n",
1224 1227 (u_longlong_t)ds->ds_num_children);
1225 1228 (void) printf("\t\tuserrefs_obj = %llu\n",
1226 1229 (u_longlong_t)ds->ds_userrefs_obj);
1227 1230 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1228 1231 (void) printf("\t\tcreation_txg = %llu\n",
1229 1232 (u_longlong_t)ds->ds_creation_txg);
1230 1233 (void) printf("\t\tdeadlist_obj = %llu\n",
1231 1234 (u_longlong_t)ds->ds_deadlist_obj);
1232 1235 (void) printf("\t\tused_bytes = %s\n", used);
1233 1236 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1234 1237 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1235 1238 (void) printf("\t\tunique = %s\n", unique);
1236 1239 (void) printf("\t\tfsid_guid = %llu\n",
1237 1240 (u_longlong_t)ds->ds_fsid_guid);
1238 1241 (void) printf("\t\tguid = %llu\n",
1239 1242 (u_longlong_t)ds->ds_guid);
1240 1243 (void) printf("\t\tflags = %llx\n",
1241 1244 (u_longlong_t)ds->ds_flags);
1242 1245 (void) printf("\t\tnext_clones_obj = %llu\n",
1243 1246 (u_longlong_t)ds->ds_next_clones_obj);
1244 1247 (void) printf("\t\tprops_obj = %llu\n",
1245 1248 (u_longlong_t)ds->ds_props_obj);
1246 1249 (void) printf("\t\tbp = %s\n", blkbuf);
1247 1250 }
1248 1251
1249 1252 /* ARGSUSED */
1250 1253 static int
1251 1254 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1252 1255 {
1253 1256 char blkbuf[BP_SPRINTF_LEN];
1254 1257
1255 1258 if (bp->blk_birth != 0) {
1256 1259 sprintf_blkptr(blkbuf, bp);
1257 1260 (void) printf("\t%s\n", blkbuf);
1258 1261 }
1259 1262 return (0);
1260 1263 }
1261 1264
1262 1265 static void
1263 1266 dump_bptree(objset_t *os, uint64_t obj, char *name)
1264 1267 {
1265 1268 char bytes[32];
1266 1269 bptree_phys_t *bt;
1267 1270 dmu_buf_t *db;
1268 1271
1269 1272 if (dump_opt['d'] < 3)
1270 1273 return;
1271 1274
1272 1275 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1273 1276 bt = db->db_data;
1274 1277 zdb_nicenum(bt->bt_bytes, bytes);
1275 1278 (void) printf("\n %s: %llu datasets, %s\n",
1276 1279 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1277 1280 dmu_buf_rele(db, FTAG);
1278 1281
1279 1282 if (dump_opt['d'] < 5)
1280 1283 return;
1281 1284
1282 1285 (void) printf("\n");
1283 1286
1284 1287 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1285 1288 }
1286 1289
1287 1290 /* ARGSUSED */
1288 1291 static int
1289 1292 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1290 1293 {
1291 1294 char blkbuf[BP_SPRINTF_LEN];
1292 1295
1293 1296 ASSERT(bp->blk_birth != 0);
1294 1297 sprintf_blkptr_compact(blkbuf, bp);
1295 1298 (void) printf("\t%s\n", blkbuf);
1296 1299 return (0);
1297 1300 }
1298 1301
1299 1302 static void
1300 1303 dump_bpobj(bpobj_t *bpo, char *name, int indent)
1301 1304 {
1302 1305 char bytes[32];
1303 1306 char comp[32];
1304 1307 char uncomp[32];
1305 1308
1306 1309 if (dump_opt['d'] < 3)
1307 1310 return;
1308 1311
1309 1312 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1310 1313 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1311 1314 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1312 1315 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1313 1316 (void) printf(" %*s: object %llu, %llu local blkptrs, "
1314 1317 "%llu subobjs, %s (%s/%s comp)\n",
1315 1318 indent * 8, name,
1316 1319 (u_longlong_t)bpo->bpo_object,
1317 1320 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1318 1321 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1319 1322 bytes, comp, uncomp);
1320 1323
1321 1324 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1322 1325 uint64_t subobj;
1323 1326 bpobj_t subbpo;
1324 1327 int error;
1325 1328 VERIFY0(dmu_read(bpo->bpo_os,
1326 1329 bpo->bpo_phys->bpo_subobjs,
1327 1330 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1328 1331 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1329 1332 if (error != 0) {
1330 1333 (void) printf("ERROR %u while trying to open "
1331 1334 "subobj id %llu\n",
1332 1335 error, (u_longlong_t)subobj);
1333 1336 continue;
1334 1337 }
1335 1338 dump_bpobj(&subbpo, "subobj", indent + 1);
1336 1339 bpobj_close(&subbpo);
1337 1340 }
1338 1341 } else {
1339 1342 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1340 1343 indent * 8, name,
1341 1344 (u_longlong_t)bpo->bpo_object,
1342 1345 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1343 1346 bytes);
1344 1347 }
1345 1348
1346 1349 if (dump_opt['d'] < 5)
1347 1350 return;
1348 1351
1349 1352
1350 1353 if (indent == 0) {
1351 1354 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1352 1355 (void) printf("\n");
1353 1356 }
1354 1357 }
1355 1358
1356 1359 static void
1357 1360 dump_deadlist(dsl_deadlist_t *dl)
1358 1361 {
1359 1362 dsl_deadlist_entry_t *dle;
1360 1363 uint64_t unused;
1361 1364 char bytes[32];
1362 1365 char comp[32];
1363 1366 char uncomp[32];
1364 1367
1365 1368 if (dump_opt['d'] < 3)
1366 1369 return;
1367 1370
1368 1371 zdb_nicenum(dl->dl_phys->dl_used, bytes);
1369 1372 zdb_nicenum(dl->dl_phys->dl_comp, comp);
1370 1373 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1371 1374 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1372 1375 bytes, comp, uncomp);
1373 1376
1374 1377 if (dump_opt['d'] < 4)
1375 1378 return;
1376 1379
1377 1380 (void) printf("\n");
1378 1381
1379 1382 /* force the tree to be loaded */
1380 1383 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1381 1384
1382 1385 for (dle = avl_first(&dl->dl_tree); dle;
1383 1386 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1384 1387 if (dump_opt['d'] >= 5) {
1385 1388 char buf[128];
1386 1389 (void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1387 1390 (longlong_t)dle->dle_mintxg,
1388 1391 (longlong_t)dle->dle_bpobj.bpo_object);
1389 1392
1390 1393 dump_bpobj(&dle->dle_bpobj, buf, 0);
1391 1394 } else {
1392 1395 (void) printf("mintxg %llu -> obj %llu\n",
1393 1396 (longlong_t)dle->dle_mintxg,
1394 1397 (longlong_t)dle->dle_bpobj.bpo_object);
1395 1398
1396 1399 }
1397 1400 }
1398 1401 }
1399 1402
1400 1403 static avl_tree_t idx_tree;
1401 1404 static avl_tree_t domain_tree;
1402 1405 static boolean_t fuid_table_loaded;
1403 1406 static boolean_t sa_loaded;
1404 1407 sa_attr_type_t *sa_attr_table;
1405 1408
1406 1409 static void
1407 1410 fuid_table_destroy()
1408 1411 {
1409 1412 if (fuid_table_loaded) {
1410 1413 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1411 1414 fuid_table_loaded = B_FALSE;
1412 1415 }
1413 1416 }
1414 1417
1415 1418 /*
1416 1419 * print uid or gid information.
1417 1420 * For normal POSIX id just the id is printed in decimal format.
1418 1421 * For CIFS files with FUID the fuid is printed in hex followed by
1419 1422 * the domain-rid string.
1420 1423 */
1421 1424 static void
1422 1425 print_idstr(uint64_t id, const char *id_type)
1423 1426 {
1424 1427 if (FUID_INDEX(id)) {
1425 1428 char *domain;
1426 1429
1427 1430 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1428 1431 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1429 1432 (u_longlong_t)id, domain, (int)FUID_RID(id));
1430 1433 } else {
1431 1434 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1432 1435 }
1433 1436
1434 1437 }
1435 1438
1436 1439 static void
1437 1440 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1438 1441 {
1439 1442 uint32_t uid_idx, gid_idx;
1440 1443
1441 1444 uid_idx = FUID_INDEX(uid);
1442 1445 gid_idx = FUID_INDEX(gid);
1443 1446
1444 1447 /* Load domain table, if not already loaded */
1445 1448 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1446 1449 uint64_t fuid_obj;
1447 1450
1448 1451 /* first find the fuid object. It lives in the master node */
1449 1452 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1450 1453 8, 1, &fuid_obj) == 0);
1451 1454 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1452 1455 (void) zfs_fuid_table_load(os, fuid_obj,
1453 1456 &idx_tree, &domain_tree);
1454 1457 fuid_table_loaded = B_TRUE;
1455 1458 }
1456 1459
1457 1460 print_idstr(uid, "uid");
1458 1461 print_idstr(gid, "gid");
1459 1462 }
1460 1463
1461 1464 /*ARGSUSED*/
1462 1465 static void
1463 1466 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1464 1467 {
1465 1468 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
1466 1469 sa_handle_t *hdl;
1467 1470 uint64_t xattr, rdev, gen;
1468 1471 uint64_t uid, gid, mode, fsize, parent, links;
1469 1472 uint64_t pflags;
1470 1473 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1471 1474 time_t z_crtime, z_atime, z_mtime, z_ctime;
1472 1475 sa_bulk_attr_t bulk[12];
1473 1476 int idx = 0;
1474 1477 int error;
1475 1478
1476 1479 if (!sa_loaded) {
1477 1480 uint64_t sa_attrs = 0;
1478 1481 uint64_t version;
1479 1482
1480 1483 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1481 1484 8, 1, &version) == 0);
1482 1485 if (version >= ZPL_VERSION_SA) {
1483 1486 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1484 1487 8, 1, &sa_attrs) == 0);
1485 1488 }
1486 1489 if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
1487 1490 ZPL_END, &sa_attr_table)) != 0) {
1488 1491 (void) printf("sa_setup failed errno %d, can't "
1489 1492 "display znode contents\n", error);
1490 1493 return;
1491 1494 }
1492 1495 sa_loaded = B_TRUE;
1493 1496 }
1494 1497
1495 1498 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1496 1499 (void) printf("Failed to get handle for SA znode\n");
1497 1500 return;
1498 1501 }
1499 1502
1500 1503 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1501 1504 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1502 1505 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1503 1506 &links, 8);
1504 1507 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1505 1508 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1506 1509 &mode, 8);
1507 1510 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1508 1511 NULL, &parent, 8);
1509 1512 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1510 1513 &fsize, 8);
1511 1514 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1512 1515 acctm, 16);
1513 1516 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1514 1517 modtm, 16);
1515 1518 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1516 1519 crtm, 16);
1517 1520 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1518 1521 chgtm, 16);
1519 1522 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1520 1523 &pflags, 8);
1521 1524
1522 1525 if (sa_bulk_lookup(hdl, bulk, idx)) {
1523 1526 (void) sa_handle_destroy(hdl);
1524 1527 return;
1525 1528 }
1526 1529
1527 1530 error = zfs_obj_to_path(os, object, path, sizeof (path));
1528 1531 if (error != 0) {
1529 1532 (void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
1530 1533 (u_longlong_t)object);
1531 1534 }
1532 1535 if (dump_opt['d'] < 3) {
1533 1536 (void) printf("\t%s\n", path);
1534 1537 (void) sa_handle_destroy(hdl);
1535 1538 return;
1536 1539 }
1537 1540
1538 1541 z_crtime = (time_t)crtm[0];
1539 1542 z_atime = (time_t)acctm[0];
1540 1543 z_mtime = (time_t)modtm[0];
1541 1544 z_ctime = (time_t)chgtm[0];
1542 1545
1543 1546 (void) printf("\tpath %s\n", path);
1544 1547 dump_uidgid(os, uid, gid);
1545 1548 (void) printf("\tatime %s", ctime(&z_atime));
1546 1549 (void) printf("\tmtime %s", ctime(&z_mtime));
1547 1550 (void) printf("\tctime %s", ctime(&z_ctime));
1548 1551 (void) printf("\tcrtime %s", ctime(&z_crtime));
1549 1552 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
1550 1553 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
1551 1554 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
1552 1555 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
1553 1556 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
1554 1557 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
1555 1558 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1556 1559 sizeof (uint64_t)) == 0)
1557 1560 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
1558 1561 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1559 1562 sizeof (uint64_t)) == 0)
1560 1563 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
1561 1564 sa_handle_destroy(hdl);
1562 1565 }
1563 1566
1564 1567 /*ARGSUSED*/
1565 1568 static void
1566 1569 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1567 1570 {
1568 1571 }
1569 1572
1570 1573 /*ARGSUSED*/
1571 1574 static void
1572 1575 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1573 1576 {
1574 1577 }
1575 1578
1576 1579 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1577 1580 dump_none, /* unallocated */
1578 1581 dump_zap, /* object directory */
1579 1582 dump_uint64, /* object array */
1580 1583 dump_none, /* packed nvlist */
1581 1584 dump_packed_nvlist, /* packed nvlist size */
1582 1585 dump_none, /* bplist */
1583 1586 dump_none, /* bplist header */
1584 1587 dump_none, /* SPA space map header */
1585 1588 dump_none, /* SPA space map */
1586 1589 dump_none, /* ZIL intent log */
1587 1590 dump_dnode, /* DMU dnode */
1588 1591 dump_dmu_objset, /* DMU objset */
1589 1592 dump_dsl_dir, /* DSL directory */
1590 1593 dump_zap, /* DSL directory child map */
1591 1594 dump_zap, /* DSL dataset snap map */
1592 1595 dump_zap, /* DSL props */
1593 1596 dump_dsl_dataset, /* DSL dataset */
1594 1597 dump_znode, /* ZFS znode */
1595 1598 dump_acl, /* ZFS V0 ACL */
1596 1599 dump_uint8, /* ZFS plain file */
1597 1600 dump_zpldir, /* ZFS directory */
1598 1601 dump_zap, /* ZFS master node */
1599 1602 dump_zap, /* ZFS delete queue */
1600 1603 dump_uint8, /* zvol object */
1601 1604 dump_zap, /* zvol prop */
1602 1605 dump_uint8, /* other uint8[] */
1603 1606 dump_uint64, /* other uint64[] */
1604 1607 dump_zap, /* other ZAP */
1605 1608 dump_zap, /* persistent error log */
1606 1609 dump_uint8, /* SPA history */
1607 1610 dump_history_offsets, /* SPA history offsets */
1608 1611 dump_zap, /* Pool properties */
1609 1612 dump_zap, /* DSL permissions */
1610 1613 dump_acl, /* ZFS ACL */
1611 1614 dump_uint8, /* ZFS SYSACL */
1612 1615 dump_none, /* FUID nvlist */
1613 1616 dump_packed_nvlist, /* FUID nvlist size */
1614 1617 dump_zap, /* DSL dataset next clones */
1615 1618 dump_zap, /* DSL scrub queue */
1616 1619 dump_zap, /* ZFS user/group used */
1617 1620 dump_zap, /* ZFS user/group quota */
1618 1621 dump_zap, /* snapshot refcount tags */
1619 1622 dump_ddt_zap, /* DDT ZAP object */
1620 1623 dump_zap, /* DDT statistics */
1621 1624 dump_znode, /* SA object */
1622 1625 dump_zap, /* SA Master Node */
1623 1626 dump_sa_attrs, /* SA attribute registration */
1624 1627 dump_sa_layouts, /* SA attribute layouts */
1625 1628 dump_zap, /* DSL scrub translations */
1626 1629 dump_none, /* fake dedup BP */
1627 1630 dump_zap, /* deadlist */
1628 1631 dump_none, /* deadlist hdr */
1629 1632 dump_zap, /* dsl clones */
1630 1633 dump_none, /* bpobj subobjs */
1631 1634 dump_unknown, /* Unknown type, must be last */
1632 1635 };
1633 1636
1634 1637 static void
1635 1638 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1636 1639 {
1637 1640 dmu_buf_t *db = NULL;
1638 1641 dmu_object_info_t doi;
1639 1642 dnode_t *dn;
1640 1643 void *bonus = NULL;
1641 1644 size_t bsize = 0;
1642 1645 char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
1643 1646 char bonus_size[32];
1644 1647 char aux[50];
1645 1648 int error;
1646 1649
1647 1650 if (*print_header) {
1648 1651 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n",
1649 1652 "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1650 1653 "%full", "type");
1651 1654 *print_header = 0;
1652 1655 }
1653 1656
1654 1657 if (object == 0) {
1655 1658 dn = DMU_META_DNODE(os);
1656 1659 } else {
1657 1660 error = dmu_bonus_hold(os, object, FTAG, &db);
1658 1661 if (error)
1659 1662 fatal("dmu_bonus_hold(%llu) failed, errno %u",
1660 1663 object, error);
1661 1664 bonus = db->db_data;
1662 1665 bsize = db->db_size;
1663 1666 dn = DB_DNODE((dmu_buf_impl_t *)db);
1664 1667 }
1665 1668 dmu_object_info_from_dnode(dn, &doi);
1666 1669
1667 1670 zdb_nicenum(doi.doi_metadata_block_size, iblk);
1668 1671 zdb_nicenum(doi.doi_data_block_size, dblk);
1669 1672 zdb_nicenum(doi.doi_max_offset, lsize);
1670 1673 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
1671 1674 zdb_nicenum(doi.doi_bonus_size, bonus_size);
1672 1675 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1673 1676 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1674 1677 doi.doi_max_offset);
1675 1678
1676 1679 aux[0] = '\0';
1677 1680
1678 1681 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1679 1682 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
1680 1683 ZDB_CHECKSUM_NAME(doi.doi_checksum));
1681 1684 }
1682 1685
1683 1686 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1684 1687 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
1685 1688 ZDB_COMPRESS_NAME(doi.doi_compress));
1686 1689 }
1687 1690
1688 1691 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n",
1689 1692 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1690 1693 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1691 1694
1692 1695 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1693 1696 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n",
1694 1697 "", "", "", "", "", bonus_size, "bonus",
1695 1698 ZDB_OT_NAME(doi.doi_bonus_type));
1696 1699 }
1697 1700
1698 1701 if (verbosity >= 4) {
1699 1702 (void) printf("\tdnode flags: %s%s%s\n",
1700 1703 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
1701 1704 "USED_BYTES " : "",
1702 1705 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
1703 1706 "USERUSED_ACCOUNTED " : "",
1704 1707 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
1705 1708 "SPILL_BLKPTR" : "");
1706 1709 (void) printf("\tdnode maxblkid: %llu\n",
1707 1710 (longlong_t)dn->dn_phys->dn_maxblkid);
1708 1711
1709 1712 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
1710 1713 bonus, bsize);
1711 1714 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1712 1715 *print_header = 1;
1713 1716 }
1714 1717
1715 1718 if (verbosity >= 5)
1716 1719 dump_indirect(dn);
1717 1720
1718 1721 if (verbosity >= 5) {
1719 1722 /*
1720 1723 * Report the list of segments that comprise the object.
1721 1724 */
1722 1725 uint64_t start = 0;
1723 1726 uint64_t end;
1724 1727 uint64_t blkfill = 1;
1725 1728 int minlvl = 1;
1726 1729
1727 1730 if (dn->dn_type == DMU_OT_DNODE) {
1728 1731 minlvl = 0;
1729 1732 blkfill = DNODES_PER_BLOCK;
1730 1733 }
1731 1734
1732 1735 for (;;) {
1733 1736 char segsize[32];
1734 1737 error = dnode_next_offset(dn,
1735 1738 0, &start, minlvl, blkfill, 0);
1736 1739 if (error)
1737 1740 break;
1738 1741 end = start;
1739 1742 error = dnode_next_offset(dn,
1740 1743 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
1741 1744 zdb_nicenum(end - start, segsize);
1742 1745 (void) printf("\t\tsegment [%016llx, %016llx)"
1743 1746 " size %5s\n", (u_longlong_t)start,
1744 1747 (u_longlong_t)end, segsize);
1745 1748 if (error)
1746 1749 break;
1747 1750 start = end;
1748 1751 }
1749 1752 }
1750 1753
1751 1754 if (db != NULL)
1752 1755 dmu_buf_rele(db, FTAG);
1753 1756 }
1754 1757
1755 1758 static char *objset_types[DMU_OST_NUMTYPES] = {
1756 1759 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1757 1760
1758 1761 static void
1759 1762 dump_dir(objset_t *os)
1760 1763 {
1761 1764 dmu_objset_stats_t dds;
1762 1765 uint64_t object, object_count;
1763 1766 uint64_t refdbytes, usedobjs, scratch;
1764 1767 char numbuf[32];
1765 1768 char blkbuf[BP_SPRINTF_LEN + 20];
1766 1769 char osname[MAXNAMELEN];
1767 1770 char *type = "UNKNOWN";
1768 1771 int verbosity = dump_opt['d'];
1769 1772 int print_header = 1;
1770 1773 int i, error;
1771 1774
1772 1775 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1773 1776 dmu_objset_fast_stat(os, &dds);
1774 1777 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1775 1778
1776 1779 if (dds.dds_type < DMU_OST_NUMTYPES)
1777 1780 type = objset_types[dds.dds_type];
1778 1781
1779 1782 if (dds.dds_type == DMU_OST_META) {
1780 1783 dds.dds_creation_txg = TXG_INITIAL;
1781 1784 usedobjs = os->os_rootbp->blk_fill;
1782 1785 refdbytes = os->os_spa->spa_dsl_pool->
1783 1786 dp_mos_dir->dd_phys->dd_used_bytes;
1784 1787 } else {
1785 1788 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1786 1789 }
1787 1790
1788 1791 ASSERT3U(usedobjs, ==, os->os_rootbp->blk_fill);
1789 1792
1790 1793 zdb_nicenum(refdbytes, numbuf);
1791 1794
1792 1795 if (verbosity >= 4) {
1793 1796 (void) sprintf(blkbuf, ", rootbp ");
1794 1797 (void) sprintf_blkptr(blkbuf + strlen(blkbuf), os->os_rootbp);
1795 1798 } else {
1796 1799 blkbuf[0] = '\0';
1797 1800 }
1798 1801
1799 1802 dmu_objset_name(os, osname);
1800 1803
1801 1804 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1802 1805 "%s, %llu objects%s\n",
1803 1806 osname, type, (u_longlong_t)dmu_objset_id(os),
1804 1807 (u_longlong_t)dds.dds_creation_txg,
1805 1808 numbuf, (u_longlong_t)usedobjs, blkbuf);
1806 1809
1807 1810 if (zopt_objects != 0) {
1808 1811 for (i = 0; i < zopt_objects; i++)
1809 1812 dump_object(os, zopt_object[i], verbosity,
1810 1813 &print_header);
1811 1814 (void) printf("\n");
1812 1815 return;
1813 1816 }
1814 1817
1815 1818 if (dump_opt['i'] != 0 || verbosity >= 2)
1816 1819 dump_intent_log(dmu_objset_zil(os));
1817 1820
1818 1821 if (dmu_objset_ds(os) != NULL)
1819 1822 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1820 1823
1821 1824 if (verbosity < 2)
1822 1825 return;
1823 1826
1824 1827 if (os->os_rootbp->blk_birth == 0)
1825 1828 return;
1826 1829
1827 1830 dump_object(os, 0, verbosity, &print_header);
1828 1831 object_count = 0;
1829 1832 if (DMU_USERUSED_DNODE(os) != NULL &&
1830 1833 DMU_USERUSED_DNODE(os)->dn_type != 0) {
1831 1834 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
1832 1835 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
1833 1836 }
1834 1837
1835 1838 object = 0;
1836 1839 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1837 1840 dump_object(os, object, verbosity, &print_header);
1838 1841 object_count++;
1839 1842 }
1840 1843
1841 1844 ASSERT3U(object_count, ==, usedobjs);
1842 1845
1843 1846 (void) printf("\n");
1844 1847
1845 1848 if (error != ESRCH) {
1846 1849 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
1847 1850 abort();
1848 1851 }
1849 1852 }
1850 1853
1851 1854 static void
1852 1855 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1853 1856 {
1854 1857 time_t timestamp = ub->ub_timestamp;
1855 1858
1856 1859 (void) printf(header ? header : "");
1857 1860 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1858 1861 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1859 1862 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1860 1863 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1861 1864 (void) printf("\ttimestamp = %llu UTC = %s",
1862 1865 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
1863 1866 if (dump_opt['u'] >= 3) {
1864 1867 char blkbuf[BP_SPRINTF_LEN];
1865 1868 sprintf_blkptr(blkbuf, &ub->ub_rootbp);
1866 1869 (void) printf("\trootbp = %s\n", blkbuf);
1867 1870 }
1868 1871 (void) printf(footer ? footer : "");
1869 1872 }
1870 1873
1871 1874 static void
1872 1875 dump_config(spa_t *spa)
1873 1876 {
1874 1877 dmu_buf_t *db;
1875 1878 size_t nvsize = 0;
1876 1879 int error = 0;
1877 1880
1878 1881
1879 1882 error = dmu_bonus_hold(spa->spa_meta_objset,
1880 1883 spa->spa_config_object, FTAG, &db);
1881 1884
1882 1885 if (error == 0) {
1883 1886 nvsize = *(uint64_t *)db->db_data;
1884 1887 dmu_buf_rele(db, FTAG);
1885 1888
1886 1889 (void) printf("\nMOS Configuration:\n");
1887 1890 dump_packed_nvlist(spa->spa_meta_objset,
1888 1891 spa->spa_config_object, (void *)&nvsize, 1);
1889 1892 } else {
1890 1893 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
1891 1894 (u_longlong_t)spa->spa_config_object, error);
1892 1895 }
1893 1896 }
1894 1897
1895 1898 static void
1896 1899 dump_cachefile(const char *cachefile)
1897 1900 {
1898 1901 int fd;
1899 1902 struct stat64 statbuf;
1900 1903 char *buf;
1901 1904 nvlist_t *config;
1902 1905
1903 1906 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
1904 1907 (void) printf("cannot open '%s': %s\n", cachefile,
1905 1908 strerror(errno));
1906 1909 exit(1);
1907 1910 }
1908 1911
1909 1912 if (fstat64(fd, &statbuf) != 0) {
1910 1913 (void) printf("failed to stat '%s': %s\n", cachefile,
1911 1914 strerror(errno));
1912 1915 exit(1);
1913 1916 }
1914 1917
1915 1918 if ((buf = malloc(statbuf.st_size)) == NULL) {
1916 1919 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
1917 1920 (u_longlong_t)statbuf.st_size);
1918 1921 exit(1);
1919 1922 }
1920 1923
1921 1924 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1922 1925 (void) fprintf(stderr, "failed to read %llu bytes\n",
1923 1926 (u_longlong_t)statbuf.st_size);
1924 1927 exit(1);
1925 1928 }
1926 1929
1927 1930 (void) close(fd);
1928 1931
1929 1932 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
1930 1933 (void) fprintf(stderr, "failed to unpack nvlist\n");
1931 1934 exit(1);
1932 1935 }
1933 1936
1934 1937 free(buf);
1935 1938
1936 1939 dump_nvlist(config, 0);
1937 1940
1938 1941 nvlist_free(config);
1939 1942 }
1940 1943
1941 1944 #define ZDB_MAX_UB_HEADER_SIZE 32
1942 1945
1943 1946 static void
1944 1947 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
1945 1948 {
1946 1949 vdev_t vd;
1947 1950 vdev_t *vdp = &vd;
1948 1951 char header[ZDB_MAX_UB_HEADER_SIZE];
1949 1952
1950 1953 vd.vdev_ashift = ashift;
1951 1954 vdp->vdev_top = vdp;
1952 1955
1953 1956 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
1954 1957 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
1955 1958 uberblock_t *ub = (void *)((char *)lbl + uoff);
1956 1959
1957 1960 if (uberblock_verify(ub))
1958 1961 continue;
1959 1962 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
1960 1963 "Uberblock[%d]\n", i);
1961 1964 dump_uberblock(ub, header, "");
1962 1965 }
1963 1966 }
1964 1967
1965 1968 static void
1966 1969 dump_label(const char *dev)
1967 1970 {
1968 1971 int fd;
1969 1972 vdev_label_t label;
1970 1973 char *path, *buf = label.vl_vdev_phys.vp_nvlist;
1971 1974 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
1972 1975 struct stat64 statbuf;
1973 1976 uint64_t psize, ashift;
1974 1977 int len = strlen(dev) + 1;
1975 1978
1976 1979 if (strncmp(dev, "/dev/dsk/", 9) == 0) {
1977 1980 len++;
1978 1981 path = malloc(len);
1979 1982 (void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
1980 1983 } else {
1981 1984 path = strdup(dev);
1982 1985 }
1983 1986
1984 1987 if ((fd = open64(path, O_RDONLY)) < 0) {
1985 1988 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
1986 1989 free(path);
1987 1990 exit(1);
1988 1991 }
1989 1992
1990 1993 if (fstat64(fd, &statbuf) != 0) {
1991 1994 (void) printf("failed to stat '%s': %s\n", path,
1992 1995 strerror(errno));
1993 1996 free(path);
1994 1997 (void) close(fd);
1995 1998 exit(1);
1996 1999 }
1997 2000
1998 2001 if (S_ISBLK(statbuf.st_mode)) {
1999 2002 (void) printf("cannot use '%s': character device required\n",
2000 2003 path);
2001 2004 free(path);
2002 2005 (void) close(fd);
2003 2006 exit(1);
2004 2007 }
2005 2008
2006 2009 psize = statbuf.st_size;
2007 2010 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2008 2011
2009 2012 for (int l = 0; l < VDEV_LABELS; l++) {
2010 2013 nvlist_t *config = NULL;
2011 2014
2012 2015 (void) printf("--------------------------------------------\n");
2013 2016 (void) printf("LABEL %d\n", l);
2014 2017 (void) printf("--------------------------------------------\n");
2015 2018
2016 2019 if (pread64(fd, &label, sizeof (label),
2017 2020 vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2018 2021 (void) printf("failed to read label %d\n", l);
2019 2022 continue;
2020 2023 }
2021 2024
2022 2025 if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2023 2026 (void) printf("failed to unpack label %d\n", l);
2024 2027 ashift = SPA_MINBLOCKSHIFT;
2025 2028 } else {
2026 2029 nvlist_t *vdev_tree = NULL;
2027 2030
2028 2031 dump_nvlist(config, 4);
2029 2032 if ((nvlist_lookup_nvlist(config,
2030 2033 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2031 2034 (nvlist_lookup_uint64(vdev_tree,
2032 2035 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2033 2036 ashift = SPA_MINBLOCKSHIFT;
2034 2037 nvlist_free(config);
2035 2038 }
2036 2039 if (dump_opt['u'])
2037 2040 dump_label_uberblocks(&label, ashift);
2038 2041 }
2039 2042
2040 2043 free(path);
2041 2044 (void) close(fd);
2042 2045 }
2043 2046
2044 2047 /*ARGSUSED*/
2045 2048 static int
2046 2049 dump_one_dir(const char *dsname, void *arg)
2047 2050 {
2048 2051 int error;
2049 2052 objset_t *os;
2050 2053
2051 2054 error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2052 2055 if (error) {
2053 2056 (void) printf("Could not open %s, error %d\n", dsname, error);
2054 2057 return (0);
2055 2058 }
2056 2059 dump_dir(os);
2057 2060 dmu_objset_disown(os, FTAG);
2058 2061 fuid_table_destroy();
2059 2062 sa_loaded = B_FALSE;
2060 2063 return (0);
2061 2064 }
2062 2065
2063 2066 /*
2064 2067 * Block statistics.
2065 2068 */
2066 2069 #define PSIZE_HISTO_SIZE (SPA_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1)
2067 2070 typedef struct zdb_blkstats {
2068 2071 uint64_t zb_asize;
2069 2072 uint64_t zb_lsize;
2070 2073 uint64_t zb_psize;
2071 2074 uint64_t zb_count;
2072 2075 uint64_t zb_gangs;
2073 2076 uint64_t zb_ditto_samevdev;
2074 2077 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2075 2078 } zdb_blkstats_t;
2076 2079
2077 2080 /*
2078 2081 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2079 2082 */
2080 2083 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2081 2084 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2082 2085 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2083 2086 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2084 2087
2085 2088 static char *zdb_ot_extname[] = {
2086 2089 "deferred free",
2087 2090 "dedup ditto",
2088 2091 "other",
2089 2092 "Total",
2090 2093 };
2091 2094
2092 2095 #define ZB_TOTAL DN_MAX_LEVELS
2093 2096
2094 2097 typedef struct zdb_cb {
2095 2098 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2096 2099 uint64_t zcb_dedup_asize;
2097 2100 uint64_t zcb_dedup_blocks;
2098 2101 uint64_t zcb_start;
2099 2102 uint64_t zcb_lastprint;
2100 2103 uint64_t zcb_totalasize;
2101 2104 uint64_t zcb_errors[256];
2102 2105 int zcb_readfails;
2103 2106 int zcb_haderrors;
2104 2107 spa_t *zcb_spa;
2105 2108 } zdb_cb_t;
2106 2109
2107 2110 static void
2108 2111 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2109 2112 dmu_object_type_t type)
2110 2113 {
2111 2114 uint64_t refcnt = 0;
2112 2115
2113 2116 ASSERT(type < ZDB_OT_TOTAL);
2114 2117
2115 2118 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2116 2119 return;
2117 2120
2118 2121 for (int i = 0; i < 4; i++) {
2119 2122 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2120 2123 int t = (i & 1) ? type : ZDB_OT_TOTAL;
2121 2124 int equal;
2122 2125 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2123 2126
2124 2127 zb->zb_asize += BP_GET_ASIZE(bp);
2125 2128 zb->zb_lsize += BP_GET_LSIZE(bp);
2126 2129 zb->zb_psize += BP_GET_PSIZE(bp);
2127 2130 zb->zb_count++;
2128 2131 zb->zb_psize_histogram[BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT]++;
2129 2132
2130 2133 zb->zb_gangs += BP_COUNT_GANG(bp);
2131 2134
2132 2135 switch (BP_GET_NDVAS(bp)) {
2133 2136 case 2:
2134 2137 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2135 2138 DVA_GET_VDEV(&bp->blk_dva[1]))
2136 2139 zb->zb_ditto_samevdev++;
2137 2140 break;
2138 2141 case 3:
2139 2142 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2140 2143 DVA_GET_VDEV(&bp->blk_dva[1])) +
2141 2144 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2142 2145 DVA_GET_VDEV(&bp->blk_dva[2])) +
2143 2146 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2144 2147 DVA_GET_VDEV(&bp->blk_dva[2]));
2145 2148 if (equal != 0)
2146 2149 zb->zb_ditto_samevdev++;
2147 2150 break;
2148 2151 }
2149 2152
2150 2153 }
2151 2154
2152 2155 if (dump_opt['L'])
2153 2156 return;
2154 2157
2155 2158 if (BP_GET_DEDUP(bp)) {
2156 2159 ddt_t *ddt;
2157 2160 ddt_entry_t *dde;
2158 2161
2159 2162 ddt = ddt_select(zcb->zcb_spa, bp);
2160 2163 ddt_enter(ddt);
2161 2164 dde = ddt_lookup(ddt, bp, B_FALSE);
2162 2165
2163 2166 if (dde == NULL) {
2164 2167 refcnt = 0;
2165 2168 } else {
2166 2169 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2167 2170 ddt_phys_decref(ddp);
2168 2171 refcnt = ddp->ddp_refcnt;
2169 2172 if (ddt_phys_total_refcnt(dde) == 0)
2170 2173 ddt_remove(ddt, dde);
2171 2174 }
2172 2175 ddt_exit(ddt);
2173 2176 }
2174 2177
2175 2178 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2176 2179 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2177 2180 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2178 2181 }
2179 2182
2180 2183 static void
2181 2184 zdb_blkptr_done(zio_t *zio)
2182 2185 {
2183 2186 spa_t *spa = zio->io_spa;
2184 2187 blkptr_t *bp = zio->io_bp;
2185 2188 int ioerr = zio->io_error;
2186 2189 zdb_cb_t *zcb = zio->io_private;
2187 2190 zbookmark_t *zb = &zio->io_bookmark;
2188 2191
2189 2192 zio_data_buf_free(zio->io_data, zio->io_size);
2190 2193
2191 2194 mutex_enter(&spa->spa_scrub_lock);
2192 2195 spa->spa_scrub_inflight--;
2193 2196 cv_broadcast(&spa->spa_scrub_io_cv);
2194 2197
2195 2198 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2196 2199 char blkbuf[BP_SPRINTF_LEN];
2197 2200
2198 2201 zcb->zcb_haderrors = 1;
2199 2202 zcb->zcb_errors[ioerr]++;
2200 2203
2201 2204 if (dump_opt['b'] >= 2)
2202 2205 sprintf_blkptr(blkbuf, bp);
2203 2206 else
2204 2207 blkbuf[0] = '\0';
2205 2208
2206 2209 (void) printf("zdb_blkptr_cb: "
2207 2210 "Got error %d reading "
2208 2211 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
2209 2212 ioerr,
2210 2213 (u_longlong_t)zb->zb_objset,
2211 2214 (u_longlong_t)zb->zb_object,
2212 2215 (u_longlong_t)zb->zb_level,
2213 2216 (u_longlong_t)zb->zb_blkid,
2214 2217 blkbuf);
2215 2218 }
2216 2219 mutex_exit(&spa->spa_scrub_lock);
2217 2220 }
2218 2221
2219 2222 static int
2220 2223 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2221 2224 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2222 2225 {
2223 2226 zdb_cb_t *zcb = arg;
2224 2227 char blkbuf[BP_SPRINTF_LEN];
2225 2228 dmu_object_type_t type;
2226 2229 boolean_t is_metadata;
2227 2230
2228 2231 if (bp == NULL)
2229 2232 return (0);
2230 2233
2231 2234 type = BP_GET_TYPE(bp);
2232 2235
2233 2236 zdb_count_block(zcb, zilog, bp,
2234 2237 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2235 2238
2236 2239 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2237 2240
2238 2241 if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) {
2239 2242 size_t size = BP_GET_PSIZE(bp);
2240 2243 void *data = zio_data_buf_alloc(size);
2241 2244 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2242 2245
2243 2246 /* If it's an intent log block, failure is expected. */
2244 2247 if (zb->zb_level == ZB_ZIL_LEVEL)
2245 2248 flags |= ZIO_FLAG_SPECULATIVE;
2246 2249
2247 2250 mutex_enter(&spa->spa_scrub_lock);
2248 2251 while (spa->spa_scrub_inflight > max_inflight)
2249 2252 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2250 2253 spa->spa_scrub_inflight++;
2251 2254 mutex_exit(&spa->spa_scrub_lock);
2252 2255
2253 2256 zio_nowait(zio_read(NULL, spa, bp, data, size,
2254 2257 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2255 2258 }
2256 2259
2257 2260 zcb->zcb_readfails = 0;
2258 2261
2259 2262 if (dump_opt['b'] >= 5) {
2260 2263 sprintf_blkptr(blkbuf, bp);
2261 2264 (void) printf("objset %llu object %llu "
2262 2265 "level %lld offset 0x%llx %s\n",
2263 2266 (u_longlong_t)zb->zb_objset,
2264 2267 (u_longlong_t)zb->zb_object,
2265 2268 (longlong_t)zb->zb_level,
2266 2269 (u_longlong_t)blkid2offset(dnp, bp, zb),
2267 2270 blkbuf);
2268 2271 }
2269 2272
2270 2273 if (dump_opt['b'] < 5 && isatty(STDERR_FILENO) &&
2271 2274 gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2272 2275 uint64_t now = gethrtime();
2273 2276 char buf[10];
2274 2277 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2275 2278 int kb_per_sec =
2276 2279 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2277 2280 int sec_remaining =
2278 2281 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2279 2282
2280 2283 zfs_nicenum(bytes, buf, sizeof (buf));
2281 2284 (void) fprintf(stderr,
2282 2285 "\r%5s completed (%4dMB/s) "
2283 2286 "estimated time remaining: %uhr %02umin %02usec ",
2284 2287 buf, kb_per_sec / 1024,
2285 2288 sec_remaining / 60 / 60,
2286 2289 sec_remaining / 60 % 60,
2287 2290 sec_remaining % 60);
2288 2291
2289 2292 zcb->zcb_lastprint = now;
2290 2293 }
2291 2294
2292 2295 return (0);
2293 2296 }
2294 2297
2295 2298 static void
2296 2299 zdb_leak(void *arg, uint64_t start, uint64_t size)
2297 2300 {
2298 2301 vdev_t *vd = arg;
2299 2302
2300 2303 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2301 2304 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2302 2305 }
2303 2306
2304 2307 static metaslab_ops_t zdb_metaslab_ops = {
2305 2308 NULL, /* alloc */
2306 2309 NULL /* fragmented */
2307 2310 };
2308 2311
2309 2312 static void
2310 2313 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2311 2314 {
2312 2315 ddt_bookmark_t ddb = { 0 };
2313 2316 ddt_entry_t dde;
2314 2317 int error;
2315 2318
2316 2319 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2317 2320 blkptr_t blk;
2318 2321 ddt_phys_t *ddp = dde.dde_phys;
2319 2322
2320 2323 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2321 2324 return;
2322 2325
2323 2326 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2324 2327
2325 2328 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2326 2329 if (ddp->ddp_phys_birth == 0)
2327 2330 continue;
2328 2331 ddt_bp_create(ddb.ddb_checksum,
2329 2332 &dde.dde_key, ddp, &blk);
2330 2333 if (p == DDT_PHYS_DITTO) {
2331 2334 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2332 2335 } else {
2333 2336 zcb->zcb_dedup_asize +=
2334 2337 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2335 2338 zcb->zcb_dedup_blocks++;
2336 2339 }
2337 2340 }
2338 2341 if (!dump_opt['L']) {
2339 2342 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2340 2343 ddt_enter(ddt);
2341 2344 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2342 2345 ddt_exit(ddt);
2343 2346 }
2344 2347 }
2345 2348
2346 2349 ASSERT(error == ENOENT);
2347 2350 }
2348 2351
2349 2352 static void
2350 2353 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2351 2354 {
2352 2355 zcb->zcb_spa = spa;
2353 2356
2354 2357 if (!dump_opt['L']) {
2355 2358 vdev_t *rvd = spa->spa_root_vdev;
2356 2359 for (int c = 0; c < rvd->vdev_children; c++) {
2357 2360 vdev_t *vd = rvd->vdev_child[c];
2358 2361 for (int m = 0; m < vd->vdev_ms_count; m++) {
2359 2362 metaslab_t *msp = vd->vdev_ms[m];
2360 2363 mutex_enter(&msp->ms_lock);
2361 2364 metaslab_unload(msp);
2362 2365
2363 2366 /*
2364 2367 * For leak detection, we overload the metaslab
2365 2368 * ms_tree to contain allocated segments
2366 2369 * instead of free segments. As a result,
2367 2370 * we can't use the normal metaslab_load/unload
2368 2371 * interfaces.
2369 2372 */
2370 2373 if (msp->ms_sm != NULL) {
2371 2374 msp->ms_ops = &zdb_metaslab_ops;
2372 2375 VERIFY0(space_map_load(msp->ms_sm,
2373 2376 msp->ms_tree, SM_ALLOC));
2374 2377 msp->ms_loaded = B_TRUE;
2375 2378 }
2376 2379 mutex_exit(&msp->ms_lock);
2377 2380 }
2378 2381 }
2379 2382 }
2380 2383
2381 2384 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2382 2385
2383 2386 zdb_ddt_leak_init(spa, zcb);
2384 2387
2385 2388 spa_config_exit(spa, SCL_CONFIG, FTAG);
2386 2389 }
2387 2390
2388 2391 static void
2389 2392 zdb_leak_fini(spa_t *spa)
2390 2393 {
2391 2394 if (!dump_opt['L']) {
2392 2395 vdev_t *rvd = spa->spa_root_vdev;
2393 2396 for (int c = 0; c < rvd->vdev_children; c++) {
2394 2397 vdev_t *vd = rvd->vdev_child[c];
2395 2398 for (int m = 0; m < vd->vdev_ms_count; m++) {
2396 2399 metaslab_t *msp = vd->vdev_ms[m];
2397 2400 mutex_enter(&msp->ms_lock);
2398 2401
2399 2402 /*
2400 2403 * The ms_tree has been overloaded to
2401 2404 * contain allocated segments. Now that we
2402 2405 * finished traversing all blocks, any
2403 2406 * block that remains in the ms_tree
2404 2407 * represents an allocated block that we
2405 2408 * did not claim during the traversal.
2406 2409 * Claimed blocks would have been removed
2407 2410 * from the ms_tree.
2408 2411 */
2409 2412 range_tree_vacate(msp->ms_tree, zdb_leak, vd);
2410 2413 msp->ms_loaded = B_FALSE;
2411 2414
2412 2415 mutex_exit(&msp->ms_lock);
2413 2416 }
2414 2417 }
2415 2418 }
2416 2419 }
2417 2420
2418 2421 /* ARGSUSED */
2419 2422 static int
2420 2423 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2421 2424 {
2422 2425 zdb_cb_t *zcb = arg;
2423 2426
2424 2427 if (dump_opt['b'] >= 5) {
2425 2428 char blkbuf[BP_SPRINTF_LEN];
2426 2429 sprintf_blkptr(blkbuf, bp);
2427 2430 (void) printf("[%s] %s\n",
2428 2431 "deferred free", blkbuf);
2429 2432 }
2430 2433 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2431 2434 return (0);
2432 2435 }
2433 2436
2434 2437 static int
2435 2438 dump_block_stats(spa_t *spa)
2436 2439 {
2437 2440 zdb_cb_t zcb = { 0 };
2438 2441 zdb_blkstats_t *zb, *tzb;
2439 2442 uint64_t norm_alloc, norm_space, total_alloc, total_found;
2440 2443 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2441 2444 int leaks = 0;
2442 2445
2443 2446 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2444 2447 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2445 2448 (dump_opt['c'] == 1) ? "metadata " : "",
2446 2449 dump_opt['c'] ? "checksums " : "",
2447 2450 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2448 2451 !dump_opt['L'] ? "nothing leaked " : "");
2449 2452
2450 2453 /*
2451 2454 * Load all space maps as SM_ALLOC maps, then traverse the pool
2452 2455 * claiming each block we discover. If the pool is perfectly
2453 2456 * consistent, the space maps will be empty when we're done.
2454 2457 * Anything left over is a leak; any block we can't claim (because
2455 2458 * it's not part of any space map) is a double allocation,
2456 2459 * reference to a freed block, or an unclaimed log block.
2457 2460 */
2458 2461 zdb_leak_init(spa, &zcb);
↓ open down ↓ |
1770 lines elided |
↑ open up ↑ |
2459 2462
2460 2463 /*
2461 2464 * If there's a deferred-free bplist, process that first.
2462 2465 */
2463 2466 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2464 2467 count_block_cb, &zcb, NULL);
2465 2468 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2466 2469 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2467 2470 count_block_cb, &zcb, NULL);
2468 2471 }
2469 - if (spa_feature_is_active(spa,
2470 - &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
2472 + if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2471 2473 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2472 2474 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2473 2475 &zcb, NULL));
2474 2476 }
2475 2477
2476 2478 if (dump_opt['c'] > 1)
2477 2479 flags |= TRAVERSE_PREFETCH_DATA;
2478 2480
2479 2481 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2480 2482 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2481 2483 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2482 2484
2483 2485 /*
2484 2486 * If we've traversed the data blocks then we need to wait for those
2485 2487 * I/Os to complete. We leverage "The Godfather" zio to wait on
2486 2488 * all async I/Os to complete.
2487 2489 */
2488 2490 if (dump_opt['c']) {
2489 2491 (void) zio_wait(spa->spa_async_zio_root);
2490 2492 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2491 2493 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2492 2494 ZIO_FLAG_GODFATHER);
2493 2495 }
2494 2496
2495 2497 if (zcb.zcb_haderrors) {
2496 2498 (void) printf("\nError counts:\n\n");
2497 2499 (void) printf("\t%5s %s\n", "errno", "count");
2498 2500 for (int e = 0; e < 256; e++) {
2499 2501 if (zcb.zcb_errors[e] != 0) {
2500 2502 (void) printf("\t%5d %llu\n",
2501 2503 e, (u_longlong_t)zcb.zcb_errors[e]);
2502 2504 }
2503 2505 }
2504 2506 }
2505 2507
2506 2508 /*
2507 2509 * Report any leaked segments.
2508 2510 */
2509 2511 zdb_leak_fini(spa);
2510 2512
2511 2513 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2512 2514
2513 2515 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2514 2516 norm_space = metaslab_class_get_space(spa_normal_class(spa));
2515 2517
2516 2518 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2517 2519 total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2518 2520
2519 2521 if (total_found == total_alloc) {
2520 2522 if (!dump_opt['L'])
2521 2523 (void) printf("\n\tNo leaks (block sum matches space"
2522 2524 " maps exactly)\n");
2523 2525 } else {
2524 2526 (void) printf("block traversal size %llu != alloc %llu "
2525 2527 "(%s %lld)\n",
2526 2528 (u_longlong_t)total_found,
2527 2529 (u_longlong_t)total_alloc,
2528 2530 (dump_opt['L']) ? "unreachable" : "leaked",
2529 2531 (longlong_t)(total_alloc - total_found));
2530 2532 leaks = 1;
2531 2533 }
2532 2534
2533 2535 if (tzb->zb_count == 0)
2534 2536 return (2);
2535 2537
2536 2538 (void) printf("\n");
2537 2539 (void) printf("\tbp count: %10llu\n",
2538 2540 (u_longlong_t)tzb->zb_count);
2539 2541 (void) printf("\tganged count: %10llu\n",
2540 2542 (longlong_t)tzb->zb_gangs);
2541 2543 (void) printf("\tbp logical: %10llu avg: %6llu\n",
2542 2544 (u_longlong_t)tzb->zb_lsize,
2543 2545 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2544 2546 (void) printf("\tbp physical: %10llu avg:"
2545 2547 " %6llu compression: %6.2f\n",
2546 2548 (u_longlong_t)tzb->zb_psize,
2547 2549 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2548 2550 (double)tzb->zb_lsize / tzb->zb_psize);
2549 2551 (void) printf("\tbp allocated: %10llu avg:"
2550 2552 " %6llu compression: %6.2f\n",
2551 2553 (u_longlong_t)tzb->zb_asize,
2552 2554 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2553 2555 (double)tzb->zb_lsize / tzb->zb_asize);
2554 2556 (void) printf("\tbp deduped: %10llu ref>1:"
2555 2557 " %6llu deduplication: %6.2f\n",
2556 2558 (u_longlong_t)zcb.zcb_dedup_asize,
2557 2559 (u_longlong_t)zcb.zcb_dedup_blocks,
2558 2560 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2559 2561 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
2560 2562 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2561 2563
2562 2564 if (tzb->zb_ditto_samevdev != 0) {
2563 2565 (void) printf("\tDittoed blocks on same vdev: %llu\n",
2564 2566 (longlong_t)tzb->zb_ditto_samevdev);
2565 2567 }
2566 2568
2567 2569 if (dump_opt['b'] >= 2) {
2568 2570 int l, t, level;
2569 2571 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2570 2572 "\t avg\t comp\t%%Total\tType\n");
2571 2573
2572 2574 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
2573 2575 char csize[32], lsize[32], psize[32], asize[32];
2574 2576 char avg[32], gang[32];
2575 2577 char *typename;
2576 2578
2577 2579 if (t < DMU_OT_NUMTYPES)
2578 2580 typename = dmu_ot[t].ot_name;
2579 2581 else
2580 2582 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2581 2583
2582 2584 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2583 2585 (void) printf("%6s\t%5s\t%5s\t%5s"
2584 2586 "\t%5s\t%5s\t%6s\t%s\n",
2585 2587 "-",
2586 2588 "-",
2587 2589 "-",
2588 2590 "-",
2589 2591 "-",
2590 2592 "-",
2591 2593 "-",
2592 2594 typename);
2593 2595 continue;
2594 2596 }
2595 2597
2596 2598 for (l = ZB_TOTAL - 1; l >= -1; l--) {
2597 2599 level = (l == -1 ? ZB_TOTAL : l);
2598 2600 zb = &zcb.zcb_type[level][t];
2599 2601
2600 2602 if (zb->zb_asize == 0)
2601 2603 continue;
2602 2604
2603 2605 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2604 2606 continue;
2605 2607
2606 2608 if (level == 0 && zb->zb_asize ==
2607 2609 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2608 2610 continue;
2609 2611
2610 2612 zdb_nicenum(zb->zb_count, csize);
2611 2613 zdb_nicenum(zb->zb_lsize, lsize);
2612 2614 zdb_nicenum(zb->zb_psize, psize);
2613 2615 zdb_nicenum(zb->zb_asize, asize);
2614 2616 zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2615 2617 zdb_nicenum(zb->zb_gangs, gang);
2616 2618
2617 2619 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2618 2620 "\t%5.2f\t%6.2f\t",
2619 2621 csize, lsize, psize, asize, avg,
2620 2622 (double)zb->zb_lsize / zb->zb_psize,
2621 2623 100.0 * zb->zb_asize / tzb->zb_asize);
2622 2624
2623 2625 if (level == ZB_TOTAL)
2624 2626 (void) printf("%s\n", typename);
2625 2627 else
2626 2628 (void) printf(" L%d %s\n",
2627 2629 level, typename);
2628 2630
2629 2631 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2630 2632 (void) printf("\t number of ganged "
2631 2633 "blocks: %s\n", gang);
2632 2634 }
2633 2635
2634 2636 if (dump_opt['b'] >= 4) {
2635 2637 (void) printf("psize "
2636 2638 "(in 512-byte sectors): "
2637 2639 "number of blocks\n");
2638 2640 dump_histogram(zb->zb_psize_histogram,
2639 2641 PSIZE_HISTO_SIZE, 0);
2640 2642 }
2641 2643 }
2642 2644 }
2643 2645 }
2644 2646
2645 2647 (void) printf("\n");
2646 2648
2647 2649 if (leaks)
2648 2650 return (2);
2649 2651
2650 2652 if (zcb.zcb_haderrors)
2651 2653 return (3);
2652 2654
2653 2655 return (0);
2654 2656 }
2655 2657
2656 2658 typedef struct zdb_ddt_entry {
2657 2659 ddt_key_t zdde_key;
2658 2660 uint64_t zdde_ref_blocks;
2659 2661 uint64_t zdde_ref_lsize;
2660 2662 uint64_t zdde_ref_psize;
2661 2663 uint64_t zdde_ref_dsize;
2662 2664 avl_node_t zdde_node;
2663 2665 } zdb_ddt_entry_t;
2664 2666
2665 2667 /* ARGSUSED */
2666 2668 static int
2667 2669 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2668 2670 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2669 2671 {
2670 2672 avl_tree_t *t = arg;
2671 2673 avl_index_t where;
2672 2674 zdb_ddt_entry_t *zdde, zdde_search;
2673 2675
2674 2676 if (bp == NULL)
2675 2677 return (0);
2676 2678
2677 2679 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2678 2680 (void) printf("traversing objset %llu, %llu objects, "
2679 2681 "%lu blocks so far\n",
2680 2682 (u_longlong_t)zb->zb_objset,
2681 2683 (u_longlong_t)bp->blk_fill,
2682 2684 avl_numnodes(t));
2683 2685 }
2684 2686
2685 2687 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2686 2688 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2687 2689 return (0);
2688 2690
2689 2691 ddt_key_fill(&zdde_search.zdde_key, bp);
2690 2692
2691 2693 zdde = avl_find(t, &zdde_search, &where);
2692 2694
2693 2695 if (zdde == NULL) {
2694 2696 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2695 2697 zdde->zdde_key = zdde_search.zdde_key;
2696 2698 avl_insert(t, zdde, where);
2697 2699 }
2698 2700
2699 2701 zdde->zdde_ref_blocks += 1;
2700 2702 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2701 2703 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2702 2704 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2703 2705
2704 2706 return (0);
2705 2707 }
2706 2708
2707 2709 static void
2708 2710 dump_simulated_ddt(spa_t *spa)
2709 2711 {
2710 2712 avl_tree_t t;
2711 2713 void *cookie = NULL;
2712 2714 zdb_ddt_entry_t *zdde;
2713 2715 ddt_histogram_t ddh_total = { 0 };
2714 2716 ddt_stat_t dds_total = { 0 };
2715 2717
2716 2718 avl_create(&t, ddt_entry_compare,
2717 2719 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2718 2720
2719 2721 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2720 2722
2721 2723 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2722 2724 zdb_ddt_add_cb, &t);
2723 2725
2724 2726 spa_config_exit(spa, SCL_CONFIG, FTAG);
2725 2727
2726 2728 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2727 2729 ddt_stat_t dds;
2728 2730 uint64_t refcnt = zdde->zdde_ref_blocks;
2729 2731 ASSERT(refcnt != 0);
2730 2732
2731 2733 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2732 2734 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2733 2735 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2734 2736 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2735 2737
2736 2738 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2737 2739 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2738 2740 dds.dds_ref_psize = zdde->zdde_ref_psize;
2739 2741 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2740 2742
2741 2743 ddt_stat_add(&ddh_total.ddh_stat[highbit(refcnt) - 1], &dds, 0);
2742 2744
2743 2745 umem_free(zdde, sizeof (*zdde));
2744 2746 }
2745 2747
2746 2748 avl_destroy(&t);
2747 2749
2748 2750 ddt_histogram_stat(&dds_total, &ddh_total);
2749 2751
2750 2752 (void) printf("Simulated DDT histogram:\n");
2751 2753
2752 2754 zpool_dump_ddt(&dds_total, &ddh_total);
2753 2755
2754 2756 dump_dedup_ratio(&dds_total);
2755 2757 }
2756 2758
2757 2759 static void
2758 2760 dump_zpool(spa_t *spa)
2759 2761 {
2760 2762 dsl_pool_t *dp = spa_get_dsl(spa);
2761 2763 int rc = 0;
2762 2764
2763 2765 if (dump_opt['S']) {
2764 2766 dump_simulated_ddt(spa);
2765 2767 return;
2766 2768 }
2767 2769
2768 2770 if (!dump_opt['e'] && dump_opt['C'] > 1) {
2769 2771 (void) printf("\nCached configuration:\n");
2770 2772 dump_nvlist(spa->spa_config, 8);
2771 2773 }
2772 2774
2773 2775 if (dump_opt['C'])
2774 2776 dump_config(spa);
2775 2777
2776 2778 if (dump_opt['u'])
2777 2779 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2778 2780
2779 2781 if (dump_opt['D'])
2780 2782 dump_all_ddts(spa);
2781 2783
2782 2784 if (dump_opt['d'] > 2 || dump_opt['m'])
2783 2785 dump_metaslabs(spa);
2784 2786
2785 2787 if (dump_opt['d'] || dump_opt['i']) {
↓ open down ↓ |
305 lines elided |
↑ open up ↑ |
2786 2788 dump_dir(dp->dp_meta_objset);
2787 2789 if (dump_opt['d'] >= 3) {
2788 2790 dump_bpobj(&spa->spa_deferred_bpobj,
2789 2791 "Deferred frees", 0);
2790 2792 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2791 2793 dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2792 2794 "Pool snapshot frees", 0);
2793 2795 }
2794 2796
2795 2797 if (spa_feature_is_active(spa,
2796 - &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
2798 + SPA_FEATURE_ASYNC_DESTROY)) {
2797 2799 dump_bptree(spa->spa_meta_objset,
2798 2800 spa->spa_dsl_pool->dp_bptree_obj,
2799 2801 "Pool dataset frees");
2800 2802 }
2801 2803 dump_dtl(spa->spa_root_vdev, 0);
2802 2804 }
2803 2805 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
2804 2806 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2805 2807 }
2806 2808 if (dump_opt['b'] || dump_opt['c'])
2807 2809 rc = dump_block_stats(spa);
2808 2810
2809 2811 if (rc == 0)
2810 2812 rc = verify_spacemap_refcounts(spa);
2811 2813
2812 2814 if (dump_opt['s'])
2813 2815 show_pool_stats(spa);
2814 2816
2815 2817 if (dump_opt['h'])
2816 2818 dump_history(spa);
2817 2819
2818 2820 if (rc != 0)
2819 2821 exit(rc);
2820 2822 }
2821 2823
2822 2824 #define ZDB_FLAG_CHECKSUM 0x0001
2823 2825 #define ZDB_FLAG_DECOMPRESS 0x0002
2824 2826 #define ZDB_FLAG_BSWAP 0x0004
2825 2827 #define ZDB_FLAG_GBH 0x0008
2826 2828 #define ZDB_FLAG_INDIRECT 0x0010
2827 2829 #define ZDB_FLAG_PHYS 0x0020
2828 2830 #define ZDB_FLAG_RAW 0x0040
2829 2831 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
2830 2832
2831 2833 int flagbits[256];
2832 2834
2833 2835 static void
2834 2836 zdb_print_blkptr(blkptr_t *bp, int flags)
2835 2837 {
2836 2838 char blkbuf[BP_SPRINTF_LEN];
2837 2839
2838 2840 if (flags & ZDB_FLAG_BSWAP)
2839 2841 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
2840 2842
2841 2843 sprintf_blkptr(blkbuf, bp);
2842 2844 (void) printf("%s\n", blkbuf);
2843 2845 }
2844 2846
2845 2847 static void
2846 2848 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
2847 2849 {
2848 2850 int i;
2849 2851
2850 2852 for (i = 0; i < nbps; i++)
2851 2853 zdb_print_blkptr(&bp[i], flags);
2852 2854 }
2853 2855
2854 2856 static void
2855 2857 zdb_dump_gbh(void *buf, int flags)
2856 2858 {
2857 2859 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
2858 2860 }
2859 2861
2860 2862 static void
2861 2863 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
2862 2864 {
2863 2865 if (flags & ZDB_FLAG_BSWAP)
2864 2866 byteswap_uint64_array(buf, size);
2865 2867 (void) write(1, buf, size);
2866 2868 }
2867 2869
2868 2870 static void
2869 2871 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
2870 2872 {
2871 2873 uint64_t *d = (uint64_t *)buf;
2872 2874 int nwords = size / sizeof (uint64_t);
2873 2875 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
2874 2876 int i, j;
2875 2877 char *hdr, *c;
2876 2878
2877 2879
2878 2880 if (do_bswap)
2879 2881 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
2880 2882 else
2881 2883 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
2882 2884
2883 2885 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
2884 2886
2885 2887 for (i = 0; i < nwords; i += 2) {
2886 2888 (void) printf("%06llx: %016llx %016llx ",
2887 2889 (u_longlong_t)(i * sizeof (uint64_t)),
2888 2890 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
2889 2891 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
2890 2892
2891 2893 c = (char *)&d[i];
2892 2894 for (j = 0; j < 2 * sizeof (uint64_t); j++)
2893 2895 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
2894 2896 (void) printf("\n");
2895 2897 }
2896 2898 }
2897 2899
2898 2900 /*
2899 2901 * There are two acceptable formats:
2900 2902 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
2901 2903 * child[.child]* - For example: 0.1.1
2902 2904 *
2903 2905 * The second form can be used to specify arbitrary vdevs anywhere
2904 2906 * in the heirarchy. For example, in a pool with a mirror of
2905 2907 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
2906 2908 */
2907 2909 static vdev_t *
2908 2910 zdb_vdev_lookup(vdev_t *vdev, char *path)
2909 2911 {
2910 2912 char *s, *p, *q;
2911 2913 int i;
2912 2914
2913 2915 if (vdev == NULL)
2914 2916 return (NULL);
2915 2917
2916 2918 /* First, assume the x.x.x.x format */
2917 2919 i = (int)strtoul(path, &s, 10);
2918 2920 if (s == path || (s && *s != '.' && *s != '\0'))
2919 2921 goto name;
2920 2922 if (i < 0 || i >= vdev->vdev_children)
2921 2923 return (NULL);
2922 2924
2923 2925 vdev = vdev->vdev_child[i];
2924 2926 if (*s == '\0')
2925 2927 return (vdev);
2926 2928 return (zdb_vdev_lookup(vdev, s+1));
2927 2929
2928 2930 name:
2929 2931 for (i = 0; i < vdev->vdev_children; i++) {
2930 2932 vdev_t *vc = vdev->vdev_child[i];
2931 2933
2932 2934 if (vc->vdev_path == NULL) {
2933 2935 vc = zdb_vdev_lookup(vc, path);
2934 2936 if (vc == NULL)
2935 2937 continue;
2936 2938 else
2937 2939 return (vc);
2938 2940 }
2939 2941
2940 2942 p = strrchr(vc->vdev_path, '/');
2941 2943 p = p ? p + 1 : vc->vdev_path;
2942 2944 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
2943 2945
2944 2946 if (strcmp(vc->vdev_path, path) == 0)
2945 2947 return (vc);
2946 2948 if (strcmp(p, path) == 0)
2947 2949 return (vc);
2948 2950 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
2949 2951 return (vc);
2950 2952 }
2951 2953
2952 2954 return (NULL);
2953 2955 }
2954 2956
2955 2957 /*
2956 2958 * Read a block from a pool and print it out. The syntax of the
2957 2959 * block descriptor is:
2958 2960 *
2959 2961 * pool:vdev_specifier:offset:size[:flags]
2960 2962 *
2961 2963 * pool - The name of the pool you wish to read from
2962 2964 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
2963 2965 * offset - offset, in hex, in bytes
2964 2966 * size - Amount of data to read, in hex, in bytes
2965 2967 * flags - A string of characters specifying options
2966 2968 * b: Decode a blkptr at given offset within block
2967 2969 * *c: Calculate and display checksums
2968 2970 * d: Decompress data before dumping
2969 2971 * e: Byteswap data before dumping
2970 2972 * g: Display data as a gang block header
2971 2973 * i: Display as an indirect block
2972 2974 * p: Do I/O to physical offset
2973 2975 * r: Dump raw data to stdout
2974 2976 *
2975 2977 * * = not yet implemented
2976 2978 */
2977 2979 static void
2978 2980 zdb_read_block(char *thing, spa_t *spa)
2979 2981 {
2980 2982 blkptr_t blk, *bp = &blk;
2981 2983 dva_t *dva = bp->blk_dva;
2982 2984 int flags = 0;
2983 2985 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
2984 2986 zio_t *zio;
2985 2987 vdev_t *vd;
2986 2988 void *pbuf, *lbuf, *buf;
2987 2989 char *s, *p, *dup, *vdev, *flagstr;
2988 2990 int i, error;
2989 2991
2990 2992 dup = strdup(thing);
2991 2993 s = strtok(dup, ":");
2992 2994 vdev = s ? s : "";
2993 2995 s = strtok(NULL, ":");
2994 2996 offset = strtoull(s ? s : "", NULL, 16);
2995 2997 s = strtok(NULL, ":");
2996 2998 size = strtoull(s ? s : "", NULL, 16);
2997 2999 s = strtok(NULL, ":");
2998 3000 flagstr = s ? s : "";
2999 3001
3000 3002 s = NULL;
3001 3003 if (size == 0)
3002 3004 s = "size must not be zero";
3003 3005 if (!IS_P2ALIGNED(size, DEV_BSIZE))
3004 3006 s = "size must be a multiple of sector size";
3005 3007 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
3006 3008 s = "offset must be a multiple of sector size";
3007 3009 if (s) {
3008 3010 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
3009 3011 free(dup);
3010 3012 return;
3011 3013 }
3012 3014
3013 3015 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
3014 3016 for (i = 0; flagstr[i]; i++) {
3015 3017 int bit = flagbits[(uchar_t)flagstr[i]];
3016 3018
3017 3019 if (bit == 0) {
3018 3020 (void) printf("***Invalid flag: %c\n",
3019 3021 flagstr[i]);
3020 3022 continue;
3021 3023 }
3022 3024 flags |= bit;
3023 3025
3024 3026 /* If it's not something with an argument, keep going */
3025 3027 if ((bit & (ZDB_FLAG_CHECKSUM |
3026 3028 ZDB_FLAG_PRINT_BLKPTR)) == 0)
3027 3029 continue;
3028 3030
3029 3031 p = &flagstr[i + 1];
3030 3032 if (bit == ZDB_FLAG_PRINT_BLKPTR)
3031 3033 blkptr_offset = strtoull(p, &p, 16);
3032 3034 if (*p != ':' && *p != '\0') {
3033 3035 (void) printf("***Invalid flag arg: '%s'\n", s);
3034 3036 free(dup);
3035 3037 return;
3036 3038 }
3037 3039 }
3038 3040 }
3039 3041
3040 3042 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
3041 3043 if (vd == NULL) {
3042 3044 (void) printf("***Invalid vdev: %s\n", vdev);
3043 3045 free(dup);
3044 3046 return;
3045 3047 } else {
3046 3048 if (vd->vdev_path)
3047 3049 (void) fprintf(stderr, "Found vdev: %s\n",
3048 3050 vd->vdev_path);
3049 3051 else
3050 3052 (void) fprintf(stderr, "Found vdev type: %s\n",
3051 3053 vd->vdev_ops->vdev_op_type);
3052 3054 }
3053 3055
3054 3056 psize = size;
3055 3057 lsize = size;
3056 3058
3057 3059 pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3058 3060 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3059 3061
3060 3062 BP_ZERO(bp);
3061 3063
3062 3064 DVA_SET_VDEV(&dva[0], vd->vdev_id);
3063 3065 DVA_SET_OFFSET(&dva[0], offset);
3064 3066 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3065 3067 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3066 3068
3067 3069 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3068 3070
3069 3071 BP_SET_LSIZE(bp, lsize);
3070 3072 BP_SET_PSIZE(bp, psize);
3071 3073 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3072 3074 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3073 3075 BP_SET_TYPE(bp, DMU_OT_NONE);
3074 3076 BP_SET_LEVEL(bp, 0);
3075 3077 BP_SET_DEDUP(bp, 0);
3076 3078 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
3077 3079
3078 3080 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3079 3081 zio = zio_root(spa, NULL, NULL, 0);
3080 3082
3081 3083 if (vd == vd->vdev_top) {
3082 3084 /*
3083 3085 * Treat this as a normal block read.
3084 3086 */
3085 3087 zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3086 3088 ZIO_PRIORITY_SYNC_READ,
3087 3089 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3088 3090 } else {
3089 3091 /*
3090 3092 * Treat this as a vdev child I/O.
3091 3093 */
3092 3094 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3093 3095 ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3094 3096 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3095 3097 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3096 3098 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3097 3099 }
3098 3100
3099 3101 error = zio_wait(zio);
3100 3102 spa_config_exit(spa, SCL_STATE, FTAG);
3101 3103
3102 3104 if (error) {
3103 3105 (void) printf("Read of %s failed, error: %d\n", thing, error);
3104 3106 goto out;
3105 3107 }
3106 3108
3107 3109 if (flags & ZDB_FLAG_DECOMPRESS) {
3108 3110 /*
3109 3111 * We don't know how the data was compressed, so just try
3110 3112 * every decompress function at every inflated blocksize.
3111 3113 */
3112 3114 enum zio_compress c;
3113 3115 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3114 3116 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3115 3117
3116 3118 bcopy(pbuf, pbuf2, psize);
3117 3119
3118 3120 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3119 3121 SPA_MAXBLOCKSIZE - psize) == 0);
3120 3122
3121 3123 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3122 3124 SPA_MAXBLOCKSIZE - psize) == 0);
3123 3125
3124 3126 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3125 3127 lsize -= SPA_MINBLOCKSIZE) {
3126 3128 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3127 3129 if (zio_decompress_data(c, pbuf, lbuf,
3128 3130 psize, lsize) == 0 &&
3129 3131 zio_decompress_data(c, pbuf2, lbuf2,
3130 3132 psize, lsize) == 0 &&
3131 3133 bcmp(lbuf, lbuf2, lsize) == 0)
3132 3134 break;
3133 3135 }
3134 3136 if (c != ZIO_COMPRESS_FUNCTIONS)
3135 3137 break;
3136 3138 lsize -= SPA_MINBLOCKSIZE;
3137 3139 }
3138 3140
3139 3141 umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3140 3142 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3141 3143
3142 3144 if (lsize <= psize) {
3143 3145 (void) printf("Decompress of %s failed\n", thing);
3144 3146 goto out;
3145 3147 }
3146 3148 buf = lbuf;
3147 3149 size = lsize;
3148 3150 } else {
3149 3151 buf = pbuf;
3150 3152 size = psize;
3151 3153 }
3152 3154
3153 3155 if (flags & ZDB_FLAG_PRINT_BLKPTR)
3154 3156 zdb_print_blkptr((blkptr_t *)(void *)
3155 3157 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
3156 3158 else if (flags & ZDB_FLAG_RAW)
3157 3159 zdb_dump_block_raw(buf, size, flags);
3158 3160 else if (flags & ZDB_FLAG_INDIRECT)
3159 3161 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
3160 3162 flags);
3161 3163 else if (flags & ZDB_FLAG_GBH)
3162 3164 zdb_dump_gbh(buf, flags);
3163 3165 else
3164 3166 zdb_dump_block(thing, buf, size, flags);
3165 3167
3166 3168 out:
3167 3169 umem_free(pbuf, SPA_MAXBLOCKSIZE);
3168 3170 umem_free(lbuf, SPA_MAXBLOCKSIZE);
3169 3171 free(dup);
3170 3172 }
3171 3173
3172 3174 static boolean_t
3173 3175 pool_match(nvlist_t *cfg, char *tgt)
3174 3176 {
3175 3177 uint64_t v, guid = strtoull(tgt, NULL, 0);
3176 3178 char *s;
3177 3179
3178 3180 if (guid != 0) {
3179 3181 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
3180 3182 return (v == guid);
3181 3183 } else {
3182 3184 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3183 3185 return (strcmp(s, tgt) == 0);
3184 3186 }
3185 3187 return (B_FALSE);
3186 3188 }
3187 3189
3188 3190 static char *
3189 3191 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3190 3192 {
3191 3193 nvlist_t *pools;
3192 3194 nvlist_t *match = NULL;
3193 3195 char *name = NULL;
3194 3196 char *sepp = NULL;
3195 3197 char sep;
3196 3198 int count = 0;
3197 3199 importargs_t args = { 0 };
3198 3200
3199 3201 args.paths = dirc;
3200 3202 args.path = dirv;
3201 3203 args.can_be_active = B_TRUE;
3202 3204
3203 3205 if ((sepp = strpbrk(*target, "/@")) != NULL) {
3204 3206 sep = *sepp;
3205 3207 *sepp = '\0';
3206 3208 }
3207 3209
3208 3210 pools = zpool_search_import(g_zfs, &args);
3209 3211
3210 3212 if (pools != NULL) {
3211 3213 nvpair_t *elem = NULL;
3212 3214 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3213 3215 verify(nvpair_value_nvlist(elem, configp) == 0);
3214 3216 if (pool_match(*configp, *target)) {
3215 3217 count++;
3216 3218 if (match != NULL) {
3217 3219 /* print previously found config */
3218 3220 if (name != NULL) {
3219 3221 (void) printf("%s\n", name);
3220 3222 dump_nvlist(match, 8);
3221 3223 name = NULL;
3222 3224 }
3223 3225 (void) printf("%s\n",
3224 3226 nvpair_name(elem));
3225 3227 dump_nvlist(*configp, 8);
3226 3228 } else {
3227 3229 match = *configp;
3228 3230 name = nvpair_name(elem);
3229 3231 }
3230 3232 }
3231 3233 }
3232 3234 }
3233 3235 if (count > 1)
3234 3236 (void) fatal("\tMatched %d pools - use pool GUID "
3235 3237 "instead of pool name or \n"
3236 3238 "\tpool name part of a dataset name to select pool", count);
3237 3239
3238 3240 if (sepp)
3239 3241 *sepp = sep;
3240 3242 /*
3241 3243 * If pool GUID was specified for pool id, replace it with pool name
3242 3244 */
3243 3245 if (name && (strstr(*target, name) != *target)) {
3244 3246 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
3245 3247
3246 3248 *target = umem_alloc(sz, UMEM_NOFAIL);
3247 3249 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
3248 3250 }
3249 3251
3250 3252 *configp = name ? match : NULL;
3251 3253
3252 3254 return (name);
3253 3255 }
3254 3256
3255 3257 int
3256 3258 main(int argc, char **argv)
3257 3259 {
3258 3260 int i, c;
3259 3261 struct rlimit rl = { 1024, 1024 };
3260 3262 spa_t *spa = NULL;
3261 3263 objset_t *os = NULL;
3262 3264 int dump_all = 1;
3263 3265 int verbose = 0;
3264 3266 int error = 0;
3265 3267 char **searchdirs = NULL;
3266 3268 int nsearch = 0;
3267 3269 char *target;
3268 3270 nvlist_t *policy = NULL;
3269 3271 uint64_t max_txg = UINT64_MAX;
3270 3272 int rewind = ZPOOL_NEVER_REWIND;
3271 3273
3272 3274 (void) setrlimit(RLIMIT_NOFILE, &rl);
3273 3275 (void) enable_extended_FILE_stdio(-1, -1);
3274 3276
3275 3277 dprintf_setup(&argc, argv);
3276 3278
3277 3279 while ((c = getopt(argc, argv, "bcdhilmM:suCDRSAFLXevp:t:U:P")) != -1) {
3278 3280 switch (c) {
3279 3281 case 'b':
3280 3282 case 'c':
3281 3283 case 'd':
3282 3284 case 'h':
3283 3285 case 'i':
3284 3286 case 'l':
3285 3287 case 'm':
3286 3288 case 's':
3287 3289 case 'u':
3288 3290 case 'C':
3289 3291 case 'D':
3290 3292 case 'R':
3291 3293 case 'S':
3292 3294 dump_opt[c]++;
3293 3295 dump_all = 0;
3294 3296 break;
3295 3297 case 'A':
3296 3298 case 'F':
3297 3299 case 'L':
3298 3300 case 'X':
3299 3301 case 'e':
3300 3302 case 'P':
3301 3303 dump_opt[c]++;
3302 3304 break;
3303 3305 case 'v':
3304 3306 verbose++;
3305 3307 break;
3306 3308 case 'M':
3307 3309 max_inflight = strtoull(optarg, NULL, 0);
3308 3310 if (max_inflight == 0) {
3309 3311 (void) fprintf(stderr, "maximum number "
3310 3312 "of inflight I/Os must be greater "
3311 3313 "than 0\n");
3312 3314 usage();
3313 3315 }
3314 3316 break;
3315 3317 case 'p':
3316 3318 if (searchdirs == NULL) {
3317 3319 searchdirs = umem_alloc(sizeof (char *),
3318 3320 UMEM_NOFAIL);
3319 3321 } else {
3320 3322 char **tmp = umem_alloc((nsearch + 1) *
3321 3323 sizeof (char *), UMEM_NOFAIL);
3322 3324 bcopy(searchdirs, tmp, nsearch *
3323 3325 sizeof (char *));
3324 3326 umem_free(searchdirs,
3325 3327 nsearch * sizeof (char *));
3326 3328 searchdirs = tmp;
3327 3329 }
3328 3330 searchdirs[nsearch++] = optarg;
3329 3331 break;
3330 3332 case 't':
3331 3333 max_txg = strtoull(optarg, NULL, 0);
3332 3334 if (max_txg < TXG_INITIAL) {
3333 3335 (void) fprintf(stderr, "incorrect txg "
3334 3336 "specified: %s\n", optarg);
3335 3337 usage();
3336 3338 }
3337 3339 break;
3338 3340 case 'U':
3339 3341 spa_config_path = optarg;
3340 3342 break;
3341 3343 default:
3342 3344 usage();
3343 3345 break;
3344 3346 }
3345 3347 }
3346 3348
3347 3349 if (!dump_opt['e'] && searchdirs != NULL) {
3348 3350 (void) fprintf(stderr, "-p option requires use of -e\n");
3349 3351 usage();
3350 3352 }
3351 3353
3352 3354 kernel_init(FREAD);
3353 3355 g_zfs = libzfs_init();
3354 3356 ASSERT(g_zfs != NULL);
3355 3357
3356 3358 if (dump_all)
3357 3359 verbose = MAX(verbose, 1);
3358 3360
3359 3361 for (c = 0; c < 256; c++) {
3360 3362 if (dump_all && !strchr("elAFLRSXP", c))
3361 3363 dump_opt[c] = 1;
3362 3364 if (dump_opt[c])
3363 3365 dump_opt[c] += verbose;
3364 3366 }
3365 3367
3366 3368 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3367 3369 zfs_recover = (dump_opt['A'] > 1);
3368 3370
3369 3371 argc -= optind;
3370 3372 argv += optind;
3371 3373
3372 3374 if (argc < 2 && dump_opt['R'])
3373 3375 usage();
3374 3376 if (argc < 1) {
3375 3377 if (!dump_opt['e'] && dump_opt['C']) {
3376 3378 dump_cachefile(spa_config_path);
3377 3379 return (0);
3378 3380 }
3379 3381 usage();
3380 3382 }
3381 3383
3382 3384 if (dump_opt['l']) {
3383 3385 dump_label(argv[0]);
3384 3386 return (0);
3385 3387 }
3386 3388
3387 3389 if (dump_opt['X'] || dump_opt['F'])
3388 3390 rewind = ZPOOL_DO_REWIND |
3389 3391 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3390 3392
3391 3393 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3392 3394 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3393 3395 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3394 3396 fatal("internal error: %s", strerror(ENOMEM));
3395 3397
3396 3398 error = 0;
3397 3399 target = argv[0];
3398 3400
3399 3401 if (dump_opt['e']) {
3400 3402 nvlist_t *cfg = NULL;
3401 3403 char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3402 3404
3403 3405 error = ENOENT;
3404 3406 if (name) {
3405 3407 if (dump_opt['C'] > 1) {
3406 3408 (void) printf("\nConfiguration for import:\n");
3407 3409 dump_nvlist(cfg, 8);
3408 3410 }
3409 3411 if (nvlist_add_nvlist(cfg,
3410 3412 ZPOOL_REWIND_POLICY, policy) != 0) {
3411 3413 fatal("can't open '%s': %s",
3412 3414 target, strerror(ENOMEM));
3413 3415 }
3414 3416 if ((error = spa_import(name, cfg, NULL,
3415 3417 ZFS_IMPORT_MISSING_LOG)) != 0) {
3416 3418 error = spa_import(name, cfg, NULL,
3417 3419 ZFS_IMPORT_VERBATIM);
3418 3420 }
3419 3421 }
3420 3422 }
3421 3423
3422 3424 if (error == 0) {
3423 3425 if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
3424 3426 error = spa_open_rewind(target, &spa, FTAG, policy,
3425 3427 NULL);
3426 3428 if (error) {
3427 3429 /*
3428 3430 * If we're missing the log device then
3429 3431 * try opening the pool after clearing the
3430 3432 * log state.
3431 3433 */
3432 3434 mutex_enter(&spa_namespace_lock);
3433 3435 if ((spa = spa_lookup(target)) != NULL &&
3434 3436 spa->spa_log_state == SPA_LOG_MISSING) {
3435 3437 spa->spa_log_state = SPA_LOG_CLEAR;
3436 3438 error = 0;
3437 3439 }
3438 3440 mutex_exit(&spa_namespace_lock);
3439 3441
3440 3442 if (!error) {
3441 3443 error = spa_open_rewind(target, &spa,
3442 3444 FTAG, policy, NULL);
3443 3445 }
3444 3446 }
3445 3447 } else {
3446 3448 error = dmu_objset_own(target, DMU_OST_ANY,
3447 3449 B_TRUE, FTAG, &os);
3448 3450 }
3449 3451 }
3450 3452 nvlist_free(policy);
3451 3453
3452 3454 if (error)
3453 3455 fatal("can't open '%s': %s", target, strerror(error));
3454 3456
3455 3457 argv++;
3456 3458 argc--;
3457 3459 if (!dump_opt['R']) {
3458 3460 if (argc > 0) {
3459 3461 zopt_objects = argc;
3460 3462 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3461 3463 for (i = 0; i < zopt_objects; i++) {
3462 3464 errno = 0;
3463 3465 zopt_object[i] = strtoull(argv[i], NULL, 0);
3464 3466 if (zopt_object[i] == 0 && errno != 0)
3465 3467 fatal("bad number %s: %s",
3466 3468 argv[i], strerror(errno));
3467 3469 }
3468 3470 }
3469 3471 if (os != NULL) {
3470 3472 dump_dir(os);
3471 3473 } else if (zopt_objects > 0 && !dump_opt['m']) {
3472 3474 dump_dir(spa->spa_meta_objset);
3473 3475 } else {
3474 3476 dump_zpool(spa);
3475 3477 }
3476 3478 } else {
3477 3479 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
3478 3480 flagbits['c'] = ZDB_FLAG_CHECKSUM;
3479 3481 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
3480 3482 flagbits['e'] = ZDB_FLAG_BSWAP;
3481 3483 flagbits['g'] = ZDB_FLAG_GBH;
3482 3484 flagbits['i'] = ZDB_FLAG_INDIRECT;
3483 3485 flagbits['p'] = ZDB_FLAG_PHYS;
3484 3486 flagbits['r'] = ZDB_FLAG_RAW;
3485 3487
3486 3488 for (i = 0; i < argc; i++)
3487 3489 zdb_read_block(argv[i], spa);
3488 3490 }
3489 3491
3490 3492 (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
3491 3493
3492 3494 fuid_table_destroy();
3493 3495 sa_loaded = B_FALSE;
3494 3496
3495 3497 libzfs_fini(g_zfs);
3496 3498 kernel_fini();
3497 3499
3498 3500 return (0);
3499 3501 }
↓ open down ↓ |
693 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX