1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 213126Sahl 22789Sahrens /* 23*12296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <assert.h> 272676Seschrock #include <ctype.h> 28789Sahrens #include <errno.h> 29789Sahrens #include <libgen.h> 30789Sahrens #include <libintl.h> 31789Sahrens #include <libuutil.h> 324543Smarks #include <libnvpair.h> 33789Sahrens #include <locale.h> 34789Sahrens #include <stddef.h> 35789Sahrens #include <stdio.h> 36789Sahrens #include <stdlib.h> 37789Sahrens #include <strings.h> 38789Sahrens #include <unistd.h> 39789Sahrens #include <fcntl.h> 40789Sahrens #include <zone.h> 419396SMatthew.Ahrens@Sun.COM #include <grp.h> 429396SMatthew.Ahrens@Sun.COM #include <pwd.h> 43789Sahrens #include <sys/mkdev.h> 44789Sahrens #include <sys/mntent.h> 45789Sahrens #include <sys/mnttab.h> 46789Sahrens #include <sys/mount.h> 47789Sahrens #include <sys/stat.h> 489396SMatthew.Ahrens@Sun.COM #include <sys/fs/zfs.h> 49789Sahrens 50789Sahrens #include <libzfs.h> 514543Smarks #include <libuutil.h> 52789Sahrens 53789Sahrens #include "zfs_iter.h" 542082Seschrock #include "zfs_util.h" 5511935SMark.Shellenbaum@Sun.COM #include "zfs_comutil.h" 562082Seschrock 572082Seschrock libzfs_handle_t *g_zfs; 58789Sahrens 59789Sahrens static FILE *mnttab_file; 604988Sek110237 static char history_str[HIS_MAX_RECORD_LEN]; 619396SMatthew.Ahrens@Sun.COM const char *pypath = "/usr/lib/zfs/pyzfs.py"; 62789Sahrens 63789Sahrens static int zfs_do_clone(int argc, char **argv); 64789Sahrens static int zfs_do_create(int argc, char **argv); 65789Sahrens static int zfs_do_destroy(int argc, char **argv); 66789Sahrens static int zfs_do_get(int argc, char **argv); 67789Sahrens static int zfs_do_inherit(int argc, char **argv); 68789Sahrens static int zfs_do_list(int argc, char **argv); 69789Sahrens static int zfs_do_mount(int argc, char **argv); 70789Sahrens static int zfs_do_rename(int argc, char **argv); 71789Sahrens static int zfs_do_rollback(int argc, char **argv); 72789Sahrens static int zfs_do_set(int argc, char **argv); 734577Sahrens static int zfs_do_upgrade(int argc, char **argv); 74789Sahrens static int zfs_do_snapshot(int argc, char **argv); 75789Sahrens static int zfs_do_unmount(int argc, char **argv); 76789Sahrens static int zfs_do_share(int argc, char **argv); 77789Sahrens static int zfs_do_unshare(int argc, char **argv); 781749Sahrens static int zfs_do_send(int argc, char **argv); 791749Sahrens static int zfs_do_receive(int argc, char **argv); 802082Seschrock static int zfs_do_promote(int argc, char **argv); 819396SMatthew.Ahrens@Sun.COM static int zfs_do_userspace(int argc, char **argv); 829396SMatthew.Ahrens@Sun.COM static int zfs_do_python(int argc, char **argv); 8310242Schris.kirby@sun.com static int zfs_do_hold(int argc, char **argv); 8410242Schris.kirby@sun.com static int zfs_do_release(int argc, char **argv); 85789Sahrens 86789Sahrens /* 876865Srm160521 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds. 88789Sahrens */ 896865Srm160521 906865Srm160521 #ifdef DEBUG 91789Sahrens const char * 923126Sahl _umem_debug_init(void) 93789Sahrens { 94789Sahrens return ("default,verbose"); /* $UMEM_DEBUG setting */ 95789Sahrens } 96789Sahrens 97789Sahrens const char * 98789Sahrens _umem_logging_init(void) 99789Sahrens { 100789Sahrens return ("fail,contents"); /* $UMEM_LOGGING setting */ 101789Sahrens } 1026865Srm160521 #endif 103789Sahrens 1041387Seschrock typedef enum { 1051387Seschrock HELP_CLONE, 1061387Seschrock HELP_CREATE, 1071387Seschrock HELP_DESTROY, 1081387Seschrock HELP_GET, 1091387Seschrock HELP_INHERIT, 1104577Sahrens HELP_UPGRADE, 1111387Seschrock HELP_LIST, 1121387Seschrock HELP_MOUNT, 1132082Seschrock HELP_PROMOTE, 1141749Sahrens HELP_RECEIVE, 1151387Seschrock HELP_RENAME, 1161387Seschrock HELP_ROLLBACK, 1171749Sahrens HELP_SEND, 1181387Seschrock HELP_SET, 1191387Seschrock HELP_SHARE, 1201387Seschrock HELP_SNAPSHOT, 1211387Seschrock HELP_UNMOUNT, 1224543Smarks HELP_UNSHARE, 1234543Smarks HELP_ALLOW, 1249396SMatthew.Ahrens@Sun.COM HELP_UNALLOW, 1259396SMatthew.Ahrens@Sun.COM HELP_USERSPACE, 12610242Schris.kirby@sun.com HELP_GROUPSPACE, 12710242Schris.kirby@sun.com HELP_HOLD, 12810242Schris.kirby@sun.com HELP_HOLDS, 12910242Schris.kirby@sun.com HELP_RELEASE 1301387Seschrock } zfs_help_t; 1311387Seschrock 132789Sahrens typedef struct zfs_command { 133789Sahrens const char *name; 134789Sahrens int (*func)(int argc, char **argv); 1351387Seschrock zfs_help_t usage; 136789Sahrens } zfs_command_t; 137789Sahrens 138789Sahrens /* 139789Sahrens * Master command table. Each ZFS command has a name, associated function, and 1401544Seschrock * usage message. The usage messages need to be internationalized, so we have 1411544Seschrock * to have a function to return the usage message based on a command index. 1421387Seschrock * 1431387Seschrock * These commands are organized according to how they are displayed in the usage 1441387Seschrock * message. An empty command (one with a NULL name) indicates an empty line in 1451387Seschrock * the generic usage message. 146789Sahrens */ 147789Sahrens static zfs_command_t command_table[] = { 1481387Seschrock { "create", zfs_do_create, HELP_CREATE }, 1491387Seschrock { "destroy", zfs_do_destroy, HELP_DESTROY }, 150789Sahrens { NULL }, 1511387Seschrock { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT }, 1521387Seschrock { "rollback", zfs_do_rollback, HELP_ROLLBACK }, 1531387Seschrock { "clone", zfs_do_clone, HELP_CLONE }, 1542082Seschrock { "promote", zfs_do_promote, HELP_PROMOTE }, 1551387Seschrock { "rename", zfs_do_rename, HELP_RENAME }, 156789Sahrens { NULL }, 1571387Seschrock { "list", zfs_do_list, HELP_LIST }, 158789Sahrens { NULL }, 1591387Seschrock { "set", zfs_do_set, HELP_SET }, 16011022STom.Erickson@Sun.COM { "get", zfs_do_get, HELP_GET }, 1611387Seschrock { "inherit", zfs_do_inherit, HELP_INHERIT }, 1624577Sahrens { "upgrade", zfs_do_upgrade, HELP_UPGRADE }, 1639396SMatthew.Ahrens@Sun.COM { "userspace", zfs_do_userspace, HELP_USERSPACE }, 1649396SMatthew.Ahrens@Sun.COM { "groupspace", zfs_do_userspace, HELP_GROUPSPACE }, 165789Sahrens { NULL }, 1661387Seschrock { "mount", zfs_do_mount, HELP_MOUNT }, 1671387Seschrock { "unmount", zfs_do_unmount, HELP_UNMOUNT }, 1681387Seschrock { "share", zfs_do_share, HELP_SHARE }, 1691387Seschrock { "unshare", zfs_do_unshare, HELP_UNSHARE }, 170789Sahrens { NULL }, 1711749Sahrens { "send", zfs_do_send, HELP_SEND }, 1721749Sahrens { "receive", zfs_do_receive, HELP_RECEIVE }, 1734543Smarks { NULL }, 1749396SMatthew.Ahrens@Sun.COM { "allow", zfs_do_python, HELP_ALLOW }, 1754543Smarks { NULL }, 1769396SMatthew.Ahrens@Sun.COM { "unallow", zfs_do_python, HELP_UNALLOW }, 17710242Schris.kirby@sun.com { NULL }, 17810242Schris.kirby@sun.com { "hold", zfs_do_hold, HELP_HOLD }, 17910242Schris.kirby@sun.com { "holds", zfs_do_python, HELP_HOLDS }, 18010242Schris.kirby@sun.com { "release", zfs_do_release, HELP_RELEASE }, 181789Sahrens }; 182789Sahrens 183789Sahrens #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 184789Sahrens 185789Sahrens zfs_command_t *current_command; 186789Sahrens 1871387Seschrock static const char * 1881387Seschrock get_usage(zfs_help_t idx) 1891387Seschrock { 1901387Seschrock switch (idx) { 1911387Seschrock case HELP_CLONE: 1927265Sahrens return (gettext("\tclone [-p] [-o property=value] ... " 1937265Sahrens "<snapshot> <filesystem|volume>\n")); 1941387Seschrock case HELP_CREATE: 1954849Sahrens return (gettext("\tcreate [-p] [-o property=value] ... " 1962676Seschrock "<filesystem>\n" 1974849Sahrens "\tcreate [-ps] [-b blocksize] [-o property=value] ... " 1984849Sahrens "-V <size> <volume>\n")); 1991387Seschrock case HELP_DESTROY: 20010385Schris.kirby@sun.com return (gettext("\tdestroy [-rRf] <filesystem|volume>\n" 20110385Schris.kirby@sun.com "\tdestroy [-rRd] <snapshot>\n")); 2021387Seschrock case HELP_GET: 2039365SChris.Gerhard@sun.com return (gettext("\tget [-rHp] [-d max] " 20411022STom.Erickson@Sun.COM "[-o \"all\" | field[,...]] [-s source[,...]]\n" 2054849Sahrens "\t <\"all\" | property[,...]> " 2062676Seschrock "[filesystem|volume|snapshot] ...\n")); 2071387Seschrock case HELP_INHERIT: 20811022STom.Erickson@Sun.COM return (gettext("\tinherit [-rS] <property> " 2097265Sahrens "<filesystem|volume|snapshot> ...\n")); 2104577Sahrens case HELP_UPGRADE: 2114577Sahrens return (gettext("\tupgrade [-v]\n" 2124577Sahrens "\tupgrade [-r] [-V version] <-a | filesystem ...>\n")); 2131387Seschrock case HELP_LIST: 2149365SChris.Gerhard@sun.com return (gettext("\tlist [-rH][-d max] " 2159365SChris.Gerhard@sun.com "[-o property[,...]] [-t type[,...]] [-s property] ...\n" 2164849Sahrens "\t [-S property] ... " 2174849Sahrens "[filesystem|volume|snapshot] ...\n")); 2181387Seschrock case HELP_MOUNT: 2191387Seschrock return (gettext("\tmount\n" 2204849Sahrens "\tmount [-vO] [-o opts] <-a | filesystem>\n")); 2212082Seschrock case HELP_PROMOTE: 2224849Sahrens return (gettext("\tpromote <clone-filesystem>\n")); 2231749Sahrens case HELP_RECEIVE: 2242665Snd150628 return (gettext("\treceive [-vnF] <filesystem|volume|" 2252665Snd150628 "snapshot>\n" 2262665Snd150628 "\treceive [-vnF] -d <filesystem>\n")); 2271387Seschrock case HELP_RENAME: 2281387Seschrock return (gettext("\trename <filesystem|volume|snapshot> " 2294007Smmusante "<filesystem|volume|snapshot>\n" 2304490Svb160487 "\trename -p <filesystem|volume> <filesystem|volume>\n" 2314007Smmusante "\trename -r <snapshot> <snapshot>")); 2321387Seschrock case HELP_ROLLBACK: 2335568Sahrens return (gettext("\trollback [-rRf] <snapshot>\n")); 2341749Sahrens case HELP_SEND: 23511022STom.Erickson@Sun.COM return (gettext("\tsend [-RDp] [-[iI] snapshot] <snapshot>\n")); 2361387Seschrock case HELP_SET: 2371387Seschrock return (gettext("\tset <property=value> " 2387265Sahrens "<filesystem|volume|snapshot> ...\n")); 2391387Seschrock case HELP_SHARE: 2404849Sahrens return (gettext("\tshare <-a | filesystem>\n")); 2411387Seschrock case HELP_SNAPSHOT: 2427265Sahrens return (gettext("\tsnapshot [-r] [-o property=value] ... " 2434849Sahrens "<filesystem@snapname|volume@snapname>\n")); 2441387Seschrock case HELP_UNMOUNT: 2454849Sahrens return (gettext("\tunmount [-f] " 2464849Sahrens "<-a | filesystem|mountpoint>\n")); 2471387Seschrock case HELP_UNSHARE: 24810228SStephanie.Scheffler@Sun.COM return (gettext("\tunshare " 2494849Sahrens "<-a | filesystem|mountpoint>\n")); 2504543Smarks case HELP_ALLOW: 2519564SStephanie.Scheffler@Sun.COM return (gettext("\tallow <filesystem|volume>\n" 2529564SStephanie.Scheffler@Sun.COM "\tallow [-ldug] " 2534849Sahrens "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n" 2544849Sahrens "\t <filesystem|volume>\n" 2554849Sahrens "\tallow [-ld] -e <perm|@setname>[,...] " 2564849Sahrens "<filesystem|volume>\n" 2574849Sahrens "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n" 2584849Sahrens "\tallow -s @setname <perm|@setname>[,...] " 2594849Sahrens "<filesystem|volume>\n")); 2604543Smarks case HELP_UNALLOW: 2614849Sahrens return (gettext("\tunallow [-rldug] " 2624849Sahrens "<\"everyone\"|user|group>[,...]\n" 2634849Sahrens "\t [<perm|@setname>[,...]] <filesystem|volume>\n" 2644849Sahrens "\tunallow [-rld] -e [<perm|@setname>[,...]] " 2654849Sahrens "<filesystem|volume>\n" 2664849Sahrens "\tunallow [-r] -c [<perm|@setname>[,...]] " 2674849Sahrens "<filesystem|volume>\n" 2684849Sahrens "\tunallow [-r] -s @setname [<perm|@setname>[,...]] " 2694849Sahrens "<filesystem|volume>\n")); 2709396SMatthew.Ahrens@Sun.COM case HELP_USERSPACE: 2719396SMatthew.Ahrens@Sun.COM return (gettext("\tuserspace [-hniHp] [-o field[,...]] " 2729396SMatthew.Ahrens@Sun.COM "[-sS field] ... [-t type[,...]]\n" 2739396SMatthew.Ahrens@Sun.COM "\t <filesystem|snapshot>\n")); 2749396SMatthew.Ahrens@Sun.COM case HELP_GROUPSPACE: 2759396SMatthew.Ahrens@Sun.COM return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] " 2769396SMatthew.Ahrens@Sun.COM "[-sS field] ... [-t type[,...]]\n" 2779396SMatthew.Ahrens@Sun.COM "\t <filesystem|snapshot>\n")); 27810242Schris.kirby@sun.com case HELP_HOLD: 27910242Schris.kirby@sun.com return (gettext("\thold [-r] <tag> <snapshot> ...\n")); 28010242Schris.kirby@sun.com case HELP_HOLDS: 28110242Schris.kirby@sun.com return (gettext("\tholds [-r] <snapshot> ...\n")); 28210242Schris.kirby@sun.com case HELP_RELEASE: 28310242Schris.kirby@sun.com return (gettext("\trelease [-r] <tag> <snapshot> ...\n")); 2841387Seschrock } 2851387Seschrock 2861387Seschrock abort(); 2871387Seschrock /* NOTREACHED */ 2881387Seschrock } 2891387Seschrock 290789Sahrens /* 291789Sahrens * Utility function to guarantee malloc() success. 292789Sahrens */ 293789Sahrens void * 294789Sahrens safe_malloc(size_t size) 295789Sahrens { 296789Sahrens void *data; 297789Sahrens 298789Sahrens if ((data = calloc(1, size)) == NULL) { 299789Sahrens (void) fprintf(stderr, "internal error: out of memory\n"); 300789Sahrens exit(1); 301789Sahrens } 302789Sahrens 303789Sahrens return (data); 304789Sahrens } 305789Sahrens 306789Sahrens /* 3075094Slling * Callback routine that will print out information for each of 3083654Sgw25295 * the properties. 3093654Sgw25295 */ 3105094Slling static int 3115094Slling usage_prop_cb(int prop, void *cb) 3123654Sgw25295 { 3133654Sgw25295 FILE *fp = cb; 3143654Sgw25295 3156643Seschrock (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop)); 3166643Seschrock 3176643Seschrock if (zfs_prop_readonly(prop)) 3186643Seschrock (void) fprintf(fp, " NO "); 3193654Sgw25295 else 3206643Seschrock (void) fprintf(fp, "YES "); 3213654Sgw25295 3223654Sgw25295 if (zfs_prop_inheritable(prop)) 3233654Sgw25295 (void) fprintf(fp, " YES "); 3243654Sgw25295 else 3253654Sgw25295 (void) fprintf(fp, " NO "); 3263654Sgw25295 3273654Sgw25295 if (zfs_prop_values(prop) == NULL) 3283654Sgw25295 (void) fprintf(fp, "-\n"); 3293654Sgw25295 else 3303654Sgw25295 (void) fprintf(fp, "%s\n", zfs_prop_values(prop)); 3313654Sgw25295 3325094Slling return (ZPROP_CONT); 3333654Sgw25295 } 3343654Sgw25295 3353654Sgw25295 /* 336789Sahrens * Display usage message. If we're inside a command, display only the usage for 337789Sahrens * that command. Otherwise, iterate over the entire command table and display 338789Sahrens * a complete usage message. 339789Sahrens */ 340789Sahrens static void 3412082Seschrock usage(boolean_t requested) 342789Sahrens { 343789Sahrens int i; 3442082Seschrock boolean_t show_properties = B_FALSE; 345789Sahrens FILE *fp = requested ? stdout : stderr; 346789Sahrens 347789Sahrens if (current_command == NULL) { 348789Sahrens 349789Sahrens (void) fprintf(fp, gettext("usage: zfs command args ...\n")); 350789Sahrens (void) fprintf(fp, 351789Sahrens gettext("where 'command' is one of the following:\n\n")); 352789Sahrens 353789Sahrens for (i = 0; i < NCOMMAND; i++) { 354789Sahrens if (command_table[i].name == NULL) 355789Sahrens (void) fprintf(fp, "\n"); 356789Sahrens else 357789Sahrens (void) fprintf(fp, "%s", 3581387Seschrock get_usage(command_table[i].usage)); 359789Sahrens } 360789Sahrens 361789Sahrens (void) fprintf(fp, gettext("\nEach dataset is of the form: " 362789Sahrens "pool/[dataset/]*dataset[@name]\n")); 363789Sahrens } else { 364789Sahrens (void) fprintf(fp, gettext("usage:\n")); 3651387Seschrock (void) fprintf(fp, "%s", get_usage(current_command->usage)); 366789Sahrens } 367789Sahrens 3682190Sdarrenm if (current_command != NULL && 3692190Sdarrenm (strcmp(current_command->name, "set") == 0 || 370789Sahrens strcmp(current_command->name, "get") == 0 || 371789Sahrens strcmp(current_command->name, "inherit") == 0 || 3722190Sdarrenm strcmp(current_command->name, "list") == 0)) 3732082Seschrock show_properties = B_TRUE; 374789Sahrens 375789Sahrens if (show_properties) { 376789Sahrens (void) fprintf(fp, 377789Sahrens gettext("\nThe following properties are supported:\n")); 378789Sahrens 3795378Sck153898 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n", 380789Sahrens "PROPERTY", "EDIT", "INHERIT", "VALUES"); 381789Sahrens 3823654Sgw25295 /* Iterate over all properties */ 3835094Slling (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE, 3845094Slling ZFS_TYPE_DATASET); 3853654Sgw25295 3869396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, "\t%-15s ", "userused@..."); 3879396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, " NO NO <size>\n"); 3889396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, "\t%-15s ", "groupused@..."); 3899396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, " NO NO <size>\n"); 3909396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, "\t%-15s ", "userquota@..."); 3919396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, "YES NO <size> | none\n"); 3929396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, "\t%-15s ", "groupquota@..."); 3939396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, "YES NO <size> | none\n"); 3949396SMatthew.Ahrens@Sun.COM 395789Sahrens (void) fprintf(fp, gettext("\nSizes are specified in bytes " 396789Sahrens "with standard units such as K, M, G, etc.\n")); 3977390SMatthew.Ahrens@Sun.COM (void) fprintf(fp, gettext("\nUser-defined properties can " 3982676Seschrock "be specified by using a name containing a colon (:).\n")); 3999396SMatthew.Ahrens@Sun.COM (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ " 4009396SMatthew.Ahrens@Sun.COM "properties must be appended with\n" 4019396SMatthew.Ahrens@Sun.COM "a user or group specifier of one of these forms:\n" 4029396SMatthew.Ahrens@Sun.COM " POSIX name (eg: \"matt\")\n" 4039396SMatthew.Ahrens@Sun.COM " POSIX id (eg: \"126829\")\n" 4049396SMatthew.Ahrens@Sun.COM " SMB name@domain (eg: \"matt@sun\")\n" 4059396SMatthew.Ahrens@Sun.COM " SMB SID (eg: \"S-1-234-567-89\")\n")); 4062190Sdarrenm } else { 4072190Sdarrenm (void) fprintf(fp, 4088269SMark.Musante@Sun.COM gettext("\nFor the property list, run: %s\n"), 4098269SMark.Musante@Sun.COM "zfs set|get"); 4105993Smarks (void) fprintf(fp, 4118269SMark.Musante@Sun.COM gettext("\nFor the delegated permission list, run: %s\n"), 4128269SMark.Musante@Sun.COM "zfs allow|unallow"); 413789Sahrens } 414789Sahrens 4152676Seschrock /* 4162676Seschrock * See comments at end of main(). 4172676Seschrock */ 4182676Seschrock if (getenv("ZFS_ABORT") != NULL) { 4192676Seschrock (void) printf("dumping core by request\n"); 4202676Seschrock abort(); 4212676Seschrock } 4222676Seschrock 423789Sahrens exit(requested ? 0 : 2); 424789Sahrens } 425789Sahrens 4267265Sahrens static int 4277265Sahrens parseprop(nvlist_t *props) 4287265Sahrens { 4297265Sahrens char *propname = optarg; 4307265Sahrens char *propval, *strval; 4317265Sahrens 4327265Sahrens if ((propval = strchr(propname, '=')) == NULL) { 4337265Sahrens (void) fprintf(stderr, gettext("missing " 4347265Sahrens "'=' for -o option\n")); 4357265Sahrens return (-1); 4367265Sahrens } 4377265Sahrens *propval = '\0'; 4387265Sahrens propval++; 4397265Sahrens if (nvlist_lookup_string(props, propname, &strval) == 0) { 4407265Sahrens (void) fprintf(stderr, gettext("property '%s' " 4417265Sahrens "specified multiple times\n"), propname); 4427265Sahrens return (-1); 4437265Sahrens } 4447265Sahrens if (nvlist_add_string(props, propname, propval) != 0) { 4457265Sahrens (void) fprintf(stderr, gettext("internal " 4467265Sahrens "error: out of memory\n")); 4477265Sahrens return (-1); 4487265Sahrens } 4497265Sahrens return (0); 4507265Sahrens } 4517265Sahrens 4529365SChris.Gerhard@sun.com static int 4539365SChris.Gerhard@sun.com parse_depth(char *opt, int *flags) 4549365SChris.Gerhard@sun.com { 4559365SChris.Gerhard@sun.com char *tmp; 4569365SChris.Gerhard@sun.com int depth; 4579365SChris.Gerhard@sun.com 4589365SChris.Gerhard@sun.com depth = (int)strtol(opt, &tmp, 0); 4599365SChris.Gerhard@sun.com if (*tmp) { 4609365SChris.Gerhard@sun.com (void) fprintf(stderr, 4619365SChris.Gerhard@sun.com gettext("%s is not an integer\n"), optarg); 4629365SChris.Gerhard@sun.com usage(B_FALSE); 4639365SChris.Gerhard@sun.com } 4649365SChris.Gerhard@sun.com if (depth < 0) { 4659365SChris.Gerhard@sun.com (void) fprintf(stderr, 4669365SChris.Gerhard@sun.com gettext("Depth can not be negative.\n")); 4679365SChris.Gerhard@sun.com usage(B_FALSE); 4689365SChris.Gerhard@sun.com } 4699365SChris.Gerhard@sun.com *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE); 4709365SChris.Gerhard@sun.com return (depth); 4719365SChris.Gerhard@sun.com } 4729365SChris.Gerhard@sun.com 473789Sahrens /* 4747265Sahrens * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol> 475789Sahrens * 476789Sahrens * Given an existing dataset, create a writable copy whose initial contents 477789Sahrens * are the same as the source. The newly created dataset maintains a 478789Sahrens * dependency on the original; the original cannot be destroyed so long as 479789Sahrens * the clone exists. 4804490Svb160487 * 4814490Svb160487 * The '-p' flag creates all the non-existing ancestors of the target first. 482789Sahrens */ 483789Sahrens static int 484789Sahrens zfs_do_clone(int argc, char **argv) 485789Sahrens { 4867265Sahrens zfs_handle_t *zhp = NULL; 4874490Svb160487 boolean_t parents = B_FALSE; 4887265Sahrens nvlist_t *props; 489789Sahrens int ret; 4904490Svb160487 int c; 491789Sahrens 4927265Sahrens if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 4937265Sahrens (void) fprintf(stderr, gettext("internal error: " 4947265Sahrens "out of memory\n")); 4957265Sahrens return (1); 4967265Sahrens } 4977265Sahrens 498789Sahrens /* check options */ 4997265Sahrens while ((c = getopt(argc, argv, "o:p")) != -1) { 5004490Svb160487 switch (c) { 5017265Sahrens case 'o': 5027265Sahrens if (parseprop(props)) 5037265Sahrens return (1); 5047265Sahrens break; 5054490Svb160487 case 'p': 5064490Svb160487 parents = B_TRUE; 5074490Svb160487 break; 5084490Svb160487 case '?': 5094490Svb160487 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5104490Svb160487 optopt); 5117265Sahrens goto usage; 5124490Svb160487 } 513789Sahrens } 514789Sahrens 5154490Svb160487 argc -= optind; 5164490Svb160487 argv += optind; 5174490Svb160487 518789Sahrens /* check number of arguments */ 5194490Svb160487 if (argc < 1) { 520789Sahrens (void) fprintf(stderr, gettext("missing source dataset " 521789Sahrens "argument\n")); 5227265Sahrens goto usage; 523789Sahrens } 5244490Svb160487 if (argc < 2) { 525789Sahrens (void) fprintf(stderr, gettext("missing target dataset " 526789Sahrens "argument\n")); 5277265Sahrens goto usage; 528789Sahrens } 5294490Svb160487 if (argc > 2) { 530789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 5317265Sahrens goto usage; 532789Sahrens } 533789Sahrens 534789Sahrens /* open the source dataset */ 5354490Svb160487 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 536789Sahrens return (1); 537789Sahrens 5384490Svb160487 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM | 5394490Svb160487 ZFS_TYPE_VOLUME)) { 5404490Svb160487 /* 5414490Svb160487 * Now create the ancestors of the target dataset. If the 5424490Svb160487 * target already exists and '-p' option was used we should not 5434490Svb160487 * complain. 5444490Svb160487 */ 5454490Svb160487 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | 5464490Svb160487 ZFS_TYPE_VOLUME)) 5474490Svb160487 return (0); 5484490Svb160487 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) 5494490Svb160487 return (1); 5504490Svb160487 } 5514490Svb160487 552789Sahrens /* pass to libzfs */ 5537265Sahrens ret = zfs_clone(zhp, argv[1], props); 554789Sahrens 555789Sahrens /* create the mountpoint if necessary */ 556789Sahrens if (ret == 0) { 5575094Slling zfs_handle_t *clone; 5585094Slling 5595094Slling clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET); 560789Sahrens if (clone != NULL) { 561789Sahrens if ((ret = zfs_mount(clone, NULL, 0)) == 0) 562789Sahrens ret = zfs_share(clone); 563789Sahrens zfs_close(clone); 564789Sahrens } 565789Sahrens } 566789Sahrens 567789Sahrens zfs_close(zhp); 5687265Sahrens nvlist_free(props); 5697265Sahrens 5707265Sahrens return (!!ret); 5717265Sahrens 5727265Sahrens usage: 5737265Sahrens if (zhp) 5747265Sahrens zfs_close(zhp); 5757265Sahrens nvlist_free(props); 5767265Sahrens usage(B_FALSE); 5777265Sahrens return (-1); 578789Sahrens } 579789Sahrens 580789Sahrens /* 5814490Svb160487 * zfs create [-p] [-o prop=value] ... fs 5824490Svb160487 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size 583789Sahrens * 584789Sahrens * Create a new dataset. This command can be used to create filesystems 585789Sahrens * and volumes. Snapshot creation is handled by 'zfs snapshot'. 586789Sahrens * For volumes, the user must specify a size to be used. 587789Sahrens * 588789Sahrens * The '-s' flag applies only to volumes, and indicates that we should not try 589789Sahrens * to set the reservation for this volume. By default we set a reservation 5905481Sck153898 * equal to the size for any volume. For pools with SPA_VERSION >= 5915481Sck153898 * SPA_VERSION_REFRESERVATION, we set a refreservation instead. 5924490Svb160487 * 5934490Svb160487 * The '-p' flag creates all the non-existing ancestors of the target first. 594789Sahrens */ 595789Sahrens static int 596789Sahrens zfs_do_create(int argc, char **argv) 597789Sahrens { 598789Sahrens zfs_type_t type = ZFS_TYPE_FILESYSTEM; 5992676Seschrock zfs_handle_t *zhp = NULL; 6002676Seschrock uint64_t volsize; 601789Sahrens int c; 6022082Seschrock boolean_t noreserve = B_FALSE; 6035367Sahrens boolean_t bflag = B_FALSE; 6044490Svb160487 boolean_t parents = B_FALSE; 6052676Seschrock int ret = 1; 6067265Sahrens nvlist_t *props; 6072676Seschrock uint64_t intval; 6086168Shs24103 int canmount; 6092676Seschrock 6102676Seschrock if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 6112676Seschrock (void) fprintf(stderr, gettext("internal error: " 6122676Seschrock "out of memory\n")); 6132676Seschrock return (1); 6142676Seschrock } 615789Sahrens 616789Sahrens /* check options */ 6174490Svb160487 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) { 618789Sahrens switch (c) { 619789Sahrens case 'V': 620789Sahrens type = ZFS_TYPE_VOLUME; 6212676Seschrock if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 6222676Seschrock (void) fprintf(stderr, gettext("bad volume " 6232676Seschrock "size '%s': %s\n"), optarg, 6242676Seschrock libzfs_error_description(g_zfs)); 6252676Seschrock goto error; 6262676Seschrock } 6272676Seschrock 6282676Seschrock if (nvlist_add_uint64(props, 6292676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 6302676Seschrock intval) != 0) { 6312676Seschrock (void) fprintf(stderr, gettext("internal " 6322676Seschrock "error: out of memory\n")); 6332676Seschrock goto error; 6342676Seschrock } 6352676Seschrock volsize = intval; 636789Sahrens break; 6374490Svb160487 case 'p': 6384490Svb160487 parents = B_TRUE; 6394490Svb160487 break; 640789Sahrens case 'b': 6415367Sahrens bflag = B_TRUE; 6422676Seschrock if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 6432676Seschrock (void) fprintf(stderr, gettext("bad volume " 6442676Seschrock "block size '%s': %s\n"), optarg, 6452676Seschrock libzfs_error_description(g_zfs)); 6462676Seschrock goto error; 6472676Seschrock } 6482676Seschrock 6492676Seschrock if (nvlist_add_uint64(props, 6502676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 6512676Seschrock intval) != 0) { 6522676Seschrock (void) fprintf(stderr, gettext("internal " 6532676Seschrock "error: out of memory\n")); 6542676Seschrock goto error; 6552676Seschrock } 6562676Seschrock break; 6572676Seschrock case 'o': 6587265Sahrens if (parseprop(props)) 6592676Seschrock goto error; 660789Sahrens break; 661789Sahrens case 's': 6622082Seschrock noreserve = B_TRUE; 663789Sahrens break; 664789Sahrens case ':': 665789Sahrens (void) fprintf(stderr, gettext("missing size " 666789Sahrens "argument\n")); 6672676Seschrock goto badusage; 668789Sahrens break; 669789Sahrens case '?': 670789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 671789Sahrens optopt); 6722676Seschrock goto badusage; 673789Sahrens } 674789Sahrens } 675789Sahrens 6765367Sahrens if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) { 6775367Sahrens (void) fprintf(stderr, gettext("'-s' and '-b' can only be " 6785367Sahrens "used when creating a volume\n")); 6792676Seschrock goto badusage; 680789Sahrens } 681789Sahrens 682789Sahrens argc -= optind; 683789Sahrens argv += optind; 684789Sahrens 685789Sahrens /* check number of arguments */ 686789Sahrens if (argc == 0) { 687789Sahrens (void) fprintf(stderr, gettext("missing %s argument\n"), 688789Sahrens zfs_type_to_name(type)); 6892676Seschrock goto badusage; 690789Sahrens } 691789Sahrens if (argc > 1) { 692789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 6932676Seschrock goto badusage; 6942676Seschrock } 6952676Seschrock 6965481Sck153898 if (type == ZFS_TYPE_VOLUME && !noreserve) { 6975481Sck153898 zpool_handle_t *zpool_handle; 6985481Sck153898 uint64_t spa_version; 6995481Sck153898 char *p; 7005481Sck153898 zfs_prop_t resv_prop; 7017265Sahrens char *strval; 7025481Sck153898 7035481Sck153898 if (p = strchr(argv[0], '/')) 7045481Sck153898 *p = '\0'; 7055481Sck153898 zpool_handle = zpool_open(g_zfs, argv[0]); 7065481Sck153898 if (p != NULL) 7075481Sck153898 *p = '/'; 7085481Sck153898 if (zpool_handle == NULL) 7095481Sck153898 goto error; 7105481Sck153898 spa_version = zpool_get_prop_int(zpool_handle, 7115481Sck153898 ZPOOL_PROP_VERSION, NULL); 7125481Sck153898 zpool_close(zpool_handle); 7135481Sck153898 if (spa_version >= SPA_VERSION_REFRESERVATION) 7145481Sck153898 resv_prop = ZFS_PROP_REFRESERVATION; 7155481Sck153898 else 7165481Sck153898 resv_prop = ZFS_PROP_RESERVATION; 71711449SEric.Taylor@Sun.COM volsize = zvol_volsize_to_reservation(volsize, props); 7185481Sck153898 7195481Sck153898 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop), 7205481Sck153898 &strval) != 0) { 7215481Sck153898 if (nvlist_add_uint64(props, 7225481Sck153898 zfs_prop_to_name(resv_prop), volsize) != 0) { 7235481Sck153898 (void) fprintf(stderr, gettext("internal " 7245481Sck153898 "error: out of memory\n")); 7255481Sck153898 nvlist_free(props); 7265481Sck153898 return (1); 7275481Sck153898 } 7282676Seschrock } 729789Sahrens } 730789Sahrens 7314490Svb160487 if (parents && zfs_name_valid(argv[0], type)) { 7324490Svb160487 /* 7334490Svb160487 * Now create the ancestors of target dataset. If the target 7344490Svb160487 * already exists and '-p' option was used we should not 7354490Svb160487 * complain. 7364490Svb160487 */ 7374490Svb160487 if (zfs_dataset_exists(g_zfs, argv[0], type)) { 7384490Svb160487 ret = 0; 7394490Svb160487 goto error; 7404490Svb160487 } 7414490Svb160487 if (zfs_create_ancestors(g_zfs, argv[0]) != 0) 7424490Svb160487 goto error; 7434490Svb160487 } 7444490Svb160487 745789Sahrens /* pass to libzfs */ 7462676Seschrock if (zfs_create(g_zfs, argv[0], type, props) != 0) 7472676Seschrock goto error; 748789Sahrens 7495094Slling if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 7502676Seschrock goto error; 7516168Shs24103 /* 7526168Shs24103 * if the user doesn't want the dataset automatically mounted, 7536168Shs24103 * then skip the mount/share step 7546168Shs24103 */ 7556168Shs24103 7566168Shs24103 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 757789Sahrens 758789Sahrens /* 759789Sahrens * Mount and/or share the new filesystem as appropriate. We provide a 760789Sahrens * verbose error message to let the user know that their filesystem was 761789Sahrens * in fact created, even if we failed to mount or share it. 762789Sahrens */ 7636168Shs24103 ret = 0; 7646168Shs24103 if (canmount == ZFS_CANMOUNT_ON) { 7656168Shs24103 if (zfs_mount(zhp, NULL, 0) != 0) { 7666168Shs24103 (void) fprintf(stderr, gettext("filesystem " 7676168Shs24103 "successfully created, but not mounted\n")); 7686168Shs24103 ret = 1; 7696168Shs24103 } else if (zfs_share(zhp) != 0) { 7706168Shs24103 (void) fprintf(stderr, gettext("filesystem " 7716168Shs24103 "successfully created, but not shared\n")); 7726168Shs24103 ret = 1; 7736168Shs24103 } 774789Sahrens } 775789Sahrens 7762676Seschrock error: 7772676Seschrock if (zhp) 7782676Seschrock zfs_close(zhp); 7792676Seschrock nvlist_free(props); 780789Sahrens return (ret); 7812676Seschrock badusage: 7822676Seschrock nvlist_free(props); 7832676Seschrock usage(B_FALSE); 7842676Seschrock return (2); 785789Sahrens } 786789Sahrens 787789Sahrens /* 78810385Schris.kirby@sun.com * zfs destroy [-rRf] <fs, vol> 78910385Schris.kirby@sun.com * zfs destroy [-rRd] <snap> 790789Sahrens * 79111022STom.Erickson@Sun.COM * -r Recursively destroy all children 79211022STom.Erickson@Sun.COM * -R Recursively destroy all dependents, including clones 79311022STom.Erickson@Sun.COM * -f Force unmounting of any dependents 79410242Schris.kirby@sun.com * -d If we can't destroy now, mark for deferred destruction 795789Sahrens * 796789Sahrens * Destroys the given dataset. By default, it will unmount any filesystems, 797789Sahrens * and refuse to destroy a dataset that has any dependents. A dependent can 798789Sahrens * either be a child, or a clone of a child. 799789Sahrens */ 800789Sahrens typedef struct destroy_cbdata { 8012082Seschrock boolean_t cb_first; 802789Sahrens int cb_force; 803789Sahrens int cb_recurse; 804789Sahrens int cb_error; 805789Sahrens int cb_needforce; 806789Sahrens int cb_doclones; 8073265Sahrens boolean_t cb_closezhp; 808789Sahrens zfs_handle_t *cb_target; 8092199Sahrens char *cb_snapname; 81010242Schris.kirby@sun.com boolean_t cb_defer_destroy; 811789Sahrens } destroy_cbdata_t; 812789Sahrens 813789Sahrens /* 814789Sahrens * Check for any dependents based on the '-r' or '-R' flags. 815789Sahrens */ 816789Sahrens static int 817789Sahrens destroy_check_dependent(zfs_handle_t *zhp, void *data) 818789Sahrens { 819789Sahrens destroy_cbdata_t *cbp = data; 820789Sahrens const char *tname = zfs_get_name(cbp->cb_target); 821789Sahrens const char *name = zfs_get_name(zhp); 822789Sahrens 823789Sahrens if (strncmp(tname, name, strlen(tname)) == 0 && 824789Sahrens (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) { 825789Sahrens /* 826789Sahrens * This is a direct descendant, not a clone somewhere else in 827789Sahrens * the hierarchy. 828789Sahrens */ 829789Sahrens if (cbp->cb_recurse) 830789Sahrens goto out; 831789Sahrens 832789Sahrens if (cbp->cb_first) { 833789Sahrens (void) fprintf(stderr, gettext("cannot destroy '%s': " 834789Sahrens "%s has children\n"), 835789Sahrens zfs_get_name(cbp->cb_target), 836789Sahrens zfs_type_to_name(zfs_get_type(cbp->cb_target))); 837789Sahrens (void) fprintf(stderr, gettext("use '-r' to destroy " 838789Sahrens "the following datasets:\n")); 8392082Seschrock cbp->cb_first = B_FALSE; 840789Sahrens cbp->cb_error = 1; 841789Sahrens } 842789Sahrens 843789Sahrens (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 844789Sahrens } else { 845789Sahrens /* 846789Sahrens * This is a clone. We only want to report this if the '-r' 847789Sahrens * wasn't specified, or the target is a snapshot. 848789Sahrens */ 849789Sahrens if (!cbp->cb_recurse && 850789Sahrens zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT) 851789Sahrens goto out; 852789Sahrens 853789Sahrens if (cbp->cb_first) { 854789Sahrens (void) fprintf(stderr, gettext("cannot destroy '%s': " 855789Sahrens "%s has dependent clones\n"), 856789Sahrens zfs_get_name(cbp->cb_target), 857789Sahrens zfs_type_to_name(zfs_get_type(cbp->cb_target))); 858789Sahrens (void) fprintf(stderr, gettext("use '-R' to destroy " 859789Sahrens "the following datasets:\n")); 8602082Seschrock cbp->cb_first = B_FALSE; 861789Sahrens cbp->cb_error = 1; 862789Sahrens } 863789Sahrens 864789Sahrens (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 865789Sahrens } 866789Sahrens 867789Sahrens out: 868789Sahrens zfs_close(zhp); 869789Sahrens return (0); 870789Sahrens } 871789Sahrens 872789Sahrens static int 873789Sahrens destroy_callback(zfs_handle_t *zhp, void *data) 874789Sahrens { 875789Sahrens destroy_cbdata_t *cbp = data; 876789Sahrens 877789Sahrens /* 878789Sahrens * Ignore pools (which we've already flagged as an error before getting 87910588SEric.Taylor@Sun.COM * here). 880789Sahrens */ 881789Sahrens if (strchr(zfs_get_name(zhp), '/') == NULL && 882789Sahrens zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 883789Sahrens zfs_close(zhp); 884789Sahrens return (0); 885789Sahrens } 886789Sahrens 887789Sahrens /* 888789Sahrens * Bail out on the first error. 889789Sahrens */ 890789Sahrens if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 || 89110242Schris.kirby@sun.com zfs_destroy(zhp, cbp->cb_defer_destroy) != 0) { 892789Sahrens zfs_close(zhp); 893789Sahrens return (-1); 894789Sahrens } 895789Sahrens 896789Sahrens zfs_close(zhp); 897789Sahrens return (0); 898789Sahrens } 899789Sahrens 9002199Sahrens static int 9012199Sahrens destroy_snap_clones(zfs_handle_t *zhp, void *arg) 9022199Sahrens { 9032199Sahrens destroy_cbdata_t *cbp = arg; 9042199Sahrens char thissnap[MAXPATHLEN]; 9052199Sahrens zfs_handle_t *szhp; 9063265Sahrens boolean_t closezhp = cbp->cb_closezhp; 9073265Sahrens int rv; 9082199Sahrens 9092199Sahrens (void) snprintf(thissnap, sizeof (thissnap), 9102199Sahrens "%s@%s", zfs_get_name(zhp), cbp->cb_snapname); 9112199Sahrens 9122199Sahrens libzfs_print_on_error(g_zfs, B_FALSE); 9132199Sahrens szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT); 9142199Sahrens libzfs_print_on_error(g_zfs, B_TRUE); 9152199Sahrens if (szhp) { 9162199Sahrens /* 9172199Sahrens * Destroy any clones of this snapshot 9182199Sahrens */ 9192474Seschrock if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback, 9202474Seschrock cbp) != 0) { 9212474Seschrock zfs_close(szhp); 9223265Sahrens if (closezhp) 9233265Sahrens zfs_close(zhp); 9242474Seschrock return (-1); 9252474Seschrock } 9262199Sahrens zfs_close(szhp); 9272199Sahrens } 9282199Sahrens 9293265Sahrens cbp->cb_closezhp = B_TRUE; 9303265Sahrens rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg); 9313265Sahrens if (closezhp) 9323265Sahrens zfs_close(zhp); 9333265Sahrens return (rv); 9342199Sahrens } 935789Sahrens 936789Sahrens static int 937789Sahrens zfs_do_destroy(int argc, char **argv) 938789Sahrens { 939789Sahrens destroy_cbdata_t cb = { 0 }; 940789Sahrens int c; 941789Sahrens zfs_handle_t *zhp; 9422199Sahrens char *cp; 94310385Schris.kirby@sun.com zfs_type_t type = ZFS_TYPE_DATASET; 944789Sahrens 945789Sahrens /* check options */ 94610242Schris.kirby@sun.com while ((c = getopt(argc, argv, "dfrR")) != -1) { 947789Sahrens switch (c) { 94810242Schris.kirby@sun.com case 'd': 94910242Schris.kirby@sun.com cb.cb_defer_destroy = B_TRUE; 95010385Schris.kirby@sun.com type = ZFS_TYPE_SNAPSHOT; 95110242Schris.kirby@sun.com break; 952789Sahrens case 'f': 953789Sahrens cb.cb_force = 1; 954789Sahrens break; 955789Sahrens case 'r': 956789Sahrens cb.cb_recurse = 1; 957789Sahrens break; 958789Sahrens case 'R': 959789Sahrens cb.cb_recurse = 1; 960789Sahrens cb.cb_doclones = 1; 961789Sahrens break; 962789Sahrens case '?': 963789Sahrens default: 964789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 965789Sahrens optopt); 9662082Seschrock usage(B_FALSE); 967789Sahrens } 968789Sahrens } 969789Sahrens 970789Sahrens argc -= optind; 971789Sahrens argv += optind; 972789Sahrens 973789Sahrens /* check number of arguments */ 974789Sahrens if (argc == 0) { 975789Sahrens (void) fprintf(stderr, gettext("missing path argument\n")); 9762082Seschrock usage(B_FALSE); 977789Sahrens } 978789Sahrens if (argc > 1) { 979789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 9802082Seschrock usage(B_FALSE); 981789Sahrens } 982789Sahrens 9832199Sahrens /* 9842199Sahrens * If we are doing recursive destroy of a snapshot, then the 9852199Sahrens * named snapshot may not exist. Go straight to libzfs. 9862199Sahrens */ 9872199Sahrens if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) { 9882199Sahrens int ret; 9892199Sahrens 9902199Sahrens *cp = '\0'; 9915094Slling if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 9922199Sahrens return (1); 9932199Sahrens *cp = '@'; 9942199Sahrens cp++; 9952199Sahrens 9962199Sahrens if (cb.cb_doclones) { 99710385Schris.kirby@sun.com boolean_t defer = cb.cb_defer_destroy; 99810385Schris.kirby@sun.com 99910385Schris.kirby@sun.com /* 100010385Schris.kirby@sun.com * Temporarily ignore the defer_destroy setting since 100110385Schris.kirby@sun.com * it's not supported for clones. 100210385Schris.kirby@sun.com */ 100310385Schris.kirby@sun.com cb.cb_defer_destroy = B_FALSE; 10042199Sahrens cb.cb_snapname = cp; 10052474Seschrock if (destroy_snap_clones(zhp, &cb) != 0) { 10062474Seschrock zfs_close(zhp); 10072474Seschrock return (1); 10082474Seschrock } 100910385Schris.kirby@sun.com cb.cb_defer_destroy = defer; 10102199Sahrens } 10112199Sahrens 101210242Schris.kirby@sun.com ret = zfs_destroy_snaps(zhp, cp, cb.cb_defer_destroy); 10132199Sahrens zfs_close(zhp); 10142199Sahrens if (ret) { 10152199Sahrens (void) fprintf(stderr, 10162199Sahrens gettext("no snapshots destroyed\n")); 10172199Sahrens } 10182199Sahrens return (ret != 0); 10192199Sahrens } 10202199Sahrens 1021789Sahrens /* Open the given dataset */ 102210385Schris.kirby@sun.com if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL) 1023789Sahrens return (1); 1024789Sahrens 1025789Sahrens cb.cb_target = zhp; 1026789Sahrens 1027789Sahrens /* 1028789Sahrens * Perform an explicit check for pools before going any further. 1029789Sahrens */ 1030789Sahrens if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL && 1031789Sahrens zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1032789Sahrens (void) fprintf(stderr, gettext("cannot destroy '%s': " 1033789Sahrens "operation does not apply to pools\n"), 1034789Sahrens zfs_get_name(zhp)); 1035789Sahrens (void) fprintf(stderr, gettext("use 'zfs destroy -r " 1036789Sahrens "%s' to destroy all datasets in the pool\n"), 1037789Sahrens zfs_get_name(zhp)); 1038789Sahrens (void) fprintf(stderr, gettext("use 'zpool destroy %s' " 1039789Sahrens "to destroy the pool itself\n"), zfs_get_name(zhp)); 1040789Sahrens zfs_close(zhp); 1041789Sahrens return (1); 1042789Sahrens } 1043789Sahrens 1044789Sahrens /* 1045789Sahrens * Check for any dependents and/or clones. 1046789Sahrens */ 10472082Seschrock cb.cb_first = B_TRUE; 104810242Schris.kirby@sun.com if (!cb.cb_doclones && !cb.cb_defer_destroy && 10492474Seschrock zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent, 10502474Seschrock &cb) != 0) { 10512474Seschrock zfs_close(zhp); 10522474Seschrock return (1); 10532474Seschrock } 10542474Seschrock 105510242Schris.kirby@sun.com if (cb.cb_error || (!cb.cb_defer_destroy && 105610242Schris.kirby@sun.com (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0))) { 1057789Sahrens zfs_close(zhp); 1058789Sahrens return (1); 1059789Sahrens } 1060789Sahrens 1061789Sahrens /* 10622474Seschrock * Do the real thing. The callback will close the handle regardless of 10632474Seschrock * whether it succeeds or not. 1064789Sahrens */ 10654543Smarks 10662474Seschrock if (destroy_callback(zhp, &cb) != 0) 10672474Seschrock return (1); 10682474Seschrock 10692474Seschrock return (0); 1070789Sahrens } 1071789Sahrens 107211022STom.Erickson@Sun.COM static boolean_t 107311022STom.Erickson@Sun.COM is_recvd_column(zprop_get_cbdata_t *cbp) 107411022STom.Erickson@Sun.COM { 107511022STom.Erickson@Sun.COM int i; 107611022STom.Erickson@Sun.COM zfs_get_column_t col; 107711022STom.Erickson@Sun.COM 107811022STom.Erickson@Sun.COM for (i = 0; i < ZFS_GET_NCOLS && 107911022STom.Erickson@Sun.COM (col = cbp->cb_columns[i]) != GET_COL_NONE; i++) 108011022STom.Erickson@Sun.COM if (col == GET_COL_RECVD) 108111022STom.Erickson@Sun.COM return (B_TRUE); 108211022STom.Erickson@Sun.COM return (B_FALSE); 108311022STom.Erickson@Sun.COM } 108411022STom.Erickson@Sun.COM 1085789Sahrens /* 108611022STom.Erickson@Sun.COM * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...] 108711022STom.Erickson@Sun.COM * < all | property[,property]... > < fs | snap | vol > ... 1088789Sahrens * 1089789Sahrens * -r recurse over any child datasets 1090789Sahrens * -H scripted mode. Headers are stripped, and fields are separated 1091789Sahrens * by tabs instead of spaces. 109211022STom.Erickson@Sun.COM * -o Set of fields to display. One of "name,property,value, 109311022STom.Erickson@Sun.COM * received,source". Default is "name,property,value,source". 109411022STom.Erickson@Sun.COM * "all" is an alias for all five. 1095789Sahrens * -s Set of sources to allow. One of 109611022STom.Erickson@Sun.COM * "local,default,inherited,received,temporary,none". Default is 109711022STom.Erickson@Sun.COM * all six. 1098789Sahrens * -p Display values in parsable (literal) format. 1099789Sahrens * 1100789Sahrens * Prints properties for the given datasets. The user can control which 1101789Sahrens * columns to display as well as which property types to allow. 1102789Sahrens */ 1103789Sahrens 1104789Sahrens /* 1105789Sahrens * Invoked to display the properties for a single dataset. 1106789Sahrens */ 1107789Sahrens static int 1108789Sahrens get_callback(zfs_handle_t *zhp, void *data) 1109789Sahrens { 1110789Sahrens char buf[ZFS_MAXPROPLEN]; 111111022STom.Erickson@Sun.COM char rbuf[ZFS_MAXPROPLEN]; 11125094Slling zprop_source_t sourcetype; 1113789Sahrens char source[ZFS_MAXNAMELEN]; 11145094Slling zprop_get_cbdata_t *cbp = data; 111511022STom.Erickson@Sun.COM nvlist_t *user_props = zfs_get_user_props(zhp); 11165094Slling zprop_list_t *pl = cbp->cb_proplist; 11172676Seschrock nvlist_t *propval; 11182676Seschrock char *strval; 11192676Seschrock char *sourceval; 112011022STom.Erickson@Sun.COM boolean_t received = is_recvd_column(cbp); 11212676Seschrock 11222676Seschrock for (; pl != NULL; pl = pl->pl_next) { 112311022STom.Erickson@Sun.COM char *recvdval = NULL; 11242676Seschrock /* 11252676Seschrock * Skip the special fake placeholder. This will also skip over 11262676Seschrock * the name property when 'all' is specified. 11272676Seschrock */ 11282676Seschrock if (pl->pl_prop == ZFS_PROP_NAME && 11292676Seschrock pl == cbp->cb_proplist) 11302676Seschrock continue; 11312676Seschrock 11325094Slling if (pl->pl_prop != ZPROP_INVAL) { 11332676Seschrock if (zfs_prop_get(zhp, pl->pl_prop, buf, 11342676Seschrock sizeof (buf), &sourcetype, source, 11352676Seschrock sizeof (source), 11362676Seschrock cbp->cb_literal) != 0) { 11372676Seschrock if (pl->pl_all) 11382676Seschrock continue; 11393912Slling if (!zfs_prop_valid_for_type(pl->pl_prop, 11405094Slling ZFS_TYPE_DATASET)) { 11413912Slling (void) fprintf(stderr, 11423912Slling gettext("No such property '%s'\n"), 11433912Slling zfs_prop_to_name(pl->pl_prop)); 11443912Slling continue; 11453912Slling } 11465094Slling sourcetype = ZPROP_SRC_NONE; 11472676Seschrock (void) strlcpy(buf, "-", sizeof (buf)); 11482676Seschrock } 11492676Seschrock 115011022STom.Erickson@Sun.COM if (received && (zfs_prop_get_recvd(zhp, 115111022STom.Erickson@Sun.COM zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf), 115211022STom.Erickson@Sun.COM cbp->cb_literal) == 0)) 115311022STom.Erickson@Sun.COM recvdval = rbuf; 115411022STom.Erickson@Sun.COM 11555094Slling zprop_print_one_property(zfs_get_name(zhp), cbp, 11562676Seschrock zfs_prop_to_name(pl->pl_prop), 115711022STom.Erickson@Sun.COM buf, sourcetype, source, recvdval); 11589396SMatthew.Ahrens@Sun.COM } else if (zfs_prop_userquota(pl->pl_user_prop)) { 11599396SMatthew.Ahrens@Sun.COM sourcetype = ZPROP_SRC_LOCAL; 11609396SMatthew.Ahrens@Sun.COM 11619396SMatthew.Ahrens@Sun.COM if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 11629396SMatthew.Ahrens@Sun.COM buf, sizeof (buf), cbp->cb_literal) != 0) { 11639396SMatthew.Ahrens@Sun.COM sourcetype = ZPROP_SRC_NONE; 11649396SMatthew.Ahrens@Sun.COM (void) strlcpy(buf, "-", sizeof (buf)); 11659396SMatthew.Ahrens@Sun.COM } 11669396SMatthew.Ahrens@Sun.COM 11679396SMatthew.Ahrens@Sun.COM zprop_print_one_property(zfs_get_name(zhp), cbp, 116811022STom.Erickson@Sun.COM pl->pl_user_prop, buf, sourcetype, source, NULL); 11692676Seschrock } else { 117011022STom.Erickson@Sun.COM if (nvlist_lookup_nvlist(user_props, 11712676Seschrock pl->pl_user_prop, &propval) != 0) { 11722676Seschrock if (pl->pl_all) 11732676Seschrock continue; 11745094Slling sourcetype = ZPROP_SRC_NONE; 11752676Seschrock strval = "-"; 11762676Seschrock } else { 11772676Seschrock verify(nvlist_lookup_string(propval, 11785094Slling ZPROP_VALUE, &strval) == 0); 11792676Seschrock verify(nvlist_lookup_string(propval, 11805094Slling ZPROP_SOURCE, &sourceval) == 0); 11812676Seschrock 11822676Seschrock if (strcmp(sourceval, 11832676Seschrock zfs_get_name(zhp)) == 0) { 11845094Slling sourcetype = ZPROP_SRC_LOCAL; 118511022STom.Erickson@Sun.COM } else if (strcmp(sourceval, 118611022STom.Erickson@Sun.COM ZPROP_SOURCE_VAL_RECVD) == 0) { 118711022STom.Erickson@Sun.COM sourcetype = ZPROP_SRC_RECEIVED; 11882676Seschrock } else { 11895094Slling sourcetype = ZPROP_SRC_INHERITED; 11902676Seschrock (void) strlcpy(source, 11912676Seschrock sourceval, sizeof (source)); 11922676Seschrock } 11932676Seschrock } 11942676Seschrock 119511022STom.Erickson@Sun.COM if (received && (zfs_prop_get_recvd(zhp, 119611022STom.Erickson@Sun.COM pl->pl_user_prop, rbuf, sizeof (rbuf), 119711022STom.Erickson@Sun.COM cbp->cb_literal) == 0)) 119811022STom.Erickson@Sun.COM recvdval = rbuf; 119911022STom.Erickson@Sun.COM 12005094Slling zprop_print_one_property(zfs_get_name(zhp), cbp, 12012676Seschrock pl->pl_user_prop, strval, sourcetype, 120211022STom.Erickson@Sun.COM source, recvdval); 1203866Seschrock } 1204789Sahrens } 1205789Sahrens 1206789Sahrens return (0); 1207789Sahrens } 1208789Sahrens 1209789Sahrens static int 1210789Sahrens zfs_do_get(int argc, char **argv) 1211789Sahrens { 12125094Slling zprop_get_cbdata_t cb = { 0 }; 12137538SRichard.Morris@Sun.COM int i, c, flags = 0; 12142676Seschrock char *value, *fields; 1215866Seschrock int ret; 12169365SChris.Gerhard@sun.com int limit = 0; 12175094Slling zprop_list_t fake_name = { 0 }; 1218789Sahrens 1219789Sahrens /* 1220789Sahrens * Set up default columns and sources. 1221789Sahrens */ 12225094Slling cb.cb_sources = ZPROP_SRC_ALL; 1223789Sahrens cb.cb_columns[0] = GET_COL_NAME; 1224789Sahrens cb.cb_columns[1] = GET_COL_PROPERTY; 1225789Sahrens cb.cb_columns[2] = GET_COL_VALUE; 1226789Sahrens cb.cb_columns[3] = GET_COL_SOURCE; 12275094Slling cb.cb_type = ZFS_TYPE_DATASET; 1228789Sahrens 1229789Sahrens /* check options */ 12309365SChris.Gerhard@sun.com while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) { 1231789Sahrens switch (c) { 1232789Sahrens case 'p': 12332082Seschrock cb.cb_literal = B_TRUE; 1234789Sahrens break; 12359365SChris.Gerhard@sun.com case 'd': 12369365SChris.Gerhard@sun.com limit = parse_depth(optarg, &flags); 12379365SChris.Gerhard@sun.com break; 1238789Sahrens case 'r': 12397538SRichard.Morris@Sun.COM flags |= ZFS_ITER_RECURSE; 1240789Sahrens break; 1241789Sahrens case 'H': 12422082Seschrock cb.cb_scripted = B_TRUE; 1243789Sahrens break; 1244789Sahrens case ':': 1245789Sahrens (void) fprintf(stderr, gettext("missing argument for " 1246789Sahrens "'%c' option\n"), optopt); 12472082Seschrock usage(B_FALSE); 1248789Sahrens break; 1249789Sahrens case 'o': 1250789Sahrens /* 1251789Sahrens * Process the set of columns to display. We zero out 1252789Sahrens * the structure to give us a blank slate. 1253789Sahrens */ 1254789Sahrens bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 1255789Sahrens i = 0; 1256789Sahrens while (*optarg != '\0') { 1257789Sahrens static char *col_subopts[] = 125811022STom.Erickson@Sun.COM { "name", "property", "value", "received", 125911022STom.Erickson@Sun.COM "source", "all", NULL }; 126011022STom.Erickson@Sun.COM 126111022STom.Erickson@Sun.COM if (i == ZFS_GET_NCOLS) { 1262789Sahrens (void) fprintf(stderr, gettext("too " 1263789Sahrens "many fields given to -o " 1264789Sahrens "option\n")); 12652082Seschrock usage(B_FALSE); 1266789Sahrens } 1267789Sahrens 1268789Sahrens switch (getsubopt(&optarg, col_subopts, 1269789Sahrens &value)) { 1270789Sahrens case 0: 1271789Sahrens cb.cb_columns[i++] = GET_COL_NAME; 1272789Sahrens break; 1273789Sahrens case 1: 1274789Sahrens cb.cb_columns[i++] = GET_COL_PROPERTY; 1275789Sahrens break; 1276789Sahrens case 2: 1277789Sahrens cb.cb_columns[i++] = GET_COL_VALUE; 1278789Sahrens break; 1279789Sahrens case 3: 128011022STom.Erickson@Sun.COM cb.cb_columns[i++] = GET_COL_RECVD; 128111022STom.Erickson@Sun.COM flags |= ZFS_ITER_RECVD_PROPS; 128211022STom.Erickson@Sun.COM break; 128311022STom.Erickson@Sun.COM case 4: 1284789Sahrens cb.cb_columns[i++] = GET_COL_SOURCE; 1285789Sahrens break; 128611022STom.Erickson@Sun.COM case 5: 128711022STom.Erickson@Sun.COM if (i > 0) { 128811022STom.Erickson@Sun.COM (void) fprintf(stderr, 128911022STom.Erickson@Sun.COM gettext("\"all\" conflicts " 129011022STom.Erickson@Sun.COM "with specific fields " 129111022STom.Erickson@Sun.COM "given to -o option\n")); 129211022STom.Erickson@Sun.COM usage(B_FALSE); 129311022STom.Erickson@Sun.COM } 129411022STom.Erickson@Sun.COM cb.cb_columns[0] = GET_COL_NAME; 129511022STom.Erickson@Sun.COM cb.cb_columns[1] = GET_COL_PROPERTY; 129611022STom.Erickson@Sun.COM cb.cb_columns[2] = GET_COL_VALUE; 129711022STom.Erickson@Sun.COM cb.cb_columns[3] = GET_COL_RECVD; 129811022STom.Erickson@Sun.COM cb.cb_columns[4] = GET_COL_SOURCE; 129911022STom.Erickson@Sun.COM flags |= ZFS_ITER_RECVD_PROPS; 130011022STom.Erickson@Sun.COM i = ZFS_GET_NCOLS; 130111022STom.Erickson@Sun.COM break; 1302789Sahrens default: 1303789Sahrens (void) fprintf(stderr, 1304789Sahrens gettext("invalid column name " 1305789Sahrens "'%s'\n"), value); 13063912Slling usage(B_FALSE); 1307789Sahrens } 1308789Sahrens } 1309789Sahrens break; 1310789Sahrens 1311789Sahrens case 's': 1312789Sahrens cb.cb_sources = 0; 1313789Sahrens while (*optarg != '\0') { 1314789Sahrens static char *source_subopts[] = { 1315789Sahrens "local", "default", "inherited", 131611022STom.Erickson@Sun.COM "received", "temporary", "none", 131711022STom.Erickson@Sun.COM NULL }; 1318789Sahrens 1319789Sahrens switch (getsubopt(&optarg, source_subopts, 1320789Sahrens &value)) { 1321789Sahrens case 0: 13225094Slling cb.cb_sources |= ZPROP_SRC_LOCAL; 1323789Sahrens break; 1324789Sahrens case 1: 13255094Slling cb.cb_sources |= ZPROP_SRC_DEFAULT; 1326789Sahrens break; 1327789Sahrens case 2: 13285094Slling cb.cb_sources |= ZPROP_SRC_INHERITED; 1329789Sahrens break; 1330789Sahrens case 3: 133111022STom.Erickson@Sun.COM cb.cb_sources |= ZPROP_SRC_RECEIVED; 133211022STom.Erickson@Sun.COM break; 133311022STom.Erickson@Sun.COM case 4: 13345094Slling cb.cb_sources |= ZPROP_SRC_TEMPORARY; 1335789Sahrens break; 133611022STom.Erickson@Sun.COM case 5: 13375094Slling cb.cb_sources |= ZPROP_SRC_NONE; 1338789Sahrens break; 1339789Sahrens default: 1340789Sahrens (void) fprintf(stderr, 1341789Sahrens gettext("invalid source " 1342789Sahrens "'%s'\n"), value); 13433912Slling usage(B_FALSE); 1344789Sahrens } 1345789Sahrens } 1346789Sahrens break; 1347789Sahrens 1348789Sahrens case '?': 1349789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1350789Sahrens optopt); 13512082Seschrock usage(B_FALSE); 1352789Sahrens } 1353789Sahrens } 1354789Sahrens 1355789Sahrens argc -= optind; 1356789Sahrens argv += optind; 1357789Sahrens 1358789Sahrens if (argc < 1) { 1359789Sahrens (void) fprintf(stderr, gettext("missing property " 1360789Sahrens "argument\n")); 13612082Seschrock usage(B_FALSE); 1362789Sahrens } 1363789Sahrens 1364789Sahrens fields = argv[0]; 1365789Sahrens 13665094Slling if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 13675094Slling != 0) 13682082Seschrock usage(B_FALSE); 1369789Sahrens 1370789Sahrens argc--; 1371789Sahrens argv++; 1372789Sahrens 1373789Sahrens /* 13742676Seschrock * As part of zfs_expand_proplist(), we keep track of the maximum column 13752676Seschrock * width for each property. For the 'NAME' (and 'SOURCE') columns, we 13762676Seschrock * need to know the maximum name length. However, the user likely did 13772676Seschrock * not specify 'name' as one of the properties to fetch, so we need to 13782676Seschrock * make sure we always include at least this property for 13792676Seschrock * print_get_headers() to work properly. 1380789Sahrens */ 13812676Seschrock if (cb.cb_proplist != NULL) { 13822676Seschrock fake_name.pl_prop = ZFS_PROP_NAME; 13832676Seschrock fake_name.pl_width = strlen(gettext("NAME")); 13842676Seschrock fake_name.pl_next = cb.cb_proplist; 13852676Seschrock cb.cb_proplist = &fake_name; 1386789Sahrens } 1387789Sahrens 13882676Seschrock cb.cb_first = B_TRUE; 13892676Seschrock 1390789Sahrens /* run for each object */ 13917538SRichard.Morris@Sun.COM ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL, 13929365SChris.Gerhard@sun.com &cb.cb_proplist, limit, get_callback, &cb); 13932676Seschrock 13942676Seschrock if (cb.cb_proplist == &fake_name) 13955094Slling zprop_free_list(fake_name.pl_next); 13962676Seschrock else 13975094Slling zprop_free_list(cb.cb_proplist); 13982676Seschrock 13992676Seschrock return (ret); 1400789Sahrens } 1401789Sahrens 1402789Sahrens /* 140311022STom.Erickson@Sun.COM * inherit [-rS] <property> <fs|vol> ... 1404789Sahrens * 140511022STom.Erickson@Sun.COM * -r Recurse over all children 140611022STom.Erickson@Sun.COM * -S Revert to received value, if any 1407789Sahrens * 1408789Sahrens * For each dataset specified on the command line, inherit the given property 1409789Sahrens * from its parent. Inheriting a property at the pool level will cause it to 1410789Sahrens * use the default value. The '-r' flag will recurse over all children, and is 1411789Sahrens * useful for setting a property on a hierarchy-wide basis, regardless of any 1412789Sahrens * local modifications for each dataset. 1413789Sahrens */ 14142926Sek110237 141511022STom.Erickson@Sun.COM typedef struct inherit_cbdata { 141611022STom.Erickson@Sun.COM const char *cb_propname; 141711022STom.Erickson@Sun.COM boolean_t cb_received; 141811022STom.Erickson@Sun.COM } inherit_cbdata_t; 141911022STom.Erickson@Sun.COM 1420789Sahrens static int 14217265Sahrens inherit_recurse_cb(zfs_handle_t *zhp, void *data) 1422789Sahrens { 142311022STom.Erickson@Sun.COM inherit_cbdata_t *cb = data; 142411022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname); 14257265Sahrens 14267265Sahrens /* 14277265Sahrens * If we're doing it recursively, then ignore properties that 14287265Sahrens * are not valid for this type of dataset. 14297265Sahrens */ 14307265Sahrens if (prop != ZPROP_INVAL && 14317265Sahrens !zfs_prop_valid_for_type(prop, zfs_get_type(zhp))) 14327265Sahrens return (0); 14337265Sahrens 143411022STom.Erickson@Sun.COM return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 14357265Sahrens } 14367265Sahrens 14377265Sahrens static int 14387265Sahrens inherit_cb(zfs_handle_t *zhp, void *data) 14397265Sahrens { 144011022STom.Erickson@Sun.COM inherit_cbdata_t *cb = data; 144111022STom.Erickson@Sun.COM 144211022STom.Erickson@Sun.COM return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1443789Sahrens } 1444789Sahrens 1445789Sahrens static int 1446789Sahrens zfs_do_inherit(int argc, char **argv) 1447789Sahrens { 1448789Sahrens int c; 1449789Sahrens zfs_prop_t prop; 145011022STom.Erickson@Sun.COM inherit_cbdata_t cb = { 0 }; 14514543Smarks char *propname; 14522926Sek110237 int ret; 14537538SRichard.Morris@Sun.COM int flags = 0; 145411022STom.Erickson@Sun.COM boolean_t received = B_FALSE; 1455789Sahrens 1456789Sahrens /* check options */ 145711022STom.Erickson@Sun.COM while ((c = getopt(argc, argv, "rS")) != -1) { 1458789Sahrens switch (c) { 1459789Sahrens case 'r': 14607538SRichard.Morris@Sun.COM flags |= ZFS_ITER_RECURSE; 1461789Sahrens break; 146211022STom.Erickson@Sun.COM case 'S': 146311022STom.Erickson@Sun.COM received = B_TRUE; 146411022STom.Erickson@Sun.COM break; 1465789Sahrens case '?': 1466789Sahrens default: 1467789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1468789Sahrens optopt); 14692082Seschrock usage(B_FALSE); 1470789Sahrens } 1471789Sahrens } 1472789Sahrens 1473789Sahrens argc -= optind; 1474789Sahrens argv += optind; 1475789Sahrens 1476789Sahrens /* check number of arguments */ 1477789Sahrens if (argc < 1) { 1478789Sahrens (void) fprintf(stderr, gettext("missing property argument\n")); 14792082Seschrock usage(B_FALSE); 1480789Sahrens } 1481789Sahrens if (argc < 2) { 1482789Sahrens (void) fprintf(stderr, gettext("missing dataset argument\n")); 14832082Seschrock usage(B_FALSE); 1484789Sahrens } 1485789Sahrens 14864543Smarks propname = argv[0]; 14872676Seschrock argc--; 14882676Seschrock argv++; 14892676Seschrock 14905094Slling if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 14912676Seschrock if (zfs_prop_readonly(prop)) { 14922676Seschrock (void) fprintf(stderr, gettext( 14932676Seschrock "%s property is read-only\n"), 14944543Smarks propname); 14952676Seschrock return (1); 14962676Seschrock } 149711022STom.Erickson@Sun.COM if (!zfs_prop_inheritable(prop) && !received) { 14982676Seschrock (void) fprintf(stderr, gettext("'%s' property cannot " 14994543Smarks "be inherited\n"), propname); 15002676Seschrock if (prop == ZFS_PROP_QUOTA || 15015378Sck153898 prop == ZFS_PROP_RESERVATION || 15025378Sck153898 prop == ZFS_PROP_REFQUOTA || 15035378Sck153898 prop == ZFS_PROP_REFRESERVATION) 15042676Seschrock (void) fprintf(stderr, gettext("use 'zfs set " 15054543Smarks "%s=none' to clear\n"), propname); 15062676Seschrock return (1); 15072676Seschrock } 150811515STom.Erickson@Sun.COM if (received && (prop == ZFS_PROP_VOLSIZE || 150911515STom.Erickson@Sun.COM prop == ZFS_PROP_VERSION)) { 151011515STom.Erickson@Sun.COM (void) fprintf(stderr, gettext("'%s' property cannot " 151111515STom.Erickson@Sun.COM "be reverted to a received value\n"), propname); 151211515STom.Erickson@Sun.COM return (1); 151311515STom.Erickson@Sun.COM } 15144543Smarks } else if (!zfs_prop_user(propname)) { 15154543Smarks (void) fprintf(stderr, gettext("invalid property '%s'\n"), 15164543Smarks propname); 15172082Seschrock usage(B_FALSE); 1518789Sahrens } 15192676Seschrock 152011022STom.Erickson@Sun.COM cb.cb_propname = propname; 152111022STom.Erickson@Sun.COM cb.cb_received = received; 152211022STom.Erickson@Sun.COM 15237538SRichard.Morris@Sun.COM if (flags & ZFS_ITER_RECURSE) { 15247538SRichard.Morris@Sun.COM ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 152511022STom.Erickson@Sun.COM NULL, NULL, 0, inherit_recurse_cb, &cb); 15267265Sahrens } else { 15277538SRichard.Morris@Sun.COM ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 152811022STom.Erickson@Sun.COM NULL, NULL, 0, inherit_cb, &cb); 15297265Sahrens } 15302926Sek110237 15312926Sek110237 return (ret); 1532789Sahrens } 1533789Sahrens 15344577Sahrens typedef struct upgrade_cbdata { 15354577Sahrens uint64_t cb_numupgraded; 15364577Sahrens uint64_t cb_numsamegraded; 15374849Sahrens uint64_t cb_numfailed; 15384577Sahrens uint64_t cb_version; 15394577Sahrens boolean_t cb_newer; 15404577Sahrens boolean_t cb_foundone; 15414577Sahrens char cb_lastfs[ZFS_MAXNAMELEN]; 15424577Sahrens } upgrade_cbdata_t; 15434577Sahrens 15444577Sahrens static int 15454577Sahrens same_pool(zfs_handle_t *zhp, const char *name) 15464577Sahrens { 15474577Sahrens int len1 = strcspn(name, "/@"); 15484577Sahrens const char *zhname = zfs_get_name(zhp); 15494577Sahrens int len2 = strcspn(zhname, "/@"); 15504577Sahrens 15514577Sahrens if (len1 != len2) 15524577Sahrens return (B_FALSE); 15534988Sek110237 return (strncmp(name, zhname, len1) == 0); 15544577Sahrens } 15554577Sahrens 15564577Sahrens static int 15574577Sahrens upgrade_list_callback(zfs_handle_t *zhp, void *data) 15584577Sahrens { 15594577Sahrens upgrade_cbdata_t *cb = data; 15604577Sahrens int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 15614577Sahrens 15624577Sahrens /* list if it's old/new */ 15634577Sahrens if ((!cb->cb_newer && version < ZPL_VERSION) || 15645367Sahrens (cb->cb_newer && version > ZPL_VERSION)) { 15654577Sahrens char *str; 15664577Sahrens if (cb->cb_newer) { 15674577Sahrens str = gettext("The following filesystems are " 15684577Sahrens "formatted using a newer software version and\n" 15694577Sahrens "cannot be accessed on the current system.\n\n"); 15704577Sahrens } else { 15714577Sahrens str = gettext("The following filesystems are " 15724577Sahrens "out of date, and can be upgraded. After being\n" 15734577Sahrens "upgraded, these filesystems (and any 'zfs send' " 15744577Sahrens "streams generated from\n" 15754577Sahrens "subsequent snapshots) will no longer be " 15764577Sahrens "accessible by older software versions.\n\n"); 15774577Sahrens } 15784577Sahrens 15794577Sahrens if (!cb->cb_foundone) { 15804577Sahrens (void) puts(str); 15814577Sahrens (void) printf(gettext("VER FILESYSTEM\n")); 15824577Sahrens (void) printf(gettext("--- ------------\n")); 15834577Sahrens cb->cb_foundone = B_TRUE; 15844577Sahrens } 15854577Sahrens 15864577Sahrens (void) printf("%2u %s\n", version, zfs_get_name(zhp)); 15874577Sahrens } 15884577Sahrens 15894577Sahrens return (0); 15904577Sahrens } 15914577Sahrens 15924577Sahrens static int 15934577Sahrens upgrade_set_callback(zfs_handle_t *zhp, void *data) 15944577Sahrens { 15954577Sahrens upgrade_cbdata_t *cb = data; 15964577Sahrens int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 159711935SMark.Shellenbaum@Sun.COM int needed_spa_version; 159811935SMark.Shellenbaum@Sun.COM int spa_version; 159911935SMark.Shellenbaum@Sun.COM 160011935SMark.Shellenbaum@Sun.COM if (zfs_spa_version(zhp, &spa_version) < 0) 160111935SMark.Shellenbaum@Sun.COM return (-1); 160211935SMark.Shellenbaum@Sun.COM 160311935SMark.Shellenbaum@Sun.COM needed_spa_version = zfs_spa_version_map(cb->cb_version); 160411935SMark.Shellenbaum@Sun.COM 160511935SMark.Shellenbaum@Sun.COM if (needed_spa_version < 0) 160611935SMark.Shellenbaum@Sun.COM return (-1); 160711935SMark.Shellenbaum@Sun.COM 160811935SMark.Shellenbaum@Sun.COM if (spa_version < needed_spa_version) { 160911935SMark.Shellenbaum@Sun.COM /* can't upgrade */ 161011935SMark.Shellenbaum@Sun.COM (void) printf(gettext("%s: can not be " 161111935SMark.Shellenbaum@Sun.COM "upgraded; the pool version needs to first " 161211935SMark.Shellenbaum@Sun.COM "be upgraded\nto version %d\n\n"), 161311935SMark.Shellenbaum@Sun.COM zfs_get_name(zhp), needed_spa_version); 161411935SMark.Shellenbaum@Sun.COM cb->cb_numfailed++; 161511935SMark.Shellenbaum@Sun.COM return (0); 16165331Samw } 16175331Samw 16184577Sahrens /* upgrade */ 16194577Sahrens if (version < cb->cb_version) { 16204577Sahrens char verstr[16]; 16214849Sahrens (void) snprintf(verstr, sizeof (verstr), 16224849Sahrens "%llu", cb->cb_version); 16234577Sahrens if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) { 16244577Sahrens /* 16254577Sahrens * If they did "zfs upgrade -a", then we could 16264577Sahrens * be doing ioctls to different pools. We need 16274577Sahrens * to log this history once to each pool. 16284577Sahrens */ 16294988Sek110237 verify(zpool_stage_history(g_zfs, history_str) == 0); 16304577Sahrens } 16314577Sahrens if (zfs_prop_set(zhp, "version", verstr) == 0) 16324577Sahrens cb->cb_numupgraded++; 16334849Sahrens else 16344849Sahrens cb->cb_numfailed++; 16354577Sahrens (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp)); 16364577Sahrens } else if (version > cb->cb_version) { 16374577Sahrens /* can't downgrade */ 16384577Sahrens (void) printf(gettext("%s: can not be downgraded; " 16394577Sahrens "it is already at version %u\n"), 16404577Sahrens zfs_get_name(zhp), version); 16414849Sahrens cb->cb_numfailed++; 16424577Sahrens } else { 16434577Sahrens cb->cb_numsamegraded++; 16444577Sahrens } 16454577Sahrens return (0); 16464577Sahrens } 16474577Sahrens 16484577Sahrens /* 16494577Sahrens * zfs upgrade 16504577Sahrens * zfs upgrade -v 16514577Sahrens * zfs upgrade [-r] [-V <version>] <-a | filesystem> 16524577Sahrens */ 16534577Sahrens static int 16544577Sahrens zfs_do_upgrade(int argc, char **argv) 16554577Sahrens { 16564577Sahrens boolean_t all = B_FALSE; 16574577Sahrens boolean_t showversions = B_FALSE; 16584577Sahrens int ret; 16594577Sahrens upgrade_cbdata_t cb = { 0 }; 16604577Sahrens char c; 16617538SRichard.Morris@Sun.COM int flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 16624577Sahrens 16634577Sahrens /* check options */ 16644577Sahrens while ((c = getopt(argc, argv, "rvV:a")) != -1) { 16654577Sahrens switch (c) { 16664577Sahrens case 'r': 16677538SRichard.Morris@Sun.COM flags |= ZFS_ITER_RECURSE; 16684577Sahrens break; 16694577Sahrens case 'v': 16704577Sahrens showversions = B_TRUE; 16714577Sahrens break; 16724577Sahrens case 'V': 16734577Sahrens if (zfs_prop_string_to_index(ZFS_PROP_VERSION, 16744577Sahrens optarg, &cb.cb_version) != 0) { 16754577Sahrens (void) fprintf(stderr, 16764577Sahrens gettext("invalid version %s\n"), optarg); 16774577Sahrens usage(B_FALSE); 16784577Sahrens } 16794577Sahrens break; 16804577Sahrens case 'a': 16814577Sahrens all = B_TRUE; 16824577Sahrens break; 16834577Sahrens case '?': 16844577Sahrens default: 16854577Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 16864577Sahrens optopt); 16874577Sahrens usage(B_FALSE); 16884577Sahrens } 16894577Sahrens } 16904577Sahrens 16914577Sahrens argc -= optind; 16924577Sahrens argv += optind; 16934577Sahrens 16947538SRichard.Morris@Sun.COM if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version)) 16954577Sahrens usage(B_FALSE); 16967538SRichard.Morris@Sun.COM if (showversions && (flags & ZFS_ITER_RECURSE || all || 16977538SRichard.Morris@Sun.COM cb.cb_version || argc)) 16984577Sahrens usage(B_FALSE); 16994577Sahrens if ((all || argc) && (showversions)) 17004577Sahrens usage(B_FALSE); 17014577Sahrens if (all && argc) 17024577Sahrens usage(B_FALSE); 17034577Sahrens 17044577Sahrens if (showversions) { 17054577Sahrens /* Show info on available versions. */ 17064577Sahrens (void) printf(gettext("The following filesystem versions are " 17074577Sahrens "supported:\n\n")); 17084577Sahrens (void) printf(gettext("VER DESCRIPTION\n")); 17094577Sahrens (void) printf("--- -----------------------------------------" 17104577Sahrens "---------------\n"); 17114577Sahrens (void) printf(gettext(" 1 Initial ZFS filesystem version\n")); 17124577Sahrens (void) printf(gettext(" 2 Enhanced directory entries\n")); 17135331Samw (void) printf(gettext(" 3 Case insensitive and File system " 171410228SStephanie.Scheffler@Sun.COM "unique identifier (FUID)\n")); 17159396SMatthew.Ahrens@Sun.COM (void) printf(gettext(" 4 userquota, groupquota " 17169396SMatthew.Ahrens@Sun.COM "properties\n")); 171711935SMark.Shellenbaum@Sun.COM (void) printf(gettext(" 5 System attributes\n")); 17184577Sahrens (void) printf(gettext("\nFor more information on a particular " 17194577Sahrens "version, including supported releases, see:\n\n")); 17204577Sahrens (void) printf("http://www.opensolaris.org/os/community/zfs/" 17214577Sahrens "version/zpl/N\n\n"); 17224577Sahrens (void) printf(gettext("Where 'N' is the version number.\n")); 17234577Sahrens ret = 0; 17244577Sahrens } else if (argc || all) { 17254577Sahrens /* Upgrade filesystems */ 17264577Sahrens if (cb.cb_version == 0) 17274577Sahrens cb.cb_version = ZPL_VERSION; 17287538SRichard.Morris@Sun.COM ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM, 17299365SChris.Gerhard@sun.com NULL, NULL, 0, upgrade_set_callback, &cb); 17304577Sahrens (void) printf(gettext("%llu filesystems upgraded\n"), 17314577Sahrens cb.cb_numupgraded); 17324577Sahrens if (cb.cb_numsamegraded) { 17334577Sahrens (void) printf(gettext("%llu filesystems already at " 17344577Sahrens "this version\n"), 17354577Sahrens cb.cb_numsamegraded); 17364577Sahrens } 17374849Sahrens if (cb.cb_numfailed != 0) 17384577Sahrens ret = 1; 17394577Sahrens } else { 17404577Sahrens /* List old-version filesytems */ 17414577Sahrens boolean_t found; 17424577Sahrens (void) printf(gettext("This system is currently running " 17434577Sahrens "ZFS filesystem version %llu.\n\n"), ZPL_VERSION); 17444577Sahrens 17457538SRichard.Morris@Sun.COM flags |= ZFS_ITER_RECURSE; 17467538SRichard.Morris@Sun.COM ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 17479365SChris.Gerhard@sun.com NULL, NULL, 0, upgrade_list_callback, &cb); 17484577Sahrens 17494577Sahrens found = cb.cb_foundone; 17504577Sahrens cb.cb_foundone = B_FALSE; 17514577Sahrens cb.cb_newer = B_TRUE; 17524577Sahrens 17537538SRichard.Morris@Sun.COM ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 17549365SChris.Gerhard@sun.com NULL, NULL, 0, upgrade_list_callback, &cb); 17554577Sahrens 17564577Sahrens if (!cb.cb_foundone && !found) { 17574577Sahrens (void) printf(gettext("All filesystems are " 17584577Sahrens "formatted with the current version.\n")); 17594577Sahrens } 17604577Sahrens } 17614577Sahrens 17624577Sahrens return (ret); 17634577Sahrens } 17644577Sahrens 1765789Sahrens /* 17669396SMatthew.Ahrens@Sun.COM * zfs userspace 17679396SMatthew.Ahrens@Sun.COM */ 17689554SMatthew.Ahrens@Sun.COM static int 17699396SMatthew.Ahrens@Sun.COM userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space) 17709396SMatthew.Ahrens@Sun.COM { 17719396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t *typep = arg; 17729396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t p = *typep; 17739554SMatthew.Ahrens@Sun.COM char *name = NULL; 17749554SMatthew.Ahrens@Sun.COM char *ug, *propname; 17759396SMatthew.Ahrens@Sun.COM char namebuf[32]; 17769396SMatthew.Ahrens@Sun.COM char sizebuf[32]; 17779396SMatthew.Ahrens@Sun.COM 17789396SMatthew.Ahrens@Sun.COM if (domain == NULL || domain[0] == '\0') { 17799396SMatthew.Ahrens@Sun.COM if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) { 17809396SMatthew.Ahrens@Sun.COM struct group *g = getgrgid(rid); 17819396SMatthew.Ahrens@Sun.COM if (g) 17829396SMatthew.Ahrens@Sun.COM name = g->gr_name; 17839396SMatthew.Ahrens@Sun.COM } else { 17849396SMatthew.Ahrens@Sun.COM struct passwd *p = getpwuid(rid); 17859396SMatthew.Ahrens@Sun.COM if (p) 17869396SMatthew.Ahrens@Sun.COM name = p->pw_name; 17879396SMatthew.Ahrens@Sun.COM } 17889396SMatthew.Ahrens@Sun.COM } 17899396SMatthew.Ahrens@Sun.COM 17909396SMatthew.Ahrens@Sun.COM if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) 17919396SMatthew.Ahrens@Sun.COM ug = "group"; 17929396SMatthew.Ahrens@Sun.COM else 17939396SMatthew.Ahrens@Sun.COM ug = "user"; 17949396SMatthew.Ahrens@Sun.COM 17959396SMatthew.Ahrens@Sun.COM if (p == ZFS_PROP_USERUSED || p == ZFS_PROP_GROUPUSED) 17969396SMatthew.Ahrens@Sun.COM propname = "used"; 17979396SMatthew.Ahrens@Sun.COM else 17989396SMatthew.Ahrens@Sun.COM propname = "quota"; 17999396SMatthew.Ahrens@Sun.COM 18009554SMatthew.Ahrens@Sun.COM if (name == NULL) { 18019396SMatthew.Ahrens@Sun.COM (void) snprintf(namebuf, sizeof (namebuf), 18029396SMatthew.Ahrens@Sun.COM "%llu", (longlong_t)rid); 18039396SMatthew.Ahrens@Sun.COM name = namebuf; 18049396SMatthew.Ahrens@Sun.COM } 18059396SMatthew.Ahrens@Sun.COM zfs_nicenum(space, sizebuf, sizeof (sizebuf)); 18069396SMatthew.Ahrens@Sun.COM 18079396SMatthew.Ahrens@Sun.COM (void) printf("%s %s %s%c%s %s\n", propname, ug, domain, 18089396SMatthew.Ahrens@Sun.COM domain[0] ? '-' : ' ', name, sizebuf); 18099554SMatthew.Ahrens@Sun.COM 18109554SMatthew.Ahrens@Sun.COM return (0); 18119396SMatthew.Ahrens@Sun.COM } 18129396SMatthew.Ahrens@Sun.COM 18139396SMatthew.Ahrens@Sun.COM static int 18149396SMatthew.Ahrens@Sun.COM zfs_do_userspace(int argc, char **argv) 18159396SMatthew.Ahrens@Sun.COM { 18169396SMatthew.Ahrens@Sun.COM zfs_handle_t *zhp; 18179396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t p; 18189396SMatthew.Ahrens@Sun.COM int error; 18199396SMatthew.Ahrens@Sun.COM 18209396SMatthew.Ahrens@Sun.COM /* 18219396SMatthew.Ahrens@Sun.COM * Try the python version. If the execv fails, we'll continue 18229396SMatthew.Ahrens@Sun.COM * and do a simplistic implementation. 18239396SMatthew.Ahrens@Sun.COM */ 18249396SMatthew.Ahrens@Sun.COM (void) execv(pypath, argv-1); 18259396SMatthew.Ahrens@Sun.COM 18269396SMatthew.Ahrens@Sun.COM (void) printf("internal error: %s not found\n" 18279396SMatthew.Ahrens@Sun.COM "falling back on built-in implementation, " 18289396SMatthew.Ahrens@Sun.COM "some features will not work\n", pypath); 18299396SMatthew.Ahrens@Sun.COM 18309396SMatthew.Ahrens@Sun.COM if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL) 18319396SMatthew.Ahrens@Sun.COM return (1); 18329396SMatthew.Ahrens@Sun.COM 18339396SMatthew.Ahrens@Sun.COM (void) printf("PROP TYPE NAME VALUE\n"); 18349396SMatthew.Ahrens@Sun.COM 18359396SMatthew.Ahrens@Sun.COM for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) { 18369396SMatthew.Ahrens@Sun.COM error = zfs_userspace(zhp, p, userspace_cb, &p); 18379396SMatthew.Ahrens@Sun.COM if (error) 18389396SMatthew.Ahrens@Sun.COM break; 18399396SMatthew.Ahrens@Sun.COM } 18409396SMatthew.Ahrens@Sun.COM return (error); 18419396SMatthew.Ahrens@Sun.COM } 18429396SMatthew.Ahrens@Sun.COM 18439396SMatthew.Ahrens@Sun.COM /* 18449365SChris.Gerhard@sun.com * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...] 18452379Ssjelinek * [-s property [-s property]...] [-S property [-S property]...] 18462379Ssjelinek * <dataset> ... 1847789Sahrens * 184811022STom.Erickson@Sun.COM * -r Recurse over all children 184911022STom.Erickson@Sun.COM * -d Limit recursion by depth. 185011022STom.Erickson@Sun.COM * -H Scripted mode; elide headers and separate columns by tabs 185111022STom.Erickson@Sun.COM * -o Control which fields to display. 185211022STom.Erickson@Sun.COM * -t Control which object types to display. 18532379Ssjelinek * -s Specify sort columns, descending order. 18542379Ssjelinek * -S Specify sort columns, ascending order. 1855789Sahrens * 1856789Sahrens * When given no arguments, lists all filesystems in the system. 1857789Sahrens * Otherwise, list the specified datasets, optionally recursing down them if 1858789Sahrens * '-r' is specified. 1859789Sahrens */ 1860789Sahrens typedef struct list_cbdata { 18612082Seschrock boolean_t cb_first; 18622082Seschrock boolean_t cb_scripted; 18635094Slling zprop_list_t *cb_proplist; 1864789Sahrens } list_cbdata_t; 1865789Sahrens 1866789Sahrens /* 1867789Sahrens * Given a list of columns to display, output appropriate headers for each one. 1868789Sahrens */ 1869789Sahrens static void 18705094Slling print_header(zprop_list_t *pl) 1871789Sahrens { 18722676Seschrock char headerbuf[ZFS_MAXPROPLEN]; 18732676Seschrock const char *header; 1874789Sahrens int i; 18752676Seschrock boolean_t first = B_TRUE; 18762676Seschrock boolean_t right_justify; 18772676Seschrock 18782676Seschrock for (; pl != NULL; pl = pl->pl_next) { 18792676Seschrock if (!first) { 1880789Sahrens (void) printf(" "); 18812676Seschrock } else { 18822676Seschrock first = B_FALSE; 18832676Seschrock } 18842676Seschrock 18852676Seschrock right_justify = B_FALSE; 18865094Slling if (pl->pl_prop != ZPROP_INVAL) { 18872676Seschrock header = zfs_prop_column_name(pl->pl_prop); 18882676Seschrock right_justify = zfs_prop_align_right(pl->pl_prop); 18892676Seschrock } else { 18902676Seschrock for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 18912676Seschrock headerbuf[i] = toupper(pl->pl_user_prop[i]); 18922676Seschrock headerbuf[i] = '\0'; 18932676Seschrock header = headerbuf; 18942676Seschrock } 18952676Seschrock 18962676Seschrock if (pl->pl_next == NULL && !right_justify) 18972676Seschrock (void) printf("%s", header); 18982676Seschrock else if (right_justify) 18992676Seschrock (void) printf("%*s", pl->pl_width, header); 19002676Seschrock else 19012676Seschrock (void) printf("%-*s", pl->pl_width, header); 1902789Sahrens } 1903789Sahrens 1904789Sahrens (void) printf("\n"); 1905789Sahrens } 1906789Sahrens 1907789Sahrens /* 1908789Sahrens * Given a dataset and a list of fields, print out all the properties according 1909789Sahrens * to the described layout. 1910789Sahrens */ 1911789Sahrens static void 19125094Slling print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted) 1913789Sahrens { 19142676Seschrock boolean_t first = B_TRUE; 1915789Sahrens char property[ZFS_MAXPROPLEN]; 19162676Seschrock nvlist_t *userprops = zfs_get_user_props(zhp); 19172676Seschrock nvlist_t *propval; 19182676Seschrock char *propstr; 19192676Seschrock boolean_t right_justify; 19202676Seschrock int width; 19212676Seschrock 19222676Seschrock for (; pl != NULL; pl = pl->pl_next) { 19232676Seschrock if (!first) { 1924789Sahrens if (scripted) 1925789Sahrens (void) printf("\t"); 1926789Sahrens else 1927789Sahrens (void) printf(" "); 19282676Seschrock } else { 19292676Seschrock first = B_FALSE; 1930789Sahrens } 1931789Sahrens 19325094Slling if (pl->pl_prop != ZPROP_INVAL) { 19332676Seschrock if (zfs_prop_get(zhp, pl->pl_prop, property, 19342676Seschrock sizeof (property), NULL, NULL, 0, B_FALSE) != 0) 19352676Seschrock propstr = "-"; 19362676Seschrock else 19372676Seschrock propstr = property; 19382676Seschrock 19392676Seschrock right_justify = zfs_prop_align_right(pl->pl_prop); 19409396SMatthew.Ahrens@Sun.COM } else if (zfs_prop_userquota(pl->pl_user_prop)) { 19419396SMatthew.Ahrens@Sun.COM if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 19429396SMatthew.Ahrens@Sun.COM property, sizeof (property), B_FALSE) != 0) 19439396SMatthew.Ahrens@Sun.COM propstr = "-"; 19449396SMatthew.Ahrens@Sun.COM else 19459396SMatthew.Ahrens@Sun.COM propstr = property; 19469396SMatthew.Ahrens@Sun.COM right_justify = B_TRUE; 19472676Seschrock } else { 19482676Seschrock if (nvlist_lookup_nvlist(userprops, 19492676Seschrock pl->pl_user_prop, &propval) != 0) 19502676Seschrock propstr = "-"; 19512676Seschrock else 19522676Seschrock verify(nvlist_lookup_string(propval, 19535094Slling ZPROP_VALUE, &propstr) == 0); 19549396SMatthew.Ahrens@Sun.COM right_justify = B_FALSE; 19552676Seschrock } 19562676Seschrock 19572676Seschrock width = pl->pl_width; 1958789Sahrens 1959866Seschrock /* 1960866Seschrock * If this is being called in scripted mode, or if this is the 1961866Seschrock * last column and it is left-justified, don't include a width 1962866Seschrock * format specifier. 1963866Seschrock */ 19642676Seschrock if (scripted || (pl->pl_next == NULL && !right_justify)) 19652676Seschrock (void) printf("%s", propstr); 19662676Seschrock else if (right_justify) 19672676Seschrock (void) printf("%*s", width, propstr); 19682676Seschrock else 19692676Seschrock (void) printf("%-*s", width, propstr); 1970789Sahrens } 1971789Sahrens 1972789Sahrens (void) printf("\n"); 1973789Sahrens } 1974789Sahrens 1975789Sahrens /* 1976789Sahrens * Generic callback function to list a dataset or snapshot. 1977789Sahrens */ 1978789Sahrens static int 1979789Sahrens list_callback(zfs_handle_t *zhp, void *data) 1980789Sahrens { 1981789Sahrens list_cbdata_t *cbp = data; 1982789Sahrens 1983789Sahrens if (cbp->cb_first) { 1984789Sahrens if (!cbp->cb_scripted) 19852676Seschrock print_header(cbp->cb_proplist); 19862082Seschrock cbp->cb_first = B_FALSE; 1987789Sahrens } 1988789Sahrens 19892676Seschrock print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted); 1990789Sahrens 1991789Sahrens return (0); 1992789Sahrens } 1993789Sahrens 1994789Sahrens static int 1995789Sahrens zfs_do_list(int argc, char **argv) 1996789Sahrens { 1997789Sahrens int c; 19982082Seschrock boolean_t scripted = B_FALSE; 1999789Sahrens static char default_fields[] = 2000789Sahrens "name,used,available,referenced,mountpoint"; 20018415SRichard.Morris@Sun.COM int types = ZFS_TYPE_DATASET; 20027538SRichard.Morris@Sun.COM boolean_t types_specified = B_FALSE; 2003789Sahrens char *fields = NULL; 2004789Sahrens list_cbdata_t cb = { 0 }; 2005789Sahrens char *value; 20069365SChris.Gerhard@sun.com int limit = 0; 2007789Sahrens int ret; 20082379Ssjelinek zfs_sort_column_t *sortcol = NULL; 20097538SRichard.Morris@Sun.COM int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS; 2010789Sahrens 2011789Sahrens /* check options */ 20129365SChris.Gerhard@sun.com while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) { 2013789Sahrens switch (c) { 2014789Sahrens case 'o': 2015789Sahrens fields = optarg; 2016789Sahrens break; 20179365SChris.Gerhard@sun.com case 'd': 20189365SChris.Gerhard@sun.com limit = parse_depth(optarg, &flags); 20199365SChris.Gerhard@sun.com break; 2020789Sahrens case 'r': 20217538SRichard.Morris@Sun.COM flags |= ZFS_ITER_RECURSE; 2022789Sahrens break; 2023789Sahrens case 'H': 20242082Seschrock scripted = B_TRUE; 2025789Sahrens break; 20262379Ssjelinek case 's': 20272676Seschrock if (zfs_add_sort_column(&sortcol, optarg, 20282676Seschrock B_FALSE) != 0) { 20292379Ssjelinek (void) fprintf(stderr, 20302379Ssjelinek gettext("invalid property '%s'\n"), optarg); 20312379Ssjelinek usage(B_FALSE); 20322379Ssjelinek } 20332379Ssjelinek break; 20342379Ssjelinek case 'S': 20352676Seschrock if (zfs_add_sort_column(&sortcol, optarg, 20362676Seschrock B_TRUE) != 0) { 20372379Ssjelinek (void) fprintf(stderr, 20382379Ssjelinek gettext("invalid property '%s'\n"), optarg); 20392379Ssjelinek usage(B_FALSE); 20402379Ssjelinek } 20412379Ssjelinek break; 2042789Sahrens case 't': 2043789Sahrens types = 0; 20447538SRichard.Morris@Sun.COM types_specified = B_TRUE; 20457538SRichard.Morris@Sun.COM flags &= ~ZFS_ITER_PROP_LISTSNAPS; 2046789Sahrens while (*optarg != '\0') { 20477538SRichard.Morris@Sun.COM static char *type_subopts[] = { "filesystem", 20487538SRichard.Morris@Sun.COM "volume", "snapshot", "all", NULL }; 20497538SRichard.Morris@Sun.COM 2050789Sahrens switch (getsubopt(&optarg, type_subopts, 2051789Sahrens &value)) { 2052789Sahrens case 0: 2053789Sahrens types |= ZFS_TYPE_FILESYSTEM; 2054789Sahrens break; 2055789Sahrens case 1: 2056789Sahrens types |= ZFS_TYPE_VOLUME; 2057789Sahrens break; 2058789Sahrens case 2: 2059789Sahrens types |= ZFS_TYPE_SNAPSHOT; 2060789Sahrens break; 20617538SRichard.Morris@Sun.COM case 3: 20627538SRichard.Morris@Sun.COM types = ZFS_TYPE_DATASET; 20637538SRichard.Morris@Sun.COM break; 20647538SRichard.Morris@Sun.COM 2065789Sahrens default: 2066789Sahrens (void) fprintf(stderr, 2067789Sahrens gettext("invalid type '%s'\n"), 2068789Sahrens value); 20692082Seschrock usage(B_FALSE); 2070789Sahrens } 2071789Sahrens } 2072789Sahrens break; 2073789Sahrens case ':': 2074789Sahrens (void) fprintf(stderr, gettext("missing argument for " 2075789Sahrens "'%c' option\n"), optopt); 20762082Seschrock usage(B_FALSE); 2077789Sahrens break; 2078789Sahrens case '?': 2079789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2080789Sahrens optopt); 20812082Seschrock usage(B_FALSE); 2082789Sahrens } 2083789Sahrens } 2084789Sahrens 2085789Sahrens argc -= optind; 2086789Sahrens argv += optind; 2087789Sahrens 2088789Sahrens if (fields == NULL) 20897390SMatthew.Ahrens@Sun.COM fields = default_fields; 20907390SMatthew.Ahrens@Sun.COM 20917390SMatthew.Ahrens@Sun.COM /* 20927538SRichard.Morris@Sun.COM * If "-o space" and no types were specified, don't display snapshots. 20937390SMatthew.Ahrens@Sun.COM */ 20947538SRichard.Morris@Sun.COM if (strcmp(fields, "space") == 0 && types_specified == B_FALSE) 20957390SMatthew.Ahrens@Sun.COM types &= ~ZFS_TYPE_SNAPSHOT; 2096789Sahrens 2097866Seschrock /* 20985094Slling * If the user specifies '-o all', the zprop_get_list() doesn't 2099866Seschrock * normally include the name of the dataset. For 'zfs list', we always 2100866Seschrock * want this property to be first. 2101866Seschrock */ 21025094Slling if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 21035094Slling != 0) 21042082Seschrock usage(B_FALSE); 21052676Seschrock 2106789Sahrens cb.cb_scripted = scripted; 21072082Seschrock cb.cb_first = B_TRUE; 2108789Sahrens 21097538SRichard.Morris@Sun.COM ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist, 21109365SChris.Gerhard@sun.com limit, list_callback, &cb); 21112379Ssjelinek 21125094Slling zprop_free_list(cb.cb_proplist); 21132379Ssjelinek zfs_free_sort_columns(sortcol); 2114789Sahrens 21154221Smmusante if (ret == 0 && cb.cb_first && !cb.cb_scripted) 2116789Sahrens (void) printf(gettext("no datasets available\n")); 2117789Sahrens 2118789Sahrens return (ret); 2119789Sahrens } 2120789Sahrens 2121789Sahrens /* 21224490Svb160487 * zfs rename <fs | snap | vol> <fs | snap | vol> 21234490Svb160487 * zfs rename -p <fs | vol> <fs | vol> 21244490Svb160487 * zfs rename -r <snap> <snap> 2125789Sahrens * 2126789Sahrens * Renames the given dataset to another of the same type. 21274490Svb160487 * 21284490Svb160487 * The '-p' flag creates all the non-existing ancestors of the target first. 2129789Sahrens */ 2130789Sahrens /* ARGSUSED */ 2131789Sahrens static int 2132789Sahrens zfs_do_rename(int argc, char **argv) 2133789Sahrens { 2134789Sahrens zfs_handle_t *zhp; 21354007Smmusante int c; 21362082Seschrock int ret; 21374490Svb160487 boolean_t recurse = B_FALSE; 21384490Svb160487 boolean_t parents = B_FALSE; 2139789Sahrens 2140789Sahrens /* check options */ 21414490Svb160487 while ((c = getopt(argc, argv, "pr")) != -1) { 21424007Smmusante switch (c) { 21434490Svb160487 case 'p': 21444490Svb160487 parents = B_TRUE; 21454490Svb160487 break; 21464007Smmusante case 'r': 21474490Svb160487 recurse = B_TRUE; 21484007Smmusante break; 21494007Smmusante case '?': 21504007Smmusante default: 21514007Smmusante (void) fprintf(stderr, gettext("invalid option '%c'\n"), 21524007Smmusante optopt); 21534007Smmusante usage(B_FALSE); 21544007Smmusante } 2155789Sahrens } 2156789Sahrens 21574007Smmusante argc -= optind; 21584007Smmusante argv += optind; 21594007Smmusante 2160789Sahrens /* check number of arguments */ 21614007Smmusante if (argc < 1) { 2162789Sahrens (void) fprintf(stderr, gettext("missing source dataset " 2163789Sahrens "argument\n")); 21642082Seschrock usage(B_FALSE); 2165789Sahrens } 21664007Smmusante if (argc < 2) { 2167789Sahrens (void) fprintf(stderr, gettext("missing target dataset " 2168789Sahrens "argument\n")); 21692082Seschrock usage(B_FALSE); 2170789Sahrens } 21714007Smmusante if (argc > 2) { 2172789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 21732082Seschrock usage(B_FALSE); 2174789Sahrens } 2175789Sahrens 21764490Svb160487 if (recurse && parents) { 21774490Svb160487 (void) fprintf(stderr, gettext("-p and -r options are mutually " 21784490Svb160487 "exclusive\n")); 21794490Svb160487 usage(B_FALSE); 21804490Svb160487 } 21814490Svb160487 21824007Smmusante if (recurse && strchr(argv[0], '@') == 0) { 21834007Smmusante (void) fprintf(stderr, gettext("source dataset for recursive " 21844007Smmusante "rename must be a snapshot\n")); 21854007Smmusante usage(B_FALSE); 21864007Smmusante } 21874007Smmusante 21884490Svb160487 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM | 21895094Slling ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL) 2190789Sahrens return (1); 2191789Sahrens 21924490Svb160487 /* If we were asked and the name looks good, try to create ancestors. */ 21934490Svb160487 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) && 21944490Svb160487 zfs_create_ancestors(g_zfs, argv[1]) != 0) { 21954490Svb160487 zfs_close(zhp); 21964490Svb160487 return (1); 21974490Svb160487 } 21984490Svb160487 21994007Smmusante ret = (zfs_rename(zhp, argv[1], recurse) != 0); 22002082Seschrock 22012082Seschrock zfs_close(zhp); 22022082Seschrock return (ret); 22032082Seschrock } 22042082Seschrock 22052082Seschrock /* 22062082Seschrock * zfs promote <fs> 22072082Seschrock * 22082082Seschrock * Promotes the given clone fs to be the parent 22092082Seschrock */ 22102082Seschrock /* ARGSUSED */ 22112082Seschrock static int 22122082Seschrock zfs_do_promote(int argc, char **argv) 22132082Seschrock { 22142082Seschrock zfs_handle_t *zhp; 22152082Seschrock int ret; 22162082Seschrock 22172082Seschrock /* check options */ 22182082Seschrock if (argc > 1 && argv[1][0] == '-') { 22192082Seschrock (void) fprintf(stderr, gettext("invalid option '%c'\n"), 22202082Seschrock argv[1][1]); 22212082Seschrock usage(B_FALSE); 22222082Seschrock } 22232082Seschrock 22242082Seschrock /* check number of arguments */ 22252082Seschrock if (argc < 2) { 22262082Seschrock (void) fprintf(stderr, gettext("missing clone filesystem" 22272597Snd150628 " argument\n")); 22282082Seschrock usage(B_FALSE); 22292082Seschrock } 22302082Seschrock if (argc > 2) { 22312082Seschrock (void) fprintf(stderr, gettext("too many arguments\n")); 22322082Seschrock usage(B_FALSE); 22332082Seschrock } 22342082Seschrock 22352082Seschrock zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 22362082Seschrock if (zhp == NULL) 22372082Seschrock return (1); 22382082Seschrock 22392082Seschrock ret = (zfs_promote(zhp) != 0); 22402082Seschrock 22412926Sek110237 2242789Sahrens zfs_close(zhp); 2243789Sahrens return (ret); 2244789Sahrens } 2245789Sahrens 2246789Sahrens /* 22475568Sahrens * zfs rollback [-rRf] <snapshot> 2248789Sahrens * 224911022STom.Erickson@Sun.COM * -r Delete any intervening snapshots before doing rollback 225011022STom.Erickson@Sun.COM * -R Delete any snapshots and their clones 225111022STom.Erickson@Sun.COM * -f ignored for backwards compatability 2252789Sahrens * 2253789Sahrens * Given a filesystem, rollback to a specific snapshot, discarding any changes 2254789Sahrens * since then and making it the active dataset. If more recent snapshots exist, 2255789Sahrens * the command will complain unless the '-r' flag is given. 2256789Sahrens */ 2257789Sahrens typedef struct rollback_cbdata { 2258789Sahrens uint64_t cb_create; 22592082Seschrock boolean_t cb_first; 2260789Sahrens int cb_doclones; 2261789Sahrens char *cb_target; 2262789Sahrens int cb_error; 22632082Seschrock boolean_t cb_recurse; 22642082Seschrock boolean_t cb_dependent; 2265789Sahrens } rollback_cbdata_t; 2266789Sahrens 2267789Sahrens /* 2268789Sahrens * Report any snapshots more recent than the one specified. Used when '-r' is 2269789Sahrens * not specified. We reuse this same callback for the snapshot dependents - if 2270789Sahrens * 'cb_dependent' is set, then this is a dependent and we should report it 2271789Sahrens * without checking the transaction group. 2272789Sahrens */ 2273789Sahrens static int 2274789Sahrens rollback_check(zfs_handle_t *zhp, void *data) 2275789Sahrens { 2276789Sahrens rollback_cbdata_t *cbp = data; 2277789Sahrens 22782082Seschrock if (cbp->cb_doclones) { 22792082Seschrock zfs_close(zhp); 2280789Sahrens return (0); 22812082Seschrock } 2282789Sahrens 2283789Sahrens if (!cbp->cb_dependent) { 2284789Sahrens if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 && 22851294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 2286789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 2287789Sahrens cbp->cb_create) { 2288789Sahrens 2289789Sahrens if (cbp->cb_first && !cbp->cb_recurse) { 2290789Sahrens (void) fprintf(stderr, gettext("cannot " 2291789Sahrens "rollback to '%s': more recent snapshots " 2292789Sahrens "exist\n"), 2293789Sahrens cbp->cb_target); 2294789Sahrens (void) fprintf(stderr, gettext("use '-r' to " 2295789Sahrens "force deletion of the following " 2296789Sahrens "snapshots:\n")); 2297789Sahrens cbp->cb_first = 0; 2298789Sahrens cbp->cb_error = 1; 2299789Sahrens } 2300789Sahrens 2301789Sahrens if (cbp->cb_recurse) { 23022082Seschrock cbp->cb_dependent = B_TRUE; 23032474Seschrock if (zfs_iter_dependents(zhp, B_TRUE, 23042474Seschrock rollback_check, cbp) != 0) { 23052474Seschrock zfs_close(zhp); 23062474Seschrock return (-1); 23072474Seschrock } 23082082Seschrock cbp->cb_dependent = B_FALSE; 2309789Sahrens } else { 2310789Sahrens (void) fprintf(stderr, "%s\n", 2311789Sahrens zfs_get_name(zhp)); 2312789Sahrens } 2313789Sahrens } 2314789Sahrens } else { 2315789Sahrens if (cbp->cb_first && cbp->cb_recurse) { 2316789Sahrens (void) fprintf(stderr, gettext("cannot rollback to " 2317789Sahrens "'%s': clones of previous snapshots exist\n"), 2318789Sahrens cbp->cb_target); 2319789Sahrens (void) fprintf(stderr, gettext("use '-R' to " 2320789Sahrens "force deletion of the following clones and " 2321789Sahrens "dependents:\n")); 2322789Sahrens cbp->cb_first = 0; 2323789Sahrens cbp->cb_error = 1; 2324789Sahrens } 2325789Sahrens 2326789Sahrens (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 2327789Sahrens } 2328789Sahrens 2329789Sahrens zfs_close(zhp); 2330789Sahrens return (0); 2331789Sahrens } 2332789Sahrens 2333789Sahrens static int 2334789Sahrens zfs_do_rollback(int argc, char **argv) 2335789Sahrens { 2336789Sahrens int ret; 2337789Sahrens int c; 23385749Sahrens boolean_t force = B_FALSE; 2339789Sahrens rollback_cbdata_t cb = { 0 }; 2340789Sahrens zfs_handle_t *zhp, *snap; 2341789Sahrens char parentname[ZFS_MAXNAMELEN]; 2342789Sahrens char *delim; 2343789Sahrens 2344789Sahrens /* check options */ 23455568Sahrens while ((c = getopt(argc, argv, "rRf")) != -1) { 2346789Sahrens switch (c) { 2347789Sahrens case 'r': 2348789Sahrens cb.cb_recurse = 1; 2349789Sahrens break; 2350789Sahrens case 'R': 2351789Sahrens cb.cb_recurse = 1; 2352789Sahrens cb.cb_doclones = 1; 2353789Sahrens break; 23545568Sahrens case 'f': 23555749Sahrens force = B_TRUE; 23565568Sahrens break; 2357789Sahrens case '?': 2358789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2359789Sahrens optopt); 23602082Seschrock usage(B_FALSE); 2361789Sahrens } 2362789Sahrens } 2363789Sahrens 2364789Sahrens argc -= optind; 2365789Sahrens argv += optind; 2366789Sahrens 2367789Sahrens /* check number of arguments */ 2368789Sahrens if (argc < 1) { 2369789Sahrens (void) fprintf(stderr, gettext("missing dataset argument\n")); 23702082Seschrock usage(B_FALSE); 2371789Sahrens } 2372789Sahrens if (argc > 1) { 2373789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 23742082Seschrock usage(B_FALSE); 2375789Sahrens } 2376789Sahrens 2377789Sahrens /* open the snapshot */ 23782082Seschrock if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 2379789Sahrens return (1); 2380789Sahrens 23811294Slling /* open the parent dataset */ 23821294Slling (void) strlcpy(parentname, argv[0], sizeof (parentname)); 2383789Sahrens verify((delim = strrchr(parentname, '@')) != NULL); 2384789Sahrens *delim = '\0'; 23855094Slling if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) { 2386789Sahrens zfs_close(snap); 2387789Sahrens return (1); 2388789Sahrens } 2389789Sahrens 2390789Sahrens /* 2391789Sahrens * Check for more recent snapshots and/or clones based on the presence 2392789Sahrens * of '-r' and '-R'. 2393789Sahrens */ 23941294Slling cb.cb_target = argv[0]; 23951294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 23962082Seschrock cb.cb_first = B_TRUE; 2397789Sahrens cb.cb_error = 0; 23982474Seschrock if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0) 23992474Seschrock goto out; 2400789Sahrens 2401789Sahrens if ((ret = cb.cb_error) != 0) 2402789Sahrens goto out; 2403789Sahrens 2404789Sahrens /* 24051294Slling * Rollback parent to the given snapshot. 2406789Sahrens */ 24075749Sahrens ret = zfs_rollback(zhp, snap, force); 2408789Sahrens 2409789Sahrens out: 2410789Sahrens zfs_close(snap); 2411789Sahrens zfs_close(zhp); 2412789Sahrens 2413789Sahrens if (ret == 0) 2414789Sahrens return (0); 2415789Sahrens else 2416789Sahrens return (1); 2417789Sahrens } 2418789Sahrens 2419789Sahrens /* 2420789Sahrens * zfs set property=value { fs | snap | vol } ... 2421789Sahrens * 2422789Sahrens * Sets the given property for all datasets specified on the command line. 2423789Sahrens */ 2424789Sahrens typedef struct set_cbdata { 2425789Sahrens char *cb_propname; 2426789Sahrens char *cb_value; 2427789Sahrens } set_cbdata_t; 2428789Sahrens 2429789Sahrens static int 2430789Sahrens set_callback(zfs_handle_t *zhp, void *data) 2431789Sahrens { 2432789Sahrens set_cbdata_t *cbp = data; 2433789Sahrens 24342676Seschrock if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) { 24352169Snd150628 switch (libzfs_errno(g_zfs)) { 24362169Snd150628 case EZFS_MOUNTFAILED: 24372169Snd150628 (void) fprintf(stderr, gettext("property may be set " 24382169Snd150628 "but unable to remount filesystem\n")); 24392169Snd150628 break; 24403126Sahl case EZFS_SHARENFSFAILED: 24412169Snd150628 (void) fprintf(stderr, gettext("property may be set " 24422169Snd150628 "but unable to reshare filesystem\n")); 24432169Snd150628 break; 24442169Snd150628 } 2445789Sahrens return (1); 24462169Snd150628 } 24472856Snd150628 return (0); 2448789Sahrens } 2449789Sahrens 2450789Sahrens static int 2451789Sahrens zfs_do_set(int argc, char **argv) 2452789Sahrens { 2453789Sahrens set_cbdata_t cb; 24542926Sek110237 int ret; 2455789Sahrens 2456789Sahrens /* check for options */ 2457789Sahrens if (argc > 1 && argv[1][0] == '-') { 2458789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2459789Sahrens argv[1][1]); 24602082Seschrock usage(B_FALSE); 2461789Sahrens } 2462789Sahrens 2463789Sahrens /* check number of arguments */ 2464789Sahrens if (argc < 2) { 2465789Sahrens (void) fprintf(stderr, gettext("missing property=value " 2466789Sahrens "argument\n")); 24672082Seschrock usage(B_FALSE); 2468789Sahrens } 2469789Sahrens if (argc < 3) { 2470789Sahrens (void) fprintf(stderr, gettext("missing dataset name\n")); 24712082Seschrock usage(B_FALSE); 2472789Sahrens } 2473789Sahrens 2474789Sahrens /* validate property=value argument */ 2475789Sahrens cb.cb_propname = argv[1]; 24766993Ssg201626 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) || 24776993Ssg201626 (cb.cb_value[1] == '\0')) { 2478789Sahrens (void) fprintf(stderr, gettext("missing value in " 2479789Sahrens "property=value argument\n")); 24802082Seschrock usage(B_FALSE); 2481789Sahrens } 2482789Sahrens 2483789Sahrens *cb.cb_value = '\0'; 2484789Sahrens cb.cb_value++; 2485789Sahrens 2486789Sahrens if (*cb.cb_propname == '\0') { 2487789Sahrens (void) fprintf(stderr, 2488789Sahrens gettext("missing property in property=value argument\n")); 24892082Seschrock usage(B_FALSE); 2490789Sahrens } 2491789Sahrens 24927538SRichard.Morris@Sun.COM ret = zfs_for_each(argc - 2, argv + 2, NULL, 24939365SChris.Gerhard@sun.com ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb); 24942926Sek110237 24952926Sek110237 return (ret); 2496789Sahrens } 2497789Sahrens 2498789Sahrens /* 24997265Sahrens * zfs snapshot [-r] [-o prop=value] ... <fs@snap> 2500789Sahrens * 2501789Sahrens * Creates a snapshot with the given name. While functionally equivalent to 25025331Samw * 'zfs create', it is a separate command to differentiate intent. 2503789Sahrens */ 2504789Sahrens static int 2505789Sahrens zfs_do_snapshot(int argc, char **argv) 2506789Sahrens { 25074490Svb160487 boolean_t recursive = B_FALSE; 25082199Sahrens int ret; 25092199Sahrens char c; 25107265Sahrens nvlist_t *props; 25117265Sahrens 25127265Sahrens if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 25137265Sahrens (void) fprintf(stderr, gettext("internal error: " 25147265Sahrens "out of memory\n")); 25157265Sahrens return (1); 25167265Sahrens } 25172199Sahrens 2518789Sahrens /* check options */ 25197265Sahrens while ((c = getopt(argc, argv, "ro:")) != -1) { 25202199Sahrens switch (c) { 25217265Sahrens case 'o': 25227265Sahrens if (parseprop(props)) 25237265Sahrens return (1); 25247265Sahrens break; 25252199Sahrens case 'r': 25262199Sahrens recursive = B_TRUE; 25272199Sahrens break; 25282199Sahrens case '?': 25292199Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 25302199Sahrens optopt); 25317265Sahrens goto usage; 25322199Sahrens } 2533789Sahrens } 2534789Sahrens 25352199Sahrens argc -= optind; 25362199Sahrens argv += optind; 25372199Sahrens 2538789Sahrens /* check number of arguments */ 25392199Sahrens if (argc < 1) { 2540789Sahrens (void) fprintf(stderr, gettext("missing snapshot argument\n")); 25417265Sahrens goto usage; 2542789Sahrens } 25432199Sahrens if (argc > 1) { 2544789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 25457265Sahrens goto usage; 2546789Sahrens } 2547789Sahrens 25487265Sahrens ret = zfs_snapshot(g_zfs, argv[0], recursive, props); 25497265Sahrens nvlist_free(props); 25502199Sahrens if (ret && recursive) 25512199Sahrens (void) fprintf(stderr, gettext("no snapshots were created\n")); 25522199Sahrens return (ret != 0); 25537265Sahrens 25547265Sahrens usage: 25557265Sahrens nvlist_free(props); 25567265Sahrens usage(B_FALSE); 25577265Sahrens return (-1); 2558789Sahrens } 2559789Sahrens 2560789Sahrens /* 256111022STom.Erickson@Sun.COM * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap> 256211022STom.Erickson@Sun.COM * zfs send [-vDp] [-i|-I <@snap>] <fs@snap> 2563789Sahrens * 2564789Sahrens * Send a backup stream to stdout. 2565789Sahrens */ 2566789Sahrens static int 25671749Sahrens zfs_do_send(int argc, char **argv) 2568789Sahrens { 2569789Sahrens char *fromname = NULL; 25705367Sahrens char *toname = NULL; 25712885Sahrens char *cp; 25722885Sahrens zfs_handle_t *zhp; 257311007SLori.Alt@Sun.COM sendflags_t flags = { 0 }; 2574789Sahrens int c, err; 2575*12296SLin.Ling@Sun.COM nvlist_t *dbgnv; 2576*12296SLin.Ling@Sun.COM boolean_t extraverbose = B_FALSE; 2577789Sahrens 2578789Sahrens /* check options */ 257911022STom.Erickson@Sun.COM while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) { 2580789Sahrens switch (c) { 2581789Sahrens case 'i': 25822885Sahrens if (fromname) 25832885Sahrens usage(B_FALSE); 2584789Sahrens fromname = optarg; 2585789Sahrens break; 25865367Sahrens case 'I': 25875367Sahrens if (fromname) 25885367Sahrens usage(B_FALSE); 25895367Sahrens fromname = optarg; 259011007SLori.Alt@Sun.COM flags.doall = B_TRUE; 25915367Sahrens break; 25925367Sahrens case 'R': 259311007SLori.Alt@Sun.COM flags.replicate = B_TRUE; 25945367Sahrens break; 259511022STom.Erickson@Sun.COM case 'p': 259611022STom.Erickson@Sun.COM flags.props = B_TRUE; 259711022STom.Erickson@Sun.COM break; 25985367Sahrens case 'v': 2599*12296SLin.Ling@Sun.COM if (flags.verbose) 2600*12296SLin.Ling@Sun.COM extraverbose = B_TRUE; 260111007SLori.Alt@Sun.COM flags.verbose = B_TRUE; 260211007SLori.Alt@Sun.COM break; 260311007SLori.Alt@Sun.COM case 'D': 260411007SLori.Alt@Sun.COM flags.dedup = B_TRUE; 26055367Sahrens break; 2606789Sahrens case ':': 2607789Sahrens (void) fprintf(stderr, gettext("missing argument for " 2608789Sahrens "'%c' option\n"), optopt); 26092082Seschrock usage(B_FALSE); 2610789Sahrens break; 2611789Sahrens case '?': 2612789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2613789Sahrens optopt); 26142082Seschrock usage(B_FALSE); 2615789Sahrens } 2616789Sahrens } 2617789Sahrens 2618789Sahrens argc -= optind; 2619789Sahrens argv += optind; 2620789Sahrens 2621789Sahrens /* check number of arguments */ 2622789Sahrens if (argc < 1) { 2623789Sahrens (void) fprintf(stderr, gettext("missing snapshot argument\n")); 26242082Seschrock usage(B_FALSE); 2625789Sahrens } 2626789Sahrens if (argc > 1) { 2627789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 26282082Seschrock usage(B_FALSE); 2629789Sahrens } 2630789Sahrens 2631789Sahrens if (isatty(STDOUT_FILENO)) { 2632789Sahrens (void) fprintf(stderr, 26332885Sahrens gettext("Error: Stream can not be written to a terminal.\n" 26343912Slling "You must redirect standard output.\n")); 2635789Sahrens return (1); 2636789Sahrens } 2637789Sahrens 26385367Sahrens cp = strchr(argv[0], '@'); 26395367Sahrens if (cp == NULL) { 26405367Sahrens (void) fprintf(stderr, 26415367Sahrens gettext("argument must be a snapshot\n")); 26425367Sahrens usage(B_FALSE); 26435367Sahrens } 26445367Sahrens *cp = '\0'; 26455367Sahrens toname = cp + 1; 26465367Sahrens zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 26475367Sahrens if (zhp == NULL) 26482665Snd150628 return (1); 26492665Snd150628 26502885Sahrens /* 26512885Sahrens * If they specified the full path to the snapshot, chop off 26525367Sahrens * everything except the short name of the snapshot, but special 26535367Sahrens * case if they specify the origin. 26542885Sahrens */ 26552885Sahrens if (fromname && (cp = strchr(fromname, '@')) != NULL) { 26565367Sahrens char origin[ZFS_MAXNAMELEN]; 26575367Sahrens zprop_source_t src; 26585367Sahrens 26595367Sahrens (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN, 26605367Sahrens origin, sizeof (origin), &src, NULL, 0, B_FALSE); 26615367Sahrens 26625367Sahrens if (strcmp(origin, fromname) == 0) { 26635367Sahrens fromname = NULL; 266411007SLori.Alt@Sun.COM flags.fromorigin = B_TRUE; 26655367Sahrens } else { 26665367Sahrens *cp = '\0'; 26675367Sahrens if (cp != fromname && strcmp(argv[0], fromname)) { 26685367Sahrens (void) fprintf(stderr, 26695367Sahrens gettext("incremental source must be " 26705367Sahrens "in same filesystem\n")); 26715367Sahrens usage(B_FALSE); 26725367Sahrens } 26735367Sahrens fromname = cp + 1; 26745367Sahrens if (strchr(fromname, '@') || strchr(fromname, '/')) { 26755367Sahrens (void) fprintf(stderr, 26765367Sahrens gettext("invalid incremental source\n")); 26775367Sahrens usage(B_FALSE); 26785367Sahrens } 26792885Sahrens } 2680789Sahrens } 2681789Sahrens 268211007SLori.Alt@Sun.COM if (flags.replicate && fromname == NULL) 268311007SLori.Alt@Sun.COM flags.doall = B_TRUE; 268411007SLori.Alt@Sun.COM 2685*12296SLin.Ling@Sun.COM err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0, 2686*12296SLin.Ling@Sun.COM extraverbose ? &dbgnv : NULL); 2687*12296SLin.Ling@Sun.COM 2688*12296SLin.Ling@Sun.COM if (extraverbose) { 2689*12296SLin.Ling@Sun.COM /* 2690*12296SLin.Ling@Sun.COM * dump_nvlist prints to stdout, but that's been 2691*12296SLin.Ling@Sun.COM * redirected to a file. Make it print to stderr 2692*12296SLin.Ling@Sun.COM * instead. 2693*12296SLin.Ling@Sun.COM */ 2694*12296SLin.Ling@Sun.COM (void) dup2(STDERR_FILENO, STDOUT_FILENO); 2695*12296SLin.Ling@Sun.COM dump_nvlist(dbgnv, 0); 2696*12296SLin.Ling@Sun.COM nvlist_free(dbgnv); 2697*12296SLin.Ling@Sun.COM } 26982885Sahrens zfs_close(zhp); 2699789Sahrens 2700789Sahrens return (err != 0); 2701789Sahrens } 2702789Sahrens 2703789Sahrens /* 270411461STom.Erickson@Sun.COM * zfs receive [-denvF] <fs@snap> 2705789Sahrens * 2706789Sahrens * Restore a backup stream from stdin. 2707789Sahrens */ 2708789Sahrens static int 27091749Sahrens zfs_do_receive(int argc, char **argv) 2710789Sahrens { 2711789Sahrens int c, err; 271211022STom.Erickson@Sun.COM recvflags_t flags = { 0 }; 271311022STom.Erickson@Sun.COM 2714789Sahrens /* check options */ 271511461STom.Erickson@Sun.COM while ((c = getopt(argc, argv, ":denuvF")) != -1) { 2716789Sahrens switch (c) { 2717789Sahrens case 'd': 27185367Sahrens flags.isprefix = B_TRUE; 2719789Sahrens break; 272011461STom.Erickson@Sun.COM case 'e': 272111461STom.Erickson@Sun.COM flags.isprefix = B_TRUE; 272211461STom.Erickson@Sun.COM flags.istail = B_TRUE; 272311461STom.Erickson@Sun.COM break; 2724789Sahrens case 'n': 27255367Sahrens flags.dryrun = B_TRUE; 2726789Sahrens break; 27278584SLori.Alt@Sun.COM case 'u': 27288584SLori.Alt@Sun.COM flags.nomount = B_TRUE; 27298584SLori.Alt@Sun.COM break; 2730789Sahrens case 'v': 27315367Sahrens flags.verbose = B_TRUE; 2732789Sahrens break; 27332665Snd150628 case 'F': 27345367Sahrens flags.force = B_TRUE; 27352665Snd150628 break; 2736789Sahrens case ':': 2737789Sahrens (void) fprintf(stderr, gettext("missing argument for " 2738789Sahrens "'%c' option\n"), optopt); 27392082Seschrock usage(B_FALSE); 2740789Sahrens break; 2741789Sahrens case '?': 2742789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2743789Sahrens optopt); 27442082Seschrock usage(B_FALSE); 2745789Sahrens } 2746789Sahrens } 2747789Sahrens 2748789Sahrens argc -= optind; 2749789Sahrens argv += optind; 2750789Sahrens 2751789Sahrens /* check number of arguments */ 2752789Sahrens if (argc < 1) { 2753789Sahrens (void) fprintf(stderr, gettext("missing snapshot argument\n")); 27542082Seschrock usage(B_FALSE); 2755789Sahrens } 2756789Sahrens if (argc > 1) { 2757789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 27582082Seschrock usage(B_FALSE); 2759789Sahrens } 2760789Sahrens 2761789Sahrens if (isatty(STDIN_FILENO)) { 2762789Sahrens (void) fprintf(stderr, 2763789Sahrens gettext("Error: Backup stream can not be read " 27643912Slling "from a terminal.\n" 27653912Slling "You must redirect standard input.\n")); 2766789Sahrens return (1); 2767789Sahrens } 2768789Sahrens 27695367Sahrens err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL); 27702926Sek110237 27714543Smarks return (err != 0); 27724543Smarks } 27734543Smarks 277410242Schris.kirby@sun.com static int 277510242Schris.kirby@sun.com zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding) 277610242Schris.kirby@sun.com { 277710242Schris.kirby@sun.com int errors = 0; 277810242Schris.kirby@sun.com int i; 277910242Schris.kirby@sun.com const char *tag; 278010242Schris.kirby@sun.com boolean_t recursive = B_FALSE; 278110342Schris.kirby@sun.com boolean_t temphold = B_FALSE; 278210342Schris.kirby@sun.com const char *opts = holding ? "rt" : "r"; 278310242Schris.kirby@sun.com int c; 278410242Schris.kirby@sun.com 278510242Schris.kirby@sun.com /* check options */ 278610342Schris.kirby@sun.com while ((c = getopt(argc, argv, opts)) != -1) { 278710242Schris.kirby@sun.com switch (c) { 278810242Schris.kirby@sun.com case 'r': 278910242Schris.kirby@sun.com recursive = B_TRUE; 279010242Schris.kirby@sun.com break; 279110342Schris.kirby@sun.com case 't': 279210342Schris.kirby@sun.com temphold = B_TRUE; 279310342Schris.kirby@sun.com break; 279410242Schris.kirby@sun.com case '?': 279510242Schris.kirby@sun.com (void) fprintf(stderr, gettext("invalid option '%c'\n"), 279610242Schris.kirby@sun.com optopt); 279710242Schris.kirby@sun.com usage(B_FALSE); 279810242Schris.kirby@sun.com } 279910242Schris.kirby@sun.com } 280010242Schris.kirby@sun.com 280110242Schris.kirby@sun.com argc -= optind; 280210242Schris.kirby@sun.com argv += optind; 280310242Schris.kirby@sun.com 280410242Schris.kirby@sun.com /* check number of arguments */ 280510242Schris.kirby@sun.com if (argc < 2) 280610242Schris.kirby@sun.com usage(B_FALSE); 280710242Schris.kirby@sun.com 280810242Schris.kirby@sun.com tag = argv[0]; 280910242Schris.kirby@sun.com --argc; 281010242Schris.kirby@sun.com ++argv; 281110242Schris.kirby@sun.com 281210342Schris.kirby@sun.com if (holding && tag[0] == '.') { 281310342Schris.kirby@sun.com /* tags starting with '.' are reserved for libzfs */ 281410342Schris.kirby@sun.com (void) fprintf(stderr, gettext("tag may not start with '.'\n")); 281510342Schris.kirby@sun.com usage(B_FALSE); 281610242Schris.kirby@sun.com } 281710242Schris.kirby@sun.com 281810242Schris.kirby@sun.com for (i = 0; i < argc; ++i) { 281910242Schris.kirby@sun.com zfs_handle_t *zhp; 282010242Schris.kirby@sun.com char parent[ZFS_MAXNAMELEN]; 282110242Schris.kirby@sun.com const char *delim; 282210242Schris.kirby@sun.com char *path = argv[i]; 282310242Schris.kirby@sun.com 282410242Schris.kirby@sun.com delim = strchr(path, '@'); 282510242Schris.kirby@sun.com if (delim == NULL) { 282610242Schris.kirby@sun.com (void) fprintf(stderr, 282710242Schris.kirby@sun.com gettext("'%s' is not a snapshot\n"), path); 282810242Schris.kirby@sun.com ++errors; 282910242Schris.kirby@sun.com continue; 283010242Schris.kirby@sun.com } 283110242Schris.kirby@sun.com (void) strncpy(parent, path, delim - path); 283210242Schris.kirby@sun.com parent[delim - path] = '\0'; 283310242Schris.kirby@sun.com 283410242Schris.kirby@sun.com zhp = zfs_open(g_zfs, parent, 283510242Schris.kirby@sun.com ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 283610242Schris.kirby@sun.com if (zhp == NULL) { 283710242Schris.kirby@sun.com ++errors; 283810242Schris.kirby@sun.com continue; 283910242Schris.kirby@sun.com } 284010342Schris.kirby@sun.com if (holding) { 284110342Schris.kirby@sun.com if (zfs_hold(zhp, delim+1, tag, recursive, 284211417SChris.Kirby@sun.com temphold, B_FALSE) != 0) 284310342Schris.kirby@sun.com ++errors; 284410342Schris.kirby@sun.com } else { 284510342Schris.kirby@sun.com if (zfs_release(zhp, delim+1, tag, recursive) != 0) 284610342Schris.kirby@sun.com ++errors; 284710342Schris.kirby@sun.com } 284810242Schris.kirby@sun.com zfs_close(zhp); 284910242Schris.kirby@sun.com } 285010242Schris.kirby@sun.com 285110242Schris.kirby@sun.com return (errors != 0); 285210242Schris.kirby@sun.com } 285310242Schris.kirby@sun.com 285410242Schris.kirby@sun.com /* 285510342Schris.kirby@sun.com * zfs hold [-r] [-t] <tag> <snap> ... 285610242Schris.kirby@sun.com * 285711022STom.Erickson@Sun.COM * -r Recursively hold 285810342Schris.kirby@sun.com * -t Temporary hold (hidden option) 285910242Schris.kirby@sun.com * 286010242Schris.kirby@sun.com * Apply a user-hold with the given tag to the list of snapshots. 286110242Schris.kirby@sun.com */ 286210242Schris.kirby@sun.com static int 286310242Schris.kirby@sun.com zfs_do_hold(int argc, char **argv) 286410242Schris.kirby@sun.com { 286510242Schris.kirby@sun.com return (zfs_do_hold_rele_impl(argc, argv, B_TRUE)); 286610242Schris.kirby@sun.com } 286710242Schris.kirby@sun.com 286810242Schris.kirby@sun.com /* 286910242Schris.kirby@sun.com * zfs release [-r] <tag> <snap> ... 287010242Schris.kirby@sun.com * 287111022STom.Erickson@Sun.COM * -r Recursively release 287210242Schris.kirby@sun.com * 287310242Schris.kirby@sun.com * Release a user-hold with the given tag from the list of snapshots. 287410242Schris.kirby@sun.com */ 287510242Schris.kirby@sun.com static int 287610242Schris.kirby@sun.com zfs_do_release(int argc, char **argv) 287710242Schris.kirby@sun.com { 287810242Schris.kirby@sun.com return (zfs_do_hold_rele_impl(argc, argv, B_FALSE)); 287910242Schris.kirby@sun.com } 288010242Schris.kirby@sun.com 28811356Seschrock typedef struct get_all_cbdata { 28821356Seschrock zfs_handle_t **cb_handles; 28831356Seschrock size_t cb_alloc; 28841356Seschrock size_t cb_used; 28853126Sahl uint_t cb_types; 28864737Smmusante boolean_t cb_verbose; 28871356Seschrock } get_all_cbdata_t; 28881356Seschrock 28894737Smmusante #define CHECK_SPINNER 30 28904737Smmusante #define SPINNER_TIME 3 /* seconds */ 28914737Smmusante #define MOUNT_TIME 5 /* seconds */ 28924737Smmusante 28931356Seschrock static int 28943126Sahl get_one_dataset(zfs_handle_t *zhp, void *data) 28951356Seschrock { 28964737Smmusante static char spin[] = { '-', '\\', '|', '/' }; 28974737Smmusante static int spinval = 0; 28984737Smmusante static int spincheck = 0; 28994737Smmusante static time_t last_spin_time = (time_t)0; 29001356Seschrock get_all_cbdata_t *cbp = data; 29013126Sahl zfs_type_t type = zfs_get_type(zhp); 29021356Seschrock 29034737Smmusante if (cbp->cb_verbose) { 29044737Smmusante if (--spincheck < 0) { 29054737Smmusante time_t now = time(NULL); 29064737Smmusante if (last_spin_time + SPINNER_TIME < now) { 29074737Smmusante (void) printf("\b%c", spin[spinval++ % 4]); 29084737Smmusante (void) fflush(stdout); 29094737Smmusante last_spin_time = now; 29104737Smmusante } 29114737Smmusante spincheck = CHECK_SPINNER; 29124737Smmusante } 29134737Smmusante } 29144737Smmusante 29151356Seschrock /* 29163126Sahl * Interate over any nested datasets. 29171356Seschrock */ 29183126Sahl if (type == ZFS_TYPE_FILESYSTEM && 29193126Sahl zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) { 29203265Sahrens zfs_close(zhp); 29213126Sahl return (1); 29223126Sahl } 29233126Sahl 29243126Sahl /* 29253126Sahl * Skip any datasets whose type does not match. 29263126Sahl */ 29273126Sahl if ((type & cbp->cb_types) == 0) { 29281356Seschrock zfs_close(zhp); 29291356Seschrock return (0); 29301356Seschrock } 29311356Seschrock 29321356Seschrock if (cbp->cb_alloc == cbp->cb_used) { 29331356Seschrock zfs_handle_t **handles; 29341356Seschrock 29351356Seschrock if (cbp->cb_alloc == 0) 29361356Seschrock cbp->cb_alloc = 64; 29371356Seschrock else 29381356Seschrock cbp->cb_alloc *= 2; 29391356Seschrock 29401356Seschrock handles = safe_malloc(cbp->cb_alloc * sizeof (void *)); 29411356Seschrock 29421356Seschrock if (cbp->cb_handles) { 29431356Seschrock bcopy(cbp->cb_handles, handles, 29441356Seschrock cbp->cb_used * sizeof (void *)); 29451356Seschrock free(cbp->cb_handles); 29461356Seschrock } 29471356Seschrock 29481356Seschrock cbp->cb_handles = handles; 29491356Seschrock } 29501356Seschrock 29511356Seschrock cbp->cb_handles[cbp->cb_used++] = zhp; 29521356Seschrock 29533126Sahl return (0); 29541356Seschrock } 29551356Seschrock 29561356Seschrock static void 29574737Smmusante get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count, 29584737Smmusante boolean_t verbose) 29591356Seschrock { 29601356Seschrock get_all_cbdata_t cb = { 0 }; 29613126Sahl cb.cb_types = types; 29624737Smmusante cb.cb_verbose = verbose; 29634737Smmusante 29644737Smmusante if (verbose) { 29654737Smmusante (void) printf("%s: *", gettext("Reading ZFS config")); 29664737Smmusante (void) fflush(stdout); 29674737Smmusante } 29683126Sahl 29693126Sahl (void) zfs_iter_root(g_zfs, get_one_dataset, &cb); 29703126Sahl 29713126Sahl *dslist = cb.cb_handles; 29721356Seschrock *count = cb.cb_used; 29734737Smmusante 29744737Smmusante if (verbose) { 29754737Smmusante (void) printf("\b%s\n", gettext("done.")); 29764737Smmusante } 29771356Seschrock } 29781356Seschrock 29791356Seschrock static int 29803126Sahl dataset_cmp(const void *a, const void *b) 29811356Seschrock { 29821356Seschrock zfs_handle_t **za = (zfs_handle_t **)a; 29831356Seschrock zfs_handle_t **zb = (zfs_handle_t **)b; 29841356Seschrock char mounta[MAXPATHLEN]; 29851356Seschrock char mountb[MAXPATHLEN]; 29863126Sahl boolean_t gota, gotb; 29873126Sahl 29883126Sahl if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 29893126Sahl verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 29903126Sahl sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 29913126Sahl if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 29923126Sahl verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 29933126Sahl sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 29943126Sahl 29953126Sahl if (gota && gotb) 29963126Sahl return (strcmp(mounta, mountb)); 29973126Sahl 29983126Sahl if (gota) 29993126Sahl return (-1); 30003126Sahl if (gotb) 30013126Sahl return (1); 30023126Sahl 30033126Sahl return (strcmp(zfs_get_name(a), zfs_get_name(b))); 30041356Seschrock } 3005789Sahrens 3006789Sahrens /* 3007789Sahrens * Generic callback for sharing or mounting filesystems. Because the code is so 3008789Sahrens * similar, we have a common function with an extra parameter to determine which 3009789Sahrens * mode we are using. 3010789Sahrens */ 3011789Sahrens #define OP_SHARE 0x1 3012789Sahrens #define OP_MOUNT 0x2 3013789Sahrens 3014789Sahrens /* 30153126Sahl * Share or mount a dataset. 3016789Sahrens */ 3017789Sahrens static int 30185331Samw share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, 30195331Samw boolean_t explicit, const char *options) 3020789Sahrens { 3021789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 3022789Sahrens char shareopts[ZFS_MAXPROPLEN]; 30235331Samw char smbshareopts[ZFS_MAXPROPLEN]; 30243126Sahl const char *cmdname = op == OP_SHARE ? "share" : "mount"; 3025789Sahrens struct mnttab mnt; 30262676Seschrock uint64_t zoned, canmount; 30273126Sahl zfs_type_t type = zfs_get_type(zhp); 30285331Samw boolean_t shared_nfs, shared_smb; 30293126Sahl 30303126Sahl assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)); 30313126Sahl 30323126Sahl if (type == ZFS_TYPE_FILESYSTEM) { 30333126Sahl /* 30343126Sahl * Check to make sure we can mount/share this dataset. If we 30353126Sahl * are in the global zone and the filesystem is exported to a 30363126Sahl * local zone, or if we are in a local zone and the 30373126Sahl * filesystem is not exported, then it is an error. 30383126Sahl */ 30393126Sahl zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 30403126Sahl 30413126Sahl if (zoned && getzoneid() == GLOBAL_ZONEID) { 30423126Sahl if (!explicit) 30433126Sahl return (0); 30443126Sahl 30453126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': " 30463126Sahl "dataset is exported to a local zone\n"), cmdname, 30473126Sahl zfs_get_name(zhp)); 30483126Sahl return (1); 30493126Sahl 30503126Sahl } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 30513126Sahl if (!explicit) 30523126Sahl return (0); 30533126Sahl 30543126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': " 30553126Sahl "permission denied\n"), cmdname, 30563126Sahl zfs_get_name(zhp)); 30573126Sahl return (1); 30583126Sahl } 30593126Sahl 30603126Sahl /* 30613126Sahl * Ignore any filesystems which don't apply to us. This 30623126Sahl * includes those with a legacy mountpoint, or those with 30633126Sahl * legacy share options. 30643126Sahl */ 30653126Sahl verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 30663126Sahl sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 30673126Sahl verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 30683126Sahl sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 30695331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts, 30705331Samw sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0); 30713126Sahl 30725331Samw if (op == OP_SHARE && strcmp(shareopts, "off") == 0 && 30735331Samw strcmp(smbshareopts, "off") == 0) { 30743126Sahl if (!explicit) 3075789Sahrens return (0); 3076789Sahrens 3077789Sahrens (void) fprintf(stderr, gettext("cannot share '%s': " 3078789Sahrens "legacy share\n"), zfs_get_name(zhp)); 3079789Sahrens (void) fprintf(stderr, gettext("use share(1M) to " 30808269SMark.Musante@Sun.COM "share this filesystem, or set " 30818269SMark.Musante@Sun.COM "sharenfs property on\n")); 3082789Sahrens return (1); 3083789Sahrens } 30843126Sahl 30853126Sahl /* 30863126Sahl * We cannot share or mount legacy filesystems. If the 30873126Sahl * shareopts is non-legacy but the mountpoint is legacy, we 30883126Sahl * treat it as a legacy share. 30893126Sahl */ 30903126Sahl if (strcmp(mountpoint, "legacy") == 0) { 30913126Sahl if (!explicit) 30923126Sahl return (0); 30933126Sahl 30943126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': " 30953126Sahl "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 30965367Sahrens (void) fprintf(stderr, gettext("use %s(1M) to " 30975367Sahrens "%s this filesystem\n"), cmdname, cmdname); 30983126Sahl return (1); 30993126Sahl } 31003126Sahl 31013126Sahl if (strcmp(mountpoint, "none") == 0) { 31023126Sahl if (!explicit) 31033126Sahl return (0); 31043126Sahl 31053126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': no " 31063126Sahl "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 31073126Sahl return (1); 31083126Sahl } 31093126Sahl 31106168Shs24103 /* 31116168Shs24103 * canmount explicit outcome 31126168Shs24103 * on no pass through 31136168Shs24103 * on yes pass through 31146168Shs24103 * off no return 0 31156168Shs24103 * off yes display error, return 1 31166168Shs24103 * noauto no return 0 31176168Shs24103 * noauto yes pass through 31186168Shs24103 */ 31198269SMark.Musante@Sun.COM canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 31206168Shs24103 if (canmount == ZFS_CANMOUNT_OFF) { 31213126Sahl if (!explicit) 31223126Sahl return (0); 31233126Sahl 31243126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': " 31253126Sahl "'canmount' property is set to 'off'\n"), cmdname, 31263126Sahl zfs_get_name(zhp)); 31273126Sahl return (1); 31286168Shs24103 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) { 31296168Shs24103 return (0); 31303126Sahl } 31313126Sahl 31323126Sahl /* 31333126Sahl * At this point, we have verified that the mountpoint and/or 31343126Sahl * shareopts are appropriate for auto management. If the 31353126Sahl * filesystem is already mounted or shared, return (failing 31363126Sahl * for explicit requests); otherwise mount or share the 31373126Sahl * filesystem. 31383126Sahl */ 31393126Sahl switch (op) { 31403126Sahl case OP_SHARE: 31415331Samw 31425331Samw shared_nfs = zfs_is_shared_nfs(zhp, NULL); 31435331Samw shared_smb = zfs_is_shared_smb(zhp, NULL); 31445331Samw 31455331Samw if (shared_nfs && shared_smb || 31465331Samw (shared_nfs && strcmp(shareopts, "on") == 0 && 31475331Samw strcmp(smbshareopts, "off") == 0) || 31485331Samw (shared_smb && strcmp(smbshareopts, "on") == 0 && 31495331Samw strcmp(shareopts, "off") == 0)) { 31503126Sahl if (!explicit) 31513126Sahl return (0); 31523126Sahl 3153789Sahrens (void) fprintf(stderr, gettext("cannot share " 3154789Sahrens "'%s': filesystem already shared\n"), 3155789Sahrens zfs_get_name(zhp)); 3156789Sahrens return (1); 3157789Sahrens } 31583126Sahl 31593126Sahl if (!zfs_is_mounted(zhp, NULL) && 31603126Sahl zfs_mount(zhp, NULL, 0) != 0) 31613126Sahl return (1); 31623126Sahl 31635331Samw if (protocol == NULL) { 31645331Samw if (zfs_shareall(zhp) != 0) 31655331Samw return (1); 31665331Samw } else if (strcmp(protocol, "nfs") == 0) { 31675331Samw if (zfs_share_nfs(zhp)) 31685331Samw return (1); 31695331Samw } else if (strcmp(protocol, "smb") == 0) { 31705331Samw if (zfs_share_smb(zhp)) 31715331Samw return (1); 31725331Samw } else { 31735331Samw (void) fprintf(stderr, gettext("cannot share " 31745331Samw "'%s': invalid share type '%s' " 31755331Samw "specified\n"), 31765331Samw zfs_get_name(zhp), protocol); 31773126Sahl return (1); 31785331Samw } 31795331Samw 31803126Sahl break; 31813126Sahl 31823126Sahl case OP_MOUNT: 31833126Sahl if (options == NULL) 31843126Sahl mnt.mnt_mntopts = ""; 31853126Sahl else 31863126Sahl mnt.mnt_mntopts = (char *)options; 31873126Sahl 31883126Sahl if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 31893126Sahl zfs_is_mounted(zhp, NULL)) { 31903126Sahl if (!explicit) 31913126Sahl return (0); 31923126Sahl 3193789Sahrens (void) fprintf(stderr, gettext("cannot mount " 3194789Sahrens "'%s': filesystem already mounted\n"), 3195789Sahrens zfs_get_name(zhp)); 3196789Sahrens return (1); 3197789Sahrens } 31983126Sahl 31993126Sahl if (zfs_mount(zhp, options, flags) != 0) 32003126Sahl return (1); 32013126Sahl break; 3202789Sahrens } 320311876SJames.Dunham@Sun.COM } else 32043126Sahl assert(op == OP_SHARE); 32053126Sahl 3206789Sahrens return (0); 3207789Sahrens } 3208789Sahrens 32094737Smmusante /* 32104737Smmusante * Reports progress in the form "(current/total)". Not thread-safe. 32114737Smmusante */ 32124737Smmusante static void 32134737Smmusante report_mount_progress(int current, int total) 32144737Smmusante { 32154737Smmusante static int len; 32164737Smmusante static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" 32174737Smmusante "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; 32184737Smmusante static time_t last_progress_time; 32194737Smmusante time_t now = time(NULL); 32204737Smmusante 32214737Smmusante /* report 1..n instead of 0..n-1 */ 32224737Smmusante ++current; 32234737Smmusante 32244737Smmusante /* display header if we're here for the first time */ 32254737Smmusante if (current == 1) { 32264737Smmusante (void) printf(gettext("Mounting ZFS filesystems: ")); 32274737Smmusante len = 0; 32285367Sahrens } else if (current != total && last_progress_time + MOUNT_TIME >= now) { 32295367Sahrens /* too soon to report again */ 32305367Sahrens return; 32315367Sahrens } 32324737Smmusante 32334737Smmusante last_progress_time = now; 32344737Smmusante 32354737Smmusante /* back up to prepare for overwriting */ 32364737Smmusante if (len) 32374737Smmusante (void) printf("%*.*s", len, len, reverse); 32384737Smmusante 32394737Smmusante /* We put a newline at the end if this is the last one. */ 32404737Smmusante len = printf("(%d/%d)%s", current, total, current == total ? "\n" : ""); 32414737Smmusante (void) fflush(stdout); 32424737Smmusante } 32434737Smmusante 32446289Smmusante static void 32456289Smmusante append_options(char *mntopts, char *newopts) 32466289Smmusante { 32476289Smmusante int len = strlen(mntopts); 32486289Smmusante 32496289Smmusante /* original length plus new string to append plus 1 for the comma */ 32506289Smmusante if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) { 32516289Smmusante (void) fprintf(stderr, gettext("the opts argument for " 32526289Smmusante "'%c' option is too long (more than %d chars)\n"), 32536289Smmusante "-o", MNT_LINE_MAX); 32546289Smmusante usage(B_FALSE); 32556289Smmusante } 32566289Smmusante 32576289Smmusante if (*mntopts) 32586289Smmusante mntopts[len++] = ','; 32596289Smmusante 32606289Smmusante (void) strcpy(&mntopts[len], newopts); 32616289Smmusante } 32626289Smmusante 3263789Sahrens static int 32643126Sahl share_mount(int op, int argc, char **argv) 3265789Sahrens { 3266789Sahrens int do_all = 0; 32674737Smmusante boolean_t verbose = B_FALSE; 32682372Slling int c, ret = 0; 32696289Smmusante char *options = NULL; 32703126Sahl int types, flags = 0; 3271789Sahrens 3272789Sahrens /* check options */ 32734737Smmusante while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a")) 3274789Sahrens != -1) { 3275789Sahrens switch (c) { 3276789Sahrens case 'a': 3277789Sahrens do_all = 1; 3278789Sahrens break; 32794737Smmusante case 'v': 32804737Smmusante verbose = B_TRUE; 32814737Smmusante break; 3282789Sahrens case 'o': 32836289Smmusante if (*optarg == '\0') { 32846289Smmusante (void) fprintf(stderr, gettext("empty mount " 32856289Smmusante "options (-o) specified\n")); 32866289Smmusante usage(B_FALSE); 32874717Srm160521 } 32886289Smmusante 32896289Smmusante if (options == NULL) 32906289Smmusante options = safe_malloc(MNT_LINE_MAX + 1); 32916289Smmusante 32926289Smmusante /* option validation is done later */ 32936289Smmusante append_options(options, optarg); 3294789Sahrens break; 32954717Srm160521 3296789Sahrens case 'O': 32973126Sahl flags |= MS_OVERLAY; 3298789Sahrens break; 3299789Sahrens case ':': 3300789Sahrens (void) fprintf(stderr, gettext("missing argument for " 3301789Sahrens "'%c' option\n"), optopt); 33022082Seschrock usage(B_FALSE); 3303789Sahrens break; 3304789Sahrens case '?': 3305789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3306789Sahrens optopt); 33072082Seschrock usage(B_FALSE); 3308789Sahrens } 3309789Sahrens } 3310789Sahrens 3311789Sahrens argc -= optind; 3312789Sahrens argv += optind; 3313789Sahrens 3314789Sahrens /* check number of arguments */ 3315789Sahrens if (do_all) { 33163126Sahl zfs_handle_t **dslist = NULL; 33171356Seschrock size_t i, count = 0; 33185331Samw char *protocol = NULL; 33191356Seschrock 33203126Sahl if (op == OP_MOUNT) { 33213126Sahl types = ZFS_TYPE_FILESYSTEM; 33223126Sahl } else if (argc > 0) { 33235331Samw if (strcmp(argv[0], "nfs") == 0 || 33245331Samw strcmp(argv[0], "smb") == 0) { 33253126Sahl types = ZFS_TYPE_FILESYSTEM; 33263126Sahl } else { 33273126Sahl (void) fprintf(stderr, gettext("share type " 332811876SJames.Dunham@Sun.COM "must be 'nfs' or 'smb'\n")); 33293126Sahl usage(B_FALSE); 33303126Sahl } 33315331Samw protocol = argv[0]; 33323126Sahl argc--; 33333126Sahl argv++; 33343126Sahl } else { 33353126Sahl types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 33363126Sahl } 33373126Sahl 3338789Sahrens if (argc != 0) { 3339789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 33402082Seschrock usage(B_FALSE); 3341789Sahrens } 3342789Sahrens 33434737Smmusante get_all_datasets(types, &dslist, &count, verbose); 33441356Seschrock 33451356Seschrock if (count == 0) 33461356Seschrock return (0); 33471356Seschrock 33483126Sahl qsort(dslist, count, sizeof (void *), dataset_cmp); 33491356Seschrock 33501356Seschrock for (i = 0; i < count; i++) { 33514737Smmusante if (verbose) 33524737Smmusante report_mount_progress(i, count); 33534737Smmusante 33545331Samw if (share_mount_one(dslist[i], op, flags, protocol, 33555331Samw B_FALSE, options) != 0) 33562369Slling ret = 1; 33573126Sahl zfs_close(dslist[i]); 33581356Seschrock } 33591356Seschrock 33603126Sahl free(dslist); 3361789Sahrens } else if (argc == 0) { 3362789Sahrens struct mnttab entry; 3363789Sahrens 33644717Srm160521 if ((op == OP_SHARE) || (options != NULL)) { 3365789Sahrens (void) fprintf(stderr, gettext("missing filesystem " 33664717Srm160521 "argument (specify -a for all)\n")); 33672082Seschrock usage(B_FALSE); 3368789Sahrens } 3369789Sahrens 3370789Sahrens /* 3371789Sahrens * When mount is given no arguments, go through /etc/mnttab and 3372789Sahrens * display any active ZFS mounts. We hide any snapshots, since 3373789Sahrens * they are controlled automatically. 3374789Sahrens */ 3375789Sahrens rewind(mnttab_file); 3376789Sahrens while (getmntent(mnttab_file, &entry) == 0) { 3377789Sahrens if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 3378789Sahrens strchr(entry.mnt_special, '@') != NULL) 3379789Sahrens continue; 3380789Sahrens 3381789Sahrens (void) printf("%-30s %s\n", entry.mnt_special, 3382789Sahrens entry.mnt_mountp); 3383789Sahrens } 3384789Sahrens 3385789Sahrens } else { 3386789Sahrens zfs_handle_t *zhp; 3387789Sahrens 33883126Sahl types = ZFS_TYPE_FILESYSTEM; 33893126Sahl if (op == OP_SHARE) 33903126Sahl types |= ZFS_TYPE_VOLUME; 33913126Sahl 3392789Sahrens if (argc > 1) { 3393789Sahrens (void) fprintf(stderr, 3394789Sahrens gettext("too many arguments\n")); 33952082Seschrock usage(B_FALSE); 3396789Sahrens } 3397789Sahrens 33983126Sahl if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) { 3399789Sahrens ret = 1; 34003126Sahl } else { 34015331Samw ret = share_mount_one(zhp, op, flags, NULL, B_TRUE, 34023126Sahl options); 3403789Sahrens zfs_close(zhp); 3404789Sahrens } 3405789Sahrens } 3406789Sahrens 3407789Sahrens return (ret); 3408789Sahrens } 3409789Sahrens 3410789Sahrens /* 341111876SJames.Dunham@Sun.COM * zfs mount -a [nfs] 3412789Sahrens * zfs mount filesystem 3413789Sahrens * 3414789Sahrens * Mount all filesystems, or mount the given filesystem. 3415789Sahrens */ 3416789Sahrens static int 3417789Sahrens zfs_do_mount(int argc, char **argv) 3418789Sahrens { 34193126Sahl return (share_mount(OP_MOUNT, argc, argv)); 3420789Sahrens } 3421789Sahrens 3422789Sahrens /* 342311876SJames.Dunham@Sun.COM * zfs share -a [nfs | smb] 3424789Sahrens * zfs share filesystem 3425789Sahrens * 3426789Sahrens * Share all filesystems, or share the given filesystem. 3427789Sahrens */ 3428789Sahrens static int 3429789Sahrens zfs_do_share(int argc, char **argv) 3430789Sahrens { 34313126Sahl return (share_mount(OP_SHARE, argc, argv)); 3432789Sahrens } 3433789Sahrens 3434789Sahrens typedef struct unshare_unmount_node { 3435789Sahrens zfs_handle_t *un_zhp; 3436789Sahrens char *un_mountp; 3437789Sahrens uu_avl_node_t un_avlnode; 3438789Sahrens } unshare_unmount_node_t; 3439789Sahrens 3440789Sahrens /* ARGSUSED */ 3441789Sahrens static int 3442789Sahrens unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 3443789Sahrens { 3444789Sahrens const unshare_unmount_node_t *l = larg; 3445789Sahrens const unshare_unmount_node_t *r = rarg; 3446789Sahrens 3447789Sahrens return (strcmp(l->un_mountp, r->un_mountp)); 3448789Sahrens } 3449789Sahrens 3450789Sahrens /* 3451789Sahrens * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 3452789Sahrens * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem, 3453789Sahrens * and unmount it appropriately. 3454789Sahrens */ 3455789Sahrens static int 34563126Sahl unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) 3457789Sahrens { 3458789Sahrens zfs_handle_t *zhp; 3459789Sahrens int ret; 3460789Sahrens struct stat64 statbuf; 3461789Sahrens struct extmnttab entry; 34623126Sahl const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount"; 34636289Smmusante ino_t path_inode; 3464789Sahrens 3465789Sahrens /* 3466789Sahrens * Search for the path in /etc/mnttab. Rather than looking for the 3467789Sahrens * specific path, which can be fooled by non-standard paths (i.e. ".." 3468789Sahrens * or "//"), we stat() the path and search for the corresponding 3469789Sahrens * (major,minor) device pair. 3470789Sahrens */ 3471789Sahrens if (stat64(path, &statbuf) != 0) { 3472789Sahrens (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 3473789Sahrens cmdname, path, strerror(errno)); 3474789Sahrens return (1); 3475789Sahrens } 34766289Smmusante path_inode = statbuf.st_ino; 3477789Sahrens 3478789Sahrens /* 3479789Sahrens * Search for the given (major,minor) pair in the mount table. 3480789Sahrens */ 3481789Sahrens rewind(mnttab_file); 3482789Sahrens while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) { 3483789Sahrens if (entry.mnt_major == major(statbuf.st_dev) && 3484789Sahrens entry.mnt_minor == minor(statbuf.st_dev)) 3485789Sahrens break; 3486789Sahrens } 3487789Sahrens if (ret != 0) { 34885904Stimh if (op == OP_SHARE) { 34895904Stimh (void) fprintf(stderr, gettext("cannot %s '%s': not " 34905904Stimh "currently mounted\n"), cmdname, path); 34915904Stimh return (1); 34925904Stimh } 34935904Stimh (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"), 34945904Stimh path); 34955904Stimh if ((ret = umount2(path, flags)) != 0) 34965904Stimh (void) fprintf(stderr, gettext("%s: %s\n"), path, 34975904Stimh strerror(errno)); 34985904Stimh return (ret != 0); 3499789Sahrens } 3500789Sahrens 3501789Sahrens if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 3502789Sahrens (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 3503789Sahrens "filesystem\n"), cmdname, path); 3504789Sahrens return (1); 3505789Sahrens } 3506789Sahrens 35072082Seschrock if ((zhp = zfs_open(g_zfs, entry.mnt_special, 35082082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 3509789Sahrens return (1); 3510789Sahrens 35116289Smmusante ret = 1; 35127386SSachin.Gaikwad@Sun.COM if (stat64(entry.mnt_mountp, &statbuf) != 0) { 35137386SSachin.Gaikwad@Sun.COM (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 35147386SSachin.Gaikwad@Sun.COM cmdname, path, strerror(errno)); 35157386SSachin.Gaikwad@Sun.COM goto out; 35167386SSachin.Gaikwad@Sun.COM } else if (statbuf.st_ino != path_inode) { 35177386SSachin.Gaikwad@Sun.COM (void) fprintf(stderr, gettext("cannot " 35187386SSachin.Gaikwad@Sun.COM "%s '%s': not a mountpoint\n"), cmdname, path); 35197386SSachin.Gaikwad@Sun.COM goto out; 35207386SSachin.Gaikwad@Sun.COM } 35217386SSachin.Gaikwad@Sun.COM 35223126Sahl if (op == OP_SHARE) { 35236289Smmusante char nfs_mnt_prop[ZFS_MAXPROPLEN]; 35246289Smmusante char smbshare_prop[ZFS_MAXPROPLEN]; 35256289Smmusante 35266289Smmusante verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop, 35276289Smmusante sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0); 35286289Smmusante verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop, 35296289Smmusante sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0); 35306289Smmusante 35315331Samw if (strcmp(nfs_mnt_prop, "off") == 0 && 35325331Samw strcmp(smbshare_prop, "off") == 0) { 3533789Sahrens (void) fprintf(stderr, gettext("cannot unshare " 3534789Sahrens "'%s': legacy share\n"), path); 3535789Sahrens (void) fprintf(stderr, gettext("use " 3536789Sahrens "unshare(1M) to unshare this filesystem\n")); 35375331Samw } else if (!zfs_is_shared(zhp)) { 3538789Sahrens (void) fprintf(stderr, gettext("cannot unshare '%s': " 3539789Sahrens "not currently shared\n"), path); 3540789Sahrens } else { 35415331Samw ret = zfs_unshareall_bypath(zhp, path); 3542789Sahrens } 3543789Sahrens } else { 35446289Smmusante char mtpt_prop[ZFS_MAXPROPLEN]; 35456289Smmusante 35466289Smmusante verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop, 35476289Smmusante sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0); 35486289Smmusante 35497386SSachin.Gaikwad@Sun.COM if (is_manual) { 35501264Slling ret = zfs_unmount(zhp, NULL, flags); 35516289Smmusante } else if (strcmp(mtpt_prop, "legacy") == 0) { 35521264Slling (void) fprintf(stderr, gettext("cannot unmount " 35531264Slling "'%s': legacy mountpoint\n"), 35541264Slling zfs_get_name(zhp)); 35551264Slling (void) fprintf(stderr, gettext("use umount(1M) " 35561264Slling "to unmount this filesystem\n")); 3557789Sahrens } else { 3558789Sahrens ret = zfs_unmountall(zhp, flags); 3559789Sahrens } 3560789Sahrens } 3561789Sahrens 35627386SSachin.Gaikwad@Sun.COM out: 3563789Sahrens zfs_close(zhp); 3564789Sahrens 3565789Sahrens return (ret != 0); 3566789Sahrens } 3567789Sahrens 3568789Sahrens /* 3569789Sahrens * Generic callback for unsharing or unmounting a filesystem. 3570789Sahrens */ 3571789Sahrens static int 35723126Sahl unshare_unmount(int op, int argc, char **argv) 3573789Sahrens { 3574789Sahrens int do_all = 0; 3575789Sahrens int flags = 0; 3576789Sahrens int ret = 0; 35773126Sahl int types, c; 3578789Sahrens zfs_handle_t *zhp; 357911876SJames.Dunham@Sun.COM char nfs_mnt_prop[ZFS_MAXPROPLEN]; 35805331Samw char sharesmb[ZFS_MAXPROPLEN]; 3581789Sahrens 3582789Sahrens /* check options */ 35833126Sahl while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) { 3584789Sahrens switch (c) { 3585789Sahrens case 'a': 3586789Sahrens do_all = 1; 3587789Sahrens break; 3588789Sahrens case 'f': 3589789Sahrens flags = MS_FORCE; 3590789Sahrens break; 3591789Sahrens case '?': 3592789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3593789Sahrens optopt); 35942082Seschrock usage(B_FALSE); 3595789Sahrens } 3596789Sahrens } 3597789Sahrens 3598789Sahrens argc -= optind; 3599789Sahrens argv += optind; 3600789Sahrens 3601789Sahrens if (do_all) { 3602789Sahrens /* 3603789Sahrens * We could make use of zfs_for_each() to walk all datasets in 3604789Sahrens * the system, but this would be very inefficient, especially 3605789Sahrens * since we would have to linearly search /etc/mnttab for each 3606789Sahrens * one. Instead, do one pass through /etc/mnttab looking for 3607789Sahrens * zfs entries and call zfs_unmount() for each one. 3608789Sahrens * 3609789Sahrens * Things get a little tricky if the administrator has created 3610789Sahrens * mountpoints beneath other ZFS filesystems. In this case, we 3611789Sahrens * have to unmount the deepest filesystems first. To accomplish 3612789Sahrens * this, we place all the mountpoints in an AVL tree sorted by 3613789Sahrens * the special type (dataset name), and walk the result in 3614789Sahrens * reverse to make sure to get any snapshots first. 3615789Sahrens */ 3616789Sahrens struct mnttab entry; 3617789Sahrens uu_avl_pool_t *pool; 3618789Sahrens uu_avl_t *tree; 3619789Sahrens unshare_unmount_node_t *node; 3620789Sahrens uu_avl_index_t idx; 3621789Sahrens uu_avl_walk_t *walk; 3622789Sahrens 36233126Sahl if (argc != 0) { 36243126Sahl (void) fprintf(stderr, gettext("too many arguments\n")); 36253126Sahl usage(B_FALSE); 36263126Sahl } 36273126Sahl 3628789Sahrens if ((pool = uu_avl_pool_create("unmount_pool", 3629789Sahrens sizeof (unshare_unmount_node_t), 3630789Sahrens offsetof(unshare_unmount_node_t, un_avlnode), 3631789Sahrens unshare_unmount_compare, 3632789Sahrens UU_DEFAULT)) == NULL) { 3633789Sahrens (void) fprintf(stderr, gettext("internal error: " 3634789Sahrens "out of memory\n")); 3635789Sahrens exit(1); 3636789Sahrens } 3637789Sahrens 3638789Sahrens if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) { 3639789Sahrens (void) fprintf(stderr, gettext("internal error: " 3640789Sahrens "out of memory\n")); 3641789Sahrens exit(1); 3642789Sahrens } 3643789Sahrens 3644789Sahrens rewind(mnttab_file); 3645789Sahrens while (getmntent(mnttab_file, &entry) == 0) { 3646789Sahrens 3647789Sahrens /* ignore non-ZFS entries */ 3648789Sahrens if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 3649789Sahrens continue; 3650789Sahrens 3651789Sahrens /* ignore snapshots */ 3652789Sahrens if (strchr(entry.mnt_special, '@') != NULL) 3653789Sahrens continue; 3654789Sahrens 36552082Seschrock if ((zhp = zfs_open(g_zfs, entry.mnt_special, 3656789Sahrens ZFS_TYPE_FILESYSTEM)) == NULL) { 3657789Sahrens ret = 1; 3658789Sahrens continue; 3659789Sahrens } 3660789Sahrens 36615331Samw switch (op) { 36625331Samw case OP_SHARE: 36635331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 366411876SJames.Dunham@Sun.COM nfs_mnt_prop, 366511876SJames.Dunham@Sun.COM sizeof (nfs_mnt_prop), 36665331Samw NULL, NULL, 0, B_FALSE) == 0); 366711876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "off") != 0) 36685331Samw break; 36695331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 367011876SJames.Dunham@Sun.COM nfs_mnt_prop, 367111876SJames.Dunham@Sun.COM sizeof (nfs_mnt_prop), 36725331Samw NULL, NULL, 0, B_FALSE) == 0); 367311876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "off") == 0) 36745331Samw continue; 36755331Samw break; 36765331Samw case OP_MOUNT: 36775331Samw /* Ignore legacy mounts */ 36785331Samw verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, 367911876SJames.Dunham@Sun.COM nfs_mnt_prop, 368011876SJames.Dunham@Sun.COM sizeof (nfs_mnt_prop), 36815331Samw NULL, NULL, 0, B_FALSE) == 0); 368211876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "legacy") == 0) 36835331Samw continue; 36846168Shs24103 /* Ignore canmount=noauto mounts */ 36856168Shs24103 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == 36866168Shs24103 ZFS_CANMOUNT_NOAUTO) 36876168Shs24103 continue; 36885331Samw default: 36895331Samw break; 3690789Sahrens } 3691789Sahrens 3692789Sahrens node = safe_malloc(sizeof (unshare_unmount_node_t)); 3693789Sahrens node->un_zhp = zhp; 3694789Sahrens 3695789Sahrens if ((node->un_mountp = strdup(entry.mnt_mountp)) == 3696789Sahrens NULL) { 3697789Sahrens (void) fprintf(stderr, gettext("internal error:" 3698789Sahrens " out of memory\n")); 3699789Sahrens exit(1); 3700789Sahrens } 3701789Sahrens 3702789Sahrens uu_avl_node_init(node, &node->un_avlnode, pool); 3703789Sahrens 3704789Sahrens if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 3705789Sahrens uu_avl_insert(tree, node, idx); 3706789Sahrens } else { 3707789Sahrens zfs_close(node->un_zhp); 3708789Sahrens free(node->un_mountp); 3709789Sahrens free(node); 3710789Sahrens } 3711789Sahrens } 3712789Sahrens 3713789Sahrens /* 3714789Sahrens * Walk the AVL tree in reverse, unmounting each filesystem and 3715789Sahrens * removing it from the AVL tree in the process. 3716789Sahrens */ 3717789Sahrens if ((walk = uu_avl_walk_start(tree, 3718789Sahrens UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) { 3719789Sahrens (void) fprintf(stderr, 3720789Sahrens gettext("internal error: out of memory")); 3721789Sahrens exit(1); 3722789Sahrens } 3723789Sahrens 3724789Sahrens while ((node = uu_avl_walk_next(walk)) != NULL) { 3725789Sahrens uu_avl_remove(tree, node); 3726789Sahrens 37273126Sahl switch (op) { 3728789Sahrens case OP_SHARE: 37295331Samw if (zfs_unshareall_bypath(node->un_zhp, 3730789Sahrens node->un_mountp) != 0) 3731789Sahrens ret = 1; 3732789Sahrens break; 3733789Sahrens 3734789Sahrens case OP_MOUNT: 3735789Sahrens if (zfs_unmount(node->un_zhp, 3736789Sahrens node->un_mountp, flags) != 0) 3737789Sahrens ret = 1; 3738789Sahrens break; 3739789Sahrens } 3740789Sahrens 3741789Sahrens zfs_close(node->un_zhp); 3742789Sahrens free(node->un_mountp); 3743789Sahrens free(node); 3744789Sahrens } 3745789Sahrens 3746789Sahrens uu_avl_walk_end(walk); 3747789Sahrens uu_avl_destroy(tree); 3748789Sahrens uu_avl_pool_destroy(pool); 37493126Sahl 3750789Sahrens } else { 37513126Sahl if (argc != 1) { 37523126Sahl if (argc == 0) 37533126Sahl (void) fprintf(stderr, 37543126Sahl gettext("missing filesystem argument\n")); 37553126Sahl else 37563126Sahl (void) fprintf(stderr, 37573126Sahl gettext("too many arguments\n")); 37583126Sahl usage(B_FALSE); 37593126Sahl } 37603126Sahl 3761789Sahrens /* 3762789Sahrens * We have an argument, but it may be a full path or a ZFS 3763789Sahrens * filesystem. Pass full paths off to unmount_path() (shared by 3764789Sahrens * manual_unmount), otherwise open the filesystem and pass to 3765789Sahrens * zfs_unmount(). 3766789Sahrens */ 3767789Sahrens if (argv[0][0] == '/') 37683126Sahl return (unshare_unmount_path(op, argv[0], 37693912Slling flags, B_FALSE)); 37702082Seschrock 37713126Sahl types = ZFS_TYPE_FILESYSTEM; 37723126Sahl if (op == OP_SHARE) 37733126Sahl types |= ZFS_TYPE_VOLUME; 37743126Sahl 37753126Sahl if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) 3776789Sahrens return (1); 3777789Sahrens 37783126Sahl if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 37793126Sahl verify(zfs_prop_get(zhp, op == OP_SHARE ? 37805331Samw ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 378111876SJames.Dunham@Sun.COM nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL, 37825331Samw NULL, 0, B_FALSE) == 0); 37833126Sahl 37843126Sahl switch (op) { 37853126Sahl case OP_SHARE: 37865331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 378711876SJames.Dunham@Sun.COM nfs_mnt_prop, 378811876SJames.Dunham@Sun.COM sizeof (nfs_mnt_prop), 37895331Samw NULL, NULL, 0, B_FALSE) == 0); 37905331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 37915331Samw sharesmb, sizeof (sharesmb), NULL, NULL, 37925331Samw 0, B_FALSE) == 0); 37935331Samw 379411876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "off") == 0 && 37955331Samw strcmp(sharesmb, "off") == 0) { 37963126Sahl (void) fprintf(stderr, gettext("cannot " 37973126Sahl "unshare '%s': legacy share\n"), 37983126Sahl zfs_get_name(zhp)); 37993126Sahl (void) fprintf(stderr, gettext("use " 38003126Sahl "unshare(1M) to unshare this " 38013126Sahl "filesystem\n")); 38023126Sahl ret = 1; 38035331Samw } else if (!zfs_is_shared(zhp)) { 38043126Sahl (void) fprintf(stderr, gettext("cannot " 38053126Sahl "unshare '%s': not currently " 38063126Sahl "shared\n"), zfs_get_name(zhp)); 38073126Sahl ret = 1; 38085331Samw } else if (zfs_unshareall(zhp) != 0) { 38093126Sahl ret = 1; 38103126Sahl } 38113126Sahl break; 38123126Sahl 38133126Sahl case OP_MOUNT: 381411876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "legacy") == 0) { 38153126Sahl (void) fprintf(stderr, gettext("cannot " 38163126Sahl "unmount '%s': legacy " 38173126Sahl "mountpoint\n"), zfs_get_name(zhp)); 38183126Sahl (void) fprintf(stderr, gettext("use " 38193126Sahl "umount(1M) to unmount this " 38203126Sahl "filesystem\n")); 38213126Sahl ret = 1; 38223126Sahl } else if (!zfs_is_mounted(zhp, NULL)) { 38233126Sahl (void) fprintf(stderr, gettext("cannot " 38243126Sahl "unmount '%s': not currently " 38253126Sahl "mounted\n"), 38263126Sahl zfs_get_name(zhp)); 38273126Sahl ret = 1; 38283126Sahl } else if (zfs_unmountall(zhp, flags) != 0) { 38293126Sahl ret = 1; 38303126Sahl } 38313126Sahl break; 38323126Sahl } 3833789Sahrens } 3834789Sahrens 3835789Sahrens zfs_close(zhp); 3836789Sahrens } 3837789Sahrens 3838789Sahrens return (ret); 3839789Sahrens } 3840789Sahrens 3841789Sahrens /* 3842789Sahrens * zfs unmount -a 3843789Sahrens * zfs unmount filesystem 3844789Sahrens * 3845789Sahrens * Unmount all filesystems, or a specific ZFS filesystem. 3846789Sahrens */ 3847789Sahrens static int 3848789Sahrens zfs_do_unmount(int argc, char **argv) 3849789Sahrens { 3850789Sahrens return (unshare_unmount(OP_MOUNT, argc, argv)); 3851789Sahrens } 3852789Sahrens 3853789Sahrens /* 3854789Sahrens * zfs unshare -a 3855789Sahrens * zfs unshare filesystem 3856789Sahrens * 3857789Sahrens * Unshare all filesystems, or a specific ZFS filesystem. 3858789Sahrens */ 3859789Sahrens static int 3860789Sahrens zfs_do_unshare(int argc, char **argv) 3861789Sahrens { 3862789Sahrens return (unshare_unmount(OP_SHARE, argc, argv)); 3863789Sahrens } 3864789Sahrens 38659396SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 38669396SMatthew.Ahrens@Sun.COM static int 38679396SMatthew.Ahrens@Sun.COM zfs_do_python(int argc, char **argv) 38689396SMatthew.Ahrens@Sun.COM { 38699396SMatthew.Ahrens@Sun.COM (void) execv(pypath, argv-1); 38709396SMatthew.Ahrens@Sun.COM (void) printf("internal error: %s not found\n", pypath); 38719396SMatthew.Ahrens@Sun.COM return (-1); 38729396SMatthew.Ahrens@Sun.COM } 38739396SMatthew.Ahrens@Sun.COM 3874789Sahrens /* 3875789Sahrens * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is 3876789Sahrens * 'legacy'. Otherwise, complain that use should be using 'zfs mount'. 3877789Sahrens */ 3878789Sahrens static int 3879789Sahrens manual_mount(int argc, char **argv) 3880789Sahrens { 3881789Sahrens zfs_handle_t *zhp; 3882789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 3883789Sahrens char mntopts[MNT_LINE_MAX] = { '\0' }; 3884789Sahrens int ret; 3885789Sahrens int c; 3886789Sahrens int flags = 0; 3887789Sahrens char *dataset, *path; 3888789Sahrens 3889789Sahrens /* check options */ 38901544Seschrock while ((c = getopt(argc, argv, ":mo:O")) != -1) { 3891789Sahrens switch (c) { 3892789Sahrens case 'o': 3893789Sahrens (void) strlcpy(mntopts, optarg, sizeof (mntopts)); 3894789Sahrens break; 3895789Sahrens case 'O': 3896789Sahrens flags |= MS_OVERLAY; 3897789Sahrens break; 38981544Seschrock case 'm': 38991544Seschrock flags |= MS_NOMNTTAB; 39001544Seschrock break; 3901789Sahrens case ':': 3902789Sahrens (void) fprintf(stderr, gettext("missing argument for " 3903789Sahrens "'%c' option\n"), optopt); 39042082Seschrock usage(B_FALSE); 3905789Sahrens break; 3906789Sahrens case '?': 3907789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3908789Sahrens optopt); 3909789Sahrens (void) fprintf(stderr, gettext("usage: mount [-o opts] " 3910789Sahrens "<path>\n")); 3911789Sahrens return (2); 3912789Sahrens } 3913789Sahrens } 3914789Sahrens 3915789Sahrens argc -= optind; 3916789Sahrens argv += optind; 3917789Sahrens 3918789Sahrens /* check that we only have two arguments */ 3919789Sahrens if (argc != 2) { 3920789Sahrens if (argc == 0) 3921789Sahrens (void) fprintf(stderr, gettext("missing dataset " 3922789Sahrens "argument\n")); 3923789Sahrens else if (argc == 1) 3924789Sahrens (void) fprintf(stderr, 3925789Sahrens gettext("missing mountpoint argument\n")); 3926789Sahrens else 3927789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 3928789Sahrens (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n"); 3929789Sahrens return (2); 3930789Sahrens } 3931789Sahrens 3932789Sahrens dataset = argv[0]; 3933789Sahrens path = argv[1]; 3934789Sahrens 3935789Sahrens /* try to open the dataset */ 39362082Seschrock if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) 3937789Sahrens return (1); 3938789Sahrens 3939789Sahrens (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 39402082Seschrock sizeof (mountpoint), NULL, NULL, 0, B_FALSE); 3941789Sahrens 3942789Sahrens /* check for legacy mountpoint and complain appropriately */ 3943789Sahrens ret = 0; 3944789Sahrens if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 3945789Sahrens if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS, 3946789Sahrens NULL, 0, mntopts, sizeof (mntopts)) != 0) { 3947789Sahrens (void) fprintf(stderr, gettext("mount failed: %s\n"), 3948789Sahrens strerror(errno)); 3949789Sahrens ret = 1; 3950789Sahrens } 3951789Sahrens } else { 3952789Sahrens (void) fprintf(stderr, gettext("filesystem '%s' cannot be " 3953789Sahrens "mounted using 'mount -F zfs'\n"), dataset); 3954789Sahrens (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' " 3955789Sahrens "instead.\n"), path); 3956789Sahrens (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' " 3957789Sahrens "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n")); 3958789Sahrens (void) fprintf(stderr, gettext("See zfs(1M) for more " 3959789Sahrens "information.\n")); 3960789Sahrens ret = 1; 3961789Sahrens } 3962789Sahrens 3963789Sahrens return (ret); 3964789Sahrens } 3965789Sahrens 3966789Sahrens /* 3967789Sahrens * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow 3968789Sahrens * unmounts of non-legacy filesystems, as this is the dominant administrative 3969789Sahrens * interface. 3970789Sahrens */ 3971789Sahrens static int 3972789Sahrens manual_unmount(int argc, char **argv) 3973789Sahrens { 3974789Sahrens int flags = 0; 3975789Sahrens int c; 3976789Sahrens 3977789Sahrens /* check options */ 3978789Sahrens while ((c = getopt(argc, argv, "f")) != -1) { 3979789Sahrens switch (c) { 3980789Sahrens case 'f': 3981789Sahrens flags = MS_FORCE; 3982789Sahrens break; 3983789Sahrens case '?': 3984789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3985789Sahrens optopt); 3986789Sahrens (void) fprintf(stderr, gettext("usage: unmount [-f] " 3987789Sahrens "<path>\n")); 3988789Sahrens return (2); 3989789Sahrens } 3990789Sahrens } 3991789Sahrens 3992789Sahrens argc -= optind; 3993789Sahrens argv += optind; 3994789Sahrens 3995789Sahrens /* check arguments */ 3996789Sahrens if (argc != 1) { 3997789Sahrens if (argc == 0) 3998789Sahrens (void) fprintf(stderr, gettext("missing path " 3999789Sahrens "argument\n")); 4000789Sahrens else 4001789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 4002789Sahrens (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n")); 4003789Sahrens return (2); 4004789Sahrens } 4005789Sahrens 40062082Seschrock return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE)); 4007789Sahrens } 4008789Sahrens 4009789Sahrens static int 40104543Smarks find_command_idx(char *command, int *idx) 40114543Smarks { 40124543Smarks int i; 40134543Smarks 40144543Smarks for (i = 0; i < NCOMMAND; i++) { 40154543Smarks if (command_table[i].name == NULL) 40164543Smarks continue; 40174543Smarks 40184543Smarks if (strcmp(command, command_table[i].name) == 0) { 40194543Smarks *idx = i; 40204543Smarks return (0); 40214543Smarks } 40224543Smarks } 40234543Smarks return (1); 40244543Smarks } 40254543Smarks 4026789Sahrens int 4027789Sahrens main(int argc, char **argv) 4028789Sahrens { 4029789Sahrens int ret; 4030789Sahrens int i; 4031789Sahrens char *progname; 4032789Sahrens char *cmdname; 4033789Sahrens 4034789Sahrens (void) setlocale(LC_ALL, ""); 4035789Sahrens (void) textdomain(TEXT_DOMAIN); 4036789Sahrens 4037789Sahrens opterr = 0; 4038789Sahrens 40392082Seschrock if ((g_zfs = libzfs_init()) == NULL) { 40402082Seschrock (void) fprintf(stderr, gettext("internal error: failed to " 40412082Seschrock "initialize ZFS library\n")); 40422082Seschrock return (1); 40432082Seschrock } 40442082Seschrock 40454988Sek110237 zpool_set_history_str("zfs", argc, argv, history_str); 40464988Sek110237 verify(zpool_stage_history(g_zfs, history_str) == 0); 40474543Smarks 40482082Seschrock libzfs_print_on_error(g_zfs, B_TRUE); 40492082Seschrock 4050789Sahrens if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) { 4051789Sahrens (void) fprintf(stderr, gettext("internal error: unable to " 4052789Sahrens "open %s\n"), MNTTAB); 4053789Sahrens return (1); 4054789Sahrens } 4055789Sahrens 4056789Sahrens /* 4057789Sahrens * This command also doubles as the /etc/fs mount and unmount program. 4058789Sahrens * Determine if we should take this behavior based on argv[0]. 4059789Sahrens */ 4060789Sahrens progname = basename(argv[0]); 4061789Sahrens if (strcmp(progname, "mount") == 0) { 4062789Sahrens ret = manual_mount(argc, argv); 4063789Sahrens } else if (strcmp(progname, "umount") == 0) { 4064789Sahrens ret = manual_unmount(argc, argv); 4065789Sahrens } else { 4066789Sahrens /* 4067789Sahrens * Make sure the user has specified some command. 4068789Sahrens */ 4069789Sahrens if (argc < 2) { 4070789Sahrens (void) fprintf(stderr, gettext("missing command\n")); 40712082Seschrock usage(B_FALSE); 4072789Sahrens } 4073789Sahrens 4074789Sahrens cmdname = argv[1]; 4075789Sahrens 4076789Sahrens /* 4077789Sahrens * The 'umount' command is an alias for 'unmount' 4078789Sahrens */ 4079789Sahrens if (strcmp(cmdname, "umount") == 0) 4080789Sahrens cmdname = "unmount"; 4081789Sahrens 4082789Sahrens /* 40831749Sahrens * The 'recv' command is an alias for 'receive' 40841749Sahrens */ 40851749Sahrens if (strcmp(cmdname, "recv") == 0) 40861749Sahrens cmdname = "receive"; 40871749Sahrens 40881749Sahrens /* 4089789Sahrens * Special case '-?' 4090789Sahrens */ 4091789Sahrens if (strcmp(cmdname, "-?") == 0) 40922082Seschrock usage(B_TRUE); 4093789Sahrens 4094789Sahrens /* 4095789Sahrens * Run the appropriate command. 4096789Sahrens */ 40978811SEric.Taylor@Sun.COM libzfs_mnttab_cache(g_zfs, B_TRUE); 40984543Smarks if (find_command_idx(cmdname, &i) == 0) { 40994543Smarks current_command = &command_table[i]; 41004543Smarks ret = command_table[i].func(argc - 1, argv + 1); 41014787Sahrens } else if (strchr(cmdname, '=') != NULL) { 41024787Sahrens verify(find_command_idx("set", &i) == 0); 41034787Sahrens current_command = &command_table[i]; 41044787Sahrens ret = command_table[i].func(argc, argv); 41054787Sahrens } else { 4106789Sahrens (void) fprintf(stderr, gettext("unrecognized " 4107789Sahrens "command '%s'\n"), cmdname); 41082082Seschrock usage(B_FALSE); 4109789Sahrens } 41108811SEric.Taylor@Sun.COM libzfs_mnttab_cache(g_zfs, B_FALSE); 4111789Sahrens } 4112789Sahrens 4113789Sahrens (void) fclose(mnttab_file); 4114789Sahrens 41152082Seschrock libzfs_fini(g_zfs); 41162082Seschrock 4117789Sahrens /* 4118789Sahrens * The 'ZFS_ABORT' environment variable causes us to dump core on exit 4119789Sahrens * for the purposes of running ::findleaks. 4120789Sahrens */ 4121789Sahrens if (getenv("ZFS_ABORT") != NULL) { 4122789Sahrens (void) printf("dumping core by request\n"); 4123789Sahrens abort(); 4124789Sahrens } 4125789Sahrens 4126789Sahrens return (ret); 4127789Sahrens } 4128