]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
1d3ba0bf | 9 | * or https://opensource.org/licenses/CDDL-1.0. |
34dc7c2f BB |
10 | * See the License for the specific language governing permissions |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
95bcd51e | 24 | * Copyright 2011 Nexenta Systems, Inc. All rights reserved. |
b1e46f86 | 25 | * Copyright (c) 2011, 2024 by Delphix. All rights reserved. |
95bcd51e | 26 | * Copyright (c) 2012 by Frederik Wessels. All rights reserved. |
df831108 | 27 | * Copyright (c) 2012 by Cyril Plisko. All rights reserved. |
eaa52d32 | 28 | * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved. |
8e896579 | 29 | * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>. |
bec1067d | 30 | * Copyright (c) 2017 Datto Inc. |
d3f2cd7e | 31 | * Copyright (c) 2017 Open-E, Inc. All Rights Reserved. |
cc99f275 | 32 | * Copyright (c) 2017, Intel Corporation. |
e55db32a | 33 | * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com> |
658fb802 | 34 | * Copyright (c) 2021, Colm Buckley <colm@tuatha.org> |
2a673e76 | 35 | * Copyright (c) 2021, Klara Inc. |
dce1bf99 | 36 | * Copyright [2021] Hewlett Packard Enterprise Development LP |
34dc7c2f BB |
37 | */ |
38 | ||
34dc7c2f BB |
39 | #include <assert.h> |
40 | #include <ctype.h> | |
41 | #include <dirent.h> | |
42 | #include <errno.h> | |
43 | #include <fcntl.h> | |
d2734cce | 44 | #include <getopt.h> |
34dc7c2f BB |
45 | #include <libgen.h> |
46 | #include <libintl.h> | |
47 | #include <libuutil.h> | |
48 | #include <locale.h> | |
e60e158e | 49 | #include <pthread.h> |
34dc7c2f BB |
50 | #include <stdio.h> |
51 | #include <stdlib.h> | |
52 | #include <string.h> | |
c183d164 | 53 | #include <thread_pool.h> |
e60e158e | 54 | #include <time.h> |
34dc7c2f | 55 | #include <unistd.h> |
34dc7c2f BB |
56 | #include <pwd.h> |
57 | #include <zone.h> | |
d6418de0 | 58 | #include <sys/wait.h> |
1bd201e7 | 59 | #include <zfs_prop.h> |
34dc7c2f | 60 | #include <sys/fs/zfs.h> |
34dc7c2f | 61 | #include <sys/stat.h> |
46364cb2 | 62 | #include <sys/systeminfo.h> |
d02ca379 | 63 | #include <sys/fm/fs/zfs.h> |
26685276 BB |
64 | #include <sys/fm/util.h> |
65 | #include <sys/fm/protocol.h> | |
8c7aa0cf | 66 | #include <sys/zfs_ioctl.h> |
b5256303 | 67 | #include <sys/mount.h> |
d4a72f23 TC |
68 | #include <sys/sysmacros.h> |
69 | ||
193a37cb | 70 | #include <math.h> |
34dc7c2f BB |
71 | |
72 | #include <libzfs.h> | |
e89f1295 | 73 | #include <libzutil.h> |
34dc7c2f BB |
74 | |
75 | #include "zpool_util.h" | |
76 | #include "zfs_comutil.h" | |
9ae529ec | 77 | #include "zfeature_common.h" |
34dc7c2f | 78 | |
428870ff BB |
79 | #include "statcommon.h" |
80 | ||
af09c050 RD |
81 | libzfs_handle_t *g_zfs; |
82 | ||
b64afa41 AS |
83 | static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */ |
84 | ||
34dc7c2f BB |
85 | static int zpool_do_create(int, char **); |
86 | static int zpool_do_destroy(int, char **); | |
87 | ||
88 | static int zpool_do_add(int, char **); | |
89 | static int zpool_do_remove(int, char **); | |
131cc95c | 90 | static int zpool_do_labelclear(int, char **); |
34dc7c2f | 91 | |
d2734cce SD |
92 | static int zpool_do_checkpoint(int, char **); |
93 | ||
34dc7c2f BB |
94 | static int zpool_do_list(int, char **); |
95 | static int zpool_do_iostat(int, char **); | |
96 | static int zpool_do_status(int, char **); | |
97 | ||
98 | static int zpool_do_online(int, char **); | |
99 | static int zpool_do_offline(int, char **); | |
100 | static int zpool_do_clear(int, char **); | |
1bd201e7 | 101 | static int zpool_do_reopen(int, char **); |
34dc7c2f | 102 | |
3541dc6d GA |
103 | static int zpool_do_reguid(int, char **); |
104 | ||
34dc7c2f BB |
105 | static int zpool_do_attach(int, char **); |
106 | static int zpool_do_detach(int, char **); | |
107 | static int zpool_do_replace(int, char **); | |
428870ff | 108 | static int zpool_do_split(int, char **); |
34dc7c2f | 109 | |
619f0976 | 110 | static int zpool_do_initialize(int, char **); |
34dc7c2f | 111 | static int zpool_do_scrub(int, char **); |
80a91e74 | 112 | static int zpool_do_resilver(int, char **); |
1b939560 | 113 | static int zpool_do_trim(int, char **); |
34dc7c2f BB |
114 | |
115 | static int zpool_do_import(int, char **); | |
116 | static int zpool_do_export(int, char **); | |
117 | ||
118 | static int zpool_do_upgrade(int, char **); | |
119 | ||
120 | static int zpool_do_history(int, char **); | |
26685276 | 121 | static int zpool_do_events(int, char **); |
34dc7c2f BB |
122 | |
123 | static int zpool_do_get(int, char **); | |
124 | static int zpool_do_set(int, char **); | |
125 | ||
bec1067d AP |
126 | static int zpool_do_sync(int, char **); |
127 | ||
50478c6d T |
128 | static int zpool_do_version(int, char **); |
129 | ||
e60e158e JG |
130 | static int zpool_do_wait(int, char **); |
131 | ||
7228ba11 RN |
132 | static int zpool_do_help(int argc, char **argv); |
133 | ||
658fb802 CB |
134 | static zpool_compat_status_t zpool_do_load_compat( |
135 | const char *, boolean_t *); | |
136 | ||
b1e46f86 GW |
137 | enum zpool_options { |
138 | ZPOOL_OPTION_POWER = 1024, | |
139 | ZPOOL_OPTION_ALLOW_INUSE, | |
140 | ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH, | |
141 | ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH | |
142 | }; | |
143 | ||
34dc7c2f BB |
144 | /* |
145 | * These libumem hooks provide a reasonable set of defaults for the allocator's | |
146 | * debugging facilities. | |
147 | */ | |
b128c09f BB |
148 | |
149 | #ifdef DEBUG | |
34dc7c2f BB |
150 | const char * |
151 | _umem_debug_init(void) | |
152 | { | |
153 | return ("default,verbose"); /* $UMEM_DEBUG setting */ | |
154 | } | |
155 | ||
156 | const char * | |
157 | _umem_logging_init(void) | |
158 | { | |
159 | return ("fail,contents"); /* $UMEM_LOGGING setting */ | |
160 | } | |
b128c09f | 161 | #endif |
34dc7c2f BB |
162 | |
163 | typedef enum { | |
164 | HELP_ADD, | |
165 | HELP_ATTACH, | |
166 | HELP_CLEAR, | |
167 | HELP_CREATE, | |
d2734cce | 168 | HELP_CHECKPOINT, |
34dc7c2f BB |
169 | HELP_DESTROY, |
170 | HELP_DETACH, | |
171 | HELP_EXPORT, | |
172 | HELP_HISTORY, | |
173 | HELP_IMPORT, | |
174 | HELP_IOSTAT, | |
131cc95c | 175 | HELP_LABELCLEAR, |
34dc7c2f BB |
176 | HELP_LIST, |
177 | HELP_OFFLINE, | |
178 | HELP_ONLINE, | |
179 | HELP_REPLACE, | |
180 | HELP_REMOVE, | |
619f0976 | 181 | HELP_INITIALIZE, |
34dc7c2f | 182 | HELP_SCRUB, |
80a91e74 | 183 | HELP_RESILVER, |
1b939560 | 184 | HELP_TRIM, |
34dc7c2f BB |
185 | HELP_STATUS, |
186 | HELP_UPGRADE, | |
26685276 | 187 | HELP_EVENTS, |
34dc7c2f | 188 | HELP_GET, |
428870ff | 189 | HELP_SET, |
3541dc6d | 190 | HELP_SPLIT, |
bec1067d | 191 | HELP_SYNC, |
1bd201e7 | 192 | HELP_REGUID, |
50478c6d | 193 | HELP_REOPEN, |
e60e158e JG |
194 | HELP_VERSION, |
195 | HELP_WAIT | |
34dc7c2f BB |
196 | } zpool_help_t; |
197 | ||
198 | ||
193a37cb TH |
199 | /* |
200 | * Flags for stats to display with "zpool iostats" | |
201 | */ | |
202 | enum iostat_type { | |
203 | IOS_DEFAULT = 0, | |
204 | IOS_LATENCY = 1, | |
205 | IOS_QUEUES = 2, | |
206 | IOS_L_HISTO = 3, | |
7e945072 | 207 | IOS_RQ_HISTO = 4, |
193a37cb TH |
208 | IOS_COUNT, /* always last element */ |
209 | }; | |
210 | ||
211 | /* iostat_type entries as bitmasks */ | |
212 | #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT) | |
213 | #define IOS_LATENCY_M (1ULL << IOS_LATENCY) | |
214 | #define IOS_QUEUES_M (1ULL << IOS_QUEUES) | |
215 | #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO) | |
7e945072 TH |
216 | #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO) |
217 | ||
218 | /* Mask of all the histo bits */ | |
219 | #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M) | |
220 | ||
221 | /* | |
222 | * Lookup table for iostat flags to nvlist names. Basically a list | |
223 | * of all the nvlists a flag requires. Also specifies the order in | |
224 | * which data gets printed in zpool iostat. | |
225 | */ | |
00888c08 | 226 | static const char *vsx_type_to_nvlist[IOS_COUNT][15] = { |
7e945072 TH |
227 | [IOS_L_HISTO] = { |
228 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
229 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
230 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
231 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
232 | ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, | |
233 | ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, | |
234 | ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, | |
235 | ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, | |
236 | ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, | |
1b939560 | 237 | ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, |
00888c08 | 238 | ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, |
7e945072 TH |
239 | NULL}, |
240 | [IOS_LATENCY] = { | |
241 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
242 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
243 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
244 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
1b939560 | 245 | ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, |
00888c08 | 246 | ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, |
7e945072 TH |
247 | NULL}, |
248 | [IOS_QUEUES] = { | |
249 | ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, | |
250 | ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, | |
251 | ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, | |
252 | ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, | |
253 | ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, | |
1b939560 | 254 | ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, |
00888c08 | 255 | ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE, |
7e945072 TH |
256 | NULL}, |
257 | [IOS_RQ_HISTO] = { | |
258 | ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO, | |
259 | ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO, | |
260 | ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO, | |
261 | ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO, | |
262 | ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO, | |
263 | ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO, | |
264 | ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO, | |
265 | ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO, | |
266 | ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO, | |
267 | ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO, | |
1b939560 BB |
268 | ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO, |
269 | ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO, | |
00888c08 TB |
270 | ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO, |
271 | ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO, | |
7e945072 TH |
272 | NULL}, |
273 | }; | |
274 | ||
275 | ||
276 | /* | |
277 | * Given a cb->cb_flags with a histogram bit set, return the iostat_type. | |
278 | * Right now, only one histo bit is ever set at one time, so we can | |
279 | * just do a highbit64(a) | |
280 | */ | |
281 | #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1) | |
193a37cb | 282 | |
34dc7c2f BB |
283 | typedef struct zpool_command { |
284 | const char *name; | |
285 | int (*func)(int, char **); | |
286 | zpool_help_t usage; | |
287 | } zpool_command_t; | |
288 | ||
289 | /* | |
290 | * Master command table. Each ZFS command has a name, associated function, and | |
291 | * usage message. The usage messages need to be internationalized, so we have | |
292 | * to have a function to return the usage message based on a command index. | |
293 | * | |
294 | * These commands are organized according to how they are displayed in the usage | |
295 | * message. An empty command (one with a NULL name) indicates an empty line in | |
296 | * the generic usage message. | |
297 | */ | |
298 | static zpool_command_t command_table[] = { | |
50478c6d T |
299 | { "version", zpool_do_version, HELP_VERSION }, |
300 | { NULL }, | |
34dc7c2f BB |
301 | { "create", zpool_do_create, HELP_CREATE }, |
302 | { "destroy", zpool_do_destroy, HELP_DESTROY }, | |
303 | { NULL }, | |
304 | { "add", zpool_do_add, HELP_ADD }, | |
305 | { "remove", zpool_do_remove, HELP_REMOVE }, | |
306 | { NULL }, | |
131cc95c DK |
307 | { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR }, |
308 | { NULL }, | |
d2734cce SD |
309 | { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT }, |
310 | { NULL }, | |
34dc7c2f BB |
311 | { "list", zpool_do_list, HELP_LIST }, |
312 | { "iostat", zpool_do_iostat, HELP_IOSTAT }, | |
313 | { "status", zpool_do_status, HELP_STATUS }, | |
314 | { NULL }, | |
315 | { "online", zpool_do_online, HELP_ONLINE }, | |
316 | { "offline", zpool_do_offline, HELP_OFFLINE }, | |
317 | { "clear", zpool_do_clear, HELP_CLEAR }, | |
1bd201e7 | 318 | { "reopen", zpool_do_reopen, HELP_REOPEN }, |
34dc7c2f BB |
319 | { NULL }, |
320 | { "attach", zpool_do_attach, HELP_ATTACH }, | |
321 | { "detach", zpool_do_detach, HELP_DETACH }, | |
322 | { "replace", zpool_do_replace, HELP_REPLACE }, | |
428870ff | 323 | { "split", zpool_do_split, HELP_SPLIT }, |
34dc7c2f | 324 | { NULL }, |
619f0976 | 325 | { "initialize", zpool_do_initialize, HELP_INITIALIZE }, |
80a91e74 | 326 | { "resilver", zpool_do_resilver, HELP_RESILVER }, |
1b939560 BB |
327 | { "scrub", zpool_do_scrub, HELP_SCRUB }, |
328 | { "trim", zpool_do_trim, HELP_TRIM }, | |
34dc7c2f BB |
329 | { NULL }, |
330 | { "import", zpool_do_import, HELP_IMPORT }, | |
331 | { "export", zpool_do_export, HELP_EXPORT }, | |
332 | { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, | |
3541dc6d | 333 | { "reguid", zpool_do_reguid, HELP_REGUID }, |
34dc7c2f BB |
334 | { NULL }, |
335 | { "history", zpool_do_history, HELP_HISTORY }, | |
26685276 BB |
336 | { "events", zpool_do_events, HELP_EVENTS }, |
337 | { NULL }, | |
34dc7c2f BB |
338 | { "get", zpool_do_get, HELP_GET }, |
339 | { "set", zpool_do_set, HELP_SET }, | |
bec1067d | 340 | { "sync", zpool_do_sync, HELP_SYNC }, |
e60e158e JG |
341 | { NULL }, |
342 | { "wait", zpool_do_wait, HELP_WAIT }, | |
34dc7c2f BB |
343 | }; |
344 | ||
193a37cb | 345 | #define NCOMMAND (ARRAY_SIZE(command_table)) |
34dc7c2f | 346 | |
cc99f275 DB |
347 | #define VDEV_ALLOC_CLASS_LOGS "logs" |
348 | ||
6f1ffb06 | 349 | static zpool_command_t *current_command; |
2a673e76 | 350 | static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV); |
34dc7c2f | 351 | static char history_str[HIS_MAX_RECORD_LEN]; |
6f1ffb06 | 352 | static boolean_t log_history = B_TRUE; |
428870ff BB |
353 | static uint_t timestamp_fmt = NODATE; |
354 | ||
34dc7c2f | 355 | static const char * |
e9aa730c GM |
356 | get_usage(zpool_help_t idx) |
357 | { | |
34dc7c2f BB |
358 | switch (idx) { |
359 | case HELP_ADD: | |
b1e46f86 | 360 | return (gettext("\tadd [-afgLnP] [-o property=value] " |
df831108 | 361 | "<pool> <vdev> ...\n")); |
34dc7c2f | 362 | case HELP_ATTACH: |
9a49d3f3 | 363 | return (gettext("\tattach [-fsw] [-o property=value] " |
df831108 | 364 | "<pool> <device> <new-device>\n")); |
34dc7c2f | 365 | case HELP_CLEAR: |
a9520e6e | 366 | return (gettext("\tclear [[--power]|[-nF]] <pool> [device]\n")); |
34dc7c2f | 367 | case HELP_CREATE: |
9ae529ec | 368 | return (gettext("\tcreate [-fnd] [-o property=value] ... \n" |
b128c09f | 369 | "\t [-O file-system-property=value] ... \n" |
34dc7c2f | 370 | "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); |
d2734cce | 371 | case HELP_CHECKPOINT: |
e60e158e | 372 | return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n")); |
34dc7c2f BB |
373 | case HELP_DESTROY: |
374 | return (gettext("\tdestroy [-f] <pool>\n")); | |
375 | case HELP_DETACH: | |
376 | return (gettext("\tdetach <pool> <device>\n")); | |
377 | case HELP_EXPORT: | |
859735c0 | 378 | return (gettext("\texport [-af] <pool> ...\n")); |
34dc7c2f BB |
379 | case HELP_HISTORY: |
380 | return (gettext("\thistory [-il] [<pool>] ...\n")); | |
381 | case HELP_IMPORT: | |
382 | return (gettext("\timport [-d dir] [-D]\n" | |
383 | "\timport [-o mntopts] [-o property=value] ... \n" | |
b5256303 | 384 | "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " |
572e2857 | 385 | "[-R root] [-F [-n]] -a\n" |
34dc7c2f | 386 | "\timport [-o mntopts] [-o property=value] ... \n" |
b5256303 | 387 | "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " |
572e2857 | 388 | "[-R root] [-F [-n]]\n" |
d2734cce | 389 | "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n")); |
34dc7c2f | 390 | case HELP_IOSTAT: |
d6418de0 TH |
391 | return (gettext("\tiostat [[[-c [script1,script2,...]" |
392 | "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n" | |
393 | "\t [[pool ...]|[pool vdev ...]|[vdev ...]]" | |
8fccfa8e | 394 | " [[-n] interval [count]]\n")); |
131cc95c DK |
395 | case HELP_LABELCLEAR: |
396 | return (gettext("\tlabelclear [-f] <vdev>\n")); | |
34dc7c2f | 397 | case HELP_LIST: |
2a8b84b7 | 398 | return (gettext("\tlist [-gHLpPv] [-o property[,...]] " |
63f88c12 | 399 | "[-T d|u] [pool] ... \n" |
400 | "\t [interval [count]]\n")); | |
34dc7c2f | 401 | case HELP_OFFLINE: |
a9520e6e TH |
402 | return (gettext("\toffline [--power]|[[-f][-t]] <pool> " |
403 | "<device> ...\n")); | |
34dc7c2f | 404 | case HELP_ONLINE: |
a9520e6e TH |
405 | return (gettext("\tonline [--power][-e] <pool> <device> " |
406 | "...\n")); | |
34dc7c2f | 407 | case HELP_REPLACE: |
9a49d3f3 | 408 | return (gettext("\treplace [-fsw] [-o property=value] " |
628668a3 | 409 | "<pool> <device> [new-device]\n")); |
34dc7c2f | 410 | case HELP_REMOVE: |
e60e158e | 411 | return (gettext("\tremove [-npsw] <pool> <device> ...\n")); |
1bd201e7 | 412 | case HELP_REOPEN: |
d3f2cd7e | 413 | return (gettext("\treopen [-n] <pool>\n")); |
619f0976 | 414 | case HELP_INITIALIZE: |
e34e15ed | 415 | return (gettext("\tinitialize [-c | -s | -u] [-w] <pool> " |
a769fb53 | 416 | "[<device> ...]\n")); |
34dc7c2f | 417 | case HELP_SCRUB: |
482eeef8 | 418 | return (gettext("\tscrub [-s | -p] [-w] [-e] <pool> ...\n")); |
80a91e74 TC |
419 | case HELP_RESILVER: |
420 | return (gettext("\tresilver <pool> ...\n")); | |
1b939560 | 421 | case HELP_TRIM: |
2288d419 | 422 | return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> " |
1b939560 | 423 | "[<device> ...]\n")); |
34dc7c2f | 424 | case HELP_STATUS: |
a9520e6e | 425 | return (gettext("\tstatus [--power] [-c [script1,script2,...]] " |
c9d8f6c5 | 426 | "[-DegiLpPstvx] [-T d|u] [pool] ...\n" |
63f88c12 | 427 | "\t [interval [count]]\n")); |
34dc7c2f BB |
428 | case HELP_UPGRADE: |
429 | return (gettext("\tupgrade\n" | |
430 | "\tupgrade -v\n" | |
431 | "\tupgrade [-V version] <-a | pool ...>\n")); | |
26685276 | 432 | case HELP_EVENTS: |
88f9c939 | 433 | return (gettext("\tevents [-vHf [pool] | -c]\n")); |
34dc7c2f | 434 | case HELP_GET: |
2a8b84b7 AS |
435 | return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] " |
436 | "<\"all\" | property[,...]> <pool> ...\n")); | |
34dc7c2f | 437 | case HELP_SET: |
a0276f70 RW |
438 | return (gettext("\tset <property=value> <pool>\n" |
439 | "\tset <vdev_property=value> <pool> <vdev>\n")); | |
428870ff | 440 | case HELP_SPLIT: |
b5256303 | 441 | return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n" |
428870ff BB |
442 | "\t [-o property=value] <pool> <newpool> " |
443 | "[<device> ...]\n")); | |
3541dc6d GA |
444 | case HELP_REGUID: |
445 | return (gettext("\treguid <pool>\n")); | |
bec1067d AP |
446 | case HELP_SYNC: |
447 | return (gettext("\tsync [pool] ...\n")); | |
50478c6d T |
448 | case HELP_VERSION: |
449 | return (gettext("\tversion\n")); | |
e60e158e JG |
450 | case HELP_WAIT: |
451 | return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] " | |
452 | "<pool> [interval]\n")); | |
90f1c3c9 AZ |
453 | default: |
454 | __builtin_unreachable(); | |
34dc7c2f | 455 | } |
34dc7c2f BB |
456 | } |
457 | ||
619f0976 GW |
458 | static void |
459 | zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res) | |
460 | { | |
461 | uint_t children = 0; | |
462 | nvlist_t **child; | |
463 | uint_t i; | |
464 | ||
465 | (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
466 | &child, &children); | |
467 | ||
468 | if (children == 0) { | |
1b939560 BB |
469 | char *path = zpool_vdev_name(g_zfs, zhp, nvroot, |
470 | VDEV_NAME_PATH); | |
471 | ||
a84c92f9 NH |
472 | if (strcmp(path, VDEV_TYPE_INDIRECT) != 0 && |
473 | strcmp(path, VDEV_TYPE_HOLE) != 0) | |
1b939560 BB |
474 | fnvlist_add_boolean(res, path); |
475 | ||
619f0976 GW |
476 | free(path); |
477 | return; | |
478 | } | |
479 | ||
480 | for (i = 0; i < children; i++) { | |
481 | zpool_collect_leaves(zhp, child[i], res); | |
482 | } | |
483 | } | |
34dc7c2f BB |
484 | |
485 | /* | |
486 | * Callback routine that will print out a pool property value. | |
487 | */ | |
488 | static int | |
2a673e76 | 489 | print_pool_prop_cb(int prop, void *cb) |
34dc7c2f BB |
490 | { |
491 | FILE *fp = cb; | |
492 | ||
cc99f275 | 493 | (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop)); |
34dc7c2f BB |
494 | |
495 | if (zpool_prop_readonly(prop)) | |
496 | (void) fprintf(fp, " NO "); | |
497 | else | |
428870ff | 498 | (void) fprintf(fp, " YES "); |
34dc7c2f BB |
499 | |
500 | if (zpool_prop_values(prop) == NULL) | |
501 | (void) fprintf(fp, "-\n"); | |
502 | else | |
503 | (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); | |
504 | ||
505 | return (ZPROP_CONT); | |
506 | } | |
507 | ||
2a673e76 AJ |
508 | /* |
509 | * Callback routine that will print out a vdev property value. | |
510 | */ | |
511 | static int | |
512 | print_vdev_prop_cb(int prop, void *cb) | |
513 | { | |
514 | FILE *fp = cb; | |
515 | ||
516 | (void) fprintf(fp, "\t%-19s ", vdev_prop_to_name(prop)); | |
517 | ||
518 | if (vdev_prop_readonly(prop)) | |
519 | (void) fprintf(fp, " NO "); | |
520 | else | |
521 | (void) fprintf(fp, " YES "); | |
522 | ||
523 | if (vdev_prop_values(prop) == NULL) | |
524 | (void) fprintf(fp, "-\n"); | |
525 | else | |
526 | (void) fprintf(fp, "%s\n", vdev_prop_values(prop)); | |
527 | ||
528 | return (ZPROP_CONT); | |
529 | } | |
530 | ||
a9520e6e TH |
531 | /* |
532 | * Given a leaf vdev name like 'L5' return its VDEV_CONFIG_PATH like | |
533 | * '/dev/disk/by-vdev/L5'. | |
534 | */ | |
535 | static const char * | |
536 | vdev_name_to_path(zpool_handle_t *zhp, char *vdev) | |
537 | { | |
538 | nvlist_t *vdev_nv = zpool_find_vdev(zhp, vdev, NULL, NULL, NULL); | |
539 | if (vdev_nv == NULL) { | |
540 | return (NULL); | |
541 | } | |
542 | return (fnvlist_lookup_string(vdev_nv, ZPOOL_CONFIG_PATH)); | |
543 | } | |
544 | ||
545 | static int | |
546 | zpool_power_on(zpool_handle_t *zhp, char *vdev) | |
547 | { | |
548 | return (zpool_power(zhp, vdev, B_TRUE)); | |
549 | } | |
550 | ||
551 | static int | |
552 | zpool_power_on_and_disk_wait(zpool_handle_t *zhp, char *vdev) | |
553 | { | |
554 | int rc; | |
555 | ||
556 | rc = zpool_power_on(zhp, vdev); | |
557 | if (rc != 0) | |
558 | return (rc); | |
559 | ||
560 | zpool_disk_wait(vdev_name_to_path(zhp, vdev)); | |
561 | ||
562 | return (0); | |
563 | } | |
564 | ||
565 | static int | |
566 | zpool_power_on_pool_and_wait_for_devices(zpool_handle_t *zhp) | |
567 | { | |
568 | nvlist_t *nv; | |
569 | const char *path = NULL; | |
570 | int rc; | |
571 | ||
572 | /* Power up all the devices first */ | |
573 | FOR_EACH_REAL_LEAF_VDEV(zhp, nv) { | |
574 | path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH); | |
575 | if (path != NULL) { | |
576 | rc = zpool_power_on(zhp, (char *)path); | |
577 | if (rc != 0) { | |
578 | return (rc); | |
579 | } | |
580 | } | |
581 | } | |
582 | ||
583 | /* | |
584 | * Wait for their devices to show up. Since we powered them on | |
585 | * at roughly the same time, they should all come online around | |
586 | * the same time. | |
587 | */ | |
588 | FOR_EACH_REAL_LEAF_VDEV(zhp, nv) { | |
589 | path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH); | |
590 | zpool_disk_wait(path); | |
591 | } | |
592 | ||
593 | return (0); | |
594 | } | |
595 | ||
596 | static int | |
597 | zpool_power_off(zpool_handle_t *zhp, char *vdev) | |
598 | { | |
599 | return (zpool_power(zhp, vdev, B_FALSE)); | |
600 | } | |
601 | ||
34dc7c2f BB |
602 | /* |
603 | * Display usage message. If we're inside a command, display only the usage for | |
604 | * that command. Otherwise, iterate over the entire command table and display | |
605 | * a complete usage message. | |
606 | */ | |
460748d4 | 607 | static __attribute__((noreturn)) void |
34dc7c2f BB |
608 | usage(boolean_t requested) |
609 | { | |
610 | FILE *fp = requested ? stdout : stderr; | |
611 | ||
612 | if (current_command == NULL) { | |
613 | int i; | |
614 | ||
615 | (void) fprintf(fp, gettext("usage: zpool command args ...\n")); | |
616 | (void) fprintf(fp, | |
617 | gettext("where 'command' is one of the following:\n\n")); | |
618 | ||
619 | for (i = 0; i < NCOMMAND; i++) { | |
620 | if (command_table[i].name == NULL) | |
621 | (void) fprintf(fp, "\n"); | |
622 | else | |
623 | (void) fprintf(fp, "%s", | |
624 | get_usage(command_table[i].usage)); | |
625 | } | |
7228ba11 RN |
626 | |
627 | (void) fprintf(fp, | |
628 | gettext("\nFor further help on a command or topic, " | |
629 | "run: %s\n"), "zpool help [<topic>]"); | |
34dc7c2f BB |
630 | } else { |
631 | (void) fprintf(fp, gettext("usage:\n")); | |
632 | (void) fprintf(fp, "%s", get_usage(current_command->usage)); | |
633 | } | |
634 | ||
635 | if (current_command != NULL && | |
2a673e76 | 636 | current_prop_type != (ZFS_TYPE_POOL | ZFS_TYPE_VDEV) && |
34dc7c2f BB |
637 | ((strcmp(current_command->name, "set") == 0) || |
638 | (strcmp(current_command->name, "get") == 0) || | |
639 | (strcmp(current_command->name, "list") == 0))) { | |
640 | ||
3c1e1933 | 641 | (void) fprintf(fp, "%s", |
34dc7c2f BB |
642 | gettext("\nthe following properties are supported:\n")); |
643 | ||
cc99f275 | 644 | (void) fprintf(fp, "\n\t%-19s %s %s\n\n", |
34dc7c2f BB |
645 | "PROPERTY", "EDIT", "VALUES"); |
646 | ||
647 | /* Iterate over all properties */ | |
2a673e76 AJ |
648 | if (current_prop_type == ZFS_TYPE_POOL) { |
649 | (void) zprop_iter(print_pool_prop_cb, fp, B_FALSE, | |
650 | B_TRUE, current_prop_type); | |
9ae529ec | 651 | |
2a673e76 AJ |
652 | (void) fprintf(fp, "\t%-19s ", "feature@..."); |
653 | (void) fprintf(fp, "YES " | |
654 | "disabled | enabled | active\n"); | |
9ae529ec | 655 | |
2a673e76 AJ |
656 | (void) fprintf(fp, gettext("\nThe feature@ properties " |
657 | "must be appended with a feature name.\n" | |
658 | "See zpool-features(7).\n")); | |
659 | } else if (current_prop_type == ZFS_TYPE_VDEV) { | |
660 | (void) zprop_iter(print_vdev_prop_cb, fp, B_FALSE, | |
661 | B_TRUE, current_prop_type); | |
662 | } | |
34dc7c2f BB |
663 | } |
664 | ||
665 | /* | |
666 | * See comments at end of main(). | |
667 | */ | |
668 | if (getenv("ZFS_ABORT") != NULL) { | |
669 | (void) printf("dumping core by request\n"); | |
670 | abort(); | |
671 | } | |
672 | ||
673 | exit(requested ? 0 : 2); | |
674 | } | |
675 | ||
619f0976 | 676 | /* |
e34e15ed | 677 | * zpool initialize [-c | -s | -u] [-w] <pool> [<vdev> ...] |
619f0976 GW |
678 | * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool |
679 | * if none specified. | |
680 | * | |
681 | * -c Cancel. Ends active initializing. | |
682 | * -s Suspend. Initializing can then be restarted with no flags. | |
e34e15ed | 683 | * -u Uninitialize. Clears initialization state. |
e60e158e | 684 | * -w Wait. Blocks until initializing has completed. |
619f0976 GW |
685 | */ |
686 | int | |
687 | zpool_do_initialize(int argc, char **argv) | |
688 | { | |
689 | int c; | |
690 | char *poolname; | |
691 | zpool_handle_t *zhp; | |
692 | nvlist_t *vdevs; | |
693 | int err = 0; | |
e60e158e | 694 | boolean_t wait = B_FALSE; |
619f0976 GW |
695 | |
696 | struct option long_options[] = { | |
697 | {"cancel", no_argument, NULL, 'c'}, | |
698 | {"suspend", no_argument, NULL, 's'}, | |
e34e15ed | 699 | {"uninit", no_argument, NULL, 'u'}, |
e60e158e | 700 | {"wait", no_argument, NULL, 'w'}, |
619f0976 GW |
701 | {0, 0, 0, 0} |
702 | }; | |
703 | ||
1b939560 | 704 | pool_initialize_func_t cmd_type = POOL_INITIALIZE_START; |
e34e15ed BB |
705 | while ((c = getopt_long(argc, argv, "csuw", long_options, |
706 | NULL)) != -1) { | |
619f0976 GW |
707 | switch (c) { |
708 | case 'c': | |
1b939560 | 709 | if (cmd_type != POOL_INITIALIZE_START && |
a769fb53 | 710 | cmd_type != POOL_INITIALIZE_CANCEL) { |
619f0976 GW |
711 | (void) fprintf(stderr, gettext("-c cannot be " |
712 | "combined with other options\n")); | |
713 | usage(B_FALSE); | |
714 | } | |
715 | cmd_type = POOL_INITIALIZE_CANCEL; | |
716 | break; | |
717 | case 's': | |
1b939560 | 718 | if (cmd_type != POOL_INITIALIZE_START && |
a769fb53 | 719 | cmd_type != POOL_INITIALIZE_SUSPEND) { |
619f0976 GW |
720 | (void) fprintf(stderr, gettext("-s cannot be " |
721 | "combined with other options\n")); | |
722 | usage(B_FALSE); | |
723 | } | |
724 | cmd_type = POOL_INITIALIZE_SUSPEND; | |
725 | break; | |
e34e15ed BB |
726 | case 'u': |
727 | if (cmd_type != POOL_INITIALIZE_START && | |
728 | cmd_type != POOL_INITIALIZE_UNINIT) { | |
729 | (void) fprintf(stderr, gettext("-u cannot be " | |
730 | "combined with other options\n")); | |
731 | usage(B_FALSE); | |
732 | } | |
733 | cmd_type = POOL_INITIALIZE_UNINIT; | |
734 | break; | |
e60e158e JG |
735 | case 'w': |
736 | wait = B_TRUE; | |
737 | break; | |
619f0976 GW |
738 | case '?': |
739 | if (optopt != 0) { | |
740 | (void) fprintf(stderr, | |
741 | gettext("invalid option '%c'\n"), optopt); | |
742 | } else { | |
743 | (void) fprintf(stderr, | |
744 | gettext("invalid option '%s'\n"), | |
745 | argv[optind - 1]); | |
746 | } | |
747 | usage(B_FALSE); | |
748 | } | |
749 | } | |
750 | ||
751 | argc -= optind; | |
752 | argv += optind; | |
753 | ||
754 | if (argc < 1) { | |
755 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
756 | usage(B_FALSE); | |
757 | return (-1); | |
758 | } | |
759 | ||
e60e158e | 760 | if (wait && (cmd_type != POOL_INITIALIZE_START)) { |
e34e15ed BB |
761 | (void) fprintf(stderr, gettext("-w cannot be used with -c, -s" |
762 | "or -u\n")); | |
e60e158e JG |
763 | usage(B_FALSE); |
764 | } | |
765 | ||
619f0976 GW |
766 | poolname = argv[0]; |
767 | zhp = zpool_open(g_zfs, poolname); | |
768 | if (zhp == NULL) | |
769 | return (-1); | |
770 | ||
771 | vdevs = fnvlist_alloc(); | |
772 | if (argc == 1) { | |
773 | /* no individual leaf vdevs specified, so add them all */ | |
774 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
775 | nvlist_t *nvroot = fnvlist_lookup_nvlist(config, | |
776 | ZPOOL_CONFIG_VDEV_TREE); | |
777 | zpool_collect_leaves(zhp, nvroot, vdevs); | |
778 | } else { | |
1b939560 | 779 | for (int i = 1; i < argc; i++) { |
619f0976 GW |
780 | fnvlist_add_boolean(vdevs, argv[i]); |
781 | } | |
782 | } | |
783 | ||
e60e158e JG |
784 | if (wait) |
785 | err = zpool_initialize_wait(zhp, cmd_type, vdevs); | |
786 | else | |
787 | err = zpool_initialize(zhp, cmd_type, vdevs); | |
619f0976 GW |
788 | |
789 | fnvlist_free(vdevs); | |
790 | zpool_close(zhp); | |
791 | ||
792 | return (err); | |
793 | } | |
794 | ||
cc99f275 DB |
795 | /* |
796 | * print a pool vdev config for dry runs | |
797 | */ | |
798 | static void | |
34dc7c2f | 799 | print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, |
cc99f275 | 800 | const char *match, int name_flags) |
34dc7c2f BB |
801 | { |
802 | nvlist_t **child; | |
803 | uint_t c, children; | |
804 | char *vname; | |
cc99f275 | 805 | boolean_t printed = B_FALSE; |
34dc7c2f BB |
806 | |
807 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
cc99f275 DB |
808 | &child, &children) != 0) { |
809 | if (name != NULL) | |
810 | (void) printf("\t%*s%s\n", indent, "", name); | |
34dc7c2f | 811 | return; |
cc99f275 | 812 | } |
34dc7c2f BB |
813 | |
814 | for (c = 0; c < children; c++) { | |
0cb40fa3 | 815 | uint64_t is_log = B_FALSE, is_hole = B_FALSE; |
d1807f16 | 816 | const char *class = ""; |
34dc7c2f | 817 | |
0cb40fa3 AF |
818 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, |
819 | &is_hole); | |
820 | ||
821 | if (is_hole == B_TRUE) { | |
822 | continue; | |
823 | } | |
824 | ||
34dc7c2f BB |
825 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
826 | &is_log); | |
cc99f275 | 827 | if (is_log) |
d1807f16 | 828 | class = VDEV_ALLOC_BIAS_LOG; |
cc99f275 DB |
829 | (void) nvlist_lookup_string(child[c], |
830 | ZPOOL_CONFIG_ALLOCATION_BIAS, &class); | |
831 | if (strcmp(match, class) != 0) | |
34dc7c2f BB |
832 | continue; |
833 | ||
cc99f275 DB |
834 | if (!printed && name != NULL) { |
835 | (void) printf("\t%*s%s\n", indent, "", name); | |
836 | printed = B_TRUE; | |
837 | } | |
d2f3e292 | 838 | vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags); |
cc99f275 DB |
839 | print_vdev_tree(zhp, vname, child[c], indent + 2, "", |
840 | name_flags); | |
34dc7c2f BB |
841 | free(vname); |
842 | } | |
843 | } | |
844 | ||
0cb40fa3 AF |
845 | /* |
846 | * Print the list of l2cache devices for dry runs. | |
847 | */ | |
848 | static void | |
849 | print_cache_list(nvlist_t *nv, int indent) | |
850 | { | |
851 | nvlist_t **child; | |
852 | uint_t c, children; | |
853 | ||
854 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, | |
855 | &child, &children) == 0 && children > 0) { | |
856 | (void) printf("\t%*s%s\n", indent, "", "cache"); | |
857 | } else { | |
858 | return; | |
859 | } | |
860 | for (c = 0; c < children; c++) { | |
861 | char *vname; | |
862 | ||
863 | vname = zpool_vdev_name(g_zfs, NULL, child[c], 0); | |
864 | (void) printf("\t%*s%s\n", indent + 2, "", vname); | |
865 | free(vname); | |
866 | } | |
867 | } | |
868 | ||
869 | /* | |
870 | * Print the list of spares for dry runs. | |
871 | */ | |
872 | static void | |
873 | print_spare_list(nvlist_t *nv, int indent) | |
874 | { | |
875 | nvlist_t **child; | |
876 | uint_t c, children; | |
877 | ||
878 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, | |
879 | &child, &children) == 0 && children > 0) { | |
880 | (void) printf("\t%*s%s\n", indent, "", "spares"); | |
881 | } else { | |
882 | return; | |
883 | } | |
884 | for (c = 0; c < children; c++) { | |
885 | char *vname; | |
886 | ||
887 | vname = zpool_vdev_name(g_zfs, NULL, child[c], 0); | |
888 | (void) printf("\t%*s%s\n", indent + 2, "", vname); | |
889 | free(vname); | |
890 | } | |
891 | } | |
892 | ||
b9b24bb4 CS |
893 | static boolean_t |
894 | prop_list_contains_feature(nvlist_t *proplist) | |
895 | { | |
896 | nvpair_t *nvp; | |
897 | for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; | |
898 | nvp = nvlist_next_nvpair(proplist, nvp)) { | |
899 | if (zpool_prop_feature(nvpair_name(nvp))) | |
900 | return (B_TRUE); | |
901 | } | |
902 | return (B_FALSE); | |
903 | } | |
904 | ||
34dc7c2f BB |
905 | /* |
906 | * Add a property pair (name, string-value) into a property nvlist. | |
907 | */ | |
908 | static int | |
a926aab9 | 909 | add_prop_list(const char *propname, const char *propval, nvlist_t **props, |
b128c09f | 910 | boolean_t poolprop) |
34dc7c2f | 911 | { |
31864e3d | 912 | zpool_prop_t prop = ZPOOL_PROP_INVAL; |
34dc7c2f | 913 | nvlist_t *proplist; |
b128c09f | 914 | const char *normnm; |
d1807f16 | 915 | const char *strval; |
34dc7c2f BB |
916 | |
917 | if (*props == NULL && | |
918 | nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { | |
919 | (void) fprintf(stderr, | |
920 | gettext("internal error: out of memory\n")); | |
921 | return (1); | |
922 | } | |
923 | ||
924 | proplist = *props; | |
925 | ||
b128c09f | 926 | if (poolprop) { |
b9b24bb4 | 927 | const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); |
e086db16 | 928 | const char *cname = |
658fb802 | 929 | zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY); |
b9b24bb4 | 930 | |
31864e3d | 931 | if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL && |
2a673e76 AJ |
932 | (!zpool_prop_feature(propname) && |
933 | !zpool_prop_vdev(propname))) { | |
b128c09f | 934 | (void) fprintf(stderr, gettext("property '%s' is " |
2a673e76 | 935 | "not a valid pool or vdev property\n"), propname); |
b128c09f BB |
936 | return (2); |
937 | } | |
b9b24bb4 CS |
938 | |
939 | /* | |
940 | * feature@ properties and version should not be specified | |
941 | * at the same time. | |
942 | */ | |
31864e3d | 943 | if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) && |
b9b24bb4 CS |
944 | nvlist_exists(proplist, vname)) || |
945 | (prop == ZPOOL_PROP_VERSION && | |
946 | prop_list_contains_feature(proplist))) { | |
947 | (void) fprintf(stderr, gettext("'feature@' and " | |
948 | "'version' properties cannot be specified " | |
949 | "together\n")); | |
950 | return (2); | |
951 | } | |
952 | ||
658fb802 | 953 | /* |
e086db16 CB |
954 | * if version is specified, only "legacy" compatibility |
955 | * may be requested | |
658fb802 CB |
956 | */ |
957 | if ((prop == ZPOOL_PROP_COMPATIBILITY && | |
e086db16 | 958 | strcmp(propval, ZPOOL_COMPAT_LEGACY) != 0 && |
658fb802 CB |
959 | nvlist_exists(proplist, vname)) || |
960 | (prop == ZPOOL_PROP_VERSION && | |
e086db16 CB |
961 | nvlist_exists(proplist, cname) && |
962 | strcmp(fnvlist_lookup_string(proplist, cname), | |
963 | ZPOOL_COMPAT_LEGACY) != 0)) { | |
964 | (void) fprintf(stderr, gettext("when 'version' is " | |
965 | "specified, the 'compatibility' feature may only " | |
966 | "be set to '" ZPOOL_COMPAT_LEGACY "'\n")); | |
658fb802 CB |
967 | return (2); |
968 | } | |
b9b24bb4 | 969 | |
2a673e76 | 970 | if (zpool_prop_feature(propname) || zpool_prop_vdev(propname)) |
9ae529ec CS |
971 | normnm = propname; |
972 | else | |
973 | normnm = zpool_prop_to_name(prop); | |
b128c09f | 974 | } else { |
5140a58f | 975 | zfs_prop_t fsprop = zfs_name_to_prop(propname); |
976 | ||
977 | if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM, | |
978 | B_FALSE)) { | |
979 | normnm = zfs_prop_to_name(fsprop); | |
980 | } else if (zfs_prop_user(propname) || | |
981 | zfs_prop_userquota(propname)) { | |
9babb374 | 982 | normnm = propname; |
5140a58f | 983 | } else { |
984 | (void) fprintf(stderr, gettext("property '%s' is " | |
985 | "not a valid filesystem property\n"), propname); | |
986 | return (2); | |
b128c09f | 987 | } |
34dc7c2f BB |
988 | } |
989 | ||
b128c09f BB |
990 | if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && |
991 | prop != ZPOOL_PROP_CACHEFILE) { | |
34dc7c2f BB |
992 | (void) fprintf(stderr, gettext("property '%s' " |
993 | "specified multiple times\n"), propname); | |
994 | return (2); | |
995 | } | |
996 | ||
b128c09f | 997 | if (nvlist_add_string(proplist, normnm, propval) != 0) { |
34dc7c2f BB |
998 | (void) fprintf(stderr, gettext("internal " |
999 | "error: out of memory\n")); | |
1000 | return (1); | |
1001 | } | |
1002 | ||
1003 | return (0); | |
1004 | } | |
1005 | ||
2f3ec900 RY |
1006 | /* |
1007 | * Set a default property pair (name, string-value) in a property nvlist | |
1008 | */ | |
1009 | static int | |
a926aab9 AZ |
1010 | add_prop_list_default(const char *propname, const char *propval, |
1011 | nvlist_t **props) | |
2f3ec900 | 1012 | { |
d1807f16 | 1013 | const char *pval; |
2f3ec900 RY |
1014 | |
1015 | if (nvlist_lookup_string(*props, propname, &pval) == 0) | |
1016 | return (0); | |
1017 | ||
1018 | return (add_prop_list(propname, propval, props, B_TRUE)); | |
1019 | } | |
1020 | ||
34dc7c2f | 1021 | /* |
b1e46f86 | 1022 | * zpool add [-afgLnP] [-o property=value] <pool> <vdev> ... |
34dc7c2f | 1023 | * |
b1e46f86 | 1024 | * -a Disable the ashift validation checks |
34dc7c2f | 1025 | * -f Force addition of devices, even if they appear in use |
d2f3e292 RY |
1026 | * -g Display guid for individual vdev name. |
1027 | * -L Follow links when resolving vdev path name. | |
34dc7c2f BB |
1028 | * -n Do not add the devices, but display the resulting layout if |
1029 | * they were to be added. | |
df831108 | 1030 | * -o Set property=value. |
a77f29f9 | 1031 | * -P Display full path for vdev name. |
34dc7c2f BB |
1032 | * |
1033 | * Adds the given vdevs to 'pool'. As with create, the bulk of this work is | |
227d3793 | 1034 | * handled by make_root_vdev(), which constructs the nvlist needed to pass to |
34dc7c2f BB |
1035 | * libzfs. |
1036 | */ | |
1037 | int | |
1038 | zpool_do_add(int argc, char **argv) | |
1039 | { | |
b1e46f86 GW |
1040 | boolean_t check_replication = B_TRUE; |
1041 | boolean_t check_inuse = B_TRUE; | |
34dc7c2f | 1042 | boolean_t dryrun = B_FALSE; |
b1e46f86 GW |
1043 | boolean_t check_ashift = B_TRUE; |
1044 | boolean_t force = B_FALSE; | |
d2f3e292 | 1045 | int name_flags = 0; |
34dc7c2f BB |
1046 | int c; |
1047 | nvlist_t *nvroot; | |
1048 | char *poolname; | |
1049 | int ret; | |
1050 | zpool_handle_t *zhp; | |
1051 | nvlist_t *config; | |
df831108 CP |
1052 | nvlist_t *props = NULL; |
1053 | char *propval; | |
34dc7c2f | 1054 | |
b1e46f86 GW |
1055 | struct option long_options[] = { |
1056 | {"allow-in-use", no_argument, NULL, ZPOOL_OPTION_ALLOW_INUSE}, | |
1057 | {"allow-replication-mismatch", no_argument, NULL, | |
1058 | ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH}, | |
1059 | {"allow-ashift-mismatch", no_argument, NULL, | |
1060 | ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH}, | |
1061 | {0, 0, 0, 0} | |
1062 | }; | |
1063 | ||
34dc7c2f | 1064 | /* check options */ |
b1e46f86 GW |
1065 | while ((c = getopt_long(argc, argv, "fgLno:P", long_options, NULL)) |
1066 | != -1) { | |
34dc7c2f BB |
1067 | switch (c) { |
1068 | case 'f': | |
1069 | force = B_TRUE; | |
1070 | break; | |
d2f3e292 RY |
1071 | case 'g': |
1072 | name_flags |= VDEV_NAME_GUID; | |
1073 | break; | |
1074 | case 'L': | |
1075 | name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
1076 | break; | |
34dc7c2f BB |
1077 | case 'n': |
1078 | dryrun = B_TRUE; | |
1079 | break; | |
df831108 CP |
1080 | case 'o': |
1081 | if ((propval = strchr(optarg, '=')) == NULL) { | |
1082 | (void) fprintf(stderr, gettext("missing " | |
1083 | "'=' for -o option\n")); | |
1084 | usage(B_FALSE); | |
1085 | } | |
1086 | *propval = '\0'; | |
1087 | propval++; | |
1088 | ||
1089 | if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || | |
1090 | (add_prop_list(optarg, propval, &props, B_TRUE))) | |
1091 | usage(B_FALSE); | |
1092 | break; | |
a77f29f9 | 1093 | case 'P': |
d2f3e292 RY |
1094 | name_flags |= VDEV_NAME_PATH; |
1095 | break; | |
b1e46f86 GW |
1096 | case ZPOOL_OPTION_ALLOW_INUSE: |
1097 | check_inuse = B_FALSE; | |
1098 | break; | |
1099 | case ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH: | |
1100 | check_replication = B_FALSE; | |
1101 | break; | |
1102 | case ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH: | |
1103 | check_ashift = B_FALSE; | |
1104 | break; | |
34dc7c2f BB |
1105 | case '?': |
1106 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1107 | optopt); | |
1108 | usage(B_FALSE); | |
1109 | } | |
1110 | } | |
1111 | ||
1112 | argc -= optind; | |
1113 | argv += optind; | |
1114 | ||
1115 | /* get pool name and check number of arguments */ | |
1116 | if (argc < 1) { | |
1117 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
1118 | usage(B_FALSE); | |
1119 | } | |
1120 | if (argc < 2) { | |
1121 | (void) fprintf(stderr, gettext("missing vdev specification\n")); | |
1122 | usage(B_FALSE); | |
1123 | } | |
1124 | ||
b1e46f86 GW |
1125 | if (force) { |
1126 | if (!check_inuse || !check_replication || !check_ashift) { | |
1127 | (void) fprintf(stderr, gettext("'-f' option is not " | |
1128 | "allowed with '--allow-replication-mismatch', " | |
1129 | "'--allow-ashift-mismatch', or " | |
1130 | "'--allow-in-use'\n")); | |
1131 | usage(B_FALSE); | |
1132 | } | |
1133 | check_inuse = B_FALSE; | |
1134 | check_replication = B_FALSE; | |
1135 | check_ashift = B_FALSE; | |
1136 | } | |
1137 | ||
34dc7c2f BB |
1138 | poolname = argv[0]; |
1139 | ||
1140 | argc--; | |
1141 | argv++; | |
1142 | ||
1143 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
1144 | return (1); | |
1145 | ||
1146 | if ((config = zpool_get_config(zhp, NULL)) == NULL) { | |
1147 | (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), | |
1148 | poolname); | |
1149 | zpool_close(zhp); | |
1150 | return (1); | |
1151 | } | |
1152 | ||
dddef7d6 | 1153 | /* unless manually specified use "ashift" pool property (if set) */ |
1154 | if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { | |
1155 | int intval; | |
1156 | zprop_source_t src; | |
1157 | char strval[ZPOOL_MAXPROPLEN]; | |
1158 | ||
1159 | intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); | |
1160 | if (src != ZPROP_SRC_DEFAULT) { | |
1161 | (void) sprintf(strval, "%" PRId32, intval); | |
1162 | verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, | |
1163 | &props, B_TRUE) == 0); | |
1164 | } | |
1165 | } | |
1166 | ||
227d3793 | 1167 | /* pass off to make_root_vdev for processing */ |
b1e46f86 GW |
1168 | nvroot = make_root_vdev(zhp, props, !check_inuse, |
1169 | check_replication, B_FALSE, dryrun, argc, argv); | |
34dc7c2f BB |
1170 | if (nvroot == NULL) { |
1171 | zpool_close(zhp); | |
1172 | return (1); | |
1173 | } | |
1174 | ||
1175 | if (dryrun) { | |
1176 | nvlist_t *poolnvroot; | |
0cb40fa3 AF |
1177 | nvlist_t **l2child, **sparechild; |
1178 | uint_t l2children, sparechildren, c; | |
e02b533e | 1179 | char *vname; |
0cb40fa3 | 1180 | boolean_t hadcache = B_FALSE, hadspare = B_FALSE; |
34dc7c2f BB |
1181 | |
1182 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
1183 | &poolnvroot) == 0); | |
1184 | ||
1185 | (void) printf(gettext("would update '%s' to the following " | |
0cb40fa3 | 1186 | "configuration:\n\n"), zpool_get_name(zhp)); |
34dc7c2f BB |
1187 | |
1188 | /* print original main pool and new tree */ | |
cc99f275 DB |
1189 | print_vdev_tree(zhp, poolname, poolnvroot, 0, "", |
1190 | name_flags | VDEV_NAME_TYPE_ID); | |
1191 | print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags); | |
1192 | ||
1193 | /* print other classes: 'dedup', 'special', and 'log' */ | |
c24fa4b1 | 1194 | if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_DEDUP)) { |
1195 | print_vdev_tree(zhp, "dedup", poolnvroot, 0, | |
1196 | VDEV_ALLOC_BIAS_DEDUP, name_flags); | |
1197 | print_vdev_tree(zhp, NULL, nvroot, 0, | |
1198 | VDEV_ALLOC_BIAS_DEDUP, name_flags); | |
1199 | } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_DEDUP)) { | |
1200 | print_vdev_tree(zhp, "dedup", nvroot, 0, | |
1201 | VDEV_ALLOC_BIAS_DEDUP, name_flags); | |
1202 | } | |
cc99f275 | 1203 | |
c24fa4b1 | 1204 | if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_SPECIAL)) { |
1205 | print_vdev_tree(zhp, "special", poolnvroot, 0, | |
1206 | VDEV_ALLOC_BIAS_SPECIAL, name_flags); | |
1207 | print_vdev_tree(zhp, NULL, nvroot, 0, | |
1208 | VDEV_ALLOC_BIAS_SPECIAL, name_flags); | |
1209 | } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_SPECIAL)) { | |
1210 | print_vdev_tree(zhp, "special", nvroot, 0, | |
1211 | VDEV_ALLOC_BIAS_SPECIAL, name_flags); | |
1212 | } | |
cc99f275 | 1213 | |
c24fa4b1 | 1214 | if (num_logs(poolnvroot) > 0) { |
1215 | print_vdev_tree(zhp, "logs", poolnvroot, 0, | |
1216 | VDEV_ALLOC_BIAS_LOG, name_flags); | |
1217 | print_vdev_tree(zhp, NULL, nvroot, 0, | |
1218 | VDEV_ALLOC_BIAS_LOG, name_flags); | |
1219 | } else if (num_logs(nvroot) > 0) { | |
1220 | print_vdev_tree(zhp, "logs", nvroot, 0, | |
1221 | VDEV_ALLOC_BIAS_LOG, name_flags); | |
1222 | } | |
34dc7c2f | 1223 | |
e02b533e TC |
1224 | /* Do the same for the caches */ |
1225 | if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE, | |
1226 | &l2child, &l2children) == 0 && l2children) { | |
1227 | hadcache = B_TRUE; | |
1228 | (void) printf(gettext("\tcache\n")); | |
1229 | for (c = 0; c < l2children; c++) { | |
1230 | vname = zpool_vdev_name(g_zfs, NULL, | |
d2f3e292 | 1231 | l2child[c], name_flags); |
e02b533e TC |
1232 | (void) printf("\t %s\n", vname); |
1233 | free(vname); | |
1234 | } | |
1235 | } | |
1236 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
1237 | &l2child, &l2children) == 0 && l2children) { | |
1238 | if (!hadcache) | |
1239 | (void) printf(gettext("\tcache\n")); | |
1240 | for (c = 0; c < l2children; c++) { | |
1241 | vname = zpool_vdev_name(g_zfs, NULL, | |
d2f3e292 | 1242 | l2child[c], name_flags); |
e02b533e TC |
1243 | (void) printf("\t %s\n", vname); |
1244 | free(vname); | |
1245 | } | |
1246 | } | |
bf169e9f | 1247 | /* And finally the spares */ |
0cb40fa3 AF |
1248 | if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES, |
1249 | &sparechild, &sparechildren) == 0 && sparechildren > 0) { | |
1250 | hadspare = B_TRUE; | |
1251 | (void) printf(gettext("\tspares\n")); | |
1252 | for (c = 0; c < sparechildren; c++) { | |
1253 | vname = zpool_vdev_name(g_zfs, NULL, | |
1254 | sparechild[c], name_flags); | |
1255 | (void) printf("\t %s\n", vname); | |
1256 | free(vname); | |
1257 | } | |
1258 | } | |
1259 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
1260 | &sparechild, &sparechildren) == 0 && sparechildren > 0) { | |
1261 | if (!hadspare) | |
1262 | (void) printf(gettext("\tspares\n")); | |
1263 | for (c = 0; c < sparechildren; c++) { | |
1264 | vname = zpool_vdev_name(g_zfs, NULL, | |
1265 | sparechild[c], name_flags); | |
1266 | (void) printf("\t %s\n", vname); | |
1267 | free(vname); | |
1268 | } | |
1269 | } | |
e02b533e | 1270 | |
34dc7c2f BB |
1271 | ret = 0; |
1272 | } else { | |
b1e46f86 | 1273 | ret = (zpool_add(zhp, nvroot, check_ashift) != 0); |
34dc7c2f BB |
1274 | } |
1275 | ||
df831108 | 1276 | nvlist_free(props); |
34dc7c2f BB |
1277 | nvlist_free(nvroot); |
1278 | zpool_close(zhp); | |
1279 | ||
1280 | return (ret); | |
1281 | } | |
1282 | ||
1283 | /* | |
e60e158e | 1284 | * zpool remove [-npsw] <pool> <vdev> ... |
34dc7c2f | 1285 | * |
a1d477c2 | 1286 | * Removes the given vdev from the pool. |
34dc7c2f BB |
1287 | */ |
1288 | int | |
1289 | zpool_do_remove(int argc, char **argv) | |
1290 | { | |
1291 | char *poolname; | |
1292 | int i, ret = 0; | |
884385a0 | 1293 | zpool_handle_t *zhp = NULL; |
a1d477c2 | 1294 | boolean_t stop = B_FALSE; |
0869b74a | 1295 | int c; |
a1d477c2 MA |
1296 | boolean_t noop = B_FALSE; |
1297 | boolean_t parsable = B_FALSE; | |
e60e158e | 1298 | boolean_t wait = B_FALSE; |
34dc7c2f | 1299 | |
a1d477c2 | 1300 | /* check options */ |
e60e158e | 1301 | while ((c = getopt(argc, argv, "npsw")) != -1) { |
a1d477c2 MA |
1302 | switch (c) { |
1303 | case 'n': | |
1304 | noop = B_TRUE; | |
1305 | break; | |
1306 | case 'p': | |
1307 | parsable = B_TRUE; | |
1308 | break; | |
1309 | case 's': | |
1310 | stop = B_TRUE; | |
1311 | break; | |
e60e158e JG |
1312 | case 'w': |
1313 | wait = B_TRUE; | |
1314 | break; | |
a1d477c2 MA |
1315 | case '?': |
1316 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1317 | optopt); | |
1318 | usage(B_FALSE); | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | argc -= optind; | |
1323 | argv += optind; | |
34dc7c2f BB |
1324 | |
1325 | /* get pool name and check number of arguments */ | |
1326 | if (argc < 1) { | |
1327 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
1328 | usage(B_FALSE); | |
1329 | } | |
34dc7c2f BB |
1330 | |
1331 | poolname = argv[0]; | |
1332 | ||
1333 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
1334 | return (1); | |
1335 | ||
a1d477c2 | 1336 | if (stop && noop) { |
ebe1d036 | 1337 | zpool_close(zhp); |
a1d477c2 MA |
1338 | (void) fprintf(stderr, gettext("stop request ignored\n")); |
1339 | return (0); | |
1340 | } | |
1341 | ||
1342 | if (stop) { | |
1343 | if (argc > 1) { | |
1344 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
1345 | usage(B_FALSE); | |
1346 | } | |
1347 | if (zpool_vdev_remove_cancel(zhp) != 0) | |
34dc7c2f | 1348 | ret = 1; |
e60e158e JG |
1349 | if (wait) { |
1350 | (void) fprintf(stderr, gettext("invalid option " | |
1351 | "combination: -w cannot be used with -s\n")); | |
1352 | usage(B_FALSE); | |
1353 | } | |
a1d477c2 MA |
1354 | } else { |
1355 | if (argc < 2) { | |
1356 | (void) fprintf(stderr, gettext("missing device\n")); | |
1357 | usage(B_FALSE); | |
1358 | } | |
1359 | ||
1360 | for (i = 1; i < argc; i++) { | |
1361 | if (noop) { | |
1362 | uint64_t size; | |
1363 | ||
1364 | if (zpool_vdev_indirect_size(zhp, argv[i], | |
1365 | &size) != 0) { | |
1366 | ret = 1; | |
1367 | break; | |
1368 | } | |
1369 | if (parsable) { | |
1370 | (void) printf("%s %llu\n", | |
1371 | argv[i], (unsigned long long)size); | |
1372 | } else { | |
1373 | char valstr[32]; | |
1374 | zfs_nicenum(size, valstr, | |
1375 | sizeof (valstr)); | |
1376 | (void) printf("Memory that will be " | |
1377 | "used after removing %s: %s\n", | |
1378 | argv[i], valstr); | |
1379 | } | |
1380 | } else { | |
1381 | if (zpool_vdev_remove(zhp, argv[i]) != 0) | |
1382 | ret = 1; | |
1383 | } | |
1384 | } | |
e60e158e JG |
1385 | |
1386 | if (ret == 0 && wait) | |
1387 | ret = zpool_wait(zhp, ZPOOL_WAIT_REMOVE); | |
34dc7c2f | 1388 | } |
884385a0 | 1389 | zpool_close(zhp); |
34dc7c2f BB |
1390 | |
1391 | return (ret); | |
1392 | } | |
1393 | ||
4dc11532 TH |
1394 | /* |
1395 | * Return 1 if a vdev is active (being used in a pool) | |
1396 | * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool) | |
1397 | * | |
1398 | * This is useful for checking if a disk in an active pool is offlined or | |
1399 | * faulted. | |
1400 | */ | |
1401 | static int | |
1402 | vdev_is_active(char *vdev_path) | |
1403 | { | |
1404 | int fd; | |
1405 | fd = open(vdev_path, O_EXCL); | |
1406 | if (fd < 0) { | |
1407 | return (1); /* cant open O_EXCL - disk is active */ | |
1408 | } | |
1409 | ||
1410 | close(fd); | |
1411 | return (0); /* disk is inactive in the pool */ | |
1412 | } | |
1413 | ||
131cc95c | 1414 | /* |
dbb38f66 YP |
1415 | * zpool labelclear [-f] <vdev> |
1416 | * | |
1417 | * -f Force clearing the label for the vdevs which are members of | |
1418 | * the exported or foreign pools. | |
131cc95c DK |
1419 | * |
1420 | * Verifies that the vdev is not active and zeros out the label information | |
1421 | * on the device. | |
1422 | */ | |
1423 | int | |
1424 | zpool_do_labelclear(int argc, char **argv) | |
1425 | { | |
dbb38f66 YP |
1426 | char vdev[MAXPATHLEN]; |
1427 | char *name = NULL; | |
131cc95c | 1428 | int c, fd = -1, ret = 0; |
dbb38f66 | 1429 | nvlist_t *config; |
131cc95c DK |
1430 | pool_state_t state; |
1431 | boolean_t inuse = B_FALSE; | |
1432 | boolean_t force = B_FALSE; | |
1433 | ||
1434 | /* check options */ | |
1435 | while ((c = getopt(argc, argv, "f")) != -1) { | |
1436 | switch (c) { | |
1437 | case 'f': | |
1438 | force = B_TRUE; | |
1439 | break; | |
1440 | default: | |
1441 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1442 | optopt); | |
1443 | usage(B_FALSE); | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | argc -= optind; | |
1448 | argv += optind; | |
1449 | ||
1450 | /* get vdev name */ | |
1451 | if (argc < 1) { | |
dbb38f66 YP |
1452 | (void) fprintf(stderr, gettext("missing vdev name\n")); |
1453 | usage(B_FALSE); | |
1454 | } | |
1455 | if (argc > 1) { | |
1456 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
131cc95c DK |
1457 | usage(B_FALSE); |
1458 | } | |
1459 | ||
9368b387 RY |
1460 | (void) strlcpy(vdev, argv[0], sizeof (vdev)); |
1461 | ||
dbb38f66 | 1462 | /* |
9368b387 | 1463 | * If we cannot open an absolute path, we quit. |
dbb38f66 YP |
1464 | * Otherwise if the provided vdev name doesn't point to a file, |
1465 | * try prepending expected disk paths and partition numbers. | |
1466 | */ | |
9368b387 | 1467 | if ((fd = open(vdev, O_RDWR)) < 0) { |
dbb38f66 | 1468 | int error; |
9368b387 RY |
1469 | if (vdev[0] == '/') { |
1470 | (void) fprintf(stderr, gettext("failed to open " | |
1471 | "%s: %s\n"), vdev, strerror(errno)); | |
1472 | return (1); | |
1473 | } | |
dbb38f66 YP |
1474 | |
1475 | error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN); | |
1476 | if (error == 0 && zfs_dev_is_whole_disk(vdev)) { | |
1477 | if (zfs_append_partition(vdev, MAXPATHLEN) == -1) | |
1478 | error = ENOENT; | |
1479 | } | |
1480 | ||
9368b387 RY |
1481 | if (error || ((fd = open(vdev, O_RDWR)) < 0)) { |
1482 | if (errno == ENOENT) { | |
1483 | (void) fprintf(stderr, gettext( | |
1484 | "failed to find device %s, try " | |
1485 | "specifying absolute path instead\n"), | |
1486 | argv[0]); | |
1487 | return (1); | |
1488 | } | |
1489 | ||
1490 | (void) fprintf(stderr, gettext("failed to open %s:" | |
1491 | " %s\n"), vdev, strerror(errno)); | |
dbb38f66 YP |
1492 | return (1); |
1493 | } | |
131cc95c DK |
1494 | } |
1495 | ||
066da71e BB |
1496 | /* |
1497 | * Flush all dirty pages for the block device. This should not be | |
1498 | * fatal when the device does not support BLKFLSBUF as would be the | |
1499 | * case for a file vdev. | |
1500 | */ | |
d46f0deb | 1501 | if ((zfs_dev_flush(fd) != 0) && (errno != ENOTTY)) |
a167aa7c GDN |
1502 | (void) fprintf(stderr, gettext("failed to invalidate " |
1503 | "cache for %s: %s\n"), vdev, strerror(errno)); | |
1504 | ||
066da71e | 1505 | if (zpool_read_label(fd, &config, NULL) != 0) { |
131cc95c | 1506 | (void) fprintf(stderr, |
066da71e | 1507 | gettext("failed to read label from %s\n"), vdev); |
c721ba43 TH |
1508 | ret = 1; |
1509 | goto errout; | |
dbb38f66 YP |
1510 | } |
1511 | nvlist_free(config); | |
131cc95c | 1512 | |
dbb38f66 YP |
1513 | ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse); |
1514 | if (ret != 0) { | |
1515 | (void) fprintf(stderr, | |
1516 | gettext("failed to check state for %s\n"), vdev); | |
c721ba43 TH |
1517 | ret = 1; |
1518 | goto errout; | |
131cc95c DK |
1519 | } |
1520 | ||
dbb38f66 YP |
1521 | if (!inuse) |
1522 | goto wipe_label; | |
1523 | ||
1524 | switch (state) { | |
1525 | default: | |
1526 | case POOL_STATE_ACTIVE: | |
1527 | case POOL_STATE_SPARE: | |
1528 | case POOL_STATE_L2CACHE: | |
4dc11532 TH |
1529 | /* |
1530 | * We allow the user to call 'zpool offline -f' | |
1531 | * on an offlined disk in an active pool. We can check if | |
1532 | * the disk is online by calling vdev_is_active(). | |
1533 | */ | |
1534 | if (force && !vdev_is_active(vdev)) | |
1535 | break; | |
1536 | ||
dbb38f66 | 1537 | (void) fprintf(stderr, gettext( |
4dc11532 | 1538 | "%s is a member (%s) of pool \"%s\""), |
dbb38f66 | 1539 | vdev, zpool_pool_state_to_name(state), name); |
4dc11532 TH |
1540 | |
1541 | if (force) { | |
1542 | (void) fprintf(stderr, gettext( | |
1543 | ". Offline the disk first to clear its label.")); | |
1544 | } | |
1545 | printf("\n"); | |
dbb38f66 YP |
1546 | ret = 1; |
1547 | goto errout; | |
131cc95c | 1548 | |
dbb38f66 YP |
1549 | case POOL_STATE_EXPORTED: |
1550 | if (force) | |
131cc95c | 1551 | break; |
dbb38f66 YP |
1552 | (void) fprintf(stderr, gettext( |
1553 | "use '-f' to override the following error:\n" | |
1554 | "%s is a member of exported pool \"%s\"\n"), | |
1555 | vdev, name); | |
1556 | ret = 1; | |
1557 | goto errout; | |
1558 | ||
1559 | case POOL_STATE_POTENTIALLY_ACTIVE: | |
1560 | if (force) | |
1561 | break; | |
1562 | (void) fprintf(stderr, gettext( | |
1563 | "use '-f' to override the following error:\n" | |
1564 | "%s is a member of potentially active pool \"%s\"\n"), | |
1565 | vdev, name); | |
1566 | ret = 1; | |
1567 | goto errout; | |
1568 | ||
1569 | case POOL_STATE_DESTROYED: | |
1570 | /* inuse should never be set for a destroyed pool */ | |
1571 | assert(0); | |
1572 | break; | |
131cc95c DK |
1573 | } |
1574 | ||
1575 | wipe_label: | |
dbb38f66 YP |
1576 | ret = zpool_clear_label(fd); |
1577 | if (ret != 0) { | |
131cc95c | 1578 | (void) fprintf(stderr, |
dbb38f66 | 1579 | gettext("failed to clear label for %s\n"), vdev); |
131cc95c DK |
1580 | } |
1581 | ||
1582 | errout: | |
dbb38f66 YP |
1583 | free(name); |
1584 | (void) close(fd); | |
131cc95c DK |
1585 | |
1586 | return (ret); | |
1587 | } | |
1588 | ||
34dc7c2f | 1589 | /* |
9ae529ec | 1590 | * zpool create [-fnd] [-o property=value] ... |
b128c09f BB |
1591 | * [-O file-system-property=value] ... |
1592 | * [-R root] [-m mountpoint] <pool> <dev> ... | |
34dc7c2f BB |
1593 | * |
1594 | * -f Force creation, even if devices appear in use | |
1595 | * -n Do not create the pool, but display the resulting layout if it | |
1596 | * were to be created. | |
1597 | * -R Create a pool under an alternate root | |
1598 | * -m Set default mountpoint for the root dataset. By default it's | |
9ae529ec | 1599 | * '/<pool>' |
34dc7c2f | 1600 | * -o Set property=value. |
e4010f27 | 1601 | * -o Set feature@feature=enabled|disabled. |
9ae529ec CS |
1602 | * -d Don't automatically enable all supported pool features |
1603 | * (individual features can be enabled with -o). | |
b128c09f | 1604 | * -O Set fsproperty=value in the pool's root file system |
34dc7c2f BB |
1605 | * |
1606 | * Creates the named pool according to the given vdev specification. The | |
227d3793 RM |
1607 | * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c. |
1608 | * Once we get the nvlist back from make_root_vdev(), we either print out the | |
1609 | * contents (if '-n' was specified), or pass it to libzfs to do the creation. | |
34dc7c2f BB |
1610 | */ |
1611 | int | |
1612 | zpool_do_create(int argc, char **argv) | |
1613 | { | |
1614 | boolean_t force = B_FALSE; | |
1615 | boolean_t dryrun = B_FALSE; | |
658fb802 CB |
1616 | boolean_t enable_pool_features = B_TRUE; |
1617 | ||
34dc7c2f BB |
1618 | int c; |
1619 | nvlist_t *nvroot = NULL; | |
1620 | char *poolname; | |
023bbe6f | 1621 | char *tname = NULL; |
34dc7c2f BB |
1622 | int ret = 1; |
1623 | char *altroot = NULL; | |
658fb802 | 1624 | char *compat = NULL; |
34dc7c2f | 1625 | char *mountpoint = NULL; |
b128c09f | 1626 | nvlist_t *fsprops = NULL; |
34dc7c2f BB |
1627 | nvlist_t *props = NULL; |
1628 | char *propval; | |
1629 | ||
1630 | /* check options */ | |
83e9986f | 1631 | while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) { |
34dc7c2f BB |
1632 | switch (c) { |
1633 | case 'f': | |
1634 | force = B_TRUE; | |
1635 | break; | |
1636 | case 'n': | |
1637 | dryrun = B_TRUE; | |
1638 | break; | |
9ae529ec | 1639 | case 'd': |
658fb802 | 1640 | enable_pool_features = B_FALSE; |
9ae529ec | 1641 | break; |
34dc7c2f BB |
1642 | case 'R': |
1643 | altroot = optarg; | |
1644 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 1645 | ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) |
34dc7c2f | 1646 | goto errout; |
2f3ec900 | 1647 | if (add_prop_list_default(zpool_prop_to_name( |
e40ca391 | 1648 | ZPOOL_PROP_CACHEFILE), "none", &props)) |
34dc7c2f BB |
1649 | goto errout; |
1650 | break; | |
1651 | case 'm': | |
7bc7f250 | 1652 | /* Equivalent to -O mountpoint=optarg */ |
34dc7c2f BB |
1653 | mountpoint = optarg; |
1654 | break; | |
1655 | case 'o': | |
1656 | if ((propval = strchr(optarg, '=')) == NULL) { | |
1657 | (void) fprintf(stderr, gettext("missing " | |
1658 | "'=' for -o option\n")); | |
1659 | goto errout; | |
1660 | } | |
1661 | *propval = '\0'; | |
1662 | propval++; | |
1663 | ||
b128c09f BB |
1664 | if (add_prop_list(optarg, propval, &props, B_TRUE)) |
1665 | goto errout; | |
9ae529ec CS |
1666 | |
1667 | /* | |
1668 | * If the user is creating a pool that doesn't support | |
1669 | * feature flags, don't enable any features. | |
1670 | */ | |
1671 | if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { | |
1672 | char *end; | |
1673 | u_longlong_t ver; | |
1674 | ||
1675 | ver = strtoull(propval, &end, 10); | |
1676 | if (*end == '\0' && | |
1677 | ver < SPA_VERSION_FEATURES) { | |
658fb802 | 1678 | enable_pool_features = B_FALSE; |
9ae529ec CS |
1679 | } |
1680 | } | |
3ac2794c BB |
1681 | if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT) |
1682 | altroot = propval; | |
658fb802 CB |
1683 | if (zpool_name_to_prop(optarg) == |
1684 | ZPOOL_PROP_COMPATIBILITY) | |
1685 | compat = propval; | |
b128c09f BB |
1686 | break; |
1687 | case 'O': | |
1688 | if ((propval = strchr(optarg, '=')) == NULL) { | |
1689 | (void) fprintf(stderr, gettext("missing " | |
1690 | "'=' for -O option\n")); | |
1691 | goto errout; | |
1692 | } | |
1693 | *propval = '\0'; | |
1694 | propval++; | |
1695 | ||
7bc7f250 WA |
1696 | /* |
1697 | * Mountpoints are checked and then added later. | |
1698 | * Uniquely among properties, they can be specified | |
1699 | * more than once, to avoid conflict with -m. | |
1700 | */ | |
1701 | if (0 == strcmp(optarg, | |
1702 | zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) { | |
1703 | mountpoint = propval; | |
1704 | } else if (add_prop_list(optarg, propval, &fsprops, | |
1705 | B_FALSE)) { | |
34dc7c2f | 1706 | goto errout; |
7bc7f250 | 1707 | } |
34dc7c2f | 1708 | break; |
83e9986f RY |
1709 | case 't': |
1710 | /* | |
1711 | * Sanity check temporary pool name. | |
1712 | */ | |
1713 | if (strchr(optarg, '/') != NULL) { | |
1714 | (void) fprintf(stderr, gettext("cannot create " | |
1715 | "'%s': invalid character '/' in temporary " | |
1716 | "name\n"), optarg); | |
1717 | (void) fprintf(stderr, gettext("use 'zfs " | |
1718 | "create' to create a dataset\n")); | |
1719 | goto errout; | |
1720 | } | |
1721 | ||
1722 | if (add_prop_list(zpool_prop_to_name( | |
1723 | ZPOOL_PROP_TNAME), optarg, &props, B_TRUE)) | |
1724 | goto errout; | |
1725 | if (add_prop_list_default(zpool_prop_to_name( | |
e40ca391 | 1726 | ZPOOL_PROP_CACHEFILE), "none", &props)) |
83e9986f | 1727 | goto errout; |
023bbe6f | 1728 | tname = optarg; |
83e9986f | 1729 | break; |
34dc7c2f BB |
1730 | case ':': |
1731 | (void) fprintf(stderr, gettext("missing argument for " | |
1732 | "'%c' option\n"), optopt); | |
1733 | goto badusage; | |
1734 | case '?': | |
1735 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1736 | optopt); | |
1737 | goto badusage; | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | argc -= optind; | |
1742 | argv += optind; | |
1743 | ||
1744 | /* get pool name and check number of arguments */ | |
1745 | if (argc < 1) { | |
1746 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
1747 | goto badusage; | |
1748 | } | |
1749 | if (argc < 2) { | |
1750 | (void) fprintf(stderr, gettext("missing vdev specification\n")); | |
1751 | goto badusage; | |
1752 | } | |
1753 | ||
1754 | poolname = argv[0]; | |
1755 | ||
1756 | /* | |
1757 | * As a special case, check for use of '/' in the name, and direct the | |
1758 | * user to use 'zfs create' instead. | |
1759 | */ | |
1760 | if (strchr(poolname, '/') != NULL) { | |
1761 | (void) fprintf(stderr, gettext("cannot create '%s': invalid " | |
1762 | "character '/' in pool name\n"), poolname); | |
1763 | (void) fprintf(stderr, gettext("use 'zfs create' to " | |
1764 | "create a dataset\n")); | |
1765 | goto errout; | |
1766 | } | |
1767 | ||
227d3793 | 1768 | /* pass off to make_root_vdev for bulk processing */ |
df30f566 | 1769 | nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun, |
b128c09f | 1770 | argc - 1, argv + 1); |
34dc7c2f | 1771 | if (nvroot == NULL) |
b128c09f | 1772 | goto errout; |
34dc7c2f BB |
1773 | |
1774 | /* make_root_vdev() allows 0 toplevel children if there are spares */ | |
1775 | if (!zfs_allocatable_devs(nvroot)) { | |
1776 | (void) fprintf(stderr, gettext("invalid vdev " | |
1777 | "specification: at least one toplevel vdev must be " | |
1778 | "specified\n")); | |
1779 | goto errout; | |
1780 | } | |
1781 | ||
34dc7c2f BB |
1782 | if (altroot != NULL && altroot[0] != '/') { |
1783 | (void) fprintf(stderr, gettext("invalid alternate root '%s': " | |
1784 | "must be an absolute path\n"), altroot); | |
1785 | goto errout; | |
1786 | } | |
1787 | ||
1788 | /* | |
1789 | * Check the validity of the mountpoint and direct the user to use the | |
1790 | * '-m' mountpoint option if it looks like its in use. | |
1791 | */ | |
1792 | if (mountpoint == NULL || | |
1793 | (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && | |
1794 | strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) { | |
1795 | char buf[MAXPATHLEN]; | |
1796 | DIR *dirp; | |
1797 | ||
1798 | if (mountpoint && mountpoint[0] != '/') { | |
1799 | (void) fprintf(stderr, gettext("invalid mountpoint " | |
1800 | "'%s': must be an absolute path, 'legacy', or " | |
1801 | "'none'\n"), mountpoint); | |
1802 | goto errout; | |
1803 | } | |
1804 | ||
1805 | if (mountpoint == NULL) { | |
1806 | if (altroot != NULL) | |
1807 | (void) snprintf(buf, sizeof (buf), "%s/%s", | |
1808 | altroot, poolname); | |
1809 | else | |
1810 | (void) snprintf(buf, sizeof (buf), "/%s", | |
1811 | poolname); | |
1812 | } else { | |
1813 | if (altroot != NULL) | |
1814 | (void) snprintf(buf, sizeof (buf), "%s%s", | |
1815 | altroot, mountpoint); | |
1816 | else | |
1817 | (void) snprintf(buf, sizeof (buf), "%s", | |
1818 | mountpoint); | |
1819 | } | |
1820 | ||
1821 | if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { | |
1822 | (void) fprintf(stderr, gettext("mountpoint '%s' : " | |
1823 | "%s\n"), buf, strerror(errno)); | |
1824 | (void) fprintf(stderr, gettext("use '-m' " | |
1825 | "option to provide a different default\n")); | |
1826 | goto errout; | |
1827 | } else if (dirp) { | |
1828 | int count = 0; | |
1829 | ||
1830 | while (count < 3 && readdir(dirp) != NULL) | |
1831 | count++; | |
1832 | (void) closedir(dirp); | |
1833 | ||
1834 | if (count > 2) { | |
1835 | (void) fprintf(stderr, gettext("mountpoint " | |
1836 | "'%s' exists and is not empty\n"), buf); | |
1837 | (void) fprintf(stderr, gettext("use '-m' " | |
1838 | "option to provide a " | |
1839 | "different default\n")); | |
1840 | goto errout; | |
1841 | } | |
1842 | } | |
1843 | } | |
1844 | ||
7bc7f250 WA |
1845 | /* |
1846 | * Now that the mountpoint's validity has been checked, ensure that | |
1847 | * the property is set appropriately prior to creating the pool. | |
1848 | */ | |
1849 | if (mountpoint != NULL) { | |
1850 | ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), | |
1851 | mountpoint, &fsprops, B_FALSE); | |
1852 | if (ret != 0) | |
1853 | goto errout; | |
1854 | } | |
1855 | ||
1856 | ret = 1; | |
34dc7c2f BB |
1857 | if (dryrun) { |
1858 | /* | |
1859 | * For a dry run invocation, print out a basic message and run | |
1860 | * through all the vdevs in the list and print out in an | |
1861 | * appropriate hierarchy. | |
1862 | */ | |
1863 | (void) printf(gettext("would create '%s' with the " | |
1864 | "following layout:\n\n"), poolname); | |
1865 | ||
cc99f275 DB |
1866 | print_vdev_tree(NULL, poolname, nvroot, 0, "", 0); |
1867 | print_vdev_tree(NULL, "dedup", nvroot, 0, | |
1868 | VDEV_ALLOC_BIAS_DEDUP, 0); | |
1869 | print_vdev_tree(NULL, "special", nvroot, 0, | |
1870 | VDEV_ALLOC_BIAS_SPECIAL, 0); | |
1871 | print_vdev_tree(NULL, "logs", nvroot, 0, | |
1872 | VDEV_ALLOC_BIAS_LOG, 0); | |
0cb40fa3 AF |
1873 | print_cache_list(nvroot, 0); |
1874 | print_spare_list(nvroot, 0); | |
34dc7c2f BB |
1875 | |
1876 | ret = 0; | |
1877 | } else { | |
1878 | /* | |
658fb802 CB |
1879 | * Load in feature set. |
1880 | * Note: if compatibility property not given, we'll have | |
1881 | * NULL, which means 'all features'. | |
34dc7c2f | 1882 | */ |
658fb802 CB |
1883 | boolean_t requested_features[SPA_FEATURES]; |
1884 | if (zpool_do_load_compat(compat, requested_features) != | |
1885 | ZPOOL_COMPATIBILITY_OK) | |
1886 | goto errout; | |
1887 | ||
1888 | /* | |
1889 | * props contains list of features to enable. | |
1890 | * For each feature: | |
1891 | * - remove it if feature@name=disabled | |
1892 | * - leave it there if feature@name=enabled | |
1893 | * - add it if: | |
1894 | * - enable_pool_features (ie: no '-d' or '-o version') | |
1895 | * - it's supported by the kernel module | |
1896 | * - it's in the requested feature set | |
e086db16 | 1897 | * - warn if it's enabled but not in compat |
658fb802 CB |
1898 | */ |
1899 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { | |
e4010f27 | 1900 | char propname[MAXPATHLEN]; |
d1807f16 | 1901 | const char *propval; |
e4010f27 | 1902 | zfeature_info_t *feat = &spa_feature_table[i]; |
9ae529ec | 1903 | |
e4010f27 | 1904 | (void) snprintf(propname, sizeof (propname), |
1905 | "feature@%s", feat->fi_uname); | |
1906 | ||
e4010f27 | 1907 | if (!nvlist_lookup_string(props, propname, &propval)) { |
63652e15 DS |
1908 | if (strcmp(propval, |
1909 | ZFS_FEATURE_DISABLED) == 0) { | |
e4010f27 | 1910 | (void) nvlist_remove_all(props, |
1911 | propname); | |
63652e15 | 1912 | } else if (strcmp(propval, |
e086db16 | 1913 | ZFS_FEATURE_ENABLED) == 0 && |
63652e15 | 1914 | !requested_features[i]) { |
e086db16 CB |
1915 | (void) fprintf(stderr, gettext( |
1916 | "Warning: feature \"%s\" enabled " | |
1917 | "but is not in specified " | |
1918 | "'compatibility' feature set.\n"), | |
1919 | feat->fi_uname); | |
63652e15 | 1920 | } |
658fb802 CB |
1921 | } else if ( |
1922 | enable_pool_features && | |
1923 | feat->fi_zfs_mod_supported && | |
1924 | requested_features[i]) { | |
7bc7f250 WA |
1925 | ret = add_prop_list(propname, |
1926 | ZFS_FEATURE_ENABLED, &props, B_TRUE); | |
1927 | if (ret != 0) | |
9ae529ec CS |
1928 | goto errout; |
1929 | } | |
1930 | } | |
7bc7f250 WA |
1931 | |
1932 | ret = 1; | |
b128c09f BB |
1933 | if (zpool_create(g_zfs, poolname, |
1934 | nvroot, props, fsprops) == 0) { | |
023bbe6f | 1935 | zfs_handle_t *pool = zfs_open(g_zfs, |
1936 | tname ? tname : poolname, ZFS_TYPE_FILESYSTEM); | |
34dc7c2f | 1937 | if (pool != NULL) { |
c15d36c6 | 1938 | if (zfs_mount(pool, NULL, 0) == 0) { |
b4d9a82f AZ |
1939 | ret = zfs_share(pool, NULL); |
1940 | zfs_commit_shares(NULL); | |
c15d36c6 | 1941 | } |
34dc7c2f BB |
1942 | zfs_close(pool); |
1943 | } | |
1944 | } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { | |
1945 | (void) fprintf(stderr, gettext("pool name may have " | |
1946 | "been omitted\n")); | |
1947 | } | |
1948 | } | |
1949 | ||
1950 | errout: | |
1951 | nvlist_free(nvroot); | |
b128c09f | 1952 | nvlist_free(fsprops); |
34dc7c2f BB |
1953 | nvlist_free(props); |
1954 | return (ret); | |
1955 | badusage: | |
b128c09f | 1956 | nvlist_free(fsprops); |
34dc7c2f BB |
1957 | nvlist_free(props); |
1958 | usage(B_FALSE); | |
1959 | return (2); | |
1960 | } | |
1961 | ||
1962 | /* | |
1963 | * zpool destroy <pool> | |
1964 | * | |
1965 | * -f Forcefully unmount any datasets | |
1966 | * | |
1967 | * Destroy the given pool. Automatically unmounts any datasets in the pool. | |
1968 | */ | |
1969 | int | |
1970 | zpool_do_destroy(int argc, char **argv) | |
1971 | { | |
1972 | boolean_t force = B_FALSE; | |
1973 | int c; | |
1974 | char *pool; | |
1975 | zpool_handle_t *zhp; | |
1976 | int ret; | |
1977 | ||
1978 | /* check options */ | |
1979 | while ((c = getopt(argc, argv, "f")) != -1) { | |
1980 | switch (c) { | |
1981 | case 'f': | |
1982 | force = B_TRUE; | |
1983 | break; | |
1984 | case '?': | |
1985 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1986 | optopt); | |
1987 | usage(B_FALSE); | |
1988 | } | |
1989 | } | |
1990 | ||
1991 | argc -= optind; | |
1992 | argv += optind; | |
1993 | ||
1994 | /* check arguments */ | |
1995 | if (argc < 1) { | |
1996 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
1997 | usage(B_FALSE); | |
1998 | } | |
1999 | if (argc > 1) { | |
2000 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
2001 | usage(B_FALSE); | |
2002 | } | |
2003 | ||
2004 | pool = argv[0]; | |
2005 | ||
2006 | if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { | |
2007 | /* | |
2008 | * As a special case, check for use of '/' in the name, and | |
2009 | * direct the user to use 'zfs destroy' instead. | |
2010 | */ | |
2011 | if (strchr(pool, '/') != NULL) | |
2012 | (void) fprintf(stderr, gettext("use 'zfs destroy' to " | |
2013 | "destroy a dataset\n")); | |
2014 | return (1); | |
2015 | } | |
2016 | ||
2017 | if (zpool_disable_datasets(zhp, force) != 0) { | |
2018 | (void) fprintf(stderr, gettext("could not destroy '%s': " | |
2019 | "could not unmount datasets\n"), zpool_get_name(zhp)); | |
a425f5bf | 2020 | zpool_close(zhp); |
34dc7c2f BB |
2021 | return (1); |
2022 | } | |
2023 | ||
6f1ffb06 MA |
2024 | /* The history must be logged as part of the export */ |
2025 | log_history = B_FALSE; | |
2026 | ||
2027 | ret = (zpool_destroy(zhp, history_str) != 0); | |
34dc7c2f BB |
2028 | |
2029 | zpool_close(zhp); | |
2030 | ||
2031 | return (ret); | |
2032 | } | |
2033 | ||
859735c0 | 2034 | typedef struct export_cbdata { |
975a1325 DB |
2035 | tpool_t *tpool; |
2036 | pthread_mutex_t mnttab_lock; | |
859735c0 TF |
2037 | boolean_t force; |
2038 | boolean_t hardforce; | |
975a1325 | 2039 | int retval; |
859735c0 TF |
2040 | } export_cbdata_t; |
2041 | ||
975a1325 DB |
2042 | |
2043 | typedef struct { | |
2044 | char *aea_poolname; | |
2045 | export_cbdata_t *aea_cbdata; | |
2046 | } async_export_args_t; | |
2047 | ||
859735c0 TF |
2048 | /* |
2049 | * Export one pool | |
2050 | */ | |
65c7cc49 | 2051 | static int |
859735c0 TF |
2052 | zpool_export_one(zpool_handle_t *zhp, void *data) |
2053 | { | |
2054 | export_cbdata_t *cb = data; | |
2055 | ||
975a1325 DB |
2056 | /* |
2057 | * zpool_disable_datasets() is not thread-safe for mnttab access. | |
2058 | * So we serialize access here for 'zpool export -a' parallel case. | |
2059 | */ | |
2060 | if (cb->tpool != NULL) | |
2061 | pthread_mutex_lock(&cb->mnttab_lock); | |
859735c0 | 2062 | |
975a1325 DB |
2063 | int retval = zpool_disable_datasets(zhp, cb->force); |
2064 | ||
2065 | if (cb->tpool != NULL) | |
2066 | pthread_mutex_unlock(&cb->mnttab_lock); | |
2067 | ||
2068 | if (retval) | |
2069 | return (1); | |
859735c0 TF |
2070 | |
2071 | if (cb->hardforce) { | |
2072 | if (zpool_export_force(zhp, history_str) != 0) | |
2073 | return (1); | |
2074 | } else if (zpool_export(zhp, cb->force, history_str) != 0) { | |
2075 | return (1); | |
2076 | } | |
2077 | ||
2078 | return (0); | |
2079 | } | |
2080 | ||
975a1325 DB |
2081 | /* |
2082 | * Asynchronous export request | |
2083 | */ | |
2084 | static void | |
2085 | zpool_export_task(void *arg) | |
2086 | { | |
2087 | async_export_args_t *aea = arg; | |
2088 | ||
2089 | zpool_handle_t *zhp = zpool_open(g_zfs, aea->aea_poolname); | |
2090 | if (zhp != NULL) { | |
2091 | int ret = zpool_export_one(zhp, aea->aea_cbdata); | |
2092 | if (ret != 0) | |
2093 | aea->aea_cbdata->retval = ret; | |
2094 | zpool_close(zhp); | |
2095 | } else { | |
2096 | aea->aea_cbdata->retval = 1; | |
2097 | } | |
2098 | ||
2099 | free(aea->aea_poolname); | |
2100 | free(aea); | |
2101 | } | |
2102 | ||
2103 | /* | |
2104 | * Process an export request in parallel | |
2105 | */ | |
2106 | static int | |
2107 | zpool_export_one_async(zpool_handle_t *zhp, void *data) | |
2108 | { | |
2109 | tpool_t *tpool = ((export_cbdata_t *)data)->tpool; | |
2110 | async_export_args_t *aea = safe_malloc(sizeof (async_export_args_t)); | |
2111 | ||
2112 | /* save pool name since zhp will go out of scope */ | |
2113 | aea->aea_poolname = strdup(zpool_get_name(zhp)); | |
2114 | aea->aea_cbdata = data; | |
2115 | ||
2116 | /* ship off actual export to another thread */ | |
2117 | if (tpool_dispatch(tpool, zpool_export_task, (void *)aea) != 0) | |
2118 | return (errno); /* unlikely */ | |
2119 | else | |
2120 | return (0); | |
2121 | } | |
2122 | ||
34dc7c2f BB |
2123 | /* |
2124 | * zpool export [-f] <pool> ... | |
2125 | * | |
859735c0 | 2126 | * -a Export all pools |
34dc7c2f BB |
2127 | * -f Forcefully unmount datasets |
2128 | * | |
2129 | * Export the given pools. By default, the command will attempt to cleanly | |
2130 | * unmount any active datasets within the pool. If the '-f' flag is specified, | |
2131 | * then the datasets will be forcefully unmounted. | |
2132 | */ | |
2133 | int | |
2134 | zpool_do_export(int argc, char **argv) | |
2135 | { | |
859735c0 TF |
2136 | export_cbdata_t cb; |
2137 | boolean_t do_all = B_FALSE; | |
34dc7c2f | 2138 | boolean_t force = B_FALSE; |
fb5f0bc8 | 2139 | boolean_t hardforce = B_FALSE; |
859735c0 | 2140 | int c, ret; |
34dc7c2f BB |
2141 | |
2142 | /* check options */ | |
859735c0 | 2143 | while ((c = getopt(argc, argv, "afF")) != -1) { |
34dc7c2f | 2144 | switch (c) { |
859735c0 TF |
2145 | case 'a': |
2146 | do_all = B_TRUE; | |
2147 | break; | |
34dc7c2f BB |
2148 | case 'f': |
2149 | force = B_TRUE; | |
2150 | break; | |
fb5f0bc8 BB |
2151 | case 'F': |
2152 | hardforce = B_TRUE; | |
2153 | break; | |
34dc7c2f BB |
2154 | case '?': |
2155 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
2156 | optopt); | |
2157 | usage(B_FALSE); | |
2158 | } | |
2159 | } | |
2160 | ||
859735c0 TF |
2161 | cb.force = force; |
2162 | cb.hardforce = hardforce; | |
975a1325 DB |
2163 | cb.tpool = NULL; |
2164 | cb.retval = 0; | |
34dc7c2f BB |
2165 | argc -= optind; |
2166 | argv += optind; | |
2167 | ||
975a1325 DB |
2168 | /* The history will be logged as part of the export itself */ |
2169 | log_history = B_FALSE; | |
2170 | ||
859735c0 TF |
2171 | if (do_all) { |
2172 | if (argc != 0) { | |
2173 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
2174 | usage(B_FALSE); | |
2175 | } | |
2176 | ||
975a1325 DB |
2177 | cb.tpool = tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN), |
2178 | 0, NULL); | |
2179 | pthread_mutex_init(&cb.mnttab_lock, NULL); | |
2180 | ||
2181 | /* Asynchronously call zpool_export_one using thread pool */ | |
2182 | ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, | |
2183 | B_FALSE, zpool_export_one_async, &cb); | |
2184 | ||
2185 | tpool_wait(cb.tpool); | |
2186 | tpool_destroy(cb.tpool); | |
2187 | (void) pthread_mutex_destroy(&cb.mnttab_lock); | |
2188 | ||
2189 | return (ret | cb.retval); | |
859735c0 TF |
2190 | } |
2191 | ||
34dc7c2f BB |
2192 | /* check arguments */ |
2193 | if (argc < 1) { | |
2194 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
2195 | usage(B_FALSE); | |
2196 | } | |
2197 | ||
2a673e76 AJ |
2198 | ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
2199 | B_FALSE, zpool_export_one, &cb); | |
34dc7c2f BB |
2200 | |
2201 | return (ret); | |
2202 | } | |
2203 | ||
2204 | /* | |
2205 | * Given a vdev configuration, determine the maximum width needed for the device | |
2206 | * name column. | |
2207 | */ | |
2208 | static int | |
d2f3e292 RY |
2209 | max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max, |
2210 | int name_flags) | |
34dc7c2f | 2211 | { |
e9072c76 AZ |
2212 | static const char *const subtypes[] = |
2213 | {ZPOOL_CONFIG_SPARES, ZPOOL_CONFIG_L2CACHE, ZPOOL_CONFIG_CHILDREN}; | |
34dc7c2f | 2214 | |
e9072c76 AZ |
2215 | char *name = zpool_vdev_name(g_zfs, zhp, nv, name_flags); |
2216 | max = MAX(strlen(name) + depth, max); | |
34dc7c2f BB |
2217 | free(name); |
2218 | ||
e9072c76 AZ |
2219 | nvlist_t **child; |
2220 | uint_t children; | |
2221 | for (size_t i = 0; i < ARRAY_SIZE(subtypes); ++i) | |
2222 | if (nvlist_lookup_nvlist_array(nv, subtypes[i], | |
2223 | &child, &children) == 0) | |
2224 | for (uint_t c = 0; c < children; ++c) | |
2225 | max = MAX(max_width(zhp, child[c], depth + 2, | |
2226 | max, name_flags), max); | |
34dc7c2f | 2227 | |
34dc7c2f BB |
2228 | return (max); |
2229 | } | |
2230 | ||
9babb374 BB |
2231 | typedef struct spare_cbdata { |
2232 | uint64_t cb_guid; | |
2233 | zpool_handle_t *cb_zhp; | |
2234 | } spare_cbdata_t; | |
2235 | ||
2236 | static boolean_t | |
2237 | find_vdev(nvlist_t *nv, uint64_t search) | |
2238 | { | |
2239 | uint64_t guid; | |
2240 | nvlist_t **child; | |
2241 | uint_t c, children; | |
2242 | ||
2243 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && | |
2244 | search == guid) | |
2245 | return (B_TRUE); | |
2246 | ||
2247 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
2248 | &child, &children) == 0) { | |
2249 | for (c = 0; c < children; c++) | |
2250 | if (find_vdev(child[c], search)) | |
2251 | return (B_TRUE); | |
2252 | } | |
2253 | ||
2254 | return (B_FALSE); | |
2255 | } | |
2256 | ||
2257 | static int | |
2258 | find_spare(zpool_handle_t *zhp, void *data) | |
2259 | { | |
2260 | spare_cbdata_t *cbp = data; | |
2261 | nvlist_t *config, *nvroot; | |
2262 | ||
2263 | config = zpool_get_config(zhp, NULL); | |
2264 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
2265 | &nvroot) == 0); | |
2266 | ||
2267 | if (find_vdev(nvroot, cbp->cb_guid)) { | |
2268 | cbp->cb_zhp = zhp; | |
2269 | return (1); | |
2270 | } | |
2271 | ||
2272 | zpool_close(zhp); | |
2273 | return (0); | |
2274 | } | |
2275 | ||
fea33e4e HJ |
2276 | typedef struct status_cbdata { |
2277 | int cb_count; | |
2278 | int cb_name_flags; | |
2279 | int cb_namewidth; | |
2280 | boolean_t cb_allpools; | |
2281 | boolean_t cb_verbose; | |
ad796b8a | 2282 | boolean_t cb_literal; |
fea33e4e HJ |
2283 | boolean_t cb_explain; |
2284 | boolean_t cb_first; | |
2285 | boolean_t cb_dedup_stats; | |
08233887 | 2286 | boolean_t cb_print_unhealthy; |
fea33e4e | 2287 | boolean_t cb_print_status; |
ad796b8a | 2288 | boolean_t cb_print_slow_ios; |
a769fb53 | 2289 | boolean_t cb_print_vdev_init; |
1b939560 | 2290 | boolean_t cb_print_vdev_trim; |
8720e9e7 | 2291 | vdev_cmd_data_list_t *vcdl; |
a9520e6e | 2292 | boolean_t cb_print_power; |
fea33e4e HJ |
2293 | } status_cbdata_t; |
2294 | ||
d6418de0 | 2295 | /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */ |
a926aab9 AZ |
2296 | static boolean_t |
2297 | is_blank_str(const char *str) | |
d6418de0 | 2298 | { |
a926aab9 | 2299 | for (; str != NULL && *str != '\0'; ++str) |
d6418de0 | 2300 | if (!isblank(*str)) |
a926aab9 AZ |
2301 | return (B_FALSE); |
2302 | return (B_TRUE); | |
d6418de0 TH |
2303 | } |
2304 | ||
2305 | /* Print command output lines for specific vdev in a specific pool */ | |
8720e9e7 | 2306 | static void |
d1807f16 | 2307 | zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, const char *path) |
8720e9e7 | 2308 | { |
d6418de0 TH |
2309 | vdev_cmd_data_t *data; |
2310 | int i, j; | |
a926aab9 | 2311 | const char *val; |
d6418de0 | 2312 | |
8720e9e7 | 2313 | for (i = 0; i < vcdl->count; i++) { |
d6418de0 TH |
2314 | if ((strcmp(vcdl->data[i].path, path) != 0) || |
2315 | (strcmp(vcdl->data[i].pool, pool) != 0)) { | |
2316 | /* Not the vdev we're looking for */ | |
2317 | continue; | |
8720e9e7 | 2318 | } |
d6418de0 TH |
2319 | |
2320 | data = &vcdl->data[i]; | |
2321 | /* Print out all the output values for this vdev */ | |
2322 | for (j = 0; j < vcdl->uniq_cols_cnt; j++) { | |
2323 | val = NULL; | |
2324 | /* Does this vdev have values for this column? */ | |
2325 | for (int k = 0; k < data->cols_cnt; k++) { | |
2326 | if (strcmp(data->cols[k], | |
2327 | vcdl->uniq_cols[j]) == 0) { | |
2328 | /* yes it does, record the value */ | |
2329 | val = data->lines[k]; | |
2330 | break; | |
2331 | } | |
2332 | } | |
2333 | /* | |
2334 | * Mark empty values with dashes to make output | |
2335 | * awk-able. | |
2336 | */ | |
7454d2bb | 2337 | if (val == NULL || is_blank_str(val)) |
d6418de0 TH |
2338 | val = "-"; |
2339 | ||
2340 | printf("%*s", vcdl->uniq_cols_width[j], val); | |
2341 | if (j < vcdl->uniq_cols_cnt - 1) | |
a926aab9 | 2342 | fputs(" ", stdout); |
d6418de0 TH |
2343 | } |
2344 | ||
2345 | /* Print out any values that aren't in a column at the end */ | |
2346 | for (j = data->cols_cnt; j < data->lines_cnt; j++) { | |
2347 | /* Did we have any columns? If so print a spacer. */ | |
2348 | if (vcdl->uniq_cols_cnt > 0) | |
a926aab9 | 2349 | fputs(" ", stdout); |
d6418de0 TH |
2350 | |
2351 | val = data->lines[j]; | |
a926aab9 | 2352 | fputs(val ?: "", stdout); |
d6418de0 TH |
2353 | } |
2354 | break; | |
8720e9e7 TH |
2355 | } |
2356 | } | |
2357 | ||
1b939560 BB |
2358 | /* |
2359 | * Print vdev initialization status for leaves | |
2360 | */ | |
2361 | static void | |
2362 | print_status_initialize(vdev_stat_t *vs, boolean_t verbose) | |
2363 | { | |
2364 | if (verbose) { | |
2365 | if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE || | |
2366 | vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED || | |
2367 | vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) && | |
2368 | !vs->vs_scan_removing) { | |
2369 | char zbuf[1024]; | |
2370 | char tbuf[256]; | |
1b939560 BB |
2371 | |
2372 | time_t t = vs->vs_initialize_action_time; | |
2373 | int initialize_pct = 100; | |
2374 | if (vs->vs_initialize_state != | |
2375 | VDEV_INITIALIZE_COMPLETE) { | |
2376 | initialize_pct = (vs->vs_initialize_bytes_done * | |
2377 | 100 / (vs->vs_initialize_bytes_est + 1)); | |
2378 | } | |
2379 | ||
f07389d3 MF |
2380 | (void) ctime_r(&t, tbuf); |
2381 | tbuf[24] = 0; | |
1b939560 BB |
2382 | |
2383 | switch (vs->vs_initialize_state) { | |
2384 | case VDEV_INITIALIZE_SUSPENDED: | |
2385 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2386 | gettext("suspended, started at"), tbuf); | |
2387 | break; | |
2388 | case VDEV_INITIALIZE_ACTIVE: | |
2389 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2390 | gettext("started at"), tbuf); | |
2391 | break; | |
2392 | case VDEV_INITIALIZE_COMPLETE: | |
2393 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2394 | gettext("completed at"), tbuf); | |
2395 | break; | |
2396 | } | |
2397 | ||
2398 | (void) printf(gettext(" (%d%% initialized%s)"), | |
2399 | initialize_pct, zbuf); | |
2400 | } else { | |
2401 | (void) printf(gettext(" (uninitialized)")); | |
2402 | } | |
2403 | } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) { | |
2404 | (void) printf(gettext(" (initializing)")); | |
2405 | } | |
2406 | } | |
2407 | ||
2408 | /* | |
2409 | * Print vdev TRIM status for leaves | |
2410 | */ | |
2411 | static void | |
2412 | print_status_trim(vdev_stat_t *vs, boolean_t verbose) | |
2413 | { | |
2414 | if (verbose) { | |
2415 | if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE || | |
2416 | vs->vs_trim_state == VDEV_TRIM_SUSPENDED || | |
2417 | vs->vs_trim_state == VDEV_TRIM_COMPLETE) && | |
2418 | !vs->vs_scan_removing) { | |
2419 | char zbuf[1024]; | |
2420 | char tbuf[256]; | |
1b939560 BB |
2421 | |
2422 | time_t t = vs->vs_trim_action_time; | |
2423 | int trim_pct = 100; | |
2424 | if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) { | |
2425 | trim_pct = (vs->vs_trim_bytes_done * | |
2426 | 100 / (vs->vs_trim_bytes_est + 1)); | |
2427 | } | |
2428 | ||
f07389d3 MF |
2429 | (void) ctime_r(&t, tbuf); |
2430 | tbuf[24] = 0; | |
1b939560 BB |
2431 | |
2432 | switch (vs->vs_trim_state) { | |
2433 | case VDEV_TRIM_SUSPENDED: | |
2434 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2435 | gettext("suspended, started at"), tbuf); | |
2436 | break; | |
2437 | case VDEV_TRIM_ACTIVE: | |
2438 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2439 | gettext("started at"), tbuf); | |
2440 | break; | |
2441 | case VDEV_TRIM_COMPLETE: | |
2442 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2443 | gettext("completed at"), tbuf); | |
2444 | break; | |
2445 | } | |
2446 | ||
2447 | (void) printf(gettext(" (%d%% trimmed%s)"), | |
2448 | trim_pct, zbuf); | |
2449 | } else if (vs->vs_trim_notsup) { | |
2450 | (void) printf(gettext(" (trim unsupported)")); | |
2451 | } else { | |
2452 | (void) printf(gettext(" (untrimmed)")); | |
2453 | } | |
2454 | } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) { | |
2455 | (void) printf(gettext(" (trimming)")); | |
2456 | } | |
2457 | } | |
2458 | ||
9fb2771a TH |
2459 | /* |
2460 | * Return the color associated with a health string. This includes returning | |
2461 | * NULL for no color change. | |
2462 | */ | |
a926aab9 | 2463 | static const char * |
9fb2771a TH |
2464 | health_str_to_color(const char *health) |
2465 | { | |
2466 | if (strcmp(health, gettext("FAULTED")) == 0 || | |
2467 | strcmp(health, gettext("SUSPENDED")) == 0 || | |
2468 | strcmp(health, gettext("UNAVAIL")) == 0) { | |
2469 | return (ANSI_RED); | |
2470 | } | |
2471 | ||
2472 | if (strcmp(health, gettext("OFFLINE")) == 0 || | |
2473 | strcmp(health, gettext("DEGRADED")) == 0 || | |
2474 | strcmp(health, gettext("REMOVED")) == 0) { | |
2475 | return (ANSI_YELLOW); | |
2476 | } | |
2477 | ||
2478 | return (NULL); | |
2479 | } | |
2480 | ||
08233887 CH |
2481 | /* |
2482 | * Called for each leaf vdev. Returns 0 if the vdev is healthy. | |
2483 | * A vdev is unhealthy if any of the following are true: | |
2484 | * 1) there are read, write, or checksum errors, | |
2485 | * 2) its state is not ONLINE, or | |
2486 | * 3) slow IO reporting was requested (-s) and there are slow IOs. | |
2487 | */ | |
2488 | static int | |
2489 | vdev_health_check_cb(void *hdl_data, nvlist_t *nv, void *data) | |
2490 | { | |
2491 | status_cbdata_t *cb = data; | |
2492 | vdev_stat_t *vs; | |
2493 | uint_t vsc; | |
2494 | (void) hdl_data; | |
2495 | ||
2496 | if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, | |
2497 | (uint64_t **)&vs, &vsc) != 0) | |
2498 | return (1); | |
2499 | ||
2500 | if (vs->vs_checksum_errors || vs->vs_read_errors || | |
2501 | vs->vs_write_errors || vs->vs_state != VDEV_STATE_HEALTHY) | |
2502 | return (1); | |
2503 | ||
2504 | if (cb->cb_print_slow_ios && vs->vs_slow_ios) | |
2505 | return (1); | |
2506 | ||
2507 | return (0); | |
2508 | } | |
2509 | ||
9babb374 BB |
2510 | /* |
2511 | * Print out configuration state as requested by status_callback. | |
2512 | */ | |
d2f3e292 | 2513 | static void |
fea33e4e | 2514 | print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name, |
9a49d3f3 | 2515 | nvlist_t *nv, int depth, boolean_t isspare, vdev_rebuild_stat_t *vrs) |
9babb374 | 2516 | { |
2ef0f8c3 | 2517 | nvlist_t **child, *root; |
6fe3498c | 2518 | uint_t c, i, vsc, children; |
428870ff | 2519 | pool_scan_stat_t *ps = NULL; |
9babb374 | 2520 | vdev_stat_t *vs; |
428870ff | 2521 | char rbuf[6], wbuf[6], cbuf[6]; |
9babb374 BB |
2522 | char *vname; |
2523 | uint64_t notpresent; | |
fea33e4e | 2524 | spare_cbdata_t spare_cb; |
6b8655ad | 2525 | const char *state; |
d1807f16 RY |
2526 | const char *type; |
2527 | const char *path = NULL; | |
08233887 CH |
2528 | const char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL, |
2529 | *scolor = NULL; | |
9babb374 | 2530 | |
9babb374 BB |
2531 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
2532 | &child, &children) != 0) | |
2533 | children = 0; | |
2534 | ||
428870ff | 2535 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
6fe3498c | 2536 | (uint64_t **)&vs, &vsc) == 0); |
428870ff | 2537 | |
a1d477c2 MA |
2538 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); |
2539 | ||
2540 | if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
2541 | return; | |
2542 | ||
9babb374 | 2543 | state = zpool_state_to_name(vs->vs_state, vs->vs_aux); |
9fb2771a | 2544 | |
9babb374 BB |
2545 | if (isspare) { |
2546 | /* | |
2547 | * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for | |
2548 | * online drives. | |
2549 | */ | |
2550 | if (vs->vs_aux == VDEV_AUX_SPARED) | |
9fb2771a | 2551 | state = gettext("INUSE"); |
9babb374 | 2552 | else if (vs->vs_state == VDEV_STATE_HEALTHY) |
9fb2771a | 2553 | state = gettext("AVAIL"); |
9babb374 BB |
2554 | } |
2555 | ||
08233887 CH |
2556 | /* |
2557 | * If '-e' is specified then top-level vdevs and their children | |
2558 | * can be pruned if all of their leaves are healthy. | |
2559 | */ | |
2560 | if (cb->cb_print_unhealthy && depth > 0 && | |
2561 | for_each_vdev_in_nvlist(nv, vdev_health_check_cb, cb) == 0) { | |
2562 | return; | |
2563 | } | |
2564 | ||
9fb2771a TH |
2565 | printf_color(health_str_to_color(state), |
2566 | "\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth, | |
9babb374 BB |
2567 | name, state); |
2568 | ||
2569 | if (!isspare) { | |
9fb2771a TH |
2570 | if (vs->vs_read_errors) |
2571 | rcolor = ANSI_RED; | |
2572 | ||
2573 | if (vs->vs_write_errors) | |
2574 | wcolor = ANSI_RED; | |
2575 | ||
2576 | if (vs->vs_checksum_errors) | |
2577 | ccolor = ANSI_RED; | |
2578 | ||
08233887 CH |
2579 | if (vs->vs_slow_ios) |
2580 | scolor = ANSI_BLUE; | |
2581 | ||
ad796b8a | 2582 | if (cb->cb_literal) { |
a926aab9 | 2583 | fputc(' ', stdout); |
9fb2771a TH |
2584 | printf_color(rcolor, "%5llu", |
2585 | (u_longlong_t)vs->vs_read_errors); | |
a926aab9 | 2586 | fputc(' ', stdout); |
9fb2771a TH |
2587 | printf_color(wcolor, "%5llu", |
2588 | (u_longlong_t)vs->vs_write_errors); | |
a926aab9 | 2589 | fputc(' ', stdout); |
9fb2771a | 2590 | printf_color(ccolor, "%5llu", |
ad796b8a TH |
2591 | (u_longlong_t)vs->vs_checksum_errors); |
2592 | } else { | |
2593 | zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); | |
2594 | zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); | |
2595 | zfs_nicenum(vs->vs_checksum_errors, cbuf, | |
2596 | sizeof (cbuf)); | |
a926aab9 | 2597 | fputc(' ', stdout); |
9fb2771a | 2598 | printf_color(rcolor, "%5s", rbuf); |
a926aab9 | 2599 | fputc(' ', stdout); |
9fb2771a | 2600 | printf_color(wcolor, "%5s", wbuf); |
a926aab9 | 2601 | fputc(' ', stdout); |
9fb2771a | 2602 | printf_color(ccolor, "%5s", cbuf); |
ad796b8a | 2603 | } |
ad796b8a TH |
2604 | if (cb->cb_print_slow_ios) { |
2605 | if (children == 0) { | |
2606 | /* Only leafs vdevs have slow IOs */ | |
2607 | zfs_nicenum(vs->vs_slow_ios, rbuf, | |
2608 | sizeof (rbuf)); | |
2609 | } else { | |
2610 | snprintf(rbuf, sizeof (rbuf), "-"); | |
2611 | } | |
2612 | ||
2613 | if (cb->cb_literal) | |
08233887 CH |
2614 | printf_color(scolor, " %5llu", |
2615 | (u_longlong_t)vs->vs_slow_ios); | |
ad796b8a | 2616 | else |
08233887 | 2617 | printf_color(scolor, " %5s", rbuf); |
ad796b8a | 2618 | } |
a9520e6e TH |
2619 | if (cb->cb_print_power) { |
2620 | if (children == 0) { | |
2621 | /* Only leaf vdevs have physical slots */ | |
2622 | switch (zpool_power_current_state(zhp, (char *) | |
2623 | fnvlist_lookup_string(nv, | |
2624 | ZPOOL_CONFIG_PATH))) { | |
2625 | case 0: | |
2626 | printf_color(ANSI_RED, " %5s", | |
2627 | gettext("off")); | |
2628 | break; | |
2629 | case 1: | |
2630 | printf(" %5s", gettext("on")); | |
2631 | break; | |
2632 | default: | |
2633 | printf(" %5s", "-"); | |
2634 | } | |
2635 | } else { | |
2636 | printf(" %5s", "-"); | |
2637 | } | |
2638 | } | |
9babb374 BB |
2639 | } |
2640 | ||
2641 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, | |
2642 | ¬present) == 0) { | |
9babb374 | 2643 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); |
9fb2771a | 2644 | (void) printf(" %s %s", gettext("was"), path); |
9babb374 BB |
2645 | } else if (vs->vs_aux != 0) { |
2646 | (void) printf(" "); | |
9fb2771a | 2647 | color_start(ANSI_RED); |
9babb374 BB |
2648 | switch (vs->vs_aux) { |
2649 | case VDEV_AUX_OPEN_FAILED: | |
2650 | (void) printf(gettext("cannot open")); | |
2651 | break; | |
2652 | ||
2653 | case VDEV_AUX_BAD_GUID_SUM: | |
2654 | (void) printf(gettext("missing device")); | |
2655 | break; | |
2656 | ||
2657 | case VDEV_AUX_NO_REPLICAS: | |
2658 | (void) printf(gettext("insufficient replicas")); | |
2659 | break; | |
2660 | ||
2661 | case VDEV_AUX_VERSION_NEWER: | |
2662 | (void) printf(gettext("newer version")); | |
2663 | break; | |
2664 | ||
9ae529ec CS |
2665 | case VDEV_AUX_UNSUP_FEAT: |
2666 | (void) printf(gettext("unsupported feature(s)")); | |
2667 | break; | |
2668 | ||
6fe3498c RM |
2669 | case VDEV_AUX_ASHIFT_TOO_BIG: |
2670 | (void) printf(gettext("unsupported minimum blocksize")); | |
2671 | break; | |
2672 | ||
9babb374 BB |
2673 | case VDEV_AUX_SPARED: |
2674 | verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, | |
fea33e4e HJ |
2675 | &spare_cb.cb_guid) == 0); |
2676 | if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) { | |
2677 | if (strcmp(zpool_get_name(spare_cb.cb_zhp), | |
9babb374 BB |
2678 | zpool_get_name(zhp)) == 0) |
2679 | (void) printf(gettext("currently in " | |
2680 | "use")); | |
2681 | else | |
2682 | (void) printf(gettext("in use by " | |
2683 | "pool '%s'"), | |
fea33e4e HJ |
2684 | zpool_get_name(spare_cb.cb_zhp)); |
2685 | zpool_close(spare_cb.cb_zhp); | |
9babb374 BB |
2686 | } else { |
2687 | (void) printf(gettext("currently in use")); | |
2688 | } | |
2689 | break; | |
2690 | ||
2691 | case VDEV_AUX_ERR_EXCEEDED: | |
cbe88229 DB |
2692 | if (vs->vs_read_errors + vs->vs_write_errors + |
2693 | vs->vs_checksum_errors == 0 && children == 0 && | |
2694 | vs->vs_slow_ios > 0) { | |
2695 | (void) printf(gettext("too many slow I/Os")); | |
2696 | } else { | |
2697 | (void) printf(gettext("too many errors")); | |
2698 | } | |
9babb374 BB |
2699 | break; |
2700 | ||
2701 | case VDEV_AUX_IO_FAILURE: | |
2702 | (void) printf(gettext("experienced I/O failures")); | |
2703 | break; | |
2704 | ||
2705 | case VDEV_AUX_BAD_LOG: | |
2706 | (void) printf(gettext("bad intent log")); | |
2707 | break; | |
2708 | ||
428870ff BB |
2709 | case VDEV_AUX_EXTERNAL: |
2710 | (void) printf(gettext("external device fault")); | |
2711 | break; | |
2712 | ||
2713 | case VDEV_AUX_SPLIT_POOL: | |
2714 | (void) printf(gettext("split into new pool")); | |
2715 | break; | |
2716 | ||
379ca9cf OF |
2717 | case VDEV_AUX_ACTIVE: |
2718 | (void) printf(gettext("currently in use")); | |
4265a929 | 2719 | break; |
379ca9cf | 2720 | |
6cb8e530 PZ |
2721 | case VDEV_AUX_CHILDREN_OFFLINE: |
2722 | (void) printf(gettext("all children offline")); | |
2723 | break; | |
2724 | ||
dce1bf99 VKV |
2725 | case VDEV_AUX_BAD_LABEL: |
2726 | (void) printf(gettext("invalid label")); | |
2727 | break; | |
2728 | ||
9babb374 BB |
2729 | default: |
2730 | (void) printf(gettext("corrupted data")); | |
2731 | break; | |
2732 | } | |
9fb2771a | 2733 | color_end(); |
3928ec53 CS |
2734 | } else if (children == 0 && !isspare && |
2735 | getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL && | |
2736 | VDEV_STAT_VALID(vs_physical_ashift, vsc) && | |
2737 | vs->vs_configured_ashift < vs->vs_physical_ashift) { | |
2738 | (void) printf( | |
2739 | gettext(" block size: %dB configured, %dB native"), | |
2740 | 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift); | |
428870ff BB |
2741 | } |
2742 | ||
2a673e76 AJ |
2743 | if (vs->vs_scan_removing != 0) { |
2744 | (void) printf(gettext(" (removing)")); | |
e77d59eb | 2745 | } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) { |
2a673e76 AJ |
2746 | (void) printf(gettext(" (non-allocating)")); |
2747 | } | |
2748 | ||
2ef0f8c3 TH |
2749 | /* The root vdev has the scrub/resilver stats */ |
2750 | root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL), | |
2751 | ZPOOL_CONFIG_VDEV_TREE); | |
2752 | (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS, | |
428870ff BB |
2753 | (uint64_t **)&ps, &c); |
2754 | ||
e9b12d41 TH |
2755 | /* |
2756 | * If you force fault a drive that's resilvering, its scan stats can | |
2757 | * get frozen in time, giving the false impression that it's | |
2758 | * being resilvered. That's why we check the state to see if the vdev | |
2759 | * is healthy before reporting "resilvering" or "repairing". | |
2760 | */ | |
2761 | if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0 && | |
2762 | vs->vs_state == VDEV_STATE_HEALTHY) { | |
80a91e74 TC |
2763 | if (vs->vs_scan_processed != 0) { |
2764 | (void) printf(gettext(" (%s)"), | |
2765 | (ps->pss_func == POOL_SCAN_RESILVER) ? | |
2766 | "resilvering" : "repairing"); | |
2767 | } else if (vs->vs_resilver_deferred) { | |
2768 | (void) printf(gettext(" (awaiting resilver)")); | |
2769 | } | |
9babb374 BB |
2770 | } |
2771 | ||
9a49d3f3 BB |
2772 | /* The top-level vdevs have the rebuild stats */ |
2773 | if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE && | |
e9b12d41 | 2774 | children == 0 && vs->vs_state == VDEV_STATE_HEALTHY) { |
9a49d3f3 BB |
2775 | if (vs->vs_rebuild_processed != 0) { |
2776 | (void) printf(gettext(" (resilvering)")); | |
2777 | } | |
2778 | } | |
2779 | ||
8720e9e7 TH |
2780 | if (cb->vcdl != NULL) { |
2781 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { | |
2782 | printf(" "); | |
2783 | zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); | |
2784 | } | |
2785 | } | |
2786 | ||
b2255edc | 2787 | /* Display vdev initialization and trim status for leaves. */ |
1b939560 BB |
2788 | if (children == 0) { |
2789 | print_status_initialize(vs, cb->cb_print_vdev_init); | |
2790 | print_status_trim(vs, cb->cb_print_vdev_trim); | |
619f0976 GW |
2791 | } |
2792 | ||
9babb374 BB |
2793 | (void) printf("\n"); |
2794 | ||
2795 | for (c = 0; c < children; c++) { | |
428870ff | 2796 | uint64_t islog = B_FALSE, ishole = B_FALSE; |
9babb374 | 2797 | |
428870ff | 2798 | /* Don't print logs or holes here */ |
9babb374 | 2799 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
428870ff BB |
2800 | &islog); |
2801 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
2802 | &ishole); | |
2803 | if (islog || ishole) | |
9babb374 | 2804 | continue; |
cc99f275 DB |
2805 | /* Only print normal classes here */ |
2806 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) | |
2807 | continue; | |
2808 | ||
9a49d3f3 BB |
2809 | /* Provide vdev_rebuild_stats to children if available */ |
2810 | if (vrs == NULL) { | |
2811 | (void) nvlist_lookup_uint64_array(nv, | |
2812 | ZPOOL_CONFIG_REBUILD_STATS, | |
2813 | (uint64_t **)&vrs, &i); | |
2814 | } | |
2815 | ||
d2f3e292 | 2816 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
fea33e4e HJ |
2817 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2818 | print_status_config(zhp, cb, vname, child[c], depth + 2, | |
9a49d3f3 | 2819 | isspare, vrs); |
9babb374 BB |
2820 | free(vname); |
2821 | } | |
2822 | } | |
2823 | ||
34dc7c2f BB |
2824 | /* |
2825 | * Print the configuration of an exported pool. Iterate over all vdevs in the | |
2826 | * pool, printing out the name and status for each one. | |
2827 | */ | |
d2f3e292 | 2828 | static void |
fea33e4e HJ |
2829 | print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv, |
2830 | int depth) | |
34dc7c2f BB |
2831 | { |
2832 | nvlist_t **child; | |
2833 | uint_t c, children; | |
2834 | vdev_stat_t *vs; | |
d1807f16 RY |
2835 | const char *type; |
2836 | char *vname; | |
34dc7c2f BB |
2837 | |
2838 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); | |
428870ff BB |
2839 | if (strcmp(type, VDEV_TYPE_MISSING) == 0 || |
2840 | strcmp(type, VDEV_TYPE_HOLE) == 0) | |
34dc7c2f BB |
2841 | return; |
2842 | ||
428870ff | 2843 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
2844 | (uint64_t **)&vs, &c) == 0); |
2845 | ||
fea33e4e | 2846 | (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name); |
34dc7c2f BB |
2847 | (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); |
2848 | ||
2849 | if (vs->vs_aux != 0) { | |
2850 | (void) printf(" "); | |
2851 | ||
2852 | switch (vs->vs_aux) { | |
2853 | case VDEV_AUX_OPEN_FAILED: | |
2854 | (void) printf(gettext("cannot open")); | |
2855 | break; | |
2856 | ||
2857 | case VDEV_AUX_BAD_GUID_SUM: | |
2858 | (void) printf(gettext("missing device")); | |
2859 | break; | |
2860 | ||
2861 | case VDEV_AUX_NO_REPLICAS: | |
2862 | (void) printf(gettext("insufficient replicas")); | |
2863 | break; | |
2864 | ||
2865 | case VDEV_AUX_VERSION_NEWER: | |
2866 | (void) printf(gettext("newer version")); | |
2867 | break; | |
2868 | ||
9ae529ec CS |
2869 | case VDEV_AUX_UNSUP_FEAT: |
2870 | (void) printf(gettext("unsupported feature(s)")); | |
2871 | break; | |
2872 | ||
34dc7c2f BB |
2873 | case VDEV_AUX_ERR_EXCEEDED: |
2874 | (void) printf(gettext("too many errors")); | |
2875 | break; | |
2876 | ||
379ca9cf OF |
2877 | case VDEV_AUX_ACTIVE: |
2878 | (void) printf(gettext("currently in use")); | |
2879 | break; | |
2880 | ||
6cb8e530 PZ |
2881 | case VDEV_AUX_CHILDREN_OFFLINE: |
2882 | (void) printf(gettext("all children offline")); | |
2883 | break; | |
2884 | ||
dce1bf99 VKV |
2885 | case VDEV_AUX_BAD_LABEL: |
2886 | (void) printf(gettext("invalid label")); | |
2887 | break; | |
2888 | ||
34dc7c2f BB |
2889 | default: |
2890 | (void) printf(gettext("corrupted data")); | |
2891 | break; | |
2892 | } | |
2893 | } | |
2894 | (void) printf("\n"); | |
2895 | ||
2896 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
2897 | &child, &children) != 0) | |
2898 | return; | |
2899 | ||
2900 | for (c = 0; c < children; c++) { | |
2901 | uint64_t is_log = B_FALSE; | |
2902 | ||
2903 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
2904 | &is_log); | |
9babb374 | 2905 | if (is_log) |
34dc7c2f | 2906 | continue; |
cc99f275 DB |
2907 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) |
2908 | continue; | |
34dc7c2f | 2909 | |
d2f3e292 | 2910 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e HJ |
2911 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2912 | print_import_config(cb, vname, child[c], depth + 2); | |
34dc7c2f BB |
2913 | free(vname); |
2914 | } | |
2915 | ||
2916 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, | |
2917 | &child, &children) == 0) { | |
2918 | (void) printf(gettext("\tcache\n")); | |
2919 | for (c = 0; c < children; c++) { | |
d2f3e292 | 2920 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e | 2921 | cb->cb_name_flags); |
34dc7c2f BB |
2922 | (void) printf("\t %s\n", vname); |
2923 | free(vname); | |
2924 | } | |
2925 | } | |
2926 | ||
2927 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, | |
2928 | &child, &children) == 0) { | |
2929 | (void) printf(gettext("\tspares\n")); | |
2930 | for (c = 0; c < children; c++) { | |
d2f3e292 | 2931 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e | 2932 | cb->cb_name_flags); |
34dc7c2f BB |
2933 | (void) printf("\t %s\n", vname); |
2934 | free(vname); | |
2935 | } | |
2936 | } | |
2937 | } | |
2938 | ||
9babb374 | 2939 | /* |
cc99f275 DB |
2940 | * Print specialized class vdevs. |
2941 | * | |
2942 | * These are recorded as top level vdevs in the main pool child array | |
2943 | * but with "is_log" set to 1 or an "alloc_bias" string. We use either | |
2944 | * print_status_config() or print_import_config() to print the top level | |
2945 | * class vdevs then any of their children (eg mirrored slogs) are printed | |
2946 | * recursively - which works because only the top level vdev is marked. | |
9babb374 BB |
2947 | */ |
2948 | static void | |
cc99f275 DB |
2949 | print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, |
2950 | const char *class) | |
9babb374 BB |
2951 | { |
2952 | uint_t c, children; | |
2953 | nvlist_t **child; | |
cc99f275 DB |
2954 | boolean_t printed = B_FALSE; |
2955 | ||
2956 | assert(zhp != NULL || !cb->cb_verbose); | |
9babb374 BB |
2957 | |
2958 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, | |
2959 | &children) != 0) | |
2960 | return; | |
2961 | ||
9babb374 BB |
2962 | for (c = 0; c < children; c++) { |
2963 | uint64_t is_log = B_FALSE; | |
d1807f16 RY |
2964 | const char *bias = NULL; |
2965 | const char *type = NULL; | |
9babb374 BB |
2966 | |
2967 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
2968 | &is_log); | |
cc99f275 DB |
2969 | |
2970 | if (is_log) { | |
a926aab9 | 2971 | bias = (char *)VDEV_ALLOC_CLASS_LOGS; |
cc99f275 DB |
2972 | } else { |
2973 | (void) nvlist_lookup_string(child[c], | |
2974 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
2975 | (void) nvlist_lookup_string(child[c], | |
2976 | ZPOOL_CONFIG_TYPE, &type); | |
2977 | } | |
2978 | ||
2979 | if (bias == NULL || strcmp(bias, class) != 0) | |
2980 | continue; | |
2981 | if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
9babb374 | 2982 | continue; |
cc99f275 DB |
2983 | |
2984 | if (!printed) { | |
2985 | (void) printf("\t%s\t\n", gettext(class)); | |
2986 | printed = B_TRUE; | |
2987 | } | |
2988 | ||
2989 | char *name = zpool_vdev_name(g_zfs, zhp, child[c], | |
fea33e4e HJ |
2990 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2991 | if (cb->cb_print_status) | |
2992 | print_status_config(zhp, cb, name, child[c], 2, | |
9a49d3f3 | 2993 | B_FALSE, NULL); |
9babb374 | 2994 | else |
fea33e4e | 2995 | print_import_config(cb, name, child[c], 2); |
9babb374 BB |
2996 | free(name); |
2997 | } | |
2998 | } | |
428870ff | 2999 | |
34dc7c2f BB |
3000 | /* |
3001 | * Display the status for the given pool. | |
3002 | */ | |
0936981d GW |
3003 | static int |
3004 | show_import(nvlist_t *config, boolean_t report_error) | |
34dc7c2f BB |
3005 | { |
3006 | uint64_t pool_state; | |
3007 | vdev_stat_t *vs; | |
d1807f16 | 3008 | const char *name; |
34dc7c2f | 3009 | uint64_t guid; |
379ca9cf | 3010 | uint64_t hostid = 0; |
a926aab9 AZ |
3011 | const char *msgid; |
3012 | const char *hostname = "unknown"; | |
379ca9cf | 3013 | nvlist_t *nvroot, *nvinfo; |
731782ec | 3014 | zpool_status_t reason; |
ffe9d382 | 3015 | zpool_errata_t errata; |
34dc7c2f BB |
3016 | const char *health; |
3017 | uint_t vsc; | |
d1807f16 | 3018 | const char *comment; |
fea33e4e | 3019 | status_cbdata_t cb = { 0 }; |
34dc7c2f BB |
3020 | |
3021 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
3022 | &name) == 0); | |
3023 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
3024 | &guid) == 0); | |
3025 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
3026 | &pool_state) == 0); | |
3027 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
3028 | &nvroot) == 0); | |
3029 | ||
428870ff | 3030 | verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
3031 | (uint64_t **)&vs, &vsc) == 0); |
3032 | health = zpool_state_to_name(vs->vs_state, vs->vs_aux); | |
3033 | ||
ffe9d382 | 3034 | reason = zpool_import_status(config, &msgid, &errata); |
34dc7c2f | 3035 | |
0936981d GW |
3036 | /* |
3037 | * If we're importing using a cachefile, then we won't report any | |
3038 | * errors unless we are in the scan phase of the import. | |
3039 | */ | |
3040 | if (reason != ZPOOL_STATUS_OK && !report_error) | |
3041 | return (reason); | |
3042 | ||
d96eb2b1 DM |
3043 | (void) printf(gettext(" pool: %s\n"), name); |
3044 | (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); | |
3045 | (void) printf(gettext(" state: %s"), health); | |
34dc7c2f BB |
3046 | if (pool_state == POOL_STATE_DESTROYED) |
3047 | (void) printf(gettext(" (DESTROYED)")); | |
3048 | (void) printf("\n"); | |
3049 | ||
3050 | switch (reason) { | |
3051 | case ZPOOL_STATUS_MISSING_DEV_R: | |
3052 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
3053 | case ZPOOL_STATUS_BAD_GUID_SUM: | |
9fb2771a TH |
3054 | printf_color(ANSI_BOLD, gettext("status: ")); |
3055 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
d96eb2b1 | 3056 | "missing from the system.\n")); |
34dc7c2f BB |
3057 | break; |
3058 | ||
3059 | case ZPOOL_STATUS_CORRUPT_LABEL_R: | |
3060 | case ZPOOL_STATUS_CORRUPT_LABEL_NR: | |
9fb2771a TH |
3061 | printf_color(ANSI_BOLD, gettext("status: ")); |
3062 | printf_color(ANSI_YELLOW, gettext("One or more devices contains" | |
3063 | " corrupted data.\n")); | |
34dc7c2f BB |
3064 | break; |
3065 | ||
3066 | case ZPOOL_STATUS_CORRUPT_DATA: | |
d96eb2b1 DM |
3067 | (void) printf( |
3068 | gettext(" status: The pool data is corrupted.\n")); | |
34dc7c2f BB |
3069 | break; |
3070 | ||
3071 | case ZPOOL_STATUS_OFFLINE_DEV: | |
9fb2771a TH |
3072 | printf_color(ANSI_BOLD, gettext("status: ")); |
3073 | printf_color(ANSI_YELLOW, gettext("One or more devices " | |
34dc7c2f BB |
3074 | "are offlined.\n")); |
3075 | break; | |
3076 | ||
3077 | case ZPOOL_STATUS_CORRUPT_POOL: | |
9fb2771a TH |
3078 | printf_color(ANSI_BOLD, gettext("status: ")); |
3079 | printf_color(ANSI_YELLOW, gettext("The pool metadata is " | |
34dc7c2f BB |
3080 | "corrupted.\n")); |
3081 | break; | |
3082 | ||
3083 | case ZPOOL_STATUS_VERSION_OLDER: | |
9fb2771a TH |
3084 | printf_color(ANSI_BOLD, gettext("status: ")); |
3085 | printf_color(ANSI_YELLOW, gettext("The pool is formatted using " | |
3086 | "a legacy on-disk version.\n")); | |
34dc7c2f BB |
3087 | break; |
3088 | ||
3089 | case ZPOOL_STATUS_VERSION_NEWER: | |
9fb2771a TH |
3090 | printf_color(ANSI_BOLD, gettext("status: ")); |
3091 | printf_color(ANSI_YELLOW, gettext("The pool is formatted using " | |
3092 | "an incompatible version.\n")); | |
34dc7c2f | 3093 | break; |
b128c09f | 3094 | |
b9b24bb4 | 3095 | case ZPOOL_STATUS_FEAT_DISABLED: |
9fb2771a | 3096 | printf_color(ANSI_BOLD, gettext("status: ")); |
e086db16 CB |
3097 | printf_color(ANSI_YELLOW, gettext("Some supported " |
3098 | "features are not enabled on the pool.\n\t" | |
3099 | "(Note that they may be intentionally disabled " | |
3100 | "if the\n\t'compatibility' property is set.)\n")); | |
658fb802 CB |
3101 | break; |
3102 | ||
3103 | case ZPOOL_STATUS_COMPATIBILITY_ERR: | |
3104 | printf_color(ANSI_BOLD, gettext("status: ")); | |
3105 | printf_color(ANSI_YELLOW, gettext("Error reading or parsing " | |
3106 | "the file(s) indicated by the 'compatibility'\n" | |
3107 | "property.\n")); | |
b9b24bb4 CS |
3108 | break; |
3109 | ||
e086db16 CB |
3110 | case ZPOOL_STATUS_INCOMPATIBLE_FEAT: |
3111 | printf_color(ANSI_BOLD, gettext("status: ")); | |
3112 | printf_color(ANSI_YELLOW, gettext("One or more features " | |
3113 | "are enabled on the pool despite not being\n" | |
3114 | "requested by the 'compatibility' property.\n")); | |
3115 | break; | |
3116 | ||
9ae529ec | 3117 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
9fb2771a TH |
3118 | printf_color(ANSI_BOLD, gettext("status: ")); |
3119 | printf_color(ANSI_YELLOW, gettext("The pool uses the following " | |
4e33ba4c | 3120 | "feature(s) not supported on this system:\n")); |
9fb2771a | 3121 | color_start(ANSI_YELLOW); |
9ae529ec | 3122 | zpool_print_unsup_feat(config); |
9fb2771a | 3123 | color_end(); |
9ae529ec CS |
3124 | break; |
3125 | ||
3126 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
9fb2771a TH |
3127 | printf_color(ANSI_BOLD, gettext("status: ")); |
3128 | printf_color(ANSI_YELLOW, gettext("The pool can only be " | |
3129 | "accessed in read-only mode on this system. It\n\tcannot be" | |
3130 | " accessed in read-write mode because it uses the " | |
9ae529ec | 3131 | "following\n\tfeature(s) not supported on this system:\n")); |
9fb2771a | 3132 | color_start(ANSI_YELLOW); |
9ae529ec | 3133 | zpool_print_unsup_feat(config); |
9fb2771a | 3134 | color_end(); |
9ae529ec CS |
3135 | break; |
3136 | ||
379ca9cf | 3137 | case ZPOOL_STATUS_HOSTID_ACTIVE: |
9fb2771a TH |
3138 | printf_color(ANSI_BOLD, gettext("status: ")); |
3139 | printf_color(ANSI_YELLOW, gettext("The pool is currently " | |
379ca9cf OF |
3140 | "imported by another system.\n")); |
3141 | break; | |
3142 | ||
3143 | case ZPOOL_STATUS_HOSTID_REQUIRED: | |
9fb2771a TH |
3144 | printf_color(ANSI_BOLD, gettext("status: ")); |
3145 | printf_color(ANSI_YELLOW, gettext("The pool has the " | |
379ca9cf OF |
3146 | "multihost property on. It cannot\n\tbe safely imported " |
3147 | "when the system hostid is not set.\n")); | |
3148 | break; | |
3149 | ||
34dc7c2f | 3150 | case ZPOOL_STATUS_HOSTID_MISMATCH: |
9fb2771a TH |
3151 | printf_color(ANSI_BOLD, gettext("status: ")); |
3152 | printf_color(ANSI_YELLOW, gettext("The pool was last accessed " | |
3153 | "by another system.\n")); | |
34dc7c2f | 3154 | break; |
b128c09f | 3155 | |
34dc7c2f BB |
3156 | case ZPOOL_STATUS_FAULTED_DEV_R: |
3157 | case ZPOOL_STATUS_FAULTED_DEV_NR: | |
9fb2771a TH |
3158 | printf_color(ANSI_BOLD, gettext("status: ")); |
3159 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
34dc7c2f BB |
3160 | "faulted.\n")); |
3161 | break; | |
3162 | ||
b128c09f | 3163 | case ZPOOL_STATUS_BAD_LOG: |
9fb2771a TH |
3164 | printf_color(ANSI_BOLD, gettext("status: ")); |
3165 | printf_color(ANSI_YELLOW, gettext("An intent log record cannot " | |
3166 | "be read.\n")); | |
b128c09f BB |
3167 | break; |
3168 | ||
428870ff | 3169 | case ZPOOL_STATUS_RESILVERING: |
9a49d3f3 | 3170 | case ZPOOL_STATUS_REBUILDING: |
9fb2771a TH |
3171 | printf_color(ANSI_BOLD, gettext("status: ")); |
3172 | printf_color(ANSI_YELLOW, gettext("One or more devices were " | |
3173 | "being resilvered.\n")); | |
428870ff BB |
3174 | break; |
3175 | ||
ffe9d382 | 3176 | case ZPOOL_STATUS_ERRATA: |
9fb2771a TH |
3177 | printf_color(ANSI_BOLD, gettext("status: ")); |
3178 | printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), | |
ffe9d382 BB |
3179 | errata); |
3180 | break; | |
3181 | ||
c494aa7f GW |
3182 | case ZPOOL_STATUS_NON_NATIVE_ASHIFT: |
3183 | printf_color(ANSI_BOLD, gettext("status: ")); | |
3184 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
3185 | "configured to use a non-native block size.\n" | |
3186 | "\tExpect reduced performance.\n")); | |
3187 | break; | |
3188 | ||
34dc7c2f BB |
3189 | default: |
3190 | /* | |
3191 | * No other status can be seen when importing pools. | |
3192 | */ | |
3193 | assert(reason == ZPOOL_STATUS_OK); | |
3194 | } | |
3195 | ||
3196 | /* | |
3197 | * Print out an action according to the overall state of the pool. | |
3198 | */ | |
3199 | if (vs->vs_state == VDEV_STATE_HEALTHY) { | |
b9b24bb4 CS |
3200 | if (reason == ZPOOL_STATUS_VERSION_OLDER || |
3201 | reason == ZPOOL_STATUS_FEAT_DISABLED) { | |
d96eb2b1 | 3202 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
3203 | "imported using its name or numeric identifier, " |
3204 | "though\n\tsome features will not be available " | |
3205 | "without an explicit 'zpool upgrade'.\n")); | |
658fb802 CB |
3206 | } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) { |
3207 | (void) printf(gettext(" action: The pool can be " | |
3208 | "imported using its name or numeric\n\tidentifier, " | |
3209 | "though the file(s) indicated by its " | |
3210 | "'compatibility'\n\tproperty cannot be parsed at " | |
3211 | "this time.\n")); | |
b9b24bb4 | 3212 | } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { |
d96eb2b1 | 3213 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
3214 | "imported using its name or numeric " |
3215 | "identifier and\n\tthe '-f' flag.\n")); | |
ffe9d382 BB |
3216 | } else if (reason == ZPOOL_STATUS_ERRATA) { |
3217 | switch (errata) { | |
3218 | case ZPOOL_ERRATA_NONE: | |
3219 | break; | |
3220 | ||
4f2dcb3e RY |
3221 | case ZPOOL_ERRATA_ZOL_2094_SCRUB: |
3222 | (void) printf(gettext(" action: The pool can " | |
3223 | "be imported using its name or numeric " | |
3224 | "identifier,\n\thowever there is a compat" | |
3225 | "ibility issue which should be corrected" | |
3226 | "\n\tby running 'zpool scrub'\n")); | |
3227 | break; | |
3228 | ||
3229 | case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY: | |
3230 | (void) printf(gettext(" action: The pool can" | |
3231 | "not be imported with this version of ZFS " | |
3232 | "due to\n\tan active asynchronous destroy. " | |
3233 | "Revert to an earlier version\n\tand " | |
3234 | "allow the destroy to complete before " | |
3235 | "updating.\n")); | |
3236 | break; | |
3237 | ||
ae76f45c TC |
3238 | case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: |
3239 | (void) printf(gettext(" action: Existing " | |
3240 | "encrypted datasets contain an on-disk " | |
3241 | "incompatibility, which\n\tneeds to be " | |
3242 | "corrected. Backup these datasets to new " | |
3243 | "encrypted datasets\n\tand destroy the " | |
3244 | "old ones.\n")); | |
3245 | break; | |
3246 | ||
f00ab3f2 | 3247 | case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: |
b3b60984 JS |
3248 | (void) printf(gettext(" action: Existing " |
3249 | "encrypted snapshots and bookmarks contain " | |
3250 | "an on-disk\n\tincompatibility. This may " | |
e2dddb7e JS |
3251 | "cause on-disk corruption if they are used" |
3252 | "\n\twith 'zfs recv'. To correct the " | |
3253 | "issue, enable the bookmark_v2 feature.\n\t" | |
3254 | "No additional action is needed if there " | |
3255 | "are no encrypted snapshots or\n\t" | |
3256 | "bookmarks. If preserving the encrypted " | |
3257 | "snapshots and bookmarks is\n\trequired, " | |
3258 | "use a non-raw send to backup and restore " | |
3259 | "them. Alternately,\n\tthey may be removed" | |
3260 | " to resolve the incompatibility.\n")); | |
f00ab3f2 | 3261 | break; |
ffe9d382 BB |
3262 | default: |
3263 | /* | |
3264 | * All errata must contain an action message. | |
3265 | */ | |
3266 | assert(0); | |
3267 | } | |
b9b24bb4 | 3268 | } else { |
d96eb2b1 | 3269 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
3270 | "imported using its name or numeric " |
3271 | "identifier.\n")); | |
b9b24bb4 | 3272 | } |
34dc7c2f | 3273 | } else if (vs->vs_state == VDEV_STATE_DEGRADED) { |
d96eb2b1 | 3274 | (void) printf(gettext(" action: The pool can be imported " |
34dc7c2f BB |
3275 | "despite missing or damaged devices. The\n\tfault " |
3276 | "tolerance of the pool may be compromised if imported.\n")); | |
3277 | } else { | |
3278 | switch (reason) { | |
3279 | case ZPOOL_STATUS_VERSION_NEWER: | |
d96eb2b1 | 3280 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
3281 | "imported. Access the pool on a system running " |
3282 | "newer\n\tsoftware, or recreate the pool from " | |
3283 | "backup.\n")); | |
3284 | break; | |
9ae529ec | 3285 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
9fb2771a TH |
3286 | printf_color(ANSI_BOLD, gettext("action: ")); |
3287 | printf_color(ANSI_YELLOW, gettext("The pool cannot be " | |
9ae529ec CS |
3288 | "imported. Access the pool on a system that " |
3289 | "supports\n\tthe required feature(s), or recreate " | |
3290 | "the pool from backup.\n")); | |
3291 | break; | |
3292 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
9fb2771a TH |
3293 | printf_color(ANSI_BOLD, gettext("action: ")); |
3294 | printf_color(ANSI_YELLOW, gettext("The pool cannot be " | |
9ae529ec CS |
3295 | "imported in read-write mode. Import the pool " |
3296 | "with\n" | |
3297 | "\t\"-o readonly=on\", access the pool on a system " | |
3298 | "that supports the\n\trequired feature(s), or " | |
3299 | "recreate the pool from backup.\n")); | |
3300 | break; | |
34dc7c2f BB |
3301 | case ZPOOL_STATUS_MISSING_DEV_R: |
3302 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
3303 | case ZPOOL_STATUS_BAD_GUID_SUM: | |
d96eb2b1 | 3304 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
3305 | "imported. Attach the missing\n\tdevices and try " |
3306 | "again.\n")); | |
3307 | break; | |
379ca9cf OF |
3308 | case ZPOOL_STATUS_HOSTID_ACTIVE: |
3309 | VERIFY0(nvlist_lookup_nvlist(config, | |
3310 | ZPOOL_CONFIG_LOAD_INFO, &nvinfo)); | |
3311 | ||
3312 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) | |
3313 | hostname = fnvlist_lookup_string(nvinfo, | |
3314 | ZPOOL_CONFIG_MMP_HOSTNAME); | |
3315 | ||
3316 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) | |
3317 | hostid = fnvlist_lookup_uint64(nvinfo, | |
3318 | ZPOOL_CONFIG_MMP_HOSTID); | |
3319 | ||
3320 | (void) printf(gettext(" action: The pool must be " | |
bde0f7d4 AZ |
3321 | "exported from %s (hostid=%"PRIx64")\n\tbefore it " |
3322 | "can be safely imported.\n"), hostname, hostid); | |
379ca9cf OF |
3323 | break; |
3324 | case ZPOOL_STATUS_HOSTID_REQUIRED: | |
3325 | (void) printf(gettext(" action: Set a unique system " | |
b9373170 | 3326 | "hostid with the zgenhostid(8) command.\n")); |
379ca9cf | 3327 | break; |
34dc7c2f | 3328 | default: |
d96eb2b1 | 3329 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
3330 | "imported due to damaged devices or data.\n")); |
3331 | } | |
3332 | } | |
3333 | ||
d96eb2b1 DM |
3334 | /* Print the comment attached to the pool. */ |
3335 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
3336 | (void) printf(gettext("comment: %s\n"), comment); | |
3337 | ||
34dc7c2f BB |
3338 | /* |
3339 | * If the state is "closed" or "can't open", and the aux state | |
3340 | * is "corrupt data": | |
3341 | */ | |
3342 | if (((vs->vs_state == VDEV_STATE_CLOSED) || | |
3343 | (vs->vs_state == VDEV_STATE_CANT_OPEN)) && | |
3344 | (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { | |
3345 | if (pool_state == POOL_STATE_DESTROYED) | |
3346 | (void) printf(gettext("\tThe pool was destroyed, " | |
3347 | "but can be imported using the '-Df' flags.\n")); | |
3348 | else if (pool_state != POOL_STATE_EXPORTED) | |
3349 | (void) printf(gettext("\tThe pool may be active on " | |
3350 | "another system, but can be imported using\n\t" | |
3351 | "the '-f' flag.\n")); | |
3352 | } | |
3353 | ||
e458fcca BB |
3354 | if (msgid != NULL) { |
3355 | (void) printf(gettext( | |
a2f944a1 RM |
3356 | " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"), |
3357 | msgid); | |
e458fcca | 3358 | } |
34dc7c2f | 3359 | |
d96eb2b1 | 3360 | (void) printf(gettext(" config:\n\n")); |
34dc7c2f | 3361 | |
2df9ad1c GG |
3362 | cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name), |
3363 | VDEV_NAME_TYPE_ID); | |
fea33e4e HJ |
3364 | if (cb.cb_namewidth < 10) |
3365 | cb.cb_namewidth = 10; | |
34dc7c2f | 3366 | |
fea33e4e | 3367 | print_import_config(&cb, name, nvroot, 0); |
cc99f275 DB |
3368 | |
3369 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP); | |
3370 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL); | |
3371 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS); | |
34dc7c2f BB |
3372 | |
3373 | if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { | |
3374 | (void) printf(gettext("\n\tAdditional devices are known to " | |
3375 | "be part of this pool, though their\n\texact " | |
3376 | "configuration cannot be determined.\n")); | |
3377 | } | |
0936981d | 3378 | return (0); |
34dc7c2f BB |
3379 | } |
3380 | ||
379ca9cf OF |
3381 | static boolean_t |
3382 | zfs_force_import_required(nvlist_t *config) | |
3383 | { | |
3384 | uint64_t state; | |
3385 | uint64_t hostid = 0; | |
3386 | nvlist_t *nvinfo; | |
3387 | ||
3388 | state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE); | |
54b1b1d8 RN |
3389 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); |
3390 | ||
3391 | /* | |
3392 | * The hostid on LOAD_INFO comes from the MOS label via | |
3393 | * spa_tryimport(). If its not there then we're likely talking to an | |
3394 | * older kernel, so use the top one, which will be from the label | |
3395 | * discovered in zpool_find_import(), or if a cachefile is in use, the | |
3396 | * local hostid. | |
3397 | */ | |
3398 | if (nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_HOSTID, &hostid) != 0) | |
450f2d0b RN |
3399 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, |
3400 | &hostid); | |
379ca9cf OF |
3401 | |
3402 | if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid()) | |
3403 | return (B_TRUE); | |
3404 | ||
379ca9cf OF |
3405 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) { |
3406 | mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo, | |
3407 | ZPOOL_CONFIG_MMP_STATE); | |
3408 | ||
3409 | if (mmp_state != MMP_STATE_INACTIVE) | |
3410 | return (B_TRUE); | |
3411 | } | |
3412 | ||
3413 | return (B_FALSE); | |
3414 | } | |
3415 | ||
34dc7c2f BB |
3416 | /* |
3417 | * Perform the import for the given configuration. This passes the heavy | |
3418 | * lifting off to zpool_import_props(), and then mounts the datasets contained | |
3419 | * within the pool. | |
3420 | */ | |
3421 | static int | |
3422 | do_import(nvlist_t *config, const char *newname, const char *mntopts, | |
b64afa41 | 3423 | nvlist_t *props, int flags, uint_t mntthreads) |
34dc7c2f | 3424 | { |
b5256303 | 3425 | int ret = 0; |
71472bf3 | 3426 | int ms_status = 0; |
34dc7c2f | 3427 | zpool_handle_t *zhp; |
bf12053c | 3428 | const char *name; |
34dc7c2f | 3429 | uint64_t version; |
34dc7c2f | 3430 | |
379ca9cf | 3431 | name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME); |
379ca9cf | 3432 | version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION); |
34dc7c2f | 3433 | |
9ae529ec | 3434 | if (!SPA_VERSION_IS_SUPPORTED(version)) { |
34dc7c2f | 3435 | (void) fprintf(stderr, gettext("cannot import '%s': pool " |
9ae529ec | 3436 | "is formatted using an unsupported ZFS version\n"), name); |
34dc7c2f | 3437 | return (1); |
379ca9cf | 3438 | } else if (zfs_force_import_required(config) && |
572e2857 | 3439 | !(flags & ZFS_IMPORT_ANY_HOST)) { |
379ca9cf OF |
3440 | mmp_state_t mmp_state = MMP_STATE_INACTIVE; |
3441 | nvlist_t *nvinfo; | |
3442 | ||
3443 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
3444 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) | |
3445 | mmp_state = fnvlist_lookup_uint64(nvinfo, | |
3446 | ZPOOL_CONFIG_MMP_STATE); | |
3447 | ||
3448 | if (mmp_state == MMP_STATE_ACTIVE) { | |
bf12053c | 3449 | const char *hostname = "<unknown>"; |
379ca9cf OF |
3450 | uint64_t hostid = 0; |
3451 | ||
3452 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) | |
3453 | hostname = fnvlist_lookup_string(nvinfo, | |
3454 | ZPOOL_CONFIG_MMP_HOSTNAME); | |
3455 | ||
3456 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) | |
3457 | hostid = fnvlist_lookup_uint64(nvinfo, | |
3458 | ZPOOL_CONFIG_MMP_HOSTID); | |
3459 | ||
3460 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3461 | "pool is imported on %s (hostid: " | |
bde0f7d4 AZ |
3462 | "0x%"PRIx64")\nExport the pool on the other " |
3463 | "system, then run 'zpool import'.\n"), | |
3464 | name, hostname, hostid); | |
379ca9cf OF |
3465 | } else if (mmp_state == MMP_STATE_NO_HOSTID) { |
3466 | (void) fprintf(stderr, gettext("Cannot import '%s': " | |
3467 | "pool has the multihost property on and the\n" | |
3468 | "system's hostid is not set. Set a unique hostid " | |
b9373170 | 3469 | "with the zgenhostid(8) command.\n"), name); |
379ca9cf | 3470 | } else { |
bf12053c AZ |
3471 | const char *hostname = "<unknown>"; |
3472 | time_t timestamp = 0; | |
379ca9cf OF |
3473 | uint64_t hostid = 0; |
3474 | ||
54b1b1d8 RN |
3475 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTNAME)) |
3476 | hostname = fnvlist_lookup_string(nvinfo, | |
3477 | ZPOOL_CONFIG_HOSTNAME); | |
3478 | else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME)) | |
379ca9cf OF |
3479 | hostname = fnvlist_lookup_string(config, |
3480 | ZPOOL_CONFIG_HOSTNAME); | |
3481 | ||
3482 | if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP)) | |
3483 | timestamp = fnvlist_lookup_uint64(config, | |
3484 | ZPOOL_CONFIG_TIMESTAMP); | |
3485 | ||
54b1b1d8 RN |
3486 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTID)) |
3487 | hostid = fnvlist_lookup_uint64(nvinfo, | |
3488 | ZPOOL_CONFIG_HOSTID); | |
3489 | else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID)) | |
379ca9cf OF |
3490 | hostid = fnvlist_lookup_uint64(config, |
3491 | ZPOOL_CONFIG_HOSTID); | |
3492 | ||
3493 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3494 | "pool was previously in use from another system.\n" | |
bde0f7d4 | 3495 | "Last accessed by %s (hostid=%"PRIx64") at %s" |
379ca9cf OF |
3496 | "The pool can be imported, use 'zpool import -f' " |
3497 | "to import the pool.\n"), name, hostname, | |
bde0f7d4 | 3498 | hostid, ctime(×tamp)); |
34dc7c2f | 3499 | } |
379ca9cf OF |
3500 | |
3501 | return (1); | |
34dc7c2f BB |
3502 | } |
3503 | ||
572e2857 | 3504 | if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) |
34dc7c2f BB |
3505 | return (1); |
3506 | ||
3507 | if (newname != NULL) | |
bf12053c | 3508 | name = newname; |
34dc7c2f | 3509 | |
45d1cae3 BB |
3510 | if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) |
3511 | return (1); | |
34dc7c2f | 3512 | |
b5256303 TC |
3513 | /* |
3514 | * Loading keys is best effort. We don't want to return immediately | |
3515 | * if it fails but we do want to give the error to the caller. | |
3516 | */ | |
a401a2c5 AZ |
3517 | if (flags & ZFS_IMPORT_LOAD_KEYS && |
3518 | zfs_crypto_attempt_load_keys(g_zfs, name) != 0) | |
b5256303 | 3519 | ret = 1; |
b5256303 | 3520 | |
d164b209 | 3521 | if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && |
71472bf3 | 3522 | !(flags & ZFS_IMPORT_ONLY)) { |
b64afa41 | 3523 | ms_status = zpool_enable_datasets(zhp, mntopts, 0, mntthreads); |
71472bf3 US |
3524 | if (ms_status == EZFS_SHAREFAILED) { |
3525 | (void) fprintf(stderr, gettext("Import was " | |
9b43d7ba | 3526 | "successful, but unable to share some datasets\n")); |
71472bf3 US |
3527 | } else if (ms_status == EZFS_MOUNTFAILED) { |
3528 | (void) fprintf(stderr, gettext("Import was " | |
9b43d7ba | 3529 | "successful, but unable to mount some datasets\n")); |
71472bf3 | 3530 | } |
34dc7c2f BB |
3531 | } |
3532 | ||
3533 | zpool_close(zhp); | |
b5256303 | 3534 | return (ret); |
34dc7c2f BB |
3535 | } |
3536 | ||
c183d164 GW |
3537 | typedef struct import_parameters { |
3538 | nvlist_t *ip_config; | |
3539 | const char *ip_mntopts; | |
3540 | nvlist_t *ip_props; | |
3541 | int ip_flags; | |
b64afa41 | 3542 | uint_t ip_mntthreads; |
c183d164 GW |
3543 | int *ip_err; |
3544 | } import_parameters_t; | |
3545 | ||
3546 | static void | |
3547 | do_import_task(void *arg) | |
3548 | { | |
3549 | import_parameters_t *ip = arg; | |
3550 | *ip->ip_err |= do_import(ip->ip_config, NULL, ip->ip_mntopts, | |
b64afa41 | 3551 | ip->ip_props, ip->ip_flags, ip->ip_mntthreads); |
c183d164 GW |
3552 | free(ip); |
3553 | } | |
3554 | ||
3555 | ||
0936981d GW |
3556 | static int |
3557 | import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags, | |
c183d164 | 3558 | char *orig_name, char *new_name, importargs_t *import) |
0936981d GW |
3559 | { |
3560 | nvlist_t *config = NULL; | |
3561 | nvlist_t *found_config = NULL; | |
3562 | uint64_t pool_state; | |
c183d164 GW |
3563 | boolean_t pool_specified = (import->poolname != NULL || |
3564 | import->guid != 0); | |
b64afa41 | 3565 | uint_t npools = 0; |
c183d164 GW |
3566 | |
3567 | ||
3568 | tpool_t *tp = NULL; | |
3569 | if (import->do_all) { | |
3570 | tp = tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN), | |
3571 | 0, NULL); | |
3572 | } | |
0936981d GW |
3573 | |
3574 | /* | |
3575 | * At this point we have a list of import candidate configs. Even if | |
3576 | * we were searching by pool name or guid, we still need to | |
3577 | * post-process the list to deal with pool state and possible | |
3578 | * duplicate names. | |
3579 | */ | |
3580 | int err = 0; | |
3581 | nvpair_t *elem = NULL; | |
3582 | boolean_t first = B_TRUE; | |
b64afa41 AS |
3583 | if (!pool_specified && import->do_all) { |
3584 | while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) | |
3585 | npools++; | |
3586 | } | |
0936981d GW |
3587 | while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { |
3588 | ||
3589 | verify(nvpair_value_nvlist(elem, &config) == 0); | |
3590 | ||
3591 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
3592 | &pool_state) == 0); | |
c183d164 GW |
3593 | if (!import->do_destroyed && |
3594 | pool_state == POOL_STATE_DESTROYED) | |
0936981d | 3595 | continue; |
c183d164 GW |
3596 | if (import->do_destroyed && |
3597 | pool_state != POOL_STATE_DESTROYED) | |
0936981d GW |
3598 | continue; |
3599 | ||
3600 | verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, | |
3601 | import->policy) == 0); | |
3602 | ||
3603 | if (!pool_specified) { | |
3604 | if (first) | |
3605 | first = B_FALSE; | |
c183d164 | 3606 | else if (!import->do_all) |
a926aab9 | 3607 | (void) fputc('\n', stdout); |
0936981d | 3608 | |
c183d164 GW |
3609 | if (import->do_all) { |
3610 | import_parameters_t *ip = safe_malloc( | |
3611 | sizeof (import_parameters_t)); | |
3612 | ||
3613 | ip->ip_config = config; | |
3614 | ip->ip_mntopts = mntopts; | |
3615 | ip->ip_props = props; | |
3616 | ip->ip_flags = flags; | |
b64afa41 | 3617 | ip->ip_mntthreads = mount_tp_nthr / npools; |
c183d164 GW |
3618 | ip->ip_err = &err; |
3619 | ||
3620 | (void) tpool_dispatch(tp, do_import_task, | |
3621 | (void *)ip); | |
0936981d GW |
3622 | } else { |
3623 | /* | |
3624 | * If we're importing from cachefile, then | |
3625 | * we don't want to report errors until we | |
3626 | * are in the scan phase of the import. If | |
3627 | * we get an error, then we return that error | |
3628 | * to invoke the scan phase. | |
3629 | */ | |
3630 | if (import->cachefile && !import->scan) | |
3631 | err = show_import(config, B_FALSE); | |
3632 | else | |
3633 | (void) show_import(config, B_TRUE); | |
3634 | } | |
3635 | } else if (import->poolname != NULL) { | |
d1807f16 | 3636 | const char *name; |
0936981d GW |
3637 | |
3638 | /* | |
3639 | * We are searching for a pool based on name. | |
3640 | */ | |
3641 | verify(nvlist_lookup_string(config, | |
3642 | ZPOOL_CONFIG_POOL_NAME, &name) == 0); | |
3643 | ||
3644 | if (strcmp(name, import->poolname) == 0) { | |
3645 | if (found_config != NULL) { | |
3646 | (void) fprintf(stderr, gettext( | |
3647 | "cannot import '%s': more than " | |
3648 | "one matching pool\n"), | |
3649 | import->poolname); | |
3650 | (void) fprintf(stderr, gettext( | |
3651 | "import by numeric ID instead\n")); | |
3652 | err = B_TRUE; | |
3653 | } | |
3654 | found_config = config; | |
3655 | } | |
3656 | } else { | |
3657 | uint64_t guid; | |
3658 | ||
3659 | /* | |
3660 | * Search for a pool by guid. | |
3661 | */ | |
3662 | verify(nvlist_lookup_uint64(config, | |
3663 | ZPOOL_CONFIG_POOL_GUID, &guid) == 0); | |
3664 | ||
3665 | if (guid == import->guid) | |
3666 | found_config = config; | |
3667 | } | |
3668 | } | |
c183d164 GW |
3669 | if (import->do_all) { |
3670 | tpool_wait(tp); | |
3671 | tpool_destroy(tp); | |
3672 | } | |
0936981d GW |
3673 | |
3674 | /* | |
3675 | * If we were searching for a specific pool, verify that we found a | |
3676 | * pool, and then do the import. | |
3677 | */ | |
3678 | if (pool_specified && err == 0) { | |
3679 | if (found_config == NULL) { | |
3680 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3681 | "no such pool available\n"), orig_name); | |
3682 | err = B_TRUE; | |
3683 | } else { | |
3684 | err |= do_import(found_config, new_name, | |
b64afa41 | 3685 | mntopts, props, flags, mount_tp_nthr); |
0936981d GW |
3686 | } |
3687 | } | |
3688 | ||
3689 | /* | |
3690 | * If we were just looking for pools, report an error if none were | |
3691 | * found. | |
3692 | */ | |
3693 | if (!pool_specified && first) | |
3694 | (void) fprintf(stderr, | |
3695 | gettext("no pools available to import\n")); | |
3696 | return (err); | |
3697 | } | |
3698 | ||
e89f1295 DB |
3699 | typedef struct target_exists_args { |
3700 | const char *poolname; | |
3701 | uint64_t poolguid; | |
3702 | } target_exists_args_t; | |
3703 | ||
3704 | static int | |
3705 | name_or_guid_exists(zpool_handle_t *zhp, void *data) | |
3706 | { | |
3707 | target_exists_args_t *args = data; | |
3708 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
3709 | int found = 0; | |
3710 | ||
3711 | if (config == NULL) | |
3712 | return (0); | |
3713 | ||
3714 | if (args->poolname != NULL) { | |
d1807f16 | 3715 | const char *pool_name; |
e89f1295 DB |
3716 | |
3717 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
3718 | &pool_name) == 0); | |
3719 | if (strcmp(pool_name, args->poolname) == 0) | |
3720 | found = 1; | |
3721 | } else { | |
3722 | uint64_t pool_guid; | |
3723 | ||
3724 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
3725 | &pool_guid) == 0); | |
3726 | if (pool_guid == args->poolguid) | |
3727 | found = 1; | |
3728 | } | |
3729 | zpool_close(zhp); | |
3730 | ||
3731 | return (found); | |
3732 | } | |
d2734cce SD |
3733 | /* |
3734 | * zpool checkpoint <pool> | |
3735 | * checkpoint --discard <pool> | |
3736 | * | |
3737 | * -d Discard the checkpoint from a checkpointed | |
3738 | * --discard pool. | |
3739 | * | |
e60e158e JG |
3740 | * -w Wait for discarding a checkpoint to complete. |
3741 | * --wait | |
3742 | * | |
d2734cce SD |
3743 | * Checkpoints the specified pool, by taking a "snapshot" of its |
3744 | * current state. A pool can only have one checkpoint at a time. | |
3745 | */ | |
3746 | int | |
3747 | zpool_do_checkpoint(int argc, char **argv) | |
3748 | { | |
e60e158e | 3749 | boolean_t discard, wait; |
d2734cce SD |
3750 | char *pool; |
3751 | zpool_handle_t *zhp; | |
3752 | int c, err; | |
3753 | ||
3754 | struct option long_options[] = { | |
3755 | {"discard", no_argument, NULL, 'd'}, | |
e60e158e | 3756 | {"wait", no_argument, NULL, 'w'}, |
d2734cce SD |
3757 | {0, 0, 0, 0} |
3758 | }; | |
3759 | ||
3760 | discard = B_FALSE; | |
e60e158e JG |
3761 | wait = B_FALSE; |
3762 | while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) { | |
d2734cce SD |
3763 | switch (c) { |
3764 | case 'd': | |
3765 | discard = B_TRUE; | |
3766 | break; | |
e60e158e JG |
3767 | case 'w': |
3768 | wait = B_TRUE; | |
3769 | break; | |
d2734cce SD |
3770 | case '?': |
3771 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3772 | optopt); | |
3773 | usage(B_FALSE); | |
3774 | } | |
3775 | } | |
3776 | ||
e60e158e JG |
3777 | if (wait && !discard) { |
3778 | (void) fprintf(stderr, gettext("--wait only valid when " | |
3779 | "--discard also specified\n")); | |
3780 | usage(B_FALSE); | |
3781 | } | |
3782 | ||
d2734cce SD |
3783 | argc -= optind; |
3784 | argv += optind; | |
3785 | ||
3786 | if (argc < 1) { | |
3787 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
3788 | usage(B_FALSE); | |
3789 | } | |
3790 | ||
3791 | if (argc > 1) { | |
3792 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
3793 | usage(B_FALSE); | |
3794 | } | |
3795 | ||
3796 | pool = argv[0]; | |
3797 | ||
3798 | if ((zhp = zpool_open(g_zfs, pool)) == NULL) { | |
3799 | /* As a special case, check for use of '/' in the name */ | |
3800 | if (strchr(pool, '/') != NULL) | |
3801 | (void) fprintf(stderr, gettext("'zpool checkpoint' " | |
3802 | "doesn't work on datasets. To save the state " | |
3803 | "of a dataset from a specific point in time " | |
3804 | "please use 'zfs snapshot'\n")); | |
3805 | return (1); | |
3806 | } | |
3807 | ||
e60e158e | 3808 | if (discard) { |
d2734cce | 3809 | err = (zpool_discard_checkpoint(zhp) != 0); |
e60e158e JG |
3810 | if (err == 0 && wait) |
3811 | err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD); | |
3812 | } else { | |
d2734cce | 3813 | err = (zpool_checkpoint(zhp) != 0); |
e60e158e | 3814 | } |
d2734cce SD |
3815 | |
3816 | zpool_close(zhp); | |
3817 | ||
3818 | return (err); | |
3819 | } | |
3820 | ||
3821 | #define CHECKPOINT_OPT 1024 | |
3822 | ||
34dc7c2f BB |
3823 | /* |
3824 | * zpool import [-d dir] [-D] | |
b5256303 | 3825 | * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] |
0936981d | 3826 | * [-d dir | -c cachefile | -s] [-f] -a |
b5256303 | 3827 | * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] |
0936981d GW |
3828 | * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id> |
3829 | * [newpool] | |
34dc7c2f | 3830 | * |
0936981d GW |
3831 | * -c Read pool information from a cachefile instead of searching |
3832 | * devices. If importing from a cachefile config fails, then | |
3833 | * fallback to searching for devices only in the directories that | |
3834 | * exist in the cachefile. | |
34dc7c2f | 3835 | * |
0936981d | 3836 | * -d Scan in a specific directory, other than /dev/. More than |
34dc7c2f BB |
3837 | * one directory can be specified using multiple '-d' options. |
3838 | * | |
0936981d GW |
3839 | * -D Scan for previously destroyed pools or import all or only |
3840 | * specified destroyed pools. | |
34dc7c2f | 3841 | * |
0936981d | 3842 | * -R Temporarily import the pool, with all mountpoints relative to |
34dc7c2f BB |
3843 | * the given root. The pool will remain exported when the machine |
3844 | * is rebooted. | |
3845 | * | |
0936981d GW |
3846 | * -V Import even in the presence of faulted vdevs. This is an |
3847 | * intentionally undocumented option for testing purposes, and | |
3848 | * treats the pool configuration as complete, leaving any bad | |
45d1cae3 BB |
3849 | * vdevs in the FAULTED state. In other words, it does verbatim |
3850 | * import. | |
b128c09f | 3851 | * |
0936981d | 3852 | * -f Force import, even if it appears that the pool is active. |
428870ff | 3853 | * |
0936981d | 3854 | * -F Attempt rewind if necessary. |
428870ff | 3855 | * |
0936981d | 3856 | * -n See if rewind would work, but don't actually rewind. |
428870ff | 3857 | * |
0936981d | 3858 | * -N Import the pool but don't mount datasets. |
572e2857 | 3859 | * |
0936981d GW |
3860 | * -T Specify a starting txg to use for import. This option is |
3861 | * intentionally undocumented option for testing purposes. | |
572e2857 | 3862 | * |
0936981d | 3863 | * -a Import all pools found. |
34dc7c2f | 3864 | * |
0936981d | 3865 | * -l Load encryption keys while importing. |
b5256303 | 3866 | * |
0936981d | 3867 | * -o Set property=value and/or temporary mount options (without '='). |
34dc7c2f | 3868 | * |
0936981d GW |
3869 | * -s Scan using the default search path, the libblkid cache will |
3870 | * not be consulted. | |
7d11e37e | 3871 | * |
0936981d GW |
3872 | * --rewind-to-checkpoint |
3873 | * Import the pool and revert back to the checkpoint. | |
d2734cce | 3874 | * |
34dc7c2f BB |
3875 | * The import command scans for pools to import, and import pools based on pool |
3876 | * name and GUID. The pool can also be renamed as part of the import process. | |
3877 | */ | |
3878 | int | |
3879 | zpool_do_import(int argc, char **argv) | |
3880 | { | |
3881 | char **searchdirs = NULL; | |
44867b6d | 3882 | char *env, *envdup = NULL; |
34dc7c2f BB |
3883 | int nsearch = 0; |
3884 | int c; | |
428870ff | 3885 | int err = 0; |
34dc7c2f BB |
3886 | nvlist_t *pools = NULL; |
3887 | boolean_t do_all = B_FALSE; | |
3888 | boolean_t do_destroyed = B_FALSE; | |
3889 | char *mntopts = NULL; | |
b128c09f BB |
3890 | uint64_t searchguid = 0; |
3891 | char *searchname = NULL; | |
34dc7c2f | 3892 | char *propval; |
428870ff | 3893 | nvlist_t *policy = NULL; |
34dc7c2f | 3894 | nvlist_t *props = NULL; |
572e2857 | 3895 | int flags = ZFS_IMPORT_NORMAL; |
428870ff BB |
3896 | uint32_t rewind_policy = ZPOOL_NO_REWIND; |
3897 | boolean_t dryrun = B_FALSE; | |
3898 | boolean_t do_rewind = B_FALSE; | |
3899 | boolean_t xtreme_rewind = B_FALSE; | |
7d11e37e | 3900 | boolean_t do_scan = B_FALSE; |
e89f1295 | 3901 | boolean_t pool_exists = B_FALSE; |
0936981d | 3902 | uint64_t txg = -1ULL; |
34dc7c2f | 3903 | char *cachefile = NULL; |
428870ff | 3904 | importargs_t idata = { 0 }; |
572e2857 | 3905 | char *endptr; |
34dc7c2f | 3906 | |
d2734cce SD |
3907 | struct option long_options[] = { |
3908 | {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT}, | |
3909 | {0, 0, 0, 0} | |
3910 | }; | |
3911 | ||
34dc7c2f | 3912 | /* check options */ |
d2734cce SD |
3913 | while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX", |
3914 | long_options, NULL)) != -1) { | |
34dc7c2f BB |
3915 | switch (c) { |
3916 | case 'a': | |
3917 | do_all = B_TRUE; | |
3918 | break; | |
3919 | case 'c': | |
3920 | cachefile = optarg; | |
3921 | break; | |
3922 | case 'd': | |
a281f769 AZ |
3923 | searchdirs = safe_realloc(searchdirs, |
3924 | (nsearch + 1) * sizeof (char *)); | |
34dc7c2f BB |
3925 | searchdirs[nsearch++] = optarg; |
3926 | break; | |
3927 | case 'D': | |
3928 | do_destroyed = B_TRUE; | |
3929 | break; | |
3930 | case 'f': | |
572e2857 | 3931 | flags |= ZFS_IMPORT_ANY_HOST; |
34dc7c2f | 3932 | break; |
b128c09f | 3933 | case 'F': |
428870ff BB |
3934 | do_rewind = B_TRUE; |
3935 | break; | |
b5256303 TC |
3936 | case 'l': |
3937 | flags |= ZFS_IMPORT_LOAD_KEYS; | |
3938 | break; | |
572e2857 BB |
3939 | case 'm': |
3940 | flags |= ZFS_IMPORT_MISSING_LOG; | |
3941 | break; | |
428870ff BB |
3942 | case 'n': |
3943 | dryrun = B_TRUE; | |
b128c09f | 3944 | break; |
572e2857 BB |
3945 | case 'N': |
3946 | flags |= ZFS_IMPORT_ONLY; | |
3947 | break; | |
34dc7c2f BB |
3948 | case 'o': |
3949 | if ((propval = strchr(optarg, '=')) != NULL) { | |
3950 | *propval = '\0'; | |
3951 | propval++; | |
b128c09f BB |
3952 | if (add_prop_list(optarg, propval, |
3953 | &props, B_TRUE)) | |
34dc7c2f BB |
3954 | goto error; |
3955 | } else { | |
3956 | mntopts = optarg; | |
3957 | } | |
3958 | break; | |
3959 | case 'R': | |
3960 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 3961 | ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) |
34dc7c2f | 3962 | goto error; |
2f3ec900 | 3963 | if (add_prop_list_default(zpool_prop_to_name( |
e40ca391 | 3964 | ZPOOL_PROP_CACHEFILE), "none", &props)) |
34dc7c2f BB |
3965 | goto error; |
3966 | break; | |
7d11e37e BB |
3967 | case 's': |
3968 | do_scan = B_TRUE; | |
3969 | break; | |
26b42f3f RY |
3970 | case 't': |
3971 | flags |= ZFS_IMPORT_TEMP_NAME; | |
00d2a8c9 | 3972 | if (add_prop_list_default(zpool_prop_to_name( |
e40ca391 | 3973 | ZPOOL_PROP_CACHEFILE), "none", &props)) |
00d2a8c9 | 3974 | goto error; |
26b42f3f RY |
3975 | break; |
3976 | ||
572e2857 BB |
3977 | case 'T': |
3978 | errno = 0; | |
dea377c0 | 3979 | txg = strtoull(optarg, &endptr, 0); |
572e2857 BB |
3980 | if (errno != 0 || *endptr != '\0') { |
3981 | (void) fprintf(stderr, | |
3982 | gettext("invalid txg value\n")); | |
3983 | usage(B_FALSE); | |
3984 | } | |
3985 | rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; | |
3986 | break; | |
428870ff | 3987 | case 'V': |
572e2857 | 3988 | flags |= ZFS_IMPORT_VERBATIM; |
428870ff BB |
3989 | break; |
3990 | case 'X': | |
3991 | xtreme_rewind = B_TRUE; | |
3992 | break; | |
d2734cce SD |
3993 | case CHECKPOINT_OPT: |
3994 | flags |= ZFS_IMPORT_CHECKPOINT; | |
3995 | break; | |
34dc7c2f BB |
3996 | case ':': |
3997 | (void) fprintf(stderr, gettext("missing argument for " | |
3998 | "'%c' option\n"), optopt); | |
3999 | usage(B_FALSE); | |
4000 | break; | |
4001 | case '?': | |
4002 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
4003 | optopt); | |
4004 | usage(B_FALSE); | |
4005 | } | |
4006 | } | |
4007 | ||
4008 | argc -= optind; | |
4009 | argv += optind; | |
4010 | ||
4011 | if (cachefile && nsearch != 0) { | |
4012 | (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); | |
4013 | usage(B_FALSE); | |
4014 | } | |
4015 | ||
0936981d GW |
4016 | if (cachefile && do_scan) { |
4017 | (void) fprintf(stderr, gettext("-c is incompatible with -s\n")); | |
4018 | usage(B_FALSE); | |
4019 | } | |
4020 | ||
b5256303 TC |
4021 | if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) { |
4022 | (void) fprintf(stderr, gettext("-l is incompatible with -N\n")); | |
4023 | usage(B_FALSE); | |
4024 | } | |
4025 | ||
4026 | if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) { | |
4027 | (void) fprintf(stderr, gettext("-l is only meaningful during " | |
4028 | "an import\n")); | |
4029 | usage(B_FALSE); | |
4030 | } | |
4031 | ||
428870ff BB |
4032 | if ((dryrun || xtreme_rewind) && !do_rewind) { |
4033 | (void) fprintf(stderr, | |
4034 | gettext("-n or -X only meaningful with -F\n")); | |
4035 | usage(B_FALSE); | |
4036 | } | |
4037 | if (dryrun) | |
4038 | rewind_policy = ZPOOL_TRY_REWIND; | |
4039 | else if (do_rewind) | |
4040 | rewind_policy = ZPOOL_DO_REWIND; | |
4041 | if (xtreme_rewind) | |
4042 | rewind_policy |= ZPOOL_EXTREME_REWIND; | |
4043 | ||
4044 | /* In the future, we can capture further policy and include it here */ | |
4045 | if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || | |
8a393be3 PZ |
4046 | nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 || |
4047 | nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, | |
4048 | rewind_policy) != 0) | |
428870ff BB |
4049 | goto error; |
4050 | ||
34dc7c2f BB |
4051 | /* check argument count */ |
4052 | if (do_all) { | |
4053 | if (argc != 0) { | |
4054 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
4055 | usage(B_FALSE); | |
4056 | } | |
4057 | } else { | |
4058 | if (argc > 2) { | |
4059 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
4060 | usage(B_FALSE); | |
4061 | } | |
f74b821a | 4062 | } |
34dc7c2f | 4063 | |
f74b821a BB |
4064 | /* |
4065 | * Check for the effective uid. We do this explicitly here because | |
4066 | * otherwise any attempt to discover pools will silently fail. | |
4067 | */ | |
4068 | if (argc == 0 && geteuid() != 0) { | |
4069 | (void) fprintf(stderr, gettext("cannot " | |
4070 | "discover pools: permission denied\n")); | |
d603ed6c | 4071 | |
a401a2c5 | 4072 | free(searchdirs); |
a425f5bf | 4073 | nvlist_free(props); |
f74b821a BB |
4074 | nvlist_free(policy); |
4075 | return (1); | |
34dc7c2f BB |
4076 | } |
4077 | ||
34dc7c2f | 4078 | /* |
34dc7c2f BB |
4079 | * Depending on the arguments given, we do one of the following: |
4080 | * | |
4081 | * <none> Iterate through all pools and display information about | |
4082 | * each one. | |
4083 | * | |
4084 | * -a Iterate through all pools and try to import each one. | |
4085 | * | |
4086 | * <id> Find the pool that corresponds to the given GUID/pool | |
4087 | * name and import that one. | |
4088 | * | |
4089 | * -D Above options applies only to destroyed pools. | |
4090 | */ | |
4091 | if (argc != 0) { | |
4092 | char *endptr; | |
4093 | ||
4094 | errno = 0; | |
4095 | searchguid = strtoull(argv[0], &endptr, 10); | |
eaa52d32 | 4096 | if (errno != 0 || *endptr != '\0') { |
34dc7c2f | 4097 | searchname = argv[0]; |
eaa52d32 PJ |
4098 | searchguid = 0; |
4099 | } | |
34dc7c2f | 4100 | |
b128c09f | 4101 | /* |
428870ff | 4102 | * User specified a name or guid. Ensure it's unique. |
b128c09f | 4103 | */ |
e89f1295 DB |
4104 | target_exists_args_t search = {searchname, searchguid}; |
4105 | pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search); | |
b128c09f BB |
4106 | } |
4107 | ||
44867b6d BB |
4108 | /* |
4109 | * Check the environment for the preferred search path. | |
4110 | */ | |
4111 | if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) { | |
a281f769 | 4112 | char *dir, *tmp = NULL; |
44867b6d BB |
4113 | |
4114 | envdup = strdup(env); | |
4115 | ||
a281f769 AZ |
4116 | for (dir = strtok_r(envdup, ":", &tmp); |
4117 | dir != NULL; | |
4118 | dir = strtok_r(NULL, ":", &tmp)) { | |
4119 | searchdirs = safe_realloc(searchdirs, | |
4120 | (nsearch + 1) * sizeof (char *)); | |
44867b6d | 4121 | searchdirs[nsearch++] = dir; |
44867b6d BB |
4122 | } |
4123 | } | |
428870ff BB |
4124 | |
4125 | idata.path = searchdirs; | |
4126 | idata.paths = nsearch; | |
4127 | idata.poolname = searchname; | |
4128 | idata.guid = searchguid; | |
4129 | idata.cachefile = cachefile; | |
7d11e37e | 4130 | idata.scan = do_scan; |
6cb8e530 | 4131 | idata.policy = policy; |
c183d164 GW |
4132 | idata.do_destroyed = do_destroyed; |
4133 | idata.do_all = do_all; | |
428870ff | 4134 | |
d9ac17a5 US |
4135 | libpc_handle_t lpch = { |
4136 | .lpc_lib_handle = g_zfs, | |
4137 | .lpc_ops = &libzfs_config_ops, | |
4138 | .lpc_printerr = B_TRUE | |
4139 | }; | |
4140 | pools = zpool_search_import(&lpch, &idata); | |
428870ff | 4141 | |
e89f1295 | 4142 | if (pools != NULL && pool_exists && |
428870ff BB |
4143 | (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { |
4144 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
4145 | "a pool with that name already exists\n"), | |
4146 | argv[0]); | |
4147 | (void) fprintf(stderr, gettext("use the form '%s " | |
4148 | "<pool | id> <newpool>' to give it a new name\n"), | |
4149 | "zpool import"); | |
4150 | err = 1; | |
e89f1295 | 4151 | } else if (pools == NULL && pool_exists) { |
428870ff BB |
4152 | (void) fprintf(stderr, gettext("cannot import '%s': " |
4153 | "a pool with that name is already created/imported,\n"), | |
4154 | argv[0]); | |
4155 | (void) fprintf(stderr, gettext("and no additional pools " | |
4156 | "with that name were found\n")); | |
4157 | err = 1; | |
4158 | } else if (pools == NULL) { | |
b128c09f BB |
4159 | if (argc != 0) { |
4160 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
4161 | "no such pool available\n"), argv[0]); | |
4162 | } | |
428870ff BB |
4163 | err = 1; |
4164 | } | |
4165 | ||
4166 | if (err == 1) { | |
a281f769 AZ |
4167 | free(searchdirs); |
4168 | free(envdup); | |
428870ff | 4169 | nvlist_free(policy); |
a425f5bf | 4170 | nvlist_free(pools); |
4171 | nvlist_free(props); | |
b128c09f BB |
4172 | return (1); |
4173 | } | |
4174 | ||
d8381f50 | 4175 | err = import_pools(pools, props, mntopts, flags, |
c183d164 | 4176 | argc >= 1 ? argv[0] : NULL, argc >= 2 ? argv[1] : NULL, &idata); |
0936981d | 4177 | |
b128c09f | 4178 | /* |
0936981d GW |
4179 | * If we're using the cachefile and we failed to import, then |
4180 | * fallback to scanning the directory for pools that match | |
4181 | * those in the cachefile. | |
b128c09f | 4182 | */ |
0936981d GW |
4183 | if (err != 0 && cachefile != NULL) { |
4184 | (void) printf(gettext("cachefile import failed, retrying\n")); | |
34dc7c2f | 4185 | |
0936981d GW |
4186 | /* |
4187 | * We use the scan flag to gather the directories that exist | |
4188 | * in the cachefile. If we need to fallback to searching for | |
4189 | * the pool config, we will only search devices in these | |
4190 | * directories. | |
4191 | */ | |
4192 | idata.scan = B_TRUE; | |
4193 | nvlist_free(pools); | |
d9ac17a5 | 4194 | pools = zpool_search_import(&lpch, &idata); |
34dc7c2f | 4195 | |
d8381f50 | 4196 | err = import_pools(pools, props, mntopts, flags, |
c183d164 GW |
4197 | argc >= 1 ? argv[0] : NULL, argc >= 2 ? argv[1] : NULL, |
4198 | &idata); | |
34dc7c2f BB |
4199 | } |
4200 | ||
34dc7c2f BB |
4201 | error: |
4202 | nvlist_free(props); | |
4203 | nvlist_free(pools); | |
428870ff | 4204 | nvlist_free(policy); |
a281f769 AZ |
4205 | free(searchdirs); |
4206 | free(envdup); | |
34dc7c2f BB |
4207 | |
4208 | return (err ? 1 : 0); | |
4209 | } | |
4210 | ||
bec1067d AP |
4211 | /* |
4212 | * zpool sync [-f] [pool] ... | |
4213 | * | |
4214 | * -f (undocumented) force uberblock (and config including zpool cache file) | |
4215 | * update. | |
4216 | * | |
4217 | * Sync the specified pool(s). | |
4218 | * Without arguments "zpool sync" will sync all pools. | |
4219 | * This command initiates TXG sync(s) and will return after the TXG(s) commit. | |
4220 | * | |
4221 | */ | |
4222 | static int | |
4223 | zpool_do_sync(int argc, char **argv) | |
4224 | { | |
4225 | int ret; | |
4226 | boolean_t force = B_FALSE; | |
4227 | ||
4228 | /* check options */ | |
4229 | while ((ret = getopt(argc, argv, "f")) != -1) { | |
4230 | switch (ret) { | |
4231 | case 'f': | |
4232 | force = B_TRUE; | |
4233 | break; | |
4234 | case '?': | |
4235 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
4236 | optopt); | |
4237 | usage(B_FALSE); | |
4238 | } | |
4239 | } | |
4240 | ||
4241 | argc -= optind; | |
4242 | argv += optind; | |
4243 | ||
4244 | /* if argc == 0 we will execute zpool_sync_one on all pools */ | |
2a673e76 AJ |
4245 | ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, |
4246 | B_FALSE, zpool_sync_one, &force); | |
bec1067d AP |
4247 | |
4248 | return (ret); | |
4249 | } | |
4250 | ||
34dc7c2f | 4251 | typedef struct iostat_cbdata { |
193a37cb | 4252 | uint64_t cb_flags; |
34dc7c2f | 4253 | int cb_namewidth; |
1bd201e7 | 4254 | int cb_iteration; |
193a37cb TH |
4255 | boolean_t cb_verbose; |
4256 | boolean_t cb_literal; | |
4257 | boolean_t cb_scripted; | |
1bd201e7 | 4258 | zpool_list_t *cb_list; |
8720e9e7 | 4259 | vdev_cmd_data_list_t *vcdl; |
2a673e76 | 4260 | vdev_cbdata_t cb_vdevs; |
34dc7c2f BB |
4261 | } iostat_cbdata_t; |
4262 | ||
193a37cb TH |
4263 | /* iostat labels */ |
4264 | typedef struct name_and_columns { | |
4265 | const char *name; /* Column name */ | |
4266 | unsigned int columns; /* Center name to this number of columns */ | |
4267 | } name_and_columns_t; | |
4268 | ||
00888c08 | 4269 | #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */ |
193a37cb TH |
4270 | |
4271 | static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] = | |
4272 | { | |
4273 | [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2}, | |
4274 | {NULL}}, | |
4275 | [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, | |
00888c08 TB |
4276 | {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1}, |
4277 | {NULL}}, | |
193a37cb TH |
4278 | [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2}, |
4279 | {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2}, | |
00888c08 | 4280 | {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL}}, |
57dc41de TH |
4281 | [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, |
4282 | {"asyncq_wait", 2}, {NULL}}, | |
7e945072 | 4283 | [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2}, |
1b939560 | 4284 | {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, |
00888c08 | 4285 | {"trim", 2}, {"rebuild", 2}, {NULL}}, |
193a37cb TH |
4286 | }; |
4287 | ||
4288 | /* Shorthand - if "columns" field not set, default to 1 column */ | |
4289 | static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] = | |
4290 | { | |
4291 | [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"}, | |
4292 | {"write"}, {NULL}}, | |
4293 | [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, | |
00888c08 TB |
4294 | {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"}, |
4295 | {NULL}}, | |
193a37cb | 4296 | [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"}, |
1b939560 | 4297 | {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, |
00888c08 | 4298 | {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}}, |
193a37cb | 4299 | [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, |
00888c08 TB |
4300 | {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"}, |
4301 | {NULL}}, | |
7e945072 | 4302 | [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, |
00888c08 TB |
4303 | {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, |
4304 | {"ind"}, {"agg"}, {NULL}}, | |
7e945072 TH |
4305 | }; |
4306 | ||
4307 | static const char *histo_to_title[] = { | |
4308 | [IOS_L_HISTO] = "latency", | |
4309 | [IOS_RQ_HISTO] = "req_size", | |
193a37cb TH |
4310 | }; |
4311 | ||
4312 | /* | |
4313 | * Return the number of labels in a null-terminated name_and_columns_t | |
4314 | * array. | |
4315 | * | |
4316 | */ | |
4317 | static unsigned int | |
4318 | label_array_len(const name_and_columns_t *labels) | |
4319 | { | |
4320 | int i = 0; | |
4321 | ||
4322 | while (labels[i].name) | |
4323 | i++; | |
4324 | ||
4325 | return (i); | |
4326 | } | |
4327 | ||
7e945072 TH |
4328 | /* |
4329 | * Return the number of strings in a null-terminated string array. | |
4330 | * For example: | |
4331 | * | |
4332 | * const char foo[] = {"bar", "baz", NULL} | |
4333 | * | |
4334 | * returns 2 | |
4335 | */ | |
4336 | static uint64_t | |
4337 | str_array_len(const char *array[]) | |
4338 | { | |
4339 | uint64_t i = 0; | |
4340 | while (array[i]) | |
4341 | i++; | |
4342 | ||
4343 | return (i); | |
4344 | } | |
4345 | ||
4346 | ||
193a37cb TH |
4347 | /* |
4348 | * Return a default column width for default/latency/queue columns. This does | |
4349 | * not include histograms, which have their columns autosized. | |
4350 | */ | |
4351 | static unsigned int | |
4352 | default_column_width(iostat_cbdata_t *cb, enum iostat_type type) | |
4353 | { | |
4354 | unsigned long column_width = 5; /* Normal niceprint */ | |
4355 | static unsigned long widths[] = { | |
4356 | /* | |
4357 | * Choose some sane default column sizes for printing the | |
4358 | * raw numbers. | |
4359 | */ | |
4360 | [IOS_DEFAULT] = 15, /* 1PB capacity */ | |
4361 | [IOS_LATENCY] = 10, /* 1B ns = 10sec */ | |
4362 | [IOS_QUEUES] = 6, /* 1M queue entries */ | |
1b939560 BB |
4363 | [IOS_L_HISTO] = 10, /* 1B ns = 10sec */ |
4364 | [IOS_RQ_HISTO] = 6, /* 1M queue entries */ | |
193a37cb TH |
4365 | }; |
4366 | ||
4367 | if (cb->cb_literal) | |
4368 | column_width = widths[type]; | |
4369 | ||
4370 | return (column_width); | |
4371 | } | |
4372 | ||
4373 | /* | |
4374 | * Print the column labels, i.e: | |
4375 | * | |
4376 | * capacity operations bandwidth | |
4377 | * alloc free read write read write ... | |
4378 | * | |
4379 | * If force_column_width is set, use it for the column width. If not set, use | |
4380 | * the default column width. | |
4381 | */ | |
65c7cc49 | 4382 | static void |
193a37cb TH |
4383 | print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width, |
4384 | const name_and_columns_t labels[][IOSTAT_MAX_LABELS]) | |
4385 | { | |
4386 | int i, idx, s; | |
1b939560 | 4387 | int text_start, rw_column_width, spaces_to_end; |
193a37cb TH |
4388 | uint64_t flags = cb->cb_flags; |
4389 | uint64_t f; | |
4390 | unsigned int column_width = force_column_width; | |
4391 | ||
4392 | /* For each bit set in flags */ | |
4393 | for (f = flags; f; f &= ~(1ULL << idx)) { | |
4394 | idx = lowbit64(f) - 1; | |
4395 | if (!force_column_width) | |
4396 | column_width = default_column_width(cb, idx); | |
4397 | /* Print our top labels centered over "read write" label. */ | |
4398 | for (i = 0; i < label_array_len(labels[idx]); i++) { | |
4399 | const char *name = labels[idx][i].name; | |
4400 | /* | |
4401 | * We treat labels[][].columns == 0 as shorthand | |
4402 | * for one column. It makes writing out the label | |
4403 | * tables more concise. | |
4404 | */ | |
4405 | unsigned int columns = MAX(1, labels[idx][i].columns); | |
4406 | unsigned int slen = strlen(name); | |
4407 | ||
4408 | rw_column_width = (column_width * columns) + | |
4409 | (2 * (columns - 1)); | |
4410 | ||
1b939560 BB |
4411 | text_start = (int)((rw_column_width) / columns - |
4412 | slen / columns); | |
4413 | if (text_start < 0) | |
4414 | text_start = 0; | |
193a37cb TH |
4415 | |
4416 | printf(" "); /* Two spaces between columns */ | |
4417 | ||
4418 | /* Space from beginning of column to label */ | |
4419 | for (s = 0; s < text_start; s++) | |
4420 | printf(" "); | |
4421 | ||
4422 | printf("%s", name); | |
4423 | ||
4424 | /* Print space after label to end of column */ | |
4425 | spaces_to_end = rw_column_width - text_start - slen; | |
1b939560 BB |
4426 | if (spaces_to_end < 0) |
4427 | spaces_to_end = 0; | |
4428 | ||
193a37cb TH |
4429 | for (s = 0; s < spaces_to_end; s++) |
4430 | printf(" "); | |
193a37cb TH |
4431 | } |
4432 | } | |
193a37cb TH |
4433 | } |
4434 | ||
d6418de0 TH |
4435 | |
4436 | /* | |
4437 | * print_cmd_columns - Print custom column titles from -c | |
4438 | * | |
4439 | * If the user specified the "zpool status|iostat -c" then print their custom | |
4440 | * column titles in the header. For example, print_cmd_columns() would print | |
4441 | * the " col1 col2" part of this: | |
4442 | * | |
4443 | * $ zpool iostat -vc 'echo col1=val1; echo col2=val2' | |
4444 | * ... | |
4445 | * capacity operations bandwidth | |
4446 | * pool alloc free read write read write col1 col2 | |
4447 | * ---------- ----- ----- ----- ----- ----- ----- ---- ---- | |
4448 | * mypool 269K 1008M 0 0 107 946 | |
4449 | * mirror 269K 1008M 0 0 107 946 | |
4450 | * sdb - - 0 0 102 473 val1 val2 | |
4451 | * sdc - - 0 0 5 473 val1 val2 | |
4452 | * ---------- ----- ----- ----- ----- ----- ----- ---- ---- | |
4453 | */ | |
65c7cc49 | 4454 | static void |
d6418de0 TH |
4455 | print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes) |
4456 | { | |
4457 | int i, j; | |
4458 | vdev_cmd_data_t *data = &vcdl->data[0]; | |
4459 | ||
4460 | if (vcdl->count == 0 || data == NULL) | |
4461 | return; | |
4462 | ||
4463 | /* | |
4464 | * Each vdev cmd should have the same column names unless the user did | |
4465 | * something weird with their cmd. Just take the column names from the | |
4466 | * first vdev and assume it works for all of them. | |
4467 | */ | |
4468 | for (i = 0; i < vcdl->uniq_cols_cnt; i++) { | |
4469 | printf(" "); | |
4470 | if (use_dashes) { | |
4471 | for (j = 0; j < vcdl->uniq_cols_width[i]; j++) | |
4472 | printf("-"); | |
4473 | } else { | |
9fb2771a | 4474 | printf_color(ANSI_BOLD, "%*s", vcdl->uniq_cols_width[i], |
d6418de0 TH |
4475 | vcdl->uniq_cols[i]); |
4476 | } | |
4477 | } | |
4478 | } | |
4479 | ||
4480 | ||
193a37cb TH |
4481 | /* |
4482 | * Utility function to print out a line of dashes like: | |
4483 | * | |
4484 | * -------------------------------- ----- ----- ----- ----- ----- | |
4485 | * | |
4486 | * ...or a dashed named-row line like: | |
4487 | * | |
4488 | * logs - - - - - | |
4489 | * | |
4490 | * @cb: iostat data | |
4491 | * | |
4492 | * @force_column_width If non-zero, use the value as the column width. | |
4493 | * Otherwise use the default column widths. | |
4494 | * | |
4495 | * @name: Print a dashed named-row line starting | |
4496 | * with @name. Otherwise, print a regular | |
4497 | * dashed line. | |
4498 | */ | |
4499 | static void | |
4500 | print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width, | |
4501 | const char *name) | |
4502 | { | |
4503 | int i; | |
4504 | unsigned int namewidth; | |
4505 | uint64_t flags = cb->cb_flags; | |
4506 | uint64_t f; | |
4507 | int idx; | |
4508 | const name_and_columns_t *labels; | |
7e945072 TH |
4509 | const char *title; |
4510 | ||
4511 | ||
4512 | if (cb->cb_flags & IOS_ANYHISTO_M) { | |
4513 | title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; | |
2a673e76 | 4514 | } else if (cb->cb_vdevs.cb_names_count) { |
7e945072 TH |
4515 | title = "vdev"; |
4516 | } else { | |
4517 | title = "pool"; | |
4518 | } | |
4519 | ||
4520 | namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), | |
4521 | name ? strlen(name) : 0); | |
193a37cb | 4522 | |
193a37cb TH |
4523 | |
4524 | if (name) { | |
193a37cb TH |
4525 | printf("%-*s", namewidth, name); |
4526 | } else { | |
4527 | for (i = 0; i < namewidth; i++) | |
4528 | (void) printf("-"); | |
4529 | } | |
4530 | ||
4531 | /* For each bit in flags */ | |
4532 | for (f = flags; f; f &= ~(1ULL << idx)) { | |
4533 | unsigned int column_width; | |
4534 | idx = lowbit64(f) - 1; | |
4535 | if (force_column_width) | |
4536 | column_width = force_column_width; | |
4537 | else | |
4538 | column_width = default_column_width(cb, idx); | |
4539 | ||
4540 | labels = iostat_bottom_labels[idx]; | |
4541 | for (i = 0; i < label_array_len(labels); i++) { | |
4542 | if (name) | |
4543 | printf(" %*s-", column_width - 1, " "); | |
4544 | else | |
4545 | printf(" %.*s", column_width, | |
4546 | "--------------------"); | |
4547 | } | |
4548 | } | |
193a37cb TH |
4549 | } |
4550 | ||
4551 | ||
4552 | static void | |
4553 | print_iostat_separator_impl(iostat_cbdata_t *cb, | |
4554 | unsigned int force_column_width) | |
4555 | { | |
4556 | print_iostat_dashes(cb, force_column_width, NULL); | |
4557 | } | |
4558 | ||
34dc7c2f BB |
4559 | static void |
4560 | print_iostat_separator(iostat_cbdata_t *cb) | |
4561 | { | |
193a37cb TH |
4562 | print_iostat_separator_impl(cb, 0); |
4563 | } | |
4564 | ||
4565 | static void | |
4566 | print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width, | |
4567 | const char *histo_vdev_name) | |
4568 | { | |
4569 | unsigned int namewidth; | |
7e945072 TH |
4570 | const char *title; |
4571 | ||
7bde396a TR |
4572 | color_start(ANSI_BOLD); |
4573 | ||
7e945072 TH |
4574 | if (cb->cb_flags & IOS_ANYHISTO_M) { |
4575 | title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; | |
2a673e76 | 4576 | } else if (cb->cb_vdevs.cb_names_count) { |
7e945072 TH |
4577 | title = "vdev"; |
4578 | } else { | |
4579 | title = "pool"; | |
4580 | } | |
34dc7c2f | 4581 | |
7e945072 TH |
4582 | namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), |
4583 | histo_vdev_name ? strlen(histo_vdev_name) : 0); | |
193a37cb | 4584 | |
7e945072 | 4585 | if (histo_vdev_name) |
193a37cb TH |
4586 | printf("%-*s", namewidth, histo_vdev_name); |
4587 | else | |
4588 | printf("%*s", namewidth, ""); | |
4589 | ||
7e945072 | 4590 | |
193a37cb | 4591 | print_iostat_labels(cb, force_column_width, iostat_top_labels); |
d6418de0 | 4592 | printf("\n"); |
193a37cb | 4593 | |
7e945072 | 4594 | printf("%-*s", namewidth, title); |
193a37cb TH |
4595 | |
4596 | print_iostat_labels(cb, force_column_width, iostat_bottom_labels); | |
d6418de0 TH |
4597 | if (cb->vcdl != NULL) |
4598 | print_cmd_columns(cb->vcdl, 0); | |
4599 | ||
4600 | printf("\n"); | |
193a37cb TH |
4601 | |
4602 | print_iostat_separator_impl(cb, force_column_width); | |
d6418de0 TH |
4603 | |
4604 | if (cb->vcdl != NULL) | |
4605 | print_cmd_columns(cb->vcdl, 1); | |
4606 | ||
7bde396a TR |
4607 | color_end(); |
4608 | ||
d6418de0 | 4609 | printf("\n"); |
34dc7c2f BB |
4610 | } |
4611 | ||
4612 | static void | |
4613 | print_iostat_header(iostat_cbdata_t *cb) | |
4614 | { | |
193a37cb | 4615 | print_iostat_header_impl(cb, 0, NULL); |
34dc7c2f BB |
4616 | } |
4617 | ||
7bde396a TR |
4618 | /* |
4619 | * Prints a size string (i.e. 120M) with the suffix ("M") colored | |
4620 | * by order of magnitude. Uses column_size to add padding. | |
4621 | */ | |
4622 | static void | |
f9e1c63f | 4623 | print_stat_color(const char *statbuf, unsigned int column_size) |
7bde396a TR |
4624 | { |
4625 | fputs(" ", stdout); | |
f9e1c63f W |
4626 | size_t len = strlen(statbuf); |
4627 | while (len < column_size) { | |
4628 | fputc(' ', stdout); | |
4629 | column_size--; | |
4630 | } | |
7bde396a TR |
4631 | if (*statbuf == '0') { |
4632 | color_start(ANSI_GRAY); | |
4633 | fputc('0', stdout); | |
7bde396a TR |
4634 | } else { |
4635 | for (; *statbuf; statbuf++) { | |
4636 | if (*statbuf == 'K') color_start(ANSI_GREEN); | |
4637 | else if (*statbuf == 'M') color_start(ANSI_YELLOW); | |
4638 | else if (*statbuf == 'G') color_start(ANSI_RED); | |
4639 | else if (*statbuf == 'T') color_start(ANSI_BOLD_BLUE); | |
4640 | else if (*statbuf == 'P') color_start(ANSI_MAGENTA); | |
4641 | else if (*statbuf == 'E') color_start(ANSI_CYAN); | |
4642 | fputc(*statbuf, stdout); | |
4643 | if (--column_size <= 0) | |
4644 | break; | |
4645 | } | |
4646 | } | |
4647 | color_end(); | |
7bde396a | 4648 | } |
193a37cb | 4649 | |
34dc7c2f BB |
4650 | /* |
4651 | * Display a single statistic. | |
4652 | */ | |
4653 | static void | |
193a37cb TH |
4654 | print_one_stat(uint64_t value, enum zfs_nicenum_format format, |
4655 | unsigned int column_size, boolean_t scripted) | |
34dc7c2f BB |
4656 | { |
4657 | char buf[64]; | |
4658 | ||
193a37cb TH |
4659 | zfs_nicenum_format(value, buf, sizeof (buf), format); |
4660 | ||
4661 | if (scripted) | |
4662 | printf("\t%s", buf); | |
4663 | else | |
7bde396a | 4664 | print_stat_color(buf, column_size); |
193a37cb TH |
4665 | } |
4666 | ||
4667 | /* | |
4668 | * Calculate the default vdev stats | |
4669 | * | |
4670 | * Subtract oldvs from newvs, apply a scaling factor, and save the resulting | |
4671 | * stats into calcvs. | |
4672 | */ | |
4673 | static void | |
4674 | calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs, | |
4675 | vdev_stat_t *calcvs) | |
4676 | { | |
4677 | int i; | |
4678 | ||
4679 | memcpy(calcvs, newvs, sizeof (*calcvs)); | |
4680 | for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++) | |
4681 | calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]); | |
4682 | ||
4683 | for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++) | |
4684 | calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]); | |
4685 | } | |
4686 | ||
4687 | /* | |
4688 | * Internal representation of the extended iostats data. | |
4689 | * | |
4690 | * The extended iostat stats are exported in nvlists as either uint64_t arrays | |
4691 | * or single uint64_t's. We make both look like arrays to make them easier | |
4692 | * to process. In order to make single uint64_t's look like arrays, we set | |
4693 | * __data to the stat data, and then set *data = &__data with count = 1. Then, | |
4694 | * we can just use *data and count. | |
4695 | */ | |
4696 | struct stat_array { | |
4697 | uint64_t *data; | |
4698 | uint_t count; /* Number of entries in data[] */ | |
4699 | uint64_t __data; /* Only used when data is a single uint64_t */ | |
4700 | }; | |
4701 | ||
4702 | static uint64_t | |
4ea3f864 GM |
4703 | stat_histo_max(struct stat_array *nva, unsigned int len) |
4704 | { | |
193a37cb TH |
4705 | uint64_t max = 0; |
4706 | int i; | |
4707 | for (i = 0; i < len; i++) | |
4708 | max = MAX(max, array64_max(nva[i].data, nva[i].count)); | |
4709 | ||
4710 | return (max); | |
4711 | } | |
4712 | ||
4713 | /* | |
4714 | * Helper function to lookup a uint64_t array or uint64_t value and store its | |
4715 | * data as a stat_array. If the nvpair is a single uint64_t value, then we make | |
4716 | * it look like a one element array to make it easier to process. | |
4717 | */ | |
4718 | static int | |
4719 | nvpair64_to_stat_array(nvlist_t *nvl, const char *name, | |
4ea3f864 GM |
4720 | struct stat_array *nva) |
4721 | { | |
193a37cb TH |
4722 | nvpair_t *tmp; |
4723 | int ret; | |
4724 | ||
4725 | verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0); | |
4726 | switch (nvpair_type(tmp)) { | |
4727 | case DATA_TYPE_UINT64_ARRAY: | |
4728 | ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count); | |
4729 | break; | |
4730 | case DATA_TYPE_UINT64: | |
4731 | ret = nvpair_value_uint64(tmp, &nva->__data); | |
4732 | nva->data = &nva->__data; | |
4733 | nva->count = 1; | |
4734 | break; | |
4735 | default: | |
4736 | /* Not a uint64_t */ | |
4737 | ret = EINVAL; | |
4738 | break; | |
4739 | } | |
4740 | ||
4741 | return (ret); | |
4742 | } | |
4743 | ||
4744 | /* | |
4745 | * Given a list of nvlist names, look up the extended stats in newnv and oldnv, | |
4746 | * subtract them, and return the results in a newly allocated stat_array. | |
4747 | * You must free the returned array after you are done with it with | |
4748 | * free_calc_stats(). | |
4749 | * | |
4750 | * Additionally, you can set "oldnv" to NULL if you simply want the newnv | |
4751 | * values. | |
4752 | */ | |
4753 | static struct stat_array * | |
4754 | calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv, | |
4755 | nvlist_t *newnv) | |
4756 | { | |
4757 | nvlist_t *oldnvx = NULL, *newnvx; | |
4758 | struct stat_array *oldnva, *newnva, *calcnva; | |
4759 | int i, j; | |
4760 | unsigned int alloc_size = (sizeof (struct stat_array)) * len; | |
4761 | ||
4762 | /* Extract our extended stats nvlist from the main list */ | |
4763 | verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX, | |
4764 | &newnvx) == 0); | |
4765 | if (oldnv) { | |
4766 | verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX, | |
4767 | &oldnvx) == 0); | |
4768 | } | |
4769 | ||
4770 | newnva = safe_malloc(alloc_size); | |
4771 | oldnva = safe_malloc(alloc_size); | |
4772 | calcnva = safe_malloc(alloc_size); | |
4773 | ||
4774 | for (j = 0; j < len; j++) { | |
4775 | verify(nvpair64_to_stat_array(newnvx, names[j], | |
4776 | &newnva[j]) == 0); | |
4777 | calcnva[j].count = newnva[j].count; | |
4778 | alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]); | |
4779 | calcnva[j].data = safe_malloc(alloc_size); | |
4780 | memcpy(calcnva[j].data, newnva[j].data, alloc_size); | |
4781 | ||
4782 | if (oldnvx) { | |
4783 | verify(nvpair64_to_stat_array(oldnvx, names[j], | |
4784 | &oldnva[j]) == 0); | |
4785 | for (i = 0; i < oldnva[j].count; i++) | |
4786 | calcnva[j].data[i] -= oldnva[j].data[i]; | |
4787 | } | |
4788 | } | |
4789 | free(newnva); | |
4790 | free(oldnva); | |
4791 | return (calcnva); | |
4792 | } | |
4793 | ||
4794 | static void | |
4795 | free_calc_stats(struct stat_array *nva, unsigned int len) | |
4796 | { | |
4797 | int i; | |
4798 | for (i = 0; i < len; i++) | |
4799 | free(nva[i].data); | |
4800 | ||
4801 | free(nva); | |
4802 | } | |
4803 | ||
4804 | static void | |
4805 | print_iostat_histo(struct stat_array *nva, unsigned int len, | |
4806 | iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth, | |
4807 | double scale) | |
4808 | { | |
4809 | int i, j; | |
4810 | char buf[6]; | |
4811 | uint64_t val; | |
4812 | enum zfs_nicenum_format format; | |
4813 | unsigned int buckets; | |
7e945072 | 4814 | unsigned int start_bucket; |
193a37cb TH |
4815 | |
4816 | if (cb->cb_literal) | |
4817 | format = ZFS_NICENUM_RAW; | |
4818 | else | |
4819 | format = ZFS_NICENUM_1024; | |
4820 | ||
4821 | /* All these histos are the same size, so just use nva[0].count */ | |
4822 | buckets = nva[0].count; | |
4823 | ||
7e945072 TH |
4824 | if (cb->cb_flags & IOS_RQ_HISTO_M) { |
4825 | /* Start at 512 - req size should never be lower than this */ | |
4826 | start_bucket = 9; | |
4827 | } else { | |
4828 | start_bucket = 0; | |
4829 | } | |
193a37cb | 4830 | |
7e945072 | 4831 | for (j = start_bucket; j < buckets; j++) { |
193a37cb | 4832 | /* Print histogram bucket label */ |
7e945072 TH |
4833 | if (cb->cb_flags & IOS_L_HISTO_M) { |
4834 | /* Ending range of this bucket */ | |
4835 | val = (1UL << (j + 1)) - 1; | |
4836 | zfs_nicetime(val, buf, sizeof (buf)); | |
4837 | } else { | |
4838 | /* Request size (starting range of bucket) */ | |
4839 | val = (1UL << j); | |
4840 | zfs_nicenum(val, buf, sizeof (buf)); | |
4841 | } | |
4842 | ||
193a37cb | 4843 | if (cb->cb_scripted) |
02730c33 | 4844 | printf("%llu", (u_longlong_t)val); |
193a37cb TH |
4845 | else |
4846 | printf("%-*s", namewidth, buf); | |
4847 | ||
4848 | /* Print the values on the line */ | |
4849 | for (i = 0; i < len; i++) { | |
4850 | print_one_stat(nva[i].data[j] * scale, format, | |
4851 | column_width, cb->cb_scripted); | |
4852 | } | |
4853 | printf("\n"); | |
4854 | } | |
4855 | } | |
4856 | ||
4857 | static void | |
4858 | print_solid_separator(unsigned int length) | |
4859 | { | |
4860 | while (length--) | |
4861 | printf("-"); | |
4862 | printf("\n"); | |
4863 | } | |
4864 | ||
4865 | static void | |
4866 | print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv, | |
4867 | nvlist_t *newnv, double scale, const char *name) | |
4868 | { | |
4869 | unsigned int column_width; | |
4870 | unsigned int namewidth; | |
4871 | unsigned int entire_width; | |
7e945072 | 4872 | enum iostat_type type; |
193a37cb | 4873 | struct stat_array *nva; |
7e945072 TH |
4874 | const char **names; |
4875 | unsigned int names_len; | |
4876 | ||
4877 | /* What type of histo are we? */ | |
4878 | type = IOS_HISTO_IDX(cb->cb_flags); | |
4879 | ||
4880 | /* Get NULL-terminated array of nvlist names for our histo */ | |
4881 | names = vsx_type_to_nvlist[type]; | |
4882 | names_len = str_array_len(names); /* num of names */ | |
4883 | ||
4884 | nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv); | |
193a37cb TH |
4885 | |
4886 | if (cb->cb_literal) { | |
4887 | column_width = MAX(5, | |
7e945072 | 4888 | (unsigned int) log10(stat_histo_max(nva, names_len)) + 1); |
193a37cb TH |
4889 | } else { |
4890 | column_width = 5; | |
4891 | } | |
4892 | ||
7e945072 TH |
4893 | namewidth = MAX(cb->cb_namewidth, |
4894 | strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)])); | |
193a37cb TH |
4895 | |
4896 | /* | |
4897 | * Calculate the entire line width of what we're printing. The | |
4898 | * +2 is for the two spaces between columns: | |
4899 | */ | |
4900 | /* read write */ | |
4901 | /* ----- ----- */ | |
4902 | /* |___| <---------- column_width */ | |
4903 | /* */ | |
4904 | /* |__________| <--- entire_width */ | |
4905 | /* */ | |
4906 | entire_width = namewidth + (column_width + 2) * | |
7e945072 | 4907 | label_array_len(iostat_bottom_labels[type]); |
193a37cb TH |
4908 | |
4909 | if (cb->cb_scripted) | |
4910 | printf("%s\n", name); | |
4911 | else | |
4912 | print_iostat_header_impl(cb, column_width, name); | |
4913 | ||
7e945072 | 4914 | print_iostat_histo(nva, names_len, cb, column_width, |
193a37cb TH |
4915 | namewidth, scale); |
4916 | ||
7e945072 | 4917 | free_calc_stats(nva, names_len); |
193a37cb TH |
4918 | if (!cb->cb_scripted) |
4919 | print_solid_separator(entire_width); | |
4920 | } | |
4921 | ||
4922 | /* | |
4923 | * Calculate the average latency of a power-of-two latency histogram | |
4924 | */ | |
4925 | static uint64_t | |
4926 | single_histo_average(uint64_t *histo, unsigned int buckets) | |
4927 | { | |
4928 | int i; | |
4929 | uint64_t count = 0, total = 0; | |
4930 | ||
4931 | for (i = 0; i < buckets; i++) { | |
4932 | /* | |
4933 | * Our buckets are power-of-two latency ranges. Use the | |
4934 | * midpoint latency of each bucket to calculate the average. | |
4935 | * For example: | |
4936 | * | |
4937 | * Bucket Midpoint | |
4938 | * 8ns-15ns: 12ns | |
4939 | * 16ns-31ns: 24ns | |
4940 | * ... | |
4941 | */ | |
4942 | if (histo[i] != 0) { | |
4943 | total += histo[i] * (((1UL << i) + ((1UL << i)/2))); | |
4944 | count += histo[i]; | |
4945 | } | |
4946 | } | |
4947 | ||
4948 | /* Prevent divide by zero */ | |
4949 | return (count == 0 ? 0 : total / count); | |
4950 | } | |
4951 | ||
4952 | static void | |
e40ca391 | 4953 | print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *newnv) |
193a37cb | 4954 | { |
193a37cb TH |
4955 | const char *names[] = { |
4956 | ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE, | |
4957 | ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, | |
4958 | ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE, | |
4959 | ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, | |
4960 | ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE, | |
4961 | ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, | |
4962 | ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE, | |
4963 | ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, | |
4964 | ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE, | |
4965 | ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, | |
1b939560 BB |
4966 | ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE, |
4967 | ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, | |
00888c08 TB |
4968 | ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE, |
4969 | ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE, | |
193a37cb TH |
4970 | }; |
4971 | ||
4972 | struct stat_array *nva; | |
4973 | ||
4974 | unsigned int column_width = default_column_width(cb, IOS_QUEUES); | |
4975 | enum zfs_nicenum_format format; | |
4976 | ||
4977 | nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv); | |
4978 | ||
4979 | if (cb->cb_literal) | |
4980 | format = ZFS_NICENUM_RAW; | |
4981 | else | |
4982 | format = ZFS_NICENUM_1024; | |
4983 | ||
e40ca391 AZ |
4984 | for (int i = 0; i < ARRAY_SIZE(names); i++) { |
4985 | uint64_t val = nva[i].data[0]; | |
193a37cb TH |
4986 | print_one_stat(val, format, column_width, cb->cb_scripted); |
4987 | } | |
4988 | ||
4989 | free_calc_stats(nva, ARRAY_SIZE(names)); | |
4990 | } | |
4991 | ||
4992 | static void | |
4993 | print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv, | |
b954e36e | 4994 | nvlist_t *newnv) |
193a37cb TH |
4995 | { |
4996 | int i; | |
4997 | uint64_t val; | |
4998 | const char *names[] = { | |
4999 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
5000 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
5001 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
5002 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
5003 | ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, | |
5004 | ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, | |
5005 | ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, | |
5006 | ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, | |
5007 | ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, | |
1b939560 | 5008 | ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, |
00888c08 | 5009 | ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, |
193a37cb TH |
5010 | }; |
5011 | struct stat_array *nva; | |
5012 | ||
5013 | unsigned int column_width = default_column_width(cb, IOS_LATENCY); | |
5014 | enum zfs_nicenum_format format; | |
5015 | ||
5016 | nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv); | |
5017 | ||
5018 | if (cb->cb_literal) | |
29eb4942 | 5019 | format = ZFS_NICENUM_RAWTIME; |
193a37cb TH |
5020 | else |
5021 | format = ZFS_NICENUM_TIME; | |
5022 | ||
5023 | /* Print our avg latencies on the line */ | |
5024 | for (i = 0; i < ARRAY_SIZE(names); i++) { | |
5025 | /* Compute average latency for a latency histo */ | |
b954e36e | 5026 | val = single_histo_average(nva[i].data, nva[i].count); |
193a37cb TH |
5027 | print_one_stat(val, format, column_width, cb->cb_scripted); |
5028 | } | |
5029 | free_calc_stats(nva, ARRAY_SIZE(names)); | |
5030 | } | |
5031 | ||
5032 | /* | |
5033 | * Print default statistics (capacity/operations/bandwidth) | |
5034 | */ | |
5035 | static void | |
5036 | print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale) | |
5037 | { | |
5038 | unsigned int column_width = default_column_width(cb, IOS_DEFAULT); | |
5039 | enum zfs_nicenum_format format; | |
5040 | char na; /* char to print for "not applicable" values */ | |
5041 | ||
5042 | if (cb->cb_literal) { | |
5043 | format = ZFS_NICENUM_RAW; | |
5044 | na = '0'; | |
5045 | } else { | |
5046 | format = ZFS_NICENUM_1024; | |
5047 | na = '-'; | |
5048 | } | |
5049 | ||
5050 | /* only toplevel vdevs have capacity stats */ | |
5051 | if (vs->vs_space == 0) { | |
5052 | if (cb->cb_scripted) | |
5053 | printf("\t%c\t%c", na, na); | |
5054 | else | |
5055 | printf(" %*c %*c", column_width, na, column_width, | |
5056 | na); | |
5057 | } else { | |
5058 | print_one_stat(vs->vs_alloc, format, column_width, | |
5059 | cb->cb_scripted); | |
5060 | print_one_stat(vs->vs_space - vs->vs_alloc, format, | |
5061 | column_width, cb->cb_scripted); | |
5062 | } | |
5063 | ||
5064 | print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale), | |
5065 | format, column_width, cb->cb_scripted); | |
5066 | print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale), | |
5067 | format, column_width, cb->cb_scripted); | |
5068 | print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale), | |
5069 | format, column_width, cb->cb_scripted); | |
5070 | print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale), | |
5071 | format, column_width, cb->cb_scripted); | |
34dc7c2f BB |
5072 | } |
5073 | ||
cf5a9d8c | 5074 | static const char *const class_name[] = { |
cc99f275 DB |
5075 | VDEV_ALLOC_BIAS_DEDUP, |
5076 | VDEV_ALLOC_BIAS_SPECIAL, | |
5077 | VDEV_ALLOC_CLASS_LOGS | |
5078 | }; | |
5079 | ||
34dc7c2f BB |
5080 | /* |
5081 | * Print out all the statistics for the given vdev. This can either be the | |
5082 | * toplevel configuration, or called recursively. If 'name' is NULL, then this | |
5083 | * is a verbose output, and we don't want to display the toplevel pool stats. | |
193a37cb TH |
5084 | * |
5085 | * Returns the number of stat lines printed. | |
34dc7c2f | 5086 | */ |
cc99f275 | 5087 | static unsigned int |
34dc7c2f BB |
5088 | print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, |
5089 | nvlist_t *newnv, iostat_cbdata_t *cb, int depth) | |
5090 | { | |
5091 | nvlist_t **oldchild, **newchild; | |
674b8934 | 5092 | uint_t c, children, oldchildren; |
193a37cb | 5093 | vdev_stat_t *oldvs, *newvs, *calcvs; |
34dc7c2f | 5094 | vdev_stat_t zerovs = { 0 }; |
193a37cb TH |
5095 | char *vname; |
5096 | int i; | |
5097 | int ret = 0; | |
34dc7c2f BB |
5098 | uint64_t tdelta; |
5099 | double scale; | |
193a37cb | 5100 | |
a1d477c2 MA |
5101 | if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) |
5102 | return (ret); | |
5103 | ||
75c62019 JCML |
5104 | calcvs = safe_malloc(sizeof (*calcvs)); |
5105 | ||
34dc7c2f | 5106 | if (oldnv != NULL) { |
428870ff BB |
5107 | verify(nvlist_lookup_uint64_array(oldnv, |
5108 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); | |
34dc7c2f BB |
5109 | } else { |
5110 | oldvs = &zerovs; | |
5111 | } | |
5112 | ||
193a37cb | 5113 | /* Do we only want to see a specific vdev? */ |
2a673e76 | 5114 | for (i = 0; i < cb->cb_vdevs.cb_names_count; i++) { |
193a37cb | 5115 | /* Yes we do. Is this the vdev? */ |
2a673e76 | 5116 | if (strcmp(name, cb->cb_vdevs.cb_names[i]) == 0) { |
193a37cb TH |
5117 | /* |
5118 | * This is our vdev. Since it is the only vdev we | |
5119 | * will be displaying, make depth = 0 so that it | |
5120 | * doesn't get indented. | |
5121 | */ | |
5122 | depth = 0; | |
5123 | break; | |
5124 | } | |
5125 | } | |
5126 | ||
2a673e76 | 5127 | if (cb->cb_vdevs.cb_names_count && (i == cb->cb_vdevs.cb_names_count)) { |
193a37cb TH |
5128 | /* Couldn't match the name */ |
5129 | goto children; | |
5130 | } | |
5131 | ||
5132 | ||
428870ff | 5133 | verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
5134 | (uint64_t **)&newvs, &c) == 0); |
5135 | ||
193a37cb TH |
5136 | /* |
5137 | * Print the vdev name unless it's is a histogram. Histograms | |
5138 | * display the vdev name in the header itself. | |
5139 | */ | |
7e945072 | 5140 | if (!(cb->cb_flags & IOS_ANYHISTO_M)) { |
193a37cb TH |
5141 | if (cb->cb_scripted) { |
5142 | printf("%s", name); | |
5143 | } else { | |
5144 | if (strlen(name) + depth > cb->cb_namewidth) | |
5145 | (void) printf("%*s%s", depth, "", name); | |
5146 | else | |
5147 | (void) printf("%*s%s%*s", depth, "", name, | |
5148 | (int)(cb->cb_namewidth - strlen(name) - | |
5149 | depth), ""); | |
5150 | } | |
5151 | } | |
34dc7c2f | 5152 | |
193a37cb | 5153 | /* Calculate our scaling factor */ |
34dc7c2f | 5154 | tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; |
7e945072 | 5155 | if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) { |
193a37cb TH |
5156 | /* |
5157 | * If we specify printing histograms with no time interval, then | |
5158 | * print the histogram numbers over the entire lifetime of the | |
5159 | * vdev. | |
5160 | */ | |
5161 | scale = 1; | |
34dc7c2f | 5162 | } else { |
193a37cb TH |
5163 | if (tdelta == 0) |
5164 | scale = 1.0; | |
5165 | else | |
5166 | scale = (double)NANOSEC / tdelta; | |
34dc7c2f BB |
5167 | } |
5168 | ||
193a37cb TH |
5169 | if (cb->cb_flags & IOS_DEFAULT_M) { |
5170 | calc_default_iostats(oldvs, newvs, calcvs); | |
5171 | print_iostat_default(calcvs, cb, scale); | |
5172 | } | |
5173 | if (cb->cb_flags & IOS_LATENCY_M) | |
b954e36e | 5174 | print_iostat_latency(cb, oldnv, newnv); |
193a37cb | 5175 | if (cb->cb_flags & IOS_QUEUES_M) |
e40ca391 | 5176 | print_iostat_queues(cb, newnv); |
7e945072 | 5177 | if (cb->cb_flags & IOS_ANYHISTO_M) { |
193a37cb TH |
5178 | printf("\n"); |
5179 | print_iostat_histos(cb, oldnv, newnv, scale, name); | |
5180 | } | |
34dc7c2f | 5181 | |
8720e9e7 | 5182 | if (cb->vcdl != NULL) { |
d1807f16 | 5183 | const char *path; |
8720e9e7 TH |
5184 | if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH, |
5185 | &path) == 0) { | |
d6418de0 | 5186 | printf(" "); |
8720e9e7 | 5187 | zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); |
8720e9e7 TH |
5188 | } |
5189 | } | |
5190 | ||
7e945072 | 5191 | if (!(cb->cb_flags & IOS_ANYHISTO_M)) |
193a37cb | 5192 | printf("\n"); |
34dc7c2f | 5193 | |
193a37cb | 5194 | ret++; |
34dc7c2f | 5195 | |
193a37cb | 5196 | children: |
92bc9278 | 5197 | |
5198 | free(calcvs); | |
5199 | ||
34dc7c2f | 5200 | if (!cb->cb_verbose) |
193a37cb | 5201 | return (ret); |
34dc7c2f BB |
5202 | |
5203 | if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, | |
5204 | &newchild, &children) != 0) | |
193a37cb | 5205 | return (ret); |
34dc7c2f | 5206 | |
674b8934 TH |
5207 | if (oldnv) { |
5208 | if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, | |
5209 | &oldchild, &oldchildren) != 0) | |
5210 | return (ret); | |
5211 | ||
5212 | children = MIN(oldchildren, children); | |
5213 | } | |
34dc7c2f | 5214 | |
cc99f275 DB |
5215 | /* |
5216 | * print normal top-level devices | |
5217 | */ | |
34dc7c2f | 5218 | for (c = 0; c < children; c++) { |
187632dc | 5219 | uint64_t ishole = B_FALSE, islog = B_FALSE; |
428870ff | 5220 | |
187632dc MH |
5221 | (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, |
5222 | &ishole); | |
5223 | ||
5224 | (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, | |
5225 | &islog); | |
5226 | ||
5227 | if (ishole || islog) | |
428870ff BB |
5228 | continue; |
5229 | ||
cc99f275 DB |
5230 | if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) |
5231 | continue; | |
5232 | ||
d2f3e292 | 5233 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], |
7b468ed2 | 5234 | cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID); |
193a37cb | 5235 | ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, |
34dc7c2f BB |
5236 | newchild[c], cb, depth + 2); |
5237 | free(vname); | |
5238 | } | |
5239 | ||
187632dc | 5240 | /* |
cc99f275 | 5241 | * print all other top-level devices |
187632dc | 5242 | */ |
cf5a9d8c | 5243 | for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) { |
cc99f275 | 5244 | boolean_t printed = B_FALSE; |
187632dc MH |
5245 | |
5246 | for (c = 0; c < children; c++) { | |
5247 | uint64_t islog = B_FALSE; | |
d1807f16 RY |
5248 | const char *bias = NULL; |
5249 | const char *type = NULL; | |
cc99f275 | 5250 | |
187632dc MH |
5251 | (void) nvlist_lookup_uint64(newchild[c], |
5252 | ZPOOL_CONFIG_IS_LOG, &islog); | |
187632dc | 5253 | if (islog) { |
d1807f16 | 5254 | bias = VDEV_ALLOC_CLASS_LOGS; |
cc99f275 DB |
5255 | } else { |
5256 | (void) nvlist_lookup_string(newchild[c], | |
5257 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
5258 | (void) nvlist_lookup_string(newchild[c], | |
5259 | ZPOOL_CONFIG_TYPE, &type); | |
187632dc | 5260 | } |
cc99f275 DB |
5261 | if (bias == NULL || strcmp(bias, class_name[n]) != 0) |
5262 | continue; | |
5263 | if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
5264 | continue; | |
187632dc | 5265 | |
cc99f275 DB |
5266 | if (!printed) { |
5267 | if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && | |
2a673e76 AJ |
5268 | !cb->cb_scripted && |
5269 | !cb->cb_vdevs.cb_names) { | |
cc99f275 DB |
5270 | print_iostat_dashes(cb, 0, |
5271 | class_name[n]); | |
5272 | } | |
5273 | printf("\n"); | |
5274 | printed = B_TRUE; | |
5275 | } | |
5276 | ||
5277 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], | |
7b468ed2 | 5278 | cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID); |
cc99f275 DB |
5279 | ret += print_vdev_stats(zhp, vname, oldnv ? |
5280 | oldchild[c] : NULL, newchild[c], cb, depth + 2); | |
5281 | free(vname); | |
5282 | } | |
187632dc MH |
5283 | } |
5284 | ||
34dc7c2f BB |
5285 | /* |
5286 | * Include level 2 ARC devices in iostat output | |
5287 | */ | |
5288 | if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, | |
5289 | &newchild, &children) != 0) | |
193a37cb | 5290 | return (ret); |
34dc7c2f | 5291 | |
674b8934 TH |
5292 | if (oldnv) { |
5293 | if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, | |
5294 | &oldchild, &oldchildren) != 0) | |
5295 | return (ret); | |
5296 | ||
5297 | children = MIN(oldchildren, children); | |
5298 | } | |
34dc7c2f BB |
5299 | |
5300 | if (children > 0) { | |
7e945072 | 5301 | if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted && |
2a673e76 | 5302 | !cb->cb_vdevs.cb_names) { |
193a37cb TH |
5303 | print_iostat_dashes(cb, 0, "cache"); |
5304 | } | |
5034557b | 5305 | printf("\n"); |
193a37cb | 5306 | |
34dc7c2f | 5307 | for (c = 0; c < children; c++) { |
428870ff | 5308 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], |
2a673e76 | 5309 | cb->cb_vdevs.cb_name_flags); |
193a37cb TH |
5310 | ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] |
5311 | : NULL, newchild[c], cb, depth + 2); | |
34dc7c2f BB |
5312 | free(vname); |
5313 | } | |
5314 | } | |
193a37cb TH |
5315 | |
5316 | return (ret); | |
34dc7c2f BB |
5317 | } |
5318 | ||
5319 | static int | |
5320 | refresh_iostat(zpool_handle_t *zhp, void *data) | |
5321 | { | |
5322 | iostat_cbdata_t *cb = data; | |
5323 | boolean_t missing; | |
5324 | ||
5325 | /* | |
5326 | * If the pool has disappeared, remove it from the list and continue. | |
5327 | */ | |
5328 | if (zpool_refresh_stats(zhp, &missing) != 0) | |
5329 | return (-1); | |
5330 | ||
5331 | if (missing) | |
5332 | pool_list_remove(cb->cb_list, zhp); | |
5333 | ||
5334 | return (0); | |
5335 | } | |
5336 | ||
5337 | /* | |
5338 | * Callback to print out the iostats for the given pool. | |
5339 | */ | |
65c7cc49 | 5340 | static int |
34dc7c2f BB |
5341 | print_iostat(zpool_handle_t *zhp, void *data) |
5342 | { | |
5343 | iostat_cbdata_t *cb = data; | |
5344 | nvlist_t *oldconfig, *newconfig; | |
5345 | nvlist_t *oldnvroot, *newnvroot; | |
193a37cb | 5346 | int ret; |
34dc7c2f BB |
5347 | |
5348 | newconfig = zpool_get_config(zhp, &oldconfig); | |
5349 | ||
5350 | if (cb->cb_iteration == 1) | |
5351 | oldconfig = NULL; | |
5352 | ||
5353 | verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, | |
5354 | &newnvroot) == 0); | |
5355 | ||
5356 | if (oldconfig == NULL) | |
5357 | oldnvroot = NULL; | |
5358 | else | |
5359 | verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, | |
5360 | &oldnvroot) == 0); | |
5361 | ||
193a37cb | 5362 | ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, |
02730c33 | 5363 | cb, 0); |
7e945072 | 5364 | if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) && |
2a673e76 AJ |
5365 | !cb->cb_scripted && cb->cb_verbose && |
5366 | !cb->cb_vdevs.cb_names_count) { | |
7e945072 | 5367 | print_iostat_separator(cb); |
d6418de0 TH |
5368 | if (cb->vcdl != NULL) { |
5369 | print_cmd_columns(cb->vcdl, 1); | |
5370 | } | |
5371 | printf("\n"); | |
7e945072 | 5372 | } |
34dc7c2f | 5373 | |
193a37cb | 5374 | return (ret); |
34dc7c2f BB |
5375 | } |
5376 | ||
9fc60702 CS |
5377 | static int |
5378 | get_columns(void) | |
5379 | { | |
5380 | struct winsize ws; | |
5381 | int columns = 80; | |
5382 | int error; | |
5383 | ||
5384 | if (isatty(STDOUT_FILENO)) { | |
5385 | error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); | |
5386 | if (error == 0) | |
5387 | columns = ws.ws_col; | |
5388 | } else { | |
5389 | columns = 999; | |
5390 | } | |
5391 | ||
d1d7e268 | 5392 | return (columns); |
9fc60702 CS |
5393 | } |
5394 | ||
c5eea0ab BB |
5395 | /* |
5396 | * Return the required length of the pool/vdev name column. The minimum | |
5397 | * allowed width and output formatting flags must be provided. | |
5398 | */ | |
5399 | static int | |
5400 | get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose) | |
34dc7c2f | 5401 | { |
34dc7c2f | 5402 | nvlist_t *config, *nvroot; |
c5eea0ab | 5403 | int width = min_width; |
34dc7c2f BB |
5404 | |
5405 | if ((config = zpool_get_config(zhp, NULL)) != NULL) { | |
5406 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
5407 | &nvroot) == 0); | |
06b80506 | 5408 | size_t poolname_len = strlen(zpool_get_name(zhp)); |
c5eea0ab BB |
5409 | if (verbose == B_FALSE) { |
5410 | width = MAX(poolname_len, min_width); | |
5411 | } else { | |
5412 | width = MAX(poolname_len, | |
5413 | max_width(zhp, nvroot, 0, min_width, flags)); | |
5414 | } | |
34dc7c2f | 5415 | } |
9fc60702 | 5416 | |
c5eea0ab | 5417 | return (width); |
34dc7c2f BB |
5418 | } |
5419 | ||
5420 | /* | |
428870ff | 5421 | * Parse the input string, get the 'interval' and 'count' value if there is one. |
34dc7c2f | 5422 | */ |
428870ff | 5423 | static void |
193a37cb | 5424 | get_interval_count(int *argcp, char **argv, float *iv, |
428870ff | 5425 | unsigned long *cnt) |
34dc7c2f | 5426 | { |
193a37cb TH |
5427 | float interval = 0; |
5428 | unsigned long count = 0; | |
1fde1e37 | 5429 | int argc = *argcp; |
34dc7c2f BB |
5430 | |
5431 | /* | |
5432 | * Determine if the last argument is an integer or a pool name | |
5433 | */ | |
3283f137 | 5434 | if (argc > 0 && zfs_isnumber(argv[argc - 1])) { |
34dc7c2f BB |
5435 | char *end; |
5436 | ||
5437 | errno = 0; | |
193a37cb | 5438 | interval = strtof(argv[argc - 1], &end); |
34dc7c2f BB |
5439 | |
5440 | if (*end == '\0' && errno == 0) { | |
5441 | if (interval == 0) { | |
61b50107 AZ |
5442 | (void) fprintf(stderr, gettext( |
5443 | "interval cannot be zero\n")); | |
34dc7c2f BB |
5444 | usage(B_FALSE); |
5445 | } | |
34dc7c2f BB |
5446 | /* |
5447 | * Ignore the last parameter | |
5448 | */ | |
5449 | argc--; | |
5450 | } else { | |
5451 | /* | |
5452 | * If this is not a valid number, just plow on. The | |
5453 | * user will get a more informative error message later | |
5454 | * on. | |
5455 | */ | |
5456 | interval = 0; | |
5457 | } | |
5458 | } | |
5459 | ||
5460 | /* | |
5461 | * If the last argument is also an integer, then we have both a count | |
428870ff | 5462 | * and an interval. |
34dc7c2f | 5463 | */ |
3283f137 | 5464 | if (argc > 0 && zfs_isnumber(argv[argc - 1])) { |
34dc7c2f BB |
5465 | char *end; |
5466 | ||
5467 | errno = 0; | |
5468 | count = interval; | |
193a37cb | 5469 | interval = strtof(argv[argc - 1], &end); |
34dc7c2f BB |
5470 | |
5471 | if (*end == '\0' && errno == 0) { | |
5472 | if (interval == 0) { | |
61b50107 AZ |
5473 | (void) fprintf(stderr, gettext( |
5474 | "interval cannot be zero\n")); | |
34dc7c2f BB |
5475 | usage(B_FALSE); |
5476 | } | |
5477 | ||
5478 | /* | |
5479 | * Ignore the last parameter | |
5480 | */ | |
5481 | argc--; | |
5482 | } else { | |
5483 | interval = 0; | |
5484 | } | |
5485 | } | |
5486 | ||
428870ff BB |
5487 | *iv = interval; |
5488 | *cnt = count; | |
5489 | *argcp = argc; | |
5490 | } | |
5491 | ||
5492 | static void | |
5493 | get_timestamp_arg(char c) | |
5494 | { | |
5495 | if (c == 'u') | |
5496 | timestamp_fmt = UDATE; | |
5497 | else if (c == 'd') | |
5498 | timestamp_fmt = DDATE; | |
5499 | else | |
5500 | usage(B_FALSE); | |
5501 | } | |
5502 | ||
5503 | /* | |
193a37cb TH |
5504 | * Return stat flags that are supported by all pools by both the module and |
5505 | * zpool iostat. "*data" should be initialized to all 0xFFs before running. | |
5506 | * It will get ANDed down until only the flags that are supported on all pools | |
5507 | * remain. | |
5508 | */ | |
5509 | static int | |
5510 | get_stat_flags_cb(zpool_handle_t *zhp, void *data) | |
5511 | { | |
5512 | uint64_t *mask = data; | |
5513 | nvlist_t *config, *nvroot, *nvx; | |
5514 | uint64_t flags = 0; | |
5515 | int i, j; | |
5516 | ||
193a37cb TH |
5517 | config = zpool_get_config(zhp, NULL); |
5518 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
5519 | &nvroot) == 0); | |
5520 | ||
5521 | /* Default stats are always supported, but for completeness.. */ | |
5522 | if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS)) | |
5523 | flags |= IOS_DEFAULT_M; | |
5524 | ||
5525 | /* Get our extended stats nvlist from the main list */ | |
5526 | if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX, | |
5527 | &nvx) != 0) { | |
5528 | /* | |
5529 | * No extended stats; they're probably running an older | |
5530 | * module. No big deal, we support that too. | |
5531 | */ | |
5532 | goto end; | |
5533 | } | |
5534 | ||
5535 | /* For each extended stat, make sure all its nvpairs are supported */ | |
5536 | for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) { | |
5537 | if (!vsx_type_to_nvlist[j][0]) | |
5538 | continue; | |
5539 | ||
5540 | /* Start off by assuming the flag is supported, then check */ | |
5541 | flags |= (1ULL << j); | |
5542 | for (i = 0; vsx_type_to_nvlist[j][i]; i++) { | |
5543 | if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) { | |
5544 | /* flag isn't supported */ | |
5545 | flags = flags & ~(1ULL << j); | |
5546 | break; | |
5547 | } | |
5548 | } | |
5549 | } | |
5550 | end: | |
5551 | *mask = *mask & flags; | |
5552 | return (0); | |
5553 | } | |
5554 | ||
5555 | /* | |
5556 | * Return a bitmask of stats that are supported on all pools by both the module | |
5557 | * and zpool iostat. | |
5558 | */ | |
5559 | static uint64_t | |
5560 | get_stat_flags(zpool_list_t *list) | |
5561 | { | |
5562 | uint64_t mask = -1; | |
5563 | ||
5564 | /* | |
5565 | * get_stat_flags_cb() will lop off bits from "mask" until only the | |
5566 | * flags that are supported on all pools remain. | |
5567 | */ | |
5568 | pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask); | |
5569 | return (mask); | |
5570 | } | |
5571 | ||
5572 | /* | |
2a673e76 | 5573 | * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise. |
193a37cb TH |
5574 | */ |
5575 | static int | |
2a8430a2 | 5576 | is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data) |
193a37cb | 5577 | { |
6f2ffd27 | 5578 | uint64_t guid; |
2a673e76 | 5579 | vdev_cbdata_t *cb = cb_data; |
2a8430a2 | 5580 | zpool_handle_t *zhp = zhp_data; |
193a37cb | 5581 | |
6f2ffd27 RW |
5582 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) |
5583 | return (0); | |
193a37cb | 5584 | |
6f2ffd27 | 5585 | return (guid == zpool_vdev_path_to_guid(zhp, cb->cb_names[0])); |
193a37cb TH |
5586 | } |
5587 | ||
5588 | /* | |
2a673e76 | 5589 | * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise. |
193a37cb TH |
5590 | */ |
5591 | static int | |
5592 | is_vdev(zpool_handle_t *zhp, void *cb_data) | |
5593 | { | |
5594 | return (for_each_vdev(zhp, is_vdev_cb, cb_data)); | |
5595 | } | |
5596 | ||
5597 | /* | |
5598 | * Check if vdevs are in a pool | |
5599 | * | |
5600 | * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise | |
5601 | * return 0. If pool_name is NULL, then search all pools. | |
5602 | */ | |
5603 | static int | |
5604 | are_vdevs_in_pool(int argc, char **argv, char *pool_name, | |
2a673e76 | 5605 | vdev_cbdata_t *cb) |
193a37cb TH |
5606 | { |
5607 | char **tmp_name; | |
5608 | int ret = 0; | |
5609 | int i; | |
5610 | int pool_count = 0; | |
5611 | ||
5612 | if ((argc == 0) || !*argv) | |
5613 | return (0); | |
5614 | ||
5615 | if (pool_name) | |
5616 | pool_count = 1; | |
5617 | ||
2a673e76 AJ |
5618 | /* Temporarily hijack cb_names for a second... */ |
5619 | tmp_name = cb->cb_names; | |
193a37cb TH |
5620 | |
5621 | /* Go though our list of prospective vdev names */ | |
5622 | for (i = 0; i < argc; i++) { | |
2a673e76 | 5623 | cb->cb_names = argv + i; |
193a37cb TH |
5624 | |
5625 | /* Is this name a vdev in our pools? */ | |
5626 | ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL, | |
2a673e76 | 5627 | ZFS_TYPE_POOL, B_FALSE, is_vdev, cb); |
193a37cb TH |
5628 | if (!ret) { |
5629 | /* No match */ | |
5630 | break; | |
5631 | } | |
5632 | } | |
5633 | ||
2a673e76 | 5634 | cb->cb_names = tmp_name; |
193a37cb TH |
5635 | |
5636 | return (ret); | |
5637 | } | |
5638 | ||
5639 | static int | |
5640 | is_pool_cb(zpool_handle_t *zhp, void *data) | |
5641 | { | |
5642 | char *name = data; | |
5643 | if (strcmp(name, zpool_get_name(zhp)) == 0) | |
5644 | return (1); | |
5645 | ||
5646 | return (0); | |
5647 | } | |
5648 | ||
5649 | /* | |
5650 | * Do we have a pool named *name? If so, return 1, otherwise 0. | |
5651 | */ | |
5652 | static int | |
5653 | is_pool(char *name) | |
5654 | { | |
2a673e76 AJ |
5655 | return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE, |
5656 | is_pool_cb, name)); | |
193a37cb TH |
5657 | } |
5658 | ||
5659 | /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */ | |
5660 | static int | |
4ea3f864 GM |
5661 | are_all_pools(int argc, char **argv) |
5662 | { | |
193a37cb TH |
5663 | if ((argc == 0) || !*argv) |
5664 | return (0); | |
5665 | ||
5666 | while (--argc >= 0) | |
5667 | if (!is_pool(argv[argc])) | |
5668 | return (0); | |
5669 | ||
5670 | return (1); | |
5671 | } | |
5672 | ||
5673 | /* | |
5674 | * Helper function to print out vdev/pool names we can't resolve. Used for an | |
5675 | * error message. | |
5676 | */ | |
5677 | static void | |
5678 | error_list_unresolved_vdevs(int argc, char **argv, char *pool_name, | |
2a673e76 | 5679 | vdev_cbdata_t *cb) |
193a37cb TH |
5680 | { |
5681 | int i; | |
5682 | char *name; | |
5683 | char *str; | |
5684 | for (i = 0; i < argc; i++) { | |
5685 | name = argv[i]; | |
5686 | ||
5687 | if (is_pool(name)) | |
5688 | str = gettext("pool"); | |
5689 | else if (are_vdevs_in_pool(1, &name, pool_name, cb)) | |
5690 | str = gettext("vdev in this pool"); | |
5691 | else if (are_vdevs_in_pool(1, &name, NULL, cb)) | |
5692 | str = gettext("vdev in another pool"); | |
5693 | else | |
5694 | str = gettext("unknown"); | |
5695 | ||
5696 | fprintf(stderr, "\t%s (%s)\n", name, str); | |
5697 | } | |
5698 | } | |
5699 | ||
5700 | /* | |
5701 | * Same as get_interval_count(), but with additional checks to not misinterpret | |
5702 | * guids as interval/count values. Assumes VDEV_NAME_GUID is set in | |
2a673e76 | 5703 | * cb.cb_vdevs.cb_name_flags. |
193a37cb TH |
5704 | */ |
5705 | static void | |
5706 | get_interval_count_filter_guids(int *argc, char **argv, float *interval, | |
5707 | unsigned long *count, iostat_cbdata_t *cb) | |
5708 | { | |
5709 | char **tmpargv = argv; | |
5710 | int argc_for_interval = 0; | |
5711 | ||
5712 | /* Is the last arg an interval value? Or a guid? */ | |
2a673e76 AJ |
5713 | if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, |
5714 | &cb->cb_vdevs)) { | |
193a37cb TH |
5715 | /* |
5716 | * The last arg is not a guid, so it's probably an | |
5717 | * interval value. | |
5718 | */ | |
5719 | argc_for_interval++; | |
5720 | ||
5721 | if (*argc >= 2 && | |
2a673e76 AJ |
5722 | !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, |
5723 | &cb->cb_vdevs)) { | |
193a37cb TH |
5724 | /* |
5725 | * The 2nd to last arg is not a guid, so it's probably | |
5726 | * an interval value. | |
5727 | */ | |
5728 | argc_for_interval++; | |
5729 | } | |
5730 | } | |
5731 | ||
5732 | /* Point to our list of possible intervals */ | |
5733 | tmpargv = &argv[*argc - argc_for_interval]; | |
5734 | ||
5735 | *argc = *argc - argc_for_interval; | |
5736 | get_interval_count(&argc_for_interval, tmpargv, | |
5737 | interval, count); | |
5738 | } | |
5739 | ||
e60e158e JG |
5740 | /* |
5741 | * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or | |
5742 | * if we were unable to determine its size. | |
5743 | */ | |
5744 | static int | |
5745 | terminal_height(void) | |
5746 | { | |
5747 | struct winsize win; | |
5748 | ||
5749 | if (isatty(STDOUT_FILENO) == 0) | |
5750 | return (-1); | |
5751 | ||
5752 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0) | |
5753 | return (win.ws_row); | |
5754 | ||
5755 | return (-1); | |
5756 | } | |
5757 | ||
d6418de0 TH |
5758 | /* |
5759 | * Run one of the zpool status/iostat -c scripts with the help (-h) option and | |
5760 | * print the result. | |
5761 | * | |
5762 | * name: Short name of the script ('iostat'). | |
5763 | * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat'); | |
5764 | */ | |
5765 | static void | |
5766 | print_zpool_script_help(char *name, char *path) | |
5767 | { | |
a926aab9 | 5768 | char *argv[] = {path, (char *)"-h", NULL}; |
d6418de0 TH |
5769 | char **lines = NULL; |
5770 | int lines_cnt = 0; | |
5771 | int rc; | |
5772 | ||
5773 | rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines, | |
5774 | &lines_cnt); | |
3e2ecae0 GDN |
5775 | if (rc != 0 || lines == NULL || lines_cnt <= 0) { |
5776 | if (lines != NULL) | |
5777 | libzfs_free_str_array(lines, lines_cnt); | |
d6418de0 | 5778 | return; |
3e2ecae0 | 5779 | } |
d6418de0 TH |
5780 | |
5781 | for (int i = 0; i < lines_cnt; i++) | |
5782 | if (!is_blank_str(lines[i])) | |
5783 | printf(" %-14s %s\n", name, lines[i]); | |
5784 | ||
5785 | libzfs_free_str_array(lines, lines_cnt); | |
5786 | } | |
5787 | ||
193a37cb | 5788 | /* |
099700d9 | 5789 | * Go though the zpool status/iostat -c scripts in the user's path, run their |
d6418de0 TH |
5790 | * help option (-h), and print out the results. |
5791 | */ | |
5792 | static void | |
099700d9 | 5793 | print_zpool_dir_scripts(char *dirpath) |
d6418de0 TH |
5794 | { |
5795 | DIR *dir; | |
5796 | struct dirent *ent; | |
5797 | char fullpath[MAXPATHLEN]; | |
5798 | struct stat dir_stat; | |
5799 | ||
099700d9 | 5800 | if ((dir = opendir(dirpath)) != NULL) { |
d6418de0 TH |
5801 | /* print all the files and directories within directory */ |
5802 | while ((ent = readdir(dir)) != NULL) { | |
ba87ed14 RY |
5803 | if (snprintf(fullpath, sizeof (fullpath), "%s/%s", |
5804 | dirpath, ent->d_name) >= sizeof (fullpath)) { | |
5805 | (void) fprintf(stderr, | |
5806 | gettext("internal error: " | |
5807 | "ZPOOL_SCRIPTS_PATH too large.\n")); | |
5808 | exit(1); | |
5809 | } | |
d6418de0 TH |
5810 | |
5811 | /* Print the scripts */ | |
5812 | if (stat(fullpath, &dir_stat) == 0) | |
5813 | if (dir_stat.st_mode & S_IXUSR && | |
5814 | S_ISREG(dir_stat.st_mode)) | |
5815 | print_zpool_script_help(ent->d_name, | |
5816 | fullpath); | |
5817 | } | |
d6418de0 | 5818 | closedir(dir); |
d6418de0 TH |
5819 | } |
5820 | } | |
5821 | ||
099700d9 GDN |
5822 | /* |
5823 | * Print out help text for all zpool status/iostat -c scripts. | |
5824 | */ | |
5825 | static void | |
a926aab9 | 5826 | print_zpool_script_list(const char *subcommand) |
099700d9 | 5827 | { |
1ce6d70c | 5828 | char *dir, *sp, *tmp; |
099700d9 GDN |
5829 | |
5830 | printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand); | |
5831 | ||
5832 | sp = zpool_get_cmd_search_path(); | |
5833 | if (sp == NULL) | |
5834 | return; | |
5835 | ||
1ce6d70c AZ |
5836 | for (dir = strtok_r(sp, ":", &tmp); |
5837 | dir != NULL; | |
5838 | dir = strtok_r(NULL, ":", &tmp)) | |
099700d9 | 5839 | print_zpool_dir_scripts(dir); |
099700d9 GDN |
5840 | |
5841 | free(sp); | |
5842 | } | |
5843 | ||
c5eea0ab BB |
5844 | /* |
5845 | * Set the minimum pool/vdev name column width. The width must be at least 10, | |
5846 | * but may be as large as the column width - 42 so it still fits on one line. | |
a36bad17 | 5847 | * NOTE: 42 is the width of the default capacity/operations/bandwidth output |
c5eea0ab BB |
5848 | */ |
5849 | static int | |
5850 | get_namewidth_iostat(zpool_handle_t *zhp, void *data) | |
5851 | { | |
5852 | iostat_cbdata_t *cb = data; | |
a36bad17 | 5853 | int width, available_width; |
c5eea0ab | 5854 | |
a36bad17 PP |
5855 | /* |
5856 | * get_namewidth() returns the maximum width of any name in that column | |
5857 | * for any pool/vdev/device line that will be output. | |
5858 | */ | |
7c0e3941 SW |
5859 | width = get_namewidth(zhp, cb->cb_namewidth, |
5860 | cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose); | |
c5eea0ab | 5861 | |
a36bad17 PP |
5862 | /* |
5863 | * The width we are calculating is the width of the header and also the | |
5864 | * padding width for names that are less than maximum width. The stats | |
5865 | * take up 42 characters, so the width available for names is: | |
5866 | */ | |
5867 | available_width = get_columns() - 42; | |
5868 | ||
5869 | /* | |
5870 | * If the maximum width fits on a screen, then great! Make everything | |
5871 | * line up by justifying all lines to the same width. If that max | |
5872 | * width is larger than what's available, the name plus stats won't fit | |
5873 | * on one line, and justifying to that width would cause every line to | |
5874 | * wrap on the screen. We only want lines with long names to wrap. | |
5875 | * Limit the padding to what won't wrap. | |
5876 | */ | |
5877 | if (width > available_width) | |
5878 | width = available_width; | |
5879 | ||
5880 | /* | |
5881 | * And regardless of whatever the screen width is (get_columns can | |
5882 | * return 0 if the width is not known or less than 42 for a narrow | |
5883 | * terminal) have the width be a minimum of 10. | |
5884 | */ | |
c5eea0ab BB |
5885 | if (width < 10) |
5886 | width = 10; | |
c5eea0ab | 5887 | |
a36bad17 | 5888 | /* Save the calculated width */ |
c5eea0ab BB |
5889 | cb->cb_namewidth = width; |
5890 | ||
5891 | return (0); | |
5892 | } | |
5893 | ||
d6418de0 TH |
5894 | /* |
5895 | * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name] | |
5896 | * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]] | |
5897 | * [interval [count]] | |
428870ff | 5898 | * |
8720e9e7 | 5899 | * -c CMD For each vdev, run command CMD |
d2f3e292 RY |
5900 | * -g Display guid for individual vdev name. |
5901 | * -L Follow links when resolving vdev path name. | |
a77f29f9 | 5902 | * -P Display full path for vdev name. |
428870ff | 5903 | * -v Display statistics for individual vdevs |
193a37cb TH |
5904 | * -h Display help |
5905 | * -p Display values in parsable (exact) format. | |
5906 | * -H Scripted mode. Don't display headers, and separate properties | |
5907 | * by a single tab. | |
5908 | * -l Display average latency | |
5909 | * -q Display queue depths | |
7e945072 TH |
5910 | * -w Display latency histograms |
5911 | * -r Display request size histogram | |
428870ff | 5912 | * -T Display a timestamp in date(1) or Unix format |
8fccfa8e | 5913 | * -n Only print headers once |
428870ff BB |
5914 | * |
5915 | * This command can be tricky because we want to be able to deal with pool | |
5916 | * creation/destruction as well as vdev configuration changes. The bulk of this | |
5917 | * processing is handled by the pool_list_* routines in zpool_iter.c. We rely | |
5918 | * on pool_list_update() to detect the addition of new pools. Configuration | |
5919 | * changes are all handled within libzfs. | |
5920 | */ | |
5921 | int | |
5922 | zpool_do_iostat(int argc, char **argv) | |
5923 | { | |
5924 | int c; | |
5925 | int ret; | |
5926 | int npools; | |
193a37cb TH |
5927 | float interval = 0; |
5928 | unsigned long count = 0; | |
8fccfa8e | 5929 | int winheight = 24; |
428870ff BB |
5930 | zpool_list_t *list; |
5931 | boolean_t verbose = B_FALSE; | |
7e945072 | 5932 | boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE; |
3491d6eb | 5933 | boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE; |
41092124 | 5934 | boolean_t omit_since_boot = B_FALSE; |
d2f3e292 RY |
5935 | boolean_t guid = B_FALSE; |
5936 | boolean_t follow_links = B_FALSE; | |
5937 | boolean_t full_name = B_FALSE; | |
8fccfa8e | 5938 | boolean_t headers_once = B_FALSE; |
d2f3e292 | 5939 | iostat_cbdata_t cb = { 0 }; |
8720e9e7 | 5940 | char *cmd = NULL; |
428870ff | 5941 | |
193a37cb TH |
5942 | /* Used for printing error message */ |
5943 | const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q', | |
7e945072 | 5944 | [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'}; |
193a37cb TH |
5945 | |
5946 | uint64_t unsupported_flags; | |
5947 | ||
428870ff | 5948 | /* check options */ |
8fccfa8e | 5949 | while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) { |
428870ff | 5950 | switch (c) { |
8720e9e7 | 5951 | case 'c': |
d6418de0 TH |
5952 | if (cmd != NULL) { |
5953 | fprintf(stderr, | |
5954 | gettext("Can't set -c flag twice\n")); | |
5955 | exit(1); | |
5956 | } | |
099700d9 GDN |
5957 | |
5958 | if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && | |
5959 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { | |
5960 | fprintf(stderr, gettext( | |
5961 | "Can't run -c, disabled by " | |
5962 | "ZPOOL_SCRIPTS_ENABLED.\n")); | |
5963 | exit(1); | |
5964 | } | |
5965 | ||
d6418de0 TH |
5966 | if ((getuid() <= 0 || geteuid() <= 0) && |
5967 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { | |
5968 | fprintf(stderr, gettext( | |
5969 | "Can't run -c with root privileges " | |
5970 | "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); | |
5971 | exit(1); | |
5972 | } | |
8720e9e7 | 5973 | cmd = optarg; |
d6418de0 | 5974 | verbose = B_TRUE; |
8720e9e7 | 5975 | break; |
d2f3e292 RY |
5976 | case 'g': |
5977 | guid = B_TRUE; | |
5978 | break; | |
5979 | case 'L': | |
5980 | follow_links = B_TRUE; | |
5981 | break; | |
a77f29f9 | 5982 | case 'P': |
d2f3e292 RY |
5983 | full_name = B_TRUE; |
5984 | break; | |
428870ff BB |
5985 | case 'T': |
5986 | get_timestamp_arg(*optarg); | |
5987 | break; | |
5988 | case 'v': | |
5989 | verbose = B_TRUE; | |
5990 | break; | |
193a37cb | 5991 | case 'p': |
3491d6eb | 5992 | parsable = B_TRUE; |
193a37cb TH |
5993 | break; |
5994 | case 'l': | |
5995 | latency = B_TRUE; | |
5996 | break; | |
5997 | case 'q': | |
5998 | queues = B_TRUE; | |
5999 | break; | |
6000 | case 'H': | |
6001 | scripted = B_TRUE; | |
6002 | break; | |
6003 | case 'w': | |
7e945072 TH |
6004 | l_histo = B_TRUE; |
6005 | break; | |
6006 | case 'r': | |
6007 | rq_histo = B_TRUE; | |
193a37cb | 6008 | break; |
41092124 HM |
6009 | case 'y': |
6010 | omit_since_boot = B_TRUE; | |
6011 | break; | |
8fccfa8e DW |
6012 | case 'n': |
6013 | headers_once = B_TRUE; | |
6014 | break; | |
193a37cb TH |
6015 | case 'h': |
6016 | usage(B_FALSE); | |
6017 | break; | |
428870ff | 6018 | case '?': |
1528bfdb | 6019 | if (optopt == 'c') { |
099700d9 | 6020 | print_zpool_script_list("iostat"); |
d6418de0 | 6021 | exit(0); |
1528bfdb TH |
6022 | } else { |
6023 | fprintf(stderr, | |
6024 | gettext("invalid option '%c'\n"), optopt); | |
6025 | } | |
428870ff BB |
6026 | usage(B_FALSE); |
6027 | } | |
6028 | } | |
6029 | ||
6030 | argc -= optind; | |
6031 | argv += optind; | |
6032 | ||
3491d6eb | 6033 | cb.cb_literal = parsable; |
193a37cb TH |
6034 | cb.cb_scripted = scripted; |
6035 | ||
6036 | if (guid) | |
2a673e76 | 6037 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID; |
193a37cb | 6038 | if (follow_links) |
2a673e76 | 6039 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; |
193a37cb | 6040 | if (full_name) |
2a673e76 | 6041 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH; |
193a37cb TH |
6042 | cb.cb_iteration = 0; |
6043 | cb.cb_namewidth = 0; | |
6044 | cb.cb_verbose = verbose; | |
6045 | ||
6046 | /* Get our interval and count values (if any) */ | |
6047 | if (guid) { | |
6048 | get_interval_count_filter_guids(&argc, argv, &interval, | |
6049 | &count, &cb); | |
6050 | } else { | |
6051 | get_interval_count(&argc, argv, &interval, &count); | |
6052 | } | |
6053 | ||
6054 | if (argc == 0) { | |
6055 | /* No args, so just print the defaults. */ | |
6056 | } else if (are_all_pools(argc, argv)) { | |
6057 | /* All the args are pool names */ | |
2a673e76 | 6058 | } else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) { |
193a37cb | 6059 | /* All the args are vdevs */ |
2a673e76 AJ |
6060 | cb.cb_vdevs.cb_names = argv; |
6061 | cb.cb_vdevs.cb_names_count = argc; | |
193a37cb TH |
6062 | argc = 0; /* No pools to process */ |
6063 | } else if (are_all_pools(1, argv)) { | |
6064 | /* The first arg is a pool name */ | |
2a673e76 AJ |
6065 | if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], |
6066 | &cb.cb_vdevs)) { | |
193a37cb | 6067 | /* ...and the rest are vdev names */ |
2a673e76 AJ |
6068 | cb.cb_vdevs.cb_names = argv + 1; |
6069 | cb.cb_vdevs.cb_names_count = argc - 1; | |
193a37cb TH |
6070 | argc = 1; /* One pool to process */ |
6071 | } else { | |
6072 | fprintf(stderr, gettext("Expected either a list of ")); | |
6073 | fprintf(stderr, gettext("pools, or list of vdevs in")); | |
6074 | fprintf(stderr, " \"%s\", ", argv[0]); | |
6075 | fprintf(stderr, gettext("but got:\n")); | |
6076 | error_list_unresolved_vdevs(argc - 1, argv + 1, | |
2a673e76 | 6077 | argv[0], &cb.cb_vdevs); |
193a37cb TH |
6078 | fprintf(stderr, "\n"); |
6079 | usage(B_FALSE); | |
6080 | return (1); | |
6081 | } | |
6082 | } else { | |
6083 | /* | |
6084 | * The args don't make sense. The first arg isn't a pool name, | |
6085 | * nor are all the args vdevs. | |
6086 | */ | |
6087 | fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n")); | |
6088 | fprintf(stderr, "\n"); | |
6089 | return (1); | |
6090 | } | |
6091 | ||
2a673e76 | 6092 | if (cb.cb_vdevs.cb_names_count != 0) { |
193a37cb TH |
6093 | /* |
6094 | * If user specified vdevs, it implies verbose. | |
6095 | */ | |
6096 | cb.cb_verbose = B_TRUE; | |
6097 | } | |
428870ff | 6098 | |
34dc7c2f BB |
6099 | /* |
6100 | * Construct the list of all interesting pools. | |
6101 | */ | |
6102 | ret = 0; | |
2a673e76 AJ |
6103 | if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable, |
6104 | &ret)) == NULL) | |
34dc7c2f BB |
6105 | return (1); |
6106 | ||
6107 | if (pool_list_count(list) == 0 && argc != 0) { | |
6108 | pool_list_free(list); | |
6109 | return (1); | |
6110 | } | |
6111 | ||
6112 | if (pool_list_count(list) == 0 && interval == 0) { | |
6113 | pool_list_free(list); | |
6114 | (void) fprintf(stderr, gettext("no pools available\n")); | |
6115 | return (1); | |
6116 | } | |
6117 | ||
d6418de0 | 6118 | if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) { |
7e945072 TH |
6119 | pool_list_free(list); |
6120 | (void) fprintf(stderr, | |
d6418de0 | 6121 | gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n")); |
7e945072 TH |
6122 | usage(B_FALSE); |
6123 | return (1); | |
6124 | } | |
6125 | ||
6126 | if (l_histo && rq_histo) { | |
193a37cb TH |
6127 | pool_list_free(list); |
6128 | (void) fprintf(stderr, | |
7e945072 | 6129 | gettext("Only one of [-r|-w] can be passed at a time\n")); |
193a37cb TH |
6130 | usage(B_FALSE); |
6131 | return (1); | |
6132 | } | |
6133 | ||
34dc7c2f BB |
6134 | /* |
6135 | * Enter the main iostat loop. | |
6136 | */ | |
6137 | cb.cb_list = list; | |
193a37cb | 6138 | |
7e945072 | 6139 | if (l_histo) { |
193a37cb TH |
6140 | /* |
6141 | * Histograms tables look out of place when you try to display | |
6142 | * them with the other stats, so make a rule that you can only | |
6143 | * print histograms by themselves. | |
6144 | */ | |
6145 | cb.cb_flags = IOS_L_HISTO_M; | |
7e945072 TH |
6146 | } else if (rq_histo) { |
6147 | cb.cb_flags = IOS_RQ_HISTO_M; | |
193a37cb TH |
6148 | } else { |
6149 | cb.cb_flags = IOS_DEFAULT_M; | |
6150 | if (latency) | |
6151 | cb.cb_flags |= IOS_LATENCY_M; | |
6152 | if (queues) | |
6153 | cb.cb_flags |= IOS_QUEUES_M; | |
6154 | } | |
6155 | ||
6156 | /* | |
6157 | * See if the module supports all the stats we want to display. | |
6158 | */ | |
6159 | unsupported_flags = cb.cb_flags & ~get_stat_flags(list); | |
6160 | if (unsupported_flags) { | |
6161 | uint64_t f; | |
6162 | int idx; | |
6163 | fprintf(stderr, | |
6164 | gettext("The loaded zfs module doesn't support:")); | |
6165 | ||
6166 | /* for each bit set in unsupported_flags */ | |
6167 | for (f = unsupported_flags; f; f &= ~(1ULL << idx)) { | |
6168 | idx = lowbit64(f) - 1; | |
6169 | fprintf(stderr, " -%c", flag_to_arg[idx]); | |
6170 | } | |
6171 | ||
02730c33 | 6172 | fprintf(stderr, ". Try running a newer module.\n"); |
193a37cb TH |
6173 | pool_list_free(list); |
6174 | ||
6175 | return (1); | |
6176 | } | |
6177 | ||
34dc7c2f | 6178 | for (;;) { |
34dc7c2f | 6179 | if ((npools = pool_list_count(list)) == 0) |
42cb3819 | 6180 | (void) fprintf(stderr, gettext("no pools available\n")); |
5a521059 | 6181 | else { |
41092124 HM |
6182 | /* |
6183 | * If this is the first iteration and -y was supplied | |
6184 | * we skip any printing. | |
6185 | */ | |
6186 | boolean_t skip = (omit_since_boot && | |
193a37cb | 6187 | cb.cb_iteration == 0); |
41092124 | 6188 | |
5a521059 PJ |
6189 | /* |
6190 | * Refresh all statistics. This is done as an | |
6191 | * explicit step before calculating the maximum name | |
6192 | * width, so that any * configuration changes are | |
6193 | * properly accounted for. | |
6194 | */ | |
6195 | (void) pool_list_iter(list, B_FALSE, refresh_iostat, | |
193a37cb | 6196 | &cb); |
34dc7c2f | 6197 | |
5a521059 PJ |
6198 | /* |
6199 | * Iterate over all pools to determine the maximum width | |
6200 | * for the pool / device name column across all pools. | |
6201 | */ | |
6202 | cb.cb_namewidth = 0; | |
c5eea0ab BB |
6203 | (void) pool_list_iter(list, B_FALSE, |
6204 | get_namewidth_iostat, &cb); | |
34dc7c2f | 6205 | |
5a521059 PJ |
6206 | if (timestamp_fmt != NODATE) |
6207 | print_timestamp(timestamp_fmt); | |
428870ff | 6208 | |
d6418de0 TH |
6209 | if (cmd != NULL && cb.cb_verbose && |
6210 | !(cb.cb_flags & IOS_ANYHISTO_M)) { | |
6211 | cb.vcdl = all_pools_for_each_vdev_run(argc, | |
2a673e76 AJ |
6212 | argv, cmd, g_zfs, cb.cb_vdevs.cb_names, |
6213 | cb.cb_vdevs.cb_names_count, | |
6214 | cb.cb_vdevs.cb_name_flags); | |
d6418de0 TH |
6215 | } else { |
6216 | cb.vcdl = NULL; | |
6217 | } | |
6218 | ||
8fccfa8e DW |
6219 | |
6220 | /* | |
6221 | * Check terminal size so we can print headers | |
6222 | * even when terminal window has its height | |
6223 | * changed. | |
6224 | */ | |
e60e158e JG |
6225 | winheight = terminal_height(); |
6226 | /* | |
6227 | * Are we connected to TTY? If not, headers_once | |
6228 | * should be true, to avoid breaking scripts. | |
6229 | */ | |
6230 | if (winheight < 0) | |
6231 | headers_once = B_TRUE; | |
8fccfa8e | 6232 | |
5a521059 | 6233 | /* |
41092124 HM |
6234 | * If it's the first time and we're not skipping it, |
6235 | * or either skip or verbose mode, print the header. | |
193a37cb TH |
6236 | * |
6237 | * The histogram code explicitly prints its header on | |
6238 | * every vdev, so skip this for histograms. | |
5a521059 | 6239 | */ |
193a37cb | 6240 | if (((++cb.cb_iteration == 1 && !skip) || |
8fccfa8e DW |
6241 | (skip != verbose) || |
6242 | (!headers_once && | |
6243 | (cb.cb_iteration % winheight) == 0)) && | |
7e945072 | 6244 | (!(cb.cb_flags & IOS_ANYHISTO_M)) && |
193a37cb | 6245 | !cb.cb_scripted) |
5a521059 | 6246 | print_iostat_header(&cb); |
34dc7c2f | 6247 | |
41092124 | 6248 | if (skip) { |
5f2700ee | 6249 | (void) fflush(stdout); |
193a37cb | 6250 | (void) fsleep(interval); |
41092124 HM |
6251 | continue; |
6252 | } | |
6253 | ||
193a37cb | 6254 | pool_list_iter(list, B_FALSE, print_iostat, &cb); |
34dc7c2f | 6255 | |
5a521059 PJ |
6256 | /* |
6257 | * If there's more than one pool, and we're not in | |
6258 | * verbose mode (which prints a separator for us), | |
6259 | * then print a separator. | |
193a37cb TH |
6260 | * |
6261 | * In addition, if we're printing specific vdevs then | |
6262 | * we also want an ending separator. | |
5a521059 | 6263 | */ |
193a37cb | 6264 | if (((npools > 1 && !verbose && |
7e945072 TH |
6265 | !(cb.cb_flags & IOS_ANYHISTO_M)) || |
6266 | (!(cb.cb_flags & IOS_ANYHISTO_M) && | |
2a673e76 | 6267 | cb.cb_vdevs.cb_names_count)) && |
193a37cb | 6268 | !cb.cb_scripted) { |
5a521059 | 6269 | print_iostat_separator(&cb); |
d6418de0 TH |
6270 | if (cb.vcdl != NULL) |
6271 | print_cmd_columns(cb.vcdl, 1); | |
6272 | printf("\n"); | |
193a37cb | 6273 | } |
d6418de0 TH |
6274 | |
6275 | if (cb.vcdl != NULL) | |
6276 | free_vdev_cmd_data_list(cb.vcdl); | |
6277 | ||
5a521059 | 6278 | } |
34dc7c2f | 6279 | |
34dc7c2f BB |
6280 | if (interval == 0) |
6281 | break; | |
6282 | ||
6283 | if (count != 0 && --count == 0) | |
6284 | break; | |
6285 | ||
5f2700ee | 6286 | (void) fflush(stdout); |
193a37cb | 6287 | (void) fsleep(interval); |
34dc7c2f BB |
6288 | } |
6289 | ||
6290 | pool_list_free(list); | |
6291 | ||
6292 | return (ret); | |
6293 | } | |
6294 | ||
6295 | typedef struct list_cbdata { | |
1bd201e7 | 6296 | boolean_t cb_verbose; |
d2f3e292 | 6297 | int cb_name_flags; |
1bd201e7 | 6298 | int cb_namewidth; |
34dc7c2f | 6299 | boolean_t cb_scripted; |
34dc7c2f | 6300 | zprop_list_t *cb_proplist; |
2a8b84b7 | 6301 | boolean_t cb_literal; |
34dc7c2f BB |
6302 | } list_cbdata_t; |
6303 | ||
cc99f275 | 6304 | |
34dc7c2f BB |
6305 | /* |
6306 | * Given a list of columns to display, output appropriate headers for each one. | |
6307 | */ | |
6308 | static void | |
1bd201e7 | 6309 | print_header(list_cbdata_t *cb) |
34dc7c2f | 6310 | { |
1bd201e7 | 6311 | zprop_list_t *pl = cb->cb_proplist; |
9ae529ec | 6312 | char headerbuf[ZPOOL_MAXPROPLEN]; |
34dc7c2f BB |
6313 | const char *header; |
6314 | boolean_t first = B_TRUE; | |
6315 | boolean_t right_justify; | |
1bd201e7 | 6316 | size_t width = 0; |
34dc7c2f BB |
6317 | |
6318 | for (; pl != NULL; pl = pl->pl_next) { | |
1bd201e7 CS |
6319 | width = pl->pl_width; |
6320 | if (first && cb->cb_verbose) { | |
6321 | /* | |
6322 | * Reset the width to accommodate the verbose listing | |
6323 | * of devices. | |
6324 | */ | |
6325 | width = cb->cb_namewidth; | |
6326 | } | |
6327 | ||
34dc7c2f | 6328 | if (!first) |
06b80506 | 6329 | (void) fputs(" ", stdout); |
34dc7c2f BB |
6330 | else |
6331 | first = B_FALSE; | |
6332 | ||
9ae529ec | 6333 | right_justify = B_FALSE; |
4ff7a8fa | 6334 | if (pl->pl_prop != ZPROP_USERPROP) { |
9ae529ec CS |
6335 | header = zpool_prop_column_name(pl->pl_prop); |
6336 | right_justify = zpool_prop_align_right(pl->pl_prop); | |
6337 | } else { | |
6338 | int i; | |
6339 | ||
6340 | for (i = 0; pl->pl_user_prop[i] != '\0'; i++) | |
6341 | headerbuf[i] = toupper(pl->pl_user_prop[i]); | |
6342 | headerbuf[i] = '\0'; | |
6343 | header = headerbuf; | |
6344 | } | |
34dc7c2f BB |
6345 | |
6346 | if (pl->pl_next == NULL && !right_justify) | |
06b80506 | 6347 | (void) fputs(header, stdout); |
34dc7c2f | 6348 | else if (right_justify) |
1bd201e7 | 6349 | (void) printf("%*s", (int)width, header); |
34dc7c2f | 6350 | else |
1bd201e7 | 6351 | (void) printf("%-*s", (int)width, header); |
34dc7c2f BB |
6352 | } |
6353 | ||
06b80506 | 6354 | (void) fputc('\n', stdout); |
34dc7c2f BB |
6355 | } |
6356 | ||
6357 | /* | |
6358 | * Given a pool and a list of properties, print out all the properties according | |
cc99f275 | 6359 | * to the described layout. Used by zpool_do_list(). |
34dc7c2f BB |
6360 | */ |
6361 | static void | |
1bd201e7 | 6362 | print_pool(zpool_handle_t *zhp, list_cbdata_t *cb) |
34dc7c2f | 6363 | { |
1bd201e7 | 6364 | zprop_list_t *pl = cb->cb_proplist; |
34dc7c2f BB |
6365 | boolean_t first = B_TRUE; |
6366 | char property[ZPOOL_MAXPROPLEN]; | |
a926aab9 | 6367 | const char *propstr; |
34dc7c2f | 6368 | boolean_t right_justify; |
1bd201e7 | 6369 | size_t width; |
34dc7c2f BB |
6370 | |
6371 | for (; pl != NULL; pl = pl->pl_next) { | |
1bd201e7 CS |
6372 | |
6373 | width = pl->pl_width; | |
6374 | if (first && cb->cb_verbose) { | |
6375 | /* | |
6376 | * Reset the width to accommodate the verbose listing | |
6377 | * of devices. | |
6378 | */ | |
6379 | width = cb->cb_namewidth; | |
6380 | } | |
6381 | ||
34dc7c2f | 6382 | if (!first) { |
1bd201e7 | 6383 | if (cb->cb_scripted) |
06b80506 | 6384 | (void) fputc('\t', stdout); |
34dc7c2f | 6385 | else |
06b80506 | 6386 | (void) fputs(" ", stdout); |
34dc7c2f BB |
6387 | } else { |
6388 | first = B_FALSE; | |
6389 | } | |
6390 | ||
6391 | right_justify = B_FALSE; | |
4ff7a8fa | 6392 | if (pl->pl_prop != ZPROP_USERPROP) { |
a05dfd00 | 6393 | if (zpool_get_prop(zhp, pl->pl_prop, property, |
2a8b84b7 | 6394 | sizeof (property), NULL, cb->cb_literal) != 0) |
34dc7c2f BB |
6395 | propstr = "-"; |
6396 | else | |
6397 | propstr = property; | |
6398 | ||
6399 | right_justify = zpool_prop_align_right(pl->pl_prop); | |
9ae529ec CS |
6400 | } else if ((zpool_prop_feature(pl->pl_user_prop) || |
6401 | zpool_prop_unsupported(pl->pl_user_prop)) && | |
6402 | zpool_prop_get_feature(zhp, pl->pl_user_prop, property, | |
6403 | sizeof (property)) == 0) { | |
6404 | propstr = property; | |
8eae2d21 AJ |
6405 | } else if (zfs_prop_user(pl->pl_user_prop) && |
6406 | zpool_get_userprop(zhp, pl->pl_user_prop, property, | |
6407 | sizeof (property), NULL) == 0) { | |
6408 | propstr = property; | |
34dc7c2f BB |
6409 | } else { |
6410 | propstr = "-"; | |
6411 | } | |
6412 | ||
34dc7c2f BB |
6413 | /* |
6414 | * If this is being called in scripted mode, or if this is the | |
6415 | * last column and it is left-justified, don't include a width | |
6416 | * format specifier. | |
6417 | */ | |
1bd201e7 | 6418 | if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) |
06b80506 | 6419 | (void) fputs(propstr, stdout); |
34dc7c2f | 6420 | else if (right_justify) |
1bd201e7 | 6421 | (void) printf("%*s", (int)width, propstr); |
34dc7c2f | 6422 | else |
1bd201e7 | 6423 | (void) printf("%-*s", (int)width, propstr); |
34dc7c2f BB |
6424 | } |
6425 | ||
06b80506 | 6426 | (void) fputc('\n', stdout); |
34dc7c2f BB |
6427 | } |
6428 | ||
1bd201e7 | 6429 | static void |
c5eea0ab BB |
6430 | print_one_column(zpool_prop_t prop, uint64_t value, const char *str, |
6431 | boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format) | |
1bd201e7 CS |
6432 | { |
6433 | char propval[64]; | |
6434 | boolean_t fixed; | |
6435 | size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); | |
6436 | ||
a05dfd00 | 6437 | switch (prop) { |
1282274f | 6438 | case ZPOOL_PROP_SIZE: |
a05dfd00 | 6439 | case ZPOOL_PROP_EXPANDSZ: |
d2734cce | 6440 | case ZPOOL_PROP_CHECKPOINT: |
c5eea0ab | 6441 | case ZPOOL_PROP_DEDUPRATIO: |
a05dfd00 GW |
6442 | if (value == 0) |
6443 | (void) strlcpy(propval, "-", sizeof (propval)); | |
6444 | else | |
bc2d8093 CE |
6445 | zfs_nicenum_format(value, propval, sizeof (propval), |
6446 | format); | |
a05dfd00 GW |
6447 | break; |
6448 | case ZPOOL_PROP_FRAGMENTATION: | |
6449 | if (value == ZFS_FRAG_INVALID) { | |
6450 | (void) strlcpy(propval, "-", sizeof (propval)); | |
bc2d8093 CE |
6451 | } else if (format == ZFS_NICENUM_RAW) { |
6452 | (void) snprintf(propval, sizeof (propval), "%llu", | |
6453 | (unsigned long long)value); | |
a05dfd00 GW |
6454 | } else { |
6455 | (void) snprintf(propval, sizeof (propval), "%llu%%", | |
6456 | (unsigned long long)value); | |
6457 | } | |
6458 | break; | |
6459 | case ZPOOL_PROP_CAPACITY: | |
cc99f275 | 6460 | /* capacity value is in parts-per-10,000 (aka permyriad) */ |
bc2d8093 CE |
6461 | if (format == ZFS_NICENUM_RAW) |
6462 | (void) snprintf(propval, sizeof (propval), "%llu", | |
cc99f275 | 6463 | (unsigned long long)value / 100); |
bc2d8093 | 6464 | else |
cc99f275 DB |
6465 | (void) snprintf(propval, sizeof (propval), |
6466 | value < 1000 ? "%1.2f%%" : value < 10000 ? | |
6467 | "%2.1f%%" : "%3.0f%%", value / 100.0); | |
a05dfd00 | 6468 | break; |
c5eea0ab BB |
6469 | case ZPOOL_PROP_HEALTH: |
6470 | width = 8; | |
96904d87 | 6471 | (void) strlcpy(propval, str, sizeof (propval)); |
c5eea0ab | 6472 | break; |
a05dfd00 | 6473 | default: |
bc2d8093 | 6474 | zfs_nicenum_format(value, propval, sizeof (propval), format); |
a05dfd00 GW |
6475 | } |
6476 | ||
6477 | if (!valid) | |
6478 | (void) strlcpy(propval, "-", sizeof (propval)); | |
1bd201e7 CS |
6479 | |
6480 | if (scripted) | |
6481 | (void) printf("\t%s", propval); | |
6482 | else | |
6483 | (void) printf(" %*s", (int)width, propval); | |
6484 | } | |
6485 | ||
cc99f275 DB |
6486 | /* |
6487 | * print static default line per vdev | |
6488 | * not compatible with '-o' <proplist> option | |
6489 | */ | |
65c7cc49 | 6490 | static void |
1bd201e7 | 6491 | print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, |
c5eea0ab | 6492 | list_cbdata_t *cb, int depth, boolean_t isspare) |
1bd201e7 CS |
6493 | { |
6494 | nvlist_t **child; | |
6495 | vdev_stat_t *vs; | |
6496 | uint_t c, children; | |
6497 | char *vname; | |
6498 | boolean_t scripted = cb->cb_scripted; | |
8e4c5c9a | 6499 | uint64_t islog = B_FALSE; |
cdb3eb5a AZ |
6500 | const char *dashes = "%-*s - - - - " |
6501 | "- - - - -\n"; | |
1bd201e7 CS |
6502 | |
6503 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, | |
6504 | (uint64_t **)&vs, &c) == 0); | |
6505 | ||
6506 | if (name != NULL) { | |
a05dfd00 GW |
6507 | boolean_t toplevel = (vs->vs_space != 0); |
6508 | uint64_t cap; | |
bc2d8093 | 6509 | enum zfs_nicenum_format format; |
c5eea0ab | 6510 | const char *state; |
bc2d8093 CE |
6511 | |
6512 | if (cb->cb_literal) | |
6513 | format = ZFS_NICENUM_RAW; | |
6514 | else | |
6515 | format = ZFS_NICENUM_1024; | |
a05dfd00 | 6516 | |
a1d477c2 MA |
6517 | if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) |
6518 | return; | |
6519 | ||
1bd201e7 CS |
6520 | if (scripted) |
6521 | (void) printf("\t%s", name); | |
6522 | else if (strlen(name) + depth > cb->cb_namewidth) | |
6523 | (void) printf("%*s%s", depth, "", name); | |
6524 | else | |
6525 | (void) printf("%*s%s%*s", depth, "", name, | |
6526 | (int)(cb->cb_namewidth - strlen(name) - depth), ""); | |
6527 | ||
a05dfd00 GW |
6528 | /* |
6529 | * Print the properties for the individual vdevs. Some | |
6530 | * properties are only applicable to toplevel vdevs. The | |
6531 | * 'toplevel' boolean value is passed to the print_one_column() | |
6532 | * to indicate that the value is valid. | |
6533 | */ | |
e77d59eb | 6534 | if (VDEV_STAT_VALID(vs_pspace, c) && vs->vs_pspace) |
1282274f A |
6535 | print_one_column(ZPOOL_PROP_SIZE, vs->vs_pspace, NULL, |
6536 | scripted, B_TRUE, format); | |
6537 | else | |
6538 | print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL, | |
6539 | scripted, toplevel, format); | |
c5eea0ab | 6540 | print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL, |
bc2d8093 | 6541 | scripted, toplevel, format); |
c5eea0ab BB |
6542 | print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc, |
6543 | NULL, scripted, toplevel, format); | |
d2734cce | 6544 | print_one_column(ZPOOL_PROP_CHECKPOINT, |
c5eea0ab BB |
6545 | vs->vs_checkpoint_space, NULL, scripted, toplevel, format); |
6546 | print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL, | |
6547 | scripted, B_TRUE, format); | |
a05dfd00 | 6548 | print_one_column(ZPOOL_PROP_FRAGMENTATION, |
c5eea0ab | 6549 | vs->vs_fragmentation, NULL, scripted, |
bc2d8093 CE |
6550 | (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel), |
6551 | format); | |
a05dfd00 | 6552 | cap = (vs->vs_space == 0) ? 0 : |
cc99f275 | 6553 | (vs->vs_alloc * 10000 / vs->vs_space); |
c5eea0ab BB |
6554 | print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL, |
6555 | scripted, toplevel, format); | |
6556 | print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL, | |
6557 | scripted, toplevel, format); | |
6558 | state = zpool_state_to_name(vs->vs_state, vs->vs_aux); | |
6559 | if (isspare) { | |
6560 | if (vs->vs_aux == VDEV_AUX_SPARED) | |
6561 | state = "INUSE"; | |
6562 | else if (vs->vs_state == VDEV_STATE_HEALTHY) | |
6563 | state = "AVAIL"; | |
6564 | } | |
6565 | print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted, | |
6566 | B_TRUE, format); | |
06b80506 | 6567 | (void) fputc('\n', stdout); |
1bd201e7 CS |
6568 | } |
6569 | ||
6570 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
6571 | &child, &children) != 0) | |
6572 | return; | |
6573 | ||
cc99f275 | 6574 | /* list the normal vdevs first */ |
1bd201e7 CS |
6575 | for (c = 0; c < children; c++) { |
6576 | uint64_t ishole = B_FALSE; | |
6577 | ||
6578 | if (nvlist_lookup_uint64(child[c], | |
6579 | ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) | |
6580 | continue; | |
6581 | ||
8e4c5c9a | 6582 | if (nvlist_lookup_uint64(child[c], |
cc99f275 DB |
6583 | ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) |
6584 | continue; | |
6585 | ||
6586 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) | |
8e4c5c9a | 6587 | continue; |
8e4c5c9a | 6588 | |
d2f3e292 | 6589 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
7b468ed2 | 6590 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
c5eea0ab | 6591 | print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE); |
1bd201e7 CS |
6592 | free(vname); |
6593 | } | |
6594 | ||
cc99f275 | 6595 | /* list the classes: 'logs', 'dedup', and 'special' */ |
cf5a9d8c | 6596 | for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) { |
cc99f275 DB |
6597 | boolean_t printed = B_FALSE; |
6598 | ||
8e4c5c9a | 6599 | for (c = 0; c < children; c++) { |
d1807f16 RY |
6600 | const char *bias = NULL; |
6601 | const char *type = NULL; | |
cc99f275 | 6602 | |
8e4c5c9a | 6603 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
cc99f275 | 6604 | &islog) == 0 && islog) { |
d1807f16 | 6605 | bias = VDEV_ALLOC_CLASS_LOGS; |
cc99f275 DB |
6606 | } else { |
6607 | (void) nvlist_lookup_string(child[c], | |
6608 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
6609 | (void) nvlist_lookup_string(child[c], | |
6610 | ZPOOL_CONFIG_TYPE, &type); | |
6611 | } | |
6612 | if (bias == NULL || strcmp(bias, class_name[n]) != 0) | |
8e4c5c9a | 6613 | continue; |
cc99f275 DB |
6614 | if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) |
6615 | continue; | |
6616 | ||
6617 | if (!printed) { | |
6618 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
6619 | (void) printf(dashes, cb->cb_namewidth, | |
6620 | class_name[n]); | |
6621 | printed = B_TRUE; | |
6622 | } | |
d2f3e292 | 6623 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
7b468ed2 | 6624 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
c5eea0ab BB |
6625 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
6626 | B_FALSE); | |
8e4c5c9a JWK |
6627 | free(vname); |
6628 | } | |
6629 | } | |
6630 | ||
1bd201e7 | 6631 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, |
8e4c5c9a JWK |
6632 | &child, &children) == 0 && children > 0) { |
6633 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
6634 | (void) printf(dashes, cb->cb_namewidth, "cache"); | |
6635 | for (c = 0; c < children; c++) { | |
d2f3e292 RY |
6636 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
6637 | cb->cb_name_flags); | |
c5eea0ab BB |
6638 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
6639 | B_FALSE); | |
8e4c5c9a JWK |
6640 | free(vname); |
6641 | } | |
6642 | } | |
1bd201e7 | 6643 | |
8e4c5c9a JWK |
6644 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child, |
6645 | &children) == 0 && children > 0) { | |
6646 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
6647 | (void) printf(dashes, cb->cb_namewidth, "spare"); | |
1bd201e7 | 6648 | for (c = 0; c < children; c++) { |
d2f3e292 RY |
6649 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
6650 | cb->cb_name_flags); | |
c5eea0ab BB |
6651 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
6652 | B_TRUE); | |
1bd201e7 CS |
6653 | free(vname); |
6654 | } | |
6655 | } | |
6656 | } | |
6657 | ||
34dc7c2f BB |
6658 | /* |
6659 | * Generic callback function to list a pool. | |
6660 | */ | |
65c7cc49 | 6661 | static int |
34dc7c2f BB |
6662 | list_callback(zpool_handle_t *zhp, void *data) |
6663 | { | |
6664 | list_cbdata_t *cbp = data; | |
6665 | ||
c5eea0ab | 6666 | print_pool(zhp, cbp); |
34dc7c2f | 6667 | |
cc99f275 | 6668 | if (cbp->cb_verbose) { |
c5eea0ab | 6669 | nvlist_t *config, *nvroot; |
cc99f275 | 6670 | |
c5eea0ab | 6671 | config = zpool_get_config(zhp, NULL); |
cc99f275 DB |
6672 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, |
6673 | &nvroot) == 0); | |
c5eea0ab | 6674 | print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE); |
cc99f275 DB |
6675 | } |
6676 | ||
c5eea0ab BB |
6677 | return (0); |
6678 | } | |
cc99f275 | 6679 | |
c5eea0ab BB |
6680 | /* |
6681 | * Set the minimum pool/vdev name column width. The width must be at least 9, | |
6682 | * but may be as large as needed. | |
6683 | */ | |
6684 | static int | |
6685 | get_namewidth_list(zpool_handle_t *zhp, void *data) | |
6686 | { | |
6687 | list_cbdata_t *cb = data; | |
6688 | int width; | |
6689 | ||
7c0e3941 SW |
6690 | width = get_namewidth(zhp, cb->cb_namewidth, |
6691 | cb->cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose); | |
1bd201e7 | 6692 | |
c5eea0ab BB |
6693 | if (width < 9) |
6694 | width = 9; | |
6695 | ||
6696 | cb->cb_namewidth = width; | |
34dc7c2f BB |
6697 | |
6698 | return (0); | |
6699 | } | |
6700 | ||
6701 | /* | |
2a8b84b7 | 6702 | * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] |
34dc7c2f | 6703 | * |
d2f3e292 | 6704 | * -g Display guid for individual vdev name. |
34dc7c2f BB |
6705 | * -H Scripted mode. Don't display headers, and separate properties |
6706 | * by a single tab. | |
d2f3e292 | 6707 | * -L Follow links when resolving vdev path name. |
34dc7c2f | 6708 | * -o List of properties to display. Defaults to |
a05dfd00 GW |
6709 | * "name,size,allocated,free,expandsize,fragmentation,capacity," |
6710 | * "dedupratio,health,altroot" | |
c5eea0ab | 6711 | * -p Display values in parsable (exact) format. |
a77f29f9 | 6712 | * -P Display full path for vdev name. |
428870ff | 6713 | * -T Display a timestamp in date(1) or Unix format |
34dc7c2f BB |
6714 | * |
6715 | * List all pools in the system, whether or not they're healthy. Output space | |
6716 | * statistics for each one, as well as health status summary. | |
6717 | */ | |
6718 | int | |
6719 | zpool_do_list(int argc, char **argv) | |
6720 | { | |
6721 | int c; | |
cd72af9c | 6722 | int ret = 0; |
34dc7c2f BB |
6723 | list_cbdata_t cb = { 0 }; |
6724 | static char default_props[] = | |
d2734cce SD |
6725 | "name,size,allocated,free,checkpoint,expandsize,fragmentation," |
6726 | "capacity,dedupratio,health,altroot"; | |
34dc7c2f | 6727 | char *props = default_props; |
193a37cb TH |
6728 | float interval = 0; |
6729 | unsigned long count = 0; | |
1bd201e7 CS |
6730 | zpool_list_t *list; |
6731 | boolean_t first = B_TRUE; | |
2a673e76 | 6732 | current_prop_type = ZFS_TYPE_POOL; |
34dc7c2f BB |
6733 | |
6734 | /* check options */ | |
2a8b84b7 | 6735 | while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) { |
34dc7c2f | 6736 | switch (c) { |
d2f3e292 RY |
6737 | case 'g': |
6738 | cb.cb_name_flags |= VDEV_NAME_GUID; | |
6739 | break; | |
34dc7c2f BB |
6740 | case 'H': |
6741 | cb.cb_scripted = B_TRUE; | |
6742 | break; | |
d2f3e292 RY |
6743 | case 'L': |
6744 | cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
6745 | break; | |
34dc7c2f BB |
6746 | case 'o': |
6747 | props = optarg; | |
6748 | break; | |
a77f29f9 | 6749 | case 'P': |
d2f3e292 RY |
6750 | cb.cb_name_flags |= VDEV_NAME_PATH; |
6751 | break; | |
2a8b84b7 AS |
6752 | case 'p': |
6753 | cb.cb_literal = B_TRUE; | |
6754 | break; | |
428870ff BB |
6755 | case 'T': |
6756 | get_timestamp_arg(*optarg); | |
6757 | break; | |
1bd201e7 CS |
6758 | case 'v': |
6759 | cb.cb_verbose = B_TRUE; | |
cc99f275 | 6760 | cb.cb_namewidth = 8; /* 8 until precalc is avail */ |
1bd201e7 | 6761 | break; |
34dc7c2f BB |
6762 | case ':': |
6763 | (void) fprintf(stderr, gettext("missing argument for " | |
6764 | "'%c' option\n"), optopt); | |
6765 | usage(B_FALSE); | |
6766 | break; | |
6767 | case '?': | |
6768 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6769 | optopt); | |
6770 | usage(B_FALSE); | |
6771 | } | |
6772 | } | |
6773 | ||
6774 | argc -= optind; | |
6775 | argv += optind; | |
6776 | ||
428870ff BB |
6777 | get_interval_count(&argc, argv, &interval, &count); |
6778 | ||
34dc7c2f BB |
6779 | if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) |
6780 | usage(B_FALSE); | |
6781 | ||
428870ff | 6782 | for (;;) { |
3e43edd2 | 6783 | if ((list = pool_list_get(argc, argv, &cb.cb_proplist, |
2a673e76 | 6784 | ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL) |
3e43edd2 | 6785 | return (1); |
1bd201e7 CS |
6786 | |
6787 | if (pool_list_count(list) == 0) | |
6788 | break; | |
34dc7c2f | 6789 | |
c5eea0ab BB |
6790 | cb.cb_namewidth = 0; |
6791 | (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb); | |
6792 | ||
428870ff BB |
6793 | if (timestamp_fmt != NODATE) |
6794 | print_timestamp(timestamp_fmt); | |
34dc7c2f | 6795 | |
1bd201e7 CS |
6796 | if (!cb.cb_scripted && (first || cb.cb_verbose)) { |
6797 | print_header(&cb); | |
6798 | first = B_FALSE; | |
428870ff | 6799 | } |
1bd201e7 | 6800 | ret = pool_list_iter(list, B_TRUE, list_callback, &cb); |
428870ff BB |
6801 | |
6802 | if (interval == 0) | |
6803 | break; | |
6804 | ||
6805 | if (count != 0 && --count == 0) | |
6806 | break; | |
6807 | ||
3e43edd2 | 6808 | pool_list_free(list); |
5f2700ee RN |
6809 | |
6810 | (void) fflush(stdout); | |
193a37cb | 6811 | (void) fsleep(interval); |
34dc7c2f BB |
6812 | } |
6813 | ||
3e43edd2 GW |
6814 | if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { |
6815 | (void) printf(gettext("no pools available\n")); | |
6816 | ret = 0; | |
6817 | } | |
6818 | ||
6819 | pool_list_free(list); | |
428870ff | 6820 | zprop_free_list(cb.cb_proplist); |
34dc7c2f BB |
6821 | return (ret); |
6822 | } | |
6823 | ||
34dc7c2f BB |
6824 | static int |
6825 | zpool_do_attach_or_replace(int argc, char **argv, int replacing) | |
6826 | { | |
6827 | boolean_t force = B_FALSE; | |
9a49d3f3 | 6828 | boolean_t rebuild = B_FALSE; |
e60e158e | 6829 | boolean_t wait = B_FALSE; |
34dc7c2f BB |
6830 | int c; |
6831 | nvlist_t *nvroot; | |
6832 | char *poolname, *old_disk, *new_disk; | |
6833 | zpool_handle_t *zhp; | |
df831108 CP |
6834 | nvlist_t *props = NULL; |
6835 | char *propval; | |
34dc7c2f BB |
6836 | int ret; |
6837 | ||
6838 | /* check options */ | |
9a49d3f3 | 6839 | while ((c = getopt(argc, argv, "fo:sw")) != -1) { |
34dc7c2f BB |
6840 | switch (c) { |
6841 | case 'f': | |
6842 | force = B_TRUE; | |
6843 | break; | |
df831108 CP |
6844 | case 'o': |
6845 | if ((propval = strchr(optarg, '=')) == NULL) { | |
6846 | (void) fprintf(stderr, gettext("missing " | |
6847 | "'=' for -o option\n")); | |
6848 | usage(B_FALSE); | |
6849 | } | |
6850 | *propval = '\0'; | |
6851 | propval++; | |
6852 | ||
6853 | if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || | |
6854 | (add_prop_list(optarg, propval, &props, B_TRUE))) | |
6855 | usage(B_FALSE); | |
6856 | break; | |
9a49d3f3 BB |
6857 | case 's': |
6858 | rebuild = B_TRUE; | |
6859 | break; | |
e60e158e JG |
6860 | case 'w': |
6861 | wait = B_TRUE; | |
6862 | break; | |
34dc7c2f BB |
6863 | case '?': |
6864 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6865 | optopt); | |
6866 | usage(B_FALSE); | |
6867 | } | |
6868 | } | |
6869 | ||
6870 | argc -= optind; | |
6871 | argv += optind; | |
6872 | ||
6873 | /* get pool name and check number of arguments */ | |
6874 | if (argc < 1) { | |
6875 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
6876 | usage(B_FALSE); | |
6877 | } | |
6878 | ||
6879 | poolname = argv[0]; | |
6880 | ||
6881 | if (argc < 2) { | |
6882 | (void) fprintf(stderr, | |
6883 | gettext("missing <device> specification\n")); | |
6884 | usage(B_FALSE); | |
6885 | } | |
6886 | ||
6887 | old_disk = argv[1]; | |
6888 | ||
6889 | if (argc < 3) { | |
6890 | if (!replacing) { | |
6891 | (void) fprintf(stderr, | |
6892 | gettext("missing <new_device> specification\n")); | |
6893 | usage(B_FALSE); | |
6894 | } | |
6895 | new_disk = old_disk; | |
6896 | argc -= 1; | |
6897 | argv += 1; | |
6898 | } else { | |
6899 | new_disk = argv[2]; | |
6900 | argc -= 2; | |
6901 | argv += 2; | |
6902 | } | |
6903 | ||
6904 | if (argc > 1) { | |
6905 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
6906 | usage(B_FALSE); | |
6907 | } | |
6908 | ||
a425f5bf | 6909 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { |
6910 | nvlist_free(props); | |
34dc7c2f | 6911 | return (1); |
a425f5bf | 6912 | } |
34dc7c2f BB |
6913 | |
6914 | if (zpool_get_config(zhp, NULL) == NULL) { | |
6915 | (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), | |
6916 | poolname); | |
6917 | zpool_close(zhp); | |
a425f5bf | 6918 | nvlist_free(props); |
34dc7c2f BB |
6919 | return (1); |
6920 | } | |
6921 | ||
dddef7d6 | 6922 | /* unless manually specified use "ashift" pool property (if set) */ |
6923 | if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { | |
6924 | int intval; | |
6925 | zprop_source_t src; | |
6926 | char strval[ZPOOL_MAXPROPLEN]; | |
6927 | ||
6928 | intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); | |
6929 | if (src != ZPROP_SRC_DEFAULT) { | |
6930 | (void) sprintf(strval, "%" PRId32, intval); | |
6931 | verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, | |
6932 | &props, B_TRUE) == 0); | |
6933 | } | |
6934 | } | |
6935 | ||
df831108 | 6936 | nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE, |
b128c09f | 6937 | argc, argv); |
34dc7c2f BB |
6938 | if (nvroot == NULL) { |
6939 | zpool_close(zhp); | |
a425f5bf | 6940 | nvlist_free(props); |
34dc7c2f BB |
6941 | return (1); |
6942 | } | |
6943 | ||
9a49d3f3 BB |
6944 | ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing, |
6945 | rebuild); | |
34dc7c2f | 6946 | |
5caeef02 DB |
6947 | if (ret == 0 && wait) { |
6948 | zpool_wait_activity_t activity = ZPOOL_WAIT_RESILVER; | |
6949 | char raidz_prefix[] = "raidz"; | |
6950 | if (replacing) { | |
6951 | activity = ZPOOL_WAIT_REPLACE; | |
6952 | } else if (strncmp(old_disk, | |
6953 | raidz_prefix, strlen(raidz_prefix)) == 0) { | |
6954 | activity = ZPOOL_WAIT_RAIDZ_EXPAND; | |
6955 | } | |
6956 | ret = zpool_wait(zhp, activity); | |
6957 | } | |
e60e158e | 6958 | |
a425f5bf | 6959 | nvlist_free(props); |
34dc7c2f BB |
6960 | nvlist_free(nvroot); |
6961 | zpool_close(zhp); | |
6962 | ||
6963 | return (ret); | |
6964 | } | |
6965 | ||
6966 | /* | |
9a49d3f3 | 6967 | * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device> |
34dc7c2f BB |
6968 | * |
6969 | * -f Force attach, even if <new_device> appears to be in use. | |
9a49d3f3 | 6970 | * -s Use sequential instead of healing reconstruction for resilver. |
e60e158e JG |
6971 | * -o Set property=value. |
6972 | * -w Wait for replacing to complete before returning | |
34dc7c2f BB |
6973 | * |
6974 | * Replace <device> with <new_device>. | |
6975 | */ | |
34dc7c2f BB |
6976 | int |
6977 | zpool_do_replace(int argc, char **argv) | |
6978 | { | |
6979 | return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); | |
6980 | } | |
6981 | ||
6982 | /* | |
5caeef02 | 6983 | * zpool attach [-fsw] [-o property=value] <pool> <device>|<vdev> <new_device> |
34dc7c2f BB |
6984 | * |
6985 | * -f Force attach, even if <new_device> appears to be in use. | |
9a49d3f3 | 6986 | * -s Use sequential instead of healing reconstruction for resilver. |
df831108 | 6987 | * -o Set property=value. |
5caeef02 DB |
6988 | * -w Wait for resilvering (mirror) or expansion (raidz) to complete |
6989 | * before returning. | |
34dc7c2f | 6990 | * |
5caeef02 DB |
6991 | * Attach <new_device> to a <device> or <vdev>, where the vdev can be of type |
6992 | * mirror or raidz. If <device> is not part of a mirror, then <device> will | |
6993 | * be transformed into a mirror of <device> and <new_device>. When a mirror | |
6994 | * is involved, <new_device> will begin life with a DTL of [0, now], and will | |
6995 | * immediately begin to resilver itself. For the raidz case, a expansion will | |
6996 | * commence and reflow the raidz data across all the disks including the | |
6997 | * <new_device>. | |
34dc7c2f BB |
6998 | */ |
6999 | int | |
7000 | zpool_do_attach(int argc, char **argv) | |
7001 | { | |
7002 | return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); | |
7003 | } | |
7004 | ||
7005 | /* | |
7006 | * zpool detach [-f] <pool> <device> | |
7007 | * | |
7008 | * -f Force detach of <device>, even if DTLs argue against it | |
7009 | * (not supported yet) | |
7010 | * | |
7011 | * Detach a device from a mirror. The operation will be refused if <device> | |
7012 | * is the last device in the mirror, or if the DTLs indicate that this device | |
7013 | * has the only valid copy of some data. | |
7014 | */ | |
34dc7c2f BB |
7015 | int |
7016 | zpool_do_detach(int argc, char **argv) | |
7017 | { | |
7018 | int c; | |
7019 | char *poolname, *path; | |
7020 | zpool_handle_t *zhp; | |
7021 | int ret; | |
7022 | ||
7023 | /* check options */ | |
d02186ee | 7024 | while ((c = getopt(argc, argv, "")) != -1) { |
34dc7c2f | 7025 | switch (c) { |
34dc7c2f BB |
7026 | case '?': |
7027 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7028 | optopt); | |
7029 | usage(B_FALSE); | |
7030 | } | |
7031 | } | |
7032 | ||
7033 | argc -= optind; | |
7034 | argv += optind; | |
7035 | ||
7036 | /* get pool name and check number of arguments */ | |
7037 | if (argc < 1) { | |
7038 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
7039 | usage(B_FALSE); | |
7040 | } | |
7041 | ||
7042 | if (argc < 2) { | |
7043 | (void) fprintf(stderr, | |
7044 | gettext("missing <device> specification\n")); | |
7045 | usage(B_FALSE); | |
7046 | } | |
7047 | ||
7048 | poolname = argv[0]; | |
7049 | path = argv[1]; | |
7050 | ||
7051 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
7052 | return (1); | |
7053 | ||
7054 | ret = zpool_vdev_detach(zhp, path); | |
7055 | ||
7056 | zpool_close(zhp); | |
7057 | ||
7058 | return (ret); | |
7059 | } | |
7060 | ||
428870ff | 7061 | /* |
a77f29f9 | 7062 | * zpool split [-gLnP] [-o prop=val] ... |
428870ff BB |
7063 | * [-o mntopt] ... |
7064 | * [-R altroot] <pool> <newpool> [<device> ...] | |
7065 | * | |
d2f3e292 RY |
7066 | * -g Display guid for individual vdev name. |
7067 | * -L Follow links when resolving vdev path name. | |
428870ff BB |
7068 | * -n Do not split the pool, but display the resulting layout if |
7069 | * it were to be split. | |
7070 | * -o Set property=value, or set mount options. | |
a77f29f9 | 7071 | * -P Display full path for vdev name. |
428870ff | 7072 | * -R Mount the split-off pool under an alternate root. |
b5256303 | 7073 | * -l Load encryption keys while importing. |
428870ff BB |
7074 | * |
7075 | * Splits the named pool and gives it the new pool name. Devices to be split | |
7076 | * off may be listed, provided that no more than one device is specified | |
7077 | * per top-level vdev mirror. The newly split pool is left in an exported | |
7078 | * state unless -R is specified. | |
7079 | * | |
7080 | * Restrictions: the top-level of the pool pool must only be made up of | |
7081 | * mirrors; all devices in the pool must be healthy; no device may be | |
7082 | * undergoing a resilvering operation. | |
7083 | */ | |
7084 | int | |
7085 | zpool_do_split(int argc, char **argv) | |
7086 | { | |
7087 | char *srcpool, *newpool, *propval; | |
7088 | char *mntopts = NULL; | |
7089 | splitflags_t flags; | |
7090 | int c, ret = 0; | |
71472bf3 | 7091 | int ms_status = 0; |
b5256303 | 7092 | boolean_t loadkeys = B_FALSE; |
428870ff BB |
7093 | zpool_handle_t *zhp; |
7094 | nvlist_t *config, *props = NULL; | |
7095 | ||
7096 | flags.dryrun = B_FALSE; | |
7097 | flags.import = B_FALSE; | |
d2f3e292 | 7098 | flags.name_flags = 0; |
428870ff BB |
7099 | |
7100 | /* check options */ | |
b5256303 | 7101 | while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) { |
428870ff | 7102 | switch (c) { |
d2f3e292 RY |
7103 | case 'g': |
7104 | flags.name_flags |= VDEV_NAME_GUID; | |
7105 | break; | |
7106 | case 'L': | |
7107 | flags.name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
7108 | break; | |
428870ff BB |
7109 | case 'R': |
7110 | flags.import = B_TRUE; | |
7111 | if (add_prop_list( | |
7112 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, | |
7113 | &props, B_TRUE) != 0) { | |
8a5fc748 | 7114 | nvlist_free(props); |
428870ff BB |
7115 | usage(B_FALSE); |
7116 | } | |
7117 | break; | |
b5256303 TC |
7118 | case 'l': |
7119 | loadkeys = B_TRUE; | |
7120 | break; | |
428870ff BB |
7121 | case 'n': |
7122 | flags.dryrun = B_TRUE; | |
7123 | break; | |
7124 | case 'o': | |
7125 | if ((propval = strchr(optarg, '=')) != NULL) { | |
7126 | *propval = '\0'; | |
7127 | propval++; | |
7128 | if (add_prop_list(optarg, propval, | |
7129 | &props, B_TRUE) != 0) { | |
8a5fc748 | 7130 | nvlist_free(props); |
428870ff BB |
7131 | usage(B_FALSE); |
7132 | } | |
7133 | } else { | |
7134 | mntopts = optarg; | |
7135 | } | |
7136 | break; | |
a77f29f9 | 7137 | case 'P': |
d2f3e292 RY |
7138 | flags.name_flags |= VDEV_NAME_PATH; |
7139 | break; | |
428870ff BB |
7140 | case ':': |
7141 | (void) fprintf(stderr, gettext("missing argument for " | |
7142 | "'%c' option\n"), optopt); | |
7143 | usage(B_FALSE); | |
7144 | break; | |
7145 | case '?': | |
7146 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7147 | optopt); | |
7148 | usage(B_FALSE); | |
7149 | break; | |
7150 | } | |
7151 | } | |
7152 | ||
7153 | if (!flags.import && mntopts != NULL) { | |
7154 | (void) fprintf(stderr, gettext("setting mntopts is only " | |
7155 | "valid when importing the pool\n")); | |
7156 | usage(B_FALSE); | |
7157 | } | |
7158 | ||
b5256303 TC |
7159 | if (!flags.import && loadkeys) { |
7160 | (void) fprintf(stderr, gettext("loading keys is only " | |
7161 | "valid when importing the pool\n")); | |
7162 | usage(B_FALSE); | |
7163 | } | |
7164 | ||
428870ff BB |
7165 | argc -= optind; |
7166 | argv += optind; | |
7167 | ||
7168 | if (argc < 1) { | |
7169 | (void) fprintf(stderr, gettext("Missing pool name\n")); | |
7170 | usage(B_FALSE); | |
7171 | } | |
7172 | if (argc < 2) { | |
7173 | (void) fprintf(stderr, gettext("Missing new pool name\n")); | |
7174 | usage(B_FALSE); | |
7175 | } | |
7176 | ||
7177 | srcpool = argv[0]; | |
7178 | newpool = argv[1]; | |
7179 | ||
7180 | argc -= 2; | |
7181 | argv += 2; | |
7182 | ||
a425f5bf | 7183 | if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) { |
7184 | nvlist_free(props); | |
428870ff | 7185 | return (1); |
a425f5bf | 7186 | } |
428870ff BB |
7187 | |
7188 | config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); | |
7189 | if (config == NULL) { | |
7190 | ret = 1; | |
7191 | } else { | |
7192 | if (flags.dryrun) { | |
7193 | (void) printf(gettext("would create '%s' with the " | |
7194 | "following layout:\n\n"), newpool); | |
cc99f275 | 7195 | print_vdev_tree(NULL, newpool, config, 0, "", |
d2f3e292 | 7196 | flags.name_flags); |
0cb40fa3 AF |
7197 | print_vdev_tree(NULL, "dedup", config, 0, |
7198 | VDEV_ALLOC_BIAS_DEDUP, 0); | |
7199 | print_vdev_tree(NULL, "special", config, 0, | |
7200 | VDEV_ALLOC_BIAS_SPECIAL, 0); | |
428870ff | 7201 | } |
428870ff BB |
7202 | } |
7203 | ||
7204 | zpool_close(zhp); | |
7205 | ||
a425f5bf | 7206 | if (ret != 0 || flags.dryrun || !flags.import) { |
7207 | nvlist_free(config); | |
7208 | nvlist_free(props); | |
428870ff | 7209 | return (ret); |
a425f5bf | 7210 | } |
428870ff BB |
7211 | |
7212 | /* | |
7213 | * The split was successful. Now we need to open the new | |
7214 | * pool and import it. | |
7215 | */ | |
a425f5bf | 7216 | if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) { |
7217 | nvlist_free(config); | |
7218 | nvlist_free(props); | |
428870ff | 7219 | return (1); |
a425f5bf | 7220 | } |
b5256303 TC |
7221 | |
7222 | if (loadkeys) { | |
7223 | ret = zfs_crypto_attempt_load_keys(g_zfs, newpool); | |
7224 | if (ret != 0) | |
7225 | ret = 1; | |
7226 | } | |
7227 | ||
71472bf3 | 7228 | if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) { |
b64afa41 AS |
7229 | ms_status = zpool_enable_datasets(zhp, mntopts, 0, |
7230 | mount_tp_nthr); | |
71472bf3 US |
7231 | if (ms_status == EZFS_SHAREFAILED) { |
7232 | (void) fprintf(stderr, gettext("Split was successful, " | |
7233 | "datasets are mounted but sharing of some datasets " | |
7234 | "has failed\n")); | |
7235 | } else if (ms_status == EZFS_MOUNTFAILED) { | |
7236 | (void) fprintf(stderr, gettext("Split was successful" | |
7237 | ", but some datasets could not be mounted\n")); | |
7238 | (void) fprintf(stderr, gettext("Try doing '%s' with a " | |
7239 | "different altroot\n"), "zpool import"); | |
7240 | } | |
428870ff BB |
7241 | } |
7242 | zpool_close(zhp); | |
a425f5bf | 7243 | nvlist_free(config); |
7244 | nvlist_free(props); | |
428870ff BB |
7245 | |
7246 | return (ret); | |
7247 | } | |
7248 | ||
428870ff | 7249 | |
34dc7c2f | 7250 | /* |
a9520e6e TH |
7251 | * zpool online [--power] <pool> <device> ... |
7252 | * | |
7253 | * --power: Power on the enclosure slot to the drive (if possible) | |
34dc7c2f BB |
7254 | */ |
7255 | int | |
7256 | zpool_do_online(int argc, char **argv) | |
7257 | { | |
7258 | int c, i; | |
7259 | char *poolname; | |
7260 | zpool_handle_t *zhp; | |
7261 | int ret = 0; | |
7262 | vdev_state_t newstate; | |
9babb374 | 7263 | int flags = 0; |
a9520e6e TH |
7264 | boolean_t is_power_on = B_FALSE; |
7265 | struct option long_options[] = { | |
b1e46f86 | 7266 | {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, |
a9520e6e TH |
7267 | {0, 0, 0, 0} |
7268 | }; | |
34dc7c2f BB |
7269 | |
7270 | /* check options */ | |
a9520e6e | 7271 | while ((c = getopt_long(argc, argv, "e", long_options, NULL)) != -1) { |
34dc7c2f | 7272 | switch (c) { |
9babb374 BB |
7273 | case 'e': |
7274 | flags |= ZFS_ONLINE_EXPAND; | |
7275 | break; | |
b1e46f86 | 7276 | case ZPOOL_OPTION_POWER: |
a9520e6e TH |
7277 | is_power_on = B_TRUE; |
7278 | break; | |
34dc7c2f BB |
7279 | case '?': |
7280 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7281 | optopt); | |
7282 | usage(B_FALSE); | |
7283 | } | |
7284 | } | |
7285 | ||
a9520e6e TH |
7286 | if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT")) |
7287 | is_power_on = B_TRUE; | |
7288 | ||
34dc7c2f BB |
7289 | argc -= optind; |
7290 | argv += optind; | |
7291 | ||
7292 | /* get pool name and check number of arguments */ | |
7293 | if (argc < 1) { | |
7294 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
7295 | usage(B_FALSE); | |
7296 | } | |
7297 | if (argc < 2) { | |
7298 | (void) fprintf(stderr, gettext("missing device name\n")); | |
7299 | usage(B_FALSE); | |
7300 | } | |
7301 | ||
7302 | poolname = argv[0]; | |
7303 | ||
7304 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
7305 | return (1); | |
7306 | ||
7307 | for (i = 1; i < argc; i++) { | |
b66c2a08 PD |
7308 | vdev_state_t oldstate; |
7309 | boolean_t avail_spare, l2cache; | |
a9520e6e TH |
7310 | int rc; |
7311 | ||
7312 | if (is_power_on) { | |
7313 | rc = zpool_power_on_and_disk_wait(zhp, argv[i]); | |
7314 | if (rc == ENOTSUP) { | |
7315 | (void) fprintf(stderr, | |
7316 | gettext("Power control not supported\n")); | |
7317 | } | |
7318 | if (rc != 0) | |
7319 | return (rc); | |
7320 | } | |
7321 | ||
b66c2a08 PD |
7322 | nvlist_t *tgt = zpool_find_vdev(zhp, argv[i], &avail_spare, |
7323 | &l2cache, NULL); | |
7324 | if (tgt == NULL) { | |
7325 | ret = 1; | |
7326 | continue; | |
7327 | } | |
7328 | uint_t vsc; | |
7329 | oldstate = ((vdev_stat_t *)fnvlist_lookup_uint64_array(tgt, | |
7330 | ZPOOL_CONFIG_VDEV_STATS, &vsc))->vs_state; | |
9babb374 | 7331 | if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { |
34dc7c2f BB |
7332 | if (newstate != VDEV_STATE_HEALTHY) { |
7333 | (void) printf(gettext("warning: device '%s' " | |
7334 | "onlined, but remains in faulted state\n"), | |
7335 | argv[i]); | |
7336 | if (newstate == VDEV_STATE_FAULTED) | |
7337 | (void) printf(gettext("use 'zpool " | |
7338 | "clear' to restore a faulted " | |
7339 | "device\n")); | |
7340 | else | |
7341 | (void) printf(gettext("use 'zpool " | |
7342 | "replace' to replace devices " | |
7343 | "that are no longer present\n")); | |
b66c2a08 PD |
7344 | if ((flags & ZFS_ONLINE_EXPAND)) { |
7345 | (void) printf(gettext("%s: failed " | |
7346 | "to expand usable space on " | |
7347 | "unhealthy device '%s'\n"), | |
7348 | (oldstate >= VDEV_STATE_DEGRADED ? | |
7349 | "error" : "warning"), argv[i]); | |
7350 | if (oldstate >= VDEV_STATE_DEGRADED) { | |
7351 | ret = 1; | |
7352 | break; | |
7353 | } | |
7354 | } | |
34dc7c2f BB |
7355 | } |
7356 | } else { | |
7357 | ret = 1; | |
7358 | } | |
7359 | } | |
7360 | ||
7361 | zpool_close(zhp); | |
7362 | ||
7363 | return (ret); | |
7364 | } | |
7365 | ||
7366 | /* | |
a9520e6e TH |
7367 | * zpool offline [-ft]|[--power] <pool> <device> ... |
7368 | * | |
34dc7c2f | 7369 | * |
4a283c7f | 7370 | * -f Force the device into a faulted state. |
34dc7c2f | 7371 | * |
4a283c7f | 7372 | * -t Only take the device off-line temporarily. The offline/faulted |
34dc7c2f | 7373 | * state will not be persistent across reboots. |
a9520e6e TH |
7374 | * |
7375 | * --power Power off the enclosure slot to the drive (if possible) | |
34dc7c2f | 7376 | */ |
34dc7c2f BB |
7377 | int |
7378 | zpool_do_offline(int argc, char **argv) | |
7379 | { | |
7380 | int c, i; | |
7381 | char *poolname; | |
7382 | zpool_handle_t *zhp; | |
7383 | int ret = 0; | |
7384 | boolean_t istmp = B_FALSE; | |
4a283c7f | 7385 | boolean_t fault = B_FALSE; |
a9520e6e TH |
7386 | boolean_t is_power_off = B_FALSE; |
7387 | ||
7388 | struct option long_options[] = { | |
b1e46f86 | 7389 | {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, |
a9520e6e TH |
7390 | {0, 0, 0, 0} |
7391 | }; | |
34dc7c2f BB |
7392 | |
7393 | /* check options */ | |
a9520e6e | 7394 | while ((c = getopt_long(argc, argv, "ft", long_options, NULL)) != -1) { |
34dc7c2f | 7395 | switch (c) { |
4a283c7f TH |
7396 | case 'f': |
7397 | fault = B_TRUE; | |
7398 | break; | |
34dc7c2f BB |
7399 | case 't': |
7400 | istmp = B_TRUE; | |
7401 | break; | |
b1e46f86 | 7402 | case ZPOOL_OPTION_POWER: |
a9520e6e TH |
7403 | is_power_off = B_TRUE; |
7404 | break; | |
34dc7c2f BB |
7405 | case '?': |
7406 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7407 | optopt); | |
7408 | usage(B_FALSE); | |
7409 | } | |
7410 | } | |
7411 | ||
a9520e6e TH |
7412 | if (is_power_off && fault) { |
7413 | (void) fprintf(stderr, | |
7414 | gettext("-0 and -f cannot be used together\n")); | |
7415 | usage(B_FALSE); | |
7416 | return (1); | |
7417 | } | |
7418 | ||
7419 | if (is_power_off && istmp) { | |
7420 | (void) fprintf(stderr, | |
7421 | gettext("-0 and -t cannot be used together\n")); | |
7422 | usage(B_FALSE); | |
7423 | return (1); | |
7424 | } | |
7425 | ||
34dc7c2f BB |
7426 | argc -= optind; |
7427 | argv += optind; | |
7428 | ||
7429 | /* get pool name and check number of arguments */ | |
7430 | if (argc < 1) { | |
7431 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
7432 | usage(B_FALSE); | |
7433 | } | |
7434 | if (argc < 2) { | |
7435 | (void) fprintf(stderr, gettext("missing device name\n")); | |
7436 | usage(B_FALSE); | |
7437 | } | |
7438 | ||
7439 | poolname = argv[0]; | |
7440 | ||
7441 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
7442 | return (1); | |
7443 | ||
7444 | for (i = 1; i < argc; i++) { | |
a9520e6e TH |
7445 | uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]); |
7446 | if (is_power_off) { | |
7447 | /* | |
7448 | * Note: we have to power off first, then set REMOVED, | |
7449 | * or else zpool_vdev_set_removed_state() returns | |
7450 | * EAGAIN. | |
7451 | */ | |
7452 | ret = zpool_power_off(zhp, argv[i]); | |
7453 | if (ret != 0) { | |
7454 | (void) fprintf(stderr, "%s %s %d\n", | |
7455 | gettext("unable to power off slot for"), | |
7456 | argv[i], ret); | |
7457 | } | |
7458 | zpool_vdev_set_removed_state(zhp, guid, VDEV_AUX_NONE); | |
7459 | ||
7460 | } else if (fault) { | |
4a283c7f TH |
7461 | vdev_aux_t aux; |
7462 | if (istmp == B_FALSE) { | |
7463 | /* Force the fault to persist across imports */ | |
7464 | aux = VDEV_AUX_EXTERNAL_PERSIST; | |
7465 | } else { | |
7466 | aux = VDEV_AUX_EXTERNAL; | |
7467 | } | |
7468 | ||
7469 | if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0) | |
7470 | ret = 1; | |
7471 | } else { | |
7472 | if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) | |
7473 | ret = 1; | |
7474 | } | |
34dc7c2f BB |
7475 | } |
7476 | ||
7477 | zpool_close(zhp); | |
7478 | ||
7479 | return (ret); | |
7480 | } | |
7481 | ||
7482 | /* | |
a9520e6e | 7483 | * zpool clear [-nF]|[--power] <pool> [device] |
34dc7c2f BB |
7484 | * |
7485 | * Clear all errors associated with a pool or a particular device. | |
7486 | */ | |
7487 | int | |
7488 | zpool_do_clear(int argc, char **argv) | |
7489 | { | |
428870ff | 7490 | int c; |
34dc7c2f | 7491 | int ret = 0; |
428870ff BB |
7492 | boolean_t dryrun = B_FALSE; |
7493 | boolean_t do_rewind = B_FALSE; | |
7494 | boolean_t xtreme_rewind = B_FALSE; | |
a9520e6e | 7495 | boolean_t is_power_on = B_FALSE; |
428870ff BB |
7496 | uint32_t rewind_policy = ZPOOL_NO_REWIND; |
7497 | nvlist_t *policy = NULL; | |
34dc7c2f BB |
7498 | zpool_handle_t *zhp; |
7499 | char *pool, *device; | |
7500 | ||
a9520e6e | 7501 | struct option long_options[] = { |
b1e46f86 | 7502 | {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, |
a9520e6e TH |
7503 | {0, 0, 0, 0} |
7504 | }; | |
7505 | ||
428870ff | 7506 | /* check options */ |
a9520e6e TH |
7507 | while ((c = getopt_long(argc, argv, "FnX", long_options, |
7508 | NULL)) != -1) { | |
428870ff BB |
7509 | switch (c) { |
7510 | case 'F': | |
7511 | do_rewind = B_TRUE; | |
7512 | break; | |
7513 | case 'n': | |
7514 | dryrun = B_TRUE; | |
7515 | break; | |
7516 | case 'X': | |
7517 | xtreme_rewind = B_TRUE; | |
7518 | break; | |
b1e46f86 | 7519 | case ZPOOL_OPTION_POWER: |
a9520e6e TH |
7520 | is_power_on = B_TRUE; |
7521 | break; | |
428870ff BB |
7522 | case '?': |
7523 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7524 | optopt); | |
7525 | usage(B_FALSE); | |
7526 | } | |
7527 | } | |
7528 | ||
a9520e6e TH |
7529 | if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT")) |
7530 | is_power_on = B_TRUE; | |
7531 | ||
428870ff BB |
7532 | argc -= optind; |
7533 | argv += optind; | |
7534 | ||
7535 | if (argc < 1) { | |
34dc7c2f BB |
7536 | (void) fprintf(stderr, gettext("missing pool name\n")); |
7537 | usage(B_FALSE); | |
7538 | } | |
7539 | ||
428870ff | 7540 | if (argc > 2) { |
34dc7c2f BB |
7541 | (void) fprintf(stderr, gettext("too many arguments\n")); |
7542 | usage(B_FALSE); | |
7543 | } | |
7544 | ||
428870ff BB |
7545 | if ((dryrun || xtreme_rewind) && !do_rewind) { |
7546 | (void) fprintf(stderr, | |
7547 | gettext("-n or -X only meaningful with -F\n")); | |
7548 | usage(B_FALSE); | |
7549 | } | |
7550 | if (dryrun) | |
7551 | rewind_policy = ZPOOL_TRY_REWIND; | |
7552 | else if (do_rewind) | |
7553 | rewind_policy = ZPOOL_DO_REWIND; | |
7554 | if (xtreme_rewind) | |
7555 | rewind_policy |= ZPOOL_EXTREME_REWIND; | |
7556 | ||
7557 | /* In future, further rewind policy choices can be passed along here */ | |
7558 | if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || | |
8a393be3 PZ |
7559 | nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, |
7560 | rewind_policy) != 0) { | |
428870ff | 7561 | return (1); |
8a393be3 | 7562 | } |
428870ff BB |
7563 | |
7564 | pool = argv[0]; | |
7565 | device = argc == 2 ? argv[1] : NULL; | |
34dc7c2f | 7566 | |
428870ff BB |
7567 | if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { |
7568 | nvlist_free(policy); | |
34dc7c2f | 7569 | return (1); |
428870ff | 7570 | } |
34dc7c2f | 7571 | |
a9520e6e TH |
7572 | if (is_power_on) { |
7573 | if (device == NULL) { | |
7574 | zpool_power_on_pool_and_wait_for_devices(zhp); | |
7575 | } else { | |
7576 | zpool_power_on_and_disk_wait(zhp, device); | |
7577 | } | |
7578 | } | |
7579 | ||
428870ff | 7580 | if (zpool_clear(zhp, device, policy) != 0) |
34dc7c2f BB |
7581 | ret = 1; |
7582 | ||
7583 | zpool_close(zhp); | |
7584 | ||
428870ff BB |
7585 | nvlist_free(policy); |
7586 | ||
34dc7c2f BB |
7587 | return (ret); |
7588 | } | |
7589 | ||
3541dc6d GA |
7590 | /* |
7591 | * zpool reguid <pool> | |
7592 | */ | |
7593 | int | |
7594 | zpool_do_reguid(int argc, char **argv) | |
7595 | { | |
7596 | int c; | |
7597 | char *poolname; | |
7598 | zpool_handle_t *zhp; | |
7599 | int ret = 0; | |
7600 | ||
7601 | /* check options */ | |
7602 | while ((c = getopt(argc, argv, "")) != -1) { | |
7603 | switch (c) { | |
7604 | case '?': | |
7605 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7606 | optopt); | |
7607 | usage(B_FALSE); | |
7608 | } | |
7609 | } | |
7610 | ||
7611 | argc -= optind; | |
7612 | argv += optind; | |
7613 | ||
7614 | /* get pool name and check number of arguments */ | |
7615 | if (argc < 1) { | |
7616 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
7617 | usage(B_FALSE); | |
7618 | } | |
7619 | ||
7620 | if (argc > 1) { | |
7621 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
7622 | usage(B_FALSE); | |
7623 | } | |
7624 | ||
7625 | poolname = argv[0]; | |
7626 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
7627 | return (1); | |
7628 | ||
7629 | ret = zpool_reguid(zhp); | |
7630 | ||
7631 | zpool_close(zhp); | |
7632 | return (ret); | |
7633 | } | |
7634 | ||
7635 | ||
1bd201e7 CS |
7636 | /* |
7637 | * zpool reopen <pool> | |
7638 | * | |
7639 | * Reopen the pool so that the kernel can update the sizes of all vdevs. | |
1bd201e7 CS |
7640 | */ |
7641 | int | |
7642 | zpool_do_reopen(int argc, char **argv) | |
7643 | { | |
5853fe79 | 7644 | int c; |
1bd201e7 | 7645 | int ret = 0; |
d3f2cd7e | 7646 | boolean_t scrub_restart = B_TRUE; |
1bd201e7 | 7647 | |
5853fe79 | 7648 | /* check options */ |
d3f2cd7e | 7649 | while ((c = getopt(argc, argv, "n")) != -1) { |
5853fe79 | 7650 | switch (c) { |
d3f2cd7e AB |
7651 | case 'n': |
7652 | scrub_restart = B_FALSE; | |
7653 | break; | |
5853fe79 GW |
7654 | case '?': |
7655 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7656 | optopt); | |
7657 | usage(B_FALSE); | |
7658 | } | |
7659 | } | |
7660 | ||
d3f2cd7e AB |
7661 | argc -= optind; |
7662 | argv += optind; | |
1bd201e7 | 7663 | |
d3f2cd7e | 7664 | /* if argc == 0 we will execute zpool_reopen_one on all pools */ |
2a673e76 AJ |
7665 | ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
7666 | B_FALSE, zpool_reopen_one, &scrub_restart); | |
1bd201e7 | 7667 | |
1bd201e7 CS |
7668 | return (ret); |
7669 | } | |
7670 | ||
34dc7c2f BB |
7671 | typedef struct scrub_cbdata { |
7672 | int cb_type; | |
0ea05c64 | 7673 | pool_scrub_cmd_t cb_scrub_cmd; |
34dc7c2f BB |
7674 | } scrub_cbdata_t; |
7675 | ||
d2734cce SD |
7676 | static boolean_t |
7677 | zpool_has_checkpoint(zpool_handle_t *zhp) | |
7678 | { | |
7679 | nvlist_t *config, *nvroot; | |
7680 | ||
7681 | config = zpool_get_config(zhp, NULL); | |
7682 | ||
7683 | if (config != NULL) { | |
7684 | pool_checkpoint_stat_t *pcs = NULL; | |
7685 | uint_t c; | |
7686 | ||
7687 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); | |
7688 | (void) nvlist_lookup_uint64_array(nvroot, | |
7689 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
7690 | ||
7691 | if (pcs == NULL || pcs->pcs_state == CS_NONE) | |
7692 | return (B_FALSE); | |
7693 | ||
7694 | assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS || | |
7695 | pcs->pcs_state == CS_CHECKPOINT_DISCARDING); | |
7696 | return (B_TRUE); | |
7697 | } | |
7698 | ||
7699 | return (B_FALSE); | |
7700 | } | |
7701 | ||
65c7cc49 | 7702 | static int |
34dc7c2f BB |
7703 | scrub_callback(zpool_handle_t *zhp, void *data) |
7704 | { | |
7705 | scrub_cbdata_t *cb = data; | |
7706 | int err; | |
7707 | ||
7708 | /* | |
7709 | * Ignore faulted pools. | |
7710 | */ | |
7711 | if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { | |
80a91e74 | 7712 | (void) fprintf(stderr, gettext("cannot scan '%s': pool is " |
34dc7c2f BB |
7713 | "currently unavailable\n"), zpool_get_name(zhp)); |
7714 | return (1); | |
7715 | } | |
7716 | ||
0ea05c64 | 7717 | err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd); |
34dc7c2f | 7718 | |
d2734cce SD |
7719 | if (err == 0 && zpool_has_checkpoint(zhp) && |
7720 | cb->cb_type == POOL_SCAN_SCRUB) { | |
7721 | (void) printf(gettext("warning: will not scrub state that " | |
7722 | "belongs to the checkpoint of pool '%s'\n"), | |
7723 | zpool_get_name(zhp)); | |
7724 | } | |
7725 | ||
34dc7c2f BB |
7726 | return (err != 0); |
7727 | } | |
7728 | ||
e60e158e JG |
7729 | static int |
7730 | wait_callback(zpool_handle_t *zhp, void *data) | |
7731 | { | |
7732 | zpool_wait_activity_t *act = data; | |
7733 | return (zpool_wait(zhp, *act)); | |
7734 | } | |
7735 | ||
34dc7c2f | 7736 | /* |
482eeef8 | 7737 | * zpool scrub [-s | -p] [-w] [-e] <pool> ... |
34dc7c2f | 7738 | * |
482eeef8 | 7739 | * -e Only scrub blocks in the error log. |
34dc7c2f | 7740 | * -s Stop. Stops any in-progress scrub. |
0ea05c64 | 7741 | * -p Pause. Pause in-progress scrub. |
e60e158e | 7742 | * -w Wait. Blocks until scrub has completed. |
34dc7c2f BB |
7743 | */ |
7744 | int | |
7745 | zpool_do_scrub(int argc, char **argv) | |
7746 | { | |
7747 | int c; | |
7748 | scrub_cbdata_t cb; | |
e60e158e JG |
7749 | boolean_t wait = B_FALSE; |
7750 | int error; | |
34dc7c2f | 7751 | |
428870ff | 7752 | cb.cb_type = POOL_SCAN_SCRUB; |
0ea05c64 | 7753 | cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; |
34dc7c2f | 7754 | |
482eeef8 GA |
7755 | boolean_t is_error_scrub = B_FALSE; |
7756 | boolean_t is_pause = B_FALSE; | |
7757 | boolean_t is_stop = B_FALSE; | |
7758 | ||
34dc7c2f | 7759 | /* check options */ |
482eeef8 | 7760 | while ((c = getopt(argc, argv, "spwe")) != -1) { |
34dc7c2f | 7761 | switch (c) { |
482eeef8 GA |
7762 | case 'e': |
7763 | is_error_scrub = B_TRUE; | |
7764 | break; | |
34dc7c2f | 7765 | case 's': |
482eeef8 | 7766 | is_stop = B_TRUE; |
34dc7c2f | 7767 | break; |
0ea05c64 | 7768 | case 'p': |
482eeef8 | 7769 | is_pause = B_TRUE; |
0ea05c64 | 7770 | break; |
e60e158e JG |
7771 | case 'w': |
7772 | wait = B_TRUE; | |
7773 | break; | |
34dc7c2f BB |
7774 | case '?': |
7775 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7776 | optopt); | |
7777 | usage(B_FALSE); | |
7778 | } | |
7779 | } | |
7780 | ||
482eeef8 GA |
7781 | if (is_pause && is_stop) { |
7782 | (void) fprintf(stderr, gettext("invalid option " | |
7783 | "combination :-s and -p are mutually exclusive\n")); | |
0ea05c64 | 7784 | usage(B_FALSE); |
482eeef8 GA |
7785 | } else { |
7786 | if (is_error_scrub) | |
7787 | cb.cb_type = POOL_SCAN_ERRORSCRUB; | |
7788 | ||
7789 | if (is_pause) { | |
7790 | cb.cb_scrub_cmd = POOL_SCRUB_PAUSE; | |
7791 | } else if (is_stop) { | |
7792 | cb.cb_type = POOL_SCAN_NONE; | |
7793 | } else { | |
7794 | cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; | |
7795 | } | |
0ea05c64 AP |
7796 | } |
7797 | ||
e60e158e JG |
7798 | if (wait && (cb.cb_type == POOL_SCAN_NONE || |
7799 | cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) { | |
7800 | (void) fprintf(stderr, gettext("invalid option combination: " | |
7801 | "-w cannot be used with -p or -s\n")); | |
7802 | usage(B_FALSE); | |
7803 | } | |
7804 | ||
34dc7c2f BB |
7805 | argc -= optind; |
7806 | argv += optind; | |
7807 | ||
7808 | if (argc < 1) { | |
7809 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
7810 | usage(B_FALSE); | |
7811 | } | |
7812 | ||
2a673e76 AJ |
7813 | error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
7814 | B_FALSE, scrub_callback, &cb); | |
e60e158e JG |
7815 | |
7816 | if (wait && !error) { | |
7817 | zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB; | |
2a673e76 AJ |
7818 | error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
7819 | B_FALSE, wait_callback, &act); | |
e60e158e JG |
7820 | } |
7821 | ||
7822 | return (error); | |
34dc7c2f BB |
7823 | } |
7824 | ||
80a91e74 TC |
7825 | /* |
7826 | * zpool resilver <pool> ... | |
7827 | * | |
7828 | * Restarts any in-progress resilver | |
7829 | */ | |
7830 | int | |
7831 | zpool_do_resilver(int argc, char **argv) | |
7832 | { | |
7833 | int c; | |
7834 | scrub_cbdata_t cb; | |
7835 | ||
7836 | cb.cb_type = POOL_SCAN_RESILVER; | |
7837 | cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; | |
80a91e74 TC |
7838 | |
7839 | /* check options */ | |
7840 | while ((c = getopt(argc, argv, "")) != -1) { | |
7841 | switch (c) { | |
7842 | case '?': | |
7843 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7844 | optopt); | |
7845 | usage(B_FALSE); | |
7846 | } | |
7847 | } | |
7848 | ||
7849 | argc -= optind; | |
7850 | argv += optind; | |
7851 | ||
7852 | if (argc < 1) { | |
7853 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
7854 | usage(B_FALSE); | |
7855 | } | |
7856 | ||
2a673e76 AJ |
7857 | return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
7858 | B_FALSE, scrub_callback, &cb)); | |
80a91e74 TC |
7859 | } |
7860 | ||
1b939560 BB |
7861 | /* |
7862 | * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...] | |
7863 | * | |
7864 | * -c Cancel. Ends any in-progress trim. | |
7865 | * -d Secure trim. Requires kernel and device support. | |
7866 | * -r <rate> Sets the TRIM rate in bytes (per second). Supports | |
7867 | * adding a multiplier suffix such as 'k' or 'm'. | |
7868 | * -s Suspend. TRIM can then be restarted with no flags. | |
2288d419 | 7869 | * -w Wait. Blocks until trimming has completed. |
1b939560 BB |
7870 | */ |
7871 | int | |
7872 | zpool_do_trim(int argc, char **argv) | |
7873 | { | |
7874 | struct option long_options[] = { | |
7875 | {"cancel", no_argument, NULL, 'c'}, | |
7876 | {"secure", no_argument, NULL, 'd'}, | |
7877 | {"rate", required_argument, NULL, 'r'}, | |
7878 | {"suspend", no_argument, NULL, 's'}, | |
2288d419 | 7879 | {"wait", no_argument, NULL, 'w'}, |
1b939560 BB |
7880 | {0, 0, 0, 0} |
7881 | }; | |
7882 | ||
7883 | pool_trim_func_t cmd_type = POOL_TRIM_START; | |
7884 | uint64_t rate = 0; | |
7885 | boolean_t secure = B_FALSE; | |
2288d419 | 7886 | boolean_t wait = B_FALSE; |
1b939560 BB |
7887 | |
7888 | int c; | |
2288d419 | 7889 | while ((c = getopt_long(argc, argv, "cdr:sw", long_options, NULL)) |
1b939560 BB |
7890 | != -1) { |
7891 | switch (c) { | |
7892 | case 'c': | |
7893 | if (cmd_type != POOL_TRIM_START && | |
7894 | cmd_type != POOL_TRIM_CANCEL) { | |
7895 | (void) fprintf(stderr, gettext("-c cannot be " | |
7896 | "combined with other options\n")); | |
7897 | usage(B_FALSE); | |
7898 | } | |
7899 | cmd_type = POOL_TRIM_CANCEL; | |
7900 | break; | |
7901 | case 'd': | |
7902 | if (cmd_type != POOL_TRIM_START) { | |
7903 | (void) fprintf(stderr, gettext("-d cannot be " | |
7904 | "combined with the -c or -s options\n")); | |
7905 | usage(B_FALSE); | |
7906 | } | |
7907 | secure = B_TRUE; | |
7908 | break; | |
7909 | case 'r': | |
7910 | if (cmd_type != POOL_TRIM_START) { | |
7911 | (void) fprintf(stderr, gettext("-r cannot be " | |
7912 | "combined with the -c or -s options\n")); | |
7913 | usage(B_FALSE); | |
7914 | } | |
a4cecfbd TS |
7915 | if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) { |
7916 | (void) fprintf(stderr, "%s: %s\n", | |
7917 | gettext("invalid value for rate"), | |
7918 | libzfs_error_description(g_zfs)); | |
1b939560 BB |
7919 | usage(B_FALSE); |
7920 | } | |
7921 | break; | |
7922 | case 's': | |
7923 | if (cmd_type != POOL_TRIM_START && | |
7924 | cmd_type != POOL_TRIM_SUSPEND) { | |
7925 | (void) fprintf(stderr, gettext("-s cannot be " | |
7926 | "combined with other options\n")); | |
7927 | usage(B_FALSE); | |
7928 | } | |
7929 | cmd_type = POOL_TRIM_SUSPEND; | |
7930 | break; | |
2288d419 BB |
7931 | case 'w': |
7932 | wait = B_TRUE; | |
7933 | break; | |
1b939560 BB |
7934 | case '?': |
7935 | if (optopt != 0) { | |
7936 | (void) fprintf(stderr, | |
7937 | gettext("invalid option '%c'\n"), optopt); | |
7938 | } else { | |
7939 | (void) fprintf(stderr, | |
7940 | gettext("invalid option '%s'\n"), | |
7941 | argv[optind - 1]); | |
7942 | } | |
7943 | usage(B_FALSE); | |
7944 | } | |
7945 | } | |
7946 | ||
7947 | argc -= optind; | |
7948 | argv += optind; | |
7949 | ||
7950 | if (argc < 1) { | |
7951 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
7952 | usage(B_FALSE); | |
7953 | return (-1); | |
7954 | } | |
7955 | ||
2288d419 BB |
7956 | if (wait && (cmd_type != POOL_TRIM_START)) { |
7957 | (void) fprintf(stderr, gettext("-w cannot be used with -c or " | |
7958 | "-s\n")); | |
7959 | usage(B_FALSE); | |
7960 | } | |
7961 | ||
1b939560 BB |
7962 | char *poolname = argv[0]; |
7963 | zpool_handle_t *zhp = zpool_open(g_zfs, poolname); | |
7964 | if (zhp == NULL) | |
7965 | return (-1); | |
7966 | ||
7967 | trimflags_t trim_flags = { | |
7968 | .secure = secure, | |
7969 | .rate = rate, | |
2288d419 | 7970 | .wait = wait, |
1b939560 BB |
7971 | }; |
7972 | ||
7973 | nvlist_t *vdevs = fnvlist_alloc(); | |
7974 | if (argc == 1) { | |
7975 | /* no individual leaf vdevs specified, so add them all */ | |
7976 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
7977 | nvlist_t *nvroot = fnvlist_lookup_nvlist(config, | |
7978 | ZPOOL_CONFIG_VDEV_TREE); | |
7979 | zpool_collect_leaves(zhp, nvroot, vdevs); | |
7980 | trim_flags.fullpool = B_TRUE; | |
7981 | } else { | |
7982 | trim_flags.fullpool = B_FALSE; | |
7983 | for (int i = 1; i < argc; i++) { | |
7984 | fnvlist_add_boolean(vdevs, argv[i]); | |
7985 | } | |
7986 | } | |
7987 | ||
7988 | int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags); | |
7989 | ||
7990 | fnvlist_free(vdevs); | |
7991 | zpool_close(zhp); | |
7992 | ||
7993 | return (error); | |
7994 | } | |
80a91e74 | 7995 | |
9a49d3f3 BB |
7996 | /* |
7997 | * Converts a total number of seconds to a human readable string broken | |
7998 | * down in to days/hours/minutes/seconds. | |
7999 | */ | |
8000 | static void | |
8001 | secs_to_dhms(uint64_t total, char *buf) | |
8002 | { | |
8003 | uint64_t days = total / 60 / 60 / 24; | |
8004 | uint64_t hours = (total / 60 / 60) % 24; | |
8005 | uint64_t mins = (total / 60) % 60; | |
8006 | uint64_t secs = (total % 60); | |
8007 | ||
8008 | if (days > 0) { | |
8009 | (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu", | |
8010 | (u_longlong_t)days, (u_longlong_t)hours, | |
8011 | (u_longlong_t)mins, (u_longlong_t)secs); | |
8012 | } else { | |
8013 | (void) sprintf(buf, "%02llu:%02llu:%02llu", | |
8014 | (u_longlong_t)hours, (u_longlong_t)mins, | |
8015 | (u_longlong_t)secs); | |
8016 | } | |
8017 | } | |
8018 | ||
482eeef8 GA |
8019 | /* |
8020 | * Print out detailed error scrub status. | |
8021 | */ | |
8022 | static void | |
8023 | print_err_scrub_status(pool_scan_stat_t *ps) | |
8024 | { | |
8025 | time_t start, end, pause; | |
8026 | uint64_t total_secs_left; | |
8027 | uint64_t secs_left, mins_left, hours_left, days_left; | |
8028 | uint64_t examined, to_be_examined; | |
8029 | ||
8030 | if (ps == NULL || ps->pss_error_scrub_func != POOL_SCAN_ERRORSCRUB) { | |
8031 | return; | |
8032 | } | |
8033 | ||
8034 | (void) printf(gettext(" scrub: ")); | |
8035 | ||
8036 | start = ps->pss_error_scrub_start; | |
8037 | end = ps->pss_error_scrub_end; | |
8038 | pause = ps->pss_pass_error_scrub_pause; | |
8039 | examined = ps->pss_error_scrub_examined; | |
8040 | to_be_examined = ps->pss_error_scrub_to_be_examined; | |
8041 | ||
8042 | assert(ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB); | |
8043 | ||
8044 | if (ps->pss_error_scrub_state == DSS_FINISHED) { | |
8045 | total_secs_left = end - start; | |
8046 | days_left = total_secs_left / 60 / 60 / 24; | |
8047 | hours_left = (total_secs_left / 60 / 60) % 24; | |
8048 | mins_left = (total_secs_left / 60) % 60; | |
8049 | secs_left = (total_secs_left % 60); | |
8050 | ||
8051 | (void) printf(gettext("scrubbed %llu error blocks in %llu days " | |
8052 | "%02llu:%02llu:%02llu on %s"), (u_longlong_t)examined, | |
8053 | (u_longlong_t)days_left, (u_longlong_t)hours_left, | |
8054 | (u_longlong_t)mins_left, (u_longlong_t)secs_left, | |
8055 | ctime(&end)); | |
8056 | ||
8057 | return; | |
8058 | } else if (ps->pss_error_scrub_state == DSS_CANCELED) { | |
8059 | (void) printf(gettext("error scrub canceled on %s"), | |
8060 | ctime(&end)); | |
8061 | return; | |
8062 | } | |
8063 | assert(ps->pss_error_scrub_state == DSS_ERRORSCRUBBING); | |
8064 | ||
8065 | /* Error scrub is in progress. */ | |
8066 | if (pause == 0) { | |
8067 | (void) printf(gettext("error scrub in progress since %s"), | |
8068 | ctime(&start)); | |
8069 | } else { | |
8070 | (void) printf(gettext("error scrub paused since %s"), | |
8071 | ctime(&pause)); | |
8072 | (void) printf(gettext("\terror scrub started on %s"), | |
8073 | ctime(&start)); | |
8074 | } | |
8075 | ||
8076 | double fraction_done = (double)examined / (to_be_examined + examined); | |
8077 | (void) printf(gettext("\t%.2f%% done, issued I/O for %llu error" | |
8078 | " blocks"), 100 * fraction_done, (u_longlong_t)examined); | |
8079 | ||
8080 | (void) printf("\n"); | |
8081 | } | |
8082 | ||
34dc7c2f BB |
8083 | /* |
8084 | * Print out detailed scrub status. | |
8085 | */ | |
a1d477c2 | 8086 | static void |
9a49d3f3 | 8087 | print_scan_scrub_resilver_status(pool_scan_stat_t *ps) |
34dc7c2f | 8088 | { |
0ea05c64 | 8089 | time_t start, end, pause; |
fa7b2390 | 8090 | uint64_t pass_scanned, scanned, pass_issued, issued, total_s, total_i; |
9a49d3f3 | 8091 | uint64_t elapsed, scan_rate, issue_rate; |
34dc7c2f | 8092 | double fraction_done; |
fa7b2390 AM |
8093 | char processed_buf[7], scanned_buf[7], issued_buf[7], total_s_buf[7]; |
8094 | char total_i_buf[7], srate_buf[7], irate_buf[7], time_buf[32]; | |
34dc7c2f | 8095 | |
9fb2771a TH |
8096 | printf(" "); |
8097 | printf_color(ANSI_BOLD, gettext("scan:")); | |
8098 | printf(" "); | |
34dc7c2f | 8099 | |
428870ff BB |
8100 | /* If there's never been a scan, there's not much to say. */ |
8101 | if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || | |
8102 | ps->pss_func >= POOL_SCAN_FUNCS) { | |
34dc7c2f BB |
8103 | (void) printf(gettext("none requested\n")); |
8104 | return; | |
8105 | } | |
8106 | ||
428870ff BB |
8107 | start = ps->pss_start_time; |
8108 | end = ps->pss_end_time; | |
0ea05c64 | 8109 | pause = ps->pss_pass_scrub_pause; |
d4a72f23 | 8110 | |
e7fbeb60 | 8111 | zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf)); |
34dc7c2f | 8112 | |
c85ac731 BB |
8113 | int is_resilver = ps->pss_func == POOL_SCAN_RESILVER; |
8114 | int is_scrub = ps->pss_func == POOL_SCAN_SCRUB; | |
8115 | assert(is_resilver || is_scrub); | |
d4a72f23 TC |
8116 | |
8117 | /* Scan is finished or canceled. */ | |
428870ff | 8118 | if (ps->pss_state == DSS_FINISHED) { |
9a49d3f3 | 8119 | secs_to_dhms(end - start, time_buf); |
428870ff | 8120 | |
c85ac731 | 8121 | if (is_scrub) { |
d4a72f23 | 8122 | (void) printf(gettext("scrub repaired %s " |
9a49d3f3 BB |
8123 | "in %s with %llu errors on %s"), processed_buf, |
8124 | time_buf, (u_longlong_t)ps->pss_errors, | |
8125 | ctime(&end)); | |
c85ac731 | 8126 | } else if (is_resilver) { |
d4a72f23 | 8127 | (void) printf(gettext("resilvered %s " |
9a49d3f3 BB |
8128 | "in %s with %llu errors on %s"), processed_buf, |
8129 | time_buf, (u_longlong_t)ps->pss_errors, | |
8130 | ctime(&end)); | |
428870ff | 8131 | } |
428870ff BB |
8132 | return; |
8133 | } else if (ps->pss_state == DSS_CANCELED) { | |
c85ac731 | 8134 | if (is_scrub) { |
428870ff BB |
8135 | (void) printf(gettext("scrub canceled on %s"), |
8136 | ctime(&end)); | |
c85ac731 | 8137 | } else if (is_resilver) { |
428870ff BB |
8138 | (void) printf(gettext("resilver canceled on %s"), |
8139 | ctime(&end)); | |
8140 | } | |
34dc7c2f BB |
8141 | return; |
8142 | } | |
8143 | ||
428870ff BB |
8144 | assert(ps->pss_state == DSS_SCANNING); |
8145 | ||
d4a72f23 | 8146 | /* Scan is in progress. Resilvers can't be paused. */ |
c85ac731 | 8147 | if (is_scrub) { |
0ea05c64 AP |
8148 | if (pause == 0) { |
8149 | (void) printf(gettext("scrub in progress since %s"), | |
8150 | ctime(&start)); | |
8151 | } else { | |
d4a72f23 TC |
8152 | (void) printf(gettext("scrub paused since %s"), |
8153 | ctime(&pause)); | |
8154 | (void) printf(gettext("\tscrub started on %s"), | |
0ea05c64 AP |
8155 | ctime(&start)); |
8156 | } | |
c85ac731 | 8157 | } else if (is_resilver) { |
428870ff BB |
8158 | (void) printf(gettext("resilver in progress since %s"), |
8159 | ctime(&start)); | |
8160 | } | |
34dc7c2f | 8161 | |
d4a72f23 TC |
8162 | scanned = ps->pss_examined; |
8163 | pass_scanned = ps->pss_pass_exam; | |
8164 | issued = ps->pss_issued; | |
8165 | pass_issued = ps->pss_pass_issued; | |
fa7b2390 AM |
8166 | total_s = ps->pss_to_examine; |
8167 | total_i = ps->pss_to_examine - ps->pss_skipped; | |
428870ff | 8168 | |
d4a72f23 | 8169 | /* we are only done with a block once we have issued the IO for it */ |
fa7b2390 | 8170 | fraction_done = (double)issued / total_i; |
d4a72f23 TC |
8171 | |
8172 | /* elapsed time for this pass, rounding up to 1 if it's 0 */ | |
428870ff | 8173 | elapsed = time(NULL) - ps->pss_pass_start; |
0ea05c64 | 8174 | elapsed -= ps->pss_pass_scrub_spent_paused; |
d4a72f23 TC |
8175 | elapsed = (elapsed != 0) ? elapsed : 1; |
8176 | ||
8177 | scan_rate = pass_scanned / elapsed; | |
8178 | issue_rate = pass_issued / elapsed; | |
d4a72f23 TC |
8179 | |
8180 | /* format all of the numbers we will be reporting */ | |
8181 | zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf)); | |
8182 | zfs_nicebytes(issued, issued_buf, sizeof (issued_buf)); | |
fa7b2390 AM |
8183 | zfs_nicebytes(total_s, total_s_buf, sizeof (total_s_buf)); |
8184 | zfs_nicebytes(total_i, total_i_buf, sizeof (total_i_buf)); | |
428870ff | 8185 | |
d4a72f23 | 8186 | /* do not print estimated time if we have a paused scrub */ |
fa7b2390 AM |
8187 | (void) printf(gettext("\t%s / %s scanned"), scanned_buf, total_s_buf); |
8188 | if (pause == 0 && scan_rate > 0) { | |
8189 | zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf)); | |
8190 | (void) printf(gettext(" at %s/s"), srate_buf); | |
572e2857 | 8191 | } |
fa7b2390 AM |
8192 | (void) printf(gettext(", %s / %s issued"), issued_buf, total_i_buf); |
8193 | if (pause == 0 && issue_rate > 0) { | |
8194 | zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf)); | |
8195 | (void) printf(gettext(" at %s/s"), irate_buf); | |
8196 | } | |
8197 | (void) printf(gettext("\n")); | |
428870ff | 8198 | |
c85ac731 | 8199 | if (is_resilver) { |
d4a72f23 | 8200 | (void) printf(gettext("\t%s resilvered, %.2f%% done"), |
428870ff | 8201 | processed_buf, 100 * fraction_done); |
c85ac731 | 8202 | } else if (is_scrub) { |
d4a72f23 | 8203 | (void) printf(gettext("\t%s repaired, %.2f%% done"), |
428870ff BB |
8204 | processed_buf, 100 * fraction_done); |
8205 | } | |
d4a72f23 TC |
8206 | |
8207 | if (pause == 0) { | |
c85ac731 BB |
8208 | /* |
8209 | * Only provide an estimate iff: | |
fa7b2390 | 8210 | * 1) we haven't yet issued all we expected, and |
c85ac731 BB |
8211 | * 2) the issue rate exceeds 10 MB/s, and |
8212 | * 3) it's either: | |
8213 | * a) a resilver which has started repairs, or | |
8214 | * b) a scrub which has entered the issue phase. | |
8215 | */ | |
fa7b2390 | 8216 | if (total_i >= issued && issue_rate >= 10 * 1024 * 1024 && |
c85ac731 BB |
8217 | ((is_resilver && ps->pss_processed > 0) || |
8218 | (is_scrub && issued > 0))) { | |
fa7b2390 | 8219 | secs_to_dhms((total_i - issued) / issue_rate, time_buf); |
9a49d3f3 BB |
8220 | (void) printf(gettext(", %s to go\n"), time_buf); |
8221 | } else { | |
8222 | (void) printf(gettext(", no estimated " | |
8223 | "completion time\n")); | |
8224 | } | |
8225 | } else { | |
8226 | (void) printf(gettext("\n")); | |
8227 | } | |
8228 | } | |
8229 | ||
8230 | static void | |
fa7b2390 | 8231 | print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, uint_t c, char *vdev_name) |
9a49d3f3 BB |
8232 | { |
8233 | if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE) | |
8234 | return; | |
8235 | ||
8236 | printf(" "); | |
8237 | printf_color(ANSI_BOLD, gettext("scan:")); | |
8238 | printf(" "); | |
8239 | ||
8240 | uint64_t bytes_scanned = vrs->vrs_bytes_scanned; | |
8241 | uint64_t bytes_issued = vrs->vrs_bytes_issued; | |
8242 | uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt; | |
fa7b2390 AM |
8243 | uint64_t bytes_est_s = vrs->vrs_bytes_est; |
8244 | uint64_t bytes_est_i = vrs->vrs_bytes_est; | |
8245 | if (c > offsetof(vdev_rebuild_stat_t, vrs_pass_bytes_skipped) / 8) | |
8246 | bytes_est_i -= vrs->vrs_pass_bytes_skipped; | |
9a49d3f3 BB |
8247 | uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned / |
8248 | (vrs->vrs_pass_time_ms + 1)) * 1000; | |
8249 | uint64_t issue_rate = (vrs->vrs_pass_bytes_issued / | |
8250 | (vrs->vrs_pass_time_ms + 1)) * 1000; | |
8251 | double scan_pct = MIN((double)bytes_scanned * 100 / | |
fa7b2390 | 8252 | (bytes_est_s + 1), 100); |
9a49d3f3 BB |
8253 | |
8254 | /* Format all of the numbers we will be reporting */ | |
8255 | char bytes_scanned_buf[7], bytes_issued_buf[7]; | |
fa7b2390 | 8256 | char bytes_rebuilt_buf[7], bytes_est_s_buf[7], bytes_est_i_buf[7]; |
9a49d3f3 BB |
8257 | char scan_rate_buf[7], issue_rate_buf[7], time_buf[32]; |
8258 | zfs_nicebytes(bytes_scanned, bytes_scanned_buf, | |
8259 | sizeof (bytes_scanned_buf)); | |
8260 | zfs_nicebytes(bytes_issued, bytes_issued_buf, | |
8261 | sizeof (bytes_issued_buf)); | |
8262 | zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf, | |
8263 | sizeof (bytes_rebuilt_buf)); | |
fa7b2390 AM |
8264 | zfs_nicebytes(bytes_est_s, bytes_est_s_buf, sizeof (bytes_est_s_buf)); |
8265 | zfs_nicebytes(bytes_est_i, bytes_est_i_buf, sizeof (bytes_est_i_buf)); | |
9a49d3f3 BB |
8266 | |
8267 | time_t start = vrs->vrs_start_time; | |
8268 | time_t end = vrs->vrs_end_time; | |
8269 | ||
8270 | /* Rebuild is finished or canceled. */ | |
8271 | if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) { | |
8272 | secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf); | |
8273 | (void) printf(gettext("resilvered (%s) %s in %s " | |
8274 | "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf, | |
8275 | time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end)); | |
8276 | return; | |
8277 | } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) { | |
8278 | (void) printf(gettext("resilver (%s) canceled on %s"), | |
8279 | vdev_name, ctime(&end)); | |
8280 | return; | |
8281 | } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { | |
8282 | (void) printf(gettext("resilver (%s) in progress since %s"), | |
8283 | vdev_name, ctime(&start)); | |
8284 | } | |
8285 | ||
8286 | assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE); | |
8287 | ||
fa7b2390 AM |
8288 | (void) printf(gettext("\t%s / %s scanned"), bytes_scanned_buf, |
8289 | bytes_est_s_buf); | |
8290 | if (scan_rate > 0) { | |
8291 | zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf)); | |
8292 | (void) printf(gettext(" at %s/s"), scan_rate_buf); | |
8293 | } | |
8294 | (void) printf(gettext(", %s / %s issued"), bytes_issued_buf, | |
8295 | bytes_est_i_buf); | |
8296 | if (issue_rate > 0) { | |
8297 | zfs_nicebytes(issue_rate, issue_rate_buf, | |
8298 | sizeof (issue_rate_buf)); | |
8299 | (void) printf(gettext(" at %s/s"), issue_rate_buf); | |
8300 | } | |
8301 | (void) printf(gettext("\n")); | |
9a49d3f3 | 8302 | |
9a49d3f3 BB |
8303 | (void) printf(gettext("\t%s resilvered, %.2f%% done"), |
8304 | bytes_rebuilt_buf, scan_pct); | |
8305 | ||
8306 | if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { | |
fa7b2390 AM |
8307 | if (bytes_est_s >= bytes_scanned && |
8308 | scan_rate >= 10 * 1024 * 1024) { | |
8309 | secs_to_dhms((bytes_est_s - bytes_scanned) / scan_rate, | |
8310 | time_buf); | |
9a49d3f3 | 8311 | (void) printf(gettext(", %s to go\n"), time_buf); |
d4a72f23 TC |
8312 | } else { |
8313 | (void) printf(gettext(", no estimated " | |
8314 | "completion time\n")); | |
8315 | } | |
8316 | } else { | |
8317 | (void) printf(gettext("\n")); | |
8318 | } | |
34dc7c2f BB |
8319 | } |
8320 | ||
d2734cce | 8321 | /* |
9a49d3f3 BB |
8322 | * Print rebuild status for top-level vdevs. |
8323 | */ | |
8324 | static void | |
8325 | print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot) | |
8326 | { | |
8327 | nvlist_t **child; | |
8328 | uint_t children; | |
8329 | ||
8330 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
8331 | &child, &children) != 0) | |
8332 | children = 0; | |
8333 | ||
8334 | for (uint_t c = 0; c < children; c++) { | |
8335 | vdev_rebuild_stat_t *vrs; | |
8336 | uint_t i; | |
8337 | ||
8338 | if (nvlist_lookup_uint64_array(child[c], | |
8339 | ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { | |
8340 | char *name = zpool_vdev_name(g_zfs, zhp, | |
8341 | child[c], VDEV_NAME_TYPE_ID); | |
fa7b2390 | 8342 | print_rebuild_status_impl(vrs, i, name); |
9a49d3f3 BB |
8343 | free(name); |
8344 | } | |
8345 | } | |
8346 | } | |
8347 | ||
8348 | /* | |
8349 | * As we don't scrub checkpointed blocks, we want to warn the user that we | |
8350 | * skipped scanning some blocks if a checkpoint exists or existed at any | |
8351 | * time during the scan. If a sequential instead of healing reconstruction | |
8352 | * was performed then the blocks were reconstructed. However, their checksums | |
8353 | * have not been verified so we still print the warning. | |
d2734cce SD |
8354 | */ |
8355 | static void | |
8356 | print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs) | |
8357 | { | |
8358 | if (ps == NULL || pcs == NULL) | |
8359 | return; | |
8360 | ||
8361 | if (pcs->pcs_state == CS_NONE || | |
8362 | pcs->pcs_state == CS_CHECKPOINT_DISCARDING) | |
8363 | return; | |
8364 | ||
8365 | assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS); | |
8366 | ||
8367 | if (ps->pss_state == DSS_NONE) | |
8368 | return; | |
8369 | ||
8370 | if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) && | |
8371 | ps->pss_end_time < pcs->pcs_start_time) | |
8372 | return; | |
8373 | ||
8374 | if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) { | |
8375 | (void) printf(gettext(" scan warning: skipped blocks " | |
8376 | "that are only referenced by the checkpoint.\n")); | |
8377 | } else { | |
8378 | assert(ps->pss_state == DSS_SCANNING); | |
8379 | (void) printf(gettext(" scan warning: skipping blocks " | |
8380 | "that are only referenced by the checkpoint.\n")); | |
8381 | } | |
8382 | } | |
8383 | ||
9a49d3f3 BB |
8384 | /* |
8385 | * Returns B_TRUE if there is an active rebuild in progress. Otherwise, | |
8386 | * B_FALSE is returned and 'rebuild_end_time' is set to the end time for | |
8387 | * the last completed (or cancelled) rebuild. | |
8388 | */ | |
8389 | static boolean_t | |
8390 | check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time) | |
8391 | { | |
8392 | nvlist_t **child; | |
8393 | uint_t children; | |
8394 | boolean_t rebuilding = B_FALSE; | |
8395 | uint64_t end_time = 0; | |
8396 | ||
8397 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
8398 | &child, &children) != 0) | |
8399 | children = 0; | |
8400 | ||
8401 | for (uint_t c = 0; c < children; c++) { | |
8402 | vdev_rebuild_stat_t *vrs; | |
8403 | uint_t i; | |
8404 | ||
8405 | if (nvlist_lookup_uint64_array(child[c], | |
8406 | ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { | |
8407 | ||
8408 | if (vrs->vrs_end_time > end_time) | |
8409 | end_time = vrs->vrs_end_time; | |
8410 | ||
8411 | if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { | |
8412 | rebuilding = B_TRUE; | |
8413 | end_time = 0; | |
8414 | break; | |
8415 | } | |
8416 | } | |
8417 | } | |
8418 | ||
8419 | if (rebuild_end_time != NULL) | |
8420 | *rebuild_end_time = end_time; | |
8421 | ||
8422 | return (rebuilding); | |
8423 | } | |
8424 | ||
8425 | /* | |
8426 | * Print the scan status. | |
8427 | */ | |
8428 | static void | |
8429 | print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot) | |
8430 | { | |
8431 | uint64_t rebuild_end_time = 0, resilver_end_time = 0; | |
8432 | boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE; | |
482eeef8 | 8433 | boolean_t have_errorscrub = B_FALSE; |
9a49d3f3 BB |
8434 | boolean_t active_resilver = B_FALSE; |
8435 | pool_checkpoint_stat_t *pcs = NULL; | |
8436 | pool_scan_stat_t *ps = NULL; | |
8437 | uint_t c; | |
482eeef8 | 8438 | time_t scrub_start = 0, errorscrub_start = 0; |
9a49d3f3 BB |
8439 | |
8440 | if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS, | |
8441 | (uint64_t **)&ps, &c) == 0) { | |
8442 | if (ps->pss_func == POOL_SCAN_RESILVER) { | |
8443 | resilver_end_time = ps->pss_end_time; | |
8444 | active_resilver = (ps->pss_state == DSS_SCANNING); | |
8445 | } | |
8446 | ||
8447 | have_resilver = (ps->pss_func == POOL_SCAN_RESILVER); | |
8448 | have_scrub = (ps->pss_func == POOL_SCAN_SCRUB); | |
482eeef8 | 8449 | scrub_start = ps->pss_start_time; |
fa7b2390 AM |
8450 | if (c > offsetof(pool_scan_stat_t, |
8451 | pss_pass_error_scrub_pause) / 8) { | |
8452 | have_errorscrub = (ps->pss_error_scrub_func == | |
8453 | POOL_SCAN_ERRORSCRUB); | |
8454 | errorscrub_start = ps->pss_error_scrub_start; | |
8455 | } | |
9a49d3f3 BB |
8456 | } |
8457 | ||
8458 | boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time); | |
8459 | boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0)); | |
8460 | ||
8461 | /* Always print the scrub status when available. */ | |
482eeef8 | 8462 | if (have_scrub && scrub_start > errorscrub_start) |
9a49d3f3 | 8463 | print_scan_scrub_resilver_status(ps); |
482eeef8 GA |
8464 | else if (have_errorscrub && errorscrub_start >= scrub_start) |
8465 | print_err_scrub_status(ps); | |
9a49d3f3 BB |
8466 | |
8467 | /* | |
8468 | * When there is an active resilver or rebuild print its status. | |
8469 | * Otherwise print the status of the last resilver or rebuild. | |
8470 | */ | |
8471 | if (active_resilver || (!active_rebuild && have_resilver && | |
8472 | resilver_end_time && resilver_end_time > rebuild_end_time)) { | |
8473 | print_scan_scrub_resilver_status(ps); | |
8474 | } else if (active_rebuild || (!active_resilver && have_rebuild && | |
8475 | rebuild_end_time && rebuild_end_time > resilver_end_time)) { | |
8476 | print_rebuild_status(zhp, nvroot); | |
8477 | } | |
8478 | ||
8479 | (void) nvlist_lookup_uint64_array(nvroot, | |
8480 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
8481 | print_checkpoint_scan_warning(ps, pcs); | |
8482 | } | |
8483 | ||
a1d477c2 MA |
8484 | /* |
8485 | * Print out detailed removal status. | |
8486 | */ | |
8487 | static void | |
8488 | print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs) | |
8489 | { | |
8490 | char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; | |
8491 | time_t start, end; | |
8492 | nvlist_t *config, *nvroot; | |
8493 | nvlist_t **child; | |
8494 | uint_t children; | |
8495 | char *vdev_name; | |
8496 | ||
8497 | if (prs == NULL || prs->prs_state == DSS_NONE) | |
8498 | return; | |
8499 | ||
8500 | /* | |
8501 | * Determine name of vdev. | |
8502 | */ | |
8503 | config = zpool_get_config(zhp, NULL); | |
8504 | nvroot = fnvlist_lookup_nvlist(config, | |
8505 | ZPOOL_CONFIG_VDEV_TREE); | |
8506 | verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
8507 | &child, &children) == 0); | |
8508 | assert(prs->prs_removing_vdev < children); | |
8509 | vdev_name = zpool_vdev_name(g_zfs, zhp, | |
8510 | child[prs->prs_removing_vdev], B_TRUE); | |
8511 | ||
95a78a03 | 8512 | printf_color(ANSI_BOLD, gettext("remove: ")); |
a1d477c2 MA |
8513 | |
8514 | start = prs->prs_start_time; | |
8515 | end = prs->prs_end_time; | |
8516 | zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf)); | |
8517 | ||
8518 | /* | |
8519 | * Removal is finished or canceled. | |
8520 | */ | |
8521 | if (prs->prs_state == DSS_FINISHED) { | |
8522 | uint64_t minutes_taken = (end - start) / 60; | |
8523 | ||
8524 | (void) printf(gettext("Removal of vdev %llu copied %s " | |
8525 | "in %lluh%um, completed on %s"), | |
8526 | (longlong_t)prs->prs_removing_vdev, | |
8527 | copied_buf, | |
8528 | (u_longlong_t)(minutes_taken / 60), | |
8529 | (uint_t)(minutes_taken % 60), | |
8530 | ctime((time_t *)&end)); | |
8531 | } else if (prs->prs_state == DSS_CANCELED) { | |
8532 | (void) printf(gettext("Removal of %s canceled on %s"), | |
8533 | vdev_name, ctime(&end)); | |
8534 | } else { | |
8535 | uint64_t copied, total, elapsed, mins_left, hours_left; | |
8536 | double fraction_done; | |
8537 | uint_t rate; | |
8538 | ||
8539 | assert(prs->prs_state == DSS_SCANNING); | |
8540 | ||
8541 | /* | |
8542 | * Removal is in progress. | |
8543 | */ | |
8544 | (void) printf(gettext( | |
8545 | "Evacuation of %s in progress since %s"), | |
8546 | vdev_name, ctime(&start)); | |
8547 | ||
8548 | copied = prs->prs_copied > 0 ? prs->prs_copied : 1; | |
8549 | total = prs->prs_to_copy; | |
8550 | fraction_done = (double)copied / total; | |
8551 | ||
8552 | /* elapsed time for this pass */ | |
8553 | elapsed = time(NULL) - prs->prs_start_time; | |
8554 | elapsed = elapsed > 0 ? elapsed : 1; | |
8555 | rate = copied / elapsed; | |
8556 | rate = rate > 0 ? rate : 1; | |
8557 | mins_left = ((total - copied) / rate) / 60; | |
8558 | hours_left = mins_left / 60; | |
8559 | ||
8560 | zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); | |
8561 | zfs_nicenum(total, total_buf, sizeof (total_buf)); | |
8562 | zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); | |
8563 | ||
8564 | /* | |
8565 | * do not print estimated time if hours_left is more than | |
8566 | * 30 days | |
8567 | */ | |
fd1c366f TL |
8568 | (void) printf(gettext( |
8569 | "\t%s copied out of %s at %s/s, %.2f%% done"), | |
a1d477c2 MA |
8570 | examined_buf, total_buf, rate_buf, 100 * fraction_done); |
8571 | if (hours_left < (30 * 24)) { | |
8572 | (void) printf(gettext(", %lluh%um to go\n"), | |
8573 | (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); | |
8574 | } else { | |
8575 | (void) printf(gettext( | |
8576 | ", (copy is slow, no estimated time)\n")); | |
8577 | } | |
8578 | } | |
75c62019 | 8579 | free(vdev_name); |
a1d477c2 MA |
8580 | |
8581 | if (prs->prs_mapping_memory > 0) { | |
8582 | char mem_buf[7]; | |
8583 | zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf)); | |
fd1c366f TL |
8584 | (void) printf(gettext( |
8585 | "\t%s memory used for removed device mappings\n"), | |
a1d477c2 MA |
8586 | mem_buf); |
8587 | } | |
8588 | } | |
8589 | ||
5caeef02 DB |
8590 | /* |
8591 | * Print out detailed raidz expansion status. | |
8592 | */ | |
8593 | static void | |
8594 | print_raidz_expand_status(zpool_handle_t *zhp, pool_raidz_expand_stat_t *pres) | |
8595 | { | |
8596 | char copied_buf[7]; | |
8597 | ||
8598 | if (pres == NULL || pres->pres_state == DSS_NONE) | |
8599 | return; | |
8600 | ||
8601 | /* | |
8602 | * Determine name of vdev. | |
8603 | */ | |
8604 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
8605 | nvlist_t *nvroot = fnvlist_lookup_nvlist(config, | |
8606 | ZPOOL_CONFIG_VDEV_TREE); | |
8607 | nvlist_t **child; | |
8608 | uint_t children; | |
8609 | verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
8610 | &child, &children) == 0); | |
8611 | assert(pres->pres_expanding_vdev < children); | |
8612 | ||
8613 | printf_color(ANSI_BOLD, gettext("expand: ")); | |
8614 | ||
8615 | time_t start = pres->pres_start_time; | |
8616 | time_t end = pres->pres_end_time; | |
8617 | char *vname = | |
8618 | zpool_vdev_name(g_zfs, zhp, child[pres->pres_expanding_vdev], 0); | |
8619 | zfs_nicenum(pres->pres_reflowed, copied_buf, sizeof (copied_buf)); | |
8620 | ||
8621 | /* | |
8622 | * Expansion is finished or canceled. | |
8623 | */ | |
8624 | if (pres->pres_state == DSS_FINISHED) { | |
8625 | char time_buf[32]; | |
8626 | secs_to_dhms(end - start, time_buf); | |
8627 | ||
8628 | (void) printf(gettext("expanded %s-%u copied %s in %s, " | |
8629 | "on %s"), vname, (int)pres->pres_expanding_vdev, | |
8630 | copied_buf, time_buf, ctime((time_t *)&end)); | |
8631 | } else { | |
8632 | char examined_buf[7], total_buf[7], rate_buf[7]; | |
8633 | uint64_t copied, total, elapsed, secs_left; | |
8634 | double fraction_done; | |
8635 | uint_t rate; | |
8636 | ||
8637 | assert(pres->pres_state == DSS_SCANNING); | |
8638 | ||
8639 | /* | |
8640 | * Expansion is in progress. | |
8641 | */ | |
8642 | (void) printf(gettext( | |
8643 | "expansion of %s-%u in progress since %s"), | |
8644 | vname, (int)pres->pres_expanding_vdev, ctime(&start)); | |
8645 | ||
8646 | copied = pres->pres_reflowed > 0 ? pres->pres_reflowed : 1; | |
8647 | total = pres->pres_to_reflow; | |
8648 | fraction_done = (double)copied / total; | |
8649 | ||
8650 | /* elapsed time for this pass */ | |
8651 | elapsed = time(NULL) - pres->pres_start_time; | |
8652 | elapsed = elapsed > 0 ? elapsed : 1; | |
8653 | rate = copied / elapsed; | |
8654 | rate = rate > 0 ? rate : 1; | |
8655 | secs_left = (total - copied) / rate; | |
8656 | ||
8657 | zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); | |
8658 | zfs_nicenum(total, total_buf, sizeof (total_buf)); | |
8659 | zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); | |
8660 | ||
8661 | /* | |
8662 | * do not print estimated time if hours_left is more than | |
8663 | * 30 days | |
8664 | */ | |
8665 | (void) printf(gettext("\t%s / %s copied at %s/s, %.2f%% done"), | |
8666 | examined_buf, total_buf, rate_buf, 100 * fraction_done); | |
8667 | if (pres->pres_waiting_for_resilver) { | |
8668 | (void) printf(gettext(", paused for resilver or " | |
8669 | "clear\n")); | |
8670 | } else if (secs_left < (30 * 24 * 3600)) { | |
8671 | char time_buf[32]; | |
8672 | secs_to_dhms(secs_left, time_buf); | |
8673 | (void) printf(gettext(", %s to go\n"), time_buf); | |
8674 | } else { | |
8675 | (void) printf(gettext( | |
8676 | ", (copy is slow, no estimated time)\n")); | |
8677 | } | |
8678 | } | |
8679 | free(vname); | |
8680 | } | |
d2734cce SD |
8681 | static void |
8682 | print_checkpoint_status(pool_checkpoint_stat_t *pcs) | |
8683 | { | |
8684 | time_t start; | |
8685 | char space_buf[7]; | |
8686 | ||
8687 | if (pcs == NULL || pcs->pcs_state == CS_NONE) | |
8688 | return; | |
8689 | ||
8690 | (void) printf(gettext("checkpoint: ")); | |
8691 | ||
8692 | start = pcs->pcs_start_time; | |
8693 | zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf)); | |
8694 | ||
8695 | if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) { | |
8696 | char *date = ctime(&start); | |
8697 | ||
8698 | /* | |
8699 | * ctime() adds a newline at the end of the generated | |
8700 | * string, thus the weird format specifier and the | |
8701 | * strlen() call used to chop it off from the output. | |
8702 | */ | |
8703 | (void) printf(gettext("created %.*s, consumes %s\n"), | |
8704 | (int)(strlen(date) - 1), date, space_buf); | |
8705 | return; | |
8706 | } | |
8707 | ||
8708 | assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING); | |
8709 | ||
8710 | (void) printf(gettext("discarding, %s remaining.\n"), | |
8711 | space_buf); | |
8712 | } | |
8713 | ||
34dc7c2f BB |
8714 | static void |
8715 | print_error_log(zpool_handle_t *zhp) | |
8716 | { | |
8717 | nvlist_t *nverrlist = NULL; | |
8718 | nvpair_t *elem; | |
8719 | char *pathname; | |
8720 | size_t len = MAXPATHLEN * 2; | |
8721 | ||
56a6054d | 8722 | if (zpool_get_errlog(zhp, &nverrlist) != 0) |
34dc7c2f | 8723 | return; |
34dc7c2f BB |
8724 | |
8725 | (void) printf("errors: Permanent errors have been " | |
8726 | "detected in the following files:\n\n"); | |
8727 | ||
8728 | pathname = safe_malloc(len); | |
8729 | elem = NULL; | |
8730 | while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { | |
8731 | nvlist_t *nv; | |
8732 | uint64_t dsobj, obj; | |
8733 | ||
8734 | verify(nvpair_value_nvlist(elem, &nv) == 0); | |
8735 | verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, | |
8736 | &dsobj) == 0); | |
8737 | verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, | |
8738 | &obj) == 0); | |
8739 | zpool_obj_to_path(zhp, dsobj, obj, pathname, len); | |
8740 | (void) printf("%7s %s\n", "", pathname); | |
8741 | } | |
8742 | free(pathname); | |
8743 | nvlist_free(nverrlist); | |
8744 | } | |
8745 | ||
8746 | static void | |
fea33e4e HJ |
8747 | print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares, |
8748 | uint_t nspares) | |
34dc7c2f BB |
8749 | { |
8750 | uint_t i; | |
8751 | char *name; | |
8752 | ||
8753 | if (nspares == 0) | |
8754 | return; | |
8755 | ||
8756 | (void) printf(gettext("\tspares\n")); | |
8757 | ||
8758 | for (i = 0; i < nspares; i++) { | |
fea33e4e HJ |
8759 | name = zpool_vdev_name(g_zfs, zhp, spares[i], |
8760 | cb->cb_name_flags); | |
9a49d3f3 | 8761 | print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL); |
34dc7c2f BB |
8762 | free(name); |
8763 | } | |
8764 | } | |
8765 | ||
8766 | static void | |
fea33e4e HJ |
8767 | print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache, |
8768 | uint_t nl2cache) | |
34dc7c2f BB |
8769 | { |
8770 | uint_t i; | |
8771 | char *name; | |
8772 | ||
8773 | if (nl2cache == 0) | |
8774 | return; | |
8775 | ||
8776 | (void) printf(gettext("\tcache\n")); | |
8777 | ||
8778 | for (i = 0; i < nl2cache; i++) { | |
fea33e4e HJ |
8779 | name = zpool_vdev_name(g_zfs, zhp, l2cache[i], |
8780 | cb->cb_name_flags); | |
9a49d3f3 BB |
8781 | print_status_config(zhp, cb, name, l2cache[i], 2, |
8782 | B_FALSE, NULL); | |
34dc7c2f BB |
8783 | free(name); |
8784 | } | |
8785 | } | |
8786 | ||
428870ff BB |
8787 | static void |
8788 | print_dedup_stats(nvlist_t *config) | |
8789 | { | |
8790 | ddt_histogram_t *ddh; | |
8791 | ddt_stat_t *dds; | |
8792 | ddt_object_t *ddo; | |
8793 | uint_t c; | |
e7fbeb60 | 8794 | char dspace[6], mspace[6]; |
428870ff BB |
8795 | |
8796 | /* | |
8797 | * If the pool was faulted then we may not have been able to | |
32a9872b | 8798 | * obtain the config. Otherwise, if we have anything in the dedup |
428870ff BB |
8799 | * table continue processing the stats. |
8800 | */ | |
8801 | if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, | |
24024589 | 8802 | (uint64_t **)&ddo, &c) != 0) |
428870ff BB |
8803 | return; |
8804 | ||
8805 | (void) printf("\n"); | |
24024589 YP |
8806 | (void) printf(gettext(" dedup: ")); |
8807 | if (ddo->ddo_count == 0) { | |
8808 | (void) printf(gettext("no DDT entries\n")); | |
8809 | return; | |
8810 | } | |
8811 | ||
e7fbeb60 | 8812 | zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace)); |
8813 | zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace)); | |
8814 | (void) printf("DDT entries %llu, size %s on disk, %s in core\n", | |
428870ff | 8815 | (u_longlong_t)ddo->ddo_count, |
e7fbeb60 | 8816 | dspace, |
8817 | mspace); | |
428870ff BB |
8818 | |
8819 | verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, | |
8820 | (uint64_t **)&dds, &c) == 0); | |
8821 | verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, | |
8822 | (uint64_t **)&ddh, &c) == 0); | |
8823 | zpool_dump_ddt(dds, ddh); | |
8824 | } | |
8825 | ||
34dc7c2f BB |
8826 | /* |
8827 | * Display a summary of pool status. Displays a summary such as: | |
8828 | * | |
8829 | * pool: tank | |
8830 | * status: DEGRADED | |
8831 | * reason: One or more devices ... | |
a2f944a1 | 8832 | * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01 |
34dc7c2f BB |
8833 | * config: |
8834 | * mirror DEGRADED | |
8835 | * c1t0d0 OK | |
8836 | * c2t0d0 UNAVAIL | |
8837 | * | |
8838 | * When given the '-v' option, we print out the complete config. If the '-e' | |
8839 | * option is specified, then we print out error rate information as well. | |
8840 | */ | |
65c7cc49 | 8841 | static int |
34dc7c2f BB |
8842 | status_callback(zpool_handle_t *zhp, void *data) |
8843 | { | |
8844 | status_cbdata_t *cbp = data; | |
8845 | nvlist_t *config, *nvroot; | |
a926aab9 | 8846 | const char *msgid; |
731782ec | 8847 | zpool_status_t reason; |
ffe9d382 | 8848 | zpool_errata_t errata; |
34dc7c2f BB |
8849 | const char *health; |
8850 | uint_t c; | |
8851 | vdev_stat_t *vs; | |
8852 | ||
8853 | config = zpool_get_config(zhp, NULL); | |
ffe9d382 | 8854 | reason = zpool_get_status(zhp, &msgid, &errata); |
34dc7c2f BB |
8855 | |
8856 | cbp->cb_count++; | |
8857 | ||
8858 | /* | |
8859 | * If we were given 'zpool status -x', only report those pools with | |
8860 | * problems. | |
8861 | */ | |
c5b247f3 TC |
8862 | if (cbp->cb_explain && |
8863 | (reason == ZPOOL_STATUS_OK || | |
8864 | reason == ZPOOL_STATUS_VERSION_OLDER || | |
658fb802 | 8865 | reason == ZPOOL_STATUS_FEAT_DISABLED || |
e086db16 CB |
8866 | reason == ZPOOL_STATUS_COMPATIBILITY_ERR || |
8867 | reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) { | |
34dc7c2f BB |
8868 | if (!cbp->cb_allpools) { |
8869 | (void) printf(gettext("pool '%s' is healthy\n"), | |
8870 | zpool_get_name(zhp)); | |
8871 | if (cbp->cb_first) | |
8872 | cbp->cb_first = B_FALSE; | |
8873 | } | |
8874 | return (0); | |
8875 | } | |
8876 | ||
8877 | if (cbp->cb_first) | |
8878 | cbp->cb_first = B_FALSE; | |
8879 | else | |
8880 | (void) printf("\n"); | |
8881 | ||
a1d477c2 | 8882 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
428870ff | 8883 | verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f | 8884 | (uint64_t **)&vs, &c) == 0); |
f0ed6c74 TH |
8885 | |
8886 | health = zpool_get_state_str(zhp); | |
34dc7c2f | 8887 | |
9fb2771a TH |
8888 | printf(" "); |
8889 | printf_color(ANSI_BOLD, gettext("pool:")); | |
8890 | printf(" %s\n", zpool_get_name(zhp)); | |
a926aab9 | 8891 | fputc(' ', stdout); |
9fb2771a TH |
8892 | printf_color(ANSI_BOLD, gettext("state: ")); |
8893 | ||
8894 | printf_color(health_str_to_color(health), "%s", health); | |
8895 | ||
a926aab9 | 8896 | fputc('\n', stdout); |
34dc7c2f BB |
8897 | |
8898 | switch (reason) { | |
8899 | case ZPOOL_STATUS_MISSING_DEV_R: | |
9fb2771a TH |
8900 | printf_color(ANSI_BOLD, gettext("status: ")); |
8901 | printf_color(ANSI_YELLOW, gettext("One or more devices could " | |
8902 | "not be opened. Sufficient replicas exist for\n\tthe pool " | |
8903 | "to continue functioning in a degraded state.\n")); | |
8904 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8905 | printf_color(ANSI_YELLOW, gettext("Attach the missing device " | |
8906 | "and online it using 'zpool online'.\n")); | |
34dc7c2f BB |
8907 | break; |
8908 | ||
8909 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
9fb2771a TH |
8910 | printf_color(ANSI_BOLD, gettext("status: ")); |
8911 | printf_color(ANSI_YELLOW, gettext("One or more devices could " | |
8912 | "not be opened. There are insufficient\n\treplicas for the" | |
8913 | " pool to continue functioning.\n")); | |
8914 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8915 | printf_color(ANSI_YELLOW, gettext("Attach the missing device " | |
8916 | "and online it using 'zpool online'.\n")); | |
34dc7c2f BB |
8917 | break; |
8918 | ||
8919 | case ZPOOL_STATUS_CORRUPT_LABEL_R: | |
9fb2771a TH |
8920 | printf_color(ANSI_BOLD, gettext("status: ")); |
8921 | printf_color(ANSI_YELLOW, gettext("One or more devices could " | |
8922 | "not be used because the label is missing or\n\tinvalid. " | |
34dc7c2f BB |
8923 | "Sufficient replicas exist for the pool to continue\n\t" |
8924 | "functioning in a degraded state.\n")); | |
9fb2771a TH |
8925 | printf_color(ANSI_BOLD, gettext("action: ")); |
8926 | printf_color(ANSI_YELLOW, gettext("Replace the device using " | |
34dc7c2f BB |
8927 | "'zpool replace'.\n")); |
8928 | break; | |
8929 | ||
8930 | case ZPOOL_STATUS_CORRUPT_LABEL_NR: | |
9fb2771a TH |
8931 | printf_color(ANSI_BOLD, gettext("status: ")); |
8932 | printf_color(ANSI_YELLOW, gettext("One or more devices could " | |
8933 | "not be used because the label is missing \n\tor invalid. " | |
34dc7c2f BB |
8934 | "There are insufficient replicas for the pool to " |
8935 | "continue\n\tfunctioning.\n")); | |
428870ff BB |
8936 | zpool_explain_recover(zpool_get_handle(zhp), |
8937 | zpool_get_name(zhp), reason, config); | |
34dc7c2f BB |
8938 | break; |
8939 | ||
8940 | case ZPOOL_STATUS_FAILING_DEV: | |
9fb2771a TH |
8941 | printf_color(ANSI_BOLD, gettext("status: ")); |
8942 | printf_color(ANSI_YELLOW, gettext("One or more devices has " | |
34dc7c2f BB |
8943 | "experienced an unrecoverable error. An\n\tattempt was " |
8944 | "made to correct the error. Applications are " | |
8945 | "unaffected.\n")); | |
9fb2771a TH |
8946 | printf_color(ANSI_BOLD, gettext("action: ")); |
8947 | printf_color(ANSI_YELLOW, gettext("Determine if the " | |
8948 | "device needs to be replaced, and clear the errors\n\tusing" | |
8949 | " 'zpool clear' or replace the device with 'zpool " | |
34dc7c2f BB |
8950 | "replace'.\n")); |
8951 | break; | |
8952 | ||
8953 | case ZPOOL_STATUS_OFFLINE_DEV: | |
9fb2771a TH |
8954 | printf_color(ANSI_BOLD, gettext("status: ")); |
8955 | printf_color(ANSI_YELLOW, gettext("One or more devices has " | |
34dc7c2f BB |
8956 | "been taken offline by the administrator.\n\tSufficient " |
8957 | "replicas exist for the pool to continue functioning in " | |
8958 | "a\n\tdegraded state.\n")); | |
9fb2771a TH |
8959 | printf_color(ANSI_BOLD, gettext("action: ")); |
8960 | printf_color(ANSI_YELLOW, gettext("Online the device " | |
8961 | "using 'zpool online' or replace the device with\n\t'zpool " | |
34dc7c2f BB |
8962 | "replace'.\n")); |
8963 | break; | |
8964 | ||
45d1cae3 | 8965 | case ZPOOL_STATUS_REMOVED_DEV: |
9fb2771a TH |
8966 | printf_color(ANSI_BOLD, gettext("status: ")); |
8967 | printf_color(ANSI_YELLOW, gettext("One or more devices has " | |
45d1cae3 BB |
8968 | "been removed by the administrator.\n\tSufficient " |
8969 | "replicas exist for the pool to continue functioning in " | |
8970 | "a\n\tdegraded state.\n")); | |
9fb2771a TH |
8971 | printf_color(ANSI_BOLD, gettext("action: ")); |
8972 | printf_color(ANSI_YELLOW, gettext("Online the device " | |
8973 | "using zpool online' or replace the device with\n\t'zpool " | |
45d1cae3 BB |
8974 | "replace'.\n")); |
8975 | break; | |
8976 | ||
34dc7c2f | 8977 | case ZPOOL_STATUS_RESILVERING: |
9a49d3f3 | 8978 | case ZPOOL_STATUS_REBUILDING: |
9fb2771a TH |
8979 | printf_color(ANSI_BOLD, gettext("status: ")); |
8980 | printf_color(ANSI_YELLOW, gettext("One or more devices is " | |
34dc7c2f BB |
8981 | "currently being resilvered. The pool will\n\tcontinue " |
8982 | "to function, possibly in a degraded state.\n")); | |
9fb2771a TH |
8983 | printf_color(ANSI_BOLD, gettext("action: ")); |
8984 | printf_color(ANSI_YELLOW, gettext("Wait for the resilver to " | |
34dc7c2f BB |
8985 | "complete.\n")); |
8986 | break; | |
8987 | ||
9a49d3f3 BB |
8988 | case ZPOOL_STATUS_REBUILD_SCRUB: |
8989 | printf_color(ANSI_BOLD, gettext("status: ")); | |
8990 | printf_color(ANSI_YELLOW, gettext("One or more devices have " | |
8991 | "been sequentially resilvered, scrubbing\n\tthe pool " | |
8992 | "is recommended.\n")); | |
8993 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8994 | printf_color(ANSI_YELLOW, gettext("Use 'zpool scrub' to " | |
8995 | "verify all data checksums.\n")); | |
8996 | break; | |
8997 | ||
34dc7c2f | 8998 | case ZPOOL_STATUS_CORRUPT_DATA: |
9fb2771a TH |
8999 | printf_color(ANSI_BOLD, gettext("status: ")); |
9000 | printf_color(ANSI_YELLOW, gettext("One or more devices has " | |
34dc7c2f BB |
9001 | "experienced an error resulting in data\n\tcorruption. " |
9002 | "Applications may be affected.\n")); | |
9fb2771a TH |
9003 | printf_color(ANSI_BOLD, gettext("action: ")); |
9004 | printf_color(ANSI_YELLOW, gettext("Restore the file in question" | |
9005 | " if possible. Otherwise restore the\n\tentire pool from " | |
34dc7c2f BB |
9006 | "backup.\n")); |
9007 | break; | |
9008 | ||
9009 | case ZPOOL_STATUS_CORRUPT_POOL: | |
9fb2771a TH |
9010 | printf_color(ANSI_BOLD, gettext("status: ")); |
9011 | printf_color(ANSI_YELLOW, gettext("The pool metadata is " | |
9012 | "corrupted and the pool cannot be opened.\n")); | |
428870ff BB |
9013 | zpool_explain_recover(zpool_get_handle(zhp), |
9014 | zpool_get_name(zhp), reason, config); | |
34dc7c2f BB |
9015 | break; |
9016 | ||
9017 | case ZPOOL_STATUS_VERSION_OLDER: | |
9fb2771a TH |
9018 | printf_color(ANSI_BOLD, gettext("status: ")); |
9019 | printf_color(ANSI_YELLOW, gettext("The pool is formatted using " | |
9020 | "a legacy on-disk format. The pool can\n\tstill be used, " | |
b9b24bb4 | 9021 | "but some features are unavailable.\n")); |
9fb2771a TH |
9022 | printf_color(ANSI_BOLD, gettext("action: ")); |
9023 | printf_color(ANSI_YELLOW, gettext("Upgrade the pool using " | |
9024 | "'zpool upgrade'. Once this is done, the\n\tpool will no " | |
9025 | "longer be accessible on software that does not support\n\t" | |
f52b31ea | 9026 | "feature flags.\n")); |
34dc7c2f BB |
9027 | break; |
9028 | ||
9029 | case ZPOOL_STATUS_VERSION_NEWER: | |
9fb2771a TH |
9030 | printf_color(ANSI_BOLD, gettext("status: ")); |
9031 | printf_color(ANSI_YELLOW, gettext("The pool has been upgraded " | |
9032 | "to a newer, incompatible on-disk version.\n\tThe pool " | |
9033 | "cannot be accessed on this system.\n")); | |
9034 | printf_color(ANSI_BOLD, gettext("action: ")); | |
9035 | printf_color(ANSI_YELLOW, gettext("Access the pool from a " | |
9036 | "system running more recent software, or\n\trestore the " | |
9037 | "pool from backup.\n")); | |
34dc7c2f BB |
9038 | break; |
9039 | ||
b9b24bb4 | 9040 | case ZPOOL_STATUS_FEAT_DISABLED: |
9fb2771a | 9041 | printf_color(ANSI_BOLD, gettext("status: ")); |
658fb802 CB |
9042 | printf_color(ANSI_YELLOW, gettext("Some supported and " |
9043 | "requested features are not enabled on the pool.\n\t" | |
9044 | "The pool can still be used, but some features are " | |
9045 | "unavailable.\n")); | |
9fb2771a TH |
9046 | printf_color(ANSI_BOLD, gettext("action: ")); |
9047 | printf_color(ANSI_YELLOW, gettext("Enable all features using " | |
b9b24bb4 CS |
9048 | "'zpool upgrade'. Once this is done,\n\tthe pool may no " |
9049 | "longer be accessible by software that does not support\n\t" | |
2badb345 | 9050 | "the features. See zpool-features(7) for details.\n")); |
b9b24bb4 CS |
9051 | break; |
9052 | ||
658fb802 CB |
9053 | case ZPOOL_STATUS_COMPATIBILITY_ERR: |
9054 | printf_color(ANSI_BOLD, gettext("status: ")); | |
9055 | printf_color(ANSI_YELLOW, gettext("This pool has a " | |
9056 | "compatibility list specified, but it could not be\n\t" | |
9057 | "read/parsed at this time. The pool can still be used, " | |
9058 | "but this\n\tshould be investigated.\n")); | |
9059 | printf_color(ANSI_BOLD, gettext("action: ")); | |
9060 | printf_color(ANSI_YELLOW, gettext("Check the value of the " | |
9061 | "'compatibility' property against the\n\t" | |
9062 | "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or " | |
9063 | ZPOOL_DATA_COMPAT_D ".\n")); | |
9064 | break; | |
9065 | ||
e086db16 CB |
9066 | case ZPOOL_STATUS_INCOMPATIBLE_FEAT: |
9067 | printf_color(ANSI_BOLD, gettext("status: ")); | |
9068 | printf_color(ANSI_YELLOW, gettext("One or more features " | |
9069 | "are enabled on the pool despite not being\n\t" | |
9070 | "requested by the 'compatibility' property.\n")); | |
9071 | printf_color(ANSI_BOLD, gettext("action: ")); | |
9072 | printf_color(ANSI_YELLOW, gettext("Consider setting " | |
9073 | "'compatibility' to an appropriate value, or\n\t" | |
9074 | "adding needed features to the relevant file in\n\t" | |
9075 | ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n")); | |
9076 | break; | |
9077 | ||
9ae529ec | 9078 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
9fb2771a TH |
9079 | printf_color(ANSI_BOLD, gettext("status: ")); |
9080 | printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed " | |
9081 | "on this system because it uses the\n\tfollowing feature(s)" | |
9082 | " not supported on this system:\n")); | |
9ae529ec CS |
9083 | zpool_print_unsup_feat(config); |
9084 | (void) printf("\n"); | |
9fb2771a TH |
9085 | printf_color(ANSI_BOLD, gettext("action: ")); |
9086 | printf_color(ANSI_YELLOW, gettext("Access the pool from a " | |
9087 | "system that supports the required feature(s),\n\tor " | |
9088 | "restore the pool from backup.\n")); | |
9ae529ec CS |
9089 | break; |
9090 | ||
9091 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
9fb2771a TH |
9092 | printf_color(ANSI_BOLD, gettext("status: ")); |
9093 | printf_color(ANSI_YELLOW, gettext("The pool can only be " | |
9094 | "accessed in read-only mode on this system. It\n\tcannot be" | |
9095 | " accessed in read-write mode because it uses the " | |
9ae529ec CS |
9096 | "following\n\tfeature(s) not supported on this system:\n")); |
9097 | zpool_print_unsup_feat(config); | |
9098 | (void) printf("\n"); | |
9fb2771a TH |
9099 | printf_color(ANSI_BOLD, gettext("action: ")); |
9100 | printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed " | |
9101 | "in read-write mode. Import the pool with\n" | |
9ae529ec CS |
9102 | "\t\"-o readonly=on\", access the pool from a system that " |
9103 | "supports the\n\trequired feature(s), or restore the " | |
9104 | "pool from backup.\n")); | |
9105 | break; | |
9106 | ||
34dc7c2f | 9107 | case ZPOOL_STATUS_FAULTED_DEV_R: |
9fb2771a TH |
9108 | printf_color(ANSI_BOLD, gettext("status: ")); |
9109 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
34dc7c2f BB |
9110 | "faulted in response to persistent errors.\n\tSufficient " |
9111 | "replicas exist for the pool to continue functioning " | |
9112 | "in a\n\tdegraded state.\n")); | |
9fb2771a TH |
9113 | printf_color(ANSI_BOLD, gettext("action: ")); |
9114 | printf_color(ANSI_YELLOW, gettext("Replace the faulted device, " | |
34dc7c2f BB |
9115 | "or use 'zpool clear' to mark the device\n\trepaired.\n")); |
9116 | break; | |
9117 | ||
9118 | case ZPOOL_STATUS_FAULTED_DEV_NR: | |
9fb2771a TH |
9119 | printf_color(ANSI_BOLD, gettext("status: ")); |
9120 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
34dc7c2f BB |
9121 | "faulted in response to persistent errors. There are " |
9122 | "insufficient replicas for the pool to\n\tcontinue " | |
9123 | "functioning.\n")); | |
9fb2771a TH |
9124 | printf_color(ANSI_BOLD, gettext("action: ")); |
9125 | printf_color(ANSI_YELLOW, gettext("Destroy and re-create the " | |
9126 | "pool from a backup source. Manually marking the device\n" | |
34dc7c2f BB |
9127 | "\trepaired using 'zpool clear' may allow some data " |
9128 | "to be recovered.\n")); | |
9129 | break; | |
9130 | ||
cec3a0a1 | 9131 | case ZPOOL_STATUS_IO_FAILURE_MMP: |
9fb2771a TH |
9132 | printf_color(ANSI_BOLD, gettext("status: ")); |
9133 | printf_color(ANSI_YELLOW, gettext("The pool is suspended " | |
9134 | "because multihost writes failed or were delayed;\n\t" | |
9135 | "another system could import the pool undetected.\n")); | |
9136 | printf_color(ANSI_BOLD, gettext("action: ")); | |
9137 | printf_color(ANSI_YELLOW, gettext("Make sure the pool's devices" | |
9138 | " are connected, then reboot your system and\n\timport the " | |
c3f2f1aa | 9139 | "pool or run 'zpool clear' to resume the pool.\n")); |
cec3a0a1 OF |
9140 | break; |
9141 | ||
b128c09f BB |
9142 | case ZPOOL_STATUS_IO_FAILURE_WAIT: |
9143 | case ZPOOL_STATUS_IO_FAILURE_CONTINUE: | |
9fb2771a TH |
9144 | printf_color(ANSI_BOLD, gettext("status: ")); |
9145 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
b128c09f | 9146 | "faulted in response to IO failures.\n")); |
9fb2771a TH |
9147 | printf_color(ANSI_BOLD, gettext("action: ")); |
9148 | printf_color(ANSI_YELLOW, gettext("Make sure the affected " | |
9149 | "devices are connected, then run 'zpool clear'.\n")); | |
b128c09f BB |
9150 | break; |
9151 | ||
9152 | case ZPOOL_STATUS_BAD_LOG: | |
9fb2771a TH |
9153 | printf_color(ANSI_BOLD, gettext("status: ")); |
9154 | printf_color(ANSI_YELLOW, gettext("An intent log record " | |
b128c09f | 9155 | "could not be read.\n" |
2627e752 | 9156 | "\tWaiting for administrator intervention to fix the " |
b128c09f | 9157 | "faulted pool.\n")); |
9fb2771a TH |
9158 | printf_color(ANSI_BOLD, gettext("action: ")); |
9159 | printf_color(ANSI_YELLOW, gettext("Either restore the affected " | |
b128c09f BB |
9160 | "device(s) and run 'zpool online',\n" |
9161 | "\tor ignore the intent log records by running " | |
9162 | "'zpool clear'.\n")); | |
9163 | break; | |
9164 | ||
6fe3498c RM |
9165 | case ZPOOL_STATUS_NON_NATIVE_ASHIFT: |
9166 | (void) printf(gettext("status: One or more devices are " | |
9167 | "configured to use a non-native block size.\n" | |
9168 | "\tExpect reduced performance.\n")); | |
9169 | (void) printf(gettext("action: Replace affected devices with " | |
9170 | "devices that support the\n\tconfigured block size, or " | |
9171 | "migrate data to a properly configured\n\tpool.\n")); | |
9172 | break; | |
9173 | ||
1cbae971 | 9174 | case ZPOOL_STATUS_HOSTID_MISMATCH: |
9fb2771a TH |
9175 | printf_color(ANSI_BOLD, gettext("status: ")); |
9176 | printf_color(ANSI_YELLOW, gettext("Mismatch between pool hostid" | |
9177 | " and system hostid on imported pool.\n\tThis pool was " | |
1cbae971 RY |
9178 | "previously imported into a system with a different " |
9179 | "hostid,\n\tand then was verbatim imported into this " | |
9180 | "system.\n")); | |
9fb2771a TH |
9181 | printf_color(ANSI_BOLD, gettext("action: ")); |
9182 | printf_color(ANSI_YELLOW, gettext("Export this pool on all " | |
9183 | "systems on which it is imported.\n" | |
1cbae971 RY |
9184 | "\tThen import it to correct the mismatch.\n")); |
9185 | break; | |
9186 | ||
ffe9d382 | 9187 | case ZPOOL_STATUS_ERRATA: |
9fb2771a TH |
9188 | printf_color(ANSI_BOLD, gettext("status: ")); |
9189 | printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), | |
ffe9d382 BB |
9190 | errata); |
9191 | ||
9192 | switch (errata) { | |
9193 | case ZPOOL_ERRATA_NONE: | |
9194 | break; | |
9195 | ||
4f2dcb3e | 9196 | case ZPOOL_ERRATA_ZOL_2094_SCRUB: |
9fb2771a TH |
9197 | printf_color(ANSI_BOLD, gettext("action: ")); |
9198 | printf_color(ANSI_YELLOW, gettext("To correct the issue" | |
9199 | " run 'zpool scrub'.\n")); | |
4f2dcb3e RY |
9200 | break; |
9201 | ||
ae76f45c TC |
9202 | case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: |
9203 | (void) printf(gettext("\tExisting encrypted datasets " | |
9204 | "contain an on-disk incompatibility\n\twhich " | |
9205 | "needs to be corrected.\n")); | |
9fb2771a TH |
9206 | printf_color(ANSI_BOLD, gettext("action: ")); |
9207 | printf_color(ANSI_YELLOW, gettext("To correct the issue" | |
9208 | " backup existing encrypted datasets to new\n\t" | |
ae76f45c TC |
9209 | "encrypted datasets and destroy the old ones. " |
9210 | "'zfs mount -o ro' can\n\tbe used to temporarily " | |
9211 | "mount existing encrypted datasets readonly.\n")); | |
9212 | break; | |
9213 | ||
f00ab3f2 | 9214 | case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: |
e2dddb7e JS |
9215 | (void) printf(gettext("\tExisting encrypted snapshots " |
9216 | "and bookmarks contain an on-disk\n\tincompat" | |
9217 | "ibility. This may cause on-disk corruption if " | |
9218 | "they are used\n\twith 'zfs recv'.\n")); | |
9fb2771a TH |
9219 | printf_color(ANSI_BOLD, gettext("action: ")); |
9220 | printf_color(ANSI_YELLOW, gettext("To correct the" | |
9221 | "issue, enable the bookmark_v2 feature. No " | |
9222 | "additional\n\taction is needed if there are no " | |
9223 | "encrypted snapshots or bookmarks.\n\tIf preserving" | |
9224 | "the encrypted snapshots and bookmarks is required," | |
9225 | " use\n\ta non-raw send to backup and restore them." | |
9226 | " Alternately, they may be\n\tremoved to resolve " | |
e2dddb7e | 9227 | "the incompatibility.\n")); |
f00ab3f2 TC |
9228 | break; |
9229 | ||
ffe9d382 BB |
9230 | default: |
9231 | /* | |
9232 | * All errata which allow the pool to be imported | |
9233 | * must contain an action message. | |
9234 | */ | |
9235 | assert(0); | |
9236 | } | |
9237 | break; | |
9238 | ||
34dc7c2f BB |
9239 | default: |
9240 | /* | |
9241 | * The remaining errors can't actually be generated, yet. | |
9242 | */ | |
9243 | assert(reason == ZPOOL_STATUS_OK); | |
9244 | } | |
9245 | ||
9fb2771a TH |
9246 | if (msgid != NULL) { |
9247 | printf(" "); | |
9248 | printf_color(ANSI_BOLD, gettext("see:")); | |
a2f944a1 RM |
9249 | printf(gettext( |
9250 | " https://openzfs.github.io/openzfs-docs/msg/%s\n"), | |
9251 | msgid); | |
9fb2771a | 9252 | } |
34dc7c2f BB |
9253 | |
9254 | if (config != NULL) { | |
34dc7c2f BB |
9255 | uint64_t nerr; |
9256 | nvlist_t **spares, **l2cache; | |
9257 | uint_t nspares, nl2cache; | |
9258 | ||
9a49d3f3 BB |
9259 | print_scan_status(zhp, nvroot); |
9260 | ||
5caeef02 | 9261 | pool_removal_stat_t *prs = NULL; |
a1d477c2 MA |
9262 | (void) nvlist_lookup_uint64_array(nvroot, |
9263 | ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); | |
9264 | print_removal_status(zhp, prs); | |
9a49d3f3 | 9265 | |
5caeef02 | 9266 | pool_checkpoint_stat_t *pcs = NULL; |
9a49d3f3 BB |
9267 | (void) nvlist_lookup_uint64_array(nvroot, |
9268 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
d2734cce | 9269 | print_checkpoint_status(pcs); |
a1d477c2 | 9270 | |
5caeef02 DB |
9271 | pool_raidz_expand_stat_t *pres = NULL; |
9272 | (void) nvlist_lookup_uint64_array(nvroot, | |
9273 | ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c); | |
9274 | print_raidz_expand_status(zhp, pres); | |
9275 | ||
fea33e4e | 9276 | cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0, |
5f20c145 | 9277 | cbp->cb_name_flags | VDEV_NAME_TYPE_ID); |
fea33e4e HJ |
9278 | if (cbp->cb_namewidth < 10) |
9279 | cbp->cb_namewidth = 10; | |
34dc7c2f | 9280 | |
9fb2771a | 9281 | color_start(ANSI_BOLD); |
34dc7c2f | 9282 | (void) printf(gettext("config:\n\n")); |
d6418de0 | 9283 | (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"), |
fea33e4e HJ |
9284 | cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE", |
9285 | "CKSUM"); | |
9fb2771a | 9286 | color_end(); |
d6418de0 | 9287 | |
9fb2771a TH |
9288 | if (cbp->cb_print_slow_ios) { |
9289 | printf_color(ANSI_BOLD, " %5s", gettext("SLOW")); | |
9290 | } | |
ad796b8a | 9291 | |
a9520e6e TH |
9292 | if (cbp->cb_print_power) { |
9293 | printf_color(ANSI_BOLD, " %5s", gettext("POWER")); | |
9294 | } | |
9295 | ||
d6418de0 TH |
9296 | if (cbp->vcdl != NULL) |
9297 | print_cmd_columns(cbp->vcdl, 0); | |
9298 | ||
9299 | printf("\n"); | |
cc99f275 | 9300 | |
fea33e4e | 9301 | print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0, |
9a49d3f3 | 9302 | B_FALSE, NULL); |
34dc7c2f | 9303 | |
cc99f275 DB |
9304 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP); |
9305 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL); | |
9306 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS); | |
9307 | ||
34dc7c2f BB |
9308 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
9309 | &l2cache, &nl2cache) == 0) | |
fea33e4e | 9310 | print_l2cache(zhp, cbp, l2cache, nl2cache); |
34dc7c2f BB |
9311 | |
9312 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
9313 | &spares, &nspares) == 0) | |
fea33e4e | 9314 | print_spares(zhp, cbp, spares, nspares); |
34dc7c2f BB |
9315 | |
9316 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, | |
9317 | &nerr) == 0) { | |
34dc7c2f | 9318 | (void) printf("\n"); |
018f2604 MA |
9319 | if (nerr == 0) { |
9320 | (void) printf(gettext( | |
9321 | "errors: No known data errors\n")); | |
9322 | } else if (!cbp->cb_verbose) { | |
08233887 | 9323 | color_start(ANSI_RED); |
34dc7c2f BB |
9324 | (void) printf(gettext("errors: %llu data " |
9325 | "errors, use '-v' for a list\n"), | |
9326 | (u_longlong_t)nerr); | |
08233887 | 9327 | color_end(); |
018f2604 | 9328 | } else { |
34dc7c2f | 9329 | print_error_log(zhp); |
018f2604 | 9330 | } |
34dc7c2f | 9331 | } |
428870ff BB |
9332 | |
9333 | if (cbp->cb_dedup_stats) | |
9334 | print_dedup_stats(config); | |
34dc7c2f BB |
9335 | } else { |
9336 | (void) printf(gettext("config: The configuration cannot be " | |
9337 | "determined.\n")); | |
9338 | } | |
9339 | ||
9340 | return (0); | |
9341 | } | |
9342 | ||
9343 | /* | |
c9d8f6c5 | 9344 | * zpool status [-c [script1,script2,...]] [-DegiLpPstvx] [--power] [-T d|u] ... |
a9520e6e | 9345 | * [pool] [interval [count]] |
34dc7c2f | 9346 | * |
8720e9e7 | 9347 | * -c CMD For each vdev, run command CMD |
c9d8f6c5 | 9348 | * -D Display dedup status (undocumented) |
08233887 | 9349 | * -e Display only unhealthy vdevs |
d2f3e292 | 9350 | * -g Display guid for individual vdev name. |
c9d8f6c5 | 9351 | * -i Display vdev initialization status. |
d2f3e292 | 9352 | * -L Follow links when resolving vdev path name. |
ad796b8a | 9353 | * -p Display values in parsable (exact) format. |
a77f29f9 | 9354 | * -P Display full path for vdev name. |
ad796b8a | 9355 | * -s Display slow IOs column. |
1b939560 | 9356 | * -t Display vdev TRIM status. |
428870ff | 9357 | * -T Display a timestamp in date(1) or Unix format |
c9d8f6c5 CH |
9358 | * -v Display complete error logs |
9359 | * -x Display only pools with potential problems | |
a9520e6e | 9360 | * --power Display vdev enclosure slot power status |
34dc7c2f BB |
9361 | * |
9362 | * Describes the health status of all pools or some subset. | |
9363 | */ | |
9364 | int | |
9365 | zpool_do_status(int argc, char **argv) | |
9366 | { | |
9367 | int c; | |
9368 | int ret; | |
193a37cb TH |
9369 | float interval = 0; |
9370 | unsigned long count = 0; | |
34dc7c2f | 9371 | status_cbdata_t cb = { 0 }; |
8720e9e7 | 9372 | char *cmd = NULL; |
34dc7c2f | 9373 | |
a9520e6e | 9374 | struct option long_options[] = { |
b1e46f86 | 9375 | {"power", no_argument, NULL, ZPOOL_OPTION_POWER}, |
a9520e6e TH |
9376 | {0, 0, 0, 0} |
9377 | }; | |
9378 | ||
34dc7c2f | 9379 | /* check options */ |
c9d8f6c5 | 9380 | while ((c = getopt_long(argc, argv, "c:DegiLpPstT:vx", long_options, |
a9520e6e | 9381 | NULL)) != -1) { |
34dc7c2f | 9382 | switch (c) { |
8720e9e7 | 9383 | case 'c': |
d6418de0 TH |
9384 | if (cmd != NULL) { |
9385 | fprintf(stderr, | |
9386 | gettext("Can't set -c flag twice\n")); | |
9387 | exit(1); | |
9388 | } | |
099700d9 GDN |
9389 | |
9390 | if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && | |
9391 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { | |
9392 | fprintf(stderr, gettext( | |
9393 | "Can't run -c, disabled by " | |
9394 | "ZPOOL_SCRIPTS_ENABLED.\n")); | |
9395 | exit(1); | |
9396 | } | |
9397 | ||
d6418de0 TH |
9398 | if ((getuid() <= 0 || geteuid() <= 0) && |
9399 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { | |
9400 | fprintf(stderr, gettext( | |
9401 | "Can't run -c with root privileges " | |
9402 | "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); | |
9403 | exit(1); | |
9404 | } | |
8720e9e7 TH |
9405 | cmd = optarg; |
9406 | break; | |
c9d8f6c5 CH |
9407 | case 'D': |
9408 | cb.cb_dedup_stats = B_TRUE; | |
9409 | break; | |
08233887 CH |
9410 | case 'e': |
9411 | cb.cb_print_unhealthy = B_TRUE; | |
9412 | break; | |
d2f3e292 RY |
9413 | case 'g': |
9414 | cb.cb_name_flags |= VDEV_NAME_GUID; | |
9415 | break; | |
c9d8f6c5 CH |
9416 | case 'i': |
9417 | cb.cb_print_vdev_init = B_TRUE; | |
9418 | break; | |
d2f3e292 RY |
9419 | case 'L': |
9420 | cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
9421 | break; | |
ad796b8a TH |
9422 | case 'p': |
9423 | cb.cb_literal = B_TRUE; | |
9424 | break; | |
a77f29f9 | 9425 | case 'P': |
d2f3e292 RY |
9426 | cb.cb_name_flags |= VDEV_NAME_PATH; |
9427 | break; | |
ad796b8a TH |
9428 | case 's': |
9429 | cb.cb_print_slow_ios = B_TRUE; | |
9430 | break; | |
1b939560 BB |
9431 | case 't': |
9432 | cb.cb_print_vdev_trim = B_TRUE; | |
9433 | break; | |
428870ff BB |
9434 | case 'T': |
9435 | get_timestamp_arg(*optarg); | |
9436 | break; | |
c9d8f6c5 CH |
9437 | case 'v': |
9438 | cb.cb_verbose = B_TRUE; | |
9439 | break; | |
9440 | case 'x': | |
9441 | cb.cb_explain = B_TRUE; | |
9442 | break; | |
b1e46f86 | 9443 | case ZPOOL_OPTION_POWER: |
a9520e6e TH |
9444 | cb.cb_print_power = B_TRUE; |
9445 | break; | |
34dc7c2f | 9446 | case '?': |
1528bfdb | 9447 | if (optopt == 'c') { |
099700d9 | 9448 | print_zpool_script_list("status"); |
d6418de0 | 9449 | exit(0); |
1528bfdb TH |
9450 | } else { |
9451 | fprintf(stderr, | |
9452 | gettext("invalid option '%c'\n"), optopt); | |
9453 | } | |
34dc7c2f BB |
9454 | usage(B_FALSE); |
9455 | } | |
9456 | } | |
9457 | ||
9458 | argc -= optind; | |
9459 | argv += optind; | |
9460 | ||
428870ff | 9461 | get_interval_count(&argc, argv, &interval, &count); |
34dc7c2f BB |
9462 | |
9463 | if (argc == 0) | |
9464 | cb.cb_allpools = B_TRUE; | |
9465 | ||
428870ff | 9466 | cb.cb_first = B_TRUE; |
fea33e4e | 9467 | cb.cb_print_status = B_TRUE; |
34dc7c2f | 9468 | |
428870ff BB |
9469 | for (;;) { |
9470 | if (timestamp_fmt != NODATE) | |
9471 | print_timestamp(timestamp_fmt); | |
34dc7c2f | 9472 | |
8720e9e7 | 9473 | if (cmd != NULL) |
1528bfdb TH |
9474 | cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd, |
9475 | NULL, NULL, 0, 0); | |
8720e9e7 | 9476 | |
2a673e76 AJ |
9477 | ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
9478 | cb.cb_literal, status_callback, &cb); | |
428870ff | 9479 | |
8720e9e7 TH |
9480 | if (cb.vcdl != NULL) |
9481 | free_vdev_cmd_data_list(cb.vcdl); | |
428870ff | 9482 | if (argc == 0 && cb.cb_count == 0) |
42cb3819 | 9483 | (void) fprintf(stderr, gettext("no pools available\n")); |
428870ff BB |
9484 | else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) |
9485 | (void) printf(gettext("all pools are healthy\n")); | |
9486 | ||
9487 | if (ret != 0) | |
9488 | return (ret); | |
9489 | ||
9490 | if (interval == 0) | |
9491 | break; | |
9492 | ||
9493 | if (count != 0 && --count == 0) | |
9494 | break; | |
9495 | ||
5f2700ee | 9496 | (void) fflush(stdout); |
193a37cb | 9497 | (void) fsleep(interval); |
428870ff BB |
9498 | } |
9499 | ||
9500 | return (0); | |
34dc7c2f BB |
9501 | } |
9502 | ||
9503 | typedef struct upgrade_cbdata { | |
34dc7c2f | 9504 | int cb_first; |
34dc7c2f BB |
9505 | int cb_argc; |
9506 | uint64_t cb_version; | |
9507 | char **cb_argv; | |
9508 | } upgrade_cbdata_t; | |
9509 | ||
287be44f DS |
9510 | static int |
9511 | check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs) | |
9512 | { | |
02730c33 | 9513 | int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION); |
287be44f DS |
9514 | int *count = (int *)unsupp_fs; |
9515 | ||
9516 | if (zfs_version > ZPL_VERSION) { | |
9517 | (void) printf(gettext("%s (v%d) is not supported by this " | |
9518 | "implementation of ZFS.\n"), | |
9519 | zfs_get_name(zhp), zfs_version); | |
9520 | (*count)++; | |
9521 | } | |
9522 | ||
baca06c2 | 9523 | zfs_iter_filesystems_v2(zhp, 0, check_unsupp_fs, unsupp_fs); |
287be44f DS |
9524 | |
9525 | zfs_close(zhp); | |
9526 | ||
9527 | return (0); | |
9528 | } | |
9529 | ||
b9b24bb4 CS |
9530 | static int |
9531 | upgrade_version(zpool_handle_t *zhp, uint64_t version) | |
9532 | { | |
9533 | int ret; | |
9534 | nvlist_t *config; | |
9535 | uint64_t oldversion; | |
287be44f | 9536 | int unsupp_fs = 0; |
b9b24bb4 CS |
9537 | |
9538 | config = zpool_get_config(zhp, NULL); | |
9539 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
9540 | &oldversion) == 0); | |
9541 | ||
e086db16 CB |
9542 | char compat[ZFS_MAXPROPLEN]; |
9543 | if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, | |
9544 | ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) | |
9545 | compat[0] = '\0'; | |
9546 | ||
b9b24bb4 CS |
9547 | assert(SPA_VERSION_IS_SUPPORTED(oldversion)); |
9548 | assert(oldversion < version); | |
9549 | ||
287be44f DS |
9550 | ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs); |
9551 | if (ret != 0) | |
9552 | return (ret); | |
9553 | ||
9554 | if (unsupp_fs) { | |
5c7afad4 BB |
9555 | (void) fprintf(stderr, gettext("Upgrade not performed due " |
9556 | "to %d unsupported filesystems (max v%d).\n"), | |
02730c33 | 9557 | unsupp_fs, (int)ZPL_VERSION); |
287be44f DS |
9558 | return (1); |
9559 | } | |
9560 | ||
e086db16 CB |
9561 | if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) { |
9562 | (void) fprintf(stderr, gettext("Upgrade not performed because " | |
9563 | "'compatibility' property set to '" | |
9564 | ZPOOL_COMPAT_LEGACY "'.\n")); | |
9565 | return (1); | |
9566 | } | |
9567 | ||
b9b24bb4 CS |
9568 | ret = zpool_upgrade(zhp, version); |
9569 | if (ret != 0) | |
9570 | return (ret); | |
9571 | ||
9572 | if (version >= SPA_VERSION_FEATURES) { | |
9573 | (void) printf(gettext("Successfully upgraded " | |
9574 | "'%s' from version %llu to feature flags.\n"), | |
02730c33 | 9575 | zpool_get_name(zhp), (u_longlong_t)oldversion); |
b9b24bb4 CS |
9576 | } else { |
9577 | (void) printf(gettext("Successfully upgraded " | |
9578 | "'%s' from version %llu to version %llu.\n"), | |
02730c33 BB |
9579 | zpool_get_name(zhp), (u_longlong_t)oldversion, |
9580 | (u_longlong_t)version); | |
b9b24bb4 CS |
9581 | } |
9582 | ||
9583 | return (0); | |
9584 | } | |
9585 | ||
9586 | static int | |
9587 | upgrade_enable_all(zpool_handle_t *zhp, int *countp) | |
9588 | { | |
9589 | int i, ret, count; | |
9590 | boolean_t firstff = B_TRUE; | |
9591 | nvlist_t *enabled = zpool_get_features(zhp); | |
9592 | ||
658fb802 CB |
9593 | char compat[ZFS_MAXPROPLEN]; |
9594 | if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, | |
9595 | ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) | |
9596 | compat[0] = '\0'; | |
9597 | ||
9598 | boolean_t requested_features[SPA_FEATURES]; | |
9599 | if (zpool_do_load_compat(compat, requested_features) != | |
9600 | ZPOOL_COMPATIBILITY_OK) | |
9601 | return (-1); | |
9602 | ||
b9b24bb4 CS |
9603 | count = 0; |
9604 | for (i = 0; i < SPA_FEATURES; i++) { | |
9605 | const char *fname = spa_feature_table[i].fi_uname; | |
9606 | const char *fguid = spa_feature_table[i].fi_guid; | |
35ec5179 BB |
9607 | |
9608 | if (!spa_feature_table[i].fi_zfs_mod_supported) | |
9609 | continue; | |
9610 | ||
658fb802 | 9611 | if (!nvlist_exists(enabled, fguid) && requested_features[i]) { |
b9b24bb4 CS |
9612 | char *propname; |
9613 | verify(-1 != asprintf(&propname, "feature@%s", fname)); | |
9614 | ret = zpool_set_prop(zhp, propname, | |
9615 | ZFS_FEATURE_ENABLED); | |
9616 | if (ret != 0) { | |
9617 | free(propname); | |
9618 | return (ret); | |
9619 | } | |
9620 | count++; | |
9621 | ||
9622 | if (firstff) { | |
9623 | (void) printf(gettext("Enabled the " | |
9624 | "following features on '%s':\n"), | |
9625 | zpool_get_name(zhp)); | |
9626 | firstff = B_FALSE; | |
9627 | } | |
9628 | (void) printf(gettext(" %s\n"), fname); | |
9629 | free(propname); | |
9630 | } | |
9631 | } | |
9632 | ||
9633 | if (countp != NULL) | |
9634 | *countp = count; | |
9635 | return (0); | |
9636 | } | |
9637 | ||
34dc7c2f BB |
9638 | static int |
9639 | upgrade_cb(zpool_handle_t *zhp, void *arg) | |
9640 | { | |
9641 | upgrade_cbdata_t *cbp = arg; | |
9642 | nvlist_t *config; | |
9643 | uint64_t version; | |
65d9212a | 9644 | boolean_t modified_pool = B_FALSE; |
b9b24bb4 | 9645 | int ret; |
34dc7c2f BB |
9646 | |
9647 | config = zpool_get_config(zhp, NULL); | |
9648 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
9649 | &version) == 0); | |
9650 | ||
b9b24bb4 | 9651 | assert(SPA_VERSION_IS_SUPPORTED(version)); |
34dc7c2f | 9652 | |
b9b24bb4 CS |
9653 | if (version < cbp->cb_version) { |
9654 | cbp->cb_first = B_FALSE; | |
9655 | ret = upgrade_version(zhp, cbp->cb_version); | |
9656 | if (ret != 0) | |
9657 | return (ret); | |
65d9212a | 9658 | modified_pool = B_TRUE; |
b9b24bb4 | 9659 | |
b9b24bb4 CS |
9660 | /* |
9661 | * If they did "zpool upgrade -a", then we could | |
9662 | * be doing ioctls to different pools. We need | |
9663 | * to log this history once to each pool, and bypass | |
9664 | * the normal history logging that happens in main(). | |
9665 | */ | |
9666 | (void) zpool_log_history(g_zfs, history_str); | |
9667 | log_history = B_FALSE; | |
b9b24bb4 CS |
9668 | } |
9669 | ||
9670 | if (cbp->cb_version >= SPA_VERSION_FEATURES) { | |
9671 | int count; | |
9672 | ret = upgrade_enable_all(zhp, &count); | |
9673 | if (ret != 0) | |
9674 | return (ret); | |
9675 | ||
9676 | if (count > 0) { | |
34dc7c2f | 9677 | cbp->cb_first = B_FALSE; |
65d9212a | 9678 | modified_pool = B_TRUE; |
34dc7c2f | 9679 | } |
b9b24bb4 | 9680 | } |
34dc7c2f | 9681 | |
65d9212a | 9682 | if (modified_pool) { |
9683 | (void) printf("\n"); | |
9684 | (void) after_zpool_upgrade(zhp); | |
b9b24bb4 CS |
9685 | } |
9686 | ||
9687 | return (0); | |
9688 | } | |
9689 | ||
9690 | static int | |
9691 | upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) | |
9692 | { | |
9693 | upgrade_cbdata_t *cbp = arg; | |
9694 | nvlist_t *config; | |
9695 | uint64_t version; | |
9696 | ||
9697 | config = zpool_get_config(zhp, NULL); | |
9698 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
9699 | &version) == 0); | |
9700 | ||
9701 | assert(SPA_VERSION_IS_SUPPORTED(version)); | |
9702 | ||
9703 | if (version < SPA_VERSION_FEATURES) { | |
34dc7c2f BB |
9704 | if (cbp->cb_first) { |
9705 | (void) printf(gettext("The following pools are " | |
b9b24bb4 CS |
9706 | "formatted with legacy version numbers and can\n" |
9707 | "be upgraded to use feature flags. After " | |
9708 | "being upgraded, these pools\nwill no " | |
9709 | "longer be accessible by software that does not " | |
e086db16 CB |
9710 | "support feature\nflags.\n\n" |
9711 | "Note that setting a pool's 'compatibility' " | |
9712 | "feature to '" ZPOOL_COMPAT_LEGACY "' will\n" | |
9713 | "inhibit upgrades.\n\n")); | |
34dc7c2f BB |
9714 | (void) printf(gettext("VER POOL\n")); |
9715 | (void) printf(gettext("--- ------------\n")); | |
9716 | cbp->cb_first = B_FALSE; | |
9717 | } | |
9718 | ||
9719 | (void) printf("%2llu %s\n", (u_longlong_t)version, | |
9720 | zpool_get_name(zhp)); | |
9721 | } | |
9722 | ||
b9b24bb4 CS |
9723 | return (0); |
9724 | } | |
9725 | ||
9726 | static int | |
9727 | upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) | |
9728 | { | |
9729 | upgrade_cbdata_t *cbp = arg; | |
9730 | nvlist_t *config; | |
9731 | uint64_t version; | |
9732 | ||
9733 | config = zpool_get_config(zhp, NULL); | |
9734 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
9735 | &version) == 0); | |
9736 | ||
9737 | if (version >= SPA_VERSION_FEATURES) { | |
9738 | int i; | |
9739 | boolean_t poolfirst = B_TRUE; | |
9740 | nvlist_t *enabled = zpool_get_features(zhp); | |
9741 | ||
9742 | for (i = 0; i < SPA_FEATURES; i++) { | |
9743 | const char *fguid = spa_feature_table[i].fi_guid; | |
9744 | const char *fname = spa_feature_table[i].fi_uname; | |
35ec5179 BB |
9745 | |
9746 | if (!spa_feature_table[i].fi_zfs_mod_supported) | |
9747 | continue; | |
9748 | ||
b9b24bb4 CS |
9749 | if (!nvlist_exists(enabled, fguid)) { |
9750 | if (cbp->cb_first) { | |
9751 | (void) printf(gettext("\nSome " | |
9752 | "supported features are not " | |
9753 | "enabled on the following pools. " | |
9754 | "Once a\nfeature is enabled the " | |
9755 | "pool may become incompatible with " | |
9756 | "software\nthat does not support " | |
9757 | "the feature. See " | |
2badb345 | 9758 | "zpool-features(7) for " |
e086db16 CB |
9759 | "details.\n\n" |
9760 | "Note that the pool " | |
9761 | "'compatibility' feature can be " | |
9762 | "used to inhibit\nfeature " | |
9763 | "upgrades.\n\n")); | |
b9b24bb4 CS |
9764 | (void) printf(gettext("POOL " |
9765 | "FEATURE\n")); | |
9766 | (void) printf(gettext("------" | |
9767 | "---------\n")); | |
9768 | cbp->cb_first = B_FALSE; | |
9769 | } | |
9770 | ||
9771 | if (poolfirst) { | |
9772 | (void) printf(gettext("%s\n"), | |
9773 | zpool_get_name(zhp)); | |
9774 | poolfirst = B_FALSE; | |
9775 | } | |
9776 | ||
9777 | (void) printf(gettext(" %s\n"), fname); | |
9778 | } | |
6f1ffb06 MA |
9779 | /* |
9780 | * If they did "zpool upgrade -a", then we could | |
9781 | * be doing ioctls to different pools. We need | |
9782 | * to log this history once to each pool, and bypass | |
9783 | * the normal history logging that happens in main(). | |
9784 | */ | |
9785 | (void) zpool_log_history(g_zfs, history_str); | |
9786 | log_history = B_FALSE; | |
b9b24bb4 CS |
9787 | } |
9788 | } | |
9789 | ||
9790 | return (0); | |
34dc7c2f BB |
9791 | } |
9792 | ||
34dc7c2f BB |
9793 | static int |
9794 | upgrade_one(zpool_handle_t *zhp, void *data) | |
9795 | { | |
65d9212a | 9796 | boolean_t modified_pool = B_FALSE; |
34dc7c2f BB |
9797 | upgrade_cbdata_t *cbp = data; |
9798 | uint64_t cur_version; | |
9799 | int ret; | |
9800 | ||
9801 | if (strcmp("log", zpool_get_name(zhp)) == 0) { | |
c66989ba | 9802 | (void) fprintf(stderr, gettext("'log' is now a reserved word\n" |
34dc7c2f BB |
9803 | "Pool 'log' must be renamed using export and import" |
9804 | " to upgrade.\n")); | |
9805 | return (1); | |
9806 | } | |
9807 | ||
9808 | cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); | |
9809 | if (cur_version > cbp->cb_version) { | |
9810 | (void) printf(gettext("Pool '%s' is already formatted " | |
b9b24bb4 | 9811 | "using more current version '%llu'.\n\n"), |
02730c33 | 9812 | zpool_get_name(zhp), (u_longlong_t)cur_version); |
34dc7c2f BB |
9813 | return (0); |
9814 | } | |
b9b24bb4 CS |
9815 | |
9816 | if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { | |
34dc7c2f | 9817 | (void) printf(gettext("Pool '%s' is already formatted " |
b9b24bb4 | 9818 | "using version %llu.\n\n"), zpool_get_name(zhp), |
02730c33 | 9819 | (u_longlong_t)cbp->cb_version); |
34dc7c2f BB |
9820 | return (0); |
9821 | } | |
9822 | ||
b9b24bb4 | 9823 | if (cur_version != cbp->cb_version) { |
65d9212a | 9824 | modified_pool = B_TRUE; |
b9b24bb4 CS |
9825 | ret = upgrade_version(zhp, cbp->cb_version); |
9826 | if (ret != 0) | |
9827 | return (ret); | |
9828 | } | |
34dc7c2f | 9829 | |
b9b24bb4 CS |
9830 | if (cbp->cb_version >= SPA_VERSION_FEATURES) { |
9831 | int count = 0; | |
9832 | ret = upgrade_enable_all(zhp, &count); | |
9833 | if (ret != 0) | |
9834 | return (ret); | |
9835 | ||
9836 | if (count != 0) { | |
65d9212a | 9837 | modified_pool = B_TRUE; |
b9b24bb4 CS |
9838 | } else if (cur_version == SPA_VERSION) { |
9839 | (void) printf(gettext("Pool '%s' already has all " | |
658fb802 | 9840 | "supported and requested features enabled.\n"), |
b9b24bb4 CS |
9841 | zpool_get_name(zhp)); |
9842 | } | |
9843 | } | |
9844 | ||
65d9212a | 9845 | if (modified_pool) { |
9846 | (void) printf("\n"); | |
9847 | (void) after_zpool_upgrade(zhp); | |
34dc7c2f BB |
9848 | } |
9849 | ||
b9b24bb4 | 9850 | return (0); |
34dc7c2f BB |
9851 | } |
9852 | ||
9853 | /* | |
9854 | * zpool upgrade | |
9855 | * zpool upgrade -v | |
9856 | * zpool upgrade [-V version] <-a | pool ...> | |
9857 | * | |
9858 | * With no arguments, display downrev'd ZFS pool available for upgrade. | |
9859 | * Individual pools can be upgraded by specifying the pool, and '-a' will | |
9860 | * upgrade all pools. | |
9861 | */ | |
9862 | int | |
9863 | zpool_do_upgrade(int argc, char **argv) | |
9864 | { | |
9865 | int c; | |
9866 | upgrade_cbdata_t cb = { 0 }; | |
9867 | int ret = 0; | |
9868 | boolean_t showversions = B_FALSE; | |
b9b24bb4 | 9869 | boolean_t upgradeall = B_FALSE; |
34dc7c2f BB |
9870 | char *end; |
9871 | ||
9872 | ||
9873 | /* check options */ | |
9babb374 | 9874 | while ((c = getopt(argc, argv, ":avV:")) != -1) { |
34dc7c2f BB |
9875 | switch (c) { |
9876 | case 'a': | |
b9b24bb4 | 9877 | upgradeall = B_TRUE; |
34dc7c2f BB |
9878 | break; |
9879 | case 'v': | |
9880 | showversions = B_TRUE; | |
9881 | break; | |
9882 | case 'V': | |
9883 | cb.cb_version = strtoll(optarg, &end, 10); | |
9ae529ec CS |
9884 | if (*end != '\0' || |
9885 | !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { | |
34dc7c2f BB |
9886 | (void) fprintf(stderr, |
9887 | gettext("invalid version '%s'\n"), optarg); | |
9888 | usage(B_FALSE); | |
9889 | } | |
9890 | break; | |
9babb374 BB |
9891 | case ':': |
9892 | (void) fprintf(stderr, gettext("missing argument for " | |
9893 | "'%c' option\n"), optopt); | |
9894 | usage(B_FALSE); | |
9895 | break; | |
34dc7c2f BB |
9896 | case '?': |
9897 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
9898 | optopt); | |
9899 | usage(B_FALSE); | |
9900 | } | |
9901 | } | |
9902 | ||
9903 | cb.cb_argc = argc; | |
9904 | cb.cb_argv = argv; | |
9905 | argc -= optind; | |
9906 | argv += optind; | |
9907 | ||
9908 | if (cb.cb_version == 0) { | |
9909 | cb.cb_version = SPA_VERSION; | |
b9b24bb4 | 9910 | } else if (!upgradeall && argc == 0) { |
34dc7c2f BB |
9911 | (void) fprintf(stderr, gettext("-V option is " |
9912 | "incompatible with other arguments\n")); | |
9913 | usage(B_FALSE); | |
9914 | } | |
9915 | ||
9916 | if (showversions) { | |
b9b24bb4 | 9917 | if (upgradeall || argc != 0) { |
34dc7c2f BB |
9918 | (void) fprintf(stderr, gettext("-v option is " |
9919 | "incompatible with other arguments\n")); | |
9920 | usage(B_FALSE); | |
9921 | } | |
b9b24bb4 | 9922 | } else if (upgradeall) { |
34dc7c2f BB |
9923 | if (argc != 0) { |
9924 | (void) fprintf(stderr, gettext("-a option should not " | |
9925 | "be used along with a pool name\n")); | |
9926 | usage(B_FALSE); | |
9927 | } | |
9928 | } | |
9929 | ||
63652e15 | 9930 | (void) printf("%s", gettext("This system supports ZFS pool feature " |
9ae529ec | 9931 | "flags.\n\n")); |
34dc7c2f | 9932 | if (showversions) { |
b9b24bb4 CS |
9933 | int i; |
9934 | ||
9935 | (void) printf(gettext("The following features are " | |
9936 | "supported:\n\n")); | |
9937 | (void) printf(gettext("FEAT DESCRIPTION\n")); | |
9938 | (void) printf("----------------------------------------------" | |
9939 | "---------------\n"); | |
9940 | for (i = 0; i < SPA_FEATURES; i++) { | |
9941 | zfeature_info_t *fi = &spa_feature_table[i]; | |
03ef8f09 MM |
9942 | if (!fi->fi_zfs_mod_supported) |
9943 | continue; | |
241b5415 MA |
9944 | const char *ro = |
9945 | (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? | |
b9b24bb4 CS |
9946 | " (read-only compatible)" : ""; |
9947 | ||
9948 | (void) printf("%-37s%s\n", fi->fi_uname, ro); | |
9949 | (void) printf(" %s\n", fi->fi_desc); | |
9950 | } | |
9951 | (void) printf("\n"); | |
9952 | ||
9953 | (void) printf(gettext("The following legacy versions are also " | |
34dc7c2f BB |
9954 | "supported:\n\n")); |
9955 | (void) printf(gettext("VER DESCRIPTION\n")); | |
9956 | (void) printf("--- -----------------------------------------" | |
9957 | "---------------\n"); | |
9958 | (void) printf(gettext(" 1 Initial ZFS version\n")); | |
9959 | (void) printf(gettext(" 2 Ditto blocks " | |
9960 | "(replicated metadata)\n")); | |
9961 | (void) printf(gettext(" 3 Hot spares and double parity " | |
9962 | "RAID-Z\n")); | |
9963 | (void) printf(gettext(" 4 zpool history\n")); | |
9964 | (void) printf(gettext(" 5 Compression using the gzip " | |
9965 | "algorithm\n")); | |
9966 | (void) printf(gettext(" 6 bootfs pool property\n")); | |
9967 | (void) printf(gettext(" 7 Separate intent log devices\n")); | |
9968 | (void) printf(gettext(" 8 Delegated administration\n")); | |
9969 | (void) printf(gettext(" 9 refquota and refreservation " | |
9970 | "properties\n")); | |
9971 | (void) printf(gettext(" 10 Cache devices\n")); | |
b128c09f BB |
9972 | (void) printf(gettext(" 11 Improved scrub performance\n")); |
9973 | (void) printf(gettext(" 12 Snapshot properties\n")); | |
9974 | (void) printf(gettext(" 13 snapused property\n")); | |
9babb374 BB |
9975 | (void) printf(gettext(" 14 passthrough-x aclinherit\n")); |
9976 | (void) printf(gettext(" 15 user/group space accounting\n")); | |
9977 | (void) printf(gettext(" 16 stmf property support\n")); | |
45d1cae3 | 9978 | (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); |
428870ff BB |
9979 | (void) printf(gettext(" 18 Snapshot user holds\n")); |
9980 | (void) printf(gettext(" 19 Log device removal\n")); | |
9981 | (void) printf(gettext(" 20 Compression using zle " | |
9982 | "(zero-length encoding)\n")); | |
9983 | (void) printf(gettext(" 21 Deduplication\n")); | |
9984 | (void) printf(gettext(" 22 Received properties\n")); | |
9985 | (void) printf(gettext(" 23 Slim ZIL\n")); | |
9986 | (void) printf(gettext(" 24 System attributes\n")); | |
9987 | (void) printf(gettext(" 25 Improved scrub stats\n")); | |
9988 | (void) printf(gettext(" 26 Improved snapshot deletion " | |
9989 | "performance\n")); | |
572e2857 BB |
9990 | (void) printf(gettext(" 27 Improved snapshot creation " |
9991 | "performance\n")); | |
9992 | (void) printf(gettext(" 28 Multiple vdev replacements\n")); | |
428870ff BB |
9993 | (void) printf(gettext("\nFor more information on a particular " |
9994 | "version, including supported releases,\n")); | |
9995 | (void) printf(gettext("see the ZFS Administration Guide.\n\n")); | |
b9b24bb4 CS |
9996 | } else if (argc == 0 && upgradeall) { |
9997 | cb.cb_first = B_TRUE; | |
34dc7c2f | 9998 | ret = zpool_iter(g_zfs, upgrade_cb, &cb); |
b9b24bb4 CS |
9999 | if (ret == 0 && cb.cb_first) { |
10000 | if (cb.cb_version == SPA_VERSION) { | |
10001 | (void) printf(gettext("All pools are already " | |
10002 | "formatted using feature flags.\n\n")); | |
10003 | (void) printf(gettext("Every feature flags " | |
658fb802 CB |
10004 | "pool already has all supported and " |
10005 | "requested features enabled.\n")); | |
b9b24bb4 CS |
10006 | } else { |
10007 | (void) printf(gettext("All pools are already " | |
10008 | "formatted with version %llu or higher.\n"), | |
02730c33 | 10009 | (u_longlong_t)cb.cb_version); |
34dc7c2f BB |
10010 | } |
10011 | } | |
b9b24bb4 CS |
10012 | } else if (argc == 0) { |
10013 | cb.cb_first = B_TRUE; | |
10014 | ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); | |
10015 | assert(ret == 0); | |
34dc7c2f | 10016 | |
b9b24bb4 CS |
10017 | if (cb.cb_first) { |
10018 | (void) printf(gettext("All pools are formatted " | |
10019 | "using feature flags.\n\n")); | |
10020 | } else { | |
10021 | (void) printf(gettext("\nUse 'zpool upgrade -v' " | |
10022 | "for a list of available legacy versions.\n")); | |
10023 | } | |
10024 | ||
10025 | cb.cb_first = B_TRUE; | |
10026 | ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); | |
10027 | assert(ret == 0); | |
10028 | ||
10029 | if (cb.cb_first) { | |
10030 | (void) printf(gettext("Every feature flags pool has " | |
658fb802 | 10031 | "all supported and requested features enabled.\n")); |
b9b24bb4 CS |
10032 | } else { |
10033 | (void) printf(gettext("\n")); | |
34dc7c2f BB |
10034 | } |
10035 | } else { | |
2a673e76 AJ |
10036 | ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, |
10037 | B_FALSE, upgrade_one, &cb); | |
34dc7c2f BB |
10038 | } |
10039 | ||
10040 | return (ret); | |
10041 | } | |
10042 | ||
10043 | typedef struct hist_cbdata { | |
10044 | boolean_t first; | |
6f1ffb06 MA |
10045 | boolean_t longfmt; |
10046 | boolean_t internal; | |
34dc7c2f BB |
10047 | } hist_cbdata_t; |
10048 | ||
7125a109 CC |
10049 | static void |
10050 | print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb) | |
34dc7c2f | 10051 | { |
34dc7c2f BB |
10052 | nvlist_t **records; |
10053 | uint_t numrecords; | |
7125a109 | 10054 | int i; |
34dc7c2f BB |
10055 | |
10056 | verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, | |
10057 | &records, &numrecords) == 0); | |
10058 | for (i = 0; i < numrecords; i++) { | |
6f1ffb06 | 10059 | nvlist_t *rec = records[i]; |
2ac90457 | 10060 | char tbuf[64] = ""; |
34dc7c2f | 10061 | |
6f1ffb06 MA |
10062 | if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { |
10063 | time_t tsec; | |
10064 | struct tm t; | |
34dc7c2f | 10065 | |
6f1ffb06 MA |
10066 | tsec = fnvlist_lookup_uint64(records[i], |
10067 | ZPOOL_HIST_TIME); | |
10068 | (void) localtime_r(&tsec, &t); | |
10069 | (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); | |
10070 | } | |
10071 | ||
2ac90457 MA |
10072 | if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) { |
10073 | uint64_t elapsed_ns = fnvlist_lookup_int64(records[i], | |
10074 | ZPOOL_HIST_ELAPSED_NS); | |
10075 | (void) snprintf(tbuf + strlen(tbuf), | |
10076 | sizeof (tbuf) - strlen(tbuf), | |
10077 | " (%lldms)", (long long)elapsed_ns / 1000 / 1000); | |
10078 | } | |
10079 | ||
6f1ffb06 MA |
10080 | if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { |
10081 | (void) printf("%s %s", tbuf, | |
10082 | fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); | |
10083 | } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { | |
10084 | int ievent = | |
10085 | fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); | |
10086 | if (!cb->internal) | |
34dc7c2f | 10087 | continue; |
6f1ffb06 MA |
10088 | if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { |
10089 | (void) printf("%s unrecognized record:\n", | |
10090 | tbuf); | |
10091 | dump_nvlist(rec, 4); | |
34dc7c2f | 10092 | continue; |
6f1ffb06 MA |
10093 | } |
10094 | (void) printf("%s [internal %s txg:%lld] %s", tbuf, | |
10095 | zfs_history_event_names[ievent], | |
02730c33 | 10096 | (longlong_t)fnvlist_lookup_uint64( |
d1d7e268 | 10097 | rec, ZPOOL_HIST_TXG), |
6f1ffb06 MA |
10098 | fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); |
10099 | } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { | |
10100 | if (!cb->internal) | |
10101 | continue; | |
10102 | (void) printf("%s [txg:%lld] %s", tbuf, | |
02730c33 | 10103 | (longlong_t)fnvlist_lookup_uint64( |
d1d7e268 | 10104 | rec, ZPOOL_HIST_TXG), |
6f1ffb06 MA |
10105 | fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); |
10106 | if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { | |
10107 | (void) printf(" %s (%llu)", | |
10108 | fnvlist_lookup_string(rec, | |
10109 | ZPOOL_HIST_DSNAME), | |
d1d7e268 | 10110 | (u_longlong_t)fnvlist_lookup_uint64(rec, |
6f1ffb06 MA |
10111 | ZPOOL_HIST_DSID)); |
10112 | } | |
10113 | (void) printf(" %s", fnvlist_lookup_string(rec, | |
10114 | ZPOOL_HIST_INT_STR)); | |
10115 | } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { | |
10116 | if (!cb->internal) | |
10117 | continue; | |
10118 | (void) printf("%s ioctl %s\n", tbuf, | |
10119 | fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); | |
10120 | if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { | |
10121 | (void) printf(" input:\n"); | |
10122 | dump_nvlist(fnvlist_lookup_nvlist(rec, | |
10123 | ZPOOL_HIST_INPUT_NVL), 8); | |
10124 | } | |
10125 | if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { | |
10126 | (void) printf(" output:\n"); | |
10127 | dump_nvlist(fnvlist_lookup_nvlist(rec, | |
10128 | ZPOOL_HIST_OUTPUT_NVL), 8); | |
10129 | } | |
d66aab7c MA |
10130 | if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) { |
10131 | (void) printf(" output nvlist omitted; " | |
10132 | "original size: %lldKB\n", | |
10133 | (longlong_t)fnvlist_lookup_int64(rec, | |
10134 | ZPOOL_HIST_OUTPUT_SIZE) / 1024); | |
10135 | } | |
d99a0153 CW |
10136 | if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) { |
10137 | (void) printf(" errno: %lld\n", | |
10138 | (longlong_t)fnvlist_lookup_int64(rec, | |
10139 | ZPOOL_HIST_ERRNO)); | |
10140 | } | |
6f1ffb06 MA |
10141 | } else { |
10142 | if (!cb->internal) | |
10143 | continue; | |
10144 | (void) printf("%s unrecognized record:\n", tbuf); | |
10145 | dump_nvlist(rec, 4); | |
34dc7c2f | 10146 | } |
34dc7c2f BB |
10147 | |
10148 | if (!cb->longfmt) { | |
10149 | (void) printf("\n"); | |
10150 | continue; | |
10151 | } | |
10152 | (void) printf(" ["); | |
6f1ffb06 MA |
10153 | if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { |
10154 | uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); | |
10155 | struct passwd *pwd = getpwuid(who); | |
10156 | (void) printf("user %d ", (int)who); | |
10157 | if (pwd != NULL) | |
10158 | (void) printf("(%s) ", pwd->pw_name); | |
34dc7c2f | 10159 | } |
6f1ffb06 MA |
10160 | if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { |
10161 | (void) printf("on %s", | |
10162 | fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); | |
34dc7c2f | 10163 | } |
6f1ffb06 MA |
10164 | if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { |
10165 | (void) printf(":%s", | |
10166 | fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); | |
34dc7c2f BB |
10167 | } |
10168 | ||
10169 | (void) printf("]"); | |
10170 | (void) printf("\n"); | |
10171 | } | |
7125a109 CC |
10172 | } |
10173 | ||
10174 | /* | |
10175 | * Print out the command history for a specific pool. | |
10176 | */ | |
10177 | static int | |
10178 | get_history_one(zpool_handle_t *zhp, void *data) | |
10179 | { | |
10180 | nvlist_t *nvhis; | |
10181 | int ret; | |
10182 | hist_cbdata_t *cb = (hist_cbdata_t *)data; | |
10183 | uint64_t off = 0; | |
10184 | boolean_t eof = B_FALSE; | |
10185 | ||
10186 | cb->first = B_FALSE; | |
10187 | ||
10188 | (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); | |
10189 | ||
10190 | while (!eof) { | |
10191 | if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0) | |
10192 | return (ret); | |
10193 | ||
10194 | print_history_records(nvhis, cb); | |
10195 | nvlist_free(nvhis); | |
10196 | } | |
34dc7c2f | 10197 | (void) printf("\n"); |
34dc7c2f BB |
10198 | |
10199 | return (ret); | |
10200 | } | |
10201 | ||
10202 | /* | |
10203 | * zpool history <pool> | |
10204 | * | |
10205 | * Displays the history of commands that modified pools. | |
10206 | */ | |
34dc7c2f BB |
10207 | int |
10208 | zpool_do_history(int argc, char **argv) | |
10209 | { | |
10210 | hist_cbdata_t cbdata = { 0 }; | |
10211 | int ret; | |
10212 | int c; | |
10213 | ||
10214 | cbdata.first = B_TRUE; | |
10215 | /* check options */ | |
10216 | while ((c = getopt(argc, argv, "li")) != -1) { | |
10217 | switch (c) { | |
10218 | case 'l': | |
6f1ffb06 | 10219 | cbdata.longfmt = B_TRUE; |
34dc7c2f BB |
10220 | break; |
10221 | case 'i': | |
6f1ffb06 | 10222 | cbdata.internal = B_TRUE; |
34dc7c2f BB |
10223 | break; |
10224 | case '?': | |
10225 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
10226 | optopt); | |
10227 | usage(B_FALSE); | |
10228 | } | |
10229 | } | |
10230 | argc -= optind; | |
10231 | argv += optind; | |
10232 | ||
2a673e76 AJ |
10233 | ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, |
10234 | B_FALSE, get_history_one, &cbdata); | |
34dc7c2f BB |
10235 | |
10236 | if (argc == 0 && cbdata.first == B_TRUE) { | |
42cb3819 | 10237 | (void) fprintf(stderr, gettext("no pools available\n")); |
34dc7c2f BB |
10238 | return (0); |
10239 | } | |
10240 | ||
10241 | return (ret); | |
10242 | } | |
10243 | ||
26685276 BB |
10244 | typedef struct ev_opts { |
10245 | int verbose; | |
c5343ba7 | 10246 | int scripted; |
26685276 BB |
10247 | int follow; |
10248 | int clear; | |
88f9c939 | 10249 | char poolname[ZFS_MAX_DATASET_NAME_LEN]; |
26685276 BB |
10250 | } ev_opts_t; |
10251 | ||
10252 | static void | |
1e1c3980 | 10253 | zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts) |
26685276 | 10254 | { |
d1807f16 RY |
10255 | char ctime_str[26], str[32]; |
10256 | const char *ptr; | |
26685276 BB |
10257 | int64_t *tv; |
10258 | uint_t n; | |
10259 | ||
10260 | verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0); | |
10261 | memset(str, ' ', 32); | |
10262 | (void) ctime_r((const time_t *)&tv[0], ctime_str); | |
7cd723e6 GA |
10263 | (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */ |
10264 | (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */ | |
10265 | (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */ | |
d1d7e268 | 10266 | (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */ |
1e1c3980 SH |
10267 | if (opts->scripted) |
10268 | (void) printf(gettext("%s\t"), str); | |
10269 | else | |
10270 | (void) printf(gettext("%s "), str); | |
26685276 BB |
10271 | |
10272 | verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0); | |
10273 | (void) printf(gettext("%s\n"), ptr); | |
10274 | } | |
10275 | ||
10276 | static void | |
10277 | zpool_do_events_nvprint(nvlist_t *nvl, int depth) | |
10278 | { | |
10279 | nvpair_t *nvp; | |
10280 | ||
10281 | for (nvp = nvlist_next_nvpair(nvl, NULL); | |
10282 | nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { | |
10283 | ||
10284 | data_type_t type = nvpair_type(nvp); | |
10285 | const char *name = nvpair_name(nvp); | |
10286 | ||
10287 | boolean_t b; | |
10288 | uint8_t i8; | |
10289 | uint16_t i16; | |
10290 | uint32_t i32; | |
10291 | uint64_t i64; | |
d1807f16 | 10292 | const char *str; |
26685276 BB |
10293 | nvlist_t *cnv; |
10294 | ||
10295 | printf(gettext("%*s%s = "), depth, "", name); | |
10296 | ||
10297 | switch (type) { | |
10298 | case DATA_TYPE_BOOLEAN: | |
10299 | printf(gettext("%s"), "1"); | |
10300 | break; | |
10301 | ||
10302 | case DATA_TYPE_BOOLEAN_VALUE: | |
10303 | (void) nvpair_value_boolean_value(nvp, &b); | |
10304 | printf(gettext("%s"), b ? "1" : "0"); | |
10305 | break; | |
10306 | ||
10307 | case DATA_TYPE_BYTE: | |
10308 | (void) nvpair_value_byte(nvp, &i8); | |
10309 | printf(gettext("0x%x"), i8); | |
10310 | break; | |
10311 | ||
10312 | case DATA_TYPE_INT8: | |
10313 | (void) nvpair_value_int8(nvp, (void *)&i8); | |
10314 | printf(gettext("0x%x"), i8); | |
10315 | break; | |
10316 | ||
10317 | case DATA_TYPE_UINT8: | |
10318 | (void) nvpair_value_uint8(nvp, &i8); | |
10319 | printf(gettext("0x%x"), i8); | |
10320 | break; | |
10321 | ||
10322 | case DATA_TYPE_INT16: | |
10323 | (void) nvpair_value_int16(nvp, (void *)&i16); | |
10324 | printf(gettext("0x%x"), i16); | |
10325 | break; | |
10326 | ||
10327 | case DATA_TYPE_UINT16: | |
10328 | (void) nvpair_value_uint16(nvp, &i16); | |
10329 | printf(gettext("0x%x"), i16); | |
10330 | break; | |
10331 | ||
10332 | case DATA_TYPE_INT32: | |
10333 | (void) nvpair_value_int32(nvp, (void *)&i32); | |
10334 | printf(gettext("0x%x"), i32); | |
10335 | break; | |
10336 | ||
10337 | case DATA_TYPE_UINT32: | |
10338 | (void) nvpair_value_uint32(nvp, &i32); | |
10339 | printf(gettext("0x%x"), i32); | |
10340 | break; | |
10341 | ||
10342 | case DATA_TYPE_INT64: | |
10343 | (void) nvpair_value_int64(nvp, (void *)&i64); | |
10344 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
10345 | break; | |
10346 | ||
10347 | case DATA_TYPE_UINT64: | |
10348 | (void) nvpair_value_uint64(nvp, &i64); | |
d02ca379 DB |
10349 | /* |
10350 | * translate vdev state values to readable | |
10351 | * strings to aide zpool events consumers | |
10352 | */ | |
10353 | if (strcmp(name, | |
10354 | FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 || | |
10355 | strcmp(name, | |
10356 | FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) { | |
10357 | printf(gettext("\"%s\" (0x%llx)"), | |
10358 | zpool_state_to_name(i64, VDEV_AUX_NONE), | |
10359 | (u_longlong_t)i64); | |
10360 | } else { | |
10361 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
10362 | } | |
26685276 BB |
10363 | break; |
10364 | ||
10365 | case DATA_TYPE_HRTIME: | |
10366 | (void) nvpair_value_hrtime(nvp, (void *)&i64); | |
10367 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
10368 | break; | |
10369 | ||
10370 | case DATA_TYPE_STRING: | |
10371 | (void) nvpair_value_string(nvp, &str); | |
10372 | printf(gettext("\"%s\""), str ? str : "<NULL>"); | |
10373 | break; | |
10374 | ||
10375 | case DATA_TYPE_NVLIST: | |
10376 | printf(gettext("(embedded nvlist)\n")); | |
10377 | (void) nvpair_value_nvlist(nvp, &cnv); | |
10378 | zpool_do_events_nvprint(cnv, depth + 8); | |
c5343ba7 | 10379 | printf(gettext("%*s(end %s)"), depth, "", name); |
26685276 BB |
10380 | break; |
10381 | ||
10382 | case DATA_TYPE_NVLIST_ARRAY: { | |
10383 | nvlist_t **val; | |
10384 | uint_t i, nelem; | |
10385 | ||
10386 | (void) nvpair_value_nvlist_array(nvp, &val, &nelem); | |
10387 | printf(gettext("(%d embedded nvlists)\n"), nelem); | |
10388 | for (i = 0; i < nelem; i++) { | |
10389 | printf(gettext("%*s%s[%d] = %s\n"), | |
d1d7e268 | 10390 | depth, "", name, i, "(embedded nvlist)"); |
26685276 BB |
10391 | zpool_do_events_nvprint(val[i], depth + 8); |
10392 | printf(gettext("%*s(end %s[%i])\n"), | |
d1d7e268 | 10393 | depth, "", name, i); |
26685276 BB |
10394 | } |
10395 | printf(gettext("%*s(end %s)\n"), depth, "", name); | |
10396 | } | |
10397 | break; | |
10398 | ||
10399 | case DATA_TYPE_INT8_ARRAY: { | |
10400 | int8_t *val; | |
10401 | uint_t i, nelem; | |
10402 | ||
10403 | (void) nvpair_value_int8_array(nvp, &val, &nelem); | |
10404 | for (i = 0; i < nelem; i++) | |
10405 | printf(gettext("0x%x "), val[i]); | |
10406 | ||
10407 | break; | |
10408 | } | |
10409 | ||
10410 | case DATA_TYPE_UINT8_ARRAY: { | |
10411 | uint8_t *val; | |
10412 | uint_t i, nelem; | |
10413 | ||
10414 | (void) nvpair_value_uint8_array(nvp, &val, &nelem); | |
10415 | for (i = 0; i < nelem; i++) | |
10416 | printf(gettext("0x%x "), val[i]); | |
10417 | ||
10418 | break; | |
10419 | } | |
10420 | ||
10421 | case DATA_TYPE_INT16_ARRAY: { | |
10422 | int16_t *val; | |
10423 | uint_t i, nelem; | |
10424 | ||
10425 | (void) nvpair_value_int16_array(nvp, &val, &nelem); | |
10426 | for (i = 0; i < nelem; i++) | |
10427 | printf(gettext("0x%x "), val[i]); | |
10428 | ||
10429 | break; | |
10430 | } | |
10431 | ||
10432 | case DATA_TYPE_UINT16_ARRAY: { | |
10433 | uint16_t *val; | |
10434 | uint_t i, nelem; | |
10435 | ||
10436 | (void) nvpair_value_uint16_array(nvp, &val, &nelem); | |
10437 | for (i = 0; i < nelem; i++) | |
10438 | printf(gettext("0x%x "), val[i]); | |
10439 | ||
10440 | break; | |
10441 | } | |
10442 | ||
10443 | case DATA_TYPE_INT32_ARRAY: { | |
10444 | int32_t *val; | |
10445 | uint_t i, nelem; | |
10446 | ||
10447 | (void) nvpair_value_int32_array(nvp, &val, &nelem); | |
10448 | for (i = 0; i < nelem; i++) | |
10449 | printf(gettext("0x%x "), val[i]); | |
10450 | ||
10451 | break; | |
10452 | } | |
10453 | ||
10454 | case DATA_TYPE_UINT32_ARRAY: { | |
10455 | uint32_t *val; | |
10456 | uint_t i, nelem; | |
10457 | ||
10458 | (void) nvpair_value_uint32_array(nvp, &val, &nelem); | |
10459 | for (i = 0; i < nelem; i++) | |
10460 | printf(gettext("0x%x "), val[i]); | |
10461 | ||
10462 | break; | |
10463 | } | |
10464 | ||
10465 | case DATA_TYPE_INT64_ARRAY: { | |
10466 | int64_t *val; | |
10467 | uint_t i, nelem; | |
10468 | ||
10469 | (void) nvpair_value_int64_array(nvp, &val, &nelem); | |
10470 | for (i = 0; i < nelem; i++) | |
d1d7e268 MK |
10471 | printf(gettext("0x%llx "), |
10472 | (u_longlong_t)val[i]); | |
26685276 BB |
10473 | |
10474 | break; | |
10475 | } | |
10476 | ||
10477 | case DATA_TYPE_UINT64_ARRAY: { | |
10478 | uint64_t *val; | |
10479 | uint_t i, nelem; | |
10480 | ||
10481 | (void) nvpair_value_uint64_array(nvp, &val, &nelem); | |
10482 | for (i = 0; i < nelem; i++) | |
d1d7e268 MK |
10483 | printf(gettext("0x%llx "), |
10484 | (u_longlong_t)val[i]); | |
26685276 BB |
10485 | |
10486 | break; | |
10487 | } | |
10488 | ||
d21705ea | 10489 | case DATA_TYPE_STRING_ARRAY: { |
d1807f16 | 10490 | const char **str; |
d21705ea BB |
10491 | uint_t i, nelem; |
10492 | ||
10493 | (void) nvpair_value_string_array(nvp, &str, &nelem); | |
10494 | for (i = 0; i < nelem; i++) | |
10495 | printf(gettext("\"%s\" "), | |
10496 | str[i] ? str[i] : "<NULL>"); | |
10497 | ||
10498 | break; | |
10499 | } | |
10500 | ||
26685276 BB |
10501 | case DATA_TYPE_BOOLEAN_ARRAY: |
10502 | case DATA_TYPE_BYTE_ARRAY: | |
10503 | case DATA_TYPE_DOUBLE: | |
6b64382b | 10504 | case DATA_TYPE_DONTCARE: |
26685276 BB |
10505 | case DATA_TYPE_UNKNOWN: |
10506 | printf(gettext("<unknown>")); | |
10507 | break; | |
10508 | } | |
10509 | ||
10510 | printf(gettext("\n")); | |
10511 | } | |
10512 | } | |
10513 | ||
10514 | static int | |
10515 | zpool_do_events_next(ev_opts_t *opts) | |
10516 | { | |
10517 | nvlist_t *nvl; | |
9b101a73 | 10518 | int zevent_fd, ret, dropped; |
d1807f16 | 10519 | const char *pool; |
26685276 | 10520 | |
9b101a73 BB |
10521 | zevent_fd = open(ZFS_DEV, O_RDWR); |
10522 | VERIFY(zevent_fd >= 0); | |
26685276 | 10523 | |
c5343ba7 BB |
10524 | if (!opts->scripted) |
10525 | (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS"); | |
26685276 BB |
10526 | |
10527 | while (1) { | |
10528 | ret = zpool_events_next(g_zfs, &nvl, &dropped, | |
8c7aa0cf | 10529 | (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd); |
26685276 BB |
10530 | if (ret || nvl == NULL) |
10531 | break; | |
10532 | ||
10533 | if (dropped > 0) | |
10534 | (void) printf(gettext("dropped %d events\n"), dropped); | |
10535 | ||
88f9c939 | 10536 | if (strlen(opts->poolname) > 0 && |
10537 | nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 && | |
10538 | strcmp(opts->poolname, pool) != 0) | |
10539 | continue; | |
10540 | ||
1e1c3980 | 10541 | zpool_do_events_short(nvl, opts); |
26685276 BB |
10542 | |
10543 | if (opts->verbose) { | |
10544 | zpool_do_events_nvprint(nvl, 8); | |
10545 | printf(gettext("\n")); | |
10546 | } | |
50fe577d | 10547 | (void) fflush(stdout); |
26685276 BB |
10548 | |
10549 | nvlist_free(nvl); | |
10550 | } | |
10551 | ||
9b101a73 | 10552 | VERIFY(0 == close(zevent_fd)); |
26685276 BB |
10553 | |
10554 | return (ret); | |
10555 | } | |
10556 | ||
10557 | static int | |
e40ca391 | 10558 | zpool_do_events_clear(void) |
26685276 BB |
10559 | { |
10560 | int count, ret; | |
10561 | ||
10562 | ret = zpool_events_clear(g_zfs, &count); | |
10563 | if (!ret) | |
10564 | (void) printf(gettext("cleared %d events\n"), count); | |
10565 | ||
10566 | return (ret); | |
10567 | } | |
10568 | ||
10569 | /* | |
88f9c939 | 10570 | * zpool events [-vHf [pool] | -c] |
26685276 BB |
10571 | * |
10572 | * Displays events logs by ZFS. | |
10573 | */ | |
10574 | int | |
10575 | zpool_do_events(int argc, char **argv) | |
10576 | { | |
10577 | ev_opts_t opts = { 0 }; | |
10578 | int ret; | |
10579 | int c; | |
10580 | ||
10581 | /* check options */ | |
c5343ba7 | 10582 | while ((c = getopt(argc, argv, "vHfc")) != -1) { |
26685276 BB |
10583 | switch (c) { |
10584 | case 'v': | |
10585 | opts.verbose = 1; | |
c5343ba7 BB |
10586 | break; |
10587 | case 'H': | |
10588 | opts.scripted = 1; | |
26685276 BB |
10589 | break; |
10590 | case 'f': | |
10591 | opts.follow = 1; | |
10592 | break; | |
10593 | case 'c': | |
10594 | opts.clear = 1; | |
10595 | break; | |
10596 | case '?': | |
10597 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
10598 | optopt); | |
10599 | usage(B_FALSE); | |
10600 | } | |
10601 | } | |
10602 | argc -= optind; | |
10603 | argv += optind; | |
10604 | ||
88f9c939 | 10605 | if (argc > 1) { |
10606 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
10607 | usage(B_FALSE); | |
10608 | } else if (argc == 1) { | |
10609 | (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname)); | |
10610 | if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) { | |
10611 | (void) fprintf(stderr, | |
10612 | gettext("invalid pool name '%s'\n"), opts.poolname); | |
10613 | usage(B_FALSE); | |
10614 | } | |
10615 | } | |
10616 | ||
10617 | if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) && | |
10618 | opts.clear) { | |
10619 | (void) fprintf(stderr, | |
10620 | gettext("invalid options combined with -c\n")); | |
10621 | usage(B_FALSE); | |
10622 | } | |
10623 | ||
26685276 | 10624 | if (opts.clear) |
e40ca391 | 10625 | ret = zpool_do_events_clear(); |
26685276 BB |
10626 | else |
10627 | ret = zpool_do_events_next(&opts); | |
10628 | ||
d1d7e268 | 10629 | return (ret); |
26685276 BB |
10630 | } |
10631 | ||
34dc7c2f | 10632 | static int |
2a673e76 | 10633 | get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data) |
34dc7c2f BB |
10634 | { |
10635 | zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; | |
2a673e76 | 10636 | char value[ZFS_MAXPROPLEN]; |
34dc7c2f | 10637 | zprop_source_t srctype; |
34dc7c2f | 10638 | |
2a673e76 AJ |
10639 | for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL; |
10640 | pl = pl->pl_next) { | |
10641 | char *prop_name; | |
34dc7c2f | 10642 | /* |
2a673e76 AJ |
10643 | * If the first property is pool name, it is a special |
10644 | * placeholder that we can skip. This will also skip | |
34dc7c2f BB |
10645 | * over the name property when 'all' is specified. |
10646 | */ | |
10647 | if (pl->pl_prop == ZPOOL_PROP_NAME && | |
10648 | pl == cbp->cb_proplist) | |
10649 | continue; | |
10650 | ||
2a673e76 AJ |
10651 | if (pl->pl_prop == ZPROP_INVAL) { |
10652 | prop_name = pl->pl_user_prop; | |
10653 | } else { | |
10654 | prop_name = (char *)vdev_prop_to_name(pl->pl_prop); | |
10655 | } | |
10656 | if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop, | |
10657 | prop_name, value, sizeof (value), &srctype, | |
10658 | cbp->cb_literal) == 0) { | |
10659 | zprop_print_one_property(vdevname, cbp, prop_name, | |
10660 | value, srctype, NULL, NULL); | |
10661 | } | |
10662 | } | |
34dc7c2f | 10663 | |
2a673e76 AJ |
10664 | return (0); |
10665 | } | |
10666 | ||
10667 | static int | |
3e4ed421 | 10668 | get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data) |
2a673e76 AJ |
10669 | { |
10670 | zpool_handle_t *zhp = zhp_data; | |
10671 | zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; | |
3e4ed421 RW |
10672 | char *vdevname; |
10673 | const char *type; | |
2a673e76 AJ |
10674 | int ret; |
10675 | ||
3e4ed421 RW |
10676 | /* |
10677 | * zpool_vdev_name() transforms the root vdev name (i.e., root-0) to the | |
10678 | * pool name for display purposes, which is not desired. Fallback to | |
10679 | * zpool_vdev_name() when not dealing with the root vdev. | |
10680 | */ | |
10681 | type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE); | |
10682 | if (zhp != NULL && strcmp(type, "root") == 0) | |
10683 | vdevname = strdup("root-0"); | |
10684 | else | |
10685 | vdevname = zpool_vdev_name(g_zfs, zhp, nv, | |
10686 | cbp->cb_vdevs.cb_name_flags); | |
2a673e76 | 10687 | |
3e4ed421 | 10688 | (void) vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist); |
2a673e76 | 10689 | |
2a673e76 AJ |
10690 | ret = get_callback_vdev(zhp, vdevname, data); |
10691 | ||
3e4ed421 RW |
10692 | free(vdevname); |
10693 | ||
2a673e76 AJ |
10694 | return (ret); |
10695 | } | |
10696 | ||
10697 | static int | |
10698 | get_callback(zpool_handle_t *zhp, void *data) | |
10699 | { | |
10700 | zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; | |
8eae2d21 | 10701 | char value[ZFS_MAXPROPLEN]; |
2a673e76 AJ |
10702 | zprop_source_t srctype; |
10703 | zprop_list_t *pl; | |
10704 | int vid; | |
10705 | ||
10706 | if (cbp->cb_type == ZFS_TYPE_VDEV) { | |
10707 | if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) { | |
2a673e76 | 10708 | for_each_vdev(zhp, get_callback_vdev_cb, data); |
9ae529ec | 10709 | } else { |
2a673e76 AJ |
10710 | /* Adjust column widths for vdev properties */ |
10711 | for (vid = 0; vid < cbp->cb_vdevs.cb_names_count; | |
10712 | vid++) { | |
10713 | vdev_expand_proplist(zhp, | |
10714 | cbp->cb_vdevs.cb_names[vid], | |
10715 | &cbp->cb_proplist); | |
10716 | } | |
10717 | /* Display the properties */ | |
10718 | for (vid = 0; vid < cbp->cb_vdevs.cb_names_count; | |
10719 | vid++) { | |
10720 | get_callback_vdev(zhp, | |
10721 | cbp->cb_vdevs.cb_names[vid], data); | |
10722 | } | |
10723 | } | |
10724 | } else { | |
10725 | assert(cbp->cb_type == ZFS_TYPE_POOL); | |
10726 | for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { | |
10727 | /* | |
10728 | * Skip the special fake placeholder. This will also | |
10729 | * skip over the name property when 'all' is specified. | |
10730 | */ | |
10731 | if (pl->pl_prop == ZPOOL_PROP_NAME && | |
10732 | pl == cbp->cb_proplist) | |
9ae529ec CS |
10733 | continue; |
10734 | ||
2a673e76 | 10735 | if (pl->pl_prop == ZPROP_INVAL && |
8eae2d21 AJ |
10736 | zfs_prop_user(pl->pl_user_prop)) { |
10737 | srctype = ZPROP_SRC_LOCAL; | |
10738 | ||
10739 | if (zpool_get_userprop(zhp, pl->pl_user_prop, | |
10740 | value, sizeof (value), &srctype) != 0) | |
10741 | continue; | |
10742 | ||
10743 | zprop_print_one_property(zpool_get_name(zhp), | |
10744 | cbp, pl->pl_user_prop, value, srctype, | |
10745 | NULL, NULL); | |
10746 | } else if (pl->pl_prop == ZPROP_INVAL && | |
2a673e76 AJ |
10747 | (zpool_prop_feature(pl->pl_user_prop) || |
10748 | zpool_prop_unsupported(pl->pl_user_prop))) { | |
10749 | srctype = ZPROP_SRC_LOCAL; | |
10750 | ||
10751 | if (zpool_prop_get_feature(zhp, | |
10752 | pl->pl_user_prop, value, | |
10753 | sizeof (value)) == 0) { | |
10754 | zprop_print_one_property( | |
10755 | zpool_get_name(zhp), cbp, | |
10756 | pl->pl_user_prop, value, srctype, | |
10757 | NULL, NULL); | |
10758 | } | |
10759 | } else { | |
10760 | if (zpool_get_prop(zhp, pl->pl_prop, value, | |
10761 | sizeof (value), &srctype, | |
10762 | cbp->cb_literal) != 0) | |
10763 | continue; | |
10764 | ||
10765 | zprop_print_one_property(zpool_get_name(zhp), | |
10766 | cbp, zpool_prop_to_name(pl->pl_prop), | |
10767 | value, srctype, NULL, NULL); | |
10768 | } | |
9ae529ec | 10769 | } |
34dc7c2f | 10770 | } |
2a673e76 | 10771 | |
34dc7c2f BB |
10772 | return (0); |
10773 | } | |
10774 | ||
2a8b84b7 AS |
10775 | /* |
10776 | * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... | |
10777 | * | |
10778 | * -H Scripted mode. Don't display headers, and separate properties | |
10779 | * by a single tab. | |
10780 | * -o List of columns to display. Defaults to | |
10781 | * "name,property,value,source". | |
4e33ba4c | 10782 | * -p Display values in parsable (exact) format. |
2a8b84b7 AS |
10783 | * |
10784 | * Get properties of pools in the system. Output space statistics | |
10785 | * for each one as well as other attributes. | |
10786 | */ | |
34dc7c2f BB |
10787 | int |
10788 | zpool_do_get(int argc, char **argv) | |
10789 | { | |
10790 | zprop_get_cbdata_t cb = { 0 }; | |
10791 | zprop_list_t fake_name = { 0 }; | |
2a8b84b7 AS |
10792 | int ret; |
10793 | int c, i; | |
2a673e76 | 10794 | char *propstr = NULL; |
3e4ed421 | 10795 | char *vdev = NULL; |
2a8b84b7 AS |
10796 | |
10797 | cb.cb_first = B_TRUE; | |
10798 | ||
10799 | /* | |
10800 | * Set up default columns and sources. | |
10801 | */ | |
10802 | cb.cb_sources = ZPROP_SRC_ALL; | |
10803 | cb.cb_columns[0] = GET_COL_NAME; | |
10804 | cb.cb_columns[1] = GET_COL_PROPERTY; | |
10805 | cb.cb_columns[2] = GET_COL_VALUE; | |
10806 | cb.cb_columns[3] = GET_COL_SOURCE; | |
10807 | cb.cb_type = ZFS_TYPE_POOL; | |
2a673e76 AJ |
10808 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID; |
10809 | current_prop_type = cb.cb_type; | |
34dc7c2f | 10810 | |
d65e7381 | 10811 | /* check options */ |
2a8b84b7 | 10812 | while ((c = getopt(argc, argv, ":Hpo:")) != -1) { |
d65e7381 RE |
10813 | switch (c) { |
10814 | case 'p': | |
10815 | cb.cb_literal = B_TRUE; | |
10816 | break; | |
79eb71dc TF |
10817 | case 'H': |
10818 | cb.cb_scripted = B_TRUE; | |
10819 | break; | |
2a8b84b7 | 10820 | case 'o': |
861166b0 | 10821 | memset(&cb.cb_columns, 0, sizeof (cb.cb_columns)); |
2a8b84b7 | 10822 | i = 0; |
66cd170d AZ |
10823 | |
10824 | for (char *tok; (tok = strsep(&optarg, ",")); ) { | |
10825 | static const char *const col_opts[] = | |
2a8b84b7 | 10826 | { "name", "property", "value", "source", |
66cd170d AZ |
10827 | "all" }; |
10828 | static const zfs_get_column_t col_cols[] = | |
10829 | { GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE, | |
10830 | GET_COL_SOURCE }; | |
2a8b84b7 | 10831 | |
c7978784 | 10832 | if (i == ZFS_GET_NCOLS - 1) { |
2a8b84b7 AS |
10833 | (void) fprintf(stderr, gettext("too " |
10834 | "many fields given to -o " | |
10835 | "option\n")); | |
10836 | usage(B_FALSE); | |
10837 | } | |
79eb71dc | 10838 | |
66cd170d AZ |
10839 | for (c = 0; c < ARRAY_SIZE(col_opts); ++c) |
10840 | if (strcmp(tok, col_opts[c]) == 0) | |
10841 | goto found; | |
10842 | ||
10843 | (void) fprintf(stderr, | |
10844 | gettext("invalid column name '%s'\n"), tok); | |
10845 | usage(B_FALSE); | |
10846 | ||
10847 | found: | |
10848 | if (c >= 4) { | |
2a8b84b7 AS |
10849 | if (i > 0) { |
10850 | (void) fprintf(stderr, | |
10851 | gettext("\"all\" conflicts " | |
10852 | "with specific fields " | |
10853 | "given to -o option\n")); | |
10854 | usage(B_FALSE); | |
10855 | } | |
66cd170d AZ |
10856 | |
10857 | memcpy(cb.cb_columns, col_cols, | |
10858 | sizeof (col_cols)); | |
c7978784 | 10859 | i = ZFS_GET_NCOLS - 1; |
66cd170d AZ |
10860 | } else |
10861 | cb.cb_columns[i++] = col_cols[c]; | |
2a8b84b7 AS |
10862 | } |
10863 | break; | |
d65e7381 RE |
10864 | case '?': |
10865 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
10866 | optopt); | |
10867 | usage(B_FALSE); | |
10868 | } | |
10869 | } | |
10870 | ||
10871 | argc -= optind; | |
10872 | argv += optind; | |
10873 | ||
10874 | if (argc < 1) { | |
9ae529ec CS |
10875 | (void) fprintf(stderr, gettext("missing property " |
10876 | "argument\n")); | |
34dc7c2f | 10877 | usage(B_FALSE); |
9ae529ec | 10878 | } |
34dc7c2f | 10879 | |
2a673e76 AJ |
10880 | /* Properties list is needed later by zprop_get_list() */ |
10881 | propstr = argv[0]; | |
34dc7c2f | 10882 | |
d65e7381 RE |
10883 | argc--; |
10884 | argv++; | |
10885 | ||
2a673e76 AJ |
10886 | if (argc == 0) { |
10887 | /* No args, so just print the defaults. */ | |
10888 | } else if (are_all_pools(argc, argv)) { | |
10889 | /* All the args are pool names */ | |
10890 | } else if (are_all_pools(1, argv)) { | |
10891 | /* The first arg is a pool name */ | |
10892 | if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) || | |
3e4ed421 | 10893 | (argc == 2 && strcmp(argv[1], "root") == 0) || |
2a673e76 AJ |
10894 | are_vdevs_in_pool(argc - 1, argv + 1, argv[0], |
10895 | &cb.cb_vdevs)) { | |
3e4ed421 RW |
10896 | |
10897 | if (strcmp(argv[1], "root") == 0) | |
10898 | vdev = strdup("root-0"); | |
10899 | else | |
10900 | vdev = strdup(argv[1]); | |
10901 | ||
2a673e76 | 10902 | /* ... and the rest are vdev names */ |
3e4ed421 | 10903 | cb.cb_vdevs.cb_names = &vdev; |
2a673e76 AJ |
10904 | cb.cb_vdevs.cb_names_count = argc - 1; |
10905 | cb.cb_type = ZFS_TYPE_VDEV; | |
10906 | argc = 1; /* One pool to process */ | |
10907 | } else { | |
10908 | fprintf(stderr, gettext("Expected a list of vdevs in" | |
10909 | " \"%s\", but got:\n"), argv[0]); | |
10910 | error_list_unresolved_vdevs(argc - 1, argv + 1, | |
10911 | argv[0], &cb.cb_vdevs); | |
10912 | fprintf(stderr, "\n"); | |
10913 | usage(B_FALSE); | |
10914 | return (1); | |
10915 | } | |
10916 | } else { | |
10917 | /* | |
b6bbaa83 | 10918 | * The first arg isn't the name of a valid pool. |
2a673e76 | 10919 | */ |
b6bbaa83 PD |
10920 | fprintf(stderr, gettext("Cannot get properties of %s: " |
10921 | "no such pool available.\n"), argv[0]); | |
2a673e76 AJ |
10922 | return (1); |
10923 | } | |
10924 | ||
10925 | if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist, | |
10926 | cb.cb_type) != 0) { | |
10927 | /* Use correct list of valid properties (pool or vdev) */ | |
10928 | current_prop_type = cb.cb_type; | |
10929 | usage(B_FALSE); | |
10930 | } | |
10931 | ||
34dc7c2f BB |
10932 | if (cb.cb_proplist != NULL) { |
10933 | fake_name.pl_prop = ZPOOL_PROP_NAME; | |
10934 | fake_name.pl_width = strlen(gettext("NAME")); | |
10935 | fake_name.pl_next = cb.cb_proplist; | |
10936 | cb.cb_proplist = &fake_name; | |
10937 | } | |
10938 | ||
2a673e76 AJ |
10939 | ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type, |
10940 | cb.cb_literal, get_callback, &cb); | |
34dc7c2f BB |
10941 | |
10942 | if (cb.cb_proplist == &fake_name) | |
10943 | zprop_free_list(fake_name.pl_next); | |
10944 | else | |
10945 | zprop_free_list(cb.cb_proplist); | |
10946 | ||
3e4ed421 RW |
10947 | if (vdev != NULL) |
10948 | free(vdev); | |
10949 | ||
34dc7c2f BB |
10950 | return (ret); |
10951 | } | |
10952 | ||
10953 | typedef struct set_cbdata { | |
10954 | char *cb_propname; | |
10955 | char *cb_value; | |
2a673e76 AJ |
10956 | zfs_type_t cb_type; |
10957 | vdev_cbdata_t cb_vdevs; | |
34dc7c2f BB |
10958 | boolean_t cb_any_successful; |
10959 | } set_cbdata_t; | |
10960 | ||
65c7cc49 | 10961 | static int |
2a673e76 | 10962 | set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb) |
34dc7c2f BB |
10963 | { |
10964 | int error; | |
34dc7c2f | 10965 | |
e086db16 CB |
10966 | /* Check if we have out-of-bounds features */ |
10967 | if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) { | |
10968 | boolean_t features[SPA_FEATURES]; | |
10969 | if (zpool_do_load_compat(cb->cb_value, features) != | |
10970 | ZPOOL_COMPATIBILITY_OK) | |
10971 | return (-1); | |
10972 | ||
10973 | nvlist_t *enabled = zpool_get_features(zhp); | |
10974 | spa_feature_t i; | |
10975 | for (i = 0; i < SPA_FEATURES; i++) { | |
10976 | const char *fguid = spa_feature_table[i].fi_guid; | |
10977 | if (nvlist_exists(enabled, fguid) && !features[i]) | |
10978 | break; | |
10979 | } | |
10980 | if (i < SPA_FEATURES) | |
10981 | (void) fprintf(stderr, gettext("Warning: one or " | |
10982 | "more features already enabled on pool '%s'\n" | |
10983 | "are not present in this compatibility set.\n"), | |
10984 | zpool_get_name(zhp)); | |
10985 | } | |
10986 | ||
10987 | /* if we're setting a feature, check it's in compatibility set */ | |
10988 | if (zpool_prop_feature(cb->cb_propname) && | |
10989 | strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) { | |
10990 | char *fname = strchr(cb->cb_propname, '@') + 1; | |
10991 | spa_feature_t f; | |
10992 | ||
10993 | if (zfeature_lookup_name(fname, &f) == 0) { | |
10994 | char compat[ZFS_MAXPROPLEN]; | |
10995 | if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, | |
10996 | compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) | |
10997 | compat[0] = '\0'; | |
10998 | ||
10999 | boolean_t features[SPA_FEATURES]; | |
11000 | if (zpool_do_load_compat(compat, features) != | |
11001 | ZPOOL_COMPATIBILITY_OK) { | |
11002 | (void) fprintf(stderr, gettext("Error: " | |
11003 | "cannot enable feature '%s' on pool '%s'\n" | |
11004 | "because the pool's 'compatibility' " | |
11005 | "property cannot be parsed.\n"), | |
11006 | fname, zpool_get_name(zhp)); | |
11007 | return (-1); | |
11008 | } | |
11009 | ||
11010 | if (!features[f]) { | |
11011 | (void) fprintf(stderr, gettext("Error: " | |
11012 | "cannot enable feature '%s' on pool '%s'\n" | |
11013 | "as it is not specified in this pool's " | |
11014 | "current compatibility set.\n" | |
11015 | "Consider setting 'compatibility' to a " | |
11016 | "less restrictive set, or to 'off'.\n"), | |
11017 | fname, zpool_get_name(zhp)); | |
11018 | return (-1); | |
11019 | } | |
11020 | } | |
11021 | } | |
11022 | ||
34dc7c2f BB |
11023 | error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); |
11024 | ||
2a673e76 AJ |
11025 | return (error); |
11026 | } | |
11027 | ||
11028 | static int | |
11029 | set_callback(zpool_handle_t *zhp, void *data) | |
11030 | { | |
11031 | int error; | |
11032 | set_cbdata_t *cb = (set_cbdata_t *)data; | |
11033 | ||
11034 | if (cb->cb_type == ZFS_TYPE_VDEV) { | |
11035 | error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names, | |
11036 | cb->cb_propname, cb->cb_value); | |
11037 | } else { | |
11038 | assert(cb->cb_type == ZFS_TYPE_POOL); | |
11039 | error = set_pool_callback(zhp, cb); | |
11040 | } | |
34dc7c2f | 11041 | |
2a673e76 | 11042 | cb->cb_any_successful = !error; |
34dc7c2f BB |
11043 | return (error); |
11044 | } | |
11045 | ||
11046 | int | |
11047 | zpool_do_set(int argc, char **argv) | |
11048 | { | |
11049 | set_cbdata_t cb = { 0 }; | |
11050 | int error; | |
3e4ed421 | 11051 | char *vdev = NULL; |
34dc7c2f | 11052 | |
2a673e76 | 11053 | current_prop_type = ZFS_TYPE_POOL; |
34dc7c2f BB |
11054 | if (argc > 1 && argv[1][0] == '-') { |
11055 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
11056 | argv[1][1]); | |
11057 | usage(B_FALSE); | |
11058 | } | |
11059 | ||
11060 | if (argc < 2) { | |
11061 | (void) fprintf(stderr, gettext("missing property=value " | |
11062 | "argument\n")); | |
11063 | usage(B_FALSE); | |
11064 | } | |
11065 | ||
11066 | if (argc < 3) { | |
11067 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
11068 | usage(B_FALSE); | |
11069 | } | |
11070 | ||
2a673e76 | 11071 | if (argc > 4) { |
34dc7c2f BB |
11072 | (void) fprintf(stderr, gettext("too many pool names\n")); |
11073 | usage(B_FALSE); | |
11074 | } | |
11075 | ||
11076 | cb.cb_propname = argv[1]; | |
2a673e76 AJ |
11077 | cb.cb_type = ZFS_TYPE_POOL; |
11078 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID; | |
34dc7c2f BB |
11079 | cb.cb_value = strchr(cb.cb_propname, '='); |
11080 | if (cb.cb_value == NULL) { | |
11081 | (void) fprintf(stderr, gettext("missing value in " | |
11082 | "property=value argument\n")); | |
11083 | usage(B_FALSE); | |
11084 | } | |
11085 | ||
11086 | *(cb.cb_value) = '\0'; | |
11087 | cb.cb_value++; | |
2a673e76 AJ |
11088 | argc -= 2; |
11089 | argv += 2; | |
11090 | ||
7a85f58d RW |
11091 | /* argv[0] is pool name */ |
11092 | if (!is_pool(argv[0])) { | |
11093 | (void) fprintf(stderr, | |
11094 | gettext("cannot open '%s': is not a pool\n"), argv[0]); | |
11095 | return (EINVAL); | |
11096 | } | |
11097 | ||
11098 | /* argv[1], when supplied, is vdev name */ | |
11099 | if (argc == 2) { | |
3e4ed421 RW |
11100 | |
11101 | if (strcmp(argv[1], "root") == 0) | |
11102 | vdev = strdup("root-0"); | |
11103 | else | |
11104 | vdev = strdup(argv[1]); | |
11105 | ||
11106 | if (!are_vdevs_in_pool(1, &vdev, argv[0], &cb.cb_vdevs)) { | |
7a85f58d RW |
11107 | (void) fprintf(stderr, gettext( |
11108 | "cannot find '%s' in '%s': device not in pool\n"), | |
3e4ed421 RW |
11109 | vdev, argv[0]); |
11110 | free(vdev); | |
7a85f58d RW |
11111 | return (EINVAL); |
11112 | } | |
3e4ed421 | 11113 | cb.cb_vdevs.cb_names = &vdev; |
2a673e76 AJ |
11114 | cb.cb_vdevs.cb_names_count = 1; |
11115 | cb.cb_type = ZFS_TYPE_VDEV; | |
2a673e76 | 11116 | } |
34dc7c2f | 11117 | |
7a85f58d | 11118 | error = for_each_pool(1, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
2a673e76 | 11119 | B_FALSE, set_callback, &cb); |
34dc7c2f | 11120 | |
3e4ed421 RW |
11121 | if (vdev != NULL) |
11122 | free(vdev); | |
11123 | ||
34dc7c2f BB |
11124 | return (error); |
11125 | } | |
2288d419 BB |
11126 | |
11127 | /* Add up the total number of bytes left to initialize/trim across all vdevs */ | |
e60e158e | 11128 | static uint64_t |
2288d419 | 11129 | vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity) |
e60e158e JG |
11130 | { |
11131 | uint64_t bytes_remaining; | |
11132 | nvlist_t **child; | |
11133 | uint_t c, children; | |
11134 | vdev_stat_t *vs; | |
11135 | ||
2288d419 BB |
11136 | assert(activity == ZPOOL_WAIT_INITIALIZE || |
11137 | activity == ZPOOL_WAIT_TRIM); | |
11138 | ||
e60e158e JG |
11139 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
11140 | (uint64_t **)&vs, &c) == 0); | |
11141 | ||
2288d419 BB |
11142 | if (activity == ZPOOL_WAIT_INITIALIZE && |
11143 | vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) | |
e60e158e JG |
11144 | bytes_remaining = vs->vs_initialize_bytes_est - |
11145 | vs->vs_initialize_bytes_done; | |
2288d419 BB |
11146 | else if (activity == ZPOOL_WAIT_TRIM && |
11147 | vs->vs_trim_state == VDEV_TRIM_ACTIVE) | |
11148 | bytes_remaining = vs->vs_trim_bytes_est - | |
11149 | vs->vs_trim_bytes_done; | |
e60e158e JG |
11150 | else |
11151 | bytes_remaining = 0; | |
11152 | ||
11153 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
11154 | &child, &children) != 0) | |
11155 | children = 0; | |
11156 | ||
11157 | for (c = 0; c < children; c++) | |
2288d419 | 11158 | bytes_remaining += vdev_activity_remaining(child[c], activity); |
e60e158e JG |
11159 | |
11160 | return (bytes_remaining); | |
11161 | } | |
11162 | ||
9a49d3f3 BB |
11163 | /* Add up the total number of bytes left to rebuild across top-level vdevs */ |
11164 | static uint64_t | |
11165 | vdev_activity_top_remaining(nvlist_t *nv) | |
11166 | { | |
11167 | uint64_t bytes_remaining = 0; | |
11168 | nvlist_t **child; | |
11169 | uint_t children; | |
11170 | int error; | |
11171 | ||
11172 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
11173 | &child, &children) != 0) | |
11174 | children = 0; | |
11175 | ||
11176 | for (uint_t c = 0; c < children; c++) { | |
11177 | vdev_rebuild_stat_t *vrs; | |
11178 | uint_t i; | |
11179 | ||
11180 | error = nvlist_lookup_uint64_array(child[c], | |
11181 | ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i); | |
11182 | if (error == 0) { | |
11183 | if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { | |
11184 | bytes_remaining += (vrs->vrs_bytes_est - | |
11185 | vrs->vrs_bytes_rebuilt); | |
11186 | } | |
11187 | } | |
11188 | } | |
11189 | ||
11190 | return (bytes_remaining); | |
11191 | } | |
11192 | ||
e60e158e JG |
11193 | /* Whether any vdevs are 'spare' or 'replacing' vdevs */ |
11194 | static boolean_t | |
11195 | vdev_any_spare_replacing(nvlist_t *nv) | |
11196 | { | |
11197 | nvlist_t **child; | |
11198 | uint_t c, children; | |
d1807f16 | 11199 | const char *vdev_type; |
e60e158e JG |
11200 | |
11201 | (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type); | |
11202 | ||
11203 | if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 || | |
b2255edc BB |
11204 | strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 || |
11205 | strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) { | |
e60e158e JG |
11206 | return (B_TRUE); |
11207 | } | |
11208 | ||
11209 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
11210 | &child, &children) != 0) | |
11211 | children = 0; | |
11212 | ||
11213 | for (c = 0; c < children; c++) { | |
11214 | if (vdev_any_spare_replacing(child[c])) | |
11215 | return (B_TRUE); | |
11216 | } | |
11217 | ||
11218 | return (B_FALSE); | |
11219 | } | |
11220 | ||
11221 | typedef struct wait_data { | |
11222 | char *wd_poolname; | |
11223 | boolean_t wd_scripted; | |
11224 | boolean_t wd_exact; | |
11225 | boolean_t wd_headers_once; | |
d553fb9b | 11226 | boolean_t wd_should_exit; |
e60e158e JG |
11227 | /* Which activities to wait for */ |
11228 | boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES]; | |
11229 | float wd_interval; | |
d553fb9b JL |
11230 | pthread_cond_t wd_cv; |
11231 | pthread_mutex_t wd_mutex; | |
e60e158e JG |
11232 | } wait_data_t; |
11233 | ||
11234 | /* | |
11235 | * Print to stdout a single line, containing one column for each activity that | |
11236 | * we are waiting for specifying how many bytes of work are left for that | |
11237 | * activity. | |
11238 | */ | |
11239 | static void | |
11240 | print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row) | |
11241 | { | |
11242 | nvlist_t *config, *nvroot; | |
11243 | uint_t c; | |
11244 | int i; | |
11245 | pool_checkpoint_stat_t *pcs = NULL; | |
11246 | pool_scan_stat_t *pss = NULL; | |
11247 | pool_removal_stat_t *prs = NULL; | |
5caeef02 | 11248 | pool_raidz_expand_stat_t *pres = NULL; |
a926aab9 | 11249 | const char *const headers[] = {"DISCARD", "FREE", "INITIALIZE", |
5caeef02 | 11250 | "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM", "RAIDZ_EXPAND"}; |
e60e158e JG |
11251 | int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES]; |
11252 | ||
11253 | /* Calculate the width of each column */ | |
11254 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { | |
11255 | /* | |
11256 | * Make sure we have enough space in the col for pretty-printed | |
11257 | * numbers and for the column header, and then leave a couple | |
11258 | * spaces between cols for readability. | |
11259 | */ | |
11260 | col_widths[i] = MAX(strlen(headers[i]), 6) + 2; | |
11261 | } | |
11262 | ||
884a48d9 RN |
11263 | if (timestamp_fmt != NODATE) |
11264 | print_timestamp(timestamp_fmt); | |
11265 | ||
e60e158e JG |
11266 | /* Print header if appropriate */ |
11267 | int term_height = terminal_height(); | |
11268 | boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 && | |
11269 | row % (term_height-1) == 0); | |
11270 | if (!wd->wd_scripted && (row == 0 || reprint_header)) { | |
11271 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { | |
11272 | if (wd->wd_enabled[i]) | |
11273 | (void) printf("%*s", col_widths[i], headers[i]); | |
11274 | } | |
a926aab9 | 11275 | (void) fputc('\n', stdout); |
e60e158e JG |
11276 | } |
11277 | ||
11278 | /* Bytes of work remaining in each activity */ | |
11279 | int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0}; | |
11280 | ||
11281 | bytes_rem[ZPOOL_WAIT_FREE] = | |
11282 | zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL); | |
11283 | ||
11284 | config = zpool_get_config(zhp, NULL); | |
11285 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); | |
11286 | ||
11287 | (void) nvlist_lookup_uint64_array(nvroot, | |
11288 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
11289 | if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING) | |
11290 | bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space; | |
11291 | ||
11292 | (void) nvlist_lookup_uint64_array(nvroot, | |
11293 | ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); | |
11294 | if (prs != NULL && prs->prs_state == DSS_SCANNING) | |
11295 | bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy - | |
11296 | prs->prs_copied; | |
11297 | ||
11298 | (void) nvlist_lookup_uint64_array(nvroot, | |
11299 | ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c); | |
11300 | if (pss != NULL && pss->pss_state == DSS_SCANNING && | |
11301 | pss->pss_pass_scrub_pause == 0) { | |
11302 | int64_t rem = pss->pss_to_examine - pss->pss_issued; | |
11303 | if (pss->pss_func == POOL_SCAN_SCRUB) | |
11304 | bytes_rem[ZPOOL_WAIT_SCRUB] = rem; | |
11305 | else | |
11306 | bytes_rem[ZPOOL_WAIT_RESILVER] = rem; | |
9a49d3f3 BB |
11307 | } else if (check_rebuilding(nvroot, NULL)) { |
11308 | bytes_rem[ZPOOL_WAIT_RESILVER] = | |
11309 | vdev_activity_top_remaining(nvroot); | |
e60e158e JG |
11310 | } |
11311 | ||
5caeef02 DB |
11312 | (void) nvlist_lookup_uint64_array(nvroot, |
11313 | ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c); | |
11314 | if (pres != NULL && pres->pres_state == DSS_SCANNING) { | |
11315 | int64_t rem = pres->pres_to_reflow - pres->pres_reflowed; | |
11316 | bytes_rem[ZPOOL_WAIT_RAIDZ_EXPAND] = rem; | |
11317 | } | |
11318 | ||
2288d419 BB |
11319 | bytes_rem[ZPOOL_WAIT_INITIALIZE] = |
11320 | vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE); | |
11321 | bytes_rem[ZPOOL_WAIT_TRIM] = | |
11322 | vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM); | |
e60e158e JG |
11323 | |
11324 | /* | |
11325 | * A replace finishes after resilvering finishes, so the amount of work | |
11326 | * left for a replace is the same as for resilvering. | |
11327 | * | |
11328 | * It isn't quite correct to say that if we have any 'spare' or | |
11329 | * 'replacing' vdevs and a resilver is happening, then a replace is in | |
11330 | * progress, like we do here. When a hot spare is used, the faulted vdev | |
11331 | * is not removed after the hot spare is resilvered, so parent 'spare' | |
11332 | * vdev is not removed either. So we could have a 'spare' vdev, but be | |
11333 | * resilvering for a different reason. However, we use it as a heuristic | |
11334 | * because we don't have access to the DTLs, which could tell us whether | |
11335 | * or not we have really finished resilvering a hot spare. | |
11336 | */ | |
11337 | if (vdev_any_spare_replacing(nvroot)) | |
11338 | bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER]; | |
11339 | ||
e60e158e JG |
11340 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { |
11341 | char buf[64]; | |
11342 | if (!wd->wd_enabled[i]) | |
11343 | continue; | |
11344 | ||
5caeef02 | 11345 | if (wd->wd_exact) { |
e60e158e JG |
11346 | (void) snprintf(buf, sizeof (buf), "%" PRIi64, |
11347 | bytes_rem[i]); | |
5caeef02 | 11348 | } else { |
e60e158e | 11349 | zfs_nicenum(bytes_rem[i], buf, sizeof (buf)); |
5caeef02 | 11350 | } |
e60e158e JG |
11351 | |
11352 | if (wd->wd_scripted) | |
11353 | (void) printf(i == 0 ? "%s" : "\t%s", buf); | |
11354 | else | |
11355 | (void) printf(" %*s", col_widths[i] - 1, buf); | |
11356 | } | |
11357 | (void) printf("\n"); | |
11358 | (void) fflush(stdout); | |
11359 | } | |
11360 | ||
65c7cc49 | 11361 | static void * |
e60e158e JG |
11362 | wait_status_thread(void *arg) |
11363 | { | |
11364 | wait_data_t *wd = (wait_data_t *)arg; | |
11365 | zpool_handle_t *zhp; | |
11366 | ||
11367 | if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL) | |
11368 | return (void *)(1); | |
11369 | ||
11370 | for (int row = 0; ; row++) { | |
11371 | boolean_t missing; | |
11372 | struct timespec timeout; | |
d553fb9b | 11373 | int ret = 0; |
e60e158e JG |
11374 | (void) clock_gettime(CLOCK_REALTIME, &timeout); |
11375 | ||
11376 | if (zpool_refresh_stats(zhp, &missing) != 0 || missing || | |
11377 | zpool_props_refresh(zhp) != 0) { | |
11378 | zpool_close(zhp); | |
11379 | return (void *)(uintptr_t)(missing ? 0 : 1); | |
11380 | } | |
11381 | ||
11382 | print_wait_status_row(wd, zhp, row); | |
11383 | ||
11384 | timeout.tv_sec += floor(wd->wd_interval); | |
11385 | long nanos = timeout.tv_nsec + | |
11386 | (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC; | |
11387 | if (nanos >= NANOSEC) { | |
11388 | timeout.tv_sec++; | |
11389 | timeout.tv_nsec = nanos - NANOSEC; | |
11390 | } else { | |
11391 | timeout.tv_nsec = nanos; | |
11392 | } | |
d553fb9b JL |
11393 | pthread_mutex_lock(&wd->wd_mutex); |
11394 | if (!wd->wd_should_exit) | |
11395 | ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex, | |
11396 | &timeout); | |
11397 | pthread_mutex_unlock(&wd->wd_mutex); | |
11398 | if (ret == 0) { | |
e60e158e | 11399 | break; /* signaled by main thread */ |
d553fb9b JL |
11400 | } else if (ret != ETIMEDOUT) { |
11401 | (void) fprintf(stderr, gettext("pthread_cond_timedwait " | |
11402 | "failed: %s\n"), strerror(ret)); | |
e60e158e JG |
11403 | zpool_close(zhp); |
11404 | return (void *)(uintptr_t)(1); | |
11405 | } | |
11406 | } | |
11407 | ||
11408 | zpool_close(zhp); | |
11409 | return (void *)(0); | |
11410 | } | |
11411 | ||
11412 | int | |
11413 | zpool_do_wait(int argc, char **argv) | |
11414 | { | |
11415 | boolean_t verbose = B_FALSE; | |
675508f6 | 11416 | int c, i; |
e60e158e JG |
11417 | unsigned long count; |
11418 | pthread_t status_thr; | |
11419 | int error = 0; | |
11420 | zpool_handle_t *zhp; | |
11421 | ||
11422 | wait_data_t wd; | |
11423 | wd.wd_scripted = B_FALSE; | |
11424 | wd.wd_exact = B_FALSE; | |
11425 | wd.wd_headers_once = B_FALSE; | |
d553fb9b | 11426 | wd.wd_should_exit = B_FALSE; |
e60e158e | 11427 | |
d553fb9b JL |
11428 | pthread_mutex_init(&wd.wd_mutex, NULL); |
11429 | pthread_cond_init(&wd.wd_cv, NULL); | |
e60e158e JG |
11430 | |
11431 | /* By default, wait for all types of activity. */ | |
11432 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) | |
11433 | wd.wd_enabled[i] = B_TRUE; | |
11434 | ||
11435 | while ((c = getopt(argc, argv, "HpT:t:")) != -1) { | |
11436 | switch (c) { | |
11437 | case 'H': | |
11438 | wd.wd_scripted = B_TRUE; | |
11439 | break; | |
11440 | case 'n': | |
11441 | wd.wd_headers_once = B_TRUE; | |
11442 | break; | |
11443 | case 'p': | |
11444 | wd.wd_exact = B_TRUE; | |
11445 | break; | |
11446 | case 'T': | |
11447 | get_timestamp_arg(*optarg); | |
11448 | break; | |
11449 | case 't': | |
e60e158e | 11450 | /* Reset activities array */ |
861166b0 | 11451 | memset(&wd.wd_enabled, 0, sizeof (wd.wd_enabled)); |
e60e158e | 11452 | |
675508f6 AZ |
11453 | for (char *tok; (tok = strsep(&optarg, ",")); ) { |
11454 | static const char *const col_opts[] = { | |
11455 | "discard", "free", "initialize", "replace", | |
5caeef02 DB |
11456 | "remove", "resilver", "scrub", "trim", |
11457 | "raidz_expand" }; | |
675508f6 AZ |
11458 | |
11459 | for (i = 0; i < ARRAY_SIZE(col_opts); ++i) | |
11460 | if (strcmp(tok, col_opts[i]) == 0) { | |
11461 | wd.wd_enabled[i] = B_TRUE; | |
11462 | goto found; | |
11463 | } | |
e60e158e | 11464 | |
675508f6 AZ |
11465 | (void) fprintf(stderr, |
11466 | gettext("invalid activity '%s'\n"), tok); | |
11467 | usage(B_FALSE); | |
11468 | found:; | |
e60e158e JG |
11469 | } |
11470 | break; | |
e60e158e JG |
11471 | case '?': |
11472 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
11473 | optopt); | |
11474 | usage(B_FALSE); | |
11475 | } | |
11476 | } | |
11477 | ||
11478 | argc -= optind; | |
11479 | argv += optind; | |
11480 | ||
11481 | get_interval_count(&argc, argv, &wd.wd_interval, &count); | |
11482 | if (count != 0) { | |
11483 | /* This subcmd only accepts an interval, not a count */ | |
11484 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
11485 | usage(B_FALSE); | |
11486 | } | |
11487 | ||
11488 | if (wd.wd_interval != 0) | |
11489 | verbose = B_TRUE; | |
11490 | ||
11491 | if (argc < 1) { | |
11492 | (void) fprintf(stderr, gettext("missing 'pool' argument\n")); | |
11493 | usage(B_FALSE); | |
11494 | } | |
11495 | if (argc > 1) { | |
11496 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
11497 | usage(B_FALSE); | |
11498 | } | |
11499 | ||
11500 | wd.wd_poolname = argv[0]; | |
11501 | ||
11502 | if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL) | |
11503 | return (1); | |
11504 | ||
11505 | if (verbose) { | |
11506 | /* | |
11507 | * We use a separate thread for printing status updates because | |
11508 | * the main thread will call lzc_wait(), which blocks as long | |
11509 | * as an activity is in progress, which can be a long time. | |
11510 | */ | |
11511 | if (pthread_create(&status_thr, NULL, wait_status_thread, &wd) | |
11512 | != 0) { | |
11513 | (void) fprintf(stderr, gettext("failed to create status" | |
11514 | "thread: %s\n"), strerror(errno)); | |
11515 | zpool_close(zhp); | |
11516 | return (1); | |
11517 | } | |
11518 | } | |
11519 | ||
11520 | /* | |
11521 | * Loop over all activities that we are supposed to wait for until none | |
11522 | * of them are in progress. Note that this means we can end up waiting | |
11523 | * for more activities to complete than just those that were in progress | |
11524 | * when we began waiting; if an activity we are interested in begins | |
11525 | * while we are waiting for another activity, we will wait for both to | |
11526 | * complete before exiting. | |
11527 | */ | |
11528 | for (;;) { | |
11529 | boolean_t missing = B_FALSE; | |
11530 | boolean_t any_waited = B_FALSE; | |
11531 | ||
11532 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { | |
11533 | boolean_t waited; | |
11534 | ||
11535 | if (!wd.wd_enabled[i]) | |
11536 | continue; | |
11537 | ||
11538 | error = zpool_wait_status(zhp, i, &missing, &waited); | |
11539 | if (error != 0 || missing) | |
11540 | break; | |
11541 | ||
11542 | any_waited = (any_waited || waited); | |
11543 | } | |
11544 | ||
11545 | if (error != 0 || missing || !any_waited) | |
11546 | break; | |
11547 | } | |
11548 | ||
11549 | zpool_close(zhp); | |
11550 | ||
11551 | if (verbose) { | |
11552 | uintptr_t status; | |
d553fb9b JL |
11553 | pthread_mutex_lock(&wd.wd_mutex); |
11554 | wd.wd_should_exit = B_TRUE; | |
11555 | pthread_cond_signal(&wd.wd_cv); | |
11556 | pthread_mutex_unlock(&wd.wd_mutex); | |
e60e158e JG |
11557 | (void) pthread_join(status_thr, (void *)&status); |
11558 | if (status != 0) | |
11559 | error = status; | |
11560 | } | |
11561 | ||
d553fb9b JL |
11562 | pthread_mutex_destroy(&wd.wd_mutex); |
11563 | pthread_cond_destroy(&wd.wd_cv); | |
e60e158e JG |
11564 | return (error); |
11565 | } | |
34dc7c2f BB |
11566 | |
11567 | static int | |
a926aab9 | 11568 | find_command_idx(const char *command, int *idx) |
34dc7c2f | 11569 | { |
a926aab9 | 11570 | for (int i = 0; i < NCOMMAND; ++i) { |
34dc7c2f BB |
11571 | if (command_table[i].name == NULL) |
11572 | continue; | |
11573 | ||
11574 | if (strcmp(command, command_table[i].name) == 0) { | |
11575 | *idx = i; | |
11576 | return (0); | |
11577 | } | |
11578 | } | |
11579 | return (1); | |
11580 | } | |
11581 | ||
50478c6d T |
11582 | /* |
11583 | * Display version message | |
11584 | */ | |
11585 | static int | |
11586 | zpool_do_version(int argc, char **argv) | |
11587 | { | |
e40ca391 | 11588 | (void) argc, (void) argv; |
2b4f2fc9 | 11589 | return (zfs_version_print() != 0); |
50478c6d T |
11590 | } |
11591 | ||
7228ba11 RN |
11592 | /* Display documentation */ |
11593 | static int | |
11594 | zpool_do_help(int argc, char **argv) | |
11595 | { | |
11596 | char page[MAXNAMELEN]; | |
11597 | if (argc < 3 || strcmp(argv[2], "zpool") == 0) | |
11598 | strcpy(page, "zpool"); | |
11599 | else if (strcmp(argv[2], "concepts") == 0 || | |
11600 | strcmp(argv[2], "props") == 0) | |
11601 | snprintf(page, sizeof (page), "zpool%s", argv[2]); | |
11602 | else | |
11603 | snprintf(page, sizeof (page), "zpool-%s", argv[2]); | |
11604 | ||
11605 | execlp("man", "man", page, NULL); | |
11606 | ||
11607 | fprintf(stderr, "couldn't run man program: %s", strerror(errno)); | |
11608 | return (-1); | |
11609 | } | |
11610 | ||
658fb802 CB |
11611 | /* |
11612 | * Do zpool_load_compat() and print error message on failure | |
11613 | */ | |
11614 | static zpool_compat_status_t | |
11615 | zpool_do_load_compat(const char *compat, boolean_t *list) | |
11616 | { | |
e086db16 CB |
11617 | char report[1024]; |
11618 | ||
658fb802 CB |
11619 | zpool_compat_status_t ret; |
11620 | ||
e086db16 CB |
11621 | ret = zpool_load_compat(compat, list, report, 1024); |
11622 | switch (ret) { | |
11623 | ||
658fb802 CB |
11624 | case ZPOOL_COMPATIBILITY_OK: |
11625 | break; | |
e086db16 CB |
11626 | |
11627 | case ZPOOL_COMPATIBILITY_NOFILES: | |
658fb802 | 11628 | case ZPOOL_COMPATIBILITY_BADFILE: |
e086db16 CB |
11629 | case ZPOOL_COMPATIBILITY_BADTOKEN: |
11630 | (void) fprintf(stderr, "Error: %s\n", report); | |
658fb802 | 11631 | break; |
e086db16 CB |
11632 | |
11633 | case ZPOOL_COMPATIBILITY_WARNTOKEN: | |
11634 | (void) fprintf(stderr, "Warning: %s\n", report); | |
11635 | ret = ZPOOL_COMPATIBILITY_OK; | |
658fb802 CB |
11636 | break; |
11637 | } | |
11638 | return (ret); | |
11639 | } | |
11640 | ||
34dc7c2f BB |
11641 | int |
11642 | main(int argc, char **argv) | |
11643 | { | |
8e896579 | 11644 | int ret = 0; |
d4ed6673 | 11645 | int i = 0; |
34dc7c2f | 11646 | char *cmdname; |
edc05fdb | 11647 | char **newargv; |
34dc7c2f BB |
11648 | |
11649 | (void) setlocale(LC_ALL, ""); | |
c2c7ca0d | 11650 | (void) setlocale(LC_NUMERIC, "C"); |
34dc7c2f | 11651 | (void) textdomain(TEXT_DOMAIN); |
5b4136bd | 11652 | srand(time(NULL)); |
34dc7c2f | 11653 | |
34dc7c2f BB |
11654 | opterr = 0; |
11655 | ||
11656 | /* | |
11657 | * Make sure the user has specified some command. | |
11658 | */ | |
11659 | if (argc < 2) { | |
11660 | (void) fprintf(stderr, gettext("missing command\n")); | |
11661 | usage(B_FALSE); | |
11662 | } | |
11663 | ||
11664 | cmdname = argv[1]; | |
11665 | ||
11666 | /* | |
11667 | * Special case '-?' | |
11668 | */ | |
d1d7e268 | 11669 | if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0) |
34dc7c2f BB |
11670 | usage(B_TRUE); |
11671 | ||
50478c6d T |
11672 | /* |
11673 | * Special case '-V|--version' | |
11674 | */ | |
11675 | if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0)) | |
11676 | return (zpool_do_version(argc, argv)); | |
11677 | ||
7228ba11 RN |
11678 | /* |
11679 | * Special case 'help' | |
11680 | */ | |
11681 | if (strcmp(cmdname, "help") == 0) | |
11682 | return (zpool_do_help(argc, argv)); | |
11683 | ||
65037d9b | 11684 | if ((g_zfs = libzfs_init()) == NULL) { |
afc8f0a6 | 11685 | (void) fprintf(stderr, "%s\n", libzfs_error_init(errno)); |
9b020fd9 | 11686 | return (1); |
65037d9b | 11687 | } |
9b020fd9 BB |
11688 | |
11689 | libzfs_print_on_error(g_zfs, B_TRUE); | |
11690 | ||
6f1ffb06 | 11691 | zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); |
34dc7c2f | 11692 | |
edc05fdb D |
11693 | /* |
11694 | * Many commands modify input strings for string parsing reasons. | |
11695 | * We create a copy to protect the original argv. | |
11696 | */ | |
a3dcc0aa | 11697 | newargv = safe_malloc((argc + 1) * sizeof (newargv[0])); |
edc05fdb D |
11698 | for (i = 0; i < argc; i++) |
11699 | newargv[i] = strdup(argv[i]); | |
11700 | newargv[argc] = NULL; | |
11701 | ||
34dc7c2f BB |
11702 | /* |
11703 | * Run the appropriate command. | |
11704 | */ | |
11705 | if (find_command_idx(cmdname, &i) == 0) { | |
11706 | current_command = &command_table[i]; | |
edc05fdb | 11707 | ret = command_table[i].func(argc - 1, newargv + 1); |
34dc7c2f BB |
11708 | } else if (strchr(cmdname, '=')) { |
11709 | verify(find_command_idx("set", &i) == 0); | |
11710 | current_command = &command_table[i]; | |
edc05fdb | 11711 | ret = command_table[i].func(argc, newargv); |
34dc7c2f BB |
11712 | } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { |
11713 | /* | |
11714 | * 'freeze' is a vile debugging abomination, so we treat | |
11715 | * it as such. | |
11716 | */ | |
4807c0ba TC |
11717 | zfs_cmd_t zc = {"\0"}; |
11718 | ||
11719 | (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); | |
11720 | ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc); | |
11721 | if (ret != 0) { | |
11722 | (void) fprintf(stderr, | |
11723 | gettext("failed to freeze pool: %d\n"), errno); | |
11724 | ret = 1; | |
11725 | } | |
11726 | ||
11727 | log_history = 0; | |
34dc7c2f BB |
11728 | } else { |
11729 | (void) fprintf(stderr, gettext("unrecognized " | |
11730 | "command '%s'\n"), cmdname); | |
11731 | usage(B_FALSE); | |
d4ed6673 | 11732 | ret = 1; |
34dc7c2f BB |
11733 | } |
11734 | ||
edc05fdb D |
11735 | for (i = 0; i < argc; i++) |
11736 | free(newargv[i]); | |
11737 | free(newargv); | |
11738 | ||
6f1ffb06 MA |
11739 | if (ret == 0 && log_history) |
11740 | (void) zpool_log_history(g_zfs, history_str); | |
11741 | ||
34dc7c2f BB |
11742 | libzfs_fini(g_zfs); |
11743 | ||
11744 | /* | |
11745 | * The 'ZFS_ABORT' environment variable causes us to dump core on exit | |
11746 | * for the purposes of running ::findleaks. | |
11747 | */ | |
11748 | if (getenv("ZFS_ABORT") != NULL) { | |
11749 | (void) printf("dumping core by request\n"); | |
11750 | abort(); | |
11751 | } | |
11752 | ||
11753 | return (ret); | |
11754 | } |