4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
33 #include <libnvpair.h>
42 #include <sys/mntent.h>
43 #include <sys/mnttab.h>
44 #include <sys/mount.h>
54 libzfs_handle_t *g_zfs;
56 static FILE *mnttab_file;
57 static char history_str[HIS_MAX_RECORD_LEN];
59 static int zfs_do_clone(int argc, char **argv);
60 static int zfs_do_create(int argc, char **argv);
61 static int zfs_do_destroy(int argc, char **argv);
62 static int zfs_do_get(int argc, char **argv);
63 static int zfs_do_inherit(int argc, char **argv);
64 static int zfs_do_list(int argc, char **argv);
65 static int zfs_do_mount(int argc, char **argv);
66 static int zfs_do_rename(int argc, char **argv);
67 static int zfs_do_rollback(int argc, char **argv);
68 static int zfs_do_set(int argc, char **argv);
69 static int zfs_do_upgrade(int argc, char **argv);
70 static int zfs_do_snapshot(int argc, char **argv);
71 static int zfs_do_unmount(int argc, char **argv);
72 static int zfs_do_share(int argc, char **argv);
73 static int zfs_do_unshare(int argc, char **argv);
74 static int zfs_do_send(int argc, char **argv);
75 static int zfs_do_receive(int argc, char **argv);
76 static int zfs_do_promote(int argc, char **argv);
77 static int zfs_do_allow(int argc, char **argv);
78 static int zfs_do_unallow(int argc, char **argv);
79 static int zfs_do_jail(int argc, char **argv);
80 static int zfs_do_unjail(int argc, char **argv);
83 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
88 _umem_debug_init(void)
90 return ("default,verbose"); /* $UMEM_DEBUG setting */
94 _umem_logging_init(void)
96 return ("fail,contents"); /* $UMEM_LOGGING setting */
125 typedef struct zfs_command {
127 int (*func)(int argc, char **argv);
132 * Master command table. Each ZFS command has a name, associated function, and
133 * usage message. The usage messages need to be internationalized, so we have
134 * to have a function to return the usage message based on a command index.
136 * These commands are organized according to how they are displayed in the usage
137 * message. An empty command (one with a NULL name) indicates an empty line in
138 * the generic usage message.
140 static zfs_command_t command_table[] = {
141 { "create", zfs_do_create, HELP_CREATE },
142 { "destroy", zfs_do_destroy, HELP_DESTROY },
144 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
145 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
146 { "clone", zfs_do_clone, HELP_CLONE },
147 { "promote", zfs_do_promote, HELP_PROMOTE },
148 { "rename", zfs_do_rename, HELP_RENAME },
150 { "list", zfs_do_list, HELP_LIST },
152 { "set", zfs_do_set, HELP_SET },
153 { "get", zfs_do_get, HELP_GET },
154 { "inherit", zfs_do_inherit, HELP_INHERIT },
155 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
157 { "mount", zfs_do_mount, HELP_MOUNT },
158 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
159 { "share", zfs_do_share, HELP_SHARE },
160 { "unshare", zfs_do_unshare, HELP_UNSHARE },
162 { "send", zfs_do_send, HELP_SEND },
163 { "receive", zfs_do_receive, HELP_RECEIVE },
165 { "allow", zfs_do_allow, HELP_ALLOW },
167 { "unallow", zfs_do_unallow, HELP_UNALLOW },
169 { "jail", zfs_do_jail, HELP_JAIL },
170 { "unjail", zfs_do_unjail, HELP_UNJAIL },
173 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
175 zfs_command_t *current_command;
178 get_usage(zfs_help_t idx)
182 return (gettext("\tclone [-p] [-o property=value] ... "
183 "<snapshot> <filesystem|volume>\n"));
185 return (gettext("\tcreate [-p] [-o property=value] ... "
187 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
188 "-V <size> <volume>\n"));
190 return (gettext("\tdestroy [-rRf] "
191 "<filesystem|volume|snapshot>\n"));
193 return (gettext("\tget [-rHp] [-o field[,...]] "
194 "[-s source[,...]]\n"
195 "\t <\"all\" | property[,...]> "
196 "[filesystem|volume|snapshot] ...\n"));
198 return (gettext("\tinherit [-r] <property> "
199 "<filesystem|volume|snapshot> ...\n"));
201 return (gettext("\tupgrade [-v]\n"
202 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
204 return (gettext("\tjail <jailid> <filesystem>\n"));
206 return (gettext("\tunjail <jailid> <filesystem>\n"));
208 return (gettext("\tlist [-rH] [-o property[,...]] "
209 "[-t type[,...]] [-s property] ...\n"
210 "\t [-S property] ... "
211 "[filesystem|volume|snapshot] ...\n"));
213 return (gettext("\tmount\n"
214 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
216 return (gettext("\tpromote <clone-filesystem>\n"));
218 return (gettext("\treceive [-vnF] <filesystem|volume|"
220 "\treceive [-vnF] -d <filesystem>\n"));
222 return (gettext("\trename <filesystem|volume|snapshot> "
223 "<filesystem|volume|snapshot>\n"
224 "\trename -p <filesystem|volume> <filesystem|volume>\n"
225 "\trename -r <snapshot> <snapshot>"));
227 return (gettext("\trollback [-rRf] <snapshot>\n"));
229 return (gettext("\tsend [-R] [-[iI] snapshot] <snapshot>\n"));
231 return (gettext("\tset <property=value> "
232 "<filesystem|volume|snapshot> ...\n"));
234 return (gettext("\tshare <-a | filesystem>\n"));
236 return (gettext("\tsnapshot [-r] [-o property=value] ... "
237 "<filesystem@snapname|volume@snapname>\n"));
239 return (gettext("\tunmount [-f] "
240 "<-a | filesystem|mountpoint>\n"));
242 return (gettext("\tunshare [-f] "
243 "<-a | filesystem|mountpoint>\n"));
245 return (gettext("\tallow [-ldug] "
246 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
247 "\t <filesystem|volume>\n"
248 "\tallow [-ld] -e <perm|@setname>[,...] "
249 "<filesystem|volume>\n"
250 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
251 "\tallow -s @setname <perm|@setname>[,...] "
252 "<filesystem|volume>\n"));
254 return (gettext("\tunallow [-rldug] "
255 "<\"everyone\"|user|group>[,...]\n"
256 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
257 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
258 "<filesystem|volume>\n"
259 "\tunallow [-r] -c [<perm|@setname>[,...]] "
260 "<filesystem|volume>\n"
261 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
262 "<filesystem|volume>\n"));
270 * Utility function to guarantee malloc() success.
273 safe_malloc(size_t size)
277 if ((data = calloc(1, size)) == NULL) {
278 (void) fprintf(stderr, "internal error: out of memory\n");
286 * Callback routine that will print out information for each of
290 usage_prop_cb(int prop, void *cb)
294 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
296 if (zfs_prop_readonly(prop))
297 (void) fprintf(fp, " NO ");
299 (void) fprintf(fp, "YES ");
301 if (zfs_prop_inheritable(prop))
302 (void) fprintf(fp, " YES ");
304 (void) fprintf(fp, " NO ");
306 if (zfs_prop_values(prop) == NULL)
307 (void) fprintf(fp, "-\n");
309 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
315 * Display usage message. If we're inside a command, display only the usage for
316 * that command. Otherwise, iterate over the entire command table and display
317 * a complete usage message.
320 usage(boolean_t requested)
323 boolean_t show_properties = B_FALSE;
324 boolean_t show_permissions = B_FALSE;
325 FILE *fp = requested ? stdout : stderr;
327 if (current_command == NULL) {
329 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
331 gettext("where 'command' is one of the following:\n\n"));
333 for (i = 0; i < NCOMMAND; i++) {
334 if (command_table[i].name == NULL)
335 (void) fprintf(fp, "\n");
337 (void) fprintf(fp, "%s",
338 get_usage(command_table[i].usage));
341 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
342 "pool/[dataset/]*dataset[@name]\n"));
344 (void) fprintf(fp, gettext("usage:\n"));
345 (void) fprintf(fp, "%s", get_usage(current_command->usage));
348 if (current_command != NULL &&
349 (strcmp(current_command->name, "set") == 0 ||
350 strcmp(current_command->name, "get") == 0 ||
351 strcmp(current_command->name, "inherit") == 0 ||
352 strcmp(current_command->name, "list") == 0))
353 show_properties = B_TRUE;
355 if (current_command != NULL &&
356 (strcmp(current_command->name, "allow") == 0 ||
357 strcmp(current_command->name, "unallow") == 0))
358 show_permissions = B_TRUE;
360 if (show_properties) {
363 gettext("\nThe following properties are supported:\n"));
365 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
366 "PROPERTY", "EDIT", "INHERIT", "VALUES");
368 /* Iterate over all properties */
369 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
372 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
373 "with standard units such as K, M, G, etc.\n"));
374 (void) fprintf(fp, gettext("\nUser-defined properties can "
375 "be specified by using a name containing a colon (:).\n"));
377 } else if (show_permissions) {
379 gettext("\nThe following permissions are supported:\n"));
381 zfs_deleg_permissions();
385 * "zfs set|get" must not be localised this is the
386 * command name and arguments.
390 gettext("\nFor the property list, run: zfs set|get\n"));
393 gettext("\nFor the delegated permission list, run:"
394 " zfs allow|unallow\n"));
398 * See comments at end of main().
400 if (getenv("ZFS_ABORT") != NULL) {
401 (void) printf("dumping core by request\n");
405 exit(requested ? 0 : 2);
409 parseprop(nvlist_t *props)
411 char *propname = optarg;
412 char *propval, *strval;
414 if ((propval = strchr(propname, '=')) == NULL) {
415 (void) fprintf(stderr, gettext("missing "
416 "'=' for -o option\n"));
421 if (nvlist_lookup_string(props, propname, &strval) == 0) {
422 (void) fprintf(stderr, gettext("property '%s' "
423 "specified multiple times\n"), propname);
426 if (nvlist_add_string(props, propname, propval) != 0) {
427 (void) fprintf(stderr, gettext("internal "
428 "error: out of memory\n"));
436 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
438 * Given an existing dataset, create a writable copy whose initial contents
439 * are the same as the source. The newly created dataset maintains a
440 * dependency on the original; the original cannot be destroyed so long as
443 * The '-p' flag creates all the non-existing ancestors of the target first.
446 zfs_do_clone(int argc, char **argv)
448 zfs_handle_t *zhp = NULL;
449 boolean_t parents = B_FALSE;
454 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
455 (void) fprintf(stderr, gettext("internal error: "
461 while ((c = getopt(argc, argv, "o:p")) != -1) {
464 if (parseprop(props))
471 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
480 /* check number of arguments */
482 (void) fprintf(stderr, gettext("missing source dataset "
487 (void) fprintf(stderr, gettext("missing target dataset "
492 (void) fprintf(stderr, gettext("too many arguments\n"));
496 /* open the source dataset */
497 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
500 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
503 * Now create the ancestors of the target dataset. If the
504 * target already exists and '-p' option was used we should not
507 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
510 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
515 ret = zfs_clone(zhp, argv[1], props);
517 /* create the mountpoint if necessary */
521 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
523 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
524 ret = zfs_share(clone);
543 * zfs create [-p] [-o prop=value] ... fs
544 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
546 * Create a new dataset. This command can be used to create filesystems
547 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
548 * For volumes, the user must specify a size to be used.
550 * The '-s' flag applies only to volumes, and indicates that we should not try
551 * to set the reservation for this volume. By default we set a reservation
552 * equal to the size for any volume. For pools with SPA_VERSION >=
553 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
555 * The '-p' flag creates all the non-existing ancestors of the target first.
558 zfs_do_create(int argc, char **argv)
560 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
561 zfs_handle_t *zhp = NULL;
564 boolean_t noreserve = B_FALSE;
565 boolean_t bflag = B_FALSE;
566 boolean_t parents = B_FALSE;
572 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
573 (void) fprintf(stderr, gettext("internal error: "
579 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
582 type = ZFS_TYPE_VOLUME;
583 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
584 (void) fprintf(stderr, gettext("bad volume "
585 "size '%s': %s\n"), optarg,
586 libzfs_error_description(g_zfs));
590 if (nvlist_add_uint64(props,
591 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
593 (void) fprintf(stderr, gettext("internal "
594 "error: out of memory\n"));
604 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
605 (void) fprintf(stderr, gettext("bad volume "
606 "block size '%s': %s\n"), optarg,
607 libzfs_error_description(g_zfs));
611 if (nvlist_add_uint64(props,
612 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
614 (void) fprintf(stderr, gettext("internal "
615 "error: out of memory\n"));
620 if (parseprop(props))
627 (void) fprintf(stderr, gettext("missing size "
632 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
638 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
639 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
640 "used when creating a volume\n"));
647 /* check number of arguments */
649 (void) fprintf(stderr, gettext("missing %s argument\n"),
650 zfs_type_to_name(type));
654 (void) fprintf(stderr, gettext("too many arguments\n"));
658 if (type == ZFS_TYPE_VOLUME && !noreserve) {
659 zpool_handle_t *zpool_handle;
660 uint64_t spa_version;
662 zfs_prop_t resv_prop;
665 if (p = strchr(argv[0], '/'))
667 zpool_handle = zpool_open(g_zfs, argv[0]);
670 if (zpool_handle == NULL)
672 spa_version = zpool_get_prop_int(zpool_handle,
673 ZPOOL_PROP_VERSION, NULL);
674 zpool_close(zpool_handle);
675 if (spa_version >= SPA_VERSION_REFRESERVATION)
676 resv_prop = ZFS_PROP_REFRESERVATION;
678 resv_prop = ZFS_PROP_RESERVATION;
680 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
682 if (nvlist_add_uint64(props,
683 zfs_prop_to_name(resv_prop), volsize) != 0) {
684 (void) fprintf(stderr, gettext("internal "
685 "error: out of memory\n"));
692 if (parents && zfs_name_valid(argv[0], type)) {
694 * Now create the ancestors of target dataset. If the target
695 * already exists and '-p' option was used we should not
698 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
702 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
707 if (zfs_create(g_zfs, argv[0], type, props) != 0)
710 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
713 * if the user doesn't want the dataset automatically mounted,
714 * then skip the mount/share step
717 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
720 * Mount and/or share the new filesystem as appropriate. We provide a
721 * verbose error message to let the user know that their filesystem was
722 * in fact created, even if we failed to mount or share it.
725 if (canmount == ZFS_CANMOUNT_ON) {
726 if (zfs_mount(zhp, NULL, 0) != 0) {
727 (void) fprintf(stderr, gettext("filesystem "
728 "successfully created, but not mounted\n"));
730 } else if (zfs_share(zhp) != 0) {
731 (void) fprintf(stderr, gettext("filesystem "
732 "successfully created, but not shared\n"));
749 * zfs destroy [-rf] <fs, snap, vol>
751 * -r Recursively destroy all children
752 * -R Recursively destroy all dependents, including clones
753 * -f Force unmounting of any dependents
755 * Destroys the given dataset. By default, it will unmount any filesystems,
756 * and refuse to destroy a dataset that has any dependents. A dependent can
757 * either be a child, or a clone of a child.
759 typedef struct destroy_cbdata {
766 boolean_t cb_closezhp;
767 zfs_handle_t *cb_target;
772 * Check for any dependents based on the '-r' or '-R' flags.
775 destroy_check_dependent(zfs_handle_t *zhp, void *data)
777 destroy_cbdata_t *cbp = data;
778 const char *tname = zfs_get_name(cbp->cb_target);
779 const char *name = zfs_get_name(zhp);
781 if (strncmp(tname, name, strlen(tname)) == 0 &&
782 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
784 * This is a direct descendant, not a clone somewhere else in
791 (void) fprintf(stderr, gettext("cannot destroy '%s': "
792 "%s has children\n"),
793 zfs_get_name(cbp->cb_target),
794 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
795 (void) fprintf(stderr, gettext("use '-r' to destroy "
796 "the following datasets:\n"));
797 cbp->cb_first = B_FALSE;
801 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
804 * This is a clone. We only want to report this if the '-r'
805 * wasn't specified, or the target is a snapshot.
807 if (!cbp->cb_recurse &&
808 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
812 (void) fprintf(stderr, gettext("cannot destroy '%s': "
813 "%s has dependent clones\n"),
814 zfs_get_name(cbp->cb_target),
815 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
816 (void) fprintf(stderr, gettext("use '-R' to destroy "
817 "the following datasets:\n"));
818 cbp->cb_first = B_FALSE;
822 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
831 destroy_callback(zfs_handle_t *zhp, void *data)
833 destroy_cbdata_t *cbp = data;
836 * Ignore pools (which we've already flagged as an error before getting
839 if (strchr(zfs_get_name(zhp), '/') == NULL &&
840 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
846 * Bail out on the first error.
848 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
849 zfs_destroy(zhp) != 0) {
859 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
861 destroy_cbdata_t *cbp = arg;
862 char thissnap[MAXPATHLEN];
864 boolean_t closezhp = cbp->cb_closezhp;
867 (void) snprintf(thissnap, sizeof (thissnap),
868 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
870 libzfs_print_on_error(g_zfs, B_FALSE);
871 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
872 libzfs_print_on_error(g_zfs, B_TRUE);
875 * Destroy any clones of this snapshot
877 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
887 cbp->cb_closezhp = B_TRUE;
888 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
895 zfs_do_destroy(int argc, char **argv)
897 destroy_cbdata_t cb = { 0 };
903 while ((c = getopt(argc, argv, "frR")) != -1) {
917 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
926 /* check number of arguments */
928 (void) fprintf(stderr, gettext("missing path argument\n"));
932 (void) fprintf(stderr, gettext("too many arguments\n"));
937 * If we are doing recursive destroy of a snapshot, then the
938 * named snapshot may not exist. Go straight to libzfs.
940 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
944 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
949 if (cb.cb_doclones) {
951 if (destroy_snap_clones(zhp, &cb) != 0) {
957 ret = zfs_destroy_snaps(zhp, cp);
960 (void) fprintf(stderr,
961 gettext("no snapshots destroyed\n"));
967 /* Open the given dataset */
968 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
974 * Perform an explicit check for pools before going any further.
976 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
977 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
978 (void) fprintf(stderr, gettext("cannot destroy '%s': "
979 "operation does not apply to pools\n"),
981 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
982 "%s' to destroy all datasets in the pool\n"),
984 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
985 "to destroy the pool itself\n"), zfs_get_name(zhp));
991 * Check for any dependents and/or clones.
993 cb.cb_first = B_TRUE;
994 if (!cb.cb_doclones &&
995 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1002 zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
1008 * Do the real thing. The callback will close the handle regardless of
1009 * whether it succeeds or not.
1012 if (destroy_callback(zhp, &cb) != 0)
1020 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
1021 * < all | property[,property]... > < fs | snap | vol > ...
1023 * -r recurse over any child datasets
1024 * -H scripted mode. Headers are stripped, and fields are separated
1025 * by tabs instead of spaces.
1026 * -o Set of fields to display. One of "name,property,value,source".
1027 * Default is all four.
1028 * -s Set of sources to allow. One of
1029 * "local,default,inherited,temporary,none". Default is all
1031 * -p Display values in parsable (literal) format.
1033 * Prints properties for the given datasets. The user can control which
1034 * columns to display as well as which property types to allow.
1038 * Invoked to display the properties for a single dataset.
1041 get_callback(zfs_handle_t *zhp, void *data)
1043 char buf[ZFS_MAXPROPLEN];
1044 zprop_source_t sourcetype;
1045 char source[ZFS_MAXNAMELEN];
1046 zprop_get_cbdata_t *cbp = data;
1047 nvlist_t *userprop = zfs_get_user_props(zhp);
1048 zprop_list_t *pl = cbp->cb_proplist;
1053 for (; pl != NULL; pl = pl->pl_next) {
1055 * Skip the special fake placeholder. This will also skip over
1056 * the name property when 'all' is specified.
1058 if (pl->pl_prop == ZFS_PROP_NAME &&
1059 pl == cbp->cb_proplist)
1062 if (pl->pl_prop != ZPROP_INVAL) {
1063 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1064 sizeof (buf), &sourcetype, source,
1066 cbp->cb_literal) != 0) {
1069 if (!zfs_prop_valid_for_type(pl->pl_prop,
1070 ZFS_TYPE_DATASET)) {
1071 (void) fprintf(stderr,
1072 gettext("No such property '%s'\n"),
1073 zfs_prop_to_name(pl->pl_prop));
1076 sourcetype = ZPROP_SRC_NONE;
1077 (void) strlcpy(buf, "-", sizeof (buf));
1080 zprop_print_one_property(zfs_get_name(zhp), cbp,
1081 zfs_prop_to_name(pl->pl_prop),
1082 buf, sourcetype, source);
1084 if (nvlist_lookup_nvlist(userprop,
1085 pl->pl_user_prop, &propval) != 0) {
1088 sourcetype = ZPROP_SRC_NONE;
1091 verify(nvlist_lookup_string(propval,
1092 ZPROP_VALUE, &strval) == 0);
1093 verify(nvlist_lookup_string(propval,
1094 ZPROP_SOURCE, &sourceval) == 0);
1096 if (strcmp(sourceval,
1097 zfs_get_name(zhp)) == 0) {
1098 sourcetype = ZPROP_SRC_LOCAL;
1100 sourcetype = ZPROP_SRC_INHERITED;
1101 (void) strlcpy(source,
1102 sourceval, sizeof (source));
1106 zprop_print_one_property(zfs_get_name(zhp), cbp,
1107 pl->pl_user_prop, strval, sourcetype,
1116 zfs_do_get(int argc, char **argv)
1118 zprop_get_cbdata_t cb = { 0 };
1119 int i, c, flags = 0;
1120 char *value, *fields;
1122 zprop_list_t fake_name = { 0 };
1125 * Set up default columns and sources.
1127 cb.cb_sources = ZPROP_SRC_ALL;
1128 cb.cb_columns[0] = GET_COL_NAME;
1129 cb.cb_columns[1] = GET_COL_PROPERTY;
1130 cb.cb_columns[2] = GET_COL_VALUE;
1131 cb.cb_columns[3] = GET_COL_SOURCE;
1132 cb.cb_type = ZFS_TYPE_DATASET;
1135 while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
1138 cb.cb_literal = B_TRUE;
1141 flags |= ZFS_ITER_RECURSE;
1144 cb.cb_scripted = B_TRUE;
1147 (void) fprintf(stderr, gettext("missing argument for "
1148 "'%c' option\n"), optopt);
1153 * Process the set of columns to display. We zero out
1154 * the structure to give us a blank slate.
1156 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1158 while (*optarg != '\0') {
1159 static char *col_subopts[] =
1160 { "name", "property", "value", "source",
1164 (void) fprintf(stderr, gettext("too "
1165 "many fields given to -o "
1170 switch (getsubopt(&optarg, col_subopts,
1173 cb.cb_columns[i++] = GET_COL_NAME;
1176 cb.cb_columns[i++] = GET_COL_PROPERTY;
1179 cb.cb_columns[i++] = GET_COL_VALUE;
1182 cb.cb_columns[i++] = GET_COL_SOURCE;
1185 (void) fprintf(stderr,
1186 gettext("invalid column name "
1195 while (*optarg != '\0') {
1196 static char *source_subopts[] = {
1197 "local", "default", "inherited",
1198 "temporary", "none", NULL };
1200 switch (getsubopt(&optarg, source_subopts,
1203 cb.cb_sources |= ZPROP_SRC_LOCAL;
1206 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1209 cb.cb_sources |= ZPROP_SRC_INHERITED;
1212 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1215 cb.cb_sources |= ZPROP_SRC_NONE;
1218 (void) fprintf(stderr,
1219 gettext("invalid source "
1227 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1237 (void) fprintf(stderr, gettext("missing property "
1244 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1252 * As part of zfs_expand_proplist(), we keep track of the maximum column
1253 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1254 * need to know the maximum name length. However, the user likely did
1255 * not specify 'name' as one of the properties to fetch, so we need to
1256 * make sure we always include at least this property for
1257 * print_get_headers() to work properly.
1259 if (cb.cb_proplist != NULL) {
1260 fake_name.pl_prop = ZFS_PROP_NAME;
1261 fake_name.pl_width = strlen(gettext("NAME"));
1262 fake_name.pl_next = cb.cb_proplist;
1263 cb.cb_proplist = &fake_name;
1266 cb.cb_first = B_TRUE;
1268 /* run for each object */
1269 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1270 &cb.cb_proplist, get_callback, &cb);
1272 if (cb.cb_proplist == &fake_name)
1273 zprop_free_list(fake_name.pl_next);
1275 zprop_free_list(cb.cb_proplist);
1281 * inherit [-r] <property> <fs|vol> ...
1283 * -r Recurse over all children
1285 * For each dataset specified on the command line, inherit the given property
1286 * from its parent. Inheriting a property at the pool level will cause it to
1287 * use the default value. The '-r' flag will recurse over all children, and is
1288 * useful for setting a property on a hierarchy-wide basis, regardless of any
1289 * local modifications for each dataset.
1293 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1295 char *propname = data;
1296 zfs_prop_t prop = zfs_name_to_prop(propname);
1299 * If we're doing it recursively, then ignore properties that
1300 * are not valid for this type of dataset.
1302 if (prop != ZPROP_INVAL &&
1303 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1306 return (zfs_prop_inherit(zhp, propname) != 0);
1310 inherit_cb(zfs_handle_t *zhp, void *data)
1312 char *propname = data;
1314 return (zfs_prop_inherit(zhp, propname) != 0);
1318 zfs_do_inherit(int argc, char **argv)
1327 while ((c = getopt(argc, argv, "r")) != -1) {
1330 flags |= ZFS_ITER_RECURSE;
1334 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1343 /* check number of arguments */
1345 (void) fprintf(stderr, gettext("missing property argument\n"));
1349 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1357 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1358 if (zfs_prop_readonly(prop)) {
1359 (void) fprintf(stderr, gettext(
1360 "%s property is read-only\n"),
1364 if (!zfs_prop_inheritable(prop)) {
1365 (void) fprintf(stderr, gettext("'%s' property cannot "
1366 "be inherited\n"), propname);
1367 if (prop == ZFS_PROP_QUOTA ||
1368 prop == ZFS_PROP_RESERVATION ||
1369 prop == ZFS_PROP_REFQUOTA ||
1370 prop == ZFS_PROP_REFRESERVATION)
1371 (void) fprintf(stderr, gettext("use 'zfs set "
1372 "%s=none' to clear\n"), propname);
1375 } else if (!zfs_prop_user(propname)) {
1376 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1381 if (flags & ZFS_ITER_RECURSE) {
1382 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1383 NULL, NULL, inherit_recurse_cb, propname);
1385 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1386 NULL, NULL, inherit_cb, propname);
1392 typedef struct upgrade_cbdata {
1393 uint64_t cb_numupgraded;
1394 uint64_t cb_numsamegraded;
1395 uint64_t cb_numfailed;
1396 uint64_t cb_version;
1398 boolean_t cb_foundone;
1399 char cb_lastfs[ZFS_MAXNAMELEN];
1403 same_pool(zfs_handle_t *zhp, const char *name)
1405 int len1 = strcspn(name, "/@");
1406 const char *zhname = zfs_get_name(zhp);
1407 int len2 = strcspn(zhname, "/@");
1411 return (strncmp(name, zhname, len1) == 0);
1415 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1417 upgrade_cbdata_t *cb = data;
1418 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1420 /* list if it's old/new */
1421 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1422 (cb->cb_newer && version > ZPL_VERSION)) {
1425 str = gettext("The following filesystems are "
1426 "formatted using a newer software version and\n"
1427 "cannot be accessed on the current system.\n\n");
1429 str = gettext("The following filesystems are "
1430 "out of date, and can be upgraded. After being\n"
1431 "upgraded, these filesystems (and any 'zfs send' "
1432 "streams generated from\n"
1433 "subsequent snapshots) will no longer be "
1434 "accessible by older software versions.\n\n");
1437 if (!cb->cb_foundone) {
1439 (void) printf(gettext("VER FILESYSTEM\n"));
1440 (void) printf(gettext("--- ------------\n"));
1441 cb->cb_foundone = B_TRUE;
1444 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1451 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1453 upgrade_cbdata_t *cb = data;
1454 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1456 if (cb->cb_version >= ZPL_VERSION_FUID) {
1459 if (zfs_spa_version(zhp, &spa_version) < 0)
1462 if (spa_version < SPA_VERSION_FUID) {
1464 (void) printf(gettext("%s: can not be upgraded; "
1465 "the pool version needs to first be upgraded\nto "
1467 zfs_get_name(zhp), SPA_VERSION_FUID);
1474 if (version < cb->cb_version) {
1476 (void) snprintf(verstr, sizeof (verstr),
1477 "%llu", cb->cb_version);
1478 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1480 * If they did "zfs upgrade -a", then we could
1481 * be doing ioctls to different pools. We need
1482 * to log this history once to each pool.
1484 verify(zpool_stage_history(g_zfs, history_str) == 0);
1486 if (zfs_prop_set(zhp, "version", verstr) == 0)
1487 cb->cb_numupgraded++;
1490 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1491 } else if (version > cb->cb_version) {
1492 /* can't downgrade */
1493 (void) printf(gettext("%s: can not be downgraded; "
1494 "it is already at version %u\n"),
1495 zfs_get_name(zhp), version);
1498 cb->cb_numsamegraded++;
1506 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1509 zfs_do_upgrade(int argc, char **argv)
1511 boolean_t all = B_FALSE;
1512 boolean_t showversions = B_FALSE;
1514 upgrade_cbdata_t cb = { 0 };
1516 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1519 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1522 flags |= ZFS_ITER_RECURSE;
1525 showversions = B_TRUE;
1528 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1529 optarg, &cb.cb_version) != 0) {
1530 (void) fprintf(stderr,
1531 gettext("invalid version %s\n"), optarg);
1540 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1549 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1551 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1552 cb.cb_version || argc))
1554 if ((all || argc) && (showversions))
1560 /* Show info on available versions. */
1561 (void) printf(gettext("The following filesystem versions are "
1563 (void) printf(gettext("VER DESCRIPTION\n"));
1564 (void) printf("--- -----------------------------------------"
1565 "---------------\n");
1566 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1567 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1568 (void) printf(gettext(" 3 Case insensitive and File system "
1569 "unique identifer (FUID)\n"));
1570 (void) printf(gettext("\nFor more information on a particular "
1571 "version, including supported releases, see:\n\n"));
1572 (void) printf("http://www.opensolaris.org/os/community/zfs/"
1573 "version/zpl/N\n\n");
1574 (void) printf(gettext("Where 'N' is the version number.\n"));
1576 } else if (argc || all) {
1577 /* Upgrade filesystems */
1578 if (cb.cb_version == 0)
1579 cb.cb_version = ZPL_VERSION;
1580 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1581 NULL, NULL, upgrade_set_callback, &cb);
1582 (void) printf(gettext("%llu filesystems upgraded\n"),
1584 if (cb.cb_numsamegraded) {
1585 (void) printf(gettext("%llu filesystems already at "
1587 cb.cb_numsamegraded);
1589 if (cb.cb_numfailed != 0)
1592 /* List old-version filesytems */
1594 (void) printf(gettext("This system is currently running "
1595 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1597 flags |= ZFS_ITER_RECURSE;
1598 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1599 NULL, NULL, upgrade_list_callback, &cb);
1601 found = cb.cb_foundone;
1602 cb.cb_foundone = B_FALSE;
1603 cb.cb_newer = B_TRUE;
1605 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1606 NULL, NULL, upgrade_list_callback, &cb);
1608 if (!cb.cb_foundone && !found) {
1609 (void) printf(gettext("All filesystems are "
1610 "formatted with the current version.\n"));
1618 * list [-rH] [-o property[,property]...] [-t type[,type]...]
1619 * [-s property [-s property]...] [-S property [-S property]...]
1622 * -r Recurse over all children
1623 * -H Scripted mode; elide headers and separate columns by tabs
1624 * -o Control which fields to display.
1625 * -t Control which object types to display.
1626 * -s Specify sort columns, descending order.
1627 * -S Specify sort columns, ascending order.
1629 * When given no arguments, lists all filesystems in the system.
1630 * Otherwise, list the specified datasets, optionally recursing down them if
1631 * '-r' is specified.
1633 typedef struct list_cbdata {
1635 boolean_t cb_scripted;
1636 zprop_list_t *cb_proplist;
1640 * Given a list of columns to display, output appropriate headers for each one.
1643 print_header(zprop_list_t *pl)
1645 char headerbuf[ZFS_MAXPROPLEN];
1648 boolean_t first = B_TRUE;
1649 boolean_t right_justify;
1651 for (; pl != NULL; pl = pl->pl_next) {
1658 right_justify = B_FALSE;
1659 if (pl->pl_prop != ZPROP_INVAL) {
1660 header = zfs_prop_column_name(pl->pl_prop);
1661 right_justify = zfs_prop_align_right(pl->pl_prop);
1663 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1664 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1665 headerbuf[i] = '\0';
1669 if (pl->pl_next == NULL && !right_justify)
1670 (void) printf("%s", header);
1671 else if (right_justify)
1672 (void) printf("%*s", pl->pl_width, header);
1674 (void) printf("%-*s", pl->pl_width, header);
1677 (void) printf("\n");
1681 * Given a dataset and a list of fields, print out all the properties according
1682 * to the described layout.
1685 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1687 boolean_t first = B_TRUE;
1688 char property[ZFS_MAXPROPLEN];
1689 nvlist_t *userprops = zfs_get_user_props(zhp);
1692 boolean_t right_justify;
1695 for (; pl != NULL; pl = pl->pl_next) {
1698 (void) printf("\t");
1705 right_justify = B_FALSE;
1706 if (pl->pl_prop != ZPROP_INVAL) {
1707 if (zfs_prop_get(zhp, pl->pl_prop, property,
1708 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1713 right_justify = zfs_prop_align_right(pl->pl_prop);
1715 if (nvlist_lookup_nvlist(userprops,
1716 pl->pl_user_prop, &propval) != 0)
1719 verify(nvlist_lookup_string(propval,
1720 ZPROP_VALUE, &propstr) == 0);
1723 width = pl->pl_width;
1726 * If this is being called in scripted mode, or if this is the
1727 * last column and it is left-justified, don't include a width
1730 if (scripted || (pl->pl_next == NULL && !right_justify))
1731 (void) printf("%s", propstr);
1732 else if (right_justify)
1733 (void) printf("%*s", width, propstr);
1735 (void) printf("%-*s", width, propstr);
1738 (void) printf("\n");
1742 * Generic callback function to list a dataset or snapshot.
1745 list_callback(zfs_handle_t *zhp, void *data)
1747 list_cbdata_t *cbp = data;
1749 if (cbp->cb_first) {
1750 if (!cbp->cb_scripted)
1751 print_header(cbp->cb_proplist);
1752 cbp->cb_first = B_FALSE;
1755 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1761 zfs_do_list(int argc, char **argv)
1764 boolean_t scripted = B_FALSE;
1765 static char default_fields[] =
1766 "name,used,available,referenced,mountpoint";
1767 int types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
1768 boolean_t types_specified = B_FALSE;
1769 char *fields = NULL;
1770 list_cbdata_t cb = { 0 };
1773 zfs_sort_column_t *sortcol = NULL;
1774 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
1777 while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
1783 flags |= ZFS_ITER_RECURSE;
1789 if (zfs_add_sort_column(&sortcol, optarg,
1791 (void) fprintf(stderr,
1792 gettext("invalid property '%s'\n"), optarg);
1797 if (zfs_add_sort_column(&sortcol, optarg,
1799 (void) fprintf(stderr,
1800 gettext("invalid property '%s'\n"), optarg);
1806 types_specified = B_TRUE;
1807 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1808 while (*optarg != '\0') {
1809 static char *type_subopts[] = { "filesystem",
1810 "volume", "snapshot", "all", NULL };
1812 switch (getsubopt(&optarg, type_subopts,
1815 types |= ZFS_TYPE_FILESYSTEM;
1818 types |= ZFS_TYPE_VOLUME;
1821 types |= ZFS_TYPE_SNAPSHOT;
1824 types = ZFS_TYPE_DATASET;
1828 (void) fprintf(stderr,
1829 gettext("invalid type '%s'\n"),
1836 (void) fprintf(stderr, gettext("missing argument for "
1837 "'%c' option\n"), optopt);
1841 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1851 fields = default_fields;
1854 * If "-o space" and no types were specified, don't display snapshots.
1856 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
1857 types &= ~ZFS_TYPE_SNAPSHOT;
1860 * If the user specifies '-o all', the zprop_get_list() doesn't
1861 * normally include the name of the dataset. For 'zfs list', we always
1862 * want this property to be first.
1864 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1868 cb.cb_scripted = scripted;
1869 cb.cb_first = B_TRUE;
1871 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
1872 list_callback, &cb);
1874 zprop_free_list(cb.cb_proplist);
1875 zfs_free_sort_columns(sortcol);
1877 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
1878 (void) printf(gettext("no datasets available\n"));
1884 * zfs rename <fs | snap | vol> <fs | snap | vol>
1885 * zfs rename -p <fs | vol> <fs | vol>
1886 * zfs rename -r <snap> <snap>
1888 * Renames the given dataset to another of the same type.
1890 * The '-p' flag creates all the non-existing ancestors of the target first.
1894 zfs_do_rename(int argc, char **argv)
1899 boolean_t recurse = B_FALSE;
1900 boolean_t parents = B_FALSE;
1903 while ((c = getopt(argc, argv, "pr")) != -1) {
1913 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1922 /* check number of arguments */
1924 (void) fprintf(stderr, gettext("missing source dataset "
1929 (void) fprintf(stderr, gettext("missing target dataset "
1934 (void) fprintf(stderr, gettext("too many arguments\n"));
1938 if (recurse && parents) {
1939 (void) fprintf(stderr, gettext("-p and -r options are mutually "
1944 if (recurse && strchr(argv[0], '@') == 0) {
1945 (void) fprintf(stderr, gettext("source dataset for recursive "
1946 "rename must be a snapshot\n"));
1950 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
1951 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
1954 /* If we were asked and the name looks good, try to create ancestors. */
1955 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
1956 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
1961 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
1970 * Promotes the given clone fs to be the parent
1974 zfs_do_promote(int argc, char **argv)
1980 if (argc > 1 && argv[1][0] == '-') {
1981 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1986 /* check number of arguments */
1988 (void) fprintf(stderr, gettext("missing clone filesystem"
1993 (void) fprintf(stderr, gettext("too many arguments\n"));
1997 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2001 ret = (zfs_promote(zhp) != 0);
2009 * zfs rollback [-rRf] <snapshot>
2011 * -r Delete any intervening snapshots before doing rollback
2012 * -R Delete any snapshots and their clones
2013 * -f ignored for backwards compatability
2015 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2016 * since then and making it the active dataset. If more recent snapshots exist,
2017 * the command will complain unless the '-r' flag is given.
2019 typedef struct rollback_cbdata {
2025 boolean_t cb_recurse;
2026 boolean_t cb_dependent;
2027 } rollback_cbdata_t;
2030 * Report any snapshots more recent than the one specified. Used when '-r' is
2031 * not specified. We reuse this same callback for the snapshot dependents - if
2032 * 'cb_dependent' is set, then this is a dependent and we should report it
2033 * without checking the transaction group.
2036 rollback_check(zfs_handle_t *zhp, void *data)
2038 rollback_cbdata_t *cbp = data;
2040 if (cbp->cb_doclones) {
2045 if (!cbp->cb_dependent) {
2046 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2047 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2048 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2051 if (cbp->cb_first && !cbp->cb_recurse) {
2052 (void) fprintf(stderr, gettext("cannot "
2053 "rollback to '%s': more recent snapshots "
2056 (void) fprintf(stderr, gettext("use '-r' to "
2057 "force deletion of the following "
2063 if (cbp->cb_recurse) {
2064 cbp->cb_dependent = B_TRUE;
2065 if (zfs_iter_dependents(zhp, B_TRUE,
2066 rollback_check, cbp) != 0) {
2070 cbp->cb_dependent = B_FALSE;
2072 (void) fprintf(stderr, "%s\n",
2077 if (cbp->cb_first && cbp->cb_recurse) {
2078 (void) fprintf(stderr, gettext("cannot rollback to "
2079 "'%s': clones of previous snapshots exist\n"),
2081 (void) fprintf(stderr, gettext("use '-R' to "
2082 "force deletion of the following clones and "
2088 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2096 zfs_do_rollback(int argc, char **argv)
2100 boolean_t force = B_FALSE;
2101 rollback_cbdata_t cb = { 0 };
2102 zfs_handle_t *zhp, *snap;
2103 char parentname[ZFS_MAXNAMELEN];
2107 while ((c = getopt(argc, argv, "rRf")) != -1) {
2120 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2129 /* check number of arguments */
2131 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2135 (void) fprintf(stderr, gettext("too many arguments\n"));
2139 /* open the snapshot */
2140 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2143 /* open the parent dataset */
2144 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2145 verify((delim = strrchr(parentname, '@')) != NULL);
2147 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2153 * Check for more recent snapshots and/or clones based on the presence
2156 cb.cb_target = argv[0];
2157 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2158 cb.cb_first = B_TRUE;
2160 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2163 if ((ret = cb.cb_error) != 0)
2167 * Rollback parent to the given snapshot.
2169 ret = zfs_rollback(zhp, snap, force);
2182 * zfs set property=value { fs | snap | vol } ...
2184 * Sets the given property for all datasets specified on the command line.
2186 typedef struct set_cbdata {
2192 set_callback(zfs_handle_t *zhp, void *data)
2194 set_cbdata_t *cbp = data;
2196 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2197 switch (libzfs_errno(g_zfs)) {
2198 case EZFS_MOUNTFAILED:
2199 (void) fprintf(stderr, gettext("property may be set "
2200 "but unable to remount filesystem\n"));
2202 case EZFS_SHARENFSFAILED:
2203 (void) fprintf(stderr, gettext("property may be set "
2204 "but unable to reshare filesystem\n"));
2213 zfs_do_set(int argc, char **argv)
2218 /* check for options */
2219 if (argc > 1 && argv[1][0] == '-') {
2220 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2225 /* check number of arguments */
2227 (void) fprintf(stderr, gettext("missing property=value "
2232 (void) fprintf(stderr, gettext("missing dataset name\n"));
2236 /* validate property=value argument */
2237 cb.cb_propname = argv[1];
2238 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2239 (cb.cb_value[1] == '\0')) {
2240 (void) fprintf(stderr, gettext("missing value in "
2241 "property=value argument\n"));
2245 *cb.cb_value = '\0';
2248 if (*cb.cb_propname == '\0') {
2249 (void) fprintf(stderr,
2250 gettext("missing property in property=value argument\n"));
2254 ret = zfs_for_each(argc - 2, argv + 2, NULL,
2255 ZFS_TYPE_DATASET, NULL, NULL, set_callback, &cb);
2261 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2263 * Creates a snapshot with the given name. While functionally equivalent to
2264 * 'zfs create', it is a separate command to differentiate intent.
2267 zfs_do_snapshot(int argc, char **argv)
2269 boolean_t recursive = B_FALSE;
2274 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2275 (void) fprintf(stderr, gettext("internal error: "
2276 "out of memory\n"));
2281 while ((c = getopt(argc, argv, "ro:")) != -1) {
2284 if (parseprop(props))
2291 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2300 /* check number of arguments */
2302 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2306 (void) fprintf(stderr, gettext("too many arguments\n"));
2310 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2312 if (ret && recursive)
2313 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2323 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2324 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2326 * Send a backup stream to stdout.
2329 zfs_do_send(int argc, char **argv)
2331 char *fromname = NULL;
2332 char *toname = NULL;
2335 boolean_t doall = B_FALSE;
2336 boolean_t replicate = B_FALSE;
2337 boolean_t fromorigin = B_FALSE;
2338 boolean_t verbose = B_FALSE;
2342 while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
2362 (void) fprintf(stderr, gettext("missing argument for "
2363 "'%c' option\n"), optopt);
2367 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2376 /* check number of arguments */
2378 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2382 (void) fprintf(stderr, gettext("too many arguments\n"));
2386 if (isatty(STDOUT_FILENO)) {
2387 (void) fprintf(stderr,
2388 gettext("Error: Stream can not be written to a terminal.\n"
2389 "You must redirect standard output.\n"));
2393 cp = strchr(argv[0], '@');
2395 (void) fprintf(stderr,
2396 gettext("argument must be a snapshot\n"));
2401 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2406 * If they specified the full path to the snapshot, chop off
2407 * everything except the short name of the snapshot, but special
2408 * case if they specify the origin.
2410 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2411 char origin[ZFS_MAXNAMELEN];
2414 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2415 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2417 if (strcmp(origin, fromname) == 0) {
2419 fromorigin = B_TRUE;
2422 if (cp != fromname && strcmp(argv[0], fromname)) {
2423 (void) fprintf(stderr,
2424 gettext("incremental source must be "
2425 "in same filesystem\n"));
2429 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2430 (void) fprintf(stderr,
2431 gettext("invalid incremental source\n"));
2437 if (replicate && fromname == NULL)
2440 err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
2441 verbose, STDOUT_FILENO);
2448 * zfs receive [-dnvF] <fs@snap>
2450 * Restore a backup stream from stdin.
2453 zfs_do_receive(int argc, char **argv)
2458 bzero(&flags, sizeof (recvflags_t));
2460 while ((c = getopt(argc, argv, ":dnvF")) != -1) {
2463 flags.isprefix = B_TRUE;
2466 flags.dryrun = B_TRUE;
2469 flags.verbose = B_TRUE;
2472 flags.force = B_TRUE;
2475 (void) fprintf(stderr, gettext("missing argument for "
2476 "'%c' option\n"), optopt);
2480 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2489 /* check number of arguments */
2491 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2495 (void) fprintf(stderr, gettext("too many arguments\n"));
2499 if (isatty(STDIN_FILENO)) {
2500 (void) fprintf(stderr,
2501 gettext("Error: Backup stream can not be read "
2502 "from a terminal.\n"
2503 "You must redirect standard input.\n"));
2507 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2512 typedef struct allow_cb {
2514 size_t a_treeoffset;
2518 zfs_print_perms(avl_tree_t *tree)
2520 zfs_perm_node_t *permnode;
2522 permnode = avl_first(tree);
2523 while (permnode != NULL) {
2524 (void) printf("%s", permnode->z_pname);
2525 permnode = AVL_NEXT(tree, permnode);
2529 (void) printf("\n");
2534 * Iterate over user/groups/everyone/... and the call perm_iter
2535 * function to print actual permission when tree has >0 nodes.
2538 zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb)
2540 zfs_allow_node_t *item;
2543 item = avl_first(tree);
2545 ptree = (void *)((char *)item + cb->a_treeoffset);
2546 if (avl_numnodes(ptree)) {
2547 if (cb->a_permcnt++ == 0)
2548 (void) printf("%s\n", banner);
2549 (void) printf("\t%s", item->z_key);
2551 * Avoid an extra space being printed
2552 * for "everyone" which is keyed with a null
2555 if (item->z_key[0] != '\0')
2557 zfs_print_perms(ptree);
2559 item = AVL_NEXT(tree, item);
2563 #define LINES "-------------------------------------------------------------\n"
2565 zfs_print_allows(char *ds)
2567 zfs_allow_t *curperms, *perms;
2569 allow_cb_t allowcb = { 0 };
2570 char banner[MAXPATHLEN];
2575 if (strrchr(ds, '@')) {
2576 (void) fprintf(stderr, gettext("Snapshots don't have 'allow'"
2580 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2583 if (zfs_perm_get(zhp, &perms)) {
2584 (void) fprintf(stderr,
2585 gettext("Failed to retrieve 'allows' on %s\n"), ds);
2593 (void) printf("%s", LINES);
2594 for (curperms = perms; curperms; curperms = curperms->z_next) {
2596 (void) snprintf(banner, sizeof (banner),
2597 "Permission sets on (%s)", curperms->z_setpoint);
2598 allowcb.a_treeoffset =
2599 offsetof(zfs_allow_node_t, z_localdescend);
2600 allowcb.a_permcnt = 0;
2601 zfs_iter_perms(&curperms->z_sets, banner, &allowcb);
2603 (void) snprintf(banner, sizeof (banner),
2604 "Create time permissions on (%s)", curperms->z_setpoint);
2605 allowcb.a_treeoffset =
2606 offsetof(zfs_allow_node_t, z_localdescend);
2607 allowcb.a_permcnt = 0;
2608 zfs_iter_perms(&curperms->z_crperms, banner, &allowcb);
2611 (void) snprintf(banner, sizeof (banner),
2612 "Local permissions on (%s)", curperms->z_setpoint);
2613 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local);
2614 allowcb.a_permcnt = 0;
2615 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2616 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2617 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2619 (void) snprintf(banner, sizeof (banner),
2620 "Descendent permissions on (%s)", curperms->z_setpoint);
2621 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend);
2622 allowcb.a_permcnt = 0;
2623 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2624 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2625 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2627 (void) snprintf(banner, sizeof (banner),
2628 "Local+Descendent permissions on (%s)",
2629 curperms->z_setpoint);
2630 allowcb.a_treeoffset =
2631 offsetof(zfs_allow_node_t, z_localdescend);
2632 allowcb.a_permcnt = 0;
2633 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2634 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2635 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2637 (void) printf("%s", LINES);
2639 zfs_free_allows(perms);
2643 #define ALLOWOPTIONS "ldcsu:g:e"
2644 #define UNALLOWOPTIONS "ldcsu:g:er"
2647 * Validate options, and build necessary datastructure to display/remove/add
2649 * Returns 0 - If permissions should be added/removed
2650 * Returns 1 - If permissions should be displayed.
2651 * Returns -1 - on failure
2654 parse_allow_args(int *argc, char **argv[], boolean_t unallow,
2655 char **ds, int *recurse, nvlist_t **zperms)
2658 char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS;
2659 zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE;
2660 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
2665 while ((c = getopt(*argc, *argv, options)) != -1) {
2668 if (who_type == ZFS_DELEG_CREATE ||
2669 who_type == ZFS_DELEG_NAMED_SET)
2672 deleg_type |= ZFS_DELEG_PERM_LOCAL;
2675 if (who_type == ZFS_DELEG_CREATE ||
2676 who_type == ZFS_DELEG_NAMED_SET)
2679 deleg_type |= ZFS_DELEG_PERM_DESCENDENT;
2685 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2689 who_type = ZFS_DELEG_CREATE;
2692 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2696 who_type = ZFS_DELEG_NAMED_SET;
2699 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2701 who_type = ZFS_DELEG_USER;
2705 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2707 who_type = ZFS_DELEG_GROUP;
2711 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2713 who_type = ZFS_DELEG_EVERYONE;
2721 if (deleg_type == 0)
2722 deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT;
2727 if (unallow == B_FALSE && *argc == 1) {
2729 * Only print permissions if no options were processed
2738 * initialize variables for zfs_build_perms based on number
2740 * 3 arguments ==> zfs [un]allow joe perm,perm,perm <dataset> or
2741 * zfs [un]allow -s @set1 perm,perm <dataset>
2742 * 2 arguments ==> zfs [un]allow -c perm,perm <dataset> or
2743 * zfs [un]allow -u|-g <name> perm <dataset> or
2744 * zfs [un]allow -e perm,perm <dataset>
2745 * zfs unallow joe <dataset>
2746 * zfs unallow -s @set1 <dataset>
2747 * 1 argument ==> zfs [un]allow -e <dataset> or
2748 * zfs [un]allow -c <dataset>
2758 * advance argc/argv for do_allow cases.
2759 * for do_allow case make sure who have a know who type
2760 * and its not a permission set.
2762 if (unallow == B_TRUE) {
2765 } else if (who_type != ZFS_DELEG_WHO_UNKNOWN &&
2766 who_type != ZFS_DELEG_NAMED_SET)
2771 if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE ||
2772 who_type == ZFS_DELEG_CREATE || who != NULL)) {
2776 if (unallow == B_FALSE &&
2777 (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2778 who_type == ZFS_DELEG_NAMED_SET))
2780 else if (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2781 who_type == ZFS_DELEG_NAMED_SET)
2783 else if (who_type != ZFS_DELEG_NAMED_SET)
2787 if (unallow == B_TRUE) {
2794 if (unallow == B_FALSE)
2796 if (who == NULL && who_type != ZFS_DELEG_CREATE &&
2797 who_type != ZFS_DELEG_EVERYONE)
2806 if (strrchr(*ds, '@')) {
2807 (void) fprintf(stderr,
2808 gettext("Can't set or remove 'allow' permissions "
2809 "on snapshots.\n"));
2813 if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_DATASET)) == NULL)
2816 if ((zfs_build_perms(zhp, who, perms,
2817 who_type, deleg_type, zperms)) != 0) {
2826 zfs_do_allow(int argc, char **argv)
2829 nvlist_t *zperms = NULL;
2834 if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds,
2835 &unused, &zperms)) == -1)
2839 return (zfs_print_allows(argv[0]));
2841 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2844 if (zfs_perm_set(zhp, zperms)) {
2846 nvlist_free(zperms);
2849 nvlist_free(zperms);
2856 unallow_callback(zfs_handle_t *zhp, void *data)
2858 nvlist_t *nvp = (nvlist_t *)data;
2861 error = zfs_perm_remove(zhp, nvp);
2863 (void) fprintf(stderr, gettext("Failed to remove permissions "
2864 "on %s\n"), zfs_get_name(zhp));
2870 zfs_do_unallow(int argc, char **argv)
2872 int recurse = B_FALSE;
2875 nvlist_t *zperms = NULL;
2878 if (parse_allow_args(&argc, &argv, B_TRUE,
2879 &ds, &recurse, &zperms) == -1)
2883 flags |= ZFS_ITER_RECURSE;
2884 error = zfs_for_each(argc, argv, flags,
2885 ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
2886 NULL, unallow_callback, (void *)zperms);
2889 nvlist_free(zperms);
2894 typedef struct get_all_cbdata {
2895 zfs_handle_t **cb_handles;
2899 boolean_t cb_verbose;
2902 #define CHECK_SPINNER 30
2903 #define SPINNER_TIME 3 /* seconds */
2904 #define MOUNT_TIME 5 /* seconds */
2907 get_one_dataset(zfs_handle_t *zhp, void *data)
2909 static char spin[] = { '-', '\\', '|', '/' };
2910 static int spinval = 0;
2911 static int spincheck = 0;
2912 static time_t last_spin_time = (time_t)0;
2913 get_all_cbdata_t *cbp = data;
2914 zfs_type_t type = zfs_get_type(zhp);
2916 if (cbp->cb_verbose) {
2917 if (--spincheck < 0) {
2918 time_t now = time(NULL);
2919 if (last_spin_time + SPINNER_TIME < now) {
2920 (void) printf("\b%c", spin[spinval++ % 4]);
2921 (void) fflush(stdout);
2922 last_spin_time = now;
2924 spincheck = CHECK_SPINNER;
2929 * Interate over any nested datasets.
2931 if (type == ZFS_TYPE_FILESYSTEM &&
2932 zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2938 * Skip any datasets whose type does not match.
2940 if ((type & cbp->cb_types) == 0) {
2945 if (cbp->cb_alloc == cbp->cb_used) {
2946 zfs_handle_t **handles;
2948 if (cbp->cb_alloc == 0)
2953 handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2955 if (cbp->cb_handles) {
2956 bcopy(cbp->cb_handles, handles,
2957 cbp->cb_used * sizeof (void *));
2958 free(cbp->cb_handles);
2961 cbp->cb_handles = handles;
2964 cbp->cb_handles[cbp->cb_used++] = zhp;
2970 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
2973 get_all_cbdata_t cb = { 0 };
2974 cb.cb_types = types;
2975 cb.cb_verbose = verbose;
2978 (void) printf("%s: *", gettext("Reading ZFS config"));
2979 (void) fflush(stdout);
2982 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2984 *dslist = cb.cb_handles;
2985 *count = cb.cb_used;
2988 (void) printf("\b%s\n", gettext("done."));
2993 dataset_cmp(const void *a, const void *b)
2995 zfs_handle_t **za = (zfs_handle_t **)a;
2996 zfs_handle_t **zb = (zfs_handle_t **)b;
2997 char mounta[MAXPATHLEN];
2998 char mountb[MAXPATHLEN];
2999 boolean_t gota, gotb;
3001 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
3002 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
3003 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
3004 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
3005 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
3006 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
3009 return (strcmp(mounta, mountb));
3016 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
3020 * Generic callback for sharing or mounting filesystems. Because the code is so
3021 * similar, we have a common function with an extra parameter to determine which
3022 * mode we are using.
3024 #define OP_SHARE 0x1
3025 #define OP_MOUNT 0x2
3028 * Share or mount a dataset.
3031 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3032 boolean_t explicit, const char *options)
3034 char mountpoint[ZFS_MAXPROPLEN];
3035 char shareopts[ZFS_MAXPROPLEN];
3036 char smbshareopts[ZFS_MAXPROPLEN];
3037 const char *cmdname = op == OP_SHARE ? "share" : "mount";
3039 uint64_t zoned, canmount;
3040 zfs_type_t type = zfs_get_type(zhp);
3041 boolean_t shared_nfs, shared_smb;
3043 assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
3045 if (type == ZFS_TYPE_FILESYSTEM) {
3047 * Check to make sure we can mount/share this dataset. If we
3048 * are in the global zone and the filesystem is exported to a
3049 * local zone, or if we are in a local zone and the
3050 * filesystem is not exported, then it is an error.
3052 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3054 if (zoned && getzoneid() == GLOBAL_ZONEID) {
3058 (void) fprintf(stderr, gettext("cannot %s '%s': "
3059 "dataset is exported to a local zone\n"), cmdname,
3063 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3067 (void) fprintf(stderr, gettext("cannot %s '%s': "
3068 "permission denied\n"), cmdname,
3074 * Ignore any filesystems which don't apply to us. This
3075 * includes those with a legacy mountpoint, or those with
3076 * legacy share options.
3078 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3079 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3080 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3081 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3082 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3083 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3084 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3086 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3087 strcmp(smbshareopts, "off") == 0) {
3091 (void) fprintf(stderr, gettext("cannot share '%s': "
3092 "legacy share\n"), zfs_get_name(zhp));
3093 (void) fprintf(stderr, gettext("use share(1M) to "
3094 "share this filesystem\n"));
3099 * We cannot share or mount legacy filesystems. If the
3100 * shareopts is non-legacy but the mountpoint is legacy, we
3101 * treat it as a legacy share.
3103 if (strcmp(mountpoint, "legacy") == 0) {
3107 (void) fprintf(stderr, gettext("cannot %s '%s': "
3108 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3109 (void) fprintf(stderr, gettext("use %s(1M) to "
3110 "%s this filesystem\n"), cmdname, cmdname);
3114 if (strcmp(mountpoint, "none") == 0) {
3118 (void) fprintf(stderr, gettext("cannot %s '%s': no "
3119 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3124 * canmount explicit outcome
3125 * on no pass through
3126 * on yes pass through
3128 * off yes display error, return 1
3129 * noauto no return 0
3130 * noauto yes pass through
3132 if (canmount == ZFS_CANMOUNT_OFF) {
3136 (void) fprintf(stderr, gettext("cannot %s '%s': "
3137 "'canmount' property is set to 'off'\n"), cmdname,
3140 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3145 * At this point, we have verified that the mountpoint and/or
3146 * shareopts are appropriate for auto management. If the
3147 * filesystem is already mounted or shared, return (failing
3148 * for explicit requests); otherwise mount or share the
3154 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3155 shared_smb = zfs_is_shared_smb(zhp, NULL);
3157 if (shared_nfs && shared_smb ||
3158 (shared_nfs && strcmp(shareopts, "on") == 0 &&
3159 strcmp(smbshareopts, "off") == 0) ||
3160 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3161 strcmp(shareopts, "off") == 0)) {
3165 (void) fprintf(stderr, gettext("cannot share "
3166 "'%s': filesystem already shared\n"),
3171 if (!zfs_is_mounted(zhp, NULL) &&
3172 zfs_mount(zhp, NULL, 0) != 0)
3175 if (protocol == NULL) {
3176 if (zfs_shareall(zhp) != 0)
3178 } else if (strcmp(protocol, "nfs") == 0) {
3179 if (zfs_share_nfs(zhp))
3181 } else if (strcmp(protocol, "smb") == 0) {
3182 if (zfs_share_smb(zhp))
3185 (void) fprintf(stderr, gettext("cannot share "
3186 "'%s': invalid share type '%s' "
3188 zfs_get_name(zhp), protocol);
3195 if (options == NULL)
3196 mnt.mnt_mntopts = "";
3198 mnt.mnt_mntopts = (char *)options;
3200 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3201 zfs_is_mounted(zhp, NULL)) {
3205 (void) fprintf(stderr, gettext("cannot mount "
3206 "'%s': filesystem already mounted\n"),
3211 if (zfs_mount(zhp, options, flags) != 0)
3216 assert(op == OP_SHARE);
3219 * Ignore any volumes that aren't shared.
3221 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
3222 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3224 if (strcmp(shareopts, "off") == 0) {
3228 (void) fprintf(stderr, gettext("cannot share '%s': "
3229 "'shareiscsi' property not set\n"),
3231 (void) fprintf(stderr, gettext("set 'shareiscsi' "
3232 "property or use iscsitadm(1M) to share this "
3237 if (zfs_is_shared_iscsi(zhp)) {
3241 (void) fprintf(stderr, gettext("cannot share "
3242 "'%s': volume already shared\n"),
3247 if (zfs_share_iscsi(zhp) != 0)
3255 * Reports progress in the form "(current/total)". Not thread-safe.
3258 report_mount_progress(int current, int total)
3261 static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3262 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3263 static time_t last_progress_time;
3264 time_t now = time(NULL);
3266 /* report 1..n instead of 0..n-1 */
3269 /* display header if we're here for the first time */
3271 (void) printf(gettext("Mounting ZFS filesystems: "));
3273 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3274 /* too soon to report again */
3278 last_progress_time = now;
3280 /* back up to prepare for overwriting */
3282 (void) printf("%*.*s", len, len, reverse);
3284 /* We put a newline at the end if this is the last one. */
3285 len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
3286 (void) fflush(stdout);
3290 append_options(char *mntopts, char *newopts)
3292 int len = strlen(mntopts);
3294 /* original length plus new string to append plus 1 for the comma */
3295 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3296 (void) fprintf(stderr, gettext("the opts argument for "
3297 "'%c' option is too long (more than %d chars)\n"),
3298 "-o", MNT_LINE_MAX);
3303 mntopts[len++] = ',';
3305 (void) strcpy(&mntopts[len], newopts);
3309 share_mount(int op, int argc, char **argv)
3312 boolean_t verbose = B_FALSE;
3314 char *options = NULL;
3315 int types, flags = 0;
3318 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3328 if (*optarg == '\0') {
3329 (void) fprintf(stderr, gettext("empty mount "
3330 "options (-o) specified\n"));
3334 if (options == NULL)
3335 options = safe_malloc(MNT_LINE_MAX + 1);
3337 /* option validation is done later */
3338 append_options(options, optarg);
3342 warnx("no overlay mounts support on FreeBSD, ignoring");
3345 (void) fprintf(stderr, gettext("missing argument for "
3346 "'%c' option\n"), optopt);
3350 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3359 /* check number of arguments */
3361 zfs_handle_t **dslist = NULL;
3362 size_t i, count = 0;
3363 char *protocol = NULL;
3365 if (op == OP_MOUNT) {
3366 types = ZFS_TYPE_FILESYSTEM;
3367 } else if (argc > 0) {
3368 if (strcmp(argv[0], "nfs") == 0 ||
3369 strcmp(argv[0], "smb") == 0) {
3370 types = ZFS_TYPE_FILESYSTEM;
3371 } else if (strcmp(argv[0], "iscsi") == 0) {
3372 types = ZFS_TYPE_VOLUME;
3374 (void) fprintf(stderr, gettext("share type "
3375 "must be 'nfs', 'smb' or 'iscsi'\n"));
3382 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3386 (void) fprintf(stderr, gettext("too many arguments\n"));
3390 get_all_datasets(types, &dslist, &count, verbose);
3395 qsort(dslist, count, sizeof (void *), dataset_cmp);
3397 for (i = 0; i < count; i++) {
3399 report_mount_progress(i, count);
3401 if (share_mount_one(dslist[i], op, flags, protocol,
3402 B_FALSE, options) != 0)
3404 zfs_close(dslist[i]);
3408 } else if (argc == 0) {
3412 if ((op == OP_SHARE) || (options != NULL)) {
3413 (void) fprintf(stderr, gettext("missing filesystem "
3414 "argument (specify -a for all)\n"));
3419 * When mount is given no arguments, go through /etc/mnttab and
3420 * display any active ZFS mounts. We hide any snapshots, since
3421 * they are controlled automatically.
3423 if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
3424 fprintf(stderr, "getmntinfo(): %s\n", strerror(errno));
3427 for (i = 0; i < n; i++) {
3428 if (strcmp(sfs[i].f_fstypename, MNTTYPE_ZFS) != 0 ||
3429 strchr(sfs[i].f_mntfromname, '@') != NULL)
3432 (void) printf("%-30s %s\n", sfs[i].f_mntfromname,
3433 sfs[i].f_mntonname);
3439 types = ZFS_TYPE_FILESYSTEM;
3441 types |= ZFS_TYPE_VOLUME;
3444 (void) fprintf(stderr,
3445 gettext("too many arguments\n"));
3449 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3452 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3462 * zfs mount -a [nfs | iscsi]
3463 * zfs mount filesystem
3465 * Mount all filesystems, or mount the given filesystem.
3468 zfs_do_mount(int argc, char **argv)
3470 return (share_mount(OP_MOUNT, argc, argv));
3474 * zfs share -a [nfs | iscsi | smb]
3475 * zfs share filesystem
3477 * Share all filesystems, or share the given filesystem.
3480 zfs_do_share(int argc, char **argv)
3482 return (share_mount(OP_SHARE, argc, argv));
3485 typedef struct unshare_unmount_node {
3486 zfs_handle_t *un_zhp;
3488 uu_avl_node_t un_avlnode;
3489 } unshare_unmount_node_t;
3493 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3495 const unshare_unmount_node_t *l = larg;
3496 const unshare_unmount_node_t *r = rarg;
3498 return (strcmp(l->un_mountp, r->un_mountp));
3502 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3503 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3504 * and unmount it appropriately.
3507 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3511 struct stat64 statbuf;
3512 struct mnttab search = { 0 }, entry;
3513 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3517 * Search for the path in /etc/mnttab. Rather than looking for the
3518 * specific path, which can be fooled by non-standard paths (i.e. ".."
3519 * or "//"), we stat() the path and search for the corresponding
3520 * (major,minor) device pair.
3522 if (stat64(path, &statbuf) != 0) {
3523 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3524 cmdname, path, strerror(errno));
3527 path_inode = statbuf.st_ino;
3530 * Search for the given (major,minor) pair in the mount table.
3532 search.mnt_mountp = path;
3533 rewind(mnttab_file);
3534 if (getmntany(mnttab_file, &entry, &search) != 0) {
3535 if (op == OP_SHARE) {
3536 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3537 "currently mounted\n"), cmdname, path);
3540 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3542 if ((ret = umount2(path, flags)) != 0)
3543 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3548 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3549 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3550 "filesystem\n"), cmdname, path);
3554 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3555 ZFS_TYPE_FILESYSTEM)) == NULL)
3559 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3560 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3561 cmdname, path, strerror(errno));
3563 } else if (statbuf.st_ino != path_inode) {
3564 (void) fprintf(stderr, gettext("cannot "
3565 "%s '%s': not a mountpoint\n"), cmdname, path);
3569 if (op == OP_SHARE) {
3570 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3571 char smbshare_prop[ZFS_MAXPROPLEN];
3573 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3574 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3575 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3576 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3578 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3579 strcmp(smbshare_prop, "off") == 0) {
3580 (void) fprintf(stderr, gettext("cannot unshare "
3581 "'%s': legacy share\n"), path);
3582 (void) fprintf(stderr, gettext("use "
3583 "unshare(1M) to unshare this filesystem\n"));
3584 } else if (!zfs_is_shared(zhp)) {
3585 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3586 "not currently shared\n"), path);
3588 ret = zfs_unshareall_bypath(zhp, path);
3591 char mtpt_prop[ZFS_MAXPROPLEN];
3593 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3594 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3597 ret = zfs_unmount(zhp, NULL, flags);
3598 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3599 (void) fprintf(stderr, gettext("cannot unmount "
3600 "'%s': legacy mountpoint\n"),
3602 (void) fprintf(stderr, gettext("use umount(1M) "
3603 "to unmount this filesystem\n"));
3605 ret = zfs_unmountall(zhp, flags);
3616 * Generic callback for unsharing or unmounting a filesystem.
3619 unshare_unmount(int op, int argc, char **argv)
3626 char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3627 char sharesmb[ZFS_MAXPROPLEN];
3630 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3639 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3650 * We could make use of zfs_for_each() to walk all datasets in
3651 * the system, but this would be very inefficient, especially
3652 * since we would have to linearly search /etc/mnttab for each
3653 * one. Instead, do one pass through /etc/mnttab looking for
3654 * zfs entries and call zfs_unmount() for each one.
3656 * Things get a little tricky if the administrator has created
3657 * mountpoints beneath other ZFS filesystems. In this case, we
3658 * have to unmount the deepest filesystems first. To accomplish
3659 * this, we place all the mountpoints in an AVL tree sorted by
3660 * the special type (dataset name), and walk the result in
3661 * reverse to make sure to get any snapshots first.
3663 uu_avl_pool_t *pool;
3665 unshare_unmount_node_t *node;
3667 uu_avl_walk_t *walk;
3672 (void) fprintf(stderr, gettext("too many arguments\n"));
3676 if ((pool = uu_avl_pool_create("unmount_pool",
3677 sizeof (unshare_unmount_node_t),
3678 offsetof(unshare_unmount_node_t, un_avlnode),
3679 unshare_unmount_compare,
3680 UU_DEFAULT)) == NULL) {
3681 (void) fprintf(stderr, gettext("internal error: "
3682 "out of memory\n"));
3686 if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3687 (void) fprintf(stderr, gettext("internal error: "
3688 "out of memory\n"));
3692 if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
3693 (void) fprintf(stderr, gettext("internal error: "
3694 "getmntinfo() failed\n"));
3697 for (i = 0; i < n; i++) {
3699 /* ignore non-ZFS entries */
3700 if (strcmp(sfs[i].f_fstypename, MNTTYPE_ZFS) != 0)
3703 /* ignore snapshots */
3704 if (strchr(sfs[i].f_mntfromname, '@') != NULL)
3707 if ((zhp = zfs_open(g_zfs, sfs[i].f_mntfromname,
3708 ZFS_TYPE_FILESYSTEM)) == NULL) {
3715 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3717 sizeof (nfsiscsi_mnt_prop),
3718 NULL, NULL, 0, B_FALSE) == 0);
3719 if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3721 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3723 sizeof (nfsiscsi_mnt_prop),
3724 NULL, NULL, 0, B_FALSE) == 0);
3725 if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3729 /* Ignore legacy mounts */
3730 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3732 sizeof (nfsiscsi_mnt_prop),
3733 NULL, NULL, 0, B_FALSE) == 0);
3734 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3736 /* Ignore canmount=noauto mounts */
3737 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3738 ZFS_CANMOUNT_NOAUTO)
3744 node = safe_malloc(sizeof (unshare_unmount_node_t));
3747 if ((node->un_mountp = strdup(sfs[i].f_mntonname)) ==
3749 (void) fprintf(stderr, gettext("internal error:"
3750 " out of memory\n"));
3754 uu_avl_node_init(node, &node->un_avlnode, pool);
3756 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3757 uu_avl_insert(tree, node, idx);
3759 zfs_close(node->un_zhp);
3760 free(node->un_mountp);
3766 * Walk the AVL tree in reverse, unmounting each filesystem and
3767 * removing it from the AVL tree in the process.
3769 if ((walk = uu_avl_walk_start(tree,
3770 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3771 (void) fprintf(stderr,
3772 gettext("internal error: out of memory"));
3776 while ((node = uu_avl_walk_next(walk)) != NULL) {
3777 uu_avl_remove(tree, node);
3781 if (zfs_unshareall_bypath(node->un_zhp,
3782 node->un_mountp) != 0)
3787 if (zfs_unmount(node->un_zhp,
3788 node->un_mountp, flags) != 0)
3793 zfs_close(node->un_zhp);
3794 free(node->un_mountp);
3798 uu_avl_walk_end(walk);
3799 uu_avl_destroy(tree);
3800 uu_avl_pool_destroy(pool);
3802 if (op == OP_SHARE) {
3804 * Finally, unshare any volumes shared via iSCSI.
3806 zfs_handle_t **dslist = NULL;
3807 size_t i, count = 0;
3809 get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
3813 qsort(dslist, count, sizeof (void *),
3816 for (i = 0; i < count; i++) {
3817 if (zfs_unshare_iscsi(dslist[i]) != 0)
3819 zfs_close(dslist[i]);
3828 (void) fprintf(stderr,
3829 gettext("missing filesystem argument\n"));
3831 (void) fprintf(stderr,
3832 gettext("too many arguments\n"));
3837 * We have an argument, but it may be a full path or a ZFS
3838 * filesystem. Pass full paths off to unmount_path() (shared by
3839 * manual_unmount), otherwise open the filesystem and pass to
3842 if (argv[0][0] == '/')
3843 return (unshare_unmount_path(op, argv[0],
3846 types = ZFS_TYPE_FILESYSTEM;
3848 types |= ZFS_TYPE_VOLUME;
3850 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3853 if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
3854 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3855 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3856 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3857 NULL, 0, B_FALSE) == 0);
3861 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3863 sizeof (nfsiscsi_mnt_prop),
3864 NULL, NULL, 0, B_FALSE) == 0);
3865 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3866 sharesmb, sizeof (sharesmb), NULL, NULL,
3869 if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3870 strcmp(sharesmb, "off") == 0) {
3871 (void) fprintf(stderr, gettext("cannot "
3872 "unshare '%s': legacy share\n"),
3874 (void) fprintf(stderr, gettext("use "
3875 "unshare(1M) to unshare this "
3878 } else if (!zfs_is_shared(zhp)) {
3879 (void) fprintf(stderr, gettext("cannot "
3880 "unshare '%s': not currently "
3881 "shared\n"), zfs_get_name(zhp));
3883 } else if (zfs_unshareall(zhp) != 0) {
3889 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3890 (void) fprintf(stderr, gettext("cannot "
3891 "unmount '%s': legacy "
3892 "mountpoint\n"), zfs_get_name(zhp));
3893 (void) fprintf(stderr, gettext("use "
3894 "umount(1M) to unmount this "
3897 } else if (!zfs_is_mounted(zhp, NULL)) {
3898 (void) fprintf(stderr, gettext("cannot "
3899 "unmount '%s': not currently "
3903 } else if (zfs_unmountall(zhp, flags) != 0) {
3909 assert(op == OP_SHARE);
3911 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3912 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3913 NULL, NULL, 0, B_FALSE) == 0);
3915 if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
3916 (void) fprintf(stderr, gettext("cannot unshare "
3917 "'%s': 'shareiscsi' property not set\n"),
3919 (void) fprintf(stderr, gettext("set "
3920 "'shareiscsi' property or use "
3921 "iscsitadm(1M) to share this volume\n"));
3923 } else if (!zfs_is_shared_iscsi(zhp)) {
3924 (void) fprintf(stderr, gettext("cannot "
3925 "unshare '%s': not currently shared\n"),
3928 } else if (zfs_unshare_iscsi(zhp) != 0) {
3941 * zfs unmount filesystem
3943 * Unmount all filesystems, or a specific ZFS filesystem.
3946 zfs_do_unmount(int argc, char **argv)
3948 return (unshare_unmount(OP_MOUNT, argc, argv));
3953 * zfs unshare filesystem
3955 * Unshare all filesystems, or a specific ZFS filesystem.
3958 zfs_do_unshare(int argc, char **argv)
3960 return (unshare_unmount(OP_SHARE, argc, argv));
3964 * Attach/detach the given dataset to/from the given jail
3968 do_jail(int argc, char **argv, int attach)
3973 /* check number of arguments */
3975 (void) fprintf(stderr, gettext("missing argument(s)\n"));
3979 (void) fprintf(stderr, gettext("too many arguments\n"));
3983 jailid = atoi(argv[1]);
3985 (void) fprintf(stderr, gettext("invalid jailid\n"));
3989 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
3993 ret = (zfs_jail(zhp, jailid, attach) != 0);
4000 * zfs jail jailid filesystem
4002 * Attach the given dataset to the given jail
4006 zfs_do_jail(int argc, char **argv)
4009 return (do_jail(argc, argv, 1));
4013 * zfs unjail jailid filesystem
4015 * Detach the given dataset from the given jail
4019 zfs_do_unjail(int argc, char **argv)
4022 return (do_jail(argc, argv, 0));
4026 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
4027 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
4030 manual_mount(int argc, char **argv)
4033 char mountpoint[ZFS_MAXPROPLEN];
4034 char mntopts[MNT_LINE_MAX] = { '\0' };
4038 char *dataset, *path;
4041 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
4044 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
4047 #if 0 /* FreeBSD: No support for MS_OVERLAY. */
4048 flags |= MS_OVERLAY;
4052 #if 0 /* FreeBSD: No support for MS_NOMNTTAB. */
4053 flags |= MS_NOMNTTAB;
4057 (void) fprintf(stderr, gettext("missing argument for "
4058 "'%c' option\n"), optopt);
4062 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4064 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
4073 /* check that we only have two arguments */
4076 (void) fprintf(stderr, gettext("missing dataset "
4079 (void) fprintf(stderr,
4080 gettext("missing mountpoint argument\n"));
4082 (void) fprintf(stderr, gettext("too many arguments\n"));
4083 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
4090 /* try to open the dataset */
4091 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
4094 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
4095 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
4097 /* check for legacy mountpoint and complain appropriately */
4099 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
4100 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
4101 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
4102 (void) fprintf(stderr, gettext("mount failed: %s\n"),
4107 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
4108 "mounted using 'mount -F zfs'\n"), dataset);
4109 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
4110 "instead.\n"), path);
4111 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
4112 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
4113 (void) fprintf(stderr, gettext("See zfs(1M) for more "
4122 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
4123 * unmounts of non-legacy filesystems, as this is the dominant administrative
4127 manual_unmount(int argc, char **argv)
4133 while ((c = getopt(argc, argv, "f")) != -1) {
4139 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4141 (void) fprintf(stderr, gettext("usage: unmount [-f] "
4150 /* check arguments */
4153 (void) fprintf(stderr, gettext("missing path "
4156 (void) fprintf(stderr, gettext("too many arguments\n"));
4157 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
4161 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
4165 volcheck(zpool_handle_t *zhp, void *data)
4167 boolean_t isinit = *((boolean_t *)data);
4170 return (zpool_create_zvol_links(zhp));
4172 return (zpool_remove_zvol_links(zhp));
4176 * Iterate over all pools in the system and either create or destroy /dev/zvol
4177 * links, depending on the value of 'isinit'.
4180 do_volcheck(boolean_t isinit)
4182 return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
4186 find_command_idx(char *command, int *idx)
4190 for (i = 0; i < NCOMMAND; i++) {
4191 if (command_table[i].name == NULL)
4194 if (strcmp(command, command_table[i].name) == 0) {
4203 main(int argc, char **argv)
4210 (void) setlocale(LC_ALL, "");
4211 (void) textdomain(TEXT_DOMAIN);
4215 if ((g_zfs = libzfs_init()) == NULL) {
4216 (void) fprintf(stderr, gettext("internal error: failed to "
4217 "initialize ZFS library\n"));
4221 zpool_set_history_str("zfs", argc, argv, history_str);
4222 verify(zpool_stage_history(g_zfs, history_str) == 0);
4224 libzfs_print_on_error(g_zfs, B_TRUE);
4226 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4227 (void) fprintf(stderr, gettext("internal error: unable to "
4228 "open %s\n"), MNTTAB);
4233 * This command also doubles as the /etc/fs mount and unmount program.
4234 * Determine if we should take this behavior based on argv[0].
4236 progname = basename(argv[0]);
4237 if (strcmp(progname, "mount") == 0) {
4238 ret = manual_mount(argc, argv);
4239 } else if (strcmp(progname, "umount") == 0) {
4240 ret = manual_unmount(argc, argv);
4243 * Make sure the user has specified some command.
4246 (void) fprintf(stderr, gettext("missing command\n"));
4253 * The 'umount' command is an alias for 'unmount'
4255 if (strcmp(cmdname, "umount") == 0)
4256 cmdname = "unmount";
4259 * The 'recv' command is an alias for 'receive'
4261 if (strcmp(cmdname, "recv") == 0)
4262 cmdname = "receive";
4267 if (strcmp(cmdname, "-?") == 0)
4271 * 'volinit' and 'volfini' do not appear in the usage message,
4272 * so we have to special case them here.
4274 if (strcmp(cmdname, "volinit") == 0)
4275 return (do_volcheck(B_TRUE));
4276 else if (strcmp(cmdname, "volfini") == 0)
4277 return (do_volcheck(B_FALSE));
4280 * Run the appropriate command.
4282 if (find_command_idx(cmdname, &i) == 0) {
4283 current_command = &command_table[i];
4284 ret = command_table[i].func(argc - 1, argv + 1);
4285 } else if (strchr(cmdname, '=') != NULL) {
4286 verify(find_command_idx("set", &i) == 0);
4287 current_command = &command_table[i];
4288 ret = command_table[i].func(argc, argv);
4290 (void) fprintf(stderr, gettext("unrecognized "
4291 "command '%s'\n"), cmdname);
4296 (void) fclose(mnttab_file);
4301 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4302 * for the purposes of running ::findleaks.
4304 if (getenv("ZFS_ABORT") != NULL) {
4305 (void) printf("dumping core by request\n");