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.
27 #pragma ident "@(#)zfs_main.c 1.58 08/03/24 SMI"
35 #include <libnvpair.h>
44 #include <sys/mkdev.h>
45 #include <sys/mntent.h>
46 #include <sys/mnttab.h>
47 #include <sys/mount.h>
57 libzfs_handle_t *g_zfs;
59 static FILE *mnttab_file;
60 static char history_str[HIS_MAX_RECORD_LEN];
62 static int zfs_do_clone(int argc, char **argv);
63 static int zfs_do_create(int argc, char **argv);
64 static int zfs_do_destroy(int argc, char **argv);
65 static int zfs_do_get(int argc, char **argv);
66 static int zfs_do_inherit(int argc, char **argv);
67 static int zfs_do_list(int argc, char **argv);
68 static int zfs_do_mount(int argc, char **argv);
69 static int zfs_do_rename(int argc, char **argv);
70 static int zfs_do_rollback(int argc, char **argv);
71 static int zfs_do_set(int argc, char **argv);
72 static int zfs_do_upgrade(int argc, char **argv);
73 static int zfs_do_snapshot(int argc, char **argv);
74 static int zfs_do_unmount(int argc, char **argv);
75 static int zfs_do_share(int argc, char **argv);
76 static int zfs_do_unshare(int argc, char **argv);
77 static int zfs_do_send(int argc, char **argv);
78 static int zfs_do_receive(int argc, char **argv);
79 static int zfs_do_promote(int argc, char **argv);
80 static int zfs_do_allow(int argc, char **argv);
81 static int zfs_do_unallow(int argc, char **argv);
84 * These libumem hooks provide a reasonable set of defaults for the allocator's
85 * debugging facilities.
88 _umem_debug_init(void)
90 return ("default,verbose"); /* $UMEM_DEBUG setting */
94 _umem_logging_init(void)
96 return ("fail,contents"); /* $UMEM_LOGGING setting */
122 typedef struct zfs_command {
124 int (*func)(int argc, char **argv);
129 * Master command table. Each ZFS command has a name, associated function, and
130 * usage message. The usage messages need to be internationalized, so we have
131 * to have a function to return the usage message based on a command index.
133 * These commands are organized according to how they are displayed in the usage
134 * message. An empty command (one with a NULL name) indicates an empty line in
135 * the generic usage message.
137 static zfs_command_t command_table[] = {
138 { "create", zfs_do_create, HELP_CREATE },
139 { "destroy", zfs_do_destroy, HELP_DESTROY },
141 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
142 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
143 { "clone", zfs_do_clone, HELP_CLONE },
144 { "promote", zfs_do_promote, HELP_PROMOTE },
145 { "rename", zfs_do_rename, HELP_RENAME },
147 { "list", zfs_do_list, HELP_LIST },
149 { "set", zfs_do_set, HELP_SET },
150 { "get", zfs_do_get, HELP_GET },
151 { "inherit", zfs_do_inherit, HELP_INHERIT },
152 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
154 { "mount", zfs_do_mount, HELP_MOUNT },
155 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
156 { "share", zfs_do_share, HELP_SHARE },
157 { "unshare", zfs_do_unshare, HELP_UNSHARE },
159 { "send", zfs_do_send, HELP_SEND },
160 { "receive", zfs_do_receive, HELP_RECEIVE },
162 { "allow", zfs_do_allow, HELP_ALLOW },
164 { "unallow", zfs_do_unallow, HELP_UNALLOW },
167 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
169 zfs_command_t *current_command;
172 get_usage(zfs_help_t idx)
176 return (gettext("\tclone [-p] <snapshot> "
177 "<filesystem|volume>\n"));
179 return (gettext("\tcreate [-p] [-o property=value] ... "
181 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
182 "-V <size> <volume>\n"));
184 return (gettext("\tdestroy [-rRf] "
185 "<filesystem|volume|snapshot>\n"));
187 return (gettext("\tget [-rHp] [-o field[,...]] "
188 "[-s source[,...]]\n"
189 "\t <\"all\" | property[,...]> "
190 "[filesystem|volume|snapshot] ...\n"));
192 return (gettext("\tinherit [-r] <property> "
193 "<filesystem|volume> ...\n"));
195 return (gettext("\tupgrade [-v]\n"
196 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
198 return (gettext("\tlist [-rH] [-o property[,...]] "
199 "[-t type[,...]] [-s property] ...\n"
200 "\t [-S property] ... "
201 "[filesystem|volume|snapshot] ...\n"));
203 return (gettext("\tmount\n"
204 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
206 return (gettext("\tpromote <clone-filesystem>\n"));
208 return (gettext("\treceive [-vnF] <filesystem|volume|"
210 "\treceive [-vnF] -d <filesystem>\n"));
212 return (gettext("\trename <filesystem|volume|snapshot> "
213 "<filesystem|volume|snapshot>\n"
214 "\trename -p <filesystem|volume> <filesystem|volume>\n"
215 "\trename -r <snapshot> <snapshot>"));
217 return (gettext("\trollback [-rRf] <snapshot>\n"));
219 return (gettext("\tsend [-R] [-[iI] snapshot] <snapshot>\n"));
221 return (gettext("\tset <property=value> "
222 "<filesystem|volume> ...\n"));
224 return (gettext("\tshare <-a | filesystem>\n"));
226 return (gettext("\tsnapshot [-r] "
227 "<filesystem@snapname|volume@snapname>\n"));
229 return (gettext("\tunmount [-f] "
230 "<-a | filesystem|mountpoint>\n"));
232 return (gettext("\tunshare [-f] "
233 "<-a | filesystem|mountpoint>\n"));
235 return (gettext("\tallow [-ldug] "
236 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
237 "\t <filesystem|volume>\n"
238 "\tallow [-ld] -e <perm|@setname>[,...] "
239 "<filesystem|volume>\n"
240 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
241 "\tallow -s @setname <perm|@setname>[,...] "
242 "<filesystem|volume>\n"));
244 return (gettext("\tunallow [-rldug] "
245 "<\"everyone\"|user|group>[,...]\n"
246 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
247 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
248 "<filesystem|volume>\n"
249 "\tunallow [-r] -c [<perm|@setname>[,...]] "
250 "<filesystem|volume>\n"
251 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
252 "<filesystem|volume>\n"));
260 * Utility function to guarantee malloc() success.
263 safe_malloc(size_t size)
267 if ((data = calloc(1, size)) == NULL) {
268 (void) fprintf(stderr, "internal error: out of memory\n");
276 * Callback routine that will print out information for each of
280 usage_prop_cb(int prop, void *cb)
284 (void) fprintf(fp, "\t%-14s ", zfs_prop_to_name(prop));
286 if (prop == ZFS_PROP_CASE)
287 (void) fprintf(fp, "NO ");
288 else if (zfs_prop_readonly(prop))
289 (void) fprintf(fp, " NO ");
291 (void) fprintf(fp, " YES ");
293 if (zfs_prop_inheritable(prop))
294 (void) fprintf(fp, " YES ");
296 (void) fprintf(fp, " NO ");
298 if (zfs_prop_values(prop) == NULL)
299 (void) fprintf(fp, "-\n");
301 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
307 * Display usage message. If we're inside a command, display only the usage for
308 * that command. Otherwise, iterate over the entire command table and display
309 * a complete usage message.
312 usage(boolean_t requested)
315 boolean_t show_properties = B_FALSE;
316 boolean_t show_permissions = B_FALSE;
317 FILE *fp = requested ? stdout : stderr;
319 if (current_command == NULL) {
321 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
323 gettext("where 'command' is one of the following:\n\n"));
325 for (i = 0; i < NCOMMAND; i++) {
326 if (command_table[i].name == NULL)
327 (void) fprintf(fp, "\n");
329 (void) fprintf(fp, "%s",
330 get_usage(command_table[i].usage));
333 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
334 "pool/[dataset/]*dataset[@name]\n"));
336 (void) fprintf(fp, gettext("usage:\n"));
337 (void) fprintf(fp, "%s", get_usage(current_command->usage));
340 if (current_command != NULL &&
341 (strcmp(current_command->name, "set") == 0 ||
342 strcmp(current_command->name, "get") == 0 ||
343 strcmp(current_command->name, "inherit") == 0 ||
344 strcmp(current_command->name, "list") == 0))
345 show_properties = B_TRUE;
347 if (current_command != NULL &&
348 (strcmp(current_command->name, "allow") == 0 ||
349 strcmp(current_command->name, "unallow") == 0))
350 show_permissions = B_TRUE;
352 if (show_properties) {
355 gettext("\nThe following properties are supported:\n"));
357 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
358 "PROPERTY", "EDIT", "INHERIT", "VALUES");
360 /* Iterate over all properties */
361 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
364 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
365 "with standard units such as K, M, G, etc.\n"));
366 (void) fprintf(fp, gettext("\n\nUser-defined properties can "
367 "be specified by using a name containing a colon (:).\n"));
369 } else if (show_permissions) {
371 gettext("\nThe following permissions are supported:\n"));
373 zfs_deleg_permissions();
377 * "zfs set|get" must not be localised this is the
378 * command name and arguments.
382 gettext("\nFor the property list, run: zfs set|get\n"));
385 gettext("\nFor the delegated permission list, run:"
386 " zfs allow|unallow\n"));
390 * See comments at end of main().
392 if (getenv("ZFS_ABORT") != NULL) {
393 (void) printf("dumping core by request\n");
397 exit(requested ? 0 : 2);
401 * zfs clone [-p] <snap> <fs | vol>
403 * Given an existing dataset, create a writable copy whose initial contents
404 * are the same as the source. The newly created dataset maintains a
405 * dependency on the original; the original cannot be destroyed so long as
408 * The '-p' flag creates all the non-existing ancestors of the target first.
411 zfs_do_clone(int argc, char **argv)
414 boolean_t parents = B_FALSE;
419 while ((c = getopt(argc, argv, "p")) != -1) {
425 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
434 /* check number of arguments */
436 (void) fprintf(stderr, gettext("missing source dataset "
441 (void) fprintf(stderr, gettext("missing target dataset "
446 (void) fprintf(stderr, gettext("too many arguments\n"));
450 /* open the source dataset */
451 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
454 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
457 * Now create the ancestors of the target dataset. If the
458 * target already exists and '-p' option was used we should not
461 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
464 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
469 ret = zfs_clone(zhp, argv[1], NULL);
471 /* create the mountpoint if necessary */
475 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
477 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
478 ret = zfs_share(clone);
485 return (ret == 0 ? 0 : 1);
489 * zfs create [-p] [-o prop=value] ... fs
490 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
492 * Create a new dataset. This command can be used to create filesystems
493 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
494 * For volumes, the user must specify a size to be used.
496 * The '-s' flag applies only to volumes, and indicates that we should not try
497 * to set the reservation for this volume. By default we set a reservation
498 * equal to the size for any volume. For pools with SPA_VERSION >=
499 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
501 * The '-p' flag creates all the non-existing ancestors of the target first.
504 zfs_do_create(int argc, char **argv)
506 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
507 zfs_handle_t *zhp = NULL;
510 boolean_t noreserve = B_FALSE;
511 boolean_t bflag = B_FALSE;
512 boolean_t parents = B_FALSE;
514 nvlist_t *props = NULL;
517 char *propval = NULL;
521 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
522 (void) fprintf(stderr, gettext("internal error: "
528 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
531 type = ZFS_TYPE_VOLUME;
532 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
533 (void) fprintf(stderr, gettext("bad volume "
534 "size '%s': %s\n"), optarg,
535 libzfs_error_description(g_zfs));
539 if (nvlist_add_uint64(props,
540 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
542 (void) fprintf(stderr, gettext("internal "
543 "error: out of memory\n"));
553 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
554 (void) fprintf(stderr, gettext("bad volume "
555 "block size '%s': %s\n"), optarg,
556 libzfs_error_description(g_zfs));
560 if (nvlist_add_uint64(props,
561 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
563 (void) fprintf(stderr, gettext("internal "
564 "error: out of memory\n"));
570 if ((propval = strchr(propname, '=')) == NULL) {
571 (void) fprintf(stderr, gettext("missing "
572 "'=' for -o option\n"));
577 if (nvlist_lookup_string(props, propname,
579 (void) fprintf(stderr, gettext("property '%s' "
580 "specified multiple times\n"), propname);
583 if (nvlist_add_string(props, propname, propval) != 0) {
584 (void) fprintf(stderr, gettext("internal "
585 "error: out of memory\n"));
593 (void) fprintf(stderr, gettext("missing size "
598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
604 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
605 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
606 "used when creating a volume\n"));
613 /* check number of arguments */
615 (void) fprintf(stderr, gettext("missing %s argument\n"),
616 zfs_type_to_name(type));
620 (void) fprintf(stderr, gettext("too many arguments\n"));
624 if (type == ZFS_TYPE_VOLUME && !noreserve) {
625 zpool_handle_t *zpool_handle;
626 uint64_t spa_version;
628 zfs_prop_t resv_prop;
630 if (p = strchr(argv[0], '/'))
632 zpool_handle = zpool_open(g_zfs, argv[0]);
635 if (zpool_handle == NULL)
637 spa_version = zpool_get_prop_int(zpool_handle,
638 ZPOOL_PROP_VERSION, NULL);
639 zpool_close(zpool_handle);
640 if (spa_version >= SPA_VERSION_REFRESERVATION)
641 resv_prop = ZFS_PROP_REFRESERVATION;
643 resv_prop = ZFS_PROP_RESERVATION;
645 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
647 if (nvlist_add_uint64(props,
648 zfs_prop_to_name(resv_prop), volsize) != 0) {
649 (void) fprintf(stderr, gettext("internal "
650 "error: out of memory\n"));
657 if (parents && zfs_name_valid(argv[0], type)) {
659 * Now create the ancestors of target dataset. If the target
660 * already exists and '-p' option was used we should not
663 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
667 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
672 if (zfs_create(g_zfs, argv[0], type, props) != 0)
675 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
678 * if the user doesn't want the dataset automatically mounted,
679 * then skip the mount/share step
682 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
685 * Mount and/or share the new filesystem as appropriate. We provide a
686 * verbose error message to let the user know that their filesystem was
687 * in fact created, even if we failed to mount or share it.
690 if (canmount == ZFS_CANMOUNT_ON) {
691 if (zfs_mount(zhp, NULL, 0) != 0) {
692 (void) fprintf(stderr, gettext("filesystem "
693 "successfully created, but not mounted\n"));
695 } else if (zfs_share(zhp) != 0) {
696 (void) fprintf(stderr, gettext("filesystem "
697 "successfully created, but not shared\n"));
714 * zfs destroy [-rf] <fs, snap, vol>
716 * -r Recursively destroy all children
717 * -R Recursively destroy all dependents, including clones
718 * -f Force unmounting of any dependents
720 * Destroys the given dataset. By default, it will unmount any filesystems,
721 * and refuse to destroy a dataset that has any dependents. A dependent can
722 * either be a child, or a clone of a child.
724 typedef struct destroy_cbdata {
731 boolean_t cb_closezhp;
732 zfs_handle_t *cb_target;
737 * Check for any dependents based on the '-r' or '-R' flags.
740 destroy_check_dependent(zfs_handle_t *zhp, void *data)
742 destroy_cbdata_t *cbp = data;
743 const char *tname = zfs_get_name(cbp->cb_target);
744 const char *name = zfs_get_name(zhp);
746 if (strncmp(tname, name, strlen(tname)) == 0 &&
747 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
749 * This is a direct descendant, not a clone somewhere else in
756 (void) fprintf(stderr, gettext("cannot destroy '%s': "
757 "%s has children\n"),
758 zfs_get_name(cbp->cb_target),
759 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
760 (void) fprintf(stderr, gettext("use '-r' to destroy "
761 "the following datasets:\n"));
762 cbp->cb_first = B_FALSE;
766 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
769 * This is a clone. We only want to report this if the '-r'
770 * wasn't specified, or the target is a snapshot.
772 if (!cbp->cb_recurse &&
773 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
777 (void) fprintf(stderr, gettext("cannot destroy '%s': "
778 "%s has dependent clones\n"),
779 zfs_get_name(cbp->cb_target),
780 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
781 (void) fprintf(stderr, gettext("use '-R' to destroy "
782 "the following datasets:\n"));
783 cbp->cb_first = B_FALSE;
787 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
796 destroy_callback(zfs_handle_t *zhp, void *data)
798 destroy_cbdata_t *cbp = data;
801 * Ignore pools (which we've already flagged as an error before getting
804 if (strchr(zfs_get_name(zhp), '/') == NULL &&
805 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
811 * Bail out on the first error.
813 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
814 zfs_destroy(zhp) != 0) {
824 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
826 destroy_cbdata_t *cbp = arg;
827 char thissnap[MAXPATHLEN];
829 boolean_t closezhp = cbp->cb_closezhp;
832 (void) snprintf(thissnap, sizeof (thissnap),
833 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
835 libzfs_print_on_error(g_zfs, B_FALSE);
836 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
837 libzfs_print_on_error(g_zfs, B_TRUE);
840 * Destroy any clones of this snapshot
842 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
852 cbp->cb_closezhp = B_TRUE;
853 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
860 zfs_do_destroy(int argc, char **argv)
862 destroy_cbdata_t cb = { 0 };
868 while ((c = getopt(argc, argv, "frR")) != -1) {
882 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
891 /* check number of arguments */
893 (void) fprintf(stderr, gettext("missing path argument\n"));
897 (void) fprintf(stderr, gettext("too many arguments\n"));
902 * If we are doing recursive destroy of a snapshot, then the
903 * named snapshot may not exist. Go straight to libzfs.
905 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
909 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
914 if (cb.cb_doclones) {
916 if (destroy_snap_clones(zhp, &cb) != 0) {
922 ret = zfs_destroy_snaps(zhp, cp);
925 (void) fprintf(stderr,
926 gettext("no snapshots destroyed\n"));
932 /* Open the given dataset */
933 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
939 * Perform an explicit check for pools before going any further.
941 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
942 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
943 (void) fprintf(stderr, gettext("cannot destroy '%s': "
944 "operation does not apply to pools\n"),
946 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
947 "%s' to destroy all datasets in the pool\n"),
949 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
950 "to destroy the pool itself\n"), zfs_get_name(zhp));
956 * Check for any dependents and/or clones.
958 cb.cb_first = B_TRUE;
959 if (!cb.cb_doclones &&
960 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
967 zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
973 * Do the real thing. The callback will close the handle regardless of
974 * whether it succeeds or not.
977 if (destroy_callback(zhp, &cb) != 0)
985 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
986 * < all | property[,property]... > < fs | snap | vol > ...
988 * -r recurse over any child datasets
989 * -H scripted mode. Headers are stripped, and fields are separated
990 * by tabs instead of spaces.
991 * -o Set of fields to display. One of "name,property,value,source".
992 * Default is all four.
993 * -s Set of sources to allow. One of
994 * "local,default,inherited,temporary,none". Default is all
996 * -p Display values in parsable (literal) format.
998 * Prints properties for the given datasets. The user can control which
999 * columns to display as well as which property types to allow.
1003 * Invoked to display the properties for a single dataset.
1006 get_callback(zfs_handle_t *zhp, void *data)
1008 char buf[ZFS_MAXPROPLEN];
1009 zprop_source_t sourcetype;
1010 char source[ZFS_MAXNAMELEN];
1011 zprop_get_cbdata_t *cbp = data;
1012 nvlist_t *userprop = zfs_get_user_props(zhp);
1013 zprop_list_t *pl = cbp->cb_proplist;
1018 for (; pl != NULL; pl = pl->pl_next) {
1020 * Skip the special fake placeholder. This will also skip over
1021 * the name property when 'all' is specified.
1023 if (pl->pl_prop == ZFS_PROP_NAME &&
1024 pl == cbp->cb_proplist)
1027 if (pl->pl_prop != ZPROP_INVAL) {
1028 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1029 sizeof (buf), &sourcetype, source,
1031 cbp->cb_literal) != 0) {
1034 if (!zfs_prop_valid_for_type(pl->pl_prop,
1035 ZFS_TYPE_DATASET)) {
1036 (void) fprintf(stderr,
1037 gettext("No such property '%s'\n"),
1038 zfs_prop_to_name(pl->pl_prop));
1041 sourcetype = ZPROP_SRC_NONE;
1042 (void) strlcpy(buf, "-", sizeof (buf));
1045 zprop_print_one_property(zfs_get_name(zhp), cbp,
1046 zfs_prop_to_name(pl->pl_prop),
1047 buf, sourcetype, source);
1049 if (nvlist_lookup_nvlist(userprop,
1050 pl->pl_user_prop, &propval) != 0) {
1053 sourcetype = ZPROP_SRC_NONE;
1056 verify(nvlist_lookup_string(propval,
1057 ZPROP_VALUE, &strval) == 0);
1058 verify(nvlist_lookup_string(propval,
1059 ZPROP_SOURCE, &sourceval) == 0);
1061 if (strcmp(sourceval,
1062 zfs_get_name(zhp)) == 0) {
1063 sourcetype = ZPROP_SRC_LOCAL;
1065 sourcetype = ZPROP_SRC_INHERITED;
1066 (void) strlcpy(source,
1067 sourceval, sizeof (source));
1071 zprop_print_one_property(zfs_get_name(zhp), cbp,
1072 pl->pl_user_prop, strval, sourcetype,
1081 zfs_do_get(int argc, char **argv)
1083 zprop_get_cbdata_t cb = { 0 };
1084 boolean_t recurse = B_FALSE;
1086 char *value, *fields;
1088 zprop_list_t fake_name = { 0 };
1091 * Set up default columns and sources.
1093 cb.cb_sources = ZPROP_SRC_ALL;
1094 cb.cb_columns[0] = GET_COL_NAME;
1095 cb.cb_columns[1] = GET_COL_PROPERTY;
1096 cb.cb_columns[2] = GET_COL_VALUE;
1097 cb.cb_columns[3] = GET_COL_SOURCE;
1098 cb.cb_type = ZFS_TYPE_DATASET;
1101 while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
1104 cb.cb_literal = B_TRUE;
1110 cb.cb_scripted = B_TRUE;
1113 (void) fprintf(stderr, gettext("missing argument for "
1114 "'%c' option\n"), optopt);
1119 * Process the set of columns to display. We zero out
1120 * the structure to give us a blank slate.
1122 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1124 while (*optarg != '\0') {
1125 static char *col_subopts[] =
1126 { "name", "property", "value", "source",
1130 (void) fprintf(stderr, gettext("too "
1131 "many fields given to -o "
1136 switch (getsubopt(&optarg, col_subopts,
1139 cb.cb_columns[i++] = GET_COL_NAME;
1142 cb.cb_columns[i++] = GET_COL_PROPERTY;
1145 cb.cb_columns[i++] = GET_COL_VALUE;
1148 cb.cb_columns[i++] = GET_COL_SOURCE;
1151 (void) fprintf(stderr,
1152 gettext("invalid column name "
1161 while (*optarg != '\0') {
1162 static char *source_subopts[] = {
1163 "local", "default", "inherited",
1164 "temporary", "none", NULL };
1166 switch (getsubopt(&optarg, source_subopts,
1169 cb.cb_sources |= ZPROP_SRC_LOCAL;
1172 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1175 cb.cb_sources |= ZPROP_SRC_INHERITED;
1178 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1181 cb.cb_sources |= ZPROP_SRC_NONE;
1184 (void) fprintf(stderr,
1185 gettext("invalid source "
1193 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1203 (void) fprintf(stderr, gettext("missing property "
1210 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1218 * As part of zfs_expand_proplist(), we keep track of the maximum column
1219 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1220 * need to know the maximum name length. However, the user likely did
1221 * not specify 'name' as one of the properties to fetch, so we need to
1222 * make sure we always include at least this property for
1223 * print_get_headers() to work properly.
1225 if (cb.cb_proplist != NULL) {
1226 fake_name.pl_prop = ZFS_PROP_NAME;
1227 fake_name.pl_width = strlen(gettext("NAME"));
1228 fake_name.pl_next = cb.cb_proplist;
1229 cb.cb_proplist = &fake_name;
1232 cb.cb_first = B_TRUE;
1234 /* run for each object */
1235 ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_DATASET, NULL,
1236 &cb.cb_proplist, get_callback, &cb, B_FALSE);
1238 if (cb.cb_proplist == &fake_name)
1239 zprop_free_list(fake_name.pl_next);
1241 zprop_free_list(cb.cb_proplist);
1247 * inherit [-r] <property> <fs|vol> ...
1249 * -r Recurse over all children
1251 * For each dataset specified on the command line, inherit the given property
1252 * from its parent. Inheriting a property at the pool level will cause it to
1253 * use the default value. The '-r' flag will recurse over all children, and is
1254 * useful for setting a property on a hierarchy-wide basis, regardless of any
1255 * local modifications for each dataset.
1259 inherit_callback(zfs_handle_t *zhp, void *data)
1261 char *propname = data;
1264 ret = zfs_prop_inherit(zhp, propname);
1269 zfs_do_inherit(int argc, char **argv)
1271 boolean_t recurse = B_FALSE;
1278 while ((c = getopt(argc, argv, "r")) != -1) {
1285 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1294 /* check number of arguments */
1296 (void) fprintf(stderr, gettext("missing property argument\n"));
1300 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1308 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1309 if (zfs_prop_readonly(prop)) {
1310 (void) fprintf(stderr, gettext(
1311 "%s property is read-only\n"),
1315 if (!zfs_prop_inheritable(prop)) {
1316 (void) fprintf(stderr, gettext("'%s' property cannot "
1317 "be inherited\n"), propname);
1318 if (prop == ZFS_PROP_QUOTA ||
1319 prop == ZFS_PROP_RESERVATION ||
1320 prop == ZFS_PROP_REFQUOTA ||
1321 prop == ZFS_PROP_REFRESERVATION)
1322 (void) fprintf(stderr, gettext("use 'zfs set "
1323 "%s=none' to clear\n"), propname);
1326 } else if (!zfs_prop_user(propname)) {
1327 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1332 ret = zfs_for_each(argc, argv, recurse,
1333 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL,
1334 inherit_callback, propname, B_FALSE);
1339 typedef struct upgrade_cbdata {
1340 uint64_t cb_numupgraded;
1341 uint64_t cb_numsamegraded;
1342 uint64_t cb_numfailed;
1343 uint64_t cb_version;
1345 boolean_t cb_foundone;
1346 char cb_lastfs[ZFS_MAXNAMELEN];
1350 same_pool(zfs_handle_t *zhp, const char *name)
1352 int len1 = strcspn(name, "/@");
1353 const char *zhname = zfs_get_name(zhp);
1354 int len2 = strcspn(zhname, "/@");
1358 return (strncmp(name, zhname, len1) == 0);
1362 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1364 upgrade_cbdata_t *cb = data;
1365 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1367 /* list if it's old/new */
1368 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1369 (cb->cb_newer && version > ZPL_VERSION)) {
1372 str = gettext("The following filesystems are "
1373 "formatted using a newer software version and\n"
1374 "cannot be accessed on the current system.\n\n");
1376 str = gettext("The following filesystems are "
1377 "out of date, and can be upgraded. After being\n"
1378 "upgraded, these filesystems (and any 'zfs send' "
1379 "streams generated from\n"
1380 "subsequent snapshots) will no longer be "
1381 "accessible by older software versions.\n\n");
1384 if (!cb->cb_foundone) {
1386 (void) printf(gettext("VER FILESYSTEM\n"));
1387 (void) printf(gettext("--- ------------\n"));
1388 cb->cb_foundone = B_TRUE;
1391 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1398 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1400 upgrade_cbdata_t *cb = data;
1401 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1403 if (cb->cb_version >= ZPL_VERSION_FUID) {
1406 if (zfs_spa_version(zhp, &spa_version) < 0)
1409 if (spa_version < SPA_VERSION_FUID) {
1411 (void) printf(gettext("%s: can not be upgraded; "
1412 "the pool version needs to first be upgraded\nto "
1414 zfs_get_name(zhp), SPA_VERSION_FUID);
1421 if (version < cb->cb_version) {
1423 (void) snprintf(verstr, sizeof (verstr),
1424 "%llu", cb->cb_version);
1425 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1427 * If they did "zfs upgrade -a", then we could
1428 * be doing ioctls to different pools. We need
1429 * to log this history once to each pool.
1431 verify(zpool_stage_history(g_zfs, history_str) == 0);
1433 if (zfs_prop_set(zhp, "version", verstr) == 0)
1434 cb->cb_numupgraded++;
1437 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1438 } else if (version > cb->cb_version) {
1439 /* can't downgrade */
1440 (void) printf(gettext("%s: can not be downgraded; "
1441 "it is already at version %u\n"),
1442 zfs_get_name(zhp), version);
1445 cb->cb_numsamegraded++;
1453 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1456 zfs_do_upgrade(int argc, char **argv)
1458 boolean_t recurse = B_FALSE;
1459 boolean_t all = B_FALSE;
1460 boolean_t showversions = B_FALSE;
1462 upgrade_cbdata_t cb = { 0 };
1466 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1472 showversions = B_TRUE;
1475 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1476 optarg, &cb.cb_version) != 0) {
1477 (void) fprintf(stderr,
1478 gettext("invalid version %s\n"), optarg);
1487 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1496 if ((!all && !argc) && (recurse | cb.cb_version))
1498 if (showversions && (recurse || all || cb.cb_version || argc))
1500 if ((all || argc) && (showversions))
1506 /* Show info on available versions. */
1507 (void) printf(gettext("The following filesystem versions are "
1509 (void) printf(gettext("VER DESCRIPTION\n"));
1510 (void) printf("--- -----------------------------------------"
1511 "---------------\n");
1512 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1513 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1514 (void) printf(gettext(" 3 Case insensitive and File system "
1515 "unique identifer (FUID)\n"));
1516 (void) printf(gettext("\nFor more information on a particular "
1517 "version, including supported releases, see:\n\n"));
1518 (void) printf("http://www.opensolaris.org/os/community/zfs/"
1519 "version/zpl/N\n\n");
1520 (void) printf(gettext("Where 'N' is the version number.\n"));
1522 } else if (argc || all) {
1523 /* Upgrade filesystems */
1524 if (cb.cb_version == 0)
1525 cb.cb_version = ZPL_VERSION;
1526 ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_FILESYSTEM,
1527 NULL, NULL, upgrade_set_callback, &cb, B_TRUE);
1528 (void) printf(gettext("%llu filesystems upgraded\n"),
1530 if (cb.cb_numsamegraded) {
1531 (void) printf(gettext("%llu filesystems already at "
1533 cb.cb_numsamegraded);
1535 if (cb.cb_numfailed != 0)
1538 /* List old-version filesytems */
1540 (void) printf(gettext("This system is currently running "
1541 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1543 ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
1544 NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
1546 found = cb.cb_foundone;
1547 cb.cb_foundone = B_FALSE;
1548 cb.cb_newer = B_TRUE;
1550 ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
1551 NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
1553 if (!cb.cb_foundone && !found) {
1554 (void) printf(gettext("All filesystems are "
1555 "formatted with the current version.\n"));
1563 * list [-rH] [-o property[,property]...] [-t type[,type]...]
1564 * [-s property [-s property]...] [-S property [-S property]...]
1567 * -r Recurse over all children
1568 * -H Scripted mode; elide headers and separate columns by tabs
1569 * -o Control which fields to display.
1570 * -t Control which object types to display.
1571 * -s Specify sort columns, descending order.
1572 * -S Specify sort columns, ascending order.
1574 * When given no arguments, lists all filesystems in the system.
1575 * Otherwise, list the specified datasets, optionally recursing down them if
1576 * '-r' is specified.
1578 typedef struct list_cbdata {
1580 boolean_t cb_scripted;
1581 zprop_list_t *cb_proplist;
1585 * Given a list of columns to display, output appropriate headers for each one.
1588 print_header(zprop_list_t *pl)
1590 char headerbuf[ZFS_MAXPROPLEN];
1593 boolean_t first = B_TRUE;
1594 boolean_t right_justify;
1596 for (; pl != NULL; pl = pl->pl_next) {
1603 right_justify = B_FALSE;
1604 if (pl->pl_prop != ZPROP_INVAL) {
1605 header = zfs_prop_column_name(pl->pl_prop);
1606 right_justify = zfs_prop_align_right(pl->pl_prop);
1608 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1609 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1610 headerbuf[i] = '\0';
1614 if (pl->pl_next == NULL && !right_justify)
1615 (void) printf("%s", header);
1616 else if (right_justify)
1617 (void) printf("%*s", pl->pl_width, header);
1619 (void) printf("%-*s", pl->pl_width, header);
1622 (void) printf("\n");
1626 * Given a dataset and a list of fields, print out all the properties according
1627 * to the described layout.
1630 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1632 boolean_t first = B_TRUE;
1633 char property[ZFS_MAXPROPLEN];
1634 nvlist_t *userprops = zfs_get_user_props(zhp);
1637 boolean_t right_justify;
1640 for (; pl != NULL; pl = pl->pl_next) {
1643 (void) printf("\t");
1650 right_justify = B_FALSE;
1651 if (pl->pl_prop != ZPROP_INVAL) {
1652 if (zfs_prop_get(zhp, pl->pl_prop, property,
1653 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1658 right_justify = zfs_prop_align_right(pl->pl_prop);
1660 if (nvlist_lookup_nvlist(userprops,
1661 pl->pl_user_prop, &propval) != 0)
1664 verify(nvlist_lookup_string(propval,
1665 ZPROP_VALUE, &propstr) == 0);
1668 width = pl->pl_width;
1671 * If this is being called in scripted mode, or if this is the
1672 * last column and it is left-justified, don't include a width
1675 if (scripted || (pl->pl_next == NULL && !right_justify))
1676 (void) printf("%s", propstr);
1677 else if (right_justify)
1678 (void) printf("%*s", width, propstr);
1680 (void) printf("%-*s", width, propstr);
1683 (void) printf("\n");
1687 * Generic callback function to list a dataset or snapshot.
1690 list_callback(zfs_handle_t *zhp, void *data)
1692 list_cbdata_t *cbp = data;
1694 if (cbp->cb_first) {
1695 if (!cbp->cb_scripted)
1696 print_header(cbp->cb_proplist);
1697 cbp->cb_first = B_FALSE;
1700 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1706 zfs_do_list(int argc, char **argv)
1709 boolean_t recurse = B_FALSE;
1710 boolean_t scripted = B_FALSE;
1711 static char default_fields[] =
1712 "name,used,available,referenced,mountpoint";
1713 int types = ZFS_TYPE_DATASET;
1714 char *fields = NULL;
1715 char *basic_fields = default_fields;
1716 list_cbdata_t cb = { 0 };
1719 char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL };
1720 zfs_sort_column_t *sortcol = NULL;
1723 while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
1735 if (zfs_add_sort_column(&sortcol, optarg,
1737 (void) fprintf(stderr,
1738 gettext("invalid property '%s'\n"), optarg);
1743 if (zfs_add_sort_column(&sortcol, optarg,
1745 (void) fprintf(stderr,
1746 gettext("invalid property '%s'\n"), optarg);
1752 while (*optarg != '\0') {
1753 switch (getsubopt(&optarg, type_subopts,
1756 types |= ZFS_TYPE_FILESYSTEM;
1759 types |= ZFS_TYPE_VOLUME;
1762 types |= ZFS_TYPE_SNAPSHOT;
1765 (void) fprintf(stderr,
1766 gettext("invalid type '%s'\n"),
1773 (void) fprintf(stderr, gettext("missing argument for "
1774 "'%c' option\n"), optopt);
1778 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1788 fields = basic_fields;
1791 * If the user specifies '-o all', the zprop_get_list() doesn't
1792 * normally include the name of the dataset. For 'zfs list', we always
1793 * want this property to be first.
1795 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1799 cb.cb_scripted = scripted;
1800 cb.cb_first = B_TRUE;
1802 ret = zfs_for_each(argc, argv, recurse, types, sortcol, &cb.cb_proplist,
1803 list_callback, &cb, B_TRUE);
1805 zprop_free_list(cb.cb_proplist);
1806 zfs_free_sort_columns(sortcol);
1808 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
1809 (void) printf(gettext("no datasets available\n"));
1815 * zfs rename <fs | snap | vol> <fs | snap | vol>
1816 * zfs rename -p <fs | vol> <fs | vol>
1817 * zfs rename -r <snap> <snap>
1819 * Renames the given dataset to another of the same type.
1821 * The '-p' flag creates all the non-existing ancestors of the target first.
1825 zfs_do_rename(int argc, char **argv)
1830 boolean_t recurse = B_FALSE;
1831 boolean_t parents = B_FALSE;
1834 while ((c = getopt(argc, argv, "pr")) != -1) {
1844 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1853 /* check number of arguments */
1855 (void) fprintf(stderr, gettext("missing source dataset "
1860 (void) fprintf(stderr, gettext("missing target dataset "
1865 (void) fprintf(stderr, gettext("too many arguments\n"));
1869 if (recurse && parents) {
1870 (void) fprintf(stderr, gettext("-p and -r options are mutually "
1875 if (recurse && strchr(argv[0], '@') == 0) {
1876 (void) fprintf(stderr, gettext("source dataset for recursive "
1877 "rename must be a snapshot\n"));
1881 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
1882 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
1885 /* If we were asked and the name looks good, try to create ancestors. */
1886 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
1887 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
1892 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
1901 * Promotes the given clone fs to be the parent
1905 zfs_do_promote(int argc, char **argv)
1911 if (argc > 1 && argv[1][0] == '-') {
1912 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1917 /* check number of arguments */
1919 (void) fprintf(stderr, gettext("missing clone filesystem"
1924 (void) fprintf(stderr, gettext("too many arguments\n"));
1928 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1932 ret = (zfs_promote(zhp) != 0);
1940 * zfs rollback [-rRf] <snapshot>
1942 * -r Delete any intervening snapshots before doing rollback
1943 * -R Delete any snapshots and their clones
1944 * -f ignored for backwards compatability
1946 * Given a filesystem, rollback to a specific snapshot, discarding any changes
1947 * since then and making it the active dataset. If more recent snapshots exist,
1948 * the command will complain unless the '-r' flag is given.
1950 typedef struct rollback_cbdata {
1956 boolean_t cb_recurse;
1957 boolean_t cb_dependent;
1958 } rollback_cbdata_t;
1961 * Report any snapshots more recent than the one specified. Used when '-r' is
1962 * not specified. We reuse this same callback for the snapshot dependents - if
1963 * 'cb_dependent' is set, then this is a dependent and we should report it
1964 * without checking the transaction group.
1967 rollback_check(zfs_handle_t *zhp, void *data)
1969 rollback_cbdata_t *cbp = data;
1971 if (cbp->cb_doclones) {
1976 if (!cbp->cb_dependent) {
1977 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
1978 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1979 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
1982 if (cbp->cb_first && !cbp->cb_recurse) {
1983 (void) fprintf(stderr, gettext("cannot "
1984 "rollback to '%s': more recent snapshots "
1987 (void) fprintf(stderr, gettext("use '-r' to "
1988 "force deletion of the following "
1994 if (cbp->cb_recurse) {
1995 cbp->cb_dependent = B_TRUE;
1996 if (zfs_iter_dependents(zhp, B_TRUE,
1997 rollback_check, cbp) != 0) {
2001 cbp->cb_dependent = B_FALSE;
2003 (void) fprintf(stderr, "%s\n",
2008 if (cbp->cb_first && cbp->cb_recurse) {
2009 (void) fprintf(stderr, gettext("cannot rollback to "
2010 "'%s': clones of previous snapshots exist\n"),
2012 (void) fprintf(stderr, gettext("use '-R' to "
2013 "force deletion of the following clones and "
2019 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2027 zfs_do_rollback(int argc, char **argv)
2031 boolean_t force = B_FALSE;
2032 rollback_cbdata_t cb = { 0 };
2033 zfs_handle_t *zhp, *snap;
2034 char parentname[ZFS_MAXNAMELEN];
2038 while ((c = getopt(argc, argv, "rRf")) != -1) {
2051 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2060 /* check number of arguments */
2062 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2066 (void) fprintf(stderr, gettext("too many arguments\n"));
2070 /* open the snapshot */
2071 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2074 /* open the parent dataset */
2075 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2076 verify((delim = strrchr(parentname, '@')) != NULL);
2078 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2084 * Check for more recent snapshots and/or clones based on the presence
2087 cb.cb_target = argv[0];
2088 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2089 cb.cb_first = B_TRUE;
2091 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2094 if ((ret = cb.cb_error) != 0)
2098 * Rollback parent to the given snapshot.
2100 ret = zfs_rollback(zhp, snap, force);
2113 * zfs set property=value { fs | snap | vol } ...
2115 * Sets the given property for all datasets specified on the command line.
2117 typedef struct set_cbdata {
2123 set_callback(zfs_handle_t *zhp, void *data)
2125 set_cbdata_t *cbp = data;
2127 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2128 switch (libzfs_errno(g_zfs)) {
2129 case EZFS_MOUNTFAILED:
2130 (void) fprintf(stderr, gettext("property may be set "
2131 "but unable to remount filesystem\n"));
2133 case EZFS_SHARENFSFAILED:
2134 (void) fprintf(stderr, gettext("property may be set "
2135 "but unable to reshare filesystem\n"));
2144 zfs_do_set(int argc, char **argv)
2149 /* check for options */
2150 if (argc > 1 && argv[1][0] == '-') {
2151 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2156 /* check number of arguments */
2158 (void) fprintf(stderr, gettext("missing property=value "
2163 (void) fprintf(stderr, gettext("missing dataset name\n"));
2167 /* validate property=value argument */
2168 cb.cb_propname = argv[1];
2169 if ((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) {
2170 (void) fprintf(stderr, gettext("missing value in "
2171 "property=value argument\n"));
2175 *cb.cb_value = '\0';
2178 if (*cb.cb_propname == '\0') {
2179 (void) fprintf(stderr,
2180 gettext("missing property in property=value argument\n"));
2185 ret = zfs_for_each(argc - 2, argv + 2, B_FALSE,
2186 ZFS_TYPE_DATASET, NULL, NULL, set_callback, &cb, B_FALSE);
2192 * zfs snapshot [-r] <fs@snap>
2194 * Creates a snapshot with the given name. While functionally equivalent to
2195 * 'zfs create', it is a separate command to differentiate intent.
2198 zfs_do_snapshot(int argc, char **argv)
2200 boolean_t recursive = B_FALSE;
2205 while ((c = getopt(argc, argv, ":r")) != -1) {
2211 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2220 /* check number of arguments */
2222 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2226 (void) fprintf(stderr, gettext("too many arguments\n"));
2230 ret = zfs_snapshot(g_zfs, argv[0], recursive);
2231 if (ret && recursive)
2232 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2237 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2238 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2240 * Send a backup stream to stdout.
2243 zfs_do_send(int argc, char **argv)
2245 char *fromname = NULL;
2246 char *toname = NULL;
2249 boolean_t doall = B_FALSE;
2250 boolean_t replicate = B_FALSE;
2251 boolean_t fromorigin = B_FALSE;
2252 boolean_t verbose = B_FALSE;
2256 while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
2276 (void) fprintf(stderr, gettext("missing argument for "
2277 "'%c' option\n"), optopt);
2281 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2290 /* check number of arguments */
2292 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2296 (void) fprintf(stderr, gettext("too many arguments\n"));
2300 if (isatty(STDOUT_FILENO)) {
2301 (void) fprintf(stderr,
2302 gettext("Error: Stream can not be written to a terminal.\n"
2303 "You must redirect standard output.\n"));
2307 cp = strchr(argv[0], '@');
2309 (void) fprintf(stderr,
2310 gettext("argument must be a snapshot\n"));
2315 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2320 * If they specified the full path to the snapshot, chop off
2321 * everything except the short name of the snapshot, but special
2322 * case if they specify the origin.
2324 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2325 char origin[ZFS_MAXNAMELEN];
2328 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2329 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2331 if (strcmp(origin, fromname) == 0) {
2333 fromorigin = B_TRUE;
2336 if (cp != fromname && strcmp(argv[0], fromname)) {
2337 (void) fprintf(stderr,
2338 gettext("incremental source must be "
2339 "in same filesystem\n"));
2343 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2344 (void) fprintf(stderr,
2345 gettext("invalid incremental source\n"));
2351 if (replicate && fromname == NULL)
2354 err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
2355 verbose, STDOUT_FILENO);
2362 * zfs receive [-dnvF] <fs@snap>
2364 * Restore a backup stream from stdin.
2367 zfs_do_receive(int argc, char **argv)
2372 bzero(&flags, sizeof (recvflags_t));
2374 while ((c = getopt(argc, argv, ":dnvF")) != -1) {
2377 flags.isprefix = B_TRUE;
2380 flags.dryrun = B_TRUE;
2383 flags.verbose = B_TRUE;
2386 flags.force = B_TRUE;
2389 (void) fprintf(stderr, gettext("missing argument for "
2390 "'%c' option\n"), optopt);
2394 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2403 /* check number of arguments */
2405 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2409 (void) fprintf(stderr, gettext("too many arguments\n"));
2413 if (isatty(STDIN_FILENO)) {
2414 (void) fprintf(stderr,
2415 gettext("Error: Backup stream can not be read "
2416 "from a terminal.\n"
2417 "You must redirect standard input.\n"));
2421 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2426 typedef struct allow_cb {
2428 size_t a_treeoffset;
2432 zfs_print_perms(avl_tree_t *tree)
2434 zfs_perm_node_t *permnode;
2436 permnode = avl_first(tree);
2437 while (permnode != NULL) {
2438 (void) printf("%s", permnode->z_pname);
2439 permnode = AVL_NEXT(tree, permnode);
2443 (void) printf("\n");
2448 * Iterate over user/groups/everyone/... and the call perm_iter
2449 * function to print actual permission when tree has >0 nodes.
2452 zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb)
2454 zfs_allow_node_t *item;
2457 item = avl_first(tree);
2459 ptree = (void *)((char *)item + cb->a_treeoffset);
2460 if (avl_numnodes(ptree)) {
2461 if (cb->a_permcnt++ == 0)
2462 (void) printf("%s\n", banner);
2463 (void) printf("\t%s", item->z_key);
2465 * Avoid an extra space being printed
2466 * for "everyone" which is keyed with a null
2469 if (item->z_key[0] != '\0')
2471 zfs_print_perms(ptree);
2473 item = AVL_NEXT(tree, item);
2477 #define LINES "-------------------------------------------------------------\n"
2479 zfs_print_allows(char *ds)
2481 zfs_allow_t *curperms, *perms;
2483 allow_cb_t allowcb = { 0 };
2484 char banner[MAXPATHLEN];
2489 if (strrchr(ds, '@')) {
2490 (void) fprintf(stderr, gettext("Snapshots don't have 'allow'"
2494 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2497 if (zfs_perm_get(zhp, &perms)) {
2498 (void) fprintf(stderr,
2499 gettext("Failed to retrieve 'allows' on %s\n"), ds);
2507 (void) printf("%s", LINES);
2508 for (curperms = perms; curperms; curperms = curperms->z_next) {
2510 (void) snprintf(banner, sizeof (banner),
2511 "Permission sets on (%s)", curperms->z_setpoint);
2512 allowcb.a_treeoffset =
2513 offsetof(zfs_allow_node_t, z_localdescend);
2514 allowcb.a_permcnt = 0;
2515 zfs_iter_perms(&curperms->z_sets, banner, &allowcb);
2517 (void) snprintf(banner, sizeof (banner),
2518 "Create time permissions on (%s)", curperms->z_setpoint);
2519 allowcb.a_treeoffset =
2520 offsetof(zfs_allow_node_t, z_localdescend);
2521 allowcb.a_permcnt = 0;
2522 zfs_iter_perms(&curperms->z_crperms, banner, &allowcb);
2525 (void) snprintf(banner, sizeof (banner),
2526 "Local permissions on (%s)", curperms->z_setpoint);
2527 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local);
2528 allowcb.a_permcnt = 0;
2529 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2530 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2531 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2533 (void) snprintf(banner, sizeof (banner),
2534 "Descendent permissions on (%s)", curperms->z_setpoint);
2535 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend);
2536 allowcb.a_permcnt = 0;
2537 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2538 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2539 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2541 (void) snprintf(banner, sizeof (banner),
2542 "Local+Descendent permissions on (%s)",
2543 curperms->z_setpoint);
2544 allowcb.a_treeoffset =
2545 offsetof(zfs_allow_node_t, z_localdescend);
2546 allowcb.a_permcnt = 0;
2547 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2548 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2549 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2551 (void) printf("%s", LINES);
2553 zfs_free_allows(perms);
2557 #define ALLOWOPTIONS "ldcsu:g:e"
2558 #define UNALLOWOPTIONS "ldcsu:g:er"
2561 * Validate options, and build necessary datastructure to display/remove/add
2563 * Returns 0 - If permissions should be added/removed
2564 * Returns 1 - If permissions should be displayed.
2565 * Returns -1 - on failure
2568 parse_allow_args(int *argc, char **argv[], boolean_t unallow,
2569 char **ds, int *recurse, nvlist_t **zperms)
2572 char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS;
2573 zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE;
2574 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
2579 while ((c = getopt(*argc, *argv, options)) != -1) {
2582 if (who_type == ZFS_DELEG_CREATE ||
2583 who_type == ZFS_DELEG_NAMED_SET)
2586 deleg_type |= ZFS_DELEG_PERM_LOCAL;
2589 if (who_type == ZFS_DELEG_CREATE ||
2590 who_type == ZFS_DELEG_NAMED_SET)
2593 deleg_type |= ZFS_DELEG_PERM_DESCENDENT;
2599 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2603 who_type = ZFS_DELEG_CREATE;
2606 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2610 who_type = ZFS_DELEG_NAMED_SET;
2613 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2615 who_type = ZFS_DELEG_USER;
2619 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2621 who_type = ZFS_DELEG_GROUP;
2625 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2627 who_type = ZFS_DELEG_EVERYONE;
2635 if (deleg_type == 0)
2636 deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT;
2641 if (unallow == B_FALSE && *argc == 1) {
2643 * Only print permissions if no options were processed
2652 * initialize variables for zfs_build_perms based on number
2654 * 3 arguments ==> zfs [un]allow joe perm,perm,perm <dataset> or
2655 * zfs [un]allow -s @set1 perm,perm <dataset>
2656 * 2 arguments ==> zfs [un]allow -c perm,perm <dataset> or
2657 * zfs [un]allow -u|-g <name> perm <dataset> or
2658 * zfs [un]allow -e perm,perm <dataset>
2659 * zfs unallow joe <dataset>
2660 * zfs unallow -s @set1 <dataset>
2661 * 1 argument ==> zfs [un]allow -e <dataset> or
2662 * zfs [un]allow -c <dataset>
2672 * advance argc/argv for do_allow cases.
2673 * for do_allow case make sure who have a know who type
2674 * and its not a permission set.
2676 if (unallow == B_TRUE) {
2679 } else if (who_type != ZFS_DELEG_WHO_UNKNOWN &&
2680 who_type != ZFS_DELEG_NAMED_SET)
2685 if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE ||
2686 who_type == ZFS_DELEG_CREATE || who != NULL)) {
2690 if (unallow == B_FALSE &&
2691 (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2692 who_type == ZFS_DELEG_NAMED_SET))
2694 else if (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2695 who_type == ZFS_DELEG_NAMED_SET)
2697 else if (who_type != ZFS_DELEG_NAMED_SET)
2701 if (unallow == B_TRUE) {
2708 if (unallow == B_FALSE)
2710 if (who == NULL && who_type != ZFS_DELEG_CREATE &&
2711 who_type != ZFS_DELEG_EVERYONE)
2720 if (strrchr(*ds, '@')) {
2721 (void) fprintf(stderr,
2722 gettext("Can't set or remove 'allow' permissions "
2723 "on snapshots.\n"));
2727 if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_DATASET)) == NULL)
2730 if ((zfs_build_perms(zhp, who, perms,
2731 who_type, deleg_type, zperms)) != 0) {
2740 zfs_do_allow(int argc, char **argv)
2743 nvlist_t *zperms = NULL;
2748 if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds,
2749 &unused, &zperms)) == -1)
2753 return (zfs_print_allows(argv[0]));
2755 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2758 if (zfs_perm_set(zhp, zperms)) {
2760 nvlist_free(zperms);
2763 nvlist_free(zperms);
2770 unallow_callback(zfs_handle_t *zhp, void *data)
2772 nvlist_t *nvp = (nvlist_t *)data;
2775 error = zfs_perm_remove(zhp, nvp);
2777 (void) fprintf(stderr, gettext("Failed to remove permissions "
2778 "on %s\n"), zfs_get_name(zhp));
2784 zfs_do_unallow(int argc, char **argv)
2786 int recurse = B_FALSE;
2789 nvlist_t *zperms = NULL;
2791 if (parse_allow_args(&argc, &argv, B_TRUE,
2792 &ds, &recurse, &zperms) == -1)
2795 error = zfs_for_each(argc, argv, recurse,
2796 ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
2797 NULL, unallow_callback, (void *)zperms, B_FALSE);
2800 nvlist_free(zperms);
2805 typedef struct get_all_cbdata {
2806 zfs_handle_t **cb_handles;
2810 boolean_t cb_verbose;
2813 #define CHECK_SPINNER 30
2814 #define SPINNER_TIME 3 /* seconds */
2815 #define MOUNT_TIME 5 /* seconds */
2818 get_one_dataset(zfs_handle_t *zhp, void *data)
2820 static char spin[] = { '-', '\\', '|', '/' };
2821 static int spinval = 0;
2822 static int spincheck = 0;
2823 static time_t last_spin_time = (time_t)0;
2824 get_all_cbdata_t *cbp = data;
2825 zfs_type_t type = zfs_get_type(zhp);
2827 if (cbp->cb_verbose) {
2828 if (--spincheck < 0) {
2829 time_t now = time(NULL);
2830 if (last_spin_time + SPINNER_TIME < now) {
2831 (void) printf("\b%c", spin[spinval++ % 4]);
2832 (void) fflush(stdout);
2833 last_spin_time = now;
2835 spincheck = CHECK_SPINNER;
2840 * Interate over any nested datasets.
2842 if (type == ZFS_TYPE_FILESYSTEM &&
2843 zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2849 * Skip any datasets whose type does not match.
2851 if ((type & cbp->cb_types) == 0) {
2856 if (cbp->cb_alloc == cbp->cb_used) {
2857 zfs_handle_t **handles;
2859 if (cbp->cb_alloc == 0)
2864 handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2866 if (cbp->cb_handles) {
2867 bcopy(cbp->cb_handles, handles,
2868 cbp->cb_used * sizeof (void *));
2869 free(cbp->cb_handles);
2872 cbp->cb_handles = handles;
2875 cbp->cb_handles[cbp->cb_used++] = zhp;
2881 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
2884 get_all_cbdata_t cb = { 0 };
2885 cb.cb_types = types;
2886 cb.cb_verbose = verbose;
2889 (void) printf("%s: *", gettext("Reading ZFS config"));
2890 (void) fflush(stdout);
2893 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2895 *dslist = cb.cb_handles;
2896 *count = cb.cb_used;
2899 (void) printf("\b%s\n", gettext("done."));
2904 dataset_cmp(const void *a, const void *b)
2906 zfs_handle_t **za = (zfs_handle_t **)a;
2907 zfs_handle_t **zb = (zfs_handle_t **)b;
2908 char mounta[MAXPATHLEN];
2909 char mountb[MAXPATHLEN];
2910 boolean_t gota, gotb;
2912 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
2913 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2914 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2915 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
2916 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2917 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2920 return (strcmp(mounta, mountb));
2927 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
2931 * Generic callback for sharing or mounting filesystems. Because the code is so
2932 * similar, we have a common function with an extra parameter to determine which
2933 * mode we are using.
2935 #define OP_SHARE 0x1
2936 #define OP_MOUNT 0x2
2939 * Share or mount a dataset.
2942 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
2943 boolean_t explicit, const char *options)
2945 char mountpoint[ZFS_MAXPROPLEN];
2946 char shareopts[ZFS_MAXPROPLEN];
2947 char smbshareopts[ZFS_MAXPROPLEN];
2948 const char *cmdname = op == OP_SHARE ? "share" : "mount";
2950 uint64_t zoned, canmount;
2951 zfs_type_t type = zfs_get_type(zhp);
2952 boolean_t shared_nfs, shared_smb;
2954 assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
2956 if (type == ZFS_TYPE_FILESYSTEM) {
2958 * Check to make sure we can mount/share this dataset. If we
2959 * are in the global zone and the filesystem is exported to a
2960 * local zone, or if we are in a local zone and the
2961 * filesystem is not exported, then it is an error.
2963 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2965 if (zoned && getzoneid() == GLOBAL_ZONEID) {
2969 (void) fprintf(stderr, gettext("cannot %s '%s': "
2970 "dataset is exported to a local zone\n"), cmdname,
2974 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
2978 (void) fprintf(stderr, gettext("cannot %s '%s': "
2979 "permission denied\n"), cmdname,
2985 * Ignore any filesystems which don't apply to us. This
2986 * includes those with a legacy mountpoint, or those with
2987 * legacy share options.
2989 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2990 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
2991 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
2992 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
2993 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
2994 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
2995 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
2997 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
2998 strcmp(smbshareopts, "off") == 0) {
3002 (void) fprintf(stderr, gettext("cannot share '%s': "
3003 "legacy share\n"), zfs_get_name(zhp));
3004 (void) fprintf(stderr, gettext("use share(1M) to "
3005 "share this filesystem\n"));
3010 * We cannot share or mount legacy filesystems. If the
3011 * shareopts is non-legacy but the mountpoint is legacy, we
3012 * treat it as a legacy share.
3014 if (strcmp(mountpoint, "legacy") == 0) {
3018 (void) fprintf(stderr, gettext("cannot %s '%s': "
3019 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3020 (void) fprintf(stderr, gettext("use %s(1M) to "
3021 "%s this filesystem\n"), cmdname, cmdname);
3025 if (strcmp(mountpoint, "none") == 0) {
3029 (void) fprintf(stderr, gettext("cannot %s '%s': no "
3030 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3035 * canmount explicit outcome
3036 * on no pass through
3037 * on yes pass through
3039 * off yes display error, return 1
3040 * noauto no return 0
3041 * noauto yes pass through
3043 if (canmount == ZFS_CANMOUNT_OFF) {
3047 (void) fprintf(stderr, gettext("cannot %s '%s': "
3048 "'canmount' property is set to 'off'\n"), cmdname,
3051 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3056 * At this point, we have verified that the mountpoint and/or
3057 * shareopts are appropriate for auto management. If the
3058 * filesystem is already mounted or shared, return (failing
3059 * for explicit requests); otherwise mount or share the
3065 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3066 shared_smb = zfs_is_shared_smb(zhp, NULL);
3068 if (shared_nfs && shared_smb ||
3069 (shared_nfs && strcmp(shareopts, "on") == 0 &&
3070 strcmp(smbshareopts, "off") == 0) ||
3071 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3072 strcmp(shareopts, "off") == 0)) {
3076 (void) fprintf(stderr, gettext("cannot share "
3077 "'%s': filesystem already shared\n"),
3082 if (!zfs_is_mounted(zhp, NULL) &&
3083 zfs_mount(zhp, NULL, 0) != 0)
3086 if (protocol == NULL) {
3087 if (zfs_shareall(zhp) != 0)
3089 } else if (strcmp(protocol, "nfs") == 0) {
3090 if (zfs_share_nfs(zhp))
3092 } else if (strcmp(protocol, "smb") == 0) {
3093 if (zfs_share_smb(zhp))
3096 (void) fprintf(stderr, gettext("cannot share "
3097 "'%s': invalid share type '%s' "
3099 zfs_get_name(zhp), protocol);
3106 if (options == NULL)
3107 mnt.mnt_mntopts = "";
3109 mnt.mnt_mntopts = (char *)options;
3111 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3112 zfs_is_mounted(zhp, NULL)) {
3116 (void) fprintf(stderr, gettext("cannot mount "
3117 "'%s': filesystem already mounted\n"),
3122 if (zfs_mount(zhp, options, flags) != 0)
3127 assert(op == OP_SHARE);
3130 * Ignore any volumes that aren't shared.
3132 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
3133 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3135 if (strcmp(shareopts, "off") == 0) {
3139 (void) fprintf(stderr, gettext("cannot share '%s': "
3140 "'shareiscsi' property not set\n"),
3142 (void) fprintf(stderr, gettext("set 'shareiscsi' "
3143 "property or use iscsitadm(1M) to share this "
3148 if (zfs_is_shared_iscsi(zhp)) {
3152 (void) fprintf(stderr, gettext("cannot share "
3153 "'%s': volume already shared\n"),
3158 if (zfs_share_iscsi(zhp) != 0)
3166 * Reports progress in the form "(current/total)". Not thread-safe.
3169 report_mount_progress(int current, int total)
3172 static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3173 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3174 static time_t last_progress_time;
3175 time_t now = time(NULL);
3177 /* report 1..n instead of 0..n-1 */
3180 /* display header if we're here for the first time */
3182 (void) printf(gettext("Mounting ZFS filesystems: "));
3184 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3185 /* too soon to report again */
3189 last_progress_time = now;
3191 /* back up to prepare for overwriting */
3193 (void) printf("%*.*s", len, len, reverse);
3195 /* We put a newline at the end if this is the last one. */
3196 len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
3197 (void) fflush(stdout);
3201 append_options(char *mntopts, char *newopts)
3203 int len = strlen(mntopts);
3205 /* original length plus new string to append plus 1 for the comma */
3206 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3207 (void) fprintf(stderr, gettext("the opts argument for "
3208 "'%c' option is too long (more than %d chars)\n"),
3209 "-o", MNT_LINE_MAX);
3214 mntopts[len++] = ',';
3216 (void) strcpy(&mntopts[len], newopts);
3220 share_mount(int op, int argc, char **argv)
3223 boolean_t verbose = B_FALSE;
3225 char *options = NULL;
3226 int types, flags = 0;
3229 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3239 if (*optarg == '\0') {
3240 (void) fprintf(stderr, gettext("empty mount "
3241 "options (-o) specified\n"));
3245 if (options == NULL)
3246 options = safe_malloc(MNT_LINE_MAX + 1);
3248 /* option validation is done later */
3249 append_options(options, optarg);
3253 flags |= MS_OVERLAY;
3256 (void) fprintf(stderr, gettext("missing argument for "
3257 "'%c' option\n"), optopt);
3261 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3270 /* check number of arguments */
3272 zfs_handle_t **dslist = NULL;
3273 size_t i, count = 0;
3274 char *protocol = NULL;
3276 if (op == OP_MOUNT) {
3277 types = ZFS_TYPE_FILESYSTEM;
3278 } else if (argc > 0) {
3279 if (strcmp(argv[0], "nfs") == 0 ||
3280 strcmp(argv[0], "smb") == 0) {
3281 types = ZFS_TYPE_FILESYSTEM;
3282 } else if (strcmp(argv[0], "iscsi") == 0) {
3283 types = ZFS_TYPE_VOLUME;
3285 (void) fprintf(stderr, gettext("share type "
3286 "must be 'nfs', 'smb' or 'iscsi'\n"));
3293 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3297 (void) fprintf(stderr, gettext("too many arguments\n"));
3301 get_all_datasets(types, &dslist, &count, verbose);
3306 qsort(dslist, count, sizeof (void *), dataset_cmp);
3308 for (i = 0; i < count; i++) {
3310 report_mount_progress(i, count);
3312 if (share_mount_one(dslist[i], op, flags, protocol,
3313 B_FALSE, options) != 0)
3315 zfs_close(dslist[i]);
3319 } else if (argc == 0) {
3320 struct mnttab entry;
3322 if ((op == OP_SHARE) || (options != NULL)) {
3323 (void) fprintf(stderr, gettext("missing filesystem "
3324 "argument (specify -a for all)\n"));
3329 * When mount is given no arguments, go through /etc/mnttab and
3330 * display any active ZFS mounts. We hide any snapshots, since
3331 * they are controlled automatically.
3333 rewind(mnttab_file);
3334 while (getmntent(mnttab_file, &entry) == 0) {
3335 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3336 strchr(entry.mnt_special, '@') != NULL)
3339 (void) printf("%-30s %s\n", entry.mnt_special,
3346 types = ZFS_TYPE_FILESYSTEM;
3348 types |= ZFS_TYPE_VOLUME;
3351 (void) fprintf(stderr,
3352 gettext("too many arguments\n"));
3356 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3359 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3369 * zfs mount -a [nfs | iscsi]
3370 * zfs mount filesystem
3372 * Mount all filesystems, or mount the given filesystem.
3375 zfs_do_mount(int argc, char **argv)
3377 return (share_mount(OP_MOUNT, argc, argv));
3381 * zfs share -a [nfs | iscsi | smb]
3382 * zfs share filesystem
3384 * Share all filesystems, or share the given filesystem.
3387 zfs_do_share(int argc, char **argv)
3389 return (share_mount(OP_SHARE, argc, argv));
3392 typedef struct unshare_unmount_node {
3393 zfs_handle_t *un_zhp;
3395 uu_avl_node_t un_avlnode;
3396 } unshare_unmount_node_t;
3400 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3402 const unshare_unmount_node_t *l = larg;
3403 const unshare_unmount_node_t *r = rarg;
3405 return (strcmp(l->un_mountp, r->un_mountp));
3409 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3410 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3411 * and unmount it appropriately.
3414 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3418 struct stat64 statbuf;
3419 struct extmnttab entry;
3420 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3424 * Search for the path in /etc/mnttab. Rather than looking for the
3425 * specific path, which can be fooled by non-standard paths (i.e. ".."
3426 * or "//"), we stat() the path and search for the corresponding
3427 * (major,minor) device pair.
3429 if (stat64(path, &statbuf) != 0) {
3430 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3431 cmdname, path, strerror(errno));
3434 path_inode = statbuf.st_ino;
3437 * Search for the given (major,minor) pair in the mount table.
3439 rewind(mnttab_file);
3440 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3441 if (entry.mnt_major == major(statbuf.st_dev) &&
3442 entry.mnt_minor == minor(statbuf.st_dev))
3446 if (op == OP_SHARE) {
3447 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3448 "currently mounted\n"), cmdname, path);
3451 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3453 if ((ret = umount2(path, flags)) != 0)
3454 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3459 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3460 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3461 "filesystem\n"), cmdname, path);
3465 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3466 ZFS_TYPE_FILESYSTEM)) == NULL)
3471 if (op == OP_SHARE) {
3472 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3473 char smbshare_prop[ZFS_MAXPROPLEN];
3475 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3476 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3477 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3478 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3480 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3481 strcmp(smbshare_prop, "off") == 0) {
3482 (void) fprintf(stderr, gettext("cannot unshare "
3483 "'%s': legacy share\n"), path);
3484 (void) fprintf(stderr, gettext("use "
3485 "unshare(1M) to unshare this filesystem\n"));
3486 } else if (!zfs_is_shared(zhp)) {
3487 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3488 "not currently shared\n"), path);
3490 ret = zfs_unshareall_bypath(zhp, path);
3493 char mtpt_prop[ZFS_MAXPROPLEN];
3495 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3496 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3498 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3499 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3500 cmdname, path, strerror(errno));
3501 } else if (statbuf.st_ino != path_inode) {
3502 (void) fprintf(stderr, gettext("cannot "
3503 "unmount '%s': not a mountpoint\n"), path);
3504 } else if (is_manual) {
3505 ret = zfs_unmount(zhp, NULL, flags);
3506 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3507 (void) fprintf(stderr, gettext("cannot unmount "
3508 "'%s': legacy mountpoint\n"),
3510 (void) fprintf(stderr, gettext("use umount(1M) "
3511 "to unmount this filesystem\n"));
3513 ret = zfs_unmountall(zhp, flags);
3523 * Generic callback for unsharing or unmounting a filesystem.
3526 unshare_unmount(int op, int argc, char **argv)
3533 char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3534 char sharesmb[ZFS_MAXPROPLEN];
3537 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3546 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3557 * We could make use of zfs_for_each() to walk all datasets in
3558 * the system, but this would be very inefficient, especially
3559 * since we would have to linearly search /etc/mnttab for each
3560 * one. Instead, do one pass through /etc/mnttab looking for
3561 * zfs entries and call zfs_unmount() for each one.
3563 * Things get a little tricky if the administrator has created
3564 * mountpoints beneath other ZFS filesystems. In this case, we
3565 * have to unmount the deepest filesystems first. To accomplish
3566 * this, we place all the mountpoints in an AVL tree sorted by
3567 * the special type (dataset name), and walk the result in
3568 * reverse to make sure to get any snapshots first.
3570 struct mnttab entry;
3571 uu_avl_pool_t *pool;
3573 unshare_unmount_node_t *node;
3575 uu_avl_walk_t *walk;
3578 (void) fprintf(stderr, gettext("too many arguments\n"));
3582 if ((pool = uu_avl_pool_create("unmount_pool",
3583 sizeof (unshare_unmount_node_t),
3584 offsetof(unshare_unmount_node_t, un_avlnode),
3585 unshare_unmount_compare,
3586 UU_DEFAULT)) == NULL) {
3587 (void) fprintf(stderr, gettext("internal error: "
3588 "out of memory\n"));
3592 if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3593 (void) fprintf(stderr, gettext("internal error: "
3594 "out of memory\n"));
3598 rewind(mnttab_file);
3599 while (getmntent(mnttab_file, &entry) == 0) {
3601 /* ignore non-ZFS entries */
3602 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3605 /* ignore snapshots */
3606 if (strchr(entry.mnt_special, '@') != NULL)
3609 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3610 ZFS_TYPE_FILESYSTEM)) == NULL) {
3617 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3619 sizeof (nfsiscsi_mnt_prop),
3620 NULL, NULL, 0, B_FALSE) == 0);
3621 if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3623 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3625 sizeof (nfsiscsi_mnt_prop),
3626 NULL, NULL, 0, B_FALSE) == 0);
3627 if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3631 /* Ignore legacy mounts */
3632 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3634 sizeof (nfsiscsi_mnt_prop),
3635 NULL, NULL, 0, B_FALSE) == 0);
3636 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3638 /* Ignore canmount=noauto mounts */
3639 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3640 ZFS_CANMOUNT_NOAUTO)
3646 node = safe_malloc(sizeof (unshare_unmount_node_t));
3649 if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
3651 (void) fprintf(stderr, gettext("internal error:"
3652 " out of memory\n"));
3656 uu_avl_node_init(node, &node->un_avlnode, pool);
3658 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3659 uu_avl_insert(tree, node, idx);
3661 zfs_close(node->un_zhp);
3662 free(node->un_mountp);
3668 * Walk the AVL tree in reverse, unmounting each filesystem and
3669 * removing it from the AVL tree in the process.
3671 if ((walk = uu_avl_walk_start(tree,
3672 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3673 (void) fprintf(stderr,
3674 gettext("internal error: out of memory"));
3678 while ((node = uu_avl_walk_next(walk)) != NULL) {
3679 uu_avl_remove(tree, node);
3683 if (zfs_unshareall_bypath(node->un_zhp,
3684 node->un_mountp) != 0)
3689 if (zfs_unmount(node->un_zhp,
3690 node->un_mountp, flags) != 0)
3695 zfs_close(node->un_zhp);
3696 free(node->un_mountp);
3700 uu_avl_walk_end(walk);
3701 uu_avl_destroy(tree);
3702 uu_avl_pool_destroy(pool);
3704 if (op == OP_SHARE) {
3706 * Finally, unshare any volumes shared via iSCSI.
3708 zfs_handle_t **dslist = NULL;
3709 size_t i, count = 0;
3711 get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
3715 qsort(dslist, count, sizeof (void *),
3718 for (i = 0; i < count; i++) {
3719 if (zfs_unshare_iscsi(dslist[i]) != 0)
3721 zfs_close(dslist[i]);
3730 (void) fprintf(stderr,
3731 gettext("missing filesystem argument\n"));
3733 (void) fprintf(stderr,
3734 gettext("too many arguments\n"));
3739 * We have an argument, but it may be a full path or a ZFS
3740 * filesystem. Pass full paths off to unmount_path() (shared by
3741 * manual_unmount), otherwise open the filesystem and pass to
3744 if (argv[0][0] == '/')
3745 return (unshare_unmount_path(op, argv[0],
3748 types = ZFS_TYPE_FILESYSTEM;
3750 types |= ZFS_TYPE_VOLUME;
3752 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3755 if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
3756 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3757 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3758 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3759 NULL, 0, B_FALSE) == 0);
3763 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3765 sizeof (nfsiscsi_mnt_prop),
3766 NULL, NULL, 0, B_FALSE) == 0);
3767 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3768 sharesmb, sizeof (sharesmb), NULL, NULL,
3771 if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3772 strcmp(sharesmb, "off") == 0) {
3773 (void) fprintf(stderr, gettext("cannot "
3774 "unshare '%s': legacy share\n"),
3776 (void) fprintf(stderr, gettext("use "
3777 "unshare(1M) to unshare this "
3780 } else if (!zfs_is_shared(zhp)) {
3781 (void) fprintf(stderr, gettext("cannot "
3782 "unshare '%s': not currently "
3783 "shared\n"), zfs_get_name(zhp));
3785 } else if (zfs_unshareall(zhp) != 0) {
3791 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3792 (void) fprintf(stderr, gettext("cannot "
3793 "unmount '%s': legacy "
3794 "mountpoint\n"), zfs_get_name(zhp));
3795 (void) fprintf(stderr, gettext("use "
3796 "umount(1M) to unmount this "
3799 } else if (!zfs_is_mounted(zhp, NULL)) {
3800 (void) fprintf(stderr, gettext("cannot "
3801 "unmount '%s': not currently "
3805 } else if (zfs_unmountall(zhp, flags) != 0) {
3811 assert(op == OP_SHARE);
3813 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3814 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3815 NULL, NULL, 0, B_FALSE) == 0);
3817 if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
3818 (void) fprintf(stderr, gettext("cannot unshare "
3819 "'%s': 'shareiscsi' property not set\n"),
3821 (void) fprintf(stderr, gettext("set "
3822 "'shareiscsi' property or use "
3823 "iscsitadm(1M) to share this volume\n"));
3825 } else if (!zfs_is_shared_iscsi(zhp)) {
3826 (void) fprintf(stderr, gettext("cannot "
3827 "unshare '%s': not currently shared\n"),
3830 } else if (zfs_unshare_iscsi(zhp) != 0) {
3843 * zfs unmount filesystem
3845 * Unmount all filesystems, or a specific ZFS filesystem.
3848 zfs_do_unmount(int argc, char **argv)
3850 return (unshare_unmount(OP_MOUNT, argc, argv));
3855 * zfs unshare filesystem
3857 * Unshare all filesystems, or a specific ZFS filesystem.
3860 zfs_do_unshare(int argc, char **argv)
3862 return (unshare_unmount(OP_SHARE, argc, argv));
3866 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
3867 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
3870 manual_mount(int argc, char **argv)
3873 char mountpoint[ZFS_MAXPROPLEN];
3874 char mntopts[MNT_LINE_MAX] = { '\0' };
3878 char *dataset, *path;
3881 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3884 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
3887 flags |= MS_OVERLAY;
3890 flags |= MS_NOMNTTAB;
3893 (void) fprintf(stderr, gettext("missing argument for "
3894 "'%c' option\n"), optopt);
3898 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3900 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
3909 /* check that we only have two arguments */
3912 (void) fprintf(stderr, gettext("missing dataset "
3915 (void) fprintf(stderr,
3916 gettext("missing mountpoint argument\n"));
3918 (void) fprintf(stderr, gettext("too many arguments\n"));
3919 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3926 /* try to open the dataset */
3927 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3930 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3931 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3933 /* check for legacy mountpoint and complain appropriately */
3935 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
3936 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
3937 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
3938 (void) fprintf(stderr, gettext("mount failed: %s\n"),
3943 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
3944 "mounted using 'mount -F zfs'\n"), dataset);
3945 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
3946 "instead.\n"), path);
3947 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
3948 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
3949 (void) fprintf(stderr, gettext("See zfs(1M) for more "
3958 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
3959 * unmounts of non-legacy filesystems, as this is the dominant administrative
3963 manual_unmount(int argc, char **argv)
3969 while ((c = getopt(argc, argv, "f")) != -1) {
3975 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3977 (void) fprintf(stderr, gettext("usage: unmount [-f] "
3986 /* check arguments */
3989 (void) fprintf(stderr, gettext("missing path "
3992 (void) fprintf(stderr, gettext("too many arguments\n"));
3993 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
3997 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
4001 volcheck(zpool_handle_t *zhp, void *data)
4003 boolean_t isinit = *((boolean_t *)data);
4006 return (zpool_create_zvol_links(zhp));
4008 return (zpool_remove_zvol_links(zhp));
4012 * Iterate over all pools in the system and either create or destroy /dev/zvol
4013 * links, depending on the value of 'isinit'.
4016 do_volcheck(boolean_t isinit)
4018 return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
4022 find_command_idx(char *command, int *idx)
4026 for (i = 0; i < NCOMMAND; i++) {
4027 if (command_table[i].name == NULL)
4030 if (strcmp(command, command_table[i].name) == 0) {
4039 main(int argc, char **argv)
4046 (void) setlocale(LC_ALL, "");
4047 (void) textdomain(TEXT_DOMAIN);
4051 if ((g_zfs = libzfs_init()) == NULL) {
4052 (void) fprintf(stderr, gettext("internal error: failed to "
4053 "initialize ZFS library\n"));
4057 zpool_set_history_str("zfs", argc, argv, history_str);
4058 verify(zpool_stage_history(g_zfs, history_str) == 0);
4060 libzfs_print_on_error(g_zfs, B_TRUE);
4062 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4063 (void) fprintf(stderr, gettext("internal error: unable to "
4064 "open %s\n"), MNTTAB);
4069 * This command also doubles as the /etc/fs mount and unmount program.
4070 * Determine if we should take this behavior based on argv[0].
4072 progname = basename(argv[0]);
4073 if (strcmp(progname, "mount") == 0) {
4074 ret = manual_mount(argc, argv);
4075 } else if (strcmp(progname, "umount") == 0) {
4076 ret = manual_unmount(argc, argv);
4079 * Make sure the user has specified some command.
4082 (void) fprintf(stderr, gettext("missing command\n"));
4089 * The 'umount' command is an alias for 'unmount'
4091 if (strcmp(cmdname, "umount") == 0)
4092 cmdname = "unmount";
4095 * The 'recv' command is an alias for 'receive'
4097 if (strcmp(cmdname, "recv") == 0)
4098 cmdname = "receive";
4103 if (strcmp(cmdname, "-?") == 0)
4107 * 'volinit' and 'volfini' do not appear in the usage message,
4108 * so we have to special case them here.
4110 if (strcmp(cmdname, "volinit") == 0)
4111 return (do_volcheck(B_TRUE));
4112 else if (strcmp(cmdname, "volfini") == 0)
4113 return (do_volcheck(B_FALSE));
4116 * Run the appropriate command.
4118 if (find_command_idx(cmdname, &i) == 0) {
4119 current_command = &command_table[i];
4120 ret = command_table[i].func(argc - 1, argv + 1);
4121 } else if (strchr(cmdname, '=') != NULL) {
4122 verify(find_command_idx("set", &i) == 0);
4123 current_command = &command_table[i];
4124 ret = command_table[i].func(argc, argv);
4126 (void) fprintf(stderr, gettext("unrecognized "
4127 "command '%s'\n"), cmdname);
4132 (void) fclose(mnttab_file);
4137 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4138 * for the purposes of running ::findleaks.
4140 if (getenv("ZFS_ABORT") != NULL) {
4141 (void) printf("dumping core by request\n");