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.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
37 #include <libnvpair.h>
50 #include <sys/mkdev.h>
51 #include <sys/mntent.h>
52 #include <sys/mnttab.h>
53 #include <sys/mount.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/types.h>
60 #include <libzfs_core.h>
62 #include <zfs_deleg.h>
65 #include <directory.h>
69 #include "zfs_comutil.h"
71 libzfs_handle_t *g_zfs;
73 static FILE *mnttab_file;
74 static char history_str[HIS_MAX_RECORD_LEN];
75 static boolean_t log_history = B_TRUE;
77 static int zfs_do_clone(int argc, char **argv);
78 static int zfs_do_create(int argc, char **argv);
79 static int zfs_do_destroy(int argc, char **argv);
80 static int zfs_do_get(int argc, char **argv);
81 static int zfs_do_inherit(int argc, char **argv);
82 static int zfs_do_list(int argc, char **argv);
83 static int zfs_do_mount(int argc, char **argv);
84 static int zfs_do_rename(int argc, char **argv);
85 static int zfs_do_rollback(int argc, char **argv);
86 static int zfs_do_set(int argc, char **argv);
87 static int zfs_do_upgrade(int argc, char **argv);
88 static int zfs_do_snapshot(int argc, char **argv);
89 static int zfs_do_unmount(int argc, char **argv);
90 static int zfs_do_share(int argc, char **argv);
91 static int zfs_do_unshare(int argc, char **argv);
92 static int zfs_do_send(int argc, char **argv);
93 static int zfs_do_receive(int argc, char **argv);
94 static int zfs_do_promote(int argc, char **argv);
95 static int zfs_do_userspace(int argc, char **argv);
96 static int zfs_do_allow(int argc, char **argv);
97 static int zfs_do_unallow(int argc, char **argv);
98 static int zfs_do_hold(int argc, char **argv);
99 static int zfs_do_holds(int argc, char **argv);
100 static int zfs_do_release(int argc, char **argv);
101 static int zfs_do_diff(int argc, char **argv);
102 static int zfs_do_bookmark(int argc, char **argv);
105 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
110 _umem_debug_init(void)
112 return ("default,verbose"); /* $UMEM_DEBUG setting */
116 _umem_logging_init(void)
118 return ("fail,contents"); /* $UMEM_LOGGING setting */
152 typedef struct zfs_command {
154 int (*func)(int argc, char **argv);
159 * Master command table. Each ZFS command has a name, associated function, and
160 * usage message. The usage messages need to be internationalized, so we have
161 * to have a function to return the usage message based on a command index.
163 * These commands are organized according to how they are displayed in the usage
164 * message. An empty command (one with a NULL name) indicates an empty line in
165 * the generic usage message.
167 static zfs_command_t command_table[] = {
168 { "create", zfs_do_create, HELP_CREATE },
169 { "destroy", zfs_do_destroy, HELP_DESTROY },
171 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
172 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
173 { "clone", zfs_do_clone, HELP_CLONE },
174 { "promote", zfs_do_promote, HELP_PROMOTE },
175 { "rename", zfs_do_rename, HELP_RENAME },
176 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
178 { "list", zfs_do_list, HELP_LIST },
180 { "set", zfs_do_set, HELP_SET },
181 { "get", zfs_do_get, HELP_GET },
182 { "inherit", zfs_do_inherit, HELP_INHERIT },
183 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
184 { "userspace", zfs_do_userspace, HELP_USERSPACE },
185 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
187 { "mount", zfs_do_mount, HELP_MOUNT },
188 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
189 { "share", zfs_do_share, HELP_SHARE },
190 { "unshare", zfs_do_unshare, HELP_UNSHARE },
192 { "send", zfs_do_send, HELP_SEND },
193 { "receive", zfs_do_receive, HELP_RECEIVE },
195 { "allow", zfs_do_allow, HELP_ALLOW },
197 { "unallow", zfs_do_unallow, HELP_UNALLOW },
199 { "hold", zfs_do_hold, HELP_HOLD },
200 { "holds", zfs_do_holds, HELP_HOLDS },
201 { "release", zfs_do_release, HELP_RELEASE },
202 { "diff", zfs_do_diff, HELP_DIFF },
205 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
207 zfs_command_t *current_command;
210 get_usage(zfs_help_t idx)
214 return (gettext("\tclone [-p] [-o property=value] ... "
215 "<snapshot> <filesystem|volume>\n"));
217 return (gettext("\tcreate [-p] [-o property=value] ... "
219 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
220 "-V <size> <volume>\n"));
222 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
223 "\tdestroy [-dnpRrv] "
224 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
225 "\tdestroy <filesystem|volume>#<bookmark>\n"));
227 return (gettext("\tget [-rHp] [-d max] "
228 "[-o \"all\" | field[,...]]\n"
229 "\t [-t type[,...]] [-s source[,...]]\n"
230 "\t <\"all\" | property[,...]> "
231 "[filesystem|volume|snapshot] ...\n"));
233 return (gettext("\tinherit [-rS] <property> "
234 "<filesystem|volume|snapshot> ...\n"));
236 return (gettext("\tupgrade [-v]\n"
237 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
239 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
240 "[-s property]...\n\t [-S property]... [-t type[,...]] "
241 "[filesystem|volume|snapshot] ...\n"));
243 return (gettext("\tmount\n"
244 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
246 return (gettext("\tpromote <clone-filesystem>\n"));
248 return (gettext("\treceive [-vnFu] <filesystem|volume|"
250 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
252 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
253 "<filesystem|volume|snapshot>\n"
254 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
255 "\trename -r <snapshot> <snapshot>\n"));
257 return (gettext("\trollback [-rRf] <snapshot>\n"));
259 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
261 "\tsend [-Le] [-i snapshot|bookmark] "
262 "<filesystem|volume|snapshot>\n"));
264 return (gettext("\tset <property=value> "
265 "<filesystem|volume|snapshot> ...\n"));
267 return (gettext("\tshare <-a | filesystem>\n"));
269 return (gettext("\tsnapshot [-r] [-o property=value] ... "
270 "<filesystem|volume>@<snap> ...\n"));
272 return (gettext("\tunmount [-f] "
273 "<-a | filesystem|mountpoint>\n"));
275 return (gettext("\tunshare "
276 "<-a | filesystem|mountpoint>\n"));
278 return (gettext("\tallow <filesystem|volume>\n"
280 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
281 "\t <filesystem|volume>\n"
282 "\tallow [-ld] -e <perm|@setname>[,...] "
283 "<filesystem|volume>\n"
284 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
285 "\tallow -s @setname <perm|@setname>[,...] "
286 "<filesystem|volume>\n"));
288 return (gettext("\tunallow [-rldug] "
289 "<\"everyone\"|user|group>[,...]\n"
290 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
291 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
292 "<filesystem|volume>\n"
293 "\tunallow [-r] -c [<perm|@setname>[,...]] "
294 "<filesystem|volume>\n"
295 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
296 "<filesystem|volume>\n"));
298 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
300 "\t [-S field] ... [-t type[,...]] "
301 "<filesystem|snapshot>\n"));
302 case HELP_GROUPSPACE:
303 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
305 "\t [-S field] ... [-t type[,...]] "
306 "<filesystem|snapshot>\n"));
308 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
310 return (gettext("\tholds [-r] <snapshot> ...\n"));
312 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
314 return (gettext("\tdiff [-FHt] <snapshot> "
315 "[snapshot|filesystem]\n"));
317 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
327 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
332 * Utility function to guarantee malloc() success.
336 safe_malloc(size_t size)
340 if ((data = calloc(1, size)) == NULL)
347 safe_strdup(char *str)
349 char *dupstr = strdup(str);
358 * Callback routine that will print out information for each of
362 usage_prop_cb(int prop, void *cb)
366 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
368 if (zfs_prop_readonly(prop))
369 (void) fprintf(fp, " NO ");
371 (void) fprintf(fp, "YES ");
373 if (zfs_prop_inheritable(prop))
374 (void) fprintf(fp, " YES ");
376 (void) fprintf(fp, " NO ");
378 if (zfs_prop_values(prop) == NULL)
379 (void) fprintf(fp, "-\n");
381 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
387 * Display usage message. If we're inside a command, display only the usage for
388 * that command. Otherwise, iterate over the entire command table and display
389 * a complete usage message.
392 usage(boolean_t requested)
395 boolean_t show_properties = B_FALSE;
396 FILE *fp = requested ? stdout : stderr;
398 if (current_command == NULL) {
400 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
402 gettext("where 'command' is one of the following:\n\n"));
404 for (i = 0; i < NCOMMAND; i++) {
405 if (command_table[i].name == NULL)
406 (void) fprintf(fp, "\n");
408 (void) fprintf(fp, "%s",
409 get_usage(command_table[i].usage));
412 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
413 "pool/[dataset/]*dataset[@name]\n"));
415 (void) fprintf(fp, gettext("usage:\n"));
416 (void) fprintf(fp, "%s", get_usage(current_command->usage));
419 if (current_command != NULL &&
420 (strcmp(current_command->name, "set") == 0 ||
421 strcmp(current_command->name, "get") == 0 ||
422 strcmp(current_command->name, "inherit") == 0 ||
423 strcmp(current_command->name, "list") == 0))
424 show_properties = B_TRUE;
426 if (show_properties) {
428 gettext("\nThe following properties are supported:\n"));
430 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
431 "PROPERTY", "EDIT", "INHERIT", "VALUES");
433 /* Iterate over all properties */
434 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
437 (void) fprintf(fp, "\t%-15s ", "userused@...");
438 (void) fprintf(fp, " NO NO <size>\n");
439 (void) fprintf(fp, "\t%-15s ", "groupused@...");
440 (void) fprintf(fp, " NO NO <size>\n");
441 (void) fprintf(fp, "\t%-15s ", "userquota@...");
442 (void) fprintf(fp, "YES NO <size> | none\n");
443 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
444 (void) fprintf(fp, "YES NO <size> | none\n");
445 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
446 (void) fprintf(fp, " NO NO <size>\n");
448 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
449 "with standard units such as K, M, G, etc.\n"));
450 (void) fprintf(fp, gettext("\nUser-defined properties can "
451 "be specified by using a name containing a colon (:).\n"));
452 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
453 "properties must be appended with\n"
454 "a user or group specifier of one of these forms:\n"
455 " POSIX name (eg: \"matt\")\n"
456 " POSIX id (eg: \"126829\")\n"
457 " SMB name@domain (eg: \"matt@sun\")\n"
458 " SMB SID (eg: \"S-1-234-567-89\")\n"));
461 gettext("\nFor the property list, run: %s\n"),
464 gettext("\nFor the delegated permission list, run: %s\n"),
465 "zfs allow|unallow");
469 * See comments at end of main().
471 if (getenv("ZFS_ABORT") != NULL) {
472 (void) printf("dumping core by request\n");
476 exit(requested ? 0 : 2);
480 parseprop(nvlist_t *props)
482 char *propname = optarg;
483 char *propval, *strval;
485 if ((propval = strchr(propname, '=')) == NULL) {
486 (void) fprintf(stderr, gettext("missing "
487 "'=' for -o option\n"));
492 if (nvlist_lookup_string(props, propname, &strval) == 0) {
493 (void) fprintf(stderr, gettext("property '%s' "
494 "specified multiple times\n"), propname);
497 if (nvlist_add_string(props, propname, propval) != 0)
503 parse_depth(char *opt, int *flags)
508 depth = (int)strtol(opt, &tmp, 0);
510 (void) fprintf(stderr,
511 gettext("%s is not an integer\n"), optarg);
515 (void) fprintf(stderr,
516 gettext("Depth can not be negative.\n"));
519 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
523 #define PROGRESS_DELAY 2 /* seconds */
525 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";
526 static time_t pt_begin;
527 static char *pt_header = NULL;
528 static boolean_t pt_shown;
531 start_progress_timer(void)
533 pt_begin = time(NULL) + PROGRESS_DELAY;
538 set_progress_header(char *header)
540 assert(pt_header == NULL);
541 pt_header = safe_strdup(header);
543 (void) printf("%s: ", header);
544 (void) fflush(stdout);
549 update_progress(char *update)
551 if (!pt_shown && time(NULL) > pt_begin) {
552 int len = strlen(update);
554 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
556 (void) fflush(stdout);
558 } else if (pt_shown) {
559 int len = strlen(update);
561 (void) printf("%s%*.*s", update, len, len, pt_reverse);
562 (void) fflush(stdout);
567 finish_progress(char *done)
570 (void) printf("%s\n", done);
571 (void) fflush(stdout);
578 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
580 * Given an existing dataset, create a writable copy whose initial contents
581 * are the same as the source. The newly created dataset maintains a
582 * dependency on the original; the original cannot be destroyed so long as
585 * The '-p' flag creates all the non-existing ancestors of the target first.
588 zfs_do_clone(int argc, char **argv)
590 zfs_handle_t *zhp = NULL;
591 boolean_t parents = B_FALSE;
596 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
600 while ((c = getopt(argc, argv, "o:p")) != -1) {
603 if (parseprop(props))
610 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
619 /* check number of arguments */
621 (void) fprintf(stderr, gettext("missing source dataset "
626 (void) fprintf(stderr, gettext("missing target dataset "
631 (void) fprintf(stderr, gettext("too many arguments\n"));
635 /* open the source dataset */
636 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
639 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
642 * Now create the ancestors of the target dataset. If the
643 * target already exists and '-p' option was used we should not
646 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
649 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
654 ret = zfs_clone(zhp, argv[1], props);
656 /* create the mountpoint if necessary */
660 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
662 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
663 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
664 ret = zfs_share(clone);
683 * zfs create [-p] [-o prop=value] ... fs
684 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
686 * Create a new dataset. This command can be used to create filesystems
687 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
688 * For volumes, the user must specify a size to be used.
690 * The '-s' flag applies only to volumes, and indicates that we should not try
691 * to set the reservation for this volume. By default we set a reservation
692 * equal to the size for any volume. For pools with SPA_VERSION >=
693 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
695 * The '-p' flag creates all the non-existing ancestors of the target first.
698 zfs_do_create(int argc, char **argv)
700 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
701 zfs_handle_t *zhp = NULL;
704 boolean_t noreserve = B_FALSE;
705 boolean_t bflag = B_FALSE;
706 boolean_t parents = B_FALSE;
710 int canmount = ZFS_CANMOUNT_OFF;
712 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
716 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
719 type = ZFS_TYPE_VOLUME;
720 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
721 (void) fprintf(stderr, gettext("bad volume "
722 "size '%s': %s\n"), optarg,
723 libzfs_error_description(g_zfs));
727 if (nvlist_add_uint64(props,
728 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
737 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
738 (void) fprintf(stderr, gettext("bad volume "
739 "block size '%s': %s\n"), optarg,
740 libzfs_error_description(g_zfs));
744 if (nvlist_add_uint64(props,
745 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
750 if (parseprop(props))
757 (void) fprintf(stderr, gettext("missing size "
761 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
767 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
768 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
769 "used when creating a volume\n"));
776 /* check number of arguments */
778 (void) fprintf(stderr, gettext("missing %s argument\n"),
779 zfs_type_to_name(type));
783 (void) fprintf(stderr, gettext("too many arguments\n"));
787 if (type == ZFS_TYPE_VOLUME && !noreserve) {
788 zpool_handle_t *zpool_handle;
789 nvlist_t *real_props;
790 uint64_t spa_version;
792 zfs_prop_t resv_prop;
796 if (p = strchr(argv[0], '/'))
798 zpool_handle = zpool_open(g_zfs, argv[0]);
801 if (zpool_handle == NULL)
803 spa_version = zpool_get_prop_int(zpool_handle,
804 ZPOOL_PROP_VERSION, NULL);
805 zpool_close(zpool_handle);
806 if (spa_version >= SPA_VERSION_REFRESERVATION)
807 resv_prop = ZFS_PROP_REFRESERVATION;
809 resv_prop = ZFS_PROP_RESERVATION;
811 (void) snprintf(msg, sizeof (msg),
812 gettext("cannot create '%s'"), argv[0]);
813 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
814 props, 0, NULL, msg)) == NULL)
817 volsize = zvol_volsize_to_reservation(volsize, real_props);
818 nvlist_free(real_props);
820 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
822 if (nvlist_add_uint64(props,
823 zfs_prop_to_name(resv_prop), volsize) != 0) {
830 if (parents && zfs_name_valid(argv[0], type)) {
832 * Now create the ancestors of target dataset. If the target
833 * already exists and '-p' option was used we should not
836 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
840 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
845 if (zfs_create(g_zfs, argv[0], type, props) != 0)
848 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
853 * if the user doesn't want the dataset automatically mounted,
854 * then skip the mount/share step
856 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
857 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
860 * Mount and/or share the new filesystem as appropriate. We provide a
861 * verbose error message to let the user know that their filesystem was
862 * in fact created, even if we failed to mount or share it.
864 if (canmount == ZFS_CANMOUNT_ON) {
865 if (zfs_mount(zhp, NULL, 0) != 0) {
866 (void) fprintf(stderr, gettext("filesystem "
867 "successfully created, but not mounted\n"));
869 } else if (zfs_share(zhp) != 0) {
870 (void) fprintf(stderr, gettext("filesystem "
871 "successfully created, but not shared\n"));
888 * zfs destroy [-rRf] <fs, vol>
889 * zfs destroy [-rRd] <snap>
891 * -r Recursively destroy all children
892 * -R Recursively destroy all dependents, including clones
893 * -f Force unmounting of any dependents
894 * -d If we can't destroy now, mark for deferred destruction
896 * Destroys the given dataset. By default, it will unmount any filesystems,
897 * and refuse to destroy a dataset that has any dependents. A dependent can
898 * either be a child, or a clone of a child.
900 typedef struct destroy_cbdata {
903 boolean_t cb_recurse;
905 boolean_t cb_doclones;
906 zfs_handle_t *cb_target;
907 boolean_t cb_defer_destroy;
908 boolean_t cb_verbose;
909 boolean_t cb_parsable;
912 nvlist_t *cb_batchedsnaps;
914 /* first snap in contiguous run */
916 /* previous snap in contiguous run */
924 * Check for any dependents based on the '-r' or '-R' flags.
927 destroy_check_dependent(zfs_handle_t *zhp, void *data)
929 destroy_cbdata_t *cbp = data;
930 const char *tname = zfs_get_name(cbp->cb_target);
931 const char *name = zfs_get_name(zhp);
933 if (strncmp(tname, name, strlen(tname)) == 0 &&
934 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
936 * This is a direct descendant, not a clone somewhere else in
943 (void) fprintf(stderr, gettext("cannot destroy '%s': "
944 "%s has children\n"),
945 zfs_get_name(cbp->cb_target),
946 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
947 (void) fprintf(stderr, gettext("use '-r' to destroy "
948 "the following datasets:\n"));
949 cbp->cb_first = B_FALSE;
950 cbp->cb_error = B_TRUE;
953 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
956 * This is a clone. We only want to report this if the '-r'
957 * wasn't specified, or the target is a snapshot.
959 if (!cbp->cb_recurse &&
960 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
964 (void) fprintf(stderr, gettext("cannot destroy '%s': "
965 "%s has dependent clones\n"),
966 zfs_get_name(cbp->cb_target),
967 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
968 (void) fprintf(stderr, gettext("use '-R' to destroy "
969 "the following datasets:\n"));
970 cbp->cb_first = B_FALSE;
971 cbp->cb_error = B_TRUE;
972 cbp->cb_dryrun = B_TRUE;
975 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
984 destroy_callback(zfs_handle_t *zhp, void *data)
986 destroy_cbdata_t *cb = data;
987 const char *name = zfs_get_name(zhp);
989 if (cb->cb_verbose) {
990 if (cb->cb_parsable) {
991 (void) printf("destroy\t%s\n", name);
992 } else if (cb->cb_dryrun) {
993 (void) printf(gettext("would destroy %s\n"),
996 (void) printf(gettext("will destroy %s\n"),
1002 * Ignore pools (which we've already flagged as an error before getting
1005 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1006 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1010 if (cb->cb_dryrun) {
1016 * We batch up all contiguous snapshots (even of different
1017 * filesystems) and destroy them with one ioctl. We can't
1018 * simply do all snap deletions and then all fs deletions,
1019 * because we must delete a clone before its origin.
1021 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1022 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1024 int error = zfs_destroy_snaps_nvl(g_zfs,
1025 cb->cb_batchedsnaps, B_FALSE);
1026 fnvlist_free(cb->cb_batchedsnaps);
1027 cb->cb_batchedsnaps = fnvlist_alloc();
1030 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1031 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1042 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1044 destroy_cbdata_t *cb = arg;
1045 const char *name = zfs_get_name(zhp);
1048 if (nvlist_exists(cb->cb_nvl, name)) {
1049 if (cb->cb_firstsnap == NULL)
1050 cb->cb_firstsnap = strdup(name);
1051 if (cb->cb_prevsnap != NULL)
1052 free(cb->cb_prevsnap);
1053 /* this snap continues the current range */
1054 cb->cb_prevsnap = strdup(name);
1055 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1057 if (cb->cb_verbose) {
1058 if (cb->cb_parsable) {
1059 (void) printf("destroy\t%s\n", name);
1060 } else if (cb->cb_dryrun) {
1061 (void) printf(gettext("would destroy %s\n"),
1064 (void) printf(gettext("will destroy %s\n"),
1068 } else if (cb->cb_firstsnap != NULL) {
1069 /* end of this range */
1071 err = lzc_snaprange_space(cb->cb_firstsnap,
1072 cb->cb_prevsnap, &used);
1073 cb->cb_snapused += used;
1074 free(cb->cb_firstsnap);
1075 cb->cb_firstsnap = NULL;
1076 free(cb->cb_prevsnap);
1077 cb->cb_prevsnap = NULL;
1084 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1087 assert(cb->cb_firstsnap == NULL);
1088 assert(cb->cb_prevsnap == NULL);
1089 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1090 if (cb->cb_firstsnap != NULL) {
1093 err = lzc_snaprange_space(cb->cb_firstsnap,
1094 cb->cb_prevsnap, &used);
1096 cb->cb_snapused += used;
1097 free(cb->cb_firstsnap);
1098 cb->cb_firstsnap = NULL;
1099 free(cb->cb_prevsnap);
1100 cb->cb_prevsnap = NULL;
1106 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1108 destroy_cbdata_t *cb = arg;
1111 /* Check for clones. */
1112 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1113 cb->cb_target = zhp;
1114 cb->cb_first = B_TRUE;
1115 err = zfs_iter_dependents(zhp, B_TRUE,
1116 destroy_check_dependent, cb);
1120 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1128 gather_snapshots(zfs_handle_t *zhp, void *arg)
1130 destroy_cbdata_t *cb = arg;
1133 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1139 if (cb->cb_verbose) {
1140 err = destroy_print_snapshots(zhp, cb);
1146 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1154 destroy_clones(destroy_cbdata_t *cb)
1157 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1159 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1160 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1163 boolean_t defer = cb->cb_defer_destroy;
1167 * We can't defer destroy non-snapshots, so set it to
1168 * false while destroying the clones.
1170 cb->cb_defer_destroy = B_FALSE;
1171 err = zfs_iter_dependents(zhp, B_FALSE,
1172 destroy_callback, cb);
1173 cb->cb_defer_destroy = defer;
1183 zfs_do_destroy(int argc, char **argv)
1185 destroy_cbdata_t cb = { 0 };
1189 zfs_handle_t *zhp = NULL;
1191 zfs_type_t type = ZFS_TYPE_DATASET;
1194 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1197 cb.cb_verbose = B_TRUE;
1200 cb.cb_verbose = B_TRUE;
1201 cb.cb_parsable = B_TRUE;
1204 cb.cb_dryrun = B_TRUE;
1207 cb.cb_defer_destroy = B_TRUE;
1208 type = ZFS_TYPE_SNAPSHOT;
1211 cb.cb_force = B_TRUE;
1214 cb.cb_recurse = B_TRUE;
1217 cb.cb_recurse = B_TRUE;
1218 cb.cb_doclones = B_TRUE;
1222 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1231 /* check number of arguments */
1233 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1237 (void) fprintf(stderr, gettext("too many arguments\n"));
1241 at = strchr(argv[0], '@');
1242 pound = strchr(argv[0], '#');
1245 /* Build the list of snaps to destroy in cb_nvl. */
1246 cb.cb_nvl = fnvlist_alloc();
1249 zhp = zfs_open(g_zfs, argv[0],
1250 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1254 cb.cb_snapspec = at + 1;
1255 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1261 if (nvlist_empty(cb.cb_nvl)) {
1262 (void) fprintf(stderr, gettext("could not find any "
1263 "snapshots to destroy; check snapshot names.\n"));
1268 if (cb.cb_verbose) {
1270 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1271 if (cb.cb_parsable) {
1272 (void) printf("reclaim\t%llu\n",
1274 } else if (cb.cb_dryrun) {
1275 (void) printf(gettext("would reclaim %s\n"),
1278 (void) printf(gettext("will reclaim %s\n"),
1283 if (!cb.cb_dryrun) {
1284 if (cb.cb_doclones) {
1285 cb.cb_batchedsnaps = fnvlist_alloc();
1286 err = destroy_clones(&cb);
1288 err = zfs_destroy_snaps_nvl(g_zfs,
1289 cb.cb_batchedsnaps, B_FALSE);
1297 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1298 cb.cb_defer_destroy);
1304 } else if (pound != NULL) {
1309 (void) fprintf(stderr,
1310 "dryrun is not supported with bookmark\n");
1314 if (cb.cb_defer_destroy) {
1315 (void) fprintf(stderr,
1316 "defer destroy is not supported with bookmark\n");
1320 if (cb.cb_recurse) {
1321 (void) fprintf(stderr,
1322 "recursive is not supported with bookmark\n");
1326 if (!zfs_bookmark_exists(argv[0])) {
1327 (void) fprintf(stderr, gettext("bookmark '%s' "
1328 "does not exist.\n"), argv[0]);
1332 nvl = fnvlist_alloc();
1333 fnvlist_add_boolean(nvl, argv[0]);
1335 err = lzc_destroy_bookmarks(nvl, NULL);
1337 (void) zfs_standard_error(g_zfs, err,
1338 "cannot destroy bookmark");
1341 nvlist_free(cb.cb_nvl);
1345 /* Open the given dataset */
1346 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1352 * Perform an explicit check for pools before going any further.
1354 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1355 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1356 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1357 "operation does not apply to pools\n"),
1359 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1360 "%s' to destroy all datasets in the pool\n"),
1362 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1363 "to destroy the pool itself\n"), zfs_get_name(zhp));
1369 * Check for any dependents and/or clones.
1371 cb.cb_first = B_TRUE;
1372 if (!cb.cb_doclones &&
1373 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1384 cb.cb_batchedsnaps = fnvlist_alloc();
1385 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1392 * Do the real thing. The callback will close the
1393 * handle regardless of whether it succeeds or not.
1395 err = destroy_callback(zhp, &cb);
1398 err = zfs_destroy_snaps_nvl(g_zfs,
1399 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1406 fnvlist_free(cb.cb_batchedsnaps);
1407 fnvlist_free(cb.cb_nvl);
1414 is_recvd_column(zprop_get_cbdata_t *cbp)
1417 zfs_get_column_t col;
1419 for (i = 0; i < ZFS_GET_NCOLS &&
1420 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1421 if (col == GET_COL_RECVD)
1427 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1428 * < all | property[,property]... > < fs | snap | vol > ...
1430 * -r recurse over any child datasets
1431 * -H scripted mode. Headers are stripped, and fields are separated
1432 * by tabs instead of spaces.
1433 * -o Set of fields to display. One of "name,property,value,
1434 * received,source". Default is "name,property,value,source".
1435 * "all" is an alias for all five.
1436 * -s Set of sources to allow. One of
1437 * "local,default,inherited,received,temporary,none". Default is
1439 * -p Display values in parsable (literal) format.
1441 * Prints properties for the given datasets. The user can control which
1442 * columns to display as well as which property types to allow.
1446 * Invoked to display the properties for a single dataset.
1449 get_callback(zfs_handle_t *zhp, void *data)
1451 char buf[ZFS_MAXPROPLEN];
1452 char rbuf[ZFS_MAXPROPLEN];
1453 zprop_source_t sourcetype;
1454 char source[ZFS_MAXNAMELEN];
1455 zprop_get_cbdata_t *cbp = data;
1456 nvlist_t *user_props = zfs_get_user_props(zhp);
1457 zprop_list_t *pl = cbp->cb_proplist;
1461 boolean_t received = is_recvd_column(cbp);
1463 for (; pl != NULL; pl = pl->pl_next) {
1464 char *recvdval = NULL;
1466 * Skip the special fake placeholder. This will also skip over
1467 * the name property when 'all' is specified.
1469 if (pl->pl_prop == ZFS_PROP_NAME &&
1470 pl == cbp->cb_proplist)
1473 if (pl->pl_prop != ZPROP_INVAL) {
1474 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1475 sizeof (buf), &sourcetype, source,
1477 cbp->cb_literal) != 0) {
1480 if (!zfs_prop_valid_for_type(pl->pl_prop,
1481 ZFS_TYPE_DATASET)) {
1482 (void) fprintf(stderr,
1483 gettext("No such property '%s'\n"),
1484 zfs_prop_to_name(pl->pl_prop));
1487 sourcetype = ZPROP_SRC_NONE;
1488 (void) strlcpy(buf, "-", sizeof (buf));
1491 if (received && (zfs_prop_get_recvd(zhp,
1492 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1493 cbp->cb_literal) == 0))
1496 zprop_print_one_property(zfs_get_name(zhp), cbp,
1497 zfs_prop_to_name(pl->pl_prop),
1498 buf, sourcetype, source, recvdval);
1499 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1500 sourcetype = ZPROP_SRC_LOCAL;
1502 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1503 buf, sizeof (buf), cbp->cb_literal) != 0) {
1504 sourcetype = ZPROP_SRC_NONE;
1505 (void) strlcpy(buf, "-", sizeof (buf));
1508 zprop_print_one_property(zfs_get_name(zhp), cbp,
1509 pl->pl_user_prop, buf, sourcetype, source, NULL);
1510 } else if (zfs_prop_written(pl->pl_user_prop)) {
1511 sourcetype = ZPROP_SRC_LOCAL;
1513 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1514 buf, sizeof (buf), cbp->cb_literal) != 0) {
1515 sourcetype = ZPROP_SRC_NONE;
1516 (void) strlcpy(buf, "-", sizeof (buf));
1519 zprop_print_one_property(zfs_get_name(zhp), cbp,
1520 pl->pl_user_prop, buf, sourcetype, source, NULL);
1522 if (nvlist_lookup_nvlist(user_props,
1523 pl->pl_user_prop, &propval) != 0) {
1526 sourcetype = ZPROP_SRC_NONE;
1529 verify(nvlist_lookup_string(propval,
1530 ZPROP_VALUE, &strval) == 0);
1531 verify(nvlist_lookup_string(propval,
1532 ZPROP_SOURCE, &sourceval) == 0);
1534 if (strcmp(sourceval,
1535 zfs_get_name(zhp)) == 0) {
1536 sourcetype = ZPROP_SRC_LOCAL;
1537 } else if (strcmp(sourceval,
1538 ZPROP_SOURCE_VAL_RECVD) == 0) {
1539 sourcetype = ZPROP_SRC_RECEIVED;
1541 sourcetype = ZPROP_SRC_INHERITED;
1542 (void) strlcpy(source,
1543 sourceval, sizeof (source));
1547 if (received && (zfs_prop_get_recvd(zhp,
1548 pl->pl_user_prop, rbuf, sizeof (rbuf),
1549 cbp->cb_literal) == 0))
1552 zprop_print_one_property(zfs_get_name(zhp), cbp,
1553 pl->pl_user_prop, strval, sourcetype,
1562 zfs_do_get(int argc, char **argv)
1564 zprop_get_cbdata_t cb = { 0 };
1565 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1566 int types = ZFS_TYPE_DATASET;
1567 char *value, *fields;
1570 zprop_list_t fake_name = { 0 };
1573 * Set up default columns and sources.
1575 cb.cb_sources = ZPROP_SRC_ALL;
1576 cb.cb_columns[0] = GET_COL_NAME;
1577 cb.cb_columns[1] = GET_COL_PROPERTY;
1578 cb.cb_columns[2] = GET_COL_VALUE;
1579 cb.cb_columns[3] = GET_COL_SOURCE;
1580 cb.cb_type = ZFS_TYPE_DATASET;
1583 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1586 cb.cb_literal = B_TRUE;
1589 limit = parse_depth(optarg, &flags);
1592 flags |= ZFS_ITER_RECURSE;
1595 cb.cb_scripted = B_TRUE;
1598 (void) fprintf(stderr, gettext("missing argument for "
1599 "'%c' option\n"), optopt);
1604 * Process the set of columns to display. We zero out
1605 * the structure to give us a blank slate.
1607 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1609 while (*optarg != '\0') {
1610 static char *col_subopts[] =
1611 { "name", "property", "value", "received",
1612 "source", "all", NULL };
1614 if (i == ZFS_GET_NCOLS) {
1615 (void) fprintf(stderr, gettext("too "
1616 "many fields given to -o "
1621 switch (getsubopt(&optarg, col_subopts,
1624 cb.cb_columns[i++] = GET_COL_NAME;
1627 cb.cb_columns[i++] = GET_COL_PROPERTY;
1630 cb.cb_columns[i++] = GET_COL_VALUE;
1633 cb.cb_columns[i++] = GET_COL_RECVD;
1634 flags |= ZFS_ITER_RECVD_PROPS;
1637 cb.cb_columns[i++] = GET_COL_SOURCE;
1641 (void) fprintf(stderr,
1642 gettext("\"all\" conflicts "
1643 "with specific fields "
1644 "given to -o option\n"));
1647 cb.cb_columns[0] = GET_COL_NAME;
1648 cb.cb_columns[1] = GET_COL_PROPERTY;
1649 cb.cb_columns[2] = GET_COL_VALUE;
1650 cb.cb_columns[3] = GET_COL_RECVD;
1651 cb.cb_columns[4] = GET_COL_SOURCE;
1652 flags |= ZFS_ITER_RECVD_PROPS;
1656 (void) fprintf(stderr,
1657 gettext("invalid column name "
1666 while (*optarg != '\0') {
1667 static char *source_subopts[] = {
1668 "local", "default", "inherited",
1669 "received", "temporary", "none",
1672 switch (getsubopt(&optarg, source_subopts,
1675 cb.cb_sources |= ZPROP_SRC_LOCAL;
1678 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1681 cb.cb_sources |= ZPROP_SRC_INHERITED;
1684 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1687 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1690 cb.cb_sources |= ZPROP_SRC_NONE;
1693 (void) fprintf(stderr,
1694 gettext("invalid source "
1703 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1704 while (*optarg != '\0') {
1705 static char *type_subopts[] = { "filesystem",
1706 "volume", "snapshot", "bookmark",
1709 switch (getsubopt(&optarg, type_subopts,
1712 types |= ZFS_TYPE_FILESYSTEM;
1715 types |= ZFS_TYPE_VOLUME;
1718 types |= ZFS_TYPE_SNAPSHOT;
1721 types |= ZFS_TYPE_BOOKMARK;
1724 types = ZFS_TYPE_DATASET |
1729 (void) fprintf(stderr,
1730 gettext("invalid type '%s'\n"),
1738 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1748 (void) fprintf(stderr, gettext("missing property "
1755 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1763 * As part of zfs_expand_proplist(), we keep track of the maximum column
1764 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1765 * need to know the maximum name length. However, the user likely did
1766 * not specify 'name' as one of the properties to fetch, so we need to
1767 * make sure we always include at least this property for
1768 * print_get_headers() to work properly.
1770 if (cb.cb_proplist != NULL) {
1771 fake_name.pl_prop = ZFS_PROP_NAME;
1772 fake_name.pl_width = strlen(gettext("NAME"));
1773 fake_name.pl_next = cb.cb_proplist;
1774 cb.cb_proplist = &fake_name;
1777 cb.cb_first = B_TRUE;
1779 /* run for each object */
1780 ret = zfs_for_each(argc, argv, flags, types, NULL,
1781 &cb.cb_proplist, limit, get_callback, &cb);
1783 if (cb.cb_proplist == &fake_name)
1784 zprop_free_list(fake_name.pl_next);
1786 zprop_free_list(cb.cb_proplist);
1792 * inherit [-rS] <property> <fs|vol> ...
1794 * -r Recurse over all children
1795 * -S Revert to received value, if any
1797 * For each dataset specified on the command line, inherit the given property
1798 * from its parent. Inheriting a property at the pool level will cause it to
1799 * use the default value. The '-r' flag will recurse over all children, and is
1800 * useful for setting a property on a hierarchy-wide basis, regardless of any
1801 * local modifications for each dataset.
1804 typedef struct inherit_cbdata {
1805 const char *cb_propname;
1806 boolean_t cb_received;
1810 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1812 inherit_cbdata_t *cb = data;
1813 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1816 * If we're doing it recursively, then ignore properties that
1817 * are not valid for this type of dataset.
1819 if (prop != ZPROP_INVAL &&
1820 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1823 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1827 inherit_cb(zfs_handle_t *zhp, void *data)
1829 inherit_cbdata_t *cb = data;
1831 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1835 zfs_do_inherit(int argc, char **argv)
1839 inherit_cbdata_t cb = { 0 };
1843 boolean_t received = B_FALSE;
1846 while ((c = getopt(argc, argv, "rS")) != -1) {
1849 flags |= ZFS_ITER_RECURSE;
1856 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1865 /* check number of arguments */
1867 (void) fprintf(stderr, gettext("missing property argument\n"));
1871 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1879 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1880 if (zfs_prop_readonly(prop)) {
1881 (void) fprintf(stderr, gettext(
1882 "%s property is read-only\n"),
1886 if (!zfs_prop_inheritable(prop) && !received) {
1887 (void) fprintf(stderr, gettext("'%s' property cannot "
1888 "be inherited\n"), propname);
1889 if (prop == ZFS_PROP_QUOTA ||
1890 prop == ZFS_PROP_RESERVATION ||
1891 prop == ZFS_PROP_REFQUOTA ||
1892 prop == ZFS_PROP_REFRESERVATION)
1893 (void) fprintf(stderr, gettext("use 'zfs set "
1894 "%s=none' to clear\n"), propname);
1897 if (received && (prop == ZFS_PROP_VOLSIZE ||
1898 prop == ZFS_PROP_VERSION)) {
1899 (void) fprintf(stderr, gettext("'%s' property cannot "
1900 "be reverted to a received value\n"), propname);
1903 } else if (!zfs_prop_user(propname)) {
1904 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1909 cb.cb_propname = propname;
1910 cb.cb_received = received;
1912 if (flags & ZFS_ITER_RECURSE) {
1913 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1914 NULL, NULL, 0, inherit_recurse_cb, &cb);
1916 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1917 NULL, NULL, 0, inherit_cb, &cb);
1923 typedef struct upgrade_cbdata {
1924 uint64_t cb_numupgraded;
1925 uint64_t cb_numsamegraded;
1926 uint64_t cb_numfailed;
1927 uint64_t cb_version;
1929 boolean_t cb_foundone;
1930 char cb_lastfs[ZFS_MAXNAMELEN];
1934 same_pool(zfs_handle_t *zhp, const char *name)
1936 int len1 = strcspn(name, "/@");
1937 const char *zhname = zfs_get_name(zhp);
1938 int len2 = strcspn(zhname, "/@");
1942 return (strncmp(name, zhname, len1) == 0);
1946 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1948 upgrade_cbdata_t *cb = data;
1949 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1951 /* list if it's old/new */
1952 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1953 (cb->cb_newer && version > ZPL_VERSION)) {
1956 str = gettext("The following filesystems are "
1957 "formatted using a newer software version and\n"
1958 "cannot be accessed on the current system.\n\n");
1960 str = gettext("The following filesystems are "
1961 "out of date, and can be upgraded. After being\n"
1962 "upgraded, these filesystems (and any 'zfs send' "
1963 "streams generated from\n"
1964 "subsequent snapshots) will no longer be "
1965 "accessible by older software versions.\n\n");
1968 if (!cb->cb_foundone) {
1970 (void) printf(gettext("VER FILESYSTEM\n"));
1971 (void) printf(gettext("--- ------------\n"));
1972 cb->cb_foundone = B_TRUE;
1975 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1982 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1984 upgrade_cbdata_t *cb = data;
1985 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1986 int needed_spa_version;
1989 if (zfs_spa_version(zhp, &spa_version) < 0)
1992 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1994 if (needed_spa_version < 0)
1997 if (spa_version < needed_spa_version) {
1999 (void) printf(gettext("%s: can not be "
2000 "upgraded; the pool version needs to first "
2001 "be upgraded\nto version %d\n\n"),
2002 zfs_get_name(zhp), needed_spa_version);
2008 if (version < cb->cb_version) {
2010 (void) snprintf(verstr, sizeof (verstr),
2011 "%llu", cb->cb_version);
2012 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2014 * If they did "zfs upgrade -a", then we could
2015 * be doing ioctls to different pools. We need
2016 * to log this history once to each pool, and bypass
2017 * the normal history logging that happens in main().
2019 (void) zpool_log_history(g_zfs, history_str);
2020 log_history = B_FALSE;
2022 if (zfs_prop_set(zhp, "version", verstr) == 0)
2023 cb->cb_numupgraded++;
2026 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2027 } else if (version > cb->cb_version) {
2028 /* can't downgrade */
2029 (void) printf(gettext("%s: can not be downgraded; "
2030 "it is already at version %u\n"),
2031 zfs_get_name(zhp), version);
2034 cb->cb_numsamegraded++;
2042 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2045 zfs_do_upgrade(int argc, char **argv)
2047 boolean_t all = B_FALSE;
2048 boolean_t showversions = B_FALSE;
2050 upgrade_cbdata_t cb = { 0 };
2052 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2055 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2058 flags |= ZFS_ITER_RECURSE;
2061 showversions = B_TRUE;
2064 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2065 optarg, &cb.cb_version) != 0) {
2066 (void) fprintf(stderr,
2067 gettext("invalid version %s\n"), optarg);
2076 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2085 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2087 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2088 cb.cb_version || argc))
2090 if ((all || argc) && (showversions))
2096 /* Show info on available versions. */
2097 (void) printf(gettext("The following filesystem versions are "
2099 (void) printf(gettext("VER DESCRIPTION\n"));
2100 (void) printf("--- -----------------------------------------"
2101 "---------------\n");
2102 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2103 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2104 (void) printf(gettext(" 3 Case insensitive and filesystem "
2105 "user identifier (FUID)\n"));
2106 (void) printf(gettext(" 4 userquota, groupquota "
2108 (void) printf(gettext(" 5 System attributes\n"));
2109 (void) printf(gettext("\nFor more information on a particular "
2110 "version, including supported releases,\n"));
2111 (void) printf("see the ZFS Administration Guide.\n\n");
2113 } else if (argc || all) {
2114 /* Upgrade filesystems */
2115 if (cb.cb_version == 0)
2116 cb.cb_version = ZPL_VERSION;
2117 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2118 NULL, NULL, 0, upgrade_set_callback, &cb);
2119 (void) printf(gettext("%llu filesystems upgraded\n"),
2121 if (cb.cb_numsamegraded) {
2122 (void) printf(gettext("%llu filesystems already at "
2124 cb.cb_numsamegraded);
2126 if (cb.cb_numfailed != 0)
2129 /* List old-version filesytems */
2131 (void) printf(gettext("This system is currently running "
2132 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2134 flags |= ZFS_ITER_RECURSE;
2135 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2136 NULL, NULL, 0, upgrade_list_callback, &cb);
2138 found = cb.cb_foundone;
2139 cb.cb_foundone = B_FALSE;
2140 cb.cb_newer = B_TRUE;
2142 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2143 NULL, NULL, 0, upgrade_list_callback, &cb);
2145 if (!cb.cb_foundone && !found) {
2146 (void) printf(gettext("All filesystems are "
2147 "formatted with the current version.\n"));
2155 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2156 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2157 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2158 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2160 * -H Scripted mode; elide headers and separate columns by tabs.
2161 * -i Translate SID to POSIX ID.
2162 * -n Print numeric ID instead of user/group name.
2163 * -o Control which fields to display.
2164 * -p Use exact (parsable) numeric output.
2165 * -s Specify sort columns, descending order.
2166 * -S Specify sort columns, ascending order.
2167 * -t Control which object types to display.
2169 * Displays space consumed by, and quotas on, each user in the specified
2170 * filesystem or snapshot.
2173 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2174 enum us_field_types {
2180 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2181 static char *us_field_names[] = { "type", "name", "used", "quota" };
2182 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2184 #define USTYPE_PSX_GRP (1 << 0)
2185 #define USTYPE_PSX_USR (1 << 1)
2186 #define USTYPE_SMB_GRP (1 << 2)
2187 #define USTYPE_SMB_USR (1 << 3)
2188 #define USTYPE_ALL \
2189 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2191 static int us_type_bits[] = {
2198 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2201 typedef struct us_node {
2203 uu_avl_node_t usn_avlnode;
2204 uu_list_node_t usn_listnode;
2207 typedef struct us_cbdata {
2209 uu_avl_pool_t *cb_avl_pool;
2211 boolean_t cb_numname;
2212 boolean_t cb_nicenum;
2213 boolean_t cb_sid2posix;
2214 zfs_userquota_prop_t cb_prop;
2215 zfs_sort_column_t *cb_sortcol;
2216 size_t cb_width[USFIELD_LAST];
2219 static boolean_t us_populated = B_FALSE;
2222 zfs_sort_column_t *si_sortcol;
2223 boolean_t si_numname;
2227 us_field_index(char *field)
2231 for (i = 0; i < USFIELD_LAST; i++) {
2232 if (strcmp(field, us_field_names[i]) == 0)
2240 us_compare(const void *larg, const void *rarg, void *unused)
2242 const us_node_t *l = larg;
2243 const us_node_t *r = rarg;
2244 us_sort_info_t *si = (us_sort_info_t *)unused;
2245 zfs_sort_column_t *sortcol = si->si_sortcol;
2246 boolean_t numname = si->si_numname;
2247 nvlist_t *lnvl = l->usn_nvl;
2248 nvlist_t *rnvl = r->usn_nvl;
2252 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2259 zfs_prop_t prop = sortcol->sc_prop;
2260 const char *propname = NULL;
2261 boolean_t reverse = sortcol->sc_reverse;
2266 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2267 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2269 rc = (rv32 < lv32) ? 1 : -1;
2274 (void) nvlist_lookup_uint64(lnvl, propname,
2276 (void) nvlist_lookup_uint64(rnvl, propname,
2279 rc = (rv64 < lv64) ? 1 : -1;
2281 (void) nvlist_lookup_string(lnvl, propname,
2283 (void) nvlist_lookup_string(rnvl, propname,
2285 rc = strcmp(lvstr, rvstr);
2289 case ZFS_PROP_QUOTA:
2292 if (prop == ZFS_PROP_USED)
2296 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2297 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2299 rc = (rv64 < lv64) ? 1 : -1;
2305 return (reverse ? 1 : -1);
2307 return (reverse ? -1 : 1);
2312 * If entries still seem to be the same, check if they are of the same
2313 * type (smbentity is added only if we are doing SID to POSIX ID
2314 * translation where we can have duplicate type/name combinations).
2316 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2317 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2319 return (lvb < rvb ? -1 : 1);
2324 static inline const char *
2325 us_type2str(unsigned field_type)
2327 switch (field_type) {
2328 case USTYPE_PSX_USR:
2329 return ("POSIX User");
2330 case USTYPE_PSX_GRP:
2331 return ("POSIX Group");
2332 case USTYPE_SMB_USR:
2333 return ("SMB User");
2334 case USTYPE_SMB_GRP:
2335 return ("SMB Group");
2337 return ("Undefined");
2342 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2344 us_cbdata_t *cb = (us_cbdata_t *)arg;
2345 zfs_userquota_prop_t prop = cb->cb_prop;
2350 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2351 uu_avl_t *avl = cb->cb_avl;
2355 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2357 const char *typestr;
2361 int typeidx, nameidx, sizeidx;
2362 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2363 boolean_t smbentity = B_FALSE;
2365 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2367 node = safe_malloc(sizeof (us_node_t));
2368 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2369 node->usn_nvl = props;
2371 if (domain != NULL && domain[0] != '\0') {
2373 char sid[ZFS_MAXNAMELEN + 32];
2377 directory_error_t e;
2381 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2383 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2384 type = USTYPE_SMB_GRP;
2385 err = sid_to_id(sid, B_FALSE, &id);
2387 type = USTYPE_SMB_USR;
2388 err = sid_to_id(sid, B_TRUE, &id);
2393 if (!cb->cb_sid2posix) {
2394 e = directory_name_from_sid(NULL, sid, &name,
2397 directory_error_free(e);
2404 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2406 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2407 type = USTYPE_PSX_GRP;
2408 if (!cb->cb_numname) {
2411 if ((g = getgrgid(rid)) != NULL)
2415 type = USTYPE_PSX_USR;
2416 if (!cb->cb_numname) {
2419 if ((p = getpwuid(rid)) != NULL)
2426 * Make sure that the type/name combination is unique when doing
2427 * SID to POSIX ID translation (hence changing the type from SMB to
2430 if (cb->cb_sid2posix &&
2431 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2434 /* Calculate/update width of TYPE field */
2435 typestr = us_type2str(type);
2436 typelen = strlen(gettext(typestr));
2437 typeidx = us_field_index("type");
2438 if (typelen > cb->cb_width[typeidx])
2439 cb->cb_width[typeidx] = typelen;
2440 if (nvlist_add_uint32(props, "type", type) != 0)
2443 /* Calculate/update width of NAME field */
2444 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2445 if (nvlist_add_uint64(props, "name", rid) != 0)
2447 namelen = snprintf(NULL, 0, "%u", rid);
2449 if (nvlist_add_string(props, "name", name) != 0)
2451 namelen = strlen(name);
2453 nameidx = us_field_index("name");
2454 if (namelen > cb->cb_width[nameidx])
2455 cb->cb_width[nameidx] = namelen;
2458 * Check if this type/name combination is in the list and update it;
2459 * otherwise add new node to the list.
2461 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2462 uu_avl_insert(avl, node, idx);
2467 props = node->usn_nvl;
2470 /* Calculate/update width of USED/QUOTA fields */
2472 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2474 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2475 sizelen = strlen(sizebuf);
2476 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2478 if (!nvlist_exists(props, "quota"))
2479 (void) nvlist_add_uint64(props, "quota", 0);
2482 if (!nvlist_exists(props, "used"))
2483 (void) nvlist_add_uint64(props, "used", 0);
2485 sizeidx = us_field_index(propname);
2486 if (sizelen > cb->cb_width[sizeidx])
2487 cb->cb_width[sizeidx] = sizelen;
2489 if (nvlist_add_uint64(props, propname, space) != 0)
2496 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2497 size_t *width, us_node_t *node)
2499 nvlist_t *nvl = node->usn_nvl;
2500 char valstr[ZFS_MAXNAMELEN];
2501 boolean_t first = B_TRUE;
2507 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2508 if (!(ustype & types))
2511 while ((field = fields[cfield]) != USFIELD_LAST) {
2512 nvpair_t *nvp = NULL;
2516 char *strval = NULL;
2518 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2519 if (strcmp(nvpair_name(nvp),
2520 us_field_names[field]) == 0)
2524 type = nvpair_type(nvp);
2526 case DATA_TYPE_UINT32:
2527 (void) nvpair_value_uint32(nvp, &val32);
2529 case DATA_TYPE_UINT64:
2530 (void) nvpair_value_uint64(nvp, &val64);
2532 case DATA_TYPE_STRING:
2533 (void) nvpair_value_string(nvp, &strval);
2536 (void) fprintf(stderr, "invalid data type\n");
2541 strval = (char *)us_type2str(val32);
2544 if (type == DATA_TYPE_UINT64) {
2545 (void) sprintf(valstr, "%llu", val64);
2551 if (type == DATA_TYPE_UINT64) {
2553 (void) sprintf(valstr, "%llu", val64);
2555 zfs_nicenum(val64, valstr,
2558 if (field == USFIELD_QUOTA &&
2559 strcmp(valstr, "0") == 0)
2569 (void) printf("\t");
2574 (void) printf("%s", strval);
2575 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2576 (void) printf("%-*s", width[field], strval);
2578 (void) printf("%*s", width[field], strval);
2584 (void) printf("\n");
2588 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2589 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2597 boolean_t first = B_TRUE;
2599 while ((field = fields[cfield]) != USFIELD_LAST) {
2600 col = gettext(us_field_hdr[field]);
2601 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2602 (void) printf(first ? "%-*s" : " %-*s",
2605 (void) printf(first ? "%*s" : " %*s",
2611 (void) printf("\n");
2614 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2615 print_us_node(scripted, parsable, fields, types, width, node);
2617 nvlist_free(node->usn_nvl);
2622 zfs_do_userspace(int argc, char **argv)
2625 zfs_userquota_prop_t p;
2626 uu_avl_pool_t *avl_pool;
2628 uu_avl_walk_t *walk;
2630 char deffields[] = "type,name,used,quota";
2631 char *ofield = NULL;
2632 char *tfield = NULL;
2636 boolean_t scripted = B_FALSE;
2637 boolean_t prtnum = B_FALSE;
2638 boolean_t parsable = B_FALSE;
2639 boolean_t sid2posix = B_FALSE;
2642 zfs_sort_column_t *sortcol = NULL;
2643 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2647 uu_list_pool_t *listpool;
2649 uu_avl_index_t idx = 0;
2650 uu_list_index_t idx2 = 0;
2655 if (strcmp(argv[0], "groupspace") == 0)
2656 /* Toggle default group types */
2657 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2659 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2675 if (zfs_add_sort_column(&sortcol, optarg,
2676 c == 's' ? B_FALSE : B_TRUE) != 0) {
2677 (void) fprintf(stderr,
2678 gettext("invalid field '%s'\n"), optarg);
2689 (void) fprintf(stderr, gettext("missing argument for "
2690 "'%c' option\n"), optopt);
2694 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2704 (void) fprintf(stderr, gettext("missing dataset name\n"));
2708 (void) fprintf(stderr, gettext("too many arguments\n"));
2712 /* Use default output fields if not specified using -o */
2716 if ((delim = strchr(ofield, ',')) != NULL)
2718 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2719 (void) fprintf(stderr, gettext("invalid type '%s' "
2720 "for -o option\n"), ofield);
2725 } while (delim != NULL);
2726 fields[cfield] = USFIELD_LAST;
2728 /* Override output types (-t option) */
2729 if (tfield != NULL) {
2733 boolean_t found = B_FALSE;
2735 if ((delim = strchr(tfield, ',')) != NULL)
2737 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2739 if (strcmp(tfield, us_type_names[i]) == 0) {
2741 types |= us_type_bits[i];
2746 (void) fprintf(stderr, gettext("invalid type "
2747 "'%s' for -t option\n"), tfield);
2752 } while (delim != NULL);
2755 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2758 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2759 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2761 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2764 /* Always add default sorting columns */
2765 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2766 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2768 cb.cb_sortcol = sortcol;
2769 cb.cb_numname = prtnum;
2770 cb.cb_nicenum = !parsable;
2771 cb.cb_avl_pool = avl_pool;
2772 cb.cb_avl = avl_tree;
2773 cb.cb_sid2posix = sid2posix;
2775 for (i = 0; i < USFIELD_LAST; i++)
2776 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2778 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2779 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2780 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2781 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2782 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2785 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2790 if ((node = uu_avl_first(avl_tree)) == NULL)
2793 us_populated = B_TRUE;
2795 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2796 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2797 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2798 uu_list_node_init(node, &node->usn_listnode, listpool);
2800 while (node != NULL) {
2802 node = uu_avl_next(avl_tree, node);
2803 uu_avl_remove(avl_tree, rmnode);
2804 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2805 uu_list_insert(list, rmnode, idx2);
2808 for (node = uu_list_first(list); node != NULL;
2809 node = uu_list_next(list, node)) {
2810 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2812 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2813 uu_avl_insert(avl_tree, node, idx);
2816 uu_list_destroy(list);
2817 uu_list_pool_destroy(listpool);
2819 /* Print and free node nvlist memory */
2820 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2823 zfs_free_sort_columns(sortcol);
2825 /* Clean up the AVL tree */
2826 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2829 while ((node = uu_avl_walk_next(walk)) != NULL) {
2830 uu_avl_remove(cb.cb_avl, node);
2834 uu_avl_walk_end(walk);
2835 uu_avl_destroy(avl_tree);
2836 uu_avl_pool_destroy(avl_pool);
2842 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2843 * [-t type[,...]] [filesystem|volume|snapshot] ...
2845 * -H Scripted mode; elide headers and separate columns by tabs.
2846 * -p Display values in parsable (literal) format.
2847 * -r Recurse over all children.
2848 * -d Limit recursion by depth.
2849 * -o Control which fields to display.
2850 * -s Specify sort columns, descending order.
2851 * -S Specify sort columns, ascending order.
2852 * -t Control which object types to display.
2854 * When given no arguments, list all filesystems in the system.
2855 * Otherwise, list the specified datasets, optionally recursing down them if
2856 * '-r' is specified.
2858 typedef struct list_cbdata {
2860 boolean_t cb_literal;
2861 boolean_t cb_scripted;
2862 zprop_list_t *cb_proplist;
2866 * Given a list of columns to display, output appropriate headers for each one.
2869 print_header(list_cbdata_t *cb)
2871 zprop_list_t *pl = cb->cb_proplist;
2872 char headerbuf[ZFS_MAXPROPLEN];
2875 boolean_t first = B_TRUE;
2876 boolean_t right_justify;
2878 for (; pl != NULL; pl = pl->pl_next) {
2885 right_justify = B_FALSE;
2886 if (pl->pl_prop != ZPROP_INVAL) {
2887 header = zfs_prop_column_name(pl->pl_prop);
2888 right_justify = zfs_prop_align_right(pl->pl_prop);
2890 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2891 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2892 headerbuf[i] = '\0';
2896 if (pl->pl_next == NULL && !right_justify)
2897 (void) printf("%s", header);
2898 else if (right_justify)
2899 (void) printf("%*s", pl->pl_width, header);
2901 (void) printf("%-*s", pl->pl_width, header);
2904 (void) printf("\n");
2908 * Given a dataset and a list of fields, print out all the properties according
2909 * to the described layout.
2912 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2914 zprop_list_t *pl = cb->cb_proplist;
2915 boolean_t first = B_TRUE;
2916 char property[ZFS_MAXPROPLEN];
2917 nvlist_t *userprops = zfs_get_user_props(zhp);
2920 boolean_t right_justify;
2922 for (; pl != NULL; pl = pl->pl_next) {
2924 if (cb->cb_scripted)
2925 (void) printf("\t");
2932 if (pl->pl_prop != ZPROP_INVAL) {
2933 if (zfs_prop_get(zhp, pl->pl_prop, property,
2934 sizeof (property), NULL, NULL, 0,
2935 cb->cb_literal) != 0)
2939 right_justify = zfs_prop_align_right(pl->pl_prop);
2940 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2941 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2942 property, sizeof (property), cb->cb_literal) != 0)
2946 right_justify = B_TRUE;
2947 } else if (zfs_prop_written(pl->pl_user_prop)) {
2948 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2949 property, sizeof (property), cb->cb_literal) != 0)
2953 right_justify = B_TRUE;
2955 if (nvlist_lookup_nvlist(userprops,
2956 pl->pl_user_prop, &propval) != 0)
2959 verify(nvlist_lookup_string(propval,
2960 ZPROP_VALUE, &propstr) == 0);
2961 right_justify = B_FALSE;
2965 * If this is being called in scripted mode, or if this is the
2966 * last column and it is left-justified, don't include a width
2969 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2970 (void) printf("%s", propstr);
2971 else if (right_justify)
2972 (void) printf("%*s", pl->pl_width, propstr);
2974 (void) printf("%-*s", pl->pl_width, propstr);
2977 (void) printf("\n");
2981 * Generic callback function to list a dataset or snapshot.
2984 list_callback(zfs_handle_t *zhp, void *data)
2986 list_cbdata_t *cbp = data;
2988 if (cbp->cb_first) {
2989 if (!cbp->cb_scripted)
2991 cbp->cb_first = B_FALSE;
2994 print_dataset(zhp, cbp);
3000 zfs_do_list(int argc, char **argv)
3003 static char default_fields[] =
3004 "name,used,available,referenced,mountpoint";
3005 int types = ZFS_TYPE_DATASET;
3006 boolean_t types_specified = B_FALSE;
3007 char *fields = NULL;
3008 list_cbdata_t cb = { 0 };
3012 zfs_sort_column_t *sortcol = NULL;
3013 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3016 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3022 cb.cb_literal = B_TRUE;
3023 flags |= ZFS_ITER_LITERAL_PROPS;
3026 limit = parse_depth(optarg, &flags);
3029 flags |= ZFS_ITER_RECURSE;
3032 cb.cb_scripted = B_TRUE;
3035 if (zfs_add_sort_column(&sortcol, optarg,
3037 (void) fprintf(stderr,
3038 gettext("invalid property '%s'\n"), optarg);
3043 if (zfs_add_sort_column(&sortcol, optarg,
3045 (void) fprintf(stderr,
3046 gettext("invalid property '%s'\n"), optarg);
3052 types_specified = B_TRUE;
3053 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3054 while (*optarg != '\0') {
3055 static char *type_subopts[] = { "filesystem",
3056 "volume", "snapshot", "snap", "bookmark",
3059 switch (getsubopt(&optarg, type_subopts,
3062 types |= ZFS_TYPE_FILESYSTEM;
3065 types |= ZFS_TYPE_VOLUME;
3069 types |= ZFS_TYPE_SNAPSHOT;
3072 types |= ZFS_TYPE_BOOKMARK;
3075 types = ZFS_TYPE_DATASET |
3079 (void) fprintf(stderr,
3080 gettext("invalid type '%s'\n"),
3087 (void) fprintf(stderr, gettext("missing argument for "
3088 "'%c' option\n"), optopt);
3092 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3102 fields = default_fields;
3105 * If "-o space" and no types were specified, don't display snapshots.
3107 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3108 types &= ~ZFS_TYPE_SNAPSHOT;
3111 * If the user specifies '-o all', the zprop_get_list() doesn't
3112 * normally include the name of the dataset. For 'zfs list', we always
3113 * want this property to be first.
3115 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3119 cb.cb_first = B_TRUE;
3121 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3122 limit, list_callback, &cb);
3124 zprop_free_list(cb.cb_proplist);
3125 zfs_free_sort_columns(sortcol);
3127 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3128 (void) printf(gettext("no datasets available\n"));
3134 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3135 * zfs rename [-f] -p <fs | vol> <fs | vol>
3136 * zfs rename -r <snap> <snap>
3138 * Renames the given dataset to another of the same type.
3140 * The '-p' flag creates all the non-existing ancestors of the target first.
3144 zfs_do_rename(int argc, char **argv)
3149 boolean_t recurse = B_FALSE;
3150 boolean_t parents = B_FALSE;
3151 boolean_t force_unmount = B_FALSE;
3154 while ((c = getopt(argc, argv, "prf")) != -1) {
3163 force_unmount = B_TRUE;
3167 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3176 /* check number of arguments */
3178 (void) fprintf(stderr, gettext("missing source dataset "
3183 (void) fprintf(stderr, gettext("missing target dataset "
3188 (void) fprintf(stderr, gettext("too many arguments\n"));
3192 if (recurse && parents) {
3193 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3198 if (recurse && strchr(argv[0], '@') == 0) {
3199 (void) fprintf(stderr, gettext("source dataset for recursive "
3200 "rename must be a snapshot\n"));
3204 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3205 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3208 /* If we were asked and the name looks good, try to create ancestors. */
3209 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3210 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3215 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3224 * Promotes the given clone fs to be the parent
3228 zfs_do_promote(int argc, char **argv)
3234 if (argc > 1 && argv[1][0] == '-') {
3235 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3240 /* check number of arguments */
3242 (void) fprintf(stderr, gettext("missing clone filesystem"
3247 (void) fprintf(stderr, gettext("too many arguments\n"));
3251 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3255 ret = (zfs_promote(zhp) != 0);
3263 * zfs rollback [-rRf] <snapshot>
3265 * -r Delete any intervening snapshots before doing rollback
3266 * -R Delete any snapshots and their clones
3267 * -f ignored for backwards compatability
3269 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3270 * since then and making it the active dataset. If more recent snapshots exist,
3271 * the command will complain unless the '-r' flag is given.
3273 typedef struct rollback_cbdata {
3279 boolean_t cb_recurse;
3280 } rollback_cbdata_t;
3283 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3285 rollback_cbdata_t *cbp = data;
3287 if (cbp->cb_first && cbp->cb_recurse) {
3288 (void) fprintf(stderr, gettext("cannot rollback to "
3289 "'%s': clones of previous snapshots exist\n"),
3291 (void) fprintf(stderr, gettext("use '-R' to "
3292 "force deletion of the following clones and "
3298 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3305 * Report any snapshots more recent than the one specified. Used when '-r' is
3306 * not specified. We reuse this same callback for the snapshot dependents - if
3307 * 'cb_dependent' is set, then this is a dependent and we should report it
3308 * without checking the transaction group.
3311 rollback_check(zfs_handle_t *zhp, void *data)
3313 rollback_cbdata_t *cbp = data;
3315 if (cbp->cb_doclones) {
3320 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3321 if (cbp->cb_first && !cbp->cb_recurse) {
3322 (void) fprintf(stderr, gettext("cannot "
3323 "rollback to '%s': more recent snapshots "
3324 "or bookmarks exist\n"),
3326 (void) fprintf(stderr, gettext("use '-r' to "
3327 "force deletion of the following "
3328 "snapshots and bookmarks:\n"));
3333 if (cbp->cb_recurse) {
3334 if (zfs_iter_dependents(zhp, B_TRUE,
3335 rollback_check_dependent, cbp) != 0) {
3340 (void) fprintf(stderr, "%s\n",
3349 zfs_do_rollback(int argc, char **argv)
3353 boolean_t force = B_FALSE;
3354 rollback_cbdata_t cb = { 0 };
3355 zfs_handle_t *zhp, *snap;
3356 char parentname[ZFS_MAXNAMELEN];
3360 while ((c = getopt(argc, argv, "rRf")) != -1) {
3373 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3382 /* check number of arguments */
3384 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3388 (void) fprintf(stderr, gettext("too many arguments\n"));
3392 /* open the snapshot */
3393 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3396 /* open the parent dataset */
3397 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3398 verify((delim = strrchr(parentname, '@')) != NULL);
3400 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3406 * Check for more recent snapshots and/or clones based on the presence
3409 cb.cb_target = argv[0];
3410 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3411 cb.cb_first = B_TRUE;
3413 if ((ret = zfs_iter_snapshots(zhp, rollback_check, &cb)) != 0)
3415 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3418 if ((ret = cb.cb_error) != 0)
3422 * Rollback parent to the given snapshot.
3424 ret = zfs_rollback(zhp, snap, force);
3437 * zfs set property=value { fs | snap | vol } ...
3439 * Sets the given property for all datasets specified on the command line.
3441 typedef struct set_cbdata {
3447 set_callback(zfs_handle_t *zhp, void *data)
3449 set_cbdata_t *cbp = data;
3451 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3452 switch (libzfs_errno(g_zfs)) {
3453 case EZFS_MOUNTFAILED:
3454 (void) fprintf(stderr, gettext("property may be set "
3455 "but unable to remount filesystem\n"));
3457 case EZFS_SHARENFSFAILED:
3458 (void) fprintf(stderr, gettext("property may be set "
3459 "but unable to reshare filesystem\n"));
3468 zfs_do_set(int argc, char **argv)
3473 /* check for options */
3474 if (argc > 1 && argv[1][0] == '-') {
3475 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3480 /* check number of arguments */
3482 (void) fprintf(stderr, gettext("missing property=value "
3487 (void) fprintf(stderr, gettext("missing dataset name\n"));
3491 /* validate property=value argument */
3492 cb.cb_propname = argv[1];
3493 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3494 (cb.cb_value[1] == '\0')) {
3495 (void) fprintf(stderr, gettext("missing value in "
3496 "property=value argument\n"));
3500 *cb.cb_value = '\0';
3503 if (*cb.cb_propname == '\0') {
3504 (void) fprintf(stderr,
3505 gettext("missing property in property=value argument\n"));
3509 ret = zfs_for_each(argc - 2, argv + 2, NULL,
3510 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3515 typedef struct snap_cbdata {
3517 boolean_t sd_recursive;
3518 const char *sd_snapname;
3522 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3524 snap_cbdata_t *sd = arg;
3529 if (sd->sd_recursive &&
3530 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3535 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3538 fnvlist_add_boolean(sd->sd_nvl, name);
3541 if (sd->sd_recursive)
3542 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3548 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3550 * Creates a snapshot with the given name. While functionally equivalent to
3551 * 'zfs create', it is a separate command to differentiate intent.
3554 zfs_do_snapshot(int argc, char **argv)
3559 snap_cbdata_t sd = { 0 };
3560 boolean_t multiple_snaps = B_FALSE;
3562 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3564 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3568 while ((c = getopt(argc, argv, "ro:")) != -1) {
3571 if (parseprop(props))
3575 sd.sd_recursive = B_TRUE;
3576 multiple_snaps = B_TRUE;
3579 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3588 /* check number of arguments */
3590 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3595 multiple_snaps = B_TRUE;
3596 for (; argc > 0; argc--, argv++) {
3600 atp = strchr(argv[0], '@');
3604 sd.sd_snapname = atp + 1;
3605 zhp = zfs_open(g_zfs, argv[0],
3606 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3609 if (zfs_snapshot_cb(zhp, &sd) != 0)
3613 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3614 nvlist_free(sd.sd_nvl);
3616 if (ret != 0 && multiple_snaps)
3617 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3621 nvlist_free(sd.sd_nvl);
3628 * Send a backup stream to stdout.
3631 zfs_do_send(int argc, char **argv)
3633 char *fromname = NULL;
3634 char *toname = NULL;
3637 sendflags_t flags = { 0 };
3639 nvlist_t *dbgnv = NULL;
3640 boolean_t extraverbose = B_FALSE;
3643 while ((c = getopt(argc, argv, ":i:I:RDpvnPLe")) != -1) {
3654 flags.doall = B_TRUE;
3657 flags.replicate = B_TRUE;
3660 flags.props = B_TRUE;
3663 flags.parsable = B_TRUE;
3664 flags.verbose = B_TRUE;
3668 extraverbose = B_TRUE;
3669 flags.verbose = B_TRUE;
3670 flags.progress = B_TRUE;
3673 flags.dedup = B_TRUE;
3676 flags.dryrun = B_TRUE;
3679 flags.largeblock = B_TRUE;
3682 flags.embed_data = B_TRUE;
3685 (void) fprintf(stderr, gettext("missing argument for "
3686 "'%c' option\n"), optopt);
3690 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3699 /* check number of arguments */
3701 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3705 (void) fprintf(stderr, gettext("too many arguments\n"));
3709 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3710 (void) fprintf(stderr,
3711 gettext("Error: Stream can not be written to a terminal.\n"
3712 "You must redirect standard output.\n"));
3717 * Special case sending a filesystem, or from a bookmark.
3719 if (strchr(argv[0], '@') == NULL ||
3720 (fromname && strchr(fromname, '#') != NULL)) {
3721 char frombuf[ZFS_MAXNAMELEN];
3722 enum lzc_send_flags lzc_flags = 0;
3724 if (flags.replicate || flags.doall || flags.props ||
3725 flags.dedup || flags.dryrun || flags.verbose ||
3727 (void) fprintf(stderr,
3729 "Unsupported flag with filesystem or bookmark.\n"));
3733 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3737 if (flags.largeblock)
3738 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3739 if (flags.embed_data)
3740 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3742 if (fromname != NULL &&
3743 (fromname[0] == '#' || fromname[0] == '@')) {
3745 * Incremental source name begins with # or @.
3746 * Default to same fs as target.
3748 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3749 cp = strchr(frombuf, '@');
3752 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3755 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3760 cp = strchr(argv[0], '@');
3763 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3768 * If they specified the full path to the snapshot, chop off
3769 * everything except the short name of the snapshot, but special
3770 * case if they specify the origin.
3772 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3773 char origin[ZFS_MAXNAMELEN];
3776 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3777 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3779 if (strcmp(origin, fromname) == 0) {
3781 flags.fromorigin = B_TRUE;
3784 if (cp != fromname && strcmp(argv[0], fromname)) {
3785 (void) fprintf(stderr,
3786 gettext("incremental source must be "
3787 "in same filesystem\n"));
3791 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3792 (void) fprintf(stderr,
3793 gettext("invalid incremental source\n"));
3799 if (flags.replicate && fromname == NULL)
3800 flags.doall = B_TRUE;
3802 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3803 extraverbose ? &dbgnv : NULL);
3805 if (extraverbose && dbgnv != NULL) {
3807 * dump_nvlist prints to stdout, but that's been
3808 * redirected to a file. Make it print to stderr
3811 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3812 dump_nvlist(dbgnv, 0);
3821 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3823 * Restore a backup stream from stdin.
3826 zfs_do_receive(int argc, char **argv)
3829 recvflags_t flags = { 0 };
3832 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3835 flags.isprefix = B_TRUE;
3838 flags.isprefix = B_TRUE;
3839 flags.istail = B_TRUE;
3842 flags.dryrun = B_TRUE;
3845 flags.nomount = B_TRUE;
3848 flags.verbose = B_TRUE;
3851 flags.force = B_TRUE;
3854 (void) fprintf(stderr, gettext("missing argument for "
3855 "'%c' option\n"), optopt);
3859 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3868 /* check number of arguments */
3870 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3874 (void) fprintf(stderr, gettext("too many arguments\n"));
3878 if (isatty(STDIN_FILENO)) {
3879 (void) fprintf(stderr,
3880 gettext("Error: Backup stream can not be read "
3881 "from a terminal.\n"
3882 "You must redirect standard input.\n"));
3886 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3892 * allow/unallow stuff
3894 /* copied from zfs/sys/dsl_deleg.h */
3895 #define ZFS_DELEG_PERM_CREATE "create"
3896 #define ZFS_DELEG_PERM_DESTROY "destroy"
3897 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3898 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3899 #define ZFS_DELEG_PERM_CLONE "clone"
3900 #define ZFS_DELEG_PERM_PROMOTE "promote"
3901 #define ZFS_DELEG_PERM_RENAME "rename"
3902 #define ZFS_DELEG_PERM_MOUNT "mount"
3903 #define ZFS_DELEG_PERM_SHARE "share"
3904 #define ZFS_DELEG_PERM_SEND "send"
3905 #define ZFS_DELEG_PERM_RECEIVE "receive"
3906 #define ZFS_DELEG_PERM_ALLOW "allow"
3907 #define ZFS_DELEG_PERM_USERPROP "userprop"
3908 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3909 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3910 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3911 #define ZFS_DELEG_PERM_USERUSED "userused"
3912 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3913 #define ZFS_DELEG_PERM_HOLD "hold"
3914 #define ZFS_DELEG_PERM_RELEASE "release"
3915 #define ZFS_DELEG_PERM_DIFF "diff"
3916 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3918 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3920 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3921 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3922 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3923 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3924 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3925 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3926 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3927 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3928 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3929 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3930 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3931 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3932 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3933 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3934 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3935 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3936 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
3938 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3939 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3940 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3941 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3942 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3943 { NULL, ZFS_DELEG_NOTE_NONE }
3946 /* permission structure */
3947 typedef struct deleg_perm {
3948 zfs_deleg_who_type_t dp_who_type;
3949 const char *dp_name;
3951 boolean_t dp_descend;
3955 typedef struct deleg_perm_node {
3956 deleg_perm_t dpn_perm;
3958 uu_avl_node_t dpn_avl_node;
3959 } deleg_perm_node_t;
3961 typedef struct fs_perm fs_perm_t;
3963 /* permissions set */
3964 typedef struct who_perm {
3965 zfs_deleg_who_type_t who_type;
3966 const char *who_name; /* id */
3967 char who_ug_name[256]; /* user/group name */
3968 fs_perm_t *who_fsperm; /* uplink */
3970 uu_avl_t *who_deleg_perm_avl; /* permissions */
3974 typedef struct who_perm_node {
3975 who_perm_t who_perm;
3976 uu_avl_node_t who_avl_node;
3979 typedef struct fs_perm_set fs_perm_set_t;
3980 /* fs permissions */
3982 const char *fsp_name;
3984 uu_avl_t *fsp_sc_avl; /* sets,create */
3985 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3987 fs_perm_set_t *fsp_set; /* uplink */
3991 typedef struct fs_perm_node {
3992 fs_perm_t fspn_fsperm;
3995 uu_list_node_t fspn_list_node;
3998 /* top level structure */
3999 struct fs_perm_set {
4000 uu_list_pool_t *fsps_list_pool;
4001 uu_list_t *fsps_list; /* list of fs_perms */
4003 uu_avl_pool_t *fsps_named_set_avl_pool;
4004 uu_avl_pool_t *fsps_who_perm_avl_pool;
4005 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4008 static inline const char *
4009 deleg_perm_type(zfs_deleg_note_t note)
4015 case ZFS_DELEG_NOTE_GROUPQUOTA:
4016 case ZFS_DELEG_NOTE_GROUPUSED:
4017 case ZFS_DELEG_NOTE_USERPROP:
4018 case ZFS_DELEG_NOTE_USERQUOTA:
4019 case ZFS_DELEG_NOTE_USERUSED:
4021 return (gettext("other"));
4023 return (gettext("subcommand"));
4028 who_type2weight(zfs_deleg_who_type_t who_type)
4032 case ZFS_DELEG_NAMED_SET_SETS:
4033 case ZFS_DELEG_NAMED_SET:
4036 case ZFS_DELEG_CREATE_SETS:
4037 case ZFS_DELEG_CREATE:
4040 case ZFS_DELEG_USER_SETS:
4041 case ZFS_DELEG_USER:
4044 case ZFS_DELEG_GROUP_SETS:
4045 case ZFS_DELEG_GROUP:
4048 case ZFS_DELEG_EVERYONE_SETS:
4049 case ZFS_DELEG_EVERYONE:
4061 who_perm_compare(const void *larg, const void *rarg, void *unused)
4063 const who_perm_node_t *l = larg;
4064 const who_perm_node_t *r = rarg;
4065 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4066 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4067 int lweight = who_type2weight(ltype);
4068 int rweight = who_type2weight(rtype);
4069 int res = lweight - rweight;
4071 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4072 ZFS_MAX_DELEG_NAME-1);
4084 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4086 const deleg_perm_node_t *l = larg;
4087 const deleg_perm_node_t *r = rarg;
4088 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4089 ZFS_MAX_DELEG_NAME-1);
4101 fs_perm_set_init(fs_perm_set_t *fspset)
4103 bzero(fspset, sizeof (fs_perm_set_t));
4105 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4106 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4107 NULL, UU_DEFAULT)) == NULL)
4109 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4110 UU_DEFAULT)) == NULL)
4113 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4114 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4115 who_perm_node_t, who_avl_node), who_perm_compare,
4116 UU_DEFAULT)) == NULL)
4119 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4120 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4121 who_perm_node_t, who_avl_node), who_perm_compare,
4122 UU_DEFAULT)) == NULL)
4125 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4126 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4127 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4132 static inline void fs_perm_fini(fs_perm_t *);
4133 static inline void who_perm_fini(who_perm_t *);
4136 fs_perm_set_fini(fs_perm_set_t *fspset)
4138 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4140 while (node != NULL) {
4141 fs_perm_node_t *next_node =
4142 uu_list_next(fspset->fsps_list, node);
4143 fs_perm_t *fsperm = &node->fspn_fsperm;
4144 fs_perm_fini(fsperm);
4145 uu_list_remove(fspset->fsps_list, node);
4150 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4151 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4152 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4156 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4159 deleg_perm->dp_who_type = type;
4160 deleg_perm->dp_name = name;
4164 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4165 zfs_deleg_who_type_t type, const char *name)
4167 uu_avl_pool_t *pool;
4168 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4170 bzero(who_perm, sizeof (who_perm_t));
4172 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4173 UU_DEFAULT)) == NULL)
4176 who_perm->who_type = type;
4177 who_perm->who_name = name;
4178 who_perm->who_fsperm = fsperm;
4182 who_perm_fini(who_perm_t *who_perm)
4184 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4186 while (node != NULL) {
4187 deleg_perm_node_t *next_node =
4188 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4190 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4195 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4199 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4201 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4202 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4204 bzero(fsperm, sizeof (fs_perm_t));
4206 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4210 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4214 fsperm->fsp_set = fspset;
4215 fsperm->fsp_name = fsname;
4219 fs_perm_fini(fs_perm_t *fsperm)
4221 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4222 while (node != NULL) {
4223 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4225 who_perm_t *who_perm = &node->who_perm;
4226 who_perm_fini(who_perm);
4227 uu_avl_remove(fsperm->fsp_sc_avl, node);
4232 node = uu_avl_first(fsperm->fsp_uge_avl);
4233 while (node != NULL) {
4234 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4236 who_perm_t *who_perm = &node->who_perm;
4237 who_perm_fini(who_perm);
4238 uu_avl_remove(fsperm->fsp_uge_avl, node);
4243 uu_avl_destroy(fsperm->fsp_sc_avl);
4244 uu_avl_destroy(fsperm->fsp_uge_avl);
4248 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4249 zfs_deleg_who_type_t who_type, const char *name, char locality)
4251 uu_avl_index_t idx = 0;
4253 deleg_perm_node_t *found_node = NULL;
4254 deleg_perm_t *deleg_perm = &node->dpn_perm;
4256 deleg_perm_init(deleg_perm, who_type, name);
4258 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4260 uu_avl_insert(avl, node, idx);
4263 deleg_perm = &node->dpn_perm;
4268 case ZFS_DELEG_LOCAL:
4269 deleg_perm->dp_local = B_TRUE;
4271 case ZFS_DELEG_DESCENDENT:
4272 deleg_perm->dp_descend = B_TRUE;
4277 assert(B_FALSE); /* invalid locality */
4282 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4284 nvpair_t *nvp = NULL;
4285 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4286 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4287 zfs_deleg_who_type_t who_type = who_perm->who_type;
4289 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4290 const char *name = nvpair_name(nvp);
4291 data_type_t type = nvpair_type(nvp);
4292 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4293 deleg_perm_node_t *node =
4294 safe_malloc(sizeof (deleg_perm_node_t));
4296 assert(type == DATA_TYPE_BOOLEAN);
4298 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4299 set_deleg_perm_node(avl, node, who_type, name, locality);
4306 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4308 nvpair_t *nvp = NULL;
4309 fs_perm_set_t *fspset = fsperm->fsp_set;
4311 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4312 nvlist_t *nvl2 = NULL;
4313 const char *name = nvpair_name(nvp);
4314 uu_avl_t *avl = NULL;
4315 uu_avl_pool_t *avl_pool;
4316 zfs_deleg_who_type_t perm_type = name[0];
4317 char perm_locality = name[1];
4318 const char *perm_name = name + 3;
4319 boolean_t is_set = B_TRUE;
4320 who_perm_t *who_perm = NULL;
4322 assert('$' == name[2]);
4324 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4327 switch (perm_type) {
4328 case ZFS_DELEG_CREATE:
4329 case ZFS_DELEG_CREATE_SETS:
4330 case ZFS_DELEG_NAMED_SET:
4331 case ZFS_DELEG_NAMED_SET_SETS:
4332 avl_pool = fspset->fsps_named_set_avl_pool;
4333 avl = fsperm->fsp_sc_avl;
4335 case ZFS_DELEG_USER:
4336 case ZFS_DELEG_USER_SETS:
4337 case ZFS_DELEG_GROUP:
4338 case ZFS_DELEG_GROUP_SETS:
4339 case ZFS_DELEG_EVERYONE:
4340 case ZFS_DELEG_EVERYONE_SETS:
4341 avl_pool = fspset->fsps_who_perm_avl_pool;
4342 avl = fsperm->fsp_uge_avl;
4347 who_perm_node_t *found_node = NULL;
4348 who_perm_node_t *node = safe_malloc(
4349 sizeof (who_perm_node_t));
4350 who_perm = &node->who_perm;
4351 uu_avl_index_t idx = 0;
4353 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4354 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4356 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4358 if (avl == fsperm->fsp_uge_avl) {
4360 struct passwd *p = NULL;
4361 struct group *g = NULL;
4362 const char *nice_name = NULL;
4364 switch (perm_type) {
4365 case ZFS_DELEG_USER_SETS:
4366 case ZFS_DELEG_USER:
4367 rid = atoi(perm_name);
4370 nice_name = p->pw_name;
4372 case ZFS_DELEG_GROUP_SETS:
4373 case ZFS_DELEG_GROUP:
4374 rid = atoi(perm_name);
4377 nice_name = g->gr_name;
4381 if (nice_name != NULL)
4383 node->who_perm.who_ug_name,
4387 uu_avl_insert(avl, node, idx);
4390 who_perm = &node->who_perm;
4394 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4401 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4403 nvpair_t *nvp = NULL;
4404 uu_avl_index_t idx = 0;
4406 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4407 nvlist_t *nvl2 = NULL;
4408 const char *fsname = nvpair_name(nvp);
4409 data_type_t type = nvpair_type(nvp);
4410 fs_perm_t *fsperm = NULL;
4411 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4415 fsperm = &node->fspn_fsperm;
4417 assert(DATA_TYPE_NVLIST == type);
4419 uu_list_node_init(node, &node->fspn_list_node,
4420 fspset->fsps_list_pool);
4422 idx = uu_list_numnodes(fspset->fsps_list);
4423 fs_perm_init(fsperm, fspset, fsname);
4425 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4428 (void) parse_fs_perm(fsperm, nvl2);
4430 uu_list_insert(fspset->fsps_list, node, idx);
4436 static inline const char *
4437 deleg_perm_comment(zfs_deleg_note_t note)
4439 const char *str = "";
4444 case ZFS_DELEG_NOTE_ALLOW:
4445 str = gettext("Must also have the permission that is being"
4446 "\n\t\t\t\tallowed");
4448 case ZFS_DELEG_NOTE_CLONE:
4449 str = gettext("Must also have the 'create' ability and 'mount'"
4450 "\n\t\t\t\tability in the origin file system");
4452 case ZFS_DELEG_NOTE_CREATE:
4453 str = gettext("Must also have the 'mount' ability");
4455 case ZFS_DELEG_NOTE_DESTROY:
4456 str = gettext("Must also have the 'mount' ability");
4458 case ZFS_DELEG_NOTE_DIFF:
4459 str = gettext("Allows lookup of paths within a dataset;"
4460 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4461 "\n\t\t\t\tin order to use zfs diff");
4463 case ZFS_DELEG_NOTE_HOLD:
4464 str = gettext("Allows adding a user hold to a snapshot");
4466 case ZFS_DELEG_NOTE_MOUNT:
4467 str = gettext("Allows mount/umount of ZFS datasets");
4469 case ZFS_DELEG_NOTE_PROMOTE:
4470 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4471 " 'promote' ability in the origin file system");
4473 case ZFS_DELEG_NOTE_RECEIVE:
4474 str = gettext("Must also have the 'mount' and 'create'"
4477 case ZFS_DELEG_NOTE_RELEASE:
4478 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4479 "might destroy the snapshot");
4481 case ZFS_DELEG_NOTE_RENAME:
4482 str = gettext("Must also have the 'mount' and 'create'"
4483 "\n\t\t\t\tability in the new parent");
4485 case ZFS_DELEG_NOTE_ROLLBACK:
4488 case ZFS_DELEG_NOTE_SEND:
4491 case ZFS_DELEG_NOTE_SHARE:
4492 str = gettext("Allows sharing file systems over NFS or SMB"
4493 "\n\t\t\t\tprotocols");
4495 case ZFS_DELEG_NOTE_SNAPSHOT:
4499 * case ZFS_DELEG_NOTE_VSCAN:
4500 * str = gettext("");
4504 case ZFS_DELEG_NOTE_GROUPQUOTA:
4505 str = gettext("Allows accessing any groupquota@... property");
4507 case ZFS_DELEG_NOTE_GROUPUSED:
4508 str = gettext("Allows reading any groupused@... property");
4510 case ZFS_DELEG_NOTE_USERPROP:
4511 str = gettext("Allows changing any user property");
4513 case ZFS_DELEG_NOTE_USERQUOTA:
4514 str = gettext("Allows accessing any userquota@... property");
4516 case ZFS_DELEG_NOTE_USERUSED:
4517 str = gettext("Allows reading any userused@... property");
4535 boolean_t recursive; /* unallow only */
4536 boolean_t prt_usage;
4538 boolean_t prt_perms;
4541 const char *dataset;
4545 prop_cmp(const void *a, const void *b)
4547 const char *str1 = *(const char **)a;
4548 const char *str2 = *(const char **)b;
4549 return (strcmp(str1, str2));
4553 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4555 const char *opt_desc[] = {
4556 "-h", gettext("show this help message and exit"),
4557 "-l", gettext("set permission locally"),
4558 "-d", gettext("set permission for descents"),
4559 "-u", gettext("set permission for user"),
4560 "-g", gettext("set permission for group"),
4561 "-e", gettext("set permission for everyone"),
4562 "-c", gettext("set create time permission"),
4563 "-s", gettext("define permission set"),
4565 "-r", gettext("remove permissions recursively"),
4567 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4568 size_t allow_size = unallow_size - 2;
4569 const char *props[ZFS_NUM_PROPS];
4572 FILE *fp = requested ? stdout : stderr;
4573 zprop_desc_t *pdtbl = zfs_prop_get_table();
4574 const char *fmt = gettext("%-16s %-14s\t%s\n");
4576 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4578 (void) fprintf(fp, gettext("Options:\n"));
4579 for (int i = 0; i < (un ? unallow_size : allow_size); i++) {
4580 const char *opt = opt_desc[i++];
4581 const char *optdsc = opt_desc[i];
4582 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4585 (void) fprintf(fp, gettext("\nThe following permissions are "
4587 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4589 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4590 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4591 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4592 const char *perm_type = deleg_perm_type(perm_note);
4593 const char *perm_comment = deleg_perm_comment(perm_note);
4594 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4597 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4598 zprop_desc_t *pd = &pdtbl[i];
4599 if (pd->pd_visible != B_TRUE)
4602 if (pd->pd_attr == PROP_READONLY)
4605 props[count++] = pd->pd_name;
4607 props[count] = NULL;
4609 qsort(props, count, sizeof (char *), prop_cmp);
4611 for (i = 0; i < count; i++)
4612 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4615 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4617 exit(requested ? 0 : 2);
4620 static inline const char *
4621 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4624 if (un && argc == expected_argc - 1)
4626 else if (argc == expected_argc)
4627 *permsp = argv[argc - 2];
4629 allow_usage(un, B_FALSE,
4630 gettext("wrong number of parameters\n"));
4632 return (argv[argc - 1]);
4636 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4638 int uge_sum = opts->user + opts->group + opts->everyone;
4639 int csuge_sum = opts->create + opts->set + uge_sum;
4640 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4641 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4644 allow_usage(un, B_FALSE,
4645 gettext("-u, -g, and -e are mutually exclusive\n"));
4647 if (opts->prt_usage)
4648 if (argc == 0 && all_sum == 0)
4649 allow_usage(un, B_TRUE, NULL);
4655 allow_usage(un, B_FALSE,
4656 gettext("invalid options combined with -s\n"));
4658 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4659 if (argv[0][0] != '@')
4660 allow_usage(un, B_FALSE,
4661 gettext("invalid set name: missing '@' prefix\n"));
4662 opts->who = argv[0];
4663 } else if (opts->create) {
4664 if (ldcsuge_sum > 1)
4665 allow_usage(un, B_FALSE,
4666 gettext("invalid options combined with -c\n"));
4667 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4668 } else if (opts->everyone) {
4670 allow_usage(un, B_FALSE,
4671 gettext("invalid options combined with -e\n"));
4672 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4673 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4675 opts->everyone = B_TRUE;
4678 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4679 } else if (argc == 1 && !un) {
4680 opts->prt_perms = B_TRUE;
4681 opts->dataset = argv[argc-1];
4683 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4684 opts->who = argv[0];
4687 if (!opts->local && !opts->descend) {
4688 opts->local = B_TRUE;
4689 opts->descend = B_TRUE;
4694 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4695 const char *who, char *perms, nvlist_t *top_nvl)
4698 char ld[2] = { '\0', '\0' };
4699 char who_buf[ZFS_MAXNAMELEN+32];
4702 nvlist_t *base_nvl = NULL;
4703 nvlist_t *set_nvl = NULL;
4706 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4708 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4712 case ZFS_DELEG_NAMED_SET_SETS:
4713 case ZFS_DELEG_NAMED_SET:
4714 set_type = ZFS_DELEG_NAMED_SET_SETS;
4715 base_type = ZFS_DELEG_NAMED_SET;
4716 ld[0] = ZFS_DELEG_NA;
4718 case ZFS_DELEG_CREATE_SETS:
4719 case ZFS_DELEG_CREATE:
4720 set_type = ZFS_DELEG_CREATE_SETS;
4721 base_type = ZFS_DELEG_CREATE;
4722 ld[0] = ZFS_DELEG_NA;
4724 case ZFS_DELEG_USER_SETS:
4725 case ZFS_DELEG_USER:
4726 set_type = ZFS_DELEG_USER_SETS;
4727 base_type = ZFS_DELEG_USER;
4729 ld[0] = ZFS_DELEG_LOCAL;
4731 ld[1] = ZFS_DELEG_DESCENDENT;
4733 case ZFS_DELEG_GROUP_SETS:
4734 case ZFS_DELEG_GROUP:
4735 set_type = ZFS_DELEG_GROUP_SETS;
4736 base_type = ZFS_DELEG_GROUP;
4738 ld[0] = ZFS_DELEG_LOCAL;
4740 ld[1] = ZFS_DELEG_DESCENDENT;
4742 case ZFS_DELEG_EVERYONE_SETS:
4743 case ZFS_DELEG_EVERYONE:
4744 set_type = ZFS_DELEG_EVERYONE_SETS;
4745 base_type = ZFS_DELEG_EVERYONE;
4747 ld[0] = ZFS_DELEG_LOCAL;
4749 ld[1] = ZFS_DELEG_DESCENDENT;
4752 if (perms != NULL) {
4754 char *end = curr + strlen(perms);
4756 while (curr < end) {
4757 char *delim = strchr(curr, ',');
4768 (void) nvlist_add_boolean(nvl, curr);
4774 for (i = 0; i < 2; i++) {
4775 char locality = ld[i];
4779 if (!nvlist_empty(base_nvl)) {
4781 (void) snprintf(who_buf,
4782 sizeof (who_buf), "%c%c$%s",
4783 base_type, locality, who);
4785 (void) snprintf(who_buf,
4786 sizeof (who_buf), "%c%c$",
4787 base_type, locality);
4789 (void) nvlist_add_nvlist(top_nvl, who_buf,
4794 if (!nvlist_empty(set_nvl)) {
4796 (void) snprintf(who_buf,
4797 sizeof (who_buf), "%c%c$%s",
4798 set_type, locality, who);
4800 (void) snprintf(who_buf,
4801 sizeof (who_buf), "%c%c$",
4802 set_type, locality);
4804 (void) nvlist_add_nvlist(top_nvl, who_buf,
4809 for (i = 0; i < 2; i++) {
4810 char locality = ld[i];
4815 (void) snprintf(who_buf, sizeof (who_buf),
4816 "%c%c$%s", base_type, locality, who);
4818 (void) snprintf(who_buf, sizeof (who_buf),
4819 "%c%c$", base_type, locality);
4820 (void) nvlist_add_boolean(top_nvl, who_buf);
4823 (void) snprintf(who_buf, sizeof (who_buf),
4824 "%c%c$%s", set_type, locality, who);
4826 (void) snprintf(who_buf, sizeof (who_buf),
4827 "%c%c$", set_type, locality);
4828 (void) nvlist_add_boolean(top_nvl, who_buf);
4834 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4836 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4840 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4841 opts->descend, opts->who, opts->perms, *nvlp);
4842 } else if (opts->create) {
4843 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4844 opts->descend, NULL, opts->perms, *nvlp);
4845 } else if (opts->everyone) {
4846 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4847 opts->descend, NULL, opts->perms, *nvlp);
4849 char *curr = opts->who;
4850 char *end = curr + strlen(curr);
4852 while (curr < end) {
4854 zfs_deleg_who_type_t who_type;
4856 char *delim = strchr(curr, ',');
4859 struct passwd *p = NULL;
4860 struct group *g = NULL;
4868 rid = (uid_t)strtol(curr, &endch, 0);
4870 who_type = ZFS_DELEG_USER;
4879 (void) snprintf(errbuf, 256, gettext(
4880 "invalid user %s"), curr);
4881 allow_usage(un, B_TRUE, errbuf);
4883 } else if (opts->group) {
4884 who_type = ZFS_DELEG_GROUP;
4893 (void) snprintf(errbuf, 256, gettext(
4894 "invalid group %s"), curr);
4895 allow_usage(un, B_TRUE, errbuf);
4898 if (*endch != '\0') {
4905 if (*endch != '\0') {
4912 who_type = ZFS_DELEG_USER;
4914 } else if (g != NULL) {
4915 who_type = ZFS_DELEG_GROUP;
4918 (void) snprintf(errbuf, 256, gettext(
4919 "invalid user/group %s"), curr);
4920 allow_usage(un, B_TRUE, errbuf);
4924 (void) sprintf(id, "%u", rid);
4927 store_allow_perm(who_type, opts->local,
4928 opts->descend, who, opts->perms, *nvlp);
4937 print_set_creat_perms(uu_avl_t *who_avl)
4939 const char *sc_title[] = {
4940 gettext("Permission sets:\n"),
4941 gettext("Create time permissions:\n"),
4944 const char **title_ptr = sc_title;
4945 who_perm_node_t *who_node = NULL;
4946 int prev_weight = -1;
4948 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4949 who_node = uu_avl_next(who_avl, who_node)) {
4950 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4951 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4952 const char *who_name = who_node->who_perm.who_name;
4953 int weight = who_type2weight(who_type);
4954 boolean_t first = B_TRUE;
4955 deleg_perm_node_t *deleg_node;
4957 if (prev_weight != weight) {
4958 (void) printf(*title_ptr++);
4959 prev_weight = weight;
4962 if (who_name == NULL || strnlen(who_name, 1) == 0)
4963 (void) printf("\t");
4965 (void) printf("\t%s ", who_name);
4967 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4968 deleg_node = uu_avl_next(avl, deleg_node)) {
4971 deleg_node->dpn_perm.dp_name);
4974 (void) printf(",%s",
4975 deleg_node->dpn_perm.dp_name);
4978 (void) printf("\n");
4983 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4986 who_perm_node_t *who_node = NULL;
4987 boolean_t prt_title = B_TRUE;
4988 uu_avl_walk_t *walk;
4990 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4993 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4994 const char *who_name = who_node->who_perm.who_name;
4995 const char *nice_who_name = who_node->who_perm.who_ug_name;
4996 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4997 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4999 deleg_perm_node_t *deleg_node;
5000 boolean_t prt_who = B_TRUE;
5002 for (deleg_node = uu_avl_first(avl);
5004 deleg_node = uu_avl_next(avl, deleg_node)) {
5005 if (local != deleg_node->dpn_perm.dp_local ||
5006 descend != deleg_node->dpn_perm.dp_descend)
5010 const char *who = NULL;
5012 prt_title = B_FALSE;
5013 (void) printf(title);
5017 case ZFS_DELEG_USER_SETS:
5018 case ZFS_DELEG_USER:
5019 who = gettext("user");
5021 who_name = nice_who_name;
5023 case ZFS_DELEG_GROUP_SETS:
5024 case ZFS_DELEG_GROUP:
5025 who = gettext("group");
5027 who_name = nice_who_name;
5029 case ZFS_DELEG_EVERYONE_SETS:
5030 case ZFS_DELEG_EVERYONE:
5031 who = gettext("everyone");
5036 if (who_name == NULL)
5037 (void) printf("\t%s", who);
5039 (void) printf("\t%s %s", who, who_name);
5042 (void) printf("%c%s", delim,
5043 deleg_node->dpn_perm.dp_name);
5048 (void) printf("\n");
5051 uu_avl_walk_end(walk);
5055 print_fs_perms(fs_perm_set_t *fspset)
5057 fs_perm_node_t *node = NULL;
5058 char buf[ZFS_MAXNAMELEN+32];
5059 const char *dsname = buf;
5061 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5062 node = uu_list_next(fspset->fsps_list, node)) {
5063 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5064 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5067 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5068 gettext("---- Permissions on %s "),
5069 node->fspn_fsperm.fsp_name);
5070 (void) printf(dsname);
5071 left = 70 - strlen(buf);
5074 (void) printf("\n");
5076 print_set_creat_perms(sc_avl);
5077 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5078 gettext("Local permissions:\n"));
5079 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5080 gettext("Descendent permissions:\n"));
5081 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5082 gettext("Local+Descendent permissions:\n"));
5086 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5088 struct deleg_perms {
5094 set_deleg_perms(zfs_handle_t *zhp, void *data)
5096 struct deleg_perms *perms = (struct deleg_perms *)data;
5097 zfs_type_t zfs_type = zfs_get_type(zhp);
5099 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5102 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5106 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5109 nvlist_t *perm_nvl = NULL;
5110 nvlist_t *update_perm_nvl = NULL;
5113 struct allow_opts opts = { 0 };
5115 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5118 while ((c = getopt(argc, argv, optstr)) != -1) {
5121 opts.local = B_TRUE;
5124 opts.descend = B_TRUE;
5130 opts.group = B_TRUE;
5133 opts.everyone = B_TRUE;
5139 opts.create = B_TRUE;
5142 opts.recursive = B_TRUE;
5145 (void) fprintf(stderr, gettext("missing argument for "
5146 "'%c' option\n"), optopt);
5150 opts.prt_usage = B_TRUE;
5153 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5162 /* check arguments */
5163 parse_allow_args(argc, argv, un, &opts);
5165 /* try to open the dataset */
5166 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5167 ZFS_TYPE_VOLUME)) == NULL) {
5168 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5173 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5176 fs_perm_set_init(&fs_perm_set);
5177 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5178 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5183 print_fs_perms(&fs_perm_set);
5185 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5186 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5189 if (un && opts.recursive) {
5190 struct deleg_perms data = { un, update_perm_nvl };
5191 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5200 nvlist_free(perm_nvl);
5201 if (update_perm_nvl != NULL)
5202 nvlist_free(update_perm_nvl);
5204 fs_perm_set_fini(&fs_perm_set);
5212 zfs_do_allow(int argc, char **argv)
5214 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5218 zfs_do_unallow(int argc, char **argv)
5220 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5224 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5229 boolean_t recursive = B_FALSE;
5230 const char *opts = holding ? "rt" : "r";
5234 while ((c = getopt(argc, argv, opts)) != -1) {
5240 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5249 /* check number of arguments */
5257 if (holding && tag[0] == '.') {
5258 /* tags starting with '.' are reserved for libzfs */
5259 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5263 for (i = 0; i < argc; ++i) {
5265 char parent[ZFS_MAXNAMELEN];
5267 char *path = argv[i];
5269 delim = strchr(path, '@');
5270 if (delim == NULL) {
5271 (void) fprintf(stderr,
5272 gettext("'%s' is not a snapshot\n"), path);
5276 (void) strncpy(parent, path, delim - path);
5277 parent[delim - path] = '\0';
5279 zhp = zfs_open(g_zfs, parent,
5280 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5286 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5289 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5295 return (errors != 0);
5299 * zfs hold [-r] [-t] <tag> <snap> ...
5301 * -r Recursively hold
5303 * Apply a user-hold with the given tag to the list of snapshots.
5306 zfs_do_hold(int argc, char **argv)
5308 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5312 * zfs release [-r] <tag> <snap> ...
5314 * -r Recursively release
5316 * Release a user-hold with the given tag from the list of snapshots.
5319 zfs_do_release(int argc, char **argv)
5321 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5324 typedef struct holds_cbdata {
5325 boolean_t cb_recursive;
5326 const char *cb_snapname;
5328 size_t cb_max_namelen;
5329 size_t cb_max_taglen;
5332 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5333 #define DATETIME_BUF_LEN (32)
5338 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5341 nvpair_t *nvp = NULL;
5342 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5346 for (i = 0; i < 3; i++) {
5347 col = gettext(hdr_cols[i]);
5349 (void) printf("%-*s ", i ? tagwidth : nwidth,
5352 (void) printf("%s\n", col);
5356 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5357 char *zname = nvpair_name(nvp);
5359 nvpair_t *nvp2 = NULL;
5360 (void) nvpair_value_nvlist(nvp, &nvl2);
5361 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5362 char tsbuf[DATETIME_BUF_LEN];
5363 char *tagname = nvpair_name(nvp2);
5367 char sep = scripted ? '\t' : ' ';
5368 size_t sepnum = scripted ? 1 : 2;
5370 (void) nvpair_value_uint64(nvp2, &val);
5372 (void) localtime_r(&time, &t);
5373 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5374 gettext(STRFTIME_FMT_STR), &t);
5376 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5377 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5383 * Generic callback function to list a dataset or snapshot.
5386 holds_callback(zfs_handle_t *zhp, void *data)
5388 holds_cbdata_t *cbp = data;
5389 nvlist_t *top_nvl = *cbp->cb_nvlp;
5390 nvlist_t *nvl = NULL;
5391 nvpair_t *nvp = NULL;
5392 const char *zname = zfs_get_name(zhp);
5393 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5395 if (cbp->cb_recursive) {
5396 const char *snapname;
5397 char *delim = strchr(zname, '@');
5401 snapname = delim + 1;
5402 if (strcmp(cbp->cb_snapname, snapname))
5406 if (zfs_get_holds(zhp, &nvl) != 0)
5409 if (znamelen > cbp->cb_max_namelen)
5410 cbp->cb_max_namelen = znamelen;
5412 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5413 const char *tag = nvpair_name(nvp);
5414 size_t taglen = strnlen(tag, MAXNAMELEN);
5415 if (taglen > cbp->cb_max_taglen)
5416 cbp->cb_max_taglen = taglen;
5419 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5423 * zfs holds [-r] <snap> ...
5425 * -r Recursively hold
5428 zfs_do_holds(int argc, char **argv)
5433 boolean_t scripted = B_FALSE;
5434 boolean_t recursive = B_FALSE;
5435 const char *opts = "rH";
5438 int types = ZFS_TYPE_SNAPSHOT;
5439 holds_cbdata_t cb = { 0 };
5446 while ((c = getopt(argc, argv, opts)) != -1) {
5455 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5462 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5463 flags |= ZFS_ITER_RECURSE;
5469 /* check number of arguments */
5473 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5476 for (i = 0; i < argc; ++i) {
5477 char *snapshot = argv[i];
5479 const char *snapname;
5481 delim = strchr(snapshot, '@');
5482 if (delim == NULL) {
5483 (void) fprintf(stderr,
5484 gettext("'%s' is not a snapshot\n"), snapshot);
5488 snapname = delim + 1;
5490 snapshot[delim - snapshot] = '\0';
5492 cb.cb_recursive = recursive;
5493 cb.cb_snapname = snapname;
5497 * 1. collect holds data, set format options
5499 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5500 holds_callback, &cb);
5506 * 2. print holds data
5508 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5510 if (nvlist_empty(nvl))
5511 (void) printf(gettext("no datasets available\n"));
5515 return (0 != errors);
5518 #define CHECK_SPINNER 30
5519 #define SPINNER_TIME 3 /* seconds */
5520 #define MOUNT_TIME 5 /* seconds */
5523 get_one_dataset(zfs_handle_t *zhp, void *data)
5525 static char *spin[] = { "-", "\\", "|", "/" };
5526 static int spinval = 0;
5527 static int spincheck = 0;
5528 static time_t last_spin_time = (time_t)0;
5529 get_all_cb_t *cbp = data;
5530 zfs_type_t type = zfs_get_type(zhp);
5532 if (cbp->cb_verbose) {
5533 if (--spincheck < 0) {
5534 time_t now = time(NULL);
5535 if (last_spin_time + SPINNER_TIME < now) {
5536 update_progress(spin[spinval++ % 4]);
5537 last_spin_time = now;
5539 spincheck = CHECK_SPINNER;
5544 * Interate over any nested datasets.
5546 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5552 * Skip any datasets whose type does not match.
5554 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5558 libzfs_add_handle(cbp, zhp);
5559 assert(cbp->cb_used <= cbp->cb_alloc);
5565 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5567 get_all_cb_t cb = { 0 };
5568 cb.cb_verbose = verbose;
5569 cb.cb_getone = get_one_dataset;
5572 set_progress_header(gettext("Reading ZFS config"));
5573 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5575 *dslist = cb.cb_handles;
5576 *count = cb.cb_used;
5579 finish_progress(gettext("done."));
5583 * Generic callback for sharing or mounting filesystems. Because the code is so
5584 * similar, we have a common function with an extra parameter to determine which
5585 * mode we are using.
5587 #define OP_SHARE 0x1
5588 #define OP_MOUNT 0x2
5591 * Share or mount a dataset.
5594 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5595 boolean_t explicit, const char *options)
5597 char mountpoint[ZFS_MAXPROPLEN];
5598 char shareopts[ZFS_MAXPROPLEN];
5599 char smbshareopts[ZFS_MAXPROPLEN];
5600 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5602 uint64_t zoned, canmount;
5603 boolean_t shared_nfs, shared_smb;
5605 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5608 * Check to make sure we can mount/share this dataset. If we
5609 * are in the global zone and the filesystem is exported to a
5610 * local zone, or if we are in a local zone and the
5611 * filesystem is not exported, then it is an error.
5613 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5615 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5619 (void) fprintf(stderr, gettext("cannot %s '%s': "
5620 "dataset is exported to a local zone\n"), cmdname,
5624 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5628 (void) fprintf(stderr, gettext("cannot %s '%s': "
5629 "permission denied\n"), cmdname,
5635 * Ignore any filesystems which don't apply to us. This
5636 * includes those with a legacy mountpoint, or those with
5637 * legacy share options.
5639 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5640 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5641 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5642 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5643 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5644 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5646 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5647 strcmp(smbshareopts, "off") == 0) {
5651 (void) fprintf(stderr, gettext("cannot share '%s': "
5652 "legacy share\n"), zfs_get_name(zhp));
5653 (void) fprintf(stderr, gettext("use share(1M) to "
5654 "share this filesystem, or set "
5655 "sharenfs property on\n"));
5660 * We cannot share or mount legacy filesystems. If the
5661 * shareopts is non-legacy but the mountpoint is legacy, we
5662 * treat it as a legacy share.
5664 if (strcmp(mountpoint, "legacy") == 0) {
5668 (void) fprintf(stderr, gettext("cannot %s '%s': "
5669 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5670 (void) fprintf(stderr, gettext("use %s(1M) to "
5671 "%s this filesystem\n"), cmdname, cmdname);
5675 if (strcmp(mountpoint, "none") == 0) {
5679 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5680 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5685 * canmount explicit outcome
5686 * on no pass through
5687 * on yes pass through
5689 * off yes display error, return 1
5690 * noauto no return 0
5691 * noauto yes pass through
5693 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5694 if (canmount == ZFS_CANMOUNT_OFF) {
5698 (void) fprintf(stderr, gettext("cannot %s '%s': "
5699 "'canmount' property is set to 'off'\n"), cmdname,
5702 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5707 * At this point, we have verified that the mountpoint and/or
5708 * shareopts are appropriate for auto management. If the
5709 * filesystem is already mounted or shared, return (failing
5710 * for explicit requests); otherwise mount or share the
5716 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5717 shared_smb = zfs_is_shared_smb(zhp, NULL);
5719 if (shared_nfs && shared_smb ||
5720 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5721 strcmp(smbshareopts, "off") == 0) ||
5722 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5723 strcmp(shareopts, "off") == 0)) {
5727 (void) fprintf(stderr, gettext("cannot share "
5728 "'%s': filesystem already shared\n"),
5733 if (!zfs_is_mounted(zhp, NULL) &&
5734 zfs_mount(zhp, NULL, 0) != 0)
5737 if (protocol == NULL) {
5738 if (zfs_shareall(zhp) != 0)
5740 } else if (strcmp(protocol, "nfs") == 0) {
5741 if (zfs_share_nfs(zhp))
5743 } else if (strcmp(protocol, "smb") == 0) {
5744 if (zfs_share_smb(zhp))
5747 (void) fprintf(stderr, gettext("cannot share "
5748 "'%s': invalid share type '%s' "
5750 zfs_get_name(zhp), protocol);
5757 if (options == NULL)
5758 mnt.mnt_mntopts = "";
5760 mnt.mnt_mntopts = (char *)options;
5762 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5763 zfs_is_mounted(zhp, NULL)) {
5767 (void) fprintf(stderr, gettext("cannot mount "
5768 "'%s': filesystem already mounted\n"),
5773 if (zfs_mount(zhp, options, flags) != 0)
5782 * Reports progress in the form "(current/total)". Not thread-safe.
5785 report_mount_progress(int current, int total)
5787 static time_t last_progress_time = 0;
5788 time_t now = time(NULL);
5791 /* report 1..n instead of 0..n-1 */
5794 /* display header if we're here for the first time */
5796 set_progress_header(gettext("Mounting ZFS filesystems"));
5797 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5798 /* too soon to report again */
5802 last_progress_time = now;
5804 (void) sprintf(info, "(%d/%d)", current, total);
5806 if (current == total)
5807 finish_progress(info);
5809 update_progress(info);
5813 append_options(char *mntopts, char *newopts)
5815 int len = strlen(mntopts);
5817 /* original length plus new string to append plus 1 for the comma */
5818 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5819 (void) fprintf(stderr, gettext("the opts argument for "
5820 "'%c' option is too long (more than %d chars)\n"),
5821 "-o", MNT_LINE_MAX);
5826 mntopts[len++] = ',';
5828 (void) strcpy(&mntopts[len], newopts);
5832 share_mount(int op, int argc, char **argv)
5835 boolean_t verbose = B_FALSE;
5837 char *options = NULL;
5841 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5851 if (*optarg == '\0') {
5852 (void) fprintf(stderr, gettext("empty mount "
5853 "options (-o) specified\n"));
5857 if (options == NULL)
5858 options = safe_malloc(MNT_LINE_MAX + 1);
5860 /* option validation is done later */
5861 append_options(options, optarg);
5865 flags |= MS_OVERLAY;
5868 (void) fprintf(stderr, gettext("missing argument for "
5869 "'%c' option\n"), optopt);
5873 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5882 /* check number of arguments */
5884 zfs_handle_t **dslist = NULL;
5885 size_t i, count = 0;
5886 char *protocol = NULL;
5888 if (op == OP_SHARE && argc > 0) {
5889 if (strcmp(argv[0], "nfs") != 0 &&
5890 strcmp(argv[0], "smb") != 0) {
5891 (void) fprintf(stderr, gettext("share type "
5892 "must be 'nfs' or 'smb'\n"));
5901 (void) fprintf(stderr, gettext("too many arguments\n"));
5905 start_progress_timer();
5906 get_all_datasets(&dslist, &count, verbose);
5911 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5913 for (i = 0; i < count; i++) {
5915 report_mount_progress(i, count);
5917 if (share_mount_one(dslist[i], op, flags, protocol,
5918 B_FALSE, options) != 0)
5920 zfs_close(dslist[i]);
5924 } else if (argc == 0) {
5925 struct mnttab entry;
5927 if ((op == OP_SHARE) || (options != NULL)) {
5928 (void) fprintf(stderr, gettext("missing filesystem "
5929 "argument (specify -a for all)\n"));
5934 * When mount is given no arguments, go through /etc/mnttab and
5935 * display any active ZFS mounts. We hide any snapshots, since
5936 * they are controlled automatically.
5938 rewind(mnttab_file);
5939 while (getmntent(mnttab_file, &entry) == 0) {
5940 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5941 strchr(entry.mnt_special, '@') != NULL)
5944 (void) printf("%-30s %s\n", entry.mnt_special,
5952 (void) fprintf(stderr,
5953 gettext("too many arguments\n"));
5957 if ((zhp = zfs_open(g_zfs, argv[0],
5958 ZFS_TYPE_FILESYSTEM)) == NULL) {
5961 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5971 * zfs mount -a [nfs]
5972 * zfs mount filesystem
5974 * Mount all filesystems, or mount the given filesystem.
5977 zfs_do_mount(int argc, char **argv)
5979 return (share_mount(OP_MOUNT, argc, argv));
5983 * zfs share -a [nfs | smb]
5984 * zfs share filesystem
5986 * Share all filesystems, or share the given filesystem.
5989 zfs_do_share(int argc, char **argv)
5991 return (share_mount(OP_SHARE, argc, argv));
5994 typedef struct unshare_unmount_node {
5995 zfs_handle_t *un_zhp;
5997 uu_avl_node_t un_avlnode;
5998 } unshare_unmount_node_t;
6002 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6004 const unshare_unmount_node_t *l = larg;
6005 const unshare_unmount_node_t *r = rarg;
6007 return (strcmp(l->un_mountp, r->un_mountp));
6011 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6012 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6013 * and unmount it appropriately.
6016 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6020 struct stat64 statbuf;
6021 struct extmnttab entry;
6022 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6026 * Search for the path in /etc/mnttab. Rather than looking for the
6027 * specific path, which can be fooled by non-standard paths (i.e. ".."
6028 * or "//"), we stat() the path and search for the corresponding
6029 * (major,minor) device pair.
6031 if (stat64(path, &statbuf) != 0) {
6032 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6033 cmdname, path, strerror(errno));
6036 path_inode = statbuf.st_ino;
6039 * Search for the given (major,minor) pair in the mount table.
6041 rewind(mnttab_file);
6042 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6043 if (entry.mnt_major == major(statbuf.st_dev) &&
6044 entry.mnt_minor == minor(statbuf.st_dev))
6048 if (op == OP_SHARE) {
6049 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6050 "currently mounted\n"), cmdname, path);
6053 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6055 if ((ret = umount2(path, flags)) != 0)
6056 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6061 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6062 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6063 "filesystem\n"), cmdname, path);
6067 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6068 ZFS_TYPE_FILESYSTEM)) == NULL)
6072 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6073 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6074 cmdname, path, strerror(errno));
6076 } else if (statbuf.st_ino != path_inode) {
6077 (void) fprintf(stderr, gettext("cannot "
6078 "%s '%s': not a mountpoint\n"), cmdname, path);
6082 if (op == OP_SHARE) {
6083 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6084 char smbshare_prop[ZFS_MAXPROPLEN];
6086 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6087 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6088 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6089 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6091 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6092 strcmp(smbshare_prop, "off") == 0) {
6093 (void) fprintf(stderr, gettext("cannot unshare "
6094 "'%s': legacy share\n"), path);
6095 (void) fprintf(stderr, gettext("use "
6096 "unshare(1M) to unshare this filesystem\n"));
6097 } else if (!zfs_is_shared(zhp)) {
6098 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6099 "not currently shared\n"), path);
6101 ret = zfs_unshareall_bypath(zhp, path);
6104 char mtpt_prop[ZFS_MAXPROPLEN];
6106 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6107 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6110 ret = zfs_unmount(zhp, NULL, flags);
6111 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6112 (void) fprintf(stderr, gettext("cannot unmount "
6113 "'%s': legacy mountpoint\n"),
6115 (void) fprintf(stderr, gettext("use umount(1M) "
6116 "to unmount this filesystem\n"));
6118 ret = zfs_unmountall(zhp, flags);
6129 * Generic callback for unsharing or unmounting a filesystem.
6132 unshare_unmount(int op, int argc, char **argv)
6139 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6140 char sharesmb[ZFS_MAXPROPLEN];
6143 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6152 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6163 * We could make use of zfs_for_each() to walk all datasets in
6164 * the system, but this would be very inefficient, especially
6165 * since we would have to linearly search /etc/mnttab for each
6166 * one. Instead, do one pass through /etc/mnttab looking for
6167 * zfs entries and call zfs_unmount() for each one.
6169 * Things get a little tricky if the administrator has created
6170 * mountpoints beneath other ZFS filesystems. In this case, we
6171 * have to unmount the deepest filesystems first. To accomplish
6172 * this, we place all the mountpoints in an AVL tree sorted by
6173 * the special type (dataset name), and walk the result in
6174 * reverse to make sure to get any snapshots first.
6176 struct mnttab entry;
6177 uu_avl_pool_t *pool;
6179 unshare_unmount_node_t *node;
6181 uu_avl_walk_t *walk;
6184 (void) fprintf(stderr, gettext("too many arguments\n"));
6188 if (((pool = uu_avl_pool_create("unmount_pool",
6189 sizeof (unshare_unmount_node_t),
6190 offsetof(unshare_unmount_node_t, un_avlnode),
6191 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6192 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6195 rewind(mnttab_file);
6196 while (getmntent(mnttab_file, &entry) == 0) {
6198 /* ignore non-ZFS entries */
6199 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6202 /* ignore snapshots */
6203 if (strchr(entry.mnt_special, '@') != NULL)
6206 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6207 ZFS_TYPE_FILESYSTEM)) == NULL) {
6214 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6216 sizeof (nfs_mnt_prop),
6217 NULL, NULL, 0, B_FALSE) == 0);
6218 if (strcmp(nfs_mnt_prop, "off") != 0)
6220 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6222 sizeof (nfs_mnt_prop),
6223 NULL, NULL, 0, B_FALSE) == 0);
6224 if (strcmp(nfs_mnt_prop, "off") == 0)
6228 /* Ignore legacy mounts */
6229 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6231 sizeof (nfs_mnt_prop),
6232 NULL, NULL, 0, B_FALSE) == 0);
6233 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6235 /* Ignore canmount=noauto mounts */
6236 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6237 ZFS_CANMOUNT_NOAUTO)
6243 node = safe_malloc(sizeof (unshare_unmount_node_t));
6245 node->un_mountp = safe_strdup(entry.mnt_mountp);
6247 uu_avl_node_init(node, &node->un_avlnode, pool);
6249 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6250 uu_avl_insert(tree, node, idx);
6252 zfs_close(node->un_zhp);
6253 free(node->un_mountp);
6259 * Walk the AVL tree in reverse, unmounting each filesystem and
6260 * removing it from the AVL tree in the process.
6262 if ((walk = uu_avl_walk_start(tree,
6263 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6266 while ((node = uu_avl_walk_next(walk)) != NULL) {
6267 uu_avl_remove(tree, node);
6271 if (zfs_unshareall_bypath(node->un_zhp,
6272 node->un_mountp) != 0)
6277 if (zfs_unmount(node->un_zhp,
6278 node->un_mountp, flags) != 0)
6283 zfs_close(node->un_zhp);
6284 free(node->un_mountp);
6288 uu_avl_walk_end(walk);
6289 uu_avl_destroy(tree);
6290 uu_avl_pool_destroy(pool);
6295 (void) fprintf(stderr,
6296 gettext("missing filesystem argument\n"));
6298 (void) fprintf(stderr,
6299 gettext("too many arguments\n"));
6304 * We have an argument, but it may be a full path or a ZFS
6305 * filesystem. Pass full paths off to unmount_path() (shared by
6306 * manual_unmount), otherwise open the filesystem and pass to
6309 if (argv[0][0] == '/')
6310 return (unshare_unmount_path(op, argv[0],
6313 if ((zhp = zfs_open(g_zfs, argv[0],
6314 ZFS_TYPE_FILESYSTEM)) == NULL)
6317 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6318 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6319 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6320 NULL, 0, B_FALSE) == 0);
6324 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6326 sizeof (nfs_mnt_prop),
6327 NULL, NULL, 0, B_FALSE) == 0);
6328 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6329 sharesmb, sizeof (sharesmb), NULL, NULL,
6332 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6333 strcmp(sharesmb, "off") == 0) {
6334 (void) fprintf(stderr, gettext("cannot "
6335 "unshare '%s': legacy share\n"),
6337 (void) fprintf(stderr, gettext("use "
6338 "unshare(1M) to unshare this "
6341 } else if (!zfs_is_shared(zhp)) {
6342 (void) fprintf(stderr, gettext("cannot "
6343 "unshare '%s': not currently "
6344 "shared\n"), zfs_get_name(zhp));
6346 } else if (zfs_unshareall(zhp) != 0) {
6352 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6353 (void) fprintf(stderr, gettext("cannot "
6354 "unmount '%s': legacy "
6355 "mountpoint\n"), zfs_get_name(zhp));
6356 (void) fprintf(stderr, gettext("use "
6357 "umount(1M) to unmount this "
6360 } else if (!zfs_is_mounted(zhp, NULL)) {
6361 (void) fprintf(stderr, gettext("cannot "
6362 "unmount '%s': not currently "
6366 } else if (zfs_unmountall(zhp, flags) != 0) {
6380 * zfs unmount filesystem
6382 * Unmount all filesystems, or a specific ZFS filesystem.
6385 zfs_do_unmount(int argc, char **argv)
6387 return (unshare_unmount(OP_MOUNT, argc, argv));
6392 * zfs unshare filesystem
6394 * Unshare all filesystems, or a specific ZFS filesystem.
6397 zfs_do_unshare(int argc, char **argv)
6399 return (unshare_unmount(OP_SHARE, argc, argv));
6403 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6404 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6407 manual_mount(int argc, char **argv)
6410 char mountpoint[ZFS_MAXPROPLEN];
6411 char mntopts[MNT_LINE_MAX] = { '\0' };
6415 char *dataset, *path;
6418 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6421 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6424 flags |= MS_OVERLAY;
6427 flags |= MS_NOMNTTAB;
6430 (void) fprintf(stderr, gettext("missing argument for "
6431 "'%c' option\n"), optopt);
6435 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6437 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6446 /* check that we only have two arguments */
6449 (void) fprintf(stderr, gettext("missing dataset "
6452 (void) fprintf(stderr,
6453 gettext("missing mountpoint argument\n"));
6455 (void) fprintf(stderr, gettext("too many arguments\n"));
6456 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6463 /* try to open the dataset */
6464 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6467 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6468 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6470 /* check for legacy mountpoint and complain appropriately */
6472 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6473 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
6474 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6475 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6480 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6481 "mounted using 'mount -F zfs'\n"), dataset);
6482 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6483 "instead.\n"), path);
6484 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6485 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6486 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6495 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6496 * unmounts of non-legacy filesystems, as this is the dominant administrative
6500 manual_unmount(int argc, char **argv)
6506 while ((c = getopt(argc, argv, "f")) != -1) {
6512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6514 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6523 /* check arguments */
6526 (void) fprintf(stderr, gettext("missing path "
6529 (void) fprintf(stderr, gettext("too many arguments\n"));
6530 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6534 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6538 find_command_idx(char *command, int *idx)
6542 for (i = 0; i < NCOMMAND; i++) {
6543 if (command_table[i].name == NULL)
6546 if (strcmp(command, command_table[i].name) == 0) {
6555 zfs_do_diff(int argc, char **argv)
6559 char *tosnap = NULL;
6560 char *fromsnap = NULL;
6565 while ((c = getopt(argc, argv, "FHt")) != -1) {
6568 flags |= ZFS_DIFF_CLASSIFY;
6571 flags |= ZFS_DIFF_PARSEABLE;
6574 flags |= ZFS_DIFF_TIMESTAMP;
6577 (void) fprintf(stderr,
6578 gettext("invalid option '%c'\n"), optopt);
6587 (void) fprintf(stderr,
6588 gettext("must provide at least one snapshot name\n"));
6593 (void) fprintf(stderr, gettext("too many arguments\n"));
6598 tosnap = (argc == 2) ? argv[1] : NULL;
6601 if (*fromsnap != '@')
6602 copy = strdup(fromsnap);
6604 copy = strdup(tosnap);
6608 if (atp = strchr(copy, '@'))
6611 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6617 * Ignore SIGPIPE so that the library can give us
6618 * information on any failure
6620 (void) sigignore(SIGPIPE);
6622 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6630 * zfs bookmark <fs@snap> <fs#bmark>
6632 * Creates a bookmark with the given name from the given snapshot.
6635 zfs_do_bookmark(int argc, char **argv)
6637 char snapname[ZFS_MAXNAMELEN];
6644 while ((c = getopt(argc, argv, "")) != -1) {
6647 (void) fprintf(stderr,
6648 gettext("invalid option '%c'\n"), optopt);
6656 /* check number of arguments */
6658 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6662 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6666 if (strchr(argv[1], '#') == NULL) {
6667 (void) fprintf(stderr,
6668 gettext("invalid bookmark name '%s' -- "
6669 "must contain a '#'\n"), argv[1]);
6673 if (argv[0][0] == '@') {
6675 * Snapshot name begins with @.
6676 * Default to same fs as bookmark.
6678 (void) strncpy(snapname, argv[1], sizeof (snapname));
6679 *strchr(snapname, '#') = '\0';
6680 (void) strlcat(snapname, argv[0], sizeof (snapname));
6682 (void) strncpy(snapname, argv[0], sizeof (snapname));
6684 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6690 nvl = fnvlist_alloc();
6691 fnvlist_add_string(nvl, argv[1], snapname);
6692 ret = lzc_bookmark(nvl, NULL);
6696 const char *err_msg;
6699 (void) snprintf(errbuf, sizeof (errbuf),
6700 dgettext(TEXT_DOMAIN,
6701 "cannot create bookmark '%s'"), argv[1]);
6705 err_msg = "bookmark is in a different pool";
6708 err_msg = "bookmark exists";
6711 err_msg = "invalid argument";
6714 err_msg = "bookmark feature not enabled";
6717 err_msg = "out of space";
6720 err_msg = "unknown error";
6723 (void) fprintf(stderr, "%s: %s\n", errbuf,
6724 dgettext(TEXT_DOMAIN, err_msg));
6735 main(int argc, char **argv)
6742 (void) setlocale(LC_ALL, "");
6743 (void) textdomain(TEXT_DOMAIN);
6747 if ((g_zfs = libzfs_init()) == NULL) {
6748 (void) fprintf(stderr, gettext("internal error: failed to "
6749 "initialize ZFS library\n"));
6753 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6755 libzfs_print_on_error(g_zfs, B_TRUE);
6757 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6758 (void) fprintf(stderr, gettext("internal error: unable to "
6759 "open %s\n"), MNTTAB);
6764 * This command also doubles as the /etc/fs mount and unmount program.
6765 * Determine if we should take this behavior based on argv[0].
6767 progname = basename(argv[0]);
6768 if (strcmp(progname, "mount") == 0) {
6769 ret = manual_mount(argc, argv);
6770 } else if (strcmp(progname, "umount") == 0) {
6771 ret = manual_unmount(argc, argv);
6774 * Make sure the user has specified some command.
6777 (void) fprintf(stderr, gettext("missing command\n"));
6784 * The 'umount' command is an alias for 'unmount'
6786 if (strcmp(cmdname, "umount") == 0)
6787 cmdname = "unmount";
6790 * The 'recv' command is an alias for 'receive'
6792 if (strcmp(cmdname, "recv") == 0)
6793 cmdname = "receive";
6796 * The 'snap' command is an alias for 'snapshot'
6798 if (strcmp(cmdname, "snap") == 0)
6799 cmdname = "snapshot";
6804 if (strcmp(cmdname, "-?") == 0)
6808 * Run the appropriate command.
6810 libzfs_mnttab_cache(g_zfs, B_TRUE);
6811 if (find_command_idx(cmdname, &i) == 0) {
6812 current_command = &command_table[i];
6813 ret = command_table[i].func(argc - 1, argv + 1);
6814 } else if (strchr(cmdname, '=') != NULL) {
6815 verify(find_command_idx("set", &i) == 0);
6816 current_command = &command_table[i];
6817 ret = command_table[i].func(argc, argv);
6819 (void) fprintf(stderr, gettext("unrecognized "
6820 "command '%s'\n"), cmdname);
6823 libzfs_mnttab_cache(g_zfs, B_FALSE);
6826 (void) fclose(mnttab_file);
6828 if (ret == 0 && log_history)
6829 (void) zpool_log_history(g_zfs, history_str);
6834 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6835 * for the purposes of running ::findleaks.
6837 if (getenv("ZFS_ABORT") != NULL) {
6838 (void) printf("dumping core by request\n");