Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/cmd/zpool/zpool_iter.c
107935 views
1
// SPDX-License-Identifier: CDDL-1.0
2
/*
3
* CDDL HEADER START
4
*
5
* The contents of this file are subject to the terms of the
6
* Common Development and Distribution License (the "License").
7
* You may not use this file except in compliance with the License.
8
*
9
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10
* or https://opensource.org/licenses/CDDL-1.0.
11
* See the License for the specific language governing permissions
12
* and limitations under the License.
13
*
14
* When distributing Covered Code, include this CDDL HEADER in each
15
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16
* If applicable, add the following below this CDDL HEADER, with the
17
* fields enclosed by brackets "[]" replaced with your own identifying
18
* information: Portions Copyright [yyyy] [name of copyright owner]
19
*
20
* CDDL HEADER END
21
*/
22
/*
23
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24
* Use is subject to license terms.
25
*/
26
27
/*
28
* Copyright 2016 Igor Kozhukhov <[email protected]>.
29
* Copyright (c) 2025, Klara, Inc.
30
*/
31
32
#include <libintl.h>
33
#include <stddef.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
38
#include <libzfs.h>
39
#include <libzutil.h>
40
#include <sys/zfs_context.h>
41
#include <sys/wait.h>
42
43
#include "zpool_util.h"
44
45
/*
46
* Private interface for iterating over pools specified on the command line.
47
* Most consumers will call for_each_pool, but in order to support iostat, we
48
* allow fined grained control through the zpool_list_t interface.
49
*/
50
51
typedef struct zpool_node {
52
zpool_handle_t *zn_handle;
53
avl_node_t zn_avlnode;
54
hrtime_t zn_last_refresh;
55
} zpool_node_t;
56
57
struct zpool_list {
58
boolean_t zl_findall;
59
boolean_t zl_literal;
60
avl_tree_t zl_avl;
61
zprop_list_t **zl_proplist;
62
zfs_type_t zl_type;
63
hrtime_t zl_last_refresh;
64
};
65
66
static int
67
zpool_compare(const void *larg, const void *rarg)
68
{
69
zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle;
70
zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle;
71
const char *lname = zpool_get_name(l);
72
const char *rname = zpool_get_name(r);
73
74
return (TREE_ISIGN(strcmp(lname, rname)));
75
}
76
77
/*
78
* Callback function for pool_list_get(). Adds the given pool to the AVL tree
79
* of known pools.
80
*/
81
static int
82
add_pool(zpool_handle_t *zhp, zpool_list_t *zlp)
83
{
84
zpool_node_t *node, *new = safe_malloc(sizeof (zpool_node_t));
85
avl_index_t idx;
86
87
new->zn_handle = zhp;
88
89
node = avl_find(&zlp->zl_avl, new, &idx);
90
if (node == NULL) {
91
if (zlp->zl_proplist &&
92
zpool_expand_proplist(zhp, zlp->zl_proplist,
93
zlp->zl_type, zlp->zl_literal) != 0) {
94
zpool_close(zhp);
95
free(new);
96
return (-1);
97
}
98
new->zn_last_refresh = zlp->zl_last_refresh;
99
avl_insert(&zlp->zl_avl, new, idx);
100
} else {
101
zpool_refresh_stats_from_handle(node->zn_handle, zhp);
102
node->zn_last_refresh = zlp->zl_last_refresh;
103
zpool_close(zhp);
104
free(new);
105
return (-1);
106
}
107
108
return (0);
109
}
110
111
/*
112
* add_pool(), but always returns 0. This allows zpool_iter() to continue
113
* even if a pool exists in the tree, or we fail to get the properties for
114
* a new one.
115
*/
116
static int
117
add_pool_cb(zpool_handle_t *zhp, void *data)
118
{
119
(void) add_pool(zhp, data);
120
return (0);
121
}
122
123
/*
124
* Create a list of pools based on the given arguments. If we're given no
125
* arguments, then iterate over all pools in the system and add them to the AVL
126
* tree. Otherwise, add only those pool explicitly specified on the command
127
* line.
128
*/
129
zpool_list_t *
130
pool_list_get(int argc, char **argv, zprop_list_t **proplist, zfs_type_t type,
131
boolean_t literal, int *err)
132
{
133
zpool_list_t *zlp;
134
135
zlp = safe_malloc(sizeof (zpool_list_t));
136
137
avl_create(&zlp->zl_avl, zpool_compare,
138
sizeof (zpool_node_t), offsetof(zpool_node_t, zn_avlnode));
139
140
zlp->zl_proplist = proplist;
141
zlp->zl_type = type;
142
143
zlp->zl_literal = literal;
144
zlp->zl_last_refresh = gethrtime();
145
146
if (argc == 0) {
147
(void) zpool_iter(g_zfs, add_pool_cb, zlp);
148
zlp->zl_findall = B_TRUE;
149
} else {
150
int i;
151
152
for (i = 0; i < argc; i++) {
153
zpool_handle_t *zhp;
154
155
if ((zhp = zpool_open_canfail(g_zfs, argv[i])) !=
156
NULL) {
157
if (add_pool(zhp, zlp) != 0)
158
*err = B_TRUE;
159
} else {
160
*err = B_TRUE;
161
}
162
}
163
}
164
165
return (zlp);
166
}
167
168
/*
169
* Refresh the state of all pools on the list. Additionally, if no options were
170
* given on the command line, add any new pools and remove any that are no
171
* longer available.
172
*/
173
int
174
pool_list_refresh(zpool_list_t *zlp)
175
{
176
zlp->zl_last_refresh = gethrtime();
177
178
if (!zlp->zl_findall) {
179
/*
180
* This list is a fixed list of pools, so we must not add
181
* or remove any. Just walk over them and refresh their
182
* state.
183
*/
184
int navail = 0;
185
for (zpool_node_t *node = avl_first(&zlp->zl_avl);
186
node != NULL; node = AVL_NEXT(&zlp->zl_avl, node)) {
187
boolean_t missing;
188
zpool_refresh_stats(node->zn_handle, &missing);
189
navail += !missing;
190
node->zn_last_refresh = zlp->zl_last_refresh;
191
}
192
return (navail);
193
}
194
195
/* Search for any new pools and add them to the list. */
196
(void) zpool_iter(g_zfs, add_pool_cb, zlp);
197
198
/* Walk the list of existing pools, and update or remove them. */
199
zpool_node_t *node, *next;
200
for (node = avl_first(&zlp->zl_avl); node != NULL; node = next) {
201
next = AVL_NEXT(&zlp->zl_avl, node);
202
203
/*
204
* Skip any that were refreshed and are online; they were added
205
* by zpool_iter() and are already up to date.
206
*/
207
if (node->zn_last_refresh == zlp->zl_last_refresh &&
208
zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL)
209
continue;
210
211
/* Refresh and remove if necessary. */
212
boolean_t missing;
213
zpool_refresh_stats(node->zn_handle, &missing);
214
if (missing) {
215
avl_remove(&zlp->zl_avl, node);
216
zpool_close(node->zn_handle);
217
free(node);
218
} else {
219
node->zn_last_refresh = zlp->zl_last_refresh;
220
}
221
}
222
223
return (avl_numnodes(&zlp->zl_avl));
224
}
225
226
/*
227
* Iterate over all pools in the list, executing the callback for each
228
*/
229
int
230
pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func,
231
void *data)
232
{
233
zpool_node_t *node, *next_node;
234
int ret = 0;
235
236
for (node = avl_first(&zlp->zl_avl); node != NULL; node = next_node) {
237
next_node = AVL_NEXT(&zlp->zl_avl, node);
238
if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL ||
239
unavail)
240
ret |= func(node->zn_handle, data);
241
}
242
243
return (ret);
244
}
245
246
/*
247
* Free all the handles associated with this list.
248
*/
249
void
250
pool_list_free(zpool_list_t *zlp)
251
{
252
zpool_node_t *node;
253
void *cookie = NULL;
254
255
while ((node = avl_destroy_nodes(&zlp->zl_avl, &cookie)) != NULL) {
256
zpool_close(node->zn_handle);
257
free(node);
258
}
259
260
avl_destroy(&zlp->zl_avl);
261
free(zlp);
262
}
263
264
/*
265
* Returns the number of elements in the pool list.
266
*/
267
int
268
pool_list_count(zpool_list_t *zlp)
269
{
270
return (avl_numnodes(&zlp->zl_avl));
271
}
272
273
/*
274
* High level function which iterates over all pools given on the command line,
275
* using the pool_list_* interfaces.
276
*/
277
int
278
for_each_pool(int argc, char **argv, boolean_t unavail,
279
zprop_list_t **proplist, zfs_type_t type, boolean_t literal,
280
zpool_iter_f func, void *data)
281
{
282
zpool_list_t *list;
283
int ret = 0;
284
285
if ((list = pool_list_get(argc, argv, proplist, type, literal,
286
&ret)) == NULL)
287
return (1);
288
289
if (pool_list_iter(list, unavail, func, data) != 0)
290
ret = 1;
291
292
pool_list_free(list);
293
294
return (ret);
295
}
296
297
/*
298
* This is the equivalent of for_each_pool() for vdevs. It iterates thorough
299
* all vdevs in the pool, ignoring root vdevs and holes, calling func() on
300
* each one.
301
*
302
* @zhp: Zpool handle
303
* @func: Function to call on each vdev
304
* @data: Custom data to pass to the function
305
*/
306
int
307
for_each_vdev(zpool_handle_t *zhp, pool_vdev_iter_f func, void *data)
308
{
309
nvlist_t *config, *nvroot = NULL;
310
311
if ((config = zpool_get_config(zhp, NULL)) != NULL) {
312
verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
313
&nvroot) == 0);
314
}
315
return (for_each_vdev_cb((void *) zhp, nvroot, func, data));
316
}
317
318
/*
319
* Process the vcdl->vdev_cmd_data[] array to figure out all the unique column
320
* names and their widths. When this function is done, vcdl->uniq_cols,
321
* vcdl->uniq_cols_cnt, and vcdl->uniq_cols_width will be filled in.
322
*/
323
static void
324
process_unique_cmd_columns(vdev_cmd_data_list_t *vcdl)
325
{
326
char **uniq_cols = NULL, **tmp = NULL;
327
int *uniq_cols_width;
328
vdev_cmd_data_t *data;
329
int cnt = 0;
330
int k;
331
332
/* For each vdev */
333
for (int i = 0; i < vcdl->count; i++) {
334
data = &vcdl->data[i];
335
/* For each column the vdev reported */
336
for (int j = 0; j < data->cols_cnt; j++) {
337
/* Is this column in our list of unique column names? */
338
for (k = 0; k < cnt; k++) {
339
if (strcmp(data->cols[j], uniq_cols[k]) == 0)
340
break; /* yes it is */
341
}
342
if (k == cnt) {
343
/* No entry for column, add to list */
344
tmp = realloc(uniq_cols, sizeof (*uniq_cols) *
345
(cnt + 1));
346
if (tmp == NULL)
347
break; /* Nothing we can do... */
348
uniq_cols = tmp;
349
uniq_cols[cnt] = data->cols[j];
350
cnt++;
351
}
352
}
353
}
354
355
/*
356
* We now have a list of all the unique column names. Figure out the
357
* max width of each column by looking at the column name and all its
358
* values.
359
*/
360
uniq_cols_width = safe_malloc(sizeof (*uniq_cols_width) * cnt);
361
for (int i = 0; i < cnt; i++) {
362
/* Start off with the column title's width */
363
uniq_cols_width[i] = strlen(uniq_cols[i]);
364
/* For each vdev */
365
for (int j = 0; j < vcdl->count; j++) {
366
/* For each of the vdev's values in a column */
367
data = &vcdl->data[j];
368
for (k = 0; k < data->cols_cnt; k++) {
369
/* Does this vdev have a value for this col? */
370
if (strcmp(data->cols[k], uniq_cols[i]) == 0) {
371
/* Is the value width larger? */
372
uniq_cols_width[i] =
373
MAX(uniq_cols_width[i],
374
strlen(data->lines[k]));
375
}
376
}
377
}
378
}
379
380
vcdl->uniq_cols = uniq_cols;
381
vcdl->uniq_cols_cnt = cnt;
382
vcdl->uniq_cols_width = uniq_cols_width;
383
}
384
385
386
/*
387
* Process a line of command output
388
*
389
* When running 'zpool iostat|status -c' the lines of output can either be
390
* in the form of:
391
*
392
* column_name=value
393
*
394
* Or just:
395
*
396
* value
397
*
398
* Process the column_name (if any) and value.
399
*
400
* Returns 0 if line was processed, and there are more lines can still be
401
* processed.
402
*
403
* Returns 1 if this was the last line to process, or error.
404
*/
405
static int
406
vdev_process_cmd_output(vdev_cmd_data_t *data, char *line)
407
{
408
char *col;
409
char *val;
410
char *equals;
411
char **tmp;
412
413
if (line == NULL)
414
return (1);
415
416
equals = strchr(line, '=');
417
if (equals != NULL) {
418
/*
419
* We have a 'column=value' type line. Split it into the
420
* column and value strings by turning the '=' into a '\0'.
421
*/
422
*equals = '\0';
423
col = line;
424
val = equals + 1;
425
} else {
426
col = NULL;
427
val = line;
428
}
429
430
/* Do we already have a column by this name? If so, skip it. */
431
if (col != NULL) {
432
for (int i = 0; i < data->cols_cnt; i++) {
433
if (strcmp(col, data->cols[i]) == 0)
434
return (0); /* Duplicate, skip */
435
}
436
}
437
438
if (val != NULL) {
439
tmp = realloc(data->lines,
440
(data->lines_cnt + 1) * sizeof (*data->lines));
441
if (tmp == NULL)
442
return (1);
443
444
data->lines = tmp;
445
data->lines[data->lines_cnt] = strdup(val);
446
data->lines_cnt++;
447
}
448
449
if (col != NULL) {
450
tmp = realloc(data->cols,
451
(data->cols_cnt + 1) * sizeof (*data->cols));
452
if (tmp == NULL)
453
return (1);
454
455
data->cols = tmp;
456
data->cols[data->cols_cnt] = strdup(col);
457
data->cols_cnt++;
458
}
459
460
if (val != NULL && col == NULL)
461
return (1);
462
463
return (0);
464
}
465
466
/*
467
* Run the cmd and store results in *data.
468
*/
469
static void
470
vdev_run_cmd(vdev_cmd_data_t *data, char *cmd)
471
{
472
int rc;
473
char *argv[2] = {cmd};
474
char **env;
475
char **lines = NULL;
476
int lines_cnt = 0;
477
int i;
478
479
env = zpool_vdev_script_alloc_env(data->pool, data->path, data->upath,
480
data->vdev_enc_sysfs_path, NULL, NULL);
481
if (env == NULL)
482
goto out;
483
484
/* Run the command */
485
rc = libzfs_run_process_get_stdout_nopath(cmd, argv, env, &lines,
486
&lines_cnt);
487
488
zpool_vdev_script_free_env(env);
489
490
if (rc != 0)
491
goto out;
492
493
/* Process the output we got */
494
for (i = 0; i < lines_cnt; i++)
495
if (vdev_process_cmd_output(data, lines[i]) != 0)
496
break;
497
498
out:
499
if (lines != NULL)
500
libzfs_free_str_array(lines, lines_cnt);
501
}
502
503
/*
504
* Generate the search path for zpool iostat/status -c scripts.
505
* The string returned must be freed.
506
*/
507
char *
508
zpool_get_cmd_search_path(void)
509
{
510
const char *env;
511
char *sp = NULL;
512
513
env = getenv("ZPOOL_SCRIPTS_PATH");
514
if (env != NULL)
515
return (strdup(env));
516
517
env = getenv("HOME");
518
if (env != NULL) {
519
if (asprintf(&sp, "%s/.zpool.d:%s",
520
env, ZPOOL_SCRIPTS_DIR) != -1) {
521
return (sp);
522
}
523
}
524
525
if (asprintf(&sp, "%s", ZPOOL_SCRIPTS_DIR) != -1)
526
return (sp);
527
528
return (NULL);
529
}
530
531
/* Thread function run for each vdev */
532
static void
533
vdev_run_cmd_thread(void *cb_cmd_data)
534
{
535
vdev_cmd_data_t *data = cb_cmd_data;
536
char *cmd = NULL, *cmddup, *cmdrest;
537
538
cmddup = strdup(data->cmd);
539
if (cmddup == NULL)
540
return;
541
542
cmdrest = cmddup;
543
while ((cmd = strtok_r(cmdrest, ",", &cmdrest))) {
544
char *dir = NULL, *sp, *sprest;
545
char fullpath[MAXPATHLEN];
546
547
if (strchr(cmd, '/') != NULL)
548
continue;
549
550
sp = zpool_get_cmd_search_path();
551
if (sp == NULL)
552
continue;
553
554
sprest = sp;
555
while ((dir = strtok_r(sprest, ":", &sprest))) {
556
if (snprintf(fullpath, sizeof (fullpath),
557
"%s/%s", dir, cmd) == -1)
558
continue;
559
560
if (access(fullpath, X_OK) == 0) {
561
vdev_run_cmd(data, fullpath);
562
break;
563
}
564
}
565
free(sp);
566
}
567
free(cmddup);
568
}
569
570
/* For each vdev in the pool run a command */
571
static int
572
for_each_vdev_run_cb(void *zhp_data, nvlist_t *nv, void *cb_vcdl)
573
{
574
vdev_cmd_data_list_t *vcdl = cb_vcdl;
575
vdev_cmd_data_t *data;
576
const char *path = NULL;
577
char *vname = NULL;
578
const char *vdev_enc_sysfs_path = NULL;
579
int i, match = 0;
580
zpool_handle_t *zhp = zhp_data;
581
582
if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
583
return (1);
584
585
/* Make sure we're getting the updated enclosure sysfs path */
586
update_vdev_config_dev_sysfs_path(nv, path,
587
ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH);
588
589
nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
590
&vdev_enc_sysfs_path);
591
592
/* Spares show more than once if they're in use, so skip if exists */
593
for (i = 0; i < vcdl->count; i++) {
594
if ((strcmp(vcdl->data[i].path, path) == 0) &&
595
(strcmp(vcdl->data[i].pool, zpool_get_name(zhp)) == 0)) {
596
/* vdev already exists, skip it */
597
return (0);
598
}
599
}
600
601
/* Check for selected vdevs here, if any */
602
for (i = 0; i < vcdl->vdev_names_count; i++) {
603
vname = zpool_vdev_name(g_zfs, zhp, nv, vcdl->cb_name_flags);
604
if (strcmp(vcdl->vdev_names[i], vname) == 0) {
605
free(vname);
606
match = 1;
607
break; /* match */
608
}
609
free(vname);
610
}
611
612
/* If we selected vdevs, and this isn't one of them, then bail out */
613
if (!match && vcdl->vdev_names_count)
614
return (0);
615
616
/*
617
* Resize our array and add in the new element.
618
*/
619
if (!(vcdl->data = realloc(vcdl->data,
620
sizeof (*vcdl->data) * (vcdl->count + 1))))
621
return (ENOMEM); /* couldn't realloc */
622
623
data = &vcdl->data[vcdl->count];
624
625
data->pool = strdup(zpool_get_name(zhp));
626
data->path = strdup(path);
627
data->upath = zfs_get_underlying_path(path);
628
data->cmd = vcdl->cmd;
629
data->lines = data->cols = NULL;
630
data->lines_cnt = data->cols_cnt = 0;
631
if (vdev_enc_sysfs_path)
632
data->vdev_enc_sysfs_path = strdup(vdev_enc_sysfs_path);
633
else
634
data->vdev_enc_sysfs_path = NULL;
635
636
vcdl->count++;
637
638
return (0);
639
}
640
641
/* Get the names and count of the vdevs */
642
static int
643
all_pools_for_each_vdev_gather_cb(zpool_handle_t *zhp, void *cb_vcdl)
644
{
645
return (for_each_vdev(zhp, for_each_vdev_run_cb, cb_vcdl));
646
}
647
648
/*
649
* Now that vcdl is populated with our complete list of vdevs, spawn
650
* off the commands.
651
*/
652
static void
653
all_pools_for_each_vdev_run_vcdl(vdev_cmd_data_list_t *vcdl)
654
{
655
taskq_t *tq = taskq_create("vdev_run_cmd",
656
5 * sysconf(_SC_NPROCESSORS_ONLN), minclsyspri, 1, INT_MAX,
657
TASKQ_DYNAMIC);
658
if (tq == NULL)
659
return;
660
661
/* Spawn off the command for each vdev */
662
for (int i = 0; i < vcdl->count; i++) {
663
(void) taskq_dispatch(tq, vdev_run_cmd_thread,
664
(void *) &vcdl->data[i], TQ_SLEEP);
665
}
666
667
/* Wait for threads to finish */
668
taskq_wait(tq);
669
taskq_destroy(tq);
670
}
671
672
/*
673
* Run command 'cmd' on all vdevs in all pools in argv. Saves the first line of
674
* output from the command in vcdk->data[].line for all vdevs. If you want
675
* to run the command on only certain vdevs, fill in g_zfs, vdev_names,
676
* vdev_names_count, and cb_name_flags. Otherwise leave them as zero.
677
*
678
* Returns a vdev_cmd_data_list_t that must be freed with
679
* free_vdev_cmd_data_list();
680
*/
681
vdev_cmd_data_list_t *
682
all_pools_for_each_vdev_run(int argc, char **argv, char *cmd,
683
libzfs_handle_t *g_zfs, char **vdev_names, int vdev_names_count,
684
int cb_name_flags)
685
{
686
vdev_cmd_data_list_t *vcdl;
687
vcdl = safe_malloc(sizeof (vdev_cmd_data_list_t));
688
vcdl->cmd = cmd;
689
690
vcdl->vdev_names = vdev_names;
691
vcdl->vdev_names_count = vdev_names_count;
692
vcdl->cb_name_flags = cb_name_flags;
693
vcdl->g_zfs = g_zfs;
694
695
/* Gather our list of all vdevs in all pools */
696
for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
697
B_FALSE, all_pools_for_each_vdev_gather_cb, vcdl);
698
699
/* Run command on all vdevs in all pools */
700
all_pools_for_each_vdev_run_vcdl(vcdl);
701
702
/*
703
* vcdl->data[] now contains all the column names and values for each
704
* vdev. We need to process that into a master list of unique column
705
* names, and figure out the width of each column.
706
*/
707
process_unique_cmd_columns(vcdl);
708
709
return (vcdl);
710
}
711
712
/*
713
* Free the vdev_cmd_data_list_t created by all_pools_for_each_vdev_run()
714
*/
715
void
716
free_vdev_cmd_data_list(vdev_cmd_data_list_t *vcdl)
717
{
718
free(vcdl->uniq_cols);
719
free(vcdl->uniq_cols_width);
720
721
for (int i = 0; i < vcdl->count; i++) {
722
free(vcdl->data[i].path);
723
free(vcdl->data[i].pool);
724
free(vcdl->data[i].upath);
725
726
for (int j = 0; j < vcdl->data[i].lines_cnt; j++)
727
free(vcdl->data[i].lines[j]);
728
729
free(vcdl->data[i].lines);
730
731
for (int j = 0; j < vcdl->data[i].cols_cnt; j++)
732
free(vcdl->data[i].cols[j]);
733
734
free(vcdl->data[i].cols);
735
free(vcdl->data[i].vdev_enc_sysfs_path);
736
}
737
free(vcdl->data);
738
free(vcdl);
739
}
740
741