Skip to content

Commit 50388ef

Browse files
committed
Zpool: uses runtime lookup for zpool properties to ensure portability
Fixes #2051
1 parent 7e8fb0f commit 50388ef

File tree

2 files changed

+47
-59
lines changed

2 files changed

+47
-59
lines changed

src/detection/zpool/libzfs_simplified.h

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,65 +25,23 @@
2525
* CDDL HEADER END
2626
*/
2727

28-
typedef enum {
29-
ZPOOL_PROP_INVAL = -1,
30-
ZPOOL_PROP_NAME,
31-
ZPOOL_PROP_SIZE,
32-
ZPOOL_PROP_CAPACITY,
33-
ZPOOL_PROP_ALTROOT,
34-
ZPOOL_PROP_HEALTH,
35-
ZPOOL_PROP_GUID,
36-
ZPOOL_PROP_VERSION,
37-
ZPOOL_PROP_BOOTFS,
38-
ZPOOL_PROP_DELEGATION,
39-
ZPOOL_PROP_AUTOREPLACE,
40-
ZPOOL_PROP_CACHEFILE,
41-
ZPOOL_PROP_FAILUREMODE,
42-
ZPOOL_PROP_LISTSNAPS,
43-
ZPOOL_PROP_AUTOEXPAND,
44-
ZPOOL_PROP_DEDUPDITTO,
45-
ZPOOL_PROP_DEDUPRATIO,
46-
ZPOOL_PROP_FREE,
47-
ZPOOL_PROP_ALLOCATED,
48-
ZPOOL_PROP_READONLY,
49-
ZPOOL_PROP_ASHIFT,
50-
ZPOOL_PROP_COMMENT,
51-
ZPOOL_PROP_EXPANDSZ,
52-
ZPOOL_PROP_FREEING,
53-
ZPOOL_PROP_FRAGMENTATION,
54-
ZPOOL_PROP_LEAKED,
55-
ZPOOL_PROP_MAXBLOCKSIZE,
56-
ZPOOL_PROP_TNAME,
57-
ZPOOL_PROP_MAXDNODESIZE,
58-
ZPOOL_PROP_MULTIHOST,
59-
ZPOOL_PROP_CHECKPOINT,
60-
ZPOOL_PROP_LOAD_GUID,
61-
ZPOOL_PROP_AUTOTRIM,
62-
ZPOOL_PROP_COMPATIBILITY,
63-
ZPOOL_PROP_BCLONEUSED,
64-
ZPOOL_PROP_BCLONESAVED,
65-
ZPOOL_PROP_BCLONERATIO,
66-
ZPOOL_NUM_PROPS
67-
} zpool_prop_t;
68-
69-
typedef enum {
70-
ZPROP_SRC_NONE = 0x1,
71-
ZPROP_SRC_DEFAULT = 0x2,
72-
ZPROP_SRC_TEMPORARY = 0x4,
73-
ZPROP_SRC_LOCAL = 0x8,
74-
ZPROP_SRC_INHERITED = 0x10,
75-
ZPROP_SRC_RECEIVED = 0x20
76-
} zprop_source_t;
77-
28+
// zpool_prop_t and zprop_source_t were previously enums in upstream OpenZFS.
29+
// However, the enum values for these types vary greatly between different platforms,
30+
// making it unsafe to use the enum values directly. To ensure portability,
31+
// we define them as simple int typedefs and use zpool_name_to_prop to look up
32+
// the correct value for a property at runtime.
33+
typedef int zpool_prop_t;
34+
typedef int zprop_source_t;
7835
typedef bool boolean_t;
7936

8037
typedef struct libzfs_handle libzfs_handle_t;
8138
typedef struct zpool_handle zpool_handle_t;
8239
typedef int (*zpool_iter_f)(zpool_handle_t *, void *);
83-
extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *);
8440

8541
extern libzfs_handle_t *libzfs_init(void);
8642
extern void libzfs_fini(libzfs_handle_t *);
43+
extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *);
44+
extern zpool_prop_t zpool_name_to_prop(const char *);
8745
// https://github.com/openzfs/zfs/blob/06c73cffabc30b61a695988ec8e290f43cb3768d/lib/libzfs/libzfs_pool.c#L300
8846
extern uint64_t zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *srctype);
8947
extern int zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, zprop_source_t *srctype, boolean_t literal);

src/detection/zpool/zpool_linux.c

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ typedef struct FFZfsData
1818
FF_LIBRARY_SYMBOL(zpool_get_prop)
1919
FF_LIBRARY_SYMBOL(zpool_close)
2020

21+
// The fields in this struct store property IDs returned by `zpool_name_to_prop`,
22+
// not the property values themselves.
23+
struct {
24+
int name;
25+
int health;
26+
int guid;
27+
int size;
28+
int free;
29+
int allocated;
30+
int fragmentation;
31+
int readonly;
32+
} props;
33+
2134
libzfs_handle_t* handle;
2235
FFlist* result;
2336
} FFZfsData;
@@ -37,21 +50,21 @@ static int enumZpoolCallback(zpool_handle_t* zpool, void* param)
3750
zprop_source_t source;
3851
FFZpoolResult* item = ffListAdd(data->result);
3952
char buf[1024];
40-
if (data->ffzpool_get_prop(zpool, ZPOOL_PROP_NAME, buf, ARRAY_SIZE(buf), &source, false) == 0)
53+
if (data->ffzpool_get_prop(zpool, data->props.name, buf, ARRAY_SIZE(buf), &source, false) == 0)
4154
ffStrbufInitS(&item->name, buf);
4255
else
4356
ffStrbufInitStatic(&item->name, "unknown");
44-
if (data->ffzpool_get_prop(zpool, ZPOOL_PROP_HEALTH, buf, ARRAY_SIZE(buf), &source, false) == 0)
57+
if (data->ffzpool_get_prop(zpool, data->props.health, buf, ARRAY_SIZE(buf), &source, false) == 0)
4558
ffStrbufInitS(&item->state, buf);
4659
else
4760
ffStrbufInitStatic(&item->state, "unknown");
48-
item->guid = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_GUID, &source);
49-
item->total = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_SIZE, &source);
50-
item->used = item->total - data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_FREE, &source);
51-
item->allocated = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_ALLOCATED, &source);
52-
uint64_t fragmentation = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_FRAGMENTATION, &source);
61+
item->guid = data->ffzpool_get_prop_int(zpool, data->props.guid, &source);
62+
item->total = data->ffzpool_get_prop_int(zpool, data->props.size, &source);
63+
item->used = item->total - data->ffzpool_get_prop_int(zpool, data->props.free, &source);
64+
item->allocated = data->ffzpool_get_prop_int(zpool, data->props.allocated, &source);
65+
uint64_t fragmentation = data->ffzpool_get_prop_int(zpool, data->props.fragmentation, &source);
5366
item->fragmentation = fragmentation == UINT64_MAX ? -DBL_MAX : (double) fragmentation;
54-
item->readOnly = (bool) data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_READONLY, &source);
67+
item->readOnly = (bool) data->ffzpool_get_prop_int(zpool, data->props.readonly, &source);
5568
data->ffzpool_close(zpool);
5669
return 0;
5770
}
@@ -73,6 +86,23 @@ const char* ffDetectZpool(FFlist* result /* list of FFZpoolResult */)
7386
.result = result,
7487
};
7588

89+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libzfs, zpool_name_to_prop);
90+
91+
#define FF_QUERY_ZPOOL_PROP_FROM_NAME(prop_name) do { \
92+
data.props.prop_name = ffzpool_name_to_prop(#prop_name); \
93+
if (data.props.prop_name < 0) \
94+
return "Failed to query prop: " #prop_name; \
95+
} while (false)
96+
FF_QUERY_ZPOOL_PROP_FROM_NAME(name);
97+
FF_QUERY_ZPOOL_PROP_FROM_NAME(health);
98+
FF_QUERY_ZPOOL_PROP_FROM_NAME(guid);
99+
FF_QUERY_ZPOOL_PROP_FROM_NAME(size);
100+
FF_QUERY_ZPOOL_PROP_FROM_NAME(free);
101+
FF_QUERY_ZPOOL_PROP_FROM_NAME(allocated);
102+
FF_QUERY_ZPOOL_PROP_FROM_NAME(fragmentation);
103+
FF_QUERY_ZPOOL_PROP_FROM_NAME(readonly);
104+
#undef FF_QUERY_ZPOOL_PROP_FROM_NAME
105+
76106
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libzfs, zpool_iter);
77107
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, libzfs_fini);
78108
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libzfs, data, zpool_get_prop_int);

0 commit comments

Comments
 (0)