]>
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. |
c15d36c6 | 25 | * Copyright (c) 2011, 2020 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> | |
e60e158e | 53 | #include <time.h> |
34dc7c2f | 54 | #include <unistd.h> |
34dc7c2f BB |
55 | #include <pwd.h> |
56 | #include <zone.h> | |
d6418de0 | 57 | #include <sys/wait.h> |
1bd201e7 | 58 | #include <zfs_prop.h> |
34dc7c2f | 59 | #include <sys/fs/zfs.h> |
34dc7c2f | 60 | #include <sys/stat.h> |
46364cb2 | 61 | #include <sys/systeminfo.h> |
d02ca379 | 62 | #include <sys/fm/fs/zfs.h> |
26685276 BB |
63 | #include <sys/fm/util.h> |
64 | #include <sys/fm/protocol.h> | |
8c7aa0cf | 65 | #include <sys/zfs_ioctl.h> |
b5256303 | 66 | #include <sys/mount.h> |
d4a72f23 TC |
67 | #include <sys/sysmacros.h> |
68 | ||
193a37cb | 69 | #include <math.h> |
34dc7c2f BB |
70 | |
71 | #include <libzfs.h> | |
e89f1295 | 72 | #include <libzutil.h> |
34dc7c2f BB |
73 | |
74 | #include "zpool_util.h" | |
75 | #include "zfs_comutil.h" | |
9ae529ec | 76 | #include "zfeature_common.h" |
34dc7c2f | 77 | |
428870ff BB |
78 | #include "statcommon.h" |
79 | ||
af09c050 RD |
80 | libzfs_handle_t *g_zfs; |
81 | ||
34dc7c2f BB |
82 | static int zpool_do_create(int, char **); |
83 | static int zpool_do_destroy(int, char **); | |
84 | ||
85 | static int zpool_do_add(int, char **); | |
86 | static int zpool_do_remove(int, char **); | |
131cc95c | 87 | static int zpool_do_labelclear(int, char **); |
34dc7c2f | 88 | |
d2734cce SD |
89 | static int zpool_do_checkpoint(int, char **); |
90 | ||
34dc7c2f BB |
91 | static int zpool_do_list(int, char **); |
92 | static int zpool_do_iostat(int, char **); | |
93 | static int zpool_do_status(int, char **); | |
94 | ||
95 | static int zpool_do_online(int, char **); | |
96 | static int zpool_do_offline(int, char **); | |
97 | static int zpool_do_clear(int, char **); | |
1bd201e7 | 98 | static int zpool_do_reopen(int, char **); |
34dc7c2f | 99 | |
3541dc6d GA |
100 | static int zpool_do_reguid(int, char **); |
101 | ||
34dc7c2f BB |
102 | static int zpool_do_attach(int, char **); |
103 | static int zpool_do_detach(int, char **); | |
104 | static int zpool_do_replace(int, char **); | |
428870ff | 105 | static int zpool_do_split(int, char **); |
34dc7c2f | 106 | |
619f0976 | 107 | static int zpool_do_initialize(int, char **); |
34dc7c2f | 108 | static int zpool_do_scrub(int, char **); |
80a91e74 | 109 | static int zpool_do_resilver(int, char **); |
1b939560 | 110 | static int zpool_do_trim(int, char **); |
34dc7c2f BB |
111 | |
112 | static int zpool_do_import(int, char **); | |
113 | static int zpool_do_export(int, char **); | |
114 | ||
115 | static int zpool_do_upgrade(int, char **); | |
116 | ||
117 | static int zpool_do_history(int, char **); | |
26685276 | 118 | static int zpool_do_events(int, char **); |
34dc7c2f BB |
119 | |
120 | static int zpool_do_get(int, char **); | |
121 | static int zpool_do_set(int, char **); | |
122 | ||
bec1067d AP |
123 | static int zpool_do_sync(int, char **); |
124 | ||
50478c6d T |
125 | static int zpool_do_version(int, char **); |
126 | ||
e60e158e JG |
127 | static int zpool_do_wait(int, char **); |
128 | ||
7228ba11 RN |
129 | static int zpool_do_help(int argc, char **argv); |
130 | ||
658fb802 CB |
131 | static zpool_compat_status_t zpool_do_load_compat( |
132 | const char *, boolean_t *); | |
133 | ||
34dc7c2f BB |
134 | /* |
135 | * These libumem hooks provide a reasonable set of defaults for the allocator's | |
136 | * debugging facilities. | |
137 | */ | |
b128c09f BB |
138 | |
139 | #ifdef DEBUG | |
34dc7c2f BB |
140 | const char * |
141 | _umem_debug_init(void) | |
142 | { | |
143 | return ("default,verbose"); /* $UMEM_DEBUG setting */ | |
144 | } | |
145 | ||
146 | const char * | |
147 | _umem_logging_init(void) | |
148 | { | |
149 | return ("fail,contents"); /* $UMEM_LOGGING setting */ | |
150 | } | |
b128c09f | 151 | #endif |
34dc7c2f BB |
152 | |
153 | typedef enum { | |
154 | HELP_ADD, | |
155 | HELP_ATTACH, | |
156 | HELP_CLEAR, | |
157 | HELP_CREATE, | |
d2734cce | 158 | HELP_CHECKPOINT, |
34dc7c2f BB |
159 | HELP_DESTROY, |
160 | HELP_DETACH, | |
161 | HELP_EXPORT, | |
162 | HELP_HISTORY, | |
163 | HELP_IMPORT, | |
164 | HELP_IOSTAT, | |
131cc95c | 165 | HELP_LABELCLEAR, |
34dc7c2f BB |
166 | HELP_LIST, |
167 | HELP_OFFLINE, | |
168 | HELP_ONLINE, | |
169 | HELP_REPLACE, | |
170 | HELP_REMOVE, | |
619f0976 | 171 | HELP_INITIALIZE, |
34dc7c2f | 172 | HELP_SCRUB, |
80a91e74 | 173 | HELP_RESILVER, |
1b939560 | 174 | HELP_TRIM, |
34dc7c2f BB |
175 | HELP_STATUS, |
176 | HELP_UPGRADE, | |
26685276 | 177 | HELP_EVENTS, |
34dc7c2f | 178 | HELP_GET, |
428870ff | 179 | HELP_SET, |
3541dc6d | 180 | HELP_SPLIT, |
bec1067d | 181 | HELP_SYNC, |
1bd201e7 | 182 | HELP_REGUID, |
50478c6d | 183 | HELP_REOPEN, |
e60e158e JG |
184 | HELP_VERSION, |
185 | HELP_WAIT | |
34dc7c2f BB |
186 | } zpool_help_t; |
187 | ||
188 | ||
193a37cb TH |
189 | /* |
190 | * Flags for stats to display with "zpool iostats" | |
191 | */ | |
192 | enum iostat_type { | |
193 | IOS_DEFAULT = 0, | |
194 | IOS_LATENCY = 1, | |
195 | IOS_QUEUES = 2, | |
196 | IOS_L_HISTO = 3, | |
7e945072 | 197 | IOS_RQ_HISTO = 4, |
193a37cb TH |
198 | IOS_COUNT, /* always last element */ |
199 | }; | |
200 | ||
201 | /* iostat_type entries as bitmasks */ | |
202 | #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT) | |
203 | #define IOS_LATENCY_M (1ULL << IOS_LATENCY) | |
204 | #define IOS_QUEUES_M (1ULL << IOS_QUEUES) | |
205 | #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO) | |
7e945072 TH |
206 | #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO) |
207 | ||
208 | /* Mask of all the histo bits */ | |
209 | #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M) | |
210 | ||
211 | /* | |
212 | * Lookup table for iostat flags to nvlist names. Basically a list | |
213 | * of all the nvlists a flag requires. Also specifies the order in | |
214 | * which data gets printed in zpool iostat. | |
215 | */ | |
00888c08 | 216 | static const char *vsx_type_to_nvlist[IOS_COUNT][15] = { |
7e945072 TH |
217 | [IOS_L_HISTO] = { |
218 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
219 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
220 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
221 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
222 | ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, | |
223 | ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, | |
224 | ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, | |
225 | ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, | |
226 | ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, | |
1b939560 | 227 | ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, |
00888c08 | 228 | ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, |
7e945072 TH |
229 | NULL}, |
230 | [IOS_LATENCY] = { | |
231 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
232 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
233 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
234 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
1b939560 | 235 | ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, |
00888c08 | 236 | ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, |
7e945072 TH |
237 | NULL}, |
238 | [IOS_QUEUES] = { | |
239 | ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, | |
240 | ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, | |
241 | ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, | |
242 | ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, | |
243 | ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, | |
1b939560 | 244 | ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, |
00888c08 | 245 | ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE, |
7e945072 TH |
246 | NULL}, |
247 | [IOS_RQ_HISTO] = { | |
248 | ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO, | |
249 | ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO, | |
250 | ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO, | |
251 | ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO, | |
252 | ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO, | |
253 | ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO, | |
254 | ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO, | |
255 | ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO, | |
256 | ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO, | |
257 | ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO, | |
1b939560 BB |
258 | ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO, |
259 | ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO, | |
00888c08 TB |
260 | ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO, |
261 | ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO, | |
7e945072 TH |
262 | NULL}, |
263 | }; | |
264 | ||
265 | ||
266 | /* | |
267 | * Given a cb->cb_flags with a histogram bit set, return the iostat_type. | |
268 | * Right now, only one histo bit is ever set at one time, so we can | |
269 | * just do a highbit64(a) | |
270 | */ | |
271 | #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1) | |
193a37cb | 272 | |
34dc7c2f BB |
273 | typedef struct zpool_command { |
274 | const char *name; | |
275 | int (*func)(int, char **); | |
276 | zpool_help_t usage; | |
277 | } zpool_command_t; | |
278 | ||
279 | /* | |
280 | * Master command table. Each ZFS command has a name, associated function, and | |
281 | * usage message. The usage messages need to be internationalized, so we have | |
282 | * to have a function to return the usage message based on a command index. | |
283 | * | |
284 | * These commands are organized according to how they are displayed in the usage | |
285 | * message. An empty command (one with a NULL name) indicates an empty line in | |
286 | * the generic usage message. | |
287 | */ | |
288 | static zpool_command_t command_table[] = { | |
50478c6d T |
289 | { "version", zpool_do_version, HELP_VERSION }, |
290 | { NULL }, | |
34dc7c2f BB |
291 | { "create", zpool_do_create, HELP_CREATE }, |
292 | { "destroy", zpool_do_destroy, HELP_DESTROY }, | |
293 | { NULL }, | |
294 | { "add", zpool_do_add, HELP_ADD }, | |
295 | { "remove", zpool_do_remove, HELP_REMOVE }, | |
296 | { NULL }, | |
131cc95c DK |
297 | { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR }, |
298 | { NULL }, | |
d2734cce SD |
299 | { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT }, |
300 | { NULL }, | |
34dc7c2f BB |
301 | { "list", zpool_do_list, HELP_LIST }, |
302 | { "iostat", zpool_do_iostat, HELP_IOSTAT }, | |
303 | { "status", zpool_do_status, HELP_STATUS }, | |
304 | { NULL }, | |
305 | { "online", zpool_do_online, HELP_ONLINE }, | |
306 | { "offline", zpool_do_offline, HELP_OFFLINE }, | |
307 | { "clear", zpool_do_clear, HELP_CLEAR }, | |
1bd201e7 | 308 | { "reopen", zpool_do_reopen, HELP_REOPEN }, |
34dc7c2f BB |
309 | { NULL }, |
310 | { "attach", zpool_do_attach, HELP_ATTACH }, | |
311 | { "detach", zpool_do_detach, HELP_DETACH }, | |
312 | { "replace", zpool_do_replace, HELP_REPLACE }, | |
428870ff | 313 | { "split", zpool_do_split, HELP_SPLIT }, |
34dc7c2f | 314 | { NULL }, |
619f0976 | 315 | { "initialize", zpool_do_initialize, HELP_INITIALIZE }, |
80a91e74 | 316 | { "resilver", zpool_do_resilver, HELP_RESILVER }, |
1b939560 BB |
317 | { "scrub", zpool_do_scrub, HELP_SCRUB }, |
318 | { "trim", zpool_do_trim, HELP_TRIM }, | |
34dc7c2f BB |
319 | { NULL }, |
320 | { "import", zpool_do_import, HELP_IMPORT }, | |
321 | { "export", zpool_do_export, HELP_EXPORT }, | |
322 | { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, | |
3541dc6d | 323 | { "reguid", zpool_do_reguid, HELP_REGUID }, |
34dc7c2f BB |
324 | { NULL }, |
325 | { "history", zpool_do_history, HELP_HISTORY }, | |
26685276 BB |
326 | { "events", zpool_do_events, HELP_EVENTS }, |
327 | { NULL }, | |
34dc7c2f BB |
328 | { "get", zpool_do_get, HELP_GET }, |
329 | { "set", zpool_do_set, HELP_SET }, | |
bec1067d | 330 | { "sync", zpool_do_sync, HELP_SYNC }, |
e60e158e JG |
331 | { NULL }, |
332 | { "wait", zpool_do_wait, HELP_WAIT }, | |
34dc7c2f BB |
333 | }; |
334 | ||
193a37cb | 335 | #define NCOMMAND (ARRAY_SIZE(command_table)) |
34dc7c2f | 336 | |
cc99f275 DB |
337 | #define VDEV_ALLOC_CLASS_LOGS "logs" |
338 | ||
6f1ffb06 | 339 | static zpool_command_t *current_command; |
2a673e76 | 340 | static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV); |
34dc7c2f | 341 | static char history_str[HIS_MAX_RECORD_LEN]; |
6f1ffb06 | 342 | static boolean_t log_history = B_TRUE; |
428870ff BB |
343 | static uint_t timestamp_fmt = NODATE; |
344 | ||
34dc7c2f | 345 | static const char * |
e9aa730c GM |
346 | get_usage(zpool_help_t idx) |
347 | { | |
34dc7c2f BB |
348 | switch (idx) { |
349 | case HELP_ADD: | |
a77f29f9 | 350 | return (gettext("\tadd [-fgLnP] [-o property=value] " |
df831108 | 351 | "<pool> <vdev> ...\n")); |
34dc7c2f | 352 | case HELP_ATTACH: |
9a49d3f3 | 353 | return (gettext("\tattach [-fsw] [-o property=value] " |
df831108 | 354 | "<pool> <device> <new-device>\n")); |
34dc7c2f | 355 | case HELP_CLEAR: |
428870ff | 356 | return (gettext("\tclear [-nF] <pool> [device]\n")); |
34dc7c2f | 357 | case HELP_CREATE: |
9ae529ec | 358 | return (gettext("\tcreate [-fnd] [-o property=value] ... \n" |
b128c09f | 359 | "\t [-O file-system-property=value] ... \n" |
34dc7c2f | 360 | "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); |
d2734cce | 361 | case HELP_CHECKPOINT: |
e60e158e | 362 | return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n")); |
34dc7c2f BB |
363 | case HELP_DESTROY: |
364 | return (gettext("\tdestroy [-f] <pool>\n")); | |
365 | case HELP_DETACH: | |
366 | return (gettext("\tdetach <pool> <device>\n")); | |
367 | case HELP_EXPORT: | |
859735c0 | 368 | return (gettext("\texport [-af] <pool> ...\n")); |
34dc7c2f BB |
369 | case HELP_HISTORY: |
370 | return (gettext("\thistory [-il] [<pool>] ...\n")); | |
371 | case HELP_IMPORT: | |
372 | return (gettext("\timport [-d dir] [-D]\n" | |
373 | "\timport [-o mntopts] [-o property=value] ... \n" | |
b5256303 | 374 | "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " |
572e2857 | 375 | "[-R root] [-F [-n]] -a\n" |
34dc7c2f | 376 | "\timport [-o mntopts] [-o property=value] ... \n" |
b5256303 | 377 | "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " |
572e2857 | 378 | "[-R root] [-F [-n]]\n" |
d2734cce | 379 | "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n")); |
34dc7c2f | 380 | case HELP_IOSTAT: |
d6418de0 TH |
381 | return (gettext("\tiostat [[[-c [script1,script2,...]" |
382 | "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n" | |
383 | "\t [[pool ...]|[pool vdev ...]|[vdev ...]]" | |
8fccfa8e | 384 | " [[-n] interval [count]]\n")); |
131cc95c DK |
385 | case HELP_LABELCLEAR: |
386 | return (gettext("\tlabelclear [-f] <vdev>\n")); | |
34dc7c2f | 387 | case HELP_LIST: |
2a8b84b7 | 388 | return (gettext("\tlist [-gHLpPv] [-o property[,...]] " |
63f88c12 | 389 | "[-T d|u] [pool] ... \n" |
390 | "\t [interval [count]]\n")); | |
34dc7c2f | 391 | case HELP_OFFLINE: |
4a283c7f | 392 | return (gettext("\toffline [-f] [-t] <pool> <device> ...\n")); |
34dc7c2f | 393 | case HELP_ONLINE: |
aebfb848 | 394 | return (gettext("\tonline [-e] <pool> <device> ...\n")); |
34dc7c2f | 395 | case HELP_REPLACE: |
9a49d3f3 | 396 | return (gettext("\treplace [-fsw] [-o property=value] " |
628668a3 | 397 | "<pool> <device> [new-device]\n")); |
34dc7c2f | 398 | case HELP_REMOVE: |
e60e158e | 399 | return (gettext("\tremove [-npsw] <pool> <device> ...\n")); |
1bd201e7 | 400 | case HELP_REOPEN: |
d3f2cd7e | 401 | return (gettext("\treopen [-n] <pool>\n")); |
619f0976 | 402 | case HELP_INITIALIZE: |
e34e15ed | 403 | return (gettext("\tinitialize [-c | -s | -u] [-w] <pool> " |
a769fb53 | 404 | "[<device> ...]\n")); |
34dc7c2f | 405 | case HELP_SCRUB: |
482eeef8 | 406 | return (gettext("\tscrub [-s | -p] [-w] [-e] <pool> ...\n")); |
80a91e74 TC |
407 | case HELP_RESILVER: |
408 | return (gettext("\tresilver <pool> ...\n")); | |
1b939560 | 409 | case HELP_TRIM: |
2288d419 | 410 | return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> " |
1b939560 | 411 | "[<device> ...]\n")); |
34dc7c2f | 412 | case HELP_STATUS: |
ad796b8a | 413 | return (gettext("\tstatus [-c [script1,script2,...]] " |
43977948 | 414 | "[-igLpPstvxD] [-T d|u] [pool] ... \n" |
63f88c12 | 415 | "\t [interval [count]]\n")); |
34dc7c2f BB |
416 | case HELP_UPGRADE: |
417 | return (gettext("\tupgrade\n" | |
418 | "\tupgrade -v\n" | |
419 | "\tupgrade [-V version] <-a | pool ...>\n")); | |
26685276 | 420 | case HELP_EVENTS: |
88f9c939 | 421 | return (gettext("\tevents [-vHf [pool] | -c]\n")); |
34dc7c2f | 422 | case HELP_GET: |
2a8b84b7 AS |
423 | return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] " |
424 | "<\"all\" | property[,...]> <pool> ...\n")); | |
34dc7c2f | 425 | case HELP_SET: |
a0276f70 RW |
426 | return (gettext("\tset <property=value> <pool>\n" |
427 | "\tset <vdev_property=value> <pool> <vdev>\n")); | |
428870ff | 428 | case HELP_SPLIT: |
b5256303 | 429 | return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n" |
428870ff BB |
430 | "\t [-o property=value] <pool> <newpool> " |
431 | "[<device> ...]\n")); | |
3541dc6d GA |
432 | case HELP_REGUID: |
433 | return (gettext("\treguid <pool>\n")); | |
bec1067d AP |
434 | case HELP_SYNC: |
435 | return (gettext("\tsync [pool] ...\n")); | |
50478c6d T |
436 | case HELP_VERSION: |
437 | return (gettext("\tversion\n")); | |
e60e158e JG |
438 | case HELP_WAIT: |
439 | return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] " | |
440 | "<pool> [interval]\n")); | |
90f1c3c9 AZ |
441 | default: |
442 | __builtin_unreachable(); | |
34dc7c2f | 443 | } |
34dc7c2f BB |
444 | } |
445 | ||
619f0976 GW |
446 | static void |
447 | zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res) | |
448 | { | |
449 | uint_t children = 0; | |
450 | nvlist_t **child; | |
451 | uint_t i; | |
452 | ||
453 | (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
454 | &child, &children); | |
455 | ||
456 | if (children == 0) { | |
1b939560 BB |
457 | char *path = zpool_vdev_name(g_zfs, zhp, nvroot, |
458 | VDEV_NAME_PATH); | |
459 | ||
a84c92f9 NH |
460 | if (strcmp(path, VDEV_TYPE_INDIRECT) != 0 && |
461 | strcmp(path, VDEV_TYPE_HOLE) != 0) | |
1b939560 BB |
462 | fnvlist_add_boolean(res, path); |
463 | ||
619f0976 GW |
464 | free(path); |
465 | return; | |
466 | } | |
467 | ||
468 | for (i = 0; i < children; i++) { | |
469 | zpool_collect_leaves(zhp, child[i], res); | |
470 | } | |
471 | } | |
34dc7c2f BB |
472 | |
473 | /* | |
474 | * Callback routine that will print out a pool property value. | |
475 | */ | |
476 | static int | |
2a673e76 | 477 | print_pool_prop_cb(int prop, void *cb) |
34dc7c2f BB |
478 | { |
479 | FILE *fp = cb; | |
480 | ||
cc99f275 | 481 | (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop)); |
34dc7c2f BB |
482 | |
483 | if (zpool_prop_readonly(prop)) | |
484 | (void) fprintf(fp, " NO "); | |
485 | else | |
428870ff | 486 | (void) fprintf(fp, " YES "); |
34dc7c2f BB |
487 | |
488 | if (zpool_prop_values(prop) == NULL) | |
489 | (void) fprintf(fp, "-\n"); | |
490 | else | |
491 | (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); | |
492 | ||
493 | return (ZPROP_CONT); | |
494 | } | |
495 | ||
2a673e76 AJ |
496 | /* |
497 | * Callback routine that will print out a vdev property value. | |
498 | */ | |
499 | static int | |
500 | print_vdev_prop_cb(int prop, void *cb) | |
501 | { | |
502 | FILE *fp = cb; | |
503 | ||
504 | (void) fprintf(fp, "\t%-19s ", vdev_prop_to_name(prop)); | |
505 | ||
506 | if (vdev_prop_readonly(prop)) | |
507 | (void) fprintf(fp, " NO "); | |
508 | else | |
509 | (void) fprintf(fp, " YES "); | |
510 | ||
511 | if (vdev_prop_values(prop) == NULL) | |
512 | (void) fprintf(fp, "-\n"); | |
513 | else | |
514 | (void) fprintf(fp, "%s\n", vdev_prop_values(prop)); | |
515 | ||
516 | return (ZPROP_CONT); | |
517 | } | |
518 | ||
34dc7c2f BB |
519 | /* |
520 | * Display usage message. If we're inside a command, display only the usage for | |
521 | * that command. Otherwise, iterate over the entire command table and display | |
522 | * a complete usage message. | |
523 | */ | |
460748d4 | 524 | static __attribute__((noreturn)) void |
34dc7c2f BB |
525 | usage(boolean_t requested) |
526 | { | |
527 | FILE *fp = requested ? stdout : stderr; | |
528 | ||
529 | if (current_command == NULL) { | |
530 | int i; | |
531 | ||
532 | (void) fprintf(fp, gettext("usage: zpool command args ...\n")); | |
533 | (void) fprintf(fp, | |
534 | gettext("where 'command' is one of the following:\n\n")); | |
535 | ||
536 | for (i = 0; i < NCOMMAND; i++) { | |
537 | if (command_table[i].name == NULL) | |
538 | (void) fprintf(fp, "\n"); | |
539 | else | |
540 | (void) fprintf(fp, "%s", | |
541 | get_usage(command_table[i].usage)); | |
542 | } | |
7228ba11 RN |
543 | |
544 | (void) fprintf(fp, | |
545 | gettext("\nFor further help on a command or topic, " | |
546 | "run: %s\n"), "zpool help [<topic>]"); | |
34dc7c2f BB |
547 | } else { |
548 | (void) fprintf(fp, gettext("usage:\n")); | |
549 | (void) fprintf(fp, "%s", get_usage(current_command->usage)); | |
550 | } | |
551 | ||
552 | if (current_command != NULL && | |
2a673e76 | 553 | current_prop_type != (ZFS_TYPE_POOL | ZFS_TYPE_VDEV) && |
34dc7c2f BB |
554 | ((strcmp(current_command->name, "set") == 0) || |
555 | (strcmp(current_command->name, "get") == 0) || | |
556 | (strcmp(current_command->name, "list") == 0))) { | |
557 | ||
3c1e1933 | 558 | (void) fprintf(fp, "%s", |
34dc7c2f BB |
559 | gettext("\nthe following properties are supported:\n")); |
560 | ||
cc99f275 | 561 | (void) fprintf(fp, "\n\t%-19s %s %s\n\n", |
34dc7c2f BB |
562 | "PROPERTY", "EDIT", "VALUES"); |
563 | ||
564 | /* Iterate over all properties */ | |
2a673e76 AJ |
565 | if (current_prop_type == ZFS_TYPE_POOL) { |
566 | (void) zprop_iter(print_pool_prop_cb, fp, B_FALSE, | |
567 | B_TRUE, current_prop_type); | |
9ae529ec | 568 | |
2a673e76 AJ |
569 | (void) fprintf(fp, "\t%-19s ", "feature@..."); |
570 | (void) fprintf(fp, "YES " | |
571 | "disabled | enabled | active\n"); | |
9ae529ec | 572 | |
2a673e76 AJ |
573 | (void) fprintf(fp, gettext("\nThe feature@ properties " |
574 | "must be appended with a feature name.\n" | |
575 | "See zpool-features(7).\n")); | |
576 | } else if (current_prop_type == ZFS_TYPE_VDEV) { | |
577 | (void) zprop_iter(print_vdev_prop_cb, fp, B_FALSE, | |
578 | B_TRUE, current_prop_type); | |
579 | } | |
34dc7c2f BB |
580 | } |
581 | ||
582 | /* | |
583 | * See comments at end of main(). | |
584 | */ | |
585 | if (getenv("ZFS_ABORT") != NULL) { | |
586 | (void) printf("dumping core by request\n"); | |
587 | abort(); | |
588 | } | |
589 | ||
590 | exit(requested ? 0 : 2); | |
591 | } | |
592 | ||
619f0976 | 593 | /* |
e34e15ed | 594 | * zpool initialize [-c | -s | -u] [-w] <pool> [<vdev> ...] |
619f0976 GW |
595 | * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool |
596 | * if none specified. | |
597 | * | |
598 | * -c Cancel. Ends active initializing. | |
599 | * -s Suspend. Initializing can then be restarted with no flags. | |
e34e15ed | 600 | * -u Uninitialize. Clears initialization state. |
e60e158e | 601 | * -w Wait. Blocks until initializing has completed. |
619f0976 GW |
602 | */ |
603 | int | |
604 | zpool_do_initialize(int argc, char **argv) | |
605 | { | |
606 | int c; | |
607 | char *poolname; | |
608 | zpool_handle_t *zhp; | |
609 | nvlist_t *vdevs; | |
610 | int err = 0; | |
e60e158e | 611 | boolean_t wait = B_FALSE; |
619f0976 GW |
612 | |
613 | struct option long_options[] = { | |
614 | {"cancel", no_argument, NULL, 'c'}, | |
615 | {"suspend", no_argument, NULL, 's'}, | |
e34e15ed | 616 | {"uninit", no_argument, NULL, 'u'}, |
e60e158e | 617 | {"wait", no_argument, NULL, 'w'}, |
619f0976 GW |
618 | {0, 0, 0, 0} |
619 | }; | |
620 | ||
1b939560 | 621 | pool_initialize_func_t cmd_type = POOL_INITIALIZE_START; |
e34e15ed BB |
622 | while ((c = getopt_long(argc, argv, "csuw", long_options, |
623 | NULL)) != -1) { | |
619f0976 GW |
624 | switch (c) { |
625 | case 'c': | |
1b939560 | 626 | if (cmd_type != POOL_INITIALIZE_START && |
a769fb53 | 627 | cmd_type != POOL_INITIALIZE_CANCEL) { |
619f0976 GW |
628 | (void) fprintf(stderr, gettext("-c cannot be " |
629 | "combined with other options\n")); | |
630 | usage(B_FALSE); | |
631 | } | |
632 | cmd_type = POOL_INITIALIZE_CANCEL; | |
633 | break; | |
634 | case 's': | |
1b939560 | 635 | if (cmd_type != POOL_INITIALIZE_START && |
a769fb53 | 636 | cmd_type != POOL_INITIALIZE_SUSPEND) { |
619f0976 GW |
637 | (void) fprintf(stderr, gettext("-s cannot be " |
638 | "combined with other options\n")); | |
639 | usage(B_FALSE); | |
640 | } | |
641 | cmd_type = POOL_INITIALIZE_SUSPEND; | |
642 | break; | |
e34e15ed BB |
643 | case 'u': |
644 | if (cmd_type != POOL_INITIALIZE_START && | |
645 | cmd_type != POOL_INITIALIZE_UNINIT) { | |
646 | (void) fprintf(stderr, gettext("-u cannot be " | |
647 | "combined with other options\n")); | |
648 | usage(B_FALSE); | |
649 | } | |
650 | cmd_type = POOL_INITIALIZE_UNINIT; | |
651 | break; | |
e60e158e JG |
652 | case 'w': |
653 | wait = B_TRUE; | |
654 | break; | |
619f0976 GW |
655 | case '?': |
656 | if (optopt != 0) { | |
657 | (void) fprintf(stderr, | |
658 | gettext("invalid option '%c'\n"), optopt); | |
659 | } else { | |
660 | (void) fprintf(stderr, | |
661 | gettext("invalid option '%s'\n"), | |
662 | argv[optind - 1]); | |
663 | } | |
664 | usage(B_FALSE); | |
665 | } | |
666 | } | |
667 | ||
668 | argc -= optind; | |
669 | argv += optind; | |
670 | ||
671 | if (argc < 1) { | |
672 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
673 | usage(B_FALSE); | |
674 | return (-1); | |
675 | } | |
676 | ||
e60e158e | 677 | if (wait && (cmd_type != POOL_INITIALIZE_START)) { |
e34e15ed BB |
678 | (void) fprintf(stderr, gettext("-w cannot be used with -c, -s" |
679 | "or -u\n")); | |
e60e158e JG |
680 | usage(B_FALSE); |
681 | } | |
682 | ||
619f0976 GW |
683 | poolname = argv[0]; |
684 | zhp = zpool_open(g_zfs, poolname); | |
685 | if (zhp == NULL) | |
686 | return (-1); | |
687 | ||
688 | vdevs = fnvlist_alloc(); | |
689 | if (argc == 1) { | |
690 | /* no individual leaf vdevs specified, so add them all */ | |
691 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
692 | nvlist_t *nvroot = fnvlist_lookup_nvlist(config, | |
693 | ZPOOL_CONFIG_VDEV_TREE); | |
694 | zpool_collect_leaves(zhp, nvroot, vdevs); | |
695 | } else { | |
1b939560 | 696 | for (int i = 1; i < argc; i++) { |
619f0976 GW |
697 | fnvlist_add_boolean(vdevs, argv[i]); |
698 | } | |
699 | } | |
700 | ||
e60e158e JG |
701 | if (wait) |
702 | err = zpool_initialize_wait(zhp, cmd_type, vdevs); | |
703 | else | |
704 | err = zpool_initialize(zhp, cmd_type, vdevs); | |
619f0976 GW |
705 | |
706 | fnvlist_free(vdevs); | |
707 | zpool_close(zhp); | |
708 | ||
709 | return (err); | |
710 | } | |
711 | ||
cc99f275 DB |
712 | /* |
713 | * print a pool vdev config for dry runs | |
714 | */ | |
715 | static void | |
34dc7c2f | 716 | print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, |
cc99f275 | 717 | const char *match, int name_flags) |
34dc7c2f BB |
718 | { |
719 | nvlist_t **child; | |
720 | uint_t c, children; | |
721 | char *vname; | |
cc99f275 | 722 | boolean_t printed = B_FALSE; |
34dc7c2f BB |
723 | |
724 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
cc99f275 DB |
725 | &child, &children) != 0) { |
726 | if (name != NULL) | |
727 | (void) printf("\t%*s%s\n", indent, "", name); | |
34dc7c2f | 728 | return; |
cc99f275 | 729 | } |
34dc7c2f BB |
730 | |
731 | for (c = 0; c < children; c++) { | |
0cb40fa3 | 732 | uint64_t is_log = B_FALSE, is_hole = B_FALSE; |
d1807f16 | 733 | const char *class = ""; |
34dc7c2f | 734 | |
0cb40fa3 AF |
735 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, |
736 | &is_hole); | |
737 | ||
738 | if (is_hole == B_TRUE) { | |
739 | continue; | |
740 | } | |
741 | ||
34dc7c2f BB |
742 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
743 | &is_log); | |
cc99f275 | 744 | if (is_log) |
d1807f16 | 745 | class = VDEV_ALLOC_BIAS_LOG; |
cc99f275 DB |
746 | (void) nvlist_lookup_string(child[c], |
747 | ZPOOL_CONFIG_ALLOCATION_BIAS, &class); | |
748 | if (strcmp(match, class) != 0) | |
34dc7c2f BB |
749 | continue; |
750 | ||
cc99f275 DB |
751 | if (!printed && name != NULL) { |
752 | (void) printf("\t%*s%s\n", indent, "", name); | |
753 | printed = B_TRUE; | |
754 | } | |
d2f3e292 | 755 | vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags); |
cc99f275 DB |
756 | print_vdev_tree(zhp, vname, child[c], indent + 2, "", |
757 | name_flags); | |
34dc7c2f BB |
758 | free(vname); |
759 | } | |
760 | } | |
761 | ||
0cb40fa3 AF |
762 | /* |
763 | * Print the list of l2cache devices for dry runs. | |
764 | */ | |
765 | static void | |
766 | print_cache_list(nvlist_t *nv, int indent) | |
767 | { | |
768 | nvlist_t **child; | |
769 | uint_t c, children; | |
770 | ||
771 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, | |
772 | &child, &children) == 0 && children > 0) { | |
773 | (void) printf("\t%*s%s\n", indent, "", "cache"); | |
774 | } else { | |
775 | return; | |
776 | } | |
777 | for (c = 0; c < children; c++) { | |
778 | char *vname; | |
779 | ||
780 | vname = zpool_vdev_name(g_zfs, NULL, child[c], 0); | |
781 | (void) printf("\t%*s%s\n", indent + 2, "", vname); | |
782 | free(vname); | |
783 | } | |
784 | } | |
785 | ||
786 | /* | |
787 | * Print the list of spares for dry runs. | |
788 | */ | |
789 | static void | |
790 | print_spare_list(nvlist_t *nv, int indent) | |
791 | { | |
792 | nvlist_t **child; | |
793 | uint_t c, children; | |
794 | ||
795 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, | |
796 | &child, &children) == 0 && children > 0) { | |
797 | (void) printf("\t%*s%s\n", indent, "", "spares"); | |
798 | } else { | |
799 | return; | |
800 | } | |
801 | for (c = 0; c < children; c++) { | |
802 | char *vname; | |
803 | ||
804 | vname = zpool_vdev_name(g_zfs, NULL, child[c], 0); | |
805 | (void) printf("\t%*s%s\n", indent + 2, "", vname); | |
806 | free(vname); | |
807 | } | |
808 | } | |
809 | ||
b9b24bb4 CS |
810 | static boolean_t |
811 | prop_list_contains_feature(nvlist_t *proplist) | |
812 | { | |
813 | nvpair_t *nvp; | |
814 | for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; | |
815 | nvp = nvlist_next_nvpair(proplist, nvp)) { | |
816 | if (zpool_prop_feature(nvpair_name(nvp))) | |
817 | return (B_TRUE); | |
818 | } | |
819 | return (B_FALSE); | |
820 | } | |
821 | ||
34dc7c2f BB |
822 | /* |
823 | * Add a property pair (name, string-value) into a property nvlist. | |
824 | */ | |
825 | static int | |
a926aab9 | 826 | add_prop_list(const char *propname, const char *propval, nvlist_t **props, |
b128c09f | 827 | boolean_t poolprop) |
34dc7c2f | 828 | { |
31864e3d | 829 | zpool_prop_t prop = ZPOOL_PROP_INVAL; |
34dc7c2f | 830 | nvlist_t *proplist; |
b128c09f | 831 | const char *normnm; |
d1807f16 | 832 | const char *strval; |
34dc7c2f BB |
833 | |
834 | if (*props == NULL && | |
835 | nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { | |
836 | (void) fprintf(stderr, | |
837 | gettext("internal error: out of memory\n")); | |
838 | return (1); | |
839 | } | |
840 | ||
841 | proplist = *props; | |
842 | ||
b128c09f | 843 | if (poolprop) { |
b9b24bb4 | 844 | const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); |
e086db16 | 845 | const char *cname = |
658fb802 | 846 | zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY); |
b9b24bb4 | 847 | |
31864e3d | 848 | if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL && |
2a673e76 AJ |
849 | (!zpool_prop_feature(propname) && |
850 | !zpool_prop_vdev(propname))) { | |
b128c09f | 851 | (void) fprintf(stderr, gettext("property '%s' is " |
2a673e76 | 852 | "not a valid pool or vdev property\n"), propname); |
b128c09f BB |
853 | return (2); |
854 | } | |
b9b24bb4 CS |
855 | |
856 | /* | |
857 | * feature@ properties and version should not be specified | |
858 | * at the same time. | |
859 | */ | |
31864e3d | 860 | if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) && |
b9b24bb4 CS |
861 | nvlist_exists(proplist, vname)) || |
862 | (prop == ZPOOL_PROP_VERSION && | |
863 | prop_list_contains_feature(proplist))) { | |
864 | (void) fprintf(stderr, gettext("'feature@' and " | |
865 | "'version' properties cannot be specified " | |
866 | "together\n")); | |
867 | return (2); | |
868 | } | |
869 | ||
658fb802 | 870 | /* |
e086db16 CB |
871 | * if version is specified, only "legacy" compatibility |
872 | * may be requested | |
658fb802 CB |
873 | */ |
874 | if ((prop == ZPOOL_PROP_COMPATIBILITY && | |
e086db16 | 875 | strcmp(propval, ZPOOL_COMPAT_LEGACY) != 0 && |
658fb802 CB |
876 | nvlist_exists(proplist, vname)) || |
877 | (prop == ZPOOL_PROP_VERSION && | |
e086db16 CB |
878 | nvlist_exists(proplist, cname) && |
879 | strcmp(fnvlist_lookup_string(proplist, cname), | |
880 | ZPOOL_COMPAT_LEGACY) != 0)) { | |
881 | (void) fprintf(stderr, gettext("when 'version' is " | |
882 | "specified, the 'compatibility' feature may only " | |
883 | "be set to '" ZPOOL_COMPAT_LEGACY "'\n")); | |
658fb802 CB |
884 | return (2); |
885 | } | |
b9b24bb4 | 886 | |
2a673e76 | 887 | if (zpool_prop_feature(propname) || zpool_prop_vdev(propname)) |
9ae529ec CS |
888 | normnm = propname; |
889 | else | |
890 | normnm = zpool_prop_to_name(prop); | |
b128c09f | 891 | } else { |
5140a58f | 892 | zfs_prop_t fsprop = zfs_name_to_prop(propname); |
893 | ||
894 | if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM, | |
895 | B_FALSE)) { | |
896 | normnm = zfs_prop_to_name(fsprop); | |
897 | } else if (zfs_prop_user(propname) || | |
898 | zfs_prop_userquota(propname)) { | |
9babb374 | 899 | normnm = propname; |
5140a58f | 900 | } else { |
901 | (void) fprintf(stderr, gettext("property '%s' is " | |
902 | "not a valid filesystem property\n"), propname); | |
903 | return (2); | |
b128c09f | 904 | } |
34dc7c2f BB |
905 | } |
906 | ||
b128c09f BB |
907 | if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && |
908 | prop != ZPOOL_PROP_CACHEFILE) { | |
34dc7c2f BB |
909 | (void) fprintf(stderr, gettext("property '%s' " |
910 | "specified multiple times\n"), propname); | |
911 | return (2); | |
912 | } | |
913 | ||
b128c09f | 914 | if (nvlist_add_string(proplist, normnm, propval) != 0) { |
34dc7c2f BB |
915 | (void) fprintf(stderr, gettext("internal " |
916 | "error: out of memory\n")); | |
917 | return (1); | |
918 | } | |
919 | ||
920 | return (0); | |
921 | } | |
922 | ||
2f3ec900 RY |
923 | /* |
924 | * Set a default property pair (name, string-value) in a property nvlist | |
925 | */ | |
926 | static int | |
a926aab9 AZ |
927 | add_prop_list_default(const char *propname, const char *propval, |
928 | nvlist_t **props) | |
2f3ec900 | 929 | { |
d1807f16 | 930 | const char *pval; |
2f3ec900 RY |
931 | |
932 | if (nvlist_lookup_string(*props, propname, &pval) == 0) | |
933 | return (0); | |
934 | ||
935 | return (add_prop_list(propname, propval, props, B_TRUE)); | |
936 | } | |
937 | ||
34dc7c2f | 938 | /* |
a77f29f9 | 939 | * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ... |
34dc7c2f BB |
940 | * |
941 | * -f Force addition of devices, even if they appear in use | |
d2f3e292 RY |
942 | * -g Display guid for individual vdev name. |
943 | * -L Follow links when resolving vdev path name. | |
34dc7c2f BB |
944 | * -n Do not add the devices, but display the resulting layout if |
945 | * they were to be added. | |
df831108 | 946 | * -o Set property=value. |
a77f29f9 | 947 | * -P Display full path for vdev name. |
34dc7c2f BB |
948 | * |
949 | * Adds the given vdevs to 'pool'. As with create, the bulk of this work is | |
227d3793 | 950 | * handled by make_root_vdev(), which constructs the nvlist needed to pass to |
34dc7c2f BB |
951 | * libzfs. |
952 | */ | |
953 | int | |
954 | zpool_do_add(int argc, char **argv) | |
955 | { | |
956 | boolean_t force = B_FALSE; | |
957 | boolean_t dryrun = B_FALSE; | |
d2f3e292 | 958 | int name_flags = 0; |
34dc7c2f BB |
959 | int c; |
960 | nvlist_t *nvroot; | |
961 | char *poolname; | |
962 | int ret; | |
963 | zpool_handle_t *zhp; | |
964 | nvlist_t *config; | |
df831108 CP |
965 | nvlist_t *props = NULL; |
966 | char *propval; | |
34dc7c2f BB |
967 | |
968 | /* check options */ | |
a77f29f9 | 969 | while ((c = getopt(argc, argv, "fgLno:P")) != -1) { |
34dc7c2f BB |
970 | switch (c) { |
971 | case 'f': | |
972 | force = B_TRUE; | |
973 | break; | |
d2f3e292 RY |
974 | case 'g': |
975 | name_flags |= VDEV_NAME_GUID; | |
976 | break; | |
977 | case 'L': | |
978 | name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
979 | break; | |
34dc7c2f BB |
980 | case 'n': |
981 | dryrun = B_TRUE; | |
982 | break; | |
df831108 CP |
983 | case 'o': |
984 | if ((propval = strchr(optarg, '=')) == NULL) { | |
985 | (void) fprintf(stderr, gettext("missing " | |
986 | "'=' for -o option\n")); | |
987 | usage(B_FALSE); | |
988 | } | |
989 | *propval = '\0'; | |
990 | propval++; | |
991 | ||
992 | if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || | |
993 | (add_prop_list(optarg, propval, &props, B_TRUE))) | |
994 | usage(B_FALSE); | |
995 | break; | |
a77f29f9 | 996 | case 'P': |
d2f3e292 RY |
997 | name_flags |= VDEV_NAME_PATH; |
998 | break; | |
34dc7c2f BB |
999 | case '?': |
1000 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1001 | optopt); | |
1002 | usage(B_FALSE); | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | argc -= optind; | |
1007 | argv += optind; | |
1008 | ||
1009 | /* get pool name and check number of arguments */ | |
1010 | if (argc < 1) { | |
1011 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
1012 | usage(B_FALSE); | |
1013 | } | |
1014 | if (argc < 2) { | |
1015 | (void) fprintf(stderr, gettext("missing vdev specification\n")); | |
1016 | usage(B_FALSE); | |
1017 | } | |
1018 | ||
1019 | poolname = argv[0]; | |
1020 | ||
1021 | argc--; | |
1022 | argv++; | |
1023 | ||
1024 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
1025 | return (1); | |
1026 | ||
1027 | if ((config = zpool_get_config(zhp, NULL)) == NULL) { | |
1028 | (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), | |
1029 | poolname); | |
1030 | zpool_close(zhp); | |
1031 | return (1); | |
1032 | } | |
1033 | ||
dddef7d6 | 1034 | /* unless manually specified use "ashift" pool property (if set) */ |
1035 | if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { | |
1036 | int intval; | |
1037 | zprop_source_t src; | |
1038 | char strval[ZPOOL_MAXPROPLEN]; | |
1039 | ||
1040 | intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); | |
1041 | if (src != ZPROP_SRC_DEFAULT) { | |
1042 | (void) sprintf(strval, "%" PRId32, intval); | |
1043 | verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, | |
1044 | &props, B_TRUE) == 0); | |
1045 | } | |
1046 | } | |
1047 | ||
227d3793 | 1048 | /* pass off to make_root_vdev for processing */ |
df831108 | 1049 | nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun, |
b128c09f | 1050 | argc, argv); |
34dc7c2f BB |
1051 | if (nvroot == NULL) { |
1052 | zpool_close(zhp); | |
1053 | return (1); | |
1054 | } | |
1055 | ||
1056 | if (dryrun) { | |
1057 | nvlist_t *poolnvroot; | |
0cb40fa3 AF |
1058 | nvlist_t **l2child, **sparechild; |
1059 | uint_t l2children, sparechildren, c; | |
e02b533e | 1060 | char *vname; |
0cb40fa3 | 1061 | boolean_t hadcache = B_FALSE, hadspare = B_FALSE; |
34dc7c2f BB |
1062 | |
1063 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
1064 | &poolnvroot) == 0); | |
1065 | ||
1066 | (void) printf(gettext("would update '%s' to the following " | |
0cb40fa3 | 1067 | "configuration:\n\n"), zpool_get_name(zhp)); |
34dc7c2f BB |
1068 | |
1069 | /* print original main pool and new tree */ | |
cc99f275 DB |
1070 | print_vdev_tree(zhp, poolname, poolnvroot, 0, "", |
1071 | name_flags | VDEV_NAME_TYPE_ID); | |
1072 | print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags); | |
1073 | ||
1074 | /* print other classes: 'dedup', 'special', and 'log' */ | |
c24fa4b1 | 1075 | if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_DEDUP)) { |
1076 | print_vdev_tree(zhp, "dedup", poolnvroot, 0, | |
1077 | VDEV_ALLOC_BIAS_DEDUP, name_flags); | |
1078 | print_vdev_tree(zhp, NULL, nvroot, 0, | |
1079 | VDEV_ALLOC_BIAS_DEDUP, name_flags); | |
1080 | } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_DEDUP)) { | |
1081 | print_vdev_tree(zhp, "dedup", nvroot, 0, | |
1082 | VDEV_ALLOC_BIAS_DEDUP, name_flags); | |
1083 | } | |
cc99f275 | 1084 | |
c24fa4b1 | 1085 | if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_SPECIAL)) { |
1086 | print_vdev_tree(zhp, "special", poolnvroot, 0, | |
1087 | VDEV_ALLOC_BIAS_SPECIAL, name_flags); | |
1088 | print_vdev_tree(zhp, NULL, nvroot, 0, | |
1089 | VDEV_ALLOC_BIAS_SPECIAL, name_flags); | |
1090 | } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_SPECIAL)) { | |
1091 | print_vdev_tree(zhp, "special", nvroot, 0, | |
1092 | VDEV_ALLOC_BIAS_SPECIAL, name_flags); | |
1093 | } | |
cc99f275 | 1094 | |
c24fa4b1 | 1095 | if (num_logs(poolnvroot) > 0) { |
1096 | print_vdev_tree(zhp, "logs", poolnvroot, 0, | |
1097 | VDEV_ALLOC_BIAS_LOG, name_flags); | |
1098 | print_vdev_tree(zhp, NULL, nvroot, 0, | |
1099 | VDEV_ALLOC_BIAS_LOG, name_flags); | |
1100 | } else if (num_logs(nvroot) > 0) { | |
1101 | print_vdev_tree(zhp, "logs", nvroot, 0, | |
1102 | VDEV_ALLOC_BIAS_LOG, name_flags); | |
1103 | } | |
34dc7c2f | 1104 | |
e02b533e TC |
1105 | /* Do the same for the caches */ |
1106 | if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE, | |
1107 | &l2child, &l2children) == 0 && l2children) { | |
1108 | hadcache = B_TRUE; | |
1109 | (void) printf(gettext("\tcache\n")); | |
1110 | for (c = 0; c < l2children; c++) { | |
1111 | vname = zpool_vdev_name(g_zfs, NULL, | |
d2f3e292 | 1112 | l2child[c], name_flags); |
e02b533e TC |
1113 | (void) printf("\t %s\n", vname); |
1114 | free(vname); | |
1115 | } | |
1116 | } | |
1117 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
1118 | &l2child, &l2children) == 0 && l2children) { | |
1119 | if (!hadcache) | |
1120 | (void) printf(gettext("\tcache\n")); | |
1121 | for (c = 0; c < l2children; c++) { | |
1122 | vname = zpool_vdev_name(g_zfs, NULL, | |
d2f3e292 | 1123 | l2child[c], name_flags); |
e02b533e TC |
1124 | (void) printf("\t %s\n", vname); |
1125 | free(vname); | |
1126 | } | |
1127 | } | |
bf169e9f | 1128 | /* And finally the spares */ |
0cb40fa3 AF |
1129 | if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES, |
1130 | &sparechild, &sparechildren) == 0 && sparechildren > 0) { | |
1131 | hadspare = B_TRUE; | |
1132 | (void) printf(gettext("\tspares\n")); | |
1133 | for (c = 0; c < sparechildren; c++) { | |
1134 | vname = zpool_vdev_name(g_zfs, NULL, | |
1135 | sparechild[c], name_flags); | |
1136 | (void) printf("\t %s\n", vname); | |
1137 | free(vname); | |
1138 | } | |
1139 | } | |
1140 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
1141 | &sparechild, &sparechildren) == 0 && sparechildren > 0) { | |
1142 | if (!hadspare) | |
1143 | (void) printf(gettext("\tspares\n")); | |
1144 | for (c = 0; c < sparechildren; c++) { | |
1145 | vname = zpool_vdev_name(g_zfs, NULL, | |
1146 | sparechild[c], name_flags); | |
1147 | (void) printf("\t %s\n", vname); | |
1148 | free(vname); | |
1149 | } | |
1150 | } | |
e02b533e | 1151 | |
34dc7c2f BB |
1152 | ret = 0; |
1153 | } else { | |
1154 | ret = (zpool_add(zhp, nvroot) != 0); | |
1155 | } | |
1156 | ||
df831108 | 1157 | nvlist_free(props); |
34dc7c2f BB |
1158 | nvlist_free(nvroot); |
1159 | zpool_close(zhp); | |
1160 | ||
1161 | return (ret); | |
1162 | } | |
1163 | ||
1164 | /* | |
e60e158e | 1165 | * zpool remove [-npsw] <pool> <vdev> ... |
34dc7c2f | 1166 | * |
a1d477c2 | 1167 | * Removes the given vdev from the pool. |
34dc7c2f BB |
1168 | */ |
1169 | int | |
1170 | zpool_do_remove(int argc, char **argv) | |
1171 | { | |
1172 | char *poolname; | |
1173 | int i, ret = 0; | |
884385a0 | 1174 | zpool_handle_t *zhp = NULL; |
a1d477c2 | 1175 | boolean_t stop = B_FALSE; |
0869b74a | 1176 | int c; |
a1d477c2 MA |
1177 | boolean_t noop = B_FALSE; |
1178 | boolean_t parsable = B_FALSE; | |
e60e158e | 1179 | boolean_t wait = B_FALSE; |
34dc7c2f | 1180 | |
a1d477c2 | 1181 | /* check options */ |
e60e158e | 1182 | while ((c = getopt(argc, argv, "npsw")) != -1) { |
a1d477c2 MA |
1183 | switch (c) { |
1184 | case 'n': | |
1185 | noop = B_TRUE; | |
1186 | break; | |
1187 | case 'p': | |
1188 | parsable = B_TRUE; | |
1189 | break; | |
1190 | case 's': | |
1191 | stop = B_TRUE; | |
1192 | break; | |
e60e158e JG |
1193 | case 'w': |
1194 | wait = B_TRUE; | |
1195 | break; | |
a1d477c2 MA |
1196 | case '?': |
1197 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1198 | optopt); | |
1199 | usage(B_FALSE); | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | argc -= optind; | |
1204 | argv += optind; | |
34dc7c2f BB |
1205 | |
1206 | /* get pool name and check number of arguments */ | |
1207 | if (argc < 1) { | |
1208 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
1209 | usage(B_FALSE); | |
1210 | } | |
34dc7c2f BB |
1211 | |
1212 | poolname = argv[0]; | |
1213 | ||
1214 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
1215 | return (1); | |
1216 | ||
a1d477c2 | 1217 | if (stop && noop) { |
ebe1d036 | 1218 | zpool_close(zhp); |
a1d477c2 MA |
1219 | (void) fprintf(stderr, gettext("stop request ignored\n")); |
1220 | return (0); | |
1221 | } | |
1222 | ||
1223 | if (stop) { | |
1224 | if (argc > 1) { | |
1225 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
1226 | usage(B_FALSE); | |
1227 | } | |
1228 | if (zpool_vdev_remove_cancel(zhp) != 0) | |
34dc7c2f | 1229 | ret = 1; |
e60e158e JG |
1230 | if (wait) { |
1231 | (void) fprintf(stderr, gettext("invalid option " | |
1232 | "combination: -w cannot be used with -s\n")); | |
1233 | usage(B_FALSE); | |
1234 | } | |
a1d477c2 MA |
1235 | } else { |
1236 | if (argc < 2) { | |
1237 | (void) fprintf(stderr, gettext("missing device\n")); | |
1238 | usage(B_FALSE); | |
1239 | } | |
1240 | ||
1241 | for (i = 1; i < argc; i++) { | |
1242 | if (noop) { | |
1243 | uint64_t size; | |
1244 | ||
1245 | if (zpool_vdev_indirect_size(zhp, argv[i], | |
1246 | &size) != 0) { | |
1247 | ret = 1; | |
1248 | break; | |
1249 | } | |
1250 | if (parsable) { | |
1251 | (void) printf("%s %llu\n", | |
1252 | argv[i], (unsigned long long)size); | |
1253 | } else { | |
1254 | char valstr[32]; | |
1255 | zfs_nicenum(size, valstr, | |
1256 | sizeof (valstr)); | |
1257 | (void) printf("Memory that will be " | |
1258 | "used after removing %s: %s\n", | |
1259 | argv[i], valstr); | |
1260 | } | |
1261 | } else { | |
1262 | if (zpool_vdev_remove(zhp, argv[i]) != 0) | |
1263 | ret = 1; | |
1264 | } | |
1265 | } | |
e60e158e JG |
1266 | |
1267 | if (ret == 0 && wait) | |
1268 | ret = zpool_wait(zhp, ZPOOL_WAIT_REMOVE); | |
34dc7c2f | 1269 | } |
884385a0 | 1270 | zpool_close(zhp); |
34dc7c2f BB |
1271 | |
1272 | return (ret); | |
1273 | } | |
1274 | ||
4dc11532 TH |
1275 | /* |
1276 | * Return 1 if a vdev is active (being used in a pool) | |
1277 | * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool) | |
1278 | * | |
1279 | * This is useful for checking if a disk in an active pool is offlined or | |
1280 | * faulted. | |
1281 | */ | |
1282 | static int | |
1283 | vdev_is_active(char *vdev_path) | |
1284 | { | |
1285 | int fd; | |
1286 | fd = open(vdev_path, O_EXCL); | |
1287 | if (fd < 0) { | |
1288 | return (1); /* cant open O_EXCL - disk is active */ | |
1289 | } | |
1290 | ||
1291 | close(fd); | |
1292 | return (0); /* disk is inactive in the pool */ | |
1293 | } | |
1294 | ||
131cc95c | 1295 | /* |
dbb38f66 YP |
1296 | * zpool labelclear [-f] <vdev> |
1297 | * | |
1298 | * -f Force clearing the label for the vdevs which are members of | |
1299 | * the exported or foreign pools. | |
131cc95c DK |
1300 | * |
1301 | * Verifies that the vdev is not active and zeros out the label information | |
1302 | * on the device. | |
1303 | */ | |
1304 | int | |
1305 | zpool_do_labelclear(int argc, char **argv) | |
1306 | { | |
dbb38f66 YP |
1307 | char vdev[MAXPATHLEN]; |
1308 | char *name = NULL; | |
131cc95c | 1309 | int c, fd = -1, ret = 0; |
dbb38f66 | 1310 | nvlist_t *config; |
131cc95c DK |
1311 | pool_state_t state; |
1312 | boolean_t inuse = B_FALSE; | |
1313 | boolean_t force = B_FALSE; | |
1314 | ||
1315 | /* check options */ | |
1316 | while ((c = getopt(argc, argv, "f")) != -1) { | |
1317 | switch (c) { | |
1318 | case 'f': | |
1319 | force = B_TRUE; | |
1320 | break; | |
1321 | default: | |
1322 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1323 | optopt); | |
1324 | usage(B_FALSE); | |
1325 | } | |
1326 | } | |
1327 | ||
1328 | argc -= optind; | |
1329 | argv += optind; | |
1330 | ||
1331 | /* get vdev name */ | |
1332 | if (argc < 1) { | |
dbb38f66 YP |
1333 | (void) fprintf(stderr, gettext("missing vdev name\n")); |
1334 | usage(B_FALSE); | |
1335 | } | |
1336 | if (argc > 1) { | |
1337 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
131cc95c DK |
1338 | usage(B_FALSE); |
1339 | } | |
1340 | ||
9368b387 RY |
1341 | (void) strlcpy(vdev, argv[0], sizeof (vdev)); |
1342 | ||
dbb38f66 | 1343 | /* |
9368b387 | 1344 | * If we cannot open an absolute path, we quit. |
dbb38f66 YP |
1345 | * Otherwise if the provided vdev name doesn't point to a file, |
1346 | * try prepending expected disk paths and partition numbers. | |
1347 | */ | |
9368b387 | 1348 | if ((fd = open(vdev, O_RDWR)) < 0) { |
dbb38f66 | 1349 | int error; |
9368b387 RY |
1350 | if (vdev[0] == '/') { |
1351 | (void) fprintf(stderr, gettext("failed to open " | |
1352 | "%s: %s\n"), vdev, strerror(errno)); | |
1353 | return (1); | |
1354 | } | |
dbb38f66 YP |
1355 | |
1356 | error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN); | |
1357 | if (error == 0 && zfs_dev_is_whole_disk(vdev)) { | |
1358 | if (zfs_append_partition(vdev, MAXPATHLEN) == -1) | |
1359 | error = ENOENT; | |
1360 | } | |
1361 | ||
9368b387 RY |
1362 | if (error || ((fd = open(vdev, O_RDWR)) < 0)) { |
1363 | if (errno == ENOENT) { | |
1364 | (void) fprintf(stderr, gettext( | |
1365 | "failed to find device %s, try " | |
1366 | "specifying absolute path instead\n"), | |
1367 | argv[0]); | |
1368 | return (1); | |
1369 | } | |
1370 | ||
1371 | (void) fprintf(stderr, gettext("failed to open %s:" | |
1372 | " %s\n"), vdev, strerror(errno)); | |
dbb38f66 YP |
1373 | return (1); |
1374 | } | |
131cc95c DK |
1375 | } |
1376 | ||
066da71e BB |
1377 | /* |
1378 | * Flush all dirty pages for the block device. This should not be | |
1379 | * fatal when the device does not support BLKFLSBUF as would be the | |
1380 | * case for a file vdev. | |
1381 | */ | |
d46f0deb | 1382 | if ((zfs_dev_flush(fd) != 0) && (errno != ENOTTY)) |
a167aa7c GDN |
1383 | (void) fprintf(stderr, gettext("failed to invalidate " |
1384 | "cache for %s: %s\n"), vdev, strerror(errno)); | |
1385 | ||
066da71e | 1386 | if (zpool_read_label(fd, &config, NULL) != 0) { |
131cc95c | 1387 | (void) fprintf(stderr, |
066da71e | 1388 | gettext("failed to read label from %s\n"), vdev); |
c721ba43 TH |
1389 | ret = 1; |
1390 | goto errout; | |
dbb38f66 YP |
1391 | } |
1392 | nvlist_free(config); | |
131cc95c | 1393 | |
dbb38f66 YP |
1394 | ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse); |
1395 | if (ret != 0) { | |
1396 | (void) fprintf(stderr, | |
1397 | gettext("failed to check state for %s\n"), vdev); | |
c721ba43 TH |
1398 | ret = 1; |
1399 | goto errout; | |
131cc95c DK |
1400 | } |
1401 | ||
dbb38f66 YP |
1402 | if (!inuse) |
1403 | goto wipe_label; | |
1404 | ||
1405 | switch (state) { | |
1406 | default: | |
1407 | case POOL_STATE_ACTIVE: | |
1408 | case POOL_STATE_SPARE: | |
1409 | case POOL_STATE_L2CACHE: | |
4dc11532 TH |
1410 | /* |
1411 | * We allow the user to call 'zpool offline -f' | |
1412 | * on an offlined disk in an active pool. We can check if | |
1413 | * the disk is online by calling vdev_is_active(). | |
1414 | */ | |
1415 | if (force && !vdev_is_active(vdev)) | |
1416 | break; | |
1417 | ||
dbb38f66 | 1418 | (void) fprintf(stderr, gettext( |
4dc11532 | 1419 | "%s is a member (%s) of pool \"%s\""), |
dbb38f66 | 1420 | vdev, zpool_pool_state_to_name(state), name); |
4dc11532 TH |
1421 | |
1422 | if (force) { | |
1423 | (void) fprintf(stderr, gettext( | |
1424 | ". Offline the disk first to clear its label.")); | |
1425 | } | |
1426 | printf("\n"); | |
dbb38f66 YP |
1427 | ret = 1; |
1428 | goto errout; | |
131cc95c | 1429 | |
dbb38f66 YP |
1430 | case POOL_STATE_EXPORTED: |
1431 | if (force) | |
131cc95c | 1432 | break; |
dbb38f66 YP |
1433 | (void) fprintf(stderr, gettext( |
1434 | "use '-f' to override the following error:\n" | |
1435 | "%s is a member of exported pool \"%s\"\n"), | |
1436 | vdev, name); | |
1437 | ret = 1; | |
1438 | goto errout; | |
1439 | ||
1440 | case POOL_STATE_POTENTIALLY_ACTIVE: | |
1441 | if (force) | |
1442 | break; | |
1443 | (void) fprintf(stderr, gettext( | |
1444 | "use '-f' to override the following error:\n" | |
1445 | "%s is a member of potentially active pool \"%s\"\n"), | |
1446 | vdev, name); | |
1447 | ret = 1; | |
1448 | goto errout; | |
1449 | ||
1450 | case POOL_STATE_DESTROYED: | |
1451 | /* inuse should never be set for a destroyed pool */ | |
1452 | assert(0); | |
1453 | break; | |
131cc95c DK |
1454 | } |
1455 | ||
1456 | wipe_label: | |
dbb38f66 YP |
1457 | ret = zpool_clear_label(fd); |
1458 | if (ret != 0) { | |
131cc95c | 1459 | (void) fprintf(stderr, |
dbb38f66 | 1460 | gettext("failed to clear label for %s\n"), vdev); |
131cc95c DK |
1461 | } |
1462 | ||
1463 | errout: | |
dbb38f66 YP |
1464 | free(name); |
1465 | (void) close(fd); | |
131cc95c DK |
1466 | |
1467 | return (ret); | |
1468 | } | |
1469 | ||
34dc7c2f | 1470 | /* |
9ae529ec | 1471 | * zpool create [-fnd] [-o property=value] ... |
b128c09f BB |
1472 | * [-O file-system-property=value] ... |
1473 | * [-R root] [-m mountpoint] <pool> <dev> ... | |
34dc7c2f BB |
1474 | * |
1475 | * -f Force creation, even if devices appear in use | |
1476 | * -n Do not create the pool, but display the resulting layout if it | |
1477 | * were to be created. | |
1478 | * -R Create a pool under an alternate root | |
1479 | * -m Set default mountpoint for the root dataset. By default it's | |
9ae529ec | 1480 | * '/<pool>' |
34dc7c2f | 1481 | * -o Set property=value. |
e4010f27 | 1482 | * -o Set feature@feature=enabled|disabled. |
9ae529ec CS |
1483 | * -d Don't automatically enable all supported pool features |
1484 | * (individual features can be enabled with -o). | |
b128c09f | 1485 | * -O Set fsproperty=value in the pool's root file system |
34dc7c2f BB |
1486 | * |
1487 | * Creates the named pool according to the given vdev specification. The | |
227d3793 RM |
1488 | * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c. |
1489 | * Once we get the nvlist back from make_root_vdev(), we either print out the | |
1490 | * contents (if '-n' was specified), or pass it to libzfs to do the creation. | |
34dc7c2f BB |
1491 | */ |
1492 | int | |
1493 | zpool_do_create(int argc, char **argv) | |
1494 | { | |
1495 | boolean_t force = B_FALSE; | |
1496 | boolean_t dryrun = B_FALSE; | |
658fb802 CB |
1497 | boolean_t enable_pool_features = B_TRUE; |
1498 | ||
34dc7c2f BB |
1499 | int c; |
1500 | nvlist_t *nvroot = NULL; | |
1501 | char *poolname; | |
023bbe6f | 1502 | char *tname = NULL; |
34dc7c2f BB |
1503 | int ret = 1; |
1504 | char *altroot = NULL; | |
658fb802 | 1505 | char *compat = NULL; |
34dc7c2f | 1506 | char *mountpoint = NULL; |
b128c09f | 1507 | nvlist_t *fsprops = NULL; |
34dc7c2f BB |
1508 | nvlist_t *props = NULL; |
1509 | char *propval; | |
1510 | ||
1511 | /* check options */ | |
83e9986f | 1512 | while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) { |
34dc7c2f BB |
1513 | switch (c) { |
1514 | case 'f': | |
1515 | force = B_TRUE; | |
1516 | break; | |
1517 | case 'n': | |
1518 | dryrun = B_TRUE; | |
1519 | break; | |
9ae529ec | 1520 | case 'd': |
658fb802 | 1521 | enable_pool_features = B_FALSE; |
9ae529ec | 1522 | break; |
34dc7c2f BB |
1523 | case 'R': |
1524 | altroot = optarg; | |
1525 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 1526 | ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) |
34dc7c2f | 1527 | goto errout; |
2f3ec900 | 1528 | if (add_prop_list_default(zpool_prop_to_name( |
e40ca391 | 1529 | ZPOOL_PROP_CACHEFILE), "none", &props)) |
34dc7c2f BB |
1530 | goto errout; |
1531 | break; | |
1532 | case 'm': | |
7bc7f250 | 1533 | /* Equivalent to -O mountpoint=optarg */ |
34dc7c2f BB |
1534 | mountpoint = optarg; |
1535 | break; | |
1536 | case 'o': | |
1537 | if ((propval = strchr(optarg, '=')) == NULL) { | |
1538 | (void) fprintf(stderr, gettext("missing " | |
1539 | "'=' for -o option\n")); | |
1540 | goto errout; | |
1541 | } | |
1542 | *propval = '\0'; | |
1543 | propval++; | |
1544 | ||
b128c09f BB |
1545 | if (add_prop_list(optarg, propval, &props, B_TRUE)) |
1546 | goto errout; | |
9ae529ec CS |
1547 | |
1548 | /* | |
1549 | * If the user is creating a pool that doesn't support | |
1550 | * feature flags, don't enable any features. | |
1551 | */ | |
1552 | if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { | |
1553 | char *end; | |
1554 | u_longlong_t ver; | |
1555 | ||
1556 | ver = strtoull(propval, &end, 10); | |
1557 | if (*end == '\0' && | |
1558 | ver < SPA_VERSION_FEATURES) { | |
658fb802 | 1559 | enable_pool_features = B_FALSE; |
9ae529ec CS |
1560 | } |
1561 | } | |
3ac2794c BB |
1562 | if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT) |
1563 | altroot = propval; | |
658fb802 CB |
1564 | if (zpool_name_to_prop(optarg) == |
1565 | ZPOOL_PROP_COMPATIBILITY) | |
1566 | compat = propval; | |
b128c09f BB |
1567 | break; |
1568 | case 'O': | |
1569 | if ((propval = strchr(optarg, '=')) == NULL) { | |
1570 | (void) fprintf(stderr, gettext("missing " | |
1571 | "'=' for -O option\n")); | |
1572 | goto errout; | |
1573 | } | |
1574 | *propval = '\0'; | |
1575 | propval++; | |
1576 | ||
7bc7f250 WA |
1577 | /* |
1578 | * Mountpoints are checked and then added later. | |
1579 | * Uniquely among properties, they can be specified | |
1580 | * more than once, to avoid conflict with -m. | |
1581 | */ | |
1582 | if (0 == strcmp(optarg, | |
1583 | zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) { | |
1584 | mountpoint = propval; | |
1585 | } else if (add_prop_list(optarg, propval, &fsprops, | |
1586 | B_FALSE)) { | |
34dc7c2f | 1587 | goto errout; |
7bc7f250 | 1588 | } |
34dc7c2f | 1589 | break; |
83e9986f RY |
1590 | case 't': |
1591 | /* | |
1592 | * Sanity check temporary pool name. | |
1593 | */ | |
1594 | if (strchr(optarg, '/') != NULL) { | |
1595 | (void) fprintf(stderr, gettext("cannot create " | |
1596 | "'%s': invalid character '/' in temporary " | |
1597 | "name\n"), optarg); | |
1598 | (void) fprintf(stderr, gettext("use 'zfs " | |
1599 | "create' to create a dataset\n")); | |
1600 | goto errout; | |
1601 | } | |
1602 | ||
1603 | if (add_prop_list(zpool_prop_to_name( | |
1604 | ZPOOL_PROP_TNAME), optarg, &props, B_TRUE)) | |
1605 | goto errout; | |
1606 | if (add_prop_list_default(zpool_prop_to_name( | |
e40ca391 | 1607 | ZPOOL_PROP_CACHEFILE), "none", &props)) |
83e9986f | 1608 | goto errout; |
023bbe6f | 1609 | tname = optarg; |
83e9986f | 1610 | break; |
34dc7c2f BB |
1611 | case ':': |
1612 | (void) fprintf(stderr, gettext("missing argument for " | |
1613 | "'%c' option\n"), optopt); | |
1614 | goto badusage; | |
1615 | case '?': | |
1616 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1617 | optopt); | |
1618 | goto badusage; | |
1619 | } | |
1620 | } | |
1621 | ||
1622 | argc -= optind; | |
1623 | argv += optind; | |
1624 | ||
1625 | /* get pool name and check number of arguments */ | |
1626 | if (argc < 1) { | |
1627 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
1628 | goto badusage; | |
1629 | } | |
1630 | if (argc < 2) { | |
1631 | (void) fprintf(stderr, gettext("missing vdev specification\n")); | |
1632 | goto badusage; | |
1633 | } | |
1634 | ||
1635 | poolname = argv[0]; | |
1636 | ||
1637 | /* | |
1638 | * As a special case, check for use of '/' in the name, and direct the | |
1639 | * user to use 'zfs create' instead. | |
1640 | */ | |
1641 | if (strchr(poolname, '/') != NULL) { | |
1642 | (void) fprintf(stderr, gettext("cannot create '%s': invalid " | |
1643 | "character '/' in pool name\n"), poolname); | |
1644 | (void) fprintf(stderr, gettext("use 'zfs create' to " | |
1645 | "create a dataset\n")); | |
1646 | goto errout; | |
1647 | } | |
1648 | ||
227d3793 | 1649 | /* pass off to make_root_vdev for bulk processing */ |
df30f566 | 1650 | nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun, |
b128c09f | 1651 | argc - 1, argv + 1); |
34dc7c2f | 1652 | if (nvroot == NULL) |
b128c09f | 1653 | goto errout; |
34dc7c2f BB |
1654 | |
1655 | /* make_root_vdev() allows 0 toplevel children if there are spares */ | |
1656 | if (!zfs_allocatable_devs(nvroot)) { | |
1657 | (void) fprintf(stderr, gettext("invalid vdev " | |
1658 | "specification: at least one toplevel vdev must be " | |
1659 | "specified\n")); | |
1660 | goto errout; | |
1661 | } | |
1662 | ||
34dc7c2f BB |
1663 | if (altroot != NULL && altroot[0] != '/') { |
1664 | (void) fprintf(stderr, gettext("invalid alternate root '%s': " | |
1665 | "must be an absolute path\n"), altroot); | |
1666 | goto errout; | |
1667 | } | |
1668 | ||
1669 | /* | |
1670 | * Check the validity of the mountpoint and direct the user to use the | |
1671 | * '-m' mountpoint option if it looks like its in use. | |
1672 | */ | |
1673 | if (mountpoint == NULL || | |
1674 | (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && | |
1675 | strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) { | |
1676 | char buf[MAXPATHLEN]; | |
1677 | DIR *dirp; | |
1678 | ||
1679 | if (mountpoint && mountpoint[0] != '/') { | |
1680 | (void) fprintf(stderr, gettext("invalid mountpoint " | |
1681 | "'%s': must be an absolute path, 'legacy', or " | |
1682 | "'none'\n"), mountpoint); | |
1683 | goto errout; | |
1684 | } | |
1685 | ||
1686 | if (mountpoint == NULL) { | |
1687 | if (altroot != NULL) | |
1688 | (void) snprintf(buf, sizeof (buf), "%s/%s", | |
1689 | altroot, poolname); | |
1690 | else | |
1691 | (void) snprintf(buf, sizeof (buf), "/%s", | |
1692 | poolname); | |
1693 | } else { | |
1694 | if (altroot != NULL) | |
1695 | (void) snprintf(buf, sizeof (buf), "%s%s", | |
1696 | altroot, mountpoint); | |
1697 | else | |
1698 | (void) snprintf(buf, sizeof (buf), "%s", | |
1699 | mountpoint); | |
1700 | } | |
1701 | ||
1702 | if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { | |
1703 | (void) fprintf(stderr, gettext("mountpoint '%s' : " | |
1704 | "%s\n"), buf, strerror(errno)); | |
1705 | (void) fprintf(stderr, gettext("use '-m' " | |
1706 | "option to provide a different default\n")); | |
1707 | goto errout; | |
1708 | } else if (dirp) { | |
1709 | int count = 0; | |
1710 | ||
1711 | while (count < 3 && readdir(dirp) != NULL) | |
1712 | count++; | |
1713 | (void) closedir(dirp); | |
1714 | ||
1715 | if (count > 2) { | |
1716 | (void) fprintf(stderr, gettext("mountpoint " | |
1717 | "'%s' exists and is not empty\n"), buf); | |
1718 | (void) fprintf(stderr, gettext("use '-m' " | |
1719 | "option to provide a " | |
1720 | "different default\n")); | |
1721 | goto errout; | |
1722 | } | |
1723 | } | |
1724 | } | |
1725 | ||
7bc7f250 WA |
1726 | /* |
1727 | * Now that the mountpoint's validity has been checked, ensure that | |
1728 | * the property is set appropriately prior to creating the pool. | |
1729 | */ | |
1730 | if (mountpoint != NULL) { | |
1731 | ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), | |
1732 | mountpoint, &fsprops, B_FALSE); | |
1733 | if (ret != 0) | |
1734 | goto errout; | |
1735 | } | |
1736 | ||
1737 | ret = 1; | |
34dc7c2f BB |
1738 | if (dryrun) { |
1739 | /* | |
1740 | * For a dry run invocation, print out a basic message and run | |
1741 | * through all the vdevs in the list and print out in an | |
1742 | * appropriate hierarchy. | |
1743 | */ | |
1744 | (void) printf(gettext("would create '%s' with the " | |
1745 | "following layout:\n\n"), poolname); | |
1746 | ||
cc99f275 DB |
1747 | print_vdev_tree(NULL, poolname, nvroot, 0, "", 0); |
1748 | print_vdev_tree(NULL, "dedup", nvroot, 0, | |
1749 | VDEV_ALLOC_BIAS_DEDUP, 0); | |
1750 | print_vdev_tree(NULL, "special", nvroot, 0, | |
1751 | VDEV_ALLOC_BIAS_SPECIAL, 0); | |
1752 | print_vdev_tree(NULL, "logs", nvroot, 0, | |
1753 | VDEV_ALLOC_BIAS_LOG, 0); | |
0cb40fa3 AF |
1754 | print_cache_list(nvroot, 0); |
1755 | print_spare_list(nvroot, 0); | |
34dc7c2f BB |
1756 | |
1757 | ret = 0; | |
1758 | } else { | |
1759 | /* | |
658fb802 CB |
1760 | * Load in feature set. |
1761 | * Note: if compatibility property not given, we'll have | |
1762 | * NULL, which means 'all features'. | |
34dc7c2f | 1763 | */ |
658fb802 CB |
1764 | boolean_t requested_features[SPA_FEATURES]; |
1765 | if (zpool_do_load_compat(compat, requested_features) != | |
1766 | ZPOOL_COMPATIBILITY_OK) | |
1767 | goto errout; | |
1768 | ||
1769 | /* | |
1770 | * props contains list of features to enable. | |
1771 | * For each feature: | |
1772 | * - remove it if feature@name=disabled | |
1773 | * - leave it there if feature@name=enabled | |
1774 | * - add it if: | |
1775 | * - enable_pool_features (ie: no '-d' or '-o version') | |
1776 | * - it's supported by the kernel module | |
1777 | * - it's in the requested feature set | |
e086db16 | 1778 | * - warn if it's enabled but not in compat |
658fb802 CB |
1779 | */ |
1780 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { | |
e4010f27 | 1781 | char propname[MAXPATHLEN]; |
d1807f16 | 1782 | const char *propval; |
e4010f27 | 1783 | zfeature_info_t *feat = &spa_feature_table[i]; |
9ae529ec | 1784 | |
e4010f27 | 1785 | (void) snprintf(propname, sizeof (propname), |
1786 | "feature@%s", feat->fi_uname); | |
1787 | ||
e4010f27 | 1788 | if (!nvlist_lookup_string(props, propname, &propval)) { |
63652e15 DS |
1789 | if (strcmp(propval, |
1790 | ZFS_FEATURE_DISABLED) == 0) { | |
e4010f27 | 1791 | (void) nvlist_remove_all(props, |
1792 | propname); | |
63652e15 | 1793 | } else if (strcmp(propval, |
e086db16 | 1794 | ZFS_FEATURE_ENABLED) == 0 && |
63652e15 | 1795 | !requested_features[i]) { |
e086db16 CB |
1796 | (void) fprintf(stderr, gettext( |
1797 | "Warning: feature \"%s\" enabled " | |
1798 | "but is not in specified " | |
1799 | "'compatibility' feature set.\n"), | |
1800 | feat->fi_uname); | |
63652e15 | 1801 | } |
658fb802 CB |
1802 | } else if ( |
1803 | enable_pool_features && | |
1804 | feat->fi_zfs_mod_supported && | |
1805 | requested_features[i]) { | |
7bc7f250 WA |
1806 | ret = add_prop_list(propname, |
1807 | ZFS_FEATURE_ENABLED, &props, B_TRUE); | |
1808 | if (ret != 0) | |
9ae529ec CS |
1809 | goto errout; |
1810 | } | |
1811 | } | |
7bc7f250 WA |
1812 | |
1813 | ret = 1; | |
b128c09f BB |
1814 | if (zpool_create(g_zfs, poolname, |
1815 | nvroot, props, fsprops) == 0) { | |
023bbe6f | 1816 | zfs_handle_t *pool = zfs_open(g_zfs, |
1817 | tname ? tname : poolname, ZFS_TYPE_FILESYSTEM); | |
34dc7c2f | 1818 | if (pool != NULL) { |
c15d36c6 | 1819 | if (zfs_mount(pool, NULL, 0) == 0) { |
b4d9a82f AZ |
1820 | ret = zfs_share(pool, NULL); |
1821 | zfs_commit_shares(NULL); | |
c15d36c6 | 1822 | } |
34dc7c2f BB |
1823 | zfs_close(pool); |
1824 | } | |
1825 | } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { | |
1826 | (void) fprintf(stderr, gettext("pool name may have " | |
1827 | "been omitted\n")); | |
1828 | } | |
1829 | } | |
1830 | ||
1831 | errout: | |
1832 | nvlist_free(nvroot); | |
b128c09f | 1833 | nvlist_free(fsprops); |
34dc7c2f BB |
1834 | nvlist_free(props); |
1835 | return (ret); | |
1836 | badusage: | |
b128c09f | 1837 | nvlist_free(fsprops); |
34dc7c2f BB |
1838 | nvlist_free(props); |
1839 | usage(B_FALSE); | |
1840 | return (2); | |
1841 | } | |
1842 | ||
1843 | /* | |
1844 | * zpool destroy <pool> | |
1845 | * | |
1846 | * -f Forcefully unmount any datasets | |
1847 | * | |
1848 | * Destroy the given pool. Automatically unmounts any datasets in the pool. | |
1849 | */ | |
1850 | int | |
1851 | zpool_do_destroy(int argc, char **argv) | |
1852 | { | |
1853 | boolean_t force = B_FALSE; | |
1854 | int c; | |
1855 | char *pool; | |
1856 | zpool_handle_t *zhp; | |
1857 | int ret; | |
1858 | ||
1859 | /* check options */ | |
1860 | while ((c = getopt(argc, argv, "f")) != -1) { | |
1861 | switch (c) { | |
1862 | case 'f': | |
1863 | force = B_TRUE; | |
1864 | break; | |
1865 | case '?': | |
1866 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1867 | optopt); | |
1868 | usage(B_FALSE); | |
1869 | } | |
1870 | } | |
1871 | ||
1872 | argc -= optind; | |
1873 | argv += optind; | |
1874 | ||
1875 | /* check arguments */ | |
1876 | if (argc < 1) { | |
1877 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
1878 | usage(B_FALSE); | |
1879 | } | |
1880 | if (argc > 1) { | |
1881 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
1882 | usage(B_FALSE); | |
1883 | } | |
1884 | ||
1885 | pool = argv[0]; | |
1886 | ||
1887 | if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { | |
1888 | /* | |
1889 | * As a special case, check for use of '/' in the name, and | |
1890 | * direct the user to use 'zfs destroy' instead. | |
1891 | */ | |
1892 | if (strchr(pool, '/') != NULL) | |
1893 | (void) fprintf(stderr, gettext("use 'zfs destroy' to " | |
1894 | "destroy a dataset\n")); | |
1895 | return (1); | |
1896 | } | |
1897 | ||
1898 | if (zpool_disable_datasets(zhp, force) != 0) { | |
1899 | (void) fprintf(stderr, gettext("could not destroy '%s': " | |
1900 | "could not unmount datasets\n"), zpool_get_name(zhp)); | |
a425f5bf | 1901 | zpool_close(zhp); |
34dc7c2f BB |
1902 | return (1); |
1903 | } | |
1904 | ||
6f1ffb06 MA |
1905 | /* The history must be logged as part of the export */ |
1906 | log_history = B_FALSE; | |
1907 | ||
1908 | ret = (zpool_destroy(zhp, history_str) != 0); | |
34dc7c2f BB |
1909 | |
1910 | zpool_close(zhp); | |
1911 | ||
1912 | return (ret); | |
1913 | } | |
1914 | ||
859735c0 TF |
1915 | typedef struct export_cbdata { |
1916 | boolean_t force; | |
1917 | boolean_t hardforce; | |
1918 | } export_cbdata_t; | |
1919 | ||
1920 | /* | |
1921 | * Export one pool | |
1922 | */ | |
65c7cc49 | 1923 | static int |
859735c0 TF |
1924 | zpool_export_one(zpool_handle_t *zhp, void *data) |
1925 | { | |
1926 | export_cbdata_t *cb = data; | |
1927 | ||
1928 | if (zpool_disable_datasets(zhp, cb->force) != 0) | |
1929 | return (1); | |
1930 | ||
1931 | /* The history must be logged as part of the export */ | |
1932 | log_history = B_FALSE; | |
1933 | ||
1934 | if (cb->hardforce) { | |
1935 | if (zpool_export_force(zhp, history_str) != 0) | |
1936 | return (1); | |
1937 | } else if (zpool_export(zhp, cb->force, history_str) != 0) { | |
1938 | return (1); | |
1939 | } | |
1940 | ||
1941 | return (0); | |
1942 | } | |
1943 | ||
34dc7c2f BB |
1944 | /* |
1945 | * zpool export [-f] <pool> ... | |
1946 | * | |
859735c0 | 1947 | * -a Export all pools |
34dc7c2f BB |
1948 | * -f Forcefully unmount datasets |
1949 | * | |
1950 | * Export the given pools. By default, the command will attempt to cleanly | |
1951 | * unmount any active datasets within the pool. If the '-f' flag is specified, | |
1952 | * then the datasets will be forcefully unmounted. | |
1953 | */ | |
1954 | int | |
1955 | zpool_do_export(int argc, char **argv) | |
1956 | { | |
859735c0 TF |
1957 | export_cbdata_t cb; |
1958 | boolean_t do_all = B_FALSE; | |
34dc7c2f | 1959 | boolean_t force = B_FALSE; |
fb5f0bc8 | 1960 | boolean_t hardforce = B_FALSE; |
859735c0 | 1961 | int c, ret; |
34dc7c2f BB |
1962 | |
1963 | /* check options */ | |
859735c0 | 1964 | while ((c = getopt(argc, argv, "afF")) != -1) { |
34dc7c2f | 1965 | switch (c) { |
859735c0 TF |
1966 | case 'a': |
1967 | do_all = B_TRUE; | |
1968 | break; | |
34dc7c2f BB |
1969 | case 'f': |
1970 | force = B_TRUE; | |
1971 | break; | |
fb5f0bc8 BB |
1972 | case 'F': |
1973 | hardforce = B_TRUE; | |
1974 | break; | |
34dc7c2f BB |
1975 | case '?': |
1976 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1977 | optopt); | |
1978 | usage(B_FALSE); | |
1979 | } | |
1980 | } | |
1981 | ||
859735c0 TF |
1982 | cb.force = force; |
1983 | cb.hardforce = hardforce; | |
34dc7c2f BB |
1984 | argc -= optind; |
1985 | argv += optind; | |
1986 | ||
859735c0 TF |
1987 | if (do_all) { |
1988 | if (argc != 0) { | |
1989 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
1990 | usage(B_FALSE); | |
1991 | } | |
1992 | ||
1993 | return (for_each_pool(argc, argv, B_TRUE, NULL, | |
2a673e76 | 1994 | ZFS_TYPE_POOL, B_FALSE, zpool_export_one, &cb)); |
859735c0 TF |
1995 | } |
1996 | ||
34dc7c2f BB |
1997 | /* check arguments */ |
1998 | if (argc < 1) { | |
1999 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
2000 | usage(B_FALSE); | |
2001 | } | |
2002 | ||
2a673e76 AJ |
2003 | ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
2004 | B_FALSE, zpool_export_one, &cb); | |
34dc7c2f BB |
2005 | |
2006 | return (ret); | |
2007 | } | |
2008 | ||
2009 | /* | |
2010 | * Given a vdev configuration, determine the maximum width needed for the device | |
2011 | * name column. | |
2012 | */ | |
2013 | static int | |
d2f3e292 RY |
2014 | max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max, |
2015 | int name_flags) | |
34dc7c2f | 2016 | { |
e9072c76 AZ |
2017 | static const char *const subtypes[] = |
2018 | {ZPOOL_CONFIG_SPARES, ZPOOL_CONFIG_L2CACHE, ZPOOL_CONFIG_CHILDREN}; | |
34dc7c2f | 2019 | |
e9072c76 AZ |
2020 | char *name = zpool_vdev_name(g_zfs, zhp, nv, name_flags); |
2021 | max = MAX(strlen(name) + depth, max); | |
34dc7c2f BB |
2022 | free(name); |
2023 | ||
e9072c76 AZ |
2024 | nvlist_t **child; |
2025 | uint_t children; | |
2026 | for (size_t i = 0; i < ARRAY_SIZE(subtypes); ++i) | |
2027 | if (nvlist_lookup_nvlist_array(nv, subtypes[i], | |
2028 | &child, &children) == 0) | |
2029 | for (uint_t c = 0; c < children; ++c) | |
2030 | max = MAX(max_width(zhp, child[c], depth + 2, | |
2031 | max, name_flags), max); | |
34dc7c2f | 2032 | |
34dc7c2f BB |
2033 | return (max); |
2034 | } | |
2035 | ||
9babb374 BB |
2036 | typedef struct spare_cbdata { |
2037 | uint64_t cb_guid; | |
2038 | zpool_handle_t *cb_zhp; | |
2039 | } spare_cbdata_t; | |
2040 | ||
2041 | static boolean_t | |
2042 | find_vdev(nvlist_t *nv, uint64_t search) | |
2043 | { | |
2044 | uint64_t guid; | |
2045 | nvlist_t **child; | |
2046 | uint_t c, children; | |
2047 | ||
2048 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && | |
2049 | search == guid) | |
2050 | return (B_TRUE); | |
2051 | ||
2052 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
2053 | &child, &children) == 0) { | |
2054 | for (c = 0; c < children; c++) | |
2055 | if (find_vdev(child[c], search)) | |
2056 | return (B_TRUE); | |
2057 | } | |
2058 | ||
2059 | return (B_FALSE); | |
2060 | } | |
2061 | ||
2062 | static int | |
2063 | find_spare(zpool_handle_t *zhp, void *data) | |
2064 | { | |
2065 | spare_cbdata_t *cbp = data; | |
2066 | nvlist_t *config, *nvroot; | |
2067 | ||
2068 | config = zpool_get_config(zhp, NULL); | |
2069 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
2070 | &nvroot) == 0); | |
2071 | ||
2072 | if (find_vdev(nvroot, cbp->cb_guid)) { | |
2073 | cbp->cb_zhp = zhp; | |
2074 | return (1); | |
2075 | } | |
2076 | ||
2077 | zpool_close(zhp); | |
2078 | return (0); | |
2079 | } | |
2080 | ||
fea33e4e HJ |
2081 | typedef struct status_cbdata { |
2082 | int cb_count; | |
2083 | int cb_name_flags; | |
2084 | int cb_namewidth; | |
2085 | boolean_t cb_allpools; | |
2086 | boolean_t cb_verbose; | |
ad796b8a | 2087 | boolean_t cb_literal; |
fea33e4e HJ |
2088 | boolean_t cb_explain; |
2089 | boolean_t cb_first; | |
2090 | boolean_t cb_dedup_stats; | |
2091 | boolean_t cb_print_status; | |
ad796b8a | 2092 | boolean_t cb_print_slow_ios; |
a769fb53 | 2093 | boolean_t cb_print_vdev_init; |
1b939560 | 2094 | boolean_t cb_print_vdev_trim; |
8720e9e7 | 2095 | vdev_cmd_data_list_t *vcdl; |
fea33e4e HJ |
2096 | } status_cbdata_t; |
2097 | ||
d6418de0 | 2098 | /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */ |
a926aab9 AZ |
2099 | static boolean_t |
2100 | is_blank_str(const char *str) | |
d6418de0 | 2101 | { |
a926aab9 | 2102 | for (; str != NULL && *str != '\0'; ++str) |
d6418de0 | 2103 | if (!isblank(*str)) |
a926aab9 AZ |
2104 | return (B_FALSE); |
2105 | return (B_TRUE); | |
d6418de0 TH |
2106 | } |
2107 | ||
2108 | /* Print command output lines for specific vdev in a specific pool */ | |
8720e9e7 | 2109 | static void |
d1807f16 | 2110 | zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, const char *path) |
8720e9e7 | 2111 | { |
d6418de0 TH |
2112 | vdev_cmd_data_t *data; |
2113 | int i, j; | |
a926aab9 | 2114 | const char *val; |
d6418de0 | 2115 | |
8720e9e7 | 2116 | for (i = 0; i < vcdl->count; i++) { |
d6418de0 TH |
2117 | if ((strcmp(vcdl->data[i].path, path) != 0) || |
2118 | (strcmp(vcdl->data[i].pool, pool) != 0)) { | |
2119 | /* Not the vdev we're looking for */ | |
2120 | continue; | |
8720e9e7 | 2121 | } |
d6418de0 TH |
2122 | |
2123 | data = &vcdl->data[i]; | |
2124 | /* Print out all the output values for this vdev */ | |
2125 | for (j = 0; j < vcdl->uniq_cols_cnt; j++) { | |
2126 | val = NULL; | |
2127 | /* Does this vdev have values for this column? */ | |
2128 | for (int k = 0; k < data->cols_cnt; k++) { | |
2129 | if (strcmp(data->cols[k], | |
2130 | vcdl->uniq_cols[j]) == 0) { | |
2131 | /* yes it does, record the value */ | |
2132 | val = data->lines[k]; | |
2133 | break; | |
2134 | } | |
2135 | } | |
2136 | /* | |
2137 | * Mark empty values with dashes to make output | |
2138 | * awk-able. | |
2139 | */ | |
7454d2bb | 2140 | if (val == NULL || is_blank_str(val)) |
d6418de0 TH |
2141 | val = "-"; |
2142 | ||
2143 | printf("%*s", vcdl->uniq_cols_width[j], val); | |
2144 | if (j < vcdl->uniq_cols_cnt - 1) | |
a926aab9 | 2145 | fputs(" ", stdout); |
d6418de0 TH |
2146 | } |
2147 | ||
2148 | /* Print out any values that aren't in a column at the end */ | |
2149 | for (j = data->cols_cnt; j < data->lines_cnt; j++) { | |
2150 | /* Did we have any columns? If so print a spacer. */ | |
2151 | if (vcdl->uniq_cols_cnt > 0) | |
a926aab9 | 2152 | fputs(" ", stdout); |
d6418de0 TH |
2153 | |
2154 | val = data->lines[j]; | |
a926aab9 | 2155 | fputs(val ?: "", stdout); |
d6418de0 TH |
2156 | } |
2157 | break; | |
8720e9e7 TH |
2158 | } |
2159 | } | |
2160 | ||
1b939560 BB |
2161 | /* |
2162 | * Print vdev initialization status for leaves | |
2163 | */ | |
2164 | static void | |
2165 | print_status_initialize(vdev_stat_t *vs, boolean_t verbose) | |
2166 | { | |
2167 | if (verbose) { | |
2168 | if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE || | |
2169 | vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED || | |
2170 | vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) && | |
2171 | !vs->vs_scan_removing) { | |
2172 | char zbuf[1024]; | |
2173 | char tbuf[256]; | |
2174 | struct tm zaction_ts; | |
2175 | ||
2176 | time_t t = vs->vs_initialize_action_time; | |
2177 | int initialize_pct = 100; | |
2178 | if (vs->vs_initialize_state != | |
2179 | VDEV_INITIALIZE_COMPLETE) { | |
2180 | initialize_pct = (vs->vs_initialize_bytes_done * | |
2181 | 100 / (vs->vs_initialize_bytes_est + 1)); | |
2182 | } | |
2183 | ||
2184 | (void) localtime_r(&t, &zaction_ts); | |
2185 | (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts); | |
2186 | ||
2187 | switch (vs->vs_initialize_state) { | |
2188 | case VDEV_INITIALIZE_SUSPENDED: | |
2189 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2190 | gettext("suspended, started at"), tbuf); | |
2191 | break; | |
2192 | case VDEV_INITIALIZE_ACTIVE: | |
2193 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2194 | gettext("started at"), tbuf); | |
2195 | break; | |
2196 | case VDEV_INITIALIZE_COMPLETE: | |
2197 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2198 | gettext("completed at"), tbuf); | |
2199 | break; | |
2200 | } | |
2201 | ||
2202 | (void) printf(gettext(" (%d%% initialized%s)"), | |
2203 | initialize_pct, zbuf); | |
2204 | } else { | |
2205 | (void) printf(gettext(" (uninitialized)")); | |
2206 | } | |
2207 | } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) { | |
2208 | (void) printf(gettext(" (initializing)")); | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | /* | |
2213 | * Print vdev TRIM status for leaves | |
2214 | */ | |
2215 | static void | |
2216 | print_status_trim(vdev_stat_t *vs, boolean_t verbose) | |
2217 | { | |
2218 | if (verbose) { | |
2219 | if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE || | |
2220 | vs->vs_trim_state == VDEV_TRIM_SUSPENDED || | |
2221 | vs->vs_trim_state == VDEV_TRIM_COMPLETE) && | |
2222 | !vs->vs_scan_removing) { | |
2223 | char zbuf[1024]; | |
2224 | char tbuf[256]; | |
2225 | struct tm zaction_ts; | |
2226 | ||
2227 | time_t t = vs->vs_trim_action_time; | |
2228 | int trim_pct = 100; | |
2229 | if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) { | |
2230 | trim_pct = (vs->vs_trim_bytes_done * | |
2231 | 100 / (vs->vs_trim_bytes_est + 1)); | |
2232 | } | |
2233 | ||
2234 | (void) localtime_r(&t, &zaction_ts); | |
2235 | (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts); | |
2236 | ||
2237 | switch (vs->vs_trim_state) { | |
2238 | case VDEV_TRIM_SUSPENDED: | |
2239 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2240 | gettext("suspended, started at"), tbuf); | |
2241 | break; | |
2242 | case VDEV_TRIM_ACTIVE: | |
2243 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2244 | gettext("started at"), tbuf); | |
2245 | break; | |
2246 | case VDEV_TRIM_COMPLETE: | |
2247 | (void) snprintf(zbuf, sizeof (zbuf), ", %s %s", | |
2248 | gettext("completed at"), tbuf); | |
2249 | break; | |
2250 | } | |
2251 | ||
2252 | (void) printf(gettext(" (%d%% trimmed%s)"), | |
2253 | trim_pct, zbuf); | |
2254 | } else if (vs->vs_trim_notsup) { | |
2255 | (void) printf(gettext(" (trim unsupported)")); | |
2256 | } else { | |
2257 | (void) printf(gettext(" (untrimmed)")); | |
2258 | } | |
2259 | } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) { | |
2260 | (void) printf(gettext(" (trimming)")); | |
2261 | } | |
2262 | } | |
2263 | ||
9fb2771a TH |
2264 | /* |
2265 | * Return the color associated with a health string. This includes returning | |
2266 | * NULL for no color change. | |
2267 | */ | |
a926aab9 | 2268 | static const char * |
9fb2771a TH |
2269 | health_str_to_color(const char *health) |
2270 | { | |
2271 | if (strcmp(health, gettext("FAULTED")) == 0 || | |
2272 | strcmp(health, gettext("SUSPENDED")) == 0 || | |
2273 | strcmp(health, gettext("UNAVAIL")) == 0) { | |
2274 | return (ANSI_RED); | |
2275 | } | |
2276 | ||
2277 | if (strcmp(health, gettext("OFFLINE")) == 0 || | |
2278 | strcmp(health, gettext("DEGRADED")) == 0 || | |
2279 | strcmp(health, gettext("REMOVED")) == 0) { | |
2280 | return (ANSI_YELLOW); | |
2281 | } | |
2282 | ||
2283 | return (NULL); | |
2284 | } | |
2285 | ||
9babb374 BB |
2286 | /* |
2287 | * Print out configuration state as requested by status_callback. | |
2288 | */ | |
d2f3e292 | 2289 | static void |
fea33e4e | 2290 | print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name, |
9a49d3f3 | 2291 | nvlist_t *nv, int depth, boolean_t isspare, vdev_rebuild_stat_t *vrs) |
9babb374 | 2292 | { |
2ef0f8c3 | 2293 | nvlist_t **child, *root; |
6fe3498c | 2294 | uint_t c, i, vsc, children; |
428870ff | 2295 | pool_scan_stat_t *ps = NULL; |
9babb374 | 2296 | vdev_stat_t *vs; |
428870ff | 2297 | char rbuf[6], wbuf[6], cbuf[6]; |
9babb374 BB |
2298 | char *vname; |
2299 | uint64_t notpresent; | |
fea33e4e | 2300 | spare_cbdata_t spare_cb; |
6b8655ad | 2301 | const char *state; |
d1807f16 RY |
2302 | const char *type; |
2303 | const char *path = NULL; | |
a926aab9 | 2304 | const char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL; |
9babb374 | 2305 | |
9babb374 BB |
2306 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
2307 | &child, &children) != 0) | |
2308 | children = 0; | |
2309 | ||
428870ff | 2310 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
6fe3498c | 2311 | (uint64_t **)&vs, &vsc) == 0); |
428870ff | 2312 | |
a1d477c2 MA |
2313 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); |
2314 | ||
2315 | if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
2316 | return; | |
2317 | ||
9babb374 | 2318 | state = zpool_state_to_name(vs->vs_state, vs->vs_aux); |
9fb2771a | 2319 | |
9babb374 BB |
2320 | if (isspare) { |
2321 | /* | |
2322 | * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for | |
2323 | * online drives. | |
2324 | */ | |
2325 | if (vs->vs_aux == VDEV_AUX_SPARED) | |
9fb2771a | 2326 | state = gettext("INUSE"); |
9babb374 | 2327 | else if (vs->vs_state == VDEV_STATE_HEALTHY) |
9fb2771a | 2328 | state = gettext("AVAIL"); |
9babb374 BB |
2329 | } |
2330 | ||
9fb2771a TH |
2331 | printf_color(health_str_to_color(state), |
2332 | "\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth, | |
9babb374 BB |
2333 | name, state); |
2334 | ||
2335 | if (!isspare) { | |
9fb2771a TH |
2336 | if (vs->vs_read_errors) |
2337 | rcolor = ANSI_RED; | |
2338 | ||
2339 | if (vs->vs_write_errors) | |
2340 | wcolor = ANSI_RED; | |
2341 | ||
2342 | if (vs->vs_checksum_errors) | |
2343 | ccolor = ANSI_RED; | |
2344 | ||
ad796b8a | 2345 | if (cb->cb_literal) { |
a926aab9 | 2346 | fputc(' ', stdout); |
9fb2771a TH |
2347 | printf_color(rcolor, "%5llu", |
2348 | (u_longlong_t)vs->vs_read_errors); | |
a926aab9 | 2349 | fputc(' ', stdout); |
9fb2771a TH |
2350 | printf_color(wcolor, "%5llu", |
2351 | (u_longlong_t)vs->vs_write_errors); | |
a926aab9 | 2352 | fputc(' ', stdout); |
9fb2771a | 2353 | printf_color(ccolor, "%5llu", |
ad796b8a TH |
2354 | (u_longlong_t)vs->vs_checksum_errors); |
2355 | } else { | |
2356 | zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); | |
2357 | zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); | |
2358 | zfs_nicenum(vs->vs_checksum_errors, cbuf, | |
2359 | sizeof (cbuf)); | |
a926aab9 | 2360 | fputc(' ', stdout); |
9fb2771a | 2361 | printf_color(rcolor, "%5s", rbuf); |
a926aab9 | 2362 | fputc(' ', stdout); |
9fb2771a | 2363 | printf_color(wcolor, "%5s", wbuf); |
a926aab9 | 2364 | fputc(' ', stdout); |
9fb2771a | 2365 | printf_color(ccolor, "%5s", cbuf); |
ad796b8a | 2366 | } |
ad796b8a TH |
2367 | if (cb->cb_print_slow_ios) { |
2368 | if (children == 0) { | |
2369 | /* Only leafs vdevs have slow IOs */ | |
2370 | zfs_nicenum(vs->vs_slow_ios, rbuf, | |
2371 | sizeof (rbuf)); | |
2372 | } else { | |
2373 | snprintf(rbuf, sizeof (rbuf), "-"); | |
2374 | } | |
2375 | ||
2376 | if (cb->cb_literal) | |
2377 | printf(" %5llu", (u_longlong_t)vs->vs_slow_ios); | |
2378 | else | |
2379 | printf(" %5s", rbuf); | |
2380 | } | |
9babb374 BB |
2381 | } |
2382 | ||
2383 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, | |
2384 | ¬present) == 0) { | |
9babb374 | 2385 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); |
9fb2771a | 2386 | (void) printf(" %s %s", gettext("was"), path); |
9babb374 BB |
2387 | } else if (vs->vs_aux != 0) { |
2388 | (void) printf(" "); | |
9fb2771a | 2389 | color_start(ANSI_RED); |
9babb374 BB |
2390 | switch (vs->vs_aux) { |
2391 | case VDEV_AUX_OPEN_FAILED: | |
2392 | (void) printf(gettext("cannot open")); | |
2393 | break; | |
2394 | ||
2395 | case VDEV_AUX_BAD_GUID_SUM: | |
2396 | (void) printf(gettext("missing device")); | |
2397 | break; | |
2398 | ||
2399 | case VDEV_AUX_NO_REPLICAS: | |
2400 | (void) printf(gettext("insufficient replicas")); | |
2401 | break; | |
2402 | ||
2403 | case VDEV_AUX_VERSION_NEWER: | |
2404 | (void) printf(gettext("newer version")); | |
2405 | break; | |
2406 | ||
9ae529ec CS |
2407 | case VDEV_AUX_UNSUP_FEAT: |
2408 | (void) printf(gettext("unsupported feature(s)")); | |
2409 | break; | |
2410 | ||
6fe3498c RM |
2411 | case VDEV_AUX_ASHIFT_TOO_BIG: |
2412 | (void) printf(gettext("unsupported minimum blocksize")); | |
2413 | break; | |
2414 | ||
9babb374 BB |
2415 | case VDEV_AUX_SPARED: |
2416 | verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, | |
fea33e4e HJ |
2417 | &spare_cb.cb_guid) == 0); |
2418 | if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) { | |
2419 | if (strcmp(zpool_get_name(spare_cb.cb_zhp), | |
9babb374 BB |
2420 | zpool_get_name(zhp)) == 0) |
2421 | (void) printf(gettext("currently in " | |
2422 | "use")); | |
2423 | else | |
2424 | (void) printf(gettext("in use by " | |
2425 | "pool '%s'"), | |
fea33e4e HJ |
2426 | zpool_get_name(spare_cb.cb_zhp)); |
2427 | zpool_close(spare_cb.cb_zhp); | |
9babb374 BB |
2428 | } else { |
2429 | (void) printf(gettext("currently in use")); | |
2430 | } | |
2431 | break; | |
2432 | ||
2433 | case VDEV_AUX_ERR_EXCEEDED: | |
2434 | (void) printf(gettext("too many errors")); | |
2435 | break; | |
2436 | ||
2437 | case VDEV_AUX_IO_FAILURE: | |
2438 | (void) printf(gettext("experienced I/O failures")); | |
2439 | break; | |
2440 | ||
2441 | case VDEV_AUX_BAD_LOG: | |
2442 | (void) printf(gettext("bad intent log")); | |
2443 | break; | |
2444 | ||
428870ff BB |
2445 | case VDEV_AUX_EXTERNAL: |
2446 | (void) printf(gettext("external device fault")); | |
2447 | break; | |
2448 | ||
2449 | case VDEV_AUX_SPLIT_POOL: | |
2450 | (void) printf(gettext("split into new pool")); | |
2451 | break; | |
2452 | ||
379ca9cf OF |
2453 | case VDEV_AUX_ACTIVE: |
2454 | (void) printf(gettext("currently in use")); | |
4265a929 | 2455 | break; |
379ca9cf | 2456 | |
6cb8e530 PZ |
2457 | case VDEV_AUX_CHILDREN_OFFLINE: |
2458 | (void) printf(gettext("all children offline")); | |
2459 | break; | |
2460 | ||
dce1bf99 VKV |
2461 | case VDEV_AUX_BAD_LABEL: |
2462 | (void) printf(gettext("invalid label")); | |
2463 | break; | |
2464 | ||
9babb374 BB |
2465 | default: |
2466 | (void) printf(gettext("corrupted data")); | |
2467 | break; | |
2468 | } | |
9fb2771a | 2469 | color_end(); |
3928ec53 CS |
2470 | } else if (children == 0 && !isspare && |
2471 | getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL && | |
2472 | VDEV_STAT_VALID(vs_physical_ashift, vsc) && | |
2473 | vs->vs_configured_ashift < vs->vs_physical_ashift) { | |
2474 | (void) printf( | |
2475 | gettext(" block size: %dB configured, %dB native"), | |
2476 | 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift); | |
428870ff BB |
2477 | } |
2478 | ||
2a673e76 AJ |
2479 | if (vs->vs_scan_removing != 0) { |
2480 | (void) printf(gettext(" (removing)")); | |
e77d59eb | 2481 | } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) { |
2a673e76 AJ |
2482 | (void) printf(gettext(" (non-allocating)")); |
2483 | } | |
2484 | ||
2ef0f8c3 TH |
2485 | /* The root vdev has the scrub/resilver stats */ |
2486 | root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL), | |
2487 | ZPOOL_CONFIG_VDEV_TREE); | |
2488 | (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS, | |
428870ff BB |
2489 | (uint64_t **)&ps, &c); |
2490 | ||
e9b12d41 TH |
2491 | /* |
2492 | * If you force fault a drive that's resilvering, its scan stats can | |
2493 | * get frozen in time, giving the false impression that it's | |
2494 | * being resilvered. That's why we check the state to see if the vdev | |
2495 | * is healthy before reporting "resilvering" or "repairing". | |
2496 | */ | |
2497 | if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0 && | |
2498 | vs->vs_state == VDEV_STATE_HEALTHY) { | |
80a91e74 TC |
2499 | if (vs->vs_scan_processed != 0) { |
2500 | (void) printf(gettext(" (%s)"), | |
2501 | (ps->pss_func == POOL_SCAN_RESILVER) ? | |
2502 | "resilvering" : "repairing"); | |
2503 | } else if (vs->vs_resilver_deferred) { | |
2504 | (void) printf(gettext(" (awaiting resilver)")); | |
2505 | } | |
9babb374 BB |
2506 | } |
2507 | ||
9a49d3f3 BB |
2508 | /* The top-level vdevs have the rebuild stats */ |
2509 | if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE && | |
e9b12d41 | 2510 | children == 0 && vs->vs_state == VDEV_STATE_HEALTHY) { |
9a49d3f3 BB |
2511 | if (vs->vs_rebuild_processed != 0) { |
2512 | (void) printf(gettext(" (resilvering)")); | |
2513 | } | |
2514 | } | |
2515 | ||
8720e9e7 TH |
2516 | if (cb->vcdl != NULL) { |
2517 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { | |
2518 | printf(" "); | |
2519 | zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); | |
2520 | } | |
2521 | } | |
2522 | ||
b2255edc | 2523 | /* Display vdev initialization and trim status for leaves. */ |
1b939560 BB |
2524 | if (children == 0) { |
2525 | print_status_initialize(vs, cb->cb_print_vdev_init); | |
2526 | print_status_trim(vs, cb->cb_print_vdev_trim); | |
619f0976 GW |
2527 | } |
2528 | ||
9babb374 BB |
2529 | (void) printf("\n"); |
2530 | ||
2531 | for (c = 0; c < children; c++) { | |
428870ff | 2532 | uint64_t islog = B_FALSE, ishole = B_FALSE; |
9babb374 | 2533 | |
428870ff | 2534 | /* Don't print logs or holes here */ |
9babb374 | 2535 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
428870ff BB |
2536 | &islog); |
2537 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
2538 | &ishole); | |
2539 | if (islog || ishole) | |
9babb374 | 2540 | continue; |
cc99f275 DB |
2541 | /* Only print normal classes here */ |
2542 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) | |
2543 | continue; | |
2544 | ||
9a49d3f3 BB |
2545 | /* Provide vdev_rebuild_stats to children if available */ |
2546 | if (vrs == NULL) { | |
2547 | (void) nvlist_lookup_uint64_array(nv, | |
2548 | ZPOOL_CONFIG_REBUILD_STATS, | |
2549 | (uint64_t **)&vrs, &i); | |
2550 | } | |
2551 | ||
d2f3e292 | 2552 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
fea33e4e HJ |
2553 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2554 | print_status_config(zhp, cb, vname, child[c], depth + 2, | |
9a49d3f3 | 2555 | isspare, vrs); |
9babb374 BB |
2556 | free(vname); |
2557 | } | |
2558 | } | |
2559 | ||
34dc7c2f BB |
2560 | /* |
2561 | * Print the configuration of an exported pool. Iterate over all vdevs in the | |
2562 | * pool, printing out the name and status for each one. | |
2563 | */ | |
d2f3e292 | 2564 | static void |
fea33e4e HJ |
2565 | print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv, |
2566 | int depth) | |
34dc7c2f BB |
2567 | { |
2568 | nvlist_t **child; | |
2569 | uint_t c, children; | |
2570 | vdev_stat_t *vs; | |
d1807f16 RY |
2571 | const char *type; |
2572 | char *vname; | |
34dc7c2f BB |
2573 | |
2574 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); | |
428870ff BB |
2575 | if (strcmp(type, VDEV_TYPE_MISSING) == 0 || |
2576 | strcmp(type, VDEV_TYPE_HOLE) == 0) | |
34dc7c2f BB |
2577 | return; |
2578 | ||
428870ff | 2579 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
2580 | (uint64_t **)&vs, &c) == 0); |
2581 | ||
fea33e4e | 2582 | (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name); |
34dc7c2f BB |
2583 | (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); |
2584 | ||
2585 | if (vs->vs_aux != 0) { | |
2586 | (void) printf(" "); | |
2587 | ||
2588 | switch (vs->vs_aux) { | |
2589 | case VDEV_AUX_OPEN_FAILED: | |
2590 | (void) printf(gettext("cannot open")); | |
2591 | break; | |
2592 | ||
2593 | case VDEV_AUX_BAD_GUID_SUM: | |
2594 | (void) printf(gettext("missing device")); | |
2595 | break; | |
2596 | ||
2597 | case VDEV_AUX_NO_REPLICAS: | |
2598 | (void) printf(gettext("insufficient replicas")); | |
2599 | break; | |
2600 | ||
2601 | case VDEV_AUX_VERSION_NEWER: | |
2602 | (void) printf(gettext("newer version")); | |
2603 | break; | |
2604 | ||
9ae529ec CS |
2605 | case VDEV_AUX_UNSUP_FEAT: |
2606 | (void) printf(gettext("unsupported feature(s)")); | |
2607 | break; | |
2608 | ||
34dc7c2f BB |
2609 | case VDEV_AUX_ERR_EXCEEDED: |
2610 | (void) printf(gettext("too many errors")); | |
2611 | break; | |
2612 | ||
379ca9cf OF |
2613 | case VDEV_AUX_ACTIVE: |
2614 | (void) printf(gettext("currently in use")); | |
2615 | break; | |
2616 | ||
6cb8e530 PZ |
2617 | case VDEV_AUX_CHILDREN_OFFLINE: |
2618 | (void) printf(gettext("all children offline")); | |
2619 | break; | |
2620 | ||
dce1bf99 VKV |
2621 | case VDEV_AUX_BAD_LABEL: |
2622 | (void) printf(gettext("invalid label")); | |
2623 | break; | |
2624 | ||
34dc7c2f BB |
2625 | default: |
2626 | (void) printf(gettext("corrupted data")); | |
2627 | break; | |
2628 | } | |
2629 | } | |
2630 | (void) printf("\n"); | |
2631 | ||
2632 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
2633 | &child, &children) != 0) | |
2634 | return; | |
2635 | ||
2636 | for (c = 0; c < children; c++) { | |
2637 | uint64_t is_log = B_FALSE; | |
2638 | ||
2639 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
2640 | &is_log); | |
9babb374 | 2641 | if (is_log) |
34dc7c2f | 2642 | continue; |
cc99f275 DB |
2643 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) |
2644 | continue; | |
34dc7c2f | 2645 | |
d2f3e292 | 2646 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e HJ |
2647 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2648 | print_import_config(cb, vname, child[c], depth + 2); | |
34dc7c2f BB |
2649 | free(vname); |
2650 | } | |
2651 | ||
2652 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, | |
2653 | &child, &children) == 0) { | |
2654 | (void) printf(gettext("\tcache\n")); | |
2655 | for (c = 0; c < children; c++) { | |
d2f3e292 | 2656 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e | 2657 | cb->cb_name_flags); |
34dc7c2f BB |
2658 | (void) printf("\t %s\n", vname); |
2659 | free(vname); | |
2660 | } | |
2661 | } | |
2662 | ||
2663 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, | |
2664 | &child, &children) == 0) { | |
2665 | (void) printf(gettext("\tspares\n")); | |
2666 | for (c = 0; c < children; c++) { | |
d2f3e292 | 2667 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e | 2668 | cb->cb_name_flags); |
34dc7c2f BB |
2669 | (void) printf("\t %s\n", vname); |
2670 | free(vname); | |
2671 | } | |
2672 | } | |
2673 | } | |
2674 | ||
9babb374 | 2675 | /* |
cc99f275 DB |
2676 | * Print specialized class vdevs. |
2677 | * | |
2678 | * These are recorded as top level vdevs in the main pool child array | |
2679 | * but with "is_log" set to 1 or an "alloc_bias" string. We use either | |
2680 | * print_status_config() or print_import_config() to print the top level | |
2681 | * class vdevs then any of their children (eg mirrored slogs) are printed | |
2682 | * recursively - which works because only the top level vdev is marked. | |
9babb374 BB |
2683 | */ |
2684 | static void | |
cc99f275 DB |
2685 | print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, |
2686 | const char *class) | |
9babb374 BB |
2687 | { |
2688 | uint_t c, children; | |
2689 | nvlist_t **child; | |
cc99f275 DB |
2690 | boolean_t printed = B_FALSE; |
2691 | ||
2692 | assert(zhp != NULL || !cb->cb_verbose); | |
9babb374 BB |
2693 | |
2694 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, | |
2695 | &children) != 0) | |
2696 | return; | |
2697 | ||
9babb374 BB |
2698 | for (c = 0; c < children; c++) { |
2699 | uint64_t is_log = B_FALSE; | |
d1807f16 RY |
2700 | const char *bias = NULL; |
2701 | const char *type = NULL; | |
9babb374 BB |
2702 | |
2703 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
2704 | &is_log); | |
cc99f275 DB |
2705 | |
2706 | if (is_log) { | |
a926aab9 | 2707 | bias = (char *)VDEV_ALLOC_CLASS_LOGS; |
cc99f275 DB |
2708 | } else { |
2709 | (void) nvlist_lookup_string(child[c], | |
2710 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
2711 | (void) nvlist_lookup_string(child[c], | |
2712 | ZPOOL_CONFIG_TYPE, &type); | |
2713 | } | |
2714 | ||
2715 | if (bias == NULL || strcmp(bias, class) != 0) | |
2716 | continue; | |
2717 | if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
9babb374 | 2718 | continue; |
cc99f275 DB |
2719 | |
2720 | if (!printed) { | |
2721 | (void) printf("\t%s\t\n", gettext(class)); | |
2722 | printed = B_TRUE; | |
2723 | } | |
2724 | ||
2725 | char *name = zpool_vdev_name(g_zfs, zhp, child[c], | |
fea33e4e HJ |
2726 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2727 | if (cb->cb_print_status) | |
2728 | print_status_config(zhp, cb, name, child[c], 2, | |
9a49d3f3 | 2729 | B_FALSE, NULL); |
9babb374 | 2730 | else |
fea33e4e | 2731 | print_import_config(cb, name, child[c], 2); |
9babb374 BB |
2732 | free(name); |
2733 | } | |
2734 | } | |
428870ff | 2735 | |
34dc7c2f BB |
2736 | /* |
2737 | * Display the status for the given pool. | |
2738 | */ | |
0936981d GW |
2739 | static int |
2740 | show_import(nvlist_t *config, boolean_t report_error) | |
34dc7c2f BB |
2741 | { |
2742 | uint64_t pool_state; | |
2743 | vdev_stat_t *vs; | |
d1807f16 | 2744 | const char *name; |
34dc7c2f | 2745 | uint64_t guid; |
379ca9cf | 2746 | uint64_t hostid = 0; |
a926aab9 AZ |
2747 | const char *msgid; |
2748 | const char *hostname = "unknown"; | |
379ca9cf | 2749 | nvlist_t *nvroot, *nvinfo; |
731782ec | 2750 | zpool_status_t reason; |
ffe9d382 | 2751 | zpool_errata_t errata; |
34dc7c2f BB |
2752 | const char *health; |
2753 | uint_t vsc; | |
d1807f16 | 2754 | const char *comment; |
fea33e4e | 2755 | status_cbdata_t cb = { 0 }; |
34dc7c2f BB |
2756 | |
2757 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
2758 | &name) == 0); | |
2759 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
2760 | &guid) == 0); | |
2761 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
2762 | &pool_state) == 0); | |
2763 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
2764 | &nvroot) == 0); | |
2765 | ||
428870ff | 2766 | verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
2767 | (uint64_t **)&vs, &vsc) == 0); |
2768 | health = zpool_state_to_name(vs->vs_state, vs->vs_aux); | |
2769 | ||
ffe9d382 | 2770 | reason = zpool_import_status(config, &msgid, &errata); |
34dc7c2f | 2771 | |
0936981d GW |
2772 | /* |
2773 | * If we're importing using a cachefile, then we won't report any | |
2774 | * errors unless we are in the scan phase of the import. | |
2775 | */ | |
2776 | if (reason != ZPOOL_STATUS_OK && !report_error) | |
2777 | return (reason); | |
2778 | ||
d96eb2b1 DM |
2779 | (void) printf(gettext(" pool: %s\n"), name); |
2780 | (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); | |
2781 | (void) printf(gettext(" state: %s"), health); | |
34dc7c2f BB |
2782 | if (pool_state == POOL_STATE_DESTROYED) |
2783 | (void) printf(gettext(" (DESTROYED)")); | |
2784 | (void) printf("\n"); | |
2785 | ||
2786 | switch (reason) { | |
2787 | case ZPOOL_STATUS_MISSING_DEV_R: | |
2788 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
2789 | case ZPOOL_STATUS_BAD_GUID_SUM: | |
9fb2771a TH |
2790 | printf_color(ANSI_BOLD, gettext("status: ")); |
2791 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
d96eb2b1 | 2792 | "missing from the system.\n")); |
34dc7c2f BB |
2793 | break; |
2794 | ||
2795 | case ZPOOL_STATUS_CORRUPT_LABEL_R: | |
2796 | case ZPOOL_STATUS_CORRUPT_LABEL_NR: | |
9fb2771a TH |
2797 | printf_color(ANSI_BOLD, gettext("status: ")); |
2798 | printf_color(ANSI_YELLOW, gettext("One or more devices contains" | |
2799 | " corrupted data.\n")); | |
34dc7c2f BB |
2800 | break; |
2801 | ||
2802 | case ZPOOL_STATUS_CORRUPT_DATA: | |
d96eb2b1 DM |
2803 | (void) printf( |
2804 | gettext(" status: The pool data is corrupted.\n")); | |
34dc7c2f BB |
2805 | break; |
2806 | ||
2807 | case ZPOOL_STATUS_OFFLINE_DEV: | |
9fb2771a TH |
2808 | printf_color(ANSI_BOLD, gettext("status: ")); |
2809 | printf_color(ANSI_YELLOW, gettext("One or more devices " | |
34dc7c2f BB |
2810 | "are offlined.\n")); |
2811 | break; | |
2812 | ||
2813 | case ZPOOL_STATUS_CORRUPT_POOL: | |
9fb2771a TH |
2814 | printf_color(ANSI_BOLD, gettext("status: ")); |
2815 | printf_color(ANSI_YELLOW, gettext("The pool metadata is " | |
34dc7c2f BB |
2816 | "corrupted.\n")); |
2817 | break; | |
2818 | ||
2819 | case ZPOOL_STATUS_VERSION_OLDER: | |
9fb2771a TH |
2820 | printf_color(ANSI_BOLD, gettext("status: ")); |
2821 | printf_color(ANSI_YELLOW, gettext("The pool is formatted using " | |
2822 | "a legacy on-disk version.\n")); | |
34dc7c2f BB |
2823 | break; |
2824 | ||
2825 | case ZPOOL_STATUS_VERSION_NEWER: | |
9fb2771a TH |
2826 | printf_color(ANSI_BOLD, gettext("status: ")); |
2827 | printf_color(ANSI_YELLOW, gettext("The pool is formatted using " | |
2828 | "an incompatible version.\n")); | |
34dc7c2f | 2829 | break; |
b128c09f | 2830 | |
b9b24bb4 | 2831 | case ZPOOL_STATUS_FEAT_DISABLED: |
9fb2771a | 2832 | printf_color(ANSI_BOLD, gettext("status: ")); |
e086db16 CB |
2833 | printf_color(ANSI_YELLOW, gettext("Some supported " |
2834 | "features are not enabled on the pool.\n\t" | |
2835 | "(Note that they may be intentionally disabled " | |
2836 | "if the\n\t'compatibility' property is set.)\n")); | |
658fb802 CB |
2837 | break; |
2838 | ||
2839 | case ZPOOL_STATUS_COMPATIBILITY_ERR: | |
2840 | printf_color(ANSI_BOLD, gettext("status: ")); | |
2841 | printf_color(ANSI_YELLOW, gettext("Error reading or parsing " | |
2842 | "the file(s) indicated by the 'compatibility'\n" | |
2843 | "property.\n")); | |
b9b24bb4 CS |
2844 | break; |
2845 | ||
e086db16 CB |
2846 | case ZPOOL_STATUS_INCOMPATIBLE_FEAT: |
2847 | printf_color(ANSI_BOLD, gettext("status: ")); | |
2848 | printf_color(ANSI_YELLOW, gettext("One or more features " | |
2849 | "are enabled on the pool despite not being\n" | |
2850 | "requested by the 'compatibility' property.\n")); | |
2851 | break; | |
2852 | ||
9ae529ec | 2853 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
9fb2771a TH |
2854 | printf_color(ANSI_BOLD, gettext("status: ")); |
2855 | printf_color(ANSI_YELLOW, gettext("The pool uses the following " | |
4e33ba4c | 2856 | "feature(s) not supported on this system:\n")); |
9fb2771a | 2857 | color_start(ANSI_YELLOW); |
9ae529ec | 2858 | zpool_print_unsup_feat(config); |
9fb2771a | 2859 | color_end(); |
9ae529ec CS |
2860 | break; |
2861 | ||
2862 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
9fb2771a TH |
2863 | printf_color(ANSI_BOLD, gettext("status: ")); |
2864 | printf_color(ANSI_YELLOW, gettext("The pool can only be " | |
2865 | "accessed in read-only mode on this system. It\n\tcannot be" | |
2866 | " accessed in read-write mode because it uses the " | |
9ae529ec | 2867 | "following\n\tfeature(s) not supported on this system:\n")); |
9fb2771a | 2868 | color_start(ANSI_YELLOW); |
9ae529ec | 2869 | zpool_print_unsup_feat(config); |
9fb2771a | 2870 | color_end(); |
9ae529ec CS |
2871 | break; |
2872 | ||
379ca9cf | 2873 | case ZPOOL_STATUS_HOSTID_ACTIVE: |
9fb2771a TH |
2874 | printf_color(ANSI_BOLD, gettext("status: ")); |
2875 | printf_color(ANSI_YELLOW, gettext("The pool is currently " | |
379ca9cf OF |
2876 | "imported by another system.\n")); |
2877 | break; | |
2878 | ||
2879 | case ZPOOL_STATUS_HOSTID_REQUIRED: | |
9fb2771a TH |
2880 | printf_color(ANSI_BOLD, gettext("status: ")); |
2881 | printf_color(ANSI_YELLOW, gettext("The pool has the " | |
379ca9cf OF |
2882 | "multihost property on. It cannot\n\tbe safely imported " |
2883 | "when the system hostid is not set.\n")); | |
2884 | break; | |
2885 | ||
34dc7c2f | 2886 | case ZPOOL_STATUS_HOSTID_MISMATCH: |
9fb2771a TH |
2887 | printf_color(ANSI_BOLD, gettext("status: ")); |
2888 | printf_color(ANSI_YELLOW, gettext("The pool was last accessed " | |
2889 | "by another system.\n")); | |
34dc7c2f | 2890 | break; |
b128c09f | 2891 | |
34dc7c2f BB |
2892 | case ZPOOL_STATUS_FAULTED_DEV_R: |
2893 | case ZPOOL_STATUS_FAULTED_DEV_NR: | |
9fb2771a TH |
2894 | printf_color(ANSI_BOLD, gettext("status: ")); |
2895 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
34dc7c2f BB |
2896 | "faulted.\n")); |
2897 | break; | |
2898 | ||
b128c09f | 2899 | case ZPOOL_STATUS_BAD_LOG: |
9fb2771a TH |
2900 | printf_color(ANSI_BOLD, gettext("status: ")); |
2901 | printf_color(ANSI_YELLOW, gettext("An intent log record cannot " | |
2902 | "be read.\n")); | |
b128c09f BB |
2903 | break; |
2904 | ||
428870ff | 2905 | case ZPOOL_STATUS_RESILVERING: |
9a49d3f3 | 2906 | case ZPOOL_STATUS_REBUILDING: |
9fb2771a TH |
2907 | printf_color(ANSI_BOLD, gettext("status: ")); |
2908 | printf_color(ANSI_YELLOW, gettext("One or more devices were " | |
2909 | "being resilvered.\n")); | |
428870ff BB |
2910 | break; |
2911 | ||
ffe9d382 | 2912 | case ZPOOL_STATUS_ERRATA: |
9fb2771a TH |
2913 | printf_color(ANSI_BOLD, gettext("status: ")); |
2914 | printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), | |
ffe9d382 BB |
2915 | errata); |
2916 | break; | |
2917 | ||
c494aa7f GW |
2918 | case ZPOOL_STATUS_NON_NATIVE_ASHIFT: |
2919 | printf_color(ANSI_BOLD, gettext("status: ")); | |
2920 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
2921 | "configured to use a non-native block size.\n" | |
2922 | "\tExpect reduced performance.\n")); | |
2923 | break; | |
2924 | ||
34dc7c2f BB |
2925 | default: |
2926 | /* | |
2927 | * No other status can be seen when importing pools. | |
2928 | */ | |
2929 | assert(reason == ZPOOL_STATUS_OK); | |
2930 | } | |
2931 | ||
2932 | /* | |
2933 | * Print out an action according to the overall state of the pool. | |
2934 | */ | |
2935 | if (vs->vs_state == VDEV_STATE_HEALTHY) { | |
b9b24bb4 CS |
2936 | if (reason == ZPOOL_STATUS_VERSION_OLDER || |
2937 | reason == ZPOOL_STATUS_FEAT_DISABLED) { | |
d96eb2b1 | 2938 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
2939 | "imported using its name or numeric identifier, " |
2940 | "though\n\tsome features will not be available " | |
2941 | "without an explicit 'zpool upgrade'.\n")); | |
658fb802 CB |
2942 | } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) { |
2943 | (void) printf(gettext(" action: The pool can be " | |
2944 | "imported using its name or numeric\n\tidentifier, " | |
2945 | "though the file(s) indicated by its " | |
2946 | "'compatibility'\n\tproperty cannot be parsed at " | |
2947 | "this time.\n")); | |
b9b24bb4 | 2948 | } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { |
d96eb2b1 | 2949 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
2950 | "imported using its name or numeric " |
2951 | "identifier and\n\tthe '-f' flag.\n")); | |
ffe9d382 BB |
2952 | } else if (reason == ZPOOL_STATUS_ERRATA) { |
2953 | switch (errata) { | |
2954 | case ZPOOL_ERRATA_NONE: | |
2955 | break; | |
2956 | ||
4f2dcb3e RY |
2957 | case ZPOOL_ERRATA_ZOL_2094_SCRUB: |
2958 | (void) printf(gettext(" action: The pool can " | |
2959 | "be imported using its name or numeric " | |
2960 | "identifier,\n\thowever there is a compat" | |
2961 | "ibility issue which should be corrected" | |
2962 | "\n\tby running 'zpool scrub'\n")); | |
2963 | break; | |
2964 | ||
2965 | case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY: | |
2966 | (void) printf(gettext(" action: The pool can" | |
2967 | "not be imported with this version of ZFS " | |
2968 | "due to\n\tan active asynchronous destroy. " | |
2969 | "Revert to an earlier version\n\tand " | |
2970 | "allow the destroy to complete before " | |
2971 | "updating.\n")); | |
2972 | break; | |
2973 | ||
ae76f45c TC |
2974 | case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: |
2975 | (void) printf(gettext(" action: Existing " | |
2976 | "encrypted datasets contain an on-disk " | |
2977 | "incompatibility, which\n\tneeds to be " | |
2978 | "corrected. Backup these datasets to new " | |
2979 | "encrypted datasets\n\tand destroy the " | |
2980 | "old ones.\n")); | |
2981 | break; | |
2982 | ||
f00ab3f2 | 2983 | case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: |
b3b60984 JS |
2984 | (void) printf(gettext(" action: Existing " |
2985 | "encrypted snapshots and bookmarks contain " | |
2986 | "an on-disk\n\tincompatibility. This may " | |
e2dddb7e JS |
2987 | "cause on-disk corruption if they are used" |
2988 | "\n\twith 'zfs recv'. To correct the " | |
2989 | "issue, enable the bookmark_v2 feature.\n\t" | |
2990 | "No additional action is needed if there " | |
2991 | "are no encrypted snapshots or\n\t" | |
2992 | "bookmarks. If preserving the encrypted " | |
2993 | "snapshots and bookmarks is\n\trequired, " | |
2994 | "use a non-raw send to backup and restore " | |
2995 | "them. Alternately,\n\tthey may be removed" | |
2996 | " to resolve the incompatibility.\n")); | |
f00ab3f2 | 2997 | break; |
ffe9d382 BB |
2998 | default: |
2999 | /* | |
3000 | * All errata must contain an action message. | |
3001 | */ | |
3002 | assert(0); | |
3003 | } | |
b9b24bb4 | 3004 | } else { |
d96eb2b1 | 3005 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
3006 | "imported using its name or numeric " |
3007 | "identifier.\n")); | |
b9b24bb4 | 3008 | } |
34dc7c2f | 3009 | } else if (vs->vs_state == VDEV_STATE_DEGRADED) { |
d96eb2b1 | 3010 | (void) printf(gettext(" action: The pool can be imported " |
34dc7c2f BB |
3011 | "despite missing or damaged devices. The\n\tfault " |
3012 | "tolerance of the pool may be compromised if imported.\n")); | |
3013 | } else { | |
3014 | switch (reason) { | |
3015 | case ZPOOL_STATUS_VERSION_NEWER: | |
d96eb2b1 | 3016 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
3017 | "imported. Access the pool on a system running " |
3018 | "newer\n\tsoftware, or recreate the pool from " | |
3019 | "backup.\n")); | |
3020 | break; | |
9ae529ec | 3021 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
9fb2771a TH |
3022 | printf_color(ANSI_BOLD, gettext("action: ")); |
3023 | printf_color(ANSI_YELLOW, gettext("The pool cannot be " | |
9ae529ec CS |
3024 | "imported. Access the pool on a system that " |
3025 | "supports\n\tthe required feature(s), or recreate " | |
3026 | "the pool from backup.\n")); | |
3027 | break; | |
3028 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
9fb2771a TH |
3029 | printf_color(ANSI_BOLD, gettext("action: ")); |
3030 | printf_color(ANSI_YELLOW, gettext("The pool cannot be " | |
9ae529ec CS |
3031 | "imported in read-write mode. Import the pool " |
3032 | "with\n" | |
3033 | "\t\"-o readonly=on\", access the pool on a system " | |
3034 | "that supports the\n\trequired feature(s), or " | |
3035 | "recreate the pool from backup.\n")); | |
3036 | break; | |
34dc7c2f BB |
3037 | case ZPOOL_STATUS_MISSING_DEV_R: |
3038 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
3039 | case ZPOOL_STATUS_BAD_GUID_SUM: | |
d96eb2b1 | 3040 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
3041 | "imported. Attach the missing\n\tdevices and try " |
3042 | "again.\n")); | |
3043 | break; | |
379ca9cf OF |
3044 | case ZPOOL_STATUS_HOSTID_ACTIVE: |
3045 | VERIFY0(nvlist_lookup_nvlist(config, | |
3046 | ZPOOL_CONFIG_LOAD_INFO, &nvinfo)); | |
3047 | ||
3048 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) | |
3049 | hostname = fnvlist_lookup_string(nvinfo, | |
3050 | ZPOOL_CONFIG_MMP_HOSTNAME); | |
3051 | ||
3052 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) | |
3053 | hostid = fnvlist_lookup_uint64(nvinfo, | |
3054 | ZPOOL_CONFIG_MMP_HOSTID); | |
3055 | ||
3056 | (void) printf(gettext(" action: The pool must be " | |
bde0f7d4 AZ |
3057 | "exported from %s (hostid=%"PRIx64")\n\tbefore it " |
3058 | "can be safely imported.\n"), hostname, hostid); | |
379ca9cf OF |
3059 | break; |
3060 | case ZPOOL_STATUS_HOSTID_REQUIRED: | |
3061 | (void) printf(gettext(" action: Set a unique system " | |
b9373170 | 3062 | "hostid with the zgenhostid(8) command.\n")); |
379ca9cf | 3063 | break; |
34dc7c2f | 3064 | default: |
d96eb2b1 | 3065 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
3066 | "imported due to damaged devices or data.\n")); |
3067 | } | |
3068 | } | |
3069 | ||
d96eb2b1 DM |
3070 | /* Print the comment attached to the pool. */ |
3071 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
3072 | (void) printf(gettext("comment: %s\n"), comment); | |
3073 | ||
34dc7c2f BB |
3074 | /* |
3075 | * If the state is "closed" or "can't open", and the aux state | |
3076 | * is "corrupt data": | |
3077 | */ | |
3078 | if (((vs->vs_state == VDEV_STATE_CLOSED) || | |
3079 | (vs->vs_state == VDEV_STATE_CANT_OPEN)) && | |
3080 | (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { | |
3081 | if (pool_state == POOL_STATE_DESTROYED) | |
3082 | (void) printf(gettext("\tThe pool was destroyed, " | |
3083 | "but can be imported using the '-Df' flags.\n")); | |
3084 | else if (pool_state != POOL_STATE_EXPORTED) | |
3085 | (void) printf(gettext("\tThe pool may be active on " | |
3086 | "another system, but can be imported using\n\t" | |
3087 | "the '-f' flag.\n")); | |
3088 | } | |
3089 | ||
e458fcca BB |
3090 | if (msgid != NULL) { |
3091 | (void) printf(gettext( | |
a2f944a1 RM |
3092 | " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"), |
3093 | msgid); | |
e458fcca | 3094 | } |
34dc7c2f | 3095 | |
d96eb2b1 | 3096 | (void) printf(gettext(" config:\n\n")); |
34dc7c2f | 3097 | |
2df9ad1c GG |
3098 | cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name), |
3099 | VDEV_NAME_TYPE_ID); | |
fea33e4e HJ |
3100 | if (cb.cb_namewidth < 10) |
3101 | cb.cb_namewidth = 10; | |
34dc7c2f | 3102 | |
fea33e4e | 3103 | print_import_config(&cb, name, nvroot, 0); |
cc99f275 DB |
3104 | |
3105 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP); | |
3106 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL); | |
3107 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS); | |
34dc7c2f BB |
3108 | |
3109 | if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { | |
3110 | (void) printf(gettext("\n\tAdditional devices are known to " | |
3111 | "be part of this pool, though their\n\texact " | |
3112 | "configuration cannot be determined.\n")); | |
3113 | } | |
0936981d | 3114 | return (0); |
34dc7c2f BB |
3115 | } |
3116 | ||
379ca9cf OF |
3117 | static boolean_t |
3118 | zfs_force_import_required(nvlist_t *config) | |
3119 | { | |
3120 | uint64_t state; | |
3121 | uint64_t hostid = 0; | |
3122 | nvlist_t *nvinfo; | |
3123 | ||
3124 | state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE); | |
54b1b1d8 RN |
3125 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); |
3126 | ||
3127 | /* | |
3128 | * The hostid on LOAD_INFO comes from the MOS label via | |
3129 | * spa_tryimport(). If its not there then we're likely talking to an | |
3130 | * older kernel, so use the top one, which will be from the label | |
3131 | * discovered in zpool_find_import(), or if a cachefile is in use, the | |
3132 | * local hostid. | |
3133 | */ | |
3134 | if (nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_HOSTID, &hostid) != 0) | |
3135 | nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid); | |
379ca9cf OF |
3136 | |
3137 | if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid()) | |
3138 | return (B_TRUE); | |
3139 | ||
379ca9cf OF |
3140 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) { |
3141 | mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo, | |
3142 | ZPOOL_CONFIG_MMP_STATE); | |
3143 | ||
3144 | if (mmp_state != MMP_STATE_INACTIVE) | |
3145 | return (B_TRUE); | |
3146 | } | |
3147 | ||
3148 | return (B_FALSE); | |
3149 | } | |
3150 | ||
34dc7c2f BB |
3151 | /* |
3152 | * Perform the import for the given configuration. This passes the heavy | |
3153 | * lifting off to zpool_import_props(), and then mounts the datasets contained | |
3154 | * within the pool. | |
3155 | */ | |
3156 | static int | |
3157 | do_import(nvlist_t *config, const char *newname, const char *mntopts, | |
572e2857 | 3158 | nvlist_t *props, int flags) |
34dc7c2f | 3159 | { |
b5256303 | 3160 | int ret = 0; |
71472bf3 | 3161 | int ms_status = 0; |
34dc7c2f | 3162 | zpool_handle_t *zhp; |
bf12053c | 3163 | const char *name; |
34dc7c2f | 3164 | uint64_t version; |
34dc7c2f | 3165 | |
379ca9cf | 3166 | name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME); |
379ca9cf | 3167 | version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION); |
34dc7c2f | 3168 | |
9ae529ec | 3169 | if (!SPA_VERSION_IS_SUPPORTED(version)) { |
34dc7c2f | 3170 | (void) fprintf(stderr, gettext("cannot import '%s': pool " |
9ae529ec | 3171 | "is formatted using an unsupported ZFS version\n"), name); |
34dc7c2f | 3172 | return (1); |
379ca9cf | 3173 | } else if (zfs_force_import_required(config) && |
572e2857 | 3174 | !(flags & ZFS_IMPORT_ANY_HOST)) { |
379ca9cf OF |
3175 | mmp_state_t mmp_state = MMP_STATE_INACTIVE; |
3176 | nvlist_t *nvinfo; | |
3177 | ||
3178 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
3179 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) | |
3180 | mmp_state = fnvlist_lookup_uint64(nvinfo, | |
3181 | ZPOOL_CONFIG_MMP_STATE); | |
3182 | ||
3183 | if (mmp_state == MMP_STATE_ACTIVE) { | |
bf12053c | 3184 | const char *hostname = "<unknown>"; |
379ca9cf OF |
3185 | uint64_t hostid = 0; |
3186 | ||
3187 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) | |
3188 | hostname = fnvlist_lookup_string(nvinfo, | |
3189 | ZPOOL_CONFIG_MMP_HOSTNAME); | |
3190 | ||
3191 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) | |
3192 | hostid = fnvlist_lookup_uint64(nvinfo, | |
3193 | ZPOOL_CONFIG_MMP_HOSTID); | |
3194 | ||
3195 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3196 | "pool is imported on %s (hostid: " | |
bde0f7d4 AZ |
3197 | "0x%"PRIx64")\nExport the pool on the other " |
3198 | "system, then run 'zpool import'.\n"), | |
3199 | name, hostname, hostid); | |
379ca9cf OF |
3200 | } else if (mmp_state == MMP_STATE_NO_HOSTID) { |
3201 | (void) fprintf(stderr, gettext("Cannot import '%s': " | |
3202 | "pool has the multihost property on and the\n" | |
3203 | "system's hostid is not set. Set a unique hostid " | |
b9373170 | 3204 | "with the zgenhostid(8) command.\n"), name); |
379ca9cf | 3205 | } else { |
bf12053c AZ |
3206 | const char *hostname = "<unknown>"; |
3207 | time_t timestamp = 0; | |
379ca9cf OF |
3208 | uint64_t hostid = 0; |
3209 | ||
54b1b1d8 RN |
3210 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTNAME)) |
3211 | hostname = fnvlist_lookup_string(nvinfo, | |
3212 | ZPOOL_CONFIG_HOSTNAME); | |
3213 | else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME)) | |
379ca9cf OF |
3214 | hostname = fnvlist_lookup_string(config, |
3215 | ZPOOL_CONFIG_HOSTNAME); | |
3216 | ||
3217 | if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP)) | |
3218 | timestamp = fnvlist_lookup_uint64(config, | |
3219 | ZPOOL_CONFIG_TIMESTAMP); | |
3220 | ||
54b1b1d8 RN |
3221 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTID)) |
3222 | hostid = fnvlist_lookup_uint64(nvinfo, | |
3223 | ZPOOL_CONFIG_HOSTID); | |
3224 | else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID)) | |
379ca9cf OF |
3225 | hostid = fnvlist_lookup_uint64(config, |
3226 | ZPOOL_CONFIG_HOSTID); | |
3227 | ||
3228 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3229 | "pool was previously in use from another system.\n" | |
bde0f7d4 | 3230 | "Last accessed by %s (hostid=%"PRIx64") at %s" |
379ca9cf OF |
3231 | "The pool can be imported, use 'zpool import -f' " |
3232 | "to import the pool.\n"), name, hostname, | |
bde0f7d4 | 3233 | hostid, ctime(×tamp)); |
34dc7c2f | 3234 | } |
379ca9cf OF |
3235 | |
3236 | return (1); | |
34dc7c2f BB |
3237 | } |
3238 | ||
572e2857 | 3239 | if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) |
34dc7c2f BB |
3240 | return (1); |
3241 | ||
3242 | if (newname != NULL) | |
bf12053c | 3243 | name = newname; |
34dc7c2f | 3244 | |
45d1cae3 BB |
3245 | if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) |
3246 | return (1); | |
34dc7c2f | 3247 | |
b5256303 TC |
3248 | /* |
3249 | * Loading keys is best effort. We don't want to return immediately | |
3250 | * if it fails but we do want to give the error to the caller. | |
3251 | */ | |
a401a2c5 AZ |
3252 | if (flags & ZFS_IMPORT_LOAD_KEYS && |
3253 | zfs_crypto_attempt_load_keys(g_zfs, name) != 0) | |
b5256303 | 3254 | ret = 1; |
b5256303 | 3255 | |
d164b209 | 3256 | if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && |
71472bf3 US |
3257 | !(flags & ZFS_IMPORT_ONLY)) { |
3258 | ms_status = zpool_enable_datasets(zhp, mntopts, 0); | |
3259 | if (ms_status == EZFS_SHAREFAILED) { | |
3260 | (void) fprintf(stderr, gettext("Import was " | |
3261 | "successful, but unable to share some datasets")); | |
3262 | } else if (ms_status == EZFS_MOUNTFAILED) { | |
3263 | (void) fprintf(stderr, gettext("Import was " | |
3264 | "successful, but unable to mount some datasets")); | |
3265 | } | |
34dc7c2f BB |
3266 | } |
3267 | ||
3268 | zpool_close(zhp); | |
b5256303 | 3269 | return (ret); |
34dc7c2f BB |
3270 | } |
3271 | ||
0936981d GW |
3272 | static int |
3273 | import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags, | |
3274 | char *orig_name, char *new_name, | |
3275 | boolean_t do_destroyed, boolean_t pool_specified, boolean_t do_all, | |
3276 | importargs_t *import) | |
3277 | { | |
3278 | nvlist_t *config = NULL; | |
3279 | nvlist_t *found_config = NULL; | |
3280 | uint64_t pool_state; | |
3281 | ||
3282 | /* | |
3283 | * At this point we have a list of import candidate configs. Even if | |
3284 | * we were searching by pool name or guid, we still need to | |
3285 | * post-process the list to deal with pool state and possible | |
3286 | * duplicate names. | |
3287 | */ | |
3288 | int err = 0; | |
3289 | nvpair_t *elem = NULL; | |
3290 | boolean_t first = B_TRUE; | |
3291 | while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { | |
3292 | ||
3293 | verify(nvpair_value_nvlist(elem, &config) == 0); | |
3294 | ||
3295 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
3296 | &pool_state) == 0); | |
3297 | if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) | |
3298 | continue; | |
3299 | if (do_destroyed && pool_state != POOL_STATE_DESTROYED) | |
3300 | continue; | |
3301 | ||
3302 | verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, | |
3303 | import->policy) == 0); | |
3304 | ||
3305 | if (!pool_specified) { | |
3306 | if (first) | |
3307 | first = B_FALSE; | |
3308 | else if (!do_all) | |
a926aab9 | 3309 | (void) fputc('\n', stdout); |
0936981d GW |
3310 | |
3311 | if (do_all) { | |
3312 | err |= do_import(config, NULL, mntopts, | |
3313 | props, flags); | |
3314 | } else { | |
3315 | /* | |
3316 | * If we're importing from cachefile, then | |
3317 | * we don't want to report errors until we | |
3318 | * are in the scan phase of the import. If | |
3319 | * we get an error, then we return that error | |
3320 | * to invoke the scan phase. | |
3321 | */ | |
3322 | if (import->cachefile && !import->scan) | |
3323 | err = show_import(config, B_FALSE); | |
3324 | else | |
3325 | (void) show_import(config, B_TRUE); | |
3326 | } | |
3327 | } else if (import->poolname != NULL) { | |
d1807f16 | 3328 | const char *name; |
0936981d GW |
3329 | |
3330 | /* | |
3331 | * We are searching for a pool based on name. | |
3332 | */ | |
3333 | verify(nvlist_lookup_string(config, | |
3334 | ZPOOL_CONFIG_POOL_NAME, &name) == 0); | |
3335 | ||
3336 | if (strcmp(name, import->poolname) == 0) { | |
3337 | if (found_config != NULL) { | |
3338 | (void) fprintf(stderr, gettext( | |
3339 | "cannot import '%s': more than " | |
3340 | "one matching pool\n"), | |
3341 | import->poolname); | |
3342 | (void) fprintf(stderr, gettext( | |
3343 | "import by numeric ID instead\n")); | |
3344 | err = B_TRUE; | |
3345 | } | |
3346 | found_config = config; | |
3347 | } | |
3348 | } else { | |
3349 | uint64_t guid; | |
3350 | ||
3351 | /* | |
3352 | * Search for a pool by guid. | |
3353 | */ | |
3354 | verify(nvlist_lookup_uint64(config, | |
3355 | ZPOOL_CONFIG_POOL_GUID, &guid) == 0); | |
3356 | ||
3357 | if (guid == import->guid) | |
3358 | found_config = config; | |
3359 | } | |
3360 | } | |
3361 | ||
3362 | /* | |
3363 | * If we were searching for a specific pool, verify that we found a | |
3364 | * pool, and then do the import. | |
3365 | */ | |
3366 | if (pool_specified && err == 0) { | |
3367 | if (found_config == NULL) { | |
3368 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3369 | "no such pool available\n"), orig_name); | |
3370 | err = B_TRUE; | |
3371 | } else { | |
3372 | err |= do_import(found_config, new_name, | |
3373 | mntopts, props, flags); | |
3374 | } | |
3375 | } | |
3376 | ||
3377 | /* | |
3378 | * If we were just looking for pools, report an error if none were | |
3379 | * found. | |
3380 | */ | |
3381 | if (!pool_specified && first) | |
3382 | (void) fprintf(stderr, | |
3383 | gettext("no pools available to import\n")); | |
3384 | return (err); | |
3385 | } | |
3386 | ||
e89f1295 DB |
3387 | typedef struct target_exists_args { |
3388 | const char *poolname; | |
3389 | uint64_t poolguid; | |
3390 | } target_exists_args_t; | |
3391 | ||
3392 | static int | |
3393 | name_or_guid_exists(zpool_handle_t *zhp, void *data) | |
3394 | { | |
3395 | target_exists_args_t *args = data; | |
3396 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
3397 | int found = 0; | |
3398 | ||
3399 | if (config == NULL) | |
3400 | return (0); | |
3401 | ||
3402 | if (args->poolname != NULL) { | |
d1807f16 | 3403 | const char *pool_name; |
e89f1295 DB |
3404 | |
3405 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
3406 | &pool_name) == 0); | |
3407 | if (strcmp(pool_name, args->poolname) == 0) | |
3408 | found = 1; | |
3409 | } else { | |
3410 | uint64_t pool_guid; | |
3411 | ||
3412 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
3413 | &pool_guid) == 0); | |
3414 | if (pool_guid == args->poolguid) | |
3415 | found = 1; | |
3416 | } | |
3417 | zpool_close(zhp); | |
3418 | ||
3419 | return (found); | |
3420 | } | |
d2734cce SD |
3421 | /* |
3422 | * zpool checkpoint <pool> | |
3423 | * checkpoint --discard <pool> | |
3424 | * | |
3425 | * -d Discard the checkpoint from a checkpointed | |
3426 | * --discard pool. | |
3427 | * | |
e60e158e JG |
3428 | * -w Wait for discarding a checkpoint to complete. |
3429 | * --wait | |
3430 | * | |
d2734cce SD |
3431 | * Checkpoints the specified pool, by taking a "snapshot" of its |
3432 | * current state. A pool can only have one checkpoint at a time. | |
3433 | */ | |
3434 | int | |
3435 | zpool_do_checkpoint(int argc, char **argv) | |
3436 | { | |
e60e158e | 3437 | boolean_t discard, wait; |
d2734cce SD |
3438 | char *pool; |
3439 | zpool_handle_t *zhp; | |
3440 | int c, err; | |
3441 | ||
3442 | struct option long_options[] = { | |
3443 | {"discard", no_argument, NULL, 'd'}, | |
e60e158e | 3444 | {"wait", no_argument, NULL, 'w'}, |
d2734cce SD |
3445 | {0, 0, 0, 0} |
3446 | }; | |
3447 | ||
3448 | discard = B_FALSE; | |
e60e158e JG |
3449 | wait = B_FALSE; |
3450 | while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) { | |
d2734cce SD |
3451 | switch (c) { |
3452 | case 'd': | |
3453 | discard = B_TRUE; | |
3454 | break; | |
e60e158e JG |
3455 | case 'w': |
3456 | wait = B_TRUE; | |
3457 | break; | |
d2734cce SD |
3458 | case '?': |
3459 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3460 | optopt); | |
3461 | usage(B_FALSE); | |
3462 | } | |
3463 | } | |
3464 | ||
e60e158e JG |
3465 | if (wait && !discard) { |
3466 | (void) fprintf(stderr, gettext("--wait only valid when " | |
3467 | "--discard also specified\n")); | |
3468 | usage(B_FALSE); | |
3469 | } | |
3470 | ||
d2734cce SD |
3471 | argc -= optind; |
3472 | argv += optind; | |
3473 | ||
3474 | if (argc < 1) { | |
3475 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
3476 | usage(B_FALSE); | |
3477 | } | |
3478 | ||
3479 | if (argc > 1) { | |
3480 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
3481 | usage(B_FALSE); | |
3482 | } | |
3483 | ||
3484 | pool = argv[0]; | |
3485 | ||
3486 | if ((zhp = zpool_open(g_zfs, pool)) == NULL) { | |
3487 | /* As a special case, check for use of '/' in the name */ | |
3488 | if (strchr(pool, '/') != NULL) | |
3489 | (void) fprintf(stderr, gettext("'zpool checkpoint' " | |
3490 | "doesn't work on datasets. To save the state " | |
3491 | "of a dataset from a specific point in time " | |
3492 | "please use 'zfs snapshot'\n")); | |
3493 | return (1); | |
3494 | } | |
3495 | ||
e60e158e | 3496 | if (discard) { |
d2734cce | 3497 | err = (zpool_discard_checkpoint(zhp) != 0); |
e60e158e JG |
3498 | if (err == 0 && wait) |
3499 | err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD); | |
3500 | } else { | |
d2734cce | 3501 | err = (zpool_checkpoint(zhp) != 0); |
e60e158e | 3502 | } |
d2734cce SD |
3503 | |
3504 | zpool_close(zhp); | |
3505 | ||
3506 | return (err); | |
3507 | } | |
3508 | ||
3509 | #define CHECKPOINT_OPT 1024 | |
3510 | ||
34dc7c2f BB |
3511 | /* |
3512 | * zpool import [-d dir] [-D] | |
b5256303 | 3513 | * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] |
0936981d | 3514 | * [-d dir | -c cachefile | -s] [-f] -a |
b5256303 | 3515 | * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] |
0936981d GW |
3516 | * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id> |
3517 | * [newpool] | |
34dc7c2f | 3518 | * |
0936981d GW |
3519 | * -c Read pool information from a cachefile instead of searching |
3520 | * devices. If importing from a cachefile config fails, then | |
3521 | * fallback to searching for devices only in the directories that | |
3522 | * exist in the cachefile. | |
34dc7c2f | 3523 | * |
0936981d | 3524 | * -d Scan in a specific directory, other than /dev/. More than |
34dc7c2f BB |
3525 | * one directory can be specified using multiple '-d' options. |
3526 | * | |
0936981d GW |
3527 | * -D Scan for previously destroyed pools or import all or only |
3528 | * specified destroyed pools. | |
34dc7c2f | 3529 | * |
0936981d | 3530 | * -R Temporarily import the pool, with all mountpoints relative to |
34dc7c2f BB |
3531 | * the given root. The pool will remain exported when the machine |
3532 | * is rebooted. | |
3533 | * | |
0936981d GW |
3534 | * -V Import even in the presence of faulted vdevs. This is an |
3535 | * intentionally undocumented option for testing purposes, and | |
3536 | * treats the pool configuration as complete, leaving any bad | |
45d1cae3 BB |
3537 | * vdevs in the FAULTED state. In other words, it does verbatim |
3538 | * import. | |
b128c09f | 3539 | * |
0936981d | 3540 | * -f Force import, even if it appears that the pool is active. |
428870ff | 3541 | * |
0936981d | 3542 | * -F Attempt rewind if necessary. |
428870ff | 3543 | * |
0936981d | 3544 | * -n See if rewind would work, but don't actually rewind. |
428870ff | 3545 | * |
0936981d | 3546 | * -N Import the pool but don't mount datasets. |
572e2857 | 3547 | * |
0936981d GW |
3548 | * -T Specify a starting txg to use for import. This option is |
3549 | * intentionally undocumented option for testing purposes. | |
572e2857 | 3550 | * |
0936981d | 3551 | * -a Import all pools found. |
34dc7c2f | 3552 | * |
0936981d | 3553 | * -l Load encryption keys while importing. |
b5256303 | 3554 | * |
0936981d | 3555 | * -o Set property=value and/or temporary mount options (without '='). |
34dc7c2f | 3556 | * |
0936981d GW |
3557 | * -s Scan using the default search path, the libblkid cache will |
3558 | * not be consulted. | |
7d11e37e | 3559 | * |
0936981d GW |
3560 | * --rewind-to-checkpoint |
3561 | * Import the pool and revert back to the checkpoint. | |
d2734cce | 3562 | * |
34dc7c2f BB |
3563 | * The import command scans for pools to import, and import pools based on pool |
3564 | * name and GUID. The pool can also be renamed as part of the import process. | |
3565 | */ | |
3566 | int | |
3567 | zpool_do_import(int argc, char **argv) | |
3568 | { | |
3569 | char **searchdirs = NULL; | |
44867b6d | 3570 | char *env, *envdup = NULL; |
34dc7c2f BB |
3571 | int nsearch = 0; |
3572 | int c; | |
428870ff | 3573 | int err = 0; |
34dc7c2f BB |
3574 | nvlist_t *pools = NULL; |
3575 | boolean_t do_all = B_FALSE; | |
3576 | boolean_t do_destroyed = B_FALSE; | |
3577 | char *mntopts = NULL; | |
b128c09f BB |
3578 | uint64_t searchguid = 0; |
3579 | char *searchname = NULL; | |
34dc7c2f | 3580 | char *propval; |
428870ff | 3581 | nvlist_t *policy = NULL; |
34dc7c2f | 3582 | nvlist_t *props = NULL; |
572e2857 | 3583 | int flags = ZFS_IMPORT_NORMAL; |
428870ff BB |
3584 | uint32_t rewind_policy = ZPOOL_NO_REWIND; |
3585 | boolean_t dryrun = B_FALSE; | |
3586 | boolean_t do_rewind = B_FALSE; | |
3587 | boolean_t xtreme_rewind = B_FALSE; | |
7d11e37e | 3588 | boolean_t do_scan = B_FALSE; |
e89f1295 | 3589 | boolean_t pool_exists = B_FALSE; |
0936981d GW |
3590 | boolean_t pool_specified = B_FALSE; |
3591 | uint64_t txg = -1ULL; | |
34dc7c2f | 3592 | char *cachefile = NULL; |
428870ff | 3593 | importargs_t idata = { 0 }; |
572e2857 | 3594 | char *endptr; |
34dc7c2f | 3595 | |
d2734cce SD |
3596 | struct option long_options[] = { |
3597 | {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT}, | |
3598 | {0, 0, 0, 0} | |
3599 | }; | |
3600 | ||
34dc7c2f | 3601 | /* check options */ |
d2734cce SD |
3602 | while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX", |
3603 | long_options, NULL)) != -1) { | |
34dc7c2f BB |
3604 | switch (c) { |
3605 | case 'a': | |
3606 | do_all = B_TRUE; | |
3607 | break; | |
3608 | case 'c': | |
3609 | cachefile = optarg; | |
3610 | break; | |
3611 | case 'd': | |
a281f769 AZ |
3612 | searchdirs = safe_realloc(searchdirs, |
3613 | (nsearch + 1) * sizeof (char *)); | |
34dc7c2f BB |
3614 | searchdirs[nsearch++] = optarg; |
3615 | break; | |
3616 | case 'D': | |
3617 | do_destroyed = B_TRUE; | |
3618 | break; | |
3619 | case 'f': | |
572e2857 | 3620 | flags |= ZFS_IMPORT_ANY_HOST; |
34dc7c2f | 3621 | break; |
b128c09f | 3622 | case 'F': |
428870ff BB |
3623 | do_rewind = B_TRUE; |
3624 | break; | |
b5256303 TC |
3625 | case 'l': |
3626 | flags |= ZFS_IMPORT_LOAD_KEYS; | |
3627 | break; | |
572e2857 BB |
3628 | case 'm': |
3629 | flags |= ZFS_IMPORT_MISSING_LOG; | |
3630 | break; | |
428870ff BB |
3631 | case 'n': |
3632 | dryrun = B_TRUE; | |
b128c09f | 3633 | break; |
572e2857 BB |
3634 | case 'N': |
3635 | flags |= ZFS_IMPORT_ONLY; | |
3636 | break; | |
34dc7c2f BB |
3637 | case 'o': |
3638 | if ((propval = strchr(optarg, '=')) != NULL) { | |
3639 | *propval = '\0'; | |
3640 | propval++; | |
b128c09f BB |
3641 | if (add_prop_list(optarg, propval, |
3642 | &props, B_TRUE)) | |
34dc7c2f BB |
3643 | goto error; |
3644 | } else { | |
3645 | mntopts = optarg; | |
3646 | } | |
3647 | break; | |
3648 | case 'R': | |
3649 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 3650 | ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) |
34dc7c2f | 3651 | goto error; |
2f3ec900 | 3652 | if (add_prop_list_default(zpool_prop_to_name( |
e40ca391 | 3653 | ZPOOL_PROP_CACHEFILE), "none", &props)) |
34dc7c2f BB |
3654 | goto error; |
3655 | break; | |
7d11e37e BB |
3656 | case 's': |
3657 | do_scan = B_TRUE; | |
3658 | break; | |
26b42f3f RY |
3659 | case 't': |
3660 | flags |= ZFS_IMPORT_TEMP_NAME; | |
00d2a8c9 | 3661 | if (add_prop_list_default(zpool_prop_to_name( |
e40ca391 | 3662 | ZPOOL_PROP_CACHEFILE), "none", &props)) |
00d2a8c9 | 3663 | goto error; |
26b42f3f RY |
3664 | break; |
3665 | ||
572e2857 BB |
3666 | case 'T': |
3667 | errno = 0; | |
dea377c0 | 3668 | txg = strtoull(optarg, &endptr, 0); |
572e2857 BB |
3669 | if (errno != 0 || *endptr != '\0') { |
3670 | (void) fprintf(stderr, | |
3671 | gettext("invalid txg value\n")); | |
3672 | usage(B_FALSE); | |
3673 | } | |
3674 | rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; | |
3675 | break; | |
428870ff | 3676 | case 'V': |
572e2857 | 3677 | flags |= ZFS_IMPORT_VERBATIM; |
428870ff BB |
3678 | break; |
3679 | case 'X': | |
3680 | xtreme_rewind = B_TRUE; | |
3681 | break; | |
d2734cce SD |
3682 | case CHECKPOINT_OPT: |
3683 | flags |= ZFS_IMPORT_CHECKPOINT; | |
3684 | break; | |
34dc7c2f BB |
3685 | case ':': |
3686 | (void) fprintf(stderr, gettext("missing argument for " | |
3687 | "'%c' option\n"), optopt); | |
3688 | usage(B_FALSE); | |
3689 | break; | |
3690 | case '?': | |
3691 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3692 | optopt); | |
3693 | usage(B_FALSE); | |
3694 | } | |
3695 | } | |
3696 | ||
3697 | argc -= optind; | |
3698 | argv += optind; | |
3699 | ||
3700 | if (cachefile && nsearch != 0) { | |
3701 | (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); | |
3702 | usage(B_FALSE); | |
3703 | } | |
3704 | ||
0936981d GW |
3705 | if (cachefile && do_scan) { |
3706 | (void) fprintf(stderr, gettext("-c is incompatible with -s\n")); | |
3707 | usage(B_FALSE); | |
3708 | } | |
3709 | ||
b5256303 TC |
3710 | if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) { |
3711 | (void) fprintf(stderr, gettext("-l is incompatible with -N\n")); | |
3712 | usage(B_FALSE); | |
3713 | } | |
3714 | ||
3715 | if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) { | |
3716 | (void) fprintf(stderr, gettext("-l is only meaningful during " | |
3717 | "an import\n")); | |
3718 | usage(B_FALSE); | |
3719 | } | |
3720 | ||
428870ff BB |
3721 | if ((dryrun || xtreme_rewind) && !do_rewind) { |
3722 | (void) fprintf(stderr, | |
3723 | gettext("-n or -X only meaningful with -F\n")); | |
3724 | usage(B_FALSE); | |
3725 | } | |
3726 | if (dryrun) | |
3727 | rewind_policy = ZPOOL_TRY_REWIND; | |
3728 | else if (do_rewind) | |
3729 | rewind_policy = ZPOOL_DO_REWIND; | |
3730 | if (xtreme_rewind) | |
3731 | rewind_policy |= ZPOOL_EXTREME_REWIND; | |
3732 | ||
3733 | /* In the future, we can capture further policy and include it here */ | |
3734 | if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || | |
8a393be3 PZ |
3735 | nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 || |
3736 | nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, | |
3737 | rewind_policy) != 0) | |
428870ff BB |
3738 | goto error; |
3739 | ||
34dc7c2f BB |
3740 | /* check argument count */ |
3741 | if (do_all) { | |
3742 | if (argc != 0) { | |
3743 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
3744 | usage(B_FALSE); | |
3745 | } | |
3746 | } else { | |
3747 | if (argc > 2) { | |
3748 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
3749 | usage(B_FALSE); | |
3750 | } | |
f74b821a | 3751 | } |
34dc7c2f | 3752 | |
f74b821a BB |
3753 | /* |
3754 | * Check for the effective uid. We do this explicitly here because | |
3755 | * otherwise any attempt to discover pools will silently fail. | |
3756 | */ | |
3757 | if (argc == 0 && geteuid() != 0) { | |
3758 | (void) fprintf(stderr, gettext("cannot " | |
3759 | "discover pools: permission denied\n")); | |
d603ed6c | 3760 | |
a401a2c5 | 3761 | free(searchdirs); |
a425f5bf | 3762 | nvlist_free(props); |
f74b821a BB |
3763 | nvlist_free(policy); |
3764 | return (1); | |
34dc7c2f BB |
3765 | } |
3766 | ||
34dc7c2f | 3767 | /* |
34dc7c2f BB |
3768 | * Depending on the arguments given, we do one of the following: |
3769 | * | |
3770 | * <none> Iterate through all pools and display information about | |
3771 | * each one. | |
3772 | * | |
3773 | * -a Iterate through all pools and try to import each one. | |
3774 | * | |
3775 | * <id> Find the pool that corresponds to the given GUID/pool | |
3776 | * name and import that one. | |
3777 | * | |
3778 | * -D Above options applies only to destroyed pools. | |
3779 | */ | |
3780 | if (argc != 0) { | |
3781 | char *endptr; | |
3782 | ||
3783 | errno = 0; | |
3784 | searchguid = strtoull(argv[0], &endptr, 10); | |
eaa52d32 | 3785 | if (errno != 0 || *endptr != '\0') { |
34dc7c2f | 3786 | searchname = argv[0]; |
eaa52d32 PJ |
3787 | searchguid = 0; |
3788 | } | |
0936981d | 3789 | pool_specified = B_TRUE; |
34dc7c2f | 3790 | |
b128c09f | 3791 | /* |
428870ff | 3792 | * User specified a name or guid. Ensure it's unique. |
b128c09f | 3793 | */ |
e89f1295 DB |
3794 | target_exists_args_t search = {searchname, searchguid}; |
3795 | pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search); | |
b128c09f BB |
3796 | } |
3797 | ||
44867b6d BB |
3798 | /* |
3799 | * Check the environment for the preferred search path. | |
3800 | */ | |
3801 | if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) { | |
a281f769 | 3802 | char *dir, *tmp = NULL; |
44867b6d BB |
3803 | |
3804 | envdup = strdup(env); | |
3805 | ||
a281f769 AZ |
3806 | for (dir = strtok_r(envdup, ":", &tmp); |
3807 | dir != NULL; | |
3808 | dir = strtok_r(NULL, ":", &tmp)) { | |
3809 | searchdirs = safe_realloc(searchdirs, | |
3810 | (nsearch + 1) * sizeof (char *)); | |
44867b6d | 3811 | searchdirs[nsearch++] = dir; |
44867b6d BB |
3812 | } |
3813 | } | |
428870ff BB |
3814 | |
3815 | idata.path = searchdirs; | |
3816 | idata.paths = nsearch; | |
3817 | idata.poolname = searchname; | |
3818 | idata.guid = searchguid; | |
3819 | idata.cachefile = cachefile; | |
7d11e37e | 3820 | idata.scan = do_scan; |
6cb8e530 | 3821 | idata.policy = policy; |
428870ff | 3822 | |
d9ac17a5 US |
3823 | libpc_handle_t lpch = { |
3824 | .lpc_lib_handle = g_zfs, | |
3825 | .lpc_ops = &libzfs_config_ops, | |
3826 | .lpc_printerr = B_TRUE | |
3827 | }; | |
3828 | pools = zpool_search_import(&lpch, &idata); | |
428870ff | 3829 | |
e89f1295 | 3830 | if (pools != NULL && pool_exists && |
428870ff BB |
3831 | (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { |
3832 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3833 | "a pool with that name already exists\n"), | |
3834 | argv[0]); | |
3835 | (void) fprintf(stderr, gettext("use the form '%s " | |
3836 | "<pool | id> <newpool>' to give it a new name\n"), | |
3837 | "zpool import"); | |
3838 | err = 1; | |
e89f1295 | 3839 | } else if (pools == NULL && pool_exists) { |
428870ff BB |
3840 | (void) fprintf(stderr, gettext("cannot import '%s': " |
3841 | "a pool with that name is already created/imported,\n"), | |
3842 | argv[0]); | |
3843 | (void) fprintf(stderr, gettext("and no additional pools " | |
3844 | "with that name were found\n")); | |
3845 | err = 1; | |
3846 | } else if (pools == NULL) { | |
b128c09f BB |
3847 | if (argc != 0) { |
3848 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3849 | "no such pool available\n"), argv[0]); | |
3850 | } | |
428870ff BB |
3851 | err = 1; |
3852 | } | |
3853 | ||
3854 | if (err == 1) { | |
a281f769 AZ |
3855 | free(searchdirs); |
3856 | free(envdup); | |
428870ff | 3857 | nvlist_free(policy); |
a425f5bf | 3858 | nvlist_free(pools); |
3859 | nvlist_free(props); | |
b128c09f BB |
3860 | return (1); |
3861 | } | |
3862 | ||
d8381f50 MA |
3863 | err = import_pools(pools, props, mntopts, flags, |
3864 | argc >= 1 ? argv[0] : NULL, | |
3865 | argc >= 2 ? argv[1] : NULL, | |
3866 | do_destroyed, pool_specified, do_all, &idata); | |
0936981d | 3867 | |
b128c09f | 3868 | /* |
0936981d GW |
3869 | * If we're using the cachefile and we failed to import, then |
3870 | * fallback to scanning the directory for pools that match | |
3871 | * those in the cachefile. | |
b128c09f | 3872 | */ |
0936981d GW |
3873 | if (err != 0 && cachefile != NULL) { |
3874 | (void) printf(gettext("cachefile import failed, retrying\n")); | |
34dc7c2f | 3875 | |
0936981d GW |
3876 | /* |
3877 | * We use the scan flag to gather the directories that exist | |
3878 | * in the cachefile. If we need to fallback to searching for | |
3879 | * the pool config, we will only search devices in these | |
3880 | * directories. | |
3881 | */ | |
3882 | idata.scan = B_TRUE; | |
3883 | nvlist_free(pools); | |
d9ac17a5 | 3884 | pools = zpool_search_import(&lpch, &idata); |
34dc7c2f | 3885 | |
d8381f50 MA |
3886 | err = import_pools(pools, props, mntopts, flags, |
3887 | argc >= 1 ? argv[0] : NULL, | |
3888 | argc >= 2 ? argv[1] : NULL, | |
3889 | do_destroyed, pool_specified, do_all, &idata); | |
34dc7c2f BB |
3890 | } |
3891 | ||
34dc7c2f BB |
3892 | error: |
3893 | nvlist_free(props); | |
3894 | nvlist_free(pools); | |
428870ff | 3895 | nvlist_free(policy); |
a281f769 AZ |
3896 | free(searchdirs); |
3897 | free(envdup); | |
34dc7c2f BB |
3898 | |
3899 | return (err ? 1 : 0); | |
3900 | } | |
3901 | ||
bec1067d AP |
3902 | /* |
3903 | * zpool sync [-f] [pool] ... | |
3904 | * | |
3905 | * -f (undocumented) force uberblock (and config including zpool cache file) | |
3906 | * update. | |
3907 | * | |
3908 | * Sync the specified pool(s). | |
3909 | * Without arguments "zpool sync" will sync all pools. | |
3910 | * This command initiates TXG sync(s) and will return after the TXG(s) commit. | |
3911 | * | |
3912 | */ | |
3913 | static int | |
3914 | zpool_do_sync(int argc, char **argv) | |
3915 | { | |
3916 | int ret; | |
3917 | boolean_t force = B_FALSE; | |
3918 | ||
3919 | /* check options */ | |
3920 | while ((ret = getopt(argc, argv, "f")) != -1) { | |
3921 | switch (ret) { | |
3922 | case 'f': | |
3923 | force = B_TRUE; | |
3924 | break; | |
3925 | case '?': | |
3926 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3927 | optopt); | |
3928 | usage(B_FALSE); | |
3929 | } | |
3930 | } | |
3931 | ||
3932 | argc -= optind; | |
3933 | argv += optind; | |
3934 | ||
3935 | /* if argc == 0 we will execute zpool_sync_one on all pools */ | |
2a673e76 AJ |
3936 | ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, |
3937 | B_FALSE, zpool_sync_one, &force); | |
bec1067d AP |
3938 | |
3939 | return (ret); | |
3940 | } | |
3941 | ||
34dc7c2f | 3942 | typedef struct iostat_cbdata { |
193a37cb | 3943 | uint64_t cb_flags; |
34dc7c2f | 3944 | int cb_namewidth; |
1bd201e7 | 3945 | int cb_iteration; |
193a37cb TH |
3946 | boolean_t cb_verbose; |
3947 | boolean_t cb_literal; | |
3948 | boolean_t cb_scripted; | |
1bd201e7 | 3949 | zpool_list_t *cb_list; |
8720e9e7 | 3950 | vdev_cmd_data_list_t *vcdl; |
2a673e76 | 3951 | vdev_cbdata_t cb_vdevs; |
34dc7c2f BB |
3952 | } iostat_cbdata_t; |
3953 | ||
193a37cb TH |
3954 | /* iostat labels */ |
3955 | typedef struct name_and_columns { | |
3956 | const char *name; /* Column name */ | |
3957 | unsigned int columns; /* Center name to this number of columns */ | |
3958 | } name_and_columns_t; | |
3959 | ||
00888c08 | 3960 | #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */ |
193a37cb TH |
3961 | |
3962 | static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] = | |
3963 | { | |
3964 | [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2}, | |
3965 | {NULL}}, | |
3966 | [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, | |
00888c08 TB |
3967 | {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1}, |
3968 | {NULL}}, | |
193a37cb TH |
3969 | [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2}, |
3970 | {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2}, | |
00888c08 | 3971 | {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL}}, |
57dc41de TH |
3972 | [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, |
3973 | {"asyncq_wait", 2}, {NULL}}, | |
7e945072 | 3974 | [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2}, |
1b939560 | 3975 | {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, |
00888c08 | 3976 | {"trim", 2}, {"rebuild", 2}, {NULL}}, |
193a37cb TH |
3977 | }; |
3978 | ||
3979 | /* Shorthand - if "columns" field not set, default to 1 column */ | |
3980 | static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] = | |
3981 | { | |
3982 | [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"}, | |
3983 | {"write"}, {NULL}}, | |
3984 | [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, | |
00888c08 TB |
3985 | {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"}, |
3986 | {NULL}}, | |
193a37cb | 3987 | [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"}, |
1b939560 | 3988 | {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, |
00888c08 | 3989 | {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}}, |
193a37cb | 3990 | [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, |
00888c08 TB |
3991 | {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"}, |
3992 | {NULL}}, | |
7e945072 | 3993 | [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, |
00888c08 TB |
3994 | {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, |
3995 | {"ind"}, {"agg"}, {NULL}}, | |
7e945072 TH |
3996 | }; |
3997 | ||
3998 | static const char *histo_to_title[] = { | |
3999 | [IOS_L_HISTO] = "latency", | |
4000 | [IOS_RQ_HISTO] = "req_size", | |
193a37cb TH |
4001 | }; |
4002 | ||
4003 | /* | |
4004 | * Return the number of labels in a null-terminated name_and_columns_t | |
4005 | * array. | |
4006 | * | |
4007 | */ | |
4008 | static unsigned int | |
4009 | label_array_len(const name_and_columns_t *labels) | |
4010 | { | |
4011 | int i = 0; | |
4012 | ||
4013 | while (labels[i].name) | |
4014 | i++; | |
4015 | ||
4016 | return (i); | |
4017 | } | |
4018 | ||
7e945072 TH |
4019 | /* |
4020 | * Return the number of strings in a null-terminated string array. | |
4021 | * For example: | |
4022 | * | |
4023 | * const char foo[] = {"bar", "baz", NULL} | |
4024 | * | |
4025 | * returns 2 | |
4026 | */ | |
4027 | static uint64_t | |
4028 | str_array_len(const char *array[]) | |
4029 | { | |
4030 | uint64_t i = 0; | |
4031 | while (array[i]) | |
4032 | i++; | |
4033 | ||
4034 | return (i); | |
4035 | } | |
4036 | ||
4037 | ||
193a37cb TH |
4038 | /* |
4039 | * Return a default column width for default/latency/queue columns. This does | |
4040 | * not include histograms, which have their columns autosized. | |
4041 | */ | |
4042 | static unsigned int | |
4043 | default_column_width(iostat_cbdata_t *cb, enum iostat_type type) | |
4044 | { | |
4045 | unsigned long column_width = 5; /* Normal niceprint */ | |
4046 | static unsigned long widths[] = { | |
4047 | /* | |
4048 | * Choose some sane default column sizes for printing the | |
4049 | * raw numbers. | |
4050 | */ | |
4051 | [IOS_DEFAULT] = 15, /* 1PB capacity */ | |
4052 | [IOS_LATENCY] = 10, /* 1B ns = 10sec */ | |
4053 | [IOS_QUEUES] = 6, /* 1M queue entries */ | |
1b939560 BB |
4054 | [IOS_L_HISTO] = 10, /* 1B ns = 10sec */ |
4055 | [IOS_RQ_HISTO] = 6, /* 1M queue entries */ | |
193a37cb TH |
4056 | }; |
4057 | ||
4058 | if (cb->cb_literal) | |
4059 | column_width = widths[type]; | |
4060 | ||
4061 | return (column_width); | |
4062 | } | |
4063 | ||
4064 | /* | |
4065 | * Print the column labels, i.e: | |
4066 | * | |
4067 | * capacity operations bandwidth | |
4068 | * alloc free read write read write ... | |
4069 | * | |
4070 | * If force_column_width is set, use it for the column width. If not set, use | |
4071 | * the default column width. | |
4072 | */ | |
65c7cc49 | 4073 | static void |
193a37cb TH |
4074 | print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width, |
4075 | const name_and_columns_t labels[][IOSTAT_MAX_LABELS]) | |
4076 | { | |
4077 | int i, idx, s; | |
1b939560 | 4078 | int text_start, rw_column_width, spaces_to_end; |
193a37cb TH |
4079 | uint64_t flags = cb->cb_flags; |
4080 | uint64_t f; | |
4081 | unsigned int column_width = force_column_width; | |
4082 | ||
4083 | /* For each bit set in flags */ | |
4084 | for (f = flags; f; f &= ~(1ULL << idx)) { | |
4085 | idx = lowbit64(f) - 1; | |
4086 | if (!force_column_width) | |
4087 | column_width = default_column_width(cb, idx); | |
4088 | /* Print our top labels centered over "read write" label. */ | |
4089 | for (i = 0; i < label_array_len(labels[idx]); i++) { | |
4090 | const char *name = labels[idx][i].name; | |
4091 | /* | |
4092 | * We treat labels[][].columns == 0 as shorthand | |
4093 | * for one column. It makes writing out the label | |
4094 | * tables more concise. | |
4095 | */ | |
4096 | unsigned int columns = MAX(1, labels[idx][i].columns); | |
4097 | unsigned int slen = strlen(name); | |
4098 | ||
4099 | rw_column_width = (column_width * columns) + | |
4100 | (2 * (columns - 1)); | |
4101 | ||
1b939560 BB |
4102 | text_start = (int)((rw_column_width) / columns - |
4103 | slen / columns); | |
4104 | if (text_start < 0) | |
4105 | text_start = 0; | |
193a37cb TH |
4106 | |
4107 | printf(" "); /* Two spaces between columns */ | |
4108 | ||
4109 | /* Space from beginning of column to label */ | |
4110 | for (s = 0; s < text_start; s++) | |
4111 | printf(" "); | |
4112 | ||
4113 | printf("%s", name); | |
4114 | ||
4115 | /* Print space after label to end of column */ | |
4116 | spaces_to_end = rw_column_width - text_start - slen; | |
1b939560 BB |
4117 | if (spaces_to_end < 0) |
4118 | spaces_to_end = 0; | |
4119 | ||
193a37cb TH |
4120 | for (s = 0; s < spaces_to_end; s++) |
4121 | printf(" "); | |
193a37cb TH |
4122 | } |
4123 | } | |
193a37cb TH |
4124 | } |
4125 | ||
d6418de0 TH |
4126 | |
4127 | /* | |
4128 | * print_cmd_columns - Print custom column titles from -c | |
4129 | * | |
4130 | * If the user specified the "zpool status|iostat -c" then print their custom | |
4131 | * column titles in the header. For example, print_cmd_columns() would print | |
4132 | * the " col1 col2" part of this: | |
4133 | * | |
4134 | * $ zpool iostat -vc 'echo col1=val1; echo col2=val2' | |
4135 | * ... | |
4136 | * capacity operations bandwidth | |
4137 | * pool alloc free read write read write col1 col2 | |
4138 | * ---------- ----- ----- ----- ----- ----- ----- ---- ---- | |
4139 | * mypool 269K 1008M 0 0 107 946 | |
4140 | * mirror 269K 1008M 0 0 107 946 | |
4141 | * sdb - - 0 0 102 473 val1 val2 | |
4142 | * sdc - - 0 0 5 473 val1 val2 | |
4143 | * ---------- ----- ----- ----- ----- ----- ----- ---- ---- | |
4144 | */ | |
65c7cc49 | 4145 | static void |
d6418de0 TH |
4146 | print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes) |
4147 | { | |
4148 | int i, j; | |
4149 | vdev_cmd_data_t *data = &vcdl->data[0]; | |
4150 | ||
4151 | if (vcdl->count == 0 || data == NULL) | |
4152 | return; | |
4153 | ||
4154 | /* | |
4155 | * Each vdev cmd should have the same column names unless the user did | |
4156 | * something weird with their cmd. Just take the column names from the | |
4157 | * first vdev and assume it works for all of them. | |
4158 | */ | |
4159 | for (i = 0; i < vcdl->uniq_cols_cnt; i++) { | |
4160 | printf(" "); | |
4161 | if (use_dashes) { | |
4162 | for (j = 0; j < vcdl->uniq_cols_width[i]; j++) | |
4163 | printf("-"); | |
4164 | } else { | |
9fb2771a | 4165 | printf_color(ANSI_BOLD, "%*s", vcdl->uniq_cols_width[i], |
d6418de0 TH |
4166 | vcdl->uniq_cols[i]); |
4167 | } | |
4168 | } | |
4169 | } | |
4170 | ||
4171 | ||
193a37cb TH |
4172 | /* |
4173 | * Utility function to print out a line of dashes like: | |
4174 | * | |
4175 | * -------------------------------- ----- ----- ----- ----- ----- | |
4176 | * | |
4177 | * ...or a dashed named-row line like: | |
4178 | * | |
4179 | * logs - - - - - | |
4180 | * | |
4181 | * @cb: iostat data | |
4182 | * | |
4183 | * @force_column_width If non-zero, use the value as the column width. | |
4184 | * Otherwise use the default column widths. | |
4185 | * | |
4186 | * @name: Print a dashed named-row line starting | |
4187 | * with @name. Otherwise, print a regular | |
4188 | * dashed line. | |
4189 | */ | |
4190 | static void | |
4191 | print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width, | |
4192 | const char *name) | |
4193 | { | |
4194 | int i; | |
4195 | unsigned int namewidth; | |
4196 | uint64_t flags = cb->cb_flags; | |
4197 | uint64_t f; | |
4198 | int idx; | |
4199 | const name_and_columns_t *labels; | |
7e945072 TH |
4200 | const char *title; |
4201 | ||
4202 | ||
4203 | if (cb->cb_flags & IOS_ANYHISTO_M) { | |
4204 | title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; | |
2a673e76 | 4205 | } else if (cb->cb_vdevs.cb_names_count) { |
7e945072 TH |
4206 | title = "vdev"; |
4207 | } else { | |
4208 | title = "pool"; | |
4209 | } | |
4210 | ||
4211 | namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), | |
4212 | name ? strlen(name) : 0); | |
193a37cb | 4213 | |
193a37cb TH |
4214 | |
4215 | if (name) { | |
193a37cb TH |
4216 | printf("%-*s", namewidth, name); |
4217 | } else { | |
4218 | for (i = 0; i < namewidth; i++) | |
4219 | (void) printf("-"); | |
4220 | } | |
4221 | ||
4222 | /* For each bit in flags */ | |
4223 | for (f = flags; f; f &= ~(1ULL << idx)) { | |
4224 | unsigned int column_width; | |
4225 | idx = lowbit64(f) - 1; | |
4226 | if (force_column_width) | |
4227 | column_width = force_column_width; | |
4228 | else | |
4229 | column_width = default_column_width(cb, idx); | |
4230 | ||
4231 | labels = iostat_bottom_labels[idx]; | |
4232 | for (i = 0; i < label_array_len(labels); i++) { | |
4233 | if (name) | |
4234 | printf(" %*s-", column_width - 1, " "); | |
4235 | else | |
4236 | printf(" %.*s", column_width, | |
4237 | "--------------------"); | |
4238 | } | |
4239 | } | |
193a37cb TH |
4240 | } |
4241 | ||
4242 | ||
4243 | static void | |
4244 | print_iostat_separator_impl(iostat_cbdata_t *cb, | |
4245 | unsigned int force_column_width) | |
4246 | { | |
4247 | print_iostat_dashes(cb, force_column_width, NULL); | |
4248 | } | |
4249 | ||
34dc7c2f BB |
4250 | static void |
4251 | print_iostat_separator(iostat_cbdata_t *cb) | |
4252 | { | |
193a37cb TH |
4253 | print_iostat_separator_impl(cb, 0); |
4254 | } | |
4255 | ||
4256 | static void | |
4257 | print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width, | |
4258 | const char *histo_vdev_name) | |
4259 | { | |
4260 | unsigned int namewidth; | |
7e945072 TH |
4261 | const char *title; |
4262 | ||
7bde396a TR |
4263 | color_start(ANSI_BOLD); |
4264 | ||
7e945072 TH |
4265 | if (cb->cb_flags & IOS_ANYHISTO_M) { |
4266 | title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; | |
2a673e76 | 4267 | } else if (cb->cb_vdevs.cb_names_count) { |
7e945072 TH |
4268 | title = "vdev"; |
4269 | } else { | |
4270 | title = "pool"; | |
4271 | } | |
34dc7c2f | 4272 | |
7e945072 TH |
4273 | namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), |
4274 | histo_vdev_name ? strlen(histo_vdev_name) : 0); | |
193a37cb | 4275 | |
7e945072 | 4276 | if (histo_vdev_name) |
193a37cb TH |
4277 | printf("%-*s", namewidth, histo_vdev_name); |
4278 | else | |
4279 | printf("%*s", namewidth, ""); | |
4280 | ||
7e945072 | 4281 | |
193a37cb | 4282 | print_iostat_labels(cb, force_column_width, iostat_top_labels); |
d6418de0 | 4283 | printf("\n"); |
193a37cb | 4284 | |
7e945072 | 4285 | printf("%-*s", namewidth, title); |
193a37cb TH |
4286 | |
4287 | print_iostat_labels(cb, force_column_width, iostat_bottom_labels); | |
d6418de0 TH |
4288 | if (cb->vcdl != NULL) |
4289 | print_cmd_columns(cb->vcdl, 0); | |
4290 | ||
4291 | printf("\n"); | |
193a37cb TH |
4292 | |
4293 | print_iostat_separator_impl(cb, force_column_width); | |
d6418de0 TH |
4294 | |
4295 | if (cb->vcdl != NULL) | |
4296 | print_cmd_columns(cb->vcdl, 1); | |
4297 | ||
7bde396a TR |
4298 | color_end(); |
4299 | ||
d6418de0 | 4300 | printf("\n"); |
34dc7c2f BB |
4301 | } |
4302 | ||
4303 | static void | |
4304 | print_iostat_header(iostat_cbdata_t *cb) | |
4305 | { | |
193a37cb | 4306 | print_iostat_header_impl(cb, 0, NULL); |
34dc7c2f BB |
4307 | } |
4308 | ||
7bde396a TR |
4309 | /* |
4310 | * Prints a size string (i.e. 120M) with the suffix ("M") colored | |
4311 | * by order of magnitude. Uses column_size to add padding. | |
4312 | */ | |
4313 | static void | |
f9e1c63f | 4314 | print_stat_color(const char *statbuf, unsigned int column_size) |
7bde396a TR |
4315 | { |
4316 | fputs(" ", stdout); | |
f9e1c63f W |
4317 | size_t len = strlen(statbuf); |
4318 | while (len < column_size) { | |
4319 | fputc(' ', stdout); | |
4320 | column_size--; | |
4321 | } | |
7bde396a TR |
4322 | if (*statbuf == '0') { |
4323 | color_start(ANSI_GRAY); | |
4324 | fputc('0', stdout); | |
7bde396a TR |
4325 | } else { |
4326 | for (; *statbuf; statbuf++) { | |
4327 | if (*statbuf == 'K') color_start(ANSI_GREEN); | |
4328 | else if (*statbuf == 'M') color_start(ANSI_YELLOW); | |
4329 | else if (*statbuf == 'G') color_start(ANSI_RED); | |
4330 | else if (*statbuf == 'T') color_start(ANSI_BOLD_BLUE); | |
4331 | else if (*statbuf == 'P') color_start(ANSI_MAGENTA); | |
4332 | else if (*statbuf == 'E') color_start(ANSI_CYAN); | |
4333 | fputc(*statbuf, stdout); | |
4334 | if (--column_size <= 0) | |
4335 | break; | |
4336 | } | |
4337 | } | |
4338 | color_end(); | |
7bde396a | 4339 | } |
193a37cb | 4340 | |
34dc7c2f BB |
4341 | /* |
4342 | * Display a single statistic. | |
4343 | */ | |
4344 | static void | |
193a37cb TH |
4345 | print_one_stat(uint64_t value, enum zfs_nicenum_format format, |
4346 | unsigned int column_size, boolean_t scripted) | |
34dc7c2f BB |
4347 | { |
4348 | char buf[64]; | |
4349 | ||
193a37cb TH |
4350 | zfs_nicenum_format(value, buf, sizeof (buf), format); |
4351 | ||
4352 | if (scripted) | |
4353 | printf("\t%s", buf); | |
4354 | else | |
7bde396a | 4355 | print_stat_color(buf, column_size); |
193a37cb TH |
4356 | } |
4357 | ||
4358 | /* | |
4359 | * Calculate the default vdev stats | |
4360 | * | |
4361 | * Subtract oldvs from newvs, apply a scaling factor, and save the resulting | |
4362 | * stats into calcvs. | |
4363 | */ | |
4364 | static void | |
4365 | calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs, | |
4366 | vdev_stat_t *calcvs) | |
4367 | { | |
4368 | int i; | |
4369 | ||
4370 | memcpy(calcvs, newvs, sizeof (*calcvs)); | |
4371 | for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++) | |
4372 | calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]); | |
4373 | ||
4374 | for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++) | |
4375 | calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]); | |
4376 | } | |
4377 | ||
4378 | /* | |
4379 | * Internal representation of the extended iostats data. | |
4380 | * | |
4381 | * The extended iostat stats are exported in nvlists as either uint64_t arrays | |
4382 | * or single uint64_t's. We make both look like arrays to make them easier | |
4383 | * to process. In order to make single uint64_t's look like arrays, we set | |
4384 | * __data to the stat data, and then set *data = &__data with count = 1. Then, | |
4385 | * we can just use *data and count. | |
4386 | */ | |
4387 | struct stat_array { | |
4388 | uint64_t *data; | |
4389 | uint_t count; /* Number of entries in data[] */ | |
4390 | uint64_t __data; /* Only used when data is a single uint64_t */ | |
4391 | }; | |
4392 | ||
4393 | static uint64_t | |
4ea3f864 GM |
4394 | stat_histo_max(struct stat_array *nva, unsigned int len) |
4395 | { | |
193a37cb TH |
4396 | uint64_t max = 0; |
4397 | int i; | |
4398 | for (i = 0; i < len; i++) | |
4399 | max = MAX(max, array64_max(nva[i].data, nva[i].count)); | |
4400 | ||
4401 | return (max); | |
4402 | } | |
4403 | ||
4404 | /* | |
4405 | * Helper function to lookup a uint64_t array or uint64_t value and store its | |
4406 | * data as a stat_array. If the nvpair is a single uint64_t value, then we make | |
4407 | * it look like a one element array to make it easier to process. | |
4408 | */ | |
4409 | static int | |
4410 | nvpair64_to_stat_array(nvlist_t *nvl, const char *name, | |
4ea3f864 GM |
4411 | struct stat_array *nva) |
4412 | { | |
193a37cb TH |
4413 | nvpair_t *tmp; |
4414 | int ret; | |
4415 | ||
4416 | verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0); | |
4417 | switch (nvpair_type(tmp)) { | |
4418 | case DATA_TYPE_UINT64_ARRAY: | |
4419 | ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count); | |
4420 | break; | |
4421 | case DATA_TYPE_UINT64: | |
4422 | ret = nvpair_value_uint64(tmp, &nva->__data); | |
4423 | nva->data = &nva->__data; | |
4424 | nva->count = 1; | |
4425 | break; | |
4426 | default: | |
4427 | /* Not a uint64_t */ | |
4428 | ret = EINVAL; | |
4429 | break; | |
4430 | } | |
4431 | ||
4432 | return (ret); | |
4433 | } | |
4434 | ||
4435 | /* | |
4436 | * Given a list of nvlist names, look up the extended stats in newnv and oldnv, | |
4437 | * subtract them, and return the results in a newly allocated stat_array. | |
4438 | * You must free the returned array after you are done with it with | |
4439 | * free_calc_stats(). | |
4440 | * | |
4441 | * Additionally, you can set "oldnv" to NULL if you simply want the newnv | |
4442 | * values. | |
4443 | */ | |
4444 | static struct stat_array * | |
4445 | calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv, | |
4446 | nvlist_t *newnv) | |
4447 | { | |
4448 | nvlist_t *oldnvx = NULL, *newnvx; | |
4449 | struct stat_array *oldnva, *newnva, *calcnva; | |
4450 | int i, j; | |
4451 | unsigned int alloc_size = (sizeof (struct stat_array)) * len; | |
4452 | ||
4453 | /* Extract our extended stats nvlist from the main list */ | |
4454 | verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX, | |
4455 | &newnvx) == 0); | |
4456 | if (oldnv) { | |
4457 | verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX, | |
4458 | &oldnvx) == 0); | |
4459 | } | |
4460 | ||
4461 | newnva = safe_malloc(alloc_size); | |
4462 | oldnva = safe_malloc(alloc_size); | |
4463 | calcnva = safe_malloc(alloc_size); | |
4464 | ||
4465 | for (j = 0; j < len; j++) { | |
4466 | verify(nvpair64_to_stat_array(newnvx, names[j], | |
4467 | &newnva[j]) == 0); | |
4468 | calcnva[j].count = newnva[j].count; | |
4469 | alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]); | |
4470 | calcnva[j].data = safe_malloc(alloc_size); | |
4471 | memcpy(calcnva[j].data, newnva[j].data, alloc_size); | |
4472 | ||
4473 | if (oldnvx) { | |
4474 | verify(nvpair64_to_stat_array(oldnvx, names[j], | |
4475 | &oldnva[j]) == 0); | |
4476 | for (i = 0; i < oldnva[j].count; i++) | |
4477 | calcnva[j].data[i] -= oldnva[j].data[i]; | |
4478 | } | |
4479 | } | |
4480 | free(newnva); | |
4481 | free(oldnva); | |
4482 | return (calcnva); | |
4483 | } | |
4484 | ||
4485 | static void | |
4486 | free_calc_stats(struct stat_array *nva, unsigned int len) | |
4487 | { | |
4488 | int i; | |
4489 | for (i = 0; i < len; i++) | |
4490 | free(nva[i].data); | |
4491 | ||
4492 | free(nva); | |
4493 | } | |
4494 | ||
4495 | static void | |
4496 | print_iostat_histo(struct stat_array *nva, unsigned int len, | |
4497 | iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth, | |
4498 | double scale) | |
4499 | { | |
4500 | int i, j; | |
4501 | char buf[6]; | |
4502 | uint64_t val; | |
4503 | enum zfs_nicenum_format format; | |
4504 | unsigned int buckets; | |
7e945072 | 4505 | unsigned int start_bucket; |
193a37cb TH |
4506 | |
4507 | if (cb->cb_literal) | |
4508 | format = ZFS_NICENUM_RAW; | |
4509 | else | |
4510 | format = ZFS_NICENUM_1024; | |
4511 | ||
4512 | /* All these histos are the same size, so just use nva[0].count */ | |
4513 | buckets = nva[0].count; | |
4514 | ||
7e945072 TH |
4515 | if (cb->cb_flags & IOS_RQ_HISTO_M) { |
4516 | /* Start at 512 - req size should never be lower than this */ | |
4517 | start_bucket = 9; | |
4518 | } else { | |
4519 | start_bucket = 0; | |
4520 | } | |
193a37cb | 4521 | |
7e945072 | 4522 | for (j = start_bucket; j < buckets; j++) { |
193a37cb | 4523 | /* Print histogram bucket label */ |
7e945072 TH |
4524 | if (cb->cb_flags & IOS_L_HISTO_M) { |
4525 | /* Ending range of this bucket */ | |
4526 | val = (1UL << (j + 1)) - 1; | |
4527 | zfs_nicetime(val, buf, sizeof (buf)); | |
4528 | } else { | |
4529 | /* Request size (starting range of bucket) */ | |
4530 | val = (1UL << j); | |
4531 | zfs_nicenum(val, buf, sizeof (buf)); | |
4532 | } | |
4533 | ||
193a37cb | 4534 | if (cb->cb_scripted) |
02730c33 | 4535 | printf("%llu", (u_longlong_t)val); |
193a37cb TH |
4536 | else |
4537 | printf("%-*s", namewidth, buf); | |
4538 | ||
4539 | /* Print the values on the line */ | |
4540 | for (i = 0; i < len; i++) { | |
4541 | print_one_stat(nva[i].data[j] * scale, format, | |
4542 | column_width, cb->cb_scripted); | |
4543 | } | |
4544 | printf("\n"); | |
4545 | } | |
4546 | } | |
4547 | ||
4548 | static void | |
4549 | print_solid_separator(unsigned int length) | |
4550 | { | |
4551 | while (length--) | |
4552 | printf("-"); | |
4553 | printf("\n"); | |
4554 | } | |
4555 | ||
4556 | static void | |
4557 | print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv, | |
4558 | nvlist_t *newnv, double scale, const char *name) | |
4559 | { | |
4560 | unsigned int column_width; | |
4561 | unsigned int namewidth; | |
4562 | unsigned int entire_width; | |
7e945072 | 4563 | enum iostat_type type; |
193a37cb | 4564 | struct stat_array *nva; |
7e945072 TH |
4565 | const char **names; |
4566 | unsigned int names_len; | |
4567 | ||
4568 | /* What type of histo are we? */ | |
4569 | type = IOS_HISTO_IDX(cb->cb_flags); | |
4570 | ||
4571 | /* Get NULL-terminated array of nvlist names for our histo */ | |
4572 | names = vsx_type_to_nvlist[type]; | |
4573 | names_len = str_array_len(names); /* num of names */ | |
4574 | ||
4575 | nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv); | |
193a37cb TH |
4576 | |
4577 | if (cb->cb_literal) { | |
4578 | column_width = MAX(5, | |
7e945072 | 4579 | (unsigned int) log10(stat_histo_max(nva, names_len)) + 1); |
193a37cb TH |
4580 | } else { |
4581 | column_width = 5; | |
4582 | } | |
4583 | ||
7e945072 TH |
4584 | namewidth = MAX(cb->cb_namewidth, |
4585 | strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)])); | |
193a37cb TH |
4586 | |
4587 | /* | |
4588 | * Calculate the entire line width of what we're printing. The | |
4589 | * +2 is for the two spaces between columns: | |
4590 | */ | |
4591 | /* read write */ | |
4592 | /* ----- ----- */ | |
4593 | /* |___| <---------- column_width */ | |
4594 | /* */ | |
4595 | /* |__________| <--- entire_width */ | |
4596 | /* */ | |
4597 | entire_width = namewidth + (column_width + 2) * | |
7e945072 | 4598 | label_array_len(iostat_bottom_labels[type]); |
193a37cb TH |
4599 | |
4600 | if (cb->cb_scripted) | |
4601 | printf("%s\n", name); | |
4602 | else | |
4603 | print_iostat_header_impl(cb, column_width, name); | |
4604 | ||
7e945072 | 4605 | print_iostat_histo(nva, names_len, cb, column_width, |
193a37cb TH |
4606 | namewidth, scale); |
4607 | ||
7e945072 | 4608 | free_calc_stats(nva, names_len); |
193a37cb TH |
4609 | if (!cb->cb_scripted) |
4610 | print_solid_separator(entire_width); | |
4611 | } | |
4612 | ||
4613 | /* | |
4614 | * Calculate the average latency of a power-of-two latency histogram | |
4615 | */ | |
4616 | static uint64_t | |
4617 | single_histo_average(uint64_t *histo, unsigned int buckets) | |
4618 | { | |
4619 | int i; | |
4620 | uint64_t count = 0, total = 0; | |
4621 | ||
4622 | for (i = 0; i < buckets; i++) { | |
4623 | /* | |
4624 | * Our buckets are power-of-two latency ranges. Use the | |
4625 | * midpoint latency of each bucket to calculate the average. | |
4626 | * For example: | |
4627 | * | |
4628 | * Bucket Midpoint | |
4629 | * 8ns-15ns: 12ns | |
4630 | * 16ns-31ns: 24ns | |
4631 | * ... | |
4632 | */ | |
4633 | if (histo[i] != 0) { | |
4634 | total += histo[i] * (((1UL << i) + ((1UL << i)/2))); | |
4635 | count += histo[i]; | |
4636 | } | |
4637 | } | |
4638 | ||
4639 | /* Prevent divide by zero */ | |
4640 | return (count == 0 ? 0 : total / count); | |
4641 | } | |
4642 | ||
4643 | static void | |
e40ca391 | 4644 | print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *newnv) |
193a37cb | 4645 | { |
193a37cb TH |
4646 | const char *names[] = { |
4647 | ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE, | |
4648 | ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, | |
4649 | ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE, | |
4650 | ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, | |
4651 | ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE, | |
4652 | ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, | |
4653 | ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE, | |
4654 | ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, | |
4655 | ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE, | |
4656 | ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, | |
1b939560 BB |
4657 | ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE, |
4658 | ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE, | |
00888c08 TB |
4659 | ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE, |
4660 | ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE, | |
193a37cb TH |
4661 | }; |
4662 | ||
4663 | struct stat_array *nva; | |
4664 | ||
4665 | unsigned int column_width = default_column_width(cb, IOS_QUEUES); | |
4666 | enum zfs_nicenum_format format; | |
4667 | ||
4668 | nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv); | |
4669 | ||
4670 | if (cb->cb_literal) | |
4671 | format = ZFS_NICENUM_RAW; | |
4672 | else | |
4673 | format = ZFS_NICENUM_1024; | |
4674 | ||
e40ca391 AZ |
4675 | for (int i = 0; i < ARRAY_SIZE(names); i++) { |
4676 | uint64_t val = nva[i].data[0]; | |
193a37cb TH |
4677 | print_one_stat(val, format, column_width, cb->cb_scripted); |
4678 | } | |
4679 | ||
4680 | free_calc_stats(nva, ARRAY_SIZE(names)); | |
4681 | } | |
4682 | ||
4683 | static void | |
4684 | print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv, | |
b954e36e | 4685 | nvlist_t *newnv) |
193a37cb TH |
4686 | { |
4687 | int i; | |
4688 | uint64_t val; | |
4689 | const char *names[] = { | |
4690 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
4691 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
4692 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
4693 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
4694 | ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, | |
4695 | ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, | |
4696 | ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, | |
4697 | ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, | |
4698 | ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, | |
1b939560 | 4699 | ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO, |
00888c08 | 4700 | ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO, |
193a37cb TH |
4701 | }; |
4702 | struct stat_array *nva; | |
4703 | ||
4704 | unsigned int column_width = default_column_width(cb, IOS_LATENCY); | |
4705 | enum zfs_nicenum_format format; | |
4706 | ||
4707 | nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv); | |
4708 | ||
4709 | if (cb->cb_literal) | |
29eb4942 | 4710 | format = ZFS_NICENUM_RAWTIME; |
193a37cb TH |
4711 | else |
4712 | format = ZFS_NICENUM_TIME; | |
4713 | ||
4714 | /* Print our avg latencies on the line */ | |
4715 | for (i = 0; i < ARRAY_SIZE(names); i++) { | |
4716 | /* Compute average latency for a latency histo */ | |
b954e36e | 4717 | val = single_histo_average(nva[i].data, nva[i].count); |
193a37cb TH |
4718 | print_one_stat(val, format, column_width, cb->cb_scripted); |
4719 | } | |
4720 | free_calc_stats(nva, ARRAY_SIZE(names)); | |
4721 | } | |
4722 | ||
4723 | /* | |
4724 | * Print default statistics (capacity/operations/bandwidth) | |
4725 | */ | |
4726 | static void | |
4727 | print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale) | |
4728 | { | |
4729 | unsigned int column_width = default_column_width(cb, IOS_DEFAULT); | |
4730 | enum zfs_nicenum_format format; | |
4731 | char na; /* char to print for "not applicable" values */ | |
4732 | ||
4733 | if (cb->cb_literal) { | |
4734 | format = ZFS_NICENUM_RAW; | |
4735 | na = '0'; | |
4736 | } else { | |
4737 | format = ZFS_NICENUM_1024; | |
4738 | na = '-'; | |
4739 | } | |
4740 | ||
4741 | /* only toplevel vdevs have capacity stats */ | |
4742 | if (vs->vs_space == 0) { | |
4743 | if (cb->cb_scripted) | |
4744 | printf("\t%c\t%c", na, na); | |
4745 | else | |
4746 | printf(" %*c %*c", column_width, na, column_width, | |
4747 | na); | |
4748 | } else { | |
4749 | print_one_stat(vs->vs_alloc, format, column_width, | |
4750 | cb->cb_scripted); | |
4751 | print_one_stat(vs->vs_space - vs->vs_alloc, format, | |
4752 | column_width, cb->cb_scripted); | |
4753 | } | |
4754 | ||
4755 | print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale), | |
4756 | format, column_width, cb->cb_scripted); | |
4757 | print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale), | |
4758 | format, column_width, cb->cb_scripted); | |
4759 | print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale), | |
4760 | format, column_width, cb->cb_scripted); | |
4761 | print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale), | |
4762 | format, column_width, cb->cb_scripted); | |
34dc7c2f BB |
4763 | } |
4764 | ||
cf5a9d8c | 4765 | static const char *const class_name[] = { |
cc99f275 DB |
4766 | VDEV_ALLOC_BIAS_DEDUP, |
4767 | VDEV_ALLOC_BIAS_SPECIAL, | |
4768 | VDEV_ALLOC_CLASS_LOGS | |
4769 | }; | |
4770 | ||
34dc7c2f BB |
4771 | /* |
4772 | * Print out all the statistics for the given vdev. This can either be the | |
4773 | * toplevel configuration, or called recursively. If 'name' is NULL, then this | |
4774 | * is a verbose output, and we don't want to display the toplevel pool stats. | |
193a37cb TH |
4775 | * |
4776 | * Returns the number of stat lines printed. | |
34dc7c2f | 4777 | */ |
cc99f275 | 4778 | static unsigned int |
34dc7c2f BB |
4779 | print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, |
4780 | nvlist_t *newnv, iostat_cbdata_t *cb, int depth) | |
4781 | { | |
4782 | nvlist_t **oldchild, **newchild; | |
674b8934 | 4783 | uint_t c, children, oldchildren; |
193a37cb | 4784 | vdev_stat_t *oldvs, *newvs, *calcvs; |
34dc7c2f | 4785 | vdev_stat_t zerovs = { 0 }; |
193a37cb TH |
4786 | char *vname; |
4787 | int i; | |
4788 | int ret = 0; | |
34dc7c2f BB |
4789 | uint64_t tdelta; |
4790 | double scale; | |
193a37cb | 4791 | |
a1d477c2 MA |
4792 | if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) |
4793 | return (ret); | |
4794 | ||
75c62019 JCML |
4795 | calcvs = safe_malloc(sizeof (*calcvs)); |
4796 | ||
34dc7c2f | 4797 | if (oldnv != NULL) { |
428870ff BB |
4798 | verify(nvlist_lookup_uint64_array(oldnv, |
4799 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); | |
34dc7c2f BB |
4800 | } else { |
4801 | oldvs = &zerovs; | |
4802 | } | |
4803 | ||
193a37cb | 4804 | /* Do we only want to see a specific vdev? */ |
2a673e76 | 4805 | for (i = 0; i < cb->cb_vdevs.cb_names_count; i++) { |
193a37cb | 4806 | /* Yes we do. Is this the vdev? */ |
2a673e76 | 4807 | if (strcmp(name, cb->cb_vdevs.cb_names[i]) == 0) { |
193a37cb TH |
4808 | /* |
4809 | * This is our vdev. Since it is the only vdev we | |
4810 | * will be displaying, make depth = 0 so that it | |
4811 | * doesn't get indented. | |
4812 | */ | |
4813 | depth = 0; | |
4814 | break; | |
4815 | } | |
4816 | } | |
4817 | ||
2a673e76 | 4818 | if (cb->cb_vdevs.cb_names_count && (i == cb->cb_vdevs.cb_names_count)) { |
193a37cb TH |
4819 | /* Couldn't match the name */ |
4820 | goto children; | |
4821 | } | |
4822 | ||
4823 | ||
428870ff | 4824 | verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
4825 | (uint64_t **)&newvs, &c) == 0); |
4826 | ||
193a37cb TH |
4827 | /* |
4828 | * Print the vdev name unless it's is a histogram. Histograms | |
4829 | * display the vdev name in the header itself. | |
4830 | */ | |
7e945072 | 4831 | if (!(cb->cb_flags & IOS_ANYHISTO_M)) { |
193a37cb TH |
4832 | if (cb->cb_scripted) { |
4833 | printf("%s", name); | |
4834 | } else { | |
4835 | if (strlen(name) + depth > cb->cb_namewidth) | |
4836 | (void) printf("%*s%s", depth, "", name); | |
4837 | else | |
4838 | (void) printf("%*s%s%*s", depth, "", name, | |
4839 | (int)(cb->cb_namewidth - strlen(name) - | |
4840 | depth), ""); | |
4841 | } | |
4842 | } | |
34dc7c2f | 4843 | |
193a37cb | 4844 | /* Calculate our scaling factor */ |
34dc7c2f | 4845 | tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; |
7e945072 | 4846 | if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) { |
193a37cb TH |
4847 | /* |
4848 | * If we specify printing histograms with no time interval, then | |
4849 | * print the histogram numbers over the entire lifetime of the | |
4850 | * vdev. | |
4851 | */ | |
4852 | scale = 1; | |
34dc7c2f | 4853 | } else { |
193a37cb TH |
4854 | if (tdelta == 0) |
4855 | scale = 1.0; | |
4856 | else | |
4857 | scale = (double)NANOSEC / tdelta; | |
34dc7c2f BB |
4858 | } |
4859 | ||
193a37cb TH |
4860 | if (cb->cb_flags & IOS_DEFAULT_M) { |
4861 | calc_default_iostats(oldvs, newvs, calcvs); | |
4862 | print_iostat_default(calcvs, cb, scale); | |
4863 | } | |
4864 | if (cb->cb_flags & IOS_LATENCY_M) | |
b954e36e | 4865 | print_iostat_latency(cb, oldnv, newnv); |
193a37cb | 4866 | if (cb->cb_flags & IOS_QUEUES_M) |
e40ca391 | 4867 | print_iostat_queues(cb, newnv); |
7e945072 | 4868 | if (cb->cb_flags & IOS_ANYHISTO_M) { |
193a37cb TH |
4869 | printf("\n"); |
4870 | print_iostat_histos(cb, oldnv, newnv, scale, name); | |
4871 | } | |
34dc7c2f | 4872 | |
8720e9e7 | 4873 | if (cb->vcdl != NULL) { |
d1807f16 | 4874 | const char *path; |
8720e9e7 TH |
4875 | if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH, |
4876 | &path) == 0) { | |
d6418de0 | 4877 | printf(" "); |
8720e9e7 | 4878 | zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); |
8720e9e7 TH |
4879 | } |
4880 | } | |
4881 | ||
7e945072 | 4882 | if (!(cb->cb_flags & IOS_ANYHISTO_M)) |
193a37cb | 4883 | printf("\n"); |
34dc7c2f | 4884 | |
193a37cb | 4885 | ret++; |
34dc7c2f | 4886 | |
193a37cb | 4887 | children: |
92bc9278 | 4888 | |
4889 | free(calcvs); | |
4890 | ||
34dc7c2f | 4891 | if (!cb->cb_verbose) |
193a37cb | 4892 | return (ret); |
34dc7c2f BB |
4893 | |
4894 | if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, | |
4895 | &newchild, &children) != 0) | |
193a37cb | 4896 | return (ret); |
34dc7c2f | 4897 | |
674b8934 TH |
4898 | if (oldnv) { |
4899 | if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, | |
4900 | &oldchild, &oldchildren) != 0) | |
4901 | return (ret); | |
4902 | ||
4903 | children = MIN(oldchildren, children); | |
4904 | } | |
34dc7c2f | 4905 | |
cc99f275 DB |
4906 | /* |
4907 | * print normal top-level devices | |
4908 | */ | |
34dc7c2f | 4909 | for (c = 0; c < children; c++) { |
187632dc | 4910 | uint64_t ishole = B_FALSE, islog = B_FALSE; |
428870ff | 4911 | |
187632dc MH |
4912 | (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, |
4913 | &ishole); | |
4914 | ||
4915 | (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, | |
4916 | &islog); | |
4917 | ||
4918 | if (ishole || islog) | |
428870ff BB |
4919 | continue; |
4920 | ||
cc99f275 DB |
4921 | if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) |
4922 | continue; | |
4923 | ||
d2f3e292 | 4924 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], |
7b468ed2 | 4925 | cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID); |
193a37cb | 4926 | ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, |
34dc7c2f BB |
4927 | newchild[c], cb, depth + 2); |
4928 | free(vname); | |
4929 | } | |
4930 | ||
187632dc | 4931 | /* |
cc99f275 | 4932 | * print all other top-level devices |
187632dc | 4933 | */ |
cf5a9d8c | 4934 | for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) { |
cc99f275 | 4935 | boolean_t printed = B_FALSE; |
187632dc MH |
4936 | |
4937 | for (c = 0; c < children; c++) { | |
4938 | uint64_t islog = B_FALSE; | |
d1807f16 RY |
4939 | const char *bias = NULL; |
4940 | const char *type = NULL; | |
cc99f275 | 4941 | |
187632dc MH |
4942 | (void) nvlist_lookup_uint64(newchild[c], |
4943 | ZPOOL_CONFIG_IS_LOG, &islog); | |
187632dc | 4944 | if (islog) { |
d1807f16 | 4945 | bias = VDEV_ALLOC_CLASS_LOGS; |
cc99f275 DB |
4946 | } else { |
4947 | (void) nvlist_lookup_string(newchild[c], | |
4948 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
4949 | (void) nvlist_lookup_string(newchild[c], | |
4950 | ZPOOL_CONFIG_TYPE, &type); | |
187632dc | 4951 | } |
cc99f275 DB |
4952 | if (bias == NULL || strcmp(bias, class_name[n]) != 0) |
4953 | continue; | |
4954 | if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
4955 | continue; | |
187632dc | 4956 | |
cc99f275 DB |
4957 | if (!printed) { |
4958 | if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && | |
2a673e76 AJ |
4959 | !cb->cb_scripted && |
4960 | !cb->cb_vdevs.cb_names) { | |
cc99f275 DB |
4961 | print_iostat_dashes(cb, 0, |
4962 | class_name[n]); | |
4963 | } | |
4964 | printf("\n"); | |
4965 | printed = B_TRUE; | |
4966 | } | |
4967 | ||
4968 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], | |
7b468ed2 | 4969 | cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID); |
cc99f275 DB |
4970 | ret += print_vdev_stats(zhp, vname, oldnv ? |
4971 | oldchild[c] : NULL, newchild[c], cb, depth + 2); | |
4972 | free(vname); | |
4973 | } | |
187632dc MH |
4974 | } |
4975 | ||
34dc7c2f BB |
4976 | /* |
4977 | * Include level 2 ARC devices in iostat output | |
4978 | */ | |
4979 | if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, | |
4980 | &newchild, &children) != 0) | |
193a37cb | 4981 | return (ret); |
34dc7c2f | 4982 | |
674b8934 TH |
4983 | if (oldnv) { |
4984 | if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, | |
4985 | &oldchild, &oldchildren) != 0) | |
4986 | return (ret); | |
4987 | ||
4988 | children = MIN(oldchildren, children); | |
4989 | } | |
34dc7c2f BB |
4990 | |
4991 | if (children > 0) { | |
7e945072 | 4992 | if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted && |
2a673e76 | 4993 | !cb->cb_vdevs.cb_names) { |
193a37cb TH |
4994 | print_iostat_dashes(cb, 0, "cache"); |
4995 | } | |
5034557b | 4996 | printf("\n"); |
193a37cb | 4997 | |
34dc7c2f | 4998 | for (c = 0; c < children; c++) { |
428870ff | 4999 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], |
2a673e76 | 5000 | cb->cb_vdevs.cb_name_flags); |
193a37cb TH |
5001 | ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] |
5002 | : NULL, newchild[c], cb, depth + 2); | |
34dc7c2f BB |
5003 | free(vname); |
5004 | } | |
5005 | } | |
193a37cb TH |
5006 | |
5007 | return (ret); | |
34dc7c2f BB |
5008 | } |
5009 | ||
5010 | static int | |
5011 | refresh_iostat(zpool_handle_t *zhp, void *data) | |
5012 | { | |
5013 | iostat_cbdata_t *cb = data; | |
5014 | boolean_t missing; | |
5015 | ||
5016 | /* | |
5017 | * If the pool has disappeared, remove it from the list and continue. | |
5018 | */ | |
5019 | if (zpool_refresh_stats(zhp, &missing) != 0) | |
5020 | return (-1); | |
5021 | ||
5022 | if (missing) | |
5023 | pool_list_remove(cb->cb_list, zhp); | |
5024 | ||
5025 | return (0); | |
5026 | } | |
5027 | ||
5028 | /* | |
5029 | * Callback to print out the iostats for the given pool. | |
5030 | */ | |
65c7cc49 | 5031 | static int |
34dc7c2f BB |
5032 | print_iostat(zpool_handle_t *zhp, void *data) |
5033 | { | |
5034 | iostat_cbdata_t *cb = data; | |
5035 | nvlist_t *oldconfig, *newconfig; | |
5036 | nvlist_t *oldnvroot, *newnvroot; | |
193a37cb | 5037 | int ret; |
34dc7c2f BB |
5038 | |
5039 | newconfig = zpool_get_config(zhp, &oldconfig); | |
5040 | ||
5041 | if (cb->cb_iteration == 1) | |
5042 | oldconfig = NULL; | |
5043 | ||
5044 | verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, | |
5045 | &newnvroot) == 0); | |
5046 | ||
5047 | if (oldconfig == NULL) | |
5048 | oldnvroot = NULL; | |
5049 | else | |
5050 | verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, | |
5051 | &oldnvroot) == 0); | |
5052 | ||
193a37cb | 5053 | ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, |
02730c33 | 5054 | cb, 0); |
7e945072 | 5055 | if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) && |
2a673e76 AJ |
5056 | !cb->cb_scripted && cb->cb_verbose && |
5057 | !cb->cb_vdevs.cb_names_count) { | |
7e945072 | 5058 | print_iostat_separator(cb); |
d6418de0 TH |
5059 | if (cb->vcdl != NULL) { |
5060 | print_cmd_columns(cb->vcdl, 1); | |
5061 | } | |
5062 | printf("\n"); | |
7e945072 | 5063 | } |
34dc7c2f | 5064 | |
193a37cb | 5065 | return (ret); |
34dc7c2f BB |
5066 | } |
5067 | ||
9fc60702 CS |
5068 | static int |
5069 | get_columns(void) | |
5070 | { | |
5071 | struct winsize ws; | |
5072 | int columns = 80; | |
5073 | int error; | |
5074 | ||
5075 | if (isatty(STDOUT_FILENO)) { | |
5076 | error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); | |
5077 | if (error == 0) | |
5078 | columns = ws.ws_col; | |
5079 | } else { | |
5080 | columns = 999; | |
5081 | } | |
5082 | ||
d1d7e268 | 5083 | return (columns); |
9fc60702 CS |
5084 | } |
5085 | ||
c5eea0ab BB |
5086 | /* |
5087 | * Return the required length of the pool/vdev name column. The minimum | |
5088 | * allowed width and output formatting flags must be provided. | |
5089 | */ | |
5090 | static int | |
5091 | get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose) | |
34dc7c2f | 5092 | { |
34dc7c2f | 5093 | nvlist_t *config, *nvroot; |
c5eea0ab | 5094 | int width = min_width; |
34dc7c2f BB |
5095 | |
5096 | if ((config = zpool_get_config(zhp, NULL)) != NULL) { | |
5097 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
5098 | &nvroot) == 0); | |
06b80506 | 5099 | size_t poolname_len = strlen(zpool_get_name(zhp)); |
c5eea0ab BB |
5100 | if (verbose == B_FALSE) { |
5101 | width = MAX(poolname_len, min_width); | |
5102 | } else { | |
5103 | width = MAX(poolname_len, | |
5104 | max_width(zhp, nvroot, 0, min_width, flags)); | |
5105 | } | |
34dc7c2f | 5106 | } |
9fc60702 | 5107 | |
c5eea0ab | 5108 | return (width); |
34dc7c2f BB |
5109 | } |
5110 | ||
5111 | /* | |
428870ff | 5112 | * Parse the input string, get the 'interval' and 'count' value if there is one. |
34dc7c2f | 5113 | */ |
428870ff | 5114 | static void |
193a37cb | 5115 | get_interval_count(int *argcp, char **argv, float *iv, |
428870ff | 5116 | unsigned long *cnt) |
34dc7c2f | 5117 | { |
193a37cb TH |
5118 | float interval = 0; |
5119 | unsigned long count = 0; | |
1fde1e37 | 5120 | int argc = *argcp; |
34dc7c2f BB |
5121 | |
5122 | /* | |
5123 | * Determine if the last argument is an integer or a pool name | |
5124 | */ | |
3283f137 | 5125 | if (argc > 0 && zfs_isnumber(argv[argc - 1])) { |
34dc7c2f BB |
5126 | char *end; |
5127 | ||
5128 | errno = 0; | |
193a37cb | 5129 | interval = strtof(argv[argc - 1], &end); |
34dc7c2f BB |
5130 | |
5131 | if (*end == '\0' && errno == 0) { | |
5132 | if (interval == 0) { | |
61b50107 AZ |
5133 | (void) fprintf(stderr, gettext( |
5134 | "interval cannot be zero\n")); | |
34dc7c2f BB |
5135 | usage(B_FALSE); |
5136 | } | |
34dc7c2f BB |
5137 | /* |
5138 | * Ignore the last parameter | |
5139 | */ | |
5140 | argc--; | |
5141 | } else { | |
5142 | /* | |
5143 | * If this is not a valid number, just plow on. The | |
5144 | * user will get a more informative error message later | |
5145 | * on. | |
5146 | */ | |
5147 | interval = 0; | |
5148 | } | |
5149 | } | |
5150 | ||
5151 | /* | |
5152 | * If the last argument is also an integer, then we have both a count | |
428870ff | 5153 | * and an interval. |
34dc7c2f | 5154 | */ |
3283f137 | 5155 | if (argc > 0 && zfs_isnumber(argv[argc - 1])) { |
34dc7c2f BB |
5156 | char *end; |
5157 | ||
5158 | errno = 0; | |
5159 | count = interval; | |
193a37cb | 5160 | interval = strtof(argv[argc - 1], &end); |
34dc7c2f BB |
5161 | |
5162 | if (*end == '\0' && errno == 0) { | |
5163 | if (interval == 0) { | |
61b50107 AZ |
5164 | (void) fprintf(stderr, gettext( |
5165 | "interval cannot be zero\n")); | |
34dc7c2f BB |
5166 | usage(B_FALSE); |
5167 | } | |
5168 | ||
5169 | /* | |
5170 | * Ignore the last parameter | |
5171 | */ | |
5172 | argc--; | |
5173 | } else { | |
5174 | interval = 0; | |
5175 | } | |
5176 | } | |
5177 | ||
428870ff BB |
5178 | *iv = interval; |
5179 | *cnt = count; | |
5180 | *argcp = argc; | |
5181 | } | |
5182 | ||
5183 | static void | |
5184 | get_timestamp_arg(char c) | |
5185 | { | |
5186 | if (c == 'u') | |
5187 | timestamp_fmt = UDATE; | |
5188 | else if (c == 'd') | |
5189 | timestamp_fmt = DDATE; | |
5190 | else | |
5191 | usage(B_FALSE); | |
5192 | } | |
5193 | ||
5194 | /* | |
193a37cb TH |
5195 | * Return stat flags that are supported by all pools by both the module and |
5196 | * zpool iostat. "*data" should be initialized to all 0xFFs before running. | |
5197 | * It will get ANDed down until only the flags that are supported on all pools | |
5198 | * remain. | |
5199 | */ | |
5200 | static int | |
5201 | get_stat_flags_cb(zpool_handle_t *zhp, void *data) | |
5202 | { | |
5203 | uint64_t *mask = data; | |
5204 | nvlist_t *config, *nvroot, *nvx; | |
5205 | uint64_t flags = 0; | |
5206 | int i, j; | |
5207 | ||
193a37cb TH |
5208 | config = zpool_get_config(zhp, NULL); |
5209 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
5210 | &nvroot) == 0); | |
5211 | ||
5212 | /* Default stats are always supported, but for completeness.. */ | |
5213 | if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS)) | |
5214 | flags |= IOS_DEFAULT_M; | |
5215 | ||
5216 | /* Get our extended stats nvlist from the main list */ | |
5217 | if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX, | |
5218 | &nvx) != 0) { | |
5219 | /* | |
5220 | * No extended stats; they're probably running an older | |
5221 | * module. No big deal, we support that too. | |
5222 | */ | |
5223 | goto end; | |
5224 | } | |
5225 | ||
5226 | /* For each extended stat, make sure all its nvpairs are supported */ | |
5227 | for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) { | |
5228 | if (!vsx_type_to_nvlist[j][0]) | |
5229 | continue; | |
5230 | ||
5231 | /* Start off by assuming the flag is supported, then check */ | |
5232 | flags |= (1ULL << j); | |
5233 | for (i = 0; vsx_type_to_nvlist[j][i]; i++) { | |
5234 | if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) { | |
5235 | /* flag isn't supported */ | |
5236 | flags = flags & ~(1ULL << j); | |
5237 | break; | |
5238 | } | |
5239 | } | |
5240 | } | |
5241 | end: | |
5242 | *mask = *mask & flags; | |
5243 | return (0); | |
5244 | } | |
5245 | ||
5246 | /* | |
5247 | * Return a bitmask of stats that are supported on all pools by both the module | |
5248 | * and zpool iostat. | |
5249 | */ | |
5250 | static uint64_t | |
5251 | get_stat_flags(zpool_list_t *list) | |
5252 | { | |
5253 | uint64_t mask = -1; | |
5254 | ||
5255 | /* | |
5256 | * get_stat_flags_cb() will lop off bits from "mask" until only the | |
5257 | * flags that are supported on all pools remain. | |
5258 | */ | |
5259 | pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask); | |
5260 | return (mask); | |
5261 | } | |
5262 | ||
5263 | /* | |
2a673e76 | 5264 | * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise. |
193a37cb TH |
5265 | */ |
5266 | static int | |
2a8430a2 | 5267 | is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data) |
193a37cb | 5268 | { |
6f2ffd27 | 5269 | uint64_t guid; |
2a673e76 | 5270 | vdev_cbdata_t *cb = cb_data; |
2a8430a2 | 5271 | zpool_handle_t *zhp = zhp_data; |
193a37cb | 5272 | |
6f2ffd27 RW |
5273 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) |
5274 | return (0); | |
193a37cb | 5275 | |
6f2ffd27 | 5276 | return (guid == zpool_vdev_path_to_guid(zhp, cb->cb_names[0])); |
193a37cb TH |
5277 | } |
5278 | ||
5279 | /* | |
2a673e76 | 5280 | * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise. |
193a37cb TH |
5281 | */ |
5282 | static int | |
5283 | is_vdev(zpool_handle_t *zhp, void *cb_data) | |
5284 | { | |
5285 | return (for_each_vdev(zhp, is_vdev_cb, cb_data)); | |
5286 | } | |
5287 | ||
5288 | /* | |
5289 | * Check if vdevs are in a pool | |
5290 | * | |
5291 | * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise | |
5292 | * return 0. If pool_name is NULL, then search all pools. | |
5293 | */ | |
5294 | static int | |
5295 | are_vdevs_in_pool(int argc, char **argv, char *pool_name, | |
2a673e76 | 5296 | vdev_cbdata_t *cb) |
193a37cb TH |
5297 | { |
5298 | char **tmp_name; | |
5299 | int ret = 0; | |
5300 | int i; | |
5301 | int pool_count = 0; | |
5302 | ||
5303 | if ((argc == 0) || !*argv) | |
5304 | return (0); | |
5305 | ||
5306 | if (pool_name) | |
5307 | pool_count = 1; | |
5308 | ||
2a673e76 AJ |
5309 | /* Temporarily hijack cb_names for a second... */ |
5310 | tmp_name = cb->cb_names; | |
193a37cb TH |
5311 | |
5312 | /* Go though our list of prospective vdev names */ | |
5313 | for (i = 0; i < argc; i++) { | |
2a673e76 | 5314 | cb->cb_names = argv + i; |
193a37cb TH |
5315 | |
5316 | /* Is this name a vdev in our pools? */ | |
5317 | ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL, | |
2a673e76 | 5318 | ZFS_TYPE_POOL, B_FALSE, is_vdev, cb); |
193a37cb TH |
5319 | if (!ret) { |
5320 | /* No match */ | |
5321 | break; | |
5322 | } | |
5323 | } | |
5324 | ||
2a673e76 | 5325 | cb->cb_names = tmp_name; |
193a37cb TH |
5326 | |
5327 | return (ret); | |
5328 | } | |
5329 | ||
5330 | static int | |
5331 | is_pool_cb(zpool_handle_t *zhp, void *data) | |
5332 | { | |
5333 | char *name = data; | |
5334 | if (strcmp(name, zpool_get_name(zhp)) == 0) | |
5335 | return (1); | |
5336 | ||
5337 | return (0); | |
5338 | } | |
5339 | ||
5340 | /* | |
5341 | * Do we have a pool named *name? If so, return 1, otherwise 0. | |
5342 | */ | |
5343 | static int | |
5344 | is_pool(char *name) | |
5345 | { | |
2a673e76 AJ |
5346 | return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE, |
5347 | is_pool_cb, name)); | |
193a37cb TH |
5348 | } |
5349 | ||
5350 | /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */ | |
5351 | static int | |
4ea3f864 GM |
5352 | are_all_pools(int argc, char **argv) |
5353 | { | |
193a37cb TH |
5354 | if ((argc == 0) || !*argv) |
5355 | return (0); | |
5356 | ||
5357 | while (--argc >= 0) | |
5358 | if (!is_pool(argv[argc])) | |
5359 | return (0); | |
5360 | ||
5361 | return (1); | |
5362 | } | |
5363 | ||
5364 | /* | |
5365 | * Helper function to print out vdev/pool names we can't resolve. Used for an | |
5366 | * error message. | |
5367 | */ | |
5368 | static void | |
5369 | error_list_unresolved_vdevs(int argc, char **argv, char *pool_name, | |
2a673e76 | 5370 | vdev_cbdata_t *cb) |
193a37cb TH |
5371 | { |
5372 | int i; | |
5373 | char *name; | |
5374 | char *str; | |
5375 | for (i = 0; i < argc; i++) { | |
5376 | name = argv[i]; | |
5377 | ||
5378 | if (is_pool(name)) | |
5379 | str = gettext("pool"); | |
5380 | else if (are_vdevs_in_pool(1, &name, pool_name, cb)) | |
5381 | str = gettext("vdev in this pool"); | |
5382 | else if (are_vdevs_in_pool(1, &name, NULL, cb)) | |
5383 | str = gettext("vdev in another pool"); | |
5384 | else | |
5385 | str = gettext("unknown"); | |
5386 | ||
5387 | fprintf(stderr, "\t%s (%s)\n", name, str); | |
5388 | } | |
5389 | } | |
5390 | ||
5391 | /* | |
5392 | * Same as get_interval_count(), but with additional checks to not misinterpret | |
5393 | * guids as interval/count values. Assumes VDEV_NAME_GUID is set in | |
2a673e76 | 5394 | * cb.cb_vdevs.cb_name_flags. |
193a37cb TH |
5395 | */ |
5396 | static void | |
5397 | get_interval_count_filter_guids(int *argc, char **argv, float *interval, | |
5398 | unsigned long *count, iostat_cbdata_t *cb) | |
5399 | { | |
5400 | char **tmpargv = argv; | |
5401 | int argc_for_interval = 0; | |
5402 | ||
5403 | /* Is the last arg an interval value? Or a guid? */ | |
2a673e76 AJ |
5404 | if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, |
5405 | &cb->cb_vdevs)) { | |
193a37cb TH |
5406 | /* |
5407 | * The last arg is not a guid, so it's probably an | |
5408 | * interval value. | |
5409 | */ | |
5410 | argc_for_interval++; | |
5411 | ||
5412 | if (*argc >= 2 && | |
2a673e76 AJ |
5413 | !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, |
5414 | &cb->cb_vdevs)) { | |
193a37cb TH |
5415 | /* |
5416 | * The 2nd to last arg is not a guid, so it's probably | |
5417 | * an interval value. | |
5418 | */ | |
5419 | argc_for_interval++; | |
5420 | } | |
5421 | } | |
5422 | ||
5423 | /* Point to our list of possible intervals */ | |
5424 | tmpargv = &argv[*argc - argc_for_interval]; | |
5425 | ||
5426 | *argc = *argc - argc_for_interval; | |
5427 | get_interval_count(&argc_for_interval, tmpargv, | |
5428 | interval, count); | |
5429 | } | |
5430 | ||
5431 | /* | |
5432 | * Floating point sleep(). Allows you to pass in a floating point value for | |
5433 | * seconds. | |
5434 | */ | |
5435 | static void | |
4ea3f864 GM |
5436 | fsleep(float sec) |
5437 | { | |
193a37cb TH |
5438 | struct timespec req; |
5439 | req.tv_sec = floor(sec); | |
5440 | req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC; | |
5441 | nanosleep(&req, NULL); | |
5442 | } | |
5443 | ||
e60e158e JG |
5444 | /* |
5445 | * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or | |
5446 | * if we were unable to determine its size. | |
5447 | */ | |
5448 | static int | |
5449 | terminal_height(void) | |
5450 | { | |
5451 | struct winsize win; | |
5452 | ||
5453 | if (isatty(STDOUT_FILENO) == 0) | |
5454 | return (-1); | |
5455 | ||
5456 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0) | |
5457 | return (win.ws_row); | |
5458 | ||
5459 | return (-1); | |
5460 | } | |
5461 | ||
d6418de0 TH |
5462 | /* |
5463 | * Run one of the zpool status/iostat -c scripts with the help (-h) option and | |
5464 | * print the result. | |
5465 | * | |
5466 | * name: Short name of the script ('iostat'). | |
5467 | * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat'); | |
5468 | */ | |
5469 | static void | |
5470 | print_zpool_script_help(char *name, char *path) | |
5471 | { | |
a926aab9 | 5472 | char *argv[] = {path, (char *)"-h", NULL}; |
d6418de0 TH |
5473 | char **lines = NULL; |
5474 | int lines_cnt = 0; | |
5475 | int rc; | |
5476 | ||
5477 | rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines, | |
5478 | &lines_cnt); | |
3e2ecae0 GDN |
5479 | if (rc != 0 || lines == NULL || lines_cnt <= 0) { |
5480 | if (lines != NULL) | |
5481 | libzfs_free_str_array(lines, lines_cnt); | |
d6418de0 | 5482 | return; |
3e2ecae0 | 5483 | } |
d6418de0 TH |
5484 | |
5485 | for (int i = 0; i < lines_cnt; i++) | |
5486 | if (!is_blank_str(lines[i])) | |
5487 | printf(" %-14s %s\n", name, lines[i]); | |
5488 | ||
5489 | libzfs_free_str_array(lines, lines_cnt); | |
5490 | } | |
5491 | ||
193a37cb | 5492 | /* |
099700d9 | 5493 | * Go though the zpool status/iostat -c scripts in the user's path, run their |
d6418de0 TH |
5494 | * help option (-h), and print out the results. |
5495 | */ | |
5496 | static void | |
099700d9 | 5497 | print_zpool_dir_scripts(char *dirpath) |
d6418de0 TH |
5498 | { |
5499 | DIR *dir; | |
5500 | struct dirent *ent; | |
5501 | char fullpath[MAXPATHLEN]; | |
5502 | struct stat dir_stat; | |
5503 | ||
099700d9 | 5504 | if ((dir = opendir(dirpath)) != NULL) { |
d6418de0 TH |
5505 | /* print all the files and directories within directory */ |
5506 | while ((ent = readdir(dir)) != NULL) { | |
ba87ed14 RY |
5507 | if (snprintf(fullpath, sizeof (fullpath), "%s/%s", |
5508 | dirpath, ent->d_name) >= sizeof (fullpath)) { | |
5509 | (void) fprintf(stderr, | |
5510 | gettext("internal error: " | |
5511 | "ZPOOL_SCRIPTS_PATH too large.\n")); | |
5512 | exit(1); | |
5513 | } | |
d6418de0 TH |
5514 | |
5515 | /* Print the scripts */ | |
5516 | if (stat(fullpath, &dir_stat) == 0) | |
5517 | if (dir_stat.st_mode & S_IXUSR && | |
5518 | S_ISREG(dir_stat.st_mode)) | |
5519 | print_zpool_script_help(ent->d_name, | |
5520 | fullpath); | |
5521 | } | |
d6418de0 | 5522 | closedir(dir); |
d6418de0 TH |
5523 | } |
5524 | } | |
5525 | ||
099700d9 GDN |
5526 | /* |
5527 | * Print out help text for all zpool status/iostat -c scripts. | |
5528 | */ | |
5529 | static void | |
a926aab9 | 5530 | print_zpool_script_list(const char *subcommand) |
099700d9 | 5531 | { |
1ce6d70c | 5532 | char *dir, *sp, *tmp; |
099700d9 GDN |
5533 | |
5534 | printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand); | |
5535 | ||
5536 | sp = zpool_get_cmd_search_path(); | |
5537 | if (sp == NULL) | |
5538 | return; | |
5539 | ||
1ce6d70c AZ |
5540 | for (dir = strtok_r(sp, ":", &tmp); |
5541 | dir != NULL; | |
5542 | dir = strtok_r(NULL, ":", &tmp)) | |
099700d9 | 5543 | print_zpool_dir_scripts(dir); |
099700d9 GDN |
5544 | |
5545 | free(sp); | |
5546 | } | |
5547 | ||
c5eea0ab BB |
5548 | /* |
5549 | * Set the minimum pool/vdev name column width. The width must be at least 10, | |
5550 | * but may be as large as the column width - 42 so it still fits on one line. | |
a36bad17 | 5551 | * NOTE: 42 is the width of the default capacity/operations/bandwidth output |
c5eea0ab BB |
5552 | */ |
5553 | static int | |
5554 | get_namewidth_iostat(zpool_handle_t *zhp, void *data) | |
5555 | { | |
5556 | iostat_cbdata_t *cb = data; | |
a36bad17 | 5557 | int width, available_width; |
c5eea0ab | 5558 | |
a36bad17 PP |
5559 | /* |
5560 | * get_namewidth() returns the maximum width of any name in that column | |
5561 | * for any pool/vdev/device line that will be output. | |
5562 | */ | |
7c0e3941 SW |
5563 | width = get_namewidth(zhp, cb->cb_namewidth, |
5564 | cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose); | |
c5eea0ab | 5565 | |
a36bad17 PP |
5566 | /* |
5567 | * The width we are calculating is the width of the header and also the | |
5568 | * padding width for names that are less than maximum width. The stats | |
5569 | * take up 42 characters, so the width available for names is: | |
5570 | */ | |
5571 | available_width = get_columns() - 42; | |
5572 | ||
5573 | /* | |
5574 | * If the maximum width fits on a screen, then great! Make everything | |
5575 | * line up by justifying all lines to the same width. If that max | |
5576 | * width is larger than what's available, the name plus stats won't fit | |
5577 | * on one line, and justifying to that width would cause every line to | |
5578 | * wrap on the screen. We only want lines with long names to wrap. | |
5579 | * Limit the padding to what won't wrap. | |
5580 | */ | |
5581 | if (width > available_width) | |
5582 | width = available_width; | |
5583 | ||
5584 | /* | |
5585 | * And regardless of whatever the screen width is (get_columns can | |
5586 | * return 0 if the width is not known or less than 42 for a narrow | |
5587 | * terminal) have the width be a minimum of 10. | |
5588 | */ | |
c5eea0ab BB |
5589 | if (width < 10) |
5590 | width = 10; | |
c5eea0ab | 5591 | |
a36bad17 | 5592 | /* Save the calculated width */ |
c5eea0ab BB |
5593 | cb->cb_namewidth = width; |
5594 | ||
5595 | return (0); | |
5596 | } | |
5597 | ||
d6418de0 TH |
5598 | /* |
5599 | * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name] | |
5600 | * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]] | |
5601 | * [interval [count]] | |
428870ff | 5602 | * |
8720e9e7 | 5603 | * -c CMD For each vdev, run command CMD |
d2f3e292 RY |
5604 | * -g Display guid for individual vdev name. |
5605 | * -L Follow links when resolving vdev path name. | |
a77f29f9 | 5606 | * -P Display full path for vdev name. |
428870ff | 5607 | * -v Display statistics for individual vdevs |
193a37cb TH |
5608 | * -h Display help |
5609 | * -p Display values in parsable (exact) format. | |
5610 | * -H Scripted mode. Don't display headers, and separate properties | |
5611 | * by a single tab. | |
5612 | * -l Display average latency | |
5613 | * -q Display queue depths | |
7e945072 TH |
5614 | * -w Display latency histograms |
5615 | * -r Display request size histogram | |
428870ff | 5616 | * -T Display a timestamp in date(1) or Unix format |
8fccfa8e | 5617 | * -n Only print headers once |
428870ff BB |
5618 | * |
5619 | * This command can be tricky because we want to be able to deal with pool | |
5620 | * creation/destruction as well as vdev configuration changes. The bulk of this | |
5621 | * processing is handled by the pool_list_* routines in zpool_iter.c. We rely | |
5622 | * on pool_list_update() to detect the addition of new pools. Configuration | |
5623 | * changes are all handled within libzfs. | |
5624 | */ | |
5625 | int | |
5626 | zpool_do_iostat(int argc, char **argv) | |
5627 | { | |
5628 | int c; | |
5629 | int ret; | |
5630 | int npools; | |
193a37cb TH |
5631 | float interval = 0; |
5632 | unsigned long count = 0; | |
8fccfa8e | 5633 | int winheight = 24; |
428870ff BB |
5634 | zpool_list_t *list; |
5635 | boolean_t verbose = B_FALSE; | |
7e945072 | 5636 | boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE; |
3491d6eb | 5637 | boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE; |
41092124 | 5638 | boolean_t omit_since_boot = B_FALSE; |
d2f3e292 RY |
5639 | boolean_t guid = B_FALSE; |
5640 | boolean_t follow_links = B_FALSE; | |
5641 | boolean_t full_name = B_FALSE; | |
8fccfa8e | 5642 | boolean_t headers_once = B_FALSE; |
d2f3e292 | 5643 | iostat_cbdata_t cb = { 0 }; |
8720e9e7 | 5644 | char *cmd = NULL; |
428870ff | 5645 | |
193a37cb TH |
5646 | /* Used for printing error message */ |
5647 | const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q', | |
7e945072 | 5648 | [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'}; |
193a37cb TH |
5649 | |
5650 | uint64_t unsupported_flags; | |
5651 | ||
428870ff | 5652 | /* check options */ |
8fccfa8e | 5653 | while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) { |
428870ff | 5654 | switch (c) { |
8720e9e7 | 5655 | case 'c': |
d6418de0 TH |
5656 | if (cmd != NULL) { |
5657 | fprintf(stderr, | |
5658 | gettext("Can't set -c flag twice\n")); | |
5659 | exit(1); | |
5660 | } | |
099700d9 GDN |
5661 | |
5662 | if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && | |
5663 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { | |
5664 | fprintf(stderr, gettext( | |
5665 | "Can't run -c, disabled by " | |
5666 | "ZPOOL_SCRIPTS_ENABLED.\n")); | |
5667 | exit(1); | |
5668 | } | |
5669 | ||
d6418de0 TH |
5670 | if ((getuid() <= 0 || geteuid() <= 0) && |
5671 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { | |
5672 | fprintf(stderr, gettext( | |
5673 | "Can't run -c with root privileges " | |
5674 | "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); | |
5675 | exit(1); | |
5676 | } | |
8720e9e7 | 5677 | cmd = optarg; |
d6418de0 | 5678 | verbose = B_TRUE; |
8720e9e7 | 5679 | break; |
d2f3e292 RY |
5680 | case 'g': |
5681 | guid = B_TRUE; | |
5682 | break; | |
5683 | case 'L': | |
5684 | follow_links = B_TRUE; | |
5685 | break; | |
a77f29f9 | 5686 | case 'P': |
d2f3e292 RY |
5687 | full_name = B_TRUE; |
5688 | break; | |
428870ff BB |
5689 | case 'T': |
5690 | get_timestamp_arg(*optarg); | |
5691 | break; | |
5692 | case 'v': | |
5693 | verbose = B_TRUE; | |
5694 | break; | |
193a37cb | 5695 | case 'p': |
3491d6eb | 5696 | parsable = B_TRUE; |
193a37cb TH |
5697 | break; |
5698 | case 'l': | |
5699 | latency = B_TRUE; | |
5700 | break; | |
5701 | case 'q': | |
5702 | queues = B_TRUE; | |
5703 | break; | |
5704 | case 'H': | |
5705 | scripted = B_TRUE; | |
5706 | break; | |
5707 | case 'w': | |
7e945072 TH |
5708 | l_histo = B_TRUE; |
5709 | break; | |
5710 | case 'r': | |
5711 | rq_histo = B_TRUE; | |
193a37cb | 5712 | break; |
41092124 HM |
5713 | case 'y': |
5714 | omit_since_boot = B_TRUE; | |
5715 | break; | |
8fccfa8e DW |
5716 | case 'n': |
5717 | headers_once = B_TRUE; | |
5718 | break; | |
193a37cb TH |
5719 | case 'h': |
5720 | usage(B_FALSE); | |
5721 | break; | |
428870ff | 5722 | case '?': |
1528bfdb | 5723 | if (optopt == 'c') { |
099700d9 | 5724 | print_zpool_script_list("iostat"); |
d6418de0 | 5725 | exit(0); |
1528bfdb TH |
5726 | } else { |
5727 | fprintf(stderr, | |
5728 | gettext("invalid option '%c'\n"), optopt); | |
5729 | } | |
428870ff BB |
5730 | usage(B_FALSE); |
5731 | } | |
5732 | } | |
5733 | ||
5734 | argc -= optind; | |
5735 | argv += optind; | |
5736 | ||
3491d6eb | 5737 | cb.cb_literal = parsable; |
193a37cb TH |
5738 | cb.cb_scripted = scripted; |
5739 | ||
5740 | if (guid) | |
2a673e76 | 5741 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID; |
193a37cb | 5742 | if (follow_links) |
2a673e76 | 5743 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; |
193a37cb | 5744 | if (full_name) |
2a673e76 | 5745 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH; |
193a37cb TH |
5746 | cb.cb_iteration = 0; |
5747 | cb.cb_namewidth = 0; | |
5748 | cb.cb_verbose = verbose; | |
5749 | ||
5750 | /* Get our interval and count values (if any) */ | |
5751 | if (guid) { | |
5752 | get_interval_count_filter_guids(&argc, argv, &interval, | |
5753 | &count, &cb); | |
5754 | } else { | |
5755 | get_interval_count(&argc, argv, &interval, &count); | |
5756 | } | |
5757 | ||
5758 | if (argc == 0) { | |
5759 | /* No args, so just print the defaults. */ | |
5760 | } else if (are_all_pools(argc, argv)) { | |
5761 | /* All the args are pool names */ | |
2a673e76 | 5762 | } else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) { |
193a37cb | 5763 | /* All the args are vdevs */ |
2a673e76 AJ |
5764 | cb.cb_vdevs.cb_names = argv; |
5765 | cb.cb_vdevs.cb_names_count = argc; | |
193a37cb TH |
5766 | argc = 0; /* No pools to process */ |
5767 | } else if (are_all_pools(1, argv)) { | |
5768 | /* The first arg is a pool name */ | |
2a673e76 AJ |
5769 | if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], |
5770 | &cb.cb_vdevs)) { | |
193a37cb | 5771 | /* ...and the rest are vdev names */ |
2a673e76 AJ |
5772 | cb.cb_vdevs.cb_names = argv + 1; |
5773 | cb.cb_vdevs.cb_names_count = argc - 1; | |
193a37cb TH |
5774 | argc = 1; /* One pool to process */ |
5775 | } else { | |
5776 | fprintf(stderr, gettext("Expected either a list of ")); | |
5777 | fprintf(stderr, gettext("pools, or list of vdevs in")); | |
5778 | fprintf(stderr, " \"%s\", ", argv[0]); | |
5779 | fprintf(stderr, gettext("but got:\n")); | |
5780 | error_list_unresolved_vdevs(argc - 1, argv + 1, | |
2a673e76 | 5781 | argv[0], &cb.cb_vdevs); |
193a37cb TH |
5782 | fprintf(stderr, "\n"); |
5783 | usage(B_FALSE); | |
5784 | return (1); | |
5785 | } | |
5786 | } else { | |
5787 | /* | |
5788 | * The args don't make sense. The first arg isn't a pool name, | |
5789 | * nor are all the args vdevs. | |
5790 | */ | |
5791 | fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n")); | |
5792 | fprintf(stderr, "\n"); | |
5793 | return (1); | |
5794 | } | |
5795 | ||
2a673e76 | 5796 | if (cb.cb_vdevs.cb_names_count != 0) { |
193a37cb TH |
5797 | /* |
5798 | * If user specified vdevs, it implies verbose. | |
5799 | */ | |
5800 | cb.cb_verbose = B_TRUE; | |
5801 | } | |
428870ff | 5802 | |
34dc7c2f BB |
5803 | /* |
5804 | * Construct the list of all interesting pools. | |
5805 | */ | |
5806 | ret = 0; | |
2a673e76 AJ |
5807 | if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable, |
5808 | &ret)) == NULL) | |
34dc7c2f BB |
5809 | return (1); |
5810 | ||
5811 | if (pool_list_count(list) == 0 && argc != 0) { | |
5812 | pool_list_free(list); | |
5813 | return (1); | |
5814 | } | |
5815 | ||
5816 | if (pool_list_count(list) == 0 && interval == 0) { | |
5817 | pool_list_free(list); | |
5818 | (void) fprintf(stderr, gettext("no pools available\n")); | |
5819 | return (1); | |
5820 | } | |
5821 | ||
d6418de0 | 5822 | if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) { |
7e945072 TH |
5823 | pool_list_free(list); |
5824 | (void) fprintf(stderr, | |
d6418de0 | 5825 | gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n")); |
7e945072 TH |
5826 | usage(B_FALSE); |
5827 | return (1); | |
5828 | } | |
5829 | ||
5830 | if (l_histo && rq_histo) { | |
193a37cb TH |
5831 | pool_list_free(list); |
5832 | (void) fprintf(stderr, | |
7e945072 | 5833 | gettext("Only one of [-r|-w] can be passed at a time\n")); |
193a37cb TH |
5834 | usage(B_FALSE); |
5835 | return (1); | |
5836 | } | |
5837 | ||
34dc7c2f BB |
5838 | /* |
5839 | * Enter the main iostat loop. | |
5840 | */ | |
5841 | cb.cb_list = list; | |
193a37cb | 5842 | |
7e945072 | 5843 | if (l_histo) { |
193a37cb TH |
5844 | /* |
5845 | * Histograms tables look out of place when you try to display | |
5846 | * them with the other stats, so make a rule that you can only | |
5847 | * print histograms by themselves. | |
5848 | */ | |
5849 | cb.cb_flags = IOS_L_HISTO_M; | |
7e945072 TH |
5850 | } else if (rq_histo) { |
5851 | cb.cb_flags = IOS_RQ_HISTO_M; | |
193a37cb TH |
5852 | } else { |
5853 | cb.cb_flags = IOS_DEFAULT_M; | |
5854 | if (latency) | |
5855 | cb.cb_flags |= IOS_LATENCY_M; | |
5856 | if (queues) | |
5857 | cb.cb_flags |= IOS_QUEUES_M; | |
5858 | } | |
5859 | ||
5860 | /* | |
5861 | * See if the module supports all the stats we want to display. | |
5862 | */ | |
5863 | unsupported_flags = cb.cb_flags & ~get_stat_flags(list); | |
5864 | if (unsupported_flags) { | |
5865 | uint64_t f; | |
5866 | int idx; | |
5867 | fprintf(stderr, | |
5868 | gettext("The loaded zfs module doesn't support:")); | |
5869 | ||
5870 | /* for each bit set in unsupported_flags */ | |
5871 | for (f = unsupported_flags; f; f &= ~(1ULL << idx)) { | |
5872 | idx = lowbit64(f) - 1; | |
5873 | fprintf(stderr, " -%c", flag_to_arg[idx]); | |
5874 | } | |
5875 | ||
02730c33 | 5876 | fprintf(stderr, ". Try running a newer module.\n"); |
193a37cb TH |
5877 | pool_list_free(list); |
5878 | ||
5879 | return (1); | |
5880 | } | |
5881 | ||
34dc7c2f | 5882 | for (;;) { |
34dc7c2f | 5883 | if ((npools = pool_list_count(list)) == 0) |
42cb3819 | 5884 | (void) fprintf(stderr, gettext("no pools available\n")); |
5a521059 | 5885 | else { |
41092124 HM |
5886 | /* |
5887 | * If this is the first iteration and -y was supplied | |
5888 | * we skip any printing. | |
5889 | */ | |
5890 | boolean_t skip = (omit_since_boot && | |
193a37cb | 5891 | cb.cb_iteration == 0); |
41092124 | 5892 | |
5a521059 PJ |
5893 | /* |
5894 | * Refresh all statistics. This is done as an | |
5895 | * explicit step before calculating the maximum name | |
5896 | * width, so that any * configuration changes are | |
5897 | * properly accounted for. | |
5898 | */ | |
5899 | (void) pool_list_iter(list, B_FALSE, refresh_iostat, | |
193a37cb | 5900 | &cb); |
34dc7c2f | 5901 | |
5a521059 PJ |
5902 | /* |
5903 | * Iterate over all pools to determine the maximum width | |
5904 | * for the pool / device name column across all pools. | |
5905 | */ | |
5906 | cb.cb_namewidth = 0; | |
c5eea0ab BB |
5907 | (void) pool_list_iter(list, B_FALSE, |
5908 | get_namewidth_iostat, &cb); | |
34dc7c2f | 5909 | |
5a521059 PJ |
5910 | if (timestamp_fmt != NODATE) |
5911 | print_timestamp(timestamp_fmt); | |
428870ff | 5912 | |
d6418de0 TH |
5913 | if (cmd != NULL && cb.cb_verbose && |
5914 | !(cb.cb_flags & IOS_ANYHISTO_M)) { | |
5915 | cb.vcdl = all_pools_for_each_vdev_run(argc, | |
2a673e76 AJ |
5916 | argv, cmd, g_zfs, cb.cb_vdevs.cb_names, |
5917 | cb.cb_vdevs.cb_names_count, | |
5918 | cb.cb_vdevs.cb_name_flags); | |
d6418de0 TH |
5919 | } else { |
5920 | cb.vcdl = NULL; | |
5921 | } | |
5922 | ||
8fccfa8e DW |
5923 | |
5924 | /* | |
5925 | * Check terminal size so we can print headers | |
5926 | * even when terminal window has its height | |
5927 | * changed. | |
5928 | */ | |
e60e158e JG |
5929 | winheight = terminal_height(); |
5930 | /* | |
5931 | * Are we connected to TTY? If not, headers_once | |
5932 | * should be true, to avoid breaking scripts. | |
5933 | */ | |
5934 | if (winheight < 0) | |
5935 | headers_once = B_TRUE; | |
8fccfa8e | 5936 | |
5a521059 | 5937 | /* |
41092124 HM |
5938 | * If it's the first time and we're not skipping it, |
5939 | * or either skip or verbose mode, print the header. | |
193a37cb TH |
5940 | * |
5941 | * The histogram code explicitly prints its header on | |
5942 | * every vdev, so skip this for histograms. | |
5a521059 | 5943 | */ |
193a37cb | 5944 | if (((++cb.cb_iteration == 1 && !skip) || |
8fccfa8e DW |
5945 | (skip != verbose) || |
5946 | (!headers_once && | |
5947 | (cb.cb_iteration % winheight) == 0)) && | |
7e945072 | 5948 | (!(cb.cb_flags & IOS_ANYHISTO_M)) && |
193a37cb | 5949 | !cb.cb_scripted) |
5a521059 | 5950 | print_iostat_header(&cb); |
34dc7c2f | 5951 | |
41092124 | 5952 | if (skip) { |
193a37cb | 5953 | (void) fsleep(interval); |
41092124 HM |
5954 | continue; |
5955 | } | |
5956 | ||
193a37cb | 5957 | pool_list_iter(list, B_FALSE, print_iostat, &cb); |
34dc7c2f | 5958 | |
5a521059 PJ |
5959 | /* |
5960 | * If there's more than one pool, and we're not in | |
5961 | * verbose mode (which prints a separator for us), | |
5962 | * then print a separator. | |
193a37cb TH |
5963 | * |
5964 | * In addition, if we're printing specific vdevs then | |
5965 | * we also want an ending separator. | |
5a521059 | 5966 | */ |
193a37cb | 5967 | if (((npools > 1 && !verbose && |
7e945072 TH |
5968 | !(cb.cb_flags & IOS_ANYHISTO_M)) || |
5969 | (!(cb.cb_flags & IOS_ANYHISTO_M) && | |
2a673e76 | 5970 | cb.cb_vdevs.cb_names_count)) && |
193a37cb | 5971 | !cb.cb_scripted) { |
5a521059 | 5972 | print_iostat_separator(&cb); |
d6418de0 TH |
5973 | if (cb.vcdl != NULL) |
5974 | print_cmd_columns(cb.vcdl, 1); | |
5975 | printf("\n"); | |
193a37cb | 5976 | } |
d6418de0 TH |
5977 | |
5978 | if (cb.vcdl != NULL) | |
5979 | free_vdev_cmd_data_list(cb.vcdl); | |
5980 | ||
5a521059 | 5981 | } |
34dc7c2f BB |
5982 | |
5983 | /* | |
5984 | * Flush the output so that redirection to a file isn't buffered | |
5985 | * indefinitely. | |
5986 | */ | |
5987 | (void) fflush(stdout); | |
5988 | ||
5989 | if (interval == 0) | |
5990 | break; | |
5991 | ||
5992 | if (count != 0 && --count == 0) | |
5993 | break; | |
5994 | ||
193a37cb | 5995 | (void) fsleep(interval); |
34dc7c2f BB |
5996 | } |
5997 | ||
5998 | pool_list_free(list); | |
5999 | ||
6000 | return (ret); | |
6001 | } | |
6002 | ||
6003 | typedef struct list_cbdata { | |
1bd201e7 | 6004 | boolean_t cb_verbose; |
d2f3e292 | 6005 | int cb_name_flags; |
1bd201e7 | 6006 | int cb_namewidth; |
34dc7c2f | 6007 | boolean_t cb_scripted; |
34dc7c2f | 6008 | zprop_list_t *cb_proplist; |
2a8b84b7 | 6009 | boolean_t cb_literal; |
34dc7c2f BB |
6010 | } list_cbdata_t; |
6011 | ||
cc99f275 | 6012 | |
34dc7c2f BB |
6013 | /* |
6014 | * Given a list of columns to display, output appropriate headers for each one. | |
6015 | */ | |
6016 | static void | |
1bd201e7 | 6017 | print_header(list_cbdata_t *cb) |
34dc7c2f | 6018 | { |
1bd201e7 | 6019 | zprop_list_t *pl = cb->cb_proplist; |
9ae529ec | 6020 | char headerbuf[ZPOOL_MAXPROPLEN]; |
34dc7c2f BB |
6021 | const char *header; |
6022 | boolean_t first = B_TRUE; | |
6023 | boolean_t right_justify; | |
1bd201e7 | 6024 | size_t width = 0; |
34dc7c2f BB |
6025 | |
6026 | for (; pl != NULL; pl = pl->pl_next) { | |
1bd201e7 CS |
6027 | width = pl->pl_width; |
6028 | if (first && cb->cb_verbose) { | |
6029 | /* | |
6030 | * Reset the width to accommodate the verbose listing | |
6031 | * of devices. | |
6032 | */ | |
6033 | width = cb->cb_namewidth; | |
6034 | } | |
6035 | ||
34dc7c2f | 6036 | if (!first) |
06b80506 | 6037 | (void) fputs(" ", stdout); |
34dc7c2f BB |
6038 | else |
6039 | first = B_FALSE; | |
6040 | ||
9ae529ec | 6041 | right_justify = B_FALSE; |
4ff7a8fa | 6042 | if (pl->pl_prop != ZPROP_USERPROP) { |
9ae529ec CS |
6043 | header = zpool_prop_column_name(pl->pl_prop); |
6044 | right_justify = zpool_prop_align_right(pl->pl_prop); | |
6045 | } else { | |
6046 | int i; | |
6047 | ||
6048 | for (i = 0; pl->pl_user_prop[i] != '\0'; i++) | |
6049 | headerbuf[i] = toupper(pl->pl_user_prop[i]); | |
6050 | headerbuf[i] = '\0'; | |
6051 | header = headerbuf; | |
6052 | } | |
34dc7c2f BB |
6053 | |
6054 | if (pl->pl_next == NULL && !right_justify) | |
06b80506 | 6055 | (void) fputs(header, stdout); |
34dc7c2f | 6056 | else if (right_justify) |
1bd201e7 | 6057 | (void) printf("%*s", (int)width, header); |
34dc7c2f | 6058 | else |
1bd201e7 | 6059 | (void) printf("%-*s", (int)width, header); |
34dc7c2f BB |
6060 | } |
6061 | ||
06b80506 | 6062 | (void) fputc('\n', stdout); |
34dc7c2f BB |
6063 | } |
6064 | ||
6065 | /* | |
6066 | * Given a pool and a list of properties, print out all the properties according | |
cc99f275 | 6067 | * to the described layout. Used by zpool_do_list(). |
34dc7c2f BB |
6068 | */ |
6069 | static void | |
1bd201e7 | 6070 | print_pool(zpool_handle_t *zhp, list_cbdata_t *cb) |
34dc7c2f | 6071 | { |
1bd201e7 | 6072 | zprop_list_t *pl = cb->cb_proplist; |
34dc7c2f BB |
6073 | boolean_t first = B_TRUE; |
6074 | char property[ZPOOL_MAXPROPLEN]; | |
a926aab9 | 6075 | const char *propstr; |
34dc7c2f | 6076 | boolean_t right_justify; |
1bd201e7 | 6077 | size_t width; |
34dc7c2f BB |
6078 | |
6079 | for (; pl != NULL; pl = pl->pl_next) { | |
1bd201e7 CS |
6080 | |
6081 | width = pl->pl_width; | |
6082 | if (first && cb->cb_verbose) { | |
6083 | /* | |
6084 | * Reset the width to accommodate the verbose listing | |
6085 | * of devices. | |
6086 | */ | |
6087 | width = cb->cb_namewidth; | |
6088 | } | |
6089 | ||
34dc7c2f | 6090 | if (!first) { |
1bd201e7 | 6091 | if (cb->cb_scripted) |
06b80506 | 6092 | (void) fputc('\t', stdout); |
34dc7c2f | 6093 | else |
06b80506 | 6094 | (void) fputs(" ", stdout); |
34dc7c2f BB |
6095 | } else { |
6096 | first = B_FALSE; | |
6097 | } | |
6098 | ||
6099 | right_justify = B_FALSE; | |
4ff7a8fa | 6100 | if (pl->pl_prop != ZPROP_USERPROP) { |
a05dfd00 | 6101 | if (zpool_get_prop(zhp, pl->pl_prop, property, |
2a8b84b7 | 6102 | sizeof (property), NULL, cb->cb_literal) != 0) |
34dc7c2f BB |
6103 | propstr = "-"; |
6104 | else | |
6105 | propstr = property; | |
6106 | ||
6107 | right_justify = zpool_prop_align_right(pl->pl_prop); | |
9ae529ec CS |
6108 | } else if ((zpool_prop_feature(pl->pl_user_prop) || |
6109 | zpool_prop_unsupported(pl->pl_user_prop)) && | |
6110 | zpool_prop_get_feature(zhp, pl->pl_user_prop, property, | |
6111 | sizeof (property)) == 0) { | |
6112 | propstr = property; | |
8eae2d21 AJ |
6113 | } else if (zfs_prop_user(pl->pl_user_prop) && |
6114 | zpool_get_userprop(zhp, pl->pl_user_prop, property, | |
6115 | sizeof (property), NULL) == 0) { | |
6116 | propstr = property; | |
34dc7c2f BB |
6117 | } else { |
6118 | propstr = "-"; | |
6119 | } | |
6120 | ||
34dc7c2f BB |
6121 | /* |
6122 | * If this is being called in scripted mode, or if this is the | |
6123 | * last column and it is left-justified, don't include a width | |
6124 | * format specifier. | |
6125 | */ | |
1bd201e7 | 6126 | if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) |
06b80506 | 6127 | (void) fputs(propstr, stdout); |
34dc7c2f | 6128 | else if (right_justify) |
1bd201e7 | 6129 | (void) printf("%*s", (int)width, propstr); |
34dc7c2f | 6130 | else |
1bd201e7 | 6131 | (void) printf("%-*s", (int)width, propstr); |
34dc7c2f BB |
6132 | } |
6133 | ||
06b80506 | 6134 | (void) fputc('\n', stdout); |
34dc7c2f BB |
6135 | } |
6136 | ||
1bd201e7 | 6137 | static void |
c5eea0ab BB |
6138 | print_one_column(zpool_prop_t prop, uint64_t value, const char *str, |
6139 | boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format) | |
1bd201e7 CS |
6140 | { |
6141 | char propval[64]; | |
6142 | boolean_t fixed; | |
6143 | size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); | |
6144 | ||
a05dfd00 | 6145 | switch (prop) { |
1282274f | 6146 | case ZPOOL_PROP_SIZE: |
a05dfd00 | 6147 | case ZPOOL_PROP_EXPANDSZ: |
d2734cce | 6148 | case ZPOOL_PROP_CHECKPOINT: |
c5eea0ab | 6149 | case ZPOOL_PROP_DEDUPRATIO: |
a05dfd00 GW |
6150 | if (value == 0) |
6151 | (void) strlcpy(propval, "-", sizeof (propval)); | |
6152 | else | |
bc2d8093 CE |
6153 | zfs_nicenum_format(value, propval, sizeof (propval), |
6154 | format); | |
a05dfd00 GW |
6155 | break; |
6156 | case ZPOOL_PROP_FRAGMENTATION: | |
6157 | if (value == ZFS_FRAG_INVALID) { | |
6158 | (void) strlcpy(propval, "-", sizeof (propval)); | |
bc2d8093 CE |
6159 | } else if (format == ZFS_NICENUM_RAW) { |
6160 | (void) snprintf(propval, sizeof (propval), "%llu", | |
6161 | (unsigned long long)value); | |
a05dfd00 GW |
6162 | } else { |
6163 | (void) snprintf(propval, sizeof (propval), "%llu%%", | |
6164 | (unsigned long long)value); | |
6165 | } | |
6166 | break; | |
6167 | case ZPOOL_PROP_CAPACITY: | |
cc99f275 | 6168 | /* capacity value is in parts-per-10,000 (aka permyriad) */ |
bc2d8093 CE |
6169 | if (format == ZFS_NICENUM_RAW) |
6170 | (void) snprintf(propval, sizeof (propval), "%llu", | |
cc99f275 | 6171 | (unsigned long long)value / 100); |
bc2d8093 | 6172 | else |
cc99f275 DB |
6173 | (void) snprintf(propval, sizeof (propval), |
6174 | value < 1000 ? "%1.2f%%" : value < 10000 ? | |
6175 | "%2.1f%%" : "%3.0f%%", value / 100.0); | |
a05dfd00 | 6176 | break; |
c5eea0ab BB |
6177 | case ZPOOL_PROP_HEALTH: |
6178 | width = 8; | |
96904d87 | 6179 | (void) strlcpy(propval, str, sizeof (propval)); |
c5eea0ab | 6180 | break; |
a05dfd00 | 6181 | default: |
bc2d8093 | 6182 | zfs_nicenum_format(value, propval, sizeof (propval), format); |
a05dfd00 GW |
6183 | } |
6184 | ||
6185 | if (!valid) | |
6186 | (void) strlcpy(propval, "-", sizeof (propval)); | |
1bd201e7 CS |
6187 | |
6188 | if (scripted) | |
6189 | (void) printf("\t%s", propval); | |
6190 | else | |
6191 | (void) printf(" %*s", (int)width, propval); | |
6192 | } | |
6193 | ||
cc99f275 DB |
6194 | /* |
6195 | * print static default line per vdev | |
6196 | * not compatible with '-o' <proplist> option | |
6197 | */ | |
65c7cc49 | 6198 | static void |
1bd201e7 | 6199 | print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, |
c5eea0ab | 6200 | list_cbdata_t *cb, int depth, boolean_t isspare) |
1bd201e7 CS |
6201 | { |
6202 | nvlist_t **child; | |
6203 | vdev_stat_t *vs; | |
6204 | uint_t c, children; | |
6205 | char *vname; | |
6206 | boolean_t scripted = cb->cb_scripted; | |
8e4c5c9a | 6207 | uint64_t islog = B_FALSE; |
cdb3eb5a AZ |
6208 | const char *dashes = "%-*s - - - - " |
6209 | "- - - - -\n"; | |
1bd201e7 CS |
6210 | |
6211 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, | |
6212 | (uint64_t **)&vs, &c) == 0); | |
6213 | ||
6214 | if (name != NULL) { | |
a05dfd00 GW |
6215 | boolean_t toplevel = (vs->vs_space != 0); |
6216 | uint64_t cap; | |
bc2d8093 | 6217 | enum zfs_nicenum_format format; |
c5eea0ab | 6218 | const char *state; |
bc2d8093 CE |
6219 | |
6220 | if (cb->cb_literal) | |
6221 | format = ZFS_NICENUM_RAW; | |
6222 | else | |
6223 | format = ZFS_NICENUM_1024; | |
a05dfd00 | 6224 | |
a1d477c2 MA |
6225 | if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) |
6226 | return; | |
6227 | ||
1bd201e7 CS |
6228 | if (scripted) |
6229 | (void) printf("\t%s", name); | |
6230 | else if (strlen(name) + depth > cb->cb_namewidth) | |
6231 | (void) printf("%*s%s", depth, "", name); | |
6232 | else | |
6233 | (void) printf("%*s%s%*s", depth, "", name, | |
6234 | (int)(cb->cb_namewidth - strlen(name) - depth), ""); | |
6235 | ||
a05dfd00 GW |
6236 | /* |
6237 | * Print the properties for the individual vdevs. Some | |
6238 | * properties are only applicable to toplevel vdevs. The | |
6239 | * 'toplevel' boolean value is passed to the print_one_column() | |
6240 | * to indicate that the value is valid. | |
6241 | */ | |
e77d59eb | 6242 | if (VDEV_STAT_VALID(vs_pspace, c) && vs->vs_pspace) |
1282274f A |
6243 | print_one_column(ZPOOL_PROP_SIZE, vs->vs_pspace, NULL, |
6244 | scripted, B_TRUE, format); | |
6245 | else | |
6246 | print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL, | |
6247 | scripted, toplevel, format); | |
c5eea0ab | 6248 | print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL, |
bc2d8093 | 6249 | scripted, toplevel, format); |
c5eea0ab BB |
6250 | print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc, |
6251 | NULL, scripted, toplevel, format); | |
d2734cce | 6252 | print_one_column(ZPOOL_PROP_CHECKPOINT, |
c5eea0ab BB |
6253 | vs->vs_checkpoint_space, NULL, scripted, toplevel, format); |
6254 | print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL, | |
6255 | scripted, B_TRUE, format); | |
a05dfd00 | 6256 | print_one_column(ZPOOL_PROP_FRAGMENTATION, |
c5eea0ab | 6257 | vs->vs_fragmentation, NULL, scripted, |
bc2d8093 CE |
6258 | (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel), |
6259 | format); | |
a05dfd00 | 6260 | cap = (vs->vs_space == 0) ? 0 : |
cc99f275 | 6261 | (vs->vs_alloc * 10000 / vs->vs_space); |
c5eea0ab BB |
6262 | print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL, |
6263 | scripted, toplevel, format); | |
6264 | print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL, | |
6265 | scripted, toplevel, format); | |
6266 | state = zpool_state_to_name(vs->vs_state, vs->vs_aux); | |
6267 | if (isspare) { | |
6268 | if (vs->vs_aux == VDEV_AUX_SPARED) | |
6269 | state = "INUSE"; | |
6270 | else if (vs->vs_state == VDEV_STATE_HEALTHY) | |
6271 | state = "AVAIL"; | |
6272 | } | |
6273 | print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted, | |
6274 | B_TRUE, format); | |
06b80506 | 6275 | (void) fputc('\n', stdout); |
1bd201e7 CS |
6276 | } |
6277 | ||
6278 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
6279 | &child, &children) != 0) | |
6280 | return; | |
6281 | ||
cc99f275 | 6282 | /* list the normal vdevs first */ |
1bd201e7 CS |
6283 | for (c = 0; c < children; c++) { |
6284 | uint64_t ishole = B_FALSE; | |
6285 | ||
6286 | if (nvlist_lookup_uint64(child[c], | |
6287 | ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) | |
6288 | continue; | |
6289 | ||
8e4c5c9a | 6290 | if (nvlist_lookup_uint64(child[c], |
cc99f275 DB |
6291 | ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) |
6292 | continue; | |
6293 | ||
6294 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) | |
8e4c5c9a | 6295 | continue; |
8e4c5c9a | 6296 | |
d2f3e292 | 6297 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
7b468ed2 | 6298 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
c5eea0ab | 6299 | print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE); |
1bd201e7 CS |
6300 | free(vname); |
6301 | } | |
6302 | ||
cc99f275 | 6303 | /* list the classes: 'logs', 'dedup', and 'special' */ |
cf5a9d8c | 6304 | for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) { |
cc99f275 DB |
6305 | boolean_t printed = B_FALSE; |
6306 | ||
8e4c5c9a | 6307 | for (c = 0; c < children; c++) { |
d1807f16 RY |
6308 | const char *bias = NULL; |
6309 | const char *type = NULL; | |
cc99f275 | 6310 | |
8e4c5c9a | 6311 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
cc99f275 | 6312 | &islog) == 0 && islog) { |
d1807f16 | 6313 | bias = VDEV_ALLOC_CLASS_LOGS; |
cc99f275 DB |
6314 | } else { |
6315 | (void) nvlist_lookup_string(child[c], | |
6316 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
6317 | (void) nvlist_lookup_string(child[c], | |
6318 | ZPOOL_CONFIG_TYPE, &type); | |
6319 | } | |
6320 | if (bias == NULL || strcmp(bias, class_name[n]) != 0) | |
8e4c5c9a | 6321 | continue; |
cc99f275 DB |
6322 | if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) |
6323 | continue; | |
6324 | ||
6325 | if (!printed) { | |
6326 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
6327 | (void) printf(dashes, cb->cb_namewidth, | |
6328 | class_name[n]); | |
6329 | printed = B_TRUE; | |
6330 | } | |
d2f3e292 | 6331 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
7b468ed2 | 6332 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
c5eea0ab BB |
6333 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
6334 | B_FALSE); | |
8e4c5c9a JWK |
6335 | free(vname); |
6336 | } | |
6337 | } | |
6338 | ||
1bd201e7 | 6339 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, |
8e4c5c9a JWK |
6340 | &child, &children) == 0 && children > 0) { |
6341 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
6342 | (void) printf(dashes, cb->cb_namewidth, "cache"); | |
6343 | for (c = 0; c < children; c++) { | |
d2f3e292 RY |
6344 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
6345 | cb->cb_name_flags); | |
c5eea0ab BB |
6346 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
6347 | B_FALSE); | |
8e4c5c9a JWK |
6348 | free(vname); |
6349 | } | |
6350 | } | |
1bd201e7 | 6351 | |
8e4c5c9a JWK |
6352 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child, |
6353 | &children) == 0 && children > 0) { | |
6354 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
6355 | (void) printf(dashes, cb->cb_namewidth, "spare"); | |
1bd201e7 | 6356 | for (c = 0; c < children; c++) { |
d2f3e292 RY |
6357 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
6358 | cb->cb_name_flags); | |
c5eea0ab BB |
6359 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
6360 | B_TRUE); | |
1bd201e7 CS |
6361 | free(vname); |
6362 | } | |
6363 | } | |
6364 | } | |
6365 | ||
34dc7c2f BB |
6366 | /* |
6367 | * Generic callback function to list a pool. | |
6368 | */ | |
65c7cc49 | 6369 | static int |
34dc7c2f BB |
6370 | list_callback(zpool_handle_t *zhp, void *data) |
6371 | { | |
6372 | list_cbdata_t *cbp = data; | |
6373 | ||
c5eea0ab | 6374 | print_pool(zhp, cbp); |
34dc7c2f | 6375 | |
cc99f275 | 6376 | if (cbp->cb_verbose) { |
c5eea0ab | 6377 | nvlist_t *config, *nvroot; |
cc99f275 | 6378 | |
c5eea0ab | 6379 | config = zpool_get_config(zhp, NULL); |
cc99f275 DB |
6380 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, |
6381 | &nvroot) == 0); | |
c5eea0ab | 6382 | print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE); |
cc99f275 DB |
6383 | } |
6384 | ||
c5eea0ab BB |
6385 | return (0); |
6386 | } | |
cc99f275 | 6387 | |
c5eea0ab BB |
6388 | /* |
6389 | * Set the minimum pool/vdev name column width. The width must be at least 9, | |
6390 | * but may be as large as needed. | |
6391 | */ | |
6392 | static int | |
6393 | get_namewidth_list(zpool_handle_t *zhp, void *data) | |
6394 | { | |
6395 | list_cbdata_t *cb = data; | |
6396 | int width; | |
6397 | ||
7c0e3941 SW |
6398 | width = get_namewidth(zhp, cb->cb_namewidth, |
6399 | cb->cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose); | |
1bd201e7 | 6400 | |
c5eea0ab BB |
6401 | if (width < 9) |
6402 | width = 9; | |
6403 | ||
6404 | cb->cb_namewidth = width; | |
34dc7c2f BB |
6405 | |
6406 | return (0); | |
6407 | } | |
6408 | ||
6409 | /* | |
2a8b84b7 | 6410 | * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] |
34dc7c2f | 6411 | * |
d2f3e292 | 6412 | * -g Display guid for individual vdev name. |
34dc7c2f BB |
6413 | * -H Scripted mode. Don't display headers, and separate properties |
6414 | * by a single tab. | |
d2f3e292 | 6415 | * -L Follow links when resolving vdev path name. |
34dc7c2f | 6416 | * -o List of properties to display. Defaults to |
a05dfd00 GW |
6417 | * "name,size,allocated,free,expandsize,fragmentation,capacity," |
6418 | * "dedupratio,health,altroot" | |
c5eea0ab | 6419 | * -p Display values in parsable (exact) format. |
a77f29f9 | 6420 | * -P Display full path for vdev name. |
428870ff | 6421 | * -T Display a timestamp in date(1) or Unix format |
34dc7c2f BB |
6422 | * |
6423 | * List all pools in the system, whether or not they're healthy. Output space | |
6424 | * statistics for each one, as well as health status summary. | |
6425 | */ | |
6426 | int | |
6427 | zpool_do_list(int argc, char **argv) | |
6428 | { | |
6429 | int c; | |
cd72af9c | 6430 | int ret = 0; |
34dc7c2f BB |
6431 | list_cbdata_t cb = { 0 }; |
6432 | static char default_props[] = | |
d2734cce SD |
6433 | "name,size,allocated,free,checkpoint,expandsize,fragmentation," |
6434 | "capacity,dedupratio,health,altroot"; | |
34dc7c2f | 6435 | char *props = default_props; |
193a37cb TH |
6436 | float interval = 0; |
6437 | unsigned long count = 0; | |
1bd201e7 CS |
6438 | zpool_list_t *list; |
6439 | boolean_t first = B_TRUE; | |
2a673e76 | 6440 | current_prop_type = ZFS_TYPE_POOL; |
34dc7c2f BB |
6441 | |
6442 | /* check options */ | |
2a8b84b7 | 6443 | while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) { |
34dc7c2f | 6444 | switch (c) { |
d2f3e292 RY |
6445 | case 'g': |
6446 | cb.cb_name_flags |= VDEV_NAME_GUID; | |
6447 | break; | |
34dc7c2f BB |
6448 | case 'H': |
6449 | cb.cb_scripted = B_TRUE; | |
6450 | break; | |
d2f3e292 RY |
6451 | case 'L': |
6452 | cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
6453 | break; | |
34dc7c2f BB |
6454 | case 'o': |
6455 | props = optarg; | |
6456 | break; | |
a77f29f9 | 6457 | case 'P': |
d2f3e292 RY |
6458 | cb.cb_name_flags |= VDEV_NAME_PATH; |
6459 | break; | |
2a8b84b7 AS |
6460 | case 'p': |
6461 | cb.cb_literal = B_TRUE; | |
6462 | break; | |
428870ff BB |
6463 | case 'T': |
6464 | get_timestamp_arg(*optarg); | |
6465 | break; | |
1bd201e7 CS |
6466 | case 'v': |
6467 | cb.cb_verbose = B_TRUE; | |
cc99f275 | 6468 | cb.cb_namewidth = 8; /* 8 until precalc is avail */ |
1bd201e7 | 6469 | break; |
34dc7c2f BB |
6470 | case ':': |
6471 | (void) fprintf(stderr, gettext("missing argument for " | |
6472 | "'%c' option\n"), optopt); | |
6473 | usage(B_FALSE); | |
6474 | break; | |
6475 | case '?': | |
6476 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6477 | optopt); | |
6478 | usage(B_FALSE); | |
6479 | } | |
6480 | } | |
6481 | ||
6482 | argc -= optind; | |
6483 | argv += optind; | |
6484 | ||
428870ff BB |
6485 | get_interval_count(&argc, argv, &interval, &count); |
6486 | ||
34dc7c2f BB |
6487 | if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) |
6488 | usage(B_FALSE); | |
6489 | ||
428870ff | 6490 | for (;;) { |
3e43edd2 | 6491 | if ((list = pool_list_get(argc, argv, &cb.cb_proplist, |
2a673e76 | 6492 | ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL) |
3e43edd2 | 6493 | return (1); |
1bd201e7 CS |
6494 | |
6495 | if (pool_list_count(list) == 0) | |
6496 | break; | |
34dc7c2f | 6497 | |
c5eea0ab BB |
6498 | cb.cb_namewidth = 0; |
6499 | (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb); | |
6500 | ||
428870ff BB |
6501 | if (timestamp_fmt != NODATE) |
6502 | print_timestamp(timestamp_fmt); | |
34dc7c2f | 6503 | |
1bd201e7 CS |
6504 | if (!cb.cb_scripted && (first || cb.cb_verbose)) { |
6505 | print_header(&cb); | |
6506 | first = B_FALSE; | |
428870ff | 6507 | } |
1bd201e7 | 6508 | ret = pool_list_iter(list, B_TRUE, list_callback, &cb); |
428870ff BB |
6509 | |
6510 | if (interval == 0) | |
6511 | break; | |
6512 | ||
6513 | if (count != 0 && --count == 0) | |
6514 | break; | |
6515 | ||
3e43edd2 | 6516 | pool_list_free(list); |
193a37cb | 6517 | (void) fsleep(interval); |
34dc7c2f BB |
6518 | } |
6519 | ||
3e43edd2 GW |
6520 | if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { |
6521 | (void) printf(gettext("no pools available\n")); | |
6522 | ret = 0; | |
6523 | } | |
6524 | ||
6525 | pool_list_free(list); | |
428870ff | 6526 | zprop_free_list(cb.cb_proplist); |
34dc7c2f BB |
6527 | return (ret); |
6528 | } | |
6529 | ||
34dc7c2f BB |
6530 | static int |
6531 | zpool_do_attach_or_replace(int argc, char **argv, int replacing) | |
6532 | { | |
6533 | boolean_t force = B_FALSE; | |
9a49d3f3 | 6534 | boolean_t rebuild = B_FALSE; |
e60e158e | 6535 | boolean_t wait = B_FALSE; |
34dc7c2f BB |
6536 | int c; |
6537 | nvlist_t *nvroot; | |
6538 | char *poolname, *old_disk, *new_disk; | |
6539 | zpool_handle_t *zhp; | |
df831108 CP |
6540 | nvlist_t *props = NULL; |
6541 | char *propval; | |
34dc7c2f BB |
6542 | int ret; |
6543 | ||
6544 | /* check options */ | |
9a49d3f3 | 6545 | while ((c = getopt(argc, argv, "fo:sw")) != -1) { |
34dc7c2f BB |
6546 | switch (c) { |
6547 | case 'f': | |
6548 | force = B_TRUE; | |
6549 | break; | |
df831108 CP |
6550 | case 'o': |
6551 | if ((propval = strchr(optarg, '=')) == NULL) { | |
6552 | (void) fprintf(stderr, gettext("missing " | |
6553 | "'=' for -o option\n")); | |
6554 | usage(B_FALSE); | |
6555 | } | |
6556 | *propval = '\0'; | |
6557 | propval++; | |
6558 | ||
6559 | if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || | |
6560 | (add_prop_list(optarg, propval, &props, B_TRUE))) | |
6561 | usage(B_FALSE); | |
6562 | break; | |
9a49d3f3 BB |
6563 | case 's': |
6564 | rebuild = B_TRUE; | |
6565 | break; | |
e60e158e JG |
6566 | case 'w': |
6567 | wait = B_TRUE; | |
6568 | break; | |
34dc7c2f BB |
6569 | case '?': |
6570 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6571 | optopt); | |
6572 | usage(B_FALSE); | |
6573 | } | |
6574 | } | |
6575 | ||
6576 | argc -= optind; | |
6577 | argv += optind; | |
6578 | ||
6579 | /* get pool name and check number of arguments */ | |
6580 | if (argc < 1) { | |
6581 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
6582 | usage(B_FALSE); | |
6583 | } | |
6584 | ||
6585 | poolname = argv[0]; | |
6586 | ||
6587 | if (argc < 2) { | |
6588 | (void) fprintf(stderr, | |
6589 | gettext("missing <device> specification\n")); | |
6590 | usage(B_FALSE); | |
6591 | } | |
6592 | ||
6593 | old_disk = argv[1]; | |
6594 | ||
6595 | if (argc < 3) { | |
6596 | if (!replacing) { | |
6597 | (void) fprintf(stderr, | |
6598 | gettext("missing <new_device> specification\n")); | |
6599 | usage(B_FALSE); | |
6600 | } | |
6601 | new_disk = old_disk; | |
6602 | argc -= 1; | |
6603 | argv += 1; | |
6604 | } else { | |
6605 | new_disk = argv[2]; | |
6606 | argc -= 2; | |
6607 | argv += 2; | |
6608 | } | |
6609 | ||
6610 | if (argc > 1) { | |
6611 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
6612 | usage(B_FALSE); | |
6613 | } | |
6614 | ||
a425f5bf | 6615 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { |
6616 | nvlist_free(props); | |
34dc7c2f | 6617 | return (1); |
a425f5bf | 6618 | } |
34dc7c2f BB |
6619 | |
6620 | if (zpool_get_config(zhp, NULL) == NULL) { | |
6621 | (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), | |
6622 | poolname); | |
6623 | zpool_close(zhp); | |
a425f5bf | 6624 | nvlist_free(props); |
34dc7c2f BB |
6625 | return (1); |
6626 | } | |
6627 | ||
dddef7d6 | 6628 | /* unless manually specified use "ashift" pool property (if set) */ |
6629 | if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { | |
6630 | int intval; | |
6631 | zprop_source_t src; | |
6632 | char strval[ZPOOL_MAXPROPLEN]; | |
6633 | ||
6634 | intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); | |
6635 | if (src != ZPROP_SRC_DEFAULT) { | |
6636 | (void) sprintf(strval, "%" PRId32, intval); | |
6637 | verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, | |
6638 | &props, B_TRUE) == 0); | |
6639 | } | |
6640 | } | |
6641 | ||
df831108 | 6642 | nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE, |
b128c09f | 6643 | argc, argv); |
34dc7c2f BB |
6644 | if (nvroot == NULL) { |
6645 | zpool_close(zhp); | |
a425f5bf | 6646 | nvlist_free(props); |
34dc7c2f BB |
6647 | return (1); |
6648 | } | |
6649 | ||
9a49d3f3 BB |
6650 | ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing, |
6651 | rebuild); | |
34dc7c2f | 6652 | |
e60e158e JG |
6653 | if (ret == 0 && wait) |
6654 | ret = zpool_wait(zhp, | |
6655 | replacing ? ZPOOL_WAIT_REPLACE : ZPOOL_WAIT_RESILVER); | |
6656 | ||
a425f5bf | 6657 | nvlist_free(props); |
34dc7c2f BB |
6658 | nvlist_free(nvroot); |
6659 | zpool_close(zhp); | |
6660 | ||
6661 | return (ret); | |
6662 | } | |
6663 | ||
6664 | /* | |
9a49d3f3 | 6665 | * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device> |
34dc7c2f BB |
6666 | * |
6667 | * -f Force attach, even if <new_device> appears to be in use. | |
9a49d3f3 | 6668 | * -s Use sequential instead of healing reconstruction for resilver. |
e60e158e JG |
6669 | * -o Set property=value. |
6670 | * -w Wait for replacing to complete before returning | |
34dc7c2f BB |
6671 | * |
6672 | * Replace <device> with <new_device>. | |
6673 | */ | |
34dc7c2f BB |
6674 | int |
6675 | zpool_do_replace(int argc, char **argv) | |
6676 | { | |
6677 | return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); | |
6678 | } | |
6679 | ||
6680 | /* | |
9a49d3f3 | 6681 | * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device> |
34dc7c2f BB |
6682 | * |
6683 | * -f Force attach, even if <new_device> appears to be in use. | |
9a49d3f3 | 6684 | * -s Use sequential instead of healing reconstruction for resilver. |
df831108 | 6685 | * -o Set property=value. |
e60e158e | 6686 | * -w Wait for resilvering to complete before returning |
34dc7c2f BB |
6687 | * |
6688 | * Attach <new_device> to the mirror containing <device>. If <device> is not | |
6689 | * part of a mirror, then <device> will be transformed into a mirror of | |
6690 | * <device> and <new_device>. In either case, <new_device> will begin life | |
6691 | * with a DTL of [0, now], and will immediately begin to resilver itself. | |
6692 | */ | |
6693 | int | |
6694 | zpool_do_attach(int argc, char **argv) | |
6695 | { | |
6696 | return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); | |
6697 | } | |
6698 | ||
6699 | /* | |
6700 | * zpool detach [-f] <pool> <device> | |
6701 | * | |
6702 | * -f Force detach of <device>, even if DTLs argue against it | |
6703 | * (not supported yet) | |
6704 | * | |
6705 | * Detach a device from a mirror. The operation will be refused if <device> | |
6706 | * is the last device in the mirror, or if the DTLs indicate that this device | |
6707 | * has the only valid copy of some data. | |
6708 | */ | |
34dc7c2f BB |
6709 | int |
6710 | zpool_do_detach(int argc, char **argv) | |
6711 | { | |
6712 | int c; | |
6713 | char *poolname, *path; | |
6714 | zpool_handle_t *zhp; | |
6715 | int ret; | |
6716 | ||
6717 | /* check options */ | |
d02186ee | 6718 | while ((c = getopt(argc, argv, "")) != -1) { |
34dc7c2f | 6719 | switch (c) { |
34dc7c2f BB |
6720 | case '?': |
6721 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6722 | optopt); | |
6723 | usage(B_FALSE); | |
6724 | } | |
6725 | } | |
6726 | ||
6727 | argc -= optind; | |
6728 | argv += optind; | |
6729 | ||
6730 | /* get pool name and check number of arguments */ | |
6731 | if (argc < 1) { | |
6732 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
6733 | usage(B_FALSE); | |
6734 | } | |
6735 | ||
6736 | if (argc < 2) { | |
6737 | (void) fprintf(stderr, | |
6738 | gettext("missing <device> specification\n")); | |
6739 | usage(B_FALSE); | |
6740 | } | |
6741 | ||
6742 | poolname = argv[0]; | |
6743 | path = argv[1]; | |
6744 | ||
6745 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
6746 | return (1); | |
6747 | ||
6748 | ret = zpool_vdev_detach(zhp, path); | |
6749 | ||
6750 | zpool_close(zhp); | |
6751 | ||
6752 | return (ret); | |
6753 | } | |
6754 | ||
428870ff | 6755 | /* |
a77f29f9 | 6756 | * zpool split [-gLnP] [-o prop=val] ... |
428870ff BB |
6757 | * [-o mntopt] ... |
6758 | * [-R altroot] <pool> <newpool> [<device> ...] | |
6759 | * | |
d2f3e292 RY |
6760 | * -g Display guid for individual vdev name. |
6761 | * -L Follow links when resolving vdev path name. | |
428870ff BB |
6762 | * -n Do not split the pool, but display the resulting layout if |
6763 | * it were to be split. | |
6764 | * -o Set property=value, or set mount options. | |
a77f29f9 | 6765 | * -P Display full path for vdev name. |
428870ff | 6766 | * -R Mount the split-off pool under an alternate root. |
b5256303 | 6767 | * -l Load encryption keys while importing. |
428870ff BB |
6768 | * |
6769 | * Splits the named pool and gives it the new pool name. Devices to be split | |
6770 | * off may be listed, provided that no more than one device is specified | |
6771 | * per top-level vdev mirror. The newly split pool is left in an exported | |
6772 | * state unless -R is specified. | |
6773 | * | |
6774 | * Restrictions: the top-level of the pool pool must only be made up of | |
6775 | * mirrors; all devices in the pool must be healthy; no device may be | |
6776 | * undergoing a resilvering operation. | |
6777 | */ | |
6778 | int | |
6779 | zpool_do_split(int argc, char **argv) | |
6780 | { | |
6781 | char *srcpool, *newpool, *propval; | |
6782 | char *mntopts = NULL; | |
6783 | splitflags_t flags; | |
6784 | int c, ret = 0; | |
71472bf3 | 6785 | int ms_status = 0; |
b5256303 | 6786 | boolean_t loadkeys = B_FALSE; |
428870ff BB |
6787 | zpool_handle_t *zhp; |
6788 | nvlist_t *config, *props = NULL; | |
6789 | ||
6790 | flags.dryrun = B_FALSE; | |
6791 | flags.import = B_FALSE; | |
d2f3e292 | 6792 | flags.name_flags = 0; |
428870ff BB |
6793 | |
6794 | /* check options */ | |
b5256303 | 6795 | while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) { |
428870ff | 6796 | switch (c) { |
d2f3e292 RY |
6797 | case 'g': |
6798 | flags.name_flags |= VDEV_NAME_GUID; | |
6799 | break; | |
6800 | case 'L': | |
6801 | flags.name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
6802 | break; | |
428870ff BB |
6803 | case 'R': |
6804 | flags.import = B_TRUE; | |
6805 | if (add_prop_list( | |
6806 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, | |
6807 | &props, B_TRUE) != 0) { | |
8a5fc748 | 6808 | nvlist_free(props); |
428870ff BB |
6809 | usage(B_FALSE); |
6810 | } | |
6811 | break; | |
b5256303 TC |
6812 | case 'l': |
6813 | loadkeys = B_TRUE; | |
6814 | break; | |
428870ff BB |
6815 | case 'n': |
6816 | flags.dryrun = B_TRUE; | |
6817 | break; | |
6818 | case 'o': | |
6819 | if ((propval = strchr(optarg, '=')) != NULL) { | |
6820 | *propval = '\0'; | |
6821 | propval++; | |
6822 | if (add_prop_list(optarg, propval, | |
6823 | &props, B_TRUE) != 0) { | |
8a5fc748 | 6824 | nvlist_free(props); |
428870ff BB |
6825 | usage(B_FALSE); |
6826 | } | |
6827 | } else { | |
6828 | mntopts = optarg; | |
6829 | } | |
6830 | break; | |
a77f29f9 | 6831 | case 'P': |
d2f3e292 RY |
6832 | flags.name_flags |= VDEV_NAME_PATH; |
6833 | break; | |
428870ff BB |
6834 | case ':': |
6835 | (void) fprintf(stderr, gettext("missing argument for " | |
6836 | "'%c' option\n"), optopt); | |
6837 | usage(B_FALSE); | |
6838 | break; | |
6839 | case '?': | |
6840 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6841 | optopt); | |
6842 | usage(B_FALSE); | |
6843 | break; | |
6844 | } | |
6845 | } | |
6846 | ||
6847 | if (!flags.import && mntopts != NULL) { | |
6848 | (void) fprintf(stderr, gettext("setting mntopts is only " | |
6849 | "valid when importing the pool\n")); | |
6850 | usage(B_FALSE); | |
6851 | } | |
6852 | ||
b5256303 TC |
6853 | if (!flags.import && loadkeys) { |
6854 | (void) fprintf(stderr, gettext("loading keys is only " | |
6855 | "valid when importing the pool\n")); | |
6856 | usage(B_FALSE); | |
6857 | } | |
6858 | ||
428870ff BB |
6859 | argc -= optind; |
6860 | argv += optind; | |
6861 | ||
6862 | if (argc < 1) { | |
6863 | (void) fprintf(stderr, gettext("Missing pool name\n")); | |
6864 | usage(B_FALSE); | |
6865 | } | |
6866 | if (argc < 2) { | |
6867 | (void) fprintf(stderr, gettext("Missing new pool name\n")); | |
6868 | usage(B_FALSE); | |
6869 | } | |
6870 | ||
6871 | srcpool = argv[0]; | |
6872 | newpool = argv[1]; | |
6873 | ||
6874 | argc -= 2; | |
6875 | argv += 2; | |
6876 | ||
a425f5bf | 6877 | if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) { |
6878 | nvlist_free(props); | |
428870ff | 6879 | return (1); |
a425f5bf | 6880 | } |
428870ff BB |
6881 | |
6882 | config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); | |
6883 | if (config == NULL) { | |
6884 | ret = 1; | |
6885 | } else { | |
6886 | if (flags.dryrun) { | |
6887 | (void) printf(gettext("would create '%s' with the " | |
6888 | "following layout:\n\n"), newpool); | |
cc99f275 | 6889 | print_vdev_tree(NULL, newpool, config, 0, "", |
d2f3e292 | 6890 | flags.name_flags); |
0cb40fa3 AF |
6891 | print_vdev_tree(NULL, "dedup", config, 0, |
6892 | VDEV_ALLOC_BIAS_DEDUP, 0); | |
6893 | print_vdev_tree(NULL, "special", config, 0, | |
6894 | VDEV_ALLOC_BIAS_SPECIAL, 0); | |
428870ff | 6895 | } |
428870ff BB |
6896 | } |
6897 | ||
6898 | zpool_close(zhp); | |
6899 | ||
a425f5bf | 6900 | if (ret != 0 || flags.dryrun || !flags.import) { |
6901 | nvlist_free(config); | |
6902 | nvlist_free(props); | |
428870ff | 6903 | return (ret); |
a425f5bf | 6904 | } |
428870ff BB |
6905 | |
6906 | /* | |
6907 | * The split was successful. Now we need to open the new | |
6908 | * pool and import it. | |
6909 | */ | |
a425f5bf | 6910 | if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) { |
6911 | nvlist_free(config); | |
6912 | nvlist_free(props); | |
428870ff | 6913 | return (1); |
a425f5bf | 6914 | } |
b5256303 TC |
6915 | |
6916 | if (loadkeys) { | |
6917 | ret = zfs_crypto_attempt_load_keys(g_zfs, newpool); | |
6918 | if (ret != 0) | |
6919 | ret = 1; | |
6920 | } | |
6921 | ||
71472bf3 US |
6922 | if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) { |
6923 | ms_status = zpool_enable_datasets(zhp, mntopts, 0); | |
6924 | if (ms_status == EZFS_SHAREFAILED) { | |
6925 | (void) fprintf(stderr, gettext("Split was successful, " | |
6926 | "datasets are mounted but sharing of some datasets " | |
6927 | "has failed\n")); | |
6928 | } else if (ms_status == EZFS_MOUNTFAILED) { | |
6929 | (void) fprintf(stderr, gettext("Split was successful" | |
6930 | ", but some datasets could not be mounted\n")); | |
6931 | (void) fprintf(stderr, gettext("Try doing '%s' with a " | |
6932 | "different altroot\n"), "zpool import"); | |
6933 | } | |
428870ff BB |
6934 | } |
6935 | zpool_close(zhp); | |
a425f5bf | 6936 | nvlist_free(config); |
6937 | nvlist_free(props); | |
428870ff BB |
6938 | |
6939 | return (ret); | |
6940 | } | |
6941 | ||
6942 | ||
6943 | ||
34dc7c2f BB |
6944 | /* |
6945 | * zpool online <pool> <device> ... | |
6946 | */ | |
6947 | int | |
6948 | zpool_do_online(int argc, char **argv) | |
6949 | { | |
6950 | int c, i; | |
6951 | char *poolname; | |
6952 | zpool_handle_t *zhp; | |
6953 | int ret = 0; | |
6954 | vdev_state_t newstate; | |
9babb374 | 6955 | int flags = 0; |
34dc7c2f BB |
6956 | |
6957 | /* check options */ | |
d02186ee | 6958 | while ((c = getopt(argc, argv, "e")) != -1) { |
34dc7c2f | 6959 | switch (c) { |
9babb374 BB |
6960 | case 'e': |
6961 | flags |= ZFS_ONLINE_EXPAND; | |
6962 | break; | |
34dc7c2f BB |
6963 | case '?': |
6964 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6965 | optopt); | |
6966 | usage(B_FALSE); | |
6967 | } | |
6968 | } | |
6969 | ||
6970 | argc -= optind; | |
6971 | argv += optind; | |
6972 | ||
6973 | /* get pool name and check number of arguments */ | |
6974 | if (argc < 1) { | |
6975 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
6976 | usage(B_FALSE); | |
6977 | } | |
6978 | if (argc < 2) { | |
6979 | (void) fprintf(stderr, gettext("missing device name\n")); | |
6980 | usage(B_FALSE); | |
6981 | } | |
6982 | ||
6983 | poolname = argv[0]; | |
6984 | ||
6985 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
6986 | return (1); | |
6987 | ||
6988 | for (i = 1; i < argc; i++) { | |
b66c2a08 PD |
6989 | vdev_state_t oldstate; |
6990 | boolean_t avail_spare, l2cache; | |
6991 | nvlist_t *tgt = zpool_find_vdev(zhp, argv[i], &avail_spare, | |
6992 | &l2cache, NULL); | |
6993 | if (tgt == NULL) { | |
6994 | ret = 1; | |
6995 | continue; | |
6996 | } | |
6997 | uint_t vsc; | |
6998 | oldstate = ((vdev_stat_t *)fnvlist_lookup_uint64_array(tgt, | |
6999 | ZPOOL_CONFIG_VDEV_STATS, &vsc))->vs_state; | |
9babb374 | 7000 | if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { |
34dc7c2f BB |
7001 | if (newstate != VDEV_STATE_HEALTHY) { |
7002 | (void) printf(gettext("warning: device '%s' " | |
7003 | "onlined, but remains in faulted state\n"), | |
7004 | argv[i]); | |
7005 | if (newstate == VDEV_STATE_FAULTED) | |
7006 | (void) printf(gettext("use 'zpool " | |
7007 | "clear' to restore a faulted " | |
7008 | "device\n")); | |
7009 | else | |
7010 | (void) printf(gettext("use 'zpool " | |
7011 | "replace' to replace devices " | |
7012 | "that are no longer present\n")); | |
b66c2a08 PD |
7013 | if ((flags & ZFS_ONLINE_EXPAND)) { |
7014 | (void) printf(gettext("%s: failed " | |
7015 | "to expand usable space on " | |
7016 | "unhealthy device '%s'\n"), | |
7017 | (oldstate >= VDEV_STATE_DEGRADED ? | |
7018 | "error" : "warning"), argv[i]); | |
7019 | if (oldstate >= VDEV_STATE_DEGRADED) { | |
7020 | ret = 1; | |
7021 | break; | |
7022 | } | |
7023 | } | |
34dc7c2f BB |
7024 | } |
7025 | } else { | |
7026 | ret = 1; | |
7027 | } | |
7028 | } | |
7029 | ||
7030 | zpool_close(zhp); | |
7031 | ||
7032 | return (ret); | |
7033 | } | |
7034 | ||
7035 | /* | |
7036 | * zpool offline [-ft] <pool> <device> ... | |
7037 | * | |
4a283c7f | 7038 | * -f Force the device into a faulted state. |
34dc7c2f | 7039 | * |
4a283c7f | 7040 | * -t Only take the device off-line temporarily. The offline/faulted |
34dc7c2f BB |
7041 | * state will not be persistent across reboots. |
7042 | */ | |
34dc7c2f BB |
7043 | int |
7044 | zpool_do_offline(int argc, char **argv) | |
7045 | { | |
7046 | int c, i; | |
7047 | char *poolname; | |
7048 | zpool_handle_t *zhp; | |
7049 | int ret = 0; | |
7050 | boolean_t istmp = B_FALSE; | |
4a283c7f | 7051 | boolean_t fault = B_FALSE; |
34dc7c2f BB |
7052 | |
7053 | /* check options */ | |
7054 | while ((c = getopt(argc, argv, "ft")) != -1) { | |
7055 | switch (c) { | |
4a283c7f TH |
7056 | case 'f': |
7057 | fault = B_TRUE; | |
7058 | break; | |
34dc7c2f BB |
7059 | case 't': |
7060 | istmp = B_TRUE; | |
7061 | break; | |
34dc7c2f BB |
7062 | case '?': |
7063 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7064 | optopt); | |
7065 | usage(B_FALSE); | |
7066 | } | |
7067 | } | |
7068 | ||
7069 | argc -= optind; | |
7070 | argv += optind; | |
7071 | ||
7072 | /* get pool name and check number of arguments */ | |
7073 | if (argc < 1) { | |
7074 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
7075 | usage(B_FALSE); | |
7076 | } | |
7077 | if (argc < 2) { | |
7078 | (void) fprintf(stderr, gettext("missing device name\n")); | |
7079 | usage(B_FALSE); | |
7080 | } | |
7081 | ||
7082 | poolname = argv[0]; | |
7083 | ||
7084 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
7085 | return (1); | |
7086 | ||
7087 | for (i = 1; i < argc; i++) { | |
4a283c7f TH |
7088 | if (fault) { |
7089 | uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]); | |
7090 | vdev_aux_t aux; | |
7091 | if (istmp == B_FALSE) { | |
7092 | /* Force the fault to persist across imports */ | |
7093 | aux = VDEV_AUX_EXTERNAL_PERSIST; | |
7094 | } else { | |
7095 | aux = VDEV_AUX_EXTERNAL; | |
7096 | } | |
7097 | ||
7098 | if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0) | |
7099 | ret = 1; | |
7100 | } else { | |
7101 | if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) | |
7102 | ret = 1; | |
7103 | } | |
34dc7c2f BB |
7104 | } |
7105 | ||
7106 | zpool_close(zhp); | |
7107 | ||
7108 | return (ret); | |
7109 | } | |
7110 | ||
7111 | /* | |
7112 | * zpool clear <pool> [device] | |
7113 | * | |
7114 | * Clear all errors associated with a pool or a particular device. | |
7115 | */ | |
7116 | int | |
7117 | zpool_do_clear(int argc, char **argv) | |
7118 | { | |
428870ff | 7119 | int c; |
34dc7c2f | 7120 | int ret = 0; |
428870ff BB |
7121 | boolean_t dryrun = B_FALSE; |
7122 | boolean_t do_rewind = B_FALSE; | |
7123 | boolean_t xtreme_rewind = B_FALSE; | |
7124 | uint32_t rewind_policy = ZPOOL_NO_REWIND; | |
7125 | nvlist_t *policy = NULL; | |
34dc7c2f BB |
7126 | zpool_handle_t *zhp; |
7127 | char *pool, *device; | |
7128 | ||
428870ff BB |
7129 | /* check options */ |
7130 | while ((c = getopt(argc, argv, "FnX")) != -1) { | |
7131 | switch (c) { | |
7132 | case 'F': | |
7133 | do_rewind = B_TRUE; | |
7134 | break; | |
7135 | case 'n': | |
7136 | dryrun = B_TRUE; | |
7137 | break; | |
7138 | case 'X': | |
7139 | xtreme_rewind = B_TRUE; | |
7140 | break; | |
7141 | case '?': | |
7142 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7143 | optopt); | |
7144 | usage(B_FALSE); | |
7145 | } | |
7146 | } | |
7147 | ||
7148 | argc -= optind; | |
7149 | argv += optind; | |
7150 | ||
7151 | if (argc < 1) { | |
34dc7c2f BB |
7152 | (void) fprintf(stderr, gettext("missing pool name\n")); |
7153 | usage(B_FALSE); | |
7154 | } | |
7155 | ||
428870ff | 7156 | if (argc > 2) { |
34dc7c2f BB |
7157 | (void) fprintf(stderr, gettext("too many arguments\n")); |
7158 | usage(B_FALSE); | |
7159 | } | |
7160 | ||
428870ff BB |
7161 | if ((dryrun || xtreme_rewind) && !do_rewind) { |
7162 | (void) fprintf(stderr, | |
7163 | gettext("-n or -X only meaningful with -F\n")); | |
7164 | usage(B_FALSE); | |
7165 | } | |
7166 | if (dryrun) | |
7167 | rewind_policy = ZPOOL_TRY_REWIND; | |
7168 | else if (do_rewind) | |
7169 | rewind_policy = ZPOOL_DO_REWIND; | |
7170 | if (xtreme_rewind) | |
7171 | rewind_policy |= ZPOOL_EXTREME_REWIND; | |
7172 | ||
7173 | /* In future, further rewind policy choices can be passed along here */ | |
7174 | if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || | |
8a393be3 PZ |
7175 | nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, |
7176 | rewind_policy) != 0) { | |
428870ff | 7177 | return (1); |
8a393be3 | 7178 | } |
428870ff BB |
7179 | |
7180 | pool = argv[0]; | |
7181 | device = argc == 2 ? argv[1] : NULL; | |
34dc7c2f | 7182 | |
428870ff BB |
7183 | if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { |
7184 | nvlist_free(policy); | |
34dc7c2f | 7185 | return (1); |
428870ff | 7186 | } |
34dc7c2f | 7187 | |
428870ff | 7188 | if (zpool_clear(zhp, device, policy) != 0) |
34dc7c2f BB |
7189 | ret = 1; |
7190 | ||
7191 | zpool_close(zhp); | |
7192 | ||
428870ff BB |
7193 | nvlist_free(policy); |
7194 | ||
34dc7c2f BB |
7195 | return (ret); |
7196 | } | |
7197 | ||
3541dc6d GA |
7198 | /* |
7199 | * zpool reguid <pool> | |
7200 | */ | |
7201 | int | |
7202 | zpool_do_reguid(int argc, char **argv) | |
7203 | { | |
7204 | int c; | |
7205 | char *poolname; | |
7206 | zpool_handle_t *zhp; | |
7207 | int ret = 0; | |
7208 | ||
7209 | /* check options */ | |
7210 | while ((c = getopt(argc, argv, "")) != -1) { | |
7211 | switch (c) { | |
7212 | case '?': | |
7213 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7214 | optopt); | |
7215 | usage(B_FALSE); | |
7216 | } | |
7217 | } | |
7218 | ||
7219 | argc -= optind; | |
7220 | argv += optind; | |
7221 | ||
7222 | /* get pool name and check number of arguments */ | |
7223 | if (argc < 1) { | |
7224 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
7225 | usage(B_FALSE); | |
7226 | } | |
7227 | ||
7228 | if (argc > 1) { | |
7229 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
7230 | usage(B_FALSE); | |
7231 | } | |
7232 | ||
7233 | poolname = argv[0]; | |
7234 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
7235 | return (1); | |
7236 | ||
7237 | ret = zpool_reguid(zhp); | |
7238 | ||
7239 | zpool_close(zhp); | |
7240 | return (ret); | |
7241 | } | |
7242 | ||
7243 | ||
1bd201e7 CS |
7244 | /* |
7245 | * zpool reopen <pool> | |
7246 | * | |
7247 | * Reopen the pool so that the kernel can update the sizes of all vdevs. | |
1bd201e7 CS |
7248 | */ |
7249 | int | |
7250 | zpool_do_reopen(int argc, char **argv) | |
7251 | { | |
5853fe79 | 7252 | int c; |
1bd201e7 | 7253 | int ret = 0; |
d3f2cd7e | 7254 | boolean_t scrub_restart = B_TRUE; |
1bd201e7 | 7255 | |
5853fe79 | 7256 | /* check options */ |
d3f2cd7e | 7257 | while ((c = getopt(argc, argv, "n")) != -1) { |
5853fe79 | 7258 | switch (c) { |
d3f2cd7e AB |
7259 | case 'n': |
7260 | scrub_restart = B_FALSE; | |
7261 | break; | |
5853fe79 GW |
7262 | case '?': |
7263 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7264 | optopt); | |
7265 | usage(B_FALSE); | |
7266 | } | |
7267 | } | |
7268 | ||
d3f2cd7e AB |
7269 | argc -= optind; |
7270 | argv += optind; | |
1bd201e7 | 7271 | |
d3f2cd7e | 7272 | /* if argc == 0 we will execute zpool_reopen_one on all pools */ |
2a673e76 AJ |
7273 | ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
7274 | B_FALSE, zpool_reopen_one, &scrub_restart); | |
1bd201e7 | 7275 | |
1bd201e7 CS |
7276 | return (ret); |
7277 | } | |
7278 | ||
34dc7c2f BB |
7279 | typedef struct scrub_cbdata { |
7280 | int cb_type; | |
0ea05c64 | 7281 | pool_scrub_cmd_t cb_scrub_cmd; |
34dc7c2f BB |
7282 | } scrub_cbdata_t; |
7283 | ||
d2734cce SD |
7284 | static boolean_t |
7285 | zpool_has_checkpoint(zpool_handle_t *zhp) | |
7286 | { | |
7287 | nvlist_t *config, *nvroot; | |
7288 | ||
7289 | config = zpool_get_config(zhp, NULL); | |
7290 | ||
7291 | if (config != NULL) { | |
7292 | pool_checkpoint_stat_t *pcs = NULL; | |
7293 | uint_t c; | |
7294 | ||
7295 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); | |
7296 | (void) nvlist_lookup_uint64_array(nvroot, | |
7297 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
7298 | ||
7299 | if (pcs == NULL || pcs->pcs_state == CS_NONE) | |
7300 | return (B_FALSE); | |
7301 | ||
7302 | assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS || | |
7303 | pcs->pcs_state == CS_CHECKPOINT_DISCARDING); | |
7304 | return (B_TRUE); | |
7305 | } | |
7306 | ||
7307 | return (B_FALSE); | |
7308 | } | |
7309 | ||
65c7cc49 | 7310 | static int |
34dc7c2f BB |
7311 | scrub_callback(zpool_handle_t *zhp, void *data) |
7312 | { | |
7313 | scrub_cbdata_t *cb = data; | |
7314 | int err; | |
7315 | ||
7316 | /* | |
7317 | * Ignore faulted pools. | |
7318 | */ | |
7319 | if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { | |
80a91e74 | 7320 | (void) fprintf(stderr, gettext("cannot scan '%s': pool is " |
34dc7c2f BB |
7321 | "currently unavailable\n"), zpool_get_name(zhp)); |
7322 | return (1); | |
7323 | } | |
7324 | ||
0ea05c64 | 7325 | err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd); |
34dc7c2f | 7326 | |
d2734cce SD |
7327 | if (err == 0 && zpool_has_checkpoint(zhp) && |
7328 | cb->cb_type == POOL_SCAN_SCRUB) { | |
7329 | (void) printf(gettext("warning: will not scrub state that " | |
7330 | "belongs to the checkpoint of pool '%s'\n"), | |
7331 | zpool_get_name(zhp)); | |
7332 | } | |
7333 | ||
34dc7c2f BB |
7334 | return (err != 0); |
7335 | } | |
7336 | ||
e60e158e JG |
7337 | static int |
7338 | wait_callback(zpool_handle_t *zhp, void *data) | |
7339 | { | |
7340 | zpool_wait_activity_t *act = data; | |
7341 | return (zpool_wait(zhp, *act)); | |
7342 | } | |
7343 | ||
34dc7c2f | 7344 | /* |
482eeef8 | 7345 | * zpool scrub [-s | -p] [-w] [-e] <pool> ... |
34dc7c2f | 7346 | * |
482eeef8 | 7347 | * -e Only scrub blocks in the error log. |
34dc7c2f | 7348 | * -s Stop. Stops any in-progress scrub. |
0ea05c64 | 7349 | * -p Pause. Pause in-progress scrub. |
e60e158e | 7350 | * -w Wait. Blocks until scrub has completed. |
34dc7c2f BB |
7351 | */ |
7352 | int | |
7353 | zpool_do_scrub(int argc, char **argv) | |
7354 | { | |
7355 | int c; | |
7356 | scrub_cbdata_t cb; | |
e60e158e JG |
7357 | boolean_t wait = B_FALSE; |
7358 | int error; | |
34dc7c2f | 7359 | |
428870ff | 7360 | cb.cb_type = POOL_SCAN_SCRUB; |
0ea05c64 | 7361 | cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; |
34dc7c2f | 7362 | |
482eeef8 GA |
7363 | boolean_t is_error_scrub = B_FALSE; |
7364 | boolean_t is_pause = B_FALSE; | |
7365 | boolean_t is_stop = B_FALSE; | |
7366 | ||
34dc7c2f | 7367 | /* check options */ |
482eeef8 | 7368 | while ((c = getopt(argc, argv, "spwe")) != -1) { |
34dc7c2f | 7369 | switch (c) { |
482eeef8 GA |
7370 | case 'e': |
7371 | is_error_scrub = B_TRUE; | |
7372 | break; | |
34dc7c2f | 7373 | case 's': |
482eeef8 | 7374 | is_stop = B_TRUE; |
34dc7c2f | 7375 | break; |
0ea05c64 | 7376 | case 'p': |
482eeef8 | 7377 | is_pause = B_TRUE; |
0ea05c64 | 7378 | break; |
e60e158e JG |
7379 | case 'w': |
7380 | wait = B_TRUE; | |
7381 | break; | |
34dc7c2f BB |
7382 | case '?': |
7383 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7384 | optopt); | |
7385 | usage(B_FALSE); | |
7386 | } | |
7387 | } | |
7388 | ||
482eeef8 GA |
7389 | if (is_pause && is_stop) { |
7390 | (void) fprintf(stderr, gettext("invalid option " | |
7391 | "combination :-s and -p are mutually exclusive\n")); | |
0ea05c64 | 7392 | usage(B_FALSE); |
482eeef8 GA |
7393 | } else { |
7394 | if (is_error_scrub) | |
7395 | cb.cb_type = POOL_SCAN_ERRORSCRUB; | |
7396 | ||
7397 | if (is_pause) { | |
7398 | cb.cb_scrub_cmd = POOL_SCRUB_PAUSE; | |
7399 | } else if (is_stop) { | |
7400 | cb.cb_type = POOL_SCAN_NONE; | |
7401 | } else { | |
7402 | cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; | |
7403 | } | |
0ea05c64 AP |
7404 | } |
7405 | ||
e60e158e JG |
7406 | if (wait && (cb.cb_type == POOL_SCAN_NONE || |
7407 | cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) { | |
7408 | (void) fprintf(stderr, gettext("invalid option combination: " | |
7409 | "-w cannot be used with -p or -s\n")); | |
7410 | usage(B_FALSE); | |
7411 | } | |
7412 | ||
34dc7c2f BB |
7413 | argc -= optind; |
7414 | argv += optind; | |
7415 | ||
7416 | if (argc < 1) { | |
7417 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
7418 | usage(B_FALSE); | |
7419 | } | |
7420 | ||
2a673e76 AJ |
7421 | error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
7422 | B_FALSE, scrub_callback, &cb); | |
e60e158e JG |
7423 | |
7424 | if (wait && !error) { | |
7425 | zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB; | |
2a673e76 AJ |
7426 | error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
7427 | B_FALSE, wait_callback, &act); | |
e60e158e JG |
7428 | } |
7429 | ||
7430 | return (error); | |
34dc7c2f BB |
7431 | } |
7432 | ||
80a91e74 TC |
7433 | /* |
7434 | * zpool resilver <pool> ... | |
7435 | * | |
7436 | * Restarts any in-progress resilver | |
7437 | */ | |
7438 | int | |
7439 | zpool_do_resilver(int argc, char **argv) | |
7440 | { | |
7441 | int c; | |
7442 | scrub_cbdata_t cb; | |
7443 | ||
7444 | cb.cb_type = POOL_SCAN_RESILVER; | |
7445 | cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; | |
80a91e74 TC |
7446 | |
7447 | /* check options */ | |
7448 | while ((c = getopt(argc, argv, "")) != -1) { | |
7449 | switch (c) { | |
7450 | case '?': | |
7451 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7452 | optopt); | |
7453 | usage(B_FALSE); | |
7454 | } | |
7455 | } | |
7456 | ||
7457 | argc -= optind; | |
7458 | argv += optind; | |
7459 | ||
7460 | if (argc < 1) { | |
7461 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
7462 | usage(B_FALSE); | |
7463 | } | |
7464 | ||
2a673e76 AJ |
7465 | return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
7466 | B_FALSE, scrub_callback, &cb)); | |
80a91e74 TC |
7467 | } |
7468 | ||
1b939560 BB |
7469 | /* |
7470 | * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...] | |
7471 | * | |
7472 | * -c Cancel. Ends any in-progress trim. | |
7473 | * -d Secure trim. Requires kernel and device support. | |
7474 | * -r <rate> Sets the TRIM rate in bytes (per second). Supports | |
7475 | * adding a multiplier suffix such as 'k' or 'm'. | |
7476 | * -s Suspend. TRIM can then be restarted with no flags. | |
2288d419 | 7477 | * -w Wait. Blocks until trimming has completed. |
1b939560 BB |
7478 | */ |
7479 | int | |
7480 | zpool_do_trim(int argc, char **argv) | |
7481 | { | |
7482 | struct option long_options[] = { | |
7483 | {"cancel", no_argument, NULL, 'c'}, | |
7484 | {"secure", no_argument, NULL, 'd'}, | |
7485 | {"rate", required_argument, NULL, 'r'}, | |
7486 | {"suspend", no_argument, NULL, 's'}, | |
2288d419 | 7487 | {"wait", no_argument, NULL, 'w'}, |
1b939560 BB |
7488 | {0, 0, 0, 0} |
7489 | }; | |
7490 | ||
7491 | pool_trim_func_t cmd_type = POOL_TRIM_START; | |
7492 | uint64_t rate = 0; | |
7493 | boolean_t secure = B_FALSE; | |
2288d419 | 7494 | boolean_t wait = B_FALSE; |
1b939560 BB |
7495 | |
7496 | int c; | |
2288d419 | 7497 | while ((c = getopt_long(argc, argv, "cdr:sw", long_options, NULL)) |
1b939560 BB |
7498 | != -1) { |
7499 | switch (c) { | |
7500 | case 'c': | |
7501 | if (cmd_type != POOL_TRIM_START && | |
7502 | cmd_type != POOL_TRIM_CANCEL) { | |
7503 | (void) fprintf(stderr, gettext("-c cannot be " | |
7504 | "combined with other options\n")); | |
7505 | usage(B_FALSE); | |
7506 | } | |
7507 | cmd_type = POOL_TRIM_CANCEL; | |
7508 | break; | |
7509 | case 'd': | |
7510 | if (cmd_type != POOL_TRIM_START) { | |
7511 | (void) fprintf(stderr, gettext("-d cannot be " | |
7512 | "combined with the -c or -s options\n")); | |
7513 | usage(B_FALSE); | |
7514 | } | |
7515 | secure = B_TRUE; | |
7516 | break; | |
7517 | case 'r': | |
7518 | if (cmd_type != POOL_TRIM_START) { | |
7519 | (void) fprintf(stderr, gettext("-r cannot be " | |
7520 | "combined with the -c or -s options\n")); | |
7521 | usage(B_FALSE); | |
7522 | } | |
a4cecfbd TS |
7523 | if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) { |
7524 | (void) fprintf(stderr, "%s: %s\n", | |
7525 | gettext("invalid value for rate"), | |
7526 | libzfs_error_description(g_zfs)); | |
1b939560 BB |
7527 | usage(B_FALSE); |
7528 | } | |
7529 | break; | |
7530 | case 's': | |
7531 | if (cmd_type != POOL_TRIM_START && | |
7532 | cmd_type != POOL_TRIM_SUSPEND) { | |
7533 | (void) fprintf(stderr, gettext("-s cannot be " | |
7534 | "combined with other options\n")); | |
7535 | usage(B_FALSE); | |
7536 | } | |
7537 | cmd_type = POOL_TRIM_SUSPEND; | |
7538 | break; | |
2288d419 BB |
7539 | case 'w': |
7540 | wait = B_TRUE; | |
7541 | break; | |
1b939560 BB |
7542 | case '?': |
7543 | if (optopt != 0) { | |
7544 | (void) fprintf(stderr, | |
7545 | gettext("invalid option '%c'\n"), optopt); | |
7546 | } else { | |
7547 | (void) fprintf(stderr, | |
7548 | gettext("invalid option '%s'\n"), | |
7549 | argv[optind - 1]); | |
7550 | } | |
7551 | usage(B_FALSE); | |
7552 | } | |
7553 | } | |
7554 | ||
7555 | argc -= optind; | |
7556 | argv += optind; | |
7557 | ||
7558 | if (argc < 1) { | |
7559 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
7560 | usage(B_FALSE); | |
7561 | return (-1); | |
7562 | } | |
7563 | ||
2288d419 BB |
7564 | if (wait && (cmd_type != POOL_TRIM_START)) { |
7565 | (void) fprintf(stderr, gettext("-w cannot be used with -c or " | |
7566 | "-s\n")); | |
7567 | usage(B_FALSE); | |
7568 | } | |
7569 | ||
1b939560 BB |
7570 | char *poolname = argv[0]; |
7571 | zpool_handle_t *zhp = zpool_open(g_zfs, poolname); | |
7572 | if (zhp == NULL) | |
7573 | return (-1); | |
7574 | ||
7575 | trimflags_t trim_flags = { | |
7576 | .secure = secure, | |
7577 | .rate = rate, | |
2288d419 | 7578 | .wait = wait, |
1b939560 BB |
7579 | }; |
7580 | ||
7581 | nvlist_t *vdevs = fnvlist_alloc(); | |
7582 | if (argc == 1) { | |
7583 | /* no individual leaf vdevs specified, so add them all */ | |
7584 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
7585 | nvlist_t *nvroot = fnvlist_lookup_nvlist(config, | |
7586 | ZPOOL_CONFIG_VDEV_TREE); | |
7587 | zpool_collect_leaves(zhp, nvroot, vdevs); | |
7588 | trim_flags.fullpool = B_TRUE; | |
7589 | } else { | |
7590 | trim_flags.fullpool = B_FALSE; | |
7591 | for (int i = 1; i < argc; i++) { | |
7592 | fnvlist_add_boolean(vdevs, argv[i]); | |
7593 | } | |
7594 | } | |
7595 | ||
7596 | int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags); | |
7597 | ||
7598 | fnvlist_free(vdevs); | |
7599 | zpool_close(zhp); | |
7600 | ||
7601 | return (error); | |
7602 | } | |
80a91e74 | 7603 | |
9a49d3f3 BB |
7604 | /* |
7605 | * Converts a total number of seconds to a human readable string broken | |
7606 | * down in to days/hours/minutes/seconds. | |
7607 | */ | |
7608 | static void | |
7609 | secs_to_dhms(uint64_t total, char *buf) | |
7610 | { | |
7611 | uint64_t days = total / 60 / 60 / 24; | |
7612 | uint64_t hours = (total / 60 / 60) % 24; | |
7613 | uint64_t mins = (total / 60) % 60; | |
7614 | uint64_t secs = (total % 60); | |
7615 | ||
7616 | if (days > 0) { | |
7617 | (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu", | |
7618 | (u_longlong_t)days, (u_longlong_t)hours, | |
7619 | (u_longlong_t)mins, (u_longlong_t)secs); | |
7620 | } else { | |
7621 | (void) sprintf(buf, "%02llu:%02llu:%02llu", | |
7622 | (u_longlong_t)hours, (u_longlong_t)mins, | |
7623 | (u_longlong_t)secs); | |
7624 | } | |
7625 | } | |
7626 | ||
482eeef8 GA |
7627 | /* |
7628 | * Print out detailed error scrub status. | |
7629 | */ | |
7630 | static void | |
7631 | print_err_scrub_status(pool_scan_stat_t *ps) | |
7632 | { | |
7633 | time_t start, end, pause; | |
7634 | uint64_t total_secs_left; | |
7635 | uint64_t secs_left, mins_left, hours_left, days_left; | |
7636 | uint64_t examined, to_be_examined; | |
7637 | ||
7638 | if (ps == NULL || ps->pss_error_scrub_func != POOL_SCAN_ERRORSCRUB) { | |
7639 | return; | |
7640 | } | |
7641 | ||
7642 | (void) printf(gettext(" scrub: ")); | |
7643 | ||
7644 | start = ps->pss_error_scrub_start; | |
7645 | end = ps->pss_error_scrub_end; | |
7646 | pause = ps->pss_pass_error_scrub_pause; | |
7647 | examined = ps->pss_error_scrub_examined; | |
7648 | to_be_examined = ps->pss_error_scrub_to_be_examined; | |
7649 | ||
7650 | assert(ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB); | |
7651 | ||
7652 | if (ps->pss_error_scrub_state == DSS_FINISHED) { | |
7653 | total_secs_left = end - start; | |
7654 | days_left = total_secs_left / 60 / 60 / 24; | |
7655 | hours_left = (total_secs_left / 60 / 60) % 24; | |
7656 | mins_left = (total_secs_left / 60) % 60; | |
7657 | secs_left = (total_secs_left % 60); | |
7658 | ||
7659 | (void) printf(gettext("scrubbed %llu error blocks in %llu days " | |
7660 | "%02llu:%02llu:%02llu on %s"), (u_longlong_t)examined, | |
7661 | (u_longlong_t)days_left, (u_longlong_t)hours_left, | |
7662 | (u_longlong_t)mins_left, (u_longlong_t)secs_left, | |
7663 | ctime(&end)); | |
7664 | ||
7665 | return; | |
7666 | } else if (ps->pss_error_scrub_state == DSS_CANCELED) { | |
7667 | (void) printf(gettext("error scrub canceled on %s"), | |
7668 | ctime(&end)); | |
7669 | return; | |
7670 | } | |
7671 | assert(ps->pss_error_scrub_state == DSS_ERRORSCRUBBING); | |
7672 | ||
7673 | /* Error scrub is in progress. */ | |
7674 | if (pause == 0) { | |
7675 | (void) printf(gettext("error scrub in progress since %s"), | |
7676 | ctime(&start)); | |
7677 | } else { | |
7678 | (void) printf(gettext("error scrub paused since %s"), | |
7679 | ctime(&pause)); | |
7680 | (void) printf(gettext("\terror scrub started on %s"), | |
7681 | ctime(&start)); | |
7682 | } | |
7683 | ||
7684 | double fraction_done = (double)examined / (to_be_examined + examined); | |
7685 | (void) printf(gettext("\t%.2f%% done, issued I/O for %llu error" | |
7686 | " blocks"), 100 * fraction_done, (u_longlong_t)examined); | |
7687 | ||
7688 | (void) printf("\n"); | |
7689 | } | |
7690 | ||
34dc7c2f BB |
7691 | /* |
7692 | * Print out detailed scrub status. | |
7693 | */ | |
a1d477c2 | 7694 | static void |
9a49d3f3 | 7695 | print_scan_scrub_resilver_status(pool_scan_stat_t *ps) |
34dc7c2f | 7696 | { |
0ea05c64 | 7697 | time_t start, end, pause; |
fa7b2390 | 7698 | uint64_t pass_scanned, scanned, pass_issued, issued, total_s, total_i; |
9a49d3f3 | 7699 | uint64_t elapsed, scan_rate, issue_rate; |
34dc7c2f | 7700 | double fraction_done; |
fa7b2390 AM |
7701 | char processed_buf[7], scanned_buf[7], issued_buf[7], total_s_buf[7]; |
7702 | char total_i_buf[7], srate_buf[7], irate_buf[7], time_buf[32]; | |
34dc7c2f | 7703 | |
9fb2771a TH |
7704 | printf(" "); |
7705 | printf_color(ANSI_BOLD, gettext("scan:")); | |
7706 | printf(" "); | |
34dc7c2f | 7707 | |
428870ff BB |
7708 | /* If there's never been a scan, there's not much to say. */ |
7709 | if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || | |
7710 | ps->pss_func >= POOL_SCAN_FUNCS) { | |
34dc7c2f BB |
7711 | (void) printf(gettext("none requested\n")); |
7712 | return; | |
7713 | } | |
7714 | ||
428870ff BB |
7715 | start = ps->pss_start_time; |
7716 | end = ps->pss_end_time; | |
0ea05c64 | 7717 | pause = ps->pss_pass_scrub_pause; |
d4a72f23 | 7718 | |
e7fbeb60 | 7719 | zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf)); |
34dc7c2f | 7720 | |
c85ac731 BB |
7721 | int is_resilver = ps->pss_func == POOL_SCAN_RESILVER; |
7722 | int is_scrub = ps->pss_func == POOL_SCAN_SCRUB; | |
7723 | assert(is_resilver || is_scrub); | |
d4a72f23 TC |
7724 | |
7725 | /* Scan is finished or canceled. */ | |
428870ff | 7726 | if (ps->pss_state == DSS_FINISHED) { |
9a49d3f3 | 7727 | secs_to_dhms(end - start, time_buf); |
428870ff | 7728 | |
c85ac731 | 7729 | if (is_scrub) { |
d4a72f23 | 7730 | (void) printf(gettext("scrub repaired %s " |
9a49d3f3 BB |
7731 | "in %s with %llu errors on %s"), processed_buf, |
7732 | time_buf, (u_longlong_t)ps->pss_errors, | |
7733 | ctime(&end)); | |
c85ac731 | 7734 | } else if (is_resilver) { |
d4a72f23 | 7735 | (void) printf(gettext("resilvered %s " |
9a49d3f3 BB |
7736 | "in %s with %llu errors on %s"), processed_buf, |
7737 | time_buf, (u_longlong_t)ps->pss_errors, | |
7738 | ctime(&end)); | |
428870ff | 7739 | } |
428870ff BB |
7740 | return; |
7741 | } else if (ps->pss_state == DSS_CANCELED) { | |
c85ac731 | 7742 | if (is_scrub) { |
428870ff BB |
7743 | (void) printf(gettext("scrub canceled on %s"), |
7744 | ctime(&end)); | |
c85ac731 | 7745 | } else if (is_resilver) { |
428870ff BB |
7746 | (void) printf(gettext("resilver canceled on %s"), |
7747 | ctime(&end)); | |
7748 | } | |
34dc7c2f BB |
7749 | return; |
7750 | } | |
7751 | ||
428870ff BB |
7752 | assert(ps->pss_state == DSS_SCANNING); |
7753 | ||
d4a72f23 | 7754 | /* Scan is in progress. Resilvers can't be paused. */ |
c85ac731 | 7755 | if (is_scrub) { |
0ea05c64 AP |
7756 | if (pause == 0) { |
7757 | (void) printf(gettext("scrub in progress since %s"), | |
7758 | ctime(&start)); | |
7759 | } else { | |
d4a72f23 TC |
7760 | (void) printf(gettext("scrub paused since %s"), |
7761 | ctime(&pause)); | |
7762 | (void) printf(gettext("\tscrub started on %s"), | |
0ea05c64 AP |
7763 | ctime(&start)); |
7764 | } | |
c85ac731 | 7765 | } else if (is_resilver) { |
428870ff BB |
7766 | (void) printf(gettext("resilver in progress since %s"), |
7767 | ctime(&start)); | |
7768 | } | |
34dc7c2f | 7769 | |
d4a72f23 TC |
7770 | scanned = ps->pss_examined; |
7771 | pass_scanned = ps->pss_pass_exam; | |
7772 | issued = ps->pss_issued; | |
7773 | pass_issued = ps->pss_pass_issued; | |
fa7b2390 AM |
7774 | total_s = ps->pss_to_examine; |
7775 | total_i = ps->pss_to_examine - ps->pss_skipped; | |
428870ff | 7776 | |
d4a72f23 | 7777 | /* we are only done with a block once we have issued the IO for it */ |
fa7b2390 | 7778 | fraction_done = (double)issued / total_i; |
d4a72f23 TC |
7779 | |
7780 | /* elapsed time for this pass, rounding up to 1 if it's 0 */ | |
428870ff | 7781 | elapsed = time(NULL) - ps->pss_pass_start; |
0ea05c64 | 7782 | elapsed -= ps->pss_pass_scrub_spent_paused; |
d4a72f23 TC |
7783 | elapsed = (elapsed != 0) ? elapsed : 1; |
7784 | ||
7785 | scan_rate = pass_scanned / elapsed; | |
7786 | issue_rate = pass_issued / elapsed; | |
d4a72f23 TC |
7787 | |
7788 | /* format all of the numbers we will be reporting */ | |
7789 | zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf)); | |
7790 | zfs_nicebytes(issued, issued_buf, sizeof (issued_buf)); | |
fa7b2390 AM |
7791 | zfs_nicebytes(total_s, total_s_buf, sizeof (total_s_buf)); |
7792 | zfs_nicebytes(total_i, total_i_buf, sizeof (total_i_buf)); | |
428870ff | 7793 | |
d4a72f23 | 7794 | /* do not print estimated time if we have a paused scrub */ |
fa7b2390 AM |
7795 | (void) printf(gettext("\t%s / %s scanned"), scanned_buf, total_s_buf); |
7796 | if (pause == 0 && scan_rate > 0) { | |
7797 | zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf)); | |
7798 | (void) printf(gettext(" at %s/s"), srate_buf); | |
572e2857 | 7799 | } |
fa7b2390 AM |
7800 | (void) printf(gettext(", %s / %s issued"), issued_buf, total_i_buf); |
7801 | if (pause == 0 && issue_rate > 0) { | |
7802 | zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf)); | |
7803 | (void) printf(gettext(" at %s/s"), irate_buf); | |
7804 | } | |
7805 | (void) printf(gettext("\n")); | |
428870ff | 7806 | |
c85ac731 | 7807 | if (is_resilver) { |
d4a72f23 | 7808 | (void) printf(gettext("\t%s resilvered, %.2f%% done"), |
428870ff | 7809 | processed_buf, 100 * fraction_done); |
c85ac731 | 7810 | } else if (is_scrub) { |
d4a72f23 | 7811 | (void) printf(gettext("\t%s repaired, %.2f%% done"), |
428870ff BB |
7812 | processed_buf, 100 * fraction_done); |
7813 | } | |
d4a72f23 TC |
7814 | |
7815 | if (pause == 0) { | |
c85ac731 BB |
7816 | /* |
7817 | * Only provide an estimate iff: | |
fa7b2390 | 7818 | * 1) we haven't yet issued all we expected, and |
c85ac731 BB |
7819 | * 2) the issue rate exceeds 10 MB/s, and |
7820 | * 3) it's either: | |
7821 | * a) a resilver which has started repairs, or | |
7822 | * b) a scrub which has entered the issue phase. | |
7823 | */ | |
fa7b2390 | 7824 | if (total_i >= issued && issue_rate >= 10 * 1024 * 1024 && |
c85ac731 BB |
7825 | ((is_resilver && ps->pss_processed > 0) || |
7826 | (is_scrub && issued > 0))) { | |
fa7b2390 | 7827 | secs_to_dhms((total_i - issued) / issue_rate, time_buf); |
9a49d3f3 BB |
7828 | (void) printf(gettext(", %s to go\n"), time_buf); |
7829 | } else { | |
7830 | (void) printf(gettext(", no estimated " | |
7831 | "completion time\n")); | |
7832 | } | |
7833 | } else { | |
7834 | (void) printf(gettext("\n")); | |
7835 | } | |
7836 | } | |
7837 | ||
7838 | static void | |
fa7b2390 | 7839 | print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, uint_t c, char *vdev_name) |
9a49d3f3 BB |
7840 | { |
7841 | if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE) | |
7842 | return; | |
7843 | ||
7844 | printf(" "); | |
7845 | printf_color(ANSI_BOLD, gettext("scan:")); | |
7846 | printf(" "); | |
7847 | ||
7848 | uint64_t bytes_scanned = vrs->vrs_bytes_scanned; | |
7849 | uint64_t bytes_issued = vrs->vrs_bytes_issued; | |
7850 | uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt; | |
fa7b2390 AM |
7851 | uint64_t bytes_est_s = vrs->vrs_bytes_est; |
7852 | uint64_t bytes_est_i = vrs->vrs_bytes_est; | |
7853 | if (c > offsetof(vdev_rebuild_stat_t, vrs_pass_bytes_skipped) / 8) | |
7854 | bytes_est_i -= vrs->vrs_pass_bytes_skipped; | |
9a49d3f3 BB |
7855 | uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned / |
7856 | (vrs->vrs_pass_time_ms + 1)) * 1000; | |
7857 | uint64_t issue_rate = (vrs->vrs_pass_bytes_issued / | |
7858 | (vrs->vrs_pass_time_ms + 1)) * 1000; | |
7859 | double scan_pct = MIN((double)bytes_scanned * 100 / | |
fa7b2390 | 7860 | (bytes_est_s + 1), 100); |
9a49d3f3 BB |
7861 | |
7862 | /* Format all of the numbers we will be reporting */ | |
7863 | char bytes_scanned_buf[7], bytes_issued_buf[7]; | |
fa7b2390 | 7864 | char bytes_rebuilt_buf[7], bytes_est_s_buf[7], bytes_est_i_buf[7]; |
9a49d3f3 BB |
7865 | char scan_rate_buf[7], issue_rate_buf[7], time_buf[32]; |
7866 | zfs_nicebytes(bytes_scanned, bytes_scanned_buf, | |
7867 | sizeof (bytes_scanned_buf)); | |
7868 | zfs_nicebytes(bytes_issued, bytes_issued_buf, | |
7869 | sizeof (bytes_issued_buf)); | |
7870 | zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf, | |
7871 | sizeof (bytes_rebuilt_buf)); | |
fa7b2390 AM |
7872 | zfs_nicebytes(bytes_est_s, bytes_est_s_buf, sizeof (bytes_est_s_buf)); |
7873 | zfs_nicebytes(bytes_est_i, bytes_est_i_buf, sizeof (bytes_est_i_buf)); | |
9a49d3f3 BB |
7874 | |
7875 | time_t start = vrs->vrs_start_time; | |
7876 | time_t end = vrs->vrs_end_time; | |
7877 | ||
7878 | /* Rebuild is finished or canceled. */ | |
7879 | if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) { | |
7880 | secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf); | |
7881 | (void) printf(gettext("resilvered (%s) %s in %s " | |
7882 | "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf, | |
7883 | time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end)); | |
7884 | return; | |
7885 | } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) { | |
7886 | (void) printf(gettext("resilver (%s) canceled on %s"), | |
7887 | vdev_name, ctime(&end)); | |
7888 | return; | |
7889 | } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { | |
7890 | (void) printf(gettext("resilver (%s) in progress since %s"), | |
7891 | vdev_name, ctime(&start)); | |
7892 | } | |
7893 | ||
7894 | assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE); | |
7895 | ||
fa7b2390 AM |
7896 | (void) printf(gettext("\t%s / %s scanned"), bytes_scanned_buf, |
7897 | bytes_est_s_buf); | |
7898 | if (scan_rate > 0) { | |
7899 | zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf)); | |
7900 | (void) printf(gettext(" at %s/s"), scan_rate_buf); | |
7901 | } | |
7902 | (void) printf(gettext(", %s / %s issued"), bytes_issued_buf, | |
7903 | bytes_est_i_buf); | |
7904 | if (issue_rate > 0) { | |
7905 | zfs_nicebytes(issue_rate, issue_rate_buf, | |
7906 | sizeof (issue_rate_buf)); | |
7907 | (void) printf(gettext(" at %s/s"), issue_rate_buf); | |
7908 | } | |
7909 | (void) printf(gettext("\n")); | |
9a49d3f3 | 7910 | |
9a49d3f3 BB |
7911 | (void) printf(gettext("\t%s resilvered, %.2f%% done"), |
7912 | bytes_rebuilt_buf, scan_pct); | |
7913 | ||
7914 | if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { | |
fa7b2390 AM |
7915 | if (bytes_est_s >= bytes_scanned && |
7916 | scan_rate >= 10 * 1024 * 1024) { | |
7917 | secs_to_dhms((bytes_est_s - bytes_scanned) / scan_rate, | |
7918 | time_buf); | |
9a49d3f3 | 7919 | (void) printf(gettext(", %s to go\n"), time_buf); |
d4a72f23 TC |
7920 | } else { |
7921 | (void) printf(gettext(", no estimated " | |
7922 | "completion time\n")); | |
7923 | } | |
7924 | } else { | |
7925 | (void) printf(gettext("\n")); | |
7926 | } | |
34dc7c2f BB |
7927 | } |
7928 | ||
d2734cce | 7929 | /* |
9a49d3f3 BB |
7930 | * Print rebuild status for top-level vdevs. |
7931 | */ | |
7932 | static void | |
7933 | print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot) | |
7934 | { | |
7935 | nvlist_t **child; | |
7936 | uint_t children; | |
7937 | ||
7938 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
7939 | &child, &children) != 0) | |
7940 | children = 0; | |
7941 | ||
7942 | for (uint_t c = 0; c < children; c++) { | |
7943 | vdev_rebuild_stat_t *vrs; | |
7944 | uint_t i; | |
7945 | ||
7946 | if (nvlist_lookup_uint64_array(child[c], | |
7947 | ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { | |
7948 | char *name = zpool_vdev_name(g_zfs, zhp, | |
7949 | child[c], VDEV_NAME_TYPE_ID); | |
fa7b2390 | 7950 | print_rebuild_status_impl(vrs, i, name); |
9a49d3f3 BB |
7951 | free(name); |
7952 | } | |
7953 | } | |
7954 | } | |
7955 | ||
7956 | /* | |
7957 | * As we don't scrub checkpointed blocks, we want to warn the user that we | |
7958 | * skipped scanning some blocks if a checkpoint exists or existed at any | |
7959 | * time during the scan. If a sequential instead of healing reconstruction | |
7960 | * was performed then the blocks were reconstructed. However, their checksums | |
7961 | * have not been verified so we still print the warning. | |
d2734cce SD |
7962 | */ |
7963 | static void | |
7964 | print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs) | |
7965 | { | |
7966 | if (ps == NULL || pcs == NULL) | |
7967 | return; | |
7968 | ||
7969 | if (pcs->pcs_state == CS_NONE || | |
7970 | pcs->pcs_state == CS_CHECKPOINT_DISCARDING) | |
7971 | return; | |
7972 | ||
7973 | assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS); | |
7974 | ||
7975 | if (ps->pss_state == DSS_NONE) | |
7976 | return; | |
7977 | ||
7978 | if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) && | |
7979 | ps->pss_end_time < pcs->pcs_start_time) | |
7980 | return; | |
7981 | ||
7982 | if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) { | |
7983 | (void) printf(gettext(" scan warning: skipped blocks " | |
7984 | "that are only referenced by the checkpoint.\n")); | |
7985 | } else { | |
7986 | assert(ps->pss_state == DSS_SCANNING); | |
7987 | (void) printf(gettext(" scan warning: skipping blocks " | |
7988 | "that are only referenced by the checkpoint.\n")); | |
7989 | } | |
7990 | } | |
7991 | ||
9a49d3f3 BB |
7992 | /* |
7993 | * Returns B_TRUE if there is an active rebuild in progress. Otherwise, | |
7994 | * B_FALSE is returned and 'rebuild_end_time' is set to the end time for | |
7995 | * the last completed (or cancelled) rebuild. | |
7996 | */ | |
7997 | static boolean_t | |
7998 | check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time) | |
7999 | { | |
8000 | nvlist_t **child; | |
8001 | uint_t children; | |
8002 | boolean_t rebuilding = B_FALSE; | |
8003 | uint64_t end_time = 0; | |
8004 | ||
8005 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
8006 | &child, &children) != 0) | |
8007 | children = 0; | |
8008 | ||
8009 | for (uint_t c = 0; c < children; c++) { | |
8010 | vdev_rebuild_stat_t *vrs; | |
8011 | uint_t i; | |
8012 | ||
8013 | if (nvlist_lookup_uint64_array(child[c], | |
8014 | ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) { | |
8015 | ||
8016 | if (vrs->vrs_end_time > end_time) | |
8017 | end_time = vrs->vrs_end_time; | |
8018 | ||
8019 | if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { | |
8020 | rebuilding = B_TRUE; | |
8021 | end_time = 0; | |
8022 | break; | |
8023 | } | |
8024 | } | |
8025 | } | |
8026 | ||
8027 | if (rebuild_end_time != NULL) | |
8028 | *rebuild_end_time = end_time; | |
8029 | ||
8030 | return (rebuilding); | |
8031 | } | |
8032 | ||
8033 | /* | |
8034 | * Print the scan status. | |
8035 | */ | |
8036 | static void | |
8037 | print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot) | |
8038 | { | |
8039 | uint64_t rebuild_end_time = 0, resilver_end_time = 0; | |
8040 | boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE; | |
482eeef8 | 8041 | boolean_t have_errorscrub = B_FALSE; |
9a49d3f3 BB |
8042 | boolean_t active_resilver = B_FALSE; |
8043 | pool_checkpoint_stat_t *pcs = NULL; | |
8044 | pool_scan_stat_t *ps = NULL; | |
8045 | uint_t c; | |
482eeef8 | 8046 | time_t scrub_start = 0, errorscrub_start = 0; |
9a49d3f3 BB |
8047 | |
8048 | if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS, | |
8049 | (uint64_t **)&ps, &c) == 0) { | |
8050 | if (ps->pss_func == POOL_SCAN_RESILVER) { | |
8051 | resilver_end_time = ps->pss_end_time; | |
8052 | active_resilver = (ps->pss_state == DSS_SCANNING); | |
8053 | } | |
8054 | ||
8055 | have_resilver = (ps->pss_func == POOL_SCAN_RESILVER); | |
8056 | have_scrub = (ps->pss_func == POOL_SCAN_SCRUB); | |
482eeef8 | 8057 | scrub_start = ps->pss_start_time; |
fa7b2390 AM |
8058 | if (c > offsetof(pool_scan_stat_t, |
8059 | pss_pass_error_scrub_pause) / 8) { | |
8060 | have_errorscrub = (ps->pss_error_scrub_func == | |
8061 | POOL_SCAN_ERRORSCRUB); | |
8062 | errorscrub_start = ps->pss_error_scrub_start; | |
8063 | } | |
9a49d3f3 BB |
8064 | } |
8065 | ||
8066 | boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time); | |
8067 | boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0)); | |
8068 | ||
8069 | /* Always print the scrub status when available. */ | |
482eeef8 | 8070 | if (have_scrub && scrub_start > errorscrub_start) |
9a49d3f3 | 8071 | print_scan_scrub_resilver_status(ps); |
482eeef8 GA |
8072 | else if (have_errorscrub && errorscrub_start >= scrub_start) |
8073 | print_err_scrub_status(ps); | |
9a49d3f3 BB |
8074 | |
8075 | /* | |
8076 | * When there is an active resilver or rebuild print its status. | |
8077 | * Otherwise print the status of the last resilver or rebuild. | |
8078 | */ | |
8079 | if (active_resilver || (!active_rebuild && have_resilver && | |
8080 | resilver_end_time && resilver_end_time > rebuild_end_time)) { | |
8081 | print_scan_scrub_resilver_status(ps); | |
8082 | } else if (active_rebuild || (!active_resilver && have_rebuild && | |
8083 | rebuild_end_time && rebuild_end_time > resilver_end_time)) { | |
8084 | print_rebuild_status(zhp, nvroot); | |
8085 | } | |
8086 | ||
8087 | (void) nvlist_lookup_uint64_array(nvroot, | |
8088 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
8089 | print_checkpoint_scan_warning(ps, pcs); | |
8090 | } | |
8091 | ||
a1d477c2 MA |
8092 | /* |
8093 | * Print out detailed removal status. | |
8094 | */ | |
8095 | static void | |
8096 | print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs) | |
8097 | { | |
8098 | char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; | |
8099 | time_t start, end; | |
8100 | nvlist_t *config, *nvroot; | |
8101 | nvlist_t **child; | |
8102 | uint_t children; | |
8103 | char *vdev_name; | |
8104 | ||
8105 | if (prs == NULL || prs->prs_state == DSS_NONE) | |
8106 | return; | |
8107 | ||
8108 | /* | |
8109 | * Determine name of vdev. | |
8110 | */ | |
8111 | config = zpool_get_config(zhp, NULL); | |
8112 | nvroot = fnvlist_lookup_nvlist(config, | |
8113 | ZPOOL_CONFIG_VDEV_TREE); | |
8114 | verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
8115 | &child, &children) == 0); | |
8116 | assert(prs->prs_removing_vdev < children); | |
8117 | vdev_name = zpool_vdev_name(g_zfs, zhp, | |
8118 | child[prs->prs_removing_vdev], B_TRUE); | |
8119 | ||
95a78a03 | 8120 | printf_color(ANSI_BOLD, gettext("remove: ")); |
a1d477c2 MA |
8121 | |
8122 | start = prs->prs_start_time; | |
8123 | end = prs->prs_end_time; | |
8124 | zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf)); | |
8125 | ||
8126 | /* | |
8127 | * Removal is finished or canceled. | |
8128 | */ | |
8129 | if (prs->prs_state == DSS_FINISHED) { | |
8130 | uint64_t minutes_taken = (end - start) / 60; | |
8131 | ||
8132 | (void) printf(gettext("Removal of vdev %llu copied %s " | |
8133 | "in %lluh%um, completed on %s"), | |
8134 | (longlong_t)prs->prs_removing_vdev, | |
8135 | copied_buf, | |
8136 | (u_longlong_t)(minutes_taken / 60), | |
8137 | (uint_t)(minutes_taken % 60), | |
8138 | ctime((time_t *)&end)); | |
8139 | } else if (prs->prs_state == DSS_CANCELED) { | |
8140 | (void) printf(gettext("Removal of %s canceled on %s"), | |
8141 | vdev_name, ctime(&end)); | |
8142 | } else { | |
8143 | uint64_t copied, total, elapsed, mins_left, hours_left; | |
8144 | double fraction_done; | |
8145 | uint_t rate; | |
8146 | ||
8147 | assert(prs->prs_state == DSS_SCANNING); | |
8148 | ||
8149 | /* | |
8150 | * Removal is in progress. | |
8151 | */ | |
8152 | (void) printf(gettext( | |
8153 | "Evacuation of %s in progress since %s"), | |
8154 | vdev_name, ctime(&start)); | |
8155 | ||
8156 | copied = prs->prs_copied > 0 ? prs->prs_copied : 1; | |
8157 | total = prs->prs_to_copy; | |
8158 | fraction_done = (double)copied / total; | |
8159 | ||
8160 | /* elapsed time for this pass */ | |
8161 | elapsed = time(NULL) - prs->prs_start_time; | |
8162 | elapsed = elapsed > 0 ? elapsed : 1; | |
8163 | rate = copied / elapsed; | |
8164 | rate = rate > 0 ? rate : 1; | |
8165 | mins_left = ((total - copied) / rate) / 60; | |
8166 | hours_left = mins_left / 60; | |
8167 | ||
8168 | zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); | |
8169 | zfs_nicenum(total, total_buf, sizeof (total_buf)); | |
8170 | zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); | |
8171 | ||
8172 | /* | |
8173 | * do not print estimated time if hours_left is more than | |
8174 | * 30 days | |
8175 | */ | |
fd1c366f TL |
8176 | (void) printf(gettext( |
8177 | "\t%s copied out of %s at %s/s, %.2f%% done"), | |
a1d477c2 MA |
8178 | examined_buf, total_buf, rate_buf, 100 * fraction_done); |
8179 | if (hours_left < (30 * 24)) { | |
8180 | (void) printf(gettext(", %lluh%um to go\n"), | |
8181 | (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); | |
8182 | } else { | |
8183 | (void) printf(gettext( | |
8184 | ", (copy is slow, no estimated time)\n")); | |
8185 | } | |
8186 | } | |
75c62019 | 8187 | free(vdev_name); |
a1d477c2 MA |
8188 | |
8189 | if (prs->prs_mapping_memory > 0) { | |
8190 | char mem_buf[7]; | |
8191 | zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf)); | |
fd1c366f TL |
8192 | (void) printf(gettext( |
8193 | "\t%s memory used for removed device mappings\n"), | |
a1d477c2 MA |
8194 | mem_buf); |
8195 | } | |
8196 | } | |
8197 | ||
d2734cce SD |
8198 | static void |
8199 | print_checkpoint_status(pool_checkpoint_stat_t *pcs) | |
8200 | { | |
8201 | time_t start; | |
8202 | char space_buf[7]; | |
8203 | ||
8204 | if (pcs == NULL || pcs->pcs_state == CS_NONE) | |
8205 | return; | |
8206 | ||
8207 | (void) printf(gettext("checkpoint: ")); | |
8208 | ||
8209 | start = pcs->pcs_start_time; | |
8210 | zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf)); | |
8211 | ||
8212 | if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) { | |
8213 | char *date = ctime(&start); | |
8214 | ||
8215 | /* | |
8216 | * ctime() adds a newline at the end of the generated | |
8217 | * string, thus the weird format specifier and the | |
8218 | * strlen() call used to chop it off from the output. | |
8219 | */ | |
8220 | (void) printf(gettext("created %.*s, consumes %s\n"), | |
8221 | (int)(strlen(date) - 1), date, space_buf); | |
8222 | return; | |
8223 | } | |
8224 | ||
8225 | assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING); | |
8226 | ||
8227 | (void) printf(gettext("discarding, %s remaining.\n"), | |
8228 | space_buf); | |
8229 | } | |
8230 | ||
34dc7c2f BB |
8231 | static void |
8232 | print_error_log(zpool_handle_t *zhp) | |
8233 | { | |
8234 | nvlist_t *nverrlist = NULL; | |
8235 | nvpair_t *elem; | |
8236 | char *pathname; | |
8237 | size_t len = MAXPATHLEN * 2; | |
8238 | ||
56a6054d | 8239 | if (zpool_get_errlog(zhp, &nverrlist) != 0) |
34dc7c2f | 8240 | return; |
34dc7c2f BB |
8241 | |
8242 | (void) printf("errors: Permanent errors have been " | |
8243 | "detected in the following files:\n\n"); | |
8244 | ||
8245 | pathname = safe_malloc(len); | |
8246 | elem = NULL; | |
8247 | while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { | |
8248 | nvlist_t *nv; | |
8249 | uint64_t dsobj, obj; | |
8250 | ||
8251 | verify(nvpair_value_nvlist(elem, &nv) == 0); | |
8252 | verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, | |
8253 | &dsobj) == 0); | |
8254 | verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, | |
8255 | &obj) == 0); | |
8256 | zpool_obj_to_path(zhp, dsobj, obj, pathname, len); | |
8257 | (void) printf("%7s %s\n", "", pathname); | |
8258 | } | |
8259 | free(pathname); | |
8260 | nvlist_free(nverrlist); | |
8261 | } | |
8262 | ||
8263 | static void | |
fea33e4e HJ |
8264 | print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares, |
8265 | uint_t nspares) | |
34dc7c2f BB |
8266 | { |
8267 | uint_t i; | |
8268 | char *name; | |
8269 | ||
8270 | if (nspares == 0) | |
8271 | return; | |
8272 | ||
8273 | (void) printf(gettext("\tspares\n")); | |
8274 | ||
8275 | for (i = 0; i < nspares; i++) { | |
fea33e4e HJ |
8276 | name = zpool_vdev_name(g_zfs, zhp, spares[i], |
8277 | cb->cb_name_flags); | |
9a49d3f3 | 8278 | print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL); |
34dc7c2f BB |
8279 | free(name); |
8280 | } | |
8281 | } | |
8282 | ||
8283 | static void | |
fea33e4e HJ |
8284 | print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache, |
8285 | uint_t nl2cache) | |
34dc7c2f BB |
8286 | { |
8287 | uint_t i; | |
8288 | char *name; | |
8289 | ||
8290 | if (nl2cache == 0) | |
8291 | return; | |
8292 | ||
8293 | (void) printf(gettext("\tcache\n")); | |
8294 | ||
8295 | for (i = 0; i < nl2cache; i++) { | |
fea33e4e HJ |
8296 | name = zpool_vdev_name(g_zfs, zhp, l2cache[i], |
8297 | cb->cb_name_flags); | |
9a49d3f3 BB |
8298 | print_status_config(zhp, cb, name, l2cache[i], 2, |
8299 | B_FALSE, NULL); | |
34dc7c2f BB |
8300 | free(name); |
8301 | } | |
8302 | } | |
8303 | ||
428870ff BB |
8304 | static void |
8305 | print_dedup_stats(nvlist_t *config) | |
8306 | { | |
8307 | ddt_histogram_t *ddh; | |
8308 | ddt_stat_t *dds; | |
8309 | ddt_object_t *ddo; | |
8310 | uint_t c; | |
e7fbeb60 | 8311 | char dspace[6], mspace[6]; |
428870ff BB |
8312 | |
8313 | /* | |
8314 | * If the pool was faulted then we may not have been able to | |
32a9872b | 8315 | * obtain the config. Otherwise, if we have anything in the dedup |
428870ff BB |
8316 | * table continue processing the stats. |
8317 | */ | |
8318 | if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, | |
24024589 | 8319 | (uint64_t **)&ddo, &c) != 0) |
428870ff BB |
8320 | return; |
8321 | ||
8322 | (void) printf("\n"); | |
24024589 YP |
8323 | (void) printf(gettext(" dedup: ")); |
8324 | if (ddo->ddo_count == 0) { | |
8325 | (void) printf(gettext("no DDT entries\n")); | |
8326 | return; | |
8327 | } | |
8328 | ||
e7fbeb60 | 8329 | zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace)); |
8330 | zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace)); | |
8331 | (void) printf("DDT entries %llu, size %s on disk, %s in core\n", | |
428870ff | 8332 | (u_longlong_t)ddo->ddo_count, |
e7fbeb60 | 8333 | dspace, |
8334 | mspace); | |
428870ff BB |
8335 | |
8336 | verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, | |
8337 | (uint64_t **)&dds, &c) == 0); | |
8338 | verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, | |
8339 | (uint64_t **)&ddh, &c) == 0); | |
8340 | zpool_dump_ddt(dds, ddh); | |
8341 | } | |
8342 | ||
34dc7c2f BB |
8343 | /* |
8344 | * Display a summary of pool status. Displays a summary such as: | |
8345 | * | |
8346 | * pool: tank | |
8347 | * status: DEGRADED | |
8348 | * reason: One or more devices ... | |
a2f944a1 | 8349 | * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01 |
34dc7c2f BB |
8350 | * config: |
8351 | * mirror DEGRADED | |
8352 | * c1t0d0 OK | |
8353 | * c2t0d0 UNAVAIL | |
8354 | * | |
8355 | * When given the '-v' option, we print out the complete config. If the '-e' | |
8356 | * option is specified, then we print out error rate information as well. | |
8357 | */ | |
65c7cc49 | 8358 | static int |
34dc7c2f BB |
8359 | status_callback(zpool_handle_t *zhp, void *data) |
8360 | { | |
8361 | status_cbdata_t *cbp = data; | |
8362 | nvlist_t *config, *nvroot; | |
a926aab9 | 8363 | const char *msgid; |
731782ec | 8364 | zpool_status_t reason; |
ffe9d382 | 8365 | zpool_errata_t errata; |
34dc7c2f BB |
8366 | const char *health; |
8367 | uint_t c; | |
8368 | vdev_stat_t *vs; | |
8369 | ||
8370 | config = zpool_get_config(zhp, NULL); | |
ffe9d382 | 8371 | reason = zpool_get_status(zhp, &msgid, &errata); |
34dc7c2f BB |
8372 | |
8373 | cbp->cb_count++; | |
8374 | ||
8375 | /* | |
8376 | * If we were given 'zpool status -x', only report those pools with | |
8377 | * problems. | |
8378 | */ | |
c5b247f3 TC |
8379 | if (cbp->cb_explain && |
8380 | (reason == ZPOOL_STATUS_OK || | |
8381 | reason == ZPOOL_STATUS_VERSION_OLDER || | |
658fb802 | 8382 | reason == ZPOOL_STATUS_FEAT_DISABLED || |
e086db16 CB |
8383 | reason == ZPOOL_STATUS_COMPATIBILITY_ERR || |
8384 | reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) { | |
34dc7c2f BB |
8385 | if (!cbp->cb_allpools) { |
8386 | (void) printf(gettext("pool '%s' is healthy\n"), | |
8387 | zpool_get_name(zhp)); | |
8388 | if (cbp->cb_first) | |
8389 | cbp->cb_first = B_FALSE; | |
8390 | } | |
8391 | return (0); | |
8392 | } | |
8393 | ||
8394 | if (cbp->cb_first) | |
8395 | cbp->cb_first = B_FALSE; | |
8396 | else | |
8397 | (void) printf("\n"); | |
8398 | ||
a1d477c2 | 8399 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
428870ff | 8400 | verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f | 8401 | (uint64_t **)&vs, &c) == 0); |
f0ed6c74 TH |
8402 | |
8403 | health = zpool_get_state_str(zhp); | |
34dc7c2f | 8404 | |
9fb2771a TH |
8405 | printf(" "); |
8406 | printf_color(ANSI_BOLD, gettext("pool:")); | |
8407 | printf(" %s\n", zpool_get_name(zhp)); | |
a926aab9 | 8408 | fputc(' ', stdout); |
9fb2771a TH |
8409 | printf_color(ANSI_BOLD, gettext("state: ")); |
8410 | ||
8411 | printf_color(health_str_to_color(health), "%s", health); | |
8412 | ||
a926aab9 | 8413 | fputc('\n', stdout); |
34dc7c2f BB |
8414 | |
8415 | switch (reason) { | |
8416 | case ZPOOL_STATUS_MISSING_DEV_R: | |
9fb2771a TH |
8417 | printf_color(ANSI_BOLD, gettext("status: ")); |
8418 | printf_color(ANSI_YELLOW, gettext("One or more devices could " | |
8419 | "not be opened. Sufficient replicas exist for\n\tthe pool " | |
8420 | "to continue functioning in a degraded state.\n")); | |
8421 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8422 | printf_color(ANSI_YELLOW, gettext("Attach the missing device " | |
8423 | "and online it using 'zpool online'.\n")); | |
34dc7c2f BB |
8424 | break; |
8425 | ||
8426 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
9fb2771a TH |
8427 | printf_color(ANSI_BOLD, gettext("status: ")); |
8428 | printf_color(ANSI_YELLOW, gettext("One or more devices could " | |
8429 | "not be opened. There are insufficient\n\treplicas for the" | |
8430 | " pool to continue functioning.\n")); | |
8431 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8432 | printf_color(ANSI_YELLOW, gettext("Attach the missing device " | |
8433 | "and online it using 'zpool online'.\n")); | |
34dc7c2f BB |
8434 | break; |
8435 | ||
8436 | case ZPOOL_STATUS_CORRUPT_LABEL_R: | |
9fb2771a TH |
8437 | printf_color(ANSI_BOLD, gettext("status: ")); |
8438 | printf_color(ANSI_YELLOW, gettext("One or more devices could " | |
8439 | "not be used because the label is missing or\n\tinvalid. " | |
34dc7c2f BB |
8440 | "Sufficient replicas exist for the pool to continue\n\t" |
8441 | "functioning in a degraded state.\n")); | |
9fb2771a TH |
8442 | printf_color(ANSI_BOLD, gettext("action: ")); |
8443 | printf_color(ANSI_YELLOW, gettext("Replace the device using " | |
34dc7c2f BB |
8444 | "'zpool replace'.\n")); |
8445 | break; | |
8446 | ||
8447 | case ZPOOL_STATUS_CORRUPT_LABEL_NR: | |
9fb2771a TH |
8448 | printf_color(ANSI_BOLD, gettext("status: ")); |
8449 | printf_color(ANSI_YELLOW, gettext("One or more devices could " | |
8450 | "not be used because the label is missing \n\tor invalid. " | |
34dc7c2f BB |
8451 | "There are insufficient replicas for the pool to " |
8452 | "continue\n\tfunctioning.\n")); | |
428870ff BB |
8453 | zpool_explain_recover(zpool_get_handle(zhp), |
8454 | zpool_get_name(zhp), reason, config); | |
34dc7c2f BB |
8455 | break; |
8456 | ||
8457 | case ZPOOL_STATUS_FAILING_DEV: | |
9fb2771a TH |
8458 | printf_color(ANSI_BOLD, gettext("status: ")); |
8459 | printf_color(ANSI_YELLOW, gettext("One or more devices has " | |
34dc7c2f BB |
8460 | "experienced an unrecoverable error. An\n\tattempt was " |
8461 | "made to correct the error. Applications are " | |
8462 | "unaffected.\n")); | |
9fb2771a TH |
8463 | printf_color(ANSI_BOLD, gettext("action: ")); |
8464 | printf_color(ANSI_YELLOW, gettext("Determine if the " | |
8465 | "device needs to be replaced, and clear the errors\n\tusing" | |
8466 | " 'zpool clear' or replace the device with 'zpool " | |
34dc7c2f BB |
8467 | "replace'.\n")); |
8468 | break; | |
8469 | ||
8470 | case ZPOOL_STATUS_OFFLINE_DEV: | |
9fb2771a TH |
8471 | printf_color(ANSI_BOLD, gettext("status: ")); |
8472 | printf_color(ANSI_YELLOW, gettext("One or more devices has " | |
34dc7c2f BB |
8473 | "been taken offline by the administrator.\n\tSufficient " |
8474 | "replicas exist for the pool to continue functioning in " | |
8475 | "a\n\tdegraded state.\n")); | |
9fb2771a TH |
8476 | printf_color(ANSI_BOLD, gettext("action: ")); |
8477 | printf_color(ANSI_YELLOW, gettext("Online the device " | |
8478 | "using 'zpool online' or replace the device with\n\t'zpool " | |
34dc7c2f BB |
8479 | "replace'.\n")); |
8480 | break; | |
8481 | ||
45d1cae3 | 8482 | case ZPOOL_STATUS_REMOVED_DEV: |
9fb2771a TH |
8483 | printf_color(ANSI_BOLD, gettext("status: ")); |
8484 | printf_color(ANSI_YELLOW, gettext("One or more devices has " | |
45d1cae3 BB |
8485 | "been removed by the administrator.\n\tSufficient " |
8486 | "replicas exist for the pool to continue functioning in " | |
8487 | "a\n\tdegraded state.\n")); | |
9fb2771a TH |
8488 | printf_color(ANSI_BOLD, gettext("action: ")); |
8489 | printf_color(ANSI_YELLOW, gettext("Online the device " | |
8490 | "using zpool online' or replace the device with\n\t'zpool " | |
45d1cae3 BB |
8491 | "replace'.\n")); |
8492 | break; | |
8493 | ||
34dc7c2f | 8494 | case ZPOOL_STATUS_RESILVERING: |
9a49d3f3 | 8495 | case ZPOOL_STATUS_REBUILDING: |
9fb2771a TH |
8496 | printf_color(ANSI_BOLD, gettext("status: ")); |
8497 | printf_color(ANSI_YELLOW, gettext("One or more devices is " | |
34dc7c2f BB |
8498 | "currently being resilvered. The pool will\n\tcontinue " |
8499 | "to function, possibly in a degraded state.\n")); | |
9fb2771a TH |
8500 | printf_color(ANSI_BOLD, gettext("action: ")); |
8501 | printf_color(ANSI_YELLOW, gettext("Wait for the resilver to " | |
34dc7c2f BB |
8502 | "complete.\n")); |
8503 | break; | |
8504 | ||
9a49d3f3 BB |
8505 | case ZPOOL_STATUS_REBUILD_SCRUB: |
8506 | printf_color(ANSI_BOLD, gettext("status: ")); | |
8507 | printf_color(ANSI_YELLOW, gettext("One or more devices have " | |
8508 | "been sequentially resilvered, scrubbing\n\tthe pool " | |
8509 | "is recommended.\n")); | |
8510 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8511 | printf_color(ANSI_YELLOW, gettext("Use 'zpool scrub' to " | |
8512 | "verify all data checksums.\n")); | |
8513 | break; | |
8514 | ||
34dc7c2f | 8515 | case ZPOOL_STATUS_CORRUPT_DATA: |
9fb2771a TH |
8516 | printf_color(ANSI_BOLD, gettext("status: ")); |
8517 | printf_color(ANSI_YELLOW, gettext("One or more devices has " | |
34dc7c2f BB |
8518 | "experienced an error resulting in data\n\tcorruption. " |
8519 | "Applications may be affected.\n")); | |
9fb2771a TH |
8520 | printf_color(ANSI_BOLD, gettext("action: ")); |
8521 | printf_color(ANSI_YELLOW, gettext("Restore the file in question" | |
8522 | " if possible. Otherwise restore the\n\tentire pool from " | |
34dc7c2f BB |
8523 | "backup.\n")); |
8524 | break; | |
8525 | ||
8526 | case ZPOOL_STATUS_CORRUPT_POOL: | |
9fb2771a TH |
8527 | printf_color(ANSI_BOLD, gettext("status: ")); |
8528 | printf_color(ANSI_YELLOW, gettext("The pool metadata is " | |
8529 | "corrupted and the pool cannot be opened.\n")); | |
428870ff BB |
8530 | zpool_explain_recover(zpool_get_handle(zhp), |
8531 | zpool_get_name(zhp), reason, config); | |
34dc7c2f BB |
8532 | break; |
8533 | ||
8534 | case ZPOOL_STATUS_VERSION_OLDER: | |
9fb2771a TH |
8535 | printf_color(ANSI_BOLD, gettext("status: ")); |
8536 | printf_color(ANSI_YELLOW, gettext("The pool is formatted using " | |
8537 | "a legacy on-disk format. The pool can\n\tstill be used, " | |
b9b24bb4 | 8538 | "but some features are unavailable.\n")); |
9fb2771a TH |
8539 | printf_color(ANSI_BOLD, gettext("action: ")); |
8540 | printf_color(ANSI_YELLOW, gettext("Upgrade the pool using " | |
8541 | "'zpool upgrade'. Once this is done, the\n\tpool will no " | |
8542 | "longer be accessible on software that does not support\n\t" | |
f52b31ea | 8543 | "feature flags.\n")); |
34dc7c2f BB |
8544 | break; |
8545 | ||
8546 | case ZPOOL_STATUS_VERSION_NEWER: | |
9fb2771a TH |
8547 | printf_color(ANSI_BOLD, gettext("status: ")); |
8548 | printf_color(ANSI_YELLOW, gettext("The pool has been upgraded " | |
8549 | "to a newer, incompatible on-disk version.\n\tThe pool " | |
8550 | "cannot be accessed on this system.\n")); | |
8551 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8552 | printf_color(ANSI_YELLOW, gettext("Access the pool from a " | |
8553 | "system running more recent software, or\n\trestore the " | |
8554 | "pool from backup.\n")); | |
34dc7c2f BB |
8555 | break; |
8556 | ||
b9b24bb4 | 8557 | case ZPOOL_STATUS_FEAT_DISABLED: |
9fb2771a | 8558 | printf_color(ANSI_BOLD, gettext("status: ")); |
658fb802 CB |
8559 | printf_color(ANSI_YELLOW, gettext("Some supported and " |
8560 | "requested features are not enabled on the pool.\n\t" | |
8561 | "The pool can still be used, but some features are " | |
8562 | "unavailable.\n")); | |
9fb2771a TH |
8563 | printf_color(ANSI_BOLD, gettext("action: ")); |
8564 | printf_color(ANSI_YELLOW, gettext("Enable all features using " | |
b9b24bb4 CS |
8565 | "'zpool upgrade'. Once this is done,\n\tthe pool may no " |
8566 | "longer be accessible by software that does not support\n\t" | |
2badb345 | 8567 | "the features. See zpool-features(7) for details.\n")); |
b9b24bb4 CS |
8568 | break; |
8569 | ||
658fb802 CB |
8570 | case ZPOOL_STATUS_COMPATIBILITY_ERR: |
8571 | printf_color(ANSI_BOLD, gettext("status: ")); | |
8572 | printf_color(ANSI_YELLOW, gettext("This pool has a " | |
8573 | "compatibility list specified, but it could not be\n\t" | |
8574 | "read/parsed at this time. The pool can still be used, " | |
8575 | "but this\n\tshould be investigated.\n")); | |
8576 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8577 | printf_color(ANSI_YELLOW, gettext("Check the value of the " | |
8578 | "'compatibility' property against the\n\t" | |
8579 | "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or " | |
8580 | ZPOOL_DATA_COMPAT_D ".\n")); | |
8581 | break; | |
8582 | ||
e086db16 CB |
8583 | case ZPOOL_STATUS_INCOMPATIBLE_FEAT: |
8584 | printf_color(ANSI_BOLD, gettext("status: ")); | |
8585 | printf_color(ANSI_YELLOW, gettext("One or more features " | |
8586 | "are enabled on the pool despite not being\n\t" | |
8587 | "requested by the 'compatibility' property.\n")); | |
8588 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8589 | printf_color(ANSI_YELLOW, gettext("Consider setting " | |
8590 | "'compatibility' to an appropriate value, or\n\t" | |
8591 | "adding needed features to the relevant file in\n\t" | |
8592 | ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n")); | |
8593 | break; | |
8594 | ||
9ae529ec | 8595 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
9fb2771a TH |
8596 | printf_color(ANSI_BOLD, gettext("status: ")); |
8597 | printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed " | |
8598 | "on this system because it uses the\n\tfollowing feature(s)" | |
8599 | " not supported on this system:\n")); | |
9ae529ec CS |
8600 | zpool_print_unsup_feat(config); |
8601 | (void) printf("\n"); | |
9fb2771a TH |
8602 | printf_color(ANSI_BOLD, gettext("action: ")); |
8603 | printf_color(ANSI_YELLOW, gettext("Access the pool from a " | |
8604 | "system that supports the required feature(s),\n\tor " | |
8605 | "restore the pool from backup.\n")); | |
9ae529ec CS |
8606 | break; |
8607 | ||
8608 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
9fb2771a TH |
8609 | printf_color(ANSI_BOLD, gettext("status: ")); |
8610 | printf_color(ANSI_YELLOW, gettext("The pool can only be " | |
8611 | "accessed in read-only mode on this system. It\n\tcannot be" | |
8612 | " accessed in read-write mode because it uses the " | |
9ae529ec CS |
8613 | "following\n\tfeature(s) not supported on this system:\n")); |
8614 | zpool_print_unsup_feat(config); | |
8615 | (void) printf("\n"); | |
9fb2771a TH |
8616 | printf_color(ANSI_BOLD, gettext("action: ")); |
8617 | printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed " | |
8618 | "in read-write mode. Import the pool with\n" | |
9ae529ec CS |
8619 | "\t\"-o readonly=on\", access the pool from a system that " |
8620 | "supports the\n\trequired feature(s), or restore the " | |
8621 | "pool from backup.\n")); | |
8622 | break; | |
8623 | ||
34dc7c2f | 8624 | case ZPOOL_STATUS_FAULTED_DEV_R: |
9fb2771a TH |
8625 | printf_color(ANSI_BOLD, gettext("status: ")); |
8626 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
34dc7c2f BB |
8627 | "faulted in response to persistent errors.\n\tSufficient " |
8628 | "replicas exist for the pool to continue functioning " | |
8629 | "in a\n\tdegraded state.\n")); | |
9fb2771a TH |
8630 | printf_color(ANSI_BOLD, gettext("action: ")); |
8631 | printf_color(ANSI_YELLOW, gettext("Replace the faulted device, " | |
34dc7c2f BB |
8632 | "or use 'zpool clear' to mark the device\n\trepaired.\n")); |
8633 | break; | |
8634 | ||
8635 | case ZPOOL_STATUS_FAULTED_DEV_NR: | |
9fb2771a TH |
8636 | printf_color(ANSI_BOLD, gettext("status: ")); |
8637 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
34dc7c2f BB |
8638 | "faulted in response to persistent errors. There are " |
8639 | "insufficient replicas for the pool to\n\tcontinue " | |
8640 | "functioning.\n")); | |
9fb2771a TH |
8641 | printf_color(ANSI_BOLD, gettext("action: ")); |
8642 | printf_color(ANSI_YELLOW, gettext("Destroy and re-create the " | |
8643 | "pool from a backup source. Manually marking the device\n" | |
34dc7c2f BB |
8644 | "\trepaired using 'zpool clear' may allow some data " |
8645 | "to be recovered.\n")); | |
8646 | break; | |
8647 | ||
cec3a0a1 | 8648 | case ZPOOL_STATUS_IO_FAILURE_MMP: |
9fb2771a TH |
8649 | printf_color(ANSI_BOLD, gettext("status: ")); |
8650 | printf_color(ANSI_YELLOW, gettext("The pool is suspended " | |
8651 | "because multihost writes failed or were delayed;\n\t" | |
8652 | "another system could import the pool undetected.\n")); | |
8653 | printf_color(ANSI_BOLD, gettext("action: ")); | |
8654 | printf_color(ANSI_YELLOW, gettext("Make sure the pool's devices" | |
8655 | " are connected, then reboot your system and\n\timport the " | |
cec3a0a1 OF |
8656 | "pool.\n")); |
8657 | break; | |
8658 | ||
b128c09f BB |
8659 | case ZPOOL_STATUS_IO_FAILURE_WAIT: |
8660 | case ZPOOL_STATUS_IO_FAILURE_CONTINUE: | |
9fb2771a TH |
8661 | printf_color(ANSI_BOLD, gettext("status: ")); |
8662 | printf_color(ANSI_YELLOW, gettext("One or more devices are " | |
b128c09f | 8663 | "faulted in response to IO failures.\n")); |
9fb2771a TH |
8664 | printf_color(ANSI_BOLD, gettext("action: ")); |
8665 | printf_color(ANSI_YELLOW, gettext("Make sure the affected " | |
8666 | "devices are connected, then run 'zpool clear'.\n")); | |
b128c09f BB |
8667 | break; |
8668 | ||
8669 | case ZPOOL_STATUS_BAD_LOG: | |
9fb2771a TH |
8670 | printf_color(ANSI_BOLD, gettext("status: ")); |
8671 | printf_color(ANSI_YELLOW, gettext("An intent log record " | |
b128c09f | 8672 | "could not be read.\n" |
2627e752 | 8673 | "\tWaiting for administrator intervention to fix the " |
b128c09f | 8674 | "faulted pool.\n")); |
9fb2771a TH |
8675 | printf_color(ANSI_BOLD, gettext("action: ")); |
8676 | printf_color(ANSI_YELLOW, gettext("Either restore the affected " | |
b128c09f BB |
8677 | "device(s) and run 'zpool online',\n" |
8678 | "\tor ignore the intent log records by running " | |
8679 | "'zpool clear'.\n")); | |
8680 | break; | |
8681 | ||
6fe3498c RM |
8682 | case ZPOOL_STATUS_NON_NATIVE_ASHIFT: |
8683 | (void) printf(gettext("status: One or more devices are " | |
8684 | "configured to use a non-native block size.\n" | |
8685 | "\tExpect reduced performance.\n")); | |
8686 | (void) printf(gettext("action: Replace affected devices with " | |
8687 | "devices that support the\n\tconfigured block size, or " | |
8688 | "migrate data to a properly configured\n\tpool.\n")); | |
8689 | break; | |
8690 | ||
1cbae971 | 8691 | case ZPOOL_STATUS_HOSTID_MISMATCH: |
9fb2771a TH |
8692 | printf_color(ANSI_BOLD, gettext("status: ")); |
8693 | printf_color(ANSI_YELLOW, gettext("Mismatch between pool hostid" | |
8694 | " and system hostid on imported pool.\n\tThis pool was " | |
1cbae971 RY |
8695 | "previously imported into a system with a different " |
8696 | "hostid,\n\tand then was verbatim imported into this " | |
8697 | "system.\n")); | |
9fb2771a TH |
8698 | printf_color(ANSI_BOLD, gettext("action: ")); |
8699 | printf_color(ANSI_YELLOW, gettext("Export this pool on all " | |
8700 | "systems on which it is imported.\n" | |
1cbae971 RY |
8701 | "\tThen import it to correct the mismatch.\n")); |
8702 | break; | |
8703 | ||
ffe9d382 | 8704 | case ZPOOL_STATUS_ERRATA: |
9fb2771a TH |
8705 | printf_color(ANSI_BOLD, gettext("status: ")); |
8706 | printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"), | |
ffe9d382 BB |
8707 | errata); |
8708 | ||
8709 | switch (errata) { | |
8710 | case ZPOOL_ERRATA_NONE: | |
8711 | break; | |
8712 | ||
4f2dcb3e | 8713 | case ZPOOL_ERRATA_ZOL_2094_SCRUB: |
9fb2771a TH |
8714 | printf_color(ANSI_BOLD, gettext("action: ")); |
8715 | printf_color(ANSI_YELLOW, gettext("To correct the issue" | |
8716 | " run 'zpool scrub'.\n")); | |
4f2dcb3e RY |
8717 | break; |
8718 | ||
ae76f45c TC |
8719 | case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: |
8720 | (void) printf(gettext("\tExisting encrypted datasets " | |
8721 | "contain an on-disk incompatibility\n\twhich " | |
8722 | "needs to be corrected.\n")); | |
9fb2771a TH |
8723 | printf_color(ANSI_BOLD, gettext("action: ")); |
8724 | printf_color(ANSI_YELLOW, gettext("To correct the issue" | |
8725 | " backup existing encrypted datasets to new\n\t" | |
ae76f45c TC |
8726 | "encrypted datasets and destroy the old ones. " |
8727 | "'zfs mount -o ro' can\n\tbe used to temporarily " | |
8728 | "mount existing encrypted datasets readonly.\n")); | |
8729 | break; | |
8730 | ||
f00ab3f2 | 8731 | case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION: |
e2dddb7e JS |
8732 | (void) printf(gettext("\tExisting encrypted snapshots " |
8733 | "and bookmarks contain an on-disk\n\tincompat" | |
8734 | "ibility. This may cause on-disk corruption if " | |
8735 | "they are used\n\twith 'zfs recv'.\n")); | |
9fb2771a TH |
8736 | printf_color(ANSI_BOLD, gettext("action: ")); |
8737 | printf_color(ANSI_YELLOW, gettext("To correct the" | |
8738 | "issue, enable the bookmark_v2 feature. No " | |
8739 | "additional\n\taction is needed if there are no " | |
8740 | "encrypted snapshots or bookmarks.\n\tIf preserving" | |
8741 | "the encrypted snapshots and bookmarks is required," | |
8742 | " use\n\ta non-raw send to backup and restore them." | |
8743 | " Alternately, they may be\n\tremoved to resolve " | |
e2dddb7e | 8744 | "the incompatibility.\n")); |
f00ab3f2 TC |
8745 | break; |
8746 | ||
ffe9d382 BB |
8747 | default: |
8748 | /* | |
8749 | * All errata which allow the pool to be imported | |
8750 | * must contain an action message. | |
8751 | */ | |
8752 | assert(0); | |
8753 | } | |
8754 | break; | |
8755 | ||
34dc7c2f BB |
8756 | default: |
8757 | /* | |
8758 | * The remaining errors can't actually be generated, yet. | |
8759 | */ | |
8760 | assert(reason == ZPOOL_STATUS_OK); | |
8761 | } | |
8762 | ||
9fb2771a TH |
8763 | if (msgid != NULL) { |
8764 | printf(" "); | |
8765 | printf_color(ANSI_BOLD, gettext("see:")); | |
a2f944a1 RM |
8766 | printf(gettext( |
8767 | " https://openzfs.github.io/openzfs-docs/msg/%s\n"), | |
8768 | msgid); | |
9fb2771a | 8769 | } |
34dc7c2f BB |
8770 | |
8771 | if (config != NULL) { | |
34dc7c2f BB |
8772 | uint64_t nerr; |
8773 | nvlist_t **spares, **l2cache; | |
8774 | uint_t nspares, nl2cache; | |
d2734cce | 8775 | pool_checkpoint_stat_t *pcs = NULL; |
a1d477c2 | 8776 | pool_removal_stat_t *prs = NULL; |
34dc7c2f | 8777 | |
9a49d3f3 BB |
8778 | print_scan_status(zhp, nvroot); |
8779 | ||
a1d477c2 MA |
8780 | (void) nvlist_lookup_uint64_array(nvroot, |
8781 | ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); | |
8782 | print_removal_status(zhp, prs); | |
9a49d3f3 BB |
8783 | |
8784 | (void) nvlist_lookup_uint64_array(nvroot, | |
8785 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
d2734cce | 8786 | print_checkpoint_status(pcs); |
a1d477c2 | 8787 | |
fea33e4e | 8788 | cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0, |
5f20c145 | 8789 | cbp->cb_name_flags | VDEV_NAME_TYPE_ID); |
fea33e4e HJ |
8790 | if (cbp->cb_namewidth < 10) |
8791 | cbp->cb_namewidth = 10; | |
34dc7c2f | 8792 | |
9fb2771a | 8793 | color_start(ANSI_BOLD); |
34dc7c2f | 8794 | (void) printf(gettext("config:\n\n")); |
d6418de0 | 8795 | (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"), |
fea33e4e HJ |
8796 | cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE", |
8797 | "CKSUM"); | |
9fb2771a | 8798 | color_end(); |
d6418de0 | 8799 | |
9fb2771a TH |
8800 | if (cbp->cb_print_slow_ios) { |
8801 | printf_color(ANSI_BOLD, " %5s", gettext("SLOW")); | |
8802 | } | |
ad796b8a | 8803 | |
d6418de0 TH |
8804 | if (cbp->vcdl != NULL) |
8805 | print_cmd_columns(cbp->vcdl, 0); | |
8806 | ||
8807 | printf("\n"); | |
cc99f275 | 8808 | |
fea33e4e | 8809 | print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0, |
9a49d3f3 | 8810 | B_FALSE, NULL); |
34dc7c2f | 8811 | |
cc99f275 DB |
8812 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP); |
8813 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL); | |
8814 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS); | |
8815 | ||
34dc7c2f BB |
8816 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
8817 | &l2cache, &nl2cache) == 0) | |
fea33e4e | 8818 | print_l2cache(zhp, cbp, l2cache, nl2cache); |
34dc7c2f BB |
8819 | |
8820 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
8821 | &spares, &nspares) == 0) | |
fea33e4e | 8822 | print_spares(zhp, cbp, spares, nspares); |
34dc7c2f BB |
8823 | |
8824 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, | |
8825 | &nerr) == 0) { | |
34dc7c2f | 8826 | (void) printf("\n"); |
018f2604 MA |
8827 | if (nerr == 0) { |
8828 | (void) printf(gettext( | |
8829 | "errors: No known data errors\n")); | |
8830 | } else if (!cbp->cb_verbose) { | |
34dc7c2f BB |
8831 | (void) printf(gettext("errors: %llu data " |
8832 | "errors, use '-v' for a list\n"), | |
8833 | (u_longlong_t)nerr); | |
018f2604 | 8834 | } else { |
34dc7c2f | 8835 | print_error_log(zhp); |
018f2604 | 8836 | } |
34dc7c2f | 8837 | } |
428870ff BB |
8838 | |
8839 | if (cbp->cb_dedup_stats) | |
8840 | print_dedup_stats(config); | |
34dc7c2f BB |
8841 | } else { |
8842 | (void) printf(gettext("config: The configuration cannot be " | |
8843 | "determined.\n")); | |
8844 | } | |
8845 | ||
8846 | return (0); | |
8847 | } | |
8848 | ||
8849 | /* | |
1b939560 | 8850 | * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ... |
d6418de0 | 8851 | * [interval [count]] |
34dc7c2f | 8852 | * |
8720e9e7 | 8853 | * -c CMD For each vdev, run command CMD |
a769fb53 | 8854 | * -i Display vdev initialization status. |
d2f3e292 RY |
8855 | * -g Display guid for individual vdev name. |
8856 | * -L Follow links when resolving vdev path name. | |
ad796b8a | 8857 | * -p Display values in parsable (exact) format. |
a77f29f9 | 8858 | * -P Display full path for vdev name. |
ad796b8a | 8859 | * -s Display slow IOs column. |
34dc7c2f BB |
8860 | * -v Display complete error logs |
8861 | * -x Display only pools with potential problems | |
428870ff | 8862 | * -D Display dedup status (undocumented) |
1b939560 | 8863 | * -t Display vdev TRIM status. |
428870ff | 8864 | * -T Display a timestamp in date(1) or Unix format |
34dc7c2f BB |
8865 | * |
8866 | * Describes the health status of all pools or some subset. | |
8867 | */ | |
8868 | int | |
8869 | zpool_do_status(int argc, char **argv) | |
8870 | { | |
8871 | int c; | |
8872 | int ret; | |
193a37cb TH |
8873 | float interval = 0; |
8874 | unsigned long count = 0; | |
34dc7c2f | 8875 | status_cbdata_t cb = { 0 }; |
8720e9e7 | 8876 | char *cmd = NULL; |
34dc7c2f BB |
8877 | |
8878 | /* check options */ | |
1b939560 | 8879 | while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) { |
34dc7c2f | 8880 | switch (c) { |
8720e9e7 | 8881 | case 'c': |
d6418de0 TH |
8882 | if (cmd != NULL) { |
8883 | fprintf(stderr, | |
8884 | gettext("Can't set -c flag twice\n")); | |
8885 | exit(1); | |
8886 | } | |
099700d9 GDN |
8887 | |
8888 | if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && | |
8889 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { | |
8890 | fprintf(stderr, gettext( | |
8891 | "Can't run -c, disabled by " | |
8892 | "ZPOOL_SCRIPTS_ENABLED.\n")); | |
8893 | exit(1); | |
8894 | } | |
8895 | ||
d6418de0 TH |
8896 | if ((getuid() <= 0 || geteuid() <= 0) && |
8897 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { | |
8898 | fprintf(stderr, gettext( | |
8899 | "Can't run -c with root privileges " | |
8900 | "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); | |
8901 | exit(1); | |
8902 | } | |
8720e9e7 TH |
8903 | cmd = optarg; |
8904 | break; | |
a769fb53 BB |
8905 | case 'i': |
8906 | cb.cb_print_vdev_init = B_TRUE; | |
8907 | break; | |
d2f3e292 RY |
8908 | case 'g': |
8909 | cb.cb_name_flags |= VDEV_NAME_GUID; | |
8910 | break; | |
8911 | case 'L': | |
8912 | cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
8913 | break; | |
ad796b8a TH |
8914 | case 'p': |
8915 | cb.cb_literal = B_TRUE; | |
8916 | break; | |
a77f29f9 | 8917 | case 'P': |
d2f3e292 RY |
8918 | cb.cb_name_flags |= VDEV_NAME_PATH; |
8919 | break; | |
ad796b8a TH |
8920 | case 's': |
8921 | cb.cb_print_slow_ios = B_TRUE; | |
8922 | break; | |
34dc7c2f BB |
8923 | case 'v': |
8924 | cb.cb_verbose = B_TRUE; | |
8925 | break; | |
8926 | case 'x': | |
8927 | cb.cb_explain = B_TRUE; | |
8928 | break; | |
428870ff BB |
8929 | case 'D': |
8930 | cb.cb_dedup_stats = B_TRUE; | |
8931 | break; | |
1b939560 BB |
8932 | case 't': |
8933 | cb.cb_print_vdev_trim = B_TRUE; | |
8934 | break; | |
428870ff BB |
8935 | case 'T': |
8936 | get_timestamp_arg(*optarg); | |
8937 | break; | |
34dc7c2f | 8938 | case '?': |
1528bfdb | 8939 | if (optopt == 'c') { |
099700d9 | 8940 | print_zpool_script_list("status"); |
d6418de0 | 8941 | exit(0); |
1528bfdb TH |
8942 | } else { |
8943 | fprintf(stderr, | |
8944 | gettext("invalid option '%c'\n"), optopt); | |
8945 | } | |
34dc7c2f BB |
8946 | usage(B_FALSE); |
8947 | } | |
8948 | } | |
8949 | ||
8950 | argc -= optind; | |
8951 | argv += optind; | |
8952 | ||
428870ff | 8953 | get_interval_count(&argc, argv, &interval, &count); |
34dc7c2f BB |
8954 | |
8955 | if (argc == 0) | |
8956 | cb.cb_allpools = B_TRUE; | |
8957 | ||
428870ff | 8958 | cb.cb_first = B_TRUE; |
fea33e4e | 8959 | cb.cb_print_status = B_TRUE; |
34dc7c2f | 8960 | |
428870ff BB |
8961 | for (;;) { |
8962 | if (timestamp_fmt != NODATE) | |
8963 | print_timestamp(timestamp_fmt); | |
34dc7c2f | 8964 | |
8720e9e7 | 8965 | if (cmd != NULL) |
1528bfdb TH |
8966 | cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd, |
8967 | NULL, NULL, 0, 0); | |
8720e9e7 | 8968 | |
2a673e76 AJ |
8969 | ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
8970 | cb.cb_literal, status_callback, &cb); | |
428870ff | 8971 | |
8720e9e7 TH |
8972 | if (cb.vcdl != NULL) |
8973 | free_vdev_cmd_data_list(cb.vcdl); | |
8974 | ||
428870ff | 8975 | if (argc == 0 && cb.cb_count == 0) |
42cb3819 | 8976 | (void) fprintf(stderr, gettext("no pools available\n")); |
428870ff BB |
8977 | else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) |
8978 | (void) printf(gettext("all pools are healthy\n")); | |
8979 | ||
8980 | if (ret != 0) | |
8981 | return (ret); | |
8982 | ||
8983 | if (interval == 0) | |
8984 | break; | |
8985 | ||
8986 | if (count != 0 && --count == 0) | |
8987 | break; | |
8988 | ||
193a37cb | 8989 | (void) fsleep(interval); |
428870ff BB |
8990 | } |
8991 | ||
8992 | return (0); | |
34dc7c2f BB |
8993 | } |
8994 | ||
8995 | typedef struct upgrade_cbdata { | |
34dc7c2f | 8996 | int cb_first; |
34dc7c2f BB |
8997 | int cb_argc; |
8998 | uint64_t cb_version; | |
8999 | char **cb_argv; | |
9000 | } upgrade_cbdata_t; | |
9001 | ||
287be44f DS |
9002 | static int |
9003 | check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs) | |
9004 | { | |
02730c33 | 9005 | int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION); |
287be44f DS |
9006 | int *count = (int *)unsupp_fs; |
9007 | ||
9008 | if (zfs_version > ZPL_VERSION) { | |
9009 | (void) printf(gettext("%s (v%d) is not supported by this " | |
9010 | "implementation of ZFS.\n"), | |
9011 | zfs_get_name(zhp), zfs_version); | |
9012 | (*count)++; | |
9013 | } | |
9014 | ||
baca06c2 | 9015 | zfs_iter_filesystems_v2(zhp, 0, check_unsupp_fs, unsupp_fs); |
287be44f DS |
9016 | |
9017 | zfs_close(zhp); | |
9018 | ||
9019 | return (0); | |
9020 | } | |
9021 | ||
b9b24bb4 CS |
9022 | static int |
9023 | upgrade_version(zpool_handle_t *zhp, uint64_t version) | |
9024 | { | |
9025 | int ret; | |
9026 | nvlist_t *config; | |
9027 | uint64_t oldversion; | |
287be44f | 9028 | int unsupp_fs = 0; |
b9b24bb4 CS |
9029 | |
9030 | config = zpool_get_config(zhp, NULL); | |
9031 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
9032 | &oldversion) == 0); | |
9033 | ||
e086db16 CB |
9034 | char compat[ZFS_MAXPROPLEN]; |
9035 | if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, | |
9036 | ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) | |
9037 | compat[0] = '\0'; | |
9038 | ||
b9b24bb4 CS |
9039 | assert(SPA_VERSION_IS_SUPPORTED(oldversion)); |
9040 | assert(oldversion < version); | |
9041 | ||
287be44f DS |
9042 | ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs); |
9043 | if (ret != 0) | |
9044 | return (ret); | |
9045 | ||
9046 | if (unsupp_fs) { | |
5c7afad4 BB |
9047 | (void) fprintf(stderr, gettext("Upgrade not performed due " |
9048 | "to %d unsupported filesystems (max v%d).\n"), | |
02730c33 | 9049 | unsupp_fs, (int)ZPL_VERSION); |
287be44f DS |
9050 | return (1); |
9051 | } | |
9052 | ||
e086db16 CB |
9053 | if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) { |
9054 | (void) fprintf(stderr, gettext("Upgrade not performed because " | |
9055 | "'compatibility' property set to '" | |
9056 | ZPOOL_COMPAT_LEGACY "'.\n")); | |
9057 | return (1); | |
9058 | } | |
9059 | ||
b9b24bb4 CS |
9060 | ret = zpool_upgrade(zhp, version); |
9061 | if (ret != 0) | |
9062 | return (ret); | |
9063 | ||
9064 | if (version >= SPA_VERSION_FEATURES) { | |
9065 | (void) printf(gettext("Successfully upgraded " | |
9066 | "'%s' from version %llu to feature flags.\n"), | |
02730c33 | 9067 | zpool_get_name(zhp), (u_longlong_t)oldversion); |
b9b24bb4 CS |
9068 | } else { |
9069 | (void) printf(gettext("Successfully upgraded " | |
9070 | "'%s' from version %llu to version %llu.\n"), | |
02730c33 BB |
9071 | zpool_get_name(zhp), (u_longlong_t)oldversion, |
9072 | (u_longlong_t)version); | |
b9b24bb4 CS |
9073 | } |
9074 | ||
9075 | return (0); | |
9076 | } | |
9077 | ||
9078 | static int | |
9079 | upgrade_enable_all(zpool_handle_t *zhp, int *countp) | |
9080 | { | |
9081 | int i, ret, count; | |
9082 | boolean_t firstff = B_TRUE; | |
9083 | nvlist_t *enabled = zpool_get_features(zhp); | |
9084 | ||
658fb802 CB |
9085 | char compat[ZFS_MAXPROPLEN]; |
9086 | if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat, | |
9087 | ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) | |
9088 | compat[0] = '\0'; | |
9089 | ||
9090 | boolean_t requested_features[SPA_FEATURES]; | |
9091 | if (zpool_do_load_compat(compat, requested_features) != | |
9092 | ZPOOL_COMPATIBILITY_OK) | |
9093 | return (-1); | |
9094 | ||
b9b24bb4 CS |
9095 | count = 0; |
9096 | for (i = 0; i < SPA_FEATURES; i++) { | |
9097 | const char *fname = spa_feature_table[i].fi_uname; | |
9098 | const char *fguid = spa_feature_table[i].fi_guid; | |
35ec5179 BB |
9099 | |
9100 | if (!spa_feature_table[i].fi_zfs_mod_supported) | |
9101 | continue; | |
9102 | ||
658fb802 | 9103 | if (!nvlist_exists(enabled, fguid) && requested_features[i]) { |
b9b24bb4 CS |
9104 | char *propname; |
9105 | verify(-1 != asprintf(&propname, "feature@%s", fname)); | |
9106 | ret = zpool_set_prop(zhp, propname, | |
9107 | ZFS_FEATURE_ENABLED); | |
9108 | if (ret != 0) { | |
9109 | free(propname); | |
9110 | return (ret); | |
9111 | } | |
9112 | count++; | |
9113 | ||
9114 | if (firstff) { | |
9115 | (void) printf(gettext("Enabled the " | |
9116 | "following features on '%s':\n"), | |
9117 | zpool_get_name(zhp)); | |
9118 | firstff = B_FALSE; | |
9119 | } | |
9120 | (void) printf(gettext(" %s\n"), fname); | |
9121 | free(propname); | |
9122 | } | |
9123 | } | |
9124 | ||
9125 | if (countp != NULL) | |
9126 | *countp = count; | |
9127 | return (0); | |
9128 | } | |
9129 | ||
34dc7c2f BB |
9130 | static int |
9131 | upgrade_cb(zpool_handle_t *zhp, void *arg) | |
9132 | { | |
9133 | upgrade_cbdata_t *cbp = arg; | |
9134 | nvlist_t *config; | |
9135 | uint64_t version; | |
65d9212a | 9136 | boolean_t modified_pool = B_FALSE; |
b9b24bb4 | 9137 | int ret; |
34dc7c2f BB |
9138 | |
9139 | config = zpool_get_config(zhp, NULL); | |
9140 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
9141 | &version) == 0); | |
9142 | ||
b9b24bb4 | 9143 | assert(SPA_VERSION_IS_SUPPORTED(version)); |
34dc7c2f | 9144 | |
b9b24bb4 CS |
9145 | if (version < cbp->cb_version) { |
9146 | cbp->cb_first = B_FALSE; | |
9147 | ret = upgrade_version(zhp, cbp->cb_version); | |
9148 | if (ret != 0) | |
9149 | return (ret); | |
65d9212a | 9150 | modified_pool = B_TRUE; |
b9b24bb4 | 9151 | |
b9b24bb4 CS |
9152 | /* |
9153 | * If they did "zpool upgrade -a", then we could | |
9154 | * be doing ioctls to different pools. We need | |
9155 | * to log this history once to each pool, and bypass | |
9156 | * the normal history logging that happens in main(). | |
9157 | */ | |
9158 | (void) zpool_log_history(g_zfs, history_str); | |
9159 | log_history = B_FALSE; | |
b9b24bb4 CS |
9160 | } |
9161 | ||
9162 | if (cbp->cb_version >= SPA_VERSION_FEATURES) { | |
9163 | int count; | |
9164 | ret = upgrade_enable_all(zhp, &count); | |
9165 | if (ret != 0) | |
9166 | return (ret); | |
9167 | ||
9168 | if (count > 0) { | |
34dc7c2f | 9169 | cbp->cb_first = B_FALSE; |
65d9212a | 9170 | modified_pool = B_TRUE; |
34dc7c2f | 9171 | } |
b9b24bb4 | 9172 | } |
34dc7c2f | 9173 | |
65d9212a | 9174 | if (modified_pool) { |
9175 | (void) printf("\n"); | |
9176 | (void) after_zpool_upgrade(zhp); | |
b9b24bb4 CS |
9177 | } |
9178 | ||
9179 | return (0); | |
9180 | } | |
9181 | ||
9182 | static int | |
9183 | upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) | |
9184 | { | |
9185 | upgrade_cbdata_t *cbp = arg; | |
9186 | nvlist_t *config; | |
9187 | uint64_t version; | |
9188 | ||
9189 | config = zpool_get_config(zhp, NULL); | |
9190 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
9191 | &version) == 0); | |
9192 | ||
9193 | assert(SPA_VERSION_IS_SUPPORTED(version)); | |
9194 | ||
9195 | if (version < SPA_VERSION_FEATURES) { | |
34dc7c2f BB |
9196 | if (cbp->cb_first) { |
9197 | (void) printf(gettext("The following pools are " | |
b9b24bb4 CS |
9198 | "formatted with legacy version numbers and can\n" |
9199 | "be upgraded to use feature flags. After " | |
9200 | "being upgraded, these pools\nwill no " | |
9201 | "longer be accessible by software that does not " | |
e086db16 CB |
9202 | "support feature\nflags.\n\n" |
9203 | "Note that setting a pool's 'compatibility' " | |
9204 | "feature to '" ZPOOL_COMPAT_LEGACY "' will\n" | |
9205 | "inhibit upgrades.\n\n")); | |
34dc7c2f BB |
9206 | (void) printf(gettext("VER POOL\n")); |
9207 | (void) printf(gettext("--- ------------\n")); | |
9208 | cbp->cb_first = B_FALSE; | |
9209 | } | |
9210 | ||
9211 | (void) printf("%2llu %s\n", (u_longlong_t)version, | |
9212 | zpool_get_name(zhp)); | |
9213 | } | |
9214 | ||
b9b24bb4 CS |
9215 | return (0); |
9216 | } | |
9217 | ||
9218 | static int | |
9219 | upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) | |
9220 | { | |
9221 | upgrade_cbdata_t *cbp = arg; | |
9222 | nvlist_t *config; | |
9223 | uint64_t version; | |
9224 | ||
9225 | config = zpool_get_config(zhp, NULL); | |
9226 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
9227 | &version) == 0); | |
9228 | ||
9229 | if (version >= SPA_VERSION_FEATURES) { | |
9230 | int i; | |
9231 | boolean_t poolfirst = B_TRUE; | |
9232 | nvlist_t *enabled = zpool_get_features(zhp); | |
9233 | ||
9234 | for (i = 0; i < SPA_FEATURES; i++) { | |
9235 | const char *fguid = spa_feature_table[i].fi_guid; | |
9236 | const char *fname = spa_feature_table[i].fi_uname; | |
35ec5179 BB |
9237 | |
9238 | if (!spa_feature_table[i].fi_zfs_mod_supported) | |
9239 | continue; | |
9240 | ||
b9b24bb4 CS |
9241 | if (!nvlist_exists(enabled, fguid)) { |
9242 | if (cbp->cb_first) { | |
9243 | (void) printf(gettext("\nSome " | |
9244 | "supported features are not " | |
9245 | "enabled on the following pools. " | |
9246 | "Once a\nfeature is enabled the " | |
9247 | "pool may become incompatible with " | |
9248 | "software\nthat does not support " | |
9249 | "the feature. See " | |
2badb345 | 9250 | "zpool-features(7) for " |
e086db16 CB |
9251 | "details.\n\n" |
9252 | "Note that the pool " | |
9253 | "'compatibility' feature can be " | |
9254 | "used to inhibit\nfeature " | |
9255 | "upgrades.\n\n")); | |
b9b24bb4 CS |
9256 | (void) printf(gettext("POOL " |
9257 | "FEATURE\n")); | |
9258 | (void) printf(gettext("------" | |
9259 | "---------\n")); | |
9260 | cbp->cb_first = B_FALSE; | |
9261 | } | |
9262 | ||
9263 | if (poolfirst) { | |
9264 | (void) printf(gettext("%s\n"), | |
9265 | zpool_get_name(zhp)); | |
9266 | poolfirst = B_FALSE; | |
9267 | } | |
9268 | ||
9269 | (void) printf(gettext(" %s\n"), fname); | |
9270 | } | |
6f1ffb06 MA |
9271 | /* |
9272 | * If they did "zpool upgrade -a", then we could | |
9273 | * be doing ioctls to different pools. We need | |
9274 | * to log this history once to each pool, and bypass | |
9275 | * the normal history logging that happens in main(). | |
9276 | */ | |
9277 | (void) zpool_log_history(g_zfs, history_str); | |
9278 | log_history = B_FALSE; | |
b9b24bb4 CS |
9279 | } |
9280 | } | |
9281 | ||
9282 | return (0); | |
34dc7c2f BB |
9283 | } |
9284 | ||
34dc7c2f BB |
9285 | static int |
9286 | upgrade_one(zpool_handle_t *zhp, void *data) | |
9287 | { | |
65d9212a | 9288 | boolean_t modified_pool = B_FALSE; |
34dc7c2f BB |
9289 | upgrade_cbdata_t *cbp = data; |
9290 | uint64_t cur_version; | |
9291 | int ret; | |
9292 | ||
9293 | if (strcmp("log", zpool_get_name(zhp)) == 0) { | |
c66989ba | 9294 | (void) fprintf(stderr, gettext("'log' is now a reserved word\n" |
34dc7c2f BB |
9295 | "Pool 'log' must be renamed using export and import" |
9296 | " to upgrade.\n")); | |
9297 | return (1); | |
9298 | } | |
9299 | ||
9300 | cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); | |
9301 | if (cur_version > cbp->cb_version) { | |
9302 | (void) printf(gettext("Pool '%s' is already formatted " | |
b9b24bb4 | 9303 | "using more current version '%llu'.\n\n"), |
02730c33 | 9304 | zpool_get_name(zhp), (u_longlong_t)cur_version); |
34dc7c2f BB |
9305 | return (0); |
9306 | } | |
b9b24bb4 CS |
9307 | |
9308 | if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { | |
34dc7c2f | 9309 | (void) printf(gettext("Pool '%s' is already formatted " |
b9b24bb4 | 9310 | "using version %llu.\n\n"), zpool_get_name(zhp), |
02730c33 | 9311 | (u_longlong_t)cbp->cb_version); |
34dc7c2f BB |
9312 | return (0); |
9313 | } | |
9314 | ||
b9b24bb4 | 9315 | if (cur_version != cbp->cb_version) { |
65d9212a | 9316 | modified_pool = B_TRUE; |
b9b24bb4 CS |
9317 | ret = upgrade_version(zhp, cbp->cb_version); |
9318 | if (ret != 0) | |
9319 | return (ret); | |
9320 | } | |
34dc7c2f | 9321 | |
b9b24bb4 CS |
9322 | if (cbp->cb_version >= SPA_VERSION_FEATURES) { |
9323 | int count = 0; | |
9324 | ret = upgrade_enable_all(zhp, &count); | |
9325 | if (ret != 0) | |
9326 | return (ret); | |
9327 | ||
9328 | if (count != 0) { | |
65d9212a | 9329 | modified_pool = B_TRUE; |
b9b24bb4 CS |
9330 | } else if (cur_version == SPA_VERSION) { |
9331 | (void) printf(gettext("Pool '%s' already has all " | |
658fb802 | 9332 | "supported and requested features enabled.\n"), |
b9b24bb4 CS |
9333 | zpool_get_name(zhp)); |
9334 | } | |
9335 | } | |
9336 | ||
65d9212a | 9337 | if (modified_pool) { |
9338 | (void) printf("\n"); | |
9339 | (void) after_zpool_upgrade(zhp); | |
34dc7c2f BB |
9340 | } |
9341 | ||
b9b24bb4 | 9342 | return (0); |
34dc7c2f BB |
9343 | } |
9344 | ||
9345 | /* | |
9346 | * zpool upgrade | |
9347 | * zpool upgrade -v | |
9348 | * zpool upgrade [-V version] <-a | pool ...> | |
9349 | * | |
9350 | * With no arguments, display downrev'd ZFS pool available for upgrade. | |
9351 | * Individual pools can be upgraded by specifying the pool, and '-a' will | |
9352 | * upgrade all pools. | |
9353 | */ | |
9354 | int | |
9355 | zpool_do_upgrade(int argc, char **argv) | |
9356 | { | |
9357 | int c; | |
9358 | upgrade_cbdata_t cb = { 0 }; | |
9359 | int ret = 0; | |
9360 | boolean_t showversions = B_FALSE; | |
b9b24bb4 | 9361 | boolean_t upgradeall = B_FALSE; |
34dc7c2f BB |
9362 | char *end; |
9363 | ||
9364 | ||
9365 | /* check options */ | |
9babb374 | 9366 | while ((c = getopt(argc, argv, ":avV:")) != -1) { |
34dc7c2f BB |
9367 | switch (c) { |
9368 | case 'a': | |
b9b24bb4 | 9369 | upgradeall = B_TRUE; |
34dc7c2f BB |
9370 | break; |
9371 | case 'v': | |
9372 | showversions = B_TRUE; | |
9373 | break; | |
9374 | case 'V': | |
9375 | cb.cb_version = strtoll(optarg, &end, 10); | |
9ae529ec CS |
9376 | if (*end != '\0' || |
9377 | !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { | |
34dc7c2f BB |
9378 | (void) fprintf(stderr, |
9379 | gettext("invalid version '%s'\n"), optarg); | |
9380 | usage(B_FALSE); | |
9381 | } | |
9382 | break; | |
9babb374 BB |
9383 | case ':': |
9384 | (void) fprintf(stderr, gettext("missing argument for " | |
9385 | "'%c' option\n"), optopt); | |
9386 | usage(B_FALSE); | |
9387 | break; | |
34dc7c2f BB |
9388 | case '?': |
9389 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
9390 | optopt); | |
9391 | usage(B_FALSE); | |
9392 | } | |
9393 | } | |
9394 | ||
9395 | cb.cb_argc = argc; | |
9396 | cb.cb_argv = argv; | |
9397 | argc -= optind; | |
9398 | argv += optind; | |
9399 | ||
9400 | if (cb.cb_version == 0) { | |
9401 | cb.cb_version = SPA_VERSION; | |
b9b24bb4 | 9402 | } else if (!upgradeall && argc == 0) { |
34dc7c2f BB |
9403 | (void) fprintf(stderr, gettext("-V option is " |
9404 | "incompatible with other arguments\n")); | |
9405 | usage(B_FALSE); | |
9406 | } | |
9407 | ||
9408 | if (showversions) { | |
b9b24bb4 | 9409 | if (upgradeall || argc != 0) { |
34dc7c2f BB |
9410 | (void) fprintf(stderr, gettext("-v option is " |
9411 | "incompatible with other arguments\n")); | |
9412 | usage(B_FALSE); | |
9413 | } | |
b9b24bb4 | 9414 | } else if (upgradeall) { |
34dc7c2f BB |
9415 | if (argc != 0) { |
9416 | (void) fprintf(stderr, gettext("-a option should not " | |
9417 | "be used along with a pool name\n")); | |
9418 | usage(B_FALSE); | |
9419 | } | |
9420 | } | |
9421 | ||
63652e15 | 9422 | (void) printf("%s", gettext("This system supports ZFS pool feature " |
9ae529ec | 9423 | "flags.\n\n")); |
34dc7c2f | 9424 | if (showversions) { |
b9b24bb4 CS |
9425 | int i; |
9426 | ||
9427 | (void) printf(gettext("The following features are " | |
9428 | "supported:\n\n")); | |
9429 | (void) printf(gettext("FEAT DESCRIPTION\n")); | |
9430 | (void) printf("----------------------------------------------" | |
9431 | "---------------\n"); | |
9432 | for (i = 0; i < SPA_FEATURES; i++) { | |
9433 | zfeature_info_t *fi = &spa_feature_table[i]; | |
03ef8f09 MM |
9434 | if (!fi->fi_zfs_mod_supported) |
9435 | continue; | |
241b5415 MA |
9436 | const char *ro = |
9437 | (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? | |
b9b24bb4 CS |
9438 | " (read-only compatible)" : ""; |
9439 | ||
9440 | (void) printf("%-37s%s\n", fi->fi_uname, ro); | |
9441 | (void) printf(" %s\n", fi->fi_desc); | |
9442 | } | |
9443 | (void) printf("\n"); | |
9444 | ||
9445 | (void) printf(gettext("The following legacy versions are also " | |
34dc7c2f BB |
9446 | "supported:\n\n")); |
9447 | (void) printf(gettext("VER DESCRIPTION\n")); | |
9448 | (void) printf("--- -----------------------------------------" | |
9449 | "---------------\n"); | |
9450 | (void) printf(gettext(" 1 Initial ZFS version\n")); | |
9451 | (void) printf(gettext(" 2 Ditto blocks " | |
9452 | "(replicated metadata)\n")); | |
9453 | (void) printf(gettext(" 3 Hot spares and double parity " | |
9454 | "RAID-Z\n")); | |
9455 | (void) printf(gettext(" 4 zpool history\n")); | |
9456 | (void) printf(gettext(" 5 Compression using the gzip " | |
9457 | "algorithm\n")); | |
9458 | (void) printf(gettext(" 6 bootfs pool property\n")); | |
9459 | (void) printf(gettext(" 7 Separate intent log devices\n")); | |
9460 | (void) printf(gettext(" 8 Delegated administration\n")); | |
9461 | (void) printf(gettext(" 9 refquota and refreservation " | |
9462 | "properties\n")); | |
9463 | (void) printf(gettext(" 10 Cache devices\n")); | |
b128c09f BB |
9464 | (void) printf(gettext(" 11 Improved scrub performance\n")); |
9465 | (void) printf(gettext(" 12 Snapshot properties\n")); | |
9466 | (void) printf(gettext(" 13 snapused property\n")); | |
9babb374 BB |
9467 | (void) printf(gettext(" 14 passthrough-x aclinherit\n")); |
9468 | (void) printf(gettext(" 15 user/group space accounting\n")); | |
9469 | (void) printf(gettext(" 16 stmf property support\n")); | |
45d1cae3 | 9470 | (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); |
428870ff BB |
9471 | (void) printf(gettext(" 18 Snapshot user holds\n")); |
9472 | (void) printf(gettext(" 19 Log device removal\n")); | |
9473 | (void) printf(gettext(" 20 Compression using zle " | |
9474 | "(zero-length encoding)\n")); | |
9475 | (void) printf(gettext(" 21 Deduplication\n")); | |
9476 | (void) printf(gettext(" 22 Received properties\n")); | |
9477 | (void) printf(gettext(" 23 Slim ZIL\n")); | |
9478 | (void) printf(gettext(" 24 System attributes\n")); | |
9479 | (void) printf(gettext(" 25 Improved scrub stats\n")); | |
9480 | (void) printf(gettext(" 26 Improved snapshot deletion " | |
9481 | "performance\n")); | |
572e2857 BB |
9482 | (void) printf(gettext(" 27 Improved snapshot creation " |
9483 | "performance\n")); | |
9484 | (void) printf(gettext(" 28 Multiple vdev replacements\n")); | |
428870ff BB |
9485 | (void) printf(gettext("\nFor more information on a particular " |
9486 | "version, including supported releases,\n")); | |
9487 | (void) printf(gettext("see the ZFS Administration Guide.\n\n")); | |
b9b24bb4 CS |
9488 | } else if (argc == 0 && upgradeall) { |
9489 | cb.cb_first = B_TRUE; | |
34dc7c2f | 9490 | ret = zpool_iter(g_zfs, upgrade_cb, &cb); |
b9b24bb4 CS |
9491 | if (ret == 0 && cb.cb_first) { |
9492 | if (cb.cb_version == SPA_VERSION) { | |
9493 | (void) printf(gettext("All pools are already " | |
9494 | "formatted using feature flags.\n\n")); | |
9495 | (void) printf(gettext("Every feature flags " | |
658fb802 CB |
9496 | "pool already has all supported and " |
9497 | "requested features enabled.\n")); | |
b9b24bb4 CS |
9498 | } else { |
9499 | (void) printf(gettext("All pools are already " | |
9500 | "formatted with version %llu or higher.\n"), | |
02730c33 | 9501 | (u_longlong_t)cb.cb_version); |
34dc7c2f BB |
9502 | } |
9503 | } | |
b9b24bb4 CS |
9504 | } else if (argc == 0) { |
9505 | cb.cb_first = B_TRUE; | |
9506 | ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); | |
9507 | assert(ret == 0); | |
34dc7c2f | 9508 | |
b9b24bb4 CS |
9509 | if (cb.cb_first) { |
9510 | (void) printf(gettext("All pools are formatted " | |
9511 | "using feature flags.\n\n")); | |
9512 | } else { | |
9513 | (void) printf(gettext("\nUse 'zpool upgrade -v' " | |
9514 | "for a list of available legacy versions.\n")); | |
9515 | } | |
9516 | ||
9517 | cb.cb_first = B_TRUE; | |
9518 | ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); | |
9519 | assert(ret == 0); | |
9520 | ||
9521 | if (cb.cb_first) { | |
9522 | (void) printf(gettext("Every feature flags pool has " | |
658fb802 | 9523 | "all supported and requested features enabled.\n")); |
b9b24bb4 CS |
9524 | } else { |
9525 | (void) printf(gettext("\n")); | |
34dc7c2f BB |
9526 | } |
9527 | } else { | |
2a673e76 AJ |
9528 | ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, |
9529 | B_FALSE, upgrade_one, &cb); | |
34dc7c2f BB |
9530 | } |
9531 | ||
9532 | return (ret); | |
9533 | } | |
9534 | ||
9535 | typedef struct hist_cbdata { | |
9536 | boolean_t first; | |
6f1ffb06 MA |
9537 | boolean_t longfmt; |
9538 | boolean_t internal; | |
34dc7c2f BB |
9539 | } hist_cbdata_t; |
9540 | ||
7125a109 CC |
9541 | static void |
9542 | print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb) | |
34dc7c2f | 9543 | { |
34dc7c2f BB |
9544 | nvlist_t **records; |
9545 | uint_t numrecords; | |
7125a109 | 9546 | int i; |
34dc7c2f BB |
9547 | |
9548 | verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, | |
9549 | &records, &numrecords) == 0); | |
9550 | for (i = 0; i < numrecords; i++) { | |
6f1ffb06 | 9551 | nvlist_t *rec = records[i]; |
2ac90457 | 9552 | char tbuf[64] = ""; |
34dc7c2f | 9553 | |
6f1ffb06 MA |
9554 | if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { |
9555 | time_t tsec; | |
9556 | struct tm t; | |
34dc7c2f | 9557 | |
6f1ffb06 MA |
9558 | tsec = fnvlist_lookup_uint64(records[i], |
9559 | ZPOOL_HIST_TIME); | |
9560 | (void) localtime_r(&tsec, &t); | |
9561 | (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); | |
9562 | } | |
9563 | ||
2ac90457 MA |
9564 | if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) { |
9565 | uint64_t elapsed_ns = fnvlist_lookup_int64(records[i], | |
9566 | ZPOOL_HIST_ELAPSED_NS); | |
9567 | (void) snprintf(tbuf + strlen(tbuf), | |
9568 | sizeof (tbuf) - strlen(tbuf), | |
9569 | " (%lldms)", (long long)elapsed_ns / 1000 / 1000); | |
9570 | } | |
9571 | ||
6f1ffb06 MA |
9572 | if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { |
9573 | (void) printf("%s %s", tbuf, | |
9574 | fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); | |
9575 | } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { | |
9576 | int ievent = | |
9577 | fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); | |
9578 | if (!cb->internal) | |
34dc7c2f | 9579 | continue; |
6f1ffb06 MA |
9580 | if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { |
9581 | (void) printf("%s unrecognized record:\n", | |
9582 | tbuf); | |
9583 | dump_nvlist(rec, 4); | |
34dc7c2f | 9584 | continue; |
6f1ffb06 MA |
9585 | } |
9586 | (void) printf("%s [internal %s txg:%lld] %s", tbuf, | |
9587 | zfs_history_event_names[ievent], | |
02730c33 | 9588 | (longlong_t)fnvlist_lookup_uint64( |
d1d7e268 | 9589 | rec, ZPOOL_HIST_TXG), |
6f1ffb06 MA |
9590 | fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); |
9591 | } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { | |
9592 | if (!cb->internal) | |
9593 | continue; | |
9594 | (void) printf("%s [txg:%lld] %s", tbuf, | |
02730c33 | 9595 | (longlong_t)fnvlist_lookup_uint64( |
d1d7e268 | 9596 | rec, ZPOOL_HIST_TXG), |
6f1ffb06 MA |
9597 | fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); |
9598 | if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { | |
9599 | (void) printf(" %s (%llu)", | |
9600 | fnvlist_lookup_string(rec, | |
9601 | ZPOOL_HIST_DSNAME), | |
d1d7e268 | 9602 | (u_longlong_t)fnvlist_lookup_uint64(rec, |
6f1ffb06 MA |
9603 | ZPOOL_HIST_DSID)); |
9604 | } | |
9605 | (void) printf(" %s", fnvlist_lookup_string(rec, | |
9606 | ZPOOL_HIST_INT_STR)); | |
9607 | } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { | |
9608 | if (!cb->internal) | |
9609 | continue; | |
9610 | (void) printf("%s ioctl %s\n", tbuf, | |
9611 | fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); | |
9612 | if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { | |
9613 | (void) printf(" input:\n"); | |
9614 | dump_nvlist(fnvlist_lookup_nvlist(rec, | |
9615 | ZPOOL_HIST_INPUT_NVL), 8); | |
9616 | } | |
9617 | if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { | |
9618 | (void) printf(" output:\n"); | |
9619 | dump_nvlist(fnvlist_lookup_nvlist(rec, | |
9620 | ZPOOL_HIST_OUTPUT_NVL), 8); | |
9621 | } | |
d66aab7c MA |
9622 | if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) { |
9623 | (void) printf(" output nvlist omitted; " | |
9624 | "original size: %lldKB\n", | |
9625 | (longlong_t)fnvlist_lookup_int64(rec, | |
9626 | ZPOOL_HIST_OUTPUT_SIZE) / 1024); | |
9627 | } | |
d99a0153 CW |
9628 | if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) { |
9629 | (void) printf(" errno: %lld\n", | |
9630 | (longlong_t)fnvlist_lookup_int64(rec, | |
9631 | ZPOOL_HIST_ERRNO)); | |
9632 | } | |
6f1ffb06 MA |
9633 | } else { |
9634 | if (!cb->internal) | |
9635 | continue; | |
9636 | (void) printf("%s unrecognized record:\n", tbuf); | |
9637 | dump_nvlist(rec, 4); | |
34dc7c2f | 9638 | } |
34dc7c2f BB |
9639 | |
9640 | if (!cb->longfmt) { | |
9641 | (void) printf("\n"); | |
9642 | continue; | |
9643 | } | |
9644 | (void) printf(" ["); | |
6f1ffb06 MA |
9645 | if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { |
9646 | uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); | |
9647 | struct passwd *pwd = getpwuid(who); | |
9648 | (void) printf("user %d ", (int)who); | |
9649 | if (pwd != NULL) | |
9650 | (void) printf("(%s) ", pwd->pw_name); | |
34dc7c2f | 9651 | } |
6f1ffb06 MA |
9652 | if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { |
9653 | (void) printf("on %s", | |
9654 | fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); | |
34dc7c2f | 9655 | } |
6f1ffb06 MA |
9656 | if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { |
9657 | (void) printf(":%s", | |
9658 | fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); | |
34dc7c2f BB |
9659 | } |
9660 | ||
9661 | (void) printf("]"); | |
9662 | (void) printf("\n"); | |
9663 | } | |
7125a109 CC |
9664 | } |
9665 | ||
9666 | /* | |
9667 | * Print out the command history for a specific pool. | |
9668 | */ | |
9669 | static int | |
9670 | get_history_one(zpool_handle_t *zhp, void *data) | |
9671 | { | |
9672 | nvlist_t *nvhis; | |
9673 | int ret; | |
9674 | hist_cbdata_t *cb = (hist_cbdata_t *)data; | |
9675 | uint64_t off = 0; | |
9676 | boolean_t eof = B_FALSE; | |
9677 | ||
9678 | cb->first = B_FALSE; | |
9679 | ||
9680 | (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); | |
9681 | ||
9682 | while (!eof) { | |
9683 | if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0) | |
9684 | return (ret); | |
9685 | ||
9686 | print_history_records(nvhis, cb); | |
9687 | nvlist_free(nvhis); | |
9688 | } | |
34dc7c2f | 9689 | (void) printf("\n"); |
34dc7c2f BB |
9690 | |
9691 | return (ret); | |
9692 | } | |
9693 | ||
9694 | /* | |
9695 | * zpool history <pool> | |
9696 | * | |
9697 | * Displays the history of commands that modified pools. | |
9698 | */ | |
34dc7c2f BB |
9699 | int |
9700 | zpool_do_history(int argc, char **argv) | |
9701 | { | |
9702 | hist_cbdata_t cbdata = { 0 }; | |
9703 | int ret; | |
9704 | int c; | |
9705 | ||
9706 | cbdata.first = B_TRUE; | |
9707 | /* check options */ | |
9708 | while ((c = getopt(argc, argv, "li")) != -1) { | |
9709 | switch (c) { | |
9710 | case 'l': | |
6f1ffb06 | 9711 | cbdata.longfmt = B_TRUE; |
34dc7c2f BB |
9712 | break; |
9713 | case 'i': | |
6f1ffb06 | 9714 | cbdata.internal = B_TRUE; |
34dc7c2f BB |
9715 | break; |
9716 | case '?': | |
9717 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
9718 | optopt); | |
9719 | usage(B_FALSE); | |
9720 | } | |
9721 | } | |
9722 | argc -= optind; | |
9723 | argv += optind; | |
9724 | ||
2a673e76 AJ |
9725 | ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL, |
9726 | B_FALSE, get_history_one, &cbdata); | |
34dc7c2f BB |
9727 | |
9728 | if (argc == 0 && cbdata.first == B_TRUE) { | |
42cb3819 | 9729 | (void) fprintf(stderr, gettext("no pools available\n")); |
34dc7c2f BB |
9730 | return (0); |
9731 | } | |
9732 | ||
9733 | return (ret); | |
9734 | } | |
9735 | ||
26685276 BB |
9736 | typedef struct ev_opts { |
9737 | int verbose; | |
c5343ba7 | 9738 | int scripted; |
26685276 BB |
9739 | int follow; |
9740 | int clear; | |
88f9c939 | 9741 | char poolname[ZFS_MAX_DATASET_NAME_LEN]; |
26685276 BB |
9742 | } ev_opts_t; |
9743 | ||
9744 | static void | |
1e1c3980 | 9745 | zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts) |
26685276 | 9746 | { |
d1807f16 RY |
9747 | char ctime_str[26], str[32]; |
9748 | const char *ptr; | |
26685276 BB |
9749 | int64_t *tv; |
9750 | uint_t n; | |
9751 | ||
9752 | verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0); | |
9753 | memset(str, ' ', 32); | |
9754 | (void) ctime_r((const time_t *)&tv[0], ctime_str); | |
7cd723e6 GA |
9755 | (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */ |
9756 | (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */ | |
9757 | (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */ | |
d1d7e268 | 9758 | (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */ |
1e1c3980 SH |
9759 | if (opts->scripted) |
9760 | (void) printf(gettext("%s\t"), str); | |
9761 | else | |
9762 | (void) printf(gettext("%s "), str); | |
26685276 BB |
9763 | |
9764 | verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0); | |
9765 | (void) printf(gettext("%s\n"), ptr); | |
9766 | } | |
9767 | ||
9768 | static void | |
9769 | zpool_do_events_nvprint(nvlist_t *nvl, int depth) | |
9770 | { | |
9771 | nvpair_t *nvp; | |
9772 | ||
9773 | for (nvp = nvlist_next_nvpair(nvl, NULL); | |
9774 | nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { | |
9775 | ||
9776 | data_type_t type = nvpair_type(nvp); | |
9777 | const char *name = nvpair_name(nvp); | |
9778 | ||
9779 | boolean_t b; | |
9780 | uint8_t i8; | |
9781 | uint16_t i16; | |
9782 | uint32_t i32; | |
9783 | uint64_t i64; | |
d1807f16 | 9784 | const char *str; |
26685276 BB |
9785 | nvlist_t *cnv; |
9786 | ||
9787 | printf(gettext("%*s%s = "), depth, "", name); | |
9788 | ||
9789 | switch (type) { | |
9790 | case DATA_TYPE_BOOLEAN: | |
9791 | printf(gettext("%s"), "1"); | |
9792 | break; | |
9793 | ||
9794 | case DATA_TYPE_BOOLEAN_VALUE: | |
9795 | (void) nvpair_value_boolean_value(nvp, &b); | |
9796 | printf(gettext("%s"), b ? "1" : "0"); | |
9797 | break; | |
9798 | ||
9799 | case DATA_TYPE_BYTE: | |
9800 | (void) nvpair_value_byte(nvp, &i8); | |
9801 | printf(gettext("0x%x"), i8); | |
9802 | break; | |
9803 | ||
9804 | case DATA_TYPE_INT8: | |
9805 | (void) nvpair_value_int8(nvp, (void *)&i8); | |
9806 | printf(gettext("0x%x"), i8); | |
9807 | break; | |
9808 | ||
9809 | case DATA_TYPE_UINT8: | |
9810 | (void) nvpair_value_uint8(nvp, &i8); | |
9811 | printf(gettext("0x%x"), i8); | |
9812 | break; | |
9813 | ||
9814 | case DATA_TYPE_INT16: | |
9815 | (void) nvpair_value_int16(nvp, (void *)&i16); | |
9816 | printf(gettext("0x%x"), i16); | |
9817 | break; | |
9818 | ||
9819 | case DATA_TYPE_UINT16: | |
9820 | (void) nvpair_value_uint16(nvp, &i16); | |
9821 | printf(gettext("0x%x"), i16); | |
9822 | break; | |
9823 | ||
9824 | case DATA_TYPE_INT32: | |
9825 | (void) nvpair_value_int32(nvp, (void *)&i32); | |
9826 | printf(gettext("0x%x"), i32); | |
9827 | break; | |
9828 | ||
9829 | case DATA_TYPE_UINT32: | |
9830 | (void) nvpair_value_uint32(nvp, &i32); | |
9831 | printf(gettext("0x%x"), i32); | |
9832 | break; | |
9833 | ||
9834 | case DATA_TYPE_INT64: | |
9835 | (void) nvpair_value_int64(nvp, (void *)&i64); | |
9836 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
9837 | break; | |
9838 | ||
9839 | case DATA_TYPE_UINT64: | |
9840 | (void) nvpair_value_uint64(nvp, &i64); | |
d02ca379 DB |
9841 | /* |
9842 | * translate vdev state values to readable | |
9843 | * strings to aide zpool events consumers | |
9844 | */ | |
9845 | if (strcmp(name, | |
9846 | FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 || | |
9847 | strcmp(name, | |
9848 | FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) { | |
9849 | printf(gettext("\"%s\" (0x%llx)"), | |
9850 | zpool_state_to_name(i64, VDEV_AUX_NONE), | |
9851 | (u_longlong_t)i64); | |
9852 | } else { | |
9853 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
9854 | } | |
26685276 BB |
9855 | break; |
9856 | ||
9857 | case DATA_TYPE_HRTIME: | |
9858 | (void) nvpair_value_hrtime(nvp, (void *)&i64); | |
9859 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
9860 | break; | |
9861 | ||
9862 | case DATA_TYPE_STRING: | |
9863 | (void) nvpair_value_string(nvp, &str); | |
9864 | printf(gettext("\"%s\""), str ? str : "<NULL>"); | |
9865 | break; | |
9866 | ||
9867 | case DATA_TYPE_NVLIST: | |
9868 | printf(gettext("(embedded nvlist)\n")); | |
9869 | (void) nvpair_value_nvlist(nvp, &cnv); | |
9870 | zpool_do_events_nvprint(cnv, depth + 8); | |
c5343ba7 | 9871 | printf(gettext("%*s(end %s)"), depth, "", name); |
26685276 BB |
9872 | break; |
9873 | ||
9874 | case DATA_TYPE_NVLIST_ARRAY: { | |
9875 | nvlist_t **val; | |
9876 | uint_t i, nelem; | |
9877 | ||
9878 | (void) nvpair_value_nvlist_array(nvp, &val, &nelem); | |
9879 | printf(gettext("(%d embedded nvlists)\n"), nelem); | |
9880 | for (i = 0; i < nelem; i++) { | |
9881 | printf(gettext("%*s%s[%d] = %s\n"), | |
d1d7e268 | 9882 | depth, "", name, i, "(embedded nvlist)"); |
26685276 BB |
9883 | zpool_do_events_nvprint(val[i], depth + 8); |
9884 | printf(gettext("%*s(end %s[%i])\n"), | |
d1d7e268 | 9885 | depth, "", name, i); |
26685276 BB |
9886 | } |
9887 | printf(gettext("%*s(end %s)\n"), depth, "", name); | |
9888 | } | |
9889 | break; | |
9890 | ||
9891 | case DATA_TYPE_INT8_ARRAY: { | |
9892 | int8_t *val; | |
9893 | uint_t i, nelem; | |
9894 | ||
9895 | (void) nvpair_value_int8_array(nvp, &val, &nelem); | |
9896 | for (i = 0; i < nelem; i++) | |
9897 | printf(gettext("0x%x "), val[i]); | |
9898 | ||
9899 | break; | |
9900 | } | |
9901 | ||
9902 | case DATA_TYPE_UINT8_ARRAY: { | |
9903 | uint8_t *val; | |
9904 | uint_t i, nelem; | |
9905 | ||
9906 | (void) nvpair_value_uint8_array(nvp, &val, &nelem); | |
9907 | for (i = 0; i < nelem; i++) | |
9908 | printf(gettext("0x%x "), val[i]); | |
9909 | ||
9910 | break; | |
9911 | } | |
9912 | ||
9913 | case DATA_TYPE_INT16_ARRAY: { | |
9914 | int16_t *val; | |
9915 | uint_t i, nelem; | |
9916 | ||
9917 | (void) nvpair_value_int16_array(nvp, &val, &nelem); | |
9918 | for (i = 0; i < nelem; i++) | |
9919 | printf(gettext("0x%x "), val[i]); | |
9920 | ||
9921 | break; | |
9922 | } | |
9923 | ||
9924 | case DATA_TYPE_UINT16_ARRAY: { | |
9925 | uint16_t *val; | |
9926 | uint_t i, nelem; | |
9927 | ||
9928 | (void) nvpair_value_uint16_array(nvp, &val, &nelem); | |
9929 | for (i = 0; i < nelem; i++) | |
9930 | printf(gettext("0x%x "), val[i]); | |
9931 | ||
9932 | break; | |
9933 | } | |
9934 | ||
9935 | case DATA_TYPE_INT32_ARRAY: { | |
9936 | int32_t *val; | |
9937 | uint_t i, nelem; | |
9938 | ||
9939 | (void) nvpair_value_int32_array(nvp, &val, &nelem); | |
9940 | for (i = 0; i < nelem; i++) | |
9941 | printf(gettext("0x%x "), val[i]); | |
9942 | ||
9943 | break; | |
9944 | } | |
9945 | ||
9946 | case DATA_TYPE_UINT32_ARRAY: { | |
9947 | uint32_t *val; | |
9948 | uint_t i, nelem; | |
9949 | ||
9950 | (void) nvpair_value_uint32_array(nvp, &val, &nelem); | |
9951 | for (i = 0; i < nelem; i++) | |
9952 | printf(gettext("0x%x "), val[i]); | |
9953 | ||
9954 | break; | |
9955 | } | |
9956 | ||
9957 | case DATA_TYPE_INT64_ARRAY: { | |
9958 | int64_t *val; | |
9959 | uint_t i, nelem; | |
9960 | ||
9961 | (void) nvpair_value_int64_array(nvp, &val, &nelem); | |
9962 | for (i = 0; i < nelem; i++) | |
d1d7e268 MK |
9963 | printf(gettext("0x%llx "), |
9964 | (u_longlong_t)val[i]); | |
26685276 BB |
9965 | |
9966 | break; | |
9967 | } | |
9968 | ||
9969 | case DATA_TYPE_UINT64_ARRAY: { | |
9970 | uint64_t *val; | |
9971 | uint_t i, nelem; | |
9972 | ||
9973 | (void) nvpair_value_uint64_array(nvp, &val, &nelem); | |
9974 | for (i = 0; i < nelem; i++) | |
d1d7e268 MK |
9975 | printf(gettext("0x%llx "), |
9976 | (u_longlong_t)val[i]); | |
26685276 BB |
9977 | |
9978 | break; | |
9979 | } | |
9980 | ||
d21705ea | 9981 | case DATA_TYPE_STRING_ARRAY: { |
d1807f16 | 9982 | const char **str; |
d21705ea BB |
9983 | uint_t i, nelem; |
9984 | ||
9985 | (void) nvpair_value_string_array(nvp, &str, &nelem); | |
9986 | for (i = 0; i < nelem; i++) | |
9987 | printf(gettext("\"%s\" "), | |
9988 | str[i] ? str[i] : "<NULL>"); | |
9989 | ||
9990 | break; | |
9991 | } | |
9992 | ||
26685276 BB |
9993 | case DATA_TYPE_BOOLEAN_ARRAY: |
9994 | case DATA_TYPE_BYTE_ARRAY: | |
9995 | case DATA_TYPE_DOUBLE: | |
6b64382b | 9996 | case DATA_TYPE_DONTCARE: |
26685276 BB |
9997 | case DATA_TYPE_UNKNOWN: |
9998 | printf(gettext("<unknown>")); | |
9999 | break; | |
10000 | } | |
10001 | ||
10002 | printf(gettext("\n")); | |
10003 | } | |
10004 | } | |
10005 | ||
10006 | static int | |
10007 | zpool_do_events_next(ev_opts_t *opts) | |
10008 | { | |
10009 | nvlist_t *nvl; | |
9b101a73 | 10010 | int zevent_fd, ret, dropped; |
d1807f16 | 10011 | const char *pool; |
26685276 | 10012 | |
9b101a73 BB |
10013 | zevent_fd = open(ZFS_DEV, O_RDWR); |
10014 | VERIFY(zevent_fd >= 0); | |
26685276 | 10015 | |
c5343ba7 BB |
10016 | if (!opts->scripted) |
10017 | (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS"); | |
26685276 BB |
10018 | |
10019 | while (1) { | |
10020 | ret = zpool_events_next(g_zfs, &nvl, &dropped, | |
8c7aa0cf | 10021 | (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd); |
26685276 BB |
10022 | if (ret || nvl == NULL) |
10023 | break; | |
10024 | ||
10025 | if (dropped > 0) | |
10026 | (void) printf(gettext("dropped %d events\n"), dropped); | |
10027 | ||
88f9c939 | 10028 | if (strlen(opts->poolname) > 0 && |
10029 | nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 && | |
10030 | strcmp(opts->poolname, pool) != 0) | |
10031 | continue; | |
10032 | ||
1e1c3980 | 10033 | zpool_do_events_short(nvl, opts); |
26685276 BB |
10034 | |
10035 | if (opts->verbose) { | |
10036 | zpool_do_events_nvprint(nvl, 8); | |
10037 | printf(gettext("\n")); | |
10038 | } | |
50fe577d | 10039 | (void) fflush(stdout); |
26685276 BB |
10040 | |
10041 | nvlist_free(nvl); | |
10042 | } | |
10043 | ||
9b101a73 | 10044 | VERIFY(0 == close(zevent_fd)); |
26685276 BB |
10045 | |
10046 | return (ret); | |
10047 | } | |
10048 | ||
10049 | static int | |
e40ca391 | 10050 | zpool_do_events_clear(void) |
26685276 BB |
10051 | { |
10052 | int count, ret; | |
10053 | ||
10054 | ret = zpool_events_clear(g_zfs, &count); | |
10055 | if (!ret) | |
10056 | (void) printf(gettext("cleared %d events\n"), count); | |
10057 | ||
10058 | return (ret); | |
10059 | } | |
10060 | ||
10061 | /* | |
88f9c939 | 10062 | * zpool events [-vHf [pool] | -c] |
26685276 BB |
10063 | * |
10064 | * Displays events logs by ZFS. | |
10065 | */ | |
10066 | int | |
10067 | zpool_do_events(int argc, char **argv) | |
10068 | { | |
10069 | ev_opts_t opts = { 0 }; | |
10070 | int ret; | |
10071 | int c; | |
10072 | ||
10073 | /* check options */ | |
c5343ba7 | 10074 | while ((c = getopt(argc, argv, "vHfc")) != -1) { |
26685276 BB |
10075 | switch (c) { |
10076 | case 'v': | |
10077 | opts.verbose = 1; | |
c5343ba7 BB |
10078 | break; |
10079 | case 'H': | |
10080 | opts.scripted = 1; | |
26685276 BB |
10081 | break; |
10082 | case 'f': | |
10083 | opts.follow = 1; | |
10084 | break; | |
10085 | case 'c': | |
10086 | opts.clear = 1; | |
10087 | break; | |
10088 | case '?': | |
10089 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
10090 | optopt); | |
10091 | usage(B_FALSE); | |
10092 | } | |
10093 | } | |
10094 | argc -= optind; | |
10095 | argv += optind; | |
10096 | ||
88f9c939 | 10097 | if (argc > 1) { |
10098 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
10099 | usage(B_FALSE); | |
10100 | } else if (argc == 1) { | |
10101 | (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname)); | |
10102 | if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) { | |
10103 | (void) fprintf(stderr, | |
10104 | gettext("invalid pool name '%s'\n"), opts.poolname); | |
10105 | usage(B_FALSE); | |
10106 | } | |
10107 | } | |
10108 | ||
10109 | if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) && | |
10110 | opts.clear) { | |
10111 | (void) fprintf(stderr, | |
10112 | gettext("invalid options combined with -c\n")); | |
10113 | usage(B_FALSE); | |
10114 | } | |
10115 | ||
26685276 | 10116 | if (opts.clear) |
e40ca391 | 10117 | ret = zpool_do_events_clear(); |
26685276 BB |
10118 | else |
10119 | ret = zpool_do_events_next(&opts); | |
10120 | ||
d1d7e268 | 10121 | return (ret); |
26685276 BB |
10122 | } |
10123 | ||
34dc7c2f | 10124 | static int |
2a673e76 | 10125 | get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data) |
34dc7c2f BB |
10126 | { |
10127 | zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; | |
2a673e76 | 10128 | char value[ZFS_MAXPROPLEN]; |
34dc7c2f | 10129 | zprop_source_t srctype; |
34dc7c2f | 10130 | |
2a673e76 AJ |
10131 | for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL; |
10132 | pl = pl->pl_next) { | |
10133 | char *prop_name; | |
34dc7c2f | 10134 | /* |
2a673e76 AJ |
10135 | * If the first property is pool name, it is a special |
10136 | * placeholder that we can skip. This will also skip | |
34dc7c2f BB |
10137 | * over the name property when 'all' is specified. |
10138 | */ | |
10139 | if (pl->pl_prop == ZPOOL_PROP_NAME && | |
10140 | pl == cbp->cb_proplist) | |
10141 | continue; | |
10142 | ||
2a673e76 AJ |
10143 | if (pl->pl_prop == ZPROP_INVAL) { |
10144 | prop_name = pl->pl_user_prop; | |
10145 | } else { | |
10146 | prop_name = (char *)vdev_prop_to_name(pl->pl_prop); | |
10147 | } | |
10148 | if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop, | |
10149 | prop_name, value, sizeof (value), &srctype, | |
10150 | cbp->cb_literal) == 0) { | |
10151 | zprop_print_one_property(vdevname, cbp, prop_name, | |
10152 | value, srctype, NULL, NULL); | |
10153 | } | |
10154 | } | |
34dc7c2f | 10155 | |
2a673e76 AJ |
10156 | return (0); |
10157 | } | |
10158 | ||
10159 | static int | |
3e4ed421 | 10160 | get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data) |
2a673e76 AJ |
10161 | { |
10162 | zpool_handle_t *zhp = zhp_data; | |
10163 | zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; | |
3e4ed421 RW |
10164 | char *vdevname; |
10165 | const char *type; | |
2a673e76 AJ |
10166 | int ret; |
10167 | ||
3e4ed421 RW |
10168 | /* |
10169 | * zpool_vdev_name() transforms the root vdev name (i.e., root-0) to the | |
10170 | * pool name for display purposes, which is not desired. Fallback to | |
10171 | * zpool_vdev_name() when not dealing with the root vdev. | |
10172 | */ | |
10173 | type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE); | |
10174 | if (zhp != NULL && strcmp(type, "root") == 0) | |
10175 | vdevname = strdup("root-0"); | |
10176 | else | |
10177 | vdevname = zpool_vdev_name(g_zfs, zhp, nv, | |
10178 | cbp->cb_vdevs.cb_name_flags); | |
2a673e76 | 10179 | |
3e4ed421 | 10180 | (void) vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist); |
2a673e76 | 10181 | |
2a673e76 AJ |
10182 | ret = get_callback_vdev(zhp, vdevname, data); |
10183 | ||
3e4ed421 RW |
10184 | free(vdevname); |
10185 | ||
2a673e76 AJ |
10186 | return (ret); |
10187 | } | |
10188 | ||
10189 | static int | |
10190 | get_callback(zpool_handle_t *zhp, void *data) | |
10191 | { | |
10192 | zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; | |
8eae2d21 | 10193 | char value[ZFS_MAXPROPLEN]; |
2a673e76 AJ |
10194 | zprop_source_t srctype; |
10195 | zprop_list_t *pl; | |
10196 | int vid; | |
10197 | ||
10198 | if (cbp->cb_type == ZFS_TYPE_VDEV) { | |
10199 | if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) { | |
2a673e76 | 10200 | for_each_vdev(zhp, get_callback_vdev_cb, data); |
9ae529ec | 10201 | } else { |
2a673e76 AJ |
10202 | /* Adjust column widths for vdev properties */ |
10203 | for (vid = 0; vid < cbp->cb_vdevs.cb_names_count; | |
10204 | vid++) { | |
10205 | vdev_expand_proplist(zhp, | |
10206 | cbp->cb_vdevs.cb_names[vid], | |
10207 | &cbp->cb_proplist); | |
10208 | } | |
10209 | /* Display the properties */ | |
10210 | for (vid = 0; vid < cbp->cb_vdevs.cb_names_count; | |
10211 | vid++) { | |
10212 | get_callback_vdev(zhp, | |
10213 | cbp->cb_vdevs.cb_names[vid], data); | |
10214 | } | |
10215 | } | |
10216 | } else { | |
10217 | assert(cbp->cb_type == ZFS_TYPE_POOL); | |
10218 | for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { | |
10219 | /* | |
10220 | * Skip the special fake placeholder. This will also | |
10221 | * skip over the name property when 'all' is specified. | |
10222 | */ | |
10223 | if (pl->pl_prop == ZPOOL_PROP_NAME && | |
10224 | pl == cbp->cb_proplist) | |
9ae529ec CS |
10225 | continue; |
10226 | ||
2a673e76 | 10227 | if (pl->pl_prop == ZPROP_INVAL && |
8eae2d21 AJ |
10228 | zfs_prop_user(pl->pl_user_prop)) { |
10229 | srctype = ZPROP_SRC_LOCAL; | |
10230 | ||
10231 | if (zpool_get_userprop(zhp, pl->pl_user_prop, | |
10232 | value, sizeof (value), &srctype) != 0) | |
10233 | continue; | |
10234 | ||
10235 | zprop_print_one_property(zpool_get_name(zhp), | |
10236 | cbp, pl->pl_user_prop, value, srctype, | |
10237 | NULL, NULL); | |
10238 | } else if (pl->pl_prop == ZPROP_INVAL && | |
2a673e76 AJ |
10239 | (zpool_prop_feature(pl->pl_user_prop) || |
10240 | zpool_prop_unsupported(pl->pl_user_prop))) { | |
10241 | srctype = ZPROP_SRC_LOCAL; | |
10242 | ||
10243 | if (zpool_prop_get_feature(zhp, | |
10244 | pl->pl_user_prop, value, | |
10245 | sizeof (value)) == 0) { | |
10246 | zprop_print_one_property( | |
10247 | zpool_get_name(zhp), cbp, | |
10248 | pl->pl_user_prop, value, srctype, | |
10249 | NULL, NULL); | |
10250 | } | |
10251 | } else { | |
10252 | if (zpool_get_prop(zhp, pl->pl_prop, value, | |
10253 | sizeof (value), &srctype, | |
10254 | cbp->cb_literal) != 0) | |
10255 | continue; | |
10256 | ||
10257 | zprop_print_one_property(zpool_get_name(zhp), | |
10258 | cbp, zpool_prop_to_name(pl->pl_prop), | |
10259 | value, srctype, NULL, NULL); | |
10260 | } | |
9ae529ec | 10261 | } |
34dc7c2f | 10262 | } |
2a673e76 | 10263 | |
34dc7c2f BB |
10264 | return (0); |
10265 | } | |
10266 | ||
2a8b84b7 AS |
10267 | /* |
10268 | * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... | |
10269 | * | |
10270 | * -H Scripted mode. Don't display headers, and separate properties | |
10271 | * by a single tab. | |
10272 | * -o List of columns to display. Defaults to | |
10273 | * "name,property,value,source". | |
4e33ba4c | 10274 | * -p Display values in parsable (exact) format. |
2a8b84b7 AS |
10275 | * |
10276 | * Get properties of pools in the system. Output space statistics | |
10277 | * for each one as well as other attributes. | |
10278 | */ | |
34dc7c2f BB |
10279 | int |
10280 | zpool_do_get(int argc, char **argv) | |
10281 | { | |
10282 | zprop_get_cbdata_t cb = { 0 }; | |
10283 | zprop_list_t fake_name = { 0 }; | |
2a8b84b7 AS |
10284 | int ret; |
10285 | int c, i; | |
2a673e76 | 10286 | char *propstr = NULL; |
3e4ed421 | 10287 | char *vdev = NULL; |
2a8b84b7 AS |
10288 | |
10289 | cb.cb_first = B_TRUE; | |
10290 | ||
10291 | /* | |
10292 | * Set up default columns and sources. | |
10293 | */ | |
10294 | cb.cb_sources = ZPROP_SRC_ALL; | |
10295 | cb.cb_columns[0] = GET_COL_NAME; | |
10296 | cb.cb_columns[1] = GET_COL_PROPERTY; | |
10297 | cb.cb_columns[2] = GET_COL_VALUE; | |
10298 | cb.cb_columns[3] = GET_COL_SOURCE; | |
10299 | cb.cb_type = ZFS_TYPE_POOL; | |
2a673e76 AJ |
10300 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID; |
10301 | current_prop_type = cb.cb_type; | |
34dc7c2f | 10302 | |
d65e7381 | 10303 | /* check options */ |
2a8b84b7 | 10304 | while ((c = getopt(argc, argv, ":Hpo:")) != -1) { |
d65e7381 RE |
10305 | switch (c) { |
10306 | case 'p': | |
10307 | cb.cb_literal = B_TRUE; | |
10308 | break; | |
79eb71dc TF |
10309 | case 'H': |
10310 | cb.cb_scripted = B_TRUE; | |
10311 | break; | |
2a8b84b7 | 10312 | case 'o': |
861166b0 | 10313 | memset(&cb.cb_columns, 0, sizeof (cb.cb_columns)); |
2a8b84b7 | 10314 | i = 0; |
66cd170d AZ |
10315 | |
10316 | for (char *tok; (tok = strsep(&optarg, ",")); ) { | |
10317 | static const char *const col_opts[] = | |
2a8b84b7 | 10318 | { "name", "property", "value", "source", |
66cd170d AZ |
10319 | "all" }; |
10320 | static const zfs_get_column_t col_cols[] = | |
10321 | { GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE, | |
10322 | GET_COL_SOURCE }; | |
2a8b84b7 | 10323 | |
c7978784 | 10324 | if (i == ZFS_GET_NCOLS - 1) { |
2a8b84b7 AS |
10325 | (void) fprintf(stderr, gettext("too " |
10326 | "many fields given to -o " | |
10327 | "option\n")); | |
10328 | usage(B_FALSE); | |
10329 | } | |
79eb71dc | 10330 | |
66cd170d AZ |
10331 | for (c = 0; c < ARRAY_SIZE(col_opts); ++c) |
10332 | if (strcmp(tok, col_opts[c]) == 0) | |
10333 | goto found; | |
10334 | ||
10335 | (void) fprintf(stderr, | |
10336 | gettext("invalid column name '%s'\n"), tok); | |
10337 | usage(B_FALSE); | |
10338 | ||
10339 | found: | |
10340 | if (c >= 4) { | |
2a8b84b7 AS |
10341 | if (i > 0) { |
10342 | (void) fprintf(stderr, | |
10343 | gettext("\"all\" conflicts " | |
10344 | "with specific fields " | |
10345 | "given to -o option\n")); | |
10346 | usage(B_FALSE); | |
10347 | } | |
66cd170d AZ |
10348 | |
10349 | memcpy(cb.cb_columns, col_cols, | |
10350 | sizeof (col_cols)); | |
c7978784 | 10351 | i = ZFS_GET_NCOLS - 1; |
66cd170d AZ |
10352 | } else |
10353 | cb.cb_columns[i++] = col_cols[c]; | |
2a8b84b7 AS |
10354 | } |
10355 | break; | |
d65e7381 RE |
10356 | case '?': |
10357 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
10358 | optopt); | |
10359 | usage(B_FALSE); | |
10360 | } | |
10361 | } | |
10362 | ||
10363 | argc -= optind; | |
10364 | argv += optind; | |
10365 | ||
10366 | if (argc < 1) { | |
9ae529ec CS |
10367 | (void) fprintf(stderr, gettext("missing property " |
10368 | "argument\n")); | |
34dc7c2f | 10369 | usage(B_FALSE); |
9ae529ec | 10370 | } |
34dc7c2f | 10371 | |
2a673e76 AJ |
10372 | /* Properties list is needed later by zprop_get_list() */ |
10373 | propstr = argv[0]; | |
34dc7c2f | 10374 | |
d65e7381 RE |
10375 | argc--; |
10376 | argv++; | |
10377 | ||
2a673e76 AJ |
10378 | if (argc == 0) { |
10379 | /* No args, so just print the defaults. */ | |
10380 | } else if (are_all_pools(argc, argv)) { | |
10381 | /* All the args are pool names */ | |
10382 | } else if (are_all_pools(1, argv)) { | |
10383 | /* The first arg is a pool name */ | |
10384 | if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) || | |
3e4ed421 | 10385 | (argc == 2 && strcmp(argv[1], "root") == 0) || |
2a673e76 AJ |
10386 | are_vdevs_in_pool(argc - 1, argv + 1, argv[0], |
10387 | &cb.cb_vdevs)) { | |
3e4ed421 RW |
10388 | |
10389 | if (strcmp(argv[1], "root") == 0) | |
10390 | vdev = strdup("root-0"); | |
10391 | else | |
10392 | vdev = strdup(argv[1]); | |
10393 | ||
2a673e76 | 10394 | /* ... and the rest are vdev names */ |
3e4ed421 | 10395 | cb.cb_vdevs.cb_names = &vdev; |
2a673e76 AJ |
10396 | cb.cb_vdevs.cb_names_count = argc - 1; |
10397 | cb.cb_type = ZFS_TYPE_VDEV; | |
10398 | argc = 1; /* One pool to process */ | |
10399 | } else { | |
10400 | fprintf(stderr, gettext("Expected a list of vdevs in" | |
10401 | " \"%s\", but got:\n"), argv[0]); | |
10402 | error_list_unresolved_vdevs(argc - 1, argv + 1, | |
10403 | argv[0], &cb.cb_vdevs); | |
10404 | fprintf(stderr, "\n"); | |
10405 | usage(B_FALSE); | |
10406 | return (1); | |
10407 | } | |
10408 | } else { | |
10409 | /* | |
10410 | * The first arg isn't a pool name, | |
10411 | */ | |
10412 | fprintf(stderr, gettext("missing pool name.\n")); | |
10413 | fprintf(stderr, "\n"); | |
10414 | usage(B_FALSE); | |
10415 | return (1); | |
10416 | } | |
10417 | ||
10418 | if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist, | |
10419 | cb.cb_type) != 0) { | |
10420 | /* Use correct list of valid properties (pool or vdev) */ | |
10421 | current_prop_type = cb.cb_type; | |
10422 | usage(B_FALSE); | |
10423 | } | |
10424 | ||
34dc7c2f BB |
10425 | if (cb.cb_proplist != NULL) { |
10426 | fake_name.pl_prop = ZPOOL_PROP_NAME; | |
10427 | fake_name.pl_width = strlen(gettext("NAME")); | |
10428 | fake_name.pl_next = cb.cb_proplist; | |
10429 | cb.cb_proplist = &fake_name; | |
10430 | } | |
10431 | ||
2a673e76 AJ |
10432 | ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type, |
10433 | cb.cb_literal, get_callback, &cb); | |
34dc7c2f BB |
10434 | |
10435 | if (cb.cb_proplist == &fake_name) | |
10436 | zprop_free_list(fake_name.pl_next); | |
10437 | else | |
10438 | zprop_free_list(cb.cb_proplist); | |
10439 | ||
3e4ed421 RW |
10440 | if (vdev != NULL) |
10441 | free(vdev); | |
10442 | ||
34dc7c2f BB |
10443 | return (ret); |
10444 | } | |
10445 | ||
10446 | typedef struct set_cbdata { | |
10447 | char *cb_propname; | |
10448 | char *cb_value; | |
2a673e76 AJ |
10449 | zfs_type_t cb_type; |
10450 | vdev_cbdata_t cb_vdevs; | |
34dc7c2f BB |
10451 | boolean_t cb_any_successful; |
10452 | } set_cbdata_t; | |
10453 | ||
65c7cc49 | 10454 | static int |
2a673e76 | 10455 | set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb) |
34dc7c2f BB |
10456 | { |
10457 | int error; | |
34dc7c2f | 10458 | |
e086db16 CB |
10459 | /* Check if we have out-of-bounds features */ |
10460 | if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) { | |
10461 | boolean_t features[SPA_FEATURES]; | |
10462 | if (zpool_do_load_compat(cb->cb_value, features) != | |
10463 | ZPOOL_COMPATIBILITY_OK) | |
10464 | return (-1); | |
10465 | ||
10466 | nvlist_t *enabled = zpool_get_features(zhp); | |
10467 | spa_feature_t i; | |
10468 | for (i = 0; i < SPA_FEATURES; i++) { | |
10469 | const char *fguid = spa_feature_table[i].fi_guid; | |
10470 | if (nvlist_exists(enabled, fguid) && !features[i]) | |
10471 | break; | |
10472 | } | |
10473 | if (i < SPA_FEATURES) | |
10474 | (void) fprintf(stderr, gettext("Warning: one or " | |
10475 | "more features already enabled on pool '%s'\n" | |
10476 | "are not present in this compatibility set.\n"), | |
10477 | zpool_get_name(zhp)); | |
10478 | } | |
10479 | ||
10480 | /* if we're setting a feature, check it's in compatibility set */ | |
10481 | if (zpool_prop_feature(cb->cb_propname) && | |
10482 | strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) { | |
10483 | char *fname = strchr(cb->cb_propname, '@') + 1; | |
10484 | spa_feature_t f; | |
10485 | ||
10486 | if (zfeature_lookup_name(fname, &f) == 0) { | |
10487 | char compat[ZFS_MAXPROPLEN]; | |
10488 | if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, | |
10489 | compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0) | |
10490 | compat[0] = '\0'; | |
10491 | ||
10492 | boolean_t features[SPA_FEATURES]; | |
10493 | if (zpool_do_load_compat(compat, features) != | |
10494 | ZPOOL_COMPATIBILITY_OK) { | |
10495 | (void) fprintf(stderr, gettext("Error: " | |
10496 | "cannot enable feature '%s' on pool '%s'\n" | |
10497 | "because the pool's 'compatibility' " | |
10498 | "property cannot be parsed.\n"), | |
10499 | fname, zpool_get_name(zhp)); | |
10500 | return (-1); | |
10501 | } | |
10502 | ||
10503 | if (!features[f]) { | |
10504 | (void) fprintf(stderr, gettext("Error: " | |
10505 | "cannot enable feature '%s' on pool '%s'\n" | |
10506 | "as it is not specified in this pool's " | |
10507 | "current compatibility set.\n" | |
10508 | "Consider setting 'compatibility' to a " | |
10509 | "less restrictive set, or to 'off'.\n"), | |
10510 | fname, zpool_get_name(zhp)); | |
10511 | return (-1); | |
10512 | } | |
10513 | } | |
10514 | } | |
10515 | ||
34dc7c2f BB |
10516 | error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); |
10517 | ||
2a673e76 AJ |
10518 | return (error); |
10519 | } | |
10520 | ||
10521 | static int | |
10522 | set_callback(zpool_handle_t *zhp, void *data) | |
10523 | { | |
10524 | int error; | |
10525 | set_cbdata_t *cb = (set_cbdata_t *)data; | |
10526 | ||
10527 | if (cb->cb_type == ZFS_TYPE_VDEV) { | |
10528 | error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names, | |
10529 | cb->cb_propname, cb->cb_value); | |
10530 | } else { | |
10531 | assert(cb->cb_type == ZFS_TYPE_POOL); | |
10532 | error = set_pool_callback(zhp, cb); | |
10533 | } | |
34dc7c2f | 10534 | |
2a673e76 | 10535 | cb->cb_any_successful = !error; |
34dc7c2f BB |
10536 | return (error); |
10537 | } | |
10538 | ||
10539 | int | |
10540 | zpool_do_set(int argc, char **argv) | |
10541 | { | |
10542 | set_cbdata_t cb = { 0 }; | |
10543 | int error; | |
3e4ed421 | 10544 | char *vdev = NULL; |
34dc7c2f | 10545 | |
2a673e76 | 10546 | current_prop_type = ZFS_TYPE_POOL; |
34dc7c2f BB |
10547 | if (argc > 1 && argv[1][0] == '-') { |
10548 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
10549 | argv[1][1]); | |
10550 | usage(B_FALSE); | |
10551 | } | |
10552 | ||
10553 | if (argc < 2) { | |
10554 | (void) fprintf(stderr, gettext("missing property=value " | |
10555 | "argument\n")); | |
10556 | usage(B_FALSE); | |
10557 | } | |
10558 | ||
10559 | if (argc < 3) { | |
10560 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
10561 | usage(B_FALSE); | |
10562 | } | |
10563 | ||
2a673e76 | 10564 | if (argc > 4) { |
34dc7c2f BB |
10565 | (void) fprintf(stderr, gettext("too many pool names\n")); |
10566 | usage(B_FALSE); | |
10567 | } | |
10568 | ||
10569 | cb.cb_propname = argv[1]; | |
2a673e76 AJ |
10570 | cb.cb_type = ZFS_TYPE_POOL; |
10571 | cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID; | |
34dc7c2f BB |
10572 | cb.cb_value = strchr(cb.cb_propname, '='); |
10573 | if (cb.cb_value == NULL) { | |
10574 | (void) fprintf(stderr, gettext("missing value in " | |
10575 | "property=value argument\n")); | |
10576 | usage(B_FALSE); | |
10577 | } | |
10578 | ||
10579 | *(cb.cb_value) = '\0'; | |
10580 | cb.cb_value++; | |
2a673e76 AJ |
10581 | argc -= 2; |
10582 | argv += 2; | |
10583 | ||
7a85f58d RW |
10584 | /* argv[0] is pool name */ |
10585 | if (!is_pool(argv[0])) { | |
10586 | (void) fprintf(stderr, | |
10587 | gettext("cannot open '%s': is not a pool\n"), argv[0]); | |
10588 | return (EINVAL); | |
10589 | } | |
10590 | ||
10591 | /* argv[1], when supplied, is vdev name */ | |
10592 | if (argc == 2) { | |
3e4ed421 RW |
10593 | |
10594 | if (strcmp(argv[1], "root") == 0) | |
10595 | vdev = strdup("root-0"); | |
10596 | else | |
10597 | vdev = strdup(argv[1]); | |
10598 | ||
10599 | if (!are_vdevs_in_pool(1, &vdev, argv[0], &cb.cb_vdevs)) { | |
7a85f58d RW |
10600 | (void) fprintf(stderr, gettext( |
10601 | "cannot find '%s' in '%s': device not in pool\n"), | |
3e4ed421 RW |
10602 | vdev, argv[0]); |
10603 | free(vdev); | |
7a85f58d RW |
10604 | return (EINVAL); |
10605 | } | |
3e4ed421 | 10606 | cb.cb_vdevs.cb_names = &vdev; |
2a673e76 AJ |
10607 | cb.cb_vdevs.cb_names_count = 1; |
10608 | cb.cb_type = ZFS_TYPE_VDEV; | |
2a673e76 | 10609 | } |
34dc7c2f | 10610 | |
7a85f58d | 10611 | error = for_each_pool(1, argv, B_TRUE, NULL, ZFS_TYPE_POOL, |
2a673e76 | 10612 | B_FALSE, set_callback, &cb); |
34dc7c2f | 10613 | |
3e4ed421 RW |
10614 | if (vdev != NULL) |
10615 | free(vdev); | |
10616 | ||
34dc7c2f BB |
10617 | return (error); |
10618 | } | |
2288d419 BB |
10619 | |
10620 | /* Add up the total number of bytes left to initialize/trim across all vdevs */ | |
e60e158e | 10621 | static uint64_t |
2288d419 | 10622 | vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity) |
e60e158e JG |
10623 | { |
10624 | uint64_t bytes_remaining; | |
10625 | nvlist_t **child; | |
10626 | uint_t c, children; | |
10627 | vdev_stat_t *vs; | |
10628 | ||
2288d419 BB |
10629 | assert(activity == ZPOOL_WAIT_INITIALIZE || |
10630 | activity == ZPOOL_WAIT_TRIM); | |
10631 | ||
e60e158e JG |
10632 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
10633 | (uint64_t **)&vs, &c) == 0); | |
10634 | ||
2288d419 BB |
10635 | if (activity == ZPOOL_WAIT_INITIALIZE && |
10636 | vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) | |
e60e158e JG |
10637 | bytes_remaining = vs->vs_initialize_bytes_est - |
10638 | vs->vs_initialize_bytes_done; | |
2288d419 BB |
10639 | else if (activity == ZPOOL_WAIT_TRIM && |
10640 | vs->vs_trim_state == VDEV_TRIM_ACTIVE) | |
10641 | bytes_remaining = vs->vs_trim_bytes_est - | |
10642 | vs->vs_trim_bytes_done; | |
e60e158e JG |
10643 | else |
10644 | bytes_remaining = 0; | |
10645 | ||
10646 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
10647 | &child, &children) != 0) | |
10648 | children = 0; | |
10649 | ||
10650 | for (c = 0; c < children; c++) | |
2288d419 | 10651 | bytes_remaining += vdev_activity_remaining(child[c], activity); |
e60e158e JG |
10652 | |
10653 | return (bytes_remaining); | |
10654 | } | |
10655 | ||
9a49d3f3 BB |
10656 | /* Add up the total number of bytes left to rebuild across top-level vdevs */ |
10657 | static uint64_t | |
10658 | vdev_activity_top_remaining(nvlist_t *nv) | |
10659 | { | |
10660 | uint64_t bytes_remaining = 0; | |
10661 | nvlist_t **child; | |
10662 | uint_t children; | |
10663 | int error; | |
10664 | ||
10665 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
10666 | &child, &children) != 0) | |
10667 | children = 0; | |
10668 | ||
10669 | for (uint_t c = 0; c < children; c++) { | |
10670 | vdev_rebuild_stat_t *vrs; | |
10671 | uint_t i; | |
10672 | ||
10673 | error = nvlist_lookup_uint64_array(child[c], | |
10674 | ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i); | |
10675 | if (error == 0) { | |
10676 | if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) { | |
10677 | bytes_remaining += (vrs->vrs_bytes_est - | |
10678 | vrs->vrs_bytes_rebuilt); | |
10679 | } | |
10680 | } | |
10681 | } | |
10682 | ||
10683 | return (bytes_remaining); | |
10684 | } | |
10685 | ||
e60e158e JG |
10686 | /* Whether any vdevs are 'spare' or 'replacing' vdevs */ |
10687 | static boolean_t | |
10688 | vdev_any_spare_replacing(nvlist_t *nv) | |
10689 | { | |
10690 | nvlist_t **child; | |
10691 | uint_t c, children; | |
d1807f16 | 10692 | const char *vdev_type; |
e60e158e JG |
10693 | |
10694 | (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type); | |
10695 | ||
10696 | if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 || | |
b2255edc BB |
10697 | strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 || |
10698 | strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) { | |
e60e158e JG |
10699 | return (B_TRUE); |
10700 | } | |
10701 | ||
10702 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
10703 | &child, &children) != 0) | |
10704 | children = 0; | |
10705 | ||
10706 | for (c = 0; c < children; c++) { | |
10707 | if (vdev_any_spare_replacing(child[c])) | |
10708 | return (B_TRUE); | |
10709 | } | |
10710 | ||
10711 | return (B_FALSE); | |
10712 | } | |
10713 | ||
10714 | typedef struct wait_data { | |
10715 | char *wd_poolname; | |
10716 | boolean_t wd_scripted; | |
10717 | boolean_t wd_exact; | |
10718 | boolean_t wd_headers_once; | |
d553fb9b | 10719 | boolean_t wd_should_exit; |
e60e158e JG |
10720 | /* Which activities to wait for */ |
10721 | boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES]; | |
10722 | float wd_interval; | |
d553fb9b JL |
10723 | pthread_cond_t wd_cv; |
10724 | pthread_mutex_t wd_mutex; | |
e60e158e JG |
10725 | } wait_data_t; |
10726 | ||
10727 | /* | |
10728 | * Print to stdout a single line, containing one column for each activity that | |
10729 | * we are waiting for specifying how many bytes of work are left for that | |
10730 | * activity. | |
10731 | */ | |
10732 | static void | |
10733 | print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row) | |
10734 | { | |
10735 | nvlist_t *config, *nvroot; | |
10736 | uint_t c; | |
10737 | int i; | |
10738 | pool_checkpoint_stat_t *pcs = NULL; | |
10739 | pool_scan_stat_t *pss = NULL; | |
10740 | pool_removal_stat_t *prs = NULL; | |
a926aab9 AZ |
10741 | const char *const headers[] = {"DISCARD", "FREE", "INITIALIZE", |
10742 | "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM"}; | |
e60e158e JG |
10743 | int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES]; |
10744 | ||
10745 | /* Calculate the width of each column */ | |
10746 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { | |
10747 | /* | |
10748 | * Make sure we have enough space in the col for pretty-printed | |
10749 | * numbers and for the column header, and then leave a couple | |
10750 | * spaces between cols for readability. | |
10751 | */ | |
10752 | col_widths[i] = MAX(strlen(headers[i]), 6) + 2; | |
10753 | } | |
10754 | ||
10755 | /* Print header if appropriate */ | |
10756 | int term_height = terminal_height(); | |
10757 | boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 && | |
10758 | row % (term_height-1) == 0); | |
10759 | if (!wd->wd_scripted && (row == 0 || reprint_header)) { | |
10760 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { | |
10761 | if (wd->wd_enabled[i]) | |
10762 | (void) printf("%*s", col_widths[i], headers[i]); | |
10763 | } | |
a926aab9 | 10764 | (void) fputc('\n', stdout); |
e60e158e JG |
10765 | } |
10766 | ||
10767 | /* Bytes of work remaining in each activity */ | |
10768 | int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0}; | |
10769 | ||
10770 | bytes_rem[ZPOOL_WAIT_FREE] = | |
10771 | zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL); | |
10772 | ||
10773 | config = zpool_get_config(zhp, NULL); | |
10774 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); | |
10775 | ||
10776 | (void) nvlist_lookup_uint64_array(nvroot, | |
10777 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
10778 | if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING) | |
10779 | bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space; | |
10780 | ||
10781 | (void) nvlist_lookup_uint64_array(nvroot, | |
10782 | ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); | |
10783 | if (prs != NULL && prs->prs_state == DSS_SCANNING) | |
10784 | bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy - | |
10785 | prs->prs_copied; | |
10786 | ||
10787 | (void) nvlist_lookup_uint64_array(nvroot, | |
10788 | ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c); | |
10789 | if (pss != NULL && pss->pss_state == DSS_SCANNING && | |
10790 | pss->pss_pass_scrub_pause == 0) { | |
10791 | int64_t rem = pss->pss_to_examine - pss->pss_issued; | |
10792 | if (pss->pss_func == POOL_SCAN_SCRUB) | |
10793 | bytes_rem[ZPOOL_WAIT_SCRUB] = rem; | |
10794 | else | |
10795 | bytes_rem[ZPOOL_WAIT_RESILVER] = rem; | |
9a49d3f3 BB |
10796 | } else if (check_rebuilding(nvroot, NULL)) { |
10797 | bytes_rem[ZPOOL_WAIT_RESILVER] = | |
10798 | vdev_activity_top_remaining(nvroot); | |
e60e158e JG |
10799 | } |
10800 | ||
2288d419 BB |
10801 | bytes_rem[ZPOOL_WAIT_INITIALIZE] = |
10802 | vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE); | |
10803 | bytes_rem[ZPOOL_WAIT_TRIM] = | |
10804 | vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM); | |
e60e158e JG |
10805 | |
10806 | /* | |
10807 | * A replace finishes after resilvering finishes, so the amount of work | |
10808 | * left for a replace is the same as for resilvering. | |
10809 | * | |
10810 | * It isn't quite correct to say that if we have any 'spare' or | |
10811 | * 'replacing' vdevs and a resilver is happening, then a replace is in | |
10812 | * progress, like we do here. When a hot spare is used, the faulted vdev | |
10813 | * is not removed after the hot spare is resilvered, so parent 'spare' | |
10814 | * vdev is not removed either. So we could have a 'spare' vdev, but be | |
10815 | * resilvering for a different reason. However, we use it as a heuristic | |
10816 | * because we don't have access to the DTLs, which could tell us whether | |
10817 | * or not we have really finished resilvering a hot spare. | |
10818 | */ | |
10819 | if (vdev_any_spare_replacing(nvroot)) | |
10820 | bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER]; | |
10821 | ||
10822 | if (timestamp_fmt != NODATE) | |
10823 | print_timestamp(timestamp_fmt); | |
10824 | ||
10825 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { | |
10826 | char buf[64]; | |
10827 | if (!wd->wd_enabled[i]) | |
10828 | continue; | |
10829 | ||
10830 | if (wd->wd_exact) | |
10831 | (void) snprintf(buf, sizeof (buf), "%" PRIi64, | |
10832 | bytes_rem[i]); | |
10833 | else | |
10834 | zfs_nicenum(bytes_rem[i], buf, sizeof (buf)); | |
10835 | ||
10836 | if (wd->wd_scripted) | |
10837 | (void) printf(i == 0 ? "%s" : "\t%s", buf); | |
10838 | else | |
10839 | (void) printf(" %*s", col_widths[i] - 1, buf); | |
10840 | } | |
10841 | (void) printf("\n"); | |
10842 | (void) fflush(stdout); | |
10843 | } | |
10844 | ||
65c7cc49 | 10845 | static void * |
e60e158e JG |
10846 | wait_status_thread(void *arg) |
10847 | { | |
10848 | wait_data_t *wd = (wait_data_t *)arg; | |
10849 | zpool_handle_t *zhp; | |
10850 | ||
10851 | if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL) | |
10852 | return (void *)(1); | |
10853 | ||
10854 | for (int row = 0; ; row++) { | |
10855 | boolean_t missing; | |
10856 | struct timespec timeout; | |
d553fb9b | 10857 | int ret = 0; |
e60e158e JG |
10858 | (void) clock_gettime(CLOCK_REALTIME, &timeout); |
10859 | ||
10860 | if (zpool_refresh_stats(zhp, &missing) != 0 || missing || | |
10861 | zpool_props_refresh(zhp) != 0) { | |
10862 | zpool_close(zhp); | |
10863 | return (void *)(uintptr_t)(missing ? 0 : 1); | |
10864 | } | |
10865 | ||
10866 | print_wait_status_row(wd, zhp, row); | |
10867 | ||
10868 | timeout.tv_sec += floor(wd->wd_interval); | |
10869 | long nanos = timeout.tv_nsec + | |
10870 | (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC; | |
10871 | if (nanos >= NANOSEC) { | |
10872 | timeout.tv_sec++; | |
10873 | timeout.tv_nsec = nanos - NANOSEC; | |
10874 | } else { | |
10875 | timeout.tv_nsec = nanos; | |
10876 | } | |
d553fb9b JL |
10877 | pthread_mutex_lock(&wd->wd_mutex); |
10878 | if (!wd->wd_should_exit) | |
10879 | ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex, | |
10880 | &timeout); | |
10881 | pthread_mutex_unlock(&wd->wd_mutex); | |
10882 | if (ret == 0) { | |
e60e158e | 10883 | break; /* signaled by main thread */ |
d553fb9b JL |
10884 | } else if (ret != ETIMEDOUT) { |
10885 | (void) fprintf(stderr, gettext("pthread_cond_timedwait " | |
10886 | "failed: %s\n"), strerror(ret)); | |
e60e158e JG |
10887 | zpool_close(zhp); |
10888 | return (void *)(uintptr_t)(1); | |
10889 | } | |
10890 | } | |
10891 | ||
10892 | zpool_close(zhp); | |
10893 | return (void *)(0); | |
10894 | } | |
10895 | ||
10896 | int | |
10897 | zpool_do_wait(int argc, char **argv) | |
10898 | { | |
10899 | boolean_t verbose = B_FALSE; | |
675508f6 | 10900 | int c, i; |
e60e158e JG |
10901 | unsigned long count; |
10902 | pthread_t status_thr; | |
10903 | int error = 0; | |
10904 | zpool_handle_t *zhp; | |
10905 | ||
10906 | wait_data_t wd; | |
10907 | wd.wd_scripted = B_FALSE; | |
10908 | wd.wd_exact = B_FALSE; | |
10909 | wd.wd_headers_once = B_FALSE; | |
d553fb9b | 10910 | wd.wd_should_exit = B_FALSE; |
e60e158e | 10911 | |
d553fb9b JL |
10912 | pthread_mutex_init(&wd.wd_mutex, NULL); |
10913 | pthread_cond_init(&wd.wd_cv, NULL); | |
e60e158e JG |
10914 | |
10915 | /* By default, wait for all types of activity. */ | |
10916 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) | |
10917 | wd.wd_enabled[i] = B_TRUE; | |
10918 | ||
10919 | while ((c = getopt(argc, argv, "HpT:t:")) != -1) { | |
10920 | switch (c) { | |
10921 | case 'H': | |
10922 | wd.wd_scripted = B_TRUE; | |
10923 | break; | |
10924 | case 'n': | |
10925 | wd.wd_headers_once = B_TRUE; | |
10926 | break; | |
10927 | case 'p': | |
10928 | wd.wd_exact = B_TRUE; | |
10929 | break; | |
10930 | case 'T': | |
10931 | get_timestamp_arg(*optarg); | |
10932 | break; | |
10933 | case 't': | |
e60e158e | 10934 | /* Reset activities array */ |
861166b0 | 10935 | memset(&wd.wd_enabled, 0, sizeof (wd.wd_enabled)); |
e60e158e | 10936 | |
675508f6 AZ |
10937 | for (char *tok; (tok = strsep(&optarg, ",")); ) { |
10938 | static const char *const col_opts[] = { | |
10939 | "discard", "free", "initialize", "replace", | |
10940 | "remove", "resilver", "scrub", "trim" }; | |
10941 | ||
10942 | for (i = 0; i < ARRAY_SIZE(col_opts); ++i) | |
10943 | if (strcmp(tok, col_opts[i]) == 0) { | |
10944 | wd.wd_enabled[i] = B_TRUE; | |
10945 | goto found; | |
10946 | } | |
e60e158e | 10947 | |
675508f6 AZ |
10948 | (void) fprintf(stderr, |
10949 | gettext("invalid activity '%s'\n"), tok); | |
10950 | usage(B_FALSE); | |
10951 | found:; | |
e60e158e JG |
10952 | } |
10953 | break; | |
e60e158e JG |
10954 | case '?': |
10955 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
10956 | optopt); | |
10957 | usage(B_FALSE); | |
10958 | } | |
10959 | } | |
10960 | ||
10961 | argc -= optind; | |
10962 | argv += optind; | |
10963 | ||
10964 | get_interval_count(&argc, argv, &wd.wd_interval, &count); | |
10965 | if (count != 0) { | |
10966 | /* This subcmd only accepts an interval, not a count */ | |
10967 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
10968 | usage(B_FALSE); | |
10969 | } | |
10970 | ||
10971 | if (wd.wd_interval != 0) | |
10972 | verbose = B_TRUE; | |
10973 | ||
10974 | if (argc < 1) { | |
10975 | (void) fprintf(stderr, gettext("missing 'pool' argument\n")); | |
10976 | usage(B_FALSE); | |
10977 | } | |
10978 | if (argc > 1) { | |
10979 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
10980 | usage(B_FALSE); | |
10981 | } | |
10982 | ||
10983 | wd.wd_poolname = argv[0]; | |
10984 | ||
10985 | if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL) | |
10986 | return (1); | |
10987 | ||
10988 | if (verbose) { | |
10989 | /* | |
10990 | * We use a separate thread for printing status updates because | |
10991 | * the main thread will call lzc_wait(), which blocks as long | |
10992 | * as an activity is in progress, which can be a long time. | |
10993 | */ | |
10994 | if (pthread_create(&status_thr, NULL, wait_status_thread, &wd) | |
10995 | != 0) { | |
10996 | (void) fprintf(stderr, gettext("failed to create status" | |
10997 | "thread: %s\n"), strerror(errno)); | |
10998 | zpool_close(zhp); | |
10999 | return (1); | |
11000 | } | |
11001 | } | |
11002 | ||
11003 | /* | |
11004 | * Loop over all activities that we are supposed to wait for until none | |
11005 | * of them are in progress. Note that this means we can end up waiting | |
11006 | * for more activities to complete than just those that were in progress | |
11007 | * when we began waiting; if an activity we are interested in begins | |
11008 | * while we are waiting for another activity, we will wait for both to | |
11009 | * complete before exiting. | |
11010 | */ | |
11011 | for (;;) { | |
11012 | boolean_t missing = B_FALSE; | |
11013 | boolean_t any_waited = B_FALSE; | |
11014 | ||
11015 | for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) { | |
11016 | boolean_t waited; | |
11017 | ||
11018 | if (!wd.wd_enabled[i]) | |
11019 | continue; | |
11020 | ||
11021 | error = zpool_wait_status(zhp, i, &missing, &waited); | |
11022 | if (error != 0 || missing) | |
11023 | break; | |
11024 | ||
11025 | any_waited = (any_waited || waited); | |
11026 | } | |
11027 | ||
11028 | if (error != 0 || missing || !any_waited) | |
11029 | break; | |
11030 | } | |
11031 | ||
11032 | zpool_close(zhp); | |
11033 | ||
11034 | if (verbose) { | |
11035 | uintptr_t status; | |
d553fb9b JL |
11036 | pthread_mutex_lock(&wd.wd_mutex); |
11037 | wd.wd_should_exit = B_TRUE; | |
11038 | pthread_cond_signal(&wd.wd_cv); | |
11039 | pthread_mutex_unlock(&wd.wd_mutex); | |
e60e158e JG |
11040 | (void) pthread_join(status_thr, (void *)&status); |
11041 | if (status != 0) | |
11042 | error = status; | |
11043 | } | |
11044 | ||
d553fb9b JL |
11045 | pthread_mutex_destroy(&wd.wd_mutex); |
11046 | pthread_cond_destroy(&wd.wd_cv); | |
e60e158e JG |
11047 | return (error); |
11048 | } | |
34dc7c2f BB |
11049 | |
11050 | static int | |
a926aab9 | 11051 | find_command_idx(const char *command, int *idx) |
34dc7c2f | 11052 | { |
a926aab9 | 11053 | for (int i = 0; i < NCOMMAND; ++i) { |
34dc7c2f BB |
11054 | if (command_table[i].name == NULL) |
11055 | continue; | |
11056 | ||
11057 | if (strcmp(command, command_table[i].name) == 0) { | |
11058 | *idx = i; | |
11059 | return (0); | |
11060 | } | |
11061 | } | |
11062 | return (1); | |
11063 | } | |
11064 | ||
50478c6d T |
11065 | /* |
11066 | * Display version message | |
11067 | */ | |
11068 | static int | |
11069 | zpool_do_version(int argc, char **argv) | |
11070 | { | |
e40ca391 | 11071 | (void) argc, (void) argv; |
2b4f2fc9 | 11072 | return (zfs_version_print() != 0); |
50478c6d T |
11073 | } |
11074 | ||
7228ba11 RN |
11075 | /* Display documentation */ |
11076 | static int | |
11077 | zpool_do_help(int argc, char **argv) | |
11078 | { | |
11079 | char page[MAXNAMELEN]; | |
11080 | if (argc < 3 || strcmp(argv[2], "zpool") == 0) | |
11081 | strcpy(page, "zpool"); | |
11082 | else if (strcmp(argv[2], "concepts") == 0 || | |
11083 | strcmp(argv[2], "props") == 0) | |
11084 | snprintf(page, sizeof (page), "zpool%s", argv[2]); | |
11085 | else | |
11086 | snprintf(page, sizeof (page), "zpool-%s", argv[2]); | |
11087 | ||
11088 | execlp("man", "man", page, NULL); | |
11089 | ||
11090 | fprintf(stderr, "couldn't run man program: %s", strerror(errno)); | |
11091 | return (-1); | |
11092 | } | |
11093 | ||
658fb802 CB |
11094 | /* |
11095 | * Do zpool_load_compat() and print error message on failure | |
11096 | */ | |
11097 | static zpool_compat_status_t | |
11098 | zpool_do_load_compat(const char *compat, boolean_t *list) | |
11099 | { | |
e086db16 CB |
11100 | char report[1024]; |
11101 | ||
658fb802 CB |
11102 | zpool_compat_status_t ret; |
11103 | ||
e086db16 CB |
11104 | ret = zpool_load_compat(compat, list, report, 1024); |
11105 | switch (ret) { | |
11106 | ||
658fb802 CB |
11107 | case ZPOOL_COMPATIBILITY_OK: |
11108 | break; | |
e086db16 CB |
11109 | |
11110 | case ZPOOL_COMPATIBILITY_NOFILES: | |
658fb802 | 11111 | case ZPOOL_COMPATIBILITY_BADFILE: |
e086db16 CB |
11112 | case ZPOOL_COMPATIBILITY_BADTOKEN: |
11113 | (void) fprintf(stderr, "Error: %s\n", report); | |
658fb802 | 11114 | break; |
e086db16 CB |
11115 | |
11116 | case ZPOOL_COMPATIBILITY_WARNTOKEN: | |
11117 | (void) fprintf(stderr, "Warning: %s\n", report); | |
11118 | ret = ZPOOL_COMPATIBILITY_OK; | |
658fb802 CB |
11119 | break; |
11120 | } | |
11121 | return (ret); | |
11122 | } | |
11123 | ||
34dc7c2f BB |
11124 | int |
11125 | main(int argc, char **argv) | |
11126 | { | |
8e896579 | 11127 | int ret = 0; |
d4ed6673 | 11128 | int i = 0; |
34dc7c2f | 11129 | char *cmdname; |
edc05fdb | 11130 | char **newargv; |
34dc7c2f BB |
11131 | |
11132 | (void) setlocale(LC_ALL, ""); | |
c2c7ca0d | 11133 | (void) setlocale(LC_NUMERIC, "C"); |
34dc7c2f | 11134 | (void) textdomain(TEXT_DOMAIN); |
5b4136bd | 11135 | srand(time(NULL)); |
34dc7c2f | 11136 | |
34dc7c2f BB |
11137 | opterr = 0; |
11138 | ||
11139 | /* | |
11140 | * Make sure the user has specified some command. | |
11141 | */ | |
11142 | if (argc < 2) { | |
11143 | (void) fprintf(stderr, gettext("missing command\n")); | |
11144 | usage(B_FALSE); | |
11145 | } | |
11146 | ||
11147 | cmdname = argv[1]; | |
11148 | ||
11149 | /* | |
11150 | * Special case '-?' | |
11151 | */ | |
d1d7e268 | 11152 | if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0) |
34dc7c2f BB |
11153 | usage(B_TRUE); |
11154 | ||
50478c6d T |
11155 | /* |
11156 | * Special case '-V|--version' | |
11157 | */ | |
11158 | if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0)) | |
11159 | return (zpool_do_version(argc, argv)); | |
11160 | ||
7228ba11 RN |
11161 | /* |
11162 | * Special case 'help' | |
11163 | */ | |
11164 | if (strcmp(cmdname, "help") == 0) | |
11165 | return (zpool_do_help(argc, argv)); | |
11166 | ||
65037d9b | 11167 | if ((g_zfs = libzfs_init()) == NULL) { |
afc8f0a6 | 11168 | (void) fprintf(stderr, "%s\n", libzfs_error_init(errno)); |
9b020fd9 | 11169 | return (1); |
65037d9b | 11170 | } |
9b020fd9 BB |
11171 | |
11172 | libzfs_print_on_error(g_zfs, B_TRUE); | |
11173 | ||
6f1ffb06 | 11174 | zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); |
34dc7c2f | 11175 | |
edc05fdb D |
11176 | /* |
11177 | * Many commands modify input strings for string parsing reasons. | |
11178 | * We create a copy to protect the original argv. | |
11179 | */ | |
a3dcc0aa | 11180 | newargv = safe_malloc((argc + 1) * sizeof (newargv[0])); |
edc05fdb D |
11181 | for (i = 0; i < argc; i++) |
11182 | newargv[i] = strdup(argv[i]); | |
11183 | newargv[argc] = NULL; | |
11184 | ||
34dc7c2f BB |
11185 | /* |
11186 | * Run the appropriate command. | |
11187 | */ | |
11188 | if (find_command_idx(cmdname, &i) == 0) { | |
11189 | current_command = &command_table[i]; | |
edc05fdb | 11190 | ret = command_table[i].func(argc - 1, newargv + 1); |
34dc7c2f BB |
11191 | } else if (strchr(cmdname, '=')) { |
11192 | verify(find_command_idx("set", &i) == 0); | |
11193 | current_command = &command_table[i]; | |
edc05fdb | 11194 | ret = command_table[i].func(argc, newargv); |
34dc7c2f BB |
11195 | } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { |
11196 | /* | |
11197 | * 'freeze' is a vile debugging abomination, so we treat | |
11198 | * it as such. | |
11199 | */ | |
4807c0ba TC |
11200 | zfs_cmd_t zc = {"\0"}; |
11201 | ||
11202 | (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); | |
11203 | ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc); | |
11204 | if (ret != 0) { | |
11205 | (void) fprintf(stderr, | |
11206 | gettext("failed to freeze pool: %d\n"), errno); | |
11207 | ret = 1; | |
11208 | } | |
11209 | ||
11210 | log_history = 0; | |
34dc7c2f BB |
11211 | } else { |
11212 | (void) fprintf(stderr, gettext("unrecognized " | |
11213 | "command '%s'\n"), cmdname); | |
11214 | usage(B_FALSE); | |
d4ed6673 | 11215 | ret = 1; |
34dc7c2f BB |
11216 | } |
11217 | ||
edc05fdb D |
11218 | for (i = 0; i < argc; i++) |
11219 | free(newargv[i]); | |
11220 | free(newargv); | |
11221 | ||
6f1ffb06 MA |
11222 | if (ret == 0 && log_history) |
11223 | (void) zpool_log_history(g_zfs, history_str); | |
11224 | ||
34dc7c2f BB |
11225 | libzfs_fini(g_zfs); |
11226 | ||
11227 | /* | |
11228 | * The 'ZFS_ABORT' environment variable causes us to dump core on exit | |
11229 | * for the purposes of running ::findleaks. | |
11230 | */ | |
11231 | if (getenv("ZFS_ABORT") != NULL) { | |
11232 | (void) printf("dumping core by request\n"); | |
11233 | abort(); | |
11234 | } | |
11235 | ||
11236 | return (ret); | |
11237 | } |