4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
33 #include <libnvpair.h>
42 #include <sys/mkdev.h>
43 #include <sys/mntent.h>
44 #include <sys/mnttab.h>
45 #include <sys/mount.h>
55 libzfs_handle_t *g_zfs;
57 static FILE *mnttab_file;
58 static char history_str[HIS_MAX_RECORD_LEN];
60 static int zfs_do_clone(int argc, char **argv);
61 static int zfs_do_create(int argc, char **argv);
62 static int zfs_do_destroy(int argc, char **argv);
63 static int zfs_do_get(int argc, char **argv);
64 static int zfs_do_inherit(int argc, char **argv);
65 static int zfs_do_list(int argc, char **argv);
66 static int zfs_do_mount(int argc, char **argv);
67 static int zfs_do_rename(int argc, char **argv);
68 static int zfs_do_rollback(int argc, char **argv);
69 static int zfs_do_set(int argc, char **argv);
70 static int zfs_do_upgrade(int argc, char **argv);
71 static int zfs_do_snapshot(int argc, char **argv);
72 static int zfs_do_unmount(int argc, char **argv);
73 static int zfs_do_share(int argc, char **argv);
74 static int zfs_do_unshare(int argc, char **argv);
75 static int zfs_do_send(int argc, char **argv);
76 static int zfs_do_receive(int argc, char **argv);
77 static int zfs_do_promote(int argc, char **argv);
78 static int zfs_do_allow(int argc, char **argv);
79 static int zfs_do_unallow(int argc, char **argv);
82 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
87 _umem_debug_init(void)
89 return ("default,verbose"); /* $UMEM_DEBUG setting */
93 _umem_logging_init(void)
95 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] [-o property=value] ... "
177 "<snapshot> <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|snapshot> ...\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|snapshot> ...\n"));
224 return (gettext("\tshare <-a | filesystem>\n"));
226 return (gettext("\tsnapshot [-r] [-o property=value] ... "
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%-15s ", zfs_prop_to_name(prop));
286 if (zfs_prop_readonly(prop))
287 (void) fprintf(fp, " NO ");
289 (void) fprintf(fp, "YES ");
291 if (zfs_prop_inheritable(prop))
292 (void) fprintf(fp, " YES ");
294 (void) fprintf(fp, " NO ");
296 if (zfs_prop_values(prop) == NULL)
297 (void) fprintf(fp, "-\n");
299 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
305 * Display usage message. If we're inside a command, display only the usage for
306 * that command. Otherwise, iterate over the entire command table and display
307 * a complete usage message.
310 usage(boolean_t requested)
313 boolean_t show_properties = B_FALSE;
314 boolean_t show_permissions = B_FALSE;
315 FILE *fp = requested ? stdout : stderr;
317 if (current_command == NULL) {
319 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
321 gettext("where 'command' is one of the following:\n\n"));
323 for (i = 0; i < NCOMMAND; i++) {
324 if (command_table[i].name == NULL)
325 (void) fprintf(fp, "\n");
327 (void) fprintf(fp, "%s",
328 get_usage(command_table[i].usage));
331 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
332 "pool/[dataset/]*dataset[@name]\n"));
334 (void) fprintf(fp, gettext("usage:\n"));
335 (void) fprintf(fp, "%s", get_usage(current_command->usage));
338 if (current_command != NULL &&
339 (strcmp(current_command->name, "set") == 0 ||
340 strcmp(current_command->name, "get") == 0 ||
341 strcmp(current_command->name, "inherit") == 0 ||
342 strcmp(current_command->name, "list") == 0))
343 show_properties = B_TRUE;
345 if (current_command != NULL &&
346 (strcmp(current_command->name, "allow") == 0 ||
347 strcmp(current_command->name, "unallow") == 0))
348 show_permissions = B_TRUE;
350 if (show_properties) {
353 gettext("\nThe following properties are supported:\n"));
355 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
356 "PROPERTY", "EDIT", "INHERIT", "VALUES");
358 /* Iterate over all properties */
359 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
362 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
363 "with standard units such as K, M, G, etc.\n"));
364 (void) fprintf(fp, gettext("\nUser-defined properties can "
365 "be specified by using a name containing a colon (:).\n"));
367 } else if (show_permissions) {
369 gettext("\nThe following permissions are supported:\n"));
371 zfs_deleg_permissions();
374 gettext("\nFor the property list, run: %s\n"),
377 gettext("\nFor the delegated permission list, run: %s\n"),
378 "zfs allow|unallow");
382 * See comments at end of main().
384 if (getenv("ZFS_ABORT") != NULL) {
385 (void) printf("dumping core by request\n");
389 exit(requested ? 0 : 2);
393 parseprop(nvlist_t *props)
395 char *propname = optarg;
396 char *propval, *strval;
398 if ((propval = strchr(propname, '=')) == NULL) {
399 (void) fprintf(stderr, gettext("missing "
400 "'=' for -o option\n"));
405 if (nvlist_lookup_string(props, propname, &strval) == 0) {
406 (void) fprintf(stderr, gettext("property '%s' "
407 "specified multiple times\n"), propname);
410 if (nvlist_add_string(props, propname, propval) != 0) {
411 (void) fprintf(stderr, gettext("internal "
412 "error: out of memory\n"));
419 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
421 * Given an existing dataset, create a writable copy whose initial contents
422 * are the same as the source. The newly created dataset maintains a
423 * dependency on the original; the original cannot be destroyed so long as
426 * The '-p' flag creates all the non-existing ancestors of the target first.
429 zfs_do_clone(int argc, char **argv)
431 zfs_handle_t *zhp = NULL;
432 boolean_t parents = B_FALSE;
437 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
438 (void) fprintf(stderr, gettext("internal error: "
444 while ((c = getopt(argc, argv, "o:p")) != -1) {
447 if (parseprop(props))
454 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
463 /* check number of arguments */
465 (void) fprintf(stderr, gettext("missing source dataset "
470 (void) fprintf(stderr, gettext("missing target dataset "
475 (void) fprintf(stderr, gettext("too many arguments\n"));
479 /* open the source dataset */
480 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
483 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
486 * Now create the ancestors of the target dataset. If the
487 * target already exists and '-p' option was used we should not
490 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
493 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
498 ret = zfs_clone(zhp, argv[1], props);
500 /* create the mountpoint if necessary */
504 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
506 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
507 ret = zfs_share(clone);
526 * zfs create [-p] [-o prop=value] ... fs
527 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
529 * Create a new dataset. This command can be used to create filesystems
530 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
531 * For volumes, the user must specify a size to be used.
533 * The '-s' flag applies only to volumes, and indicates that we should not try
534 * to set the reservation for this volume. By default we set a reservation
535 * equal to the size for any volume. For pools with SPA_VERSION >=
536 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
538 * The '-p' flag creates all the non-existing ancestors of the target first.
541 zfs_do_create(int argc, char **argv)
543 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
544 zfs_handle_t *zhp = NULL;
547 boolean_t noreserve = B_FALSE;
548 boolean_t bflag = B_FALSE;
549 boolean_t parents = B_FALSE;
555 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
556 (void) fprintf(stderr, gettext("internal error: "
562 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
565 type = ZFS_TYPE_VOLUME;
566 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
567 (void) fprintf(stderr, gettext("bad volume "
568 "size '%s': %s\n"), optarg,
569 libzfs_error_description(g_zfs));
573 if (nvlist_add_uint64(props,
574 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
576 (void) fprintf(stderr, gettext("internal "
577 "error: out of memory\n"));
587 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
588 (void) fprintf(stderr, gettext("bad volume "
589 "block size '%s': %s\n"), optarg,
590 libzfs_error_description(g_zfs));
594 if (nvlist_add_uint64(props,
595 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
597 (void) fprintf(stderr, gettext("internal "
598 "error: out of memory\n"));
603 if (parseprop(props))
610 (void) fprintf(stderr, gettext("missing size "
615 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
621 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
622 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
623 "used when creating a volume\n"));
630 /* check number of arguments */
632 (void) fprintf(stderr, gettext("missing %s argument\n"),
633 zfs_type_to_name(type));
637 (void) fprintf(stderr, gettext("too many arguments\n"));
641 if (type == ZFS_TYPE_VOLUME && !noreserve) {
642 zpool_handle_t *zpool_handle;
643 uint64_t spa_version;
645 zfs_prop_t resv_prop;
648 if (p = strchr(argv[0], '/'))
650 zpool_handle = zpool_open(g_zfs, argv[0]);
653 if (zpool_handle == NULL)
655 spa_version = zpool_get_prop_int(zpool_handle,
656 ZPOOL_PROP_VERSION, NULL);
657 zpool_close(zpool_handle);
658 if (spa_version >= SPA_VERSION_REFRESERVATION)
659 resv_prop = ZFS_PROP_REFRESERVATION;
661 resv_prop = ZFS_PROP_RESERVATION;
663 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
665 if (nvlist_add_uint64(props,
666 zfs_prop_to_name(resv_prop), volsize) != 0) {
667 (void) fprintf(stderr, gettext("internal "
668 "error: out of memory\n"));
675 if (parents && zfs_name_valid(argv[0], type)) {
677 * Now create the ancestors of target dataset. If the target
678 * already exists and '-p' option was used we should not
681 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
685 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
690 if (zfs_create(g_zfs, argv[0], type, props) != 0)
693 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
696 * if the user doesn't want the dataset automatically mounted,
697 * then skip the mount/share step
700 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
703 * Mount and/or share the new filesystem as appropriate. We provide a
704 * verbose error message to let the user know that their filesystem was
705 * in fact created, even if we failed to mount or share it.
708 if (canmount == ZFS_CANMOUNT_ON) {
709 if (zfs_mount(zhp, NULL, 0) != 0) {
710 (void) fprintf(stderr, gettext("filesystem "
711 "successfully created, but not mounted\n"));
713 } else if (zfs_share(zhp) != 0) {
714 (void) fprintf(stderr, gettext("filesystem "
715 "successfully created, but not shared\n"));
732 * zfs destroy [-rf] <fs, snap, vol>
734 * -r Recursively destroy all children
735 * -R Recursively destroy all dependents, including clones
736 * -f Force unmounting of any dependents
738 * Destroys the given dataset. By default, it will unmount any filesystems,
739 * and refuse to destroy a dataset that has any dependents. A dependent can
740 * either be a child, or a clone of a child.
742 typedef struct destroy_cbdata {
749 boolean_t cb_closezhp;
750 zfs_handle_t *cb_target;
755 * Check for any dependents based on the '-r' or '-R' flags.
758 destroy_check_dependent(zfs_handle_t *zhp, void *data)
760 destroy_cbdata_t *cbp = data;
761 const char *tname = zfs_get_name(cbp->cb_target);
762 const char *name = zfs_get_name(zhp);
764 if (strncmp(tname, name, strlen(tname)) == 0 &&
765 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
767 * This is a direct descendant, not a clone somewhere else in
774 (void) fprintf(stderr, gettext("cannot destroy '%s': "
775 "%s has children\n"),
776 zfs_get_name(cbp->cb_target),
777 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
778 (void) fprintf(stderr, gettext("use '-r' to destroy "
779 "the following datasets:\n"));
780 cbp->cb_first = B_FALSE;
784 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
787 * This is a clone. We only want to report this if the '-r'
788 * wasn't specified, or the target is a snapshot.
790 if (!cbp->cb_recurse &&
791 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
795 (void) fprintf(stderr, gettext("cannot destroy '%s': "
796 "%s has dependent clones\n"),
797 zfs_get_name(cbp->cb_target),
798 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
799 (void) fprintf(stderr, gettext("use '-R' to destroy "
800 "the following datasets:\n"));
801 cbp->cb_first = B_FALSE;
805 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
814 destroy_callback(zfs_handle_t *zhp, void *data)
816 destroy_cbdata_t *cbp = data;
819 * Ignore pools (which we've already flagged as an error before getting
822 if (strchr(zfs_get_name(zhp), '/') == NULL &&
823 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
829 * Bail out on the first error.
831 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
832 zfs_destroy(zhp) != 0) {
842 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
844 destroy_cbdata_t *cbp = arg;
845 char thissnap[MAXPATHLEN];
847 boolean_t closezhp = cbp->cb_closezhp;
850 (void) snprintf(thissnap, sizeof (thissnap),
851 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
853 libzfs_print_on_error(g_zfs, B_FALSE);
854 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
855 libzfs_print_on_error(g_zfs, B_TRUE);
858 * Destroy any clones of this snapshot
860 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
870 cbp->cb_closezhp = B_TRUE;
871 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
878 zfs_do_destroy(int argc, char **argv)
880 destroy_cbdata_t cb = { 0 };
886 while ((c = getopt(argc, argv, "frR")) != -1) {
900 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
909 /* check number of arguments */
911 (void) fprintf(stderr, gettext("missing path argument\n"));
915 (void) fprintf(stderr, gettext("too many arguments\n"));
920 * If we are doing recursive destroy of a snapshot, then the
921 * named snapshot may not exist. Go straight to libzfs.
923 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
927 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
932 if (cb.cb_doclones) {
934 if (destroy_snap_clones(zhp, &cb) != 0) {
940 ret = zfs_destroy_snaps(zhp, cp);
943 (void) fprintf(stderr,
944 gettext("no snapshots destroyed\n"));
950 /* Open the given dataset */
951 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
957 * Perform an explicit check for pools before going any further.
959 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
960 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
961 (void) fprintf(stderr, gettext("cannot destroy '%s': "
962 "operation does not apply to pools\n"),
964 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
965 "%s' to destroy all datasets in the pool\n"),
967 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
968 "to destroy the pool itself\n"), zfs_get_name(zhp));
974 * Check for any dependents and/or clones.
976 cb.cb_first = B_TRUE;
977 if (!cb.cb_doclones &&
978 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
985 zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
991 * Do the real thing. The callback will close the handle regardless of
992 * whether it succeeds or not.
995 if (destroy_callback(zhp, &cb) != 0)
1003 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
1004 * < all | property[,property]... > < fs | snap | vol > ...
1006 * -r recurse over any child datasets
1007 * -H scripted mode. Headers are stripped, and fields are separated
1008 * by tabs instead of spaces.
1009 * -o Set of fields to display. One of "name,property,value,source".
1010 * Default is all four.
1011 * -s Set of sources to allow. One of
1012 * "local,default,inherited,temporary,none". Default is all
1014 * -p Display values in parsable (literal) format.
1016 * Prints properties for the given datasets. The user can control which
1017 * columns to display as well as which property types to allow.
1021 * Invoked to display the properties for a single dataset.
1024 get_callback(zfs_handle_t *zhp, void *data)
1026 char buf[ZFS_MAXPROPLEN];
1027 zprop_source_t sourcetype;
1028 char source[ZFS_MAXNAMELEN];
1029 zprop_get_cbdata_t *cbp = data;
1030 nvlist_t *userprop = zfs_get_user_props(zhp);
1031 zprop_list_t *pl = cbp->cb_proplist;
1036 for (; pl != NULL; pl = pl->pl_next) {
1038 * Skip the special fake placeholder. This will also skip over
1039 * the name property when 'all' is specified.
1041 if (pl->pl_prop == ZFS_PROP_NAME &&
1042 pl == cbp->cb_proplist)
1045 if (pl->pl_prop != ZPROP_INVAL) {
1046 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1047 sizeof (buf), &sourcetype, source,
1049 cbp->cb_literal) != 0) {
1052 if (!zfs_prop_valid_for_type(pl->pl_prop,
1053 ZFS_TYPE_DATASET)) {
1054 (void) fprintf(stderr,
1055 gettext("No such property '%s'\n"),
1056 zfs_prop_to_name(pl->pl_prop));
1059 sourcetype = ZPROP_SRC_NONE;
1060 (void) strlcpy(buf, "-", sizeof (buf));
1063 zprop_print_one_property(zfs_get_name(zhp), cbp,
1064 zfs_prop_to_name(pl->pl_prop),
1065 buf, sourcetype, source);
1067 if (nvlist_lookup_nvlist(userprop,
1068 pl->pl_user_prop, &propval) != 0) {
1071 sourcetype = ZPROP_SRC_NONE;
1074 verify(nvlist_lookup_string(propval,
1075 ZPROP_VALUE, &strval) == 0);
1076 verify(nvlist_lookup_string(propval,
1077 ZPROP_SOURCE, &sourceval) == 0);
1079 if (strcmp(sourceval,
1080 zfs_get_name(zhp)) == 0) {
1081 sourcetype = ZPROP_SRC_LOCAL;
1083 sourcetype = ZPROP_SRC_INHERITED;
1084 (void) strlcpy(source,
1085 sourceval, sizeof (source));
1089 zprop_print_one_property(zfs_get_name(zhp), cbp,
1090 pl->pl_user_prop, strval, sourcetype,
1099 zfs_do_get(int argc, char **argv)
1101 zprop_get_cbdata_t cb = { 0 };
1102 int i, c, flags = 0;
1103 char *value, *fields;
1105 zprop_list_t fake_name = { 0 };
1108 * Set up default columns and sources.
1110 cb.cb_sources = ZPROP_SRC_ALL;
1111 cb.cb_columns[0] = GET_COL_NAME;
1112 cb.cb_columns[1] = GET_COL_PROPERTY;
1113 cb.cb_columns[2] = GET_COL_VALUE;
1114 cb.cb_columns[3] = GET_COL_SOURCE;
1115 cb.cb_type = ZFS_TYPE_DATASET;
1118 while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
1121 cb.cb_literal = B_TRUE;
1124 flags |= ZFS_ITER_RECURSE;
1127 cb.cb_scripted = B_TRUE;
1130 (void) fprintf(stderr, gettext("missing argument for "
1131 "'%c' option\n"), optopt);
1136 * Process the set of columns to display. We zero out
1137 * the structure to give us a blank slate.
1139 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1141 while (*optarg != '\0') {
1142 static char *col_subopts[] =
1143 { "name", "property", "value", "source",
1147 (void) fprintf(stderr, gettext("too "
1148 "many fields given to -o "
1153 switch (getsubopt(&optarg, col_subopts,
1156 cb.cb_columns[i++] = GET_COL_NAME;
1159 cb.cb_columns[i++] = GET_COL_PROPERTY;
1162 cb.cb_columns[i++] = GET_COL_VALUE;
1165 cb.cb_columns[i++] = GET_COL_SOURCE;
1168 (void) fprintf(stderr,
1169 gettext("invalid column name "
1178 while (*optarg != '\0') {
1179 static char *source_subopts[] = {
1180 "local", "default", "inherited",
1181 "temporary", "none", NULL };
1183 switch (getsubopt(&optarg, source_subopts,
1186 cb.cb_sources |= ZPROP_SRC_LOCAL;
1189 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1192 cb.cb_sources |= ZPROP_SRC_INHERITED;
1195 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1198 cb.cb_sources |= ZPROP_SRC_NONE;
1201 (void) fprintf(stderr,
1202 gettext("invalid source "
1210 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1220 (void) fprintf(stderr, gettext("missing property "
1227 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1235 * As part of zfs_expand_proplist(), we keep track of the maximum column
1236 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1237 * need to know the maximum name length. However, the user likely did
1238 * not specify 'name' as one of the properties to fetch, so we need to
1239 * make sure we always include at least this property for
1240 * print_get_headers() to work properly.
1242 if (cb.cb_proplist != NULL) {
1243 fake_name.pl_prop = ZFS_PROP_NAME;
1244 fake_name.pl_width = strlen(gettext("NAME"));
1245 fake_name.pl_next = cb.cb_proplist;
1246 cb.cb_proplist = &fake_name;
1249 cb.cb_first = B_TRUE;
1251 /* run for each object */
1252 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1253 &cb.cb_proplist, get_callback, &cb);
1255 if (cb.cb_proplist == &fake_name)
1256 zprop_free_list(fake_name.pl_next);
1258 zprop_free_list(cb.cb_proplist);
1264 * inherit [-r] <property> <fs|vol> ...
1266 * -r Recurse over all children
1268 * For each dataset specified on the command line, inherit the given property
1269 * from its parent. Inheriting a property at the pool level will cause it to
1270 * use the default value. The '-r' flag will recurse over all children, and is
1271 * useful for setting a property on a hierarchy-wide basis, regardless of any
1272 * local modifications for each dataset.
1276 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1278 char *propname = data;
1279 zfs_prop_t prop = zfs_name_to_prop(propname);
1282 * If we're doing it recursively, then ignore properties that
1283 * are not valid for this type of dataset.
1285 if (prop != ZPROP_INVAL &&
1286 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1289 return (zfs_prop_inherit(zhp, propname) != 0);
1293 inherit_cb(zfs_handle_t *zhp, void *data)
1295 char *propname = data;
1297 return (zfs_prop_inherit(zhp, propname) != 0);
1301 zfs_do_inherit(int argc, char **argv)
1310 while ((c = getopt(argc, argv, "r")) != -1) {
1313 flags |= ZFS_ITER_RECURSE;
1317 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1326 /* check number of arguments */
1328 (void) fprintf(stderr, gettext("missing property argument\n"));
1332 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1340 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1341 if (zfs_prop_readonly(prop)) {
1342 (void) fprintf(stderr, gettext(
1343 "%s property is read-only\n"),
1347 if (!zfs_prop_inheritable(prop)) {
1348 (void) fprintf(stderr, gettext("'%s' property cannot "
1349 "be inherited\n"), propname);
1350 if (prop == ZFS_PROP_QUOTA ||
1351 prop == ZFS_PROP_RESERVATION ||
1352 prop == ZFS_PROP_REFQUOTA ||
1353 prop == ZFS_PROP_REFRESERVATION)
1354 (void) fprintf(stderr, gettext("use 'zfs set "
1355 "%s=none' to clear\n"), propname);
1358 } else if (!zfs_prop_user(propname)) {
1359 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1364 if (flags & ZFS_ITER_RECURSE) {
1365 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1366 NULL, NULL, inherit_recurse_cb, propname);
1368 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1369 NULL, NULL, inherit_cb, propname);
1375 typedef struct upgrade_cbdata {
1376 uint64_t cb_numupgraded;
1377 uint64_t cb_numsamegraded;
1378 uint64_t cb_numfailed;
1379 uint64_t cb_version;
1381 boolean_t cb_foundone;
1382 char cb_lastfs[ZFS_MAXNAMELEN];
1386 same_pool(zfs_handle_t *zhp, const char *name)
1388 int len1 = strcspn(name, "/@");
1389 const char *zhname = zfs_get_name(zhp);
1390 int len2 = strcspn(zhname, "/@");
1394 return (strncmp(name, zhname, len1) == 0);
1398 upgrade_list_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 /* list if it's old/new */
1404 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1405 (cb->cb_newer && version > ZPL_VERSION)) {
1408 str = gettext("The following filesystems are "
1409 "formatted using a newer software version and\n"
1410 "cannot be accessed on the current system.\n\n");
1412 str = gettext("The following filesystems are "
1413 "out of date, and can be upgraded. After being\n"
1414 "upgraded, these filesystems (and any 'zfs send' "
1415 "streams generated from\n"
1416 "subsequent snapshots) will no longer be "
1417 "accessible by older software versions.\n\n");
1420 if (!cb->cb_foundone) {
1422 (void) printf(gettext("VER FILESYSTEM\n"));
1423 (void) printf(gettext("--- ------------\n"));
1424 cb->cb_foundone = B_TRUE;
1427 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1434 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1436 upgrade_cbdata_t *cb = data;
1437 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1439 if (cb->cb_version >= ZPL_VERSION_FUID) {
1442 if (zfs_spa_version(zhp, &spa_version) < 0)
1445 if (spa_version < SPA_VERSION_FUID) {
1447 (void) printf(gettext("%s: can not be upgraded; "
1448 "the pool version needs to first be upgraded\nto "
1450 zfs_get_name(zhp), SPA_VERSION_FUID);
1457 if (version < cb->cb_version) {
1459 (void) snprintf(verstr, sizeof (verstr),
1460 "%llu", cb->cb_version);
1461 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1463 * If they did "zfs upgrade -a", then we could
1464 * be doing ioctls to different pools. We need
1465 * to log this history once to each pool.
1467 verify(zpool_stage_history(g_zfs, history_str) == 0);
1469 if (zfs_prop_set(zhp, "version", verstr) == 0)
1470 cb->cb_numupgraded++;
1473 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1474 } else if (version > cb->cb_version) {
1475 /* can't downgrade */
1476 (void) printf(gettext("%s: can not be downgraded; "
1477 "it is already at version %u\n"),
1478 zfs_get_name(zhp), version);
1481 cb->cb_numsamegraded++;
1489 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1492 zfs_do_upgrade(int argc, char **argv)
1494 boolean_t all = B_FALSE;
1495 boolean_t showversions = B_FALSE;
1497 upgrade_cbdata_t cb = { 0 };
1499 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1502 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1505 flags |= ZFS_ITER_RECURSE;
1508 showversions = B_TRUE;
1511 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1512 optarg, &cb.cb_version) != 0) {
1513 (void) fprintf(stderr,
1514 gettext("invalid version %s\n"), optarg);
1523 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1532 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1534 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1535 cb.cb_version || argc))
1537 if ((all || argc) && (showversions))
1543 /* Show info on available versions. */
1544 (void) printf(gettext("The following filesystem versions are "
1546 (void) printf(gettext("VER DESCRIPTION\n"));
1547 (void) printf("--- -----------------------------------------"
1548 "---------------\n");
1549 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1550 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1551 (void) printf(gettext(" 3 Case insensitive and File system "
1552 "unique identifer (FUID)\n"));
1553 (void) printf(gettext("\nFor more information on a particular "
1554 "version, including supported releases, see:\n\n"));
1555 (void) printf("http://www.opensolaris.org/os/community/zfs/"
1556 "version/zpl/N\n\n");
1557 (void) printf(gettext("Where 'N' is the version number.\n"));
1559 } else if (argc || all) {
1560 /* Upgrade filesystems */
1561 if (cb.cb_version == 0)
1562 cb.cb_version = ZPL_VERSION;
1563 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1564 NULL, NULL, upgrade_set_callback, &cb);
1565 (void) printf(gettext("%llu filesystems upgraded\n"),
1567 if (cb.cb_numsamegraded) {
1568 (void) printf(gettext("%llu filesystems already at "
1570 cb.cb_numsamegraded);
1572 if (cb.cb_numfailed != 0)
1575 /* List old-version filesytems */
1577 (void) printf(gettext("This system is currently running "
1578 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1580 flags |= ZFS_ITER_RECURSE;
1581 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1582 NULL, NULL, upgrade_list_callback, &cb);
1584 found = cb.cb_foundone;
1585 cb.cb_foundone = B_FALSE;
1586 cb.cb_newer = B_TRUE;
1588 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1589 NULL, NULL, upgrade_list_callback, &cb);
1591 if (!cb.cb_foundone && !found) {
1592 (void) printf(gettext("All filesystems are "
1593 "formatted with the current version.\n"));
1601 * list [-rH] [-o property[,property]...] [-t type[,type]...]
1602 * [-s property [-s property]...] [-S property [-S property]...]
1605 * -r Recurse over all children
1606 * -H Scripted mode; elide headers and separate columns by tabs
1607 * -o Control which fields to display.
1608 * -t Control which object types to display.
1609 * -s Specify sort columns, descending order.
1610 * -S Specify sort columns, ascending order.
1612 * When given no arguments, lists all filesystems in the system.
1613 * Otherwise, list the specified datasets, optionally recursing down them if
1614 * '-r' is specified.
1616 typedef struct list_cbdata {
1618 boolean_t cb_scripted;
1619 zprop_list_t *cb_proplist;
1623 * Given a list of columns to display, output appropriate headers for each one.
1626 print_header(zprop_list_t *pl)
1628 char headerbuf[ZFS_MAXPROPLEN];
1631 boolean_t first = B_TRUE;
1632 boolean_t right_justify;
1634 for (; pl != NULL; pl = pl->pl_next) {
1641 right_justify = B_FALSE;
1642 if (pl->pl_prop != ZPROP_INVAL) {
1643 header = zfs_prop_column_name(pl->pl_prop);
1644 right_justify = zfs_prop_align_right(pl->pl_prop);
1646 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1647 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1648 headerbuf[i] = '\0';
1652 if (pl->pl_next == NULL && !right_justify)
1653 (void) printf("%s", header);
1654 else if (right_justify)
1655 (void) printf("%*s", pl->pl_width, header);
1657 (void) printf("%-*s", pl->pl_width, header);
1660 (void) printf("\n");
1664 * Given a dataset and a list of fields, print out all the properties according
1665 * to the described layout.
1668 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1670 boolean_t first = B_TRUE;
1671 char property[ZFS_MAXPROPLEN];
1672 nvlist_t *userprops = zfs_get_user_props(zhp);
1675 boolean_t right_justify;
1678 for (; pl != NULL; pl = pl->pl_next) {
1681 (void) printf("\t");
1688 right_justify = B_FALSE;
1689 if (pl->pl_prop != ZPROP_INVAL) {
1690 if (zfs_prop_get(zhp, pl->pl_prop, property,
1691 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1696 right_justify = zfs_prop_align_right(pl->pl_prop);
1698 if (nvlist_lookup_nvlist(userprops,
1699 pl->pl_user_prop, &propval) != 0)
1702 verify(nvlist_lookup_string(propval,
1703 ZPROP_VALUE, &propstr) == 0);
1706 width = pl->pl_width;
1709 * If this is being called in scripted mode, or if this is the
1710 * last column and it is left-justified, don't include a width
1713 if (scripted || (pl->pl_next == NULL && !right_justify))
1714 (void) printf("%s", propstr);
1715 else if (right_justify)
1716 (void) printf("%*s", width, propstr);
1718 (void) printf("%-*s", width, propstr);
1721 (void) printf("\n");
1725 * Generic callback function to list a dataset or snapshot.
1728 list_callback(zfs_handle_t *zhp, void *data)
1730 list_cbdata_t *cbp = data;
1732 if (cbp->cb_first) {
1733 if (!cbp->cb_scripted)
1734 print_header(cbp->cb_proplist);
1735 cbp->cb_first = B_FALSE;
1738 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1744 zfs_do_list(int argc, char **argv)
1747 boolean_t scripted = B_FALSE;
1748 static char default_fields[] =
1749 "name,used,available,referenced,mountpoint";
1750 int types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
1751 boolean_t types_specified = B_FALSE;
1752 char *fields = NULL;
1753 list_cbdata_t cb = { 0 };
1756 zfs_sort_column_t *sortcol = NULL;
1757 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
1760 while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
1766 flags |= ZFS_ITER_RECURSE;
1772 if (zfs_add_sort_column(&sortcol, optarg,
1774 (void) fprintf(stderr,
1775 gettext("invalid property '%s'\n"), optarg);
1780 if (zfs_add_sort_column(&sortcol, optarg,
1782 (void) fprintf(stderr,
1783 gettext("invalid property '%s'\n"), optarg);
1789 types_specified = B_TRUE;
1790 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1791 while (*optarg != '\0') {
1792 static char *type_subopts[] = { "filesystem",
1793 "volume", "snapshot", "all", NULL };
1795 switch (getsubopt(&optarg, type_subopts,
1798 types |= ZFS_TYPE_FILESYSTEM;
1801 types |= ZFS_TYPE_VOLUME;
1804 types |= ZFS_TYPE_SNAPSHOT;
1807 types = ZFS_TYPE_DATASET;
1811 (void) fprintf(stderr,
1812 gettext("invalid type '%s'\n"),
1819 (void) fprintf(stderr, gettext("missing argument for "
1820 "'%c' option\n"), optopt);
1824 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1834 fields = default_fields;
1837 * If "-o space" and no types were specified, don't display snapshots.
1839 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
1840 types &= ~ZFS_TYPE_SNAPSHOT;
1843 * If the user specifies '-o all', the zprop_get_list() doesn't
1844 * normally include the name of the dataset. For 'zfs list', we always
1845 * want this property to be first.
1847 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1851 cb.cb_scripted = scripted;
1852 cb.cb_first = B_TRUE;
1854 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
1855 list_callback, &cb);
1857 zprop_free_list(cb.cb_proplist);
1858 zfs_free_sort_columns(sortcol);
1860 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
1861 (void) printf(gettext("no datasets available\n"));
1867 * zfs rename <fs | snap | vol> <fs | snap | vol>
1868 * zfs rename -p <fs | vol> <fs | vol>
1869 * zfs rename -r <snap> <snap>
1871 * Renames the given dataset to another of the same type.
1873 * The '-p' flag creates all the non-existing ancestors of the target first.
1877 zfs_do_rename(int argc, char **argv)
1882 boolean_t recurse = B_FALSE;
1883 boolean_t parents = B_FALSE;
1886 while ((c = getopt(argc, argv, "pr")) != -1) {
1896 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1905 /* check number of arguments */
1907 (void) fprintf(stderr, gettext("missing source dataset "
1912 (void) fprintf(stderr, gettext("missing target dataset "
1917 (void) fprintf(stderr, gettext("too many arguments\n"));
1921 if (recurse && parents) {
1922 (void) fprintf(stderr, gettext("-p and -r options are mutually "
1927 if (recurse && strchr(argv[0], '@') == 0) {
1928 (void) fprintf(stderr, gettext("source dataset for recursive "
1929 "rename must be a snapshot\n"));
1933 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
1934 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
1937 /* If we were asked and the name looks good, try to create ancestors. */
1938 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
1939 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
1944 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
1953 * Promotes the given clone fs to be the parent
1957 zfs_do_promote(int argc, char **argv)
1963 if (argc > 1 && argv[1][0] == '-') {
1964 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1969 /* check number of arguments */
1971 (void) fprintf(stderr, gettext("missing clone filesystem"
1976 (void) fprintf(stderr, gettext("too many arguments\n"));
1980 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1984 ret = (zfs_promote(zhp) != 0);
1992 * zfs rollback [-rRf] <snapshot>
1994 * -r Delete any intervening snapshots before doing rollback
1995 * -R Delete any snapshots and their clones
1996 * -f ignored for backwards compatability
1998 * Given a filesystem, rollback to a specific snapshot, discarding any changes
1999 * since then and making it the active dataset. If more recent snapshots exist,
2000 * the command will complain unless the '-r' flag is given.
2002 typedef struct rollback_cbdata {
2008 boolean_t cb_recurse;
2009 boolean_t cb_dependent;
2010 } rollback_cbdata_t;
2013 * Report any snapshots more recent than the one specified. Used when '-r' is
2014 * not specified. We reuse this same callback for the snapshot dependents - if
2015 * 'cb_dependent' is set, then this is a dependent and we should report it
2016 * without checking the transaction group.
2019 rollback_check(zfs_handle_t *zhp, void *data)
2021 rollback_cbdata_t *cbp = data;
2023 if (cbp->cb_doclones) {
2028 if (!cbp->cb_dependent) {
2029 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2030 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2031 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2034 if (cbp->cb_first && !cbp->cb_recurse) {
2035 (void) fprintf(stderr, gettext("cannot "
2036 "rollback to '%s': more recent snapshots "
2039 (void) fprintf(stderr, gettext("use '-r' to "
2040 "force deletion of the following "
2046 if (cbp->cb_recurse) {
2047 cbp->cb_dependent = B_TRUE;
2048 if (zfs_iter_dependents(zhp, B_TRUE,
2049 rollback_check, cbp) != 0) {
2053 cbp->cb_dependent = B_FALSE;
2055 (void) fprintf(stderr, "%s\n",
2060 if (cbp->cb_first && cbp->cb_recurse) {
2061 (void) fprintf(stderr, gettext("cannot rollback to "
2062 "'%s': clones of previous snapshots exist\n"),
2064 (void) fprintf(stderr, gettext("use '-R' to "
2065 "force deletion of the following clones and "
2071 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2079 zfs_do_rollback(int argc, char **argv)
2083 boolean_t force = B_FALSE;
2084 rollback_cbdata_t cb = { 0 };
2085 zfs_handle_t *zhp, *snap;
2086 char parentname[ZFS_MAXNAMELEN];
2090 while ((c = getopt(argc, argv, "rRf")) != -1) {
2103 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2112 /* check number of arguments */
2114 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2118 (void) fprintf(stderr, gettext("too many arguments\n"));
2122 /* open the snapshot */
2123 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2126 /* open the parent dataset */
2127 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2128 verify((delim = strrchr(parentname, '@')) != NULL);
2130 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2136 * Check for more recent snapshots and/or clones based on the presence
2139 cb.cb_target = argv[0];
2140 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2141 cb.cb_first = B_TRUE;
2143 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2146 if ((ret = cb.cb_error) != 0)
2150 * Rollback parent to the given snapshot.
2152 ret = zfs_rollback(zhp, snap, force);
2165 * zfs set property=value { fs | snap | vol } ...
2167 * Sets the given property for all datasets specified on the command line.
2169 typedef struct set_cbdata {
2175 set_callback(zfs_handle_t *zhp, void *data)
2177 set_cbdata_t *cbp = data;
2179 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2180 switch (libzfs_errno(g_zfs)) {
2181 case EZFS_MOUNTFAILED:
2182 (void) fprintf(stderr, gettext("property may be set "
2183 "but unable to remount filesystem\n"));
2185 case EZFS_SHARENFSFAILED:
2186 (void) fprintf(stderr, gettext("property may be set "
2187 "but unable to reshare filesystem\n"));
2196 zfs_do_set(int argc, char **argv)
2201 /* check for options */
2202 if (argc > 1 && argv[1][0] == '-') {
2203 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2208 /* check number of arguments */
2210 (void) fprintf(stderr, gettext("missing property=value "
2215 (void) fprintf(stderr, gettext("missing dataset name\n"));
2219 /* validate property=value argument */
2220 cb.cb_propname = argv[1];
2221 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2222 (cb.cb_value[1] == '\0')) {
2223 (void) fprintf(stderr, gettext("missing value in "
2224 "property=value argument\n"));
2228 *cb.cb_value = '\0';
2231 if (*cb.cb_propname == '\0') {
2232 (void) fprintf(stderr,
2233 gettext("missing property in property=value argument\n"));
2237 ret = zfs_for_each(argc - 2, argv + 2, NULL,
2238 ZFS_TYPE_DATASET, NULL, NULL, set_callback, &cb);
2244 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2246 * Creates a snapshot with the given name. While functionally equivalent to
2247 * 'zfs create', it is a separate command to differentiate intent.
2250 zfs_do_snapshot(int argc, char **argv)
2252 boolean_t recursive = B_FALSE;
2257 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2258 (void) fprintf(stderr, gettext("internal error: "
2259 "out of memory\n"));
2264 while ((c = getopt(argc, argv, "ro:")) != -1) {
2267 if (parseprop(props))
2274 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2283 /* check number of arguments */
2285 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2289 (void) fprintf(stderr, gettext("too many arguments\n"));
2293 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2295 if (ret && recursive)
2296 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2306 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2307 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2309 * Send a backup stream to stdout.
2312 zfs_do_send(int argc, char **argv)
2314 char *fromname = NULL;
2315 char *toname = NULL;
2318 boolean_t doall = B_FALSE;
2319 boolean_t replicate = B_FALSE;
2320 boolean_t fromorigin = B_FALSE;
2321 boolean_t verbose = B_FALSE;
2325 while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
2345 (void) fprintf(stderr, gettext("missing argument for "
2346 "'%c' option\n"), optopt);
2350 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2359 /* check number of arguments */
2361 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2365 (void) fprintf(stderr, gettext("too many arguments\n"));
2369 if (isatty(STDOUT_FILENO)) {
2370 (void) fprintf(stderr,
2371 gettext("Error: Stream can not be written to a terminal.\n"
2372 "You must redirect standard output.\n"));
2376 cp = strchr(argv[0], '@');
2378 (void) fprintf(stderr,
2379 gettext("argument must be a snapshot\n"));
2384 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2389 * If they specified the full path to the snapshot, chop off
2390 * everything except the short name of the snapshot, but special
2391 * case if they specify the origin.
2393 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2394 char origin[ZFS_MAXNAMELEN];
2397 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2398 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2400 if (strcmp(origin, fromname) == 0) {
2402 fromorigin = B_TRUE;
2405 if (cp != fromname && strcmp(argv[0], fromname)) {
2406 (void) fprintf(stderr,
2407 gettext("incremental source must be "
2408 "in same filesystem\n"));
2412 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2413 (void) fprintf(stderr,
2414 gettext("invalid incremental source\n"));
2420 if (replicate && fromname == NULL)
2423 err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
2424 verbose, STDOUT_FILENO);
2431 * zfs receive [-dnvF] <fs@snap>
2433 * Restore a backup stream from stdin.
2436 zfs_do_receive(int argc, char **argv)
2441 bzero(&flags, sizeof (recvflags_t));
2443 while ((c = getopt(argc, argv, ":dnvF")) != -1) {
2446 flags.isprefix = B_TRUE;
2449 flags.dryrun = B_TRUE;
2452 flags.verbose = B_TRUE;
2455 flags.force = B_TRUE;
2458 (void) fprintf(stderr, gettext("missing argument for "
2459 "'%c' option\n"), optopt);
2463 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2472 /* check number of arguments */
2474 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2478 (void) fprintf(stderr, gettext("too many arguments\n"));
2482 if (isatty(STDIN_FILENO)) {
2483 (void) fprintf(stderr,
2484 gettext("Error: Backup stream can not be read "
2485 "from a terminal.\n"
2486 "You must redirect standard input.\n"));
2490 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2495 typedef struct allow_cb {
2497 size_t a_treeoffset;
2501 zfs_print_perms(avl_tree_t *tree)
2503 zfs_perm_node_t *permnode;
2505 permnode = avl_first(tree);
2506 while (permnode != NULL) {
2507 (void) printf("%s", permnode->z_pname);
2508 permnode = AVL_NEXT(tree, permnode);
2512 (void) printf("\n");
2517 * Iterate over user/groups/everyone/... and the call perm_iter
2518 * function to print actual permission when tree has >0 nodes.
2521 zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb)
2523 zfs_allow_node_t *item;
2526 item = avl_first(tree);
2528 ptree = (void *)((char *)item + cb->a_treeoffset);
2529 if (avl_numnodes(ptree)) {
2530 if (cb->a_permcnt++ == 0)
2531 (void) printf("%s\n", banner);
2532 (void) printf("\t%s", item->z_key);
2534 * Avoid an extra space being printed
2535 * for "everyone" which is keyed with a null
2538 if (item->z_key[0] != '\0')
2540 zfs_print_perms(ptree);
2542 item = AVL_NEXT(tree, item);
2546 #define LINES "-------------------------------------------------------------\n"
2548 zfs_print_allows(char *ds)
2550 zfs_allow_t *curperms, *perms;
2552 allow_cb_t allowcb = { 0 };
2553 char banner[MAXPATHLEN];
2558 if (strrchr(ds, '@')) {
2559 (void) fprintf(stderr, gettext("Snapshots don't have 'allow'"
2563 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2566 if (zfs_perm_get(zhp, &perms)) {
2567 (void) fprintf(stderr,
2568 gettext("Failed to retrieve 'allows' on %s\n"), ds);
2576 (void) printf("%s", LINES);
2577 for (curperms = perms; curperms; curperms = curperms->z_next) {
2579 (void) snprintf(banner, sizeof (banner),
2580 gettext("Permission sets on (%s)"), curperms->z_setpoint);
2581 allowcb.a_treeoffset =
2582 offsetof(zfs_allow_node_t, z_localdescend);
2583 allowcb.a_permcnt = 0;
2584 zfs_iter_perms(&curperms->z_sets, banner, &allowcb);
2586 (void) snprintf(banner, sizeof (banner),
2587 gettext("Create time permissions on (%s)"),
2588 curperms->z_setpoint);
2589 allowcb.a_treeoffset =
2590 offsetof(zfs_allow_node_t, z_localdescend);
2591 allowcb.a_permcnt = 0;
2592 zfs_iter_perms(&curperms->z_crperms, banner, &allowcb);
2595 (void) snprintf(banner, sizeof (banner),
2596 gettext("Local permissions on (%s)"), curperms->z_setpoint);
2597 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local);
2598 allowcb.a_permcnt = 0;
2599 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2600 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2601 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2603 (void) snprintf(banner, sizeof (banner),
2604 gettext("Descendent permissions on (%s)"),
2605 curperms->z_setpoint);
2606 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend);
2607 allowcb.a_permcnt = 0;
2608 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2609 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2610 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2612 (void) snprintf(banner, sizeof (banner),
2613 gettext("Local+Descendent permissions on (%s)"),
2614 curperms->z_setpoint);
2615 allowcb.a_treeoffset =
2616 offsetof(zfs_allow_node_t, z_localdescend);
2617 allowcb.a_permcnt = 0;
2618 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2619 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2620 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2622 (void) printf("%s", LINES);
2624 zfs_free_allows(perms);
2628 #define ALLOWOPTIONS "ldcsu:g:e"
2629 #define UNALLOWOPTIONS "ldcsu:g:er"
2632 * Validate options, and build necessary datastructure to display/remove/add
2634 * Returns 0 - If permissions should be added/removed
2635 * Returns 1 - If permissions should be displayed.
2636 * Returns -1 - on failure
2639 parse_allow_args(int *argc, char **argv[], boolean_t unallow,
2640 char **ds, int *recurse, nvlist_t **zperms)
2643 char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS;
2644 zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE;
2645 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
2650 while ((c = getopt(*argc, *argv, options)) != -1) {
2653 if (who_type == ZFS_DELEG_CREATE ||
2654 who_type == ZFS_DELEG_NAMED_SET)
2657 deleg_type |= ZFS_DELEG_PERM_LOCAL;
2660 if (who_type == ZFS_DELEG_CREATE ||
2661 who_type == ZFS_DELEG_NAMED_SET)
2664 deleg_type |= ZFS_DELEG_PERM_DESCENDENT;
2670 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2674 who_type = ZFS_DELEG_CREATE;
2677 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2681 who_type = ZFS_DELEG_NAMED_SET;
2684 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2686 who_type = ZFS_DELEG_USER;
2690 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2692 who_type = ZFS_DELEG_GROUP;
2696 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2698 who_type = ZFS_DELEG_EVERYONE;
2706 if (deleg_type == 0)
2707 deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT;
2712 if (unallow == B_FALSE && *argc == 1) {
2714 * Only print permissions if no options were processed
2723 * initialize variables for zfs_build_perms based on number
2725 * 3 arguments ==> zfs [un]allow joe perm,perm,perm <dataset> or
2726 * zfs [un]allow -s @set1 perm,perm <dataset>
2727 * 2 arguments ==> zfs [un]allow -c perm,perm <dataset> or
2728 * zfs [un]allow -u|-g <name> perm <dataset> or
2729 * zfs [un]allow -e perm,perm <dataset>
2730 * zfs unallow joe <dataset>
2731 * zfs unallow -s @set1 <dataset>
2732 * 1 argument ==> zfs [un]allow -e <dataset> or
2733 * zfs [un]allow -c <dataset>
2743 * advance argc/argv for do_allow cases.
2744 * for do_allow case make sure who have a know who type
2745 * and its not a permission set.
2747 if (unallow == B_TRUE) {
2750 } else if (who_type != ZFS_DELEG_WHO_UNKNOWN &&
2751 who_type != ZFS_DELEG_NAMED_SET)
2756 if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE ||
2757 who_type == ZFS_DELEG_CREATE || who != NULL)) {
2761 if (unallow == B_FALSE &&
2762 (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2763 who_type == ZFS_DELEG_NAMED_SET))
2765 else if (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2766 who_type == ZFS_DELEG_NAMED_SET)
2768 else if (who_type != ZFS_DELEG_NAMED_SET)
2772 if (unallow == B_TRUE) {
2779 if (unallow == B_FALSE)
2781 if (who == NULL && who_type != ZFS_DELEG_CREATE &&
2782 who_type != ZFS_DELEG_EVERYONE)
2791 if (strrchr(*ds, '@')) {
2792 (void) fprintf(stderr,
2793 gettext("Can't set or remove 'allow' permissions "
2794 "on snapshots.\n"));
2798 if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_DATASET)) == NULL)
2801 if ((zfs_build_perms(zhp, who, perms,
2802 who_type, deleg_type, zperms)) != 0) {
2811 zfs_do_allow(int argc, char **argv)
2814 nvlist_t *zperms = NULL;
2819 if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds,
2820 &unused, &zperms)) == -1)
2824 return (zfs_print_allows(argv[0]));
2826 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2829 if (zfs_perm_set(zhp, zperms)) {
2831 nvlist_free(zperms);
2834 nvlist_free(zperms);
2841 unallow_callback(zfs_handle_t *zhp, void *data)
2843 nvlist_t *nvp = (nvlist_t *)data;
2846 error = zfs_perm_remove(zhp, nvp);
2848 (void) fprintf(stderr, gettext("Failed to remove permissions "
2849 "on %s\n"), zfs_get_name(zhp));
2855 zfs_do_unallow(int argc, char **argv)
2857 int recurse = B_FALSE;
2860 nvlist_t *zperms = NULL;
2863 if (parse_allow_args(&argc, &argv, B_TRUE,
2864 &ds, &recurse, &zperms) == -1)
2868 flags |= ZFS_ITER_RECURSE;
2869 error = zfs_for_each(argc, argv, flags,
2870 ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
2871 NULL, unallow_callback, (void *)zperms);
2874 nvlist_free(zperms);
2879 typedef struct get_all_cbdata {
2880 zfs_handle_t **cb_handles;
2884 boolean_t cb_verbose;
2887 #define CHECK_SPINNER 30
2888 #define SPINNER_TIME 3 /* seconds */
2889 #define MOUNT_TIME 5 /* seconds */
2892 get_one_dataset(zfs_handle_t *zhp, void *data)
2894 static char spin[] = { '-', '\\', '|', '/' };
2895 static int spinval = 0;
2896 static int spincheck = 0;
2897 static time_t last_spin_time = (time_t)0;
2898 get_all_cbdata_t *cbp = data;
2899 zfs_type_t type = zfs_get_type(zhp);
2901 if (cbp->cb_verbose) {
2902 if (--spincheck < 0) {
2903 time_t now = time(NULL);
2904 if (last_spin_time + SPINNER_TIME < now) {
2905 (void) printf("\b%c", spin[spinval++ % 4]);
2906 (void) fflush(stdout);
2907 last_spin_time = now;
2909 spincheck = CHECK_SPINNER;
2914 * Interate over any nested datasets.
2916 if (type == ZFS_TYPE_FILESYSTEM &&
2917 zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2923 * Skip any datasets whose type does not match.
2925 if ((type & cbp->cb_types) == 0) {
2930 if (cbp->cb_alloc == cbp->cb_used) {
2931 zfs_handle_t **handles;
2933 if (cbp->cb_alloc == 0)
2938 handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2940 if (cbp->cb_handles) {
2941 bcopy(cbp->cb_handles, handles,
2942 cbp->cb_used * sizeof (void *));
2943 free(cbp->cb_handles);
2946 cbp->cb_handles = handles;
2949 cbp->cb_handles[cbp->cb_used++] = zhp;
2955 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
2958 get_all_cbdata_t cb = { 0 };
2959 cb.cb_types = types;
2960 cb.cb_verbose = verbose;
2963 (void) printf("%s: *", gettext("Reading ZFS config"));
2964 (void) fflush(stdout);
2967 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2969 *dslist = cb.cb_handles;
2970 *count = cb.cb_used;
2973 (void) printf("\b%s\n", gettext("done."));
2978 dataset_cmp(const void *a, const void *b)
2980 zfs_handle_t **za = (zfs_handle_t **)a;
2981 zfs_handle_t **zb = (zfs_handle_t **)b;
2982 char mounta[MAXPATHLEN];
2983 char mountb[MAXPATHLEN];
2984 boolean_t gota, gotb;
2986 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
2987 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2988 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2989 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
2990 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2991 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2994 return (strcmp(mounta, mountb));
3001 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
3005 * Generic callback for sharing or mounting filesystems. Because the code is so
3006 * similar, we have a common function with an extra parameter to determine which
3007 * mode we are using.
3009 #define OP_SHARE 0x1
3010 #define OP_MOUNT 0x2
3013 * Share or mount a dataset.
3016 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3017 boolean_t explicit, const char *options)
3019 char mountpoint[ZFS_MAXPROPLEN];
3020 char shareopts[ZFS_MAXPROPLEN];
3021 char smbshareopts[ZFS_MAXPROPLEN];
3022 const char *cmdname = op == OP_SHARE ? "share" : "mount";
3024 uint64_t zoned, canmount;
3025 zfs_type_t type = zfs_get_type(zhp);
3026 boolean_t shared_nfs, shared_smb;
3028 assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
3030 if (type == ZFS_TYPE_FILESYSTEM) {
3032 * Check to make sure we can mount/share this dataset. If we
3033 * are in the global zone and the filesystem is exported to a
3034 * local zone, or if we are in a local zone and the
3035 * filesystem is not exported, then it is an error.
3037 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3039 if (zoned && getzoneid() == GLOBAL_ZONEID) {
3043 (void) fprintf(stderr, gettext("cannot %s '%s': "
3044 "dataset is exported to a local zone\n"), cmdname,
3048 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3052 (void) fprintf(stderr, gettext("cannot %s '%s': "
3053 "permission denied\n"), cmdname,
3059 * Ignore any filesystems which don't apply to us. This
3060 * includes those with a legacy mountpoint, or those with
3061 * legacy share options.
3063 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3064 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3065 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3066 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3067 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3068 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3070 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3071 strcmp(smbshareopts, "off") == 0) {
3075 (void) fprintf(stderr, gettext("cannot share '%s': "
3076 "legacy share\n"), zfs_get_name(zhp));
3077 (void) fprintf(stderr, gettext("use share(1M) to "
3078 "share this filesystem, or set "
3079 "sharenfs property on\n"));
3084 * We cannot share or mount legacy filesystems. If the
3085 * shareopts is non-legacy but the mountpoint is legacy, we
3086 * treat it as a legacy share.
3088 if (strcmp(mountpoint, "legacy") == 0) {
3092 (void) fprintf(stderr, gettext("cannot %s '%s': "
3093 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3094 (void) fprintf(stderr, gettext("use %s(1M) to "
3095 "%s this filesystem\n"), cmdname, cmdname);
3099 if (strcmp(mountpoint, "none") == 0) {
3103 (void) fprintf(stderr, gettext("cannot %s '%s': no "
3104 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3109 * canmount explicit outcome
3110 * on no pass through
3111 * on yes pass through
3113 * off yes display error, return 1
3114 * noauto no return 0
3115 * noauto yes pass through
3117 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3118 if (canmount == ZFS_CANMOUNT_OFF) {
3122 (void) fprintf(stderr, gettext("cannot %s '%s': "
3123 "'canmount' property is set to 'off'\n"), cmdname,
3126 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3131 * At this point, we have verified that the mountpoint and/or
3132 * shareopts are appropriate for auto management. If the
3133 * filesystem is already mounted or shared, return (failing
3134 * for explicit requests); otherwise mount or share the
3140 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3141 shared_smb = zfs_is_shared_smb(zhp, NULL);
3143 if (shared_nfs && shared_smb ||
3144 (shared_nfs && strcmp(shareopts, "on") == 0 &&
3145 strcmp(smbshareopts, "off") == 0) ||
3146 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3147 strcmp(shareopts, "off") == 0)) {
3151 (void) fprintf(stderr, gettext("cannot share "
3152 "'%s': filesystem already shared\n"),
3157 if (!zfs_is_mounted(zhp, NULL) &&
3158 zfs_mount(zhp, NULL, 0) != 0)
3161 if (protocol == NULL) {
3162 if (zfs_shareall(zhp) != 0)
3164 } else if (strcmp(protocol, "nfs") == 0) {
3165 if (zfs_share_nfs(zhp))
3167 } else if (strcmp(protocol, "smb") == 0) {
3168 if (zfs_share_smb(zhp))
3171 (void) fprintf(stderr, gettext("cannot share "
3172 "'%s': invalid share type '%s' "
3174 zfs_get_name(zhp), protocol);
3181 if (options == NULL)
3182 mnt.mnt_mntopts = "";
3184 mnt.mnt_mntopts = (char *)options;
3186 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3187 zfs_is_mounted(zhp, NULL)) {
3191 (void) fprintf(stderr, gettext("cannot mount "
3192 "'%s': filesystem already mounted\n"),
3197 if (zfs_mount(zhp, options, flags) != 0)
3202 assert(op == OP_SHARE);
3205 * Ignore any volumes that aren't shared.
3207 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
3208 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3210 if (strcmp(shareopts, "off") == 0) {
3214 (void) fprintf(stderr, gettext("cannot share '%s': "
3215 "'shareiscsi' property not set\n"),
3217 (void) fprintf(stderr, gettext("set 'shareiscsi' "
3218 "property or use iscsitadm(1M) to share this "
3223 if (zfs_is_shared_iscsi(zhp)) {
3227 (void) fprintf(stderr, gettext("cannot share "
3228 "'%s': volume already shared\n"),
3233 if (zfs_share_iscsi(zhp) != 0)
3241 * Reports progress in the form "(current/total)". Not thread-safe.
3244 report_mount_progress(int current, int total)
3247 static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3248 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3249 static time_t last_progress_time;
3250 time_t now = time(NULL);
3252 /* report 1..n instead of 0..n-1 */
3255 /* display header if we're here for the first time */
3257 (void) printf(gettext("Mounting ZFS filesystems: "));
3259 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3260 /* too soon to report again */
3264 last_progress_time = now;
3266 /* back up to prepare for overwriting */
3268 (void) printf("%*.*s", len, len, reverse);
3270 /* We put a newline at the end if this is the last one. */
3271 len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
3272 (void) fflush(stdout);
3276 append_options(char *mntopts, char *newopts)
3278 int len = strlen(mntopts);
3280 /* original length plus new string to append plus 1 for the comma */
3281 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3282 (void) fprintf(stderr, gettext("the opts argument for "
3283 "'%c' option is too long (more than %d chars)\n"),
3284 "-o", MNT_LINE_MAX);
3289 mntopts[len++] = ',';
3291 (void) strcpy(&mntopts[len], newopts);
3295 share_mount(int op, int argc, char **argv)
3298 boolean_t verbose = B_FALSE;
3300 char *options = NULL;
3301 int types, flags = 0;
3304 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3314 if (*optarg == '\0') {
3315 (void) fprintf(stderr, gettext("empty mount "
3316 "options (-o) specified\n"));
3320 if (options == NULL)
3321 options = safe_malloc(MNT_LINE_MAX + 1);
3323 /* option validation is done later */
3324 append_options(options, optarg);
3328 flags |= MS_OVERLAY;
3331 (void) fprintf(stderr, gettext("missing argument for "
3332 "'%c' option\n"), optopt);
3336 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3345 /* check number of arguments */
3347 zfs_handle_t **dslist = NULL;
3348 size_t i, count = 0;
3349 char *protocol = NULL;
3351 if (op == OP_MOUNT) {
3352 types = ZFS_TYPE_FILESYSTEM;
3353 } else if (argc > 0) {
3354 if (strcmp(argv[0], "nfs") == 0 ||
3355 strcmp(argv[0], "smb") == 0) {
3356 types = ZFS_TYPE_FILESYSTEM;
3357 } else if (strcmp(argv[0], "iscsi") == 0) {
3358 types = ZFS_TYPE_VOLUME;
3360 (void) fprintf(stderr, gettext("share type "
3361 "must be 'nfs', 'smb' or 'iscsi'\n"));
3368 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3372 (void) fprintf(stderr, gettext("too many arguments\n"));
3376 get_all_datasets(types, &dslist, &count, verbose);
3381 qsort(dslist, count, sizeof (void *), dataset_cmp);
3383 for (i = 0; i < count; i++) {
3385 report_mount_progress(i, count);
3387 if (share_mount_one(dslist[i], op, flags, protocol,
3388 B_FALSE, options) != 0)
3390 zfs_close(dslist[i]);
3394 } else if (argc == 0) {
3395 struct mnttab entry;
3397 if ((op == OP_SHARE) || (options != NULL)) {
3398 (void) fprintf(stderr, gettext("missing filesystem "
3399 "argument (specify -a for all)\n"));
3404 * When mount is given no arguments, go through /etc/mnttab and
3405 * display any active ZFS mounts. We hide any snapshots, since
3406 * they are controlled automatically.
3408 rewind(mnttab_file);
3409 while (getmntent(mnttab_file, &entry) == 0) {
3410 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3411 strchr(entry.mnt_special, '@') != NULL)
3414 (void) printf("%-30s %s\n", entry.mnt_special,
3421 types = ZFS_TYPE_FILESYSTEM;
3423 types |= ZFS_TYPE_VOLUME;
3426 (void) fprintf(stderr,
3427 gettext("too many arguments\n"));
3431 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3434 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3444 * zfs mount -a [nfs | iscsi]
3445 * zfs mount filesystem
3447 * Mount all filesystems, or mount the given filesystem.
3450 zfs_do_mount(int argc, char **argv)
3452 return (share_mount(OP_MOUNT, argc, argv));
3456 * zfs share -a [nfs | iscsi | smb]
3457 * zfs share filesystem
3459 * Share all filesystems, or share the given filesystem.
3462 zfs_do_share(int argc, char **argv)
3464 return (share_mount(OP_SHARE, argc, argv));
3467 typedef struct unshare_unmount_node {
3468 zfs_handle_t *un_zhp;
3470 uu_avl_node_t un_avlnode;
3471 } unshare_unmount_node_t;
3475 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3477 const unshare_unmount_node_t *l = larg;
3478 const unshare_unmount_node_t *r = rarg;
3480 return (strcmp(l->un_mountp, r->un_mountp));
3484 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3485 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3486 * and unmount it appropriately.
3489 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3493 struct stat64 statbuf;
3494 struct extmnttab entry;
3495 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3499 * Search for the path in /etc/mnttab. Rather than looking for the
3500 * specific path, which can be fooled by non-standard paths (i.e. ".."
3501 * or "//"), we stat() the path and search for the corresponding
3502 * (major,minor) device pair.
3504 if (stat64(path, &statbuf) != 0) {
3505 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3506 cmdname, path, strerror(errno));
3509 path_inode = statbuf.st_ino;
3512 * Search for the given (major,minor) pair in the mount table.
3514 rewind(mnttab_file);
3515 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3516 if (entry.mnt_major == major(statbuf.st_dev) &&
3517 entry.mnt_minor == minor(statbuf.st_dev))
3521 if (op == OP_SHARE) {
3522 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3523 "currently mounted\n"), cmdname, path);
3526 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3528 if ((ret = umount2(path, flags)) != 0)
3529 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3534 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3535 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3536 "filesystem\n"), cmdname, path);
3540 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3541 ZFS_TYPE_FILESYSTEM)) == NULL)
3545 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3546 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3547 cmdname, path, strerror(errno));
3549 } else if (statbuf.st_ino != path_inode) {
3550 (void) fprintf(stderr, gettext("cannot "
3551 "%s '%s': not a mountpoint\n"), cmdname, path);
3555 if (op == OP_SHARE) {
3556 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3557 char smbshare_prop[ZFS_MAXPROPLEN];
3559 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3560 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3561 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3562 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3564 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3565 strcmp(smbshare_prop, "off") == 0) {
3566 (void) fprintf(stderr, gettext("cannot unshare "
3567 "'%s': legacy share\n"), path);
3568 (void) fprintf(stderr, gettext("use "
3569 "unshare(1M) to unshare this filesystem\n"));
3570 } else if (!zfs_is_shared(zhp)) {
3571 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3572 "not currently shared\n"), path);
3574 ret = zfs_unshareall_bypath(zhp, path);
3577 char mtpt_prop[ZFS_MAXPROPLEN];
3579 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3580 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3583 ret = zfs_unmount(zhp, NULL, flags);
3584 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3585 (void) fprintf(stderr, gettext("cannot unmount "
3586 "'%s': legacy mountpoint\n"),
3588 (void) fprintf(stderr, gettext("use umount(1M) "
3589 "to unmount this filesystem\n"));
3591 ret = zfs_unmountall(zhp, flags);
3602 * Generic callback for unsharing or unmounting a filesystem.
3605 unshare_unmount(int op, int argc, char **argv)
3612 char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3613 char sharesmb[ZFS_MAXPROPLEN];
3616 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3625 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3636 * We could make use of zfs_for_each() to walk all datasets in
3637 * the system, but this would be very inefficient, especially
3638 * since we would have to linearly search /etc/mnttab for each
3639 * one. Instead, do one pass through /etc/mnttab looking for
3640 * zfs entries and call zfs_unmount() for each one.
3642 * Things get a little tricky if the administrator has created
3643 * mountpoints beneath other ZFS filesystems. In this case, we
3644 * have to unmount the deepest filesystems first. To accomplish
3645 * this, we place all the mountpoints in an AVL tree sorted by
3646 * the special type (dataset name), and walk the result in
3647 * reverse to make sure to get any snapshots first.
3649 struct mnttab entry;
3650 uu_avl_pool_t *pool;
3652 unshare_unmount_node_t *node;
3654 uu_avl_walk_t *walk;
3657 (void) fprintf(stderr, gettext("too many arguments\n"));
3661 if ((pool = uu_avl_pool_create("unmount_pool",
3662 sizeof (unshare_unmount_node_t),
3663 offsetof(unshare_unmount_node_t, un_avlnode),
3664 unshare_unmount_compare,
3665 UU_DEFAULT)) == NULL) {
3666 (void) fprintf(stderr, gettext("internal error: "
3667 "out of memory\n"));
3671 if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3672 (void) fprintf(stderr, gettext("internal error: "
3673 "out of memory\n"));
3677 rewind(mnttab_file);
3678 while (getmntent(mnttab_file, &entry) == 0) {
3680 /* ignore non-ZFS entries */
3681 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3684 /* ignore snapshots */
3685 if (strchr(entry.mnt_special, '@') != NULL)
3688 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3689 ZFS_TYPE_FILESYSTEM)) == NULL) {
3696 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3698 sizeof (nfsiscsi_mnt_prop),
3699 NULL, NULL, 0, B_FALSE) == 0);
3700 if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3702 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3704 sizeof (nfsiscsi_mnt_prop),
3705 NULL, NULL, 0, B_FALSE) == 0);
3706 if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3710 /* Ignore legacy mounts */
3711 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3713 sizeof (nfsiscsi_mnt_prop),
3714 NULL, NULL, 0, B_FALSE) == 0);
3715 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3717 /* Ignore canmount=noauto mounts */
3718 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3719 ZFS_CANMOUNT_NOAUTO)
3725 node = safe_malloc(sizeof (unshare_unmount_node_t));
3728 if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
3730 (void) fprintf(stderr, gettext("internal error:"
3731 " out of memory\n"));
3735 uu_avl_node_init(node, &node->un_avlnode, pool);
3737 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3738 uu_avl_insert(tree, node, idx);
3740 zfs_close(node->un_zhp);
3741 free(node->un_mountp);
3747 * Walk the AVL tree in reverse, unmounting each filesystem and
3748 * removing it from the AVL tree in the process.
3750 if ((walk = uu_avl_walk_start(tree,
3751 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3752 (void) fprintf(stderr,
3753 gettext("internal error: out of memory"));
3757 while ((node = uu_avl_walk_next(walk)) != NULL) {
3758 uu_avl_remove(tree, node);
3762 if (zfs_unshareall_bypath(node->un_zhp,
3763 node->un_mountp) != 0)
3768 if (zfs_unmount(node->un_zhp,
3769 node->un_mountp, flags) != 0)
3774 zfs_close(node->un_zhp);
3775 free(node->un_mountp);
3779 uu_avl_walk_end(walk);
3780 uu_avl_destroy(tree);
3781 uu_avl_pool_destroy(pool);
3783 if (op == OP_SHARE) {
3785 * Finally, unshare any volumes shared via iSCSI.
3787 zfs_handle_t **dslist = NULL;
3788 size_t i, count = 0;
3790 get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
3794 qsort(dslist, count, sizeof (void *),
3797 for (i = 0; i < count; i++) {
3798 if (zfs_unshare_iscsi(dslist[i]) != 0)
3800 zfs_close(dslist[i]);
3809 (void) fprintf(stderr,
3810 gettext("missing filesystem argument\n"));
3812 (void) fprintf(stderr,
3813 gettext("too many arguments\n"));
3818 * We have an argument, but it may be a full path or a ZFS
3819 * filesystem. Pass full paths off to unmount_path() (shared by
3820 * manual_unmount), otherwise open the filesystem and pass to
3823 if (argv[0][0] == '/')
3824 return (unshare_unmount_path(op, argv[0],
3827 types = ZFS_TYPE_FILESYSTEM;
3829 types |= ZFS_TYPE_VOLUME;
3831 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3834 if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
3835 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3836 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3837 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3838 NULL, 0, B_FALSE) == 0);
3842 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3844 sizeof (nfsiscsi_mnt_prop),
3845 NULL, NULL, 0, B_FALSE) == 0);
3846 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3847 sharesmb, sizeof (sharesmb), NULL, NULL,
3850 if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3851 strcmp(sharesmb, "off") == 0) {
3852 (void) fprintf(stderr, gettext("cannot "
3853 "unshare '%s': legacy share\n"),
3855 (void) fprintf(stderr, gettext("use "
3856 "unshare(1M) to unshare this "
3859 } else if (!zfs_is_shared(zhp)) {
3860 (void) fprintf(stderr, gettext("cannot "
3861 "unshare '%s': not currently "
3862 "shared\n"), zfs_get_name(zhp));
3864 } else if (zfs_unshareall(zhp) != 0) {
3870 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3871 (void) fprintf(stderr, gettext("cannot "
3872 "unmount '%s': legacy "
3873 "mountpoint\n"), zfs_get_name(zhp));
3874 (void) fprintf(stderr, gettext("use "
3875 "umount(1M) to unmount this "
3878 } else if (!zfs_is_mounted(zhp, NULL)) {
3879 (void) fprintf(stderr, gettext("cannot "
3880 "unmount '%s': not currently "
3884 } else if (zfs_unmountall(zhp, flags) != 0) {
3890 assert(op == OP_SHARE);
3892 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3893 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3894 NULL, NULL, 0, B_FALSE) == 0);
3896 if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
3897 (void) fprintf(stderr, gettext("cannot unshare "
3898 "'%s': 'shareiscsi' property not set\n"),
3900 (void) fprintf(stderr, gettext("set "
3901 "'shareiscsi' property or use "
3902 "iscsitadm(1M) to share this volume\n"));
3904 } else if (!zfs_is_shared_iscsi(zhp)) {
3905 (void) fprintf(stderr, gettext("cannot "
3906 "unshare '%s': not currently shared\n"),
3909 } else if (zfs_unshare_iscsi(zhp) != 0) {
3922 * zfs unmount filesystem
3924 * Unmount all filesystems, or a specific ZFS filesystem.
3927 zfs_do_unmount(int argc, char **argv)
3929 return (unshare_unmount(OP_MOUNT, argc, argv));
3934 * zfs unshare filesystem
3936 * Unshare all filesystems, or a specific ZFS filesystem.
3939 zfs_do_unshare(int argc, char **argv)
3941 return (unshare_unmount(OP_SHARE, argc, argv));
3945 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
3946 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
3949 manual_mount(int argc, char **argv)
3952 char mountpoint[ZFS_MAXPROPLEN];
3953 char mntopts[MNT_LINE_MAX] = { '\0' };
3957 char *dataset, *path;
3960 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3963 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
3966 flags |= MS_OVERLAY;
3969 flags |= MS_NOMNTTAB;
3972 (void) fprintf(stderr, gettext("missing argument for "
3973 "'%c' option\n"), optopt);
3977 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3979 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
3988 /* check that we only have two arguments */
3991 (void) fprintf(stderr, gettext("missing dataset "
3994 (void) fprintf(stderr,
3995 gettext("missing mountpoint argument\n"));
3997 (void) fprintf(stderr, gettext("too many arguments\n"));
3998 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
4005 /* try to open the dataset */
4006 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
4009 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
4010 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
4012 /* check for legacy mountpoint and complain appropriately */
4014 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
4015 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
4016 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
4017 (void) fprintf(stderr, gettext("mount failed: %s\n"),
4022 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
4023 "mounted using 'mount -F zfs'\n"), dataset);
4024 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
4025 "instead.\n"), path);
4026 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
4027 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
4028 (void) fprintf(stderr, gettext("See zfs(1M) for more "
4037 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
4038 * unmounts of non-legacy filesystems, as this is the dominant administrative
4042 manual_unmount(int argc, char **argv)
4048 while ((c = getopt(argc, argv, "f")) != -1) {
4054 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4056 (void) fprintf(stderr, gettext("usage: unmount [-f] "
4065 /* check arguments */
4068 (void) fprintf(stderr, gettext("missing path "
4071 (void) fprintf(stderr, gettext("too many arguments\n"));
4072 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
4076 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
4080 volcheck(zpool_handle_t *zhp, void *data)
4082 boolean_t isinit = *((boolean_t *)data);
4085 return (zpool_create_zvol_links(zhp));
4087 return (zpool_remove_zvol_links(zhp));
4091 * Iterate over all pools in the system and either create or destroy /dev/zvol
4092 * links, depending on the value of 'isinit'.
4095 do_volcheck(boolean_t isinit)
4097 return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
4101 find_command_idx(char *command, int *idx)
4105 for (i = 0; i < NCOMMAND; i++) {
4106 if (command_table[i].name == NULL)
4109 if (strcmp(command, command_table[i].name) == 0) {
4118 main(int argc, char **argv)
4125 (void) setlocale(LC_ALL, "");
4126 (void) textdomain(TEXT_DOMAIN);
4130 if ((g_zfs = libzfs_init()) == NULL) {
4131 (void) fprintf(stderr, gettext("internal error: failed to "
4132 "initialize ZFS library\n"));
4136 zpool_set_history_str("zfs", argc, argv, history_str);
4137 verify(zpool_stage_history(g_zfs, history_str) == 0);
4139 libzfs_print_on_error(g_zfs, B_TRUE);
4141 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4142 (void) fprintf(stderr, gettext("internal error: unable to "
4143 "open %s\n"), MNTTAB);
4148 * This command also doubles as the /etc/fs mount and unmount program.
4149 * Determine if we should take this behavior based on argv[0].
4151 progname = basename(argv[0]);
4152 if (strcmp(progname, "mount") == 0) {
4153 ret = manual_mount(argc, argv);
4154 } else if (strcmp(progname, "umount") == 0) {
4155 ret = manual_unmount(argc, argv);
4158 * Make sure the user has specified some command.
4161 (void) fprintf(stderr, gettext("missing command\n"));
4168 * The 'umount' command is an alias for 'unmount'
4170 if (strcmp(cmdname, "umount") == 0)
4171 cmdname = "unmount";
4174 * The 'recv' command is an alias for 'receive'
4176 if (strcmp(cmdname, "recv") == 0)
4177 cmdname = "receive";
4182 if (strcmp(cmdname, "-?") == 0)
4186 * 'volinit' and 'volfini' do not appear in the usage message,
4187 * so we have to special case them here.
4189 if (strcmp(cmdname, "volinit") == 0)
4190 return (do_volcheck(B_TRUE));
4191 else if (strcmp(cmdname, "volfini") == 0)
4192 return (do_volcheck(B_FALSE));
4195 * Run the appropriate command.
4197 if (find_command_idx(cmdname, &i) == 0) {
4198 current_command = &command_table[i];
4199 ret = command_table[i].func(argc - 1, argv + 1);
4200 } else if (strchr(cmdname, '=') != NULL) {
4201 verify(find_command_idx("set", &i) == 0);
4202 current_command = &command_table[i];
4203 ret = command_table[i].func(argc, argv);
4205 (void) fprintf(stderr, gettext("unrecognized "
4206 "command '%s'\n"), cmdname);
4211 (void) fclose(mnttab_file);
4216 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4217 * for the purposes of running ::findleaks.
4219 if (getenv("ZFS_ABORT") != NULL) {
4220 (void) printf("dumping core by request\n");