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 2009 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] [-d max] "
194 "[-o field[,...]] [-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][-d max] "
209 "[-o property[,...]] [-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 parse_depth(char *opt, int *flags)
441 depth = (int)strtol(opt, &tmp, 0);
443 (void) fprintf(stderr,
444 gettext("%s is not an integer\n"), optarg);
448 (void) fprintf(stderr,
449 gettext("Depth can not be negative.\n"));
452 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
457 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
459 * Given an existing dataset, create a writable copy whose initial contents
460 * are the same as the source. The newly created dataset maintains a
461 * dependency on the original; the original cannot be destroyed so long as
464 * The '-p' flag creates all the non-existing ancestors of the target first.
467 zfs_do_clone(int argc, char **argv)
469 zfs_handle_t *zhp = NULL;
470 boolean_t parents = B_FALSE;
475 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
476 (void) fprintf(stderr, gettext("internal error: "
482 while ((c = getopt(argc, argv, "o:p")) != -1) {
485 if (parseprop(props))
492 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
501 /* check number of arguments */
503 (void) fprintf(stderr, gettext("missing source dataset "
508 (void) fprintf(stderr, gettext("missing target dataset "
513 (void) fprintf(stderr, gettext("too many arguments\n"));
517 /* open the source dataset */
518 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
521 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
524 * Now create the ancestors of the target dataset. If the
525 * target already exists and '-p' option was used we should not
528 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
531 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
536 ret = zfs_clone(zhp, argv[1], props);
538 /* create the mountpoint if necessary */
542 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
544 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
545 ret = zfs_share(clone);
564 * zfs create [-p] [-o prop=value] ... fs
565 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
567 * Create a new dataset. This command can be used to create filesystems
568 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
569 * For volumes, the user must specify a size to be used.
571 * The '-s' flag applies only to volumes, and indicates that we should not try
572 * to set the reservation for this volume. By default we set a reservation
573 * equal to the size for any volume. For pools with SPA_VERSION >=
574 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
576 * The '-p' flag creates all the non-existing ancestors of the target first.
579 zfs_do_create(int argc, char **argv)
581 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
582 zfs_handle_t *zhp = NULL;
585 boolean_t noreserve = B_FALSE;
586 boolean_t bflag = B_FALSE;
587 boolean_t parents = B_FALSE;
593 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
594 (void) fprintf(stderr, gettext("internal error: "
600 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
603 type = ZFS_TYPE_VOLUME;
604 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
605 (void) fprintf(stderr, gettext("bad volume "
606 "size '%s': %s\n"), optarg,
607 libzfs_error_description(g_zfs));
611 if (nvlist_add_uint64(props,
612 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
614 (void) fprintf(stderr, gettext("internal "
615 "error: out of memory\n"));
625 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
626 (void) fprintf(stderr, gettext("bad volume "
627 "block size '%s': %s\n"), optarg,
628 libzfs_error_description(g_zfs));
632 if (nvlist_add_uint64(props,
633 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
635 (void) fprintf(stderr, gettext("internal "
636 "error: out of memory\n"));
641 if (parseprop(props))
648 (void) fprintf(stderr, gettext("missing size "
653 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
659 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
660 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
661 "used when creating a volume\n"));
668 /* check number of arguments */
670 (void) fprintf(stderr, gettext("missing %s argument\n"),
671 zfs_type_to_name(type));
675 (void) fprintf(stderr, gettext("too many arguments\n"));
679 if (type == ZFS_TYPE_VOLUME && !noreserve) {
680 zpool_handle_t *zpool_handle;
681 uint64_t spa_version;
683 zfs_prop_t resv_prop;
686 if (p = strchr(argv[0], '/'))
688 zpool_handle = zpool_open(g_zfs, argv[0]);
691 if (zpool_handle == NULL)
693 spa_version = zpool_get_prop_int(zpool_handle,
694 ZPOOL_PROP_VERSION, NULL);
695 zpool_close(zpool_handle);
696 if (spa_version >= SPA_VERSION_REFRESERVATION)
697 resv_prop = ZFS_PROP_REFRESERVATION;
699 resv_prop = ZFS_PROP_RESERVATION;
701 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
703 if (nvlist_add_uint64(props,
704 zfs_prop_to_name(resv_prop), volsize) != 0) {
705 (void) fprintf(stderr, gettext("internal "
706 "error: out of memory\n"));
713 if (parents && zfs_name_valid(argv[0], type)) {
715 * Now create the ancestors of target dataset. If the target
716 * already exists and '-p' option was used we should not
719 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
723 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
728 if (zfs_create(g_zfs, argv[0], type, props) != 0)
731 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
734 * if the user doesn't want the dataset automatically mounted,
735 * then skip the mount/share step
738 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
741 * Mount and/or share the new filesystem as appropriate. We provide a
742 * verbose error message to let the user know that their filesystem was
743 * in fact created, even if we failed to mount or share it.
746 if (canmount == ZFS_CANMOUNT_ON) {
747 if (zfs_mount(zhp, NULL, 0) != 0) {
748 (void) fprintf(stderr, gettext("filesystem "
749 "successfully created, but not mounted\n"));
751 } else if (zfs_share(zhp) != 0) {
752 (void) fprintf(stderr, gettext("filesystem "
753 "successfully created, but not shared\n"));
770 * zfs destroy [-rf] <fs, snap, vol>
772 * -r Recursively destroy all children
773 * -R Recursively destroy all dependents, including clones
774 * -f Force unmounting of any dependents
776 * Destroys the given dataset. By default, it will unmount any filesystems,
777 * and refuse to destroy a dataset that has any dependents. A dependent can
778 * either be a child, or a clone of a child.
780 typedef struct destroy_cbdata {
787 boolean_t cb_closezhp;
788 zfs_handle_t *cb_target;
793 * Check for any dependents based on the '-r' or '-R' flags.
796 destroy_check_dependent(zfs_handle_t *zhp, void *data)
798 destroy_cbdata_t *cbp = data;
799 const char *tname = zfs_get_name(cbp->cb_target);
800 const char *name = zfs_get_name(zhp);
802 if (strncmp(tname, name, strlen(tname)) == 0 &&
803 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
805 * This is a direct descendant, not a clone somewhere else in
812 (void) fprintf(stderr, gettext("cannot destroy '%s': "
813 "%s has children\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));
825 * This is a clone. We only want to report this if the '-r'
826 * wasn't specified, or the target is a snapshot.
828 if (!cbp->cb_recurse &&
829 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
833 (void) fprintf(stderr, gettext("cannot destroy '%s': "
834 "%s has dependent clones\n"),
835 zfs_get_name(cbp->cb_target),
836 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
837 (void) fprintf(stderr, gettext("use '-R' to destroy "
838 "the following datasets:\n"));
839 cbp->cb_first = B_FALSE;
843 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
852 destroy_callback(zfs_handle_t *zhp, void *data)
854 destroy_cbdata_t *cbp = data;
857 * Ignore pools (which we've already flagged as an error before getting
860 if (strchr(zfs_get_name(zhp), '/') == NULL &&
861 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
867 * Bail out on the first error.
869 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
870 zfs_destroy(zhp) != 0) {
880 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
882 destroy_cbdata_t *cbp = arg;
883 char thissnap[MAXPATHLEN];
885 boolean_t closezhp = cbp->cb_closezhp;
888 (void) snprintf(thissnap, sizeof (thissnap),
889 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
891 libzfs_print_on_error(g_zfs, B_FALSE);
892 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
893 libzfs_print_on_error(g_zfs, B_TRUE);
896 * Destroy any clones of this snapshot
898 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
908 cbp->cb_closezhp = B_TRUE;
909 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
916 zfs_do_destroy(int argc, char **argv)
918 destroy_cbdata_t cb = { 0 };
924 while ((c = getopt(argc, argv, "frR")) != -1) {
938 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
947 /* check number of arguments */
949 (void) fprintf(stderr, gettext("missing path argument\n"));
953 (void) fprintf(stderr, gettext("too many arguments\n"));
958 * If we are doing recursive destroy of a snapshot, then the
959 * named snapshot may not exist. Go straight to libzfs.
961 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
965 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
970 if (cb.cb_doclones) {
972 if (destroy_snap_clones(zhp, &cb) != 0) {
978 ret = zfs_destroy_snaps(zhp, cp);
981 (void) fprintf(stderr,
982 gettext("no snapshots destroyed\n"));
988 /* Open the given dataset */
989 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
995 * Perform an explicit check for pools before going any further.
997 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
998 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
999 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1000 "operation does not apply to pools\n"),
1002 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1003 "%s' to destroy all datasets in the pool\n"),
1005 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1006 "to destroy the pool itself\n"), zfs_get_name(zhp));
1012 * Check for any dependents and/or clones.
1014 cb.cb_first = B_TRUE;
1015 if (!cb.cb_doclones &&
1016 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1023 zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
1029 * Do the real thing. The callback will close the handle regardless of
1030 * whether it succeeds or not.
1033 if (destroy_callback(zhp, &cb) != 0)
1041 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
1042 * < all | property[,property]... > < fs | snap | vol > ...
1044 * -r recurse over any child datasets
1045 * -H scripted mode. Headers are stripped, and fields are separated
1046 * by tabs instead of spaces.
1047 * -o Set of fields to display. One of "name,property,value,source".
1048 * Default is all four.
1049 * -s Set of sources to allow. One of
1050 * "local,default,inherited,temporary,none". Default is all
1052 * -p Display values in parsable (literal) format.
1054 * Prints properties for the given datasets. The user can control which
1055 * columns to display as well as which property types to allow.
1059 * Invoked to display the properties for a single dataset.
1062 get_callback(zfs_handle_t *zhp, void *data)
1064 char buf[ZFS_MAXPROPLEN];
1065 zprop_source_t sourcetype;
1066 char source[ZFS_MAXNAMELEN];
1067 zprop_get_cbdata_t *cbp = data;
1068 nvlist_t *userprop = zfs_get_user_props(zhp);
1069 zprop_list_t *pl = cbp->cb_proplist;
1074 for (; pl != NULL; pl = pl->pl_next) {
1076 * Skip the special fake placeholder. This will also skip over
1077 * the name property when 'all' is specified.
1079 if (pl->pl_prop == ZFS_PROP_NAME &&
1080 pl == cbp->cb_proplist)
1083 if (pl->pl_prop != ZPROP_INVAL) {
1084 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1085 sizeof (buf), &sourcetype, source,
1087 cbp->cb_literal) != 0) {
1090 if (!zfs_prop_valid_for_type(pl->pl_prop,
1091 ZFS_TYPE_DATASET)) {
1092 (void) fprintf(stderr,
1093 gettext("No such property '%s'\n"),
1094 zfs_prop_to_name(pl->pl_prop));
1097 sourcetype = ZPROP_SRC_NONE;
1098 (void) strlcpy(buf, "-", sizeof (buf));
1101 zprop_print_one_property(zfs_get_name(zhp), cbp,
1102 zfs_prop_to_name(pl->pl_prop),
1103 buf, sourcetype, source);
1105 if (nvlist_lookup_nvlist(userprop,
1106 pl->pl_user_prop, &propval) != 0) {
1109 sourcetype = ZPROP_SRC_NONE;
1112 verify(nvlist_lookup_string(propval,
1113 ZPROP_VALUE, &strval) == 0);
1114 verify(nvlist_lookup_string(propval,
1115 ZPROP_SOURCE, &sourceval) == 0);
1117 if (strcmp(sourceval,
1118 zfs_get_name(zhp)) == 0) {
1119 sourcetype = ZPROP_SRC_LOCAL;
1121 sourcetype = ZPROP_SRC_INHERITED;
1122 (void) strlcpy(source,
1123 sourceval, sizeof (source));
1127 zprop_print_one_property(zfs_get_name(zhp), cbp,
1128 pl->pl_user_prop, strval, sourcetype,
1137 zfs_do_get(int argc, char **argv)
1139 zprop_get_cbdata_t cb = { 0 };
1140 int i, c, flags = 0;
1141 char *value, *fields;
1144 zprop_list_t fake_name = { 0 };
1147 * Set up default columns and sources.
1149 cb.cb_sources = ZPROP_SRC_ALL;
1150 cb.cb_columns[0] = GET_COL_NAME;
1151 cb.cb_columns[1] = GET_COL_PROPERTY;
1152 cb.cb_columns[2] = GET_COL_VALUE;
1153 cb.cb_columns[3] = GET_COL_SOURCE;
1154 cb.cb_type = ZFS_TYPE_DATASET;
1157 while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1160 cb.cb_literal = B_TRUE;
1163 limit = parse_depth(optarg, &flags);
1166 flags |= ZFS_ITER_RECURSE;
1169 cb.cb_scripted = B_TRUE;
1172 (void) fprintf(stderr, gettext("missing argument for "
1173 "'%c' option\n"), optopt);
1178 * Process the set of columns to display. We zero out
1179 * the structure to give us a blank slate.
1181 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1183 while (*optarg != '\0') {
1184 static char *col_subopts[] =
1185 { "name", "property", "value", "source",
1189 (void) fprintf(stderr, gettext("too "
1190 "many fields given to -o "
1195 switch (getsubopt(&optarg, col_subopts,
1198 cb.cb_columns[i++] = GET_COL_NAME;
1201 cb.cb_columns[i++] = GET_COL_PROPERTY;
1204 cb.cb_columns[i++] = GET_COL_VALUE;
1207 cb.cb_columns[i++] = GET_COL_SOURCE;
1210 (void) fprintf(stderr,
1211 gettext("invalid column name "
1220 while (*optarg != '\0') {
1221 static char *source_subopts[] = {
1222 "local", "default", "inherited",
1223 "temporary", "none", NULL };
1225 switch (getsubopt(&optarg, source_subopts,
1228 cb.cb_sources |= ZPROP_SRC_LOCAL;
1231 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1234 cb.cb_sources |= ZPROP_SRC_INHERITED;
1237 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1240 cb.cb_sources |= ZPROP_SRC_NONE;
1243 (void) fprintf(stderr,
1244 gettext("invalid source "
1252 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1262 (void) fprintf(stderr, gettext("missing property "
1269 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1277 * As part of zfs_expand_proplist(), we keep track of the maximum column
1278 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1279 * need to know the maximum name length. However, the user likely did
1280 * not specify 'name' as one of the properties to fetch, so we need to
1281 * make sure we always include at least this property for
1282 * print_get_headers() to work properly.
1284 if (cb.cb_proplist != NULL) {
1285 fake_name.pl_prop = ZFS_PROP_NAME;
1286 fake_name.pl_width = strlen(gettext("NAME"));
1287 fake_name.pl_next = cb.cb_proplist;
1288 cb.cb_proplist = &fake_name;
1291 cb.cb_first = B_TRUE;
1293 /* run for each object */
1294 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1295 &cb.cb_proplist, limit, get_callback, &cb);
1297 if (cb.cb_proplist == &fake_name)
1298 zprop_free_list(fake_name.pl_next);
1300 zprop_free_list(cb.cb_proplist);
1306 * inherit [-r] <property> <fs|vol> ...
1308 * -r Recurse over all children
1310 * For each dataset specified on the command line, inherit the given property
1311 * from its parent. Inheriting a property at the pool level will cause it to
1312 * use the default value. The '-r' flag will recurse over all children, and is
1313 * useful for setting a property on a hierarchy-wide basis, regardless of any
1314 * local modifications for each dataset.
1318 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1320 char *propname = data;
1321 zfs_prop_t prop = zfs_name_to_prop(propname);
1324 * If we're doing it recursively, then ignore properties that
1325 * are not valid for this type of dataset.
1327 if (prop != ZPROP_INVAL &&
1328 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1331 return (zfs_prop_inherit(zhp, propname) != 0);
1335 inherit_cb(zfs_handle_t *zhp, void *data)
1337 char *propname = data;
1339 return (zfs_prop_inherit(zhp, propname) != 0);
1343 zfs_do_inherit(int argc, char **argv)
1352 while ((c = getopt(argc, argv, "r")) != -1) {
1355 flags |= ZFS_ITER_RECURSE;
1359 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1368 /* check number of arguments */
1370 (void) fprintf(stderr, gettext("missing property argument\n"));
1374 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1382 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1383 if (zfs_prop_readonly(prop)) {
1384 (void) fprintf(stderr, gettext(
1385 "%s property is read-only\n"),
1389 if (!zfs_prop_inheritable(prop)) {
1390 (void) fprintf(stderr, gettext("'%s' property cannot "
1391 "be inherited\n"), propname);
1392 if (prop == ZFS_PROP_QUOTA ||
1393 prop == ZFS_PROP_RESERVATION ||
1394 prop == ZFS_PROP_REFQUOTA ||
1395 prop == ZFS_PROP_REFRESERVATION)
1396 (void) fprintf(stderr, gettext("use 'zfs set "
1397 "%s=none' to clear\n"), propname);
1400 } else if (!zfs_prop_user(propname)) {
1401 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1406 if (flags & ZFS_ITER_RECURSE) {
1407 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1408 NULL, NULL, 0, inherit_recurse_cb, propname);
1410 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1411 NULL, NULL, 0, inherit_cb, propname);
1417 typedef struct upgrade_cbdata {
1418 uint64_t cb_numupgraded;
1419 uint64_t cb_numsamegraded;
1420 uint64_t cb_numfailed;
1421 uint64_t cb_version;
1423 boolean_t cb_foundone;
1424 char cb_lastfs[ZFS_MAXNAMELEN];
1428 same_pool(zfs_handle_t *zhp, const char *name)
1430 int len1 = strcspn(name, "/@");
1431 const char *zhname = zfs_get_name(zhp);
1432 int len2 = strcspn(zhname, "/@");
1436 return (strncmp(name, zhname, len1) == 0);
1440 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1442 upgrade_cbdata_t *cb = data;
1443 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1445 /* list if it's old/new */
1446 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1447 (cb->cb_newer && version > ZPL_VERSION)) {
1450 str = gettext("The following filesystems are "
1451 "formatted using a newer software version and\n"
1452 "cannot be accessed on the current system.\n\n");
1454 str = gettext("The following filesystems are "
1455 "out of date, and can be upgraded. After being\n"
1456 "upgraded, these filesystems (and any 'zfs send' "
1457 "streams generated from\n"
1458 "subsequent snapshots) will no longer be "
1459 "accessible by older software versions.\n\n");
1462 if (!cb->cb_foundone) {
1464 (void) printf(gettext("VER FILESYSTEM\n"));
1465 (void) printf(gettext("--- ------------\n"));
1466 cb->cb_foundone = B_TRUE;
1469 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1476 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1478 upgrade_cbdata_t *cb = data;
1479 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1481 if (cb->cb_version >= ZPL_VERSION_FUID) {
1484 if (zfs_spa_version(zhp, &spa_version) < 0)
1487 if (spa_version < SPA_VERSION_FUID) {
1489 (void) printf(gettext("%s: can not be upgraded; "
1490 "the pool version needs to first be upgraded\nto "
1492 zfs_get_name(zhp), SPA_VERSION_FUID);
1499 if (version < cb->cb_version) {
1501 (void) snprintf(verstr, sizeof (verstr),
1502 "%llu", cb->cb_version);
1503 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1505 * If they did "zfs upgrade -a", then we could
1506 * be doing ioctls to different pools. We need
1507 * to log this history once to each pool.
1509 verify(zpool_stage_history(g_zfs, history_str) == 0);
1511 if (zfs_prop_set(zhp, "version", verstr) == 0)
1512 cb->cb_numupgraded++;
1515 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1516 } else if (version > cb->cb_version) {
1517 /* can't downgrade */
1518 (void) printf(gettext("%s: can not be downgraded; "
1519 "it is already at version %u\n"),
1520 zfs_get_name(zhp), version);
1523 cb->cb_numsamegraded++;
1531 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1534 zfs_do_upgrade(int argc, char **argv)
1536 boolean_t all = B_FALSE;
1537 boolean_t showversions = B_FALSE;
1539 upgrade_cbdata_t cb = { 0 };
1541 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1544 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1547 flags |= ZFS_ITER_RECURSE;
1550 showversions = B_TRUE;
1553 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1554 optarg, &cb.cb_version) != 0) {
1555 (void) fprintf(stderr,
1556 gettext("invalid version %s\n"), optarg);
1565 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1574 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1576 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1577 cb.cb_version || argc))
1579 if ((all || argc) && (showversions))
1585 /* Show info on available versions. */
1586 (void) printf(gettext("The following filesystem versions are "
1588 (void) printf(gettext("VER DESCRIPTION\n"));
1589 (void) printf("--- -----------------------------------------"
1590 "---------------\n");
1591 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1592 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1593 (void) printf(gettext(" 3 Case insensitive and File system "
1594 "unique identifer (FUID)\n"));
1595 (void) printf(gettext("\nFor more information on a particular "
1596 "version, including supported releases, see:\n\n"));
1597 (void) printf("http://www.opensolaris.org/os/community/zfs/"
1598 "version/zpl/N\n\n");
1599 (void) printf(gettext("Where 'N' is the version number.\n"));
1601 } else if (argc || all) {
1602 /* Upgrade filesystems */
1603 if (cb.cb_version == 0)
1604 cb.cb_version = ZPL_VERSION;
1605 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1606 NULL, NULL, 0, upgrade_set_callback, &cb);
1607 (void) printf(gettext("%llu filesystems upgraded\n"),
1609 if (cb.cb_numsamegraded) {
1610 (void) printf(gettext("%llu filesystems already at "
1612 cb.cb_numsamegraded);
1614 if (cb.cb_numfailed != 0)
1617 /* List old-version filesytems */
1619 (void) printf(gettext("This system is currently running "
1620 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1622 flags |= ZFS_ITER_RECURSE;
1623 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1624 NULL, NULL, 0, upgrade_list_callback, &cb);
1626 found = cb.cb_foundone;
1627 cb.cb_foundone = B_FALSE;
1628 cb.cb_newer = B_TRUE;
1630 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1631 NULL, NULL, 0, upgrade_list_callback, &cb);
1633 if (!cb.cb_foundone && !found) {
1634 (void) printf(gettext("All filesystems are "
1635 "formatted with the current version.\n"));
1643 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
1644 * [-s property [-s property]...] [-S property [-S property]...]
1647 * -r Recurse over all children
1648 * -d Limit recursion by depth.
1649 * -H Scripted mode; elide headers and separate columns by tabs
1650 * -o Control which fields to display.
1651 * -t Control which object types to display.
1652 * -s Specify sort columns, descending order.
1653 * -S Specify sort columns, ascending order.
1655 * When given no arguments, lists all filesystems in the system.
1656 * Otherwise, list the specified datasets, optionally recursing down them if
1657 * '-r' is specified.
1659 typedef struct list_cbdata {
1661 boolean_t cb_scripted;
1662 zprop_list_t *cb_proplist;
1666 * Given a list of columns to display, output appropriate headers for each one.
1669 print_header(zprop_list_t *pl)
1671 char headerbuf[ZFS_MAXPROPLEN];
1674 boolean_t first = B_TRUE;
1675 boolean_t right_justify;
1677 for (; pl != NULL; pl = pl->pl_next) {
1684 right_justify = B_FALSE;
1685 if (pl->pl_prop != ZPROP_INVAL) {
1686 header = zfs_prop_column_name(pl->pl_prop);
1687 right_justify = zfs_prop_align_right(pl->pl_prop);
1689 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1690 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1691 headerbuf[i] = '\0';
1695 if (pl->pl_next == NULL && !right_justify)
1696 (void) printf("%s", header);
1697 else if (right_justify)
1698 (void) printf("%*s", pl->pl_width, header);
1700 (void) printf("%-*s", pl->pl_width, header);
1703 (void) printf("\n");
1707 * Given a dataset and a list of fields, print out all the properties according
1708 * to the described layout.
1711 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1713 boolean_t first = B_TRUE;
1714 char property[ZFS_MAXPROPLEN];
1715 nvlist_t *userprops = zfs_get_user_props(zhp);
1718 boolean_t right_justify;
1721 for (; pl != NULL; pl = pl->pl_next) {
1724 (void) printf("\t");
1731 right_justify = B_FALSE;
1732 if (pl->pl_prop != ZPROP_INVAL) {
1733 if (zfs_prop_get(zhp, pl->pl_prop, property,
1734 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1739 right_justify = zfs_prop_align_right(pl->pl_prop);
1741 if (nvlist_lookup_nvlist(userprops,
1742 pl->pl_user_prop, &propval) != 0)
1745 verify(nvlist_lookup_string(propval,
1746 ZPROP_VALUE, &propstr) == 0);
1749 width = pl->pl_width;
1752 * If this is being called in scripted mode, or if this is the
1753 * last column and it is left-justified, don't include a width
1756 if (scripted || (pl->pl_next == NULL && !right_justify))
1757 (void) printf("%s", propstr);
1758 else if (right_justify)
1759 (void) printf("%*s", width, propstr);
1761 (void) printf("%-*s", width, propstr);
1764 (void) printf("\n");
1768 * Generic callback function to list a dataset or snapshot.
1771 list_callback(zfs_handle_t *zhp, void *data)
1773 list_cbdata_t *cbp = data;
1775 if (cbp->cb_first) {
1776 if (!cbp->cb_scripted)
1777 print_header(cbp->cb_proplist);
1778 cbp->cb_first = B_FALSE;
1781 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1787 zfs_do_list(int argc, char **argv)
1790 boolean_t scripted = B_FALSE;
1791 static char default_fields[] =
1792 "name,used,available,referenced,mountpoint";
1793 int types = ZFS_TYPE_DATASET;
1794 boolean_t types_specified = B_FALSE;
1795 char *fields = NULL;
1796 list_cbdata_t cb = { 0 };
1800 zfs_sort_column_t *sortcol = NULL;
1801 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
1804 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
1810 limit = parse_depth(optarg, &flags);
1813 flags |= ZFS_ITER_RECURSE;
1819 if (zfs_add_sort_column(&sortcol, optarg,
1821 (void) fprintf(stderr,
1822 gettext("invalid property '%s'\n"), optarg);
1827 if (zfs_add_sort_column(&sortcol, optarg,
1829 (void) fprintf(stderr,
1830 gettext("invalid property '%s'\n"), optarg);
1836 types_specified = B_TRUE;
1837 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1838 while (*optarg != '\0') {
1839 static char *type_subopts[] = { "filesystem",
1840 "volume", "snapshot", "all", NULL };
1842 switch (getsubopt(&optarg, type_subopts,
1845 types |= ZFS_TYPE_FILESYSTEM;
1848 types |= ZFS_TYPE_VOLUME;
1851 types |= ZFS_TYPE_SNAPSHOT;
1854 types = ZFS_TYPE_DATASET;
1858 (void) fprintf(stderr,
1859 gettext("invalid type '%s'\n"),
1866 (void) fprintf(stderr, gettext("missing argument for "
1867 "'%c' option\n"), optopt);
1871 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1881 fields = default_fields;
1884 * If "-o space" and no types were specified, don't display snapshots.
1886 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
1887 types &= ~ZFS_TYPE_SNAPSHOT;
1890 * If the user specifies '-o all', the zprop_get_list() doesn't
1891 * normally include the name of the dataset. For 'zfs list', we always
1892 * want this property to be first.
1894 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1898 cb.cb_scripted = scripted;
1899 cb.cb_first = B_TRUE;
1901 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
1902 limit, list_callback, &cb);
1904 zprop_free_list(cb.cb_proplist);
1905 zfs_free_sort_columns(sortcol);
1907 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
1908 (void) printf(gettext("no datasets available\n"));
1914 * zfs rename <fs | snap | vol> <fs | snap | vol>
1915 * zfs rename -p <fs | vol> <fs | vol>
1916 * zfs rename -r <snap> <snap>
1918 * Renames the given dataset to another of the same type.
1920 * The '-p' flag creates all the non-existing ancestors of the target first.
1924 zfs_do_rename(int argc, char **argv)
1929 boolean_t recurse = B_FALSE;
1930 boolean_t parents = B_FALSE;
1933 while ((c = getopt(argc, argv, "pr")) != -1) {
1943 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1952 /* check number of arguments */
1954 (void) fprintf(stderr, gettext("missing source dataset "
1959 (void) fprintf(stderr, gettext("missing target dataset "
1964 (void) fprintf(stderr, gettext("too many arguments\n"));
1968 if (recurse && parents) {
1969 (void) fprintf(stderr, gettext("-p and -r options are mutually "
1974 if (recurse && strchr(argv[0], '@') == 0) {
1975 (void) fprintf(stderr, gettext("source dataset for recursive "
1976 "rename must be a snapshot\n"));
1980 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
1981 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
1984 /* If we were asked and the name looks good, try to create ancestors. */
1985 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
1986 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
1991 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
2000 * Promotes the given clone fs to be the parent
2004 zfs_do_promote(int argc, char **argv)
2010 if (argc > 1 && argv[1][0] == '-') {
2011 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2016 /* check number of arguments */
2018 (void) fprintf(stderr, gettext("missing clone filesystem"
2023 (void) fprintf(stderr, gettext("too many arguments\n"));
2027 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2031 ret = (zfs_promote(zhp) != 0);
2039 * zfs rollback [-rRf] <snapshot>
2041 * -r Delete any intervening snapshots before doing rollback
2042 * -R Delete any snapshots and their clones
2043 * -f ignored for backwards compatability
2045 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2046 * since then and making it the active dataset. If more recent snapshots exist,
2047 * the command will complain unless the '-r' flag is given.
2049 typedef struct rollback_cbdata {
2055 boolean_t cb_recurse;
2056 boolean_t cb_dependent;
2057 } rollback_cbdata_t;
2060 * Report any snapshots more recent than the one specified. Used when '-r' is
2061 * not specified. We reuse this same callback for the snapshot dependents - if
2062 * 'cb_dependent' is set, then this is a dependent and we should report it
2063 * without checking the transaction group.
2066 rollback_check(zfs_handle_t *zhp, void *data)
2068 rollback_cbdata_t *cbp = data;
2070 if (cbp->cb_doclones) {
2075 if (!cbp->cb_dependent) {
2076 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2077 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2078 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2081 if (cbp->cb_first && !cbp->cb_recurse) {
2082 (void) fprintf(stderr, gettext("cannot "
2083 "rollback to '%s': more recent snapshots "
2086 (void) fprintf(stderr, gettext("use '-r' to "
2087 "force deletion of the following "
2093 if (cbp->cb_recurse) {
2094 cbp->cb_dependent = B_TRUE;
2095 if (zfs_iter_dependents(zhp, B_TRUE,
2096 rollback_check, cbp) != 0) {
2100 cbp->cb_dependent = B_FALSE;
2102 (void) fprintf(stderr, "%s\n",
2107 if (cbp->cb_first && cbp->cb_recurse) {
2108 (void) fprintf(stderr, gettext("cannot rollback to "
2109 "'%s': clones of previous snapshots exist\n"),
2111 (void) fprintf(stderr, gettext("use '-R' to "
2112 "force deletion of the following clones and "
2118 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2126 zfs_do_rollback(int argc, char **argv)
2130 boolean_t force = B_FALSE;
2131 rollback_cbdata_t cb = { 0 };
2132 zfs_handle_t *zhp, *snap;
2133 char parentname[ZFS_MAXNAMELEN];
2137 while ((c = getopt(argc, argv, "rRf")) != -1) {
2150 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2159 /* check number of arguments */
2161 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2165 (void) fprintf(stderr, gettext("too many arguments\n"));
2169 /* open the snapshot */
2170 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2173 /* open the parent dataset */
2174 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2175 verify((delim = strrchr(parentname, '@')) != NULL);
2177 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2183 * Check for more recent snapshots and/or clones based on the presence
2186 cb.cb_target = argv[0];
2187 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2188 cb.cb_first = B_TRUE;
2190 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2193 if ((ret = cb.cb_error) != 0)
2197 * Rollback parent to the given snapshot.
2199 ret = zfs_rollback(zhp, snap, force);
2212 * zfs set property=value { fs | snap | vol } ...
2214 * Sets the given property for all datasets specified on the command line.
2216 typedef struct set_cbdata {
2222 set_callback(zfs_handle_t *zhp, void *data)
2224 set_cbdata_t *cbp = data;
2226 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2227 switch (libzfs_errno(g_zfs)) {
2228 case EZFS_MOUNTFAILED:
2229 (void) fprintf(stderr, gettext("property may be set "
2230 "but unable to remount filesystem\n"));
2232 case EZFS_SHARENFSFAILED:
2233 (void) fprintf(stderr, gettext("property may be set "
2234 "but unable to reshare filesystem\n"));
2243 zfs_do_set(int argc, char **argv)
2248 /* check for options */
2249 if (argc > 1 && argv[1][0] == '-') {
2250 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2255 /* check number of arguments */
2257 (void) fprintf(stderr, gettext("missing property=value "
2262 (void) fprintf(stderr, gettext("missing dataset name\n"));
2266 /* validate property=value argument */
2267 cb.cb_propname = argv[1];
2268 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2269 (cb.cb_value[1] == '\0')) {
2270 (void) fprintf(stderr, gettext("missing value in "
2271 "property=value argument\n"));
2275 *cb.cb_value = '\0';
2278 if (*cb.cb_propname == '\0') {
2279 (void) fprintf(stderr,
2280 gettext("missing property in property=value argument\n"));
2284 ret = zfs_for_each(argc - 2, argv + 2, NULL,
2285 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
2291 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2293 * Creates a snapshot with the given name. While functionally equivalent to
2294 * 'zfs create', it is a separate command to differentiate intent.
2297 zfs_do_snapshot(int argc, char **argv)
2299 boolean_t recursive = B_FALSE;
2304 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2305 (void) fprintf(stderr, gettext("internal error: "
2306 "out of memory\n"));
2311 while ((c = getopt(argc, argv, "ro:")) != -1) {
2314 if (parseprop(props))
2321 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2330 /* check number of arguments */
2332 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2336 (void) fprintf(stderr, gettext("too many arguments\n"));
2340 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2342 if (ret && recursive)
2343 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2353 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2354 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2356 * Send a backup stream to stdout.
2359 zfs_do_send(int argc, char **argv)
2361 char *fromname = NULL;
2362 char *toname = NULL;
2365 boolean_t doall = B_FALSE;
2366 boolean_t replicate = B_FALSE;
2367 boolean_t fromorigin = B_FALSE;
2368 boolean_t verbose = B_FALSE;
2372 while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
2392 (void) fprintf(stderr, gettext("missing argument for "
2393 "'%c' option\n"), optopt);
2397 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2406 /* check number of arguments */
2408 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2412 (void) fprintf(stderr, gettext("too many arguments\n"));
2416 if (isatty(STDOUT_FILENO)) {
2417 (void) fprintf(stderr,
2418 gettext("Error: Stream can not be written to a terminal.\n"
2419 "You must redirect standard output.\n"));
2423 cp = strchr(argv[0], '@');
2425 (void) fprintf(stderr,
2426 gettext("argument must be a snapshot\n"));
2431 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2436 * If they specified the full path to the snapshot, chop off
2437 * everything except the short name of the snapshot, but special
2438 * case if they specify the origin.
2440 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2441 char origin[ZFS_MAXNAMELEN];
2444 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2445 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2447 if (strcmp(origin, fromname) == 0) {
2449 fromorigin = B_TRUE;
2452 if (cp != fromname && strcmp(argv[0], fromname)) {
2453 (void) fprintf(stderr,
2454 gettext("incremental source must be "
2455 "in same filesystem\n"));
2459 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2460 (void) fprintf(stderr,
2461 gettext("invalid incremental source\n"));
2467 if (replicate && fromname == NULL)
2470 err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
2471 verbose, STDOUT_FILENO);
2478 * zfs receive [-dnvF] <fs@snap>
2480 * Restore a backup stream from stdin.
2483 zfs_do_receive(int argc, char **argv)
2488 bzero(&flags, sizeof (recvflags_t));
2490 while ((c = getopt(argc, argv, ":dnuvF")) != -1) {
2493 flags.isprefix = B_TRUE;
2496 flags.dryrun = B_TRUE;
2499 flags.nomount = B_TRUE;
2502 flags.verbose = B_TRUE;
2505 flags.force = B_TRUE;
2508 (void) fprintf(stderr, gettext("missing argument for "
2509 "'%c' option\n"), optopt);
2513 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2522 /* check number of arguments */
2524 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2528 (void) fprintf(stderr, gettext("too many arguments\n"));
2532 if (isatty(STDIN_FILENO)) {
2533 (void) fprintf(stderr,
2534 gettext("Error: Backup stream can not be read "
2535 "from a terminal.\n"
2536 "You must redirect standard input.\n"));
2540 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2545 typedef struct allow_cb {
2547 size_t a_treeoffset;
2551 zfs_print_perms(avl_tree_t *tree)
2553 zfs_perm_node_t *permnode;
2555 permnode = avl_first(tree);
2556 while (permnode != NULL) {
2557 (void) printf("%s", permnode->z_pname);
2558 permnode = AVL_NEXT(tree, permnode);
2562 (void) printf("\n");
2567 * Iterate over user/groups/everyone/... and the call perm_iter
2568 * function to print actual permission when tree has >0 nodes.
2571 zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb)
2573 zfs_allow_node_t *item;
2576 item = avl_first(tree);
2578 ptree = (void *)((char *)item + cb->a_treeoffset);
2579 if (avl_numnodes(ptree)) {
2580 if (cb->a_permcnt++ == 0)
2581 (void) printf("%s\n", banner);
2582 (void) printf("\t%s", item->z_key);
2584 * Avoid an extra space being printed
2585 * for "everyone" which is keyed with a null
2588 if (item->z_key[0] != '\0')
2590 zfs_print_perms(ptree);
2592 item = AVL_NEXT(tree, item);
2596 #define LINES "-------------------------------------------------------------\n"
2598 zfs_print_allows(char *ds)
2600 zfs_allow_t *curperms, *perms;
2602 allow_cb_t allowcb = { 0 };
2603 char banner[MAXPATHLEN];
2608 if (strrchr(ds, '@')) {
2609 (void) fprintf(stderr, gettext("Snapshots don't have 'allow'"
2613 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2616 if (zfs_perm_get(zhp, &perms)) {
2617 (void) fprintf(stderr,
2618 gettext("Failed to retrieve 'allows' on %s\n"), ds);
2626 (void) printf("%s", LINES);
2627 for (curperms = perms; curperms; curperms = curperms->z_next) {
2629 (void) snprintf(banner, sizeof (banner),
2630 "Permission sets on (%s)", curperms->z_setpoint);
2631 allowcb.a_treeoffset =
2632 offsetof(zfs_allow_node_t, z_localdescend);
2633 allowcb.a_permcnt = 0;
2634 zfs_iter_perms(&curperms->z_sets, banner, &allowcb);
2636 (void) snprintf(banner, sizeof (banner),
2637 "Create time permissions on (%s)", curperms->z_setpoint);
2638 allowcb.a_treeoffset =
2639 offsetof(zfs_allow_node_t, z_localdescend);
2640 allowcb.a_permcnt = 0;
2641 zfs_iter_perms(&curperms->z_crperms, banner, &allowcb);
2644 (void) snprintf(banner, sizeof (banner),
2645 "Local permissions on (%s)", curperms->z_setpoint);
2646 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local);
2647 allowcb.a_permcnt = 0;
2648 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2649 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2650 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2652 (void) snprintf(banner, sizeof (banner),
2653 "Descendent permissions on (%s)", curperms->z_setpoint);
2654 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend);
2655 allowcb.a_permcnt = 0;
2656 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2657 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2658 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2660 (void) snprintf(banner, sizeof (banner),
2661 "Local+Descendent permissions on (%s)",
2662 curperms->z_setpoint);
2663 allowcb.a_treeoffset =
2664 offsetof(zfs_allow_node_t, z_localdescend);
2665 allowcb.a_permcnt = 0;
2666 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2667 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2668 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2670 (void) printf("%s", LINES);
2672 zfs_free_allows(perms);
2676 #define ALLOWOPTIONS "ldcsu:g:e"
2677 #define UNALLOWOPTIONS "ldcsu:g:er"
2680 * Validate options, and build necessary datastructure to display/remove/add
2682 * Returns 0 - If permissions should be added/removed
2683 * Returns 1 - If permissions should be displayed.
2684 * Returns -1 - on failure
2687 parse_allow_args(int *argc, char **argv[], boolean_t unallow,
2688 char **ds, int *recurse, nvlist_t **zperms)
2691 char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS;
2692 zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE;
2693 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
2698 while ((c = getopt(*argc, *argv, options)) != -1) {
2701 if (who_type == ZFS_DELEG_CREATE ||
2702 who_type == ZFS_DELEG_NAMED_SET)
2705 deleg_type |= ZFS_DELEG_PERM_LOCAL;
2708 if (who_type == ZFS_DELEG_CREATE ||
2709 who_type == ZFS_DELEG_NAMED_SET)
2712 deleg_type |= ZFS_DELEG_PERM_DESCENDENT;
2718 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2722 who_type = ZFS_DELEG_CREATE;
2725 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2729 who_type = ZFS_DELEG_NAMED_SET;
2732 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2734 who_type = ZFS_DELEG_USER;
2738 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2740 who_type = ZFS_DELEG_GROUP;
2744 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2746 who_type = ZFS_DELEG_EVERYONE;
2754 if (deleg_type == 0)
2755 deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT;
2760 if (unallow == B_FALSE && *argc == 1) {
2762 * Only print permissions if no options were processed
2771 * initialize variables for zfs_build_perms based on number
2773 * 3 arguments ==> zfs [un]allow joe perm,perm,perm <dataset> or
2774 * zfs [un]allow -s @set1 perm,perm <dataset>
2775 * 2 arguments ==> zfs [un]allow -c perm,perm <dataset> or
2776 * zfs [un]allow -u|-g <name> perm <dataset> or
2777 * zfs [un]allow -e perm,perm <dataset>
2778 * zfs unallow joe <dataset>
2779 * zfs unallow -s @set1 <dataset>
2780 * 1 argument ==> zfs [un]allow -e <dataset> or
2781 * zfs [un]allow -c <dataset>
2791 * advance argc/argv for do_allow cases.
2792 * for do_allow case make sure who have a know who type
2793 * and its not a permission set.
2795 if (unallow == B_TRUE) {
2798 } else if (who_type != ZFS_DELEG_WHO_UNKNOWN &&
2799 who_type != ZFS_DELEG_NAMED_SET)
2804 if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE ||
2805 who_type == ZFS_DELEG_CREATE || who != NULL)) {
2809 if (unallow == B_FALSE &&
2810 (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2811 who_type == ZFS_DELEG_NAMED_SET))
2813 else if (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2814 who_type == ZFS_DELEG_NAMED_SET)
2816 else if (who_type != ZFS_DELEG_NAMED_SET)
2820 if (unallow == B_TRUE) {
2827 if (unallow == B_FALSE)
2829 if (who == NULL && who_type != ZFS_DELEG_CREATE &&
2830 who_type != ZFS_DELEG_EVERYONE)
2839 if (strrchr(*ds, '@')) {
2840 (void) fprintf(stderr,
2841 gettext("Can't set or remove 'allow' permissions "
2842 "on snapshots.\n"));
2846 if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_DATASET)) == NULL)
2849 if ((zfs_build_perms(zhp, who, perms,
2850 who_type, deleg_type, zperms)) != 0) {
2859 zfs_do_allow(int argc, char **argv)
2862 nvlist_t *zperms = NULL;
2867 if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds,
2868 &unused, &zperms)) == -1)
2872 return (zfs_print_allows(argv[0]));
2874 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2877 if (zfs_perm_set(zhp, zperms)) {
2879 nvlist_free(zperms);
2882 nvlist_free(zperms);
2889 unallow_callback(zfs_handle_t *zhp, void *data)
2891 nvlist_t *nvp = (nvlist_t *)data;
2894 error = zfs_perm_remove(zhp, nvp);
2896 (void) fprintf(stderr, gettext("Failed to remove permissions "
2897 "on %s\n"), zfs_get_name(zhp));
2903 zfs_do_unallow(int argc, char **argv)
2905 int recurse = B_FALSE;
2908 nvlist_t *zperms = NULL;
2911 if (parse_allow_args(&argc, &argv, B_TRUE,
2912 &ds, &recurse, &zperms) == -1)
2916 flags |= ZFS_ITER_RECURSE;
2917 error = zfs_for_each(argc, argv, flags,
2918 ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
2919 NULL, 0, unallow_callback, (void *)zperms);
2922 nvlist_free(zperms);
2927 typedef struct get_all_cbdata {
2928 zfs_handle_t **cb_handles;
2932 boolean_t cb_verbose;
2935 #define CHECK_SPINNER 30
2936 #define SPINNER_TIME 3 /* seconds */
2937 #define MOUNT_TIME 5 /* seconds */
2940 get_one_dataset(zfs_handle_t *zhp, void *data)
2942 static char spin[] = { '-', '\\', '|', '/' };
2943 static int spinval = 0;
2944 static int spincheck = 0;
2945 static time_t last_spin_time = (time_t)0;
2946 get_all_cbdata_t *cbp = data;
2947 zfs_type_t type = zfs_get_type(zhp);
2949 if (cbp->cb_verbose) {
2950 if (--spincheck < 0) {
2951 time_t now = time(NULL);
2952 if (last_spin_time + SPINNER_TIME < now) {
2953 (void) printf("\b%c", spin[spinval++ % 4]);
2954 (void) fflush(stdout);
2955 last_spin_time = now;
2957 spincheck = CHECK_SPINNER;
2962 * Interate over any nested datasets.
2964 if (type == ZFS_TYPE_FILESYSTEM &&
2965 zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2971 * Skip any datasets whose type does not match.
2973 if ((type & cbp->cb_types) == 0) {
2978 if (cbp->cb_alloc == cbp->cb_used) {
2979 zfs_handle_t **handles;
2981 if (cbp->cb_alloc == 0)
2986 handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2988 if (cbp->cb_handles) {
2989 bcopy(cbp->cb_handles, handles,
2990 cbp->cb_used * sizeof (void *));
2991 free(cbp->cb_handles);
2994 cbp->cb_handles = handles;
2997 cbp->cb_handles[cbp->cb_used++] = zhp;
3003 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
3006 get_all_cbdata_t cb = { 0 };
3007 cb.cb_types = types;
3008 cb.cb_verbose = verbose;
3011 (void) printf("%s: *", gettext("Reading ZFS config"));
3012 (void) fflush(stdout);
3015 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
3017 *dslist = cb.cb_handles;
3018 *count = cb.cb_used;
3021 (void) printf("\b%s\n", gettext("done."));
3026 dataset_cmp(const void *a, const void *b)
3028 zfs_handle_t **za = (zfs_handle_t **)a;
3029 zfs_handle_t **zb = (zfs_handle_t **)b;
3030 char mounta[MAXPATHLEN];
3031 char mountb[MAXPATHLEN];
3032 boolean_t gota, gotb;
3034 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
3035 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
3036 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
3037 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
3038 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
3039 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
3042 return (strcmp(mounta, mountb));
3049 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
3053 * Generic callback for sharing or mounting filesystems. Because the code is so
3054 * similar, we have a common function with an extra parameter to determine which
3055 * mode we are using.
3057 #define OP_SHARE 0x1
3058 #define OP_MOUNT 0x2
3061 * Share or mount a dataset.
3064 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3065 boolean_t explicit, const char *options)
3067 char mountpoint[ZFS_MAXPROPLEN];
3068 char shareopts[ZFS_MAXPROPLEN];
3069 char smbshareopts[ZFS_MAXPROPLEN];
3070 const char *cmdname = op == OP_SHARE ? "share" : "mount";
3072 uint64_t zoned, canmount;
3073 zfs_type_t type = zfs_get_type(zhp);
3074 boolean_t shared_nfs, shared_smb;
3076 assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
3078 if (type == ZFS_TYPE_FILESYSTEM) {
3080 * Check to make sure we can mount/share this dataset. If we
3081 * are in the global zone and the filesystem is exported to a
3082 * local zone, or if we are in a local zone and the
3083 * filesystem is not exported, then it is an error.
3085 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3087 if (zoned && getzoneid() == GLOBAL_ZONEID) {
3091 (void) fprintf(stderr, gettext("cannot %s '%s': "
3092 "dataset is exported to a local zone\n"), cmdname,
3096 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3100 (void) fprintf(stderr, gettext("cannot %s '%s': "
3101 "permission denied\n"), cmdname,
3107 * Ignore any filesystems which don't apply to us. This
3108 * includes those with a legacy mountpoint, or those with
3109 * legacy share options.
3111 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3112 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3113 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3114 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3115 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3116 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3117 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3119 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3120 strcmp(smbshareopts, "off") == 0) {
3124 (void) fprintf(stderr, gettext("cannot share '%s': "
3125 "legacy share\n"), zfs_get_name(zhp));
3126 (void) fprintf(stderr, gettext("use share(1M) to "
3127 "share this filesystem\n"));
3132 * We cannot share or mount legacy filesystems. If the
3133 * shareopts is non-legacy but the mountpoint is legacy, we
3134 * treat it as a legacy share.
3136 if (strcmp(mountpoint, "legacy") == 0) {
3140 (void) fprintf(stderr, gettext("cannot %s '%s': "
3141 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3142 (void) fprintf(stderr, gettext("use %s(1M) to "
3143 "%s this filesystem\n"), cmdname, cmdname);
3147 if (strcmp(mountpoint, "none") == 0) {
3151 (void) fprintf(stderr, gettext("cannot %s '%s': no "
3152 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3157 * canmount explicit outcome
3158 * on no pass through
3159 * on yes pass through
3161 * off yes display error, return 1
3162 * noauto no return 0
3163 * noauto yes pass through
3165 if (canmount == ZFS_CANMOUNT_OFF) {
3169 (void) fprintf(stderr, gettext("cannot %s '%s': "
3170 "'canmount' property is set to 'off'\n"), cmdname,
3173 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3178 * At this point, we have verified that the mountpoint and/or
3179 * shareopts are appropriate for auto management. If the
3180 * filesystem is already mounted or shared, return (failing
3181 * for explicit requests); otherwise mount or share the
3187 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3188 shared_smb = zfs_is_shared_smb(zhp, NULL);
3190 if (shared_nfs && shared_smb ||
3191 (shared_nfs && strcmp(shareopts, "on") == 0 &&
3192 strcmp(smbshareopts, "off") == 0) ||
3193 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3194 strcmp(shareopts, "off") == 0)) {
3198 (void) fprintf(stderr, gettext("cannot share "
3199 "'%s': filesystem already shared\n"),
3204 if (!zfs_is_mounted(zhp, NULL) &&
3205 zfs_mount(zhp, NULL, 0) != 0)
3208 if (protocol == NULL) {
3209 if (zfs_shareall(zhp) != 0)
3211 } else if (strcmp(protocol, "nfs") == 0) {
3212 if (zfs_share_nfs(zhp))
3214 } else if (strcmp(protocol, "smb") == 0) {
3215 if (zfs_share_smb(zhp))
3218 (void) fprintf(stderr, gettext("cannot share "
3219 "'%s': invalid share type '%s' "
3221 zfs_get_name(zhp), protocol);
3228 if (options == NULL)
3229 mnt.mnt_mntopts = "";
3231 mnt.mnt_mntopts = (char *)options;
3233 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3234 zfs_is_mounted(zhp, NULL)) {
3238 (void) fprintf(stderr, gettext("cannot mount "
3239 "'%s': filesystem already mounted\n"),
3244 if (zfs_mount(zhp, options, flags) != 0)
3249 assert(op == OP_SHARE);
3252 * Ignore any volumes that aren't shared.
3254 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
3255 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3257 if (strcmp(shareopts, "off") == 0) {
3261 (void) fprintf(stderr, gettext("cannot share '%s': "
3262 "'shareiscsi' property not set\n"),
3264 (void) fprintf(stderr, gettext("set 'shareiscsi' "
3265 "property or use iscsitadm(1M) to share this "
3270 if (zfs_is_shared_iscsi(zhp)) {
3274 (void) fprintf(stderr, gettext("cannot share "
3275 "'%s': volume already shared\n"),
3280 if (zfs_share_iscsi(zhp) != 0)
3288 * Reports progress in the form "(current/total)". Not thread-safe.
3291 report_mount_progress(int current, int total)
3294 static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3295 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3296 static time_t last_progress_time;
3297 time_t now = time(NULL);
3299 /* report 1..n instead of 0..n-1 */
3302 /* display header if we're here for the first time */
3304 (void) printf(gettext("Mounting ZFS filesystems: "));
3306 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3307 /* too soon to report again */
3311 last_progress_time = now;
3313 /* back up to prepare for overwriting */
3315 (void) printf("%*.*s", len, len, reverse);
3317 /* We put a newline at the end if this is the last one. */
3318 len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
3319 (void) fflush(stdout);
3323 append_options(char *mntopts, char *newopts)
3325 int len = strlen(mntopts);
3327 /* original length plus new string to append plus 1 for the comma */
3328 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3329 (void) fprintf(stderr, gettext("the opts argument for "
3330 "'%c' option is too long (more than %d chars)\n"),
3331 "-o", MNT_LINE_MAX);
3336 mntopts[len++] = ',';
3338 (void) strcpy(&mntopts[len], newopts);
3342 share_mount(int op, int argc, char **argv)
3345 boolean_t verbose = B_FALSE;
3347 char *options = NULL;
3348 int types, flags = 0;
3351 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3361 if (*optarg == '\0') {
3362 (void) fprintf(stderr, gettext("empty mount "
3363 "options (-o) specified\n"));
3367 if (options == NULL)
3368 options = safe_malloc(MNT_LINE_MAX + 1);
3370 /* option validation is done later */
3371 append_options(options, optarg);
3375 warnx("no overlay mounts support on FreeBSD, ignoring");
3378 (void) fprintf(stderr, gettext("missing argument for "
3379 "'%c' option\n"), optopt);
3383 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3392 /* check number of arguments */
3394 zfs_handle_t **dslist = NULL;
3395 size_t i, count = 0;
3396 char *protocol = NULL;
3398 if (op == OP_MOUNT) {
3399 types = ZFS_TYPE_FILESYSTEM;
3400 } else if (argc > 0) {
3401 if (strcmp(argv[0], "nfs") == 0 ||
3402 strcmp(argv[0], "smb") == 0) {
3403 types = ZFS_TYPE_FILESYSTEM;
3404 } else if (strcmp(argv[0], "iscsi") == 0) {
3405 types = ZFS_TYPE_VOLUME;
3407 (void) fprintf(stderr, gettext("share type "
3408 "must be 'nfs', 'smb' or 'iscsi'\n"));
3415 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3419 (void) fprintf(stderr, gettext("too many arguments\n"));
3423 get_all_datasets(types, &dslist, &count, verbose);
3428 qsort(dslist, count, sizeof (void *), dataset_cmp);
3430 for (i = 0; i < count; i++) {
3432 report_mount_progress(i, count);
3434 if (share_mount_one(dslist[i], op, flags, protocol,
3435 B_FALSE, options) != 0)
3437 zfs_close(dslist[i]);
3441 } else if (argc == 0) {
3445 if ((op == OP_SHARE) || (options != NULL)) {
3446 (void) fprintf(stderr, gettext("missing filesystem "
3447 "argument (specify -a for all)\n"));
3452 * When mount is given no arguments, go through /etc/mnttab and
3453 * display any active ZFS mounts. We hide any snapshots, since
3454 * they are controlled automatically.
3456 if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
3457 fprintf(stderr, "getmntinfo(): %s\n", strerror(errno));
3460 for (i = 0; i < n; i++) {
3461 if (strcmp(sfs[i].f_fstypename, MNTTYPE_ZFS) != 0 ||
3462 strchr(sfs[i].f_mntfromname, '@') != NULL)
3465 (void) printf("%-30s %s\n", sfs[i].f_mntfromname,
3466 sfs[i].f_mntonname);
3472 types = ZFS_TYPE_FILESYSTEM;
3474 types |= ZFS_TYPE_VOLUME;
3477 (void) fprintf(stderr,
3478 gettext("too many arguments\n"));
3482 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3485 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3495 * zfs mount -a [nfs | iscsi]
3496 * zfs mount filesystem
3498 * Mount all filesystems, or mount the given filesystem.
3501 zfs_do_mount(int argc, char **argv)
3503 return (share_mount(OP_MOUNT, argc, argv));
3507 * zfs share -a [nfs | iscsi | smb]
3508 * zfs share filesystem
3510 * Share all filesystems, or share the given filesystem.
3513 zfs_do_share(int argc, char **argv)
3515 return (share_mount(OP_SHARE, argc, argv));
3518 typedef struct unshare_unmount_node {
3519 zfs_handle_t *un_zhp;
3521 uu_avl_node_t un_avlnode;
3522 } unshare_unmount_node_t;
3526 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3528 const unshare_unmount_node_t *l = larg;
3529 const unshare_unmount_node_t *r = rarg;
3531 return (strcmp(l->un_mountp, r->un_mountp));
3535 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3536 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3537 * and unmount it appropriately.
3540 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3544 struct stat64 statbuf;
3545 struct mnttab search = { 0 }, entry;
3546 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3550 * Search for the path in /etc/mnttab. Rather than looking for the
3551 * specific path, which can be fooled by non-standard paths (i.e. ".."
3552 * or "//"), we stat() the path and search for the corresponding
3553 * (major,minor) device pair.
3555 if (stat64(path, &statbuf) != 0) {
3556 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3557 cmdname, path, strerror(errno));
3560 path_inode = statbuf.st_ino;
3563 * Search for the given (major,minor) pair in the mount table.
3565 search.mnt_mountp = path;
3566 rewind(mnttab_file);
3567 if (getmntany(mnttab_file, &entry, &search) != 0) {
3568 if (op == OP_SHARE) {
3569 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3570 "currently mounted\n"), cmdname, path);
3573 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3575 if ((ret = umount2(path, flags)) != 0)
3576 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3581 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3582 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3583 "filesystem\n"), cmdname, path);
3587 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3588 ZFS_TYPE_FILESYSTEM)) == NULL)
3592 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3593 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3594 cmdname, path, strerror(errno));
3596 } else if (statbuf.st_ino != path_inode) {
3597 (void) fprintf(stderr, gettext("cannot "
3598 "%s '%s': not a mountpoint\n"), cmdname, path);
3602 if (op == OP_SHARE) {
3603 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3604 char smbshare_prop[ZFS_MAXPROPLEN];
3606 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3607 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3608 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3609 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3611 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3612 strcmp(smbshare_prop, "off") == 0) {
3613 (void) fprintf(stderr, gettext("cannot unshare "
3614 "'%s': legacy share\n"), path);
3615 (void) fprintf(stderr, gettext("use "
3616 "unshare(1M) to unshare this filesystem\n"));
3617 } else if (!zfs_is_shared(zhp)) {
3618 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3619 "not currently shared\n"), path);
3621 ret = zfs_unshareall_bypath(zhp, path);
3624 char mtpt_prop[ZFS_MAXPROPLEN];
3626 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3627 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3630 ret = zfs_unmount(zhp, NULL, flags);
3631 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3632 (void) fprintf(stderr, gettext("cannot unmount "
3633 "'%s': legacy mountpoint\n"),
3635 (void) fprintf(stderr, gettext("use umount(1M) "
3636 "to unmount this filesystem\n"));
3638 ret = zfs_unmountall(zhp, flags);
3649 * Generic callback for unsharing or unmounting a filesystem.
3652 unshare_unmount(int op, int argc, char **argv)
3659 char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3660 char sharesmb[ZFS_MAXPROPLEN];
3663 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3672 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3683 * We could make use of zfs_for_each() to walk all datasets in
3684 * the system, but this would be very inefficient, especially
3685 * since we would have to linearly search /etc/mnttab for each
3686 * one. Instead, do one pass through /etc/mnttab looking for
3687 * zfs entries and call zfs_unmount() for each one.
3689 * Things get a little tricky if the administrator has created
3690 * mountpoints beneath other ZFS filesystems. In this case, we
3691 * have to unmount the deepest filesystems first. To accomplish
3692 * this, we place all the mountpoints in an AVL tree sorted by
3693 * the special type (dataset name), and walk the result in
3694 * reverse to make sure to get any snapshots first.
3696 uu_avl_pool_t *pool;
3698 unshare_unmount_node_t *node;
3700 uu_avl_walk_t *walk;
3705 (void) fprintf(stderr, gettext("too many arguments\n"));
3709 if ((pool = uu_avl_pool_create("unmount_pool",
3710 sizeof (unshare_unmount_node_t),
3711 offsetof(unshare_unmount_node_t, un_avlnode),
3712 unshare_unmount_compare,
3713 UU_DEFAULT)) == NULL) {
3714 (void) fprintf(stderr, gettext("internal error: "
3715 "out of memory\n"));
3719 if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3720 (void) fprintf(stderr, gettext("internal error: "
3721 "out of memory\n"));
3725 if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
3726 (void) fprintf(stderr, gettext("internal error: "
3727 "getmntinfo() failed\n"));
3730 for (i = 0; i < n; i++) {
3732 /* ignore non-ZFS entries */
3733 if (strcmp(sfs[i].f_fstypename, MNTTYPE_ZFS) != 0)
3736 /* ignore snapshots */
3737 if (strchr(sfs[i].f_mntfromname, '@') != NULL)
3740 if ((zhp = zfs_open(g_zfs, sfs[i].f_mntfromname,
3741 ZFS_TYPE_FILESYSTEM)) == NULL) {
3748 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3750 sizeof (nfsiscsi_mnt_prop),
3751 NULL, NULL, 0, B_FALSE) == 0);
3752 if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3754 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3756 sizeof (nfsiscsi_mnt_prop),
3757 NULL, NULL, 0, B_FALSE) == 0);
3758 if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3762 /* Ignore legacy mounts */
3763 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3765 sizeof (nfsiscsi_mnt_prop),
3766 NULL, NULL, 0, B_FALSE) == 0);
3767 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3769 /* Ignore canmount=noauto mounts */
3770 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3771 ZFS_CANMOUNT_NOAUTO)
3777 node = safe_malloc(sizeof (unshare_unmount_node_t));
3780 if ((node->un_mountp = strdup(sfs[i].f_mntonname)) ==
3782 (void) fprintf(stderr, gettext("internal error:"
3783 " out of memory\n"));
3787 uu_avl_node_init(node, &node->un_avlnode, pool);
3789 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3790 uu_avl_insert(tree, node, idx);
3792 zfs_close(node->un_zhp);
3793 free(node->un_mountp);
3799 * Walk the AVL tree in reverse, unmounting each filesystem and
3800 * removing it from the AVL tree in the process.
3802 if ((walk = uu_avl_walk_start(tree,
3803 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3804 (void) fprintf(stderr,
3805 gettext("internal error: out of memory"));
3809 while ((node = uu_avl_walk_next(walk)) != NULL) {
3810 uu_avl_remove(tree, node);
3814 if (zfs_unshareall_bypath(node->un_zhp,
3815 node->un_mountp) != 0)
3820 if (zfs_unmount(node->un_zhp,
3821 node->un_mountp, flags) != 0)
3826 zfs_close(node->un_zhp);
3827 free(node->un_mountp);
3831 uu_avl_walk_end(walk);
3832 uu_avl_destroy(tree);
3833 uu_avl_pool_destroy(pool);
3835 if (op == OP_SHARE) {
3837 * Finally, unshare any volumes shared via iSCSI.
3839 zfs_handle_t **dslist = NULL;
3840 size_t i, count = 0;
3842 get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
3846 qsort(dslist, count, sizeof (void *),
3849 for (i = 0; i < count; i++) {
3850 if (zfs_unshare_iscsi(dslist[i]) != 0)
3852 zfs_close(dslist[i]);
3861 (void) fprintf(stderr,
3862 gettext("missing filesystem argument\n"));
3864 (void) fprintf(stderr,
3865 gettext("too many arguments\n"));
3870 * We have an argument, but it may be a full path or a ZFS
3871 * filesystem. Pass full paths off to unmount_path() (shared by
3872 * manual_unmount), otherwise open the filesystem and pass to
3875 if (argv[0][0] == '/')
3876 return (unshare_unmount_path(op, argv[0],
3879 types = ZFS_TYPE_FILESYSTEM;
3881 types |= ZFS_TYPE_VOLUME;
3883 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3886 if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
3887 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3888 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3889 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3890 NULL, 0, B_FALSE) == 0);
3894 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3896 sizeof (nfsiscsi_mnt_prop),
3897 NULL, NULL, 0, B_FALSE) == 0);
3898 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3899 sharesmb, sizeof (sharesmb), NULL, NULL,
3902 if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3903 strcmp(sharesmb, "off") == 0) {
3904 (void) fprintf(stderr, gettext("cannot "
3905 "unshare '%s': legacy share\n"),
3907 (void) fprintf(stderr, gettext("use "
3908 "unshare(1M) to unshare this "
3911 } else if (!zfs_is_shared(zhp)) {
3912 (void) fprintf(stderr, gettext("cannot "
3913 "unshare '%s': not currently "
3914 "shared\n"), zfs_get_name(zhp));
3916 } else if (zfs_unshareall(zhp) != 0) {
3922 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3923 (void) fprintf(stderr, gettext("cannot "
3924 "unmount '%s': legacy "
3925 "mountpoint\n"), zfs_get_name(zhp));
3926 (void) fprintf(stderr, gettext("use "
3927 "umount(1M) to unmount this "
3930 } else if (!zfs_is_mounted(zhp, NULL)) {
3931 (void) fprintf(stderr, gettext("cannot "
3932 "unmount '%s': not currently "
3936 } else if (zfs_unmountall(zhp, flags) != 0) {
3942 assert(op == OP_SHARE);
3944 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3945 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3946 NULL, NULL, 0, B_FALSE) == 0);
3948 if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
3949 (void) fprintf(stderr, gettext("cannot unshare "
3950 "'%s': 'shareiscsi' property not set\n"),
3952 (void) fprintf(stderr, gettext("set "
3953 "'shareiscsi' property or use "
3954 "iscsitadm(1M) to share this volume\n"));
3956 } else if (!zfs_is_shared_iscsi(zhp)) {
3957 (void) fprintf(stderr, gettext("cannot "
3958 "unshare '%s': not currently shared\n"),
3961 } else if (zfs_unshare_iscsi(zhp) != 0) {
3974 * zfs unmount filesystem
3976 * Unmount all filesystems, or a specific ZFS filesystem.
3979 zfs_do_unmount(int argc, char **argv)
3981 return (unshare_unmount(OP_MOUNT, argc, argv));
3986 * zfs unshare filesystem
3988 * Unshare all filesystems, or a specific ZFS filesystem.
3991 zfs_do_unshare(int argc, char **argv)
3993 return (unshare_unmount(OP_SHARE, argc, argv));
3997 * Attach/detach the given dataset to/from the given jail
4001 do_jail(int argc, char **argv, int attach)
4006 /* check number of arguments */
4008 (void) fprintf(stderr, gettext("missing argument(s)\n"));
4012 (void) fprintf(stderr, gettext("too many arguments\n"));
4016 jailid = atoi(argv[1]);
4018 (void) fprintf(stderr, gettext("invalid jailid\n"));
4022 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
4026 ret = (zfs_jail(zhp, jailid, attach) != 0);
4033 * zfs jail jailid filesystem
4035 * Attach the given dataset to the given jail
4039 zfs_do_jail(int argc, char **argv)
4042 return (do_jail(argc, argv, 1));
4046 * zfs unjail jailid filesystem
4048 * Detach the given dataset from the given jail
4052 zfs_do_unjail(int argc, char **argv)
4055 return (do_jail(argc, argv, 0));
4059 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
4060 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
4063 manual_mount(int argc, char **argv)
4066 char mountpoint[ZFS_MAXPROPLEN];
4067 char mntopts[MNT_LINE_MAX] = { '\0' };
4071 char *dataset, *path;
4074 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
4077 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
4080 #if 0 /* FreeBSD: No support for MS_OVERLAY. */
4081 flags |= MS_OVERLAY;
4085 #if 0 /* FreeBSD: No support for MS_NOMNTTAB. */
4086 flags |= MS_NOMNTTAB;
4090 (void) fprintf(stderr, gettext("missing argument for "
4091 "'%c' option\n"), optopt);
4095 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4097 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
4106 /* check that we only have two arguments */
4109 (void) fprintf(stderr, gettext("missing dataset "
4112 (void) fprintf(stderr,
4113 gettext("missing mountpoint argument\n"));
4115 (void) fprintf(stderr, gettext("too many arguments\n"));
4116 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
4123 /* try to open the dataset */
4124 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
4127 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
4128 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
4130 /* check for legacy mountpoint and complain appropriately */
4132 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
4133 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
4134 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
4135 (void) fprintf(stderr, gettext("mount failed: %s\n"),
4140 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
4141 "mounted using 'mount -F zfs'\n"), dataset);
4142 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
4143 "instead.\n"), path);
4144 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
4145 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
4146 (void) fprintf(stderr, gettext("See zfs(1M) for more "
4155 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
4156 * unmounts of non-legacy filesystems, as this is the dominant administrative
4160 manual_unmount(int argc, char **argv)
4166 while ((c = getopt(argc, argv, "f")) != -1) {
4172 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4174 (void) fprintf(stderr, gettext("usage: unmount [-f] "
4183 /* check arguments */
4186 (void) fprintf(stderr, gettext("missing path "
4189 (void) fprintf(stderr, gettext("too many arguments\n"));
4190 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
4194 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
4198 volcheck(zpool_handle_t *zhp, void *data)
4200 boolean_t isinit = *((boolean_t *)data);
4203 return (zpool_create_zvol_links(zhp));
4205 return (zpool_remove_zvol_links(zhp));
4209 * Iterate over all pools in the system and either create or destroy /dev/zvol
4210 * links, depending on the value of 'isinit'.
4213 do_volcheck(boolean_t isinit)
4215 return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
4219 find_command_idx(char *command, int *idx)
4223 for (i = 0; i < NCOMMAND; i++) {
4224 if (command_table[i].name == NULL)
4227 if (strcmp(command, command_table[i].name) == 0) {
4236 main(int argc, char **argv)
4243 (void) setlocale(LC_ALL, "");
4244 (void) textdomain(TEXT_DOMAIN);
4248 if ((g_zfs = libzfs_init()) == NULL) {
4249 (void) fprintf(stderr, gettext("internal error: failed to "
4250 "initialize ZFS library\n"));
4254 zpool_set_history_str("zfs", argc, argv, history_str);
4255 verify(zpool_stage_history(g_zfs, history_str) == 0);
4257 libzfs_print_on_error(g_zfs, B_TRUE);
4259 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4260 (void) fprintf(stderr, gettext("internal error: unable to "
4261 "open %s\n"), MNTTAB);
4266 * This command also doubles as the /etc/fs mount and unmount program.
4267 * Determine if we should take this behavior based on argv[0].
4269 progname = basename(argv[0]);
4270 if (strcmp(progname, "mount") == 0) {
4271 ret = manual_mount(argc, argv);
4272 } else if (strcmp(progname, "umount") == 0) {
4273 ret = manual_unmount(argc, argv);
4276 * Make sure the user has specified some command.
4279 (void) fprintf(stderr, gettext("missing command\n"));
4286 * The 'umount' command is an alias for 'unmount'
4288 if (strcmp(cmdname, "umount") == 0)
4289 cmdname = "unmount";
4292 * The 'recv' command is an alias for 'receive'
4294 if (strcmp(cmdname, "recv") == 0)
4295 cmdname = "receive";
4300 if (strcmp(cmdname, "-?") == 0)
4304 * 'volinit' and 'volfini' do not appear in the usage message,
4305 * so we have to special case them here.
4307 if (strcmp(cmdname, "volinit") == 0)
4308 return (do_volcheck(B_TRUE));
4309 else if (strcmp(cmdname, "volfini") == 0)
4310 return (do_volcheck(B_FALSE));
4313 * Run the appropriate command.
4315 if (find_command_idx(cmdname, &i) == 0) {
4316 current_command = &command_table[i];
4317 ret = command_table[i].func(argc - 1, argv + 1);
4318 } else if (strchr(cmdname, '=') != NULL) {
4319 verify(find_command_idx("set", &i) == 0);
4320 current_command = &command_table[i];
4321 ret = command_table[i].func(argc, argv);
4323 (void) fprintf(stderr, gettext("unrecognized "
4324 "command '%s'\n"), cmdname);
4329 (void) fclose(mnttab_file);
4334 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4335 * for the purposes of running ::findleaks.
4337 if (getenv("ZFS_ABORT") != NULL) {
4338 (void) printf("dumping core by request\n");