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. 28fa9e4066Sahrens */ 29fa9e4066Sahrens 30fa9e4066Sahrens #include <assert.h> 31fa9e4066Sahrens #include <ctype.h> 32fa9e4066Sahrens #include <dirent.h> 33fa9e4066Sahrens #include <errno.h> 34fa9e4066Sahrens #include <fcntl.h> 35fa9e4066Sahrens #include <libgen.h> 36fa9e4066Sahrens #include <libintl.h> 37fa9e4066Sahrens #include <libuutil.h> 38fa9e4066Sahrens #include <locale.h> 39fa9e4066Sahrens #include <stdio.h> 40fa9e4066Sahrens #include <stdlib.h> 41fa9e4066Sahrens #include <string.h> 42fa9e4066Sahrens #include <strings.h> 43fa9e4066Sahrens #include <unistd.h> 44fa9e4066Sahrens #include <priv.h> 45ecd6cf80Smarks #include <pwd.h> 46ecd6cf80Smarks #include <zone.h> 474263d13fSGeorge Wilson #include <zfs_prop.h> 48b1b8ab34Slling #include <sys/fs/zfs.h> 49fa9e4066Sahrens #include <sys/stat.h> 50fa9e4066Sahrens 51fa9e4066Sahrens #include <libzfs.h> 52fa9e4066Sahrens 53fa9e4066Sahrens #include "zpool_util.h" 54b7b97454Sperrin #include "zfs_comutil.h" 55ad135b5dSChristopher Siden #include "zfeature_common.h" 56fa9e4066Sahrens 5726fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h" 5826fd7700SKrishnendu Sadhukhan - Sun Microsystems 59fa9e4066Sahrens static int zpool_do_create(int, char **); 60fa9e4066Sahrens static int zpool_do_destroy(int, char **); 61fa9e4066Sahrens 62fa9e4066Sahrens static int zpool_do_add(int, char **); 6399653d4eSeschrock static int zpool_do_remove(int, char **); 64fa9e4066Sahrens 65fa9e4066Sahrens static int zpool_do_list(int, char **); 66fa9e4066Sahrens static int zpool_do_iostat(int, char **); 67fa9e4066Sahrens static int zpool_do_status(int, char **); 68fa9e4066Sahrens 69fa9e4066Sahrens static int zpool_do_online(int, char **); 70fa9e4066Sahrens static int zpool_do_offline(int, char **); 71ea8dc4b6Seschrock static int zpool_do_clear(int, char **); 724263d13fSGeorge Wilson static int zpool_do_reopen(int, char **); 73fa9e4066Sahrens 74e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **); 75e9103aaeSGarrett D'Amore 76fa9e4066Sahrens static int zpool_do_attach(int, char **); 77fa9e4066Sahrens static int zpool_do_detach(int, char **); 78fa9e4066Sahrens static int zpool_do_replace(int, char **); 791195e687SMark J Musante static int zpool_do_split(int, char **); 80fa9e4066Sahrens 81fa9e4066Sahrens static int zpool_do_scrub(int, char **); 82fa9e4066Sahrens 83fa9e4066Sahrens static int zpool_do_import(int, char **); 84fa9e4066Sahrens static int zpool_do_export(int, char **); 85fa9e4066Sahrens 86eaca9bbdSeschrock static int zpool_do_upgrade(int, char **); 87eaca9bbdSeschrock 8806eeb2adSek110237 static int zpool_do_history(int, char **); 8906eeb2adSek110237 90b1b8ab34Slling static int zpool_do_get(int, char **); 91b1b8ab34Slling static int zpool_do_set(int, char **); 92b1b8ab34Slling 93fa9e4066Sahrens /* 94fa9e4066Sahrens * These libumem hooks provide a reasonable set of defaults for the allocator's 95fa9e4066Sahrens * debugging facilities. 96fa9e4066Sahrens */ 9729ab75c9Srm160521 9829ab75c9Srm160521 #ifdef DEBUG 99fa9e4066Sahrens const char * 10099653d4eSeschrock _umem_debug_init(void) 101fa9e4066Sahrens { 102fa9e4066Sahrens return ("default,verbose"); /* $UMEM_DEBUG setting */ 103fa9e4066Sahrens } 104fa9e4066Sahrens 105fa9e4066Sahrens const char * 106fa9e4066Sahrens _umem_logging_init(void) 107fa9e4066Sahrens { 108fa9e4066Sahrens return ("fail,contents"); /* $UMEM_LOGGING setting */ 109fa9e4066Sahrens } 11029ab75c9Srm160521 #endif 111fa9e4066Sahrens 11265cd9f28Seschrock typedef enum { 11365cd9f28Seschrock HELP_ADD, 11465cd9f28Seschrock HELP_ATTACH, 115ea8dc4b6Seschrock HELP_CLEAR, 11665cd9f28Seschrock HELP_CREATE, 11765cd9f28Seschrock HELP_DESTROY, 11865cd9f28Seschrock HELP_DETACH, 11965cd9f28Seschrock HELP_EXPORT, 12006eeb2adSek110237 HELP_HISTORY, 12165cd9f28Seschrock HELP_IMPORT, 12265cd9f28Seschrock HELP_IOSTAT, 12365cd9f28Seschrock HELP_LIST, 12465cd9f28Seschrock HELP_OFFLINE, 12565cd9f28Seschrock HELP_ONLINE, 12665cd9f28Seschrock HELP_REPLACE, 12799653d4eSeschrock HELP_REMOVE, 12865cd9f28Seschrock HELP_SCRUB, 129eaca9bbdSeschrock HELP_STATUS, 130b1b8ab34Slling HELP_UPGRADE, 131b1b8ab34Slling HELP_GET, 1321195e687SMark J Musante HELP_SET, 133e9103aaeSGarrett D'Amore HELP_SPLIT, 1344263d13fSGeorge Wilson HELP_REGUID, 1354263d13fSGeorge Wilson HELP_REOPEN 13665cd9f28Seschrock } zpool_help_t; 13765cd9f28Seschrock 13865cd9f28Seschrock 139fa9e4066Sahrens typedef struct zpool_command { 140fa9e4066Sahrens const char *name; 141fa9e4066Sahrens int (*func)(int, char **); 14265cd9f28Seschrock zpool_help_t usage; 143fa9e4066Sahrens } zpool_command_t; 144fa9e4066Sahrens 145fa9e4066Sahrens /* 146fa9e4066Sahrens * Master command table. Each ZFS command has a name, associated function, and 147ea8dc4b6Seschrock * usage message. The usage messages need to be internationalized, so we have 148ea8dc4b6Seschrock * to have a function to return the usage message based on a command index. 14965cd9f28Seschrock * 15065cd9f28Seschrock * These commands are organized according to how they are displayed in the usage 15165cd9f28Seschrock * message. An empty command (one with a NULL name) indicates an empty line in 15265cd9f28Seschrock * the generic usage message. 153fa9e4066Sahrens */ 154fa9e4066Sahrens static zpool_command_t command_table[] = { 15565cd9f28Seschrock { "create", zpool_do_create, HELP_CREATE }, 15665cd9f28Seschrock { "destroy", zpool_do_destroy, HELP_DESTROY }, 157fa9e4066Sahrens { NULL }, 15865cd9f28Seschrock { "add", zpool_do_add, HELP_ADD }, 15999653d4eSeschrock { "remove", zpool_do_remove, HELP_REMOVE }, 160fa9e4066Sahrens { NULL }, 16165cd9f28Seschrock { "list", zpool_do_list, HELP_LIST }, 16265cd9f28Seschrock { "iostat", zpool_do_iostat, HELP_IOSTAT }, 16365cd9f28Seschrock { "status", zpool_do_status, HELP_STATUS }, 164fa9e4066Sahrens { NULL }, 16565cd9f28Seschrock { "online", zpool_do_online, HELP_ONLINE }, 16665cd9f28Seschrock { "offline", zpool_do_offline, HELP_OFFLINE }, 167ea8dc4b6Seschrock { "clear", zpool_do_clear, HELP_CLEAR }, 1684263d13fSGeorge Wilson { "reopen", zpool_do_reopen, HELP_REOPEN }, 169fa9e4066Sahrens { NULL }, 17065cd9f28Seschrock { "attach", zpool_do_attach, HELP_ATTACH }, 17165cd9f28Seschrock { "detach", zpool_do_detach, HELP_DETACH }, 17265cd9f28Seschrock { "replace", zpool_do_replace, HELP_REPLACE }, 1731195e687SMark J Musante { "split", zpool_do_split, HELP_SPLIT }, 174fa9e4066Sahrens { NULL }, 17565cd9f28Seschrock { "scrub", zpool_do_scrub, HELP_SCRUB }, 176fa9e4066Sahrens { NULL }, 17765cd9f28Seschrock { "import", zpool_do_import, HELP_IMPORT }, 17865cd9f28Seschrock { "export", zpool_do_export, HELP_EXPORT }, 17906eeb2adSek110237 { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, 180e9103aaeSGarrett D'Amore { "reguid", zpool_do_reguid, HELP_REGUID }, 18106eeb2adSek110237 { NULL }, 182b1b8ab34Slling { "history", zpool_do_history, HELP_HISTORY }, 183b1b8ab34Slling { "get", zpool_do_get, HELP_GET }, 184b1b8ab34Slling { "set", zpool_do_set, HELP_SET }, 185fa9e4066Sahrens }; 186fa9e4066Sahrens 187fa9e4066Sahrens #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 188fa9e4066Sahrens 1894445fffbSMatthew Ahrens static zpool_command_t *current_command; 1902a6b87f0Sek110237 static char history_str[HIS_MAX_RECORD_LEN]; 1914445fffbSMatthew Ahrens static boolean_t log_history = B_TRUE; 19226fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE; 19326fd7700SKrishnendu Sadhukhan - Sun Microsystems 19465cd9f28Seschrock static const char * 195*9a686fbcSPaul Dagnelie get_usage(zpool_help_t idx) 196*9a686fbcSPaul Dagnelie { 19765cd9f28Seschrock switch (idx) { 19865cd9f28Seschrock case HELP_ADD: 19965cd9f28Seschrock return (gettext("\tadd [-fn] <pool> <vdev> ...\n")); 20065cd9f28Seschrock case HELP_ATTACH: 20165cd9f28Seschrock return (gettext("\tattach [-f] <pool> <device> " 202e45ce728Sahrens "<new-device>\n")); 203ea8dc4b6Seschrock case HELP_CLEAR: 204468c413aSTim Haley return (gettext("\tclear [-nF] <pool> [device]\n")); 20565cd9f28Seschrock case HELP_CREATE: 206ad135b5dSChristopher Siden return (gettext("\tcreate [-fnd] [-o property=value] ... \n" 2070a48a24eStimh "\t [-O file-system-property=value] ... \n" 208990b4856Slling "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); 20965cd9f28Seschrock case HELP_DESTROY: 21065cd9f28Seschrock return (gettext("\tdestroy [-f] <pool>\n")); 21165cd9f28Seschrock case HELP_DETACH: 21265cd9f28Seschrock return (gettext("\tdetach <pool> <device>\n")); 21365cd9f28Seschrock case HELP_EXPORT: 21465cd9f28Seschrock return (gettext("\texport [-f] <pool> ...\n")); 21506eeb2adSek110237 case HELP_HISTORY: 216ecd6cf80Smarks return (gettext("\thistory [-il] [<pool>] ...\n")); 21765cd9f28Seschrock case HELP_IMPORT: 2184c58d714Sdarrenm return (gettext("\timport [-d dir] [-D]\n" 219f9af39baSGeorge Wilson "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n" 2202f8aaab3Seschrock "\timport [-o mntopts] [-o property=value] ... \n" 221f9af39baSGeorge Wilson "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " 222f9af39baSGeorge Wilson "[-R root] [-F [-n]] -a\n" 2232f8aaab3Seschrock "\timport [-o mntopts] [-o property=value] ... \n" 224f9af39baSGeorge Wilson "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " 225f9af39baSGeorge Wilson "[-R root] [-F [-n]]\n" 226f9af39baSGeorge Wilson "\t <pool | id> [newpool]\n")); 22765cd9f28Seschrock case HELP_IOSTAT: 22826fd7700SKrishnendu Sadhukhan - Sun Microsystems return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval " 22965cd9f28Seschrock "[count]]\n")); 23065cd9f28Seschrock case HELP_LIST: 231c58b3526SAdam Stevko return (gettext("\tlist [-Hp] [-o property[,...]] " 2323f9d6ad7SLin Ling "[-T d|u] [pool] ... [interval [count]]\n")); 23365cd9f28Seschrock case HELP_OFFLINE: 234441d80aaSlling return (gettext("\toffline [-t] <pool> <device> ...\n")); 23565cd9f28Seschrock case HELP_ONLINE: 236441d80aaSlling return (gettext("\tonline <pool> <device> ...\n")); 23765cd9f28Seschrock case HELP_REPLACE: 23865cd9f28Seschrock return (gettext("\treplace [-f] <pool> <device> " 239e45ce728Sahrens "[new-device]\n")); 24099653d4eSeschrock case HELP_REMOVE: 241fa94a07fSbrendan return (gettext("\tremove <pool> <device> ...\n")); 2424263d13fSGeorge Wilson case HELP_REOPEN: 24331d7e8faSGeorge Wilson return (gettext("\treopen <pool>\n")); 24465cd9f28Seschrock case HELP_SCRUB: 24565cd9f28Seschrock return (gettext("\tscrub [-s] <pool> ...\n")); 24665cd9f28Seschrock case HELP_STATUS: 2473f9d6ad7SLin Ling return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval " 2483f9d6ad7SLin Ling "[count]]\n")); 249eaca9bbdSeschrock case HELP_UPGRADE: 250eaca9bbdSeschrock return (gettext("\tupgrade\n" 251eaca9bbdSeschrock "\tupgrade -v\n" 252990b4856Slling "\tupgrade [-V version] <-a | pool ...>\n")); 253b1b8ab34Slling case HELP_GET: 254c58b3526SAdam Stevko return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] " 255c58b3526SAdam Stevko "<\"all\" | property[,...]> <pool> ...\n")); 256b1b8ab34Slling case HELP_SET: 257b1b8ab34Slling return (gettext("\tset <property=value> <pool> \n")); 2581195e687SMark J Musante case HELP_SPLIT: 2591195e687SMark J Musante return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n" 2601195e687SMark J Musante "\t [-o property=value] <pool> <newpool> " 2611195e687SMark J Musante "[<device> ...]\n")); 262e9103aaeSGarrett D'Amore case HELP_REGUID: 263e9103aaeSGarrett D'Amore return (gettext("\treguid <pool>\n")); 26465cd9f28Seschrock } 26565cd9f28Seschrock 26665cd9f28Seschrock abort(); 26765cd9f28Seschrock /* NOTREACHED */ 26865cd9f28Seschrock } 26965cd9f28Seschrock 270fa9e4066Sahrens 271fa9e4066Sahrens /* 272b1b8ab34Slling * Callback routine that will print out a pool property value. 273b1b8ab34Slling */ 274990b4856Slling static int 275990b4856Slling print_prop_cb(int prop, void *cb) 276b1b8ab34Slling { 277b1b8ab34Slling FILE *fp = cb; 278b1b8ab34Slling 279b24ab676SJeff Bonwick (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop)); 280b1b8ab34Slling 281990b4856Slling if (zpool_prop_readonly(prop)) 282990b4856Slling (void) fprintf(fp, " NO "); 283990b4856Slling else 284990b4856Slling (void) fprintf(fp, " YES "); 285990b4856Slling 286b1b8ab34Slling if (zpool_prop_values(prop) == NULL) 287b1b8ab34Slling (void) fprintf(fp, "-\n"); 288b1b8ab34Slling else 289b1b8ab34Slling (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); 290b1b8ab34Slling 291990b4856Slling return (ZPROP_CONT); 292b1b8ab34Slling } 293b1b8ab34Slling 294b1b8ab34Slling /* 295fa9e4066Sahrens * Display usage message. If we're inside a command, display only the usage for 296fa9e4066Sahrens * that command. Otherwise, iterate over the entire command table and display 297fa9e4066Sahrens * a complete usage message. 298fa9e4066Sahrens */ 299fa9e4066Sahrens void 30099653d4eSeschrock usage(boolean_t requested) 301fa9e4066Sahrens { 302fa9e4066Sahrens FILE *fp = requested ? stdout : stderr; 303fa9e4066Sahrens 304fa9e4066Sahrens if (current_command == NULL) { 305fa9e4066Sahrens int i; 306fa9e4066Sahrens 307fa9e4066Sahrens (void) fprintf(fp, gettext("usage: zpool command args ...\n")); 308fa9e4066Sahrens (void) fprintf(fp, 309fa9e4066Sahrens gettext("where 'command' is one of the following:\n\n")); 310fa9e4066Sahrens 311fa9e4066Sahrens for (i = 0; i < NCOMMAND; i++) { 312fa9e4066Sahrens if (command_table[i].name == NULL) 313fa9e4066Sahrens (void) fprintf(fp, "\n"); 314fa9e4066Sahrens else 315fa9e4066Sahrens (void) fprintf(fp, "%s", 31665cd9f28Seschrock get_usage(command_table[i].usage)); 317fa9e4066Sahrens } 318fa9e4066Sahrens } else { 319fa9e4066Sahrens (void) fprintf(fp, gettext("usage:\n")); 32065cd9f28Seschrock (void) fprintf(fp, "%s", get_usage(current_command->usage)); 321fa9e4066Sahrens } 322fa9e4066Sahrens 323b1b8ab34Slling if (current_command != NULL && 324b1b8ab34Slling ((strcmp(current_command->name, "set") == 0) || 325990b4856Slling (strcmp(current_command->name, "get") == 0) || 326990b4856Slling (strcmp(current_command->name, "list") == 0))) { 327b1b8ab34Slling 328b1b8ab34Slling (void) fprintf(fp, 329b1b8ab34Slling gettext("\nthe following properties are supported:\n")); 330b1b8ab34Slling 331b24ab676SJeff Bonwick (void) fprintf(fp, "\n\t%-15s %s %s\n\n", 332990b4856Slling "PROPERTY", "EDIT", "VALUES"); 333b1b8ab34Slling 334b1b8ab34Slling /* Iterate over all properties */ 335990b4856Slling (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE, 336990b4856Slling ZFS_TYPE_POOL); 337ad135b5dSChristopher Siden 338ad135b5dSChristopher Siden (void) fprintf(fp, "\t%-15s ", "feature@..."); 339ad135b5dSChristopher Siden (void) fprintf(fp, "YES disabled | enabled | active\n"); 340ad135b5dSChristopher Siden 341ad135b5dSChristopher Siden (void) fprintf(fp, gettext("\nThe feature@ properties must be " 342ad135b5dSChristopher Siden "appended with a feature name.\nSee zpool-features(5).\n")); 343b1b8ab34Slling } 344b1b8ab34Slling 345e9dbad6fSeschrock /* 346e9dbad6fSeschrock * See comments at end of main(). 347e9dbad6fSeschrock */ 348e9dbad6fSeschrock if (getenv("ZFS_ABORT") != NULL) { 349e9dbad6fSeschrock (void) printf("dumping core by request\n"); 350e9dbad6fSeschrock abort(); 351e9dbad6fSeschrock } 352e9dbad6fSeschrock 353fa9e4066Sahrens exit(requested ? 0 : 2); 354fa9e4066Sahrens } 355fa9e4066Sahrens 356fa9e4066Sahrens void 3578654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, 3588654d025Sperrin boolean_t print_logs) 359fa9e4066Sahrens { 360fa9e4066Sahrens nvlist_t **child; 361fa9e4066Sahrens uint_t c, children; 362afefbcddSeschrock char *vname; 363fa9e4066Sahrens 364fa9e4066Sahrens if (name != NULL) 365fa9e4066Sahrens (void) printf("\t%*s%s\n", indent, "", name); 366fa9e4066Sahrens 367fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 368fa9e4066Sahrens &child, &children) != 0) 369fa9e4066Sahrens return; 370fa9e4066Sahrens 371afefbcddSeschrock for (c = 0; c < children; c++) { 3728654d025Sperrin uint64_t is_log = B_FALSE; 3738654d025Sperrin 3748654d025Sperrin (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 3758654d025Sperrin &is_log); 3768654d025Sperrin if ((is_log && !print_logs) || (!is_log && print_logs)) 3778654d025Sperrin continue; 3788654d025Sperrin 37988ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 3808654d025Sperrin print_vdev_tree(zhp, vname, child[c], indent + 2, 3818654d025Sperrin B_FALSE); 382afefbcddSeschrock free(vname); 383afefbcddSeschrock } 384fa9e4066Sahrens } 385fa9e4066Sahrens 38657221772SChristopher Siden static boolean_t 38757221772SChristopher Siden prop_list_contains_feature(nvlist_t *proplist) 38857221772SChristopher Siden { 38957221772SChristopher Siden nvpair_t *nvp; 39057221772SChristopher Siden for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; 39157221772SChristopher Siden nvp = nvlist_next_nvpair(proplist, nvp)) { 39257221772SChristopher Siden if (zpool_prop_feature(nvpair_name(nvp))) 39357221772SChristopher Siden return (B_TRUE); 39457221772SChristopher Siden } 39557221772SChristopher Siden return (B_FALSE); 39657221772SChristopher Siden } 39757221772SChristopher Siden 398fa9e4066Sahrens /* 399990b4856Slling * Add a property pair (name, string-value) into a property nvlist. 400990b4856Slling */ 401990b4856Slling static int 4020a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props, 4030a48a24eStimh boolean_t poolprop) 404990b4856Slling { 4050a48a24eStimh zpool_prop_t prop = ZPROP_INVAL; 4060a48a24eStimh zfs_prop_t fprop; 407990b4856Slling nvlist_t *proplist; 4080a48a24eStimh const char *normnm; 4090a48a24eStimh char *strval; 410990b4856Slling 411990b4856Slling if (*props == NULL && 412990b4856Slling nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { 413990b4856Slling (void) fprintf(stderr, 414990b4856Slling gettext("internal error: out of memory\n")); 415990b4856Slling return (1); 416990b4856Slling } 417990b4856Slling 418990b4856Slling proplist = *props; 419990b4856Slling 4200a48a24eStimh if (poolprop) { 42157221772SChristopher Siden const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); 42257221772SChristopher Siden 423ad135b5dSChristopher Siden if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL && 424ad135b5dSChristopher Siden !zpool_prop_feature(propname)) { 425990b4856Slling (void) fprintf(stderr, gettext("property '%s' is " 426990b4856Slling "not a valid pool property\n"), propname); 427990b4856Slling return (2); 428990b4856Slling } 42957221772SChristopher Siden 43057221772SChristopher Siden /* 43157221772SChristopher Siden * feature@ properties and version should not be specified 43257221772SChristopher Siden * at the same time. 43357221772SChristopher Siden */ 43457221772SChristopher Siden if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) && 43557221772SChristopher Siden nvlist_exists(proplist, vname)) || 43657221772SChristopher Siden (prop == ZPOOL_PROP_VERSION && 43757221772SChristopher Siden prop_list_contains_feature(proplist))) { 43857221772SChristopher Siden (void) fprintf(stderr, gettext("'feature@' and " 43957221772SChristopher Siden "'version' properties cannot be specified " 44057221772SChristopher Siden "together\n")); 44157221772SChristopher Siden return (2); 44257221772SChristopher Siden } 44357221772SChristopher Siden 44457221772SChristopher Siden 445ad135b5dSChristopher Siden if (zpool_prop_feature(propname)) 446ad135b5dSChristopher Siden normnm = propname; 447ad135b5dSChristopher Siden else 4480a48a24eStimh normnm = zpool_prop_to_name(prop); 4490a48a24eStimh } else { 45014843421SMatthew Ahrens if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 4510a48a24eStimh normnm = zfs_prop_to_name(fprop); 45214843421SMatthew Ahrens } else { 45314843421SMatthew Ahrens normnm = propname; 45414843421SMatthew Ahrens } 4550a48a24eStimh } 456990b4856Slling 4570a48a24eStimh if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && 4580a48a24eStimh prop != ZPOOL_PROP_CACHEFILE) { 459990b4856Slling (void) fprintf(stderr, gettext("property '%s' " 460990b4856Slling "specified multiple times\n"), propname); 461990b4856Slling return (2); 462990b4856Slling } 463990b4856Slling 4640a48a24eStimh if (nvlist_add_string(proplist, normnm, propval) != 0) { 465990b4856Slling (void) fprintf(stderr, gettext("internal " 466990b4856Slling "error: out of memory\n")); 467990b4856Slling return (1); 468990b4856Slling } 469990b4856Slling 470990b4856Slling return (0); 471990b4856Slling } 472990b4856Slling 473990b4856Slling /* 474fa9e4066Sahrens * zpool add [-fn] <pool> <vdev> ... 475fa9e4066Sahrens * 476fa9e4066Sahrens * -f Force addition of devices, even if they appear in use 477fa9e4066Sahrens * -n Do not add the devices, but display the resulting layout if 478fa9e4066Sahrens * they were to be added. 479fa9e4066Sahrens * 480fa9e4066Sahrens * Adds the given vdevs to 'pool'. As with create, the bulk of this work is 481fa9e4066Sahrens * handled by get_vdev_spec(), which constructs the nvlist needed to pass to 482fa9e4066Sahrens * libzfs. 483fa9e4066Sahrens */ 484fa9e4066Sahrens int 485fa9e4066Sahrens zpool_do_add(int argc, char **argv) 486fa9e4066Sahrens { 48799653d4eSeschrock boolean_t force = B_FALSE; 48899653d4eSeschrock boolean_t dryrun = B_FALSE; 489fa9e4066Sahrens int c; 490fa9e4066Sahrens nvlist_t *nvroot; 491fa9e4066Sahrens char *poolname; 492fa9e4066Sahrens int ret; 493fa9e4066Sahrens zpool_handle_t *zhp; 494fa9e4066Sahrens nvlist_t *config; 495fa9e4066Sahrens 496fa9e4066Sahrens /* check options */ 497fa9e4066Sahrens while ((c = getopt(argc, argv, "fn")) != -1) { 498fa9e4066Sahrens switch (c) { 499fa9e4066Sahrens case 'f': 50099653d4eSeschrock force = B_TRUE; 501fa9e4066Sahrens break; 502fa9e4066Sahrens case 'n': 50399653d4eSeschrock dryrun = B_TRUE; 504fa9e4066Sahrens break; 505fa9e4066Sahrens case '?': 506fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 507fa9e4066Sahrens optopt); 50899653d4eSeschrock usage(B_FALSE); 509fa9e4066Sahrens } 510fa9e4066Sahrens } 511fa9e4066Sahrens 512fa9e4066Sahrens argc -= optind; 513fa9e4066Sahrens argv += optind; 514fa9e4066Sahrens 515fa9e4066Sahrens /* get pool name and check number of arguments */ 516fa9e4066Sahrens if (argc < 1) { 517fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 51899653d4eSeschrock usage(B_FALSE); 519fa9e4066Sahrens } 520fa9e4066Sahrens if (argc < 2) { 521fa9e4066Sahrens (void) fprintf(stderr, gettext("missing vdev specification\n")); 52299653d4eSeschrock usage(B_FALSE); 523fa9e4066Sahrens } 524fa9e4066Sahrens 525fa9e4066Sahrens poolname = argv[0]; 526fa9e4066Sahrens 527fa9e4066Sahrens argc--; 528fa9e4066Sahrens argv++; 529fa9e4066Sahrens 53099653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 531fa9e4066Sahrens return (1); 532fa9e4066Sahrens 533088e9d47Seschrock if ((config = zpool_get_config(zhp, NULL)) == NULL) { 534fa9e4066Sahrens (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 535fa9e4066Sahrens poolname); 536fa9e4066Sahrens zpool_close(zhp); 537fa9e4066Sahrens return (1); 538fa9e4066Sahrens } 539fa9e4066Sahrens 540fa9e4066Sahrens /* pass off to get_vdev_spec for processing */ 541705040edSEric Taylor nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun, 542705040edSEric Taylor argc, argv); 543fa9e4066Sahrens if (nvroot == NULL) { 544fa9e4066Sahrens zpool_close(zhp); 545fa9e4066Sahrens return (1); 546fa9e4066Sahrens } 547fa9e4066Sahrens 548fa9e4066Sahrens if (dryrun) { 549fa9e4066Sahrens nvlist_t *poolnvroot; 550fa9e4066Sahrens 551fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 552fa9e4066Sahrens &poolnvroot) == 0); 553fa9e4066Sahrens 554fa9e4066Sahrens (void) printf(gettext("would update '%s' to the following " 555fa9e4066Sahrens "configuration:\n"), zpool_get_name(zhp)); 556fa9e4066Sahrens 5578654d025Sperrin /* print original main pool and new tree */ 5588654d025Sperrin print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE); 5598654d025Sperrin print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE); 5608654d025Sperrin 5618654d025Sperrin /* Do the same for the logs */ 5628654d025Sperrin if (num_logs(poolnvroot) > 0) { 5638654d025Sperrin print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE); 5648654d025Sperrin print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE); 5658654d025Sperrin } else if (num_logs(nvroot) > 0) { 5668654d025Sperrin print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE); 5678654d025Sperrin } 568fa9e4066Sahrens 569fa9e4066Sahrens ret = 0; 570fa9e4066Sahrens } else { 571fa9e4066Sahrens ret = (zpool_add(zhp, nvroot) != 0); 572fa9e4066Sahrens } 573fa9e4066Sahrens 57499653d4eSeschrock nvlist_free(nvroot); 57599653d4eSeschrock zpool_close(zhp); 57699653d4eSeschrock 57799653d4eSeschrock return (ret); 57899653d4eSeschrock } 57999653d4eSeschrock 58099653d4eSeschrock /* 581fa94a07fSbrendan * zpool remove <pool> <vdev> ... 58299653d4eSeschrock * 5833f9d6ad7SLin Ling * Removes the given vdev from the pool. Currently, this supports removing 5843f9d6ad7SLin Ling * spares, cache, and log devices from the pool. 58599653d4eSeschrock */ 58699653d4eSeschrock int 58799653d4eSeschrock zpool_do_remove(int argc, char **argv) 58899653d4eSeschrock { 58999653d4eSeschrock char *poolname; 590fa94a07fSbrendan int i, ret = 0; 59199653d4eSeschrock zpool_handle_t *zhp; 59299653d4eSeschrock 59399653d4eSeschrock argc--; 59499653d4eSeschrock argv++; 59599653d4eSeschrock 59699653d4eSeschrock /* get pool name and check number of arguments */ 59799653d4eSeschrock if (argc < 1) { 59899653d4eSeschrock (void) fprintf(stderr, gettext("missing pool name argument\n")); 59999653d4eSeschrock usage(B_FALSE); 60099653d4eSeschrock } 60199653d4eSeschrock if (argc < 2) { 60299653d4eSeschrock (void) fprintf(stderr, gettext("missing device\n")); 60399653d4eSeschrock usage(B_FALSE); 60499653d4eSeschrock } 60599653d4eSeschrock 60699653d4eSeschrock poolname = argv[0]; 60799653d4eSeschrock 60899653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 60999653d4eSeschrock return (1); 61099653d4eSeschrock 611fa94a07fSbrendan for (i = 1; i < argc; i++) { 612fa94a07fSbrendan if (zpool_vdev_remove(zhp, argv[i]) != 0) 613fa94a07fSbrendan ret = 1; 614fa94a07fSbrendan } 61599653d4eSeschrock 616fa9e4066Sahrens return (ret); 617fa9e4066Sahrens } 618fa9e4066Sahrens 619fa9e4066Sahrens /* 620ad135b5dSChristopher Siden * zpool create [-fnd] [-o property=value] ... 6210a48a24eStimh * [-O file-system-property=value] ... 6220a48a24eStimh * [-R root] [-m mountpoint] <pool> <dev> ... 623fa9e4066Sahrens * 624fa9e4066Sahrens * -f Force creation, even if devices appear in use 625fa9e4066Sahrens * -n Do not create the pool, but display the resulting layout if it 626fa9e4066Sahrens * were to be created. 627fa9e4066Sahrens * -R Create a pool under an alternate root 628fa9e4066Sahrens * -m Set default mountpoint for the root dataset. By default it's 629fa9e4066Sahrens * '/<pool>' 630990b4856Slling * -o Set property=value. 631ad135b5dSChristopher Siden * -d Don't automatically enable all supported pool features 632ad135b5dSChristopher Siden * (individual features can be enabled with -o). 6330a48a24eStimh * -O Set fsproperty=value in the pool's root file system 634fa9e4066Sahrens * 635b1b8ab34Slling * Creates the named pool according to the given vdev specification. The 636fa9e4066Sahrens * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once 637fa9e4066Sahrens * we get the nvlist back from get_vdev_spec(), we either print out the contents 638fa9e4066Sahrens * (if '-n' was specified), or pass it to libzfs to do the creation. 639fa9e4066Sahrens */ 640fa9e4066Sahrens int 641fa9e4066Sahrens zpool_do_create(int argc, char **argv) 642fa9e4066Sahrens { 64399653d4eSeschrock boolean_t force = B_FALSE; 64499653d4eSeschrock boolean_t dryrun = B_FALSE; 645ad135b5dSChristopher Siden boolean_t enable_all_pool_feat = B_TRUE; 646fa9e4066Sahrens int c; 647990b4856Slling nvlist_t *nvroot = NULL; 648fa9e4066Sahrens char *poolname; 649990b4856Slling int ret = 1; 650fa9e4066Sahrens char *altroot = NULL; 651fa9e4066Sahrens char *mountpoint = NULL; 6520a48a24eStimh nvlist_t *fsprops = NULL; 653990b4856Slling nvlist_t *props = NULL; 6542f8aaab3Seschrock char *propval; 655fa9e4066Sahrens 656fa9e4066Sahrens /* check options */ 657ad135b5dSChristopher Siden while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) { 658fa9e4066Sahrens switch (c) { 659fa9e4066Sahrens case 'f': 66099653d4eSeschrock force = B_TRUE; 661fa9e4066Sahrens break; 662fa9e4066Sahrens case 'n': 66399653d4eSeschrock dryrun = B_TRUE; 664fa9e4066Sahrens break; 665ad135b5dSChristopher Siden case 'd': 666ad135b5dSChristopher Siden enable_all_pool_feat = B_FALSE; 667ad135b5dSChristopher Siden break; 668fa9e4066Sahrens case 'R': 669fa9e4066Sahrens altroot = optarg; 670990b4856Slling if (add_prop_list(zpool_prop_to_name( 6710a48a24eStimh ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 672990b4856Slling goto errout; 6732f8aaab3Seschrock if (nvlist_lookup_string(props, 6742f8aaab3Seschrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 6752f8aaab3Seschrock &propval) == 0) 6762f8aaab3Seschrock break; 677990b4856Slling if (add_prop_list(zpool_prop_to_name( 6780a48a24eStimh ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 679990b4856Slling goto errout; 680fa9e4066Sahrens break; 681fa9e4066Sahrens case 'm': 6828b713775SWill Andrews /* Equivalent to -O mountpoint=optarg */ 683fa9e4066Sahrens mountpoint = optarg; 684fa9e4066Sahrens break; 685990b4856Slling case 'o': 686990b4856Slling if ((propval = strchr(optarg, '=')) == NULL) { 687990b4856Slling (void) fprintf(stderr, gettext("missing " 688990b4856Slling "'=' for -o option\n")); 689990b4856Slling goto errout; 690990b4856Slling } 691990b4856Slling *propval = '\0'; 692990b4856Slling propval++; 693990b4856Slling 6940a48a24eStimh if (add_prop_list(optarg, propval, &props, B_TRUE)) 6950a48a24eStimh goto errout; 696ad135b5dSChristopher Siden 697ad135b5dSChristopher Siden /* 698ad135b5dSChristopher Siden * If the user is creating a pool that doesn't support 699ad135b5dSChristopher Siden * feature flags, don't enable any features. 700ad135b5dSChristopher Siden */ 701ad135b5dSChristopher Siden if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { 702ad135b5dSChristopher Siden char *end; 703ad135b5dSChristopher Siden u_longlong_t ver; 704ad135b5dSChristopher Siden 705ad135b5dSChristopher Siden ver = strtoull(propval, &end, 10); 706ad135b5dSChristopher Siden if (*end == '\0' && 707ad135b5dSChristopher Siden ver < SPA_VERSION_FEATURES) { 708ad135b5dSChristopher Siden enable_all_pool_feat = B_FALSE; 709ad135b5dSChristopher Siden } 710ad135b5dSChristopher Siden } 711c423721fSXin Li if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT) 712c423721fSXin Li altroot = propval; 7130a48a24eStimh break; 7140a48a24eStimh case 'O': 7150a48a24eStimh if ((propval = strchr(optarg, '=')) == NULL) { 7160a48a24eStimh (void) fprintf(stderr, gettext("missing " 7170a48a24eStimh "'=' for -O option\n")); 7180a48a24eStimh goto errout; 7190a48a24eStimh } 7200a48a24eStimh *propval = '\0'; 7210a48a24eStimh propval++; 7220a48a24eStimh 7238b713775SWill Andrews /* 7248b713775SWill Andrews * Mountpoints are checked and then added later. 7258b713775SWill Andrews * Uniquely among properties, they can be specified 7268b713775SWill Andrews * more than once, to avoid conflict with -m. 7278b713775SWill Andrews */ 7288b713775SWill Andrews if (0 == strcmp(optarg, 7298b713775SWill Andrews zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) { 7308b713775SWill Andrews mountpoint = propval; 7318b713775SWill Andrews } else if (add_prop_list(optarg, propval, &fsprops, 7328b713775SWill Andrews B_FALSE)) { 733990b4856Slling goto errout; 7348b713775SWill Andrews } 735990b4856Slling break; 736fa9e4066Sahrens case ':': 737fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for " 738fa9e4066Sahrens "'%c' option\n"), optopt); 739990b4856Slling goto badusage; 740fa9e4066Sahrens case '?': 741fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 742fa9e4066Sahrens optopt); 743990b4856Slling goto badusage; 744fa9e4066Sahrens } 745fa9e4066Sahrens } 746fa9e4066Sahrens 747fa9e4066Sahrens argc -= optind; 748fa9e4066Sahrens argv += optind; 749fa9e4066Sahrens 750fa9e4066Sahrens /* get pool name and check number of arguments */ 751fa9e4066Sahrens if (argc < 1) { 752fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 753990b4856Slling goto badusage; 754fa9e4066Sahrens } 755fa9e4066Sahrens if (argc < 2) { 756fa9e4066Sahrens (void) fprintf(stderr, gettext("missing vdev specification\n")); 757990b4856Slling goto badusage; 758fa9e4066Sahrens } 759fa9e4066Sahrens 760fa9e4066Sahrens poolname = argv[0]; 761fa9e4066Sahrens 762fa9e4066Sahrens /* 763fa9e4066Sahrens * As a special case, check for use of '/' in the name, and direct the 764fa9e4066Sahrens * user to use 'zfs create' instead. 765fa9e4066Sahrens */ 766fa9e4066Sahrens if (strchr(poolname, '/') != NULL) { 767fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot create '%s': invalid " 768fa9e4066Sahrens "character '/' in pool name\n"), poolname); 769fa9e4066Sahrens (void) fprintf(stderr, gettext("use 'zfs create' to " 770fa9e4066Sahrens "create a dataset\n")); 771990b4856Slling goto errout; 772fa9e4066Sahrens } 773fa9e4066Sahrens 774fa9e4066Sahrens /* pass off to get_vdev_spec for bulk processing */ 775705040edSEric Taylor nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun, 776705040edSEric Taylor argc - 1, argv + 1); 777fa9e4066Sahrens if (nvroot == NULL) 7780a48a24eStimh goto errout; 779fa9e4066Sahrens 78099653d4eSeschrock /* make_root_vdev() allows 0 toplevel children if there are spares */ 781b7b97454Sperrin if (!zfs_allocatable_devs(nvroot)) { 78299653d4eSeschrock (void) fprintf(stderr, gettext("invalid vdev " 78399653d4eSeschrock "specification: at least one toplevel vdev must be " 78499653d4eSeschrock "specified\n")); 785990b4856Slling goto errout; 78699653d4eSeschrock } 78799653d4eSeschrock 788fa9e4066Sahrens if (altroot != NULL && altroot[0] != '/') { 789fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid alternate root '%s': " 790e9dbad6fSeschrock "must be an absolute path\n"), altroot); 791990b4856Slling goto errout; 792fa9e4066Sahrens } 793fa9e4066Sahrens 794fa9e4066Sahrens /* 795fa9e4066Sahrens * Check the validity of the mountpoint and direct the user to use the 796fa9e4066Sahrens * '-m' mountpoint option if it looks like its in use. 797fa9e4066Sahrens */ 798fa9e4066Sahrens if (mountpoint == NULL || 799fa9e4066Sahrens (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && 800fa9e4066Sahrens strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) { 801fa9e4066Sahrens char buf[MAXPATHLEN]; 80211022c7cStimh DIR *dirp; 803fa9e4066Sahrens 804fa9e4066Sahrens if (mountpoint && mountpoint[0] != '/') { 805fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid mountpoint " 806fa9e4066Sahrens "'%s': must be an absolute path, 'legacy', or " 807fa9e4066Sahrens "'none'\n"), mountpoint); 808990b4856Slling goto errout; 809fa9e4066Sahrens } 810fa9e4066Sahrens 811fa9e4066Sahrens if (mountpoint == NULL) { 812fa9e4066Sahrens if (altroot != NULL) 813fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s/%s", 814fa9e4066Sahrens altroot, poolname); 815fa9e4066Sahrens else 816fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "/%s", 817fa9e4066Sahrens poolname); 818fa9e4066Sahrens } else { 819fa9e4066Sahrens if (altroot != NULL) 820fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s%s", 821fa9e4066Sahrens altroot, mountpoint); 822fa9e4066Sahrens else 823fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s", 824fa9e4066Sahrens mountpoint); 825fa9e4066Sahrens } 826fa9e4066Sahrens 82711022c7cStimh if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { 82811022c7cStimh (void) fprintf(stderr, gettext("mountpoint '%s' : " 82911022c7cStimh "%s\n"), buf, strerror(errno)); 830fa9e4066Sahrens (void) fprintf(stderr, gettext("use '-m' " 831fa9e4066Sahrens "option to provide a different default\n")); 832990b4856Slling goto errout; 83311022c7cStimh } else if (dirp) { 83411022c7cStimh int count = 0; 83511022c7cStimh 83611022c7cStimh while (count < 3 && readdir(dirp) != NULL) 83711022c7cStimh count++; 83811022c7cStimh (void) closedir(dirp); 83911022c7cStimh 84011022c7cStimh if (count > 2) { 84111022c7cStimh (void) fprintf(stderr, gettext("mountpoint " 84211022c7cStimh "'%s' exists and is not empty\n"), buf); 84311022c7cStimh (void) fprintf(stderr, gettext("use '-m' " 84411022c7cStimh "option to provide a " 84511022c7cStimh "different default\n")); 84611022c7cStimh goto errout; 84711022c7cStimh } 848fa9e4066Sahrens } 849fa9e4066Sahrens } 850fa9e4066Sahrens 8518b713775SWill Andrews /* 8528b713775SWill Andrews * Now that the mountpoint's validity has been checked, ensure that 8538b713775SWill Andrews * the property is set appropriately prior to creating the pool. 8548b713775SWill Andrews */ 8558b713775SWill Andrews if (mountpoint != NULL) { 8568b713775SWill Andrews ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 8578b713775SWill Andrews mountpoint, &fsprops, B_FALSE); 8588b713775SWill Andrews if (ret != 0) 8598b713775SWill Andrews goto errout; 8608b713775SWill Andrews } 8618b713775SWill Andrews 8628b713775SWill Andrews ret = 1; 863fa9e4066Sahrens if (dryrun) { 864fa9e4066Sahrens /* 865fa9e4066Sahrens * For a dry run invocation, print out a basic message and run 866fa9e4066Sahrens * through all the vdevs in the list and print out in an 867fa9e4066Sahrens * appropriate hierarchy. 868fa9e4066Sahrens */ 869fa9e4066Sahrens (void) printf(gettext("would create '%s' with the " 870fa9e4066Sahrens "following layout:\n\n"), poolname); 871fa9e4066Sahrens 8728654d025Sperrin print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE); 8738654d025Sperrin if (num_logs(nvroot) > 0) 8748654d025Sperrin print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE); 875fa9e4066Sahrens 876fa9e4066Sahrens ret = 0; 877fa9e4066Sahrens } else { 878fa9e4066Sahrens /* 879fa9e4066Sahrens * Hand off to libzfs. 880fa9e4066Sahrens */ 881ad135b5dSChristopher Siden if (enable_all_pool_feat) { 8822acef22dSMatthew Ahrens spa_feature_t i; 883ad135b5dSChristopher Siden for (i = 0; i < SPA_FEATURES; i++) { 884ad135b5dSChristopher Siden char propname[MAXPATHLEN]; 885ad135b5dSChristopher Siden zfeature_info_t *feat = &spa_feature_table[i]; 886ad135b5dSChristopher Siden 887ad135b5dSChristopher Siden (void) snprintf(propname, sizeof (propname), 888ad135b5dSChristopher Siden "feature@%s", feat->fi_uname); 889ad135b5dSChristopher Siden 890ad135b5dSChristopher Siden /* 891ad135b5dSChristopher Siden * Skip feature if user specified it manually 892ad135b5dSChristopher Siden * on the command line. 893ad135b5dSChristopher Siden */ 894ad135b5dSChristopher Siden if (nvlist_exists(props, propname)) 895ad135b5dSChristopher Siden continue; 896ad135b5dSChristopher Siden 8978b713775SWill Andrews ret = add_prop_list(propname, 8988b713775SWill Andrews ZFS_FEATURE_ENABLED, &props, B_TRUE); 8998b713775SWill Andrews if (ret != 0) 900ad135b5dSChristopher Siden goto errout; 901ad135b5dSChristopher Siden } 902ad135b5dSChristopher Siden } 9038b713775SWill Andrews 9048b713775SWill Andrews ret = 1; 9050a48a24eStimh if (zpool_create(g_zfs, poolname, 9060a48a24eStimh nvroot, props, fsprops) == 0) { 90799653d4eSeschrock zfs_handle_t *pool = zfs_open(g_zfs, poolname, 908fa9e4066Sahrens ZFS_TYPE_FILESYSTEM); 909fa9e4066Sahrens if (pool != NULL) { 910fa9e4066Sahrens if (zfs_mount(pool, NULL, 0) == 0) 911da6c28aaSamw ret = zfs_shareall(pool); 912fa9e4066Sahrens zfs_close(pool); 913fa9e4066Sahrens } 91499653d4eSeschrock } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { 91599653d4eSeschrock (void) fprintf(stderr, gettext("pool name may have " 91699653d4eSeschrock "been omitted\n")); 917fa9e4066Sahrens } 918fa9e4066Sahrens } 919fa9e4066Sahrens 920990b4856Slling errout: 921fa9e4066Sahrens nvlist_free(nvroot); 9220a48a24eStimh nvlist_free(fsprops); 923990b4856Slling nvlist_free(props); 924fa9e4066Sahrens return (ret); 925990b4856Slling badusage: 9260a48a24eStimh nvlist_free(fsprops); 927990b4856Slling nvlist_free(props); 928990b4856Slling usage(B_FALSE); 929990b4856Slling return (2); 930fa9e4066Sahrens } 931fa9e4066Sahrens 932fa9e4066Sahrens /* 933fa9e4066Sahrens * zpool destroy <pool> 934fa9e4066Sahrens * 935fa9e4066Sahrens * -f Forcefully unmount any datasets 936fa9e4066Sahrens * 937fa9e4066Sahrens * Destroy the given pool. Automatically unmounts any datasets in the pool. 938fa9e4066Sahrens */ 939fa9e4066Sahrens int 940fa9e4066Sahrens zpool_do_destroy(int argc, char **argv) 941fa9e4066Sahrens { 94299653d4eSeschrock boolean_t force = B_FALSE; 943fa9e4066Sahrens int c; 944fa9e4066Sahrens char *pool; 945fa9e4066Sahrens zpool_handle_t *zhp; 946fa9e4066Sahrens int ret; 947fa9e4066Sahrens 948fa9e4066Sahrens /* check options */ 949fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) { 950fa9e4066Sahrens switch (c) { 951fa9e4066Sahrens case 'f': 95299653d4eSeschrock force = B_TRUE; 953fa9e4066Sahrens break; 954fa9e4066Sahrens case '?': 955fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 956fa9e4066Sahrens optopt); 95799653d4eSeschrock usage(B_FALSE); 958fa9e4066Sahrens } 959fa9e4066Sahrens } 960fa9e4066Sahrens 961fa9e4066Sahrens argc -= optind; 962fa9e4066Sahrens argv += optind; 963fa9e4066Sahrens 964fa9e4066Sahrens /* check arguments */ 965fa9e4066Sahrens if (argc < 1) { 966fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool argument\n")); 96799653d4eSeschrock usage(B_FALSE); 968fa9e4066Sahrens } 969fa9e4066Sahrens if (argc > 1) { 970fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 97199653d4eSeschrock usage(B_FALSE); 972fa9e4066Sahrens } 973fa9e4066Sahrens 974fa9e4066Sahrens pool = argv[0]; 975fa9e4066Sahrens 97699653d4eSeschrock if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 977fa9e4066Sahrens /* 978fa9e4066Sahrens * As a special case, check for use of '/' in the name, and 979fa9e4066Sahrens * direct the user to use 'zfs destroy' instead. 980fa9e4066Sahrens */ 981fa9e4066Sahrens if (strchr(pool, '/') != NULL) 982fa9e4066Sahrens (void) fprintf(stderr, gettext("use 'zfs destroy' to " 983fa9e4066Sahrens "destroy a dataset\n")); 984fa9e4066Sahrens return (1); 985fa9e4066Sahrens } 986fa9e4066Sahrens 987f3861e1aSahl if (zpool_disable_datasets(zhp, force) != 0) { 988fa9e4066Sahrens (void) fprintf(stderr, gettext("could not destroy '%s': " 989fa9e4066Sahrens "could not unmount datasets\n"), zpool_get_name(zhp)); 990fa9e4066Sahrens return (1); 991fa9e4066Sahrens } 992fa9e4066Sahrens 9934445fffbSMatthew Ahrens /* The history must be logged as part of the export */ 9944445fffbSMatthew Ahrens log_history = B_FALSE; 9954445fffbSMatthew Ahrens 9964445fffbSMatthew Ahrens ret = (zpool_destroy(zhp, history_str) != 0); 997fa9e4066Sahrens 998fa9e4066Sahrens zpool_close(zhp); 999fa9e4066Sahrens 1000fa9e4066Sahrens return (ret); 1001fa9e4066Sahrens } 1002fa9e4066Sahrens 1003fa9e4066Sahrens /* 1004fa9e4066Sahrens * zpool export [-f] <pool> ... 1005fa9e4066Sahrens * 1006fa9e4066Sahrens * -f Forcefully unmount datasets 1007fa9e4066Sahrens * 1008b1b8ab34Slling * Export the given pools. By default, the command will attempt to cleanly 1009fa9e4066Sahrens * unmount any active datasets within the pool. If the '-f' flag is specified, 1010fa9e4066Sahrens * then the datasets will be forcefully unmounted. 1011fa9e4066Sahrens */ 1012fa9e4066Sahrens int 1013fa9e4066Sahrens zpool_do_export(int argc, char **argv) 1014fa9e4066Sahrens { 101599653d4eSeschrock boolean_t force = B_FALSE; 1016394ab0cbSGeorge Wilson boolean_t hardforce = B_FALSE; 1017fa9e4066Sahrens int c; 1018fa9e4066Sahrens zpool_handle_t *zhp; 1019fa9e4066Sahrens int ret; 1020fa9e4066Sahrens int i; 1021fa9e4066Sahrens 1022fa9e4066Sahrens /* check options */ 1023394ab0cbSGeorge Wilson while ((c = getopt(argc, argv, "fF")) != -1) { 1024fa9e4066Sahrens switch (c) { 1025fa9e4066Sahrens case 'f': 102699653d4eSeschrock force = B_TRUE; 1027fa9e4066Sahrens break; 1028394ab0cbSGeorge Wilson case 'F': 1029394ab0cbSGeorge Wilson hardforce = B_TRUE; 1030394ab0cbSGeorge Wilson break; 1031fa9e4066Sahrens case '?': 1032fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1033fa9e4066Sahrens optopt); 103499653d4eSeschrock usage(B_FALSE); 1035fa9e4066Sahrens } 1036fa9e4066Sahrens } 1037fa9e4066Sahrens 1038fa9e4066Sahrens argc -= optind; 1039fa9e4066Sahrens argv += optind; 1040fa9e4066Sahrens 1041fa9e4066Sahrens /* check arguments */ 1042fa9e4066Sahrens if (argc < 1) { 1043fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool argument\n")); 104499653d4eSeschrock usage(B_FALSE); 1045fa9e4066Sahrens } 1046fa9e4066Sahrens 1047fa9e4066Sahrens ret = 0; 1048fa9e4066Sahrens for (i = 0; i < argc; i++) { 104999653d4eSeschrock if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) { 1050fa9e4066Sahrens ret = 1; 1051fa9e4066Sahrens continue; 1052fa9e4066Sahrens } 1053fa9e4066Sahrens 1054f3861e1aSahl if (zpool_disable_datasets(zhp, force) != 0) { 1055fa9e4066Sahrens ret = 1; 1056fa9e4066Sahrens zpool_close(zhp); 1057fa9e4066Sahrens continue; 1058fa9e4066Sahrens } 1059fa9e4066Sahrens 10604445fffbSMatthew Ahrens /* The history must be logged as part of the export */ 10614445fffbSMatthew Ahrens log_history = B_FALSE; 10624445fffbSMatthew Ahrens 1063394ab0cbSGeorge Wilson if (hardforce) { 10644445fffbSMatthew Ahrens if (zpool_export_force(zhp, history_str) != 0) 1065fa9e4066Sahrens ret = 1; 10664445fffbSMatthew Ahrens } else if (zpool_export(zhp, force, history_str) != 0) { 1067394ab0cbSGeorge Wilson ret = 1; 1068394ab0cbSGeorge Wilson } 1069fa9e4066Sahrens 1070fa9e4066Sahrens zpool_close(zhp); 1071fa9e4066Sahrens } 1072fa9e4066Sahrens 1073fa9e4066Sahrens return (ret); 1074fa9e4066Sahrens } 1075fa9e4066Sahrens 1076fa9e4066Sahrens /* 1077fa9e4066Sahrens * Given a vdev configuration, determine the maximum width needed for the device 1078fa9e4066Sahrens * name column. 1079fa9e4066Sahrens */ 1080fa9e4066Sahrens static int 1081c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max) 1082fa9e4066Sahrens { 108388ecc943SGeorge Wilson char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE); 1084fa9e4066Sahrens nvlist_t **child; 1085fa9e4066Sahrens uint_t c, children; 1086fa9e4066Sahrens int ret; 1087fa9e4066Sahrens 1088fa9e4066Sahrens if (strlen(name) + depth > max) 1089fa9e4066Sahrens max = strlen(name) + depth; 1090fa9e4066Sahrens 1091afefbcddSeschrock free(name); 1092afefbcddSeschrock 109399653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 109499653d4eSeschrock &child, &children) == 0) { 1095fa9e4066Sahrens for (c = 0; c < children; c++) 109699653d4eSeschrock if ((ret = max_width(zhp, child[c], depth + 2, 109799653d4eSeschrock max)) > max) 1098fa9e4066Sahrens max = ret; 109999653d4eSeschrock } 110099653d4eSeschrock 1101fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1102fa94a07fSbrendan &child, &children) == 0) { 1103fa94a07fSbrendan for (c = 0; c < children; c++) 1104fa94a07fSbrendan if ((ret = max_width(zhp, child[c], depth + 2, 1105fa94a07fSbrendan max)) > max) 1106fa94a07fSbrendan max = ret; 1107fa94a07fSbrendan } 1108fa94a07fSbrendan 110999653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 111099653d4eSeschrock &child, &children) == 0) { 111199653d4eSeschrock for (c = 0; c < children; c++) 111299653d4eSeschrock if ((ret = max_width(zhp, child[c], depth + 2, 111399653d4eSeschrock max)) > max) 111499653d4eSeschrock max = ret; 111599653d4eSeschrock } 111699653d4eSeschrock 1117fa9e4066Sahrens 1118fa9e4066Sahrens return (max); 1119fa9e4066Sahrens } 1120fa9e4066Sahrens 1121e6ca193dSGeorge Wilson typedef struct spare_cbdata { 1122e6ca193dSGeorge Wilson uint64_t cb_guid; 1123e6ca193dSGeorge Wilson zpool_handle_t *cb_zhp; 1124e6ca193dSGeorge Wilson } spare_cbdata_t; 1125e6ca193dSGeorge Wilson 1126e6ca193dSGeorge Wilson static boolean_t 1127e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search) 1128e6ca193dSGeorge Wilson { 1129e6ca193dSGeorge Wilson uint64_t guid; 1130e6ca193dSGeorge Wilson nvlist_t **child; 1131e6ca193dSGeorge Wilson uint_t c, children; 1132e6ca193dSGeorge Wilson 1133e6ca193dSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && 1134e6ca193dSGeorge Wilson search == guid) 1135e6ca193dSGeorge Wilson return (B_TRUE); 1136e6ca193dSGeorge Wilson 1137e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1138e6ca193dSGeorge Wilson &child, &children) == 0) { 1139e6ca193dSGeorge Wilson for (c = 0; c < children; c++) 1140e6ca193dSGeorge Wilson if (find_vdev(child[c], search)) 1141e6ca193dSGeorge Wilson return (B_TRUE); 1142e6ca193dSGeorge Wilson } 1143e6ca193dSGeorge Wilson 1144e6ca193dSGeorge Wilson return (B_FALSE); 1145e6ca193dSGeorge Wilson } 1146e6ca193dSGeorge Wilson 1147e6ca193dSGeorge Wilson static int 1148e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data) 1149e6ca193dSGeorge Wilson { 1150e6ca193dSGeorge Wilson spare_cbdata_t *cbp = data; 1151e6ca193dSGeorge Wilson nvlist_t *config, *nvroot; 1152e6ca193dSGeorge Wilson 1153e6ca193dSGeorge Wilson config = zpool_get_config(zhp, NULL); 1154e6ca193dSGeorge Wilson verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1155e6ca193dSGeorge Wilson &nvroot) == 0); 1156e6ca193dSGeorge Wilson 1157e6ca193dSGeorge Wilson if (find_vdev(nvroot, cbp->cb_guid)) { 1158e6ca193dSGeorge Wilson cbp->cb_zhp = zhp; 1159e6ca193dSGeorge Wilson return (1); 1160e6ca193dSGeorge Wilson } 1161e6ca193dSGeorge Wilson 1162e6ca193dSGeorge Wilson zpool_close(zhp); 1163e6ca193dSGeorge Wilson return (0); 1164e6ca193dSGeorge Wilson } 1165e6ca193dSGeorge Wilson 1166e6ca193dSGeorge Wilson /* 1167e6ca193dSGeorge Wilson * Print out configuration state as requested by status_callback. 1168e6ca193dSGeorge Wilson */ 1169e6ca193dSGeorge Wilson void 1170e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 1171e6ca193dSGeorge Wilson int namewidth, int depth, boolean_t isspare) 1172e6ca193dSGeorge Wilson { 1173e6ca193dSGeorge Wilson nvlist_t **child; 1174e6ca193dSGeorge Wilson uint_t c, children; 11753f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 1176e6ca193dSGeorge Wilson vdev_stat_t *vs; 11773f9d6ad7SLin Ling char rbuf[6], wbuf[6], cbuf[6]; 1178e6ca193dSGeorge Wilson char *vname; 1179e6ca193dSGeorge Wilson uint64_t notpresent; 1180e6ca193dSGeorge Wilson spare_cbdata_t cb; 1181e6ca193dSGeorge Wilson char *state; 1182e6ca193dSGeorge Wilson 1183e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1184e6ca193dSGeorge Wilson &child, &children) != 0) 1185e6ca193dSGeorge Wilson children = 0; 1186e6ca193dSGeorge Wilson 11873f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 11883f9d6ad7SLin Ling (uint64_t **)&vs, &c) == 0); 11893f9d6ad7SLin Ling 1190e6ca193dSGeorge Wilson state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 1191e6ca193dSGeorge Wilson if (isspare) { 1192e6ca193dSGeorge Wilson /* 1193e6ca193dSGeorge Wilson * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for 1194e6ca193dSGeorge Wilson * online drives. 1195e6ca193dSGeorge Wilson */ 1196e6ca193dSGeorge Wilson if (vs->vs_aux == VDEV_AUX_SPARED) 1197e6ca193dSGeorge Wilson state = "INUSE"; 1198e6ca193dSGeorge Wilson else if (vs->vs_state == VDEV_STATE_HEALTHY) 1199e6ca193dSGeorge Wilson state = "AVAIL"; 1200e6ca193dSGeorge Wilson } 1201e6ca193dSGeorge Wilson 1202e6ca193dSGeorge Wilson (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth, 1203e6ca193dSGeorge Wilson name, state); 1204e6ca193dSGeorge Wilson 1205e6ca193dSGeorge Wilson if (!isspare) { 1206e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); 1207e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); 1208e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf)); 1209e6ca193dSGeorge Wilson (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf); 1210e6ca193dSGeorge Wilson } 1211e6ca193dSGeorge Wilson 1212e6ca193dSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1213e6ca193dSGeorge Wilson ¬present) == 0) { 1214e6ca193dSGeorge Wilson char *path; 1215e6ca193dSGeorge Wilson verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 1216e6ca193dSGeorge Wilson (void) printf(" was %s", path); 1217e6ca193dSGeorge Wilson } else if (vs->vs_aux != 0) { 1218e6ca193dSGeorge Wilson (void) printf(" "); 1219e6ca193dSGeorge Wilson 1220e6ca193dSGeorge Wilson switch (vs->vs_aux) { 1221e6ca193dSGeorge Wilson case VDEV_AUX_OPEN_FAILED: 1222e6ca193dSGeorge Wilson (void) printf(gettext("cannot open")); 1223e6ca193dSGeorge Wilson break; 1224e6ca193dSGeorge Wilson 1225e6ca193dSGeorge Wilson case VDEV_AUX_BAD_GUID_SUM: 1226e6ca193dSGeorge Wilson (void) printf(gettext("missing device")); 1227e6ca193dSGeorge Wilson break; 1228e6ca193dSGeorge Wilson 1229e6ca193dSGeorge Wilson case VDEV_AUX_NO_REPLICAS: 1230e6ca193dSGeorge Wilson (void) printf(gettext("insufficient replicas")); 1231e6ca193dSGeorge Wilson break; 1232e6ca193dSGeorge Wilson 1233e6ca193dSGeorge Wilson case VDEV_AUX_VERSION_NEWER: 1234e6ca193dSGeorge Wilson (void) printf(gettext("newer version")); 1235e6ca193dSGeorge Wilson break; 1236e6ca193dSGeorge Wilson 1237ad135b5dSChristopher Siden case VDEV_AUX_UNSUP_FEAT: 1238ad135b5dSChristopher Siden (void) printf(gettext("unsupported feature(s)")); 1239ad135b5dSChristopher Siden break; 1240ad135b5dSChristopher Siden 1241e6ca193dSGeorge Wilson case VDEV_AUX_SPARED: 1242e6ca193dSGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1243e6ca193dSGeorge Wilson &cb.cb_guid) == 0); 1244e6ca193dSGeorge Wilson if (zpool_iter(g_zfs, find_spare, &cb) == 1) { 1245e6ca193dSGeorge Wilson if (strcmp(zpool_get_name(cb.cb_zhp), 1246e6ca193dSGeorge Wilson zpool_get_name(zhp)) == 0) 1247e6ca193dSGeorge Wilson (void) printf(gettext("currently in " 1248e6ca193dSGeorge Wilson "use")); 1249e6ca193dSGeorge Wilson else 1250e6ca193dSGeorge Wilson (void) printf(gettext("in use by " 1251e6ca193dSGeorge Wilson "pool '%s'"), 1252e6ca193dSGeorge Wilson zpool_get_name(cb.cb_zhp)); 1253e6ca193dSGeorge Wilson zpool_close(cb.cb_zhp); 1254e6ca193dSGeorge Wilson } else { 1255e6ca193dSGeorge Wilson (void) printf(gettext("currently in use")); 1256e6ca193dSGeorge Wilson } 1257e6ca193dSGeorge Wilson break; 1258e6ca193dSGeorge Wilson 1259e6ca193dSGeorge Wilson case VDEV_AUX_ERR_EXCEEDED: 1260e6ca193dSGeorge Wilson (void) printf(gettext("too many errors")); 1261e6ca193dSGeorge Wilson break; 1262e6ca193dSGeorge Wilson 1263e6ca193dSGeorge Wilson case VDEV_AUX_IO_FAILURE: 1264e6ca193dSGeorge Wilson (void) printf(gettext("experienced I/O failures")); 1265e6ca193dSGeorge Wilson break; 1266e6ca193dSGeorge Wilson 1267e6ca193dSGeorge Wilson case VDEV_AUX_BAD_LOG: 1268e6ca193dSGeorge Wilson (void) printf(gettext("bad intent log")); 1269e6ca193dSGeorge Wilson break; 1270e6ca193dSGeorge Wilson 1271069f55e2SEric Schrock case VDEV_AUX_EXTERNAL: 1272069f55e2SEric Schrock (void) printf(gettext("external device fault")); 1273069f55e2SEric Schrock break; 1274069f55e2SEric Schrock 12751195e687SMark J Musante case VDEV_AUX_SPLIT_POOL: 12761195e687SMark J Musante (void) printf(gettext("split into new pool")); 12771195e687SMark J Musante break; 12781195e687SMark J Musante 1279e6ca193dSGeorge Wilson default: 1280e6ca193dSGeorge Wilson (void) printf(gettext("corrupted data")); 1281e6ca193dSGeorge Wilson break; 1282e6ca193dSGeorge Wilson } 12833f9d6ad7SLin Ling } 12843f9d6ad7SLin Ling 12853f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS, 12863f9d6ad7SLin Ling (uint64_t **)&ps, &c); 12873f9d6ad7SLin Ling 12883f9d6ad7SLin Ling if (ps && ps->pss_state == DSS_SCANNING && 12893f9d6ad7SLin Ling vs->vs_scan_processed != 0 && children == 0) { 12903f9d6ad7SLin Ling (void) printf(gettext(" (%s)"), 12913f9d6ad7SLin Ling (ps->pss_func == POOL_SCAN_RESILVER) ? 12923f9d6ad7SLin Ling "resilvering" : "repairing"); 1293e6ca193dSGeorge Wilson } 1294e6ca193dSGeorge Wilson 1295e6ca193dSGeorge Wilson (void) printf("\n"); 1296e6ca193dSGeorge Wilson 1297e6ca193dSGeorge Wilson for (c = 0; c < children; c++) { 129888ecc943SGeorge Wilson uint64_t islog = B_FALSE, ishole = B_FALSE; 1299e6ca193dSGeorge Wilson 130088ecc943SGeorge Wilson /* Don't print logs or holes here */ 1301e6ca193dSGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 130288ecc943SGeorge Wilson &islog); 130388ecc943SGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 130488ecc943SGeorge Wilson &ishole); 130588ecc943SGeorge Wilson if (islog || ishole) 1306e6ca193dSGeorge Wilson continue; 130788ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); 1308e6ca193dSGeorge Wilson print_status_config(zhp, vname, child[c], 1309e6ca193dSGeorge Wilson namewidth, depth + 2, isspare); 1310e6ca193dSGeorge Wilson free(vname); 1311e6ca193dSGeorge Wilson } 1312e6ca193dSGeorge Wilson } 1313e6ca193dSGeorge Wilson 1314fa9e4066Sahrens 1315fa9e4066Sahrens /* 1316fa9e4066Sahrens * Print the configuration of an exported pool. Iterate over all vdevs in the 1317fa9e4066Sahrens * pool, printing out the name and status for each one. 1318fa9e4066Sahrens */ 1319fa9e4066Sahrens void 1320e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth) 1321fa9e4066Sahrens { 1322fa9e4066Sahrens nvlist_t **child; 1323fa9e4066Sahrens uint_t c, children; 1324fa9e4066Sahrens vdev_stat_t *vs; 1325afefbcddSeschrock char *type, *vname; 1326fa9e4066Sahrens 1327fa9e4066Sahrens verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 132888ecc943SGeorge Wilson if (strcmp(type, VDEV_TYPE_MISSING) == 0 || 132988ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0) 1330fa9e4066Sahrens return; 1331fa9e4066Sahrens 13323f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 1333fa9e4066Sahrens (uint64_t **)&vs, &c) == 0); 1334fa9e4066Sahrens 1335fa9e4066Sahrens (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name); 1336990b4856Slling (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); 1337fa9e4066Sahrens 1338fa9e4066Sahrens if (vs->vs_aux != 0) { 13393d7072f8Seschrock (void) printf(" "); 1340fa9e4066Sahrens 1341fa9e4066Sahrens switch (vs->vs_aux) { 1342fa9e4066Sahrens case VDEV_AUX_OPEN_FAILED: 1343fa9e4066Sahrens (void) printf(gettext("cannot open")); 1344fa9e4066Sahrens break; 1345fa9e4066Sahrens 1346fa9e4066Sahrens case VDEV_AUX_BAD_GUID_SUM: 1347fa9e4066Sahrens (void) printf(gettext("missing device")); 1348fa9e4066Sahrens break; 1349fa9e4066Sahrens 1350fa9e4066Sahrens case VDEV_AUX_NO_REPLICAS: 1351fa9e4066Sahrens (void) printf(gettext("insufficient replicas")); 1352fa9e4066Sahrens break; 1353fa9e4066Sahrens 1354eaca9bbdSeschrock case VDEV_AUX_VERSION_NEWER: 1355eaca9bbdSeschrock (void) printf(gettext("newer version")); 1356eaca9bbdSeschrock break; 1357eaca9bbdSeschrock 1358ad135b5dSChristopher Siden case VDEV_AUX_UNSUP_FEAT: 1359ad135b5dSChristopher Siden (void) printf(gettext("unsupported feature(s)")); 1360ad135b5dSChristopher Siden break; 1361ad135b5dSChristopher Siden 13623d7072f8Seschrock case VDEV_AUX_ERR_EXCEEDED: 13633d7072f8Seschrock (void) printf(gettext("too many errors")); 13643d7072f8Seschrock break; 13653d7072f8Seschrock 1366fa9e4066Sahrens default: 1367fa9e4066Sahrens (void) printf(gettext("corrupted data")); 1368fa9e4066Sahrens break; 1369fa9e4066Sahrens } 1370fa9e4066Sahrens } 1371fa9e4066Sahrens (void) printf("\n"); 1372fa9e4066Sahrens 1373fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1374fa9e4066Sahrens &child, &children) != 0) 1375fa9e4066Sahrens return; 1376fa9e4066Sahrens 1377afefbcddSeschrock for (c = 0; c < children; c++) { 13788654d025Sperrin uint64_t is_log = B_FALSE; 13798654d025Sperrin 13808654d025Sperrin (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 13818654d025Sperrin &is_log); 1382e6ca193dSGeorge Wilson if (is_log) 13838654d025Sperrin continue; 13848654d025Sperrin 138588ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE); 1386e6ca193dSGeorge Wilson print_import_config(vname, child[c], namewidth, depth + 2); 1387afefbcddSeschrock free(vname); 1388afefbcddSeschrock } 138999653d4eSeschrock 1390fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1391fa94a07fSbrendan &child, &children) == 0) { 1392fa94a07fSbrendan (void) printf(gettext("\tcache\n")); 1393fa94a07fSbrendan for (c = 0; c < children; c++) { 139488ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); 1395fa94a07fSbrendan (void) printf("\t %s\n", vname); 1396fa94a07fSbrendan free(vname); 1397fa94a07fSbrendan } 1398fa94a07fSbrendan } 139999653d4eSeschrock 1400fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1401fa94a07fSbrendan &child, &children) == 0) { 140299653d4eSeschrock (void) printf(gettext("\tspares\n")); 140399653d4eSeschrock for (c = 0; c < children; c++) { 140488ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); 140599653d4eSeschrock (void) printf("\t %s\n", vname); 140699653d4eSeschrock free(vname); 140799653d4eSeschrock } 1408fa9e4066Sahrens } 1409fa94a07fSbrendan } 1410fa9e4066Sahrens 1411fa9e4066Sahrens /* 1412e6ca193dSGeorge Wilson * Print log vdevs. 1413e6ca193dSGeorge Wilson * Logs are recorded as top level vdevs in the main pool child array 1414e6ca193dSGeorge Wilson * but with "is_log" set to 1. We use either print_status_config() or 1415e6ca193dSGeorge Wilson * print_import_config() to print the top level logs then any log 1416e6ca193dSGeorge Wilson * children (eg mirrored slogs) are printed recursively - which 1417e6ca193dSGeorge Wilson * works because only the top level vdev is marked "is_log" 1418e6ca193dSGeorge Wilson */ 1419e6ca193dSGeorge Wilson static void 1420e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose) 1421e6ca193dSGeorge Wilson { 1422e6ca193dSGeorge Wilson uint_t c, children; 1423e6ca193dSGeorge Wilson nvlist_t **child; 1424e6ca193dSGeorge Wilson 1425e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, 1426e6ca193dSGeorge Wilson &children) != 0) 1427e6ca193dSGeorge Wilson return; 1428e6ca193dSGeorge Wilson 1429e6ca193dSGeorge Wilson (void) printf(gettext("\tlogs\n")); 1430e6ca193dSGeorge Wilson 1431e6ca193dSGeorge Wilson for (c = 0; c < children; c++) { 1432e6ca193dSGeorge Wilson uint64_t is_log = B_FALSE; 1433e6ca193dSGeorge Wilson char *name; 1434e6ca193dSGeorge Wilson 1435e6ca193dSGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1436e6ca193dSGeorge Wilson &is_log); 1437e6ca193dSGeorge Wilson if (!is_log) 1438e6ca193dSGeorge Wilson continue; 143988ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); 1440e6ca193dSGeorge Wilson if (verbose) 1441e6ca193dSGeorge Wilson print_status_config(zhp, name, child[c], namewidth, 1442e6ca193dSGeorge Wilson 2, B_FALSE); 1443e6ca193dSGeorge Wilson else 1444e6ca193dSGeorge Wilson print_import_config(name, child[c], namewidth, 2); 1445e6ca193dSGeorge Wilson free(name); 1446e6ca193dSGeorge Wilson } 1447e6ca193dSGeorge Wilson } 1448468c413aSTim Haley 1449e6ca193dSGeorge Wilson /* 1450fa9e4066Sahrens * Display the status for the given pool. 1451fa9e4066Sahrens */ 1452fa9e4066Sahrens static void 1453fa9e4066Sahrens show_import(nvlist_t *config) 1454fa9e4066Sahrens { 1455fa9e4066Sahrens uint64_t pool_state; 1456fa9e4066Sahrens vdev_stat_t *vs; 1457fa9e4066Sahrens char *name; 1458fa9e4066Sahrens uint64_t guid; 1459fa9e4066Sahrens char *msgid; 1460fa9e4066Sahrens nvlist_t *nvroot; 1461fa9e4066Sahrens int reason; 146246657f8dSmmusante const char *health; 1463fa9e4066Sahrens uint_t vsc; 1464fa9e4066Sahrens int namewidth; 14658704186eSDan McDonald char *comment; 1466fa9e4066Sahrens 1467fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1468fa9e4066Sahrens &name) == 0); 1469fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1470fa9e4066Sahrens &guid) == 0); 1471fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1472fa9e4066Sahrens &pool_state) == 0); 1473fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1474fa9e4066Sahrens &nvroot) == 0); 1475fa9e4066Sahrens 14763f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 1477fa9e4066Sahrens (uint64_t **)&vs, &vsc) == 0); 1478990b4856Slling health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 1479fa9e4066Sahrens 1480fa9e4066Sahrens reason = zpool_import_status(config, &msgid); 1481fa9e4066Sahrens 148246657f8dSmmusante (void) printf(gettext(" pool: %s\n"), name); 148346657f8dSmmusante (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); 148446657f8dSmmusante (void) printf(gettext(" state: %s"), health); 14854c58d714Sdarrenm if (pool_state == POOL_STATE_DESTROYED) 148646657f8dSmmusante (void) printf(gettext(" (DESTROYED)")); 14874c58d714Sdarrenm (void) printf("\n"); 1488fa9e4066Sahrens 1489fa9e4066Sahrens switch (reason) { 1490fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_R: 1491fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_NR: 1492fa9e4066Sahrens case ZPOOL_STATUS_BAD_GUID_SUM: 14938704186eSDan McDonald (void) printf(gettext(" status: One or more devices are " 14948704186eSDan McDonald "missing from the system.\n")); 1495fa9e4066Sahrens break; 1496fa9e4066Sahrens 1497fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_R: 1498fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_NR: 1499fa9e4066Sahrens (void) printf(gettext(" status: One or more devices contains " 1500fa9e4066Sahrens "corrupted data.\n")); 1501fa9e4066Sahrens break; 1502fa9e4066Sahrens 1503fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_DATA: 15048704186eSDan McDonald (void) printf( 15058704186eSDan McDonald gettext(" status: The pool data is corrupted.\n")); 1506fa9e4066Sahrens break; 1507fa9e4066Sahrens 1508441d80aaSlling case ZPOOL_STATUS_OFFLINE_DEV: 1509441d80aaSlling (void) printf(gettext(" status: One or more devices " 1510441d80aaSlling "are offlined.\n")); 1511441d80aaSlling break; 1512441d80aaSlling 1513ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_POOL: 1514ea8dc4b6Seschrock (void) printf(gettext(" status: The pool metadata is " 1515ea8dc4b6Seschrock "corrupted.\n")); 1516ea8dc4b6Seschrock break; 1517ea8dc4b6Seschrock 1518eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_OLDER: 151957221772SChristopher Siden (void) printf(gettext(" status: The pool is formatted using a " 152057221772SChristopher Siden "legacy on-disk version.\n")); 1521eaca9bbdSeschrock break; 1522eaca9bbdSeschrock 1523eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER: 1524eaca9bbdSeschrock (void) printf(gettext(" status: The pool is formatted using an " 1525eaca9bbdSeschrock "incompatible version.\n")); 1526eaca9bbdSeschrock break; 1527b87f3af3Sperrin 152857221772SChristopher Siden case ZPOOL_STATUS_FEAT_DISABLED: 152957221772SChristopher Siden (void) printf(gettext(" status: Some supported features are " 153057221772SChristopher Siden "not enabled on the pool.\n")); 153157221772SChristopher Siden break; 153257221772SChristopher Siden 1533ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ: 1534ad135b5dSChristopher Siden (void) printf(gettext("status: The pool uses the following " 1535ad135b5dSChristopher Siden "feature(s) not supported on this sytem:\n")); 1536ad135b5dSChristopher Siden zpool_print_unsup_feat(config); 1537ad135b5dSChristopher Siden break; 1538ad135b5dSChristopher Siden 1539ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 1540ad135b5dSChristopher Siden (void) printf(gettext("status: The pool can only be accessed " 1541ad135b5dSChristopher Siden "in read-only mode on this system. It\n\tcannot be " 1542ad135b5dSChristopher Siden "accessed in read-write mode because it uses the " 1543ad135b5dSChristopher Siden "following\n\tfeature(s) not supported on this system:\n")); 1544ad135b5dSChristopher Siden zpool_print_unsup_feat(config); 1545ad135b5dSChristopher Siden break; 1546ad135b5dSChristopher Siden 154795173954Sek110237 case ZPOOL_STATUS_HOSTID_MISMATCH: 154895173954Sek110237 (void) printf(gettext(" status: The pool was last accessed by " 154995173954Sek110237 "another system.\n")); 155095173954Sek110237 break; 1551b87f3af3Sperrin 15523d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_R: 15533d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_NR: 15543d7072f8Seschrock (void) printf(gettext(" status: One or more devices are " 15553d7072f8Seschrock "faulted.\n")); 15563d7072f8Seschrock break; 15573d7072f8Seschrock 1558b87f3af3Sperrin case ZPOOL_STATUS_BAD_LOG: 1559b87f3af3Sperrin (void) printf(gettext(" status: An intent log record cannot be " 1560b87f3af3Sperrin "read.\n")); 1561b87f3af3Sperrin break; 1562b87f3af3Sperrin 15633f9d6ad7SLin Ling case ZPOOL_STATUS_RESILVERING: 15643f9d6ad7SLin Ling (void) printf(gettext(" status: One or more devices were being " 15653f9d6ad7SLin Ling "resilvered.\n")); 15663f9d6ad7SLin Ling break; 15673f9d6ad7SLin Ling 1568fa9e4066Sahrens default: 1569fa9e4066Sahrens /* 1570fa9e4066Sahrens * No other status can be seen when importing pools. 1571fa9e4066Sahrens */ 1572fa9e4066Sahrens assert(reason == ZPOOL_STATUS_OK); 1573fa9e4066Sahrens } 1574fa9e4066Sahrens 1575fa9e4066Sahrens /* 1576fa9e4066Sahrens * Print out an action according to the overall state of the pool. 1577fa9e4066Sahrens */ 157846657f8dSmmusante if (vs->vs_state == VDEV_STATE_HEALTHY) { 157957221772SChristopher Siden if (reason == ZPOOL_STATUS_VERSION_OLDER || 158057221772SChristopher Siden reason == ZPOOL_STATUS_FEAT_DISABLED) { 1581eaca9bbdSeschrock (void) printf(gettext(" action: The pool can be " 1582eaca9bbdSeschrock "imported using its name or numeric identifier, " 1583eaca9bbdSeschrock "though\n\tsome features will not be available " 1584eaca9bbdSeschrock "without an explicit 'zpool upgrade'.\n")); 158557221772SChristopher Siden } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { 158695173954Sek110237 (void) printf(gettext(" action: The pool can be " 158795173954Sek110237 "imported using its name or numeric " 158895173954Sek110237 "identifier and\n\tthe '-f' flag.\n")); 158957221772SChristopher Siden } else { 1590eaca9bbdSeschrock (void) printf(gettext(" action: The pool can be " 1591eaca9bbdSeschrock "imported using its name or numeric " 1592eaca9bbdSeschrock "identifier.\n")); 159357221772SChristopher Siden } 159446657f8dSmmusante } else if (vs->vs_state == VDEV_STATE_DEGRADED) { 1595fa9e4066Sahrens (void) printf(gettext(" action: The pool can be imported " 1596fa9e4066Sahrens "despite missing or damaged devices. The\n\tfault " 1597eaca9bbdSeschrock "tolerance of the pool may be compromised if imported.\n")); 1598fa9e4066Sahrens } else { 1599eaca9bbdSeschrock switch (reason) { 1600eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER: 1601eaca9bbdSeschrock (void) printf(gettext(" action: The pool cannot be " 1602eaca9bbdSeschrock "imported. Access the pool on a system running " 1603eaca9bbdSeschrock "newer\n\tsoftware, or recreate the pool from " 1604eaca9bbdSeschrock "backup.\n")); 1605eaca9bbdSeschrock break; 1606ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ: 1607ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be " 1608ad135b5dSChristopher Siden "imported. Access the pool on a system that " 1609ad135b5dSChristopher Siden "supports\n\tthe required feature(s), or recreate " 1610ad135b5dSChristopher Siden "the pool from backup.\n")); 1611ad135b5dSChristopher Siden break; 1612ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 1613ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be " 1614ad135b5dSChristopher Siden "imported in read-write mode. Import the pool " 1615ad135b5dSChristopher Siden "with\n" 1616ad135b5dSChristopher Siden "\t\"-o readonly=on\", access the pool on a system " 1617ad135b5dSChristopher Siden "that supports the\n\trequired feature(s), or " 1618ad135b5dSChristopher Siden "recreate the pool from backup.\n")); 1619ad135b5dSChristopher Siden break; 1620eaca9bbdSeschrock case ZPOOL_STATUS_MISSING_DEV_R: 1621eaca9bbdSeschrock case ZPOOL_STATUS_MISSING_DEV_NR: 1622eaca9bbdSeschrock case ZPOOL_STATUS_BAD_GUID_SUM: 1623fa9e4066Sahrens (void) printf(gettext(" action: The pool cannot be " 1624fa9e4066Sahrens "imported. Attach the missing\n\tdevices and try " 1625fa9e4066Sahrens "again.\n")); 1626eaca9bbdSeschrock break; 1627eaca9bbdSeschrock default: 1628fa9e4066Sahrens (void) printf(gettext(" action: The pool cannot be " 1629fa9e4066Sahrens "imported due to damaged devices or data.\n")); 1630fa9e4066Sahrens } 1631eaca9bbdSeschrock } 1632eaca9bbdSeschrock 16338704186eSDan McDonald /* Print the comment attached to the pool. */ 16348704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 16358704186eSDan McDonald (void) printf(gettext("comment: %s\n"), comment); 16368704186eSDan McDonald 163746657f8dSmmusante /* 163846657f8dSmmusante * If the state is "closed" or "can't open", and the aux state 163946657f8dSmmusante * is "corrupt data": 164046657f8dSmmusante */ 164146657f8dSmmusante if (((vs->vs_state == VDEV_STATE_CLOSED) || 164246657f8dSmmusante (vs->vs_state == VDEV_STATE_CANT_OPEN)) && 164346657f8dSmmusante (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { 1644eaca9bbdSeschrock if (pool_state == POOL_STATE_DESTROYED) 1645eaca9bbdSeschrock (void) printf(gettext("\tThe pool was destroyed, " 1646eaca9bbdSeschrock "but can be imported using the '-Df' flags.\n")); 1647eaca9bbdSeschrock else if (pool_state != POOL_STATE_EXPORTED) 1648eaca9bbdSeschrock (void) printf(gettext("\tThe pool may be active on " 164918ce54dfSek110237 "another system, but can be imported using\n\t" 1650eaca9bbdSeschrock "the '-f' flag.\n")); 1651eaca9bbdSeschrock } 1652fa9e4066Sahrens 1653fa9e4066Sahrens if (msgid != NULL) 1654654b400cSJoshua M. Clulow (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 1655fa9e4066Sahrens msgid); 1656fa9e4066Sahrens 1657fa9e4066Sahrens (void) printf(gettext(" config:\n\n")); 1658fa9e4066Sahrens 1659c67d9675Seschrock namewidth = max_width(NULL, nvroot, 0, 0); 1660fa9e4066Sahrens if (namewidth < 10) 1661fa9e4066Sahrens namewidth = 10; 16628654d025Sperrin 1663e6ca193dSGeorge Wilson print_import_config(name, nvroot, namewidth, 0); 1664e6ca193dSGeorge Wilson if (num_logs(nvroot) > 0) 1665e6ca193dSGeorge Wilson print_logs(NULL, nvroot, namewidth, B_FALSE); 1666fa9e4066Sahrens 1667fa9e4066Sahrens if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { 166846657f8dSmmusante (void) printf(gettext("\n\tAdditional devices are known to " 1669fa9e4066Sahrens "be part of this pool, though their\n\texact " 167046657f8dSmmusante "configuration cannot be determined.\n")); 1671fa9e4066Sahrens } 1672fa9e4066Sahrens } 1673fa9e4066Sahrens 1674fa9e4066Sahrens /* 1675fa9e4066Sahrens * Perform the import for the given configuration. This passes the heavy 1676990b4856Slling * lifting off to zpool_import_props(), and then mounts the datasets contained 1677990b4856Slling * within the pool. 1678fa9e4066Sahrens */ 1679fa9e4066Sahrens static int 1680fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts, 16814b964adaSGeorge Wilson nvlist_t *props, int flags) 1682fa9e4066Sahrens { 1683fa9e4066Sahrens zpool_handle_t *zhp; 1684fa9e4066Sahrens char *name; 1685fa9e4066Sahrens uint64_t state; 1686eaca9bbdSeschrock uint64_t version; 1687fa9e4066Sahrens 1688fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1689fa9e4066Sahrens &name) == 0); 1690fa9e4066Sahrens 1691fa9e4066Sahrens verify(nvlist_lookup_uint64(config, 1692fa9e4066Sahrens ZPOOL_CONFIG_POOL_STATE, &state) == 0); 1693eaca9bbdSeschrock verify(nvlist_lookup_uint64(config, 1694eaca9bbdSeschrock ZPOOL_CONFIG_VERSION, &version) == 0); 1695ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(version)) { 1696eaca9bbdSeschrock (void) fprintf(stderr, gettext("cannot import '%s': pool " 1697ad135b5dSChristopher Siden "is formatted using an unsupported ZFS version\n"), name); 1698eaca9bbdSeschrock return (1); 16994b964adaSGeorge Wilson } else if (state != POOL_STATE_EXPORTED && 17004b964adaSGeorge Wilson !(flags & ZFS_IMPORT_ANY_HOST)) { 170195173954Sek110237 uint64_t hostid; 170295173954Sek110237 170395173954Sek110237 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, 170495173954Sek110237 &hostid) == 0) { 170595173954Sek110237 if ((unsigned long)hostid != gethostid()) { 170695173954Sek110237 char *hostname; 170795173954Sek110237 uint64_t timestamp; 170895173954Sek110237 time_t t; 170995173954Sek110237 171095173954Sek110237 verify(nvlist_lookup_string(config, 171195173954Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 171295173954Sek110237 verify(nvlist_lookup_uint64(config, 171395173954Sek110237 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0); 171495173954Sek110237 t = timestamp; 171595173954Sek110237 (void) fprintf(stderr, gettext("cannot import " 171695173954Sek110237 "'%s': pool may be in use from other " 171795173954Sek110237 "system, it was last accessed by %s " 171895173954Sek110237 "(hostid: 0x%lx) on %s"), name, hostname, 171995173954Sek110237 (unsigned long)hostid, 172095173954Sek110237 asctime(localtime(&t))); 172195173954Sek110237 (void) fprintf(stderr, gettext("use '-f' to " 172295173954Sek110237 "import anyway\n")); 1723fa9e4066Sahrens return (1); 1724fa9e4066Sahrens } 172595173954Sek110237 } else { 172695173954Sek110237 (void) fprintf(stderr, gettext("cannot import '%s': " 172795173954Sek110237 "pool may be in use from other system\n"), name); 172895173954Sek110237 (void) fprintf(stderr, gettext("use '-f' to import " 172995173954Sek110237 "anyway\n")); 173095173954Sek110237 return (1); 173195173954Sek110237 } 173295173954Sek110237 } 1733fa9e4066Sahrens 17344b964adaSGeorge Wilson if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) 1735fa9e4066Sahrens return (1); 1736fa9e4066Sahrens 1737fa9e4066Sahrens if (newname != NULL) 1738fa9e4066Sahrens name = (char *)newname; 1739fa9e4066Sahrens 17404f0f5e5bSVictor Latushkin if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) 17414f0f5e5bSVictor Latushkin return (1); 1742fa9e4066Sahrens 1743379c004dSEric Schrock if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 1744f9af39baSGeorge Wilson !(flags & ZFS_IMPORT_ONLY) && 1745379c004dSEric Schrock zpool_enable_datasets(zhp, mntopts, 0) != 0) { 1746fa9e4066Sahrens zpool_close(zhp); 1747fa9e4066Sahrens return (1); 1748fa9e4066Sahrens } 1749fa9e4066Sahrens 1750fa9e4066Sahrens zpool_close(zhp); 1751468c413aSTim Haley return (0); 1752fa9e4066Sahrens } 1753fa9e4066Sahrens 1754fa9e4066Sahrens /* 17554c58d714Sdarrenm * zpool import [-d dir] [-D] 17562f8aaab3Seschrock * import [-o mntopts] [-o prop=value] ... [-R root] [-D] 17572f8aaab3Seschrock * [-d dir | -c cachefile] [-f] -a 17582f8aaab3Seschrock * import [-o mntopts] [-o prop=value] ... [-R root] [-D] 1759468c413aSTim Haley * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool] 17602f8aaab3Seschrock * 17612f8aaab3Seschrock * -c Read pool information from a cachefile instead of searching 17622f8aaab3Seschrock * devices. 1763fa9e4066Sahrens * 1764fa9e4066Sahrens * -d Scan in a specific directory, other than /dev/dsk. More than 1765fa9e4066Sahrens * one directory can be specified using multiple '-d' options. 1766fa9e4066Sahrens * 17674c58d714Sdarrenm * -D Scan for previously destroyed pools or import all or only 17684c58d714Sdarrenm * specified destroyed pools. 17694c58d714Sdarrenm * 1770fa9e4066Sahrens * -R Temporarily import the pool, with all mountpoints relative to 1771fa9e4066Sahrens * the given root. The pool will remain exported when the machine 1772fa9e4066Sahrens * is rebooted. 1773fa9e4066Sahrens * 1774468c413aSTim Haley * -V Import even in the presence of faulted vdevs. This is an 1775c5904d13Seschrock * intentionally undocumented option for testing purposes, and 1776c5904d13Seschrock * treats the pool configuration as complete, leaving any bad 17774f0f5e5bSVictor Latushkin * vdevs in the FAULTED state. In other words, it does verbatim 17784f0f5e5bSVictor Latushkin * import. 1779c5904d13Seschrock * 1780468c413aSTim Haley * -f Force import, even if it appears that the pool is active. 1781468c413aSTim Haley * 1782468c413aSTim Haley * -F Attempt rewind if necessary. 1783468c413aSTim Haley * 1784468c413aSTim Haley * -n See if rewind would work, but don't actually rewind. 1785468c413aSTim Haley * 1786f9af39baSGeorge Wilson * -N Import the pool but don't mount datasets. 1787f9af39baSGeorge Wilson * 1788f9af39baSGeorge Wilson * -T Specify a starting txg to use for import. This option is 1789f9af39baSGeorge Wilson * intentionally undocumented option for testing purposes. 1790f9af39baSGeorge Wilson * 1791fa9e4066Sahrens * -a Import all pools found. 1792fa9e4066Sahrens * 1793990b4856Slling * -o Set property=value and/or temporary mount options (without '='). 1794ecd6cf80Smarks * 1795fa9e4066Sahrens * The import command scans for pools to import, and import pools based on pool 1796fa9e4066Sahrens * name and GUID. The pool can also be renamed as part of the import process. 1797fa9e4066Sahrens */ 1798fa9e4066Sahrens int 1799fa9e4066Sahrens zpool_do_import(int argc, char **argv) 1800fa9e4066Sahrens { 1801fa9e4066Sahrens char **searchdirs = NULL; 1802fa9e4066Sahrens int nsearch = 0; 1803fa9e4066Sahrens int c; 1804d41c4376SMark J Musante int err = 0; 18052f8aaab3Seschrock nvlist_t *pools = NULL; 180699653d4eSeschrock boolean_t do_all = B_FALSE; 180799653d4eSeschrock boolean_t do_destroyed = B_FALSE; 1808fa9e4066Sahrens char *mntopts = NULL; 1809fa9e4066Sahrens nvpair_t *elem; 1810fa9e4066Sahrens nvlist_t *config; 181124e697d4Sck153898 uint64_t searchguid = 0; 181224e697d4Sck153898 char *searchname = NULL; 1813990b4856Slling char *propval; 1814fa9e4066Sahrens nvlist_t *found_config; 1815468c413aSTim Haley nvlist_t *policy = NULL; 1816ecd6cf80Smarks nvlist_t *props = NULL; 181799653d4eSeschrock boolean_t first; 18184b964adaSGeorge Wilson int flags = ZFS_IMPORT_NORMAL; 1819468c413aSTim Haley uint32_t rewind_policy = ZPOOL_NO_REWIND; 1820468c413aSTim Haley boolean_t dryrun = B_FALSE; 1821468c413aSTim Haley boolean_t do_rewind = B_FALSE; 1822468c413aSTim Haley boolean_t xtreme_rewind = B_FALSE; 1823f9af39baSGeorge Wilson uint64_t pool_state, txg = -1ULL; 18242f8aaab3Seschrock char *cachefile = NULL; 1825d41c4376SMark J Musante importargs_t idata = { 0 }; 1826f9af39baSGeorge Wilson char *endptr; 1827fa9e4066Sahrens 1828fa9e4066Sahrens /* check options */ 1829f9af39baSGeorge Wilson while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) { 1830fa9e4066Sahrens switch (c) { 1831fa9e4066Sahrens case 'a': 183299653d4eSeschrock do_all = B_TRUE; 1833fa9e4066Sahrens break; 18342f8aaab3Seschrock case 'c': 18352f8aaab3Seschrock cachefile = optarg; 18362f8aaab3Seschrock break; 1837fa9e4066Sahrens case 'd': 1838fa9e4066Sahrens if (searchdirs == NULL) { 1839fa9e4066Sahrens searchdirs = safe_malloc(sizeof (char *)); 1840fa9e4066Sahrens } else { 1841fa9e4066Sahrens char **tmp = safe_malloc((nsearch + 1) * 1842fa9e4066Sahrens sizeof (char *)); 1843fa9e4066Sahrens bcopy(searchdirs, tmp, nsearch * 1844fa9e4066Sahrens sizeof (char *)); 1845fa9e4066Sahrens free(searchdirs); 1846fa9e4066Sahrens searchdirs = tmp; 1847fa9e4066Sahrens } 1848fa9e4066Sahrens searchdirs[nsearch++] = optarg; 1849fa9e4066Sahrens break; 18504c58d714Sdarrenm case 'D': 185199653d4eSeschrock do_destroyed = B_TRUE; 18524c58d714Sdarrenm break; 1853fa9e4066Sahrens case 'f': 18544b964adaSGeorge Wilson flags |= ZFS_IMPORT_ANY_HOST; 1855fa9e4066Sahrens break; 1856c5904d13Seschrock case 'F': 1857468c413aSTim Haley do_rewind = B_TRUE; 1858468c413aSTim Haley break; 18594b964adaSGeorge Wilson case 'm': 18604b964adaSGeorge Wilson flags |= ZFS_IMPORT_MISSING_LOG; 18614b964adaSGeorge Wilson break; 1862468c413aSTim Haley case 'n': 1863468c413aSTim Haley dryrun = B_TRUE; 1864c5904d13Seschrock break; 1865f9af39baSGeorge Wilson case 'N': 1866f9af39baSGeorge Wilson flags |= ZFS_IMPORT_ONLY; 1867f9af39baSGeorge Wilson break; 1868fa9e4066Sahrens case 'o': 1869990b4856Slling if ((propval = strchr(optarg, '=')) != NULL) { 1870990b4856Slling *propval = '\0'; 1871990b4856Slling propval++; 18720a48a24eStimh if (add_prop_list(optarg, propval, 18730a48a24eStimh &props, B_TRUE)) 1874990b4856Slling goto error; 1875990b4856Slling } else { 1876fa9e4066Sahrens mntopts = optarg; 1877990b4856Slling } 1878fa9e4066Sahrens break; 1879fa9e4066Sahrens case 'R': 1880990b4856Slling if (add_prop_list(zpool_prop_to_name( 18810a48a24eStimh ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 1882990b4856Slling goto error; 18832f8aaab3Seschrock if (nvlist_lookup_string(props, 18842f8aaab3Seschrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 18852f8aaab3Seschrock &propval) == 0) 18862f8aaab3Seschrock break; 1887990b4856Slling if (add_prop_list(zpool_prop_to_name( 18880a48a24eStimh ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 1889990b4856Slling goto error; 1890fa9e4066Sahrens break; 1891f9af39baSGeorge Wilson case 'T': 1892f9af39baSGeorge Wilson errno = 0; 1893e42d2059SMatthew Ahrens txg = strtoull(optarg, &endptr, 0); 1894f9af39baSGeorge Wilson if (errno != 0 || *endptr != '\0') { 1895f9af39baSGeorge Wilson (void) fprintf(stderr, 1896f9af39baSGeorge Wilson gettext("invalid txg value\n")); 1897f9af39baSGeorge Wilson usage(B_FALSE); 1898f9af39baSGeorge Wilson } 1899f9af39baSGeorge Wilson rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; 1900f9af39baSGeorge Wilson break; 1901468c413aSTim Haley case 'V': 19024b964adaSGeorge Wilson flags |= ZFS_IMPORT_VERBATIM; 1903468c413aSTim Haley break; 1904468c413aSTim Haley case 'X': 1905468c413aSTim Haley xtreme_rewind = B_TRUE; 1906468c413aSTim Haley break; 1907fa9e4066Sahrens case ':': 1908fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for " 1909fa9e4066Sahrens "'%c' option\n"), optopt); 191099653d4eSeschrock usage(B_FALSE); 1911fa9e4066Sahrens break; 1912fa9e4066Sahrens case '?': 1913fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1914fa9e4066Sahrens optopt); 191599653d4eSeschrock usage(B_FALSE); 1916fa9e4066Sahrens } 1917fa9e4066Sahrens } 1918fa9e4066Sahrens 1919fa9e4066Sahrens argc -= optind; 1920fa9e4066Sahrens argv += optind; 1921fa9e4066Sahrens 19222f8aaab3Seschrock if (cachefile && nsearch != 0) { 19232f8aaab3Seschrock (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); 19242f8aaab3Seschrock usage(B_FALSE); 19252f8aaab3Seschrock } 19262f8aaab3Seschrock 1927468c413aSTim Haley if ((dryrun || xtreme_rewind) && !do_rewind) { 1928468c413aSTim Haley (void) fprintf(stderr, 1929468c413aSTim Haley gettext("-n or -X only meaningful with -F\n")); 1930468c413aSTim Haley usage(B_FALSE); 1931468c413aSTim Haley } 1932468c413aSTim Haley if (dryrun) 1933468c413aSTim Haley rewind_policy = ZPOOL_TRY_REWIND; 1934468c413aSTim Haley else if (do_rewind) 1935468c413aSTim Haley rewind_policy = ZPOOL_DO_REWIND; 1936468c413aSTim Haley if (xtreme_rewind) 1937468c413aSTim Haley rewind_policy |= ZPOOL_EXTREME_REWIND; 1938468c413aSTim Haley 1939468c413aSTim Haley /* In the future, we can capture further policy and include it here */ 1940468c413aSTim Haley if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 1941f9af39baSGeorge Wilson nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 || 1942468c413aSTim Haley nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 1943468c413aSTim Haley goto error; 1944468c413aSTim Haley 1945fa9e4066Sahrens if (searchdirs == NULL) { 1946fa9e4066Sahrens searchdirs = safe_malloc(sizeof (char *)); 1947fa9e4066Sahrens searchdirs[0] = "/dev/dsk"; 1948fa9e4066Sahrens nsearch = 1; 1949fa9e4066Sahrens } 1950fa9e4066Sahrens 1951fa9e4066Sahrens /* check argument count */ 1952fa9e4066Sahrens if (do_all) { 1953fa9e4066Sahrens if (argc != 0) { 1954fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 195599653d4eSeschrock usage(B_FALSE); 1956fa9e4066Sahrens } 1957fa9e4066Sahrens } else { 1958fa9e4066Sahrens if (argc > 2) { 1959fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 196099653d4eSeschrock usage(B_FALSE); 1961fa9e4066Sahrens } 1962fa9e4066Sahrens 1963fa9e4066Sahrens /* 1964fa9e4066Sahrens * Check for the SYS_CONFIG privilege. We do this explicitly 1965fa9e4066Sahrens * here because otherwise any attempt to discover pools will 1966fa9e4066Sahrens * silently fail. 1967fa9e4066Sahrens */ 1968fa9e4066Sahrens if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) { 1969fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot " 1970fa9e4066Sahrens "discover pools: permission denied\n")); 197199653d4eSeschrock free(searchdirs); 1972468c413aSTim Haley nvlist_free(policy); 1973fa9e4066Sahrens return (1); 1974fa9e4066Sahrens } 1975fa9e4066Sahrens } 1976fa9e4066Sahrens 1977fa9e4066Sahrens /* 1978fa9e4066Sahrens * Depending on the arguments given, we do one of the following: 1979fa9e4066Sahrens * 1980fa9e4066Sahrens * <none> Iterate through all pools and display information about 1981fa9e4066Sahrens * each one. 1982fa9e4066Sahrens * 1983fa9e4066Sahrens * -a Iterate through all pools and try to import each one. 1984fa9e4066Sahrens * 1985fa9e4066Sahrens * <id> Find the pool that corresponds to the given GUID/pool 1986fa9e4066Sahrens * name and import that one. 19874c58d714Sdarrenm * 19884c58d714Sdarrenm * -D Above options applies only to destroyed pools. 1989fa9e4066Sahrens */ 1990fa9e4066Sahrens if (argc != 0) { 1991fa9e4066Sahrens char *endptr; 1992fa9e4066Sahrens 1993fa9e4066Sahrens errno = 0; 1994fa9e4066Sahrens searchguid = strtoull(argv[0], &endptr, 10); 19959edf9ebdSPrasad Joshi if (errno != 0 || *endptr != '\0') { 1996fa9e4066Sahrens searchname = argv[0]; 19979edf9ebdSPrasad Joshi searchguid = 0; 19989edf9ebdSPrasad Joshi } 1999fa9e4066Sahrens found_config = NULL; 2000fa9e4066Sahrens 200124e697d4Sck153898 /* 2002d41c4376SMark J Musante * User specified a name or guid. Ensure it's unique. 200324e697d4Sck153898 */ 2004d41c4376SMark J Musante idata.unique = B_TRUE; 200524e697d4Sck153898 } 200624e697d4Sck153898 2007d41c4376SMark J Musante 2008d41c4376SMark J Musante idata.path = searchdirs; 2009d41c4376SMark J Musante idata.paths = nsearch; 2010d41c4376SMark J Musante idata.poolname = searchname; 2011d41c4376SMark J Musante idata.guid = searchguid; 2012d41c4376SMark J Musante idata.cachefile = cachefile; 2013d41c4376SMark J Musante 2014d41c4376SMark J Musante pools = zpool_search_import(g_zfs, &idata); 2015d41c4376SMark J Musante 2016d41c4376SMark J Musante if (pools != NULL && idata.exists && 2017d41c4376SMark J Musante (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { 2018d41c4376SMark J Musante (void) fprintf(stderr, gettext("cannot import '%s': " 2019d41c4376SMark J Musante "a pool with that name already exists\n"), 2020d41c4376SMark J Musante argv[0]); 2021d41c4376SMark J Musante (void) fprintf(stderr, gettext("use the form '%s " 2022d41c4376SMark J Musante "<pool | id> <newpool>' to give it a new name\n"), 2023d41c4376SMark J Musante "zpool import"); 2024d41c4376SMark J Musante err = 1; 2025d41c4376SMark J Musante } else if (pools == NULL && idata.exists) { 2026d41c4376SMark J Musante (void) fprintf(stderr, gettext("cannot import '%s': " 2027d41c4376SMark J Musante "a pool with that name is already created/imported,\n"), 2028d41c4376SMark J Musante argv[0]); 2029d41c4376SMark J Musante (void) fprintf(stderr, gettext("and no additional pools " 2030d41c4376SMark J Musante "with that name were found\n")); 2031d41c4376SMark J Musante err = 1; 2032d41c4376SMark J Musante } else if (pools == NULL) { 203324e697d4Sck153898 if (argc != 0) { 203424e697d4Sck153898 (void) fprintf(stderr, gettext("cannot import '%s': " 203524e697d4Sck153898 "no such pool available\n"), argv[0]); 203624e697d4Sck153898 } 2037d41c4376SMark J Musante err = 1; 2038d41c4376SMark J Musante } 2039d41c4376SMark J Musante 2040d41c4376SMark J Musante if (err == 1) { 204124e697d4Sck153898 free(searchdirs); 2042468c413aSTim Haley nvlist_free(policy); 204324e697d4Sck153898 return (1); 204424e697d4Sck153898 } 204524e697d4Sck153898 204624e697d4Sck153898 /* 204724e697d4Sck153898 * At this point we have a list of import candidate configs. Even if 204824e697d4Sck153898 * we were searching by pool name or guid, we still need to 204924e697d4Sck153898 * post-process the list to deal with pool state and possible 205024e697d4Sck153898 * duplicate names. 205124e697d4Sck153898 */ 2052fa9e4066Sahrens err = 0; 2053fa9e4066Sahrens elem = NULL; 205499653d4eSeschrock first = B_TRUE; 2055fa9e4066Sahrens while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 2056fa9e4066Sahrens 2057fa9e4066Sahrens verify(nvpair_value_nvlist(elem, &config) == 0); 2058fa9e4066Sahrens 20594c58d714Sdarrenm verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 20604c58d714Sdarrenm &pool_state) == 0); 20614c58d714Sdarrenm if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) 20624c58d714Sdarrenm continue; 20634c58d714Sdarrenm if (do_destroyed && pool_state != POOL_STATE_DESTROYED) 20644c58d714Sdarrenm continue; 20654c58d714Sdarrenm 2066468c413aSTim Haley verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY, 2067468c413aSTim Haley policy) == 0); 2068468c413aSTim Haley 2069fa9e4066Sahrens if (argc == 0) { 2070fa9e4066Sahrens if (first) 207199653d4eSeschrock first = B_FALSE; 20723bb79becSeschrock else if (!do_all) 2073fa9e4066Sahrens (void) printf("\n"); 2074fa9e4066Sahrens 2075468c413aSTim Haley if (do_all) { 2076fa9e4066Sahrens err |= do_import(config, NULL, mntopts, 20774b964adaSGeorge Wilson props, flags); 2078468c413aSTim Haley } else { 2079fa9e4066Sahrens show_import(config); 2080468c413aSTim Haley } 2081fa9e4066Sahrens } else if (searchname != NULL) { 2082fa9e4066Sahrens char *name; 2083fa9e4066Sahrens 2084fa9e4066Sahrens /* 2085fa9e4066Sahrens * We are searching for a pool based on name. 2086fa9e4066Sahrens */ 2087fa9e4066Sahrens verify(nvlist_lookup_string(config, 2088fa9e4066Sahrens ZPOOL_CONFIG_POOL_NAME, &name) == 0); 2089fa9e4066Sahrens 2090fa9e4066Sahrens if (strcmp(name, searchname) == 0) { 2091fa9e4066Sahrens if (found_config != NULL) { 2092fa9e4066Sahrens (void) fprintf(stderr, gettext( 2093fa9e4066Sahrens "cannot import '%s': more than " 2094fa9e4066Sahrens "one matching pool\n"), searchname); 2095fa9e4066Sahrens (void) fprintf(stderr, gettext( 2096fa9e4066Sahrens "import by numeric ID instead\n")); 209799653d4eSeschrock err = B_TRUE; 2098fa9e4066Sahrens } 2099fa9e4066Sahrens found_config = config; 2100fa9e4066Sahrens } 2101fa9e4066Sahrens } else { 2102fa9e4066Sahrens uint64_t guid; 2103fa9e4066Sahrens 2104fa9e4066Sahrens /* 2105fa9e4066Sahrens * Search for a pool by guid. 2106fa9e4066Sahrens */ 2107fa9e4066Sahrens verify(nvlist_lookup_uint64(config, 2108fa9e4066Sahrens ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 2109fa9e4066Sahrens 2110fa9e4066Sahrens if (guid == searchguid) 2111fa9e4066Sahrens found_config = config; 2112fa9e4066Sahrens } 2113fa9e4066Sahrens } 2114fa9e4066Sahrens 2115fa9e4066Sahrens /* 2116fa9e4066Sahrens * If we were searching for a specific pool, verify that we found a 2117fa9e4066Sahrens * pool, and then do the import. 2118fa9e4066Sahrens */ 2119fa9e4066Sahrens if (argc != 0 && err == 0) { 2120fa9e4066Sahrens if (found_config == NULL) { 2121fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot import '%s': " 2122fa9e4066Sahrens "no such pool available\n"), argv[0]); 212399653d4eSeschrock err = B_TRUE; 2124fa9e4066Sahrens } else { 2125fa9e4066Sahrens err |= do_import(found_config, argc == 1 ? NULL : 21264b964adaSGeorge Wilson argv[1], mntopts, props, flags); 2127fa9e4066Sahrens } 2128fa9e4066Sahrens } 2129fa9e4066Sahrens 2130fa9e4066Sahrens /* 2131fa9e4066Sahrens * If we were just looking for pools, report an error if none were 2132fa9e4066Sahrens * found. 2133fa9e4066Sahrens */ 2134fa9e4066Sahrens if (argc == 0 && first) 2135fa9e4066Sahrens (void) fprintf(stderr, 2136fa9e4066Sahrens gettext("no pools available to import\n")); 2137fa9e4066Sahrens 2138ecd6cf80Smarks error: 2139ecd6cf80Smarks nvlist_free(props); 2140fa9e4066Sahrens nvlist_free(pools); 2141468c413aSTim Haley nvlist_free(policy); 214299653d4eSeschrock free(searchdirs); 2143fa9e4066Sahrens 2144fa9e4066Sahrens return (err ? 1 : 0); 2145fa9e4066Sahrens } 2146fa9e4066Sahrens 2147fa9e4066Sahrens typedef struct iostat_cbdata { 21484263d13fSGeorge Wilson boolean_t cb_verbose; 2149fa9e4066Sahrens int cb_namewidth; 21504263d13fSGeorge Wilson int cb_iteration; 21514263d13fSGeorge Wilson zpool_list_t *cb_list; 2152fa9e4066Sahrens } iostat_cbdata_t; 2153fa9e4066Sahrens 2154fa9e4066Sahrens static void 2155fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb) 2156fa9e4066Sahrens { 2157fa9e4066Sahrens int i = 0; 2158fa9e4066Sahrens 2159fa9e4066Sahrens for (i = 0; i < cb->cb_namewidth; i++) 2160fa9e4066Sahrens (void) printf("-"); 2161fa9e4066Sahrens (void) printf(" ----- ----- ----- ----- ----- -----\n"); 2162fa9e4066Sahrens } 2163fa9e4066Sahrens 2164fa9e4066Sahrens static void 2165fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb) 2166fa9e4066Sahrens { 2167fa9e4066Sahrens (void) printf("%*s capacity operations bandwidth\n", 2168fa9e4066Sahrens cb->cb_namewidth, ""); 2169485bbbf5SGeorge Wilson (void) printf("%-*s alloc free read write read write\n", 2170fa9e4066Sahrens cb->cb_namewidth, "pool"); 2171fa9e4066Sahrens print_iostat_separator(cb); 2172fa9e4066Sahrens } 2173fa9e4066Sahrens 2174fa9e4066Sahrens /* 2175fa9e4066Sahrens * Display a single statistic. 2176fa9e4066Sahrens */ 2177990b4856Slling static void 2178fa9e4066Sahrens print_one_stat(uint64_t value) 2179fa9e4066Sahrens { 2180fa9e4066Sahrens char buf[64]; 2181fa9e4066Sahrens 2182fa9e4066Sahrens zfs_nicenum(value, buf, sizeof (buf)); 2183fa9e4066Sahrens (void) printf(" %5s", buf); 2184fa9e4066Sahrens } 2185fa9e4066Sahrens 2186fa9e4066Sahrens /* 2187fa9e4066Sahrens * Print out all the statistics for the given vdev. This can either be the 2188fa9e4066Sahrens * toplevel configuration, or called recursively. If 'name' is NULL, then this 2189fa9e4066Sahrens * is a verbose output, and we don't want to display the toplevel pool stats. 2190fa9e4066Sahrens */ 2191fa9e4066Sahrens void 2192c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, 2193c67d9675Seschrock nvlist_t *newnv, iostat_cbdata_t *cb, int depth) 2194fa9e4066Sahrens { 2195fa9e4066Sahrens nvlist_t **oldchild, **newchild; 2196fa9e4066Sahrens uint_t c, children; 2197fa9e4066Sahrens vdev_stat_t *oldvs, *newvs; 2198fa9e4066Sahrens vdev_stat_t zerovs = { 0 }; 2199fa9e4066Sahrens uint64_t tdelta; 2200fa9e4066Sahrens double scale; 2201afefbcddSeschrock char *vname; 2202fa9e4066Sahrens 2203fa9e4066Sahrens if (oldnv != NULL) { 22043f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(oldnv, 22053f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); 2206fa9e4066Sahrens } else { 2207fa9e4066Sahrens oldvs = &zerovs; 2208fa9e4066Sahrens } 2209fa9e4066Sahrens 22103f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, 2211fa9e4066Sahrens (uint64_t **)&newvs, &c) == 0); 2212fa9e4066Sahrens 2213fa9e4066Sahrens if (strlen(name) + depth > cb->cb_namewidth) 2214fa9e4066Sahrens (void) printf("%*s%s", depth, "", name); 2215fa9e4066Sahrens else 2216fa9e4066Sahrens (void) printf("%*s%s%*s", depth, "", name, 2217fa9e4066Sahrens (int)(cb->cb_namewidth - strlen(name) - depth), ""); 2218fa9e4066Sahrens 2219fa9e4066Sahrens tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; 2220fa9e4066Sahrens 2221fa9e4066Sahrens if (tdelta == 0) 2222fa9e4066Sahrens scale = 1.0; 2223fa9e4066Sahrens else 2224fa9e4066Sahrens scale = (double)NANOSEC / tdelta; 2225fa9e4066Sahrens 2226fa9e4066Sahrens /* only toplevel vdevs have capacity stats */ 2227fa9e4066Sahrens if (newvs->vs_space == 0) { 2228fa9e4066Sahrens (void) printf(" - -"); 2229fa9e4066Sahrens } else { 2230fa9e4066Sahrens print_one_stat(newvs->vs_alloc); 2231fa9e4066Sahrens print_one_stat(newvs->vs_space - newvs->vs_alloc); 2232fa9e4066Sahrens } 2233fa9e4066Sahrens 2234fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] - 2235fa9e4066Sahrens oldvs->vs_ops[ZIO_TYPE_READ]))); 2236fa9e4066Sahrens 2237fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] - 2238fa9e4066Sahrens oldvs->vs_ops[ZIO_TYPE_WRITE]))); 2239fa9e4066Sahrens 2240fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] - 2241fa9e4066Sahrens oldvs->vs_bytes[ZIO_TYPE_READ]))); 2242fa9e4066Sahrens 2243fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] - 2244fa9e4066Sahrens oldvs->vs_bytes[ZIO_TYPE_WRITE]))); 2245fa9e4066Sahrens 2246fa9e4066Sahrens (void) printf("\n"); 2247fa9e4066Sahrens 2248fa9e4066Sahrens if (!cb->cb_verbose) 2249fa9e4066Sahrens return; 2250fa9e4066Sahrens 2251fa9e4066Sahrens if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, 2252fa9e4066Sahrens &newchild, &children) != 0) 2253fa9e4066Sahrens return; 2254fa9e4066Sahrens 2255fa9e4066Sahrens if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, 2256fa9e4066Sahrens &oldchild, &c) != 0) 2257fa9e4066Sahrens return; 2258fa9e4066Sahrens 2259afefbcddSeschrock for (c = 0; c < children; c++) { 22609d439f90SMike Harsch uint64_t ishole = B_FALSE, islog = B_FALSE; 22613f9d6ad7SLin Ling 22629d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, 22639d439f90SMike Harsch &ishole); 22649d439f90SMike Harsch 22659d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, 22669d439f90SMike Harsch &islog); 22679d439f90SMike Harsch 22689d439f90SMike Harsch if (ishole || islog) 22693f9d6ad7SLin Ling continue; 22703f9d6ad7SLin Ling 227188ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE); 2272c67d9675Seschrock print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 2273afefbcddSeschrock newchild[c], cb, depth + 2); 2274afefbcddSeschrock free(vname); 2275afefbcddSeschrock } 2276fa94a07fSbrendan 2277fa94a07fSbrendan /* 22789d439f90SMike Harsch * Log device section 22799d439f90SMike Harsch */ 22809d439f90SMike Harsch 22819d439f90SMike Harsch if (num_logs(newnv) > 0) { 22829d439f90SMike Harsch (void) printf("%-*s - - - - - " 22839d439f90SMike Harsch "-\n", cb->cb_namewidth, "logs"); 22849d439f90SMike Harsch 22859d439f90SMike Harsch for (c = 0; c < children; c++) { 22869d439f90SMike Harsch uint64_t islog = B_FALSE; 22879d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c], 22889d439f90SMike Harsch ZPOOL_CONFIG_IS_LOG, &islog); 22899d439f90SMike Harsch 22909d439f90SMike Harsch if (islog) { 22919d439f90SMike Harsch vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 22929d439f90SMike Harsch B_FALSE); 22939d439f90SMike Harsch print_vdev_stats(zhp, vname, oldnv ? 22949d439f90SMike Harsch oldchild[c] : NULL, newchild[c], 22959d439f90SMike Harsch cb, depth + 2); 22969d439f90SMike Harsch free(vname); 22979d439f90SMike Harsch } 22989d439f90SMike Harsch } 22999d439f90SMike Harsch 23009d439f90SMike Harsch } 23019d439f90SMike Harsch 23029d439f90SMike Harsch /* 2303fa94a07fSbrendan * Include level 2 ARC devices in iostat output 2304fa94a07fSbrendan */ 2305fa94a07fSbrendan if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, 2306fa94a07fSbrendan &newchild, &children) != 0) 2307fa94a07fSbrendan return; 2308fa94a07fSbrendan 2309fa94a07fSbrendan if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, 2310fa94a07fSbrendan &oldchild, &c) != 0) 2311fa94a07fSbrendan return; 2312fa94a07fSbrendan 2313fa94a07fSbrendan if (children > 0) { 2314fa94a07fSbrendan (void) printf("%-*s - - - - - " 2315fa94a07fSbrendan "-\n", cb->cb_namewidth, "cache"); 2316fa94a07fSbrendan for (c = 0; c < children; c++) { 231788ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 231888ecc943SGeorge Wilson B_FALSE); 2319fa94a07fSbrendan print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 2320fa94a07fSbrendan newchild[c], cb, depth + 2); 2321fa94a07fSbrendan free(vname); 2322fa94a07fSbrendan } 2323fa94a07fSbrendan } 2324fa9e4066Sahrens } 2325fa9e4066Sahrens 2326088e9d47Seschrock static int 2327088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data) 2328088e9d47Seschrock { 2329088e9d47Seschrock iostat_cbdata_t *cb = data; 233094de1d4cSeschrock boolean_t missing; 2331088e9d47Seschrock 2332088e9d47Seschrock /* 2333088e9d47Seschrock * If the pool has disappeared, remove it from the list and continue. 2334088e9d47Seschrock */ 233594de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) 233694de1d4cSeschrock return (-1); 233794de1d4cSeschrock 233894de1d4cSeschrock if (missing) 2339088e9d47Seschrock pool_list_remove(cb->cb_list, zhp); 2340088e9d47Seschrock 2341088e9d47Seschrock return (0); 2342088e9d47Seschrock } 2343088e9d47Seschrock 2344fa9e4066Sahrens /* 2345fa9e4066Sahrens * Callback to print out the iostats for the given pool. 2346fa9e4066Sahrens */ 2347fa9e4066Sahrens int 2348fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data) 2349fa9e4066Sahrens { 2350fa9e4066Sahrens iostat_cbdata_t *cb = data; 2351fa9e4066Sahrens nvlist_t *oldconfig, *newconfig; 2352fa9e4066Sahrens nvlist_t *oldnvroot, *newnvroot; 2353fa9e4066Sahrens 2354088e9d47Seschrock newconfig = zpool_get_config(zhp, &oldconfig); 2355fa9e4066Sahrens 2356088e9d47Seschrock if (cb->cb_iteration == 1) 2357fa9e4066Sahrens oldconfig = NULL; 2358fa9e4066Sahrens 2359fa9e4066Sahrens verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, 2360fa9e4066Sahrens &newnvroot) == 0); 2361fa9e4066Sahrens 2362088e9d47Seschrock if (oldconfig == NULL) 2363fa9e4066Sahrens oldnvroot = NULL; 2364088e9d47Seschrock else 2365088e9d47Seschrock verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, 2366088e9d47Seschrock &oldnvroot) == 0); 2367fa9e4066Sahrens 2368fa9e4066Sahrens /* 2369fa9e4066Sahrens * Print out the statistics for the pool. 2370fa9e4066Sahrens */ 2371c67d9675Seschrock print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0); 2372fa9e4066Sahrens 2373fa9e4066Sahrens if (cb->cb_verbose) 2374fa9e4066Sahrens print_iostat_separator(cb); 2375fa9e4066Sahrens 2376fa9e4066Sahrens return (0); 2377fa9e4066Sahrens } 2378fa9e4066Sahrens 2379fa9e4066Sahrens int 2380fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data) 2381fa9e4066Sahrens { 2382fa9e4066Sahrens iostat_cbdata_t *cb = data; 2383fa9e4066Sahrens nvlist_t *config, *nvroot; 2384fa9e4066Sahrens 2385088e9d47Seschrock if ((config = zpool_get_config(zhp, NULL)) != NULL) { 2386fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2387fa9e4066Sahrens &nvroot) == 0); 2388fa9e4066Sahrens if (!cb->cb_verbose) 2389fa9e4066Sahrens cb->cb_namewidth = strlen(zpool_get_name(zhp)); 2390fa9e4066Sahrens else 2391e1d5e507SFrederik Wessels cb->cb_namewidth = max_width(zhp, nvroot, 0, 2392e1d5e507SFrederik Wessels cb->cb_namewidth); 2393fa9e4066Sahrens } 2394fa9e4066Sahrens 2395fa9e4066Sahrens /* 2396fa9e4066Sahrens * The width must fall into the range [10,38]. The upper limit is the 2397fa9e4066Sahrens * maximum we can have and still fit in 80 columns. 2398fa9e4066Sahrens */ 2399fa9e4066Sahrens if (cb->cb_namewidth < 10) 2400fa9e4066Sahrens cb->cb_namewidth = 10; 2401fa9e4066Sahrens if (cb->cb_namewidth > 38) 2402fa9e4066Sahrens cb->cb_namewidth = 38; 2403fa9e4066Sahrens 2404fa9e4066Sahrens return (0); 2405fa9e4066Sahrens } 2406fa9e4066Sahrens 2407fa9e4066Sahrens /* 24083f9d6ad7SLin Ling * Parse the input string, get the 'interval' and 'count' value if there is one. 2409fa9e4066Sahrens */ 24103f9d6ad7SLin Ling static void 24113f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv, 24123f9d6ad7SLin Ling unsigned long *cnt) 2413fa9e4066Sahrens { 2414fa9e4066Sahrens unsigned long interval = 0, count = 0; 24153f9d6ad7SLin Ling int argc = *argcp, errno; 2416fa9e4066Sahrens 2417fa9e4066Sahrens /* 2418fa9e4066Sahrens * Determine if the last argument is an integer or a pool name 2419fa9e4066Sahrens */ 2420fa9e4066Sahrens if (argc > 0 && isdigit(argv[argc - 1][0])) { 2421fa9e4066Sahrens char *end; 2422fa9e4066Sahrens 2423fa9e4066Sahrens errno = 0; 2424fa9e4066Sahrens interval = strtoul(argv[argc - 1], &end, 10); 2425fa9e4066Sahrens 2426fa9e4066Sahrens if (*end == '\0' && errno == 0) { 2427fa9e4066Sahrens if (interval == 0) { 2428fa9e4066Sahrens (void) fprintf(stderr, gettext("interval " 2429fa9e4066Sahrens "cannot be zero\n")); 243099653d4eSeschrock usage(B_FALSE); 2431fa9e4066Sahrens } 2432fa9e4066Sahrens /* 2433fa9e4066Sahrens * Ignore the last parameter 2434fa9e4066Sahrens */ 2435fa9e4066Sahrens argc--; 2436fa9e4066Sahrens } else { 2437fa9e4066Sahrens /* 2438fa9e4066Sahrens * If this is not a valid number, just plow on. The 2439fa9e4066Sahrens * user will get a more informative error message later 2440fa9e4066Sahrens * on. 2441fa9e4066Sahrens */ 2442fa9e4066Sahrens interval = 0; 2443fa9e4066Sahrens } 2444fa9e4066Sahrens } 2445fa9e4066Sahrens 2446fa9e4066Sahrens /* 2447fa9e4066Sahrens * If the last argument is also an integer, then we have both a count 24483f9d6ad7SLin Ling * and an interval. 2449fa9e4066Sahrens */ 2450fa9e4066Sahrens if (argc > 0 && isdigit(argv[argc - 1][0])) { 2451fa9e4066Sahrens char *end; 2452fa9e4066Sahrens 2453fa9e4066Sahrens errno = 0; 2454fa9e4066Sahrens count = interval; 2455fa9e4066Sahrens interval = strtoul(argv[argc - 1], &end, 10); 2456fa9e4066Sahrens 2457fa9e4066Sahrens if (*end == '\0' && errno == 0) { 2458fa9e4066Sahrens if (interval == 0) { 2459fa9e4066Sahrens (void) fprintf(stderr, gettext("interval " 2460fa9e4066Sahrens "cannot be zero\n")); 246199653d4eSeschrock usage(B_FALSE); 2462fa9e4066Sahrens } 2463fa9e4066Sahrens 2464fa9e4066Sahrens /* 2465fa9e4066Sahrens * Ignore the last parameter 2466fa9e4066Sahrens */ 2467fa9e4066Sahrens argc--; 2468fa9e4066Sahrens } else { 2469fa9e4066Sahrens interval = 0; 2470fa9e4066Sahrens } 2471fa9e4066Sahrens } 2472fa9e4066Sahrens 24733f9d6ad7SLin Ling *iv = interval; 24743f9d6ad7SLin Ling *cnt = count; 24753f9d6ad7SLin Ling *argcp = argc; 24763f9d6ad7SLin Ling } 24773f9d6ad7SLin Ling 24783f9d6ad7SLin Ling static void 24793f9d6ad7SLin Ling get_timestamp_arg(char c) 24803f9d6ad7SLin Ling { 24813f9d6ad7SLin Ling if (c == 'u') 24823f9d6ad7SLin Ling timestamp_fmt = UDATE; 24833f9d6ad7SLin Ling else if (c == 'd') 24843f9d6ad7SLin Ling timestamp_fmt = DDATE; 24853f9d6ad7SLin Ling else 24863f9d6ad7SLin Ling usage(B_FALSE); 24873f9d6ad7SLin Ling } 24883f9d6ad7SLin Ling 24893f9d6ad7SLin Ling /* 24903f9d6ad7SLin Ling * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]] 24913f9d6ad7SLin Ling * 24923f9d6ad7SLin Ling * -v Display statistics for individual vdevs 24933f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format 24943f9d6ad7SLin Ling * 24953f9d6ad7SLin Ling * This command can be tricky because we want to be able to deal with pool 24963f9d6ad7SLin Ling * creation/destruction as well as vdev configuration changes. The bulk of this 24973f9d6ad7SLin Ling * processing is handled by the pool_list_* routines in zpool_iter.c. We rely 24983f9d6ad7SLin Ling * on pool_list_update() to detect the addition of new pools. Configuration 24993f9d6ad7SLin Ling * changes are all handled within libzfs. 25003f9d6ad7SLin Ling */ 25013f9d6ad7SLin Ling int 25023f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv) 25033f9d6ad7SLin Ling { 25043f9d6ad7SLin Ling int c; 25053f9d6ad7SLin Ling int ret; 25063f9d6ad7SLin Ling int npools; 25073f9d6ad7SLin Ling unsigned long interval = 0, count = 0; 25083f9d6ad7SLin Ling zpool_list_t *list; 25093f9d6ad7SLin Ling boolean_t verbose = B_FALSE; 25103f9d6ad7SLin Ling iostat_cbdata_t cb; 25113f9d6ad7SLin Ling 25123f9d6ad7SLin Ling /* check options */ 25133f9d6ad7SLin Ling while ((c = getopt(argc, argv, "T:v")) != -1) { 25143f9d6ad7SLin Ling switch (c) { 25153f9d6ad7SLin Ling case 'T': 25163f9d6ad7SLin Ling get_timestamp_arg(*optarg); 25173f9d6ad7SLin Ling break; 25183f9d6ad7SLin Ling case 'v': 25193f9d6ad7SLin Ling verbose = B_TRUE; 25203f9d6ad7SLin Ling break; 25213f9d6ad7SLin Ling case '?': 25223f9d6ad7SLin Ling (void) fprintf(stderr, gettext("invalid option '%c'\n"), 25233f9d6ad7SLin Ling optopt); 25243f9d6ad7SLin Ling usage(B_FALSE); 25253f9d6ad7SLin Ling } 25263f9d6ad7SLin Ling } 25273f9d6ad7SLin Ling 25283f9d6ad7SLin Ling argc -= optind; 25293f9d6ad7SLin Ling argv += optind; 25303f9d6ad7SLin Ling 25313f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count); 25323f9d6ad7SLin Ling 2533fa9e4066Sahrens /* 2534fa9e4066Sahrens * Construct the list of all interesting pools. 2535fa9e4066Sahrens */ 2536fa9e4066Sahrens ret = 0; 2537b1b8ab34Slling if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL) 2538fa9e4066Sahrens return (1); 2539fa9e4066Sahrens 254099653d4eSeschrock if (pool_list_count(list) == 0 && argc != 0) { 254199653d4eSeschrock pool_list_free(list); 2542fa9e4066Sahrens return (1); 254399653d4eSeschrock } 2544fa9e4066Sahrens 2545fa9e4066Sahrens if (pool_list_count(list) == 0 && interval == 0) { 254699653d4eSeschrock pool_list_free(list); 2547fa9e4066Sahrens (void) fprintf(stderr, gettext("no pools available\n")); 2548fa9e4066Sahrens return (1); 2549fa9e4066Sahrens } 2550fa9e4066Sahrens 2551fa9e4066Sahrens /* 2552fa9e4066Sahrens * Enter the main iostat loop. 2553fa9e4066Sahrens */ 2554fa9e4066Sahrens cb.cb_list = list; 2555fa9e4066Sahrens cb.cb_verbose = verbose; 2556fa9e4066Sahrens cb.cb_iteration = 0; 2557fa9e4066Sahrens cb.cb_namewidth = 0; 2558fa9e4066Sahrens 2559fa9e4066Sahrens for (;;) { 2560fa9e4066Sahrens pool_list_update(list); 2561fa9e4066Sahrens 2562fa9e4066Sahrens if ((npools = pool_list_count(list)) == 0) 2563fa9e4066Sahrens break; 2564fa9e4066Sahrens 2565fa9e4066Sahrens /* 2566088e9d47Seschrock * Refresh all statistics. This is done as an explicit step 2567088e9d47Seschrock * before calculating the maximum name width, so that any 2568088e9d47Seschrock * configuration changes are properly accounted for. 2569088e9d47Seschrock */ 257099653d4eSeschrock (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb); 2571088e9d47Seschrock 2572088e9d47Seschrock /* 2573fa9e4066Sahrens * Iterate over all pools to determine the maximum width 2574fa9e4066Sahrens * for the pool / device name column across all pools. 2575fa9e4066Sahrens */ 2576fa9e4066Sahrens cb.cb_namewidth = 0; 257799653d4eSeschrock (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); 2578fa9e4066Sahrens 257926fd7700SKrishnendu Sadhukhan - Sun Microsystems if (timestamp_fmt != NODATE) 258026fd7700SKrishnendu Sadhukhan - Sun Microsystems print_timestamp(timestamp_fmt); 258126fd7700SKrishnendu Sadhukhan - Sun Microsystems 2582fa9e4066Sahrens /* 2583fa9e4066Sahrens * If it's the first time, or verbose mode, print the header. 2584fa9e4066Sahrens */ 2585fa9e4066Sahrens if (++cb.cb_iteration == 1 || verbose) 2586fa9e4066Sahrens print_iostat_header(&cb); 2587fa9e4066Sahrens 258899653d4eSeschrock (void) pool_list_iter(list, B_FALSE, print_iostat, &cb); 2589fa9e4066Sahrens 2590fa9e4066Sahrens /* 2591fa9e4066Sahrens * If there's more than one pool, and we're not in verbose mode 2592fa9e4066Sahrens * (which prints a separator for us), then print a separator. 2593fa9e4066Sahrens */ 2594fa9e4066Sahrens if (npools > 1 && !verbose) 2595fa9e4066Sahrens print_iostat_separator(&cb); 2596fa9e4066Sahrens 2597fa9e4066Sahrens if (verbose) 2598fa9e4066Sahrens (void) printf("\n"); 2599fa9e4066Sahrens 260039c23413Seschrock /* 260139c23413Seschrock * Flush the output so that redirection to a file isn't buffered 260239c23413Seschrock * indefinitely. 260339c23413Seschrock */ 260439c23413Seschrock (void) fflush(stdout); 260539c23413Seschrock 2606fa9e4066Sahrens if (interval == 0) 2607fa9e4066Sahrens break; 2608fa9e4066Sahrens 2609fa9e4066Sahrens if (count != 0 && --count == 0) 2610fa9e4066Sahrens break; 2611fa9e4066Sahrens 2612fa9e4066Sahrens (void) sleep(interval); 2613fa9e4066Sahrens } 2614fa9e4066Sahrens 2615fa9e4066Sahrens pool_list_free(list); 2616fa9e4066Sahrens 2617fa9e4066Sahrens return (ret); 2618fa9e4066Sahrens } 2619fa9e4066Sahrens 2620fa9e4066Sahrens typedef struct list_cbdata { 26214263d13fSGeorge Wilson boolean_t cb_verbose; 26224263d13fSGeorge Wilson int cb_namewidth; 262399653d4eSeschrock boolean_t cb_scripted; 2624990b4856Slling zprop_list_t *cb_proplist; 2625c58b3526SAdam Stevko boolean_t cb_literal; 2626fa9e4066Sahrens } list_cbdata_t; 2627fa9e4066Sahrens 2628fa9e4066Sahrens /* 2629fa9e4066Sahrens * Given a list of columns to display, output appropriate headers for each one. 2630fa9e4066Sahrens */ 2631990b4856Slling static void 26324263d13fSGeorge Wilson print_header(list_cbdata_t *cb) 2633fa9e4066Sahrens { 26344263d13fSGeorge Wilson zprop_list_t *pl = cb->cb_proplist; 2635ad135b5dSChristopher Siden char headerbuf[ZPOOL_MAXPROPLEN]; 2636990b4856Slling const char *header; 2637990b4856Slling boolean_t first = B_TRUE; 2638990b4856Slling boolean_t right_justify; 26394263d13fSGeorge Wilson size_t width = 0; 2640fa9e4066Sahrens 2641990b4856Slling for (; pl != NULL; pl = pl->pl_next) { 26424263d13fSGeorge Wilson width = pl->pl_width; 26434263d13fSGeorge Wilson if (first && cb->cb_verbose) { 26444263d13fSGeorge Wilson /* 26454263d13fSGeorge Wilson * Reset the width to accommodate the verbose listing 26464263d13fSGeorge Wilson * of devices. 26474263d13fSGeorge Wilson */ 26484263d13fSGeorge Wilson width = cb->cb_namewidth; 26494263d13fSGeorge Wilson } 26504263d13fSGeorge Wilson 2651990b4856Slling if (!first) 2652fa9e4066Sahrens (void) printf(" "); 2653fa9e4066Sahrens else 2654990b4856Slling first = B_FALSE; 2655fa9e4066Sahrens 2656ad135b5dSChristopher Siden right_justify = B_FALSE; 2657ad135b5dSChristopher Siden if (pl->pl_prop != ZPROP_INVAL) { 2658990b4856Slling header = zpool_prop_column_name(pl->pl_prop); 2659990b4856Slling right_justify = zpool_prop_align_right(pl->pl_prop); 2660ad135b5dSChristopher Siden } else { 2661ad135b5dSChristopher Siden int i; 2662ad135b5dSChristopher Siden 2663ad135b5dSChristopher Siden for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 2664ad135b5dSChristopher Siden headerbuf[i] = toupper(pl->pl_user_prop[i]); 2665ad135b5dSChristopher Siden headerbuf[i] = '\0'; 2666ad135b5dSChristopher Siden header = headerbuf; 2667ad135b5dSChristopher Siden } 2668990b4856Slling 2669990b4856Slling if (pl->pl_next == NULL && !right_justify) 2670990b4856Slling (void) printf("%s", header); 2671990b4856Slling else if (right_justify) 26724263d13fSGeorge Wilson (void) printf("%*s", width, header); 2673990b4856Slling else 26744263d13fSGeorge Wilson (void) printf("%-*s", width, header); 26754263d13fSGeorge Wilson 2676fa9e4066Sahrens } 2677fa9e4066Sahrens 2678fa9e4066Sahrens (void) printf("\n"); 2679fa9e4066Sahrens } 2680fa9e4066Sahrens 2681990b4856Slling /* 2682990b4856Slling * Given a pool and a list of properties, print out all the properties according 2683990b4856Slling * to the described layout. 2684990b4856Slling */ 2685990b4856Slling static void 26864263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb) 2687990b4856Slling { 26884263d13fSGeorge Wilson zprop_list_t *pl = cb->cb_proplist; 2689990b4856Slling boolean_t first = B_TRUE; 2690990b4856Slling char property[ZPOOL_MAXPROPLEN]; 2691990b4856Slling char *propstr; 2692990b4856Slling boolean_t right_justify; 26934263d13fSGeorge Wilson size_t width; 2694990b4856Slling 2695990b4856Slling for (; pl != NULL; pl = pl->pl_next) { 26964263d13fSGeorge Wilson 26974263d13fSGeorge Wilson width = pl->pl_width; 26984263d13fSGeorge Wilson if (first && cb->cb_verbose) { 26994263d13fSGeorge Wilson /* 27004263d13fSGeorge Wilson * Reset the width to accommodate the verbose listing 27014263d13fSGeorge Wilson * of devices. 27024263d13fSGeorge Wilson */ 27034263d13fSGeorge Wilson width = cb->cb_namewidth; 27044263d13fSGeorge Wilson } 27054263d13fSGeorge Wilson 2706990b4856Slling if (!first) { 27074263d13fSGeorge Wilson if (cb->cb_scripted) 2708990b4856Slling (void) printf("\t"); 2709990b4856Slling else 2710990b4856Slling (void) printf(" "); 2711990b4856Slling } else { 2712990b4856Slling first = B_FALSE; 2713990b4856Slling } 2714990b4856Slling 2715990b4856Slling right_justify = B_FALSE; 2716990b4856Slling if (pl->pl_prop != ZPROP_INVAL) { 27177a09f97bSGeorge Wilson if (zpool_get_prop(zhp, pl->pl_prop, property, 2718c58b3526SAdam Stevko sizeof (property), NULL, cb->cb_literal) != 0) 2719990b4856Slling propstr = "-"; 2720990b4856Slling else 2721990b4856Slling propstr = property; 2722990b4856Slling 2723990b4856Slling right_justify = zpool_prop_align_right(pl->pl_prop); 2724ad135b5dSChristopher Siden } else if ((zpool_prop_feature(pl->pl_user_prop) || 2725ad135b5dSChristopher Siden zpool_prop_unsupported(pl->pl_user_prop)) && 2726ad135b5dSChristopher Siden zpool_prop_get_feature(zhp, pl->pl_user_prop, property, 2727ad135b5dSChristopher Siden sizeof (property)) == 0) { 2728ad135b5dSChristopher Siden propstr = property; 2729990b4856Slling } else { 2730990b4856Slling propstr = "-"; 2731990b4856Slling } 2732990b4856Slling 2733990b4856Slling 2734990b4856Slling /* 2735990b4856Slling * If this is being called in scripted mode, or if this is the 2736990b4856Slling * last column and it is left-justified, don't include a width 2737990b4856Slling * format specifier. 2738990b4856Slling */ 27394263d13fSGeorge Wilson if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 2740990b4856Slling (void) printf("%s", propstr); 2741990b4856Slling else if (right_justify) 2742990b4856Slling (void) printf("%*s", width, propstr); 2743990b4856Slling else 2744990b4856Slling (void) printf("%-*s", width, propstr); 2745990b4856Slling } 2746990b4856Slling 2747990b4856Slling (void) printf("\n"); 2748990b4856Slling } 2749990b4856Slling 27504263d13fSGeorge Wilson static void 27517a09f97bSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted, 27527a09f97bSGeorge Wilson boolean_t valid) 27534263d13fSGeorge Wilson { 27544263d13fSGeorge Wilson char propval[64]; 27554263d13fSGeorge Wilson boolean_t fixed; 27564263d13fSGeorge Wilson size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); 27574263d13fSGeorge Wilson 27587a09f97bSGeorge Wilson switch (prop) { 27597a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ: 27607a09f97bSGeorge Wilson if (value == 0) 27614263d13fSGeorge Wilson (void) strlcpy(propval, "-", sizeof (propval)); 27622e4c9986SGeorge Wilson else 27632e4c9986SGeorge Wilson zfs_nicenum(value, propval, sizeof (propval)); 27647a09f97bSGeorge Wilson break; 27657a09f97bSGeorge Wilson case ZPOOL_PROP_FRAGMENTATION: 27667a09f97bSGeorge Wilson if (value == ZFS_FRAG_INVALID) { 27677a09f97bSGeorge Wilson (void) strlcpy(propval, "-", sizeof (propval)); 27687a09f97bSGeorge Wilson } else { 27697a09f97bSGeorge Wilson (void) snprintf(propval, sizeof (propval), "%llu%%", 27707a09f97bSGeorge Wilson value); 27717a09f97bSGeorge Wilson } 27727a09f97bSGeorge Wilson break; 27737a09f97bSGeorge Wilson case ZPOOL_PROP_CAPACITY: 27747a09f97bSGeorge Wilson (void) snprintf(propval, sizeof (propval), "%llu%%", value); 27757a09f97bSGeorge Wilson break; 27767a09f97bSGeorge Wilson default: 27777a09f97bSGeorge Wilson zfs_nicenum(value, propval, sizeof (propval)); 27787a09f97bSGeorge Wilson } 27797a09f97bSGeorge Wilson 27807a09f97bSGeorge Wilson if (!valid) 27817a09f97bSGeorge Wilson (void) strlcpy(propval, "-", sizeof (propval)); 27824263d13fSGeorge Wilson 27834263d13fSGeorge Wilson if (scripted) 27844263d13fSGeorge Wilson (void) printf("\t%s", propval); 27854263d13fSGeorge Wilson else 27864263d13fSGeorge Wilson (void) printf(" %*s", width, propval); 27874263d13fSGeorge Wilson } 27884263d13fSGeorge Wilson 27894263d13fSGeorge Wilson void 27904263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 27914263d13fSGeorge Wilson list_cbdata_t *cb, int depth) 27924263d13fSGeorge Wilson { 27934263d13fSGeorge Wilson nvlist_t **child; 27944263d13fSGeorge Wilson vdev_stat_t *vs; 27954263d13fSGeorge Wilson uint_t c, children; 27964263d13fSGeorge Wilson char *vname; 27974263d13fSGeorge Wilson boolean_t scripted = cb->cb_scripted; 279852244c09SJohn Wren Kennedy uint64_t islog = B_FALSE; 279952244c09SJohn Wren Kennedy boolean_t haslog = B_FALSE; 280052244c09SJohn Wren Kennedy char *dashes = "%-*s - - - - - -\n"; 28014263d13fSGeorge Wilson 28024263d13fSGeorge Wilson verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 28034263d13fSGeorge Wilson (uint64_t **)&vs, &c) == 0); 28044263d13fSGeorge Wilson 28054263d13fSGeorge Wilson if (name != NULL) { 28067a09f97bSGeorge Wilson boolean_t toplevel = (vs->vs_space != 0); 28077a09f97bSGeorge Wilson uint64_t cap; 28087a09f97bSGeorge Wilson 28094263d13fSGeorge Wilson if (scripted) 28104263d13fSGeorge Wilson (void) printf("\t%s", name); 28114263d13fSGeorge Wilson else if (strlen(name) + depth > cb->cb_namewidth) 28124263d13fSGeorge Wilson (void) printf("%*s%s", depth, "", name); 28134263d13fSGeorge Wilson else 28144263d13fSGeorge Wilson (void) printf("%*s%s%*s", depth, "", name, 28154263d13fSGeorge Wilson (int)(cb->cb_namewidth - strlen(name) - depth), ""); 28164263d13fSGeorge Wilson 28177a09f97bSGeorge Wilson /* 28187a09f97bSGeorge Wilson * Print the properties for the individual vdevs. Some 28197a09f97bSGeorge Wilson * properties are only applicable to toplevel vdevs. The 28207a09f97bSGeorge Wilson * 'toplevel' boolean value is passed to the print_one_column() 28217a09f97bSGeorge Wilson * to indicate that the value is valid. 28227a09f97bSGeorge Wilson */ 28237a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted, 28247a09f97bSGeorge Wilson toplevel); 28257a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted, 28267a09f97bSGeorge Wilson toplevel); 28277a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc, 28287a09f97bSGeorge Wilson scripted, toplevel); 28297a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted, 28307a09f97bSGeorge Wilson B_TRUE); 28312e4c9986SGeorge Wilson print_one_column(ZPOOL_PROP_FRAGMENTATION, 28327a09f97bSGeorge Wilson vs->vs_fragmentation, scripted, 28337a09f97bSGeorge Wilson (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel)); 28347a09f97bSGeorge Wilson cap = (vs->vs_space == 0) ? 0 : 28357a09f97bSGeorge Wilson (vs->vs_alloc * 100 / vs->vs_space); 28367a09f97bSGeorge Wilson print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel); 28374263d13fSGeorge Wilson (void) printf("\n"); 28384263d13fSGeorge Wilson } 28394263d13fSGeorge Wilson 28404263d13fSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 28414263d13fSGeorge Wilson &child, &children) != 0) 28424263d13fSGeorge Wilson return; 28434263d13fSGeorge Wilson 28444263d13fSGeorge Wilson for (c = 0; c < children; c++) { 28454263d13fSGeorge Wilson uint64_t ishole = B_FALSE; 28464263d13fSGeorge Wilson 28474263d13fSGeorge Wilson if (nvlist_lookup_uint64(child[c], 28484263d13fSGeorge Wilson ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) 28494263d13fSGeorge Wilson continue; 28504263d13fSGeorge Wilson 285152244c09SJohn Wren Kennedy if (nvlist_lookup_uint64(child[c], 285252244c09SJohn Wren Kennedy ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) { 285352244c09SJohn Wren Kennedy haslog = B_TRUE; 285452244c09SJohn Wren Kennedy continue; 285552244c09SJohn Wren Kennedy } 285652244c09SJohn Wren Kennedy 28574263d13fSGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 28584263d13fSGeorge Wilson print_list_stats(zhp, vname, child[c], cb, depth + 2); 28594263d13fSGeorge Wilson free(vname); 28604263d13fSGeorge Wilson } 28614263d13fSGeorge Wilson 286252244c09SJohn Wren Kennedy if (haslog == B_TRUE) { 286352244c09SJohn Wren Kennedy /* LINTED E_SEC_PRINTF_VAR_FMT */ 286452244c09SJohn Wren Kennedy (void) printf(dashes, cb->cb_namewidth, "log"); 28654263d13fSGeorge Wilson for (c = 0; c < children; c++) { 286652244c09SJohn Wren Kennedy if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 286752244c09SJohn Wren Kennedy &islog) != 0 || !islog) 286852244c09SJohn Wren Kennedy continue; 286952244c09SJohn Wren Kennedy vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 287052244c09SJohn Wren Kennedy print_list_stats(zhp, vname, child[c], cb, depth + 2); 287152244c09SJohn Wren Kennedy free(vname); 287252244c09SJohn Wren Kennedy } 287352244c09SJohn Wren Kennedy } 287452244c09SJohn Wren Kennedy 287552244c09SJohn Wren Kennedy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 287652244c09SJohn Wren Kennedy &child, &children) == 0 && children > 0) { 287752244c09SJohn Wren Kennedy /* LINTED E_SEC_PRINTF_VAR_FMT */ 287852244c09SJohn Wren Kennedy (void) printf(dashes, cb->cb_namewidth, "cache"); 287952244c09SJohn Wren Kennedy for (c = 0; c < children; c++) { 288052244c09SJohn Wren Kennedy vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 288152244c09SJohn Wren Kennedy print_list_stats(zhp, vname, child[c], cb, depth + 2); 288252244c09SJohn Wren Kennedy free(vname); 288352244c09SJohn Wren Kennedy } 288452244c09SJohn Wren Kennedy } 288552244c09SJohn Wren Kennedy 288652244c09SJohn Wren Kennedy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child, 288752244c09SJohn Wren Kennedy &children) == 0 && children > 0) { 288852244c09SJohn Wren Kennedy /* LINTED E_SEC_PRINTF_VAR_FMT */ 288952244c09SJohn Wren Kennedy (void) printf(dashes, cb->cb_namewidth, "spare"); 289052244c09SJohn Wren Kennedy for (c = 0; c < children; c++) { 289152244c09SJohn Wren Kennedy vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 28924263d13fSGeorge Wilson print_list_stats(zhp, vname, child[c], cb, depth + 2); 28934263d13fSGeorge Wilson free(vname); 28944263d13fSGeorge Wilson } 28954263d13fSGeorge Wilson } 28964263d13fSGeorge Wilson } 28974263d13fSGeorge Wilson 28984263d13fSGeorge Wilson 2899990b4856Slling /* 2900990b4856Slling * Generic callback function to list a pool. 2901990b4856Slling */ 2902fa9e4066Sahrens int 2903fa9e4066Sahrens list_callback(zpool_handle_t *zhp, void *data) 2904fa9e4066Sahrens { 2905fa9e4066Sahrens list_cbdata_t *cbp = data; 29064263d13fSGeorge Wilson nvlist_t *config; 29074263d13fSGeorge Wilson nvlist_t *nvroot; 2908fa9e4066Sahrens 29094263d13fSGeorge Wilson config = zpool_get_config(zhp, NULL); 2910fa9e4066Sahrens 29114263d13fSGeorge Wilson print_pool(zhp, cbp); 29124263d13fSGeorge Wilson if (!cbp->cb_verbose) 29134263d13fSGeorge Wilson return (0); 29144263d13fSGeorge Wilson 29154263d13fSGeorge Wilson verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 29164263d13fSGeorge Wilson &nvroot) == 0); 29174263d13fSGeorge Wilson print_list_stats(zhp, NULL, nvroot, cbp, 0); 2918fa9e4066Sahrens 2919fa9e4066Sahrens return (0); 2920fa9e4066Sahrens } 2921fa9e4066Sahrens 2922fa9e4066Sahrens /* 2923c58b3526SAdam Stevko * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] 2924fa9e4066Sahrens * 2925990b4856Slling * -H Scripted mode. Don't display headers, and separate properties 2926990b4856Slling * by a single tab. 2927990b4856Slling * -o List of properties to display. Defaults to 29287a09f97bSGeorge Wilson * "name,size,allocated,free,expandsize,fragmentation,capacity," 29297a09f97bSGeorge Wilson * "dedupratio,health,altroot" 2930c58b3526SAdam Stevko * -p Diplay values in parsable (exact) format. 29313f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format 2932fa9e4066Sahrens * 2933fa9e4066Sahrens * List all pools in the system, whether or not they're healthy. Output space 2934fa9e4066Sahrens * statistics for each one, as well as health status summary. 2935fa9e4066Sahrens */ 2936fa9e4066Sahrens int 2937fa9e4066Sahrens zpool_do_list(int argc, char **argv) 2938fa9e4066Sahrens { 2939fa9e4066Sahrens int c; 2940fa9e4066Sahrens int ret; 2941fa9e4066Sahrens list_cbdata_t cb = { 0 }; 2942990b4856Slling static char default_props[] = 29437a09f97bSGeorge Wilson "name,size,allocated,free,expandsize,fragmentation,capacity," 29442e4c9986SGeorge Wilson "dedupratio,health,altroot"; 2945990b4856Slling char *props = default_props; 29463f9d6ad7SLin Ling unsigned long interval = 0, count = 0; 29474263d13fSGeorge Wilson zpool_list_t *list; 29484263d13fSGeorge Wilson boolean_t first = B_TRUE; 2949fa9e4066Sahrens 2950fa9e4066Sahrens /* check options */ 2951c58b3526SAdam Stevko while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) { 2952fa9e4066Sahrens switch (c) { 2953fa9e4066Sahrens case 'H': 295499653d4eSeschrock cb.cb_scripted = B_TRUE; 2955fa9e4066Sahrens break; 2956fa9e4066Sahrens case 'o': 2957990b4856Slling props = optarg; 2958fa9e4066Sahrens break; 2959c58b3526SAdam Stevko case 'p': 2960c58b3526SAdam Stevko cb.cb_literal = B_TRUE; 2961c58b3526SAdam Stevko break; 29623f9d6ad7SLin Ling case 'T': 29633f9d6ad7SLin Ling get_timestamp_arg(*optarg); 29643f9d6ad7SLin Ling break; 29654263d13fSGeorge Wilson case 'v': 29664263d13fSGeorge Wilson cb.cb_verbose = B_TRUE; 29674263d13fSGeorge Wilson break; 2968fa9e4066Sahrens case ':': 2969fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for " 2970fa9e4066Sahrens "'%c' option\n"), optopt); 297199653d4eSeschrock usage(B_FALSE); 2972fa9e4066Sahrens break; 2973fa9e4066Sahrens case '?': 2974fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2975fa9e4066Sahrens optopt); 297699653d4eSeschrock usage(B_FALSE); 2977fa9e4066Sahrens } 2978fa9e4066Sahrens } 2979fa9e4066Sahrens 2980fa9e4066Sahrens argc -= optind; 2981fa9e4066Sahrens argv += optind; 2982fa9e4066Sahrens 29833f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count); 29843f9d6ad7SLin Ling 2985990b4856Slling if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) 298699653d4eSeschrock usage(B_FALSE); 2987fa9e4066Sahrens 29884263d13fSGeorge Wilson for (;;) { 2989cd67d23dSGeorge Wilson if ((list = pool_list_get(argc, argv, &cb.cb_proplist, 2990cd67d23dSGeorge Wilson &ret)) == NULL) 2991cd67d23dSGeorge Wilson return (1); 29924263d13fSGeorge Wilson 29934263d13fSGeorge Wilson if (pool_list_count(list) == 0) 29944263d13fSGeorge Wilson break; 29954263d13fSGeorge Wilson 29964263d13fSGeorge Wilson cb.cb_namewidth = 0; 29974263d13fSGeorge Wilson (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); 29984263d13fSGeorge Wilson 29994263d13fSGeorge Wilson if (timestamp_fmt != NODATE) 30004263d13fSGeorge Wilson print_timestamp(timestamp_fmt); 30014263d13fSGeorge Wilson 30024263d13fSGeorge Wilson if (!cb.cb_scripted && (first || cb.cb_verbose)) { 30034263d13fSGeorge Wilson print_header(&cb); 30044263d13fSGeorge Wilson first = B_FALSE; 30054263d13fSGeorge Wilson } 30064263d13fSGeorge Wilson ret = pool_list_iter(list, B_TRUE, list_callback, &cb); 30074263d13fSGeorge Wilson 30083f9d6ad7SLin Ling if (interval == 0) 30093f9d6ad7SLin Ling break; 30103f9d6ad7SLin Ling 30113f9d6ad7SLin Ling if (count != 0 && --count == 0) 30123f9d6ad7SLin Ling break; 30133f9d6ad7SLin Ling 3014cd67d23dSGeorge Wilson pool_list_free(list); 30153f9d6ad7SLin Ling (void) sleep(interval); 30163f9d6ad7SLin Ling } 30173f9d6ad7SLin Ling 3018cd67d23dSGeorge Wilson if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { 3019cd67d23dSGeorge Wilson (void) printf(gettext("no pools available\n")); 3020cd67d23dSGeorge Wilson ret = 0; 3021cd67d23dSGeorge Wilson } 3022cd67d23dSGeorge Wilson 3023cd67d23dSGeorge Wilson pool_list_free(list); 30243f9d6ad7SLin Ling zprop_free_list(cb.cb_proplist); 3025fa9e4066Sahrens return (ret); 3026fa9e4066Sahrens } 3027fa9e4066Sahrens 3028fa9e4066Sahrens static nvlist_t * 3029fa9e4066Sahrens zpool_get_vdev_by_name(nvlist_t *nv, char *name) 3030fa9e4066Sahrens { 3031fa9e4066Sahrens nvlist_t **child; 3032fa9e4066Sahrens uint_t c, children; 3033fa9e4066Sahrens nvlist_t *match; 3034fa9e4066Sahrens char *path; 3035fa9e4066Sahrens 3036fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 3037fa9e4066Sahrens &child, &children) != 0) { 3038fa9e4066Sahrens verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 3039fa9e4066Sahrens if (strncmp(name, "/dev/dsk/", 9) == 0) 3040fa9e4066Sahrens name += 9; 3041fa9e4066Sahrens if (strncmp(path, "/dev/dsk/", 9) == 0) 3042fa9e4066Sahrens path += 9; 3043fa9e4066Sahrens if (strcmp(name, path) == 0) 3044fa9e4066Sahrens return (nv); 3045fa9e4066Sahrens return (NULL); 3046fa9e4066Sahrens } 3047fa9e4066Sahrens 3048fa9e4066Sahrens for (c = 0; c < children; c++) 3049fa9e4066Sahrens if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL) 3050fa9e4066Sahrens return (match); 3051fa9e4066Sahrens 3052fa9e4066Sahrens return (NULL); 3053fa9e4066Sahrens } 3054fa9e4066Sahrens 3055fa9e4066Sahrens static int 3056fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing) 3057fa9e4066Sahrens { 305899653d4eSeschrock boolean_t force = B_FALSE; 3059fa9e4066Sahrens int c; 3060fa9e4066Sahrens nvlist_t *nvroot; 3061fa9e4066Sahrens char *poolname, *old_disk, *new_disk; 3062fa9e4066Sahrens zpool_handle_t *zhp; 306399653d4eSeschrock int ret; 3064fa9e4066Sahrens 3065fa9e4066Sahrens /* check options */ 3066fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) { 3067fa9e4066Sahrens switch (c) { 3068fa9e4066Sahrens case 'f': 306999653d4eSeschrock force = B_TRUE; 3070fa9e4066Sahrens break; 3071fa9e4066Sahrens case '?': 3072fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3073fa9e4066Sahrens optopt); 307499653d4eSeschrock usage(B_FALSE); 3075fa9e4066Sahrens } 3076fa9e4066Sahrens } 3077fa9e4066Sahrens 3078fa9e4066Sahrens argc -= optind; 3079fa9e4066Sahrens argv += optind; 3080fa9e4066Sahrens 3081fa9e4066Sahrens /* get pool name and check number of arguments */ 3082fa9e4066Sahrens if (argc < 1) { 3083fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 308499653d4eSeschrock usage(B_FALSE); 3085fa9e4066Sahrens } 3086fa9e4066Sahrens 3087fa9e4066Sahrens poolname = argv[0]; 3088fa9e4066Sahrens 3089fa9e4066Sahrens if (argc < 2) { 3090fa9e4066Sahrens (void) fprintf(stderr, 3091fa9e4066Sahrens gettext("missing <device> specification\n")); 309299653d4eSeschrock usage(B_FALSE); 3093fa9e4066Sahrens } 3094fa9e4066Sahrens 3095fa9e4066Sahrens old_disk = argv[1]; 3096fa9e4066Sahrens 3097fa9e4066Sahrens if (argc < 3) { 3098fa9e4066Sahrens if (!replacing) { 3099fa9e4066Sahrens (void) fprintf(stderr, 3100fa9e4066Sahrens gettext("missing <new_device> specification\n")); 310199653d4eSeschrock usage(B_FALSE); 3102fa9e4066Sahrens } 3103fa9e4066Sahrens new_disk = old_disk; 3104fa9e4066Sahrens argc -= 1; 3105fa9e4066Sahrens argv += 1; 3106fa9e4066Sahrens } else { 3107fa9e4066Sahrens new_disk = argv[2]; 3108fa9e4066Sahrens argc -= 2; 3109fa9e4066Sahrens argv += 2; 3110fa9e4066Sahrens } 3111fa9e4066Sahrens 3112fa9e4066Sahrens if (argc > 1) { 3113fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 311499653d4eSeschrock usage(B_FALSE); 3115fa9e4066Sahrens } 3116fa9e4066Sahrens 311799653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3118fa9e4066Sahrens return (1); 3119fa9e4066Sahrens 31208488aeb5Staylor if (zpool_get_config(zhp, NULL) == NULL) { 3121fa9e4066Sahrens (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 3122fa9e4066Sahrens poolname); 3123fa9e4066Sahrens zpool_close(zhp); 3124fa9e4066Sahrens return (1); 3125fa9e4066Sahrens } 3126fa9e4066Sahrens 3127705040edSEric Taylor nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE, 3128705040edSEric Taylor argc, argv); 3129fa9e4066Sahrens if (nvroot == NULL) { 3130fa9e4066Sahrens zpool_close(zhp); 3131fa9e4066Sahrens return (1); 3132fa9e4066Sahrens } 3133fa9e4066Sahrens 313499653d4eSeschrock ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing); 313599653d4eSeschrock 313699653d4eSeschrock nvlist_free(nvroot); 313799653d4eSeschrock zpool_close(zhp); 313899653d4eSeschrock 313999653d4eSeschrock return (ret); 3140fa9e4066Sahrens } 3141fa9e4066Sahrens 3142fa9e4066Sahrens /* 3143fa9e4066Sahrens * zpool replace [-f] <pool> <device> <new_device> 3144fa9e4066Sahrens * 3145fa9e4066Sahrens * -f Force attach, even if <new_device> appears to be in use. 3146fa9e4066Sahrens * 3147fa9e4066Sahrens * Replace <device> with <new_device>. 3148fa9e4066Sahrens */ 3149fa9e4066Sahrens /* ARGSUSED */ 3150fa9e4066Sahrens int 3151fa9e4066Sahrens zpool_do_replace(int argc, char **argv) 3152fa9e4066Sahrens { 3153fa9e4066Sahrens return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); 3154fa9e4066Sahrens } 3155fa9e4066Sahrens 3156fa9e4066Sahrens /* 3157fa9e4066Sahrens * zpool attach [-f] <pool> <device> <new_device> 3158fa9e4066Sahrens * 3159fa9e4066Sahrens * -f Force attach, even if <new_device> appears to be in use. 3160fa9e4066Sahrens * 3161fa9e4066Sahrens * Attach <new_device> to the mirror containing <device>. If <device> is not 3162fa9e4066Sahrens * part of a mirror, then <device> will be transformed into a mirror of 3163fa9e4066Sahrens * <device> and <new_device>. In either case, <new_device> will begin life 3164fa9e4066Sahrens * with a DTL of [0, now], and will immediately begin to resilver itself. 3165fa9e4066Sahrens */ 3166fa9e4066Sahrens int 3167fa9e4066Sahrens zpool_do_attach(int argc, char **argv) 3168fa9e4066Sahrens { 3169fa9e4066Sahrens return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); 3170fa9e4066Sahrens } 3171fa9e4066Sahrens 3172fa9e4066Sahrens /* 3173fa9e4066Sahrens * zpool detach [-f] <pool> <device> 3174fa9e4066Sahrens * 3175fa9e4066Sahrens * -f Force detach of <device>, even if DTLs argue against it 3176fa9e4066Sahrens * (not supported yet) 3177fa9e4066Sahrens * 3178fa9e4066Sahrens * Detach a device from a mirror. The operation will be refused if <device> 3179fa9e4066Sahrens * is the last device in the mirror, or if the DTLs indicate that this device 3180fa9e4066Sahrens * has the only valid copy of some data. 3181fa9e4066Sahrens */ 3182fa9e4066Sahrens /* ARGSUSED */ 3183fa9e4066Sahrens int 3184fa9e4066Sahrens zpool_do_detach(int argc, char **argv) 3185fa9e4066Sahrens { 3186fa9e4066Sahrens int c; 3187fa9e4066Sahrens char *poolname, *path; 3188fa9e4066Sahrens zpool_handle_t *zhp; 318999653d4eSeschrock int ret; 3190fa9e4066Sahrens 3191fa9e4066Sahrens /* check options */ 3192fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) { 3193fa9e4066Sahrens switch (c) { 3194fa9e4066Sahrens case 'f': 3195fa9e4066Sahrens case '?': 3196fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3197fa9e4066Sahrens optopt); 319899653d4eSeschrock usage(B_FALSE); 3199fa9e4066Sahrens } 3200fa9e4066Sahrens } 3201fa9e4066Sahrens 3202fa9e4066Sahrens argc -= optind; 3203fa9e4066Sahrens argv += optind; 3204fa9e4066Sahrens 3205fa9e4066Sahrens /* get pool name and check number of arguments */ 3206fa9e4066Sahrens if (argc < 1) { 3207fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 320899653d4eSeschrock usage(B_FALSE); 3209fa9e4066Sahrens } 3210fa9e4066Sahrens 3211fa9e4066Sahrens if (argc < 2) { 3212fa9e4066Sahrens (void) fprintf(stderr, 3213fa9e4066Sahrens gettext("missing <device> specification\n")); 321499653d4eSeschrock usage(B_FALSE); 3215fa9e4066Sahrens } 3216fa9e4066Sahrens 3217fa9e4066Sahrens poolname = argv[0]; 3218fa9e4066Sahrens path = argv[1]; 3219fa9e4066Sahrens 322099653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3221fa9e4066Sahrens return (1); 3222fa9e4066Sahrens 322399653d4eSeschrock ret = zpool_vdev_detach(zhp, path); 322499653d4eSeschrock 322599653d4eSeschrock zpool_close(zhp); 322699653d4eSeschrock 322799653d4eSeschrock return (ret); 3228fa9e4066Sahrens } 3229fa9e4066Sahrens 3230fa9e4066Sahrens /* 32311195e687SMark J Musante * zpool split [-n] [-o prop=val] ... 32321195e687SMark J Musante * [-o mntopt] ... 32331195e687SMark J Musante * [-R altroot] <pool> <newpool> [<device> ...] 32341195e687SMark J Musante * 32351195e687SMark J Musante * -n Do not split the pool, but display the resulting layout if 32361195e687SMark J Musante * it were to be split. 32371195e687SMark J Musante * -o Set property=value, or set mount options. 32381195e687SMark J Musante * -R Mount the split-off pool under an alternate root. 32391195e687SMark J Musante * 32401195e687SMark J Musante * Splits the named pool and gives it the new pool name. Devices to be split 32411195e687SMark J Musante * off may be listed, provided that no more than one device is specified 32421195e687SMark J Musante * per top-level vdev mirror. The newly split pool is left in an exported 32431195e687SMark J Musante * state unless -R is specified. 32441195e687SMark J Musante * 32451195e687SMark J Musante * Restrictions: the top-level of the pool pool must only be made up of 32461195e687SMark J Musante * mirrors; all devices in the pool must be healthy; no device may be 32471195e687SMark J Musante * undergoing a resilvering operation. 32481195e687SMark J Musante */ 32491195e687SMark J Musante int 32501195e687SMark J Musante zpool_do_split(int argc, char **argv) 32511195e687SMark J Musante { 32521195e687SMark J Musante char *srcpool, *newpool, *propval; 32531195e687SMark J Musante char *mntopts = NULL; 32541195e687SMark J Musante splitflags_t flags; 32551195e687SMark J Musante int c, ret = 0; 32561195e687SMark J Musante zpool_handle_t *zhp; 32571195e687SMark J Musante nvlist_t *config, *props = NULL; 32581195e687SMark J Musante 32591195e687SMark J Musante flags.dryrun = B_FALSE; 32601195e687SMark J Musante flags.import = B_FALSE; 32611195e687SMark J Musante 32621195e687SMark J Musante /* check options */ 32631195e687SMark J Musante while ((c = getopt(argc, argv, ":R:no:")) != -1) { 32641195e687SMark J Musante switch (c) { 32651195e687SMark J Musante case 'R': 32661195e687SMark J Musante flags.import = B_TRUE; 32671195e687SMark J Musante if (add_prop_list( 32681195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, 32691195e687SMark J Musante &props, B_TRUE) != 0) { 32701195e687SMark J Musante if (props) 32711195e687SMark J Musante nvlist_free(props); 32721195e687SMark J Musante usage(B_FALSE); 32731195e687SMark J Musante } 32741195e687SMark J Musante break; 32751195e687SMark J Musante case 'n': 32761195e687SMark J Musante flags.dryrun = B_TRUE; 32771195e687SMark J Musante break; 32781195e687SMark J Musante case 'o': 32791195e687SMark J Musante if ((propval = strchr(optarg, '=')) != NULL) { 32801195e687SMark J Musante *propval = '\0'; 32811195e687SMark J Musante propval++; 32821195e687SMark J Musante if (add_prop_list(optarg, propval, 32831195e687SMark J Musante &props, B_TRUE) != 0) { 32841195e687SMark J Musante if (props) 32851195e687SMark J Musante nvlist_free(props); 32861195e687SMark J Musante usage(B_FALSE); 32871195e687SMark J Musante } 32881195e687SMark J Musante } else { 32891195e687SMark J Musante mntopts = optarg; 32901195e687SMark J Musante } 32911195e687SMark J Musante break; 32921195e687SMark J Musante case ':': 32931195e687SMark J Musante (void) fprintf(stderr, gettext("missing argument for " 32941195e687SMark J Musante "'%c' option\n"), optopt); 32951195e687SMark J Musante usage(B_FALSE); 32961195e687SMark J Musante break; 32971195e687SMark J Musante case '?': 32981195e687SMark J Musante (void) fprintf(stderr, gettext("invalid option '%c'\n"), 32991195e687SMark J Musante optopt); 33001195e687SMark J Musante usage(B_FALSE); 33011195e687SMark J Musante break; 33021195e687SMark J Musante } 33031195e687SMark J Musante } 33041195e687SMark J Musante 33051195e687SMark J Musante if (!flags.import && mntopts != NULL) { 33061195e687SMark J Musante (void) fprintf(stderr, gettext("setting mntopts is only " 33071195e687SMark J Musante "valid when importing the pool\n")); 33081195e687SMark J Musante usage(B_FALSE); 33091195e687SMark J Musante } 33101195e687SMark J Musante 33111195e687SMark J Musante argc -= optind; 33121195e687SMark J Musante argv += optind; 33131195e687SMark J Musante 33141195e687SMark J Musante if (argc < 1) { 33151195e687SMark J Musante (void) fprintf(stderr, gettext("Missing pool name\n")); 33161195e687SMark J Musante usage(B_FALSE); 33171195e687SMark J Musante } 33181195e687SMark J Musante if (argc < 2) { 33191195e687SMark J Musante (void) fprintf(stderr, gettext("Missing new pool name\n")); 33201195e687SMark J Musante usage(B_FALSE); 33211195e687SMark J Musante } 33221195e687SMark J Musante 33231195e687SMark J Musante srcpool = argv[0]; 33241195e687SMark J Musante newpool = argv[1]; 33251195e687SMark J Musante 33261195e687SMark J Musante argc -= 2; 33271195e687SMark J Musante argv += 2; 33281195e687SMark J Musante 33291195e687SMark J Musante if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) 33301195e687SMark J Musante return (1); 33311195e687SMark J Musante 33321195e687SMark J Musante config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 33331195e687SMark J Musante if (config == NULL) { 33341195e687SMark J Musante ret = 1; 33351195e687SMark J Musante } else { 33361195e687SMark J Musante if (flags.dryrun) { 33371195e687SMark J Musante (void) printf(gettext("would create '%s' with the " 33381195e687SMark J Musante "following layout:\n\n"), newpool); 33391195e687SMark J Musante print_vdev_tree(NULL, newpool, config, 0, B_FALSE); 33401195e687SMark J Musante } 33411195e687SMark J Musante nvlist_free(config); 33421195e687SMark J Musante } 33431195e687SMark J Musante 33441195e687SMark J Musante zpool_close(zhp); 33451195e687SMark J Musante 33461195e687SMark J Musante if (ret != 0 || flags.dryrun || !flags.import) 33471195e687SMark J Musante return (ret); 33481195e687SMark J Musante 33491195e687SMark J Musante /* 33501195e687SMark J Musante * The split was successful. Now we need to open the new 33511195e687SMark J Musante * pool and import it. 33521195e687SMark J Musante */ 33531195e687SMark J Musante if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) 33541195e687SMark J Musante return (1); 33551195e687SMark J Musante if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 33561195e687SMark J Musante zpool_enable_datasets(zhp, mntopts, 0) != 0) { 33571195e687SMark J Musante ret = 1; 3358fe7cd8aaSCyril Plisko (void) fprintf(stderr, gettext("Split was successful, but " 33591195e687SMark J Musante "the datasets could not all be mounted\n")); 33601195e687SMark J Musante (void) fprintf(stderr, gettext("Try doing '%s' with a " 33611195e687SMark J Musante "different altroot\n"), "zpool import"); 33621195e687SMark J Musante } 33631195e687SMark J Musante zpool_close(zhp); 33641195e687SMark J Musante 33651195e687SMark J Musante return (ret); 33661195e687SMark J Musante } 33671195e687SMark J Musante 33681195e687SMark J Musante 33691195e687SMark J Musante 33701195e687SMark J Musante /* 3371441d80aaSlling * zpool online <pool> <device> ... 3372fa9e4066Sahrens */ 3373fa9e4066Sahrens int 3374fa9e4066Sahrens zpool_do_online(int argc, char **argv) 3375fa9e4066Sahrens { 3376fa9e4066Sahrens int c, i; 3377fa9e4066Sahrens char *poolname; 3378fa9e4066Sahrens zpool_handle_t *zhp; 3379fa9e4066Sahrens int ret = 0; 33803d7072f8Seschrock vdev_state_t newstate; 3381573ca77eSGeorge Wilson int flags = 0; 3382fa9e4066Sahrens 3383fa9e4066Sahrens /* check options */ 3384573ca77eSGeorge Wilson while ((c = getopt(argc, argv, "et")) != -1) { 3385fa9e4066Sahrens switch (c) { 3386573ca77eSGeorge Wilson case 'e': 3387573ca77eSGeorge Wilson flags |= ZFS_ONLINE_EXPAND; 3388573ca77eSGeorge Wilson break; 3389fa9e4066Sahrens case 't': 3390fa9e4066Sahrens case '?': 3391fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3392fa9e4066Sahrens optopt); 339399653d4eSeschrock usage(B_FALSE); 3394fa9e4066Sahrens } 3395fa9e4066Sahrens } 3396fa9e4066Sahrens 3397fa9e4066Sahrens argc -= optind; 3398fa9e4066Sahrens argv += optind; 3399fa9e4066Sahrens 3400fa9e4066Sahrens /* get pool name and check number of arguments */ 3401fa9e4066Sahrens if (argc < 1) { 3402fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name\n")); 340399653d4eSeschrock usage(B_FALSE); 3404fa9e4066Sahrens } 3405fa9e4066Sahrens if (argc < 2) { 3406fa9e4066Sahrens (void) fprintf(stderr, gettext("missing device name\n")); 340799653d4eSeschrock usage(B_FALSE); 3408fa9e4066Sahrens } 3409fa9e4066Sahrens 3410fa9e4066Sahrens poolname = argv[0]; 3411fa9e4066Sahrens 341299653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3413fa9e4066Sahrens return (1); 3414fa9e4066Sahrens 34153d7072f8Seschrock for (i = 1; i < argc; i++) { 3416573ca77eSGeorge Wilson if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { 34173d7072f8Seschrock if (newstate != VDEV_STATE_HEALTHY) { 34183d7072f8Seschrock (void) printf(gettext("warning: device '%s' " 34193d7072f8Seschrock "onlined, but remains in faulted state\n"), 3420fa9e4066Sahrens argv[i]); 34213d7072f8Seschrock if (newstate == VDEV_STATE_FAULTED) 34223d7072f8Seschrock (void) printf(gettext("use 'zpool " 34233d7072f8Seschrock "clear' to restore a faulted " 34243d7072f8Seschrock "device\n")); 3425fa9e4066Sahrens else 34263d7072f8Seschrock (void) printf(gettext("use 'zpool " 34273d7072f8Seschrock "replace' to replace devices " 34283d7072f8Seschrock "that are no longer present\n")); 34293d7072f8Seschrock } 34303d7072f8Seschrock } else { 3431fa9e4066Sahrens ret = 1; 34323d7072f8Seschrock } 34333d7072f8Seschrock } 3434fa9e4066Sahrens 343599653d4eSeschrock zpool_close(zhp); 343699653d4eSeschrock 3437fa9e4066Sahrens return (ret); 3438fa9e4066Sahrens } 3439fa9e4066Sahrens 3440fa9e4066Sahrens /* 3441441d80aaSlling * zpool offline [-ft] <pool> <device> ... 3442fa9e4066Sahrens * 3443fa9e4066Sahrens * -f Force the device into the offline state, even if doing 3444fa9e4066Sahrens * so would appear to compromise pool availability. 3445fa9e4066Sahrens * (not supported yet) 3446fa9e4066Sahrens * 3447fa9e4066Sahrens * -t Only take the device off-line temporarily. The offline 3448fa9e4066Sahrens * state will not be persistent across reboots. 3449fa9e4066Sahrens */ 3450fa9e4066Sahrens /* ARGSUSED */ 3451fa9e4066Sahrens int 3452fa9e4066Sahrens zpool_do_offline(int argc, char **argv) 3453fa9e4066Sahrens { 3454fa9e4066Sahrens int c, i; 3455fa9e4066Sahrens char *poolname; 3456fa9e4066Sahrens zpool_handle_t *zhp; 345799653d4eSeschrock int ret = 0; 345899653d4eSeschrock boolean_t istmp = B_FALSE; 3459fa9e4066Sahrens 3460fa9e4066Sahrens /* check options */ 3461fa9e4066Sahrens while ((c = getopt(argc, argv, "ft")) != -1) { 3462fa9e4066Sahrens switch (c) { 3463fa9e4066Sahrens case 't': 346499653d4eSeschrock istmp = B_TRUE; 3465441d80aaSlling break; 3466441d80aaSlling case 'f': 3467fa9e4066Sahrens case '?': 3468fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3469fa9e4066Sahrens optopt); 347099653d4eSeschrock usage(B_FALSE); 3471fa9e4066Sahrens } 3472fa9e4066Sahrens } 3473fa9e4066Sahrens 3474fa9e4066Sahrens argc -= optind; 3475fa9e4066Sahrens argv += optind; 3476fa9e4066Sahrens 3477fa9e4066Sahrens /* get pool name and check number of arguments */ 3478fa9e4066Sahrens if (argc < 1) { 3479fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name\n")); 348099653d4eSeschrock usage(B_FALSE); 3481fa9e4066Sahrens } 3482fa9e4066Sahrens if (argc < 2) { 3483fa9e4066Sahrens (void) fprintf(stderr, gettext("missing device name\n")); 348499653d4eSeschrock usage(B_FALSE); 3485fa9e4066Sahrens } 3486fa9e4066Sahrens 3487fa9e4066Sahrens poolname = argv[0]; 3488fa9e4066Sahrens 348999653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3490fa9e4066Sahrens return (1); 3491fa9e4066Sahrens 34923d7072f8Seschrock for (i = 1; i < argc; i++) { 34933d7072f8Seschrock if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 3494fa9e4066Sahrens ret = 1; 34953d7072f8Seschrock } 3496fa9e4066Sahrens 349799653d4eSeschrock zpool_close(zhp); 349899653d4eSeschrock 3499fa9e4066Sahrens return (ret); 3500fa9e4066Sahrens } 3501fa9e4066Sahrens 3502ea8dc4b6Seschrock /* 3503ea8dc4b6Seschrock * zpool clear <pool> [device] 3504ea8dc4b6Seschrock * 3505ea8dc4b6Seschrock * Clear all errors associated with a pool or a particular device. 3506ea8dc4b6Seschrock */ 3507ea8dc4b6Seschrock int 3508ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv) 3509ea8dc4b6Seschrock { 3510468c413aSTim Haley int c; 3511ea8dc4b6Seschrock int ret = 0; 3512468c413aSTim Haley boolean_t dryrun = B_FALSE; 3513468c413aSTim Haley boolean_t do_rewind = B_FALSE; 3514468c413aSTim Haley boolean_t xtreme_rewind = B_FALSE; 3515468c413aSTim Haley uint32_t rewind_policy = ZPOOL_NO_REWIND; 3516468c413aSTim Haley nvlist_t *policy = NULL; 3517ea8dc4b6Seschrock zpool_handle_t *zhp; 3518ea8dc4b6Seschrock char *pool, *device; 3519ea8dc4b6Seschrock 3520468c413aSTim Haley /* check options */ 3521468c413aSTim Haley while ((c = getopt(argc, argv, "FnX")) != -1) { 3522468c413aSTim Haley switch (c) { 3523468c413aSTim Haley case 'F': 3524468c413aSTim Haley do_rewind = B_TRUE; 3525468c413aSTim Haley break; 3526468c413aSTim Haley case 'n': 3527468c413aSTim Haley dryrun = B_TRUE; 3528468c413aSTim Haley break; 3529468c413aSTim Haley case 'X': 3530468c413aSTim Haley xtreme_rewind = B_TRUE; 3531468c413aSTim Haley break; 3532468c413aSTim Haley case '?': 3533468c413aSTim Haley (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3534468c413aSTim Haley optopt); 3535468c413aSTim Haley usage(B_FALSE); 3536468c413aSTim Haley } 3537468c413aSTim Haley } 3538468c413aSTim Haley 3539468c413aSTim Haley argc -= optind; 3540468c413aSTim Haley argv += optind; 3541468c413aSTim Haley 3542468c413aSTim Haley if (argc < 1) { 3543ea8dc4b6Seschrock (void) fprintf(stderr, gettext("missing pool name\n")); 354499653d4eSeschrock usage(B_FALSE); 3545ea8dc4b6Seschrock } 3546ea8dc4b6Seschrock 3547468c413aSTim Haley if (argc > 2) { 3548ea8dc4b6Seschrock (void) fprintf(stderr, gettext("too many arguments\n")); 354999653d4eSeschrock usage(B_FALSE); 3550ea8dc4b6Seschrock } 3551ea8dc4b6Seschrock 3552468c413aSTim Haley if ((dryrun || xtreme_rewind) && !do_rewind) { 3553468c413aSTim Haley (void) fprintf(stderr, 3554468c413aSTim Haley gettext("-n or -X only meaningful with -F\n")); 3555468c413aSTim Haley usage(B_FALSE); 3556468c413aSTim Haley } 3557468c413aSTim Haley if (dryrun) 3558468c413aSTim Haley rewind_policy = ZPOOL_TRY_REWIND; 3559468c413aSTim Haley else if (do_rewind) 3560468c413aSTim Haley rewind_policy = ZPOOL_DO_REWIND; 3561468c413aSTim Haley if (xtreme_rewind) 3562468c413aSTim Haley rewind_policy |= ZPOOL_EXTREME_REWIND; 3563ea8dc4b6Seschrock 3564468c413aSTim Haley /* In future, further rewind policy choices can be passed along here */ 3565468c413aSTim Haley if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 3566468c413aSTim Haley nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 3567ea8dc4b6Seschrock return (1); 3568ea8dc4b6Seschrock 3569468c413aSTim Haley pool = argv[0]; 3570468c413aSTim Haley device = argc == 2 ? argv[1] : NULL; 3571468c413aSTim Haley 3572468c413aSTim Haley if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 3573468c413aSTim Haley nvlist_free(policy); 3574468c413aSTim Haley return (1); 3575468c413aSTim Haley } 3576468c413aSTim Haley 3577468c413aSTim Haley if (zpool_clear(zhp, device, policy) != 0) 3578ea8dc4b6Seschrock ret = 1; 3579ea8dc4b6Seschrock 3580ea8dc4b6Seschrock zpool_close(zhp); 3581ea8dc4b6Seschrock 3582468c413aSTim Haley nvlist_free(policy); 3583468c413aSTim Haley 3584ea8dc4b6Seschrock return (ret); 3585ea8dc4b6Seschrock } 3586ea8dc4b6Seschrock 3587e9103aaeSGarrett D'Amore /* 3588e9103aaeSGarrett D'Amore * zpool reguid <pool> 3589e9103aaeSGarrett D'Amore */ 3590e9103aaeSGarrett D'Amore int 3591e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv) 3592e9103aaeSGarrett D'Amore { 3593e9103aaeSGarrett D'Amore int c; 3594e9103aaeSGarrett D'Amore char *poolname; 3595e9103aaeSGarrett D'Amore zpool_handle_t *zhp; 3596e9103aaeSGarrett D'Amore int ret = 0; 3597e9103aaeSGarrett D'Amore 3598e9103aaeSGarrett D'Amore /* check options */ 3599e9103aaeSGarrett D'Amore while ((c = getopt(argc, argv, "")) != -1) { 3600e9103aaeSGarrett D'Amore switch (c) { 3601e9103aaeSGarrett D'Amore case '?': 3602e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3603e9103aaeSGarrett D'Amore optopt); 3604e9103aaeSGarrett D'Amore usage(B_FALSE); 3605e9103aaeSGarrett D'Amore } 3606e9103aaeSGarrett D'Amore } 3607e9103aaeSGarrett D'Amore 3608e9103aaeSGarrett D'Amore argc -= optind; 3609e9103aaeSGarrett D'Amore argv += optind; 3610e9103aaeSGarrett D'Amore 3611e9103aaeSGarrett D'Amore /* get pool name and check number of arguments */ 3612e9103aaeSGarrett D'Amore if (argc < 1) { 3613e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("missing pool name\n")); 3614e9103aaeSGarrett D'Amore usage(B_FALSE); 3615e9103aaeSGarrett D'Amore } 3616e9103aaeSGarrett D'Amore 3617e9103aaeSGarrett D'Amore if (argc > 1) { 3618e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("too many arguments\n")); 3619e9103aaeSGarrett D'Amore usage(B_FALSE); 3620e9103aaeSGarrett D'Amore } 3621e9103aaeSGarrett D'Amore 3622e9103aaeSGarrett D'Amore poolname = argv[0]; 3623e9103aaeSGarrett D'Amore if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3624e9103aaeSGarrett D'Amore return (1); 3625e9103aaeSGarrett D'Amore 3626e9103aaeSGarrett D'Amore ret = zpool_reguid(zhp); 3627e9103aaeSGarrett D'Amore 3628e9103aaeSGarrett D'Amore zpool_close(zhp); 3629e9103aaeSGarrett D'Amore return (ret); 3630e9103aaeSGarrett D'Amore } 3631e9103aaeSGarrett D'Amore 3632e9103aaeSGarrett D'Amore 36334263d13fSGeorge Wilson /* 36344263d13fSGeorge Wilson * zpool reopen <pool> 36354263d13fSGeorge Wilson * 36364263d13fSGeorge Wilson * Reopen the pool so that the kernel can update the sizes of all vdevs. 36374263d13fSGeorge Wilson */ 36384263d13fSGeorge Wilson int 36394263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv) 36404263d13fSGeorge Wilson { 364131d7e8faSGeorge Wilson int c; 36424263d13fSGeorge Wilson int ret = 0; 36434263d13fSGeorge Wilson zpool_handle_t *zhp; 36444263d13fSGeorge Wilson char *pool; 36454263d13fSGeorge Wilson 364631d7e8faSGeorge Wilson /* check options */ 364731d7e8faSGeorge Wilson while ((c = getopt(argc, argv, "")) != -1) { 364831d7e8faSGeorge Wilson switch (c) { 364931d7e8faSGeorge Wilson case '?': 365031d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("invalid option '%c'\n"), 365131d7e8faSGeorge Wilson optopt); 365231d7e8faSGeorge Wilson usage(B_FALSE); 365331d7e8faSGeorge Wilson } 365431d7e8faSGeorge Wilson } 365531d7e8faSGeorge Wilson 36564263d13fSGeorge Wilson argc--; 36574263d13fSGeorge Wilson argv++; 36584263d13fSGeorge Wilson 365931d7e8faSGeorge Wilson if (argc < 1) { 366031d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("missing pool name\n")); 366131d7e8faSGeorge Wilson usage(B_FALSE); 366231d7e8faSGeorge Wilson } 366331d7e8faSGeorge Wilson 366431d7e8faSGeorge Wilson if (argc > 1) { 366531d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("too many arguments\n")); 366631d7e8faSGeorge Wilson usage(B_FALSE); 366731d7e8faSGeorge Wilson } 36684263d13fSGeorge Wilson 36694263d13fSGeorge Wilson pool = argv[0]; 36704263d13fSGeorge Wilson if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) 36714263d13fSGeorge Wilson return (1); 36724263d13fSGeorge Wilson 36734263d13fSGeorge Wilson ret = zpool_reopen(zhp); 36744263d13fSGeorge Wilson zpool_close(zhp); 36754263d13fSGeorge Wilson return (ret); 36764263d13fSGeorge Wilson } 36774263d13fSGeorge Wilson 3678fa9e4066Sahrens typedef struct scrub_cbdata { 3679fa9e4066Sahrens int cb_type; 368006eeb2adSek110237 int cb_argc; 368106eeb2adSek110237 char **cb_argv; 3682fa9e4066Sahrens } scrub_cbdata_t; 3683fa9e4066Sahrens 3684fa9e4066Sahrens int 3685fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data) 3686fa9e4066Sahrens { 3687fa9e4066Sahrens scrub_cbdata_t *cb = data; 368806eeb2adSek110237 int err; 3689fa9e4066Sahrens 3690ea8dc4b6Seschrock /* 3691ea8dc4b6Seschrock * Ignore faulted pools. 3692ea8dc4b6Seschrock */ 3693ea8dc4b6Seschrock if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 3694ea8dc4b6Seschrock (void) fprintf(stderr, gettext("cannot scrub '%s': pool is " 3695ea8dc4b6Seschrock "currently unavailable\n"), zpool_get_name(zhp)); 3696ea8dc4b6Seschrock return (1); 3697ea8dc4b6Seschrock } 3698ea8dc4b6Seschrock 36993f9d6ad7SLin Ling err = zpool_scan(zhp, cb->cb_type); 370006eeb2adSek110237 370106eeb2adSek110237 return (err != 0); 3702fa9e4066Sahrens } 3703fa9e4066Sahrens 3704fa9e4066Sahrens /* 3705fa9e4066Sahrens * zpool scrub [-s] <pool> ... 3706fa9e4066Sahrens * 3707fa9e4066Sahrens * -s Stop. Stops any in-progress scrub. 3708fa9e4066Sahrens */ 3709fa9e4066Sahrens int 3710fa9e4066Sahrens zpool_do_scrub(int argc, char **argv) 3711fa9e4066Sahrens { 3712fa9e4066Sahrens int c; 3713fa9e4066Sahrens scrub_cbdata_t cb; 3714fa9e4066Sahrens 37153f9d6ad7SLin Ling cb.cb_type = POOL_SCAN_SCRUB; 3716fa9e4066Sahrens 3717fa9e4066Sahrens /* check options */ 3718fa9e4066Sahrens while ((c = getopt(argc, argv, "s")) != -1) { 3719fa9e4066Sahrens switch (c) { 3720fa9e4066Sahrens case 's': 37213f9d6ad7SLin Ling cb.cb_type = POOL_SCAN_NONE; 3722fa9e4066Sahrens break; 3723fa9e4066Sahrens case '?': 3724fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3725fa9e4066Sahrens optopt); 372699653d4eSeschrock usage(B_FALSE); 3727fa9e4066Sahrens } 3728fa9e4066Sahrens } 3729fa9e4066Sahrens 373006eeb2adSek110237 cb.cb_argc = argc; 373106eeb2adSek110237 cb.cb_argv = argv; 3732fa9e4066Sahrens argc -= optind; 3733fa9e4066Sahrens argv += optind; 3734fa9e4066Sahrens 3735fa9e4066Sahrens if (argc < 1) { 3736fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 373799653d4eSeschrock usage(B_FALSE); 3738fa9e4066Sahrens } 3739fa9e4066Sahrens 3740b1b8ab34Slling return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb)); 3741fa9e4066Sahrens } 3742fa9e4066Sahrens 3743fa9e4066Sahrens typedef struct status_cbdata { 3744fa9e4066Sahrens int cb_count; 3745e9dbad6fSeschrock boolean_t cb_allpools; 374699653d4eSeschrock boolean_t cb_verbose; 374799653d4eSeschrock boolean_t cb_explain; 374899653d4eSeschrock boolean_t cb_first; 37499eb19f4dSGeorge Wilson boolean_t cb_dedup_stats; 3750fa9e4066Sahrens } status_cbdata_t; 3751fa9e4066Sahrens 3752fa9e4066Sahrens /* 3753fa9e4066Sahrens * Print out detailed scrub status. 3754fa9e4066Sahrens */ 3755fa9e4066Sahrens void 37563f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps) 3757fa9e4066Sahrens { 37583f9d6ad7SLin Ling time_t start, end; 375973d314ceSLin Ling uint64_t elapsed, mins_left, hours_left; 37603f9d6ad7SLin Ling uint64_t pass_exam, examined, total; 37613f9d6ad7SLin Ling uint_t rate; 3762fa9e4066Sahrens double fraction_done; 37633f9d6ad7SLin Ling char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 3764fa9e4066Sahrens 37653f9d6ad7SLin Ling (void) printf(gettext(" scan: ")); 3766fa9e4066Sahrens 37673f9d6ad7SLin Ling /* If there's never been a scan, there's not much to say. */ 37683f9d6ad7SLin Ling if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 37693f9d6ad7SLin Ling ps->pss_func >= POOL_SCAN_FUNCS) { 3770fa9e4066Sahrens (void) printf(gettext("none requested\n")); 3771fa9e4066Sahrens return; 3772fa9e4066Sahrens } 3773fa9e4066Sahrens 37743f9d6ad7SLin Ling start = ps->pss_start_time; 37753f9d6ad7SLin Ling end = ps->pss_end_time; 37763f9d6ad7SLin Ling zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf)); 3777fa9e4066Sahrens 37783f9d6ad7SLin Ling assert(ps->pss_func == POOL_SCAN_SCRUB || 37793f9d6ad7SLin Ling ps->pss_func == POOL_SCAN_RESILVER); 37803f9d6ad7SLin Ling /* 37813f9d6ad7SLin Ling * Scan is finished or canceled. 37823f9d6ad7SLin Ling */ 37833f9d6ad7SLin Ling if (ps->pss_state == DSS_FINISHED) { 37843f9d6ad7SLin Ling uint64_t minutes_taken = (end - start) / 60; 37853f9d6ad7SLin Ling char *fmt; 3786fa9e4066Sahrens 37873f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) { 37883f9d6ad7SLin Ling fmt = gettext("scrub repaired %s in %lluh%um with " 37893f9d6ad7SLin Ling "%llu errors on %s"); 37903f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) { 37913f9d6ad7SLin Ling fmt = gettext("resilvered %s in %lluh%um with " 37923f9d6ad7SLin Ling "%llu errors on %s"); 37933f9d6ad7SLin Ling } 37943f9d6ad7SLin Ling /* LINTED */ 37953f9d6ad7SLin Ling (void) printf(fmt, processed_buf, 379618ce54dfSek110237 (u_longlong_t)(minutes_taken / 60), 379718ce54dfSek110237 (uint_t)(minutes_taken % 60), 37983f9d6ad7SLin Ling (u_longlong_t)ps->pss_errors, 37993f9d6ad7SLin Ling ctime((time_t *)&end)); 38003f9d6ad7SLin Ling return; 38013f9d6ad7SLin Ling } else if (ps->pss_state == DSS_CANCELED) { 38023f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) { 38033f9d6ad7SLin Ling (void) printf(gettext("scrub canceled on %s"), 38043f9d6ad7SLin Ling ctime(&end)); 38053f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) { 38063f9d6ad7SLin Ling (void) printf(gettext("resilver canceled on %s"), 38073f9d6ad7SLin Ling ctime(&end)); 38083f9d6ad7SLin Ling } 3809fa9e4066Sahrens return; 3810fa9e4066Sahrens } 3811fa9e4066Sahrens 38123f9d6ad7SLin Ling assert(ps->pss_state == DSS_SCANNING); 3813fa9e4066Sahrens 38143f9d6ad7SLin Ling /* 38153f9d6ad7SLin Ling * Scan is in progress. 38163f9d6ad7SLin Ling */ 38173f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) { 38183f9d6ad7SLin Ling (void) printf(gettext("scrub in progress since %s"), 38193f9d6ad7SLin Ling ctime(&start)); 38203f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) { 38213f9d6ad7SLin Ling (void) printf(gettext("resilver in progress since %s"), 38223f9d6ad7SLin Ling ctime(&start)); 38233f9d6ad7SLin Ling } 38243f9d6ad7SLin Ling 38253f9d6ad7SLin Ling examined = ps->pss_examined ? ps->pss_examined : 1; 38263f9d6ad7SLin Ling total = ps->pss_to_examine; 3827fa9e4066Sahrens fraction_done = (double)examined / total; 3828fa9e4066Sahrens 38293f9d6ad7SLin Ling /* elapsed time for this pass */ 38303f9d6ad7SLin Ling elapsed = time(NULL) - ps->pss_pass_start; 38313f9d6ad7SLin Ling elapsed = elapsed ? elapsed : 1; 38323f9d6ad7SLin Ling pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1; 38333f9d6ad7SLin Ling rate = pass_exam / elapsed; 38343f9d6ad7SLin Ling rate = rate ? rate : 1; 38353f9d6ad7SLin Ling mins_left = ((total - examined) / rate) / 60; 383673d314ceSLin Ling hours_left = mins_left / 60; 38373f9d6ad7SLin Ling 38383f9d6ad7SLin Ling zfs_nicenum(examined, examined_buf, sizeof (examined_buf)); 38393f9d6ad7SLin Ling zfs_nicenum(total, total_buf, sizeof (total_buf)); 38403f9d6ad7SLin Ling zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 38413f9d6ad7SLin Ling 384273d314ceSLin Ling /* 384373d314ceSLin Ling * do not print estimated time if hours_left is more than 30 days 384473d314ceSLin Ling */ 384573d314ceSLin Ling (void) printf(gettext(" %s scanned out of %s at %s/s"), 384673d314ceSLin Ling examined_buf, total_buf, rate_buf); 384773d314ceSLin Ling if (hours_left < (30 * 24)) { 384873d314ceSLin Ling (void) printf(gettext(", %lluh%um to go\n"), 384973d314ceSLin Ling (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 385073d314ceSLin Ling } else { 385173d314ceSLin Ling (void) printf(gettext( 385273d314ceSLin Ling ", (scan is slow, no estimated time)\n")); 385373d314ceSLin Ling } 38543f9d6ad7SLin Ling 38553f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_RESILVER) { 38563f9d6ad7SLin Ling (void) printf(gettext(" %s resilvered, %.2f%% done\n"), 38573f9d6ad7SLin Ling processed_buf, 100 * fraction_done); 38583f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_SCRUB) { 38593f9d6ad7SLin Ling (void) printf(gettext(" %s repaired, %.2f%% done\n"), 38603f9d6ad7SLin Ling processed_buf, 100 * fraction_done); 38613f9d6ad7SLin Ling } 3862fa9e4066Sahrens } 3863fa9e4066Sahrens 3864ea8dc4b6Seschrock static void 3865ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp) 3866ea8dc4b6Seschrock { 386775519f38Sek110237 nvlist_t *nverrlist = NULL; 386855434c77Sek110237 nvpair_t *elem; 386955434c77Sek110237 char *pathname; 387055434c77Sek110237 size_t len = MAXPATHLEN * 2; 3871ea8dc4b6Seschrock 387255434c77Sek110237 if (zpool_get_errlog(zhp, &nverrlist) != 0) { 3873ea8dc4b6Seschrock (void) printf("errors: List of errors unavailable " 3874ea8dc4b6Seschrock "(insufficient privileges)\n"); 3875ea8dc4b6Seschrock return; 3876ea8dc4b6Seschrock } 3877ea8dc4b6Seschrock 387855434c77Sek110237 (void) printf("errors: Permanent errors have been " 387955434c77Sek110237 "detected in the following files:\n\n"); 3880ea8dc4b6Seschrock 388155434c77Sek110237 pathname = safe_malloc(len); 388255434c77Sek110237 elem = NULL; 388355434c77Sek110237 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 388455434c77Sek110237 nvlist_t *nv; 388555434c77Sek110237 uint64_t dsobj, obj; 3886ea8dc4b6Seschrock 388755434c77Sek110237 verify(nvpair_value_nvlist(elem, &nv) == 0); 388855434c77Sek110237 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 388955434c77Sek110237 &dsobj) == 0); 389055434c77Sek110237 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 389155434c77Sek110237 &obj) == 0); 389255434c77Sek110237 zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 389355434c77Sek110237 (void) printf("%7s %s\n", "", pathname); 3894ea8dc4b6Seschrock } 389555434c77Sek110237 free(pathname); 389655434c77Sek110237 nvlist_free(nverrlist); 3897ea8dc4b6Seschrock } 3898ea8dc4b6Seschrock 389999653d4eSeschrock static void 390099653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares, 390199653d4eSeschrock int namewidth) 390299653d4eSeschrock { 390399653d4eSeschrock uint_t i; 390499653d4eSeschrock char *name; 390599653d4eSeschrock 390699653d4eSeschrock if (nspares == 0) 390799653d4eSeschrock return; 390899653d4eSeschrock 390999653d4eSeschrock (void) printf(gettext("\tspares\n")); 391099653d4eSeschrock 391199653d4eSeschrock for (i = 0; i < nspares; i++) { 391288ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE); 391399653d4eSeschrock print_status_config(zhp, name, spares[i], 3914aa8cf21aSNeil Perrin namewidth, 2, B_TRUE); 391599653d4eSeschrock free(name); 391699653d4eSeschrock } 391799653d4eSeschrock } 391899653d4eSeschrock 3919fa94a07fSbrendan static void 3920fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache, 3921fa94a07fSbrendan int namewidth) 3922fa94a07fSbrendan { 3923fa94a07fSbrendan uint_t i; 3924fa94a07fSbrendan char *name; 3925fa94a07fSbrendan 3926fa94a07fSbrendan if (nl2cache == 0) 3927fa94a07fSbrendan return; 3928fa94a07fSbrendan 3929fa94a07fSbrendan (void) printf(gettext("\tcache\n")); 3930fa94a07fSbrendan 3931fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 393288ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE); 3933fa94a07fSbrendan print_status_config(zhp, name, l2cache[i], 3934aa8cf21aSNeil Perrin namewidth, 2, B_FALSE); 3935aa8cf21aSNeil Perrin free(name); 3936aa8cf21aSNeil Perrin } 3937aa8cf21aSNeil Perrin } 3938aa8cf21aSNeil Perrin 39399eb19f4dSGeorge Wilson static void 39409eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config) 39419eb19f4dSGeorge Wilson { 39429eb19f4dSGeorge Wilson ddt_histogram_t *ddh; 39439eb19f4dSGeorge Wilson ddt_stat_t *dds; 39449eb19f4dSGeorge Wilson ddt_object_t *ddo; 39459eb19f4dSGeorge Wilson uint_t c; 39469eb19f4dSGeorge Wilson 39479eb19f4dSGeorge Wilson /* 39489eb19f4dSGeorge Wilson * If the pool was faulted then we may not have been able to 39492384d9f8SGeorge Wilson * obtain the config. Otherwise, if we have anything in the dedup 39509eb19f4dSGeorge Wilson * table continue processing the stats. 39519eb19f4dSGeorge Wilson */ 39529eb19f4dSGeorge Wilson if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 3953ce72e614SYuri Pankov (uint64_t **)&ddo, &c) != 0) 39549eb19f4dSGeorge Wilson return; 39559eb19f4dSGeorge Wilson 39569eb19f4dSGeorge Wilson (void) printf("\n"); 3957ce72e614SYuri Pankov (void) printf(gettext(" dedup: ")); 3958ce72e614SYuri Pankov if (ddo->ddo_count == 0) { 3959ce72e614SYuri Pankov (void) printf(gettext("no DDT entries\n")); 3960ce72e614SYuri Pankov return; 3961ce72e614SYuri Pankov } 3962ce72e614SYuri Pankov 39639eb19f4dSGeorge Wilson (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n", 39649eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_count, 39659eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_dspace, 39669eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_mspace); 39679eb19f4dSGeorge Wilson 39689eb19f4dSGeorge Wilson verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 39699eb19f4dSGeorge Wilson (uint64_t **)&dds, &c) == 0); 39709eb19f4dSGeorge Wilson verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 39719eb19f4dSGeorge Wilson (uint64_t **)&ddh, &c) == 0); 39729eb19f4dSGeorge Wilson zpool_dump_ddt(dds, ddh); 39739eb19f4dSGeorge Wilson } 39749eb19f4dSGeorge Wilson 3975aa8cf21aSNeil Perrin /* 3976fa9e4066Sahrens * Display a summary of pool status. Displays a summary such as: 3977fa9e4066Sahrens * 3978fa9e4066Sahrens * pool: tank 3979fa9e4066Sahrens * status: DEGRADED 3980fa9e4066Sahrens * reason: One or more devices ... 3981654b400cSJoshua M. Clulow * see: http://illumos.org/msg/ZFS-xxxx-01 3982fa9e4066Sahrens * config: 3983fa9e4066Sahrens * mirror DEGRADED 3984fa9e4066Sahrens * c1t0d0 OK 3985ea8dc4b6Seschrock * c2t0d0 UNAVAIL 3986fa9e4066Sahrens * 3987fa9e4066Sahrens * When given the '-v' option, we print out the complete config. If the '-e' 3988fa9e4066Sahrens * option is specified, then we print out error rate information as well. 3989fa9e4066Sahrens */ 3990fa9e4066Sahrens int 3991fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data) 3992fa9e4066Sahrens { 3993fa9e4066Sahrens status_cbdata_t *cbp = data; 3994fa9e4066Sahrens nvlist_t *config, *nvroot; 3995fa9e4066Sahrens char *msgid; 3996fa9e4066Sahrens int reason; 399746657f8dSmmusante const char *health; 399846657f8dSmmusante uint_t c; 399946657f8dSmmusante vdev_stat_t *vs; 4000fa9e4066Sahrens 4001088e9d47Seschrock config = zpool_get_config(zhp, NULL); 4002fa9e4066Sahrens reason = zpool_get_status(zhp, &msgid); 4003fa9e4066Sahrens 4004fa9e4066Sahrens cbp->cb_count++; 4005fa9e4066Sahrens 4006fa9e4066Sahrens /* 4007fa9e4066Sahrens * If we were given 'zpool status -x', only report those pools with 4008fa9e4066Sahrens * problems. 4009fa9e4066Sahrens */ 4010b3a6f804STim Connors if (cbp->cb_explain && 4011b3a6f804STim Connors (reason == ZPOOL_STATUS_OK || 4012b3a6f804STim Connors reason == ZPOOL_STATUS_VERSION_OLDER || 4013b3a6f804STim Connors reason == ZPOOL_STATUS_FEAT_DISABLED)) { 4014e9dbad6fSeschrock if (!cbp->cb_allpools) { 4015e9dbad6fSeschrock (void) printf(gettext("pool '%s' is healthy\n"), 4016e9dbad6fSeschrock zpool_get_name(zhp)); 4017e9dbad6fSeschrock if (cbp->cb_first) 4018e9dbad6fSeschrock cbp->cb_first = B_FALSE; 4019e9dbad6fSeschrock } 4020fa9e4066Sahrens return (0); 4021e9dbad6fSeschrock } 4022fa9e4066Sahrens 4023fa9e4066Sahrens if (cbp->cb_first) 402499653d4eSeschrock cbp->cb_first = B_FALSE; 4025fa9e4066Sahrens else 4026fa9e4066Sahrens (void) printf("\n"); 4027fa9e4066Sahrens 402846657f8dSmmusante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 402946657f8dSmmusante &nvroot) == 0); 40303f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 403146657f8dSmmusante (uint64_t **)&vs, &c) == 0); 4032990b4856Slling health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 4033fa9e4066Sahrens 4034fa9e4066Sahrens (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp)); 4035fa9e4066Sahrens (void) printf(gettext(" state: %s\n"), health); 4036fa9e4066Sahrens 4037fa9e4066Sahrens switch (reason) { 4038fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_R: 4039fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not " 4040fa9e4066Sahrens "be opened. Sufficient replicas exist for\n\tthe pool to " 4041fa9e4066Sahrens "continue functioning in a degraded state.\n")); 4042fa9e4066Sahrens (void) printf(gettext("action: Attach the missing device and " 4043fa9e4066Sahrens "online it using 'zpool online'.\n")); 4044fa9e4066Sahrens break; 4045fa9e4066Sahrens 4046fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_NR: 4047fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not " 4048fa9e4066Sahrens "be opened. There are insufficient\n\treplicas for the " 4049fa9e4066Sahrens "pool to continue functioning.\n")); 4050fa9e4066Sahrens (void) printf(gettext("action: Attach the missing device and " 4051fa9e4066Sahrens "online it using 'zpool online'.\n")); 4052fa9e4066Sahrens break; 4053fa9e4066Sahrens 4054fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_R: 4055fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not " 4056fa9e4066Sahrens "be used because the label is missing or\n\tinvalid. " 4057fa9e4066Sahrens "Sufficient replicas exist for the pool to continue\n\t" 4058fa9e4066Sahrens "functioning in a degraded state.\n")); 4059fa9e4066Sahrens (void) printf(gettext("action: Replace the device using " 4060fa9e4066Sahrens "'zpool replace'.\n")); 4061fa9e4066Sahrens break; 4062fa9e4066Sahrens 4063fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_NR: 4064fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not " 4065b1b8ab34Slling "be used because the label is missing \n\tor invalid. " 4066fa9e4066Sahrens "There are insufficient replicas for the pool to " 4067fa9e4066Sahrens "continue\n\tfunctioning.\n")); 4068468c413aSTim Haley zpool_explain_recover(zpool_get_handle(zhp), 4069468c413aSTim Haley zpool_get_name(zhp), reason, config); 4070fa9e4066Sahrens break; 4071fa9e4066Sahrens 4072fa9e4066Sahrens case ZPOOL_STATUS_FAILING_DEV: 4073fa9e4066Sahrens (void) printf(gettext("status: One or more devices has " 4074fa9e4066Sahrens "experienced an unrecoverable error. An\n\tattempt was " 4075fa9e4066Sahrens "made to correct the error. Applications are " 4076fa9e4066Sahrens "unaffected.\n")); 4077fa9e4066Sahrens (void) printf(gettext("action: Determine if the device needs " 4078fa9e4066Sahrens "to be replaced, and clear the errors\n\tusing " 4079ea8dc4b6Seschrock "'zpool clear' or replace the device with 'zpool " 4080fa9e4066Sahrens "replace'.\n")); 4081fa9e4066Sahrens break; 4082fa9e4066Sahrens 4083fa9e4066Sahrens case ZPOOL_STATUS_OFFLINE_DEV: 4084fa9e4066Sahrens (void) printf(gettext("status: One or more devices has " 4085d7d4af51Smmusante "been taken offline by the administrator.\n\tSufficient " 4086fa9e4066Sahrens "replicas exist for the pool to continue functioning in " 4087fa9e4066Sahrens "a\n\tdegraded state.\n")); 4088fa9e4066Sahrens (void) printf(gettext("action: Online the device using " 4089fa9e4066Sahrens "'zpool online' or replace the device with\n\t'zpool " 4090fa9e4066Sahrens "replace'.\n")); 4091fa9e4066Sahrens break; 4092fa9e4066Sahrens 4093c25309d4SGeorge Wilson case ZPOOL_STATUS_REMOVED_DEV: 4094c25309d4SGeorge Wilson (void) printf(gettext("status: One or more devices has " 4095c25309d4SGeorge Wilson "been removed by the administrator.\n\tSufficient " 4096c25309d4SGeorge Wilson "replicas exist for the pool to continue functioning in " 4097c25309d4SGeorge Wilson "a\n\tdegraded state.\n")); 4098c25309d4SGeorge Wilson (void) printf(gettext("action: Online the device using " 4099c25309d4SGeorge Wilson "'zpool online' or replace the device with\n\t'zpool " 4100c25309d4SGeorge Wilson "replace'.\n")); 4101c25309d4SGeorge Wilson break; 4102c25309d4SGeorge Wilson 4103fa9e4066Sahrens case ZPOOL_STATUS_RESILVERING: 4104fa9e4066Sahrens (void) printf(gettext("status: One or more devices is " 4105fa9e4066Sahrens "currently being resilvered. The pool will\n\tcontinue " 4106fa9e4066Sahrens "to function, possibly in a degraded state.\n")); 4107fa9e4066Sahrens (void) printf(gettext("action: Wait for the resilver to " 4108fa9e4066Sahrens "complete.\n")); 4109fa9e4066Sahrens break; 4110fa9e4066Sahrens 4111ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_DATA: 4112ea8dc4b6Seschrock (void) printf(gettext("status: One or more devices has " 4113ea8dc4b6Seschrock "experienced an error resulting in data\n\tcorruption. " 4114ea8dc4b6Seschrock "Applications may be affected.\n")); 4115ea8dc4b6Seschrock (void) printf(gettext("action: Restore the file in question " 4116ea8dc4b6Seschrock "if possible. Otherwise restore the\n\tentire pool from " 4117ea8dc4b6Seschrock "backup.\n")); 4118ea8dc4b6Seschrock break; 4119ea8dc4b6Seschrock 4120ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_POOL: 4121ea8dc4b6Seschrock (void) printf(gettext("status: The pool metadata is corrupted " 4122ea8dc4b6Seschrock "and the pool cannot be opened.\n")); 4123468c413aSTim Haley zpool_explain_recover(zpool_get_handle(zhp), 4124468c413aSTim Haley zpool_get_name(zhp), reason, config); 4125ea8dc4b6Seschrock break; 4126ea8dc4b6Seschrock 4127eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_OLDER: 412857221772SChristopher Siden (void) printf(gettext("status: The pool is formatted using a " 412957221772SChristopher Siden "legacy on-disk format. The pool can\n\tstill be used, " 413057221772SChristopher Siden "but some features are unavailable.\n")); 4131eaca9bbdSeschrock (void) printf(gettext("action: Upgrade the pool using 'zpool " 4132eaca9bbdSeschrock "upgrade'. Once this is done, the\n\tpool will no longer " 413357221772SChristopher Siden "be accessible on software that does not support feature\n" 413457221772SChristopher Siden "\tflags.\n")); 4135eaca9bbdSeschrock break; 4136eaca9bbdSeschrock 4137eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER: 4138eaca9bbdSeschrock (void) printf(gettext("status: The pool has been upgraded to a " 4139eaca9bbdSeschrock "newer, incompatible on-disk version.\n\tThe pool cannot " 4140eaca9bbdSeschrock "be accessed on this system.\n")); 4141eaca9bbdSeschrock (void) printf(gettext("action: Access the pool from a system " 4142eaca9bbdSeschrock "running more recent software, or\n\trestore the pool from " 4143eaca9bbdSeschrock "backup.\n")); 4144eaca9bbdSeschrock break; 4145eaca9bbdSeschrock 414657221772SChristopher Siden case ZPOOL_STATUS_FEAT_DISABLED: 414757221772SChristopher Siden (void) printf(gettext("status: Some supported features are not " 414857221772SChristopher Siden "enabled on the pool. The pool can\n\tstill be used, but " 414957221772SChristopher Siden "some features are unavailable.\n")); 415057221772SChristopher Siden (void) printf(gettext("action: Enable all features using " 415157221772SChristopher Siden "'zpool upgrade'. Once this is done,\n\tthe pool may no " 415257221772SChristopher Siden "longer be accessible by software that does not support\n\t" 415357221772SChristopher Siden "the features. See zpool-features(5) for details.\n")); 415457221772SChristopher Siden break; 415557221772SChristopher Siden 4156ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ: 4157ad135b5dSChristopher Siden (void) printf(gettext("status: The pool cannot be accessed on " 4158ad135b5dSChristopher Siden "this system because it uses the\n\tfollowing feature(s) " 4159ad135b5dSChristopher Siden "not supported on this system:\n")); 4160ad135b5dSChristopher Siden zpool_print_unsup_feat(config); 4161ad135b5dSChristopher Siden (void) printf("\n"); 4162ad135b5dSChristopher Siden (void) printf(gettext("action: Access the pool from a system " 4163ad135b5dSChristopher Siden "that supports the required feature(s),\n\tor restore the " 4164ad135b5dSChristopher Siden "pool from backup.\n")); 4165ad135b5dSChristopher Siden break; 4166ad135b5dSChristopher Siden 4167ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 4168ad135b5dSChristopher Siden (void) printf(gettext("status: The pool can only be accessed " 4169ad135b5dSChristopher Siden "in read-only mode on this system. It\n\tcannot be " 4170ad135b5dSChristopher Siden "accessed in read-write mode because it uses the " 4171ad135b5dSChristopher Siden "following\n\tfeature(s) not supported on this system:\n")); 4172ad135b5dSChristopher Siden zpool_print_unsup_feat(config); 4173ad135b5dSChristopher Siden (void) printf("\n"); 4174ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be accessed in " 4175ad135b5dSChristopher Siden "read-write mode. Import the pool with\n" 4176ad135b5dSChristopher Siden "\t\"-o readonly=on\", access the pool from a system that " 4177ad135b5dSChristopher Siden "supports the\n\trequired feature(s), or restore the " 4178ad135b5dSChristopher Siden "pool from backup.\n")); 4179ad135b5dSChristopher Siden break; 4180ad135b5dSChristopher Siden 41813d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_R: 41823d7072f8Seschrock (void) printf(gettext("status: One or more devices are " 41833d7072f8Seschrock "faulted in response to persistent errors.\n\tSufficient " 41843d7072f8Seschrock "replicas exist for the pool to continue functioning " 41853d7072f8Seschrock "in a\n\tdegraded state.\n")); 41863d7072f8Seschrock (void) printf(gettext("action: Replace the faulted device, " 41873d7072f8Seschrock "or use 'zpool clear' to mark the device\n\trepaired.\n")); 41883d7072f8Seschrock break; 41893d7072f8Seschrock 41903d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_NR: 41913d7072f8Seschrock (void) printf(gettext("status: One or more devices are " 41923d7072f8Seschrock "faulted in response to persistent errors. There are " 41933d7072f8Seschrock "insufficient replicas for the pool to\n\tcontinue " 41943d7072f8Seschrock "functioning.\n")); 41953d7072f8Seschrock (void) printf(gettext("action: Destroy and re-create the pool " 41963d7072f8Seschrock "from a backup source. Manually marking the device\n" 41973d7072f8Seschrock "\trepaired using 'zpool clear' may allow some data " 41983d7072f8Seschrock "to be recovered.\n")); 41993d7072f8Seschrock break; 42003d7072f8Seschrock 420132b87932Sek110237 case ZPOOL_STATUS_IO_FAILURE_WAIT: 420232b87932Sek110237 case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 420332b87932Sek110237 (void) printf(gettext("status: One or more devices are " 42048a79c1b5Sek110237 "faulted in response to IO failures.\n")); 420532b87932Sek110237 (void) printf(gettext("action: Make sure the affected devices " 420632b87932Sek110237 "are connected, then run 'zpool clear'.\n")); 420732b87932Sek110237 break; 420832b87932Sek110237 4209b87f3af3Sperrin case ZPOOL_STATUS_BAD_LOG: 4210b87f3af3Sperrin (void) printf(gettext("status: An intent log record " 4211b87f3af3Sperrin "could not be read.\n" 4212b87f3af3Sperrin "\tWaiting for adminstrator intervention to fix the " 4213b87f3af3Sperrin "faulted pool.\n")); 4214b87f3af3Sperrin (void) printf(gettext("action: Either restore the affected " 4215b87f3af3Sperrin "device(s) and run 'zpool online',\n" 4216b87f3af3Sperrin "\tor ignore the intent log records by running " 4217b87f3af3Sperrin "'zpool clear'.\n")); 4218b87f3af3Sperrin break; 4219b87f3af3Sperrin 4220fa9e4066Sahrens default: 4221fa9e4066Sahrens /* 4222fa9e4066Sahrens * The remaining errors can't actually be generated, yet. 4223fa9e4066Sahrens */ 4224fa9e4066Sahrens assert(reason == ZPOOL_STATUS_OK); 4225fa9e4066Sahrens } 4226fa9e4066Sahrens 4227fa9e4066Sahrens if (msgid != NULL) 4228654b400cSJoshua M. Clulow (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 4229fa9e4066Sahrens msgid); 4230fa9e4066Sahrens 4231fa9e4066Sahrens if (config != NULL) { 4232fa9e4066Sahrens int namewidth; 4233ea8dc4b6Seschrock uint64_t nerr; 4234fa94a07fSbrendan nvlist_t **spares, **l2cache; 4235fa94a07fSbrendan uint_t nspares, nl2cache; 42363f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 4237fa9e4066Sahrens 42383f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 42393f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c); 42403f9d6ad7SLin Ling print_scan_status(ps); 4241fa9e4066Sahrens 4242c67d9675Seschrock namewidth = max_width(zhp, nvroot, 0, 0); 4243fa9e4066Sahrens if (namewidth < 10) 4244fa9e4066Sahrens namewidth = 10; 4245fa9e4066Sahrens 4246fa9e4066Sahrens (void) printf(gettext("config:\n\n")); 4247fa9e4066Sahrens (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth, 4248fa9e4066Sahrens "NAME", "STATE", "READ", "WRITE", "CKSUM"); 4249c67d9675Seschrock print_status_config(zhp, zpool_get_name(zhp), nvroot, 4250aa8cf21aSNeil Perrin namewidth, 0, B_FALSE); 425199653d4eSeschrock 42524dea40f0SNeil Perrin if (num_logs(nvroot) > 0) 4253e6ca193dSGeorge Wilson print_logs(zhp, nvroot, namewidth, B_TRUE); 4254fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 4255fa94a07fSbrendan &l2cache, &nl2cache) == 0) 4256fa94a07fSbrendan print_l2cache(zhp, l2cache, nl2cache, namewidth); 4257fa94a07fSbrendan 425899653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 425999653d4eSeschrock &spares, &nspares) == 0) 426099653d4eSeschrock print_spares(zhp, spares, nspares, namewidth); 4261ea8dc4b6Seschrock 4262ea8dc4b6Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 4263ea8dc4b6Seschrock &nerr) == 0) { 426455434c77Sek110237 nvlist_t *nverrlist = NULL; 426555434c77Sek110237 4266ea8dc4b6Seschrock /* 4267ea8dc4b6Seschrock * If the approximate error count is small, get a 4268ea8dc4b6Seschrock * precise count by fetching the entire log and 4269ea8dc4b6Seschrock * uniquifying the results. 4270ea8dc4b6Seschrock */ 427175519f38Sek110237 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && 427255434c77Sek110237 zpool_get_errlog(zhp, &nverrlist) == 0) { 427355434c77Sek110237 nvpair_t *elem; 427455434c77Sek110237 427555434c77Sek110237 elem = NULL; 427655434c77Sek110237 nerr = 0; 427755434c77Sek110237 while ((elem = nvlist_next_nvpair(nverrlist, 427855434c77Sek110237 elem)) != NULL) { 427955434c77Sek110237 nerr++; 428055434c77Sek110237 } 428155434c77Sek110237 } 428255434c77Sek110237 nvlist_free(nverrlist); 4283ea8dc4b6Seschrock 4284ea8dc4b6Seschrock (void) printf("\n"); 428599653d4eSeschrock 4286ea8dc4b6Seschrock if (nerr == 0) 4287ea8dc4b6Seschrock (void) printf(gettext("errors: No known data " 4288ea8dc4b6Seschrock "errors\n")); 4289ea8dc4b6Seschrock else if (!cbp->cb_verbose) 4290e9dbad6fSeschrock (void) printf(gettext("errors: %llu data " 42915ad82045Snd150628 "errors, use '-v' for a list\n"), 42925ad82045Snd150628 (u_longlong_t)nerr); 4293ea8dc4b6Seschrock else 4294ea8dc4b6Seschrock print_error_log(zhp); 4295ea8dc4b6Seschrock } 42969eb19f4dSGeorge Wilson 42979eb19f4dSGeorge Wilson if (cbp->cb_dedup_stats) 42989eb19f4dSGeorge Wilson print_dedup_stats(config); 4299fa9e4066Sahrens } else { 4300fa9e4066Sahrens (void) printf(gettext("config: The configuration cannot be " 4301fa9e4066Sahrens "determined.\n")); 4302fa9e4066Sahrens } 4303fa9e4066Sahrens 4304fa9e4066Sahrens return (0); 4305fa9e4066Sahrens } 4306fa9e4066Sahrens 4307fa9e4066Sahrens /* 43083f9d6ad7SLin Ling * zpool status [-vx] [-T d|u] [pool] ... [interval [count]] 4309fa9e4066Sahrens * 4310fa9e4066Sahrens * -v Display complete error logs 4311fa9e4066Sahrens * -x Display only pools with potential problems 43129eb19f4dSGeorge Wilson * -D Display dedup status (undocumented) 43133f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format 4314fa9e4066Sahrens * 4315fa9e4066Sahrens * Describes the health status of all pools or some subset. 4316fa9e4066Sahrens */ 4317fa9e4066Sahrens int 4318fa9e4066Sahrens zpool_do_status(int argc, char **argv) 4319fa9e4066Sahrens { 4320fa9e4066Sahrens int c; 4321fa9e4066Sahrens int ret; 43223f9d6ad7SLin Ling unsigned long interval = 0, count = 0; 4323fa9e4066Sahrens status_cbdata_t cb = { 0 }; 4324fa9e4066Sahrens 4325fa9e4066Sahrens /* check options */ 43263f9d6ad7SLin Ling while ((c = getopt(argc, argv, "vxDT:")) != -1) { 4327fa9e4066Sahrens switch (c) { 4328fa9e4066Sahrens case 'v': 432999653d4eSeschrock cb.cb_verbose = B_TRUE; 4330fa9e4066Sahrens break; 4331fa9e4066Sahrens case 'x': 433299653d4eSeschrock cb.cb_explain = B_TRUE; 4333fa9e4066Sahrens break; 43349eb19f4dSGeorge Wilson case 'D': 43359eb19f4dSGeorge Wilson cb.cb_dedup_stats = B_TRUE; 43369eb19f4dSGeorge Wilson break; 43373f9d6ad7SLin Ling case 'T': 43383f9d6ad7SLin Ling get_timestamp_arg(*optarg); 43393f9d6ad7SLin Ling break; 4340fa9e4066Sahrens case '?': 4341fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4342fa9e4066Sahrens optopt); 434399653d4eSeschrock usage(B_FALSE); 4344fa9e4066Sahrens } 4345fa9e4066Sahrens } 4346fa9e4066Sahrens 4347fa9e4066Sahrens argc -= optind; 4348fa9e4066Sahrens argv += optind; 4349fa9e4066Sahrens 43503f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count); 4351fa9e4066Sahrens 4352e9dbad6fSeschrock if (argc == 0) 4353e9dbad6fSeschrock cb.cb_allpools = B_TRUE; 4354e9dbad6fSeschrock 43553f9d6ad7SLin Ling cb.cb_first = B_TRUE; 43563f9d6ad7SLin Ling 43573f9d6ad7SLin Ling for (;;) { 43583f9d6ad7SLin Ling if (timestamp_fmt != NODATE) 43593f9d6ad7SLin Ling print_timestamp(timestamp_fmt); 43603f9d6ad7SLin Ling 43613f9d6ad7SLin Ling ret = for_each_pool(argc, argv, B_TRUE, NULL, 43623f9d6ad7SLin Ling status_callback, &cb); 4363fa9e4066Sahrens 4364fa9e4066Sahrens if (argc == 0 && cb.cb_count == 0) 4365fa9e4066Sahrens (void) printf(gettext("no pools available\n")); 4366e9dbad6fSeschrock else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) 4367fa9e4066Sahrens (void) printf(gettext("all pools are healthy\n")); 4368fa9e4066Sahrens 43693f9d6ad7SLin Ling if (ret != 0) 4370fa9e4066Sahrens return (ret); 43713f9d6ad7SLin Ling 43723f9d6ad7SLin Ling if (interval == 0) 43733f9d6ad7SLin Ling break; 43743f9d6ad7SLin Ling 43753f9d6ad7SLin Ling if (count != 0 && --count == 0) 43763f9d6ad7SLin Ling break; 43773f9d6ad7SLin Ling 43783f9d6ad7SLin Ling (void) sleep(interval); 43793f9d6ad7SLin Ling } 43803f9d6ad7SLin Ling 43813f9d6ad7SLin Ling return (0); 4382fa9e4066Sahrens } 4383fa9e4066Sahrens 4384eaca9bbdSeschrock typedef struct upgrade_cbdata { 4385eaca9bbdSeschrock int cb_first; 438606eeb2adSek110237 int cb_argc; 4387990b4856Slling uint64_t cb_version; 438806eeb2adSek110237 char **cb_argv; 4389eaca9bbdSeschrock } upgrade_cbdata_t; 4390eaca9bbdSeschrock 4391eaca9bbdSeschrock static int 439257221772SChristopher Siden upgrade_version(zpool_handle_t *zhp, uint64_t version) 439357221772SChristopher Siden { 439457221772SChristopher Siden int ret; 439557221772SChristopher Siden nvlist_t *config; 439657221772SChristopher Siden uint64_t oldversion; 439757221772SChristopher Siden 439857221772SChristopher Siden config = zpool_get_config(zhp, NULL); 439957221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 440057221772SChristopher Siden &oldversion) == 0); 440157221772SChristopher Siden 440257221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 440357221772SChristopher Siden assert(oldversion < version); 440457221772SChristopher Siden 440557221772SChristopher Siden ret = zpool_upgrade(zhp, version); 440657221772SChristopher Siden if (ret != 0) 440757221772SChristopher Siden return (ret); 440857221772SChristopher Siden 440957221772SChristopher Siden if (version >= SPA_VERSION_FEATURES) { 441057221772SChristopher Siden (void) printf(gettext("Successfully upgraded " 441157221772SChristopher Siden "'%s' from version %llu to feature flags.\n"), 441257221772SChristopher Siden zpool_get_name(zhp), oldversion); 441357221772SChristopher Siden } else { 441457221772SChristopher Siden (void) printf(gettext("Successfully upgraded " 441557221772SChristopher Siden "'%s' from version %llu to version %llu.\n"), 441657221772SChristopher Siden zpool_get_name(zhp), oldversion, version); 441757221772SChristopher Siden } 441857221772SChristopher Siden 441957221772SChristopher Siden return (0); 442057221772SChristopher Siden } 442157221772SChristopher Siden 442257221772SChristopher Siden static int 442357221772SChristopher Siden upgrade_enable_all(zpool_handle_t *zhp, int *countp) 442457221772SChristopher Siden { 442557221772SChristopher Siden int i, ret, count; 442657221772SChristopher Siden boolean_t firstff = B_TRUE; 442757221772SChristopher Siden nvlist_t *enabled = zpool_get_features(zhp); 442857221772SChristopher Siden 442957221772SChristopher Siden count = 0; 443057221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) { 443157221772SChristopher Siden const char *fname = spa_feature_table[i].fi_uname; 443257221772SChristopher Siden const char *fguid = spa_feature_table[i].fi_guid; 443357221772SChristopher Siden if (!nvlist_exists(enabled, fguid)) { 443457221772SChristopher Siden char *propname; 443557221772SChristopher Siden verify(-1 != asprintf(&propname, "feature@%s", fname)); 443657221772SChristopher Siden ret = zpool_set_prop(zhp, propname, 443757221772SChristopher Siden ZFS_FEATURE_ENABLED); 443857221772SChristopher Siden if (ret != 0) { 443957221772SChristopher Siden free(propname); 444057221772SChristopher Siden return (ret); 444157221772SChristopher Siden } 444257221772SChristopher Siden count++; 444357221772SChristopher Siden 444457221772SChristopher Siden if (firstff) { 444557221772SChristopher Siden (void) printf(gettext("Enabled the " 444657221772SChristopher Siden "following features on '%s':\n"), 444757221772SChristopher Siden zpool_get_name(zhp)); 444857221772SChristopher Siden firstff = B_FALSE; 444957221772SChristopher Siden } 445057221772SChristopher Siden (void) printf(gettext(" %s\n"), fname); 445157221772SChristopher Siden free(propname); 445257221772SChristopher Siden } 445357221772SChristopher Siden } 445457221772SChristopher Siden 445557221772SChristopher Siden if (countp != NULL) 445657221772SChristopher Siden *countp = count; 445757221772SChristopher Siden return (0); 445857221772SChristopher Siden } 445957221772SChristopher Siden 446057221772SChristopher Siden static int 4461eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg) 4462eaca9bbdSeschrock { 4463eaca9bbdSeschrock upgrade_cbdata_t *cbp = arg; 4464eaca9bbdSeschrock nvlist_t *config; 4465eaca9bbdSeschrock uint64_t version; 446657221772SChristopher Siden boolean_t printnl = B_FALSE; 446757221772SChristopher Siden int ret; 4468eaca9bbdSeschrock 4469eaca9bbdSeschrock config = zpool_get_config(zhp, NULL); 4470eaca9bbdSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4471eaca9bbdSeschrock &version) == 0); 4472eaca9bbdSeschrock 447357221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(version)); 4474eaca9bbdSeschrock 447557221772SChristopher Siden if (version < cbp->cb_version) { 447699653d4eSeschrock cbp->cb_first = B_FALSE; 447757221772SChristopher Siden ret = upgrade_version(zhp, cbp->cb_version); 447857221772SChristopher Siden if (ret != 0) 447957221772SChristopher Siden return (ret); 448057221772SChristopher Siden printnl = B_TRUE; 448157221772SChristopher Siden 44824445fffbSMatthew Ahrens /* 44834445fffbSMatthew Ahrens * If they did "zpool upgrade -a", then we could 44844445fffbSMatthew Ahrens * be doing ioctls to different pools. We need 44854445fffbSMatthew Ahrens * to log this history once to each pool, and bypass 44864445fffbSMatthew Ahrens * the normal history logging that happens in main(). 44874445fffbSMatthew Ahrens */ 44884445fffbSMatthew Ahrens (void) zpool_log_history(g_zfs, history_str); 44894445fffbSMatthew Ahrens log_history = B_FALSE; 449006eeb2adSek110237 } 4491eaca9bbdSeschrock 449257221772SChristopher Siden if (cbp->cb_version >= SPA_VERSION_FEATURES) { 449357221772SChristopher Siden int count; 449457221772SChristopher Siden ret = upgrade_enable_all(zhp, &count); 449557221772SChristopher Siden if (ret != 0) 449657221772SChristopher Siden return (ret); 449757221772SChristopher Siden 449857221772SChristopher Siden if (count > 0) { 449957221772SChristopher Siden cbp->cb_first = B_FALSE; 450057221772SChristopher Siden printnl = B_TRUE; 450157221772SChristopher Siden } 450257221772SChristopher Siden } 450357221772SChristopher Siden 450457221772SChristopher Siden if (printnl) { 450557221772SChristopher Siden (void) printf(gettext("\n")); 450657221772SChristopher Siden } 450757221772SChristopher Siden 450857221772SChristopher Siden return (0); 450957221772SChristopher Siden } 451057221772SChristopher Siden 451157221772SChristopher Siden static int 451257221772SChristopher Siden upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 451357221772SChristopher Siden { 451457221772SChristopher Siden upgrade_cbdata_t *cbp = arg; 451557221772SChristopher Siden nvlist_t *config; 451657221772SChristopher Siden uint64_t version; 451757221772SChristopher Siden 451857221772SChristopher Siden config = zpool_get_config(zhp, NULL); 451957221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 452057221772SChristopher Siden &version) == 0); 452157221772SChristopher Siden 452257221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(version)); 452357221772SChristopher Siden 452457221772SChristopher Siden if (version < SPA_VERSION_FEATURES) { 4525eaca9bbdSeschrock if (cbp->cb_first) { 4526eaca9bbdSeschrock (void) printf(gettext("The following pools are " 452757221772SChristopher Siden "formatted with legacy version numbers and can\n" 452857221772SChristopher Siden "be upgraded to use feature flags. After " 452957221772SChristopher Siden "being upgraded, these pools\nwill no " 453057221772SChristopher Siden "longer be accessible by software that does not " 453157221772SChristopher Siden "support feature\nflags.\n\n")); 4532eaca9bbdSeschrock (void) printf(gettext("VER POOL\n")); 4533eaca9bbdSeschrock (void) printf(gettext("--- ------------\n")); 453499653d4eSeschrock cbp->cb_first = B_FALSE; 4535eaca9bbdSeschrock } 4536eaca9bbdSeschrock 45375ad82045Snd150628 (void) printf("%2llu %s\n", (u_longlong_t)version, 4538eaca9bbdSeschrock zpool_get_name(zhp)); 4539eaca9bbdSeschrock } 4540eaca9bbdSeschrock 454157221772SChristopher Siden return (0); 454257221772SChristopher Siden } 454357221772SChristopher Siden 454457221772SChristopher Siden static int 454557221772SChristopher Siden upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 454657221772SChristopher Siden { 454757221772SChristopher Siden upgrade_cbdata_t *cbp = arg; 454857221772SChristopher Siden nvlist_t *config; 454957221772SChristopher Siden uint64_t version; 455057221772SChristopher Siden 455157221772SChristopher Siden config = zpool_get_config(zhp, NULL); 455257221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 455357221772SChristopher Siden &version) == 0); 455457221772SChristopher Siden 455557221772SChristopher Siden if (version >= SPA_VERSION_FEATURES) { 455657221772SChristopher Siden int i; 455757221772SChristopher Siden boolean_t poolfirst = B_TRUE; 455857221772SChristopher Siden nvlist_t *enabled = zpool_get_features(zhp); 455957221772SChristopher Siden 456057221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) { 456157221772SChristopher Siden const char *fguid = spa_feature_table[i].fi_guid; 456257221772SChristopher Siden const char *fname = spa_feature_table[i].fi_uname; 456357221772SChristopher Siden if (!nvlist_exists(enabled, fguid)) { 456457221772SChristopher Siden if (cbp->cb_first) { 456557221772SChristopher Siden (void) printf(gettext("\nSome " 456657221772SChristopher Siden "supported features are not " 456757221772SChristopher Siden "enabled on the following pools. " 456857221772SChristopher Siden "Once a\nfeature is enabled the " 456957221772SChristopher Siden "pool may become incompatible with " 457057221772SChristopher Siden "software\nthat does not support " 457157221772SChristopher Siden "the feature. See " 457257221772SChristopher Siden "zpool-features(5) for " 457357221772SChristopher Siden "details.\n\n")); 457457221772SChristopher Siden (void) printf(gettext("POOL " 457557221772SChristopher Siden "FEATURE\n")); 457657221772SChristopher Siden (void) printf(gettext("------" 457757221772SChristopher Siden "---------\n")); 457857221772SChristopher Siden cbp->cb_first = B_FALSE; 457957221772SChristopher Siden } 458057221772SChristopher Siden 458157221772SChristopher Siden if (poolfirst) { 458257221772SChristopher Siden (void) printf(gettext("%s\n"), 458357221772SChristopher Siden zpool_get_name(zhp)); 458457221772SChristopher Siden poolfirst = B_FALSE; 458557221772SChristopher Siden } 458657221772SChristopher Siden 458757221772SChristopher Siden (void) printf(gettext(" %s\n"), fname); 458857221772SChristopher Siden } 458957221772SChristopher Siden } 459057221772SChristopher Siden } 459157221772SChristopher Siden 459257221772SChristopher Siden return (0); 4593eaca9bbdSeschrock } 4594eaca9bbdSeschrock 4595eaca9bbdSeschrock /* ARGSUSED */ 4596eaca9bbdSeschrock static int 459706eeb2adSek110237 upgrade_one(zpool_handle_t *zhp, void *data) 4598eaca9bbdSeschrock { 459957221772SChristopher Siden boolean_t printnl = B_FALSE; 4600990b4856Slling upgrade_cbdata_t *cbp = data; 4601990b4856Slling uint64_t cur_version; 4602eaca9bbdSeschrock int ret; 4603eaca9bbdSeschrock 46048654d025Sperrin if (strcmp("log", zpool_get_name(zhp)) == 0) { 46058654d025Sperrin (void) printf(gettext("'log' is now a reserved word\n" 46068654d025Sperrin "Pool 'log' must be renamed using export and import" 46078654d025Sperrin " to upgrade.\n")); 46088654d025Sperrin return (1); 46098654d025Sperrin } 4610990b4856Slling 4611990b4856Slling cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 4612e6c728e1Sbrendan if (cur_version > cbp->cb_version) { 4613eaca9bbdSeschrock (void) printf(gettext("Pool '%s' is already formatted " 461457221772SChristopher Siden "using more current version '%llu'.\n\n"), 4615e6c728e1Sbrendan zpool_get_name(zhp), cur_version); 4616e6c728e1Sbrendan return (0); 4617e6c728e1Sbrendan } 461857221772SChristopher Siden 461957221772SChristopher Siden if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 4620e6c728e1Sbrendan (void) printf(gettext("Pool '%s' is already formatted " 462157221772SChristopher Siden "using version %llu.\n\n"), zpool_get_name(zhp), 462257221772SChristopher Siden cbp->cb_version); 4623eaca9bbdSeschrock return (0); 4624eaca9bbdSeschrock } 4625eaca9bbdSeschrock 462657221772SChristopher Siden if (cur_version != cbp->cb_version) { 462757221772SChristopher Siden printnl = B_TRUE; 462857221772SChristopher Siden ret = upgrade_version(zhp, cbp->cb_version); 462957221772SChristopher Siden if (ret != 0) 463057221772SChristopher Siden return (ret); 463106eeb2adSek110237 } 4632eaca9bbdSeschrock 463357221772SChristopher Siden if (cbp->cb_version >= SPA_VERSION_FEATURES) { 463457221772SChristopher Siden int count = 0; 463557221772SChristopher Siden ret = upgrade_enable_all(zhp, &count); 463657221772SChristopher Siden if (ret != 0) 463757221772SChristopher Siden return (ret); 463857221772SChristopher Siden 463957221772SChristopher Siden if (count != 0) { 464057221772SChristopher Siden printnl = B_TRUE; 464157221772SChristopher Siden } else if (cur_version == SPA_VERSION) { 464257221772SChristopher Siden (void) printf(gettext("Pool '%s' already has all " 464357221772SChristopher Siden "supported features enabled.\n"), 464457221772SChristopher Siden zpool_get_name(zhp)); 464557221772SChristopher Siden } 464657221772SChristopher Siden } 464757221772SChristopher Siden 464857221772SChristopher Siden if (printnl) { 464957221772SChristopher Siden (void) printf(gettext("\n")); 465057221772SChristopher Siden } 465157221772SChristopher Siden 465257221772SChristopher Siden return (0); 4653eaca9bbdSeschrock } 4654eaca9bbdSeschrock 4655eaca9bbdSeschrock /* 4656eaca9bbdSeschrock * zpool upgrade 4657eaca9bbdSeschrock * zpool upgrade -v 4658990b4856Slling * zpool upgrade [-V version] <-a | pool ...> 4659eaca9bbdSeschrock * 4660eaca9bbdSeschrock * With no arguments, display downrev'd ZFS pool available for upgrade. 4661eaca9bbdSeschrock * Individual pools can be upgraded by specifying the pool, and '-a' will 4662eaca9bbdSeschrock * upgrade all pools. 4663eaca9bbdSeschrock */ 4664eaca9bbdSeschrock int 4665eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv) 4666eaca9bbdSeschrock { 4667eaca9bbdSeschrock int c; 4668eaca9bbdSeschrock upgrade_cbdata_t cb = { 0 }; 4669eaca9bbdSeschrock int ret = 0; 4670eaca9bbdSeschrock boolean_t showversions = B_FALSE; 467157221772SChristopher Siden boolean_t upgradeall = B_FALSE; 4672990b4856Slling char *end; 4673990b4856Slling 4674eaca9bbdSeschrock 4675eaca9bbdSeschrock /* check options */ 4676478ed9adSEric Taylor while ((c = getopt(argc, argv, ":avV:")) != -1) { 4677eaca9bbdSeschrock switch (c) { 4678eaca9bbdSeschrock case 'a': 467957221772SChristopher Siden upgradeall = B_TRUE; 4680eaca9bbdSeschrock break; 4681eaca9bbdSeschrock case 'v': 4682eaca9bbdSeschrock showversions = B_TRUE; 4683eaca9bbdSeschrock break; 4684990b4856Slling case 'V': 4685990b4856Slling cb.cb_version = strtoll(optarg, &end, 10); 4686ad135b5dSChristopher Siden if (*end != '\0' || 4687ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 4688990b4856Slling (void) fprintf(stderr, 4689990b4856Slling gettext("invalid version '%s'\n"), optarg); 4690990b4856Slling usage(B_FALSE); 4691990b4856Slling } 4692990b4856Slling break; 4693478ed9adSEric Taylor case ':': 4694478ed9adSEric Taylor (void) fprintf(stderr, gettext("missing argument for " 4695478ed9adSEric Taylor "'%c' option\n"), optopt); 4696478ed9adSEric Taylor usage(B_FALSE); 4697478ed9adSEric Taylor break; 4698eaca9bbdSeschrock case '?': 4699eaca9bbdSeschrock (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4700eaca9bbdSeschrock optopt); 470199653d4eSeschrock usage(B_FALSE); 4702eaca9bbdSeschrock } 4703eaca9bbdSeschrock } 4704eaca9bbdSeschrock 470506eeb2adSek110237 cb.cb_argc = argc; 470606eeb2adSek110237 cb.cb_argv = argv; 4707eaca9bbdSeschrock argc -= optind; 4708eaca9bbdSeschrock argv += optind; 4709eaca9bbdSeschrock 4710351420b3Slling if (cb.cb_version == 0) { 4711351420b3Slling cb.cb_version = SPA_VERSION; 471257221772SChristopher Siden } else if (!upgradeall && argc == 0) { 4713351420b3Slling (void) fprintf(stderr, gettext("-V option is " 4714351420b3Slling "incompatible with other arguments\n")); 4715351420b3Slling usage(B_FALSE); 4716351420b3Slling } 4717351420b3Slling 4718eaca9bbdSeschrock if (showversions) { 471957221772SChristopher Siden if (upgradeall || argc != 0) { 4720eaca9bbdSeschrock (void) fprintf(stderr, gettext("-v option is " 4721eaca9bbdSeschrock "incompatible with other arguments\n")); 472299653d4eSeschrock usage(B_FALSE); 4723eaca9bbdSeschrock } 472457221772SChristopher Siden } else if (upgradeall) { 4725eaca9bbdSeschrock if (argc != 0) { 4726351420b3Slling (void) fprintf(stderr, gettext("-a option should not " 4727351420b3Slling "be used along with a pool name\n")); 472899653d4eSeschrock usage(B_FALSE); 4729eaca9bbdSeschrock } 4730eaca9bbdSeschrock } 4731eaca9bbdSeschrock 4732ad135b5dSChristopher Siden (void) printf(gettext("This system supports ZFS pool feature " 4733ad135b5dSChristopher Siden "flags.\n\n")); 4734eaca9bbdSeschrock if (showversions) { 473557221772SChristopher Siden int i; 473657221772SChristopher Siden 473757221772SChristopher Siden (void) printf(gettext("The following features are " 473857221772SChristopher Siden "supported:\n\n")); 473957221772SChristopher Siden (void) printf(gettext("FEAT DESCRIPTION\n")); 474057221772SChristopher Siden (void) printf("----------------------------------------------" 474157221772SChristopher Siden "---------------\n"); 474257221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) { 474357221772SChristopher Siden zfeature_info_t *fi = &spa_feature_table[i]; 4744ca0cc391SMatthew Ahrens const char *ro = 4745ca0cc391SMatthew Ahrens (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 474657221772SChristopher Siden " (read-only compatible)" : ""; 474757221772SChristopher Siden 474857221772SChristopher Siden (void) printf("%-37s%s\n", fi->fi_uname, ro); 474957221772SChristopher Siden (void) printf(" %s\n", fi->fi_desc); 475057221772SChristopher Siden } 475157221772SChristopher Siden (void) printf("\n"); 475257221772SChristopher Siden 475357221772SChristopher Siden (void) printf(gettext("The following legacy versions are also " 4754d7d4af51Smmusante "supported:\n\n")); 4755eaca9bbdSeschrock (void) printf(gettext("VER DESCRIPTION\n")); 4756eaca9bbdSeschrock (void) printf("--- -----------------------------------------" 4757eaca9bbdSeschrock "---------------\n"); 475899653d4eSeschrock (void) printf(gettext(" 1 Initial ZFS version\n")); 475944cd46caSbillm (void) printf(gettext(" 2 Ditto blocks " 476044cd46caSbillm "(replicated metadata)\n")); 476199653d4eSeschrock (void) printf(gettext(" 3 Hot spares and double parity " 476299653d4eSeschrock "RAID-Z\n")); 4763d7306b64Sek110237 (void) printf(gettext(" 4 zpool history\n")); 4764c9431fa1Sahl (void) printf(gettext(" 5 Compression using the gzip " 4765c9431fa1Sahl "algorithm\n")); 4766990b4856Slling (void) printf(gettext(" 6 bootfs pool property\n")); 47678654d025Sperrin (void) printf(gettext(" 7 Separate intent log devices\n")); 4768ecd6cf80Smarks (void) printf(gettext(" 8 Delegated administration\n")); 4769a9799022Sck153898 (void) printf(gettext(" 9 refquota and refreservation " 4770a9799022Sck153898 "properties\n")); 4771fa94a07fSbrendan (void) printf(gettext(" 10 Cache devices\n")); 4772088f3894Sahrens (void) printf(gettext(" 11 Improved scrub performance\n")); 4773bb0ade09Sahrens (void) printf(gettext(" 12 Snapshot properties\n")); 477474e7dc98SMatthew Ahrens (void) printf(gettext(" 13 snapused property\n")); 477514843421SMatthew Ahrens (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 477614843421SMatthew Ahrens (void) printf(gettext(" 15 user/group space accounting\n")); 4777478ed9adSEric Taylor (void) printf(gettext(" 16 stmf property support\n")); 47787aeab329SAdam Leventhal (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 4779b24ab676SJeff Bonwick (void) printf(gettext(" 18 Snapshot user holds\n")); 478088ecc943SGeorge Wilson (void) printf(gettext(" 19 Log device removal\n")); 4781b24ab676SJeff Bonwick (void) printf(gettext(" 20 Compression using zle " 4782b24ab676SJeff Bonwick "(zero-length encoding)\n")); 4783b24ab676SJeff Bonwick (void) printf(gettext(" 21 Deduplication\n")); 478492241e0bSTom Erickson (void) printf(gettext(" 22 Received properties\n")); 47856e1f5caaSNeil Perrin (void) printf(gettext(" 23 Slim ZIL\n")); 47860a586ceaSMark Shellenbaum (void) printf(gettext(" 24 System attributes\n")); 47873f9d6ad7SLin Ling (void) printf(gettext(" 25 Improved scrub stats\n")); 4788cde58dbcSMatthew Ahrens (void) printf(gettext(" 26 Improved snapshot deletion " 4789cde58dbcSMatthew Ahrens "performance\n")); 47906e0cbcaaSMatthew Ahrens (void) printf(gettext(" 27 Improved snapshot creation " 47916e0cbcaaSMatthew Ahrens "performance\n")); 4792cb04b873SMark J Musante (void) printf(gettext(" 28 Multiple vdev replacements\n")); 4793b24ab676SJeff Bonwick (void) printf(gettext("\nFor more information on a particular " 47949a8685acSstephanie scheffler "version, including supported releases,\n")); 47959a8685acSstephanie scheffler (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 479657221772SChristopher Siden } else if (argc == 0 && upgradeall) { 4797eaca9bbdSeschrock cb.cb_first = B_TRUE; 479899653d4eSeschrock ret = zpool_iter(g_zfs, upgrade_cb, &cb); 479957221772SChristopher Siden if (ret == 0 && cb.cb_first) { 480057221772SChristopher Siden if (cb.cb_version == SPA_VERSION) { 480157221772SChristopher Siden (void) printf(gettext("All pools are already " 480257221772SChristopher Siden "formatted using feature flags.\n\n")); 480357221772SChristopher Siden (void) printf(gettext("Every feature flags " 480457221772SChristopher Siden "pool already has all supported features " 480557221772SChristopher Siden "enabled.\n")); 480657221772SChristopher Siden } else { 480757221772SChristopher Siden (void) printf(gettext("All pools are already " 480857221772SChristopher Siden "formatted with version %llu or higher.\n"), 480957221772SChristopher Siden cb.cb_version); 4810eaca9bbdSeschrock } 4811eaca9bbdSeschrock } 481257221772SChristopher Siden } else if (argc == 0) { 481357221772SChristopher Siden cb.cb_first = B_TRUE; 481457221772SChristopher Siden ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 481557221772SChristopher Siden assert(ret == 0); 481657221772SChristopher Siden 481757221772SChristopher Siden if (cb.cb_first) { 481857221772SChristopher Siden (void) printf(gettext("All pools are formatted " 481957221772SChristopher Siden "using feature flags.\n\n")); 482057221772SChristopher Siden } else { 482157221772SChristopher Siden (void) printf(gettext("\nUse 'zpool upgrade -v' " 482257221772SChristopher Siden "for a list of available legacy versions.\n")); 482357221772SChristopher Siden } 4824eaca9bbdSeschrock 482557221772SChristopher Siden cb.cb_first = B_TRUE; 482657221772SChristopher Siden ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 482757221772SChristopher Siden assert(ret == 0); 482857221772SChristopher Siden 482957221772SChristopher Siden if (cb.cb_first) { 483057221772SChristopher Siden (void) printf(gettext("Every feature flags pool has " 483157221772SChristopher Siden "all supported features enabled.\n")); 483257221772SChristopher Siden } else { 483357221772SChristopher Siden (void) printf(gettext("\n")); 4834eaca9bbdSeschrock } 4835eaca9bbdSeschrock } else { 4836b1b8ab34Slling ret = for_each_pool(argc, argv, B_FALSE, NULL, 4837b1b8ab34Slling upgrade_one, &cb); 483806eeb2adSek110237 } 483906eeb2adSek110237 484006eeb2adSek110237 return (ret); 484106eeb2adSek110237 } 484206eeb2adSek110237 4843ecd6cf80Smarks typedef struct hist_cbdata { 4844ecd6cf80Smarks boolean_t first; 48454445fffbSMatthew Ahrens boolean_t longfmt; 48464445fffbSMatthew Ahrens boolean_t internal; 4847ecd6cf80Smarks } hist_cbdata_t; 4848ecd6cf80Smarks 484906eeb2adSek110237 /* 485006eeb2adSek110237 * Print out the command history for a specific pool. 485106eeb2adSek110237 */ 485206eeb2adSek110237 static int 485306eeb2adSek110237 get_history_one(zpool_handle_t *zhp, void *data) 485406eeb2adSek110237 { 485506eeb2adSek110237 nvlist_t *nvhis; 485606eeb2adSek110237 nvlist_t **records; 485706eeb2adSek110237 uint_t numrecords; 485806eeb2adSek110237 int ret, i; 4859ecd6cf80Smarks hist_cbdata_t *cb = (hist_cbdata_t *)data; 486006eeb2adSek110237 4861ecd6cf80Smarks cb->first = B_FALSE; 486206eeb2adSek110237 486306eeb2adSek110237 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 486406eeb2adSek110237 486506eeb2adSek110237 if ((ret = zpool_get_history(zhp, &nvhis)) != 0) 486606eeb2adSek110237 return (ret); 486706eeb2adSek110237 486806eeb2adSek110237 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 486906eeb2adSek110237 &records, &numrecords) == 0); 487006eeb2adSek110237 for (i = 0; i < numrecords; i++) { 48714445fffbSMatthew Ahrens nvlist_t *rec = records[i]; 48724445fffbSMatthew Ahrens char tbuf[30] = ""; 4873ecd6cf80Smarks 48744445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 48754445fffbSMatthew Ahrens time_t tsec; 48764445fffbSMatthew Ahrens struct tm t; 4877ecd6cf80Smarks 48784445fffbSMatthew Ahrens tsec = fnvlist_lookup_uint64(records[i], 48794445fffbSMatthew Ahrens ZPOOL_HIST_TIME); 488006eeb2adSek110237 (void) localtime_r(&tsec, &t); 488106eeb2adSek110237 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 48824445fffbSMatthew Ahrens } 48834445fffbSMatthew Ahrens 48844445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 48854445fffbSMatthew Ahrens (void) printf("%s %s", tbuf, 48864445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 48874445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 48884445fffbSMatthew Ahrens int ievent = 48894445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 48904445fffbSMatthew Ahrens if (!cb->internal) 48914445fffbSMatthew Ahrens continue; 48924445fffbSMatthew Ahrens if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 48934445fffbSMatthew Ahrens (void) printf("%s unrecognized record:\n", 48944445fffbSMatthew Ahrens tbuf); 48954445fffbSMatthew Ahrens dump_nvlist(rec, 4); 48964445fffbSMatthew Ahrens continue; 48974445fffbSMatthew Ahrens } 48984445fffbSMatthew Ahrens (void) printf("%s [internal %s txg:%lld] %s", tbuf, 48994445fffbSMatthew Ahrens zfs_history_event_names[ievent], 49004445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 49014445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 49024445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 49034445fffbSMatthew Ahrens if (!cb->internal) 49044445fffbSMatthew Ahrens continue; 49054445fffbSMatthew Ahrens (void) printf("%s [txg:%lld] %s", tbuf, 49064445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 49074445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 49084445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 49094445fffbSMatthew Ahrens (void) printf(" %s (%llu)", 49104445fffbSMatthew Ahrens fnvlist_lookup_string(rec, 49114445fffbSMatthew Ahrens ZPOOL_HIST_DSNAME), 49124445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, 49134445fffbSMatthew Ahrens ZPOOL_HIST_DSID)); 49144445fffbSMatthew Ahrens } 49154445fffbSMatthew Ahrens (void) printf(" %s", fnvlist_lookup_string(rec, 49164445fffbSMatthew Ahrens ZPOOL_HIST_INT_STR)); 49174445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 49184445fffbSMatthew Ahrens if (!cb->internal) 49194445fffbSMatthew Ahrens continue; 49204445fffbSMatthew Ahrens (void) printf("%s ioctl %s\n", tbuf, 49214445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 49224445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 49234445fffbSMatthew Ahrens (void) printf(" input:\n"); 49244445fffbSMatthew Ahrens dump_nvlist(fnvlist_lookup_nvlist(rec, 49254445fffbSMatthew Ahrens ZPOOL_HIST_INPUT_NVL), 8); 49264445fffbSMatthew Ahrens } 49274445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 49284445fffbSMatthew Ahrens (void) printf(" output:\n"); 49294445fffbSMatthew Ahrens dump_nvlist(fnvlist_lookup_nvlist(rec, 49304445fffbSMatthew Ahrens ZPOOL_HIST_OUTPUT_NVL), 8); 49314445fffbSMatthew Ahrens } 49324445fffbSMatthew Ahrens } else { 49334445fffbSMatthew Ahrens if (!cb->internal) 49344445fffbSMatthew Ahrens continue; 49354445fffbSMatthew Ahrens (void) printf("%s unrecognized record:\n", tbuf); 49364445fffbSMatthew Ahrens dump_nvlist(rec, 4); 49374445fffbSMatthew Ahrens } 4938ecd6cf80Smarks 4939ecd6cf80Smarks if (!cb->longfmt) { 4940ecd6cf80Smarks (void) printf("\n"); 4941ecd6cf80Smarks continue; 494206eeb2adSek110237 } 4943ecd6cf80Smarks (void) printf(" ["); 49444445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 49454445fffbSMatthew Ahrens uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 49464445fffbSMatthew Ahrens struct passwd *pwd = getpwuid(who); 49474445fffbSMatthew Ahrens (void) printf("user %d ", (int)who); 49484445fffbSMatthew Ahrens if (pwd != NULL) 49494445fffbSMatthew Ahrens (void) printf("(%s) ", pwd->pw_name); 4950ecd6cf80Smarks } 49514445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 49524445fffbSMatthew Ahrens (void) printf("on %s", 49534445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 4954ecd6cf80Smarks } 49554445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 49564445fffbSMatthew Ahrens (void) printf(":%s", 49574445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 4958ecd6cf80Smarks } 4959ecd6cf80Smarks (void) printf("]"); 4960ecd6cf80Smarks (void) printf("\n"); 496106eeb2adSek110237 } 496206eeb2adSek110237 (void) printf("\n"); 496306eeb2adSek110237 nvlist_free(nvhis); 496406eeb2adSek110237 496506eeb2adSek110237 return (ret); 496606eeb2adSek110237 } 496706eeb2adSek110237 496806eeb2adSek110237 /* 496906eeb2adSek110237 * zpool history <pool> 497006eeb2adSek110237 * 497106eeb2adSek110237 * Displays the history of commands that modified pools. 497206eeb2adSek110237 */ 497306eeb2adSek110237 int 497406eeb2adSek110237 zpool_do_history(int argc, char **argv) 497506eeb2adSek110237 { 4976ecd6cf80Smarks hist_cbdata_t cbdata = { 0 }; 497706eeb2adSek110237 int ret; 4978ecd6cf80Smarks int c; 497906eeb2adSek110237 4980ecd6cf80Smarks cbdata.first = B_TRUE; 4981ecd6cf80Smarks /* check options */ 4982ecd6cf80Smarks while ((c = getopt(argc, argv, "li")) != -1) { 4983ecd6cf80Smarks switch (c) { 4984ecd6cf80Smarks case 'l': 49854445fffbSMatthew Ahrens cbdata.longfmt = B_TRUE; 4986ecd6cf80Smarks break; 4987ecd6cf80Smarks case 'i': 49884445fffbSMatthew Ahrens cbdata.internal = B_TRUE; 4989ecd6cf80Smarks break; 4990ecd6cf80Smarks case '?': 4991ecd6cf80Smarks (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4992ecd6cf80Smarks optopt); 4993ecd6cf80Smarks usage(B_FALSE); 4994ecd6cf80Smarks } 4995ecd6cf80Smarks } 499606eeb2adSek110237 argc -= optind; 499706eeb2adSek110237 argv += optind; 499806eeb2adSek110237 4999b1b8ab34Slling ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one, 5000ecd6cf80Smarks &cbdata); 500106eeb2adSek110237 5002ecd6cf80Smarks if (argc == 0 && cbdata.first == B_TRUE) { 500306eeb2adSek110237 (void) printf(gettext("no pools available\n")); 500406eeb2adSek110237 return (0); 5005eaca9bbdSeschrock } 5006eaca9bbdSeschrock 5007eaca9bbdSeschrock return (ret); 5008eaca9bbdSeschrock } 5009eaca9bbdSeschrock 5010b1b8ab34Slling static int 5011b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data) 5012b1b8ab34Slling { 5013990b4856Slling zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 5014b1b8ab34Slling char value[MAXNAMELEN]; 5015990b4856Slling zprop_source_t srctype; 5016990b4856Slling zprop_list_t *pl; 5017b1b8ab34Slling 5018b1b8ab34Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 5019b1b8ab34Slling 5020b1b8ab34Slling /* 5021990b4856Slling * Skip the special fake placeholder. This will also skip 5022990b4856Slling * over the name property when 'all' is specified. 5023b1b8ab34Slling */ 5024990b4856Slling if (pl->pl_prop == ZPOOL_PROP_NAME && 5025b1b8ab34Slling pl == cbp->cb_proplist) 5026b1b8ab34Slling continue; 5027b1b8ab34Slling 5028ad135b5dSChristopher Siden if (pl->pl_prop == ZPROP_INVAL && 5029ad135b5dSChristopher Siden (zpool_prop_feature(pl->pl_user_prop) || 5030ad135b5dSChristopher Siden zpool_prop_unsupported(pl->pl_user_prop))) { 5031ad135b5dSChristopher Siden srctype = ZPROP_SRC_LOCAL; 5032ad135b5dSChristopher Siden 5033ad135b5dSChristopher Siden if (zpool_prop_get_feature(zhp, pl->pl_user_prop, 5034ad135b5dSChristopher Siden value, sizeof (value)) == 0) { 5035ad135b5dSChristopher Siden zprop_print_one_property(zpool_get_name(zhp), 5036ad135b5dSChristopher Siden cbp, pl->pl_user_prop, value, srctype, 5037ad135b5dSChristopher Siden NULL, NULL); 5038ad135b5dSChristopher Siden } 5039ad135b5dSChristopher Siden } else { 5040ad135b5dSChristopher Siden if (zpool_get_prop(zhp, pl->pl_prop, value, 5041c58b3526SAdam Stevko sizeof (value), &srctype, cbp->cb_literal) != 0) 5042b1b8ab34Slling continue; 5043b1b8ab34Slling 5044990b4856Slling zprop_print_one_property(zpool_get_name(zhp), cbp, 5045ad135b5dSChristopher Siden zpool_prop_to_name(pl->pl_prop), value, srctype, 5046ad135b5dSChristopher Siden NULL, NULL); 5047ad135b5dSChristopher Siden } 5048b1b8ab34Slling } 5049b1b8ab34Slling return (0); 5050b1b8ab34Slling } 5051b1b8ab34Slling 5052c58b3526SAdam Stevko /* 5053c58b3526SAdam Stevko * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... 5054c58b3526SAdam Stevko * 5055c58b3526SAdam Stevko * -H Scripted mode. Don't display headers, and separate properties 5056c58b3526SAdam Stevko * by a single tab. 5057c58b3526SAdam Stevko * -o List of columns to display. Defaults to 5058c58b3526SAdam Stevko * "name,property,value,source". 5059c58b3526SAdam Stevko * -p Diplay values in parsable (exact) format. 5060c58b3526SAdam Stevko * 5061c58b3526SAdam Stevko * Get properties of pools in the system. Output space statistics 5062c58b3526SAdam Stevko * for each one as well as other attributes. 5063c58b3526SAdam Stevko */ 5064b1b8ab34Slling int 5065b1b8ab34Slling zpool_do_get(int argc, char **argv) 5066b1b8ab34Slling { 5067990b4856Slling zprop_get_cbdata_t cb = { 0 }; 5068990b4856Slling zprop_list_t fake_name = { 0 }; 5069b1b8ab34Slling int ret; 5070c58b3526SAdam Stevko int c, i; 5071c58b3526SAdam Stevko char *value; 5072b1b8ab34Slling 5073b1b8ab34Slling cb.cb_first = B_TRUE; 5074c58b3526SAdam Stevko 5075c58b3526SAdam Stevko /* 5076c58b3526SAdam Stevko * Set up default columns and sources. 5077c58b3526SAdam Stevko */ 5078990b4856Slling cb.cb_sources = ZPROP_SRC_ALL; 5079b1b8ab34Slling cb.cb_columns[0] = GET_COL_NAME; 5080b1b8ab34Slling cb.cb_columns[1] = GET_COL_PROPERTY; 5081b1b8ab34Slling cb.cb_columns[2] = GET_COL_VALUE; 5082b1b8ab34Slling cb.cb_columns[3] = GET_COL_SOURCE; 5083990b4856Slling cb.cb_type = ZFS_TYPE_POOL; 5084b1b8ab34Slling 5085c58b3526SAdam Stevko /* check options */ 5086c58b3526SAdam Stevko while ((c = getopt(argc, argv, ":Hpo:")) != -1) { 5087c58b3526SAdam Stevko switch (c) { 5088c58b3526SAdam Stevko case 'p': 5089c58b3526SAdam Stevko cb.cb_literal = B_TRUE; 5090c58b3526SAdam Stevko break; 5091c58b3526SAdam Stevko case 'H': 5092c58b3526SAdam Stevko cb.cb_scripted = B_TRUE; 5093c58b3526SAdam Stevko break; 5094c58b3526SAdam Stevko case 'o': 5095c58b3526SAdam Stevko bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 5096c58b3526SAdam Stevko i = 0; 5097c58b3526SAdam Stevko while (*optarg != '\0') { 5098c58b3526SAdam Stevko static char *col_subopts[] = 5099c58b3526SAdam Stevko { "name", "property", "value", "source", 5100c58b3526SAdam Stevko "all", NULL }; 5101c58b3526SAdam Stevko 5102c58b3526SAdam Stevko if (i == ZFS_GET_NCOLS) { 5103c58b3526SAdam Stevko (void) fprintf(stderr, gettext("too " 5104c58b3526SAdam Stevko "many fields given to -o " 5105c58b3526SAdam Stevko "option\n")); 5106c58b3526SAdam Stevko usage(B_FALSE); 5107c58b3526SAdam Stevko } 5108c58b3526SAdam Stevko 5109c58b3526SAdam Stevko switch (getsubopt(&optarg, col_subopts, 5110c58b3526SAdam Stevko &value)) { 5111c58b3526SAdam Stevko case 0: 5112c58b3526SAdam Stevko cb.cb_columns[i++] = GET_COL_NAME; 5113c58b3526SAdam Stevko break; 5114c58b3526SAdam Stevko case 1: 5115c58b3526SAdam Stevko cb.cb_columns[i++] = GET_COL_PROPERTY; 5116c58b3526SAdam Stevko break; 5117c58b3526SAdam Stevko case 2: 5118c58b3526SAdam Stevko cb.cb_columns[i++] = GET_COL_VALUE; 5119c58b3526SAdam Stevko break; 5120c58b3526SAdam Stevko case 3: 5121c58b3526SAdam Stevko cb.cb_columns[i++] = GET_COL_SOURCE; 5122c58b3526SAdam Stevko break; 5123c58b3526SAdam Stevko case 4: 5124c58b3526SAdam Stevko if (i > 0) { 5125c58b3526SAdam Stevko (void) fprintf(stderr, 5126c58b3526SAdam Stevko gettext("\"all\" conflicts " 5127c58b3526SAdam Stevko "with specific fields " 5128c58b3526SAdam Stevko "given to -o option\n")); 5129c58b3526SAdam Stevko usage(B_FALSE); 5130c58b3526SAdam Stevko } 5131c58b3526SAdam Stevko cb.cb_columns[0] = GET_COL_NAME; 5132c58b3526SAdam Stevko cb.cb_columns[1] = GET_COL_PROPERTY; 5133c58b3526SAdam Stevko cb.cb_columns[2] = GET_COL_VALUE; 5134c58b3526SAdam Stevko cb.cb_columns[3] = GET_COL_SOURCE; 5135c58b3526SAdam Stevko i = ZFS_GET_NCOLS; 5136c58b3526SAdam Stevko break; 5137c58b3526SAdam Stevko default: 5138c58b3526SAdam Stevko (void) fprintf(stderr, 5139c58b3526SAdam Stevko gettext("invalid column name " 5140c58b3526SAdam Stevko "'%s'\n"), value); 5141c58b3526SAdam Stevko usage(B_FALSE); 5142c58b3526SAdam Stevko } 5143c58b3526SAdam Stevko } 5144c58b3526SAdam Stevko break; 5145c58b3526SAdam Stevko case '?': 5146c58b3526SAdam Stevko (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5147c58b3526SAdam Stevko optopt); 5148c58b3526SAdam Stevko usage(B_FALSE); 5149c58b3526SAdam Stevko } 5150c58b3526SAdam Stevko } 5151c58b3526SAdam Stevko 5152c58b3526SAdam Stevko argc -= optind; 5153c58b3526SAdam Stevko argv += optind; 5154c58b3526SAdam Stevko 5155c58b3526SAdam Stevko if (argc < 1) { 5156c58b3526SAdam Stevko (void) fprintf(stderr, gettext("missing property " 5157c58b3526SAdam Stevko "argument\n")); 5158c58b3526SAdam Stevko usage(B_FALSE); 5159c58b3526SAdam Stevko } 5160c58b3526SAdam Stevko 5161c58b3526SAdam Stevko if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, 5162990b4856Slling ZFS_TYPE_POOL) != 0) 5163b1b8ab34Slling usage(B_FALSE); 5164b1b8ab34Slling 5165c58b3526SAdam Stevko argc--; 5166c58b3526SAdam Stevko argv++; 5167c58b3526SAdam Stevko 5168b1b8ab34Slling if (cb.cb_proplist != NULL) { 5169990b4856Slling fake_name.pl_prop = ZPOOL_PROP_NAME; 5170b1b8ab34Slling fake_name.pl_width = strlen(gettext("NAME")); 5171b1b8ab34Slling fake_name.pl_next = cb.cb_proplist; 5172b1b8ab34Slling cb.cb_proplist = &fake_name; 5173b1b8ab34Slling } 5174b1b8ab34Slling 5175c58b3526SAdam Stevko ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, 5176b1b8ab34Slling get_callback, &cb); 5177b1b8ab34Slling 5178b1b8ab34Slling if (cb.cb_proplist == &fake_name) 5179990b4856Slling zprop_free_list(fake_name.pl_next); 5180b1b8ab34Slling else 5181990b4856Slling zprop_free_list(cb.cb_proplist); 5182b1b8ab34Slling 5183b1b8ab34Slling return (ret); 5184b1b8ab34Slling } 5185b1b8ab34Slling 5186b1b8ab34Slling typedef struct set_cbdata { 5187b1b8ab34Slling char *cb_propname; 5188b1b8ab34Slling char *cb_value; 5189b1b8ab34Slling boolean_t cb_any_successful; 5190b1b8ab34Slling } set_cbdata_t; 5191b1b8ab34Slling 5192b1b8ab34Slling int 5193b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data) 5194b1b8ab34Slling { 5195b1b8ab34Slling int error; 5196b1b8ab34Slling set_cbdata_t *cb = (set_cbdata_t *)data; 5197b1b8ab34Slling 5198b1b8ab34Slling error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 5199b1b8ab34Slling 5200b1b8ab34Slling if (!error) 5201b1b8ab34Slling cb->cb_any_successful = B_TRUE; 5202b1b8ab34Slling 5203b1b8ab34Slling return (error); 5204b1b8ab34Slling } 5205b1b8ab34Slling 5206b1b8ab34Slling int 5207b1b8ab34Slling zpool_do_set(int argc, char **argv) 5208b1b8ab34Slling { 5209b1b8ab34Slling set_cbdata_t cb = { 0 }; 5210b1b8ab34Slling int error; 5211b1b8ab34Slling 5212b1b8ab34Slling if (argc > 1 && argv[1][0] == '-') { 5213b1b8ab34Slling (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5214b1b8ab34Slling argv[1][1]); 5215b1b8ab34Slling usage(B_FALSE); 5216b1b8ab34Slling } 5217b1b8ab34Slling 5218b1b8ab34Slling if (argc < 2) { 5219b1b8ab34Slling (void) fprintf(stderr, gettext("missing property=value " 5220b1b8ab34Slling "argument\n")); 5221b1b8ab34Slling usage(B_FALSE); 5222b1b8ab34Slling } 5223b1b8ab34Slling 5224b1b8ab34Slling if (argc < 3) { 5225b1b8ab34Slling (void) fprintf(stderr, gettext("missing pool name\n")); 5226b1b8ab34Slling usage(B_FALSE); 5227b1b8ab34Slling } 5228b1b8ab34Slling 5229b1b8ab34Slling if (argc > 3) { 5230b1b8ab34Slling (void) fprintf(stderr, gettext("too many pool names\n")); 5231b1b8ab34Slling usage(B_FALSE); 5232b1b8ab34Slling } 5233b1b8ab34Slling 5234b1b8ab34Slling cb.cb_propname = argv[1]; 5235b1b8ab34Slling cb.cb_value = strchr(cb.cb_propname, '='); 5236b1b8ab34Slling if (cb.cb_value == NULL) { 5237b1b8ab34Slling (void) fprintf(stderr, gettext("missing value in " 5238b1b8ab34Slling "property=value argument\n")); 5239b1b8ab34Slling usage(B_FALSE); 5240b1b8ab34Slling } 5241b1b8ab34Slling 5242b1b8ab34Slling *(cb.cb_value) = '\0'; 5243b1b8ab34Slling cb.cb_value++; 5244b1b8ab34Slling 5245b1b8ab34Slling error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, 5246b1b8ab34Slling set_callback, &cb); 5247b1b8ab34Slling 5248b1b8ab34Slling return (error); 5249b1b8ab34Slling } 5250b1b8ab34Slling 5251b1b8ab34Slling static int 5252b1b8ab34Slling find_command_idx(char *command, int *idx) 5253b1b8ab34Slling { 5254b1b8ab34Slling int i; 5255b1b8ab34Slling 5256b1b8ab34Slling for (i = 0; i < NCOMMAND; i++) { 5257b1b8ab34Slling if (command_table[i].name == NULL) 5258b1b8ab34Slling continue; 5259b1b8ab34Slling 5260b1b8ab34Slling if (strcmp(command, command_table[i].name) == 0) { 5261b1b8ab34Slling *idx = i; 5262b1b8ab34Slling return (0); 5263b1b8ab34Slling } 5264b1b8ab34Slling } 5265b1b8ab34Slling return (1); 5266b1b8ab34Slling } 5267b1b8ab34Slling 5268fa9e4066Sahrens int 5269fa9e4066Sahrens main(int argc, char **argv) 5270fa9e4066Sahrens { 5271fa9e4066Sahrens int ret; 5272fa9e4066Sahrens int i; 5273fa9e4066Sahrens char *cmdname; 5274fa9e4066Sahrens 5275fa9e4066Sahrens (void) setlocale(LC_ALL, ""); 5276fa9e4066Sahrens (void) textdomain(TEXT_DOMAIN); 5277fa9e4066Sahrens 527899653d4eSeschrock if ((g_zfs = libzfs_init()) == NULL) { 527999653d4eSeschrock (void) fprintf(stderr, gettext("internal error: failed to " 5280203a47d8Snd150628 "initialize ZFS library\n")); 528199653d4eSeschrock return (1); 528299653d4eSeschrock } 528399653d4eSeschrock 528499653d4eSeschrock libzfs_print_on_error(g_zfs, B_TRUE); 528599653d4eSeschrock 5286fa9e4066Sahrens opterr = 0; 5287fa9e4066Sahrens 5288fa9e4066Sahrens /* 5289fa9e4066Sahrens * Make sure the user has specified some command. 5290fa9e4066Sahrens */ 5291fa9e4066Sahrens if (argc < 2) { 5292fa9e4066Sahrens (void) fprintf(stderr, gettext("missing command\n")); 529399653d4eSeschrock usage(B_FALSE); 5294fa9e4066Sahrens } 5295fa9e4066Sahrens 5296fa9e4066Sahrens cmdname = argv[1]; 5297fa9e4066Sahrens 5298fa9e4066Sahrens /* 5299fa9e4066Sahrens * Special case '-?' 5300fa9e4066Sahrens */ 5301fa9e4066Sahrens if (strcmp(cmdname, "-?") == 0) 530299653d4eSeschrock usage(B_TRUE); 5303fa9e4066Sahrens 53044445fffbSMatthew Ahrens zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 53052a6b87f0Sek110237 5306fa9e4066Sahrens /* 5307fa9e4066Sahrens * Run the appropriate command. 5308fa9e4066Sahrens */ 5309b1b8ab34Slling if (find_command_idx(cmdname, &i) == 0) { 5310fa9e4066Sahrens current_command = &command_table[i]; 5311fa9e4066Sahrens ret = command_table[i].func(argc - 1, argv + 1); 531291ebeef5Sahrens } else if (strchr(cmdname, '=')) { 531391ebeef5Sahrens verify(find_command_idx("set", &i) == 0); 531491ebeef5Sahrens current_command = &command_table[i]; 531591ebeef5Sahrens ret = command_table[i].func(argc, argv); 531691ebeef5Sahrens } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 5317fa9e4066Sahrens /* 531891ebeef5Sahrens * 'freeze' is a vile debugging abomination, so we treat 531991ebeef5Sahrens * it as such. 5320fa9e4066Sahrens */ 5321ea8dc4b6Seschrock char buf[16384]; 5322ea8dc4b6Seschrock int fd = open(ZFS_DEV, O_RDWR); 5323fa9e4066Sahrens (void) strcpy((void *)buf, argv[2]); 5324fa9e4066Sahrens return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf)); 532591ebeef5Sahrens } else { 5326fa9e4066Sahrens (void) fprintf(stderr, gettext("unrecognized " 5327fa9e4066Sahrens "command '%s'\n"), cmdname); 532899653d4eSeschrock usage(B_FALSE); 5329fa9e4066Sahrens } 5330fa9e4066Sahrens 53314445fffbSMatthew Ahrens if (ret == 0 && log_history) 53324445fffbSMatthew Ahrens (void) zpool_log_history(g_zfs, history_str); 53334445fffbSMatthew Ahrens 533499653d4eSeschrock libzfs_fini(g_zfs); 533599653d4eSeschrock 5336fa9e4066Sahrens /* 5337fa9e4066Sahrens * The 'ZFS_ABORT' environment variable causes us to dump core on exit 5338fa9e4066Sahrens * for the purposes of running ::findleaks. 5339fa9e4066Sahrens */ 5340fa9e4066Sahrens if (getenv("ZFS_ABORT") != NULL) { 5341fa9e4066Sahrens (void) printf("dumping core by request\n"); 5342fa9e4066Sahrens abort(); 5343fa9e4066Sahrens } 5344fa9e4066Sahrens 5345fa9e4066Sahrens return (ret); 5346fa9e4066Sahrens } 5347