Skip to content

Commit d98c26d

Browse files
committed
Zpool: add NetBSD support
Signed-off-by: hoshinomori <hoshinomori@owarisekai.moe>
1 parent e04cc9c commit d98c26d

File tree

3 files changed

+245
-2
lines changed

3 files changed

+245
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ cmake_dependent_option(ENABLE_DDCUTIL "Enable ddcutil" ON "LINUX" OFF)
9292
cmake_dependent_option(ENABLE_DIRECTX_HEADERS "Enable DirectX headers for WSL" ON "LINUX" OFF)
9393
cmake_dependent_option(ENABLE_ELF "Enable libelf" ON "LINUX OR ANDROID OR DragonFly OR Haiku OR GNU" OFF)
9494
cmake_dependent_option(ENABLE_THREADS "Enable multithreading" ON "Threads_FOUND" OFF)
95-
cmake_dependent_option(ENABLE_LIBZFS "Enable libzfs" ON "LINUX OR FreeBSD OR SunOS" OFF)
95+
cmake_dependent_option(ENABLE_LIBZFS "Enable libzfs" ON "LINUX OR FreeBSD OR NetBSD OR SunOS" OFF)
9696
cmake_dependent_option(ENABLE_PCIACCESS "Enable libpciaccess" ON "GNU" OFF)
9797

9898
option(ENABLE_ZLIB "Enable zlib" ON)
@@ -1726,6 +1726,7 @@ elseif(NetBSD)
17261726
PRIVATE "bluetooth"
17271727
PRIVATE "m"
17281728
PRIVATE "prop"
1729+
PRIVATE $<$<BOOL:${ENABLE_LIBZFS}>:zfs;nvpair>
17291730
)
17301731
elseif(SunOS)
17311732
target_link_libraries(libfastfetch
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#pragma once
2+
3+
#include "fastfetch.h"
4+
5+
/*
6+
* CDDL HEADER START
7+
*
8+
* The contents of this file are subject to the terms of the
9+
* Common Development and Distribution License (the "License").
10+
* You may not use this file except in compliance with the License.
11+
*
12+
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
13+
* or https://opensource.org/licenses/CDDL-1.0.
14+
* See the License for the specific language governing permissions
15+
* and limitations under the License.
16+
*
17+
* When distributing Covered Code, include this CDDL HEADER in each
18+
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
19+
* If applicable, add the following below this CDDL HEADER, with the
20+
* fields enclosed by brackets "[]" replaced with your own identifying
21+
* information: Portions Copyright [yyyy] [name of copyright owner]
22+
*
23+
* CDDL HEADER END
24+
*/
25+
26+
// From https://github.com/NetBSD/src/blob/trunk/external/cddl/osnet/sys/sys/opentypes.h
27+
28+
typedef longlong_t hrtime_t;
29+
typedef unsigned int uint_t;
30+
typedef bool boolean_t;
31+
32+
// From https://github.com/NetBSD/src/blob/trunk/external/cddl/osnet/dist/uts/common/sys/fs/zfs.h
33+
34+
typedef enum {
35+
ZPOOL_PROP_NAME,
36+
ZPOOL_PROP_SIZE,
37+
ZPOOL_PROP_CAPACITY,
38+
ZPOOL_PROP_ALTROOT,
39+
ZPOOL_PROP_HEALTH,
40+
ZPOOL_PROP_GUID,
41+
ZPOOL_PROP_VERSION,
42+
ZPOOL_PROP_BOOTFS,
43+
ZPOOL_PROP_DELEGATION,
44+
ZPOOL_PROP_AUTOREPLACE,
45+
ZPOOL_PROP_CACHEFILE,
46+
ZPOOL_PROP_FAILUREMODE,
47+
ZPOOL_PROP_LISTSNAPS,
48+
ZPOOL_PROP_AUTOEXPAND,
49+
ZPOOL_PROP_DEDUPDITTO,
50+
ZPOOL_PROP_DEDUPRATIO,
51+
ZPOOL_PROP_FREE,
52+
ZPOOL_PROP_ALLOCATED,
53+
ZPOOL_PROP_READONLY,
54+
ZPOOL_PROP_COMMENT,
55+
ZPOOL_PROP_EXPANDSZ,
56+
ZPOOL_PROP_FREEING,
57+
ZPOOL_PROP_FRAGMENTATION,
58+
ZPOOL_PROP_LEAKED,
59+
ZPOOL_PROP_MAXBLOCKSIZE,
60+
ZPOOL_NUM_PROPS
61+
} zpool_prop_t;
62+
63+
typedef enum {
64+
ZPROP_SRC_NONE = 0x1,
65+
ZPROP_SRC_DEFAULT = 0x2,
66+
ZPROP_SRC_TEMPORARY = 0x4,
67+
ZPROP_SRC_LOCAL = 0x8,
68+
ZPROP_SRC_INHERITED = 0x10,
69+
ZPROP_SRC_RECEIVED = 0x20
70+
} zprop_source_t;
71+
72+
#define ZPOOL_CONFIG_VDEV_TREE "vdev_tree"
73+
#define ZPOOL_CONFIG_VDEV_STATS "vdev_stats"
74+
75+
typedef enum vdev_state {
76+
VDEV_STATE_UNKNOWN = 0,
77+
VDEV_STATE_CLOSED,
78+
VDEV_STATE_OFFLINE,
79+
VDEV_STATE_REMOVED,
80+
VDEV_STATE_CANT_OPEN,
81+
VDEV_STATE_FAULTED,
82+
VDEV_STATE_DEGRADED,
83+
VDEV_STATE_HEALTHY
84+
} vdev_state_t;
85+
86+
typedef enum vdev_aux {
87+
VDEV_AUX_NONE,
88+
VDEV_AUX_OPEN_FAILED,
89+
VDEV_AUX_CORRUPT_DATA,
90+
VDEV_AUX_NO_REPLICAS,
91+
VDEV_AUX_BAD_GUID_SUM,
92+
VDEV_AUX_TOO_SMALL,
93+
VDEV_AUX_BAD_LABEL,
94+
VDEV_AUX_VERSION_NEWER,
95+
VDEV_AUX_VERSION_OLDER,
96+
VDEV_AUX_UNSUP_FEAT,
97+
VDEV_AUX_SPARED,
98+
VDEV_AUX_ERR_EXCEEDED,
99+
VDEV_AUX_IO_FAILURE,
100+
VDEV_AUX_BAD_LOG,
101+
VDEV_AUX_EXTERNAL,
102+
VDEV_AUX_SPLIT_POOL,
103+
VDEV_AUX_ASHIFT_TOO_BIG
104+
} vdev_aux_t;
105+
106+
typedef enum pool_state {
107+
POOL_STATE_ACTIVE = 0,
108+
POOL_STATE_EXPORTED,
109+
POOL_STATE_DESTROYED,
110+
POOL_STATE_SPARE,
111+
POOL_STATE_L2CACHE,
112+
POOL_STATE_UNINITIALIZED,
113+
POOL_STATE_UNAVAIL,
114+
POOL_STATE_POTENTIALLY_ACTIVE
115+
} pool_state_t;
116+
117+
typedef enum zio_type {
118+
ZIO_TYPE_NULL = 0,
119+
ZIO_TYPE_READ,
120+
ZIO_TYPE_WRITE,
121+
ZIO_TYPE_FREE,
122+
ZIO_TYPE_CLAIM,
123+
ZIO_TYPE_IOCTL,
124+
ZIO_TYPES
125+
} zio_type_t;
126+
127+
128+
typedef struct vdev_stat {
129+
hrtime_t vs_timestamp;
130+
uint64_t vs_state;
131+
uint64_t vs_aux;
132+
uint64_t vs_alloc;
133+
uint64_t vs_space;
134+
uint64_t vs_dspace;
135+
uint64_t vs_rsize;
136+
uint64_t vs_esize;
137+
uint64_t vs_ops[ZIO_TYPES];
138+
uint64_t vs_bytes[ZIO_TYPES];
139+
uint64_t vs_read_errors;
140+
uint64_t vs_write_errors;
141+
uint64_t vs_checksum_errors;
142+
uint64_t vs_self_healed;
143+
uint64_t vs_scan_removing;
144+
uint64_t vs_scan_processed;
145+
uint64_t vs_configured_ashift;
146+
uint64_t vs_logical_ashift;
147+
uint64_t vs_physical_ashift;
148+
uint64_t vs_fragmentation;
149+
} vdev_stat_t;
150+
151+
// From https://github.com/NetBSD/src/blob/trunk/external/cddl/osnet/uts/common/sys/nvpair.h
152+
153+
typedef struct nvlist {
154+
int32_t nvl_version;
155+
uint32_t nvl_nvflag;
156+
uint64_t nvl_priv;
157+
uint32_t nvl_flag;
158+
int32_t nvl_pad;
159+
} nvlist_t;
160+
161+
// From https://github.com/NetBSD/src/blob/trunk/external/cddl/osnet/dist/lib/libzfs/common/libzfs.h
162+
163+
typedef enum {
164+
ZPOOL_STATUS_CORRUPT_CACHE,
165+
ZPOOL_STATUS_MISSING_DEV_R,
166+
ZPOOL_STATUS_MISSING_DEV_NR,
167+
ZPOOL_STATUS_CORRUPT_LABEL_R,
168+
ZPOOL_STATUS_CORRUPT_LABEL_NR,
169+
ZPOOL_STATUS_BAD_GUID_SUM,
170+
ZPOOL_STATUS_CORRUPT_POOL,
171+
ZPOOL_STATUS_CORRUPT_DATA,
172+
ZPOOL_STATUS_FAILING_DEV,
173+
ZPOOL_STATUS_VERSION_NEWER,
174+
ZPOOL_STATUS_HOSTID_MISMATCH,
175+
ZPOOL_STATUS_IO_FAILURE_WAIT,
176+
ZPOOL_STATUS_IO_FAILURE_CONTINUE,
177+
ZPOOL_STATUS_BAD_LOG,
178+
ZPOOL_STATUS_UNSUP_FEAT_READ,
179+
ZPOOL_STATUS_UNSUP_FEAT_WRITE,
180+
ZPOOL_STATUS_FAULTED_DEV_R,
181+
ZPOOL_STATUS_FAULTED_DEV_NR,
182+
ZPOOL_STATUS_VERSION_OLDER,
183+
ZPOOL_STATUS_FEAT_DISABLED,
184+
ZPOOL_STATUS_RESILVERING,
185+
ZPOOL_STATUS_OFFLINE_DEV,
186+
ZPOOL_STATUS_REMOVED_DEV,
187+
ZPOOL_STATUS_NON_NATIVE_ASHIFT,
188+
ZPOOL_STATUS_OK
189+
} zpool_status_t;
190+
191+
typedef struct zpool_handle zpool_handle_t;
192+
typedef struct libzfs_handle libzfs_handle_t;
193+
typedef int (*zpool_iter_f)(zpool_handle_t *, void *);
194+
195+
extern libzfs_handle_t *libzfs_init(void);
196+
extern void libzfs_fini(libzfs_handle_t *);
197+
extern const char *zpool_get_name(zpool_handle_t *);
198+
extern int zpool_get_state(zpool_handle_t *);
199+
extern const char *zpool_state_to_name(vdev_state_t, vdev_aux_t);
200+
extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *);
201+
extern uint64_t zpool_get_prop_int(zpool_handle_t *, zpool_prop_t, zprop_source_t *);
202+
extern zpool_status_t zpool_get_status(zpool_handle_t *, char **);
203+
extern nvlist_t *zpool_get_config(zpool_handle_t *, nvlist_t **);
204+
205+
const char *zpool_get_state_str(zpool_handle_t *);
206+
207+
// From https://github.com/NetBSD/src/blob/trunk/external/cddl/osnet/dist/uts/common/sys/nvpair.h
208+
209+
extern nvlist_t *opensolaris_fnvlist_lookup_nvlist(nvlist_t *, const char *);
210+
extern int opensolaris_nvlist_lookup_uint64_array(nvlist_t *, const char *, uint64_t **, uint_t *);
211+
extern int opensolaris_nvlist_lookup_uint32_array(nvlist_t *, const char *, uint32_t **, uint_t *);

src/detection/zpool/zpool_linux.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#ifdef FF_HAVE_LIBZFS
44
#include "util/kmod.h"
55

6-
#ifdef __sun
6+
#if defined(__sun)
77
#define FF_DISABLE_DLOPEN
88
#include <libzfs.h>
99

@@ -36,6 +36,37 @@ const char* zpool_get_state_str(zpool_handle_t* zpool)
3636
}
3737
}
3838
}
39+
#elif defined(__NetBSD__)
40+
#define FF_DISABLE_DLOPEN
41+
#include "libzfs_simplified_nbsd.h"
42+
43+
const char* zpool_get_state_str(zpool_handle_t* zpool)
44+
{
45+
if (zpool_get_state(zpool) == POOL_STATE_UNAVAIL)
46+
return "FAULTED";
47+
else
48+
{
49+
const char *str;
50+
zpool_status_t status = zpool_get_status(zpool, (char**) &str);
51+
if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
52+
status == ZPOOL_STATUS_IO_FAILURE_CONTINUE)
53+
return "SUSPENDED";
54+
else
55+
{
56+
nvlist_t *nvroot = opensolaris_fnvlist_lookup_nvlist(zpool_get_config(zpool, NULL), ZPOOL_CONFIG_VDEV_TREE);
57+
uint_t vsc;
58+
vdev_stat_t *vs;
59+
#ifdef __x86_64__
60+
if (opensolaris_nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, (uint64_t**) &vs, &vsc) != 0)
61+
#else
62+
if (opensolaris_nvlist_lookup_uint32_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, (uint32_t**) &vs, &vsc) != 0)
63+
#endif
64+
return "UNKNOWN";
65+
else
66+
return zpool_state_to_name(vs->vs_state, vs->vs_aux);
67+
}
68+
}
69+
}
3970
#else
4071
#include "libzfs_simplified.h"
4172
#endif

0 commit comments

Comments
 (0)