]>
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 | |
9 | * or http://www.opensolaris.org/os/licensing. | |
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. |
cc99f275 | 25 | * Copyright (c) 2011, 2018 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. |
34dc7c2f BB |
33 | */ |
34 | ||
34dc7c2f BB |
35 | #include <assert.h> |
36 | #include <ctype.h> | |
37 | #include <dirent.h> | |
38 | #include <errno.h> | |
39 | #include <fcntl.h> | |
d2734cce | 40 | #include <getopt.h> |
34dc7c2f BB |
41 | #include <libgen.h> |
42 | #include <libintl.h> | |
43 | #include <libuutil.h> | |
44 | #include <locale.h> | |
45 | #include <stdio.h> | |
46 | #include <stdlib.h> | |
47 | #include <string.h> | |
48 | #include <strings.h> | |
49 | #include <unistd.h> | |
34dc7c2f BB |
50 | #include <pwd.h> |
51 | #include <zone.h> | |
d6418de0 | 52 | #include <sys/wait.h> |
1bd201e7 | 53 | #include <zfs_prop.h> |
34dc7c2f | 54 | #include <sys/fs/zfs.h> |
34dc7c2f | 55 | #include <sys/stat.h> |
46364cb2 | 56 | #include <sys/systeminfo.h> |
d02ca379 | 57 | #include <sys/fm/fs/zfs.h> |
26685276 BB |
58 | #include <sys/fm/util.h> |
59 | #include <sys/fm/protocol.h> | |
8c7aa0cf | 60 | #include <sys/zfs_ioctl.h> |
b5256303 | 61 | #include <sys/mount.h> |
d4a72f23 TC |
62 | #include <sys/sysmacros.h> |
63 | ||
193a37cb | 64 | #include <math.h> |
34dc7c2f BB |
65 | |
66 | #include <libzfs.h> | |
e89f1295 | 67 | #include <libzutil.h> |
34dc7c2f BB |
68 | |
69 | #include "zpool_util.h" | |
70 | #include "zfs_comutil.h" | |
9ae529ec | 71 | #include "zfeature_common.h" |
34dc7c2f | 72 | |
428870ff BB |
73 | #include "statcommon.h" |
74 | ||
34dc7c2f BB |
75 | static int zpool_do_create(int, char **); |
76 | static int zpool_do_destroy(int, char **); | |
77 | ||
78 | static int zpool_do_add(int, char **); | |
79 | static int zpool_do_remove(int, char **); | |
131cc95c | 80 | static int zpool_do_labelclear(int, char **); |
34dc7c2f | 81 | |
d2734cce SD |
82 | static int zpool_do_checkpoint(int, char **); |
83 | ||
34dc7c2f BB |
84 | static int zpool_do_list(int, char **); |
85 | static int zpool_do_iostat(int, char **); | |
86 | static int zpool_do_status(int, char **); | |
87 | ||
88 | static int zpool_do_online(int, char **); | |
89 | static int zpool_do_offline(int, char **); | |
90 | static int zpool_do_clear(int, char **); | |
1bd201e7 | 91 | static int zpool_do_reopen(int, char **); |
34dc7c2f | 92 | |
3541dc6d GA |
93 | static int zpool_do_reguid(int, char **); |
94 | ||
34dc7c2f BB |
95 | static int zpool_do_attach(int, char **); |
96 | static int zpool_do_detach(int, char **); | |
97 | static int zpool_do_replace(int, char **); | |
428870ff | 98 | static int zpool_do_split(int, char **); |
34dc7c2f | 99 | |
619f0976 | 100 | static int zpool_do_initialize(int, char **); |
34dc7c2f | 101 | static int zpool_do_scrub(int, char **); |
80a91e74 | 102 | static int zpool_do_resilver(int, char **); |
34dc7c2f BB |
103 | |
104 | static int zpool_do_import(int, char **); | |
105 | static int zpool_do_export(int, char **); | |
106 | ||
107 | static int zpool_do_upgrade(int, char **); | |
108 | ||
109 | static int zpool_do_history(int, char **); | |
26685276 | 110 | static int zpool_do_events(int, char **); |
34dc7c2f BB |
111 | |
112 | static int zpool_do_get(int, char **); | |
113 | static int zpool_do_set(int, char **); | |
114 | ||
bec1067d AP |
115 | static int zpool_do_sync(int, char **); |
116 | ||
34dc7c2f BB |
117 | /* |
118 | * These libumem hooks provide a reasonable set of defaults for the allocator's | |
119 | * debugging facilities. | |
120 | */ | |
b128c09f BB |
121 | |
122 | #ifdef DEBUG | |
34dc7c2f BB |
123 | const char * |
124 | _umem_debug_init(void) | |
125 | { | |
126 | return ("default,verbose"); /* $UMEM_DEBUG setting */ | |
127 | } | |
128 | ||
129 | const char * | |
130 | _umem_logging_init(void) | |
131 | { | |
132 | return ("fail,contents"); /* $UMEM_LOGGING setting */ | |
133 | } | |
b128c09f | 134 | #endif |
34dc7c2f BB |
135 | |
136 | typedef enum { | |
137 | HELP_ADD, | |
138 | HELP_ATTACH, | |
139 | HELP_CLEAR, | |
140 | HELP_CREATE, | |
d2734cce | 141 | HELP_CHECKPOINT, |
34dc7c2f BB |
142 | HELP_DESTROY, |
143 | HELP_DETACH, | |
144 | HELP_EXPORT, | |
145 | HELP_HISTORY, | |
146 | HELP_IMPORT, | |
147 | HELP_IOSTAT, | |
131cc95c | 148 | HELP_LABELCLEAR, |
34dc7c2f BB |
149 | HELP_LIST, |
150 | HELP_OFFLINE, | |
151 | HELP_ONLINE, | |
152 | HELP_REPLACE, | |
153 | HELP_REMOVE, | |
619f0976 | 154 | HELP_INITIALIZE, |
34dc7c2f | 155 | HELP_SCRUB, |
80a91e74 | 156 | HELP_RESILVER, |
34dc7c2f BB |
157 | HELP_STATUS, |
158 | HELP_UPGRADE, | |
26685276 | 159 | HELP_EVENTS, |
34dc7c2f | 160 | HELP_GET, |
428870ff | 161 | HELP_SET, |
3541dc6d | 162 | HELP_SPLIT, |
bec1067d | 163 | HELP_SYNC, |
1bd201e7 CS |
164 | HELP_REGUID, |
165 | HELP_REOPEN | |
34dc7c2f BB |
166 | } zpool_help_t; |
167 | ||
168 | ||
193a37cb TH |
169 | /* |
170 | * Flags for stats to display with "zpool iostats" | |
171 | */ | |
172 | enum iostat_type { | |
173 | IOS_DEFAULT = 0, | |
174 | IOS_LATENCY = 1, | |
175 | IOS_QUEUES = 2, | |
176 | IOS_L_HISTO = 3, | |
7e945072 | 177 | IOS_RQ_HISTO = 4, |
193a37cb TH |
178 | IOS_COUNT, /* always last element */ |
179 | }; | |
180 | ||
181 | /* iostat_type entries as bitmasks */ | |
182 | #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT) | |
183 | #define IOS_LATENCY_M (1ULL << IOS_LATENCY) | |
184 | #define IOS_QUEUES_M (1ULL << IOS_QUEUES) | |
185 | #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO) | |
7e945072 TH |
186 | #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO) |
187 | ||
188 | /* Mask of all the histo bits */ | |
189 | #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M) | |
190 | ||
191 | /* | |
192 | * Lookup table for iostat flags to nvlist names. Basically a list | |
193 | * of all the nvlists a flag requires. Also specifies the order in | |
194 | * which data gets printed in zpool iostat. | |
195 | */ | |
196 | static const char *vsx_type_to_nvlist[IOS_COUNT][11] = { | |
197 | [IOS_L_HISTO] = { | |
198 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
199 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
200 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
201 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
202 | ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, | |
203 | ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, | |
204 | ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, | |
205 | ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, | |
206 | ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, | |
207 | NULL}, | |
208 | [IOS_LATENCY] = { | |
209 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
210 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
211 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
212 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
213 | NULL}, | |
214 | [IOS_QUEUES] = { | |
215 | ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, | |
216 | ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, | |
217 | ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, | |
218 | ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, | |
219 | ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, | |
220 | NULL}, | |
221 | [IOS_RQ_HISTO] = { | |
222 | ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO, | |
223 | ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO, | |
224 | ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO, | |
225 | ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO, | |
226 | ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO, | |
227 | ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO, | |
228 | ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO, | |
229 | ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO, | |
230 | ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO, | |
231 | ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO, | |
232 | NULL}, | |
233 | }; | |
234 | ||
235 | ||
236 | /* | |
237 | * Given a cb->cb_flags with a histogram bit set, return the iostat_type. | |
238 | * Right now, only one histo bit is ever set at one time, so we can | |
239 | * just do a highbit64(a) | |
240 | */ | |
241 | #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1) | |
193a37cb | 242 | |
34dc7c2f BB |
243 | typedef struct zpool_command { |
244 | const char *name; | |
245 | int (*func)(int, char **); | |
246 | zpool_help_t usage; | |
247 | } zpool_command_t; | |
248 | ||
249 | /* | |
250 | * Master command table. Each ZFS command has a name, associated function, and | |
251 | * usage message. The usage messages need to be internationalized, so we have | |
252 | * to have a function to return the usage message based on a command index. | |
253 | * | |
254 | * These commands are organized according to how they are displayed in the usage | |
255 | * message. An empty command (one with a NULL name) indicates an empty line in | |
256 | * the generic usage message. | |
257 | */ | |
258 | static zpool_command_t command_table[] = { | |
259 | { "create", zpool_do_create, HELP_CREATE }, | |
260 | { "destroy", zpool_do_destroy, HELP_DESTROY }, | |
261 | { NULL }, | |
262 | { "add", zpool_do_add, HELP_ADD }, | |
263 | { "remove", zpool_do_remove, HELP_REMOVE }, | |
264 | { NULL }, | |
131cc95c DK |
265 | { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR }, |
266 | { NULL }, | |
d2734cce SD |
267 | { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT }, |
268 | { NULL }, | |
34dc7c2f BB |
269 | { "list", zpool_do_list, HELP_LIST }, |
270 | { "iostat", zpool_do_iostat, HELP_IOSTAT }, | |
271 | { "status", zpool_do_status, HELP_STATUS }, | |
272 | { NULL }, | |
273 | { "online", zpool_do_online, HELP_ONLINE }, | |
274 | { "offline", zpool_do_offline, HELP_OFFLINE }, | |
275 | { "clear", zpool_do_clear, HELP_CLEAR }, | |
1bd201e7 | 276 | { "reopen", zpool_do_reopen, HELP_REOPEN }, |
34dc7c2f BB |
277 | { NULL }, |
278 | { "attach", zpool_do_attach, HELP_ATTACH }, | |
279 | { "detach", zpool_do_detach, HELP_DETACH }, | |
280 | { "replace", zpool_do_replace, HELP_REPLACE }, | |
428870ff | 281 | { "split", zpool_do_split, HELP_SPLIT }, |
34dc7c2f | 282 | { NULL }, |
619f0976 | 283 | { "initialize", zpool_do_initialize, HELP_INITIALIZE }, |
34dc7c2f | 284 | { "scrub", zpool_do_scrub, HELP_SCRUB }, |
80a91e74 | 285 | { "resilver", zpool_do_resilver, HELP_RESILVER }, |
34dc7c2f BB |
286 | { NULL }, |
287 | { "import", zpool_do_import, HELP_IMPORT }, | |
288 | { "export", zpool_do_export, HELP_EXPORT }, | |
289 | { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, | |
3541dc6d | 290 | { "reguid", zpool_do_reguid, HELP_REGUID }, |
34dc7c2f BB |
291 | { NULL }, |
292 | { "history", zpool_do_history, HELP_HISTORY }, | |
26685276 BB |
293 | { "events", zpool_do_events, HELP_EVENTS }, |
294 | { NULL }, | |
34dc7c2f BB |
295 | { "get", zpool_do_get, HELP_GET }, |
296 | { "set", zpool_do_set, HELP_SET }, | |
bec1067d | 297 | { "sync", zpool_do_sync, HELP_SYNC }, |
34dc7c2f BB |
298 | }; |
299 | ||
193a37cb | 300 | #define NCOMMAND (ARRAY_SIZE(command_table)) |
34dc7c2f | 301 | |
cc99f275 DB |
302 | #define VDEV_ALLOC_CLASS_LOGS "logs" |
303 | ||
6f1ffb06 | 304 | static zpool_command_t *current_command; |
34dc7c2f | 305 | static char history_str[HIS_MAX_RECORD_LEN]; |
6f1ffb06 | 306 | static boolean_t log_history = B_TRUE; |
428870ff BB |
307 | static uint_t timestamp_fmt = NODATE; |
308 | ||
34dc7c2f | 309 | static const char * |
e9aa730c GM |
310 | get_usage(zpool_help_t idx) |
311 | { | |
34dc7c2f BB |
312 | switch (idx) { |
313 | case HELP_ADD: | |
a77f29f9 | 314 | return (gettext("\tadd [-fgLnP] [-o property=value] " |
df831108 | 315 | "<pool> <vdev> ...\n")); |
34dc7c2f | 316 | case HELP_ATTACH: |
df831108 CP |
317 | return (gettext("\tattach [-f] [-o property=value] " |
318 | "<pool> <device> <new-device>\n")); | |
34dc7c2f | 319 | case HELP_CLEAR: |
428870ff | 320 | return (gettext("\tclear [-nF] <pool> [device]\n")); |
34dc7c2f | 321 | case HELP_CREATE: |
9ae529ec | 322 | return (gettext("\tcreate [-fnd] [-o property=value] ... \n" |
b128c09f | 323 | "\t [-O file-system-property=value] ... \n" |
34dc7c2f | 324 | "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); |
d2734cce SD |
325 | case HELP_CHECKPOINT: |
326 | return (gettext("\tcheckpoint [--discard] <pool> ...\n")); | |
34dc7c2f BB |
327 | case HELP_DESTROY: |
328 | return (gettext("\tdestroy [-f] <pool>\n")); | |
329 | case HELP_DETACH: | |
330 | return (gettext("\tdetach <pool> <device>\n")); | |
331 | case HELP_EXPORT: | |
859735c0 | 332 | return (gettext("\texport [-af] <pool> ...\n")); |
34dc7c2f BB |
333 | case HELP_HISTORY: |
334 | return (gettext("\thistory [-il] [<pool>] ...\n")); | |
335 | case HELP_IMPORT: | |
336 | return (gettext("\timport [-d dir] [-D]\n" | |
337 | "\timport [-o mntopts] [-o property=value] ... \n" | |
b5256303 | 338 | "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " |
572e2857 | 339 | "[-R root] [-F [-n]] -a\n" |
34dc7c2f | 340 | "\timport [-o mntopts] [-o property=value] ... \n" |
b5256303 | 341 | "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] " |
572e2857 | 342 | "[-R root] [-F [-n]]\n" |
d2734cce | 343 | "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n")); |
34dc7c2f | 344 | case HELP_IOSTAT: |
d6418de0 TH |
345 | return (gettext("\tiostat [[[-c [script1,script2,...]" |
346 | "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n" | |
347 | "\t [[pool ...]|[pool vdev ...]|[vdev ...]]" | |
348 | " [interval [count]]\n")); | |
131cc95c DK |
349 | case HELP_LABELCLEAR: |
350 | return (gettext("\tlabelclear [-f] <vdev>\n")); | |
34dc7c2f | 351 | case HELP_LIST: |
2a8b84b7 | 352 | return (gettext("\tlist [-gHLpPv] [-o property[,...]] " |
63f88c12 | 353 | "[-T d|u] [pool] ... \n" |
354 | "\t [interval [count]]\n")); | |
34dc7c2f | 355 | case HELP_OFFLINE: |
4a283c7f | 356 | return (gettext("\toffline [-f] [-t] <pool> <device> ...\n")); |
34dc7c2f | 357 | case HELP_ONLINE: |
aebfb848 | 358 | return (gettext("\tonline [-e] <pool> <device> ...\n")); |
34dc7c2f | 359 | case HELP_REPLACE: |
628668a3 TF |
360 | return (gettext("\treplace [-f] [-o property=value] " |
361 | "<pool> <device> [new-device]\n")); | |
34dc7c2f | 362 | case HELP_REMOVE: |
a1d477c2 | 363 | return (gettext("\tremove [-nps] <pool> <device> ...\n")); |
1bd201e7 | 364 | case HELP_REOPEN: |
d3f2cd7e | 365 | return (gettext("\treopen [-n] <pool>\n")); |
619f0976 GW |
366 | case HELP_INITIALIZE: |
367 | return (gettext("\tinitialize [-cs] <pool> [<device> ...]\n")); | |
34dc7c2f | 368 | case HELP_SCRUB: |
0ea05c64 | 369 | return (gettext("\tscrub [-s | -p] <pool> ...\n")); |
80a91e74 TC |
370 | case HELP_RESILVER: |
371 | return (gettext("\tresilver <pool> ...\n")); | |
34dc7c2f | 372 | case HELP_STATUS: |
ad796b8a TH |
373 | return (gettext("\tstatus [-c [script1,script2,...]] " |
374 | "[-gLpPsvxD] [-T d|u] [pool] ... \n" | |
63f88c12 | 375 | "\t [interval [count]]\n")); |
34dc7c2f BB |
376 | case HELP_UPGRADE: |
377 | return (gettext("\tupgrade\n" | |
378 | "\tupgrade -v\n" | |
379 | "\tupgrade [-V version] <-a | pool ...>\n")); | |
26685276 | 380 | case HELP_EVENTS: |
88f9c939 | 381 | return (gettext("\tevents [-vHf [pool] | -c]\n")); |
34dc7c2f | 382 | case HELP_GET: |
2a8b84b7 AS |
383 | return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] " |
384 | "<\"all\" | property[,...]> <pool> ...\n")); | |
34dc7c2f BB |
385 | case HELP_SET: |
386 | return (gettext("\tset <property=value> <pool> \n")); | |
428870ff | 387 | case HELP_SPLIT: |
b5256303 | 388 | return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n" |
428870ff BB |
389 | "\t [-o property=value] <pool> <newpool> " |
390 | "[<device> ...]\n")); | |
3541dc6d GA |
391 | case HELP_REGUID: |
392 | return (gettext("\treguid <pool>\n")); | |
bec1067d AP |
393 | case HELP_SYNC: |
394 | return (gettext("\tsync [pool] ...\n")); | |
34dc7c2f BB |
395 | } |
396 | ||
397 | abort(); | |
398 | /* NOTREACHED */ | |
399 | } | |
400 | ||
619f0976 GW |
401 | static void |
402 | zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res) | |
403 | { | |
404 | uint_t children = 0; | |
405 | nvlist_t **child; | |
406 | uint_t i; | |
407 | ||
408 | (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
409 | &child, &children); | |
410 | ||
411 | if (children == 0) { | |
412 | char *path = zpool_vdev_name(g_zfs, zhp, nvroot, B_FALSE); | |
413 | fnvlist_add_boolean(res, path); | |
414 | free(path); | |
415 | return; | |
416 | } | |
417 | ||
418 | for (i = 0; i < children; i++) { | |
419 | zpool_collect_leaves(zhp, child[i], res); | |
420 | } | |
421 | } | |
34dc7c2f BB |
422 | |
423 | /* | |
424 | * Callback routine that will print out a pool property value. | |
425 | */ | |
426 | static int | |
427 | print_prop_cb(int prop, void *cb) | |
428 | { | |
429 | FILE *fp = cb; | |
430 | ||
cc99f275 | 431 | (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop)); |
34dc7c2f BB |
432 | |
433 | if (zpool_prop_readonly(prop)) | |
434 | (void) fprintf(fp, " NO "); | |
435 | else | |
428870ff | 436 | (void) fprintf(fp, " YES "); |
34dc7c2f BB |
437 | |
438 | if (zpool_prop_values(prop) == NULL) | |
439 | (void) fprintf(fp, "-\n"); | |
440 | else | |
441 | (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); | |
442 | ||
443 | return (ZPROP_CONT); | |
444 | } | |
445 | ||
446 | /* | |
447 | * Display usage message. If we're inside a command, display only the usage for | |
448 | * that command. Otherwise, iterate over the entire command table and display | |
449 | * a complete usage message. | |
450 | */ | |
451 | void | |
452 | usage(boolean_t requested) | |
453 | { | |
454 | FILE *fp = requested ? stdout : stderr; | |
455 | ||
456 | if (current_command == NULL) { | |
457 | int i; | |
458 | ||
459 | (void) fprintf(fp, gettext("usage: zpool command args ...\n")); | |
460 | (void) fprintf(fp, | |
461 | gettext("where 'command' is one of the following:\n\n")); | |
462 | ||
463 | for (i = 0; i < NCOMMAND; i++) { | |
464 | if (command_table[i].name == NULL) | |
465 | (void) fprintf(fp, "\n"); | |
466 | else | |
467 | (void) fprintf(fp, "%s", | |
468 | get_usage(command_table[i].usage)); | |
469 | } | |
470 | } else { | |
471 | (void) fprintf(fp, gettext("usage:\n")); | |
472 | (void) fprintf(fp, "%s", get_usage(current_command->usage)); | |
473 | } | |
474 | ||
475 | if (current_command != NULL && | |
476 | ((strcmp(current_command->name, "set") == 0) || | |
477 | (strcmp(current_command->name, "get") == 0) || | |
478 | (strcmp(current_command->name, "list") == 0))) { | |
479 | ||
480 | (void) fprintf(fp, | |
481 | gettext("\nthe following properties are supported:\n")); | |
482 | ||
cc99f275 | 483 | (void) fprintf(fp, "\n\t%-19s %s %s\n\n", |
34dc7c2f BB |
484 | "PROPERTY", "EDIT", "VALUES"); |
485 | ||
486 | /* Iterate over all properties */ | |
487 | (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE, | |
488 | ZFS_TYPE_POOL); | |
9ae529ec | 489 | |
cc99f275 | 490 | (void) fprintf(fp, "\t%-19s ", "feature@..."); |
9ae529ec CS |
491 | (void) fprintf(fp, "YES disabled | enabled | active\n"); |
492 | ||
493 | (void) fprintf(fp, gettext("\nThe feature@ properties must be " | |
494 | "appended with a feature name.\nSee zpool-features(5).\n")); | |
34dc7c2f BB |
495 | } |
496 | ||
497 | /* | |
498 | * See comments at end of main(). | |
499 | */ | |
500 | if (getenv("ZFS_ABORT") != NULL) { | |
501 | (void) printf("dumping core by request\n"); | |
502 | abort(); | |
503 | } | |
504 | ||
505 | exit(requested ? 0 : 2); | |
506 | } | |
507 | ||
619f0976 GW |
508 | /* |
509 | * zpool initialize [-cs] <pool> [<vdev> ...] | |
510 | * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool | |
511 | * if none specified. | |
512 | * | |
513 | * -c Cancel. Ends active initializing. | |
514 | * -s Suspend. Initializing can then be restarted with no flags. | |
515 | */ | |
516 | int | |
517 | zpool_do_initialize(int argc, char **argv) | |
518 | { | |
519 | int c; | |
520 | char *poolname; | |
521 | zpool_handle_t *zhp; | |
522 | nvlist_t *vdevs; | |
523 | int err = 0; | |
524 | ||
525 | struct option long_options[] = { | |
526 | {"cancel", no_argument, NULL, 'c'}, | |
527 | {"suspend", no_argument, NULL, 's'}, | |
528 | {0, 0, 0, 0} | |
529 | }; | |
530 | ||
531 | pool_initialize_func_t cmd_type = POOL_INITIALIZE_DO; | |
532 | while ((c = getopt_long(argc, argv, "cs", long_options, NULL)) != -1) { | |
533 | switch (c) { | |
534 | case 'c': | |
535 | if (cmd_type != POOL_INITIALIZE_DO) { | |
536 | (void) fprintf(stderr, gettext("-c cannot be " | |
537 | "combined with other options\n")); | |
538 | usage(B_FALSE); | |
539 | } | |
540 | cmd_type = POOL_INITIALIZE_CANCEL; | |
541 | break; | |
542 | case 's': | |
543 | if (cmd_type != POOL_INITIALIZE_DO) { | |
544 | (void) fprintf(stderr, gettext("-s cannot be " | |
545 | "combined with other options\n")); | |
546 | usage(B_FALSE); | |
547 | } | |
548 | cmd_type = POOL_INITIALIZE_SUSPEND; | |
549 | break; | |
550 | case '?': | |
551 | if (optopt != 0) { | |
552 | (void) fprintf(stderr, | |
553 | gettext("invalid option '%c'\n"), optopt); | |
554 | } else { | |
555 | (void) fprintf(stderr, | |
556 | gettext("invalid option '%s'\n"), | |
557 | argv[optind - 1]); | |
558 | } | |
559 | usage(B_FALSE); | |
560 | } | |
561 | } | |
562 | ||
563 | argc -= optind; | |
564 | argv += optind; | |
565 | ||
566 | if (argc < 1) { | |
567 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
568 | usage(B_FALSE); | |
569 | return (-1); | |
570 | } | |
571 | ||
572 | poolname = argv[0]; | |
573 | zhp = zpool_open(g_zfs, poolname); | |
574 | if (zhp == NULL) | |
575 | return (-1); | |
576 | ||
577 | vdevs = fnvlist_alloc(); | |
578 | if (argc == 1) { | |
579 | /* no individual leaf vdevs specified, so add them all */ | |
580 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
581 | nvlist_t *nvroot = fnvlist_lookup_nvlist(config, | |
582 | ZPOOL_CONFIG_VDEV_TREE); | |
583 | zpool_collect_leaves(zhp, nvroot, vdevs); | |
584 | } else { | |
585 | int i; | |
586 | for (i = 1; i < argc; i++) { | |
587 | fnvlist_add_boolean(vdevs, argv[i]); | |
588 | } | |
589 | } | |
590 | ||
591 | err = zpool_initialize(zhp, cmd_type, vdevs); | |
592 | ||
593 | fnvlist_free(vdevs); | |
594 | zpool_close(zhp); | |
595 | ||
596 | return (err); | |
597 | } | |
598 | ||
cc99f275 DB |
599 | /* |
600 | * print a pool vdev config for dry runs | |
601 | */ | |
602 | static void | |
34dc7c2f | 603 | print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, |
cc99f275 | 604 | const char *match, int name_flags) |
34dc7c2f BB |
605 | { |
606 | nvlist_t **child; | |
607 | uint_t c, children; | |
608 | char *vname; | |
cc99f275 | 609 | boolean_t printed = B_FALSE; |
34dc7c2f BB |
610 | |
611 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
cc99f275 DB |
612 | &child, &children) != 0) { |
613 | if (name != NULL) | |
614 | (void) printf("\t%*s%s\n", indent, "", name); | |
34dc7c2f | 615 | return; |
cc99f275 | 616 | } |
34dc7c2f BB |
617 | |
618 | for (c = 0; c < children; c++) { | |
619 | uint64_t is_log = B_FALSE; | |
cc99f275 | 620 | char *class = ""; |
34dc7c2f BB |
621 | |
622 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
623 | &is_log); | |
cc99f275 DB |
624 | if (is_log) |
625 | class = VDEV_ALLOC_BIAS_LOG; | |
626 | (void) nvlist_lookup_string(child[c], | |
627 | ZPOOL_CONFIG_ALLOCATION_BIAS, &class); | |
628 | if (strcmp(match, class) != 0) | |
34dc7c2f BB |
629 | continue; |
630 | ||
cc99f275 DB |
631 | if (!printed && name != NULL) { |
632 | (void) printf("\t%*s%s\n", indent, "", name); | |
633 | printed = B_TRUE; | |
634 | } | |
d2f3e292 | 635 | vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags); |
cc99f275 DB |
636 | print_vdev_tree(zhp, vname, child[c], indent + 2, "", |
637 | name_flags); | |
34dc7c2f BB |
638 | free(vname); |
639 | } | |
640 | } | |
641 | ||
b9b24bb4 CS |
642 | static boolean_t |
643 | prop_list_contains_feature(nvlist_t *proplist) | |
644 | { | |
645 | nvpair_t *nvp; | |
646 | for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; | |
647 | nvp = nvlist_next_nvpair(proplist, nvp)) { | |
648 | if (zpool_prop_feature(nvpair_name(nvp))) | |
649 | return (B_TRUE); | |
650 | } | |
651 | return (B_FALSE); | |
652 | } | |
653 | ||
34dc7c2f BB |
654 | /* |
655 | * Add a property pair (name, string-value) into a property nvlist. | |
656 | */ | |
657 | static int | |
b128c09f BB |
658 | add_prop_list(const char *propname, char *propval, nvlist_t **props, |
659 | boolean_t poolprop) | |
34dc7c2f | 660 | { |
31864e3d | 661 | zpool_prop_t prop = ZPOOL_PROP_INVAL; |
34dc7c2f | 662 | nvlist_t *proplist; |
b128c09f BB |
663 | const char *normnm; |
664 | char *strval; | |
34dc7c2f BB |
665 | |
666 | if (*props == NULL && | |
667 | nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { | |
668 | (void) fprintf(stderr, | |
669 | gettext("internal error: out of memory\n")); | |
670 | return (1); | |
671 | } | |
672 | ||
673 | proplist = *props; | |
674 | ||
b128c09f | 675 | if (poolprop) { |
b9b24bb4 CS |
676 | const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); |
677 | ||
31864e3d | 678 | if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL && |
9ae529ec | 679 | !zpool_prop_feature(propname)) { |
b128c09f BB |
680 | (void) fprintf(stderr, gettext("property '%s' is " |
681 | "not a valid pool property\n"), propname); | |
682 | return (2); | |
683 | } | |
b9b24bb4 CS |
684 | |
685 | /* | |
686 | * feature@ properties and version should not be specified | |
687 | * at the same time. | |
688 | */ | |
31864e3d | 689 | if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) && |
b9b24bb4 CS |
690 | nvlist_exists(proplist, vname)) || |
691 | (prop == ZPOOL_PROP_VERSION && | |
692 | prop_list_contains_feature(proplist))) { | |
693 | (void) fprintf(stderr, gettext("'feature@' and " | |
694 | "'version' properties cannot be specified " | |
695 | "together\n")); | |
696 | return (2); | |
697 | } | |
698 | ||
699 | ||
9ae529ec CS |
700 | if (zpool_prop_feature(propname)) |
701 | normnm = propname; | |
702 | else | |
703 | normnm = zpool_prop_to_name(prop); | |
b128c09f | 704 | } else { |
5140a58f | 705 | zfs_prop_t fsprop = zfs_name_to_prop(propname); |
706 | ||
707 | if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM, | |
708 | B_FALSE)) { | |
709 | normnm = zfs_prop_to_name(fsprop); | |
710 | } else if (zfs_prop_user(propname) || | |
711 | zfs_prop_userquota(propname)) { | |
9babb374 | 712 | normnm = propname; |
5140a58f | 713 | } else { |
714 | (void) fprintf(stderr, gettext("property '%s' is " | |
715 | "not a valid filesystem property\n"), propname); | |
716 | return (2); | |
b128c09f | 717 | } |
34dc7c2f BB |
718 | } |
719 | ||
b128c09f BB |
720 | if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && |
721 | prop != ZPOOL_PROP_CACHEFILE) { | |
34dc7c2f BB |
722 | (void) fprintf(stderr, gettext("property '%s' " |
723 | "specified multiple times\n"), propname); | |
724 | return (2); | |
725 | } | |
726 | ||
b128c09f | 727 | if (nvlist_add_string(proplist, normnm, propval) != 0) { |
34dc7c2f BB |
728 | (void) fprintf(stderr, gettext("internal " |
729 | "error: out of memory\n")); | |
730 | return (1); | |
731 | } | |
732 | ||
733 | return (0); | |
734 | } | |
735 | ||
2f3ec900 RY |
736 | /* |
737 | * Set a default property pair (name, string-value) in a property nvlist | |
738 | */ | |
739 | static int | |
740 | add_prop_list_default(const char *propname, char *propval, nvlist_t **props, | |
741 | boolean_t poolprop) | |
742 | { | |
743 | char *pval; | |
744 | ||
745 | if (nvlist_lookup_string(*props, propname, &pval) == 0) | |
746 | return (0); | |
747 | ||
748 | return (add_prop_list(propname, propval, props, B_TRUE)); | |
749 | } | |
750 | ||
34dc7c2f | 751 | /* |
a77f29f9 | 752 | * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ... |
34dc7c2f BB |
753 | * |
754 | * -f Force addition of devices, even if they appear in use | |
d2f3e292 RY |
755 | * -g Display guid for individual vdev name. |
756 | * -L Follow links when resolving vdev path name. | |
34dc7c2f BB |
757 | * -n Do not add the devices, but display the resulting layout if |
758 | * they were to be added. | |
df831108 | 759 | * -o Set property=value. |
a77f29f9 | 760 | * -P Display full path for vdev name. |
34dc7c2f BB |
761 | * |
762 | * Adds the given vdevs to 'pool'. As with create, the bulk of this work is | |
763 | * handled by get_vdev_spec(), which constructs the nvlist needed to pass to | |
764 | * libzfs. | |
765 | */ | |
766 | int | |
767 | zpool_do_add(int argc, char **argv) | |
768 | { | |
769 | boolean_t force = B_FALSE; | |
770 | boolean_t dryrun = B_FALSE; | |
d2f3e292 | 771 | int name_flags = 0; |
34dc7c2f BB |
772 | int c; |
773 | nvlist_t *nvroot; | |
774 | char *poolname; | |
775 | int ret; | |
776 | zpool_handle_t *zhp; | |
777 | nvlist_t *config; | |
df831108 CP |
778 | nvlist_t *props = NULL; |
779 | char *propval; | |
34dc7c2f BB |
780 | |
781 | /* check options */ | |
a77f29f9 | 782 | while ((c = getopt(argc, argv, "fgLno:P")) != -1) { |
34dc7c2f BB |
783 | switch (c) { |
784 | case 'f': | |
785 | force = B_TRUE; | |
786 | break; | |
d2f3e292 RY |
787 | case 'g': |
788 | name_flags |= VDEV_NAME_GUID; | |
789 | break; | |
790 | case 'L': | |
791 | name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
792 | break; | |
34dc7c2f BB |
793 | case 'n': |
794 | dryrun = B_TRUE; | |
795 | break; | |
df831108 CP |
796 | case 'o': |
797 | if ((propval = strchr(optarg, '=')) == NULL) { | |
798 | (void) fprintf(stderr, gettext("missing " | |
799 | "'=' for -o option\n")); | |
800 | usage(B_FALSE); | |
801 | } | |
802 | *propval = '\0'; | |
803 | propval++; | |
804 | ||
805 | if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || | |
806 | (add_prop_list(optarg, propval, &props, B_TRUE))) | |
807 | usage(B_FALSE); | |
808 | break; | |
a77f29f9 | 809 | case 'P': |
d2f3e292 RY |
810 | name_flags |= VDEV_NAME_PATH; |
811 | break; | |
34dc7c2f BB |
812 | case '?': |
813 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
814 | optopt); | |
815 | usage(B_FALSE); | |
816 | } | |
817 | } | |
818 | ||
819 | argc -= optind; | |
820 | argv += optind; | |
821 | ||
822 | /* get pool name and check number of arguments */ | |
823 | if (argc < 1) { | |
824 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
825 | usage(B_FALSE); | |
826 | } | |
827 | if (argc < 2) { | |
828 | (void) fprintf(stderr, gettext("missing vdev specification\n")); | |
829 | usage(B_FALSE); | |
830 | } | |
831 | ||
832 | poolname = argv[0]; | |
833 | ||
834 | argc--; | |
835 | argv++; | |
836 | ||
837 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
838 | return (1); | |
839 | ||
840 | if ((config = zpool_get_config(zhp, NULL)) == NULL) { | |
841 | (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), | |
842 | poolname); | |
843 | zpool_close(zhp); | |
844 | return (1); | |
845 | } | |
846 | ||
dddef7d6 | 847 | /* unless manually specified use "ashift" pool property (if set) */ |
848 | if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { | |
849 | int intval; | |
850 | zprop_source_t src; | |
851 | char strval[ZPOOL_MAXPROPLEN]; | |
852 | ||
853 | intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); | |
854 | if (src != ZPROP_SRC_DEFAULT) { | |
855 | (void) sprintf(strval, "%" PRId32, intval); | |
856 | verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, | |
857 | &props, B_TRUE) == 0); | |
858 | } | |
859 | } | |
860 | ||
34dc7c2f | 861 | /* pass off to get_vdev_spec for processing */ |
df831108 | 862 | nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun, |
b128c09f | 863 | argc, argv); |
34dc7c2f BB |
864 | if (nvroot == NULL) { |
865 | zpool_close(zhp); | |
866 | return (1); | |
867 | } | |
868 | ||
869 | if (dryrun) { | |
870 | nvlist_t *poolnvroot; | |
e02b533e TC |
871 | nvlist_t **l2child; |
872 | uint_t l2children, c; | |
873 | char *vname; | |
874 | boolean_t hadcache = B_FALSE; | |
34dc7c2f BB |
875 | |
876 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
877 | &poolnvroot) == 0); | |
878 | ||
879 | (void) printf(gettext("would update '%s' to the following " | |
880 | "configuration:\n"), zpool_get_name(zhp)); | |
881 | ||
882 | /* print original main pool and new tree */ | |
cc99f275 DB |
883 | print_vdev_tree(zhp, poolname, poolnvroot, 0, "", |
884 | name_flags | VDEV_NAME_TYPE_ID); | |
885 | print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags); | |
886 | ||
887 | /* print other classes: 'dedup', 'special', and 'log' */ | |
888 | print_vdev_tree(zhp, "dedup", poolnvroot, 0, | |
889 | VDEV_ALLOC_BIAS_DEDUP, name_flags); | |
890 | print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_DEDUP, | |
891 | name_flags); | |
892 | ||
893 | print_vdev_tree(zhp, "special", poolnvroot, 0, | |
894 | VDEV_ALLOC_BIAS_SPECIAL, name_flags); | |
895 | print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_SPECIAL, | |
896 | name_flags); | |
897 | ||
898 | print_vdev_tree(zhp, "logs", poolnvroot, 0, VDEV_ALLOC_BIAS_LOG, | |
899 | name_flags); | |
900 | print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_LOG, | |
d2f3e292 | 901 | name_flags); |
34dc7c2f | 902 | |
e02b533e TC |
903 | /* Do the same for the caches */ |
904 | if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE, | |
905 | &l2child, &l2children) == 0 && l2children) { | |
906 | hadcache = B_TRUE; | |
907 | (void) printf(gettext("\tcache\n")); | |
908 | for (c = 0; c < l2children; c++) { | |
909 | vname = zpool_vdev_name(g_zfs, NULL, | |
d2f3e292 | 910 | l2child[c], name_flags); |
e02b533e TC |
911 | (void) printf("\t %s\n", vname); |
912 | free(vname); | |
913 | } | |
914 | } | |
915 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
916 | &l2child, &l2children) == 0 && l2children) { | |
917 | if (!hadcache) | |
918 | (void) printf(gettext("\tcache\n")); | |
919 | for (c = 0; c < l2children; c++) { | |
920 | vname = zpool_vdev_name(g_zfs, NULL, | |
d2f3e292 | 921 | l2child[c], name_flags); |
e02b533e TC |
922 | (void) printf("\t %s\n", vname); |
923 | free(vname); | |
924 | } | |
925 | } | |
926 | ||
34dc7c2f BB |
927 | ret = 0; |
928 | } else { | |
929 | ret = (zpool_add(zhp, nvroot) != 0); | |
930 | } | |
931 | ||
df831108 | 932 | nvlist_free(props); |
34dc7c2f BB |
933 | nvlist_free(nvroot); |
934 | zpool_close(zhp); | |
935 | ||
936 | return (ret); | |
937 | } | |
938 | ||
939 | /* | |
428870ff | 940 | * zpool remove <pool> <vdev> ... |
34dc7c2f | 941 | * |
a1d477c2 | 942 | * Removes the given vdev from the pool. |
34dc7c2f BB |
943 | */ |
944 | int | |
945 | zpool_do_remove(int argc, char **argv) | |
946 | { | |
947 | char *poolname; | |
948 | int i, ret = 0; | |
884385a0 | 949 | zpool_handle_t *zhp = NULL; |
a1d477c2 MA |
950 | boolean_t stop = B_FALSE; |
951 | char c; | |
952 | boolean_t noop = B_FALSE; | |
953 | boolean_t parsable = B_FALSE; | |
34dc7c2f | 954 | |
a1d477c2 MA |
955 | /* check options */ |
956 | while ((c = getopt(argc, argv, "nps")) != -1) { | |
957 | switch (c) { | |
958 | case 'n': | |
959 | noop = B_TRUE; | |
960 | break; | |
961 | case 'p': | |
962 | parsable = B_TRUE; | |
963 | break; | |
964 | case 's': | |
965 | stop = B_TRUE; | |
966 | break; | |
967 | case '?': | |
968 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
969 | optopt); | |
970 | usage(B_FALSE); | |
971 | } | |
972 | } | |
973 | ||
974 | argc -= optind; | |
975 | argv += optind; | |
34dc7c2f BB |
976 | |
977 | /* get pool name and check number of arguments */ | |
978 | if (argc < 1) { | |
979 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
980 | usage(B_FALSE); | |
981 | } | |
34dc7c2f BB |
982 | |
983 | poolname = argv[0]; | |
984 | ||
985 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
986 | return (1); | |
987 | ||
a1d477c2 MA |
988 | if (stop && noop) { |
989 | (void) fprintf(stderr, gettext("stop request ignored\n")); | |
990 | return (0); | |
991 | } | |
992 | ||
993 | if (stop) { | |
994 | if (argc > 1) { | |
995 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
996 | usage(B_FALSE); | |
997 | } | |
998 | if (zpool_vdev_remove_cancel(zhp) != 0) | |
34dc7c2f | 999 | ret = 1; |
a1d477c2 MA |
1000 | } else { |
1001 | if (argc < 2) { | |
1002 | (void) fprintf(stderr, gettext("missing device\n")); | |
1003 | usage(B_FALSE); | |
1004 | } | |
1005 | ||
1006 | for (i = 1; i < argc; i++) { | |
1007 | if (noop) { | |
1008 | uint64_t size; | |
1009 | ||
1010 | if (zpool_vdev_indirect_size(zhp, argv[i], | |
1011 | &size) != 0) { | |
1012 | ret = 1; | |
1013 | break; | |
1014 | } | |
1015 | if (parsable) { | |
1016 | (void) printf("%s %llu\n", | |
1017 | argv[i], (unsigned long long)size); | |
1018 | } else { | |
1019 | char valstr[32]; | |
1020 | zfs_nicenum(size, valstr, | |
1021 | sizeof (valstr)); | |
1022 | (void) printf("Memory that will be " | |
1023 | "used after removing %s: %s\n", | |
1024 | argv[i], valstr); | |
1025 | } | |
1026 | } else { | |
1027 | if (zpool_vdev_remove(zhp, argv[i]) != 0) | |
1028 | ret = 1; | |
1029 | } | |
1030 | } | |
34dc7c2f | 1031 | } |
884385a0 | 1032 | zpool_close(zhp); |
34dc7c2f BB |
1033 | |
1034 | return (ret); | |
1035 | } | |
1036 | ||
131cc95c | 1037 | /* |
dbb38f66 YP |
1038 | * zpool labelclear [-f] <vdev> |
1039 | * | |
1040 | * -f Force clearing the label for the vdevs which are members of | |
1041 | * the exported or foreign pools. | |
131cc95c DK |
1042 | * |
1043 | * Verifies that the vdev is not active and zeros out the label information | |
1044 | * on the device. | |
1045 | */ | |
1046 | int | |
1047 | zpool_do_labelclear(int argc, char **argv) | |
1048 | { | |
dbb38f66 YP |
1049 | char vdev[MAXPATHLEN]; |
1050 | char *name = NULL; | |
1051 | struct stat st; | |
131cc95c | 1052 | int c, fd = -1, ret = 0; |
dbb38f66 | 1053 | nvlist_t *config; |
131cc95c DK |
1054 | pool_state_t state; |
1055 | boolean_t inuse = B_FALSE; | |
1056 | boolean_t force = B_FALSE; | |
1057 | ||
1058 | /* check options */ | |
1059 | while ((c = getopt(argc, argv, "f")) != -1) { | |
1060 | switch (c) { | |
1061 | case 'f': | |
1062 | force = B_TRUE; | |
1063 | break; | |
1064 | default: | |
1065 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1066 | optopt); | |
1067 | usage(B_FALSE); | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | argc -= optind; | |
1072 | argv += optind; | |
1073 | ||
1074 | /* get vdev name */ | |
1075 | if (argc < 1) { | |
dbb38f66 YP |
1076 | (void) fprintf(stderr, gettext("missing vdev name\n")); |
1077 | usage(B_FALSE); | |
1078 | } | |
1079 | if (argc > 1) { | |
1080 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
131cc95c DK |
1081 | usage(B_FALSE); |
1082 | } | |
1083 | ||
dbb38f66 YP |
1084 | /* |
1085 | * Check if we were given absolute path and use it as is. | |
1086 | * Otherwise if the provided vdev name doesn't point to a file, | |
1087 | * try prepending expected disk paths and partition numbers. | |
1088 | */ | |
1089 | (void) strlcpy(vdev, argv[0], sizeof (vdev)); | |
1090 | if (vdev[0] != '/' && stat(vdev, &st) != 0) { | |
1091 | int error; | |
1092 | ||
1093 | error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN); | |
1094 | if (error == 0 && zfs_dev_is_whole_disk(vdev)) { | |
1095 | if (zfs_append_partition(vdev, MAXPATHLEN) == -1) | |
1096 | error = ENOENT; | |
1097 | } | |
1098 | ||
1099 | if (error || (stat(vdev, &st) != 0)) { | |
1100 | (void) fprintf(stderr, gettext( | |
1101 | "failed to find device %s, try specifying absolute " | |
1102 | "path instead\n"), argv[0]); | |
1103 | return (1); | |
1104 | } | |
131cc95c DK |
1105 | } |
1106 | ||
dbb38f66 YP |
1107 | if ((fd = open(vdev, O_RDWR)) < 0) { |
1108 | (void) fprintf(stderr, gettext("failed to open %s: %s\n"), | |
1109 | vdev, strerror(errno)); | |
1110 | return (1); | |
1111 | } | |
131cc95c | 1112 | |
a167aa7c GDN |
1113 | if (ioctl(fd, BLKFLSBUF) != 0) |
1114 | (void) fprintf(stderr, gettext("failed to invalidate " | |
1115 | "cache for %s: %s\n"), vdev, strerror(errno)); | |
1116 | ||
dbb38f66 | 1117 | if (zpool_read_label(fd, &config, NULL) != 0 || config == NULL) { |
131cc95c | 1118 | (void) fprintf(stderr, |
dbb38f66 | 1119 | gettext("failed to check state for %s\n"), vdev); |
c721ba43 TH |
1120 | ret = 1; |
1121 | goto errout; | |
dbb38f66 YP |
1122 | } |
1123 | nvlist_free(config); | |
131cc95c | 1124 | |
dbb38f66 YP |
1125 | ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse); |
1126 | if (ret != 0) { | |
1127 | (void) fprintf(stderr, | |
1128 | gettext("failed to check state for %s\n"), vdev); | |
c721ba43 TH |
1129 | ret = 1; |
1130 | goto errout; | |
131cc95c DK |
1131 | } |
1132 | ||
dbb38f66 YP |
1133 | if (!inuse) |
1134 | goto wipe_label; | |
1135 | ||
1136 | switch (state) { | |
1137 | default: | |
1138 | case POOL_STATE_ACTIVE: | |
1139 | case POOL_STATE_SPARE: | |
1140 | case POOL_STATE_L2CACHE: | |
1141 | (void) fprintf(stderr, gettext( | |
1142 | "%s is a member (%s) of pool \"%s\"\n"), | |
1143 | vdev, zpool_pool_state_to_name(state), name); | |
1144 | ret = 1; | |
1145 | goto errout; | |
131cc95c | 1146 | |
dbb38f66 YP |
1147 | case POOL_STATE_EXPORTED: |
1148 | if (force) | |
131cc95c | 1149 | break; |
dbb38f66 YP |
1150 | (void) fprintf(stderr, gettext( |
1151 | "use '-f' to override the following error:\n" | |
1152 | "%s is a member of exported pool \"%s\"\n"), | |
1153 | vdev, name); | |
1154 | ret = 1; | |
1155 | goto errout; | |
1156 | ||
1157 | case POOL_STATE_POTENTIALLY_ACTIVE: | |
1158 | if (force) | |
1159 | break; | |
1160 | (void) fprintf(stderr, gettext( | |
1161 | "use '-f' to override the following error:\n" | |
1162 | "%s is a member of potentially active pool \"%s\"\n"), | |
1163 | vdev, name); | |
1164 | ret = 1; | |
1165 | goto errout; | |
1166 | ||
1167 | case POOL_STATE_DESTROYED: | |
1168 | /* inuse should never be set for a destroyed pool */ | |
1169 | assert(0); | |
1170 | break; | |
131cc95c DK |
1171 | } |
1172 | ||
1173 | wipe_label: | |
dbb38f66 YP |
1174 | ret = zpool_clear_label(fd); |
1175 | if (ret != 0) { | |
131cc95c | 1176 | (void) fprintf(stderr, |
dbb38f66 | 1177 | gettext("failed to clear label for %s\n"), vdev); |
131cc95c DK |
1178 | } |
1179 | ||
1180 | errout: | |
dbb38f66 YP |
1181 | free(name); |
1182 | (void) close(fd); | |
131cc95c DK |
1183 | |
1184 | return (ret); | |
1185 | } | |
1186 | ||
34dc7c2f | 1187 | /* |
9ae529ec | 1188 | * zpool create [-fnd] [-o property=value] ... |
b128c09f BB |
1189 | * [-O file-system-property=value] ... |
1190 | * [-R root] [-m mountpoint] <pool> <dev> ... | |
34dc7c2f BB |
1191 | * |
1192 | * -f Force creation, even if devices appear in use | |
1193 | * -n Do not create the pool, but display the resulting layout if it | |
1194 | * were to be created. | |
1195 | * -R Create a pool under an alternate root | |
1196 | * -m Set default mountpoint for the root dataset. By default it's | |
9ae529ec | 1197 | * '/<pool>' |
34dc7c2f | 1198 | * -o Set property=value. |
e4010f27 | 1199 | * -o Set feature@feature=enabled|disabled. |
9ae529ec CS |
1200 | * -d Don't automatically enable all supported pool features |
1201 | * (individual features can be enabled with -o). | |
b128c09f | 1202 | * -O Set fsproperty=value in the pool's root file system |
34dc7c2f BB |
1203 | * |
1204 | * Creates the named pool according to the given vdev specification. The | |
1205 | * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once | |
1206 | * we get the nvlist back from get_vdev_spec(), we either print out the contents | |
1207 | * (if '-n' was specified), or pass it to libzfs to do the creation. | |
1208 | */ | |
1209 | int | |
1210 | zpool_do_create(int argc, char **argv) | |
1211 | { | |
1212 | boolean_t force = B_FALSE; | |
1213 | boolean_t dryrun = B_FALSE; | |
9ae529ec | 1214 | boolean_t enable_all_pool_feat = B_TRUE; |
34dc7c2f BB |
1215 | int c; |
1216 | nvlist_t *nvroot = NULL; | |
1217 | char *poolname; | |
023bbe6f | 1218 | char *tname = NULL; |
34dc7c2f BB |
1219 | int ret = 1; |
1220 | char *altroot = NULL; | |
1221 | char *mountpoint = NULL; | |
b128c09f | 1222 | nvlist_t *fsprops = NULL; |
34dc7c2f BB |
1223 | nvlist_t *props = NULL; |
1224 | char *propval; | |
1225 | ||
1226 | /* check options */ | |
83e9986f | 1227 | while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) { |
34dc7c2f BB |
1228 | switch (c) { |
1229 | case 'f': | |
1230 | force = B_TRUE; | |
1231 | break; | |
1232 | case 'n': | |
1233 | dryrun = B_TRUE; | |
1234 | break; | |
9ae529ec CS |
1235 | case 'd': |
1236 | enable_all_pool_feat = B_FALSE; | |
1237 | break; | |
34dc7c2f BB |
1238 | case 'R': |
1239 | altroot = optarg; | |
1240 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 1241 | ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) |
34dc7c2f | 1242 | goto errout; |
2f3ec900 | 1243 | if (add_prop_list_default(zpool_prop_to_name( |
b128c09f | 1244 | ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) |
34dc7c2f BB |
1245 | goto errout; |
1246 | break; | |
1247 | case 'm': | |
7bc7f250 | 1248 | /* Equivalent to -O mountpoint=optarg */ |
34dc7c2f BB |
1249 | mountpoint = optarg; |
1250 | break; | |
1251 | case 'o': | |
1252 | if ((propval = strchr(optarg, '=')) == NULL) { | |
1253 | (void) fprintf(stderr, gettext("missing " | |
1254 | "'=' for -o option\n")); | |
1255 | goto errout; | |
1256 | } | |
1257 | *propval = '\0'; | |
1258 | propval++; | |
1259 | ||
b128c09f BB |
1260 | if (add_prop_list(optarg, propval, &props, B_TRUE)) |
1261 | goto errout; | |
9ae529ec CS |
1262 | |
1263 | /* | |
1264 | * If the user is creating a pool that doesn't support | |
1265 | * feature flags, don't enable any features. | |
1266 | */ | |
1267 | if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { | |
1268 | char *end; | |
1269 | u_longlong_t ver; | |
1270 | ||
1271 | ver = strtoull(propval, &end, 10); | |
1272 | if (*end == '\0' && | |
1273 | ver < SPA_VERSION_FEATURES) { | |
1274 | enable_all_pool_feat = B_FALSE; | |
1275 | } | |
1276 | } | |
3ac2794c BB |
1277 | if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT) |
1278 | altroot = propval; | |
b128c09f BB |
1279 | break; |
1280 | case 'O': | |
1281 | if ((propval = strchr(optarg, '=')) == NULL) { | |
1282 | (void) fprintf(stderr, gettext("missing " | |
1283 | "'=' for -O option\n")); | |
1284 | goto errout; | |
1285 | } | |
1286 | *propval = '\0'; | |
1287 | propval++; | |
1288 | ||
7bc7f250 WA |
1289 | /* |
1290 | * Mountpoints are checked and then added later. | |
1291 | * Uniquely among properties, they can be specified | |
1292 | * more than once, to avoid conflict with -m. | |
1293 | */ | |
1294 | if (0 == strcmp(optarg, | |
1295 | zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) { | |
1296 | mountpoint = propval; | |
1297 | } else if (add_prop_list(optarg, propval, &fsprops, | |
1298 | B_FALSE)) { | |
34dc7c2f | 1299 | goto errout; |
7bc7f250 | 1300 | } |
34dc7c2f | 1301 | break; |
83e9986f RY |
1302 | case 't': |
1303 | /* | |
1304 | * Sanity check temporary pool name. | |
1305 | */ | |
1306 | if (strchr(optarg, '/') != NULL) { | |
1307 | (void) fprintf(stderr, gettext("cannot create " | |
1308 | "'%s': invalid character '/' in temporary " | |
1309 | "name\n"), optarg); | |
1310 | (void) fprintf(stderr, gettext("use 'zfs " | |
1311 | "create' to create a dataset\n")); | |
1312 | goto errout; | |
1313 | } | |
1314 | ||
1315 | if (add_prop_list(zpool_prop_to_name( | |
1316 | ZPOOL_PROP_TNAME), optarg, &props, B_TRUE)) | |
1317 | goto errout; | |
1318 | if (add_prop_list_default(zpool_prop_to_name( | |
1319 | ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) | |
1320 | goto errout; | |
023bbe6f | 1321 | tname = optarg; |
83e9986f | 1322 | break; |
34dc7c2f BB |
1323 | case ':': |
1324 | (void) fprintf(stderr, gettext("missing argument for " | |
1325 | "'%c' option\n"), optopt); | |
1326 | goto badusage; | |
1327 | case '?': | |
1328 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1329 | optopt); | |
1330 | goto badusage; | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | argc -= optind; | |
1335 | argv += optind; | |
1336 | ||
1337 | /* get pool name and check number of arguments */ | |
1338 | if (argc < 1) { | |
1339 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
1340 | goto badusage; | |
1341 | } | |
1342 | if (argc < 2) { | |
1343 | (void) fprintf(stderr, gettext("missing vdev specification\n")); | |
1344 | goto badusage; | |
1345 | } | |
1346 | ||
1347 | poolname = argv[0]; | |
1348 | ||
1349 | /* | |
1350 | * As a special case, check for use of '/' in the name, and direct the | |
1351 | * user to use 'zfs create' instead. | |
1352 | */ | |
1353 | if (strchr(poolname, '/') != NULL) { | |
1354 | (void) fprintf(stderr, gettext("cannot create '%s': invalid " | |
1355 | "character '/' in pool name\n"), poolname); | |
1356 | (void) fprintf(stderr, gettext("use 'zfs create' to " | |
1357 | "create a dataset\n")); | |
1358 | goto errout; | |
1359 | } | |
1360 | ||
1361 | /* pass off to get_vdev_spec for bulk processing */ | |
df30f566 | 1362 | nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun, |
b128c09f | 1363 | argc - 1, argv + 1); |
34dc7c2f | 1364 | if (nvroot == NULL) |
b128c09f | 1365 | goto errout; |
34dc7c2f BB |
1366 | |
1367 | /* make_root_vdev() allows 0 toplevel children if there are spares */ | |
1368 | if (!zfs_allocatable_devs(nvroot)) { | |
1369 | (void) fprintf(stderr, gettext("invalid vdev " | |
1370 | "specification: at least one toplevel vdev must be " | |
1371 | "specified\n")); | |
1372 | goto errout; | |
1373 | } | |
1374 | ||
34dc7c2f BB |
1375 | if (altroot != NULL && altroot[0] != '/') { |
1376 | (void) fprintf(stderr, gettext("invalid alternate root '%s': " | |
1377 | "must be an absolute path\n"), altroot); | |
1378 | goto errout; | |
1379 | } | |
1380 | ||
1381 | /* | |
1382 | * Check the validity of the mountpoint and direct the user to use the | |
1383 | * '-m' mountpoint option if it looks like its in use. | |
1384 | */ | |
1385 | if (mountpoint == NULL || | |
1386 | (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && | |
1387 | strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) { | |
1388 | char buf[MAXPATHLEN]; | |
1389 | DIR *dirp; | |
1390 | ||
1391 | if (mountpoint && mountpoint[0] != '/') { | |
1392 | (void) fprintf(stderr, gettext("invalid mountpoint " | |
1393 | "'%s': must be an absolute path, 'legacy', or " | |
1394 | "'none'\n"), mountpoint); | |
1395 | goto errout; | |
1396 | } | |
1397 | ||
1398 | if (mountpoint == NULL) { | |
1399 | if (altroot != NULL) | |
1400 | (void) snprintf(buf, sizeof (buf), "%s/%s", | |
1401 | altroot, poolname); | |
1402 | else | |
1403 | (void) snprintf(buf, sizeof (buf), "/%s", | |
1404 | poolname); | |
1405 | } else { | |
1406 | if (altroot != NULL) | |
1407 | (void) snprintf(buf, sizeof (buf), "%s%s", | |
1408 | altroot, mountpoint); | |
1409 | else | |
1410 | (void) snprintf(buf, sizeof (buf), "%s", | |
1411 | mountpoint); | |
1412 | } | |
1413 | ||
1414 | if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { | |
1415 | (void) fprintf(stderr, gettext("mountpoint '%s' : " | |
1416 | "%s\n"), buf, strerror(errno)); | |
1417 | (void) fprintf(stderr, gettext("use '-m' " | |
1418 | "option to provide a different default\n")); | |
1419 | goto errout; | |
1420 | } else if (dirp) { | |
1421 | int count = 0; | |
1422 | ||
1423 | while (count < 3 && readdir(dirp) != NULL) | |
1424 | count++; | |
1425 | (void) closedir(dirp); | |
1426 | ||
1427 | if (count > 2) { | |
1428 | (void) fprintf(stderr, gettext("mountpoint " | |
1429 | "'%s' exists and is not empty\n"), buf); | |
1430 | (void) fprintf(stderr, gettext("use '-m' " | |
1431 | "option to provide a " | |
1432 | "different default\n")); | |
1433 | goto errout; | |
1434 | } | |
1435 | } | |
1436 | } | |
1437 | ||
7bc7f250 WA |
1438 | /* |
1439 | * Now that the mountpoint's validity has been checked, ensure that | |
1440 | * the property is set appropriately prior to creating the pool. | |
1441 | */ | |
1442 | if (mountpoint != NULL) { | |
1443 | ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), | |
1444 | mountpoint, &fsprops, B_FALSE); | |
1445 | if (ret != 0) | |
1446 | goto errout; | |
1447 | } | |
1448 | ||
1449 | ret = 1; | |
34dc7c2f BB |
1450 | if (dryrun) { |
1451 | /* | |
1452 | * For a dry run invocation, print out a basic message and run | |
1453 | * through all the vdevs in the list and print out in an | |
1454 | * appropriate hierarchy. | |
1455 | */ | |
1456 | (void) printf(gettext("would create '%s' with the " | |
1457 | "following layout:\n\n"), poolname); | |
1458 | ||
cc99f275 DB |
1459 | print_vdev_tree(NULL, poolname, nvroot, 0, "", 0); |
1460 | print_vdev_tree(NULL, "dedup", nvroot, 0, | |
1461 | VDEV_ALLOC_BIAS_DEDUP, 0); | |
1462 | print_vdev_tree(NULL, "special", nvroot, 0, | |
1463 | VDEV_ALLOC_BIAS_SPECIAL, 0); | |
1464 | print_vdev_tree(NULL, "logs", nvroot, 0, | |
1465 | VDEV_ALLOC_BIAS_LOG, 0); | |
34dc7c2f BB |
1466 | |
1467 | ret = 0; | |
1468 | } else { | |
1469 | /* | |
1470 | * Hand off to libzfs. | |
1471 | */ | |
e4010f27 | 1472 | spa_feature_t i; |
1473 | for (i = 0; i < SPA_FEATURES; i++) { | |
1474 | char propname[MAXPATHLEN]; | |
1475 | char *propval; | |
1476 | zfeature_info_t *feat = &spa_feature_table[i]; | |
9ae529ec | 1477 | |
e4010f27 | 1478 | (void) snprintf(propname, sizeof (propname), |
1479 | "feature@%s", feat->fi_uname); | |
1480 | ||
1481 | /* | |
1482 | * Only features contained in props will be enabled: | |
1483 | * remove from the nvlist every ZFS_FEATURE_DISABLED | |
1484 | * value and add every missing ZFS_FEATURE_ENABLED if | |
1485 | * enable_all_pool_feat is set. | |
1486 | */ | |
1487 | if (!nvlist_lookup_string(props, propname, &propval)) { | |
1488 | if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0) | |
1489 | (void) nvlist_remove_all(props, | |
1490 | propname); | |
1491 | } else if (enable_all_pool_feat) { | |
7bc7f250 WA |
1492 | ret = add_prop_list(propname, |
1493 | ZFS_FEATURE_ENABLED, &props, B_TRUE); | |
1494 | if (ret != 0) | |
9ae529ec CS |
1495 | goto errout; |
1496 | } | |
1497 | } | |
7bc7f250 WA |
1498 | |
1499 | ret = 1; | |
b128c09f BB |
1500 | if (zpool_create(g_zfs, poolname, |
1501 | nvroot, props, fsprops) == 0) { | |
023bbe6f | 1502 | zfs_handle_t *pool = zfs_open(g_zfs, |
1503 | tname ? tname : poolname, ZFS_TYPE_FILESYSTEM); | |
34dc7c2f | 1504 | if (pool != NULL) { |
34dc7c2f BB |
1505 | if (zfs_mount(pool, NULL, 0) == 0) |
1506 | ret = zfs_shareall(pool); | |
1507 | zfs_close(pool); | |
1508 | } | |
1509 | } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { | |
1510 | (void) fprintf(stderr, gettext("pool name may have " | |
1511 | "been omitted\n")); | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | errout: | |
1516 | nvlist_free(nvroot); | |
b128c09f | 1517 | nvlist_free(fsprops); |
34dc7c2f BB |
1518 | nvlist_free(props); |
1519 | return (ret); | |
1520 | badusage: | |
b128c09f | 1521 | nvlist_free(fsprops); |
34dc7c2f BB |
1522 | nvlist_free(props); |
1523 | usage(B_FALSE); | |
1524 | return (2); | |
1525 | } | |
1526 | ||
1527 | /* | |
1528 | * zpool destroy <pool> | |
1529 | * | |
1530 | * -f Forcefully unmount any datasets | |
1531 | * | |
1532 | * Destroy the given pool. Automatically unmounts any datasets in the pool. | |
1533 | */ | |
1534 | int | |
1535 | zpool_do_destroy(int argc, char **argv) | |
1536 | { | |
1537 | boolean_t force = B_FALSE; | |
1538 | int c; | |
1539 | char *pool; | |
1540 | zpool_handle_t *zhp; | |
1541 | int ret; | |
1542 | ||
1543 | /* check options */ | |
1544 | while ((c = getopt(argc, argv, "f")) != -1) { | |
1545 | switch (c) { | |
1546 | case 'f': | |
1547 | force = B_TRUE; | |
1548 | break; | |
1549 | case '?': | |
1550 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1551 | optopt); | |
1552 | usage(B_FALSE); | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | argc -= optind; | |
1557 | argv += optind; | |
1558 | ||
1559 | /* check arguments */ | |
1560 | if (argc < 1) { | |
1561 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
1562 | usage(B_FALSE); | |
1563 | } | |
1564 | if (argc > 1) { | |
1565 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
1566 | usage(B_FALSE); | |
1567 | } | |
1568 | ||
1569 | pool = argv[0]; | |
1570 | ||
1571 | if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { | |
1572 | /* | |
1573 | * As a special case, check for use of '/' in the name, and | |
1574 | * direct the user to use 'zfs destroy' instead. | |
1575 | */ | |
1576 | if (strchr(pool, '/') != NULL) | |
1577 | (void) fprintf(stderr, gettext("use 'zfs destroy' to " | |
1578 | "destroy a dataset\n")); | |
1579 | return (1); | |
1580 | } | |
1581 | ||
1582 | if (zpool_disable_datasets(zhp, force) != 0) { | |
1583 | (void) fprintf(stderr, gettext("could not destroy '%s': " | |
1584 | "could not unmount datasets\n"), zpool_get_name(zhp)); | |
a425f5bf | 1585 | zpool_close(zhp); |
34dc7c2f BB |
1586 | return (1); |
1587 | } | |
1588 | ||
6f1ffb06 MA |
1589 | /* The history must be logged as part of the export */ |
1590 | log_history = B_FALSE; | |
1591 | ||
1592 | ret = (zpool_destroy(zhp, history_str) != 0); | |
34dc7c2f BB |
1593 | |
1594 | zpool_close(zhp); | |
1595 | ||
1596 | return (ret); | |
1597 | } | |
1598 | ||
859735c0 TF |
1599 | typedef struct export_cbdata { |
1600 | boolean_t force; | |
1601 | boolean_t hardforce; | |
1602 | } export_cbdata_t; | |
1603 | ||
1604 | /* | |
1605 | * Export one pool | |
1606 | */ | |
1607 | int | |
1608 | zpool_export_one(zpool_handle_t *zhp, void *data) | |
1609 | { | |
1610 | export_cbdata_t *cb = data; | |
1611 | ||
1612 | if (zpool_disable_datasets(zhp, cb->force) != 0) | |
1613 | return (1); | |
1614 | ||
1615 | /* The history must be logged as part of the export */ | |
1616 | log_history = B_FALSE; | |
1617 | ||
1618 | if (cb->hardforce) { | |
1619 | if (zpool_export_force(zhp, history_str) != 0) | |
1620 | return (1); | |
1621 | } else if (zpool_export(zhp, cb->force, history_str) != 0) { | |
1622 | return (1); | |
1623 | } | |
1624 | ||
1625 | return (0); | |
1626 | } | |
1627 | ||
34dc7c2f BB |
1628 | /* |
1629 | * zpool export [-f] <pool> ... | |
1630 | * | |
859735c0 | 1631 | * -a Export all pools |
34dc7c2f BB |
1632 | * -f Forcefully unmount datasets |
1633 | * | |
1634 | * Export the given pools. By default, the command will attempt to cleanly | |
1635 | * unmount any active datasets within the pool. If the '-f' flag is specified, | |
1636 | * then the datasets will be forcefully unmounted. | |
1637 | */ | |
1638 | int | |
1639 | zpool_do_export(int argc, char **argv) | |
1640 | { | |
859735c0 TF |
1641 | export_cbdata_t cb; |
1642 | boolean_t do_all = B_FALSE; | |
34dc7c2f | 1643 | boolean_t force = B_FALSE; |
fb5f0bc8 | 1644 | boolean_t hardforce = B_FALSE; |
859735c0 | 1645 | int c, ret; |
34dc7c2f BB |
1646 | |
1647 | /* check options */ | |
859735c0 | 1648 | while ((c = getopt(argc, argv, "afF")) != -1) { |
34dc7c2f | 1649 | switch (c) { |
859735c0 TF |
1650 | case 'a': |
1651 | do_all = B_TRUE; | |
1652 | break; | |
34dc7c2f BB |
1653 | case 'f': |
1654 | force = B_TRUE; | |
1655 | break; | |
fb5f0bc8 BB |
1656 | case 'F': |
1657 | hardforce = B_TRUE; | |
1658 | break; | |
34dc7c2f BB |
1659 | case '?': |
1660 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
1661 | optopt); | |
1662 | usage(B_FALSE); | |
1663 | } | |
1664 | } | |
1665 | ||
859735c0 TF |
1666 | cb.force = force; |
1667 | cb.hardforce = hardforce; | |
34dc7c2f BB |
1668 | argc -= optind; |
1669 | argv += optind; | |
1670 | ||
859735c0 TF |
1671 | if (do_all) { |
1672 | if (argc != 0) { | |
1673 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
1674 | usage(B_FALSE); | |
1675 | } | |
1676 | ||
1677 | return (for_each_pool(argc, argv, B_TRUE, NULL, | |
1678 | zpool_export_one, &cb)); | |
1679 | } | |
1680 | ||
34dc7c2f BB |
1681 | /* check arguments */ |
1682 | if (argc < 1) { | |
1683 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
1684 | usage(B_FALSE); | |
1685 | } | |
1686 | ||
859735c0 | 1687 | ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb); |
34dc7c2f BB |
1688 | |
1689 | return (ret); | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * Given a vdev configuration, determine the maximum width needed for the device | |
1694 | * name column. | |
1695 | */ | |
1696 | static int | |
d2f3e292 RY |
1697 | max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max, |
1698 | int name_flags) | |
34dc7c2f | 1699 | { |
d2f3e292 | 1700 | char *name; |
34dc7c2f BB |
1701 | nvlist_t **child; |
1702 | uint_t c, children; | |
1703 | int ret; | |
1704 | ||
5f20c145 | 1705 | name = zpool_vdev_name(g_zfs, zhp, nv, name_flags); |
34dc7c2f BB |
1706 | if (strlen(name) + depth > max) |
1707 | max = strlen(name) + depth; | |
1708 | ||
1709 | free(name); | |
1710 | ||
1711 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, | |
1712 | &child, &children) == 0) { | |
1713 | for (c = 0; c < children; c++) | |
1714 | if ((ret = max_width(zhp, child[c], depth + 2, | |
d2f3e292 | 1715 | max, name_flags)) > max) |
34dc7c2f BB |
1716 | max = ret; |
1717 | } | |
1718 | ||
1719 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, | |
1720 | &child, &children) == 0) { | |
1721 | for (c = 0; c < children; c++) | |
1722 | if ((ret = max_width(zhp, child[c], depth + 2, | |
d2f3e292 | 1723 | max, name_flags)) > max) |
34dc7c2f BB |
1724 | max = ret; |
1725 | } | |
1726 | ||
1727 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
1728 | &child, &children) == 0) { | |
1729 | for (c = 0; c < children; c++) | |
1730 | if ((ret = max_width(zhp, child[c], depth + 2, | |
d2f3e292 | 1731 | max, name_flags)) > max) |
34dc7c2f BB |
1732 | max = ret; |
1733 | } | |
1734 | ||
34dc7c2f BB |
1735 | return (max); |
1736 | } | |
1737 | ||
9babb374 BB |
1738 | typedef struct spare_cbdata { |
1739 | uint64_t cb_guid; | |
1740 | zpool_handle_t *cb_zhp; | |
1741 | } spare_cbdata_t; | |
1742 | ||
1743 | static boolean_t | |
1744 | find_vdev(nvlist_t *nv, uint64_t search) | |
1745 | { | |
1746 | uint64_t guid; | |
1747 | nvlist_t **child; | |
1748 | uint_t c, children; | |
1749 | ||
1750 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && | |
1751 | search == guid) | |
1752 | return (B_TRUE); | |
1753 | ||
1754 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
1755 | &child, &children) == 0) { | |
1756 | for (c = 0; c < children; c++) | |
1757 | if (find_vdev(child[c], search)) | |
1758 | return (B_TRUE); | |
1759 | } | |
1760 | ||
1761 | return (B_FALSE); | |
1762 | } | |
1763 | ||
1764 | static int | |
1765 | find_spare(zpool_handle_t *zhp, void *data) | |
1766 | { | |
1767 | spare_cbdata_t *cbp = data; | |
1768 | nvlist_t *config, *nvroot; | |
1769 | ||
1770 | config = zpool_get_config(zhp, NULL); | |
1771 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
1772 | &nvroot) == 0); | |
1773 | ||
1774 | if (find_vdev(nvroot, cbp->cb_guid)) { | |
1775 | cbp->cb_zhp = zhp; | |
1776 | return (1); | |
1777 | } | |
1778 | ||
1779 | zpool_close(zhp); | |
1780 | return (0); | |
1781 | } | |
1782 | ||
fea33e4e HJ |
1783 | typedef struct status_cbdata { |
1784 | int cb_count; | |
1785 | int cb_name_flags; | |
1786 | int cb_namewidth; | |
1787 | boolean_t cb_allpools; | |
1788 | boolean_t cb_verbose; | |
ad796b8a | 1789 | boolean_t cb_literal; |
fea33e4e HJ |
1790 | boolean_t cb_explain; |
1791 | boolean_t cb_first; | |
1792 | boolean_t cb_dedup_stats; | |
1793 | boolean_t cb_print_status; | |
ad796b8a | 1794 | boolean_t cb_print_slow_ios; |
8720e9e7 | 1795 | vdev_cmd_data_list_t *vcdl; |
fea33e4e HJ |
1796 | } status_cbdata_t; |
1797 | ||
d6418de0 TH |
1798 | /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */ |
1799 | static int | |
1800 | is_blank_str(char *str) | |
1801 | { | |
1802 | while (str != NULL && *str != '\0') { | |
1803 | if (!isblank(*str)) | |
1804 | return (0); | |
1805 | str++; | |
1806 | } | |
1807 | return (1); | |
1808 | } | |
1809 | ||
1810 | /* Print command output lines for specific vdev in a specific pool */ | |
8720e9e7 TH |
1811 | static void |
1812 | zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path) | |
1813 | { | |
d6418de0 TH |
1814 | vdev_cmd_data_t *data; |
1815 | int i, j; | |
1816 | char *val; | |
1817 | ||
8720e9e7 | 1818 | for (i = 0; i < vcdl->count; i++) { |
d6418de0 TH |
1819 | if ((strcmp(vcdl->data[i].path, path) != 0) || |
1820 | (strcmp(vcdl->data[i].pool, pool) != 0)) { | |
1821 | /* Not the vdev we're looking for */ | |
1822 | continue; | |
8720e9e7 | 1823 | } |
d6418de0 TH |
1824 | |
1825 | data = &vcdl->data[i]; | |
1826 | /* Print out all the output values for this vdev */ | |
1827 | for (j = 0; j < vcdl->uniq_cols_cnt; j++) { | |
1828 | val = NULL; | |
1829 | /* Does this vdev have values for this column? */ | |
1830 | for (int k = 0; k < data->cols_cnt; k++) { | |
1831 | if (strcmp(data->cols[k], | |
1832 | vcdl->uniq_cols[j]) == 0) { | |
1833 | /* yes it does, record the value */ | |
1834 | val = data->lines[k]; | |
1835 | break; | |
1836 | } | |
1837 | } | |
1838 | /* | |
1839 | * Mark empty values with dashes to make output | |
1840 | * awk-able. | |
1841 | */ | |
1842 | if (is_blank_str(val)) | |
1843 | val = "-"; | |
1844 | ||
1845 | printf("%*s", vcdl->uniq_cols_width[j], val); | |
1846 | if (j < vcdl->uniq_cols_cnt - 1) | |
1847 | printf(" "); | |
1848 | } | |
1849 | ||
1850 | /* Print out any values that aren't in a column at the end */ | |
1851 | for (j = data->cols_cnt; j < data->lines_cnt; j++) { | |
1852 | /* Did we have any columns? If so print a spacer. */ | |
1853 | if (vcdl->uniq_cols_cnt > 0) | |
1854 | printf(" "); | |
1855 | ||
1856 | val = data->lines[j]; | |
1857 | printf("%s", val ? val : ""); | |
1858 | } | |
1859 | break; | |
8720e9e7 TH |
1860 | } |
1861 | } | |
1862 | ||
9babb374 BB |
1863 | /* |
1864 | * Print out configuration state as requested by status_callback. | |
1865 | */ | |
d2f3e292 | 1866 | static void |
fea33e4e HJ |
1867 | print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name, |
1868 | nvlist_t *nv, int depth, boolean_t isspare) | |
9babb374 | 1869 | { |
2ef0f8c3 | 1870 | nvlist_t **child, *root; |
9babb374 | 1871 | uint_t c, children; |
428870ff | 1872 | pool_scan_stat_t *ps = NULL; |
9babb374 | 1873 | vdev_stat_t *vs; |
428870ff | 1874 | char rbuf[6], wbuf[6], cbuf[6]; |
9babb374 BB |
1875 | char *vname; |
1876 | uint64_t notpresent; | |
fea33e4e | 1877 | spare_cbdata_t spare_cb; |
6b8655ad | 1878 | const char *state; |
a1d477c2 | 1879 | char *type; |
8720e9e7 | 1880 | char *path = NULL; |
9babb374 | 1881 | |
9babb374 BB |
1882 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1883 | &child, &children) != 0) | |
1884 | children = 0; | |
1885 | ||
428870ff BB |
1886 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
1887 | (uint64_t **)&vs, &c) == 0); | |
1888 | ||
a1d477c2 MA |
1889 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); |
1890 | ||
1891 | if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
1892 | return; | |
1893 | ||
9babb374 BB |
1894 | state = zpool_state_to_name(vs->vs_state, vs->vs_aux); |
1895 | if (isspare) { | |
1896 | /* | |
1897 | * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for | |
1898 | * online drives. | |
1899 | */ | |
1900 | if (vs->vs_aux == VDEV_AUX_SPARED) | |
1901 | state = "INUSE"; | |
1902 | else if (vs->vs_state == VDEV_STATE_HEALTHY) | |
1903 | state = "AVAIL"; | |
1904 | } | |
1905 | ||
fea33e4e | 1906 | (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth, |
9babb374 BB |
1907 | name, state); |
1908 | ||
1909 | if (!isspare) { | |
ad796b8a TH |
1910 | if (cb->cb_literal) { |
1911 | printf(" %5llu %5llu %5llu", | |
1912 | (u_longlong_t)vs->vs_read_errors, | |
1913 | (u_longlong_t)vs->vs_write_errors, | |
1914 | (u_longlong_t)vs->vs_checksum_errors); | |
1915 | } else { | |
1916 | zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); | |
1917 | zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); | |
1918 | zfs_nicenum(vs->vs_checksum_errors, cbuf, | |
1919 | sizeof (cbuf)); | |
1920 | printf(" %5s %5s %5s", rbuf, wbuf, cbuf); | |
1921 | } | |
1922 | ||
1923 | if (cb->cb_print_slow_ios) { | |
1924 | if (children == 0) { | |
1925 | /* Only leafs vdevs have slow IOs */ | |
1926 | zfs_nicenum(vs->vs_slow_ios, rbuf, | |
1927 | sizeof (rbuf)); | |
1928 | } else { | |
1929 | snprintf(rbuf, sizeof (rbuf), "-"); | |
1930 | } | |
1931 | ||
1932 | if (cb->cb_literal) | |
1933 | printf(" %5llu", (u_longlong_t)vs->vs_slow_ios); | |
1934 | else | |
1935 | printf(" %5s", rbuf); | |
1936 | } | |
1937 | ||
9babb374 BB |
1938 | } |
1939 | ||
1940 | if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, | |
1941 | ¬present) == 0) { | |
9babb374 BB |
1942 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); |
1943 | (void) printf(" was %s", path); | |
1944 | } else if (vs->vs_aux != 0) { | |
1945 | (void) printf(" "); | |
1946 | ||
1947 | switch (vs->vs_aux) { | |
1948 | case VDEV_AUX_OPEN_FAILED: | |
1949 | (void) printf(gettext("cannot open")); | |
1950 | break; | |
1951 | ||
1952 | case VDEV_AUX_BAD_GUID_SUM: | |
1953 | (void) printf(gettext("missing device")); | |
1954 | break; | |
1955 | ||
1956 | case VDEV_AUX_NO_REPLICAS: | |
1957 | (void) printf(gettext("insufficient replicas")); | |
1958 | break; | |
1959 | ||
1960 | case VDEV_AUX_VERSION_NEWER: | |
1961 | (void) printf(gettext("newer version")); | |
1962 | break; | |
1963 | ||
9ae529ec CS |
1964 | case VDEV_AUX_UNSUP_FEAT: |
1965 | (void) printf(gettext("unsupported feature(s)")); | |
1966 | break; | |
1967 | ||
9babb374 BB |
1968 | case VDEV_AUX_SPARED: |
1969 | verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, | |
fea33e4e HJ |
1970 | &spare_cb.cb_guid) == 0); |
1971 | if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) { | |
1972 | if (strcmp(zpool_get_name(spare_cb.cb_zhp), | |
9babb374 BB |
1973 | zpool_get_name(zhp)) == 0) |
1974 | (void) printf(gettext("currently in " | |
1975 | "use")); | |
1976 | else | |
1977 | (void) printf(gettext("in use by " | |
1978 | "pool '%s'"), | |
fea33e4e HJ |
1979 | zpool_get_name(spare_cb.cb_zhp)); |
1980 | zpool_close(spare_cb.cb_zhp); | |
9babb374 BB |
1981 | } else { |
1982 | (void) printf(gettext("currently in use")); | |
1983 | } | |
1984 | break; | |
1985 | ||
1986 | case VDEV_AUX_ERR_EXCEEDED: | |
1987 | (void) printf(gettext("too many errors")); | |
1988 | break; | |
1989 | ||
1990 | case VDEV_AUX_IO_FAILURE: | |
1991 | (void) printf(gettext("experienced I/O failures")); | |
1992 | break; | |
1993 | ||
1994 | case VDEV_AUX_BAD_LOG: | |
1995 | (void) printf(gettext("bad intent log")); | |
1996 | break; | |
1997 | ||
428870ff BB |
1998 | case VDEV_AUX_EXTERNAL: |
1999 | (void) printf(gettext("external device fault")); | |
2000 | break; | |
2001 | ||
2002 | case VDEV_AUX_SPLIT_POOL: | |
2003 | (void) printf(gettext("split into new pool")); | |
2004 | break; | |
2005 | ||
379ca9cf OF |
2006 | case VDEV_AUX_ACTIVE: |
2007 | (void) printf(gettext("currently in use")); | |
4265a929 | 2008 | break; |
379ca9cf | 2009 | |
6cb8e530 PZ |
2010 | case VDEV_AUX_CHILDREN_OFFLINE: |
2011 | (void) printf(gettext("all children offline")); | |
2012 | break; | |
2013 | ||
9babb374 BB |
2014 | default: |
2015 | (void) printf(gettext("corrupted data")); | |
2016 | break; | |
2017 | } | |
428870ff BB |
2018 | } |
2019 | ||
2ef0f8c3 TH |
2020 | /* The root vdev has the scrub/resilver stats */ |
2021 | root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL), | |
2022 | ZPOOL_CONFIG_VDEV_TREE); | |
2023 | (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS, | |
428870ff BB |
2024 | (uint64_t **)&ps, &c); |
2025 | ||
80a91e74 TC |
2026 | if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0) { |
2027 | if (vs->vs_scan_processed != 0) { | |
2028 | (void) printf(gettext(" (%s)"), | |
2029 | (ps->pss_func == POOL_SCAN_RESILVER) ? | |
2030 | "resilvering" : "repairing"); | |
2031 | } else if (vs->vs_resilver_deferred) { | |
2032 | (void) printf(gettext(" (awaiting resilver)")); | |
2033 | } | |
9babb374 BB |
2034 | } |
2035 | ||
8720e9e7 TH |
2036 | if (cb->vcdl != NULL) { |
2037 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { | |
2038 | printf(" "); | |
2039 | zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); | |
2040 | } | |
2041 | } | |
2042 | ||
619f0976 GW |
2043 | if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE || |
2044 | vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED || | |
2045 | vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) && | |
2046 | !vs->vs_scan_removing) { | |
2047 | char zbuf[1024]; | |
2048 | char tbuf[256]; | |
2049 | struct tm zaction_ts; | |
2050 | ||
2051 | time_t t = vs->vs_initialize_action_time; | |
2052 | int initialize_pct = 100; | |
2053 | if (vs->vs_initialize_state != VDEV_INITIALIZE_COMPLETE) { | |
2054 | initialize_pct = (vs->vs_initialize_bytes_done * 100 / | |
2055 | (vs->vs_initialize_bytes_est + 1)); | |
2056 | } | |
2057 | ||
2058 | (void) localtime_r(&t, &zaction_ts); | |
2059 | (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts); | |
2060 | ||
2061 | switch (vs->vs_initialize_state) { | |
2062 | case VDEV_INITIALIZE_SUSPENDED: | |
2063 | (void) snprintf(zbuf, sizeof (zbuf), | |
2064 | ", suspended, started at %s", tbuf); | |
2065 | break; | |
2066 | case VDEV_INITIALIZE_ACTIVE: | |
2067 | (void) snprintf(zbuf, sizeof (zbuf), | |
2068 | ", started at %s", tbuf); | |
2069 | break; | |
2070 | case VDEV_INITIALIZE_COMPLETE: | |
2071 | (void) snprintf(zbuf, sizeof (zbuf), | |
2072 | ", completed at %s", tbuf); | |
2073 | break; | |
2074 | } | |
2075 | ||
2076 | (void) printf(gettext(" (%d%% initialized%s)"), | |
2077 | initialize_pct, zbuf); | |
2078 | } | |
2079 | ||
9babb374 BB |
2080 | (void) printf("\n"); |
2081 | ||
2082 | for (c = 0; c < children; c++) { | |
428870ff | 2083 | uint64_t islog = B_FALSE, ishole = B_FALSE; |
9babb374 | 2084 | |
428870ff | 2085 | /* Don't print logs or holes here */ |
9babb374 | 2086 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
428870ff BB |
2087 | &islog); |
2088 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
2089 | &ishole); | |
2090 | if (islog || ishole) | |
9babb374 | 2091 | continue; |
cc99f275 DB |
2092 | /* Only print normal classes here */ |
2093 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) | |
2094 | continue; | |
2095 | ||
d2f3e292 | 2096 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
fea33e4e | 2097 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2ef0f8c3 | 2098 | |
fea33e4e HJ |
2099 | print_status_config(zhp, cb, vname, child[c], depth + 2, |
2100 | isspare); | |
9babb374 BB |
2101 | free(vname); |
2102 | } | |
2103 | } | |
2104 | ||
34dc7c2f BB |
2105 | /* |
2106 | * Print the configuration of an exported pool. Iterate over all vdevs in the | |
2107 | * pool, printing out the name and status for each one. | |
2108 | */ | |
d2f3e292 | 2109 | static void |
fea33e4e HJ |
2110 | print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv, |
2111 | int depth) | |
34dc7c2f BB |
2112 | { |
2113 | nvlist_t **child; | |
2114 | uint_t c, children; | |
2115 | vdev_stat_t *vs; | |
2116 | char *type, *vname; | |
2117 | ||
2118 | verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); | |
428870ff BB |
2119 | if (strcmp(type, VDEV_TYPE_MISSING) == 0 || |
2120 | strcmp(type, VDEV_TYPE_HOLE) == 0) | |
34dc7c2f BB |
2121 | return; |
2122 | ||
428870ff | 2123 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
2124 | (uint64_t **)&vs, &c) == 0); |
2125 | ||
fea33e4e | 2126 | (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name); |
34dc7c2f BB |
2127 | (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); |
2128 | ||
2129 | if (vs->vs_aux != 0) { | |
2130 | (void) printf(" "); | |
2131 | ||
2132 | switch (vs->vs_aux) { | |
2133 | case VDEV_AUX_OPEN_FAILED: | |
2134 | (void) printf(gettext("cannot open")); | |
2135 | break; | |
2136 | ||
2137 | case VDEV_AUX_BAD_GUID_SUM: | |
2138 | (void) printf(gettext("missing device")); | |
2139 | break; | |
2140 | ||
2141 | case VDEV_AUX_NO_REPLICAS: | |
2142 | (void) printf(gettext("insufficient replicas")); | |
2143 | break; | |
2144 | ||
2145 | case VDEV_AUX_VERSION_NEWER: | |
2146 | (void) printf(gettext("newer version")); | |
2147 | break; | |
2148 | ||
9ae529ec CS |
2149 | case VDEV_AUX_UNSUP_FEAT: |
2150 | (void) printf(gettext("unsupported feature(s)")); | |
2151 | break; | |
2152 | ||
34dc7c2f BB |
2153 | case VDEV_AUX_ERR_EXCEEDED: |
2154 | (void) printf(gettext("too many errors")); | |
2155 | break; | |
2156 | ||
379ca9cf OF |
2157 | case VDEV_AUX_ACTIVE: |
2158 | (void) printf(gettext("currently in use")); | |
2159 | break; | |
2160 | ||
6cb8e530 PZ |
2161 | case VDEV_AUX_CHILDREN_OFFLINE: |
2162 | (void) printf(gettext("all children offline")); | |
2163 | break; | |
2164 | ||
34dc7c2f BB |
2165 | default: |
2166 | (void) printf(gettext("corrupted data")); | |
2167 | break; | |
2168 | } | |
2169 | } | |
2170 | (void) printf("\n"); | |
2171 | ||
2172 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
2173 | &child, &children) != 0) | |
2174 | return; | |
2175 | ||
2176 | for (c = 0; c < children; c++) { | |
2177 | uint64_t is_log = B_FALSE; | |
2178 | ||
2179 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
2180 | &is_log); | |
9babb374 | 2181 | if (is_log) |
34dc7c2f | 2182 | continue; |
cc99f275 DB |
2183 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) |
2184 | continue; | |
34dc7c2f | 2185 | |
d2f3e292 | 2186 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e HJ |
2187 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2188 | print_import_config(cb, vname, child[c], depth + 2); | |
34dc7c2f BB |
2189 | free(vname); |
2190 | } | |
2191 | ||
2192 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, | |
2193 | &child, &children) == 0) { | |
2194 | (void) printf(gettext("\tcache\n")); | |
2195 | for (c = 0; c < children; c++) { | |
d2f3e292 | 2196 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e | 2197 | cb->cb_name_flags); |
34dc7c2f BB |
2198 | (void) printf("\t %s\n", vname); |
2199 | free(vname); | |
2200 | } | |
2201 | } | |
2202 | ||
2203 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, | |
2204 | &child, &children) == 0) { | |
2205 | (void) printf(gettext("\tspares\n")); | |
2206 | for (c = 0; c < children; c++) { | |
d2f3e292 | 2207 | vname = zpool_vdev_name(g_zfs, NULL, child[c], |
fea33e4e | 2208 | cb->cb_name_flags); |
34dc7c2f BB |
2209 | (void) printf("\t %s\n", vname); |
2210 | free(vname); | |
2211 | } | |
2212 | } | |
2213 | } | |
2214 | ||
9babb374 | 2215 | /* |
cc99f275 DB |
2216 | * Print specialized class vdevs. |
2217 | * | |
2218 | * These are recorded as top level vdevs in the main pool child array | |
2219 | * but with "is_log" set to 1 or an "alloc_bias" string. We use either | |
2220 | * print_status_config() or print_import_config() to print the top level | |
2221 | * class vdevs then any of their children (eg mirrored slogs) are printed | |
2222 | * recursively - which works because only the top level vdev is marked. | |
9babb374 BB |
2223 | */ |
2224 | static void | |
cc99f275 DB |
2225 | print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv, |
2226 | const char *class) | |
9babb374 BB |
2227 | { |
2228 | uint_t c, children; | |
2229 | nvlist_t **child; | |
cc99f275 DB |
2230 | boolean_t printed = B_FALSE; |
2231 | ||
2232 | assert(zhp != NULL || !cb->cb_verbose); | |
9babb374 BB |
2233 | |
2234 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, | |
2235 | &children) != 0) | |
2236 | return; | |
2237 | ||
9babb374 BB |
2238 | for (c = 0; c < children; c++) { |
2239 | uint64_t is_log = B_FALSE; | |
cc99f275 DB |
2240 | char *bias = NULL; |
2241 | char *type = NULL; | |
9babb374 BB |
2242 | |
2243 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, | |
2244 | &is_log); | |
cc99f275 DB |
2245 | |
2246 | if (is_log) { | |
2247 | bias = VDEV_ALLOC_CLASS_LOGS; | |
2248 | } else { | |
2249 | (void) nvlist_lookup_string(child[c], | |
2250 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
2251 | (void) nvlist_lookup_string(child[c], | |
2252 | ZPOOL_CONFIG_TYPE, &type); | |
2253 | } | |
2254 | ||
2255 | if (bias == NULL || strcmp(bias, class) != 0) | |
2256 | continue; | |
2257 | if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
9babb374 | 2258 | continue; |
cc99f275 DB |
2259 | |
2260 | if (!printed) { | |
2261 | (void) printf("\t%s\t\n", gettext(class)); | |
2262 | printed = B_TRUE; | |
2263 | } | |
2264 | ||
2265 | char *name = zpool_vdev_name(g_zfs, zhp, child[c], | |
fea33e4e HJ |
2266 | cb->cb_name_flags | VDEV_NAME_TYPE_ID); |
2267 | if (cb->cb_print_status) | |
2268 | print_status_config(zhp, cb, name, child[c], 2, | |
2269 | B_FALSE); | |
9babb374 | 2270 | else |
fea33e4e | 2271 | print_import_config(cb, name, child[c], 2); |
9babb374 BB |
2272 | free(name); |
2273 | } | |
2274 | } | |
428870ff | 2275 | |
34dc7c2f BB |
2276 | /* |
2277 | * Display the status for the given pool. | |
2278 | */ | |
2279 | static void | |
2280 | show_import(nvlist_t *config) | |
2281 | { | |
2282 | uint64_t pool_state; | |
2283 | vdev_stat_t *vs; | |
2284 | char *name; | |
2285 | uint64_t guid; | |
379ca9cf | 2286 | uint64_t hostid = 0; |
34dc7c2f | 2287 | char *msgid; |
379ca9cf OF |
2288 | char *hostname = "unknown"; |
2289 | nvlist_t *nvroot, *nvinfo; | |
731782ec | 2290 | zpool_status_t reason; |
ffe9d382 | 2291 | zpool_errata_t errata; |
34dc7c2f BB |
2292 | const char *health; |
2293 | uint_t vsc; | |
d96eb2b1 | 2294 | char *comment; |
fea33e4e | 2295 | status_cbdata_t cb = { 0 }; |
34dc7c2f BB |
2296 | |
2297 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
2298 | &name) == 0); | |
2299 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
2300 | &guid) == 0); | |
2301 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
2302 | &pool_state) == 0); | |
2303 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
2304 | &nvroot) == 0); | |
2305 | ||
428870ff | 2306 | verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
2307 | (uint64_t **)&vs, &vsc) == 0); |
2308 | health = zpool_state_to_name(vs->vs_state, vs->vs_aux); | |
2309 | ||
ffe9d382 | 2310 | reason = zpool_import_status(config, &msgid, &errata); |
34dc7c2f | 2311 | |
d96eb2b1 DM |
2312 | (void) printf(gettext(" pool: %s\n"), name); |
2313 | (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); | |
2314 | (void) printf(gettext(" state: %s"), health); | |
34dc7c2f BB |
2315 | if (pool_state == POOL_STATE_DESTROYED) |
2316 | (void) printf(gettext(" (DESTROYED)")); | |
2317 | (void) printf("\n"); | |
2318 | ||
2319 | switch (reason) { | |
2320 | case ZPOOL_STATUS_MISSING_DEV_R: | |
2321 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
2322 | case ZPOOL_STATUS_BAD_GUID_SUM: | |
d96eb2b1 DM |
2323 | (void) printf(gettext(" status: One or more devices are " |
2324 | "missing from the system.\n")); | |
34dc7c2f BB |
2325 | break; |
2326 | ||
2327 | case ZPOOL_STATUS_CORRUPT_LABEL_R: | |
2328 | case ZPOOL_STATUS_CORRUPT_LABEL_NR: | |
d96eb2b1 | 2329 | (void) printf(gettext(" status: One or more devices contains " |
34dc7c2f BB |
2330 | "corrupted data.\n")); |
2331 | break; | |
2332 | ||
2333 | case ZPOOL_STATUS_CORRUPT_DATA: | |
d96eb2b1 DM |
2334 | (void) printf( |
2335 | gettext(" status: The pool data is corrupted.\n")); | |
34dc7c2f BB |
2336 | break; |
2337 | ||
2338 | case ZPOOL_STATUS_OFFLINE_DEV: | |
d96eb2b1 | 2339 | (void) printf(gettext(" status: One or more devices " |
34dc7c2f BB |
2340 | "are offlined.\n")); |
2341 | break; | |
2342 | ||
2343 | case ZPOOL_STATUS_CORRUPT_POOL: | |
d96eb2b1 | 2344 | (void) printf(gettext(" status: The pool metadata is " |
34dc7c2f BB |
2345 | "corrupted.\n")); |
2346 | break; | |
2347 | ||
2348 | case ZPOOL_STATUS_VERSION_OLDER: | |
b9b24bb4 CS |
2349 | (void) printf(gettext(" status: The pool is formatted using a " |
2350 | "legacy on-disk version.\n")); | |
34dc7c2f BB |
2351 | break; |
2352 | ||
2353 | case ZPOOL_STATUS_VERSION_NEWER: | |
d96eb2b1 | 2354 | (void) printf(gettext(" status: The pool is formatted using an " |
34dc7c2f BB |
2355 | "incompatible version.\n")); |
2356 | break; | |
b128c09f | 2357 | |
b9b24bb4 CS |
2358 | case ZPOOL_STATUS_FEAT_DISABLED: |
2359 | (void) printf(gettext(" status: Some supported features are " | |
2360 | "not enabled on the pool.\n")); | |
2361 | break; | |
2362 | ||
9ae529ec CS |
2363 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
2364 | (void) printf(gettext("status: The pool uses the following " | |
4e33ba4c | 2365 | "feature(s) not supported on this system:\n")); |
9ae529ec CS |
2366 | zpool_print_unsup_feat(config); |
2367 | break; | |
2368 | ||
2369 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
2370 | (void) printf(gettext("status: The pool can only be accessed " | |
2371 | "in read-only mode on this system. It\n\tcannot be " | |
2372 | "accessed in read-write mode because it uses the " | |
2373 | "following\n\tfeature(s) not supported on this system:\n")); | |
2374 | zpool_print_unsup_feat(config); | |
2375 | break; | |
2376 | ||
379ca9cf OF |
2377 | case ZPOOL_STATUS_HOSTID_ACTIVE: |
2378 | (void) printf(gettext(" status: The pool is currently " | |
2379 | "imported by another system.\n")); | |
2380 | break; | |
2381 | ||
2382 | case ZPOOL_STATUS_HOSTID_REQUIRED: | |
2383 | (void) printf(gettext(" status: The pool has the " | |
2384 | "multihost property on. It cannot\n\tbe safely imported " | |
2385 | "when the system hostid is not set.\n")); | |
2386 | break; | |
2387 | ||
34dc7c2f | 2388 | case ZPOOL_STATUS_HOSTID_MISMATCH: |
d96eb2b1 | 2389 | (void) printf(gettext(" status: The pool was last accessed by " |
34dc7c2f BB |
2390 | "another system.\n")); |
2391 | break; | |
b128c09f | 2392 | |
34dc7c2f BB |
2393 | case ZPOOL_STATUS_FAULTED_DEV_R: |
2394 | case ZPOOL_STATUS_FAULTED_DEV_NR: | |
d96eb2b1 | 2395 | (void) printf(gettext(" status: One or more devices are " |
34dc7c2f BB |
2396 | "faulted.\n")); |
2397 | break; | |
2398 | ||
b128c09f | 2399 | case ZPOOL_STATUS_BAD_LOG: |
d96eb2b1 | 2400 | (void) printf(gettext(" status: An intent log record cannot be " |
b128c09f BB |
2401 | "read.\n")); |
2402 | break; | |
2403 | ||
428870ff | 2404 | case ZPOOL_STATUS_RESILVERING: |
d96eb2b1 | 2405 | (void) printf(gettext(" status: One or more devices were being " |
428870ff BB |
2406 | "resilvered.\n")); |
2407 | break; | |
2408 | ||
ffe9d382 BB |
2409 | case ZPOOL_STATUS_ERRATA: |
2410 | (void) printf(gettext(" status: Errata #%d detected.\n"), | |
2411 | errata); | |
2412 | break; | |
2413 | ||
34dc7c2f BB |
2414 | default: |
2415 | /* | |
2416 | * No other status can be seen when importing pools. | |
2417 | */ | |
2418 | assert(reason == ZPOOL_STATUS_OK); | |
2419 | } | |
2420 | ||
2421 | /* | |
2422 | * Print out an action according to the overall state of the pool. | |
2423 | */ | |
2424 | if (vs->vs_state == VDEV_STATE_HEALTHY) { | |
b9b24bb4 CS |
2425 | if (reason == ZPOOL_STATUS_VERSION_OLDER || |
2426 | reason == ZPOOL_STATUS_FEAT_DISABLED) { | |
d96eb2b1 | 2427 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
2428 | "imported using its name or numeric identifier, " |
2429 | "though\n\tsome features will not be available " | |
2430 | "without an explicit 'zpool upgrade'.\n")); | |
b9b24bb4 | 2431 | } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { |
d96eb2b1 | 2432 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
2433 | "imported using its name or numeric " |
2434 | "identifier and\n\tthe '-f' flag.\n")); | |
ffe9d382 BB |
2435 | } else if (reason == ZPOOL_STATUS_ERRATA) { |
2436 | switch (errata) { | |
2437 | case ZPOOL_ERRATA_NONE: | |
2438 | break; | |
2439 | ||
4f2dcb3e RY |
2440 | case ZPOOL_ERRATA_ZOL_2094_SCRUB: |
2441 | (void) printf(gettext(" action: The pool can " | |
2442 | "be imported using its name or numeric " | |
2443 | "identifier,\n\thowever there is a compat" | |
2444 | "ibility issue which should be corrected" | |
2445 | "\n\tby running 'zpool scrub'\n")); | |
2446 | break; | |
2447 | ||
2448 | case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY: | |
2449 | (void) printf(gettext(" action: The pool can" | |
2450 | "not be imported with this version of ZFS " | |
2451 | "due to\n\tan active asynchronous destroy. " | |
2452 | "Revert to an earlier version\n\tand " | |
2453 | "allow the destroy to complete before " | |
2454 | "updating.\n")); | |
2455 | break; | |
2456 | ||
ae76f45c TC |
2457 | case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: |
2458 | (void) printf(gettext(" action: Existing " | |
2459 | "encrypted datasets contain an on-disk " | |
2460 | "incompatibility, which\n\tneeds to be " | |
2461 | "corrected. Backup these datasets to new " | |
2462 | "encrypted datasets\n\tand destroy the " | |
2463 | "old ones.\n")); | |
2464 | break; | |
2465 | ||
ffe9d382 BB |
2466 | default: |
2467 | /* | |
2468 | * All errata must contain an action message. | |
2469 | */ | |
2470 | assert(0); | |
2471 | } | |
b9b24bb4 | 2472 | } else { |
d96eb2b1 | 2473 | (void) printf(gettext(" action: The pool can be " |
34dc7c2f BB |
2474 | "imported using its name or numeric " |
2475 | "identifier.\n")); | |
b9b24bb4 | 2476 | } |
34dc7c2f | 2477 | } else if (vs->vs_state == VDEV_STATE_DEGRADED) { |
d96eb2b1 | 2478 | (void) printf(gettext(" action: The pool can be imported " |
34dc7c2f BB |
2479 | "despite missing or damaged devices. The\n\tfault " |
2480 | "tolerance of the pool may be compromised if imported.\n")); | |
2481 | } else { | |
2482 | switch (reason) { | |
2483 | case ZPOOL_STATUS_VERSION_NEWER: | |
d96eb2b1 | 2484 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
2485 | "imported. Access the pool on a system running " |
2486 | "newer\n\tsoftware, or recreate the pool from " | |
2487 | "backup.\n")); | |
2488 | break; | |
9ae529ec CS |
2489 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
2490 | (void) printf(gettext("action: The pool cannot be " | |
2491 | "imported. Access the pool on a system that " | |
2492 | "supports\n\tthe required feature(s), or recreate " | |
2493 | "the pool from backup.\n")); | |
2494 | break; | |
2495 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
2496 | (void) printf(gettext("action: The pool cannot be " | |
2497 | "imported in read-write mode. Import the pool " | |
2498 | "with\n" | |
2499 | "\t\"-o readonly=on\", access the pool on a system " | |
2500 | "that supports the\n\trequired feature(s), or " | |
2501 | "recreate the pool from backup.\n")); | |
2502 | break; | |
34dc7c2f BB |
2503 | case ZPOOL_STATUS_MISSING_DEV_R: |
2504 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
2505 | case ZPOOL_STATUS_BAD_GUID_SUM: | |
d96eb2b1 | 2506 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
2507 | "imported. Attach the missing\n\tdevices and try " |
2508 | "again.\n")); | |
2509 | break; | |
379ca9cf OF |
2510 | case ZPOOL_STATUS_HOSTID_ACTIVE: |
2511 | VERIFY0(nvlist_lookup_nvlist(config, | |
2512 | ZPOOL_CONFIG_LOAD_INFO, &nvinfo)); | |
2513 | ||
2514 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) | |
2515 | hostname = fnvlist_lookup_string(nvinfo, | |
2516 | ZPOOL_CONFIG_MMP_HOSTNAME); | |
2517 | ||
2518 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) | |
2519 | hostid = fnvlist_lookup_uint64(nvinfo, | |
2520 | ZPOOL_CONFIG_MMP_HOSTID); | |
2521 | ||
2522 | (void) printf(gettext(" action: The pool must be " | |
2523 | "exported from %s (hostid=%lx)\n\tbefore it " | |
2524 | "can be safely imported.\n"), hostname, | |
2525 | (unsigned long) hostid); | |
2526 | break; | |
2527 | case ZPOOL_STATUS_HOSTID_REQUIRED: | |
2528 | (void) printf(gettext(" action: Set a unique system " | |
b9373170 | 2529 | "hostid with the zgenhostid(8) command.\n")); |
379ca9cf | 2530 | break; |
34dc7c2f | 2531 | default: |
d96eb2b1 | 2532 | (void) printf(gettext(" action: The pool cannot be " |
34dc7c2f BB |
2533 | "imported due to damaged devices or data.\n")); |
2534 | } | |
2535 | } | |
2536 | ||
d96eb2b1 DM |
2537 | /* Print the comment attached to the pool. */ |
2538 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
2539 | (void) printf(gettext("comment: %s\n"), comment); | |
2540 | ||
34dc7c2f BB |
2541 | /* |
2542 | * If the state is "closed" or "can't open", and the aux state | |
2543 | * is "corrupt data": | |
2544 | */ | |
2545 | if (((vs->vs_state == VDEV_STATE_CLOSED) || | |
2546 | (vs->vs_state == VDEV_STATE_CANT_OPEN)) && | |
2547 | (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { | |
2548 | if (pool_state == POOL_STATE_DESTROYED) | |
2549 | (void) printf(gettext("\tThe pool was destroyed, " | |
2550 | "but can be imported using the '-Df' flags.\n")); | |
2551 | else if (pool_state != POOL_STATE_EXPORTED) | |
2552 | (void) printf(gettext("\tThe pool may be active on " | |
2553 | "another system, but can be imported using\n\t" | |
2554 | "the '-f' flag.\n")); | |
2555 | } | |
2556 | ||
2557 | if (msgid != NULL) | |
3cee2262 | 2558 | (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"), |
34dc7c2f BB |
2559 | msgid); |
2560 | ||
d96eb2b1 | 2561 | (void) printf(gettext(" config:\n\n")); |
34dc7c2f | 2562 | |
2df9ad1c GG |
2563 | cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name), |
2564 | VDEV_NAME_TYPE_ID); | |
fea33e4e HJ |
2565 | if (cb.cb_namewidth < 10) |
2566 | cb.cb_namewidth = 10; | |
34dc7c2f | 2567 | |
fea33e4e | 2568 | print_import_config(&cb, name, nvroot, 0); |
cc99f275 DB |
2569 | |
2570 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP); | |
2571 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL); | |
2572 | print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS); | |
34dc7c2f BB |
2573 | |
2574 | if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { | |
2575 | (void) printf(gettext("\n\tAdditional devices are known to " | |
2576 | "be part of this pool, though their\n\texact " | |
2577 | "configuration cannot be determined.\n")); | |
2578 | } | |
2579 | } | |
2580 | ||
379ca9cf OF |
2581 | static boolean_t |
2582 | zfs_force_import_required(nvlist_t *config) | |
2583 | { | |
2584 | uint64_t state; | |
2585 | uint64_t hostid = 0; | |
2586 | nvlist_t *nvinfo; | |
2587 | ||
2588 | state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE); | |
2589 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid); | |
2590 | ||
2591 | if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid()) | |
2592 | return (B_TRUE); | |
2593 | ||
2594 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
2595 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) { | |
2596 | mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo, | |
2597 | ZPOOL_CONFIG_MMP_STATE); | |
2598 | ||
2599 | if (mmp_state != MMP_STATE_INACTIVE) | |
2600 | return (B_TRUE); | |
2601 | } | |
2602 | ||
2603 | return (B_FALSE); | |
2604 | } | |
2605 | ||
34dc7c2f BB |
2606 | /* |
2607 | * Perform the import for the given configuration. This passes the heavy | |
2608 | * lifting off to zpool_import_props(), and then mounts the datasets contained | |
2609 | * within the pool. | |
2610 | */ | |
2611 | static int | |
2612 | do_import(nvlist_t *config, const char *newname, const char *mntopts, | |
572e2857 | 2613 | nvlist_t *props, int flags) |
34dc7c2f | 2614 | { |
b5256303 | 2615 | int ret = 0; |
34dc7c2f BB |
2616 | zpool_handle_t *zhp; |
2617 | char *name; | |
34dc7c2f | 2618 | uint64_t version; |
34dc7c2f | 2619 | |
379ca9cf | 2620 | name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME); |
379ca9cf | 2621 | version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION); |
34dc7c2f | 2622 | |
9ae529ec | 2623 | if (!SPA_VERSION_IS_SUPPORTED(version)) { |
34dc7c2f | 2624 | (void) fprintf(stderr, gettext("cannot import '%s': pool " |
9ae529ec | 2625 | "is formatted using an unsupported ZFS version\n"), name); |
34dc7c2f | 2626 | return (1); |
379ca9cf | 2627 | } else if (zfs_force_import_required(config) && |
572e2857 | 2628 | !(flags & ZFS_IMPORT_ANY_HOST)) { |
379ca9cf OF |
2629 | mmp_state_t mmp_state = MMP_STATE_INACTIVE; |
2630 | nvlist_t *nvinfo; | |
2631 | ||
2632 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
2633 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) | |
2634 | mmp_state = fnvlist_lookup_uint64(nvinfo, | |
2635 | ZPOOL_CONFIG_MMP_STATE); | |
2636 | ||
2637 | if (mmp_state == MMP_STATE_ACTIVE) { | |
2638 | char *hostname = "<unknown>"; | |
2639 | uint64_t hostid = 0; | |
2640 | ||
2641 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME)) | |
2642 | hostname = fnvlist_lookup_string(nvinfo, | |
2643 | ZPOOL_CONFIG_MMP_HOSTNAME); | |
2644 | ||
2645 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID)) | |
2646 | hostid = fnvlist_lookup_uint64(nvinfo, | |
2647 | ZPOOL_CONFIG_MMP_HOSTID); | |
2648 | ||
2649 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
2650 | "pool is imported on %s (hostid: " | |
2651 | "0x%lx)\nExport the pool on the other system, " | |
2652 | "then run 'zpool import'.\n"), | |
2653 | name, hostname, (unsigned long) hostid); | |
2654 | } else if (mmp_state == MMP_STATE_NO_HOSTID) { | |
2655 | (void) fprintf(stderr, gettext("Cannot import '%s': " | |
2656 | "pool has the multihost property on and the\n" | |
2657 | "system's hostid is not set. Set a unique hostid " | |
b9373170 | 2658 | "with the zgenhostid(8) command.\n"), name); |
379ca9cf OF |
2659 | } else { |
2660 | char *hostname = "<unknown>"; | |
2661 | uint64_t timestamp = 0; | |
2662 | uint64_t hostid = 0; | |
2663 | ||
2664 | if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME)) | |
2665 | hostname = fnvlist_lookup_string(config, | |
2666 | ZPOOL_CONFIG_HOSTNAME); | |
2667 | ||
2668 | if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP)) | |
2669 | timestamp = fnvlist_lookup_uint64(config, | |
2670 | ZPOOL_CONFIG_TIMESTAMP); | |
2671 | ||
2672 | if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID)) | |
2673 | hostid = fnvlist_lookup_uint64(config, | |
2674 | ZPOOL_CONFIG_HOSTID); | |
2675 | ||
2676 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
2677 | "pool was previously in use from another system.\n" | |
2678 | "Last accessed by %s (hostid=%lx) at %s" | |
2679 | "The pool can be imported, use 'zpool import -f' " | |
2680 | "to import the pool.\n"), name, hostname, | |
2681 | (unsigned long)hostid, ctime((time_t *)×tamp)); | |
34dc7c2f | 2682 | } |
379ca9cf OF |
2683 | |
2684 | return (1); | |
34dc7c2f BB |
2685 | } |
2686 | ||
572e2857 | 2687 | if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) |
34dc7c2f BB |
2688 | return (1); |
2689 | ||
2690 | if (newname != NULL) | |
2691 | name = (char *)newname; | |
2692 | ||
45d1cae3 BB |
2693 | if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) |
2694 | return (1); | |
34dc7c2f | 2695 | |
b5256303 TC |
2696 | /* |
2697 | * Loading keys is best effort. We don't want to return immediately | |
2698 | * if it fails but we do want to give the error to the caller. | |
2699 | */ | |
2700 | if (flags & ZFS_IMPORT_LOAD_KEYS) { | |
2701 | ret = zfs_crypto_attempt_load_keys(g_zfs, name); | |
2702 | if (ret != 0) | |
2703 | ret = 1; | |
2704 | } | |
2705 | ||
d164b209 | 2706 | if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && |
572e2857 | 2707 | !(flags & ZFS_IMPORT_ONLY) && |
d164b209 | 2708 | zpool_enable_datasets(zhp, mntopts, 0) != 0) { |
34dc7c2f BB |
2709 | zpool_close(zhp); |
2710 | return (1); | |
2711 | } | |
2712 | ||
2713 | zpool_close(zhp); | |
b5256303 | 2714 | return (ret); |
34dc7c2f BB |
2715 | } |
2716 | ||
e89f1295 DB |
2717 | typedef struct target_exists_args { |
2718 | const char *poolname; | |
2719 | uint64_t poolguid; | |
2720 | } target_exists_args_t; | |
2721 | ||
2722 | static int | |
2723 | name_or_guid_exists(zpool_handle_t *zhp, void *data) | |
2724 | { | |
2725 | target_exists_args_t *args = data; | |
2726 | nvlist_t *config = zpool_get_config(zhp, NULL); | |
2727 | int found = 0; | |
2728 | ||
2729 | if (config == NULL) | |
2730 | return (0); | |
2731 | ||
2732 | if (args->poolname != NULL) { | |
2733 | char *pool_name; | |
2734 | ||
2735 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
2736 | &pool_name) == 0); | |
2737 | if (strcmp(pool_name, args->poolname) == 0) | |
2738 | found = 1; | |
2739 | } else { | |
2740 | uint64_t pool_guid; | |
2741 | ||
2742 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
2743 | &pool_guid) == 0); | |
2744 | if (pool_guid == args->poolguid) | |
2745 | found = 1; | |
2746 | } | |
2747 | zpool_close(zhp); | |
2748 | ||
2749 | return (found); | |
2750 | } | |
d2734cce SD |
2751 | /* |
2752 | * zpool checkpoint <pool> | |
2753 | * checkpoint --discard <pool> | |
2754 | * | |
2755 | * -d Discard the checkpoint from a checkpointed | |
2756 | * --discard pool. | |
2757 | * | |
2758 | * Checkpoints the specified pool, by taking a "snapshot" of its | |
2759 | * current state. A pool can only have one checkpoint at a time. | |
2760 | */ | |
2761 | int | |
2762 | zpool_do_checkpoint(int argc, char **argv) | |
2763 | { | |
2764 | boolean_t discard; | |
2765 | char *pool; | |
2766 | zpool_handle_t *zhp; | |
2767 | int c, err; | |
2768 | ||
2769 | struct option long_options[] = { | |
2770 | {"discard", no_argument, NULL, 'd'}, | |
2771 | {0, 0, 0, 0} | |
2772 | }; | |
2773 | ||
2774 | discard = B_FALSE; | |
2775 | while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) { | |
2776 | switch (c) { | |
2777 | case 'd': | |
2778 | discard = B_TRUE; | |
2779 | break; | |
2780 | case '?': | |
2781 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
2782 | optopt); | |
2783 | usage(B_FALSE); | |
2784 | } | |
2785 | } | |
2786 | ||
2787 | argc -= optind; | |
2788 | argv += optind; | |
2789 | ||
2790 | if (argc < 1) { | |
2791 | (void) fprintf(stderr, gettext("missing pool argument\n")); | |
2792 | usage(B_FALSE); | |
2793 | } | |
2794 | ||
2795 | if (argc > 1) { | |
2796 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
2797 | usage(B_FALSE); | |
2798 | } | |
2799 | ||
2800 | pool = argv[0]; | |
2801 | ||
2802 | if ((zhp = zpool_open(g_zfs, pool)) == NULL) { | |
2803 | /* As a special case, check for use of '/' in the name */ | |
2804 | if (strchr(pool, '/') != NULL) | |
2805 | (void) fprintf(stderr, gettext("'zpool checkpoint' " | |
2806 | "doesn't work on datasets. To save the state " | |
2807 | "of a dataset from a specific point in time " | |
2808 | "please use 'zfs snapshot'\n")); | |
2809 | return (1); | |
2810 | } | |
2811 | ||
2812 | if (discard) | |
2813 | err = (zpool_discard_checkpoint(zhp) != 0); | |
2814 | else | |
2815 | err = (zpool_checkpoint(zhp) != 0); | |
2816 | ||
2817 | zpool_close(zhp); | |
2818 | ||
2819 | return (err); | |
2820 | } | |
2821 | ||
2822 | #define CHECKPOINT_OPT 1024 | |
2823 | ||
34dc7c2f BB |
2824 | /* |
2825 | * zpool import [-d dir] [-D] | |
b5256303 | 2826 | * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] |
34dc7c2f | 2827 | * [-d dir | -c cachefile] [-f] -a |
b5256303 | 2828 | * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l] |
428870ff | 2829 | * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool] |
34dc7c2f BB |
2830 | * |
2831 | * -c Read pool information from a cachefile instead of searching | |
2832 | * devices. | |
2833 | * | |
d603ed6c | 2834 | * -d Scan in a specific directory, other than /dev/. More than |
34dc7c2f BB |
2835 | * one directory can be specified using multiple '-d' options. |
2836 | * | |
2837 | * -D Scan for previously destroyed pools or import all or only | |
2838 | * specified destroyed pools. | |
2839 | * | |
2840 | * -R Temporarily import the pool, with all mountpoints relative to | |
2841 | * the given root. The pool will remain exported when the machine | |
2842 | * is rebooted. | |
2843 | * | |
428870ff | 2844 | * -V Import even in the presence of faulted vdevs. This is an |
b128c09f BB |
2845 | * intentionally undocumented option for testing purposes, and |
2846 | * treats the pool configuration as complete, leaving any bad | |
45d1cae3 BB |
2847 | * vdevs in the FAULTED state. In other words, it does verbatim |
2848 | * import. | |
b128c09f | 2849 | * |
428870ff BB |
2850 | * -f Force import, even if it appears that the pool is active. |
2851 | * | |
2852 | * -F Attempt rewind if necessary. | |
2853 | * | |
2854 | * -n See if rewind would work, but don't actually rewind. | |
2855 | * | |
572e2857 BB |
2856 | * -N Import the pool but don't mount datasets. |
2857 | * | |
2858 | * -T Specify a starting txg to use for import. This option is | |
2859 | * intentionally undocumented option for testing purposes. | |
2860 | * | |
34dc7c2f BB |
2861 | * -a Import all pools found. |
2862 | * | |
b5256303 TC |
2863 | * -l Load encryption keys while importing. |
2864 | * | |
34dc7c2f BB |
2865 | * -o Set property=value and/or temporary mount options (without '='). |
2866 | * | |
7d11e37e BB |
2867 | * -s Scan using the default search path, the libblkid cache will |
2868 | * not be consulted. | |
2869 | * | |
d2734cce SD |
2870 | * --rewind-to-checkpoint |
2871 | * Import the pool and revert back to the checkpoint. | |
2872 | * | |
34dc7c2f BB |
2873 | * The import command scans for pools to import, and import pools based on pool |
2874 | * name and GUID. The pool can also be renamed as part of the import process. | |
2875 | */ | |
2876 | int | |
2877 | zpool_do_import(int argc, char **argv) | |
2878 | { | |
2879 | char **searchdirs = NULL; | |
44867b6d | 2880 | char *env, *envdup = NULL; |
34dc7c2f BB |
2881 | int nsearch = 0; |
2882 | int c; | |
428870ff | 2883 | int err = 0; |
34dc7c2f BB |
2884 | nvlist_t *pools = NULL; |
2885 | boolean_t do_all = B_FALSE; | |
2886 | boolean_t do_destroyed = B_FALSE; | |
2887 | char *mntopts = NULL; | |
34dc7c2f BB |
2888 | nvpair_t *elem; |
2889 | nvlist_t *config; | |
b128c09f BB |
2890 | uint64_t searchguid = 0; |
2891 | char *searchname = NULL; | |
34dc7c2f BB |
2892 | char *propval; |
2893 | nvlist_t *found_config; | |
428870ff | 2894 | nvlist_t *policy = NULL; |
34dc7c2f BB |
2895 | nvlist_t *props = NULL; |
2896 | boolean_t first; | |
572e2857 | 2897 | int flags = ZFS_IMPORT_NORMAL; |
428870ff BB |
2898 | uint32_t rewind_policy = ZPOOL_NO_REWIND; |
2899 | boolean_t dryrun = B_FALSE; | |
2900 | boolean_t do_rewind = B_FALSE; | |
2901 | boolean_t xtreme_rewind = B_FALSE; | |
7d11e37e | 2902 | boolean_t do_scan = B_FALSE; |
e89f1295 | 2903 | boolean_t pool_exists = B_FALSE; |
572e2857 | 2904 | uint64_t pool_state, txg = -1ULL; |
34dc7c2f | 2905 | char *cachefile = NULL; |
428870ff | 2906 | importargs_t idata = { 0 }; |
572e2857 | 2907 | char *endptr; |
34dc7c2f | 2908 | |
d2734cce SD |
2909 | struct option long_options[] = { |
2910 | {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT}, | |
2911 | {0, 0, 0, 0} | |
2912 | }; | |
2913 | ||
34dc7c2f | 2914 | /* check options */ |
d2734cce SD |
2915 | while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX", |
2916 | long_options, NULL)) != -1) { | |
34dc7c2f BB |
2917 | switch (c) { |
2918 | case 'a': | |
2919 | do_all = B_TRUE; | |
2920 | break; | |
2921 | case 'c': | |
2922 | cachefile = optarg; | |
2923 | break; | |
2924 | case 'd': | |
2925 | if (searchdirs == NULL) { | |
2926 | searchdirs = safe_malloc(sizeof (char *)); | |
2927 | } else { | |
2928 | char **tmp = safe_malloc((nsearch + 1) * | |
2929 | sizeof (char *)); | |
2930 | bcopy(searchdirs, tmp, nsearch * | |
2931 | sizeof (char *)); | |
2932 | free(searchdirs); | |
2933 | searchdirs = tmp; | |
2934 | } | |
2935 | searchdirs[nsearch++] = optarg; | |
2936 | break; | |
2937 | case 'D': | |
2938 | do_destroyed = B_TRUE; | |
2939 | break; | |
2940 | case 'f': | |
572e2857 | 2941 | flags |= ZFS_IMPORT_ANY_HOST; |
34dc7c2f | 2942 | break; |
b128c09f | 2943 | case 'F': |
428870ff BB |
2944 | do_rewind = B_TRUE; |
2945 | break; | |
b5256303 TC |
2946 | case 'l': |
2947 | flags |= ZFS_IMPORT_LOAD_KEYS; | |
2948 | break; | |
572e2857 BB |
2949 | case 'm': |
2950 | flags |= ZFS_IMPORT_MISSING_LOG; | |
2951 | break; | |
428870ff BB |
2952 | case 'n': |
2953 | dryrun = B_TRUE; | |
b128c09f | 2954 | break; |
572e2857 BB |
2955 | case 'N': |
2956 | flags |= ZFS_IMPORT_ONLY; | |
2957 | break; | |
34dc7c2f BB |
2958 | case 'o': |
2959 | if ((propval = strchr(optarg, '=')) != NULL) { | |
2960 | *propval = '\0'; | |
2961 | propval++; | |
b128c09f BB |
2962 | if (add_prop_list(optarg, propval, |
2963 | &props, B_TRUE)) | |
34dc7c2f BB |
2964 | goto error; |
2965 | } else { | |
2966 | mntopts = optarg; | |
2967 | } | |
2968 | break; | |
2969 | case 'R': | |
2970 | if (add_prop_list(zpool_prop_to_name( | |
b128c09f | 2971 | ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) |
34dc7c2f | 2972 | goto error; |
2f3ec900 | 2973 | if (add_prop_list_default(zpool_prop_to_name( |
b128c09f | 2974 | ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) |
34dc7c2f BB |
2975 | goto error; |
2976 | break; | |
7d11e37e BB |
2977 | case 's': |
2978 | do_scan = B_TRUE; | |
2979 | break; | |
26b42f3f RY |
2980 | case 't': |
2981 | flags |= ZFS_IMPORT_TEMP_NAME; | |
00d2a8c9 RY |
2982 | if (add_prop_list_default(zpool_prop_to_name( |
2983 | ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) | |
2984 | goto error; | |
26b42f3f RY |
2985 | break; |
2986 | ||
572e2857 BB |
2987 | case 'T': |
2988 | errno = 0; | |
dea377c0 | 2989 | txg = strtoull(optarg, &endptr, 0); |
572e2857 BB |
2990 | if (errno != 0 || *endptr != '\0') { |
2991 | (void) fprintf(stderr, | |
2992 | gettext("invalid txg value\n")); | |
2993 | usage(B_FALSE); | |
2994 | } | |
2995 | rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; | |
2996 | break; | |
428870ff | 2997 | case 'V': |
572e2857 | 2998 | flags |= ZFS_IMPORT_VERBATIM; |
428870ff BB |
2999 | break; |
3000 | case 'X': | |
3001 | xtreme_rewind = B_TRUE; | |
3002 | break; | |
d2734cce SD |
3003 | case CHECKPOINT_OPT: |
3004 | flags |= ZFS_IMPORT_CHECKPOINT; | |
3005 | break; | |
34dc7c2f BB |
3006 | case ':': |
3007 | (void) fprintf(stderr, gettext("missing argument for " | |
3008 | "'%c' option\n"), optopt); | |
3009 | usage(B_FALSE); | |
3010 | break; | |
3011 | case '?': | |
3012 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3013 | optopt); | |
3014 | usage(B_FALSE); | |
3015 | } | |
3016 | } | |
3017 | ||
3018 | argc -= optind; | |
3019 | argv += optind; | |
3020 | ||
3021 | if (cachefile && nsearch != 0) { | |
3022 | (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); | |
3023 | usage(B_FALSE); | |
3024 | } | |
3025 | ||
b5256303 TC |
3026 | if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) { |
3027 | (void) fprintf(stderr, gettext("-l is incompatible with -N\n")); | |
3028 | usage(B_FALSE); | |
3029 | } | |
3030 | ||
3031 | if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) { | |
3032 | (void) fprintf(stderr, gettext("-l is only meaningful during " | |
3033 | "an import\n")); | |
3034 | usage(B_FALSE); | |
3035 | } | |
3036 | ||
428870ff BB |
3037 | if ((dryrun || xtreme_rewind) && !do_rewind) { |
3038 | (void) fprintf(stderr, | |
3039 | gettext("-n or -X only meaningful with -F\n")); | |
3040 | usage(B_FALSE); | |
3041 | } | |
3042 | if (dryrun) | |
3043 | rewind_policy = ZPOOL_TRY_REWIND; | |
3044 | else if (do_rewind) | |
3045 | rewind_policy = ZPOOL_DO_REWIND; | |
3046 | if (xtreme_rewind) | |
3047 | rewind_policy |= ZPOOL_EXTREME_REWIND; | |
3048 | ||
3049 | /* In the future, we can capture further policy and include it here */ | |
3050 | if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || | |
8a393be3 PZ |
3051 | nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 || |
3052 | nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, | |
3053 | rewind_policy) != 0) | |
428870ff BB |
3054 | goto error; |
3055 | ||
34dc7c2f BB |
3056 | /* check argument count */ |
3057 | if (do_all) { | |
3058 | if (argc != 0) { | |
3059 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
3060 | usage(B_FALSE); | |
3061 | } | |
3062 | } else { | |
3063 | if (argc > 2) { | |
3064 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
3065 | usage(B_FALSE); | |
3066 | } | |
f74b821a | 3067 | } |
34dc7c2f | 3068 | |
f74b821a BB |
3069 | /* |
3070 | * Check for the effective uid. We do this explicitly here because | |
3071 | * otherwise any attempt to discover pools will silently fail. | |
3072 | */ | |
3073 | if (argc == 0 && geteuid() != 0) { | |
3074 | (void) fprintf(stderr, gettext("cannot " | |
3075 | "discover pools: permission denied\n")); | |
3076 | if (searchdirs != NULL) | |
3077 | free(searchdirs); | |
d603ed6c | 3078 | |
a425f5bf | 3079 | nvlist_free(props); |
f74b821a BB |
3080 | nvlist_free(policy); |
3081 | return (1); | |
34dc7c2f BB |
3082 | } |
3083 | ||
34dc7c2f | 3084 | /* |
34dc7c2f BB |
3085 | * Depending on the arguments given, we do one of the following: |
3086 | * | |
3087 | * <none> Iterate through all pools and display information about | |
3088 | * each one. | |
3089 | * | |
3090 | * -a Iterate through all pools and try to import each one. | |
3091 | * | |
3092 | * <id> Find the pool that corresponds to the given GUID/pool | |
3093 | * name and import that one. | |
3094 | * | |
3095 | * -D Above options applies only to destroyed pools. | |
3096 | */ | |
3097 | if (argc != 0) { | |
3098 | char *endptr; | |
3099 | ||
3100 | errno = 0; | |
3101 | searchguid = strtoull(argv[0], &endptr, 10); | |
eaa52d32 | 3102 | if (errno != 0 || *endptr != '\0') { |
34dc7c2f | 3103 | searchname = argv[0]; |
eaa52d32 PJ |
3104 | searchguid = 0; |
3105 | } | |
34dc7c2f | 3106 | found_config = NULL; |
34dc7c2f | 3107 | |
b128c09f | 3108 | /* |
428870ff | 3109 | * User specified a name or guid. Ensure it's unique. |
b128c09f | 3110 | */ |
e89f1295 DB |
3111 | target_exists_args_t search = {searchname, searchguid}; |
3112 | pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search); | |
b128c09f BB |
3113 | } |
3114 | ||
44867b6d BB |
3115 | /* |
3116 | * Check the environment for the preferred search path. | |
3117 | */ | |
3118 | if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) { | |
3119 | char *dir; | |
3120 | ||
3121 | envdup = strdup(env); | |
3122 | ||
3123 | dir = strtok(envdup, ":"); | |
3124 | while (dir != NULL) { | |
3125 | if (searchdirs == NULL) { | |
3126 | searchdirs = safe_malloc(sizeof (char *)); | |
3127 | } else { | |
3128 | char **tmp = safe_malloc((nsearch + 1) * | |
3129 | sizeof (char *)); | |
3130 | bcopy(searchdirs, tmp, nsearch * | |
3131 | sizeof (char *)); | |
3132 | free(searchdirs); | |
3133 | searchdirs = tmp; | |
3134 | } | |
3135 | searchdirs[nsearch++] = dir; | |
3136 | dir = strtok(NULL, ":"); | |
3137 | } | |
3138 | } | |
428870ff BB |
3139 | |
3140 | idata.path = searchdirs; | |
3141 | idata.paths = nsearch; | |
3142 | idata.poolname = searchname; | |
3143 | idata.guid = searchguid; | |
3144 | idata.cachefile = cachefile; | |
7d11e37e | 3145 | idata.scan = do_scan; |
6cb8e530 | 3146 | idata.policy = policy; |
428870ff | 3147 | |
e89f1295 | 3148 | pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops); |
428870ff | 3149 | |
e89f1295 | 3150 | if (pools != NULL && pool_exists && |
428870ff BB |
3151 | (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { |
3152 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3153 | "a pool with that name already exists\n"), | |
3154 | argv[0]); | |
3155 | (void) fprintf(stderr, gettext("use the form '%s " | |
3156 | "<pool | id> <newpool>' to give it a new name\n"), | |
3157 | "zpool import"); | |
3158 | err = 1; | |
e89f1295 | 3159 | } else if (pools == NULL && pool_exists) { |
428870ff BB |
3160 | (void) fprintf(stderr, gettext("cannot import '%s': " |
3161 | "a pool with that name is already created/imported,\n"), | |
3162 | argv[0]); | |
3163 | (void) fprintf(stderr, gettext("and no additional pools " | |
3164 | "with that name were found\n")); | |
3165 | err = 1; | |
3166 | } else if (pools == NULL) { | |
b128c09f BB |
3167 | if (argc != 0) { |
3168 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3169 | "no such pool available\n"), argv[0]); | |
3170 | } | |
428870ff BB |
3171 | err = 1; |
3172 | } | |
3173 | ||
3174 | if (err == 1) { | |
d603ed6c BB |
3175 | if (searchdirs != NULL) |
3176 | free(searchdirs); | |
44867b6d BB |
3177 | if (envdup != NULL) |
3178 | free(envdup); | |
428870ff | 3179 | nvlist_free(policy); |
a425f5bf | 3180 | nvlist_free(pools); |
3181 | nvlist_free(props); | |
b128c09f BB |
3182 | return (1); |
3183 | } | |
3184 | ||
3185 | /* | |
3186 | * At this point we have a list of import candidate configs. Even if | |
3187 | * we were searching by pool name or guid, we still need to | |
3188 | * post-process the list to deal with pool state and possible | |
3189 | * duplicate names. | |
3190 | */ | |
34dc7c2f BB |
3191 | err = 0; |
3192 | elem = NULL; | |
3193 | first = B_TRUE; | |
3194 | while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { | |
3195 | ||
3196 | verify(nvpair_value_nvlist(elem, &config) == 0); | |
3197 | ||
3198 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
3199 | &pool_state) == 0); | |
3200 | if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) | |
3201 | continue; | |
3202 | if (do_destroyed && pool_state != POOL_STATE_DESTROYED) | |
3203 | continue; | |
3204 | ||
8a393be3 | 3205 | verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY, |
428870ff BB |
3206 | policy) == 0); |
3207 | ||
34dc7c2f BB |
3208 | if (argc == 0) { |
3209 | if (first) | |
3210 | first = B_FALSE; | |
3211 | else if (!do_all) | |
3212 | (void) printf("\n"); | |
3213 | ||
428870ff | 3214 | if (do_all) { |
34dc7c2f | 3215 | err |= do_import(config, NULL, mntopts, |
572e2857 | 3216 | props, flags); |
428870ff | 3217 | } else { |
34dc7c2f | 3218 | show_import(config); |
428870ff | 3219 | } |
34dc7c2f BB |
3220 | } else if (searchname != NULL) { |
3221 | char *name; | |
3222 | ||
3223 | /* | |
3224 | * We are searching for a pool based on name. | |
3225 | */ | |
3226 | verify(nvlist_lookup_string(config, | |
3227 | ZPOOL_CONFIG_POOL_NAME, &name) == 0); | |
3228 | ||
3229 | if (strcmp(name, searchname) == 0) { | |
3230 | if (found_config != NULL) { | |
3231 | (void) fprintf(stderr, gettext( | |
3232 | "cannot import '%s': more than " | |
3233 | "one matching pool\n"), searchname); | |
3234 | (void) fprintf(stderr, gettext( | |
3235 | "import by numeric ID instead\n")); | |
3236 | err = B_TRUE; | |
3237 | } | |
3238 | found_config = config; | |
3239 | } | |
3240 | } else { | |
3241 | uint64_t guid; | |
3242 | ||
3243 | /* | |
3244 | * Search for a pool by guid. | |
3245 | */ | |
3246 | verify(nvlist_lookup_uint64(config, | |
3247 | ZPOOL_CONFIG_POOL_GUID, &guid) == 0); | |
3248 | ||
3249 | if (guid == searchguid) | |
3250 | found_config = config; | |
3251 | } | |
3252 | } | |
3253 | ||
3254 | /* | |
3255 | * If we were searching for a specific pool, verify that we found a | |
3256 | * pool, and then do the import. | |
3257 | */ | |
3258 | if (argc != 0 && err == 0) { | |
3259 | if (found_config == NULL) { | |
3260 | (void) fprintf(stderr, gettext("cannot import '%s': " | |
3261 | "no such pool available\n"), argv[0]); | |
3262 | err = B_TRUE; | |
3263 | } else { | |
3264 | err |= do_import(found_config, argc == 1 ? NULL : | |
572e2857 | 3265 | argv[1], mntopts, props, flags); |
34dc7c2f BB |
3266 | } |
3267 | } | |
3268 | ||
3269 | /* | |
3270 | * If we were just looking for pools, report an error if none were | |
3271 | * found. | |
3272 | */ | |
3273 | if (argc == 0 && first) | |
3274 | (void) fprintf(stderr, | |
3275 | gettext("no pools available to import\n")); | |
3276 | ||
3277 | error: | |
3278 | nvlist_free(props); | |
3279 | nvlist_free(pools); | |
428870ff | 3280 | nvlist_free(policy); |
d603ed6c BB |
3281 | if (searchdirs != NULL) |
3282 | free(searchdirs); | |
44867b6d BB |
3283 | if (envdup != NULL) |
3284 | free(envdup); | |
34dc7c2f BB |
3285 | |
3286 | return (err ? 1 : 0); | |
3287 | } | |
3288 | ||
bec1067d AP |
3289 | /* |
3290 | * zpool sync [-f] [pool] ... | |
3291 | * | |
3292 | * -f (undocumented) force uberblock (and config including zpool cache file) | |
3293 | * update. | |
3294 | * | |
3295 | * Sync the specified pool(s). | |
3296 | * Without arguments "zpool sync" will sync all pools. | |
3297 | * This command initiates TXG sync(s) and will return after the TXG(s) commit. | |
3298 | * | |
3299 | */ | |
3300 | static int | |
3301 | zpool_do_sync(int argc, char **argv) | |
3302 | { | |
3303 | int ret; | |
3304 | boolean_t force = B_FALSE; | |
3305 | ||
3306 | /* check options */ | |
3307 | while ((ret = getopt(argc, argv, "f")) != -1) { | |
3308 | switch (ret) { | |
3309 | case 'f': | |
3310 | force = B_TRUE; | |
3311 | break; | |
3312 | case '?': | |
3313 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
3314 | optopt); | |
3315 | usage(B_FALSE); | |
3316 | } | |
3317 | } | |
3318 | ||
3319 | argc -= optind; | |
3320 | argv += optind; | |
3321 | ||
3322 | /* if argc == 0 we will execute zpool_sync_one on all pools */ | |
3323 | ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force); | |
3324 | ||
3325 | return (ret); | |
3326 | } | |
3327 | ||
34dc7c2f | 3328 | typedef struct iostat_cbdata { |
193a37cb | 3329 | uint64_t cb_flags; |
d2f3e292 | 3330 | int cb_name_flags; |
34dc7c2f | 3331 | int cb_namewidth; |
1bd201e7 | 3332 | int cb_iteration; |
193a37cb TH |
3333 | char **cb_vdev_names; /* Only show these vdevs */ |
3334 | unsigned int cb_vdev_names_count; | |
3335 | boolean_t cb_verbose; | |
3336 | boolean_t cb_literal; | |
3337 | boolean_t cb_scripted; | |
1bd201e7 | 3338 | zpool_list_t *cb_list; |
8720e9e7 | 3339 | vdev_cmd_data_list_t *vcdl; |
34dc7c2f BB |
3340 | } iostat_cbdata_t; |
3341 | ||
193a37cb TH |
3342 | /* iostat labels */ |
3343 | typedef struct name_and_columns { | |
3344 | const char *name; /* Column name */ | |
3345 | unsigned int columns; /* Center name to this number of columns */ | |
3346 | } name_and_columns_t; | |
3347 | ||
3348 | #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */ | |
3349 | ||
3350 | static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] = | |
3351 | { | |
3352 | [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2}, | |
3353 | {NULL}}, | |
3354 | [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2}, | |
3355 | {"asyncq_wait", 2}, {"scrub"}}, | |
3356 | [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2}, | |
3357 | {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2}, | |
3358 | {NULL}}, | |
3359 | [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, | |
3360 | {"sync_queue", 2}, {"async_queue", 2}, {NULL}}, | |
7e945072 TH |
3361 | [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2}, |
3362 | {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL}}, | |
3363 | ||
193a37cb TH |
3364 | }; |
3365 | ||
3366 | /* Shorthand - if "columns" field not set, default to 1 column */ | |
3367 | static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] = | |
3368 | { | |
3369 | [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"}, | |
3370 | {"write"}, {NULL}}, | |
3371 | [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, | |
3372 | {"write"}, {"read"}, {"write"}, {"wait"}, {NULL}}, | |
3373 | [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"}, | |
3374 | {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}}, | |
3375 | [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"}, | |
3376 | {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL}}, | |
7e945072 TH |
3377 | [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, |
3378 | {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL}}, | |
3379 | }; | |
3380 | ||
3381 | static const char *histo_to_title[] = { | |
3382 | [IOS_L_HISTO] = "latency", | |
3383 | [IOS_RQ_HISTO] = "req_size", | |
193a37cb TH |
3384 | }; |
3385 | ||
3386 | /* | |
3387 | * Return the number of labels in a null-terminated name_and_columns_t | |
3388 | * array. | |
3389 | * | |
3390 | */ | |
3391 | static unsigned int | |
3392 | label_array_len(const name_and_columns_t *labels) | |
3393 | { | |
3394 | int i = 0; | |
3395 | ||
3396 | while (labels[i].name) | |
3397 | i++; | |
3398 | ||
3399 | return (i); | |
3400 | } | |
3401 | ||
7e945072 TH |
3402 | /* |
3403 | * Return the number of strings in a null-terminated string array. | |
3404 | * For example: | |
3405 | * | |
3406 | * const char foo[] = {"bar", "baz", NULL} | |
3407 | * | |
3408 | * returns 2 | |
3409 | */ | |
3410 | static uint64_t | |
3411 | str_array_len(const char *array[]) | |
3412 | { | |
3413 | uint64_t i = 0; | |
3414 | while (array[i]) | |
3415 | i++; | |
3416 | ||
3417 | return (i); | |
3418 | } | |
3419 | ||
3420 | ||
193a37cb TH |
3421 | /* |
3422 | * Return a default column width for default/latency/queue columns. This does | |
3423 | * not include histograms, which have their columns autosized. | |
3424 | */ | |
3425 | static unsigned int | |
3426 | default_column_width(iostat_cbdata_t *cb, enum iostat_type type) | |
3427 | { | |
3428 | unsigned long column_width = 5; /* Normal niceprint */ | |
3429 | static unsigned long widths[] = { | |
3430 | /* | |
3431 | * Choose some sane default column sizes for printing the | |
3432 | * raw numbers. | |
3433 | */ | |
3434 | [IOS_DEFAULT] = 15, /* 1PB capacity */ | |
3435 | [IOS_LATENCY] = 10, /* 1B ns = 10sec */ | |
3436 | [IOS_QUEUES] = 6, /* 1M queue entries */ | |
3437 | }; | |
3438 | ||
3439 | if (cb->cb_literal) | |
3440 | column_width = widths[type]; | |
3441 | ||
3442 | return (column_width); | |
3443 | } | |
3444 | ||
3445 | /* | |
3446 | * Print the column labels, i.e: | |
3447 | * | |
3448 | * capacity operations bandwidth | |
3449 | * alloc free read write read write ... | |
3450 | * | |
3451 | * If force_column_width is set, use it for the column width. If not set, use | |
3452 | * the default column width. | |
3453 | */ | |
3454 | void | |
3455 | print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width, | |
3456 | const name_and_columns_t labels[][IOSTAT_MAX_LABELS]) | |
3457 | { | |
3458 | int i, idx, s; | |
3459 | unsigned int text_start, rw_column_width, spaces_to_end; | |
3460 | uint64_t flags = cb->cb_flags; | |
3461 | uint64_t f; | |
3462 | unsigned int column_width = force_column_width; | |
3463 | ||
3464 | /* For each bit set in flags */ | |
3465 | for (f = flags; f; f &= ~(1ULL << idx)) { | |
3466 | idx = lowbit64(f) - 1; | |
3467 | if (!force_column_width) | |
3468 | column_width = default_column_width(cb, idx); | |
3469 | /* Print our top labels centered over "read write" label. */ | |
3470 | for (i = 0; i < label_array_len(labels[idx]); i++) { | |
3471 | const char *name = labels[idx][i].name; | |
3472 | /* | |
3473 | * We treat labels[][].columns == 0 as shorthand | |
3474 | * for one column. It makes writing out the label | |
3475 | * tables more concise. | |
3476 | */ | |
3477 | unsigned int columns = MAX(1, labels[idx][i].columns); | |
3478 | unsigned int slen = strlen(name); | |
3479 | ||
3480 | rw_column_width = (column_width * columns) + | |
3481 | (2 * (columns - 1)); | |
3482 | ||
02730c33 | 3483 | text_start = (int)((rw_column_width)/columns - |
193a37cb TH |
3484 | slen/columns); |
3485 | ||
3486 | printf(" "); /* Two spaces between columns */ | |
3487 | ||
3488 | /* Space from beginning of column to label */ | |
3489 | for (s = 0; s < text_start; s++) | |
3490 | printf(" "); | |
3491 | ||
3492 | printf("%s", name); | |
3493 | ||
3494 | /* Print space after label to end of column */ | |
3495 | spaces_to_end = rw_column_width - text_start - slen; | |
3496 | for (s = 0; s < spaces_to_end; s++) | |
3497 | printf(" "); | |
3498 | ||
3499 | } | |
3500 | } | |
193a37cb TH |
3501 | } |
3502 | ||
d6418de0 TH |
3503 | |
3504 | /* | |
3505 | * print_cmd_columns - Print custom column titles from -c | |
3506 | * | |
3507 | * If the user specified the "zpool status|iostat -c" then print their custom | |
3508 | * column titles in the header. For example, print_cmd_columns() would print | |
3509 | * the " col1 col2" part of this: | |
3510 | * | |
3511 | * $ zpool iostat -vc 'echo col1=val1; echo col2=val2' | |
3512 | * ... | |
3513 | * capacity operations bandwidth | |
3514 | * pool alloc free read write read write col1 col2 | |
3515 | * ---------- ----- ----- ----- ----- ----- ----- ---- ---- | |
3516 | * mypool 269K 1008M 0 0 107 946 | |
3517 | * mirror 269K 1008M 0 0 107 946 | |
3518 | * sdb - - 0 0 102 473 val1 val2 | |
3519 | * sdc - - 0 0 5 473 val1 val2 | |
3520 | * ---------- ----- ----- ----- ----- ----- ----- ---- ---- | |
3521 | */ | |
3522 | void | |
3523 | print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes) | |
3524 | { | |
3525 | int i, j; | |
3526 | vdev_cmd_data_t *data = &vcdl->data[0]; | |
3527 | ||
3528 | if (vcdl->count == 0 || data == NULL) | |
3529 | return; | |
3530 | ||
3531 | /* | |
3532 | * Each vdev cmd should have the same column names unless the user did | |
3533 | * something weird with their cmd. Just take the column names from the | |
3534 | * first vdev and assume it works for all of them. | |
3535 | */ | |
3536 | for (i = 0; i < vcdl->uniq_cols_cnt; i++) { | |
3537 | printf(" "); | |
3538 | if (use_dashes) { | |
3539 | for (j = 0; j < vcdl->uniq_cols_width[i]; j++) | |
3540 | printf("-"); | |
3541 | } else { | |
3542 | printf("%*s", vcdl->uniq_cols_width[i], | |
3543 | vcdl->uniq_cols[i]); | |
3544 | } | |
3545 | } | |
3546 | } | |
3547 | ||
3548 | ||
193a37cb TH |
3549 | /* |
3550 | * Utility function to print out a line of dashes like: | |
3551 | * | |
3552 | * -------------------------------- ----- ----- ----- ----- ----- | |
3553 | * | |
3554 | * ...or a dashed named-row line like: | |
3555 | * | |
3556 | * logs - - - - - | |
3557 | * | |
3558 | * @cb: iostat data | |
3559 | * | |
3560 | * @force_column_width If non-zero, use the value as the column width. | |
3561 | * Otherwise use the default column widths. | |
3562 | * | |
3563 | * @name: Print a dashed named-row line starting | |
3564 | * with @name. Otherwise, print a regular | |
3565 | * dashed line. | |
3566 | */ | |
3567 | static void | |
3568 | print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width, | |
3569 | const char *name) | |
3570 | { | |
3571 | int i; | |
3572 | unsigned int namewidth; | |
3573 | uint64_t flags = cb->cb_flags; | |
3574 | uint64_t f; | |
3575 | int idx; | |
3576 | const name_and_columns_t *labels; | |
7e945072 TH |
3577 | const char *title; |
3578 | ||
3579 | ||
3580 | if (cb->cb_flags & IOS_ANYHISTO_M) { | |
3581 | title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; | |
3582 | } else if (cb->cb_vdev_names_count) { | |
3583 | title = "vdev"; | |
3584 | } else { | |
3585 | title = "pool"; | |
3586 | } | |
3587 | ||
3588 | namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), | |
3589 | name ? strlen(name) : 0); | |
193a37cb | 3590 | |
193a37cb TH |
3591 | |
3592 | if (name) { | |
193a37cb TH |
3593 | printf("%-*s", namewidth, name); |
3594 | } else { | |
3595 | for (i = 0; i < namewidth; i++) | |
3596 | (void) printf("-"); | |
3597 | } | |
3598 | ||
3599 | /* For each bit in flags */ | |
3600 | for (f = flags; f; f &= ~(1ULL << idx)) { | |
3601 | unsigned int column_width; | |
3602 | idx = lowbit64(f) - 1; | |
3603 | if (force_column_width) | |
3604 | column_width = force_column_width; | |
3605 | else | |
3606 | column_width = default_column_width(cb, idx); | |
3607 | ||
3608 | labels = iostat_bottom_labels[idx]; | |
3609 | for (i = 0; i < label_array_len(labels); i++) { | |
3610 | if (name) | |
3611 | printf(" %*s-", column_width - 1, " "); | |
3612 | else | |
3613 | printf(" %.*s", column_width, | |
3614 | "--------------------"); | |
3615 | } | |
3616 | } | |
193a37cb TH |
3617 | } |
3618 | ||
3619 | ||
3620 | static void | |
3621 | print_iostat_separator_impl(iostat_cbdata_t *cb, | |
3622 | unsigned int force_column_width) | |
3623 | { | |
3624 | print_iostat_dashes(cb, force_column_width, NULL); | |
3625 | } | |
3626 | ||
34dc7c2f BB |
3627 | static void |
3628 | print_iostat_separator(iostat_cbdata_t *cb) | |
3629 | { | |
193a37cb TH |
3630 | print_iostat_separator_impl(cb, 0); |
3631 | } | |
3632 | ||
3633 | static void | |
3634 | print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width, | |
3635 | const char *histo_vdev_name) | |
3636 | { | |
3637 | unsigned int namewidth; | |
7e945072 TH |
3638 | const char *title; |
3639 | ||
3640 | if (cb->cb_flags & IOS_ANYHISTO_M) { | |
3641 | title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]; | |
3642 | } else if (cb->cb_vdev_names_count) { | |
3643 | title = "vdev"; | |
3644 | } else { | |
3645 | title = "pool"; | |
3646 | } | |
34dc7c2f | 3647 | |
7e945072 TH |
3648 | namewidth = MAX(MAX(strlen(title), cb->cb_namewidth), |
3649 | histo_vdev_name ? strlen(histo_vdev_name) : 0); | |
193a37cb | 3650 | |
7e945072 | 3651 | if (histo_vdev_name) |
193a37cb TH |
3652 | printf("%-*s", namewidth, histo_vdev_name); |
3653 | else | |
3654 | printf("%*s", namewidth, ""); | |
3655 | ||
7e945072 | 3656 | |
193a37cb | 3657 | print_iostat_labels(cb, force_column_width, iostat_top_labels); |
d6418de0 | 3658 | printf("\n"); |
193a37cb | 3659 | |
7e945072 | 3660 | printf("%-*s", namewidth, title); |
193a37cb TH |
3661 | |
3662 | print_iostat_labels(cb, force_column_width, iostat_bottom_labels); | |
d6418de0 TH |
3663 | if (cb->vcdl != NULL) |
3664 | print_cmd_columns(cb->vcdl, 0); | |
3665 | ||
3666 | printf("\n"); | |
193a37cb TH |
3667 | |
3668 | print_iostat_separator_impl(cb, force_column_width); | |
d6418de0 TH |
3669 | |
3670 | if (cb->vcdl != NULL) | |
3671 | print_cmd_columns(cb->vcdl, 1); | |
3672 | ||
3673 | printf("\n"); | |
34dc7c2f BB |
3674 | } |
3675 | ||
3676 | static void | |
3677 | print_iostat_header(iostat_cbdata_t *cb) | |
3678 | { | |
193a37cb | 3679 | print_iostat_header_impl(cb, 0, NULL); |
34dc7c2f BB |
3680 | } |
3681 | ||
193a37cb | 3682 | |
34dc7c2f BB |
3683 | /* |
3684 | * Display a single statistic. | |
3685 | */ | |
3686 | static void | |
193a37cb TH |
3687 | print_one_stat(uint64_t value, enum zfs_nicenum_format format, |
3688 | unsigned int column_size, boolean_t scripted) | |
34dc7c2f BB |
3689 | { |
3690 | char buf[64]; | |
3691 | ||
193a37cb TH |
3692 | zfs_nicenum_format(value, buf, sizeof (buf), format); |
3693 | ||
3694 | if (scripted) | |
3695 | printf("\t%s", buf); | |
3696 | else | |
3697 | printf(" %*s", column_size, buf); | |
3698 | } | |
3699 | ||
3700 | /* | |
3701 | * Calculate the default vdev stats | |
3702 | * | |
3703 | * Subtract oldvs from newvs, apply a scaling factor, and save the resulting | |
3704 | * stats into calcvs. | |
3705 | */ | |
3706 | static void | |
3707 | calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs, | |
3708 | vdev_stat_t *calcvs) | |
3709 | { | |
3710 | int i; | |
3711 | ||
3712 | memcpy(calcvs, newvs, sizeof (*calcvs)); | |
3713 | for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++) | |
3714 | calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]); | |
3715 | ||
3716 | for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++) | |
3717 | calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]); | |
3718 | } | |
3719 | ||
3720 | /* | |
3721 | * Internal representation of the extended iostats data. | |
3722 | * | |
3723 | * The extended iostat stats are exported in nvlists as either uint64_t arrays | |
3724 | * or single uint64_t's. We make both look like arrays to make them easier | |
3725 | * to process. In order to make single uint64_t's look like arrays, we set | |
3726 | * __data to the stat data, and then set *data = &__data with count = 1. Then, | |
3727 | * we can just use *data and count. | |
3728 | */ | |
3729 | struct stat_array { | |
3730 | uint64_t *data; | |
3731 | uint_t count; /* Number of entries in data[] */ | |
3732 | uint64_t __data; /* Only used when data is a single uint64_t */ | |
3733 | }; | |
3734 | ||
3735 | static uint64_t | |
4ea3f864 GM |
3736 | stat_histo_max(struct stat_array *nva, unsigned int len) |
3737 | { | |
193a37cb TH |
3738 | uint64_t max = 0; |
3739 | int i; | |
3740 | for (i = 0; i < len; i++) | |
3741 | max = MAX(max, array64_max(nva[i].data, nva[i].count)); | |
3742 | ||
3743 | return (max); | |
3744 | } | |
3745 | ||
3746 | /* | |
3747 | * Helper function to lookup a uint64_t array or uint64_t value and store its | |
3748 | * data as a stat_array. If the nvpair is a single uint64_t value, then we make | |
3749 | * it look like a one element array to make it easier to process. | |
3750 | */ | |
3751 | static int | |
3752 | nvpair64_to_stat_array(nvlist_t *nvl, const char *name, | |
4ea3f864 GM |
3753 | struct stat_array *nva) |
3754 | { | |
193a37cb TH |
3755 | nvpair_t *tmp; |
3756 | int ret; | |
3757 | ||
3758 | verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0); | |
3759 | switch (nvpair_type(tmp)) { | |
3760 | case DATA_TYPE_UINT64_ARRAY: | |
3761 | ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count); | |
3762 | break; | |
3763 | case DATA_TYPE_UINT64: | |
3764 | ret = nvpair_value_uint64(tmp, &nva->__data); | |
3765 | nva->data = &nva->__data; | |
3766 | nva->count = 1; | |
3767 | break; | |
3768 | default: | |
3769 | /* Not a uint64_t */ | |
3770 | ret = EINVAL; | |
3771 | break; | |
3772 | } | |
3773 | ||
3774 | return (ret); | |
3775 | } | |
3776 | ||
3777 | /* | |
3778 | * Given a list of nvlist names, look up the extended stats in newnv and oldnv, | |
3779 | * subtract them, and return the results in a newly allocated stat_array. | |
3780 | * You must free the returned array after you are done with it with | |
3781 | * free_calc_stats(). | |
3782 | * | |
3783 | * Additionally, you can set "oldnv" to NULL if you simply want the newnv | |
3784 | * values. | |
3785 | */ | |
3786 | static struct stat_array * | |
3787 | calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv, | |
3788 | nvlist_t *newnv) | |
3789 | { | |
3790 | nvlist_t *oldnvx = NULL, *newnvx; | |
3791 | struct stat_array *oldnva, *newnva, *calcnva; | |
3792 | int i, j; | |
3793 | unsigned int alloc_size = (sizeof (struct stat_array)) * len; | |
3794 | ||
3795 | /* Extract our extended stats nvlist from the main list */ | |
3796 | verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX, | |
3797 | &newnvx) == 0); | |
3798 | if (oldnv) { | |
3799 | verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX, | |
3800 | &oldnvx) == 0); | |
3801 | } | |
3802 | ||
3803 | newnva = safe_malloc(alloc_size); | |
3804 | oldnva = safe_malloc(alloc_size); | |
3805 | calcnva = safe_malloc(alloc_size); | |
3806 | ||
3807 | for (j = 0; j < len; j++) { | |
3808 | verify(nvpair64_to_stat_array(newnvx, names[j], | |
3809 | &newnva[j]) == 0); | |
3810 | calcnva[j].count = newnva[j].count; | |
3811 | alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]); | |
3812 | calcnva[j].data = safe_malloc(alloc_size); | |
3813 | memcpy(calcnva[j].data, newnva[j].data, alloc_size); | |
3814 | ||
3815 | if (oldnvx) { | |
3816 | verify(nvpair64_to_stat_array(oldnvx, names[j], | |
3817 | &oldnva[j]) == 0); | |
3818 | for (i = 0; i < oldnva[j].count; i++) | |
3819 | calcnva[j].data[i] -= oldnva[j].data[i]; | |
3820 | } | |
3821 | } | |
3822 | free(newnva); | |
3823 | free(oldnva); | |
3824 | return (calcnva); | |
3825 | } | |
3826 | ||
3827 | static void | |
3828 | free_calc_stats(struct stat_array *nva, unsigned int len) | |
3829 | { | |
3830 | int i; | |
3831 | for (i = 0; i < len; i++) | |
3832 | free(nva[i].data); | |
3833 | ||
3834 | free(nva); | |
3835 | } | |
3836 | ||
3837 | static void | |
3838 | print_iostat_histo(struct stat_array *nva, unsigned int len, | |
3839 | iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth, | |
3840 | double scale) | |
3841 | { | |
3842 | int i, j; | |
3843 | char buf[6]; | |
3844 | uint64_t val; | |
3845 | enum zfs_nicenum_format format; | |
3846 | unsigned int buckets; | |
7e945072 | 3847 | unsigned int start_bucket; |
193a37cb TH |
3848 | |
3849 | if (cb->cb_literal) | |
3850 | format = ZFS_NICENUM_RAW; | |
3851 | else | |
3852 | format = ZFS_NICENUM_1024; | |
3853 | ||
3854 | /* All these histos are the same size, so just use nva[0].count */ | |
3855 | buckets = nva[0].count; | |
3856 | ||
7e945072 TH |
3857 | if (cb->cb_flags & IOS_RQ_HISTO_M) { |
3858 | /* Start at 512 - req size should never be lower than this */ | |
3859 | start_bucket = 9; | |
3860 | } else { | |
3861 | start_bucket = 0; | |
3862 | } | |
193a37cb | 3863 | |
7e945072 | 3864 | for (j = start_bucket; j < buckets; j++) { |
193a37cb | 3865 | /* Print histogram bucket label */ |
7e945072 TH |
3866 | if (cb->cb_flags & IOS_L_HISTO_M) { |
3867 | /* Ending range of this bucket */ | |
3868 | val = (1UL << (j + 1)) - 1; | |
3869 | zfs_nicetime(val, buf, sizeof (buf)); | |
3870 | } else { | |
3871 | /* Request size (starting range of bucket) */ | |
3872 | val = (1UL << j); | |
3873 | zfs_nicenum(val, buf, sizeof (buf)); | |
3874 | } | |
3875 | ||
193a37cb | 3876 | if (cb->cb_scripted) |
02730c33 | 3877 | printf("%llu", (u_longlong_t)val); |
193a37cb TH |
3878 | else |
3879 | printf("%-*s", namewidth, buf); | |
3880 | ||
3881 | /* Print the values on the line */ | |
3882 | for (i = 0; i < len; i++) { | |
3883 | print_one_stat(nva[i].data[j] * scale, format, | |
3884 | column_width, cb->cb_scripted); | |
3885 | } | |
3886 | printf("\n"); | |
3887 | } | |
3888 | } | |
3889 | ||
3890 | static void | |
3891 | print_solid_separator(unsigned int length) | |
3892 | { | |
3893 | while (length--) | |
3894 | printf("-"); | |
3895 | printf("\n"); | |
3896 | } | |
3897 | ||
3898 | static void | |
3899 | print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv, | |
3900 | nvlist_t *newnv, double scale, const char *name) | |
3901 | { | |
3902 | unsigned int column_width; | |
3903 | unsigned int namewidth; | |
3904 | unsigned int entire_width; | |
7e945072 | 3905 | enum iostat_type type; |
193a37cb | 3906 | struct stat_array *nva; |
7e945072 TH |
3907 | const char **names; |
3908 | unsigned int names_len; | |
3909 | ||
3910 | /* What type of histo are we? */ | |
3911 | type = IOS_HISTO_IDX(cb->cb_flags); | |
3912 | ||
3913 | /* Get NULL-terminated array of nvlist names for our histo */ | |
3914 | names = vsx_type_to_nvlist[type]; | |
3915 | names_len = str_array_len(names); /* num of names */ | |
3916 | ||
3917 | nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv); | |
193a37cb TH |
3918 | |
3919 | if (cb->cb_literal) { | |
3920 | column_width = MAX(5, | |
7e945072 | 3921 | (unsigned int) log10(stat_histo_max(nva, names_len)) + 1); |
193a37cb TH |
3922 | } else { |
3923 | column_width = 5; | |
3924 | } | |
3925 | ||
7e945072 TH |
3926 | namewidth = MAX(cb->cb_namewidth, |
3927 | strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)])); | |
193a37cb TH |
3928 | |
3929 | /* | |
3930 | * Calculate the entire line width of what we're printing. The | |
3931 | * +2 is for the two spaces between columns: | |
3932 | */ | |
3933 | /* read write */ | |
3934 | /* ----- ----- */ | |
3935 | /* |___| <---------- column_width */ | |
3936 | /* */ | |
3937 | /* |__________| <--- entire_width */ | |
3938 | /* */ | |
3939 | entire_width = namewidth + (column_width + 2) * | |
7e945072 | 3940 | label_array_len(iostat_bottom_labels[type]); |
193a37cb TH |
3941 | |
3942 | if (cb->cb_scripted) | |
3943 | printf("%s\n", name); | |
3944 | else | |
3945 | print_iostat_header_impl(cb, column_width, name); | |
3946 | ||
7e945072 | 3947 | print_iostat_histo(nva, names_len, cb, column_width, |
193a37cb TH |
3948 | namewidth, scale); |
3949 | ||
7e945072 | 3950 | free_calc_stats(nva, names_len); |
193a37cb TH |
3951 | if (!cb->cb_scripted) |
3952 | print_solid_separator(entire_width); | |
3953 | } | |
3954 | ||
3955 | /* | |
3956 | * Calculate the average latency of a power-of-two latency histogram | |
3957 | */ | |
3958 | static uint64_t | |
3959 | single_histo_average(uint64_t *histo, unsigned int buckets) | |
3960 | { | |
3961 | int i; | |
3962 | uint64_t count = 0, total = 0; | |
3963 | ||
3964 | for (i = 0; i < buckets; i++) { | |
3965 | /* | |
3966 | * Our buckets are power-of-two latency ranges. Use the | |
3967 | * midpoint latency of each bucket to calculate the average. | |
3968 | * For example: | |
3969 | * | |
3970 | * Bucket Midpoint | |
3971 | * 8ns-15ns: 12ns | |
3972 | * 16ns-31ns: 24ns | |
3973 | * ... | |
3974 | */ | |
3975 | if (histo[i] != 0) { | |
3976 | total += histo[i] * (((1UL << i) + ((1UL << i)/2))); | |
3977 | count += histo[i]; | |
3978 | } | |
3979 | } | |
3980 | ||
3981 | /* Prevent divide by zero */ | |
3982 | return (count == 0 ? 0 : total / count); | |
3983 | } | |
3984 | ||
3985 | static void | |
3986 | print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *oldnv, | |
b954e36e | 3987 | nvlist_t *newnv) |
193a37cb TH |
3988 | { |
3989 | int i; | |
3990 | uint64_t val; | |
3991 | const char *names[] = { | |
3992 | ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE, | |
3993 | ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE, | |
3994 | ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE, | |
3995 | ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE, | |
3996 | ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE, | |
3997 | ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE, | |
3998 | ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE, | |
3999 | ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE, | |
4000 | ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE, | |
4001 | ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE, | |
4002 | }; | |
4003 | ||
4004 | struct stat_array *nva; | |
4005 | ||
4006 | unsigned int column_width = default_column_width(cb, IOS_QUEUES); | |
4007 | enum zfs_nicenum_format format; | |
4008 | ||
4009 | nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv); | |
4010 | ||
4011 | if (cb->cb_literal) | |
4012 | format = ZFS_NICENUM_RAW; | |
4013 | else | |
4014 | format = ZFS_NICENUM_1024; | |
4015 | ||
4016 | for (i = 0; i < ARRAY_SIZE(names); i++) { | |
b954e36e | 4017 | val = nva[i].data[0]; |
193a37cb TH |
4018 | print_one_stat(val, format, column_width, cb->cb_scripted); |
4019 | } | |
4020 | ||
4021 | free_calc_stats(nva, ARRAY_SIZE(names)); | |
4022 | } | |
4023 | ||
4024 | static void | |
4025 | print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv, | |
b954e36e | 4026 | nvlist_t *newnv) |
193a37cb TH |
4027 | { |
4028 | int i; | |
4029 | uint64_t val; | |
4030 | const char *names[] = { | |
4031 | ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO, | |
4032 | ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO, | |
4033 | ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO, | |
4034 | ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO, | |
4035 | ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO, | |
4036 | ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO, | |
4037 | ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO, | |
4038 | ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO, | |
4039 | ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO, | |
4040 | }; | |
4041 | struct stat_array *nva; | |
4042 | ||
4043 | unsigned int column_width = default_column_width(cb, IOS_LATENCY); | |
4044 | enum zfs_nicenum_format format; | |
4045 | ||
4046 | nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv); | |
4047 | ||
4048 | if (cb->cb_literal) | |
29eb4942 | 4049 | format = ZFS_NICENUM_RAWTIME; |
193a37cb TH |
4050 | else |
4051 | format = ZFS_NICENUM_TIME; | |
4052 | ||
4053 | /* Print our avg latencies on the line */ | |
4054 | for (i = 0; i < ARRAY_SIZE(names); i++) { | |
4055 | /* Compute average latency for a latency histo */ | |
b954e36e | 4056 | val = single_histo_average(nva[i].data, nva[i].count); |
193a37cb TH |
4057 | print_one_stat(val, format, column_width, cb->cb_scripted); |
4058 | } | |
4059 | free_calc_stats(nva, ARRAY_SIZE(names)); | |
4060 | } | |
4061 | ||
4062 | /* | |
4063 | * Print default statistics (capacity/operations/bandwidth) | |
4064 | */ | |
4065 | static void | |
4066 | print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale) | |
4067 | { | |
4068 | unsigned int column_width = default_column_width(cb, IOS_DEFAULT); | |
4069 | enum zfs_nicenum_format format; | |
4070 | char na; /* char to print for "not applicable" values */ | |
4071 | ||
4072 | if (cb->cb_literal) { | |
4073 | format = ZFS_NICENUM_RAW; | |
4074 | na = '0'; | |
4075 | } else { | |
4076 | format = ZFS_NICENUM_1024; | |
4077 | na = '-'; | |
4078 | } | |
4079 | ||
4080 | /* only toplevel vdevs have capacity stats */ | |
4081 | if (vs->vs_space == 0) { | |
4082 | if (cb->cb_scripted) | |
4083 | printf("\t%c\t%c", na, na); | |
4084 | else | |
4085 | printf(" %*c %*c", column_width, na, column_width, | |
4086 | na); | |
4087 | } else { | |
4088 | print_one_stat(vs->vs_alloc, format, column_width, | |
4089 | cb->cb_scripted); | |
4090 | print_one_stat(vs->vs_space - vs->vs_alloc, format, | |
4091 | column_width, cb->cb_scripted); | |
4092 | } | |
4093 | ||
4094 | print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale), | |
4095 | format, column_width, cb->cb_scripted); | |
4096 | print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale), | |
4097 | format, column_width, cb->cb_scripted); | |
4098 | print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale), | |
4099 | format, column_width, cb->cb_scripted); | |
4100 | print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale), | |
4101 | format, column_width, cb->cb_scripted); | |
34dc7c2f BB |
4102 | } |
4103 | ||
cc99f275 DB |
4104 | static const char *class_name[] = { |
4105 | VDEV_ALLOC_BIAS_DEDUP, | |
4106 | VDEV_ALLOC_BIAS_SPECIAL, | |
4107 | VDEV_ALLOC_CLASS_LOGS | |
4108 | }; | |
4109 | ||
34dc7c2f BB |
4110 | /* |
4111 | * Print out all the statistics for the given vdev. This can either be the | |
4112 | * toplevel configuration, or called recursively. If 'name' is NULL, then this | |
4113 | * is a verbose output, and we don't want to display the toplevel pool stats. | |
193a37cb TH |
4114 | * |
4115 | * Returns the number of stat lines printed. | |
34dc7c2f | 4116 | */ |
cc99f275 | 4117 | static unsigned int |
34dc7c2f BB |
4118 | print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, |
4119 | nvlist_t *newnv, iostat_cbdata_t *cb, int depth) | |
4120 | { | |
4121 | nvlist_t **oldchild, **newchild; | |
674b8934 | 4122 | uint_t c, children, oldchildren; |
193a37cb | 4123 | vdev_stat_t *oldvs, *newvs, *calcvs; |
34dc7c2f | 4124 | vdev_stat_t zerovs = { 0 }; |
193a37cb TH |
4125 | char *vname; |
4126 | int i; | |
4127 | int ret = 0; | |
34dc7c2f BB |
4128 | uint64_t tdelta; |
4129 | double scale; | |
193a37cb TH |
4130 | |
4131 | calcvs = safe_malloc(sizeof (*calcvs)); | |
34dc7c2f | 4132 | |
a1d477c2 MA |
4133 | if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) |
4134 | return (ret); | |
4135 | ||
34dc7c2f | 4136 | if (oldnv != NULL) { |
428870ff BB |
4137 | verify(nvlist_lookup_uint64_array(oldnv, |
4138 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); | |
34dc7c2f BB |
4139 | } else { |
4140 | oldvs = &zerovs; | |
4141 | } | |
4142 | ||
193a37cb TH |
4143 | /* Do we only want to see a specific vdev? */ |
4144 | for (i = 0; i < cb->cb_vdev_names_count; i++) { | |
4145 | /* Yes we do. Is this the vdev? */ | |
4146 | if (strcmp(name, cb->cb_vdev_names[i]) == 0) { | |
4147 | /* | |
4148 | * This is our vdev. Since it is the only vdev we | |
4149 | * will be displaying, make depth = 0 so that it | |
4150 | * doesn't get indented. | |
4151 | */ | |
4152 | depth = 0; | |
4153 | break; | |
4154 | } | |
4155 | } | |
4156 | ||
4157 | if (cb->cb_vdev_names_count && (i == cb->cb_vdev_names_count)) { | |
4158 | /* Couldn't match the name */ | |
4159 | goto children; | |
4160 | } | |
4161 | ||
4162 | ||
428870ff | 4163 | verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
4164 | (uint64_t **)&newvs, &c) == 0); |
4165 | ||
193a37cb TH |
4166 | /* |
4167 | * Print the vdev name unless it's is a histogram. Histograms | |
4168 | * display the vdev name in the header itself. | |
4169 | */ | |
7e945072 | 4170 | if (!(cb->cb_flags & IOS_ANYHISTO_M)) { |
193a37cb TH |
4171 | if (cb->cb_scripted) { |
4172 | printf("%s", name); | |
4173 | } else { | |
4174 | if (strlen(name) + depth > cb->cb_namewidth) | |
4175 | (void) printf("%*s%s", depth, "", name); | |
4176 | else | |
4177 | (void) printf("%*s%s%*s", depth, "", name, | |
4178 | (int)(cb->cb_namewidth - strlen(name) - | |
4179 | depth), ""); | |
4180 | } | |
4181 | } | |
34dc7c2f | 4182 | |
193a37cb | 4183 | /* Calculate our scaling factor */ |
34dc7c2f | 4184 | tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; |
7e945072 | 4185 | if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) { |
193a37cb TH |
4186 | /* |
4187 | * If we specify printing histograms with no time interval, then | |
4188 | * print the histogram numbers over the entire lifetime of the | |
4189 | * vdev. | |
4190 | */ | |
4191 | scale = 1; | |
34dc7c2f | 4192 | } else { |
193a37cb TH |
4193 | if (tdelta == 0) |
4194 | scale = 1.0; | |
4195 | else | |
4196 | scale = (double)NANOSEC / tdelta; | |
34dc7c2f BB |
4197 | } |
4198 | ||
193a37cb TH |
4199 | if (cb->cb_flags & IOS_DEFAULT_M) { |
4200 | calc_default_iostats(oldvs, newvs, calcvs); | |
4201 | print_iostat_default(calcvs, cb, scale); | |
4202 | } | |
4203 | if (cb->cb_flags & IOS_LATENCY_M) | |
b954e36e | 4204 | print_iostat_latency(cb, oldnv, newnv); |
193a37cb | 4205 | if (cb->cb_flags & IOS_QUEUES_M) |
b954e36e | 4206 | print_iostat_queues(cb, oldnv, newnv); |
7e945072 | 4207 | if (cb->cb_flags & IOS_ANYHISTO_M) { |
193a37cb TH |
4208 | printf("\n"); |
4209 | print_iostat_histos(cb, oldnv, newnv, scale, name); | |
4210 | } | |
34dc7c2f | 4211 | |
8720e9e7 TH |
4212 | if (cb->vcdl != NULL) { |
4213 | char *path; | |
4214 | if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH, | |
4215 | &path) == 0) { | |
d6418de0 | 4216 | printf(" "); |
8720e9e7 | 4217 | zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path); |
8720e9e7 TH |
4218 | } |
4219 | } | |
4220 | ||
7e945072 | 4221 | if (!(cb->cb_flags & IOS_ANYHISTO_M)) |
193a37cb | 4222 | printf("\n"); |
34dc7c2f | 4223 | |
193a37cb | 4224 | ret++; |
34dc7c2f | 4225 | |
193a37cb | 4226 | children: |
92bc9278 | 4227 | |
4228 | free(calcvs); | |
4229 | ||
34dc7c2f | 4230 | if (!cb->cb_verbose) |
193a37cb | 4231 | return (ret); |
34dc7c2f BB |
4232 | |
4233 | if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, | |
4234 | &newchild, &children) != 0) | |
193a37cb | 4235 | return (ret); |
34dc7c2f | 4236 | |
674b8934 TH |
4237 | if (oldnv) { |
4238 | if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, | |
4239 | &oldchild, &oldchildren) != 0) | |
4240 | return (ret); | |
4241 | ||
4242 | children = MIN(oldchildren, children); | |
4243 | } | |
34dc7c2f | 4244 | |
cc99f275 DB |
4245 | /* |
4246 | * print normal top-level devices | |
4247 | */ | |
34dc7c2f | 4248 | for (c = 0; c < children; c++) { |
187632dc | 4249 | uint64_t ishole = B_FALSE, islog = B_FALSE; |
428870ff | 4250 | |
187632dc MH |
4251 | (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, |
4252 | &ishole); | |
4253 | ||
4254 | (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, | |
4255 | &islog); | |
4256 | ||
4257 | if (ishole || islog) | |
428870ff BB |
4258 | continue; |
4259 | ||
cc99f275 DB |
4260 | if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) |
4261 | continue; | |
4262 | ||
d2f3e292 RY |
4263 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], |
4264 | cb->cb_name_flags); | |
193a37cb | 4265 | ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, |
34dc7c2f BB |
4266 | newchild[c], cb, depth + 2); |
4267 | free(vname); | |
4268 | } | |
4269 | ||
187632dc | 4270 | /* |
cc99f275 | 4271 | * print all other top-level devices |
187632dc | 4272 | */ |
cc99f275 DB |
4273 | for (uint_t n = 0; n < 3; n++) { |
4274 | boolean_t printed = B_FALSE; | |
187632dc MH |
4275 | |
4276 | for (c = 0; c < children; c++) { | |
4277 | uint64_t islog = B_FALSE; | |
cc99f275 DB |
4278 | char *bias = NULL; |
4279 | char *type = NULL; | |
4280 | ||
187632dc MH |
4281 | (void) nvlist_lookup_uint64(newchild[c], |
4282 | ZPOOL_CONFIG_IS_LOG, &islog); | |
187632dc | 4283 | if (islog) { |
cc99f275 DB |
4284 | bias = VDEV_ALLOC_CLASS_LOGS; |
4285 | } else { | |
4286 | (void) nvlist_lookup_string(newchild[c], | |
4287 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
4288 | (void) nvlist_lookup_string(newchild[c], | |
4289 | ZPOOL_CONFIG_TYPE, &type); | |
187632dc | 4290 | } |
cc99f275 DB |
4291 | if (bias == NULL || strcmp(bias, class_name[n]) != 0) |
4292 | continue; | |
4293 | if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) | |
4294 | continue; | |
187632dc | 4295 | |
cc99f275 DB |
4296 | if (!printed) { |
4297 | if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && | |
4298 | !cb->cb_scripted && !cb->cb_vdev_names) { | |
4299 | print_iostat_dashes(cb, 0, | |
4300 | class_name[n]); | |
4301 | } | |
4302 | printf("\n"); | |
4303 | printed = B_TRUE; | |
4304 | } | |
4305 | ||
4306 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], | |
4307 | cb->cb_name_flags); | |
4308 | ret += print_vdev_stats(zhp, vname, oldnv ? | |
4309 | oldchild[c] : NULL, newchild[c], cb, depth + 2); | |
4310 | free(vname); | |
4311 | } | |
187632dc MH |
4312 | } |
4313 | ||
34dc7c2f BB |
4314 | /* |
4315 | * Include level 2 ARC devices in iostat output | |
4316 | */ | |
4317 | if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, | |
4318 | &newchild, &children) != 0) | |
193a37cb | 4319 | return (ret); |
34dc7c2f | 4320 | |
674b8934 TH |
4321 | if (oldnv) { |
4322 | if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, | |
4323 | &oldchild, &oldchildren) != 0) | |
4324 | return (ret); | |
4325 | ||
4326 | children = MIN(oldchildren, children); | |
4327 | } | |
34dc7c2f BB |
4328 | |
4329 | if (children > 0) { | |
7e945072 | 4330 | if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted && |
193a37cb TH |
4331 | !cb->cb_vdev_names) { |
4332 | print_iostat_dashes(cb, 0, "cache"); | |
4333 | } | |
5034557b | 4334 | printf("\n"); |
193a37cb | 4335 | |
34dc7c2f | 4336 | for (c = 0; c < children; c++) { |
428870ff | 4337 | vname = zpool_vdev_name(g_zfs, zhp, newchild[c], |
d2f3e292 | 4338 | cb->cb_name_flags); |
193a37cb TH |
4339 | ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] |
4340 | : NULL, newchild[c], cb, depth + 2); | |
34dc7c2f BB |
4341 | free(vname); |
4342 | } | |
4343 | } | |
193a37cb TH |
4344 | |
4345 | return (ret); | |
34dc7c2f BB |
4346 | } |
4347 | ||
4348 | static int | |
4349 | refresh_iostat(zpool_handle_t *zhp, void *data) | |
4350 | { | |
4351 | iostat_cbdata_t *cb = data; | |
4352 | boolean_t missing; | |
4353 | ||
4354 | /* | |
4355 | * If the pool has disappeared, remove it from the list and continue. | |
4356 | */ | |
4357 | if (zpool_refresh_stats(zhp, &missing) != 0) | |
4358 | return (-1); | |
4359 | ||
4360 | if (missing) | |
4361 | pool_list_remove(cb->cb_list, zhp); | |
4362 | ||
4363 | return (0); | |
4364 | } | |
4365 | ||
4366 | /* | |
4367 | * Callback to print out the iostats for the given pool. | |
4368 | */ | |
4369 | int | |
4370 | print_iostat(zpool_handle_t *zhp, void *data) | |
4371 | { | |
4372 | iostat_cbdata_t *cb = data; | |
4373 | nvlist_t *oldconfig, *newconfig; | |
4374 | nvlist_t *oldnvroot, *newnvroot; | |
193a37cb | 4375 | int ret; |
34dc7c2f BB |
4376 | |
4377 | newconfig = zpool_get_config(zhp, &oldconfig); | |
4378 | ||
4379 | if (cb->cb_iteration == 1) | |
4380 | oldconfig = NULL; | |
4381 | ||
4382 | verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, | |
4383 | &newnvroot) == 0); | |
4384 | ||
4385 | if (oldconfig == NULL) | |
4386 | oldnvroot = NULL; | |
4387 | else | |
4388 | verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, | |
4389 | &oldnvroot) == 0); | |
4390 | ||
193a37cb | 4391 | ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, |
02730c33 | 4392 | cb, 0); |
7e945072 TH |
4393 | if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) && |
4394 | !cb->cb_scripted && cb->cb_verbose && !cb->cb_vdev_names_count) { | |
4395 | print_iostat_separator(cb); | |
d6418de0 TH |
4396 | if (cb->vcdl != NULL) { |
4397 | print_cmd_columns(cb->vcdl, 1); | |
4398 | } | |
4399 | printf("\n"); | |
7e945072 | 4400 | } |
34dc7c2f | 4401 | |
193a37cb | 4402 | return (ret); |
34dc7c2f BB |
4403 | } |
4404 | ||
9fc60702 CS |
4405 | static int |
4406 | get_columns(void) | |
4407 | { | |
4408 | struct winsize ws; | |
4409 | int columns = 80; | |
4410 | int error; | |
4411 | ||
4412 | if (isatty(STDOUT_FILENO)) { | |
4413 | error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); | |
4414 | if (error == 0) | |
4415 | columns = ws.ws_col; | |
4416 | } else { | |
4417 | columns = 999; | |
4418 | } | |
4419 | ||
d1d7e268 | 4420 | return (columns); |
9fc60702 CS |
4421 | } |
4422 | ||
c5eea0ab BB |
4423 | /* |
4424 | * Return the required length of the pool/vdev name column. The minimum | |
4425 | * allowed width and output formatting flags must be provided. | |
4426 | */ | |
4427 | static int | |
4428 | get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose) | |
34dc7c2f | 4429 | { |
34dc7c2f | 4430 | nvlist_t *config, *nvroot; |
c5eea0ab | 4431 | int width = min_width; |
34dc7c2f BB |
4432 | |
4433 | if ((config = zpool_get_config(zhp, NULL)) != NULL) { | |
4434 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
4435 | &nvroot) == 0); | |
193a37cb | 4436 | unsigned int poolname_len = strlen(zpool_get_name(zhp)); |
c5eea0ab BB |
4437 | if (verbose == B_FALSE) { |
4438 | width = MAX(poolname_len, min_width); | |
4439 | } else { | |
4440 | width = MAX(poolname_len, | |
4441 | max_width(zhp, nvroot, 0, min_width, flags)); | |
4442 | } | |
34dc7c2f | 4443 | } |
9fc60702 | 4444 | |
c5eea0ab | 4445 | return (width); |
34dc7c2f BB |
4446 | } |
4447 | ||
4448 | /* | |
428870ff | 4449 | * Parse the input string, get the 'interval' and 'count' value if there is one. |
34dc7c2f | 4450 | */ |
428870ff | 4451 | static void |
193a37cb | 4452 | get_interval_count(int *argcp, char **argv, float *iv, |
428870ff | 4453 | unsigned long *cnt) |
34dc7c2f | 4454 | { |
193a37cb TH |
4455 | float interval = 0; |
4456 | unsigned long count = 0; | |
1fde1e37 | 4457 | int argc = *argcp; |
34dc7c2f BB |
4458 | |
4459 | /* | |
4460 | * Determine if the last argument is an integer or a pool name | |
4461 | */ | |
193a37cb | 4462 | if (argc > 0 && isnumber(argv[argc - 1])) { |
34dc7c2f BB |
4463 | char *end; |
4464 | ||
4465 | errno = 0; | |
193a37cb | 4466 | interval = strtof(argv[argc - 1], &end); |
34dc7c2f BB |
4467 | |
4468 | if (*end == '\0' && errno == 0) { | |
4469 | if (interval == 0) { | |
4470 | (void) fprintf(stderr, gettext("interval " | |
4471 | "cannot be zero\n")); | |
4472 | usage(B_FALSE); | |
4473 | } | |
34dc7c2f BB |
4474 | /* |
4475 | * Ignore the last parameter | |
4476 | */ | |
4477 | argc--; | |
4478 | } else { | |
4479 | /* | |
4480 | * If this is not a valid number, just plow on. The | |
4481 | * user will get a more informative error message later | |
4482 | * on. | |
4483 | */ | |
4484 | interval = 0; | |
4485 | } | |
4486 | } | |
4487 | ||
4488 | /* | |
4489 | * If the last argument is also an integer, then we have both a count | |
428870ff | 4490 | * and an interval. |
34dc7c2f | 4491 | */ |
193a37cb | 4492 | if (argc > 0 && isnumber(argv[argc - 1])) { |
34dc7c2f BB |
4493 | char *end; |
4494 | ||
4495 | errno = 0; | |
4496 | count = interval; | |
193a37cb | 4497 | interval = strtof(argv[argc - 1], &end); |
34dc7c2f BB |
4498 | |
4499 | if (*end == '\0' && errno == 0) { | |
4500 | if (interval == 0) { | |
4501 | (void) fprintf(stderr, gettext("interval " | |
4502 | "cannot be zero\n")); | |
4503 | usage(B_FALSE); | |
4504 | } | |
4505 | ||
4506 | /* | |
4507 | * Ignore the last parameter | |
4508 | */ | |
4509 | argc--; | |
4510 | } else { | |
4511 | interval = 0; | |
4512 | } | |
4513 | } | |
4514 | ||
428870ff BB |
4515 | *iv = interval; |
4516 | *cnt = count; | |
4517 | *argcp = argc; | |
4518 | } | |
4519 | ||
4520 | static void | |
4521 | get_timestamp_arg(char c) | |
4522 | { | |
4523 | if (c == 'u') | |
4524 | timestamp_fmt = UDATE; | |
4525 | else if (c == 'd') | |
4526 | timestamp_fmt = DDATE; | |
4527 | else | |
4528 | usage(B_FALSE); | |
4529 | } | |
4530 | ||
4531 | /* | |
193a37cb TH |
4532 | * Return stat flags that are supported by all pools by both the module and |
4533 | * zpool iostat. "*data" should be initialized to all 0xFFs before running. | |
4534 | * It will get ANDed down until only the flags that are supported on all pools | |
4535 | * remain. | |
4536 | */ | |
4537 | static int | |
4538 | get_stat_flags_cb(zpool_handle_t *zhp, void *data) | |
4539 | { | |
4540 | uint64_t *mask = data; | |
4541 | nvlist_t *config, *nvroot, *nvx; | |
4542 | uint64_t flags = 0; | |
4543 | int i, j; | |
4544 | ||
193a37cb TH |
4545 | config = zpool_get_config(zhp, NULL); |
4546 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
4547 | &nvroot) == 0); | |
4548 | ||
4549 | /* Default stats are always supported, but for completeness.. */ | |
4550 | if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS)) | |
4551 | flags |= IOS_DEFAULT_M; | |
4552 | ||
4553 | /* Get our extended stats nvlist from the main list */ | |
4554 | if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX, | |
4555 | &nvx) != 0) { | |
4556 | /* | |
4557 | * No extended stats; they're probably running an older | |
4558 | * module. No big deal, we support that too. | |
4559 | */ | |
4560 | goto end; | |
4561 | } | |
4562 | ||
4563 | /* For each extended stat, make sure all its nvpairs are supported */ | |
4564 | for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) { | |
4565 | if (!vsx_type_to_nvlist[j][0]) | |
4566 | continue; | |
4567 | ||
4568 | /* Start off by assuming the flag is supported, then check */ | |
4569 | flags |= (1ULL << j); | |
4570 | for (i = 0; vsx_type_to_nvlist[j][i]; i++) { | |
4571 | if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) { | |
4572 | /* flag isn't supported */ | |
4573 | flags = flags & ~(1ULL << j); | |
4574 | break; | |
4575 | } | |
4576 | } | |
4577 | } | |
4578 | end: | |
4579 | *mask = *mask & flags; | |
4580 | return (0); | |
4581 | } | |
4582 | ||
4583 | /* | |
4584 | * Return a bitmask of stats that are supported on all pools by both the module | |
4585 | * and zpool iostat. | |
4586 | */ | |
4587 | static uint64_t | |
4588 | get_stat_flags(zpool_list_t *list) | |
4589 | { | |
4590 | uint64_t mask = -1; | |
4591 | ||
4592 | /* | |
4593 | * get_stat_flags_cb() will lop off bits from "mask" until only the | |
4594 | * flags that are supported on all pools remain. | |
4595 | */ | |
4596 | pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask); | |
4597 | return (mask); | |
4598 | } | |
4599 | ||
4600 | /* | |
4601 | * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise. | |
4602 | */ | |
4603 | static int | |
4604 | is_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, void *cb_data) | |
4605 | { | |
4606 | iostat_cbdata_t *cb = cb_data; | |
92bc9278 | 4607 | char *name = NULL; |
4608 | int ret = 0; | |
193a37cb TH |
4609 | |
4610 | name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags); | |
4611 | ||
4612 | if (strcmp(name, cb->cb_vdev_names[0]) == 0) | |
92bc9278 | 4613 | ret = 1; /* match */ |
4614 | free(name); | |
193a37cb | 4615 | |
92bc9278 | 4616 | return (ret); |
193a37cb TH |
4617 | } |
4618 | ||
4619 | /* | |
4620 | * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise. | |
4621 | */ | |
4622 | static int | |
4623 | is_vdev(zpool_handle_t *zhp, void *cb_data) | |
4624 | { | |
4625 | return (for_each_vdev(zhp, is_vdev_cb, cb_data)); | |
4626 | } | |
4627 | ||
4628 | /* | |
4629 | * Check if vdevs are in a pool | |
4630 | * | |
4631 | * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise | |
4632 | * return 0. If pool_name is NULL, then search all pools. | |
4633 | */ | |
4634 | static int | |
4635 | are_vdevs_in_pool(int argc, char **argv, char *pool_name, | |
4636 | iostat_cbdata_t *cb) | |
4637 | { | |
4638 | char **tmp_name; | |
4639 | int ret = 0; | |
4640 | int i; | |
4641 | int pool_count = 0; | |
4642 | ||
4643 | if ((argc == 0) || !*argv) | |
4644 | return (0); | |
4645 | ||
4646 | if (pool_name) | |
4647 | pool_count = 1; | |
4648 | ||
4649 | /* Temporarily hijack cb_vdev_names for a second... */ | |
4650 | tmp_name = cb->cb_vdev_names; | |
4651 | ||
4652 | /* Go though our list of prospective vdev names */ | |
4653 | for (i = 0; i < argc; i++) { | |
4654 | cb->cb_vdev_names = argv + i; | |
4655 | ||
4656 | /* Is this name a vdev in our pools? */ | |
4657 | ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL, | |
4658 | is_vdev, cb); | |
4659 | if (!ret) { | |
4660 | /* No match */ | |
4661 | break; | |
4662 | } | |
4663 | } | |
4664 | ||
4665 | cb->cb_vdev_names = tmp_name; | |
4666 | ||
4667 | return (ret); | |
4668 | } | |
4669 | ||
4670 | static int | |
4671 | is_pool_cb(zpool_handle_t *zhp, void *data) | |
4672 | { | |
4673 | char *name = data; | |
4674 | if (strcmp(name, zpool_get_name(zhp)) == 0) | |
4675 | return (1); | |
4676 | ||
4677 | return (0); | |
4678 | } | |
4679 | ||
4680 | /* | |
4681 | * Do we have a pool named *name? If so, return 1, otherwise 0. | |
4682 | */ | |
4683 | static int | |
4684 | is_pool(char *name) | |
4685 | { | |
4686 | return (for_each_pool(0, NULL, B_TRUE, NULL, is_pool_cb, name)); | |
4687 | } | |
4688 | ||
4689 | /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */ | |
4690 | static int | |
4ea3f864 GM |
4691 | are_all_pools(int argc, char **argv) |
4692 | { | |
193a37cb TH |
4693 | if ((argc == 0) || !*argv) |
4694 | return (0); | |
4695 | ||
4696 | while (--argc >= 0) | |
4697 | if (!is_pool(argv[argc])) | |
4698 | return (0); | |
4699 | ||
4700 | return (1); | |
4701 | } | |
4702 | ||
4703 | /* | |
4704 | * Helper function to print out vdev/pool names we can't resolve. Used for an | |
4705 | * error message. | |
4706 | */ | |
4707 | static void | |
4708 | error_list_unresolved_vdevs(int argc, char **argv, char *pool_name, | |
4709 | iostat_cbdata_t *cb) | |
4710 | { | |
4711 | int i; | |
4712 | char *name; | |
4713 | char *str; | |
4714 | for (i = 0; i < argc; i++) { | |
4715 | name = argv[i]; | |
4716 | ||
4717 | if (is_pool(name)) | |
4718 | str = gettext("pool"); | |
4719 | else if (are_vdevs_in_pool(1, &name, pool_name, cb)) | |
4720 | str = gettext("vdev in this pool"); | |
4721 | else if (are_vdevs_in_pool(1, &name, NULL, cb)) | |
4722 | str = gettext("vdev in another pool"); | |
4723 | else | |
4724 | str = gettext("unknown"); | |
4725 | ||
4726 | fprintf(stderr, "\t%s (%s)\n", name, str); | |
4727 | } | |
4728 | } | |
4729 | ||
4730 | /* | |
4731 | * Same as get_interval_count(), but with additional checks to not misinterpret | |
4732 | * guids as interval/count values. Assumes VDEV_NAME_GUID is set in | |
4733 | * cb.cb_name_flags. | |
4734 | */ | |
4735 | static void | |
4736 | get_interval_count_filter_guids(int *argc, char **argv, float *interval, | |
4737 | unsigned long *count, iostat_cbdata_t *cb) | |
4738 | { | |
4739 | char **tmpargv = argv; | |
4740 | int argc_for_interval = 0; | |
4741 | ||
4742 | /* Is the last arg an interval value? Or a guid? */ | |
4743 | if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL, cb)) { | |
4744 | /* | |
4745 | * The last arg is not a guid, so it's probably an | |
4746 | * interval value. | |
4747 | */ | |
4748 | argc_for_interval++; | |
4749 | ||
4750 | if (*argc >= 2 && | |
4751 | !are_vdevs_in_pool(1, &argv[*argc - 2], NULL, cb)) { | |
4752 | /* | |
4753 | * The 2nd to last arg is not a guid, so it's probably | |
4754 | * an interval value. | |
4755 | */ | |
4756 | argc_for_interval++; | |
4757 | } | |
4758 | } | |
4759 | ||
4760 | /* Point to our list of possible intervals */ | |
4761 | tmpargv = &argv[*argc - argc_for_interval]; | |
4762 | ||
4763 | *argc = *argc - argc_for_interval; | |
4764 | get_interval_count(&argc_for_interval, tmpargv, | |
4765 | interval, count); | |
4766 | } | |
4767 | ||
4768 | /* | |
4769 | * Floating point sleep(). Allows you to pass in a floating point value for | |
4770 | * seconds. | |
4771 | */ | |
4772 | static void | |
4ea3f864 GM |
4773 | fsleep(float sec) |
4774 | { | |
193a37cb TH |
4775 | struct timespec req; |
4776 | req.tv_sec = floor(sec); | |
4777 | req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC; | |
4778 | nanosleep(&req, NULL); | |
4779 | } | |
4780 | ||
d6418de0 TH |
4781 | /* |
4782 | * Run one of the zpool status/iostat -c scripts with the help (-h) option and | |
4783 | * print the result. | |
4784 | * | |
4785 | * name: Short name of the script ('iostat'). | |
4786 | * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat'); | |
4787 | */ | |
4788 | static void | |
4789 | print_zpool_script_help(char *name, char *path) | |
4790 | { | |
4791 | char *argv[] = {path, "-h", NULL}; | |
4792 | char **lines = NULL; | |
4793 | int lines_cnt = 0; | |
4794 | int rc; | |
4795 | ||
4796 | rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines, | |
4797 | &lines_cnt); | |
3e2ecae0 GDN |
4798 | if (rc != 0 || lines == NULL || lines_cnt <= 0) { |
4799 | if (lines != NULL) | |
4800 | libzfs_free_str_array(lines, lines_cnt); | |
d6418de0 | 4801 | return; |
3e2ecae0 | 4802 | } |
d6418de0 TH |
4803 | |
4804 | for (int i = 0; i < lines_cnt; i++) | |
4805 | if (!is_blank_str(lines[i])) | |
4806 | printf(" %-14s %s\n", name, lines[i]); | |
4807 | ||
4808 | libzfs_free_str_array(lines, lines_cnt); | |
4809 | } | |
4810 | ||
193a37cb | 4811 | /* |
099700d9 | 4812 | * Go though the zpool status/iostat -c scripts in the user's path, run their |
d6418de0 TH |
4813 | * help option (-h), and print out the results. |
4814 | */ | |
4815 | static void | |
099700d9 | 4816 | print_zpool_dir_scripts(char *dirpath) |
d6418de0 TH |
4817 | { |
4818 | DIR *dir; | |
4819 | struct dirent *ent; | |
4820 | char fullpath[MAXPATHLEN]; | |
4821 | struct stat dir_stat; | |
4822 | ||
099700d9 | 4823 | if ((dir = opendir(dirpath)) != NULL) { |
d6418de0 TH |
4824 | /* print all the files and directories within directory */ |
4825 | while ((ent = readdir(dir)) != NULL) { | |
099700d9 | 4826 | sprintf(fullpath, "%s/%s", dirpath, ent->d_name); |
d6418de0 TH |
4827 | |
4828 | /* Print the scripts */ | |
4829 | if (stat(fullpath, &dir_stat) == 0) | |
4830 | if (dir_stat.st_mode & S_IXUSR && | |
4831 | S_ISREG(dir_stat.st_mode)) | |
4832 | print_zpool_script_help(ent->d_name, | |
4833 | fullpath); | |
4834 | } | |
d6418de0 | 4835 | closedir(dir); |
d6418de0 TH |
4836 | } |
4837 | } | |
4838 | ||
099700d9 GDN |
4839 | /* |
4840 | * Print out help text for all zpool status/iostat -c scripts. | |
4841 | */ | |
4842 | static void | |
4843 | print_zpool_script_list(char *subcommand) | |
4844 | { | |
4845 | char *dir, *sp; | |
4846 | ||
4847 | printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand); | |
4848 | ||
4849 | sp = zpool_get_cmd_search_path(); | |
4850 | if (sp == NULL) | |
4851 | return; | |
4852 | ||
4853 | dir = strtok(sp, ":"); | |
4854 | while (dir != NULL) { | |
4855 | print_zpool_dir_scripts(dir); | |
4856 | dir = strtok(NULL, ":"); | |
4857 | } | |
4858 | ||
4859 | free(sp); | |
4860 | } | |
4861 | ||
c5eea0ab BB |
4862 | /* |
4863 | * Set the minimum pool/vdev name column width. The width must be at least 10, | |
4864 | * but may be as large as the column width - 42 so it still fits on one line. | |
4865 | */ | |
4866 | static int | |
4867 | get_namewidth_iostat(zpool_handle_t *zhp, void *data) | |
4868 | { | |
4869 | iostat_cbdata_t *cb = data; | |
4870 | int width, columns; | |
4871 | ||
4872 | width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags, | |
4873 | cb->cb_verbose); | |
4874 | columns = get_columns(); | |
4875 | ||
4876 | if (width < 10) | |
4877 | width = 10; | |
4878 | if (width > columns - 42) | |
4879 | width = columns - 42; | |
4880 | ||
4881 | cb->cb_namewidth = width; | |
4882 | ||
4883 | return (0); | |
4884 | } | |
4885 | ||
d6418de0 TH |
4886 | /* |
4887 | * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name] | |
4888 | * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]] | |
4889 | * [interval [count]] | |
428870ff | 4890 | * |
8720e9e7 | 4891 | * -c CMD For each vdev, run command CMD |
d2f3e292 RY |
4892 | * -g Display guid for individual vdev name. |
4893 | * -L Follow links when resolving vdev path name. | |
a77f29f9 | 4894 | * -P Display full path for vdev name. |
428870ff | 4895 | * -v Display statistics for individual vdevs |
193a37cb TH |
4896 | * -h Display help |
4897 | * -p Display values in parsable (exact) format. | |
4898 | * -H Scripted mode. Don't display headers, and separate properties | |
4899 | * by a single tab. | |
4900 | * -l Display average latency | |
4901 | * -q Display queue depths | |
7e945072 TH |
4902 | * -w Display latency histograms |
4903 | * -r Display request size histogram | |
428870ff BB |
4904 | * -T Display a timestamp in date(1) or Unix format |
4905 | * | |
4906 | * This command can be tricky because we want to be able to deal with pool | |
4907 | * creation/destruction as well as vdev configuration changes. The bulk of this | |
4908 | * processing is handled by the pool_list_* routines in zpool_iter.c. We rely | |
4909 | * on pool_list_update() to detect the addition of new pools. Configuration | |
4910 | * changes are all handled within libzfs. | |
4911 | */ | |
4912 | int | |
4913 | zpool_do_iostat(int argc, char **argv) | |
4914 | { | |
4915 | int c; | |
4916 | int ret; | |
4917 | int npools; | |
193a37cb TH |
4918 | float interval = 0; |
4919 | unsigned long count = 0; | |
428870ff BB |
4920 | zpool_list_t *list; |
4921 | boolean_t verbose = B_FALSE; | |
7e945072 | 4922 | boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE; |
3491d6eb | 4923 | boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE; |
41092124 | 4924 | boolean_t omit_since_boot = B_FALSE; |
d2f3e292 RY |
4925 | boolean_t guid = B_FALSE; |
4926 | boolean_t follow_links = B_FALSE; | |
4927 | boolean_t full_name = B_FALSE; | |
4928 | iostat_cbdata_t cb = { 0 }; | |
8720e9e7 | 4929 | char *cmd = NULL; |
428870ff | 4930 | |
193a37cb TH |
4931 | /* Used for printing error message */ |
4932 | const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q', | |
7e945072 | 4933 | [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'}; |
193a37cb TH |
4934 | |
4935 | uint64_t unsupported_flags; | |
4936 | ||
428870ff | 4937 | /* check options */ |
8720e9e7 | 4938 | while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwH")) != -1) { |
428870ff | 4939 | switch (c) { |
8720e9e7 | 4940 | case 'c': |
d6418de0 TH |
4941 | if (cmd != NULL) { |
4942 | fprintf(stderr, | |
4943 | gettext("Can't set -c flag twice\n")); | |
4944 | exit(1); | |
4945 | } | |
099700d9 GDN |
4946 | |
4947 | if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && | |
4948 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { | |
4949 | fprintf(stderr, gettext( | |
4950 | "Can't run -c, disabled by " | |
4951 | "ZPOOL_SCRIPTS_ENABLED.\n")); | |
4952 | exit(1); | |
4953 | } | |
4954 | ||
d6418de0 TH |
4955 | if ((getuid() <= 0 || geteuid() <= 0) && |
4956 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { | |
4957 | fprintf(stderr, gettext( | |
4958 | "Can't run -c with root privileges " | |
4959 | "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); | |
4960 | exit(1); | |
4961 | } | |
8720e9e7 | 4962 | cmd = optarg; |
d6418de0 | 4963 | verbose = B_TRUE; |
8720e9e7 | 4964 | break; |
d2f3e292 RY |
4965 | case 'g': |
4966 | guid = B_TRUE; | |
4967 | break; | |
4968 | case 'L': | |
4969 | follow_links = B_TRUE; | |
4970 | break; | |
a77f29f9 | 4971 | case 'P': |
d2f3e292 RY |
4972 | full_name = B_TRUE; |
4973 | break; | |
428870ff BB |
4974 | case 'T': |
4975 | get_timestamp_arg(*optarg); | |
4976 | break; | |
4977 | case 'v': | |
4978 | verbose = B_TRUE; | |
4979 | break; | |
193a37cb | 4980 | case 'p': |
3491d6eb | 4981 | parsable = B_TRUE; |
193a37cb TH |
4982 | break; |
4983 | case 'l': | |
4984 | latency = B_TRUE; | |
4985 | break; | |
4986 | case 'q': | |
4987 | queues = B_TRUE; | |
4988 | break; | |
4989 | case 'H': | |
4990 | scripted = B_TRUE; | |
4991 | break; | |
4992 | case 'w': | |
7e945072 TH |
4993 | l_histo = B_TRUE; |
4994 | break; | |
4995 | case 'r': | |
4996 | rq_histo = B_TRUE; | |
193a37cb | 4997 | break; |
41092124 HM |
4998 | case 'y': |
4999 | omit_since_boot = B_TRUE; | |
5000 | break; | |
193a37cb TH |
5001 | case 'h': |
5002 | usage(B_FALSE); | |
5003 | break; | |
428870ff | 5004 | case '?': |
1528bfdb | 5005 | if (optopt == 'c') { |
099700d9 | 5006 | print_zpool_script_list("iostat"); |
d6418de0 | 5007 | exit(0); |
1528bfdb TH |
5008 | } else { |
5009 | fprintf(stderr, | |
5010 | gettext("invalid option '%c'\n"), optopt); | |
5011 | } | |
428870ff BB |
5012 | usage(B_FALSE); |
5013 | } | |
5014 | } | |
5015 | ||
5016 | argc -= optind; | |
5017 | argv += optind; | |
5018 | ||
3491d6eb | 5019 | cb.cb_literal = parsable; |
193a37cb TH |
5020 | cb.cb_scripted = scripted; |
5021 | ||
5022 | if (guid) | |
5023 | cb.cb_name_flags |= VDEV_NAME_GUID; | |
5024 | if (follow_links) | |
5025 | cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
5026 | if (full_name) | |
5027 | cb.cb_name_flags |= VDEV_NAME_PATH; | |
5028 | cb.cb_iteration = 0; | |
5029 | cb.cb_namewidth = 0; | |
5030 | cb.cb_verbose = verbose; | |
5031 | ||
5032 | /* Get our interval and count values (if any) */ | |
5033 | if (guid) { | |
5034 | get_interval_count_filter_guids(&argc, argv, &interval, | |
5035 | &count, &cb); | |
5036 | } else { | |
5037 | get_interval_count(&argc, argv, &interval, &count); | |
5038 | } | |
5039 | ||
5040 | if (argc == 0) { | |
5041 | /* No args, so just print the defaults. */ | |
5042 | } else if (are_all_pools(argc, argv)) { | |
5043 | /* All the args are pool names */ | |
5044 | } else if (are_vdevs_in_pool(argc, argv, NULL, &cb)) { | |
5045 | /* All the args are vdevs */ | |
5046 | cb.cb_vdev_names = argv; | |
5047 | cb.cb_vdev_names_count = argc; | |
5048 | argc = 0; /* No pools to process */ | |
5049 | } else if (are_all_pools(1, argv)) { | |
5050 | /* The first arg is a pool name */ | |
5051 | if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0], &cb)) { | |
5052 | /* ...and the rest are vdev names */ | |
5053 | cb.cb_vdev_names = argv + 1; | |
5054 | cb.cb_vdev_names_count = argc - 1; | |
5055 | argc = 1; /* One pool to process */ | |
5056 | } else { | |
5057 | fprintf(stderr, gettext("Expected either a list of ")); | |
5058 | fprintf(stderr, gettext("pools, or list of vdevs in")); | |
5059 | fprintf(stderr, " \"%s\", ", argv[0]); | |
5060 | fprintf(stderr, gettext("but got:\n")); | |
5061 | error_list_unresolved_vdevs(argc - 1, argv + 1, | |
5062 | argv[0], &cb); | |
5063 | fprintf(stderr, "\n"); | |
5064 | usage(B_FALSE); | |
5065 | return (1); | |
5066 | } | |
5067 | } else { | |
5068 | /* | |
5069 | * The args don't make sense. The first arg isn't a pool name, | |
5070 | * nor are all the args vdevs. | |
5071 | */ | |
5072 | fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n")); | |
5073 | fprintf(stderr, "\n"); | |
5074 | return (1); | |
5075 | } | |
5076 | ||
5077 | if (cb.cb_vdev_names_count != 0) { | |
5078 | /* | |
5079 | * If user specified vdevs, it implies verbose. | |
5080 | */ | |
5081 | cb.cb_verbose = B_TRUE; | |
5082 | } | |
428870ff | 5083 | |
34dc7c2f BB |
5084 | /* |
5085 | * Construct the list of all interesting pools. | |
5086 | */ | |
5087 | ret = 0; | |
5088 | if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL) | |
5089 | return (1); | |
5090 | ||
5091 | if (pool_list_count(list) == 0 && argc != 0) { | |
5092 | pool_list_free(list); | |
5093 | return (1); | |
5094 | } | |
5095 | ||
5096 | if (pool_list_count(list) == 0 && interval == 0) { | |
5097 | pool_list_free(list); | |
5098 | (void) fprintf(stderr, gettext("no pools available\n")); | |
5099 | return (1); | |
5100 | } | |
5101 | ||
d6418de0 | 5102 | if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) { |
7e945072 TH |
5103 | pool_list_free(list); |
5104 | (void) fprintf(stderr, | |
d6418de0 | 5105 | gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n")); |
7e945072 TH |
5106 | usage(B_FALSE); |
5107 | return (1); | |
5108 | } | |
5109 | ||
5110 | if (l_histo && rq_histo) { | |
193a37cb TH |
5111 | pool_list_free(list); |
5112 | (void) fprintf(stderr, | |
7e945072 | 5113 | gettext("Only one of [-r|-w] can be passed at a time\n")); |
193a37cb TH |
5114 | usage(B_FALSE); |
5115 | return (1); | |
5116 | } | |
5117 | ||
34dc7c2f BB |
5118 | /* |
5119 | * Enter the main iostat loop. | |
5120 | */ | |
5121 | cb.cb_list = list; | |
193a37cb | 5122 | |
7e945072 | 5123 | if (l_histo) { |
193a37cb TH |
5124 | /* |
5125 | * Histograms tables look out of place when you try to display | |
5126 | * them with the other stats, so make a rule that you can only | |
5127 | * print histograms by themselves. | |
5128 | */ | |
5129 | cb.cb_flags = IOS_L_HISTO_M; | |
7e945072 TH |
5130 | } else if (rq_histo) { |
5131 | cb.cb_flags = IOS_RQ_HISTO_M; | |
193a37cb TH |
5132 | } else { |
5133 | cb.cb_flags = IOS_DEFAULT_M; | |
5134 | if (latency) | |
5135 | cb.cb_flags |= IOS_LATENCY_M; | |
5136 | if (queues) | |
5137 | cb.cb_flags |= IOS_QUEUES_M; | |
5138 | } | |
5139 | ||
5140 | /* | |
5141 | * See if the module supports all the stats we want to display. | |
5142 | */ | |
5143 | unsupported_flags = cb.cb_flags & ~get_stat_flags(list); | |
5144 | if (unsupported_flags) { | |
5145 | uint64_t f; | |
5146 | int idx; | |
5147 | fprintf(stderr, | |
5148 | gettext("The loaded zfs module doesn't support:")); | |
5149 | ||
5150 | /* for each bit set in unsupported_flags */ | |
5151 | for (f = unsupported_flags; f; f &= ~(1ULL << idx)) { | |
5152 | idx = lowbit64(f) - 1; | |
5153 | fprintf(stderr, " -%c", flag_to_arg[idx]); | |
5154 | } | |
5155 | ||
02730c33 | 5156 | fprintf(stderr, ". Try running a newer module.\n"); |
193a37cb TH |
5157 | pool_list_free(list); |
5158 | ||
5159 | return (1); | |
5160 | } | |
5161 | ||
34dc7c2f | 5162 | for (;;) { |
34dc7c2f | 5163 | if ((npools = pool_list_count(list)) == 0) |
42cb3819 | 5164 | (void) fprintf(stderr, gettext("no pools available\n")); |
5a521059 | 5165 | else { |
41092124 HM |
5166 | /* |
5167 | * If this is the first iteration and -y was supplied | |
5168 | * we skip any printing. | |
5169 | */ | |
5170 | boolean_t skip = (omit_since_boot && | |
193a37cb | 5171 | cb.cb_iteration == 0); |
41092124 | 5172 | |
5a521059 PJ |
5173 | /* |
5174 | * Refresh all statistics. This is done as an | |
5175 | * explicit step before calculating the maximum name | |
5176 | * width, so that any * configuration changes are | |
5177 | * properly accounted for. | |
5178 | */ | |
5179 | (void) pool_list_iter(list, B_FALSE, refresh_iostat, | |
193a37cb | 5180 | &cb); |
34dc7c2f | 5181 | |
5a521059 PJ |
5182 | /* |
5183 | * Iterate over all pools to determine the maximum width | |
5184 | * for the pool / device name column across all pools. | |
5185 | */ | |
5186 | cb.cb_namewidth = 0; | |
c5eea0ab BB |
5187 | (void) pool_list_iter(list, B_FALSE, |
5188 | get_namewidth_iostat, &cb); | |
34dc7c2f | 5189 | |
5a521059 PJ |
5190 | if (timestamp_fmt != NODATE) |
5191 | print_timestamp(timestamp_fmt); | |
428870ff | 5192 | |
d6418de0 TH |
5193 | if (cmd != NULL && cb.cb_verbose && |
5194 | !(cb.cb_flags & IOS_ANYHISTO_M)) { | |
5195 | cb.vcdl = all_pools_for_each_vdev_run(argc, | |
5196 | argv, cmd, g_zfs, cb.cb_vdev_names, | |
5197 | cb.cb_vdev_names_count, cb.cb_name_flags); | |
5198 | } else { | |
5199 | cb.vcdl = NULL; | |
5200 | } | |
5201 | ||
5a521059 | 5202 | /* |
41092124 HM |
5203 | * If it's the first time and we're not skipping it, |
5204 | * or either skip or verbose mode, print the header. | |
193a37cb TH |
5205 | * |
5206 | * The histogram code explicitly prints its header on | |
5207 | * every vdev, so skip this for histograms. | |
5a521059 | 5208 | */ |
193a37cb TH |
5209 | if (((++cb.cb_iteration == 1 && !skip) || |
5210 | (skip != verbose)) && | |
7e945072 | 5211 | (!(cb.cb_flags & IOS_ANYHISTO_M)) && |
193a37cb | 5212 | !cb.cb_scripted) |
5a521059 | 5213 | print_iostat_header(&cb); |
34dc7c2f | 5214 | |
41092124 | 5215 | if (skip) { |
193a37cb | 5216 | (void) fsleep(interval); |
41092124 HM |
5217 | continue; |
5218 | } | |
5219 | ||
8720e9e7 | 5220 | |
193a37cb | 5221 | pool_list_iter(list, B_FALSE, print_iostat, &cb); |
34dc7c2f | 5222 | |
5a521059 PJ |
5223 | /* |
5224 | * If there's more than one pool, and we're not in | |
5225 | * verbose mode (which prints a separator for us), | |
5226 | * then print a separator. | |
193a37cb TH |
5227 | * |
5228 | * In addition, if we're printing specific vdevs then | |
5229 | * we also want an ending separator. | |
5a521059 | 5230 | */ |
193a37cb | 5231 | if (((npools > 1 && !verbose && |
7e945072 TH |
5232 | !(cb.cb_flags & IOS_ANYHISTO_M)) || |
5233 | (!(cb.cb_flags & IOS_ANYHISTO_M) && | |
193a37cb TH |
5234 | cb.cb_vdev_names_count)) && |
5235 | !cb.cb_scripted) { | |
5a521059 | 5236 | print_iostat_separator(&cb); |
d6418de0 TH |
5237 | if (cb.vcdl != NULL) |
5238 | print_cmd_columns(cb.vcdl, 1); | |
5239 | printf("\n"); | |
193a37cb | 5240 | } |
d6418de0 TH |
5241 | |
5242 | if (cb.vcdl != NULL) | |
5243 | free_vdev_cmd_data_list(cb.vcdl); | |
5244 | ||
5a521059 | 5245 | } |
34dc7c2f BB |
5246 | |
5247 | /* | |
5248 | * Flush the output so that redirection to a file isn't buffered | |
5249 | * indefinitely. | |
5250 | */ | |
5251 | (void) fflush(stdout); | |
5252 | ||
5253 | if (interval == 0) | |
5254 | break; | |
5255 | ||
5256 | if (count != 0 && --count == 0) | |
5257 | break; | |
5258 | ||
193a37cb | 5259 | (void) fsleep(interval); |
34dc7c2f BB |
5260 | } |
5261 | ||
5262 | pool_list_free(list); | |
5263 | ||
5264 | return (ret); | |
5265 | } | |
5266 | ||
5267 | typedef struct list_cbdata { | |
1bd201e7 | 5268 | boolean_t cb_verbose; |
d2f3e292 | 5269 | int cb_name_flags; |
1bd201e7 | 5270 | int cb_namewidth; |
34dc7c2f | 5271 | boolean_t cb_scripted; |
34dc7c2f | 5272 | zprop_list_t *cb_proplist; |
2a8b84b7 | 5273 | boolean_t cb_literal; |
34dc7c2f BB |
5274 | } list_cbdata_t; |
5275 | ||
cc99f275 | 5276 | |
34dc7c2f BB |
5277 | /* |
5278 | * Given a list of columns to display, output appropriate headers for each one. | |
5279 | */ | |
5280 | static void | |
1bd201e7 | 5281 | print_header(list_cbdata_t *cb) |
34dc7c2f | 5282 | { |
1bd201e7 | 5283 | zprop_list_t *pl = cb->cb_proplist; |
9ae529ec | 5284 | char headerbuf[ZPOOL_MAXPROPLEN]; |
34dc7c2f BB |
5285 | const char *header; |
5286 | boolean_t first = B_TRUE; | |
5287 | boolean_t right_justify; | |
1bd201e7 | 5288 | size_t width = 0; |
34dc7c2f BB |
5289 | |
5290 | for (; pl != NULL; pl = pl->pl_next) { | |
1bd201e7 CS |
5291 | width = pl->pl_width; |
5292 | if (first && cb->cb_verbose) { | |
5293 | /* | |
5294 | * Reset the width to accommodate the verbose listing | |
5295 | * of devices. | |
5296 | */ | |
5297 | width = cb->cb_namewidth; | |
5298 | } | |
5299 | ||
34dc7c2f BB |
5300 | if (!first) |
5301 | (void) printf(" "); | |
5302 | else | |
5303 | first = B_FALSE; | |
5304 | ||
9ae529ec CS |
5305 | right_justify = B_FALSE; |
5306 | if (pl->pl_prop != ZPROP_INVAL) { | |
5307 | header = zpool_prop_column_name(pl->pl_prop); | |
5308 | right_justify = zpool_prop_align_right(pl->pl_prop); | |
5309 | } else { | |
5310 | int i; | |
5311 | ||
5312 | for (i = 0; pl->pl_user_prop[i] != '\0'; i++) | |
5313 | headerbuf[i] = toupper(pl->pl_user_prop[i]); | |
5314 | headerbuf[i] = '\0'; | |
5315 | header = headerbuf; | |
5316 | } | |
34dc7c2f BB |
5317 | |
5318 | if (pl->pl_next == NULL && !right_justify) | |
5319 | (void) printf("%s", header); | |
5320 | else if (right_justify) | |
1bd201e7 | 5321 | (void) printf("%*s", (int)width, header); |
34dc7c2f | 5322 | else |
1bd201e7 | 5323 | (void) printf("%-*s", (int)width, header); |
34dc7c2f BB |
5324 | } |
5325 | ||
5326 | (void) printf("\n"); | |
5327 | } | |
5328 | ||
5329 | /* | |
5330 | * Given a pool and a list of properties, print out all the properties according | |
cc99f275 | 5331 | * to the described layout. Used by zpool_do_list(). |
34dc7c2f BB |
5332 | */ |
5333 | static void | |
1bd201e7 | 5334 | print_pool(zpool_handle_t *zhp, list_cbdata_t *cb) |
34dc7c2f | 5335 | { |
1bd201e7 | 5336 | zprop_list_t *pl = cb->cb_proplist; |
34dc7c2f BB |
5337 | boolean_t first = B_TRUE; |
5338 | char property[ZPOOL_MAXPROPLEN]; | |
5339 | char *propstr; | |
5340 | boolean_t right_justify; | |
1bd201e7 | 5341 | size_t width; |
34dc7c2f BB |
5342 | |
5343 | for (; pl != NULL; pl = pl->pl_next) { | |
1bd201e7 CS |
5344 | |
5345 | width = pl->pl_width; | |
5346 | if (first && cb->cb_verbose) { | |
5347 | /* | |
5348 | * Reset the width to accommodate the verbose listing | |
5349 | * of devices. | |
5350 | */ | |
5351 | width = cb->cb_namewidth; | |
5352 | } | |
5353 | ||
34dc7c2f | 5354 | if (!first) { |
1bd201e7 | 5355 | if (cb->cb_scripted) |
34dc7c2f BB |
5356 | (void) printf("\t"); |
5357 | else | |
5358 | (void) printf(" "); | |
5359 | } else { | |
5360 | first = B_FALSE; | |
5361 | } | |
5362 | ||
5363 | right_justify = B_FALSE; | |
5364 | if (pl->pl_prop != ZPROP_INVAL) { | |
a05dfd00 | 5365 | if (zpool_get_prop(zhp, pl->pl_prop, property, |
2a8b84b7 | 5366 | sizeof (property), NULL, cb->cb_literal) != 0) |
34dc7c2f BB |
5367 | propstr = "-"; |
5368 | else | |
5369 | propstr = property; | |
5370 | ||
5371 | right_justify = zpool_prop_align_right(pl->pl_prop); | |
9ae529ec CS |
5372 | } else if ((zpool_prop_feature(pl->pl_user_prop) || |
5373 | zpool_prop_unsupported(pl->pl_user_prop)) && | |
5374 | zpool_prop_get_feature(zhp, pl->pl_user_prop, property, | |
5375 | sizeof (property)) == 0) { | |
5376 | propstr = property; | |
34dc7c2f BB |
5377 | } else { |
5378 | propstr = "-"; | |
5379 | } | |
5380 | ||
34dc7c2f BB |
5381 | |
5382 | /* | |
5383 | * If this is being called in scripted mode, or if this is the | |
5384 | * last column and it is left-justified, don't include a width | |
5385 | * format specifier. | |
5386 | */ | |
1bd201e7 | 5387 | if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) |
34dc7c2f BB |
5388 | (void) printf("%s", propstr); |
5389 | else if (right_justify) | |
1bd201e7 | 5390 | (void) printf("%*s", (int)width, propstr); |
34dc7c2f | 5391 | else |
1bd201e7 | 5392 | (void) printf("%-*s", (int)width, propstr); |
34dc7c2f BB |
5393 | } |
5394 | ||
5395 | (void) printf("\n"); | |
5396 | } | |
5397 | ||
1bd201e7 | 5398 | static void |
c5eea0ab BB |
5399 | print_one_column(zpool_prop_t prop, uint64_t value, const char *str, |
5400 | boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format) | |
1bd201e7 CS |
5401 | { |
5402 | char propval[64]; | |
5403 | boolean_t fixed; | |
5404 | size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); | |
5405 | ||
a05dfd00 GW |
5406 | switch (prop) { |
5407 | case ZPOOL_PROP_EXPANDSZ: | |
d2734cce | 5408 | case ZPOOL_PROP_CHECKPOINT: |
c5eea0ab | 5409 | case ZPOOL_PROP_DEDUPRATIO: |
a05dfd00 GW |
5410 | if (value == 0) |
5411 | (void) strlcpy(propval, "-", sizeof (propval)); | |
5412 | else | |
bc2d8093 CE |
5413 | zfs_nicenum_format(value, propval, sizeof (propval), |
5414 | format); | |
a05dfd00 GW |
5415 | break; |
5416 | case ZPOOL_PROP_FRAGMENTATION: | |
5417 | if (value == ZFS_FRAG_INVALID) { | |
5418 | (void) strlcpy(propval, "-", sizeof (propval)); | |
bc2d8093 CE |
5419 | } else if (format == ZFS_NICENUM_RAW) { |
5420 | (void) snprintf(propval, sizeof (propval), "%llu", | |
5421 | (unsigned long long)value); | |
a05dfd00 GW |
5422 | } else { |
5423 | (void) snprintf(propval, sizeof (propval), "%llu%%", | |
5424 | (unsigned long long)value); | |
5425 | } | |
5426 | break; | |
5427 | case ZPOOL_PROP_CAPACITY: | |
cc99f275 | 5428 | /* capacity value is in parts-per-10,000 (aka permyriad) */ |
bc2d8093 CE |
5429 | if (format == ZFS_NICENUM_RAW) |
5430 | (void) snprintf(propval, sizeof (propval), "%llu", | |
cc99f275 | 5431 | (unsigned long long)value / 100); |
bc2d8093 | 5432 | else |
cc99f275 DB |
5433 | (void) snprintf(propval, sizeof (propval), |
5434 | value < 1000 ? "%1.2f%%" : value < 10000 ? | |
5435 | "%2.1f%%" : "%3.0f%%", value / 100.0); | |
a05dfd00 | 5436 | break; |
c5eea0ab BB |
5437 | case ZPOOL_PROP_HEALTH: |
5438 | width = 8; | |
5439 | snprintf(propval, sizeof (propval), "%-*s", (int)width, str); | |
5440 | break; | |
a05dfd00 | 5441 | default: |
bc2d8093 | 5442 | zfs_nicenum_format(value, propval, sizeof (propval), format); |
a05dfd00 GW |
5443 | } |
5444 | ||
5445 | if (!valid) | |
5446 | (void) strlcpy(propval, "-", sizeof (propval)); | |
1bd201e7 CS |
5447 | |
5448 | if (scripted) | |
5449 | (void) printf("\t%s", propval); | |
5450 | else | |
5451 | (void) printf(" %*s", (int)width, propval); | |
5452 | } | |
5453 | ||
cc99f275 DB |
5454 | /* |
5455 | * print static default line per vdev | |
5456 | * not compatible with '-o' <proplist> option | |
5457 | */ | |
1bd201e7 CS |
5458 | void |
5459 | print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, | |
c5eea0ab | 5460 | list_cbdata_t *cb, int depth, boolean_t isspare) |
1bd201e7 CS |
5461 | { |
5462 | nvlist_t **child; | |
5463 | vdev_stat_t *vs; | |
5464 | uint_t c, children; | |
5465 | char *vname; | |
5466 | boolean_t scripted = cb->cb_scripted; | |
8e4c5c9a | 5467 | uint64_t islog = B_FALSE; |
c5eea0ab BB |
5468 | char *dashes = "%-*s - - - - " |
5469 | "- - - - -\n"; | |
1bd201e7 CS |
5470 | |
5471 | verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, | |
5472 | (uint64_t **)&vs, &c) == 0); | |
5473 | ||
5474 | if (name != NULL) { | |
a05dfd00 GW |
5475 | boolean_t toplevel = (vs->vs_space != 0); |
5476 | uint64_t cap; | |
bc2d8093 | 5477 | enum zfs_nicenum_format format; |
c5eea0ab | 5478 | const char *state; |
bc2d8093 CE |
5479 | |
5480 | if (cb->cb_literal) | |
5481 | format = ZFS_NICENUM_RAW; | |
5482 | else | |
5483 | format = ZFS_NICENUM_1024; | |
a05dfd00 | 5484 | |
a1d477c2 MA |
5485 | if (strcmp(name, VDEV_TYPE_INDIRECT) == 0) |
5486 | return; | |
5487 | ||
1bd201e7 CS |
5488 | if (scripted) |
5489 | (void) printf("\t%s", name); | |
5490 | else if (strlen(name) + depth > cb->cb_namewidth) | |
5491 | (void) printf("%*s%s", depth, "", name); | |
5492 | else | |
5493 | (void) printf("%*s%s%*s", depth, "", name, | |
5494 | (int)(cb->cb_namewidth - strlen(name) - depth), ""); | |
5495 | ||
a05dfd00 GW |
5496 | /* |
5497 | * Print the properties for the individual vdevs. Some | |
5498 | * properties are only applicable to toplevel vdevs. The | |
5499 | * 'toplevel' boolean value is passed to the print_one_column() | |
5500 | * to indicate that the value is valid. | |
5501 | */ | |
c5eea0ab | 5502 | print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL, scripted, |
bc2d8093 | 5503 | toplevel, format); |
c5eea0ab | 5504 | print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL, |
bc2d8093 | 5505 | scripted, toplevel, format); |
c5eea0ab BB |
5506 | print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc, |
5507 | NULL, scripted, toplevel, format); | |
d2734cce | 5508 | print_one_column(ZPOOL_PROP_CHECKPOINT, |
c5eea0ab BB |
5509 | vs->vs_checkpoint_space, NULL, scripted, toplevel, format); |
5510 | print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL, | |
5511 | scripted, B_TRUE, format); | |
a05dfd00 | 5512 | print_one_column(ZPOOL_PROP_FRAGMENTATION, |
c5eea0ab | 5513 | vs->vs_fragmentation, NULL, scripted, |
bc2d8093 CE |
5514 | (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel), |
5515 | format); | |
a05dfd00 | 5516 | cap = (vs->vs_space == 0) ? 0 : |
cc99f275 | 5517 | (vs->vs_alloc * 10000 / vs->vs_space); |
c5eea0ab BB |
5518 | print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL, |
5519 | scripted, toplevel, format); | |
5520 | print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL, | |
5521 | scripted, toplevel, format); | |
5522 | state = zpool_state_to_name(vs->vs_state, vs->vs_aux); | |
5523 | if (isspare) { | |
5524 | if (vs->vs_aux == VDEV_AUX_SPARED) | |
5525 | state = "INUSE"; | |
5526 | else if (vs->vs_state == VDEV_STATE_HEALTHY) | |
5527 | state = "AVAIL"; | |
5528 | } | |
5529 | print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted, | |
5530 | B_TRUE, format); | |
1bd201e7 CS |
5531 | (void) printf("\n"); |
5532 | } | |
5533 | ||
5534 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
5535 | &child, &children) != 0) | |
5536 | return; | |
5537 | ||
cc99f275 | 5538 | /* list the normal vdevs first */ |
1bd201e7 CS |
5539 | for (c = 0; c < children; c++) { |
5540 | uint64_t ishole = B_FALSE; | |
5541 | ||
5542 | if (nvlist_lookup_uint64(child[c], | |
5543 | ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) | |
5544 | continue; | |
5545 | ||
8e4c5c9a | 5546 | if (nvlist_lookup_uint64(child[c], |
cc99f275 DB |
5547 | ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) |
5548 | continue; | |
5549 | ||
5550 | if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS)) | |
8e4c5c9a | 5551 | continue; |
8e4c5c9a | 5552 | |
d2f3e292 RY |
5553 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
5554 | cb->cb_name_flags); | |
c5eea0ab | 5555 | print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE); |
1bd201e7 CS |
5556 | free(vname); |
5557 | } | |
5558 | ||
cc99f275 DB |
5559 | /* list the classes: 'logs', 'dedup', and 'special' */ |
5560 | for (uint_t n = 0; n < 3; n++) { | |
5561 | boolean_t printed = B_FALSE; | |
5562 | ||
8e4c5c9a | 5563 | for (c = 0; c < children; c++) { |
cc99f275 DB |
5564 | char *bias = NULL; |
5565 | char *type = NULL; | |
5566 | ||
8e4c5c9a | 5567 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, |
cc99f275 DB |
5568 | &islog) == 0 && islog) { |
5569 | bias = VDEV_ALLOC_CLASS_LOGS; | |
5570 | } else { | |
5571 | (void) nvlist_lookup_string(child[c], | |
5572 | ZPOOL_CONFIG_ALLOCATION_BIAS, &bias); | |
5573 | (void) nvlist_lookup_string(child[c], | |
5574 | ZPOOL_CONFIG_TYPE, &type); | |
5575 | } | |
5576 | if (bias == NULL || strcmp(bias, class_name[n]) != 0) | |
8e4c5c9a | 5577 | continue; |
cc99f275 DB |
5578 | if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0) |
5579 | continue; | |
5580 | ||
5581 | if (!printed) { | |
5582 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
5583 | (void) printf(dashes, cb->cb_namewidth, | |
5584 | class_name[n]); | |
5585 | printed = B_TRUE; | |
5586 | } | |
d2f3e292 RY |
5587 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
5588 | cb->cb_name_flags); | |
c5eea0ab BB |
5589 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
5590 | B_FALSE); | |
8e4c5c9a JWK |
5591 | free(vname); |
5592 | } | |
5593 | } | |
5594 | ||
1bd201e7 | 5595 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, |
8e4c5c9a JWK |
5596 | &child, &children) == 0 && children > 0) { |
5597 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
5598 | (void) printf(dashes, cb->cb_namewidth, "cache"); | |
5599 | for (c = 0; c < children; c++) { | |
d2f3e292 RY |
5600 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
5601 | cb->cb_name_flags); | |
c5eea0ab BB |
5602 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
5603 | B_FALSE); | |
8e4c5c9a JWK |
5604 | free(vname); |
5605 | } | |
5606 | } | |
1bd201e7 | 5607 | |
8e4c5c9a JWK |
5608 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child, |
5609 | &children) == 0 && children > 0) { | |
5610 | /* LINTED E_SEC_PRINTF_VAR_FMT */ | |
5611 | (void) printf(dashes, cb->cb_namewidth, "spare"); | |
1bd201e7 | 5612 | for (c = 0; c < children; c++) { |
d2f3e292 RY |
5613 | vname = zpool_vdev_name(g_zfs, zhp, child[c], |
5614 | cb->cb_name_flags); | |
c5eea0ab BB |
5615 | print_list_stats(zhp, vname, child[c], cb, depth + 2, |
5616 | B_TRUE); | |
1bd201e7 CS |
5617 | free(vname); |
5618 | } | |
5619 | } | |
5620 | } | |
5621 | ||
34dc7c2f BB |
5622 | /* |
5623 | * Generic callback function to list a pool. | |
5624 | */ | |
5625 | int | |
5626 | list_callback(zpool_handle_t *zhp, void *data) | |
5627 | { | |
5628 | list_cbdata_t *cbp = data; | |
5629 | ||
c5eea0ab | 5630 | print_pool(zhp, cbp); |
34dc7c2f | 5631 | |
cc99f275 | 5632 | if (cbp->cb_verbose) { |
c5eea0ab | 5633 | nvlist_t *config, *nvroot; |
cc99f275 | 5634 | |
c5eea0ab | 5635 | config = zpool_get_config(zhp, NULL); |
cc99f275 DB |
5636 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, |
5637 | &nvroot) == 0); | |
c5eea0ab | 5638 | print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE); |
cc99f275 DB |
5639 | } |
5640 | ||
c5eea0ab BB |
5641 | return (0); |
5642 | } | |
cc99f275 | 5643 | |
c5eea0ab BB |
5644 | /* |
5645 | * Set the minimum pool/vdev name column width. The width must be at least 9, | |
5646 | * but may be as large as needed. | |
5647 | */ | |
5648 | static int | |
5649 | get_namewidth_list(zpool_handle_t *zhp, void *data) | |
5650 | { | |
5651 | list_cbdata_t *cb = data; | |
5652 | int width; | |
5653 | ||
5654 | width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags, | |
5655 | cb->cb_verbose); | |
1bd201e7 | 5656 | |
c5eea0ab BB |
5657 | if (width < 9) |
5658 | width = 9; | |
5659 | ||
5660 | cb->cb_namewidth = width; | |
34dc7c2f BB |
5661 | |
5662 | return (0); | |
5663 | } | |
5664 | ||
5665 | /* | |
2a8b84b7 | 5666 | * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] |
34dc7c2f | 5667 | * |
d2f3e292 | 5668 | * -g Display guid for individual vdev name. |
34dc7c2f BB |
5669 | * -H Scripted mode. Don't display headers, and separate properties |
5670 | * by a single tab. | |
d2f3e292 | 5671 | * -L Follow links when resolving vdev path name. |
34dc7c2f | 5672 | * -o List of properties to display. Defaults to |
a05dfd00 GW |
5673 | * "name,size,allocated,free,expandsize,fragmentation,capacity," |
5674 | * "dedupratio,health,altroot" | |
c5eea0ab | 5675 | * -p Display values in parsable (exact) format. |
a77f29f9 | 5676 | * -P Display full path for vdev name. |
428870ff | 5677 | * -T Display a timestamp in date(1) or Unix format |
34dc7c2f BB |
5678 | * |
5679 | * List all pools in the system, whether or not they're healthy. Output space | |
5680 | * statistics for each one, as well as health status summary. | |
5681 | */ | |
5682 | int | |
5683 | zpool_do_list(int argc, char **argv) | |
5684 | { | |
5685 | int c; | |
cd72af9c | 5686 | int ret = 0; |
34dc7c2f BB |
5687 | list_cbdata_t cb = { 0 }; |
5688 | static char default_props[] = | |
d2734cce SD |
5689 | "name,size,allocated,free,checkpoint,expandsize,fragmentation," |
5690 | "capacity,dedupratio,health,altroot"; | |
34dc7c2f | 5691 | char *props = default_props; |
193a37cb TH |
5692 | float interval = 0; |
5693 | unsigned long count = 0; | |
1bd201e7 CS |
5694 | zpool_list_t *list; |
5695 | boolean_t first = B_TRUE; | |
34dc7c2f BB |
5696 | |
5697 | /* check options */ | |
2a8b84b7 | 5698 | while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) { |
34dc7c2f | 5699 | switch (c) { |
d2f3e292 RY |
5700 | case 'g': |
5701 | cb.cb_name_flags |= VDEV_NAME_GUID; | |
5702 | break; | |
34dc7c2f BB |
5703 | case 'H': |
5704 | cb.cb_scripted = B_TRUE; | |
5705 | break; | |
d2f3e292 RY |
5706 | case 'L': |
5707 | cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
5708 | break; | |
34dc7c2f BB |
5709 | case 'o': |
5710 | props = optarg; | |
5711 | break; | |
a77f29f9 | 5712 | case 'P': |
d2f3e292 RY |
5713 | cb.cb_name_flags |= VDEV_NAME_PATH; |
5714 | break; | |
2a8b84b7 AS |
5715 | case 'p': |
5716 | cb.cb_literal = B_TRUE; | |
5717 | break; | |
428870ff BB |
5718 | case 'T': |
5719 | get_timestamp_arg(*optarg); | |
5720 | break; | |
1bd201e7 CS |
5721 | case 'v': |
5722 | cb.cb_verbose = B_TRUE; | |
cc99f275 | 5723 | cb.cb_namewidth = 8; /* 8 until precalc is avail */ |
1bd201e7 | 5724 | break; |
34dc7c2f BB |
5725 | case ':': |
5726 | (void) fprintf(stderr, gettext("missing argument for " | |
5727 | "'%c' option\n"), optopt); | |
5728 | usage(B_FALSE); | |
5729 | break; | |
5730 | case '?': | |
5731 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
5732 | optopt); | |
5733 | usage(B_FALSE); | |
5734 | } | |
5735 | } | |
5736 | ||
5737 | argc -= optind; | |
5738 | argv += optind; | |
5739 | ||
428870ff BB |
5740 | get_interval_count(&argc, argv, &interval, &count); |
5741 | ||
34dc7c2f BB |
5742 | if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) |
5743 | usage(B_FALSE); | |
5744 | ||
428870ff | 5745 | for (;;) { |
3e43edd2 GW |
5746 | if ((list = pool_list_get(argc, argv, &cb.cb_proplist, |
5747 | &ret)) == NULL) | |
5748 | return (1); | |
1bd201e7 CS |
5749 | |
5750 | if (pool_list_count(list) == 0) | |
5751 | break; | |
34dc7c2f | 5752 | |
c5eea0ab BB |
5753 | cb.cb_namewidth = 0; |
5754 | (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb); | |
5755 | ||
428870ff BB |
5756 | if (timestamp_fmt != NODATE) |
5757 | print_timestamp(timestamp_fmt); | |
34dc7c2f | 5758 | |
1bd201e7 CS |
5759 | if (!cb.cb_scripted && (first || cb.cb_verbose)) { |
5760 | print_header(&cb); | |
5761 | first = B_FALSE; | |
428870ff | 5762 | } |
1bd201e7 | 5763 | ret = pool_list_iter(list, B_TRUE, list_callback, &cb); |
428870ff BB |
5764 | |
5765 | if (interval == 0) | |
5766 | break; | |
5767 | ||
5768 | if (count != 0 && --count == 0) | |
5769 | break; | |
5770 | ||
3e43edd2 | 5771 | pool_list_free(list); |
193a37cb | 5772 | (void) fsleep(interval); |
34dc7c2f BB |
5773 | } |
5774 | ||
3e43edd2 GW |
5775 | if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { |
5776 | (void) printf(gettext("no pools available\n")); | |
5777 | ret = 0; | |
5778 | } | |
5779 | ||
5780 | pool_list_free(list); | |
428870ff | 5781 | zprop_free_list(cb.cb_proplist); |
34dc7c2f BB |
5782 | return (ret); |
5783 | } | |
5784 | ||
34dc7c2f BB |
5785 | static int |
5786 | zpool_do_attach_or_replace(int argc, char **argv, int replacing) | |
5787 | { | |
5788 | boolean_t force = B_FALSE; | |
5789 | int c; | |
5790 | nvlist_t *nvroot; | |
5791 | char *poolname, *old_disk, *new_disk; | |
5792 | zpool_handle_t *zhp; | |
df831108 CP |
5793 | nvlist_t *props = NULL; |
5794 | char *propval; | |
34dc7c2f BB |
5795 | int ret; |
5796 | ||
5797 | /* check options */ | |
4588bf57 | 5798 | while ((c = getopt(argc, argv, "fo:")) != -1) { |
34dc7c2f BB |
5799 | switch (c) { |
5800 | case 'f': | |
5801 | force = B_TRUE; | |
5802 | break; | |
df831108 CP |
5803 | case 'o': |
5804 | if ((propval = strchr(optarg, '=')) == NULL) { | |
5805 | (void) fprintf(stderr, gettext("missing " | |
5806 | "'=' for -o option\n")); | |
5807 | usage(B_FALSE); | |
5808 | } | |
5809 | *propval = '\0'; | |
5810 | propval++; | |
5811 | ||
5812 | if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) || | |
5813 | (add_prop_list(optarg, propval, &props, B_TRUE))) | |
5814 | usage(B_FALSE); | |
5815 | break; | |
34dc7c2f BB |
5816 | case '?': |
5817 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
5818 | optopt); | |
5819 | usage(B_FALSE); | |
5820 | } | |
5821 | } | |
5822 | ||
5823 | argc -= optind; | |
5824 | argv += optind; | |
5825 | ||
5826 | /* get pool name and check number of arguments */ | |
5827 | if (argc < 1) { | |
5828 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
5829 | usage(B_FALSE); | |
5830 | } | |
5831 | ||
5832 | poolname = argv[0]; | |
5833 | ||
5834 | if (argc < 2) { | |
5835 | (void) fprintf(stderr, | |
5836 | gettext("missing <device> specification\n")); | |
5837 | usage(B_FALSE); | |
5838 | } | |
5839 | ||
5840 | old_disk = argv[1]; | |
5841 | ||
5842 | if (argc < 3) { | |
5843 | if (!replacing) { | |
5844 | (void) fprintf(stderr, | |
5845 | gettext("missing <new_device> specification\n")); | |
5846 | usage(B_FALSE); | |
5847 | } | |
5848 | new_disk = old_disk; | |
5849 | argc -= 1; | |
5850 | argv += 1; | |
5851 | } else { | |
5852 | new_disk = argv[2]; | |
5853 | argc -= 2; | |
5854 | argv += 2; | |
5855 | } | |
5856 | ||
5857 | if (argc > 1) { | |
5858 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
5859 | usage(B_FALSE); | |
5860 | } | |
5861 | ||
a425f5bf | 5862 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { |
5863 | nvlist_free(props); | |
34dc7c2f | 5864 | return (1); |
a425f5bf | 5865 | } |
34dc7c2f BB |
5866 | |
5867 | if (zpool_get_config(zhp, NULL) == NULL) { | |
5868 | (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), | |
5869 | poolname); | |
5870 | zpool_close(zhp); | |
a425f5bf | 5871 | nvlist_free(props); |
34dc7c2f BB |
5872 | return (1); |
5873 | } | |
5874 | ||
dddef7d6 | 5875 | /* unless manually specified use "ashift" pool property (if set) */ |
5876 | if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) { | |
5877 | int intval; | |
5878 | zprop_source_t src; | |
5879 | char strval[ZPOOL_MAXPROPLEN]; | |
5880 | ||
5881 | intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src); | |
5882 | if (src != ZPROP_SRC_DEFAULT) { | |
5883 | (void) sprintf(strval, "%" PRId32, intval); | |
5884 | verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval, | |
5885 | &props, B_TRUE) == 0); | |
5886 | } | |
5887 | } | |
5888 | ||
df831108 | 5889 | nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE, |
b128c09f | 5890 | argc, argv); |
34dc7c2f BB |
5891 | if (nvroot == NULL) { |
5892 | zpool_close(zhp); | |
a425f5bf | 5893 | nvlist_free(props); |
34dc7c2f BB |
5894 | return (1); |
5895 | } | |
5896 | ||
5897 | ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing); | |
5898 | ||
a425f5bf | 5899 | nvlist_free(props); |
34dc7c2f BB |
5900 | nvlist_free(nvroot); |
5901 | zpool_close(zhp); | |
5902 | ||
5903 | return (ret); | |
5904 | } | |
5905 | ||
5906 | /* | |
5907 | * zpool replace [-f] <pool> <device> <new_device> | |
5908 | * | |
5909 | * -f Force attach, even if <new_device> appears to be in use. | |
5910 | * | |
5911 | * Replace <device> with <new_device>. | |
5912 | */ | |
5913 | /* ARGSUSED */ | |
5914 | int | |
5915 | zpool_do_replace(int argc, char **argv) | |
5916 | { | |
5917 | return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); | |
5918 | } | |
5919 | ||
5920 | /* | |
df831108 | 5921 | * zpool attach [-f] [-o property=value] <pool> <device> <new_device> |
34dc7c2f BB |
5922 | * |
5923 | * -f Force attach, even if <new_device> appears to be in use. | |
df831108 | 5924 | * -o Set property=value. |
34dc7c2f BB |
5925 | * |
5926 | * Attach <new_device> to the mirror containing <device>. If <device> is not | |
5927 | * part of a mirror, then <device> will be transformed into a mirror of | |
5928 | * <device> and <new_device>. In either case, <new_device> will begin life | |
5929 | * with a DTL of [0, now], and will immediately begin to resilver itself. | |
5930 | */ | |
5931 | int | |
5932 | zpool_do_attach(int argc, char **argv) | |
5933 | { | |
5934 | return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); | |
5935 | } | |
5936 | ||
5937 | /* | |
5938 | * zpool detach [-f] <pool> <device> | |
5939 | * | |
5940 | * -f Force detach of <device>, even if DTLs argue against it | |
5941 | * (not supported yet) | |
5942 | * | |
5943 | * Detach a device from a mirror. The operation will be refused if <device> | |
5944 | * is the last device in the mirror, or if the DTLs indicate that this device | |
5945 | * has the only valid copy of some data. | |
5946 | */ | |
5947 | /* ARGSUSED */ | |
5948 | int | |
5949 | zpool_do_detach(int argc, char **argv) | |
5950 | { | |
5951 | int c; | |
5952 | char *poolname, *path; | |
5953 | zpool_handle_t *zhp; | |
5954 | int ret; | |
5955 | ||
5956 | /* check options */ | |
5957 | while ((c = getopt(argc, argv, "f")) != -1) { | |
5958 | switch (c) { | |
5959 | case 'f': | |
5960 | case '?': | |
5961 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
5962 | optopt); | |
5963 | usage(B_FALSE); | |
5964 | } | |
5965 | } | |
5966 | ||
5967 | argc -= optind; | |
5968 | argv += optind; | |
5969 | ||
5970 | /* get pool name and check number of arguments */ | |
5971 | if (argc < 1) { | |
5972 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
5973 | usage(B_FALSE); | |
5974 | } | |
5975 | ||
5976 | if (argc < 2) { | |
5977 | (void) fprintf(stderr, | |
5978 | gettext("missing <device> specification\n")); | |
5979 | usage(B_FALSE); | |
5980 | } | |
5981 | ||
5982 | poolname = argv[0]; | |
5983 | path = argv[1]; | |
5984 | ||
5985 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
5986 | return (1); | |
5987 | ||
5988 | ret = zpool_vdev_detach(zhp, path); | |
5989 | ||
5990 | zpool_close(zhp); | |
5991 | ||
5992 | return (ret); | |
5993 | } | |
5994 | ||
428870ff | 5995 | /* |
a77f29f9 | 5996 | * zpool split [-gLnP] [-o prop=val] ... |
428870ff BB |
5997 | * [-o mntopt] ... |
5998 | * [-R altroot] <pool> <newpool> [<device> ...] | |
5999 | * | |
d2f3e292 RY |
6000 | * -g Display guid for individual vdev name. |
6001 | * -L Follow links when resolving vdev path name. | |
428870ff BB |
6002 | * -n Do not split the pool, but display the resulting layout if |
6003 | * it were to be split. | |
6004 | * -o Set property=value, or set mount options. | |
a77f29f9 | 6005 | * -P Display full path for vdev name. |
428870ff | 6006 | * -R Mount the split-off pool under an alternate root. |
b5256303 | 6007 | * -l Load encryption keys while importing. |
428870ff BB |
6008 | * |
6009 | * Splits the named pool and gives it the new pool name. Devices to be split | |
6010 | * off may be listed, provided that no more than one device is specified | |
6011 | * per top-level vdev mirror. The newly split pool is left in an exported | |
6012 | * state unless -R is specified. | |
6013 | * | |
6014 | * Restrictions: the top-level of the pool pool must only be made up of | |
6015 | * mirrors; all devices in the pool must be healthy; no device may be | |
6016 | * undergoing a resilvering operation. | |
6017 | */ | |
6018 | int | |
6019 | zpool_do_split(int argc, char **argv) | |
6020 | { | |
6021 | char *srcpool, *newpool, *propval; | |
6022 | char *mntopts = NULL; | |
6023 | splitflags_t flags; | |
6024 | int c, ret = 0; | |
b5256303 | 6025 | boolean_t loadkeys = B_FALSE; |
428870ff BB |
6026 | zpool_handle_t *zhp; |
6027 | nvlist_t *config, *props = NULL; | |
6028 | ||
6029 | flags.dryrun = B_FALSE; | |
6030 | flags.import = B_FALSE; | |
d2f3e292 | 6031 | flags.name_flags = 0; |
428870ff BB |
6032 | |
6033 | /* check options */ | |
b5256303 | 6034 | while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) { |
428870ff | 6035 | switch (c) { |
d2f3e292 RY |
6036 | case 'g': |
6037 | flags.name_flags |= VDEV_NAME_GUID; | |
6038 | break; | |
6039 | case 'L': | |
6040 | flags.name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
6041 | break; | |
428870ff BB |
6042 | case 'R': |
6043 | flags.import = B_TRUE; | |
6044 | if (add_prop_list( | |
6045 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, | |
6046 | &props, B_TRUE) != 0) { | |
8a5fc748 | 6047 | nvlist_free(props); |
428870ff BB |
6048 | usage(B_FALSE); |
6049 | } | |
6050 | break; | |
b5256303 TC |
6051 | case 'l': |
6052 | loadkeys = B_TRUE; | |
6053 | break; | |
428870ff BB |
6054 | case 'n': |
6055 | flags.dryrun = B_TRUE; | |
6056 | break; | |
6057 | case 'o': | |
6058 | if ((propval = strchr(optarg, '=')) != NULL) { | |
6059 | *propval = '\0'; | |
6060 | propval++; | |
6061 | if (add_prop_list(optarg, propval, | |
6062 | &props, B_TRUE) != 0) { | |
8a5fc748 | 6063 | nvlist_free(props); |
428870ff BB |
6064 | usage(B_FALSE); |
6065 | } | |
6066 | } else { | |
6067 | mntopts = optarg; | |
6068 | } | |
6069 | break; | |
a77f29f9 | 6070 | case 'P': |
d2f3e292 RY |
6071 | flags.name_flags |= VDEV_NAME_PATH; |
6072 | break; | |
428870ff BB |
6073 | case ':': |
6074 | (void) fprintf(stderr, gettext("missing argument for " | |
6075 | "'%c' option\n"), optopt); | |
6076 | usage(B_FALSE); | |
6077 | break; | |
6078 | case '?': | |
6079 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6080 | optopt); | |
6081 | usage(B_FALSE); | |
6082 | break; | |
6083 | } | |
6084 | } | |
6085 | ||
6086 | if (!flags.import && mntopts != NULL) { | |
6087 | (void) fprintf(stderr, gettext("setting mntopts is only " | |
6088 | "valid when importing the pool\n")); | |
6089 | usage(B_FALSE); | |
6090 | } | |
6091 | ||
b5256303 TC |
6092 | if (!flags.import && loadkeys) { |
6093 | (void) fprintf(stderr, gettext("loading keys is only " | |
6094 | "valid when importing the pool\n")); | |
6095 | usage(B_FALSE); | |
6096 | } | |
6097 | ||
428870ff BB |
6098 | argc -= optind; |
6099 | argv += optind; | |
6100 | ||
6101 | if (argc < 1) { | |
6102 | (void) fprintf(stderr, gettext("Missing pool name\n")); | |
6103 | usage(B_FALSE); | |
6104 | } | |
6105 | if (argc < 2) { | |
6106 | (void) fprintf(stderr, gettext("Missing new pool name\n")); | |
6107 | usage(B_FALSE); | |
6108 | } | |
6109 | ||
6110 | srcpool = argv[0]; | |
6111 | newpool = argv[1]; | |
6112 | ||
6113 | argc -= 2; | |
6114 | argv += 2; | |
6115 | ||
a425f5bf | 6116 | if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) { |
6117 | nvlist_free(props); | |
428870ff | 6118 | return (1); |
a425f5bf | 6119 | } |
428870ff BB |
6120 | |
6121 | config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); | |
6122 | if (config == NULL) { | |
6123 | ret = 1; | |
6124 | } else { | |
6125 | if (flags.dryrun) { | |
6126 | (void) printf(gettext("would create '%s' with the " | |
6127 | "following layout:\n\n"), newpool); | |
cc99f275 | 6128 | print_vdev_tree(NULL, newpool, config, 0, "", |
d2f3e292 | 6129 | flags.name_flags); |
428870ff | 6130 | } |
428870ff BB |
6131 | } |
6132 | ||
6133 | zpool_close(zhp); | |
6134 | ||
a425f5bf | 6135 | if (ret != 0 || flags.dryrun || !flags.import) { |
6136 | nvlist_free(config); | |
6137 | nvlist_free(props); | |
428870ff | 6138 | return (ret); |
a425f5bf | 6139 | } |
428870ff BB |
6140 | |
6141 | /* | |
6142 | * The split was successful. Now we need to open the new | |
6143 | * pool and import it. | |
6144 | */ | |
a425f5bf | 6145 | if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) { |
6146 | nvlist_free(config); | |
6147 | nvlist_free(props); | |
428870ff | 6148 | return (1); |
a425f5bf | 6149 | } |
b5256303 TC |
6150 | |
6151 | if (loadkeys) { | |
6152 | ret = zfs_crypto_attempt_load_keys(g_zfs, newpool); | |
6153 | if (ret != 0) | |
6154 | ret = 1; | |
6155 | } | |
6156 | ||
428870ff BB |
6157 | if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && |
6158 | zpool_enable_datasets(zhp, mntopts, 0) != 0) { | |
6159 | ret = 1; | |
af909a10 | 6160 | (void) fprintf(stderr, gettext("Split was successful, but " |
428870ff BB |
6161 | "the datasets could not all be mounted\n")); |
6162 | (void) fprintf(stderr, gettext("Try doing '%s' with a " | |
6163 | "different altroot\n"), "zpool import"); | |
6164 | } | |
6165 | zpool_close(zhp); | |
a425f5bf | 6166 | nvlist_free(config); |
6167 | nvlist_free(props); | |
428870ff BB |
6168 | |
6169 | return (ret); | |
6170 | } | |
6171 | ||
6172 | ||
6173 | ||
34dc7c2f BB |
6174 | /* |
6175 | * zpool online <pool> <device> ... | |
6176 | */ | |
6177 | int | |
6178 | zpool_do_online(int argc, char **argv) | |
6179 | { | |
6180 | int c, i; | |
6181 | char *poolname; | |
6182 | zpool_handle_t *zhp; | |
6183 | int ret = 0; | |
6184 | vdev_state_t newstate; | |
9babb374 | 6185 | int flags = 0; |
34dc7c2f BB |
6186 | |
6187 | /* check options */ | |
9babb374 | 6188 | while ((c = getopt(argc, argv, "et")) != -1) { |
34dc7c2f | 6189 | switch (c) { |
9babb374 BB |
6190 | case 'e': |
6191 | flags |= ZFS_ONLINE_EXPAND; | |
6192 | break; | |
34dc7c2f BB |
6193 | case 't': |
6194 | case '?': | |
6195 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6196 | optopt); | |
6197 | usage(B_FALSE); | |
6198 | } | |
6199 | } | |
6200 | ||
6201 | argc -= optind; | |
6202 | argv += optind; | |
6203 | ||
6204 | /* get pool name and check number of arguments */ | |
6205 | if (argc < 1) { | |
6206 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
6207 | usage(B_FALSE); | |
6208 | } | |
6209 | if (argc < 2) { | |
6210 | (void) fprintf(stderr, gettext("missing device name\n")); | |
6211 | usage(B_FALSE); | |
6212 | } | |
6213 | ||
6214 | poolname = argv[0]; | |
6215 | ||
6216 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
6217 | return (1); | |
6218 | ||
6219 | for (i = 1; i < argc; i++) { | |
9babb374 | 6220 | if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { |
34dc7c2f BB |
6221 | if (newstate != VDEV_STATE_HEALTHY) { |
6222 | (void) printf(gettext("warning: device '%s' " | |
6223 | "onlined, but remains in faulted state\n"), | |
6224 | argv[i]); | |
6225 | if (newstate == VDEV_STATE_FAULTED) | |
6226 | (void) printf(gettext("use 'zpool " | |
6227 | "clear' to restore a faulted " | |
6228 | "device\n")); | |
6229 | else | |
6230 | (void) printf(gettext("use 'zpool " | |
6231 | "replace' to replace devices " | |
6232 | "that are no longer present\n")); | |
6233 | } | |
6234 | } else { | |
6235 | ret = 1; | |
6236 | } | |
6237 | } | |
6238 | ||
6239 | zpool_close(zhp); | |
6240 | ||
6241 | return (ret); | |
6242 | } | |
6243 | ||
6244 | /* | |
6245 | * zpool offline [-ft] <pool> <device> ... | |
6246 | * | |
4a283c7f | 6247 | * -f Force the device into a faulted state. |
34dc7c2f | 6248 | * |
4a283c7f | 6249 | * -t Only take the device off-line temporarily. The offline/faulted |
34dc7c2f BB |
6250 | * state will not be persistent across reboots. |
6251 | */ | |
6252 | /* ARGSUSED */ | |
6253 | int | |
6254 | zpool_do_offline(int argc, char **argv) | |
6255 | { | |
6256 | int c, i; | |
6257 | char *poolname; | |
6258 | zpool_handle_t *zhp; | |
6259 | int ret = 0; | |
6260 | boolean_t istmp = B_FALSE; | |
4a283c7f | 6261 | boolean_t fault = B_FALSE; |
34dc7c2f BB |
6262 | |
6263 | /* check options */ | |
6264 | while ((c = getopt(argc, argv, "ft")) != -1) { | |
6265 | switch (c) { | |
4a283c7f TH |
6266 | case 'f': |
6267 | fault = B_TRUE; | |
6268 | break; | |
34dc7c2f BB |
6269 | case 't': |
6270 | istmp = B_TRUE; | |
6271 | break; | |
34dc7c2f BB |
6272 | case '?': |
6273 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6274 | optopt); | |
6275 | usage(B_FALSE); | |
6276 | } | |
6277 | } | |
6278 | ||
6279 | argc -= optind; | |
6280 | argv += optind; | |
6281 | ||
6282 | /* get pool name and check number of arguments */ | |
6283 | if (argc < 1) { | |
6284 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
6285 | usage(B_FALSE); | |
6286 | } | |
6287 | if (argc < 2) { | |
6288 | (void) fprintf(stderr, gettext("missing device name\n")); | |
6289 | usage(B_FALSE); | |
6290 | } | |
6291 | ||
6292 | poolname = argv[0]; | |
6293 | ||
6294 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
6295 | return (1); | |
6296 | ||
6297 | for (i = 1; i < argc; i++) { | |
4a283c7f TH |
6298 | if (fault) { |
6299 | uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]); | |
6300 | vdev_aux_t aux; | |
6301 | if (istmp == B_FALSE) { | |
6302 | /* Force the fault to persist across imports */ | |
6303 | aux = VDEV_AUX_EXTERNAL_PERSIST; | |
6304 | } else { | |
6305 | aux = VDEV_AUX_EXTERNAL; | |
6306 | } | |
6307 | ||
6308 | if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0) | |
6309 | ret = 1; | |
6310 | } else { | |
6311 | if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) | |
6312 | ret = 1; | |
6313 | } | |
34dc7c2f BB |
6314 | } |
6315 | ||
6316 | zpool_close(zhp); | |
6317 | ||
6318 | return (ret); | |
6319 | } | |
6320 | ||
6321 | /* | |
6322 | * zpool clear <pool> [device] | |
6323 | * | |
6324 | * Clear all errors associated with a pool or a particular device. | |
6325 | */ | |
6326 | int | |
6327 | zpool_do_clear(int argc, char **argv) | |
6328 | { | |
428870ff | 6329 | int c; |
34dc7c2f | 6330 | int ret = 0; |
428870ff BB |
6331 | boolean_t dryrun = B_FALSE; |
6332 | boolean_t do_rewind = B_FALSE; | |
6333 | boolean_t xtreme_rewind = B_FALSE; | |
6334 | uint32_t rewind_policy = ZPOOL_NO_REWIND; | |
6335 | nvlist_t *policy = NULL; | |
34dc7c2f BB |
6336 | zpool_handle_t *zhp; |
6337 | char *pool, *device; | |
6338 | ||
428870ff BB |
6339 | /* check options */ |
6340 | while ((c = getopt(argc, argv, "FnX")) != -1) { | |
6341 | switch (c) { | |
6342 | case 'F': | |
6343 | do_rewind = B_TRUE; | |
6344 | break; | |
6345 | case 'n': | |
6346 | dryrun = B_TRUE; | |
6347 | break; | |
6348 | case 'X': | |
6349 | xtreme_rewind = B_TRUE; | |
6350 | break; | |
6351 | case '?': | |
6352 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6353 | optopt); | |
6354 | usage(B_FALSE); | |
6355 | } | |
6356 | } | |
6357 | ||
6358 | argc -= optind; | |
6359 | argv += optind; | |
6360 | ||
6361 | if (argc < 1) { | |
34dc7c2f BB |
6362 | (void) fprintf(stderr, gettext("missing pool name\n")); |
6363 | usage(B_FALSE); | |
6364 | } | |
6365 | ||
428870ff | 6366 | if (argc > 2) { |
34dc7c2f BB |
6367 | (void) fprintf(stderr, gettext("too many arguments\n")); |
6368 | usage(B_FALSE); | |
6369 | } | |
6370 | ||
428870ff BB |
6371 | if ((dryrun || xtreme_rewind) && !do_rewind) { |
6372 | (void) fprintf(stderr, | |
6373 | gettext("-n or -X only meaningful with -F\n")); | |
6374 | usage(B_FALSE); | |
6375 | } | |
6376 | if (dryrun) | |
6377 | rewind_policy = ZPOOL_TRY_REWIND; | |
6378 | else if (do_rewind) | |
6379 | rewind_policy = ZPOOL_DO_REWIND; | |
6380 | if (xtreme_rewind) | |
6381 | rewind_policy |= ZPOOL_EXTREME_REWIND; | |
6382 | ||
6383 | /* In future, further rewind policy choices can be passed along here */ | |
6384 | if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || | |
8a393be3 PZ |
6385 | nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, |
6386 | rewind_policy) != 0) { | |
428870ff | 6387 | return (1); |
8a393be3 | 6388 | } |
428870ff BB |
6389 | |
6390 | pool = argv[0]; | |
6391 | device = argc == 2 ? argv[1] : NULL; | |
34dc7c2f | 6392 | |
428870ff BB |
6393 | if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { |
6394 | nvlist_free(policy); | |
34dc7c2f | 6395 | return (1); |
428870ff | 6396 | } |
34dc7c2f | 6397 | |
428870ff | 6398 | if (zpool_clear(zhp, device, policy) != 0) |
34dc7c2f BB |
6399 | ret = 1; |
6400 | ||
6401 | zpool_close(zhp); | |
6402 | ||
428870ff BB |
6403 | nvlist_free(policy); |
6404 | ||
34dc7c2f BB |
6405 | return (ret); |
6406 | } | |
6407 | ||
3541dc6d GA |
6408 | /* |
6409 | * zpool reguid <pool> | |
6410 | */ | |
6411 | int | |
6412 | zpool_do_reguid(int argc, char **argv) | |
6413 | { | |
6414 | int c; | |
6415 | char *poolname; | |
6416 | zpool_handle_t *zhp; | |
6417 | int ret = 0; | |
6418 | ||
6419 | /* check options */ | |
6420 | while ((c = getopt(argc, argv, "")) != -1) { | |
6421 | switch (c) { | |
6422 | case '?': | |
6423 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6424 | optopt); | |
6425 | usage(B_FALSE); | |
6426 | } | |
6427 | } | |
6428 | ||
6429 | argc -= optind; | |
6430 | argv += optind; | |
6431 | ||
6432 | /* get pool name and check number of arguments */ | |
6433 | if (argc < 1) { | |
6434 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
6435 | usage(B_FALSE); | |
6436 | } | |
6437 | ||
6438 | if (argc > 1) { | |
6439 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
6440 | usage(B_FALSE); | |
6441 | } | |
6442 | ||
6443 | poolname = argv[0]; | |
6444 | if ((zhp = zpool_open(g_zfs, poolname)) == NULL) | |
6445 | return (1); | |
6446 | ||
6447 | ret = zpool_reguid(zhp); | |
6448 | ||
6449 | zpool_close(zhp); | |
6450 | return (ret); | |
6451 | } | |
6452 | ||
6453 | ||
1bd201e7 CS |
6454 | /* |
6455 | * zpool reopen <pool> | |
6456 | * | |
6457 | * Reopen the pool so that the kernel can update the sizes of all vdevs. | |
1bd201e7 CS |
6458 | */ |
6459 | int | |
6460 | zpool_do_reopen(int argc, char **argv) | |
6461 | { | |
5853fe79 | 6462 | int c; |
1bd201e7 | 6463 | int ret = 0; |
d3f2cd7e | 6464 | boolean_t scrub_restart = B_TRUE; |
1bd201e7 | 6465 | |
5853fe79 | 6466 | /* check options */ |
d3f2cd7e | 6467 | while ((c = getopt(argc, argv, "n")) != -1) { |
5853fe79 | 6468 | switch (c) { |
d3f2cd7e AB |
6469 | case 'n': |
6470 | scrub_restart = B_FALSE; | |
6471 | break; | |
5853fe79 GW |
6472 | case '?': |
6473 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6474 | optopt); | |
6475 | usage(B_FALSE); | |
6476 | } | |
6477 | } | |
6478 | ||
d3f2cd7e AB |
6479 | argc -= optind; |
6480 | argv += optind; | |
1bd201e7 | 6481 | |
d3f2cd7e AB |
6482 | /* if argc == 0 we will execute zpool_reopen_one on all pools */ |
6483 | ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_reopen_one, | |
6484 | &scrub_restart); | |
1bd201e7 | 6485 | |
1bd201e7 CS |
6486 | return (ret); |
6487 | } | |
6488 | ||
34dc7c2f BB |
6489 | typedef struct scrub_cbdata { |
6490 | int cb_type; | |
6491 | int cb_argc; | |
6492 | char **cb_argv; | |
0ea05c64 | 6493 | pool_scrub_cmd_t cb_scrub_cmd; |
34dc7c2f BB |
6494 | } scrub_cbdata_t; |
6495 | ||
d2734cce SD |
6496 | static boolean_t |
6497 | zpool_has_checkpoint(zpool_handle_t *zhp) | |
6498 | { | |
6499 | nvlist_t *config, *nvroot; | |
6500 | ||
6501 | config = zpool_get_config(zhp, NULL); | |
6502 | ||
6503 | if (config != NULL) { | |
6504 | pool_checkpoint_stat_t *pcs = NULL; | |
6505 | uint_t c; | |
6506 | ||
6507 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); | |
6508 | (void) nvlist_lookup_uint64_array(nvroot, | |
6509 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
6510 | ||
6511 | if (pcs == NULL || pcs->pcs_state == CS_NONE) | |
6512 | return (B_FALSE); | |
6513 | ||
6514 | assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS || | |
6515 | pcs->pcs_state == CS_CHECKPOINT_DISCARDING); | |
6516 | return (B_TRUE); | |
6517 | } | |
6518 | ||
6519 | return (B_FALSE); | |
6520 | } | |
6521 | ||
34dc7c2f BB |
6522 | int |
6523 | scrub_callback(zpool_handle_t *zhp, void *data) | |
6524 | { | |
6525 | scrub_cbdata_t *cb = data; | |
6526 | int err; | |
6527 | ||
6528 | /* | |
6529 | * Ignore faulted pools. | |
6530 | */ | |
6531 | if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { | |
80a91e74 | 6532 | (void) fprintf(stderr, gettext("cannot scan '%s': pool is " |
34dc7c2f BB |
6533 | "currently unavailable\n"), zpool_get_name(zhp)); |
6534 | return (1); | |
6535 | } | |
6536 | ||
0ea05c64 | 6537 | err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd); |
34dc7c2f | 6538 | |
d2734cce SD |
6539 | if (err == 0 && zpool_has_checkpoint(zhp) && |
6540 | cb->cb_type == POOL_SCAN_SCRUB) { | |
6541 | (void) printf(gettext("warning: will not scrub state that " | |
6542 | "belongs to the checkpoint of pool '%s'\n"), | |
6543 | zpool_get_name(zhp)); | |
6544 | } | |
6545 | ||
34dc7c2f BB |
6546 | return (err != 0); |
6547 | } | |
6548 | ||
6549 | /* | |
0ea05c64 | 6550 | * zpool scrub [-s | -p] <pool> ... |
34dc7c2f BB |
6551 | * |
6552 | * -s Stop. Stops any in-progress scrub. | |
0ea05c64 | 6553 | * -p Pause. Pause in-progress scrub. |
34dc7c2f BB |
6554 | */ |
6555 | int | |
6556 | zpool_do_scrub(int argc, char **argv) | |
6557 | { | |
6558 | int c; | |
6559 | scrub_cbdata_t cb; | |
6560 | ||
428870ff | 6561 | cb.cb_type = POOL_SCAN_SCRUB; |
0ea05c64 | 6562 | cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; |
34dc7c2f BB |
6563 | |
6564 | /* check options */ | |
0ea05c64 | 6565 | while ((c = getopt(argc, argv, "sp")) != -1) { |
34dc7c2f BB |
6566 | switch (c) { |
6567 | case 's': | |
428870ff | 6568 | cb.cb_type = POOL_SCAN_NONE; |
34dc7c2f | 6569 | break; |
0ea05c64 AP |
6570 | case 'p': |
6571 | cb.cb_scrub_cmd = POOL_SCRUB_PAUSE; | |
6572 | break; | |
34dc7c2f BB |
6573 | case '?': |
6574 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6575 | optopt); | |
6576 | usage(B_FALSE); | |
6577 | } | |
6578 | } | |
6579 | ||
0ea05c64 AP |
6580 | if (cb.cb_type == POOL_SCAN_NONE && |
6581 | cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) { | |
6582 | (void) fprintf(stderr, gettext("invalid option combination: " | |
6583 | "-s and -p are mutually exclusive\n")); | |
6584 | usage(B_FALSE); | |
6585 | } | |
6586 | ||
34dc7c2f BB |
6587 | cb.cb_argc = argc; |
6588 | cb.cb_argv = argv; | |
6589 | argc -= optind; | |
6590 | argv += optind; | |
6591 | ||
6592 | if (argc < 1) { | |
6593 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
6594 | usage(B_FALSE); | |
6595 | } | |
6596 | ||
6597 | return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb)); | |
6598 | } | |
6599 | ||
80a91e74 TC |
6600 | /* |
6601 | * zpool resilver <pool> ... | |
6602 | * | |
6603 | * Restarts any in-progress resilver | |
6604 | */ | |
6605 | int | |
6606 | zpool_do_resilver(int argc, char **argv) | |
6607 | { | |
6608 | int c; | |
6609 | scrub_cbdata_t cb; | |
6610 | ||
6611 | cb.cb_type = POOL_SCAN_RESILVER; | |
6612 | cb.cb_scrub_cmd = POOL_SCRUB_NORMAL; | |
6613 | cb.cb_argc = argc; | |
6614 | cb.cb_argv = argv; | |
6615 | ||
6616 | /* check options */ | |
6617 | while ((c = getopt(argc, argv, "")) != -1) { | |
6618 | switch (c) { | |
6619 | case '?': | |
6620 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
6621 | optopt); | |
6622 | usage(B_FALSE); | |
6623 | } | |
6624 | } | |
6625 | ||
6626 | argc -= optind; | |
6627 | argv += optind; | |
6628 | ||
6629 | if (argc < 1) { | |
6630 | (void) fprintf(stderr, gettext("missing pool name argument\n")); | |
6631 | usage(B_FALSE); | |
6632 | } | |
6633 | ||
6634 | return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb)); | |
6635 | } | |
6636 | ||
6637 | ||
34dc7c2f BB |
6638 | /* |
6639 | * Print out detailed scrub status. | |
6640 | */ | |
a1d477c2 | 6641 | static void |
428870ff | 6642 | print_scan_status(pool_scan_stat_t *ps) |
34dc7c2f | 6643 | { |
0ea05c64 | 6644 | time_t start, end, pause; |
d4a72f23 TC |
6645 | uint64_t total_secs_left; |
6646 | uint64_t elapsed, secs_left, mins_left, hours_left, days_left; | |
6647 | uint64_t pass_scanned, scanned, pass_issued, issued, total; | |
0cf3430d | 6648 | uint64_t scan_rate, issue_rate; |
34dc7c2f | 6649 | double fraction_done; |
d4a72f23 TC |
6650 | char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7]; |
6651 | char srate_buf[7], irate_buf[7]; | |
34dc7c2f | 6652 | |
24024589 | 6653 | (void) printf(gettext(" scan: ")); |
34dc7c2f | 6654 | |
428870ff BB |
6655 | /* If there's never been a scan, there's not much to say. */ |
6656 | if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || | |
6657 | ps->pss_func >= POOL_SCAN_FUNCS) { | |
34dc7c2f BB |
6658 | (void) printf(gettext("none requested\n")); |
6659 | return; | |
6660 | } | |
6661 | ||
428870ff BB |
6662 | start = ps->pss_start_time; |
6663 | end = ps->pss_end_time; | |
0ea05c64 | 6664 | pause = ps->pss_pass_scrub_pause; |
d4a72f23 | 6665 | |
e7fbeb60 | 6666 | zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf)); |
34dc7c2f | 6667 | |
428870ff BB |
6668 | assert(ps->pss_func == POOL_SCAN_SCRUB || |
6669 | ps->pss_func == POOL_SCAN_RESILVER); | |
d4a72f23 TC |
6670 | |
6671 | /* Scan is finished or canceled. */ | |
428870ff | 6672 | if (ps->pss_state == DSS_FINISHED) { |
d4a72f23 TC |
6673 | total_secs_left = end - start; |
6674 | days_left = total_secs_left / 60 / 60 / 24; | |
6675 | hours_left = (total_secs_left / 60 / 60) % 24; | |
6676 | mins_left = (total_secs_left / 60) % 60; | |
6677 | secs_left = (total_secs_left % 60); | |
428870ff BB |
6678 | |
6679 | if (ps->pss_func == POOL_SCAN_SCRUB) { | |
d4a72f23 TC |
6680 | (void) printf(gettext("scrub repaired %s " |
6681 | "in %llu days %02llu:%02llu:%02llu " | |
6682 | "with %llu errors on %s"), processed_buf, | |
6683 | (u_longlong_t)days_left, (u_longlong_t)hours_left, | |
6684 | (u_longlong_t)mins_left, (u_longlong_t)secs_left, | |
6685 | (u_longlong_t)ps->pss_errors, ctime(&end)); | |
428870ff | 6686 | } else if (ps->pss_func == POOL_SCAN_RESILVER) { |
d4a72f23 TC |
6687 | (void) printf(gettext("resilvered %s " |
6688 | "in %llu days %02llu:%02llu:%02llu " | |
6689 | "with %llu errors on %s"), processed_buf, | |
6690 | (u_longlong_t)days_left, (u_longlong_t)hours_left, | |
6691 | (u_longlong_t)mins_left, (u_longlong_t)secs_left, | |
6692 | (u_longlong_t)ps->pss_errors, ctime(&end)); | |
428870ff | 6693 | } |
428870ff BB |
6694 | return; |
6695 | } else if (ps->pss_state == DSS_CANCELED) { | |
6696 | if (ps->pss_func == POOL_SCAN_SCRUB) { | |
6697 | (void) printf(gettext("scrub canceled on %s"), | |
6698 | ctime(&end)); | |
6699 | } else if (ps->pss_func == POOL_SCAN_RESILVER) { | |
6700 | (void) printf(gettext("resilver canceled on %s"), | |
6701 | ctime(&end)); | |
6702 | } | |
34dc7c2f BB |
6703 | return; |
6704 | } | |
6705 | ||
428870ff BB |
6706 | assert(ps->pss_state == DSS_SCANNING); |
6707 | ||
d4a72f23 | 6708 | /* Scan is in progress. Resilvers can't be paused. */ |
428870ff | 6709 | if (ps->pss_func == POOL_SCAN_SCRUB) { |
0ea05c64 AP |
6710 | if (pause == 0) { |
6711 | (void) printf(gettext("scrub in progress since %s"), | |
6712 | ctime(&start)); | |
6713 | } else { | |
d4a72f23 TC |
6714 | (void) printf(gettext("scrub paused since %s"), |
6715 | ctime(&pause)); | |
6716 | (void) printf(gettext("\tscrub started on %s"), | |
0ea05c64 AP |
6717 | ctime(&start)); |
6718 | } | |
428870ff BB |
6719 | } else if (ps->pss_func == POOL_SCAN_RESILVER) { |
6720 | (void) printf(gettext("resilver in progress since %s"), | |
6721 | ctime(&start)); | |
6722 | } | |
34dc7c2f | 6723 | |
d4a72f23 TC |
6724 | scanned = ps->pss_examined; |
6725 | pass_scanned = ps->pss_pass_exam; | |
6726 | issued = ps->pss_issued; | |
6727 | pass_issued = ps->pss_pass_issued; | |
428870ff | 6728 | total = ps->pss_to_examine; |
428870ff | 6729 | |
d4a72f23 TC |
6730 | /* we are only done with a block once we have issued the IO for it */ |
6731 | fraction_done = (double)issued / total; | |
6732 | ||
6733 | /* elapsed time for this pass, rounding up to 1 if it's 0 */ | |
428870ff | 6734 | elapsed = time(NULL) - ps->pss_pass_start; |
0ea05c64 | 6735 | elapsed -= ps->pss_pass_scrub_spent_paused; |
d4a72f23 TC |
6736 | elapsed = (elapsed != 0) ? elapsed : 1; |
6737 | ||
6738 | scan_rate = pass_scanned / elapsed; | |
6739 | issue_rate = pass_issued / elapsed; | |
6740 | total_secs_left = (issue_rate != 0) ? | |
6741 | ((total - issued) / issue_rate) : UINT64_MAX; | |
6742 | ||
6743 | days_left = total_secs_left / 60 / 60 / 24; | |
6744 | hours_left = (total_secs_left / 60 / 60) % 24; | |
6745 | mins_left = (total_secs_left / 60) % 60; | |
6746 | secs_left = (total_secs_left % 60); | |
6747 | ||
6748 | /* format all of the numbers we will be reporting */ | |
6749 | zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf)); | |
6750 | zfs_nicebytes(issued, issued_buf, sizeof (issued_buf)); | |
e7fbeb60 | 6751 | zfs_nicebytes(total, total_buf, sizeof (total_buf)); |
d4a72f23 TC |
6752 | zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf)); |
6753 | zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf)); | |
428870ff | 6754 | |
d4a72f23 | 6755 | /* do not print estimated time if we have a paused scrub */ |
0ea05c64 | 6756 | if (pause == 0) { |
d4a72f23 TC |
6757 | (void) printf(gettext("\t%s scanned at %s/s, " |
6758 | "%s issued at %s/s, %s total\n"), | |
6759 | scanned_buf, srate_buf, issued_buf, irate_buf, total_buf); | |
572e2857 | 6760 | } else { |
d4a72f23 TC |
6761 | (void) printf(gettext("\t%s scanned, %s issued, %s total\n"), |
6762 | scanned_buf, issued_buf, total_buf); | |
572e2857 | 6763 | } |
428870ff BB |
6764 | |
6765 | if (ps->pss_func == POOL_SCAN_RESILVER) { | |
d4a72f23 | 6766 | (void) printf(gettext("\t%s resilvered, %.2f%% done"), |
428870ff BB |
6767 | processed_buf, 100 * fraction_done); |
6768 | } else if (ps->pss_func == POOL_SCAN_SCRUB) { | |
d4a72f23 | 6769 | (void) printf(gettext("\t%s repaired, %.2f%% done"), |
428870ff BB |
6770 | processed_buf, 100 * fraction_done); |
6771 | } | |
d4a72f23 TC |
6772 | |
6773 | if (pause == 0) { | |
6774 | if (issue_rate >= 10 * 1024 * 1024) { | |
6775 | (void) printf(gettext(", %llu days " | |
6776 | "%02llu:%02llu:%02llu to go\n"), | |
6777 | (u_longlong_t)days_left, (u_longlong_t)hours_left, | |
6778 | (u_longlong_t)mins_left, (u_longlong_t)secs_left); | |
6779 | } else { | |
6780 | (void) printf(gettext(", no estimated " | |
6781 | "completion time\n")); | |
6782 | } | |
6783 | } else { | |
6784 | (void) printf(gettext("\n")); | |
6785 | } | |
34dc7c2f BB |
6786 | } |
6787 | ||
d2734cce SD |
6788 | /* |
6789 | * As we don't scrub checkpointed blocks, we want to warn the | |
6790 | * user that we skipped scanning some blocks if a checkpoint exists | |
6791 | * or existed at any time during the scan. | |
6792 | */ | |
6793 | static void | |
6794 | print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs) | |
6795 | { | |
6796 | if (ps == NULL || pcs == NULL) | |
6797 | return; | |
6798 | ||
6799 | if (pcs->pcs_state == CS_NONE || | |
6800 | pcs->pcs_state == CS_CHECKPOINT_DISCARDING) | |
6801 | return; | |
6802 | ||
6803 | assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS); | |
6804 | ||
6805 | if (ps->pss_state == DSS_NONE) | |
6806 | return; | |
6807 | ||
6808 | if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) && | |
6809 | ps->pss_end_time < pcs->pcs_start_time) | |
6810 | return; | |
6811 | ||
6812 | if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) { | |
6813 | (void) printf(gettext(" scan warning: skipped blocks " | |
6814 | "that are only referenced by the checkpoint.\n")); | |
6815 | } else { | |
6816 | assert(ps->pss_state == DSS_SCANNING); | |
6817 | (void) printf(gettext(" scan warning: skipping blocks " | |
6818 | "that are only referenced by the checkpoint.\n")); | |
6819 | } | |
6820 | } | |
6821 | ||
a1d477c2 MA |
6822 | /* |
6823 | * Print out detailed removal status. | |
6824 | */ | |
6825 | static void | |
6826 | print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs) | |
6827 | { | |
6828 | char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; | |
6829 | time_t start, end; | |
6830 | nvlist_t *config, *nvroot; | |
6831 | nvlist_t **child; | |
6832 | uint_t children; | |
6833 | char *vdev_name; | |
6834 | ||
6835 | if (prs == NULL || prs->prs_state == DSS_NONE) | |
6836 | return; | |
6837 | ||
6838 | /* | |
6839 | * Determine name of vdev. | |
6840 | */ | |
6841 | config = zpool_get_config(zhp, NULL); | |
6842 | nvroot = fnvlist_lookup_nvlist(config, | |
6843 | ZPOOL_CONFIG_VDEV_TREE); | |
6844 | verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
6845 | &child, &children) == 0); | |
6846 | assert(prs->prs_removing_vdev < children); | |
6847 | vdev_name = zpool_vdev_name(g_zfs, zhp, | |
6848 | child[prs->prs_removing_vdev], B_TRUE); | |
6849 | ||
6850 | (void) printf(gettext("remove: ")); | |
6851 | ||
6852 | start = prs->prs_start_time; | |
6853 | end = prs->prs_end_time; | |
6854 | zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf)); | |
6855 | ||
6856 | /* | |
6857 | * Removal is finished or canceled. | |
6858 | */ | |
6859 | if (prs->prs_state == DSS_FINISHED) { | |
6860 | uint64_t minutes_taken = (end - start) / 60; | |
6861 | ||
6862 | (void) printf(gettext("Removal of vdev %llu copied %s " | |
6863 | "in %lluh%um, completed on %s"), | |
6864 | (longlong_t)prs->prs_removing_vdev, | |
6865 | copied_buf, | |
6866 | (u_longlong_t)(minutes_taken / 60), | |
6867 | (uint_t)(minutes_taken % 60), | |
6868 | ctime((time_t *)&end)); | |
6869 | } else if (prs->prs_state == DSS_CANCELED) { | |
6870 | (void) printf(gettext("Removal of %s canceled on %s"), | |
6871 | vdev_name, ctime(&end)); | |
6872 | } else { | |
6873 | uint64_t copied, total, elapsed, mins_left, hours_left; | |
6874 | double fraction_done; | |
6875 | uint_t rate; | |
6876 | ||
6877 | assert(prs->prs_state == DSS_SCANNING); | |
6878 | ||
6879 | /* | |
6880 | * Removal is in progress. | |
6881 | */ | |
6882 | (void) printf(gettext( | |
6883 | "Evacuation of %s in progress since %s"), | |
6884 | vdev_name, ctime(&start)); | |
6885 | ||
6886 | copied = prs->prs_copied > 0 ? prs->prs_copied : 1; | |
6887 | total = prs->prs_to_copy; | |
6888 | fraction_done = (double)copied / total; | |
6889 | ||
6890 | /* elapsed time for this pass */ | |
6891 | elapsed = time(NULL) - prs->prs_start_time; | |
6892 | elapsed = elapsed > 0 ? elapsed : 1; | |
6893 | rate = copied / elapsed; | |
6894 | rate = rate > 0 ? rate : 1; | |
6895 | mins_left = ((total - copied) / rate) / 60; | |
6896 | hours_left = mins_left / 60; | |
6897 | ||
6898 | zfs_nicenum(copied, examined_buf, sizeof (examined_buf)); | |
6899 | zfs_nicenum(total, total_buf, sizeof (total_buf)); | |
6900 | zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); | |
6901 | ||
6902 | /* | |
6903 | * do not print estimated time if hours_left is more than | |
6904 | * 30 days | |
6905 | */ | |
6906 | (void) printf(gettext(" %s copied out of %s at %s/s, " | |
6907 | "%.2f%% done"), | |
6908 | examined_buf, total_buf, rate_buf, 100 * fraction_done); | |
6909 | if (hours_left < (30 * 24)) { | |
6910 | (void) printf(gettext(", %lluh%um to go\n"), | |
6911 | (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); | |
6912 | } else { | |
6913 | (void) printf(gettext( | |
6914 | ", (copy is slow, no estimated time)\n")); | |
6915 | } | |
6916 | } | |
6917 | ||
6918 | if (prs->prs_mapping_memory > 0) { | |
6919 | char mem_buf[7]; | |
6920 | zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf)); | |
6921 | (void) printf(gettext(" %s memory used for " | |
6922 | "removed device mappings\n"), | |
6923 | mem_buf); | |
6924 | } | |
6925 | } | |
6926 | ||
d2734cce SD |
6927 | static void |
6928 | print_checkpoint_status(pool_checkpoint_stat_t *pcs) | |
6929 | { | |
6930 | time_t start; | |
6931 | char space_buf[7]; | |
6932 | ||
6933 | if (pcs == NULL || pcs->pcs_state == CS_NONE) | |
6934 | return; | |
6935 | ||
6936 | (void) printf(gettext("checkpoint: ")); | |
6937 | ||
6938 | start = pcs->pcs_start_time; | |
6939 | zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf)); | |
6940 | ||
6941 | if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) { | |
6942 | char *date = ctime(&start); | |
6943 | ||
6944 | /* | |
6945 | * ctime() adds a newline at the end of the generated | |
6946 | * string, thus the weird format specifier and the | |
6947 | * strlen() call used to chop it off from the output. | |
6948 | */ | |
6949 | (void) printf(gettext("created %.*s, consumes %s\n"), | |
6950 | (int)(strlen(date) - 1), date, space_buf); | |
6951 | return; | |
6952 | } | |
6953 | ||
6954 | assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING); | |
6955 | ||
6956 | (void) printf(gettext("discarding, %s remaining.\n"), | |
6957 | space_buf); | |
6958 | } | |
6959 | ||
34dc7c2f BB |
6960 | static void |
6961 | print_error_log(zpool_handle_t *zhp) | |
6962 | { | |
6963 | nvlist_t *nverrlist = NULL; | |
6964 | nvpair_t *elem; | |
6965 | char *pathname; | |
6966 | size_t len = MAXPATHLEN * 2; | |
6967 | ||
56a6054d | 6968 | if (zpool_get_errlog(zhp, &nverrlist) != 0) |
34dc7c2f | 6969 | return; |
34dc7c2f BB |
6970 | |
6971 | (void) printf("errors: Permanent errors have been " | |
6972 | "detected in the following files:\n\n"); | |
6973 | ||
6974 | pathname = safe_malloc(len); | |
6975 | elem = NULL; | |
6976 | while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { | |
6977 | nvlist_t *nv; | |
6978 | uint64_t dsobj, obj; | |
6979 | ||
6980 | verify(nvpair_value_nvlist(elem, &nv) == 0); | |
6981 | verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, | |
6982 | &dsobj) == 0); | |
6983 | verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, | |
6984 | &obj) == 0); | |
6985 | zpool_obj_to_path(zhp, dsobj, obj, pathname, len); | |
6986 | (void) printf("%7s %s\n", "", pathname); | |
6987 | } | |
6988 | free(pathname); | |
6989 | nvlist_free(nverrlist); | |
6990 | } | |
6991 | ||
6992 | static void | |
fea33e4e HJ |
6993 | print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares, |
6994 | uint_t nspares) | |
34dc7c2f BB |
6995 | { |
6996 | uint_t i; | |
6997 | char *name; | |
6998 | ||
6999 | if (nspares == 0) | |
7000 | return; | |
7001 | ||
7002 | (void) printf(gettext("\tspares\n")); | |
7003 | ||
7004 | for (i = 0; i < nspares; i++) { | |
fea33e4e HJ |
7005 | name = zpool_vdev_name(g_zfs, zhp, spares[i], |
7006 | cb->cb_name_flags); | |
7007 | print_status_config(zhp, cb, name, spares[i], 2, B_TRUE); | |
34dc7c2f BB |
7008 | free(name); |
7009 | } | |
7010 | } | |
7011 | ||
7012 | static void | |
fea33e4e HJ |
7013 | print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache, |
7014 | uint_t nl2cache) | |
34dc7c2f BB |
7015 | { |
7016 | uint_t i; | |
7017 | char *name; | |
7018 | ||
7019 | if (nl2cache == 0) | |
7020 | return; | |
7021 | ||
7022 | (void) printf(gettext("\tcache\n")); | |
7023 | ||
7024 | for (i = 0; i < nl2cache; i++) { | |
fea33e4e HJ |
7025 | name = zpool_vdev_name(g_zfs, zhp, l2cache[i], |
7026 | cb->cb_name_flags); | |
7027 | print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE); | |
34dc7c2f BB |
7028 | free(name); |
7029 | } | |
7030 | } | |
7031 | ||
428870ff BB |
7032 | static void |
7033 | print_dedup_stats(nvlist_t *config) | |
7034 | { | |
7035 | ddt_histogram_t *ddh; | |
7036 | ddt_stat_t *dds; | |
7037 | ddt_object_t *ddo; | |
7038 | uint_t c; | |
e7fbeb60 | 7039 | char dspace[6], mspace[6]; |
428870ff BB |
7040 | |
7041 | /* | |
7042 | * If the pool was faulted then we may not have been able to | |
32a9872b | 7043 | * obtain the config. Otherwise, if we have anything in the dedup |
428870ff BB |
7044 | * table continue processing the stats. |
7045 | */ | |
7046 | if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, | |
24024589 | 7047 | (uint64_t **)&ddo, &c) != 0) |
428870ff BB |
7048 | return; |
7049 | ||
7050 | (void) printf("\n"); | |
24024589 YP |
7051 | (void) printf(gettext(" dedup: ")); |
7052 | if (ddo->ddo_count == 0) { | |
7053 | (void) printf(gettext("no DDT entries\n")); | |
7054 | return; | |
7055 | } | |
7056 | ||
e7fbeb60 | 7057 | zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace)); |
7058 | zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace)); | |
7059 | (void) printf("DDT entries %llu, size %s on disk, %s in core\n", | |
428870ff | 7060 | (u_longlong_t)ddo->ddo_count, |
e7fbeb60 | 7061 | dspace, |
7062 | mspace); | |
428870ff BB |
7063 | |
7064 | verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, | |
7065 | (uint64_t **)&dds, &c) == 0); | |
7066 | verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, | |
7067 | (uint64_t **)&ddh, &c) == 0); | |
7068 | zpool_dump_ddt(dds, ddh); | |
7069 | } | |
7070 | ||
34dc7c2f BB |
7071 | /* |
7072 | * Display a summary of pool status. Displays a summary such as: | |
7073 | * | |
7074 | * pool: tank | |
7075 | * status: DEGRADED | |
7076 | * reason: One or more devices ... | |
3cee2262 | 7077 | * see: http://zfsonlinux.org/msg/ZFS-xxxx-01 |
34dc7c2f BB |
7078 | * config: |
7079 | * mirror DEGRADED | |
7080 | * c1t0d0 OK | |
7081 | * c2t0d0 UNAVAIL | |
7082 | * | |
7083 | * When given the '-v' option, we print out the complete config. If the '-e' | |
7084 | * option is specified, then we print out error rate information as well. | |
7085 | */ | |
7086 | int | |
7087 | status_callback(zpool_handle_t *zhp, void *data) | |
7088 | { | |
7089 | status_cbdata_t *cbp = data; | |
7090 | nvlist_t *config, *nvroot; | |
7091 | char *msgid; | |
731782ec | 7092 | zpool_status_t reason; |
ffe9d382 | 7093 | zpool_errata_t errata; |
34dc7c2f BB |
7094 | const char *health; |
7095 | uint_t c; | |
7096 | vdev_stat_t *vs; | |
7097 | ||
7098 | config = zpool_get_config(zhp, NULL); | |
ffe9d382 | 7099 | reason = zpool_get_status(zhp, &msgid, &errata); |
34dc7c2f BB |
7100 | |
7101 | cbp->cb_count++; | |
7102 | ||
7103 | /* | |
7104 | * If we were given 'zpool status -x', only report those pools with | |
7105 | * problems. | |
7106 | */ | |
c5b247f3 TC |
7107 | if (cbp->cb_explain && |
7108 | (reason == ZPOOL_STATUS_OK || | |
7109 | reason == ZPOOL_STATUS_VERSION_OLDER || | |
7110 | reason == ZPOOL_STATUS_FEAT_DISABLED)) { | |
34dc7c2f BB |
7111 | if (!cbp->cb_allpools) { |
7112 | (void) printf(gettext("pool '%s' is healthy\n"), | |
7113 | zpool_get_name(zhp)); | |
7114 | if (cbp->cb_first) | |
7115 | cbp->cb_first = B_FALSE; | |
7116 | } | |
7117 | return (0); | |
7118 | } | |
7119 | ||
7120 | if (cbp->cb_first) | |
7121 | cbp->cb_first = B_FALSE; | |
7122 | else | |
7123 | (void) printf("\n"); | |
7124 | ||
a1d477c2 | 7125 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
428870ff | 7126 | verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f | 7127 | (uint64_t **)&vs, &c) == 0); |
f0ed6c74 TH |
7128 | |
7129 | health = zpool_get_state_str(zhp); | |
34dc7c2f BB |
7130 | |
7131 | (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp)); | |
7132 | (void) printf(gettext(" state: %s\n"), health); | |
7133 | ||
7134 | switch (reason) { | |
7135 | case ZPOOL_STATUS_MISSING_DEV_R: | |
7136 | (void) printf(gettext("status: One or more devices could not " | |
7137 | "be opened. Sufficient replicas exist for\n\tthe pool to " | |
7138 | "continue functioning in a degraded state.\n")); | |
7139 | (void) printf(gettext("action: Attach the missing device and " | |
7140 | "online it using 'zpool online'.\n")); | |
7141 | break; | |
7142 | ||
7143 | case ZPOOL_STATUS_MISSING_DEV_NR: | |
7144 | (void) printf(gettext("status: One or more devices could not " | |
7145 | "be opened. There are insufficient\n\treplicas for the " | |
7146 | "pool to continue functioning.\n")); | |
7147 | (void) printf(gettext("action: Attach the missing device and " | |
7148 | "online it using 'zpool online'.\n")); | |
7149 | break; | |
7150 | ||
7151 | case ZPOOL_STATUS_CORRUPT_LABEL_R: | |
7152 | (void) printf(gettext("status: One or more devices could not " | |
7153 | "be used because the label is missing or\n\tinvalid. " | |
7154 | "Sufficient replicas exist for the pool to continue\n\t" | |
7155 | "functioning in a degraded state.\n")); | |
7156 | (void) printf(gettext("action: Replace the device using " | |
7157 | "'zpool replace'.\n")); | |
7158 | break; | |
7159 | ||
7160 | case ZPOOL_STATUS_CORRUPT_LABEL_NR: | |
7161 | (void) printf(gettext("status: One or more devices could not " | |
7162 | "be used because the label is missing \n\tor invalid. " | |
7163 | "There are insufficient replicas for the pool to " | |
7164 | "continue\n\tfunctioning.\n")); | |
428870ff BB |
7165 | zpool_explain_recover(zpool_get_handle(zhp), |
7166 | zpool_get_name(zhp), reason, config); | |
34dc7c2f BB |
7167 | break; |
7168 | ||
7169 | case ZPOOL_STATUS_FAILING_DEV: | |
7170 | (void) printf(gettext("status: One or more devices has " | |
7171 | "experienced an unrecoverable error. An\n\tattempt was " | |
7172 | "made to correct the error. Applications are " | |
7173 | "unaffected.\n")); | |
7174 | (void) printf(gettext("action: Determine if the device needs " | |
7175 | "to be replaced, and clear the errors\n\tusing " | |
7176 | "'zpool clear' or replace the device with 'zpool " | |
7177 | "replace'.\n")); | |
7178 | break; | |
7179 | ||
7180 | case ZPOOL_STATUS_OFFLINE_DEV: | |
7181 | (void) printf(gettext("status: One or more devices has " | |
7182 | "been taken offline by the administrator.\n\tSufficient " | |
7183 | "replicas exist for the pool to continue functioning in " | |
7184 | "a\n\tdegraded state.\n")); | |
7185 | (void) printf(gettext("action: Online the device using " | |
7186 | "'zpool online' or replace the device with\n\t'zpool " | |
7187 | "replace'.\n")); | |
7188 | break; | |
7189 | ||
45d1cae3 BB |
7190 | case ZPOOL_STATUS_REMOVED_DEV: |
7191 | (void) printf(gettext("status: One or more devices has " | |
7192 | "been removed by the administrator.\n\tSufficient " | |
7193 | "replicas exist for the pool to continue functioning in " | |
7194 | "a\n\tdegraded state.\n")); | |
7195 | (void) printf(gettext("action: Online the device using " | |
7196 | "'zpool online' or replace the device with\n\t'zpool " | |
7197 | "replace'.\n")); | |
7198 | break; | |
7199 | ||
34dc7c2f BB |
7200 | case ZPOOL_STATUS_RESILVERING: |
7201 | (void) printf(gettext("status: One or more devices is " | |
7202 | "currently being resilvered. The pool will\n\tcontinue " | |
7203 | "to function, possibly in a degraded state.\n")); | |
7204 | (void) printf(gettext("action: Wait for the resilver to " | |
7205 | "complete.\n")); | |
7206 | break; | |
7207 | ||
7208 | case ZPOOL_STATUS_CORRUPT_DATA: | |
7209 | (void) printf(gettext("status: One or more devices has " | |
7210 | "experienced an error resulting in data\n\tcorruption. " | |
7211 | "Applications may be affected.\n")); | |
7212 | (void) printf(gettext("action: Restore the file in question " | |
7213 | "if possible. Otherwise restore the\n\tentire pool from " | |
7214 | "backup.\n")); | |
7215 | break; | |
7216 | ||
7217 | case ZPOOL_STATUS_CORRUPT_POOL: | |
7218 | (void) printf(gettext("status: The pool metadata is corrupted " | |
7219 | "and the pool cannot be opened.\n")); | |
428870ff BB |
7220 | zpool_explain_recover(zpool_get_handle(zhp), |
7221 | zpool_get_name(zhp), reason, config); | |
34dc7c2f BB |
7222 | break; |
7223 | ||
7224 | case ZPOOL_STATUS_VERSION_OLDER: | |
b9b24bb4 CS |
7225 | (void) printf(gettext("status: The pool is formatted using a " |
7226 | "legacy on-disk format. The pool can\n\tstill be used, " | |
7227 | "but some features are unavailable.\n")); | |
34dc7c2f BB |
7228 | (void) printf(gettext("action: Upgrade the pool using 'zpool " |
7229 | "upgrade'. Once this is done, the\n\tpool will no longer " | |
f52b31ea BB |
7230 | "be accessible on software that does not support\n\t" |
7231 | "feature flags.\n")); | |
34dc7c2f BB |
7232 | break; |
7233 | ||
7234 | case ZPOOL_STATUS_VERSION_NEWER: | |
7235 | (void) printf(gettext("status: The pool has been upgraded to a " | |
7236 | "newer, incompatible on-disk version.\n\tThe pool cannot " | |
7237 | "be accessed on this system.\n")); | |
7238 | (void) printf(gettext("action: Access the pool from a system " | |
7239 | "running more recent software, or\n\trestore the pool from " | |
7240 | "backup.\n")); | |
7241 | break; | |
7242 | ||
b9b24bb4 CS |
7243 | case ZPOOL_STATUS_FEAT_DISABLED: |
7244 | (void) printf(gettext("status: Some supported features are not " | |
7245 | "enabled on the pool. The pool can\n\tstill be used, but " | |
7246 | "some features are unavailable.\n")); | |
7247 | (void) printf(gettext("action: Enable all features using " | |
7248 | "'zpool upgrade'. Once this is done,\n\tthe pool may no " | |
7249 | "longer be accessible by software that does not support\n\t" | |
7250 | "the features. See zpool-features(5) for details.\n")); | |
7251 | break; | |
7252 | ||
9ae529ec CS |
7253 | case ZPOOL_STATUS_UNSUP_FEAT_READ: |
7254 | (void) printf(gettext("status: The pool cannot be accessed on " | |
7255 | "this system because it uses the\n\tfollowing feature(s) " | |
7256 | "not supported on this system:\n")); | |
7257 | zpool_print_unsup_feat(config); | |
7258 | (void) printf("\n"); | |
7259 | (void) printf(gettext("action: Access the pool from a system " | |
7260 | "that supports the required feature(s),\n\tor restore the " | |
7261 | "pool from backup.\n")); | |
7262 | break; | |
7263 | ||
7264 | case ZPOOL_STATUS_UNSUP_FEAT_WRITE: | |
7265 | (void) printf(gettext("status: The pool can only be accessed " | |
7266 | "in read-only mode on this system. It\n\tcannot be " | |
7267 | "accessed in read-write mode because it uses the " | |
7268 | "following\n\tfeature(s) not supported on this system:\n")); | |
7269 | zpool_print_unsup_feat(config); | |
7270 | (void) printf("\n"); | |
7271 | (void) printf(gettext("action: The pool cannot be accessed in " | |
7272 | "read-write mode. Import the pool with\n" | |
7273 | "\t\"-o readonly=on\", access the pool from a system that " | |
7274 | "supports the\n\trequired feature(s), or restore the " | |
7275 | "pool from backup.\n")); | |
7276 | break; | |
7277 | ||
34dc7c2f BB |
7278 | case ZPOOL_STATUS_FAULTED_DEV_R: |
7279 | (void) printf(gettext("status: One or more devices are " | |
7280 | "faulted in response to persistent errors.\n\tSufficient " | |
7281 | "replicas exist for the pool to continue functioning " | |
7282 | "in a\n\tdegraded state.\n")); | |
7283 | (void) printf(gettext("action: Replace the faulted device, " | |
7284 | "or use 'zpool clear' to mark the device\n\trepaired.\n")); | |
7285 | break; | |
7286 | ||
7287 | case ZPOOL_STATUS_FAULTED_DEV_NR: | |
7288 | (void) printf(gettext("status: One or more devices are " | |
7289 | "faulted in response to persistent errors. There are " | |
7290 | "insufficient replicas for the pool to\n\tcontinue " | |
7291 | "functioning.\n")); | |
7292 | (void) printf(gettext("action: Destroy and re-create the pool " | |
7293 | "from a backup source. Manually marking the device\n" | |
7294 | "\trepaired using 'zpool clear' may allow some data " | |
7295 | "to be recovered.\n")); | |
7296 | break; | |
7297 | ||
cec3a0a1 OF |
7298 | case ZPOOL_STATUS_IO_FAILURE_MMP: |
7299 | (void) printf(gettext("status: The pool is suspended because " | |
7300 | "multihost writes failed or were delayed;\n\tanother " | |
7301 | "system could import the pool undetected.\n")); | |
7302 | (void) printf(gettext("action: Make sure the pool's devices " | |
7303 | "are connected, then reboot your system and\n\timport the " | |
7304 | "pool.\n")); | |
7305 | break; | |
7306 | ||
b128c09f BB |
7307 | case ZPOOL_STATUS_IO_FAILURE_WAIT: |
7308 | case ZPOOL_STATUS_IO_FAILURE_CONTINUE: | |
7309 | (void) printf(gettext("status: One or more devices are " | |
7310 | "faulted in response to IO failures.\n")); | |
7311 | (void) printf(gettext("action: Make sure the affected devices " | |
7312 | "are connected, then run 'zpool clear'.\n")); | |
7313 | break; | |
7314 | ||
7315 | case ZPOOL_STATUS_BAD_LOG: | |
7316 | (void) printf(gettext("status: An intent log record " | |
7317 | "could not be read.\n" | |
2627e752 | 7318 | "\tWaiting for administrator intervention to fix the " |
b128c09f BB |
7319 | "faulted pool.\n")); |
7320 | (void) printf(gettext("action: Either restore the affected " | |
7321 | "device(s) and run 'zpool online',\n" | |
7322 | "\tor ignore the intent log records by running " | |
7323 | "'zpool clear'.\n")); | |
7324 | break; | |
7325 | ||
1cbae971 RY |
7326 | case ZPOOL_STATUS_HOSTID_MISMATCH: |
7327 | (void) printf(gettext("status: Mismatch between pool hostid " | |
7328 | "and system hostid on imported pool.\n\tThis pool was " | |
7329 | "previously imported into a system with a different " | |
7330 | "hostid,\n\tand then was verbatim imported into this " | |
7331 | "system.\n")); | |
7332 | (void) printf(gettext("action: Export this pool on all systems " | |
7333 | "on which it is imported.\n" | |
7334 | "\tThen import it to correct the mismatch.\n")); | |
7335 | break; | |
7336 | ||
ffe9d382 BB |
7337 | case ZPOOL_STATUS_ERRATA: |
7338 | (void) printf(gettext("status: Errata #%d detected.\n"), | |
7339 | errata); | |
7340 | ||
7341 | switch (errata) { | |
7342 | case ZPOOL_ERRATA_NONE: | |
7343 | break; | |
7344 | ||
4f2dcb3e RY |
7345 | case ZPOOL_ERRATA_ZOL_2094_SCRUB: |
7346 | (void) printf(gettext("action: To correct the issue " | |
7347 | "run 'zpool scrub'.\n")); | |
7348 | break; | |
7349 | ||
ae76f45c TC |
7350 | case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION: |
7351 | (void) printf(gettext("\tExisting encrypted datasets " | |
7352 | "contain an on-disk incompatibility\n\twhich " | |
7353 | "needs to be corrected.\n")); | |
7354 | (void) printf(gettext("action: To correct the issue " | |
7355 | "backup existing encrypted datasets to new\n\t" | |
7356 | "encrypted datasets and destroy the old ones. " | |
7357 | "'zfs mount -o ro' can\n\tbe used to temporarily " | |
7358 | "mount existing encrypted datasets readonly.\n")); | |
7359 | break; | |
7360 | ||
ffe9d382 BB |
7361 | default: |
7362 | /* | |
7363 | * All errata which allow the pool to be imported | |
7364 | * must contain an action message. | |
7365 | */ | |
7366 | assert(0); | |
7367 | } | |
7368 | break; | |
7369 | ||
34dc7c2f BB |
7370 | default: |
7371 | /* | |
7372 | * The remaining errors can't actually be generated, yet. | |
7373 | */ | |
7374 | assert(reason == ZPOOL_STATUS_OK); | |
7375 | } | |
7376 | ||
7377 | if (msgid != NULL) | |
3cee2262 | 7378 | (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"), |
34dc7c2f BB |
7379 | msgid); |
7380 | ||
7381 | if (config != NULL) { | |
34dc7c2f BB |
7382 | uint64_t nerr; |
7383 | nvlist_t **spares, **l2cache; | |
7384 | uint_t nspares, nl2cache; | |
d2734cce | 7385 | pool_checkpoint_stat_t *pcs = NULL; |
428870ff | 7386 | pool_scan_stat_t *ps = NULL; |
a1d477c2 | 7387 | pool_removal_stat_t *prs = NULL; |
34dc7c2f | 7388 | |
d2734cce SD |
7389 | (void) nvlist_lookup_uint64_array(nvroot, |
7390 | ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c); | |
428870ff BB |
7391 | (void) nvlist_lookup_uint64_array(nvroot, |
7392 | ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c); | |
a1d477c2 MA |
7393 | (void) nvlist_lookup_uint64_array(nvroot, |
7394 | ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c); | |
d2734cce SD |
7395 | |
7396 | print_scan_status(ps); | |
7397 | print_checkpoint_scan_warning(ps, pcs); | |
a1d477c2 | 7398 | print_removal_status(zhp, prs); |
d2734cce | 7399 | print_checkpoint_status(pcs); |
a1d477c2 | 7400 | |
fea33e4e | 7401 | cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0, |
5f20c145 | 7402 | cbp->cb_name_flags | VDEV_NAME_TYPE_ID); |
fea33e4e HJ |
7403 | if (cbp->cb_namewidth < 10) |
7404 | cbp->cb_namewidth = 10; | |
34dc7c2f BB |
7405 | |
7406 | (void) printf(gettext("config:\n\n")); | |
d6418de0 | 7407 | (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"), |
fea33e4e HJ |
7408 | cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE", |
7409 | "CKSUM"); | |
d6418de0 | 7410 | |
ad796b8a TH |
7411 | if (cbp->cb_print_slow_ios) |
7412 | (void) printf(" %5s", gettext("SLOW")); | |
7413 | ||
d6418de0 TH |
7414 | if (cbp->vcdl != NULL) |
7415 | print_cmd_columns(cbp->vcdl, 0); | |
7416 | ||
7417 | printf("\n"); | |
cc99f275 | 7418 | |
fea33e4e HJ |
7419 | print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0, |
7420 | B_FALSE); | |
34dc7c2f | 7421 | |
cc99f275 DB |
7422 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP); |
7423 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL); | |
7424 | print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS); | |
7425 | ||
34dc7c2f BB |
7426 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
7427 | &l2cache, &nl2cache) == 0) | |
fea33e4e | 7428 | print_l2cache(zhp, cbp, l2cache, nl2cache); |
34dc7c2f BB |
7429 | |
7430 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
7431 | &spares, &nspares) == 0) | |
fea33e4e | 7432 | print_spares(zhp, cbp, spares, nspares); |
34dc7c2f BB |
7433 | |
7434 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, | |
7435 | &nerr) == 0) { | |
7436 | nvlist_t *nverrlist = NULL; | |
7437 | ||
7438 | /* | |
7439 | * If the approximate error count is small, get a | |
7440 | * precise count by fetching the entire log and | |
7441 | * uniquifying the results. | |
7442 | */ | |
7443 | if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && | |
7444 | zpool_get_errlog(zhp, &nverrlist) == 0) { | |
7445 | nvpair_t *elem; | |
7446 | ||
7447 | elem = NULL; | |
7448 | nerr = 0; | |
7449 | while ((elem = nvlist_next_nvpair(nverrlist, | |
7450 | elem)) != NULL) { | |
7451 | nerr++; | |
7452 | } | |
7453 | } | |
7454 | nvlist_free(nverrlist); | |
7455 | ||
7456 | (void) printf("\n"); | |
7457 | ||
7458 | if (nerr == 0) | |
7459 | (void) printf(gettext("errors: No known data " | |
7460 | "errors\n")); | |
7461 | else if (!cbp->cb_verbose) | |
7462 | (void) printf(gettext("errors: %llu data " | |
7463 | "errors, use '-v' for a list\n"), | |
7464 | (u_longlong_t)nerr); | |
7465 | else | |
7466 | print_error_log(zhp); | |
7467 | } | |
428870ff BB |
7468 | |
7469 | if (cbp->cb_dedup_stats) | |
7470 | print_dedup_stats(config); | |
34dc7c2f BB |
7471 | } else { |
7472 | (void) printf(gettext("config: The configuration cannot be " | |
7473 | "determined.\n")); | |
7474 | } | |
7475 | ||
7476 | return (0); | |
7477 | } | |
7478 | ||
7479 | /* | |
ad796b8a | 7480 | * zpool status [-c [script1,script2,...]] [-gLpPsvx] [-T d|u] [pool] ... |
d6418de0 | 7481 | * [interval [count]] |
34dc7c2f | 7482 | * |
8720e9e7 | 7483 | * -c CMD For each vdev, run command CMD |
d2f3e292 RY |
7484 | * -g Display guid for individual vdev name. |
7485 | * -L Follow links when resolving vdev path name. | |
ad796b8a | 7486 | * -p Display values in parsable (exact) format. |
a77f29f9 | 7487 | * -P Display full path for vdev name. |
ad796b8a | 7488 | * -s Display slow IOs column. |
34dc7c2f BB |
7489 | * -v Display complete error logs |
7490 | * -x Display only pools with potential problems | |
428870ff BB |
7491 | * -D Display dedup status (undocumented) |
7492 | * -T Display a timestamp in date(1) or Unix format | |
34dc7c2f BB |
7493 | * |
7494 | * Describes the health status of all pools or some subset. | |
7495 | */ | |
7496 | int | |
7497 | zpool_do_status(int argc, char **argv) | |
7498 | { | |
7499 | int c; | |
7500 | int ret; | |
193a37cb TH |
7501 | float interval = 0; |
7502 | unsigned long count = 0; | |
34dc7c2f | 7503 | status_cbdata_t cb = { 0 }; |
8720e9e7 | 7504 | char *cmd = NULL; |
34dc7c2f BB |
7505 | |
7506 | /* check options */ | |
ad796b8a | 7507 | while ((c = getopt(argc, argv, "c:gLpPsvxDT:")) != -1) { |
34dc7c2f | 7508 | switch (c) { |
8720e9e7 | 7509 | case 'c': |
d6418de0 TH |
7510 | if (cmd != NULL) { |
7511 | fprintf(stderr, | |
7512 | gettext("Can't set -c flag twice\n")); | |
7513 | exit(1); | |
7514 | } | |
099700d9 GDN |
7515 | |
7516 | if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL && | |
7517 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) { | |
7518 | fprintf(stderr, gettext( | |
7519 | "Can't run -c, disabled by " | |
7520 | "ZPOOL_SCRIPTS_ENABLED.\n")); | |
7521 | exit(1); | |
7522 | } | |
7523 | ||
d6418de0 TH |
7524 | if ((getuid() <= 0 || geteuid() <= 0) && |
7525 | !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) { | |
7526 | fprintf(stderr, gettext( | |
7527 | "Can't run -c with root privileges " | |
7528 | "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n")); | |
7529 | exit(1); | |
7530 | } | |
8720e9e7 TH |
7531 | cmd = optarg; |
7532 | break; | |
d2f3e292 RY |
7533 | case 'g': |
7534 | cb.cb_name_flags |= VDEV_NAME_GUID; | |
7535 | break; | |
7536 | case 'L': | |
7537 | cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; | |
7538 | break; | |
ad796b8a TH |
7539 | case 'p': |
7540 | cb.cb_literal = B_TRUE; | |
7541 | break; | |
a77f29f9 | 7542 | case 'P': |
d2f3e292 RY |
7543 | cb.cb_name_flags |= VDEV_NAME_PATH; |
7544 | break; | |
ad796b8a TH |
7545 | case 's': |
7546 | cb.cb_print_slow_ios = B_TRUE; | |
7547 | break; | |
34dc7c2f BB |
7548 | case 'v': |
7549 | cb.cb_verbose = B_TRUE; | |
7550 | break; | |
7551 | case 'x': | |
7552 | cb.cb_explain = B_TRUE; | |
7553 | break; | |
428870ff BB |
7554 | case 'D': |
7555 | cb.cb_dedup_stats = B_TRUE; | |
7556 | break; | |
7557 | case 'T': | |
7558 | get_timestamp_arg(*optarg); | |
7559 | break; | |
34dc7c2f | 7560 | case '?': |
1528bfdb | 7561 | if (optopt == 'c') { |
099700d9 | 7562 | print_zpool_script_list("status"); |
d6418de0 | 7563 | exit(0); |
1528bfdb TH |
7564 | } else { |
7565 | fprintf(stderr, | |
7566 | gettext("invalid option '%c'\n"), optopt); | |
7567 | } | |
34dc7c2f BB |
7568 | usage(B_FALSE); |
7569 | } | |
7570 | } | |
7571 | ||
7572 | argc -= optind; | |
7573 | argv += optind; | |
7574 | ||
428870ff | 7575 | get_interval_count(&argc, argv, &interval, &count); |
34dc7c2f BB |
7576 | |
7577 | if (argc == 0) | |
7578 | cb.cb_allpools = B_TRUE; | |
7579 | ||
428870ff | 7580 | cb.cb_first = B_TRUE; |
fea33e4e | 7581 | cb.cb_print_status = B_TRUE; |
34dc7c2f | 7582 | |
428870ff BB |
7583 | for (;;) { |
7584 | if (timestamp_fmt != NODATE) | |
7585 | print_timestamp(timestamp_fmt); | |
34dc7c2f | 7586 | |
8720e9e7 | 7587 | if (cmd != NULL) |
1528bfdb TH |
7588 | cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd, |
7589 | NULL, NULL, 0, 0); | |
8720e9e7 | 7590 | |
428870ff BB |
7591 | ret = for_each_pool(argc, argv, B_TRUE, NULL, |
7592 | status_callback, &cb); | |
7593 | ||
8720e9e7 TH |
7594 | if (cb.vcdl != NULL) |
7595 | free_vdev_cmd_data_list(cb.vcdl); | |
7596 | ||
428870ff | 7597 | if (argc == 0 && cb.cb_count == 0) |
42cb3819 | 7598 | (void) fprintf(stderr, gettext("no pools available\n")); |
428870ff BB |
7599 | else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) |
7600 | (void) printf(gettext("all pools are healthy\n")); | |
7601 | ||
7602 | if (ret != 0) | |
7603 | return (ret); | |
7604 | ||
7605 | if (interval == 0) | |
7606 | break; | |
7607 | ||
7608 | if (count != 0 && --count == 0) | |
7609 | break; | |
7610 | ||
193a37cb | 7611 | (void) fsleep(interval); |
428870ff BB |
7612 | } |
7613 | ||
7614 | return (0); | |
34dc7c2f BB |
7615 | } |
7616 | ||
7617 | typedef struct upgrade_cbdata { | |
34dc7c2f | 7618 | int cb_first; |
34dc7c2f BB |
7619 | int cb_argc; |
7620 | uint64_t cb_version; | |
7621 | char **cb_argv; | |
7622 | } upgrade_cbdata_t; | |
7623 | ||
287be44f DS |
7624 | static int |
7625 | check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs) | |
7626 | { | |
02730c33 | 7627 | int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION); |
287be44f DS |
7628 | int *count = (int *)unsupp_fs; |
7629 | ||
7630 | if (zfs_version > ZPL_VERSION) { | |
7631 | (void) printf(gettext("%s (v%d) is not supported by this " | |
7632 | "implementation of ZFS.\n"), | |
7633 | zfs_get_name(zhp), zfs_version); | |
7634 | (*count)++; | |
7635 | } | |
7636 | ||
7637 | zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs); | |
7638 | ||
7639 | zfs_close(zhp); | |
7640 | ||
7641 | return (0); | |
7642 | } | |
7643 | ||
b9b24bb4 CS |
7644 | static int |
7645 | upgrade_version(zpool_handle_t *zhp, uint64_t version) | |
7646 | { | |
7647 | int ret; | |
7648 | nvlist_t *config; | |
7649 | uint64_t oldversion; | |
287be44f | 7650 | int unsupp_fs = 0; |
b9b24bb4 CS |
7651 | |
7652 | config = zpool_get_config(zhp, NULL); | |
7653 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
7654 | &oldversion) == 0); | |
7655 | ||
7656 | assert(SPA_VERSION_IS_SUPPORTED(oldversion)); | |
7657 | assert(oldversion < version); | |
7658 | ||
287be44f DS |
7659 | ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs); |
7660 | if (ret != 0) | |
7661 | return (ret); | |
7662 | ||
7663 | if (unsupp_fs) { | |
5c7afad4 BB |
7664 | (void) fprintf(stderr, gettext("Upgrade not performed due " |
7665 | "to %d unsupported filesystems (max v%d).\n"), | |
02730c33 | 7666 | unsupp_fs, (int)ZPL_VERSION); |
287be44f DS |
7667 | return (1); |
7668 | } | |
7669 | ||
b9b24bb4 CS |
7670 | ret = zpool_upgrade(zhp, version); |
7671 | if (ret != 0) | |
7672 | return (ret); | |
7673 | ||
7674 | if (version >= SPA_VERSION_FEATURES) { | |
7675 | (void) printf(gettext("Successfully upgraded " | |
7676 | "'%s' from version %llu to feature flags.\n"), | |
02730c33 | 7677 | zpool_get_name(zhp), (u_longlong_t)oldversion); |
b9b24bb4 CS |
7678 | } else { |
7679 | (void) printf(gettext("Successfully upgraded " | |
7680 | "'%s' from version %llu to version %llu.\n"), | |
02730c33 BB |
7681 | zpool_get_name(zhp), (u_longlong_t)oldversion, |
7682 | (u_longlong_t)version); | |
b9b24bb4 CS |
7683 | } |
7684 | ||
7685 | return (0); | |
7686 | } | |
7687 | ||
7688 | static int | |
7689 | upgrade_enable_all(zpool_handle_t *zhp, int *countp) | |
7690 | { | |
7691 | int i, ret, count; | |
7692 | boolean_t firstff = B_TRUE; | |
7693 | nvlist_t *enabled = zpool_get_features(zhp); | |
7694 | ||
7695 | count = 0; | |
7696 | for (i = 0; i < SPA_FEATURES; i++) { | |
7697 | const char *fname = spa_feature_table[i].fi_uname; | |
7698 | const char *fguid = spa_feature_table[i].fi_guid; | |
7699 | if (!nvlist_exists(enabled, fguid)) { | |
7700 | char *propname; | |
7701 | verify(-1 != asprintf(&propname, "feature@%s", fname)); | |
7702 | ret = zpool_set_prop(zhp, propname, | |
7703 | ZFS_FEATURE_ENABLED); | |
7704 | if (ret != 0) { | |
7705 | free(propname); | |
7706 | return (ret); | |
7707 | } | |
7708 | count++; | |
7709 | ||
7710 | if (firstff) { | |
7711 | (void) printf(gettext("Enabled the " | |
7712 | "following features on '%s':\n"), | |
7713 | zpool_get_name(zhp)); | |
7714 | firstff = B_FALSE; | |
7715 | } | |
7716 | (void) printf(gettext(" %s\n"), fname); | |
7717 | free(propname); | |
7718 | } | |
7719 | } | |
7720 | ||
7721 | if (countp != NULL) | |
7722 | *countp = count; | |
7723 | return (0); | |
7724 | } | |
7725 | ||
34dc7c2f BB |
7726 | static int |
7727 | upgrade_cb(zpool_handle_t *zhp, void *arg) | |
7728 | { | |
7729 | upgrade_cbdata_t *cbp = arg; | |
7730 | nvlist_t *config; | |
7731 | uint64_t version; | |
b9b24bb4 CS |
7732 | boolean_t printnl = B_FALSE; |
7733 | int ret; | |
34dc7c2f BB |
7734 | |
7735 | config = zpool_get_config(zhp, NULL); | |
7736 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
7737 | &version) == 0); | |
7738 | ||
b9b24bb4 | 7739 | assert(SPA_VERSION_IS_SUPPORTED(version)); |
34dc7c2f | 7740 | |
b9b24bb4 CS |
7741 | if (version < cbp->cb_version) { |
7742 | cbp->cb_first = B_FALSE; | |
7743 | ret = upgrade_version(zhp, cbp->cb_version); | |
7744 | if (ret != 0) | |
7745 | return (ret); | |
7746 | printnl = B_TRUE; | |
7747 | ||
b9b24bb4 CS |
7748 | /* |
7749 | * If they did "zpool upgrade -a", then we could | |
7750 | * be doing ioctls to different pools. We need | |
7751 | * to log this history once to each pool, and bypass | |
7752 | * the normal history logging that happens in main(). | |
7753 | */ | |
7754 | (void) zpool_log_history(g_zfs, history_str); | |
7755 | log_history = B_FALSE; | |
b9b24bb4 CS |
7756 | } |
7757 | ||
7758 | if (cbp->cb_version >= SPA_VERSION_FEATURES) { | |
7759 | int count; | |
7760 | ret = upgrade_enable_all(zhp, &count); | |
7761 | if (ret != 0) | |
7762 | return (ret); | |
7763 | ||
7764 | if (count > 0) { | |
34dc7c2f | 7765 | cbp->cb_first = B_FALSE; |
b9b24bb4 | 7766 | printnl = B_TRUE; |
34dc7c2f | 7767 | } |
b9b24bb4 | 7768 | } |
34dc7c2f | 7769 | |
b9b24bb4 CS |
7770 | if (printnl) { |
7771 | (void) printf(gettext("\n")); | |
7772 | } | |
7773 | ||
7774 | return (0); | |
7775 | } | |
7776 | ||
7777 | static int | |
7778 | upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) | |
7779 | { | |
7780 | upgrade_cbdata_t *cbp = arg; | |
7781 | nvlist_t *config; | |
7782 | uint64_t version; | |
7783 | ||
7784 | config = zpool_get_config(zhp, NULL); | |
7785 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
7786 | &version) == 0); | |
7787 | ||
7788 | assert(SPA_VERSION_IS_SUPPORTED(version)); | |
7789 | ||
7790 | if (version < SPA_VERSION_FEATURES) { | |
34dc7c2f BB |
7791 | if (cbp->cb_first) { |
7792 | (void) printf(gettext("The following pools are " | |
b9b24bb4 CS |
7793 | "formatted with legacy version numbers and can\n" |
7794 | "be upgraded to use feature flags. After " | |
7795 | "being upgraded, these pools\nwill no " | |
7796 | "longer be accessible by software that does not " | |
7797 | "support feature\nflags.\n\n")); | |
34dc7c2f BB |
7798 | (void) printf(gettext("VER POOL\n")); |
7799 | (void) printf(gettext("--- ------------\n")); | |
7800 | cbp->cb_first = B_FALSE; | |
7801 | } | |
7802 | ||
7803 | (void) printf("%2llu %s\n", (u_longlong_t)version, | |
7804 | zpool_get_name(zhp)); | |
7805 | } | |
7806 | ||
b9b24bb4 CS |
7807 | return (0); |
7808 | } | |
7809 | ||
7810 | static int | |
7811 | upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) | |
7812 | { | |
7813 | upgrade_cbdata_t *cbp = arg; | |
7814 | nvlist_t *config; | |
7815 | uint64_t version; | |
7816 | ||
7817 | config = zpool_get_config(zhp, NULL); | |
7818 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
7819 | &version) == 0); | |
7820 | ||
7821 | if (version >= SPA_VERSION_FEATURES) { | |
7822 | int i; | |
7823 | boolean_t poolfirst = B_TRUE; | |
7824 | nvlist_t *enabled = zpool_get_features(zhp); | |
7825 | ||
7826 | for (i = 0; i < SPA_FEATURES; i++) { | |
7827 | const char *fguid = spa_feature_table[i].fi_guid; | |
7828 | const char *fname = spa_feature_table[i].fi_uname; | |
7829 | if (!nvlist_exists(enabled, fguid)) { | |
7830 | if (cbp->cb_first) { | |
7831 | (void) printf(gettext("\nSome " | |
7832 | "supported features are not " | |
7833 | "enabled on the following pools. " | |
7834 | "Once a\nfeature is enabled the " | |
7835 | "pool may become incompatible with " | |
7836 | "software\nthat does not support " | |
7837 | "the feature. See " | |
7838 | "zpool-features(5) for " | |
7839 | "details.\n\n")); | |
7840 | (void) printf(gettext("POOL " | |
7841 | "FEATURE\n")); | |
7842 | (void) printf(gettext("------" | |
7843 | "---------\n")); | |
7844 | cbp->cb_first = B_FALSE; | |
7845 | } | |
7846 | ||
7847 | if (poolfirst) { | |
7848 | (void) printf(gettext("%s\n"), | |
7849 | zpool_get_name(zhp)); | |
7850 | poolfirst = B_FALSE; | |
7851 | } | |
7852 | ||
7853 | (void) printf(gettext(" %s\n"), fname); | |
7854 | } | |
6f1ffb06 MA |
7855 | /* |
7856 | * If they did "zpool upgrade -a", then we could | |
7857 | * be doing ioctls to different pools. We need | |
7858 | * to log this history once to each pool, and bypass | |
7859 | * the normal history logging that happens in main(). | |
7860 | */ | |
7861 | (void) zpool_log_history(g_zfs, history_str); | |
7862 | log_history = B_FALSE; | |
b9b24bb4 CS |
7863 | } |
7864 | } | |
7865 | ||
7866 | return (0); | |
34dc7c2f BB |
7867 | } |
7868 | ||
7869 | /* ARGSUSED */ | |
7870 | static int | |
7871 | upgrade_one(zpool_handle_t *zhp, void *data) | |
7872 | { | |
b9b24bb4 | 7873 | boolean_t printnl = B_FALSE; |
34dc7c2f BB |
7874 | upgrade_cbdata_t *cbp = data; |
7875 | uint64_t cur_version; | |
7876 | int ret; | |
7877 | ||
7878 | if (strcmp("log", zpool_get_name(zhp)) == 0) { | |
c66989ba | 7879 | (void) fprintf(stderr, gettext("'log' is now a reserved word\n" |
34dc7c2f BB |
7880 | "Pool 'log' must be renamed using export and import" |
7881 | " to upgrade.\n")); | |
7882 | return (1); | |
7883 | } | |
7884 | ||
7885 | cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); | |
7886 | if (cur_version > cbp->cb_version) { | |
7887 | (void) printf(gettext("Pool '%s' is already formatted " | |
b9b24bb4 | 7888 | "using more current version '%llu'.\n\n"), |
02730c33 | 7889 | zpool_get_name(zhp), (u_longlong_t)cur_version); |
34dc7c2f BB |
7890 | return (0); |
7891 | } | |
b9b24bb4 CS |
7892 | |
7893 | if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { | |
34dc7c2f | 7894 | (void) printf(gettext("Pool '%s' is already formatted " |
b9b24bb4 | 7895 | "using version %llu.\n\n"), zpool_get_name(zhp), |
02730c33 | 7896 | (u_longlong_t)cbp->cb_version); |
34dc7c2f BB |
7897 | return (0); |
7898 | } | |
7899 | ||
b9b24bb4 CS |
7900 | if (cur_version != cbp->cb_version) { |
7901 | printnl = B_TRUE; | |
7902 | ret = upgrade_version(zhp, cbp->cb_version); | |
7903 | if (ret != 0) | |
7904 | return (ret); | |
7905 | } | |
34dc7c2f | 7906 | |
b9b24bb4 CS |
7907 | if (cbp->cb_version >= SPA_VERSION_FEATURES) { |
7908 | int count = 0; | |
7909 | ret = upgrade_enable_all(zhp, &count); | |
7910 | if (ret != 0) | |
7911 | return (ret); | |
7912 | ||
7913 | if (count != 0) { | |
7914 | printnl = B_TRUE; | |
7915 | } else if (cur_version == SPA_VERSION) { | |
7916 | (void) printf(gettext("Pool '%s' already has all " | |
7917 | "supported features enabled.\n"), | |
7918 | zpool_get_name(zhp)); | |
7919 | } | |
7920 | } | |
7921 | ||
7922 | if (printnl) { | |
7923 | (void) printf(gettext("\n")); | |
34dc7c2f BB |
7924 | } |
7925 | ||
b9b24bb4 | 7926 | return (0); |
34dc7c2f BB |
7927 | } |
7928 | ||
7929 | /* | |
7930 | * zpool upgrade | |
7931 | * zpool upgrade -v | |
7932 | * zpool upgrade [-V version] <-a | pool ...> | |
7933 | * | |
7934 | * With no arguments, display downrev'd ZFS pool available for upgrade. | |
7935 | * Individual pools can be upgraded by specifying the pool, and '-a' will | |
7936 | * upgrade all pools. | |
7937 | */ | |
7938 | int | |
7939 | zpool_do_upgrade(int argc, char **argv) | |
7940 | { | |
7941 | int c; | |
7942 | upgrade_cbdata_t cb = { 0 }; | |
7943 | int ret = 0; | |
7944 | boolean_t showversions = B_FALSE; | |
b9b24bb4 | 7945 | boolean_t upgradeall = B_FALSE; |
34dc7c2f BB |
7946 | char *end; |
7947 | ||
7948 | ||
7949 | /* check options */ | |
9babb374 | 7950 | while ((c = getopt(argc, argv, ":avV:")) != -1) { |
34dc7c2f BB |
7951 | switch (c) { |
7952 | case 'a': | |
b9b24bb4 | 7953 | upgradeall = B_TRUE; |
34dc7c2f BB |
7954 | break; |
7955 | case 'v': | |
7956 | showversions = B_TRUE; | |
7957 | break; | |
7958 | case 'V': | |
7959 | cb.cb_version = strtoll(optarg, &end, 10); | |
9ae529ec CS |
7960 | if (*end != '\0' || |
7961 | !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { | |
34dc7c2f BB |
7962 | (void) fprintf(stderr, |
7963 | gettext("invalid version '%s'\n"), optarg); | |
7964 | usage(B_FALSE); | |
7965 | } | |
7966 | break; | |
9babb374 BB |
7967 | case ':': |
7968 | (void) fprintf(stderr, gettext("missing argument for " | |
7969 | "'%c' option\n"), optopt); | |
7970 | usage(B_FALSE); | |
7971 | break; | |
34dc7c2f BB |
7972 | case '?': |
7973 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
7974 | optopt); | |
7975 | usage(B_FALSE); | |
7976 | } | |
7977 | } | |
7978 | ||
7979 | cb.cb_argc = argc; | |
7980 | cb.cb_argv = argv; | |
7981 | argc -= optind; | |
7982 | argv += optind; | |
7983 | ||
7984 | if (cb.cb_version == 0) { | |
7985 | cb.cb_version = SPA_VERSION; | |
b9b24bb4 | 7986 | } else if (!upgradeall && argc == 0) { |
34dc7c2f BB |
7987 | (void) fprintf(stderr, gettext("-V option is " |
7988 | "incompatible with other arguments\n")); | |
7989 | usage(B_FALSE); | |
7990 | } | |
7991 | ||
7992 | if (showversions) { | |
b9b24bb4 | 7993 | if (upgradeall || argc != 0) { |
34dc7c2f BB |
7994 | (void) fprintf(stderr, gettext("-v option is " |
7995 | "incompatible with other arguments\n")); | |
7996 | usage(B_FALSE); | |
7997 | } | |
b9b24bb4 | 7998 | } else if (upgradeall) { |
34dc7c2f BB |
7999 | if (argc != 0) { |
8000 | (void) fprintf(stderr, gettext("-a option should not " | |
8001 | "be used along with a pool name\n")); | |
8002 | usage(B_FALSE); | |
8003 | } | |
8004 | } | |
8005 | ||
9ae529ec CS |
8006 | (void) printf(gettext("This system supports ZFS pool feature " |
8007 | "flags.\n\n")); | |
34dc7c2f | 8008 | if (showversions) { |
b9b24bb4 CS |
8009 | int i; |
8010 | ||
8011 | (void) printf(gettext("The following features are " | |
8012 | "supported:\n\n")); | |
8013 | (void) printf(gettext("FEAT DESCRIPTION\n")); | |
8014 | (void) printf("----------------------------------------------" | |
8015 | "---------------\n"); | |
8016 | for (i = 0; i < SPA_FEATURES; i++) { | |
8017 | zfeature_info_t *fi = &spa_feature_table[i]; | |
241b5415 MA |
8018 | const char *ro = |
8019 | (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? | |
b9b24bb4 CS |
8020 | " (read-only compatible)" : ""; |
8021 | ||
8022 | (void) printf("%-37s%s\n", fi->fi_uname, ro); | |
8023 | (void) printf(" %s\n", fi->fi_desc); | |
8024 | } | |
8025 | (void) printf("\n"); | |
8026 | ||
8027 | (void) printf(gettext("The following legacy versions are also " | |
34dc7c2f BB |
8028 | "supported:\n\n")); |
8029 | (void) printf(gettext("VER DESCRIPTION\n")); | |
8030 | (void) printf("--- -----------------------------------------" | |
8031 | "---------------\n"); | |
8032 | (void) printf(gettext(" 1 Initial ZFS version\n")); | |
8033 | (void) printf(gettext(" 2 Ditto blocks " | |
8034 | "(replicated metadata)\n")); | |
8035 | (void) printf(gettext(" 3 Hot spares and double parity " | |
8036 | "RAID-Z\n")); | |
8037 | (void) printf(gettext(" 4 zpool history\n")); | |
8038 | (void) printf(gettext(" 5 Compression using the gzip " | |
8039 | "algorithm\n")); | |
8040 | (void) printf(gettext(" 6 bootfs pool property\n")); | |
8041 | (void) printf(gettext(" 7 Separate intent log devices\n")); | |
8042 | (void) printf(gettext(" 8 Delegated administration\n")); | |
8043 | (void) printf(gettext(" 9 refquota and refreservation " | |
8044 | "properties\n")); | |
8045 | (void) printf(gettext(" 10 Cache devices\n")); | |
b128c09f BB |
8046 | (void) printf(gettext(" 11 Improved scrub performance\n")); |
8047 | (void) printf(gettext(" 12 Snapshot properties\n")); | |
8048 | (void) printf(gettext(" 13 snapused property\n")); | |
9babb374 BB |
8049 | (void) printf(gettext(" 14 passthrough-x aclinherit\n")); |
8050 | (void) printf(gettext(" 15 user/group space accounting\n")); | |
8051 | (void) printf(gettext(" 16 stmf property support\n")); | |
45d1cae3 | 8052 | (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); |
428870ff BB |
8053 | (void) printf(gettext(" 18 Snapshot user holds\n")); |
8054 | (void) printf(gettext(" 19 Log device removal\n")); | |
8055 | (void) printf(gettext(" 20 Compression using zle " | |
8056 | "(zero-length encoding)\n")); | |
8057 | (void) printf(gettext(" 21 Deduplication\n")); | |
8058 | (void) printf(gettext(" 22 Received properties\n")); | |
8059 | (void) printf(gettext(" 23 Slim ZIL\n")); | |
8060 | (void) printf(gettext(" 24 System attributes\n")); | |
8061 | (void) printf(gettext(" 25 Improved scrub stats\n")); | |
8062 | (void) printf(gettext(" 26 Improved snapshot deletion " | |
8063 | "performance\n")); | |
572e2857 BB |
8064 | (void) printf(gettext(" 27 Improved snapshot creation " |
8065 | "performance\n")); | |
8066 | (void) printf(gettext(" 28 Multiple vdev replacements\n")); | |
428870ff BB |
8067 | (void) printf(gettext("\nFor more information on a particular " |
8068 | "version, including supported releases,\n")); | |
8069 | (void) printf(gettext("see the ZFS Administration Guide.\n\n")); | |
b9b24bb4 CS |
8070 | } else if (argc == 0 && upgradeall) { |
8071 | cb.cb_first = B_TRUE; | |
34dc7c2f | 8072 | ret = zpool_iter(g_zfs, upgrade_cb, &cb); |
b9b24bb4 CS |
8073 | if (ret == 0 && cb.cb_first) { |
8074 | if (cb.cb_version == SPA_VERSION) { | |
8075 | (void) printf(gettext("All pools are already " | |
8076 | "formatted using feature flags.\n\n")); | |
8077 | (void) printf(gettext("Every feature flags " | |
8078 | "pool already has all supported features " | |
8079 | "enabled.\n")); | |
8080 | } else { | |
8081 | (void) printf(gettext("All pools are already " | |
8082 | "formatted with version %llu or higher.\n"), | |
02730c33 | 8083 | (u_longlong_t)cb.cb_version); |
34dc7c2f BB |
8084 | } |
8085 | } | |
b9b24bb4 CS |
8086 | } else if (argc == 0) { |
8087 | cb.cb_first = B_TRUE; | |
8088 | ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); | |
8089 | assert(ret == 0); | |
34dc7c2f | 8090 | |
b9b24bb4 CS |
8091 | if (cb.cb_first) { |
8092 | (void) printf(gettext("All pools are formatted " | |
8093 | "using feature flags.\n\n")); | |
8094 | } else { | |
8095 | (void) printf(gettext("\nUse 'zpool upgrade -v' " | |
8096 | "for a list of available legacy versions.\n")); | |
8097 | } | |
8098 | ||
8099 | cb.cb_first = B_TRUE; | |
8100 | ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); | |
8101 | assert(ret == 0); | |
8102 | ||
8103 | if (cb.cb_first) { | |
8104 | (void) printf(gettext("Every feature flags pool has " | |
8105 | "all supported features enabled.\n")); | |
8106 | } else { | |
8107 | (void) printf(gettext("\n")); | |
34dc7c2f BB |
8108 | } |
8109 | } else { | |
8110 | ret = for_each_pool(argc, argv, B_FALSE, NULL, | |
8111 | upgrade_one, &cb); | |
8112 | } | |
8113 | ||
8114 | return (ret); | |
8115 | } | |
8116 | ||
8117 | typedef struct hist_cbdata { | |
8118 | boolean_t first; | |
6f1ffb06 MA |
8119 | boolean_t longfmt; |
8120 | boolean_t internal; | |
34dc7c2f BB |
8121 | } hist_cbdata_t; |
8122 | ||
34dc7c2f BB |
8123 | /* |
8124 | * Print out the command history for a specific pool. | |
8125 | */ | |
8126 | static int | |
8127 | get_history_one(zpool_handle_t *zhp, void *data) | |
8128 | { | |
8129 | nvlist_t *nvhis; | |
8130 | nvlist_t **records; | |
8131 | uint_t numrecords; | |
34dc7c2f | 8132 | int ret, i; |
34dc7c2f | 8133 | hist_cbdata_t *cb = (hist_cbdata_t *)data; |
34dc7c2f BB |
8134 | |
8135 | cb->first = B_FALSE; | |
8136 | ||
8137 | (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); | |
8138 | ||
8139 | if ((ret = zpool_get_history(zhp, &nvhis)) != 0) | |
8140 | return (ret); | |
8141 | ||
8142 | verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, | |
8143 | &records, &numrecords) == 0); | |
8144 | for (i = 0; i < numrecords; i++) { | |
6f1ffb06 MA |
8145 | nvlist_t *rec = records[i]; |
8146 | char tbuf[30] = ""; | |
34dc7c2f | 8147 | |
6f1ffb06 MA |
8148 | if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { |
8149 | time_t tsec; | |
8150 | struct tm t; | |
34dc7c2f | 8151 | |
6f1ffb06 MA |
8152 | tsec = fnvlist_lookup_uint64(records[i], |
8153 | ZPOOL_HIST_TIME); | |
8154 | (void) localtime_r(&tsec, &t); | |
8155 | (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); | |
8156 | } | |
8157 | ||
8158 | if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { | |
8159 | (void) printf("%s %s", tbuf, | |
8160 | fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); | |
8161 | } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { | |
8162 | int ievent = | |
8163 | fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); | |
8164 | if (!cb->internal) | |
34dc7c2f | 8165 | continue; |
6f1ffb06 MA |
8166 | if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { |
8167 | (void) printf("%s unrecognized record:\n", | |
8168 | tbuf); | |
8169 | dump_nvlist(rec, 4); | |
34dc7c2f | 8170 | continue; |
6f1ffb06 MA |
8171 | } |
8172 | (void) printf("%s [internal %s txg:%lld] %s", tbuf, | |
8173 | zfs_history_event_names[ievent], | |
02730c33 | 8174 | (longlong_t)fnvlist_lookup_uint64( |
d1d7e268 | 8175 | rec, ZPOOL_HIST_TXG), |
6f1ffb06 MA |
8176 | fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); |
8177 | } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { | |
8178 | if (!cb->internal) | |
8179 | continue; | |
8180 | (void) printf("%s [txg:%lld] %s", tbuf, | |
02730c33 | 8181 | (longlong_t)fnvlist_lookup_uint64( |
d1d7e268 | 8182 | rec, ZPOOL_HIST_TXG), |
6f1ffb06 MA |
8183 | fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); |
8184 | if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { | |
8185 | (void) printf(" %s (%llu)", | |
8186 | fnvlist_lookup_string(rec, | |
8187 | ZPOOL_HIST_DSNAME), | |
d1d7e268 | 8188 | (u_longlong_t)fnvlist_lookup_uint64(rec, |
6f1ffb06 MA |
8189 | ZPOOL_HIST_DSID)); |
8190 | } | |
8191 | (void) printf(" %s", fnvlist_lookup_string(rec, | |
8192 | ZPOOL_HIST_INT_STR)); | |
8193 | } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { | |
8194 | if (!cb->internal) | |
8195 | continue; | |
8196 | (void) printf("%s ioctl %s\n", tbuf, | |
8197 | fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); | |
8198 | if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { | |
8199 | (void) printf(" input:\n"); | |
8200 | dump_nvlist(fnvlist_lookup_nvlist(rec, | |
8201 | ZPOOL_HIST_INPUT_NVL), 8); | |
8202 | } | |
8203 | if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { | |
8204 | (void) printf(" output:\n"); | |
8205 | dump_nvlist(fnvlist_lookup_nvlist(rec, | |
8206 | ZPOOL_HIST_OUTPUT_NVL), 8); | |
8207 | } | |
d99a0153 CW |
8208 | if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) { |
8209 | (void) printf(" errno: %lld\n", | |
8210 | (longlong_t)fnvlist_lookup_int64(rec, | |
8211 | ZPOOL_HIST_ERRNO)); | |
8212 | } | |
6f1ffb06 MA |
8213 | } else { |
8214 | if (!cb->internal) | |
8215 | continue; | |
8216 | (void) printf("%s unrecognized record:\n", tbuf); | |
8217 | dump_nvlist(rec, 4); | |
34dc7c2f | 8218 | } |
34dc7c2f BB |
8219 | |
8220 | if (!cb->longfmt) { | |
8221 | (void) printf("\n"); | |
8222 | continue; | |
8223 | } | |
8224 | (void) printf(" ["); | |
6f1ffb06 MA |
8225 | if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { |
8226 | uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); | |
8227 | struct passwd *pwd = getpwuid(who); | |
8228 | (void) printf("user %d ", (int)who); | |
8229 | if (pwd != NULL) | |
8230 | (void) printf("(%s) ", pwd->pw_name); | |
34dc7c2f | 8231 | } |
6f1ffb06 MA |
8232 | if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { |
8233 | (void) printf("on %s", | |
8234 | fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); | |
34dc7c2f | 8235 | } |
6f1ffb06 MA |
8236 | if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { |
8237 | (void) printf(":%s", | |
8238 | fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); | |
34dc7c2f BB |
8239 | } |
8240 | ||
8241 | (void) printf("]"); | |
8242 | (void) printf("\n"); | |
8243 | } | |
8244 | (void) printf("\n"); | |
8245 | nvlist_free(nvhis); | |
8246 | ||
8247 | return (ret); | |
8248 | } | |
8249 | ||
8250 | /* | |
8251 | * zpool history <pool> | |
8252 | * | |
8253 | * Displays the history of commands that modified pools. | |
8254 | */ | |
34dc7c2f BB |
8255 | int |
8256 | zpool_do_history(int argc, char **argv) | |
8257 | { | |
8258 | hist_cbdata_t cbdata = { 0 }; | |
8259 | int ret; | |
8260 | int c; | |
8261 | ||
8262 | cbdata.first = B_TRUE; | |
8263 | /* check options */ | |
8264 | while ((c = getopt(argc, argv, "li")) != -1) { | |
8265 | switch (c) { | |
8266 | case 'l': | |
6f1ffb06 | 8267 | cbdata.longfmt = B_TRUE; |
34dc7c2f BB |
8268 | break; |
8269 | case 'i': | |
6f1ffb06 | 8270 | cbdata.internal = B_TRUE; |
34dc7c2f BB |
8271 | break; |
8272 | case '?': | |
8273 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
8274 | optopt); | |
8275 | usage(B_FALSE); | |
8276 | } | |
8277 | } | |
8278 | argc -= optind; | |
8279 | argv += optind; | |
8280 | ||
8281 | ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one, | |
8282 | &cbdata); | |
8283 | ||
8284 | if (argc == 0 && cbdata.first == B_TRUE) { | |
42cb3819 | 8285 | (void) fprintf(stderr, gettext("no pools available\n")); |
34dc7c2f BB |
8286 | return (0); |
8287 | } | |
8288 | ||
8289 | return (ret); | |
8290 | } | |
8291 | ||
26685276 BB |
8292 | typedef struct ev_opts { |
8293 | int verbose; | |
c5343ba7 | 8294 | int scripted; |
26685276 BB |
8295 | int follow; |
8296 | int clear; | |
88f9c939 | 8297 | char poolname[ZFS_MAX_DATASET_NAME_LEN]; |
26685276 BB |
8298 | } ev_opts_t; |
8299 | ||
8300 | static void | |
1e1c3980 | 8301 | zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts) |
26685276 BB |
8302 | { |
8303 | char ctime_str[26], str[32], *ptr; | |
8304 | int64_t *tv; | |
8305 | uint_t n; | |
8306 | ||
8307 | verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0); | |
8308 | memset(str, ' ', 32); | |
8309 | (void) ctime_r((const time_t *)&tv[0], ctime_str); | |
d1d7e268 MK |
8310 | (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */ |
8311 | (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */ | |
8312 | (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */ | |
8313 | (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */ | |
1e1c3980 SH |
8314 | if (opts->scripted) |
8315 | (void) printf(gettext("%s\t"), str); | |
8316 | else | |
8317 | (void) printf(gettext("%s "), str); | |
26685276 BB |
8318 | |
8319 | verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0); | |
8320 | (void) printf(gettext("%s\n"), ptr); | |
8321 | } | |
8322 | ||
8323 | static void | |
8324 | zpool_do_events_nvprint(nvlist_t *nvl, int depth) | |
8325 | { | |
8326 | nvpair_t *nvp; | |
8327 | ||
8328 | for (nvp = nvlist_next_nvpair(nvl, NULL); | |
8329 | nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) { | |
8330 | ||
8331 | data_type_t type = nvpair_type(nvp); | |
8332 | const char *name = nvpair_name(nvp); | |
8333 | ||
8334 | boolean_t b; | |
8335 | uint8_t i8; | |
8336 | uint16_t i16; | |
8337 | uint32_t i32; | |
8338 | uint64_t i64; | |
8339 | char *str; | |
8340 | nvlist_t *cnv; | |
8341 | ||
8342 | printf(gettext("%*s%s = "), depth, "", name); | |
8343 | ||
8344 | switch (type) { | |
8345 | case DATA_TYPE_BOOLEAN: | |
8346 | printf(gettext("%s"), "1"); | |
8347 | break; | |
8348 | ||
8349 | case DATA_TYPE_BOOLEAN_VALUE: | |
8350 | (void) nvpair_value_boolean_value(nvp, &b); | |
8351 | printf(gettext("%s"), b ? "1" : "0"); | |
8352 | break; | |
8353 | ||
8354 | case DATA_TYPE_BYTE: | |
8355 | (void) nvpair_value_byte(nvp, &i8); | |
8356 | printf(gettext("0x%x"), i8); | |
8357 | break; | |
8358 | ||
8359 | case DATA_TYPE_INT8: | |
8360 | (void) nvpair_value_int8(nvp, (void *)&i8); | |
8361 | printf(gettext("0x%x"), i8); | |
8362 | break; | |
8363 | ||
8364 | case DATA_TYPE_UINT8: | |
8365 | (void) nvpair_value_uint8(nvp, &i8); | |
8366 | printf(gettext("0x%x"), i8); | |
8367 | break; | |
8368 | ||
8369 | case DATA_TYPE_INT16: | |
8370 | (void) nvpair_value_int16(nvp, (void *)&i16); | |
8371 | printf(gettext("0x%x"), i16); | |
8372 | break; | |
8373 | ||
8374 | case DATA_TYPE_UINT16: | |
8375 | (void) nvpair_value_uint16(nvp, &i16); | |
8376 | printf(gettext("0x%x"), i16); | |
8377 | break; | |
8378 | ||
8379 | case DATA_TYPE_INT32: | |
8380 | (void) nvpair_value_int32(nvp, (void *)&i32); | |
8381 | printf(gettext("0x%x"), i32); | |
8382 | break; | |
8383 | ||
8384 | case DATA_TYPE_UINT32: | |
8385 | (void) nvpair_value_uint32(nvp, &i32); | |
8386 | printf(gettext("0x%x"), i32); | |
8387 | break; | |
8388 | ||
8389 | case DATA_TYPE_INT64: | |
8390 | (void) nvpair_value_int64(nvp, (void *)&i64); | |
8391 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
8392 | break; | |
8393 | ||
8394 | case DATA_TYPE_UINT64: | |
8395 | (void) nvpair_value_uint64(nvp, &i64); | |
d02ca379 DB |
8396 | /* |
8397 | * translate vdev state values to readable | |
8398 | * strings to aide zpool events consumers | |
8399 | */ | |
8400 | if (strcmp(name, | |
8401 | FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 || | |
8402 | strcmp(name, | |
8403 | FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) { | |
8404 | printf(gettext("\"%s\" (0x%llx)"), | |
8405 | zpool_state_to_name(i64, VDEV_AUX_NONE), | |
8406 | (u_longlong_t)i64); | |
8407 | } else { | |
8408 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
8409 | } | |
26685276 BB |
8410 | break; |
8411 | ||
8412 | case DATA_TYPE_HRTIME: | |
8413 | (void) nvpair_value_hrtime(nvp, (void *)&i64); | |
8414 | printf(gettext("0x%llx"), (u_longlong_t)i64); | |
8415 | break; | |
8416 | ||
8417 | case DATA_TYPE_STRING: | |
8418 | (void) nvpair_value_string(nvp, &str); | |
8419 | printf(gettext("\"%s\""), str ? str : "<NULL>"); | |
8420 | break; | |
8421 | ||
8422 | case DATA_TYPE_NVLIST: | |
8423 | printf(gettext("(embedded nvlist)\n")); | |
8424 | (void) nvpair_value_nvlist(nvp, &cnv); | |
8425 | zpool_do_events_nvprint(cnv, depth + 8); | |
c5343ba7 | 8426 | printf(gettext("%*s(end %s)"), depth, "", name); |
26685276 BB |
8427 | break; |
8428 | ||
8429 | case DATA_TYPE_NVLIST_ARRAY: { | |
8430 | nvlist_t **val; | |
8431 | uint_t i, nelem; | |
8432 | ||
8433 | (void) nvpair_value_nvlist_array(nvp, &val, &nelem); | |
8434 | printf(gettext("(%d embedded nvlists)\n"), nelem); | |
8435 | for (i = 0; i < nelem; i++) { | |
8436 | printf(gettext("%*s%s[%d] = %s\n"), | |
d1d7e268 | 8437 | depth, "", name, i, "(embedded nvlist)"); |
26685276 BB |
8438 | zpool_do_events_nvprint(val[i], depth + 8); |
8439 | printf(gettext("%*s(end %s[%i])\n"), | |
d1d7e268 | 8440 | depth, "", name, i); |
26685276 BB |
8441 | } |
8442 | printf(gettext("%*s(end %s)\n"), depth, "", name); | |
8443 | } | |
8444 | break; | |
8445 | ||
8446 | case DATA_TYPE_INT8_ARRAY: { | |
8447 | int8_t *val; | |
8448 | uint_t i, nelem; | |
8449 | ||
8450 | (void) nvpair_value_int8_array(nvp, &val, &nelem); | |
8451 | for (i = 0; i < nelem; i++) | |
8452 | printf(gettext("0x%x "), val[i]); | |
8453 | ||
8454 | break; | |
8455 | } | |
8456 | ||
8457 | case DATA_TYPE_UINT8_ARRAY: { | |
8458 | uint8_t *val; | |
8459 | uint_t i, nelem; | |
8460 | ||
8461 | (void) nvpair_value_uint8_array(nvp, &val, &nelem); | |
8462 | for (i = 0; i < nelem; i++) | |
8463 | printf(gettext("0x%x "), val[i]); | |
8464 | ||
8465 | break; | |
8466 | } | |
8467 | ||
8468 | case DATA_TYPE_INT16_ARRAY: { | |
8469 | int16_t *val; | |
8470 | uint_t i, nelem; | |
8471 | ||
8472 | (void) nvpair_value_int16_array(nvp, &val, &nelem); | |
8473 | for (i = 0; i < nelem; i++) | |
8474 | printf(gettext("0x%x "), val[i]); | |
8475 | ||
8476 | break; | |
8477 | } | |
8478 | ||
8479 | case DATA_TYPE_UINT16_ARRAY: { | |
8480 | uint16_t *val; | |
8481 | uint_t i, nelem; | |
8482 | ||
8483 | (void) nvpair_value_uint16_array(nvp, &val, &nelem); | |
8484 | for (i = 0; i < nelem; i++) | |
8485 | printf(gettext("0x%x "), val[i]); | |
8486 | ||
8487 | break; | |
8488 | } | |
8489 | ||
8490 | case DATA_TYPE_INT32_ARRAY: { | |
8491 | int32_t *val; | |
8492 | uint_t i, nelem; | |
8493 | ||
8494 | (void) nvpair_value_int32_array(nvp, &val, &nelem); | |
8495 | for (i = 0; i < nelem; i++) | |
8496 | printf(gettext("0x%x "), val[i]); | |
8497 | ||
8498 | break; | |
8499 | } | |
8500 | ||
8501 | case DATA_TYPE_UINT32_ARRAY: { | |
8502 | uint32_t *val; | |
8503 | uint_t i, nelem; | |
8504 | ||
8505 | (void) nvpair_value_uint32_array(nvp, &val, &nelem); | |
8506 | for (i = 0; i < nelem; i++) | |
8507 | printf(gettext("0x%x "), val[i]); | |
8508 | ||
8509 | break; | |
8510 | } | |
8511 | ||
8512 | case DATA_TYPE_INT64_ARRAY: { | |
8513 | int64_t *val; | |
8514 | uint_t i, nelem; | |
8515 | ||
8516 | (void) nvpair_value_int64_array(nvp, &val, &nelem); | |
8517 | for (i = 0; i < nelem; i++) | |
d1d7e268 MK |
8518 | printf(gettext("0x%llx "), |
8519 | (u_longlong_t)val[i]); | |
26685276 BB |
8520 | |
8521 | break; | |
8522 | } | |
8523 | ||
8524 | case DATA_TYPE_UINT64_ARRAY: { | |
8525 | uint64_t *val; | |
8526 | uint_t i, nelem; | |
8527 | ||
8528 | (void) nvpair_value_uint64_array(nvp, &val, &nelem); | |
8529 | for (i = 0; i < nelem; i++) | |
d1d7e268 MK |
8530 | printf(gettext("0x%llx "), |
8531 | (u_longlong_t)val[i]); | |
26685276 BB |
8532 | |
8533 | break; | |
8534 | } | |
8535 | ||
d21705ea BB |
8536 | case DATA_TYPE_STRING_ARRAY: { |
8537 | char **str; | |
8538 | uint_t i, nelem; | |
8539 | ||
8540 | (void) nvpair_value_string_array(nvp, &str, &nelem); | |
8541 | for (i = 0; i < nelem; i++) | |
8542 | printf(gettext("\"%s\" "), | |
8543 | str[i] ? str[i] : "<NULL>"); | |
8544 | ||
8545 | break; | |
8546 | } | |
8547 | ||
26685276 BB |
8548 | case DATA_TYPE_BOOLEAN_ARRAY: |
8549 | case DATA_TYPE_BYTE_ARRAY: | |
8550 | case DATA_TYPE_DOUBLE: | |
6b64382b | 8551 | case DATA_TYPE_DONTCARE: |
26685276 BB |
8552 | case DATA_TYPE_UNKNOWN: |
8553 | printf(gettext("<unknown>")); | |
8554 | break; | |
8555 | } | |
8556 | ||
8557 | printf(gettext("\n")); | |
8558 | } | |
8559 | } | |
8560 | ||
8561 | static int | |
8562 | zpool_do_events_next(ev_opts_t *opts) | |
8563 | { | |
8564 | nvlist_t *nvl; | |
9b101a73 | 8565 | int zevent_fd, ret, dropped; |
88f9c939 | 8566 | char *pool; |
26685276 | 8567 | |
9b101a73 BB |
8568 | zevent_fd = open(ZFS_DEV, O_RDWR); |
8569 | VERIFY(zevent_fd >= 0); | |
26685276 | 8570 | |
c5343ba7 BB |
8571 | if (!opts->scripted) |
8572 | (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS"); | |
26685276 BB |
8573 | |
8574 | while (1) { | |
8575 | ret = zpool_events_next(g_zfs, &nvl, &dropped, | |
8c7aa0cf | 8576 | (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd); |
26685276 BB |
8577 | if (ret || nvl == NULL) |
8578 | break; | |
8579 | ||
8580 | if (dropped > 0) | |
8581 | (void) printf(gettext("dropped %d events\n"), dropped); | |
8582 | ||
88f9c939 | 8583 | if (strlen(opts->poolname) > 0 && |
8584 | nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 && | |
8585 | strcmp(opts->poolname, pool) != 0) | |
8586 | continue; | |
8587 | ||
1e1c3980 | 8588 | zpool_do_events_short(nvl, opts); |
26685276 BB |
8589 | |
8590 | if (opts->verbose) { | |
8591 | zpool_do_events_nvprint(nvl, 8); | |
8592 | printf(gettext("\n")); | |
8593 | } | |
50fe577d | 8594 | (void) fflush(stdout); |
26685276 BB |
8595 | |
8596 | nvlist_free(nvl); | |
8597 | } | |
8598 | ||
9b101a73 | 8599 | VERIFY(0 == close(zevent_fd)); |
26685276 BB |
8600 | |
8601 | return (ret); | |
8602 | } | |
8603 | ||
8604 | static int | |
8605 | zpool_do_events_clear(ev_opts_t *opts) | |
8606 | { | |
8607 | int count, ret; | |
8608 | ||
8609 | ret = zpool_events_clear(g_zfs, &count); | |
8610 | if (!ret) | |
8611 | (void) printf(gettext("cleared %d events\n"), count); | |
8612 | ||
8613 | return (ret); | |
8614 | } | |
8615 | ||
8616 | /* | |
88f9c939 | 8617 | * zpool events [-vHf [pool] | -c] |
26685276 BB |
8618 | * |
8619 | * Displays events logs by ZFS. | |
8620 | */ | |
8621 | int | |
8622 | zpool_do_events(int argc, char **argv) | |
8623 | { | |
8624 | ev_opts_t opts = { 0 }; | |
8625 | int ret; | |
8626 | int c; | |
8627 | ||
8628 | /* check options */ | |
c5343ba7 | 8629 | while ((c = getopt(argc, argv, "vHfc")) != -1) { |
26685276 BB |
8630 | switch (c) { |
8631 | case 'v': | |
8632 | opts.verbose = 1; | |
c5343ba7 BB |
8633 | break; |
8634 | case 'H': | |
8635 | opts.scripted = 1; | |
26685276 BB |
8636 | break; |
8637 | case 'f': | |
8638 | opts.follow = 1; | |
8639 | break; | |
8640 | case 'c': | |
8641 | opts.clear = 1; | |
8642 | break; | |
8643 | case '?': | |
8644 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
8645 | optopt); | |
8646 | usage(B_FALSE); | |
8647 | } | |
8648 | } | |
8649 | argc -= optind; | |
8650 | argv += optind; | |
8651 | ||
88f9c939 | 8652 | if (argc > 1) { |
8653 | (void) fprintf(stderr, gettext("too many arguments\n")); | |
8654 | usage(B_FALSE); | |
8655 | } else if (argc == 1) { | |
8656 | (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname)); | |
8657 | if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) { | |
8658 | (void) fprintf(stderr, | |
8659 | gettext("invalid pool name '%s'\n"), opts.poolname); | |
8660 | usage(B_FALSE); | |
8661 | } | |
8662 | } | |
8663 | ||
8664 | if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) && | |
8665 | opts.clear) { | |
8666 | (void) fprintf(stderr, | |
8667 | gettext("invalid options combined with -c\n")); | |
8668 | usage(B_FALSE); | |
8669 | } | |
8670 | ||
26685276 BB |
8671 | if (opts.clear) |
8672 | ret = zpool_do_events_clear(&opts); | |
8673 | else | |
8674 | ret = zpool_do_events_next(&opts); | |
8675 | ||
d1d7e268 | 8676 | return (ret); |
26685276 BB |
8677 | } |
8678 | ||
34dc7c2f BB |
8679 | static int |
8680 | get_callback(zpool_handle_t *zhp, void *data) | |
8681 | { | |
8682 | zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; | |
8683 | char value[MAXNAMELEN]; | |
8684 | zprop_source_t srctype; | |
8685 | zprop_list_t *pl; | |
8686 | ||
8687 | for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { | |
8688 | ||
8689 | /* | |
8690 | * Skip the special fake placeholder. This will also skip | |
8691 | * over the name property when 'all' is specified. | |
8692 | */ | |
8693 | if (pl->pl_prop == ZPOOL_PROP_NAME && | |
8694 | pl == cbp->cb_proplist) | |
8695 | continue; | |
8696 | ||
9ae529ec CS |
8697 | if (pl->pl_prop == ZPROP_INVAL && |
8698 | (zpool_prop_feature(pl->pl_user_prop) || | |
8699 | zpool_prop_unsupported(pl->pl_user_prop))) { | |
8700 | srctype = ZPROP_SRC_LOCAL; | |
34dc7c2f | 8701 | |
9ae529ec CS |
8702 | if (zpool_prop_get_feature(zhp, pl->pl_user_prop, |
8703 | value, sizeof (value)) == 0) { | |
8704 | zprop_print_one_property(zpool_get_name(zhp), | |
8705 | cbp, pl->pl_user_prop, value, srctype, | |
8706 | NULL, NULL); | |
8707 | } | |
8708 | } else { | |
2a8b84b7 | 8709 | if (zpool_get_prop(zhp, pl->pl_prop, value, |
d65e7381 | 8710 | sizeof (value), &srctype, cbp->cb_literal) != 0) |
9ae529ec CS |
8711 | continue; |
8712 | ||
8713 | zprop_print_one_property(zpool_get_name(zhp), cbp, | |
8714 | zpool_prop_to_name(pl->pl_prop), value, srctype, | |
8715 | NULL, NULL); | |
8716 | } | |
34dc7c2f BB |
8717 | } |
8718 | return (0); | |
8719 | } | |
8720 | ||
2a8b84b7 AS |
8721 | /* |
8722 | * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... | |
8723 | * | |
8724 | * -H Scripted mode. Don't display headers, and separate properties | |
8725 | * by a single tab. | |
8726 | * -o List of columns to display. Defaults to | |
8727 | * "name,property,value,source". | |
4e33ba4c | 8728 | * -p Display values in parsable (exact) format. |
2a8b84b7 AS |
8729 | * |
8730 | * Get properties of pools in the system. Output space statistics | |
8731 | * for each one as well as other attributes. | |
8732 | */ | |
34dc7c2f BB |
8733 | int |
8734 | zpool_do_get(int argc, char **argv) | |
8735 | { | |
8736 | zprop_get_cbdata_t cb = { 0 }; | |
8737 | zprop_list_t fake_name = { 0 }; | |
2a8b84b7 AS |
8738 | int ret; |
8739 | int c, i; | |
8740 | char *value; | |
8741 | ||
8742 | cb.cb_first = B_TRUE; | |
8743 | ||
8744 | /* | |
8745 | * Set up default columns and sources. | |
8746 | */ | |
8747 | cb.cb_sources = ZPROP_SRC_ALL; | |
8748 | cb.cb_columns[0] = GET_COL_NAME; | |
8749 | cb.cb_columns[1] = GET_COL_PROPERTY; | |
8750 | cb.cb_columns[2] = GET_COL_VALUE; | |
8751 | cb.cb_columns[3] = GET_COL_SOURCE; | |
8752 | cb.cb_type = ZFS_TYPE_POOL; | |
34dc7c2f | 8753 | |
d65e7381 | 8754 | /* check options */ |
2a8b84b7 | 8755 | while ((c = getopt(argc, argv, ":Hpo:")) != -1) { |
d65e7381 RE |
8756 | switch (c) { |
8757 | case 'p': | |
8758 | cb.cb_literal = B_TRUE; | |
8759 | break; | |
79eb71dc TF |
8760 | case 'H': |
8761 | cb.cb_scripted = B_TRUE; | |
8762 | break; | |
2a8b84b7 AS |
8763 | case 'o': |
8764 | bzero(&cb.cb_columns, sizeof (cb.cb_columns)); | |
8765 | i = 0; | |
8766 | while (*optarg != '\0') { | |
8767 | static char *col_subopts[] = | |
8768 | { "name", "property", "value", "source", | |
8769 | "all", NULL }; | |
8770 | ||
8771 | if (i == ZFS_GET_NCOLS) { | |
8772 | (void) fprintf(stderr, gettext("too " | |
8773 | "many fields given to -o " | |
8774 | "option\n")); | |
8775 | usage(B_FALSE); | |
8776 | } | |
79eb71dc | 8777 | |
2a8b84b7 AS |
8778 | switch (getsubopt(&optarg, col_subopts, |
8779 | &value)) { | |
8780 | case 0: | |
8781 | cb.cb_columns[i++] = GET_COL_NAME; | |
8782 | break; | |
8783 | case 1: | |
8784 | cb.cb_columns[i++] = GET_COL_PROPERTY; | |
8785 | break; | |
8786 | case 2: | |
8787 | cb.cb_columns[i++] = GET_COL_VALUE; | |
8788 | break; | |
8789 | case 3: | |
8790 | cb.cb_columns[i++] = GET_COL_SOURCE; | |
8791 | break; | |
8792 | case 4: | |
8793 | if (i > 0) { | |
8794 | (void) fprintf(stderr, | |
8795 | gettext("\"all\" conflicts " | |
8796 | "with specific fields " | |
8797 | "given to -o option\n")); | |
8798 | usage(B_FALSE); | |
8799 | } | |
8800 | cb.cb_columns[0] = GET_COL_NAME; | |
8801 | cb.cb_columns[1] = GET_COL_PROPERTY; | |
8802 | cb.cb_columns[2] = GET_COL_VALUE; | |
8803 | cb.cb_columns[3] = GET_COL_SOURCE; | |
8804 | i = ZFS_GET_NCOLS; | |
8805 | break; | |
8806 | default: | |
8807 | (void) fprintf(stderr, | |
8808 | gettext("invalid column name " | |
8809 | "'%s'\n"), value); | |
8810 | usage(B_FALSE); | |
8811 | } | |
8812 | } | |
8813 | break; | |
d65e7381 RE |
8814 | case '?': |
8815 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
8816 | optopt); | |
8817 | usage(B_FALSE); | |
8818 | } | |
8819 | } | |
8820 | ||
8821 | argc -= optind; | |
8822 | argv += optind; | |
8823 | ||
8824 | if (argc < 1) { | |
9ae529ec CS |
8825 | (void) fprintf(stderr, gettext("missing property " |
8826 | "argument\n")); | |
34dc7c2f | 8827 | usage(B_FALSE); |
9ae529ec | 8828 | } |
34dc7c2f | 8829 | |
2a8b84b7 AS |
8830 | if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, |
8831 | ZFS_TYPE_POOL) != 0) | |
34dc7c2f BB |
8832 | usage(B_FALSE); |
8833 | ||
d65e7381 RE |
8834 | argc--; |
8835 | argv++; | |
8836 | ||
34dc7c2f BB |
8837 | if (cb.cb_proplist != NULL) { |
8838 | fake_name.pl_prop = ZPOOL_PROP_NAME; | |
8839 | fake_name.pl_width = strlen(gettext("NAME")); | |
8840 | fake_name.pl_next = cb.cb_proplist; | |
8841 | cb.cb_proplist = &fake_name; | |
8842 | } | |
8843 | ||
d65e7381 | 8844 | ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, |
34dc7c2f BB |
8845 | get_callback, &cb); |
8846 | ||
8847 | if (cb.cb_proplist == &fake_name) | |
8848 | zprop_free_list(fake_name.pl_next); | |
8849 | else | |
8850 | zprop_free_list(cb.cb_proplist); | |
8851 | ||
8852 | return (ret); | |
8853 | } | |
8854 | ||
8855 | typedef struct set_cbdata { | |
8856 | char *cb_propname; | |
8857 | char *cb_value; | |
8858 | boolean_t cb_any_successful; | |
8859 | } set_cbdata_t; | |
8860 | ||
8861 | int | |
8862 | set_callback(zpool_handle_t *zhp, void *data) | |
8863 | { | |
8864 | int error; | |
8865 | set_cbdata_t *cb = (set_cbdata_t *)data; | |
8866 | ||
8867 | error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); | |
8868 | ||
8869 | if (!error) | |
8870 | cb->cb_any_successful = B_TRUE; | |
8871 | ||
8872 | return (error); | |
8873 | } | |
8874 | ||
8875 | int | |
8876 | zpool_do_set(int argc, char **argv) | |
8877 | { | |
8878 | set_cbdata_t cb = { 0 }; | |
8879 | int error; | |
8880 | ||
8881 | if (argc > 1 && argv[1][0] == '-') { | |
8882 | (void) fprintf(stderr, gettext("invalid option '%c'\n"), | |
8883 | argv[1][1]); | |
8884 | usage(B_FALSE); | |
8885 | } | |
8886 | ||
8887 | if (argc < 2) { | |
8888 | (void) fprintf(stderr, gettext("missing property=value " | |
8889 | "argument\n")); | |
8890 | usage(B_FALSE); | |
8891 | } | |
8892 | ||
8893 | if (argc < 3) { | |
8894 | (void) fprintf(stderr, gettext("missing pool name\n")); | |
8895 | usage(B_FALSE); | |
8896 | } | |
8897 | ||
8898 | if (argc > 3) { | |
8899 | (void) fprintf(stderr, gettext("too many pool names\n")); | |
8900 | usage(B_FALSE); | |
8901 | } | |
8902 | ||
8903 | cb.cb_propname = argv[1]; | |
8904 | cb.cb_value = strchr(cb.cb_propname, '='); | |
8905 | if (cb.cb_value == NULL) { | |
8906 | (void) fprintf(stderr, gettext("missing value in " | |
8907 | "property=value argument\n")); | |
8908 | usage(B_FALSE); | |
8909 | } | |
8910 | ||
8911 | *(cb.cb_value) = '\0'; | |
8912 | cb.cb_value++; | |
8913 | ||
8914 | error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, | |
8915 | set_callback, &cb); | |
8916 | ||
8917 | return (error); | |
8918 | } | |
8919 | ||
8920 | static int | |
8921 | find_command_idx(char *command, int *idx) | |
8922 | { | |
8923 | int i; | |
8924 | ||
8925 | for (i = 0; i < NCOMMAND; i++) { | |
8926 | if (command_table[i].name == NULL) | |
8927 | continue; | |
8928 | ||
8929 | if (strcmp(command, command_table[i].name) == 0) { | |
8930 | *idx = i; | |
8931 | return (0); | |
8932 | } | |
8933 | } | |
8934 | return (1); | |
8935 | } | |
8936 | ||
8937 | int | |
8938 | main(int argc, char **argv) | |
8939 | { | |
8e896579 | 8940 | int ret = 0; |
d4ed6673 | 8941 | int i = 0; |
34dc7c2f | 8942 | char *cmdname; |
edc05fdb | 8943 | char **newargv; |
34dc7c2f BB |
8944 | |
8945 | (void) setlocale(LC_ALL, ""); | |
8946 | (void) textdomain(TEXT_DOMAIN); | |
5b4136bd | 8947 | srand(time(NULL)); |
34dc7c2f | 8948 | |
34dc7c2f BB |
8949 | opterr = 0; |
8950 | ||
8951 | /* | |
8952 | * Make sure the user has specified some command. | |
8953 | */ | |
8954 | if (argc < 2) { | |
8955 | (void) fprintf(stderr, gettext("missing command\n")); | |
8956 | usage(B_FALSE); | |
8957 | } | |
8958 | ||
8959 | cmdname = argv[1]; | |
8960 | ||
8961 | /* | |
8962 | * Special case '-?' | |
8963 | */ | |
d1d7e268 | 8964 | if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0) |
34dc7c2f BB |
8965 | usage(B_TRUE); |
8966 | ||
65037d9b BB |
8967 | if ((g_zfs = libzfs_init()) == NULL) { |
8968 | (void) fprintf(stderr, "%s", libzfs_error_init(errno)); | |
9b020fd9 | 8969 | return (1); |
65037d9b | 8970 | } |
9b020fd9 BB |
8971 | |
8972 | libzfs_print_on_error(g_zfs, B_TRUE); | |
8973 | ||
6f1ffb06 | 8974 | zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); |
34dc7c2f | 8975 | |
edc05fdb D |
8976 | /* |
8977 | * Many commands modify input strings for string parsing reasons. | |
8978 | * We create a copy to protect the original argv. | |
8979 | */ | |
8980 | newargv = malloc((argc + 1) * sizeof (newargv[0])); | |
8981 | for (i = 0; i < argc; i++) | |
8982 | newargv[i] = strdup(argv[i]); | |
8983 | newargv[argc] = NULL; | |
8984 | ||
34dc7c2f BB |
8985 | /* |
8986 | * Run the appropriate command. | |
8987 | */ | |
8988 | if (find_command_idx(cmdname, &i) == 0) { | |
8989 | current_command = &command_table[i]; | |
edc05fdb | 8990 | ret = command_table[i].func(argc - 1, newargv + 1); |
34dc7c2f BB |
8991 | } else if (strchr(cmdname, '=')) { |
8992 | verify(find_command_idx("set", &i) == 0); | |
8993 | current_command = &command_table[i]; | |
edc05fdb | 8994 | ret = command_table[i].func(argc, newargv); |
34dc7c2f BB |
8995 | } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { |
8996 | /* | |
8997 | * 'freeze' is a vile debugging abomination, so we treat | |
8998 | * it as such. | |
8999 | */ | |
4807c0ba TC |
9000 | zfs_cmd_t zc = {"\0"}; |
9001 | ||
9002 | (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); | |
9003 | ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc); | |
9004 | if (ret != 0) { | |
9005 | (void) fprintf(stderr, | |
9006 | gettext("failed to freeze pool: %d\n"), errno); | |
9007 | ret = 1; | |
9008 | } | |
9009 | ||
9010 | log_history = 0; | |
34dc7c2f BB |
9011 | } else { |
9012 | (void) fprintf(stderr, gettext("unrecognized " | |
9013 | "command '%s'\n"), cmdname); | |
9014 | usage(B_FALSE); | |
d4ed6673 | 9015 | ret = 1; |
34dc7c2f BB |
9016 | } |
9017 | ||
edc05fdb D |
9018 | for (i = 0; i < argc; i++) |
9019 | free(newargv[i]); | |
9020 | free(newargv); | |
9021 | ||
6f1ffb06 MA |
9022 | if (ret == 0 && log_history) |
9023 | (void) zpool_log_history(g_zfs, history_str); | |
9024 | ||
34dc7c2f BB |
9025 | libzfs_fini(g_zfs); |
9026 | ||
9027 | /* | |
9028 | * The 'ZFS_ABORT' environment variable causes us to dump core on exit | |
9029 | * for the purposes of running ::findleaks. | |
9030 | */ | |
9031 | if (getenv("ZFS_ABORT") != NULL) { | |
9032 | (void) printf("dumping core by request\n"); | |
9033 | abort(); | |
9034 | } | |
9035 | ||
9036 | return (ret); | |
9037 | } |