1884 Empty "used" field for zfs *space commands
[unleashed.git] / usr / src / cmd / zfs / zfs_main.c
blob28a808ab53456a70c39eb9c1f002620b40fefe20
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright 2012 Milan Jurik. All rights reserved.
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
30 #include <assert.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <libgen.h>
34 #include <libintl.h>
35 #include <libuutil.h>
36 #include <libnvpair.h>
37 #include <locale.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <strings.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <zone.h>
45 #include <grp.h>
46 #include <pwd.h>
47 #include <signal.h>
48 #include <sys/list.h>
49 #include <sys/mkdev.h>
50 #include <sys/mntent.h>
51 #include <sys/mnttab.h>
52 #include <sys/mount.h>
53 #include <sys/stat.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/types.h>
56 #include <time.h>
58 #include <libzfs.h>
59 #include <libzfs_core.h>
60 #include <zfs_prop.h>
61 #include <zfs_deleg.h>
62 #include <libuutil.h>
63 #include <aclutils.h>
64 #include <directory.h>
66 #include "zfs_iter.h"
67 #include "zfs_util.h"
68 #include "zfs_comutil.h"
70 libzfs_handle_t *g_zfs;
72 static FILE *mnttab_file;
73 static char history_str[HIS_MAX_RECORD_LEN];
74 static boolean_t log_history = B_TRUE;
76 static int zfs_do_clone(int argc, char **argv);
77 static int zfs_do_create(int argc, char **argv);
78 static int zfs_do_destroy(int argc, char **argv);
79 static int zfs_do_get(int argc, char **argv);
80 static int zfs_do_inherit(int argc, char **argv);
81 static int zfs_do_list(int argc, char **argv);
82 static int zfs_do_mount(int argc, char **argv);
83 static int zfs_do_rename(int argc, char **argv);
84 static int zfs_do_rollback(int argc, char **argv);
85 static int zfs_do_set(int argc, char **argv);
86 static int zfs_do_upgrade(int argc, char **argv);
87 static int zfs_do_snapshot(int argc, char **argv);
88 static int zfs_do_unmount(int argc, char **argv);
89 static int zfs_do_share(int argc, char **argv);
90 static int zfs_do_unshare(int argc, char **argv);
91 static int zfs_do_send(int argc, char **argv);
92 static int zfs_do_receive(int argc, char **argv);
93 static int zfs_do_promote(int argc, char **argv);
94 static int zfs_do_userspace(int argc, char **argv);
95 static int zfs_do_allow(int argc, char **argv);
96 static int zfs_do_unallow(int argc, char **argv);
97 static int zfs_do_hold(int argc, char **argv);
98 static int zfs_do_holds(int argc, char **argv);
99 static int zfs_do_release(int argc, char **argv);
100 static int zfs_do_diff(int argc, char **argv);
103 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
106 #ifdef DEBUG
107 const char *
108 _umem_debug_init(void)
110 return ("default,verbose"); /* $UMEM_DEBUG setting */
113 const char *
114 _umem_logging_init(void)
116 return ("fail,contents"); /* $UMEM_LOGGING setting */
118 #endif
120 typedef enum {
121 HELP_CLONE,
122 HELP_CREATE,
123 HELP_DESTROY,
124 HELP_GET,
125 HELP_INHERIT,
126 HELP_UPGRADE,
127 HELP_LIST,
128 HELP_MOUNT,
129 HELP_PROMOTE,
130 HELP_RECEIVE,
131 HELP_RENAME,
132 HELP_ROLLBACK,
133 HELP_SEND,
134 HELP_SET,
135 HELP_SHARE,
136 HELP_SNAPSHOT,
137 HELP_UNMOUNT,
138 HELP_UNSHARE,
139 HELP_ALLOW,
140 HELP_UNALLOW,
141 HELP_USERSPACE,
142 HELP_GROUPSPACE,
143 HELP_HOLD,
144 HELP_HOLDS,
145 HELP_RELEASE,
146 HELP_DIFF,
147 } zfs_help_t;
149 typedef struct zfs_command {
150 const char *name;
151 int (*func)(int argc, char **argv);
152 zfs_help_t usage;
153 } zfs_command_t;
156 * Master command table. Each ZFS command has a name, associated function, and
157 * usage message. The usage messages need to be internationalized, so we have
158 * to have a function to return the usage message based on a command index.
160 * These commands are organized according to how they are displayed in the usage
161 * message. An empty command (one with a NULL name) indicates an empty line in
162 * the generic usage message.
164 static zfs_command_t command_table[] = {
165 { "create", zfs_do_create, HELP_CREATE },
166 { "destroy", zfs_do_destroy, HELP_DESTROY },
167 { NULL },
168 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
169 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
170 { "clone", zfs_do_clone, HELP_CLONE },
171 { "promote", zfs_do_promote, HELP_PROMOTE },
172 { "rename", zfs_do_rename, HELP_RENAME },
173 { NULL },
174 { "list", zfs_do_list, HELP_LIST },
175 { NULL },
176 { "set", zfs_do_set, HELP_SET },
177 { "get", zfs_do_get, HELP_GET },
178 { "inherit", zfs_do_inherit, HELP_INHERIT },
179 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
180 { "userspace", zfs_do_userspace, HELP_USERSPACE },
181 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
182 { NULL },
183 { "mount", zfs_do_mount, HELP_MOUNT },
184 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
185 { "share", zfs_do_share, HELP_SHARE },
186 { "unshare", zfs_do_unshare, HELP_UNSHARE },
187 { NULL },
188 { "send", zfs_do_send, HELP_SEND },
189 { "receive", zfs_do_receive, HELP_RECEIVE },
190 { NULL },
191 { "allow", zfs_do_allow, HELP_ALLOW },
192 { NULL },
193 { "unallow", zfs_do_unallow, HELP_UNALLOW },
194 { NULL },
195 { "hold", zfs_do_hold, HELP_HOLD },
196 { "holds", zfs_do_holds, HELP_HOLDS },
197 { "release", zfs_do_release, HELP_RELEASE },
198 { "diff", zfs_do_diff, HELP_DIFF },
201 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
203 zfs_command_t *current_command;
205 static const char *
206 get_usage(zfs_help_t idx)
208 switch (idx) {
209 case HELP_CLONE:
210 return (gettext("\tclone [-p] [-o property=value] ... "
211 "<snapshot> <filesystem|volume>\n"));
212 case HELP_CREATE:
213 return (gettext("\tcreate [-p] [-o property=value] ... "
214 "<filesystem>\n"
215 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
216 "-V <size> <volume>\n"));
217 case HELP_DESTROY:
218 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
219 "\tdestroy [-dnpRrv] "
220 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
221 case HELP_GET:
222 return (gettext("\tget [-rHp] [-d max] "
223 "[-o \"all\" | field[,...]] [-t type[,...]] "
224 "[-s source[,...]]\n"
225 "\t <\"all\" | property[,...]> "
226 "[filesystem|volume|snapshot] ...\n"));
227 case HELP_INHERIT:
228 return (gettext("\tinherit [-rS] <property> "
229 "<filesystem|volume|snapshot> ...\n"));
230 case HELP_UPGRADE:
231 return (gettext("\tupgrade [-v]\n"
232 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
233 case HELP_LIST:
234 return (gettext("\tlist [-rH][-d max] "
235 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
236 "\t [-S property] ... "
237 "[filesystem|volume|snapshot] ...\n"));
238 case HELP_MOUNT:
239 return (gettext("\tmount\n"
240 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
241 case HELP_PROMOTE:
242 return (gettext("\tpromote <clone-filesystem>\n"));
243 case HELP_RECEIVE:
244 return (gettext("\treceive [-vnFu] <filesystem|volume|"
245 "snapshot>\n"
246 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
247 case HELP_RENAME:
248 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
249 "<filesystem|volume|snapshot>\n"
250 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
251 "\trename -r <snapshot> <snapshot>"));
252 case HELP_ROLLBACK:
253 return (gettext("\trollback [-rRf] <snapshot>\n"));
254 case HELP_SEND:
255 return (gettext("\tsend [-DnPpRrv] [-[iI] snapshot] "
256 "<snapshot>\n"));
257 case HELP_SET:
258 return (gettext("\tset <property=value> "
259 "<filesystem|volume|snapshot> ...\n"));
260 case HELP_SHARE:
261 return (gettext("\tshare <-a | filesystem>\n"));
262 case HELP_SNAPSHOT:
263 return (gettext("\tsnapshot [-r] [-o property=value] ... "
264 "<filesystem@snapname|volume@snapname> ...\n"));
265 case HELP_UNMOUNT:
266 return (gettext("\tunmount [-f] "
267 "<-a | filesystem|mountpoint>\n"));
268 case HELP_UNSHARE:
269 return (gettext("\tunshare "
270 "<-a | filesystem|mountpoint>\n"));
271 case HELP_ALLOW:
272 return (gettext("\tallow <filesystem|volume>\n"
273 "\tallow [-ldug] "
274 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
275 "\t <filesystem|volume>\n"
276 "\tallow [-ld] -e <perm|@setname>[,...] "
277 "<filesystem|volume>\n"
278 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
279 "\tallow -s @setname <perm|@setname>[,...] "
280 "<filesystem|volume>\n"));
281 case HELP_UNALLOW:
282 return (gettext("\tunallow [-rldug] "
283 "<\"everyone\"|user|group>[,...]\n"
284 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
285 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
286 "<filesystem|volume>\n"
287 "\tunallow [-r] -c [<perm|@setname>[,...]] "
288 "<filesystem|volume>\n"
289 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
290 "<filesystem|volume>\n"));
291 case HELP_USERSPACE:
292 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
293 "[-s field] ...\n\t[-S field] ... "
294 "[-t type[,...]] <filesystem|snapshot>\n"));
295 case HELP_GROUPSPACE:
296 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
297 "[-s field] ...\n\t[-S field] ... "
298 "[-t type[,...]] <filesystem|snapshot>\n"));
299 case HELP_HOLD:
300 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
301 case HELP_HOLDS:
302 return (gettext("\tholds [-r] <snapshot> ...\n"));
303 case HELP_RELEASE:
304 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
305 case HELP_DIFF:
306 return (gettext("\tdiff [-FHt] <snapshot> "
307 "[snapshot|filesystem]\n"));
310 abort();
311 /* NOTREACHED */
314 void
315 nomem(void)
317 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
318 exit(1);
322 * Utility function to guarantee malloc() success.
325 void *
326 safe_malloc(size_t size)
328 void *data;
330 if ((data = calloc(1, size)) == NULL)
331 nomem();
333 return (data);
336 static char *
337 safe_strdup(char *str)
339 char *dupstr = strdup(str);
341 if (dupstr == NULL)
342 nomem();
344 return (dupstr);
348 * Callback routine that will print out information for each of
349 * the properties.
351 static int
352 usage_prop_cb(int prop, void *cb)
354 FILE *fp = cb;
356 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
358 if (zfs_prop_readonly(prop))
359 (void) fprintf(fp, " NO ");
360 else
361 (void) fprintf(fp, "YES ");
363 if (zfs_prop_inheritable(prop))
364 (void) fprintf(fp, " YES ");
365 else
366 (void) fprintf(fp, " NO ");
368 if (zfs_prop_values(prop) == NULL)
369 (void) fprintf(fp, "-\n");
370 else
371 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
373 return (ZPROP_CONT);
377 * Display usage message. If we're inside a command, display only the usage for
378 * that command. Otherwise, iterate over the entire command table and display
379 * a complete usage message.
381 static void
382 usage(boolean_t requested)
384 int i;
385 boolean_t show_properties = B_FALSE;
386 FILE *fp = requested ? stdout : stderr;
388 if (current_command == NULL) {
390 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
391 (void) fprintf(fp,
392 gettext("where 'command' is one of the following:\n\n"));
394 for (i = 0; i < NCOMMAND; i++) {
395 if (command_table[i].name == NULL)
396 (void) fprintf(fp, "\n");
397 else
398 (void) fprintf(fp, "%s",
399 get_usage(command_table[i].usage));
402 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
403 "pool/[dataset/]*dataset[@name]\n"));
404 } else {
405 (void) fprintf(fp, gettext("usage:\n"));
406 (void) fprintf(fp, "%s", get_usage(current_command->usage));
409 if (current_command != NULL &&
410 (strcmp(current_command->name, "set") == 0 ||
411 strcmp(current_command->name, "get") == 0 ||
412 strcmp(current_command->name, "inherit") == 0 ||
413 strcmp(current_command->name, "list") == 0))
414 show_properties = B_TRUE;
416 if (show_properties) {
417 (void) fprintf(fp,
418 gettext("\nThe following properties are supported:\n"));
420 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
421 "PROPERTY", "EDIT", "INHERIT", "VALUES");
423 /* Iterate over all properties */
424 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
425 ZFS_TYPE_DATASET);
427 (void) fprintf(fp, "\t%-15s ", "userused@...");
428 (void) fprintf(fp, " NO NO <size>\n");
429 (void) fprintf(fp, "\t%-15s ", "groupused@...");
430 (void) fprintf(fp, " NO NO <size>\n");
431 (void) fprintf(fp, "\t%-15s ", "userquota@...");
432 (void) fprintf(fp, "YES NO <size> | none\n");
433 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
434 (void) fprintf(fp, "YES NO <size> | none\n");
435 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
436 (void) fprintf(fp, " NO NO <size>\n");
438 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
439 "with standard units such as K, M, G, etc.\n"));
440 (void) fprintf(fp, gettext("\nUser-defined properties can "
441 "be specified by using a name containing a colon (:).\n"));
442 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
443 "properties must be appended with\n"
444 "a user or group specifier of one of these forms:\n"
445 " POSIX name (eg: \"matt\")\n"
446 " POSIX id (eg: \"126829\")\n"
447 " SMB name@domain (eg: \"matt@sun\")\n"
448 " SMB SID (eg: \"S-1-234-567-89\")\n"));
449 } else {
450 (void) fprintf(fp,
451 gettext("\nFor the property list, run: %s\n"),
452 "zfs set|get");
453 (void) fprintf(fp,
454 gettext("\nFor the delegated permission list, run: %s\n"),
455 "zfs allow|unallow");
459 * See comments at end of main().
461 if (getenv("ZFS_ABORT") != NULL) {
462 (void) printf("dumping core by request\n");
463 abort();
466 exit(requested ? 0 : 2);
469 static int
470 parseprop(nvlist_t *props)
472 char *propname = optarg;
473 char *propval, *strval;
475 if ((propval = strchr(propname, '=')) == NULL) {
476 (void) fprintf(stderr, gettext("missing "
477 "'=' for -o option\n"));
478 return (-1);
480 *propval = '\0';
481 propval++;
482 if (nvlist_lookup_string(props, propname, &strval) == 0) {
483 (void) fprintf(stderr, gettext("property '%s' "
484 "specified multiple times\n"), propname);
485 return (-1);
487 if (nvlist_add_string(props, propname, propval) != 0)
488 nomem();
489 return (0);
492 static int
493 parse_depth(char *opt, int *flags)
495 char *tmp;
496 int depth;
498 depth = (int)strtol(opt, &tmp, 0);
499 if (*tmp) {
500 (void) fprintf(stderr,
501 gettext("%s is not an integer\n"), optarg);
502 usage(B_FALSE);
504 if (depth < 0) {
505 (void) fprintf(stderr,
506 gettext("Depth can not be negative.\n"));
507 usage(B_FALSE);
509 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
510 return (depth);
513 #define PROGRESS_DELAY 2 /* seconds */
515 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";
516 static time_t pt_begin;
517 static char *pt_header = NULL;
518 static boolean_t pt_shown;
520 static void
521 start_progress_timer(void)
523 pt_begin = time(NULL) + PROGRESS_DELAY;
524 pt_shown = B_FALSE;
527 static void
528 set_progress_header(char *header)
530 assert(pt_header == NULL);
531 pt_header = safe_strdup(header);
532 if (pt_shown) {
533 (void) printf("%s: ", header);
534 (void) fflush(stdout);
538 static void
539 update_progress(char *update)
541 if (!pt_shown && time(NULL) > pt_begin) {
542 int len = strlen(update);
544 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
545 pt_reverse);
546 (void) fflush(stdout);
547 pt_shown = B_TRUE;
548 } else if (pt_shown) {
549 int len = strlen(update);
551 (void) printf("%s%*.*s", update, len, len, pt_reverse);
552 (void) fflush(stdout);
556 static void
557 finish_progress(char *done)
559 if (pt_shown) {
560 (void) printf("%s\n", done);
561 (void) fflush(stdout);
563 free(pt_header);
564 pt_header = NULL;
567 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
569 * Given an existing dataset, create a writable copy whose initial contents
570 * are the same as the source. The newly created dataset maintains a
571 * dependency on the original; the original cannot be destroyed so long as
572 * the clone exists.
574 * The '-p' flag creates all the non-existing ancestors of the target first.
576 static int
577 zfs_do_clone(int argc, char **argv)
579 zfs_handle_t *zhp = NULL;
580 boolean_t parents = B_FALSE;
581 nvlist_t *props;
582 int ret = 0;
583 int c;
585 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
586 nomem();
588 /* check options */
589 while ((c = getopt(argc, argv, "o:p")) != -1) {
590 switch (c) {
591 case 'o':
592 if (parseprop(props))
593 return (1);
594 break;
595 case 'p':
596 parents = B_TRUE;
597 break;
598 case '?':
599 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
600 optopt);
601 goto usage;
605 argc -= optind;
606 argv += optind;
608 /* check number of arguments */
609 if (argc < 1) {
610 (void) fprintf(stderr, gettext("missing source dataset "
611 "argument\n"));
612 goto usage;
614 if (argc < 2) {
615 (void) fprintf(stderr, gettext("missing target dataset "
616 "argument\n"));
617 goto usage;
619 if (argc > 2) {
620 (void) fprintf(stderr, gettext("too many arguments\n"));
621 goto usage;
624 /* open the source dataset */
625 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
626 return (1);
628 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
629 ZFS_TYPE_VOLUME)) {
631 * Now create the ancestors of the target dataset. If the
632 * target already exists and '-p' option was used we should not
633 * complain.
635 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
636 ZFS_TYPE_VOLUME))
637 return (0);
638 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
639 return (1);
642 /* pass to libzfs */
643 ret = zfs_clone(zhp, argv[1], props);
645 /* create the mountpoint if necessary */
646 if (ret == 0) {
647 zfs_handle_t *clone;
649 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
650 if (clone != NULL) {
651 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
652 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
653 ret = zfs_share(clone);
654 zfs_close(clone);
658 zfs_close(zhp);
659 nvlist_free(props);
661 return (!!ret);
663 usage:
664 if (zhp)
665 zfs_close(zhp);
666 nvlist_free(props);
667 usage(B_FALSE);
668 return (-1);
672 * zfs create [-p] [-o prop=value] ... fs
673 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
675 * Create a new dataset. This command can be used to create filesystems
676 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
677 * For volumes, the user must specify a size to be used.
679 * The '-s' flag applies only to volumes, and indicates that we should not try
680 * to set the reservation for this volume. By default we set a reservation
681 * equal to the size for any volume. For pools with SPA_VERSION >=
682 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
684 * The '-p' flag creates all the non-existing ancestors of the target first.
686 static int
687 zfs_do_create(int argc, char **argv)
689 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
690 zfs_handle_t *zhp = NULL;
691 uint64_t volsize;
692 int c;
693 boolean_t noreserve = B_FALSE;
694 boolean_t bflag = B_FALSE;
695 boolean_t parents = B_FALSE;
696 int ret = 1;
697 nvlist_t *props;
698 uint64_t intval;
699 int canmount = ZFS_CANMOUNT_OFF;
701 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
702 nomem();
704 /* check options */
705 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
706 switch (c) {
707 case 'V':
708 type = ZFS_TYPE_VOLUME;
709 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
710 (void) fprintf(stderr, gettext("bad volume "
711 "size '%s': %s\n"), optarg,
712 libzfs_error_description(g_zfs));
713 goto error;
716 if (nvlist_add_uint64(props,
717 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
718 nomem();
719 volsize = intval;
720 break;
721 case 'p':
722 parents = B_TRUE;
723 break;
724 case 'b':
725 bflag = B_TRUE;
726 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
727 (void) fprintf(stderr, gettext("bad volume "
728 "block size '%s': %s\n"), optarg,
729 libzfs_error_description(g_zfs));
730 goto error;
733 if (nvlist_add_uint64(props,
734 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
735 intval) != 0)
736 nomem();
737 break;
738 case 'o':
739 if (parseprop(props))
740 goto error;
741 break;
742 case 's':
743 noreserve = B_TRUE;
744 break;
745 case ':':
746 (void) fprintf(stderr, gettext("missing size "
747 "argument\n"));
748 goto badusage;
749 case '?':
750 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
751 optopt);
752 goto badusage;
756 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
757 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
758 "used when creating a volume\n"));
759 goto badusage;
762 argc -= optind;
763 argv += optind;
765 /* check number of arguments */
766 if (argc == 0) {
767 (void) fprintf(stderr, gettext("missing %s argument\n"),
768 zfs_type_to_name(type));
769 goto badusage;
771 if (argc > 1) {
772 (void) fprintf(stderr, gettext("too many arguments\n"));
773 goto badusage;
776 if (type == ZFS_TYPE_VOLUME && !noreserve) {
777 zpool_handle_t *zpool_handle;
778 uint64_t spa_version;
779 char *p;
780 zfs_prop_t resv_prop;
781 char *strval;
783 if (p = strchr(argv[0], '/'))
784 *p = '\0';
785 zpool_handle = zpool_open(g_zfs, argv[0]);
786 if (p != NULL)
787 *p = '/';
788 if (zpool_handle == NULL)
789 goto error;
790 spa_version = zpool_get_prop_int(zpool_handle,
791 ZPOOL_PROP_VERSION, NULL);
792 zpool_close(zpool_handle);
793 if (spa_version >= SPA_VERSION_REFRESERVATION)
794 resv_prop = ZFS_PROP_REFRESERVATION;
795 else
796 resv_prop = ZFS_PROP_RESERVATION;
797 volsize = zvol_volsize_to_reservation(volsize, props);
799 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
800 &strval) != 0) {
801 if (nvlist_add_uint64(props,
802 zfs_prop_to_name(resv_prop), volsize) != 0) {
803 nvlist_free(props);
804 nomem();
809 if (parents && zfs_name_valid(argv[0], type)) {
811 * Now create the ancestors of target dataset. If the target
812 * already exists and '-p' option was used we should not
813 * complain.
815 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
816 ret = 0;
817 goto error;
819 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
820 goto error;
823 /* pass to libzfs */
824 if (zfs_create(g_zfs, argv[0], type, props) != 0)
825 goto error;
827 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
828 goto error;
830 ret = 0;
832 * if the user doesn't want the dataset automatically mounted,
833 * then skip the mount/share step
835 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
836 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
839 * Mount and/or share the new filesystem as appropriate. We provide a
840 * verbose error message to let the user know that their filesystem was
841 * in fact created, even if we failed to mount or share it.
843 if (canmount == ZFS_CANMOUNT_ON) {
844 if (zfs_mount(zhp, NULL, 0) != 0) {
845 (void) fprintf(stderr, gettext("filesystem "
846 "successfully created, but not mounted\n"));
847 ret = 1;
848 } else if (zfs_share(zhp) != 0) {
849 (void) fprintf(stderr, gettext("filesystem "
850 "successfully created, but not shared\n"));
851 ret = 1;
855 error:
856 if (zhp)
857 zfs_close(zhp);
858 nvlist_free(props);
859 return (ret);
860 badusage:
861 nvlist_free(props);
862 usage(B_FALSE);
863 return (2);
867 * zfs destroy [-rRf] <fs, vol>
868 * zfs destroy [-rRd] <snap>
870 * -r Recursively destroy all children
871 * -R Recursively destroy all dependents, including clones
872 * -f Force unmounting of any dependents
873 * -d If we can't destroy now, mark for deferred destruction
875 * Destroys the given dataset. By default, it will unmount any filesystems,
876 * and refuse to destroy a dataset that has any dependents. A dependent can
877 * either be a child, or a clone of a child.
879 typedef struct destroy_cbdata {
880 boolean_t cb_first;
881 boolean_t cb_force;
882 boolean_t cb_recurse;
883 boolean_t cb_error;
884 boolean_t cb_doclones;
885 zfs_handle_t *cb_target;
886 boolean_t cb_defer_destroy;
887 boolean_t cb_verbose;
888 boolean_t cb_parsable;
889 boolean_t cb_dryrun;
890 nvlist_t *cb_nvl;
892 /* first snap in contiguous run */
893 char *cb_firstsnap;
894 /* previous snap in contiguous run */
895 char *cb_prevsnap;
896 int64_t cb_snapused;
897 char *cb_snapspec;
898 } destroy_cbdata_t;
901 * Check for any dependents based on the '-r' or '-R' flags.
903 static int
904 destroy_check_dependent(zfs_handle_t *zhp, void *data)
906 destroy_cbdata_t *cbp = data;
907 const char *tname = zfs_get_name(cbp->cb_target);
908 const char *name = zfs_get_name(zhp);
910 if (strncmp(tname, name, strlen(tname)) == 0 &&
911 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
913 * This is a direct descendant, not a clone somewhere else in
914 * the hierarchy.
916 if (cbp->cb_recurse)
917 goto out;
919 if (cbp->cb_first) {
920 (void) fprintf(stderr, gettext("cannot destroy '%s': "
921 "%s has children\n"),
922 zfs_get_name(cbp->cb_target),
923 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
924 (void) fprintf(stderr, gettext("use '-r' to destroy "
925 "the following datasets:\n"));
926 cbp->cb_first = B_FALSE;
927 cbp->cb_error = B_TRUE;
930 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
931 } else {
933 * This is a clone. We only want to report this if the '-r'
934 * wasn't specified, or the target is a snapshot.
936 if (!cbp->cb_recurse &&
937 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
938 goto out;
940 if (cbp->cb_first) {
941 (void) fprintf(stderr, gettext("cannot destroy '%s': "
942 "%s has dependent clones\n"),
943 zfs_get_name(cbp->cb_target),
944 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
945 (void) fprintf(stderr, gettext("use '-R' to destroy "
946 "the following datasets:\n"));
947 cbp->cb_first = B_FALSE;
948 cbp->cb_error = B_TRUE;
949 cbp->cb_dryrun = B_TRUE;
952 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
955 out:
956 zfs_close(zhp);
957 return (0);
960 static int
961 destroy_callback(zfs_handle_t *zhp, void *data)
963 destroy_cbdata_t *cb = data;
964 const char *name = zfs_get_name(zhp);
966 if (cb->cb_verbose) {
967 if (cb->cb_parsable) {
968 (void) printf("destroy\t%s\n", name);
969 } else if (cb->cb_dryrun) {
970 (void) printf(gettext("would destroy %s\n"),
971 name);
972 } else {
973 (void) printf(gettext("will destroy %s\n"),
974 name);
979 * Ignore pools (which we've already flagged as an error before getting
980 * here).
982 if (strchr(zfs_get_name(zhp), '/') == NULL &&
983 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
984 zfs_close(zhp);
985 return (0);
988 if (!cb->cb_dryrun) {
989 if (zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
990 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
991 zfs_close(zhp);
992 return (-1);
996 zfs_close(zhp);
997 return (0);
1000 static int
1001 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1003 destroy_cbdata_t *cb = arg;
1004 const char *name = zfs_get_name(zhp);
1005 int err = 0;
1007 if (nvlist_exists(cb->cb_nvl, name)) {
1008 if (cb->cb_firstsnap == NULL)
1009 cb->cb_firstsnap = strdup(name);
1010 if (cb->cb_prevsnap != NULL)
1011 free(cb->cb_prevsnap);
1012 /* this snap continues the current range */
1013 cb->cb_prevsnap = strdup(name);
1014 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1015 nomem();
1016 if (cb->cb_verbose) {
1017 if (cb->cb_parsable) {
1018 (void) printf("destroy\t%s\n", name);
1019 } else if (cb->cb_dryrun) {
1020 (void) printf(gettext("would destroy %s\n"),
1021 name);
1022 } else {
1023 (void) printf(gettext("will destroy %s\n"),
1024 name);
1027 } else if (cb->cb_firstsnap != NULL) {
1028 /* end of this range */
1029 uint64_t used = 0;
1030 err = lzc_snaprange_space(cb->cb_firstsnap,
1031 cb->cb_prevsnap, &used);
1032 cb->cb_snapused += used;
1033 free(cb->cb_firstsnap);
1034 cb->cb_firstsnap = NULL;
1035 free(cb->cb_prevsnap);
1036 cb->cb_prevsnap = NULL;
1038 zfs_close(zhp);
1039 return (err);
1042 static int
1043 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1045 int err = 0;
1046 assert(cb->cb_firstsnap == NULL);
1047 assert(cb->cb_prevsnap == NULL);
1048 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1049 if (cb->cb_firstsnap != NULL) {
1050 uint64_t used = 0;
1051 if (err == 0) {
1052 err = lzc_snaprange_space(cb->cb_firstsnap,
1053 cb->cb_prevsnap, &used);
1055 cb->cb_snapused += used;
1056 free(cb->cb_firstsnap);
1057 cb->cb_firstsnap = NULL;
1058 free(cb->cb_prevsnap);
1059 cb->cb_prevsnap = NULL;
1061 return (err);
1064 static int
1065 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1067 destroy_cbdata_t *cb = arg;
1068 int err = 0;
1070 /* Check for clones. */
1071 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1072 cb->cb_target = zhp;
1073 cb->cb_first = B_TRUE;
1074 err = zfs_iter_dependents(zhp, B_TRUE,
1075 destroy_check_dependent, cb);
1078 if (err == 0) {
1079 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1080 nomem();
1082 zfs_close(zhp);
1083 return (err);
1086 static int
1087 gather_snapshots(zfs_handle_t *zhp, void *arg)
1089 destroy_cbdata_t *cb = arg;
1090 int err = 0;
1092 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1093 if (err == ENOENT)
1094 err = 0;
1095 if (err != 0)
1096 goto out;
1098 if (cb->cb_verbose) {
1099 err = destroy_print_snapshots(zhp, cb);
1100 if (err != 0)
1101 goto out;
1104 if (cb->cb_recurse)
1105 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1107 out:
1108 zfs_close(zhp);
1109 return (err);
1112 static int
1113 destroy_clones(destroy_cbdata_t *cb)
1115 nvpair_t *pair;
1116 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1117 pair != NULL;
1118 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1119 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1120 ZFS_TYPE_SNAPSHOT);
1121 if (zhp != NULL) {
1122 boolean_t defer = cb->cb_defer_destroy;
1123 int err = 0;
1126 * We can't defer destroy non-snapshots, so set it to
1127 * false while destroying the clones.
1129 cb->cb_defer_destroy = B_FALSE;
1130 err = zfs_iter_dependents(zhp, B_FALSE,
1131 destroy_callback, cb);
1132 cb->cb_defer_destroy = defer;
1133 zfs_close(zhp);
1134 if (err != 0)
1135 return (err);
1138 return (0);
1141 static int
1142 zfs_do_destroy(int argc, char **argv)
1144 destroy_cbdata_t cb = { 0 };
1145 int c;
1146 zfs_handle_t *zhp;
1147 char *at;
1148 zfs_type_t type = ZFS_TYPE_DATASET;
1150 /* check options */
1151 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1152 switch (c) {
1153 case 'v':
1154 cb.cb_verbose = B_TRUE;
1155 break;
1156 case 'p':
1157 cb.cb_verbose = B_TRUE;
1158 cb.cb_parsable = B_TRUE;
1159 break;
1160 case 'n':
1161 cb.cb_dryrun = B_TRUE;
1162 break;
1163 case 'd':
1164 cb.cb_defer_destroy = B_TRUE;
1165 type = ZFS_TYPE_SNAPSHOT;
1166 break;
1167 case 'f':
1168 cb.cb_force = B_TRUE;
1169 break;
1170 case 'r':
1171 cb.cb_recurse = B_TRUE;
1172 break;
1173 case 'R':
1174 cb.cb_recurse = B_TRUE;
1175 cb.cb_doclones = B_TRUE;
1176 break;
1177 case '?':
1178 default:
1179 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1180 optopt);
1181 usage(B_FALSE);
1185 argc -= optind;
1186 argv += optind;
1188 /* check number of arguments */
1189 if (argc == 0) {
1190 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1191 usage(B_FALSE);
1193 if (argc > 1) {
1194 (void) fprintf(stderr, gettext("too many arguments\n"));
1195 usage(B_FALSE);
1198 at = strchr(argv[0], '@');
1199 if (at != NULL) {
1200 int err = 0;
1202 /* Build the list of snaps to destroy in cb_nvl. */
1203 if (nvlist_alloc(&cb.cb_nvl, NV_UNIQUE_NAME, 0) != 0)
1204 nomem();
1206 *at = '\0';
1207 zhp = zfs_open(g_zfs, argv[0],
1208 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1209 if (zhp == NULL)
1210 return (1);
1212 cb.cb_snapspec = at + 1;
1213 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1214 cb.cb_error) {
1215 zfs_close(zhp);
1216 nvlist_free(cb.cb_nvl);
1217 return (1);
1220 if (nvlist_empty(cb.cb_nvl)) {
1221 (void) fprintf(stderr, gettext("could not find any "
1222 "snapshots to destroy; check snapshot names.\n"));
1223 zfs_close(zhp);
1224 nvlist_free(cb.cb_nvl);
1225 return (1);
1228 if (cb.cb_verbose) {
1229 char buf[16];
1230 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1231 if (cb.cb_parsable) {
1232 (void) printf("reclaim\t%llu\n",
1233 cb.cb_snapused);
1234 } else if (cb.cb_dryrun) {
1235 (void) printf(gettext("would reclaim %s\n"),
1236 buf);
1237 } else {
1238 (void) printf(gettext("will reclaim %s\n"),
1239 buf);
1243 if (!cb.cb_dryrun) {
1244 if (cb.cb_doclones)
1245 err = destroy_clones(&cb);
1246 if (err == 0) {
1247 err = zfs_destroy_snaps_nvl(zhp, cb.cb_nvl,
1248 cb.cb_defer_destroy);
1252 zfs_close(zhp);
1253 nvlist_free(cb.cb_nvl);
1254 if (err != 0)
1255 return (1);
1256 } else {
1257 /* Open the given dataset */
1258 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1259 return (1);
1261 cb.cb_target = zhp;
1264 * Perform an explicit check for pools before going any further.
1266 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1267 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1268 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1269 "operation does not apply to pools\n"),
1270 zfs_get_name(zhp));
1271 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1272 "%s' to destroy all datasets in the pool\n"),
1273 zfs_get_name(zhp));
1274 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1275 "to destroy the pool itself\n"), zfs_get_name(zhp));
1276 zfs_close(zhp);
1277 return (1);
1281 * Check for any dependents and/or clones.
1283 cb.cb_first = B_TRUE;
1284 if (!cb.cb_doclones &&
1285 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1286 &cb) != 0) {
1287 zfs_close(zhp);
1288 return (1);
1291 if (cb.cb_error) {
1292 zfs_close(zhp);
1293 return (1);
1296 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1297 &cb) != 0) {
1298 zfs_close(zhp);
1299 return (1);
1303 * Do the real thing. The callback will close the
1304 * handle regardless of whether it succeeds or not.
1306 if (destroy_callback(zhp, &cb) != 0)
1307 return (1);
1310 return (0);
1313 static boolean_t
1314 is_recvd_column(zprop_get_cbdata_t *cbp)
1316 int i;
1317 zfs_get_column_t col;
1319 for (i = 0; i < ZFS_GET_NCOLS &&
1320 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1321 if (col == GET_COL_RECVD)
1322 return (B_TRUE);
1323 return (B_FALSE);
1327 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1328 * < all | property[,property]... > < fs | snap | vol > ...
1330 * -r recurse over any child datasets
1331 * -H scripted mode. Headers are stripped, and fields are separated
1332 * by tabs instead of spaces.
1333 * -o Set of fields to display. One of "name,property,value,
1334 * received,source". Default is "name,property,value,source".
1335 * "all" is an alias for all five.
1336 * -s Set of sources to allow. One of
1337 * "local,default,inherited,received,temporary,none". Default is
1338 * all six.
1339 * -p Display values in parsable (literal) format.
1341 * Prints properties for the given datasets. The user can control which
1342 * columns to display as well as which property types to allow.
1346 * Invoked to display the properties for a single dataset.
1348 static int
1349 get_callback(zfs_handle_t *zhp, void *data)
1351 char buf[ZFS_MAXPROPLEN];
1352 char rbuf[ZFS_MAXPROPLEN];
1353 zprop_source_t sourcetype;
1354 char source[ZFS_MAXNAMELEN];
1355 zprop_get_cbdata_t *cbp = data;
1356 nvlist_t *user_props = zfs_get_user_props(zhp);
1357 zprop_list_t *pl = cbp->cb_proplist;
1358 nvlist_t *propval;
1359 char *strval;
1360 char *sourceval;
1361 boolean_t received = is_recvd_column(cbp);
1363 for (; pl != NULL; pl = pl->pl_next) {
1364 char *recvdval = NULL;
1366 * Skip the special fake placeholder. This will also skip over
1367 * the name property when 'all' is specified.
1369 if (pl->pl_prop == ZFS_PROP_NAME &&
1370 pl == cbp->cb_proplist)
1371 continue;
1373 if (pl->pl_prop != ZPROP_INVAL) {
1374 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1375 sizeof (buf), &sourcetype, source,
1376 sizeof (source),
1377 cbp->cb_literal) != 0) {
1378 if (pl->pl_all)
1379 continue;
1380 if (!zfs_prop_valid_for_type(pl->pl_prop,
1381 ZFS_TYPE_DATASET)) {
1382 (void) fprintf(stderr,
1383 gettext("No such property '%s'\n"),
1384 zfs_prop_to_name(pl->pl_prop));
1385 continue;
1387 sourcetype = ZPROP_SRC_NONE;
1388 (void) strlcpy(buf, "-", sizeof (buf));
1391 if (received && (zfs_prop_get_recvd(zhp,
1392 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1393 cbp->cb_literal) == 0))
1394 recvdval = rbuf;
1396 zprop_print_one_property(zfs_get_name(zhp), cbp,
1397 zfs_prop_to_name(pl->pl_prop),
1398 buf, sourcetype, source, recvdval);
1399 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1400 sourcetype = ZPROP_SRC_LOCAL;
1402 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1403 buf, sizeof (buf), cbp->cb_literal) != 0) {
1404 sourcetype = ZPROP_SRC_NONE;
1405 (void) strlcpy(buf, "-", sizeof (buf));
1408 zprop_print_one_property(zfs_get_name(zhp), cbp,
1409 pl->pl_user_prop, buf, sourcetype, source, NULL);
1410 } else if (zfs_prop_written(pl->pl_user_prop)) {
1411 sourcetype = ZPROP_SRC_LOCAL;
1413 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1414 buf, sizeof (buf), cbp->cb_literal) != 0) {
1415 sourcetype = ZPROP_SRC_NONE;
1416 (void) strlcpy(buf, "-", sizeof (buf));
1419 zprop_print_one_property(zfs_get_name(zhp), cbp,
1420 pl->pl_user_prop, buf, sourcetype, source, NULL);
1421 } else {
1422 if (nvlist_lookup_nvlist(user_props,
1423 pl->pl_user_prop, &propval) != 0) {
1424 if (pl->pl_all)
1425 continue;
1426 sourcetype = ZPROP_SRC_NONE;
1427 strval = "-";
1428 } else {
1429 verify(nvlist_lookup_string(propval,
1430 ZPROP_VALUE, &strval) == 0);
1431 verify(nvlist_lookup_string(propval,
1432 ZPROP_SOURCE, &sourceval) == 0);
1434 if (strcmp(sourceval,
1435 zfs_get_name(zhp)) == 0) {
1436 sourcetype = ZPROP_SRC_LOCAL;
1437 } else if (strcmp(sourceval,
1438 ZPROP_SOURCE_VAL_RECVD) == 0) {
1439 sourcetype = ZPROP_SRC_RECEIVED;
1440 } else {
1441 sourcetype = ZPROP_SRC_INHERITED;
1442 (void) strlcpy(source,
1443 sourceval, sizeof (source));
1447 if (received && (zfs_prop_get_recvd(zhp,
1448 pl->pl_user_prop, rbuf, sizeof (rbuf),
1449 cbp->cb_literal) == 0))
1450 recvdval = rbuf;
1452 zprop_print_one_property(zfs_get_name(zhp), cbp,
1453 pl->pl_user_prop, strval, sourcetype,
1454 source, recvdval);
1458 return (0);
1461 static int
1462 zfs_do_get(int argc, char **argv)
1464 zprop_get_cbdata_t cb = { 0 };
1465 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1466 int types = ZFS_TYPE_DATASET;
1467 char *value, *fields;
1468 int ret = 0;
1469 int limit = 0;
1470 zprop_list_t fake_name = { 0 };
1473 * Set up default columns and sources.
1475 cb.cb_sources = ZPROP_SRC_ALL;
1476 cb.cb_columns[0] = GET_COL_NAME;
1477 cb.cb_columns[1] = GET_COL_PROPERTY;
1478 cb.cb_columns[2] = GET_COL_VALUE;
1479 cb.cb_columns[3] = GET_COL_SOURCE;
1480 cb.cb_type = ZFS_TYPE_DATASET;
1482 /* check options */
1483 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1484 switch (c) {
1485 case 'p':
1486 cb.cb_literal = B_TRUE;
1487 break;
1488 case 'd':
1489 limit = parse_depth(optarg, &flags);
1490 break;
1491 case 'r':
1492 flags |= ZFS_ITER_RECURSE;
1493 break;
1494 case 'H':
1495 cb.cb_scripted = B_TRUE;
1496 break;
1497 case ':':
1498 (void) fprintf(stderr, gettext("missing argument for "
1499 "'%c' option\n"), optopt);
1500 usage(B_FALSE);
1501 break;
1502 case 'o':
1504 * Process the set of columns to display. We zero out
1505 * the structure to give us a blank slate.
1507 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1508 i = 0;
1509 while (*optarg != '\0') {
1510 static char *col_subopts[] =
1511 { "name", "property", "value", "received",
1512 "source", "all", NULL };
1514 if (i == ZFS_GET_NCOLS) {
1515 (void) fprintf(stderr, gettext("too "
1516 "many fields given to -o "
1517 "option\n"));
1518 usage(B_FALSE);
1521 switch (getsubopt(&optarg, col_subopts,
1522 &value)) {
1523 case 0:
1524 cb.cb_columns[i++] = GET_COL_NAME;
1525 break;
1526 case 1:
1527 cb.cb_columns[i++] = GET_COL_PROPERTY;
1528 break;
1529 case 2:
1530 cb.cb_columns[i++] = GET_COL_VALUE;
1531 break;
1532 case 3:
1533 cb.cb_columns[i++] = GET_COL_RECVD;
1534 flags |= ZFS_ITER_RECVD_PROPS;
1535 break;
1536 case 4:
1537 cb.cb_columns[i++] = GET_COL_SOURCE;
1538 break;
1539 case 5:
1540 if (i > 0) {
1541 (void) fprintf(stderr,
1542 gettext("\"all\" conflicts "
1543 "with specific fields "
1544 "given to -o option\n"));
1545 usage(B_FALSE);
1547 cb.cb_columns[0] = GET_COL_NAME;
1548 cb.cb_columns[1] = GET_COL_PROPERTY;
1549 cb.cb_columns[2] = GET_COL_VALUE;
1550 cb.cb_columns[3] = GET_COL_RECVD;
1551 cb.cb_columns[4] = GET_COL_SOURCE;
1552 flags |= ZFS_ITER_RECVD_PROPS;
1553 i = ZFS_GET_NCOLS;
1554 break;
1555 default:
1556 (void) fprintf(stderr,
1557 gettext("invalid column name "
1558 "'%s'\n"), value);
1559 usage(B_FALSE);
1562 break;
1564 case 's':
1565 cb.cb_sources = 0;
1566 while (*optarg != '\0') {
1567 static char *source_subopts[] = {
1568 "local", "default", "inherited",
1569 "received", "temporary", "none",
1570 NULL };
1572 switch (getsubopt(&optarg, source_subopts,
1573 &value)) {
1574 case 0:
1575 cb.cb_sources |= ZPROP_SRC_LOCAL;
1576 break;
1577 case 1:
1578 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1579 break;
1580 case 2:
1581 cb.cb_sources |= ZPROP_SRC_INHERITED;
1582 break;
1583 case 3:
1584 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1585 break;
1586 case 4:
1587 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1588 break;
1589 case 5:
1590 cb.cb_sources |= ZPROP_SRC_NONE;
1591 break;
1592 default:
1593 (void) fprintf(stderr,
1594 gettext("invalid source "
1595 "'%s'\n"), value);
1596 usage(B_FALSE);
1599 break;
1601 case 't':
1602 types = 0;
1603 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1604 while (*optarg != '\0') {
1605 static char *type_subopts[] = { "filesystem",
1606 "volume", "snapshot", "all", NULL };
1608 switch (getsubopt(&optarg, type_subopts,
1609 &value)) {
1610 case 0:
1611 types |= ZFS_TYPE_FILESYSTEM;
1612 break;
1613 case 1:
1614 types |= ZFS_TYPE_VOLUME;
1615 break;
1616 case 2:
1617 types |= ZFS_TYPE_SNAPSHOT;
1618 break;
1619 case 3:
1620 types = ZFS_TYPE_DATASET;
1621 break;
1623 default:
1624 (void) fprintf(stderr,
1625 gettext("invalid type '%s'\n"),
1626 value);
1627 usage(B_FALSE);
1630 break;
1632 case '?':
1633 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1634 optopt);
1635 usage(B_FALSE);
1639 argc -= optind;
1640 argv += optind;
1642 if (argc < 1) {
1643 (void) fprintf(stderr, gettext("missing property "
1644 "argument\n"));
1645 usage(B_FALSE);
1648 fields = argv[0];
1650 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1651 != 0)
1652 usage(B_FALSE);
1654 argc--;
1655 argv++;
1658 * As part of zfs_expand_proplist(), we keep track of the maximum column
1659 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1660 * need to know the maximum name length. However, the user likely did
1661 * not specify 'name' as one of the properties to fetch, so we need to
1662 * make sure we always include at least this property for
1663 * print_get_headers() to work properly.
1665 if (cb.cb_proplist != NULL) {
1666 fake_name.pl_prop = ZFS_PROP_NAME;
1667 fake_name.pl_width = strlen(gettext("NAME"));
1668 fake_name.pl_next = cb.cb_proplist;
1669 cb.cb_proplist = &fake_name;
1672 cb.cb_first = B_TRUE;
1674 /* run for each object */
1675 ret = zfs_for_each(argc, argv, flags, types, NULL,
1676 &cb.cb_proplist, limit, get_callback, &cb);
1678 if (cb.cb_proplist == &fake_name)
1679 zprop_free_list(fake_name.pl_next);
1680 else
1681 zprop_free_list(cb.cb_proplist);
1683 return (ret);
1687 * inherit [-rS] <property> <fs|vol> ...
1689 * -r Recurse over all children
1690 * -S Revert to received value, if any
1692 * For each dataset specified on the command line, inherit the given property
1693 * from its parent. Inheriting a property at the pool level will cause it to
1694 * use the default value. The '-r' flag will recurse over all children, and is
1695 * useful for setting a property on a hierarchy-wide basis, regardless of any
1696 * local modifications for each dataset.
1699 typedef struct inherit_cbdata {
1700 const char *cb_propname;
1701 boolean_t cb_received;
1702 } inherit_cbdata_t;
1704 static int
1705 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1707 inherit_cbdata_t *cb = data;
1708 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1711 * If we're doing it recursively, then ignore properties that
1712 * are not valid for this type of dataset.
1714 if (prop != ZPROP_INVAL &&
1715 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1716 return (0);
1718 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1721 static int
1722 inherit_cb(zfs_handle_t *zhp, void *data)
1724 inherit_cbdata_t *cb = data;
1726 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1729 static int
1730 zfs_do_inherit(int argc, char **argv)
1732 int c;
1733 zfs_prop_t prop;
1734 inherit_cbdata_t cb = { 0 };
1735 char *propname;
1736 int ret = 0;
1737 int flags = 0;
1738 boolean_t received = B_FALSE;
1740 /* check options */
1741 while ((c = getopt(argc, argv, "rS")) != -1) {
1742 switch (c) {
1743 case 'r':
1744 flags |= ZFS_ITER_RECURSE;
1745 break;
1746 case 'S':
1747 received = B_TRUE;
1748 break;
1749 case '?':
1750 default:
1751 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1752 optopt);
1753 usage(B_FALSE);
1757 argc -= optind;
1758 argv += optind;
1760 /* check number of arguments */
1761 if (argc < 1) {
1762 (void) fprintf(stderr, gettext("missing property argument\n"));
1763 usage(B_FALSE);
1765 if (argc < 2) {
1766 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1767 usage(B_FALSE);
1770 propname = argv[0];
1771 argc--;
1772 argv++;
1774 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1775 if (zfs_prop_readonly(prop)) {
1776 (void) fprintf(stderr, gettext(
1777 "%s property is read-only\n"),
1778 propname);
1779 return (1);
1781 if (!zfs_prop_inheritable(prop) && !received) {
1782 (void) fprintf(stderr, gettext("'%s' property cannot "
1783 "be inherited\n"), propname);
1784 if (prop == ZFS_PROP_QUOTA ||
1785 prop == ZFS_PROP_RESERVATION ||
1786 prop == ZFS_PROP_REFQUOTA ||
1787 prop == ZFS_PROP_REFRESERVATION)
1788 (void) fprintf(stderr, gettext("use 'zfs set "
1789 "%s=none' to clear\n"), propname);
1790 return (1);
1792 if (received && (prop == ZFS_PROP_VOLSIZE ||
1793 prop == ZFS_PROP_VERSION)) {
1794 (void) fprintf(stderr, gettext("'%s' property cannot "
1795 "be reverted to a received value\n"), propname);
1796 return (1);
1798 } else if (!zfs_prop_user(propname)) {
1799 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1800 propname);
1801 usage(B_FALSE);
1804 cb.cb_propname = propname;
1805 cb.cb_received = received;
1807 if (flags & ZFS_ITER_RECURSE) {
1808 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1809 NULL, NULL, 0, inherit_recurse_cb, &cb);
1810 } else {
1811 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1812 NULL, NULL, 0, inherit_cb, &cb);
1815 return (ret);
1818 typedef struct upgrade_cbdata {
1819 uint64_t cb_numupgraded;
1820 uint64_t cb_numsamegraded;
1821 uint64_t cb_numfailed;
1822 uint64_t cb_version;
1823 boolean_t cb_newer;
1824 boolean_t cb_foundone;
1825 char cb_lastfs[ZFS_MAXNAMELEN];
1826 } upgrade_cbdata_t;
1828 static int
1829 same_pool(zfs_handle_t *zhp, const char *name)
1831 int len1 = strcspn(name, "/@");
1832 const char *zhname = zfs_get_name(zhp);
1833 int len2 = strcspn(zhname, "/@");
1835 if (len1 != len2)
1836 return (B_FALSE);
1837 return (strncmp(name, zhname, len1) == 0);
1840 static int
1841 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1843 upgrade_cbdata_t *cb = data;
1844 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1846 /* list if it's old/new */
1847 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1848 (cb->cb_newer && version > ZPL_VERSION)) {
1849 char *str;
1850 if (cb->cb_newer) {
1851 str = gettext("The following filesystems are "
1852 "formatted using a newer software version and\n"
1853 "cannot be accessed on the current system.\n\n");
1854 } else {
1855 str = gettext("The following filesystems are "
1856 "out of date, and can be upgraded. After being\n"
1857 "upgraded, these filesystems (and any 'zfs send' "
1858 "streams generated from\n"
1859 "subsequent snapshots) will no longer be "
1860 "accessible by older software versions.\n\n");
1863 if (!cb->cb_foundone) {
1864 (void) puts(str);
1865 (void) printf(gettext("VER FILESYSTEM\n"));
1866 (void) printf(gettext("--- ------------\n"));
1867 cb->cb_foundone = B_TRUE;
1870 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1873 return (0);
1876 static int
1877 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1879 upgrade_cbdata_t *cb = data;
1880 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1881 int needed_spa_version;
1882 int spa_version;
1884 if (zfs_spa_version(zhp, &spa_version) < 0)
1885 return (-1);
1887 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1889 if (needed_spa_version < 0)
1890 return (-1);
1892 if (spa_version < needed_spa_version) {
1893 /* can't upgrade */
1894 (void) printf(gettext("%s: can not be "
1895 "upgraded; the pool version needs to first "
1896 "be upgraded\nto version %d\n\n"),
1897 zfs_get_name(zhp), needed_spa_version);
1898 cb->cb_numfailed++;
1899 return (0);
1902 /* upgrade */
1903 if (version < cb->cb_version) {
1904 char verstr[16];
1905 (void) snprintf(verstr, sizeof (verstr),
1906 "%llu", cb->cb_version);
1907 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1909 * If they did "zfs upgrade -a", then we could
1910 * be doing ioctls to different pools. We need
1911 * to log this history once to each pool, and bypass
1912 * the normal history logging that happens in main().
1914 (void) zpool_log_history(g_zfs, history_str);
1915 log_history = B_FALSE;
1917 if (zfs_prop_set(zhp, "version", verstr) == 0)
1918 cb->cb_numupgraded++;
1919 else
1920 cb->cb_numfailed++;
1921 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1922 } else if (version > cb->cb_version) {
1923 /* can't downgrade */
1924 (void) printf(gettext("%s: can not be downgraded; "
1925 "it is already at version %u\n"),
1926 zfs_get_name(zhp), version);
1927 cb->cb_numfailed++;
1928 } else {
1929 cb->cb_numsamegraded++;
1931 return (0);
1935 * zfs upgrade
1936 * zfs upgrade -v
1937 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1939 static int
1940 zfs_do_upgrade(int argc, char **argv)
1942 boolean_t all = B_FALSE;
1943 boolean_t showversions = B_FALSE;
1944 int ret = 0;
1945 upgrade_cbdata_t cb = { 0 };
1946 char c;
1947 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1949 /* check options */
1950 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1951 switch (c) {
1952 case 'r':
1953 flags |= ZFS_ITER_RECURSE;
1954 break;
1955 case 'v':
1956 showversions = B_TRUE;
1957 break;
1958 case 'V':
1959 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1960 optarg, &cb.cb_version) != 0) {
1961 (void) fprintf(stderr,
1962 gettext("invalid version %s\n"), optarg);
1963 usage(B_FALSE);
1965 break;
1966 case 'a':
1967 all = B_TRUE;
1968 break;
1969 case '?':
1970 default:
1971 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1972 optopt);
1973 usage(B_FALSE);
1977 argc -= optind;
1978 argv += optind;
1980 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1981 usage(B_FALSE);
1982 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1983 cb.cb_version || argc))
1984 usage(B_FALSE);
1985 if ((all || argc) && (showversions))
1986 usage(B_FALSE);
1987 if (all && argc)
1988 usage(B_FALSE);
1990 if (showversions) {
1991 /* Show info on available versions. */
1992 (void) printf(gettext("The following filesystem versions are "
1993 "supported:\n\n"));
1994 (void) printf(gettext("VER DESCRIPTION\n"));
1995 (void) printf("--- -----------------------------------------"
1996 "---------------\n");
1997 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1998 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1999 (void) printf(gettext(" 3 Case insensitive and filesystem "
2000 "user identifier (FUID)\n"));
2001 (void) printf(gettext(" 4 userquota, groupquota "
2002 "properties\n"));
2003 (void) printf(gettext(" 5 System attributes\n"));
2004 (void) printf(gettext("\nFor more information on a particular "
2005 "version, including supported releases,\n"));
2006 (void) printf("see the ZFS Administration Guide.\n\n");
2007 ret = 0;
2008 } else if (argc || all) {
2009 /* Upgrade filesystems */
2010 if (cb.cb_version == 0)
2011 cb.cb_version = ZPL_VERSION;
2012 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2013 NULL, NULL, 0, upgrade_set_callback, &cb);
2014 (void) printf(gettext("%llu filesystems upgraded\n"),
2015 cb.cb_numupgraded);
2016 if (cb.cb_numsamegraded) {
2017 (void) printf(gettext("%llu filesystems already at "
2018 "this version\n"),
2019 cb.cb_numsamegraded);
2021 if (cb.cb_numfailed != 0)
2022 ret = 1;
2023 } else {
2024 /* List old-version filesytems */
2025 boolean_t found;
2026 (void) printf(gettext("This system is currently running "
2027 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2029 flags |= ZFS_ITER_RECURSE;
2030 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2031 NULL, NULL, 0, upgrade_list_callback, &cb);
2033 found = cb.cb_foundone;
2034 cb.cb_foundone = B_FALSE;
2035 cb.cb_newer = B_TRUE;
2037 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2038 NULL, NULL, 0, upgrade_list_callback, &cb);
2040 if (!cb.cb_foundone && !found) {
2041 (void) printf(gettext("All filesystems are "
2042 "formatted with the current version.\n"));
2046 return (ret);
2050 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2051 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2052 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2053 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2055 * -H Scripted mode; elide headers and separate columns by tabs.
2056 * -i Translate SID to POSIX ID.
2057 * -n Print numeric ID instead of user/group name.
2058 * -o Control which fields to display.
2059 * -p Use exact (parseable) numeric output.
2060 * -s Specify sort columns, descending order.
2061 * -S Specify sort columns, ascending order.
2062 * -t Control which object types to display.
2064 * Displays space consumed by, and quotas on, each user in the specified
2065 * filesystem or snapshot.
2068 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2069 enum us_field_types {
2070 USFIELD_TYPE,
2071 USFIELD_NAME,
2072 USFIELD_USED,
2073 USFIELD_QUOTA
2075 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2076 static char *us_field_names[] = { "type", "name", "used", "quota" };
2077 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2079 #define USTYPE_PSX_GRP (1 << 0)
2080 #define USTYPE_PSX_USR (1 << 1)
2081 #define USTYPE_SMB_GRP (1 << 2)
2082 #define USTYPE_SMB_USR (1 << 3)
2083 #define USTYPE_ALL \
2084 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2086 static int us_type_bits[] = {
2087 USTYPE_PSX_GRP,
2088 USTYPE_PSX_USR,
2089 USTYPE_SMB_GRP,
2090 USTYPE_SMB_USR,
2091 USTYPE_ALL
2093 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2094 "smbuser", "all" };
2096 typedef struct us_node {
2097 nvlist_t *usn_nvl;
2098 uu_avl_node_t usn_avlnode;
2099 uu_list_node_t usn_listnode;
2100 } us_node_t;
2102 typedef struct us_cbdata {
2103 nvlist_t **cb_nvlp;
2104 uu_avl_pool_t *cb_avl_pool;
2105 uu_avl_t *cb_avl;
2106 boolean_t cb_numname;
2107 boolean_t cb_nicenum;
2108 boolean_t cb_sid2posix;
2109 zfs_userquota_prop_t cb_prop;
2110 zfs_sort_column_t *cb_sortcol;
2111 size_t cb_width[USFIELD_LAST];
2112 } us_cbdata_t;
2114 static boolean_t us_populated = B_FALSE;
2116 typedef struct {
2117 zfs_sort_column_t *si_sortcol;
2118 boolean_t si_numname;
2119 } us_sort_info_t;
2121 static int
2122 us_field_index(char *field)
2124 int i;
2126 for (i = 0; i < USFIELD_LAST; i++) {
2127 if (strcmp(field, us_field_names[i]) == 0)
2128 return (i);
2131 return (-1);
2134 static int
2135 us_compare(const void *larg, const void *rarg, void *unused)
2137 const us_node_t *l = larg;
2138 const us_node_t *r = rarg;
2139 us_sort_info_t *si = (us_sort_info_t *)unused;
2140 zfs_sort_column_t *sortcol = si->si_sortcol;
2141 boolean_t numname = si->si_numname;
2142 nvlist_t *lnvl = l->usn_nvl;
2143 nvlist_t *rnvl = r->usn_nvl;
2144 int rc = 0;
2145 boolean_t lvb, rvb;
2147 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2148 char *lvstr = "";
2149 char *rvstr = "";
2150 uint32_t lv32 = 0;
2151 uint32_t rv32 = 0;
2152 uint64_t lv64 = 0;
2153 uint64_t rv64 = 0;
2154 zfs_prop_t prop = sortcol->sc_prop;
2155 const char *propname = NULL;
2156 boolean_t reverse = sortcol->sc_reverse;
2158 switch (prop) {
2159 case ZFS_PROP_TYPE:
2160 propname = "type";
2161 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2162 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2163 if (rv32 != lv32)
2164 rc = (rv32 < lv32) ? 1 : -1;
2165 break;
2166 case ZFS_PROP_NAME:
2167 propname = "name";
2168 if (numname) {
2169 (void) nvlist_lookup_uint64(lnvl, propname,
2170 &lv64);
2171 (void) nvlist_lookup_uint64(rnvl, propname,
2172 &rv64);
2173 if (rv64 != lv64)
2174 rc = (rv64 < lv64) ? 1 : -1;
2175 } else {
2176 (void) nvlist_lookup_string(lnvl, propname,
2177 &lvstr);
2178 (void) nvlist_lookup_string(rnvl, propname,
2179 &rvstr);
2180 rc = strcmp(lvstr, rvstr);
2182 break;
2183 case ZFS_PROP_USED:
2184 case ZFS_PROP_QUOTA:
2185 if (!us_populated)
2186 break;
2187 if (prop == ZFS_PROP_USED)
2188 propname = "used";
2189 else
2190 propname = "quota";
2191 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2192 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2193 if (rv64 != lv64)
2194 rc = (rv64 < lv64) ? 1 : -1;
2195 break;
2198 if (rc != 0) {
2199 if (rc < 0)
2200 return (reverse ? 1 : -1);
2201 else
2202 return (reverse ? -1 : 1);
2207 * If entries still seem to be the same, check if they are of the same
2208 * type (smbentity is added only if we are doing SID to POSIX ID
2209 * translation where we can have duplicate type/name combinations).
2211 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2212 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2213 lvb != rvb)
2214 return (lvb < rvb ? -1 : 1);
2216 return (0);
2219 static inline const char *
2220 us_type2str(unsigned field_type)
2222 switch (field_type) {
2223 case USTYPE_PSX_USR:
2224 return ("POSIX User");
2225 case USTYPE_PSX_GRP:
2226 return ("POSIX Group");
2227 case USTYPE_SMB_USR:
2228 return ("SMB User");
2229 case USTYPE_SMB_GRP:
2230 return ("SMB Group");
2231 default:
2232 return ("Undefined");
2236 static int
2237 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2239 us_cbdata_t *cb = (us_cbdata_t *)arg;
2240 zfs_userquota_prop_t prop = cb->cb_prop;
2241 char *name = NULL;
2242 char *propname;
2243 char sizebuf[32];
2244 us_node_t *node;
2245 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2246 uu_avl_t *avl = cb->cb_avl;
2247 uu_avl_index_t idx;
2248 nvlist_t *props;
2249 us_node_t *n;
2250 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2251 unsigned type;
2252 const char *typestr;
2253 size_t namelen;
2254 size_t typelen;
2255 size_t sizelen;
2256 int typeidx, nameidx, sizeidx;
2257 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2258 boolean_t smbentity = B_FALSE;
2260 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2261 nomem();
2262 node = safe_malloc(sizeof (us_node_t));
2263 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2264 node->usn_nvl = props;
2266 if (domain != NULL && domain[0] != '\0') {
2267 /* SMB */
2268 char sid[ZFS_MAXNAMELEN + 32];
2269 uid_t id;
2270 uint64_t classes;
2271 int err;
2272 directory_error_t e;
2274 smbentity = B_TRUE;
2276 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2278 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2279 type = USTYPE_SMB_GRP;
2280 err = sid_to_id(sid, B_FALSE, &id);
2281 } else {
2282 type = USTYPE_SMB_USR;
2283 err = sid_to_id(sid, B_TRUE, &id);
2286 if (err == 0) {
2287 rid = id;
2288 if (!cb->cb_sid2posix) {
2289 e = directory_name_from_sid(NULL, sid, &name,
2290 &classes);
2291 if (e != NULL) {
2292 directory_error_free(e);
2293 return (1);
2295 if (name == NULL)
2296 name = sid;
2301 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2302 /* POSIX or -i */
2303 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2304 type = USTYPE_PSX_GRP;
2305 if (!cb->cb_numname) {
2306 struct group *g;
2308 if ((g = getgrgid(rid)) != NULL)
2309 name = g->gr_name;
2311 } else {
2312 type = USTYPE_PSX_USR;
2313 if (!cb->cb_numname) {
2314 struct passwd *p;
2316 if ((p = getpwuid(rid)) != NULL)
2317 name = p->pw_name;
2323 * Make sure that the type/name combination is unique when doing
2324 * SID to POSIX ID translation (hence changing the type from SMB to
2325 * POSIX).
2327 if (cb->cb_sid2posix &&
2328 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2329 nomem();
2331 /* Calculate/update width of TYPE field */
2332 typestr = us_type2str(type);
2333 typelen = strlen(gettext(typestr));
2334 typeidx = us_field_index("type");
2335 if (typelen > cb->cb_width[typeidx])
2336 cb->cb_width[typeidx] = typelen;
2337 if (nvlist_add_uint32(props, "type", type) != 0)
2338 nomem();
2340 /* Calculate/update width of NAME field */
2341 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2342 if (nvlist_add_uint64(props, "name", rid) != 0)
2343 nomem();
2344 namelen = snprintf(NULL, 0, "%u", rid);
2345 } else {
2346 if (nvlist_add_string(props, "name", name) != 0)
2347 nomem();
2348 namelen = strlen(name);
2350 nameidx = us_field_index("name");
2351 if (namelen > cb->cb_width[nameidx])
2352 cb->cb_width[nameidx] = namelen;
2355 * Check if this type/name combination is in the list and update it;
2356 * otherwise add new node to the list.
2358 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2359 uu_avl_insert(avl, node, idx);
2360 } else {
2361 nvlist_free(props);
2362 free(node);
2363 node = n;
2364 props = node->usn_nvl;
2367 /* Calculate/update width of USED/QUOTA fields */
2368 if (cb->cb_nicenum)
2369 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2370 else
2371 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2372 sizelen = strlen(sizebuf);
2373 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2374 propname = "used";
2375 if (!nvlist_exists(props, "quota"))
2376 (void) nvlist_add_uint64(props, "quota", 0);
2377 } else {
2378 propname = "quota";
2379 if (!nvlist_exists(props, "used"))
2380 (void) nvlist_add_uint64(props, "used", 0);
2382 sizeidx = us_field_index(propname);
2383 if (sizelen > cb->cb_width[sizeidx])
2384 cb->cb_width[sizeidx] = sizelen;
2386 if (nvlist_add_uint64(props, propname, space) != 0)
2387 nomem();
2389 return (0);
2392 static void
2393 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2394 size_t *width, us_node_t *node)
2396 nvlist_t *nvl = node->usn_nvl;
2397 char valstr[ZFS_MAXNAMELEN];
2398 boolean_t first = B_TRUE;
2399 int cfield = 0;
2400 int field;
2401 uint32_t ustype;
2403 /* Check type */
2404 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2405 if (!(ustype & types))
2406 return;
2408 while ((field = fields[cfield]) != USFIELD_LAST) {
2409 nvpair_t *nvp = NULL;
2410 data_type_t type;
2411 uint32_t val32;
2412 uint64_t val64;
2413 char *strval = NULL;
2415 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2416 if (strcmp(nvpair_name(nvp),
2417 us_field_names[field]) == 0)
2418 break;
2421 type = nvpair_type(nvp);
2422 switch (type) {
2423 case DATA_TYPE_UINT32:
2424 (void) nvpair_value_uint32(nvp, &val32);
2425 break;
2426 case DATA_TYPE_UINT64:
2427 (void) nvpair_value_uint64(nvp, &val64);
2428 break;
2429 case DATA_TYPE_STRING:
2430 (void) nvpair_value_string(nvp, &strval);
2431 break;
2432 default:
2433 (void) fprintf(stderr, "invalid data type\n");
2436 switch (field) {
2437 case USFIELD_TYPE:
2438 strval = (char *)us_type2str(val32);
2439 break;
2440 case USFIELD_NAME:
2441 if (type == DATA_TYPE_UINT64) {
2442 (void) sprintf(valstr, "%llu", val64);
2443 strval = valstr;
2445 break;
2446 case USFIELD_USED:
2447 case USFIELD_QUOTA:
2448 if (type == DATA_TYPE_UINT64) {
2449 if (parsable) {
2450 (void) sprintf(valstr, "%llu", val64);
2451 } else {
2452 zfs_nicenum(val64, valstr,
2453 sizeof (valstr));
2455 if (field == USFIELD_QUOTA &&
2456 strcmp(valstr, "0") == 0)
2457 strval = "none";
2458 else
2459 strval = valstr;
2461 break;
2464 if (!first) {
2465 if (scripted)
2466 (void) printf("\t");
2467 else
2468 (void) printf(" ");
2470 if (scripted)
2471 (void) printf("%s", strval);
2472 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2473 (void) printf("%-*s", width[field], strval);
2474 else
2475 (void) printf("%*s", width[field], strval);
2477 first = B_FALSE;
2478 cfield++;
2481 (void) printf("\n");
2484 static void
2485 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2486 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2488 us_node_t *node;
2489 const char *col;
2490 int cfield = 0;
2491 int field;
2493 if (!scripted) {
2494 boolean_t first = B_TRUE;
2496 while ((field = fields[cfield]) != USFIELD_LAST) {
2497 col = gettext(us_field_hdr[field]);
2498 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2499 (void) printf(first ? "%-*s" : " %-*s",
2500 width[field], col);
2501 } else {
2502 (void) printf(first ? "%*s" : " %*s",
2503 width[field], col);
2505 first = B_FALSE;
2506 cfield++;
2508 (void) printf("\n");
2511 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2512 print_us_node(scripted, parsable, fields, types, width, node);
2513 if (rmnode)
2514 nvlist_free(node->usn_nvl);
2518 static int
2519 zfs_do_userspace(int argc, char **argv)
2521 zfs_handle_t *zhp;
2522 zfs_userquota_prop_t p;
2523 uu_avl_pool_t *avl_pool;
2524 uu_avl_t *avl_tree;
2525 uu_avl_walk_t *walk;
2526 char *delim;
2527 char deffields[] = "type,name,used,quota";
2528 char *ofield = NULL;
2529 char *tfield = NULL;
2530 int cfield = 0;
2531 int fields[256];
2532 int i;
2533 boolean_t scripted = B_FALSE;
2534 boolean_t prtnum = B_FALSE;
2535 boolean_t parsable = B_FALSE;
2536 boolean_t sid2posix = B_FALSE;
2537 int error = 0;
2538 int c;
2539 zfs_sort_column_t *sortcol = NULL;
2540 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2541 us_cbdata_t cb;
2542 us_node_t *node;
2543 us_node_t *rmnode;
2544 uu_list_pool_t *listpool;
2545 uu_list_t *list;
2546 uu_avl_index_t idx = 0;
2547 uu_list_index_t idx2 = 0;
2549 if (argc < 2)
2550 usage(B_FALSE);
2552 if (strcmp(argv[0], "groupspace") == 0)
2553 /* Toggle default group types */
2554 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2556 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2557 switch (c) {
2558 case 'n':
2559 prtnum = B_TRUE;
2560 break;
2561 case 'H':
2562 scripted = B_TRUE;
2563 break;
2564 case 'p':
2565 parsable = B_TRUE;
2566 break;
2567 case 'o':
2568 ofield = optarg;
2569 break;
2570 case 's':
2571 case 'S':
2572 if (zfs_add_sort_column(&sortcol, optarg,
2573 c == 's' ? B_FALSE : B_TRUE) != 0) {
2574 (void) fprintf(stderr,
2575 gettext("invalid field '%s'\n"), optarg);
2576 usage(B_FALSE);
2578 break;
2579 case 't':
2580 tfield = optarg;
2581 break;
2582 case 'i':
2583 sid2posix = B_TRUE;
2584 break;
2585 case ':':
2586 (void) fprintf(stderr, gettext("missing argument for "
2587 "'%c' option\n"), optopt);
2588 usage(B_FALSE);
2589 break;
2590 case '?':
2591 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2592 optopt);
2593 usage(B_FALSE);
2597 argc -= optind;
2598 argv += optind;
2600 if (argc < 1) {
2601 (void) fprintf(stderr, gettext("missing dataset name\n"));
2602 usage(B_FALSE);
2604 if (argc > 1) {
2605 (void) fprintf(stderr, gettext("too many arguments\n"));
2606 usage(B_FALSE);
2609 /* Use default output fields if not specified using -o */
2610 if (ofield == NULL)
2611 ofield = deffields;
2612 do {
2613 if ((delim = strchr(ofield, ',')) != NULL)
2614 *delim = '\0';
2615 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2616 (void) fprintf(stderr, gettext("invalid type '%s' "
2617 "for -o option\n"), ofield);
2618 return (-1);
2620 if (delim != NULL)
2621 ofield = delim + 1;
2622 } while (delim != NULL);
2623 fields[cfield] = USFIELD_LAST;
2625 /* Override output types (-t option) */
2626 if (tfield != NULL) {
2627 types = 0;
2629 do {
2630 boolean_t found = B_FALSE;
2632 if ((delim = strchr(tfield, ',')) != NULL)
2633 *delim = '\0';
2634 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2635 i++) {
2636 if (strcmp(tfield, us_type_names[i]) == 0) {
2637 found = B_TRUE;
2638 types |= us_type_bits[i];
2639 break;
2642 if (!found) {
2643 (void) fprintf(stderr, gettext("invalid type "
2644 "'%s' for -t option\n"), tfield);
2645 return (-1);
2647 if (delim != NULL)
2648 tfield = delim + 1;
2649 } while (delim != NULL);
2652 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2653 return (1);
2655 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2656 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2657 nomem();
2658 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2659 nomem();
2661 /* Always add default sorting columns */
2662 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2663 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2665 cb.cb_sortcol = sortcol;
2666 cb.cb_numname = prtnum;
2667 cb.cb_nicenum = !parsable;
2668 cb.cb_avl_pool = avl_pool;
2669 cb.cb_avl = avl_tree;
2670 cb.cb_sid2posix = sid2posix;
2672 for (i = 0; i < USFIELD_LAST; i++)
2673 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2675 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2676 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2677 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2678 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2679 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2680 continue;
2681 cb.cb_prop = p;
2682 error = zfs_userspace(zhp, p, userspace_cb, &cb);
2683 if (error)
2684 break;
2687 /* Sort the list */
2688 us_populated = B_TRUE;
2689 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2690 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2691 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2693 node = uu_avl_first(avl_tree);
2694 uu_list_node_init(node, &node->usn_listnode, listpool);
2696 while (node != NULL) {
2697 rmnode = node;
2698 node = uu_avl_next(avl_tree, node);
2699 uu_avl_remove(avl_tree, rmnode);
2700 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2701 uu_list_insert(list, rmnode, idx2);
2704 for (node = uu_list_first(list); node != NULL;
2705 node = uu_list_next(list, node)) {
2706 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2708 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2709 uu_avl_insert(avl_tree, node, idx);
2712 uu_list_destroy(list);
2713 uu_list_pool_destroy(listpool);
2715 /* Print and free node nvlist memory */
2716 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2717 cb.cb_avl);
2719 zfs_free_sort_columns(sortcol);
2721 /* Clean up the AVL tree */
2722 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2723 nomem();
2725 while ((node = uu_avl_walk_next(walk)) != NULL) {
2726 uu_avl_remove(cb.cb_avl, node);
2727 free(node);
2730 uu_avl_walk_end(walk);
2731 uu_avl_destroy(avl_tree);
2732 uu_avl_pool_destroy(avl_pool);
2734 return (error);
2738 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2739 * [-s property [-s property]...] [-S property [-S property]...]
2740 * <dataset> ...
2742 * -r Recurse over all children
2743 * -d Limit recursion by depth.
2744 * -H Scripted mode; elide headers and separate columns by tabs
2745 * -o Control which fields to display.
2746 * -t Control which object types to display.
2747 * -s Specify sort columns, descending order.
2748 * -S Specify sort columns, ascending order.
2750 * When given no arguments, lists all filesystems in the system.
2751 * Otherwise, list the specified datasets, optionally recursing down them if
2752 * '-r' is specified.
2754 typedef struct list_cbdata {
2755 boolean_t cb_first;
2756 boolean_t cb_scripted;
2757 zprop_list_t *cb_proplist;
2758 } list_cbdata_t;
2761 * Given a list of columns to display, output appropriate headers for each one.
2763 static void
2764 print_header(zprop_list_t *pl)
2766 char headerbuf[ZFS_MAXPROPLEN];
2767 const char *header;
2768 int i;
2769 boolean_t first = B_TRUE;
2770 boolean_t right_justify;
2772 for (; pl != NULL; pl = pl->pl_next) {
2773 if (!first) {
2774 (void) printf(" ");
2775 } else {
2776 first = B_FALSE;
2779 right_justify = B_FALSE;
2780 if (pl->pl_prop != ZPROP_INVAL) {
2781 header = zfs_prop_column_name(pl->pl_prop);
2782 right_justify = zfs_prop_align_right(pl->pl_prop);
2783 } else {
2784 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2785 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2786 headerbuf[i] = '\0';
2787 header = headerbuf;
2790 if (pl->pl_next == NULL && !right_justify)
2791 (void) printf("%s", header);
2792 else if (right_justify)
2793 (void) printf("%*s", pl->pl_width, header);
2794 else
2795 (void) printf("%-*s", pl->pl_width, header);
2798 (void) printf("\n");
2802 * Given a dataset and a list of fields, print out all the properties according
2803 * to the described layout.
2805 static void
2806 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2808 boolean_t first = B_TRUE;
2809 char property[ZFS_MAXPROPLEN];
2810 nvlist_t *userprops = zfs_get_user_props(zhp);
2811 nvlist_t *propval;
2812 char *propstr;
2813 boolean_t right_justify;
2814 int width;
2816 for (; pl != NULL; pl = pl->pl_next) {
2817 if (!first) {
2818 if (scripted)
2819 (void) printf("\t");
2820 else
2821 (void) printf(" ");
2822 } else {
2823 first = B_FALSE;
2826 if (pl->pl_prop != ZPROP_INVAL) {
2827 if (zfs_prop_get(zhp, pl->pl_prop, property,
2828 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2829 propstr = "-";
2830 else
2831 propstr = property;
2833 right_justify = zfs_prop_align_right(pl->pl_prop);
2834 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2835 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2836 property, sizeof (property), B_FALSE) != 0)
2837 propstr = "-";
2838 else
2839 propstr = property;
2840 right_justify = B_TRUE;
2841 } else if (zfs_prop_written(pl->pl_user_prop)) {
2842 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2843 property, sizeof (property), B_FALSE) != 0)
2844 propstr = "-";
2845 else
2846 propstr = property;
2847 right_justify = B_TRUE;
2848 } else {
2849 if (nvlist_lookup_nvlist(userprops,
2850 pl->pl_user_prop, &propval) != 0)
2851 propstr = "-";
2852 else
2853 verify(nvlist_lookup_string(propval,
2854 ZPROP_VALUE, &propstr) == 0);
2855 right_justify = B_FALSE;
2858 width = pl->pl_width;
2861 * If this is being called in scripted mode, or if this is the
2862 * last column and it is left-justified, don't include a width
2863 * format specifier.
2865 if (scripted || (pl->pl_next == NULL && !right_justify))
2866 (void) printf("%s", propstr);
2867 else if (right_justify)
2868 (void) printf("%*s", width, propstr);
2869 else
2870 (void) printf("%-*s", width, propstr);
2873 (void) printf("\n");
2877 * Generic callback function to list a dataset or snapshot.
2879 static int
2880 list_callback(zfs_handle_t *zhp, void *data)
2882 list_cbdata_t *cbp = data;
2884 if (cbp->cb_first) {
2885 if (!cbp->cb_scripted)
2886 print_header(cbp->cb_proplist);
2887 cbp->cb_first = B_FALSE;
2890 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2892 return (0);
2895 static int
2896 zfs_do_list(int argc, char **argv)
2898 int c;
2899 boolean_t scripted = B_FALSE;
2900 static char default_fields[] =
2901 "name,used,available,referenced,mountpoint";
2902 int types = ZFS_TYPE_DATASET;
2903 boolean_t types_specified = B_FALSE;
2904 char *fields = NULL;
2905 list_cbdata_t cb = { 0 };
2906 char *value;
2907 int limit = 0;
2908 int ret = 0;
2909 zfs_sort_column_t *sortcol = NULL;
2910 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2912 /* check options */
2913 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2914 switch (c) {
2915 case 'o':
2916 fields = optarg;
2917 break;
2918 case 'd':
2919 limit = parse_depth(optarg, &flags);
2920 break;
2921 case 'r':
2922 flags |= ZFS_ITER_RECURSE;
2923 break;
2924 case 'H':
2925 scripted = B_TRUE;
2926 break;
2927 case 's':
2928 if (zfs_add_sort_column(&sortcol, optarg,
2929 B_FALSE) != 0) {
2930 (void) fprintf(stderr,
2931 gettext("invalid property '%s'\n"), optarg);
2932 usage(B_FALSE);
2934 break;
2935 case 'S':
2936 if (zfs_add_sort_column(&sortcol, optarg,
2937 B_TRUE) != 0) {
2938 (void) fprintf(stderr,
2939 gettext("invalid property '%s'\n"), optarg);
2940 usage(B_FALSE);
2942 break;
2943 case 't':
2944 types = 0;
2945 types_specified = B_TRUE;
2946 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2947 while (*optarg != '\0') {
2948 static char *type_subopts[] = { "filesystem",
2949 "volume", "snapshot", "all", NULL };
2951 switch (getsubopt(&optarg, type_subopts,
2952 &value)) {
2953 case 0:
2954 types |= ZFS_TYPE_FILESYSTEM;
2955 break;
2956 case 1:
2957 types |= ZFS_TYPE_VOLUME;
2958 break;
2959 case 2:
2960 types |= ZFS_TYPE_SNAPSHOT;
2961 break;
2962 case 3:
2963 types = ZFS_TYPE_DATASET;
2964 break;
2966 default:
2967 (void) fprintf(stderr,
2968 gettext("invalid type '%s'\n"),
2969 value);
2970 usage(B_FALSE);
2973 break;
2974 case ':':
2975 (void) fprintf(stderr, gettext("missing argument for "
2976 "'%c' option\n"), optopt);
2977 usage(B_FALSE);
2978 break;
2979 case '?':
2980 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2981 optopt);
2982 usage(B_FALSE);
2986 argc -= optind;
2987 argv += optind;
2989 if (fields == NULL)
2990 fields = default_fields;
2993 * If "-o space" and no types were specified, don't display snapshots.
2995 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
2996 types &= ~ZFS_TYPE_SNAPSHOT;
2999 * If the user specifies '-o all', the zprop_get_list() doesn't
3000 * normally include the name of the dataset. For 'zfs list', we always
3001 * want this property to be first.
3003 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3004 != 0)
3005 usage(B_FALSE);
3007 cb.cb_scripted = scripted;
3008 cb.cb_first = B_TRUE;
3010 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3011 limit, list_callback, &cb);
3013 zprop_free_list(cb.cb_proplist);
3014 zfs_free_sort_columns(sortcol);
3016 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3017 (void) printf(gettext("no datasets available\n"));
3019 return (ret);
3023 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3024 * zfs rename [-f] -p <fs | vol> <fs | vol>
3025 * zfs rename -r <snap> <snap>
3027 * Renames the given dataset to another of the same type.
3029 * The '-p' flag creates all the non-existing ancestors of the target first.
3031 /* ARGSUSED */
3032 static int
3033 zfs_do_rename(int argc, char **argv)
3035 zfs_handle_t *zhp;
3036 int c;
3037 int ret = 0;
3038 boolean_t recurse = B_FALSE;
3039 boolean_t parents = B_FALSE;
3040 boolean_t force_unmount = B_FALSE;
3042 /* check options */
3043 while ((c = getopt(argc, argv, "prf")) != -1) {
3044 switch (c) {
3045 case 'p':
3046 parents = B_TRUE;
3047 break;
3048 case 'r':
3049 recurse = B_TRUE;
3050 break;
3051 case 'f':
3052 force_unmount = B_TRUE;
3053 break;
3054 case '?':
3055 default:
3056 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3057 optopt);
3058 usage(B_FALSE);
3062 argc -= optind;
3063 argv += optind;
3065 /* check number of arguments */
3066 if (argc < 1) {
3067 (void) fprintf(stderr, gettext("missing source dataset "
3068 "argument\n"));
3069 usage(B_FALSE);
3071 if (argc < 2) {
3072 (void) fprintf(stderr, gettext("missing target dataset "
3073 "argument\n"));
3074 usage(B_FALSE);
3076 if (argc > 2) {
3077 (void) fprintf(stderr, gettext("too many arguments\n"));
3078 usage(B_FALSE);
3081 if (recurse && parents) {
3082 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3083 "exclusive\n"));
3084 usage(B_FALSE);
3087 if (recurse && strchr(argv[0], '@') == 0) {
3088 (void) fprintf(stderr, gettext("source dataset for recursive "
3089 "rename must be a snapshot\n"));
3090 usage(B_FALSE);
3093 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3094 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3095 return (1);
3097 /* If we were asked and the name looks good, try to create ancestors. */
3098 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3099 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3100 zfs_close(zhp);
3101 return (1);
3104 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3106 zfs_close(zhp);
3107 return (ret);
3111 * zfs promote <fs>
3113 * Promotes the given clone fs to be the parent
3115 /* ARGSUSED */
3116 static int
3117 zfs_do_promote(int argc, char **argv)
3119 zfs_handle_t *zhp;
3120 int ret = 0;
3122 /* check options */
3123 if (argc > 1 && argv[1][0] == '-') {
3124 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3125 argv[1][1]);
3126 usage(B_FALSE);
3129 /* check number of arguments */
3130 if (argc < 2) {
3131 (void) fprintf(stderr, gettext("missing clone filesystem"
3132 " argument\n"));
3133 usage(B_FALSE);
3135 if (argc > 2) {
3136 (void) fprintf(stderr, gettext("too many arguments\n"));
3137 usage(B_FALSE);
3140 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3141 if (zhp == NULL)
3142 return (1);
3144 ret = (zfs_promote(zhp) != 0);
3147 zfs_close(zhp);
3148 return (ret);
3152 * zfs rollback [-rRf] <snapshot>
3154 * -r Delete any intervening snapshots before doing rollback
3155 * -R Delete any snapshots and their clones
3156 * -f ignored for backwards compatability
3158 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3159 * since then and making it the active dataset. If more recent snapshots exist,
3160 * the command will complain unless the '-r' flag is given.
3162 typedef struct rollback_cbdata {
3163 uint64_t cb_create;
3164 boolean_t cb_first;
3165 int cb_doclones;
3166 char *cb_target;
3167 int cb_error;
3168 boolean_t cb_recurse;
3169 boolean_t cb_dependent;
3170 } rollback_cbdata_t;
3173 * Report any snapshots more recent than the one specified. Used when '-r' is
3174 * not specified. We reuse this same callback for the snapshot dependents - if
3175 * 'cb_dependent' is set, then this is a dependent and we should report it
3176 * without checking the transaction group.
3178 static int
3179 rollback_check(zfs_handle_t *zhp, void *data)
3181 rollback_cbdata_t *cbp = data;
3183 if (cbp->cb_doclones) {
3184 zfs_close(zhp);
3185 return (0);
3188 if (!cbp->cb_dependent) {
3189 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3190 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3191 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3192 cbp->cb_create) {
3194 if (cbp->cb_first && !cbp->cb_recurse) {
3195 (void) fprintf(stderr, gettext("cannot "
3196 "rollback to '%s': more recent snapshots "
3197 "exist\n"),
3198 cbp->cb_target);
3199 (void) fprintf(stderr, gettext("use '-r' to "
3200 "force deletion of the following "
3201 "snapshots:\n"));
3202 cbp->cb_first = 0;
3203 cbp->cb_error = 1;
3206 if (cbp->cb_recurse) {
3207 cbp->cb_dependent = B_TRUE;
3208 if (zfs_iter_dependents(zhp, B_TRUE,
3209 rollback_check, cbp) != 0) {
3210 zfs_close(zhp);
3211 return (-1);
3213 cbp->cb_dependent = B_FALSE;
3214 } else {
3215 (void) fprintf(stderr, "%s\n",
3216 zfs_get_name(zhp));
3219 } else {
3220 if (cbp->cb_first && cbp->cb_recurse) {
3221 (void) fprintf(stderr, gettext("cannot rollback to "
3222 "'%s': clones of previous snapshots exist\n"),
3223 cbp->cb_target);
3224 (void) fprintf(stderr, gettext("use '-R' to "
3225 "force deletion of the following clones and "
3226 "dependents:\n"));
3227 cbp->cb_first = 0;
3228 cbp->cb_error = 1;
3231 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3234 zfs_close(zhp);
3235 return (0);
3238 static int
3239 zfs_do_rollback(int argc, char **argv)
3241 int ret = 0;
3242 int c;
3243 boolean_t force = B_FALSE;
3244 rollback_cbdata_t cb = { 0 };
3245 zfs_handle_t *zhp, *snap;
3246 char parentname[ZFS_MAXNAMELEN];
3247 char *delim;
3249 /* check options */
3250 while ((c = getopt(argc, argv, "rRf")) != -1) {
3251 switch (c) {
3252 case 'r':
3253 cb.cb_recurse = 1;
3254 break;
3255 case 'R':
3256 cb.cb_recurse = 1;
3257 cb.cb_doclones = 1;
3258 break;
3259 case 'f':
3260 force = B_TRUE;
3261 break;
3262 case '?':
3263 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3264 optopt);
3265 usage(B_FALSE);
3269 argc -= optind;
3270 argv += optind;
3272 /* check number of arguments */
3273 if (argc < 1) {
3274 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3275 usage(B_FALSE);
3277 if (argc > 1) {
3278 (void) fprintf(stderr, gettext("too many arguments\n"));
3279 usage(B_FALSE);
3282 /* open the snapshot */
3283 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3284 return (1);
3286 /* open the parent dataset */
3287 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3288 verify((delim = strrchr(parentname, '@')) != NULL);
3289 *delim = '\0';
3290 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3291 zfs_close(snap);
3292 return (1);
3296 * Check for more recent snapshots and/or clones based on the presence
3297 * of '-r' and '-R'.
3299 cb.cb_target = argv[0];
3300 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3301 cb.cb_first = B_TRUE;
3302 cb.cb_error = 0;
3303 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3304 goto out;
3306 if ((ret = cb.cb_error) != 0)
3307 goto out;
3310 * Rollback parent to the given snapshot.
3312 ret = zfs_rollback(zhp, snap, force);
3314 out:
3315 zfs_close(snap);
3316 zfs_close(zhp);
3318 if (ret == 0)
3319 return (0);
3320 else
3321 return (1);
3325 * zfs set property=value { fs | snap | vol } ...
3327 * Sets the given property for all datasets specified on the command line.
3329 typedef struct set_cbdata {
3330 char *cb_propname;
3331 char *cb_value;
3332 } set_cbdata_t;
3334 static int
3335 set_callback(zfs_handle_t *zhp, void *data)
3337 set_cbdata_t *cbp = data;
3339 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3340 switch (libzfs_errno(g_zfs)) {
3341 case EZFS_MOUNTFAILED:
3342 (void) fprintf(stderr, gettext("property may be set "
3343 "but unable to remount filesystem\n"));
3344 break;
3345 case EZFS_SHARENFSFAILED:
3346 (void) fprintf(stderr, gettext("property may be set "
3347 "but unable to reshare filesystem\n"));
3348 break;
3350 return (1);
3352 return (0);
3355 static int
3356 zfs_do_set(int argc, char **argv)
3358 set_cbdata_t cb;
3359 int ret = 0;
3361 /* check for options */
3362 if (argc > 1 && argv[1][0] == '-') {
3363 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3364 argv[1][1]);
3365 usage(B_FALSE);
3368 /* check number of arguments */
3369 if (argc < 2) {
3370 (void) fprintf(stderr, gettext("missing property=value "
3371 "argument\n"));
3372 usage(B_FALSE);
3374 if (argc < 3) {
3375 (void) fprintf(stderr, gettext("missing dataset name\n"));
3376 usage(B_FALSE);
3379 /* validate property=value argument */
3380 cb.cb_propname = argv[1];
3381 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3382 (cb.cb_value[1] == '\0')) {
3383 (void) fprintf(stderr, gettext("missing value in "
3384 "property=value argument\n"));
3385 usage(B_FALSE);
3388 *cb.cb_value = '\0';
3389 cb.cb_value++;
3391 if (*cb.cb_propname == '\0') {
3392 (void) fprintf(stderr,
3393 gettext("missing property in property=value argument\n"));
3394 usage(B_FALSE);
3397 ret = zfs_for_each(argc - 2, argv + 2, NULL,
3398 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3400 return (ret);
3403 typedef struct snap_cbdata {
3404 nvlist_t *sd_nvl;
3405 boolean_t sd_recursive;
3406 const char *sd_snapname;
3407 } snap_cbdata_t;
3409 static int
3410 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3412 snap_cbdata_t *sd = arg;
3413 char *name;
3414 int rv = 0;
3415 int error;
3417 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3418 if (error == -1)
3419 nomem();
3420 fnvlist_add_boolean(sd->sd_nvl, name);
3421 free(name);
3423 if (sd->sd_recursive)
3424 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3425 zfs_close(zhp);
3426 return (rv);
3430 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3432 * Creates a snapshot with the given name. While functionally equivalent to
3433 * 'zfs create', it is a separate command to differentiate intent.
3435 static int
3436 zfs_do_snapshot(int argc, char **argv)
3438 int ret = 0;
3439 char c;
3440 nvlist_t *props;
3441 snap_cbdata_t sd = { 0 };
3442 boolean_t multiple_snaps = B_FALSE;
3444 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3445 nomem();
3446 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3447 nomem();
3449 /* check options */
3450 while ((c = getopt(argc, argv, "ro:")) != -1) {
3451 switch (c) {
3452 case 'o':
3453 if (parseprop(props))
3454 return (1);
3455 break;
3456 case 'r':
3457 sd.sd_recursive = B_TRUE;
3458 multiple_snaps = B_TRUE;
3459 break;
3460 case '?':
3461 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3462 optopt);
3463 goto usage;
3467 argc -= optind;
3468 argv += optind;
3470 /* check number of arguments */
3471 if (argc < 1) {
3472 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3473 goto usage;
3476 if (argc > 1)
3477 multiple_snaps = B_TRUE;
3478 for (; argc > 0; argc--, argv++) {
3479 char *atp;
3480 zfs_handle_t *zhp;
3482 atp = strchr(argv[0], '@');
3483 if (atp == NULL)
3484 goto usage;
3485 *atp = '\0';
3486 sd.sd_snapname = atp + 1;
3487 zhp = zfs_open(g_zfs, argv[0],
3488 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3489 if (zhp == NULL)
3490 goto usage;
3491 if (zfs_snapshot_cb(zhp, &sd) != 0)
3492 goto usage;
3495 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3496 nvlist_free(sd.sd_nvl);
3497 nvlist_free(props);
3498 if (ret != 0 && multiple_snaps)
3499 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3500 return (ret != 0);
3502 usage:
3503 nvlist_free(sd.sd_nvl);
3504 nvlist_free(props);
3505 usage(B_FALSE);
3506 return (-1);
3510 * Send a backup stream to stdout.
3512 static int
3513 zfs_do_send(int argc, char **argv)
3515 char *fromname = NULL;
3516 char *toname = NULL;
3517 char *cp;
3518 zfs_handle_t *zhp;
3519 sendflags_t flags = { 0 };
3520 int c, err;
3521 nvlist_t *dbgnv = NULL;
3522 boolean_t extraverbose = B_FALSE;
3524 /* check options */
3525 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3526 switch (c) {
3527 case 'i':
3528 if (fromname)
3529 usage(B_FALSE);
3530 fromname = optarg;
3531 break;
3532 case 'I':
3533 if (fromname)
3534 usage(B_FALSE);
3535 fromname = optarg;
3536 flags.doall = B_TRUE;
3537 break;
3538 case 'R':
3539 flags.replicate = B_TRUE;
3540 break;
3541 case 'p':
3542 flags.props = B_TRUE;
3543 break;
3544 case 'P':
3545 flags.parsable = B_TRUE;
3546 flags.verbose = B_TRUE;
3547 break;
3548 case 'v':
3549 if (flags.verbose)
3550 extraverbose = B_TRUE;
3551 flags.verbose = B_TRUE;
3552 flags.progress = B_TRUE;
3553 break;
3554 case 'D':
3555 flags.dedup = B_TRUE;
3556 break;
3557 case 'n':
3558 flags.dryrun = B_TRUE;
3559 break;
3560 case ':':
3561 (void) fprintf(stderr, gettext("missing argument for "
3562 "'%c' option\n"), optopt);
3563 usage(B_FALSE);
3564 break;
3565 case '?':
3566 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3567 optopt);
3568 usage(B_FALSE);
3572 argc -= optind;
3573 argv += optind;
3575 /* check number of arguments */
3576 if (argc < 1) {
3577 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3578 usage(B_FALSE);
3580 if (argc > 1) {
3581 (void) fprintf(stderr, gettext("too many arguments\n"));
3582 usage(B_FALSE);
3585 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3586 (void) fprintf(stderr,
3587 gettext("Error: Stream can not be written to a terminal.\n"
3588 "You must redirect standard output.\n"));
3589 return (1);
3592 cp = strchr(argv[0], '@');
3593 if (cp == NULL) {
3594 (void) fprintf(stderr,
3595 gettext("argument must be a snapshot\n"));
3596 usage(B_FALSE);
3598 *cp = '\0';
3599 toname = cp + 1;
3600 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3601 if (zhp == NULL)
3602 return (1);
3605 * If they specified the full path to the snapshot, chop off
3606 * everything except the short name of the snapshot, but special
3607 * case if they specify the origin.
3609 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3610 char origin[ZFS_MAXNAMELEN];
3611 zprop_source_t src;
3613 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3614 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3616 if (strcmp(origin, fromname) == 0) {
3617 fromname = NULL;
3618 flags.fromorigin = B_TRUE;
3619 } else {
3620 *cp = '\0';
3621 if (cp != fromname && strcmp(argv[0], fromname)) {
3622 (void) fprintf(stderr,
3623 gettext("incremental source must be "
3624 "in same filesystem\n"));
3625 usage(B_FALSE);
3627 fromname = cp + 1;
3628 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3629 (void) fprintf(stderr,
3630 gettext("invalid incremental source\n"));
3631 usage(B_FALSE);
3636 if (flags.replicate && fromname == NULL)
3637 flags.doall = B_TRUE;
3639 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3640 extraverbose ? &dbgnv : NULL);
3642 if (extraverbose && dbgnv != NULL) {
3644 * dump_nvlist prints to stdout, but that's been
3645 * redirected to a file. Make it print to stderr
3646 * instead.
3648 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3649 dump_nvlist(dbgnv, 0);
3650 nvlist_free(dbgnv);
3652 zfs_close(zhp);
3654 return (err != 0);
3658 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3660 * Restore a backup stream from stdin.
3662 static int
3663 zfs_do_receive(int argc, char **argv)
3665 int c, err;
3666 recvflags_t flags = { 0 };
3668 /* check options */
3669 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3670 switch (c) {
3671 case 'd':
3672 flags.isprefix = B_TRUE;
3673 break;
3674 case 'e':
3675 flags.isprefix = B_TRUE;
3676 flags.istail = B_TRUE;
3677 break;
3678 case 'n':
3679 flags.dryrun = B_TRUE;
3680 break;
3681 case 'u':
3682 flags.nomount = B_TRUE;
3683 break;
3684 case 'v':
3685 flags.verbose = B_TRUE;
3686 break;
3687 case 'F':
3688 flags.force = B_TRUE;
3689 break;
3690 case ':':
3691 (void) fprintf(stderr, gettext("missing argument for "
3692 "'%c' option\n"), optopt);
3693 usage(B_FALSE);
3694 break;
3695 case '?':
3696 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3697 optopt);
3698 usage(B_FALSE);
3702 argc -= optind;
3703 argv += optind;
3705 /* check number of arguments */
3706 if (argc < 1) {
3707 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3708 usage(B_FALSE);
3710 if (argc > 1) {
3711 (void) fprintf(stderr, gettext("too many arguments\n"));
3712 usage(B_FALSE);
3715 if (isatty(STDIN_FILENO)) {
3716 (void) fprintf(stderr,
3717 gettext("Error: Backup stream can not be read "
3718 "from a terminal.\n"
3719 "You must redirect standard input.\n"));
3720 return (1);
3723 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3725 return (err != 0);
3729 * allow/unallow stuff
3731 /* copied from zfs/sys/dsl_deleg.h */
3732 #define ZFS_DELEG_PERM_CREATE "create"
3733 #define ZFS_DELEG_PERM_DESTROY "destroy"
3734 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3735 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3736 #define ZFS_DELEG_PERM_CLONE "clone"
3737 #define ZFS_DELEG_PERM_PROMOTE "promote"
3738 #define ZFS_DELEG_PERM_RENAME "rename"
3739 #define ZFS_DELEG_PERM_MOUNT "mount"
3740 #define ZFS_DELEG_PERM_SHARE "share"
3741 #define ZFS_DELEG_PERM_SEND "send"
3742 #define ZFS_DELEG_PERM_RECEIVE "receive"
3743 #define ZFS_DELEG_PERM_ALLOW "allow"
3744 #define ZFS_DELEG_PERM_USERPROP "userprop"
3745 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3746 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3747 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3748 #define ZFS_DELEG_PERM_USERUSED "userused"
3749 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3750 #define ZFS_DELEG_PERM_HOLD "hold"
3751 #define ZFS_DELEG_PERM_RELEASE "release"
3752 #define ZFS_DELEG_PERM_DIFF "diff"
3754 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3756 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3757 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3758 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3759 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3760 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3761 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3762 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3763 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3764 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3765 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3766 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3767 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3768 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3769 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3770 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3771 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3773 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3774 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3775 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3776 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3777 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3778 { NULL, ZFS_DELEG_NOTE_NONE }
3781 /* permission structure */
3782 typedef struct deleg_perm {
3783 zfs_deleg_who_type_t dp_who_type;
3784 const char *dp_name;
3785 boolean_t dp_local;
3786 boolean_t dp_descend;
3787 } deleg_perm_t;
3789 /* */
3790 typedef struct deleg_perm_node {
3791 deleg_perm_t dpn_perm;
3793 uu_avl_node_t dpn_avl_node;
3794 } deleg_perm_node_t;
3796 typedef struct fs_perm fs_perm_t;
3798 /* permissions set */
3799 typedef struct who_perm {
3800 zfs_deleg_who_type_t who_type;
3801 const char *who_name; /* id */
3802 char who_ug_name[256]; /* user/group name */
3803 fs_perm_t *who_fsperm; /* uplink */
3805 uu_avl_t *who_deleg_perm_avl; /* permissions */
3806 } who_perm_t;
3808 /* */
3809 typedef struct who_perm_node {
3810 who_perm_t who_perm;
3811 uu_avl_node_t who_avl_node;
3812 } who_perm_node_t;
3814 typedef struct fs_perm_set fs_perm_set_t;
3815 /* fs permissions */
3816 struct fs_perm {
3817 const char *fsp_name;
3819 uu_avl_t *fsp_sc_avl; /* sets,create */
3820 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3822 fs_perm_set_t *fsp_set; /* uplink */
3825 /* */
3826 typedef struct fs_perm_node {
3827 fs_perm_t fspn_fsperm;
3828 uu_avl_t *fspn_avl;
3830 uu_list_node_t fspn_list_node;
3831 } fs_perm_node_t;
3833 /* top level structure */
3834 struct fs_perm_set {
3835 uu_list_pool_t *fsps_list_pool;
3836 uu_list_t *fsps_list; /* list of fs_perms */
3838 uu_avl_pool_t *fsps_named_set_avl_pool;
3839 uu_avl_pool_t *fsps_who_perm_avl_pool;
3840 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3843 static inline const char *
3844 deleg_perm_type(zfs_deleg_note_t note)
3846 /* subcommands */
3847 switch (note) {
3848 /* SUBCOMMANDS */
3849 /* OTHER */
3850 case ZFS_DELEG_NOTE_GROUPQUOTA:
3851 case ZFS_DELEG_NOTE_GROUPUSED:
3852 case ZFS_DELEG_NOTE_USERPROP:
3853 case ZFS_DELEG_NOTE_USERQUOTA:
3854 case ZFS_DELEG_NOTE_USERUSED:
3855 /* other */
3856 return (gettext("other"));
3857 default:
3858 return (gettext("subcommand"));
3862 static int inline
3863 who_type2weight(zfs_deleg_who_type_t who_type)
3865 int res;
3866 switch (who_type) {
3867 case ZFS_DELEG_NAMED_SET_SETS:
3868 case ZFS_DELEG_NAMED_SET:
3869 res = 0;
3870 break;
3871 case ZFS_DELEG_CREATE_SETS:
3872 case ZFS_DELEG_CREATE:
3873 res = 1;
3874 break;
3875 case ZFS_DELEG_USER_SETS:
3876 case ZFS_DELEG_USER:
3877 res = 2;
3878 break;
3879 case ZFS_DELEG_GROUP_SETS:
3880 case ZFS_DELEG_GROUP:
3881 res = 3;
3882 break;
3883 case ZFS_DELEG_EVERYONE_SETS:
3884 case ZFS_DELEG_EVERYONE:
3885 res = 4;
3886 break;
3887 default:
3888 res = -1;
3891 return (res);
3894 /* ARGSUSED */
3895 static int
3896 who_perm_compare(const void *larg, const void *rarg, void *unused)
3898 const who_perm_node_t *l = larg;
3899 const who_perm_node_t *r = rarg;
3900 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3901 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3902 int lweight = who_type2weight(ltype);
3903 int rweight = who_type2weight(rtype);
3904 int res = lweight - rweight;
3905 if (res == 0)
3906 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3907 ZFS_MAX_DELEG_NAME-1);
3909 if (res == 0)
3910 return (0);
3911 if (res > 0)
3912 return (1);
3913 else
3914 return (-1);
3917 /* ARGSUSED */
3918 static int
3919 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3921 const deleg_perm_node_t *l = larg;
3922 const deleg_perm_node_t *r = rarg;
3923 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3924 ZFS_MAX_DELEG_NAME-1);
3926 if (res == 0)
3927 return (0);
3929 if (res > 0)
3930 return (1);
3931 else
3932 return (-1);
3935 static inline void
3936 fs_perm_set_init(fs_perm_set_t *fspset)
3938 bzero(fspset, sizeof (fs_perm_set_t));
3940 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3941 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3942 NULL, UU_DEFAULT)) == NULL)
3943 nomem();
3944 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3945 UU_DEFAULT)) == NULL)
3946 nomem();
3948 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3949 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3950 who_perm_node_t, who_avl_node), who_perm_compare,
3951 UU_DEFAULT)) == NULL)
3952 nomem();
3954 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3955 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3956 who_perm_node_t, who_avl_node), who_perm_compare,
3957 UU_DEFAULT)) == NULL)
3958 nomem();
3960 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3961 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3962 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3963 == NULL)
3964 nomem();
3967 static inline void fs_perm_fini(fs_perm_t *);
3968 static inline void who_perm_fini(who_perm_t *);
3970 static inline void
3971 fs_perm_set_fini(fs_perm_set_t *fspset)
3973 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
3975 while (node != NULL) {
3976 fs_perm_node_t *next_node =
3977 uu_list_next(fspset->fsps_list, node);
3978 fs_perm_t *fsperm = &node->fspn_fsperm;
3979 fs_perm_fini(fsperm);
3980 uu_list_remove(fspset->fsps_list, node);
3981 free(node);
3982 node = next_node;
3985 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
3986 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
3987 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
3990 static inline void
3991 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
3992 const char *name)
3994 deleg_perm->dp_who_type = type;
3995 deleg_perm->dp_name = name;
3998 static inline void
3999 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4000 zfs_deleg_who_type_t type, const char *name)
4002 uu_avl_pool_t *pool;
4003 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4005 bzero(who_perm, sizeof (who_perm_t));
4007 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4008 UU_DEFAULT)) == NULL)
4009 nomem();
4011 who_perm->who_type = type;
4012 who_perm->who_name = name;
4013 who_perm->who_fsperm = fsperm;
4016 static inline void
4017 who_perm_fini(who_perm_t *who_perm)
4019 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4021 while (node != NULL) {
4022 deleg_perm_node_t *next_node =
4023 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4025 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4026 free(node);
4027 node = next_node;
4030 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4033 static inline void
4034 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4036 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4037 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4039 bzero(fsperm, sizeof (fs_perm_t));
4041 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4042 == NULL)
4043 nomem();
4045 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4046 == NULL)
4047 nomem();
4049 fsperm->fsp_set = fspset;
4050 fsperm->fsp_name = fsname;
4053 static inline void
4054 fs_perm_fini(fs_perm_t *fsperm)
4056 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4057 while (node != NULL) {
4058 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4059 node);
4060 who_perm_t *who_perm = &node->who_perm;
4061 who_perm_fini(who_perm);
4062 uu_avl_remove(fsperm->fsp_sc_avl, node);
4063 free(node);
4064 node = next_node;
4067 node = uu_avl_first(fsperm->fsp_uge_avl);
4068 while (node != NULL) {
4069 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4070 node);
4071 who_perm_t *who_perm = &node->who_perm;
4072 who_perm_fini(who_perm);
4073 uu_avl_remove(fsperm->fsp_uge_avl, node);
4074 free(node);
4075 node = next_node;
4078 uu_avl_destroy(fsperm->fsp_sc_avl);
4079 uu_avl_destroy(fsperm->fsp_uge_avl);
4082 static void inline
4083 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4084 zfs_deleg_who_type_t who_type, const char *name, char locality)
4086 uu_avl_index_t idx = 0;
4088 deleg_perm_node_t *found_node = NULL;
4089 deleg_perm_t *deleg_perm = &node->dpn_perm;
4091 deleg_perm_init(deleg_perm, who_type, name);
4093 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4094 == NULL)
4095 uu_avl_insert(avl, node, idx);
4096 else {
4097 node = found_node;
4098 deleg_perm = &node->dpn_perm;
4102 switch (locality) {
4103 case ZFS_DELEG_LOCAL:
4104 deleg_perm->dp_local = B_TRUE;
4105 break;
4106 case ZFS_DELEG_DESCENDENT:
4107 deleg_perm->dp_descend = B_TRUE;
4108 break;
4109 case ZFS_DELEG_NA:
4110 break;
4111 default:
4112 assert(B_FALSE); /* invalid locality */
4116 static inline int
4117 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4119 nvpair_t *nvp = NULL;
4120 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4121 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4122 zfs_deleg_who_type_t who_type = who_perm->who_type;
4124 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4125 const char *name = nvpair_name(nvp);
4126 data_type_t type = nvpair_type(nvp);
4127 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4128 deleg_perm_node_t *node =
4129 safe_malloc(sizeof (deleg_perm_node_t));
4131 assert(type == DATA_TYPE_BOOLEAN);
4133 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4134 set_deleg_perm_node(avl, node, who_type, name, locality);
4137 return (0);
4140 static inline int
4141 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4143 nvpair_t *nvp = NULL;
4144 fs_perm_set_t *fspset = fsperm->fsp_set;
4146 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4147 nvlist_t *nvl2 = NULL;
4148 const char *name = nvpair_name(nvp);
4149 uu_avl_t *avl = NULL;
4150 uu_avl_pool_t *avl_pool;
4151 zfs_deleg_who_type_t perm_type = name[0];
4152 char perm_locality = name[1];
4153 const char *perm_name = name + 3;
4154 boolean_t is_set = B_TRUE;
4155 who_perm_t *who_perm = NULL;
4157 assert('$' == name[2]);
4159 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4160 return (-1);
4162 switch (perm_type) {
4163 case ZFS_DELEG_CREATE:
4164 case ZFS_DELEG_CREATE_SETS:
4165 case ZFS_DELEG_NAMED_SET:
4166 case ZFS_DELEG_NAMED_SET_SETS:
4167 avl_pool = fspset->fsps_named_set_avl_pool;
4168 avl = fsperm->fsp_sc_avl;
4169 break;
4170 case ZFS_DELEG_USER:
4171 case ZFS_DELEG_USER_SETS:
4172 case ZFS_DELEG_GROUP:
4173 case ZFS_DELEG_GROUP_SETS:
4174 case ZFS_DELEG_EVERYONE:
4175 case ZFS_DELEG_EVERYONE_SETS:
4176 avl_pool = fspset->fsps_who_perm_avl_pool;
4177 avl = fsperm->fsp_uge_avl;
4178 break;
4181 if (is_set) {
4182 who_perm_node_t *found_node = NULL;
4183 who_perm_node_t *node = safe_malloc(
4184 sizeof (who_perm_node_t));
4185 who_perm = &node->who_perm;
4186 uu_avl_index_t idx = 0;
4188 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4189 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4191 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4192 == NULL) {
4193 if (avl == fsperm->fsp_uge_avl) {
4194 uid_t rid = 0;
4195 struct passwd *p = NULL;
4196 struct group *g = NULL;
4197 const char *nice_name = NULL;
4199 switch (perm_type) {
4200 case ZFS_DELEG_USER_SETS:
4201 case ZFS_DELEG_USER:
4202 rid = atoi(perm_name);
4203 p = getpwuid(rid);
4204 if (p)
4205 nice_name = p->pw_name;
4206 break;
4207 case ZFS_DELEG_GROUP_SETS:
4208 case ZFS_DELEG_GROUP:
4209 rid = atoi(perm_name);
4210 g = getgrgid(rid);
4211 if (g)
4212 nice_name = g->gr_name;
4213 break;
4216 if (nice_name != NULL)
4217 (void) strlcpy(
4218 node->who_perm.who_ug_name,
4219 nice_name, 256);
4222 uu_avl_insert(avl, node, idx);
4223 } else {
4224 node = found_node;
4225 who_perm = &node->who_perm;
4229 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4232 return (0);
4235 static inline int
4236 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4238 nvpair_t *nvp = NULL;
4239 uu_avl_index_t idx = 0;
4241 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4242 nvlist_t *nvl2 = NULL;
4243 const char *fsname = nvpair_name(nvp);
4244 data_type_t type = nvpair_type(nvp);
4245 fs_perm_t *fsperm = NULL;
4246 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4247 if (node == NULL)
4248 nomem();
4250 fsperm = &node->fspn_fsperm;
4252 assert(DATA_TYPE_NVLIST == type);
4254 uu_list_node_init(node, &node->fspn_list_node,
4255 fspset->fsps_list_pool);
4257 idx = uu_list_numnodes(fspset->fsps_list);
4258 fs_perm_init(fsperm, fspset, fsname);
4260 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4261 return (-1);
4263 (void) parse_fs_perm(fsperm, nvl2);
4265 uu_list_insert(fspset->fsps_list, node, idx);
4268 return (0);
4271 static inline const char *
4272 deleg_perm_comment(zfs_deleg_note_t note)
4274 const char *str = "";
4276 /* subcommands */
4277 switch (note) {
4278 /* SUBCOMMANDS */
4279 case ZFS_DELEG_NOTE_ALLOW:
4280 str = gettext("Must also have the permission that is being"
4281 "\n\t\t\t\tallowed");
4282 break;
4283 case ZFS_DELEG_NOTE_CLONE:
4284 str = gettext("Must also have the 'create' ability and 'mount'"
4285 "\n\t\t\t\tability in the origin file system");
4286 break;
4287 case ZFS_DELEG_NOTE_CREATE:
4288 str = gettext("Must also have the 'mount' ability");
4289 break;
4290 case ZFS_DELEG_NOTE_DESTROY:
4291 str = gettext("Must also have the 'mount' ability");
4292 break;
4293 case ZFS_DELEG_NOTE_DIFF:
4294 str = gettext("Allows lookup of paths within a dataset;"
4295 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4296 "\n\t\t\t\tin order to use zfs diff");
4297 break;
4298 case ZFS_DELEG_NOTE_HOLD:
4299 str = gettext("Allows adding a user hold to a snapshot");
4300 break;
4301 case ZFS_DELEG_NOTE_MOUNT:
4302 str = gettext("Allows mount/umount of ZFS datasets");
4303 break;
4304 case ZFS_DELEG_NOTE_PROMOTE:
4305 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4306 " 'promote' ability in the origin file system");
4307 break;
4308 case ZFS_DELEG_NOTE_RECEIVE:
4309 str = gettext("Must also have the 'mount' and 'create'"
4310 " ability");
4311 break;
4312 case ZFS_DELEG_NOTE_RELEASE:
4313 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4314 "might destroy the snapshot");
4315 break;
4316 case ZFS_DELEG_NOTE_RENAME:
4317 str = gettext("Must also have the 'mount' and 'create'"
4318 "\n\t\t\t\tability in the new parent");
4319 break;
4320 case ZFS_DELEG_NOTE_ROLLBACK:
4321 str = gettext("");
4322 break;
4323 case ZFS_DELEG_NOTE_SEND:
4324 str = gettext("");
4325 break;
4326 case ZFS_DELEG_NOTE_SHARE:
4327 str = gettext("Allows sharing file systems over NFS or SMB"
4328 "\n\t\t\t\tprotocols");
4329 break;
4330 case ZFS_DELEG_NOTE_SNAPSHOT:
4331 str = gettext("");
4332 break;
4334 * case ZFS_DELEG_NOTE_VSCAN:
4335 * str = gettext("");
4336 * break;
4338 /* OTHER */
4339 case ZFS_DELEG_NOTE_GROUPQUOTA:
4340 str = gettext("Allows accessing any groupquota@... property");
4341 break;
4342 case ZFS_DELEG_NOTE_GROUPUSED:
4343 str = gettext("Allows reading any groupused@... property");
4344 break;
4345 case ZFS_DELEG_NOTE_USERPROP:
4346 str = gettext("Allows changing any user property");
4347 break;
4348 case ZFS_DELEG_NOTE_USERQUOTA:
4349 str = gettext("Allows accessing any userquota@... property");
4350 break;
4351 case ZFS_DELEG_NOTE_USERUSED:
4352 str = gettext("Allows reading any userused@... property");
4353 break;
4354 /* other */
4355 default:
4356 str = "";
4359 return (str);
4362 struct allow_opts {
4363 boolean_t local;
4364 boolean_t descend;
4365 boolean_t user;
4366 boolean_t group;
4367 boolean_t everyone;
4368 boolean_t create;
4369 boolean_t set;
4370 boolean_t recursive; /* unallow only */
4371 boolean_t prt_usage;
4373 boolean_t prt_perms;
4374 char *who;
4375 char *perms;
4376 const char *dataset;
4379 static inline int
4380 prop_cmp(const void *a, const void *b)
4382 const char *str1 = *(const char **)a;
4383 const char *str2 = *(const char **)b;
4384 return (strcmp(str1, str2));
4387 static void
4388 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4390 const char *opt_desc[] = {
4391 "-h", gettext("show this help message and exit"),
4392 "-l", gettext("set permission locally"),
4393 "-d", gettext("set permission for descents"),
4394 "-u", gettext("set permission for user"),
4395 "-g", gettext("set permission for group"),
4396 "-e", gettext("set permission for everyone"),
4397 "-c", gettext("set create time permission"),
4398 "-s", gettext("define permission set"),
4399 /* unallow only */
4400 "-r", gettext("remove permissions recursively"),
4402 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4403 size_t allow_size = unallow_size - 2;
4404 const char *props[ZFS_NUM_PROPS];
4405 int i;
4406 size_t count = 0;
4407 FILE *fp = requested ? stdout : stderr;
4408 zprop_desc_t *pdtbl = zfs_prop_get_table();
4409 const char *fmt = gettext("%-16s %-14s\t%s\n");
4411 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4412 HELP_ALLOW));
4413 (void) fprintf(fp, gettext("Options:\n"));
4414 for (int i = 0; i < (un ? unallow_size : allow_size); i++) {
4415 const char *opt = opt_desc[i++];
4416 const char *optdsc = opt_desc[i];
4417 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4420 (void) fprintf(fp, gettext("\nThe following permissions are "
4421 "supported:\n\n"));
4422 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4423 gettext("NOTES"));
4424 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4425 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4426 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4427 const char *perm_type = deleg_perm_type(perm_note);
4428 const char *perm_comment = deleg_perm_comment(perm_note);
4429 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4432 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4433 zprop_desc_t *pd = &pdtbl[i];
4434 if (pd->pd_visible != B_TRUE)
4435 continue;
4437 if (pd->pd_attr == PROP_READONLY)
4438 continue;
4440 props[count++] = pd->pd_name;
4442 props[count] = NULL;
4444 qsort(props, count, sizeof (char *), prop_cmp);
4446 for (i = 0; i < count; i++)
4447 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4449 if (msg != NULL)
4450 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4452 exit(requested ? 0 : 2);
4455 static inline const char *
4456 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4457 char **permsp)
4459 if (un && argc == expected_argc - 1)
4460 *permsp = NULL;
4461 else if (argc == expected_argc)
4462 *permsp = argv[argc - 2];
4463 else
4464 allow_usage(un, B_FALSE,
4465 gettext("wrong number of parameters\n"));
4467 return (argv[argc - 1]);
4470 static void
4471 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4473 int uge_sum = opts->user + opts->group + opts->everyone;
4474 int csuge_sum = opts->create + opts->set + uge_sum;
4475 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4476 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4478 if (uge_sum > 1)
4479 allow_usage(un, B_FALSE,
4480 gettext("-u, -g, and -e are mutually exclusive\n"));
4482 if (opts->prt_usage)
4483 if (argc == 0 && all_sum == 0)
4484 allow_usage(un, B_TRUE, NULL);
4485 else
4486 usage(B_FALSE);
4488 if (opts->set) {
4489 if (csuge_sum > 1)
4490 allow_usage(un, B_FALSE,
4491 gettext("invalid options combined with -s\n"));
4493 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4494 if (argv[0][0] != '@')
4495 allow_usage(un, B_FALSE,
4496 gettext("invalid set name: missing '@' prefix\n"));
4497 opts->who = argv[0];
4498 } else if (opts->create) {
4499 if (ldcsuge_sum > 1)
4500 allow_usage(un, B_FALSE,
4501 gettext("invalid options combined with -c\n"));
4502 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4503 } else if (opts->everyone) {
4504 if (csuge_sum > 1)
4505 allow_usage(un, B_FALSE,
4506 gettext("invalid options combined with -e\n"));
4507 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4508 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4509 == 0) {
4510 opts->everyone = B_TRUE;
4511 argc--;
4512 argv++;
4513 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4514 } else if (argc == 1 && !un) {
4515 opts->prt_perms = B_TRUE;
4516 opts->dataset = argv[argc-1];
4517 } else {
4518 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4519 opts->who = argv[0];
4522 if (!opts->local && !opts->descend) {
4523 opts->local = B_TRUE;
4524 opts->descend = B_TRUE;
4528 static void
4529 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4530 const char *who, char *perms, nvlist_t *top_nvl)
4532 int i;
4533 char ld[2] = { '\0', '\0' };
4534 char who_buf[ZFS_MAXNAMELEN+32];
4535 char base_type;
4536 char set_type;
4537 nvlist_t *base_nvl = NULL;
4538 nvlist_t *set_nvl = NULL;
4539 nvlist_t *nvl;
4541 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4542 nomem();
4543 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4544 nomem();
4546 switch (type) {
4547 case ZFS_DELEG_NAMED_SET_SETS:
4548 case ZFS_DELEG_NAMED_SET:
4549 set_type = ZFS_DELEG_NAMED_SET_SETS;
4550 base_type = ZFS_DELEG_NAMED_SET;
4551 ld[0] = ZFS_DELEG_NA;
4552 break;
4553 case ZFS_DELEG_CREATE_SETS:
4554 case ZFS_DELEG_CREATE:
4555 set_type = ZFS_DELEG_CREATE_SETS;
4556 base_type = ZFS_DELEG_CREATE;
4557 ld[0] = ZFS_DELEG_NA;
4558 break;
4559 case ZFS_DELEG_USER_SETS:
4560 case ZFS_DELEG_USER:
4561 set_type = ZFS_DELEG_USER_SETS;
4562 base_type = ZFS_DELEG_USER;
4563 if (local)
4564 ld[0] = ZFS_DELEG_LOCAL;
4565 if (descend)
4566 ld[1] = ZFS_DELEG_DESCENDENT;
4567 break;
4568 case ZFS_DELEG_GROUP_SETS:
4569 case ZFS_DELEG_GROUP:
4570 set_type = ZFS_DELEG_GROUP_SETS;
4571 base_type = ZFS_DELEG_GROUP;
4572 if (local)
4573 ld[0] = ZFS_DELEG_LOCAL;
4574 if (descend)
4575 ld[1] = ZFS_DELEG_DESCENDENT;
4576 break;
4577 case ZFS_DELEG_EVERYONE_SETS:
4578 case ZFS_DELEG_EVERYONE:
4579 set_type = ZFS_DELEG_EVERYONE_SETS;
4580 base_type = ZFS_DELEG_EVERYONE;
4581 if (local)
4582 ld[0] = ZFS_DELEG_LOCAL;
4583 if (descend)
4584 ld[1] = ZFS_DELEG_DESCENDENT;
4587 if (perms != NULL) {
4588 char *curr = perms;
4589 char *end = curr + strlen(perms);
4591 while (curr < end) {
4592 char *delim = strchr(curr, ',');
4593 if (delim == NULL)
4594 delim = end;
4595 else
4596 *delim = '\0';
4598 if (curr[0] == '@')
4599 nvl = set_nvl;
4600 else
4601 nvl = base_nvl;
4603 (void) nvlist_add_boolean(nvl, curr);
4604 if (delim != end)
4605 *delim = ',';
4606 curr = delim + 1;
4609 for (i = 0; i < 2; i++) {
4610 char locality = ld[i];
4611 if (locality == 0)
4612 continue;
4614 if (!nvlist_empty(base_nvl)) {
4615 if (who != NULL)
4616 (void) snprintf(who_buf,
4617 sizeof (who_buf), "%c%c$%s",
4618 base_type, locality, who);
4619 else
4620 (void) snprintf(who_buf,
4621 sizeof (who_buf), "%c%c$",
4622 base_type, locality);
4624 (void) nvlist_add_nvlist(top_nvl, who_buf,
4625 base_nvl);
4629 if (!nvlist_empty(set_nvl)) {
4630 if (who != NULL)
4631 (void) snprintf(who_buf,
4632 sizeof (who_buf), "%c%c$%s",
4633 set_type, locality, who);
4634 else
4635 (void) snprintf(who_buf,
4636 sizeof (who_buf), "%c%c$",
4637 set_type, locality);
4639 (void) nvlist_add_nvlist(top_nvl, who_buf,
4640 set_nvl);
4643 } else {
4644 for (i = 0; i < 2; i++) {
4645 char locality = ld[i];
4646 if (locality == 0)
4647 continue;
4649 if (who != NULL)
4650 (void) snprintf(who_buf, sizeof (who_buf),
4651 "%c%c$%s", base_type, locality, who);
4652 else
4653 (void) snprintf(who_buf, sizeof (who_buf),
4654 "%c%c$", base_type, locality);
4655 (void) nvlist_add_boolean(top_nvl, who_buf);
4657 if (who != NULL)
4658 (void) snprintf(who_buf, sizeof (who_buf),
4659 "%c%c$%s", set_type, locality, who);
4660 else
4661 (void) snprintf(who_buf, sizeof (who_buf),
4662 "%c%c$", set_type, locality);
4663 (void) nvlist_add_boolean(top_nvl, who_buf);
4668 static int
4669 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4671 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4672 nomem();
4674 if (opts->set) {
4675 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4676 opts->descend, opts->who, opts->perms, *nvlp);
4677 } else if (opts->create) {
4678 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4679 opts->descend, NULL, opts->perms, *nvlp);
4680 } else if (opts->everyone) {
4681 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4682 opts->descend, NULL, opts->perms, *nvlp);
4683 } else {
4684 char *curr = opts->who;
4685 char *end = curr + strlen(curr);
4687 while (curr < end) {
4688 const char *who;
4689 zfs_deleg_who_type_t who_type;
4690 char *endch;
4691 char *delim = strchr(curr, ',');
4692 char errbuf[256];
4693 char id[64];
4694 struct passwd *p = NULL;
4695 struct group *g = NULL;
4697 uid_t rid;
4698 if (delim == NULL)
4699 delim = end;
4700 else
4701 *delim = '\0';
4703 rid = (uid_t)strtol(curr, &endch, 0);
4704 if (opts->user) {
4705 who_type = ZFS_DELEG_USER;
4706 if (*endch != '\0')
4707 p = getpwnam(curr);
4708 else
4709 p = getpwuid(rid);
4711 if (p != NULL)
4712 rid = p->pw_uid;
4713 else {
4714 (void) snprintf(errbuf, 256, gettext(
4715 "invalid user %s"), curr);
4716 allow_usage(un, B_TRUE, errbuf);
4718 } else if (opts->group) {
4719 who_type = ZFS_DELEG_GROUP;
4720 if (*endch != '\0')
4721 g = getgrnam(curr);
4722 else
4723 g = getgrgid(rid);
4725 if (g != NULL)
4726 rid = g->gr_gid;
4727 else {
4728 (void) snprintf(errbuf, 256, gettext(
4729 "invalid group %s"), curr);
4730 allow_usage(un, B_TRUE, errbuf);
4732 } else {
4733 if (*endch != '\0') {
4734 p = getpwnam(curr);
4735 } else {
4736 p = getpwuid(rid);
4739 if (p == NULL)
4740 if (*endch != '\0') {
4741 g = getgrnam(curr);
4742 } else {
4743 g = getgrgid(rid);
4746 if (p != NULL) {
4747 who_type = ZFS_DELEG_USER;
4748 rid = p->pw_uid;
4749 } else if (g != NULL) {
4750 who_type = ZFS_DELEG_GROUP;
4751 rid = g->gr_gid;
4752 } else {
4753 (void) snprintf(errbuf, 256, gettext(
4754 "invalid user/group %s"), curr);
4755 allow_usage(un, B_TRUE, errbuf);
4759 (void) sprintf(id, "%u", rid);
4760 who = id;
4762 store_allow_perm(who_type, opts->local,
4763 opts->descend, who, opts->perms, *nvlp);
4764 curr = delim + 1;
4768 return (0);
4771 static void
4772 print_set_creat_perms(uu_avl_t *who_avl)
4774 const char *sc_title[] = {
4775 gettext("Permission sets:\n"),
4776 gettext("Create time permissions:\n"),
4777 NULL
4779 const char **title_ptr = sc_title;
4780 who_perm_node_t *who_node = NULL;
4781 int prev_weight = -1;
4783 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4784 who_node = uu_avl_next(who_avl, who_node)) {
4785 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4786 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4787 const char *who_name = who_node->who_perm.who_name;
4788 int weight = who_type2weight(who_type);
4789 boolean_t first = B_TRUE;
4790 deleg_perm_node_t *deleg_node;
4792 if (prev_weight != weight) {
4793 (void) printf(*title_ptr++);
4794 prev_weight = weight;
4797 if (who_name == NULL || strnlen(who_name, 1) == 0)
4798 (void) printf("\t");
4799 else
4800 (void) printf("\t%s ", who_name);
4802 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4803 deleg_node = uu_avl_next(avl, deleg_node)) {
4804 if (first) {
4805 (void) printf("%s",
4806 deleg_node->dpn_perm.dp_name);
4807 first = B_FALSE;
4808 } else
4809 (void) printf(",%s",
4810 deleg_node->dpn_perm.dp_name);
4813 (void) printf("\n");
4817 static void inline
4818 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4819 const char *title)
4821 who_perm_node_t *who_node = NULL;
4822 boolean_t prt_title = B_TRUE;
4823 uu_avl_walk_t *walk;
4825 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4826 nomem();
4828 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4829 const char *who_name = who_node->who_perm.who_name;
4830 const char *nice_who_name = who_node->who_perm.who_ug_name;
4831 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4832 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4833 char delim = ' ';
4834 deleg_perm_node_t *deleg_node;
4835 boolean_t prt_who = B_TRUE;
4837 for (deleg_node = uu_avl_first(avl);
4838 deleg_node != NULL;
4839 deleg_node = uu_avl_next(avl, deleg_node)) {
4840 if (local != deleg_node->dpn_perm.dp_local ||
4841 descend != deleg_node->dpn_perm.dp_descend)
4842 continue;
4844 if (prt_who) {
4845 const char *who = NULL;
4846 if (prt_title) {
4847 prt_title = B_FALSE;
4848 (void) printf(title);
4851 switch (who_type) {
4852 case ZFS_DELEG_USER_SETS:
4853 case ZFS_DELEG_USER:
4854 who = gettext("user");
4855 if (nice_who_name)
4856 who_name = nice_who_name;
4857 break;
4858 case ZFS_DELEG_GROUP_SETS:
4859 case ZFS_DELEG_GROUP:
4860 who = gettext("group");
4861 if (nice_who_name)
4862 who_name = nice_who_name;
4863 break;
4864 case ZFS_DELEG_EVERYONE_SETS:
4865 case ZFS_DELEG_EVERYONE:
4866 who = gettext("everyone");
4867 who_name = NULL;
4870 prt_who = B_FALSE;
4871 if (who_name == NULL)
4872 (void) printf("\t%s", who);
4873 else
4874 (void) printf("\t%s %s", who, who_name);
4877 (void) printf("%c%s", delim,
4878 deleg_node->dpn_perm.dp_name);
4879 delim = ',';
4882 if (!prt_who)
4883 (void) printf("\n");
4886 uu_avl_walk_end(walk);
4889 static void
4890 print_fs_perms(fs_perm_set_t *fspset)
4892 fs_perm_node_t *node = NULL;
4893 char buf[ZFS_MAXNAMELEN+32];
4894 const char *dsname = buf;
4896 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4897 node = uu_list_next(fspset->fsps_list, node)) {
4898 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4899 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4900 int left = 0;
4902 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4903 gettext("---- Permissions on %s "),
4904 node->fspn_fsperm.fsp_name);
4905 (void) printf(dsname);
4906 left = 70 - strlen(buf);
4907 while (left-- > 0)
4908 (void) printf("-");
4909 (void) printf("\n");
4911 print_set_creat_perms(sc_avl);
4912 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4913 gettext("Local permissions:\n"));
4914 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4915 gettext("Descendent permissions:\n"));
4916 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4917 gettext("Local+Descendent permissions:\n"));
4921 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4923 struct deleg_perms {
4924 boolean_t un;
4925 nvlist_t *nvl;
4928 static int
4929 set_deleg_perms(zfs_handle_t *zhp, void *data)
4931 struct deleg_perms *perms = (struct deleg_perms *)data;
4932 zfs_type_t zfs_type = zfs_get_type(zhp);
4934 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4935 return (0);
4937 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4940 static int
4941 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4943 zfs_handle_t *zhp;
4944 nvlist_t *perm_nvl = NULL;
4945 nvlist_t *update_perm_nvl = NULL;
4946 int error = 1;
4947 int c;
4948 struct allow_opts opts = { 0 };
4950 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4952 /* check opts */
4953 while ((c = getopt(argc, argv, optstr)) != -1) {
4954 switch (c) {
4955 case 'l':
4956 opts.local = B_TRUE;
4957 break;
4958 case 'd':
4959 opts.descend = B_TRUE;
4960 break;
4961 case 'u':
4962 opts.user = B_TRUE;
4963 break;
4964 case 'g':
4965 opts.group = B_TRUE;
4966 break;
4967 case 'e':
4968 opts.everyone = B_TRUE;
4969 break;
4970 case 's':
4971 opts.set = B_TRUE;
4972 break;
4973 case 'c':
4974 opts.create = B_TRUE;
4975 break;
4976 case 'r':
4977 opts.recursive = B_TRUE;
4978 break;
4979 case ':':
4980 (void) fprintf(stderr, gettext("missing argument for "
4981 "'%c' option\n"), optopt);
4982 usage(B_FALSE);
4983 break;
4984 case 'h':
4985 opts.prt_usage = B_TRUE;
4986 break;
4987 case '?':
4988 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4989 optopt);
4990 usage(B_FALSE);
4994 argc -= optind;
4995 argv += optind;
4997 /* check arguments */
4998 parse_allow_args(argc, argv, un, &opts);
5000 /* try to open the dataset */
5001 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5002 ZFS_TYPE_VOLUME)) == NULL) {
5003 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5004 opts.dataset);
5005 return (-1);
5008 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5009 goto cleanup2;
5011 fs_perm_set_init(&fs_perm_set);
5012 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5013 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5014 goto cleanup1;
5017 if (opts.prt_perms)
5018 print_fs_perms(&fs_perm_set);
5019 else {
5020 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5021 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5022 goto cleanup0;
5024 if (un && opts.recursive) {
5025 struct deleg_perms data = { un, update_perm_nvl };
5026 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5027 &data) != 0)
5028 goto cleanup0;
5032 error = 0;
5034 cleanup0:
5035 nvlist_free(perm_nvl);
5036 if (update_perm_nvl != NULL)
5037 nvlist_free(update_perm_nvl);
5038 cleanup1:
5039 fs_perm_set_fini(&fs_perm_set);
5040 cleanup2:
5041 zfs_close(zhp);
5043 return (error);
5047 * zfs allow [-r] [-t] <tag> <snap> ...
5049 * -r Recursively hold
5050 * -t Temporary hold (hidden option)
5052 * Apply a user-hold with the given tag to the list of snapshots.
5054 static int
5055 zfs_do_allow(int argc, char **argv)
5057 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5061 * zfs unallow [-r] [-t] <tag> <snap> ...
5063 * -r Recursively hold
5064 * -t Temporary hold (hidden option)
5066 * Apply a user-hold with the given tag to the list of snapshots.
5068 static int
5069 zfs_do_unallow(int argc, char **argv)
5071 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5074 static int
5075 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5077 int errors = 0;
5078 int i;
5079 const char *tag;
5080 boolean_t recursive = B_FALSE;
5081 boolean_t temphold = B_FALSE;
5082 const char *opts = holding ? "rt" : "r";
5083 int c;
5085 /* check options */
5086 while ((c = getopt(argc, argv, opts)) != -1) {
5087 switch (c) {
5088 case 'r':
5089 recursive = B_TRUE;
5090 break;
5091 case 't':
5092 temphold = B_TRUE;
5093 break;
5094 case '?':
5095 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5096 optopt);
5097 usage(B_FALSE);
5101 argc -= optind;
5102 argv += optind;
5104 /* check number of arguments */
5105 if (argc < 2)
5106 usage(B_FALSE);
5108 tag = argv[0];
5109 --argc;
5110 ++argv;
5112 if (holding && tag[0] == '.') {
5113 /* tags starting with '.' are reserved for libzfs */
5114 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5115 usage(B_FALSE);
5118 for (i = 0; i < argc; ++i) {
5119 zfs_handle_t *zhp;
5120 char parent[ZFS_MAXNAMELEN];
5121 const char *delim;
5122 char *path = argv[i];
5124 delim = strchr(path, '@');
5125 if (delim == NULL) {
5126 (void) fprintf(stderr,
5127 gettext("'%s' is not a snapshot\n"), path);
5128 ++errors;
5129 continue;
5131 (void) strncpy(parent, path, delim - path);
5132 parent[delim - path] = '\0';
5134 zhp = zfs_open(g_zfs, parent,
5135 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5136 if (zhp == NULL) {
5137 ++errors;
5138 continue;
5140 if (holding) {
5141 if (zfs_hold(zhp, delim+1, tag, recursive,
5142 temphold, B_FALSE, -1, 0, 0) != 0)
5143 ++errors;
5144 } else {
5145 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5146 ++errors;
5148 zfs_close(zhp);
5151 return (errors != 0);
5155 * zfs hold [-r] [-t] <tag> <snap> ...
5157 * -r Recursively hold
5158 * -t Temporary hold (hidden option)
5160 * Apply a user-hold with the given tag to the list of snapshots.
5162 static int
5163 zfs_do_hold(int argc, char **argv)
5165 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5169 * zfs release [-r] <tag> <snap> ...
5171 * -r Recursively release
5173 * Release a user-hold with the given tag from the list of snapshots.
5175 static int
5176 zfs_do_release(int argc, char **argv)
5178 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5181 typedef struct holds_cbdata {
5182 boolean_t cb_recursive;
5183 const char *cb_snapname;
5184 nvlist_t **cb_nvlp;
5185 size_t cb_max_namelen;
5186 size_t cb_max_taglen;
5187 } holds_cbdata_t;
5189 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5190 #define DATETIME_BUF_LEN (32)
5194 static void
5195 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5197 int i;
5198 nvpair_t *nvp = NULL;
5199 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5200 const char *col;
5202 if (!scripted) {
5203 for (i = 0; i < 3; i++) {
5204 col = gettext(hdr_cols[i]);
5205 if (i < 2)
5206 (void) printf("%-*s ", i ? tagwidth : nwidth,
5207 col);
5208 else
5209 (void) printf("%s\n", col);
5213 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5214 char *zname = nvpair_name(nvp);
5215 nvlist_t *nvl2;
5216 nvpair_t *nvp2 = NULL;
5217 (void) nvpair_value_nvlist(nvp, &nvl2);
5218 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5219 char tsbuf[DATETIME_BUF_LEN];
5220 char *tagname = nvpair_name(nvp2);
5221 uint64_t val = 0;
5222 time_t time;
5223 struct tm t;
5224 char sep = scripted ? '\t' : ' ';
5225 size_t sepnum = scripted ? 1 : 2;
5227 (void) nvpair_value_uint64(nvp2, &val);
5228 time = (time_t)val;
5229 (void) localtime_r(&time, &t);
5230 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5231 gettext(STRFTIME_FMT_STR), &t);
5233 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5234 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5240 * Generic callback function to list a dataset or snapshot.
5242 static int
5243 holds_callback(zfs_handle_t *zhp, void *data)
5245 holds_cbdata_t *cbp = data;
5246 nvlist_t *top_nvl = *cbp->cb_nvlp;
5247 nvlist_t *nvl = NULL;
5248 nvpair_t *nvp = NULL;
5249 const char *zname = zfs_get_name(zhp);
5250 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5252 if (cbp->cb_recursive) {
5253 const char *snapname;
5254 char *delim = strchr(zname, '@');
5255 if (delim == NULL)
5256 return (0);
5258 snapname = delim + 1;
5259 if (strcmp(cbp->cb_snapname, snapname))
5260 return (0);
5263 if (zfs_get_holds(zhp, &nvl) != 0)
5264 return (-1);
5266 if (znamelen > cbp->cb_max_namelen)
5267 cbp->cb_max_namelen = znamelen;
5269 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5270 const char *tag = nvpair_name(nvp);
5271 size_t taglen = strnlen(tag, MAXNAMELEN);
5272 if (taglen > cbp->cb_max_taglen)
5273 cbp->cb_max_taglen = taglen;
5276 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5280 * zfs holds [-r] <snap> ...
5282 * -r Recursively hold
5284 static int
5285 zfs_do_holds(int argc, char **argv)
5287 int errors = 0;
5288 int c;
5289 int i;
5290 boolean_t scripted = B_FALSE;
5291 boolean_t recursive = B_FALSE;
5292 const char *opts = "rH";
5293 nvlist_t *nvl;
5295 int types = ZFS_TYPE_SNAPSHOT;
5296 holds_cbdata_t cb = { 0 };
5298 int limit = 0;
5299 int ret = 0;
5300 int flags = 0;
5302 /* check options */
5303 while ((c = getopt(argc, argv, opts)) != -1) {
5304 switch (c) {
5305 case 'r':
5306 recursive = B_TRUE;
5307 break;
5308 case 'H':
5309 scripted = B_TRUE;
5310 break;
5311 case '?':
5312 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5313 optopt);
5314 usage(B_FALSE);
5318 if (recursive) {
5319 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5320 flags |= ZFS_ITER_RECURSE;
5323 argc -= optind;
5324 argv += optind;
5326 /* check number of arguments */
5327 if (argc < 1)
5328 usage(B_FALSE);
5330 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5331 nomem();
5333 for (i = 0; i < argc; ++i) {
5334 char *snapshot = argv[i];
5335 const char *delim;
5336 const char *snapname;
5338 delim = strchr(snapshot, '@');
5339 if (delim == NULL) {
5340 (void) fprintf(stderr,
5341 gettext("'%s' is not a snapshot\n"), snapshot);
5342 ++errors;
5343 continue;
5345 snapname = delim + 1;
5346 if (recursive)
5347 snapshot[delim - snapshot] = '\0';
5349 cb.cb_recursive = recursive;
5350 cb.cb_snapname = snapname;
5351 cb.cb_nvlp = &nvl;
5354 * 1. collect holds data, set format options
5356 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5357 holds_callback, &cb);
5358 if (ret != 0)
5359 ++errors;
5363 * 2. print holds data
5365 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5367 if (nvlist_empty(nvl))
5368 (void) printf(gettext("no datasets available\n"));
5370 nvlist_free(nvl);
5372 return (0 != errors);
5375 #define CHECK_SPINNER 30
5376 #define SPINNER_TIME 3 /* seconds */
5377 #define MOUNT_TIME 5 /* seconds */
5379 static int
5380 get_one_dataset(zfs_handle_t *zhp, void *data)
5382 static char *spin[] = { "-", "\\", "|", "/" };
5383 static int spinval = 0;
5384 static int spincheck = 0;
5385 static time_t last_spin_time = (time_t)0;
5386 get_all_cb_t *cbp = data;
5387 zfs_type_t type = zfs_get_type(zhp);
5389 if (cbp->cb_verbose) {
5390 if (--spincheck < 0) {
5391 time_t now = time(NULL);
5392 if (last_spin_time + SPINNER_TIME < now) {
5393 update_progress(spin[spinval++ % 4]);
5394 last_spin_time = now;
5396 spincheck = CHECK_SPINNER;
5401 * Interate over any nested datasets.
5403 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5404 zfs_close(zhp);
5405 return (1);
5409 * Skip any datasets whose type does not match.
5411 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5412 zfs_close(zhp);
5413 return (0);
5415 libzfs_add_handle(cbp, zhp);
5416 assert(cbp->cb_used <= cbp->cb_alloc);
5418 return (0);
5421 static void
5422 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5424 get_all_cb_t cb = { 0 };
5425 cb.cb_verbose = verbose;
5426 cb.cb_getone = get_one_dataset;
5428 if (verbose)
5429 set_progress_header(gettext("Reading ZFS config"));
5430 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5432 *dslist = cb.cb_handles;
5433 *count = cb.cb_used;
5435 if (verbose)
5436 finish_progress(gettext("done."));
5440 * Generic callback for sharing or mounting filesystems. Because the code is so
5441 * similar, we have a common function with an extra parameter to determine which
5442 * mode we are using.
5444 #define OP_SHARE 0x1
5445 #define OP_MOUNT 0x2
5448 * Share or mount a dataset.
5450 static int
5451 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5452 boolean_t explicit, const char *options)
5454 char mountpoint[ZFS_MAXPROPLEN];
5455 char shareopts[ZFS_MAXPROPLEN];
5456 char smbshareopts[ZFS_MAXPROPLEN];
5457 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5458 struct mnttab mnt;
5459 uint64_t zoned, canmount;
5460 boolean_t shared_nfs, shared_smb;
5462 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5465 * Check to make sure we can mount/share this dataset. If we
5466 * are in the global zone and the filesystem is exported to a
5467 * local zone, or if we are in a local zone and the
5468 * filesystem is not exported, then it is an error.
5470 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5472 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5473 if (!explicit)
5474 return (0);
5476 (void) fprintf(stderr, gettext("cannot %s '%s': "
5477 "dataset is exported to a local zone\n"), cmdname,
5478 zfs_get_name(zhp));
5479 return (1);
5481 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5482 if (!explicit)
5483 return (0);
5485 (void) fprintf(stderr, gettext("cannot %s '%s': "
5486 "permission denied\n"), cmdname,
5487 zfs_get_name(zhp));
5488 return (1);
5492 * Ignore any filesystems which don't apply to us. This
5493 * includes those with a legacy mountpoint, or those with
5494 * legacy share options.
5496 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5497 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5498 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5499 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5500 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5501 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5503 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5504 strcmp(smbshareopts, "off") == 0) {
5505 if (!explicit)
5506 return (0);
5508 (void) fprintf(stderr, gettext("cannot share '%s': "
5509 "legacy share\n"), zfs_get_name(zhp));
5510 (void) fprintf(stderr, gettext("use share(1M) to "
5511 "share this filesystem, or set "
5512 "sharenfs property on\n"));
5513 return (1);
5517 * We cannot share or mount legacy filesystems. If the
5518 * shareopts is non-legacy but the mountpoint is legacy, we
5519 * treat it as a legacy share.
5521 if (strcmp(mountpoint, "legacy") == 0) {
5522 if (!explicit)
5523 return (0);
5525 (void) fprintf(stderr, gettext("cannot %s '%s': "
5526 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5527 (void) fprintf(stderr, gettext("use %s(1M) to "
5528 "%s this filesystem\n"), cmdname, cmdname);
5529 return (1);
5532 if (strcmp(mountpoint, "none") == 0) {
5533 if (!explicit)
5534 return (0);
5536 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5537 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5538 return (1);
5542 * canmount explicit outcome
5543 * on no pass through
5544 * on yes pass through
5545 * off no return 0
5546 * off yes display error, return 1
5547 * noauto no return 0
5548 * noauto yes pass through
5550 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5551 if (canmount == ZFS_CANMOUNT_OFF) {
5552 if (!explicit)
5553 return (0);
5555 (void) fprintf(stderr, gettext("cannot %s '%s': "
5556 "'canmount' property is set to 'off'\n"), cmdname,
5557 zfs_get_name(zhp));
5558 return (1);
5559 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5560 return (0);
5564 * At this point, we have verified that the mountpoint and/or
5565 * shareopts are appropriate for auto management. If the
5566 * filesystem is already mounted or shared, return (failing
5567 * for explicit requests); otherwise mount or share the
5568 * filesystem.
5570 switch (op) {
5571 case OP_SHARE:
5573 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5574 shared_smb = zfs_is_shared_smb(zhp, NULL);
5576 if (shared_nfs && shared_smb ||
5577 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5578 strcmp(smbshareopts, "off") == 0) ||
5579 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5580 strcmp(shareopts, "off") == 0)) {
5581 if (!explicit)
5582 return (0);
5584 (void) fprintf(stderr, gettext("cannot share "
5585 "'%s': filesystem already shared\n"),
5586 zfs_get_name(zhp));
5587 return (1);
5590 if (!zfs_is_mounted(zhp, NULL) &&
5591 zfs_mount(zhp, NULL, 0) != 0)
5592 return (1);
5594 if (protocol == NULL) {
5595 if (zfs_shareall(zhp) != 0)
5596 return (1);
5597 } else if (strcmp(protocol, "nfs") == 0) {
5598 if (zfs_share_nfs(zhp))
5599 return (1);
5600 } else if (strcmp(protocol, "smb") == 0) {
5601 if (zfs_share_smb(zhp))
5602 return (1);
5603 } else {
5604 (void) fprintf(stderr, gettext("cannot share "
5605 "'%s': invalid share type '%s' "
5606 "specified\n"),
5607 zfs_get_name(zhp), protocol);
5608 return (1);
5611 break;
5613 case OP_MOUNT:
5614 if (options == NULL)
5615 mnt.mnt_mntopts = "";
5616 else
5617 mnt.mnt_mntopts = (char *)options;
5619 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5620 zfs_is_mounted(zhp, NULL)) {
5621 if (!explicit)
5622 return (0);
5624 (void) fprintf(stderr, gettext("cannot mount "
5625 "'%s': filesystem already mounted\n"),
5626 zfs_get_name(zhp));
5627 return (1);
5630 if (zfs_mount(zhp, options, flags) != 0)
5631 return (1);
5632 break;
5635 return (0);
5639 * Reports progress in the form "(current/total)". Not thread-safe.
5641 static void
5642 report_mount_progress(int current, int total)
5644 static time_t last_progress_time = 0;
5645 time_t now = time(NULL);
5646 char info[32];
5648 /* report 1..n instead of 0..n-1 */
5649 ++current;
5651 /* display header if we're here for the first time */
5652 if (current == 1) {
5653 set_progress_header(gettext("Mounting ZFS filesystems"));
5654 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5655 /* too soon to report again */
5656 return;
5659 last_progress_time = now;
5661 (void) sprintf(info, "(%d/%d)", current, total);
5663 if (current == total)
5664 finish_progress(info);
5665 else
5666 update_progress(info);
5669 static void
5670 append_options(char *mntopts, char *newopts)
5672 int len = strlen(mntopts);
5674 /* original length plus new string to append plus 1 for the comma */
5675 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5676 (void) fprintf(stderr, gettext("the opts argument for "
5677 "'%c' option is too long (more than %d chars)\n"),
5678 "-o", MNT_LINE_MAX);
5679 usage(B_FALSE);
5682 if (*mntopts)
5683 mntopts[len++] = ',';
5685 (void) strcpy(&mntopts[len], newopts);
5688 static int
5689 share_mount(int op, int argc, char **argv)
5691 int do_all = 0;
5692 boolean_t verbose = B_FALSE;
5693 int c, ret = 0;
5694 char *options = NULL;
5695 int flags = 0;
5697 /* check options */
5698 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5699 != -1) {
5700 switch (c) {
5701 case 'a':
5702 do_all = 1;
5703 break;
5704 case 'v':
5705 verbose = B_TRUE;
5706 break;
5707 case 'o':
5708 if (*optarg == '\0') {
5709 (void) fprintf(stderr, gettext("empty mount "
5710 "options (-o) specified\n"));
5711 usage(B_FALSE);
5714 if (options == NULL)
5715 options = safe_malloc(MNT_LINE_MAX + 1);
5717 /* option validation is done later */
5718 append_options(options, optarg);
5719 break;
5721 case 'O':
5722 flags |= MS_OVERLAY;
5723 break;
5724 case ':':
5725 (void) fprintf(stderr, gettext("missing argument for "
5726 "'%c' option\n"), optopt);
5727 usage(B_FALSE);
5728 break;
5729 case '?':
5730 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5731 optopt);
5732 usage(B_FALSE);
5736 argc -= optind;
5737 argv += optind;
5739 /* check number of arguments */
5740 if (do_all) {
5741 zfs_handle_t **dslist = NULL;
5742 size_t i, count = 0;
5743 char *protocol = NULL;
5745 if (op == OP_SHARE && argc > 0) {
5746 if (strcmp(argv[0], "nfs") != 0 &&
5747 strcmp(argv[0], "smb") != 0) {
5748 (void) fprintf(stderr, gettext("share type "
5749 "must be 'nfs' or 'smb'\n"));
5750 usage(B_FALSE);
5752 protocol = argv[0];
5753 argc--;
5754 argv++;
5757 if (argc != 0) {
5758 (void) fprintf(stderr, gettext("too many arguments\n"));
5759 usage(B_FALSE);
5762 start_progress_timer();
5763 get_all_datasets(&dslist, &count, verbose);
5765 if (count == 0)
5766 return (0);
5768 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5770 for (i = 0; i < count; i++) {
5771 if (verbose)
5772 report_mount_progress(i, count);
5774 if (share_mount_one(dslist[i], op, flags, protocol,
5775 B_FALSE, options) != 0)
5776 ret = 1;
5777 zfs_close(dslist[i]);
5780 free(dslist);
5781 } else if (argc == 0) {
5782 struct mnttab entry;
5784 if ((op == OP_SHARE) || (options != NULL)) {
5785 (void) fprintf(stderr, gettext("missing filesystem "
5786 "argument (specify -a for all)\n"));
5787 usage(B_FALSE);
5791 * When mount is given no arguments, go through /etc/mnttab and
5792 * display any active ZFS mounts. We hide any snapshots, since
5793 * they are controlled automatically.
5795 rewind(mnttab_file);
5796 while (getmntent(mnttab_file, &entry) == 0) {
5797 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5798 strchr(entry.mnt_special, '@') != NULL)
5799 continue;
5801 (void) printf("%-30s %s\n", entry.mnt_special,
5802 entry.mnt_mountp);
5805 } else {
5806 zfs_handle_t *zhp;
5808 if (argc > 1) {
5809 (void) fprintf(stderr,
5810 gettext("too many arguments\n"));
5811 usage(B_FALSE);
5814 if ((zhp = zfs_open(g_zfs, argv[0],
5815 ZFS_TYPE_FILESYSTEM)) == NULL) {
5816 ret = 1;
5817 } else {
5818 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5819 options);
5820 zfs_close(zhp);
5824 return (ret);
5828 * zfs mount -a [nfs]
5829 * zfs mount filesystem
5831 * Mount all filesystems, or mount the given filesystem.
5833 static int
5834 zfs_do_mount(int argc, char **argv)
5836 return (share_mount(OP_MOUNT, argc, argv));
5840 * zfs share -a [nfs | smb]
5841 * zfs share filesystem
5843 * Share all filesystems, or share the given filesystem.
5845 static int
5846 zfs_do_share(int argc, char **argv)
5848 return (share_mount(OP_SHARE, argc, argv));
5851 typedef struct unshare_unmount_node {
5852 zfs_handle_t *un_zhp;
5853 char *un_mountp;
5854 uu_avl_node_t un_avlnode;
5855 } unshare_unmount_node_t;
5857 /* ARGSUSED */
5858 static int
5859 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5861 const unshare_unmount_node_t *l = larg;
5862 const unshare_unmount_node_t *r = rarg;
5864 return (strcmp(l->un_mountp, r->un_mountp));
5868 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5869 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
5870 * and unmount it appropriately.
5872 static int
5873 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5875 zfs_handle_t *zhp;
5876 int ret = 0;
5877 struct stat64 statbuf;
5878 struct extmnttab entry;
5879 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5880 ino_t path_inode;
5883 * Search for the path in /etc/mnttab. Rather than looking for the
5884 * specific path, which can be fooled by non-standard paths (i.e. ".."
5885 * or "//"), we stat() the path and search for the corresponding
5886 * (major,minor) device pair.
5888 if (stat64(path, &statbuf) != 0) {
5889 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5890 cmdname, path, strerror(errno));
5891 return (1);
5893 path_inode = statbuf.st_ino;
5896 * Search for the given (major,minor) pair in the mount table.
5898 rewind(mnttab_file);
5899 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5900 if (entry.mnt_major == major(statbuf.st_dev) &&
5901 entry.mnt_minor == minor(statbuf.st_dev))
5902 break;
5904 if (ret != 0) {
5905 if (op == OP_SHARE) {
5906 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5907 "currently mounted\n"), cmdname, path);
5908 return (1);
5910 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
5911 path);
5912 if ((ret = umount2(path, flags)) != 0)
5913 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5914 strerror(errno));
5915 return (ret != 0);
5918 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5919 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5920 "filesystem\n"), cmdname, path);
5921 return (1);
5924 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5925 ZFS_TYPE_FILESYSTEM)) == NULL)
5926 return (1);
5928 ret = 1;
5929 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5930 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5931 cmdname, path, strerror(errno));
5932 goto out;
5933 } else if (statbuf.st_ino != path_inode) {
5934 (void) fprintf(stderr, gettext("cannot "
5935 "%s '%s': not a mountpoint\n"), cmdname, path);
5936 goto out;
5939 if (op == OP_SHARE) {
5940 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5941 char smbshare_prop[ZFS_MAXPROPLEN];
5943 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5944 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5945 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5946 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5948 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5949 strcmp(smbshare_prop, "off") == 0) {
5950 (void) fprintf(stderr, gettext("cannot unshare "
5951 "'%s': legacy share\n"), path);
5952 (void) fprintf(stderr, gettext("use "
5953 "unshare(1M) to unshare this filesystem\n"));
5954 } else if (!zfs_is_shared(zhp)) {
5955 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5956 "not currently shared\n"), path);
5957 } else {
5958 ret = zfs_unshareall_bypath(zhp, path);
5960 } else {
5961 char mtpt_prop[ZFS_MAXPROPLEN];
5963 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
5964 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
5966 if (is_manual) {
5967 ret = zfs_unmount(zhp, NULL, flags);
5968 } else if (strcmp(mtpt_prop, "legacy") == 0) {
5969 (void) fprintf(stderr, gettext("cannot unmount "
5970 "'%s': legacy mountpoint\n"),
5971 zfs_get_name(zhp));
5972 (void) fprintf(stderr, gettext("use umount(1M) "
5973 "to unmount this filesystem\n"));
5974 } else {
5975 ret = zfs_unmountall(zhp, flags);
5979 out:
5980 zfs_close(zhp);
5982 return (ret != 0);
5986 * Generic callback for unsharing or unmounting a filesystem.
5988 static int
5989 unshare_unmount(int op, int argc, char **argv)
5991 int do_all = 0;
5992 int flags = 0;
5993 int ret = 0;
5994 int c;
5995 zfs_handle_t *zhp;
5996 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5997 char sharesmb[ZFS_MAXPROPLEN];
5999 /* check options */
6000 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6001 switch (c) {
6002 case 'a':
6003 do_all = 1;
6004 break;
6005 case 'f':
6006 flags = MS_FORCE;
6007 break;
6008 case '?':
6009 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6010 optopt);
6011 usage(B_FALSE);
6015 argc -= optind;
6016 argv += optind;
6018 if (do_all) {
6020 * We could make use of zfs_for_each() to walk all datasets in
6021 * the system, but this would be very inefficient, especially
6022 * since we would have to linearly search /etc/mnttab for each
6023 * one. Instead, do one pass through /etc/mnttab looking for
6024 * zfs entries and call zfs_unmount() for each one.
6026 * Things get a little tricky if the administrator has created
6027 * mountpoints beneath other ZFS filesystems. In this case, we
6028 * have to unmount the deepest filesystems first. To accomplish
6029 * this, we place all the mountpoints in an AVL tree sorted by
6030 * the special type (dataset name), and walk the result in
6031 * reverse to make sure to get any snapshots first.
6033 struct mnttab entry;
6034 uu_avl_pool_t *pool;
6035 uu_avl_t *tree;
6036 unshare_unmount_node_t *node;
6037 uu_avl_index_t idx;
6038 uu_avl_walk_t *walk;
6040 if (argc != 0) {
6041 (void) fprintf(stderr, gettext("too many arguments\n"));
6042 usage(B_FALSE);
6045 if (((pool = uu_avl_pool_create("unmount_pool",
6046 sizeof (unshare_unmount_node_t),
6047 offsetof(unshare_unmount_node_t, un_avlnode),
6048 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6049 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6050 nomem();
6052 rewind(mnttab_file);
6053 while (getmntent(mnttab_file, &entry) == 0) {
6055 /* ignore non-ZFS entries */
6056 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6057 continue;
6059 /* ignore snapshots */
6060 if (strchr(entry.mnt_special, '@') != NULL)
6061 continue;
6063 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6064 ZFS_TYPE_FILESYSTEM)) == NULL) {
6065 ret = 1;
6066 continue;
6069 switch (op) {
6070 case OP_SHARE:
6071 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6072 nfs_mnt_prop,
6073 sizeof (nfs_mnt_prop),
6074 NULL, NULL, 0, B_FALSE) == 0);
6075 if (strcmp(nfs_mnt_prop, "off") != 0)
6076 break;
6077 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6078 nfs_mnt_prop,
6079 sizeof (nfs_mnt_prop),
6080 NULL, NULL, 0, B_FALSE) == 0);
6081 if (strcmp(nfs_mnt_prop, "off") == 0)
6082 continue;
6083 break;
6084 case OP_MOUNT:
6085 /* Ignore legacy mounts */
6086 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6087 nfs_mnt_prop,
6088 sizeof (nfs_mnt_prop),
6089 NULL, NULL, 0, B_FALSE) == 0);
6090 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6091 continue;
6092 /* Ignore canmount=noauto mounts */
6093 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6094 ZFS_CANMOUNT_NOAUTO)
6095 continue;
6096 default:
6097 break;
6100 node = safe_malloc(sizeof (unshare_unmount_node_t));
6101 node->un_zhp = zhp;
6102 node->un_mountp = safe_strdup(entry.mnt_mountp);
6104 uu_avl_node_init(node, &node->un_avlnode, pool);
6106 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6107 uu_avl_insert(tree, node, idx);
6108 } else {
6109 zfs_close(node->un_zhp);
6110 free(node->un_mountp);
6111 free(node);
6116 * Walk the AVL tree in reverse, unmounting each filesystem and
6117 * removing it from the AVL tree in the process.
6119 if ((walk = uu_avl_walk_start(tree,
6120 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6121 nomem();
6123 while ((node = uu_avl_walk_next(walk)) != NULL) {
6124 uu_avl_remove(tree, node);
6126 switch (op) {
6127 case OP_SHARE:
6128 if (zfs_unshareall_bypath(node->un_zhp,
6129 node->un_mountp) != 0)
6130 ret = 1;
6131 break;
6133 case OP_MOUNT:
6134 if (zfs_unmount(node->un_zhp,
6135 node->un_mountp, flags) != 0)
6136 ret = 1;
6137 break;
6140 zfs_close(node->un_zhp);
6141 free(node->un_mountp);
6142 free(node);
6145 uu_avl_walk_end(walk);
6146 uu_avl_destroy(tree);
6147 uu_avl_pool_destroy(pool);
6149 } else {
6150 if (argc != 1) {
6151 if (argc == 0)
6152 (void) fprintf(stderr,
6153 gettext("missing filesystem argument\n"));
6154 else
6155 (void) fprintf(stderr,
6156 gettext("too many arguments\n"));
6157 usage(B_FALSE);
6161 * We have an argument, but it may be a full path or a ZFS
6162 * filesystem. Pass full paths off to unmount_path() (shared by
6163 * manual_unmount), otherwise open the filesystem and pass to
6164 * zfs_unmount().
6166 if (argv[0][0] == '/')
6167 return (unshare_unmount_path(op, argv[0],
6168 flags, B_FALSE));
6170 if ((zhp = zfs_open(g_zfs, argv[0],
6171 ZFS_TYPE_FILESYSTEM)) == NULL)
6172 return (1);
6174 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6175 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6176 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6177 NULL, 0, B_FALSE) == 0);
6179 switch (op) {
6180 case OP_SHARE:
6181 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6182 nfs_mnt_prop,
6183 sizeof (nfs_mnt_prop),
6184 NULL, NULL, 0, B_FALSE) == 0);
6185 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6186 sharesmb, sizeof (sharesmb), NULL, NULL,
6187 0, B_FALSE) == 0);
6189 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6190 strcmp(sharesmb, "off") == 0) {
6191 (void) fprintf(stderr, gettext("cannot "
6192 "unshare '%s': legacy share\n"),
6193 zfs_get_name(zhp));
6194 (void) fprintf(stderr, gettext("use "
6195 "unshare(1M) to unshare this "
6196 "filesystem\n"));
6197 ret = 1;
6198 } else if (!zfs_is_shared(zhp)) {
6199 (void) fprintf(stderr, gettext("cannot "
6200 "unshare '%s': not currently "
6201 "shared\n"), zfs_get_name(zhp));
6202 ret = 1;
6203 } else if (zfs_unshareall(zhp) != 0) {
6204 ret = 1;
6206 break;
6208 case OP_MOUNT:
6209 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6210 (void) fprintf(stderr, gettext("cannot "
6211 "unmount '%s': legacy "
6212 "mountpoint\n"), zfs_get_name(zhp));
6213 (void) fprintf(stderr, gettext("use "
6214 "umount(1M) to unmount this "
6215 "filesystem\n"));
6216 ret = 1;
6217 } else if (!zfs_is_mounted(zhp, NULL)) {
6218 (void) fprintf(stderr, gettext("cannot "
6219 "unmount '%s': not currently "
6220 "mounted\n"),
6221 zfs_get_name(zhp));
6222 ret = 1;
6223 } else if (zfs_unmountall(zhp, flags) != 0) {
6224 ret = 1;
6226 break;
6229 zfs_close(zhp);
6232 return (ret);
6236 * zfs unmount -a
6237 * zfs unmount filesystem
6239 * Unmount all filesystems, or a specific ZFS filesystem.
6241 static int
6242 zfs_do_unmount(int argc, char **argv)
6244 return (unshare_unmount(OP_MOUNT, argc, argv));
6248 * zfs unshare -a
6249 * zfs unshare filesystem
6251 * Unshare all filesystems, or a specific ZFS filesystem.
6253 static int
6254 zfs_do_unshare(int argc, char **argv)
6256 return (unshare_unmount(OP_SHARE, argc, argv));
6260 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6261 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6263 static int
6264 manual_mount(int argc, char **argv)
6266 zfs_handle_t *zhp;
6267 char mountpoint[ZFS_MAXPROPLEN];
6268 char mntopts[MNT_LINE_MAX] = { '\0' };
6269 int ret = 0;
6270 int c;
6271 int flags = 0;
6272 char *dataset, *path;
6274 /* check options */
6275 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6276 switch (c) {
6277 case 'o':
6278 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6279 break;
6280 case 'O':
6281 flags |= MS_OVERLAY;
6282 break;
6283 case 'm':
6284 flags |= MS_NOMNTTAB;
6285 break;
6286 case ':':
6287 (void) fprintf(stderr, gettext("missing argument for "
6288 "'%c' option\n"), optopt);
6289 usage(B_FALSE);
6290 break;
6291 case '?':
6292 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6293 optopt);
6294 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6295 "<path>\n"));
6296 return (2);
6300 argc -= optind;
6301 argv += optind;
6303 /* check that we only have two arguments */
6304 if (argc != 2) {
6305 if (argc == 0)
6306 (void) fprintf(stderr, gettext("missing dataset "
6307 "argument\n"));
6308 else if (argc == 1)
6309 (void) fprintf(stderr,
6310 gettext("missing mountpoint argument\n"));
6311 else
6312 (void) fprintf(stderr, gettext("too many arguments\n"));
6313 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6314 return (2);
6317 dataset = argv[0];
6318 path = argv[1];
6320 /* try to open the dataset */
6321 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6322 return (1);
6324 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6325 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6327 /* check for legacy mountpoint and complain appropriately */
6328 ret = 0;
6329 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6330 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
6331 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6332 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6333 strerror(errno));
6334 ret = 1;
6336 } else {
6337 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6338 "mounted using 'mount -F zfs'\n"), dataset);
6339 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6340 "instead.\n"), path);
6341 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6342 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6343 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6344 "information.\n"));
6345 ret = 1;
6348 return (ret);
6352 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6353 * unmounts of non-legacy filesystems, as this is the dominant administrative
6354 * interface.
6356 static int
6357 manual_unmount(int argc, char **argv)
6359 int flags = 0;
6360 int c;
6362 /* check options */
6363 while ((c = getopt(argc, argv, "f")) != -1) {
6364 switch (c) {
6365 case 'f':
6366 flags = MS_FORCE;
6367 break;
6368 case '?':
6369 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6370 optopt);
6371 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6372 "<path>\n"));
6373 return (2);
6377 argc -= optind;
6378 argv += optind;
6380 /* check arguments */
6381 if (argc != 1) {
6382 if (argc == 0)
6383 (void) fprintf(stderr, gettext("missing path "
6384 "argument\n"));
6385 else
6386 (void) fprintf(stderr, gettext("too many arguments\n"));
6387 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6388 return (2);
6391 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6394 static int
6395 find_command_idx(char *command, int *idx)
6397 int i;
6399 for (i = 0; i < NCOMMAND; i++) {
6400 if (command_table[i].name == NULL)
6401 continue;
6403 if (strcmp(command, command_table[i].name) == 0) {
6404 *idx = i;
6405 return (0);
6408 return (1);
6411 static int
6412 zfs_do_diff(int argc, char **argv)
6414 zfs_handle_t *zhp;
6415 int flags = 0;
6416 char *tosnap = NULL;
6417 char *fromsnap = NULL;
6418 char *atp, *copy;
6419 int err = 0;
6420 int c;
6422 while ((c = getopt(argc, argv, "FHt")) != -1) {
6423 switch (c) {
6424 case 'F':
6425 flags |= ZFS_DIFF_CLASSIFY;
6426 break;
6427 case 'H':
6428 flags |= ZFS_DIFF_PARSEABLE;
6429 break;
6430 case 't':
6431 flags |= ZFS_DIFF_TIMESTAMP;
6432 break;
6433 default:
6434 (void) fprintf(stderr,
6435 gettext("invalid option '%c'\n"), optopt);
6436 usage(B_FALSE);
6440 argc -= optind;
6441 argv += optind;
6443 if (argc < 1) {
6444 (void) fprintf(stderr,
6445 gettext("must provide at least one snapshot name\n"));
6446 usage(B_FALSE);
6449 if (argc > 2) {
6450 (void) fprintf(stderr, gettext("too many arguments\n"));
6451 usage(B_FALSE);
6454 fromsnap = argv[0];
6455 tosnap = (argc == 2) ? argv[1] : NULL;
6457 copy = NULL;
6458 if (*fromsnap != '@')
6459 copy = strdup(fromsnap);
6460 else if (tosnap)
6461 copy = strdup(tosnap);
6462 if (copy == NULL)
6463 usage(B_FALSE);
6465 if (atp = strchr(copy, '@'))
6466 *atp = '\0';
6468 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6469 return (1);
6471 free(copy);
6474 * Ignore SIGPIPE so that the library can give us
6475 * information on any failure
6477 (void) sigignore(SIGPIPE);
6479 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6481 zfs_close(zhp);
6483 return (err != 0);
6487 main(int argc, char **argv)
6489 int ret = 0;
6490 int i;
6491 char *progname;
6492 char *cmdname;
6494 (void) setlocale(LC_ALL, "");
6495 (void) textdomain(TEXT_DOMAIN);
6497 opterr = 0;
6499 if ((g_zfs = libzfs_init()) == NULL) {
6500 (void) fprintf(stderr, gettext("internal error: failed to "
6501 "initialize ZFS library\n"));
6502 return (1);
6505 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6507 libzfs_print_on_error(g_zfs, B_TRUE);
6509 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6510 (void) fprintf(stderr, gettext("internal error: unable to "
6511 "open %s\n"), MNTTAB);
6512 return (1);
6516 * This command also doubles as the /etc/fs mount and unmount program.
6517 * Determine if we should take this behavior based on argv[0].
6519 progname = basename(argv[0]);
6520 if (strcmp(progname, "mount") == 0) {
6521 ret = manual_mount(argc, argv);
6522 } else if (strcmp(progname, "umount") == 0) {
6523 ret = manual_unmount(argc, argv);
6524 } else {
6526 * Make sure the user has specified some command.
6528 if (argc < 2) {
6529 (void) fprintf(stderr, gettext("missing command\n"));
6530 usage(B_FALSE);
6533 cmdname = argv[1];
6536 * The 'umount' command is an alias for 'unmount'
6538 if (strcmp(cmdname, "umount") == 0)
6539 cmdname = "unmount";
6542 * The 'recv' command is an alias for 'receive'
6544 if (strcmp(cmdname, "recv") == 0)
6545 cmdname = "receive";
6548 * Special case '-?'
6550 if (strcmp(cmdname, "-?") == 0)
6551 usage(B_TRUE);
6554 * Run the appropriate command.
6556 libzfs_mnttab_cache(g_zfs, B_TRUE);
6557 if (find_command_idx(cmdname, &i) == 0) {
6558 current_command = &command_table[i];
6559 ret = command_table[i].func(argc - 1, argv + 1);
6560 } else if (strchr(cmdname, '=') != NULL) {
6561 verify(find_command_idx("set", &i) == 0);
6562 current_command = &command_table[i];
6563 ret = command_table[i].func(argc, argv);
6564 } else {
6565 (void) fprintf(stderr, gettext("unrecognized "
6566 "command '%s'\n"), cmdname);
6567 usage(B_FALSE);
6569 libzfs_mnttab_cache(g_zfs, B_FALSE);
6572 (void) fclose(mnttab_file);
6574 if (ret == 0 && log_history)
6575 (void) zpool_log_history(g_zfs, history_str);
6577 libzfs_fini(g_zfs);
6580 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6581 * for the purposes of running ::findleaks.
6583 if (getenv("ZFS_ABORT") != NULL) {
6584 (void) printf("dumping core by request\n");
6585 abort();
6588 return (ret);