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 /* 2312296SLin.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: 224*12409STom.Erickson@Sun.COM return (gettext("\treceive [-vnFu] <filesystem|volume|" 2252665Snd150628 "snapshot>\n" 226*12409STom.Erickson@Sun.COM "\treceive [-vnFu] [-d | -e] <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 " 171912321SStephanie.Scheffler@Sun.COM "version, including supported releases,\n")); 172012321SStephanie.Scheffler@Sun.COM (void) printf("see the ZFS Administration Guide.\n\n"); 17214577Sahrens ret = 0; 17224577Sahrens } else if (argc || all) { 17234577Sahrens /* Upgrade filesystems */ 17244577Sahrens if (cb.cb_version == 0) 17254577Sahrens cb.cb_version = ZPL_VERSION; 17267538SRichard.Morris@Sun.COM ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM, 17279365SChris.Gerhard@sun.com NULL, NULL, 0, upgrade_set_callback, &cb); 17284577Sahrens (void) printf(gettext("%llu filesystems upgraded\n"), 17294577Sahrens cb.cb_numupgraded); 17304577Sahrens if (cb.cb_numsamegraded) { 17314577Sahrens (void) printf(gettext("%llu filesystems already at " 17324577Sahrens "this version\n"), 17334577Sahrens cb.cb_numsamegraded); 17344577Sahrens } 17354849Sahrens if (cb.cb_numfailed != 0) 17364577Sahrens ret = 1; 17374577Sahrens } else { 17384577Sahrens /* List old-version filesytems */ 17394577Sahrens boolean_t found; 17404577Sahrens (void) printf(gettext("This system is currently running " 17414577Sahrens "ZFS filesystem version %llu.\n\n"), ZPL_VERSION); 17424577Sahrens 17437538SRichard.Morris@Sun.COM flags |= ZFS_ITER_RECURSE; 17447538SRichard.Morris@Sun.COM ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 17459365SChris.Gerhard@sun.com NULL, NULL, 0, upgrade_list_callback, &cb); 17464577Sahrens 17474577Sahrens found = cb.cb_foundone; 17484577Sahrens cb.cb_foundone = B_FALSE; 17494577Sahrens cb.cb_newer = B_TRUE; 17504577Sahrens 17517538SRichard.Morris@Sun.COM ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 17529365SChris.Gerhard@sun.com NULL, NULL, 0, upgrade_list_callback, &cb); 17534577Sahrens 17544577Sahrens if (!cb.cb_foundone && !found) { 17554577Sahrens (void) printf(gettext("All filesystems are " 17564577Sahrens "formatted with the current version.\n")); 17574577Sahrens } 17584577Sahrens } 17594577Sahrens 17604577Sahrens return (ret); 17614577Sahrens } 17624577Sahrens 1763789Sahrens /* 17649396SMatthew.Ahrens@Sun.COM * zfs userspace 17659396SMatthew.Ahrens@Sun.COM */ 17669554SMatthew.Ahrens@Sun.COM static int 17679396SMatthew.Ahrens@Sun.COM userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space) 17689396SMatthew.Ahrens@Sun.COM { 17699396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t *typep = arg; 17709396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t p = *typep; 17719554SMatthew.Ahrens@Sun.COM char *name = NULL; 17729554SMatthew.Ahrens@Sun.COM char *ug, *propname; 17739396SMatthew.Ahrens@Sun.COM char namebuf[32]; 17749396SMatthew.Ahrens@Sun.COM char sizebuf[32]; 17759396SMatthew.Ahrens@Sun.COM 17769396SMatthew.Ahrens@Sun.COM if (domain == NULL || domain[0] == '\0') { 17779396SMatthew.Ahrens@Sun.COM if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) { 17789396SMatthew.Ahrens@Sun.COM struct group *g = getgrgid(rid); 17799396SMatthew.Ahrens@Sun.COM if (g) 17809396SMatthew.Ahrens@Sun.COM name = g->gr_name; 17819396SMatthew.Ahrens@Sun.COM } else { 17829396SMatthew.Ahrens@Sun.COM struct passwd *p = getpwuid(rid); 17839396SMatthew.Ahrens@Sun.COM if (p) 17849396SMatthew.Ahrens@Sun.COM name = p->pw_name; 17859396SMatthew.Ahrens@Sun.COM } 17869396SMatthew.Ahrens@Sun.COM } 17879396SMatthew.Ahrens@Sun.COM 17889396SMatthew.Ahrens@Sun.COM if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) 17899396SMatthew.Ahrens@Sun.COM ug = "group"; 17909396SMatthew.Ahrens@Sun.COM else 17919396SMatthew.Ahrens@Sun.COM ug = "user"; 17929396SMatthew.Ahrens@Sun.COM 17939396SMatthew.Ahrens@Sun.COM if (p == ZFS_PROP_USERUSED || p == ZFS_PROP_GROUPUSED) 17949396SMatthew.Ahrens@Sun.COM propname = "used"; 17959396SMatthew.Ahrens@Sun.COM else 17969396SMatthew.Ahrens@Sun.COM propname = "quota"; 17979396SMatthew.Ahrens@Sun.COM 17989554SMatthew.Ahrens@Sun.COM if (name == NULL) { 17999396SMatthew.Ahrens@Sun.COM (void) snprintf(namebuf, sizeof (namebuf), 18009396SMatthew.Ahrens@Sun.COM "%llu", (longlong_t)rid); 18019396SMatthew.Ahrens@Sun.COM name = namebuf; 18029396SMatthew.Ahrens@Sun.COM } 18039396SMatthew.Ahrens@Sun.COM zfs_nicenum(space, sizebuf, sizeof (sizebuf)); 18049396SMatthew.Ahrens@Sun.COM 18059396SMatthew.Ahrens@Sun.COM (void) printf("%s %s %s%c%s %s\n", propname, ug, domain, 18069396SMatthew.Ahrens@Sun.COM domain[0] ? '-' : ' ', name, sizebuf); 18079554SMatthew.Ahrens@Sun.COM 18089554SMatthew.Ahrens@Sun.COM return (0); 18099396SMatthew.Ahrens@Sun.COM } 18109396SMatthew.Ahrens@Sun.COM 18119396SMatthew.Ahrens@Sun.COM static int 18129396SMatthew.Ahrens@Sun.COM zfs_do_userspace(int argc, char **argv) 18139396SMatthew.Ahrens@Sun.COM { 18149396SMatthew.Ahrens@Sun.COM zfs_handle_t *zhp; 18159396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t p; 18169396SMatthew.Ahrens@Sun.COM int error; 18179396SMatthew.Ahrens@Sun.COM 18189396SMatthew.Ahrens@Sun.COM /* 18199396SMatthew.Ahrens@Sun.COM * Try the python version. If the execv fails, we'll continue 18209396SMatthew.Ahrens@Sun.COM * and do a simplistic implementation. 18219396SMatthew.Ahrens@Sun.COM */ 18229396SMatthew.Ahrens@Sun.COM (void) execv(pypath, argv-1); 18239396SMatthew.Ahrens@Sun.COM 18249396SMatthew.Ahrens@Sun.COM (void) printf("internal error: %s not found\n" 18259396SMatthew.Ahrens@Sun.COM "falling back on built-in implementation, " 18269396SMatthew.Ahrens@Sun.COM "some features will not work\n", pypath); 18279396SMatthew.Ahrens@Sun.COM 18289396SMatthew.Ahrens@Sun.COM if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL) 18299396SMatthew.Ahrens@Sun.COM return (1); 18309396SMatthew.Ahrens@Sun.COM 18319396SMatthew.Ahrens@Sun.COM (void) printf("PROP TYPE NAME VALUE\n"); 18329396SMatthew.Ahrens@Sun.COM 18339396SMatthew.Ahrens@Sun.COM for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) { 18349396SMatthew.Ahrens@Sun.COM error = zfs_userspace(zhp, p, userspace_cb, &p); 18359396SMatthew.Ahrens@Sun.COM if (error) 18369396SMatthew.Ahrens@Sun.COM break; 18379396SMatthew.Ahrens@Sun.COM } 18389396SMatthew.Ahrens@Sun.COM return (error); 18399396SMatthew.Ahrens@Sun.COM } 18409396SMatthew.Ahrens@Sun.COM 18419396SMatthew.Ahrens@Sun.COM /* 18429365SChris.Gerhard@sun.com * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...] 18432379Ssjelinek * [-s property [-s property]...] [-S property [-S property]...] 18442379Ssjelinek * <dataset> ... 1845789Sahrens * 184611022STom.Erickson@Sun.COM * -r Recurse over all children 184711022STom.Erickson@Sun.COM * -d Limit recursion by depth. 184811022STom.Erickson@Sun.COM * -H Scripted mode; elide headers and separate columns by tabs 184911022STom.Erickson@Sun.COM * -o Control which fields to display. 185011022STom.Erickson@Sun.COM * -t Control which object types to display. 18512379Ssjelinek * -s Specify sort columns, descending order. 18522379Ssjelinek * -S Specify sort columns, ascending order. 1853789Sahrens * 1854789Sahrens * When given no arguments, lists all filesystems in the system. 1855789Sahrens * Otherwise, list the specified datasets, optionally recursing down them if 1856789Sahrens * '-r' is specified. 1857789Sahrens */ 1858789Sahrens typedef struct list_cbdata { 18592082Seschrock boolean_t cb_first; 18602082Seschrock boolean_t cb_scripted; 18615094Slling zprop_list_t *cb_proplist; 1862789Sahrens } list_cbdata_t; 1863789Sahrens 1864789Sahrens /* 1865789Sahrens * Given a list of columns to display, output appropriate headers for each one. 1866789Sahrens */ 1867789Sahrens static void 18685094Slling print_header(zprop_list_t *pl) 1869789Sahrens { 18702676Seschrock char headerbuf[ZFS_MAXPROPLEN]; 18712676Seschrock const char *header; 1872789Sahrens int i; 18732676Seschrock boolean_t first = B_TRUE; 18742676Seschrock boolean_t right_justify; 18752676Seschrock 18762676Seschrock for (; pl != NULL; pl = pl->pl_next) { 18772676Seschrock if (!first) { 1878789Sahrens (void) printf(" "); 18792676Seschrock } else { 18802676Seschrock first = B_FALSE; 18812676Seschrock } 18822676Seschrock 18832676Seschrock right_justify = B_FALSE; 18845094Slling if (pl->pl_prop != ZPROP_INVAL) { 18852676Seschrock header = zfs_prop_column_name(pl->pl_prop); 18862676Seschrock right_justify = zfs_prop_align_right(pl->pl_prop); 18872676Seschrock } else { 18882676Seschrock for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 18892676Seschrock headerbuf[i] = toupper(pl->pl_user_prop[i]); 18902676Seschrock headerbuf[i] = '\0'; 18912676Seschrock header = headerbuf; 18922676Seschrock } 18932676Seschrock 18942676Seschrock if (pl->pl_next == NULL && !right_justify) 18952676Seschrock (void) printf("%s", header); 18962676Seschrock else if (right_justify) 18972676Seschrock (void) printf("%*s", pl->pl_width, header); 18982676Seschrock else 18992676Seschrock (void) printf("%-*s", pl->pl_width, header); 1900789Sahrens } 1901789Sahrens 1902789Sahrens (void) printf("\n"); 1903789Sahrens } 1904789Sahrens 1905789Sahrens /* 1906789Sahrens * Given a dataset and a list of fields, print out all the properties according 1907789Sahrens * to the described layout. 1908789Sahrens */ 1909789Sahrens static void 19105094Slling print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted) 1911789Sahrens { 19122676Seschrock boolean_t first = B_TRUE; 1913789Sahrens char property[ZFS_MAXPROPLEN]; 19142676Seschrock nvlist_t *userprops = zfs_get_user_props(zhp); 19152676Seschrock nvlist_t *propval; 19162676Seschrock char *propstr; 19172676Seschrock boolean_t right_justify; 19182676Seschrock int width; 19192676Seschrock 19202676Seschrock for (; pl != NULL; pl = pl->pl_next) { 19212676Seschrock if (!first) { 1922789Sahrens if (scripted) 1923789Sahrens (void) printf("\t"); 1924789Sahrens else 1925789Sahrens (void) printf(" "); 19262676Seschrock } else { 19272676Seschrock first = B_FALSE; 1928789Sahrens } 1929789Sahrens 19305094Slling if (pl->pl_prop != ZPROP_INVAL) { 19312676Seschrock if (zfs_prop_get(zhp, pl->pl_prop, property, 19322676Seschrock sizeof (property), NULL, NULL, 0, B_FALSE) != 0) 19332676Seschrock propstr = "-"; 19342676Seschrock else 19352676Seschrock propstr = property; 19362676Seschrock 19372676Seschrock right_justify = zfs_prop_align_right(pl->pl_prop); 19389396SMatthew.Ahrens@Sun.COM } else if (zfs_prop_userquota(pl->pl_user_prop)) { 19399396SMatthew.Ahrens@Sun.COM if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 19409396SMatthew.Ahrens@Sun.COM property, sizeof (property), B_FALSE) != 0) 19419396SMatthew.Ahrens@Sun.COM propstr = "-"; 19429396SMatthew.Ahrens@Sun.COM else 19439396SMatthew.Ahrens@Sun.COM propstr = property; 19449396SMatthew.Ahrens@Sun.COM right_justify = B_TRUE; 19452676Seschrock } else { 19462676Seschrock if (nvlist_lookup_nvlist(userprops, 19472676Seschrock pl->pl_user_prop, &propval) != 0) 19482676Seschrock propstr = "-"; 19492676Seschrock else 19502676Seschrock verify(nvlist_lookup_string(propval, 19515094Slling ZPROP_VALUE, &propstr) == 0); 19529396SMatthew.Ahrens@Sun.COM right_justify = B_FALSE; 19532676Seschrock } 19542676Seschrock 19552676Seschrock width = pl->pl_width; 1956789Sahrens 1957866Seschrock /* 1958866Seschrock * If this is being called in scripted mode, or if this is the 1959866Seschrock * last column and it is left-justified, don't include a width 1960866Seschrock * format specifier. 1961866Seschrock */ 19622676Seschrock if (scripted || (pl->pl_next == NULL && !right_justify)) 19632676Seschrock (void) printf("%s", propstr); 19642676Seschrock else if (right_justify) 19652676Seschrock (void) printf("%*s", width, propstr); 19662676Seschrock else 19672676Seschrock (void) printf("%-*s", width, propstr); 1968789Sahrens } 1969789Sahrens 1970789Sahrens (void) printf("\n"); 1971789Sahrens } 1972789Sahrens 1973789Sahrens /* 1974789Sahrens * Generic callback function to list a dataset or snapshot. 1975789Sahrens */ 1976789Sahrens static int 1977789Sahrens list_callback(zfs_handle_t *zhp, void *data) 1978789Sahrens { 1979789Sahrens list_cbdata_t *cbp = data; 1980789Sahrens 1981789Sahrens if (cbp->cb_first) { 1982789Sahrens if (!cbp->cb_scripted) 19832676Seschrock print_header(cbp->cb_proplist); 19842082Seschrock cbp->cb_first = B_FALSE; 1985789Sahrens } 1986789Sahrens 19872676Seschrock print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted); 1988789Sahrens 1989789Sahrens return (0); 1990789Sahrens } 1991789Sahrens 1992789Sahrens static int 1993789Sahrens zfs_do_list(int argc, char **argv) 1994789Sahrens { 1995789Sahrens int c; 19962082Seschrock boolean_t scripted = B_FALSE; 1997789Sahrens static char default_fields[] = 1998789Sahrens "name,used,available,referenced,mountpoint"; 19998415SRichard.Morris@Sun.COM int types = ZFS_TYPE_DATASET; 20007538SRichard.Morris@Sun.COM boolean_t types_specified = B_FALSE; 2001789Sahrens char *fields = NULL; 2002789Sahrens list_cbdata_t cb = { 0 }; 2003789Sahrens char *value; 20049365SChris.Gerhard@sun.com int limit = 0; 2005789Sahrens int ret; 20062379Ssjelinek zfs_sort_column_t *sortcol = NULL; 20077538SRichard.Morris@Sun.COM int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS; 2008789Sahrens 2009789Sahrens /* check options */ 20109365SChris.Gerhard@sun.com while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) { 2011789Sahrens switch (c) { 2012789Sahrens case 'o': 2013789Sahrens fields = optarg; 2014789Sahrens break; 20159365SChris.Gerhard@sun.com case 'd': 20169365SChris.Gerhard@sun.com limit = parse_depth(optarg, &flags); 20179365SChris.Gerhard@sun.com break; 2018789Sahrens case 'r': 20197538SRichard.Morris@Sun.COM flags |= ZFS_ITER_RECURSE; 2020789Sahrens break; 2021789Sahrens case 'H': 20222082Seschrock scripted = B_TRUE; 2023789Sahrens break; 20242379Ssjelinek case 's': 20252676Seschrock if (zfs_add_sort_column(&sortcol, optarg, 20262676Seschrock B_FALSE) != 0) { 20272379Ssjelinek (void) fprintf(stderr, 20282379Ssjelinek gettext("invalid property '%s'\n"), optarg); 20292379Ssjelinek usage(B_FALSE); 20302379Ssjelinek } 20312379Ssjelinek break; 20322379Ssjelinek case 'S': 20332676Seschrock if (zfs_add_sort_column(&sortcol, optarg, 20342676Seschrock B_TRUE) != 0) { 20352379Ssjelinek (void) fprintf(stderr, 20362379Ssjelinek gettext("invalid property '%s'\n"), optarg); 20372379Ssjelinek usage(B_FALSE); 20382379Ssjelinek } 20392379Ssjelinek break; 2040789Sahrens case 't': 2041789Sahrens types = 0; 20427538SRichard.Morris@Sun.COM types_specified = B_TRUE; 20437538SRichard.Morris@Sun.COM flags &= ~ZFS_ITER_PROP_LISTSNAPS; 2044789Sahrens while (*optarg != '\0') { 20457538SRichard.Morris@Sun.COM static char *type_subopts[] = { "filesystem", 20467538SRichard.Morris@Sun.COM "volume", "snapshot", "all", NULL }; 20477538SRichard.Morris@Sun.COM 2048789Sahrens switch (getsubopt(&optarg, type_subopts, 2049789Sahrens &value)) { 2050789Sahrens case 0: 2051789Sahrens types |= ZFS_TYPE_FILESYSTEM; 2052789Sahrens break; 2053789Sahrens case 1: 2054789Sahrens types |= ZFS_TYPE_VOLUME; 2055789Sahrens break; 2056789Sahrens case 2: 2057789Sahrens types |= ZFS_TYPE_SNAPSHOT; 2058789Sahrens break; 20597538SRichard.Morris@Sun.COM case 3: 20607538SRichard.Morris@Sun.COM types = ZFS_TYPE_DATASET; 20617538SRichard.Morris@Sun.COM break; 20627538SRichard.Morris@Sun.COM 2063789Sahrens default: 2064789Sahrens (void) fprintf(stderr, 2065789Sahrens gettext("invalid type '%s'\n"), 2066789Sahrens value); 20672082Seschrock usage(B_FALSE); 2068789Sahrens } 2069789Sahrens } 2070789Sahrens break; 2071789Sahrens case ':': 2072789Sahrens (void) fprintf(stderr, gettext("missing argument for " 2073789Sahrens "'%c' option\n"), optopt); 20742082Seschrock usage(B_FALSE); 2075789Sahrens break; 2076789Sahrens case '?': 2077789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2078789Sahrens optopt); 20792082Seschrock usage(B_FALSE); 2080789Sahrens } 2081789Sahrens } 2082789Sahrens 2083789Sahrens argc -= optind; 2084789Sahrens argv += optind; 2085789Sahrens 2086789Sahrens if (fields == NULL) 20877390SMatthew.Ahrens@Sun.COM fields = default_fields; 20887390SMatthew.Ahrens@Sun.COM 20897390SMatthew.Ahrens@Sun.COM /* 20907538SRichard.Morris@Sun.COM * If "-o space" and no types were specified, don't display snapshots. 20917390SMatthew.Ahrens@Sun.COM */ 20927538SRichard.Morris@Sun.COM if (strcmp(fields, "space") == 0 && types_specified == B_FALSE) 20937390SMatthew.Ahrens@Sun.COM types &= ~ZFS_TYPE_SNAPSHOT; 2094789Sahrens 2095866Seschrock /* 20965094Slling * If the user specifies '-o all', the zprop_get_list() doesn't 2097866Seschrock * normally include the name of the dataset. For 'zfs list', we always 2098866Seschrock * want this property to be first. 2099866Seschrock */ 21005094Slling if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 21015094Slling != 0) 21022082Seschrock usage(B_FALSE); 21032676Seschrock 2104789Sahrens cb.cb_scripted = scripted; 21052082Seschrock cb.cb_first = B_TRUE; 2106789Sahrens 21077538SRichard.Morris@Sun.COM ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist, 21089365SChris.Gerhard@sun.com limit, list_callback, &cb); 21092379Ssjelinek 21105094Slling zprop_free_list(cb.cb_proplist); 21112379Ssjelinek zfs_free_sort_columns(sortcol); 2112789Sahrens 21134221Smmusante if (ret == 0 && cb.cb_first && !cb.cb_scripted) 2114789Sahrens (void) printf(gettext("no datasets available\n")); 2115789Sahrens 2116789Sahrens return (ret); 2117789Sahrens } 2118789Sahrens 2119789Sahrens /* 21204490Svb160487 * zfs rename <fs | snap | vol> <fs | snap | vol> 21214490Svb160487 * zfs rename -p <fs | vol> <fs | vol> 21224490Svb160487 * zfs rename -r <snap> <snap> 2123789Sahrens * 2124789Sahrens * Renames the given dataset to another of the same type. 21254490Svb160487 * 21264490Svb160487 * The '-p' flag creates all the non-existing ancestors of the target first. 2127789Sahrens */ 2128789Sahrens /* ARGSUSED */ 2129789Sahrens static int 2130789Sahrens zfs_do_rename(int argc, char **argv) 2131789Sahrens { 2132789Sahrens zfs_handle_t *zhp; 21334007Smmusante int c; 21342082Seschrock int ret; 21354490Svb160487 boolean_t recurse = B_FALSE; 21364490Svb160487 boolean_t parents = B_FALSE; 2137789Sahrens 2138789Sahrens /* check options */ 21394490Svb160487 while ((c = getopt(argc, argv, "pr")) != -1) { 21404007Smmusante switch (c) { 21414490Svb160487 case 'p': 21424490Svb160487 parents = B_TRUE; 21434490Svb160487 break; 21444007Smmusante case 'r': 21454490Svb160487 recurse = B_TRUE; 21464007Smmusante break; 21474007Smmusante case '?': 21484007Smmusante default: 21494007Smmusante (void) fprintf(stderr, gettext("invalid option '%c'\n"), 21504007Smmusante optopt); 21514007Smmusante usage(B_FALSE); 21524007Smmusante } 2153789Sahrens } 2154789Sahrens 21554007Smmusante argc -= optind; 21564007Smmusante argv += optind; 21574007Smmusante 2158789Sahrens /* check number of arguments */ 21594007Smmusante if (argc < 1) { 2160789Sahrens (void) fprintf(stderr, gettext("missing source dataset " 2161789Sahrens "argument\n")); 21622082Seschrock usage(B_FALSE); 2163789Sahrens } 21644007Smmusante if (argc < 2) { 2165789Sahrens (void) fprintf(stderr, gettext("missing target dataset " 2166789Sahrens "argument\n")); 21672082Seschrock usage(B_FALSE); 2168789Sahrens } 21694007Smmusante if (argc > 2) { 2170789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 21712082Seschrock usage(B_FALSE); 2172789Sahrens } 2173789Sahrens 21744490Svb160487 if (recurse && parents) { 21754490Svb160487 (void) fprintf(stderr, gettext("-p and -r options are mutually " 21764490Svb160487 "exclusive\n")); 21774490Svb160487 usage(B_FALSE); 21784490Svb160487 } 21794490Svb160487 21804007Smmusante if (recurse && strchr(argv[0], '@') == 0) { 21814007Smmusante (void) fprintf(stderr, gettext("source dataset for recursive " 21824007Smmusante "rename must be a snapshot\n")); 21834007Smmusante usage(B_FALSE); 21844007Smmusante } 21854007Smmusante 21864490Svb160487 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM | 21875094Slling ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL) 2188789Sahrens return (1); 2189789Sahrens 21904490Svb160487 /* If we were asked and the name looks good, try to create ancestors. */ 21914490Svb160487 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) && 21924490Svb160487 zfs_create_ancestors(g_zfs, argv[1]) != 0) { 21934490Svb160487 zfs_close(zhp); 21944490Svb160487 return (1); 21954490Svb160487 } 21964490Svb160487 21974007Smmusante ret = (zfs_rename(zhp, argv[1], recurse) != 0); 21982082Seschrock 21992082Seschrock zfs_close(zhp); 22002082Seschrock return (ret); 22012082Seschrock } 22022082Seschrock 22032082Seschrock /* 22042082Seschrock * zfs promote <fs> 22052082Seschrock * 22062082Seschrock * Promotes the given clone fs to be the parent 22072082Seschrock */ 22082082Seschrock /* ARGSUSED */ 22092082Seschrock static int 22102082Seschrock zfs_do_promote(int argc, char **argv) 22112082Seschrock { 22122082Seschrock zfs_handle_t *zhp; 22132082Seschrock int ret; 22142082Seschrock 22152082Seschrock /* check options */ 22162082Seschrock if (argc > 1 && argv[1][0] == '-') { 22172082Seschrock (void) fprintf(stderr, gettext("invalid option '%c'\n"), 22182082Seschrock argv[1][1]); 22192082Seschrock usage(B_FALSE); 22202082Seschrock } 22212082Seschrock 22222082Seschrock /* check number of arguments */ 22232082Seschrock if (argc < 2) { 22242082Seschrock (void) fprintf(stderr, gettext("missing clone filesystem" 22252597Snd150628 " argument\n")); 22262082Seschrock usage(B_FALSE); 22272082Seschrock } 22282082Seschrock if (argc > 2) { 22292082Seschrock (void) fprintf(stderr, gettext("too many arguments\n")); 22302082Seschrock usage(B_FALSE); 22312082Seschrock } 22322082Seschrock 22332082Seschrock zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 22342082Seschrock if (zhp == NULL) 22352082Seschrock return (1); 22362082Seschrock 22372082Seschrock ret = (zfs_promote(zhp) != 0); 22382082Seschrock 22392926Sek110237 2240789Sahrens zfs_close(zhp); 2241789Sahrens return (ret); 2242789Sahrens } 2243789Sahrens 2244789Sahrens /* 22455568Sahrens * zfs rollback [-rRf] <snapshot> 2246789Sahrens * 224711022STom.Erickson@Sun.COM * -r Delete any intervening snapshots before doing rollback 224811022STom.Erickson@Sun.COM * -R Delete any snapshots and their clones 224911022STom.Erickson@Sun.COM * -f ignored for backwards compatability 2250789Sahrens * 2251789Sahrens * Given a filesystem, rollback to a specific snapshot, discarding any changes 2252789Sahrens * since then and making it the active dataset. If more recent snapshots exist, 2253789Sahrens * the command will complain unless the '-r' flag is given. 2254789Sahrens */ 2255789Sahrens typedef struct rollback_cbdata { 2256789Sahrens uint64_t cb_create; 22572082Seschrock boolean_t cb_first; 2258789Sahrens int cb_doclones; 2259789Sahrens char *cb_target; 2260789Sahrens int cb_error; 22612082Seschrock boolean_t cb_recurse; 22622082Seschrock boolean_t cb_dependent; 2263789Sahrens } rollback_cbdata_t; 2264789Sahrens 2265789Sahrens /* 2266789Sahrens * Report any snapshots more recent than the one specified. Used when '-r' is 2267789Sahrens * not specified. We reuse this same callback for the snapshot dependents - if 2268789Sahrens * 'cb_dependent' is set, then this is a dependent and we should report it 2269789Sahrens * without checking the transaction group. 2270789Sahrens */ 2271789Sahrens static int 2272789Sahrens rollback_check(zfs_handle_t *zhp, void *data) 2273789Sahrens { 2274789Sahrens rollback_cbdata_t *cbp = data; 2275789Sahrens 22762082Seschrock if (cbp->cb_doclones) { 22772082Seschrock zfs_close(zhp); 2278789Sahrens return (0); 22792082Seschrock } 2280789Sahrens 2281789Sahrens if (!cbp->cb_dependent) { 2282789Sahrens if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 && 22831294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 2284789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 2285789Sahrens cbp->cb_create) { 2286789Sahrens 2287789Sahrens if (cbp->cb_first && !cbp->cb_recurse) { 2288789Sahrens (void) fprintf(stderr, gettext("cannot " 2289789Sahrens "rollback to '%s': more recent snapshots " 2290789Sahrens "exist\n"), 2291789Sahrens cbp->cb_target); 2292789Sahrens (void) fprintf(stderr, gettext("use '-r' to " 2293789Sahrens "force deletion of the following " 2294789Sahrens "snapshots:\n")); 2295789Sahrens cbp->cb_first = 0; 2296789Sahrens cbp->cb_error = 1; 2297789Sahrens } 2298789Sahrens 2299789Sahrens if (cbp->cb_recurse) { 23002082Seschrock cbp->cb_dependent = B_TRUE; 23012474Seschrock if (zfs_iter_dependents(zhp, B_TRUE, 23022474Seschrock rollback_check, cbp) != 0) { 23032474Seschrock zfs_close(zhp); 23042474Seschrock return (-1); 23052474Seschrock } 23062082Seschrock cbp->cb_dependent = B_FALSE; 2307789Sahrens } else { 2308789Sahrens (void) fprintf(stderr, "%s\n", 2309789Sahrens zfs_get_name(zhp)); 2310789Sahrens } 2311789Sahrens } 2312789Sahrens } else { 2313789Sahrens if (cbp->cb_first && cbp->cb_recurse) { 2314789Sahrens (void) fprintf(stderr, gettext("cannot rollback to " 2315789Sahrens "'%s': clones of previous snapshots exist\n"), 2316789Sahrens cbp->cb_target); 2317789Sahrens (void) fprintf(stderr, gettext("use '-R' to " 2318789Sahrens "force deletion of the following clones and " 2319789Sahrens "dependents:\n")); 2320789Sahrens cbp->cb_first = 0; 2321789Sahrens cbp->cb_error = 1; 2322789Sahrens } 2323789Sahrens 2324789Sahrens (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 2325789Sahrens } 2326789Sahrens 2327789Sahrens zfs_close(zhp); 2328789Sahrens return (0); 2329789Sahrens } 2330789Sahrens 2331789Sahrens static int 2332789Sahrens zfs_do_rollback(int argc, char **argv) 2333789Sahrens { 2334789Sahrens int ret; 2335789Sahrens int c; 23365749Sahrens boolean_t force = B_FALSE; 2337789Sahrens rollback_cbdata_t cb = { 0 }; 2338789Sahrens zfs_handle_t *zhp, *snap; 2339789Sahrens char parentname[ZFS_MAXNAMELEN]; 2340789Sahrens char *delim; 2341789Sahrens 2342789Sahrens /* check options */ 23435568Sahrens while ((c = getopt(argc, argv, "rRf")) != -1) { 2344789Sahrens switch (c) { 2345789Sahrens case 'r': 2346789Sahrens cb.cb_recurse = 1; 2347789Sahrens break; 2348789Sahrens case 'R': 2349789Sahrens cb.cb_recurse = 1; 2350789Sahrens cb.cb_doclones = 1; 2351789Sahrens break; 23525568Sahrens case 'f': 23535749Sahrens force = B_TRUE; 23545568Sahrens break; 2355789Sahrens case '?': 2356789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2357789Sahrens optopt); 23582082Seschrock usage(B_FALSE); 2359789Sahrens } 2360789Sahrens } 2361789Sahrens 2362789Sahrens argc -= optind; 2363789Sahrens argv += optind; 2364789Sahrens 2365789Sahrens /* check number of arguments */ 2366789Sahrens if (argc < 1) { 2367789Sahrens (void) fprintf(stderr, gettext("missing dataset argument\n")); 23682082Seschrock usage(B_FALSE); 2369789Sahrens } 2370789Sahrens if (argc > 1) { 2371789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 23722082Seschrock usage(B_FALSE); 2373789Sahrens } 2374789Sahrens 2375789Sahrens /* open the snapshot */ 23762082Seschrock if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 2377789Sahrens return (1); 2378789Sahrens 23791294Slling /* open the parent dataset */ 23801294Slling (void) strlcpy(parentname, argv[0], sizeof (parentname)); 2381789Sahrens verify((delim = strrchr(parentname, '@')) != NULL); 2382789Sahrens *delim = '\0'; 23835094Slling if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) { 2384789Sahrens zfs_close(snap); 2385789Sahrens return (1); 2386789Sahrens } 2387789Sahrens 2388789Sahrens /* 2389789Sahrens * Check for more recent snapshots and/or clones based on the presence 2390789Sahrens * of '-r' and '-R'. 2391789Sahrens */ 23921294Slling cb.cb_target = argv[0]; 23931294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 23942082Seschrock cb.cb_first = B_TRUE; 2395789Sahrens cb.cb_error = 0; 23962474Seschrock if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0) 23972474Seschrock goto out; 2398789Sahrens 2399789Sahrens if ((ret = cb.cb_error) != 0) 2400789Sahrens goto out; 2401789Sahrens 2402789Sahrens /* 24031294Slling * Rollback parent to the given snapshot. 2404789Sahrens */ 24055749Sahrens ret = zfs_rollback(zhp, snap, force); 2406789Sahrens 2407789Sahrens out: 2408789Sahrens zfs_close(snap); 2409789Sahrens zfs_close(zhp); 2410789Sahrens 2411789Sahrens if (ret == 0) 2412789Sahrens return (0); 2413789Sahrens else 2414789Sahrens return (1); 2415789Sahrens } 2416789Sahrens 2417789Sahrens /* 2418789Sahrens * zfs set property=value { fs | snap | vol } ... 2419789Sahrens * 2420789Sahrens * Sets the given property for all datasets specified on the command line. 2421789Sahrens */ 2422789Sahrens typedef struct set_cbdata { 2423789Sahrens char *cb_propname; 2424789Sahrens char *cb_value; 2425789Sahrens } set_cbdata_t; 2426789Sahrens 2427789Sahrens static int 2428789Sahrens set_callback(zfs_handle_t *zhp, void *data) 2429789Sahrens { 2430789Sahrens set_cbdata_t *cbp = data; 2431789Sahrens 24322676Seschrock if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) { 24332169Snd150628 switch (libzfs_errno(g_zfs)) { 24342169Snd150628 case EZFS_MOUNTFAILED: 24352169Snd150628 (void) fprintf(stderr, gettext("property may be set " 24362169Snd150628 "but unable to remount filesystem\n")); 24372169Snd150628 break; 24383126Sahl case EZFS_SHARENFSFAILED: 24392169Snd150628 (void) fprintf(stderr, gettext("property may be set " 24402169Snd150628 "but unable to reshare filesystem\n")); 24412169Snd150628 break; 24422169Snd150628 } 2443789Sahrens return (1); 24442169Snd150628 } 24452856Snd150628 return (0); 2446789Sahrens } 2447789Sahrens 2448789Sahrens static int 2449789Sahrens zfs_do_set(int argc, char **argv) 2450789Sahrens { 2451789Sahrens set_cbdata_t cb; 24522926Sek110237 int ret; 2453789Sahrens 2454789Sahrens /* check for options */ 2455789Sahrens if (argc > 1 && argv[1][0] == '-') { 2456789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2457789Sahrens argv[1][1]); 24582082Seschrock usage(B_FALSE); 2459789Sahrens } 2460789Sahrens 2461789Sahrens /* check number of arguments */ 2462789Sahrens if (argc < 2) { 2463789Sahrens (void) fprintf(stderr, gettext("missing property=value " 2464789Sahrens "argument\n")); 24652082Seschrock usage(B_FALSE); 2466789Sahrens } 2467789Sahrens if (argc < 3) { 2468789Sahrens (void) fprintf(stderr, gettext("missing dataset name\n")); 24692082Seschrock usage(B_FALSE); 2470789Sahrens } 2471789Sahrens 2472789Sahrens /* validate property=value argument */ 2473789Sahrens cb.cb_propname = argv[1]; 24746993Ssg201626 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) || 24756993Ssg201626 (cb.cb_value[1] == '\0')) { 2476789Sahrens (void) fprintf(stderr, gettext("missing value in " 2477789Sahrens "property=value argument\n")); 24782082Seschrock usage(B_FALSE); 2479789Sahrens } 2480789Sahrens 2481789Sahrens *cb.cb_value = '\0'; 2482789Sahrens cb.cb_value++; 2483789Sahrens 2484789Sahrens if (*cb.cb_propname == '\0') { 2485789Sahrens (void) fprintf(stderr, 2486789Sahrens gettext("missing property in property=value argument\n")); 24872082Seschrock usage(B_FALSE); 2488789Sahrens } 2489789Sahrens 24907538SRichard.Morris@Sun.COM ret = zfs_for_each(argc - 2, argv + 2, NULL, 24919365SChris.Gerhard@sun.com ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb); 24922926Sek110237 24932926Sek110237 return (ret); 2494789Sahrens } 2495789Sahrens 2496789Sahrens /* 24977265Sahrens * zfs snapshot [-r] [-o prop=value] ... <fs@snap> 2498789Sahrens * 2499789Sahrens * Creates a snapshot with the given name. While functionally equivalent to 25005331Samw * 'zfs create', it is a separate command to differentiate intent. 2501789Sahrens */ 2502789Sahrens static int 2503789Sahrens zfs_do_snapshot(int argc, char **argv) 2504789Sahrens { 25054490Svb160487 boolean_t recursive = B_FALSE; 25062199Sahrens int ret; 25072199Sahrens char c; 25087265Sahrens nvlist_t *props; 25097265Sahrens 25107265Sahrens if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 25117265Sahrens (void) fprintf(stderr, gettext("internal error: " 25127265Sahrens "out of memory\n")); 25137265Sahrens return (1); 25147265Sahrens } 25152199Sahrens 2516789Sahrens /* check options */ 25177265Sahrens while ((c = getopt(argc, argv, "ro:")) != -1) { 25182199Sahrens switch (c) { 25197265Sahrens case 'o': 25207265Sahrens if (parseprop(props)) 25217265Sahrens return (1); 25227265Sahrens break; 25232199Sahrens case 'r': 25242199Sahrens recursive = B_TRUE; 25252199Sahrens break; 25262199Sahrens case '?': 25272199Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 25282199Sahrens optopt); 25297265Sahrens goto usage; 25302199Sahrens } 2531789Sahrens } 2532789Sahrens 25332199Sahrens argc -= optind; 25342199Sahrens argv += optind; 25352199Sahrens 2536789Sahrens /* check number of arguments */ 25372199Sahrens if (argc < 1) { 2538789Sahrens (void) fprintf(stderr, gettext("missing snapshot argument\n")); 25397265Sahrens goto usage; 2540789Sahrens } 25412199Sahrens if (argc > 1) { 2542789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 25437265Sahrens goto usage; 2544789Sahrens } 2545789Sahrens 25467265Sahrens ret = zfs_snapshot(g_zfs, argv[0], recursive, props); 25477265Sahrens nvlist_free(props); 25482199Sahrens if (ret && recursive) 25492199Sahrens (void) fprintf(stderr, gettext("no snapshots were created\n")); 25502199Sahrens return (ret != 0); 25517265Sahrens 25527265Sahrens usage: 25537265Sahrens nvlist_free(props); 25547265Sahrens usage(B_FALSE); 25557265Sahrens return (-1); 2556789Sahrens } 2557789Sahrens 2558789Sahrens /* 255911022STom.Erickson@Sun.COM * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap> 256011022STom.Erickson@Sun.COM * zfs send [-vDp] [-i|-I <@snap>] <fs@snap> 2561789Sahrens * 2562789Sahrens * Send a backup stream to stdout. 2563789Sahrens */ 2564789Sahrens static int 25651749Sahrens zfs_do_send(int argc, char **argv) 2566789Sahrens { 2567789Sahrens char *fromname = NULL; 25685367Sahrens char *toname = NULL; 25692885Sahrens char *cp; 25702885Sahrens zfs_handle_t *zhp; 257111007SLori.Alt@Sun.COM sendflags_t flags = { 0 }; 2572789Sahrens int c, err; 257312296SLin.Ling@Sun.COM nvlist_t *dbgnv; 257412296SLin.Ling@Sun.COM boolean_t extraverbose = B_FALSE; 2575789Sahrens 2576789Sahrens /* check options */ 257711022STom.Erickson@Sun.COM while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) { 2578789Sahrens switch (c) { 2579789Sahrens case 'i': 25802885Sahrens if (fromname) 25812885Sahrens usage(B_FALSE); 2582789Sahrens fromname = optarg; 2583789Sahrens break; 25845367Sahrens case 'I': 25855367Sahrens if (fromname) 25865367Sahrens usage(B_FALSE); 25875367Sahrens fromname = optarg; 258811007SLori.Alt@Sun.COM flags.doall = B_TRUE; 25895367Sahrens break; 25905367Sahrens case 'R': 259111007SLori.Alt@Sun.COM flags.replicate = B_TRUE; 25925367Sahrens break; 259311022STom.Erickson@Sun.COM case 'p': 259411022STom.Erickson@Sun.COM flags.props = B_TRUE; 259511022STom.Erickson@Sun.COM break; 25965367Sahrens case 'v': 259712296SLin.Ling@Sun.COM if (flags.verbose) 259812296SLin.Ling@Sun.COM extraverbose = B_TRUE; 259911007SLori.Alt@Sun.COM flags.verbose = B_TRUE; 260011007SLori.Alt@Sun.COM break; 260111007SLori.Alt@Sun.COM case 'D': 260211007SLori.Alt@Sun.COM flags.dedup = B_TRUE; 26035367Sahrens break; 2604789Sahrens case ':': 2605789Sahrens (void) fprintf(stderr, gettext("missing argument for " 2606789Sahrens "'%c' option\n"), optopt); 26072082Seschrock usage(B_FALSE); 2608789Sahrens break; 2609789Sahrens case '?': 2610789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2611789Sahrens optopt); 26122082Seschrock usage(B_FALSE); 2613789Sahrens } 2614789Sahrens } 2615789Sahrens 2616789Sahrens argc -= optind; 2617789Sahrens argv += optind; 2618789Sahrens 2619789Sahrens /* check number of arguments */ 2620789Sahrens if (argc < 1) { 2621789Sahrens (void) fprintf(stderr, gettext("missing snapshot argument\n")); 26222082Seschrock usage(B_FALSE); 2623789Sahrens } 2624789Sahrens if (argc > 1) { 2625789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 26262082Seschrock usage(B_FALSE); 2627789Sahrens } 2628789Sahrens 2629789Sahrens if (isatty(STDOUT_FILENO)) { 2630789Sahrens (void) fprintf(stderr, 26312885Sahrens gettext("Error: Stream can not be written to a terminal.\n" 26323912Slling "You must redirect standard output.\n")); 2633789Sahrens return (1); 2634789Sahrens } 2635789Sahrens 26365367Sahrens cp = strchr(argv[0], '@'); 26375367Sahrens if (cp == NULL) { 26385367Sahrens (void) fprintf(stderr, 26395367Sahrens gettext("argument must be a snapshot\n")); 26405367Sahrens usage(B_FALSE); 26415367Sahrens } 26425367Sahrens *cp = '\0'; 26435367Sahrens toname = cp + 1; 26445367Sahrens zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 26455367Sahrens if (zhp == NULL) 26462665Snd150628 return (1); 26472665Snd150628 26482885Sahrens /* 26492885Sahrens * If they specified the full path to the snapshot, chop off 26505367Sahrens * everything except the short name of the snapshot, but special 26515367Sahrens * case if they specify the origin. 26522885Sahrens */ 26532885Sahrens if (fromname && (cp = strchr(fromname, '@')) != NULL) { 26545367Sahrens char origin[ZFS_MAXNAMELEN]; 26555367Sahrens zprop_source_t src; 26565367Sahrens 26575367Sahrens (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN, 26585367Sahrens origin, sizeof (origin), &src, NULL, 0, B_FALSE); 26595367Sahrens 26605367Sahrens if (strcmp(origin, fromname) == 0) { 26615367Sahrens fromname = NULL; 266211007SLori.Alt@Sun.COM flags.fromorigin = B_TRUE; 26635367Sahrens } else { 26645367Sahrens *cp = '\0'; 26655367Sahrens if (cp != fromname && strcmp(argv[0], fromname)) { 26665367Sahrens (void) fprintf(stderr, 26675367Sahrens gettext("incremental source must be " 26685367Sahrens "in same filesystem\n")); 26695367Sahrens usage(B_FALSE); 26705367Sahrens } 26715367Sahrens fromname = cp + 1; 26725367Sahrens if (strchr(fromname, '@') || strchr(fromname, '/')) { 26735367Sahrens (void) fprintf(stderr, 26745367Sahrens gettext("invalid incremental source\n")); 26755367Sahrens usage(B_FALSE); 26765367Sahrens } 26772885Sahrens } 2678789Sahrens } 2679789Sahrens 268011007SLori.Alt@Sun.COM if (flags.replicate && fromname == NULL) 268111007SLori.Alt@Sun.COM flags.doall = B_TRUE; 268211007SLori.Alt@Sun.COM 268312296SLin.Ling@Sun.COM err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0, 268412296SLin.Ling@Sun.COM extraverbose ? &dbgnv : NULL); 268512296SLin.Ling@Sun.COM 268612296SLin.Ling@Sun.COM if (extraverbose) { 268712296SLin.Ling@Sun.COM /* 268812296SLin.Ling@Sun.COM * dump_nvlist prints to stdout, but that's been 268912296SLin.Ling@Sun.COM * redirected to a file. Make it print to stderr 269012296SLin.Ling@Sun.COM * instead. 269112296SLin.Ling@Sun.COM */ 269212296SLin.Ling@Sun.COM (void) dup2(STDERR_FILENO, STDOUT_FILENO); 269312296SLin.Ling@Sun.COM dump_nvlist(dbgnv, 0); 269412296SLin.Ling@Sun.COM nvlist_free(dbgnv); 269512296SLin.Ling@Sun.COM } 26962885Sahrens zfs_close(zhp); 2697789Sahrens 2698789Sahrens return (err != 0); 2699789Sahrens } 2700789Sahrens 2701789Sahrens /* 2702*12409STom.Erickson@Sun.COM * zfs receive [-vnFu] [-d | -e] <fs@snap> 2703789Sahrens * 2704789Sahrens * Restore a backup stream from stdin. 2705789Sahrens */ 2706789Sahrens static int 27071749Sahrens zfs_do_receive(int argc, char **argv) 2708789Sahrens { 2709789Sahrens int c, err; 271011022STom.Erickson@Sun.COM recvflags_t flags = { 0 }; 271111022STom.Erickson@Sun.COM 2712789Sahrens /* check options */ 271311461STom.Erickson@Sun.COM while ((c = getopt(argc, argv, ":denuvF")) != -1) { 2714789Sahrens switch (c) { 2715789Sahrens case 'd': 27165367Sahrens flags.isprefix = B_TRUE; 2717789Sahrens break; 271811461STom.Erickson@Sun.COM case 'e': 271911461STom.Erickson@Sun.COM flags.isprefix = B_TRUE; 272011461STom.Erickson@Sun.COM flags.istail = B_TRUE; 272111461STom.Erickson@Sun.COM break; 2722789Sahrens case 'n': 27235367Sahrens flags.dryrun = B_TRUE; 2724789Sahrens break; 27258584SLori.Alt@Sun.COM case 'u': 27268584SLori.Alt@Sun.COM flags.nomount = B_TRUE; 27278584SLori.Alt@Sun.COM break; 2728789Sahrens case 'v': 27295367Sahrens flags.verbose = B_TRUE; 2730789Sahrens break; 27312665Snd150628 case 'F': 27325367Sahrens flags.force = B_TRUE; 27332665Snd150628 break; 2734789Sahrens case ':': 2735789Sahrens (void) fprintf(stderr, gettext("missing argument for " 2736789Sahrens "'%c' option\n"), optopt); 27372082Seschrock usage(B_FALSE); 2738789Sahrens break; 2739789Sahrens case '?': 2740789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2741789Sahrens optopt); 27422082Seschrock usage(B_FALSE); 2743789Sahrens } 2744789Sahrens } 2745789Sahrens 2746789Sahrens argc -= optind; 2747789Sahrens argv += optind; 2748789Sahrens 2749789Sahrens /* check number of arguments */ 2750789Sahrens if (argc < 1) { 2751789Sahrens (void) fprintf(stderr, gettext("missing snapshot argument\n")); 27522082Seschrock usage(B_FALSE); 2753789Sahrens } 2754789Sahrens if (argc > 1) { 2755789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 27562082Seschrock usage(B_FALSE); 2757789Sahrens } 2758789Sahrens 2759789Sahrens if (isatty(STDIN_FILENO)) { 2760789Sahrens (void) fprintf(stderr, 2761789Sahrens gettext("Error: Backup stream can not be read " 27623912Slling "from a terminal.\n" 27633912Slling "You must redirect standard input.\n")); 2764789Sahrens return (1); 2765789Sahrens } 2766789Sahrens 27675367Sahrens err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL); 27682926Sek110237 27694543Smarks return (err != 0); 27704543Smarks } 27714543Smarks 277210242Schris.kirby@sun.com static int 277310242Schris.kirby@sun.com zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding) 277410242Schris.kirby@sun.com { 277510242Schris.kirby@sun.com int errors = 0; 277610242Schris.kirby@sun.com int i; 277710242Schris.kirby@sun.com const char *tag; 277810242Schris.kirby@sun.com boolean_t recursive = B_FALSE; 277910342Schris.kirby@sun.com boolean_t temphold = B_FALSE; 278010342Schris.kirby@sun.com const char *opts = holding ? "rt" : "r"; 278110242Schris.kirby@sun.com int c; 278210242Schris.kirby@sun.com 278310242Schris.kirby@sun.com /* check options */ 278410342Schris.kirby@sun.com while ((c = getopt(argc, argv, opts)) != -1) { 278510242Schris.kirby@sun.com switch (c) { 278610242Schris.kirby@sun.com case 'r': 278710242Schris.kirby@sun.com recursive = B_TRUE; 278810242Schris.kirby@sun.com break; 278910342Schris.kirby@sun.com case 't': 279010342Schris.kirby@sun.com temphold = B_TRUE; 279110342Schris.kirby@sun.com break; 279210242Schris.kirby@sun.com case '?': 279310242Schris.kirby@sun.com (void) fprintf(stderr, gettext("invalid option '%c'\n"), 279410242Schris.kirby@sun.com optopt); 279510242Schris.kirby@sun.com usage(B_FALSE); 279610242Schris.kirby@sun.com } 279710242Schris.kirby@sun.com } 279810242Schris.kirby@sun.com 279910242Schris.kirby@sun.com argc -= optind; 280010242Schris.kirby@sun.com argv += optind; 280110242Schris.kirby@sun.com 280210242Schris.kirby@sun.com /* check number of arguments */ 280310242Schris.kirby@sun.com if (argc < 2) 280410242Schris.kirby@sun.com usage(B_FALSE); 280510242Schris.kirby@sun.com 280610242Schris.kirby@sun.com tag = argv[0]; 280710242Schris.kirby@sun.com --argc; 280810242Schris.kirby@sun.com ++argv; 280910242Schris.kirby@sun.com 281010342Schris.kirby@sun.com if (holding && tag[0] == '.') { 281110342Schris.kirby@sun.com /* tags starting with '.' are reserved for libzfs */ 281210342Schris.kirby@sun.com (void) fprintf(stderr, gettext("tag may not start with '.'\n")); 281310342Schris.kirby@sun.com usage(B_FALSE); 281410242Schris.kirby@sun.com } 281510242Schris.kirby@sun.com 281610242Schris.kirby@sun.com for (i = 0; i < argc; ++i) { 281710242Schris.kirby@sun.com zfs_handle_t *zhp; 281810242Schris.kirby@sun.com char parent[ZFS_MAXNAMELEN]; 281910242Schris.kirby@sun.com const char *delim; 282010242Schris.kirby@sun.com char *path = argv[i]; 282110242Schris.kirby@sun.com 282210242Schris.kirby@sun.com delim = strchr(path, '@'); 282310242Schris.kirby@sun.com if (delim == NULL) { 282410242Schris.kirby@sun.com (void) fprintf(stderr, 282510242Schris.kirby@sun.com gettext("'%s' is not a snapshot\n"), path); 282610242Schris.kirby@sun.com ++errors; 282710242Schris.kirby@sun.com continue; 282810242Schris.kirby@sun.com } 282910242Schris.kirby@sun.com (void) strncpy(parent, path, delim - path); 283010242Schris.kirby@sun.com parent[delim - path] = '\0'; 283110242Schris.kirby@sun.com 283210242Schris.kirby@sun.com zhp = zfs_open(g_zfs, parent, 283310242Schris.kirby@sun.com ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 283410242Schris.kirby@sun.com if (zhp == NULL) { 283510242Schris.kirby@sun.com ++errors; 283610242Schris.kirby@sun.com continue; 283710242Schris.kirby@sun.com } 283810342Schris.kirby@sun.com if (holding) { 283910342Schris.kirby@sun.com if (zfs_hold(zhp, delim+1, tag, recursive, 284011417SChris.Kirby@sun.com temphold, B_FALSE) != 0) 284110342Schris.kirby@sun.com ++errors; 284210342Schris.kirby@sun.com } else { 284310342Schris.kirby@sun.com if (zfs_release(zhp, delim+1, tag, recursive) != 0) 284410342Schris.kirby@sun.com ++errors; 284510342Schris.kirby@sun.com } 284610242Schris.kirby@sun.com zfs_close(zhp); 284710242Schris.kirby@sun.com } 284810242Schris.kirby@sun.com 284910242Schris.kirby@sun.com return (errors != 0); 285010242Schris.kirby@sun.com } 285110242Schris.kirby@sun.com 285210242Schris.kirby@sun.com /* 285310342Schris.kirby@sun.com * zfs hold [-r] [-t] <tag> <snap> ... 285410242Schris.kirby@sun.com * 285511022STom.Erickson@Sun.COM * -r Recursively hold 285610342Schris.kirby@sun.com * -t Temporary hold (hidden option) 285710242Schris.kirby@sun.com * 285810242Schris.kirby@sun.com * Apply a user-hold with the given tag to the list of snapshots. 285910242Schris.kirby@sun.com */ 286010242Schris.kirby@sun.com static int 286110242Schris.kirby@sun.com zfs_do_hold(int argc, char **argv) 286210242Schris.kirby@sun.com { 286310242Schris.kirby@sun.com return (zfs_do_hold_rele_impl(argc, argv, B_TRUE)); 286410242Schris.kirby@sun.com } 286510242Schris.kirby@sun.com 286610242Schris.kirby@sun.com /* 286710242Schris.kirby@sun.com * zfs release [-r] <tag> <snap> ... 286810242Schris.kirby@sun.com * 286911022STom.Erickson@Sun.COM * -r Recursively release 287010242Schris.kirby@sun.com * 287110242Schris.kirby@sun.com * Release a user-hold with the given tag from the list of snapshots. 287210242Schris.kirby@sun.com */ 287310242Schris.kirby@sun.com static int 287410242Schris.kirby@sun.com zfs_do_release(int argc, char **argv) 287510242Schris.kirby@sun.com { 287610242Schris.kirby@sun.com return (zfs_do_hold_rele_impl(argc, argv, B_FALSE)); 287710242Schris.kirby@sun.com } 287810242Schris.kirby@sun.com 28791356Seschrock typedef struct get_all_cbdata { 28801356Seschrock zfs_handle_t **cb_handles; 28811356Seschrock size_t cb_alloc; 28821356Seschrock size_t cb_used; 28833126Sahl uint_t cb_types; 28844737Smmusante boolean_t cb_verbose; 28851356Seschrock } get_all_cbdata_t; 28861356Seschrock 28874737Smmusante #define CHECK_SPINNER 30 28884737Smmusante #define SPINNER_TIME 3 /* seconds */ 28894737Smmusante #define MOUNT_TIME 5 /* seconds */ 28904737Smmusante 28911356Seschrock static int 28923126Sahl get_one_dataset(zfs_handle_t *zhp, void *data) 28931356Seschrock { 28944737Smmusante static char spin[] = { '-', '\\', '|', '/' }; 28954737Smmusante static int spinval = 0; 28964737Smmusante static int spincheck = 0; 28974737Smmusante static time_t last_spin_time = (time_t)0; 28981356Seschrock get_all_cbdata_t *cbp = data; 28993126Sahl zfs_type_t type = zfs_get_type(zhp); 29001356Seschrock 29014737Smmusante if (cbp->cb_verbose) { 29024737Smmusante if (--spincheck < 0) { 29034737Smmusante time_t now = time(NULL); 29044737Smmusante if (last_spin_time + SPINNER_TIME < now) { 29054737Smmusante (void) printf("\b%c", spin[spinval++ % 4]); 29064737Smmusante (void) fflush(stdout); 29074737Smmusante last_spin_time = now; 29084737Smmusante } 29094737Smmusante spincheck = CHECK_SPINNER; 29104737Smmusante } 29114737Smmusante } 29124737Smmusante 29131356Seschrock /* 29143126Sahl * Interate over any nested datasets. 29151356Seschrock */ 29163126Sahl if (type == ZFS_TYPE_FILESYSTEM && 29173126Sahl zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) { 29183265Sahrens zfs_close(zhp); 29193126Sahl return (1); 29203126Sahl } 29213126Sahl 29223126Sahl /* 29233126Sahl * Skip any datasets whose type does not match. 29243126Sahl */ 29253126Sahl if ((type & cbp->cb_types) == 0) { 29261356Seschrock zfs_close(zhp); 29271356Seschrock return (0); 29281356Seschrock } 29291356Seschrock 29301356Seschrock if (cbp->cb_alloc == cbp->cb_used) { 29311356Seschrock zfs_handle_t **handles; 29321356Seschrock 29331356Seschrock if (cbp->cb_alloc == 0) 29341356Seschrock cbp->cb_alloc = 64; 29351356Seschrock else 29361356Seschrock cbp->cb_alloc *= 2; 29371356Seschrock 29381356Seschrock handles = safe_malloc(cbp->cb_alloc * sizeof (void *)); 29391356Seschrock 29401356Seschrock if (cbp->cb_handles) { 29411356Seschrock bcopy(cbp->cb_handles, handles, 29421356Seschrock cbp->cb_used * sizeof (void *)); 29431356Seschrock free(cbp->cb_handles); 29441356Seschrock } 29451356Seschrock 29461356Seschrock cbp->cb_handles = handles; 29471356Seschrock } 29481356Seschrock 29491356Seschrock cbp->cb_handles[cbp->cb_used++] = zhp; 29501356Seschrock 29513126Sahl return (0); 29521356Seschrock } 29531356Seschrock 29541356Seschrock static void 29554737Smmusante get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count, 29564737Smmusante boolean_t verbose) 29571356Seschrock { 29581356Seschrock get_all_cbdata_t cb = { 0 }; 29593126Sahl cb.cb_types = types; 29604737Smmusante cb.cb_verbose = verbose; 29614737Smmusante 29624737Smmusante if (verbose) { 29634737Smmusante (void) printf("%s: *", gettext("Reading ZFS config")); 29644737Smmusante (void) fflush(stdout); 29654737Smmusante } 29663126Sahl 29673126Sahl (void) zfs_iter_root(g_zfs, get_one_dataset, &cb); 29683126Sahl 29693126Sahl *dslist = cb.cb_handles; 29701356Seschrock *count = cb.cb_used; 29714737Smmusante 29724737Smmusante if (verbose) { 29734737Smmusante (void) printf("\b%s\n", gettext("done.")); 29744737Smmusante } 29751356Seschrock } 29761356Seschrock 29771356Seschrock static int 29783126Sahl dataset_cmp(const void *a, const void *b) 29791356Seschrock { 29801356Seschrock zfs_handle_t **za = (zfs_handle_t **)a; 29811356Seschrock zfs_handle_t **zb = (zfs_handle_t **)b; 29821356Seschrock char mounta[MAXPATHLEN]; 29831356Seschrock char mountb[MAXPATHLEN]; 29843126Sahl boolean_t gota, gotb; 29853126Sahl 29863126Sahl if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 29873126Sahl verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 29883126Sahl sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 29893126Sahl if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 29903126Sahl verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 29913126Sahl sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 29923126Sahl 29933126Sahl if (gota && gotb) 29943126Sahl return (strcmp(mounta, mountb)); 29953126Sahl 29963126Sahl if (gota) 29973126Sahl return (-1); 29983126Sahl if (gotb) 29993126Sahl return (1); 30003126Sahl 30013126Sahl return (strcmp(zfs_get_name(a), zfs_get_name(b))); 30021356Seschrock } 3003789Sahrens 3004789Sahrens /* 3005789Sahrens * Generic callback for sharing or mounting filesystems. Because the code is so 3006789Sahrens * similar, we have a common function with an extra parameter to determine which 3007789Sahrens * mode we are using. 3008789Sahrens */ 3009789Sahrens #define OP_SHARE 0x1 3010789Sahrens #define OP_MOUNT 0x2 3011789Sahrens 3012789Sahrens /* 30133126Sahl * Share or mount a dataset. 3014789Sahrens */ 3015789Sahrens static int 30165331Samw share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, 30175331Samw boolean_t explicit, const char *options) 3018789Sahrens { 3019789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 3020789Sahrens char shareopts[ZFS_MAXPROPLEN]; 30215331Samw char smbshareopts[ZFS_MAXPROPLEN]; 30223126Sahl const char *cmdname = op == OP_SHARE ? "share" : "mount"; 3023789Sahrens struct mnttab mnt; 30242676Seschrock uint64_t zoned, canmount; 30253126Sahl zfs_type_t type = zfs_get_type(zhp); 30265331Samw boolean_t shared_nfs, shared_smb; 30273126Sahl 30283126Sahl assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)); 30293126Sahl 30303126Sahl if (type == ZFS_TYPE_FILESYSTEM) { 30313126Sahl /* 30323126Sahl * Check to make sure we can mount/share this dataset. If we 30333126Sahl * are in the global zone and the filesystem is exported to a 30343126Sahl * local zone, or if we are in a local zone and the 30353126Sahl * filesystem is not exported, then it is an error. 30363126Sahl */ 30373126Sahl zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 30383126Sahl 30393126Sahl if (zoned && getzoneid() == GLOBAL_ZONEID) { 30403126Sahl if (!explicit) 30413126Sahl return (0); 30423126Sahl 30433126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': " 30443126Sahl "dataset is exported to a local zone\n"), cmdname, 30453126Sahl zfs_get_name(zhp)); 30463126Sahl return (1); 30473126Sahl 30483126Sahl } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 30493126Sahl if (!explicit) 30503126Sahl return (0); 30513126Sahl 30523126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': " 30533126Sahl "permission denied\n"), cmdname, 30543126Sahl zfs_get_name(zhp)); 30553126Sahl return (1); 30563126Sahl } 30573126Sahl 30583126Sahl /* 30593126Sahl * Ignore any filesystems which don't apply to us. This 30603126Sahl * includes those with a legacy mountpoint, or those with 30613126Sahl * legacy share options. 30623126Sahl */ 30633126Sahl verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 30643126Sahl sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 30653126Sahl verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 30663126Sahl sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 30675331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts, 30685331Samw sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0); 30693126Sahl 30705331Samw if (op == OP_SHARE && strcmp(shareopts, "off") == 0 && 30715331Samw strcmp(smbshareopts, "off") == 0) { 30723126Sahl if (!explicit) 3073789Sahrens return (0); 3074789Sahrens 3075789Sahrens (void) fprintf(stderr, gettext("cannot share '%s': " 3076789Sahrens "legacy share\n"), zfs_get_name(zhp)); 3077789Sahrens (void) fprintf(stderr, gettext("use share(1M) to " 30788269SMark.Musante@Sun.COM "share this filesystem, or set " 30798269SMark.Musante@Sun.COM "sharenfs property on\n")); 3080789Sahrens return (1); 3081789Sahrens } 30823126Sahl 30833126Sahl /* 30843126Sahl * We cannot share or mount legacy filesystems. If the 30853126Sahl * shareopts is non-legacy but the mountpoint is legacy, we 30863126Sahl * treat it as a legacy share. 30873126Sahl */ 30883126Sahl if (strcmp(mountpoint, "legacy") == 0) { 30893126Sahl if (!explicit) 30903126Sahl return (0); 30913126Sahl 30923126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': " 30933126Sahl "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 30945367Sahrens (void) fprintf(stderr, gettext("use %s(1M) to " 30955367Sahrens "%s this filesystem\n"), cmdname, cmdname); 30963126Sahl return (1); 30973126Sahl } 30983126Sahl 30993126Sahl if (strcmp(mountpoint, "none") == 0) { 31003126Sahl if (!explicit) 31013126Sahl return (0); 31023126Sahl 31033126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': no " 31043126Sahl "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 31053126Sahl return (1); 31063126Sahl } 31073126Sahl 31086168Shs24103 /* 31096168Shs24103 * canmount explicit outcome 31106168Shs24103 * on no pass through 31116168Shs24103 * on yes pass through 31126168Shs24103 * off no return 0 31136168Shs24103 * off yes display error, return 1 31146168Shs24103 * noauto no return 0 31156168Shs24103 * noauto yes pass through 31166168Shs24103 */ 31178269SMark.Musante@Sun.COM canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 31186168Shs24103 if (canmount == ZFS_CANMOUNT_OFF) { 31193126Sahl if (!explicit) 31203126Sahl return (0); 31213126Sahl 31223126Sahl (void) fprintf(stderr, gettext("cannot %s '%s': " 31233126Sahl "'canmount' property is set to 'off'\n"), cmdname, 31243126Sahl zfs_get_name(zhp)); 31253126Sahl return (1); 31266168Shs24103 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) { 31276168Shs24103 return (0); 31283126Sahl } 31293126Sahl 31303126Sahl /* 31313126Sahl * At this point, we have verified that the mountpoint and/or 31323126Sahl * shareopts are appropriate for auto management. If the 31333126Sahl * filesystem is already mounted or shared, return (failing 31343126Sahl * for explicit requests); otherwise mount or share the 31353126Sahl * filesystem. 31363126Sahl */ 31373126Sahl switch (op) { 31383126Sahl case OP_SHARE: 31395331Samw 31405331Samw shared_nfs = zfs_is_shared_nfs(zhp, NULL); 31415331Samw shared_smb = zfs_is_shared_smb(zhp, NULL); 31425331Samw 31435331Samw if (shared_nfs && shared_smb || 31445331Samw (shared_nfs && strcmp(shareopts, "on") == 0 && 31455331Samw strcmp(smbshareopts, "off") == 0) || 31465331Samw (shared_smb && strcmp(smbshareopts, "on") == 0 && 31475331Samw strcmp(shareopts, "off") == 0)) { 31483126Sahl if (!explicit) 31493126Sahl return (0); 31503126Sahl 3151789Sahrens (void) fprintf(stderr, gettext("cannot share " 3152789Sahrens "'%s': filesystem already shared\n"), 3153789Sahrens zfs_get_name(zhp)); 3154789Sahrens return (1); 3155789Sahrens } 31563126Sahl 31573126Sahl if (!zfs_is_mounted(zhp, NULL) && 31583126Sahl zfs_mount(zhp, NULL, 0) != 0) 31593126Sahl return (1); 31603126Sahl 31615331Samw if (protocol == NULL) { 31625331Samw if (zfs_shareall(zhp) != 0) 31635331Samw return (1); 31645331Samw } else if (strcmp(protocol, "nfs") == 0) { 31655331Samw if (zfs_share_nfs(zhp)) 31665331Samw return (1); 31675331Samw } else if (strcmp(protocol, "smb") == 0) { 31685331Samw if (zfs_share_smb(zhp)) 31695331Samw return (1); 31705331Samw } else { 31715331Samw (void) fprintf(stderr, gettext("cannot share " 31725331Samw "'%s': invalid share type '%s' " 31735331Samw "specified\n"), 31745331Samw zfs_get_name(zhp), protocol); 31753126Sahl return (1); 31765331Samw } 31775331Samw 31783126Sahl break; 31793126Sahl 31803126Sahl case OP_MOUNT: 31813126Sahl if (options == NULL) 31823126Sahl mnt.mnt_mntopts = ""; 31833126Sahl else 31843126Sahl mnt.mnt_mntopts = (char *)options; 31853126Sahl 31863126Sahl if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 31873126Sahl zfs_is_mounted(zhp, NULL)) { 31883126Sahl if (!explicit) 31893126Sahl return (0); 31903126Sahl 3191789Sahrens (void) fprintf(stderr, gettext("cannot mount " 3192789Sahrens "'%s': filesystem already mounted\n"), 3193789Sahrens zfs_get_name(zhp)); 3194789Sahrens return (1); 3195789Sahrens } 31963126Sahl 31973126Sahl if (zfs_mount(zhp, options, flags) != 0) 31983126Sahl return (1); 31993126Sahl break; 3200789Sahrens } 320111876SJames.Dunham@Sun.COM } else 32023126Sahl assert(op == OP_SHARE); 32033126Sahl 3204789Sahrens return (0); 3205789Sahrens } 3206789Sahrens 32074737Smmusante /* 32084737Smmusante * Reports progress in the form "(current/total)". Not thread-safe. 32094737Smmusante */ 32104737Smmusante static void 32114737Smmusante report_mount_progress(int current, int total) 32124737Smmusante { 32134737Smmusante static int len; 32144737Smmusante static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" 32154737Smmusante "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; 32164737Smmusante static time_t last_progress_time; 32174737Smmusante time_t now = time(NULL); 32184737Smmusante 32194737Smmusante /* report 1..n instead of 0..n-1 */ 32204737Smmusante ++current; 32214737Smmusante 32224737Smmusante /* display header if we're here for the first time */ 32234737Smmusante if (current == 1) { 32244737Smmusante (void) printf(gettext("Mounting ZFS filesystems: ")); 32254737Smmusante len = 0; 32265367Sahrens } else if (current != total && last_progress_time + MOUNT_TIME >= now) { 32275367Sahrens /* too soon to report again */ 32285367Sahrens return; 32295367Sahrens } 32304737Smmusante 32314737Smmusante last_progress_time = now; 32324737Smmusante 32334737Smmusante /* back up to prepare for overwriting */ 32344737Smmusante if (len) 32354737Smmusante (void) printf("%*.*s", len, len, reverse); 32364737Smmusante 32374737Smmusante /* We put a newline at the end if this is the last one. */ 32384737Smmusante len = printf("(%d/%d)%s", current, total, current == total ? "\n" : ""); 32394737Smmusante (void) fflush(stdout); 32404737Smmusante } 32414737Smmusante 32426289Smmusante static void 32436289Smmusante append_options(char *mntopts, char *newopts) 32446289Smmusante { 32456289Smmusante int len = strlen(mntopts); 32466289Smmusante 32476289Smmusante /* original length plus new string to append plus 1 for the comma */ 32486289Smmusante if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) { 32496289Smmusante (void) fprintf(stderr, gettext("the opts argument for " 32506289Smmusante "'%c' option is too long (more than %d chars)\n"), 32516289Smmusante "-o", MNT_LINE_MAX); 32526289Smmusante usage(B_FALSE); 32536289Smmusante } 32546289Smmusante 32556289Smmusante if (*mntopts) 32566289Smmusante mntopts[len++] = ','; 32576289Smmusante 32586289Smmusante (void) strcpy(&mntopts[len], newopts); 32596289Smmusante } 32606289Smmusante 3261789Sahrens static int 32623126Sahl share_mount(int op, int argc, char **argv) 3263789Sahrens { 3264789Sahrens int do_all = 0; 32654737Smmusante boolean_t verbose = B_FALSE; 32662372Slling int c, ret = 0; 32676289Smmusante char *options = NULL; 32683126Sahl int types, flags = 0; 3269789Sahrens 3270789Sahrens /* check options */ 32714737Smmusante while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a")) 3272789Sahrens != -1) { 3273789Sahrens switch (c) { 3274789Sahrens case 'a': 3275789Sahrens do_all = 1; 3276789Sahrens break; 32774737Smmusante case 'v': 32784737Smmusante verbose = B_TRUE; 32794737Smmusante break; 3280789Sahrens case 'o': 32816289Smmusante if (*optarg == '\0') { 32826289Smmusante (void) fprintf(stderr, gettext("empty mount " 32836289Smmusante "options (-o) specified\n")); 32846289Smmusante usage(B_FALSE); 32854717Srm160521 } 32866289Smmusante 32876289Smmusante if (options == NULL) 32886289Smmusante options = safe_malloc(MNT_LINE_MAX + 1); 32896289Smmusante 32906289Smmusante /* option validation is done later */ 32916289Smmusante append_options(options, optarg); 3292789Sahrens break; 32934717Srm160521 3294789Sahrens case 'O': 32953126Sahl flags |= MS_OVERLAY; 3296789Sahrens break; 3297789Sahrens case ':': 3298789Sahrens (void) fprintf(stderr, gettext("missing argument for " 3299789Sahrens "'%c' option\n"), optopt); 33002082Seschrock usage(B_FALSE); 3301789Sahrens break; 3302789Sahrens case '?': 3303789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3304789Sahrens optopt); 33052082Seschrock usage(B_FALSE); 3306789Sahrens } 3307789Sahrens } 3308789Sahrens 3309789Sahrens argc -= optind; 3310789Sahrens argv += optind; 3311789Sahrens 3312789Sahrens /* check number of arguments */ 3313789Sahrens if (do_all) { 33143126Sahl zfs_handle_t **dslist = NULL; 33151356Seschrock size_t i, count = 0; 33165331Samw char *protocol = NULL; 33171356Seschrock 33183126Sahl if (op == OP_MOUNT) { 33193126Sahl types = ZFS_TYPE_FILESYSTEM; 33203126Sahl } else if (argc > 0) { 33215331Samw if (strcmp(argv[0], "nfs") == 0 || 33225331Samw strcmp(argv[0], "smb") == 0) { 33233126Sahl types = ZFS_TYPE_FILESYSTEM; 33243126Sahl } else { 33253126Sahl (void) fprintf(stderr, gettext("share type " 332611876SJames.Dunham@Sun.COM "must be 'nfs' or 'smb'\n")); 33273126Sahl usage(B_FALSE); 33283126Sahl } 33295331Samw protocol = argv[0]; 33303126Sahl argc--; 33313126Sahl argv++; 33323126Sahl } else { 33333126Sahl types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 33343126Sahl } 33353126Sahl 3336789Sahrens if (argc != 0) { 3337789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 33382082Seschrock usage(B_FALSE); 3339789Sahrens } 3340789Sahrens 33414737Smmusante get_all_datasets(types, &dslist, &count, verbose); 33421356Seschrock 33431356Seschrock if (count == 0) 33441356Seschrock return (0); 33451356Seschrock 33463126Sahl qsort(dslist, count, sizeof (void *), dataset_cmp); 33471356Seschrock 33481356Seschrock for (i = 0; i < count; i++) { 33494737Smmusante if (verbose) 33504737Smmusante report_mount_progress(i, count); 33514737Smmusante 33525331Samw if (share_mount_one(dslist[i], op, flags, protocol, 33535331Samw B_FALSE, options) != 0) 33542369Slling ret = 1; 33553126Sahl zfs_close(dslist[i]); 33561356Seschrock } 33571356Seschrock 33583126Sahl free(dslist); 3359789Sahrens } else if (argc == 0) { 3360789Sahrens struct mnttab entry; 3361789Sahrens 33624717Srm160521 if ((op == OP_SHARE) || (options != NULL)) { 3363789Sahrens (void) fprintf(stderr, gettext("missing filesystem " 33644717Srm160521 "argument (specify -a for all)\n")); 33652082Seschrock usage(B_FALSE); 3366789Sahrens } 3367789Sahrens 3368789Sahrens /* 3369789Sahrens * When mount is given no arguments, go through /etc/mnttab and 3370789Sahrens * display any active ZFS mounts. We hide any snapshots, since 3371789Sahrens * they are controlled automatically. 3372789Sahrens */ 3373789Sahrens rewind(mnttab_file); 3374789Sahrens while (getmntent(mnttab_file, &entry) == 0) { 3375789Sahrens if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 3376789Sahrens strchr(entry.mnt_special, '@') != NULL) 3377789Sahrens continue; 3378789Sahrens 3379789Sahrens (void) printf("%-30s %s\n", entry.mnt_special, 3380789Sahrens entry.mnt_mountp); 3381789Sahrens } 3382789Sahrens 3383789Sahrens } else { 3384789Sahrens zfs_handle_t *zhp; 3385789Sahrens 33863126Sahl types = ZFS_TYPE_FILESYSTEM; 33873126Sahl if (op == OP_SHARE) 33883126Sahl types |= ZFS_TYPE_VOLUME; 33893126Sahl 3390789Sahrens if (argc > 1) { 3391789Sahrens (void) fprintf(stderr, 3392789Sahrens gettext("too many arguments\n")); 33932082Seschrock usage(B_FALSE); 3394789Sahrens } 3395789Sahrens 33963126Sahl if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) { 3397789Sahrens ret = 1; 33983126Sahl } else { 33995331Samw ret = share_mount_one(zhp, op, flags, NULL, B_TRUE, 34003126Sahl options); 3401789Sahrens zfs_close(zhp); 3402789Sahrens } 3403789Sahrens } 3404789Sahrens 3405789Sahrens return (ret); 3406789Sahrens } 3407789Sahrens 3408789Sahrens /* 340911876SJames.Dunham@Sun.COM * zfs mount -a [nfs] 3410789Sahrens * zfs mount filesystem 3411789Sahrens * 3412789Sahrens * Mount all filesystems, or mount the given filesystem. 3413789Sahrens */ 3414789Sahrens static int 3415789Sahrens zfs_do_mount(int argc, char **argv) 3416789Sahrens { 34173126Sahl return (share_mount(OP_MOUNT, argc, argv)); 3418789Sahrens } 3419789Sahrens 3420789Sahrens /* 342111876SJames.Dunham@Sun.COM * zfs share -a [nfs | smb] 3422789Sahrens * zfs share filesystem 3423789Sahrens * 3424789Sahrens * Share all filesystems, or share the given filesystem. 3425789Sahrens */ 3426789Sahrens static int 3427789Sahrens zfs_do_share(int argc, char **argv) 3428789Sahrens { 34293126Sahl return (share_mount(OP_SHARE, argc, argv)); 3430789Sahrens } 3431789Sahrens 3432789Sahrens typedef struct unshare_unmount_node { 3433789Sahrens zfs_handle_t *un_zhp; 3434789Sahrens char *un_mountp; 3435789Sahrens uu_avl_node_t un_avlnode; 3436789Sahrens } unshare_unmount_node_t; 3437789Sahrens 3438789Sahrens /* ARGSUSED */ 3439789Sahrens static int 3440789Sahrens unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 3441789Sahrens { 3442789Sahrens const unshare_unmount_node_t *l = larg; 3443789Sahrens const unshare_unmount_node_t *r = rarg; 3444789Sahrens 3445789Sahrens return (strcmp(l->un_mountp, r->un_mountp)); 3446789Sahrens } 3447789Sahrens 3448789Sahrens /* 3449789Sahrens * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 3450789Sahrens * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem, 3451789Sahrens * and unmount it appropriately. 3452789Sahrens */ 3453789Sahrens static int 34543126Sahl unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) 3455789Sahrens { 3456789Sahrens zfs_handle_t *zhp; 3457789Sahrens int ret; 3458789Sahrens struct stat64 statbuf; 3459789Sahrens struct extmnttab entry; 34603126Sahl const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount"; 34616289Smmusante ino_t path_inode; 3462789Sahrens 3463789Sahrens /* 3464789Sahrens * Search for the path in /etc/mnttab. Rather than looking for the 3465789Sahrens * specific path, which can be fooled by non-standard paths (i.e. ".." 3466789Sahrens * or "//"), we stat() the path and search for the corresponding 3467789Sahrens * (major,minor) device pair. 3468789Sahrens */ 3469789Sahrens if (stat64(path, &statbuf) != 0) { 3470789Sahrens (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 3471789Sahrens cmdname, path, strerror(errno)); 3472789Sahrens return (1); 3473789Sahrens } 34746289Smmusante path_inode = statbuf.st_ino; 3475789Sahrens 3476789Sahrens /* 3477789Sahrens * Search for the given (major,minor) pair in the mount table. 3478789Sahrens */ 3479789Sahrens rewind(mnttab_file); 3480789Sahrens while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) { 3481789Sahrens if (entry.mnt_major == major(statbuf.st_dev) && 3482789Sahrens entry.mnt_minor == minor(statbuf.st_dev)) 3483789Sahrens break; 3484789Sahrens } 3485789Sahrens if (ret != 0) { 34865904Stimh if (op == OP_SHARE) { 34875904Stimh (void) fprintf(stderr, gettext("cannot %s '%s': not " 34885904Stimh "currently mounted\n"), cmdname, path); 34895904Stimh return (1); 34905904Stimh } 34915904Stimh (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"), 34925904Stimh path); 34935904Stimh if ((ret = umount2(path, flags)) != 0) 34945904Stimh (void) fprintf(stderr, gettext("%s: %s\n"), path, 34955904Stimh strerror(errno)); 34965904Stimh return (ret != 0); 3497789Sahrens } 3498789Sahrens 3499789Sahrens if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 3500789Sahrens (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 3501789Sahrens "filesystem\n"), cmdname, path); 3502789Sahrens return (1); 3503789Sahrens } 3504789Sahrens 35052082Seschrock if ((zhp = zfs_open(g_zfs, entry.mnt_special, 35062082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 3507789Sahrens return (1); 3508789Sahrens 35096289Smmusante ret = 1; 35107386SSachin.Gaikwad@Sun.COM if (stat64(entry.mnt_mountp, &statbuf) != 0) { 35117386SSachin.Gaikwad@Sun.COM (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 35127386SSachin.Gaikwad@Sun.COM cmdname, path, strerror(errno)); 35137386SSachin.Gaikwad@Sun.COM goto out; 35147386SSachin.Gaikwad@Sun.COM } else if (statbuf.st_ino != path_inode) { 35157386SSachin.Gaikwad@Sun.COM (void) fprintf(stderr, gettext("cannot " 35167386SSachin.Gaikwad@Sun.COM "%s '%s': not a mountpoint\n"), cmdname, path); 35177386SSachin.Gaikwad@Sun.COM goto out; 35187386SSachin.Gaikwad@Sun.COM } 35197386SSachin.Gaikwad@Sun.COM 35203126Sahl if (op == OP_SHARE) { 35216289Smmusante char nfs_mnt_prop[ZFS_MAXPROPLEN]; 35226289Smmusante char smbshare_prop[ZFS_MAXPROPLEN]; 35236289Smmusante 35246289Smmusante verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop, 35256289Smmusante sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0); 35266289Smmusante verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop, 35276289Smmusante sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0); 35286289Smmusante 35295331Samw if (strcmp(nfs_mnt_prop, "off") == 0 && 35305331Samw strcmp(smbshare_prop, "off") == 0) { 3531789Sahrens (void) fprintf(stderr, gettext("cannot unshare " 3532789Sahrens "'%s': legacy share\n"), path); 3533789Sahrens (void) fprintf(stderr, gettext("use " 3534789Sahrens "unshare(1M) to unshare this filesystem\n")); 35355331Samw } else if (!zfs_is_shared(zhp)) { 3536789Sahrens (void) fprintf(stderr, gettext("cannot unshare '%s': " 3537789Sahrens "not currently shared\n"), path); 3538789Sahrens } else { 35395331Samw ret = zfs_unshareall_bypath(zhp, path); 3540789Sahrens } 3541789Sahrens } else { 35426289Smmusante char mtpt_prop[ZFS_MAXPROPLEN]; 35436289Smmusante 35446289Smmusante verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop, 35456289Smmusante sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0); 35466289Smmusante 35477386SSachin.Gaikwad@Sun.COM if (is_manual) { 35481264Slling ret = zfs_unmount(zhp, NULL, flags); 35496289Smmusante } else if (strcmp(mtpt_prop, "legacy") == 0) { 35501264Slling (void) fprintf(stderr, gettext("cannot unmount " 35511264Slling "'%s': legacy mountpoint\n"), 35521264Slling zfs_get_name(zhp)); 35531264Slling (void) fprintf(stderr, gettext("use umount(1M) " 35541264Slling "to unmount this filesystem\n")); 3555789Sahrens } else { 3556789Sahrens ret = zfs_unmountall(zhp, flags); 3557789Sahrens } 3558789Sahrens } 3559789Sahrens 35607386SSachin.Gaikwad@Sun.COM out: 3561789Sahrens zfs_close(zhp); 3562789Sahrens 3563789Sahrens return (ret != 0); 3564789Sahrens } 3565789Sahrens 3566789Sahrens /* 3567789Sahrens * Generic callback for unsharing or unmounting a filesystem. 3568789Sahrens */ 3569789Sahrens static int 35703126Sahl unshare_unmount(int op, int argc, char **argv) 3571789Sahrens { 3572789Sahrens int do_all = 0; 3573789Sahrens int flags = 0; 3574789Sahrens int ret = 0; 35753126Sahl int types, c; 3576789Sahrens zfs_handle_t *zhp; 357711876SJames.Dunham@Sun.COM char nfs_mnt_prop[ZFS_MAXPROPLEN]; 35785331Samw char sharesmb[ZFS_MAXPROPLEN]; 3579789Sahrens 3580789Sahrens /* check options */ 35813126Sahl while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) { 3582789Sahrens switch (c) { 3583789Sahrens case 'a': 3584789Sahrens do_all = 1; 3585789Sahrens break; 3586789Sahrens case 'f': 3587789Sahrens flags = MS_FORCE; 3588789Sahrens break; 3589789Sahrens case '?': 3590789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3591789Sahrens optopt); 35922082Seschrock usage(B_FALSE); 3593789Sahrens } 3594789Sahrens } 3595789Sahrens 3596789Sahrens argc -= optind; 3597789Sahrens argv += optind; 3598789Sahrens 3599789Sahrens if (do_all) { 3600789Sahrens /* 3601789Sahrens * We could make use of zfs_for_each() to walk all datasets in 3602789Sahrens * the system, but this would be very inefficient, especially 3603789Sahrens * since we would have to linearly search /etc/mnttab for each 3604789Sahrens * one. Instead, do one pass through /etc/mnttab looking for 3605789Sahrens * zfs entries and call zfs_unmount() for each one. 3606789Sahrens * 3607789Sahrens * Things get a little tricky if the administrator has created 3608789Sahrens * mountpoints beneath other ZFS filesystems. In this case, we 3609789Sahrens * have to unmount the deepest filesystems first. To accomplish 3610789Sahrens * this, we place all the mountpoints in an AVL tree sorted by 3611789Sahrens * the special type (dataset name), and walk the result in 3612789Sahrens * reverse to make sure to get any snapshots first. 3613789Sahrens */ 3614789Sahrens struct mnttab entry; 3615789Sahrens uu_avl_pool_t *pool; 3616789Sahrens uu_avl_t *tree; 3617789Sahrens unshare_unmount_node_t *node; 3618789Sahrens uu_avl_index_t idx; 3619789Sahrens uu_avl_walk_t *walk; 3620789Sahrens 36213126Sahl if (argc != 0) { 36223126Sahl (void) fprintf(stderr, gettext("too many arguments\n")); 36233126Sahl usage(B_FALSE); 36243126Sahl } 36253126Sahl 3626789Sahrens if ((pool = uu_avl_pool_create("unmount_pool", 3627789Sahrens sizeof (unshare_unmount_node_t), 3628789Sahrens offsetof(unshare_unmount_node_t, un_avlnode), 3629789Sahrens unshare_unmount_compare, 3630789Sahrens UU_DEFAULT)) == NULL) { 3631789Sahrens (void) fprintf(stderr, gettext("internal error: " 3632789Sahrens "out of memory\n")); 3633789Sahrens exit(1); 3634789Sahrens } 3635789Sahrens 3636789Sahrens if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) { 3637789Sahrens (void) fprintf(stderr, gettext("internal error: " 3638789Sahrens "out of memory\n")); 3639789Sahrens exit(1); 3640789Sahrens } 3641789Sahrens 3642789Sahrens rewind(mnttab_file); 3643789Sahrens while (getmntent(mnttab_file, &entry) == 0) { 3644789Sahrens 3645789Sahrens /* ignore non-ZFS entries */ 3646789Sahrens if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 3647789Sahrens continue; 3648789Sahrens 3649789Sahrens /* ignore snapshots */ 3650789Sahrens if (strchr(entry.mnt_special, '@') != NULL) 3651789Sahrens continue; 3652789Sahrens 36532082Seschrock if ((zhp = zfs_open(g_zfs, entry.mnt_special, 3654789Sahrens ZFS_TYPE_FILESYSTEM)) == NULL) { 3655789Sahrens ret = 1; 3656789Sahrens continue; 3657789Sahrens } 3658789Sahrens 36595331Samw switch (op) { 36605331Samw case OP_SHARE: 36615331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 366211876SJames.Dunham@Sun.COM nfs_mnt_prop, 366311876SJames.Dunham@Sun.COM sizeof (nfs_mnt_prop), 36645331Samw NULL, NULL, 0, B_FALSE) == 0); 366511876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "off") != 0) 36665331Samw break; 36675331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 366811876SJames.Dunham@Sun.COM nfs_mnt_prop, 366911876SJames.Dunham@Sun.COM sizeof (nfs_mnt_prop), 36705331Samw NULL, NULL, 0, B_FALSE) == 0); 367111876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "off") == 0) 36725331Samw continue; 36735331Samw break; 36745331Samw case OP_MOUNT: 36755331Samw /* Ignore legacy mounts */ 36765331Samw verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, 367711876SJames.Dunham@Sun.COM nfs_mnt_prop, 367811876SJames.Dunham@Sun.COM sizeof (nfs_mnt_prop), 36795331Samw NULL, NULL, 0, B_FALSE) == 0); 368011876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "legacy") == 0) 36815331Samw continue; 36826168Shs24103 /* Ignore canmount=noauto mounts */ 36836168Shs24103 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == 36846168Shs24103 ZFS_CANMOUNT_NOAUTO) 36856168Shs24103 continue; 36865331Samw default: 36875331Samw break; 3688789Sahrens } 3689789Sahrens 3690789Sahrens node = safe_malloc(sizeof (unshare_unmount_node_t)); 3691789Sahrens node->un_zhp = zhp; 3692789Sahrens 3693789Sahrens if ((node->un_mountp = strdup(entry.mnt_mountp)) == 3694789Sahrens NULL) { 3695789Sahrens (void) fprintf(stderr, gettext("internal error:" 3696789Sahrens " out of memory\n")); 3697789Sahrens exit(1); 3698789Sahrens } 3699789Sahrens 3700789Sahrens uu_avl_node_init(node, &node->un_avlnode, pool); 3701789Sahrens 3702789Sahrens if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 3703789Sahrens uu_avl_insert(tree, node, idx); 3704789Sahrens } else { 3705789Sahrens zfs_close(node->un_zhp); 3706789Sahrens free(node->un_mountp); 3707789Sahrens free(node); 3708789Sahrens } 3709789Sahrens } 3710789Sahrens 3711789Sahrens /* 3712789Sahrens * Walk the AVL tree in reverse, unmounting each filesystem and 3713789Sahrens * removing it from the AVL tree in the process. 3714789Sahrens */ 3715789Sahrens if ((walk = uu_avl_walk_start(tree, 3716789Sahrens UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) { 3717789Sahrens (void) fprintf(stderr, 3718789Sahrens gettext("internal error: out of memory")); 3719789Sahrens exit(1); 3720789Sahrens } 3721789Sahrens 3722789Sahrens while ((node = uu_avl_walk_next(walk)) != NULL) { 3723789Sahrens uu_avl_remove(tree, node); 3724789Sahrens 37253126Sahl switch (op) { 3726789Sahrens case OP_SHARE: 37275331Samw if (zfs_unshareall_bypath(node->un_zhp, 3728789Sahrens node->un_mountp) != 0) 3729789Sahrens ret = 1; 3730789Sahrens break; 3731789Sahrens 3732789Sahrens case OP_MOUNT: 3733789Sahrens if (zfs_unmount(node->un_zhp, 3734789Sahrens node->un_mountp, flags) != 0) 3735789Sahrens ret = 1; 3736789Sahrens break; 3737789Sahrens } 3738789Sahrens 3739789Sahrens zfs_close(node->un_zhp); 3740789Sahrens free(node->un_mountp); 3741789Sahrens free(node); 3742789Sahrens } 3743789Sahrens 3744789Sahrens uu_avl_walk_end(walk); 3745789Sahrens uu_avl_destroy(tree); 3746789Sahrens uu_avl_pool_destroy(pool); 37473126Sahl 3748789Sahrens } else { 37493126Sahl if (argc != 1) { 37503126Sahl if (argc == 0) 37513126Sahl (void) fprintf(stderr, 37523126Sahl gettext("missing filesystem argument\n")); 37533126Sahl else 37543126Sahl (void) fprintf(stderr, 37553126Sahl gettext("too many arguments\n")); 37563126Sahl usage(B_FALSE); 37573126Sahl } 37583126Sahl 3759789Sahrens /* 3760789Sahrens * We have an argument, but it may be a full path or a ZFS 3761789Sahrens * filesystem. Pass full paths off to unmount_path() (shared by 3762789Sahrens * manual_unmount), otherwise open the filesystem and pass to 3763789Sahrens * zfs_unmount(). 3764789Sahrens */ 3765789Sahrens if (argv[0][0] == '/') 37663126Sahl return (unshare_unmount_path(op, argv[0], 37673912Slling flags, B_FALSE)); 37682082Seschrock 37693126Sahl types = ZFS_TYPE_FILESYSTEM; 37703126Sahl if (op == OP_SHARE) 37713126Sahl types |= ZFS_TYPE_VOLUME; 37723126Sahl 37733126Sahl if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) 3774789Sahrens return (1); 3775789Sahrens 37763126Sahl if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 37773126Sahl verify(zfs_prop_get(zhp, op == OP_SHARE ? 37785331Samw ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 377911876SJames.Dunham@Sun.COM nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL, 37805331Samw NULL, 0, B_FALSE) == 0); 37813126Sahl 37823126Sahl switch (op) { 37833126Sahl case OP_SHARE: 37845331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 378511876SJames.Dunham@Sun.COM nfs_mnt_prop, 378611876SJames.Dunham@Sun.COM sizeof (nfs_mnt_prop), 37875331Samw NULL, NULL, 0, B_FALSE) == 0); 37885331Samw verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 37895331Samw sharesmb, sizeof (sharesmb), NULL, NULL, 37905331Samw 0, B_FALSE) == 0); 37915331Samw 379211876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "off") == 0 && 37935331Samw strcmp(sharesmb, "off") == 0) { 37943126Sahl (void) fprintf(stderr, gettext("cannot " 37953126Sahl "unshare '%s': legacy share\n"), 37963126Sahl zfs_get_name(zhp)); 37973126Sahl (void) fprintf(stderr, gettext("use " 37983126Sahl "unshare(1M) to unshare this " 37993126Sahl "filesystem\n")); 38003126Sahl ret = 1; 38015331Samw } else if (!zfs_is_shared(zhp)) { 38023126Sahl (void) fprintf(stderr, gettext("cannot " 38033126Sahl "unshare '%s': not currently " 38043126Sahl "shared\n"), zfs_get_name(zhp)); 38053126Sahl ret = 1; 38065331Samw } else if (zfs_unshareall(zhp) != 0) { 38073126Sahl ret = 1; 38083126Sahl } 38093126Sahl break; 38103126Sahl 38113126Sahl case OP_MOUNT: 381211876SJames.Dunham@Sun.COM if (strcmp(nfs_mnt_prop, "legacy") == 0) { 38133126Sahl (void) fprintf(stderr, gettext("cannot " 38143126Sahl "unmount '%s': legacy " 38153126Sahl "mountpoint\n"), zfs_get_name(zhp)); 38163126Sahl (void) fprintf(stderr, gettext("use " 38173126Sahl "umount(1M) to unmount this " 38183126Sahl "filesystem\n")); 38193126Sahl ret = 1; 38203126Sahl } else if (!zfs_is_mounted(zhp, NULL)) { 38213126Sahl (void) fprintf(stderr, gettext("cannot " 38223126Sahl "unmount '%s': not currently " 38233126Sahl "mounted\n"), 38243126Sahl zfs_get_name(zhp)); 38253126Sahl ret = 1; 38263126Sahl } else if (zfs_unmountall(zhp, flags) != 0) { 38273126Sahl ret = 1; 38283126Sahl } 38293126Sahl break; 38303126Sahl } 3831789Sahrens } 3832789Sahrens 3833789Sahrens zfs_close(zhp); 3834789Sahrens } 3835789Sahrens 3836789Sahrens return (ret); 3837789Sahrens } 3838789Sahrens 3839789Sahrens /* 3840789Sahrens * zfs unmount -a 3841789Sahrens * zfs unmount filesystem 3842789Sahrens * 3843789Sahrens * Unmount all filesystems, or a specific ZFS filesystem. 3844789Sahrens */ 3845789Sahrens static int 3846789Sahrens zfs_do_unmount(int argc, char **argv) 3847789Sahrens { 3848789Sahrens return (unshare_unmount(OP_MOUNT, argc, argv)); 3849789Sahrens } 3850789Sahrens 3851789Sahrens /* 3852789Sahrens * zfs unshare -a 3853789Sahrens * zfs unshare filesystem 3854789Sahrens * 3855789Sahrens * Unshare all filesystems, or a specific ZFS filesystem. 3856789Sahrens */ 3857789Sahrens static int 3858789Sahrens zfs_do_unshare(int argc, char **argv) 3859789Sahrens { 3860789Sahrens return (unshare_unmount(OP_SHARE, argc, argv)); 3861789Sahrens } 3862789Sahrens 38639396SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 38649396SMatthew.Ahrens@Sun.COM static int 38659396SMatthew.Ahrens@Sun.COM zfs_do_python(int argc, char **argv) 38669396SMatthew.Ahrens@Sun.COM { 38679396SMatthew.Ahrens@Sun.COM (void) execv(pypath, argv-1); 38689396SMatthew.Ahrens@Sun.COM (void) printf("internal error: %s not found\n", pypath); 38699396SMatthew.Ahrens@Sun.COM return (-1); 38709396SMatthew.Ahrens@Sun.COM } 38719396SMatthew.Ahrens@Sun.COM 3872789Sahrens /* 3873789Sahrens * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is 3874789Sahrens * 'legacy'. Otherwise, complain that use should be using 'zfs mount'. 3875789Sahrens */ 3876789Sahrens static int 3877789Sahrens manual_mount(int argc, char **argv) 3878789Sahrens { 3879789Sahrens zfs_handle_t *zhp; 3880789Sahrens char mountpoint[ZFS_MAXPROPLEN]; 3881789Sahrens char mntopts[MNT_LINE_MAX] = { '\0' }; 3882789Sahrens int ret; 3883789Sahrens int c; 3884789Sahrens int flags = 0; 3885789Sahrens char *dataset, *path; 3886789Sahrens 3887789Sahrens /* check options */ 38881544Seschrock while ((c = getopt(argc, argv, ":mo:O")) != -1) { 3889789Sahrens switch (c) { 3890789Sahrens case 'o': 3891789Sahrens (void) strlcpy(mntopts, optarg, sizeof (mntopts)); 3892789Sahrens break; 3893789Sahrens case 'O': 3894789Sahrens flags |= MS_OVERLAY; 3895789Sahrens break; 38961544Seschrock case 'm': 38971544Seschrock flags |= MS_NOMNTTAB; 38981544Seschrock break; 3899789Sahrens case ':': 3900789Sahrens (void) fprintf(stderr, gettext("missing argument for " 3901789Sahrens "'%c' option\n"), optopt); 39022082Seschrock usage(B_FALSE); 3903789Sahrens break; 3904789Sahrens case '?': 3905789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3906789Sahrens optopt); 3907789Sahrens (void) fprintf(stderr, gettext("usage: mount [-o opts] " 3908789Sahrens "<path>\n")); 3909789Sahrens return (2); 3910789Sahrens } 3911789Sahrens } 3912789Sahrens 3913789Sahrens argc -= optind; 3914789Sahrens argv += optind; 3915789Sahrens 3916789Sahrens /* check that we only have two arguments */ 3917789Sahrens if (argc != 2) { 3918789Sahrens if (argc == 0) 3919789Sahrens (void) fprintf(stderr, gettext("missing dataset " 3920789Sahrens "argument\n")); 3921789Sahrens else if (argc == 1) 3922789Sahrens (void) fprintf(stderr, 3923789Sahrens gettext("missing mountpoint argument\n")); 3924789Sahrens else 3925789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 3926789Sahrens (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n"); 3927789Sahrens return (2); 3928789Sahrens } 3929789Sahrens 3930789Sahrens dataset = argv[0]; 3931789Sahrens path = argv[1]; 3932789Sahrens 3933789Sahrens /* try to open the dataset */ 39342082Seschrock if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) 3935789Sahrens return (1); 3936789Sahrens 3937789Sahrens (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 39382082Seschrock sizeof (mountpoint), NULL, NULL, 0, B_FALSE); 3939789Sahrens 3940789Sahrens /* check for legacy mountpoint and complain appropriately */ 3941789Sahrens ret = 0; 3942789Sahrens if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 3943789Sahrens if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS, 3944789Sahrens NULL, 0, mntopts, sizeof (mntopts)) != 0) { 3945789Sahrens (void) fprintf(stderr, gettext("mount failed: %s\n"), 3946789Sahrens strerror(errno)); 3947789Sahrens ret = 1; 3948789Sahrens } 3949789Sahrens } else { 3950789Sahrens (void) fprintf(stderr, gettext("filesystem '%s' cannot be " 3951789Sahrens "mounted using 'mount -F zfs'\n"), dataset); 3952789Sahrens (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' " 3953789Sahrens "instead.\n"), path); 3954789Sahrens (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' " 3955789Sahrens "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n")); 3956789Sahrens (void) fprintf(stderr, gettext("See zfs(1M) for more " 3957789Sahrens "information.\n")); 3958789Sahrens ret = 1; 3959789Sahrens } 3960789Sahrens 3961789Sahrens return (ret); 3962789Sahrens } 3963789Sahrens 3964789Sahrens /* 3965789Sahrens * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow 3966789Sahrens * unmounts of non-legacy filesystems, as this is the dominant administrative 3967789Sahrens * interface. 3968789Sahrens */ 3969789Sahrens static int 3970789Sahrens manual_unmount(int argc, char **argv) 3971789Sahrens { 3972789Sahrens int flags = 0; 3973789Sahrens int c; 3974789Sahrens 3975789Sahrens /* check options */ 3976789Sahrens while ((c = getopt(argc, argv, "f")) != -1) { 3977789Sahrens switch (c) { 3978789Sahrens case 'f': 3979789Sahrens flags = MS_FORCE; 3980789Sahrens break; 3981789Sahrens case '?': 3982789Sahrens (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3983789Sahrens optopt); 3984789Sahrens (void) fprintf(stderr, gettext("usage: unmount [-f] " 3985789Sahrens "<path>\n")); 3986789Sahrens return (2); 3987789Sahrens } 3988789Sahrens } 3989789Sahrens 3990789Sahrens argc -= optind; 3991789Sahrens argv += optind; 3992789Sahrens 3993789Sahrens /* check arguments */ 3994789Sahrens if (argc != 1) { 3995789Sahrens if (argc == 0) 3996789Sahrens (void) fprintf(stderr, gettext("missing path " 3997789Sahrens "argument\n")); 3998789Sahrens else 3999789Sahrens (void) fprintf(stderr, gettext("too many arguments\n")); 4000789Sahrens (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n")); 4001789Sahrens return (2); 4002789Sahrens } 4003789Sahrens 40042082Seschrock return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE)); 4005789Sahrens } 4006789Sahrens 4007789Sahrens static int 40084543Smarks find_command_idx(char *command, int *idx) 40094543Smarks { 40104543Smarks int i; 40114543Smarks 40124543Smarks for (i = 0; i < NCOMMAND; i++) { 40134543Smarks if (command_table[i].name == NULL) 40144543Smarks continue; 40154543Smarks 40164543Smarks if (strcmp(command, command_table[i].name) == 0) { 40174543Smarks *idx = i; 40184543Smarks return (0); 40194543Smarks } 40204543Smarks } 40214543Smarks return (1); 40224543Smarks } 40234543Smarks 4024789Sahrens int 4025789Sahrens main(int argc, char **argv) 4026789Sahrens { 4027789Sahrens int ret; 4028789Sahrens int i; 4029789Sahrens char *progname; 4030789Sahrens char *cmdname; 4031789Sahrens 4032789Sahrens (void) setlocale(LC_ALL, ""); 4033789Sahrens (void) textdomain(TEXT_DOMAIN); 4034789Sahrens 4035789Sahrens opterr = 0; 4036789Sahrens 40372082Seschrock if ((g_zfs = libzfs_init()) == NULL) { 40382082Seschrock (void) fprintf(stderr, gettext("internal error: failed to " 40392082Seschrock "initialize ZFS library\n")); 40402082Seschrock return (1); 40412082Seschrock } 40422082Seschrock 40434988Sek110237 zpool_set_history_str("zfs", argc, argv, history_str); 40444988Sek110237 verify(zpool_stage_history(g_zfs, history_str) == 0); 40454543Smarks 40462082Seschrock libzfs_print_on_error(g_zfs, B_TRUE); 40472082Seschrock 4048789Sahrens if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) { 4049789Sahrens (void) fprintf(stderr, gettext("internal error: unable to " 4050789Sahrens "open %s\n"), MNTTAB); 4051789Sahrens return (1); 4052789Sahrens } 4053789Sahrens 4054789Sahrens /* 4055789Sahrens * This command also doubles as the /etc/fs mount and unmount program. 4056789Sahrens * Determine if we should take this behavior based on argv[0]. 4057789Sahrens */ 4058789Sahrens progname = basename(argv[0]); 4059789Sahrens if (strcmp(progname, "mount") == 0) { 4060789Sahrens ret = manual_mount(argc, argv); 4061789Sahrens } else if (strcmp(progname, "umount") == 0) { 4062789Sahrens ret = manual_unmount(argc, argv); 4063789Sahrens } else { 4064789Sahrens /* 4065789Sahrens * Make sure the user has specified some command. 4066789Sahrens */ 4067789Sahrens if (argc < 2) { 4068789Sahrens (void) fprintf(stderr, gettext("missing command\n")); 40692082Seschrock usage(B_FALSE); 4070789Sahrens } 4071789Sahrens 4072789Sahrens cmdname = argv[1]; 4073789Sahrens 4074789Sahrens /* 4075789Sahrens * The 'umount' command is an alias for 'unmount' 4076789Sahrens */ 4077789Sahrens if (strcmp(cmdname, "umount") == 0) 4078789Sahrens cmdname = "unmount"; 4079789Sahrens 4080789Sahrens /* 40811749Sahrens * The 'recv' command is an alias for 'receive' 40821749Sahrens */ 40831749Sahrens if (strcmp(cmdname, "recv") == 0) 40841749Sahrens cmdname = "receive"; 40851749Sahrens 40861749Sahrens /* 4087789Sahrens * Special case '-?' 4088789Sahrens */ 4089789Sahrens if (strcmp(cmdname, "-?") == 0) 40902082Seschrock usage(B_TRUE); 4091789Sahrens 4092789Sahrens /* 4093789Sahrens * Run the appropriate command. 4094789Sahrens */ 40958811SEric.Taylor@Sun.COM libzfs_mnttab_cache(g_zfs, B_TRUE); 40964543Smarks if (find_command_idx(cmdname, &i) == 0) { 40974543Smarks current_command = &command_table[i]; 40984543Smarks ret = command_table[i].func(argc - 1, argv + 1); 40994787Sahrens } else if (strchr(cmdname, '=') != NULL) { 41004787Sahrens verify(find_command_idx("set", &i) == 0); 41014787Sahrens current_command = &command_table[i]; 41024787Sahrens ret = command_table[i].func(argc, argv); 41034787Sahrens } else { 4104789Sahrens (void) fprintf(stderr, gettext("unrecognized " 4105789Sahrens "command '%s'\n"), cmdname); 41062082Seschrock usage(B_FALSE); 4107789Sahrens } 41088811SEric.Taylor@Sun.COM libzfs_mnttab_cache(g_zfs, B_FALSE); 4109789Sahrens } 4110789Sahrens 4111789Sahrens (void) fclose(mnttab_file); 4112789Sahrens 41132082Seschrock libzfs_fini(g_zfs); 41142082Seschrock 4115789Sahrens /* 4116789Sahrens * The 'ZFS_ABORT' environment variable causes us to dump core on exit 4117789Sahrens * for the purposes of running ::findleaks. 4118789Sahrens */ 4119789Sahrens if (getenv("ZFS_ABORT") != NULL) { 4120789Sahrens (void) printf("dumping core by request\n"); 4121789Sahrens abort(); 4122789Sahrens } 4123789Sahrens 4124789Sahrens return (ret); 4125789Sahrens } 4126