1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25 #include <stdio.h>
26 #include <stdio_ext.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <sys/zfs_context.h>
30 #include <sys/spa.h>
31 #include <sys/spa_impl.h>
32 #include <sys/dmu.h>
33 #include <sys/zap.h>
34 #include <sys/fs/zfs.h>
35 #include <sys/zfs_znode.h>
36 #include <sys/zfs_sa.h>
37 #include <sys/sa.h>
38 #include <sys/sa_impl.h>
39 #include <sys/vdev.h>
40 #include <sys/vdev_impl.h>
41 #include <sys/metaslab_impl.h>
42 #include <sys/dmu_objset.h>
43 #include <sys/dsl_dir.h>
44 #include <sys/dsl_dataset.h>
45 #include <sys/dsl_pool.h>
46 #include <sys/dbuf.h>
47 #include <sys/zil.h>
48 #include <sys/zil_impl.h>
49 #include <sys/stat.h>
50 #include <sys/resource.h>
51 #include <sys/dmu_traverse.h>
52 #include <sys/zio_checksum.h>
53 #include <sys/zio_compress.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/arc.h>
56 #include <sys/ddt.h>
57 #undef ZFS_MAXNAMELEN
58 #undef verify
59 #include <libzfs.h>
60
61 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
62 zio_compress_table[(idx)].ci_name : "UNKNOWN")
63 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
64 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
65 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
66 dmu_ot[(idx)].ot_name : "UNKNOWN")
67 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : DMU_OT_NUMTYPES)
68
69 #ifndef lint
70 extern int zfs_recover;
71 #else
72 int zfs_recover;
73 #endif
74
75 const char cmdname[] = "zdb";
76 uint8_t dump_opt[256];
77
78 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
79
80 extern void dump_intent_log(zilog_t *);
81 uint64_t *zopt_object = NULL;
82 int zopt_objects = 0;
83 libzfs_handle_t *g_zfs;
84
85 /*
86 * These libumem hooks provide a reasonable set of defaults for the allocator's
1071 DO(CHILD);
1072 DO(CHILD_RSRV);
1073 DO(REFRSRV);
1074 #undef DO
1075 }
1076
1077 /*ARGSUSED*/
1078 static void
1079 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1080 {
1081 dsl_dataset_phys_t *ds = data;
1082 time_t crtime;
1083 char used[32], compressed[32], uncompressed[32], unique[32];
1084 char blkbuf[BP_SPRINTF_LEN];
1085
1086 if (ds == NULL)
1087 return;
1088
1089 ASSERT(size == sizeof (*ds));
1090 crtime = ds->ds_creation_time;
1091 zdb_nicenum(ds->ds_used_bytes, used);
1092 zdb_nicenum(ds->ds_compressed_bytes, compressed);
1093 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
1094 zdb_nicenum(ds->ds_unique_bytes, unique);
1095 sprintf_blkptr(blkbuf, &ds->ds_bp);
1096
1097 (void) printf("\t\tdir_obj = %llu\n",
1098 (u_longlong_t)ds->ds_dir_obj);
1099 (void) printf("\t\tprev_snap_obj = %llu\n",
1100 (u_longlong_t)ds->ds_prev_snap_obj);
1101 (void) printf("\t\tprev_snap_txg = %llu\n",
1102 (u_longlong_t)ds->ds_prev_snap_txg);
1103 (void) printf("\t\tnext_snap_obj = %llu\n",
1104 (u_longlong_t)ds->ds_next_snap_obj);
1105 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1106 (u_longlong_t)ds->ds_snapnames_zapobj);
1107 (void) printf("\t\tnum_children = %llu\n",
1108 (u_longlong_t)ds->ds_num_children);
1109 (void) printf("\t\tuserrefs_obj = %llu\n",
1110 (u_longlong_t)ds->ds_userrefs_obj);
1111 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1115 (u_longlong_t)ds->ds_deadlist_obj);
1116 (void) printf("\t\tused_bytes = %s\n", used);
1117 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1118 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1119 (void) printf("\t\tunique = %s\n", unique);
1120 (void) printf("\t\tfsid_guid = %llu\n",
1121 (u_longlong_t)ds->ds_fsid_guid);
1122 (void) printf("\t\tguid = %llu\n",
1123 (u_longlong_t)ds->ds_guid);
1124 (void) printf("\t\tflags = %llx\n",
1125 (u_longlong_t)ds->ds_flags);
1126 (void) printf("\t\tnext_clones_obj = %llu\n",
1127 (u_longlong_t)ds->ds_next_clones_obj);
1128 (void) printf("\t\tprops_obj = %llu\n",
1129 (u_longlong_t)ds->ds_props_obj);
1130 (void) printf("\t\tbp = %s\n", blkbuf);
1131 }
1132
1133 /* ARGSUSED */
1134 static int
1135 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1136 {
1137 char blkbuf[BP_SPRINTF_LEN];
1138
1139 ASSERT(bp->blk_birth != 0);
1140 sprintf_blkptr_compact(blkbuf, bp);
1141 (void) printf("\t%s\n", blkbuf);
1142 return (0);
1143 }
1144
1145 static void
1146 dump_bpobj(bpobj_t *bpo, char *name)
1147 {
1148 char bytes[32];
1149 char comp[32];
1150 char uncomp[32];
1151
1152 if (dump_opt['d'] < 3)
1153 return;
1154
1866 fuid_table_destroy();
1867 sa_loaded = B_FALSE;
1868 return (0);
1869 }
1870
1871 /*
1872 * Block statistics.
1873 */
1874 typedef struct zdb_blkstats {
1875 uint64_t zb_asize;
1876 uint64_t zb_lsize;
1877 uint64_t zb_psize;
1878 uint64_t zb_count;
1879 } zdb_blkstats_t;
1880
1881 /*
1882 * Extended object types to report deferred frees and dedup auto-ditto blocks.
1883 */
1884 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
1885 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
1886 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 2)
1887
1888 static char *zdb_ot_extname[] = {
1889 "deferred free",
1890 "dedup ditto",
1891 "Total",
1892 };
1893
1894 #define ZB_TOTAL DN_MAX_LEVELS
1895
1896 typedef struct zdb_cb {
1897 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
1898 uint64_t zcb_dedup_asize;
1899 uint64_t zcb_dedup_blocks;
1900 uint64_t zcb_errors[256];
1901 int zcb_readfails;
1902 int zcb_haderrors;
1903 spa_t *zcb_spa;
1904 } zdb_cb_t;
1905
1906 static void
1907 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
1908 dmu_object_type_t type)
1909 {
1910 uint64_t refcnt = 0;
1951 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
1952 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
1953 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
1954 }
1955
1956 /* ARGSUSED */
1957 static int
1958 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
1959 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1960 {
1961 zdb_cb_t *zcb = arg;
1962 char blkbuf[BP_SPRINTF_LEN];
1963 dmu_object_type_t type;
1964 boolean_t is_metadata;
1965
1966 if (bp == NULL)
1967 return (0);
1968
1969 type = BP_GET_TYPE(bp);
1970
1971 zdb_count_block(zcb, zilog, bp, type);
1972
1973 is_metadata = (BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata);
1974
1975 if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) {
1976 int ioerr;
1977 size_t size = BP_GET_PSIZE(bp);
1978 void *data = malloc(size);
1979 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
1980
1981 /* If it's an intent log block, failure is expected. */
1982 if (zb->zb_level == ZB_ZIL_LEVEL)
1983 flags |= ZIO_FLAG_SPECULATIVE;
1984
1985 ioerr = zio_wait(zio_read(NULL, spa, bp, data, size,
1986 NULL, NULL, ZIO_PRIORITY_ASYNC_READ, flags, zb));
1987
1988 free(data);
1989
1990 if (ioerr && !(flags & ZIO_FLAG_SPECULATIVE)) {
1991 zcb->zcb_haderrors = 1;
1992 zcb->zcb_errors[ioerr]++;
1993
2178 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2179 !dump_opt['L'] ? "nothing leaked " : "");
2180
2181 /*
2182 * Load all space maps as SM_ALLOC maps, then traverse the pool
2183 * claiming each block we discover. If the pool is perfectly
2184 * consistent, the space maps will be empty when we're done.
2185 * Anything left over is a leak; any block we can't claim (because
2186 * it's not part of any space map) is a double allocation,
2187 * reference to a freed block, or an unclaimed log block.
2188 */
2189 zdb_leak_init(spa, &zcb);
2190
2191 /*
2192 * If there's a deferred-free bplist, process that first.
2193 */
2194 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2195 count_block_cb, &zcb, NULL);
2196 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2197 count_block_cb, &zcb, NULL);
2198
2199 if (dump_opt['c'] > 1)
2200 flags |= TRAVERSE_PREFETCH_DATA;
2201
2202 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2203
2204 if (zcb.zcb_haderrors) {
2205 (void) printf("\nError counts:\n\n");
2206 (void) printf("\t%5s %s\n", "errno", "count");
2207 for (int e = 0; e < 256; e++) {
2208 if (zcb.zcb_errors[e] != 0) {
2209 (void) printf("\t%5d %llu\n",
2210 e, (u_longlong_t)zcb.zcb_errors[e]);
2211 }
2212 }
2213 }
2214
2215 /*
2216 * Report any leaked segments.
2217 */
2354 static int
2355 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2356 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2357 {
2358 avl_tree_t *t = arg;
2359 avl_index_t where;
2360 zdb_ddt_entry_t *zdde, zdde_search;
2361
2362 if (bp == NULL)
2363 return (0);
2364
2365 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2366 (void) printf("traversing objset %llu, %llu objects, "
2367 "%lu blocks so far\n",
2368 (u_longlong_t)zb->zb_objset,
2369 (u_longlong_t)bp->blk_fill,
2370 avl_numnodes(t));
2371 }
2372
2373 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2374 BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata)
2375 return (0);
2376
2377 ddt_key_fill(&zdde_search.zdde_key, bp);
2378
2379 zdde = avl_find(t, &zdde_search, &where);
2380
2381 if (zdde == NULL) {
2382 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2383 zdde->zdde_key = zdde_search.zdde_key;
2384 avl_insert(t, zdde, where);
2385 }
2386
2387 zdde->zdde_ref_blocks += 1;
2388 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2389 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2390 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2391
2392 return (0);
2393 }
2394
2459 }
2460
2461 if (dump_opt['C'])
2462 dump_config(spa);
2463
2464 if (dump_opt['u'])
2465 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2466
2467 if (dump_opt['D'])
2468 dump_all_ddts(spa);
2469
2470 if (dump_opt['d'] > 2 || dump_opt['m'])
2471 dump_metaslabs(spa);
2472
2473 if (dump_opt['d'] || dump_opt['i']) {
2474 dump_dir(dp->dp_meta_objset);
2475 if (dump_opt['d'] >= 3) {
2476 dump_bpobj(&spa->spa_deferred_bpobj, "Deferred frees");
2477 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2478 dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2479 "Pool frees");
2480 }
2481 dump_dtl(spa->spa_root_vdev, 0);
2482 }
2483 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
2484 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2485 }
2486 if (dump_opt['b'] || dump_opt['c'])
2487 rc = dump_block_stats(spa);
2488
2489 if (dump_opt['s'])
2490 show_pool_stats(spa);
2491
2492 if (dump_opt['h'])
2493 dump_history(spa);
2494
2495 if (rc != 0)
2496 exit(rc);
2497 }
2498
2499 #define ZDB_FLAG_CHECKSUM 0x0001
2500 #define ZDB_FLAG_DECOMPRESS 0x0002
|
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 */
26
27 #include <stdio.h>
28 #include <stdio_ext.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <sys/zfs_context.h>
32 #include <sys/spa.h>
33 #include <sys/spa_impl.h>
34 #include <sys/dmu.h>
35 #include <sys/zap.h>
36 #include <sys/fs/zfs.h>
37 #include <sys/zfs_znode.h>
38 #include <sys/zfs_sa.h>
39 #include <sys/sa.h>
40 #include <sys/sa_impl.h>
41 #include <sys/vdev.h>
42 #include <sys/vdev_impl.h>
43 #include <sys/metaslab_impl.h>
44 #include <sys/dmu_objset.h>
45 #include <sys/dsl_dir.h>
46 #include <sys/dsl_dataset.h>
47 #include <sys/dsl_pool.h>
48 #include <sys/dbuf.h>
49 #include <sys/zil.h>
50 #include <sys/zil_impl.h>
51 #include <sys/stat.h>
52 #include <sys/resource.h>
53 #include <sys/dmu_traverse.h>
54 #include <sys/zio_checksum.h>
55 #include <sys/zio_compress.h>
56 #include <sys/zfs_fuid.h>
57 #include <sys/arc.h>
58 #include <sys/ddt.h>
59 #include <sys/zfeature.h>
60 #undef ZFS_MAXNAMELEN
61 #undef verify
62 #include <libzfs.h>
63
64 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
65 zio_compress_table[(idx)].ci_name : "UNKNOWN")
66 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
67 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
68 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
69 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \
70 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
71 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : DMU_OT_NUMTYPES)
72
73 #ifndef lint
74 extern int zfs_recover;
75 #else
76 int zfs_recover;
77 #endif
78
79 const char cmdname[] = "zdb";
80 uint8_t dump_opt[256];
81
82 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
83
84 extern void dump_intent_log(zilog_t *);
85 uint64_t *zopt_object = NULL;
86 int zopt_objects = 0;
87 libzfs_handle_t *g_zfs;
88
89 /*
90 * These libumem hooks provide a reasonable set of defaults for the allocator's
1075 DO(CHILD);
1076 DO(CHILD_RSRV);
1077 DO(REFRSRV);
1078 #undef DO
1079 }
1080
1081 /*ARGSUSED*/
1082 static void
1083 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1084 {
1085 dsl_dataset_phys_t *ds = data;
1086 time_t crtime;
1087 char used[32], compressed[32], uncompressed[32], unique[32];
1088 char blkbuf[BP_SPRINTF_LEN];
1089
1090 if (ds == NULL)
1091 return;
1092
1093 ASSERT(size == sizeof (*ds));
1094 crtime = ds->ds_creation_time;
1095 zdb_nicenum(ds->ds_referenced_bytes, used);
1096 zdb_nicenum(ds->ds_compressed_bytes, compressed);
1097 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
1098 zdb_nicenum(ds->ds_unique_bytes, unique);
1099 sprintf_blkptr(blkbuf, &ds->ds_bp);
1100
1101 (void) printf("\t\tdir_obj = %llu\n",
1102 (u_longlong_t)ds->ds_dir_obj);
1103 (void) printf("\t\tprev_snap_obj = %llu\n",
1104 (u_longlong_t)ds->ds_prev_snap_obj);
1105 (void) printf("\t\tprev_snap_txg = %llu\n",
1106 (u_longlong_t)ds->ds_prev_snap_txg);
1107 (void) printf("\t\tnext_snap_obj = %llu\n",
1108 (u_longlong_t)ds->ds_next_snap_obj);
1109 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1110 (u_longlong_t)ds->ds_snapnames_zapobj);
1111 (void) printf("\t\tnum_children = %llu\n",
1112 (u_longlong_t)ds->ds_num_children);
1113 (void) printf("\t\tuserrefs_obj = %llu\n",
1114 (u_longlong_t)ds->ds_userrefs_obj);
1115 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1119 (u_longlong_t)ds->ds_deadlist_obj);
1120 (void) printf("\t\tused_bytes = %s\n", used);
1121 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1122 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1123 (void) printf("\t\tunique = %s\n", unique);
1124 (void) printf("\t\tfsid_guid = %llu\n",
1125 (u_longlong_t)ds->ds_fsid_guid);
1126 (void) printf("\t\tguid = %llu\n",
1127 (u_longlong_t)ds->ds_guid);
1128 (void) printf("\t\tflags = %llx\n",
1129 (u_longlong_t)ds->ds_flags);
1130 (void) printf("\t\tnext_clones_obj = %llu\n",
1131 (u_longlong_t)ds->ds_next_clones_obj);
1132 (void) printf("\t\tprops_obj = %llu\n",
1133 (u_longlong_t)ds->ds_props_obj);
1134 (void) printf("\t\tbp = %s\n", blkbuf);
1135 }
1136
1137 /* ARGSUSED */
1138 static int
1139 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1140 {
1141 char blkbuf[BP_SPRINTF_LEN];
1142
1143 if (bp->blk_birth != 0) {
1144 sprintf_blkptr(blkbuf, bp);
1145 (void) printf("\t%s\n", blkbuf);
1146 }
1147 return (0);
1148 }
1149
1150 static void
1151 dump_bptree(objset_t *os, uint64_t obj, char *name)
1152 {
1153 char bytes[32];
1154 bptree_phys_t *bt;
1155 dmu_buf_t *db;
1156
1157 if (dump_opt['d'] < 3)
1158 return;
1159
1160 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1161 bt = db->db_data;
1162 zdb_nicenum(bt->bt_bytes, bytes);
1163 (void) printf("\n %s: %llu datasets, %s\n",
1164 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1165 dmu_buf_rele(db, FTAG);
1166
1167 if (dump_opt['d'] < 5)
1168 return;
1169
1170 (void) printf("\n");
1171
1172 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1173 }
1174
1175 /* ARGSUSED */
1176 static int
1177 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1178 {
1179 char blkbuf[BP_SPRINTF_LEN];
1180
1181 ASSERT(bp->blk_birth != 0);
1182 sprintf_blkptr_compact(blkbuf, bp);
1183 (void) printf("\t%s\n", blkbuf);
1184 return (0);
1185 }
1186
1187 static void
1188 dump_bpobj(bpobj_t *bpo, char *name)
1189 {
1190 char bytes[32];
1191 char comp[32];
1192 char uncomp[32];
1193
1194 if (dump_opt['d'] < 3)
1195 return;
1196
1908 fuid_table_destroy();
1909 sa_loaded = B_FALSE;
1910 return (0);
1911 }
1912
1913 /*
1914 * Block statistics.
1915 */
1916 typedef struct zdb_blkstats {
1917 uint64_t zb_asize;
1918 uint64_t zb_lsize;
1919 uint64_t zb_psize;
1920 uint64_t zb_count;
1921 } zdb_blkstats_t;
1922
1923 /*
1924 * Extended object types to report deferred frees and dedup auto-ditto blocks.
1925 */
1926 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
1927 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
1928 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
1929 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
1930
1931 static char *zdb_ot_extname[] = {
1932 "deferred free",
1933 "dedup ditto",
1934 "other",
1935 "Total",
1936 };
1937
1938 #define ZB_TOTAL DN_MAX_LEVELS
1939
1940 typedef struct zdb_cb {
1941 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
1942 uint64_t zcb_dedup_asize;
1943 uint64_t zcb_dedup_blocks;
1944 uint64_t zcb_errors[256];
1945 int zcb_readfails;
1946 int zcb_haderrors;
1947 spa_t *zcb_spa;
1948 } zdb_cb_t;
1949
1950 static void
1951 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
1952 dmu_object_type_t type)
1953 {
1954 uint64_t refcnt = 0;
1995 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
1996 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
1997 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
1998 }
1999
2000 /* ARGSUSED */
2001 static int
2002 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
2003 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2004 {
2005 zdb_cb_t *zcb = arg;
2006 char blkbuf[BP_SPRINTF_LEN];
2007 dmu_object_type_t type;
2008 boolean_t is_metadata;
2009
2010 if (bp == NULL)
2011 return (0);
2012
2013 type = BP_GET_TYPE(bp);
2014
2015 zdb_count_block(zcb, zilog, bp,
2016 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2017
2018 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2019
2020 if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) {
2021 int ioerr;
2022 size_t size = BP_GET_PSIZE(bp);
2023 void *data = malloc(size);
2024 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2025
2026 /* If it's an intent log block, failure is expected. */
2027 if (zb->zb_level == ZB_ZIL_LEVEL)
2028 flags |= ZIO_FLAG_SPECULATIVE;
2029
2030 ioerr = zio_wait(zio_read(NULL, spa, bp, data, size,
2031 NULL, NULL, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2032
2033 free(data);
2034
2035 if (ioerr && !(flags & ZIO_FLAG_SPECULATIVE)) {
2036 zcb->zcb_haderrors = 1;
2037 zcb->zcb_errors[ioerr]++;
2038
2223 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2224 !dump_opt['L'] ? "nothing leaked " : "");
2225
2226 /*
2227 * Load all space maps as SM_ALLOC maps, then traverse the pool
2228 * claiming each block we discover. If the pool is perfectly
2229 * consistent, the space maps will be empty when we're done.
2230 * Anything left over is a leak; any block we can't claim (because
2231 * it's not part of any space map) is a double allocation,
2232 * reference to a freed block, or an unclaimed log block.
2233 */
2234 zdb_leak_init(spa, &zcb);
2235
2236 /*
2237 * If there's a deferred-free bplist, process that first.
2238 */
2239 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2240 count_block_cb, &zcb, NULL);
2241 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2242 count_block_cb, &zcb, NULL);
2243 if (spa_feature_is_active(spa,
2244 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
2245 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2246 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2247 &zcb, NULL));
2248 }
2249
2250 if (dump_opt['c'] > 1)
2251 flags |= TRAVERSE_PREFETCH_DATA;
2252
2253 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2254
2255 if (zcb.zcb_haderrors) {
2256 (void) printf("\nError counts:\n\n");
2257 (void) printf("\t%5s %s\n", "errno", "count");
2258 for (int e = 0; e < 256; e++) {
2259 if (zcb.zcb_errors[e] != 0) {
2260 (void) printf("\t%5d %llu\n",
2261 e, (u_longlong_t)zcb.zcb_errors[e]);
2262 }
2263 }
2264 }
2265
2266 /*
2267 * Report any leaked segments.
2268 */
2405 static int
2406 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2407 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2408 {
2409 avl_tree_t *t = arg;
2410 avl_index_t where;
2411 zdb_ddt_entry_t *zdde, zdde_search;
2412
2413 if (bp == NULL)
2414 return (0);
2415
2416 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2417 (void) printf("traversing objset %llu, %llu objects, "
2418 "%lu blocks so far\n",
2419 (u_longlong_t)zb->zb_objset,
2420 (u_longlong_t)bp->blk_fill,
2421 avl_numnodes(t));
2422 }
2423
2424 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2425 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2426 return (0);
2427
2428 ddt_key_fill(&zdde_search.zdde_key, bp);
2429
2430 zdde = avl_find(t, &zdde_search, &where);
2431
2432 if (zdde == NULL) {
2433 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2434 zdde->zdde_key = zdde_search.zdde_key;
2435 avl_insert(t, zdde, where);
2436 }
2437
2438 zdde->zdde_ref_blocks += 1;
2439 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2440 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2441 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2442
2443 return (0);
2444 }
2445
2510 }
2511
2512 if (dump_opt['C'])
2513 dump_config(spa);
2514
2515 if (dump_opt['u'])
2516 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2517
2518 if (dump_opt['D'])
2519 dump_all_ddts(spa);
2520
2521 if (dump_opt['d'] > 2 || dump_opt['m'])
2522 dump_metaslabs(spa);
2523
2524 if (dump_opt['d'] || dump_opt['i']) {
2525 dump_dir(dp->dp_meta_objset);
2526 if (dump_opt['d'] >= 3) {
2527 dump_bpobj(&spa->spa_deferred_bpobj, "Deferred frees");
2528 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2529 dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2530 "Pool snapshot frees");
2531 }
2532
2533 if (spa_feature_is_active(spa,
2534 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
2535 dump_bptree(spa->spa_meta_objset,
2536 spa->spa_dsl_pool->dp_bptree_obj,
2537 "Pool dataset frees");
2538 }
2539 dump_dtl(spa->spa_root_vdev, 0);
2540 }
2541 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
2542 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2543 }
2544 if (dump_opt['b'] || dump_opt['c'])
2545 rc = dump_block_stats(spa);
2546
2547 if (dump_opt['s'])
2548 show_pool_stats(spa);
2549
2550 if (dump_opt['h'])
2551 dump_history(spa);
2552
2553 if (rc != 0)
2554 exit(rc);
2555 }
2556
2557 #define ZDB_FLAG_CHECKSUM 0x0001
2558 #define ZDB_FLAG_DECOMPRESS 0x0002
|