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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
32 #include <libnvpair.h>
44 #include <sys/mkdev.h>
45 #include <sys/mntent.h>
46 #include <sys/mnttab.h>
47 #include <sys/mount.h>
49 #include <sys/fs/zfs.h>
50 #include <sys/types.h>
58 #include "zfs_comutil.h"
60 libzfs_handle_t *g_zfs;
62 static FILE *mnttab_file;
63 static char history_str[HIS_MAX_RECORD_LEN];
64 const char *pypath = "/usr/lib/zfs/pyzfs.py";
66 static int zfs_do_clone(int argc, char **argv);
67 static int zfs_do_create(int argc, char **argv);
68 static int zfs_do_destroy(int argc, char **argv);
69 static int zfs_do_get(int argc, char **argv);
70 static int zfs_do_inherit(int argc, char **argv);
71 static int zfs_do_list(int argc, char **argv);
72 static int zfs_do_mount(int argc, char **argv);
73 static int zfs_do_rename(int argc, char **argv);
74 static int zfs_do_rollback(int argc, char **argv);
75 static int zfs_do_set(int argc, char **argv);
76 static int zfs_do_upgrade(int argc, char **argv);
77 static int zfs_do_snapshot(int argc, char **argv);
78 static int zfs_do_unmount(int argc, char **argv);
79 static int zfs_do_share(int argc, char **argv);
80 static int zfs_do_unshare(int argc, char **argv);
81 static int zfs_do_send(int argc, char **argv);
82 static int zfs_do_receive(int argc, char **argv);
83 static int zfs_do_promote(int argc, char **argv);
84 static int zfs_do_userspace(int argc, char **argv);
85 static int zfs_do_python(int argc, char **argv);
86 static int zfs_do_hold(int argc, char **argv);
87 static int zfs_do_release(int argc, char **argv);
88 static int zfs_do_diff(int argc, char **argv);
91 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
96 _umem_debug_init(void)
98 return ("default,verbose"); /* $UMEM_DEBUG setting */
102 _umem_logging_init(void)
104 return ("fail,contents"); /* $UMEM_LOGGING setting */
137 typedef struct zfs_command {
139 int (*func)(int argc, char **argv);
144 * Master command table. Each ZFS command has a name, associated function, and
145 * usage message. The usage messages need to be internationalized, so we have
146 * to have a function to return the usage message based on a command index.
148 * These commands are organized according to how they are displayed in the usage
149 * message. An empty command (one with a NULL name) indicates an empty line in
150 * the generic usage message.
152 static zfs_command_t command_table[] = {
153 { "create", zfs_do_create, HELP_CREATE },
154 { "destroy", zfs_do_destroy, HELP_DESTROY },
156 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
157 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
158 { "clone", zfs_do_clone, HELP_CLONE },
159 { "promote", zfs_do_promote, HELP_PROMOTE },
160 { "rename", zfs_do_rename, HELP_RENAME },
162 { "list", zfs_do_list, HELP_LIST },
164 { "set", zfs_do_set, HELP_SET },
165 { "get", zfs_do_get, HELP_GET },
166 { "inherit", zfs_do_inherit, HELP_INHERIT },
167 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
168 { "userspace", zfs_do_userspace, HELP_USERSPACE },
169 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
171 { "mount", zfs_do_mount, HELP_MOUNT },
172 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
173 { "share", zfs_do_share, HELP_SHARE },
174 { "unshare", zfs_do_unshare, HELP_UNSHARE },
176 { "send", zfs_do_send, HELP_SEND },
177 { "receive", zfs_do_receive, HELP_RECEIVE },
179 { "allow", zfs_do_python, HELP_ALLOW },
181 { "unallow", zfs_do_python, HELP_UNALLOW },
183 { "hold", zfs_do_hold, HELP_HOLD },
184 { "holds", zfs_do_python, HELP_HOLDS },
185 { "release", zfs_do_release, HELP_RELEASE },
186 { "diff", zfs_do_diff, HELP_DIFF },
189 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
191 zfs_command_t *current_command;
194 get_usage(zfs_help_t idx)
198 return (gettext("\tclone [-p] [-o property=value] ... "
199 "<snapshot> <filesystem|volume>\n"));
201 return (gettext("\tcreate [-p] [-o property=value] ... "
203 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
204 "-V <size> <volume>\n"));
206 return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
207 "\tdestroy [-rRd] <snapshot>\n"));
209 return (gettext("\tget [-rHp] [-d max] "
210 "[-o \"all\" | field[,...]] [-s source[,...]]\n"
211 "\t <\"all\" | property[,...]> "
212 "[filesystem|volume|snapshot] ...\n"));
214 return (gettext("\tinherit [-rS] <property> "
215 "<filesystem|volume|snapshot> ...\n"));
217 return (gettext("\tupgrade [-v]\n"
218 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
220 return (gettext("\tlist [-rH][-d max] "
221 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
222 "\t [-S property] ... "
223 "[filesystem|volume|snapshot] ...\n"));
225 return (gettext("\tmount\n"
226 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
228 return (gettext("\tpromote <clone-filesystem>\n"));
230 return (gettext("\treceive [-vnFu] <filesystem|volume|"
232 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
234 return (gettext("\trename <filesystem|volume|snapshot> "
235 "<filesystem|volume|snapshot>\n"
236 "\trename -p <filesystem|volume> <filesystem|volume>\n"
237 "\trename -r <snapshot> <snapshot>"));
239 return (gettext("\trollback [-rRf] <snapshot>\n"));
241 return (gettext("\tsend [-RDp] [-[iI] snapshot] <snapshot>\n"));
243 return (gettext("\tset <property=value> "
244 "<filesystem|volume|snapshot> ...\n"));
246 return (gettext("\tshare <-a | filesystem>\n"));
248 return (gettext("\tsnapshot [-r] [-o property=value] ... "
249 "<filesystem@snapname|volume@snapname>\n"));
251 return (gettext("\tunmount [-f] "
252 "<-a | filesystem|mountpoint>\n"));
254 return (gettext("\tunshare "
255 "<-a | filesystem|mountpoint>\n"));
257 return (gettext("\tallow <filesystem|volume>\n"
259 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
260 "\t <filesystem|volume>\n"
261 "\tallow [-ld] -e <perm|@setname>[,...] "
262 "<filesystem|volume>\n"
263 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
264 "\tallow -s @setname <perm|@setname>[,...] "
265 "<filesystem|volume>\n"));
267 return (gettext("\tunallow [-rldug] "
268 "<\"everyone\"|user|group>[,...]\n"
269 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
270 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
271 "<filesystem|volume>\n"
272 "\tunallow [-r] -c [<perm|@setname>[,...]] "
273 "<filesystem|volume>\n"
274 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
275 "<filesystem|volume>\n"));
277 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
278 "[-sS field] ... [-t type[,...]]\n"
279 "\t <filesystem|snapshot>\n"));
280 case HELP_GROUPSPACE:
281 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
282 "[-sS field] ... [-t type[,...]]\n"
283 "\t <filesystem|snapshot>\n"));
285 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
287 return (gettext("\tholds [-r] <snapshot> ...\n"));
289 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
291 return (gettext("\tdiff [-FHt] <snapshot> "
292 "[snapshot|filesystem]\n"));
302 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
307 * Utility function to guarantee malloc() success.
311 safe_malloc(size_t size)
315 if ((data = calloc(1, size)) == NULL)
323 safe_strdup(char *str)
325 char *dupstr = strdup(str);
332 #endif /* HAVE_ZPL */
335 * Callback routine that will print out information for each of
339 usage_prop_cb(int prop, void *cb)
343 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
345 if (zfs_prop_readonly(prop))
346 (void) fprintf(fp, " NO ");
348 (void) fprintf(fp, "YES ");
350 if (zfs_prop_inheritable(prop))
351 (void) fprintf(fp, " YES ");
353 (void) fprintf(fp, " NO ");
355 if (zfs_prop_values(prop) == NULL)
356 (void) fprintf(fp, "-\n");
358 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
364 * Display usage message. If we're inside a command, display only the usage for
365 * that command. Otherwise, iterate over the entire command table and display
366 * a complete usage message.
369 usage(boolean_t requested)
372 boolean_t show_properties = B_FALSE;
373 FILE *fp = requested ? stdout : stderr;
375 if (current_command == NULL) {
377 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
379 gettext("where 'command' is one of the following:\n\n"));
381 for (i = 0; i < NCOMMAND; i++) {
382 if (command_table[i].name == NULL)
383 (void) fprintf(fp, "\n");
385 (void) fprintf(fp, "%s",
386 get_usage(command_table[i].usage));
389 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
390 "pool/[dataset/]*dataset[@name]\n"));
392 (void) fprintf(fp, gettext("usage:\n"));
393 (void) fprintf(fp, "%s", get_usage(current_command->usage));
396 if (current_command != NULL &&
397 (strcmp(current_command->name, "set") == 0 ||
398 strcmp(current_command->name, "get") == 0 ||
399 strcmp(current_command->name, "inherit") == 0 ||
400 strcmp(current_command->name, "list") == 0))
401 show_properties = B_TRUE;
403 if (show_properties) {
405 gettext("\nThe following properties are supported:\n"));
407 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
408 "PROPERTY", "EDIT", "INHERIT", "VALUES");
410 /* Iterate over all properties */
411 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
414 (void) fprintf(fp, "\t%-15s ", "userused@...");
415 (void) fprintf(fp, " NO NO <size>\n");
416 (void) fprintf(fp, "\t%-15s ", "groupused@...");
417 (void) fprintf(fp, " NO NO <size>\n");
418 (void) fprintf(fp, "\t%-15s ", "userquota@...");
419 (void) fprintf(fp, "YES NO <size> | none\n");
420 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
421 (void) fprintf(fp, "YES NO <size> | none\n");
423 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
424 "with standard units such as K, M, G, etc.\n"));
425 (void) fprintf(fp, gettext("\nUser-defined properties can "
426 "be specified by using a name containing a colon (:).\n"));
427 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
428 "properties must be appended with\n"
429 "a user or group specifier of one of these forms:\n"
430 " POSIX name (eg: \"matt\")\n"
431 " POSIX id (eg: \"126829\")\n"
432 " SMB name@domain (eg: \"matt@sun\")\n"
433 " SMB SID (eg: \"S-1-234-567-89\")\n"));
436 gettext("\nFor the property list, run: %s\n"),
439 gettext("\nFor the delegated permission list, run: %s\n"),
440 "zfs allow|unallow");
444 * See comments at end of main().
446 if (getenv("ZFS_ABORT") != NULL) {
447 (void) printf("dumping core by request\n");
451 exit(requested ? 0 : 2);
455 parseprop(nvlist_t *props)
457 char *propname = optarg;
458 char *propval, *strval;
460 if ((propval = strchr(propname, '=')) == NULL) {
461 (void) fprintf(stderr, gettext("missing "
462 "'=' for -o option\n"));
467 if (nvlist_lookup_string(props, propname, &strval) == 0) {
468 (void) fprintf(stderr, gettext("property '%s' "
469 "specified multiple times\n"), propname);
472 if (nvlist_add_string(props, propname, propval) != 0)
478 parse_depth(char *opt, int *flags)
483 depth = (int)strtol(opt, &tmp, 0);
485 (void) fprintf(stderr,
486 gettext("%s is not an integer\n"), optarg);
490 (void) fprintf(stderr,
491 gettext("Depth can not be negative.\n"));
494 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
498 #define PROGRESS_DELAY 2 /* seconds */
501 static char *pt_reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
502 static time_t pt_begin;
503 static char *pt_header = NULL;
504 static boolean_t pt_shown;
507 start_progress_timer(void)
509 pt_begin = time(NULL) + PROGRESS_DELAY;
514 set_progress_header(char *header)
516 assert(pt_header == NULL);
517 pt_header = safe_strdup(header);
519 (void) printf("%s: ", header);
520 (void) fflush(stdout);
525 update_progress(char *update)
527 if (!pt_shown && time(NULL) > pt_begin) {
528 int len = strlen(update);
530 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
532 (void) fflush(stdout);
534 } else if (pt_shown) {
535 int len = strlen(update);
537 (void) printf("%s%*.*s", update, len, len, pt_reverse);
538 (void) fflush(stdout);
543 finish_progress(char *done)
546 (void) printf("%s\n", done);
547 (void) fflush(stdout);
552 #endif /* HAVE_ZPL */
555 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
557 * Given an existing dataset, create a writable copy whose initial contents
558 * are the same as the source. The newly created dataset maintains a
559 * dependency on the original; the original cannot be destroyed so long as
562 * The '-p' flag creates all the non-existing ancestors of the target first.
565 zfs_do_clone(int argc, char **argv)
567 zfs_handle_t *zhp = NULL;
568 boolean_t parents = B_FALSE;
573 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
577 while ((c = getopt(argc, argv, "o:p")) != -1) {
580 if (parseprop(props))
587 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
596 /* check number of arguments */
598 (void) fprintf(stderr, gettext("missing source dataset "
603 (void) fprintf(stderr, gettext("missing target dataset "
608 (void) fprintf(stderr, gettext("too many arguments\n"));
612 /* open the source dataset */
613 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
616 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
619 * Now create the ancestors of the target dataset. If the
620 * target already exists and '-p' option was used we should not
623 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
626 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
631 ret = zfs_clone(zhp, argv[1], props);
633 /* create the mountpoint if necessary */
638 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
640 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
641 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
642 ret = zfs_share(clone);
646 #endif /* HAVE_ZPL */
662 * zfs create [-p] [-o prop=value] ... fs
663 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
665 * Create a new dataset. This command can be used to create filesystems
666 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
667 * For volumes, the user must specify a size to be used.
669 * The '-s' flag applies only to volumes, and indicates that we should not try
670 * to set the reservation for this volume. By default we set a reservation
671 * equal to the size for any volume. For pools with SPA_VERSION >=
672 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
674 * The '-p' flag creates all the non-existing ancestors of the target first.
677 zfs_do_create(int argc, char **argv)
679 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
680 zfs_handle_t *zhp = NULL;
681 uint64_t volsize = 0;
683 boolean_t noreserve = B_FALSE;
684 boolean_t bflag = B_FALSE;
685 boolean_t parents = B_FALSE;
689 int canmount = ZFS_CANMOUNT_OFF;
691 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
695 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
698 type = ZFS_TYPE_VOLUME;
699 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
700 (void) fprintf(stderr, gettext("bad volume "
701 "size '%s': %s\n"), optarg,
702 libzfs_error_description(g_zfs));
706 if (nvlist_add_uint64(props,
707 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
716 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
717 (void) fprintf(stderr, gettext("bad volume "
718 "block size '%s': %s\n"), optarg,
719 libzfs_error_description(g_zfs));
723 if (nvlist_add_uint64(props,
724 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
729 if (parseprop(props))
736 (void) fprintf(stderr, gettext("missing size "
741 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
747 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
748 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
749 "used when creating a volume\n"));
756 /* check number of arguments */
758 (void) fprintf(stderr, gettext("missing %s argument\n"),
759 zfs_type_to_name(type));
763 (void) fprintf(stderr, gettext("too many arguments\n"));
767 if (type == ZFS_TYPE_VOLUME && !noreserve) {
768 zpool_handle_t *zpool_handle;
769 uint64_t spa_version;
771 zfs_prop_t resv_prop;
774 if ((p = strchr(argv[0], '/')))
776 zpool_handle = zpool_open(g_zfs, argv[0]);
779 if (zpool_handle == NULL)
781 spa_version = zpool_get_prop_int(zpool_handle,
782 ZPOOL_PROP_VERSION, NULL);
783 zpool_close(zpool_handle);
784 if (spa_version >= SPA_VERSION_REFRESERVATION)
785 resv_prop = ZFS_PROP_REFRESERVATION;
787 resv_prop = ZFS_PROP_RESERVATION;
788 volsize = zvol_volsize_to_reservation(volsize, props);
790 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
792 if (nvlist_add_uint64(props,
793 zfs_prop_to_name(resv_prop), volsize) != 0) {
800 if (parents && zfs_name_valid(argv[0], type)) {
802 * Now create the ancestors of target dataset. If the target
803 * already exists and '-p' option was used we should not
806 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
810 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
815 if (zfs_create(g_zfs, argv[0], type, props) != 0)
818 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
823 * if the user doesn't want the dataset automatically mounted,
824 * then skip the mount/share step
826 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
827 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
830 * Mount and/or share the new filesystem as appropriate. We provide a
831 * verbose error message to let the user know that their filesystem was
832 * in fact created, even if we failed to mount or share it.
835 if (canmount == ZFS_CANMOUNT_ON) {
836 if (zfs_mount(zhp, NULL, 0) != 0) {
837 (void) fprintf(stderr, gettext("filesystem "
838 "successfully created, but not mounted\n"));
840 } else if (zfs_share(zhp) != 0) {
841 (void) fprintf(stderr, gettext("filesystem "
842 "successfully created, but not shared\n"));
846 #endif /* HAVE_ZPL */
860 * zfs destroy [-rRf] <fs, vol>
861 * zfs destroy [-rRd] <snap>
863 * -r Recursively destroy all children
864 * -R Recursively destroy all dependents, including clones
865 * -f Force unmounting of any dependents
866 * -d If we can't destroy now, mark for deferred destruction
868 * Destroys the given dataset. By default, it will unmount any filesystems,
869 * and refuse to destroy a dataset that has any dependents. A dependent can
870 * either be a child, or a clone of a child.
872 typedef struct destroy_cbdata {
879 boolean_t cb_closezhp;
880 zfs_handle_t *cb_target;
882 boolean_t cb_defer_destroy;
886 * Check for any dependents based on the '-r' or '-R' flags.
889 destroy_check_dependent(zfs_handle_t *zhp, void *data)
891 destroy_cbdata_t *cbp = data;
892 const char *tname = zfs_get_name(cbp->cb_target);
893 const char *name = zfs_get_name(zhp);
895 if (strncmp(tname, name, strlen(tname)) == 0 &&
896 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
898 * This is a direct descendant, not a clone somewhere else in
905 (void) fprintf(stderr, gettext("cannot destroy '%s': "
906 "%s has children\n"),
907 zfs_get_name(cbp->cb_target),
908 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
909 (void) fprintf(stderr, gettext("use '-r' to destroy "
910 "the following datasets:\n"));
911 cbp->cb_first = B_FALSE;
915 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
918 * This is a clone. We only want to report this if the '-r'
919 * wasn't specified, or the target is a snapshot.
921 if (!cbp->cb_recurse &&
922 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
926 (void) fprintf(stderr, gettext("cannot destroy '%s': "
927 "%s has dependent clones\n"),
928 zfs_get_name(cbp->cb_target),
929 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
930 (void) fprintf(stderr, gettext("use '-R' to destroy "
931 "the following datasets:\n"));
932 cbp->cb_first = B_FALSE;
936 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
945 destroy_callback(zfs_handle_t *zhp, void *data)
947 destroy_cbdata_t *cbp = data;
950 * Ignore pools (which we've already flagged as an error before getting
953 if (strchr(zfs_get_name(zhp), '/') == NULL &&
954 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
960 * Bail out on the first error.
962 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
963 zfs_destroy(zhp, cbp->cb_defer_destroy) != 0) {
973 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
975 destroy_cbdata_t *cbp = arg;
976 char thissnap[MAXPATHLEN];
978 boolean_t closezhp = cbp->cb_closezhp;
981 (void) snprintf(thissnap, sizeof (thissnap),
982 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
984 libzfs_print_on_error(g_zfs, B_FALSE);
985 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
986 libzfs_print_on_error(g_zfs, B_TRUE);
989 * Destroy any clones of this snapshot
991 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
1001 cbp->cb_closezhp = B_TRUE;
1002 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
1009 zfs_do_destroy(int argc, char **argv)
1011 destroy_cbdata_t cb = { 0 };
1015 zfs_type_t type = ZFS_TYPE_DATASET;
1018 while ((c = getopt(argc, argv, "dfrR")) != -1) {
1021 cb.cb_defer_destroy = B_TRUE;
1022 type = ZFS_TYPE_SNAPSHOT;
1036 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1045 /* check number of arguments */
1047 (void) fprintf(stderr, gettext("missing path argument\n"));
1051 (void) fprintf(stderr, gettext("too many arguments\n"));
1056 * If we are doing recursive destroy of a snapshot, then the
1057 * named snapshot may not exist. Go straight to libzfs.
1059 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
1063 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
1068 if (cb.cb_doclones) {
1069 boolean_t defer = cb.cb_defer_destroy;
1072 * Temporarily ignore the defer_destroy setting since
1073 * it's not supported for clones.
1075 cb.cb_defer_destroy = B_FALSE;
1076 cb.cb_snapname = cp;
1077 if (destroy_snap_clones(zhp, &cb) != 0) {
1081 cb.cb_defer_destroy = defer;
1084 ret = zfs_destroy_snaps(zhp, cp, cb.cb_defer_destroy);
1087 (void) fprintf(stderr,
1088 gettext("no snapshots destroyed\n"));
1093 /* Open the given dataset */
1094 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1100 * Perform an explicit check for pools before going any further.
1102 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1103 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1104 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1105 "operation does not apply to pools\n"),
1107 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1108 "%s' to destroy all datasets in the pool\n"),
1110 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1111 "to destroy the pool itself\n"), zfs_get_name(zhp));
1117 * Check for any dependents and/or clones.
1119 cb.cb_first = B_TRUE;
1120 if (!cb.cb_doclones && !cb.cb_defer_destroy &&
1121 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1127 if (cb.cb_error || (!cb.cb_defer_destroy &&
1128 (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0))) {
1134 * Do the real thing. The callback will close the handle regardless of
1135 * whether it succeeds or not.
1138 if (destroy_callback(zhp, &cb) != 0)
1145 is_recvd_column(zprop_get_cbdata_t *cbp)
1148 zfs_get_column_t col;
1150 for (i = 0; i < ZFS_GET_NCOLS &&
1151 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1152 if (col == GET_COL_RECVD)
1158 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1159 * < all | property[,property]... > < fs | snap | vol > ...
1161 * -r recurse over any child datasets
1162 * -H scripted mode. Headers are stripped, and fields are separated
1163 * by tabs instead of spaces.
1164 * -o Set of fields to display. One of "name,property,value,
1165 * received,source". Default is "name,property,value,source".
1166 * "all" is an alias for all five.
1167 * -s Set of sources to allow. One of
1168 * "local,default,inherited,received,temporary,none". Default is
1170 * -p Display values in parsable (literal) format.
1172 * Prints properties for the given datasets. The user can control which
1173 * columns to display as well as which property types to allow.
1177 * Invoked to display the properties for a single dataset.
1180 get_callback(zfs_handle_t *zhp, void *data)
1182 char buf[ZFS_MAXPROPLEN];
1183 char rbuf[ZFS_MAXPROPLEN];
1184 zprop_source_t sourcetype;
1185 char source[ZFS_MAXNAMELEN];
1186 zprop_get_cbdata_t *cbp = data;
1187 nvlist_t *user_props = zfs_get_user_props(zhp);
1188 zprop_list_t *pl = cbp->cb_proplist;
1192 boolean_t received = is_recvd_column(cbp);
1194 for (; pl != NULL; pl = pl->pl_next) {
1195 char *recvdval = NULL;
1197 * Skip the special fake placeholder. This will also skip over
1198 * the name property when 'all' is specified.
1200 if (pl->pl_prop == ZFS_PROP_NAME &&
1201 pl == cbp->cb_proplist)
1204 if (pl->pl_prop != ZPROP_INVAL) {
1205 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1206 sizeof (buf), &sourcetype, source,
1208 cbp->cb_literal) != 0) {
1211 if (!zfs_prop_valid_for_type(pl->pl_prop,
1212 ZFS_TYPE_DATASET)) {
1213 (void) fprintf(stderr,
1214 gettext("No such property '%s'\n"),
1215 zfs_prop_to_name(pl->pl_prop));
1218 sourcetype = ZPROP_SRC_NONE;
1219 (void) strlcpy(buf, "-", sizeof (buf));
1222 if (received && (zfs_prop_get_recvd(zhp,
1223 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1224 cbp->cb_literal) == 0))
1227 zprop_print_one_property(zfs_get_name(zhp), cbp,
1228 zfs_prop_to_name(pl->pl_prop),
1229 buf, sourcetype, source, recvdval);
1230 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1231 sourcetype = ZPROP_SRC_LOCAL;
1233 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1234 buf, sizeof (buf), cbp->cb_literal) != 0) {
1235 sourcetype = ZPROP_SRC_NONE;
1236 (void) strlcpy(buf, "-", sizeof (buf));
1239 zprop_print_one_property(zfs_get_name(zhp), cbp,
1240 pl->pl_user_prop, buf, sourcetype, source, NULL);
1242 if (nvlist_lookup_nvlist(user_props,
1243 pl->pl_user_prop, &propval) != 0) {
1246 sourcetype = ZPROP_SRC_NONE;
1249 verify(nvlist_lookup_string(propval,
1250 ZPROP_VALUE, &strval) == 0);
1251 verify(nvlist_lookup_string(propval,
1252 ZPROP_SOURCE, &sourceval) == 0);
1254 if (strcmp(sourceval,
1255 zfs_get_name(zhp)) == 0) {
1256 sourcetype = ZPROP_SRC_LOCAL;
1257 } else if (strcmp(sourceval,
1258 ZPROP_SOURCE_VAL_RECVD) == 0) {
1259 sourcetype = ZPROP_SRC_RECEIVED;
1261 sourcetype = ZPROP_SRC_INHERITED;
1262 (void) strlcpy(source,
1263 sourceval, sizeof (source));
1267 if (received && (zfs_prop_get_recvd(zhp,
1268 pl->pl_user_prop, rbuf, sizeof (rbuf),
1269 cbp->cb_literal) == 0))
1272 zprop_print_one_property(zfs_get_name(zhp), cbp,
1273 pl->pl_user_prop, strval, sourcetype,
1282 zfs_do_get(int argc, char **argv)
1284 zprop_get_cbdata_t cb = { 0 };
1285 int i, c, flags = 0;
1286 char *value, *fields;
1289 zprop_list_t fake_name = { 0 };
1292 * Set up default columns and sources.
1294 cb.cb_sources = ZPROP_SRC_ALL;
1295 cb.cb_columns[0] = GET_COL_NAME;
1296 cb.cb_columns[1] = GET_COL_PROPERTY;
1297 cb.cb_columns[2] = GET_COL_VALUE;
1298 cb.cb_columns[3] = GET_COL_SOURCE;
1299 cb.cb_type = ZFS_TYPE_DATASET;
1302 while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1305 cb.cb_literal = B_TRUE;
1308 limit = parse_depth(optarg, &flags);
1311 flags |= ZFS_ITER_RECURSE;
1314 cb.cb_scripted = B_TRUE;
1317 (void) fprintf(stderr, gettext("missing argument for "
1318 "'%c' option\n"), optopt);
1323 * Process the set of columns to display. We zero out
1324 * the structure to give us a blank slate.
1326 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1328 while (*optarg != '\0') {
1329 static char *col_subopts[] =
1330 { "name", "property", "value", "received",
1331 "source", "all", NULL };
1333 if (i == ZFS_GET_NCOLS) {
1334 (void) fprintf(stderr, gettext("too "
1335 "many fields given to -o "
1340 switch (getsubopt(&optarg, col_subopts,
1343 cb.cb_columns[i++] = GET_COL_NAME;
1346 cb.cb_columns[i++] = GET_COL_PROPERTY;
1349 cb.cb_columns[i++] = GET_COL_VALUE;
1352 cb.cb_columns[i++] = GET_COL_RECVD;
1353 flags |= ZFS_ITER_RECVD_PROPS;
1356 cb.cb_columns[i++] = GET_COL_SOURCE;
1360 (void) fprintf(stderr,
1361 gettext("\"all\" conflicts "
1362 "with specific fields "
1363 "given to -o option\n"));
1366 cb.cb_columns[0] = GET_COL_NAME;
1367 cb.cb_columns[1] = GET_COL_PROPERTY;
1368 cb.cb_columns[2] = GET_COL_VALUE;
1369 cb.cb_columns[3] = GET_COL_RECVD;
1370 cb.cb_columns[4] = GET_COL_SOURCE;
1371 flags |= ZFS_ITER_RECVD_PROPS;
1375 (void) fprintf(stderr,
1376 gettext("invalid column name "
1385 while (*optarg != '\0') {
1386 static char *source_subopts[] = {
1387 "local", "default", "inherited",
1388 "received", "temporary", "none",
1391 switch (getsubopt(&optarg, source_subopts,
1394 cb.cb_sources |= ZPROP_SRC_LOCAL;
1397 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1400 cb.cb_sources |= ZPROP_SRC_INHERITED;
1403 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1406 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1409 cb.cb_sources |= ZPROP_SRC_NONE;
1412 (void) fprintf(stderr,
1413 gettext("invalid source "
1421 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1431 (void) fprintf(stderr, gettext("missing property "
1438 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1446 * As part of zfs_expand_proplist(), we keep track of the maximum column
1447 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1448 * need to know the maximum name length. However, the user likely did
1449 * not specify 'name' as one of the properties to fetch, so we need to
1450 * make sure we always include at least this property for
1451 * print_get_headers() to work properly.
1453 if (cb.cb_proplist != NULL) {
1454 fake_name.pl_prop = ZFS_PROP_NAME;
1455 fake_name.pl_width = strlen(gettext("NAME"));
1456 fake_name.pl_next = cb.cb_proplist;
1457 cb.cb_proplist = &fake_name;
1460 cb.cb_first = B_TRUE;
1462 /* run for each object */
1463 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1464 &cb.cb_proplist, limit, get_callback, &cb);
1466 if (cb.cb_proplist == &fake_name)
1467 zprop_free_list(fake_name.pl_next);
1469 zprop_free_list(cb.cb_proplist);
1475 * inherit [-rS] <property> <fs|vol> ...
1477 * -r Recurse over all children
1478 * -S Revert to received value, if any
1480 * For each dataset specified on the command line, inherit the given property
1481 * from its parent. Inheriting a property at the pool level will cause it to
1482 * use the default value. The '-r' flag will recurse over all children, and is
1483 * useful for setting a property on a hierarchy-wide basis, regardless of any
1484 * local modifications for each dataset.
1487 typedef struct inherit_cbdata {
1488 const char *cb_propname;
1489 boolean_t cb_received;
1493 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1495 inherit_cbdata_t *cb = data;
1496 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1499 * If we're doing it recursively, then ignore properties that
1500 * are not valid for this type of dataset.
1502 if (prop != ZPROP_INVAL &&
1503 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1506 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1510 inherit_cb(zfs_handle_t *zhp, void *data)
1512 inherit_cbdata_t *cb = data;
1514 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1518 zfs_do_inherit(int argc, char **argv)
1522 inherit_cbdata_t cb = { 0 };
1526 boolean_t received = B_FALSE;
1529 while ((c = getopt(argc, argv, "rS")) != -1) {
1532 flags |= ZFS_ITER_RECURSE;
1539 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1548 /* check number of arguments */
1550 (void) fprintf(stderr, gettext("missing property argument\n"));
1554 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1562 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1563 if (zfs_prop_readonly(prop)) {
1564 (void) fprintf(stderr, gettext(
1565 "%s property is read-only\n"),
1569 if (!zfs_prop_inheritable(prop) && !received) {
1570 (void) fprintf(stderr, gettext("'%s' property cannot "
1571 "be inherited\n"), propname);
1572 if (prop == ZFS_PROP_QUOTA ||
1573 prop == ZFS_PROP_RESERVATION ||
1574 prop == ZFS_PROP_REFQUOTA ||
1575 prop == ZFS_PROP_REFRESERVATION)
1576 (void) fprintf(stderr, gettext("use 'zfs set "
1577 "%s=none' to clear\n"), propname);
1580 if (received && (prop == ZFS_PROP_VOLSIZE ||
1581 prop == ZFS_PROP_VERSION)) {
1582 (void) fprintf(stderr, gettext("'%s' property cannot "
1583 "be reverted to a received value\n"), propname);
1586 } else if (!zfs_prop_user(propname)) {
1587 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1592 cb.cb_propname = propname;
1593 cb.cb_received = received;
1595 if (flags & ZFS_ITER_RECURSE) {
1596 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1597 NULL, NULL, 0, inherit_recurse_cb, &cb);
1599 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1600 NULL, NULL, 0, inherit_cb, &cb);
1606 typedef struct upgrade_cbdata {
1607 uint64_t cb_numupgraded;
1608 uint64_t cb_numsamegraded;
1609 uint64_t cb_numfailed;
1610 uint64_t cb_version;
1612 boolean_t cb_foundone;
1613 char cb_lastfs[ZFS_MAXNAMELEN];
1617 same_pool(zfs_handle_t *zhp, const char *name)
1619 int len1 = strcspn(name, "/@");
1620 const char *zhname = zfs_get_name(zhp);
1621 int len2 = strcspn(zhname, "/@");
1625 return (strncmp(name, zhname, len1) == 0);
1629 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1631 upgrade_cbdata_t *cb = data;
1632 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1634 /* list if it's old/new */
1635 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1636 (cb->cb_newer && version > ZPL_VERSION)) {
1639 str = gettext("The following filesystems are "
1640 "formatted using a newer software version and\n"
1641 "cannot be accessed on the current system.\n\n");
1643 str = gettext("The following filesystems are "
1644 "out of date, and can be upgraded. After being\n"
1645 "upgraded, these filesystems (and any 'zfs send' "
1646 "streams generated from\n"
1647 "subsequent snapshots) will no longer be "
1648 "accessible by older software versions.\n\n");
1651 if (!cb->cb_foundone) {
1653 (void) printf(gettext("VER FILESYSTEM\n"));
1654 (void) printf(gettext("--- ------------\n"));
1655 cb->cb_foundone = B_TRUE;
1658 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1665 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1667 upgrade_cbdata_t *cb = data;
1668 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1669 int needed_spa_version;
1672 if (zfs_spa_version(zhp, &spa_version) < 0)
1675 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1677 if (needed_spa_version < 0)
1680 if (spa_version < needed_spa_version) {
1682 (void) printf(gettext("%s: can not be "
1683 "upgraded; the pool version needs to first "
1684 "be upgraded\nto version %d\n\n"),
1685 zfs_get_name(zhp), needed_spa_version);
1691 if (version < cb->cb_version) {
1693 (void) snprintf(verstr, sizeof (verstr),
1694 "%llu", (u_longlong_t)cb->cb_version);
1695 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1697 * If they did "zfs upgrade -a", then we could
1698 * be doing ioctls to different pools. We need
1699 * to log this history once to each pool.
1701 verify(zpool_stage_history(g_zfs, history_str) == 0);
1703 if (zfs_prop_set(zhp, "version", verstr) == 0)
1704 cb->cb_numupgraded++;
1707 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1708 } else if (version > cb->cb_version) {
1709 /* can't downgrade */
1710 (void) printf(gettext("%s: can not be downgraded; "
1711 "it is already at version %u\n"),
1712 zfs_get_name(zhp), version);
1715 cb->cb_numsamegraded++;
1723 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1726 zfs_do_upgrade(int argc, char **argv)
1728 boolean_t all = B_FALSE;
1729 boolean_t showversions = B_FALSE;
1731 upgrade_cbdata_t cb = { 0 };
1733 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1736 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1739 flags |= ZFS_ITER_RECURSE;
1742 showversions = B_TRUE;
1745 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1746 optarg, &cb.cb_version) != 0) {
1747 (void) fprintf(stderr,
1748 gettext("invalid version %s\n"), optarg);
1757 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1766 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1768 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1769 cb.cb_version || argc))
1771 if ((all || argc) && (showversions))
1777 /* Show info on available versions. */
1778 (void) printf(gettext("The following filesystem versions are "
1780 (void) printf(gettext("VER DESCRIPTION\n"));
1781 (void) printf("--- -----------------------------------------"
1782 "---------------\n");
1783 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1784 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1785 (void) printf(gettext(" 3 Case insensitive and File system "
1786 "unique identifier (FUID)\n"));
1787 (void) printf(gettext(" 4 userquota, groupquota "
1789 (void) printf(gettext(" 5 System attributes\n"));
1790 (void) printf(gettext("\nFor more information on a particular "
1791 "version, including supported releases,\n"));
1792 (void) printf("see the ZFS Administration Guide.\n\n");
1794 } else if (argc || all) {
1795 /* Upgrade filesystems */
1796 if (cb.cb_version == 0)
1797 cb.cb_version = ZPL_VERSION;
1798 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1799 NULL, NULL, 0, upgrade_set_callback, &cb);
1800 (void) printf(gettext("%llu filesystems upgraded\n"),
1801 (u_longlong_t)cb.cb_numupgraded);
1802 if (cb.cb_numsamegraded) {
1803 (void) printf(gettext("%llu filesystems already at "
1805 (u_longlong_t)cb.cb_numsamegraded);
1807 if (cb.cb_numfailed != 0)
1810 /* List old-version filesytems */
1812 (void) printf(gettext("This system is currently running "
1813 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1815 flags |= ZFS_ITER_RECURSE;
1816 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1817 NULL, NULL, 0, upgrade_list_callback, &cb);
1819 found = cb.cb_foundone;
1820 cb.cb_foundone = B_FALSE;
1821 cb.cb_newer = B_TRUE;
1823 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1824 NULL, NULL, 0, upgrade_list_callback, &cb);
1826 if (!cb.cb_foundone && !found) {
1827 (void) printf(gettext("All filesystems are "
1828 "formatted with the current version.\n"));
1839 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
1841 zfs_userquota_prop_t *typep = arg;
1842 zfs_userquota_prop_t p = *typep;
1844 char *ug, *propname;
1848 if (domain == NULL || domain[0] == '\0') {
1849 if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) {
1850 struct group *g = getgrgid(rid);
1854 struct passwd *p = getpwuid(rid);
1860 if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA)
1865 if (p == ZFS_PROP_USERUSED || p == ZFS_PROP_GROUPUSED)
1871 (void) snprintf(namebuf, sizeof (namebuf),
1872 "%llu", (longlong_t)rid);
1875 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
1877 (void) printf("%s %s %s%c%s %s\n", propname, ug, domain,
1878 domain[0] ? '-' : ' ', name, sizebuf);
1884 zfs_do_userspace(int argc, char **argv)
1887 zfs_userquota_prop_t p;
1891 * Try the python version. If the execv fails, we'll continue
1892 * and do a simplistic implementation.
1894 (void) execv(pypath, argv-1);
1896 (void) printf("internal error: %s not found\n"
1897 "falling back on built-in implementation, "
1898 "some features will not work\n", pypath);
1900 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
1903 (void) printf("PROP TYPE NAME VALUE\n");
1905 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
1906 error = zfs_userspace(zhp, p, userspace_cb, &p);
1914 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
1915 * [-s property [-s property]...] [-S property [-S property]...]
1918 * -r Recurse over all children
1919 * -d Limit recursion by depth.
1920 * -H Scripted mode; elide headers and separate columns by tabs
1921 * -o Control which fields to display.
1922 * -t Control which object types to display.
1923 * -s Specify sort columns, descending order.
1924 * -S Specify sort columns, ascending order.
1926 * When given no arguments, lists all filesystems in the system.
1927 * Otherwise, list the specified datasets, optionally recursing down them if
1928 * '-r' is specified.
1930 typedef struct list_cbdata {
1932 boolean_t cb_scripted;
1933 zprop_list_t *cb_proplist;
1937 * Given a list of columns to display, output appropriate headers for each one.
1940 print_header(zprop_list_t *pl)
1942 char headerbuf[ZFS_MAXPROPLEN];
1945 boolean_t first = B_TRUE;
1946 boolean_t right_justify;
1948 for (; pl != NULL; pl = pl->pl_next) {
1955 right_justify = B_FALSE;
1956 if (pl->pl_prop != ZPROP_INVAL) {
1957 header = zfs_prop_column_name(pl->pl_prop);
1958 right_justify = zfs_prop_align_right(pl->pl_prop);
1960 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1961 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1962 headerbuf[i] = '\0';
1966 if (pl->pl_next == NULL && !right_justify)
1967 (void) printf("%s", header);
1968 else if (right_justify)
1969 (void) printf("%*s", (int)pl->pl_width, header);
1971 (void) printf("%-*s", (int)pl->pl_width, header);
1974 (void) printf("\n");
1978 * Given a dataset and a list of fields, print out all the properties according
1979 * to the described layout.
1982 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1984 boolean_t first = B_TRUE;
1985 char property[ZFS_MAXPROPLEN];
1986 nvlist_t *userprops = zfs_get_user_props(zhp);
1989 boolean_t right_justify;
1992 for (; pl != NULL; pl = pl->pl_next) {
1995 (void) printf("\t");
2002 if (pl->pl_prop != ZPROP_INVAL) {
2003 if (zfs_prop_get(zhp, pl->pl_prop, property,
2004 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2009 right_justify = zfs_prop_align_right(pl->pl_prop);
2010 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2011 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2012 property, sizeof (property), B_FALSE) != 0)
2016 right_justify = B_TRUE;
2018 if (nvlist_lookup_nvlist(userprops,
2019 pl->pl_user_prop, &propval) != 0)
2022 verify(nvlist_lookup_string(propval,
2023 ZPROP_VALUE, &propstr) == 0);
2024 right_justify = B_FALSE;
2027 width = pl->pl_width;
2030 * If this is being called in scripted mode, or if this is the
2031 * last column and it is left-justified, don't include a width
2034 if (scripted || (pl->pl_next == NULL && !right_justify))
2035 (void) printf("%s", propstr);
2036 else if (right_justify)
2037 (void) printf("%*s", width, propstr);
2039 (void) printf("%-*s", width, propstr);
2042 (void) printf("\n");
2046 * Generic callback function to list a dataset or snapshot.
2049 list_callback(zfs_handle_t *zhp, void *data)
2051 list_cbdata_t *cbp = data;
2053 if (cbp->cb_first) {
2054 if (!cbp->cb_scripted)
2055 print_header(cbp->cb_proplist);
2056 cbp->cb_first = B_FALSE;
2059 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2065 zfs_do_list(int argc, char **argv)
2068 boolean_t scripted = B_FALSE;
2069 static char default_fields[] =
2070 "name,used,available,referenced,mountpoint";
2071 int types = ZFS_TYPE_DATASET;
2072 boolean_t types_specified = B_FALSE;
2073 char *fields = NULL;
2074 list_cbdata_t cb = { 0 };
2078 zfs_sort_column_t *sortcol = NULL;
2079 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2082 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2088 limit = parse_depth(optarg, &flags);
2091 flags |= ZFS_ITER_RECURSE;
2097 if (zfs_add_sort_column(&sortcol, optarg,
2099 (void) fprintf(stderr,
2100 gettext("invalid property '%s'\n"), optarg);
2105 if (zfs_add_sort_column(&sortcol, optarg,
2107 (void) fprintf(stderr,
2108 gettext("invalid property '%s'\n"), optarg);
2114 types_specified = B_TRUE;
2115 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2116 while (*optarg != '\0') {
2117 static char *type_subopts[] = { "filesystem",
2118 "volume", "snapshot", "all", NULL };
2120 switch (getsubopt(&optarg, type_subopts,
2123 types |= ZFS_TYPE_FILESYSTEM;
2126 types |= ZFS_TYPE_VOLUME;
2129 types |= ZFS_TYPE_SNAPSHOT;
2132 types = ZFS_TYPE_DATASET;
2136 (void) fprintf(stderr,
2137 gettext("invalid type '%s'\n"),
2144 (void) fprintf(stderr, gettext("missing argument for "
2145 "'%c' option\n"), optopt);
2149 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2159 fields = default_fields;
2162 * If "-o space" and no types were specified, don't display snapshots.
2164 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
2165 types &= ~ZFS_TYPE_SNAPSHOT;
2168 * If the user specifies '-o all', the zprop_get_list() doesn't
2169 * normally include the name of the dataset. For 'zfs list', we always
2170 * want this property to be first.
2172 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2176 cb.cb_scripted = scripted;
2177 cb.cb_first = B_TRUE;
2179 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
2180 limit, list_callback, &cb);
2182 zprop_free_list(cb.cb_proplist);
2183 zfs_free_sort_columns(sortcol);
2185 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
2186 (void) printf(gettext("no datasets available\n"));
2192 * zfs rename <fs | snap | vol> <fs | snap | vol>
2193 * zfs rename -p <fs | vol> <fs | vol>
2194 * zfs rename -r <snap> <snap>
2196 * Renames the given dataset to another of the same type.
2198 * The '-p' flag creates all the non-existing ancestors of the target first.
2202 zfs_do_rename(int argc, char **argv)
2207 boolean_t recurse = B_FALSE;
2208 boolean_t parents = B_FALSE;
2211 while ((c = getopt(argc, argv, "pr")) != -1) {
2221 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2230 /* check number of arguments */
2232 (void) fprintf(stderr, gettext("missing source dataset "
2237 (void) fprintf(stderr, gettext("missing target dataset "
2242 (void) fprintf(stderr, gettext("too many arguments\n"));
2246 if (recurse && parents) {
2247 (void) fprintf(stderr, gettext("-p and -r options are mutually "
2252 if (recurse && strchr(argv[0], '@') == 0) {
2253 (void) fprintf(stderr, gettext("source dataset for recursive "
2254 "rename must be a snapshot\n"));
2258 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
2259 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
2262 /* If we were asked and the name looks good, try to create ancestors. */
2263 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
2264 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
2269 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
2278 * Promotes the given clone fs to be the parent
2282 zfs_do_promote(int argc, char **argv)
2288 if (argc > 1 && argv[1][0] == '-') {
2289 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2294 /* check number of arguments */
2296 (void) fprintf(stderr, gettext("missing clone filesystem"
2301 (void) fprintf(stderr, gettext("too many arguments\n"));
2305 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2309 ret = (zfs_promote(zhp) != 0);
2317 * zfs rollback [-rRf] <snapshot>
2319 * -r Delete any intervening snapshots before doing rollback
2320 * -R Delete any snapshots and their clones
2321 * -f ignored for backwards compatability
2323 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2324 * since then and making it the active dataset. If more recent snapshots exist,
2325 * the command will complain unless the '-r' flag is given.
2327 typedef struct rollback_cbdata {
2333 boolean_t cb_recurse;
2334 boolean_t cb_dependent;
2335 } rollback_cbdata_t;
2338 * Report any snapshots more recent than the one specified. Used when '-r' is
2339 * not specified. We reuse this same callback for the snapshot dependents - if
2340 * 'cb_dependent' is set, then this is a dependent and we should report it
2341 * without checking the transaction group.
2344 rollback_check(zfs_handle_t *zhp, void *data)
2346 rollback_cbdata_t *cbp = data;
2348 if (cbp->cb_doclones) {
2353 if (!cbp->cb_dependent) {
2354 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2355 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2356 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2359 if (cbp->cb_first && !cbp->cb_recurse) {
2360 (void) fprintf(stderr, gettext("cannot "
2361 "rollback to '%s': more recent snapshots "
2364 (void) fprintf(stderr, gettext("use '-r' to "
2365 "force deletion of the following "
2371 if (cbp->cb_recurse) {
2372 cbp->cb_dependent = B_TRUE;
2373 if (zfs_iter_dependents(zhp, B_TRUE,
2374 rollback_check, cbp) != 0) {
2378 cbp->cb_dependent = B_FALSE;
2380 (void) fprintf(stderr, "%s\n",
2385 if (cbp->cb_first && cbp->cb_recurse) {
2386 (void) fprintf(stderr, gettext("cannot rollback to "
2387 "'%s': clones of previous snapshots exist\n"),
2389 (void) fprintf(stderr, gettext("use '-R' to "
2390 "force deletion of the following clones and "
2396 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2404 zfs_do_rollback(int argc, char **argv)
2408 boolean_t force = B_FALSE;
2409 rollback_cbdata_t cb = { 0 };
2410 zfs_handle_t *zhp, *snap;
2411 char parentname[ZFS_MAXNAMELEN];
2415 while ((c = getopt(argc, argv, "rRf")) != -1) {
2428 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2437 /* check number of arguments */
2439 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2443 (void) fprintf(stderr, gettext("too many arguments\n"));
2447 /* open the snapshot */
2448 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2451 /* open the parent dataset */
2452 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2453 verify((delim = strrchr(parentname, '@')) != NULL);
2455 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2461 * Check for more recent snapshots and/or clones based on the presence
2464 cb.cb_target = argv[0];
2465 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2466 cb.cb_first = B_TRUE;
2468 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2471 if ((ret = cb.cb_error) != 0)
2475 * Rollback parent to the given snapshot.
2477 ret = zfs_rollback(zhp, snap, force);
2490 * zfs set property=value { fs | snap | vol } ...
2492 * Sets the given property for all datasets specified on the command line.
2494 typedef struct set_cbdata {
2500 set_callback(zfs_handle_t *zhp, void *data)
2502 set_cbdata_t *cbp = data;
2504 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2505 switch (libzfs_errno(g_zfs)) {
2506 case EZFS_MOUNTFAILED:
2507 (void) fprintf(stderr, gettext("property may be set "
2508 "but unable to remount filesystem\n"));
2510 case EZFS_SHARENFSFAILED:
2511 (void) fprintf(stderr, gettext("property may be set "
2512 "but unable to reshare filesystem\n"));
2521 zfs_do_set(int argc, char **argv)
2526 /* check for options */
2527 if (argc > 1 && argv[1][0] == '-') {
2528 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2533 /* check number of arguments */
2535 (void) fprintf(stderr, gettext("missing property=value "
2540 (void) fprintf(stderr, gettext("missing dataset name\n"));
2544 /* validate property=value argument */
2545 cb.cb_propname = argv[1];
2546 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2547 (cb.cb_value[1] == '\0')) {
2548 (void) fprintf(stderr, gettext("missing value in "
2549 "property=value argument\n"));
2553 *cb.cb_value = '\0';
2556 if (*cb.cb_propname == '\0') {
2557 (void) fprintf(stderr,
2558 gettext("missing property in property=value argument\n"));
2562 ret = zfs_for_each(argc - 2, argv + 2, 0,
2563 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
2569 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2571 * Creates a snapshot with the given name. While functionally equivalent to
2572 * 'zfs create', it is a separate command to differentiate intent.
2575 zfs_do_snapshot(int argc, char **argv)
2577 boolean_t recursive = B_FALSE;
2582 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2586 while ((c = getopt(argc, argv, "ro:")) != -1) {
2589 if (parseprop(props))
2596 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2605 /* check number of arguments */
2607 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2611 (void) fprintf(stderr, gettext("too many arguments\n"));
2615 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2617 if (ret && recursive)
2618 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2628 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
2629 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
2631 * Send a backup stream to stdout.
2634 zfs_do_send(int argc, char **argv)
2636 char *fromname = NULL;
2637 char *toname = NULL;
2640 sendflags_t flags = { 0 };
2643 boolean_t extraverbose = B_FALSE;
2646 while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) {
2657 flags.doall = B_TRUE;
2660 flags.replicate = B_TRUE;
2663 flags.props = B_TRUE;
2667 extraverbose = B_TRUE;
2668 flags.verbose = B_TRUE;
2671 flags.dedup = B_TRUE;
2674 (void) fprintf(stderr, gettext("missing argument for "
2675 "'%c' option\n"), optopt);
2679 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2688 /* check number of arguments */
2690 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2694 (void) fprintf(stderr, gettext("too many arguments\n"));
2698 if (isatty(STDOUT_FILENO)) {
2699 (void) fprintf(stderr,
2700 gettext("Error: Stream can not be written to a terminal.\n"
2701 "You must redirect standard output.\n"));
2705 cp = strchr(argv[0], '@');
2707 (void) fprintf(stderr,
2708 gettext("argument must be a snapshot\n"));
2713 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2718 * If they specified the full path to the snapshot, chop off
2719 * everything except the short name of the snapshot, but special
2720 * case if they specify the origin.
2722 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2723 char origin[ZFS_MAXNAMELEN];
2726 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2727 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2729 if (strcmp(origin, fromname) == 0) {
2731 flags.fromorigin = B_TRUE;
2734 if (cp != fromname && strcmp(argv[0], fromname)) {
2735 (void) fprintf(stderr,
2736 gettext("incremental source must be "
2737 "in same filesystem\n"));
2741 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2742 (void) fprintf(stderr,
2743 gettext("invalid incremental source\n"));
2749 if (flags.replicate && fromname == NULL)
2750 flags.doall = B_TRUE;
2752 err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0,
2753 extraverbose ? &dbgnv : NULL);
2757 * dump_nvlist prints to stdout, but that's been
2758 * redirected to a file. Make it print to stderr
2761 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
2762 dump_nvlist(dbgnv, 0);
2771 * zfs receive [-vnFu] [-d | -e] <fs@snap>
2773 * Restore a backup stream from stdin.
2776 zfs_do_receive(int argc, char **argv)
2779 recvflags_t flags = { 0 };
2782 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
2785 flags.isprefix = B_TRUE;
2788 flags.isprefix = B_TRUE;
2789 flags.istail = B_TRUE;
2792 flags.dryrun = B_TRUE;
2795 flags.nomount = B_TRUE;
2798 flags.verbose = B_TRUE;
2801 flags.force = B_TRUE;
2804 (void) fprintf(stderr, gettext("missing argument for "
2805 "'%c' option\n"), optopt);
2809 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2818 /* check number of arguments */
2820 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2824 (void) fprintf(stderr, gettext("too many arguments\n"));
2828 if (isatty(STDIN_FILENO)) {
2829 (void) fprintf(stderr,
2830 gettext("Error: Backup stream can not be read "
2831 "from a terminal.\n"
2832 "You must redirect standard input.\n"));
2836 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2842 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
2847 boolean_t recursive = B_FALSE;
2848 boolean_t temphold = B_FALSE;
2849 const char *opts = holding ? "rt" : "r";
2853 while ((c = getopt(argc, argv, opts)) != -1) {
2862 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2871 /* check number of arguments */
2879 if (holding && tag[0] == '.') {
2880 /* tags starting with '.' are reserved for libzfs */
2881 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
2885 for (i = 0; i < argc; ++i) {
2887 char parent[ZFS_MAXNAMELEN];
2889 char *path = argv[i];
2891 delim = strchr(path, '@');
2892 if (delim == NULL) {
2893 (void) fprintf(stderr,
2894 gettext("'%s' is not a snapshot\n"), path);
2898 (void) strncpy(parent, path, delim - path);
2899 parent[delim - path] = '\0';
2901 zhp = zfs_open(g_zfs, parent,
2902 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2908 if (zfs_hold(zhp, delim+1, tag, recursive,
2909 temphold, B_FALSE, -1, 0, 0) != 0)
2912 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
2918 return (errors != 0);
2922 * zfs hold [-r] [-t] <tag> <snap> ...
2924 * -r Recursively hold
2925 * -t Temporary hold (hidden option)
2927 * Apply a user-hold with the given tag to the list of snapshots.
2930 zfs_do_hold(int argc, char **argv)
2932 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
2936 * zfs release [-r] <tag> <snap> ...
2938 * -r Recursively release
2940 * Release a user-hold with the given tag from the list of snapshots.
2943 zfs_do_release(int argc, char **argv)
2945 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
2948 #define CHECK_SPINNER 30
2949 #define SPINNER_TIME 3 /* seconds */
2950 #define MOUNT_TIME 5 /* seconds */
2954 get_one_dataset(zfs_handle_t *zhp, void *data)
2956 static char *spin[] = { "-", "\\", "|", "/" };
2957 static int spinval = 0;
2958 static int spincheck = 0;
2959 static time_t last_spin_time = (time_t)0;
2960 get_all_cb_t *cbp = data;
2961 zfs_type_t type = zfs_get_type(zhp);
2963 if (cbp->cb_verbose) {
2964 if (--spincheck < 0) {
2965 time_t now = time(NULL);
2966 if (last_spin_time + SPINNER_TIME < now) {
2967 update_progress(spin[spinval++ % 4]);
2968 last_spin_time = now;
2970 spincheck = CHECK_SPINNER;
2975 * Interate over any nested datasets.
2977 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2983 * Skip any datasets whose type does not match.
2985 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
2989 libzfs_add_handle(cbp, zhp);
2990 assert(cbp->cb_used <= cbp->cb_alloc);
2996 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
2998 get_all_cb_t cb = { 0 };
2999 cb.cb_verbose = verbose;
3000 cb.cb_getone = get_one_dataset;
3003 set_progress_header(gettext("Reading ZFS config"));
3004 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
3006 *dslist = cb.cb_handles;
3007 *count = cb.cb_used;
3010 finish_progress(gettext("done."));
3014 * Generic callback for sharing or mounting filesystems. Because the code is so
3015 * similar, we have a common function with an extra parameter to determine which
3016 * mode we are using.
3018 #define OP_SHARE 0x1
3019 #define OP_MOUNT 0x2
3022 * Share or mount a dataset.
3025 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3026 boolean_t explicit, const char *options)
3028 char mountpoint[ZFS_MAXPROPLEN];
3029 char shareopts[ZFS_MAXPROPLEN];
3030 char smbshareopts[ZFS_MAXPROPLEN];
3031 const char *cmdname = op == OP_SHARE ? "share" : "mount";
3033 uint64_t zoned, canmount;
3034 boolean_t shared_nfs, shared_smb;
3036 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
3039 * Check to make sure we can mount/share this dataset. If we
3040 * are in the global zone and the filesystem is exported to a
3041 * local zone, or if we are in a local zone and the
3042 * filesystem is not exported, then it is an error.
3044 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3046 if (zoned && getzoneid() == GLOBAL_ZONEID) {
3050 (void) fprintf(stderr, gettext("cannot %s '%s': "
3051 "dataset is exported to a local zone\n"), cmdname,
3055 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3059 (void) fprintf(stderr, gettext("cannot %s '%s': "
3060 "permission denied\n"), cmdname,
3066 * Ignore any filesystems which don't apply to us. This
3067 * includes those with a legacy mountpoint, or those with
3068 * legacy share options.
3070 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3071 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3072 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3073 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3074 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3075 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3077 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3078 strcmp(smbshareopts, "off") == 0) {
3082 (void) fprintf(stderr, gettext("cannot share '%s': "
3083 "legacy share\n"), zfs_get_name(zhp));
3084 (void) fprintf(stderr, gettext("use share(1M) to "
3085 "share this filesystem, or set "
3086 "sharenfs property on\n"));
3091 * We cannot share or mount legacy filesystems. If the
3092 * shareopts is non-legacy but the mountpoint is legacy, we
3093 * treat it as a legacy share.
3095 if (strcmp(mountpoint, "legacy") == 0) {
3099 (void) fprintf(stderr, gettext("cannot %s '%s': "
3100 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3101 (void) fprintf(stderr, gettext("use %s(1M) to "
3102 "%s this filesystem\n"), cmdname, cmdname);
3106 if (strcmp(mountpoint, "none") == 0) {
3110 (void) fprintf(stderr, gettext("cannot %s '%s': no "
3111 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3116 * canmount explicit outcome
3117 * on no pass through
3118 * on yes pass through
3120 * off yes display error, return 1
3121 * noauto no return 0
3122 * noauto yes pass through
3124 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3125 if (canmount == ZFS_CANMOUNT_OFF) {
3129 (void) fprintf(stderr, gettext("cannot %s '%s': "
3130 "'canmount' property is set to 'off'\n"), cmdname,
3133 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3138 * At this point, we have verified that the mountpoint and/or
3139 * shareopts are appropriate for auto management. If the
3140 * filesystem is already mounted or shared, return (failing
3141 * for explicit requests); otherwise mount or share the
3147 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3148 shared_smb = zfs_is_shared_smb(zhp, NULL);
3150 if (shared_nfs && shared_smb ||
3151 (shared_nfs && strcmp(shareopts, "on") == 0 &&
3152 strcmp(smbshareopts, "off") == 0) ||
3153 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3154 strcmp(shareopts, "off") == 0)) {
3158 (void) fprintf(stderr, gettext("cannot share "
3159 "'%s': filesystem already shared\n"),
3164 if (!zfs_is_mounted(zhp, NULL) &&
3165 zfs_mount(zhp, NULL, 0) != 0)
3168 if (protocol == NULL) {
3169 if (zfs_shareall(zhp) != 0)
3171 } else if (strcmp(protocol, "nfs") == 0) {
3172 if (zfs_share_nfs(zhp))
3174 } else if (strcmp(protocol, "smb") == 0) {
3175 if (zfs_share_smb(zhp))
3178 (void) fprintf(stderr, gettext("cannot share "
3179 "'%s': invalid share type '%s' "
3181 zfs_get_name(zhp), protocol);
3188 if (options == NULL)
3189 mnt.mnt_mntopts = "";
3191 mnt.mnt_mntopts = (char *)options;
3193 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3194 zfs_is_mounted(zhp, NULL)) {
3198 (void) fprintf(stderr, gettext("cannot mount "
3199 "'%s': filesystem already mounted\n"),
3204 if (zfs_mount(zhp, options, flags) != 0)
3213 * Reports progress in the form "(current/total)". Not thread-safe.
3216 report_mount_progress(int current, int total)
3218 static time_t last_progress_time = 0;
3219 time_t now = time(NULL);
3222 /* report 1..n instead of 0..n-1 */
3225 /* display header if we're here for the first time */
3227 set_progress_header(gettext("Mounting ZFS filesystems"));
3228 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3229 /* too soon to report again */
3233 last_progress_time = now;
3235 (void) sprintf(info, "(%d/%d)", current, total);
3237 if (current == total)
3238 finish_progress(info);
3240 update_progress(info);
3244 append_options(char *mntopts, char *newopts)
3246 int len = strlen(mntopts);
3248 /* original length plus new string to append plus 1 for the comma */
3249 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3250 (void) fprintf(stderr, gettext("the opts argument for "
3251 "'%s' option is too long (more than %d chars)\n"),
3252 "-o", MNT_LINE_MAX);
3257 mntopts[len++] = ',';
3259 (void) strcpy(&mntopts[len], newopts);
3263 share_mount(int op, int argc, char **argv)
3266 boolean_t verbose = B_FALSE;
3268 char *options = NULL;
3272 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3282 if (*optarg == '\0') {
3283 (void) fprintf(stderr, gettext("empty mount "
3284 "options (-o) specified\n"));
3288 if (options == NULL)
3289 options = safe_malloc(MNT_LINE_MAX + 1);
3291 /* option validation is done later */
3292 append_options(options, optarg);
3296 flags |= MS_OVERLAY;
3299 (void) fprintf(stderr, gettext("missing argument for "
3300 "'%c' option\n"), optopt);
3304 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3313 /* check number of arguments */
3315 zfs_handle_t **dslist = NULL;
3316 size_t i, count = 0;
3317 char *protocol = NULL;
3319 if (op == OP_SHARE && argc > 0) {
3320 if (strcmp(argv[0], "nfs") != 0 &&
3321 strcmp(argv[0], "smb") != 0) {
3322 (void) fprintf(stderr, gettext("share type "
3323 "must be 'nfs' or 'smb'\n"));
3332 (void) fprintf(stderr, gettext("too many arguments\n"));
3336 start_progress_timer();
3337 get_all_datasets(&dslist, &count, verbose);
3342 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
3344 for (i = 0; i < count; i++) {
3346 report_mount_progress(i, count);
3348 if (share_mount_one(dslist[i], op, flags, protocol,
3349 B_FALSE, options) != 0)
3351 zfs_close(dslist[i]);
3355 } else if (argc == 0) {
3356 struct mnttab entry;
3358 if ((op == OP_SHARE) || (options != NULL)) {
3359 (void) fprintf(stderr, gettext("missing filesystem "
3360 "argument (specify -a for all)\n"));
3365 * When mount is given no arguments, go through /etc/mnttab and
3366 * display any active ZFS mounts. We hide any snapshots, since
3367 * they are controlled automatically.
3369 rewind(mnttab_file);
3370 while (getmntent(mnttab_file, &entry) == 0) {
3371 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3372 strchr(entry.mnt_special, '@') != NULL)
3375 (void) printf("%-30s %s\n", entry.mnt_special,
3383 (void) fprintf(stderr,
3384 gettext("too many arguments\n"));
3388 if ((zhp = zfs_open(g_zfs, argv[0],
3389 ZFS_TYPE_FILESYSTEM)) == NULL) {
3392 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3400 #endif /* HAVE_ZPL */
3403 * zfs mount -a [nfs]
3404 * zfs mount filesystem
3406 * Mount all filesystems, or mount the given filesystem.
3409 zfs_do_mount(int argc, char **argv)
3412 return (share_mount(OP_MOUNT, argc, argv));
3415 #endif /* HAVE_ZPL */
3419 * zfs share -a [nfs | smb]
3420 * zfs share filesystem
3422 * Share all filesystems, or share the given filesystem.
3425 zfs_do_share(int argc, char **argv)
3428 return (share_mount(OP_SHARE, argc, argv));
3431 #endif /* HAVE_ZPL */
3435 typedef struct unshare_unmount_node {
3436 zfs_handle_t *un_zhp;
3438 uu_avl_node_t un_avlnode;
3439 } unshare_unmount_node_t;
3443 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3445 const unshare_unmount_node_t *l = larg;
3446 const unshare_unmount_node_t *r = rarg;
3448 return (strcmp(l->un_mountp, r->un_mountp));
3452 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3453 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3454 * and unmount it appropriately.
3457 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3461 struct stat64 statbuf;
3462 struct extmnttab entry;
3463 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3467 * Search for the path in /etc/mnttab. Rather than looking for the
3468 * specific path, which can be fooled by non-standard paths (i.e. ".."
3469 * or "//"), we stat() the path and search for the corresponding
3470 * (major,minor) device pair.
3472 if (stat64(path, &statbuf) != 0) {
3473 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3474 cmdname, path, strerror(errno));
3477 path_inode = statbuf.st_ino;
3480 * Search for the given (major,minor) pair in the mount table.
3482 rewind(mnttab_file);
3483 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3484 if (entry.mnt_major == major(statbuf.st_dev) &&
3485 entry.mnt_minor == minor(statbuf.st_dev))
3489 if (op == OP_SHARE) {
3490 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3491 "currently mounted\n"), cmdname, path);
3494 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3496 if ((ret = umount2(path, flags)) != 0)
3497 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3502 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3503 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3504 "filesystem\n"), cmdname, path);
3508 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3509 ZFS_TYPE_FILESYSTEM)) == NULL)
3513 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3514 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3515 cmdname, path, strerror(errno));
3517 } else if (statbuf.st_ino != path_inode) {
3518 (void) fprintf(stderr, gettext("cannot "
3519 "%s '%s': not a mountpoint\n"), cmdname, path);
3523 if (op == OP_SHARE) {
3524 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3525 char smbshare_prop[ZFS_MAXPROPLEN];
3527 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3528 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3529 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3530 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3532 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3533 strcmp(smbshare_prop, "off") == 0) {
3534 (void) fprintf(stderr, gettext("cannot unshare "
3535 "'%s': legacy share\n"), path);
3536 (void) fprintf(stderr, gettext("use "
3537 "unshare(1M) to unshare this filesystem\n"));
3538 } else if (!zfs_is_shared(zhp)) {
3539 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3540 "not currently shared\n"), path);
3542 ret = zfs_unshareall_bypath(zhp, path);
3545 char mtpt_prop[ZFS_MAXPROPLEN];
3547 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3548 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3551 ret = zfs_unmount(zhp, NULL, flags);
3552 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3553 (void) fprintf(stderr, gettext("cannot unmount "
3554 "'%s': legacy mountpoint\n"),
3556 (void) fprintf(stderr, gettext("use umount(1M) "
3557 "to unmount this filesystem\n"));
3559 ret = zfs_unmountall(zhp, flags);
3570 * Generic callback for unsharing or unmounting a filesystem.
3573 unshare_unmount(int op, int argc, char **argv)
3580 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3581 char sharesmb[ZFS_MAXPROPLEN];
3584 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3593 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3604 * We could make use of zfs_for_each() to walk all datasets in
3605 * the system, but this would be very inefficient, especially
3606 * since we would have to linearly search /etc/mnttab for each
3607 * one. Instead, do one pass through /etc/mnttab looking for
3608 * zfs entries and call zfs_unmount() for each one.
3610 * Things get a little tricky if the administrator has created
3611 * mountpoints beneath other ZFS filesystems. In this case, we
3612 * have to unmount the deepest filesystems first. To accomplish
3613 * this, we place all the mountpoints in an AVL tree sorted by
3614 * the special type (dataset name), and walk the result in
3615 * reverse to make sure to get any snapshots first.
3617 struct mnttab entry;
3618 uu_avl_pool_t *pool;
3620 unshare_unmount_node_t *node;
3622 uu_avl_walk_t *walk;
3625 (void) fprintf(stderr, gettext("too many arguments\n"));
3629 if (((pool = uu_avl_pool_create("unmount_pool",
3630 sizeof (unshare_unmount_node_t),
3631 offsetof(unshare_unmount_node_t, un_avlnode),
3632 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
3633 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
3636 rewind(mnttab_file);
3637 while (getmntent(mnttab_file, &entry) == 0) {
3639 /* ignore non-ZFS entries */
3640 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3643 /* ignore snapshots */
3644 if (strchr(entry.mnt_special, '@') != NULL)
3647 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3648 ZFS_TYPE_FILESYSTEM)) == NULL) {
3655 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3657 sizeof (nfs_mnt_prop),
3658 NULL, NULL, 0, B_FALSE) == 0);
3659 if (strcmp(nfs_mnt_prop, "off") != 0)
3661 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3663 sizeof (nfs_mnt_prop),
3664 NULL, NULL, 0, B_FALSE) == 0);
3665 if (strcmp(nfs_mnt_prop, "off") == 0)
3669 /* Ignore legacy mounts */
3670 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3672 sizeof (nfs_mnt_prop),
3673 NULL, NULL, 0, B_FALSE) == 0);
3674 if (strcmp(nfs_mnt_prop, "legacy") == 0)
3676 /* Ignore canmount=noauto mounts */
3677 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3678 ZFS_CANMOUNT_NOAUTO)
3684 node = safe_malloc(sizeof (unshare_unmount_node_t));
3686 node->un_mountp = safe_strdup(entry.mnt_mountp);
3688 uu_avl_node_init(node, &node->un_avlnode, pool);
3690 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3691 uu_avl_insert(tree, node, idx);
3693 zfs_close(node->un_zhp);
3694 free(node->un_mountp);
3700 * Walk the AVL tree in reverse, unmounting each filesystem and
3701 * removing it from the AVL tree in the process.
3703 if ((walk = uu_avl_walk_start(tree,
3704 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
3707 while ((node = uu_avl_walk_next(walk)) != NULL) {
3708 uu_avl_remove(tree, node);
3712 if (zfs_unshareall_bypath(node->un_zhp,
3713 node->un_mountp) != 0)
3718 if (zfs_unmount(node->un_zhp,
3719 node->un_mountp, flags) != 0)
3724 zfs_close(node->un_zhp);
3725 free(node->un_mountp);
3729 uu_avl_walk_end(walk);
3730 uu_avl_destroy(tree);
3731 uu_avl_pool_destroy(pool);
3736 (void) fprintf(stderr,
3737 gettext("missing filesystem argument\n"));
3739 (void) fprintf(stderr,
3740 gettext("too many arguments\n"));
3745 * We have an argument, but it may be a full path or a ZFS
3746 * filesystem. Pass full paths off to unmount_path() (shared by
3747 * manual_unmount), otherwise open the filesystem and pass to
3750 if (argv[0][0] == '/')
3751 return (unshare_unmount_path(op, argv[0],
3754 if ((zhp = zfs_open(g_zfs, argv[0],
3755 ZFS_TYPE_FILESYSTEM)) == NULL)
3758 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3759 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3760 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
3761 NULL, 0, B_FALSE) == 0);
3765 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3767 sizeof (nfs_mnt_prop),
3768 NULL, NULL, 0, B_FALSE) == 0);
3769 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3770 sharesmb, sizeof (sharesmb), NULL, NULL,
3773 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3774 strcmp(sharesmb, "off") == 0) {
3775 (void) fprintf(stderr, gettext("cannot "
3776 "unshare '%s': legacy share\n"),
3778 (void) fprintf(stderr, gettext("use "
3779 "unshare(1M) to unshare this "
3782 } else if (!zfs_is_shared(zhp)) {
3783 (void) fprintf(stderr, gettext("cannot "
3784 "unshare '%s': not currently "
3785 "shared\n"), zfs_get_name(zhp));
3787 } else if (zfs_unshareall(zhp) != 0) {
3793 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
3794 (void) fprintf(stderr, gettext("cannot "
3795 "unmount '%s': legacy "
3796 "mountpoint\n"), zfs_get_name(zhp));
3797 (void) fprintf(stderr, gettext("use "
3798 "umount(1M) to unmount this "
3801 } else if (!zfs_is_mounted(zhp, NULL)) {
3802 (void) fprintf(stderr, gettext("cannot "
3803 "unmount '%s': not currently "
3807 } else if (zfs_unmountall(zhp, flags) != 0) {
3818 #endif /* HAVE_ZPL */
3822 * zfs unmount filesystem
3824 * Unmount all filesystems, or a specific ZFS filesystem.
3827 zfs_do_unmount(int argc, char **argv)
3830 return (unshare_unmount(OP_MOUNT, argc, argv));
3833 #endif /* HAVE_ZPL */
3838 * zfs unshare filesystem
3840 * Unshare all filesystems, or a specific ZFS filesystem.
3843 zfs_do_unshare(int argc, char **argv)
3846 return (unshare_unmount(OP_SHARE, argc, argv));
3849 #endif /* HAVE_ZPL */
3854 zfs_do_python(int argc, char **argv)
3856 (void) execv(pypath, argv-1);
3857 (void) printf("internal error: %s not found\n", pypath);
3862 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
3863 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
3867 manual_mount(int argc, char **argv)
3870 char mountpoint[ZFS_MAXPROPLEN];
3871 char mntopts[MNT_LINE_MAX] = { '\0' };
3875 char *dataset, *path;
3878 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3881 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
3884 flags |= MS_OVERLAY;
3887 flags |= MS_NOMNTTAB;
3890 (void) fprintf(stderr, gettext("missing argument for "
3891 "'%c' option\n"), optopt);
3895 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3897 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
3906 /* check that we only have two arguments */
3909 (void) fprintf(stderr, gettext("missing dataset "
3912 (void) fprintf(stderr,
3913 gettext("missing mountpoint argument\n"));
3915 (void) fprintf(stderr, gettext("too many arguments\n"));
3916 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3923 /* try to open the dataset */
3924 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3927 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3928 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3930 /* check for legacy mountpoint and complain appropriately */
3932 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
3933 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
3934 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
3935 (void) fprintf(stderr, gettext("mount failed: %s\n"),
3940 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
3941 "mounted using 'mount -F zfs'\n"), dataset);
3942 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
3943 "instead.\n"), path);
3944 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
3945 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
3946 (void) fprintf(stderr, gettext("See zfs(1M) for more "
3955 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
3956 * unmounts of non-legacy filesystems, as this is the dominant administrative
3960 manual_unmount(int argc, char **argv)
3966 while ((c = getopt(argc, argv, "f")) != -1) {
3972 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3974 (void) fprintf(stderr, gettext("usage: unmount [-f] "
3983 /* check arguments */
3986 (void) fprintf(stderr, gettext("missing path "
3989 (void) fprintf(stderr, gettext("too many arguments\n"));
3990 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
3994 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
3996 #endif /* HAVE_ZPL */
3999 find_command_idx(char *command, int *idx)
4003 for (i = 0; i < NCOMMAND; i++) {
4004 if (command_table[i].name == NULL)
4007 if (strcmp(command, command_table[i].name) == 0) {
4016 zfs_do_diff(int argc, char **argv)
4020 char *tosnap = NULL;
4021 char *fromsnap = NULL;
4026 while ((c = getopt(argc, argv, "FHt")) != -1) {
4029 flags |= ZFS_DIFF_CLASSIFY;
4032 flags |= ZFS_DIFF_PARSEABLE;
4035 flags |= ZFS_DIFF_TIMESTAMP;
4038 (void) fprintf(stderr,
4039 gettext("invalid option '%c'\n"), optopt);
4048 (void) fprintf(stderr,
4049 gettext("must provide at least one snapshot name\n"));
4054 (void) fprintf(stderr, gettext("too many arguments\n"));
4059 tosnap = (argc == 2) ? argv[1] : NULL;
4062 if (*fromsnap != '@')
4063 copy = strdup(fromsnap);
4065 copy = strdup(tosnap);
4069 if ((atp = strchr(copy, '@')))
4072 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
4078 * Ignore SIGPIPE so that the library can give us
4079 * information on any failure
4081 (void) sigignore(SIGPIPE);
4083 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
4091 main(int argc, char **argv)
4100 (void) setlocale(LC_ALL, "");
4101 (void) textdomain(TEXT_DOMAIN);
4105 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4106 (void) fprintf(stderr, gettext("internal error: unable to "
4107 "open %s\n"), MNTTAB);
4113 * This command also doubles as the /etc/fs mount and unmount program.
4114 * Determine if we should take this behavior based on argv[0].
4116 progname = basename(argv[0]);
4117 if (strcmp(progname, "mount") == 0) {
4118 ret = manual_mount(argc, argv);
4119 } else if (strcmp(progname, "umount") == 0) {
4120 ret = manual_unmount(argc, argv);
4124 #endif /* HAVE_ZPL */
4126 * Make sure the user has specified some command.
4129 (void) fprintf(stderr, gettext("missing command\n"));
4136 * The 'umount' command is an alias for 'unmount'
4138 if (strcmp(cmdname, "umount") == 0)
4139 cmdname = "unmount";
4142 * The 'recv' command is an alias for 'receive'
4144 if (strcmp(cmdname, "recv") == 0)
4145 cmdname = "receive";
4150 if ((strcmp(cmdname, "-?") == 0) ||
4151 (strcmp(cmdname, "--help") == 0))
4154 if ((g_zfs = libzfs_init()) == NULL)
4157 zpool_set_history_str("zfs", argc, argv, history_str);
4158 verify(zpool_stage_history(g_zfs, history_str) == 0);
4160 libzfs_print_on_error(g_zfs, B_TRUE);
4163 * Run the appropriate command.
4165 libzfs_mnttab_cache(g_zfs, B_TRUE);
4166 if (find_command_idx(cmdname, &i) == 0) {
4167 current_command = &command_table[i];
4168 ret = command_table[i].func(argc - 1, argv + 1);
4169 } else if (strchr(cmdname, '=') != NULL) {
4170 verify(find_command_idx("set", &i) == 0);
4171 current_command = &command_table[i];
4172 ret = command_table[i].func(argc, argv);
4174 (void) fprintf(stderr, gettext("unrecognized "
4175 "command '%s'\n"), cmdname);
4179 libzfs_mnttab_cache(g_zfs, B_FALSE);
4182 (void) fclose(mnttab_file);
4187 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4188 * for the purposes of running ::findleaks.
4190 if (getenv("ZFS_ABORT") != NULL) {
4191 (void) printf("dumping core by request\n");