Skip to content

Commit 0282d24

Browse files
committed
Actually add the new files as well
Signed-off-by: Jorgen Lundman <lundman@lundman.net>
1 parent fc1e12d commit 0282d24

14 files changed

Lines changed: 1512 additions & 70 deletions

cmd/os/windows/zed_service/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use_clang()
44
um_add_executable(zed_service
55
zed_service.c
66
ops_status.c
7+
ops_import.c
8+
ops_export.c
79
# resource.rc
810
)
911

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#define _CRT_SECURE_NO_WARNINGS
2+
#include <windows.h>
3+
#include <stdint.h>
4+
#include <stdio.h>
5+
6+
#include <libzfs.h>
7+
#include <sys/nvpair.h>
8+
9+
#include "memfile.h"
10+
#include "ops_export.h"
11+
#include "pipe_rpc.h"
12+
13+
/* Global libzfs handle provided by the service */
14+
static libzfs_handle_t *g_lzh = NULL;
15+
16+
static void
17+
add_ok_err(nvlist_t *dst, int ok, const char *name, const char *err)
18+
{
19+
fnvlist_add_boolean_value(dst, "ok", ok ? B_TRUE : B_FALSE);
20+
if (name) fnvlist_add_string(dst, "name", name);
21+
if (!ok && err) fnvlist_add_string(dst, "err", err);
22+
}
23+
24+
/* Export one pool handle with flags mapped to your tree's API */
25+
static int
26+
export_one_handle(zpool_handle_t *zhp, uint32_t flags)
27+
{
28+
int rc = -1;
29+
30+
rc = zpool_export(zhp,
31+
(flags & ZEXP_FORCE) ? B_TRUE : B_FALSE,
32+
(flags & ZEXP_HARD) ? B_TRUE : B_FALSE);
33+
34+
return (rc);
35+
}
36+
37+
/* ---------- zpool_iter callbacks (pure C) ---------- */
38+
39+
typedef struct {
40+
uint64_t want_guid;
41+
zpool_handle_t *found; /* OUT: matching handle (do not close) */
42+
} find_by_guid_ctx_t;
43+
44+
static int
45+
find_by_guid_cb(zpool_handle_t *zhp, void *data)
46+
{
47+
find_by_guid_ctx_t *ctx = (find_by_guid_ctx_t *)data;
48+
nvlist_t *cfg = zpool_get_config(zhp, NULL);
49+
uint64_t g = 0;
50+
51+
if (cfg != NULL)
52+
(void) nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &g);
53+
54+
if (g == ctx->want_guid) {
55+
ctx->found = zhp;
56+
return (1); /* stop iteration */
57+
}
58+
59+
/* not a match, close and continue */
60+
zpool_close(zhp);
61+
return (0);
62+
}
63+
64+
/* ---------- Public JSON builders ---------- */
65+
66+
char *
67+
zed_export_one_json(uint32_t flags, uint64_t guid, size_t *out_len)
68+
{
69+
*out_len = 0;
70+
nvlist_t *res = fnvlist_alloc();
71+
add_ok_err(res, 0, NULL, NULL);
72+
73+
#ifdef ENABLE_FAKE_POOLS
74+
if (guid == 0xF00DFADEFACEULL) {
75+
add_ok_err(res, 1, "FAKE", NULL);
76+
memfile_t mf;
77+
FILE *fp = memfile_open(&mf);
78+
nvlist_print_json(fp, res);
79+
fclose(fp);
80+
fnvlist_free(res);
81+
return (memfile_take(&mf, out_len));
82+
}
83+
#endif
84+
85+
g_lzh = libzfs_init();
86+
if (!g_lzh) {
87+
add_ok_err(res, 0, NULL, "libzfs_init not available");
88+
memfile_t mf;
89+
FILE *fp = memfile_open(&mf);
90+
nvlist_print_json(fp, res);
91+
fclose(fp);
92+
fnvlist_free(res);
93+
return (memfile_take(&mf, out_len));
94+
}
95+
96+
find_by_guid_ctx_t ctx;
97+
ctx.want_guid = guid;
98+
ctx.found = NULL;
99+
100+
(void) zpool_iter(g_lzh, find_by_guid_cb, &ctx);
101+
102+
if (ctx.found == NULL) {
103+
add_ok_err(res, 0, NULL, "not imported");
104+
} else {
105+
const char *name = zpool_get_name(ctx.found);
106+
dprintf("export_one: exporting guid=%llu name=%s flags=0x%x\n",
107+
(unsigned long long)guid, name ? name : "(null)", flags);
108+
109+
int rc = export_one_handle(ctx.found, flags);
110+
111+
/*
112+
* In many trees, zpool_export*() closes the handle internally.
113+
* If your tree returns with a still-open handle, you can call:
114+
* zpool_close(ctx.found);
115+
* here safely.
116+
*/
117+
118+
if (rc == 0) {
119+
add_ok_err(res, 1, name, NULL);
120+
} else {
121+
const char *desc = libzfs_error_description(g_lzh);
122+
add_ok_err(res, 0, name, desc ? desc : "export failed");
123+
}
124+
}
125+
126+
memfile_t mf;
127+
FILE *fp = memfile_open(&mf);
128+
nvlist_print_json(fp, res);
129+
fclose(fp);
130+
fnvlist_free(res);
131+
return (memfile_take(&mf, out_len));
132+
}
133+
134+
typedef struct {
135+
libzfs_handle_t *lzh;
136+
nvlist_t *root; /* holds "exported" and "errors" */
137+
uint32_t flags;
138+
} export_all_ctx_t;
139+
140+
static int
141+
export_all_cb(zpool_handle_t *zhp, void *data)
142+
{
143+
export_all_ctx_t *ctx = (export_all_ctx_t *)data;
144+
const char *name = zpool_get_name(zhp);
145+
146+
dprintf("export_all: exporting %s flags=0x%x\n",
147+
name ? name : "(null)", ctx->flags);
148+
149+
int rc = export_one_handle(zhp, ctx->flags);
150+
151+
if (rc == 0) {
152+
if (name) {
153+
const char *one[1] = { name };
154+
fnvlist_add_string_array(ctx->root, "exported", one, 1);
155+
}
156+
} else {
157+
nvlist_t *e = fnvlist_alloc();
158+
if (name) fnvlist_add_string(e, "name", name);
159+
{
160+
const char *desc = libzfs_error_description(ctx->lzh);
161+
fnvlist_add_string(e, "err",
162+
desc ? desc : "export failed");
163+
}
164+
const nvlist_t *onee[1] = { e };
165+
fnvlist_add_nvlist_array(ctx->root, "errors", onee, 1);
166+
fnvlist_free(e);
167+
}
168+
169+
/* Keep iterating; export typically handles closing as needed. */
170+
return (0);
171+
}
172+
173+
char *
174+
zed_export_all_json(uint32_t flags, size_t *out_len)
175+
{
176+
*out_len = 0;
177+
nvlist_t *root = fnvlist_alloc();
178+
fnvlist_add_string_array(root, "exported", NULL, 0);
179+
fnvlist_add_nvlist_array(root, "errors", NULL, 0);
180+
181+
#ifdef ENABLE_FAKE_POOLS
182+
{
183+
const char *one[1] = { "FAKE" };
184+
fnvlist_add_string_array(root, "exported", one, 1);
185+
}
186+
#endif
187+
188+
g_lzh = libzfs_init();
189+
if (g_lzh) {
190+
export_all_ctx_t ctx;
191+
ctx.lzh = g_lzh;
192+
ctx.root = root;
193+
ctx.flags = flags;
194+
195+
(void) zpool_iter(g_lzh, export_all_cb, &ctx);
196+
}
197+
198+
memfile_t mf;
199+
FILE *fp = memfile_open(&mf);
200+
nvlist_print_json(fp, root);
201+
fclose(fp);
202+
fnvlist_free(root);
203+
return (memfile_take(&mf, out_len));
204+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
#include <stddef.h>
3+
#include <stdint.h>
4+
5+
char *zed_export_one_json(uint32_t flags, uint64_t guid, size_t *out_len);
6+
char *zed_export_all_json(uint32_t flags, size_t *out_len);

0 commit comments

Comments
 (0)