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