forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zpool.c
executable file
·500 lines (431 loc) · 13.7 KB
/
zpool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* C wrappers around some zfs calls and C in general that should simplify
* using libzfs from go language, and make go code shorter and more readable.
*/
#include <libzfs.h>
#include <libzfs/sys/zfs_context.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include "common.h"
#include "zpool.h"
#if LIBZFS_VERSION_MAJOR == 0 && LIBZFS_VERSION_MINOR == 8
#include <libzfs/libzutil.h>
#include <libzfs/sys/fs/zfs.h>
typedef unsigned long int rlim64_t;
char *sZPOOL_CONFIG_VERSION = ZPOOL_CONFIG_VERSION;
char *sZPOOL_CONFIG_POOL_NAME = ZPOOL_CONFIG_POOL_NAME;
char *sZPOOL_CONFIG_POOL_STATE = ZPOOL_CONFIG_POOL_STATE;
char *sZPOOL_CONFIG_POOL_TXG = ZPOOL_CONFIG_POOL_TXG;
char *sZPOOL_CONFIG_POOL_GUID = ZPOOL_CONFIG_POOL_GUID;
char *sZPOOL_CONFIG_CREATE_TXG = ZPOOL_CONFIG_CREATE_TXG;
char *sZPOOL_CONFIG_TOP_GUID = ZPOOL_CONFIG_TOP_GUID;
char *sZPOOL_CONFIG_VDEV_TREE = ZPOOL_CONFIG_VDEV_TREE;
char *sZPOOL_CONFIG_TYPE = ZPOOL_CONFIG_TYPE;
char *sZPOOL_CONFIG_CHILDREN = ZPOOL_CONFIG_CHILDREN;
char *sZPOOL_CONFIG_ID = ZPOOL_CONFIG_ID;
char *sZPOOL_CONFIG_GUID = ZPOOL_CONFIG_GUID;
char *sZPOOL_CONFIG_PATH = ZPOOL_CONFIG_PATH;
char *sZPOOL_CONFIG_DEVID = ZPOOL_CONFIG_DEVID;
char *sZPOOL_CONFIG_METASLAB_ARRAY = ZPOOL_CONFIG_METASLAB_ARRAY;
char *sZPOOL_CONFIG_METASLAB_SHIFT = ZPOOL_CONFIG_METASLAB_SHIFT;
char *sZPOOL_CONFIG_ASHIFT = ZPOOL_CONFIG_ASHIFT;
char *sZPOOL_CONFIG_ASIZE = ZPOOL_CONFIG_ASIZE;
char *sZPOOL_CONFIG_DTL = ZPOOL_CONFIG_DTL;
char *sZPOOL_CONFIG_SCAN_STATS = ZPOOL_CONFIG_SCAN_STATS;
char *sZPOOL_CONFIG_VDEV_STATS = ZPOOL_CONFIG_VDEV_STATS;
char *sZPOOL_CONFIG_WHOLE_DISK = ZPOOL_CONFIG_WHOLE_DISK;
char *sZPOOL_CONFIG_ERRCOUNT = ZPOOL_CONFIG_ERRCOUNT;
char *sZPOOL_CONFIG_NOT_PRESENT = ZPOOL_CONFIG_NOT_PRESENT;
char *sZPOOL_CONFIG_SPARES = ZPOOL_CONFIG_SPARES;
char *sZPOOL_CONFIG_IS_SPARE = ZPOOL_CONFIG_IS_SPARE;
char *sZPOOL_CONFIG_NPARITY = ZPOOL_CONFIG_NPARITY;
char *sZPOOL_CONFIG_HOSTID = ZPOOL_CONFIG_HOSTID;
char *sZPOOL_CONFIG_HOSTNAME = ZPOOL_CONFIG_HOSTNAME;
char *sZPOOL_CONFIG_LOADED_TIME = ZPOOL_CONFIG_LOADED_TIME;
char *sZPOOL_CONFIG_UNSPARE = ZPOOL_CONFIG_UNSPARE;
char *sZPOOL_CONFIG_PHYS_PATH = ZPOOL_CONFIG_PHYS_PATH;
char *sZPOOL_CONFIG_IS_LOG = ZPOOL_CONFIG_IS_LOG;
char *sZPOOL_CONFIG_L2CACHE = ZPOOL_CONFIG_L2CACHE;
char *sZPOOL_CONFIG_HOLE_ARRAY = ZPOOL_CONFIG_HOLE_ARRAY;
char *sZPOOL_CONFIG_VDEV_CHILDREN = ZPOOL_CONFIG_VDEV_CHILDREN;
char *sZPOOL_CONFIG_IS_HOLE = ZPOOL_CONFIG_IS_HOLE;
char *sZPOOL_CONFIG_DDT_HISTOGRAM = ZPOOL_CONFIG_DDT_HISTOGRAM;
char *sZPOOL_CONFIG_DDT_OBJ_STATS = ZPOOL_CONFIG_DDT_OBJ_STATS;
char *sZPOOL_CONFIG_DDT_STATS = ZPOOL_CONFIG_DDT_STATS;
char *sZPOOL_CONFIG_SPLIT = ZPOOL_CONFIG_SPLIT;
char *sZPOOL_CONFIG_ORIG_GUID = ZPOOL_CONFIG_ORIG_GUID;
char *sZPOOL_CONFIG_SPLIT_GUID = ZPOOL_CONFIG_SPLIT_GUID;
char *sZPOOL_CONFIG_SPLIT_LIST = ZPOOL_CONFIG_SPLIT_LIST;
char *sZPOOL_CONFIG_REMOVING = ZPOOL_CONFIG_REMOVING;
char *sZPOOL_CONFIG_RESILVER_TXG = ZPOOL_CONFIG_RESILVER_TXG;
char *sZPOOL_CONFIG_COMMENT = ZPOOL_CONFIG_COMMENT;
char *sZPOOL_CONFIG_SUSPENDED = ZPOOL_CONFIG_SUSPENDED;
char *sZPOOL_CONFIG_TIMESTAMP = ZPOOL_CONFIG_TIMESTAMP;
char *sZPOOL_CONFIG_BOOTFS = ZPOOL_CONFIG_BOOTFS;
char *sZPOOL_CONFIG_MISSING_DEVICES = ZPOOL_CONFIG_MISSING_DEVICES;
char *sZPOOL_CONFIG_LOAD_INFO = ZPOOL_CONFIG_LOAD_INFO;
char *sZPOOL_CONFIG_REWIND_INFO = ZPOOL_CONFIG_REWIND_INFO;
char *sZPOOL_CONFIG_UNSUP_FEAT = ZPOOL_CONFIG_UNSUP_FEAT;
char *sZPOOL_CONFIG_ENABLED_FEAT = ZPOOL_CONFIG_ENABLED_FEAT;
char *sZPOOL_CONFIG_CAN_RDONLY = ZPOOL_CONFIG_CAN_RDONLY;
char *sZPOOL_CONFIG_FEATURES_FOR_READ = ZPOOL_CONFIG_FEATURES_FOR_READ;
char *sZPOOL_CONFIG_FEATURE_STATS = ZPOOL_CONFIG_FEATURE_STATS;
char *sZPOOL_CONFIG_ERRATA = ZPOOL_CONFIG_ERRATA;
char *sZPOOL_CONFIG_OFFLINE = ZPOOL_CONFIG_OFFLINE;
char *sZPOOL_CONFIG_FAULTED = ZPOOL_CONFIG_FAULTED;
char *sZPOOL_CONFIG_DEGRADED = ZPOOL_CONFIG_DEGRADED;
char *sZPOOL_CONFIG_REMOVED = ZPOOL_CONFIG_REMOVED;
char *sZPOOL_CONFIG_FRU = ZPOOL_CONFIG_FRU;
char *sZPOOL_CONFIG_AUX_STATE = ZPOOL_CONFIG_AUX_STATE;
char *sZPOOL_LOAD_POLICY = ZPOOL_LOAD_POLICY;
char *sZPOOL_LOAD_REWIND_POLICY = ZPOOL_LOAD_REWIND_POLICY;
char *sZPOOL_LOAD_REQUEST_TXG = ZPOOL_LOAD_REQUEST_TXG;
char *sZPOOL_LOAD_META_THRESH = ZPOOL_LOAD_META_THRESH;
char *sZPOOL_LOAD_DATA_THRESH = ZPOOL_LOAD_DATA_THRESH;
char *sZPOOL_CONFIG_LOAD_TIME = ZPOOL_CONFIG_LOAD_TIME;
char *sZPOOL_CONFIG_LOAD_DATA_ERRORS = ZPOOL_CONFIG_LOAD_DATA_ERRORS;
char *sZPOOL_CONFIG_REWIND_TIME = ZPOOL_CONFIG_REWIND_TIME;
static char _lasterr_[1024];
const char *lasterr(void) {
return _lasterr_;
}
zpool_list_t *create_zpool_list_item() {
zpool_list_t *zlist = malloc(sizeof(zpool_list_t));
memset(zlist, 0, sizeof(zpool_list_t));
return zlist;
}
int zpool_list_callb(zpool_handle_t *pool, void *data) {
zpool_list_t **lroot = (zpool_list_t**)data;
zpool_list_t *nroot = create_zpool_list_item();
if ( !((*lroot)->zph) ) {
(*lroot)->zph = pool;
} else {
nroot->zph = pool;
nroot->pnext = (void*)*lroot;
*lroot = nroot;
}
return 0;
}
zpool_list_ptr zpool_list_openall() {
int err = 0;
zpool_list_t *zlist = create_zpool_list_item();
err = zpool_iter(libzfs_get_handle(), zpool_list_callb, &zlist);
if ( err != 0 || zlist->zph == NULL ) {
zpool_list_free(zlist);
zlist = NULL;
}
return zlist;
}
zpool_list_t* zpool_list_open(const char *name) {
zpool_list_t *zlist = create_zpool_list_item();
zlist->zph = zpool_open(libzfs_get_handle(), name);
if ( zlist->zph ) {
return zlist;
} else {
zpool_list_free(zlist);
}
return 0;
}
zpool_list_t *zpool_next(zpool_list_t *pool) {
return pool->pnext;
}
void zpool_list_free(zpool_list_t *list) {
zpool_list_ptr next;
while(list) {
next = list->pnext;
free(list);
list = next;
}
}
void zpool_list_close(zpool_list_t *pool) {
if (pool->zph) {
zpool_close(pool->zph);
}
zpool_list_free(pool);
}
property_list_t *next_property(property_list_t *list) {
if (list != 0) {
return list->pnext;
}
return list;
}
void zprop_source_tostr(char *dst, zprop_source_t source) {
switch (source) {
case ZPROP_SRC_NONE:
strcpy(dst, "none");
break;
case ZPROP_SRC_TEMPORARY:
strcpy(dst, "temporary");
break;
case ZPROP_SRC_LOCAL:
strcpy(dst, "local");
break;
case ZPROP_SRC_INHERITED:
strcpy(dst, "inherited");
break;
case ZPROP_SRC_RECEIVED:
strcpy(dst, "received");
break;
default:
strcpy(dst, "default");
break;
}
}
property_list_ptr read_zpool_property(zpool_list_ptr pool, int prop) {
int r = 0;
zprop_source_t source;
property_list_ptr list = new_property_list();
r = zpool_get_prop(pool->zph, prop,
list->value, INT_MAX_VALUE, &source, B_FALSE);
if (r == 0) {
// strcpy(list->name, zpool_prop_to_name(prop));
zprop_source_tostr(list->source, source);
} else {
free_properties(list);
return NULL;
}
list->property = (int)prop;
return list;
}
property_list_ptr read_append_zpool_property(zpool_list_ptr pool, property_list_ptr proot, zpool_prop_t prop) {
int r = 0;
property_list_t *newitem = NULL;
newitem = read_zpool_property(pool, prop);
if (newitem == NULL) {
return proot;
}
// printf("p: %s %s %s\n", newitem->name, newitem->value, newitem->source);
newitem->pnext = proot;
proot = newitem;
return proot;
}
property_list_t *read_zpool_properties(zpool_list_ptr pool) {
// read pool name as first property
property_list_t *root = NULL, *list = NULL;
for (zpool_prop_t prop = ZPOOL_PROP_NAME; prop < ZPOOL_NUM_PROPS; prop++) {
root = read_append_zpool_property(pool, root, prop);
}
// printf("Finished properties reading.\n");
return root;
}
pool_state_t zpool_read_state(zpool_handle_t *zh) {
return zpool_get_state(zh);
}
const char *gettext(const char *txt) {
return txt;
}
/*
* Add a property pair (name, string-value) into a property nvlist.
*/
// int
// add_prop_list(const char *propname, char *propval, nvlist_t **props,
// boolean_t poolprop) {
// zpool_prop_t prop = ZPROP_INVAL;
// zfs_prop_t fprop;
// nvlist_t *proplist;
// const char *normnm;
// char *strval;
// if (*props == NULL &&
// nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
// (void) snprintf(_lasterr_, 1024, "internal error: out of memory");
// return (1);
// }
// proplist = *props;
// if (poolprop) {
// const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
// if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
// !zpool_prop_feature(propname)) {
// (void) snprintf(_lasterr_, 1024, "property '%s' is "
// "not a valid pool property", propname);
// return (2);
// }
// /*
// * feature@ properties and version should not be specified
// * at the same time.
// */
// // if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
// // nvlist_exists(proplist, vname)) ||
// // (prop == ZPOOL_PROP_VERSION &&
// // prop_list_contains_feature(proplist))) {
// // (void) fprintf(stderr, gettext("'feature@' and "
// // "'version' properties cannot be specified "
// // "together\n"));
// // return (2);
// // }
// if (zpool_prop_feature(propname))
// normnm = propname;
// else
// normnm = zpool_prop_to_name(prop);
// } else {
// if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
// normnm = zfs_prop_to_name(fprop);
// } else {
// normnm = propname;
// }
// }
// if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
// prop != ZPOOL_PROP_CACHEFILE) {
// (void) snprintf(_lasterr_, 1024, "property '%s' "
// "specified multiple times", propname);
// return (2);
// }
// if (nvlist_add_string(proplist, normnm, propval) != 0) {
// (void) snprintf(_lasterr_, 1024, "internal "
// "error: out of memory\n");
// return (1);
// }
// return (0);
// }
nvlist_t** nvlist_alloc_array(int count) {
return malloc(count*sizeof(nvlist_t*));
}
void nvlist_array_set(nvlist_t** a, int i, nvlist_t *item) {
a[i] = item;
}
void nvlist_free_array(nvlist_t **a) {
free(a);
}
nvlist_t *nvlist_array_at(nvlist_t **a, uint_t i) {
return a[i];
}
int refresh_stats(zpool_list_t *pool)
{
boolean_t missing;
int err = zpool_refresh_stats(pool->zph, &missing);
if ( err != 0 ) {
return err;
}
if ( missing == B_TRUE ) {
return -1;
}
return 0;
}
const char *get_vdev_type(nvlist_ptr nv) {
char *value = NULL;
int r = nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &value);
if(r != 0) {
return NULL;
}
return value;
}
const vdev_stat_ptr get_vdev_stats(nvlist_ptr nv) {
vdev_stat_ptr vs = NULL;
uint_t count;
int r = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, (uint64_t**)&vs, &count);
if(r != 0) {
return NULL;
}
return vs;
}
pool_scan_stat_ptr get_vdev_scan_stats(nvlist_t *nv) {
pool_scan_stat_ptr vds = NULL;
uint_t c;
int r = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS, (uint64_t**)&vds, &c);
if(r != 0) {
return NULL;
}
return vds;
}
vdev_children_ptr get_vdev_children(nvlist_t *nv) {
int r;
vdev_children_ptr children = malloc(sizeof(vdev_children_t));
memset(children, 0, sizeof(vdev_children_t));
r = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &(children->first), &(children->count));
if (r != 0) {
free(children);
return NULL;
}
return children;
}
vdev_children_ptr get_vdev_spares(nvlist_t *nv) {
int r;
vdev_children_ptr children = malloc(sizeof(vdev_children_t));
memset(children, 0, sizeof(vdev_children_t));
r = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &(children->first), &(children->count));
if (r != 0) {
free(children);
return NULL;
}
return children;
}
vdev_children_ptr get_vdev_l2cache(nvlist_t *nv) {
int r;
vdev_children_ptr children = malloc(sizeof(vdev_children_t));
memset(children, 0, sizeof(vdev_children_t));
r = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, &(children->first), &(children->count));
if (r != 0) {
free(children);
return NULL;
}
return children;
}
const char *get_vdev_path(nvlist_ptr nv) {
char *path = NULL;
uint64_t notpresent = 0;
int r = nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, ¬present);
if (r == 0 || notpresent != 0) {
if ( 0 != nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) ) {
return NULL;
}
}
return path;
}
uint64_t get_vdev_is_log(nvlist_ptr nv) {
uint64_t islog = B_FALSE;
nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
return islog;
}
// return
uint64_t get_zpool_state(nvlist_ptr nv) {
uint64_t state = 0;
nvlist_lookup_uint64(nv, ZPOOL_CONFIG_POOL_STATE, &state);
return state;
}
uint64_t get_zpool_guid(nvlist_ptr nv) {
uint64_t guid = 0;
nvlist_lookup_uint64(nv, ZPOOL_CONFIG_POOL_GUID, &guid);
return guid;
}
const char *get_zpool_name(nvlist_ptr nv) {
char *name = NULL;
if (0 != nvlist_lookup_string(nv, ZPOOL_CONFIG_POOL_NAME, &name)) {
return NULL;
}
return name;
}
const char *get_zpool_comment(nvlist_ptr nv) {
char *comment = NULL;
if (0 != nvlist_lookup_string(nv, ZPOOL_CONFIG_COMMENT, &comment)) {
return NULL;
}
return comment;
}
nvlist_ptr get_zpool_vdev_tree(nvlist_ptr nv) {
nvlist_ptr vdev_tree = NULL;
if ( 0 != nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) ) {
return NULL;
}
return vdev_tree;
}
nvlist_ptr go_zpool_search_import(libzfs_handle_ptr zfsh, int paths, char **path, boolean_t do_scan) {
importargs_t idata;
memset(&idata, 0, sizeof(importargs_t));
nvlist_ptr pools = NULL;
idata.path = path;
idata.paths = paths;
idata.scan = 0;
pools = zpool_search_import(libzfs_get_handle(), &idata, &libzfs_config_ops);
return pools;
}
int do_zpool_clear(zpool_list_t *pool, const char *device, u_int32_t rewind_policy) {
nvlist_t *policy = NULL;
int ret = 0;
if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind_policy) != 0)
return (1);
if (zpool_clear(pool->zph, device, policy) != 0)
ret = 1;
nvlist_free(policy);
return (ret);
}
#endif // LIBZFS_VERSION_MINOR == 8