1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5441d80aaSlling * Common Development and Distribution License (the "License").
6441d80aaSlling * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
2199653d4eSeschrock
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24ce72e614SYuri Pankov * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25ca0cc391SMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26e1d5e507SFrederik Wessels * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
279edf9ebdSPrasad Joshi * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
28b327cd3fSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29fa9e4066Sahrens */
30fa9e4066Sahrens
31fa9e4066Sahrens #include <assert.h>
32fa9e4066Sahrens #include <ctype.h>
33fa9e4066Sahrens #include <dirent.h>
34fa9e4066Sahrens #include <errno.h>
35fa9e4066Sahrens #include <fcntl.h>
36fa9e4066Sahrens #include <libgen.h>
37fa9e4066Sahrens #include <libintl.h>
38fa9e4066Sahrens #include <libuutil.h>
39fa9e4066Sahrens #include <locale.h>
40fa9e4066Sahrens #include <stdio.h>
41fa9e4066Sahrens #include <stdlib.h>
42fa9e4066Sahrens #include <string.h>
43fa9e4066Sahrens #include <strings.h>
44fa9e4066Sahrens #include <unistd.h>
45fa9e4066Sahrens #include <priv.h>
46ecd6cf80Smarks #include <pwd.h>
47ecd6cf80Smarks #include <zone.h>
484263d13fSGeorge Wilson #include <zfs_prop.h>
49b1b8ab34Slling #include <sys/fs/zfs.h>
50fa9e4066Sahrens #include <sys/stat.h>
5192e1c704SSimon Klinkert #include <syslog.h>
52fa9e4066Sahrens
53fa9e4066Sahrens #include <libzfs.h>
54fa9e4066Sahrens
55fa9e4066Sahrens #include "zpool_util.h"
56b7b97454Sperrin #include "zfs_comutil.h"
57ad135b5dSChristopher Siden #include "zfeature_common.h"
58fa9e4066Sahrens
5926fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
6026fd7700SKrishnendu Sadhukhan - Sun Microsystems
61fa9e4066Sahrens static int zpool_do_create(int, char **);
62fa9e4066Sahrens static int zpool_do_destroy(int, char **);
63fa9e4066Sahrens
64fa9e4066Sahrens static int zpool_do_add(int, char **);
6599653d4eSeschrock static int zpool_do_remove(int, char **);
66fa9e4066Sahrens
67fa9e4066Sahrens static int zpool_do_list(int, char **);
68fa9e4066Sahrens static int zpool_do_iostat(int, char **);
69fa9e4066Sahrens static int zpool_do_status(int, char **);
70fa9e4066Sahrens
71fa9e4066Sahrens static int zpool_do_online(int, char **);
72fa9e4066Sahrens static int zpool_do_offline(int, char **);
73ea8dc4b6Seschrock static int zpool_do_clear(int, char **);
744263d13fSGeorge Wilson static int zpool_do_reopen(int, char **);
75fa9e4066Sahrens
76e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **);
77e9103aaeSGarrett D'Amore
78fa9e4066Sahrens static int zpool_do_attach(int, char **);
79fa9e4066Sahrens static int zpool_do_detach(int, char **);
80fa9e4066Sahrens static int zpool_do_replace(int, char **);
811195e687SMark J Musante static int zpool_do_split(int, char **);
82fa9e4066Sahrens
83fa9e4066Sahrens static int zpool_do_scrub(int, char **);
84fa9e4066Sahrens
85fa9e4066Sahrens static int zpool_do_import(int, char **);
86fa9e4066Sahrens static int zpool_do_export(int, char **);
87fa9e4066Sahrens
88eaca9bbdSeschrock static int zpool_do_upgrade(int, char **);
89eaca9bbdSeschrock
9006eeb2adSek110237 static int zpool_do_history(int, char **);
9106eeb2adSek110237
92b1b8ab34Slling static int zpool_do_get(int, char **);
93b1b8ab34Slling static int zpool_do_set(int, char **);
94b1b8ab34Slling
95fa9e4066Sahrens /*
96fa9e4066Sahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
97fa9e4066Sahrens * debugging facilities.
98fa9e4066Sahrens */
9929ab75c9Srm160521
10029ab75c9Srm160521 #ifdef DEBUG
101fa9e4066Sahrens const char *
_umem_debug_init(void)10299653d4eSeschrock _umem_debug_init(void)
103fa9e4066Sahrens {
104fa9e4066Sahrens return ("default,verbose"); /* $UMEM_DEBUG setting */
105fa9e4066Sahrens }
106fa9e4066Sahrens
107fa9e4066Sahrens const char *
_umem_logging_init(void)108fa9e4066Sahrens _umem_logging_init(void)
109fa9e4066Sahrens {
110fa9e4066Sahrens return ("fail,contents"); /* $UMEM_LOGGING setting */
111fa9e4066Sahrens }
11229ab75c9Srm160521 #endif
113fa9e4066Sahrens
11465cd9f28Seschrock typedef enum {
11565cd9f28Seschrock HELP_ADD,
11665cd9f28Seschrock HELP_ATTACH,
117ea8dc4b6Seschrock HELP_CLEAR,
11865cd9f28Seschrock HELP_CREATE,
11965cd9f28Seschrock HELP_DESTROY,
12065cd9f28Seschrock HELP_DETACH,
12165cd9f28Seschrock HELP_EXPORT,
12206eeb2adSek110237 HELP_HISTORY,
12365cd9f28Seschrock HELP_IMPORT,
12465cd9f28Seschrock HELP_IOSTAT,
12565cd9f28Seschrock HELP_LIST,
12665cd9f28Seschrock HELP_OFFLINE,
12765cd9f28Seschrock HELP_ONLINE,
12865cd9f28Seschrock HELP_REPLACE,
12999653d4eSeschrock HELP_REMOVE,
13065cd9f28Seschrock HELP_SCRUB,
131eaca9bbdSeschrock HELP_STATUS,
132b1b8ab34Slling HELP_UPGRADE,
133b1b8ab34Slling HELP_GET,
1341195e687SMark J Musante HELP_SET,
135e9103aaeSGarrett D'Amore HELP_SPLIT,
1364263d13fSGeorge Wilson HELP_REGUID,
1374263d13fSGeorge Wilson HELP_REOPEN
13865cd9f28Seschrock } zpool_help_t;
13965cd9f28Seschrock
14065cd9f28Seschrock
141fa9e4066Sahrens typedef struct zpool_command {
142fa9e4066Sahrens const char *name;
143fa9e4066Sahrens int (*func)(int, char **);
14465cd9f28Seschrock zpool_help_t usage;
145fa9e4066Sahrens } zpool_command_t;
146fa9e4066Sahrens
147fa9e4066Sahrens /*
148fa9e4066Sahrens * Master command table. Each ZFS command has a name, associated function, and
149ea8dc4b6Seschrock * usage message. The usage messages need to be internationalized, so we have
150ea8dc4b6Seschrock * to have a function to return the usage message based on a command index.
15165cd9f28Seschrock *
15265cd9f28Seschrock * These commands are organized according to how they are displayed in the usage
15365cd9f28Seschrock * message. An empty command (one with a NULL name) indicates an empty line in
15465cd9f28Seschrock * the generic usage message.
155fa9e4066Sahrens */
156fa9e4066Sahrens static zpool_command_t command_table[] = {
15765cd9f28Seschrock { "create", zpool_do_create, HELP_CREATE },
15865cd9f28Seschrock { "destroy", zpool_do_destroy, HELP_DESTROY },
159fa9e4066Sahrens { NULL },
16065cd9f28Seschrock { "add", zpool_do_add, HELP_ADD },
16199653d4eSeschrock { "remove", zpool_do_remove, HELP_REMOVE },
162fa9e4066Sahrens { NULL },
16365cd9f28Seschrock { "list", zpool_do_list, HELP_LIST },
16465cd9f28Seschrock { "iostat", zpool_do_iostat, HELP_IOSTAT },
16565cd9f28Seschrock { "status", zpool_do_status, HELP_STATUS },
166fa9e4066Sahrens { NULL },
16765cd9f28Seschrock { "online", zpool_do_online, HELP_ONLINE },
16865cd9f28Seschrock { "offline", zpool_do_offline, HELP_OFFLINE },
169ea8dc4b6Seschrock { "clear", zpool_do_clear, HELP_CLEAR },
1704263d13fSGeorge Wilson { "reopen", zpool_do_reopen, HELP_REOPEN },
171fa9e4066Sahrens { NULL },
17265cd9f28Seschrock { "attach", zpool_do_attach, HELP_ATTACH },
17365cd9f28Seschrock { "detach", zpool_do_detach, HELP_DETACH },
17465cd9f28Seschrock { "replace", zpool_do_replace, HELP_REPLACE },
1751195e687SMark J Musante { "split", zpool_do_split, HELP_SPLIT },
176fa9e4066Sahrens { NULL },
17765cd9f28Seschrock { "scrub", zpool_do_scrub, HELP_SCRUB },
178fa9e4066Sahrens { NULL },
17965cd9f28Seschrock { "import", zpool_do_import, HELP_IMPORT },
18065cd9f28Seschrock { "export", zpool_do_export, HELP_EXPORT },
18106eeb2adSek110237 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
182e9103aaeSGarrett D'Amore { "reguid", zpool_do_reguid, HELP_REGUID },
18306eeb2adSek110237 { NULL },
184b1b8ab34Slling { "history", zpool_do_history, HELP_HISTORY },
185b1b8ab34Slling { "get", zpool_do_get, HELP_GET },
186b1b8ab34Slling { "set", zpool_do_set, HELP_SET },
187fa9e4066Sahrens };
188fa9e4066Sahrens
189fa9e4066Sahrens #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
190fa9e4066Sahrens
1914445fffbSMatthew Ahrens static zpool_command_t *current_command;
1922a6b87f0Sek110237 static char history_str[HIS_MAX_RECORD_LEN];
1934445fffbSMatthew Ahrens static boolean_t log_history = B_TRUE;
19426fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
19526fd7700SKrishnendu Sadhukhan - Sun Microsystems
19665cd9f28Seschrock static const char *
get_usage(zpool_help_t idx)1979a686fbcSPaul Dagnelie get_usage(zpool_help_t idx)
1989a686fbcSPaul Dagnelie {
19965cd9f28Seschrock switch (idx) {
20065cd9f28Seschrock case HELP_ADD:
20165cd9f28Seschrock return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
20265cd9f28Seschrock case HELP_ATTACH:
20365cd9f28Seschrock return (gettext("\tattach [-f] <pool> <device> "
204e45ce728Sahrens "<new-device>\n"));
205ea8dc4b6Seschrock case HELP_CLEAR:
206468c413aSTim Haley return (gettext("\tclear [-nF] <pool> [device]\n"));
20765cd9f28Seschrock case HELP_CREATE:
208ad135b5dSChristopher Siden return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
2090a48a24eStimh "\t [-O file-system-property=value] ... \n"
210990b4856Slling "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
21165cd9f28Seschrock case HELP_DESTROY:
21265cd9f28Seschrock return (gettext("\tdestroy [-f] <pool>\n"));
21365cd9f28Seschrock case HELP_DETACH:
21465cd9f28Seschrock return (gettext("\tdetach <pool> <device>\n"));
21565cd9f28Seschrock case HELP_EXPORT:
21665cd9f28Seschrock return (gettext("\texport [-f] <pool> ...\n"));
21706eeb2adSek110237 case HELP_HISTORY:
218ecd6cf80Smarks return (gettext("\thistory [-il] [<pool>] ...\n"));
21965cd9f28Seschrock case HELP_IMPORT:
2204c58d714Sdarrenm return (gettext("\timport [-d dir] [-D]\n"
221f9af39baSGeorge Wilson "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
2222f8aaab3Seschrock "\timport [-o mntopts] [-o property=value] ... \n"
223f9af39baSGeorge Wilson "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
224f9af39baSGeorge Wilson "[-R root] [-F [-n]] -a\n"
2252f8aaab3Seschrock "\timport [-o mntopts] [-o property=value] ... \n"
226f9af39baSGeorge Wilson "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
227f9af39baSGeorge Wilson "[-R root] [-F [-n]]\n"
228f9af39baSGeorge Wilson "\t <pool | id> [newpool]\n"));
22965cd9f28Seschrock case HELP_IOSTAT:
23026fd7700SKrishnendu Sadhukhan - Sun Microsystems return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
23165cd9f28Seschrock "[count]]\n"));
23265cd9f28Seschrock case HELP_LIST:
233c58b3526SAdam Stevko return (gettext("\tlist [-Hp] [-o property[,...]] "
2343f9d6ad7SLin Ling "[-T d|u] [pool] ... [interval [count]]\n"));
23565cd9f28Seschrock case HELP_OFFLINE:
236441d80aaSlling return (gettext("\toffline [-t] <pool> <device> ...\n"));
23765cd9f28Seschrock case HELP_ONLINE:
238441d80aaSlling return (gettext("\tonline <pool> <device> ...\n"));
23965cd9f28Seschrock case HELP_REPLACE:
24065cd9f28Seschrock return (gettext("\treplace [-f] <pool> <device> "
241e45ce728Sahrens "[new-device]\n"));
24299653d4eSeschrock case HELP_REMOVE:
243fa94a07fSbrendan return (gettext("\tremove <pool> <device> ...\n"));
2444263d13fSGeorge Wilson case HELP_REOPEN:
24531d7e8faSGeorge Wilson return (gettext("\treopen <pool>\n"));
24665cd9f28Seschrock case HELP_SCRUB:
24765cd9f28Seschrock return (gettext("\tscrub [-s] <pool> ...\n"));
24865cd9f28Seschrock case HELP_STATUS:
2493f9d6ad7SLin Ling return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
2503f9d6ad7SLin Ling "[count]]\n"));
251eaca9bbdSeschrock case HELP_UPGRADE:
252eaca9bbdSeschrock return (gettext("\tupgrade\n"
253eaca9bbdSeschrock "\tupgrade -v\n"
254990b4856Slling "\tupgrade [-V version] <-a | pool ...>\n"));
255b1b8ab34Slling case HELP_GET:
256c58b3526SAdam Stevko return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
257c58b3526SAdam Stevko "<\"all\" | property[,...]> <pool> ...\n"));
258b1b8ab34Slling case HELP_SET:
259b1b8ab34Slling return (gettext("\tset <property=value> <pool> \n"));
2601195e687SMark J Musante case HELP_SPLIT:
2611195e687SMark J Musante return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
2621195e687SMark J Musante "\t [-o property=value] <pool> <newpool> "
2631195e687SMark J Musante "[<device> ...]\n"));
264e9103aaeSGarrett D'Amore case HELP_REGUID:
265e9103aaeSGarrett D'Amore return (gettext("\treguid <pool>\n"));
26665cd9f28Seschrock }
26765cd9f28Seschrock
26865cd9f28Seschrock abort();
26965cd9f28Seschrock /* NOTREACHED */
27065cd9f28Seschrock }
27165cd9f28Seschrock
272fa9e4066Sahrens
273fa9e4066Sahrens /*
274b1b8ab34Slling * Callback routine that will print out a pool property value.
275b1b8ab34Slling */
276990b4856Slling static int
print_prop_cb(int prop,void * cb)277990b4856Slling print_prop_cb(int prop, void *cb)
278b1b8ab34Slling {
279b1b8ab34Slling FILE *fp = cb;
280b1b8ab34Slling
281b24ab676SJeff Bonwick (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
282b1b8ab34Slling
283990b4856Slling if (zpool_prop_readonly(prop))
284990b4856Slling (void) fprintf(fp, " NO ");
285990b4856Slling else
286990b4856Slling (void) fprintf(fp, " YES ");
287990b4856Slling
288b1b8ab34Slling if (zpool_prop_values(prop) == NULL)
289b1b8ab34Slling (void) fprintf(fp, "-\n");
290b1b8ab34Slling else
291b1b8ab34Slling (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
292b1b8ab34Slling
293990b4856Slling return (ZPROP_CONT);
294b1b8ab34Slling }
295b1b8ab34Slling
296b1b8ab34Slling /*
297fa9e4066Sahrens * Display usage message. If we're inside a command, display only the usage for
298fa9e4066Sahrens * that command. Otherwise, iterate over the entire command table and display
299fa9e4066Sahrens * a complete usage message.
300fa9e4066Sahrens */
301fa9e4066Sahrens void
usage(boolean_t requested)30299653d4eSeschrock usage(boolean_t requested)
303fa9e4066Sahrens {
304fa9e4066Sahrens FILE *fp = requested ? stdout : stderr;
305fa9e4066Sahrens
306fa9e4066Sahrens if (current_command == NULL) {
307fa9e4066Sahrens int i;
308fa9e4066Sahrens
309fa9e4066Sahrens (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
310fa9e4066Sahrens (void) fprintf(fp,
311fa9e4066Sahrens gettext("where 'command' is one of the following:\n\n"));
312fa9e4066Sahrens
313fa9e4066Sahrens for (i = 0; i < NCOMMAND; i++) {
314fa9e4066Sahrens if (command_table[i].name == NULL)
315fa9e4066Sahrens (void) fprintf(fp, "\n");
316fa9e4066Sahrens else
317fa9e4066Sahrens (void) fprintf(fp, "%s",
31865cd9f28Seschrock get_usage(command_table[i].usage));
319fa9e4066Sahrens }
320fa9e4066Sahrens } else {
321fa9e4066Sahrens (void) fprintf(fp, gettext("usage:\n"));
32265cd9f28Seschrock (void) fprintf(fp, "%s", get_usage(current_command->usage));
323fa9e4066Sahrens }
324fa9e4066Sahrens
325b1b8ab34Slling if (current_command != NULL &&
326b1b8ab34Slling ((strcmp(current_command->name, "set") == 0) ||
327990b4856Slling (strcmp(current_command->name, "get") == 0) ||
328990b4856Slling (strcmp(current_command->name, "list") == 0))) {
329b1b8ab34Slling
330b1b8ab34Slling (void) fprintf(fp,
331b1b8ab34Slling gettext("\nthe following properties are supported:\n"));
332b1b8ab34Slling
333b24ab676SJeff Bonwick (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
334990b4856Slling "PROPERTY", "EDIT", "VALUES");
335b1b8ab34Slling
336b1b8ab34Slling /* Iterate over all properties */
337990b4856Slling (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
338990b4856Slling ZFS_TYPE_POOL);
339ad135b5dSChristopher Siden
340ad135b5dSChristopher Siden (void) fprintf(fp, "\t%-15s ", "feature@...");
341ad135b5dSChristopher Siden (void) fprintf(fp, "YES disabled | enabled | active\n");
342ad135b5dSChristopher Siden
343ad135b5dSChristopher Siden (void) fprintf(fp, gettext("\nThe feature@ properties must be "
344ad135b5dSChristopher Siden "appended with a feature name.\nSee zpool-features(5).\n"));
345b1b8ab34Slling }
346b1b8ab34Slling
347e9dbad6fSeschrock /*
348e9dbad6fSeschrock * See comments at end of main().
349e9dbad6fSeschrock */
350e9dbad6fSeschrock if (getenv("ZFS_ABORT") != NULL) {
351e9dbad6fSeschrock (void) printf("dumping core by request\n");
352e9dbad6fSeschrock abort();
353e9dbad6fSeschrock }
354e9dbad6fSeschrock
355fa9e4066Sahrens exit(requested ? 0 : 2);
356fa9e4066Sahrens }
357fa9e4066Sahrens
358fa9e4066Sahrens void
print_vdev_tree(zpool_handle_t * zhp,const char * name,nvlist_t * nv,int indent,boolean_t print_logs)3598654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
3608654d025Sperrin boolean_t print_logs)
361fa9e4066Sahrens {
362fa9e4066Sahrens nvlist_t **child;
363fa9e4066Sahrens uint_t c, children;
364afefbcddSeschrock char *vname;
365fa9e4066Sahrens
366fa9e4066Sahrens if (name != NULL)
367fa9e4066Sahrens (void) printf("\t%*s%s\n", indent, "", name);
368fa9e4066Sahrens
369fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
370fa9e4066Sahrens &child, &children) != 0)
371fa9e4066Sahrens return;
372fa9e4066Sahrens
373afefbcddSeschrock for (c = 0; c < children; c++) {
3748654d025Sperrin uint64_t is_log = B_FALSE;
3758654d025Sperrin
3768654d025Sperrin (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3778654d025Sperrin &is_log);
3788654d025Sperrin if ((is_log && !print_logs) || (!is_log && print_logs))
3798654d025Sperrin continue;
3808654d025Sperrin
38188ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3828654d025Sperrin print_vdev_tree(zhp, vname, child[c], indent + 2,
3838654d025Sperrin B_FALSE);
384afefbcddSeschrock free(vname);
385afefbcddSeschrock }
386fa9e4066Sahrens }
387fa9e4066Sahrens
38857221772SChristopher Siden static boolean_t
prop_list_contains_feature(nvlist_t * proplist)38957221772SChristopher Siden prop_list_contains_feature(nvlist_t *proplist)
39057221772SChristopher Siden {
39157221772SChristopher Siden nvpair_t *nvp;
39257221772SChristopher Siden for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
39357221772SChristopher Siden nvp = nvlist_next_nvpair(proplist, nvp)) {
39457221772SChristopher Siden if (zpool_prop_feature(nvpair_name(nvp)))
39557221772SChristopher Siden return (B_TRUE);
39657221772SChristopher Siden }
39757221772SChristopher Siden return (B_FALSE);
39857221772SChristopher Siden }
39957221772SChristopher Siden
400fa9e4066Sahrens /*
401990b4856Slling * Add a property pair (name, string-value) into a property nvlist.
402990b4856Slling */
403990b4856Slling static int
add_prop_list(const char * propname,char * propval,nvlist_t ** props,boolean_t poolprop)4040a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props,
4050a48a24eStimh boolean_t poolprop)
406990b4856Slling {
4070a48a24eStimh zpool_prop_t prop = ZPROP_INVAL;
4080a48a24eStimh zfs_prop_t fprop;
409990b4856Slling nvlist_t *proplist;
4100a48a24eStimh const char *normnm;
4110a48a24eStimh char *strval;
412990b4856Slling
413990b4856Slling if (*props == NULL &&
414990b4856Slling nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
415990b4856Slling (void) fprintf(stderr,
416990b4856Slling gettext("internal error: out of memory\n"));
417990b4856Slling return (1);
418990b4856Slling }
419990b4856Slling
420990b4856Slling proplist = *props;
421990b4856Slling
4220a48a24eStimh if (poolprop) {
42357221772SChristopher Siden const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
42457221772SChristopher Siden
425ad135b5dSChristopher Siden if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
426ad135b5dSChristopher Siden !zpool_prop_feature(propname)) {
427990b4856Slling (void) fprintf(stderr, gettext("property '%s' is "
428990b4856Slling "not a valid pool property\n"), propname);
429990b4856Slling return (2);
430990b4856Slling }
43157221772SChristopher Siden
43257221772SChristopher Siden /*
43357221772SChristopher Siden * feature@ properties and version should not be specified
43457221772SChristopher Siden * at the same time.
43557221772SChristopher Siden */
43657221772SChristopher Siden if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
43757221772SChristopher Siden nvlist_exists(proplist, vname)) ||
43857221772SChristopher Siden (prop == ZPOOL_PROP_VERSION &&
43957221772SChristopher Siden prop_list_contains_feature(proplist))) {
44057221772SChristopher Siden (void) fprintf(stderr, gettext("'feature@' and "
44157221772SChristopher Siden "'version' properties cannot be specified "
44257221772SChristopher Siden "together\n"));
44357221772SChristopher Siden return (2);
44457221772SChristopher Siden }
44557221772SChristopher Siden
44657221772SChristopher Siden
447ad135b5dSChristopher Siden if (zpool_prop_feature(propname))
448ad135b5dSChristopher Siden normnm = propname;
449ad135b5dSChristopher Siden else
4500a48a24eStimh normnm = zpool_prop_to_name(prop);
4510a48a24eStimh } else {
45214843421SMatthew Ahrens if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
4530a48a24eStimh normnm = zfs_prop_to_name(fprop);
45414843421SMatthew Ahrens } else {
45514843421SMatthew Ahrens normnm = propname;
45614843421SMatthew Ahrens }
4570a48a24eStimh }
458990b4856Slling
4590a48a24eStimh if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
4600a48a24eStimh prop != ZPOOL_PROP_CACHEFILE) {
461990b4856Slling (void) fprintf(stderr, gettext("property '%s' "
462990b4856Slling "specified multiple times\n"), propname);
463990b4856Slling return (2);
464990b4856Slling }
465990b4856Slling
4660a48a24eStimh if (nvlist_add_string(proplist, normnm, propval) != 0) {
467990b4856Slling (void) fprintf(stderr, gettext("internal "
468990b4856Slling "error: out of memory\n"));
469990b4856Slling return (1);
470990b4856Slling }
471990b4856Slling
472990b4856Slling return (0);
473990b4856Slling }
474990b4856Slling
475990b4856Slling /*
476fa9e4066Sahrens * zpool add [-fn] <pool> <vdev> ...
477fa9e4066Sahrens *
478fa9e4066Sahrens * -f Force addition of devices, even if they appear in use
479fa9e4066Sahrens * -n Do not add the devices, but display the resulting layout if
480fa9e4066Sahrens * they were to be added.
481fa9e4066Sahrens *
482fa9e4066Sahrens * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
483fa9e4066Sahrens * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
484fa9e4066Sahrens * libzfs.
485fa9e4066Sahrens */
486fa9e4066Sahrens int
zpool_do_add(int argc,char ** argv)487fa9e4066Sahrens zpool_do_add(int argc, char **argv)
488fa9e4066Sahrens {
48999653d4eSeschrock boolean_t force = B_FALSE;
49099653d4eSeschrock boolean_t dryrun = B_FALSE;
491fa9e4066Sahrens int c;
492fa9e4066Sahrens nvlist_t *nvroot;
493fa9e4066Sahrens char *poolname;
494fa9e4066Sahrens int ret;
495fa9e4066Sahrens zpool_handle_t *zhp;
496fa9e4066Sahrens nvlist_t *config;
497fa9e4066Sahrens
498fa9e4066Sahrens /* check options */
499fa9e4066Sahrens while ((c = getopt(argc, argv, "fn")) != -1) {
500fa9e4066Sahrens switch (c) {
501fa9e4066Sahrens case 'f':
50299653d4eSeschrock force = B_TRUE;
503fa9e4066Sahrens break;
504fa9e4066Sahrens case 'n':
50599653d4eSeschrock dryrun = B_TRUE;
506fa9e4066Sahrens break;
507fa9e4066Sahrens case '?':
508fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
509fa9e4066Sahrens optopt);
51099653d4eSeschrock usage(B_FALSE);
511fa9e4066Sahrens }
512fa9e4066Sahrens }
513fa9e4066Sahrens
514fa9e4066Sahrens argc -= optind;
515fa9e4066Sahrens argv += optind;
516fa9e4066Sahrens
517fa9e4066Sahrens /* get pool name and check number of arguments */
518fa9e4066Sahrens if (argc < 1) {
519fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n"));
52099653d4eSeschrock usage(B_FALSE);
521fa9e4066Sahrens }
522fa9e4066Sahrens if (argc < 2) {
523fa9e4066Sahrens (void) fprintf(stderr, gettext("missing vdev specification\n"));
52499653d4eSeschrock usage(B_FALSE);
525fa9e4066Sahrens }
526fa9e4066Sahrens
527fa9e4066Sahrens poolname = argv[0];
528fa9e4066Sahrens
529fa9e4066Sahrens argc--;
530fa9e4066Sahrens argv++;
531fa9e4066Sahrens
53299653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
533fa9e4066Sahrens return (1);
534fa9e4066Sahrens
535088e9d47Seschrock if ((config = zpool_get_config(zhp, NULL)) == NULL) {
536fa9e4066Sahrens (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
537fa9e4066Sahrens poolname);
538fa9e4066Sahrens zpool_close(zhp);
539fa9e4066Sahrens return (1);
540fa9e4066Sahrens }
541fa9e4066Sahrens
542fa9e4066Sahrens /* pass off to get_vdev_spec for processing */
543705040edSEric Taylor nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
544705040edSEric Taylor argc, argv);
545fa9e4066Sahrens if (nvroot == NULL) {
546fa9e4066Sahrens zpool_close(zhp);
547fa9e4066Sahrens return (1);
548fa9e4066Sahrens }
549fa9e4066Sahrens
550fa9e4066Sahrens if (dryrun) {
551fa9e4066Sahrens nvlist_t *poolnvroot;
552fa9e4066Sahrens
553fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
554fa9e4066Sahrens &poolnvroot) == 0);
555fa9e4066Sahrens
556fa9e4066Sahrens (void) printf(gettext("would update '%s' to the following "
557fa9e4066Sahrens "configuration:\n"), zpool_get_name(zhp));
558fa9e4066Sahrens
5598654d025Sperrin /* print original main pool and new tree */
5608654d025Sperrin print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
5618654d025Sperrin print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
5628654d025Sperrin
5638654d025Sperrin /* Do the same for the logs */
5648654d025Sperrin if (num_logs(poolnvroot) > 0) {
5658654d025Sperrin print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
5668654d025Sperrin print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
5678654d025Sperrin } else if (num_logs(nvroot) > 0) {
5688654d025Sperrin print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
5698654d025Sperrin }
570fa9e4066Sahrens
571fa9e4066Sahrens ret = 0;
572fa9e4066Sahrens } else {
573fa9e4066Sahrens ret = (zpool_add(zhp, nvroot) != 0);
574fa9e4066Sahrens }
575fa9e4066Sahrens
57699653d4eSeschrock nvlist_free(nvroot);
57799653d4eSeschrock zpool_close(zhp);
57899653d4eSeschrock
57999653d4eSeschrock return (ret);
58099653d4eSeschrock }
58199653d4eSeschrock
58299653d4eSeschrock /*
583fa94a07fSbrendan * zpool remove <pool> <vdev> ...
58499653d4eSeschrock *
5853f9d6ad7SLin Ling * Removes the given vdev from the pool. Currently, this supports removing
5863f9d6ad7SLin Ling * spares, cache, and log devices from the pool.
58799653d4eSeschrock */
58899653d4eSeschrock int
zpool_do_remove(int argc,char ** argv)58999653d4eSeschrock zpool_do_remove(int argc, char **argv)
59099653d4eSeschrock {
59199653d4eSeschrock char *poolname;
592fa94a07fSbrendan int i, ret = 0;
59399653d4eSeschrock zpool_handle_t *zhp;
59499653d4eSeschrock
59599653d4eSeschrock argc--;
59699653d4eSeschrock argv++;
59799653d4eSeschrock
59899653d4eSeschrock /* get pool name and check number of arguments */
59999653d4eSeschrock if (argc < 1) {
60099653d4eSeschrock (void) fprintf(stderr, gettext("missing pool name argument\n"));
60199653d4eSeschrock usage(B_FALSE);
60299653d4eSeschrock }
60399653d4eSeschrock if (argc < 2) {
60499653d4eSeschrock (void) fprintf(stderr, gettext("missing device\n"));
60599653d4eSeschrock usage(B_FALSE);
60699653d4eSeschrock }
60799653d4eSeschrock
60899653d4eSeschrock poolname = argv[0];
60999653d4eSeschrock
61099653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
61199653d4eSeschrock return (1);
61299653d4eSeschrock
613fa94a07fSbrendan for (i = 1; i < argc; i++) {
614fa94a07fSbrendan if (zpool_vdev_remove(zhp, argv[i]) != 0)
615fa94a07fSbrendan ret = 1;
616fa94a07fSbrendan }
61799653d4eSeschrock
618fa9e4066Sahrens return (ret);
619fa9e4066Sahrens }
620fa9e4066Sahrens
621fa9e4066Sahrens /*
622ad135b5dSChristopher Siden * zpool create [-fnd] [-o property=value] ...
6230a48a24eStimh * [-O file-system-property=value] ...
6240a48a24eStimh * [-R root] [-m mountpoint] <pool> <dev> ...
625fa9e4066Sahrens *
626fa9e4066Sahrens * -f Force creation, even if devices appear in use
627fa9e4066Sahrens * -n Do not create the pool, but display the resulting layout if it
628fa9e4066Sahrens * were to be created.
629fa9e4066Sahrens * -R Create a pool under an alternate root
630fa9e4066Sahrens * -m Set default mountpoint for the root dataset. By default it's
631fa9e4066Sahrens * '/<pool>'
632990b4856Slling * -o Set property=value.
633ad135b5dSChristopher Siden * -d Don't automatically enable all supported pool features
634ad135b5dSChristopher Siden * (individual features can be enabled with -o).
6350a48a24eStimh * -O Set fsproperty=value in the pool's root file system
636fa9e4066Sahrens *
637b1b8ab34Slling * Creates the named pool according to the given vdev specification. The
638fa9e4066Sahrens * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
639fa9e4066Sahrens * we get the nvlist back from get_vdev_spec(), we either print out the contents
640fa9e4066Sahrens * (if '-n' was specified), or pass it to libzfs to do the creation.
641fa9e4066Sahrens */
642fa9e4066Sahrens int
zpool_do_create(int argc,char ** argv)643fa9e4066Sahrens zpool_do_create(int argc, char **argv)
644fa9e4066Sahrens {
64599653d4eSeschrock boolean_t force = B_FALSE;
64699653d4eSeschrock boolean_t dryrun = B_FALSE;
647ad135b5dSChristopher Siden boolean_t enable_all_pool_feat = B_TRUE;
648fa9e4066Sahrens int c;
649990b4856Slling nvlist_t *nvroot = NULL;
650fa9e4066Sahrens char *poolname;
651990b4856Slling int ret = 1;
652fa9e4066Sahrens char *altroot = NULL;
653fa9e4066Sahrens char *mountpoint = NULL;
6540a48a24eStimh nvlist_t *fsprops = NULL;
655990b4856Slling nvlist_t *props = NULL;
6562f8aaab3Seschrock char *propval;
657fa9e4066Sahrens
658fa9e4066Sahrens /* check options */
659ad135b5dSChristopher Siden while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
660fa9e4066Sahrens switch (c) {
661fa9e4066Sahrens case 'f':
66299653d4eSeschrock force = B_TRUE;
663fa9e4066Sahrens break;
664fa9e4066Sahrens case 'n':
66599653d4eSeschrock dryrun = B_TRUE;
666fa9e4066Sahrens break;
667ad135b5dSChristopher Siden case 'd':
668ad135b5dSChristopher Siden enable_all_pool_feat = B_FALSE;
669ad135b5dSChristopher Siden break;
670fa9e4066Sahrens case 'R':
671fa9e4066Sahrens altroot = optarg;
672990b4856Slling if (add_prop_list(zpool_prop_to_name(
6730a48a24eStimh ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
674990b4856Slling goto errout;
6752f8aaab3Seschrock if (nvlist_lookup_string(props,
6762f8aaab3Seschrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
6772f8aaab3Seschrock &propval) == 0)
6782f8aaab3Seschrock break;
679990b4856Slling if (add_prop_list(zpool_prop_to_name(
6800a48a24eStimh ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
681990b4856Slling goto errout;
682fa9e4066Sahrens break;
683fa9e4066Sahrens case 'm':
6848b713775SWill Andrews /* Equivalent to -O mountpoint=optarg */
685fa9e4066Sahrens mountpoint = optarg;
686fa9e4066Sahrens break;
687990b4856Slling case 'o':
688990b4856Slling if ((propval = strchr(optarg, '=')) == NULL) {
689990b4856Slling (void) fprintf(stderr, gettext("missing "
690990b4856Slling "'=' for -o option\n"));
691990b4856Slling goto errout;
692990b4856Slling }
693990b4856Slling *propval = '\0';
694990b4856Slling propval++;
695990b4856Slling
6960a48a24eStimh if (add_prop_list(optarg, propval, &props, B_TRUE))
6970a48a24eStimh goto errout;
698ad135b5dSChristopher Siden
699ad135b5dSChristopher Siden /*
700ad135b5dSChristopher Siden * If the user is creating a pool that doesn't support
701ad135b5dSChristopher Siden * feature flags, don't enable any features.
702ad135b5dSChristopher Siden */
703ad135b5dSChristopher Siden if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
704ad135b5dSChristopher Siden char *end;
705ad135b5dSChristopher Siden u_longlong_t ver;
706ad135b5dSChristopher Siden
707ad135b5dSChristopher Siden ver = strtoull(propval, &end, 10);
708ad135b5dSChristopher Siden if (*end == '\0' &&
709ad135b5dSChristopher Siden ver < SPA_VERSION_FEATURES) {
710ad135b5dSChristopher Siden enable_all_pool_feat = B_FALSE;
711ad135b5dSChristopher Siden }
712ad135b5dSChristopher Siden }
713c423721fSXin Li if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
714c423721fSXin Li altroot = propval;
7150a48a24eStimh break;
7160a48a24eStimh case 'O':
7170a48a24eStimh if ((propval = strchr(optarg, '=')) == NULL) {
7180a48a24eStimh (void) fprintf(stderr, gettext("missing "
7190a48a24eStimh "'=' for -O option\n"));
7200a48a24eStimh goto errout;
7210a48a24eStimh }
7220a48a24eStimh *propval = '\0';
7230a48a24eStimh propval++;
7240a48a24eStimh
7258b713775SWill Andrews /*
7268b713775SWill Andrews * Mountpoints are checked and then added later.
7278b713775SWill Andrews * Uniquely among properties, they can be specified
7288b713775SWill Andrews * more than once, to avoid conflict with -m.
7298b713775SWill Andrews */
7308b713775SWill Andrews if (0 == strcmp(optarg,
7318b713775SWill Andrews zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
7328b713775SWill Andrews mountpoint = propval;
7338b713775SWill Andrews } else if (add_prop_list(optarg, propval, &fsprops,
7348b713775SWill Andrews B_FALSE)) {
735990b4856Slling goto errout;
7368b713775SWill Andrews }
737990b4856Slling break;
738fa9e4066Sahrens case ':':
739fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for "
740fa9e4066Sahrens "'%c' option\n"), optopt);
741990b4856Slling goto badusage;
742fa9e4066Sahrens case '?':
743fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
744fa9e4066Sahrens optopt);
745990b4856Slling goto badusage;
746fa9e4066Sahrens }
747fa9e4066Sahrens }
748fa9e4066Sahrens
749fa9e4066Sahrens argc -= optind;
750fa9e4066Sahrens argv += optind;
751fa9e4066Sahrens
752fa9e4066Sahrens /* get pool name and check number of arguments */
753fa9e4066Sahrens if (argc < 1) {
754fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n"));
755990b4856Slling goto badusage;
756fa9e4066Sahrens }
757fa9e4066Sahrens if (argc < 2) {
758fa9e4066Sahrens (void) fprintf(stderr, gettext("missing vdev specification\n"));
759990b4856Slling goto badusage;
760fa9e4066Sahrens }
761fa9e4066Sahrens
762fa9e4066Sahrens poolname = argv[0];
763fa9e4066Sahrens
764fa9e4066Sahrens /*
765fa9e4066Sahrens * As a special case, check for use of '/' in the name, and direct the
766fa9e4066Sahrens * user to use 'zfs create' instead.
767fa9e4066Sahrens */
768fa9e4066Sahrens if (strchr(poolname, '/') != NULL) {
769fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot create '%s': invalid "
770fa9e4066Sahrens "character '/' in pool name\n"), poolname);
771fa9e4066Sahrens (void) fprintf(stderr, gettext("use 'zfs create' to "
772fa9e4066Sahrens "create a dataset\n"));
773990b4856Slling goto errout;
774fa9e4066Sahrens }
775fa9e4066Sahrens
776fa9e4066Sahrens /* pass off to get_vdev_spec for bulk processing */
777705040edSEric Taylor nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
778705040edSEric Taylor argc - 1, argv + 1);
779fa9e4066Sahrens if (nvroot == NULL)
7800a48a24eStimh goto errout;
781fa9e4066Sahrens
78299653d4eSeschrock /* make_root_vdev() allows 0 toplevel children if there are spares */
783b7b97454Sperrin if (!zfs_allocatable_devs(nvroot)) {
78499653d4eSeschrock (void) fprintf(stderr, gettext("invalid vdev "
78599653d4eSeschrock "specification: at least one toplevel vdev must be "
78699653d4eSeschrock "specified\n"));
787990b4856Slling goto errout;
78899653d4eSeschrock }
78999653d4eSeschrock
790fa9e4066Sahrens if (altroot != NULL && altroot[0] != '/') {
791fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid alternate root '%s': "
792e9dbad6fSeschrock "must be an absolute path\n"), altroot);
793990b4856Slling goto errout;
794fa9e4066Sahrens }
795fa9e4066Sahrens
796fa9e4066Sahrens /*
797fa9e4066Sahrens * Check the validity of the mountpoint and direct the user to use the
798fa9e4066Sahrens * '-m' mountpoint option if it looks like its in use.
799fa9e4066Sahrens */
800fa9e4066Sahrens if (mountpoint == NULL ||
801fa9e4066Sahrens (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
802fa9e4066Sahrens strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
803fa9e4066Sahrens char buf[MAXPATHLEN];
80411022c7cStimh DIR *dirp;
805fa9e4066Sahrens
806fa9e4066Sahrens if (mountpoint && mountpoint[0] != '/') {
807fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid mountpoint "
808fa9e4066Sahrens "'%s': must be an absolute path, 'legacy', or "
809fa9e4066Sahrens "'none'\n"), mountpoint);
810990b4856Slling goto errout;
811fa9e4066Sahrens }
812fa9e4066Sahrens
813fa9e4066Sahrens if (mountpoint == NULL) {
814fa9e4066Sahrens if (altroot != NULL)
815fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s/%s",
816fa9e4066Sahrens altroot, poolname);
817fa9e4066Sahrens else
818fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "/%s",
819fa9e4066Sahrens poolname);
820fa9e4066Sahrens } else {
821fa9e4066Sahrens if (altroot != NULL)
822fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s%s",
823fa9e4066Sahrens altroot, mountpoint);
824fa9e4066Sahrens else
825fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s",
826fa9e4066Sahrens mountpoint);
827fa9e4066Sahrens }
828fa9e4066Sahrens
82911022c7cStimh if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
83011022c7cStimh (void) fprintf(stderr, gettext("mountpoint '%s' : "
83111022c7cStimh "%s\n"), buf, strerror(errno));
832fa9e4066Sahrens (void) fprintf(stderr, gettext("use '-m' "
833fa9e4066Sahrens "option to provide a different default\n"));
834990b4856Slling goto errout;
83511022c7cStimh } else if (dirp) {
83611022c7cStimh int count = 0;
83711022c7cStimh
83811022c7cStimh while (count < 3 && readdir(dirp) != NULL)
83911022c7cStimh count++;
84011022c7cStimh (void) closedir(dirp);
84111022c7cStimh
84211022c7cStimh if (count > 2) {
84311022c7cStimh (void) fprintf(stderr, gettext("mountpoint "
84411022c7cStimh "'%s' exists and is not empty\n"), buf);
84511022c7cStimh (void) fprintf(stderr, gettext("use '-m' "
84611022c7cStimh "option to provide a "
84711022c7cStimh "different default\n"));
84811022c7cStimh goto errout;
84911022c7cStimh }
850fa9e4066Sahrens }
851fa9e4066Sahrens }
852fa9e4066Sahrens
8538b713775SWill Andrews /*
8548b713775SWill Andrews * Now that the mountpoint's validity has been checked, ensure that
8558b713775SWill Andrews * the property is set appropriately prior to creating the pool.
8568b713775SWill Andrews */
8578b713775SWill Andrews if (mountpoint != NULL) {
8588b713775SWill Andrews ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
8598b713775SWill Andrews mountpoint, &fsprops, B_FALSE);
8608b713775SWill Andrews if (ret != 0)
8618b713775SWill Andrews goto errout;
8628b713775SWill Andrews }
8638b713775SWill Andrews
8648b713775SWill Andrews ret = 1;
865fa9e4066Sahrens if (dryrun) {
866fa9e4066Sahrens /*
867fa9e4066Sahrens * For a dry run invocation, print out a basic message and run
868fa9e4066Sahrens * through all the vdevs in the list and print out in an
869fa9e4066Sahrens * appropriate hierarchy.
870fa9e4066Sahrens */
871fa9e4066Sahrens (void) printf(gettext("would create '%s' with the "
872fa9e4066Sahrens "following layout:\n\n"), poolname);
873fa9e4066Sahrens
8748654d025Sperrin print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
8758654d025Sperrin if (num_logs(nvroot) > 0)
8768654d025Sperrin print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
877fa9e4066Sahrens
878fa9e4066Sahrens ret = 0;
879fa9e4066Sahrens } else {
880fa9e4066Sahrens /*
881fa9e4066Sahrens * Hand off to libzfs.
882fa9e4066Sahrens */
883ad135b5dSChristopher Siden if (enable_all_pool_feat) {
8842acef22dSMatthew Ahrens spa_feature_t i;
885ad135b5dSChristopher Siden for (i = 0; i < SPA_FEATURES; i++) {
886ad135b5dSChristopher Siden char propname[MAXPATHLEN];
887ad135b5dSChristopher Siden zfeature_info_t *feat = &spa_feature_table[i];
888ad135b5dSChristopher Siden
889ad135b5dSChristopher Siden (void) snprintf(propname, sizeof (propname),
890ad135b5dSChristopher Siden "feature@%s", feat->fi_uname);
891ad135b5dSChristopher Siden
892ad135b5dSChristopher Siden /*
893ad135b5dSChristopher Siden * Skip feature if user specified it manually
894ad135b5dSChristopher Siden * on the command line.
895ad135b5dSChristopher Siden */
896ad135b5dSChristopher Siden if (nvlist_exists(props, propname))
897ad135b5dSChristopher Siden continue;
898ad135b5dSChristopher Siden
8998b713775SWill Andrews ret = add_prop_list(propname,
9008b713775SWill Andrews ZFS_FEATURE_ENABLED, &props, B_TRUE);
9018b713775SWill Andrews if (ret != 0)
902ad135b5dSChristopher Siden goto errout;
903ad135b5dSChristopher Siden }
904ad135b5dSChristopher Siden }
9058b713775SWill Andrews
9068b713775SWill Andrews ret = 1;
9070a48a24eStimh if (zpool_create(g_zfs, poolname,
9080a48a24eStimh nvroot, props, fsprops) == 0) {
90999653d4eSeschrock zfs_handle_t *pool = zfs_open(g_zfs, poolname,
910fa9e4066Sahrens ZFS_TYPE_FILESYSTEM);
911fa9e4066Sahrens if (pool != NULL) {
912fa9e4066Sahrens if (zfs_mount(pool, NULL, 0) == 0)
913da6c28aaSamw ret = zfs_shareall(pool);
914fa9e4066Sahrens zfs_close(pool);
915fa9e4066Sahrens }
91699653d4eSeschrock } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
91799653d4eSeschrock (void) fprintf(stderr, gettext("pool name may have "
91899653d4eSeschrock "been omitted\n"));
919fa9e4066Sahrens }
920fa9e4066Sahrens }
921fa9e4066Sahrens
922990b4856Slling errout:
923fa9e4066Sahrens nvlist_free(nvroot);
9240a48a24eStimh nvlist_free(fsprops);
925990b4856Slling nvlist_free(props);
926fa9e4066Sahrens return (ret);
927990b4856Slling badusage:
9280a48a24eStimh nvlist_free(fsprops);
929990b4856Slling nvlist_free(props);
930990b4856Slling usage(B_FALSE);
931990b4856Slling return (2);
932fa9e4066Sahrens }
933fa9e4066Sahrens
934fa9e4066Sahrens /*
935fa9e4066Sahrens * zpool destroy <pool>
936fa9e4066Sahrens *
937fa9e4066Sahrens * -f Forcefully unmount any datasets
938fa9e4066Sahrens *
939fa9e4066Sahrens * Destroy the given pool. Automatically unmounts any datasets in the pool.
940fa9e4066Sahrens */
941fa9e4066Sahrens int
zpool_do_destroy(int argc,char ** argv)942fa9e4066Sahrens zpool_do_destroy(int argc, char **argv)
943fa9e4066Sahrens {
94499653d4eSeschrock boolean_t force = B_FALSE;
945fa9e4066Sahrens int c;
946fa9e4066Sahrens char *pool;
947fa9e4066Sahrens zpool_handle_t *zhp;
948fa9e4066Sahrens int ret;
949fa9e4066Sahrens
950fa9e4066Sahrens /* check options */
951fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) {
952fa9e4066Sahrens switch (c) {
953fa9e4066Sahrens case 'f':
95499653d4eSeschrock force = B_TRUE;
955fa9e4066Sahrens break;
956fa9e4066Sahrens case '?':
957fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
958fa9e4066Sahrens optopt);
95999653d4eSeschrock usage(B_FALSE);
960fa9e4066Sahrens }
961fa9e4066Sahrens }
962fa9e4066Sahrens
963fa9e4066Sahrens argc -= optind;
964fa9e4066Sahrens argv += optind;
965fa9e4066Sahrens
966fa9e4066Sahrens /* check arguments */
967fa9e4066Sahrens if (argc < 1) {
968fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool argument\n"));
96999653d4eSeschrock usage(B_FALSE);
970fa9e4066Sahrens }
971fa9e4066Sahrens if (argc > 1) {
972fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n"));
97399653d4eSeschrock usage(B_FALSE);
974fa9e4066Sahrens }
975fa9e4066Sahrens
976fa9e4066Sahrens pool = argv[0];
977fa9e4066Sahrens
97899653d4eSeschrock if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
979fa9e4066Sahrens /*
980fa9e4066Sahrens * As a special case, check for use of '/' in the name, and
981fa9e4066Sahrens * direct the user to use 'zfs destroy' instead.
982fa9e4066Sahrens */
983fa9e4066Sahrens if (strchr(pool, '/') != NULL)
984fa9e4066Sahrens (void) fprintf(stderr, gettext("use 'zfs destroy' to "
985fa9e4066Sahrens "destroy a dataset\n"));
986fa9e4066Sahrens return (1);
987fa9e4066Sahrens }
988fa9e4066Sahrens
989f3861e1aSahl if (zpool_disable_datasets(zhp, force) != 0) {
990fa9e4066Sahrens (void) fprintf(stderr, gettext("could not destroy '%s': "
991fa9e4066Sahrens "could not unmount datasets\n"), zpool_get_name(zhp));
992fa9e4066Sahrens return (1);
993fa9e4066Sahrens }
994fa9e4066Sahrens
9954445fffbSMatthew Ahrens /* The history must be logged as part of the export */
9964445fffbSMatthew Ahrens log_history = B_FALSE;
9974445fffbSMatthew Ahrens
9984445fffbSMatthew Ahrens ret = (zpool_destroy(zhp, history_str) != 0);
999fa9e4066Sahrens
1000fa9e4066Sahrens zpool_close(zhp);
1001fa9e4066Sahrens
1002fa9e4066Sahrens return (ret);
1003fa9e4066Sahrens }
1004fa9e4066Sahrens
1005fa9e4066Sahrens /*
1006fa9e4066Sahrens * zpool export [-f] <pool> ...
1007fa9e4066Sahrens *
1008fa9e4066Sahrens * -f Forcefully unmount datasets
1009fa9e4066Sahrens *
1010b1b8ab34Slling * Export the given pools. By default, the command will attempt to cleanly
1011fa9e4066Sahrens * unmount any active datasets within the pool. If the '-f' flag is specified,
1012fa9e4066Sahrens * then the datasets will be forcefully unmounted.
1013fa9e4066Sahrens */
1014fa9e4066Sahrens int
zpool_do_export(int argc,char ** argv)1015fa9e4066Sahrens zpool_do_export(int argc, char **argv)
1016fa9e4066Sahrens {
101799653d4eSeschrock boolean_t force = B_FALSE;
1018394ab0cbSGeorge Wilson boolean_t hardforce = B_FALSE;
1019fa9e4066Sahrens int c;
1020fa9e4066Sahrens zpool_handle_t *zhp;
1021fa9e4066Sahrens int ret;
1022fa9e4066Sahrens int i;
1023fa9e4066Sahrens
1024fa9e4066Sahrens /* check options */
1025394ab0cbSGeorge Wilson while ((c = getopt(argc, argv, "fF")) != -1) {
1026fa9e4066Sahrens switch (c) {
1027fa9e4066Sahrens case 'f':
102899653d4eSeschrock force = B_TRUE;
1029fa9e4066Sahrens break;
1030394ab0cbSGeorge Wilson case 'F':
1031394ab0cbSGeorge Wilson hardforce = B_TRUE;
1032394ab0cbSGeorge Wilson break;
1033fa9e4066Sahrens case '?':
1034fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1035fa9e4066Sahrens optopt);
103699653d4eSeschrock usage(B_FALSE);
1037fa9e4066Sahrens }
1038fa9e4066Sahrens }
1039fa9e4066Sahrens
1040fa9e4066Sahrens argc -= optind;
1041fa9e4066Sahrens argv += optind;
1042fa9e4066Sahrens
1043fa9e4066Sahrens /* check arguments */
1044fa9e4066Sahrens if (argc < 1) {
1045fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool argument\n"));
104699653d4eSeschrock usage(B_FALSE);
1047fa9e4066Sahrens }
1048fa9e4066Sahrens
1049fa9e4066Sahrens ret = 0;
1050fa9e4066Sahrens for (i = 0; i < argc; i++) {
105199653d4eSeschrock if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1052fa9e4066Sahrens ret = 1;
1053fa9e4066Sahrens continue;
1054fa9e4066Sahrens }
1055fa9e4066Sahrens
1056f3861e1aSahl if (zpool_disable_datasets(zhp, force) != 0) {
1057fa9e4066Sahrens ret = 1;
1058fa9e4066Sahrens zpool_close(zhp);
1059fa9e4066Sahrens continue;
1060fa9e4066Sahrens }
1061fa9e4066Sahrens
10624445fffbSMatthew Ahrens /* The history must be logged as part of the export */
10634445fffbSMatthew Ahrens log_history = B_FALSE;
10644445fffbSMatthew Ahrens
1065394ab0cbSGeorge Wilson if (hardforce) {
10664445fffbSMatthew Ahrens if (zpool_export_force(zhp, history_str) != 0)
1067fa9e4066Sahrens ret = 1;
10684445fffbSMatthew Ahrens } else if (zpool_export(zhp, force, history_str) != 0) {
1069394ab0cbSGeorge Wilson ret = 1;
1070394ab0cbSGeorge Wilson }
1071fa9e4066Sahrens
1072fa9e4066Sahrens zpool_close(zhp);
1073fa9e4066Sahrens }
1074fa9e4066Sahrens
1075fa9e4066Sahrens return (ret);
1076fa9e4066Sahrens }
1077fa9e4066Sahrens
1078fa9e4066Sahrens /*
1079fa9e4066Sahrens * Given a vdev configuration, determine the maximum width needed for the device
1080fa9e4066Sahrens * name column.
1081fa9e4066Sahrens */
1082fa9e4066Sahrens static int
max_width(zpool_handle_t * zhp,nvlist_t * nv,int depth,int max)1083c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1084fa9e4066Sahrens {
108588ecc943SGeorge Wilson char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1086fa9e4066Sahrens nvlist_t **child;
1087fa9e4066Sahrens uint_t c, children;
1088fa9e4066Sahrens int ret;
1089fa9e4066Sahrens
1090fa9e4066Sahrens if (strlen(name) + depth > max)
1091fa9e4066Sahrens max = strlen(name) + depth;
1092fa9e4066Sahrens
1093afefbcddSeschrock free(name);
1094afefbcddSeschrock
109599653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
109699653d4eSeschrock &child, &children) == 0) {
1097fa9e4066Sahrens for (c = 0; c < children; c++)
109899653d4eSeschrock if ((ret = max_width(zhp, child[c], depth + 2,
109999653d4eSeschrock max)) > max)
1100fa9e4066Sahrens max = ret;
110199653d4eSeschrock }
110299653d4eSeschrock
1103fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1104fa94a07fSbrendan &child, &children) == 0) {
1105fa94a07fSbrendan for (c = 0; c < children; c++)
1106fa94a07fSbrendan if ((ret = max_width(zhp, child[c], depth + 2,
1107fa94a07fSbrendan max)) > max)
1108fa94a07fSbrendan max = ret;
1109fa94a07fSbrendan }
1110fa94a07fSbrendan
111199653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
111299653d4eSeschrock &child, &children) == 0) {
111399653d4eSeschrock for (c = 0; c < children; c++)
111499653d4eSeschrock if ((ret = max_width(zhp, child[c], depth + 2,
111599653d4eSeschrock max)) > max)
111699653d4eSeschrock max = ret;
111799653d4eSeschrock }
111899653d4eSeschrock
1119fa9e4066Sahrens
1120fa9e4066Sahrens return (max);
1121fa9e4066Sahrens }
1122fa9e4066Sahrens
1123e6ca193dSGeorge Wilson typedef struct spare_cbdata {
1124e6ca193dSGeorge Wilson uint64_t cb_guid;
1125e6ca193dSGeorge Wilson zpool_handle_t *cb_zhp;
1126e6ca193dSGeorge Wilson } spare_cbdata_t;
1127e6ca193dSGeorge Wilson
1128e6ca193dSGeorge Wilson static boolean_t
find_vdev(nvlist_t * nv,uint64_t search)1129e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search)
1130e6ca193dSGeorge Wilson {
1131e6ca193dSGeorge Wilson uint64_t guid;
1132e6ca193dSGeorge Wilson nvlist_t **child;
1133e6ca193dSGeorge Wilson uint_t c, children;
1134e6ca193dSGeorge Wilson
1135e6ca193dSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1136e6ca193dSGeorge Wilson search == guid)
1137e6ca193dSGeorge Wilson return (B_TRUE);
1138e6ca193dSGeorge Wilson
1139e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1140e6ca193dSGeorge Wilson &child, &children) == 0) {
1141e6ca193dSGeorge Wilson for (c = 0; c < children; c++)
1142e6ca193dSGeorge Wilson if (find_vdev(child[c], search))
1143e6ca193dSGeorge Wilson return (B_TRUE);
1144e6ca193dSGeorge Wilson }
1145e6ca193dSGeorge Wilson
1146e6ca193dSGeorge Wilson return (B_FALSE);
1147e6ca193dSGeorge Wilson }
1148e6ca193dSGeorge Wilson
1149e6ca193dSGeorge Wilson static int
find_spare(zpool_handle_t * zhp,void * data)1150e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data)
1151e6ca193dSGeorge Wilson {
1152e6ca193dSGeorge Wilson spare_cbdata_t *cbp = data;
1153e6ca193dSGeorge Wilson nvlist_t *config, *nvroot;
1154e6ca193dSGeorge Wilson
1155e6ca193dSGeorge Wilson config = zpool_get_config(zhp, NULL);
1156e6ca193dSGeorge Wilson verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1157e6ca193dSGeorge Wilson &nvroot) == 0);
1158e6ca193dSGeorge Wilson
1159e6ca193dSGeorge Wilson if (find_vdev(nvroot, cbp->cb_guid)) {
1160e6ca193dSGeorge Wilson cbp->cb_zhp = zhp;
1161e6ca193dSGeorge Wilson return (1);
1162e6ca193dSGeorge Wilson }
1163e6ca193dSGeorge Wilson
1164e6ca193dSGeorge Wilson zpool_close(zhp);
1165e6ca193dSGeorge Wilson return (0);
1166e6ca193dSGeorge Wilson }
1167e6ca193dSGeorge Wilson
1168e6ca193dSGeorge Wilson /*
1169e6ca193dSGeorge Wilson * Print out configuration state as requested by status_callback.
1170e6ca193dSGeorge Wilson */
1171e6ca193dSGeorge Wilson void
print_status_config(zpool_handle_t * zhp,const char * name,nvlist_t * nv,int namewidth,int depth,boolean_t isspare)1172e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1173e6ca193dSGeorge Wilson int namewidth, int depth, boolean_t isspare)
1174e6ca193dSGeorge Wilson {
1175e6ca193dSGeorge Wilson nvlist_t **child;
1176e6ca193dSGeorge Wilson uint_t c, children;
11773f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL;
1178e6ca193dSGeorge Wilson vdev_stat_t *vs;
11793f9d6ad7SLin Ling char rbuf[6], wbuf[6], cbuf[6];
1180e6ca193dSGeorge Wilson char *vname;
1181e6ca193dSGeorge Wilson uint64_t notpresent;
1182e6ca193dSGeorge Wilson spare_cbdata_t cb;
1183e6ca193dSGeorge Wilson char *state;
1184e6ca193dSGeorge Wilson
1185e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1186e6ca193dSGeorge Wilson &child, &children) != 0)
1187e6ca193dSGeorge Wilson children = 0;
1188e6ca193dSGeorge Wilson
11893f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
11903f9d6ad7SLin Ling (uint64_t **)&vs, &c) == 0);
11913f9d6ad7SLin Ling
1192e6ca193dSGeorge Wilson state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1193e6ca193dSGeorge Wilson if (isspare) {
1194e6ca193dSGeorge Wilson /*
1195e6ca193dSGeorge Wilson * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1196e6ca193dSGeorge Wilson * online drives.
1197e6ca193dSGeorge Wilson */
1198e6ca193dSGeorge Wilson if (vs->vs_aux == VDEV_AUX_SPARED)
1199e6ca193dSGeorge Wilson state = "INUSE";
1200e6ca193dSGeorge Wilson else if (vs->vs_state == VDEV_STATE_HEALTHY)
1201e6ca193dSGeorge Wilson state = "AVAIL";
1202e6ca193dSGeorge Wilson }
1203e6ca193dSGeorge Wilson
1204e6ca193dSGeorge Wilson (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1205e6ca193dSGeorge Wilson name, state);
1206e6ca193dSGeorge Wilson
1207e6ca193dSGeorge Wilson if (!isspare) {
1208e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1209e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1210e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1211e6ca193dSGeorge Wilson (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1212e6ca193dSGeorge Wilson }
1213e6ca193dSGeorge Wilson
1214e6ca193dSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1215e6ca193dSGeorge Wilson ¬present) == 0) {
1216e6ca193dSGeorge Wilson char *path;
1217e6ca193dSGeorge Wilson verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1218e6ca193dSGeorge Wilson (void) printf(" was %s", path);
1219e6ca193dSGeorge Wilson } else if (vs->vs_aux != 0) {
1220e6ca193dSGeorge Wilson (void) printf(" ");
1221e6ca193dSGeorge Wilson
1222e6ca193dSGeorge Wilson switch (vs->vs_aux) {
1223e6ca193dSGeorge Wilson case VDEV_AUX_OPEN_FAILED:
1224e6ca193dSGeorge Wilson (void) printf(gettext("cannot open"));
1225e6ca193dSGeorge Wilson break;
1226e6ca193dSGeorge Wilson
1227e6ca193dSGeorge Wilson case VDEV_AUX_BAD_GUID_SUM:
1228e6ca193dSGeorge Wilson (void) printf(gettext("missing device"));
1229e6ca193dSGeorge Wilson break;
1230e6ca193dSGeorge Wilson
1231e6ca193dSGeorge Wilson case VDEV_AUX_NO_REPLICAS:
1232e6ca193dSGeorge Wilson (void) printf(gettext("insufficient replicas"));
1233e6ca193dSGeorge Wilson break;
1234e6ca193dSGeorge Wilson
1235e6ca193dSGeorge Wilson case VDEV_AUX_VERSION_NEWER:
1236e6ca193dSGeorge Wilson (void) printf(gettext("newer version"));
1237e6ca193dSGeorge Wilson break;
1238e6ca193dSGeorge Wilson
1239ad135b5dSChristopher Siden case VDEV_AUX_UNSUP_FEAT:
1240ad135b5dSChristopher Siden (void) printf(gettext("unsupported feature(s)"));
1241ad135b5dSChristopher Siden break;
1242ad135b5dSChristopher Siden
1243e6ca193dSGeorge Wilson case VDEV_AUX_SPARED:
1244e6ca193dSGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1245e6ca193dSGeorge Wilson &cb.cb_guid) == 0);
1246e6ca193dSGeorge Wilson if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1247e6ca193dSGeorge Wilson if (strcmp(zpool_get_name(cb.cb_zhp),
1248e6ca193dSGeorge Wilson zpool_get_name(zhp)) == 0)
1249e6ca193dSGeorge Wilson (void) printf(gettext("currently in "
1250e6ca193dSGeorge Wilson "use"));
1251e6ca193dSGeorge Wilson else
1252e6ca193dSGeorge Wilson (void) printf(gettext("in use by "
1253e6ca193dSGeorge Wilson "pool '%s'"),
1254e6ca193dSGeorge Wilson zpool_get_name(cb.cb_zhp));
1255e6ca193dSGeorge Wilson zpool_close(cb.cb_zhp);
1256e6ca193dSGeorge Wilson } else {
1257e6ca193dSGeorge Wilson (void) printf(gettext("currently in use"));
1258e6ca193dSGeorge Wilson }
1259e6ca193dSGeorge Wilson break;
1260e6ca193dSGeorge Wilson
1261e6ca193dSGeorge Wilson case VDEV_AUX_ERR_EXCEEDED:
1262e6ca193dSGeorge Wilson (void) printf(gettext("too many errors"));
1263e6ca193dSGeorge Wilson break;
1264e6ca193dSGeorge Wilson
1265e6ca193dSGeorge Wilson case VDEV_AUX_IO_FAILURE:
1266e6ca193dSGeorge Wilson (void) printf(gettext("experienced I/O failures"));
1267e6ca193dSGeorge Wilson break;
1268e6ca193dSGeorge Wilson
1269e6ca193dSGeorge Wilson case VDEV_AUX_BAD_LOG:
1270e6ca193dSGeorge Wilson (void) printf(gettext("bad intent log"));
1271e6ca193dSGeorge Wilson break;
1272e6ca193dSGeorge Wilson
1273069f55e2SEric Schrock case VDEV_AUX_EXTERNAL:
1274069f55e2SEric Schrock (void) printf(gettext("external device fault"));
1275069f55e2SEric Schrock break;
1276069f55e2SEric Schrock
12771195e687SMark J Musante case VDEV_AUX_SPLIT_POOL:
12781195e687SMark J Musante (void) printf(gettext("split into new pool"));
12791195e687SMark J Musante break;
12801195e687SMark J Musante
1281e6ca193dSGeorge Wilson default:
1282e6ca193dSGeorge Wilson (void) printf(gettext("corrupted data"));
1283e6ca193dSGeorge Wilson break;
1284e6ca193dSGeorge Wilson }
12853f9d6ad7SLin Ling }
12863f9d6ad7SLin Ling
12873f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
12883f9d6ad7SLin Ling (uint64_t **)&ps, &c);
12893f9d6ad7SLin Ling
12903f9d6ad7SLin Ling if (ps && ps->pss_state == DSS_SCANNING &&
12913f9d6ad7SLin Ling vs->vs_scan_processed != 0 && children == 0) {
12923f9d6ad7SLin Ling (void) printf(gettext(" (%s)"),
12933f9d6ad7SLin Ling (ps->pss_func == POOL_SCAN_RESILVER) ?
12943f9d6ad7SLin Ling "resilvering" : "repairing");
1295e6ca193dSGeorge Wilson }
1296e6ca193dSGeorge Wilson
1297e6ca193dSGeorge Wilson (void) printf("\n");
1298e6ca193dSGeorge Wilson
1299e6ca193dSGeorge Wilson for (c = 0; c < children; c++) {
130088ecc943SGeorge Wilson uint64_t islog = B_FALSE, ishole = B_FALSE;
1301e6ca193dSGeorge Wilson
130288ecc943SGeorge Wilson /* Don't print logs or holes here */
1303e6ca193dSGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
130488ecc943SGeorge Wilson &islog);
130588ecc943SGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
130688ecc943SGeorge Wilson &ishole);
130788ecc943SGeorge Wilson if (islog || ishole)
1308e6ca193dSGeorge Wilson continue;
130988ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1310e6ca193dSGeorge Wilson print_status_config(zhp, vname, child[c],
1311e6ca193dSGeorge Wilson namewidth, depth + 2, isspare);
1312e6ca193dSGeorge Wilson free(vname);
1313e6ca193dSGeorge Wilson }
1314e6ca193dSGeorge Wilson }
1315e6ca193dSGeorge Wilson
1316fa9e4066Sahrens
1317fa9e4066Sahrens /*
1318fa9e4066Sahrens * Print the configuration of an exported pool. Iterate over all vdevs in the
1319fa9e4066Sahrens * pool, printing out the name and status for each one.
1320fa9e4066Sahrens */
1321fa9e4066Sahrens void
print_import_config(const char * name,nvlist_t * nv,int namewidth,int depth)1322e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1323fa9e4066Sahrens {
1324fa9e4066Sahrens nvlist_t **child;
1325fa9e4066Sahrens uint_t c, children;
1326fa9e4066Sahrens vdev_stat_t *vs;
1327afefbcddSeschrock char *type, *vname;
1328fa9e4066Sahrens
1329fa9e4066Sahrens verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
133088ecc943SGeorge Wilson if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
133188ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0)
1332fa9e4066Sahrens return;
1333fa9e4066Sahrens
13343f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1335fa9e4066Sahrens (uint64_t **)&vs, &c) == 0);
1336fa9e4066Sahrens
1337fa9e4066Sahrens (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1338990b4856Slling (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1339fa9e4066Sahrens
1340fa9e4066Sahrens if (vs->vs_aux != 0) {
13413d7072f8Seschrock (void) printf(" ");
1342fa9e4066Sahrens
1343fa9e4066Sahrens switch (vs->vs_aux) {
1344fa9e4066Sahrens case VDEV_AUX_OPEN_FAILED:
1345fa9e4066Sahrens (void) printf(gettext("cannot open"));
1346fa9e4066Sahrens break;
1347fa9e4066Sahrens
1348fa9e4066Sahrens case VDEV_AUX_BAD_GUID_SUM:
1349fa9e4066Sahrens (void) printf(gettext("missing device"));
1350fa9e4066Sahrens break;
1351fa9e4066Sahrens
1352fa9e4066Sahrens case VDEV_AUX_NO_REPLICAS:
1353fa9e4066Sahrens (void) printf(gettext("insufficient replicas"));
1354fa9e4066Sahrens break;
1355fa9e4066Sahrens
1356eaca9bbdSeschrock case VDEV_AUX_VERSION_NEWER:
1357eaca9bbdSeschrock (void) printf(gettext("newer version"));
1358eaca9bbdSeschrock break;
1359eaca9bbdSeschrock
1360ad135b5dSChristopher Siden case VDEV_AUX_UNSUP_FEAT:
1361ad135b5dSChristopher Siden (void) printf(gettext("unsupported feature(s)"));
1362ad135b5dSChristopher Siden break;
1363ad135b5dSChristopher Siden
13643d7072f8Seschrock case VDEV_AUX_ERR_EXCEEDED:
13653d7072f8Seschrock (void) printf(gettext("too many errors"));
13663d7072f8Seschrock break;
13673d7072f8Seschrock
1368fa9e4066Sahrens default:
1369fa9e4066Sahrens (void) printf(gettext("corrupted data"));
1370fa9e4066Sahrens break;
1371fa9e4066Sahrens }
1372fa9e4066Sahrens }
1373fa9e4066Sahrens (void) printf("\n");
1374fa9e4066Sahrens
1375fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1376fa9e4066Sahrens &child, &children) != 0)
1377fa9e4066Sahrens return;
1378fa9e4066Sahrens
1379afefbcddSeschrock for (c = 0; c < children; c++) {
13808654d025Sperrin uint64_t is_log = B_FALSE;
13818654d025Sperrin
13828654d025Sperrin (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
13838654d025Sperrin &is_log);
1384e6ca193dSGeorge Wilson if (is_log)
13858654d025Sperrin continue;
13868654d025Sperrin
138788ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1388e6ca193dSGeorge Wilson print_import_config(vname, child[c], namewidth, depth + 2);
1389afefbcddSeschrock free(vname);
1390afefbcddSeschrock }
139199653d4eSeschrock
1392fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1393fa94a07fSbrendan &child, &children) == 0) {
1394fa94a07fSbrendan (void) printf(gettext("\tcache\n"));
1395fa94a07fSbrendan for (c = 0; c < children; c++) {
139688ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1397fa94a07fSbrendan (void) printf("\t %s\n", vname);
1398fa94a07fSbrendan free(vname);
1399fa94a07fSbrendan }
1400fa94a07fSbrendan }
140199653d4eSeschrock
1402fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1403fa94a07fSbrendan &child, &children) == 0) {
140499653d4eSeschrock (void) printf(gettext("\tspares\n"));
140599653d4eSeschrock for (c = 0; c < children; c++) {
140688ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
140799653d4eSeschrock (void) printf("\t %s\n", vname);
140899653d4eSeschrock free(vname);
140999653d4eSeschrock }
1410fa9e4066Sahrens }
1411fa94a07fSbrendan }
1412fa9e4066Sahrens
1413fa9e4066Sahrens /*
1414e6ca193dSGeorge Wilson * Print log vdevs.
1415e6ca193dSGeorge Wilson * Logs are recorded as top level vdevs in the main pool child array
1416e6ca193dSGeorge Wilson * but with "is_log" set to 1. We use either print_status_config() or
1417e6ca193dSGeorge Wilson * print_import_config() to print the top level logs then any log
1418e6ca193dSGeorge Wilson * children (eg mirrored slogs) are printed recursively - which
1419e6ca193dSGeorge Wilson * works because only the top level vdev is marked "is_log"
1420e6ca193dSGeorge Wilson */
1421e6ca193dSGeorge Wilson static void
print_logs(zpool_handle_t * zhp,nvlist_t * nv,int namewidth,boolean_t verbose)1422e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1423e6ca193dSGeorge Wilson {
1424e6ca193dSGeorge Wilson uint_t c, children;
1425e6ca193dSGeorge Wilson nvlist_t **child;
1426e6ca193dSGeorge Wilson
1427e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1428e6ca193dSGeorge Wilson &children) != 0)
1429e6ca193dSGeorge Wilson return;
1430e6ca193dSGeorge Wilson
1431e6ca193dSGeorge Wilson (void) printf(gettext("\tlogs\n"));
1432e6ca193dSGeorge Wilson
1433e6ca193dSGeorge Wilson for (c = 0; c < children; c++) {
1434e6ca193dSGeorge Wilson uint64_t is_log = B_FALSE;
1435e6ca193dSGeorge Wilson char *name;
1436e6ca193dSGeorge Wilson
1437e6ca193dSGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1438e6ca193dSGeorge Wilson &is_log);
1439e6ca193dSGeorge Wilson if (!is_log)
1440e6ca193dSGeorge Wilson continue;
144188ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1442e6ca193dSGeorge Wilson if (verbose)
1443e6ca193dSGeorge Wilson print_status_config(zhp, name, child[c], namewidth,
1444e6ca193dSGeorge Wilson 2, B_FALSE);
1445e6ca193dSGeorge Wilson else
1446e6ca193dSGeorge Wilson print_import_config(name, child[c], namewidth, 2);
1447e6ca193dSGeorge Wilson free(name);
1448e6ca193dSGeorge Wilson }
1449e6ca193dSGeorge Wilson }
1450468c413aSTim Haley
1451e6ca193dSGeorge Wilson /*
1452fa9e4066Sahrens * Display the status for the given pool.
1453fa9e4066Sahrens */
1454fa9e4066Sahrens static void
show_import(nvlist_t * config)1455fa9e4066Sahrens show_import(nvlist_t *config)
1456fa9e4066Sahrens {
1457fa9e4066Sahrens uint64_t pool_state;
1458fa9e4066Sahrens vdev_stat_t *vs;
1459fa9e4066Sahrens char *name;
1460fa9e4066Sahrens uint64_t guid;
1461fa9e4066Sahrens char *msgid;
1462fa9e4066Sahrens nvlist_t *nvroot;
1463fa9e4066Sahrens int reason;
146446657f8dSmmusante const char *health;
1465fa9e4066Sahrens uint_t vsc;
1466fa9e4066Sahrens int namewidth;
14678704186eSDan McDonald char *comment;
1468fa9e4066Sahrens
1469fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1470fa9e4066Sahrens &name) == 0);
1471fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1472fa9e4066Sahrens &guid) == 0);
1473fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1474fa9e4066Sahrens &pool_state) == 0);
1475fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1476fa9e4066Sahrens &nvroot) == 0);
1477fa9e4066Sahrens
14783f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1479fa9e4066Sahrens (uint64_t **)&vs, &vsc) == 0);
1480990b4856Slling health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1481fa9e4066Sahrens
1482fa9e4066Sahrens reason = zpool_import_status(config, &msgid);
1483fa9e4066Sahrens
148446657f8dSmmusante (void) printf(gettext(" pool: %s\n"), name);
148546657f8dSmmusante (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
148646657f8dSmmusante (void) printf(gettext(" state: %s"), health);
14874c58d714Sdarrenm if (pool_state == POOL_STATE_DESTROYED)
148846657f8dSmmusante (void) printf(gettext(" (DESTROYED)"));
14894c58d714Sdarrenm (void) printf("\n");
1490fa9e4066Sahrens
1491fa9e4066Sahrens switch (reason) {
1492fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_R:
1493fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_NR:
1494fa9e4066Sahrens case ZPOOL_STATUS_BAD_GUID_SUM:
14958704186eSDan McDonald (void) printf(gettext(" status: One or more devices are "
14968704186eSDan McDonald "missing from the system.\n"));
1497fa9e4066Sahrens break;
1498fa9e4066Sahrens
1499fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_R:
1500fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1501fa9e4066Sahrens (void) printf(gettext(" status: One or more devices contains "
1502fa9e4066Sahrens "corrupted data.\n"));
1503fa9e4066Sahrens break;
1504fa9e4066Sahrens
1505fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_DATA:
15068704186eSDan McDonald (void) printf(
15078704186eSDan McDonald gettext(" status: The pool data is corrupted.\n"));
1508fa9e4066Sahrens break;
1509fa9e4066Sahrens
1510441d80aaSlling case ZPOOL_STATUS_OFFLINE_DEV:
1511441d80aaSlling (void) printf(gettext(" status: One or more devices "
1512441d80aaSlling "are offlined.\n"));
1513441d80aaSlling break;
1514441d80aaSlling
1515ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_POOL:
1516ea8dc4b6Seschrock (void) printf(gettext(" status: The pool metadata is "
1517ea8dc4b6Seschrock "corrupted.\n"));
1518ea8dc4b6Seschrock break;
1519ea8dc4b6Seschrock
1520eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_OLDER:
152157221772SChristopher Siden (void) printf(gettext(" status: The pool is formatted using a "
152257221772SChristopher Siden "legacy on-disk version.\n"));
1523eaca9bbdSeschrock break;
1524eaca9bbdSeschrock
1525eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER:
1526eaca9bbdSeschrock (void) printf(gettext(" status: The pool is formatted using an "
1527eaca9bbdSeschrock "incompatible version.\n"));
1528eaca9bbdSeschrock break;
1529b87f3af3Sperrin
153057221772SChristopher Siden case ZPOOL_STATUS_FEAT_DISABLED:
153157221772SChristopher Siden (void) printf(gettext(" status: Some supported features are "
153257221772SChristopher Siden "not enabled on the pool.\n"));
153357221772SChristopher Siden break;
153457221772SChristopher Siden
1535ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ:
1536ad135b5dSChristopher Siden (void) printf(gettext("status: The pool uses the following "
1537ad135b5dSChristopher Siden "feature(s) not supported on this sytem:\n"));
1538ad135b5dSChristopher Siden zpool_print_unsup_feat(config);
1539ad135b5dSChristopher Siden break;
1540ad135b5dSChristopher Siden
1541ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1542ad135b5dSChristopher Siden (void) printf(gettext("status: The pool can only be accessed "
1543ad135b5dSChristopher Siden "in read-only mode on this system. It\n\tcannot be "
1544ad135b5dSChristopher Siden "accessed in read-write mode because it uses the "
1545ad135b5dSChristopher Siden "following\n\tfeature(s) not supported on this system:\n"));
1546ad135b5dSChristopher Siden zpool_print_unsup_feat(config);
1547ad135b5dSChristopher Siden break;
1548ad135b5dSChristopher Siden
154995173954Sek110237 case ZPOOL_STATUS_HOSTID_MISMATCH:
155095173954Sek110237 (void) printf(gettext(" status: The pool was last accessed by "
155195173954Sek110237 "another system.\n"));
155295173954Sek110237 break;
1553b87f3af3Sperrin
15543d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_R:
15553d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_NR:
15563d7072f8Seschrock (void) printf(gettext(" status: One or more devices are "
15573d7072f8Seschrock "faulted.\n"));
15583d7072f8Seschrock break;
15593d7072f8Seschrock
1560b87f3af3Sperrin case ZPOOL_STATUS_BAD_LOG:
1561b87f3af3Sperrin (void) printf(gettext(" status: An intent log record cannot be "
1562b87f3af3Sperrin "read.\n"));
1563b87f3af3Sperrin break;
1564b87f3af3Sperrin
15653f9d6ad7SLin Ling case ZPOOL_STATUS_RESILVERING:
15663f9d6ad7SLin Ling (void) printf(gettext(" status: One or more devices were being "
15673f9d6ad7SLin Ling "resilvered.\n"));
15683f9d6ad7SLin Ling break;
15693f9d6ad7SLin Ling
1570fa9e4066Sahrens default:
1571fa9e4066Sahrens /*
1572fa9e4066Sahrens * No other status can be seen when importing pools.
1573fa9e4066Sahrens */
1574fa9e4066Sahrens assert(reason == ZPOOL_STATUS_OK);
1575fa9e4066Sahrens }
1576fa9e4066Sahrens
1577fa9e4066Sahrens /*
1578fa9e4066Sahrens * Print out an action according to the overall state of the pool.
1579fa9e4066Sahrens */
158046657f8dSmmusante if (vs->vs_state == VDEV_STATE_HEALTHY) {
158157221772SChristopher Siden if (reason == ZPOOL_STATUS_VERSION_OLDER ||
158257221772SChristopher Siden reason == ZPOOL_STATUS_FEAT_DISABLED) {
1583eaca9bbdSeschrock (void) printf(gettext(" action: The pool can be "
1584eaca9bbdSeschrock "imported using its name or numeric identifier, "
1585eaca9bbdSeschrock "though\n\tsome features will not be available "
1586eaca9bbdSeschrock "without an explicit 'zpool upgrade'.\n"));
158757221772SChristopher Siden } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
158895173954Sek110237 (void) printf(gettext(" action: The pool can be "
158995173954Sek110237 "imported using its name or numeric "
159095173954Sek110237 "identifier and\n\tthe '-f' flag.\n"));
159157221772SChristopher Siden } else {
1592eaca9bbdSeschrock (void) printf(gettext(" action: The pool can be "
1593eaca9bbdSeschrock "imported using its name or numeric "
1594eaca9bbdSeschrock "identifier.\n"));
159557221772SChristopher Siden }
159646657f8dSmmusante } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1597fa9e4066Sahrens (void) printf(gettext(" action: The pool can be imported "
1598fa9e4066Sahrens "despite missing or damaged devices. The\n\tfault "
1599eaca9bbdSeschrock "tolerance of the pool may be compromised if imported.\n"));
1600fa9e4066Sahrens } else {
1601eaca9bbdSeschrock switch (reason) {
1602eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER:
1603eaca9bbdSeschrock (void) printf(gettext(" action: The pool cannot be "
1604eaca9bbdSeschrock "imported. Access the pool on a system running "
1605eaca9bbdSeschrock "newer\n\tsoftware, or recreate the pool from "
1606eaca9bbdSeschrock "backup.\n"));
1607eaca9bbdSeschrock break;
1608ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ:
1609ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be "
1610ad135b5dSChristopher Siden "imported. Access the pool on a system that "
1611ad135b5dSChristopher Siden "supports\n\tthe required feature(s), or recreate "
1612ad135b5dSChristopher Siden "the pool from backup.\n"));
1613ad135b5dSChristopher Siden break;
1614ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1615ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be "
1616ad135b5dSChristopher Siden "imported in read-write mode. Import the pool "
1617ad135b5dSChristopher Siden "with\n"
1618ad135b5dSChristopher Siden "\t\"-o readonly=on\", access the pool on a system "
1619ad135b5dSChristopher Siden "that supports the\n\trequired feature(s), or "
1620ad135b5dSChristopher Siden "recreate the pool from backup.\n"));
1621ad135b5dSChristopher Siden break;
1622eaca9bbdSeschrock case ZPOOL_STATUS_MISSING_DEV_R:
1623eaca9bbdSeschrock case ZPOOL_STATUS_MISSING_DEV_NR:
1624eaca9bbdSeschrock case ZPOOL_STATUS_BAD_GUID_SUM:
1625fa9e4066Sahrens (void) printf(gettext(" action: The pool cannot be "
1626fa9e4066Sahrens "imported. Attach the missing\n\tdevices and try "
1627fa9e4066Sahrens "again.\n"));
1628eaca9bbdSeschrock break;
1629eaca9bbdSeschrock default:
1630fa9e4066Sahrens (void) printf(gettext(" action: The pool cannot be "
1631fa9e4066Sahrens "imported due to damaged devices or data.\n"));
1632fa9e4066Sahrens }
1633eaca9bbdSeschrock }
1634eaca9bbdSeschrock
16358704186eSDan McDonald /* Print the comment attached to the pool. */
16368704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
16378704186eSDan McDonald (void) printf(gettext("comment: %s\n"), comment);
16388704186eSDan McDonald
163946657f8dSmmusante /*
164046657f8dSmmusante * If the state is "closed" or "can't open", and the aux state
164146657f8dSmmusante * is "corrupt data":
164246657f8dSmmusante */
164346657f8dSmmusante if (((vs->vs_state == VDEV_STATE_CLOSED) ||
164446657f8dSmmusante (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
164546657f8dSmmusante (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1646eaca9bbdSeschrock if (pool_state == POOL_STATE_DESTROYED)
1647eaca9bbdSeschrock (void) printf(gettext("\tThe pool was destroyed, "
1648eaca9bbdSeschrock "but can be imported using the '-Df' flags.\n"));
1649eaca9bbdSeschrock else if (pool_state != POOL_STATE_EXPORTED)
1650eaca9bbdSeschrock (void) printf(gettext("\tThe pool may be active on "
165118ce54dfSek110237 "another system, but can be imported using\n\t"
1652eaca9bbdSeschrock "the '-f' flag.\n"));
1653eaca9bbdSeschrock }
1654fa9e4066Sahrens
1655fa9e4066Sahrens if (msgid != NULL)
1656654b400cSJoshua M. Clulow (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1657fa9e4066Sahrens msgid);
1658fa9e4066Sahrens
1659fa9e4066Sahrens (void) printf(gettext(" config:\n\n"));
1660fa9e4066Sahrens
1661c67d9675Seschrock namewidth = max_width(NULL, nvroot, 0, 0);
1662fa9e4066Sahrens if (namewidth < 10)
1663fa9e4066Sahrens namewidth = 10;
16648654d025Sperrin
1665e6ca193dSGeorge Wilson print_import_config(name, nvroot, namewidth, 0);
1666e6ca193dSGeorge Wilson if (num_logs(nvroot) > 0)
1667e6ca193dSGeorge Wilson print_logs(NULL, nvroot, namewidth, B_FALSE);
1668fa9e4066Sahrens
1669fa9e4066Sahrens if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
167046657f8dSmmusante (void) printf(gettext("\n\tAdditional devices are known to "
1671fa9e4066Sahrens "be part of this pool, though their\n\texact "
167246657f8dSmmusante "configuration cannot be determined.\n"));
1673fa9e4066Sahrens }
1674fa9e4066Sahrens }
1675fa9e4066Sahrens
1676fa9e4066Sahrens /*
1677fa9e4066Sahrens * Perform the import for the given configuration. This passes the heavy
1678990b4856Slling * lifting off to zpool_import_props(), and then mounts the datasets contained
1679990b4856Slling * within the pool.
1680fa9e4066Sahrens */
1681fa9e4066Sahrens static int
do_import(nvlist_t * config,const char * newname,const char * mntopts,nvlist_t * props,int flags)1682fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts,
16834b964adaSGeorge Wilson nvlist_t *props, int flags)
1684fa9e4066Sahrens {
1685fa9e4066Sahrens zpool_handle_t *zhp;
1686fa9e4066Sahrens char *name;
1687fa9e4066Sahrens uint64_t state;
1688eaca9bbdSeschrock uint64_t version;
1689fa9e4066Sahrens
1690fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1691fa9e4066Sahrens &name) == 0);
1692fa9e4066Sahrens
1693fa9e4066Sahrens verify(nvlist_lookup_uint64(config,
1694fa9e4066Sahrens ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1695eaca9bbdSeschrock verify(nvlist_lookup_uint64(config,
1696eaca9bbdSeschrock ZPOOL_CONFIG_VERSION, &version) == 0);
1697ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(version)) {
1698eaca9bbdSeschrock (void) fprintf(stderr, gettext("cannot import '%s': pool "
1699ad135b5dSChristopher Siden "is formatted using an unsupported ZFS version\n"), name);
1700eaca9bbdSeschrock return (1);
17014b964adaSGeorge Wilson } else if (state != POOL_STATE_EXPORTED &&
17024b964adaSGeorge Wilson !(flags & ZFS_IMPORT_ANY_HOST)) {
170395173954Sek110237 uint64_t hostid;
170495173954Sek110237
170595173954Sek110237 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
170695173954Sek110237 &hostid) == 0) {
170795173954Sek110237 if ((unsigned long)hostid != gethostid()) {
170895173954Sek110237 char *hostname;
170995173954Sek110237 uint64_t timestamp;
171095173954Sek110237 time_t t;
171195173954Sek110237
171295173954Sek110237 verify(nvlist_lookup_string(config,
171395173954Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
171495173954Sek110237 verify(nvlist_lookup_uint64(config,
171595173954Sek110237 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
171695173954Sek110237 t = timestamp;
171795173954Sek110237 (void) fprintf(stderr, gettext("cannot import "
171895173954Sek110237 "'%s': pool may be in use from other "
171995173954Sek110237 "system, it was last accessed by %s "
172095173954Sek110237 "(hostid: 0x%lx) on %s"), name, hostname,
172195173954Sek110237 (unsigned long)hostid,
172295173954Sek110237 asctime(localtime(&t)));
172395173954Sek110237 (void) fprintf(stderr, gettext("use '-f' to "
172495173954Sek110237 "import anyway\n"));
1725fa9e4066Sahrens return (1);
1726fa9e4066Sahrens }
172795173954Sek110237 } else {
172895173954Sek110237 (void) fprintf(stderr, gettext("cannot import '%s': "
172995173954Sek110237 "pool may be in use from other system\n"), name);
173095173954Sek110237 (void) fprintf(stderr, gettext("use '-f' to import "
173195173954Sek110237 "anyway\n"));
173295173954Sek110237 return (1);
173395173954Sek110237 }
173495173954Sek110237 }
1735fa9e4066Sahrens
17364b964adaSGeorge Wilson if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1737fa9e4066Sahrens return (1);
1738fa9e4066Sahrens
1739fa9e4066Sahrens if (newname != NULL)
1740fa9e4066Sahrens name = (char *)newname;
1741fa9e4066Sahrens
17424f0f5e5bSVictor Latushkin if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
17434f0f5e5bSVictor Latushkin return (1);
1744fa9e4066Sahrens
1745379c004dSEric Schrock if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1746f9af39baSGeorge Wilson !(flags & ZFS_IMPORT_ONLY) &&
1747379c004dSEric Schrock zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1748fa9e4066Sahrens zpool_close(zhp);
1749fa9e4066Sahrens return (1);
1750fa9e4066Sahrens }
1751fa9e4066Sahrens
1752fa9e4066Sahrens zpool_close(zhp);
1753468c413aSTim Haley return (0);
1754fa9e4066Sahrens }
1755fa9e4066Sahrens
1756fa9e4066Sahrens /*
17574c58d714Sdarrenm * zpool import [-d dir] [-D]
17582f8aaab3Seschrock * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
17592f8aaab3Seschrock * [-d dir | -c cachefile] [-f] -a
17602f8aaab3Seschrock * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1761468c413aSTim Haley * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
17622f8aaab3Seschrock *
17632f8aaab3Seschrock * -c Read pool information from a cachefile instead of searching
17642f8aaab3Seschrock * devices.
1765fa9e4066Sahrens *
1766fa9e4066Sahrens * -d Scan in a specific directory, other than /dev/dsk. More than
1767fa9e4066Sahrens * one directory can be specified using multiple '-d' options.
1768fa9e4066Sahrens *
17694c58d714Sdarrenm * -D Scan for previously destroyed pools or import all or only
17704c58d714Sdarrenm * specified destroyed pools.
17714c58d714Sdarrenm *
1772fa9e4066Sahrens * -R Temporarily import the pool, with all mountpoints relative to
1773fa9e4066Sahrens * the given root. The pool will remain exported when the machine
1774fa9e4066Sahrens * is rebooted.
1775fa9e4066Sahrens *
1776468c413aSTim Haley * -V Import even in the presence of faulted vdevs. This is an
1777c5904d13Seschrock * intentionally undocumented option for testing purposes, and
1778c5904d13Seschrock * treats the pool configuration as complete, leaving any bad
17794f0f5e5bSVictor Latushkin * vdevs in the FAULTED state. In other words, it does verbatim
17804f0f5e5bSVictor Latushkin * import.
1781c5904d13Seschrock *
1782468c413aSTim Haley * -f Force import, even if it appears that the pool is active.
1783468c413aSTim Haley *
1784468c413aSTim Haley * -F Attempt rewind if necessary.
1785468c413aSTim Haley *
1786468c413aSTim Haley * -n See if rewind would work, but don't actually rewind.
1787468c413aSTim Haley *
1788f9af39baSGeorge Wilson * -N Import the pool but don't mount datasets.
1789f9af39baSGeorge Wilson *
1790f9af39baSGeorge Wilson * -T Specify a starting txg to use for import. This option is
1791f9af39baSGeorge Wilson * intentionally undocumented option for testing purposes.
1792f9af39baSGeorge Wilson *
1793fa9e4066Sahrens * -a Import all pools found.
1794fa9e4066Sahrens *
1795990b4856Slling * -o Set property=value and/or temporary mount options (without '=').
1796ecd6cf80Smarks *
1797fa9e4066Sahrens * The import command scans for pools to import, and import pools based on pool
1798fa9e4066Sahrens * name and GUID. The pool can also be renamed as part of the import process.
1799fa9e4066Sahrens */
1800fa9e4066Sahrens int
zpool_do_import(int argc,char ** argv)1801fa9e4066Sahrens zpool_do_import(int argc, char **argv)
1802fa9e4066Sahrens {
1803fa9e4066Sahrens char **searchdirs = NULL;
1804fa9e4066Sahrens int nsearch = 0;
1805fa9e4066Sahrens int c;
1806d41c4376SMark J Musante int err = 0;
18072f8aaab3Seschrock nvlist_t *pools = NULL;
180899653d4eSeschrock boolean_t do_all = B_FALSE;
180999653d4eSeschrock boolean_t do_destroyed = B_FALSE;
1810fa9e4066Sahrens char *mntopts = NULL;
1811fa9e4066Sahrens nvpair_t *elem;
1812fa9e4066Sahrens nvlist_t *config;
181324e697d4Sck153898 uint64_t searchguid = 0;
181424e697d4Sck153898 char *searchname = NULL;
1815990b4856Slling char *propval;
1816fa9e4066Sahrens nvlist_t *found_config;
1817468c413aSTim Haley nvlist_t *policy = NULL;
1818ecd6cf80Smarks nvlist_t *props = NULL;
181999653d4eSeschrock boolean_t first;
18204b964adaSGeorge Wilson int flags = ZFS_IMPORT_NORMAL;
1821468c413aSTim Haley uint32_t rewind_policy = ZPOOL_NO_REWIND;
1822468c413aSTim Haley boolean_t dryrun = B_FALSE;
1823468c413aSTim Haley boolean_t do_rewind = B_FALSE;
1824468c413aSTim Haley boolean_t xtreme_rewind = B_FALSE;
1825f9af39baSGeorge Wilson uint64_t pool_state, txg = -1ULL;
18262f8aaab3Seschrock char *cachefile = NULL;
1827d41c4376SMark J Musante importargs_t idata = { 0 };
1828f9af39baSGeorge Wilson char *endptr;
1829fa9e4066Sahrens
1830fa9e4066Sahrens /* check options */
1831f9af39baSGeorge Wilson while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1832fa9e4066Sahrens switch (c) {
1833fa9e4066Sahrens case 'a':
183499653d4eSeschrock do_all = B_TRUE;
1835fa9e4066Sahrens break;
18362f8aaab3Seschrock case 'c':
18372f8aaab3Seschrock cachefile = optarg;
18382f8aaab3Seschrock break;
1839fa9e4066Sahrens case 'd':
1840fa9e4066Sahrens if (searchdirs == NULL) {
1841fa9e4066Sahrens searchdirs = safe_malloc(sizeof (char *));
1842fa9e4066Sahrens } else {
1843fa9e4066Sahrens char **tmp = safe_malloc((nsearch + 1) *
1844fa9e4066Sahrens sizeof (char *));
1845fa9e4066Sahrens bcopy(searchdirs, tmp, nsearch *
1846fa9e4066Sahrens sizeof (char *));
1847fa9e4066Sahrens free(searchdirs);
1848fa9e4066Sahrens searchdirs = tmp;
1849fa9e4066Sahrens }
1850fa9e4066Sahrens searchdirs[nsearch++] = optarg;
1851fa9e4066Sahrens break;
18524c58d714Sdarrenm case 'D':
185399653d4eSeschrock do_destroyed = B_TRUE;
18544c58d714Sdarrenm break;
1855fa9e4066Sahrens case 'f':
18564b964adaSGeorge Wilson flags |= ZFS_IMPORT_ANY_HOST;
1857fa9e4066Sahrens break;
1858c5904d13Seschrock case 'F':
1859468c413aSTim Haley do_rewind = B_TRUE;
1860468c413aSTim Haley break;
18614b964adaSGeorge Wilson case 'm':
18624b964adaSGeorge Wilson flags |= ZFS_IMPORT_MISSING_LOG;
18634b964adaSGeorge Wilson break;
1864468c413aSTim Haley case 'n':
1865468c413aSTim Haley dryrun = B_TRUE;
1866c5904d13Seschrock break;
1867f9af39baSGeorge Wilson case 'N':
1868f9af39baSGeorge Wilson flags |= ZFS_IMPORT_ONLY;
1869f9af39baSGeorge Wilson break;
1870fa9e4066Sahrens case 'o':
1871990b4856Slling if ((propval = strchr(optarg, '=')) != NULL) {
1872990b4856Slling *propval = '\0';
1873990b4856Slling propval++;
18740a48a24eStimh if (add_prop_list(optarg, propval,
18750a48a24eStimh &props, B_TRUE))
1876990b4856Slling goto error;
1877990b4856Slling } else {
1878fa9e4066Sahrens mntopts = optarg;
1879990b4856Slling }
1880fa9e4066Sahrens break;
1881fa9e4066Sahrens case 'R':
1882990b4856Slling if (add_prop_list(zpool_prop_to_name(
18830a48a24eStimh ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1884990b4856Slling goto error;
18852f8aaab3Seschrock if (nvlist_lookup_string(props,
18862f8aaab3Seschrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
18872f8aaab3Seschrock &propval) == 0)
18882f8aaab3Seschrock break;
1889990b4856Slling if (add_prop_list(zpool_prop_to_name(
18900a48a24eStimh ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1891990b4856Slling goto error;
1892fa9e4066Sahrens break;
1893f9af39baSGeorge Wilson case 'T':
1894f9af39baSGeorge Wilson errno = 0;
1895e42d2059SMatthew Ahrens txg = strtoull(optarg, &endptr, 0);
1896f9af39baSGeorge Wilson if (errno != 0 || *endptr != '\0') {
1897f9af39baSGeorge Wilson (void) fprintf(stderr,
1898f9af39baSGeorge Wilson gettext("invalid txg value\n"));
1899f9af39baSGeorge Wilson usage(B_FALSE);
1900f9af39baSGeorge Wilson }
1901f9af39baSGeorge Wilson rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1902f9af39baSGeorge Wilson break;
1903468c413aSTim Haley case 'V':
19044b964adaSGeorge Wilson flags |= ZFS_IMPORT_VERBATIM;
1905468c413aSTim Haley break;
1906468c413aSTim Haley case 'X':
1907468c413aSTim Haley xtreme_rewind = B_TRUE;
1908468c413aSTim Haley break;
1909fa9e4066Sahrens case ':':
1910fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for "
1911fa9e4066Sahrens "'%c' option\n"), optopt);
191299653d4eSeschrock usage(B_FALSE);
1913fa9e4066Sahrens break;
1914fa9e4066Sahrens case '?':
1915fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1916fa9e4066Sahrens optopt);
191799653d4eSeschrock usage(B_FALSE);
1918fa9e4066Sahrens }
1919fa9e4066Sahrens }
1920fa9e4066Sahrens
1921fa9e4066Sahrens argc -= optind;
1922fa9e4066Sahrens argv += optind;
1923fa9e4066Sahrens
19242f8aaab3Seschrock if (cachefile && nsearch != 0) {
19252f8aaab3Seschrock (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
19262f8aaab3Seschrock usage(B_FALSE);
19272f8aaab3Seschrock }
19282f8aaab3Seschrock
1929468c413aSTim Haley if ((dryrun || xtreme_rewind) && !do_rewind) {
1930468c413aSTim Haley (void) fprintf(stderr,
1931468c413aSTim Haley gettext("-n or -X only meaningful with -F\n"));
1932468c413aSTim Haley usage(B_FALSE);
1933468c413aSTim Haley }
1934468c413aSTim Haley if (dryrun)
1935468c413aSTim Haley rewind_policy = ZPOOL_TRY_REWIND;
1936468c413aSTim Haley else if (do_rewind)
1937468c413aSTim Haley rewind_policy = ZPOOL_DO_REWIND;
1938468c413aSTim Haley if (xtreme_rewind)
1939468c413aSTim Haley rewind_policy |= ZPOOL_EXTREME_REWIND;
1940468c413aSTim Haley
1941468c413aSTim Haley /* In the future, we can capture further policy and include it here */
1942468c413aSTim Haley if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1943f9af39baSGeorge Wilson nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1944468c413aSTim Haley nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1945468c413aSTim Haley goto error;
1946468c413aSTim Haley
1947fa9e4066Sahrens if (searchdirs == NULL) {
1948fa9e4066Sahrens searchdirs = safe_malloc(sizeof (char *));
1949fa9e4066Sahrens searchdirs[0] = "/dev/dsk";
1950fa9e4066Sahrens nsearch = 1;
1951fa9e4066Sahrens }
1952fa9e4066Sahrens
1953fa9e4066Sahrens /* check argument count */
1954fa9e4066Sahrens if (do_all) {
1955fa9e4066Sahrens if (argc != 0) {
1956fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n"));
195799653d4eSeschrock usage(B_FALSE);
1958fa9e4066Sahrens }
1959fa9e4066Sahrens } else {
1960fa9e4066Sahrens if (argc > 2) {
1961fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n"));
196299653d4eSeschrock usage(B_FALSE);
1963fa9e4066Sahrens }
1964fa9e4066Sahrens
1965fa9e4066Sahrens /*
1966fa9e4066Sahrens * Check for the SYS_CONFIG privilege. We do this explicitly
1967fa9e4066Sahrens * here because otherwise any attempt to discover pools will
1968fa9e4066Sahrens * silently fail.
1969fa9e4066Sahrens */
1970fa9e4066Sahrens if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1971fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot "
1972fa9e4066Sahrens "discover pools: permission denied\n"));
197399653d4eSeschrock free(searchdirs);
1974468c413aSTim Haley nvlist_free(policy);
1975fa9e4066Sahrens return (1);
1976fa9e4066Sahrens }
1977fa9e4066Sahrens }
1978fa9e4066Sahrens
1979fa9e4066Sahrens /*
1980fa9e4066Sahrens * Depending on the arguments given, we do one of the following:
1981fa9e4066Sahrens *
1982fa9e4066Sahrens * <none> Iterate through all pools and display information about
1983fa9e4066Sahrens * each one.
1984fa9e4066Sahrens *
1985fa9e4066Sahrens * -a Iterate through all pools and try to import each one.
1986fa9e4066Sahrens *
1987fa9e4066Sahrens * <id> Find the pool that corresponds to the given GUID/pool
1988fa9e4066Sahrens * name and import that one.
19894c58d714Sdarrenm *
19904c58d714Sdarrenm * -D Above options applies only to destroyed pools.
1991fa9e4066Sahrens */
1992fa9e4066Sahrens if (argc != 0) {
1993fa9e4066Sahrens char *endptr;
1994fa9e4066Sahrens
1995fa9e4066Sahrens errno = 0;
1996fa9e4066Sahrens searchguid = strtoull(argv[0], &endptr, 10);
19979edf9ebdSPrasad Joshi if (errno != 0 || *endptr != '\0') {
1998fa9e4066Sahrens searchname = argv[0];
19999edf9ebdSPrasad Joshi searchguid = 0;
20009edf9ebdSPrasad Joshi }
2001fa9e4066Sahrens found_config = NULL;
2002fa9e4066Sahrens
200324e697d4Sck153898 /*
2004d41c4376SMark J Musante * User specified a name or guid. Ensure it's unique.
200524e697d4Sck153898 */
2006d41c4376SMark J Musante idata.unique = B_TRUE;
200724e697d4Sck153898 }
200824e697d4Sck153898
2009d41c4376SMark J Musante
2010d41c4376SMark J Musante idata.path = searchdirs;
2011d41c4376SMark J Musante idata.paths = nsearch;
2012d41c4376SMark J Musante idata.poolname = searchname;
2013d41c4376SMark J Musante idata.guid = searchguid;
2014d41c4376SMark J Musante idata.cachefile = cachefile;
2015d41c4376SMark J Musante
2016d41c4376SMark J Musante pools = zpool_search_import(g_zfs, &idata);
2017d41c4376SMark J Musante
2018d41c4376SMark J Musante if (pools != NULL && idata.exists &&
2019d41c4376SMark J Musante (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2020d41c4376SMark J Musante (void) fprintf(stderr, gettext("cannot import '%s': "
2021d41c4376SMark J Musante "a pool with that name already exists\n"),
2022d41c4376SMark J Musante argv[0]);
2023d41c4376SMark J Musante (void) fprintf(stderr, gettext("use the form '%s "
2024d41c4376SMark J Musante "<pool | id> <newpool>' to give it a new name\n"),
2025d41c4376SMark J Musante "zpool import");
2026d41c4376SMark J Musante err = 1;
2027d41c4376SMark J Musante } else if (pools == NULL && idata.exists) {
2028d41c4376SMark J Musante (void) fprintf(stderr, gettext("cannot import '%s': "
2029d41c4376SMark J Musante "a pool with that name is already created/imported,\n"),
2030d41c4376SMark J Musante argv[0]);
2031d41c4376SMark J Musante (void) fprintf(stderr, gettext("and no additional pools "
2032d41c4376SMark J Musante "with that name were found\n"));
2033d41c4376SMark J Musante err = 1;
2034d41c4376SMark J Musante } else if (pools == NULL) {
203524e697d4Sck153898 if (argc != 0) {
203624e697d4Sck153898 (void) fprintf(stderr, gettext("cannot import '%s': "
203724e697d4Sck153898 "no such pool available\n"), argv[0]);
203824e697d4Sck153898 }
2039d41c4376SMark J Musante err = 1;
2040d41c4376SMark J Musante }
2041d41c4376SMark J Musante
2042d41c4376SMark J Musante if (err == 1) {
204324e697d4Sck153898 free(searchdirs);
2044468c413aSTim Haley nvlist_free(policy);
204524e697d4Sck153898 return (1);
204624e697d4Sck153898 }
204724e697d4Sck153898
204824e697d4Sck153898 /*
204924e697d4Sck153898 * At this point we have a list of import candidate configs. Even if
205024e697d4Sck153898 * we were searching by pool name or guid, we still need to
205124e697d4Sck153898 * post-process the list to deal with pool state and possible
205224e697d4Sck153898 * duplicate names.
205324e697d4Sck153898 */
2054fa9e4066Sahrens err = 0;
2055fa9e4066Sahrens elem = NULL;
205699653d4eSeschrock first = B_TRUE;
2057fa9e4066Sahrens while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2058fa9e4066Sahrens
2059fa9e4066Sahrens verify(nvpair_value_nvlist(elem, &config) == 0);
2060fa9e4066Sahrens
20614c58d714Sdarrenm verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
20624c58d714Sdarrenm &pool_state) == 0);
20634c58d714Sdarrenm if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
20644c58d714Sdarrenm continue;
20654c58d714Sdarrenm if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
20664c58d714Sdarrenm continue;
20674c58d714Sdarrenm
2068468c413aSTim Haley verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2069468c413aSTim Haley policy) == 0);
2070468c413aSTim Haley
2071fa9e4066Sahrens if (argc == 0) {
2072fa9e4066Sahrens if (first)
207399653d4eSeschrock first = B_FALSE;
20743bb79becSeschrock else if (!do_all)
2075fa9e4066Sahrens (void) printf("\n");
2076fa9e4066Sahrens
2077468c413aSTim Haley if (do_all) {
2078fa9e4066Sahrens err |= do_import(config, NULL, mntopts,
20794b964adaSGeorge Wilson props, flags);
2080468c413aSTim Haley } else {
2081fa9e4066Sahrens show_import(config);
2082468c413aSTim Haley }
2083fa9e4066Sahrens } else if (searchname != NULL) {
2084fa9e4066Sahrens char *name;
2085fa9e4066Sahrens
2086fa9e4066Sahrens /*
2087fa9e4066Sahrens * We are searching for a pool based on name.
2088fa9e4066Sahrens */
2089fa9e4066Sahrens verify(nvlist_lookup_string(config,
2090fa9e4066Sahrens ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2091fa9e4066Sahrens
2092fa9e4066Sahrens if (strcmp(name, searchname) == 0) {
2093fa9e4066Sahrens if (found_config != NULL) {
2094fa9e4066Sahrens (void) fprintf(stderr, gettext(
2095fa9e4066Sahrens "cannot import '%s': more than "
2096fa9e4066Sahrens "one matching pool\n"), searchname);
2097fa9e4066Sahrens (void) fprintf(stderr, gettext(
2098fa9e4066Sahrens "import by numeric ID instead\n"));
209999653d4eSeschrock err = B_TRUE;
2100fa9e4066Sahrens }
2101fa9e4066Sahrens found_config = config;
2102fa9e4066Sahrens }
2103fa9e4066Sahrens } else {
2104fa9e4066Sahrens uint64_t guid;
2105fa9e4066Sahrens
2106fa9e4066Sahrens /*
2107fa9e4066Sahrens * Search for a pool by guid.
2108fa9e4066Sahrens */
2109fa9e4066Sahrens verify(nvlist_lookup_uint64(config,
2110fa9e4066Sahrens ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2111fa9e4066Sahrens
2112fa9e4066Sahrens if (guid == searchguid)
2113fa9e4066Sahrens found_config = config;
2114fa9e4066Sahrens }
2115fa9e4066Sahrens }
2116fa9e4066Sahrens
2117fa9e4066Sahrens /*
2118fa9e4066Sahrens * If we were searching for a specific pool, verify that we found a
2119fa9e4066Sahrens * pool, and then do the import.
2120fa9e4066Sahrens */
2121fa9e4066Sahrens if (argc != 0 && err == 0) {
2122fa9e4066Sahrens if (found_config == NULL) {
2123fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot import '%s': "
2124fa9e4066Sahrens "no such pool available\n"), argv[0]);
212599653d4eSeschrock err = B_TRUE;
2126fa9e4066Sahrens } else {
2127fa9e4066Sahrens err |= do_import(found_config, argc == 1 ? NULL :
21284b964adaSGeorge Wilson argv[1], mntopts, props, flags);
2129fa9e4066Sahrens }
2130fa9e4066Sahrens }
2131fa9e4066Sahrens
2132fa9e4066Sahrens /*
2133fa9e4066Sahrens * If we were just looking for pools, report an error if none were
2134fa9e4066Sahrens * found.
2135fa9e4066Sahrens */
2136fa9e4066Sahrens if (argc == 0 && first)
2137fa9e4066Sahrens (void) fprintf(stderr,
2138fa9e4066Sahrens gettext("no pools available to import\n"));
2139fa9e4066Sahrens
2140ecd6cf80Smarks error:
2141ecd6cf80Smarks nvlist_free(props);
2142fa9e4066Sahrens nvlist_free(pools);
2143468c413aSTim Haley nvlist_free(policy);
214499653d4eSeschrock free(searchdirs);
2145fa9e4066Sahrens
2146fa9e4066Sahrens return (err ? 1 : 0);
2147fa9e4066Sahrens }
2148fa9e4066Sahrens
2149fa9e4066Sahrens typedef struct iostat_cbdata {
21504263d13fSGeorge Wilson boolean_t cb_verbose;
2151fa9e4066Sahrens int cb_namewidth;
21524263d13fSGeorge Wilson int cb_iteration;
21534263d13fSGeorge Wilson zpool_list_t *cb_list;
2154fa9e4066Sahrens } iostat_cbdata_t;
2155fa9e4066Sahrens
2156fa9e4066Sahrens static void
print_iostat_separator(iostat_cbdata_t * cb)2157fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb)
2158fa9e4066Sahrens {
2159fa9e4066Sahrens int i = 0;
2160fa9e4066Sahrens
2161fa9e4066Sahrens for (i = 0; i < cb->cb_namewidth; i++)
2162fa9e4066Sahrens (void) printf("-");
2163fa9e4066Sahrens (void) printf(" ----- ----- ----- ----- ----- -----\n");
2164fa9e4066Sahrens }
2165fa9e4066Sahrens
2166fa9e4066Sahrens static void
print_iostat_header(iostat_cbdata_t * cb)2167fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb)
2168fa9e4066Sahrens {
2169fa9e4066Sahrens (void) printf("%*s capacity operations bandwidth\n",
2170fa9e4066Sahrens cb->cb_namewidth, "");
2171485bbbf5SGeorge Wilson (void) printf("%-*s alloc free read write read write\n",
2172fa9e4066Sahrens cb->cb_namewidth, "pool");
2173fa9e4066Sahrens print_iostat_separator(cb);
2174fa9e4066Sahrens }
2175fa9e4066Sahrens
2176fa9e4066Sahrens /*
2177fa9e4066Sahrens * Display a single statistic.
2178fa9e4066Sahrens */
2179990b4856Slling static void
print_one_stat(uint64_t value)2180fa9e4066Sahrens print_one_stat(uint64_t value)
2181fa9e4066Sahrens {
2182fa9e4066Sahrens char buf[64];
2183fa9e4066Sahrens
2184fa9e4066Sahrens zfs_nicenum(value, buf, sizeof (buf));
2185fa9e4066Sahrens (void) printf(" %5s", buf);
2186fa9e4066Sahrens }
2187fa9e4066Sahrens
2188fa9e4066Sahrens /*
2189fa9e4066Sahrens * Print out all the statistics for the given vdev. This can either be the
2190fa9e4066Sahrens * toplevel configuration, or called recursively. If 'name' is NULL, then this
2191fa9e4066Sahrens * is a verbose output, and we don't want to display the toplevel pool stats.
2192fa9e4066Sahrens */
2193fa9e4066Sahrens void
print_vdev_stats(zpool_handle_t * zhp,const char * name,nvlist_t * oldnv,nvlist_t * newnv,iostat_cbdata_t * cb,int depth)2194c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2195c67d9675Seschrock nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2196fa9e4066Sahrens {
2197fa9e4066Sahrens nvlist_t **oldchild, **newchild;
2198fa9e4066Sahrens uint_t c, children;
2199fa9e4066Sahrens vdev_stat_t *oldvs, *newvs;
2200fa9e4066Sahrens vdev_stat_t zerovs = { 0 };
2201fa9e4066Sahrens uint64_t tdelta;
2202fa9e4066Sahrens double scale;
2203afefbcddSeschrock char *vname;
2204fa9e4066Sahrens
2205fa9e4066Sahrens if (oldnv != NULL) {
22063f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(oldnv,
22073f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2208fa9e4066Sahrens } else {
2209fa9e4066Sahrens oldvs = &zerovs;
2210fa9e4066Sahrens }
2211fa9e4066Sahrens
22123f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2213fa9e4066Sahrens (uint64_t **)&newvs, &c) == 0);
2214fa9e4066Sahrens
2215fa9e4066Sahrens if (strlen(name) + depth > cb->cb_namewidth)
2216fa9e4066Sahrens (void) printf("%*s%s", depth, "", name);
2217fa9e4066Sahrens else
2218fa9e4066Sahrens (void) printf("%*s%s%*s", depth, "", name,
2219fa9e4066Sahrens (int)(cb->cb_namewidth - strlen(name) - depth), "");
2220fa9e4066Sahrens
2221fa9e4066Sahrens tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2222fa9e4066Sahrens
2223fa9e4066Sahrens if (tdelta == 0)
2224fa9e4066Sahrens scale = 1.0;
2225fa9e4066Sahrens else
2226fa9e4066Sahrens scale = (double)NANOSEC / tdelta;
2227fa9e4066Sahrens
2228fa9e4066Sahrens /* only toplevel vdevs have capacity stats */
2229fa9e4066Sahrens if (newvs->vs_space == 0) {
2230fa9e4066Sahrens (void) printf(" - -");
2231fa9e4066Sahrens } else {
2232fa9e4066Sahrens print_one_stat(newvs->vs_alloc);
2233fa9e4066Sahrens print_one_stat(newvs->vs_space - newvs->vs_alloc);
2234fa9e4066Sahrens }
2235fa9e4066Sahrens
2236fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2237fa9e4066Sahrens oldvs->vs_ops[ZIO_TYPE_READ])));
2238fa9e4066Sahrens
2239fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2240fa9e4066Sahrens oldvs->vs_ops[ZIO_TYPE_WRITE])));
2241fa9e4066Sahrens
2242fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2243fa9e4066Sahrens oldvs->vs_bytes[ZIO_TYPE_READ])));
2244fa9e4066Sahrens
2245fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2246fa9e4066Sahrens oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2247fa9e4066Sahrens
2248fa9e4066Sahrens (void) printf("\n");
2249fa9e4066Sahrens
2250fa9e4066Sahrens if (!cb->cb_verbose)
2251fa9e4066Sahrens return;
2252fa9e4066Sahrens
2253fa9e4066Sahrens if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2254fa9e4066Sahrens &newchild, &children) != 0)
2255fa9e4066Sahrens return;
2256fa9e4066Sahrens
2257fa9e4066Sahrens if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2258fa9e4066Sahrens &oldchild, &c) != 0)
2259fa9e4066Sahrens return;
2260fa9e4066Sahrens
2261afefbcddSeschrock for (c = 0; c < children; c++) {
22629d439f90SMike Harsch uint64_t ishole = B_FALSE, islog = B_FALSE;
22633f9d6ad7SLin Ling
22649d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
22659d439f90SMike Harsch &ishole);
22669d439f90SMike Harsch
22679d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
22689d439f90SMike Harsch &islog);
22699d439f90SMike Harsch
22709d439f90SMike Harsch if (ishole || islog)
22713f9d6ad7SLin Ling continue;
22723f9d6ad7SLin Ling
227388ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2274c67d9675Seschrock print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2275afefbcddSeschrock newchild[c], cb, depth + 2);
2276afefbcddSeschrock free(vname);
2277afefbcddSeschrock }
2278fa94a07fSbrendan
2279fa94a07fSbrendan /*
22809d439f90SMike Harsch * Log device section
22819d439f90SMike Harsch */
22829d439f90SMike Harsch
22839d439f90SMike Harsch if (num_logs(newnv) > 0) {
22849d439f90SMike Harsch (void) printf("%-*s - - - - - "
22859d439f90SMike Harsch "-\n", cb->cb_namewidth, "logs");
22869d439f90SMike Harsch
22879d439f90SMike Harsch for (c = 0; c < children; c++) {
22889d439f90SMike Harsch uint64_t islog = B_FALSE;
22899d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c],
22909d439f90SMike Harsch ZPOOL_CONFIG_IS_LOG, &islog);
22919d439f90SMike Harsch
22929d439f90SMike Harsch if (islog) {
22939d439f90SMike Harsch vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
22949d439f90SMike Harsch B_FALSE);
22959d439f90SMike Harsch print_vdev_stats(zhp, vname, oldnv ?
22969d439f90SMike Harsch oldchild[c] : NULL, newchild[c],
22979d439f90SMike Harsch cb, depth + 2);
22989d439f90SMike Harsch free(vname);
22999d439f90SMike Harsch }
23009d439f90SMike Harsch }
23019d439f90SMike Harsch
23029d439f90SMike Harsch }
23039d439f90SMike Harsch
23049d439f90SMike Harsch /*
2305fa94a07fSbrendan * Include level 2 ARC devices in iostat output
2306fa94a07fSbrendan */
2307fa94a07fSbrendan if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2308fa94a07fSbrendan &newchild, &children) != 0)
2309fa94a07fSbrendan return;
2310fa94a07fSbrendan
2311fa94a07fSbrendan if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2312fa94a07fSbrendan &oldchild, &c) != 0)
2313fa94a07fSbrendan return;
2314fa94a07fSbrendan
2315fa94a07fSbrendan if (children > 0) {
2316fa94a07fSbrendan (void) printf("%-*s - - - - - "
2317fa94a07fSbrendan "-\n", cb->cb_namewidth, "cache");
2318fa94a07fSbrendan for (c = 0; c < children; c++) {
231988ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
232088ecc943SGeorge Wilson B_FALSE);
2321fa94a07fSbrendan print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2322fa94a07fSbrendan newchild[c], cb, depth + 2);
2323fa94a07fSbrendan free(vname);
2324fa94a07fSbrendan }
2325fa94a07fSbrendan }
2326fa9e4066Sahrens }
2327fa9e4066Sahrens
2328088e9d47Seschrock static int
refresh_iostat(zpool_handle_t * zhp,void * data)2329088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data)
2330088e9d47Seschrock {
2331088e9d47Seschrock iostat_cbdata_t *cb = data;
233294de1d4cSeschrock boolean_t missing;
2333088e9d47Seschrock
2334088e9d47Seschrock /*
2335088e9d47Seschrock * If the pool has disappeared, remove it from the list and continue.
2336088e9d47Seschrock */
233794de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0)
233894de1d4cSeschrock return (-1);
233994de1d4cSeschrock
234094de1d4cSeschrock if (missing)
2341088e9d47Seschrock pool_list_remove(cb->cb_list, zhp);
2342088e9d47Seschrock
2343088e9d47Seschrock return (0);
2344088e9d47Seschrock }
2345088e9d47Seschrock
2346fa9e4066Sahrens /*
2347fa9e4066Sahrens * Callback to print out the iostats for the given pool.
2348fa9e4066Sahrens */
2349fa9e4066Sahrens int
print_iostat(zpool_handle_t * zhp,void * data)2350fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data)
2351fa9e4066Sahrens {
2352fa9e4066Sahrens iostat_cbdata_t *cb = data;
2353fa9e4066Sahrens nvlist_t *oldconfig, *newconfig;
2354fa9e4066Sahrens nvlist_t *oldnvroot, *newnvroot;
2355fa9e4066Sahrens
2356088e9d47Seschrock newconfig = zpool_get_config(zhp, &oldconfig);
2357fa9e4066Sahrens
2358088e9d47Seschrock if (cb->cb_iteration == 1)
2359fa9e4066Sahrens oldconfig = NULL;
2360fa9e4066Sahrens
2361fa9e4066Sahrens verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2362fa9e4066Sahrens &newnvroot) == 0);
2363fa9e4066Sahrens
2364088e9d47Seschrock if (oldconfig == NULL)
2365fa9e4066Sahrens oldnvroot = NULL;
2366088e9d47Seschrock else
2367088e9d47Seschrock verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2368088e9d47Seschrock &oldnvroot) == 0);
2369fa9e4066Sahrens
2370fa9e4066Sahrens /*
2371fa9e4066Sahrens * Print out the statistics for the pool.
2372fa9e4066Sahrens */
2373c67d9675Seschrock print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2374fa9e4066Sahrens
2375fa9e4066Sahrens if (cb->cb_verbose)
2376fa9e4066Sahrens print_iostat_separator(cb);
2377fa9e4066Sahrens
2378fa9e4066Sahrens return (0);
2379fa9e4066Sahrens }
2380fa9e4066Sahrens
2381fa9e4066Sahrens int
get_namewidth(zpool_handle_t * zhp,void * data)2382fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data)
2383fa9e4066Sahrens {
2384fa9e4066Sahrens iostat_cbdata_t *cb = data;
2385fa9e4066Sahrens nvlist_t *config, *nvroot;
2386fa9e4066Sahrens
2387088e9d47Seschrock if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2388fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2389fa9e4066Sahrens &nvroot) == 0);
2390fa9e4066Sahrens if (!cb->cb_verbose)
2391fa9e4066Sahrens cb->cb_namewidth = strlen(zpool_get_name(zhp));
2392fa9e4066Sahrens else
2393e1d5e507SFrederik Wessels cb->cb_namewidth = max_width(zhp, nvroot, 0,
2394e1d5e507SFrederik Wessels cb->cb_namewidth);
2395fa9e4066Sahrens }
2396fa9e4066Sahrens
2397fa9e4066Sahrens /*
2398fa9e4066Sahrens * The width must fall into the range [10,38]. The upper limit is the
2399fa9e4066Sahrens * maximum we can have and still fit in 80 columns.
2400fa9e4066Sahrens */
2401fa9e4066Sahrens if (cb->cb_namewidth < 10)
2402fa9e4066Sahrens cb->cb_namewidth = 10;
2403fa9e4066Sahrens if (cb->cb_namewidth > 38)
2404fa9e4066Sahrens cb->cb_namewidth = 38;
2405fa9e4066Sahrens
2406fa9e4066Sahrens return (0);
2407fa9e4066Sahrens }
2408fa9e4066Sahrens
2409fa9e4066Sahrens /*
24103f9d6ad7SLin Ling * Parse the input string, get the 'interval' and 'count' value if there is one.
2411fa9e4066Sahrens */
24123f9d6ad7SLin Ling static void
get_interval_count(int * argcp,char ** argv,unsigned long * iv,unsigned long * cnt)24133f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv,
24143f9d6ad7SLin Ling unsigned long *cnt)
2415fa9e4066Sahrens {
2416fa9e4066Sahrens unsigned long interval = 0, count = 0;
24173f9d6ad7SLin Ling int argc = *argcp, errno;
2418fa9e4066Sahrens
2419fa9e4066Sahrens /*
2420fa9e4066Sahrens * Determine if the last argument is an integer or a pool name
2421fa9e4066Sahrens */
2422fa9e4066Sahrens if (argc > 0 && isdigit(argv[argc - 1][0])) {
2423fa9e4066Sahrens char *end;
2424fa9e4066Sahrens
2425fa9e4066Sahrens errno = 0;
2426fa9e4066Sahrens interval = strtoul(argv[argc - 1], &end, 10);
2427fa9e4066Sahrens
2428fa9e4066Sahrens if (*end == '\0' && errno == 0) {
2429fa9e4066Sahrens if (interval == 0) {
2430fa9e4066Sahrens (void) fprintf(stderr, gettext("interval "
2431fa9e4066Sahrens "cannot be zero\n"));
243299653d4eSeschrock usage(B_FALSE);
2433fa9e4066Sahrens }
2434fa9e4066Sahrens /*
2435fa9e4066Sahrens * Ignore the last parameter
2436fa9e4066Sahrens */
2437fa9e4066Sahrens argc--;
2438fa9e4066Sahrens } else {
2439fa9e4066Sahrens /*
2440fa9e4066Sahrens * If this is not a valid number, just plow on. The
2441fa9e4066Sahrens * user will get a more informative error message later
2442fa9e4066Sahrens * on.
2443fa9e4066Sahrens */
2444fa9e4066Sahrens interval = 0;
2445fa9e4066Sahrens }
2446fa9e4066Sahrens }
2447fa9e4066Sahrens
2448fa9e4066Sahrens /*
2449fa9e4066Sahrens * If the last argument is also an integer, then we have both a count
24503f9d6ad7SLin Ling * and an interval.
2451fa9e4066Sahrens */
2452fa9e4066Sahrens if (argc > 0 && isdigit(argv[argc - 1][0])) {
2453fa9e4066Sahrens char *end;
2454fa9e4066Sahrens
2455fa9e4066Sahrens errno = 0;
2456fa9e4066Sahrens count = interval;
2457fa9e4066Sahrens interval = strtoul(argv[argc - 1], &end, 10);
2458fa9e4066Sahrens
2459fa9e4066Sahrens if (*end == '\0' && errno == 0) {
2460fa9e4066Sahrens if (interval == 0) {
2461fa9e4066Sahrens (void) fprintf(stderr, gettext("interval "
2462fa9e4066Sahrens "cannot be zero\n"));
246399653d4eSeschrock usage(B_FALSE);
2464fa9e4066Sahrens }
2465fa9e4066Sahrens
2466fa9e4066Sahrens /*
2467fa9e4066Sahrens * Ignore the last parameter
2468fa9e4066Sahrens */
2469fa9e4066Sahrens argc--;
2470fa9e4066Sahrens } else {
2471fa9e4066Sahrens interval = 0;
2472fa9e4066Sahrens }
2473fa9e4066Sahrens }
2474fa9e4066Sahrens
24753f9d6ad7SLin Ling *iv = interval;
24763f9d6ad7SLin Ling *cnt = count;
24773f9d6ad7SLin Ling *argcp = argc;
24783f9d6ad7SLin Ling }
24793f9d6ad7SLin Ling
24803f9d6ad7SLin Ling static void
get_timestamp_arg(char c)24813f9d6ad7SLin Ling get_timestamp_arg(char c)
24823f9d6ad7SLin Ling {
24833f9d6ad7SLin Ling if (c == 'u')
24843f9d6ad7SLin Ling timestamp_fmt = UDATE;
24853f9d6ad7SLin Ling else if (c == 'd')
24863f9d6ad7SLin Ling timestamp_fmt = DDATE;
24873f9d6ad7SLin Ling else
24883f9d6ad7SLin Ling usage(B_FALSE);
24893f9d6ad7SLin Ling }
24903f9d6ad7SLin Ling
24913f9d6ad7SLin Ling /*
24923f9d6ad7SLin Ling * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
24933f9d6ad7SLin Ling *
24943f9d6ad7SLin Ling * -v Display statistics for individual vdevs
24953f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format
24963f9d6ad7SLin Ling *
24973f9d6ad7SLin Ling * This command can be tricky because we want to be able to deal with pool
24983f9d6ad7SLin Ling * creation/destruction as well as vdev configuration changes. The bulk of this
24993f9d6ad7SLin Ling * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
25003f9d6ad7SLin Ling * on pool_list_update() to detect the addition of new pools. Configuration
25013f9d6ad7SLin Ling * changes are all handled within libzfs.
25023f9d6ad7SLin Ling */
25033f9d6ad7SLin Ling int
zpool_do_iostat(int argc,char ** argv)25043f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv)
25053f9d6ad7SLin Ling {
25063f9d6ad7SLin Ling int c;
25073f9d6ad7SLin Ling int ret;
25083f9d6ad7SLin Ling int npools;
25093f9d6ad7SLin Ling unsigned long interval = 0, count = 0;
25103f9d6ad7SLin Ling zpool_list_t *list;
25113f9d6ad7SLin Ling boolean_t verbose = B_FALSE;
25123f9d6ad7SLin Ling iostat_cbdata_t cb;
25133f9d6ad7SLin Ling
25143f9d6ad7SLin Ling /* check options */
25153f9d6ad7SLin Ling while ((c = getopt(argc, argv, "T:v")) != -1) {
25163f9d6ad7SLin Ling switch (c) {
25173f9d6ad7SLin Ling case 'T':
25183f9d6ad7SLin Ling get_timestamp_arg(*optarg);
25193f9d6ad7SLin Ling break;
25203f9d6ad7SLin Ling case 'v':
25213f9d6ad7SLin Ling verbose = B_TRUE;
25223f9d6ad7SLin Ling break;
25233f9d6ad7SLin Ling case '?':
25243f9d6ad7SLin Ling (void) fprintf(stderr, gettext("invalid option '%c'\n"),
25253f9d6ad7SLin Ling optopt);
25263f9d6ad7SLin Ling usage(B_FALSE);
25273f9d6ad7SLin Ling }
25283f9d6ad7SLin Ling }
25293f9d6ad7SLin Ling
25303f9d6ad7SLin Ling argc -= optind;
25313f9d6ad7SLin Ling argv += optind;
25323f9d6ad7SLin Ling
25333f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count);
25343f9d6ad7SLin Ling
2535fa9e4066Sahrens /*
2536fa9e4066Sahrens * Construct the list of all interesting pools.
2537fa9e4066Sahrens */
2538fa9e4066Sahrens ret = 0;
2539b1b8ab34Slling if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2540fa9e4066Sahrens return (1);
2541fa9e4066Sahrens
254299653d4eSeschrock if (pool_list_count(list) == 0 && argc != 0) {
254399653d4eSeschrock pool_list_free(list);
2544fa9e4066Sahrens return (1);
254599653d4eSeschrock }
2546fa9e4066Sahrens
2547fa9e4066Sahrens if (pool_list_count(list) == 0 && interval == 0) {
254899653d4eSeschrock pool_list_free(list);
2549fa9e4066Sahrens (void) fprintf(stderr, gettext("no pools available\n"));
2550fa9e4066Sahrens return (1);
2551fa9e4066Sahrens }
2552fa9e4066Sahrens
2553fa9e4066Sahrens /*
2554fa9e4066Sahrens * Enter the main iostat loop.
2555fa9e4066Sahrens */
2556fa9e4066Sahrens cb.cb_list = list;
2557fa9e4066Sahrens cb.cb_verbose = verbose;
2558fa9e4066Sahrens cb.cb_iteration = 0;
2559fa9e4066Sahrens cb.cb_namewidth = 0;
2560fa9e4066Sahrens
2561fa9e4066Sahrens for (;;) {
2562fa9e4066Sahrens pool_list_update(list);
2563fa9e4066Sahrens
2564fa9e4066Sahrens if ((npools = pool_list_count(list)) == 0)
2565fa9e4066Sahrens break;
2566fa9e4066Sahrens
2567fa9e4066Sahrens /*
2568088e9d47Seschrock * Refresh all statistics. This is done as an explicit step
2569088e9d47Seschrock * before calculating the maximum name width, so that any
2570088e9d47Seschrock * configuration changes are properly accounted for.
2571088e9d47Seschrock */
257299653d4eSeschrock (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2573088e9d47Seschrock
2574088e9d47Seschrock /*
2575fa9e4066Sahrens * Iterate over all pools to determine the maximum width
2576fa9e4066Sahrens * for the pool / device name column across all pools.
2577fa9e4066Sahrens */
2578fa9e4066Sahrens cb.cb_namewidth = 0;
257999653d4eSeschrock (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2580fa9e4066Sahrens
258126fd7700SKrishnendu Sadhukhan - Sun Microsystems if (timestamp_fmt != NODATE)
258226fd7700SKrishnendu Sadhukhan - Sun Microsystems print_timestamp(timestamp_fmt);
258326fd7700SKrishnendu Sadhukhan - Sun Microsystems
2584fa9e4066Sahrens /*
2585fa9e4066Sahrens * If it's the first time, or verbose mode, print the header.
2586fa9e4066Sahrens */
2587fa9e4066Sahrens if (++cb.cb_iteration == 1 || verbose)
2588fa9e4066Sahrens print_iostat_header(&cb);
2589fa9e4066Sahrens
259099653d4eSeschrock (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2591fa9e4066Sahrens
2592fa9e4066Sahrens /*
2593fa9e4066Sahrens * If there's more than one pool, and we're not in verbose mode
2594fa9e4066Sahrens * (which prints a separator for us), then print a separator.
2595fa9e4066Sahrens */
2596fa9e4066Sahrens if (npools > 1 && !verbose)
2597fa9e4066Sahrens print_iostat_separator(&cb);
2598fa9e4066Sahrens
2599fa9e4066Sahrens if (verbose)
2600fa9e4066Sahrens (void) printf("\n");
2601fa9e4066Sahrens
260239c23413Seschrock /*
260339c23413Seschrock * Flush the output so that redirection to a file isn't buffered
260439c23413Seschrock * indefinitely.
260539c23413Seschrock */
260639c23413Seschrock (void) fflush(stdout);
260739c23413Seschrock
2608fa9e4066Sahrens if (interval == 0)
2609fa9e4066Sahrens break;
2610fa9e4066Sahrens
2611fa9e4066Sahrens if (count != 0 && --count == 0)
2612fa9e4066Sahrens break;
2613fa9e4066Sahrens
2614fa9e4066Sahrens (void) sleep(interval);
2615fa9e4066Sahrens }
2616fa9e4066Sahrens
2617fa9e4066Sahrens pool_list_free(list);
2618fa9e4066Sahrens
2619fa9e4066Sahrens return (ret);
2620fa9e4066Sahrens }
2621fa9e4066Sahrens
2622fa9e4066Sahrens typedef struct list_cbdata {
26234263d13fSGeorge Wilson boolean_t cb_verbose;
26244263d13fSGeorge Wilson int cb_namewidth;
262599653d4eSeschrock boolean_t cb_scripted;
2626990b4856Slling zprop_list_t *cb_proplist;
2627c58b3526SAdam Stevko boolean_t cb_literal;
2628fa9e4066Sahrens } list_cbdata_t;
2629fa9e4066Sahrens
2630fa9e4066Sahrens /*
2631fa9e4066Sahrens * Given a list of columns to display, output appropriate headers for each one.
2632fa9e4066Sahrens */
2633990b4856Slling static void
print_header(list_cbdata_t * cb)26344263d13fSGeorge Wilson print_header(list_cbdata_t *cb)
2635fa9e4066Sahrens {
26364263d13fSGeorge Wilson zprop_list_t *pl = cb->cb_proplist;
2637ad135b5dSChristopher Siden char headerbuf[ZPOOL_MAXPROPLEN];
2638990b4856Slling const char *header;
2639990b4856Slling boolean_t first = B_TRUE;
2640990b4856Slling boolean_t right_justify;
26414263d13fSGeorge Wilson size_t width = 0;
2642fa9e4066Sahrens
2643990b4856Slling for (; pl != NULL; pl = pl->pl_next) {
26444263d13fSGeorge Wilson width = pl->pl_width;
26454263d13fSGeorge Wilson if (first && cb->cb_verbose) {
26464263d13fSGeorge Wilson /*
26474263d13fSGeorge Wilson * Reset the width to accommodate the verbose listing
26484263d13fSGeorge Wilson * of devices.
26494263d13fSGeorge Wilson */
26504263d13fSGeorge Wilson width = cb->cb_namewidth;
26514263d13fSGeorge Wilson }
26524263d13fSGeorge Wilson
2653990b4856Slling if (!first)
2654fa9e4066Sahrens (void) printf(" ");
2655fa9e4066Sahrens else
2656990b4856Slling first = B_FALSE;
2657fa9e4066Sahrens
2658ad135b5dSChristopher Siden right_justify = B_FALSE;
2659ad135b5dSChristopher Siden if (pl->pl_prop != ZPROP_INVAL) {
2660990b4856Slling header = zpool_prop_column_name(pl->pl_prop);
2661990b4856Slling right_justify = zpool_prop_align_right(pl->pl_prop);
2662ad135b5dSChristopher Siden } else {
2663ad135b5dSChristopher Siden int i;
2664ad135b5dSChristopher Siden
2665ad135b5dSChristopher Siden for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2666ad135b5dSChristopher Siden headerbuf[i] = toupper(pl->pl_user_prop[i]);
2667ad135b5dSChristopher Siden headerbuf[i] = '\0';
2668ad135b5dSChristopher Siden header = headerbuf;
2669ad135b5dSChristopher Siden }
2670990b4856Slling
2671990b4856Slling if (pl->pl_next == NULL && !right_justify)
2672990b4856Slling (void) printf("%s", header);
2673990b4856Slling else if (right_justify)
26744263d13fSGeorge Wilson (void) printf("%*s", width, header);
2675990b4856Slling else
26764263d13fSGeorge Wilson (void) printf("%-*s", width, header);
26774263d13fSGeorge Wilson
2678fa9e4066Sahrens }
2679fa9e4066Sahrens
2680fa9e4066Sahrens (void) printf("\n");
2681fa9e4066Sahrens }
2682fa9e4066Sahrens
2683990b4856Slling /*
2684990b4856Slling * Given a pool and a list of properties, print out all the properties according
2685990b4856Slling * to the described layout.
2686990b4856Slling */
2687990b4856Slling static void
print_pool(zpool_handle_t * zhp,list_cbdata_t * cb)26884263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2689990b4856Slling {
26904263d13fSGeorge Wilson zprop_list_t *pl = cb->cb_proplist;
2691990b4856Slling boolean_t first = B_TRUE;
2692990b4856Slling char property[ZPOOL_MAXPROPLEN];
2693990b4856Slling char *propstr;
2694990b4856Slling boolean_t right_justify;
26954263d13fSGeorge Wilson size_t width;
2696990b4856Slling
2697990b4856Slling for (; pl != NULL; pl = pl->pl_next) {
26984263d13fSGeorge Wilson
26994263d13fSGeorge Wilson width = pl->pl_width;
27004263d13fSGeorge Wilson if (first && cb->cb_verbose) {
27014263d13fSGeorge Wilson /*
27024263d13fSGeorge Wilson * Reset the width to accommodate the verbose listing
27034263d13fSGeorge Wilson * of devices.
27044263d13fSGeorge Wilson */
27054263d13fSGeorge Wilson width = cb->cb_namewidth;
27064263d13fSGeorge Wilson }
27074263d13fSGeorge Wilson
2708990b4856Slling if (!first) {
27094263d13fSGeorge Wilson if (cb->cb_scripted)
2710990b4856Slling (void) printf("\t");
2711990b4856Slling else
2712990b4856Slling (void) printf(" ");
2713990b4856Slling } else {
2714990b4856Slling first = B_FALSE;
2715990b4856Slling }
2716990b4856Slling
2717990b4856Slling right_justify = B_FALSE;
2718990b4856Slling if (pl->pl_prop != ZPROP_INVAL) {
27197a09f97bSGeorge Wilson if (zpool_get_prop(zhp, pl->pl_prop, property,
2720c58b3526SAdam Stevko sizeof (property), NULL, cb->cb_literal) != 0)
2721990b4856Slling propstr = "-";
2722990b4856Slling else
2723990b4856Slling propstr = property;
2724990b4856Slling
2725990b4856Slling right_justify = zpool_prop_align_right(pl->pl_prop);
2726ad135b5dSChristopher Siden } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2727ad135b5dSChristopher Siden zpool_prop_unsupported(pl->pl_user_prop)) &&
2728ad135b5dSChristopher Siden zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2729ad135b5dSChristopher Siden sizeof (property)) == 0) {
2730ad135b5dSChristopher Siden propstr = property;
2731990b4856Slling } else {
2732990b4856Slling propstr = "-";
2733990b4856Slling }
2734990b4856Slling
2735990b4856Slling
2736990b4856Slling /*
2737990b4856Slling * If this is being called in scripted mode, or if this is the
2738990b4856Slling * last column and it is left-justified, don't include a width
2739990b4856Slling * format specifier.
2740990b4856Slling */
27414263d13fSGeorge Wilson if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2742990b4856Slling (void) printf("%s", propstr);
2743990b4856Slling else if (right_justify)
2744990b4856Slling (void) printf("%*s", width, propstr);
2745990b4856Slling else
2746990b4856Slling (void) printf("%-*s", width, propstr);
2747990b4856Slling }
2748990b4856Slling
2749990b4856Slling (void) printf("\n");
2750990b4856Slling }
2751990b4856Slling
27524263d13fSGeorge Wilson static void
print_one_column(zpool_prop_t prop,uint64_t value,boolean_t scripted,boolean_t valid)27537a09f97bSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
27547a09f97bSGeorge Wilson boolean_t valid)
27554263d13fSGeorge Wilson {
27564263d13fSGeorge Wilson char propval[64];
27574263d13fSGeorge Wilson boolean_t fixed;
27584263d13fSGeorge Wilson size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
27594263d13fSGeorge Wilson
27607a09f97bSGeorge Wilson switch (prop) {
27617a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ:
27627a09f97bSGeorge Wilson if (value == 0)
27634263d13fSGeorge Wilson (void) strlcpy(propval, "-", sizeof (propval));
27642e4c9986SGeorge Wilson else
27652e4c9986SGeorge Wilson zfs_nicenum(value, propval, sizeof (propval));
27667a09f97bSGeorge Wilson break;
27677a09f97bSGeorge Wilson case ZPOOL_PROP_FRAGMENTATION:
27687a09f97bSGeorge Wilson if (value == ZFS_FRAG_INVALID) {
27697a09f97bSGeorge Wilson (void) strlcpy(propval, "-", sizeof (propval));
27707a09f97bSGeorge Wilson } else {
27717a09f97bSGeorge Wilson (void) snprintf(propval, sizeof (propval), "%llu%%",
27727a09f97bSGeorge Wilson value);
27737a09f97bSGeorge Wilson }
27747a09f97bSGeorge Wilson break;
27757a09f97bSGeorge Wilson case ZPOOL_PROP_CAPACITY:
27767a09f97bSGeorge Wilson (void) snprintf(propval, sizeof (propval), "%llu%%", value);
27777a09f97bSGeorge Wilson break;
27787a09f97bSGeorge Wilson default:
27797a09f97bSGeorge Wilson zfs_nicenum(value, propval, sizeof (propval));
27807a09f97bSGeorge Wilson }
27817a09f97bSGeorge Wilson
27827a09f97bSGeorge Wilson if (!valid)
27837a09f97bSGeorge Wilson (void) strlcpy(propval, "-", sizeof (propval));
27844263d13fSGeorge Wilson
27854263d13fSGeorge Wilson if (scripted)
27864263d13fSGeorge Wilson (void) printf("\t%s", propval);
27874263d13fSGeorge Wilson else
27884263d13fSGeorge Wilson (void) printf(" %*s", width, propval);
27894263d13fSGeorge Wilson }
27904263d13fSGeorge Wilson
27914263d13fSGeorge Wilson void
print_list_stats(zpool_handle_t * zhp,const char * name,nvlist_t * nv,list_cbdata_t * cb,int depth)27924263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
27934263d13fSGeorge Wilson list_cbdata_t *cb, int depth)
27944263d13fSGeorge Wilson {
27954263d13fSGeorge Wilson nvlist_t **child;
27964263d13fSGeorge Wilson vdev_stat_t *vs;
27974263d13fSGeorge Wilson uint_t c, children;
27984263d13fSGeorge Wilson char *vname;
27994263d13fSGeorge Wilson boolean_t scripted = cb->cb_scripted;
280052244c09SJohn Wren Kennedy uint64_t islog = B_FALSE;
280152244c09SJohn Wren Kennedy boolean_t haslog = B_FALSE;
280252244c09SJohn Wren Kennedy char *dashes = "%-*s - - - - - -\n";
28034263d13fSGeorge Wilson
28044263d13fSGeorge Wilson verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
28054263d13fSGeorge Wilson (uint64_t **)&vs, &c) == 0);
28064263d13fSGeorge Wilson
28074263d13fSGeorge Wilson if (name != NULL) {
28087a09f97bSGeorge Wilson boolean_t toplevel = (vs->vs_space != 0);
28097a09f97bSGeorge Wilson uint64_t cap;
28107a09f97bSGeorge Wilson
28114263d13fSGeorge Wilson if (scripted)
28124263d13fSGeorge Wilson (void) printf("\t%s", name);
28134263d13fSGeorge Wilson else if (strlen(name) + depth > cb->cb_namewidth)
28144263d13fSGeorge Wilson (void) printf("%*s%s", depth, "", name);
28154263d13fSGeorge Wilson else
28164263d13fSGeorge Wilson (void) printf("%*s%s%*s", depth, "", name,
28174263d13fSGeorge Wilson (int)(cb->cb_namewidth - strlen(name) - depth), "");
28184263d13fSGeorge Wilson
28197a09f97bSGeorge Wilson /*
28207a09f97bSGeorge Wilson * Print the properties for the individual vdevs. Some
28217a09f97bSGeorge Wilson * properties are only applicable to toplevel vdevs. The
28227a09f97bSGeorge Wilson * 'toplevel' boolean value is passed to the print_one_column()
28237a09f97bSGeorge Wilson * to indicate that the value is valid.
28247a09f97bSGeorge Wilson */
28257a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
28267a09f97bSGeorge Wilson toplevel);
28277a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
28287a09f97bSGeorge Wilson toplevel);
28297a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
28307a09f97bSGeorge Wilson scripted, toplevel);
28317a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
28327a09f97bSGeorge Wilson B_TRUE);
28332e4c9986SGeorge Wilson print_one_column(ZPOOL_PROP_FRAGMENTATION,
28347a09f97bSGeorge Wilson vs->vs_fragmentation, scripted,
28357a09f97bSGeorge Wilson (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
28367a09f97bSGeorge Wilson cap = (vs->vs_space == 0) ? 0 :
28377a09f97bSGeorge Wilson (vs->vs_alloc * 100 / vs->vs_space);
28387a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
28394263d13fSGeorge Wilson (void) printf("\n");
28404263d13fSGeorge Wilson }
28414263d13fSGeorge Wilson
28424263d13fSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
28434263d13fSGeorge Wilson &child, &children) != 0)
28444263d13fSGeorge Wilson return;
28454263d13fSGeorge Wilson
28464263d13fSGeorge Wilson for (c = 0; c < children; c++) {
28474263d13fSGeorge Wilson uint64_t ishole = B_FALSE;
28484263d13fSGeorge Wilson
28494263d13fSGeorge Wilson if (nvlist_lookup_uint64(child[c],
28504263d13fSGeorge Wilson ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
28514263d13fSGeorge Wilson continue;
28524263d13fSGeorge Wilson
285352244c09SJohn Wren Kennedy if (nvlist_lookup_uint64(child[c],
285452244c09SJohn Wren Kennedy ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
285552244c09SJohn Wren Kennedy haslog = B_TRUE;
285652244c09SJohn Wren Kennedy continue;
285752244c09SJohn Wren Kennedy }
285852244c09SJohn Wren Kennedy
28594263d13fSGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
28604263d13fSGeorge Wilson print_list_stats(zhp, vname, child[c], cb, depth + 2);
28614263d13fSGeorge Wilson free(vname);
28624263d13fSGeorge Wilson }
28634263d13fSGeorge Wilson
286452244c09SJohn Wren Kennedy if (haslog == B_TRUE) {
286552244c09SJohn Wren Kennedy /* LINTED E_SEC_PRINTF_VAR_FMT */
286652244c09SJohn Wren Kennedy (void) printf(dashes, cb->cb_namewidth, "log");
28674263d13fSGeorge Wilson for (c = 0; c < children; c++) {
286852244c09SJohn Wren Kennedy if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
286952244c09SJohn Wren Kennedy &islog) != 0 || !islog)
287052244c09SJohn Wren Kennedy continue;
287152244c09SJohn Wren Kennedy vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
287252244c09SJohn Wren Kennedy print_list_stats(zhp, vname, child[c], cb, depth + 2);
287352244c09SJohn Wren Kennedy free(vname);
287452244c09SJohn Wren Kennedy }
287552244c09SJohn Wren Kennedy }
287652244c09SJohn Wren Kennedy
287752244c09SJohn Wren Kennedy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
287852244c09SJohn Wren Kennedy &child, &children) == 0 && children > 0) {
287952244c09SJohn Wren Kennedy /* LINTED E_SEC_PRINTF_VAR_FMT */
288052244c09SJohn Wren Kennedy (void) printf(dashes, cb->cb_namewidth, "cache");
288152244c09SJohn Wren Kennedy for (c = 0; c < children; c++) {
288252244c09SJohn Wren Kennedy vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
288352244c09SJohn Wren Kennedy print_list_stats(zhp, vname, child[c], cb, depth + 2);
288452244c09SJohn Wren Kennedy free(vname);
288552244c09SJohn Wren Kennedy }
288652244c09SJohn Wren Kennedy }
288752244c09SJohn Wren Kennedy
288852244c09SJohn Wren Kennedy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
288952244c09SJohn Wren Kennedy &children) == 0 && children > 0) {
289052244c09SJohn Wren Kennedy /* LINTED E_SEC_PRINTF_VAR_FMT */
289152244c09SJohn Wren Kennedy (void) printf(dashes, cb->cb_namewidth, "spare");
289252244c09SJohn Wren Kennedy for (c = 0; c < children; c++) {
289352244c09SJohn Wren Kennedy vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
28944263d13fSGeorge Wilson print_list_stats(zhp, vname, child[c], cb, depth + 2);
28954263d13fSGeorge Wilson free(vname);
28964263d13fSGeorge Wilson }
28974263d13fSGeorge Wilson }
28984263d13fSGeorge Wilson }
28994263d13fSGeorge Wilson
29004263d13fSGeorge Wilson
2901990b4856Slling /*
2902990b4856Slling * Generic callback function to list a pool.
2903990b4856Slling */
2904fa9e4066Sahrens int
list_callback(zpool_handle_t * zhp,void * data)2905fa9e4066Sahrens list_callback(zpool_handle_t *zhp, void *data)
2906fa9e4066Sahrens {
2907fa9e4066Sahrens list_cbdata_t *cbp = data;
29084263d13fSGeorge Wilson nvlist_t *config;
29094263d13fSGeorge Wilson nvlist_t *nvroot;
2910fa9e4066Sahrens
29114263d13fSGeorge Wilson config = zpool_get_config(zhp, NULL);
2912fa9e4066Sahrens
29134263d13fSGeorge Wilson print_pool(zhp, cbp);
29144263d13fSGeorge Wilson if (!cbp->cb_verbose)
29154263d13fSGeorge Wilson return (0);
29164263d13fSGeorge Wilson
29174263d13fSGeorge Wilson verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
29184263d13fSGeorge Wilson &nvroot) == 0);
29194263d13fSGeorge Wilson print_list_stats(zhp, NULL, nvroot, cbp, 0);
2920fa9e4066Sahrens
2921fa9e4066Sahrens return (0);
2922fa9e4066Sahrens }
2923fa9e4066Sahrens
2924fa9e4066Sahrens /*
2925c58b3526SAdam Stevko * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2926fa9e4066Sahrens *
2927990b4856Slling * -H Scripted mode. Don't display headers, and separate properties
2928990b4856Slling * by a single tab.
2929990b4856Slling * -o List of properties to display. Defaults to
29307a09f97bSGeorge Wilson * "name,size,allocated,free,expandsize,fragmentation,capacity,"
29317a09f97bSGeorge Wilson * "dedupratio,health,altroot"
2932c58b3526SAdam Stevko * -p Diplay values in parsable (exact) format.
29333f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format
2934fa9e4066Sahrens *
2935fa9e4066Sahrens * List all pools in the system, whether or not they're healthy. Output space
2936fa9e4066Sahrens * statistics for each one, as well as health status summary.
2937fa9e4066Sahrens */
2938fa9e4066Sahrens int
zpool_do_list(int argc,char ** argv)2939fa9e4066Sahrens zpool_do_list(int argc, char **argv)
2940fa9e4066Sahrens {
2941fa9e4066Sahrens int c;
2942fa9e4066Sahrens int ret;
2943fa9e4066Sahrens list_cbdata_t cb = { 0 };
2944990b4856Slling static char default_props[] =
29457a09f97bSGeorge Wilson "name,size,allocated,free,expandsize,fragmentation,capacity,"
29462e4c9986SGeorge Wilson "dedupratio,health,altroot";
2947990b4856Slling char *props = default_props;
29483f9d6ad7SLin Ling unsigned long interval = 0, count = 0;
29494263d13fSGeorge Wilson zpool_list_t *list;
29504263d13fSGeorge Wilson boolean_t first = B_TRUE;
2951fa9e4066Sahrens
2952fa9e4066Sahrens /* check options */
2953c58b3526SAdam Stevko while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
2954fa9e4066Sahrens switch (c) {
2955fa9e4066Sahrens case 'H':
295699653d4eSeschrock cb.cb_scripted = B_TRUE;
2957fa9e4066Sahrens break;
2958fa9e4066Sahrens case 'o':
2959990b4856Slling props = optarg;
2960fa9e4066Sahrens break;
2961c58b3526SAdam Stevko case 'p':
2962c58b3526SAdam Stevko cb.cb_literal = B_TRUE;
2963c58b3526SAdam Stevko break;
29643f9d6ad7SLin Ling case 'T':
29653f9d6ad7SLin Ling get_timestamp_arg(*optarg);
29663f9d6ad7SLin Ling break;
29674263d13fSGeorge Wilson case 'v':
29684263d13fSGeorge Wilson cb.cb_verbose = B_TRUE;
29694263d13fSGeorge Wilson break;
2970fa9e4066Sahrens case ':':
2971fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for "
2972fa9e4066Sahrens "'%c' option\n"), optopt);
297399653d4eSeschrock usage(B_FALSE);
2974fa9e4066Sahrens break;
2975fa9e4066Sahrens case '?':
2976fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2977fa9e4066Sahrens optopt);
297899653d4eSeschrock usage(B_FALSE);
2979fa9e4066Sahrens }
2980fa9e4066Sahrens }
2981fa9e4066Sahrens
2982fa9e4066Sahrens argc -= optind;
2983fa9e4066Sahrens argv += optind;
2984fa9e4066Sahrens
29853f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count);
29863f9d6ad7SLin Ling
2987990b4856Slling if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
298899653d4eSeschrock usage(B_FALSE);
2989fa9e4066Sahrens
29904263d13fSGeorge Wilson for (;;) {
2991cd67d23dSGeorge Wilson if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
2992cd67d23dSGeorge Wilson &ret)) == NULL)
2993cd67d23dSGeorge Wilson return (1);
29944263d13fSGeorge Wilson
29954263d13fSGeorge Wilson if (pool_list_count(list) == 0)
29964263d13fSGeorge Wilson break;
29974263d13fSGeorge Wilson
29984263d13fSGeorge Wilson cb.cb_namewidth = 0;
29994263d13fSGeorge Wilson (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
30004263d13fSGeorge Wilson
30014263d13fSGeorge Wilson if (timestamp_fmt != NODATE)
30024263d13fSGeorge Wilson print_timestamp(timestamp_fmt);
30034263d13fSGeorge Wilson
30044263d13fSGeorge Wilson if (!cb.cb_scripted && (first || cb.cb_verbose)) {
30054263d13fSGeorge Wilson print_header(&cb);
30064263d13fSGeorge Wilson first = B_FALSE;
30074263d13fSGeorge Wilson }
30084263d13fSGeorge Wilson ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
30094263d13fSGeorge Wilson
30103f9d6ad7SLin Ling if (interval == 0)
30113f9d6ad7SLin Ling break;
30123f9d6ad7SLin Ling
30133f9d6ad7SLin Ling if (count != 0 && --count == 0)
30143f9d6ad7SLin Ling break;
30153f9d6ad7SLin Ling
3016cd67d23dSGeorge Wilson pool_list_free(list);
30173f9d6ad7SLin Ling (void) sleep(interval);
30183f9d6ad7SLin Ling }
30193f9d6ad7SLin Ling
3020cd67d23dSGeorge Wilson if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3021cd67d23dSGeorge Wilson (void) printf(gettext("no pools available\n"));
3022cd67d23dSGeorge Wilson ret = 0;
3023cd67d23dSGeorge Wilson }
3024cd67d23dSGeorge Wilson
3025cd67d23dSGeorge Wilson pool_list_free(list);
30263f9d6ad7SLin Ling zprop_free_list(cb.cb_proplist);
3027fa9e4066Sahrens return (ret);
3028fa9e4066Sahrens }
3029fa9e4066Sahrens
3030fa9e4066Sahrens static int
zpool_do_attach_or_replace(int argc,char ** argv,int replacing)3031fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3032fa9e4066Sahrens {
303399653d4eSeschrock boolean_t force = B_FALSE;
3034fa9e4066Sahrens int c;
3035fa9e4066Sahrens nvlist_t *nvroot;
3036fa9e4066Sahrens char *poolname, *old_disk, *new_disk;
3037fa9e4066Sahrens zpool_handle_t *zhp;
303899653d4eSeschrock int ret;
3039fa9e4066Sahrens
3040fa9e4066Sahrens /* check options */
3041fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) {
3042fa9e4066Sahrens switch (c) {
3043fa9e4066Sahrens case 'f':
304499653d4eSeschrock force = B_TRUE;
3045fa9e4066Sahrens break;
3046fa9e4066Sahrens case '?':
3047fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3048fa9e4066Sahrens optopt);
304999653d4eSeschrock usage(B_FALSE);
3050fa9e4066Sahrens }
3051fa9e4066Sahrens }
3052fa9e4066Sahrens
3053fa9e4066Sahrens argc -= optind;
3054fa9e4066Sahrens argv += optind;
3055fa9e4066Sahrens
3056fa9e4066Sahrens /* get pool name and check number of arguments */
3057fa9e4066Sahrens if (argc < 1) {
3058fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n"));
305999653d4eSeschrock usage(B_FALSE);
3060fa9e4066Sahrens }
3061fa9e4066Sahrens
3062fa9e4066Sahrens poolname = argv[0];
3063fa9e4066Sahrens
3064fa9e4066Sahrens if (argc < 2) {
3065fa9e4066Sahrens (void) fprintf(stderr,
3066fa9e4066Sahrens gettext("missing <device> specification\n"));
306799653d4eSeschrock usage(B_FALSE);
3068fa9e4066Sahrens }
3069fa9e4066Sahrens
3070fa9e4066Sahrens old_disk = argv[1];
3071fa9e4066Sahrens
3072fa9e4066Sahrens if (argc < 3) {
3073fa9e4066Sahrens if (!replacing) {
3074fa9e4066Sahrens (void) fprintf(stderr,
3075fa9e4066Sahrens gettext("missing <new_device> specification\n"));
307699653d4eSeschrock usage(B_FALSE);
3077fa9e4066Sahrens }
3078fa9e4066Sahrens new_disk = old_disk;
3079fa9e4066Sahrens argc -= 1;
3080fa9e4066Sahrens argv += 1;
3081fa9e4066Sahrens } else {
3082fa9e4066Sahrens new_disk = argv[2];
3083fa9e4066Sahrens argc -= 2;
3084fa9e4066Sahrens argv += 2;
3085fa9e4066Sahrens }
3086fa9e4066Sahrens
3087fa9e4066Sahrens if (argc > 1) {
3088fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n"));
308999653d4eSeschrock usage(B_FALSE);
3090fa9e4066Sahrens }
3091fa9e4066Sahrens
309299653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3093fa9e4066Sahrens return (1);
3094fa9e4066Sahrens
30958488aeb5Staylor if (zpool_get_config(zhp, NULL) == NULL) {
3096fa9e4066Sahrens (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3097fa9e4066Sahrens poolname);
3098fa9e4066Sahrens zpool_close(zhp);
3099fa9e4066Sahrens return (1);
3100fa9e4066Sahrens }
3101fa9e4066Sahrens
3102705040edSEric Taylor nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3103705040edSEric Taylor argc, argv);
3104fa9e4066Sahrens if (nvroot == NULL) {
3105fa9e4066Sahrens zpool_close(zhp);
3106fa9e4066Sahrens return (1);
3107fa9e4066Sahrens }
3108fa9e4066Sahrens
310999653d4eSeschrock ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
311099653d4eSeschrock
311199653d4eSeschrock nvlist_free(nvroot);
311299653d4eSeschrock zpool_close(zhp);
311399653d4eSeschrock
311499653d4eSeschrock return (ret);
3115fa9e4066Sahrens }
3116fa9e4066Sahrens
3117fa9e4066Sahrens /*
3118fa9e4066Sahrens * zpool replace [-f] <pool> <device> <new_device>
3119fa9e4066Sahrens *
3120fa9e4066Sahrens * -f Force attach, even if <new_device> appears to be in use.
3121fa9e4066Sahrens *
3122fa9e4066Sahrens * Replace <device> with <new_device>.
3123fa9e4066Sahrens */
3124fa9e4066Sahrens /* ARGSUSED */
3125fa9e4066Sahrens int
zpool_do_replace(int argc,char ** argv)3126fa9e4066Sahrens zpool_do_replace(int argc, char **argv)
3127fa9e4066Sahrens {
3128fa9e4066Sahrens return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3129fa9e4066Sahrens }
3130fa9e4066Sahrens
3131fa9e4066Sahrens /*
3132fa9e4066Sahrens * zpool attach [-f] <pool> <device> <new_device>
3133fa9e4066Sahrens *
3134fa9e4066Sahrens * -f Force attach, even if <new_device> appears to be in use.
3135fa9e4066Sahrens *
3136fa9e4066Sahrens * Attach <new_device> to the mirror containing <device>. If <device> is not
3137fa9e4066Sahrens * part of a mirror, then <device> will be transformed into a mirror of
3138fa9e4066Sahrens * <device> and <new_device>. In either case, <new_device> will begin life
3139fa9e4066Sahrens * with a DTL of [0, now], and will immediately begin to resilver itself.
3140fa9e4066Sahrens */
3141fa9e4066Sahrens int
zpool_do_attach(int argc,char ** argv)3142fa9e4066Sahrens zpool_do_attach(int argc, char **argv)
3143fa9e4066Sahrens {
3144fa9e4066Sahrens return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3145fa9e4066Sahrens }
3146fa9e4066Sahrens
3147fa9e4066Sahrens /*
3148fa9e4066Sahrens * zpool detach [-f] <pool> <device>
3149fa9e4066Sahrens *
3150fa9e4066Sahrens * -f Force detach of <device>, even if DTLs argue against it
3151fa9e4066Sahrens * (not supported yet)
3152fa9e4066Sahrens *
3153fa9e4066Sahrens * Detach a device from a mirror. The operation will be refused if <device>
3154fa9e4066Sahrens * is the last device in the mirror, or if the DTLs indicate that this device
3155fa9e4066Sahrens * has the only valid copy of some data.
3156fa9e4066Sahrens */
3157fa9e4066Sahrens /* ARGSUSED */
3158fa9e4066Sahrens int
zpool_do_detach(int argc,char ** argv)3159fa9e4066Sahrens zpool_do_detach(int argc, char **argv)
3160fa9e4066Sahrens {
3161fa9e4066Sahrens int c;
3162fa9e4066Sahrens char *poolname, *path;
3163fa9e4066Sahrens zpool_handle_t *zhp;
316499653d4eSeschrock int ret;
3165fa9e4066Sahrens
3166fa9e4066Sahrens /* check options */
3167fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) {
3168fa9e4066Sahrens switch (c) {
3169fa9e4066Sahrens case 'f':
3170fa9e4066Sahrens case '?':
3171fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3172fa9e4066Sahrens optopt);
317399653d4eSeschrock usage(B_FALSE);
3174fa9e4066Sahrens }
3175fa9e4066Sahrens }
3176fa9e4066Sahrens
3177fa9e4066Sahrens argc -= optind;
3178fa9e4066Sahrens argv += optind;
3179fa9e4066Sahrens
3180fa9e4066Sahrens /* get pool name and check number of arguments */
3181fa9e4066Sahrens if (argc < 1) {
3182fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n"));
318399653d4eSeschrock usage(B_FALSE);
3184fa9e4066Sahrens }
3185fa9e4066Sahrens
3186fa9e4066Sahrens if (argc < 2) {
3187fa9e4066Sahrens (void) fprintf(stderr,
3188fa9e4066Sahrens gettext("missing <device> specification\n"));
318999653d4eSeschrock usage(B_FALSE);
3190fa9e4066Sahrens }
3191fa9e4066Sahrens
3192fa9e4066Sahrens poolname = argv[0];
3193fa9e4066Sahrens path = argv[1];
3194fa9e4066Sahrens
319599653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3196fa9e4066Sahrens return (1);
3197fa9e4066Sahrens
319899653d4eSeschrock ret = zpool_vdev_detach(zhp, path);
319999653d4eSeschrock
320099653d4eSeschrock zpool_close(zhp);
320199653d4eSeschrock
320299653d4eSeschrock return (ret);
3203fa9e4066Sahrens }
3204fa9e4066Sahrens
3205fa9e4066Sahrens /*
32061195e687SMark J Musante * zpool split [-n] [-o prop=val] ...
32071195e687SMark J Musante * [-o mntopt] ...
32081195e687SMark J Musante * [-R altroot] <pool> <newpool> [<device> ...]
32091195e687SMark J Musante *
32101195e687SMark J Musante * -n Do not split the pool, but display the resulting layout if
32111195e687SMark J Musante * it were to be split.
32121195e687SMark J Musante * -o Set property=value, or set mount options.
32131195e687SMark J Musante * -R Mount the split-off pool under an alternate root.
32141195e687SMark J Musante *
32151195e687SMark J Musante * Splits the named pool and gives it the new pool name. Devices to be split
32161195e687SMark J Musante * off may be listed, provided that no more than one device is specified
32171195e687SMark J Musante * per top-level vdev mirror. The newly split pool is left in an exported
32181195e687SMark J Musante * state unless -R is specified.
32191195e687SMark J Musante *
32201195e687SMark J Musante * Restrictions: the top-level of the pool pool must only be made up of
32211195e687SMark J Musante * mirrors; all devices in the pool must be healthy; no device may be
32221195e687SMark J Musante * undergoing a resilvering operation.
32231195e687SMark J Musante */
32241195e687SMark J Musante int
zpool_do_split(int argc,char ** argv)32251195e687SMark J Musante zpool_do_split(int argc, char **argv)
32261195e687SMark J Musante {
32271195e687SMark J Musante char *srcpool, *newpool, *propval;
32281195e687SMark J Musante char *mntopts = NULL;
32291195e687SMark J Musante splitflags_t flags;
32301195e687SMark J Musante int c, ret = 0;
32311195e687SMark J Musante zpool_handle_t *zhp;
32321195e687SMark J Musante nvlist_t *config, *props = NULL;
32331195e687SMark J Musante
32341195e687SMark J Musante flags.dryrun = B_FALSE;
32351195e687SMark J Musante flags.import = B_FALSE;
32361195e687SMark J Musante
32371195e687SMark J Musante /* check options */
32381195e687SMark J Musante while ((c = getopt(argc, argv, ":R:no:")) != -1) {
32391195e687SMark J Musante switch (c) {
32401195e687SMark J Musante case 'R':
32411195e687SMark J Musante flags.import = B_TRUE;
32421195e687SMark J Musante if (add_prop_list(
32431195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
32441195e687SMark J Musante &props, B_TRUE) != 0) {
32451195e687SMark J Musante nvlist_free(props);
32461195e687SMark J Musante usage(B_FALSE);
32471195e687SMark J Musante }
32481195e687SMark J Musante break;
32491195e687SMark J Musante case 'n':
32501195e687SMark J Musante flags.dryrun = B_TRUE;
32511195e687SMark J Musante break;
32521195e687SMark J Musante case 'o':
32531195e687SMark J Musante if ((propval = strchr(optarg, '=')) != NULL) {
32541195e687SMark J Musante *propval = '\0';
32551195e687SMark J Musante propval++;
32561195e687SMark J Musante if (add_prop_list(optarg, propval,
32571195e687SMark J Musante &props, B_TRUE) != 0) {
32581195e687SMark J Musante nvlist_free(props);
32591195e687SMark J Musante usage(B_FALSE);
32601195e687SMark J Musante }
32611195e687SMark J Musante } else {
32621195e687SMark J Musante mntopts = optarg;
32631195e687SMark J Musante }
32641195e687SMark J Musante break;
32651195e687SMark J Musante case ':':
32661195e687SMark J Musante (void) fprintf(stderr, gettext("missing argument for "
32671195e687SMark J Musante "'%c' option\n"), optopt);
32681195e687SMark J Musante usage(B_FALSE);
32691195e687SMark J Musante break;
32701195e687SMark J Musante case '?':
32711195e687SMark J Musante (void) fprintf(stderr, gettext("invalid option '%c'\n"),
32721195e687SMark J Musante optopt);
32731195e687SMark J Musante usage(B_FALSE);
32741195e687SMark J Musante break;
32751195e687SMark J Musante }
32761195e687SMark J Musante }
32771195e687SMark J Musante
32781195e687SMark J Musante if (!flags.import && mntopts != NULL) {
32791195e687SMark J Musante (void) fprintf(stderr, gettext("setting mntopts is only "
32801195e687SMark J Musante "valid when importing the pool\n"));
32811195e687SMark J Musante usage(B_FALSE);
32821195e687SMark J Musante }
32831195e687SMark J Musante
32841195e687SMark J Musante argc -= optind;
32851195e687SMark J Musante argv += optind;
32861195e687SMark J Musante
32871195e687SMark J Musante if (argc < 1) {
32881195e687SMark J Musante (void) fprintf(stderr, gettext("Missing pool name\n"));
32891195e687SMark J Musante usage(B_FALSE);
32901195e687SMark J Musante }
32911195e687SMark J Musante if (argc < 2) {
32921195e687SMark J Musante (void) fprintf(stderr, gettext("Missing new pool name\n"));
32931195e687SMark J Musante usage(B_FALSE);
32941195e687SMark J Musante }
32951195e687SMark J Musante
32961195e687SMark J Musante srcpool = argv[0];
32971195e687SMark J Musante newpool = argv[1];
32981195e687SMark J Musante
32991195e687SMark J Musante argc -= 2;
33001195e687SMark J Musante argv += 2;
33011195e687SMark J Musante
33021195e687SMark J Musante if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
33031195e687SMark J Musante return (1);
33041195e687SMark J Musante
33051195e687SMark J Musante config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
33061195e687SMark J Musante if (config == NULL) {
33071195e687SMark J Musante ret = 1;
33081195e687SMark J Musante } else {
33091195e687SMark J Musante if (flags.dryrun) {
33101195e687SMark J Musante (void) printf(gettext("would create '%s' with the "
33111195e687SMark J Musante "following layout:\n\n"), newpool);
33121195e687SMark J Musante print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
33131195e687SMark J Musante }
33141195e687SMark J Musante nvlist_free(config);
33151195e687SMark J Musante }
33161195e687SMark J Musante
33171195e687SMark J Musante zpool_close(zhp);
33181195e687SMark J Musante
33191195e687SMark J Musante if (ret != 0 || flags.dryrun || !flags.import)
33201195e687SMark J Musante return (ret);
33211195e687SMark J Musante
33221195e687SMark J Musante /*
33231195e687SMark J Musante * The split was successful. Now we need to open the new
33241195e687SMark J Musante * pool and import it.
33251195e687SMark J Musante */
33261195e687SMark J Musante if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
33271195e687SMark J Musante return (1);
33281195e687SMark J Musante if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
33291195e687SMark J Musante zpool_enable_datasets(zhp, mntopts, 0) != 0) {
33301195e687SMark J Musante ret = 1;
3331fe7cd8aaSCyril Plisko (void) fprintf(stderr, gettext("Split was successful, but "
33321195e687SMark J Musante "the datasets could not all be mounted\n"));
33331195e687SMark J Musante (void) fprintf(stderr, gettext("Try doing '%s' with a "
33341195e687SMark J Musante "different altroot\n"), "zpool import");
33351195e687SMark J Musante }
33361195e687SMark J Musante zpool_close(zhp);
33371195e687SMark J Musante
33381195e687SMark J Musante return (ret);
33391195e687SMark J Musante }
33401195e687SMark J Musante
33411195e687SMark J Musante
33421195e687SMark J Musante
33431195e687SMark J Musante /*
3344441d80aaSlling * zpool online <pool> <device> ...
3345fa9e4066Sahrens */
3346fa9e4066Sahrens int
zpool_do_online(int argc,char ** argv)3347fa9e4066Sahrens zpool_do_online(int argc, char **argv)
3348fa9e4066Sahrens {
3349fa9e4066Sahrens int c, i;
3350fa9e4066Sahrens char *poolname;
3351fa9e4066Sahrens zpool_handle_t *zhp;
3352fa9e4066Sahrens int ret = 0;
33533d7072f8Seschrock vdev_state_t newstate;
3354573ca77eSGeorge Wilson int flags = 0;
3355fa9e4066Sahrens
3356fa9e4066Sahrens /* check options */
3357573ca77eSGeorge Wilson while ((c = getopt(argc, argv, "et")) != -1) {
3358fa9e4066Sahrens switch (c) {
3359573ca77eSGeorge Wilson case 'e':
3360573ca77eSGeorge Wilson flags |= ZFS_ONLINE_EXPAND;
3361573ca77eSGeorge Wilson break;
3362fa9e4066Sahrens case 't':
3363fa9e4066Sahrens case '?':
3364fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3365fa9e4066Sahrens optopt);
336699653d4eSeschrock usage(B_FALSE);
3367fa9e4066Sahrens }
3368fa9e4066Sahrens }
3369fa9e4066Sahrens
3370fa9e4066Sahrens argc -= optind;
3371fa9e4066Sahrens argv += optind;
3372fa9e4066Sahrens
3373fa9e4066Sahrens /* get pool name and check number of arguments */
3374fa9e4066Sahrens if (argc < 1) {
3375fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name\n"));
337699653d4eSeschrock usage(B_FALSE);
3377fa9e4066Sahrens }
3378fa9e4066Sahrens if (argc < 2) {
3379fa9e4066Sahrens (void) fprintf(stderr, gettext("missing device name\n"));
338099653d4eSeschrock usage(B_FALSE);
3381fa9e4066Sahrens }
3382fa9e4066Sahrens
3383fa9e4066Sahrens poolname = argv[0];
3384fa9e4066Sahrens
338599653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3386fa9e4066Sahrens return (1);
3387fa9e4066Sahrens
33883d7072f8Seschrock for (i = 1; i < argc; i++) {
3389573ca77eSGeorge Wilson if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
33903d7072f8Seschrock if (newstate != VDEV_STATE_HEALTHY) {
33913d7072f8Seschrock (void) printf(gettext("warning: device '%s' "
33923d7072f8Seschrock "onlined, but remains in faulted state\n"),
3393fa9e4066Sahrens argv[i]);
33943d7072f8Seschrock if (newstate == VDEV_STATE_FAULTED)
33953d7072f8Seschrock (void) printf(gettext("use 'zpool "
33963d7072f8Seschrock "clear' to restore a faulted "
33973d7072f8Seschrock "device\n"));
3398fa9e4066Sahrens else
33993d7072f8Seschrock (void) printf(gettext("use 'zpool "
34003d7072f8Seschrock "replace' to replace devices "
34013d7072f8Seschrock "that are no longer present\n"));
34023d7072f8Seschrock }
34033d7072f8Seschrock } else {
3404fa9e4066Sahrens ret = 1;
34053d7072f8Seschrock }
34063d7072f8Seschrock }
3407fa9e4066Sahrens
340899653d4eSeschrock zpool_close(zhp);
340999653d4eSeschrock
3410fa9e4066Sahrens return (ret);
3411fa9e4066Sahrens }
3412fa9e4066Sahrens
3413fa9e4066Sahrens /*
3414441d80aaSlling * zpool offline [-ft] <pool> <device> ...
3415fa9e4066Sahrens *
3416fa9e4066Sahrens * -f Force the device into the offline state, even if doing
3417fa9e4066Sahrens * so would appear to compromise pool availability.
3418fa9e4066Sahrens * (not supported yet)
3419fa9e4066Sahrens *
3420fa9e4066Sahrens * -t Only take the device off-line temporarily. The offline
3421fa9e4066Sahrens * state will not be persistent across reboots.
3422fa9e4066Sahrens */
3423fa9e4066Sahrens /* ARGSUSED */
3424fa9e4066Sahrens int
zpool_do_offline(int argc,char ** argv)3425fa9e4066Sahrens zpool_do_offline(int argc, char **argv)
3426fa9e4066Sahrens {
3427fa9e4066Sahrens int c, i;
3428fa9e4066Sahrens char *poolname;
3429fa9e4066Sahrens zpool_handle_t *zhp;
343099653d4eSeschrock int ret = 0;
343199653d4eSeschrock boolean_t istmp = B_FALSE;
3432fa9e4066Sahrens
3433fa9e4066Sahrens /* check options */
3434fa9e4066Sahrens while ((c = getopt(argc, argv, "ft")) != -1) {
3435fa9e4066Sahrens switch (c) {
3436fa9e4066Sahrens case 't':
343799653d4eSeschrock istmp = B_TRUE;
3438441d80aaSlling break;
3439441d80aaSlling case 'f':
3440fa9e4066Sahrens case '?':
3441fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3442fa9e4066Sahrens optopt);
344399653d4eSeschrock usage(B_FALSE);
3444fa9e4066Sahrens }
3445fa9e4066Sahrens }
3446fa9e4066Sahrens
3447fa9e4066Sahrens argc -= optind;
3448fa9e4066Sahrens argv += optind;
3449fa9e4066Sahrens
3450fa9e4066Sahrens /* get pool name and check number of arguments */
3451fa9e4066Sahrens if (argc < 1) {
3452fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name\n"));
345399653d4eSeschrock usage(B_FALSE);
3454fa9e4066Sahrens }
3455fa9e4066Sahrens if (argc < 2) {
3456fa9e4066Sahrens (void) fprintf(stderr, gettext("missing device name\n"));
345799653d4eSeschrock usage(B_FALSE);
3458fa9e4066Sahrens }
3459fa9e4066Sahrens
3460fa9e4066Sahrens poolname = argv[0];
3461fa9e4066Sahrens
346299653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3463fa9e4066Sahrens return (1);
3464fa9e4066Sahrens
34653d7072f8Seschrock for (i = 1; i < argc; i++) {
34663d7072f8Seschrock if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3467fa9e4066Sahrens ret = 1;
34683d7072f8Seschrock }
3469fa9e4066Sahrens
347099653d4eSeschrock zpool_close(zhp);
347199653d4eSeschrock
3472fa9e4066Sahrens return (ret);
3473fa9e4066Sahrens }
3474fa9e4066Sahrens
3475ea8dc4b6Seschrock /*
3476ea8dc4b6Seschrock * zpool clear <pool> [device]
3477ea8dc4b6Seschrock *
3478ea8dc4b6Seschrock * Clear all errors associated with a pool or a particular device.
3479ea8dc4b6Seschrock */
3480ea8dc4b6Seschrock int
zpool_do_clear(int argc,char ** argv)3481ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv)
3482ea8dc4b6Seschrock {
3483468c413aSTim Haley int c;
3484ea8dc4b6Seschrock int ret = 0;
3485468c413aSTim Haley boolean_t dryrun = B_FALSE;
3486468c413aSTim Haley boolean_t do_rewind = B_FALSE;
3487468c413aSTim Haley boolean_t xtreme_rewind = B_FALSE;
3488468c413aSTim Haley uint32_t rewind_policy = ZPOOL_NO_REWIND;
3489468c413aSTim Haley nvlist_t *policy = NULL;
3490ea8dc4b6Seschrock zpool_handle_t *zhp;
3491ea8dc4b6Seschrock char *pool, *device;
3492ea8dc4b6Seschrock
3493468c413aSTim Haley /* check options */
3494468c413aSTim Haley while ((c = getopt(argc, argv, "FnX")) != -1) {
3495468c413aSTim Haley switch (c) {
3496468c413aSTim Haley case 'F':
3497468c413aSTim Haley do_rewind = B_TRUE;
3498468c413aSTim Haley break;
3499468c413aSTim Haley case 'n':
3500468c413aSTim Haley dryrun = B_TRUE;
3501468c413aSTim Haley break;
3502468c413aSTim Haley case 'X':
3503468c413aSTim Haley xtreme_rewind = B_TRUE;
3504468c413aSTim Haley break;
3505468c413aSTim Haley case '?':
3506468c413aSTim Haley (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3507468c413aSTim Haley optopt);
3508468c413aSTim Haley usage(B_FALSE);
3509468c413aSTim Haley }
3510468c413aSTim Haley }
3511468c413aSTim Haley
3512468c413aSTim Haley argc -= optind;
3513468c413aSTim Haley argv += optind;
3514468c413aSTim Haley
3515468c413aSTim Haley if (argc < 1) {
3516ea8dc4b6Seschrock (void) fprintf(stderr, gettext("missing pool name\n"));
351799653d4eSeschrock usage(B_FALSE);
3518ea8dc4b6Seschrock }
3519ea8dc4b6Seschrock
3520468c413aSTim Haley if (argc > 2) {
3521ea8dc4b6Seschrock (void) fprintf(stderr, gettext("too many arguments\n"));
352299653d4eSeschrock usage(B_FALSE);
3523ea8dc4b6Seschrock }
3524ea8dc4b6Seschrock
3525468c413aSTim Haley if ((dryrun || xtreme_rewind) && !do_rewind) {
3526468c413aSTim Haley (void) fprintf(stderr,
3527468c413aSTim Haley gettext("-n or -X only meaningful with -F\n"));
3528468c413aSTim Haley usage(B_FALSE);
3529468c413aSTim Haley }
3530468c413aSTim Haley if (dryrun)
3531468c413aSTim Haley rewind_policy = ZPOOL_TRY_REWIND;
3532468c413aSTim Haley else if (do_rewind)
3533468c413aSTim Haley rewind_policy = ZPOOL_DO_REWIND;
3534468c413aSTim Haley if (xtreme_rewind)
3535468c413aSTim Haley rewind_policy |= ZPOOL_EXTREME_REWIND;
3536ea8dc4b6Seschrock
3537468c413aSTim Haley /* In future, further rewind policy choices can be passed along here */
3538468c413aSTim Haley if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3539468c413aSTim Haley nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3540ea8dc4b6Seschrock return (1);
3541ea8dc4b6Seschrock
3542468c413aSTim Haley pool = argv[0];
3543468c413aSTim Haley device = argc == 2 ? argv[1] : NULL;
3544468c413aSTim Haley
3545468c413aSTim Haley if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3546468c413aSTim Haley nvlist_free(policy);
3547468c413aSTim Haley return (1);
3548468c413aSTim Haley }
3549468c413aSTim Haley
3550468c413aSTim Haley if (zpool_clear(zhp, device, policy) != 0)
3551ea8dc4b6Seschrock ret = 1;
3552ea8dc4b6Seschrock
3553ea8dc4b6Seschrock zpool_close(zhp);
3554ea8dc4b6Seschrock
3555468c413aSTim Haley nvlist_free(policy);
3556468c413aSTim Haley
3557ea8dc4b6Seschrock return (ret);
3558ea8dc4b6Seschrock }
3559ea8dc4b6Seschrock
3560e9103aaeSGarrett D'Amore /*
3561e9103aaeSGarrett D'Amore * zpool reguid <pool>
3562e9103aaeSGarrett D'Amore */
3563e9103aaeSGarrett D'Amore int
zpool_do_reguid(int argc,char ** argv)3564e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv)
3565e9103aaeSGarrett D'Amore {
3566e9103aaeSGarrett D'Amore int c;
3567e9103aaeSGarrett D'Amore char *poolname;
3568e9103aaeSGarrett D'Amore zpool_handle_t *zhp;
3569e9103aaeSGarrett D'Amore int ret = 0;
3570e9103aaeSGarrett D'Amore
3571e9103aaeSGarrett D'Amore /* check options */
3572e9103aaeSGarrett D'Amore while ((c = getopt(argc, argv, "")) != -1) {
3573e9103aaeSGarrett D'Amore switch (c) {
3574e9103aaeSGarrett D'Amore case '?':
3575e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3576e9103aaeSGarrett D'Amore optopt);
3577e9103aaeSGarrett D'Amore usage(B_FALSE);
3578e9103aaeSGarrett D'Amore }
3579e9103aaeSGarrett D'Amore }
3580e9103aaeSGarrett D'Amore
3581e9103aaeSGarrett D'Amore argc -= optind;
3582e9103aaeSGarrett D'Amore argv += optind;
3583e9103aaeSGarrett D'Amore
3584e9103aaeSGarrett D'Amore /* get pool name and check number of arguments */
3585e9103aaeSGarrett D'Amore if (argc < 1) {
3586e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("missing pool name\n"));
3587e9103aaeSGarrett D'Amore usage(B_FALSE);
3588e9103aaeSGarrett D'Amore }
3589e9103aaeSGarrett D'Amore
3590e9103aaeSGarrett D'Amore if (argc > 1) {
3591e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("too many arguments\n"));
3592e9103aaeSGarrett D'Amore usage(B_FALSE);
3593e9103aaeSGarrett D'Amore }
3594e9103aaeSGarrett D'Amore
3595e9103aaeSGarrett D'Amore poolname = argv[0];
3596e9103aaeSGarrett D'Amore if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3597e9103aaeSGarrett D'Amore return (1);
3598e9103aaeSGarrett D'Amore
3599e9103aaeSGarrett D'Amore ret = zpool_reguid(zhp);
3600e9103aaeSGarrett D'Amore
3601e9103aaeSGarrett D'Amore zpool_close(zhp);
3602e9103aaeSGarrett D'Amore return (ret);
3603e9103aaeSGarrett D'Amore }
3604e9103aaeSGarrett D'Amore
3605e9103aaeSGarrett D'Amore
36064263d13fSGeorge Wilson /*
36074263d13fSGeorge Wilson * zpool reopen <pool>
36084263d13fSGeorge Wilson *
36094263d13fSGeorge Wilson * Reopen the pool so that the kernel can update the sizes of all vdevs.
36104263d13fSGeorge Wilson */
36114263d13fSGeorge Wilson int
zpool_do_reopen(int argc,char ** argv)36124263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv)
36134263d13fSGeorge Wilson {
361431d7e8faSGeorge Wilson int c;
36154263d13fSGeorge Wilson int ret = 0;
36164263d13fSGeorge Wilson zpool_handle_t *zhp;
36174263d13fSGeorge Wilson char *pool;
36184263d13fSGeorge Wilson
361931d7e8faSGeorge Wilson /* check options */
362031d7e8faSGeorge Wilson while ((c = getopt(argc, argv, "")) != -1) {
362131d7e8faSGeorge Wilson switch (c) {
362231d7e8faSGeorge Wilson case '?':
362331d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("invalid option '%c'\n"),
362431d7e8faSGeorge Wilson optopt);
362531d7e8faSGeorge Wilson usage(B_FALSE);
362631d7e8faSGeorge Wilson }
362731d7e8faSGeorge Wilson }
362831d7e8faSGeorge Wilson
36294263d13fSGeorge Wilson argc--;
36304263d13fSGeorge Wilson argv++;
36314263d13fSGeorge Wilson
363231d7e8faSGeorge Wilson if (argc < 1) {
363331d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("missing pool name\n"));
363431d7e8faSGeorge Wilson usage(B_FALSE);
363531d7e8faSGeorge Wilson }
363631d7e8faSGeorge Wilson
363731d7e8faSGeorge Wilson if (argc > 1) {
363831d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("too many arguments\n"));
363931d7e8faSGeorge Wilson usage(B_FALSE);
364031d7e8faSGeorge Wilson }
36414263d13fSGeorge Wilson
36424263d13fSGeorge Wilson pool = argv[0];
36434263d13fSGeorge Wilson if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
36444263d13fSGeorge Wilson return (1);
36454263d13fSGeorge Wilson
36464263d13fSGeorge Wilson ret = zpool_reopen(zhp);
36474263d13fSGeorge Wilson zpool_close(zhp);
36484263d13fSGeorge Wilson return (ret);
36494263d13fSGeorge Wilson }
36504263d13fSGeorge Wilson
3651fa9e4066Sahrens typedef struct scrub_cbdata {
3652fa9e4066Sahrens int cb_type;
365306eeb2adSek110237 int cb_argc;
365406eeb2adSek110237 char **cb_argv;
3655fa9e4066Sahrens } scrub_cbdata_t;
3656fa9e4066Sahrens
3657fa9e4066Sahrens int
scrub_callback(zpool_handle_t * zhp,void * data)3658fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data)
3659fa9e4066Sahrens {
3660fa9e4066Sahrens scrub_cbdata_t *cb = data;
366106eeb2adSek110237 int err;
3662fa9e4066Sahrens
3663ea8dc4b6Seschrock /*
3664ea8dc4b6Seschrock * Ignore faulted pools.
3665ea8dc4b6Seschrock */
3666ea8dc4b6Seschrock if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3667ea8dc4b6Seschrock (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3668ea8dc4b6Seschrock "currently unavailable\n"), zpool_get_name(zhp));
3669ea8dc4b6Seschrock return (1);
3670ea8dc4b6Seschrock }
3671ea8dc4b6Seschrock
36723f9d6ad7SLin Ling err = zpool_scan(zhp, cb->cb_type);
367306eeb2adSek110237
367406eeb2adSek110237 return (err != 0);
3675fa9e4066Sahrens }
3676fa9e4066Sahrens
3677fa9e4066Sahrens /*
3678fa9e4066Sahrens * zpool scrub [-s] <pool> ...
3679fa9e4066Sahrens *
3680fa9e4066Sahrens * -s Stop. Stops any in-progress scrub.
3681fa9e4066Sahrens */
3682fa9e4066Sahrens int
zpool_do_scrub(int argc,char ** argv)3683fa9e4066Sahrens zpool_do_scrub(int argc, char **argv)
3684fa9e4066Sahrens {
3685fa9e4066Sahrens int c;
3686fa9e4066Sahrens scrub_cbdata_t cb;
3687fa9e4066Sahrens
36883f9d6ad7SLin Ling cb.cb_type = POOL_SCAN_SCRUB;
3689fa9e4066Sahrens
3690fa9e4066Sahrens /* check options */
3691fa9e4066Sahrens while ((c = getopt(argc, argv, "s")) != -1) {
3692fa9e4066Sahrens switch (c) {
3693fa9e4066Sahrens case 's':
36943f9d6ad7SLin Ling cb.cb_type = POOL_SCAN_NONE;
3695fa9e4066Sahrens break;
3696fa9e4066Sahrens case '?':
3697fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3698fa9e4066Sahrens optopt);
369999653d4eSeschrock usage(B_FALSE);
3700fa9e4066Sahrens }
3701fa9e4066Sahrens }
3702fa9e4066Sahrens
370306eeb2adSek110237 cb.cb_argc = argc;
370406eeb2adSek110237 cb.cb_argv = argv;
3705fa9e4066Sahrens argc -= optind;
3706fa9e4066Sahrens argv += optind;
3707fa9e4066Sahrens
3708fa9e4066Sahrens if (argc < 1) {
3709fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n"));
371099653d4eSeschrock usage(B_FALSE);
3711fa9e4066Sahrens }
3712fa9e4066Sahrens
3713b1b8ab34Slling return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3714fa9e4066Sahrens }
3715fa9e4066Sahrens
3716fa9e4066Sahrens typedef struct status_cbdata {
3717fa9e4066Sahrens int cb_count;
3718e9dbad6fSeschrock boolean_t cb_allpools;
371999653d4eSeschrock boolean_t cb_verbose;
372099653d4eSeschrock boolean_t cb_explain;
372199653d4eSeschrock boolean_t cb_first;
37229eb19f4dSGeorge Wilson boolean_t cb_dedup_stats;
3723fa9e4066Sahrens } status_cbdata_t;
3724fa9e4066Sahrens
3725fa9e4066Sahrens /*
3726fa9e4066Sahrens * Print out detailed scrub status.
3727fa9e4066Sahrens */
3728fa9e4066Sahrens void
print_scan_status(pool_scan_stat_t * ps)37293f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps)
3730fa9e4066Sahrens {
37313f9d6ad7SLin Ling time_t start, end;
373273d314ceSLin Ling uint64_t elapsed, mins_left, hours_left;
37333f9d6ad7SLin Ling uint64_t pass_exam, examined, total;
37343f9d6ad7SLin Ling uint_t rate;
3735fa9e4066Sahrens double fraction_done;
37363f9d6ad7SLin Ling char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3737fa9e4066Sahrens
37383f9d6ad7SLin Ling (void) printf(gettext(" scan: "));
3739fa9e4066Sahrens
37403f9d6ad7SLin Ling /* If there's never been a scan, there's not much to say. */
37413f9d6ad7SLin Ling if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
37423f9d6ad7SLin Ling ps->pss_func >= POOL_SCAN_FUNCS) {
3743fa9e4066Sahrens (void) printf(gettext("none requested\n"));
3744fa9e4066Sahrens return;
3745fa9e4066Sahrens }
3746fa9e4066Sahrens
37473f9d6ad7SLin Ling start = ps->pss_start_time;
37483f9d6ad7SLin Ling end = ps->pss_end_time;
37493f9d6ad7SLin Ling zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3750fa9e4066Sahrens
37513f9d6ad7SLin Ling assert(ps->pss_func == POOL_SCAN_SCRUB ||
37523f9d6ad7SLin Ling ps->pss_func == POOL_SCAN_RESILVER);
37533f9d6ad7SLin Ling /*
37543f9d6ad7SLin Ling * Scan is finished or canceled.
37553f9d6ad7SLin Ling */
37563f9d6ad7SLin Ling if (ps->pss_state == DSS_FINISHED) {
37573f9d6ad7SLin Ling uint64_t minutes_taken = (end - start) / 60;
3758b327cd3fSIgor Kozhukhov char *fmt = NULL;
3759fa9e4066Sahrens
37603f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) {
37613f9d6ad7SLin Ling fmt = gettext("scrub repaired %s in %lluh%um with "
37623f9d6ad7SLin Ling "%llu errors on %s");
37633f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) {
37643f9d6ad7SLin Ling fmt = gettext("resilvered %s in %lluh%um with "
37653f9d6ad7SLin Ling "%llu errors on %s");
37663f9d6ad7SLin Ling }
37673f9d6ad7SLin Ling /* LINTED */
37683f9d6ad7SLin Ling (void) printf(fmt, processed_buf,
376918ce54dfSek110237 (u_longlong_t)(minutes_taken / 60),
377018ce54dfSek110237 (uint_t)(minutes_taken % 60),
37713f9d6ad7SLin Ling (u_longlong_t)ps->pss_errors,
37723f9d6ad7SLin Ling ctime((time_t *)&end));
37733f9d6ad7SLin Ling return;
37743f9d6ad7SLin Ling } else if (ps->pss_state == DSS_CANCELED) {
37753f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) {
37763f9d6ad7SLin Ling (void) printf(gettext("scrub canceled on %s"),
37773f9d6ad7SLin Ling ctime(&end));
37783f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) {
37793f9d6ad7SLin Ling (void) printf(gettext("resilver canceled on %s"),
37803f9d6ad7SLin Ling ctime(&end));
37813f9d6ad7SLin Ling }
3782fa9e4066Sahrens return;
3783fa9e4066Sahrens }
3784fa9e4066Sahrens
37853f9d6ad7SLin Ling assert(ps->pss_state == DSS_SCANNING);
3786fa9e4066Sahrens
37873f9d6ad7SLin Ling /*
37883f9d6ad7SLin Ling * Scan is in progress.
37893f9d6ad7SLin Ling */
37903f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) {
37913f9d6ad7SLin Ling (void) printf(gettext("scrub in progress since %s"),
37923f9d6ad7SLin Ling ctime(&start));
37933f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) {
37943f9d6ad7SLin Ling (void) printf(gettext("resilver in progress since %s"),
37953f9d6ad7SLin Ling ctime(&start));
37963f9d6ad7SLin Ling }
37973f9d6ad7SLin Ling
37983f9d6ad7SLin Ling examined = ps->pss_examined ? ps->pss_examined : 1;
37993f9d6ad7SLin Ling total = ps->pss_to_examine;
3800fa9e4066Sahrens fraction_done = (double)examined / total;
3801fa9e4066Sahrens
38023f9d6ad7SLin Ling /* elapsed time for this pass */
38033f9d6ad7SLin Ling elapsed = time(NULL) - ps->pss_pass_start;
38043f9d6ad7SLin Ling elapsed = elapsed ? elapsed : 1;
38053f9d6ad7SLin Ling pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
38063f9d6ad7SLin Ling rate = pass_exam / elapsed;
38073f9d6ad7SLin Ling rate = rate ? rate : 1;
38083f9d6ad7SLin Ling mins_left = ((total - examined) / rate) / 60;
380973d314ceSLin Ling hours_left = mins_left / 60;
38103f9d6ad7SLin Ling
38113f9d6ad7SLin Ling zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
38123f9d6ad7SLin Ling zfs_nicenum(total, total_buf, sizeof (total_buf));
38133f9d6ad7SLin Ling zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
38143f9d6ad7SLin Ling
381573d314ceSLin Ling /*
381673d314ceSLin Ling * do not print estimated time if hours_left is more than 30 days
381773d314ceSLin Ling */
381873d314ceSLin Ling (void) printf(gettext(" %s scanned out of %s at %s/s"),
381973d314ceSLin Ling examined_buf, total_buf, rate_buf);
382073d314ceSLin Ling if (hours_left < (30 * 24)) {
382173d314ceSLin Ling (void) printf(gettext(", %lluh%um to go\n"),
382273d314ceSLin Ling (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
382373d314ceSLin Ling } else {
382473d314ceSLin Ling (void) printf(gettext(
382573d314ceSLin Ling ", (scan is slow, no estimated time)\n"));
382673d314ceSLin Ling }
38273f9d6ad7SLin Ling
38283f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_RESILVER) {
38293f9d6ad7SLin Ling (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
38303f9d6ad7SLin Ling processed_buf, 100 * fraction_done);
38313f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_SCRUB) {
38323f9d6ad7SLin Ling (void) printf(gettext(" %s repaired, %.2f%% done\n"),
38333f9d6ad7SLin Ling processed_buf, 100 * fraction_done);
38343f9d6ad7SLin Ling }
3835fa9e4066Sahrens }
3836fa9e4066Sahrens
3837ea8dc4b6Seschrock static void
print_error_log(zpool_handle_t * zhp)3838ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp)
3839ea8dc4b6Seschrock {
384075519f38Sek110237 nvlist_t *nverrlist = NULL;
384155434c77Sek110237 nvpair_t *elem;
384255434c77Sek110237 char *pathname;
384355434c77Sek110237 size_t len = MAXPATHLEN * 2;
3844ea8dc4b6Seschrock
384555434c77Sek110237 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3846ea8dc4b6Seschrock (void) printf("errors: List of errors unavailable "
3847ea8dc4b6Seschrock "(insufficient privileges)\n");
3848ea8dc4b6Seschrock return;
3849ea8dc4b6Seschrock }
3850ea8dc4b6Seschrock
385155434c77Sek110237 (void) printf("errors: Permanent errors have been "
385255434c77Sek110237 "detected in the following files:\n\n");
3853ea8dc4b6Seschrock
385455434c77Sek110237 pathname = safe_malloc(len);
385555434c77Sek110237 elem = NULL;
385655434c77Sek110237 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
385755434c77Sek110237 nvlist_t *nv;
385855434c77Sek110237 uint64_t dsobj, obj;
3859ea8dc4b6Seschrock
386055434c77Sek110237 verify(nvpair_value_nvlist(elem, &nv) == 0);
386155434c77Sek110237 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
386255434c77Sek110237 &dsobj) == 0);
386355434c77Sek110237 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
386455434c77Sek110237 &obj) == 0);
386555434c77Sek110237 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
386655434c77Sek110237 (void) printf("%7s %s\n", "", pathname);
3867ea8dc4b6Seschrock }
386855434c77Sek110237 free(pathname);
386955434c77Sek110237 nvlist_free(nverrlist);
3870ea8dc4b6Seschrock }
3871ea8dc4b6Seschrock
387299653d4eSeschrock static void
print_spares(zpool_handle_t * zhp,nvlist_t ** spares,uint_t nspares,int namewidth)387399653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
387499653d4eSeschrock int namewidth)
387599653d4eSeschrock {
387699653d4eSeschrock uint_t i;
387799653d4eSeschrock char *name;
387899653d4eSeschrock
387999653d4eSeschrock if (nspares == 0)
388099653d4eSeschrock return;
388199653d4eSeschrock
388299653d4eSeschrock (void) printf(gettext("\tspares\n"));
388399653d4eSeschrock
388499653d4eSeschrock for (i = 0; i < nspares; i++) {
388588ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
388699653d4eSeschrock print_status_config(zhp, name, spares[i],
3887aa8cf21aSNeil Perrin namewidth, 2, B_TRUE);
388899653d4eSeschrock free(name);
388999653d4eSeschrock }
389099653d4eSeschrock }
389199653d4eSeschrock
3892fa94a07fSbrendan static void
print_l2cache(zpool_handle_t * zhp,nvlist_t ** l2cache,uint_t nl2cache,int namewidth)3893fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3894fa94a07fSbrendan int namewidth)
3895fa94a07fSbrendan {
3896fa94a07fSbrendan uint_t i;
3897fa94a07fSbrendan char *name;
3898fa94a07fSbrendan
3899fa94a07fSbrendan if (nl2cache == 0)
3900fa94a07fSbrendan return;
3901fa94a07fSbrendan
3902fa94a07fSbrendan (void) printf(gettext("\tcache\n"));
3903fa94a07fSbrendan
3904fa94a07fSbrendan for (i = 0; i < nl2cache; i++) {
390588ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3906fa94a07fSbrendan print_status_config(zhp, name, l2cache[i],
3907aa8cf21aSNeil Perrin namewidth, 2, B_FALSE);
3908aa8cf21aSNeil Perrin free(name);
3909aa8cf21aSNeil Perrin }
3910aa8cf21aSNeil Perrin }
3911aa8cf21aSNeil Perrin
39129eb19f4dSGeorge Wilson static void
print_dedup_stats(nvlist_t * config)39139eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config)
39149eb19f4dSGeorge Wilson {
39159eb19f4dSGeorge Wilson ddt_histogram_t *ddh;
39169eb19f4dSGeorge Wilson ddt_stat_t *dds;
39179eb19f4dSGeorge Wilson ddt_object_t *ddo;
39189eb19f4dSGeorge Wilson uint_t c;
39199eb19f4dSGeorge Wilson
39209eb19f4dSGeorge Wilson /*
39219eb19f4dSGeorge Wilson * If the pool was faulted then we may not have been able to
39222384d9f8SGeorge Wilson * obtain the config. Otherwise, if we have anything in the dedup
39239eb19f4dSGeorge Wilson * table continue processing the stats.
39249eb19f4dSGeorge Wilson */
39259eb19f4dSGeorge Wilson if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3926ce72e614SYuri Pankov (uint64_t **)&ddo, &c) != 0)
39279eb19f4dSGeorge Wilson return;
39289eb19f4dSGeorge Wilson
39299eb19f4dSGeorge Wilson (void) printf("\n");
3930ce72e614SYuri Pankov (void) printf(gettext(" dedup: "));
3931ce72e614SYuri Pankov if (ddo->ddo_count == 0) {
3932ce72e614SYuri Pankov (void) printf(gettext("no DDT entries\n"));
3933ce72e614SYuri Pankov return;
3934ce72e614SYuri Pankov }
3935ce72e614SYuri Pankov
39369eb19f4dSGeorge Wilson (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
39379eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_count,
39389eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_dspace,
39399eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_mspace);
39409eb19f4dSGeorge Wilson
39419eb19f4dSGeorge Wilson verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
39429eb19f4dSGeorge Wilson (uint64_t **)&dds, &c) == 0);
39439eb19f4dSGeorge Wilson verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
39449eb19f4dSGeorge Wilson (uint64_t **)&ddh, &c) == 0);
39459eb19f4dSGeorge Wilson zpool_dump_ddt(dds, ddh);
39469eb19f4dSGeorge Wilson }
39479eb19f4dSGeorge Wilson
3948aa8cf21aSNeil Perrin /*
3949fa9e4066Sahrens * Display a summary of pool status. Displays a summary such as:
3950fa9e4066Sahrens *
3951fa9e4066Sahrens * pool: tank
3952fa9e4066Sahrens * status: DEGRADED
3953fa9e4066Sahrens * reason: One or more devices ...
3954654b400cSJoshua M. Clulow * see: http://illumos.org/msg/ZFS-xxxx-01
3955fa9e4066Sahrens * config:
3956fa9e4066Sahrens * mirror DEGRADED
3957fa9e4066Sahrens * c1t0d0 OK
3958ea8dc4b6Seschrock * c2t0d0 UNAVAIL
3959fa9e4066Sahrens *
3960fa9e4066Sahrens * When given the '-v' option, we print out the complete config. If the '-e'
3961fa9e4066Sahrens * option is specified, then we print out error rate information as well.
3962fa9e4066Sahrens */
3963fa9e4066Sahrens int
status_callback(zpool_handle_t * zhp,void * data)3964fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data)
3965fa9e4066Sahrens {
3966fa9e4066Sahrens status_cbdata_t *cbp = data;
3967fa9e4066Sahrens nvlist_t *config, *nvroot;
3968fa9e4066Sahrens char *msgid;
3969fa9e4066Sahrens int reason;
397046657f8dSmmusante const char *health;
397146657f8dSmmusante uint_t c;
397246657f8dSmmusante vdev_stat_t *vs;
3973fa9e4066Sahrens
3974088e9d47Seschrock config = zpool_get_config(zhp, NULL);
3975fa9e4066Sahrens reason = zpool_get_status(zhp, &msgid);
3976fa9e4066Sahrens
3977fa9e4066Sahrens cbp->cb_count++;
3978fa9e4066Sahrens
3979fa9e4066Sahrens /*
3980fa9e4066Sahrens * If we were given 'zpool status -x', only report those pools with
3981fa9e4066Sahrens * problems.
3982fa9e4066Sahrens */
3983b3a6f804STim Connors if (cbp->cb_explain &&
3984b3a6f804STim Connors (reason == ZPOOL_STATUS_OK ||
3985b3a6f804STim Connors reason == ZPOOL_STATUS_VERSION_OLDER ||
3986b3a6f804STim Connors reason == ZPOOL_STATUS_FEAT_DISABLED)) {
3987e9dbad6fSeschrock if (!cbp->cb_allpools) {
3988e9dbad6fSeschrock (void) printf(gettext("pool '%s' is healthy\n"),
3989e9dbad6fSeschrock zpool_get_name(zhp));
3990e9dbad6fSeschrock if (cbp->cb_first)
3991e9dbad6fSeschrock cbp->cb_first = B_FALSE;
3992e9dbad6fSeschrock }
3993fa9e4066Sahrens return (0);
3994e9dbad6fSeschrock }
3995fa9e4066Sahrens
3996fa9e4066Sahrens if (cbp->cb_first)
399799653d4eSeschrock cbp->cb_first = B_FALSE;
3998fa9e4066Sahrens else
3999fa9e4066Sahrens (void) printf("\n");
4000fa9e4066Sahrens
400146657f8dSmmusante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
400246657f8dSmmusante &nvroot) == 0);
40033f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
400446657f8dSmmusante (uint64_t **)&vs, &c) == 0);
4005990b4856Slling health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4006fa9e4066Sahrens
4007fa9e4066Sahrens (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4008fa9e4066Sahrens (void) printf(gettext(" state: %s\n"), health);
4009fa9e4066Sahrens
4010fa9e4066Sahrens switch (reason) {
4011fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_R:
4012fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not "
4013fa9e4066Sahrens "be opened. Sufficient replicas exist for\n\tthe pool to "
4014fa9e4066Sahrens "continue functioning in a degraded state.\n"));
4015fa9e4066Sahrens (void) printf(gettext("action: Attach the missing device and "
4016fa9e4066Sahrens "online it using 'zpool online'.\n"));
4017fa9e4066Sahrens break;
4018fa9e4066Sahrens
4019fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_NR:
4020fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not "
4021fa9e4066Sahrens "be opened. There are insufficient\n\treplicas for the "
4022fa9e4066Sahrens "pool to continue functioning.\n"));
4023fa9e4066Sahrens (void) printf(gettext("action: Attach the missing device and "
4024fa9e4066Sahrens "online it using 'zpool online'.\n"));
4025fa9e4066Sahrens break;
4026fa9e4066Sahrens
4027fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_R:
4028fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not "
4029fa9e4066Sahrens "be used because the label is missing or\n\tinvalid. "
4030fa9e4066Sahrens "Sufficient replicas exist for the pool to continue\n\t"
4031fa9e4066Sahrens "functioning in a degraded state.\n"));
4032fa9e4066Sahrens (void) printf(gettext("action: Replace the device using "
4033fa9e4066Sahrens "'zpool replace'.\n"));
4034fa9e4066Sahrens break;
4035fa9e4066Sahrens
4036fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4037fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not "
4038b1b8ab34Slling "be used because the label is missing \n\tor invalid. "
4039fa9e4066Sahrens "There are insufficient replicas for the pool to "
4040fa9e4066Sahrens "continue\n\tfunctioning.\n"));
4041468c413aSTim Haley zpool_explain_recover(zpool_get_handle(zhp),
4042468c413aSTim Haley zpool_get_name(zhp), reason, config);
4043fa9e4066Sahrens break;
4044fa9e4066Sahrens
4045fa9e4066Sahrens case ZPOOL_STATUS_FAILING_DEV:
4046fa9e4066Sahrens (void) printf(gettext("status: One or more devices has "
4047fa9e4066Sahrens "experienced an unrecoverable error. An\n\tattempt was "
4048fa9e4066Sahrens "made to correct the error. Applications are "
4049fa9e4066Sahrens "unaffected.\n"));
4050fa9e4066Sahrens (void) printf(gettext("action: Determine if the device needs "
4051fa9e4066Sahrens "to be replaced, and clear the errors\n\tusing "
4052ea8dc4b6Seschrock "'zpool clear' or replace the device with 'zpool "
4053fa9e4066Sahrens "replace'.\n"));
4054fa9e4066Sahrens break;
4055fa9e4066Sahrens
4056fa9e4066Sahrens case ZPOOL_STATUS_OFFLINE_DEV:
4057fa9e4066Sahrens (void) printf(gettext("status: One or more devices has "
4058d7d4af51Smmusante "been taken offline by the administrator.\n\tSufficient "
4059fa9e4066Sahrens "replicas exist for the pool to continue functioning in "
4060fa9e4066Sahrens "a\n\tdegraded state.\n"));
4061fa9e4066Sahrens (void) printf(gettext("action: Online the device using "
4062fa9e4066Sahrens "'zpool online' or replace the device with\n\t'zpool "
4063fa9e4066Sahrens "replace'.\n"));
4064fa9e4066Sahrens break;
4065fa9e4066Sahrens
4066c25309d4SGeorge Wilson case ZPOOL_STATUS_REMOVED_DEV:
4067c25309d4SGeorge Wilson (void) printf(gettext("status: One or more devices has "
4068c25309d4SGeorge Wilson "been removed by the administrator.\n\tSufficient "
4069c25309d4SGeorge Wilson "replicas exist for the pool to continue functioning in "
4070c25309d4SGeorge Wilson "a\n\tdegraded state.\n"));
4071c25309d4SGeorge Wilson (void) printf(gettext("action: Online the device using "
4072c25309d4SGeorge Wilson "'zpool online' or replace the device with\n\t'zpool "
4073c25309d4SGeorge Wilson "replace'.\n"));
4074c25309d4SGeorge Wilson break;
4075c25309d4SGeorge Wilson
4076fa9e4066Sahrens case ZPOOL_STATUS_RESILVERING:
4077fa9e4066Sahrens (void) printf(gettext("status: One or more devices is "
4078fa9e4066Sahrens "currently being resilvered. The pool will\n\tcontinue "
4079fa9e4066Sahrens "to function, possibly in a degraded state.\n"));
4080fa9e4066Sahrens (void) printf(gettext("action: Wait for the resilver to "
4081fa9e4066Sahrens "complete.\n"));
4082fa9e4066Sahrens break;
4083fa9e4066Sahrens
4084ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_DATA:
4085ea8dc4b6Seschrock (void) printf(gettext("status: One or more devices has "
4086ea8dc4b6Seschrock "experienced an error resulting in data\n\tcorruption. "
4087ea8dc4b6Seschrock "Applications may be affected.\n"));
4088ea8dc4b6Seschrock (void) printf(gettext("action: Restore the file in question "
4089ea8dc4b6Seschrock "if possible. Otherwise restore the\n\tentire pool from "
4090ea8dc4b6Seschrock "backup.\n"));
4091ea8dc4b6Seschrock break;
4092ea8dc4b6Seschrock
4093ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_POOL:
4094ea8dc4b6Seschrock (void) printf(gettext("status: The pool metadata is corrupted "
4095ea8dc4b6Seschrock "and the pool cannot be opened.\n"));
4096468c413aSTim Haley zpool_explain_recover(zpool_get_handle(zhp),
4097468c413aSTim Haley zpool_get_name(zhp), reason, config);
4098ea8dc4b6Seschrock break;
4099ea8dc4b6Seschrock
4100eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_OLDER:
410157221772SChristopher Siden (void) printf(gettext("status: The pool is formatted using a "
410257221772SChristopher Siden "legacy on-disk format. The pool can\n\tstill be used, "
410357221772SChristopher Siden "but some features are unavailable.\n"));
4104eaca9bbdSeschrock (void) printf(gettext("action: Upgrade the pool using 'zpool "
4105eaca9bbdSeschrock "upgrade'. Once this is done, the\n\tpool will no longer "
410657221772SChristopher Siden "be accessible on software that does not support feature\n"
410757221772SChristopher Siden "\tflags.\n"));
4108eaca9bbdSeschrock break;
4109eaca9bbdSeschrock
4110eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER:
4111eaca9bbdSeschrock (void) printf(gettext("status: The pool has been upgraded to a "
4112eaca9bbdSeschrock "newer, incompatible on-disk version.\n\tThe pool cannot "
4113eaca9bbdSeschrock "be accessed on this system.\n"));
4114eaca9bbdSeschrock (void) printf(gettext("action: Access the pool from a system "
4115eaca9bbdSeschrock "running more recent software, or\n\trestore the pool from "
4116eaca9bbdSeschrock "backup.\n"));
4117eaca9bbdSeschrock break;
4118eaca9bbdSeschrock
411957221772SChristopher Siden case ZPOOL_STATUS_FEAT_DISABLED:
412057221772SChristopher Siden (void) printf(gettext("status: Some supported features are not "
412157221772SChristopher Siden "enabled on the pool. The pool can\n\tstill be used, but "
412257221772SChristopher Siden "some features are unavailable.\n"));
412357221772SChristopher Siden (void) printf(gettext("action: Enable all features using "
412457221772SChristopher Siden "'zpool upgrade'. Once this is done,\n\tthe pool may no "
412557221772SChristopher Siden "longer be accessible by software that does not support\n\t"
412657221772SChristopher Siden "the features. See zpool-features(5) for details.\n"));
412757221772SChristopher Siden break;
412857221772SChristopher Siden
4129ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ:
4130ad135b5dSChristopher Siden (void) printf(gettext("status: The pool cannot be accessed on "
4131ad135b5dSChristopher Siden "this system because it uses the\n\tfollowing feature(s) "
4132ad135b5dSChristopher Siden "not supported on this system:\n"));
4133ad135b5dSChristopher Siden zpool_print_unsup_feat(config);
4134ad135b5dSChristopher Siden (void) printf("\n");
4135ad135b5dSChristopher Siden (void) printf(gettext("action: Access the pool from a system "
4136ad135b5dSChristopher Siden "that supports the required feature(s),\n\tor restore the "
4137ad135b5dSChristopher Siden "pool from backup.\n"));
4138ad135b5dSChristopher Siden break;
4139ad135b5dSChristopher Siden
4140ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4141ad135b5dSChristopher Siden (void) printf(gettext("status: The pool can only be accessed "
4142ad135b5dSChristopher Siden "in read-only mode on this system. It\n\tcannot be "
4143ad135b5dSChristopher Siden "accessed in read-write mode because it uses the "
4144ad135b5dSChristopher Siden "following\n\tfeature(s) not supported on this system:\n"));
4145ad135b5dSChristopher Siden zpool_print_unsup_feat(config);
4146ad135b5dSChristopher Siden (void) printf("\n");
4147ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be accessed in "
4148ad135b5dSChristopher Siden "read-write mode. Import the pool with\n"
4149ad135b5dSChristopher Siden "\t\"-o readonly=on\", access the pool from a system that "
4150ad135b5dSChristopher Siden "supports the\n\trequired feature(s), or restore the "
4151ad135b5dSChristopher Siden "pool from backup.\n"));
4152ad135b5dSChristopher Siden break;
4153ad135b5dSChristopher Siden
41543d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_R:
41553d7072f8Seschrock (void) printf(gettext("status: One or more devices are "
41563d7072f8Seschrock "faulted in response to persistent errors.\n\tSufficient "
41573d7072f8Seschrock "replicas exist for the pool to continue functioning "
41583d7072f8Seschrock "in a\n\tdegraded state.\n"));
41593d7072f8Seschrock (void) printf(gettext("action: Replace the faulted device, "
41603d7072f8Seschrock "or use 'zpool clear' to mark the device\n\trepaired.\n"));
41613d7072f8Seschrock break;
41623d7072f8Seschrock
41633d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_NR:
41643d7072f8Seschrock (void) printf(gettext("status: One or more devices are "
41653d7072f8Seschrock "faulted in response to persistent errors. There are "
41663d7072f8Seschrock "insufficient replicas for the pool to\n\tcontinue "
41673d7072f8Seschrock "functioning.\n"));
41683d7072f8Seschrock (void) printf(gettext("action: Destroy and re-create the pool "
41693d7072f8Seschrock "from a backup source. Manually marking the device\n"
41703d7072f8Seschrock "\trepaired using 'zpool clear' may allow some data "
41713d7072f8Seschrock "to be recovered.\n"));
41723d7072f8Seschrock break;
41733d7072f8Seschrock
417432b87932Sek110237 case ZPOOL_STATUS_IO_FAILURE_WAIT:
417532b87932Sek110237 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
417632b87932Sek110237 (void) printf(gettext("status: One or more devices are "
41778a79c1b5Sek110237 "faulted in response to IO failures.\n"));
417832b87932Sek110237 (void) printf(gettext("action: Make sure the affected devices "
417932b87932Sek110237 "are connected, then run 'zpool clear'.\n"));
418032b87932Sek110237 break;
418132b87932Sek110237
4182b87f3af3Sperrin case ZPOOL_STATUS_BAD_LOG:
4183b87f3af3Sperrin (void) printf(gettext("status: An intent log record "
4184b87f3af3Sperrin "could not be read.\n"
4185b87f3af3Sperrin "\tWaiting for adminstrator intervention to fix the "
4186b87f3af3Sperrin "faulted pool.\n"));
4187b87f3af3Sperrin (void) printf(gettext("action: Either restore the affected "
4188b87f3af3Sperrin "device(s) and run 'zpool online',\n"
4189b87f3af3Sperrin "\tor ignore the intent log records by running "
4190b87f3af3Sperrin "'zpool clear'.\n"));
4191b87f3af3Sperrin break;
4192b87f3af3Sperrin
4193fa9e4066Sahrens default:
4194fa9e4066Sahrens /*
4195fa9e4066Sahrens * The remaining errors can't actually be generated, yet.
4196fa9e4066Sahrens */
4197fa9e4066Sahrens assert(reason == ZPOOL_STATUS_OK);
4198fa9e4066Sahrens }
4199fa9e4066Sahrens
4200fa9e4066Sahrens if (msgid != NULL)
4201654b400cSJoshua M. Clulow (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4202fa9e4066Sahrens msgid);
4203fa9e4066Sahrens
4204fa9e4066Sahrens if (config != NULL) {
4205fa9e4066Sahrens int namewidth;
4206ea8dc4b6Seschrock uint64_t nerr;
4207fa94a07fSbrendan nvlist_t **spares, **l2cache;
4208fa94a07fSbrendan uint_t nspares, nl2cache;
42093f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL;
4210fa9e4066Sahrens
42113f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot,
42123f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
42133f9d6ad7SLin Ling print_scan_status(ps);
4214fa9e4066Sahrens
4215c67d9675Seschrock namewidth = max_width(zhp, nvroot, 0, 0);
4216fa9e4066Sahrens if (namewidth < 10)
4217fa9e4066Sahrens namewidth = 10;
4218fa9e4066Sahrens
4219fa9e4066Sahrens (void) printf(gettext("config:\n\n"));
4220fa9e4066Sahrens (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4221fa9e4066Sahrens "NAME", "STATE", "READ", "WRITE", "CKSUM");
4222c67d9675Seschrock print_status_config(zhp, zpool_get_name(zhp), nvroot,
4223aa8cf21aSNeil Perrin namewidth, 0, B_FALSE);
422499653d4eSeschrock
42254dea40f0SNeil Perrin if (num_logs(nvroot) > 0)
4226e6ca193dSGeorge Wilson print_logs(zhp, nvroot, namewidth, B_TRUE);
4227fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4228fa94a07fSbrendan &l2cache, &nl2cache) == 0)
4229fa94a07fSbrendan print_l2cache(zhp, l2cache, nl2cache, namewidth);
4230fa94a07fSbrendan
423199653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
423299653d4eSeschrock &spares, &nspares) == 0)
423399653d4eSeschrock print_spares(zhp, spares, nspares, namewidth);
4234ea8dc4b6Seschrock
4235ea8dc4b6Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4236ea8dc4b6Seschrock &nerr) == 0) {
423755434c77Sek110237 nvlist_t *nverrlist = NULL;
423855434c77Sek110237
4239ea8dc4b6Seschrock /*
4240ea8dc4b6Seschrock * If the approximate error count is small, get a
4241ea8dc4b6Seschrock * precise count by fetching the entire log and
4242ea8dc4b6Seschrock * uniquifying the results.
4243ea8dc4b6Seschrock */
424475519f38Sek110237 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
424555434c77Sek110237 zpool_get_errlog(zhp, &nverrlist) == 0) {
424655434c77Sek110237 nvpair_t *elem;
424755434c77Sek110237
424855434c77Sek110237 elem = NULL;
424955434c77Sek110237 nerr = 0;
425055434c77Sek110237 while ((elem = nvlist_next_nvpair(nverrlist,
425155434c77Sek110237 elem)) != NULL) {
425255434c77Sek110237 nerr++;
425355434c77Sek110237 }
425455434c77Sek110237 }
425555434c77Sek110237 nvlist_free(nverrlist);
4256ea8dc4b6Seschrock
4257ea8dc4b6Seschrock (void) printf("\n");
425899653d4eSeschrock
4259ea8dc4b6Seschrock if (nerr == 0)
4260ea8dc4b6Seschrock (void) printf(gettext("errors: No known data "
4261ea8dc4b6Seschrock "errors\n"));
4262ea8dc4b6Seschrock else if (!cbp->cb_verbose)
4263e9dbad6fSeschrock (void) printf(gettext("errors: %llu data "
42645ad82045Snd150628 "errors, use '-v' for a list\n"),
42655ad82045Snd150628 (u_longlong_t)nerr);
4266ea8dc4b6Seschrock else
4267ea8dc4b6Seschrock print_error_log(zhp);
4268ea8dc4b6Seschrock }
42699eb19f4dSGeorge Wilson
42709eb19f4dSGeorge Wilson if (cbp->cb_dedup_stats)
42719eb19f4dSGeorge Wilson print_dedup_stats(config);
4272fa9e4066Sahrens } else {
4273fa9e4066Sahrens (void) printf(gettext("config: The configuration cannot be "
4274fa9e4066Sahrens "determined.\n"));
4275fa9e4066Sahrens }
4276fa9e4066Sahrens
4277fa9e4066Sahrens return (0);
4278fa9e4066Sahrens }
4279fa9e4066Sahrens
4280fa9e4066Sahrens /*
42813f9d6ad7SLin Ling * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4282fa9e4066Sahrens *
4283fa9e4066Sahrens * -v Display complete error logs
4284fa9e4066Sahrens * -x Display only pools with potential problems
42859eb19f4dSGeorge Wilson * -D Display dedup status (undocumented)
42863f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format
4287fa9e4066Sahrens *
4288fa9e4066Sahrens * Describes the health status of all pools or some subset.
4289fa9e4066Sahrens */
4290fa9e4066Sahrens int
zpool_do_status(int argc,char ** argv)4291fa9e4066Sahrens zpool_do_status(int argc, char **argv)
4292fa9e4066Sahrens {
4293fa9e4066Sahrens int c;
4294fa9e4066Sahrens int ret;
42953f9d6ad7SLin Ling unsigned long interval = 0, count = 0;
4296fa9e4066Sahrens status_cbdata_t cb = { 0 };
4297fa9e4066Sahrens
4298fa9e4066Sahrens /* check options */
42993f9d6ad7SLin Ling while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4300fa9e4066Sahrens switch (c) {
4301fa9e4066Sahrens case 'v':
430299653d4eSeschrock cb.cb_verbose = B_TRUE;
4303fa9e4066Sahrens break;
4304fa9e4066Sahrens case 'x':
430599653d4eSeschrock cb.cb_explain = B_TRUE;
4306fa9e4066Sahrens break;
43079eb19f4dSGeorge Wilson case 'D':
43089eb19f4dSGeorge Wilson cb.cb_dedup_stats = B_TRUE;
43099eb19f4dSGeorge Wilson break;
43103f9d6ad7SLin Ling case 'T':
43113f9d6ad7SLin Ling get_timestamp_arg(*optarg);
43123f9d6ad7SLin Ling break;
4313fa9e4066Sahrens case '?':
4314fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4315fa9e4066Sahrens optopt);
431699653d4eSeschrock usage(B_FALSE);
4317fa9e4066Sahrens }
4318fa9e4066Sahrens }
4319fa9e4066Sahrens
4320fa9e4066Sahrens argc -= optind;
4321fa9e4066Sahrens argv += optind;
4322fa9e4066Sahrens
43233f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count);
4324fa9e4066Sahrens
4325e9dbad6fSeschrock if (argc == 0)
4326e9dbad6fSeschrock cb.cb_allpools = B_TRUE;
4327e9dbad6fSeschrock
43283f9d6ad7SLin Ling cb.cb_first = B_TRUE;
43293f9d6ad7SLin Ling
43303f9d6ad7SLin Ling for (;;) {
43313f9d6ad7SLin Ling if (timestamp_fmt != NODATE)
43323f9d6ad7SLin Ling print_timestamp(timestamp_fmt);
43333f9d6ad7SLin Ling
43343f9d6ad7SLin Ling ret = for_each_pool(argc, argv, B_TRUE, NULL,
43353f9d6ad7SLin Ling status_callback, &cb);
4336fa9e4066Sahrens
4337fa9e4066Sahrens if (argc == 0 && cb.cb_count == 0)
4338fa9e4066Sahrens (void) printf(gettext("no pools available\n"));
4339e9dbad6fSeschrock else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4340fa9e4066Sahrens (void) printf(gettext("all pools are healthy\n"));
4341fa9e4066Sahrens
43423f9d6ad7SLin Ling if (ret != 0)
4343fa9e4066Sahrens return (ret);
43443f9d6ad7SLin Ling
43453f9d6ad7SLin Ling if (interval == 0)
43463f9d6ad7SLin Ling break;
43473f9d6ad7SLin Ling
43483f9d6ad7SLin Ling if (count != 0 && --count == 0)
43493f9d6ad7SLin Ling break;
43503f9d6ad7SLin Ling
43513f9d6ad7SLin Ling (void) sleep(interval);
43523f9d6ad7SLin Ling }
43533f9d6ad7SLin Ling
43543f9d6ad7SLin Ling return (0);
4355fa9e4066Sahrens }
4356fa9e4066Sahrens
4357eaca9bbdSeschrock typedef struct upgrade_cbdata {
4358eaca9bbdSeschrock int cb_first;
435906eeb2adSek110237 int cb_argc;
4360990b4856Slling uint64_t cb_version;
436106eeb2adSek110237 char **cb_argv;
4362eaca9bbdSeschrock } upgrade_cbdata_t;
4363eaca9bbdSeschrock
4364eaca9bbdSeschrock static int
upgrade_version(zpool_handle_t * zhp,uint64_t version)436557221772SChristopher Siden upgrade_version(zpool_handle_t *zhp, uint64_t version)
436657221772SChristopher Siden {
436757221772SChristopher Siden int ret;
436857221772SChristopher Siden nvlist_t *config;
436957221772SChristopher Siden uint64_t oldversion;
437057221772SChristopher Siden
437157221772SChristopher Siden config = zpool_get_config(zhp, NULL);
437257221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
437357221772SChristopher Siden &oldversion) == 0);
437457221772SChristopher Siden
437557221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(oldversion));
437657221772SChristopher Siden assert(oldversion < version);
437757221772SChristopher Siden
437857221772SChristopher Siden ret = zpool_upgrade(zhp, version);
437957221772SChristopher Siden if (ret != 0)
438057221772SChristopher Siden return (ret);
438157221772SChristopher Siden
438257221772SChristopher Siden if (version >= SPA_VERSION_FEATURES) {
438357221772SChristopher Siden (void) printf(gettext("Successfully upgraded "
438457221772SChristopher Siden "'%s' from version %llu to feature flags.\n"),
438557221772SChristopher Siden zpool_get_name(zhp), oldversion);
438657221772SChristopher Siden } else {
438757221772SChristopher Siden (void) printf(gettext("Successfully upgraded "
438857221772SChristopher Siden "'%s' from version %llu to version %llu.\n"),
438957221772SChristopher Siden zpool_get_name(zhp), oldversion, version);
439057221772SChristopher Siden }
439157221772SChristopher Siden
439257221772SChristopher Siden return (0);
439357221772SChristopher Siden }
439457221772SChristopher Siden
439557221772SChristopher Siden static int
upgrade_enable_all(zpool_handle_t * zhp,int * countp)439657221772SChristopher Siden upgrade_enable_all(zpool_handle_t *zhp, int *countp)
439757221772SChristopher Siden {
439857221772SChristopher Siden int i, ret, count;
439957221772SChristopher Siden boolean_t firstff = B_TRUE;
440057221772SChristopher Siden nvlist_t *enabled = zpool_get_features(zhp);
440157221772SChristopher Siden
440257221772SChristopher Siden count = 0;
440357221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) {
440457221772SChristopher Siden const char *fname = spa_feature_table[i].fi_uname;
440557221772SChristopher Siden const char *fguid = spa_feature_table[i].fi_guid;
440657221772SChristopher Siden if (!nvlist_exists(enabled, fguid)) {
440757221772SChristopher Siden char *propname;
440857221772SChristopher Siden verify(-1 != asprintf(&propname, "feature@%s", fname));
440957221772SChristopher Siden ret = zpool_set_prop(zhp, propname,
441057221772SChristopher Siden ZFS_FEATURE_ENABLED);
441157221772SChristopher Siden if (ret != 0) {
441257221772SChristopher Siden free(propname);
441357221772SChristopher Siden return (ret);
441457221772SChristopher Siden }
441557221772SChristopher Siden count++;
441657221772SChristopher Siden
441757221772SChristopher Siden if (firstff) {
441857221772SChristopher Siden (void) printf(gettext("Enabled the "
441957221772SChristopher Siden "following features on '%s':\n"),
442057221772SChristopher Siden zpool_get_name(zhp));
442157221772SChristopher Siden firstff = B_FALSE;
442257221772SChristopher Siden }
442357221772SChristopher Siden (void) printf(gettext(" %s\n"), fname);
442457221772SChristopher Siden free(propname);
442557221772SChristopher Siden }
442657221772SChristopher Siden }
442757221772SChristopher Siden
442857221772SChristopher Siden if (countp != NULL)
442957221772SChristopher Siden *countp = count;
443057221772SChristopher Siden return (0);
443157221772SChristopher Siden }
443257221772SChristopher Siden
443357221772SChristopher Siden static int
upgrade_cb(zpool_handle_t * zhp,void * arg)4434eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg)
4435eaca9bbdSeschrock {
4436eaca9bbdSeschrock upgrade_cbdata_t *cbp = arg;
4437eaca9bbdSeschrock nvlist_t *config;
4438eaca9bbdSeschrock uint64_t version;
443957221772SChristopher Siden boolean_t printnl = B_FALSE;
444057221772SChristopher Siden int ret;
4441eaca9bbdSeschrock
4442eaca9bbdSeschrock config = zpool_get_config(zhp, NULL);
4443eaca9bbdSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4444eaca9bbdSeschrock &version) == 0);
4445eaca9bbdSeschrock
444657221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(version));
4447eaca9bbdSeschrock
444857221772SChristopher Siden if (version < cbp->cb_version) {
444999653d4eSeschrock cbp->cb_first = B_FALSE;
445057221772SChristopher Siden ret = upgrade_version(zhp, cbp->cb_version);
445157221772SChristopher Siden if (ret != 0)
445257221772SChristopher Siden return (ret);
445357221772SChristopher Siden printnl = B_TRUE;
445457221772SChristopher Siden
44554445fffbSMatthew Ahrens /*
44564445fffbSMatthew Ahrens * If they did "zpool upgrade -a", then we could
44574445fffbSMatthew Ahrens * be doing ioctls to different pools. We need
44584445fffbSMatthew Ahrens * to log this history once to each pool, and bypass
44594445fffbSMatthew Ahrens * the normal history logging that happens in main().
44604445fffbSMatthew Ahrens */
44614445fffbSMatthew Ahrens (void) zpool_log_history(g_zfs, history_str);
44624445fffbSMatthew Ahrens log_history = B_FALSE;
446306eeb2adSek110237 }
4464eaca9bbdSeschrock
446557221772SChristopher Siden if (cbp->cb_version >= SPA_VERSION_FEATURES) {
446657221772SChristopher Siden int count;
446757221772SChristopher Siden ret = upgrade_enable_all(zhp, &count);
446857221772SChristopher Siden if (ret != 0)
446957221772SChristopher Siden return (ret);
447057221772SChristopher Siden
447157221772SChristopher Siden if (count > 0) {
447257221772SChristopher Siden cbp->cb_first = B_FALSE;
447357221772SChristopher Siden printnl = B_TRUE;
447457221772SChristopher Siden }
447557221772SChristopher Siden }
447657221772SChristopher Siden
447757221772SChristopher Siden if (printnl) {
447857221772SChristopher Siden (void) printf(gettext("\n"));
447957221772SChristopher Siden }
448057221772SChristopher Siden
448157221772SChristopher Siden return (0);
448257221772SChristopher Siden }
448357221772SChristopher Siden
448457221772SChristopher Siden static int
upgrade_list_older_cb(zpool_handle_t * zhp,void * arg)448557221772SChristopher Siden upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
448657221772SChristopher Siden {
448757221772SChristopher Siden upgrade_cbdata_t *cbp = arg;
448857221772SChristopher Siden nvlist_t *config;
448957221772SChristopher Siden uint64_t version;
449057221772SChristopher Siden
449157221772SChristopher Siden config = zpool_get_config(zhp, NULL);
449257221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
449357221772SChristopher Siden &version) == 0);
449457221772SChristopher Siden
449557221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(version));
449657221772SChristopher Siden
449757221772SChristopher Siden if (version < SPA_VERSION_FEATURES) {
4498eaca9bbdSeschrock if (cbp->cb_first) {
4499eaca9bbdSeschrock (void) printf(gettext("The following pools are "
450057221772SChristopher Siden "formatted with legacy version numbers and can\n"
450157221772SChristopher Siden "be upgraded to use feature flags. After "
450257221772SChristopher Siden "being upgraded, these pools\nwill no "
450357221772SChristopher Siden "longer be accessible by software that does not "
450457221772SChristopher Siden "support feature\nflags.\n\n"));
4505eaca9bbdSeschrock (void) printf(gettext("VER POOL\n"));
4506eaca9bbdSeschrock (void) printf(gettext("--- ------------\n"));
450799653d4eSeschrock cbp->cb_first = B_FALSE;
4508eaca9bbdSeschrock }
4509eaca9bbdSeschrock
45105ad82045Snd150628 (void) printf("%2llu %s\n", (u_longlong_t)version,
4511eaca9bbdSeschrock zpool_get_name(zhp));
4512eaca9bbdSeschrock }
4513eaca9bbdSeschrock
451457221772SChristopher Siden return (0);
451557221772SChristopher Siden }
451657221772SChristopher Siden
451757221772SChristopher Siden static int
upgrade_list_disabled_cb(zpool_handle_t * zhp,void * arg)451857221772SChristopher Siden upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
451957221772SChristopher Siden {
452057221772SChristopher Siden upgrade_cbdata_t *cbp = arg;
452157221772SChristopher Siden nvlist_t *config;
452257221772SChristopher Siden uint64_t version;
452357221772SChristopher Siden
452457221772SChristopher Siden config = zpool_get_config(zhp, NULL);
452557221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
452657221772SChristopher Siden &version) == 0);
452757221772SChristopher Siden
452857221772SChristopher Siden if (version >= SPA_VERSION_FEATURES) {
452957221772SChristopher Siden int i;
453057221772SChristopher Siden boolean_t poolfirst = B_TRUE;
453157221772SChristopher Siden nvlist_t *enabled = zpool_get_features(zhp);
453257221772SChristopher Siden
453357221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) {
453457221772SChristopher Siden const char *fguid = spa_feature_table[i].fi_guid;
453557221772SChristopher Siden const char *fname = spa_feature_table[i].fi_uname;
453657221772SChristopher Siden if (!nvlist_exists(enabled, fguid)) {
453757221772SChristopher Siden if (cbp->cb_first) {
453857221772SChristopher Siden (void) printf(gettext("\nSome "
453957221772SChristopher Siden "supported features are not "
454057221772SChristopher Siden "enabled on the following pools. "
454157221772SChristopher Siden "Once a\nfeature is enabled the "
454257221772SChristopher Siden "pool may become incompatible with "
454357221772SChristopher Siden "software\nthat does not support "
454457221772SChristopher Siden "the feature. See "
454557221772SChristopher Siden "zpool-features(5) for "
454657221772SChristopher Siden "details.\n\n"));
454757221772SChristopher Siden (void) printf(gettext("POOL "
454857221772SChristopher Siden "FEATURE\n"));
454957221772SChristopher Siden (void) printf(gettext("------"
455057221772SChristopher Siden "---------\n"));
455157221772SChristopher Siden cbp->cb_first = B_FALSE;
455257221772SChristopher Siden }
455357221772SChristopher Siden
455457221772SChristopher Siden if (poolfirst) {
455557221772SChristopher Siden (void) printf(gettext("%s\n"),
455657221772SChristopher Siden zpool_get_name(zhp));
455757221772SChristopher Siden poolfirst = B_FALSE;
455857221772SChristopher Siden }
455957221772SChristopher Siden
456057221772SChristopher Siden (void) printf(gettext(" %s\n"), fname);
456157221772SChristopher Siden }
456257221772SChristopher Siden }
456357221772SChristopher Siden }
456457221772SChristopher Siden
456557221772SChristopher Siden return (0);
4566eaca9bbdSeschrock }
4567eaca9bbdSeschrock
4568eaca9bbdSeschrock /* ARGSUSED */
4569eaca9bbdSeschrock static int
upgrade_one(zpool_handle_t * zhp,void * data)457006eeb2adSek110237 upgrade_one(zpool_handle_t *zhp, void *data)
4571eaca9bbdSeschrock {
457257221772SChristopher Siden boolean_t printnl = B_FALSE;
4573990b4856Slling upgrade_cbdata_t *cbp = data;
4574990b4856Slling uint64_t cur_version;
4575eaca9bbdSeschrock int ret;
4576eaca9bbdSeschrock
45778654d025Sperrin if (strcmp("log", zpool_get_name(zhp)) == 0) {
45788654d025Sperrin (void) printf(gettext("'log' is now a reserved word\n"
45798654d025Sperrin "Pool 'log' must be renamed using export and import"
45808654d025Sperrin " to upgrade.\n"));
45818654d025Sperrin return (1);
45828654d025Sperrin }
4583990b4856Slling
4584990b4856Slling cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4585e6c728e1Sbrendan if (cur_version > cbp->cb_version) {
4586eaca9bbdSeschrock (void) printf(gettext("Pool '%s' is already formatted "
458757221772SChristopher Siden "using more current version '%llu'.\n\n"),
4588e6c728e1Sbrendan zpool_get_name(zhp), cur_version);
4589e6c728e1Sbrendan return (0);
4590e6c728e1Sbrendan }
459157221772SChristopher Siden
459257221772SChristopher Siden if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4593e6c728e1Sbrendan (void) printf(gettext("Pool '%s' is already formatted "
459457221772SChristopher Siden "using version %llu.\n\n"), zpool_get_name(zhp),
459557221772SChristopher Siden cbp->cb_version);
4596eaca9bbdSeschrock return (0);
4597eaca9bbdSeschrock }
4598eaca9bbdSeschrock
459957221772SChristopher Siden if (cur_version != cbp->cb_version) {
460057221772SChristopher Siden printnl = B_TRUE;
460157221772SChristopher Siden ret = upgrade_version(zhp, cbp->cb_version);
460257221772SChristopher Siden if (ret != 0)
460357221772SChristopher Siden return (ret);
460406eeb2adSek110237 }
4605eaca9bbdSeschrock
460657221772SChristopher Siden if (cbp->cb_version >= SPA_VERSION_FEATURES) {
460757221772SChristopher Siden int count = 0;
460857221772SChristopher Siden ret = upgrade_enable_all(zhp, &count);
460957221772SChristopher Siden if (ret != 0)
461057221772SChristopher Siden return (ret);
461157221772SChristopher Siden
461257221772SChristopher Siden if (count != 0) {
461357221772SChristopher Siden printnl = B_TRUE;
461457221772SChristopher Siden } else if (cur_version == SPA_VERSION) {
461557221772SChristopher Siden (void) printf(gettext("Pool '%s' already has all "
461657221772SChristopher Siden "supported features enabled.\n"),
461757221772SChristopher Siden zpool_get_name(zhp));
461857221772SChristopher Siden }
461957221772SChristopher Siden }
462057221772SChristopher Siden
462157221772SChristopher Siden if (printnl) {
462257221772SChristopher Siden (void) printf(gettext("\n"));
462357221772SChristopher Siden }
462457221772SChristopher Siden
462557221772SChristopher Siden return (0);
4626eaca9bbdSeschrock }
4627eaca9bbdSeschrock
4628eaca9bbdSeschrock /*
4629eaca9bbdSeschrock * zpool upgrade
4630eaca9bbdSeschrock * zpool upgrade -v
4631990b4856Slling * zpool upgrade [-V version] <-a | pool ...>
4632eaca9bbdSeschrock *
4633eaca9bbdSeschrock * With no arguments, display downrev'd ZFS pool available for upgrade.
4634eaca9bbdSeschrock * Individual pools can be upgraded by specifying the pool, and '-a' will
4635eaca9bbdSeschrock * upgrade all pools.
4636eaca9bbdSeschrock */
4637eaca9bbdSeschrock int
zpool_do_upgrade(int argc,char ** argv)4638eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv)
4639eaca9bbdSeschrock {
4640eaca9bbdSeschrock int c;
4641eaca9bbdSeschrock upgrade_cbdata_t cb = { 0 };
4642eaca9bbdSeschrock int ret = 0;
4643eaca9bbdSeschrock boolean_t showversions = B_FALSE;
464457221772SChristopher Siden boolean_t upgradeall = B_FALSE;
4645990b4856Slling char *end;
4646990b4856Slling
4647eaca9bbdSeschrock
4648eaca9bbdSeschrock /* check options */
4649478ed9adSEric Taylor while ((c = getopt(argc, argv, ":avV:")) != -1) {
4650eaca9bbdSeschrock switch (c) {
4651eaca9bbdSeschrock case 'a':
465257221772SChristopher Siden upgradeall = B_TRUE;
4653eaca9bbdSeschrock break;
4654eaca9bbdSeschrock case 'v':
4655eaca9bbdSeschrock showversions = B_TRUE;
4656eaca9bbdSeschrock break;
4657990b4856Slling case 'V':
4658990b4856Slling cb.cb_version = strtoll(optarg, &end, 10);
4659ad135b5dSChristopher Siden if (*end != '\0' ||
4660ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4661990b4856Slling (void) fprintf(stderr,
4662990b4856Slling gettext("invalid version '%s'\n"), optarg);
4663990b4856Slling usage(B_FALSE);
4664990b4856Slling }
4665990b4856Slling break;
4666478ed9adSEric Taylor case ':':
4667478ed9adSEric Taylor (void) fprintf(stderr, gettext("missing argument for "
4668478ed9adSEric Taylor "'%c' option\n"), optopt);
4669478ed9adSEric Taylor usage(B_FALSE);
4670478ed9adSEric Taylor break;
4671eaca9bbdSeschrock case '?':
4672eaca9bbdSeschrock (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4673eaca9bbdSeschrock optopt);
467499653d4eSeschrock usage(B_FALSE);
4675eaca9bbdSeschrock }
4676eaca9bbdSeschrock }
4677eaca9bbdSeschrock
467806eeb2adSek110237 cb.cb_argc = argc;
467906eeb2adSek110237 cb.cb_argv = argv;
4680eaca9bbdSeschrock argc -= optind;
4681eaca9bbdSeschrock argv += optind;
4682eaca9bbdSeschrock
4683351420b3Slling if (cb.cb_version == 0) {
4684351420b3Slling cb.cb_version = SPA_VERSION;
468557221772SChristopher Siden } else if (!upgradeall && argc == 0) {
4686351420b3Slling (void) fprintf(stderr, gettext("-V option is "
4687351420b3Slling "incompatible with other arguments\n"));
4688351420b3Slling usage(B_FALSE);
4689351420b3Slling }
4690351420b3Slling
4691eaca9bbdSeschrock if (showversions) {
469257221772SChristopher Siden if (upgradeall || argc != 0) {
4693eaca9bbdSeschrock (void) fprintf(stderr, gettext("-v option is "
4694eaca9bbdSeschrock "incompatible with other arguments\n"));
469599653d4eSeschrock usage(B_FALSE);
4696eaca9bbdSeschrock }
469757221772SChristopher Siden } else if (upgradeall) {
4698eaca9bbdSeschrock if (argc != 0) {
4699351420b3Slling (void) fprintf(stderr, gettext("-a option should not "
4700351420b3Slling "be used along with a pool name\n"));
470199653d4eSeschrock usage(B_FALSE);
4702eaca9bbdSeschrock }
4703eaca9bbdSeschrock }
4704eaca9bbdSeschrock
4705ad135b5dSChristopher Siden (void) printf(gettext("This system supports ZFS pool feature "
4706ad135b5dSChristopher Siden "flags.\n\n"));
4707eaca9bbdSeschrock if (showversions) {
470857221772SChristopher Siden int i;
470957221772SChristopher Siden
471057221772SChristopher Siden (void) printf(gettext("The following features are "
471157221772SChristopher Siden "supported:\n\n"));
471257221772SChristopher Siden (void) printf(gettext("FEAT DESCRIPTION\n"));
471357221772SChristopher Siden (void) printf("----------------------------------------------"
471457221772SChristopher Siden "---------------\n");
471557221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) {
471657221772SChristopher Siden zfeature_info_t *fi = &spa_feature_table[i];
4717ca0cc391SMatthew Ahrens const char *ro =
4718ca0cc391SMatthew Ahrens (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
471957221772SChristopher Siden " (read-only compatible)" : "";
472057221772SChristopher Siden
472157221772SChristopher Siden (void) printf("%-37s%s\n", fi->fi_uname, ro);
472257221772SChristopher Siden (void) printf(" %s\n", fi->fi_desc);
472357221772SChristopher Siden }
472457221772SChristopher Siden (void) printf("\n");
472557221772SChristopher Siden
472657221772SChristopher Siden (void) printf(gettext("The following legacy versions are also "
4727d7d4af51Smmusante "supported:\n\n"));
4728eaca9bbdSeschrock (void) printf(gettext("VER DESCRIPTION\n"));
4729eaca9bbdSeschrock (void) printf("--- -----------------------------------------"
4730eaca9bbdSeschrock "---------------\n");
473199653d4eSeschrock (void) printf(gettext(" 1 Initial ZFS version\n"));
473244cd46caSbillm (void) printf(gettext(" 2 Ditto blocks "
473344cd46caSbillm "(replicated metadata)\n"));
473499653d4eSeschrock (void) printf(gettext(" 3 Hot spares and double parity "
473599653d4eSeschrock "RAID-Z\n"));
4736d7306b64Sek110237 (void) printf(gettext(" 4 zpool history\n"));
4737c9431fa1Sahl (void) printf(gettext(" 5 Compression using the gzip "
4738c9431fa1Sahl "algorithm\n"));
4739990b4856Slling (void) printf(gettext(" 6 bootfs pool property\n"));
47408654d025Sperrin (void) printf(gettext(" 7 Separate intent log devices\n"));
4741ecd6cf80Smarks (void) printf(gettext(" 8 Delegated administration\n"));
4742a9799022Sck153898 (void) printf(gettext(" 9 refquota and refreservation "
4743a9799022Sck153898 "properties\n"));
4744fa94a07fSbrendan (void) printf(gettext(" 10 Cache devices\n"));
4745088f3894Sahrens (void) printf(gettext(" 11 Improved scrub performance\n"));
4746bb0ade09Sahrens (void) printf(gettext(" 12 Snapshot properties\n"));
474774e7dc98SMatthew Ahrens (void) printf(gettext(" 13 snapused property\n"));
474814843421SMatthew Ahrens (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
474914843421SMatthew Ahrens (void) printf(gettext(" 15 user/group space accounting\n"));
4750478ed9adSEric Taylor (void) printf(gettext(" 16 stmf property support\n"));
47517aeab329SAdam Leventhal (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4752b24ab676SJeff Bonwick (void) printf(gettext(" 18 Snapshot user holds\n"));
475388ecc943SGeorge Wilson (void) printf(gettext(" 19 Log device removal\n"));
4754b24ab676SJeff Bonwick (void) printf(gettext(" 20 Compression using zle "
4755b24ab676SJeff Bonwick "(zero-length encoding)\n"));
4756b24ab676SJeff Bonwick (void) printf(gettext(" 21 Deduplication\n"));
475792241e0bSTom Erickson (void) printf(gettext(" 22 Received properties\n"));
47586e1f5caaSNeil Perrin (void) printf(gettext(" 23 Slim ZIL\n"));
47590a586ceaSMark Shellenbaum (void) printf(gettext(" 24 System attributes\n"));
47603f9d6ad7SLin Ling (void) printf(gettext(" 25 Improved scrub stats\n"));
4761cde58dbcSMatthew Ahrens (void) printf(gettext(" 26 Improved snapshot deletion "
4762cde58dbcSMatthew Ahrens "performance\n"));
47636e0cbcaaSMatthew Ahrens (void) printf(gettext(" 27 Improved snapshot creation "
47646e0cbcaaSMatthew Ahrens "performance\n"));
4765cb04b873SMark J Musante (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4766b24ab676SJeff Bonwick (void) printf(gettext("\nFor more information on a particular "
47679a8685acSstephanie scheffler "version, including supported releases,\n"));
47689a8685acSstephanie scheffler (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
476957221772SChristopher Siden } else if (argc == 0 && upgradeall) {
4770eaca9bbdSeschrock cb.cb_first = B_TRUE;
477199653d4eSeschrock ret = zpool_iter(g_zfs, upgrade_cb, &cb);
477257221772SChristopher Siden if (ret == 0 && cb.cb_first) {
477357221772SChristopher Siden if (cb.cb_version == SPA_VERSION) {
477457221772SChristopher Siden (void) printf(gettext("All pools are already "
477557221772SChristopher Siden "formatted using feature flags.\n\n"));
477657221772SChristopher Siden (void) printf(gettext("Every feature flags "
477757221772SChristopher Siden "pool already has all supported features "
477857221772SChristopher Siden "enabled.\n"));
477957221772SChristopher Siden } else {
478057221772SChristopher Siden (void) printf(gettext("All pools are already "
478157221772SChristopher Siden "formatted with version %llu or higher.\n"),
478257221772SChristopher Siden cb.cb_version);
4783eaca9bbdSeschrock }
4784eaca9bbdSeschrock }
478557221772SChristopher Siden } else if (argc == 0) {
478657221772SChristopher Siden cb.cb_first = B_TRUE;
478757221772SChristopher Siden ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
478857221772SChristopher Siden assert(ret == 0);
478957221772SChristopher Siden
479057221772SChristopher Siden if (cb.cb_first) {
479157221772SChristopher Siden (void) printf(gettext("All pools are formatted "
479257221772SChristopher Siden "using feature flags.\n\n"));
479357221772SChristopher Siden } else {
479457221772SChristopher Siden (void) printf(gettext("\nUse 'zpool upgrade -v' "
479557221772SChristopher Siden "for a list of available legacy versions.\n"));
479657221772SChristopher Siden }
4797eaca9bbdSeschrock
479857221772SChristopher Siden cb.cb_first = B_TRUE;
479957221772SChristopher Siden ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
480057221772SChristopher Siden assert(ret == 0);
480157221772SChristopher Siden
480257221772SChristopher Siden if (cb.cb_first) {
480357221772SChristopher Siden (void) printf(gettext("Every feature flags pool has "
480457221772SChristopher Siden "all supported features enabled.\n"));
480557221772SChristopher Siden } else {
480657221772SChristopher Siden (void) printf(gettext("\n"));
4807eaca9bbdSeschrock }
4808eaca9bbdSeschrock } else {
4809b1b8ab34Slling ret = for_each_pool(argc, argv, B_FALSE, NULL,
4810b1b8ab34Slling upgrade_one, &cb);
481106eeb2adSek110237 }
481206eeb2adSek110237
481306eeb2adSek110237 return (ret);
481406eeb2adSek110237 }
481506eeb2adSek110237
4816ecd6cf80Smarks typedef struct hist_cbdata {
4817ecd6cf80Smarks boolean_t first;
48184445fffbSMatthew Ahrens boolean_t longfmt;
48194445fffbSMatthew Ahrens boolean_t internal;
4820ecd6cf80Smarks } hist_cbdata_t;
4821ecd6cf80Smarks
482206eeb2adSek110237 /*
482306eeb2adSek110237 * Print out the command history for a specific pool.
482406eeb2adSek110237 */
482506eeb2adSek110237 static int
get_history_one(zpool_handle_t * zhp,void * data)482606eeb2adSek110237 get_history_one(zpool_handle_t *zhp, void *data)
482706eeb2adSek110237 {
482806eeb2adSek110237 nvlist_t *nvhis;
482906eeb2adSek110237 nvlist_t **records;
483006eeb2adSek110237 uint_t numrecords;
483106eeb2adSek110237 int ret, i;
4832ecd6cf80Smarks hist_cbdata_t *cb = (hist_cbdata_t *)data;
483306eeb2adSek110237
4834ecd6cf80Smarks cb->first = B_FALSE;
483506eeb2adSek110237
483606eeb2adSek110237 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
483706eeb2adSek110237
483806eeb2adSek110237 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
483906eeb2adSek110237 return (ret);
484006eeb2adSek110237
484106eeb2adSek110237 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
484206eeb2adSek110237 &records, &numrecords) == 0);
484306eeb2adSek110237 for (i = 0; i < numrecords; i++) {
48444445fffbSMatthew Ahrens nvlist_t *rec = records[i];
48454445fffbSMatthew Ahrens char tbuf[30] = "";
4846ecd6cf80Smarks
48474445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
48484445fffbSMatthew Ahrens time_t tsec;
48494445fffbSMatthew Ahrens struct tm t;
4850ecd6cf80Smarks
48514445fffbSMatthew Ahrens tsec = fnvlist_lookup_uint64(records[i],
48524445fffbSMatthew Ahrens ZPOOL_HIST_TIME);
485306eeb2adSek110237 (void) localtime_r(&tsec, &t);
485406eeb2adSek110237 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
48554445fffbSMatthew Ahrens }
48564445fffbSMatthew Ahrens
48574445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
48584445fffbSMatthew Ahrens (void) printf("%s %s", tbuf,
48594445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
48604445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
48614445fffbSMatthew Ahrens int ievent =
48624445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
48634445fffbSMatthew Ahrens if (!cb->internal)
48644445fffbSMatthew Ahrens continue;
48654445fffbSMatthew Ahrens if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
48664445fffbSMatthew Ahrens (void) printf("%s unrecognized record:\n",
48674445fffbSMatthew Ahrens tbuf);
48684445fffbSMatthew Ahrens dump_nvlist(rec, 4);
48694445fffbSMatthew Ahrens continue;
48704445fffbSMatthew Ahrens }
48714445fffbSMatthew Ahrens (void) printf("%s [internal %s txg:%lld] %s", tbuf,
48724445fffbSMatthew Ahrens zfs_history_event_names[ievent],
48734445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
48744445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
48754445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
48764445fffbSMatthew Ahrens if (!cb->internal)
48774445fffbSMatthew Ahrens continue;
48784445fffbSMatthew Ahrens (void) printf("%s [txg:%lld] %s", tbuf,
48794445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
48804445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
48814445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
48824445fffbSMatthew Ahrens (void) printf(" %s (%llu)",
48834445fffbSMatthew Ahrens fnvlist_lookup_string(rec,
48844445fffbSMatthew Ahrens ZPOOL_HIST_DSNAME),
48854445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec,
48864445fffbSMatthew Ahrens ZPOOL_HIST_DSID));
48874445fffbSMatthew Ahrens }
48884445fffbSMatthew Ahrens (void) printf(" %s", fnvlist_lookup_string(rec,
48894445fffbSMatthew Ahrens ZPOOL_HIST_INT_STR));
48904445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
48914445fffbSMatthew Ahrens if (!cb->internal)
48924445fffbSMatthew Ahrens continue;
48934445fffbSMatthew Ahrens (void) printf("%s ioctl %s\n", tbuf,
48944445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
48954445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
48964445fffbSMatthew Ahrens (void) printf(" input:\n");
48974445fffbSMatthew Ahrens dump_nvlist(fnvlist_lookup_nvlist(rec,
48984445fffbSMatthew Ahrens ZPOOL_HIST_INPUT_NVL), 8);
48994445fffbSMatthew Ahrens }
49004445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
49014445fffbSMatthew Ahrens (void) printf(" output:\n");
49024445fffbSMatthew Ahrens dump_nvlist(fnvlist_lookup_nvlist(rec,
49034445fffbSMatthew Ahrens ZPOOL_HIST_OUTPUT_NVL), 8);
49044445fffbSMatthew Ahrens }
49054445fffbSMatthew Ahrens } else {
49064445fffbSMatthew Ahrens if (!cb->internal)
49074445fffbSMatthew Ahrens continue;
49084445fffbSMatthew Ahrens (void) printf("%s unrecognized record:\n", tbuf);
49094445fffbSMatthew Ahrens dump_nvlist(rec, 4);
49104445fffbSMatthew Ahrens }
4911ecd6cf80Smarks
4912ecd6cf80Smarks if (!cb->longfmt) {
4913ecd6cf80Smarks (void) printf("\n");
4914ecd6cf80Smarks continue;
491506eeb2adSek110237 }
4916ecd6cf80Smarks (void) printf(" [");
49174445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
49184445fffbSMatthew Ahrens uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
49194445fffbSMatthew Ahrens struct passwd *pwd = getpwuid(who);
49204445fffbSMatthew Ahrens (void) printf("user %d ", (int)who);
49214445fffbSMatthew Ahrens if (pwd != NULL)
49224445fffbSMatthew Ahrens (void) printf("(%s) ", pwd->pw_name);
4923ecd6cf80Smarks }
49244445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
49254445fffbSMatthew Ahrens (void) printf("on %s",
49264445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
4927ecd6cf80Smarks }
49284445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
49294445fffbSMatthew Ahrens (void) printf(":%s",
49304445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
4931ecd6cf80Smarks }
4932ecd6cf80Smarks (void) printf("]");
4933ecd6cf80Smarks (void) printf("\n");
493406eeb2adSek110237 }
493506eeb2adSek110237 (void) printf("\n");
493606eeb2adSek110237 nvlist_free(nvhis);
493706eeb2adSek110237
493806eeb2adSek110237 return (ret);
493906eeb2adSek110237 }
494006eeb2adSek110237
494106eeb2adSek110237 /*
494206eeb2adSek110237 * zpool history <pool>
494306eeb2adSek110237 *
494406eeb2adSek110237 * Displays the history of commands that modified pools.
494506eeb2adSek110237 */
494606eeb2adSek110237 int
zpool_do_history(int argc,char ** argv)494706eeb2adSek110237 zpool_do_history(int argc, char **argv)
494806eeb2adSek110237 {
4949ecd6cf80Smarks hist_cbdata_t cbdata = { 0 };
495006eeb2adSek110237 int ret;
4951ecd6cf80Smarks int c;
495206eeb2adSek110237
4953ecd6cf80Smarks cbdata.first = B_TRUE;
4954ecd6cf80Smarks /* check options */
4955ecd6cf80Smarks while ((c = getopt(argc, argv, "li")) != -1) {
4956ecd6cf80Smarks switch (c) {
4957ecd6cf80Smarks case 'l':
49584445fffbSMatthew Ahrens cbdata.longfmt = B_TRUE;
4959ecd6cf80Smarks break;
4960ecd6cf80Smarks case 'i':
49614445fffbSMatthew Ahrens cbdata.internal = B_TRUE;
4962ecd6cf80Smarks break;
4963ecd6cf80Smarks case '?':
4964ecd6cf80Smarks (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4965ecd6cf80Smarks optopt);
4966ecd6cf80Smarks usage(B_FALSE);
4967ecd6cf80Smarks }
4968ecd6cf80Smarks }
496906eeb2adSek110237 argc -= optind;
497006eeb2adSek110237 argv += optind;
497106eeb2adSek110237
4972b1b8ab34Slling ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4973ecd6cf80Smarks &cbdata);
497406eeb2adSek110237
4975ecd6cf80Smarks if (argc == 0 && cbdata.first == B_TRUE) {
497606eeb2adSek110237 (void) printf(gettext("no pools available\n"));
497706eeb2adSek110237 return (0);
4978eaca9bbdSeschrock }
4979eaca9bbdSeschrock
4980eaca9bbdSeschrock return (ret);
4981eaca9bbdSeschrock }
4982eaca9bbdSeschrock
4983b1b8ab34Slling static int
get_callback(zpool_handle_t * zhp,void * data)4984b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data)
4985b1b8ab34Slling {
4986990b4856Slling zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4987b1b8ab34Slling char value[MAXNAMELEN];
4988990b4856Slling zprop_source_t srctype;
4989990b4856Slling zprop_list_t *pl;
4990b1b8ab34Slling
4991b1b8ab34Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4992b1b8ab34Slling
4993b1b8ab34Slling /*
4994990b4856Slling * Skip the special fake placeholder. This will also skip
4995990b4856Slling * over the name property when 'all' is specified.
4996b1b8ab34Slling */
4997990b4856Slling if (pl->pl_prop == ZPOOL_PROP_NAME &&
4998b1b8ab34Slling pl == cbp->cb_proplist)
4999b1b8ab34Slling continue;
5000b1b8ab34Slling
5001ad135b5dSChristopher Siden if (pl->pl_prop == ZPROP_INVAL &&
5002ad135b5dSChristopher Siden (zpool_prop_feature(pl->pl_user_prop) ||
5003ad135b5dSChristopher Siden zpool_prop_unsupported(pl->pl_user_prop))) {
5004ad135b5dSChristopher Siden srctype = ZPROP_SRC_LOCAL;
5005ad135b5dSChristopher Siden
5006ad135b5dSChristopher Siden if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5007ad135b5dSChristopher Siden value, sizeof (value)) == 0) {
5008ad135b5dSChristopher Siden zprop_print_one_property(zpool_get_name(zhp),
5009ad135b5dSChristopher Siden cbp, pl->pl_user_prop, value, srctype,
5010ad135b5dSChristopher Siden NULL, NULL);
5011ad135b5dSChristopher Siden }
5012ad135b5dSChristopher Siden } else {
5013ad135b5dSChristopher Siden if (zpool_get_prop(zhp, pl->pl_prop, value,
5014c58b3526SAdam Stevko sizeof (value), &srctype, cbp->cb_literal) != 0)
5015b1b8ab34Slling continue;
5016b1b8ab34Slling
5017990b4856Slling zprop_print_one_property(zpool_get_name(zhp), cbp,
5018ad135b5dSChristopher Siden zpool_prop_to_name(pl->pl_prop), value, srctype,
5019ad135b5dSChristopher Siden NULL, NULL);
5020ad135b5dSChristopher Siden }
5021b1b8ab34Slling }
5022b1b8ab34Slling return (0);
5023b1b8ab34Slling }
5024b1b8ab34Slling
5025c58b3526SAdam Stevko /*
5026c58b3526SAdam Stevko * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5027c58b3526SAdam Stevko *
5028c58b3526SAdam Stevko * -H Scripted mode. Don't display headers, and separate properties
5029c58b3526SAdam Stevko * by a single tab.
5030c58b3526SAdam Stevko * -o List of columns to display. Defaults to
5031c58b3526SAdam Stevko * "name,property,value,source".
5032c58b3526SAdam Stevko * -p Diplay values in parsable (exact) format.
5033c58b3526SAdam Stevko *
5034c58b3526SAdam Stevko * Get properties of pools in the system. Output space statistics
5035c58b3526SAdam Stevko * for each one as well as other attributes.
5036c58b3526SAdam Stevko */
5037b1b8ab34Slling int
zpool_do_get(int argc,char ** argv)5038b1b8ab34Slling zpool_do_get(int argc, char **argv)
5039b1b8ab34Slling {
5040990b4856Slling zprop_get_cbdata_t cb = { 0 };
5041990b4856Slling zprop_list_t fake_name = { 0 };
5042b1b8ab34Slling int ret;
5043c58b3526SAdam Stevko int c, i;
5044c58b3526SAdam Stevko char *value;
5045b1b8ab34Slling
5046b1b8ab34Slling cb.cb_first = B_TRUE;
5047c58b3526SAdam Stevko
5048c58b3526SAdam Stevko /*
5049c58b3526SAdam Stevko * Set up default columns and sources.
5050c58b3526SAdam Stevko */
5051990b4856Slling cb.cb_sources = ZPROP_SRC_ALL;
5052b1b8ab34Slling cb.cb_columns[0] = GET_COL_NAME;
5053b1b8ab34Slling cb.cb_columns[1] = GET_COL_PROPERTY;
5054b1b8ab34Slling cb.cb_columns[2] = GET_COL_VALUE;
5055b1b8ab34Slling cb.cb_columns[3] = GET_COL_SOURCE;
5056990b4856Slling cb.cb_type = ZFS_TYPE_POOL;
5057b1b8ab34Slling
5058c58b3526SAdam Stevko /* check options */
5059c58b3526SAdam Stevko while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5060c58b3526SAdam Stevko switch (c) {
5061c58b3526SAdam Stevko case 'p':
5062c58b3526SAdam Stevko cb.cb_literal = B_TRUE;
5063c58b3526SAdam Stevko break;
5064c58b3526SAdam Stevko case 'H':
5065c58b3526SAdam Stevko cb.cb_scripted = B_TRUE;
5066c58b3526SAdam Stevko break;
5067c58b3526SAdam Stevko case 'o':
5068c58b3526SAdam Stevko bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5069c58b3526SAdam Stevko i = 0;
5070c58b3526SAdam Stevko while (*optarg != '\0') {
5071c58b3526SAdam Stevko static char *col_subopts[] =
5072c58b3526SAdam Stevko { "name", "property", "value", "source",
5073c58b3526SAdam Stevko "all", NULL };
5074c58b3526SAdam Stevko
5075c58b3526SAdam Stevko if (i == ZFS_GET_NCOLS) {
5076c58b3526SAdam Stevko (void) fprintf(stderr, gettext("too "
5077c58b3526SAdam Stevko "many fields given to -o "
5078c58b3526SAdam Stevko "option\n"));
5079c58b3526SAdam Stevko usage(B_FALSE);
5080c58b3526SAdam Stevko }
5081c58b3526SAdam Stevko
5082c58b3526SAdam Stevko switch (getsubopt(&optarg, col_subopts,
5083c58b3526SAdam Stevko &value)) {
5084c58b3526SAdam Stevko case 0:
5085c58b3526SAdam Stevko cb.cb_columns[i++] = GET_COL_NAME;
5086c58b3526SAdam Stevko break;
5087c58b3526SAdam Stevko case 1:
5088c58b3526SAdam Stevko cb.cb_columns[i++] = GET_COL_PROPERTY;
5089c58b3526SAdam Stevko break;
5090c58b3526SAdam Stevko case 2:
5091c58b3526SAdam Stevko cb.cb_columns[i++] = GET_COL_VALUE;
5092c58b3526SAdam Stevko break;
5093c58b3526SAdam Stevko case 3:
5094c58b3526SAdam Stevko cb.cb_columns[i++] = GET_COL_SOURCE;
5095c58b3526SAdam Stevko break;
5096c58b3526SAdam Stevko case 4:
5097c58b3526SAdam Stevko if (i > 0) {
5098c58b3526SAdam Stevko (void) fprintf(stderr,
5099c58b3526SAdam Stevko gettext("\"all\" conflicts "
5100c58b3526SAdam Stevko "with specific fields "
5101c58b3526SAdam Stevko "given to -o option\n"));
5102c58b3526SAdam Stevko usage(B_FALSE);
5103c58b3526SAdam Stevko }
5104c58b3526SAdam Stevko cb.cb_columns[0] = GET_COL_NAME;
5105c58b3526SAdam Stevko cb.cb_columns[1] = GET_COL_PROPERTY;
5106c58b3526SAdam Stevko cb.cb_columns[2] = GET_COL_VALUE;
5107c58b3526SAdam Stevko cb.cb_columns[3] = GET_COL_SOURCE;
5108c58b3526SAdam Stevko i = ZFS_GET_NCOLS;
5109c58b3526SAdam Stevko break;
5110c58b3526SAdam Stevko default:
5111c58b3526SAdam Stevko (void) fprintf(stderr,
5112c58b3526SAdam Stevko gettext("invalid column name "
5113c58b3526SAdam Stevko "'%s'\n"), value);
5114c58b3526SAdam Stevko usage(B_FALSE);
5115c58b3526SAdam Stevko }
5116c58b3526SAdam Stevko }
5117c58b3526SAdam Stevko break;
5118c58b3526SAdam Stevko case '?':
5119c58b3526SAdam Stevko (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5120c58b3526SAdam Stevko optopt);
5121c58b3526SAdam Stevko usage(B_FALSE);
5122c58b3526SAdam Stevko }
5123c58b3526SAdam Stevko }
5124c58b3526SAdam Stevko
5125c58b3526SAdam Stevko argc -= optind;
5126c58b3526SAdam Stevko argv += optind;
5127c58b3526SAdam Stevko
5128c58b3526SAdam Stevko if (argc < 1) {
5129c58b3526SAdam Stevko (void) fprintf(stderr, gettext("missing property "
5130c58b3526SAdam Stevko "argument\n"));
5131c58b3526SAdam Stevko usage(B_FALSE);
5132c58b3526SAdam Stevko }
5133c58b3526SAdam Stevko
5134c58b3526SAdam Stevko if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5135990b4856Slling ZFS_TYPE_POOL) != 0)
5136b1b8ab34Slling usage(B_FALSE);
5137b1b8ab34Slling
5138c58b3526SAdam Stevko argc--;
5139c58b3526SAdam Stevko argv++;
5140c58b3526SAdam Stevko
5141b1b8ab34Slling if (cb.cb_proplist != NULL) {
5142990b4856Slling fake_name.pl_prop = ZPOOL_PROP_NAME;
5143b1b8ab34Slling fake_name.pl_width = strlen(gettext("NAME"));
5144b1b8ab34Slling fake_name.pl_next = cb.cb_proplist;
5145b1b8ab34Slling cb.cb_proplist = &fake_name;
5146b1b8ab34Slling }
5147b1b8ab34Slling
5148c58b3526SAdam Stevko ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5149b1b8ab34Slling get_callback, &cb);
5150b1b8ab34Slling
5151b1b8ab34Slling if (cb.cb_proplist == &fake_name)
5152990b4856Slling zprop_free_list(fake_name.pl_next);
5153b1b8ab34Slling else
5154990b4856Slling zprop_free_list(cb.cb_proplist);
5155b1b8ab34Slling
5156b1b8ab34Slling return (ret);
5157b1b8ab34Slling }
5158b1b8ab34Slling
5159b1b8ab34Slling typedef struct set_cbdata {
5160b1b8ab34Slling char *cb_propname;
5161b1b8ab34Slling char *cb_value;
5162b1b8ab34Slling boolean_t cb_any_successful;
5163b1b8ab34Slling } set_cbdata_t;
5164b1b8ab34Slling
5165b1b8ab34Slling int
set_callback(zpool_handle_t * zhp,void * data)5166b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data)
5167b1b8ab34Slling {
5168b1b8ab34Slling int error;
5169b1b8ab34Slling set_cbdata_t *cb = (set_cbdata_t *)data;
5170b1b8ab34Slling
5171b1b8ab34Slling error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5172b1b8ab34Slling
5173b1b8ab34Slling if (!error)
5174b1b8ab34Slling cb->cb_any_successful = B_TRUE;
5175b1b8ab34Slling
5176b1b8ab34Slling return (error);
5177b1b8ab34Slling }
5178b1b8ab34Slling
5179b1b8ab34Slling int
zpool_do_set(int argc,char ** argv)5180b1b8ab34Slling zpool_do_set(int argc, char **argv)
5181b1b8ab34Slling {
5182b1b8ab34Slling set_cbdata_t cb = { 0 };
5183b1b8ab34Slling int error;
5184b1b8ab34Slling
5185b1b8ab34Slling if (argc > 1 && argv[1][0] == '-') {
5186b1b8ab34Slling (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5187b1b8ab34Slling argv[1][1]);
5188b1b8ab34Slling usage(B_FALSE);
5189b1b8ab34Slling }
5190b1b8ab34Slling
5191b1b8ab34Slling if (argc < 2) {
5192b1b8ab34Slling (void) fprintf(stderr, gettext("missing property=value "
5193b1b8ab34Slling "argument\n"));
5194b1b8ab34Slling usage(B_FALSE);
5195b1b8ab34Slling }
5196b1b8ab34Slling
5197b1b8ab34Slling if (argc < 3) {
5198b1b8ab34Slling (void) fprintf(stderr, gettext("missing pool name\n"));
5199b1b8ab34Slling usage(B_FALSE);
5200b1b8ab34Slling }
5201b1b8ab34Slling
5202b1b8ab34Slling if (argc > 3) {
5203b1b8ab34Slling (void) fprintf(stderr, gettext("too many pool names\n"));
5204b1b8ab34Slling usage(B_FALSE);
5205b1b8ab34Slling }
5206b1b8ab34Slling
5207b1b8ab34Slling cb.cb_propname = argv[1];
5208b1b8ab34Slling cb.cb_value = strchr(cb.cb_propname, '=');
5209b1b8ab34Slling if (cb.cb_value == NULL) {
5210b1b8ab34Slling (void) fprintf(stderr, gettext("missing value in "
5211b1b8ab34Slling "property=value argument\n"));
5212b1b8ab34Slling usage(B_FALSE);
5213b1b8ab34Slling }
5214b1b8ab34Slling
5215b1b8ab34Slling *(cb.cb_value) = '\0';
5216b1b8ab34Slling cb.cb_value++;
5217b1b8ab34Slling
5218b1b8ab34Slling error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5219b1b8ab34Slling set_callback, &cb);
5220b1b8ab34Slling
5221b1b8ab34Slling return (error);
5222b1b8ab34Slling }
5223b1b8ab34Slling
5224b1b8ab34Slling static int
find_command_idx(char * command,int * idx)5225b1b8ab34Slling find_command_idx(char *command, int *idx)
5226b1b8ab34Slling {
5227b1b8ab34Slling int i;
5228b1b8ab34Slling
5229b1b8ab34Slling for (i = 0; i < NCOMMAND; i++) {
5230b1b8ab34Slling if (command_table[i].name == NULL)
5231b1b8ab34Slling continue;
5232b1b8ab34Slling
5233b1b8ab34Slling if (strcmp(command, command_table[i].name) == 0) {
5234b1b8ab34Slling *idx = i;
5235b1b8ab34Slling return (0);
5236b1b8ab34Slling }
5237b1b8ab34Slling }
5238b1b8ab34Slling return (1);
5239b1b8ab34Slling }
5240b1b8ab34Slling
5241fa9e4066Sahrens int
main(int argc,char ** argv)5242fa9e4066Sahrens main(int argc, char **argv)
5243fa9e4066Sahrens {
5244b327cd3fSIgor Kozhukhov int ret = 0;
5245fa9e4066Sahrens int i;
5246fa9e4066Sahrens char *cmdname;
5247fa9e4066Sahrens
5248fa9e4066Sahrens (void) setlocale(LC_ALL, "");
5249fa9e4066Sahrens (void) textdomain(TEXT_DOMAIN);
5250fa9e4066Sahrens
525199653d4eSeschrock if ((g_zfs = libzfs_init()) == NULL) {
525299653d4eSeschrock (void) fprintf(stderr, gettext("internal error: failed to "
5253203a47d8Snd150628 "initialize ZFS library\n"));
525499653d4eSeschrock return (1);
525599653d4eSeschrock }
525699653d4eSeschrock
525799653d4eSeschrock libzfs_print_on_error(g_zfs, B_TRUE);
525899653d4eSeschrock
5259fa9e4066Sahrens opterr = 0;
5260fa9e4066Sahrens
5261fa9e4066Sahrens /*
5262fa9e4066Sahrens * Make sure the user has specified some command.
5263fa9e4066Sahrens */
5264fa9e4066Sahrens if (argc < 2) {
5265fa9e4066Sahrens (void) fprintf(stderr, gettext("missing command\n"));
526699653d4eSeschrock usage(B_FALSE);
5267fa9e4066Sahrens }
5268fa9e4066Sahrens
5269fa9e4066Sahrens cmdname = argv[1];
5270fa9e4066Sahrens
527192e1c704SSimon Klinkert /* Log zpool commands to syslog. */
527292e1c704SSimon Klinkert if (argc > 1) {
527392e1c704SSimon Klinkert char *fullcmd, *tmp;
527492e1c704SSimon Klinkert int full_arg_len = 1;
527592e1c704SSimon Klinkert int arg_len;
527692e1c704SSimon Klinkert
527792e1c704SSimon Klinkert openlog("zpool", LOG_PID, LOG_DAEMON);
527892e1c704SSimon Klinkert for (i = 0; i < argc; i++) {
527992e1c704SSimon Klinkert full_arg_len += strlen(argv[i]) + 1;
528092e1c704SSimon Klinkert }
528192e1c704SSimon Klinkert fullcmd = malloc(full_arg_len);
528292e1c704SSimon Klinkert assert(fullcmd != NULL);
528392e1c704SSimon Klinkert
528492e1c704SSimon Klinkert tmp = fullcmd;
528592e1c704SSimon Klinkert for (i = 0; i < argc; i++) {
528692e1c704SSimon Klinkert arg_len = strlen(argv[i]);
528792e1c704SSimon Klinkert strncpy(tmp, argv[i], arg_len);
528892e1c704SSimon Klinkert tmp += arg_len;
528992e1c704SSimon Klinkert strcpy(tmp, " ");
529092e1c704SSimon Klinkert ++tmp;
529192e1c704SSimon Klinkert }
5292*986ccb29SMarcel Telka syslog(LOG_INFO, "%s", fullcmd);
529392e1c704SSimon Klinkert closelog();
529492e1c704SSimon Klinkert free(fullcmd);
529592e1c704SSimon Klinkert }
529692e1c704SSimon Klinkert
5297fa9e4066Sahrens /*
5298fa9e4066Sahrens * Special case '-?'
5299fa9e4066Sahrens */
5300fa9e4066Sahrens if (strcmp(cmdname, "-?") == 0)
530199653d4eSeschrock usage(B_TRUE);
5302fa9e4066Sahrens
53034445fffbSMatthew Ahrens zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
53042a6b87f0Sek110237
5305fa9e4066Sahrens /*
5306fa9e4066Sahrens * Run the appropriate command.
5307fa9e4066Sahrens */
5308b1b8ab34Slling if (find_command_idx(cmdname, &i) == 0) {
5309fa9e4066Sahrens current_command = &command_table[i];
5310fa9e4066Sahrens ret = command_table[i].func(argc - 1, argv + 1);
531191ebeef5Sahrens } else if (strchr(cmdname, '=')) {
531291ebeef5Sahrens verify(find_command_idx("set", &i) == 0);
531391ebeef5Sahrens current_command = &command_table[i];
531491ebeef5Sahrens ret = command_table[i].func(argc, argv);
531591ebeef5Sahrens } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5316fa9e4066Sahrens /*
531791ebeef5Sahrens * 'freeze' is a vile debugging abomination, so we treat
531891ebeef5Sahrens * it as such.
5319fa9e4066Sahrens */
5320ea8dc4b6Seschrock char buf[16384];
5321ea8dc4b6Seschrock int fd = open(ZFS_DEV, O_RDWR);
5322fa9e4066Sahrens (void) strcpy((void *)buf, argv[2]);
5323fa9e4066Sahrens return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
532491ebeef5Sahrens } else {
5325fa9e4066Sahrens (void) fprintf(stderr, gettext("unrecognized "
5326fa9e4066Sahrens "command '%s'\n"), cmdname);
532799653d4eSeschrock usage(B_FALSE);
5328fa9e4066Sahrens }
5329fa9e4066Sahrens
53304445fffbSMatthew Ahrens if (ret == 0 && log_history)
53314445fffbSMatthew Ahrens (void) zpool_log_history(g_zfs, history_str);
53324445fffbSMatthew Ahrens
533399653d4eSeschrock libzfs_fini(g_zfs);
533499653d4eSeschrock
5335fa9e4066Sahrens /*
5336fa9e4066Sahrens * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5337fa9e4066Sahrens * for the purposes of running ::findleaks.
5338fa9e4066Sahrens */
5339fa9e4066Sahrens if (getenv("ZFS_ABORT") != NULL) {
5340fa9e4066Sahrens (void) printf("dumping core by request\n");
5341fa9e4066Sahrens abort();
5342fa9e4066Sahrens }
5343fa9e4066Sahrens
5344fa9e4066Sahrens return (ret);
5345fa9e4066Sahrens }
5346