Skip to content

Commit a0d3fe7

Browse files
authored
libzdb: Initial breakout of libzdb
Step 1 in trying to slowly rip the zdb functions out of zdb.c to allow people to play with more flexible things to leverage zdb's functionality. No promises on any functions or structs being stable, now or probably in general unless someone builds a more polished abstraction, the goal at the moment is to slowly untangle the global state usage in zdb... Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Rich Ercolani <[email protected]> Closes #15804
1 parent 06e25f9 commit a0d3fe7

File tree

7 files changed

+184
-108
lines changed

7 files changed

+184
-108
lines changed

cmd/zdb/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ zdb_SOURCES = \
1010
%D%/zdb_il.c
1111

1212
zdb_LDADD = \
13+
libzdb.la \
1314
libzpool.la \
1415
libzfs_core.la \
1516
libnvpair.la

cmd/zdb/zdb.c

+2-106
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,10 @@
8888
#include <libnvpair.h>
8989
#include <libzutil.h>
9090

91-
#include "zdb.h"
91+
#include <libzdb.h>
9292

93-
#define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
94-
zio_compress_table[(idx)].ci_name : "UNKNOWN")
95-
#define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
96-
zio_checksum_table[(idx)].ci_name : "UNKNOWN")
97-
#define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
98-
(idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ? \
99-
DMU_OT_ZAP_OTHER : \
100-
(idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
101-
DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
102-
103-
/* Some platforms require part of inode IDs to be remapped */
104-
#ifdef __APPLE__
105-
#define ZDB_MAP_OBJECT_ID(obj) INO_XNUTOZFS(obj, 2)
106-
#else
107-
#define ZDB_MAP_OBJECT_ID(obj) (obj)
108-
#endif
93+
#include "zdb.h"
10994

110-
static const char *
111-
zdb_ot_name(dmu_object_type_t type)
112-
{
113-
if (type < DMU_OT_NUMTYPES)
114-
return (dmu_ot[type].ot_name);
115-
else if ((type & DMU_OT_NEWTYPE) &&
116-
((type & DMU_OT_BYTESWAP_MASK) < DMU_BSWAP_NUMFUNCS))
117-
return (dmu_ot_byteswap[type & DMU_OT_BYTESWAP_MASK].ob_name);
118-
else
119-
return ("UNKNOWN");
120-
}
12195

12296
extern int reference_tracking_enable;
12397
extern int zfs_recover;
@@ -135,35 +109,12 @@ typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
135109
static uint64_t *zopt_metaslab = NULL;
136110
static unsigned zopt_metaslab_args = 0;
137111

138-
typedef struct zopt_object_range {
139-
uint64_t zor_obj_start;
140-
uint64_t zor_obj_end;
141-
uint64_t zor_flags;
142-
} zopt_object_range_t;
143112

144113
static zopt_object_range_t *zopt_object_ranges = NULL;
145114
static unsigned zopt_object_args = 0;
146115

147116
static int flagbits[256];
148117

149-
#define ZOR_FLAG_PLAIN_FILE 0x0001
150-
#define ZOR_FLAG_DIRECTORY 0x0002
151-
#define ZOR_FLAG_SPACE_MAP 0x0004
152-
#define ZOR_FLAG_ZAP 0x0008
153-
#define ZOR_FLAG_ALL_TYPES -1
154-
#define ZOR_SUPPORTED_FLAGS (ZOR_FLAG_PLAIN_FILE | \
155-
ZOR_FLAG_DIRECTORY | \
156-
ZOR_FLAG_SPACE_MAP | \
157-
ZOR_FLAG_ZAP)
158-
159-
#define ZDB_FLAG_CHECKSUM 0x0001
160-
#define ZDB_FLAG_DECOMPRESS 0x0002
161-
#define ZDB_FLAG_BSWAP 0x0004
162-
#define ZDB_FLAG_GBH 0x0008
163-
#define ZDB_FLAG_INDIRECT 0x0010
164-
#define ZDB_FLAG_RAW 0x0020
165-
#define ZDB_FLAG_PRINT_BLKPTR 0x0040
166-
#define ZDB_FLAG_VERBOSE 0x0080
167118

168119
static uint64_t max_inflight_bytes = 256 * 1024 * 1024; /* 256MB */
169120
static int leaked_objects = 0;
@@ -176,62 +127,7 @@ static void mos_obj_refd_multiple(uint64_t);
176127
static int dump_bpobj_cb(void *arg, const blkptr_t *bp, boolean_t free,
177128
dmu_tx_t *tx);
178129

179-
typedef struct sublivelist_verify {
180-
/* FREE's that haven't yet matched to an ALLOC, in one sub-livelist */
181-
zfs_btree_t sv_pair;
182-
183-
/* ALLOC's without a matching FREE, accumulates across sub-livelists */
184-
zfs_btree_t sv_leftover;
185-
} sublivelist_verify_t;
186-
187-
static int
188-
livelist_compare(const void *larg, const void *rarg)
189-
{
190-
const blkptr_t *l = larg;
191-
const blkptr_t *r = rarg;
192130

193-
/* Sort them according to dva[0] */
194-
uint64_t l_dva0_vdev, r_dva0_vdev;
195-
l_dva0_vdev = DVA_GET_VDEV(&l->blk_dva[0]);
196-
r_dva0_vdev = DVA_GET_VDEV(&r->blk_dva[0]);
197-
if (l_dva0_vdev < r_dva0_vdev)
198-
return (-1);
199-
else if (l_dva0_vdev > r_dva0_vdev)
200-
return (+1);
201-
202-
/* if vdevs are equal, sort by offsets. */
203-
uint64_t l_dva0_offset;
204-
uint64_t r_dva0_offset;
205-
l_dva0_offset = DVA_GET_OFFSET(&l->blk_dva[0]);
206-
r_dva0_offset = DVA_GET_OFFSET(&r->blk_dva[0]);
207-
if (l_dva0_offset < r_dva0_offset) {
208-
return (-1);
209-
} else if (l_dva0_offset > r_dva0_offset) {
210-
return (+1);
211-
}
212-
213-
/*
214-
* Since we're storing blkptrs without cancelling FREE/ALLOC pairs,
215-
* it's possible the offsets are equal. In that case, sort by txg
216-
*/
217-
if (l->blk_birth < r->blk_birth) {
218-
return (-1);
219-
} else if (l->blk_birth > r->blk_birth) {
220-
return (+1);
221-
}
222-
return (0);
223-
}
224-
225-
typedef struct sublivelist_verify_block {
226-
dva_t svb_dva;
227-
228-
/*
229-
* We need this to check if the block marked as allocated
230-
* in the livelist was freed (and potentially reallocated)
231-
* in the metaslab spacemaps at a later TXG.
232-
*/
233-
uint64_t svb_allocated_txg;
234-
} sublivelist_verify_block_t;
235131

236132
static void zdb_print_blkptr(const blkptr_t *bp, int flags);
237133

include/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ USER_H = \
186186
libuutil.h \
187187
libuutil_common.h \
188188
libuutil_impl.h \
189+
libzdb.h \
189190
libzfs.h \
190191
libzfs_core.h \
191192
libzfsbootenv.h \

include/libzdb.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
2+
zio_compress_table[(idx)].ci_name : "UNKNOWN")
3+
#define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
4+
zio_checksum_table[(idx)].ci_name : "UNKNOWN")
5+
#define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
6+
(idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ? \
7+
DMU_OT_ZAP_OTHER : \
8+
(idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
9+
DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
10+
11+
/* Some platforms require part of inode IDs to be remapped */
12+
#ifdef __APPLE__
13+
#define ZDB_MAP_OBJECT_ID(obj) INO_XNUTOZFS(obj, 2)
14+
#else
15+
#define ZDB_MAP_OBJECT_ID(obj) (obj)
16+
#endif
17+
18+
#define ZOR_FLAG_PLAIN_FILE 0x0001
19+
#define ZOR_FLAG_DIRECTORY 0x0002
20+
#define ZOR_FLAG_SPACE_MAP 0x0004
21+
#define ZOR_FLAG_ZAP 0x0008
22+
#define ZOR_FLAG_ALL_TYPES -1
23+
#define ZOR_SUPPORTED_FLAGS (ZOR_FLAG_PLAIN_FILE | \
24+
ZOR_FLAG_DIRECTORY | \
25+
ZOR_FLAG_SPACE_MAP | \
26+
ZOR_FLAG_ZAP)
27+
28+
#define ZDB_FLAG_CHECKSUM 0x0001
29+
#define ZDB_FLAG_DECOMPRESS 0x0002
30+
#define ZDB_FLAG_BSWAP 0x0004
31+
#define ZDB_FLAG_GBH 0x0008
32+
#define ZDB_FLAG_INDIRECT 0x0010
33+
#define ZDB_FLAG_RAW 0x0020
34+
#define ZDB_FLAG_PRINT_BLKPTR 0x0040
35+
#define ZDB_FLAG_VERBOSE 0x0080
36+
37+
38+
typedef struct zdb_ctx {
39+
} zdb_ctx_t;
40+
41+
typedef struct zopt_object_range {
42+
uint64_t zor_obj_start;
43+
uint64_t zor_obj_end;
44+
uint64_t zor_flags;
45+
} zopt_object_range_t;
46+
47+
48+
typedef struct sublivelist_verify {
49+
/* FREE's that haven't yet matched to an ALLOC, in one sub-livelist */
50+
zfs_btree_t sv_pair;
51+
52+
/* ALLOC's without a matching FREE, accumulates across sub-livelists */
53+
zfs_btree_t sv_leftover;
54+
} sublivelist_verify_t;
55+
56+
typedef struct sublivelist_verify_block {
57+
dva_t svb_dva;
58+
59+
/*
60+
* We need this to check if the block marked as allocated
61+
* in the livelist was freed (and potentially reallocated)
62+
* in the metaslab spacemaps at a later TXG.
63+
*/
64+
uint64_t svb_allocated_txg;
65+
} sublivelist_verify_block_t;
66+
67+
const char *zdb_ot_name(dmu_object_type_t type);
68+
int livelist_compare(const void *larg, const void *rarg);

lib/Makefile.am

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
# These library interfaces are subject to change at any time.
1010
#
1111
#
12-
# CMDS: zhack/ztest/zdb/ zfs/zpool/zed/
12+
# CMDS: zhack/ztest/ zfs/zpool/zed/
1313
# raidz_{test,bench} zinject/zstream
1414
# | |
1515
# LIBS: | | libzfsbootenv*
16-
# | | |
16+
# |--libzdb--zdb | |
1717
# | | |
1818
# libzpool libzfs* ----------------+
1919
# | | | \ / | | |
@@ -62,6 +62,7 @@ include $(srcdir)/%D%/libspl/Makefile.am
6262
include $(srcdir)/%D%/libtpool/Makefile.am
6363
include $(srcdir)/%D%/libunicode/Makefile.am
6464
include $(srcdir)/%D%/libuutil/Makefile.am
65+
include $(srcdir)/%D%/libzdb/Makefile.am
6566
include $(srcdir)/%D%/libzfs_core/Makefile.am
6667
include $(srcdir)/%D%/libzfs/Makefile.am
6768
include $(srcdir)/%D%/libzfsbootenv/Makefile.am

lib/libzdb/Makefile.am

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
libzdb_la_CFLAGS = $(AM_CFLAGS) $(LIBRARY_CFLAGS)
2+
libzdb_la_CFLAGS += -fvisibility=hidden
3+
4+
noinst_LTLIBRARIES += libzdb.la
5+
6+
libzdb_la_SOURCES = \
7+
%D%/libzdb.c

lib/libzdb/libzdb.c

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 (l->blk_birth < r->blk_birth) {
97+
return (-1);
98+
} else if (l->blk_birth > r->blk_birth) {
99+
return (+1);
100+
}
101+
return (0);
102+
}

0 commit comments

Comments
 (0)