xref: /freebsd/sys/contrib/openzfs/lib/libzdb/libzdb.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include <getopt.h>
6 #include <openssl/evp.h>
7 #include <sys/zfs_context.h>
8 #include <sys/spa.h>
9 #include <sys/spa_impl.h>
10 #include <sys/dmu.h>
11 #include <sys/zap.h>
12 #include <sys/fs/zfs.h>
13 #include <sys/zfs_znode.h>
14 #include <sys/zfs_sa.h>
15 #include <sys/sa.h>
16 #include <sys/sa_impl.h>
17 #include <sys/vdev.h>
18 #include <sys/vdev_impl.h>
19 #include <sys/metaslab_impl.h>
20 #include <sys/dmu_objset.h>
21 #include <sys/dsl_dir.h>
22 #include <sys/dsl_dataset.h>
23 #include <sys/dsl_pool.h>
24 #include <sys/dsl_bookmark.h>
25 #include <sys/dbuf.h>
26 #include <sys/zil.h>
27 #include <sys/zil_impl.h>
28 #include <sys/stat.h>
29 #include <sys/resource.h>
30 #include <sys/dmu_send.h>
31 #include <sys/dmu_traverse.h>
32 #include <sys/zio_checksum.h>
33 #include <sys/zio_compress.h>
34 #include <sys/zfs_fuid.h>
35 #include <sys/arc.h>
36 #include <sys/arc_impl.h>
37 #include <sys/ddt.h>
38 #include <sys/zfeature.h>
39 #include <sys/abd.h>
40 #include <sys/blkptr.h>
41 #include <sys/dsl_crypt.h>
42 #include <sys/dsl_scan.h>
43 #include <sys/btree.h>
44 #include <sys/brt.h>
45 #include <sys/brt_impl.h>
46 #include <zfs_comutil.h>
47 #include <sys/zstd/zstd.h>
48 
49 #include <libnvpair.h>
50 #include <libzutil.h>
51 
52 #include <libzdb.h>
53 
54 const char *
55 zdb_ot_name(dmu_object_type_t type)
56 {
57 	if (type < DMU_OT_NUMTYPES)
58 		return (dmu_ot[type].ot_name);
59 	else if ((type & DMU_OT_NEWTYPE) &&
60 	    ((type & DMU_OT_BYTESWAP_MASK) < DMU_BSWAP_NUMFUNCS))
61 		return (dmu_ot_byteswap[type & DMU_OT_BYTESWAP_MASK].ob_name);
62 	else
63 		return ("UNKNOWN");
64 }
65 
66 int
67 livelist_compare(const void *larg, const void *rarg)
68 {
69 	const blkptr_t *l = larg;
70 	const blkptr_t *r = rarg;
71 
72 	/* Sort them according to dva[0] */
73 	uint64_t l_dva0_vdev, r_dva0_vdev;
74 	l_dva0_vdev = DVA_GET_VDEV(&l->blk_dva[0]);
75 	r_dva0_vdev = DVA_GET_VDEV(&r->blk_dva[0]);
76 	if (l_dva0_vdev < r_dva0_vdev)
77 		return (-1);
78 	else if (l_dva0_vdev > r_dva0_vdev)
79 		return (+1);
80 
81 	/* if vdevs are equal, sort by offsets. */
82 	uint64_t l_dva0_offset;
83 	uint64_t r_dva0_offset;
84 	l_dva0_offset = DVA_GET_OFFSET(&l->blk_dva[0]);
85 	r_dva0_offset = DVA_GET_OFFSET(&r->blk_dva[0]);
86 	if (l_dva0_offset < r_dva0_offset) {
87 		return (-1);
88 	} else if (l_dva0_offset > r_dva0_offset) {
89 		return (+1);
90 	}
91 
92 	/*
93 	 * Since we're storing blkptrs without cancelling FREE/ALLOC pairs,
94 	 * it's possible the offsets are equal. In that case, sort by txg
95 	 */
96 	if (BP_GET_LOGICAL_BIRTH(l) < BP_GET_LOGICAL_BIRTH(r)) {
97 		return (-1);
98 	} else if (BP_GET_LOGICAL_BIRTH(l) > BP_GET_LOGICAL_BIRTH(r)) {
99 		return (+1);
100 	}
101 	return (0);
102 }
103