4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
33 #include <libnvpair.h>
44 #include <sys/mntent.h>
45 #include <sys/mnttab.h>
46 #include <sys/mount.h>
48 #include <sys/fs/zfs.h>
56 libzfs_handle_t *g_zfs;
58 static FILE *mnttab_file;
59 static char history_str[HIS_MAX_RECORD_LEN];
60 const char *pypath = "/usr/lib/zfs/pyzfs.py";
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_userspace(int argc, char **argv);
81 static int zfs_do_python(int argc, char **argv);
82 static int zfs_do_jail(int argc, char **argv);
83 static int zfs_do_unjail(int argc, char **argv);
86 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
91 _umem_debug_init(void)
93 return ("default,verbose"); /* $UMEM_DEBUG setting */
97 _umem_logging_init(void)
99 return ("fail,contents"); /* $UMEM_LOGGING setting */
130 typedef struct zfs_command {
132 int (*func)(int argc, char **argv);
137 * Master command table. Each ZFS command has a name, associated function, and
138 * usage message. The usage messages need to be internationalized, so we have
139 * to have a function to return the usage message based on a command index.
141 * These commands are organized according to how they are displayed in the usage
142 * message. An empty command (one with a NULL name) indicates an empty line in
143 * the generic usage message.
145 static zfs_command_t command_table[] = {
146 { "create", zfs_do_create, HELP_CREATE },
147 { "destroy", zfs_do_destroy, HELP_DESTROY },
149 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
150 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
151 { "clone", zfs_do_clone, HELP_CLONE },
152 { "promote", zfs_do_promote, HELP_PROMOTE },
153 { "rename", zfs_do_rename, HELP_RENAME },
155 { "list", zfs_do_list, HELP_LIST },
157 { "set", zfs_do_set, HELP_SET },
158 { "get", zfs_do_get, HELP_GET },
159 { "inherit", zfs_do_inherit, HELP_INHERIT },
160 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
161 { "userspace", zfs_do_userspace, HELP_USERSPACE },
162 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
164 { "mount", zfs_do_mount, HELP_MOUNT },
165 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
166 { "share", zfs_do_share, HELP_SHARE },
167 { "unshare", zfs_do_unshare, HELP_UNSHARE },
169 { "send", zfs_do_send, HELP_SEND },
170 { "receive", zfs_do_receive, HELP_RECEIVE },
172 { "allow", zfs_do_python, HELP_ALLOW },
174 { "unallow", zfs_do_python, HELP_UNALLOW },
176 { "jail", zfs_do_jail, HELP_JAIL },
177 { "unjail", zfs_do_unjail, HELP_UNJAIL },
180 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
182 zfs_command_t *current_command;
185 get_usage(zfs_help_t idx)
189 return (gettext("\tclone [-p] [-o property=value] ... "
190 "<snapshot> <filesystem|volume>\n"));
192 return (gettext("\tcreate [-p] [-o property=value] ... "
194 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
195 "-V <size> <volume>\n"));
197 return (gettext("\tdestroy [-rRf] "
198 "<filesystem|volume|snapshot>\n"));
200 return (gettext("\tget [-rHp] [-d max] "
201 "[-o field[,...]] [-s source[,...]]\n"
202 "\t <\"all\" | property[,...]> "
203 "[filesystem|volume|snapshot] ...\n"));
205 return (gettext("\tinherit [-r] <property> "
206 "<filesystem|volume|snapshot> ...\n"));
208 return (gettext("\tupgrade [-v]\n"
209 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
211 return (gettext("\tjail <jailid> <filesystem>\n"));
213 return (gettext("\tunjail <jailid> <filesystem>\n"));
215 return (gettext("\tlist [-rH][-d max] "
216 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
217 "\t [-S property] ... "
218 "[filesystem|volume|snapshot] ...\n"));
220 return (gettext("\tmount\n"
221 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
223 return (gettext("\tpromote <clone-filesystem>\n"));
225 return (gettext("\treceive [-vnF] <filesystem|volume|"
227 "\treceive [-vnF] -d <filesystem>\n"));
229 return (gettext("\trename <filesystem|volume|snapshot> "
230 "<filesystem|volume|snapshot>\n"
231 "\trename -p <filesystem|volume> <filesystem|volume>\n"
232 "\trename -r <snapshot> <snapshot>"));
234 return (gettext("\trollback [-rRf] <snapshot>\n"));
236 return (gettext("\tsend [-R] [-[iI] snapshot] <snapshot>\n"));
238 return (gettext("\tset <property=value> "
239 "<filesystem|volume|snapshot> ...\n"));
241 return (gettext("\tshare <-a | filesystem>\n"));
243 return (gettext("\tsnapshot [-r] [-o property=value] ... "
244 "<filesystem@snapname|volume@snapname>\n"));
246 return (gettext("\tunmount [-f] "
247 "<-a | filesystem|mountpoint>\n"));
249 return (gettext("\tunshare [-f] "
250 "<-a | filesystem|mountpoint>\n"));
252 return (gettext("\tallow [-ldug] "
253 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
254 "\t <filesystem|volume>\n"
255 "\tallow [-ld] -e <perm|@setname>[,...] "
256 "<filesystem|volume>\n"
257 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
258 "\tallow -s @setname <perm|@setname>[,...] "
259 "<filesystem|volume>\n"));
261 return (gettext("\tunallow [-rldug] "
262 "<\"everyone\"|user|group>[,...]\n"
263 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
264 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
265 "<filesystem|volume>\n"
266 "\tunallow [-r] -c [<perm|@setname>[,...]] "
267 "<filesystem|volume>\n"
268 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
269 "<filesystem|volume>\n"));
271 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
272 "[-sS field] ... [-t type[,...]]\n"
273 "\t <filesystem|snapshot>\n"));
274 case HELP_GROUPSPACE:
275 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
276 "[-sS field] ... [-t type[,...]]\n"
277 "\t <filesystem|snapshot>\n"));
285 * Utility function to guarantee malloc() success.
288 safe_malloc(size_t size)
292 if ((data = calloc(1, size)) == NULL) {
293 (void) fprintf(stderr, "internal error: out of memory\n");
301 * Callback routine that will print out information for each of
305 usage_prop_cb(int prop, void *cb)
309 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
311 if (zfs_prop_readonly(prop))
312 (void) fprintf(fp, " NO ");
314 (void) fprintf(fp, "YES ");
316 if (zfs_prop_inheritable(prop))
317 (void) fprintf(fp, " YES ");
319 (void) fprintf(fp, " NO ");
321 if (zfs_prop_values(prop) == NULL)
322 (void) fprintf(fp, "-\n");
324 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
330 * Display usage message. If we're inside a command, display only the usage for
331 * that command. Otherwise, iterate over the entire command table and display
332 * a complete usage message.
335 usage(boolean_t requested)
338 boolean_t show_properties = B_FALSE;
339 FILE *fp = requested ? stdout : stderr;
341 if (current_command == NULL) {
343 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
345 gettext("where 'command' is one of the following:\n\n"));
347 for (i = 0; i < NCOMMAND; i++) {
348 if (command_table[i].name == NULL)
349 (void) fprintf(fp, "\n");
351 (void) fprintf(fp, "%s",
352 get_usage(command_table[i].usage));
355 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
356 "pool/[dataset/]*dataset[@name]\n"));
358 (void) fprintf(fp, gettext("usage:\n"));
359 (void) fprintf(fp, "%s", get_usage(current_command->usage));
362 if (current_command != NULL &&
363 (strcmp(current_command->name, "set") == 0 ||
364 strcmp(current_command->name, "get") == 0 ||
365 strcmp(current_command->name, "inherit") == 0 ||
366 strcmp(current_command->name, "list") == 0))
367 show_properties = B_TRUE;
369 if (show_properties) {
371 gettext("\nThe following properties are supported:\n"));
373 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
374 "PROPERTY", "EDIT", "INHERIT", "VALUES");
376 /* Iterate over all properties */
377 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
380 (void) fprintf(fp, "\t%-15s ", "userused@...");
381 (void) fprintf(fp, " NO NO <size>\n");
382 (void) fprintf(fp, "\t%-15s ", "groupused@...");
383 (void) fprintf(fp, " NO NO <size>\n");
384 (void) fprintf(fp, "\t%-15s ", "userquota@...");
385 (void) fprintf(fp, "YES NO <size> | none\n");
386 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
387 (void) fprintf(fp, "YES NO <size> | none\n");
389 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
390 "with standard units such as K, M, G, etc.\n"));
391 (void) fprintf(fp, gettext("\nUser-defined properties can "
392 "be specified by using a name containing a colon (:).\n"));
393 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
394 "properties must be appended with\n"
395 "a user or group specifier of one of these forms:\n"
396 " POSIX name (eg: \"matt\")\n"
397 " POSIX id (eg: \"126829\")\n"
398 " SMB name@domain (eg: \"matt@sun\")\n"
399 " SMB SID (eg: \"S-1-234-567-89\")\n"));
402 gettext("\nFor the property list, run: %s\n"),
405 gettext("\nFor the delegated permission list, run: %s\n"),
406 "zfs allow|unallow");
410 * See comments at end of main().
412 if (getenv("ZFS_ABORT") != NULL) {
413 (void) printf("dumping core by request\n");
417 exit(requested ? 0 : 2);
421 parseprop(nvlist_t *props)
423 char *propname = optarg;
424 char *propval, *strval;
426 if ((propval = strchr(propname, '=')) == NULL) {
427 (void) fprintf(stderr, gettext("missing "
428 "'=' for -o option\n"));
433 if (nvlist_lookup_string(props, propname, &strval) == 0) {
434 (void) fprintf(stderr, gettext("property '%s' "
435 "specified multiple times\n"), propname);
438 if (nvlist_add_string(props, propname, propval) != 0) {
439 (void) fprintf(stderr, gettext("internal "
440 "error: out of memory\n"));
447 parse_depth(char *opt, int *flags)
452 depth = (int)strtol(opt, &tmp, 0);
454 (void) fprintf(stderr,
455 gettext("%s is not an integer\n"), optarg);
459 (void) fprintf(stderr,
460 gettext("Depth can not be negative.\n"));
463 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
468 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
470 * Given an existing dataset, create a writable copy whose initial contents
471 * are the same as the source. The newly created dataset maintains a
472 * dependency on the original; the original cannot be destroyed so long as
475 * The '-p' flag creates all the non-existing ancestors of the target first.
478 zfs_do_clone(int argc, char **argv)
480 zfs_handle_t *zhp = NULL;
481 boolean_t parents = B_FALSE;
486 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
487 (void) fprintf(stderr, gettext("internal error: "
493 while ((c = getopt(argc, argv, "o:p")) != -1) {
496 if (parseprop(props))
503 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
512 /* check number of arguments */
514 (void) fprintf(stderr, gettext("missing source dataset "
519 (void) fprintf(stderr, gettext("missing target dataset "
524 (void) fprintf(stderr, gettext("too many arguments\n"));
528 /* open the source dataset */
529 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
532 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
535 * Now create the ancestors of the target dataset. If the
536 * target already exists and '-p' option was used we should not
539 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
542 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
547 ret = zfs_clone(zhp, argv[1], props);
549 /* create the mountpoint if necessary */
553 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
555 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
556 ret = zfs_share(clone);
575 * zfs create [-p] [-o prop=value] ... fs
576 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
578 * Create a new dataset. This command can be used to create filesystems
579 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
580 * For volumes, the user must specify a size to be used.
582 * The '-s' flag applies only to volumes, and indicates that we should not try
583 * to set the reservation for this volume. By default we set a reservation
584 * equal to the size for any volume. For pools with SPA_VERSION >=
585 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
587 * The '-p' flag creates all the non-existing ancestors of the target first.
590 zfs_do_create(int argc, char **argv)
592 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
593 zfs_handle_t *zhp = NULL;
596 boolean_t noreserve = B_FALSE;
597 boolean_t bflag = B_FALSE;
598 boolean_t parents = B_FALSE;
604 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
605 (void) fprintf(stderr, gettext("internal error: "
611 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
614 type = ZFS_TYPE_VOLUME;
615 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
616 (void) fprintf(stderr, gettext("bad volume "
617 "size '%s': %s\n"), optarg,
618 libzfs_error_description(g_zfs));
622 if (nvlist_add_uint64(props,
623 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
625 (void) fprintf(stderr, gettext("internal "
626 "error: out of memory\n"));
636 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
637 (void) fprintf(stderr, gettext("bad volume "
638 "block size '%s': %s\n"), optarg,
639 libzfs_error_description(g_zfs));
643 if (nvlist_add_uint64(props,
644 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
646 (void) fprintf(stderr, gettext("internal "
647 "error: out of memory\n"));
652 if (parseprop(props))
659 (void) fprintf(stderr, gettext("missing size "
664 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
670 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
671 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
672 "used when creating a volume\n"));
679 /* check number of arguments */
681 (void) fprintf(stderr, gettext("missing %s argument\n"),
682 zfs_type_to_name(type));
686 (void) fprintf(stderr, gettext("too many arguments\n"));
690 if (type == ZFS_TYPE_VOLUME && !noreserve) {
691 zpool_handle_t *zpool_handle;
692 uint64_t spa_version;
694 zfs_prop_t resv_prop;
697 if (p = strchr(argv[0], '/'))
699 zpool_handle = zpool_open(g_zfs, argv[0]);
702 if (zpool_handle == NULL)
704 spa_version = zpool_get_prop_int(zpool_handle,
705 ZPOOL_PROP_VERSION, NULL);
706 zpool_close(zpool_handle);
707 if (spa_version >= SPA_VERSION_REFRESERVATION)
708 resv_prop = ZFS_PROP_REFRESERVATION;
710 resv_prop = ZFS_PROP_RESERVATION;
712 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
714 if (nvlist_add_uint64(props,
715 zfs_prop_to_name(resv_prop), volsize) != 0) {
716 (void) fprintf(stderr, gettext("internal "
717 "error: out of memory\n"));
724 if (parents && zfs_name_valid(argv[0], type)) {
726 * Now create the ancestors of target dataset. If the target
727 * already exists and '-p' option was used we should not
730 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
734 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
739 if (zfs_create(g_zfs, argv[0], type, props) != 0)
742 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
745 * if the user doesn't want the dataset automatically mounted,
746 * then skip the mount/share step
749 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
752 * Mount and/or share the new filesystem as appropriate. We provide a
753 * verbose error message to let the user know that their filesystem was
754 * in fact created, even if we failed to mount or share it.
757 if (canmount == ZFS_CANMOUNT_ON) {
758 if (zfs_mount(zhp, NULL, 0) != 0) {
759 (void) fprintf(stderr, gettext("filesystem "
760 "successfully created, but not mounted\n"));
762 } else if (zfs_share(zhp) != 0) {
763 (void) fprintf(stderr, gettext("filesystem "
764 "successfully created, but not shared\n"));
781 * zfs destroy [-rf] <fs, snap, vol>
783 * -r Recursively destroy all children
784 * -R Recursively destroy all dependents, including clones
785 * -f Force unmounting of any dependents
787 * Destroys the given dataset. By default, it will unmount any filesystems,
788 * and refuse to destroy a dataset that has any dependents. A dependent can
789 * either be a child, or a clone of a child.
791 typedef struct destroy_cbdata {
798 boolean_t cb_closezhp;
799 zfs_handle_t *cb_target;
804 * Check for any dependents based on the '-r' or '-R' flags.
807 destroy_check_dependent(zfs_handle_t *zhp, void *data)
809 destroy_cbdata_t *cbp = data;
810 const char *tname = zfs_get_name(cbp->cb_target);
811 const char *name = zfs_get_name(zhp);
813 if (strncmp(tname, name, strlen(tname)) == 0 &&
814 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
816 * This is a direct descendant, not a clone somewhere else in
823 (void) fprintf(stderr, gettext("cannot destroy '%s': "
824 "%s has children\n"),
825 zfs_get_name(cbp->cb_target),
826 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
827 (void) fprintf(stderr, gettext("use '-r' to destroy "
828 "the following datasets:\n"));
829 cbp->cb_first = B_FALSE;
833 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
836 * This is a clone. We only want to report this if the '-r'
837 * wasn't specified, or the target is a snapshot.
839 if (!cbp->cb_recurse &&
840 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
844 (void) fprintf(stderr, gettext("cannot destroy '%s': "
845 "%s has dependent clones\n"),
846 zfs_get_name(cbp->cb_target),
847 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
848 (void) fprintf(stderr, gettext("use '-R' to destroy "
849 "the following datasets:\n"));
850 cbp->cb_first = B_FALSE;
854 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
863 destroy_callback(zfs_handle_t *zhp, void *data)
865 destroy_cbdata_t *cbp = data;
868 * Ignore pools (which we've already flagged as an error before getting
871 if (strchr(zfs_get_name(zhp), '/') == NULL &&
872 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
878 * Bail out on the first error.
880 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
881 zfs_destroy(zhp) != 0) {
891 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
893 destroy_cbdata_t *cbp = arg;
894 char thissnap[MAXPATHLEN];
896 boolean_t closezhp = cbp->cb_closezhp;
899 (void) snprintf(thissnap, sizeof (thissnap),
900 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
902 libzfs_print_on_error(g_zfs, B_FALSE);
903 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
904 libzfs_print_on_error(g_zfs, B_TRUE);
907 * Destroy any clones of this snapshot
909 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
919 cbp->cb_closezhp = B_TRUE;
920 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
927 zfs_do_destroy(int argc, char **argv)
929 destroy_cbdata_t cb = { 0 };
935 while ((c = getopt(argc, argv, "frR")) != -1) {
949 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
958 /* check number of arguments */
960 (void) fprintf(stderr, gettext("missing path argument\n"));
964 (void) fprintf(stderr, gettext("too many arguments\n"));
969 * If we are doing recursive destroy of a snapshot, then the
970 * named snapshot may not exist. Go straight to libzfs.
972 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
976 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
981 if (cb.cb_doclones) {
983 if (destroy_snap_clones(zhp, &cb) != 0) {
989 ret = zfs_destroy_snaps(zhp, cp);
992 (void) fprintf(stderr,
993 gettext("no snapshots destroyed\n"));
999 /* Open the given dataset */
1000 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
1006 * Perform an explicit check for pools before going any further.
1008 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1009 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1010 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1011 "operation does not apply to pools\n"),
1013 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1014 "%s' to destroy all datasets in the pool\n"),
1016 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1017 "to destroy the pool itself\n"), zfs_get_name(zhp));
1023 * Check for any dependents and/or clones.
1025 cb.cb_first = B_TRUE;
1026 if (!cb.cb_doclones &&
1027 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1034 zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
1040 * Do the real thing. The callback will close the handle regardless of
1041 * whether it succeeds or not.
1044 if (destroy_callback(zhp, &cb) != 0)
1052 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
1053 * < all | property[,property]... > < fs | snap | vol > ...
1055 * -r recurse over any child datasets
1056 * -H scripted mode. Headers are stripped, and fields are separated
1057 * by tabs instead of spaces.
1058 * -o Set of fields to display. One of "name,property,value,source".
1059 * Default is all four.
1060 * -s Set of sources to allow. One of
1061 * "local,default,inherited,temporary,none". Default is all
1063 * -p Display values in parsable (literal) format.
1065 * Prints properties for the given datasets. The user can control which
1066 * columns to display as well as which property types to allow.
1070 * Invoked to display the properties for a single dataset.
1073 get_callback(zfs_handle_t *zhp, void *data)
1075 char buf[ZFS_MAXPROPLEN];
1076 zprop_source_t sourcetype;
1077 char source[ZFS_MAXNAMELEN];
1078 zprop_get_cbdata_t *cbp = data;
1079 nvlist_t *userprop = zfs_get_user_props(zhp);
1080 zprop_list_t *pl = cbp->cb_proplist;
1085 for (; pl != NULL; pl = pl->pl_next) {
1087 * Skip the special fake placeholder. This will also skip over
1088 * the name property when 'all' is specified.
1090 if (pl->pl_prop == ZFS_PROP_NAME &&
1091 pl == cbp->cb_proplist)
1094 if (pl->pl_prop != ZPROP_INVAL) {
1095 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1096 sizeof (buf), &sourcetype, source,
1098 cbp->cb_literal) != 0) {
1101 if (!zfs_prop_valid_for_type(pl->pl_prop,
1102 ZFS_TYPE_DATASET)) {
1103 (void) fprintf(stderr,
1104 gettext("No such property '%s'\n"),
1105 zfs_prop_to_name(pl->pl_prop));
1108 sourcetype = ZPROP_SRC_NONE;
1109 (void) strlcpy(buf, "-", sizeof (buf));
1112 zprop_print_one_property(zfs_get_name(zhp), cbp,
1113 zfs_prop_to_name(pl->pl_prop),
1114 buf, sourcetype, source);
1115 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1116 sourcetype = ZPROP_SRC_LOCAL;
1118 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1119 buf, sizeof (buf), cbp->cb_literal) != 0) {
1120 sourcetype = ZPROP_SRC_NONE;
1121 (void) strlcpy(buf, "-", sizeof (buf));
1124 zprop_print_one_property(zfs_get_name(zhp), cbp,
1125 pl->pl_user_prop, buf, sourcetype, source);
1127 if (nvlist_lookup_nvlist(userprop,
1128 pl->pl_user_prop, &propval) != 0) {
1131 sourcetype = ZPROP_SRC_NONE;
1134 verify(nvlist_lookup_string(propval,
1135 ZPROP_VALUE, &strval) == 0);
1136 verify(nvlist_lookup_string(propval,
1137 ZPROP_SOURCE, &sourceval) == 0);
1139 if (strcmp(sourceval,
1140 zfs_get_name(zhp)) == 0) {
1141 sourcetype = ZPROP_SRC_LOCAL;
1143 sourcetype = ZPROP_SRC_INHERITED;
1144 (void) strlcpy(source,
1145 sourceval, sizeof (source));
1149 zprop_print_one_property(zfs_get_name(zhp), cbp,
1150 pl->pl_user_prop, strval, sourcetype,
1159 zfs_do_get(int argc, char **argv)
1161 zprop_get_cbdata_t cb = { 0 };
1162 int i, c, flags = 0;
1163 char *value, *fields;
1166 zprop_list_t fake_name = { 0 };
1169 * Set up default columns and sources.
1171 cb.cb_sources = ZPROP_SRC_ALL;
1172 cb.cb_columns[0] = GET_COL_NAME;
1173 cb.cb_columns[1] = GET_COL_PROPERTY;
1174 cb.cb_columns[2] = GET_COL_VALUE;
1175 cb.cb_columns[3] = GET_COL_SOURCE;
1176 cb.cb_type = ZFS_TYPE_DATASET;
1179 while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1182 cb.cb_literal = B_TRUE;
1185 limit = parse_depth(optarg, &flags);
1188 flags |= ZFS_ITER_RECURSE;
1191 cb.cb_scripted = B_TRUE;
1194 (void) fprintf(stderr, gettext("missing argument for "
1195 "'%c' option\n"), optopt);
1200 * Process the set of columns to display. We zero out
1201 * the structure to give us a blank slate.
1203 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1205 while (*optarg != '\0') {
1206 static char *col_subopts[] =
1207 { "name", "property", "value", "source",
1211 (void) fprintf(stderr, gettext("too "
1212 "many fields given to -o "
1217 switch (getsubopt(&optarg, col_subopts,
1220 cb.cb_columns[i++] = GET_COL_NAME;
1223 cb.cb_columns[i++] = GET_COL_PROPERTY;
1226 cb.cb_columns[i++] = GET_COL_VALUE;
1229 cb.cb_columns[i++] = GET_COL_SOURCE;
1232 (void) fprintf(stderr,
1233 gettext("invalid column name "
1242 while (*optarg != '\0') {
1243 static char *source_subopts[] = {
1244 "local", "default", "inherited",
1245 "temporary", "none", NULL };
1247 switch (getsubopt(&optarg, source_subopts,
1250 cb.cb_sources |= ZPROP_SRC_LOCAL;
1253 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1256 cb.cb_sources |= ZPROP_SRC_INHERITED;
1259 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1262 cb.cb_sources |= ZPROP_SRC_NONE;
1265 (void) fprintf(stderr,
1266 gettext("invalid source "
1274 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1284 (void) fprintf(stderr, gettext("missing property "
1291 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1299 * As part of zfs_expand_proplist(), we keep track of the maximum column
1300 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1301 * need to know the maximum name length. However, the user likely did
1302 * not specify 'name' as one of the properties to fetch, so we need to
1303 * make sure we always include at least this property for
1304 * print_get_headers() to work properly.
1306 if (cb.cb_proplist != NULL) {
1307 fake_name.pl_prop = ZFS_PROP_NAME;
1308 fake_name.pl_width = strlen(gettext("NAME"));
1309 fake_name.pl_next = cb.cb_proplist;
1310 cb.cb_proplist = &fake_name;
1313 cb.cb_first = B_TRUE;
1315 /* run for each object */
1316 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1317 &cb.cb_proplist, limit, get_callback, &cb);
1319 if (cb.cb_proplist == &fake_name)
1320 zprop_free_list(fake_name.pl_next);
1322 zprop_free_list(cb.cb_proplist);
1328 * inherit [-r] <property> <fs|vol> ...
1330 * -r Recurse over all children
1332 * For each dataset specified on the command line, inherit the given property
1333 * from its parent. Inheriting a property at the pool level will cause it to
1334 * use the default value. The '-r' flag will recurse over all children, and is
1335 * useful for setting a property on a hierarchy-wide basis, regardless of any
1336 * local modifications for each dataset.
1340 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1342 char *propname = data;
1343 zfs_prop_t prop = zfs_name_to_prop(propname);
1346 * If we're doing it recursively, then ignore properties that
1347 * are not valid for this type of dataset.
1349 if (prop != ZPROP_INVAL &&
1350 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1353 return (zfs_prop_inherit(zhp, propname) != 0);
1357 inherit_cb(zfs_handle_t *zhp, void *data)
1359 char *propname = data;
1361 return (zfs_prop_inherit(zhp, propname) != 0);
1365 zfs_do_inherit(int argc, char **argv)
1374 while ((c = getopt(argc, argv, "r")) != -1) {
1377 flags |= ZFS_ITER_RECURSE;
1381 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1390 /* check number of arguments */
1392 (void) fprintf(stderr, gettext("missing property argument\n"));
1396 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1404 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1405 if (zfs_prop_readonly(prop)) {
1406 (void) fprintf(stderr, gettext(
1407 "%s property is read-only\n"),
1411 if (!zfs_prop_inheritable(prop)) {
1412 (void) fprintf(stderr, gettext("'%s' property cannot "
1413 "be inherited\n"), propname);
1414 if (prop == ZFS_PROP_QUOTA ||
1415 prop == ZFS_PROP_RESERVATION ||
1416 prop == ZFS_PROP_REFQUOTA ||
1417 prop == ZFS_PROP_REFRESERVATION)
1418 (void) fprintf(stderr, gettext("use 'zfs set "
1419 "%s=none' to clear\n"), propname);
1422 } else if (!zfs_prop_user(propname)) {
1423 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1428 if (flags & ZFS_ITER_RECURSE) {
1429 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1430 NULL, NULL, 0, inherit_recurse_cb, propname);
1432 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1433 NULL, NULL, 0, inherit_cb, propname);
1439 typedef struct upgrade_cbdata {
1440 uint64_t cb_numupgraded;
1441 uint64_t cb_numsamegraded;
1442 uint64_t cb_numfailed;
1443 uint64_t cb_version;
1445 boolean_t cb_foundone;
1446 char cb_lastfs[ZFS_MAXNAMELEN];
1450 same_pool(zfs_handle_t *zhp, const char *name)
1452 int len1 = strcspn(name, "/@");
1453 const char *zhname = zfs_get_name(zhp);
1454 int len2 = strcspn(zhname, "/@");
1458 return (strncmp(name, zhname, len1) == 0);
1462 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1464 upgrade_cbdata_t *cb = data;
1465 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1467 /* list if it's old/new */
1468 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1469 (cb->cb_newer && version > ZPL_VERSION)) {
1472 str = gettext("The following filesystems are "
1473 "formatted using a newer software version and\n"
1474 "cannot be accessed on the current system.\n\n");
1476 str = gettext("The following filesystems are "
1477 "out of date, and can be upgraded. After being\n"
1478 "upgraded, these filesystems (and any 'zfs send' "
1479 "streams generated from\n"
1480 "subsequent snapshots) will no longer be "
1481 "accessible by older software versions.\n\n");
1484 if (!cb->cb_foundone) {
1486 (void) printf(gettext("VER FILESYSTEM\n"));
1487 (void) printf(gettext("--- ------------\n"));
1488 cb->cb_foundone = B_TRUE;
1491 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1498 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1500 upgrade_cbdata_t *cb = data;
1501 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1503 static struct { int zplver; int spaver; } table[] = {
1504 {ZPL_VERSION_FUID, SPA_VERSION_FUID},
1505 {ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
1510 for (i = 0; table[i].zplver; i++) {
1511 if (cb->cb_version >= table[i].zplver) {
1514 if (zfs_spa_version(zhp, &spa_version) < 0)
1517 if (spa_version < table[i].spaver) {
1519 (void) printf(gettext("%s: can not be "
1520 "upgraded; the pool version needs to first "
1521 "be upgraded\nto version %d\n\n"),
1522 zfs_get_name(zhp), table[i].spaver);
1530 if (version < cb->cb_version) {
1532 (void) snprintf(verstr, sizeof (verstr),
1533 "%llu", cb->cb_version);
1534 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1536 * If they did "zfs upgrade -a", then we could
1537 * be doing ioctls to different pools. We need
1538 * to log this history once to each pool.
1540 verify(zpool_stage_history(g_zfs, history_str) == 0);
1542 if (zfs_prop_set(zhp, "version", verstr) == 0)
1543 cb->cb_numupgraded++;
1546 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1547 } else if (version > cb->cb_version) {
1548 /* can't downgrade */
1549 (void) printf(gettext("%s: can not be downgraded; "
1550 "it is already at version %u\n"),
1551 zfs_get_name(zhp), version);
1554 cb->cb_numsamegraded++;
1562 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1565 zfs_do_upgrade(int argc, char **argv)
1567 boolean_t all = B_FALSE;
1568 boolean_t showversions = B_FALSE;
1570 upgrade_cbdata_t cb = { 0 };
1572 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1575 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1578 flags |= ZFS_ITER_RECURSE;
1581 showversions = B_TRUE;
1584 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1585 optarg, &cb.cb_version) != 0) {
1586 (void) fprintf(stderr,
1587 gettext("invalid version %s\n"), optarg);
1596 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1605 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1607 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1608 cb.cb_version || argc))
1610 if ((all || argc) && (showversions))
1616 /* Show info on available versions. */
1617 (void) printf(gettext("The following filesystem versions are "
1619 (void) printf(gettext("VER DESCRIPTION\n"));
1620 (void) printf("--- -----------------------------------------"
1621 "---------------\n");
1622 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1623 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1624 (void) printf(gettext(" 3 Case insensitive and File system "
1625 "unique identifer (FUID)\n"));
1626 (void) printf(gettext(" 4 userquota, groupquota "
1628 (void) printf(gettext("\nFor more information on a particular "
1629 "version, including supported releases, see:\n\n"));
1630 (void) printf("http://www.opensolaris.org/os/community/zfs/"
1631 "version/zpl/N\n\n");
1632 (void) printf(gettext("Where 'N' is the version number.\n"));
1634 } else if (argc || all) {
1635 /* Upgrade filesystems */
1636 if (cb.cb_version == 0)
1637 cb.cb_version = ZPL_VERSION;
1638 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1639 NULL, NULL, 0, upgrade_set_callback, &cb);
1640 (void) printf(gettext("%llu filesystems upgraded\n"),
1642 if (cb.cb_numsamegraded) {
1643 (void) printf(gettext("%llu filesystems already at "
1645 cb.cb_numsamegraded);
1647 if (cb.cb_numfailed != 0)
1650 /* List old-version filesytems */
1652 (void) printf(gettext("This system is currently running "
1653 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1655 flags |= ZFS_ITER_RECURSE;
1656 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1657 NULL, NULL, 0, upgrade_list_callback, &cb);
1659 found = cb.cb_foundone;
1660 cb.cb_foundone = B_FALSE;
1661 cb.cb_newer = B_TRUE;
1663 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1664 NULL, NULL, 0, upgrade_list_callback, &cb);
1666 if (!cb.cb_foundone && !found) {
1667 (void) printf(gettext("All filesystems are "
1668 "formatted with the current version.\n"));
1679 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
1681 zfs_userquota_prop_t *typep = arg;
1682 zfs_userquota_prop_t p = *typep;
1684 char *ug, *propname;
1688 if (domain == NULL || domain[0] == '\0') {
1689 if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) {
1690 struct group *g = getgrgid(rid);
1694 struct passwd *p = getpwuid(rid);
1700 if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA)
1705 if (p == ZFS_PROP_USERUSED || p == ZFS_PROP_GROUPUSED)
1711 (void) snprintf(namebuf, sizeof (namebuf),
1712 "%llu", (longlong_t)rid);
1715 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
1717 (void) printf("%s %s %s%c%s %s\n", propname, ug, domain,
1718 domain[0] ? '-' : ' ', name, sizebuf);
1724 zfs_do_userspace(int argc, char **argv)
1727 zfs_userquota_prop_t p;
1731 * Try the python version. If the execv fails, we'll continue
1732 * and do a simplistic implementation.
1734 (void) execv(pypath, argv-1);
1736 (void) fprintf(stderr, "internal error: %s not found\n"
1737 "falling back on built-in implementation, "
1738 "some features will not work\n", pypath);
1739 (void) fprintf(stderr, " install sysutils/py-zfs port to correct this\n");
1741 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
1744 (void) printf("PROP TYPE NAME VALUE\n");
1746 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
1747 error = zfs_userspace(zhp, p, userspace_cb, &p);
1755 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
1756 * [-s property [-s property]...] [-S property [-S property]...]
1759 * -r Recurse over all children
1760 * -d Limit recursion by depth.
1761 * -H Scripted mode; elide headers and separate columns by tabs
1762 * -o Control which fields to display.
1763 * -t Control which object types to display.
1764 * -s Specify sort columns, descending order.
1765 * -S Specify sort columns, ascending order.
1767 * When given no arguments, lists all filesystems in the system.
1768 * Otherwise, list the specified datasets, optionally recursing down them if
1769 * '-r' is specified.
1771 typedef struct list_cbdata {
1773 boolean_t cb_scripted;
1774 zprop_list_t *cb_proplist;
1778 * Given a list of columns to display, output appropriate headers for each one.
1781 print_header(zprop_list_t *pl)
1783 char headerbuf[ZFS_MAXPROPLEN];
1786 boolean_t first = B_TRUE;
1787 boolean_t right_justify;
1789 for (; pl != NULL; pl = pl->pl_next) {
1796 right_justify = B_FALSE;
1797 if (pl->pl_prop != ZPROP_INVAL) {
1798 header = zfs_prop_column_name(pl->pl_prop);
1799 right_justify = zfs_prop_align_right(pl->pl_prop);
1801 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1802 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1803 headerbuf[i] = '\0';
1807 if (pl->pl_next == NULL && !right_justify)
1808 (void) printf("%s", header);
1809 else if (right_justify)
1810 (void) printf("%*s", pl->pl_width, header);
1812 (void) printf("%-*s", pl->pl_width, header);
1815 (void) printf("\n");
1819 * Given a dataset and a list of fields, print out all the properties according
1820 * to the described layout.
1823 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1825 boolean_t first = B_TRUE;
1826 char property[ZFS_MAXPROPLEN];
1827 nvlist_t *userprops = zfs_get_user_props(zhp);
1830 boolean_t right_justify;
1833 for (; pl != NULL; pl = pl->pl_next) {
1836 (void) printf("\t");
1843 if (pl->pl_prop != ZPROP_INVAL) {
1844 if (zfs_prop_get(zhp, pl->pl_prop, property,
1845 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1850 right_justify = zfs_prop_align_right(pl->pl_prop);
1851 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1852 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1853 property, sizeof (property), B_FALSE) != 0)
1857 right_justify = B_TRUE;
1859 if (nvlist_lookup_nvlist(userprops,
1860 pl->pl_user_prop, &propval) != 0)
1863 verify(nvlist_lookup_string(propval,
1864 ZPROP_VALUE, &propstr) == 0);
1865 right_justify = B_FALSE;
1868 width = pl->pl_width;
1871 * If this is being called in scripted mode, or if this is the
1872 * last column and it is left-justified, don't include a width
1875 if (scripted || (pl->pl_next == NULL && !right_justify))
1876 (void) printf("%s", propstr);
1877 else if (right_justify)
1878 (void) printf("%*s", width, propstr);
1880 (void) printf("%-*s", width, propstr);
1883 (void) printf("\n");
1887 * Generic callback function to list a dataset or snapshot.
1890 list_callback(zfs_handle_t *zhp, void *data)
1892 list_cbdata_t *cbp = data;
1894 if (cbp->cb_first) {
1895 if (!cbp->cb_scripted)
1896 print_header(cbp->cb_proplist);
1897 cbp->cb_first = B_FALSE;
1900 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1906 zfs_do_list(int argc, char **argv)
1909 boolean_t scripted = B_FALSE;
1910 static char default_fields[] =
1911 "name,used,available,referenced,mountpoint";
1912 int types = ZFS_TYPE_DATASET;
1913 boolean_t types_specified = B_FALSE;
1914 char *fields = NULL;
1915 list_cbdata_t cb = { 0 };
1919 zfs_sort_column_t *sortcol = NULL;
1920 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
1923 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
1929 limit = parse_depth(optarg, &flags);
1932 flags |= ZFS_ITER_RECURSE;
1938 if (zfs_add_sort_column(&sortcol, optarg,
1940 (void) fprintf(stderr,
1941 gettext("invalid property '%s'\n"), optarg);
1946 if (zfs_add_sort_column(&sortcol, optarg,
1948 (void) fprintf(stderr,
1949 gettext("invalid property '%s'\n"), optarg);
1955 types_specified = B_TRUE;
1956 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1957 while (*optarg != '\0') {
1958 static char *type_subopts[] = { "filesystem",
1959 "volume", "snapshot", "all", NULL };
1961 switch (getsubopt(&optarg, type_subopts,
1964 types |= ZFS_TYPE_FILESYSTEM;
1967 types |= ZFS_TYPE_VOLUME;
1970 types |= ZFS_TYPE_SNAPSHOT;
1973 types = ZFS_TYPE_DATASET;
1977 (void) fprintf(stderr,
1978 gettext("invalid type '%s'\n"),
1985 (void) fprintf(stderr, gettext("missing argument for "
1986 "'%c' option\n"), optopt);
1990 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2000 fields = default_fields;
2003 * If "-o space" and no types were specified, don't display snapshots.
2005 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
2006 types &= ~ZFS_TYPE_SNAPSHOT;
2009 * If the user specifies '-o all', the zprop_get_list() doesn't
2010 * normally include the name of the dataset. For 'zfs list', we always
2011 * want this property to be first.
2013 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2017 cb.cb_scripted = scripted;
2018 cb.cb_first = B_TRUE;
2020 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
2021 limit, list_callback, &cb);
2023 zprop_free_list(cb.cb_proplist);
2024 zfs_free_sort_columns(sortcol);
2026 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
2027 (void) printf(gettext("no datasets available\n"));
2033 * zfs rename <fs | snap | vol> <fs | snap | vol>
2034 * zfs rename -p <fs | vol> <fs | vol>
2035 * zfs rename -r <snap> <snap>
2037 * Renames the given dataset to another of the same type.
2039 * The '-p' flag creates all the non-existing ancestors of the target first.
2043 zfs_do_rename(int argc, char **argv)
2048 boolean_t recurse = B_FALSE;
2049 boolean_t parents = B_FALSE;
2052 while ((c = getopt(argc, argv, "pr")) != -1) {
2062 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2071 /* check number of arguments */
2073 (void) fprintf(stderr, gettext("missing source dataset "
2078 (void) fprintf(stderr, gettext("missing target dataset "
2083 (void) fprintf(stderr, gettext("too many arguments\n"));
2087 if (recurse && parents) {
2088 (void) fprintf(stderr, gettext("-p and -r options are mutually "
2093 if (recurse && strchr(argv[0], '@') == 0) {
2094 (void) fprintf(stderr, gettext("source dataset for recursive "
2095 "rename must be a snapshot\n"));
2099 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
2100 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
2103 /* If we were asked and the name looks good, try to create ancestors. */
2104 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
2105 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
2110 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
2119 * Promotes the given clone fs to be the parent
2123 zfs_do_promote(int argc, char **argv)
2129 if (argc > 1 && argv[1][0] == '-') {
2130 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2135 /* check number of arguments */
2137 (void) fprintf(stderr, gettext("missing clone filesystem"
2142 (void) fprintf(stderr, gettext("too many arguments\n"));
2146 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2150 ret = (zfs_promote(zhp) != 0);
2158 * zfs rollback [-rRf] <snapshot>
2160 * -r Delete any intervening snapshots before doing rollback
2161 * -R Delete any snapshots and their clones
2162 * -f ignored for backwards compatability
2164 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2165 * since then and making it the active dataset. If more recent snapshots exist,
2166 * the command will complain unless the '-r' flag is given.
2168 typedef struct rollback_cbdata {
2174 boolean_t cb_recurse;
2175 boolean_t cb_dependent;
2176 } rollback_cbdata_t;
2179 * Report any snapshots more recent than the one specified. Used when '-r' is
2180 * not specified. We reuse this same callback for the snapshot dependents - if
2181 * 'cb_dependent' is set, then this is a dependent and we should report it
2182 * without checking the transaction group.
2185 rollback_check(zfs_handle_t *zhp, void *data)
2187 rollback_cbdata_t *cbp = data;
2189 if (cbp->cb_doclones) {
2194 if (!cbp->cb_dependent) {
2195 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2196 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2197 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2200 if (cbp->cb_first && !cbp->cb_recurse) {
2201 (void) fprintf(stderr, gettext("cannot "
2202 "rollback to '%s': more recent snapshots "
2205 (void) fprintf(stderr, gettext("use '-r' to "
2206 "force deletion of the following "
2212 if (cbp->cb_recurse) {
2213 cbp->cb_dependent = B_TRUE;
2214 if (zfs_iter_dependents(zhp, B_TRUE,
2215 rollback_check, cbp) != 0) {
2219 cbp->cb_dependent = B_FALSE;
2221 (void) fprintf(stderr, "%s\n",
2226 if (cbp->cb_first && cbp->cb_recurse) {
2227 (void) fprintf(stderr, gettext("cannot rollback to "
2228 "'%s': clones of previous snapshots exist\n"),
2230 (void) fprintf(stderr, gettext("use '-R' to "
2231 "force deletion of the following clones and "
2237 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2245 zfs_do_rollback(int argc, char **argv)
2249 boolean_t force = B_FALSE;
2250 rollback_cbdata_t cb = { 0 };
2251 zfs_handle_t *zhp, *snap;
2252 char parentname[ZFS_MAXNAMELEN];
2256 while ((c = getopt(argc, argv, "rRf")) != -1) {
2269 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2278 /* check number of arguments */
2280 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2284 (void) fprintf(stderr, gettext("too many arguments\n"));
2288 /* open the snapshot */
2289 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2292 /* open the parent dataset */
2293 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2294 verify((delim = strrchr(parentname, '@')) != NULL);
2296 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2302 * Check for more recent snapshots and/or clones based on the presence
2305 cb.cb_target = argv[0];
2306 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2307 cb.cb_first = B_TRUE;
2309 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2312 if ((ret = cb.cb_error) != 0)
2316 * Rollback parent to the given snapshot.
2318 ret = zfs_rollback(zhp, snap, force);
2331 * zfs set property=value { fs | snap | vol } ...
2333 * Sets the given property for all datasets specified on the command line.
2335 typedef struct set_cbdata {
2341 set_callback(zfs_handle_t *zhp, void *data)
2343 set_cbdata_t *cbp = data;
2345 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2346 switch (libzfs_errno(g_zfs)) {
2347 case EZFS_MOUNTFAILED:
2348 (void) fprintf(stderr, gettext("property may be set "
2349 "but unable to remount filesystem\n"));
2351 case EZFS_SHARENFSFAILED:
2352 (void) fprintf(stderr, gettext("property may be set "
2353 "but unable to reshare filesystem\n"));
2362 zfs_do_set(int argc, char **argv)
2367 /* check for options */
2368 if (argc > 1 && argv[1][0] == '-') {
2369 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2374 /* check number of arguments */
2376 (void) fprintf(stderr, gettext("missing property=value "
2381 (void) fprintf(stderr, gettext("missing dataset name\n"));
2385 /* validate property=value argument */
2386 cb.cb_propname = argv[1];
2387 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2388 (cb.cb_value[1] == '\0')) {
2389 (void) fprintf(stderr, gettext("missing value in "
2390 "property=value argument\n"));
2394 *cb.cb_value = '\0';
2397 if (*cb.cb_propname == '\0') {
2398 (void) fprintf(stderr,
2399 gettext("missing property in property=value argument\n"));
2403 ret = zfs_for_each(argc - 2, argv + 2, 0,
2404 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
2410 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2412 * Creates a snapshot with the given name. While functionally equivalent to
2413 * 'zfs create', it is a separate command to differentiate intent.
2416 zfs_do_snapshot(int argc, char **argv)
2418 boolean_t recursive = B_FALSE;
2423 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2424 (void) fprintf(stderr, gettext("internal error: "
2425 "out of memory\n"));
2430 while ((c = getopt(argc, argv, "ro:")) != -1) {
2433 if (parseprop(props))
2440 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2449 /* check number of arguments */
2451 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2455 (void) fprintf(stderr, gettext("too many arguments\n"));
2459 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2461 if (ret && recursive)
2462 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2472 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2473 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2475 * Send a backup stream to stdout.
2478 zfs_do_send(int argc, char **argv)
2480 char *fromname = NULL;
2481 char *toname = NULL;
2484 boolean_t doall = B_FALSE;
2485 boolean_t replicate = B_FALSE;
2486 boolean_t fromorigin = B_FALSE;
2487 boolean_t verbose = B_FALSE;
2491 while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
2511 (void) fprintf(stderr, gettext("missing argument for "
2512 "'%c' option\n"), optopt);
2516 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2525 /* check number of arguments */
2527 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2531 (void) fprintf(stderr, gettext("too many arguments\n"));
2535 if (isatty(STDOUT_FILENO)) {
2536 (void) fprintf(stderr,
2537 gettext("Error: Stream can not be written to a terminal.\n"
2538 "You must redirect standard output.\n"));
2542 cp = strchr(argv[0], '@');
2544 (void) fprintf(stderr,
2545 gettext("argument must be a snapshot\n"));
2550 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2555 * If they specified the full path to the snapshot, chop off
2556 * everything except the short name of the snapshot, but special
2557 * case if they specify the origin.
2559 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2560 char origin[ZFS_MAXNAMELEN];
2563 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2564 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2566 if (strcmp(origin, fromname) == 0) {
2568 fromorigin = B_TRUE;
2571 if (cp != fromname && strcmp(argv[0], fromname)) {
2572 (void) fprintf(stderr,
2573 gettext("incremental source must be "
2574 "in same filesystem\n"));
2578 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2579 (void) fprintf(stderr,
2580 gettext("invalid incremental source\n"));
2586 if (replicate && fromname == NULL)
2589 err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
2590 verbose, STDOUT_FILENO);
2597 * zfs receive [-dnvF] <fs@snap>
2599 * Restore a backup stream from stdin.
2602 zfs_do_receive(int argc, char **argv)
2607 bzero(&flags, sizeof (recvflags_t));
2609 while ((c = getopt(argc, argv, ":dnuvF")) != -1) {
2612 flags.isprefix = B_TRUE;
2615 flags.dryrun = B_TRUE;
2618 flags.nomount = B_TRUE;
2621 flags.verbose = B_TRUE;
2624 flags.force = B_TRUE;
2627 (void) fprintf(stderr, gettext("missing argument for "
2628 "'%c' option\n"), optopt);
2632 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2641 /* check number of arguments */
2643 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2647 (void) fprintf(stderr, gettext("too many arguments\n"));
2651 if (isatty(STDIN_FILENO)) {
2652 (void) fprintf(stderr,
2653 gettext("Error: Backup stream can not be read "
2654 "from a terminal.\n"
2655 "You must redirect standard input.\n"));
2659 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2664 typedef struct get_all_cbdata {
2665 zfs_handle_t **cb_handles;
2669 boolean_t cb_verbose;
2672 #define CHECK_SPINNER 30
2673 #define SPINNER_TIME 3 /* seconds */
2674 #define MOUNT_TIME 5 /* seconds */
2677 get_one_dataset(zfs_handle_t *zhp, void *data)
2679 static char spin[] = { '-', '\\', '|', '/' };
2680 static int spinval = 0;
2681 static int spincheck = 0;
2682 static time_t last_spin_time = (time_t)0;
2683 get_all_cbdata_t *cbp = data;
2684 zfs_type_t type = zfs_get_type(zhp);
2686 if (cbp->cb_verbose) {
2687 if (--spincheck < 0) {
2688 time_t now = time(NULL);
2689 if (last_spin_time + SPINNER_TIME < now) {
2690 (void) printf("\b%c", spin[spinval++ % 4]);
2691 (void) fflush(stdout);
2692 last_spin_time = now;
2694 spincheck = CHECK_SPINNER;
2699 * Interate over any nested datasets.
2701 if (type == ZFS_TYPE_FILESYSTEM &&
2702 zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2708 * Skip any datasets whose type does not match.
2710 if ((type & cbp->cb_types) == 0) {
2715 if (cbp->cb_alloc == cbp->cb_used) {
2716 zfs_handle_t **handles;
2718 if (cbp->cb_alloc == 0)
2723 handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2725 if (cbp->cb_handles) {
2726 bcopy(cbp->cb_handles, handles,
2727 cbp->cb_used * sizeof (void *));
2728 free(cbp->cb_handles);
2731 cbp->cb_handles = handles;
2734 cbp->cb_handles[cbp->cb_used++] = zhp;
2740 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
2743 get_all_cbdata_t cb = { 0 };
2744 cb.cb_types = types;
2745 cb.cb_verbose = verbose;
2748 (void) printf("%s: *", gettext("Reading ZFS config"));
2749 (void) fflush(stdout);
2752 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2754 *dslist = cb.cb_handles;
2755 *count = cb.cb_used;
2758 (void) printf("\b%s\n", gettext("done."));
2763 dataset_cmp(const void *a, const void *b)
2765 zfs_handle_t **za = (zfs_handle_t **)a;
2766 zfs_handle_t **zb = (zfs_handle_t **)b;
2767 char mounta[MAXPATHLEN];
2768 char mountb[MAXPATHLEN];
2769 boolean_t gota, gotb;
2771 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
2772 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2773 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2774 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
2775 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2776 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2779 return (strcmp(mounta, mountb));
2786 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
2790 * Generic callback for sharing or mounting filesystems. Because the code is so
2791 * similar, we have a common function with an extra parameter to determine which
2792 * mode we are using.
2794 #define OP_SHARE 0x1
2795 #define OP_MOUNT 0x2
2798 * Share or mount a dataset.
2801 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
2802 boolean_t explicit, const char *options)
2804 char mountpoint[ZFS_MAXPROPLEN];
2805 char shareopts[ZFS_MAXPROPLEN];
2806 char smbshareopts[ZFS_MAXPROPLEN];
2807 const char *cmdname = op == OP_SHARE ? "share" : "mount";
2809 uint64_t zoned, canmount;
2810 zfs_type_t type = zfs_get_type(zhp);
2811 boolean_t shared_nfs, shared_smb;
2813 assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
2815 if (type == ZFS_TYPE_FILESYSTEM) {
2817 * Check to make sure we can mount/share this dataset. If we
2818 * are in the global zone and the filesystem is exported to a
2819 * local zone, or if we are in a local zone and the
2820 * filesystem is not exported, then it is an error.
2822 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2824 if (zoned && getzoneid() == GLOBAL_ZONEID) {
2828 (void) fprintf(stderr, gettext("cannot %s '%s': "
2829 "dataset is exported to a local zone\n"), cmdname,
2833 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
2837 (void) fprintf(stderr, gettext("cannot %s '%s': "
2838 "permission denied\n"), cmdname,
2844 * Ignore any filesystems which don't apply to us. This
2845 * includes those with a legacy mountpoint, or those with
2846 * legacy share options.
2848 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2849 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
2850 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
2851 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
2852 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
2853 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
2855 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
2856 strcmp(smbshareopts, "off") == 0) {
2860 (void) fprintf(stderr, gettext("cannot share '%s': "
2861 "legacy share\n"), zfs_get_name(zhp));
2862 (void) fprintf(stderr, gettext("use share(1M) to "
2863 "share this filesystem, or set "
2864 "sharenfs property on\n"));
2869 * We cannot share or mount legacy filesystems. If the
2870 * shareopts is non-legacy but the mountpoint is legacy, we
2871 * treat it as a legacy share.
2873 if (strcmp(mountpoint, "legacy") == 0) {
2877 (void) fprintf(stderr, gettext("cannot %s '%s': "
2878 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
2879 (void) fprintf(stderr, gettext("use %s(1M) to "
2880 "%s this filesystem\n"), cmdname, cmdname);
2884 if (strcmp(mountpoint, "none") == 0) {
2888 (void) fprintf(stderr, gettext("cannot %s '%s': no "
2889 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
2894 * canmount explicit outcome
2895 * on no pass through
2896 * on yes pass through
2898 * off yes display error, return 1
2899 * noauto no return 0
2900 * noauto yes pass through
2902 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
2903 if (canmount == ZFS_CANMOUNT_OFF) {
2907 (void) fprintf(stderr, gettext("cannot %s '%s': "
2908 "'canmount' property is set to 'off'\n"), cmdname,
2911 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
2916 * At this point, we have verified that the mountpoint and/or
2917 * shareopts are appropriate for auto management. If the
2918 * filesystem is already mounted or shared, return (failing
2919 * for explicit requests); otherwise mount or share the
2925 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
2926 shared_smb = zfs_is_shared_smb(zhp, NULL);
2928 if (shared_nfs && shared_smb ||
2929 (shared_nfs && strcmp(shareopts, "on") == 0 &&
2930 strcmp(smbshareopts, "off") == 0) ||
2931 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
2932 strcmp(shareopts, "off") == 0)) {
2936 (void) fprintf(stderr, gettext("cannot share "
2937 "'%s': filesystem already shared\n"),
2942 if (!zfs_is_mounted(zhp, NULL) &&
2943 zfs_mount(zhp, NULL, 0) != 0)
2946 if (protocol == NULL) {
2947 if (zfs_shareall(zhp) != 0)
2949 } else if (strcmp(protocol, "nfs") == 0) {
2950 if (zfs_share_nfs(zhp))
2952 } else if (strcmp(protocol, "smb") == 0) {
2953 if (zfs_share_smb(zhp))
2956 (void) fprintf(stderr, gettext("cannot share "
2957 "'%s': invalid share type '%s' "
2959 zfs_get_name(zhp), protocol);
2966 if (options == NULL)
2967 mnt.mnt_mntopts = "";
2969 mnt.mnt_mntopts = (char *)options;
2971 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
2972 zfs_is_mounted(zhp, NULL)) {
2976 (void) fprintf(stderr, gettext("cannot mount "
2977 "'%s': filesystem already mounted\n"),
2982 if (zfs_mount(zhp, options, flags) != 0)
2987 assert(op == OP_SHARE);
2990 * Ignore any volumes that aren't shared.
2992 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
2993 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
2995 if (strcmp(shareopts, "off") == 0) {
2999 (void) fprintf(stderr, gettext("cannot share '%s': "
3000 "'shareiscsi' property not set\n"),
3002 (void) fprintf(stderr, gettext("set 'shareiscsi' "
3003 "property or use iscsitadm(1M) to share this "
3008 if (zfs_is_shared_iscsi(zhp)) {
3012 (void) fprintf(stderr, gettext("cannot share "
3013 "'%s': volume already shared\n"),
3018 if (zfs_share_iscsi(zhp) != 0)
3026 * Reports progress in the form "(current/total)". Not thread-safe.
3029 report_mount_progress(int current, int total)
3032 static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3033 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3034 static time_t last_progress_time;
3035 time_t now = time(NULL);
3037 /* report 1..n instead of 0..n-1 */
3040 /* display header if we're here for the first time */
3042 (void) printf(gettext("Mounting ZFS filesystems: "));
3044 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3045 /* too soon to report again */
3049 last_progress_time = now;
3051 /* back up to prepare for overwriting */
3053 (void) printf("%*.*s", len, len, reverse);
3055 /* We put a newline at the end if this is the last one. */
3056 len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
3057 (void) fflush(stdout);
3061 append_options(char *mntopts, char *newopts)
3063 int len = strlen(mntopts);
3065 /* original length plus new string to append plus 1 for the comma */
3066 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3067 (void) fprintf(stderr, gettext("the opts argument for "
3068 "'%c' option is too long (more than %d chars)\n"),
3069 "-o", MNT_LINE_MAX);
3074 mntopts[len++] = ',';
3076 (void) strcpy(&mntopts[len], newopts);
3080 share_mount(int op, int argc, char **argv)
3083 boolean_t verbose = B_FALSE;
3085 char *options = NULL;
3086 int types, flags = 0;
3089 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3099 if (*optarg == '\0') {
3100 (void) fprintf(stderr, gettext("empty mount "
3101 "options (-o) specified\n"));
3105 if (options == NULL)
3106 options = safe_malloc(MNT_LINE_MAX + 1);
3108 /* option validation is done later */
3109 append_options(options, optarg);
3113 warnx("no overlay mounts support on FreeBSD, ignoring");
3116 (void) fprintf(stderr, gettext("missing argument for "
3117 "'%c' option\n"), optopt);
3121 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3130 /* check number of arguments */
3132 zfs_handle_t **dslist = NULL;
3133 size_t i, count = 0;
3134 char *protocol = NULL;
3136 if (op == OP_MOUNT) {
3137 types = ZFS_TYPE_FILESYSTEM;
3138 } else if (argc > 0) {
3139 if (strcmp(argv[0], "nfs") == 0 ||
3140 strcmp(argv[0], "smb") == 0) {
3141 types = ZFS_TYPE_FILESYSTEM;
3142 } else if (strcmp(argv[0], "iscsi") == 0) {
3143 types = ZFS_TYPE_VOLUME;
3145 (void) fprintf(stderr, gettext("share type "
3146 "must be 'nfs', 'smb' or 'iscsi'\n"));
3153 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3157 (void) fprintf(stderr, gettext("too many arguments\n"));
3161 get_all_datasets(types, &dslist, &count, verbose);
3166 qsort(dslist, count, sizeof (void *), dataset_cmp);
3168 for (i = 0; i < count; i++) {
3170 report_mount_progress(i, count);
3172 if (share_mount_one(dslist[i], op, flags, protocol,
3173 B_FALSE, options) != 0)
3175 zfs_close(dslist[i]);
3179 } else if (argc == 0) {
3183 if ((op == OP_SHARE) || (options != NULL)) {
3184 (void) fprintf(stderr, gettext("missing filesystem "
3185 "argument (specify -a for all)\n"));
3190 * When mount is given no arguments, go through /etc/mnttab and
3191 * display any active ZFS mounts. We hide any snapshots, since
3192 * they are controlled automatically.
3194 if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
3195 fprintf(stderr, "getmntinfo(): %s\n", strerror(errno));
3198 for (i = 0; i < n; i++) {
3199 if (strcmp(sfs[i].f_fstypename, MNTTYPE_ZFS) != 0 ||
3200 strchr(sfs[i].f_mntfromname, '@') != NULL)
3203 (void) printf("%-30s %s\n", sfs[i].f_mntfromname,
3204 sfs[i].f_mntonname);
3210 types = ZFS_TYPE_FILESYSTEM;
3212 types |= ZFS_TYPE_VOLUME;
3215 (void) fprintf(stderr,
3216 gettext("too many arguments\n"));
3220 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3223 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3233 * zfs mount -a [nfs | iscsi]
3234 * zfs mount filesystem
3236 * Mount all filesystems, or mount the given filesystem.
3239 zfs_do_mount(int argc, char **argv)
3241 return (share_mount(OP_MOUNT, argc, argv));
3245 * zfs share -a [nfs | iscsi | smb]
3246 * zfs share filesystem
3248 * Share all filesystems, or share the given filesystem.
3251 zfs_do_share(int argc, char **argv)
3253 return (share_mount(OP_SHARE, argc, argv));
3256 typedef struct unshare_unmount_node {
3257 zfs_handle_t *un_zhp;
3259 uu_avl_node_t un_avlnode;
3260 } unshare_unmount_node_t;
3264 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3266 const unshare_unmount_node_t *l = larg;
3267 const unshare_unmount_node_t *r = rarg;
3269 return (strcmp(l->un_mountp, r->un_mountp));
3273 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3274 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3275 * and unmount it appropriately.
3278 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3282 struct stat64 statbuf;
3283 struct mnttab search = { 0 }, entry;
3284 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3288 * Search for the path in /etc/mnttab. Rather than looking for the
3289 * specific path, which can be fooled by non-standard paths (i.e. ".."
3290 * or "//"), we stat() the path and search for the corresponding
3291 * (major,minor) device pair.
3293 if (stat64(path, &statbuf) != 0) {
3294 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3295 cmdname, path, strerror(errno));
3298 path_inode = statbuf.st_ino;
3301 * Search for the given (major,minor) pair in the mount table.
3303 search.mnt_mountp = path;
3304 rewind(mnttab_file);
3305 if (getmntany(mnttab_file, &entry, &search) != 0) {
3306 if (op == OP_SHARE) {
3307 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3308 "currently mounted\n"), cmdname, path);
3311 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3313 if ((ret = umount2(path, flags)) != 0)
3314 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3319 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3320 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3321 "filesystem\n"), cmdname, path);
3325 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3326 ZFS_TYPE_FILESYSTEM)) == NULL)
3330 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3331 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3332 cmdname, path, strerror(errno));
3334 } else if (statbuf.st_ino != path_inode) {
3335 (void) fprintf(stderr, gettext("cannot "
3336 "%s '%s': not a mountpoint\n"), cmdname, path);
3340 if (op == OP_SHARE) {
3341 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3342 char smbshare_prop[ZFS_MAXPROPLEN];
3344 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3345 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3346 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3347 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3349 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3350 strcmp(smbshare_prop, "off") == 0) {
3351 (void) fprintf(stderr, gettext("cannot unshare "
3352 "'%s': legacy share\n"), path);
3353 (void) fprintf(stderr, gettext("use "
3354 "unshare(1M) to unshare this filesystem\n"));
3355 } else if (!zfs_is_shared(zhp)) {
3356 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3357 "not currently shared\n"), path);
3359 ret = zfs_unshareall_bypath(zhp, path);
3362 char mtpt_prop[ZFS_MAXPROPLEN];
3364 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3365 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3368 ret = zfs_unmount(zhp, NULL, flags);
3369 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3370 (void) fprintf(stderr, gettext("cannot unmount "
3371 "'%s': legacy mountpoint\n"),
3373 (void) fprintf(stderr, gettext("use umount(1M) "
3374 "to unmount this filesystem\n"));
3376 ret = zfs_unmountall(zhp, flags);
3387 * Generic callback for unsharing or unmounting a filesystem.
3390 unshare_unmount(int op, int argc, char **argv)
3397 char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3398 char sharesmb[ZFS_MAXPROPLEN];
3401 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3410 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3421 * We could make use of zfs_for_each() to walk all datasets in
3422 * the system, but this would be very inefficient, especially
3423 * since we would have to linearly search /etc/mnttab for each
3424 * one. Instead, do one pass through /etc/mnttab looking for
3425 * zfs entries and call zfs_unmount() for each one.
3427 * Things get a little tricky if the administrator has created
3428 * mountpoints beneath other ZFS filesystems. In this case, we
3429 * have to unmount the deepest filesystems first. To accomplish
3430 * this, we place all the mountpoints in an AVL tree sorted by
3431 * the special type (dataset name), and walk the result in
3432 * reverse to make sure to get any snapshots first.
3434 uu_avl_pool_t *pool;
3436 unshare_unmount_node_t *node;
3438 uu_avl_walk_t *walk;
3443 (void) fprintf(stderr, gettext("too many arguments\n"));
3447 if ((pool = uu_avl_pool_create("unmount_pool",
3448 sizeof (unshare_unmount_node_t),
3449 offsetof(unshare_unmount_node_t, un_avlnode),
3450 unshare_unmount_compare,
3451 UU_DEFAULT)) == NULL) {
3452 (void) fprintf(stderr, gettext("internal error: "
3453 "out of memory\n"));
3457 if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3458 (void) fprintf(stderr, gettext("internal error: "
3459 "out of memory\n"));
3463 if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
3464 (void) fprintf(stderr, gettext("internal error: "
3465 "getmntinfo() failed\n"));
3468 for (i = 0; i < n; i++) {
3470 /* ignore non-ZFS entries */
3471 if (strcmp(sfs[i].f_fstypename, MNTTYPE_ZFS) != 0)
3474 /* ignore snapshots */
3475 if (strchr(sfs[i].f_mntfromname, '@') != NULL)
3478 if ((zhp = zfs_open(g_zfs, sfs[i].f_mntfromname,
3479 ZFS_TYPE_FILESYSTEM)) == NULL) {
3486 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3488 sizeof (nfsiscsi_mnt_prop),
3489 NULL, NULL, 0, B_FALSE) == 0);
3490 if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3492 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3494 sizeof (nfsiscsi_mnt_prop),
3495 NULL, NULL, 0, B_FALSE) == 0);
3496 if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3500 /* Ignore legacy mounts */
3501 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3503 sizeof (nfsiscsi_mnt_prop),
3504 NULL, NULL, 0, B_FALSE) == 0);
3505 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3507 /* Ignore canmount=noauto mounts */
3508 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3509 ZFS_CANMOUNT_NOAUTO)
3515 node = safe_malloc(sizeof (unshare_unmount_node_t));
3518 if ((node->un_mountp = strdup(sfs[i].f_mntonname)) ==
3520 (void) fprintf(stderr, gettext("internal error:"
3521 " out of memory\n"));
3525 uu_avl_node_init(node, &node->un_avlnode, pool);
3527 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3528 uu_avl_insert(tree, node, idx);
3530 zfs_close(node->un_zhp);
3531 free(node->un_mountp);
3537 * Walk the AVL tree in reverse, unmounting each filesystem and
3538 * removing it from the AVL tree in the process.
3540 if ((walk = uu_avl_walk_start(tree,
3541 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3542 (void) fprintf(stderr,
3543 gettext("internal error: out of memory"));
3547 while ((node = uu_avl_walk_next(walk)) != NULL) {
3548 uu_avl_remove(tree, node);
3552 if (zfs_unshareall_bypath(node->un_zhp,
3553 node->un_mountp) != 0)
3558 if (zfs_unmount(node->un_zhp,
3559 node->un_mountp, flags) != 0)
3564 zfs_close(node->un_zhp);
3565 free(node->un_mountp);
3569 uu_avl_walk_end(walk);
3570 uu_avl_destroy(tree);
3571 uu_avl_pool_destroy(pool);
3573 if (op == OP_SHARE) {
3575 * Finally, unshare any volumes shared via iSCSI.
3577 zfs_handle_t **dslist = NULL;
3578 size_t i, count = 0;
3580 get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
3584 qsort(dslist, count, sizeof (void *),
3587 for (i = 0; i < count; i++) {
3588 if (zfs_unshare_iscsi(dslist[i]) != 0)
3590 zfs_close(dslist[i]);
3599 (void) fprintf(stderr,
3600 gettext("missing filesystem argument\n"));
3602 (void) fprintf(stderr,
3603 gettext("too many arguments\n"));
3608 * We have an argument, but it may be a full path or a ZFS
3609 * filesystem. Pass full paths off to unmount_path() (shared by
3610 * manual_unmount), otherwise open the filesystem and pass to
3613 if (argv[0][0] == '/')
3614 return (unshare_unmount_path(op, argv[0],
3617 types = ZFS_TYPE_FILESYSTEM;
3619 types |= ZFS_TYPE_VOLUME;
3621 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3624 if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
3625 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3626 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3627 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3628 NULL, 0, B_FALSE) == 0);
3632 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3634 sizeof (nfsiscsi_mnt_prop),
3635 NULL, NULL, 0, B_FALSE) == 0);
3636 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3637 sharesmb, sizeof (sharesmb), NULL, NULL,
3640 if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3641 strcmp(sharesmb, "off") == 0) {
3642 (void) fprintf(stderr, gettext("cannot "
3643 "unshare '%s': legacy share\n"),
3645 (void) fprintf(stderr, gettext("use "
3646 "unshare(1M) to unshare this "
3649 } else if (!zfs_is_shared(zhp)) {
3650 (void) fprintf(stderr, gettext("cannot "
3651 "unshare '%s': not currently "
3652 "shared\n"), zfs_get_name(zhp));
3654 } else if (zfs_unshareall(zhp) != 0) {
3660 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3661 (void) fprintf(stderr, gettext("cannot "
3662 "unmount '%s': legacy "
3663 "mountpoint\n"), zfs_get_name(zhp));
3664 (void) fprintf(stderr, gettext("use "
3665 "umount(1M) to unmount this "
3668 } else if (!zfs_is_mounted(zhp, NULL)) {
3669 (void) fprintf(stderr, gettext("cannot "
3670 "unmount '%s': not currently "
3674 } else if (zfs_unmountall(zhp, flags) != 0) {
3680 assert(op == OP_SHARE);
3682 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3683 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3684 NULL, NULL, 0, B_FALSE) == 0);
3686 if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
3687 (void) fprintf(stderr, gettext("cannot unshare "
3688 "'%s': 'shareiscsi' property not set\n"),
3690 (void) fprintf(stderr, gettext("set "
3691 "'shareiscsi' property or use "
3692 "iscsitadm(1M) to share this volume\n"));
3694 } else if (!zfs_is_shared_iscsi(zhp)) {
3695 (void) fprintf(stderr, gettext("cannot "
3696 "unshare '%s': not currently shared\n"),
3699 } else if (zfs_unshare_iscsi(zhp) != 0) {
3712 * zfs unmount filesystem
3714 * Unmount all filesystems, or a specific ZFS filesystem.
3717 zfs_do_unmount(int argc, char **argv)
3719 return (unshare_unmount(OP_MOUNT, argc, argv));
3724 * zfs unshare filesystem
3726 * Unshare all filesystems, or a specific ZFS filesystem.
3729 zfs_do_unshare(int argc, char **argv)
3731 return (unshare_unmount(OP_SHARE, argc, argv));
3735 * Attach/detach the given dataset to/from the given jail
3739 do_jail(int argc, char **argv, int attach)
3744 /* check number of arguments */
3746 (void) fprintf(stderr, gettext("missing argument(s)\n"));
3750 (void) fprintf(stderr, gettext("too many arguments\n"));
3754 jailid = atoi(argv[1]);
3756 (void) fprintf(stderr, gettext("invalid jailid\n"));
3760 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
3764 ret = (zfs_jail(zhp, jailid, attach) != 0);
3771 * zfs jail jailid filesystem
3773 * Attach the given dataset to the given jail
3777 zfs_do_jail(int argc, char **argv)
3780 return (do_jail(argc, argv, 1));
3784 * zfs unjail jailid filesystem
3786 * Detach the given dataset from the given jail
3790 zfs_do_unjail(int argc, char **argv)
3793 return (do_jail(argc, argv, 0));
3798 zfs_do_python(int argc, char **argv)
3800 (void) execv(pypath, argv-1);
3801 (void) fprintf(stderr, "internal error: %s not found\n", pypath);
3802 (void) fprintf(stderr, " install sysutils/py-zfs port to correct this\n");
3807 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
3808 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
3811 manual_mount(int argc, char **argv)
3814 char mountpoint[ZFS_MAXPROPLEN];
3815 char mntopts[MNT_LINE_MAX] = { '\0' };
3819 char *dataset, *path;
3822 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3825 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
3828 #if 0 /* FreeBSD: No support for MS_OVERLAY. */
3829 flags |= MS_OVERLAY;
3833 #if 0 /* FreeBSD: No support for MS_NOMNTTAB. */
3834 flags |= MS_NOMNTTAB;
3838 (void) fprintf(stderr, gettext("missing argument for "
3839 "'%c' option\n"), optopt);
3843 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3845 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
3854 /* check that we only have two arguments */
3857 (void) fprintf(stderr, gettext("missing dataset "
3860 (void) fprintf(stderr,
3861 gettext("missing mountpoint argument\n"));
3863 (void) fprintf(stderr, gettext("too many arguments\n"));
3864 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3871 /* try to open the dataset */
3872 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3875 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3876 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3878 /* check for legacy mountpoint and complain appropriately */
3880 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
3881 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
3882 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
3883 (void) fprintf(stderr, gettext("mount failed: %s\n"),
3888 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
3889 "mounted using 'mount -F zfs'\n"), dataset);
3890 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
3891 "instead.\n"), path);
3892 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
3893 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
3894 (void) fprintf(stderr, gettext("See zfs(1M) for more "
3903 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
3904 * unmounts of non-legacy filesystems, as this is the dominant administrative
3908 manual_unmount(int argc, char **argv)
3914 while ((c = getopt(argc, argv, "f")) != -1) {
3920 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3922 (void) fprintf(stderr, gettext("usage: unmount [-f] "
3931 /* check arguments */
3934 (void) fprintf(stderr, gettext("missing path "
3937 (void) fprintf(stderr, gettext("too many arguments\n"));
3938 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
3942 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
3946 volcheck(zpool_handle_t *zhp, void *data)
3948 boolean_t isinit = *((boolean_t *)data);
3951 return (zpool_create_zvol_links(zhp));
3953 return (zpool_remove_zvol_links(zhp));
3957 * Iterate over all pools in the system and either create or destroy /dev/zvol
3958 * links, depending on the value of 'isinit'.
3961 do_volcheck(boolean_t isinit)
3963 return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
3967 find_command_idx(char *command, int *idx)
3971 for (i = 0; i < NCOMMAND; i++) {
3972 if (command_table[i].name == NULL)
3975 if (strcmp(command, command_table[i].name) == 0) {
3984 main(int argc, char **argv)
3991 (void) setlocale(LC_ALL, "");
3992 (void) textdomain(TEXT_DOMAIN);
3996 if ((g_zfs = libzfs_init()) == NULL) {
3997 (void) fprintf(stderr, gettext("internal error: failed to "
3998 "initialize ZFS library\n"));
4002 zpool_set_history_str("zfs", argc, argv, history_str);
4003 verify(zpool_stage_history(g_zfs, history_str) == 0);
4005 libzfs_print_on_error(g_zfs, B_TRUE);
4007 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4008 (void) fprintf(stderr, gettext("internal error: unable to "
4009 "open %s\n"), MNTTAB);
4014 * This command also doubles as the /etc/fs mount and unmount program.
4015 * Determine if we should take this behavior based on argv[0].
4017 progname = basename(argv[0]);
4018 if (strcmp(progname, "mount") == 0) {
4019 ret = manual_mount(argc, argv);
4020 } else if (strcmp(progname, "umount") == 0) {
4021 ret = manual_unmount(argc, argv);
4024 * Make sure the user has specified some command.
4027 (void) fprintf(stderr, gettext("missing command\n"));
4034 * The 'umount' command is an alias for 'unmount'
4036 if (strcmp(cmdname, "umount") == 0)
4037 cmdname = "unmount";
4040 * The 'recv' command is an alias for 'receive'
4042 if (strcmp(cmdname, "recv") == 0)
4043 cmdname = "receive";
4048 if (strcmp(cmdname, "-?") == 0)
4052 * 'volinit' and 'volfini' do not appear in the usage message,
4053 * so we have to special case them here.
4055 if (strcmp(cmdname, "volinit") == 0)
4056 return (do_volcheck(B_TRUE));
4057 else if (strcmp(cmdname, "volfini") == 0)
4058 return (do_volcheck(B_FALSE));
4061 * Run the appropriate command.
4063 libzfs_mnttab_cache(g_zfs, B_TRUE);
4064 if (find_command_idx(cmdname, &i) == 0) {
4065 current_command = &command_table[i];
4066 ret = command_table[i].func(argc - 1, argv + 1);
4067 } else if (strchr(cmdname, '=') != NULL) {
4068 verify(find_command_idx("set", &i) == 0);
4069 current_command = &command_table[i];
4070 ret = command_table[i].func(argc, argv);
4072 (void) fprintf(stderr, gettext("unrecognized "
4073 "command '%s'\n"), cmdname);
4076 libzfs_mnttab_cache(g_zfs, B_FALSE);
4079 (void) fclose(mnttab_file);
4084 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4085 * for the purposes of running ::findleaks.
4087 if (getenv("ZFS_ABORT") != NULL) {
4088 (void) printf("dumping core by request\n");