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)
322 safe_strdup(char *str)
324 char *dupstr = strdup(str);
333 * Callback routine that will print out information for each of
337 usage_prop_cb(int prop, void *cb)
341 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
343 if (zfs_prop_readonly(prop))
344 (void) fprintf(fp, " NO ");
346 (void) fprintf(fp, "YES ");
348 if (zfs_prop_inheritable(prop))
349 (void) fprintf(fp, " YES ");
351 (void) fprintf(fp, " NO ");
353 if (zfs_prop_values(prop) == NULL)
354 (void) fprintf(fp, "-\n");
356 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
362 * Display usage message. If we're inside a command, display only the usage for
363 * that command. Otherwise, iterate over the entire command table and display
364 * a complete usage message.
367 usage(boolean_t requested)
370 boolean_t show_properties = B_FALSE;
371 FILE *fp = requested ? stdout : stderr;
373 if (current_command == NULL) {
375 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
377 gettext("where 'command' is one of the following:\n\n"));
379 for (i = 0; i < NCOMMAND; i++) {
380 if (command_table[i].name == NULL)
381 (void) fprintf(fp, "\n");
383 (void) fprintf(fp, "%s",
384 get_usage(command_table[i].usage));
387 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
388 "pool/[dataset/]*dataset[@name]\n"));
390 (void) fprintf(fp, gettext("usage:\n"));
391 (void) fprintf(fp, "%s", get_usage(current_command->usage));
394 if (current_command != NULL &&
395 (strcmp(current_command->name, "set") == 0 ||
396 strcmp(current_command->name, "get") == 0 ||
397 strcmp(current_command->name, "inherit") == 0 ||
398 strcmp(current_command->name, "list") == 0))
399 show_properties = B_TRUE;
401 if (show_properties) {
403 gettext("\nThe following properties are supported:\n"));
405 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
406 "PROPERTY", "EDIT", "INHERIT", "VALUES");
408 /* Iterate over all properties */
409 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
412 (void) fprintf(fp, "\t%-15s ", "userused@...");
413 (void) fprintf(fp, " NO NO <size>\n");
414 (void) fprintf(fp, "\t%-15s ", "groupused@...");
415 (void) fprintf(fp, " NO NO <size>\n");
416 (void) fprintf(fp, "\t%-15s ", "userquota@...");
417 (void) fprintf(fp, "YES NO <size> | none\n");
418 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
419 (void) fprintf(fp, "YES NO <size> | none\n");
421 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
422 "with standard units such as K, M, G, etc.\n"));
423 (void) fprintf(fp, gettext("\nUser-defined properties can "
424 "be specified by using a name containing a colon (:).\n"));
425 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
426 "properties must be appended with\n"
427 "a user or group specifier of one of these forms:\n"
428 " POSIX name (eg: \"matt\")\n"
429 " POSIX id (eg: \"126829\")\n"
430 " SMB name@domain (eg: \"matt@sun\")\n"
431 " SMB SID (eg: \"S-1-234-567-89\")\n"));
434 gettext("\nFor the property list, run: %s\n"),
437 gettext("\nFor the delegated permission list, run: %s\n"),
438 "zfs allow|unallow");
442 * See comments at end of main().
444 if (getenv("ZFS_ABORT") != NULL) {
445 (void) printf("dumping core by request\n");
449 exit(requested ? 0 : 2);
453 parseprop(nvlist_t *props)
455 char *propname = optarg;
456 char *propval, *strval;
458 if ((propval = strchr(propname, '=')) == NULL) {
459 (void) fprintf(stderr, gettext("missing "
460 "'=' for -o option\n"));
465 if (nvlist_lookup_string(props, propname, &strval) == 0) {
466 (void) fprintf(stderr, gettext("property '%s' "
467 "specified multiple times\n"), propname);
470 if (nvlist_add_string(props, propname, propval) != 0)
476 parse_depth(char *opt, int *flags)
481 depth = (int)strtol(opt, &tmp, 0);
483 (void) fprintf(stderr,
484 gettext("%s is not an integer\n"), optarg);
488 (void) fprintf(stderr,
489 gettext("Depth can not be negative.\n"));
492 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
496 #define PROGRESS_DELAY 2 /* seconds */
498 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";
499 static time_t pt_begin;
500 static char *pt_header = NULL;
501 static boolean_t pt_shown;
504 start_progress_timer(void)
506 pt_begin = time(NULL) + PROGRESS_DELAY;
511 set_progress_header(char *header)
513 assert(pt_header == NULL);
514 pt_header = safe_strdup(header);
516 (void) printf("%s: ", header);
517 (void) fflush(stdout);
522 update_progress(char *update)
524 if (!pt_shown && time(NULL) > pt_begin) {
525 int len = strlen(update);
527 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
529 (void) fflush(stdout);
531 } else if (pt_shown) {
532 int len = strlen(update);
534 (void) printf("%s%*.*s", update, len, len, pt_reverse);
535 (void) fflush(stdout);
540 finish_progress(char *done)
543 (void) printf("%s\n", done);
544 (void) fflush(stdout);
551 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
553 * Given an existing dataset, create a writable copy whose initial contents
554 * are the same as the source. The newly created dataset maintains a
555 * dependency on the original; the original cannot be destroyed so long as
558 * The '-p' flag creates all the non-existing ancestors of the target first.
561 zfs_do_clone(int argc, char **argv)
563 zfs_handle_t *zhp = NULL;
564 boolean_t parents = B_FALSE;
569 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
573 while ((c = getopt(argc, argv, "o:p")) != -1) {
576 if (parseprop(props))
583 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
592 /* check number of arguments */
594 (void) fprintf(stderr, gettext("missing source dataset "
599 (void) fprintf(stderr, gettext("missing target dataset "
604 (void) fprintf(stderr, gettext("too many arguments\n"));
608 /* open the source dataset */
609 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
612 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
615 * Now create the ancestors of the target dataset. If the
616 * target already exists and '-p' option was used we should not
619 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
622 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
627 ret = zfs_clone(zhp, argv[1], props);
629 /* create the mountpoint if necessary */
633 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
635 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
636 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
637 ret = zfs_share(clone);
656 * zfs create [-p] [-o prop=value] ... fs
657 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
659 * Create a new dataset. This command can be used to create filesystems
660 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
661 * For volumes, the user must specify a size to be used.
663 * The '-s' flag applies only to volumes, and indicates that we should not try
664 * to set the reservation for this volume. By default we set a reservation
665 * equal to the size for any volume. For pools with SPA_VERSION >=
666 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
668 * The '-p' flag creates all the non-existing ancestors of the target first.
671 zfs_do_create(int argc, char **argv)
673 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
674 zfs_handle_t *zhp = NULL;
675 uint64_t volsize = 0;
677 boolean_t noreserve = B_FALSE;
678 boolean_t bflag = B_FALSE;
679 boolean_t parents = B_FALSE;
683 int canmount = ZFS_CANMOUNT_OFF;
685 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
689 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
692 type = ZFS_TYPE_VOLUME;
693 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
694 (void) fprintf(stderr, gettext("bad volume "
695 "size '%s': %s\n"), optarg,
696 libzfs_error_description(g_zfs));
700 if (nvlist_add_uint64(props,
701 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
710 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
711 (void) fprintf(stderr, gettext("bad volume "
712 "block size '%s': %s\n"), optarg,
713 libzfs_error_description(g_zfs));
717 if (nvlist_add_uint64(props,
718 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
723 if (parseprop(props))
730 (void) fprintf(stderr, gettext("missing size "
735 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
741 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
742 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
743 "used when creating a volume\n"));
750 /* check number of arguments */
752 (void) fprintf(stderr, gettext("missing %s argument\n"),
753 zfs_type_to_name(type));
757 (void) fprintf(stderr, gettext("too many arguments\n"));
761 if (type == ZFS_TYPE_VOLUME && !noreserve) {
762 zpool_handle_t *zpool_handle;
763 uint64_t spa_version;
765 zfs_prop_t resv_prop;
768 if ((p = strchr(argv[0], '/')))
770 zpool_handle = zpool_open(g_zfs, argv[0]);
773 if (zpool_handle == NULL)
775 spa_version = zpool_get_prop_int(zpool_handle,
776 ZPOOL_PROP_VERSION, NULL);
777 zpool_close(zpool_handle);
778 if (spa_version >= SPA_VERSION_REFRESERVATION)
779 resv_prop = ZFS_PROP_REFRESERVATION;
781 resv_prop = ZFS_PROP_RESERVATION;
782 volsize = zvol_volsize_to_reservation(volsize, props);
784 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
786 if (nvlist_add_uint64(props,
787 zfs_prop_to_name(resv_prop), volsize) != 0) {
794 if (parents && zfs_name_valid(argv[0], type)) {
796 * Now create the ancestors of target dataset. If the target
797 * already exists and '-p' option was used we should not
800 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
804 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
809 if (zfs_create(g_zfs, argv[0], type, props) != 0)
812 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
817 * if the user doesn't want the dataset automatically mounted,
818 * then skip the mount/share step
820 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
821 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
824 * Mount and/or share the new filesystem as appropriate. We provide a
825 * verbose error message to let the user know that their filesystem was
826 * in fact created, even if we failed to mount or share it.
828 if (canmount == ZFS_CANMOUNT_ON) {
829 if (zfs_mount(zhp, NULL, 0) != 0) {
830 (void) fprintf(stderr, gettext("filesystem "
831 "successfully created, but not mounted\n"));
833 } else if (zfs_share(zhp) != 0) {
834 (void) fprintf(stderr, gettext("filesystem "
835 "successfully created, but not shared\n"));
852 * zfs destroy [-rRf] <fs, vol>
853 * zfs destroy [-rRd] <snap>
855 * -r Recursively destroy all children
856 * -R Recursively destroy all dependents, including clones
857 * -f Force unmounting of any dependents
858 * -d If we can't destroy now, mark for deferred destruction
860 * Destroys the given dataset. By default, it will unmount any filesystems,
861 * and refuse to destroy a dataset that has any dependents. A dependent can
862 * either be a child, or a clone of a child.
864 typedef struct destroy_cbdata {
871 boolean_t cb_closezhp;
872 zfs_handle_t *cb_target;
874 boolean_t cb_defer_destroy;
878 * Check for any dependents based on the '-r' or '-R' flags.
881 destroy_check_dependent(zfs_handle_t *zhp, void *data)
883 destroy_cbdata_t *cbp = data;
884 const char *tname = zfs_get_name(cbp->cb_target);
885 const char *name = zfs_get_name(zhp);
887 if (strncmp(tname, name, strlen(tname)) == 0 &&
888 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
890 * This is a direct descendant, not a clone somewhere else in
897 (void) fprintf(stderr, gettext("cannot destroy '%s': "
898 "%s has children\n"),
899 zfs_get_name(cbp->cb_target),
900 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
901 (void) fprintf(stderr, gettext("use '-r' to destroy "
902 "the following datasets:\n"));
903 cbp->cb_first = B_FALSE;
907 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
910 * This is a clone. We only want to report this if the '-r'
911 * wasn't specified, or the target is a snapshot.
913 if (!cbp->cb_recurse &&
914 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
918 (void) fprintf(stderr, gettext("cannot destroy '%s': "
919 "%s has dependent clones\n"),
920 zfs_get_name(cbp->cb_target),
921 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
922 (void) fprintf(stderr, gettext("use '-R' to destroy "
923 "the following datasets:\n"));
924 cbp->cb_first = B_FALSE;
928 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
937 destroy_callback(zfs_handle_t *zhp, void *data)
939 destroy_cbdata_t *cbp = data;
942 * Ignore pools (which we've already flagged as an error before getting
945 if (strchr(zfs_get_name(zhp), '/') == NULL &&
946 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
952 * Bail out on the first error.
954 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
955 zfs_destroy(zhp, cbp->cb_defer_destroy) != 0) {
965 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
967 destroy_cbdata_t *cbp = arg;
968 char thissnap[MAXPATHLEN];
970 boolean_t closezhp = cbp->cb_closezhp;
973 (void) snprintf(thissnap, sizeof (thissnap),
974 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
976 libzfs_print_on_error(g_zfs, B_FALSE);
977 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
978 libzfs_print_on_error(g_zfs, B_TRUE);
981 * Destroy any clones of this snapshot
983 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
993 cbp->cb_closezhp = B_TRUE;
994 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
1001 zfs_do_destroy(int argc, char **argv)
1003 destroy_cbdata_t cb = { 0 };
1007 zfs_type_t type = ZFS_TYPE_DATASET;
1010 while ((c = getopt(argc, argv, "dfrR")) != -1) {
1013 cb.cb_defer_destroy = B_TRUE;
1014 type = ZFS_TYPE_SNAPSHOT;
1028 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1037 /* check number of arguments */
1039 (void) fprintf(stderr, gettext("missing path argument\n"));
1043 (void) fprintf(stderr, gettext("too many arguments\n"));
1048 * If we are doing recursive destroy of a snapshot, then the
1049 * named snapshot may not exist. Go straight to libzfs.
1051 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
1055 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
1060 if (cb.cb_doclones) {
1061 boolean_t defer = cb.cb_defer_destroy;
1064 * Temporarily ignore the defer_destroy setting since
1065 * it's not supported for clones.
1067 cb.cb_defer_destroy = B_FALSE;
1068 cb.cb_snapname = cp;
1069 if (destroy_snap_clones(zhp, &cb) != 0) {
1073 cb.cb_defer_destroy = defer;
1076 ret = zfs_destroy_snaps(zhp, cp, cb.cb_defer_destroy);
1079 (void) fprintf(stderr,
1080 gettext("no snapshots destroyed\n"));
1085 /* Open the given dataset */
1086 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1092 * Perform an explicit check for pools before going any further.
1094 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1095 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1096 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1097 "operation does not apply to pools\n"),
1099 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1100 "%s' to destroy all datasets in the pool\n"),
1102 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1103 "to destroy the pool itself\n"), zfs_get_name(zhp));
1109 * Check for any dependents and/or clones.
1111 cb.cb_first = B_TRUE;
1112 if (!cb.cb_doclones && !cb.cb_defer_destroy &&
1113 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1119 if (cb.cb_error || (!cb.cb_defer_destroy &&
1120 (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0))) {
1126 * Do the real thing. The callback will close the handle regardless of
1127 * whether it succeeds or not.
1130 if (destroy_callback(zhp, &cb) != 0)
1137 is_recvd_column(zprop_get_cbdata_t *cbp)
1140 zfs_get_column_t col;
1142 for (i = 0; i < ZFS_GET_NCOLS &&
1143 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1144 if (col == GET_COL_RECVD)
1150 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1151 * < all | property[,property]... > < fs | snap | vol > ...
1153 * -r recurse over any child datasets
1154 * -H scripted mode. Headers are stripped, and fields are separated
1155 * by tabs instead of spaces.
1156 * -o Set of fields to display. One of "name,property,value,
1157 * received,source". Default is "name,property,value,source".
1158 * "all" is an alias for all five.
1159 * -s Set of sources to allow. One of
1160 * "local,default,inherited,received,temporary,none". Default is
1162 * -p Display values in parsable (literal) format.
1164 * Prints properties for the given datasets. The user can control which
1165 * columns to display as well as which property types to allow.
1169 * Invoked to display the properties for a single dataset.
1172 get_callback(zfs_handle_t *zhp, void *data)
1174 char buf[ZFS_MAXPROPLEN];
1175 char rbuf[ZFS_MAXPROPLEN];
1176 zprop_source_t sourcetype;
1177 char source[ZFS_MAXNAMELEN];
1178 zprop_get_cbdata_t *cbp = data;
1179 nvlist_t *user_props = zfs_get_user_props(zhp);
1180 zprop_list_t *pl = cbp->cb_proplist;
1184 boolean_t received = is_recvd_column(cbp);
1186 for (; pl != NULL; pl = pl->pl_next) {
1187 char *recvdval = NULL;
1189 * Skip the special fake placeholder. This will also skip over
1190 * the name property when 'all' is specified.
1192 if (pl->pl_prop == ZFS_PROP_NAME &&
1193 pl == cbp->cb_proplist)
1196 if (pl->pl_prop != ZPROP_INVAL) {
1197 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1198 sizeof (buf), &sourcetype, source,
1200 cbp->cb_literal) != 0) {
1203 if (!zfs_prop_valid_for_type(pl->pl_prop,
1204 ZFS_TYPE_DATASET)) {
1205 (void) fprintf(stderr,
1206 gettext("No such property '%s'\n"),
1207 zfs_prop_to_name(pl->pl_prop));
1210 sourcetype = ZPROP_SRC_NONE;
1211 (void) strlcpy(buf, "-", sizeof (buf));
1214 if (received && (zfs_prop_get_recvd(zhp,
1215 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1216 cbp->cb_literal) == 0))
1219 zprop_print_one_property(zfs_get_name(zhp), cbp,
1220 zfs_prop_to_name(pl->pl_prop),
1221 buf, sourcetype, source, recvdval);
1222 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1223 sourcetype = ZPROP_SRC_LOCAL;
1225 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1226 buf, sizeof (buf), cbp->cb_literal) != 0) {
1227 sourcetype = ZPROP_SRC_NONE;
1228 (void) strlcpy(buf, "-", sizeof (buf));
1231 zprop_print_one_property(zfs_get_name(zhp), cbp,
1232 pl->pl_user_prop, buf, sourcetype, source, NULL);
1234 if (nvlist_lookup_nvlist(user_props,
1235 pl->pl_user_prop, &propval) != 0) {
1238 sourcetype = ZPROP_SRC_NONE;
1241 verify(nvlist_lookup_string(propval,
1242 ZPROP_VALUE, &strval) == 0);
1243 verify(nvlist_lookup_string(propval,
1244 ZPROP_SOURCE, &sourceval) == 0);
1246 if (strcmp(sourceval,
1247 zfs_get_name(zhp)) == 0) {
1248 sourcetype = ZPROP_SRC_LOCAL;
1249 } else if (strcmp(sourceval,
1250 ZPROP_SOURCE_VAL_RECVD) == 0) {
1251 sourcetype = ZPROP_SRC_RECEIVED;
1253 sourcetype = ZPROP_SRC_INHERITED;
1254 (void) strlcpy(source,
1255 sourceval, sizeof (source));
1259 if (received && (zfs_prop_get_recvd(zhp,
1260 pl->pl_user_prop, rbuf, sizeof (rbuf),
1261 cbp->cb_literal) == 0))
1264 zprop_print_one_property(zfs_get_name(zhp), cbp,
1265 pl->pl_user_prop, strval, sourcetype,
1274 zfs_do_get(int argc, char **argv)
1276 zprop_get_cbdata_t cb = { 0 };
1277 int i, c, flags = 0;
1278 char *value, *fields;
1281 zprop_list_t fake_name = { 0 };
1284 * Set up default columns and sources.
1286 cb.cb_sources = ZPROP_SRC_ALL;
1287 cb.cb_columns[0] = GET_COL_NAME;
1288 cb.cb_columns[1] = GET_COL_PROPERTY;
1289 cb.cb_columns[2] = GET_COL_VALUE;
1290 cb.cb_columns[3] = GET_COL_SOURCE;
1291 cb.cb_type = ZFS_TYPE_DATASET;
1294 while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1297 cb.cb_literal = B_TRUE;
1300 limit = parse_depth(optarg, &flags);
1303 flags |= ZFS_ITER_RECURSE;
1306 cb.cb_scripted = B_TRUE;
1309 (void) fprintf(stderr, gettext("missing argument for "
1310 "'%c' option\n"), optopt);
1315 * Process the set of columns to display. We zero out
1316 * the structure to give us a blank slate.
1318 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1320 while (*optarg != '\0') {
1321 static char *col_subopts[] =
1322 { "name", "property", "value", "received",
1323 "source", "all", NULL };
1325 if (i == ZFS_GET_NCOLS) {
1326 (void) fprintf(stderr, gettext("too "
1327 "many fields given to -o "
1332 switch (getsubopt(&optarg, col_subopts,
1335 cb.cb_columns[i++] = GET_COL_NAME;
1338 cb.cb_columns[i++] = GET_COL_PROPERTY;
1341 cb.cb_columns[i++] = GET_COL_VALUE;
1344 cb.cb_columns[i++] = GET_COL_RECVD;
1345 flags |= ZFS_ITER_RECVD_PROPS;
1348 cb.cb_columns[i++] = GET_COL_SOURCE;
1352 (void) fprintf(stderr,
1353 gettext("\"all\" conflicts "
1354 "with specific fields "
1355 "given to -o option\n"));
1358 cb.cb_columns[0] = GET_COL_NAME;
1359 cb.cb_columns[1] = GET_COL_PROPERTY;
1360 cb.cb_columns[2] = GET_COL_VALUE;
1361 cb.cb_columns[3] = GET_COL_RECVD;
1362 cb.cb_columns[4] = GET_COL_SOURCE;
1363 flags |= ZFS_ITER_RECVD_PROPS;
1367 (void) fprintf(stderr,
1368 gettext("invalid column name "
1377 while (*optarg != '\0') {
1378 static char *source_subopts[] = {
1379 "local", "default", "inherited",
1380 "received", "temporary", "none",
1383 switch (getsubopt(&optarg, source_subopts,
1386 cb.cb_sources |= ZPROP_SRC_LOCAL;
1389 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1392 cb.cb_sources |= ZPROP_SRC_INHERITED;
1395 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1398 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1401 cb.cb_sources |= ZPROP_SRC_NONE;
1404 (void) fprintf(stderr,
1405 gettext("invalid source "
1413 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1423 (void) fprintf(stderr, gettext("missing property "
1430 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1438 * As part of zfs_expand_proplist(), we keep track of the maximum column
1439 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1440 * need to know the maximum name length. However, the user likely did
1441 * not specify 'name' as one of the properties to fetch, so we need to
1442 * make sure we always include at least this property for
1443 * print_get_headers() to work properly.
1445 if (cb.cb_proplist != NULL) {
1446 fake_name.pl_prop = ZFS_PROP_NAME;
1447 fake_name.pl_width = strlen(gettext("NAME"));
1448 fake_name.pl_next = cb.cb_proplist;
1449 cb.cb_proplist = &fake_name;
1452 cb.cb_first = B_TRUE;
1454 /* run for each object */
1455 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1456 &cb.cb_proplist, limit, get_callback, &cb);
1458 if (cb.cb_proplist == &fake_name)
1459 zprop_free_list(fake_name.pl_next);
1461 zprop_free_list(cb.cb_proplist);
1467 * inherit [-rS] <property> <fs|vol> ...
1469 * -r Recurse over all children
1470 * -S Revert to received value, if any
1472 * For each dataset specified on the command line, inherit the given property
1473 * from its parent. Inheriting a property at the pool level will cause it to
1474 * use the default value. The '-r' flag will recurse over all children, and is
1475 * useful for setting a property on a hierarchy-wide basis, regardless of any
1476 * local modifications for each dataset.
1479 typedef struct inherit_cbdata {
1480 const char *cb_propname;
1481 boolean_t cb_received;
1485 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1487 inherit_cbdata_t *cb = data;
1488 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1491 * If we're doing it recursively, then ignore properties that
1492 * are not valid for this type of dataset.
1494 if (prop != ZPROP_INVAL &&
1495 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1498 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1502 inherit_cb(zfs_handle_t *zhp, void *data)
1504 inherit_cbdata_t *cb = data;
1506 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1510 zfs_do_inherit(int argc, char **argv)
1514 inherit_cbdata_t cb = { 0 };
1518 boolean_t received = B_FALSE;
1521 while ((c = getopt(argc, argv, "rS")) != -1) {
1524 flags |= ZFS_ITER_RECURSE;
1531 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1540 /* check number of arguments */
1542 (void) fprintf(stderr, gettext("missing property argument\n"));
1546 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1554 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1555 if (zfs_prop_readonly(prop)) {
1556 (void) fprintf(stderr, gettext(
1557 "%s property is read-only\n"),
1561 if (!zfs_prop_inheritable(prop) && !received) {
1562 (void) fprintf(stderr, gettext("'%s' property cannot "
1563 "be inherited\n"), propname);
1564 if (prop == ZFS_PROP_QUOTA ||
1565 prop == ZFS_PROP_RESERVATION ||
1566 prop == ZFS_PROP_REFQUOTA ||
1567 prop == ZFS_PROP_REFRESERVATION)
1568 (void) fprintf(stderr, gettext("use 'zfs set "
1569 "%s=none' to clear\n"), propname);
1572 if (received && (prop == ZFS_PROP_VOLSIZE ||
1573 prop == ZFS_PROP_VERSION)) {
1574 (void) fprintf(stderr, gettext("'%s' property cannot "
1575 "be reverted to a received value\n"), propname);
1578 } else if (!zfs_prop_user(propname)) {
1579 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1584 cb.cb_propname = propname;
1585 cb.cb_received = received;
1587 if (flags & ZFS_ITER_RECURSE) {
1588 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1589 NULL, NULL, 0, inherit_recurse_cb, &cb);
1591 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1592 NULL, NULL, 0, inherit_cb, &cb);
1598 typedef struct upgrade_cbdata {
1599 uint64_t cb_numupgraded;
1600 uint64_t cb_numsamegraded;
1601 uint64_t cb_numfailed;
1602 uint64_t cb_version;
1604 boolean_t cb_foundone;
1605 char cb_lastfs[ZFS_MAXNAMELEN];
1609 same_pool(zfs_handle_t *zhp, const char *name)
1611 int len1 = strcspn(name, "/@");
1612 const char *zhname = zfs_get_name(zhp);
1613 int len2 = strcspn(zhname, "/@");
1617 return (strncmp(name, zhname, len1) == 0);
1621 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1623 upgrade_cbdata_t *cb = data;
1624 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1626 /* list if it's old/new */
1627 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1628 (cb->cb_newer && version > ZPL_VERSION)) {
1631 str = gettext("The following filesystems are "
1632 "formatted using a newer software version and\n"
1633 "cannot be accessed on the current system.\n\n");
1635 str = gettext("The following filesystems are "
1636 "out of date, and can be upgraded. After being\n"
1637 "upgraded, these filesystems (and any 'zfs send' "
1638 "streams generated from\n"
1639 "subsequent snapshots) will no longer be "
1640 "accessible by older software versions.\n\n");
1643 if (!cb->cb_foundone) {
1645 (void) printf(gettext("VER FILESYSTEM\n"));
1646 (void) printf(gettext("--- ------------\n"));
1647 cb->cb_foundone = B_TRUE;
1650 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1657 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1659 upgrade_cbdata_t *cb = data;
1660 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1661 int needed_spa_version;
1664 if (zfs_spa_version(zhp, &spa_version) < 0)
1667 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1669 if (needed_spa_version < 0)
1672 if (spa_version < needed_spa_version) {
1674 (void) printf(gettext("%s: can not be "
1675 "upgraded; the pool version needs to first "
1676 "be upgraded\nto version %d\n\n"),
1677 zfs_get_name(zhp), needed_spa_version);
1683 if (version < cb->cb_version) {
1685 (void) snprintf(verstr, sizeof (verstr),
1686 "%llu", (u_longlong_t)cb->cb_version);
1687 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1689 * If they did "zfs upgrade -a", then we could
1690 * be doing ioctls to different pools. We need
1691 * to log this history once to each pool.
1693 verify(zpool_stage_history(g_zfs, history_str) == 0);
1695 if (zfs_prop_set(zhp, "version", verstr) == 0)
1696 cb->cb_numupgraded++;
1699 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1700 } else if (version > cb->cb_version) {
1701 /* can't downgrade */
1702 (void) printf(gettext("%s: can not be downgraded; "
1703 "it is already at version %u\n"),
1704 zfs_get_name(zhp), version);
1707 cb->cb_numsamegraded++;
1715 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1718 zfs_do_upgrade(int argc, char **argv)
1720 boolean_t all = B_FALSE;
1721 boolean_t showversions = B_FALSE;
1723 upgrade_cbdata_t cb = { 0 };
1725 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1728 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1731 flags |= ZFS_ITER_RECURSE;
1734 showversions = B_TRUE;
1737 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1738 optarg, &cb.cb_version) != 0) {
1739 (void) fprintf(stderr,
1740 gettext("invalid version %s\n"), optarg);
1749 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1758 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1760 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1761 cb.cb_version || argc))
1763 if ((all || argc) && (showversions))
1769 /* Show info on available versions. */
1770 (void) printf(gettext("The following filesystem versions are "
1772 (void) printf(gettext("VER DESCRIPTION\n"));
1773 (void) printf("--- -----------------------------------------"
1774 "---------------\n");
1775 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1776 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1777 (void) printf(gettext(" 3 Case insensitive and File system "
1778 "unique identifier (FUID)\n"));
1779 (void) printf(gettext(" 4 userquota, groupquota "
1781 (void) printf(gettext(" 5 System attributes\n"));
1782 (void) printf(gettext("\nFor more information on a particular "
1783 "version, including supported releases,\n"));
1784 (void) printf("see the ZFS Administration Guide.\n\n");
1786 } else if (argc || all) {
1787 /* Upgrade filesystems */
1788 if (cb.cb_version == 0)
1789 cb.cb_version = ZPL_VERSION;
1790 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1791 NULL, NULL, 0, upgrade_set_callback, &cb);
1792 (void) printf(gettext("%llu filesystems upgraded\n"),
1793 (u_longlong_t)cb.cb_numupgraded);
1794 if (cb.cb_numsamegraded) {
1795 (void) printf(gettext("%llu filesystems already at "
1797 (u_longlong_t)cb.cb_numsamegraded);
1799 if (cb.cb_numfailed != 0)
1802 /* List old-version filesytems */
1804 (void) printf(gettext("This system is currently running "
1805 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1807 flags |= ZFS_ITER_RECURSE;
1808 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1809 NULL, NULL, 0, upgrade_list_callback, &cb);
1811 found = cb.cb_foundone;
1812 cb.cb_foundone = B_FALSE;
1813 cb.cb_newer = B_TRUE;
1815 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1816 NULL, NULL, 0, upgrade_list_callback, &cb);
1818 if (!cb.cb_foundone && !found) {
1819 (void) printf(gettext("All filesystems are "
1820 "formatted with the current version.\n"));
1831 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
1833 zfs_userquota_prop_t *typep = arg;
1834 zfs_userquota_prop_t p = *typep;
1836 char *ug, *propname;
1840 if (domain == NULL || domain[0] == '\0') {
1841 if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) {
1842 struct group *g = getgrgid(rid);
1846 struct passwd *p = getpwuid(rid);
1852 if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA)
1857 if (p == ZFS_PROP_USERUSED || p == ZFS_PROP_GROUPUSED)
1863 (void) snprintf(namebuf, sizeof (namebuf),
1864 "%llu", (longlong_t)rid);
1867 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
1869 (void) printf("%s %s %s%c%s %s\n", propname, ug, domain,
1870 domain[0] ? '-' : ' ', name, sizebuf);
1876 zfs_do_userspace(int argc, char **argv)
1879 zfs_userquota_prop_t p;
1883 * Try the python version. If the execv fails, we'll continue
1884 * and do a simplistic implementation.
1886 (void) execv(pypath, argv-1);
1888 (void) printf("internal error: %s not found\n"
1889 "falling back on built-in implementation, "
1890 "some features will not work\n", pypath);
1892 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
1895 (void) printf("PROP TYPE NAME VALUE\n");
1897 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
1898 error = zfs_userspace(zhp, p, userspace_cb, &p);
1906 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
1907 * [-s property [-s property]...] [-S property [-S property]...]
1910 * -r Recurse over all children
1911 * -d Limit recursion by depth.
1912 * -H Scripted mode; elide headers and separate columns by tabs
1913 * -o Control which fields to display.
1914 * -t Control which object types to display.
1915 * -s Specify sort columns, descending order.
1916 * -S Specify sort columns, ascending order.
1918 * When given no arguments, lists all filesystems in the system.
1919 * Otherwise, list the specified datasets, optionally recursing down them if
1920 * '-r' is specified.
1922 typedef struct list_cbdata {
1924 boolean_t cb_scripted;
1925 zprop_list_t *cb_proplist;
1929 * Given a list of columns to display, output appropriate headers for each one.
1932 print_header(zprop_list_t *pl)
1934 char headerbuf[ZFS_MAXPROPLEN];
1937 boolean_t first = B_TRUE;
1938 boolean_t right_justify;
1940 for (; pl != NULL; pl = pl->pl_next) {
1947 right_justify = B_FALSE;
1948 if (pl->pl_prop != ZPROP_INVAL) {
1949 header = zfs_prop_column_name(pl->pl_prop);
1950 right_justify = zfs_prop_align_right(pl->pl_prop);
1952 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1953 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1954 headerbuf[i] = '\0';
1958 if (pl->pl_next == NULL && !right_justify)
1959 (void) printf("%s", header);
1960 else if (right_justify)
1961 (void) printf("%*s", (int)pl->pl_width, header);
1963 (void) printf("%-*s", (int)pl->pl_width, header);
1966 (void) printf("\n");
1970 * Given a dataset and a list of fields, print out all the properties according
1971 * to the described layout.
1974 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1976 boolean_t first = B_TRUE;
1977 char property[ZFS_MAXPROPLEN];
1978 nvlist_t *userprops = zfs_get_user_props(zhp);
1981 boolean_t right_justify;
1984 for (; pl != NULL; pl = pl->pl_next) {
1987 (void) printf("\t");
1994 if (pl->pl_prop != ZPROP_INVAL) {
1995 if (zfs_prop_get(zhp, pl->pl_prop, property,
1996 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2001 right_justify = zfs_prop_align_right(pl->pl_prop);
2002 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2003 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2004 property, sizeof (property), B_FALSE) != 0)
2008 right_justify = B_TRUE;
2010 if (nvlist_lookup_nvlist(userprops,
2011 pl->pl_user_prop, &propval) != 0)
2014 verify(nvlist_lookup_string(propval,
2015 ZPROP_VALUE, &propstr) == 0);
2016 right_justify = B_FALSE;
2019 width = pl->pl_width;
2022 * If this is being called in scripted mode, or if this is the
2023 * last column and it is left-justified, don't include a width
2026 if (scripted || (pl->pl_next == NULL && !right_justify))
2027 (void) printf("%s", propstr);
2028 else if (right_justify)
2029 (void) printf("%*s", width, propstr);
2031 (void) printf("%-*s", width, propstr);
2034 (void) printf("\n");
2038 * Generic callback function to list a dataset or snapshot.
2041 list_callback(zfs_handle_t *zhp, void *data)
2043 list_cbdata_t *cbp = data;
2045 if (cbp->cb_first) {
2046 if (!cbp->cb_scripted)
2047 print_header(cbp->cb_proplist);
2048 cbp->cb_first = B_FALSE;
2051 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2057 zfs_do_list(int argc, char **argv)
2060 boolean_t scripted = B_FALSE;
2061 static char default_fields[] =
2062 "name,used,available,referenced,mountpoint";
2063 int types = ZFS_TYPE_DATASET;
2064 boolean_t types_specified = B_FALSE;
2065 char *fields = NULL;
2066 list_cbdata_t cb = { 0 };
2070 zfs_sort_column_t *sortcol = NULL;
2071 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2074 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2080 limit = parse_depth(optarg, &flags);
2083 flags |= ZFS_ITER_RECURSE;
2089 if (zfs_add_sort_column(&sortcol, optarg,
2091 (void) fprintf(stderr,
2092 gettext("invalid property '%s'\n"), optarg);
2097 if (zfs_add_sort_column(&sortcol, optarg,
2099 (void) fprintf(stderr,
2100 gettext("invalid property '%s'\n"), optarg);
2106 types_specified = B_TRUE;
2107 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2108 while (*optarg != '\0') {
2109 static char *type_subopts[] = { "filesystem",
2110 "volume", "snapshot", "all", NULL };
2112 switch (getsubopt(&optarg, type_subopts,
2115 types |= ZFS_TYPE_FILESYSTEM;
2118 types |= ZFS_TYPE_VOLUME;
2121 types |= ZFS_TYPE_SNAPSHOT;
2124 types = ZFS_TYPE_DATASET;
2128 (void) fprintf(stderr,
2129 gettext("invalid type '%s'\n"),
2136 (void) fprintf(stderr, gettext("missing argument for "
2137 "'%c' option\n"), optopt);
2141 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2151 fields = default_fields;
2154 * If "-o space" and no types were specified, don't display snapshots.
2156 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
2157 types &= ~ZFS_TYPE_SNAPSHOT;
2160 * If the user specifies '-o all', the zprop_get_list() doesn't
2161 * normally include the name of the dataset. For 'zfs list', we always
2162 * want this property to be first.
2164 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2168 cb.cb_scripted = scripted;
2169 cb.cb_first = B_TRUE;
2171 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
2172 limit, list_callback, &cb);
2174 zprop_free_list(cb.cb_proplist);
2175 zfs_free_sort_columns(sortcol);
2177 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
2178 (void) printf(gettext("no datasets available\n"));
2184 * zfs rename <fs | snap | vol> <fs | snap | vol>
2185 * zfs rename -p <fs | vol> <fs | vol>
2186 * zfs rename -r <snap> <snap>
2188 * Renames the given dataset to another of the same type.
2190 * The '-p' flag creates all the non-existing ancestors of the target first.
2194 zfs_do_rename(int argc, char **argv)
2199 boolean_t recurse = B_FALSE;
2200 boolean_t parents = B_FALSE;
2203 while ((c = getopt(argc, argv, "pr")) != -1) {
2213 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2222 /* check number of arguments */
2224 (void) fprintf(stderr, gettext("missing source dataset "
2229 (void) fprintf(stderr, gettext("missing target dataset "
2234 (void) fprintf(stderr, gettext("too many arguments\n"));
2238 if (recurse && parents) {
2239 (void) fprintf(stderr, gettext("-p and -r options are mutually "
2244 if (recurse && strchr(argv[0], '@') == 0) {
2245 (void) fprintf(stderr, gettext("source dataset for recursive "
2246 "rename must be a snapshot\n"));
2250 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
2251 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
2254 /* If we were asked and the name looks good, try to create ancestors. */
2255 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
2256 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
2261 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
2270 * Promotes the given clone fs to be the parent
2274 zfs_do_promote(int argc, char **argv)
2280 if (argc > 1 && argv[1][0] == '-') {
2281 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2286 /* check number of arguments */
2288 (void) fprintf(stderr, gettext("missing clone filesystem"
2293 (void) fprintf(stderr, gettext("too many arguments\n"));
2297 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2301 ret = (zfs_promote(zhp) != 0);
2309 * zfs rollback [-rRf] <snapshot>
2311 * -r Delete any intervening snapshots before doing rollback
2312 * -R Delete any snapshots and their clones
2313 * -f ignored for backwards compatability
2315 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2316 * since then and making it the active dataset. If more recent snapshots exist,
2317 * the command will complain unless the '-r' flag is given.
2319 typedef struct rollback_cbdata {
2325 boolean_t cb_recurse;
2326 boolean_t cb_dependent;
2327 } rollback_cbdata_t;
2330 * Report any snapshots more recent than the one specified. Used when '-r' is
2331 * not specified. We reuse this same callback for the snapshot dependents - if
2332 * 'cb_dependent' is set, then this is a dependent and we should report it
2333 * without checking the transaction group.
2336 rollback_check(zfs_handle_t *zhp, void *data)
2338 rollback_cbdata_t *cbp = data;
2340 if (cbp->cb_doclones) {
2345 if (!cbp->cb_dependent) {
2346 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2347 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2348 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2351 if (cbp->cb_first && !cbp->cb_recurse) {
2352 (void) fprintf(stderr, gettext("cannot "
2353 "rollback to '%s': more recent snapshots "
2356 (void) fprintf(stderr, gettext("use '-r' to "
2357 "force deletion of the following "
2363 if (cbp->cb_recurse) {
2364 cbp->cb_dependent = B_TRUE;
2365 if (zfs_iter_dependents(zhp, B_TRUE,
2366 rollback_check, cbp) != 0) {
2370 cbp->cb_dependent = B_FALSE;
2372 (void) fprintf(stderr, "%s\n",
2377 if (cbp->cb_first && cbp->cb_recurse) {
2378 (void) fprintf(stderr, gettext("cannot rollback to "
2379 "'%s': clones of previous snapshots exist\n"),
2381 (void) fprintf(stderr, gettext("use '-R' to "
2382 "force deletion of the following clones and "
2388 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2396 zfs_do_rollback(int argc, char **argv)
2400 boolean_t force = B_FALSE;
2401 rollback_cbdata_t cb = { 0 };
2402 zfs_handle_t *zhp, *snap;
2403 char parentname[ZFS_MAXNAMELEN];
2407 while ((c = getopt(argc, argv, "rRf")) != -1) {
2420 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2429 /* check number of arguments */
2431 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2435 (void) fprintf(stderr, gettext("too many arguments\n"));
2439 /* open the snapshot */
2440 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2443 /* open the parent dataset */
2444 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2445 verify((delim = strrchr(parentname, '@')) != NULL);
2447 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2453 * Check for more recent snapshots and/or clones based on the presence
2456 cb.cb_target = argv[0];
2457 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2458 cb.cb_first = B_TRUE;
2460 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2463 if ((ret = cb.cb_error) != 0)
2467 * Rollback parent to the given snapshot.
2469 ret = zfs_rollback(zhp, snap, force);
2482 * zfs set property=value { fs | snap | vol } ...
2484 * Sets the given property for all datasets specified on the command line.
2486 typedef struct set_cbdata {
2492 set_callback(zfs_handle_t *zhp, void *data)
2494 set_cbdata_t *cbp = data;
2496 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2497 switch (libzfs_errno(g_zfs)) {
2498 case EZFS_MOUNTFAILED:
2499 (void) fprintf(stderr, gettext("property may be set "
2500 "but unable to remount filesystem\n"));
2502 case EZFS_SHARENFSFAILED:
2503 (void) fprintf(stderr, gettext("property may be set "
2504 "but unable to reshare filesystem\n"));
2513 zfs_do_set(int argc, char **argv)
2518 /* check for options */
2519 if (argc > 1 && argv[1][0] == '-') {
2520 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2525 /* check number of arguments */
2527 (void) fprintf(stderr, gettext("missing property=value "
2532 (void) fprintf(stderr, gettext("missing dataset name\n"));
2536 /* validate property=value argument */
2537 cb.cb_propname = argv[1];
2538 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2539 (cb.cb_value[1] == '\0')) {
2540 (void) fprintf(stderr, gettext("missing value in "
2541 "property=value argument\n"));
2545 *cb.cb_value = '\0';
2548 if (*cb.cb_propname == '\0') {
2549 (void) fprintf(stderr,
2550 gettext("missing property in property=value argument\n"));
2554 ret = zfs_for_each(argc - 2, argv + 2, 0,
2555 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
2561 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2563 * Creates a snapshot with the given name. While functionally equivalent to
2564 * 'zfs create', it is a separate command to differentiate intent.
2567 zfs_do_snapshot(int argc, char **argv)
2569 boolean_t recursive = B_FALSE;
2574 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2578 while ((c = getopt(argc, argv, "ro:")) != -1) {
2581 if (parseprop(props))
2588 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2597 /* check number of arguments */
2599 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2603 (void) fprintf(stderr, gettext("too many arguments\n"));
2607 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2609 if (ret && recursive)
2610 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2620 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
2621 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
2623 * Send a backup stream to stdout.
2626 zfs_do_send(int argc, char **argv)
2628 char *fromname = NULL;
2629 char *toname = NULL;
2632 sendflags_t flags = { 0 };
2635 boolean_t extraverbose = B_FALSE;
2638 while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) {
2649 flags.doall = B_TRUE;
2652 flags.replicate = B_TRUE;
2655 flags.props = B_TRUE;
2659 extraverbose = B_TRUE;
2660 flags.verbose = B_TRUE;
2663 flags.dedup = B_TRUE;
2666 (void) fprintf(stderr, gettext("missing argument for "
2667 "'%c' option\n"), optopt);
2671 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2680 /* check number of arguments */
2682 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2686 (void) fprintf(stderr, gettext("too many arguments\n"));
2690 if (isatty(STDOUT_FILENO)) {
2691 (void) fprintf(stderr,
2692 gettext("Error: Stream can not be written to a terminal.\n"
2693 "You must redirect standard output.\n"));
2697 cp = strchr(argv[0], '@');
2699 (void) fprintf(stderr,
2700 gettext("argument must be a snapshot\n"));
2705 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2710 * If they specified the full path to the snapshot, chop off
2711 * everything except the short name of the snapshot, but special
2712 * case if they specify the origin.
2714 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2715 char origin[ZFS_MAXNAMELEN];
2718 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2719 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2721 if (strcmp(origin, fromname) == 0) {
2723 flags.fromorigin = B_TRUE;
2726 if (cp != fromname && strcmp(argv[0], fromname)) {
2727 (void) fprintf(stderr,
2728 gettext("incremental source must be "
2729 "in same filesystem\n"));
2733 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2734 (void) fprintf(stderr,
2735 gettext("invalid incremental source\n"));
2741 if (flags.replicate && fromname == NULL)
2742 flags.doall = B_TRUE;
2744 err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0,
2745 extraverbose ? &dbgnv : NULL);
2749 * dump_nvlist prints to stdout, but that's been
2750 * redirected to a file. Make it print to stderr
2753 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
2754 dump_nvlist(dbgnv, 0);
2763 * zfs receive [-vnFu] [-d | -e] <fs@snap>
2765 * Restore a backup stream from stdin.
2768 zfs_do_receive(int argc, char **argv)
2771 recvflags_t flags = { 0 };
2774 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
2777 flags.isprefix = B_TRUE;
2780 flags.isprefix = B_TRUE;
2781 flags.istail = B_TRUE;
2784 flags.dryrun = B_TRUE;
2787 flags.nomount = B_TRUE;
2790 flags.verbose = B_TRUE;
2793 flags.force = B_TRUE;
2796 (void) fprintf(stderr, gettext("missing argument for "
2797 "'%c' option\n"), optopt);
2801 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2810 /* check number of arguments */
2812 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2816 (void) fprintf(stderr, gettext("too many arguments\n"));
2820 if (isatty(STDIN_FILENO)) {
2821 (void) fprintf(stderr,
2822 gettext("Error: Backup stream can not be read "
2823 "from a terminal.\n"
2824 "You must redirect standard input.\n"));
2828 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2834 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
2839 boolean_t recursive = B_FALSE;
2840 boolean_t temphold = B_FALSE;
2841 const char *opts = holding ? "rt" : "r";
2845 while ((c = getopt(argc, argv, opts)) != -1) {
2854 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2863 /* check number of arguments */
2871 if (holding && tag[0] == '.') {
2872 /* tags starting with '.' are reserved for libzfs */
2873 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
2877 for (i = 0; i < argc; ++i) {
2879 char parent[ZFS_MAXNAMELEN];
2881 char *path = argv[i];
2883 delim = strchr(path, '@');
2884 if (delim == NULL) {
2885 (void) fprintf(stderr,
2886 gettext("'%s' is not a snapshot\n"), path);
2890 (void) strncpy(parent, path, delim - path);
2891 parent[delim - path] = '\0';
2893 zhp = zfs_open(g_zfs, parent,
2894 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2900 if (zfs_hold(zhp, delim+1, tag, recursive,
2901 temphold, B_FALSE, -1, 0, 0) != 0)
2904 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
2910 return (errors != 0);
2914 * zfs hold [-r] [-t] <tag> <snap> ...
2916 * -r Recursively hold
2917 * -t Temporary hold (hidden option)
2919 * Apply a user-hold with the given tag to the list of snapshots.
2922 zfs_do_hold(int argc, char **argv)
2924 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
2928 * zfs release [-r] <tag> <snap> ...
2930 * -r Recursively release
2932 * Release a user-hold with the given tag from the list of snapshots.
2935 zfs_do_release(int argc, char **argv)
2937 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
2940 #define CHECK_SPINNER 30
2941 #define SPINNER_TIME 3 /* seconds */
2942 #define MOUNT_TIME 5 /* seconds */
2945 get_one_dataset(zfs_handle_t *zhp, void *data)
2947 static char *spin[] = { "-", "\\", "|", "/" };
2948 static int spinval = 0;
2949 static int spincheck = 0;
2950 static time_t last_spin_time = (time_t)0;
2951 get_all_cb_t *cbp = data;
2952 zfs_type_t type = zfs_get_type(zhp);
2954 if (cbp->cb_verbose) {
2955 if (--spincheck < 0) {
2956 time_t now = time(NULL);
2957 if (last_spin_time + SPINNER_TIME < now) {
2958 update_progress(spin[spinval++ % 4]);
2959 last_spin_time = now;
2961 spincheck = CHECK_SPINNER;
2966 * Interate over any nested datasets.
2968 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2974 * Skip any datasets whose type does not match.
2976 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
2980 libzfs_add_handle(cbp, zhp);
2981 assert(cbp->cb_used <= cbp->cb_alloc);
2987 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
2989 get_all_cb_t cb = { 0 };
2990 cb.cb_verbose = verbose;
2991 cb.cb_getone = get_one_dataset;
2994 set_progress_header(gettext("Reading ZFS config"));
2995 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2997 *dslist = cb.cb_handles;
2998 *count = cb.cb_used;
3001 finish_progress(gettext("done."));
3005 * Generic callback for sharing or mounting filesystems. Because the code is so
3006 * similar, we have a common function with an extra parameter to determine which
3007 * mode we are using.
3009 #define OP_SHARE 0x1
3010 #define OP_MOUNT 0x2
3013 * Share or mount a dataset.
3016 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3017 boolean_t explicit, const char *options)
3019 char mountpoint[ZFS_MAXPROPLEN];
3020 char shareopts[ZFS_MAXPROPLEN];
3021 char smbshareopts[ZFS_MAXPROPLEN];
3022 const char *cmdname = op == OP_SHARE ? "share" : "mount";
3024 uint64_t zoned, canmount;
3025 boolean_t shared_nfs, shared_smb;
3027 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
3030 * Check to make sure we can mount/share this dataset. If we
3031 * are in the global zone and the filesystem is exported to a
3032 * local zone, or if we are in a local zone and the
3033 * filesystem is not exported, then it is an error.
3035 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3037 if (zoned && getzoneid() == GLOBAL_ZONEID) {
3041 (void) fprintf(stderr, gettext("cannot %s '%s': "
3042 "dataset is exported to a local zone\n"), cmdname,
3046 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3050 (void) fprintf(stderr, gettext("cannot %s '%s': "
3051 "permission denied\n"), cmdname,
3057 * Ignore any filesystems which don't apply to us. This
3058 * includes those with a legacy mountpoint, or those with
3059 * legacy share options.
3061 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3062 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3063 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3064 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3065 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3066 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3068 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3069 strcmp(smbshareopts, "off") == 0) {
3073 (void) fprintf(stderr, gettext("cannot share '%s': "
3074 "legacy share\n"), zfs_get_name(zhp));
3075 (void) fprintf(stderr, gettext("use share(1M) to "
3076 "share this filesystem, or set "
3077 "sharenfs property on\n"));
3082 * We cannot share or mount legacy filesystems. If the
3083 * shareopts is non-legacy but the mountpoint is legacy, we
3084 * treat it as a legacy share.
3086 if (strcmp(mountpoint, "legacy") == 0) {
3090 (void) fprintf(stderr, gettext("cannot %s '%s': "
3091 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3092 (void) fprintf(stderr, gettext("use %s(1M) to "
3093 "%s this filesystem\n"), cmdname, cmdname);
3097 if (strcmp(mountpoint, "none") == 0) {
3101 (void) fprintf(stderr, gettext("cannot %s '%s': no "
3102 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3107 * canmount explicit outcome
3108 * on no pass through
3109 * on yes pass through
3111 * off yes display error, return 1
3112 * noauto no return 0
3113 * noauto yes pass through
3115 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3116 if (canmount == ZFS_CANMOUNT_OFF) {
3120 (void) fprintf(stderr, gettext("cannot %s '%s': "
3121 "'canmount' property is set to 'off'\n"), cmdname,
3124 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3129 * At this point, we have verified that the mountpoint and/or
3130 * shareopts are appropriate for auto management. If the
3131 * filesystem is already mounted or shared, return (failing
3132 * for explicit requests); otherwise mount or share the
3138 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3139 shared_smb = zfs_is_shared_smb(zhp, NULL);
3141 if ((shared_nfs && shared_smb) ||
3142 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
3143 (strcmp(smbshareopts, "off") == 0)) ||
3144 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
3145 (strcmp(shareopts, "off") == 0))) {
3149 (void) fprintf(stderr, gettext("cannot share "
3150 "'%s': filesystem already shared\n"),
3155 if (!zfs_is_mounted(zhp, NULL) &&
3156 zfs_mount(zhp, NULL, 0) != 0)
3159 if (protocol == NULL) {
3160 if (zfs_shareall(zhp) != 0)
3162 } else if (strcmp(protocol, "nfs") == 0) {
3163 if (zfs_share_nfs(zhp))
3165 } else if (strcmp(protocol, "smb") == 0) {
3166 if (zfs_share_smb(zhp))
3169 (void) fprintf(stderr, gettext("cannot share "
3170 "'%s': invalid share type '%s' "
3172 zfs_get_name(zhp), protocol);
3179 if (options == NULL)
3180 mnt.mnt_mntopts = "";
3182 mnt.mnt_mntopts = (char *)options;
3184 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3185 zfs_is_mounted(zhp, NULL)) {
3189 (void) fprintf(stderr, gettext("cannot mount "
3190 "'%s': filesystem already mounted\n"),
3195 if (zfs_mount(zhp, options, flags) != 0)
3204 * Reports progress in the form "(current/total)". Not thread-safe.
3207 report_mount_progress(int current, int total)
3209 static time_t last_progress_time = 0;
3210 time_t now = time(NULL);
3213 /* report 1..n instead of 0..n-1 */
3216 /* display header if we're here for the first time */
3218 set_progress_header(gettext("Mounting ZFS filesystems"));
3219 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3220 /* too soon to report again */
3224 last_progress_time = now;
3226 (void) sprintf(info, "(%d/%d)", current, total);
3228 if (current == total)
3229 finish_progress(info);
3231 update_progress(info);
3235 append_options(char *mntopts, char *newopts)
3237 int len = strlen(mntopts);
3239 /* original length plus new string to append plus 1 for the comma */
3240 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3241 (void) fprintf(stderr, gettext("the opts argument for "
3242 "'%s' option is too long (more than %d chars)\n"),
3243 "-o", MNT_LINE_MAX);
3248 mntopts[len++] = ',';
3250 (void) strcpy(&mntopts[len], newopts);
3254 share_mount(int op, int argc, char **argv)
3257 boolean_t verbose = B_FALSE;
3259 char *options = NULL;
3263 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:" : "a"))
3273 if (*optarg == '\0') {
3274 (void) fprintf(stderr, gettext("empty mount "
3275 "options (-o) specified\n"));
3279 if (options == NULL)
3280 options = safe_malloc(MNT_LINE_MAX + 1);
3282 /* option validation is done later */
3283 append_options(options, optarg);
3287 (void) fprintf(stderr, gettext("missing argument for "
3288 "'%c' option\n"), optopt);
3292 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3301 /* check number of arguments */
3303 zfs_handle_t **dslist = NULL;
3304 size_t i, count = 0;
3305 char *protocol = NULL;
3307 if (op == OP_SHARE && argc > 0) {
3308 if (strcmp(argv[0], "nfs") != 0 &&
3309 strcmp(argv[0], "smb") != 0) {
3310 (void) fprintf(stderr, gettext("share type "
3311 "must be 'nfs' or 'smb'\n"));
3320 (void) fprintf(stderr, gettext("too many arguments\n"));
3324 start_progress_timer();
3325 get_all_datasets(&dslist, &count, verbose);
3330 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
3332 for (i = 0; i < count; i++) {
3334 report_mount_progress(i, count);
3336 if (share_mount_one(dslist[i], op, flags, protocol,
3337 B_FALSE, options) != 0)
3339 zfs_close(dslist[i]);
3343 } else if (argc == 0) {
3344 struct mnttab entry;
3346 if ((op == OP_SHARE) || (options != NULL)) {
3347 (void) fprintf(stderr, gettext("missing filesystem "
3348 "argument (specify -a for all)\n"));
3353 * When mount is given no arguments, go through /etc/mtab and
3354 * display any active ZFS mounts. We hide any snapshots, since
3355 * they are controlled automatically.
3357 rewind(mnttab_file);
3358 while (getmntent(mnttab_file, &entry) == 0) {
3359 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3360 strchr(entry.mnt_special, '@') != NULL)
3363 (void) printf("%-30s %s\n", entry.mnt_special,
3371 (void) fprintf(stderr,
3372 gettext("too many arguments\n"));
3376 if ((zhp = zfs_open(g_zfs, argv[0],
3377 ZFS_TYPE_FILESYSTEM)) == NULL) {
3380 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3390 * zfs mount -a [nfs]
3391 * zfs mount filesystem
3393 * Mount all filesystems, or mount the given filesystem.
3396 zfs_do_mount(int argc, char **argv)
3398 return (share_mount(OP_MOUNT, argc, argv));
3402 * zfs share -a [nfs | smb]
3403 * zfs share filesystem
3405 * Share all filesystems, or share the given filesystem.
3408 zfs_do_share(int argc, char **argv)
3410 return (share_mount(OP_SHARE, argc, argv));
3413 typedef struct unshare_unmount_node {
3414 zfs_handle_t *un_zhp;
3416 uu_avl_node_t un_avlnode;
3417 } unshare_unmount_node_t;
3421 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3423 const unshare_unmount_node_t *l = larg;
3424 const unshare_unmount_node_t *r = rarg;
3426 return (strcmp(l->un_mountp, r->un_mountp));
3430 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3431 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
3432 * and unmount it appropriately.
3435 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3439 struct stat64 statbuf;
3440 struct extmnttab entry;
3441 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3445 * Search for the path in /etc/mtab. Rather than looking for the
3446 * specific path, which can be fooled by non-standard paths (i.e. ".."
3447 * or "//"), we stat() the path and search for the corresponding
3448 * (major,minor) device pair.
3450 if (stat64(path, &statbuf) != 0) {
3451 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3452 cmdname, path, strerror(errno));
3455 path_inode = statbuf.st_ino;
3458 * Search for the given (major,minor) pair in the mount table.
3460 rewind(mnttab_file);
3461 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3462 if (entry.mnt_major == major(statbuf.st_dev) &&
3463 entry.mnt_minor == minor(statbuf.st_dev))
3467 if (op == OP_SHARE) {
3468 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3469 "currently mounted\n"), cmdname, path);
3472 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
3474 if ((ret = umount2(path, flags)) != 0)
3475 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3480 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3481 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3482 "filesystem\n"), cmdname, path);
3486 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3487 ZFS_TYPE_FILESYSTEM)) == NULL)
3491 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3492 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3493 cmdname, path, strerror(errno));
3495 } else if (statbuf.st_ino != path_inode) {
3496 (void) fprintf(stderr, gettext("cannot "
3497 "%s '%s': not a mountpoint\n"), cmdname, path);
3501 if (op == OP_SHARE) {
3502 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3503 char smbshare_prop[ZFS_MAXPROPLEN];
3505 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3506 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3507 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3508 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3510 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3511 strcmp(smbshare_prop, "off") == 0) {
3512 (void) fprintf(stderr, gettext("cannot unshare "
3513 "'%s': legacy share\n"), path);
3514 (void) fprintf(stderr, gettext("use exportfs(8) "
3515 "or smbcontrol(1) to unshare this filesystem\n"));
3516 } else if (!zfs_is_shared(zhp)) {
3517 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3518 "not currently shared\n"), path);
3520 ret = zfs_unshareall_bypath(zhp, path);
3523 char mtpt_prop[ZFS_MAXPROPLEN];
3525 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3526 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3529 ret = zfs_unmount(zhp, NULL, flags);
3530 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3531 (void) fprintf(stderr, gettext("cannot unmount "
3532 "'%s': legacy mountpoint\n"),
3534 (void) fprintf(stderr, gettext("use umount(8) "
3535 "to unmount this filesystem\n"));
3537 ret = zfs_unmountall(zhp, flags);
3548 * Generic callback for unsharing or unmounting a filesystem.
3551 unshare_unmount(int op, int argc, char **argv)
3558 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3559 char sharesmb[ZFS_MAXPROPLEN];
3562 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3571 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3582 * We could make use of zfs_for_each() to walk all datasets in
3583 * the system, but this would be very inefficient, especially
3584 * since we would have to linearly search /etc/mtab for each
3585 * one. Instead, do one pass through /etc/mtab looking for
3586 * zfs entries and call zfs_unmount() for each one.
3588 * Things get a little tricky if the administrator has created
3589 * mountpoints beneath other ZFS filesystems. In this case, we
3590 * have to unmount the deepest filesystems first. To accomplish
3591 * this, we place all the mountpoints in an AVL tree sorted by
3592 * the special type (dataset name), and walk the result in
3593 * reverse to make sure to get any snapshots first.
3595 struct mnttab entry;
3596 uu_avl_pool_t *pool;
3597 uu_avl_t *tree = NULL;
3598 unshare_unmount_node_t *node;
3600 uu_avl_walk_t *walk;
3603 (void) fprintf(stderr, gettext("too many arguments\n"));
3607 if (((pool = uu_avl_pool_create("unmount_pool",
3608 sizeof (unshare_unmount_node_t),
3609 offsetof(unshare_unmount_node_t, un_avlnode),
3610 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
3611 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
3614 rewind(mnttab_file);
3615 while (getmntent(mnttab_file, &entry) == 0) {
3617 /* ignore non-ZFS entries */
3618 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3621 /* ignore snapshots */
3622 if (strchr(entry.mnt_special, '@') != NULL)
3625 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3626 ZFS_TYPE_FILESYSTEM)) == NULL) {
3633 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3635 sizeof (nfs_mnt_prop),
3636 NULL, NULL, 0, B_FALSE) == 0);
3637 if (strcmp(nfs_mnt_prop, "off") != 0)
3639 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3641 sizeof (nfs_mnt_prop),
3642 NULL, NULL, 0, B_FALSE) == 0);
3643 if (strcmp(nfs_mnt_prop, "off") == 0)
3647 /* Ignore legacy mounts */
3648 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3650 sizeof (nfs_mnt_prop),
3651 NULL, NULL, 0, B_FALSE) == 0);
3652 if (strcmp(nfs_mnt_prop, "legacy") == 0)
3654 /* Ignore canmount=noauto mounts */
3655 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3656 ZFS_CANMOUNT_NOAUTO)
3662 node = safe_malloc(sizeof (unshare_unmount_node_t));
3664 node->un_mountp = safe_strdup(entry.mnt_mountp);
3666 uu_avl_node_init(node, &node->un_avlnode, pool);
3668 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3669 uu_avl_insert(tree, node, idx);
3671 zfs_close(node->un_zhp);
3672 free(node->un_mountp);
3678 * Walk the AVL tree in reverse, unmounting each filesystem and
3679 * removing it from the AVL tree in the process.
3681 if ((walk = uu_avl_walk_start(tree,
3682 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
3685 while ((node = uu_avl_walk_next(walk)) != NULL) {
3686 uu_avl_remove(tree, node);
3690 if (zfs_unshareall_bypath(node->un_zhp,
3691 node->un_mountp) != 0)
3696 if (zfs_unmount(node->un_zhp,
3697 node->un_mountp, flags) != 0)
3702 zfs_close(node->un_zhp);
3703 free(node->un_mountp);
3707 uu_avl_walk_end(walk);
3708 uu_avl_destroy(tree);
3709 uu_avl_pool_destroy(pool);
3714 (void) fprintf(stderr,
3715 gettext("missing filesystem argument\n"));
3717 (void) fprintf(stderr,
3718 gettext("too many arguments\n"));
3723 * We have an argument, but it may be a full path or a ZFS
3724 * filesystem. Pass full paths off to unmount_path() (shared by
3725 * manual_unmount), otherwise open the filesystem and pass to
3728 if (argv[0][0] == '/')
3729 return (unshare_unmount_path(op, argv[0],
3732 if ((zhp = zfs_open(g_zfs, argv[0],
3733 ZFS_TYPE_FILESYSTEM)) == NULL)
3736 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3737 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3738 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
3739 NULL, 0, B_FALSE) == 0);
3743 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3745 sizeof (nfs_mnt_prop),
3746 NULL, NULL, 0, B_FALSE) == 0);
3747 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3748 sharesmb, sizeof (sharesmb), NULL, NULL,
3751 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3752 strcmp(sharesmb, "off") == 0) {
3753 (void) fprintf(stderr, gettext("cannot "
3754 "unshare '%s': legacy share\n"),
3756 (void) fprintf(stderr, gettext("use "
3757 "unshare(1M) to unshare this "
3760 } else if (!zfs_is_shared(zhp)) {
3761 (void) fprintf(stderr, gettext("cannot "
3762 "unshare '%s': not currently "
3763 "shared\n"), zfs_get_name(zhp));
3765 } else if (zfs_unshareall(zhp) != 0) {
3771 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
3772 (void) fprintf(stderr, gettext("cannot "
3773 "unmount '%s': legacy "
3774 "mountpoint\n"), zfs_get_name(zhp));
3775 (void) fprintf(stderr, gettext("use "
3776 "umount(1M) to unmount this "
3779 } else if (!zfs_is_mounted(zhp, NULL)) {
3780 (void) fprintf(stderr, gettext("cannot "
3781 "unmount '%s': not currently "
3785 } else if (zfs_unmountall(zhp, flags) != 0) {
3799 * zfs unmount filesystem
3801 * Unmount all filesystems, or a specific ZFS filesystem.
3804 zfs_do_unmount(int argc, char **argv)
3806 return (unshare_unmount(OP_MOUNT, argc, argv));
3811 * zfs unshare filesystem
3813 * Unshare all filesystems, or a specific ZFS filesystem.
3816 zfs_do_unshare(int argc, char **argv)
3818 return (unshare_unmount(OP_SHARE, argc, argv));
3823 zfs_do_python(int argc, char **argv)
3825 (void) execv(pypath, argv-1);
3826 (void) printf("internal error: %s not found\n", pypath);
3830 typedef struct option_map {
3835 static const option_map_t option_map[] = {
3836 /* Canonicalized filesystem independent options from mount(8) */
3837 { MNTOPT_NOAUTO, MS_COMMENT },
3838 { MNTOPT_DEFAULTS, MS_COMMENT },
3839 { MNTOPT_NODEVICES, MS_NODEV },
3840 { MNTOPT_DIRSYNC, MS_DIRSYNC },
3841 { MNTOPT_NOEXEC, MS_NOEXEC },
3842 { MNTOPT_GROUP, MS_GROUP },
3843 { MNTOPT_NETDEV, MS_COMMENT },
3844 { MNTOPT_NOFAIL, MS_COMMENT },
3845 { MNTOPT_NOSUID, MS_NOSUID },
3846 { MNTOPT_OWNER, MS_OWNER },
3847 { MNTOPT_REMOUNT, MS_REMOUNT },
3848 { MNTOPT_RO, MS_RDONLY },
3849 { MNTOPT_SYNC, MS_SYNCHRONOUS },
3850 { MNTOPT_USER, MS_USERS },
3851 { MNTOPT_USERS, MS_USERS },
3853 { MNTOPT_NOATIME, MS_NOATIME },
3855 #ifdef MS_NODIRATIME
3856 { MNTOPT_NODIRATIME, MS_NODIRATIME },
3859 { MNTOPT_RELATIME, MS_RELATIME },
3861 #ifdef MS_STRICTATIME
3862 { MNTOPT_DFRATIME, MS_STRICTATIME },
3865 { MNTOPT_CONTEXT, MS_COMMENT },
3866 { MNTOPT_FSCONTEXT, MS_COMMENT },
3867 { MNTOPT_DEFCONTEXT, MS_COMMENT },
3868 { MNTOPT_ROOTCONTEXT, MS_COMMENT },
3871 { MNTOPT_IVERSION, MS_I_VERSION },
3874 { MNTOPT_NBMAND, MS_MANDLOCK },
3876 /* Valid options not found in mount(8) */
3877 { MNTOPT_BIND, MS_BIND },
3879 { MNTOPT_RBIND, MS_BIND|MS_REC },
3881 { MNTOPT_COMMENT, MS_COMMENT },
3882 { MNTOPT_BOOTWAIT, MS_COMMENT },
3883 { MNTOPT_NOBOOTWAIT, MS_COMMENT },
3884 { MNTOPT_OPTIONAL, MS_COMMENT },
3885 { MNTOPT_SHOWTHROUGH, MS_COMMENT },
3887 { MNTOPT_NOSUB, MS_NOSUB },
3890 { MNTOPT_QUIET, MS_SILENT },
3892 /* Custom zfs options */
3893 { MNTOPT_NOXATTR, MS_COMMENT },
3897 * Break the mount option in to a name/value pair. The name is
3898 * validated against the option map and mount flags set accordingly.
3901 parse_option(char *mntopt, unsigned long *mntflags, int sloppy)
3903 const option_map_t *opt;
3904 char *ptr, *name, *value = NULL;
3907 name = strdup(mntopt);
3911 for (ptr = name; ptr && *ptr; ptr++) {
3919 for (opt = option_map; opt->name != NULL; opt++) {
3920 if (strncmp(name, opt->name, strlen(name)) == 0) {
3921 *mntflags |= opt->mask;
3923 /* MS_USERS implies default user options */
3924 if (opt->mask & (MS_USERS))
3925 *mntflags |= (MS_NOEXEC|MS_NOSUID|MS_NODEV);
3927 /* MS_OWNER|MS_GROUP imply default owner options */
3928 if (opt->mask & (MS_OWNER | MS_GROUP))
3929 *mntflags |= (MS_NOSUID|MS_NODEV);
3939 /* If required further process on the value may be done here */
3945 * Translate the mount option string in to MS_* mount flags for the
3946 * kernel vfs. When sloppy is non-zero unknown options will be ignored
3947 * otherwise they are considered fatal are copied in to badopt.
3950 parse_options(char *mntopts, unsigned long *mntflags, int sloppy, char *badopt)
3952 int rc = 0, quote = 0;
3953 char *ptr, *opt, *opts;
3955 opts = strdup(mntopts);
3963 * Scan through all mount options which must be comma delimited.
3964 * We must be careful to notice regions which are double quoted
3965 * and skip commas in these regions. Each option is then checked
3966 * to determine if it is a known option.
3968 for (ptr = opts; ptr && *ptr; ptr++) {
3978 if ((*ptr == ',') || (*ptr == '\0')) {
3980 rc = parse_option(opt, mntflags, sloppy);
3982 strcpy(badopt, opt);
3995 * Called when invoked as /sbin/mount.zfs, mount helper for mount(8).
3998 manual_mount(int argc, char **argv)
4001 char legacy[ZFS_MAXPROPLEN];
4002 char mntopts[MNT_LINE_MAX] = { '\0' };
4003 char badopt[MNT_LINE_MAX] = { '\0' };
4004 char *dataset, *mntpoint;
4005 unsigned long mntflags;
4006 int sloppy = 0, fake = 0, verbose = 0;
4010 while ((c = getopt(argc, argv, "sfnvo:h?")) != -1) {
4019 /* Ignored, handled by mount(8) */
4025 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
4029 (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
4031 (void) fprintf(stderr, gettext("Usage: mount.zfs "
4032 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
4033 return (MOUNT_USAGE);
4040 /* check that we only have two arguments */
4043 (void) fprintf(stderr, gettext("missing dataset "
4046 (void) fprintf(stderr,
4047 gettext("missing mountpoint argument\n"));
4049 (void) fprintf(stderr, gettext("too many arguments\n"));
4050 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
4051 return (MOUNT_USAGE);
4057 /* try to open the dataset to access the mount point */
4058 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) {
4059 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
4060 "mounted, unable to open the dataset\n"), dataset);
4061 return (MOUNT_USAGE);
4064 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, legacy,
4065 sizeof (legacy), NULL, NULL, 0, B_FALSE);
4069 /* check for legacy mountpoint or util mount option */
4070 if ((!strcmp(legacy, ZFS_MOUNTPOINT_LEGACY) == 0) &&
4071 (strstr(mntopts, MNTOPT_ZFSUTIL) == NULL)) {
4072 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
4073 "mounted using 'mount -a -t zfs'\n"), dataset);
4074 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
4075 "instead.\n"), mntpoint);
4076 (void) fprintf(stderr, gettext("If you must use 'mount -a -t "
4077 "zfs' or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
4078 (void) fprintf(stderr, gettext("See zfs(8) for more "
4080 return (MOUNT_USAGE);
4083 /* validate mount options and set mntflags */
4084 rc = parse_options(mntopts, &mntflags, sloppy, badopt);
4088 (void) fprintf(stderr, gettext("filesystem '%s' "
4089 "cannot be mounted due to a memory allocation "
4090 "failure\n"), dataset);
4091 return (MOUNT_SYSERR);
4093 (void) fprintf(stderr, gettext("filesystem '%s' "
4094 "cannot be mounted of due to the invalid option "
4095 "'%s'\n"), dataset, badopt);
4096 (void) fprintf(stderr, gettext("Use the '-s' option "
4097 "to ignore the bad mount option.\n"));
4098 return (MOUNT_USAGE);
4100 (void) fprintf(stderr, gettext("filesystem '%s' "
4101 "cannot be mounted due to internal error %d\n"),
4103 return (MOUNT_SOFTWARE);
4108 printf("mount.zfs: dataset: \"%s\", mountpoint: \"%s\" "
4109 "mountflags: 0x%lx, mountopts: \"%s\"\n", dataset,
4110 mntpoint, mntflags, mntopts);
4112 /* load the zfs posix layer module (zpl) */
4113 if (libzfs_load_module("zpl")) {
4114 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
4115 "mounted without the zpl kernel module\n"), dataset);
4116 (void) fprintf(stderr, gettext("Use 'dmesg' to determine why "
4117 "the module could not be loaded.\n"));
4118 return (MOUNT_SYSERR);
4122 rc = mount(dataset, mntpoint, MNTTYPE_ZFS, mntflags, mntopts);
4124 (void) fprintf(stderr, gettext("filesystem '%s' can"
4125 "not be mounted due to error %d\n"), dataset, rc);
4126 return (MOUNT_USAGE);
4130 return (MOUNT_SUCCESS);
4133 #ifdef HAVE_UNMOUNT_HELPER
4135 * Called when invoked as /sbin/umount.zfs, mount helper for mount(8).
4136 * Unlike a manual mount, we allow unmounts of non-legacy filesystems,
4137 * as this is the dominant administrative interface.
4140 manual_unmount(int argc, char **argv)
4142 int verbose = 0, flags = 0;
4146 while ((c = getopt(argc, argv, "nlfvrh?")) != -1) {
4149 /* Ignored, handled by mount(8) */
4161 /* Remount read-only on umount failure, unsupported */
4162 (void) fprintf(stderr, gettext("Unsupported option "
4164 return (MOUNT_USAGE);
4167 (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
4169 (void) fprintf(stderr, gettext("Usage: umount.zfs "
4170 "[-nlfvr] <mountpoint>\n"));
4171 return (MOUNT_USAGE);
4178 /* check that we only have one argument */
4181 (void) fprintf(stderr, gettext("missing mountpoint "
4184 (void) fprintf(stderr, gettext("too many arguments\n"));
4186 (void) fprintf(stderr, gettext("Usage: umount.zfs [-nlfvr] "
4188 return (MOUNT_USAGE);
4191 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
4193 #endif /* HAVE_UNMOUNT_HELPER */
4196 find_command_idx(char *command, int *idx)
4200 for (i = 0; i < NCOMMAND; i++) {
4201 if (command_table[i].name == NULL)
4204 if (strcmp(command, command_table[i].name) == 0) {
4213 zfs_do_diff(int argc, char **argv)
4217 char *tosnap = NULL;
4218 char *fromsnap = NULL;
4223 while ((c = getopt(argc, argv, "FHt")) != -1) {
4226 flags |= ZFS_DIFF_CLASSIFY;
4229 flags |= ZFS_DIFF_PARSEABLE;
4232 flags |= ZFS_DIFF_TIMESTAMP;
4235 (void) fprintf(stderr,
4236 gettext("invalid option '%c'\n"), optopt);
4245 (void) fprintf(stderr,
4246 gettext("must provide at least one snapshot name\n"));
4251 (void) fprintf(stderr, gettext("too many arguments\n"));
4256 tosnap = (argc == 2) ? argv[1] : NULL;
4259 if (*fromsnap != '@')
4260 copy = strdup(fromsnap);
4262 copy = strdup(tosnap);
4266 if ((atp = strchr(copy, '@')))
4269 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
4275 * Ignore SIGPIPE so that the library can give us
4276 * information on any failure
4278 (void) sigignore(SIGPIPE);
4280 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
4288 main(int argc, char **argv)
4295 (void) setlocale(LC_ALL, "");
4296 (void) textdomain(TEXT_DOMAIN);
4300 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4301 (void) fprintf(stderr, gettext("internal error: unable to "
4302 "open %s\n"), MNTTAB);
4307 * This command also doubles as the /etc/fs mount and unmount program.
4308 * Determine if we should take this behavior based on argv[0].
4310 progname = basename(argv[0]);
4311 if (strcmp(progname, "mount.zfs") == 0) {
4312 ret = manual_mount(argc, argv);
4313 #ifdef HAVE_UNMOUNT_HELPER
4314 } else if (strcmp(progname, "umount.zfs") == 0) {
4315 ret = manual_unmount(argc, argv);
4316 #endif /* HAVE_UNMOUNT_HELPER */
4319 * Make sure the user has specified some command.
4322 (void) fprintf(stderr, gettext("missing command\n"));
4329 * The 'umount' command is an alias for 'unmount'
4331 if (strcmp(cmdname, "umount") == 0)
4332 cmdname = "unmount";
4335 * The 'recv' command is an alias for 'receive'
4337 if (strcmp(cmdname, "recv") == 0)
4338 cmdname = "receive";
4343 if ((strcmp(cmdname, "-?") == 0) ||
4344 (strcmp(cmdname, "--help") == 0))
4347 if ((g_zfs = libzfs_init()) == NULL)
4350 zpool_set_history_str("zfs", argc, argv, history_str);
4351 verify(zpool_stage_history(g_zfs, history_str) == 0);
4353 libzfs_print_on_error(g_zfs, B_TRUE);
4356 * Run the appropriate command.
4358 libzfs_mnttab_cache(g_zfs, B_TRUE);
4359 if (find_command_idx(cmdname, &i) == 0) {
4360 current_command = &command_table[i];
4361 ret = command_table[i].func(argc - 1, argv + 1);
4362 } else if (strchr(cmdname, '=') != NULL) {
4363 verify(find_command_idx("set", &i) == 0);
4364 current_command = &command_table[i];
4365 ret = command_table[i].func(argc, argv);
4367 (void) fprintf(stderr, gettext("unrecognized "
4368 "command '%s'\n"), cmdname);
4372 libzfs_mnttab_cache(g_zfs, B_FALSE);
4375 (void) fclose(mnttab_file);
4380 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4381 * for the purposes of running ::findleaks.
4383 if (getenv("ZFS_ABORT") != NULL) {
4384 (void) printf("dumping core by request\n");