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. 254263d13fSGeorge Wilson * Copyright (c) 2012 by Delphix. All rights reserved. 26e1d5e507SFrederik Wessels * Copyright (c) 2012 by Frederik Wessels. All rights reserved. 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29fa9e4066Sahrens #include <assert.h> 30fa9e4066Sahrens #include <ctype.h> 31fa9e4066Sahrens #include <dirent.h> 32fa9e4066Sahrens #include <errno.h> 33fa9e4066Sahrens #include <fcntl.h> 34fa9e4066Sahrens #include <libgen.h> 35fa9e4066Sahrens #include <libintl.h> 36fa9e4066Sahrens #include <libuutil.h> 37fa9e4066Sahrens #include <locale.h> 38fa9e4066Sahrens #include <stdio.h> 39fa9e4066Sahrens #include <stdlib.h> 40fa9e4066Sahrens #include <string.h> 41fa9e4066Sahrens #include <strings.h> 42fa9e4066Sahrens #include <unistd.h> 43fa9e4066Sahrens #include <priv.h> 44ecd6cf80Smarks #include <pwd.h> 45ecd6cf80Smarks #include <zone.h> 464263d13fSGeorge Wilson #include <zfs_prop.h> 47b1b8ab34Slling #include <sys/fs/zfs.h> 48fa9e4066Sahrens #include <sys/stat.h> 49fa9e4066Sahrens 50fa9e4066Sahrens #include <libzfs.h> 51fa9e4066Sahrens 52fa9e4066Sahrens #include "zpool_util.h" 53b7b97454Sperrin #include "zfs_comutil.h" 54ad135b5dSChristopher Siden #include "zfeature_common.h" 55fa9e4066Sahrens 5626fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h" 5726fd7700SKrishnendu Sadhukhan - Sun Microsystems 58fa9e4066Sahrens static int zpool_do_create(int, char **); 59fa9e4066Sahrens static int zpool_do_destroy(int, char **); 60fa9e4066Sahrens 61fa9e4066Sahrens static int zpool_do_add(int, char **); 6299653d4eSeschrock static int zpool_do_remove(int, char **); 63fa9e4066Sahrens 64fa9e4066Sahrens static int zpool_do_list(int, char **); 65fa9e4066Sahrens static int zpool_do_iostat(int, char **); 66fa9e4066Sahrens static int zpool_do_status(int, char **); 67fa9e4066Sahrens 68fa9e4066Sahrens static int zpool_do_online(int, char **); 69fa9e4066Sahrens static int zpool_do_offline(int, char **); 70ea8dc4b6Seschrock static int zpool_do_clear(int, char **); 714263d13fSGeorge Wilson static int zpool_do_reopen(int, char **); 72fa9e4066Sahrens 73e9103aaeSGarrett D'Amore static int zpool_do_reguid(int, char **); 74e9103aaeSGarrett D'Amore 75fa9e4066Sahrens static int zpool_do_attach(int, char **); 76fa9e4066Sahrens static int zpool_do_detach(int, char **); 77fa9e4066Sahrens static int zpool_do_replace(int, char **); 781195e687SMark J Musante static int zpool_do_split(int, char **); 79fa9e4066Sahrens 80fa9e4066Sahrens static int zpool_do_scrub(int, char **); 81fa9e4066Sahrens 82fa9e4066Sahrens static int zpool_do_import(int, char **); 83fa9e4066Sahrens static int zpool_do_export(int, char **); 84fa9e4066Sahrens 85eaca9bbdSeschrock static int zpool_do_upgrade(int, char **); 86eaca9bbdSeschrock 8706eeb2adSek110237 static int zpool_do_history(int, char **); 8806eeb2adSek110237 89b1b8ab34Slling static int zpool_do_get(int, char **); 90b1b8ab34Slling static int zpool_do_set(int, char **); 91b1b8ab34Slling 92fa9e4066Sahrens /* 93fa9e4066Sahrens * These libumem hooks provide a reasonable set of defaults for the allocator's 94fa9e4066Sahrens * debugging facilities. 95fa9e4066Sahrens */ 9629ab75c9Srm160521 9729ab75c9Srm160521 #ifdef DEBUG 98fa9e4066Sahrens const char * 9999653d4eSeschrock _umem_debug_init(void) 100fa9e4066Sahrens { 101fa9e4066Sahrens return ("default,verbose"); /* $UMEM_DEBUG setting */ 102fa9e4066Sahrens } 103fa9e4066Sahrens 104fa9e4066Sahrens const char * 105fa9e4066Sahrens _umem_logging_init(void) 106fa9e4066Sahrens { 107fa9e4066Sahrens return ("fail,contents"); /* $UMEM_LOGGING setting */ 108fa9e4066Sahrens } 10929ab75c9Srm160521 #endif 110fa9e4066Sahrens 11165cd9f28Seschrock typedef enum { 11265cd9f28Seschrock HELP_ADD, 11365cd9f28Seschrock HELP_ATTACH, 114ea8dc4b6Seschrock HELP_CLEAR, 11565cd9f28Seschrock HELP_CREATE, 11665cd9f28Seschrock HELP_DESTROY, 11765cd9f28Seschrock HELP_DETACH, 11865cd9f28Seschrock HELP_EXPORT, 11906eeb2adSek110237 HELP_HISTORY, 12065cd9f28Seschrock HELP_IMPORT, 12165cd9f28Seschrock HELP_IOSTAT, 12265cd9f28Seschrock HELP_LIST, 12365cd9f28Seschrock HELP_OFFLINE, 12465cd9f28Seschrock HELP_ONLINE, 12565cd9f28Seschrock HELP_REPLACE, 12699653d4eSeschrock HELP_REMOVE, 12765cd9f28Seschrock HELP_SCRUB, 128eaca9bbdSeschrock HELP_STATUS, 129b1b8ab34Slling HELP_UPGRADE, 130b1b8ab34Slling HELP_GET, 1311195e687SMark J Musante HELP_SET, 132e9103aaeSGarrett D'Amore HELP_SPLIT, 1334263d13fSGeorge Wilson HELP_REGUID, 1344263d13fSGeorge Wilson HELP_REOPEN 13565cd9f28Seschrock } zpool_help_t; 13665cd9f28Seschrock 13765cd9f28Seschrock 138fa9e4066Sahrens typedef struct zpool_command { 139fa9e4066Sahrens const char *name; 140fa9e4066Sahrens int (*func)(int, char **); 14165cd9f28Seschrock zpool_help_t usage; 142fa9e4066Sahrens } zpool_command_t; 143fa9e4066Sahrens 144fa9e4066Sahrens /* 145fa9e4066Sahrens * Master command table. Each ZFS command has a name, associated function, and 146ea8dc4b6Seschrock * usage message. The usage messages need to be internationalized, so we have 147ea8dc4b6Seschrock * to have a function to return the usage message based on a command index. 14865cd9f28Seschrock * 14965cd9f28Seschrock * These commands are organized according to how they are displayed in the usage 15065cd9f28Seschrock * message. An empty command (one with a NULL name) indicates an empty line in 15165cd9f28Seschrock * the generic usage message. 152fa9e4066Sahrens */ 153fa9e4066Sahrens static zpool_command_t command_table[] = { 15465cd9f28Seschrock { "create", zpool_do_create, HELP_CREATE }, 15565cd9f28Seschrock { "destroy", zpool_do_destroy, HELP_DESTROY }, 156fa9e4066Sahrens { NULL }, 15765cd9f28Seschrock { "add", zpool_do_add, HELP_ADD }, 15899653d4eSeschrock { "remove", zpool_do_remove, HELP_REMOVE }, 159fa9e4066Sahrens { NULL }, 16065cd9f28Seschrock { "list", zpool_do_list, HELP_LIST }, 16165cd9f28Seschrock { "iostat", zpool_do_iostat, HELP_IOSTAT }, 16265cd9f28Seschrock { "status", zpool_do_status, HELP_STATUS }, 163fa9e4066Sahrens { NULL }, 16465cd9f28Seschrock { "online", zpool_do_online, HELP_ONLINE }, 16565cd9f28Seschrock { "offline", zpool_do_offline, HELP_OFFLINE }, 166ea8dc4b6Seschrock { "clear", zpool_do_clear, HELP_CLEAR }, 1674263d13fSGeorge Wilson { "reopen", zpool_do_reopen, HELP_REOPEN }, 168fa9e4066Sahrens { NULL }, 16965cd9f28Seschrock { "attach", zpool_do_attach, HELP_ATTACH }, 17065cd9f28Seschrock { "detach", zpool_do_detach, HELP_DETACH }, 17165cd9f28Seschrock { "replace", zpool_do_replace, HELP_REPLACE }, 1721195e687SMark J Musante { "split", zpool_do_split, HELP_SPLIT }, 173fa9e4066Sahrens { NULL }, 17465cd9f28Seschrock { "scrub", zpool_do_scrub, HELP_SCRUB }, 175fa9e4066Sahrens { NULL }, 17665cd9f28Seschrock { "import", zpool_do_import, HELP_IMPORT }, 17765cd9f28Seschrock { "export", zpool_do_export, HELP_EXPORT }, 17806eeb2adSek110237 { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, 179e9103aaeSGarrett D'Amore { "reguid", zpool_do_reguid, HELP_REGUID }, 18006eeb2adSek110237 { NULL }, 181b1b8ab34Slling { "history", zpool_do_history, HELP_HISTORY }, 182b1b8ab34Slling { "get", zpool_do_get, HELP_GET }, 183b1b8ab34Slling { "set", zpool_do_set, HELP_SET }, 184fa9e4066Sahrens }; 185fa9e4066Sahrens 186fa9e4066Sahrens #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 187fa9e4066Sahrens 1884445fffbSMatthew Ahrens static zpool_command_t *current_command; 1892a6b87f0Sek110237 static char history_str[HIS_MAX_RECORD_LEN]; 1904445fffbSMatthew Ahrens static boolean_t log_history = B_TRUE; 19126fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE; 19226fd7700SKrishnendu Sadhukhan - Sun Microsystems 19365cd9f28Seschrock static const char * 19465cd9f28Seschrock get_usage(zpool_help_t idx) { 19565cd9f28Seschrock switch (idx) { 19665cd9f28Seschrock case HELP_ADD: 19765cd9f28Seschrock return (gettext("\tadd [-fn] <pool> <vdev> ...\n")); 19865cd9f28Seschrock case HELP_ATTACH: 19965cd9f28Seschrock return (gettext("\tattach [-f] <pool> <device> " 200e45ce728Sahrens "<new-device>\n")); 201ea8dc4b6Seschrock case HELP_CLEAR: 202468c413aSTim Haley return (gettext("\tclear [-nF] <pool> [device]\n")); 20365cd9f28Seschrock case HELP_CREATE: 204ad135b5dSChristopher Siden return (gettext("\tcreate [-fnd] [-o property=value] ... \n" 2050a48a24eStimh "\t [-O file-system-property=value] ... \n" 206990b4856Slling "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); 20765cd9f28Seschrock case HELP_DESTROY: 20865cd9f28Seschrock return (gettext("\tdestroy [-f] <pool>\n")); 20965cd9f28Seschrock case HELP_DETACH: 21065cd9f28Seschrock return (gettext("\tdetach <pool> <device>\n")); 21165cd9f28Seschrock case HELP_EXPORT: 21265cd9f28Seschrock return (gettext("\texport [-f] <pool> ...\n")); 21306eeb2adSek110237 case HELP_HISTORY: 214ecd6cf80Smarks return (gettext("\thistory [-il] [<pool>] ...\n")); 21565cd9f28Seschrock case HELP_IMPORT: 2164c58d714Sdarrenm return (gettext("\timport [-d dir] [-D]\n" 217f9af39baSGeorge Wilson "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n" 2182f8aaab3Seschrock "\timport [-o mntopts] [-o property=value] ... \n" 219f9af39baSGeorge Wilson "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " 220f9af39baSGeorge Wilson "[-R root] [-F [-n]] -a\n" 2212f8aaab3Seschrock "\timport [-o mntopts] [-o property=value] ... \n" 222f9af39baSGeorge Wilson "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " 223f9af39baSGeorge Wilson "[-R root] [-F [-n]]\n" 224f9af39baSGeorge Wilson "\t <pool | id> [newpool]\n")); 22565cd9f28Seschrock case HELP_IOSTAT: 22626fd7700SKrishnendu Sadhukhan - Sun Microsystems return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval " 22765cd9f28Seschrock "[count]]\n")); 22865cd9f28Seschrock case HELP_LIST: 229990b4856Slling return (gettext("\tlist [-H] [-o property[,...]] " 2303f9d6ad7SLin Ling "[-T d|u] [pool] ... [interval [count]]\n")); 23165cd9f28Seschrock case HELP_OFFLINE: 232441d80aaSlling return (gettext("\toffline [-t] <pool> <device> ...\n")); 23365cd9f28Seschrock case HELP_ONLINE: 234441d80aaSlling return (gettext("\tonline <pool> <device> ...\n")); 23565cd9f28Seschrock case HELP_REPLACE: 23665cd9f28Seschrock return (gettext("\treplace [-f] <pool> <device> " 237e45ce728Sahrens "[new-device]\n")); 23899653d4eSeschrock case HELP_REMOVE: 239fa94a07fSbrendan return (gettext("\tremove <pool> <device> ...\n")); 2404263d13fSGeorge Wilson case HELP_REOPEN: 241*31d7e8faSGeorge Wilson return (gettext("\treopen <pool>\n")); 24265cd9f28Seschrock case HELP_SCRUB: 24365cd9f28Seschrock return (gettext("\tscrub [-s] <pool> ...\n")); 24465cd9f28Seschrock case HELP_STATUS: 2453f9d6ad7SLin Ling return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval " 2463f9d6ad7SLin Ling "[count]]\n")); 247eaca9bbdSeschrock case HELP_UPGRADE: 248eaca9bbdSeschrock return (gettext("\tupgrade\n" 249eaca9bbdSeschrock "\tupgrade -v\n" 250990b4856Slling "\tupgrade [-V version] <-a | pool ...>\n")); 251b1b8ab34Slling case HELP_GET: 252e45ce728Sahrens return (gettext("\tget <\"all\" | property[,...]> " 253b1b8ab34Slling "<pool> ...\n")); 254b1b8ab34Slling case HELP_SET: 255b1b8ab34Slling return (gettext("\tset <property=value> <pool> \n")); 2561195e687SMark J Musante case HELP_SPLIT: 2571195e687SMark J Musante return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n" 2581195e687SMark J Musante "\t [-o property=value] <pool> <newpool> " 2591195e687SMark J Musante "[<device> ...]\n")); 260e9103aaeSGarrett D'Amore case HELP_REGUID: 261e9103aaeSGarrett D'Amore return (gettext("\treguid <pool>\n")); 26265cd9f28Seschrock } 26365cd9f28Seschrock 26465cd9f28Seschrock abort(); 26565cd9f28Seschrock /* NOTREACHED */ 26665cd9f28Seschrock } 26765cd9f28Seschrock 268fa9e4066Sahrens 269fa9e4066Sahrens /* 270b1b8ab34Slling * Callback routine that will print out a pool property value. 271b1b8ab34Slling */ 272990b4856Slling static int 273990b4856Slling print_prop_cb(int prop, void *cb) 274b1b8ab34Slling { 275b1b8ab34Slling FILE *fp = cb; 276b1b8ab34Slling 277b24ab676SJeff Bonwick (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop)); 278b1b8ab34Slling 279990b4856Slling if (zpool_prop_readonly(prop)) 280990b4856Slling (void) fprintf(fp, " NO "); 281990b4856Slling else 282990b4856Slling (void) fprintf(fp, " YES "); 283990b4856Slling 284b1b8ab34Slling if (zpool_prop_values(prop) == NULL) 285b1b8ab34Slling (void) fprintf(fp, "-\n"); 286b1b8ab34Slling else 287b1b8ab34Slling (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); 288b1b8ab34Slling 289990b4856Slling return (ZPROP_CONT); 290b1b8ab34Slling } 291b1b8ab34Slling 292b1b8ab34Slling /* 293fa9e4066Sahrens * Display usage message. If we're inside a command, display only the usage for 294fa9e4066Sahrens * that command. Otherwise, iterate over the entire command table and display 295fa9e4066Sahrens * a complete usage message. 296fa9e4066Sahrens */ 297fa9e4066Sahrens void 29899653d4eSeschrock usage(boolean_t requested) 299fa9e4066Sahrens { 300fa9e4066Sahrens FILE *fp = requested ? stdout : stderr; 301fa9e4066Sahrens 302fa9e4066Sahrens if (current_command == NULL) { 303fa9e4066Sahrens int i; 304fa9e4066Sahrens 305fa9e4066Sahrens (void) fprintf(fp, gettext("usage: zpool command args ...\n")); 306fa9e4066Sahrens (void) fprintf(fp, 307fa9e4066Sahrens gettext("where 'command' is one of the following:\n\n")); 308fa9e4066Sahrens 309fa9e4066Sahrens for (i = 0; i < NCOMMAND; i++) { 310fa9e4066Sahrens if (command_table[i].name == NULL) 311fa9e4066Sahrens (void) fprintf(fp, "\n"); 312fa9e4066Sahrens else 313fa9e4066Sahrens (void) fprintf(fp, "%s", 31465cd9f28Seschrock get_usage(command_table[i].usage)); 315fa9e4066Sahrens } 316fa9e4066Sahrens } else { 317fa9e4066Sahrens (void) fprintf(fp, gettext("usage:\n")); 31865cd9f28Seschrock (void) fprintf(fp, "%s", get_usage(current_command->usage)); 319fa9e4066Sahrens } 320fa9e4066Sahrens 321b1b8ab34Slling if (current_command != NULL && 322b1b8ab34Slling ((strcmp(current_command->name, "set") == 0) || 323990b4856Slling (strcmp(current_command->name, "get") == 0) || 324990b4856Slling (strcmp(current_command->name, "list") == 0))) { 325b1b8ab34Slling 326b1b8ab34Slling (void) fprintf(fp, 327b1b8ab34Slling gettext("\nthe following properties are supported:\n")); 328b1b8ab34Slling 329b24ab676SJeff Bonwick (void) fprintf(fp, "\n\t%-15s %s %s\n\n", 330990b4856Slling "PROPERTY", "EDIT", "VALUES"); 331b1b8ab34Slling 332b1b8ab34Slling /* Iterate over all properties */ 333990b4856Slling (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE, 334990b4856Slling ZFS_TYPE_POOL); 335ad135b5dSChristopher Siden 336ad135b5dSChristopher Siden (void) fprintf(fp, "\t%-15s ", "feature@..."); 337ad135b5dSChristopher Siden (void) fprintf(fp, "YES disabled | enabled | active\n"); 338ad135b5dSChristopher Siden 339ad135b5dSChristopher Siden (void) fprintf(fp, gettext("\nThe feature@ properties must be " 340ad135b5dSChristopher Siden "appended with a feature name.\nSee zpool-features(5).\n")); 341b1b8ab34Slling } 342b1b8ab34Slling 343e9dbad6fSeschrock /* 344e9dbad6fSeschrock * See comments at end of main(). 345e9dbad6fSeschrock */ 346e9dbad6fSeschrock if (getenv("ZFS_ABORT") != NULL) { 347e9dbad6fSeschrock (void) printf("dumping core by request\n"); 348e9dbad6fSeschrock abort(); 349e9dbad6fSeschrock } 350e9dbad6fSeschrock 351fa9e4066Sahrens exit(requested ? 0 : 2); 352fa9e4066Sahrens } 353fa9e4066Sahrens 354fa9e4066Sahrens void 3558654d025Sperrin print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, 3568654d025Sperrin boolean_t print_logs) 357fa9e4066Sahrens { 358fa9e4066Sahrens nvlist_t **child; 359fa9e4066Sahrens uint_t c, children; 360afefbcddSeschrock char *vname; 361fa9e4066Sahrens 362fa9e4066Sahrens if (name != NULL) 363fa9e4066Sahrens (void) printf("\t%*s%s\n", indent, "", name); 364fa9e4066Sahrens 365fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 366fa9e4066Sahrens &child, &children) != 0) 367fa9e4066Sahrens return; 368fa9e4066Sahrens 369afefbcddSeschrock for (c = 0; c < children; c++) { 3708654d025Sperrin uint64_t is_log = B_FALSE; 3718654d025Sperrin 3728654d025Sperrin (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 3738654d025Sperrin &is_log); 3748654d025Sperrin if ((is_log && !print_logs) || (!is_log && print_logs)) 3758654d025Sperrin continue; 3768654d025Sperrin 37788ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 3788654d025Sperrin print_vdev_tree(zhp, vname, child[c], indent + 2, 3798654d025Sperrin B_FALSE); 380afefbcddSeschrock free(vname); 381afefbcddSeschrock } 382fa9e4066Sahrens } 383fa9e4066Sahrens 38457221772SChristopher Siden static boolean_t 38557221772SChristopher Siden prop_list_contains_feature(nvlist_t *proplist) 38657221772SChristopher Siden { 38757221772SChristopher Siden nvpair_t *nvp; 38857221772SChristopher Siden for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; 38957221772SChristopher Siden nvp = nvlist_next_nvpair(proplist, nvp)) { 39057221772SChristopher Siden if (zpool_prop_feature(nvpair_name(nvp))) 39157221772SChristopher Siden return (B_TRUE); 39257221772SChristopher Siden } 39357221772SChristopher Siden return (B_FALSE); 39457221772SChristopher Siden } 39557221772SChristopher Siden 396fa9e4066Sahrens /* 397990b4856Slling * Add a property pair (name, string-value) into a property nvlist. 398990b4856Slling */ 399990b4856Slling static int 4000a48a24eStimh add_prop_list(const char *propname, char *propval, nvlist_t **props, 4010a48a24eStimh boolean_t poolprop) 402990b4856Slling { 4030a48a24eStimh zpool_prop_t prop = ZPROP_INVAL; 4040a48a24eStimh zfs_prop_t fprop; 405990b4856Slling nvlist_t *proplist; 4060a48a24eStimh const char *normnm; 4070a48a24eStimh char *strval; 408990b4856Slling 409990b4856Slling if (*props == NULL && 410990b4856Slling nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { 411990b4856Slling (void) fprintf(stderr, 412990b4856Slling gettext("internal error: out of memory\n")); 413990b4856Slling return (1); 414990b4856Slling } 415990b4856Slling 416990b4856Slling proplist = *props; 417990b4856Slling 4180a48a24eStimh if (poolprop) { 41957221772SChristopher Siden const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); 42057221772SChristopher Siden 421ad135b5dSChristopher Siden if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL && 422ad135b5dSChristopher Siden !zpool_prop_feature(propname)) { 423990b4856Slling (void) fprintf(stderr, gettext("property '%s' is " 424990b4856Slling "not a valid pool property\n"), propname); 425990b4856Slling return (2); 426990b4856Slling } 42757221772SChristopher Siden 42857221772SChristopher Siden /* 42957221772SChristopher Siden * feature@ properties and version should not be specified 43057221772SChristopher Siden * at the same time. 43157221772SChristopher Siden */ 43257221772SChristopher Siden if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) && 43357221772SChristopher Siden nvlist_exists(proplist, vname)) || 43457221772SChristopher Siden (prop == ZPOOL_PROP_VERSION && 43557221772SChristopher Siden prop_list_contains_feature(proplist))) { 43657221772SChristopher Siden (void) fprintf(stderr, gettext("'feature@' and " 43757221772SChristopher Siden "'version' properties cannot be specified " 43857221772SChristopher Siden "together\n")); 43957221772SChristopher Siden return (2); 44057221772SChristopher Siden } 44157221772SChristopher Siden 44257221772SChristopher Siden 443ad135b5dSChristopher Siden if (zpool_prop_feature(propname)) 444ad135b5dSChristopher Siden normnm = propname; 445ad135b5dSChristopher Siden else 4460a48a24eStimh normnm = zpool_prop_to_name(prop); 4470a48a24eStimh } else { 44814843421SMatthew Ahrens if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 4490a48a24eStimh normnm = zfs_prop_to_name(fprop); 45014843421SMatthew Ahrens } else { 45114843421SMatthew Ahrens normnm = propname; 45214843421SMatthew Ahrens } 4530a48a24eStimh } 454990b4856Slling 4550a48a24eStimh if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && 4560a48a24eStimh prop != ZPOOL_PROP_CACHEFILE) { 457990b4856Slling (void) fprintf(stderr, gettext("property '%s' " 458990b4856Slling "specified multiple times\n"), propname); 459990b4856Slling return (2); 460990b4856Slling } 461990b4856Slling 4620a48a24eStimh if (nvlist_add_string(proplist, normnm, propval) != 0) { 463990b4856Slling (void) fprintf(stderr, gettext("internal " 464990b4856Slling "error: out of memory\n")); 465990b4856Slling return (1); 466990b4856Slling } 467990b4856Slling 468990b4856Slling return (0); 469990b4856Slling } 470990b4856Slling 471990b4856Slling /* 472fa9e4066Sahrens * zpool add [-fn] <pool> <vdev> ... 473fa9e4066Sahrens * 474fa9e4066Sahrens * -f Force addition of devices, even if they appear in use 475fa9e4066Sahrens * -n Do not add the devices, but display the resulting layout if 476fa9e4066Sahrens * they were to be added. 477fa9e4066Sahrens * 478fa9e4066Sahrens * Adds the given vdevs to 'pool'. As with create, the bulk of this work is 479fa9e4066Sahrens * handled by get_vdev_spec(), which constructs the nvlist needed to pass to 480fa9e4066Sahrens * libzfs. 481fa9e4066Sahrens */ 482fa9e4066Sahrens int 483fa9e4066Sahrens zpool_do_add(int argc, char **argv) 484fa9e4066Sahrens { 48599653d4eSeschrock boolean_t force = B_FALSE; 48699653d4eSeschrock boolean_t dryrun = B_FALSE; 487fa9e4066Sahrens int c; 488fa9e4066Sahrens nvlist_t *nvroot; 489fa9e4066Sahrens char *poolname; 490fa9e4066Sahrens int ret; 491fa9e4066Sahrens zpool_handle_t *zhp; 492fa9e4066Sahrens nvlist_t *config; 493fa9e4066Sahrens 494fa9e4066Sahrens /* check options */ 495fa9e4066Sahrens while ((c = getopt(argc, argv, "fn")) != -1) { 496fa9e4066Sahrens switch (c) { 497fa9e4066Sahrens case 'f': 49899653d4eSeschrock force = B_TRUE; 499fa9e4066Sahrens break; 500fa9e4066Sahrens case 'n': 50199653d4eSeschrock dryrun = B_TRUE; 502fa9e4066Sahrens break; 503fa9e4066Sahrens case '?': 504fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 505fa9e4066Sahrens optopt); 50699653d4eSeschrock usage(B_FALSE); 507fa9e4066Sahrens } 508fa9e4066Sahrens } 509fa9e4066Sahrens 510fa9e4066Sahrens argc -= optind; 511fa9e4066Sahrens argv += optind; 512fa9e4066Sahrens 513fa9e4066Sahrens /* get pool name and check number of arguments */ 514fa9e4066Sahrens if (argc < 1) { 515fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 51699653d4eSeschrock usage(B_FALSE); 517fa9e4066Sahrens } 518fa9e4066Sahrens if (argc < 2) { 519fa9e4066Sahrens (void) fprintf(stderr, gettext("missing vdev specification\n")); 52099653d4eSeschrock usage(B_FALSE); 521fa9e4066Sahrens } 522fa9e4066Sahrens 523fa9e4066Sahrens poolname = argv[0]; 524fa9e4066Sahrens 525fa9e4066Sahrens argc--; 526fa9e4066Sahrens argv++; 527fa9e4066Sahrens 52899653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 529fa9e4066Sahrens return (1); 530fa9e4066Sahrens 531088e9d47Seschrock if ((config = zpool_get_config(zhp, NULL)) == NULL) { 532fa9e4066Sahrens (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 533fa9e4066Sahrens poolname); 534fa9e4066Sahrens zpool_close(zhp); 535fa9e4066Sahrens return (1); 536fa9e4066Sahrens } 537fa9e4066Sahrens 538fa9e4066Sahrens /* pass off to get_vdev_spec for processing */ 539705040edSEric Taylor nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun, 540705040edSEric Taylor argc, argv); 541fa9e4066Sahrens if (nvroot == NULL) { 542fa9e4066Sahrens zpool_close(zhp); 543fa9e4066Sahrens return (1); 544fa9e4066Sahrens } 545fa9e4066Sahrens 546fa9e4066Sahrens if (dryrun) { 547fa9e4066Sahrens nvlist_t *poolnvroot; 548fa9e4066Sahrens 549fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 550fa9e4066Sahrens &poolnvroot) == 0); 551fa9e4066Sahrens 552fa9e4066Sahrens (void) printf(gettext("would update '%s' to the following " 553fa9e4066Sahrens "configuration:\n"), zpool_get_name(zhp)); 554fa9e4066Sahrens 5558654d025Sperrin /* print original main pool and new tree */ 5568654d025Sperrin print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE); 5578654d025Sperrin print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE); 5588654d025Sperrin 5598654d025Sperrin /* Do the same for the logs */ 5608654d025Sperrin if (num_logs(poolnvroot) > 0) { 5618654d025Sperrin print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE); 5628654d025Sperrin print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE); 5638654d025Sperrin } else if (num_logs(nvroot) > 0) { 5648654d025Sperrin print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE); 5658654d025Sperrin } 566fa9e4066Sahrens 567fa9e4066Sahrens ret = 0; 568fa9e4066Sahrens } else { 569fa9e4066Sahrens ret = (zpool_add(zhp, nvroot) != 0); 570fa9e4066Sahrens } 571fa9e4066Sahrens 57299653d4eSeschrock nvlist_free(nvroot); 57399653d4eSeschrock zpool_close(zhp); 57499653d4eSeschrock 57599653d4eSeschrock return (ret); 57699653d4eSeschrock } 57799653d4eSeschrock 57899653d4eSeschrock /* 579fa94a07fSbrendan * zpool remove <pool> <vdev> ... 58099653d4eSeschrock * 5813f9d6ad7SLin Ling * Removes the given vdev from the pool. Currently, this supports removing 5823f9d6ad7SLin Ling * spares, cache, and log devices from the pool. 58399653d4eSeschrock */ 58499653d4eSeschrock int 58599653d4eSeschrock zpool_do_remove(int argc, char **argv) 58699653d4eSeschrock { 58799653d4eSeschrock char *poolname; 588fa94a07fSbrendan int i, ret = 0; 58999653d4eSeschrock zpool_handle_t *zhp; 59099653d4eSeschrock 59199653d4eSeschrock argc--; 59299653d4eSeschrock argv++; 59399653d4eSeschrock 59499653d4eSeschrock /* get pool name and check number of arguments */ 59599653d4eSeschrock if (argc < 1) { 59699653d4eSeschrock (void) fprintf(stderr, gettext("missing pool name argument\n")); 59799653d4eSeschrock usage(B_FALSE); 59899653d4eSeschrock } 59999653d4eSeschrock if (argc < 2) { 60099653d4eSeschrock (void) fprintf(stderr, gettext("missing device\n")); 60199653d4eSeschrock usage(B_FALSE); 60299653d4eSeschrock } 60399653d4eSeschrock 60499653d4eSeschrock poolname = argv[0]; 60599653d4eSeschrock 60699653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 60799653d4eSeschrock return (1); 60899653d4eSeschrock 609fa94a07fSbrendan for (i = 1; i < argc; i++) { 610fa94a07fSbrendan if (zpool_vdev_remove(zhp, argv[i]) != 0) 611fa94a07fSbrendan ret = 1; 612fa94a07fSbrendan } 61399653d4eSeschrock 614fa9e4066Sahrens return (ret); 615fa9e4066Sahrens } 616fa9e4066Sahrens 617fa9e4066Sahrens /* 618ad135b5dSChristopher Siden * zpool create [-fnd] [-o property=value] ... 6190a48a24eStimh * [-O file-system-property=value] ... 6200a48a24eStimh * [-R root] [-m mountpoint] <pool> <dev> ... 621fa9e4066Sahrens * 622fa9e4066Sahrens * -f Force creation, even if devices appear in use 623fa9e4066Sahrens * -n Do not create the pool, but display the resulting layout if it 624fa9e4066Sahrens * were to be created. 625fa9e4066Sahrens * -R Create a pool under an alternate root 626fa9e4066Sahrens * -m Set default mountpoint for the root dataset. By default it's 627fa9e4066Sahrens * '/<pool>' 628990b4856Slling * -o Set property=value. 629ad135b5dSChristopher Siden * -d Don't automatically enable all supported pool features 630ad135b5dSChristopher Siden * (individual features can be enabled with -o). 6310a48a24eStimh * -O Set fsproperty=value in the pool's root file system 632fa9e4066Sahrens * 633b1b8ab34Slling * Creates the named pool according to the given vdev specification. The 634fa9e4066Sahrens * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once 635fa9e4066Sahrens * we get the nvlist back from get_vdev_spec(), we either print out the contents 636fa9e4066Sahrens * (if '-n' was specified), or pass it to libzfs to do the creation. 637fa9e4066Sahrens */ 638fa9e4066Sahrens int 639fa9e4066Sahrens zpool_do_create(int argc, char **argv) 640fa9e4066Sahrens { 64199653d4eSeschrock boolean_t force = B_FALSE; 64299653d4eSeschrock boolean_t dryrun = B_FALSE; 643ad135b5dSChristopher Siden boolean_t enable_all_pool_feat = B_TRUE; 644fa9e4066Sahrens int c; 645990b4856Slling nvlist_t *nvroot = NULL; 646fa9e4066Sahrens char *poolname; 647990b4856Slling int ret = 1; 648fa9e4066Sahrens char *altroot = NULL; 649fa9e4066Sahrens char *mountpoint = NULL; 6500a48a24eStimh nvlist_t *fsprops = NULL; 651990b4856Slling nvlist_t *props = NULL; 6522f8aaab3Seschrock char *propval; 653fa9e4066Sahrens 654fa9e4066Sahrens /* check options */ 655ad135b5dSChristopher Siden while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) { 656fa9e4066Sahrens switch (c) { 657fa9e4066Sahrens case 'f': 65899653d4eSeschrock force = B_TRUE; 659fa9e4066Sahrens break; 660fa9e4066Sahrens case 'n': 66199653d4eSeschrock dryrun = B_TRUE; 662fa9e4066Sahrens break; 663ad135b5dSChristopher Siden case 'd': 664ad135b5dSChristopher Siden enable_all_pool_feat = B_FALSE; 665ad135b5dSChristopher Siden break; 666fa9e4066Sahrens case 'R': 667fa9e4066Sahrens altroot = optarg; 668990b4856Slling if (add_prop_list(zpool_prop_to_name( 6690a48a24eStimh ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 670990b4856Slling goto errout; 6712f8aaab3Seschrock if (nvlist_lookup_string(props, 6722f8aaab3Seschrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 6732f8aaab3Seschrock &propval) == 0) 6742f8aaab3Seschrock break; 675990b4856Slling if (add_prop_list(zpool_prop_to_name( 6760a48a24eStimh ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 677990b4856Slling goto errout; 678fa9e4066Sahrens break; 679fa9e4066Sahrens case 'm': 680fa9e4066Sahrens mountpoint = optarg; 681fa9e4066Sahrens break; 682990b4856Slling case 'o': 683990b4856Slling if ((propval = strchr(optarg, '=')) == NULL) { 684990b4856Slling (void) fprintf(stderr, gettext("missing " 685990b4856Slling "'=' for -o option\n")); 686990b4856Slling goto errout; 687990b4856Slling } 688990b4856Slling *propval = '\0'; 689990b4856Slling propval++; 690990b4856Slling 6910a48a24eStimh if (add_prop_list(optarg, propval, &props, B_TRUE)) 6920a48a24eStimh goto errout; 693ad135b5dSChristopher Siden 694ad135b5dSChristopher Siden /* 695ad135b5dSChristopher Siden * If the user is creating a pool that doesn't support 696ad135b5dSChristopher Siden * feature flags, don't enable any features. 697ad135b5dSChristopher Siden */ 698ad135b5dSChristopher Siden if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { 699ad135b5dSChristopher Siden char *end; 700ad135b5dSChristopher Siden u_longlong_t ver; 701ad135b5dSChristopher Siden 702ad135b5dSChristopher Siden ver = strtoull(propval, &end, 10); 703ad135b5dSChristopher Siden if (*end == '\0' && 704ad135b5dSChristopher Siden ver < SPA_VERSION_FEATURES) { 705ad135b5dSChristopher Siden enable_all_pool_feat = B_FALSE; 706ad135b5dSChristopher Siden } 707ad135b5dSChristopher Siden } 7080a48a24eStimh break; 7090a48a24eStimh case 'O': 7100a48a24eStimh if ((propval = strchr(optarg, '=')) == NULL) { 7110a48a24eStimh (void) fprintf(stderr, gettext("missing " 7120a48a24eStimh "'=' for -O option\n")); 7130a48a24eStimh goto errout; 7140a48a24eStimh } 7150a48a24eStimh *propval = '\0'; 7160a48a24eStimh propval++; 7170a48a24eStimh 7180a48a24eStimh if (add_prop_list(optarg, propval, &fsprops, B_FALSE)) 719990b4856Slling goto errout; 720990b4856Slling break; 721fa9e4066Sahrens case ':': 722fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for " 723fa9e4066Sahrens "'%c' option\n"), optopt); 724990b4856Slling goto badusage; 725fa9e4066Sahrens case '?': 726fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 727fa9e4066Sahrens optopt); 728990b4856Slling goto badusage; 729fa9e4066Sahrens } 730fa9e4066Sahrens } 731fa9e4066Sahrens 732fa9e4066Sahrens argc -= optind; 733fa9e4066Sahrens argv += optind; 734fa9e4066Sahrens 735fa9e4066Sahrens /* get pool name and check number of arguments */ 736fa9e4066Sahrens if (argc < 1) { 737fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 738990b4856Slling goto badusage; 739fa9e4066Sahrens } 740fa9e4066Sahrens if (argc < 2) { 741fa9e4066Sahrens (void) fprintf(stderr, gettext("missing vdev specification\n")); 742990b4856Slling goto badusage; 743fa9e4066Sahrens } 744fa9e4066Sahrens 745fa9e4066Sahrens poolname = argv[0]; 746fa9e4066Sahrens 747fa9e4066Sahrens /* 748fa9e4066Sahrens * As a special case, check for use of '/' in the name, and direct the 749fa9e4066Sahrens * user to use 'zfs create' instead. 750fa9e4066Sahrens */ 751fa9e4066Sahrens if (strchr(poolname, '/') != NULL) { 752fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot create '%s': invalid " 753fa9e4066Sahrens "character '/' in pool name\n"), poolname); 754fa9e4066Sahrens (void) fprintf(stderr, gettext("use 'zfs create' to " 755fa9e4066Sahrens "create a dataset\n")); 756990b4856Slling goto errout; 757fa9e4066Sahrens } 758fa9e4066Sahrens 759fa9e4066Sahrens /* pass off to get_vdev_spec for bulk processing */ 760705040edSEric Taylor nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun, 761705040edSEric Taylor argc - 1, argv + 1); 762fa9e4066Sahrens if (nvroot == NULL) 7630a48a24eStimh goto errout; 764fa9e4066Sahrens 76599653d4eSeschrock /* make_root_vdev() allows 0 toplevel children if there are spares */ 766b7b97454Sperrin if (!zfs_allocatable_devs(nvroot)) { 76799653d4eSeschrock (void) fprintf(stderr, gettext("invalid vdev " 76899653d4eSeschrock "specification: at least one toplevel vdev must be " 76999653d4eSeschrock "specified\n")); 770990b4856Slling goto errout; 77199653d4eSeschrock } 77299653d4eSeschrock 773fa9e4066Sahrens if (altroot != NULL && altroot[0] != '/') { 774fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid alternate root '%s': " 775e9dbad6fSeschrock "must be an absolute path\n"), altroot); 776990b4856Slling goto errout; 777fa9e4066Sahrens } 778fa9e4066Sahrens 779fa9e4066Sahrens /* 780fa9e4066Sahrens * Check the validity of the mountpoint and direct the user to use the 781fa9e4066Sahrens * '-m' mountpoint option if it looks like its in use. 782fa9e4066Sahrens */ 783fa9e4066Sahrens if (mountpoint == NULL || 784fa9e4066Sahrens (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && 785fa9e4066Sahrens strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) { 786fa9e4066Sahrens char buf[MAXPATHLEN]; 78711022c7cStimh DIR *dirp; 788fa9e4066Sahrens 789fa9e4066Sahrens if (mountpoint && mountpoint[0] != '/') { 790fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid mountpoint " 791fa9e4066Sahrens "'%s': must be an absolute path, 'legacy', or " 792fa9e4066Sahrens "'none'\n"), mountpoint); 793990b4856Slling goto errout; 794fa9e4066Sahrens } 795fa9e4066Sahrens 796fa9e4066Sahrens if (mountpoint == NULL) { 797fa9e4066Sahrens if (altroot != NULL) 798fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s/%s", 799fa9e4066Sahrens altroot, poolname); 800fa9e4066Sahrens else 801fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "/%s", 802fa9e4066Sahrens poolname); 803fa9e4066Sahrens } else { 804fa9e4066Sahrens if (altroot != NULL) 805fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s%s", 806fa9e4066Sahrens altroot, mountpoint); 807fa9e4066Sahrens else 808fa9e4066Sahrens (void) snprintf(buf, sizeof (buf), "%s", 809fa9e4066Sahrens mountpoint); 810fa9e4066Sahrens } 811fa9e4066Sahrens 81211022c7cStimh if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { 81311022c7cStimh (void) fprintf(stderr, gettext("mountpoint '%s' : " 81411022c7cStimh "%s\n"), buf, strerror(errno)); 815fa9e4066Sahrens (void) fprintf(stderr, gettext("use '-m' " 816fa9e4066Sahrens "option to provide a different default\n")); 817990b4856Slling goto errout; 81811022c7cStimh } else if (dirp) { 81911022c7cStimh int count = 0; 82011022c7cStimh 82111022c7cStimh while (count < 3 && readdir(dirp) != NULL) 82211022c7cStimh count++; 82311022c7cStimh (void) closedir(dirp); 82411022c7cStimh 82511022c7cStimh if (count > 2) { 82611022c7cStimh (void) fprintf(stderr, gettext("mountpoint " 82711022c7cStimh "'%s' exists and is not empty\n"), buf); 82811022c7cStimh (void) fprintf(stderr, gettext("use '-m' " 82911022c7cStimh "option to provide a " 83011022c7cStimh "different default\n")); 83111022c7cStimh goto errout; 83211022c7cStimh } 833fa9e4066Sahrens } 834fa9e4066Sahrens } 835fa9e4066Sahrens 836fa9e4066Sahrens if (dryrun) { 837fa9e4066Sahrens /* 838fa9e4066Sahrens * For a dry run invocation, print out a basic message and run 839fa9e4066Sahrens * through all the vdevs in the list and print out in an 840fa9e4066Sahrens * appropriate hierarchy. 841fa9e4066Sahrens */ 842fa9e4066Sahrens (void) printf(gettext("would create '%s' with the " 843fa9e4066Sahrens "following layout:\n\n"), poolname); 844fa9e4066Sahrens 8458654d025Sperrin print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE); 8468654d025Sperrin if (num_logs(nvroot) > 0) 8478654d025Sperrin print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE); 848fa9e4066Sahrens 849fa9e4066Sahrens ret = 0; 850fa9e4066Sahrens } else { 851fa9e4066Sahrens /* 852fa9e4066Sahrens * Hand off to libzfs. 853fa9e4066Sahrens */ 854ad135b5dSChristopher Siden if (enable_all_pool_feat) { 855ad135b5dSChristopher Siden int i; 856ad135b5dSChristopher Siden for (i = 0; i < SPA_FEATURES; i++) { 857ad135b5dSChristopher Siden char propname[MAXPATHLEN]; 858ad135b5dSChristopher Siden zfeature_info_t *feat = &spa_feature_table[i]; 859ad135b5dSChristopher Siden 860ad135b5dSChristopher Siden (void) snprintf(propname, sizeof (propname), 861ad135b5dSChristopher Siden "feature@%s", feat->fi_uname); 862ad135b5dSChristopher Siden 863ad135b5dSChristopher Siden /* 864ad135b5dSChristopher Siden * Skip feature if user specified it manually 865ad135b5dSChristopher Siden * on the command line. 866ad135b5dSChristopher Siden */ 867ad135b5dSChristopher Siden if (nvlist_exists(props, propname)) 868ad135b5dSChristopher Siden continue; 869ad135b5dSChristopher Siden 870ad135b5dSChristopher Siden if (add_prop_list(propname, ZFS_FEATURE_ENABLED, 871ad135b5dSChristopher Siden &props, B_TRUE) != 0) 872ad135b5dSChristopher Siden goto errout; 873ad135b5dSChristopher Siden } 874ad135b5dSChristopher Siden } 8750a48a24eStimh if (zpool_create(g_zfs, poolname, 8760a48a24eStimh nvroot, props, fsprops) == 0) { 87799653d4eSeschrock zfs_handle_t *pool = zfs_open(g_zfs, poolname, 878fa9e4066Sahrens ZFS_TYPE_FILESYSTEM); 879fa9e4066Sahrens if (pool != NULL) { 880fa9e4066Sahrens if (mountpoint != NULL) 881fa9e4066Sahrens verify(zfs_prop_set(pool, 882e9dbad6fSeschrock zfs_prop_to_name( 883e9dbad6fSeschrock ZFS_PROP_MOUNTPOINT), 884fa9e4066Sahrens mountpoint) == 0); 885fa9e4066Sahrens if (zfs_mount(pool, NULL, 0) == 0) 886da6c28aaSamw ret = zfs_shareall(pool); 887fa9e4066Sahrens zfs_close(pool); 888fa9e4066Sahrens } 88999653d4eSeschrock } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { 89099653d4eSeschrock (void) fprintf(stderr, gettext("pool name may have " 89199653d4eSeschrock "been omitted\n")); 892fa9e4066Sahrens } 893fa9e4066Sahrens } 894fa9e4066Sahrens 895990b4856Slling errout: 896fa9e4066Sahrens nvlist_free(nvroot); 8970a48a24eStimh nvlist_free(fsprops); 898990b4856Slling nvlist_free(props); 899fa9e4066Sahrens return (ret); 900990b4856Slling badusage: 9010a48a24eStimh nvlist_free(fsprops); 902990b4856Slling nvlist_free(props); 903990b4856Slling usage(B_FALSE); 904990b4856Slling return (2); 905fa9e4066Sahrens } 906fa9e4066Sahrens 907fa9e4066Sahrens /* 908fa9e4066Sahrens * zpool destroy <pool> 909fa9e4066Sahrens * 910fa9e4066Sahrens * -f Forcefully unmount any datasets 911fa9e4066Sahrens * 912fa9e4066Sahrens * Destroy the given pool. Automatically unmounts any datasets in the pool. 913fa9e4066Sahrens */ 914fa9e4066Sahrens int 915fa9e4066Sahrens zpool_do_destroy(int argc, char **argv) 916fa9e4066Sahrens { 91799653d4eSeschrock boolean_t force = B_FALSE; 918fa9e4066Sahrens int c; 919fa9e4066Sahrens char *pool; 920fa9e4066Sahrens zpool_handle_t *zhp; 921fa9e4066Sahrens int ret; 922fa9e4066Sahrens 923fa9e4066Sahrens /* check options */ 924fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) { 925fa9e4066Sahrens switch (c) { 926fa9e4066Sahrens case 'f': 92799653d4eSeschrock force = B_TRUE; 928fa9e4066Sahrens break; 929fa9e4066Sahrens case '?': 930fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 931fa9e4066Sahrens optopt); 93299653d4eSeschrock usage(B_FALSE); 933fa9e4066Sahrens } 934fa9e4066Sahrens } 935fa9e4066Sahrens 936fa9e4066Sahrens argc -= optind; 937fa9e4066Sahrens argv += optind; 938fa9e4066Sahrens 939fa9e4066Sahrens /* check arguments */ 940fa9e4066Sahrens if (argc < 1) { 941fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool argument\n")); 94299653d4eSeschrock usage(B_FALSE); 943fa9e4066Sahrens } 944fa9e4066Sahrens if (argc > 1) { 945fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 94699653d4eSeschrock usage(B_FALSE); 947fa9e4066Sahrens } 948fa9e4066Sahrens 949fa9e4066Sahrens pool = argv[0]; 950fa9e4066Sahrens 95199653d4eSeschrock if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 952fa9e4066Sahrens /* 953fa9e4066Sahrens * As a special case, check for use of '/' in the name, and 954fa9e4066Sahrens * direct the user to use 'zfs destroy' instead. 955fa9e4066Sahrens */ 956fa9e4066Sahrens if (strchr(pool, '/') != NULL) 957fa9e4066Sahrens (void) fprintf(stderr, gettext("use 'zfs destroy' to " 958fa9e4066Sahrens "destroy a dataset\n")); 959fa9e4066Sahrens return (1); 960fa9e4066Sahrens } 961fa9e4066Sahrens 962f3861e1aSahl if (zpool_disable_datasets(zhp, force) != 0) { 963fa9e4066Sahrens (void) fprintf(stderr, gettext("could not destroy '%s': " 964fa9e4066Sahrens "could not unmount datasets\n"), zpool_get_name(zhp)); 965fa9e4066Sahrens return (1); 966fa9e4066Sahrens } 967fa9e4066Sahrens 9684445fffbSMatthew Ahrens /* The history must be logged as part of the export */ 9694445fffbSMatthew Ahrens log_history = B_FALSE; 9704445fffbSMatthew Ahrens 9714445fffbSMatthew Ahrens ret = (zpool_destroy(zhp, history_str) != 0); 972fa9e4066Sahrens 973fa9e4066Sahrens zpool_close(zhp); 974fa9e4066Sahrens 975fa9e4066Sahrens return (ret); 976fa9e4066Sahrens } 977fa9e4066Sahrens 978fa9e4066Sahrens /* 979fa9e4066Sahrens * zpool export [-f] <pool> ... 980fa9e4066Sahrens * 981fa9e4066Sahrens * -f Forcefully unmount datasets 982fa9e4066Sahrens * 983b1b8ab34Slling * Export the given pools. By default, the command will attempt to cleanly 984fa9e4066Sahrens * unmount any active datasets within the pool. If the '-f' flag is specified, 985fa9e4066Sahrens * then the datasets will be forcefully unmounted. 986fa9e4066Sahrens */ 987fa9e4066Sahrens int 988fa9e4066Sahrens zpool_do_export(int argc, char **argv) 989fa9e4066Sahrens { 99099653d4eSeschrock boolean_t force = B_FALSE; 991394ab0cbSGeorge Wilson boolean_t hardforce = B_FALSE; 992fa9e4066Sahrens int c; 993fa9e4066Sahrens zpool_handle_t *zhp; 994fa9e4066Sahrens int ret; 995fa9e4066Sahrens int i; 996fa9e4066Sahrens 997fa9e4066Sahrens /* check options */ 998394ab0cbSGeorge Wilson while ((c = getopt(argc, argv, "fF")) != -1) { 999fa9e4066Sahrens switch (c) { 1000fa9e4066Sahrens case 'f': 100199653d4eSeschrock force = B_TRUE; 1002fa9e4066Sahrens break; 1003394ab0cbSGeorge Wilson case 'F': 1004394ab0cbSGeorge Wilson hardforce = B_TRUE; 1005394ab0cbSGeorge Wilson break; 1006fa9e4066Sahrens case '?': 1007fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1008fa9e4066Sahrens optopt); 100999653d4eSeschrock usage(B_FALSE); 1010fa9e4066Sahrens } 1011fa9e4066Sahrens } 1012fa9e4066Sahrens 1013fa9e4066Sahrens argc -= optind; 1014fa9e4066Sahrens argv += optind; 1015fa9e4066Sahrens 1016fa9e4066Sahrens /* check arguments */ 1017fa9e4066Sahrens if (argc < 1) { 1018fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool argument\n")); 101999653d4eSeschrock usage(B_FALSE); 1020fa9e4066Sahrens } 1021fa9e4066Sahrens 1022fa9e4066Sahrens ret = 0; 1023fa9e4066Sahrens for (i = 0; i < argc; i++) { 102499653d4eSeschrock if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) { 1025fa9e4066Sahrens ret = 1; 1026fa9e4066Sahrens continue; 1027fa9e4066Sahrens } 1028fa9e4066Sahrens 1029f3861e1aSahl if (zpool_disable_datasets(zhp, force) != 0) { 1030fa9e4066Sahrens ret = 1; 1031fa9e4066Sahrens zpool_close(zhp); 1032fa9e4066Sahrens continue; 1033fa9e4066Sahrens } 1034fa9e4066Sahrens 10354445fffbSMatthew Ahrens /* The history must be logged as part of the export */ 10364445fffbSMatthew Ahrens log_history = B_FALSE; 10374445fffbSMatthew Ahrens 1038394ab0cbSGeorge Wilson if (hardforce) { 10394445fffbSMatthew Ahrens if (zpool_export_force(zhp, history_str) != 0) 1040fa9e4066Sahrens ret = 1; 10414445fffbSMatthew Ahrens } else if (zpool_export(zhp, force, history_str) != 0) { 1042394ab0cbSGeorge Wilson ret = 1; 1043394ab0cbSGeorge Wilson } 1044fa9e4066Sahrens 1045fa9e4066Sahrens zpool_close(zhp); 1046fa9e4066Sahrens } 1047fa9e4066Sahrens 1048fa9e4066Sahrens return (ret); 1049fa9e4066Sahrens } 1050fa9e4066Sahrens 1051fa9e4066Sahrens /* 1052fa9e4066Sahrens * Given a vdev configuration, determine the maximum width needed for the device 1053fa9e4066Sahrens * name column. 1054fa9e4066Sahrens */ 1055fa9e4066Sahrens static int 1056c67d9675Seschrock max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max) 1057fa9e4066Sahrens { 105888ecc943SGeorge Wilson char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE); 1059fa9e4066Sahrens nvlist_t **child; 1060fa9e4066Sahrens uint_t c, children; 1061fa9e4066Sahrens int ret; 1062fa9e4066Sahrens 1063fa9e4066Sahrens if (strlen(name) + depth > max) 1064fa9e4066Sahrens max = strlen(name) + depth; 1065fa9e4066Sahrens 1066afefbcddSeschrock free(name); 1067afefbcddSeschrock 106899653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 106999653d4eSeschrock &child, &children) == 0) { 1070fa9e4066Sahrens for (c = 0; c < children; c++) 107199653d4eSeschrock if ((ret = max_width(zhp, child[c], depth + 2, 107299653d4eSeschrock max)) > max) 1073fa9e4066Sahrens max = ret; 107499653d4eSeschrock } 107599653d4eSeschrock 1076fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1077fa94a07fSbrendan &child, &children) == 0) { 1078fa94a07fSbrendan for (c = 0; c < children; c++) 1079fa94a07fSbrendan if ((ret = max_width(zhp, child[c], depth + 2, 1080fa94a07fSbrendan max)) > max) 1081fa94a07fSbrendan max = ret; 1082fa94a07fSbrendan } 1083fa94a07fSbrendan 108499653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 108599653d4eSeschrock &child, &children) == 0) { 108699653d4eSeschrock for (c = 0; c < children; c++) 108799653d4eSeschrock if ((ret = max_width(zhp, child[c], depth + 2, 108899653d4eSeschrock max)) > max) 108999653d4eSeschrock max = ret; 109099653d4eSeschrock } 109199653d4eSeschrock 1092fa9e4066Sahrens 1093fa9e4066Sahrens return (max); 1094fa9e4066Sahrens } 1095fa9e4066Sahrens 1096e6ca193dSGeorge Wilson typedef struct spare_cbdata { 1097e6ca193dSGeorge Wilson uint64_t cb_guid; 1098e6ca193dSGeorge Wilson zpool_handle_t *cb_zhp; 1099e6ca193dSGeorge Wilson } spare_cbdata_t; 1100e6ca193dSGeorge Wilson 1101e6ca193dSGeorge Wilson static boolean_t 1102e6ca193dSGeorge Wilson find_vdev(nvlist_t *nv, uint64_t search) 1103e6ca193dSGeorge Wilson { 1104e6ca193dSGeorge Wilson uint64_t guid; 1105e6ca193dSGeorge Wilson nvlist_t **child; 1106e6ca193dSGeorge Wilson uint_t c, children; 1107e6ca193dSGeorge Wilson 1108e6ca193dSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && 1109e6ca193dSGeorge Wilson search == guid) 1110e6ca193dSGeorge Wilson return (B_TRUE); 1111e6ca193dSGeorge Wilson 1112e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1113e6ca193dSGeorge Wilson &child, &children) == 0) { 1114e6ca193dSGeorge Wilson for (c = 0; c < children; c++) 1115e6ca193dSGeorge Wilson if (find_vdev(child[c], search)) 1116e6ca193dSGeorge Wilson return (B_TRUE); 1117e6ca193dSGeorge Wilson } 1118e6ca193dSGeorge Wilson 1119e6ca193dSGeorge Wilson return (B_FALSE); 1120e6ca193dSGeorge Wilson } 1121e6ca193dSGeorge Wilson 1122e6ca193dSGeorge Wilson static int 1123e6ca193dSGeorge Wilson find_spare(zpool_handle_t *zhp, void *data) 1124e6ca193dSGeorge Wilson { 1125e6ca193dSGeorge Wilson spare_cbdata_t *cbp = data; 1126e6ca193dSGeorge Wilson nvlist_t *config, *nvroot; 1127e6ca193dSGeorge Wilson 1128e6ca193dSGeorge Wilson config = zpool_get_config(zhp, NULL); 1129e6ca193dSGeorge Wilson verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1130e6ca193dSGeorge Wilson &nvroot) == 0); 1131e6ca193dSGeorge Wilson 1132e6ca193dSGeorge Wilson if (find_vdev(nvroot, cbp->cb_guid)) { 1133e6ca193dSGeorge Wilson cbp->cb_zhp = zhp; 1134e6ca193dSGeorge Wilson return (1); 1135e6ca193dSGeorge Wilson } 1136e6ca193dSGeorge Wilson 1137e6ca193dSGeorge Wilson zpool_close(zhp); 1138e6ca193dSGeorge Wilson return (0); 1139e6ca193dSGeorge Wilson } 1140e6ca193dSGeorge Wilson 1141e6ca193dSGeorge Wilson /* 1142e6ca193dSGeorge Wilson * Print out configuration state as requested by status_callback. 1143e6ca193dSGeorge Wilson */ 1144e6ca193dSGeorge Wilson void 1145e6ca193dSGeorge Wilson print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 1146e6ca193dSGeorge Wilson int namewidth, int depth, boolean_t isspare) 1147e6ca193dSGeorge Wilson { 1148e6ca193dSGeorge Wilson nvlist_t **child; 1149e6ca193dSGeorge Wilson uint_t c, children; 11503f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 1151e6ca193dSGeorge Wilson vdev_stat_t *vs; 11523f9d6ad7SLin Ling char rbuf[6], wbuf[6], cbuf[6]; 1153e6ca193dSGeorge Wilson char *vname; 1154e6ca193dSGeorge Wilson uint64_t notpresent; 1155e6ca193dSGeorge Wilson spare_cbdata_t cb; 1156e6ca193dSGeorge Wilson char *state; 1157e6ca193dSGeorge Wilson 1158e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1159e6ca193dSGeorge Wilson &child, &children) != 0) 1160e6ca193dSGeorge Wilson children = 0; 1161e6ca193dSGeorge Wilson 11623f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 11633f9d6ad7SLin Ling (uint64_t **)&vs, &c) == 0); 11643f9d6ad7SLin Ling 1165e6ca193dSGeorge Wilson state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 1166e6ca193dSGeorge Wilson if (isspare) { 1167e6ca193dSGeorge Wilson /* 1168e6ca193dSGeorge Wilson * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for 1169e6ca193dSGeorge Wilson * online drives. 1170e6ca193dSGeorge Wilson */ 1171e6ca193dSGeorge Wilson if (vs->vs_aux == VDEV_AUX_SPARED) 1172e6ca193dSGeorge Wilson state = "INUSE"; 1173e6ca193dSGeorge Wilson else if (vs->vs_state == VDEV_STATE_HEALTHY) 1174e6ca193dSGeorge Wilson state = "AVAIL"; 1175e6ca193dSGeorge Wilson } 1176e6ca193dSGeorge Wilson 1177e6ca193dSGeorge Wilson (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth, 1178e6ca193dSGeorge Wilson name, state); 1179e6ca193dSGeorge Wilson 1180e6ca193dSGeorge Wilson if (!isspare) { 1181e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); 1182e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); 1183e6ca193dSGeorge Wilson zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf)); 1184e6ca193dSGeorge Wilson (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf); 1185e6ca193dSGeorge Wilson } 1186e6ca193dSGeorge Wilson 1187e6ca193dSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1188e6ca193dSGeorge Wilson ¬present) == 0) { 1189e6ca193dSGeorge Wilson char *path; 1190e6ca193dSGeorge Wilson verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 1191e6ca193dSGeorge Wilson (void) printf(" was %s", path); 1192e6ca193dSGeorge Wilson } else if (vs->vs_aux != 0) { 1193e6ca193dSGeorge Wilson (void) printf(" "); 1194e6ca193dSGeorge Wilson 1195e6ca193dSGeorge Wilson switch (vs->vs_aux) { 1196e6ca193dSGeorge Wilson case VDEV_AUX_OPEN_FAILED: 1197e6ca193dSGeorge Wilson (void) printf(gettext("cannot open")); 1198e6ca193dSGeorge Wilson break; 1199e6ca193dSGeorge Wilson 1200e6ca193dSGeorge Wilson case VDEV_AUX_BAD_GUID_SUM: 1201e6ca193dSGeorge Wilson (void) printf(gettext("missing device")); 1202e6ca193dSGeorge Wilson break; 1203e6ca193dSGeorge Wilson 1204e6ca193dSGeorge Wilson case VDEV_AUX_NO_REPLICAS: 1205e6ca193dSGeorge Wilson (void) printf(gettext("insufficient replicas")); 1206e6ca193dSGeorge Wilson break; 1207e6ca193dSGeorge Wilson 1208e6ca193dSGeorge Wilson case VDEV_AUX_VERSION_NEWER: 1209e6ca193dSGeorge Wilson (void) printf(gettext("newer version")); 1210e6ca193dSGeorge Wilson break; 1211e6ca193dSGeorge Wilson 1212ad135b5dSChristopher Siden case VDEV_AUX_UNSUP_FEAT: 1213ad135b5dSChristopher Siden (void) printf(gettext("unsupported feature(s)")); 1214ad135b5dSChristopher Siden break; 1215ad135b5dSChristopher Siden 1216e6ca193dSGeorge Wilson case VDEV_AUX_SPARED: 1217e6ca193dSGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1218e6ca193dSGeorge Wilson &cb.cb_guid) == 0); 1219e6ca193dSGeorge Wilson if (zpool_iter(g_zfs, find_spare, &cb) == 1) { 1220e6ca193dSGeorge Wilson if (strcmp(zpool_get_name(cb.cb_zhp), 1221e6ca193dSGeorge Wilson zpool_get_name(zhp)) == 0) 1222e6ca193dSGeorge Wilson (void) printf(gettext("currently in " 1223e6ca193dSGeorge Wilson "use")); 1224e6ca193dSGeorge Wilson else 1225e6ca193dSGeorge Wilson (void) printf(gettext("in use by " 1226e6ca193dSGeorge Wilson "pool '%s'"), 1227e6ca193dSGeorge Wilson zpool_get_name(cb.cb_zhp)); 1228e6ca193dSGeorge Wilson zpool_close(cb.cb_zhp); 1229e6ca193dSGeorge Wilson } else { 1230e6ca193dSGeorge Wilson (void) printf(gettext("currently in use")); 1231e6ca193dSGeorge Wilson } 1232e6ca193dSGeorge Wilson break; 1233e6ca193dSGeorge Wilson 1234e6ca193dSGeorge Wilson case VDEV_AUX_ERR_EXCEEDED: 1235e6ca193dSGeorge Wilson (void) printf(gettext("too many errors")); 1236e6ca193dSGeorge Wilson break; 1237e6ca193dSGeorge Wilson 1238e6ca193dSGeorge Wilson case VDEV_AUX_IO_FAILURE: 1239e6ca193dSGeorge Wilson (void) printf(gettext("experienced I/O failures")); 1240e6ca193dSGeorge Wilson break; 1241e6ca193dSGeorge Wilson 1242e6ca193dSGeorge Wilson case VDEV_AUX_BAD_LOG: 1243e6ca193dSGeorge Wilson (void) printf(gettext("bad intent log")); 1244e6ca193dSGeorge Wilson break; 1245e6ca193dSGeorge Wilson 1246069f55e2SEric Schrock case VDEV_AUX_EXTERNAL: 1247069f55e2SEric Schrock (void) printf(gettext("external device fault")); 1248069f55e2SEric Schrock break; 1249069f55e2SEric Schrock 12501195e687SMark J Musante case VDEV_AUX_SPLIT_POOL: 12511195e687SMark J Musante (void) printf(gettext("split into new pool")); 12521195e687SMark J Musante break; 12531195e687SMark J Musante 1254e6ca193dSGeorge Wilson default: 1255e6ca193dSGeorge Wilson (void) printf(gettext("corrupted data")); 1256e6ca193dSGeorge Wilson break; 1257e6ca193dSGeorge Wilson } 12583f9d6ad7SLin Ling } 12593f9d6ad7SLin Ling 12603f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS, 12613f9d6ad7SLin Ling (uint64_t **)&ps, &c); 12623f9d6ad7SLin Ling 12633f9d6ad7SLin Ling if (ps && ps->pss_state == DSS_SCANNING && 12643f9d6ad7SLin Ling vs->vs_scan_processed != 0 && children == 0) { 12653f9d6ad7SLin Ling (void) printf(gettext(" (%s)"), 12663f9d6ad7SLin Ling (ps->pss_func == POOL_SCAN_RESILVER) ? 12673f9d6ad7SLin Ling "resilvering" : "repairing"); 1268e6ca193dSGeorge Wilson } 1269e6ca193dSGeorge Wilson 1270e6ca193dSGeorge Wilson (void) printf("\n"); 1271e6ca193dSGeorge Wilson 1272e6ca193dSGeorge Wilson for (c = 0; c < children; c++) { 127388ecc943SGeorge Wilson uint64_t islog = B_FALSE, ishole = B_FALSE; 1274e6ca193dSGeorge Wilson 127588ecc943SGeorge Wilson /* Don't print logs or holes here */ 1276e6ca193dSGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 127788ecc943SGeorge Wilson &islog); 127888ecc943SGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 127988ecc943SGeorge Wilson &ishole); 128088ecc943SGeorge Wilson if (islog || ishole) 1281e6ca193dSGeorge Wilson continue; 128288ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); 1283e6ca193dSGeorge Wilson print_status_config(zhp, vname, child[c], 1284e6ca193dSGeorge Wilson namewidth, depth + 2, isspare); 1285e6ca193dSGeorge Wilson free(vname); 1286e6ca193dSGeorge Wilson } 1287e6ca193dSGeorge Wilson } 1288e6ca193dSGeorge Wilson 1289fa9e4066Sahrens 1290fa9e4066Sahrens /* 1291fa9e4066Sahrens * Print the configuration of an exported pool. Iterate over all vdevs in the 1292fa9e4066Sahrens * pool, printing out the name and status for each one. 1293fa9e4066Sahrens */ 1294fa9e4066Sahrens void 1295e6ca193dSGeorge Wilson print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth) 1296fa9e4066Sahrens { 1297fa9e4066Sahrens nvlist_t **child; 1298fa9e4066Sahrens uint_t c, children; 1299fa9e4066Sahrens vdev_stat_t *vs; 1300afefbcddSeschrock char *type, *vname; 1301fa9e4066Sahrens 1302fa9e4066Sahrens verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 130388ecc943SGeorge Wilson if (strcmp(type, VDEV_TYPE_MISSING) == 0 || 130488ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0) 1305fa9e4066Sahrens return; 1306fa9e4066Sahrens 13073f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 1308fa9e4066Sahrens (uint64_t **)&vs, &c) == 0); 1309fa9e4066Sahrens 1310fa9e4066Sahrens (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name); 1311990b4856Slling (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); 1312fa9e4066Sahrens 1313fa9e4066Sahrens if (vs->vs_aux != 0) { 13143d7072f8Seschrock (void) printf(" "); 1315fa9e4066Sahrens 1316fa9e4066Sahrens switch (vs->vs_aux) { 1317fa9e4066Sahrens case VDEV_AUX_OPEN_FAILED: 1318fa9e4066Sahrens (void) printf(gettext("cannot open")); 1319fa9e4066Sahrens break; 1320fa9e4066Sahrens 1321fa9e4066Sahrens case VDEV_AUX_BAD_GUID_SUM: 1322fa9e4066Sahrens (void) printf(gettext("missing device")); 1323fa9e4066Sahrens break; 1324fa9e4066Sahrens 1325fa9e4066Sahrens case VDEV_AUX_NO_REPLICAS: 1326fa9e4066Sahrens (void) printf(gettext("insufficient replicas")); 1327fa9e4066Sahrens break; 1328fa9e4066Sahrens 1329eaca9bbdSeschrock case VDEV_AUX_VERSION_NEWER: 1330eaca9bbdSeschrock (void) printf(gettext("newer version")); 1331eaca9bbdSeschrock break; 1332eaca9bbdSeschrock 1333ad135b5dSChristopher Siden case VDEV_AUX_UNSUP_FEAT: 1334ad135b5dSChristopher Siden (void) printf(gettext("unsupported feature(s)")); 1335ad135b5dSChristopher Siden break; 1336ad135b5dSChristopher Siden 13373d7072f8Seschrock case VDEV_AUX_ERR_EXCEEDED: 13383d7072f8Seschrock (void) printf(gettext("too many errors")); 13393d7072f8Seschrock break; 13403d7072f8Seschrock 1341fa9e4066Sahrens default: 1342fa9e4066Sahrens (void) printf(gettext("corrupted data")); 1343fa9e4066Sahrens break; 1344fa9e4066Sahrens } 1345fa9e4066Sahrens } 1346fa9e4066Sahrens (void) printf("\n"); 1347fa9e4066Sahrens 1348fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1349fa9e4066Sahrens &child, &children) != 0) 1350fa9e4066Sahrens return; 1351fa9e4066Sahrens 1352afefbcddSeschrock for (c = 0; c < children; c++) { 13538654d025Sperrin uint64_t is_log = B_FALSE; 13548654d025Sperrin 13558654d025Sperrin (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 13568654d025Sperrin &is_log); 1357e6ca193dSGeorge Wilson if (is_log) 13588654d025Sperrin continue; 13598654d025Sperrin 136088ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE); 1361e6ca193dSGeorge Wilson print_import_config(vname, child[c], namewidth, depth + 2); 1362afefbcddSeschrock free(vname); 1363afefbcddSeschrock } 136499653d4eSeschrock 1365fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1366fa94a07fSbrendan &child, &children) == 0) { 1367fa94a07fSbrendan (void) printf(gettext("\tcache\n")); 1368fa94a07fSbrendan for (c = 0; c < children; c++) { 136988ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); 1370fa94a07fSbrendan (void) printf("\t %s\n", vname); 1371fa94a07fSbrendan free(vname); 1372fa94a07fSbrendan } 1373fa94a07fSbrendan } 137499653d4eSeschrock 1375fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1376fa94a07fSbrendan &child, &children) == 0) { 137799653d4eSeschrock (void) printf(gettext("\tspares\n")); 137899653d4eSeschrock for (c = 0; c < children; c++) { 137988ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); 138099653d4eSeschrock (void) printf("\t %s\n", vname); 138199653d4eSeschrock free(vname); 138299653d4eSeschrock } 1383fa9e4066Sahrens } 1384fa94a07fSbrendan } 1385fa9e4066Sahrens 1386fa9e4066Sahrens /* 1387e6ca193dSGeorge Wilson * Print log vdevs. 1388e6ca193dSGeorge Wilson * Logs are recorded as top level vdevs in the main pool child array 1389e6ca193dSGeorge Wilson * but with "is_log" set to 1. We use either print_status_config() or 1390e6ca193dSGeorge Wilson * print_import_config() to print the top level logs then any log 1391e6ca193dSGeorge Wilson * children (eg mirrored slogs) are printed recursively - which 1392e6ca193dSGeorge Wilson * works because only the top level vdev is marked "is_log" 1393e6ca193dSGeorge Wilson */ 1394e6ca193dSGeorge Wilson static void 1395e6ca193dSGeorge Wilson print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose) 1396e6ca193dSGeorge Wilson { 1397e6ca193dSGeorge Wilson uint_t c, children; 1398e6ca193dSGeorge Wilson nvlist_t **child; 1399e6ca193dSGeorge Wilson 1400e6ca193dSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, 1401e6ca193dSGeorge Wilson &children) != 0) 1402e6ca193dSGeorge Wilson return; 1403e6ca193dSGeorge Wilson 1404e6ca193dSGeorge Wilson (void) printf(gettext("\tlogs\n")); 1405e6ca193dSGeorge Wilson 1406e6ca193dSGeorge Wilson for (c = 0; c < children; c++) { 1407e6ca193dSGeorge Wilson uint64_t is_log = B_FALSE; 1408e6ca193dSGeorge Wilson char *name; 1409e6ca193dSGeorge Wilson 1410e6ca193dSGeorge Wilson (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1411e6ca193dSGeorge Wilson &is_log); 1412e6ca193dSGeorge Wilson if (!is_log) 1413e6ca193dSGeorge Wilson continue; 141488ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); 1415e6ca193dSGeorge Wilson if (verbose) 1416e6ca193dSGeorge Wilson print_status_config(zhp, name, child[c], namewidth, 1417e6ca193dSGeorge Wilson 2, B_FALSE); 1418e6ca193dSGeorge Wilson else 1419e6ca193dSGeorge Wilson print_import_config(name, child[c], namewidth, 2); 1420e6ca193dSGeorge Wilson free(name); 1421e6ca193dSGeorge Wilson } 1422e6ca193dSGeorge Wilson } 1423468c413aSTim Haley 1424e6ca193dSGeorge Wilson /* 1425fa9e4066Sahrens * Display the status for the given pool. 1426fa9e4066Sahrens */ 1427fa9e4066Sahrens static void 1428fa9e4066Sahrens show_import(nvlist_t *config) 1429fa9e4066Sahrens { 1430fa9e4066Sahrens uint64_t pool_state; 1431fa9e4066Sahrens vdev_stat_t *vs; 1432fa9e4066Sahrens char *name; 1433fa9e4066Sahrens uint64_t guid; 1434fa9e4066Sahrens char *msgid; 1435fa9e4066Sahrens nvlist_t *nvroot; 1436fa9e4066Sahrens int reason; 143746657f8dSmmusante const char *health; 1438fa9e4066Sahrens uint_t vsc; 1439fa9e4066Sahrens int namewidth; 14408704186eSDan McDonald char *comment; 1441fa9e4066Sahrens 1442fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1443fa9e4066Sahrens &name) == 0); 1444fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1445fa9e4066Sahrens &guid) == 0); 1446fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1447fa9e4066Sahrens &pool_state) == 0); 1448fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1449fa9e4066Sahrens &nvroot) == 0); 1450fa9e4066Sahrens 14513f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 1452fa9e4066Sahrens (uint64_t **)&vs, &vsc) == 0); 1453990b4856Slling health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 1454fa9e4066Sahrens 1455fa9e4066Sahrens reason = zpool_import_status(config, &msgid); 1456fa9e4066Sahrens 145746657f8dSmmusante (void) printf(gettext(" pool: %s\n"), name); 145846657f8dSmmusante (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); 145946657f8dSmmusante (void) printf(gettext(" state: %s"), health); 14604c58d714Sdarrenm if (pool_state == POOL_STATE_DESTROYED) 146146657f8dSmmusante (void) printf(gettext(" (DESTROYED)")); 14624c58d714Sdarrenm (void) printf("\n"); 1463fa9e4066Sahrens 1464fa9e4066Sahrens switch (reason) { 1465fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_R: 1466fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_NR: 1467fa9e4066Sahrens case ZPOOL_STATUS_BAD_GUID_SUM: 14688704186eSDan McDonald (void) printf(gettext(" status: One or more devices are " 14698704186eSDan McDonald "missing from the system.\n")); 1470fa9e4066Sahrens break; 1471fa9e4066Sahrens 1472fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_R: 1473fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_NR: 1474fa9e4066Sahrens (void) printf(gettext(" status: One or more devices contains " 1475fa9e4066Sahrens "corrupted data.\n")); 1476fa9e4066Sahrens break; 1477fa9e4066Sahrens 1478fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_DATA: 14798704186eSDan McDonald (void) printf( 14808704186eSDan McDonald gettext(" status: The pool data is corrupted.\n")); 1481fa9e4066Sahrens break; 1482fa9e4066Sahrens 1483441d80aaSlling case ZPOOL_STATUS_OFFLINE_DEV: 1484441d80aaSlling (void) printf(gettext(" status: One or more devices " 1485441d80aaSlling "are offlined.\n")); 1486441d80aaSlling break; 1487441d80aaSlling 1488ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_POOL: 1489ea8dc4b6Seschrock (void) printf(gettext(" status: The pool metadata is " 1490ea8dc4b6Seschrock "corrupted.\n")); 1491ea8dc4b6Seschrock break; 1492ea8dc4b6Seschrock 1493eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_OLDER: 149457221772SChristopher Siden (void) printf(gettext(" status: The pool is formatted using a " 149557221772SChristopher Siden "legacy on-disk version.\n")); 1496eaca9bbdSeschrock break; 1497eaca9bbdSeschrock 1498eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER: 1499eaca9bbdSeschrock (void) printf(gettext(" status: The pool is formatted using an " 1500eaca9bbdSeschrock "incompatible version.\n")); 1501eaca9bbdSeschrock break; 1502b87f3af3Sperrin 150357221772SChristopher Siden case ZPOOL_STATUS_FEAT_DISABLED: 150457221772SChristopher Siden (void) printf(gettext(" status: Some supported features are " 150557221772SChristopher Siden "not enabled on the pool.\n")); 150657221772SChristopher Siden break; 150757221772SChristopher Siden 1508ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ: 1509ad135b5dSChristopher Siden (void) printf(gettext("status: The pool uses the following " 1510ad135b5dSChristopher Siden "feature(s) not supported on this sytem:\n")); 1511ad135b5dSChristopher Siden zpool_print_unsup_feat(config); 1512ad135b5dSChristopher Siden break; 1513ad135b5dSChristopher Siden 1514ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 1515ad135b5dSChristopher Siden (void) printf(gettext("status: The pool can only be accessed " 1516ad135b5dSChristopher Siden "in read-only mode on this system. It\n\tcannot be " 1517ad135b5dSChristopher Siden "accessed in read-write mode because it uses the " 1518ad135b5dSChristopher Siden "following\n\tfeature(s) not supported on this system:\n")); 1519ad135b5dSChristopher Siden zpool_print_unsup_feat(config); 1520ad135b5dSChristopher Siden break; 1521ad135b5dSChristopher Siden 152295173954Sek110237 case ZPOOL_STATUS_HOSTID_MISMATCH: 152395173954Sek110237 (void) printf(gettext(" status: The pool was last accessed by " 152495173954Sek110237 "another system.\n")); 152595173954Sek110237 break; 1526b87f3af3Sperrin 15273d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_R: 15283d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_NR: 15293d7072f8Seschrock (void) printf(gettext(" status: One or more devices are " 15303d7072f8Seschrock "faulted.\n")); 15313d7072f8Seschrock break; 15323d7072f8Seschrock 1533b87f3af3Sperrin case ZPOOL_STATUS_BAD_LOG: 1534b87f3af3Sperrin (void) printf(gettext(" status: An intent log record cannot be " 1535b87f3af3Sperrin "read.\n")); 1536b87f3af3Sperrin break; 1537b87f3af3Sperrin 15383f9d6ad7SLin Ling case ZPOOL_STATUS_RESILVERING: 15393f9d6ad7SLin Ling (void) printf(gettext(" status: One or more devices were being " 15403f9d6ad7SLin Ling "resilvered.\n")); 15413f9d6ad7SLin Ling break; 15423f9d6ad7SLin Ling 1543fa9e4066Sahrens default: 1544fa9e4066Sahrens /* 1545fa9e4066Sahrens * No other status can be seen when importing pools. 1546fa9e4066Sahrens */ 1547fa9e4066Sahrens assert(reason == ZPOOL_STATUS_OK); 1548fa9e4066Sahrens } 1549fa9e4066Sahrens 1550fa9e4066Sahrens /* 1551fa9e4066Sahrens * Print out an action according to the overall state of the pool. 1552fa9e4066Sahrens */ 155346657f8dSmmusante if (vs->vs_state == VDEV_STATE_HEALTHY) { 155457221772SChristopher Siden if (reason == ZPOOL_STATUS_VERSION_OLDER || 155557221772SChristopher Siden reason == ZPOOL_STATUS_FEAT_DISABLED) { 1556eaca9bbdSeschrock (void) printf(gettext(" action: The pool can be " 1557eaca9bbdSeschrock "imported using its name or numeric identifier, " 1558eaca9bbdSeschrock "though\n\tsome features will not be available " 1559eaca9bbdSeschrock "without an explicit 'zpool upgrade'.\n")); 156057221772SChristopher Siden } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { 156195173954Sek110237 (void) printf(gettext(" action: The pool can be " 156295173954Sek110237 "imported using its name or numeric " 156395173954Sek110237 "identifier and\n\tthe '-f' flag.\n")); 156457221772SChristopher Siden } else { 1565eaca9bbdSeschrock (void) printf(gettext(" action: The pool can be " 1566eaca9bbdSeschrock "imported using its name or numeric " 1567eaca9bbdSeschrock "identifier.\n")); 156857221772SChristopher Siden } 156946657f8dSmmusante } else if (vs->vs_state == VDEV_STATE_DEGRADED) { 1570fa9e4066Sahrens (void) printf(gettext(" action: The pool can be imported " 1571fa9e4066Sahrens "despite missing or damaged devices. The\n\tfault " 1572eaca9bbdSeschrock "tolerance of the pool may be compromised if imported.\n")); 1573fa9e4066Sahrens } else { 1574eaca9bbdSeschrock switch (reason) { 1575eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER: 1576eaca9bbdSeschrock (void) printf(gettext(" action: The pool cannot be " 1577eaca9bbdSeschrock "imported. Access the pool on a system running " 1578eaca9bbdSeschrock "newer\n\tsoftware, or recreate the pool from " 1579eaca9bbdSeschrock "backup.\n")); 1580eaca9bbdSeschrock break; 1581ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ: 1582ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be " 1583ad135b5dSChristopher Siden "imported. Access the pool on a system that " 1584ad135b5dSChristopher Siden "supports\n\tthe required feature(s), or recreate " 1585ad135b5dSChristopher Siden "the pool from backup.\n")); 1586ad135b5dSChristopher Siden break; 1587ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 1588ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be " 1589ad135b5dSChristopher Siden "imported in read-write mode. Import the pool " 1590ad135b5dSChristopher Siden "with\n" 1591ad135b5dSChristopher Siden "\t\"-o readonly=on\", access the pool on a system " 1592ad135b5dSChristopher Siden "that supports the\n\trequired feature(s), or " 1593ad135b5dSChristopher Siden "recreate the pool from backup.\n")); 1594ad135b5dSChristopher Siden break; 1595eaca9bbdSeschrock case ZPOOL_STATUS_MISSING_DEV_R: 1596eaca9bbdSeschrock case ZPOOL_STATUS_MISSING_DEV_NR: 1597eaca9bbdSeschrock case ZPOOL_STATUS_BAD_GUID_SUM: 1598fa9e4066Sahrens (void) printf(gettext(" action: The pool cannot be " 1599fa9e4066Sahrens "imported. Attach the missing\n\tdevices and try " 1600fa9e4066Sahrens "again.\n")); 1601eaca9bbdSeschrock break; 1602eaca9bbdSeschrock default: 1603fa9e4066Sahrens (void) printf(gettext(" action: The pool cannot be " 1604fa9e4066Sahrens "imported due to damaged devices or data.\n")); 1605fa9e4066Sahrens } 1606eaca9bbdSeschrock } 1607eaca9bbdSeschrock 16088704186eSDan McDonald /* Print the comment attached to the pool. */ 16098704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 16108704186eSDan McDonald (void) printf(gettext("comment: %s\n"), comment); 16118704186eSDan McDonald 161246657f8dSmmusante /* 161346657f8dSmmusante * If the state is "closed" or "can't open", and the aux state 161446657f8dSmmusante * is "corrupt data": 161546657f8dSmmusante */ 161646657f8dSmmusante if (((vs->vs_state == VDEV_STATE_CLOSED) || 161746657f8dSmmusante (vs->vs_state == VDEV_STATE_CANT_OPEN)) && 161846657f8dSmmusante (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { 1619eaca9bbdSeschrock if (pool_state == POOL_STATE_DESTROYED) 1620eaca9bbdSeschrock (void) printf(gettext("\tThe pool was destroyed, " 1621eaca9bbdSeschrock "but can be imported using the '-Df' flags.\n")); 1622eaca9bbdSeschrock else if (pool_state != POOL_STATE_EXPORTED) 1623eaca9bbdSeschrock (void) printf(gettext("\tThe pool may be active on " 162418ce54dfSek110237 "another system, but can be imported using\n\t" 1625eaca9bbdSeschrock "the '-f' flag.\n")); 1626eaca9bbdSeschrock } 1627fa9e4066Sahrens 1628fa9e4066Sahrens if (msgid != NULL) 1629654b400cSJoshua M. Clulow (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 1630fa9e4066Sahrens msgid); 1631fa9e4066Sahrens 1632fa9e4066Sahrens (void) printf(gettext(" config:\n\n")); 1633fa9e4066Sahrens 1634c67d9675Seschrock namewidth = max_width(NULL, nvroot, 0, 0); 1635fa9e4066Sahrens if (namewidth < 10) 1636fa9e4066Sahrens namewidth = 10; 16378654d025Sperrin 1638e6ca193dSGeorge Wilson print_import_config(name, nvroot, namewidth, 0); 1639e6ca193dSGeorge Wilson if (num_logs(nvroot) > 0) 1640e6ca193dSGeorge Wilson print_logs(NULL, nvroot, namewidth, B_FALSE); 1641fa9e4066Sahrens 1642fa9e4066Sahrens if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { 164346657f8dSmmusante (void) printf(gettext("\n\tAdditional devices are known to " 1644fa9e4066Sahrens "be part of this pool, though their\n\texact " 164546657f8dSmmusante "configuration cannot be determined.\n")); 1646fa9e4066Sahrens } 1647fa9e4066Sahrens } 1648fa9e4066Sahrens 1649fa9e4066Sahrens /* 1650fa9e4066Sahrens * Perform the import for the given configuration. This passes the heavy 1651990b4856Slling * lifting off to zpool_import_props(), and then mounts the datasets contained 1652990b4856Slling * within the pool. 1653fa9e4066Sahrens */ 1654fa9e4066Sahrens static int 1655fa9e4066Sahrens do_import(nvlist_t *config, const char *newname, const char *mntopts, 16564b964adaSGeorge Wilson nvlist_t *props, int flags) 1657fa9e4066Sahrens { 1658fa9e4066Sahrens zpool_handle_t *zhp; 1659fa9e4066Sahrens char *name; 1660fa9e4066Sahrens uint64_t state; 1661eaca9bbdSeschrock uint64_t version; 1662fa9e4066Sahrens 1663fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1664fa9e4066Sahrens &name) == 0); 1665fa9e4066Sahrens 1666fa9e4066Sahrens verify(nvlist_lookup_uint64(config, 1667fa9e4066Sahrens ZPOOL_CONFIG_POOL_STATE, &state) == 0); 1668eaca9bbdSeschrock verify(nvlist_lookup_uint64(config, 1669eaca9bbdSeschrock ZPOOL_CONFIG_VERSION, &version) == 0); 1670ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(version)) { 1671eaca9bbdSeschrock (void) fprintf(stderr, gettext("cannot import '%s': pool " 1672ad135b5dSChristopher Siden "is formatted using an unsupported ZFS version\n"), name); 1673eaca9bbdSeschrock return (1); 16744b964adaSGeorge Wilson } else if (state != POOL_STATE_EXPORTED && 16754b964adaSGeorge Wilson !(flags & ZFS_IMPORT_ANY_HOST)) { 167695173954Sek110237 uint64_t hostid; 167795173954Sek110237 167895173954Sek110237 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, 167995173954Sek110237 &hostid) == 0) { 168095173954Sek110237 if ((unsigned long)hostid != gethostid()) { 168195173954Sek110237 char *hostname; 168295173954Sek110237 uint64_t timestamp; 168395173954Sek110237 time_t t; 168495173954Sek110237 168595173954Sek110237 verify(nvlist_lookup_string(config, 168695173954Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 168795173954Sek110237 verify(nvlist_lookup_uint64(config, 168895173954Sek110237 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0); 168995173954Sek110237 t = timestamp; 169095173954Sek110237 (void) fprintf(stderr, gettext("cannot import " 169195173954Sek110237 "'%s': pool may be in use from other " 169295173954Sek110237 "system, it was last accessed by %s " 169395173954Sek110237 "(hostid: 0x%lx) on %s"), name, hostname, 169495173954Sek110237 (unsigned long)hostid, 169595173954Sek110237 asctime(localtime(&t))); 169695173954Sek110237 (void) fprintf(stderr, gettext("use '-f' to " 169795173954Sek110237 "import anyway\n")); 1698fa9e4066Sahrens return (1); 1699fa9e4066Sahrens } 170095173954Sek110237 } else { 170195173954Sek110237 (void) fprintf(stderr, gettext("cannot import '%s': " 170295173954Sek110237 "pool may be in use from other system\n"), name); 170395173954Sek110237 (void) fprintf(stderr, gettext("use '-f' to import " 170495173954Sek110237 "anyway\n")); 170595173954Sek110237 return (1); 170695173954Sek110237 } 170795173954Sek110237 } 1708fa9e4066Sahrens 17094b964adaSGeorge Wilson if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) 1710fa9e4066Sahrens return (1); 1711fa9e4066Sahrens 1712fa9e4066Sahrens if (newname != NULL) 1713fa9e4066Sahrens name = (char *)newname; 1714fa9e4066Sahrens 17154f0f5e5bSVictor Latushkin if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) 17164f0f5e5bSVictor Latushkin return (1); 1717fa9e4066Sahrens 1718379c004dSEric Schrock if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 1719f9af39baSGeorge Wilson !(flags & ZFS_IMPORT_ONLY) && 1720379c004dSEric Schrock zpool_enable_datasets(zhp, mntopts, 0) != 0) { 1721fa9e4066Sahrens zpool_close(zhp); 1722fa9e4066Sahrens return (1); 1723fa9e4066Sahrens } 1724fa9e4066Sahrens 1725fa9e4066Sahrens zpool_close(zhp); 1726468c413aSTim Haley return (0); 1727fa9e4066Sahrens } 1728fa9e4066Sahrens 1729fa9e4066Sahrens /* 17304c58d714Sdarrenm * zpool import [-d dir] [-D] 17312f8aaab3Seschrock * import [-o mntopts] [-o prop=value] ... [-R root] [-D] 17322f8aaab3Seschrock * [-d dir | -c cachefile] [-f] -a 17332f8aaab3Seschrock * import [-o mntopts] [-o prop=value] ... [-R root] [-D] 1734468c413aSTim Haley * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool] 17352f8aaab3Seschrock * 17362f8aaab3Seschrock * -c Read pool information from a cachefile instead of searching 17372f8aaab3Seschrock * devices. 1738fa9e4066Sahrens * 1739fa9e4066Sahrens * -d Scan in a specific directory, other than /dev/dsk. More than 1740fa9e4066Sahrens * one directory can be specified using multiple '-d' options. 1741fa9e4066Sahrens * 17424c58d714Sdarrenm * -D Scan for previously destroyed pools or import all or only 17434c58d714Sdarrenm * specified destroyed pools. 17444c58d714Sdarrenm * 1745fa9e4066Sahrens * -R Temporarily import the pool, with all mountpoints relative to 1746fa9e4066Sahrens * the given root. The pool will remain exported when the machine 1747fa9e4066Sahrens * is rebooted. 1748fa9e4066Sahrens * 1749468c413aSTim Haley * -V Import even in the presence of faulted vdevs. This is an 1750c5904d13Seschrock * intentionally undocumented option for testing purposes, and 1751c5904d13Seschrock * treats the pool configuration as complete, leaving any bad 17524f0f5e5bSVictor Latushkin * vdevs in the FAULTED state. In other words, it does verbatim 17534f0f5e5bSVictor Latushkin * import. 1754c5904d13Seschrock * 1755468c413aSTim Haley * -f Force import, even if it appears that the pool is active. 1756468c413aSTim Haley * 1757468c413aSTim Haley * -F Attempt rewind if necessary. 1758468c413aSTim Haley * 1759468c413aSTim Haley * -n See if rewind would work, but don't actually rewind. 1760468c413aSTim Haley * 1761f9af39baSGeorge Wilson * -N Import the pool but don't mount datasets. 1762f9af39baSGeorge Wilson * 1763f9af39baSGeorge Wilson * -T Specify a starting txg to use for import. This option is 1764f9af39baSGeorge Wilson * intentionally undocumented option for testing purposes. 1765f9af39baSGeorge Wilson * 1766fa9e4066Sahrens * -a Import all pools found. 1767fa9e4066Sahrens * 1768990b4856Slling * -o Set property=value and/or temporary mount options (without '='). 1769ecd6cf80Smarks * 1770fa9e4066Sahrens * The import command scans for pools to import, and import pools based on pool 1771fa9e4066Sahrens * name and GUID. The pool can also be renamed as part of the import process. 1772fa9e4066Sahrens */ 1773fa9e4066Sahrens int 1774fa9e4066Sahrens zpool_do_import(int argc, char **argv) 1775fa9e4066Sahrens { 1776fa9e4066Sahrens char **searchdirs = NULL; 1777fa9e4066Sahrens int nsearch = 0; 1778fa9e4066Sahrens int c; 1779d41c4376SMark J Musante int err = 0; 17802f8aaab3Seschrock nvlist_t *pools = NULL; 178199653d4eSeschrock boolean_t do_all = B_FALSE; 178299653d4eSeschrock boolean_t do_destroyed = B_FALSE; 1783fa9e4066Sahrens char *mntopts = NULL; 1784fa9e4066Sahrens nvpair_t *elem; 1785fa9e4066Sahrens nvlist_t *config; 178624e697d4Sck153898 uint64_t searchguid = 0; 178724e697d4Sck153898 char *searchname = NULL; 1788990b4856Slling char *propval; 1789fa9e4066Sahrens nvlist_t *found_config; 1790468c413aSTim Haley nvlist_t *policy = NULL; 1791ecd6cf80Smarks nvlist_t *props = NULL; 179299653d4eSeschrock boolean_t first; 17934b964adaSGeorge Wilson int flags = ZFS_IMPORT_NORMAL; 1794468c413aSTim Haley uint32_t rewind_policy = ZPOOL_NO_REWIND; 1795468c413aSTim Haley boolean_t dryrun = B_FALSE; 1796468c413aSTim Haley boolean_t do_rewind = B_FALSE; 1797468c413aSTim Haley boolean_t xtreme_rewind = B_FALSE; 1798f9af39baSGeorge Wilson uint64_t pool_state, txg = -1ULL; 17992f8aaab3Seschrock char *cachefile = NULL; 1800d41c4376SMark J Musante importargs_t idata = { 0 }; 1801f9af39baSGeorge Wilson char *endptr; 1802fa9e4066Sahrens 1803fa9e4066Sahrens /* check options */ 1804f9af39baSGeorge Wilson while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) { 1805fa9e4066Sahrens switch (c) { 1806fa9e4066Sahrens case 'a': 180799653d4eSeschrock do_all = B_TRUE; 1808fa9e4066Sahrens break; 18092f8aaab3Seschrock case 'c': 18102f8aaab3Seschrock cachefile = optarg; 18112f8aaab3Seschrock break; 1812fa9e4066Sahrens case 'd': 1813fa9e4066Sahrens if (searchdirs == NULL) { 1814fa9e4066Sahrens searchdirs = safe_malloc(sizeof (char *)); 1815fa9e4066Sahrens } else { 1816fa9e4066Sahrens char **tmp = safe_malloc((nsearch + 1) * 1817fa9e4066Sahrens sizeof (char *)); 1818fa9e4066Sahrens bcopy(searchdirs, tmp, nsearch * 1819fa9e4066Sahrens sizeof (char *)); 1820fa9e4066Sahrens free(searchdirs); 1821fa9e4066Sahrens searchdirs = tmp; 1822fa9e4066Sahrens } 1823fa9e4066Sahrens searchdirs[nsearch++] = optarg; 1824fa9e4066Sahrens break; 18254c58d714Sdarrenm case 'D': 182699653d4eSeschrock do_destroyed = B_TRUE; 18274c58d714Sdarrenm break; 1828fa9e4066Sahrens case 'f': 18294b964adaSGeorge Wilson flags |= ZFS_IMPORT_ANY_HOST; 1830fa9e4066Sahrens break; 1831c5904d13Seschrock case 'F': 1832468c413aSTim Haley do_rewind = B_TRUE; 1833468c413aSTim Haley break; 18344b964adaSGeorge Wilson case 'm': 18354b964adaSGeorge Wilson flags |= ZFS_IMPORT_MISSING_LOG; 18364b964adaSGeorge Wilson break; 1837468c413aSTim Haley case 'n': 1838468c413aSTim Haley dryrun = B_TRUE; 1839c5904d13Seschrock break; 1840f9af39baSGeorge Wilson case 'N': 1841f9af39baSGeorge Wilson flags |= ZFS_IMPORT_ONLY; 1842f9af39baSGeorge Wilson break; 1843fa9e4066Sahrens case 'o': 1844990b4856Slling if ((propval = strchr(optarg, '=')) != NULL) { 1845990b4856Slling *propval = '\0'; 1846990b4856Slling propval++; 18470a48a24eStimh if (add_prop_list(optarg, propval, 18480a48a24eStimh &props, B_TRUE)) 1849990b4856Slling goto error; 1850990b4856Slling } else { 1851fa9e4066Sahrens mntopts = optarg; 1852990b4856Slling } 1853fa9e4066Sahrens break; 1854fa9e4066Sahrens case 'R': 1855990b4856Slling if (add_prop_list(zpool_prop_to_name( 18560a48a24eStimh ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 1857990b4856Slling goto error; 18582f8aaab3Seschrock if (nvlist_lookup_string(props, 18592f8aaab3Seschrock zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 18602f8aaab3Seschrock &propval) == 0) 18612f8aaab3Seschrock break; 1862990b4856Slling if (add_prop_list(zpool_prop_to_name( 18630a48a24eStimh ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 1864990b4856Slling goto error; 1865fa9e4066Sahrens break; 1866f9af39baSGeorge Wilson case 'T': 1867f9af39baSGeorge Wilson errno = 0; 1868f9af39baSGeorge Wilson txg = strtoull(optarg, &endptr, 10); 1869f9af39baSGeorge Wilson if (errno != 0 || *endptr != '\0') { 1870f9af39baSGeorge Wilson (void) fprintf(stderr, 1871f9af39baSGeorge Wilson gettext("invalid txg value\n")); 1872f9af39baSGeorge Wilson usage(B_FALSE); 1873f9af39baSGeorge Wilson } 1874f9af39baSGeorge Wilson rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; 1875f9af39baSGeorge Wilson break; 1876468c413aSTim Haley case 'V': 18774b964adaSGeorge Wilson flags |= ZFS_IMPORT_VERBATIM; 1878468c413aSTim Haley break; 1879468c413aSTim Haley case 'X': 1880468c413aSTim Haley xtreme_rewind = B_TRUE; 1881468c413aSTim Haley break; 1882fa9e4066Sahrens case ':': 1883fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for " 1884fa9e4066Sahrens "'%c' option\n"), optopt); 188599653d4eSeschrock usage(B_FALSE); 1886fa9e4066Sahrens break; 1887fa9e4066Sahrens case '?': 1888fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1889fa9e4066Sahrens optopt); 189099653d4eSeschrock usage(B_FALSE); 1891fa9e4066Sahrens } 1892fa9e4066Sahrens } 1893fa9e4066Sahrens 1894fa9e4066Sahrens argc -= optind; 1895fa9e4066Sahrens argv += optind; 1896fa9e4066Sahrens 18972f8aaab3Seschrock if (cachefile && nsearch != 0) { 18982f8aaab3Seschrock (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); 18992f8aaab3Seschrock usage(B_FALSE); 19002f8aaab3Seschrock } 19012f8aaab3Seschrock 1902468c413aSTim Haley if ((dryrun || xtreme_rewind) && !do_rewind) { 1903468c413aSTim Haley (void) fprintf(stderr, 1904468c413aSTim Haley gettext("-n or -X only meaningful with -F\n")); 1905468c413aSTim Haley usage(B_FALSE); 1906468c413aSTim Haley } 1907468c413aSTim Haley if (dryrun) 1908468c413aSTim Haley rewind_policy = ZPOOL_TRY_REWIND; 1909468c413aSTim Haley else if (do_rewind) 1910468c413aSTim Haley rewind_policy = ZPOOL_DO_REWIND; 1911468c413aSTim Haley if (xtreme_rewind) 1912468c413aSTim Haley rewind_policy |= ZPOOL_EXTREME_REWIND; 1913468c413aSTim Haley 1914468c413aSTim Haley /* In the future, we can capture further policy and include it here */ 1915468c413aSTim Haley if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 1916f9af39baSGeorge Wilson nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 || 1917468c413aSTim Haley nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 1918468c413aSTim Haley goto error; 1919468c413aSTim Haley 1920fa9e4066Sahrens if (searchdirs == NULL) { 1921fa9e4066Sahrens searchdirs = safe_malloc(sizeof (char *)); 1922fa9e4066Sahrens searchdirs[0] = "/dev/dsk"; 1923fa9e4066Sahrens nsearch = 1; 1924fa9e4066Sahrens } 1925fa9e4066Sahrens 1926fa9e4066Sahrens /* check argument count */ 1927fa9e4066Sahrens if (do_all) { 1928fa9e4066Sahrens if (argc != 0) { 1929fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 193099653d4eSeschrock usage(B_FALSE); 1931fa9e4066Sahrens } 1932fa9e4066Sahrens } else { 1933fa9e4066Sahrens if (argc > 2) { 1934fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 193599653d4eSeschrock usage(B_FALSE); 1936fa9e4066Sahrens } 1937fa9e4066Sahrens 1938fa9e4066Sahrens /* 1939fa9e4066Sahrens * Check for the SYS_CONFIG privilege. We do this explicitly 1940fa9e4066Sahrens * here because otherwise any attempt to discover pools will 1941fa9e4066Sahrens * silently fail. 1942fa9e4066Sahrens */ 1943fa9e4066Sahrens if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) { 1944fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot " 1945fa9e4066Sahrens "discover pools: permission denied\n")); 194699653d4eSeschrock free(searchdirs); 1947468c413aSTim Haley nvlist_free(policy); 1948fa9e4066Sahrens return (1); 1949fa9e4066Sahrens } 1950fa9e4066Sahrens } 1951fa9e4066Sahrens 1952fa9e4066Sahrens /* 1953fa9e4066Sahrens * Depending on the arguments given, we do one of the following: 1954fa9e4066Sahrens * 1955fa9e4066Sahrens * <none> Iterate through all pools and display information about 1956fa9e4066Sahrens * each one. 1957fa9e4066Sahrens * 1958fa9e4066Sahrens * -a Iterate through all pools and try to import each one. 1959fa9e4066Sahrens * 1960fa9e4066Sahrens * <id> Find the pool that corresponds to the given GUID/pool 1961fa9e4066Sahrens * name and import that one. 19624c58d714Sdarrenm * 19634c58d714Sdarrenm * -D Above options applies only to destroyed pools. 1964fa9e4066Sahrens */ 1965fa9e4066Sahrens if (argc != 0) { 1966fa9e4066Sahrens char *endptr; 1967fa9e4066Sahrens 1968fa9e4066Sahrens errno = 0; 1969fa9e4066Sahrens searchguid = strtoull(argv[0], &endptr, 10); 1970fa9e4066Sahrens if (errno != 0 || *endptr != '\0') 1971fa9e4066Sahrens searchname = argv[0]; 1972fa9e4066Sahrens found_config = NULL; 1973fa9e4066Sahrens 197424e697d4Sck153898 /* 1975d41c4376SMark J Musante * User specified a name or guid. Ensure it's unique. 197624e697d4Sck153898 */ 1977d41c4376SMark J Musante idata.unique = B_TRUE; 197824e697d4Sck153898 } 197924e697d4Sck153898 1980d41c4376SMark J Musante 1981d41c4376SMark J Musante idata.path = searchdirs; 1982d41c4376SMark J Musante idata.paths = nsearch; 1983d41c4376SMark J Musante idata.poolname = searchname; 1984d41c4376SMark J Musante idata.guid = searchguid; 1985d41c4376SMark J Musante idata.cachefile = cachefile; 1986d41c4376SMark J Musante 1987d41c4376SMark J Musante pools = zpool_search_import(g_zfs, &idata); 1988d41c4376SMark J Musante 1989d41c4376SMark J Musante if (pools != NULL && idata.exists && 1990d41c4376SMark J Musante (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { 1991d41c4376SMark J Musante (void) fprintf(stderr, gettext("cannot import '%s': " 1992d41c4376SMark J Musante "a pool with that name already exists\n"), 1993d41c4376SMark J Musante argv[0]); 1994d41c4376SMark J Musante (void) fprintf(stderr, gettext("use the form '%s " 1995d41c4376SMark J Musante "<pool | id> <newpool>' to give it a new name\n"), 1996d41c4376SMark J Musante "zpool import"); 1997d41c4376SMark J Musante err = 1; 1998d41c4376SMark J Musante } else if (pools == NULL && idata.exists) { 1999d41c4376SMark J Musante (void) fprintf(stderr, gettext("cannot import '%s': " 2000d41c4376SMark J Musante "a pool with that name is already created/imported,\n"), 2001d41c4376SMark J Musante argv[0]); 2002d41c4376SMark J Musante (void) fprintf(stderr, gettext("and no additional pools " 2003d41c4376SMark J Musante "with that name were found\n")); 2004d41c4376SMark J Musante err = 1; 2005d41c4376SMark J Musante } else if (pools == NULL) { 200624e697d4Sck153898 if (argc != 0) { 200724e697d4Sck153898 (void) fprintf(stderr, gettext("cannot import '%s': " 200824e697d4Sck153898 "no such pool available\n"), argv[0]); 200924e697d4Sck153898 } 2010d41c4376SMark J Musante err = 1; 2011d41c4376SMark J Musante } 2012d41c4376SMark J Musante 2013d41c4376SMark J Musante if (err == 1) { 201424e697d4Sck153898 free(searchdirs); 2015468c413aSTim Haley nvlist_free(policy); 201624e697d4Sck153898 return (1); 201724e697d4Sck153898 } 201824e697d4Sck153898 201924e697d4Sck153898 /* 202024e697d4Sck153898 * At this point we have a list of import candidate configs. Even if 202124e697d4Sck153898 * we were searching by pool name or guid, we still need to 202224e697d4Sck153898 * post-process the list to deal with pool state and possible 202324e697d4Sck153898 * duplicate names. 202424e697d4Sck153898 */ 2025fa9e4066Sahrens err = 0; 2026fa9e4066Sahrens elem = NULL; 202799653d4eSeschrock first = B_TRUE; 2028fa9e4066Sahrens while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 2029fa9e4066Sahrens 2030fa9e4066Sahrens verify(nvpair_value_nvlist(elem, &config) == 0); 2031fa9e4066Sahrens 20324c58d714Sdarrenm verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 20334c58d714Sdarrenm &pool_state) == 0); 20344c58d714Sdarrenm if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) 20354c58d714Sdarrenm continue; 20364c58d714Sdarrenm if (do_destroyed && pool_state != POOL_STATE_DESTROYED) 20374c58d714Sdarrenm continue; 20384c58d714Sdarrenm 2039468c413aSTim Haley verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY, 2040468c413aSTim Haley policy) == 0); 2041468c413aSTim Haley 2042fa9e4066Sahrens if (argc == 0) { 2043fa9e4066Sahrens if (first) 204499653d4eSeschrock first = B_FALSE; 20453bb79becSeschrock else if (!do_all) 2046fa9e4066Sahrens (void) printf("\n"); 2047fa9e4066Sahrens 2048468c413aSTim Haley if (do_all) { 2049fa9e4066Sahrens err |= do_import(config, NULL, mntopts, 20504b964adaSGeorge Wilson props, flags); 2051468c413aSTim Haley } else { 2052fa9e4066Sahrens show_import(config); 2053468c413aSTim Haley } 2054fa9e4066Sahrens } else if (searchname != NULL) { 2055fa9e4066Sahrens char *name; 2056fa9e4066Sahrens 2057fa9e4066Sahrens /* 2058fa9e4066Sahrens * We are searching for a pool based on name. 2059fa9e4066Sahrens */ 2060fa9e4066Sahrens verify(nvlist_lookup_string(config, 2061fa9e4066Sahrens ZPOOL_CONFIG_POOL_NAME, &name) == 0); 2062fa9e4066Sahrens 2063fa9e4066Sahrens if (strcmp(name, searchname) == 0) { 2064fa9e4066Sahrens if (found_config != NULL) { 2065fa9e4066Sahrens (void) fprintf(stderr, gettext( 2066fa9e4066Sahrens "cannot import '%s': more than " 2067fa9e4066Sahrens "one matching pool\n"), searchname); 2068fa9e4066Sahrens (void) fprintf(stderr, gettext( 2069fa9e4066Sahrens "import by numeric ID instead\n")); 207099653d4eSeschrock err = B_TRUE; 2071fa9e4066Sahrens } 2072fa9e4066Sahrens found_config = config; 2073fa9e4066Sahrens } 2074fa9e4066Sahrens } else { 2075fa9e4066Sahrens uint64_t guid; 2076fa9e4066Sahrens 2077fa9e4066Sahrens /* 2078fa9e4066Sahrens * Search for a pool by guid. 2079fa9e4066Sahrens */ 2080fa9e4066Sahrens verify(nvlist_lookup_uint64(config, 2081fa9e4066Sahrens ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 2082fa9e4066Sahrens 2083fa9e4066Sahrens if (guid == searchguid) 2084fa9e4066Sahrens found_config = config; 2085fa9e4066Sahrens } 2086fa9e4066Sahrens } 2087fa9e4066Sahrens 2088fa9e4066Sahrens /* 2089fa9e4066Sahrens * If we were searching for a specific pool, verify that we found a 2090fa9e4066Sahrens * pool, and then do the import. 2091fa9e4066Sahrens */ 2092fa9e4066Sahrens if (argc != 0 && err == 0) { 2093fa9e4066Sahrens if (found_config == NULL) { 2094fa9e4066Sahrens (void) fprintf(stderr, gettext("cannot import '%s': " 2095fa9e4066Sahrens "no such pool available\n"), argv[0]); 209699653d4eSeschrock err = B_TRUE; 2097fa9e4066Sahrens } else { 2098fa9e4066Sahrens err |= do_import(found_config, argc == 1 ? NULL : 20994b964adaSGeorge Wilson argv[1], mntopts, props, flags); 2100fa9e4066Sahrens } 2101fa9e4066Sahrens } 2102fa9e4066Sahrens 2103fa9e4066Sahrens /* 2104fa9e4066Sahrens * If we were just looking for pools, report an error if none were 2105fa9e4066Sahrens * found. 2106fa9e4066Sahrens */ 2107fa9e4066Sahrens if (argc == 0 && first) 2108fa9e4066Sahrens (void) fprintf(stderr, 2109fa9e4066Sahrens gettext("no pools available to import\n")); 2110fa9e4066Sahrens 2111ecd6cf80Smarks error: 2112ecd6cf80Smarks nvlist_free(props); 2113fa9e4066Sahrens nvlist_free(pools); 2114468c413aSTim Haley nvlist_free(policy); 211599653d4eSeschrock free(searchdirs); 2116fa9e4066Sahrens 2117fa9e4066Sahrens return (err ? 1 : 0); 2118fa9e4066Sahrens } 2119fa9e4066Sahrens 2120fa9e4066Sahrens typedef struct iostat_cbdata { 21214263d13fSGeorge Wilson boolean_t cb_verbose; 2122fa9e4066Sahrens int cb_namewidth; 21234263d13fSGeorge Wilson int cb_iteration; 21244263d13fSGeorge Wilson zpool_list_t *cb_list; 2125fa9e4066Sahrens } iostat_cbdata_t; 2126fa9e4066Sahrens 2127fa9e4066Sahrens static void 2128fa9e4066Sahrens print_iostat_separator(iostat_cbdata_t *cb) 2129fa9e4066Sahrens { 2130fa9e4066Sahrens int i = 0; 2131fa9e4066Sahrens 2132fa9e4066Sahrens for (i = 0; i < cb->cb_namewidth; i++) 2133fa9e4066Sahrens (void) printf("-"); 2134fa9e4066Sahrens (void) printf(" ----- ----- ----- ----- ----- -----\n"); 2135fa9e4066Sahrens } 2136fa9e4066Sahrens 2137fa9e4066Sahrens static void 2138fa9e4066Sahrens print_iostat_header(iostat_cbdata_t *cb) 2139fa9e4066Sahrens { 2140fa9e4066Sahrens (void) printf("%*s capacity operations bandwidth\n", 2141fa9e4066Sahrens cb->cb_namewidth, ""); 2142485bbbf5SGeorge Wilson (void) printf("%-*s alloc free read write read write\n", 2143fa9e4066Sahrens cb->cb_namewidth, "pool"); 2144fa9e4066Sahrens print_iostat_separator(cb); 2145fa9e4066Sahrens } 2146fa9e4066Sahrens 2147fa9e4066Sahrens /* 2148fa9e4066Sahrens * Display a single statistic. 2149fa9e4066Sahrens */ 2150990b4856Slling static void 2151fa9e4066Sahrens print_one_stat(uint64_t value) 2152fa9e4066Sahrens { 2153fa9e4066Sahrens char buf[64]; 2154fa9e4066Sahrens 2155fa9e4066Sahrens zfs_nicenum(value, buf, sizeof (buf)); 2156fa9e4066Sahrens (void) printf(" %5s", buf); 2157fa9e4066Sahrens } 2158fa9e4066Sahrens 2159fa9e4066Sahrens /* 2160fa9e4066Sahrens * Print out all the statistics for the given vdev. This can either be the 2161fa9e4066Sahrens * toplevel configuration, or called recursively. If 'name' is NULL, then this 2162fa9e4066Sahrens * is a verbose output, and we don't want to display the toplevel pool stats. 2163fa9e4066Sahrens */ 2164fa9e4066Sahrens void 2165c67d9675Seschrock print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, 2166c67d9675Seschrock nvlist_t *newnv, iostat_cbdata_t *cb, int depth) 2167fa9e4066Sahrens { 2168fa9e4066Sahrens nvlist_t **oldchild, **newchild; 2169fa9e4066Sahrens uint_t c, children; 2170fa9e4066Sahrens vdev_stat_t *oldvs, *newvs; 2171fa9e4066Sahrens vdev_stat_t zerovs = { 0 }; 2172fa9e4066Sahrens uint64_t tdelta; 2173fa9e4066Sahrens double scale; 2174afefbcddSeschrock char *vname; 2175fa9e4066Sahrens 2176fa9e4066Sahrens if (oldnv != NULL) { 21773f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(oldnv, 21783f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); 2179fa9e4066Sahrens } else { 2180fa9e4066Sahrens oldvs = &zerovs; 2181fa9e4066Sahrens } 2182fa9e4066Sahrens 21833f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, 2184fa9e4066Sahrens (uint64_t **)&newvs, &c) == 0); 2185fa9e4066Sahrens 2186fa9e4066Sahrens if (strlen(name) + depth > cb->cb_namewidth) 2187fa9e4066Sahrens (void) printf("%*s%s", depth, "", name); 2188fa9e4066Sahrens else 2189fa9e4066Sahrens (void) printf("%*s%s%*s", depth, "", name, 2190fa9e4066Sahrens (int)(cb->cb_namewidth - strlen(name) - depth), ""); 2191fa9e4066Sahrens 2192fa9e4066Sahrens tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; 2193fa9e4066Sahrens 2194fa9e4066Sahrens if (tdelta == 0) 2195fa9e4066Sahrens scale = 1.0; 2196fa9e4066Sahrens else 2197fa9e4066Sahrens scale = (double)NANOSEC / tdelta; 2198fa9e4066Sahrens 2199fa9e4066Sahrens /* only toplevel vdevs have capacity stats */ 2200fa9e4066Sahrens if (newvs->vs_space == 0) { 2201fa9e4066Sahrens (void) printf(" - -"); 2202fa9e4066Sahrens } else { 2203fa9e4066Sahrens print_one_stat(newvs->vs_alloc); 2204fa9e4066Sahrens print_one_stat(newvs->vs_space - newvs->vs_alloc); 2205fa9e4066Sahrens } 2206fa9e4066Sahrens 2207fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] - 2208fa9e4066Sahrens oldvs->vs_ops[ZIO_TYPE_READ]))); 2209fa9e4066Sahrens 2210fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] - 2211fa9e4066Sahrens oldvs->vs_ops[ZIO_TYPE_WRITE]))); 2212fa9e4066Sahrens 2213fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] - 2214fa9e4066Sahrens oldvs->vs_bytes[ZIO_TYPE_READ]))); 2215fa9e4066Sahrens 2216fa9e4066Sahrens print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] - 2217fa9e4066Sahrens oldvs->vs_bytes[ZIO_TYPE_WRITE]))); 2218fa9e4066Sahrens 2219fa9e4066Sahrens (void) printf("\n"); 2220fa9e4066Sahrens 2221fa9e4066Sahrens if (!cb->cb_verbose) 2222fa9e4066Sahrens return; 2223fa9e4066Sahrens 2224fa9e4066Sahrens if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, 2225fa9e4066Sahrens &newchild, &children) != 0) 2226fa9e4066Sahrens return; 2227fa9e4066Sahrens 2228fa9e4066Sahrens if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, 2229fa9e4066Sahrens &oldchild, &c) != 0) 2230fa9e4066Sahrens return; 2231fa9e4066Sahrens 2232afefbcddSeschrock for (c = 0; c < children; c++) { 22339d439f90SMike Harsch uint64_t ishole = B_FALSE, islog = B_FALSE; 22343f9d6ad7SLin Ling 22359d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, 22369d439f90SMike Harsch &ishole); 22379d439f90SMike Harsch 22389d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, 22399d439f90SMike Harsch &islog); 22409d439f90SMike Harsch 22419d439f90SMike Harsch if (ishole || islog) 22423f9d6ad7SLin Ling continue; 22433f9d6ad7SLin Ling 224488ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE); 2245c67d9675Seschrock print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 2246afefbcddSeschrock newchild[c], cb, depth + 2); 2247afefbcddSeschrock free(vname); 2248afefbcddSeschrock } 2249fa94a07fSbrendan 2250fa94a07fSbrendan /* 22519d439f90SMike Harsch * Log device section 22529d439f90SMike Harsch */ 22539d439f90SMike Harsch 22549d439f90SMike Harsch if (num_logs(newnv) > 0) { 22559d439f90SMike Harsch (void) printf("%-*s - - - - - " 22569d439f90SMike Harsch "-\n", cb->cb_namewidth, "logs"); 22579d439f90SMike Harsch 22589d439f90SMike Harsch for (c = 0; c < children; c++) { 22599d439f90SMike Harsch uint64_t islog = B_FALSE; 22609d439f90SMike Harsch (void) nvlist_lookup_uint64(newchild[c], 22619d439f90SMike Harsch ZPOOL_CONFIG_IS_LOG, &islog); 22629d439f90SMike Harsch 22639d439f90SMike Harsch if (islog) { 22649d439f90SMike Harsch vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 22659d439f90SMike Harsch B_FALSE); 22669d439f90SMike Harsch print_vdev_stats(zhp, vname, oldnv ? 22679d439f90SMike Harsch oldchild[c] : NULL, newchild[c], 22689d439f90SMike Harsch cb, depth + 2); 22699d439f90SMike Harsch free(vname); 22709d439f90SMike Harsch } 22719d439f90SMike Harsch } 22729d439f90SMike Harsch 22739d439f90SMike Harsch } 22749d439f90SMike Harsch 22759d439f90SMike Harsch /* 2276fa94a07fSbrendan * Include level 2 ARC devices in iostat output 2277fa94a07fSbrendan */ 2278fa94a07fSbrendan if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, 2279fa94a07fSbrendan &newchild, &children) != 0) 2280fa94a07fSbrendan return; 2281fa94a07fSbrendan 2282fa94a07fSbrendan if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, 2283fa94a07fSbrendan &oldchild, &c) != 0) 2284fa94a07fSbrendan return; 2285fa94a07fSbrendan 2286fa94a07fSbrendan if (children > 0) { 2287fa94a07fSbrendan (void) printf("%-*s - - - - - " 2288fa94a07fSbrendan "-\n", cb->cb_namewidth, "cache"); 2289fa94a07fSbrendan for (c = 0; c < children; c++) { 229088ecc943SGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 229188ecc943SGeorge Wilson B_FALSE); 2292fa94a07fSbrendan print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 2293fa94a07fSbrendan newchild[c], cb, depth + 2); 2294fa94a07fSbrendan free(vname); 2295fa94a07fSbrendan } 2296fa94a07fSbrendan } 2297fa9e4066Sahrens } 2298fa9e4066Sahrens 2299088e9d47Seschrock static int 2300088e9d47Seschrock refresh_iostat(zpool_handle_t *zhp, void *data) 2301088e9d47Seschrock { 2302088e9d47Seschrock iostat_cbdata_t *cb = data; 230394de1d4cSeschrock boolean_t missing; 2304088e9d47Seschrock 2305088e9d47Seschrock /* 2306088e9d47Seschrock * If the pool has disappeared, remove it from the list and continue. 2307088e9d47Seschrock */ 230894de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) 230994de1d4cSeschrock return (-1); 231094de1d4cSeschrock 231194de1d4cSeschrock if (missing) 2312088e9d47Seschrock pool_list_remove(cb->cb_list, zhp); 2313088e9d47Seschrock 2314088e9d47Seschrock return (0); 2315088e9d47Seschrock } 2316088e9d47Seschrock 2317fa9e4066Sahrens /* 2318fa9e4066Sahrens * Callback to print out the iostats for the given pool. 2319fa9e4066Sahrens */ 2320fa9e4066Sahrens int 2321fa9e4066Sahrens print_iostat(zpool_handle_t *zhp, void *data) 2322fa9e4066Sahrens { 2323fa9e4066Sahrens iostat_cbdata_t *cb = data; 2324fa9e4066Sahrens nvlist_t *oldconfig, *newconfig; 2325fa9e4066Sahrens nvlist_t *oldnvroot, *newnvroot; 2326fa9e4066Sahrens 2327088e9d47Seschrock newconfig = zpool_get_config(zhp, &oldconfig); 2328fa9e4066Sahrens 2329088e9d47Seschrock if (cb->cb_iteration == 1) 2330fa9e4066Sahrens oldconfig = NULL; 2331fa9e4066Sahrens 2332fa9e4066Sahrens verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, 2333fa9e4066Sahrens &newnvroot) == 0); 2334fa9e4066Sahrens 2335088e9d47Seschrock if (oldconfig == NULL) 2336fa9e4066Sahrens oldnvroot = NULL; 2337088e9d47Seschrock else 2338088e9d47Seschrock verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, 2339088e9d47Seschrock &oldnvroot) == 0); 2340fa9e4066Sahrens 2341fa9e4066Sahrens /* 2342fa9e4066Sahrens * Print out the statistics for the pool. 2343fa9e4066Sahrens */ 2344c67d9675Seschrock print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0); 2345fa9e4066Sahrens 2346fa9e4066Sahrens if (cb->cb_verbose) 2347fa9e4066Sahrens print_iostat_separator(cb); 2348fa9e4066Sahrens 2349fa9e4066Sahrens return (0); 2350fa9e4066Sahrens } 2351fa9e4066Sahrens 2352fa9e4066Sahrens int 2353fa9e4066Sahrens get_namewidth(zpool_handle_t *zhp, void *data) 2354fa9e4066Sahrens { 2355fa9e4066Sahrens iostat_cbdata_t *cb = data; 2356fa9e4066Sahrens nvlist_t *config, *nvroot; 2357fa9e4066Sahrens 2358088e9d47Seschrock if ((config = zpool_get_config(zhp, NULL)) != NULL) { 2359fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2360fa9e4066Sahrens &nvroot) == 0); 2361fa9e4066Sahrens if (!cb->cb_verbose) 2362fa9e4066Sahrens cb->cb_namewidth = strlen(zpool_get_name(zhp)); 2363fa9e4066Sahrens else 2364e1d5e507SFrederik Wessels cb->cb_namewidth = max_width(zhp, nvroot, 0, 2365e1d5e507SFrederik Wessels cb->cb_namewidth); 2366fa9e4066Sahrens } 2367fa9e4066Sahrens 2368fa9e4066Sahrens /* 2369fa9e4066Sahrens * The width must fall into the range [10,38]. The upper limit is the 2370fa9e4066Sahrens * maximum we can have and still fit in 80 columns. 2371fa9e4066Sahrens */ 2372fa9e4066Sahrens if (cb->cb_namewidth < 10) 2373fa9e4066Sahrens cb->cb_namewidth = 10; 2374fa9e4066Sahrens if (cb->cb_namewidth > 38) 2375fa9e4066Sahrens cb->cb_namewidth = 38; 2376fa9e4066Sahrens 2377fa9e4066Sahrens return (0); 2378fa9e4066Sahrens } 2379fa9e4066Sahrens 2380fa9e4066Sahrens /* 23813f9d6ad7SLin Ling * Parse the input string, get the 'interval' and 'count' value if there is one. 2382fa9e4066Sahrens */ 23833f9d6ad7SLin Ling static void 23843f9d6ad7SLin Ling get_interval_count(int *argcp, char **argv, unsigned long *iv, 23853f9d6ad7SLin Ling unsigned long *cnt) 2386fa9e4066Sahrens { 2387fa9e4066Sahrens unsigned long interval = 0, count = 0; 23883f9d6ad7SLin Ling int argc = *argcp, errno; 2389fa9e4066Sahrens 2390fa9e4066Sahrens /* 2391fa9e4066Sahrens * Determine if the last argument is an integer or a pool name 2392fa9e4066Sahrens */ 2393fa9e4066Sahrens if (argc > 0 && isdigit(argv[argc - 1][0])) { 2394fa9e4066Sahrens char *end; 2395fa9e4066Sahrens 2396fa9e4066Sahrens errno = 0; 2397fa9e4066Sahrens interval = strtoul(argv[argc - 1], &end, 10); 2398fa9e4066Sahrens 2399fa9e4066Sahrens if (*end == '\0' && errno == 0) { 2400fa9e4066Sahrens if (interval == 0) { 2401fa9e4066Sahrens (void) fprintf(stderr, gettext("interval " 2402fa9e4066Sahrens "cannot be zero\n")); 240399653d4eSeschrock usage(B_FALSE); 2404fa9e4066Sahrens } 2405fa9e4066Sahrens /* 2406fa9e4066Sahrens * Ignore the last parameter 2407fa9e4066Sahrens */ 2408fa9e4066Sahrens argc--; 2409fa9e4066Sahrens } else { 2410fa9e4066Sahrens /* 2411fa9e4066Sahrens * If this is not a valid number, just plow on. The 2412fa9e4066Sahrens * user will get a more informative error message later 2413fa9e4066Sahrens * on. 2414fa9e4066Sahrens */ 2415fa9e4066Sahrens interval = 0; 2416fa9e4066Sahrens } 2417fa9e4066Sahrens } 2418fa9e4066Sahrens 2419fa9e4066Sahrens /* 2420fa9e4066Sahrens * If the last argument is also an integer, then we have both a count 24213f9d6ad7SLin Ling * and an interval. 2422fa9e4066Sahrens */ 2423fa9e4066Sahrens if (argc > 0 && isdigit(argv[argc - 1][0])) { 2424fa9e4066Sahrens char *end; 2425fa9e4066Sahrens 2426fa9e4066Sahrens errno = 0; 2427fa9e4066Sahrens count = interval; 2428fa9e4066Sahrens interval = strtoul(argv[argc - 1], &end, 10); 2429fa9e4066Sahrens 2430fa9e4066Sahrens if (*end == '\0' && errno == 0) { 2431fa9e4066Sahrens if (interval == 0) { 2432fa9e4066Sahrens (void) fprintf(stderr, gettext("interval " 2433fa9e4066Sahrens "cannot be zero\n")); 243499653d4eSeschrock usage(B_FALSE); 2435fa9e4066Sahrens } 2436fa9e4066Sahrens 2437fa9e4066Sahrens /* 2438fa9e4066Sahrens * Ignore the last parameter 2439fa9e4066Sahrens */ 2440fa9e4066Sahrens argc--; 2441fa9e4066Sahrens } else { 2442fa9e4066Sahrens interval = 0; 2443fa9e4066Sahrens } 2444fa9e4066Sahrens } 2445fa9e4066Sahrens 24463f9d6ad7SLin Ling *iv = interval; 24473f9d6ad7SLin Ling *cnt = count; 24483f9d6ad7SLin Ling *argcp = argc; 24493f9d6ad7SLin Ling } 24503f9d6ad7SLin Ling 24513f9d6ad7SLin Ling static void 24523f9d6ad7SLin Ling get_timestamp_arg(char c) 24533f9d6ad7SLin Ling { 24543f9d6ad7SLin Ling if (c == 'u') 24553f9d6ad7SLin Ling timestamp_fmt = UDATE; 24563f9d6ad7SLin Ling else if (c == 'd') 24573f9d6ad7SLin Ling timestamp_fmt = DDATE; 24583f9d6ad7SLin Ling else 24593f9d6ad7SLin Ling usage(B_FALSE); 24603f9d6ad7SLin Ling } 24613f9d6ad7SLin Ling 24623f9d6ad7SLin Ling /* 24633f9d6ad7SLin Ling * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]] 24643f9d6ad7SLin Ling * 24653f9d6ad7SLin Ling * -v Display statistics for individual vdevs 24663f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format 24673f9d6ad7SLin Ling * 24683f9d6ad7SLin Ling * This command can be tricky because we want to be able to deal with pool 24693f9d6ad7SLin Ling * creation/destruction as well as vdev configuration changes. The bulk of this 24703f9d6ad7SLin Ling * processing is handled by the pool_list_* routines in zpool_iter.c. We rely 24713f9d6ad7SLin Ling * on pool_list_update() to detect the addition of new pools. Configuration 24723f9d6ad7SLin Ling * changes are all handled within libzfs. 24733f9d6ad7SLin Ling */ 24743f9d6ad7SLin Ling int 24753f9d6ad7SLin Ling zpool_do_iostat(int argc, char **argv) 24763f9d6ad7SLin Ling { 24773f9d6ad7SLin Ling int c; 24783f9d6ad7SLin Ling int ret; 24793f9d6ad7SLin Ling int npools; 24803f9d6ad7SLin Ling unsigned long interval = 0, count = 0; 24813f9d6ad7SLin Ling zpool_list_t *list; 24823f9d6ad7SLin Ling boolean_t verbose = B_FALSE; 24833f9d6ad7SLin Ling iostat_cbdata_t cb; 24843f9d6ad7SLin Ling 24853f9d6ad7SLin Ling /* check options */ 24863f9d6ad7SLin Ling while ((c = getopt(argc, argv, "T:v")) != -1) { 24873f9d6ad7SLin Ling switch (c) { 24883f9d6ad7SLin Ling case 'T': 24893f9d6ad7SLin Ling get_timestamp_arg(*optarg); 24903f9d6ad7SLin Ling break; 24913f9d6ad7SLin Ling case 'v': 24923f9d6ad7SLin Ling verbose = B_TRUE; 24933f9d6ad7SLin Ling break; 24943f9d6ad7SLin Ling case '?': 24953f9d6ad7SLin Ling (void) fprintf(stderr, gettext("invalid option '%c'\n"), 24963f9d6ad7SLin Ling optopt); 24973f9d6ad7SLin Ling usage(B_FALSE); 24983f9d6ad7SLin Ling } 24993f9d6ad7SLin Ling } 25003f9d6ad7SLin Ling 25013f9d6ad7SLin Ling argc -= optind; 25023f9d6ad7SLin Ling argv += optind; 25033f9d6ad7SLin Ling 25043f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count); 25053f9d6ad7SLin Ling 2506fa9e4066Sahrens /* 2507fa9e4066Sahrens * Construct the list of all interesting pools. 2508fa9e4066Sahrens */ 2509fa9e4066Sahrens ret = 0; 2510b1b8ab34Slling if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL) 2511fa9e4066Sahrens return (1); 2512fa9e4066Sahrens 251399653d4eSeschrock if (pool_list_count(list) == 0 && argc != 0) { 251499653d4eSeschrock pool_list_free(list); 2515fa9e4066Sahrens return (1); 251699653d4eSeschrock } 2517fa9e4066Sahrens 2518fa9e4066Sahrens if (pool_list_count(list) == 0 && interval == 0) { 251999653d4eSeschrock pool_list_free(list); 2520fa9e4066Sahrens (void) fprintf(stderr, gettext("no pools available\n")); 2521fa9e4066Sahrens return (1); 2522fa9e4066Sahrens } 2523fa9e4066Sahrens 2524fa9e4066Sahrens /* 2525fa9e4066Sahrens * Enter the main iostat loop. 2526fa9e4066Sahrens */ 2527fa9e4066Sahrens cb.cb_list = list; 2528fa9e4066Sahrens cb.cb_verbose = verbose; 2529fa9e4066Sahrens cb.cb_iteration = 0; 2530fa9e4066Sahrens cb.cb_namewidth = 0; 2531fa9e4066Sahrens 2532fa9e4066Sahrens for (;;) { 2533fa9e4066Sahrens pool_list_update(list); 2534fa9e4066Sahrens 2535fa9e4066Sahrens if ((npools = pool_list_count(list)) == 0) 2536fa9e4066Sahrens break; 2537fa9e4066Sahrens 2538fa9e4066Sahrens /* 2539088e9d47Seschrock * Refresh all statistics. This is done as an explicit step 2540088e9d47Seschrock * before calculating the maximum name width, so that any 2541088e9d47Seschrock * configuration changes are properly accounted for. 2542088e9d47Seschrock */ 254399653d4eSeschrock (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb); 2544088e9d47Seschrock 2545088e9d47Seschrock /* 2546fa9e4066Sahrens * Iterate over all pools to determine the maximum width 2547fa9e4066Sahrens * for the pool / device name column across all pools. 2548fa9e4066Sahrens */ 2549fa9e4066Sahrens cb.cb_namewidth = 0; 255099653d4eSeschrock (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); 2551fa9e4066Sahrens 255226fd7700SKrishnendu Sadhukhan - Sun Microsystems if (timestamp_fmt != NODATE) 255326fd7700SKrishnendu Sadhukhan - Sun Microsystems print_timestamp(timestamp_fmt); 255426fd7700SKrishnendu Sadhukhan - Sun Microsystems 2555fa9e4066Sahrens /* 2556fa9e4066Sahrens * If it's the first time, or verbose mode, print the header. 2557fa9e4066Sahrens */ 2558fa9e4066Sahrens if (++cb.cb_iteration == 1 || verbose) 2559fa9e4066Sahrens print_iostat_header(&cb); 2560fa9e4066Sahrens 256199653d4eSeschrock (void) pool_list_iter(list, B_FALSE, print_iostat, &cb); 2562fa9e4066Sahrens 2563fa9e4066Sahrens /* 2564fa9e4066Sahrens * If there's more than one pool, and we're not in verbose mode 2565fa9e4066Sahrens * (which prints a separator for us), then print a separator. 2566fa9e4066Sahrens */ 2567fa9e4066Sahrens if (npools > 1 && !verbose) 2568fa9e4066Sahrens print_iostat_separator(&cb); 2569fa9e4066Sahrens 2570fa9e4066Sahrens if (verbose) 2571fa9e4066Sahrens (void) printf("\n"); 2572fa9e4066Sahrens 257339c23413Seschrock /* 257439c23413Seschrock * Flush the output so that redirection to a file isn't buffered 257539c23413Seschrock * indefinitely. 257639c23413Seschrock */ 257739c23413Seschrock (void) fflush(stdout); 257839c23413Seschrock 2579fa9e4066Sahrens if (interval == 0) 2580fa9e4066Sahrens break; 2581fa9e4066Sahrens 2582fa9e4066Sahrens if (count != 0 && --count == 0) 2583fa9e4066Sahrens break; 2584fa9e4066Sahrens 2585fa9e4066Sahrens (void) sleep(interval); 2586fa9e4066Sahrens } 2587fa9e4066Sahrens 2588fa9e4066Sahrens pool_list_free(list); 2589fa9e4066Sahrens 2590fa9e4066Sahrens return (ret); 2591fa9e4066Sahrens } 2592fa9e4066Sahrens 2593fa9e4066Sahrens typedef struct list_cbdata { 25944263d13fSGeorge Wilson boolean_t cb_verbose; 25954263d13fSGeorge Wilson int cb_namewidth; 259699653d4eSeschrock boolean_t cb_scripted; 2597990b4856Slling zprop_list_t *cb_proplist; 2598fa9e4066Sahrens } list_cbdata_t; 2599fa9e4066Sahrens 2600fa9e4066Sahrens /* 2601fa9e4066Sahrens * Given a list of columns to display, output appropriate headers for each one. 2602fa9e4066Sahrens */ 2603990b4856Slling static void 26044263d13fSGeorge Wilson print_header(list_cbdata_t *cb) 2605fa9e4066Sahrens { 26064263d13fSGeorge Wilson zprop_list_t *pl = cb->cb_proplist; 2607ad135b5dSChristopher Siden char headerbuf[ZPOOL_MAXPROPLEN]; 2608990b4856Slling const char *header; 2609990b4856Slling boolean_t first = B_TRUE; 2610990b4856Slling boolean_t right_justify; 26114263d13fSGeorge Wilson size_t width = 0; 2612fa9e4066Sahrens 2613990b4856Slling for (; pl != NULL; pl = pl->pl_next) { 26144263d13fSGeorge Wilson width = pl->pl_width; 26154263d13fSGeorge Wilson if (first && cb->cb_verbose) { 26164263d13fSGeorge Wilson /* 26174263d13fSGeorge Wilson * Reset the width to accommodate the verbose listing 26184263d13fSGeorge Wilson * of devices. 26194263d13fSGeorge Wilson */ 26204263d13fSGeorge Wilson width = cb->cb_namewidth; 26214263d13fSGeorge Wilson } 26224263d13fSGeorge Wilson 2623990b4856Slling if (!first) 2624fa9e4066Sahrens (void) printf(" "); 2625fa9e4066Sahrens else 2626990b4856Slling first = B_FALSE; 2627fa9e4066Sahrens 2628ad135b5dSChristopher Siden right_justify = B_FALSE; 2629ad135b5dSChristopher Siden if (pl->pl_prop != ZPROP_INVAL) { 2630990b4856Slling header = zpool_prop_column_name(pl->pl_prop); 2631990b4856Slling right_justify = zpool_prop_align_right(pl->pl_prop); 2632ad135b5dSChristopher Siden } else { 2633ad135b5dSChristopher Siden int i; 2634ad135b5dSChristopher Siden 2635ad135b5dSChristopher Siden for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 2636ad135b5dSChristopher Siden headerbuf[i] = toupper(pl->pl_user_prop[i]); 2637ad135b5dSChristopher Siden headerbuf[i] = '\0'; 2638ad135b5dSChristopher Siden header = headerbuf; 2639ad135b5dSChristopher Siden } 2640990b4856Slling 2641990b4856Slling if (pl->pl_next == NULL && !right_justify) 2642990b4856Slling (void) printf("%s", header); 2643990b4856Slling else if (right_justify) 26444263d13fSGeorge Wilson (void) printf("%*s", width, header); 2645990b4856Slling else 26464263d13fSGeorge Wilson (void) printf("%-*s", width, header); 26474263d13fSGeorge Wilson 2648fa9e4066Sahrens } 2649fa9e4066Sahrens 2650fa9e4066Sahrens (void) printf("\n"); 2651fa9e4066Sahrens } 2652fa9e4066Sahrens 2653990b4856Slling /* 2654990b4856Slling * Given a pool and a list of properties, print out all the properties according 2655990b4856Slling * to the described layout. 2656990b4856Slling */ 2657990b4856Slling static void 26584263d13fSGeorge Wilson print_pool(zpool_handle_t *zhp, list_cbdata_t *cb) 2659990b4856Slling { 26604263d13fSGeorge Wilson zprop_list_t *pl = cb->cb_proplist; 2661990b4856Slling boolean_t first = B_TRUE; 2662990b4856Slling char property[ZPOOL_MAXPROPLEN]; 2663990b4856Slling char *propstr; 2664990b4856Slling boolean_t right_justify; 26654263d13fSGeorge Wilson size_t width; 2666990b4856Slling 2667990b4856Slling for (; pl != NULL; pl = pl->pl_next) { 26684263d13fSGeorge Wilson 26694263d13fSGeorge Wilson width = pl->pl_width; 26704263d13fSGeorge Wilson if (first && cb->cb_verbose) { 26714263d13fSGeorge Wilson /* 26724263d13fSGeorge Wilson * Reset the width to accommodate the verbose listing 26734263d13fSGeorge Wilson * of devices. 26744263d13fSGeorge Wilson */ 26754263d13fSGeorge Wilson width = cb->cb_namewidth; 26764263d13fSGeorge Wilson } 26774263d13fSGeorge Wilson 2678990b4856Slling if (!first) { 26794263d13fSGeorge Wilson if (cb->cb_scripted) 2680990b4856Slling (void) printf("\t"); 2681990b4856Slling else 2682990b4856Slling (void) printf(" "); 2683990b4856Slling } else { 2684990b4856Slling first = B_FALSE; 2685990b4856Slling } 2686990b4856Slling 2687990b4856Slling right_justify = B_FALSE; 2688990b4856Slling if (pl->pl_prop != ZPROP_INVAL) { 26894263d13fSGeorge Wilson if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ && 26904263d13fSGeorge Wilson zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0) 26914263d13fSGeorge Wilson propstr = "-"; 26924263d13fSGeorge Wilson else if (zpool_get_prop(zhp, pl->pl_prop, property, 2693990b4856Slling sizeof (property), NULL) != 0) 2694990b4856Slling propstr = "-"; 2695990b4856Slling else 2696990b4856Slling propstr = property; 2697990b4856Slling 2698990b4856Slling right_justify = zpool_prop_align_right(pl->pl_prop); 2699ad135b5dSChristopher Siden } else if ((zpool_prop_feature(pl->pl_user_prop) || 2700ad135b5dSChristopher Siden zpool_prop_unsupported(pl->pl_user_prop)) && 2701ad135b5dSChristopher Siden zpool_prop_get_feature(zhp, pl->pl_user_prop, property, 2702ad135b5dSChristopher Siden sizeof (property)) == 0) { 2703ad135b5dSChristopher Siden propstr = property; 2704990b4856Slling } else { 2705990b4856Slling propstr = "-"; 2706990b4856Slling } 2707990b4856Slling 2708990b4856Slling 2709990b4856Slling /* 2710990b4856Slling * If this is being called in scripted mode, or if this is the 2711990b4856Slling * last column and it is left-justified, don't include a width 2712990b4856Slling * format specifier. 2713990b4856Slling */ 27144263d13fSGeorge Wilson if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 2715990b4856Slling (void) printf("%s", propstr); 2716990b4856Slling else if (right_justify) 2717990b4856Slling (void) printf("%*s", width, propstr); 2718990b4856Slling else 2719990b4856Slling (void) printf("%-*s", width, propstr); 2720990b4856Slling } 2721990b4856Slling 2722990b4856Slling (void) printf("\n"); 2723990b4856Slling } 2724990b4856Slling 27254263d13fSGeorge Wilson static void 27264263d13fSGeorge Wilson print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted) 27274263d13fSGeorge Wilson { 27284263d13fSGeorge Wilson char propval[64]; 27294263d13fSGeorge Wilson boolean_t fixed; 27304263d13fSGeorge Wilson size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); 27314263d13fSGeorge Wilson 27324263d13fSGeorge Wilson zfs_nicenum(value, propval, sizeof (propval)); 27334263d13fSGeorge Wilson 27344263d13fSGeorge Wilson if (prop == ZPOOL_PROP_EXPANDSZ && value == 0) 27354263d13fSGeorge Wilson (void) strlcpy(propval, "-", sizeof (propval)); 27364263d13fSGeorge Wilson 27374263d13fSGeorge Wilson if (scripted) 27384263d13fSGeorge Wilson (void) printf("\t%s", propval); 27394263d13fSGeorge Wilson else 27404263d13fSGeorge Wilson (void) printf(" %*s", width, propval); 27414263d13fSGeorge Wilson } 27424263d13fSGeorge Wilson 27434263d13fSGeorge Wilson void 27444263d13fSGeorge Wilson print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 27454263d13fSGeorge Wilson list_cbdata_t *cb, int depth) 27464263d13fSGeorge Wilson { 27474263d13fSGeorge Wilson nvlist_t **child; 27484263d13fSGeorge Wilson vdev_stat_t *vs; 27494263d13fSGeorge Wilson uint_t c, children; 27504263d13fSGeorge Wilson char *vname; 27514263d13fSGeorge Wilson boolean_t scripted = cb->cb_scripted; 27524263d13fSGeorge Wilson 27534263d13fSGeorge Wilson verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 27544263d13fSGeorge Wilson (uint64_t **)&vs, &c) == 0); 27554263d13fSGeorge Wilson 27564263d13fSGeorge Wilson if (name != NULL) { 27574263d13fSGeorge Wilson if (scripted) 27584263d13fSGeorge Wilson (void) printf("\t%s", name); 27594263d13fSGeorge Wilson else if (strlen(name) + depth > cb->cb_namewidth) 27604263d13fSGeorge Wilson (void) printf("%*s%s", depth, "", name); 27614263d13fSGeorge Wilson else 27624263d13fSGeorge Wilson (void) printf("%*s%s%*s", depth, "", name, 27634263d13fSGeorge Wilson (int)(cb->cb_namewidth - strlen(name) - depth), ""); 27644263d13fSGeorge Wilson 27654263d13fSGeorge Wilson /* only toplevel vdevs have capacity stats */ 27664263d13fSGeorge Wilson if (vs->vs_space == 0) { 27674263d13fSGeorge Wilson if (scripted) 27684263d13fSGeorge Wilson (void) printf("\t-\t-\t-"); 27694263d13fSGeorge Wilson else 27704263d13fSGeorge Wilson (void) printf(" - - -"); 27714263d13fSGeorge Wilson } else { 27724263d13fSGeorge Wilson print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, 27734263d13fSGeorge Wilson scripted); 27744263d13fSGeorge Wilson print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc, 27754263d13fSGeorge Wilson scripted); 27764263d13fSGeorge Wilson print_one_column(ZPOOL_PROP_FREE, 27774263d13fSGeorge Wilson vs->vs_space - vs->vs_alloc, scripted); 27784263d13fSGeorge Wilson } 27794263d13fSGeorge Wilson print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, 27804263d13fSGeorge Wilson scripted); 27814263d13fSGeorge Wilson (void) printf("\n"); 27824263d13fSGeorge Wilson } 27834263d13fSGeorge Wilson 27844263d13fSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 27854263d13fSGeorge Wilson &child, &children) != 0) 27864263d13fSGeorge Wilson return; 27874263d13fSGeorge Wilson 27884263d13fSGeorge Wilson for (c = 0; c < children; c++) { 27894263d13fSGeorge Wilson uint64_t ishole = B_FALSE; 27904263d13fSGeorge Wilson 27914263d13fSGeorge Wilson if (nvlist_lookup_uint64(child[c], 27924263d13fSGeorge Wilson ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) 27934263d13fSGeorge Wilson continue; 27944263d13fSGeorge Wilson 27954263d13fSGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 27964263d13fSGeorge Wilson print_list_stats(zhp, vname, child[c], cb, depth + 2); 27974263d13fSGeorge Wilson free(vname); 27984263d13fSGeorge Wilson } 27994263d13fSGeorge Wilson 28004263d13fSGeorge Wilson /* 28014263d13fSGeorge Wilson * Include level 2 ARC devices in iostat output 28024263d13fSGeorge Wilson */ 28034263d13fSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 28044263d13fSGeorge Wilson &child, &children) != 0) 28054263d13fSGeorge Wilson return; 28064263d13fSGeorge Wilson 28074263d13fSGeorge Wilson if (children > 0) { 28084263d13fSGeorge Wilson (void) printf("%-*s - - - - - " 28094263d13fSGeorge Wilson "-\n", cb->cb_namewidth, "cache"); 28104263d13fSGeorge Wilson for (c = 0; c < children; c++) { 28114263d13fSGeorge Wilson vname = zpool_vdev_name(g_zfs, zhp, child[c], 28124263d13fSGeorge Wilson B_FALSE); 28134263d13fSGeorge Wilson print_list_stats(zhp, vname, child[c], cb, depth + 2); 28144263d13fSGeorge Wilson free(vname); 28154263d13fSGeorge Wilson } 28164263d13fSGeorge Wilson } 28174263d13fSGeorge Wilson } 28184263d13fSGeorge Wilson 28194263d13fSGeorge Wilson 2820990b4856Slling /* 2821990b4856Slling * Generic callback function to list a pool. 2822990b4856Slling */ 2823fa9e4066Sahrens int 2824fa9e4066Sahrens list_callback(zpool_handle_t *zhp, void *data) 2825fa9e4066Sahrens { 2826fa9e4066Sahrens list_cbdata_t *cbp = data; 28274263d13fSGeorge Wilson nvlist_t *config; 28284263d13fSGeorge Wilson nvlist_t *nvroot; 2829fa9e4066Sahrens 28304263d13fSGeorge Wilson config = zpool_get_config(zhp, NULL); 2831fa9e4066Sahrens 28324263d13fSGeorge Wilson print_pool(zhp, cbp); 28334263d13fSGeorge Wilson if (!cbp->cb_verbose) 28344263d13fSGeorge Wilson return (0); 28354263d13fSGeorge Wilson 28364263d13fSGeorge Wilson verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 28374263d13fSGeorge Wilson &nvroot) == 0); 28384263d13fSGeorge Wilson print_list_stats(zhp, NULL, nvroot, cbp, 0); 2839fa9e4066Sahrens 2840fa9e4066Sahrens return (0); 2841fa9e4066Sahrens } 2842fa9e4066Sahrens 2843fa9e4066Sahrens /* 28443f9d6ad7SLin Ling * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] 2845fa9e4066Sahrens * 2846990b4856Slling * -H Scripted mode. Don't display headers, and separate properties 2847990b4856Slling * by a single tab. 2848990b4856Slling * -o List of properties to display. Defaults to 2849485bbbf5SGeorge Wilson * "name,size,allocated,free,capacity,health,altroot" 28503f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format 2851fa9e4066Sahrens * 2852fa9e4066Sahrens * List all pools in the system, whether or not they're healthy. Output space 2853fa9e4066Sahrens * statistics for each one, as well as health status summary. 2854fa9e4066Sahrens */ 2855fa9e4066Sahrens int 2856fa9e4066Sahrens zpool_do_list(int argc, char **argv) 2857fa9e4066Sahrens { 2858fa9e4066Sahrens int c; 2859fa9e4066Sahrens int ret; 2860fa9e4066Sahrens list_cbdata_t cb = { 0 }; 2861990b4856Slling static char default_props[] = 28624263d13fSGeorge Wilson "name,size,allocated,free,expandsize,capacity,dedupratio," 28634263d13fSGeorge Wilson "health,altroot"; 2864990b4856Slling char *props = default_props; 28653f9d6ad7SLin Ling unsigned long interval = 0, count = 0; 28664263d13fSGeorge Wilson zpool_list_t *list; 28674263d13fSGeorge Wilson boolean_t first = B_TRUE; 2868fa9e4066Sahrens 2869fa9e4066Sahrens /* check options */ 28704263d13fSGeorge Wilson while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) { 2871fa9e4066Sahrens switch (c) { 2872fa9e4066Sahrens case 'H': 287399653d4eSeschrock cb.cb_scripted = B_TRUE; 2874fa9e4066Sahrens break; 2875fa9e4066Sahrens case 'o': 2876990b4856Slling props = optarg; 2877fa9e4066Sahrens break; 28783f9d6ad7SLin Ling case 'T': 28793f9d6ad7SLin Ling get_timestamp_arg(*optarg); 28803f9d6ad7SLin Ling break; 28814263d13fSGeorge Wilson case 'v': 28824263d13fSGeorge Wilson cb.cb_verbose = B_TRUE; 28834263d13fSGeorge Wilson break; 2884fa9e4066Sahrens case ':': 2885fa9e4066Sahrens (void) fprintf(stderr, gettext("missing argument for " 2886fa9e4066Sahrens "'%c' option\n"), optopt); 288799653d4eSeschrock usage(B_FALSE); 2888fa9e4066Sahrens break; 2889fa9e4066Sahrens case '?': 2890fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2891fa9e4066Sahrens optopt); 289299653d4eSeschrock usage(B_FALSE); 2893fa9e4066Sahrens } 2894fa9e4066Sahrens } 2895fa9e4066Sahrens 2896fa9e4066Sahrens argc -= optind; 2897fa9e4066Sahrens argv += optind; 2898fa9e4066Sahrens 28993f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count); 29003f9d6ad7SLin Ling 2901990b4856Slling if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) 290299653d4eSeschrock usage(B_FALSE); 2903fa9e4066Sahrens 29044263d13fSGeorge Wilson if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL) 29054263d13fSGeorge Wilson return (1); 2906fa9e4066Sahrens 29074263d13fSGeorge Wilson if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { 2908fa9e4066Sahrens (void) printf(gettext("no pools available\n")); 29093f9d6ad7SLin Ling zprop_free_list(cb.cb_proplist); 2910fa9e4066Sahrens return (0); 2911fa9e4066Sahrens } 2912fa9e4066Sahrens 29134263d13fSGeorge Wilson for (;;) { 29144263d13fSGeorge Wilson pool_list_update(list); 29154263d13fSGeorge Wilson 29164263d13fSGeorge Wilson if (pool_list_count(list) == 0) 29174263d13fSGeorge Wilson break; 29184263d13fSGeorge Wilson 29194263d13fSGeorge Wilson cb.cb_namewidth = 0; 29204263d13fSGeorge Wilson (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); 29214263d13fSGeorge Wilson 29224263d13fSGeorge Wilson if (timestamp_fmt != NODATE) 29234263d13fSGeorge Wilson print_timestamp(timestamp_fmt); 29244263d13fSGeorge Wilson 29254263d13fSGeorge Wilson if (!cb.cb_scripted && (first || cb.cb_verbose)) { 29264263d13fSGeorge Wilson print_header(&cb); 29274263d13fSGeorge Wilson first = B_FALSE; 29284263d13fSGeorge Wilson } 29294263d13fSGeorge Wilson ret = pool_list_iter(list, B_TRUE, list_callback, &cb); 29304263d13fSGeorge Wilson 29313f9d6ad7SLin Ling if (interval == 0) 29323f9d6ad7SLin Ling break; 29333f9d6ad7SLin Ling 29343f9d6ad7SLin Ling if (count != 0 && --count == 0) 29353f9d6ad7SLin Ling break; 29363f9d6ad7SLin Ling 29373f9d6ad7SLin Ling (void) sleep(interval); 29383f9d6ad7SLin Ling } 29393f9d6ad7SLin Ling 29403f9d6ad7SLin Ling zprop_free_list(cb.cb_proplist); 2941fa9e4066Sahrens return (ret); 2942fa9e4066Sahrens } 2943fa9e4066Sahrens 2944fa9e4066Sahrens static nvlist_t * 2945fa9e4066Sahrens zpool_get_vdev_by_name(nvlist_t *nv, char *name) 2946fa9e4066Sahrens { 2947fa9e4066Sahrens nvlist_t **child; 2948fa9e4066Sahrens uint_t c, children; 2949fa9e4066Sahrens nvlist_t *match; 2950fa9e4066Sahrens char *path; 2951fa9e4066Sahrens 2952fa9e4066Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2953fa9e4066Sahrens &child, &children) != 0) { 2954fa9e4066Sahrens verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 2955fa9e4066Sahrens if (strncmp(name, "/dev/dsk/", 9) == 0) 2956fa9e4066Sahrens name += 9; 2957fa9e4066Sahrens if (strncmp(path, "/dev/dsk/", 9) == 0) 2958fa9e4066Sahrens path += 9; 2959fa9e4066Sahrens if (strcmp(name, path) == 0) 2960fa9e4066Sahrens return (nv); 2961fa9e4066Sahrens return (NULL); 2962fa9e4066Sahrens } 2963fa9e4066Sahrens 2964fa9e4066Sahrens for (c = 0; c < children; c++) 2965fa9e4066Sahrens if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL) 2966fa9e4066Sahrens return (match); 2967fa9e4066Sahrens 2968fa9e4066Sahrens return (NULL); 2969fa9e4066Sahrens } 2970fa9e4066Sahrens 2971fa9e4066Sahrens static int 2972fa9e4066Sahrens zpool_do_attach_or_replace(int argc, char **argv, int replacing) 2973fa9e4066Sahrens { 297499653d4eSeschrock boolean_t force = B_FALSE; 2975fa9e4066Sahrens int c; 2976fa9e4066Sahrens nvlist_t *nvroot; 2977fa9e4066Sahrens char *poolname, *old_disk, *new_disk; 2978fa9e4066Sahrens zpool_handle_t *zhp; 297999653d4eSeschrock int ret; 2980fa9e4066Sahrens 2981fa9e4066Sahrens /* check options */ 2982fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) { 2983fa9e4066Sahrens switch (c) { 2984fa9e4066Sahrens case 'f': 298599653d4eSeschrock force = B_TRUE; 2986fa9e4066Sahrens break; 2987fa9e4066Sahrens case '?': 2988fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2989fa9e4066Sahrens optopt); 299099653d4eSeschrock usage(B_FALSE); 2991fa9e4066Sahrens } 2992fa9e4066Sahrens } 2993fa9e4066Sahrens 2994fa9e4066Sahrens argc -= optind; 2995fa9e4066Sahrens argv += optind; 2996fa9e4066Sahrens 2997fa9e4066Sahrens /* get pool name and check number of arguments */ 2998fa9e4066Sahrens if (argc < 1) { 2999fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 300099653d4eSeschrock usage(B_FALSE); 3001fa9e4066Sahrens } 3002fa9e4066Sahrens 3003fa9e4066Sahrens poolname = argv[0]; 3004fa9e4066Sahrens 3005fa9e4066Sahrens if (argc < 2) { 3006fa9e4066Sahrens (void) fprintf(stderr, 3007fa9e4066Sahrens gettext("missing <device> specification\n")); 300899653d4eSeschrock usage(B_FALSE); 3009fa9e4066Sahrens } 3010fa9e4066Sahrens 3011fa9e4066Sahrens old_disk = argv[1]; 3012fa9e4066Sahrens 3013fa9e4066Sahrens if (argc < 3) { 3014fa9e4066Sahrens if (!replacing) { 3015fa9e4066Sahrens (void) fprintf(stderr, 3016fa9e4066Sahrens gettext("missing <new_device> specification\n")); 301799653d4eSeschrock usage(B_FALSE); 3018fa9e4066Sahrens } 3019fa9e4066Sahrens new_disk = old_disk; 3020fa9e4066Sahrens argc -= 1; 3021fa9e4066Sahrens argv += 1; 3022fa9e4066Sahrens } else { 3023fa9e4066Sahrens new_disk = argv[2]; 3024fa9e4066Sahrens argc -= 2; 3025fa9e4066Sahrens argv += 2; 3026fa9e4066Sahrens } 3027fa9e4066Sahrens 3028fa9e4066Sahrens if (argc > 1) { 3029fa9e4066Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 303099653d4eSeschrock usage(B_FALSE); 3031fa9e4066Sahrens } 3032fa9e4066Sahrens 303399653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3034fa9e4066Sahrens return (1); 3035fa9e4066Sahrens 30368488aeb5Staylor if (zpool_get_config(zhp, NULL) == NULL) { 3037fa9e4066Sahrens (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 3038fa9e4066Sahrens poolname); 3039fa9e4066Sahrens zpool_close(zhp); 3040fa9e4066Sahrens return (1); 3041fa9e4066Sahrens } 3042fa9e4066Sahrens 3043705040edSEric Taylor nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE, 3044705040edSEric Taylor argc, argv); 3045fa9e4066Sahrens if (nvroot == NULL) { 3046fa9e4066Sahrens zpool_close(zhp); 3047fa9e4066Sahrens return (1); 3048fa9e4066Sahrens } 3049fa9e4066Sahrens 305099653d4eSeschrock ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing); 305199653d4eSeschrock 305299653d4eSeschrock nvlist_free(nvroot); 305399653d4eSeschrock zpool_close(zhp); 305499653d4eSeschrock 305599653d4eSeschrock return (ret); 3056fa9e4066Sahrens } 3057fa9e4066Sahrens 3058fa9e4066Sahrens /* 3059fa9e4066Sahrens * zpool replace [-f] <pool> <device> <new_device> 3060fa9e4066Sahrens * 3061fa9e4066Sahrens * -f Force attach, even if <new_device> appears to be in use. 3062fa9e4066Sahrens * 3063fa9e4066Sahrens * Replace <device> with <new_device>. 3064fa9e4066Sahrens */ 3065fa9e4066Sahrens /* ARGSUSED */ 3066fa9e4066Sahrens int 3067fa9e4066Sahrens zpool_do_replace(int argc, char **argv) 3068fa9e4066Sahrens { 3069fa9e4066Sahrens return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); 3070fa9e4066Sahrens } 3071fa9e4066Sahrens 3072fa9e4066Sahrens /* 3073fa9e4066Sahrens * zpool attach [-f] <pool> <device> <new_device> 3074fa9e4066Sahrens * 3075fa9e4066Sahrens * -f Force attach, even if <new_device> appears to be in use. 3076fa9e4066Sahrens * 3077fa9e4066Sahrens * Attach <new_device> to the mirror containing <device>. If <device> is not 3078fa9e4066Sahrens * part of a mirror, then <device> will be transformed into a mirror of 3079fa9e4066Sahrens * <device> and <new_device>. In either case, <new_device> will begin life 3080fa9e4066Sahrens * with a DTL of [0, now], and will immediately begin to resilver itself. 3081fa9e4066Sahrens */ 3082fa9e4066Sahrens int 3083fa9e4066Sahrens zpool_do_attach(int argc, char **argv) 3084fa9e4066Sahrens { 3085fa9e4066Sahrens return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); 3086fa9e4066Sahrens } 3087fa9e4066Sahrens 3088fa9e4066Sahrens /* 3089fa9e4066Sahrens * zpool detach [-f] <pool> <device> 3090fa9e4066Sahrens * 3091fa9e4066Sahrens * -f Force detach of <device>, even if DTLs argue against it 3092fa9e4066Sahrens * (not supported yet) 3093fa9e4066Sahrens * 3094fa9e4066Sahrens * Detach a device from a mirror. The operation will be refused if <device> 3095fa9e4066Sahrens * is the last device in the mirror, or if the DTLs indicate that this device 3096fa9e4066Sahrens * has the only valid copy of some data. 3097fa9e4066Sahrens */ 3098fa9e4066Sahrens /* ARGSUSED */ 3099fa9e4066Sahrens int 3100fa9e4066Sahrens zpool_do_detach(int argc, char **argv) 3101fa9e4066Sahrens { 3102fa9e4066Sahrens int c; 3103fa9e4066Sahrens char *poolname, *path; 3104fa9e4066Sahrens zpool_handle_t *zhp; 310599653d4eSeschrock int ret; 3106fa9e4066Sahrens 3107fa9e4066Sahrens /* check options */ 3108fa9e4066Sahrens while ((c = getopt(argc, argv, "f")) != -1) { 3109fa9e4066Sahrens switch (c) { 3110fa9e4066Sahrens case 'f': 3111fa9e4066Sahrens case '?': 3112fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3113fa9e4066Sahrens optopt); 311499653d4eSeschrock usage(B_FALSE); 3115fa9e4066Sahrens } 3116fa9e4066Sahrens } 3117fa9e4066Sahrens 3118fa9e4066Sahrens argc -= optind; 3119fa9e4066Sahrens argv += optind; 3120fa9e4066Sahrens 3121fa9e4066Sahrens /* get pool name and check number of arguments */ 3122fa9e4066Sahrens if (argc < 1) { 3123fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 312499653d4eSeschrock usage(B_FALSE); 3125fa9e4066Sahrens } 3126fa9e4066Sahrens 3127fa9e4066Sahrens if (argc < 2) { 3128fa9e4066Sahrens (void) fprintf(stderr, 3129fa9e4066Sahrens gettext("missing <device> specification\n")); 313099653d4eSeschrock usage(B_FALSE); 3131fa9e4066Sahrens } 3132fa9e4066Sahrens 3133fa9e4066Sahrens poolname = argv[0]; 3134fa9e4066Sahrens path = argv[1]; 3135fa9e4066Sahrens 313699653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3137fa9e4066Sahrens return (1); 3138fa9e4066Sahrens 313999653d4eSeschrock ret = zpool_vdev_detach(zhp, path); 314099653d4eSeschrock 314199653d4eSeschrock zpool_close(zhp); 314299653d4eSeschrock 314399653d4eSeschrock return (ret); 3144fa9e4066Sahrens } 3145fa9e4066Sahrens 3146fa9e4066Sahrens /* 31471195e687SMark J Musante * zpool split [-n] [-o prop=val] ... 31481195e687SMark J Musante * [-o mntopt] ... 31491195e687SMark J Musante * [-R altroot] <pool> <newpool> [<device> ...] 31501195e687SMark J Musante * 31511195e687SMark J Musante * -n Do not split the pool, but display the resulting layout if 31521195e687SMark J Musante * it were to be split. 31531195e687SMark J Musante * -o Set property=value, or set mount options. 31541195e687SMark J Musante * -R Mount the split-off pool under an alternate root. 31551195e687SMark J Musante * 31561195e687SMark J Musante * Splits the named pool and gives it the new pool name. Devices to be split 31571195e687SMark J Musante * off may be listed, provided that no more than one device is specified 31581195e687SMark J Musante * per top-level vdev mirror. The newly split pool is left in an exported 31591195e687SMark J Musante * state unless -R is specified. 31601195e687SMark J Musante * 31611195e687SMark J Musante * Restrictions: the top-level of the pool pool must only be made up of 31621195e687SMark J Musante * mirrors; all devices in the pool must be healthy; no device may be 31631195e687SMark J Musante * undergoing a resilvering operation. 31641195e687SMark J Musante */ 31651195e687SMark J Musante int 31661195e687SMark J Musante zpool_do_split(int argc, char **argv) 31671195e687SMark J Musante { 31681195e687SMark J Musante char *srcpool, *newpool, *propval; 31691195e687SMark J Musante char *mntopts = NULL; 31701195e687SMark J Musante splitflags_t flags; 31711195e687SMark J Musante int c, ret = 0; 31721195e687SMark J Musante zpool_handle_t *zhp; 31731195e687SMark J Musante nvlist_t *config, *props = NULL; 31741195e687SMark J Musante 31751195e687SMark J Musante flags.dryrun = B_FALSE; 31761195e687SMark J Musante flags.import = B_FALSE; 31771195e687SMark J Musante 31781195e687SMark J Musante /* check options */ 31791195e687SMark J Musante while ((c = getopt(argc, argv, ":R:no:")) != -1) { 31801195e687SMark J Musante switch (c) { 31811195e687SMark J Musante case 'R': 31821195e687SMark J Musante flags.import = B_TRUE; 31831195e687SMark J Musante if (add_prop_list( 31841195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, 31851195e687SMark J Musante &props, B_TRUE) != 0) { 31861195e687SMark J Musante if (props) 31871195e687SMark J Musante nvlist_free(props); 31881195e687SMark J Musante usage(B_FALSE); 31891195e687SMark J Musante } 31901195e687SMark J Musante break; 31911195e687SMark J Musante case 'n': 31921195e687SMark J Musante flags.dryrun = B_TRUE; 31931195e687SMark J Musante break; 31941195e687SMark J Musante case 'o': 31951195e687SMark J Musante if ((propval = strchr(optarg, '=')) != NULL) { 31961195e687SMark J Musante *propval = '\0'; 31971195e687SMark J Musante propval++; 31981195e687SMark J Musante if (add_prop_list(optarg, propval, 31991195e687SMark J Musante &props, B_TRUE) != 0) { 32001195e687SMark J Musante if (props) 32011195e687SMark J Musante nvlist_free(props); 32021195e687SMark J Musante usage(B_FALSE); 32031195e687SMark J Musante } 32041195e687SMark J Musante } else { 32051195e687SMark J Musante mntopts = optarg; 32061195e687SMark J Musante } 32071195e687SMark J Musante break; 32081195e687SMark J Musante case ':': 32091195e687SMark J Musante (void) fprintf(stderr, gettext("missing argument for " 32101195e687SMark J Musante "'%c' option\n"), optopt); 32111195e687SMark J Musante usage(B_FALSE); 32121195e687SMark J Musante break; 32131195e687SMark J Musante case '?': 32141195e687SMark J Musante (void) fprintf(stderr, gettext("invalid option '%c'\n"), 32151195e687SMark J Musante optopt); 32161195e687SMark J Musante usage(B_FALSE); 32171195e687SMark J Musante break; 32181195e687SMark J Musante } 32191195e687SMark J Musante } 32201195e687SMark J Musante 32211195e687SMark J Musante if (!flags.import && mntopts != NULL) { 32221195e687SMark J Musante (void) fprintf(stderr, gettext("setting mntopts is only " 32231195e687SMark J Musante "valid when importing the pool\n")); 32241195e687SMark J Musante usage(B_FALSE); 32251195e687SMark J Musante } 32261195e687SMark J Musante 32271195e687SMark J Musante argc -= optind; 32281195e687SMark J Musante argv += optind; 32291195e687SMark J Musante 32301195e687SMark J Musante if (argc < 1) { 32311195e687SMark J Musante (void) fprintf(stderr, gettext("Missing pool name\n")); 32321195e687SMark J Musante usage(B_FALSE); 32331195e687SMark J Musante } 32341195e687SMark J Musante if (argc < 2) { 32351195e687SMark J Musante (void) fprintf(stderr, gettext("Missing new pool name\n")); 32361195e687SMark J Musante usage(B_FALSE); 32371195e687SMark J Musante } 32381195e687SMark J Musante 32391195e687SMark J Musante srcpool = argv[0]; 32401195e687SMark J Musante newpool = argv[1]; 32411195e687SMark J Musante 32421195e687SMark J Musante argc -= 2; 32431195e687SMark J Musante argv += 2; 32441195e687SMark J Musante 32451195e687SMark J Musante if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) 32461195e687SMark J Musante return (1); 32471195e687SMark J Musante 32481195e687SMark J Musante config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 32491195e687SMark J Musante if (config == NULL) { 32501195e687SMark J Musante ret = 1; 32511195e687SMark J Musante } else { 32521195e687SMark J Musante if (flags.dryrun) { 32531195e687SMark J Musante (void) printf(gettext("would create '%s' with the " 32541195e687SMark J Musante "following layout:\n\n"), newpool); 32551195e687SMark J Musante print_vdev_tree(NULL, newpool, config, 0, B_FALSE); 32561195e687SMark J Musante } 32571195e687SMark J Musante nvlist_free(config); 32581195e687SMark J Musante } 32591195e687SMark J Musante 32601195e687SMark J Musante zpool_close(zhp); 32611195e687SMark J Musante 32621195e687SMark J Musante if (ret != 0 || flags.dryrun || !flags.import) 32631195e687SMark J Musante return (ret); 32641195e687SMark J Musante 32651195e687SMark J Musante /* 32661195e687SMark J Musante * The split was successful. Now we need to open the new 32671195e687SMark J Musante * pool and import it. 32681195e687SMark J Musante */ 32691195e687SMark J Musante if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) 32701195e687SMark J Musante return (1); 32711195e687SMark J Musante if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 32721195e687SMark J Musante zpool_enable_datasets(zhp, mntopts, 0) != 0) { 32731195e687SMark J Musante ret = 1; 3274fe7cd8aaSCyril Plisko (void) fprintf(stderr, gettext("Split was successful, but " 32751195e687SMark J Musante "the datasets could not all be mounted\n")); 32761195e687SMark J Musante (void) fprintf(stderr, gettext("Try doing '%s' with a " 32771195e687SMark J Musante "different altroot\n"), "zpool import"); 32781195e687SMark J Musante } 32791195e687SMark J Musante zpool_close(zhp); 32801195e687SMark J Musante 32811195e687SMark J Musante return (ret); 32821195e687SMark J Musante } 32831195e687SMark J Musante 32841195e687SMark J Musante 32851195e687SMark J Musante 32861195e687SMark J Musante /* 3287441d80aaSlling * zpool online <pool> <device> ... 3288fa9e4066Sahrens */ 3289fa9e4066Sahrens int 3290fa9e4066Sahrens zpool_do_online(int argc, char **argv) 3291fa9e4066Sahrens { 3292fa9e4066Sahrens int c, i; 3293fa9e4066Sahrens char *poolname; 3294fa9e4066Sahrens zpool_handle_t *zhp; 3295fa9e4066Sahrens int ret = 0; 32963d7072f8Seschrock vdev_state_t newstate; 3297573ca77eSGeorge Wilson int flags = 0; 3298fa9e4066Sahrens 3299fa9e4066Sahrens /* check options */ 3300573ca77eSGeorge Wilson while ((c = getopt(argc, argv, "et")) != -1) { 3301fa9e4066Sahrens switch (c) { 3302573ca77eSGeorge Wilson case 'e': 3303573ca77eSGeorge Wilson flags |= ZFS_ONLINE_EXPAND; 3304573ca77eSGeorge Wilson break; 3305fa9e4066Sahrens case 't': 3306fa9e4066Sahrens case '?': 3307fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3308fa9e4066Sahrens optopt); 330999653d4eSeschrock usage(B_FALSE); 3310fa9e4066Sahrens } 3311fa9e4066Sahrens } 3312fa9e4066Sahrens 3313fa9e4066Sahrens argc -= optind; 3314fa9e4066Sahrens argv += optind; 3315fa9e4066Sahrens 3316fa9e4066Sahrens /* get pool name and check number of arguments */ 3317fa9e4066Sahrens if (argc < 1) { 3318fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name\n")); 331999653d4eSeschrock usage(B_FALSE); 3320fa9e4066Sahrens } 3321fa9e4066Sahrens if (argc < 2) { 3322fa9e4066Sahrens (void) fprintf(stderr, gettext("missing device name\n")); 332399653d4eSeschrock usage(B_FALSE); 3324fa9e4066Sahrens } 3325fa9e4066Sahrens 3326fa9e4066Sahrens poolname = argv[0]; 3327fa9e4066Sahrens 332899653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3329fa9e4066Sahrens return (1); 3330fa9e4066Sahrens 33313d7072f8Seschrock for (i = 1; i < argc; i++) { 3332573ca77eSGeorge Wilson if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { 33333d7072f8Seschrock if (newstate != VDEV_STATE_HEALTHY) { 33343d7072f8Seschrock (void) printf(gettext("warning: device '%s' " 33353d7072f8Seschrock "onlined, but remains in faulted state\n"), 3336fa9e4066Sahrens argv[i]); 33373d7072f8Seschrock if (newstate == VDEV_STATE_FAULTED) 33383d7072f8Seschrock (void) printf(gettext("use 'zpool " 33393d7072f8Seschrock "clear' to restore a faulted " 33403d7072f8Seschrock "device\n")); 3341fa9e4066Sahrens else 33423d7072f8Seschrock (void) printf(gettext("use 'zpool " 33433d7072f8Seschrock "replace' to replace devices " 33443d7072f8Seschrock "that are no longer present\n")); 33453d7072f8Seschrock } 33463d7072f8Seschrock } else { 3347fa9e4066Sahrens ret = 1; 33483d7072f8Seschrock } 33493d7072f8Seschrock } 3350fa9e4066Sahrens 335199653d4eSeschrock zpool_close(zhp); 335299653d4eSeschrock 3353fa9e4066Sahrens return (ret); 3354fa9e4066Sahrens } 3355fa9e4066Sahrens 3356fa9e4066Sahrens /* 3357441d80aaSlling * zpool offline [-ft] <pool> <device> ... 3358fa9e4066Sahrens * 3359fa9e4066Sahrens * -f Force the device into the offline state, even if doing 3360fa9e4066Sahrens * so would appear to compromise pool availability. 3361fa9e4066Sahrens * (not supported yet) 3362fa9e4066Sahrens * 3363fa9e4066Sahrens * -t Only take the device off-line temporarily. The offline 3364fa9e4066Sahrens * state will not be persistent across reboots. 3365fa9e4066Sahrens */ 3366fa9e4066Sahrens /* ARGSUSED */ 3367fa9e4066Sahrens int 3368fa9e4066Sahrens zpool_do_offline(int argc, char **argv) 3369fa9e4066Sahrens { 3370fa9e4066Sahrens int c, i; 3371fa9e4066Sahrens char *poolname; 3372fa9e4066Sahrens zpool_handle_t *zhp; 337399653d4eSeschrock int ret = 0; 337499653d4eSeschrock boolean_t istmp = B_FALSE; 3375fa9e4066Sahrens 3376fa9e4066Sahrens /* check options */ 3377fa9e4066Sahrens while ((c = getopt(argc, argv, "ft")) != -1) { 3378fa9e4066Sahrens switch (c) { 3379fa9e4066Sahrens case 't': 338099653d4eSeschrock istmp = B_TRUE; 3381441d80aaSlling break; 3382441d80aaSlling case 'f': 3383fa9e4066Sahrens case '?': 3384fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3385fa9e4066Sahrens optopt); 338699653d4eSeschrock usage(B_FALSE); 3387fa9e4066Sahrens } 3388fa9e4066Sahrens } 3389fa9e4066Sahrens 3390fa9e4066Sahrens argc -= optind; 3391fa9e4066Sahrens argv += optind; 3392fa9e4066Sahrens 3393fa9e4066Sahrens /* get pool name and check number of arguments */ 3394fa9e4066Sahrens if (argc < 1) { 3395fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name\n")); 339699653d4eSeschrock usage(B_FALSE); 3397fa9e4066Sahrens } 3398fa9e4066Sahrens if (argc < 2) { 3399fa9e4066Sahrens (void) fprintf(stderr, gettext("missing device name\n")); 340099653d4eSeschrock usage(B_FALSE); 3401fa9e4066Sahrens } 3402fa9e4066Sahrens 3403fa9e4066Sahrens poolname = argv[0]; 3404fa9e4066Sahrens 340599653d4eSeschrock if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3406fa9e4066Sahrens return (1); 3407fa9e4066Sahrens 34083d7072f8Seschrock for (i = 1; i < argc; i++) { 34093d7072f8Seschrock if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 3410fa9e4066Sahrens ret = 1; 34113d7072f8Seschrock } 3412fa9e4066Sahrens 341399653d4eSeschrock zpool_close(zhp); 341499653d4eSeschrock 3415fa9e4066Sahrens return (ret); 3416fa9e4066Sahrens } 3417fa9e4066Sahrens 3418ea8dc4b6Seschrock /* 3419ea8dc4b6Seschrock * zpool clear <pool> [device] 3420ea8dc4b6Seschrock * 3421ea8dc4b6Seschrock * Clear all errors associated with a pool or a particular device. 3422ea8dc4b6Seschrock */ 3423ea8dc4b6Seschrock int 3424ea8dc4b6Seschrock zpool_do_clear(int argc, char **argv) 3425ea8dc4b6Seschrock { 3426468c413aSTim Haley int c; 3427ea8dc4b6Seschrock int ret = 0; 3428468c413aSTim Haley boolean_t dryrun = B_FALSE; 3429468c413aSTim Haley boolean_t do_rewind = B_FALSE; 3430468c413aSTim Haley boolean_t xtreme_rewind = B_FALSE; 3431468c413aSTim Haley uint32_t rewind_policy = ZPOOL_NO_REWIND; 3432468c413aSTim Haley nvlist_t *policy = NULL; 3433ea8dc4b6Seschrock zpool_handle_t *zhp; 3434ea8dc4b6Seschrock char *pool, *device; 3435ea8dc4b6Seschrock 3436468c413aSTim Haley /* check options */ 3437468c413aSTim Haley while ((c = getopt(argc, argv, "FnX")) != -1) { 3438468c413aSTim Haley switch (c) { 3439468c413aSTim Haley case 'F': 3440468c413aSTim Haley do_rewind = B_TRUE; 3441468c413aSTim Haley break; 3442468c413aSTim Haley case 'n': 3443468c413aSTim Haley dryrun = B_TRUE; 3444468c413aSTim Haley break; 3445468c413aSTim Haley case 'X': 3446468c413aSTim Haley xtreme_rewind = B_TRUE; 3447468c413aSTim Haley break; 3448468c413aSTim Haley case '?': 3449468c413aSTim Haley (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3450468c413aSTim Haley optopt); 3451468c413aSTim Haley usage(B_FALSE); 3452468c413aSTim Haley } 3453468c413aSTim Haley } 3454468c413aSTim Haley 3455468c413aSTim Haley argc -= optind; 3456468c413aSTim Haley argv += optind; 3457468c413aSTim Haley 3458468c413aSTim Haley if (argc < 1) { 3459ea8dc4b6Seschrock (void) fprintf(stderr, gettext("missing pool name\n")); 346099653d4eSeschrock usage(B_FALSE); 3461ea8dc4b6Seschrock } 3462ea8dc4b6Seschrock 3463468c413aSTim Haley if (argc > 2) { 3464ea8dc4b6Seschrock (void) fprintf(stderr, gettext("too many arguments\n")); 346599653d4eSeschrock usage(B_FALSE); 3466ea8dc4b6Seschrock } 3467ea8dc4b6Seschrock 3468468c413aSTim Haley if ((dryrun || xtreme_rewind) && !do_rewind) { 3469468c413aSTim Haley (void) fprintf(stderr, 3470468c413aSTim Haley gettext("-n or -X only meaningful with -F\n")); 3471468c413aSTim Haley usage(B_FALSE); 3472468c413aSTim Haley } 3473468c413aSTim Haley if (dryrun) 3474468c413aSTim Haley rewind_policy = ZPOOL_TRY_REWIND; 3475468c413aSTim Haley else if (do_rewind) 3476468c413aSTim Haley rewind_policy = ZPOOL_DO_REWIND; 3477468c413aSTim Haley if (xtreme_rewind) 3478468c413aSTim Haley rewind_policy |= ZPOOL_EXTREME_REWIND; 3479ea8dc4b6Seschrock 3480468c413aSTim Haley /* In future, further rewind policy choices can be passed along here */ 3481468c413aSTim Haley if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 3482468c413aSTim Haley nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 3483ea8dc4b6Seschrock return (1); 3484ea8dc4b6Seschrock 3485468c413aSTim Haley pool = argv[0]; 3486468c413aSTim Haley device = argc == 2 ? argv[1] : NULL; 3487468c413aSTim Haley 3488468c413aSTim Haley if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 3489468c413aSTim Haley nvlist_free(policy); 3490468c413aSTim Haley return (1); 3491468c413aSTim Haley } 3492468c413aSTim Haley 3493468c413aSTim Haley if (zpool_clear(zhp, device, policy) != 0) 3494ea8dc4b6Seschrock ret = 1; 3495ea8dc4b6Seschrock 3496ea8dc4b6Seschrock zpool_close(zhp); 3497ea8dc4b6Seschrock 3498468c413aSTim Haley nvlist_free(policy); 3499468c413aSTim Haley 3500ea8dc4b6Seschrock return (ret); 3501ea8dc4b6Seschrock } 3502ea8dc4b6Seschrock 3503e9103aaeSGarrett D'Amore /* 3504e9103aaeSGarrett D'Amore * zpool reguid <pool> 3505e9103aaeSGarrett D'Amore */ 3506e9103aaeSGarrett D'Amore int 3507e9103aaeSGarrett D'Amore zpool_do_reguid(int argc, char **argv) 3508e9103aaeSGarrett D'Amore { 3509e9103aaeSGarrett D'Amore int c; 3510e9103aaeSGarrett D'Amore char *poolname; 3511e9103aaeSGarrett D'Amore zpool_handle_t *zhp; 3512e9103aaeSGarrett D'Amore int ret = 0; 3513e9103aaeSGarrett D'Amore 3514e9103aaeSGarrett D'Amore /* check options */ 3515e9103aaeSGarrett D'Amore while ((c = getopt(argc, argv, "")) != -1) { 3516e9103aaeSGarrett D'Amore switch (c) { 3517e9103aaeSGarrett D'Amore case '?': 3518e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3519e9103aaeSGarrett D'Amore optopt); 3520e9103aaeSGarrett D'Amore usage(B_FALSE); 3521e9103aaeSGarrett D'Amore } 3522e9103aaeSGarrett D'Amore } 3523e9103aaeSGarrett D'Amore 3524e9103aaeSGarrett D'Amore argc -= optind; 3525e9103aaeSGarrett D'Amore argv += optind; 3526e9103aaeSGarrett D'Amore 3527e9103aaeSGarrett D'Amore /* get pool name and check number of arguments */ 3528e9103aaeSGarrett D'Amore if (argc < 1) { 3529e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("missing pool name\n")); 3530e9103aaeSGarrett D'Amore usage(B_FALSE); 3531e9103aaeSGarrett D'Amore } 3532e9103aaeSGarrett D'Amore 3533e9103aaeSGarrett D'Amore if (argc > 1) { 3534e9103aaeSGarrett D'Amore (void) fprintf(stderr, gettext("too many arguments\n")); 3535e9103aaeSGarrett D'Amore usage(B_FALSE); 3536e9103aaeSGarrett D'Amore } 3537e9103aaeSGarrett D'Amore 3538e9103aaeSGarrett D'Amore poolname = argv[0]; 3539e9103aaeSGarrett D'Amore if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3540e9103aaeSGarrett D'Amore return (1); 3541e9103aaeSGarrett D'Amore 3542e9103aaeSGarrett D'Amore ret = zpool_reguid(zhp); 3543e9103aaeSGarrett D'Amore 3544e9103aaeSGarrett D'Amore zpool_close(zhp); 3545e9103aaeSGarrett D'Amore return (ret); 3546e9103aaeSGarrett D'Amore } 3547e9103aaeSGarrett D'Amore 3548e9103aaeSGarrett D'Amore 35494263d13fSGeorge Wilson /* 35504263d13fSGeorge Wilson * zpool reopen <pool> 35514263d13fSGeorge Wilson * 35524263d13fSGeorge Wilson * Reopen the pool so that the kernel can update the sizes of all vdevs. 35534263d13fSGeorge Wilson */ 35544263d13fSGeorge Wilson int 35554263d13fSGeorge Wilson zpool_do_reopen(int argc, char **argv) 35564263d13fSGeorge Wilson { 3557*31d7e8faSGeorge Wilson int c; 35584263d13fSGeorge Wilson int ret = 0; 35594263d13fSGeorge Wilson zpool_handle_t *zhp; 35604263d13fSGeorge Wilson char *pool; 35614263d13fSGeorge Wilson 3562*31d7e8faSGeorge Wilson /* check options */ 3563*31d7e8faSGeorge Wilson while ((c = getopt(argc, argv, "")) != -1) { 3564*31d7e8faSGeorge Wilson switch (c) { 3565*31d7e8faSGeorge Wilson case '?': 3566*31d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3567*31d7e8faSGeorge Wilson optopt); 3568*31d7e8faSGeorge Wilson usage(B_FALSE); 3569*31d7e8faSGeorge Wilson } 3570*31d7e8faSGeorge Wilson } 3571*31d7e8faSGeorge Wilson 35724263d13fSGeorge Wilson argc--; 35734263d13fSGeorge Wilson argv++; 35744263d13fSGeorge Wilson 3575*31d7e8faSGeorge Wilson if (argc < 1) { 3576*31d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("missing pool name\n")); 3577*31d7e8faSGeorge Wilson usage(B_FALSE); 3578*31d7e8faSGeorge Wilson } 3579*31d7e8faSGeorge Wilson 3580*31d7e8faSGeorge Wilson if (argc > 1) { 3581*31d7e8faSGeorge Wilson (void) fprintf(stderr, gettext("too many arguments\n")); 3582*31d7e8faSGeorge Wilson usage(B_FALSE); 3583*31d7e8faSGeorge Wilson } 35844263d13fSGeorge Wilson 35854263d13fSGeorge Wilson pool = argv[0]; 35864263d13fSGeorge Wilson if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) 35874263d13fSGeorge Wilson return (1); 35884263d13fSGeorge Wilson 35894263d13fSGeorge Wilson ret = zpool_reopen(zhp); 35904263d13fSGeorge Wilson zpool_close(zhp); 35914263d13fSGeorge Wilson return (ret); 35924263d13fSGeorge Wilson } 35934263d13fSGeorge Wilson 3594fa9e4066Sahrens typedef struct scrub_cbdata { 3595fa9e4066Sahrens int cb_type; 359606eeb2adSek110237 int cb_argc; 359706eeb2adSek110237 char **cb_argv; 3598fa9e4066Sahrens } scrub_cbdata_t; 3599fa9e4066Sahrens 3600fa9e4066Sahrens int 3601fa9e4066Sahrens scrub_callback(zpool_handle_t *zhp, void *data) 3602fa9e4066Sahrens { 3603fa9e4066Sahrens scrub_cbdata_t *cb = data; 360406eeb2adSek110237 int err; 3605fa9e4066Sahrens 3606ea8dc4b6Seschrock /* 3607ea8dc4b6Seschrock * Ignore faulted pools. 3608ea8dc4b6Seschrock */ 3609ea8dc4b6Seschrock if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 3610ea8dc4b6Seschrock (void) fprintf(stderr, gettext("cannot scrub '%s': pool is " 3611ea8dc4b6Seschrock "currently unavailable\n"), zpool_get_name(zhp)); 3612ea8dc4b6Seschrock return (1); 3613ea8dc4b6Seschrock } 3614ea8dc4b6Seschrock 36153f9d6ad7SLin Ling err = zpool_scan(zhp, cb->cb_type); 361606eeb2adSek110237 361706eeb2adSek110237 return (err != 0); 3618fa9e4066Sahrens } 3619fa9e4066Sahrens 3620fa9e4066Sahrens /* 3621fa9e4066Sahrens * zpool scrub [-s] <pool> ... 3622fa9e4066Sahrens * 3623fa9e4066Sahrens * -s Stop. Stops any in-progress scrub. 3624fa9e4066Sahrens */ 3625fa9e4066Sahrens int 3626fa9e4066Sahrens zpool_do_scrub(int argc, char **argv) 3627fa9e4066Sahrens { 3628fa9e4066Sahrens int c; 3629fa9e4066Sahrens scrub_cbdata_t cb; 3630fa9e4066Sahrens 36313f9d6ad7SLin Ling cb.cb_type = POOL_SCAN_SCRUB; 3632fa9e4066Sahrens 3633fa9e4066Sahrens /* check options */ 3634fa9e4066Sahrens while ((c = getopt(argc, argv, "s")) != -1) { 3635fa9e4066Sahrens switch (c) { 3636fa9e4066Sahrens case 's': 36373f9d6ad7SLin Ling cb.cb_type = POOL_SCAN_NONE; 3638fa9e4066Sahrens break; 3639fa9e4066Sahrens case '?': 3640fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3641fa9e4066Sahrens optopt); 364299653d4eSeschrock usage(B_FALSE); 3643fa9e4066Sahrens } 3644fa9e4066Sahrens } 3645fa9e4066Sahrens 364606eeb2adSek110237 cb.cb_argc = argc; 364706eeb2adSek110237 cb.cb_argv = argv; 3648fa9e4066Sahrens argc -= optind; 3649fa9e4066Sahrens argv += optind; 3650fa9e4066Sahrens 3651fa9e4066Sahrens if (argc < 1) { 3652fa9e4066Sahrens (void) fprintf(stderr, gettext("missing pool name argument\n")); 365399653d4eSeschrock usage(B_FALSE); 3654fa9e4066Sahrens } 3655fa9e4066Sahrens 3656b1b8ab34Slling return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb)); 3657fa9e4066Sahrens } 3658fa9e4066Sahrens 3659fa9e4066Sahrens typedef struct status_cbdata { 3660fa9e4066Sahrens int cb_count; 3661e9dbad6fSeschrock boolean_t cb_allpools; 366299653d4eSeschrock boolean_t cb_verbose; 366399653d4eSeschrock boolean_t cb_explain; 366499653d4eSeschrock boolean_t cb_first; 36659eb19f4dSGeorge Wilson boolean_t cb_dedup_stats; 3666fa9e4066Sahrens } status_cbdata_t; 3667fa9e4066Sahrens 3668fa9e4066Sahrens /* 3669fa9e4066Sahrens * Print out detailed scrub status. 3670fa9e4066Sahrens */ 3671fa9e4066Sahrens void 36723f9d6ad7SLin Ling print_scan_status(pool_scan_stat_t *ps) 3673fa9e4066Sahrens { 36743f9d6ad7SLin Ling time_t start, end; 367573d314ceSLin Ling uint64_t elapsed, mins_left, hours_left; 36763f9d6ad7SLin Ling uint64_t pass_exam, examined, total; 36773f9d6ad7SLin Ling uint_t rate; 3678fa9e4066Sahrens double fraction_done; 36793f9d6ad7SLin Ling char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 3680fa9e4066Sahrens 36813f9d6ad7SLin Ling (void) printf(gettext(" scan: ")); 3682fa9e4066Sahrens 36833f9d6ad7SLin Ling /* If there's never been a scan, there's not much to say. */ 36843f9d6ad7SLin Ling if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 36853f9d6ad7SLin Ling ps->pss_func >= POOL_SCAN_FUNCS) { 3686fa9e4066Sahrens (void) printf(gettext("none requested\n")); 3687fa9e4066Sahrens return; 3688fa9e4066Sahrens } 3689fa9e4066Sahrens 36903f9d6ad7SLin Ling start = ps->pss_start_time; 36913f9d6ad7SLin Ling end = ps->pss_end_time; 36923f9d6ad7SLin Ling zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf)); 3693fa9e4066Sahrens 36943f9d6ad7SLin Ling assert(ps->pss_func == POOL_SCAN_SCRUB || 36953f9d6ad7SLin Ling ps->pss_func == POOL_SCAN_RESILVER); 36963f9d6ad7SLin Ling /* 36973f9d6ad7SLin Ling * Scan is finished or canceled. 36983f9d6ad7SLin Ling */ 36993f9d6ad7SLin Ling if (ps->pss_state == DSS_FINISHED) { 37003f9d6ad7SLin Ling uint64_t minutes_taken = (end - start) / 60; 37013f9d6ad7SLin Ling char *fmt; 3702fa9e4066Sahrens 37033f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) { 37043f9d6ad7SLin Ling fmt = gettext("scrub repaired %s in %lluh%um with " 37053f9d6ad7SLin Ling "%llu errors on %s"); 37063f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) { 37073f9d6ad7SLin Ling fmt = gettext("resilvered %s in %lluh%um with " 37083f9d6ad7SLin Ling "%llu errors on %s"); 37093f9d6ad7SLin Ling } 37103f9d6ad7SLin Ling /* LINTED */ 37113f9d6ad7SLin Ling (void) printf(fmt, processed_buf, 371218ce54dfSek110237 (u_longlong_t)(minutes_taken / 60), 371318ce54dfSek110237 (uint_t)(minutes_taken % 60), 37143f9d6ad7SLin Ling (u_longlong_t)ps->pss_errors, 37153f9d6ad7SLin Ling ctime((time_t *)&end)); 37163f9d6ad7SLin Ling return; 37173f9d6ad7SLin Ling } else if (ps->pss_state == DSS_CANCELED) { 37183f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) { 37193f9d6ad7SLin Ling (void) printf(gettext("scrub canceled on %s"), 37203f9d6ad7SLin Ling ctime(&end)); 37213f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) { 37223f9d6ad7SLin Ling (void) printf(gettext("resilver canceled on %s"), 37233f9d6ad7SLin Ling ctime(&end)); 37243f9d6ad7SLin Ling } 3725fa9e4066Sahrens return; 3726fa9e4066Sahrens } 3727fa9e4066Sahrens 37283f9d6ad7SLin Ling assert(ps->pss_state == DSS_SCANNING); 3729fa9e4066Sahrens 37303f9d6ad7SLin Ling /* 37313f9d6ad7SLin Ling * Scan is in progress. 37323f9d6ad7SLin Ling */ 37333f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_SCRUB) { 37343f9d6ad7SLin Ling (void) printf(gettext("scrub in progress since %s"), 37353f9d6ad7SLin Ling ctime(&start)); 37363f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_RESILVER) { 37373f9d6ad7SLin Ling (void) printf(gettext("resilver in progress since %s"), 37383f9d6ad7SLin Ling ctime(&start)); 37393f9d6ad7SLin Ling } 37403f9d6ad7SLin Ling 37413f9d6ad7SLin Ling examined = ps->pss_examined ? ps->pss_examined : 1; 37423f9d6ad7SLin Ling total = ps->pss_to_examine; 3743fa9e4066Sahrens fraction_done = (double)examined / total; 3744fa9e4066Sahrens 37453f9d6ad7SLin Ling /* elapsed time for this pass */ 37463f9d6ad7SLin Ling elapsed = time(NULL) - ps->pss_pass_start; 37473f9d6ad7SLin Ling elapsed = elapsed ? elapsed : 1; 37483f9d6ad7SLin Ling pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1; 37493f9d6ad7SLin Ling rate = pass_exam / elapsed; 37503f9d6ad7SLin Ling rate = rate ? rate : 1; 37513f9d6ad7SLin Ling mins_left = ((total - examined) / rate) / 60; 375273d314ceSLin Ling hours_left = mins_left / 60; 37533f9d6ad7SLin Ling 37543f9d6ad7SLin Ling zfs_nicenum(examined, examined_buf, sizeof (examined_buf)); 37553f9d6ad7SLin Ling zfs_nicenum(total, total_buf, sizeof (total_buf)); 37563f9d6ad7SLin Ling zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 37573f9d6ad7SLin Ling 375873d314ceSLin Ling /* 375973d314ceSLin Ling * do not print estimated time if hours_left is more than 30 days 376073d314ceSLin Ling */ 376173d314ceSLin Ling (void) printf(gettext(" %s scanned out of %s at %s/s"), 376273d314ceSLin Ling examined_buf, total_buf, rate_buf); 376373d314ceSLin Ling if (hours_left < (30 * 24)) { 376473d314ceSLin Ling (void) printf(gettext(", %lluh%um to go\n"), 376573d314ceSLin Ling (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 376673d314ceSLin Ling } else { 376773d314ceSLin Ling (void) printf(gettext( 376873d314ceSLin Ling ", (scan is slow, no estimated time)\n")); 376973d314ceSLin Ling } 37703f9d6ad7SLin Ling 37713f9d6ad7SLin Ling if (ps->pss_func == POOL_SCAN_RESILVER) { 37723f9d6ad7SLin Ling (void) printf(gettext(" %s resilvered, %.2f%% done\n"), 37733f9d6ad7SLin Ling processed_buf, 100 * fraction_done); 37743f9d6ad7SLin Ling } else if (ps->pss_func == POOL_SCAN_SCRUB) { 37753f9d6ad7SLin Ling (void) printf(gettext(" %s repaired, %.2f%% done\n"), 37763f9d6ad7SLin Ling processed_buf, 100 * fraction_done); 37773f9d6ad7SLin Ling } 3778fa9e4066Sahrens } 3779fa9e4066Sahrens 3780ea8dc4b6Seschrock static void 3781ea8dc4b6Seschrock print_error_log(zpool_handle_t *zhp) 3782ea8dc4b6Seschrock { 378375519f38Sek110237 nvlist_t *nverrlist = NULL; 378455434c77Sek110237 nvpair_t *elem; 378555434c77Sek110237 char *pathname; 378655434c77Sek110237 size_t len = MAXPATHLEN * 2; 3787ea8dc4b6Seschrock 378855434c77Sek110237 if (zpool_get_errlog(zhp, &nverrlist) != 0) { 3789ea8dc4b6Seschrock (void) printf("errors: List of errors unavailable " 3790ea8dc4b6Seschrock "(insufficient privileges)\n"); 3791ea8dc4b6Seschrock return; 3792ea8dc4b6Seschrock } 3793ea8dc4b6Seschrock 379455434c77Sek110237 (void) printf("errors: Permanent errors have been " 379555434c77Sek110237 "detected in the following files:\n\n"); 3796ea8dc4b6Seschrock 379755434c77Sek110237 pathname = safe_malloc(len); 379855434c77Sek110237 elem = NULL; 379955434c77Sek110237 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 380055434c77Sek110237 nvlist_t *nv; 380155434c77Sek110237 uint64_t dsobj, obj; 3802ea8dc4b6Seschrock 380355434c77Sek110237 verify(nvpair_value_nvlist(elem, &nv) == 0); 380455434c77Sek110237 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 380555434c77Sek110237 &dsobj) == 0); 380655434c77Sek110237 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 380755434c77Sek110237 &obj) == 0); 380855434c77Sek110237 zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 380955434c77Sek110237 (void) printf("%7s %s\n", "", pathname); 3810ea8dc4b6Seschrock } 381155434c77Sek110237 free(pathname); 381255434c77Sek110237 nvlist_free(nverrlist); 3813ea8dc4b6Seschrock } 3814ea8dc4b6Seschrock 381599653d4eSeschrock static void 381699653d4eSeschrock print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares, 381799653d4eSeschrock int namewidth) 381899653d4eSeschrock { 381999653d4eSeschrock uint_t i; 382099653d4eSeschrock char *name; 382199653d4eSeschrock 382299653d4eSeschrock if (nspares == 0) 382399653d4eSeschrock return; 382499653d4eSeschrock 382599653d4eSeschrock (void) printf(gettext("\tspares\n")); 382699653d4eSeschrock 382799653d4eSeschrock for (i = 0; i < nspares; i++) { 382888ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE); 382999653d4eSeschrock print_status_config(zhp, name, spares[i], 3830aa8cf21aSNeil Perrin namewidth, 2, B_TRUE); 383199653d4eSeschrock free(name); 383299653d4eSeschrock } 383399653d4eSeschrock } 383499653d4eSeschrock 3835fa94a07fSbrendan static void 3836fa94a07fSbrendan print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache, 3837fa94a07fSbrendan int namewidth) 3838fa94a07fSbrendan { 3839fa94a07fSbrendan uint_t i; 3840fa94a07fSbrendan char *name; 3841fa94a07fSbrendan 3842fa94a07fSbrendan if (nl2cache == 0) 3843fa94a07fSbrendan return; 3844fa94a07fSbrendan 3845fa94a07fSbrendan (void) printf(gettext("\tcache\n")); 3846fa94a07fSbrendan 3847fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 384888ecc943SGeorge Wilson name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE); 3849fa94a07fSbrendan print_status_config(zhp, name, l2cache[i], 3850aa8cf21aSNeil Perrin namewidth, 2, B_FALSE); 3851aa8cf21aSNeil Perrin free(name); 3852aa8cf21aSNeil Perrin } 3853aa8cf21aSNeil Perrin } 3854aa8cf21aSNeil Perrin 38559eb19f4dSGeorge Wilson static void 38569eb19f4dSGeorge Wilson print_dedup_stats(nvlist_t *config) 38579eb19f4dSGeorge Wilson { 38589eb19f4dSGeorge Wilson ddt_histogram_t *ddh; 38599eb19f4dSGeorge Wilson ddt_stat_t *dds; 38609eb19f4dSGeorge Wilson ddt_object_t *ddo; 38619eb19f4dSGeorge Wilson uint_t c; 38629eb19f4dSGeorge Wilson 38639eb19f4dSGeorge Wilson /* 38649eb19f4dSGeorge Wilson * If the pool was faulted then we may not have been able to 38652384d9f8SGeorge Wilson * obtain the config. Otherwise, if we have anything in the dedup 38669eb19f4dSGeorge Wilson * table continue processing the stats. 38679eb19f4dSGeorge Wilson */ 38689eb19f4dSGeorge Wilson if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 3869ce72e614SYuri Pankov (uint64_t **)&ddo, &c) != 0) 38709eb19f4dSGeorge Wilson return; 38719eb19f4dSGeorge Wilson 38729eb19f4dSGeorge Wilson (void) printf("\n"); 3873ce72e614SYuri Pankov (void) printf(gettext(" dedup: ")); 3874ce72e614SYuri Pankov if (ddo->ddo_count == 0) { 3875ce72e614SYuri Pankov (void) printf(gettext("no DDT entries\n")); 3876ce72e614SYuri Pankov return; 3877ce72e614SYuri Pankov } 3878ce72e614SYuri Pankov 38799eb19f4dSGeorge Wilson (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n", 38809eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_count, 38819eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_dspace, 38829eb19f4dSGeorge Wilson (u_longlong_t)ddo->ddo_mspace); 38839eb19f4dSGeorge Wilson 38849eb19f4dSGeorge Wilson verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 38859eb19f4dSGeorge Wilson (uint64_t **)&dds, &c) == 0); 38869eb19f4dSGeorge Wilson verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 38879eb19f4dSGeorge Wilson (uint64_t **)&ddh, &c) == 0); 38889eb19f4dSGeorge Wilson zpool_dump_ddt(dds, ddh); 38899eb19f4dSGeorge Wilson } 38909eb19f4dSGeorge Wilson 3891aa8cf21aSNeil Perrin /* 3892fa9e4066Sahrens * Display a summary of pool status. Displays a summary such as: 3893fa9e4066Sahrens * 3894fa9e4066Sahrens * pool: tank 3895fa9e4066Sahrens * status: DEGRADED 3896fa9e4066Sahrens * reason: One or more devices ... 3897654b400cSJoshua M. Clulow * see: http://illumos.org/msg/ZFS-xxxx-01 3898fa9e4066Sahrens * config: 3899fa9e4066Sahrens * mirror DEGRADED 3900fa9e4066Sahrens * c1t0d0 OK 3901ea8dc4b6Seschrock * c2t0d0 UNAVAIL 3902fa9e4066Sahrens * 3903fa9e4066Sahrens * When given the '-v' option, we print out the complete config. If the '-e' 3904fa9e4066Sahrens * option is specified, then we print out error rate information as well. 3905fa9e4066Sahrens */ 3906fa9e4066Sahrens int 3907fa9e4066Sahrens status_callback(zpool_handle_t *zhp, void *data) 3908fa9e4066Sahrens { 3909fa9e4066Sahrens status_cbdata_t *cbp = data; 3910fa9e4066Sahrens nvlist_t *config, *nvroot; 3911fa9e4066Sahrens char *msgid; 3912fa9e4066Sahrens int reason; 391346657f8dSmmusante const char *health; 391446657f8dSmmusante uint_t c; 391546657f8dSmmusante vdev_stat_t *vs; 3916fa9e4066Sahrens 3917088e9d47Seschrock config = zpool_get_config(zhp, NULL); 3918fa9e4066Sahrens reason = zpool_get_status(zhp, &msgid); 3919fa9e4066Sahrens 3920fa9e4066Sahrens cbp->cb_count++; 3921fa9e4066Sahrens 3922fa9e4066Sahrens /* 3923fa9e4066Sahrens * If we were given 'zpool status -x', only report those pools with 3924fa9e4066Sahrens * problems. 3925fa9e4066Sahrens */ 3926e9dbad6fSeschrock if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) { 3927e9dbad6fSeschrock if (!cbp->cb_allpools) { 3928e9dbad6fSeschrock (void) printf(gettext("pool '%s' is healthy\n"), 3929e9dbad6fSeschrock zpool_get_name(zhp)); 3930e9dbad6fSeschrock if (cbp->cb_first) 3931e9dbad6fSeschrock cbp->cb_first = B_FALSE; 3932e9dbad6fSeschrock } 3933fa9e4066Sahrens return (0); 3934e9dbad6fSeschrock } 3935fa9e4066Sahrens 3936fa9e4066Sahrens if (cbp->cb_first) 393799653d4eSeschrock cbp->cb_first = B_FALSE; 3938fa9e4066Sahrens else 3939fa9e4066Sahrens (void) printf("\n"); 3940fa9e4066Sahrens 394146657f8dSmmusante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 394246657f8dSmmusante &nvroot) == 0); 39433f9d6ad7SLin Ling verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 394446657f8dSmmusante (uint64_t **)&vs, &c) == 0); 3945990b4856Slling health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 3946fa9e4066Sahrens 3947fa9e4066Sahrens (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp)); 3948fa9e4066Sahrens (void) printf(gettext(" state: %s\n"), health); 3949fa9e4066Sahrens 3950fa9e4066Sahrens switch (reason) { 3951fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_R: 3952fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not " 3953fa9e4066Sahrens "be opened. Sufficient replicas exist for\n\tthe pool to " 3954fa9e4066Sahrens "continue functioning in a degraded state.\n")); 3955fa9e4066Sahrens (void) printf(gettext("action: Attach the missing device and " 3956fa9e4066Sahrens "online it using 'zpool online'.\n")); 3957fa9e4066Sahrens break; 3958fa9e4066Sahrens 3959fa9e4066Sahrens case ZPOOL_STATUS_MISSING_DEV_NR: 3960fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not " 3961fa9e4066Sahrens "be opened. There are insufficient\n\treplicas for the " 3962fa9e4066Sahrens "pool to continue functioning.\n")); 3963fa9e4066Sahrens (void) printf(gettext("action: Attach the missing device and " 3964fa9e4066Sahrens "online it using 'zpool online'.\n")); 3965fa9e4066Sahrens break; 3966fa9e4066Sahrens 3967fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_R: 3968fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not " 3969fa9e4066Sahrens "be used because the label is missing or\n\tinvalid. " 3970fa9e4066Sahrens "Sufficient replicas exist for the pool to continue\n\t" 3971fa9e4066Sahrens "functioning in a degraded state.\n")); 3972fa9e4066Sahrens (void) printf(gettext("action: Replace the device using " 3973fa9e4066Sahrens "'zpool replace'.\n")); 3974fa9e4066Sahrens break; 3975fa9e4066Sahrens 3976fa9e4066Sahrens case ZPOOL_STATUS_CORRUPT_LABEL_NR: 3977fa9e4066Sahrens (void) printf(gettext("status: One or more devices could not " 3978b1b8ab34Slling "be used because the label is missing \n\tor invalid. " 3979fa9e4066Sahrens "There are insufficient replicas for the pool to " 3980fa9e4066Sahrens "continue\n\tfunctioning.\n")); 3981468c413aSTim Haley zpool_explain_recover(zpool_get_handle(zhp), 3982468c413aSTim Haley zpool_get_name(zhp), reason, config); 3983fa9e4066Sahrens break; 3984fa9e4066Sahrens 3985fa9e4066Sahrens case ZPOOL_STATUS_FAILING_DEV: 3986fa9e4066Sahrens (void) printf(gettext("status: One or more devices has " 3987fa9e4066Sahrens "experienced an unrecoverable error. An\n\tattempt was " 3988fa9e4066Sahrens "made to correct the error. Applications are " 3989fa9e4066Sahrens "unaffected.\n")); 3990fa9e4066Sahrens (void) printf(gettext("action: Determine if the device needs " 3991fa9e4066Sahrens "to be replaced, and clear the errors\n\tusing " 3992ea8dc4b6Seschrock "'zpool clear' or replace the device with 'zpool " 3993fa9e4066Sahrens "replace'.\n")); 3994fa9e4066Sahrens break; 3995fa9e4066Sahrens 3996fa9e4066Sahrens case ZPOOL_STATUS_OFFLINE_DEV: 3997fa9e4066Sahrens (void) printf(gettext("status: One or more devices has " 3998d7d4af51Smmusante "been taken offline by the administrator.\n\tSufficient " 3999fa9e4066Sahrens "replicas exist for the pool to continue functioning in " 4000fa9e4066Sahrens "a\n\tdegraded state.\n")); 4001fa9e4066Sahrens (void) printf(gettext("action: Online the device using " 4002fa9e4066Sahrens "'zpool online' or replace the device with\n\t'zpool " 4003fa9e4066Sahrens "replace'.\n")); 4004fa9e4066Sahrens break; 4005fa9e4066Sahrens 4006c25309d4SGeorge Wilson case ZPOOL_STATUS_REMOVED_DEV: 4007c25309d4SGeorge Wilson (void) printf(gettext("status: One or more devices has " 4008c25309d4SGeorge Wilson "been removed by the administrator.\n\tSufficient " 4009c25309d4SGeorge Wilson "replicas exist for the pool to continue functioning in " 4010c25309d4SGeorge Wilson "a\n\tdegraded state.\n")); 4011c25309d4SGeorge Wilson (void) printf(gettext("action: Online the device using " 4012c25309d4SGeorge Wilson "'zpool online' or replace the device with\n\t'zpool " 4013c25309d4SGeorge Wilson "replace'.\n")); 4014c25309d4SGeorge Wilson break; 4015c25309d4SGeorge Wilson 4016fa9e4066Sahrens case ZPOOL_STATUS_RESILVERING: 4017fa9e4066Sahrens (void) printf(gettext("status: One or more devices is " 4018fa9e4066Sahrens "currently being resilvered. The pool will\n\tcontinue " 4019fa9e4066Sahrens "to function, possibly in a degraded state.\n")); 4020fa9e4066Sahrens (void) printf(gettext("action: Wait for the resilver to " 4021fa9e4066Sahrens "complete.\n")); 4022fa9e4066Sahrens break; 4023fa9e4066Sahrens 4024ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_DATA: 4025ea8dc4b6Seschrock (void) printf(gettext("status: One or more devices has " 4026ea8dc4b6Seschrock "experienced an error resulting in data\n\tcorruption. " 4027ea8dc4b6Seschrock "Applications may be affected.\n")); 4028ea8dc4b6Seschrock (void) printf(gettext("action: Restore the file in question " 4029ea8dc4b6Seschrock "if possible. Otherwise restore the\n\tentire pool from " 4030ea8dc4b6Seschrock "backup.\n")); 4031ea8dc4b6Seschrock break; 4032ea8dc4b6Seschrock 4033ea8dc4b6Seschrock case ZPOOL_STATUS_CORRUPT_POOL: 4034ea8dc4b6Seschrock (void) printf(gettext("status: The pool metadata is corrupted " 4035ea8dc4b6Seschrock "and the pool cannot be opened.\n")); 4036468c413aSTim Haley zpool_explain_recover(zpool_get_handle(zhp), 4037468c413aSTim Haley zpool_get_name(zhp), reason, config); 4038ea8dc4b6Seschrock break; 4039ea8dc4b6Seschrock 4040eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_OLDER: 404157221772SChristopher Siden (void) printf(gettext("status: The pool is formatted using a " 404257221772SChristopher Siden "legacy on-disk format. The pool can\n\tstill be used, " 404357221772SChristopher Siden "but some features are unavailable.\n")); 4044eaca9bbdSeschrock (void) printf(gettext("action: Upgrade the pool using 'zpool " 4045eaca9bbdSeschrock "upgrade'. Once this is done, the\n\tpool will no longer " 404657221772SChristopher Siden "be accessible on software that does not support feature\n" 404757221772SChristopher Siden "\tflags.\n")); 4048eaca9bbdSeschrock break; 4049eaca9bbdSeschrock 4050eaca9bbdSeschrock case ZPOOL_STATUS_VERSION_NEWER: 4051eaca9bbdSeschrock (void) printf(gettext("status: The pool has been upgraded to a " 4052eaca9bbdSeschrock "newer, incompatible on-disk version.\n\tThe pool cannot " 4053eaca9bbdSeschrock "be accessed on this system.\n")); 4054eaca9bbdSeschrock (void) printf(gettext("action: Access the pool from a system " 4055eaca9bbdSeschrock "running more recent software, or\n\trestore the pool from " 4056eaca9bbdSeschrock "backup.\n")); 4057eaca9bbdSeschrock break; 4058eaca9bbdSeschrock 405957221772SChristopher Siden case ZPOOL_STATUS_FEAT_DISABLED: 406057221772SChristopher Siden (void) printf(gettext("status: Some supported features are not " 406157221772SChristopher Siden "enabled on the pool. The pool can\n\tstill be used, but " 406257221772SChristopher Siden "some features are unavailable.\n")); 406357221772SChristopher Siden (void) printf(gettext("action: Enable all features using " 406457221772SChristopher Siden "'zpool upgrade'. Once this is done,\n\tthe pool may no " 406557221772SChristopher Siden "longer be accessible by software that does not support\n\t" 406657221772SChristopher Siden "the features. See zpool-features(5) for details.\n")); 406757221772SChristopher Siden break; 406857221772SChristopher Siden 4069ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_READ: 4070ad135b5dSChristopher Siden (void) printf(gettext("status: The pool cannot be accessed on " 4071ad135b5dSChristopher Siden "this system because it uses the\n\tfollowing feature(s) " 4072ad135b5dSChristopher Siden "not supported on this system:\n")); 4073ad135b5dSChristopher Siden zpool_print_unsup_feat(config); 4074ad135b5dSChristopher Siden (void) printf("\n"); 4075ad135b5dSChristopher Siden (void) printf(gettext("action: Access the pool from a system " 4076ad135b5dSChristopher Siden "that supports the required feature(s),\n\tor restore the " 4077ad135b5dSChristopher Siden "pool from backup.\n")); 4078ad135b5dSChristopher Siden break; 4079ad135b5dSChristopher Siden 4080ad135b5dSChristopher Siden case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 4081ad135b5dSChristopher Siden (void) printf(gettext("status: The pool can only be accessed " 4082ad135b5dSChristopher Siden "in read-only mode on this system. It\n\tcannot be " 4083ad135b5dSChristopher Siden "accessed in read-write mode because it uses the " 4084ad135b5dSChristopher Siden "following\n\tfeature(s) not supported on this system:\n")); 4085ad135b5dSChristopher Siden zpool_print_unsup_feat(config); 4086ad135b5dSChristopher Siden (void) printf("\n"); 4087ad135b5dSChristopher Siden (void) printf(gettext("action: The pool cannot be accessed in " 4088ad135b5dSChristopher Siden "read-write mode. Import the pool with\n" 4089ad135b5dSChristopher Siden "\t\"-o readonly=on\", access the pool from a system that " 4090ad135b5dSChristopher Siden "supports the\n\trequired feature(s), or restore the " 4091ad135b5dSChristopher Siden "pool from backup.\n")); 4092ad135b5dSChristopher Siden break; 4093ad135b5dSChristopher Siden 40943d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_R: 40953d7072f8Seschrock (void) printf(gettext("status: One or more devices are " 40963d7072f8Seschrock "faulted in response to persistent errors.\n\tSufficient " 40973d7072f8Seschrock "replicas exist for the pool to continue functioning " 40983d7072f8Seschrock "in a\n\tdegraded state.\n")); 40993d7072f8Seschrock (void) printf(gettext("action: Replace the faulted device, " 41003d7072f8Seschrock "or use 'zpool clear' to mark the device\n\trepaired.\n")); 41013d7072f8Seschrock break; 41023d7072f8Seschrock 41033d7072f8Seschrock case ZPOOL_STATUS_FAULTED_DEV_NR: 41043d7072f8Seschrock (void) printf(gettext("status: One or more devices are " 41053d7072f8Seschrock "faulted in response to persistent errors. There are " 41063d7072f8Seschrock "insufficient replicas for the pool to\n\tcontinue " 41073d7072f8Seschrock "functioning.\n")); 41083d7072f8Seschrock (void) printf(gettext("action: Destroy and re-create the pool " 41093d7072f8Seschrock "from a backup source. Manually marking the device\n" 41103d7072f8Seschrock "\trepaired using 'zpool clear' may allow some data " 41113d7072f8Seschrock "to be recovered.\n")); 41123d7072f8Seschrock break; 41133d7072f8Seschrock 411432b87932Sek110237 case ZPOOL_STATUS_IO_FAILURE_WAIT: 411532b87932Sek110237 case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 411632b87932Sek110237 (void) printf(gettext("status: One or more devices are " 41178a79c1b5Sek110237 "faulted in response to IO failures.\n")); 411832b87932Sek110237 (void) printf(gettext("action: Make sure the affected devices " 411932b87932Sek110237 "are connected, then run 'zpool clear'.\n")); 412032b87932Sek110237 break; 412132b87932Sek110237 4122b87f3af3Sperrin case ZPOOL_STATUS_BAD_LOG: 4123b87f3af3Sperrin (void) printf(gettext("status: An intent log record " 4124b87f3af3Sperrin "could not be read.\n" 4125b87f3af3Sperrin "\tWaiting for adminstrator intervention to fix the " 4126b87f3af3Sperrin "faulted pool.\n")); 4127b87f3af3Sperrin (void) printf(gettext("action: Either restore the affected " 4128b87f3af3Sperrin "device(s) and run 'zpool online',\n" 4129b87f3af3Sperrin "\tor ignore the intent log records by running " 4130b87f3af3Sperrin "'zpool clear'.\n")); 4131b87f3af3Sperrin break; 4132b87f3af3Sperrin 4133fa9e4066Sahrens default: 4134fa9e4066Sahrens /* 4135fa9e4066Sahrens * The remaining errors can't actually be generated, yet. 4136fa9e4066Sahrens */ 4137fa9e4066Sahrens assert(reason == ZPOOL_STATUS_OK); 4138fa9e4066Sahrens } 4139fa9e4066Sahrens 4140fa9e4066Sahrens if (msgid != NULL) 4141654b400cSJoshua M. Clulow (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 4142fa9e4066Sahrens msgid); 4143fa9e4066Sahrens 4144fa9e4066Sahrens if (config != NULL) { 4145fa9e4066Sahrens int namewidth; 4146ea8dc4b6Seschrock uint64_t nerr; 4147fa94a07fSbrendan nvlist_t **spares, **l2cache; 4148fa94a07fSbrendan uint_t nspares, nl2cache; 41493f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 4150fa9e4066Sahrens 41513f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 41523f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c); 41533f9d6ad7SLin Ling print_scan_status(ps); 4154fa9e4066Sahrens 4155c67d9675Seschrock namewidth = max_width(zhp, nvroot, 0, 0); 4156fa9e4066Sahrens if (namewidth < 10) 4157fa9e4066Sahrens namewidth = 10; 4158fa9e4066Sahrens 4159fa9e4066Sahrens (void) printf(gettext("config:\n\n")); 4160fa9e4066Sahrens (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth, 4161fa9e4066Sahrens "NAME", "STATE", "READ", "WRITE", "CKSUM"); 4162c67d9675Seschrock print_status_config(zhp, zpool_get_name(zhp), nvroot, 4163aa8cf21aSNeil Perrin namewidth, 0, B_FALSE); 416499653d4eSeschrock 41654dea40f0SNeil Perrin if (num_logs(nvroot) > 0) 4166e6ca193dSGeorge Wilson print_logs(zhp, nvroot, namewidth, B_TRUE); 4167fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 4168fa94a07fSbrendan &l2cache, &nl2cache) == 0) 4169fa94a07fSbrendan print_l2cache(zhp, l2cache, nl2cache, namewidth); 4170fa94a07fSbrendan 417199653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 417299653d4eSeschrock &spares, &nspares) == 0) 417399653d4eSeschrock print_spares(zhp, spares, nspares, namewidth); 4174ea8dc4b6Seschrock 4175ea8dc4b6Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 4176ea8dc4b6Seschrock &nerr) == 0) { 417755434c77Sek110237 nvlist_t *nverrlist = NULL; 417855434c77Sek110237 4179ea8dc4b6Seschrock /* 4180ea8dc4b6Seschrock * If the approximate error count is small, get a 4181ea8dc4b6Seschrock * precise count by fetching the entire log and 4182ea8dc4b6Seschrock * uniquifying the results. 4183ea8dc4b6Seschrock */ 418475519f38Sek110237 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && 418555434c77Sek110237 zpool_get_errlog(zhp, &nverrlist) == 0) { 418655434c77Sek110237 nvpair_t *elem; 418755434c77Sek110237 418855434c77Sek110237 elem = NULL; 418955434c77Sek110237 nerr = 0; 419055434c77Sek110237 while ((elem = nvlist_next_nvpair(nverrlist, 419155434c77Sek110237 elem)) != NULL) { 419255434c77Sek110237 nerr++; 419355434c77Sek110237 } 419455434c77Sek110237 } 419555434c77Sek110237 nvlist_free(nverrlist); 4196ea8dc4b6Seschrock 4197ea8dc4b6Seschrock (void) printf("\n"); 419899653d4eSeschrock 4199ea8dc4b6Seschrock if (nerr == 0) 4200ea8dc4b6Seschrock (void) printf(gettext("errors: No known data " 4201ea8dc4b6Seschrock "errors\n")); 4202ea8dc4b6Seschrock else if (!cbp->cb_verbose) 4203e9dbad6fSeschrock (void) printf(gettext("errors: %llu data " 42045ad82045Snd150628 "errors, use '-v' for a list\n"), 42055ad82045Snd150628 (u_longlong_t)nerr); 4206ea8dc4b6Seschrock else 4207ea8dc4b6Seschrock print_error_log(zhp); 4208ea8dc4b6Seschrock } 42099eb19f4dSGeorge Wilson 42109eb19f4dSGeorge Wilson if (cbp->cb_dedup_stats) 42119eb19f4dSGeorge Wilson print_dedup_stats(config); 4212fa9e4066Sahrens } else { 4213fa9e4066Sahrens (void) printf(gettext("config: The configuration cannot be " 4214fa9e4066Sahrens "determined.\n")); 4215fa9e4066Sahrens } 4216fa9e4066Sahrens 4217fa9e4066Sahrens return (0); 4218fa9e4066Sahrens } 4219fa9e4066Sahrens 4220fa9e4066Sahrens /* 42213f9d6ad7SLin Ling * zpool status [-vx] [-T d|u] [pool] ... [interval [count]] 4222fa9e4066Sahrens * 4223fa9e4066Sahrens * -v Display complete error logs 4224fa9e4066Sahrens * -x Display only pools with potential problems 42259eb19f4dSGeorge Wilson * -D Display dedup status (undocumented) 42263f9d6ad7SLin Ling * -T Display a timestamp in date(1) or Unix format 4227fa9e4066Sahrens * 4228fa9e4066Sahrens * Describes the health status of all pools or some subset. 4229fa9e4066Sahrens */ 4230fa9e4066Sahrens int 4231fa9e4066Sahrens zpool_do_status(int argc, char **argv) 4232fa9e4066Sahrens { 4233fa9e4066Sahrens int c; 4234fa9e4066Sahrens int ret; 42353f9d6ad7SLin Ling unsigned long interval = 0, count = 0; 4236fa9e4066Sahrens status_cbdata_t cb = { 0 }; 4237fa9e4066Sahrens 4238fa9e4066Sahrens /* check options */ 42393f9d6ad7SLin Ling while ((c = getopt(argc, argv, "vxDT:")) != -1) { 4240fa9e4066Sahrens switch (c) { 4241fa9e4066Sahrens case 'v': 424299653d4eSeschrock cb.cb_verbose = B_TRUE; 4243fa9e4066Sahrens break; 4244fa9e4066Sahrens case 'x': 424599653d4eSeschrock cb.cb_explain = B_TRUE; 4246fa9e4066Sahrens break; 42479eb19f4dSGeorge Wilson case 'D': 42489eb19f4dSGeorge Wilson cb.cb_dedup_stats = B_TRUE; 42499eb19f4dSGeorge Wilson break; 42503f9d6ad7SLin Ling case 'T': 42513f9d6ad7SLin Ling get_timestamp_arg(*optarg); 42523f9d6ad7SLin Ling break; 4253fa9e4066Sahrens case '?': 4254fa9e4066Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4255fa9e4066Sahrens optopt); 425699653d4eSeschrock usage(B_FALSE); 4257fa9e4066Sahrens } 4258fa9e4066Sahrens } 4259fa9e4066Sahrens 4260fa9e4066Sahrens argc -= optind; 4261fa9e4066Sahrens argv += optind; 4262fa9e4066Sahrens 42633f9d6ad7SLin Ling get_interval_count(&argc, argv, &interval, &count); 4264fa9e4066Sahrens 4265e9dbad6fSeschrock if (argc == 0) 4266e9dbad6fSeschrock cb.cb_allpools = B_TRUE; 4267e9dbad6fSeschrock 42683f9d6ad7SLin Ling cb.cb_first = B_TRUE; 42693f9d6ad7SLin Ling 42703f9d6ad7SLin Ling for (;;) { 42713f9d6ad7SLin Ling if (timestamp_fmt != NODATE) 42723f9d6ad7SLin Ling print_timestamp(timestamp_fmt); 42733f9d6ad7SLin Ling 42743f9d6ad7SLin Ling ret = for_each_pool(argc, argv, B_TRUE, NULL, 42753f9d6ad7SLin Ling status_callback, &cb); 4276fa9e4066Sahrens 4277fa9e4066Sahrens if (argc == 0 && cb.cb_count == 0) 4278fa9e4066Sahrens (void) printf(gettext("no pools available\n")); 4279e9dbad6fSeschrock else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) 4280fa9e4066Sahrens (void) printf(gettext("all pools are healthy\n")); 4281fa9e4066Sahrens 42823f9d6ad7SLin Ling if (ret != 0) 4283fa9e4066Sahrens return (ret); 42843f9d6ad7SLin Ling 42853f9d6ad7SLin Ling if (interval == 0) 42863f9d6ad7SLin Ling break; 42873f9d6ad7SLin Ling 42883f9d6ad7SLin Ling if (count != 0 && --count == 0) 42893f9d6ad7SLin Ling break; 42903f9d6ad7SLin Ling 42913f9d6ad7SLin Ling (void) sleep(interval); 42923f9d6ad7SLin Ling } 42933f9d6ad7SLin Ling 42943f9d6ad7SLin Ling return (0); 4295fa9e4066Sahrens } 4296fa9e4066Sahrens 4297eaca9bbdSeschrock typedef struct upgrade_cbdata { 4298eaca9bbdSeschrock int cb_first; 429906eeb2adSek110237 int cb_argc; 4300990b4856Slling uint64_t cb_version; 430106eeb2adSek110237 char **cb_argv; 4302eaca9bbdSeschrock } upgrade_cbdata_t; 4303eaca9bbdSeschrock 4304eaca9bbdSeschrock static int 430557221772SChristopher Siden upgrade_version(zpool_handle_t *zhp, uint64_t version) 430657221772SChristopher Siden { 430757221772SChristopher Siden int ret; 430857221772SChristopher Siden nvlist_t *config; 430957221772SChristopher Siden uint64_t oldversion; 431057221772SChristopher Siden 431157221772SChristopher Siden config = zpool_get_config(zhp, NULL); 431257221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 431357221772SChristopher Siden &oldversion) == 0); 431457221772SChristopher Siden 431557221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 431657221772SChristopher Siden assert(oldversion < version); 431757221772SChristopher Siden 431857221772SChristopher Siden ret = zpool_upgrade(zhp, version); 431957221772SChristopher Siden if (ret != 0) 432057221772SChristopher Siden return (ret); 432157221772SChristopher Siden 432257221772SChristopher Siden if (version >= SPA_VERSION_FEATURES) { 432357221772SChristopher Siden (void) printf(gettext("Successfully upgraded " 432457221772SChristopher Siden "'%s' from version %llu to feature flags.\n"), 432557221772SChristopher Siden zpool_get_name(zhp), oldversion); 432657221772SChristopher Siden } else { 432757221772SChristopher Siden (void) printf(gettext("Successfully upgraded " 432857221772SChristopher Siden "'%s' from version %llu to version %llu.\n"), 432957221772SChristopher Siden zpool_get_name(zhp), oldversion, version); 433057221772SChristopher Siden } 433157221772SChristopher Siden 433257221772SChristopher Siden return (0); 433357221772SChristopher Siden } 433457221772SChristopher Siden 433557221772SChristopher Siden static int 433657221772SChristopher Siden upgrade_enable_all(zpool_handle_t *zhp, int *countp) 433757221772SChristopher Siden { 433857221772SChristopher Siden int i, ret, count; 433957221772SChristopher Siden boolean_t firstff = B_TRUE; 434057221772SChristopher Siden nvlist_t *enabled = zpool_get_features(zhp); 434157221772SChristopher Siden 434257221772SChristopher Siden count = 0; 434357221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) { 434457221772SChristopher Siden const char *fname = spa_feature_table[i].fi_uname; 434557221772SChristopher Siden const char *fguid = spa_feature_table[i].fi_guid; 434657221772SChristopher Siden if (!nvlist_exists(enabled, fguid)) { 434757221772SChristopher Siden char *propname; 434857221772SChristopher Siden verify(-1 != asprintf(&propname, "feature@%s", fname)); 434957221772SChristopher Siden ret = zpool_set_prop(zhp, propname, 435057221772SChristopher Siden ZFS_FEATURE_ENABLED); 435157221772SChristopher Siden if (ret != 0) { 435257221772SChristopher Siden free(propname); 435357221772SChristopher Siden return (ret); 435457221772SChristopher Siden } 435557221772SChristopher Siden count++; 435657221772SChristopher Siden 435757221772SChristopher Siden if (firstff) { 435857221772SChristopher Siden (void) printf(gettext("Enabled the " 435957221772SChristopher Siden "following features on '%s':\n"), 436057221772SChristopher Siden zpool_get_name(zhp)); 436157221772SChristopher Siden firstff = B_FALSE; 436257221772SChristopher Siden } 436357221772SChristopher Siden (void) printf(gettext(" %s\n"), fname); 436457221772SChristopher Siden free(propname); 436557221772SChristopher Siden } 436657221772SChristopher Siden } 436757221772SChristopher Siden 436857221772SChristopher Siden if (countp != NULL) 436957221772SChristopher Siden *countp = count; 437057221772SChristopher Siden return (0); 437157221772SChristopher Siden } 437257221772SChristopher Siden 437357221772SChristopher Siden static int 4374eaca9bbdSeschrock upgrade_cb(zpool_handle_t *zhp, void *arg) 4375eaca9bbdSeschrock { 4376eaca9bbdSeschrock upgrade_cbdata_t *cbp = arg; 4377eaca9bbdSeschrock nvlist_t *config; 4378eaca9bbdSeschrock uint64_t version; 437957221772SChristopher Siden boolean_t printnl = B_FALSE; 438057221772SChristopher Siden int ret; 4381eaca9bbdSeschrock 4382eaca9bbdSeschrock config = zpool_get_config(zhp, NULL); 4383eaca9bbdSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4384eaca9bbdSeschrock &version) == 0); 4385eaca9bbdSeschrock 438657221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(version)); 4387eaca9bbdSeschrock 438857221772SChristopher Siden if (version < cbp->cb_version) { 438999653d4eSeschrock cbp->cb_first = B_FALSE; 439057221772SChristopher Siden ret = upgrade_version(zhp, cbp->cb_version); 439157221772SChristopher Siden if (ret != 0) 439257221772SChristopher Siden return (ret); 439357221772SChristopher Siden printnl = B_TRUE; 439457221772SChristopher Siden 43954445fffbSMatthew Ahrens /* 43964445fffbSMatthew Ahrens * If they did "zpool upgrade -a", then we could 43974445fffbSMatthew Ahrens * be doing ioctls to different pools. We need 43984445fffbSMatthew Ahrens * to log this history once to each pool, and bypass 43994445fffbSMatthew Ahrens * the normal history logging that happens in main(). 44004445fffbSMatthew Ahrens */ 44014445fffbSMatthew Ahrens (void) zpool_log_history(g_zfs, history_str); 44024445fffbSMatthew Ahrens log_history = B_FALSE; 440306eeb2adSek110237 } 4404eaca9bbdSeschrock 440557221772SChristopher Siden if (cbp->cb_version >= SPA_VERSION_FEATURES) { 440657221772SChristopher Siden int count; 440757221772SChristopher Siden ret = upgrade_enable_all(zhp, &count); 440857221772SChristopher Siden if (ret != 0) 440957221772SChristopher Siden return (ret); 441057221772SChristopher Siden 441157221772SChristopher Siden if (count > 0) { 441257221772SChristopher Siden cbp->cb_first = B_FALSE; 441357221772SChristopher Siden printnl = B_TRUE; 441457221772SChristopher Siden } 441557221772SChristopher Siden } 441657221772SChristopher Siden 441757221772SChristopher Siden if (printnl) { 441857221772SChristopher Siden (void) printf(gettext("\n")); 441957221772SChristopher Siden } 442057221772SChristopher Siden 442157221772SChristopher Siden return (0); 442257221772SChristopher Siden } 442357221772SChristopher Siden 442457221772SChristopher Siden static int 442557221772SChristopher Siden upgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 442657221772SChristopher Siden { 442757221772SChristopher Siden upgrade_cbdata_t *cbp = arg; 442857221772SChristopher Siden nvlist_t *config; 442957221772SChristopher Siden uint64_t version; 443057221772SChristopher Siden 443157221772SChristopher Siden config = zpool_get_config(zhp, NULL); 443257221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 443357221772SChristopher Siden &version) == 0); 443457221772SChristopher Siden 443557221772SChristopher Siden assert(SPA_VERSION_IS_SUPPORTED(version)); 443657221772SChristopher Siden 443757221772SChristopher Siden if (version < SPA_VERSION_FEATURES) { 4438eaca9bbdSeschrock if (cbp->cb_first) { 4439eaca9bbdSeschrock (void) printf(gettext("The following pools are " 444057221772SChristopher Siden "formatted with legacy version numbers and can\n" 444157221772SChristopher Siden "be upgraded to use feature flags. After " 444257221772SChristopher Siden "being upgraded, these pools\nwill no " 444357221772SChristopher Siden "longer be accessible by software that does not " 444457221772SChristopher Siden "support feature\nflags.\n\n")); 4445eaca9bbdSeschrock (void) printf(gettext("VER POOL\n")); 4446eaca9bbdSeschrock (void) printf(gettext("--- ------------\n")); 444799653d4eSeschrock cbp->cb_first = B_FALSE; 4448eaca9bbdSeschrock } 4449eaca9bbdSeschrock 44505ad82045Snd150628 (void) printf("%2llu %s\n", (u_longlong_t)version, 4451eaca9bbdSeschrock zpool_get_name(zhp)); 4452eaca9bbdSeschrock } 4453eaca9bbdSeschrock 445457221772SChristopher Siden return (0); 445557221772SChristopher Siden } 445657221772SChristopher Siden 445757221772SChristopher Siden static int 445857221772SChristopher Siden upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 445957221772SChristopher Siden { 446057221772SChristopher Siden upgrade_cbdata_t *cbp = arg; 446157221772SChristopher Siden nvlist_t *config; 446257221772SChristopher Siden uint64_t version; 446357221772SChristopher Siden 446457221772SChristopher Siden config = zpool_get_config(zhp, NULL); 446557221772SChristopher Siden verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 446657221772SChristopher Siden &version) == 0); 446757221772SChristopher Siden 446857221772SChristopher Siden if (version >= SPA_VERSION_FEATURES) { 446957221772SChristopher Siden int i; 447057221772SChristopher Siden boolean_t poolfirst = B_TRUE; 447157221772SChristopher Siden nvlist_t *enabled = zpool_get_features(zhp); 447257221772SChristopher Siden 447357221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) { 447457221772SChristopher Siden const char *fguid = spa_feature_table[i].fi_guid; 447557221772SChristopher Siden const char *fname = spa_feature_table[i].fi_uname; 447657221772SChristopher Siden if (!nvlist_exists(enabled, fguid)) { 447757221772SChristopher Siden if (cbp->cb_first) { 447857221772SChristopher Siden (void) printf(gettext("\nSome " 447957221772SChristopher Siden "supported features are not " 448057221772SChristopher Siden "enabled on the following pools. " 448157221772SChristopher Siden "Once a\nfeature is enabled the " 448257221772SChristopher Siden "pool may become incompatible with " 448357221772SChristopher Siden "software\nthat does not support " 448457221772SChristopher Siden "the feature. See " 448557221772SChristopher Siden "zpool-features(5) for " 448657221772SChristopher Siden "details.\n\n")); 448757221772SChristopher Siden (void) printf(gettext("POOL " 448857221772SChristopher Siden "FEATURE\n")); 448957221772SChristopher Siden (void) printf(gettext("------" 449057221772SChristopher Siden "---------\n")); 449157221772SChristopher Siden cbp->cb_first = B_FALSE; 449257221772SChristopher Siden } 449357221772SChristopher Siden 449457221772SChristopher Siden if (poolfirst) { 449557221772SChristopher Siden (void) printf(gettext("%s\n"), 449657221772SChristopher Siden zpool_get_name(zhp)); 449757221772SChristopher Siden poolfirst = B_FALSE; 449857221772SChristopher Siden } 449957221772SChristopher Siden 450057221772SChristopher Siden (void) printf(gettext(" %s\n"), fname); 450157221772SChristopher Siden } 450257221772SChristopher Siden } 450357221772SChristopher Siden } 450457221772SChristopher Siden 450557221772SChristopher Siden return (0); 4506eaca9bbdSeschrock } 4507eaca9bbdSeschrock 4508eaca9bbdSeschrock /* ARGSUSED */ 4509eaca9bbdSeschrock static int 451006eeb2adSek110237 upgrade_one(zpool_handle_t *zhp, void *data) 4511eaca9bbdSeschrock { 451257221772SChristopher Siden boolean_t printnl = B_FALSE; 4513990b4856Slling upgrade_cbdata_t *cbp = data; 4514990b4856Slling uint64_t cur_version; 4515eaca9bbdSeschrock int ret; 4516eaca9bbdSeschrock 45178654d025Sperrin if (strcmp("log", zpool_get_name(zhp)) == 0) { 45188654d025Sperrin (void) printf(gettext("'log' is now a reserved word\n" 45198654d025Sperrin "Pool 'log' must be renamed using export and import" 45208654d025Sperrin " to upgrade.\n")); 45218654d025Sperrin return (1); 45228654d025Sperrin } 4523990b4856Slling 4524990b4856Slling cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 4525e6c728e1Sbrendan if (cur_version > cbp->cb_version) { 4526eaca9bbdSeschrock (void) printf(gettext("Pool '%s' is already formatted " 452757221772SChristopher Siden "using more current version '%llu'.\n\n"), 4528e6c728e1Sbrendan zpool_get_name(zhp), cur_version); 4529e6c728e1Sbrendan return (0); 4530e6c728e1Sbrendan } 453157221772SChristopher Siden 453257221772SChristopher Siden if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 4533e6c728e1Sbrendan (void) printf(gettext("Pool '%s' is already formatted " 453457221772SChristopher Siden "using version %llu.\n\n"), zpool_get_name(zhp), 453557221772SChristopher Siden cbp->cb_version); 4536eaca9bbdSeschrock return (0); 4537eaca9bbdSeschrock } 4538eaca9bbdSeschrock 453957221772SChristopher Siden if (cur_version != cbp->cb_version) { 454057221772SChristopher Siden printnl = B_TRUE; 454157221772SChristopher Siden ret = upgrade_version(zhp, cbp->cb_version); 454257221772SChristopher Siden if (ret != 0) 454357221772SChristopher Siden return (ret); 454406eeb2adSek110237 } 4545eaca9bbdSeschrock 454657221772SChristopher Siden if (cbp->cb_version >= SPA_VERSION_FEATURES) { 454757221772SChristopher Siden int count = 0; 454857221772SChristopher Siden ret = upgrade_enable_all(zhp, &count); 454957221772SChristopher Siden if (ret != 0) 455057221772SChristopher Siden return (ret); 455157221772SChristopher Siden 455257221772SChristopher Siden if (count != 0) { 455357221772SChristopher Siden printnl = B_TRUE; 455457221772SChristopher Siden } else if (cur_version == SPA_VERSION) { 455557221772SChristopher Siden (void) printf(gettext("Pool '%s' already has all " 455657221772SChristopher Siden "supported features enabled.\n"), 455757221772SChristopher Siden zpool_get_name(zhp)); 455857221772SChristopher Siden } 455957221772SChristopher Siden } 456057221772SChristopher Siden 456157221772SChristopher Siden if (printnl) { 456257221772SChristopher Siden (void) printf(gettext("\n")); 456357221772SChristopher Siden } 456457221772SChristopher Siden 456557221772SChristopher Siden return (0); 4566eaca9bbdSeschrock } 4567eaca9bbdSeschrock 4568eaca9bbdSeschrock /* 4569eaca9bbdSeschrock * zpool upgrade 4570eaca9bbdSeschrock * zpool upgrade -v 4571990b4856Slling * zpool upgrade [-V version] <-a | pool ...> 4572eaca9bbdSeschrock * 4573eaca9bbdSeschrock * With no arguments, display downrev'd ZFS pool available for upgrade. 4574eaca9bbdSeschrock * Individual pools can be upgraded by specifying the pool, and '-a' will 4575eaca9bbdSeschrock * upgrade all pools. 4576eaca9bbdSeschrock */ 4577eaca9bbdSeschrock int 4578eaca9bbdSeschrock zpool_do_upgrade(int argc, char **argv) 4579eaca9bbdSeschrock { 4580eaca9bbdSeschrock int c; 4581eaca9bbdSeschrock upgrade_cbdata_t cb = { 0 }; 4582eaca9bbdSeschrock int ret = 0; 4583eaca9bbdSeschrock boolean_t showversions = B_FALSE; 458457221772SChristopher Siden boolean_t upgradeall = B_FALSE; 4585990b4856Slling char *end; 4586990b4856Slling 4587eaca9bbdSeschrock 4588eaca9bbdSeschrock /* check options */ 4589478ed9adSEric Taylor while ((c = getopt(argc, argv, ":avV:")) != -1) { 4590eaca9bbdSeschrock switch (c) { 4591eaca9bbdSeschrock case 'a': 459257221772SChristopher Siden upgradeall = B_TRUE; 4593eaca9bbdSeschrock break; 4594eaca9bbdSeschrock case 'v': 4595eaca9bbdSeschrock showversions = B_TRUE; 4596eaca9bbdSeschrock break; 4597990b4856Slling case 'V': 4598990b4856Slling cb.cb_version = strtoll(optarg, &end, 10); 4599ad135b5dSChristopher Siden if (*end != '\0' || 4600ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 4601990b4856Slling (void) fprintf(stderr, 4602990b4856Slling gettext("invalid version '%s'\n"), optarg); 4603990b4856Slling usage(B_FALSE); 4604990b4856Slling } 4605990b4856Slling break; 4606478ed9adSEric Taylor case ':': 4607478ed9adSEric Taylor (void) fprintf(stderr, gettext("missing argument for " 4608478ed9adSEric Taylor "'%c' option\n"), optopt); 4609478ed9adSEric Taylor usage(B_FALSE); 4610478ed9adSEric Taylor break; 4611eaca9bbdSeschrock case '?': 4612eaca9bbdSeschrock (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4613eaca9bbdSeschrock optopt); 461499653d4eSeschrock usage(B_FALSE); 4615eaca9bbdSeschrock } 4616eaca9bbdSeschrock } 4617eaca9bbdSeschrock 461806eeb2adSek110237 cb.cb_argc = argc; 461906eeb2adSek110237 cb.cb_argv = argv; 4620eaca9bbdSeschrock argc -= optind; 4621eaca9bbdSeschrock argv += optind; 4622eaca9bbdSeschrock 4623351420b3Slling if (cb.cb_version == 0) { 4624351420b3Slling cb.cb_version = SPA_VERSION; 462557221772SChristopher Siden } else if (!upgradeall && argc == 0) { 4626351420b3Slling (void) fprintf(stderr, gettext("-V option is " 4627351420b3Slling "incompatible with other arguments\n")); 4628351420b3Slling usage(B_FALSE); 4629351420b3Slling } 4630351420b3Slling 4631eaca9bbdSeschrock if (showversions) { 463257221772SChristopher Siden if (upgradeall || argc != 0) { 4633eaca9bbdSeschrock (void) fprintf(stderr, gettext("-v option is " 4634eaca9bbdSeschrock "incompatible with other arguments\n")); 463599653d4eSeschrock usage(B_FALSE); 4636eaca9bbdSeschrock } 463757221772SChristopher Siden } else if (upgradeall) { 4638eaca9bbdSeschrock if (argc != 0) { 4639351420b3Slling (void) fprintf(stderr, gettext("-a option should not " 4640351420b3Slling "be used along with a pool name\n")); 464199653d4eSeschrock usage(B_FALSE); 4642eaca9bbdSeschrock } 4643eaca9bbdSeschrock } 4644eaca9bbdSeschrock 4645ad135b5dSChristopher Siden (void) printf(gettext("This system supports ZFS pool feature " 4646ad135b5dSChristopher Siden "flags.\n\n")); 4647eaca9bbdSeschrock if (showversions) { 464857221772SChristopher Siden int i; 464957221772SChristopher Siden 465057221772SChristopher Siden (void) printf(gettext("The following features are " 465157221772SChristopher Siden "supported:\n\n")); 465257221772SChristopher Siden (void) printf(gettext("FEAT DESCRIPTION\n")); 465357221772SChristopher Siden (void) printf("----------------------------------------------" 465457221772SChristopher Siden "---------------\n"); 465557221772SChristopher Siden for (i = 0; i < SPA_FEATURES; i++) { 465657221772SChristopher Siden zfeature_info_t *fi = &spa_feature_table[i]; 465757221772SChristopher Siden const char *ro = fi->fi_can_readonly ? 465857221772SChristopher Siden " (read-only compatible)" : ""; 465957221772SChristopher Siden 466057221772SChristopher Siden (void) printf("%-37s%s\n", fi->fi_uname, ro); 466157221772SChristopher Siden (void) printf(" %s\n", fi->fi_desc); 466257221772SChristopher Siden } 466357221772SChristopher Siden (void) printf("\n"); 466457221772SChristopher Siden 466557221772SChristopher Siden (void) printf(gettext("The following legacy versions are also " 4666d7d4af51Smmusante "supported:\n\n")); 4667eaca9bbdSeschrock (void) printf(gettext("VER DESCRIPTION\n")); 4668eaca9bbdSeschrock (void) printf("--- -----------------------------------------" 4669eaca9bbdSeschrock "---------------\n"); 467099653d4eSeschrock (void) printf(gettext(" 1 Initial ZFS version\n")); 467144cd46caSbillm (void) printf(gettext(" 2 Ditto blocks " 467244cd46caSbillm "(replicated metadata)\n")); 467399653d4eSeschrock (void) printf(gettext(" 3 Hot spares and double parity " 467499653d4eSeschrock "RAID-Z\n")); 4675d7306b64Sek110237 (void) printf(gettext(" 4 zpool history\n")); 4676c9431fa1Sahl (void) printf(gettext(" 5 Compression using the gzip " 4677c9431fa1Sahl "algorithm\n")); 4678990b4856Slling (void) printf(gettext(" 6 bootfs pool property\n")); 46798654d025Sperrin (void) printf(gettext(" 7 Separate intent log devices\n")); 4680ecd6cf80Smarks (void) printf(gettext(" 8 Delegated administration\n")); 4681a9799022Sck153898 (void) printf(gettext(" 9 refquota and refreservation " 4682a9799022Sck153898 "properties\n")); 4683fa94a07fSbrendan (void) printf(gettext(" 10 Cache devices\n")); 4684088f3894Sahrens (void) printf(gettext(" 11 Improved scrub performance\n")); 4685bb0ade09Sahrens (void) printf(gettext(" 12 Snapshot properties\n")); 468674e7dc98SMatthew Ahrens (void) printf(gettext(" 13 snapused property\n")); 468714843421SMatthew Ahrens (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 468814843421SMatthew Ahrens (void) printf(gettext(" 15 user/group space accounting\n")); 4689478ed9adSEric Taylor (void) printf(gettext(" 16 stmf property support\n")); 46907aeab329SAdam Leventhal (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 4691b24ab676SJeff Bonwick (void) printf(gettext(" 18 Snapshot user holds\n")); 469288ecc943SGeorge Wilson (void) printf(gettext(" 19 Log device removal\n")); 4693b24ab676SJeff Bonwick (void) printf(gettext(" 20 Compression using zle " 4694b24ab676SJeff Bonwick "(zero-length encoding)\n")); 4695b24ab676SJeff Bonwick (void) printf(gettext(" 21 Deduplication\n")); 469692241e0bSTom Erickson (void) printf(gettext(" 22 Received properties\n")); 46976e1f5caaSNeil Perrin (void) printf(gettext(" 23 Slim ZIL\n")); 46980a586ceaSMark Shellenbaum (void) printf(gettext(" 24 System attributes\n")); 46993f9d6ad7SLin Ling (void) printf(gettext(" 25 Improved scrub stats\n")); 4700cde58dbcSMatthew Ahrens (void) printf(gettext(" 26 Improved snapshot deletion " 4701cde58dbcSMatthew Ahrens "performance\n")); 47026e0cbcaaSMatthew Ahrens (void) printf(gettext(" 27 Improved snapshot creation " 47036e0cbcaaSMatthew Ahrens "performance\n")); 4704cb04b873SMark J Musante (void) printf(gettext(" 28 Multiple vdev replacements\n")); 4705b24ab676SJeff Bonwick (void) printf(gettext("\nFor more information on a particular " 47069a8685acSstephanie scheffler "version, including supported releases,\n")); 47079a8685acSstephanie scheffler (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 470857221772SChristopher Siden } else if (argc == 0 && upgradeall) { 4709eaca9bbdSeschrock cb.cb_first = B_TRUE; 471099653d4eSeschrock ret = zpool_iter(g_zfs, upgrade_cb, &cb); 471157221772SChristopher Siden if (ret == 0 && cb.cb_first) { 471257221772SChristopher Siden if (cb.cb_version == SPA_VERSION) { 471357221772SChristopher Siden (void) printf(gettext("All pools are already " 471457221772SChristopher Siden "formatted using feature flags.\n\n")); 471557221772SChristopher Siden (void) printf(gettext("Every feature flags " 471657221772SChristopher Siden "pool already has all supported features " 471757221772SChristopher Siden "enabled.\n")); 471857221772SChristopher Siden } else { 471957221772SChristopher Siden (void) printf(gettext("All pools are already " 472057221772SChristopher Siden "formatted with version %llu or higher.\n"), 472157221772SChristopher Siden cb.cb_version); 4722eaca9bbdSeschrock } 4723eaca9bbdSeschrock } 472457221772SChristopher Siden } else if (argc == 0) { 472557221772SChristopher Siden cb.cb_first = B_TRUE; 472657221772SChristopher Siden ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 472757221772SChristopher Siden assert(ret == 0); 472857221772SChristopher Siden 472957221772SChristopher Siden if (cb.cb_first) { 473057221772SChristopher Siden (void) printf(gettext("All pools are formatted " 473157221772SChristopher Siden "using feature flags.\n\n")); 473257221772SChristopher Siden } else { 473357221772SChristopher Siden (void) printf(gettext("\nUse 'zpool upgrade -v' " 473457221772SChristopher Siden "for a list of available legacy versions.\n")); 473557221772SChristopher Siden } 4736eaca9bbdSeschrock 473757221772SChristopher Siden cb.cb_first = B_TRUE; 473857221772SChristopher Siden ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 473957221772SChristopher Siden assert(ret == 0); 474057221772SChristopher Siden 474157221772SChristopher Siden if (cb.cb_first) { 474257221772SChristopher Siden (void) printf(gettext("Every feature flags pool has " 474357221772SChristopher Siden "all supported features enabled.\n")); 474457221772SChristopher Siden } else { 474557221772SChristopher Siden (void) printf(gettext("\n")); 4746eaca9bbdSeschrock } 4747eaca9bbdSeschrock } else { 4748b1b8ab34Slling ret = for_each_pool(argc, argv, B_FALSE, NULL, 4749b1b8ab34Slling upgrade_one, &cb); 475006eeb2adSek110237 } 475106eeb2adSek110237 475206eeb2adSek110237 return (ret); 475306eeb2adSek110237 } 475406eeb2adSek110237 4755ecd6cf80Smarks typedef struct hist_cbdata { 4756ecd6cf80Smarks boolean_t first; 47574445fffbSMatthew Ahrens boolean_t longfmt; 47584445fffbSMatthew Ahrens boolean_t internal; 4759ecd6cf80Smarks } hist_cbdata_t; 4760ecd6cf80Smarks 476106eeb2adSek110237 /* 476206eeb2adSek110237 * Print out the command history for a specific pool. 476306eeb2adSek110237 */ 476406eeb2adSek110237 static int 476506eeb2adSek110237 get_history_one(zpool_handle_t *zhp, void *data) 476606eeb2adSek110237 { 476706eeb2adSek110237 nvlist_t *nvhis; 476806eeb2adSek110237 nvlist_t **records; 476906eeb2adSek110237 uint_t numrecords; 477006eeb2adSek110237 int ret, i; 4771ecd6cf80Smarks hist_cbdata_t *cb = (hist_cbdata_t *)data; 477206eeb2adSek110237 4773ecd6cf80Smarks cb->first = B_FALSE; 477406eeb2adSek110237 477506eeb2adSek110237 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 477606eeb2adSek110237 477706eeb2adSek110237 if ((ret = zpool_get_history(zhp, &nvhis)) != 0) 477806eeb2adSek110237 return (ret); 477906eeb2adSek110237 478006eeb2adSek110237 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 478106eeb2adSek110237 &records, &numrecords) == 0); 478206eeb2adSek110237 for (i = 0; i < numrecords; i++) { 47834445fffbSMatthew Ahrens nvlist_t *rec = records[i]; 47844445fffbSMatthew Ahrens char tbuf[30] = ""; 4785ecd6cf80Smarks 47864445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 47874445fffbSMatthew Ahrens time_t tsec; 47884445fffbSMatthew Ahrens struct tm t; 4789ecd6cf80Smarks 47904445fffbSMatthew Ahrens tsec = fnvlist_lookup_uint64(records[i], 47914445fffbSMatthew Ahrens ZPOOL_HIST_TIME); 479206eeb2adSek110237 (void) localtime_r(&tsec, &t); 479306eeb2adSek110237 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 47944445fffbSMatthew Ahrens } 47954445fffbSMatthew Ahrens 47964445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 47974445fffbSMatthew Ahrens (void) printf("%s %s", tbuf, 47984445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 47994445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 48004445fffbSMatthew Ahrens int ievent = 48014445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 48024445fffbSMatthew Ahrens if (!cb->internal) 48034445fffbSMatthew Ahrens continue; 48044445fffbSMatthew Ahrens if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 48054445fffbSMatthew Ahrens (void) printf("%s unrecognized record:\n", 48064445fffbSMatthew Ahrens tbuf); 48074445fffbSMatthew Ahrens dump_nvlist(rec, 4); 48084445fffbSMatthew Ahrens continue; 48094445fffbSMatthew Ahrens } 48104445fffbSMatthew Ahrens (void) printf("%s [internal %s txg:%lld] %s", tbuf, 48114445fffbSMatthew Ahrens zfs_history_event_names[ievent], 48124445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 48134445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 48144445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 48154445fffbSMatthew Ahrens if (!cb->internal) 48164445fffbSMatthew Ahrens continue; 48174445fffbSMatthew Ahrens (void) printf("%s [txg:%lld] %s", tbuf, 48184445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 48194445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 48204445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 48214445fffbSMatthew Ahrens (void) printf(" %s (%llu)", 48224445fffbSMatthew Ahrens fnvlist_lookup_string(rec, 48234445fffbSMatthew Ahrens ZPOOL_HIST_DSNAME), 48244445fffbSMatthew Ahrens fnvlist_lookup_uint64(rec, 48254445fffbSMatthew Ahrens ZPOOL_HIST_DSID)); 48264445fffbSMatthew Ahrens } 48274445fffbSMatthew Ahrens (void) printf(" %s", fnvlist_lookup_string(rec, 48284445fffbSMatthew Ahrens ZPOOL_HIST_INT_STR)); 48294445fffbSMatthew Ahrens } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 48304445fffbSMatthew Ahrens if (!cb->internal) 48314445fffbSMatthew Ahrens continue; 48324445fffbSMatthew Ahrens (void) printf("%s ioctl %s\n", tbuf, 48334445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 48344445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 48354445fffbSMatthew Ahrens (void) printf(" input:\n"); 48364445fffbSMatthew Ahrens dump_nvlist(fnvlist_lookup_nvlist(rec, 48374445fffbSMatthew Ahrens ZPOOL_HIST_INPUT_NVL), 8); 48384445fffbSMatthew Ahrens } 48394445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 48404445fffbSMatthew Ahrens (void) printf(" output:\n"); 48414445fffbSMatthew Ahrens dump_nvlist(fnvlist_lookup_nvlist(rec, 48424445fffbSMatthew Ahrens ZPOOL_HIST_OUTPUT_NVL), 8); 48434445fffbSMatthew Ahrens } 48444445fffbSMatthew Ahrens } else { 48454445fffbSMatthew Ahrens if (!cb->internal) 48464445fffbSMatthew Ahrens continue; 48474445fffbSMatthew Ahrens (void) printf("%s unrecognized record:\n", tbuf); 48484445fffbSMatthew Ahrens dump_nvlist(rec, 4); 48494445fffbSMatthew Ahrens } 4850ecd6cf80Smarks 4851ecd6cf80Smarks if (!cb->longfmt) { 4852ecd6cf80Smarks (void) printf("\n"); 4853ecd6cf80Smarks continue; 485406eeb2adSek110237 } 4855ecd6cf80Smarks (void) printf(" ["); 48564445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 48574445fffbSMatthew Ahrens uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 48584445fffbSMatthew Ahrens struct passwd *pwd = getpwuid(who); 48594445fffbSMatthew Ahrens (void) printf("user %d ", (int)who); 48604445fffbSMatthew Ahrens if (pwd != NULL) 48614445fffbSMatthew Ahrens (void) printf("(%s) ", pwd->pw_name); 4862ecd6cf80Smarks } 48634445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 48644445fffbSMatthew Ahrens (void) printf("on %s", 48654445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 4866ecd6cf80Smarks } 48674445fffbSMatthew Ahrens if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 48684445fffbSMatthew Ahrens (void) printf(":%s", 48694445fffbSMatthew Ahrens fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 4870ecd6cf80Smarks } 4871ecd6cf80Smarks (void) printf("]"); 4872ecd6cf80Smarks (void) printf("\n"); 487306eeb2adSek110237 } 487406eeb2adSek110237 (void) printf("\n"); 487506eeb2adSek110237 nvlist_free(nvhis); 487606eeb2adSek110237 487706eeb2adSek110237 return (ret); 487806eeb2adSek110237 } 487906eeb2adSek110237 488006eeb2adSek110237 /* 488106eeb2adSek110237 * zpool history <pool> 488206eeb2adSek110237 * 488306eeb2adSek110237 * Displays the history of commands that modified pools. 488406eeb2adSek110237 */ 488506eeb2adSek110237 int 488606eeb2adSek110237 zpool_do_history(int argc, char **argv) 488706eeb2adSek110237 { 4888ecd6cf80Smarks hist_cbdata_t cbdata = { 0 }; 488906eeb2adSek110237 int ret; 4890ecd6cf80Smarks int c; 489106eeb2adSek110237 4892ecd6cf80Smarks cbdata.first = B_TRUE; 4893ecd6cf80Smarks /* check options */ 4894ecd6cf80Smarks while ((c = getopt(argc, argv, "li")) != -1) { 4895ecd6cf80Smarks switch (c) { 4896ecd6cf80Smarks case 'l': 48974445fffbSMatthew Ahrens cbdata.longfmt = B_TRUE; 4898ecd6cf80Smarks break; 4899ecd6cf80Smarks case 'i': 49004445fffbSMatthew Ahrens cbdata.internal = B_TRUE; 4901ecd6cf80Smarks break; 4902ecd6cf80Smarks case '?': 4903ecd6cf80Smarks (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4904ecd6cf80Smarks optopt); 4905ecd6cf80Smarks usage(B_FALSE); 4906ecd6cf80Smarks } 4907ecd6cf80Smarks } 490806eeb2adSek110237 argc -= optind; 490906eeb2adSek110237 argv += optind; 491006eeb2adSek110237 4911b1b8ab34Slling ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one, 4912ecd6cf80Smarks &cbdata); 491306eeb2adSek110237 4914ecd6cf80Smarks if (argc == 0 && cbdata.first == B_TRUE) { 491506eeb2adSek110237 (void) printf(gettext("no pools available\n")); 491606eeb2adSek110237 return (0); 4917eaca9bbdSeschrock } 4918eaca9bbdSeschrock 4919eaca9bbdSeschrock return (ret); 4920eaca9bbdSeschrock } 4921eaca9bbdSeschrock 4922b1b8ab34Slling static int 4923b1b8ab34Slling get_callback(zpool_handle_t *zhp, void *data) 4924b1b8ab34Slling { 4925990b4856Slling zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 4926b1b8ab34Slling char value[MAXNAMELEN]; 4927990b4856Slling zprop_source_t srctype; 4928990b4856Slling zprop_list_t *pl; 4929b1b8ab34Slling 4930b1b8ab34Slling for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 4931b1b8ab34Slling 4932b1b8ab34Slling /* 4933990b4856Slling * Skip the special fake placeholder. This will also skip 4934990b4856Slling * over the name property when 'all' is specified. 4935b1b8ab34Slling */ 4936990b4856Slling if (pl->pl_prop == ZPOOL_PROP_NAME && 4937b1b8ab34Slling pl == cbp->cb_proplist) 4938b1b8ab34Slling continue; 4939b1b8ab34Slling 4940ad135b5dSChristopher Siden if (pl->pl_prop == ZPROP_INVAL && 4941ad135b5dSChristopher Siden (zpool_prop_feature(pl->pl_user_prop) || 4942ad135b5dSChristopher Siden zpool_prop_unsupported(pl->pl_user_prop))) { 4943ad135b5dSChristopher Siden srctype = ZPROP_SRC_LOCAL; 4944ad135b5dSChristopher Siden 4945ad135b5dSChristopher Siden if (zpool_prop_get_feature(zhp, pl->pl_user_prop, 4946ad135b5dSChristopher Siden value, sizeof (value)) == 0) { 4947ad135b5dSChristopher Siden zprop_print_one_property(zpool_get_name(zhp), 4948ad135b5dSChristopher Siden cbp, pl->pl_user_prop, value, srctype, 4949ad135b5dSChristopher Siden NULL, NULL); 4950ad135b5dSChristopher Siden } 4951ad135b5dSChristopher Siden } else { 4952ad135b5dSChristopher Siden if (zpool_get_prop(zhp, pl->pl_prop, value, 4953ad135b5dSChristopher Siden sizeof (value), &srctype) != 0) 4954b1b8ab34Slling continue; 4955b1b8ab34Slling 4956990b4856Slling zprop_print_one_property(zpool_get_name(zhp), cbp, 4957ad135b5dSChristopher Siden zpool_prop_to_name(pl->pl_prop), value, srctype, 4958ad135b5dSChristopher Siden NULL, NULL); 4959ad135b5dSChristopher Siden } 4960b1b8ab34Slling } 4961b1b8ab34Slling return (0); 4962b1b8ab34Slling } 4963b1b8ab34Slling 4964b1b8ab34Slling int 4965b1b8ab34Slling zpool_do_get(int argc, char **argv) 4966b1b8ab34Slling { 4967990b4856Slling zprop_get_cbdata_t cb = { 0 }; 4968990b4856Slling zprop_list_t fake_name = { 0 }; 4969b1b8ab34Slling int ret; 4970b1b8ab34Slling 4971ad135b5dSChristopher Siden if (argc < 2) { 4972ad135b5dSChristopher Siden (void) fprintf(stderr, gettext("missing property " 4973ad135b5dSChristopher Siden "argument\n")); 4974b1b8ab34Slling usage(B_FALSE); 4975ad135b5dSChristopher Siden } 4976b1b8ab34Slling 4977b1b8ab34Slling cb.cb_first = B_TRUE; 4978990b4856Slling cb.cb_sources = ZPROP_SRC_ALL; 4979b1b8ab34Slling cb.cb_columns[0] = GET_COL_NAME; 4980b1b8ab34Slling cb.cb_columns[1] = GET_COL_PROPERTY; 4981b1b8ab34Slling cb.cb_columns[2] = GET_COL_VALUE; 4982b1b8ab34Slling cb.cb_columns[3] = GET_COL_SOURCE; 4983990b4856Slling cb.cb_type = ZFS_TYPE_POOL; 4984b1b8ab34Slling 4985990b4856Slling if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist, 4986990b4856Slling ZFS_TYPE_POOL) != 0) 4987b1b8ab34Slling usage(B_FALSE); 4988b1b8ab34Slling 4989b1b8ab34Slling if (cb.cb_proplist != NULL) { 4990990b4856Slling fake_name.pl_prop = ZPOOL_PROP_NAME; 4991b1b8ab34Slling fake_name.pl_width = strlen(gettext("NAME")); 4992b1b8ab34Slling fake_name.pl_next = cb.cb_proplist; 4993b1b8ab34Slling cb.cb_proplist = &fake_name; 4994b1b8ab34Slling } 4995b1b8ab34Slling 4996b1b8ab34Slling ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist, 4997b1b8ab34Slling get_callback, &cb); 4998b1b8ab34Slling 4999b1b8ab34Slling if (cb.cb_proplist == &fake_name) 5000990b4856Slling zprop_free_list(fake_name.pl_next); 5001b1b8ab34Slling else 5002990b4856Slling zprop_free_list(cb.cb_proplist); 5003b1b8ab34Slling 5004b1b8ab34Slling return (ret); 5005b1b8ab34Slling } 5006b1b8ab34Slling 5007b1b8ab34Slling typedef struct set_cbdata { 5008b1b8ab34Slling char *cb_propname; 5009b1b8ab34Slling char *cb_value; 5010b1b8ab34Slling boolean_t cb_any_successful; 5011b1b8ab34Slling } set_cbdata_t; 5012b1b8ab34Slling 5013b1b8ab34Slling int 5014b1b8ab34Slling set_callback(zpool_handle_t *zhp, void *data) 5015b1b8ab34Slling { 5016b1b8ab34Slling int error; 5017b1b8ab34Slling set_cbdata_t *cb = (set_cbdata_t *)data; 5018b1b8ab34Slling 5019b1b8ab34Slling error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 5020b1b8ab34Slling 5021b1b8ab34Slling if (!error) 5022b1b8ab34Slling cb->cb_any_successful = B_TRUE; 5023b1b8ab34Slling 5024b1b8ab34Slling return (error); 5025b1b8ab34Slling } 5026b1b8ab34Slling 5027b1b8ab34Slling int 5028b1b8ab34Slling zpool_do_set(int argc, char **argv) 5029b1b8ab34Slling { 5030b1b8ab34Slling set_cbdata_t cb = { 0 }; 5031b1b8ab34Slling int error; 5032b1b8ab34Slling 5033b1b8ab34Slling if (argc > 1 && argv[1][0] == '-') { 5034b1b8ab34Slling (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5035b1b8ab34Slling argv[1][1]); 5036b1b8ab34Slling usage(B_FALSE); 5037b1b8ab34Slling } 5038b1b8ab34Slling 5039b1b8ab34Slling if (argc < 2) { 5040b1b8ab34Slling (void) fprintf(stderr, gettext("missing property=value " 5041b1b8ab34Slling "argument\n")); 5042b1b8ab34Slling usage(B_FALSE); 5043b1b8ab34Slling } 5044b1b8ab34Slling 5045b1b8ab34Slling if (argc < 3) { 5046b1b8ab34Slling (void) fprintf(stderr, gettext("missing pool name\n")); 5047b1b8ab34Slling usage(B_FALSE); 5048b1b8ab34Slling } 5049b1b8ab34Slling 5050b1b8ab34Slling if (argc > 3) { 5051b1b8ab34Slling (void) fprintf(stderr, gettext("too many pool names\n")); 5052b1b8ab34Slling usage(B_FALSE); 5053b1b8ab34Slling } 5054b1b8ab34Slling 5055b1b8ab34Slling cb.cb_propname = argv[1]; 5056b1b8ab34Slling cb.cb_value = strchr(cb.cb_propname, '='); 5057b1b8ab34Slling if (cb.cb_value == NULL) { 5058b1b8ab34Slling (void) fprintf(stderr, gettext("missing value in " 5059b1b8ab34Slling "property=value argument\n")); 5060b1b8ab34Slling usage(B_FALSE); 5061b1b8ab34Slling } 5062b1b8ab34Slling 5063b1b8ab34Slling *(cb.cb_value) = '\0'; 5064b1b8ab34Slling cb.cb_value++; 5065b1b8ab34Slling 5066b1b8ab34Slling error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, 5067b1b8ab34Slling set_callback, &cb); 5068b1b8ab34Slling 5069b1b8ab34Slling return (error); 5070b1b8ab34Slling } 5071b1b8ab34Slling 5072b1b8ab34Slling static int 5073b1b8ab34Slling find_command_idx(char *command, int *idx) 5074b1b8ab34Slling { 5075b1b8ab34Slling int i; 5076b1b8ab34Slling 5077b1b8ab34Slling for (i = 0; i < NCOMMAND; i++) { 5078b1b8ab34Slling if (command_table[i].name == NULL) 5079b1b8ab34Slling continue; 5080b1b8ab34Slling 5081b1b8ab34Slling if (strcmp(command, command_table[i].name) == 0) { 5082b1b8ab34Slling *idx = i; 5083b1b8ab34Slling return (0); 5084b1b8ab34Slling } 5085b1b8ab34Slling } 5086b1b8ab34Slling return (1); 5087b1b8ab34Slling } 5088b1b8ab34Slling 5089fa9e4066Sahrens int 5090fa9e4066Sahrens main(int argc, char **argv) 5091fa9e4066Sahrens { 5092fa9e4066Sahrens int ret; 5093fa9e4066Sahrens int i; 5094fa9e4066Sahrens char *cmdname; 5095fa9e4066Sahrens 5096fa9e4066Sahrens (void) setlocale(LC_ALL, ""); 5097fa9e4066Sahrens (void) textdomain(TEXT_DOMAIN); 5098fa9e4066Sahrens 509999653d4eSeschrock if ((g_zfs = libzfs_init()) == NULL) { 510099653d4eSeschrock (void) fprintf(stderr, gettext("internal error: failed to " 5101203a47d8Snd150628 "initialize ZFS library\n")); 510299653d4eSeschrock return (1); 510399653d4eSeschrock } 510499653d4eSeschrock 510599653d4eSeschrock libzfs_print_on_error(g_zfs, B_TRUE); 510699653d4eSeschrock 5107fa9e4066Sahrens opterr = 0; 5108fa9e4066Sahrens 5109fa9e4066Sahrens /* 5110fa9e4066Sahrens * Make sure the user has specified some command. 5111fa9e4066Sahrens */ 5112fa9e4066Sahrens if (argc < 2) { 5113fa9e4066Sahrens (void) fprintf(stderr, gettext("missing command\n")); 511499653d4eSeschrock usage(B_FALSE); 5115fa9e4066Sahrens } 5116fa9e4066Sahrens 5117fa9e4066Sahrens cmdname = argv[1]; 5118fa9e4066Sahrens 5119fa9e4066Sahrens /* 5120fa9e4066Sahrens * Special case '-?' 5121fa9e4066Sahrens */ 5122fa9e4066Sahrens if (strcmp(cmdname, "-?") == 0) 512399653d4eSeschrock usage(B_TRUE); 5124fa9e4066Sahrens 51254445fffbSMatthew Ahrens zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 51262a6b87f0Sek110237 5127fa9e4066Sahrens /* 5128fa9e4066Sahrens * Run the appropriate command. 5129fa9e4066Sahrens */ 5130b1b8ab34Slling if (find_command_idx(cmdname, &i) == 0) { 5131fa9e4066Sahrens current_command = &command_table[i]; 5132fa9e4066Sahrens ret = command_table[i].func(argc - 1, argv + 1); 513391ebeef5Sahrens } else if (strchr(cmdname, '=')) { 513491ebeef5Sahrens verify(find_command_idx("set", &i) == 0); 513591ebeef5Sahrens current_command = &command_table[i]; 513691ebeef5Sahrens ret = command_table[i].func(argc, argv); 513791ebeef5Sahrens } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 5138fa9e4066Sahrens /* 513991ebeef5Sahrens * 'freeze' is a vile debugging abomination, so we treat 514091ebeef5Sahrens * it as such. 5141fa9e4066Sahrens */ 5142ea8dc4b6Seschrock char buf[16384]; 5143ea8dc4b6Seschrock int fd = open(ZFS_DEV, O_RDWR); 5144fa9e4066Sahrens (void) strcpy((void *)buf, argv[2]); 5145fa9e4066Sahrens return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf)); 514691ebeef5Sahrens } else { 5147fa9e4066Sahrens (void) fprintf(stderr, gettext("unrecognized " 5148fa9e4066Sahrens "command '%s'\n"), cmdname); 514999653d4eSeschrock usage(B_FALSE); 5150fa9e4066Sahrens } 5151fa9e4066Sahrens 51524445fffbSMatthew Ahrens if (ret == 0 && log_history) 51534445fffbSMatthew Ahrens (void) zpool_log_history(g_zfs, history_str); 51544445fffbSMatthew Ahrens 515599653d4eSeschrock libzfs_fini(g_zfs); 515699653d4eSeschrock 5157fa9e4066Sahrens /* 5158fa9e4066Sahrens * The 'ZFS_ABORT' environment variable causes us to dump core on exit 5159fa9e4066Sahrens * for the purposes of running ::findleaks. 5160fa9e4066Sahrens */ 5161fa9e4066Sahrens if (getenv("ZFS_ABORT") != NULL) { 5162fa9e4066Sahrens (void) printf("dumping core by request\n"); 5163fa9e4066Sahrens abort(); 5164fa9e4066Sahrens } 5165fa9e4066Sahrens 5166fa9e4066Sahrens return (ret); 5167fa9e4066Sahrens } 5168