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