9213 zfs: sytem typo
[unleashed.git] / usr / src / cmd / zfs / zfs_main.c
blob9ab006bfaaf10d1519bf2c1bb9df3551a0b1f9ec
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 (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
31 * Copyright 2016 Nexenta Systems, Inc.
34 #include <assert.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <getopt.h>
38 #include <libgen.h>
39 #include <libintl.h>
40 #include <libuutil.h>
41 #include <libnvpair.h>
42 #include <locale.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <strings.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <zone.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <signal.h>
53 #include <sys/debug.h>
54 #include <sys/list.h>
55 #include <sys/mkdev.h>
56 #include <sys/mntent.h>
57 #include <sys/mnttab.h>
58 #include <sys/mount.h>
59 #include <sys/stat.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/types.h>
62 #include <time.h>
64 #include <libzfs.h>
65 #include <libzfs_core.h>
66 #include <zfs_prop.h>
67 #include <zfs_deleg.h>
68 #include <libuutil.h>
69 #include <aclutils.h>
70 #include <directory.h>
71 #include <idmap.h>
72 #include <libshare.h>
74 #include "zfs_iter.h"
75 #include "zfs_util.h"
76 #include "zfs_comutil.h"
78 libzfs_handle_t *g_zfs;
80 static FILE *mnttab_file;
81 static char history_str[HIS_MAX_RECORD_LEN];
82 static boolean_t log_history = B_TRUE;
84 static int zfs_do_clone(int argc, char **argv);
85 static int zfs_do_create(int argc, char **argv);
86 static int zfs_do_destroy(int argc, char **argv);
87 static int zfs_do_get(int argc, char **argv);
88 static int zfs_do_inherit(int argc, char **argv);
89 static int zfs_do_list(int argc, char **argv);
90 static int zfs_do_mount(int argc, char **argv);
91 static int zfs_do_rename(int argc, char **argv);
92 static int zfs_do_rollback(int argc, char **argv);
93 static int zfs_do_set(int argc, char **argv);
94 static int zfs_do_upgrade(int argc, char **argv);
95 static int zfs_do_snapshot(int argc, char **argv);
96 static int zfs_do_unmount(int argc, char **argv);
97 static int zfs_do_share(int argc, char **argv);
98 static int zfs_do_unshare(int argc, char **argv);
99 static int zfs_do_send(int argc, char **argv);
100 static int zfs_do_receive(int argc, char **argv);
101 static int zfs_do_promote(int argc, char **argv);
102 static int zfs_do_userspace(int argc, char **argv);
103 static int zfs_do_allow(int argc, char **argv);
104 static int zfs_do_unallow(int argc, char **argv);
105 static int zfs_do_hold(int argc, char **argv);
106 static int zfs_do_holds(int argc, char **argv);
107 static int zfs_do_release(int argc, char **argv);
108 static int zfs_do_diff(int argc, char **argv);
109 static int zfs_do_bookmark(int argc, char **argv);
110 static int zfs_do_remap(int argc, char **argv);
111 static int zfs_do_channel_program(int argc, char **argv);
114 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
117 #ifdef DEBUG
118 const char *
119 _umem_debug_init(void)
121 return ("default,verbose"); /* $UMEM_DEBUG setting */
124 const char *
125 _umem_logging_init(void)
127 return ("fail,contents"); /* $UMEM_LOGGING setting */
129 #endif
131 typedef enum {
132 HELP_CLONE,
133 HELP_CREATE,
134 HELP_DESTROY,
135 HELP_GET,
136 HELP_INHERIT,
137 HELP_UPGRADE,
138 HELP_LIST,
139 HELP_MOUNT,
140 HELP_PROMOTE,
141 HELP_RECEIVE,
142 HELP_RENAME,
143 HELP_ROLLBACK,
144 HELP_SEND,
145 HELP_SET,
146 HELP_SHARE,
147 HELP_SNAPSHOT,
148 HELP_UNMOUNT,
149 HELP_UNSHARE,
150 HELP_ALLOW,
151 HELP_UNALLOW,
152 HELP_USERSPACE,
153 HELP_GROUPSPACE,
154 HELP_HOLD,
155 HELP_HOLDS,
156 HELP_RELEASE,
157 HELP_DIFF,
158 HELP_REMAP,
159 HELP_BOOKMARK,
160 HELP_CHANNEL_PROGRAM,
161 } zfs_help_t;
163 typedef struct zfs_command {
164 const char *name;
165 int (*func)(int argc, char **argv);
166 zfs_help_t usage;
167 } zfs_command_t;
170 * Master command table. Each ZFS command has a name, associated function, and
171 * usage message. The usage messages need to be internationalized, so we have
172 * to have a function to return the usage message based on a command index.
174 * These commands are organized according to how they are displayed in the usage
175 * message. An empty command (one with a NULL name) indicates an empty line in
176 * the generic usage message.
178 static zfs_command_t command_table[] = {
179 { "create", zfs_do_create, HELP_CREATE },
180 { "destroy", zfs_do_destroy, HELP_DESTROY },
181 { NULL },
182 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
183 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
184 { "clone", zfs_do_clone, HELP_CLONE },
185 { "promote", zfs_do_promote, HELP_PROMOTE },
186 { "rename", zfs_do_rename, HELP_RENAME },
187 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
188 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
189 { NULL },
190 { "list", zfs_do_list, HELP_LIST },
191 { NULL },
192 { "set", zfs_do_set, HELP_SET },
193 { "get", zfs_do_get, HELP_GET },
194 { "inherit", zfs_do_inherit, HELP_INHERIT },
195 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
196 { "userspace", zfs_do_userspace, HELP_USERSPACE },
197 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
198 { NULL },
199 { "mount", zfs_do_mount, HELP_MOUNT },
200 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
201 { "share", zfs_do_share, HELP_SHARE },
202 { "unshare", zfs_do_unshare, HELP_UNSHARE },
203 { NULL },
204 { "send", zfs_do_send, HELP_SEND },
205 { "receive", zfs_do_receive, HELP_RECEIVE },
206 { NULL },
207 { "allow", zfs_do_allow, HELP_ALLOW },
208 { NULL },
209 { "unallow", zfs_do_unallow, HELP_UNALLOW },
210 { NULL },
211 { "hold", zfs_do_hold, HELP_HOLD },
212 { "holds", zfs_do_holds, HELP_HOLDS },
213 { "release", zfs_do_release, HELP_RELEASE },
214 { "diff", zfs_do_diff, HELP_DIFF },
215 { "remap", zfs_do_remap, HELP_REMAP },
218 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
220 zfs_command_t *current_command;
222 static const char *
223 get_usage(zfs_help_t idx)
225 switch (idx) {
226 case HELP_CLONE:
227 return (gettext("\tclone [-p] [-o property=value] ... "
228 "<snapshot> <filesystem|volume>\n"));
229 case HELP_CREATE:
230 return (gettext("\tcreate [-p] [-o property=value] ... "
231 "<filesystem>\n"
232 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
233 "-V <size> <volume>\n"));
234 case HELP_DESTROY:
235 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
236 "\tdestroy [-dnpRrv] "
237 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
238 "\tdestroy <filesystem|volume>#<bookmark>\n"));
239 case HELP_GET:
240 return (gettext("\tget [-rHp] [-d max] "
241 "[-o \"all\" | field[,...]]\n"
242 "\t [-t type[,...]] [-s source[,...]]\n"
243 "\t <\"all\" | property[,...]> "
244 "[filesystem|volume|snapshot|bookmark] ...\n"));
245 case HELP_INHERIT:
246 return (gettext("\tinherit [-rS] <property> "
247 "<filesystem|volume|snapshot> ...\n"));
248 case HELP_UPGRADE:
249 return (gettext("\tupgrade [-v]\n"
250 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
251 case HELP_LIST:
252 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
253 "[-s property]...\n\t [-S property]... [-t type[,...]] "
254 "[filesystem|volume|snapshot] ...\n"));
255 case HELP_MOUNT:
256 return (gettext("\tmount\n"
257 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
258 case HELP_PROMOTE:
259 return (gettext("\tpromote <clone-filesystem>\n"));
260 case HELP_RECEIVE:
261 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
262 "snapshot>\n"
263 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
264 "<filesystem>\n"
265 "\treceive -A <filesystem|volume>\n"));
266 case HELP_RENAME:
267 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
268 "<filesystem|volume|snapshot>\n"
269 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
270 "\trename -r <snapshot> <snapshot>\n"));
271 case HELP_ROLLBACK:
272 return (gettext("\trollback [-rRf] <snapshot>\n"));
273 case HELP_SEND:
274 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
275 "<snapshot>\n"
276 "\tsend [-Le] [-i snapshot|bookmark] "
277 "<filesystem|volume|snapshot>\n"
278 "\tsend [-nvPe] -t <receive_resume_token>\n"));
279 case HELP_SET:
280 return (gettext("\tset <property=value> ... "
281 "<filesystem|volume|snapshot> ...\n"));
282 case HELP_SHARE:
283 return (gettext("\tshare <-a | filesystem>\n"));
284 case HELP_SNAPSHOT:
285 return (gettext("\tsnapshot [-r] [-o property=value] ... "
286 "<filesystem|volume>@<snap> ...\n"));
287 case HELP_UNMOUNT:
288 return (gettext("\tunmount [-f] "
289 "<-a | filesystem|mountpoint>\n"));
290 case HELP_UNSHARE:
291 return (gettext("\tunshare "
292 "<-a | filesystem|mountpoint>\n"));
293 case HELP_ALLOW:
294 return (gettext("\tallow <filesystem|volume>\n"
295 "\tallow [-ldug] "
296 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
297 "\t <filesystem|volume>\n"
298 "\tallow [-ld] -e <perm|@setname>[,...] "
299 "<filesystem|volume>\n"
300 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
301 "\tallow -s @setname <perm|@setname>[,...] "
302 "<filesystem|volume>\n"));
303 case HELP_UNALLOW:
304 return (gettext("\tunallow [-rldug] "
305 "<\"everyone\"|user|group>[,...]\n"
306 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
307 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
308 "<filesystem|volume>\n"
309 "\tunallow [-r] -c [<perm|@setname>[,...]] "
310 "<filesystem|volume>\n"
311 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
312 "<filesystem|volume>\n"));
313 case HELP_USERSPACE:
314 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
315 "[-s field] ...\n"
316 "\t [-S field] ... [-t type[,...]] "
317 "<filesystem|snapshot>\n"));
318 case HELP_GROUPSPACE:
319 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
320 "[-s field] ...\n"
321 "\t [-S field] ... [-t type[,...]] "
322 "<filesystem|snapshot>\n"));
323 case HELP_HOLD:
324 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
325 case HELP_HOLDS:
326 return (gettext("\tholds [-r] <snapshot> ...\n"));
327 case HELP_RELEASE:
328 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
329 case HELP_DIFF:
330 return (gettext("\tdiff [-FHt] <snapshot> "
331 "[snapshot|filesystem]\n"));
332 case HELP_REMAP:
333 return (gettext("\tremap <filesystem | volume>\n"));
334 case HELP_BOOKMARK:
335 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
336 case HELP_CHANNEL_PROGRAM:
337 return (gettext("\tprogram [-n] [-t <instruction limit>] "
338 "[-m <memory limit (b)>] <pool> <program file> "
339 "[lua args...]\n"));
342 abort();
343 /* NOTREACHED */
346 void
347 nomem(void)
349 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
350 exit(1);
354 * Utility function to guarantee malloc() success.
357 void *
358 safe_malloc(size_t size)
360 void *data;
362 if ((data = calloc(1, size)) == NULL)
363 nomem();
365 return (data);
368 void *
369 safe_realloc(void *data, size_t size)
371 void *newp;
372 if ((newp = realloc(data, size)) == NULL) {
373 free(data);
374 nomem();
377 return (newp);
380 static char *
381 safe_strdup(char *str)
383 char *dupstr = strdup(str);
385 if (dupstr == NULL)
386 nomem();
388 return (dupstr);
392 * Callback routine that will print out information for each of
393 * the properties.
395 static int
396 usage_prop_cb(int prop, void *cb)
398 FILE *fp = cb;
400 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
402 if (zfs_prop_readonly(prop))
403 (void) fprintf(fp, " NO ");
404 else
405 (void) fprintf(fp, "YES ");
407 if (zfs_prop_inheritable(prop))
408 (void) fprintf(fp, " YES ");
409 else
410 (void) fprintf(fp, " NO ");
412 if (zfs_prop_values(prop) == NULL)
413 (void) fprintf(fp, "-\n");
414 else
415 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
417 return (ZPROP_CONT);
421 * Display usage message. If we're inside a command, display only the usage for
422 * that command. Otherwise, iterate over the entire command table and display
423 * a complete usage message.
425 static void
426 usage(boolean_t requested)
428 int i;
429 boolean_t show_properties = B_FALSE;
430 FILE *fp = requested ? stdout : stderr;
432 if (current_command == NULL) {
434 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
435 (void) fprintf(fp,
436 gettext("where 'command' is one of the following:\n\n"));
438 for (i = 0; i < NCOMMAND; i++) {
439 if (command_table[i].name == NULL)
440 (void) fprintf(fp, "\n");
441 else
442 (void) fprintf(fp, "%s",
443 get_usage(command_table[i].usage));
446 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
447 "pool/[dataset/]*dataset[@name]\n"));
448 } else {
449 (void) fprintf(fp, gettext("usage:\n"));
450 (void) fprintf(fp, "%s", get_usage(current_command->usage));
453 if (current_command != NULL &&
454 (strcmp(current_command->name, "set") == 0 ||
455 strcmp(current_command->name, "get") == 0 ||
456 strcmp(current_command->name, "inherit") == 0 ||
457 strcmp(current_command->name, "list") == 0))
458 show_properties = B_TRUE;
460 if (show_properties) {
461 (void) fprintf(fp,
462 gettext("\nThe following properties are supported:\n"));
464 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
465 "PROPERTY", "EDIT", "INHERIT", "VALUES");
467 /* Iterate over all properties */
468 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
469 ZFS_TYPE_DATASET);
471 (void) fprintf(fp, "\t%-15s ", "userused@...");
472 (void) fprintf(fp, " NO NO <size>\n");
473 (void) fprintf(fp, "\t%-15s ", "groupused@...");
474 (void) fprintf(fp, " NO NO <size>\n");
475 (void) fprintf(fp, "\t%-15s ", "userquota@...");
476 (void) fprintf(fp, "YES NO <size> | none\n");
477 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
478 (void) fprintf(fp, "YES NO <size> | none\n");
479 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
480 (void) fprintf(fp, " NO NO <size>\n");
482 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
483 "with standard units such as K, M, G, etc.\n"));
484 (void) fprintf(fp, gettext("\nUser-defined properties can "
485 "be specified by using a name containing a colon (:).\n"));
486 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
487 "properties must be appended with\n"
488 "a user or group specifier of one of these forms:\n"
489 " POSIX name (eg: \"matt\")\n"
490 " POSIX id (eg: \"126829\")\n"
491 " SMB name@domain (eg: \"matt@sun\")\n"
492 " SMB SID (eg: \"S-1-234-567-89\")\n"));
493 } else {
494 (void) fprintf(fp,
495 gettext("\nFor the property list, run: %s\n"),
496 "zfs set|get");
497 (void) fprintf(fp,
498 gettext("\nFor the delegated permission list, run: %s\n"),
499 "zfs allow|unallow");
503 * See comments at end of main().
505 if (getenv("ZFS_ABORT") != NULL) {
506 (void) printf("dumping core by request\n");
507 abort();
510 exit(requested ? 0 : 2);
514 * Take a property=value argument string and add it to the given nvlist.
515 * Modifies the argument inplace.
517 static int
518 parseprop(nvlist_t *props, char *propname)
520 char *propval, *strval;
522 if ((propval = strchr(propname, '=')) == NULL) {
523 (void) fprintf(stderr, gettext("missing "
524 "'=' for property=value argument\n"));
525 return (-1);
527 *propval = '\0';
528 propval++;
529 if (nvlist_lookup_string(props, propname, &strval) == 0) {
530 (void) fprintf(stderr, gettext("property '%s' "
531 "specified multiple times\n"), propname);
532 return (-1);
534 if (nvlist_add_string(props, propname, propval) != 0)
535 nomem();
536 return (0);
539 static int
540 parse_depth(char *opt, int *flags)
542 char *tmp;
543 int depth;
545 depth = (int)strtol(opt, &tmp, 0);
546 if (*tmp) {
547 (void) fprintf(stderr,
548 gettext("%s is not an integer\n"), optarg);
549 usage(B_FALSE);
551 if (depth < 0) {
552 (void) fprintf(stderr,
553 gettext("Depth can not be negative.\n"));
554 usage(B_FALSE);
556 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
557 return (depth);
560 #define PROGRESS_DELAY 2 /* seconds */
562 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";
563 static time_t pt_begin;
564 static char *pt_header = NULL;
565 static boolean_t pt_shown;
567 static void
568 start_progress_timer(void)
570 pt_begin = time(NULL) + PROGRESS_DELAY;
571 pt_shown = B_FALSE;
574 static void
575 set_progress_header(char *header)
577 assert(pt_header == NULL);
578 pt_header = safe_strdup(header);
579 if (pt_shown) {
580 (void) printf("%s: ", header);
581 (void) fflush(stdout);
585 static void
586 update_progress(char *update)
588 if (!pt_shown && time(NULL) > pt_begin) {
589 int len = strlen(update);
591 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
592 pt_reverse);
593 (void) fflush(stdout);
594 pt_shown = B_TRUE;
595 } else if (pt_shown) {
596 int len = strlen(update);
598 (void) printf("%s%*.*s", update, len, len, pt_reverse);
599 (void) fflush(stdout);
603 static void
604 finish_progress(char *done)
606 if (pt_shown) {
607 (void) printf("%s\n", done);
608 (void) fflush(stdout);
610 free(pt_header);
611 pt_header = NULL;
615 * Check if the dataset is mountable and should be automatically mounted.
617 static boolean_t
618 should_auto_mount(zfs_handle_t *zhp)
620 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
621 return (B_FALSE);
622 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
626 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
628 * Given an existing dataset, create a writable copy whose initial contents
629 * are the same as the source. The newly created dataset maintains a
630 * dependency on the original; the original cannot be destroyed so long as
631 * the clone exists.
633 * The '-p' flag creates all the non-existing ancestors of the target first.
635 static int
636 zfs_do_clone(int argc, char **argv)
638 zfs_handle_t *zhp = NULL;
639 boolean_t parents = B_FALSE;
640 nvlist_t *props;
641 int ret = 0;
642 int c;
644 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
645 nomem();
647 /* check options */
648 while ((c = getopt(argc, argv, "o:p")) != -1) {
649 switch (c) {
650 case 'o':
651 if (parseprop(props, optarg) != 0)
652 return (1);
653 break;
654 case 'p':
655 parents = B_TRUE;
656 break;
657 case '?':
658 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
659 optopt);
660 goto usage;
664 argc -= optind;
665 argv += optind;
667 /* check number of arguments */
668 if (argc < 1) {
669 (void) fprintf(stderr, gettext("missing source dataset "
670 "argument\n"));
671 goto usage;
673 if (argc < 2) {
674 (void) fprintf(stderr, gettext("missing target dataset "
675 "argument\n"));
676 goto usage;
678 if (argc > 2) {
679 (void) fprintf(stderr, gettext("too many arguments\n"));
680 goto usage;
683 /* open the source dataset */
684 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
685 return (1);
687 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
688 ZFS_TYPE_VOLUME)) {
690 * Now create the ancestors of the target dataset. If the
691 * target already exists and '-p' option was used we should not
692 * complain.
694 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
695 ZFS_TYPE_VOLUME))
696 return (0);
697 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
698 return (1);
701 /* pass to libzfs */
702 ret = zfs_clone(zhp, argv[1], props);
704 /* create the mountpoint if necessary */
705 if (ret == 0) {
706 zfs_handle_t *clone;
708 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
709 if (clone != NULL) {
711 * If the user doesn't want the dataset
712 * automatically mounted, then skip the mount/share
713 * step.
715 if (should_auto_mount(clone)) {
716 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
717 (void) fprintf(stderr, gettext("clone "
718 "successfully created, "
719 "but not mounted\n"));
720 } else if ((ret = zfs_share(clone)) != 0) {
721 (void) fprintf(stderr, gettext("clone "
722 "successfully created, "
723 "but not shared\n"));
726 zfs_close(clone);
730 zfs_close(zhp);
731 nvlist_free(props);
733 return (!!ret);
735 usage:
736 if (zhp)
737 zfs_close(zhp);
738 nvlist_free(props);
739 usage(B_FALSE);
740 return (-1);
744 * zfs create [-p] [-o prop=value] ... fs
745 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
747 * Create a new dataset. This command can be used to create filesystems
748 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
749 * For volumes, the user must specify a size to be used.
751 * The '-s' flag applies only to volumes, and indicates that we should not try
752 * to set the reservation for this volume. By default we set a reservation
753 * equal to the size for any volume. For pools with SPA_VERSION >=
754 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
756 * The '-p' flag creates all the non-existing ancestors of the target first.
758 static int
759 zfs_do_create(int argc, char **argv)
761 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
762 zfs_handle_t *zhp = NULL;
763 uint64_t volsize = 0;
764 int c;
765 boolean_t noreserve = B_FALSE;
766 boolean_t bflag = B_FALSE;
767 boolean_t parents = B_FALSE;
768 int ret = 1;
769 nvlist_t *props;
770 uint64_t intval;
772 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
773 nomem();
775 /* check options */
776 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
777 switch (c) {
778 case 'V':
779 type = ZFS_TYPE_VOLUME;
780 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
781 (void) fprintf(stderr, gettext("bad volume "
782 "size '%s': %s\n"), optarg,
783 libzfs_error_description(g_zfs));
784 goto error;
787 if (nvlist_add_uint64(props,
788 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
789 nomem();
790 volsize = intval;
791 break;
792 case 'p':
793 parents = B_TRUE;
794 break;
795 case 'b':
796 bflag = B_TRUE;
797 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
798 (void) fprintf(stderr, gettext("bad volume "
799 "block size '%s': %s\n"), optarg,
800 libzfs_error_description(g_zfs));
801 goto error;
804 if (nvlist_add_uint64(props,
805 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
806 intval) != 0)
807 nomem();
808 break;
809 case 'o':
810 if (parseprop(props, optarg) != 0)
811 goto error;
812 break;
813 case 's':
814 noreserve = B_TRUE;
815 break;
816 case ':':
817 (void) fprintf(stderr, gettext("missing size "
818 "argument\n"));
819 goto badusage;
820 case '?':
821 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
822 optopt);
823 goto badusage;
827 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
828 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
829 "used when creating a volume\n"));
830 goto badusage;
833 argc -= optind;
834 argv += optind;
836 /* check number of arguments */
837 if (argc == 0) {
838 (void) fprintf(stderr, gettext("missing %s argument\n"),
839 zfs_type_to_name(type));
840 goto badusage;
842 if (argc > 1) {
843 (void) fprintf(stderr, gettext("too many arguments\n"));
844 goto badusage;
847 if (type == ZFS_TYPE_VOLUME && !noreserve) {
848 zpool_handle_t *zpool_handle;
849 nvlist_t *real_props = NULL;
850 uint64_t spa_version;
851 char *p;
852 zfs_prop_t resv_prop;
853 char *strval;
854 char msg[1024];
856 if ((p = strchr(argv[0], '/')) != NULL)
857 *p = '\0';
858 zpool_handle = zpool_open(g_zfs, argv[0]);
859 if (p != NULL)
860 *p = '/';
861 if (zpool_handle == NULL)
862 goto error;
863 spa_version = zpool_get_prop_int(zpool_handle,
864 ZPOOL_PROP_VERSION, NULL);
865 if (spa_version >= SPA_VERSION_REFRESERVATION)
866 resv_prop = ZFS_PROP_REFRESERVATION;
867 else
868 resv_prop = ZFS_PROP_RESERVATION;
870 (void) snprintf(msg, sizeof (msg),
871 gettext("cannot create '%s'"), argv[0]);
872 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
873 props, 0, NULL, zpool_handle, msg)) == NULL) {
874 zpool_close(zpool_handle);
875 goto error;
877 zpool_close(zpool_handle);
879 volsize = zvol_volsize_to_reservation(volsize, real_props);
880 nvlist_free(real_props);
882 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
883 &strval) != 0) {
884 if (nvlist_add_uint64(props,
885 zfs_prop_to_name(resv_prop), volsize) != 0) {
886 nvlist_free(props);
887 nomem();
892 if (parents && zfs_name_valid(argv[0], type)) {
894 * Now create the ancestors of target dataset. If the target
895 * already exists and '-p' option was used we should not
896 * complain.
898 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
899 ret = 0;
900 goto error;
902 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
903 goto error;
906 /* pass to libzfs */
907 if (zfs_create(g_zfs, argv[0], type, props) != 0)
908 goto error;
910 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
911 goto error;
913 ret = 0;
916 * Mount and/or share the new filesystem as appropriate. We provide a
917 * verbose error message to let the user know that their filesystem was
918 * in fact created, even if we failed to mount or share it.
919 * If the user doesn't want the dataset automatically mounted,
920 * then skip the mount/share step altogether.
922 if (should_auto_mount(zhp)) {
923 if (zfs_mount(zhp, NULL, 0) != 0) {
924 (void) fprintf(stderr, gettext("filesystem "
925 "successfully created, but not mounted\n"));
926 ret = 1;
927 } else if (zfs_share(zhp) != 0) {
928 (void) fprintf(stderr, gettext("filesystem "
929 "successfully created, but not shared\n"));
930 ret = 1;
934 error:
935 if (zhp)
936 zfs_close(zhp);
937 nvlist_free(props);
938 return (ret);
939 badusage:
940 nvlist_free(props);
941 usage(B_FALSE);
942 return (2);
946 * zfs destroy [-rRf] <fs, vol>
947 * zfs destroy [-rRd] <snap>
949 * -r Recursively destroy all children
950 * -R Recursively destroy all dependents, including clones
951 * -f Force unmounting of any dependents
952 * -d If we can't destroy now, mark for deferred destruction
954 * Destroys the given dataset. By default, it will unmount any filesystems,
955 * and refuse to destroy a dataset that has any dependents. A dependent can
956 * either be a child, or a clone of a child.
958 typedef struct destroy_cbdata {
959 boolean_t cb_first;
960 boolean_t cb_force;
961 boolean_t cb_recurse;
962 boolean_t cb_error;
963 boolean_t cb_doclones;
964 zfs_handle_t *cb_target;
965 boolean_t cb_defer_destroy;
966 boolean_t cb_verbose;
967 boolean_t cb_parsable;
968 boolean_t cb_dryrun;
969 nvlist_t *cb_nvl;
970 nvlist_t *cb_batchedsnaps;
972 /* first snap in contiguous run */
973 char *cb_firstsnap;
974 /* previous snap in contiguous run */
975 char *cb_prevsnap;
976 int64_t cb_snapused;
977 char *cb_snapspec;
978 char *cb_bookmark;
979 } destroy_cbdata_t;
982 * Check for any dependents based on the '-r' or '-R' flags.
984 static int
985 destroy_check_dependent(zfs_handle_t *zhp, void *data)
987 destroy_cbdata_t *cbp = data;
988 const char *tname = zfs_get_name(cbp->cb_target);
989 const char *name = zfs_get_name(zhp);
991 if (strncmp(tname, name, strlen(tname)) == 0 &&
992 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
994 * This is a direct descendant, not a clone somewhere else in
995 * the hierarchy.
997 if (cbp->cb_recurse)
998 goto out;
1000 if (cbp->cb_first) {
1001 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1002 "%s has children\n"),
1003 zfs_get_name(cbp->cb_target),
1004 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1005 (void) fprintf(stderr, gettext("use '-r' to destroy "
1006 "the following datasets:\n"));
1007 cbp->cb_first = B_FALSE;
1008 cbp->cb_error = B_TRUE;
1011 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1012 } else {
1014 * This is a clone. We only want to report this if the '-r'
1015 * wasn't specified, or the target is a snapshot.
1017 if (!cbp->cb_recurse &&
1018 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1019 goto out;
1021 if (cbp->cb_first) {
1022 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1023 "%s has dependent clones\n"),
1024 zfs_get_name(cbp->cb_target),
1025 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1026 (void) fprintf(stderr, gettext("use '-R' to destroy "
1027 "the following datasets:\n"));
1028 cbp->cb_first = B_FALSE;
1029 cbp->cb_error = B_TRUE;
1030 cbp->cb_dryrun = B_TRUE;
1033 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1036 out:
1037 zfs_close(zhp);
1038 return (0);
1041 static int
1042 destroy_callback(zfs_handle_t *zhp, void *data)
1044 destroy_cbdata_t *cb = data;
1045 const char *name = zfs_get_name(zhp);
1047 if (cb->cb_verbose) {
1048 if (cb->cb_parsable) {
1049 (void) printf("destroy\t%s\n", name);
1050 } else if (cb->cb_dryrun) {
1051 (void) printf(gettext("would destroy %s\n"),
1052 name);
1053 } else {
1054 (void) printf(gettext("will destroy %s\n"),
1055 name);
1060 * Ignore pools (which we've already flagged as an error before getting
1061 * here).
1063 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1064 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1065 zfs_close(zhp);
1066 return (0);
1068 if (cb->cb_dryrun) {
1069 zfs_close(zhp);
1070 return (0);
1074 * We batch up all contiguous snapshots (even of different
1075 * filesystems) and destroy them with one ioctl. We can't
1076 * simply do all snap deletions and then all fs deletions,
1077 * because we must delete a clone before its origin.
1079 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1080 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1081 } else {
1082 int error = zfs_destroy_snaps_nvl(g_zfs,
1083 cb->cb_batchedsnaps, B_FALSE);
1084 fnvlist_free(cb->cb_batchedsnaps);
1085 cb->cb_batchedsnaps = fnvlist_alloc();
1087 if (error != 0 ||
1088 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1089 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1090 zfs_close(zhp);
1091 return (-1);
1095 zfs_close(zhp);
1096 return (0);
1099 static int
1100 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1102 destroy_cbdata_t *cb = arg;
1103 const char *name = zfs_get_name(zhp);
1104 int err = 0;
1106 if (nvlist_exists(cb->cb_nvl, name)) {
1107 if (cb->cb_firstsnap == NULL)
1108 cb->cb_firstsnap = strdup(name);
1109 if (cb->cb_prevsnap != NULL)
1110 free(cb->cb_prevsnap);
1111 /* this snap continues the current range */
1112 cb->cb_prevsnap = strdup(name);
1113 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1114 nomem();
1115 if (cb->cb_verbose) {
1116 if (cb->cb_parsable) {
1117 (void) printf("destroy\t%s\n", name);
1118 } else if (cb->cb_dryrun) {
1119 (void) printf(gettext("would destroy %s\n"),
1120 name);
1121 } else {
1122 (void) printf(gettext("will destroy %s\n"),
1123 name);
1126 } else if (cb->cb_firstsnap != NULL) {
1127 /* end of this range */
1128 uint64_t used = 0;
1129 err = lzc_snaprange_space(cb->cb_firstsnap,
1130 cb->cb_prevsnap, &used);
1131 cb->cb_snapused += used;
1132 free(cb->cb_firstsnap);
1133 cb->cb_firstsnap = NULL;
1134 free(cb->cb_prevsnap);
1135 cb->cb_prevsnap = NULL;
1137 zfs_close(zhp);
1138 return (err);
1141 static int
1142 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1144 int err = 0;
1145 assert(cb->cb_firstsnap == NULL);
1146 assert(cb->cb_prevsnap == NULL);
1147 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1148 if (cb->cb_firstsnap != NULL) {
1149 uint64_t used = 0;
1150 if (err == 0) {
1151 err = lzc_snaprange_space(cb->cb_firstsnap,
1152 cb->cb_prevsnap, &used);
1154 cb->cb_snapused += used;
1155 free(cb->cb_firstsnap);
1156 cb->cb_firstsnap = NULL;
1157 free(cb->cb_prevsnap);
1158 cb->cb_prevsnap = NULL;
1160 return (err);
1163 static int
1164 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1166 destroy_cbdata_t *cb = arg;
1167 int err = 0;
1169 /* Check for clones. */
1170 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1171 cb->cb_target = zhp;
1172 cb->cb_first = B_TRUE;
1173 err = zfs_iter_dependents(zhp, B_TRUE,
1174 destroy_check_dependent, cb);
1177 if (err == 0) {
1178 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1179 nomem();
1181 zfs_close(zhp);
1182 return (err);
1185 static int
1186 gather_snapshots(zfs_handle_t *zhp, void *arg)
1188 destroy_cbdata_t *cb = arg;
1189 int err = 0;
1191 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1192 if (err == ENOENT)
1193 err = 0;
1194 if (err != 0)
1195 goto out;
1197 if (cb->cb_verbose) {
1198 err = destroy_print_snapshots(zhp, cb);
1199 if (err != 0)
1200 goto out;
1203 if (cb->cb_recurse)
1204 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1206 out:
1207 zfs_close(zhp);
1208 return (err);
1211 static int
1212 destroy_clones(destroy_cbdata_t *cb)
1214 nvpair_t *pair;
1215 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1216 pair != NULL;
1217 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1218 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1219 ZFS_TYPE_SNAPSHOT);
1220 if (zhp != NULL) {
1221 boolean_t defer = cb->cb_defer_destroy;
1222 int err = 0;
1225 * We can't defer destroy non-snapshots, so set it to
1226 * false while destroying the clones.
1228 cb->cb_defer_destroy = B_FALSE;
1229 err = zfs_iter_dependents(zhp, B_FALSE,
1230 destroy_callback, cb);
1231 cb->cb_defer_destroy = defer;
1232 zfs_close(zhp);
1233 if (err != 0)
1234 return (err);
1237 return (0);
1240 static int
1241 zfs_do_destroy(int argc, char **argv)
1243 destroy_cbdata_t cb = { 0 };
1244 int rv = 0;
1245 int err = 0;
1246 int c;
1247 zfs_handle_t *zhp = NULL;
1248 char *at, *pound;
1249 zfs_type_t type = ZFS_TYPE_DATASET;
1251 /* check options */
1252 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1253 switch (c) {
1254 case 'v':
1255 cb.cb_verbose = B_TRUE;
1256 break;
1257 case 'p':
1258 cb.cb_verbose = B_TRUE;
1259 cb.cb_parsable = B_TRUE;
1260 break;
1261 case 'n':
1262 cb.cb_dryrun = B_TRUE;
1263 break;
1264 case 'd':
1265 cb.cb_defer_destroy = B_TRUE;
1266 type = ZFS_TYPE_SNAPSHOT;
1267 break;
1268 case 'f':
1269 cb.cb_force = B_TRUE;
1270 break;
1271 case 'r':
1272 cb.cb_recurse = B_TRUE;
1273 break;
1274 case 'R':
1275 cb.cb_recurse = B_TRUE;
1276 cb.cb_doclones = B_TRUE;
1277 break;
1278 case '?':
1279 default:
1280 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1281 optopt);
1282 usage(B_FALSE);
1286 argc -= optind;
1287 argv += optind;
1289 /* check number of arguments */
1290 if (argc == 0) {
1291 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1292 usage(B_FALSE);
1294 if (argc > 1) {
1295 (void) fprintf(stderr, gettext("too many arguments\n"));
1296 usage(B_FALSE);
1299 at = strchr(argv[0], '@');
1300 pound = strchr(argv[0], '#');
1301 if (at != NULL) {
1303 /* Build the list of snaps to destroy in cb_nvl. */
1304 cb.cb_nvl = fnvlist_alloc();
1306 *at = '\0';
1307 zhp = zfs_open(g_zfs, argv[0],
1308 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1309 if (zhp == NULL)
1310 return (1);
1312 cb.cb_snapspec = at + 1;
1313 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1314 cb.cb_error) {
1315 rv = 1;
1316 goto out;
1319 if (nvlist_empty(cb.cb_nvl)) {
1320 (void) fprintf(stderr, gettext("could not find any "
1321 "snapshots to destroy; check snapshot names.\n"));
1322 rv = 1;
1323 goto out;
1326 if (cb.cb_verbose) {
1327 char buf[16];
1328 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1329 if (cb.cb_parsable) {
1330 (void) printf("reclaim\t%llu\n",
1331 cb.cb_snapused);
1332 } else if (cb.cb_dryrun) {
1333 (void) printf(gettext("would reclaim %s\n"),
1334 buf);
1335 } else {
1336 (void) printf(gettext("will reclaim %s\n"),
1337 buf);
1341 if (!cb.cb_dryrun) {
1342 if (cb.cb_doclones) {
1343 cb.cb_batchedsnaps = fnvlist_alloc();
1344 err = destroy_clones(&cb);
1345 if (err == 0) {
1346 err = zfs_destroy_snaps_nvl(g_zfs,
1347 cb.cb_batchedsnaps, B_FALSE);
1349 if (err != 0) {
1350 rv = 1;
1351 goto out;
1354 if (err == 0) {
1355 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1356 cb.cb_defer_destroy);
1360 if (err != 0)
1361 rv = 1;
1362 } else if (pound != NULL) {
1363 int err;
1364 nvlist_t *nvl;
1366 if (cb.cb_dryrun) {
1367 (void) fprintf(stderr,
1368 "dryrun is not supported with bookmark\n");
1369 return (-1);
1372 if (cb.cb_defer_destroy) {
1373 (void) fprintf(stderr,
1374 "defer destroy is not supported with bookmark\n");
1375 return (-1);
1378 if (cb.cb_recurse) {
1379 (void) fprintf(stderr,
1380 "recursive is not supported with bookmark\n");
1381 return (-1);
1384 if (!zfs_bookmark_exists(argv[0])) {
1385 (void) fprintf(stderr, gettext("bookmark '%s' "
1386 "does not exist.\n"), argv[0]);
1387 return (1);
1390 nvl = fnvlist_alloc();
1391 fnvlist_add_boolean(nvl, argv[0]);
1393 err = lzc_destroy_bookmarks(nvl, NULL);
1394 if (err != 0) {
1395 (void) zfs_standard_error(g_zfs, err,
1396 "cannot destroy bookmark");
1399 nvlist_free(cb.cb_nvl);
1401 return (err);
1402 } else {
1403 /* Open the given dataset */
1404 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1405 return (1);
1407 cb.cb_target = zhp;
1410 * Perform an explicit check for pools before going any further.
1412 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1413 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1414 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1415 "operation does not apply to pools\n"),
1416 zfs_get_name(zhp));
1417 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1418 "%s' to destroy all datasets in the pool\n"),
1419 zfs_get_name(zhp));
1420 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1421 "to destroy the pool itself\n"), zfs_get_name(zhp));
1422 rv = 1;
1423 goto out;
1427 * Check for any dependents and/or clones.
1429 cb.cb_first = B_TRUE;
1430 if (!cb.cb_doclones &&
1431 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1432 &cb) != 0) {
1433 rv = 1;
1434 goto out;
1437 if (cb.cb_error) {
1438 rv = 1;
1439 goto out;
1442 cb.cb_batchedsnaps = fnvlist_alloc();
1443 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1444 &cb) != 0) {
1445 rv = 1;
1446 goto out;
1450 * Do the real thing. The callback will close the
1451 * handle regardless of whether it succeeds or not.
1453 err = destroy_callback(zhp, &cb);
1454 zhp = NULL;
1455 if (err == 0) {
1456 err = zfs_destroy_snaps_nvl(g_zfs,
1457 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1459 if (err != 0)
1460 rv = 1;
1463 out:
1464 fnvlist_free(cb.cb_batchedsnaps);
1465 fnvlist_free(cb.cb_nvl);
1466 if (zhp != NULL)
1467 zfs_close(zhp);
1468 return (rv);
1471 static boolean_t
1472 is_recvd_column(zprop_get_cbdata_t *cbp)
1474 int i;
1475 zfs_get_column_t col;
1477 for (i = 0; i < ZFS_GET_NCOLS &&
1478 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1479 if (col == GET_COL_RECVD)
1480 return (B_TRUE);
1481 return (B_FALSE);
1485 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1486 * < all | property[,property]... > < fs | snap | vol > ...
1488 * -r recurse over any child datasets
1489 * -H scripted mode. Headers are stripped, and fields are separated
1490 * by tabs instead of spaces.
1491 * -o Set of fields to display. One of "name,property,value,
1492 * received,source". Default is "name,property,value,source".
1493 * "all" is an alias for all five.
1494 * -s Set of sources to allow. One of
1495 * "local,default,inherited,received,temporary,none". Default is
1496 * all six.
1497 * -p Display values in parsable (literal) format.
1499 * Prints properties for the given datasets. The user can control which
1500 * columns to display as well as which property types to allow.
1504 * Invoked to display the properties for a single dataset.
1506 static int
1507 get_callback(zfs_handle_t *zhp, void *data)
1509 char buf[ZFS_MAXPROPLEN];
1510 char rbuf[ZFS_MAXPROPLEN];
1511 zprop_source_t sourcetype;
1512 char source[ZFS_MAX_DATASET_NAME_LEN];
1513 zprop_get_cbdata_t *cbp = data;
1514 nvlist_t *user_props = zfs_get_user_props(zhp);
1515 zprop_list_t *pl = cbp->cb_proplist;
1516 nvlist_t *propval;
1517 char *strval;
1518 char *sourceval;
1519 boolean_t received = is_recvd_column(cbp);
1521 for (; pl != NULL; pl = pl->pl_next) {
1522 char *recvdval = NULL;
1524 * Skip the special fake placeholder. This will also skip over
1525 * the name property when 'all' is specified.
1527 if (pl->pl_prop == ZFS_PROP_NAME &&
1528 pl == cbp->cb_proplist)
1529 continue;
1531 if (pl->pl_prop != ZPROP_INVAL) {
1532 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1533 sizeof (buf), &sourcetype, source,
1534 sizeof (source),
1535 cbp->cb_literal) != 0) {
1536 if (pl->pl_all)
1537 continue;
1538 if (!zfs_prop_valid_for_type(pl->pl_prop,
1539 ZFS_TYPE_DATASET)) {
1540 (void) fprintf(stderr,
1541 gettext("No such property '%s'\n"),
1542 zfs_prop_to_name(pl->pl_prop));
1543 continue;
1545 sourcetype = ZPROP_SRC_NONE;
1546 (void) strlcpy(buf, "-", sizeof (buf));
1549 if (received && (zfs_prop_get_recvd(zhp,
1550 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1551 cbp->cb_literal) == 0))
1552 recvdval = rbuf;
1554 zprop_print_one_property(zfs_get_name(zhp), cbp,
1555 zfs_prop_to_name(pl->pl_prop),
1556 buf, sourcetype, source, recvdval);
1557 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1558 sourcetype = ZPROP_SRC_LOCAL;
1560 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1561 buf, sizeof (buf), cbp->cb_literal) != 0) {
1562 sourcetype = ZPROP_SRC_NONE;
1563 (void) strlcpy(buf, "-", sizeof (buf));
1566 zprop_print_one_property(zfs_get_name(zhp), cbp,
1567 pl->pl_user_prop, buf, sourcetype, source, NULL);
1568 } else if (zfs_prop_written(pl->pl_user_prop)) {
1569 sourcetype = ZPROP_SRC_LOCAL;
1571 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1572 buf, sizeof (buf), cbp->cb_literal) != 0) {
1573 sourcetype = ZPROP_SRC_NONE;
1574 (void) strlcpy(buf, "-", sizeof (buf));
1577 zprop_print_one_property(zfs_get_name(zhp), cbp,
1578 pl->pl_user_prop, buf, sourcetype, source, NULL);
1579 } else {
1580 if (nvlist_lookup_nvlist(user_props,
1581 pl->pl_user_prop, &propval) != 0) {
1582 if (pl->pl_all)
1583 continue;
1584 sourcetype = ZPROP_SRC_NONE;
1585 strval = "-";
1586 } else {
1587 verify(nvlist_lookup_string(propval,
1588 ZPROP_VALUE, &strval) == 0);
1589 verify(nvlist_lookup_string(propval,
1590 ZPROP_SOURCE, &sourceval) == 0);
1592 if (strcmp(sourceval,
1593 zfs_get_name(zhp)) == 0) {
1594 sourcetype = ZPROP_SRC_LOCAL;
1595 } else if (strcmp(sourceval,
1596 ZPROP_SOURCE_VAL_RECVD) == 0) {
1597 sourcetype = ZPROP_SRC_RECEIVED;
1598 } else {
1599 sourcetype = ZPROP_SRC_INHERITED;
1600 (void) strlcpy(source,
1601 sourceval, sizeof (source));
1605 if (received && (zfs_prop_get_recvd(zhp,
1606 pl->pl_user_prop, rbuf, sizeof (rbuf),
1607 cbp->cb_literal) == 0))
1608 recvdval = rbuf;
1610 zprop_print_one_property(zfs_get_name(zhp), cbp,
1611 pl->pl_user_prop, strval, sourcetype,
1612 source, recvdval);
1616 return (0);
1619 static int
1620 zfs_do_get(int argc, char **argv)
1622 zprop_get_cbdata_t cb = { 0 };
1623 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1624 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1625 char *value, *fields;
1626 int ret = 0;
1627 int limit = 0;
1628 zprop_list_t fake_name = { 0 };
1631 * Set up default columns and sources.
1633 cb.cb_sources = ZPROP_SRC_ALL;
1634 cb.cb_columns[0] = GET_COL_NAME;
1635 cb.cb_columns[1] = GET_COL_PROPERTY;
1636 cb.cb_columns[2] = GET_COL_VALUE;
1637 cb.cb_columns[3] = GET_COL_SOURCE;
1638 cb.cb_type = ZFS_TYPE_DATASET;
1640 /* check options */
1641 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1642 switch (c) {
1643 case 'p':
1644 cb.cb_literal = B_TRUE;
1645 break;
1646 case 'd':
1647 limit = parse_depth(optarg, &flags);
1648 break;
1649 case 'r':
1650 flags |= ZFS_ITER_RECURSE;
1651 break;
1652 case 'H':
1653 cb.cb_scripted = B_TRUE;
1654 break;
1655 case ':':
1656 (void) fprintf(stderr, gettext("missing argument for "
1657 "'%c' option\n"), optopt);
1658 usage(B_FALSE);
1659 break;
1660 case 'o':
1662 * Process the set of columns to display. We zero out
1663 * the structure to give us a blank slate.
1665 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1666 i = 0;
1667 while (*optarg != '\0') {
1668 static char *col_subopts[] =
1669 { "name", "property", "value", "received",
1670 "source", "all", NULL };
1672 if (i == ZFS_GET_NCOLS) {
1673 (void) fprintf(stderr, gettext("too "
1674 "many fields given to -o "
1675 "option\n"));
1676 usage(B_FALSE);
1679 switch (getsubopt(&optarg, col_subopts,
1680 &value)) {
1681 case 0:
1682 cb.cb_columns[i++] = GET_COL_NAME;
1683 break;
1684 case 1:
1685 cb.cb_columns[i++] = GET_COL_PROPERTY;
1686 break;
1687 case 2:
1688 cb.cb_columns[i++] = GET_COL_VALUE;
1689 break;
1690 case 3:
1691 cb.cb_columns[i++] = GET_COL_RECVD;
1692 flags |= ZFS_ITER_RECVD_PROPS;
1693 break;
1694 case 4:
1695 cb.cb_columns[i++] = GET_COL_SOURCE;
1696 break;
1697 case 5:
1698 if (i > 0) {
1699 (void) fprintf(stderr,
1700 gettext("\"all\" conflicts "
1701 "with specific fields "
1702 "given to -o option\n"));
1703 usage(B_FALSE);
1705 cb.cb_columns[0] = GET_COL_NAME;
1706 cb.cb_columns[1] = GET_COL_PROPERTY;
1707 cb.cb_columns[2] = GET_COL_VALUE;
1708 cb.cb_columns[3] = GET_COL_RECVD;
1709 cb.cb_columns[4] = GET_COL_SOURCE;
1710 flags |= ZFS_ITER_RECVD_PROPS;
1711 i = ZFS_GET_NCOLS;
1712 break;
1713 default:
1714 (void) fprintf(stderr,
1715 gettext("invalid column name "
1716 "'%s'\n"), value);
1717 usage(B_FALSE);
1720 break;
1722 case 's':
1723 cb.cb_sources = 0;
1724 while (*optarg != '\0') {
1725 static char *source_subopts[] = {
1726 "local", "default", "inherited",
1727 "received", "temporary", "none",
1728 NULL };
1730 switch (getsubopt(&optarg, source_subopts,
1731 &value)) {
1732 case 0:
1733 cb.cb_sources |= ZPROP_SRC_LOCAL;
1734 break;
1735 case 1:
1736 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1737 break;
1738 case 2:
1739 cb.cb_sources |= ZPROP_SRC_INHERITED;
1740 break;
1741 case 3:
1742 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1743 break;
1744 case 4:
1745 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1746 break;
1747 case 5:
1748 cb.cb_sources |= ZPROP_SRC_NONE;
1749 break;
1750 default:
1751 (void) fprintf(stderr,
1752 gettext("invalid source "
1753 "'%s'\n"), value);
1754 usage(B_FALSE);
1757 break;
1759 case 't':
1760 types = 0;
1761 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1762 while (*optarg != '\0') {
1763 static char *type_subopts[] = { "filesystem",
1764 "volume", "snapshot", "bookmark",
1765 "all", NULL };
1767 switch (getsubopt(&optarg, type_subopts,
1768 &value)) {
1769 case 0:
1770 types |= ZFS_TYPE_FILESYSTEM;
1771 break;
1772 case 1:
1773 types |= ZFS_TYPE_VOLUME;
1774 break;
1775 case 2:
1776 types |= ZFS_TYPE_SNAPSHOT;
1777 break;
1778 case 3:
1779 types |= ZFS_TYPE_BOOKMARK;
1780 break;
1781 case 4:
1782 types = ZFS_TYPE_DATASET |
1783 ZFS_TYPE_BOOKMARK;
1784 break;
1786 default:
1787 (void) fprintf(stderr,
1788 gettext("invalid type '%s'\n"),
1789 value);
1790 usage(B_FALSE);
1793 break;
1795 case '?':
1796 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1797 optopt);
1798 usage(B_FALSE);
1802 argc -= optind;
1803 argv += optind;
1805 if (argc < 1) {
1806 (void) fprintf(stderr, gettext("missing property "
1807 "argument\n"));
1808 usage(B_FALSE);
1811 fields = argv[0];
1813 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1814 != 0)
1815 usage(B_FALSE);
1817 argc--;
1818 argv++;
1821 * As part of zfs_expand_proplist(), we keep track of the maximum column
1822 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1823 * need to know the maximum name length. However, the user likely did
1824 * not specify 'name' as one of the properties to fetch, so we need to
1825 * make sure we always include at least this property for
1826 * print_get_headers() to work properly.
1828 if (cb.cb_proplist != NULL) {
1829 fake_name.pl_prop = ZFS_PROP_NAME;
1830 fake_name.pl_width = strlen(gettext("NAME"));
1831 fake_name.pl_next = cb.cb_proplist;
1832 cb.cb_proplist = &fake_name;
1835 cb.cb_first = B_TRUE;
1837 /* run for each object */
1838 ret = zfs_for_each(argc, argv, flags, types, NULL,
1839 &cb.cb_proplist, limit, get_callback, &cb);
1841 if (cb.cb_proplist == &fake_name)
1842 zprop_free_list(fake_name.pl_next);
1843 else
1844 zprop_free_list(cb.cb_proplist);
1846 return (ret);
1850 * inherit [-rS] <property> <fs|vol> ...
1852 * -r Recurse over all children
1853 * -S Revert to received value, if any
1855 * For each dataset specified on the command line, inherit the given property
1856 * from its parent. Inheriting a property at the pool level will cause it to
1857 * use the default value. The '-r' flag will recurse over all children, and is
1858 * useful for setting a property on a hierarchy-wide basis, regardless of any
1859 * local modifications for each dataset.
1862 typedef struct inherit_cbdata {
1863 const char *cb_propname;
1864 boolean_t cb_received;
1865 } inherit_cbdata_t;
1867 static int
1868 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1870 inherit_cbdata_t *cb = data;
1871 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1874 * If we're doing it recursively, then ignore properties that
1875 * are not valid for this type of dataset.
1877 if (prop != ZPROP_INVAL &&
1878 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1879 return (0);
1881 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1884 static int
1885 inherit_cb(zfs_handle_t *zhp, void *data)
1887 inherit_cbdata_t *cb = data;
1889 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1892 static int
1893 zfs_do_inherit(int argc, char **argv)
1895 int c;
1896 zfs_prop_t prop;
1897 inherit_cbdata_t cb = { 0 };
1898 char *propname;
1899 int ret = 0;
1900 int flags = 0;
1901 boolean_t received = B_FALSE;
1903 /* check options */
1904 while ((c = getopt(argc, argv, "rS")) != -1) {
1905 switch (c) {
1906 case 'r':
1907 flags |= ZFS_ITER_RECURSE;
1908 break;
1909 case 'S':
1910 received = B_TRUE;
1911 break;
1912 case '?':
1913 default:
1914 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1915 optopt);
1916 usage(B_FALSE);
1920 argc -= optind;
1921 argv += optind;
1923 /* check number of arguments */
1924 if (argc < 1) {
1925 (void) fprintf(stderr, gettext("missing property argument\n"));
1926 usage(B_FALSE);
1928 if (argc < 2) {
1929 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1930 usage(B_FALSE);
1933 propname = argv[0];
1934 argc--;
1935 argv++;
1937 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1938 if (zfs_prop_readonly(prop)) {
1939 (void) fprintf(stderr, gettext(
1940 "%s property is read-only\n"),
1941 propname);
1942 return (1);
1944 if (!zfs_prop_inheritable(prop) && !received) {
1945 (void) fprintf(stderr, gettext("'%s' property cannot "
1946 "be inherited\n"), propname);
1947 if (prop == ZFS_PROP_QUOTA ||
1948 prop == ZFS_PROP_RESERVATION ||
1949 prop == ZFS_PROP_REFQUOTA ||
1950 prop == ZFS_PROP_REFRESERVATION) {
1951 (void) fprintf(stderr, gettext("use 'zfs set "
1952 "%s=none' to clear\n"), propname);
1953 (void) fprintf(stderr, gettext("use 'zfs "
1954 "inherit -S %s' to revert to received "
1955 "value\n"), propname);
1957 return (1);
1959 if (received && (prop == ZFS_PROP_VOLSIZE ||
1960 prop == ZFS_PROP_VERSION)) {
1961 (void) fprintf(stderr, gettext("'%s' property cannot "
1962 "be reverted to a received value\n"), propname);
1963 return (1);
1965 } else if (!zfs_prop_user(propname)) {
1966 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1967 propname);
1968 usage(B_FALSE);
1971 cb.cb_propname = propname;
1972 cb.cb_received = received;
1974 if (flags & ZFS_ITER_RECURSE) {
1975 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1976 NULL, NULL, 0, inherit_recurse_cb, &cb);
1977 } else {
1978 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1979 NULL, NULL, 0, inherit_cb, &cb);
1982 return (ret);
1985 typedef struct upgrade_cbdata {
1986 uint64_t cb_numupgraded;
1987 uint64_t cb_numsamegraded;
1988 uint64_t cb_numfailed;
1989 uint64_t cb_version;
1990 boolean_t cb_newer;
1991 boolean_t cb_foundone;
1992 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
1993 } upgrade_cbdata_t;
1995 static int
1996 same_pool(zfs_handle_t *zhp, const char *name)
1998 int len1 = strcspn(name, "/@");
1999 const char *zhname = zfs_get_name(zhp);
2000 int len2 = strcspn(zhname, "/@");
2002 if (len1 != len2)
2003 return (B_FALSE);
2004 return (strncmp(name, zhname, len1) == 0);
2007 static int
2008 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2010 upgrade_cbdata_t *cb = data;
2011 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2013 /* list if it's old/new */
2014 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2015 (cb->cb_newer && version > ZPL_VERSION)) {
2016 char *str;
2017 if (cb->cb_newer) {
2018 str = gettext("The following filesystems are "
2019 "formatted using a newer software version and\n"
2020 "cannot be accessed on the current system.\n\n");
2021 } else {
2022 str = gettext("The following filesystems are "
2023 "out of date, and can be upgraded. After being\n"
2024 "upgraded, these filesystems (and any 'zfs send' "
2025 "streams generated from\n"
2026 "subsequent snapshots) will no longer be "
2027 "accessible by older software versions.\n\n");
2030 if (!cb->cb_foundone) {
2031 (void) puts(str);
2032 (void) printf(gettext("VER FILESYSTEM\n"));
2033 (void) printf(gettext("--- ------------\n"));
2034 cb->cb_foundone = B_TRUE;
2037 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2040 return (0);
2043 static int
2044 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2046 upgrade_cbdata_t *cb = data;
2047 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2048 int needed_spa_version;
2049 int spa_version;
2051 if (zfs_spa_version(zhp, &spa_version) < 0)
2052 return (-1);
2054 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2056 if (needed_spa_version < 0)
2057 return (-1);
2059 if (spa_version < needed_spa_version) {
2060 /* can't upgrade */
2061 (void) printf(gettext("%s: can not be "
2062 "upgraded; the pool version needs to first "
2063 "be upgraded\nto version %d\n\n"),
2064 zfs_get_name(zhp), needed_spa_version);
2065 cb->cb_numfailed++;
2066 return (0);
2069 /* upgrade */
2070 if (version < cb->cb_version) {
2071 char verstr[16];
2072 (void) snprintf(verstr, sizeof (verstr),
2073 "%llu", cb->cb_version);
2074 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2076 * If they did "zfs upgrade -a", then we could
2077 * be doing ioctls to different pools. We need
2078 * to log this history once to each pool, and bypass
2079 * the normal history logging that happens in main().
2081 (void) zpool_log_history(g_zfs, history_str);
2082 log_history = B_FALSE;
2084 if (zfs_prop_set(zhp, "version", verstr) == 0)
2085 cb->cb_numupgraded++;
2086 else
2087 cb->cb_numfailed++;
2088 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2089 } else if (version > cb->cb_version) {
2090 /* can't downgrade */
2091 (void) printf(gettext("%s: can not be downgraded; "
2092 "it is already at version %u\n"),
2093 zfs_get_name(zhp), version);
2094 cb->cb_numfailed++;
2095 } else {
2096 cb->cb_numsamegraded++;
2098 return (0);
2102 * zfs upgrade
2103 * zfs upgrade -v
2104 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2106 static int
2107 zfs_do_upgrade(int argc, char **argv)
2109 boolean_t all = B_FALSE;
2110 boolean_t showversions = B_FALSE;
2111 int ret = 0;
2112 upgrade_cbdata_t cb = { 0 };
2113 char c;
2114 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2116 /* check options */
2117 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2118 switch (c) {
2119 case 'r':
2120 flags |= ZFS_ITER_RECURSE;
2121 break;
2122 case 'v':
2123 showversions = B_TRUE;
2124 break;
2125 case 'V':
2126 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2127 optarg, &cb.cb_version) != 0) {
2128 (void) fprintf(stderr,
2129 gettext("invalid version %s\n"), optarg);
2130 usage(B_FALSE);
2132 break;
2133 case 'a':
2134 all = B_TRUE;
2135 break;
2136 case '?':
2137 default:
2138 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2139 optopt);
2140 usage(B_FALSE);
2144 argc -= optind;
2145 argv += optind;
2147 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2148 usage(B_FALSE);
2149 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2150 cb.cb_version || argc))
2151 usage(B_FALSE);
2152 if ((all || argc) && (showversions))
2153 usage(B_FALSE);
2154 if (all && argc)
2155 usage(B_FALSE);
2157 if (showversions) {
2158 /* Show info on available versions. */
2159 (void) printf(gettext("The following filesystem versions are "
2160 "supported:\n\n"));
2161 (void) printf(gettext("VER DESCRIPTION\n"));
2162 (void) printf("--- -----------------------------------------"
2163 "---------------\n");
2164 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2165 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2166 (void) printf(gettext(" 3 Case insensitive and filesystem "
2167 "user identifier (FUID)\n"));
2168 (void) printf(gettext(" 4 userquota, groupquota "
2169 "properties\n"));
2170 (void) printf(gettext(" 5 System attributes\n"));
2171 (void) printf(gettext("\nFor more information on a particular "
2172 "version, including supported releases,\n"));
2173 (void) printf("see the ZFS Administration Guide.\n\n");
2174 ret = 0;
2175 } else if (argc || all) {
2176 /* Upgrade filesystems */
2177 if (cb.cb_version == 0)
2178 cb.cb_version = ZPL_VERSION;
2179 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2180 NULL, NULL, 0, upgrade_set_callback, &cb);
2181 (void) printf(gettext("%llu filesystems upgraded\n"),
2182 cb.cb_numupgraded);
2183 if (cb.cb_numsamegraded) {
2184 (void) printf(gettext("%llu filesystems already at "
2185 "this version\n"),
2186 cb.cb_numsamegraded);
2188 if (cb.cb_numfailed != 0)
2189 ret = 1;
2190 } else {
2191 /* List old-version filesystems */
2192 boolean_t found;
2193 (void) printf(gettext("This system is currently running "
2194 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2196 flags |= ZFS_ITER_RECURSE;
2197 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2198 NULL, NULL, 0, upgrade_list_callback, &cb);
2200 found = cb.cb_foundone;
2201 cb.cb_foundone = B_FALSE;
2202 cb.cb_newer = B_TRUE;
2204 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2205 NULL, NULL, 0, upgrade_list_callback, &cb);
2207 if (!cb.cb_foundone && !found) {
2208 (void) printf(gettext("All filesystems are "
2209 "formatted with the current version.\n"));
2213 return (ret);
2217 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2218 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2219 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2220 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2222 * -H Scripted mode; elide headers and separate columns by tabs.
2223 * -i Translate SID to POSIX ID.
2224 * -n Print numeric ID instead of user/group name.
2225 * -o Control which fields to display.
2226 * -p Use exact (parsable) numeric output.
2227 * -s Specify sort columns, descending order.
2228 * -S Specify sort columns, ascending order.
2229 * -t Control which object types to display.
2231 * Displays space consumed by, and quotas on, each user in the specified
2232 * filesystem or snapshot.
2235 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2236 enum us_field_types {
2237 USFIELD_TYPE,
2238 USFIELD_NAME,
2239 USFIELD_USED,
2240 USFIELD_QUOTA
2242 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2243 static char *us_field_names[] = { "type", "name", "used", "quota" };
2244 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2246 #define USTYPE_PSX_GRP (1 << 0)
2247 #define USTYPE_PSX_USR (1 << 1)
2248 #define USTYPE_SMB_GRP (1 << 2)
2249 #define USTYPE_SMB_USR (1 << 3)
2250 #define USTYPE_ALL \
2251 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2253 static int us_type_bits[] = {
2254 USTYPE_PSX_GRP,
2255 USTYPE_PSX_USR,
2256 USTYPE_SMB_GRP,
2257 USTYPE_SMB_USR,
2258 USTYPE_ALL
2260 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2261 "smbuser", "all" };
2263 typedef struct us_node {
2264 nvlist_t *usn_nvl;
2265 uu_avl_node_t usn_avlnode;
2266 uu_list_node_t usn_listnode;
2267 } us_node_t;
2269 typedef struct us_cbdata {
2270 nvlist_t **cb_nvlp;
2271 uu_avl_pool_t *cb_avl_pool;
2272 uu_avl_t *cb_avl;
2273 boolean_t cb_numname;
2274 boolean_t cb_nicenum;
2275 boolean_t cb_sid2posix;
2276 zfs_userquota_prop_t cb_prop;
2277 zfs_sort_column_t *cb_sortcol;
2278 size_t cb_width[USFIELD_LAST];
2279 } us_cbdata_t;
2281 static boolean_t us_populated = B_FALSE;
2283 typedef struct {
2284 zfs_sort_column_t *si_sortcol;
2285 boolean_t si_numname;
2286 } us_sort_info_t;
2288 static int
2289 us_field_index(char *field)
2291 int i;
2293 for (i = 0; i < USFIELD_LAST; i++) {
2294 if (strcmp(field, us_field_names[i]) == 0)
2295 return (i);
2298 return (-1);
2301 static int
2302 us_compare(const void *larg, const void *rarg, void *unused)
2304 const us_node_t *l = larg;
2305 const us_node_t *r = rarg;
2306 us_sort_info_t *si = (us_sort_info_t *)unused;
2307 zfs_sort_column_t *sortcol = si->si_sortcol;
2308 boolean_t numname = si->si_numname;
2309 nvlist_t *lnvl = l->usn_nvl;
2310 nvlist_t *rnvl = r->usn_nvl;
2311 int rc = 0;
2312 boolean_t lvb, rvb;
2314 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2315 char *lvstr = "";
2316 char *rvstr = "";
2317 uint32_t lv32 = 0;
2318 uint32_t rv32 = 0;
2319 uint64_t lv64 = 0;
2320 uint64_t rv64 = 0;
2321 zfs_prop_t prop = sortcol->sc_prop;
2322 const char *propname = NULL;
2323 boolean_t reverse = sortcol->sc_reverse;
2325 switch (prop) {
2326 case ZFS_PROP_TYPE:
2327 propname = "type";
2328 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2329 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2330 if (rv32 != lv32)
2331 rc = (rv32 < lv32) ? 1 : -1;
2332 break;
2333 case ZFS_PROP_NAME:
2334 propname = "name";
2335 if (numname) {
2336 (void) nvlist_lookup_uint64(lnvl, propname,
2337 &lv64);
2338 (void) nvlist_lookup_uint64(rnvl, propname,
2339 &rv64);
2340 if (rv64 != lv64)
2341 rc = (rv64 < lv64) ? 1 : -1;
2342 } else {
2343 (void) nvlist_lookup_string(lnvl, propname,
2344 &lvstr);
2345 (void) nvlist_lookup_string(rnvl, propname,
2346 &rvstr);
2347 rc = strcmp(lvstr, rvstr);
2349 break;
2350 case ZFS_PROP_USED:
2351 case ZFS_PROP_QUOTA:
2352 if (!us_populated)
2353 break;
2354 if (prop == ZFS_PROP_USED)
2355 propname = "used";
2356 else
2357 propname = "quota";
2358 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2359 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2360 if (rv64 != lv64)
2361 rc = (rv64 < lv64) ? 1 : -1;
2362 break;
2364 default:
2365 break;
2368 if (rc != 0) {
2369 if (rc < 0)
2370 return (reverse ? 1 : -1);
2371 else
2372 return (reverse ? -1 : 1);
2377 * If entries still seem to be the same, check if they are of the same
2378 * type (smbentity is added only if we are doing SID to POSIX ID
2379 * translation where we can have duplicate type/name combinations).
2381 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2382 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2383 lvb != rvb)
2384 return (lvb < rvb ? -1 : 1);
2386 return (0);
2389 static inline const char *
2390 us_type2str(unsigned field_type)
2392 switch (field_type) {
2393 case USTYPE_PSX_USR:
2394 return ("POSIX User");
2395 case USTYPE_PSX_GRP:
2396 return ("POSIX Group");
2397 case USTYPE_SMB_USR:
2398 return ("SMB User");
2399 case USTYPE_SMB_GRP:
2400 return ("SMB Group");
2401 default:
2402 return ("Undefined");
2406 static int
2407 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2409 us_cbdata_t *cb = (us_cbdata_t *)arg;
2410 zfs_userquota_prop_t prop = cb->cb_prop;
2411 char *name = NULL;
2412 char *propname;
2413 char sizebuf[32];
2414 us_node_t *node;
2415 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2416 uu_avl_t *avl = cb->cb_avl;
2417 uu_avl_index_t idx;
2418 nvlist_t *props;
2419 us_node_t *n;
2420 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2421 unsigned type = 0;
2422 const char *typestr;
2423 size_t namelen;
2424 size_t typelen;
2425 size_t sizelen;
2426 int typeidx, nameidx, sizeidx;
2427 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2428 boolean_t smbentity = B_FALSE;
2430 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2431 nomem();
2432 node = safe_malloc(sizeof (us_node_t));
2433 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2434 node->usn_nvl = props;
2436 if (domain != NULL && domain[0] != '\0') {
2437 /* SMB */
2438 char sid[MAXNAMELEN + 32];
2439 uid_t id;
2440 int err;
2441 int flag = IDMAP_REQ_FLG_USE_CACHE;
2443 smbentity = B_TRUE;
2445 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2447 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2448 type = USTYPE_SMB_GRP;
2449 err = sid_to_id(sid, B_FALSE, &id);
2450 } else {
2451 type = USTYPE_SMB_USR;
2452 err = sid_to_id(sid, B_TRUE, &id);
2455 if (err == 0) {
2456 rid = id;
2457 if (!cb->cb_sid2posix) {
2458 if (type == USTYPE_SMB_USR) {
2459 (void) idmap_getwinnamebyuid(rid, flag,
2460 &name, NULL);
2461 } else {
2462 (void) idmap_getwinnamebygid(rid, flag,
2463 &name, NULL);
2465 if (name == NULL)
2466 name = sid;
2471 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2472 /* POSIX or -i */
2473 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2474 type = USTYPE_PSX_GRP;
2475 if (!cb->cb_numname) {
2476 struct group *g;
2478 if ((g = getgrgid(rid)) != NULL)
2479 name = g->gr_name;
2481 } else {
2482 type = USTYPE_PSX_USR;
2483 if (!cb->cb_numname) {
2484 struct passwd *p;
2486 if ((p = getpwuid(rid)) != NULL)
2487 name = p->pw_name;
2493 * Make sure that the type/name combination is unique when doing
2494 * SID to POSIX ID translation (hence changing the type from SMB to
2495 * POSIX).
2497 if (cb->cb_sid2posix &&
2498 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2499 nomem();
2501 /* Calculate/update width of TYPE field */
2502 typestr = us_type2str(type);
2503 typelen = strlen(gettext(typestr));
2504 typeidx = us_field_index("type");
2505 if (typelen > cb->cb_width[typeidx])
2506 cb->cb_width[typeidx] = typelen;
2507 if (nvlist_add_uint32(props, "type", type) != 0)
2508 nomem();
2510 /* Calculate/update width of NAME field */
2511 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2512 if (nvlist_add_uint64(props, "name", rid) != 0)
2513 nomem();
2514 namelen = snprintf(NULL, 0, "%u", rid);
2515 } else {
2516 if (nvlist_add_string(props, "name", name) != 0)
2517 nomem();
2518 namelen = strlen(name);
2520 nameidx = us_field_index("name");
2521 if (namelen > cb->cb_width[nameidx])
2522 cb->cb_width[nameidx] = namelen;
2525 * Check if this type/name combination is in the list and update it;
2526 * otherwise add new node to the list.
2528 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2529 uu_avl_insert(avl, node, idx);
2530 } else {
2531 nvlist_free(props);
2532 free(node);
2533 node = n;
2534 props = node->usn_nvl;
2537 /* Calculate/update width of USED/QUOTA fields */
2538 if (cb->cb_nicenum)
2539 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2540 else
2541 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2542 sizelen = strlen(sizebuf);
2543 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2544 propname = "used";
2545 if (!nvlist_exists(props, "quota"))
2546 (void) nvlist_add_uint64(props, "quota", 0);
2547 } else {
2548 propname = "quota";
2549 if (!nvlist_exists(props, "used"))
2550 (void) nvlist_add_uint64(props, "used", 0);
2552 sizeidx = us_field_index(propname);
2553 if (sizelen > cb->cb_width[sizeidx])
2554 cb->cb_width[sizeidx] = sizelen;
2556 if (nvlist_add_uint64(props, propname, space) != 0)
2557 nomem();
2559 return (0);
2562 static void
2563 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2564 size_t *width, us_node_t *node)
2566 nvlist_t *nvl = node->usn_nvl;
2567 char valstr[MAXNAMELEN];
2568 boolean_t first = B_TRUE;
2569 int cfield = 0;
2570 int field;
2571 uint32_t ustype;
2573 /* Check type */
2574 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2575 if (!(ustype & types))
2576 return;
2578 while ((field = fields[cfield]) != USFIELD_LAST) {
2579 nvpair_t *nvp = NULL;
2580 data_type_t type;
2581 uint32_t val32;
2582 uint64_t val64;
2583 char *strval = NULL;
2585 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2586 if (strcmp(nvpair_name(nvp),
2587 us_field_names[field]) == 0)
2588 break;
2591 type = nvpair_type(nvp);
2592 switch (type) {
2593 case DATA_TYPE_UINT32:
2594 (void) nvpair_value_uint32(nvp, &val32);
2595 break;
2596 case DATA_TYPE_UINT64:
2597 (void) nvpair_value_uint64(nvp, &val64);
2598 break;
2599 case DATA_TYPE_STRING:
2600 (void) nvpair_value_string(nvp, &strval);
2601 break;
2602 default:
2603 (void) fprintf(stderr, "invalid data type\n");
2606 switch (field) {
2607 case USFIELD_TYPE:
2608 strval = (char *)us_type2str(val32);
2609 break;
2610 case USFIELD_NAME:
2611 if (type == DATA_TYPE_UINT64) {
2612 (void) sprintf(valstr, "%llu", val64);
2613 strval = valstr;
2615 break;
2616 case USFIELD_USED:
2617 case USFIELD_QUOTA:
2618 if (type == DATA_TYPE_UINT64) {
2619 if (parsable) {
2620 (void) sprintf(valstr, "%llu", val64);
2621 } else {
2622 zfs_nicenum(val64, valstr,
2623 sizeof (valstr));
2625 if (field == USFIELD_QUOTA &&
2626 strcmp(valstr, "0") == 0)
2627 strval = "none";
2628 else
2629 strval = valstr;
2631 break;
2634 if (!first) {
2635 if (scripted)
2636 (void) printf("\t");
2637 else
2638 (void) printf(" ");
2640 if (scripted)
2641 (void) printf("%s", strval);
2642 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2643 (void) printf("%-*s", width[field], strval);
2644 else
2645 (void) printf("%*s", width[field], strval);
2647 first = B_FALSE;
2648 cfield++;
2651 (void) printf("\n");
2654 static void
2655 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2656 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2658 us_node_t *node;
2659 const char *col;
2660 int cfield = 0;
2661 int field;
2663 if (!scripted) {
2664 boolean_t first = B_TRUE;
2666 while ((field = fields[cfield]) != USFIELD_LAST) {
2667 col = gettext(us_field_hdr[field]);
2668 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2669 (void) printf(first ? "%-*s" : " %-*s",
2670 width[field], col);
2671 } else {
2672 (void) printf(first ? "%*s" : " %*s",
2673 width[field], col);
2675 first = B_FALSE;
2676 cfield++;
2678 (void) printf("\n");
2681 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2682 print_us_node(scripted, parsable, fields, types, width, node);
2683 if (rmnode)
2684 nvlist_free(node->usn_nvl);
2688 static int
2689 zfs_do_userspace(int argc, char **argv)
2691 zfs_handle_t *zhp;
2692 zfs_userquota_prop_t p;
2693 uu_avl_pool_t *avl_pool;
2694 uu_avl_t *avl_tree;
2695 uu_avl_walk_t *walk;
2696 char *delim;
2697 char deffields[] = "type,name,used,quota";
2698 char *ofield = NULL;
2699 char *tfield = NULL;
2700 int cfield = 0;
2701 int fields[256];
2702 int i;
2703 boolean_t scripted = B_FALSE;
2704 boolean_t prtnum = B_FALSE;
2705 boolean_t parsable = B_FALSE;
2706 boolean_t sid2posix = B_FALSE;
2707 int ret = 0;
2708 int c;
2709 zfs_sort_column_t *sortcol = NULL;
2710 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2711 us_cbdata_t cb;
2712 us_node_t *node;
2713 us_node_t *rmnode;
2714 uu_list_pool_t *listpool;
2715 uu_list_t *list;
2716 uu_avl_index_t idx = 0;
2717 uu_list_index_t idx2 = 0;
2719 if (argc < 2)
2720 usage(B_FALSE);
2722 if (strcmp(argv[0], "groupspace") == 0)
2723 /* Toggle default group types */
2724 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2726 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2727 switch (c) {
2728 case 'n':
2729 prtnum = B_TRUE;
2730 break;
2731 case 'H':
2732 scripted = B_TRUE;
2733 break;
2734 case 'p':
2735 parsable = B_TRUE;
2736 break;
2737 case 'o':
2738 ofield = optarg;
2739 break;
2740 case 's':
2741 case 'S':
2742 if (zfs_add_sort_column(&sortcol, optarg,
2743 c == 's' ? B_FALSE : B_TRUE) != 0) {
2744 (void) fprintf(stderr,
2745 gettext("invalid field '%s'\n"), optarg);
2746 usage(B_FALSE);
2748 break;
2749 case 't':
2750 tfield = optarg;
2751 break;
2752 case 'i':
2753 sid2posix = B_TRUE;
2754 break;
2755 case ':':
2756 (void) fprintf(stderr, gettext("missing argument for "
2757 "'%c' option\n"), optopt);
2758 usage(B_FALSE);
2759 break;
2760 case '?':
2761 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2762 optopt);
2763 usage(B_FALSE);
2767 argc -= optind;
2768 argv += optind;
2770 if (argc < 1) {
2771 (void) fprintf(stderr, gettext("missing dataset name\n"));
2772 usage(B_FALSE);
2774 if (argc > 1) {
2775 (void) fprintf(stderr, gettext("too many arguments\n"));
2776 usage(B_FALSE);
2779 /* Use default output fields if not specified using -o */
2780 if (ofield == NULL)
2781 ofield = deffields;
2782 do {
2783 if ((delim = strchr(ofield, ',')) != NULL)
2784 *delim = '\0';
2785 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2786 (void) fprintf(stderr, gettext("invalid type '%s' "
2787 "for -o option\n"), ofield);
2788 return (-1);
2790 if (delim != NULL)
2791 ofield = delim + 1;
2792 } while (delim != NULL);
2793 fields[cfield] = USFIELD_LAST;
2795 /* Override output types (-t option) */
2796 if (tfield != NULL) {
2797 types = 0;
2799 do {
2800 boolean_t found = B_FALSE;
2802 if ((delim = strchr(tfield, ',')) != NULL)
2803 *delim = '\0';
2804 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2805 i++) {
2806 if (strcmp(tfield, us_type_names[i]) == 0) {
2807 found = B_TRUE;
2808 types |= us_type_bits[i];
2809 break;
2812 if (!found) {
2813 (void) fprintf(stderr, gettext("invalid type "
2814 "'%s' for -t option\n"), tfield);
2815 return (-1);
2817 if (delim != NULL)
2818 tfield = delim + 1;
2819 } while (delim != NULL);
2822 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2823 return (1);
2825 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2826 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2827 nomem();
2828 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2829 nomem();
2831 /* Always add default sorting columns */
2832 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2833 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2835 cb.cb_sortcol = sortcol;
2836 cb.cb_numname = prtnum;
2837 cb.cb_nicenum = !parsable;
2838 cb.cb_avl_pool = avl_pool;
2839 cb.cb_avl = avl_tree;
2840 cb.cb_sid2posix = sid2posix;
2842 for (i = 0; i < USFIELD_LAST; i++)
2843 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2845 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2846 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2847 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2848 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2849 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2850 continue;
2851 cb.cb_prop = p;
2852 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2853 return (ret);
2856 /* Sort the list */
2857 if ((node = uu_avl_first(avl_tree)) == NULL)
2858 return (0);
2860 us_populated = B_TRUE;
2862 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2863 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2864 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2865 uu_list_node_init(node, &node->usn_listnode, listpool);
2867 while (node != NULL) {
2868 rmnode = node;
2869 node = uu_avl_next(avl_tree, node);
2870 uu_avl_remove(avl_tree, rmnode);
2871 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2872 uu_list_insert(list, rmnode, idx2);
2875 for (node = uu_list_first(list); node != NULL;
2876 node = uu_list_next(list, node)) {
2877 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2879 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2880 uu_avl_insert(avl_tree, node, idx);
2883 uu_list_destroy(list);
2884 uu_list_pool_destroy(listpool);
2886 /* Print and free node nvlist memory */
2887 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2888 cb.cb_avl);
2890 zfs_free_sort_columns(sortcol);
2892 /* Clean up the AVL tree */
2893 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2894 nomem();
2896 while ((node = uu_avl_walk_next(walk)) != NULL) {
2897 uu_avl_remove(cb.cb_avl, node);
2898 free(node);
2901 uu_avl_walk_end(walk);
2902 uu_avl_destroy(avl_tree);
2903 uu_avl_pool_destroy(avl_pool);
2905 return (ret);
2909 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2910 * [-t type[,...]] [filesystem|volume|snapshot] ...
2912 * -H Scripted mode; elide headers and separate columns by tabs.
2913 * -p Display values in parsable (literal) format.
2914 * -r Recurse over all children.
2915 * -d Limit recursion by depth.
2916 * -o Control which fields to display.
2917 * -s Specify sort columns, descending order.
2918 * -S Specify sort columns, ascending order.
2919 * -t Control which object types to display.
2921 * When given no arguments, list all filesystems in the system.
2922 * Otherwise, list the specified datasets, optionally recursing down them if
2923 * '-r' is specified.
2925 typedef struct list_cbdata {
2926 boolean_t cb_first;
2927 boolean_t cb_literal;
2928 boolean_t cb_scripted;
2929 zprop_list_t *cb_proplist;
2930 } list_cbdata_t;
2933 * Given a list of columns to display, output appropriate headers for each one.
2935 static void
2936 print_header(list_cbdata_t *cb)
2938 zprop_list_t *pl = cb->cb_proplist;
2939 char headerbuf[ZFS_MAXPROPLEN];
2940 const char *header;
2941 int i;
2942 boolean_t first = B_TRUE;
2943 boolean_t right_justify;
2945 for (; pl != NULL; pl = pl->pl_next) {
2946 if (!first) {
2947 (void) printf(" ");
2948 } else {
2949 first = B_FALSE;
2952 right_justify = B_FALSE;
2953 if (pl->pl_prop != ZPROP_INVAL) {
2954 header = zfs_prop_column_name(pl->pl_prop);
2955 right_justify = zfs_prop_align_right(pl->pl_prop);
2956 } else {
2957 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2958 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2959 headerbuf[i] = '\0';
2960 header = headerbuf;
2963 if (pl->pl_next == NULL && !right_justify)
2964 (void) printf("%s", header);
2965 else if (right_justify)
2966 (void) printf("%*s", pl->pl_width, header);
2967 else
2968 (void) printf("%-*s", pl->pl_width, header);
2971 (void) printf("\n");
2975 * Given a dataset and a list of fields, print out all the properties according
2976 * to the described layout.
2978 static void
2979 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2981 zprop_list_t *pl = cb->cb_proplist;
2982 boolean_t first = B_TRUE;
2983 char property[ZFS_MAXPROPLEN];
2984 nvlist_t *userprops = zfs_get_user_props(zhp);
2985 nvlist_t *propval;
2986 char *propstr;
2987 boolean_t right_justify;
2989 for (; pl != NULL; pl = pl->pl_next) {
2990 if (!first) {
2991 if (cb->cb_scripted)
2992 (void) printf("\t");
2993 else
2994 (void) printf(" ");
2995 } else {
2996 first = B_FALSE;
2999 if (pl->pl_prop == ZFS_PROP_NAME) {
3000 (void) strlcpy(property, zfs_get_name(zhp),
3001 sizeof (property));
3002 propstr = property;
3003 right_justify = zfs_prop_align_right(pl->pl_prop);
3004 } else if (pl->pl_prop != ZPROP_INVAL) {
3005 if (zfs_prop_get(zhp, pl->pl_prop, property,
3006 sizeof (property), NULL, NULL, 0,
3007 cb->cb_literal) != 0)
3008 propstr = "-";
3009 else
3010 propstr = property;
3011 right_justify = zfs_prop_align_right(pl->pl_prop);
3012 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3013 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3014 property, sizeof (property), cb->cb_literal) != 0)
3015 propstr = "-";
3016 else
3017 propstr = property;
3018 right_justify = B_TRUE;
3019 } else if (zfs_prop_written(pl->pl_user_prop)) {
3020 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3021 property, sizeof (property), cb->cb_literal) != 0)
3022 propstr = "-";
3023 else
3024 propstr = property;
3025 right_justify = B_TRUE;
3026 } else {
3027 if (nvlist_lookup_nvlist(userprops,
3028 pl->pl_user_prop, &propval) != 0)
3029 propstr = "-";
3030 else
3031 verify(nvlist_lookup_string(propval,
3032 ZPROP_VALUE, &propstr) == 0);
3033 right_justify = B_FALSE;
3037 * If this is being called in scripted mode, or if this is the
3038 * last column and it is left-justified, don't include a width
3039 * format specifier.
3041 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3042 (void) printf("%s", propstr);
3043 else if (right_justify)
3044 (void) printf("%*s", pl->pl_width, propstr);
3045 else
3046 (void) printf("%-*s", pl->pl_width, propstr);
3049 (void) printf("\n");
3053 * Generic callback function to list a dataset or snapshot.
3055 static int
3056 list_callback(zfs_handle_t *zhp, void *data)
3058 list_cbdata_t *cbp = data;
3060 if (cbp->cb_first) {
3061 if (!cbp->cb_scripted)
3062 print_header(cbp);
3063 cbp->cb_first = B_FALSE;
3066 print_dataset(zhp, cbp);
3068 return (0);
3071 static int
3072 zfs_do_list(int argc, char **argv)
3074 int c;
3075 static char default_fields[] =
3076 "name,used,available,referenced,mountpoint";
3077 int types = ZFS_TYPE_DATASET;
3078 boolean_t types_specified = B_FALSE;
3079 char *fields = NULL;
3080 list_cbdata_t cb = { 0 };
3081 char *value;
3082 int limit = 0;
3083 int ret = 0;
3084 zfs_sort_column_t *sortcol = NULL;
3085 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3087 /* check options */
3088 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3089 switch (c) {
3090 case 'o':
3091 fields = optarg;
3092 break;
3093 case 'p':
3094 cb.cb_literal = B_TRUE;
3095 flags |= ZFS_ITER_LITERAL_PROPS;
3096 break;
3097 case 'd':
3098 limit = parse_depth(optarg, &flags);
3099 break;
3100 case 'r':
3101 flags |= ZFS_ITER_RECURSE;
3102 break;
3103 case 'H':
3104 cb.cb_scripted = B_TRUE;
3105 break;
3106 case 's':
3107 if (zfs_add_sort_column(&sortcol, optarg,
3108 B_FALSE) != 0) {
3109 (void) fprintf(stderr,
3110 gettext("invalid property '%s'\n"), optarg);
3111 usage(B_FALSE);
3113 break;
3114 case 'S':
3115 if (zfs_add_sort_column(&sortcol, optarg,
3116 B_TRUE) != 0) {
3117 (void) fprintf(stderr,
3118 gettext("invalid property '%s'\n"), optarg);
3119 usage(B_FALSE);
3121 break;
3122 case 't':
3123 types = 0;
3124 types_specified = B_TRUE;
3125 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3126 while (*optarg != '\0') {
3127 static char *type_subopts[] = { "filesystem",
3128 "volume", "snapshot", "snap", "bookmark",
3129 "all", NULL };
3131 switch (getsubopt(&optarg, type_subopts,
3132 &value)) {
3133 case 0:
3134 types |= ZFS_TYPE_FILESYSTEM;
3135 break;
3136 case 1:
3137 types |= ZFS_TYPE_VOLUME;
3138 break;
3139 case 2:
3140 case 3:
3141 types |= ZFS_TYPE_SNAPSHOT;
3142 break;
3143 case 4:
3144 types |= ZFS_TYPE_BOOKMARK;
3145 break;
3146 case 5:
3147 types = ZFS_TYPE_DATASET |
3148 ZFS_TYPE_BOOKMARK;
3149 break;
3150 default:
3151 (void) fprintf(stderr,
3152 gettext("invalid type '%s'\n"),
3153 value);
3154 usage(B_FALSE);
3157 break;
3158 case ':':
3159 (void) fprintf(stderr, gettext("missing argument for "
3160 "'%c' option\n"), optopt);
3161 usage(B_FALSE);
3162 break;
3163 case '?':
3164 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3165 optopt);
3166 usage(B_FALSE);
3170 argc -= optind;
3171 argv += optind;
3173 if (fields == NULL)
3174 fields = default_fields;
3177 * If we are only going to list snapshot names and sort by name,
3178 * then we can use faster version.
3180 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3181 flags |= ZFS_ITER_SIMPLE;
3184 * If "-o space" and no types were specified, don't display snapshots.
3186 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3187 types &= ~ZFS_TYPE_SNAPSHOT;
3190 * If the user specifies '-o all', the zprop_get_list() doesn't
3191 * normally include the name of the dataset. For 'zfs list', we always
3192 * want this property to be first.
3194 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3195 != 0)
3196 usage(B_FALSE);
3198 cb.cb_first = B_TRUE;
3200 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3201 limit, list_callback, &cb);
3203 zprop_free_list(cb.cb_proplist);
3204 zfs_free_sort_columns(sortcol);
3206 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3207 (void) printf(gettext("no datasets available\n"));
3209 return (ret);
3213 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3214 * zfs rename [-f] -p <fs | vol> <fs | vol>
3215 * zfs rename -r <snap> <snap>
3217 * Renames the given dataset to another of the same type.
3219 * The '-p' flag creates all the non-existing ancestors of the target first.
3221 /* ARGSUSED */
3222 static int
3223 zfs_do_rename(int argc, char **argv)
3225 zfs_handle_t *zhp;
3226 int c;
3227 int ret = 0;
3228 boolean_t recurse = B_FALSE;
3229 boolean_t parents = B_FALSE;
3230 boolean_t force_unmount = B_FALSE;
3232 /* check options */
3233 while ((c = getopt(argc, argv, "prf")) != -1) {
3234 switch (c) {
3235 case 'p':
3236 parents = B_TRUE;
3237 break;
3238 case 'r':
3239 recurse = B_TRUE;
3240 break;
3241 case 'f':
3242 force_unmount = B_TRUE;
3243 break;
3244 case '?':
3245 default:
3246 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3247 optopt);
3248 usage(B_FALSE);
3252 argc -= optind;
3253 argv += optind;
3255 /* check number of arguments */
3256 if (argc < 1) {
3257 (void) fprintf(stderr, gettext("missing source dataset "
3258 "argument\n"));
3259 usage(B_FALSE);
3261 if (argc < 2) {
3262 (void) fprintf(stderr, gettext("missing target dataset "
3263 "argument\n"));
3264 usage(B_FALSE);
3266 if (argc > 2) {
3267 (void) fprintf(stderr, gettext("too many arguments\n"));
3268 usage(B_FALSE);
3271 if (recurse && parents) {
3272 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3273 "exclusive\n"));
3274 usage(B_FALSE);
3277 if (recurse && strchr(argv[0], '@') == 0) {
3278 (void) fprintf(stderr, gettext("source dataset for recursive "
3279 "rename must be a snapshot\n"));
3280 usage(B_FALSE);
3283 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3284 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3285 return (1);
3287 /* If we were asked and the name looks good, try to create ancestors. */
3288 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3289 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3290 zfs_close(zhp);
3291 return (1);
3294 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3296 zfs_close(zhp);
3297 return (ret);
3301 * zfs promote <fs>
3303 * Promotes the given clone fs to be the parent
3305 /* ARGSUSED */
3306 static int
3307 zfs_do_promote(int argc, char **argv)
3309 zfs_handle_t *zhp;
3310 int ret = 0;
3312 /* check options */
3313 if (argc > 1 && argv[1][0] == '-') {
3314 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3315 argv[1][1]);
3316 usage(B_FALSE);
3319 /* check number of arguments */
3320 if (argc < 2) {
3321 (void) fprintf(stderr, gettext("missing clone filesystem"
3322 " argument\n"));
3323 usage(B_FALSE);
3325 if (argc > 2) {
3326 (void) fprintf(stderr, gettext("too many arguments\n"));
3327 usage(B_FALSE);
3330 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3331 if (zhp == NULL)
3332 return (1);
3334 ret = (zfs_promote(zhp) != 0);
3337 zfs_close(zhp);
3338 return (ret);
3342 * zfs rollback [-rRf] <snapshot>
3344 * -r Delete any intervening snapshots before doing rollback
3345 * -R Delete any snapshots and their clones
3346 * -f ignored for backwards compatability
3348 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3349 * since then and making it the active dataset. If more recent snapshots exist,
3350 * the command will complain unless the '-r' flag is given.
3352 typedef struct rollback_cbdata {
3353 uint64_t cb_create;
3354 boolean_t cb_first;
3355 int cb_doclones;
3356 char *cb_target;
3357 int cb_error;
3358 boolean_t cb_recurse;
3359 } rollback_cbdata_t;
3361 static int
3362 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3364 rollback_cbdata_t *cbp = data;
3366 if (cbp->cb_first && cbp->cb_recurse) {
3367 (void) fprintf(stderr, gettext("cannot rollback to "
3368 "'%s': clones of previous snapshots exist\n"),
3369 cbp->cb_target);
3370 (void) fprintf(stderr, gettext("use '-R' to "
3371 "force deletion of the following clones and "
3372 "dependents:\n"));
3373 cbp->cb_first = 0;
3374 cbp->cb_error = 1;
3377 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3379 zfs_close(zhp);
3380 return (0);
3384 * Report any snapshots more recent than the one specified. Used when '-r' is
3385 * not specified. We reuse this same callback for the snapshot dependents - if
3386 * 'cb_dependent' is set, then this is a dependent and we should report it
3387 * without checking the transaction group.
3389 static int
3390 rollback_check(zfs_handle_t *zhp, void *data)
3392 rollback_cbdata_t *cbp = data;
3394 if (cbp->cb_doclones) {
3395 zfs_close(zhp);
3396 return (0);
3399 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3400 if (cbp->cb_first && !cbp->cb_recurse) {
3401 (void) fprintf(stderr, gettext("cannot "
3402 "rollback to '%s': more recent snapshots "
3403 "or bookmarks exist\n"),
3404 cbp->cb_target);
3405 (void) fprintf(stderr, gettext("use '-r' to "
3406 "force deletion of the following "
3407 "snapshots and bookmarks:\n"));
3408 cbp->cb_first = 0;
3409 cbp->cb_error = 1;
3412 if (cbp->cb_recurse) {
3413 if (zfs_iter_dependents(zhp, B_TRUE,
3414 rollback_check_dependent, cbp) != 0) {
3415 zfs_close(zhp);
3416 return (-1);
3418 } else {
3419 (void) fprintf(stderr, "%s\n",
3420 zfs_get_name(zhp));
3423 zfs_close(zhp);
3424 return (0);
3427 static int
3428 zfs_do_rollback(int argc, char **argv)
3430 int ret = 0;
3431 int c;
3432 boolean_t force = B_FALSE;
3433 rollback_cbdata_t cb = { 0 };
3434 zfs_handle_t *zhp, *snap;
3435 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3436 char *delim;
3438 /* check options */
3439 while ((c = getopt(argc, argv, "rRf")) != -1) {
3440 switch (c) {
3441 case 'r':
3442 cb.cb_recurse = 1;
3443 break;
3444 case 'R':
3445 cb.cb_recurse = 1;
3446 cb.cb_doclones = 1;
3447 break;
3448 case 'f':
3449 force = B_TRUE;
3450 break;
3451 case '?':
3452 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3453 optopt);
3454 usage(B_FALSE);
3458 argc -= optind;
3459 argv += optind;
3461 /* check number of arguments */
3462 if (argc < 1) {
3463 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3464 usage(B_FALSE);
3466 if (argc > 1) {
3467 (void) fprintf(stderr, gettext("too many arguments\n"));
3468 usage(B_FALSE);
3471 /* open the snapshot */
3472 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3473 return (1);
3475 /* open the parent dataset */
3476 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3477 verify((delim = strrchr(parentname, '@')) != NULL);
3478 *delim = '\0';
3479 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3480 zfs_close(snap);
3481 return (1);
3485 * Check for more recent snapshots and/or clones based on the presence
3486 * of '-r' and '-R'.
3488 cb.cb_target = argv[0];
3489 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3490 cb.cb_first = B_TRUE;
3491 cb.cb_error = 0;
3492 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3493 goto out;
3494 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3495 goto out;
3497 if ((ret = cb.cb_error) != 0)
3498 goto out;
3501 * Rollback parent to the given snapshot.
3503 ret = zfs_rollback(zhp, snap, force);
3505 out:
3506 zfs_close(snap);
3507 zfs_close(zhp);
3509 if (ret == 0)
3510 return (0);
3511 else
3512 return (1);
3516 * zfs set property=value ... { fs | snap | vol } ...
3518 * Sets the given properties for all datasets specified on the command line.
3521 static int
3522 set_callback(zfs_handle_t *zhp, void *data)
3524 nvlist_t *props = data;
3526 if (zfs_prop_set_list(zhp, props) != 0) {
3527 switch (libzfs_errno(g_zfs)) {
3528 case EZFS_MOUNTFAILED:
3529 (void) fprintf(stderr, gettext("property may be set "
3530 "but unable to remount filesystem\n"));
3531 break;
3532 case EZFS_SHARENFSFAILED:
3533 (void) fprintf(stderr, gettext("property may be set "
3534 "but unable to reshare filesystem\n"));
3535 break;
3537 return (1);
3539 return (0);
3542 static int
3543 zfs_do_set(int argc, char **argv)
3545 nvlist_t *props = NULL;
3546 int ds_start = -1; /* argv idx of first dataset arg */
3547 int ret = 0;
3549 /* check for options */
3550 if (argc > 1 && argv[1][0] == '-') {
3551 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3552 argv[1][1]);
3553 usage(B_FALSE);
3556 /* check number of arguments */
3557 if (argc < 2) {
3558 (void) fprintf(stderr, gettext("missing arguments\n"));
3559 usage(B_FALSE);
3561 if (argc < 3) {
3562 if (strchr(argv[1], '=') == NULL) {
3563 (void) fprintf(stderr, gettext("missing property=value "
3564 "argument(s)\n"));
3565 } else {
3566 (void) fprintf(stderr, gettext("missing dataset "
3567 "name(s)\n"));
3569 usage(B_FALSE);
3572 /* validate argument order: prop=val args followed by dataset args */
3573 for (int i = 1; i < argc; i++) {
3574 if (strchr(argv[i], '=') != NULL) {
3575 if (ds_start > 0) {
3576 /* out-of-order prop=val argument */
3577 (void) fprintf(stderr, gettext("invalid "
3578 "argument order\n"), i);
3579 usage(B_FALSE);
3581 } else if (ds_start < 0) {
3582 ds_start = i;
3585 if (ds_start < 0) {
3586 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3587 usage(B_FALSE);
3590 /* Populate a list of property settings */
3591 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3592 nomem();
3593 for (int i = 1; i < ds_start; i++) {
3594 if ((ret = parseprop(props, argv[i])) != 0)
3595 goto error;
3598 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3599 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3601 error:
3602 nvlist_free(props);
3603 return (ret);
3606 typedef struct snap_cbdata {
3607 nvlist_t *sd_nvl;
3608 boolean_t sd_recursive;
3609 const char *sd_snapname;
3610 } snap_cbdata_t;
3612 static int
3613 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3615 snap_cbdata_t *sd = arg;
3616 char *name;
3617 int rv = 0;
3618 int error;
3620 if (sd->sd_recursive &&
3621 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3622 zfs_close(zhp);
3623 return (0);
3626 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3627 if (error == -1)
3628 nomem();
3629 fnvlist_add_boolean(sd->sd_nvl, name);
3630 free(name);
3632 if (sd->sd_recursive)
3633 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3634 zfs_close(zhp);
3635 return (rv);
3639 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3641 * Creates a snapshot with the given name. While functionally equivalent to
3642 * 'zfs create', it is a separate command to differentiate intent.
3644 static int
3645 zfs_do_snapshot(int argc, char **argv)
3647 int ret = 0;
3648 char c;
3649 nvlist_t *props;
3650 snap_cbdata_t sd = { 0 };
3651 boolean_t multiple_snaps = B_FALSE;
3653 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3654 nomem();
3655 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3656 nomem();
3658 /* check options */
3659 while ((c = getopt(argc, argv, "ro:")) != -1) {
3660 switch (c) {
3661 case 'o':
3662 if (parseprop(props, optarg) != 0)
3663 return (1);
3664 break;
3665 case 'r':
3666 sd.sd_recursive = B_TRUE;
3667 multiple_snaps = B_TRUE;
3668 break;
3669 case '?':
3670 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3671 optopt);
3672 goto usage;
3676 argc -= optind;
3677 argv += optind;
3679 /* check number of arguments */
3680 if (argc < 1) {
3681 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3682 goto usage;
3685 if (argc > 1)
3686 multiple_snaps = B_TRUE;
3687 for (; argc > 0; argc--, argv++) {
3688 char *atp;
3689 zfs_handle_t *zhp;
3691 atp = strchr(argv[0], '@');
3692 if (atp == NULL)
3693 goto usage;
3694 *atp = '\0';
3695 sd.sd_snapname = atp + 1;
3696 zhp = zfs_open(g_zfs, argv[0],
3697 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3698 if (zhp == NULL)
3699 goto usage;
3700 if (zfs_snapshot_cb(zhp, &sd) != 0)
3701 goto usage;
3704 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3705 nvlist_free(sd.sd_nvl);
3706 nvlist_free(props);
3707 if (ret != 0 && multiple_snaps)
3708 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3709 return (ret != 0);
3711 usage:
3712 nvlist_free(sd.sd_nvl);
3713 nvlist_free(props);
3714 usage(B_FALSE);
3715 return (-1);
3719 * Send a backup stream to stdout.
3721 static int
3722 zfs_do_send(int argc, char **argv)
3724 char *fromname = NULL;
3725 char *toname = NULL;
3726 char *resume_token = NULL;
3727 char *cp;
3728 zfs_handle_t *zhp;
3729 sendflags_t flags = { 0 };
3730 int c, err;
3731 nvlist_t *dbgnv = NULL;
3732 boolean_t extraverbose = B_FALSE;
3734 struct option long_options[] = {
3735 {"replicate", no_argument, NULL, 'R'},
3736 {"props", no_argument, NULL, 'p'},
3737 {"parsable", no_argument, NULL, 'P'},
3738 {"dedup", no_argument, NULL, 'D'},
3739 {"verbose", no_argument, NULL, 'v'},
3740 {"dryrun", no_argument, NULL, 'n'},
3741 {"large-block", no_argument, NULL, 'L'},
3742 {"embed", no_argument, NULL, 'e'},
3743 {"resume", required_argument, NULL, 't'},
3744 {"compressed", no_argument, NULL, 'c'},
3745 {0, 0, 0, 0}
3748 /* check options */
3749 while ((c = getopt_long(argc, argv, ":i:I:RbDpvnPLet:c", long_options,
3750 NULL)) != -1) {
3751 switch (c) {
3752 case 'i':
3753 if (fromname)
3754 usage(B_FALSE);
3755 fromname = optarg;
3756 break;
3757 case 'I':
3758 if (fromname)
3759 usage(B_FALSE);
3760 fromname = optarg;
3761 flags.doall = B_TRUE;
3762 break;
3763 case 'R':
3764 flags.replicate = B_TRUE;
3765 break;
3766 case 'p':
3767 flags.props = B_TRUE;
3768 break;
3769 case 'P':
3770 flags.parsable = B_TRUE;
3771 flags.verbose = B_TRUE;
3772 break;
3773 case 'v':
3774 if (flags.verbose)
3775 extraverbose = B_TRUE;
3776 flags.verbose = B_TRUE;
3777 flags.progress = B_TRUE;
3778 break;
3779 case 'D':
3780 flags.dedup = B_TRUE;
3781 break;
3782 case 'n':
3783 flags.dryrun = B_TRUE;
3784 break;
3785 case 'L':
3786 flags.largeblock = B_TRUE;
3787 break;
3788 case 'e':
3789 flags.embed_data = B_TRUE;
3790 break;
3791 case 't':
3792 resume_token = optarg;
3793 break;
3794 case 'c':
3795 flags.compress = B_TRUE;
3796 break;
3797 case ':':
3799 * If a parameter was not passed, optopt contains the
3800 * value that would normally lead us into the
3801 * appropriate case statement. If it's > 256, then this
3802 * must be a longopt and we should look at argv to get
3803 * the string. Otherwise it's just the character, so we
3804 * should use it directly.
3806 if (optopt <= UINT8_MAX) {
3807 (void) fprintf(stderr,
3808 gettext("missing argument for '%c' "
3809 "option\n"), optopt);
3810 } else {
3811 (void) fprintf(stderr,
3812 gettext("missing argument for '%s' "
3813 "option\n"), argv[optind - 1]);
3815 usage(B_FALSE);
3816 break;
3817 case '?':
3818 /*FALLTHROUGH*/
3819 default:
3821 * If an invalid flag was passed, optopt contains the
3822 * character if it was a short flag, or 0 if it was a
3823 * longopt.
3825 if (optopt != 0) {
3826 (void) fprintf(stderr,
3827 gettext("invalid option '%c'\n"), optopt);
3828 } else {
3829 (void) fprintf(stderr,
3830 gettext("invalid option '%s'\n"),
3831 argv[optind - 1]);
3834 usage(B_FALSE);
3838 argc -= optind;
3839 argv += optind;
3841 if (resume_token != NULL) {
3842 if (fromname != NULL || flags.replicate || flags.props ||
3843 flags.dedup) {
3844 (void) fprintf(stderr,
3845 gettext("invalid flags combined with -t\n"));
3846 usage(B_FALSE);
3848 if (argc != 0) {
3849 (void) fprintf(stderr, gettext("no additional "
3850 "arguments are permitted with -t\n"));
3851 usage(B_FALSE);
3853 } else {
3854 if (argc < 1) {
3855 (void) fprintf(stderr,
3856 gettext("missing snapshot argument\n"));
3857 usage(B_FALSE);
3859 if (argc > 1) {
3860 (void) fprintf(stderr, gettext("too many arguments\n"));
3861 usage(B_FALSE);
3865 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3866 (void) fprintf(stderr,
3867 gettext("Error: Stream can not be written to a terminal.\n"
3868 "You must redirect standard output.\n"));
3869 return (1);
3872 if (resume_token != NULL) {
3873 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3874 resume_token));
3878 * Special case sending a filesystem, or from a bookmark.
3880 if (strchr(argv[0], '@') == NULL ||
3881 (fromname && strchr(fromname, '#') != NULL)) {
3882 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
3883 enum lzc_send_flags lzc_flags = 0;
3885 if (flags.replicate || flags.doall || flags.props ||
3886 flags.dedup || flags.dryrun || flags.verbose ||
3887 flags.progress) {
3888 (void) fprintf(stderr,
3889 gettext("Error: "
3890 "Unsupported flag with filesystem or bookmark.\n"));
3891 return (1);
3894 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3895 if (zhp == NULL)
3896 return (1);
3898 if (flags.largeblock)
3899 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3900 if (flags.embed_data)
3901 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3902 if (flags.compress)
3903 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
3905 if (fromname != NULL &&
3906 (fromname[0] == '#' || fromname[0] == '@')) {
3908 * Incremental source name begins with # or @.
3909 * Default to same fs as target.
3911 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3912 cp = strchr(frombuf, '@');
3913 if (cp != NULL)
3914 *cp = '\0';
3915 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3916 fromname = frombuf;
3918 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3919 zfs_close(zhp);
3920 return (err != 0);
3923 cp = strchr(argv[0], '@');
3924 *cp = '\0';
3925 toname = cp + 1;
3926 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3927 if (zhp == NULL)
3928 return (1);
3931 * If they specified the full path to the snapshot, chop off
3932 * everything except the short name of the snapshot, but special
3933 * case if they specify the origin.
3935 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3936 char origin[ZFS_MAX_DATASET_NAME_LEN];
3937 zprop_source_t src;
3939 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3940 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3942 if (strcmp(origin, fromname) == 0) {
3943 fromname = NULL;
3944 flags.fromorigin = B_TRUE;
3945 } else {
3946 *cp = '\0';
3947 if (cp != fromname && strcmp(argv[0], fromname)) {
3948 (void) fprintf(stderr,
3949 gettext("incremental source must be "
3950 "in same filesystem\n"));
3951 usage(B_FALSE);
3953 fromname = cp + 1;
3954 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3955 (void) fprintf(stderr,
3956 gettext("invalid incremental source\n"));
3957 usage(B_FALSE);
3962 if (flags.replicate && fromname == NULL)
3963 flags.doall = B_TRUE;
3965 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3966 extraverbose ? &dbgnv : NULL);
3968 if (extraverbose && dbgnv != NULL) {
3970 * dump_nvlist prints to stdout, but that's been
3971 * redirected to a file. Make it print to stderr
3972 * instead.
3974 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3975 dump_nvlist(dbgnv, 0);
3976 nvlist_free(dbgnv);
3978 zfs_close(zhp);
3980 return (err != 0);
3984 * Restore a backup stream from stdin.
3986 static int
3987 zfs_do_receive(int argc, char **argv)
3989 int c, err = 0;
3990 recvflags_t flags = { 0 };
3991 boolean_t abort_resumable = B_FALSE;
3993 nvlist_t *props;
3994 nvpair_t *nvp = NULL;
3996 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3997 nomem();
3999 /* check options */
4000 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
4001 switch (c) {
4002 case 'o':
4003 if (parseprop(props, optarg) != 0)
4004 return (1);
4005 break;
4006 case 'd':
4007 flags.isprefix = B_TRUE;
4008 break;
4009 case 'e':
4010 flags.isprefix = B_TRUE;
4011 flags.istail = B_TRUE;
4012 break;
4013 case 'n':
4014 flags.dryrun = B_TRUE;
4015 break;
4016 case 'u':
4017 flags.nomount = B_TRUE;
4018 break;
4019 case 'v':
4020 flags.verbose = B_TRUE;
4021 break;
4022 case 's':
4023 flags.resumable = B_TRUE;
4024 break;
4025 case 'F':
4026 flags.force = B_TRUE;
4027 break;
4028 case 'A':
4029 abort_resumable = B_TRUE;
4030 break;
4031 case ':':
4032 (void) fprintf(stderr, gettext("missing argument for "
4033 "'%c' option\n"), optopt);
4034 usage(B_FALSE);
4035 break;
4036 case '?':
4037 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4038 optopt);
4039 usage(B_FALSE);
4043 argc -= optind;
4044 argv += optind;
4046 /* check number of arguments */
4047 if (argc < 1) {
4048 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4049 usage(B_FALSE);
4051 if (argc > 1) {
4052 (void) fprintf(stderr, gettext("too many arguments\n"));
4053 usage(B_FALSE);
4056 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4057 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4058 (void) fprintf(stderr, gettext("invalid option"));
4059 usage(B_FALSE);
4063 if (abort_resumable) {
4064 if (flags.isprefix || flags.istail || flags.dryrun ||
4065 flags.resumable || flags.nomount) {
4066 (void) fprintf(stderr, gettext("invalid option"));
4067 usage(B_FALSE);
4070 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4071 (void) snprintf(namebuf, sizeof (namebuf),
4072 "%s/%%recv", argv[0]);
4074 if (zfs_dataset_exists(g_zfs, namebuf,
4075 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4076 zfs_handle_t *zhp = zfs_open(g_zfs,
4077 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4078 if (zhp == NULL)
4079 return (1);
4080 err = zfs_destroy(zhp, B_FALSE);
4081 } else {
4082 zfs_handle_t *zhp = zfs_open(g_zfs,
4083 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4084 if (zhp == NULL)
4085 usage(B_FALSE);
4086 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4087 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4088 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4089 (void) fprintf(stderr,
4090 gettext("'%s' does not have any "
4091 "resumable receive state to abort\n"),
4092 argv[0]);
4093 return (1);
4095 err = zfs_destroy(zhp, B_FALSE);
4098 return (err != 0);
4101 if (isatty(STDIN_FILENO)) {
4102 (void) fprintf(stderr,
4103 gettext("Error: Backup stream can not be read "
4104 "from a terminal.\n"
4105 "You must redirect standard input.\n"));
4106 return (1);
4108 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4110 return (err != 0);
4114 * allow/unallow stuff
4116 /* copied from zfs/sys/dsl_deleg.h */
4117 #define ZFS_DELEG_PERM_CREATE "create"
4118 #define ZFS_DELEG_PERM_DESTROY "destroy"
4119 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4120 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4121 #define ZFS_DELEG_PERM_CLONE "clone"
4122 #define ZFS_DELEG_PERM_PROMOTE "promote"
4123 #define ZFS_DELEG_PERM_RENAME "rename"
4124 #define ZFS_DELEG_PERM_MOUNT "mount"
4125 #define ZFS_DELEG_PERM_SHARE "share"
4126 #define ZFS_DELEG_PERM_SEND "send"
4127 #define ZFS_DELEG_PERM_RECEIVE "receive"
4128 #define ZFS_DELEG_PERM_ALLOW "allow"
4129 #define ZFS_DELEG_PERM_USERPROP "userprop"
4130 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4131 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4132 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4133 #define ZFS_DELEG_PERM_USERUSED "userused"
4134 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4135 #define ZFS_DELEG_PERM_HOLD "hold"
4136 #define ZFS_DELEG_PERM_RELEASE "release"
4137 #define ZFS_DELEG_PERM_DIFF "diff"
4138 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4139 #define ZFS_DELEG_PERM_REMAP "remap"
4141 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4143 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4144 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4145 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4146 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4147 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4148 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4149 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4150 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4151 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4152 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4153 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4154 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4155 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4156 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4157 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4158 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4159 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4160 { ZFS_DELEG_PERM_REMAP, ZFS_DELEG_NOTE_REMAP },
4162 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4163 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4164 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4165 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4166 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4167 { NULL, ZFS_DELEG_NOTE_NONE }
4170 /* permission structure */
4171 typedef struct deleg_perm {
4172 zfs_deleg_who_type_t dp_who_type;
4173 const char *dp_name;
4174 boolean_t dp_local;
4175 boolean_t dp_descend;
4176 } deleg_perm_t;
4178 /* */
4179 typedef struct deleg_perm_node {
4180 deleg_perm_t dpn_perm;
4182 uu_avl_node_t dpn_avl_node;
4183 } deleg_perm_node_t;
4185 typedef struct fs_perm fs_perm_t;
4187 /* permissions set */
4188 typedef struct who_perm {
4189 zfs_deleg_who_type_t who_type;
4190 const char *who_name; /* id */
4191 char who_ug_name[256]; /* user/group name */
4192 fs_perm_t *who_fsperm; /* uplink */
4194 uu_avl_t *who_deleg_perm_avl; /* permissions */
4195 } who_perm_t;
4197 /* */
4198 typedef struct who_perm_node {
4199 who_perm_t who_perm;
4200 uu_avl_node_t who_avl_node;
4201 } who_perm_node_t;
4203 typedef struct fs_perm_set fs_perm_set_t;
4204 /* fs permissions */
4205 struct fs_perm {
4206 const char *fsp_name;
4208 uu_avl_t *fsp_sc_avl; /* sets,create */
4209 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4211 fs_perm_set_t *fsp_set; /* uplink */
4214 /* */
4215 typedef struct fs_perm_node {
4216 fs_perm_t fspn_fsperm;
4217 uu_avl_t *fspn_avl;
4219 uu_list_node_t fspn_list_node;
4220 } fs_perm_node_t;
4222 /* top level structure */
4223 struct fs_perm_set {
4224 uu_list_pool_t *fsps_list_pool;
4225 uu_list_t *fsps_list; /* list of fs_perms */
4227 uu_avl_pool_t *fsps_named_set_avl_pool;
4228 uu_avl_pool_t *fsps_who_perm_avl_pool;
4229 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4232 static inline const char *
4233 deleg_perm_type(zfs_deleg_note_t note)
4235 /* subcommands */
4236 switch (note) {
4237 /* SUBCOMMANDS */
4238 /* OTHER */
4239 case ZFS_DELEG_NOTE_GROUPQUOTA:
4240 case ZFS_DELEG_NOTE_GROUPUSED:
4241 case ZFS_DELEG_NOTE_USERPROP:
4242 case ZFS_DELEG_NOTE_USERQUOTA:
4243 case ZFS_DELEG_NOTE_USERUSED:
4244 /* other */
4245 return (gettext("other"));
4246 default:
4247 return (gettext("subcommand"));
4251 static int
4252 who_type2weight(zfs_deleg_who_type_t who_type)
4254 int res;
4255 switch (who_type) {
4256 case ZFS_DELEG_NAMED_SET_SETS:
4257 case ZFS_DELEG_NAMED_SET:
4258 res = 0;
4259 break;
4260 case ZFS_DELEG_CREATE_SETS:
4261 case ZFS_DELEG_CREATE:
4262 res = 1;
4263 break;
4264 case ZFS_DELEG_USER_SETS:
4265 case ZFS_DELEG_USER:
4266 res = 2;
4267 break;
4268 case ZFS_DELEG_GROUP_SETS:
4269 case ZFS_DELEG_GROUP:
4270 res = 3;
4271 break;
4272 case ZFS_DELEG_EVERYONE_SETS:
4273 case ZFS_DELEG_EVERYONE:
4274 res = 4;
4275 break;
4276 default:
4277 res = -1;
4280 return (res);
4283 /* ARGSUSED */
4284 static int
4285 who_perm_compare(const void *larg, const void *rarg, void *unused)
4287 const who_perm_node_t *l = larg;
4288 const who_perm_node_t *r = rarg;
4289 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4290 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4291 int lweight = who_type2weight(ltype);
4292 int rweight = who_type2weight(rtype);
4293 int res = lweight - rweight;
4294 if (res == 0)
4295 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4296 ZFS_MAX_DELEG_NAME-1);
4298 if (res == 0)
4299 return (0);
4300 if (res > 0)
4301 return (1);
4302 else
4303 return (-1);
4306 /* ARGSUSED */
4307 static int
4308 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4310 const deleg_perm_node_t *l = larg;
4311 const deleg_perm_node_t *r = rarg;
4312 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4313 ZFS_MAX_DELEG_NAME-1);
4315 if (res == 0)
4316 return (0);
4318 if (res > 0)
4319 return (1);
4320 else
4321 return (-1);
4324 static inline void
4325 fs_perm_set_init(fs_perm_set_t *fspset)
4327 bzero(fspset, sizeof (fs_perm_set_t));
4329 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4330 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4331 NULL, UU_DEFAULT)) == NULL)
4332 nomem();
4333 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4334 UU_DEFAULT)) == NULL)
4335 nomem();
4337 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4338 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4339 who_perm_node_t, who_avl_node), who_perm_compare,
4340 UU_DEFAULT)) == NULL)
4341 nomem();
4343 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4344 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4345 who_perm_node_t, who_avl_node), who_perm_compare,
4346 UU_DEFAULT)) == NULL)
4347 nomem();
4349 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4350 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4351 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4352 == NULL)
4353 nomem();
4356 static inline void fs_perm_fini(fs_perm_t *);
4357 static inline void who_perm_fini(who_perm_t *);
4359 static inline void
4360 fs_perm_set_fini(fs_perm_set_t *fspset)
4362 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4364 while (node != NULL) {
4365 fs_perm_node_t *next_node =
4366 uu_list_next(fspset->fsps_list, node);
4367 fs_perm_t *fsperm = &node->fspn_fsperm;
4368 fs_perm_fini(fsperm);
4369 uu_list_remove(fspset->fsps_list, node);
4370 free(node);
4371 node = next_node;
4374 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4375 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4376 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4379 static inline void
4380 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4381 const char *name)
4383 deleg_perm->dp_who_type = type;
4384 deleg_perm->dp_name = name;
4387 static inline void
4388 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4389 zfs_deleg_who_type_t type, const char *name)
4391 uu_avl_pool_t *pool;
4392 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4394 bzero(who_perm, sizeof (who_perm_t));
4396 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4397 UU_DEFAULT)) == NULL)
4398 nomem();
4400 who_perm->who_type = type;
4401 who_perm->who_name = name;
4402 who_perm->who_fsperm = fsperm;
4405 static inline void
4406 who_perm_fini(who_perm_t *who_perm)
4408 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4410 while (node != NULL) {
4411 deleg_perm_node_t *next_node =
4412 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4414 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4415 free(node);
4416 node = next_node;
4419 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4422 static inline void
4423 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4425 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4426 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4428 bzero(fsperm, sizeof (fs_perm_t));
4430 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4431 == NULL)
4432 nomem();
4434 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4435 == NULL)
4436 nomem();
4438 fsperm->fsp_set = fspset;
4439 fsperm->fsp_name = fsname;
4442 static inline void
4443 fs_perm_fini(fs_perm_t *fsperm)
4445 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4446 while (node != NULL) {
4447 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4448 node);
4449 who_perm_t *who_perm = &node->who_perm;
4450 who_perm_fini(who_perm);
4451 uu_avl_remove(fsperm->fsp_sc_avl, node);
4452 free(node);
4453 node = next_node;
4456 node = uu_avl_first(fsperm->fsp_uge_avl);
4457 while (node != NULL) {
4458 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4459 node);
4460 who_perm_t *who_perm = &node->who_perm;
4461 who_perm_fini(who_perm);
4462 uu_avl_remove(fsperm->fsp_uge_avl, node);
4463 free(node);
4464 node = next_node;
4467 uu_avl_destroy(fsperm->fsp_sc_avl);
4468 uu_avl_destroy(fsperm->fsp_uge_avl);
4471 static void
4472 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4473 zfs_deleg_who_type_t who_type, const char *name, char locality)
4475 uu_avl_index_t idx = 0;
4477 deleg_perm_node_t *found_node = NULL;
4478 deleg_perm_t *deleg_perm = &node->dpn_perm;
4480 deleg_perm_init(deleg_perm, who_type, name);
4482 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4483 == NULL)
4484 uu_avl_insert(avl, node, idx);
4485 else {
4486 node = found_node;
4487 deleg_perm = &node->dpn_perm;
4491 switch (locality) {
4492 case ZFS_DELEG_LOCAL:
4493 deleg_perm->dp_local = B_TRUE;
4494 break;
4495 case ZFS_DELEG_DESCENDENT:
4496 deleg_perm->dp_descend = B_TRUE;
4497 break;
4498 case ZFS_DELEG_NA:
4499 break;
4500 default:
4501 assert(B_FALSE); /* invalid locality */
4505 static inline int
4506 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4508 nvpair_t *nvp = NULL;
4509 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4510 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4511 zfs_deleg_who_type_t who_type = who_perm->who_type;
4513 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4514 const char *name = nvpair_name(nvp);
4515 data_type_t type = nvpair_type(nvp);
4516 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4517 deleg_perm_node_t *node =
4518 safe_malloc(sizeof (deleg_perm_node_t));
4520 assert(type == DATA_TYPE_BOOLEAN);
4522 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4523 set_deleg_perm_node(avl, node, who_type, name, locality);
4526 return (0);
4529 static inline int
4530 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4532 nvpair_t *nvp = NULL;
4533 fs_perm_set_t *fspset = fsperm->fsp_set;
4535 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4536 nvlist_t *nvl2 = NULL;
4537 const char *name = nvpair_name(nvp);
4538 uu_avl_t *avl = NULL;
4539 uu_avl_pool_t *avl_pool = NULL;
4540 zfs_deleg_who_type_t perm_type = name[0];
4541 char perm_locality = name[1];
4542 const char *perm_name = name + 3;
4543 boolean_t is_set = B_TRUE;
4544 who_perm_t *who_perm = NULL;
4546 assert('$' == name[2]);
4548 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4549 return (-1);
4551 switch (perm_type) {
4552 case ZFS_DELEG_CREATE:
4553 case ZFS_DELEG_CREATE_SETS:
4554 case ZFS_DELEG_NAMED_SET:
4555 case ZFS_DELEG_NAMED_SET_SETS:
4556 avl_pool = fspset->fsps_named_set_avl_pool;
4557 avl = fsperm->fsp_sc_avl;
4558 break;
4559 case ZFS_DELEG_USER:
4560 case ZFS_DELEG_USER_SETS:
4561 case ZFS_DELEG_GROUP:
4562 case ZFS_DELEG_GROUP_SETS:
4563 case ZFS_DELEG_EVERYONE:
4564 case ZFS_DELEG_EVERYONE_SETS:
4565 avl_pool = fspset->fsps_who_perm_avl_pool;
4566 avl = fsperm->fsp_uge_avl;
4567 break;
4569 default:
4570 assert(!"unhandled zfs_deleg_who_type_t");
4573 if (is_set) {
4574 who_perm_node_t *found_node = NULL;
4575 who_perm_node_t *node = safe_malloc(
4576 sizeof (who_perm_node_t));
4577 who_perm = &node->who_perm;
4578 uu_avl_index_t idx = 0;
4580 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4581 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4583 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4584 == NULL) {
4585 if (avl == fsperm->fsp_uge_avl) {
4586 uid_t rid = 0;
4587 struct passwd *p = NULL;
4588 struct group *g = NULL;
4589 const char *nice_name = NULL;
4591 switch (perm_type) {
4592 case ZFS_DELEG_USER_SETS:
4593 case ZFS_DELEG_USER:
4594 rid = atoi(perm_name);
4595 p = getpwuid(rid);
4596 if (p)
4597 nice_name = p->pw_name;
4598 break;
4599 case ZFS_DELEG_GROUP_SETS:
4600 case ZFS_DELEG_GROUP:
4601 rid = atoi(perm_name);
4602 g = getgrgid(rid);
4603 if (g)
4604 nice_name = g->gr_name;
4605 break;
4607 default:
4608 break;
4611 if (nice_name != NULL)
4612 (void) strlcpy(
4613 node->who_perm.who_ug_name,
4614 nice_name, 256);
4617 uu_avl_insert(avl, node, idx);
4618 } else {
4619 node = found_node;
4620 who_perm = &node->who_perm;
4624 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4627 return (0);
4630 static inline int
4631 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4633 nvpair_t *nvp = NULL;
4634 uu_avl_index_t idx = 0;
4636 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4637 nvlist_t *nvl2 = NULL;
4638 const char *fsname = nvpair_name(nvp);
4639 data_type_t type = nvpair_type(nvp);
4640 fs_perm_t *fsperm = NULL;
4641 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4642 if (node == NULL)
4643 nomem();
4645 fsperm = &node->fspn_fsperm;
4647 assert(DATA_TYPE_NVLIST == type);
4649 uu_list_node_init(node, &node->fspn_list_node,
4650 fspset->fsps_list_pool);
4652 idx = uu_list_numnodes(fspset->fsps_list);
4653 fs_perm_init(fsperm, fspset, fsname);
4655 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4656 return (-1);
4658 (void) parse_fs_perm(fsperm, nvl2);
4660 uu_list_insert(fspset->fsps_list, node, idx);
4663 return (0);
4666 static inline const char *
4667 deleg_perm_comment(zfs_deleg_note_t note)
4669 const char *str = "";
4671 /* subcommands */
4672 switch (note) {
4673 /* SUBCOMMANDS */
4674 case ZFS_DELEG_NOTE_ALLOW:
4675 str = gettext("Must also have the permission that is being"
4676 "\n\t\t\t\tallowed");
4677 break;
4678 case ZFS_DELEG_NOTE_CLONE:
4679 str = gettext("Must also have the 'create' ability and 'mount'"
4680 "\n\t\t\t\tability in the origin file system");
4681 break;
4682 case ZFS_DELEG_NOTE_CREATE:
4683 str = gettext("Must also have the 'mount' ability");
4684 break;
4685 case ZFS_DELEG_NOTE_DESTROY:
4686 str = gettext("Must also have the 'mount' ability");
4687 break;
4688 case ZFS_DELEG_NOTE_DIFF:
4689 str = gettext("Allows lookup of paths within a dataset;"
4690 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4691 "\n\t\t\t\tin order to use zfs diff");
4692 break;
4693 case ZFS_DELEG_NOTE_HOLD:
4694 str = gettext("Allows adding a user hold to a snapshot");
4695 break;
4696 case ZFS_DELEG_NOTE_MOUNT:
4697 str = gettext("Allows mount/umount of ZFS datasets");
4698 break;
4699 case ZFS_DELEG_NOTE_PROMOTE:
4700 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4701 " 'promote' ability in the origin file system");
4702 break;
4703 case ZFS_DELEG_NOTE_RECEIVE:
4704 str = gettext("Must also have the 'mount' and 'create'"
4705 " ability");
4706 break;
4707 case ZFS_DELEG_NOTE_RELEASE:
4708 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4709 "might destroy the snapshot");
4710 break;
4711 case ZFS_DELEG_NOTE_RENAME:
4712 str = gettext("Must also have the 'mount' and 'create'"
4713 "\n\t\t\t\tability in the new parent");
4714 break;
4715 case ZFS_DELEG_NOTE_ROLLBACK:
4716 str = gettext("");
4717 break;
4718 case ZFS_DELEG_NOTE_SEND:
4719 str = gettext("");
4720 break;
4721 case ZFS_DELEG_NOTE_SHARE:
4722 str = gettext("Allows sharing file systems over NFS or SMB"
4723 "\n\t\t\t\tprotocols");
4724 break;
4725 case ZFS_DELEG_NOTE_SNAPSHOT:
4726 str = gettext("");
4727 break;
4729 * case ZFS_DELEG_NOTE_VSCAN:
4730 * str = gettext("");
4731 * break;
4733 /* OTHER */
4734 case ZFS_DELEG_NOTE_GROUPQUOTA:
4735 str = gettext("Allows accessing any groupquota@... property");
4736 break;
4737 case ZFS_DELEG_NOTE_GROUPUSED:
4738 str = gettext("Allows reading any groupused@... property");
4739 break;
4740 case ZFS_DELEG_NOTE_USERPROP:
4741 str = gettext("Allows changing any user property");
4742 break;
4743 case ZFS_DELEG_NOTE_USERQUOTA:
4744 str = gettext("Allows accessing any userquota@... property");
4745 break;
4746 case ZFS_DELEG_NOTE_USERUSED:
4747 str = gettext("Allows reading any userused@... property");
4748 break;
4749 /* other */
4750 default:
4751 str = "";
4754 return (str);
4757 struct allow_opts {
4758 boolean_t local;
4759 boolean_t descend;
4760 boolean_t user;
4761 boolean_t group;
4762 boolean_t everyone;
4763 boolean_t create;
4764 boolean_t set;
4765 boolean_t recursive; /* unallow only */
4766 boolean_t prt_usage;
4768 boolean_t prt_perms;
4769 char *who;
4770 char *perms;
4771 const char *dataset;
4774 static inline int
4775 prop_cmp(const void *a, const void *b)
4777 const char *str1 = *(const char **)a;
4778 const char *str2 = *(const char **)b;
4779 return (strcmp(str1, str2));
4782 static void
4783 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4785 const char *opt_desc[] = {
4786 "-h", gettext("show this help message and exit"),
4787 "-l", gettext("set permission locally"),
4788 "-d", gettext("set permission for descents"),
4789 "-u", gettext("set permission for user"),
4790 "-g", gettext("set permission for group"),
4791 "-e", gettext("set permission for everyone"),
4792 "-c", gettext("set create time permission"),
4793 "-s", gettext("define permission set"),
4794 /* unallow only */
4795 "-r", gettext("remove permissions recursively"),
4797 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4798 size_t allow_size = unallow_size - 2;
4799 const char *props[ZFS_NUM_PROPS];
4800 int i;
4801 size_t count = 0;
4802 FILE *fp = requested ? stdout : stderr;
4803 zprop_desc_t *pdtbl = zfs_prop_get_table();
4804 const char *fmt = gettext("%-16s %-14s\t%s\n");
4806 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4807 HELP_ALLOW));
4808 (void) fprintf(fp, gettext("Options:\n"));
4809 for (int i = 0; i < (un ? unallow_size : allow_size); i++) {
4810 const char *opt = opt_desc[i++];
4811 const char *optdsc = opt_desc[i];
4812 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4815 (void) fprintf(fp, gettext("\nThe following permissions are "
4816 "supported:\n\n"));
4817 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4818 gettext("NOTES"));
4819 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4820 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4821 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4822 const char *perm_type = deleg_perm_type(perm_note);
4823 const char *perm_comment = deleg_perm_comment(perm_note);
4824 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4827 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4828 zprop_desc_t *pd = &pdtbl[i];
4829 if (pd->pd_visible != B_TRUE)
4830 continue;
4832 if (pd->pd_attr == PROP_READONLY)
4833 continue;
4835 props[count++] = pd->pd_name;
4837 props[count] = NULL;
4839 qsort(props, count, sizeof (char *), prop_cmp);
4841 for (i = 0; i < count; i++)
4842 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4844 if (msg != NULL)
4845 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4847 exit(requested ? 0 : 2);
4850 static inline const char *
4851 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4852 char **permsp)
4854 if (un && argc == expected_argc - 1)
4855 *permsp = NULL;
4856 else if (argc == expected_argc)
4857 *permsp = argv[argc - 2];
4858 else
4859 allow_usage(un, B_FALSE,
4860 gettext("wrong number of parameters\n"));
4862 return (argv[argc - 1]);
4865 static void
4866 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4868 int uge_sum = opts->user + opts->group + opts->everyone;
4869 int csuge_sum = opts->create + opts->set + uge_sum;
4870 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4871 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4873 if (uge_sum > 1)
4874 allow_usage(un, B_FALSE,
4875 gettext("-u, -g, and -e are mutually exclusive\n"));
4877 if (opts->prt_usage) {
4878 if (argc == 0 && all_sum == 0)
4879 allow_usage(un, B_TRUE, NULL);
4880 else
4881 usage(B_FALSE);
4884 if (opts->set) {
4885 if (csuge_sum > 1)
4886 allow_usage(un, B_FALSE,
4887 gettext("invalid options combined with -s\n"));
4889 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4890 if (argv[0][0] != '@')
4891 allow_usage(un, B_FALSE,
4892 gettext("invalid set name: missing '@' prefix\n"));
4893 opts->who = argv[0];
4894 } else if (opts->create) {
4895 if (ldcsuge_sum > 1)
4896 allow_usage(un, B_FALSE,
4897 gettext("invalid options combined with -c\n"));
4898 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4899 } else if (opts->everyone) {
4900 if (csuge_sum > 1)
4901 allow_usage(un, B_FALSE,
4902 gettext("invalid options combined with -e\n"));
4903 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4904 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4905 == 0) {
4906 opts->everyone = B_TRUE;
4907 argc--;
4908 argv++;
4909 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4910 } else if (argc == 1 && !un) {
4911 opts->prt_perms = B_TRUE;
4912 opts->dataset = argv[argc-1];
4913 } else {
4914 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4915 opts->who = argv[0];
4918 if (!opts->local && !opts->descend) {
4919 opts->local = B_TRUE;
4920 opts->descend = B_TRUE;
4924 static void
4925 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4926 const char *who, char *perms, nvlist_t *top_nvl)
4928 int i;
4929 char ld[2] = { '\0', '\0' };
4930 char who_buf[MAXNAMELEN + 32];
4931 char base_type = '\0';
4932 char set_type = '\0';
4933 nvlist_t *base_nvl = NULL;
4934 nvlist_t *set_nvl = NULL;
4935 nvlist_t *nvl;
4937 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4938 nomem();
4939 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4940 nomem();
4942 switch (type) {
4943 case ZFS_DELEG_NAMED_SET_SETS:
4944 case ZFS_DELEG_NAMED_SET:
4945 set_type = ZFS_DELEG_NAMED_SET_SETS;
4946 base_type = ZFS_DELEG_NAMED_SET;
4947 ld[0] = ZFS_DELEG_NA;
4948 break;
4949 case ZFS_DELEG_CREATE_SETS:
4950 case ZFS_DELEG_CREATE:
4951 set_type = ZFS_DELEG_CREATE_SETS;
4952 base_type = ZFS_DELEG_CREATE;
4953 ld[0] = ZFS_DELEG_NA;
4954 break;
4955 case ZFS_DELEG_USER_SETS:
4956 case ZFS_DELEG_USER:
4957 set_type = ZFS_DELEG_USER_SETS;
4958 base_type = ZFS_DELEG_USER;
4959 if (local)
4960 ld[0] = ZFS_DELEG_LOCAL;
4961 if (descend)
4962 ld[1] = ZFS_DELEG_DESCENDENT;
4963 break;
4964 case ZFS_DELEG_GROUP_SETS:
4965 case ZFS_DELEG_GROUP:
4966 set_type = ZFS_DELEG_GROUP_SETS;
4967 base_type = ZFS_DELEG_GROUP;
4968 if (local)
4969 ld[0] = ZFS_DELEG_LOCAL;
4970 if (descend)
4971 ld[1] = ZFS_DELEG_DESCENDENT;
4972 break;
4973 case ZFS_DELEG_EVERYONE_SETS:
4974 case ZFS_DELEG_EVERYONE:
4975 set_type = ZFS_DELEG_EVERYONE_SETS;
4976 base_type = ZFS_DELEG_EVERYONE;
4977 if (local)
4978 ld[0] = ZFS_DELEG_LOCAL;
4979 if (descend)
4980 ld[1] = ZFS_DELEG_DESCENDENT;
4981 break;
4983 default:
4984 assert(set_type != '\0' && base_type != '\0');
4987 if (perms != NULL) {
4988 char *curr = perms;
4989 char *end = curr + strlen(perms);
4991 while (curr < end) {
4992 char *delim = strchr(curr, ',');
4993 if (delim == NULL)
4994 delim = end;
4995 else
4996 *delim = '\0';
4998 if (curr[0] == '@')
4999 nvl = set_nvl;
5000 else
5001 nvl = base_nvl;
5003 (void) nvlist_add_boolean(nvl, curr);
5004 if (delim != end)
5005 *delim = ',';
5006 curr = delim + 1;
5009 for (i = 0; i < 2; i++) {
5010 char locality = ld[i];
5011 if (locality == 0)
5012 continue;
5014 if (!nvlist_empty(base_nvl)) {
5015 if (who != NULL)
5016 (void) snprintf(who_buf,
5017 sizeof (who_buf), "%c%c$%s",
5018 base_type, locality, who);
5019 else
5020 (void) snprintf(who_buf,
5021 sizeof (who_buf), "%c%c$",
5022 base_type, locality);
5024 (void) nvlist_add_nvlist(top_nvl, who_buf,
5025 base_nvl);
5029 if (!nvlist_empty(set_nvl)) {
5030 if (who != NULL)
5031 (void) snprintf(who_buf,
5032 sizeof (who_buf), "%c%c$%s",
5033 set_type, locality, who);
5034 else
5035 (void) snprintf(who_buf,
5036 sizeof (who_buf), "%c%c$",
5037 set_type, locality);
5039 (void) nvlist_add_nvlist(top_nvl, who_buf,
5040 set_nvl);
5043 } else {
5044 for (i = 0; i < 2; i++) {
5045 char locality = ld[i];
5046 if (locality == 0)
5047 continue;
5049 if (who != NULL)
5050 (void) snprintf(who_buf, sizeof (who_buf),
5051 "%c%c$%s", base_type, locality, who);
5052 else
5053 (void) snprintf(who_buf, sizeof (who_buf),
5054 "%c%c$", base_type, locality);
5055 (void) nvlist_add_boolean(top_nvl, who_buf);
5057 if (who != NULL)
5058 (void) snprintf(who_buf, sizeof (who_buf),
5059 "%c%c$%s", set_type, locality, who);
5060 else
5061 (void) snprintf(who_buf, sizeof (who_buf),
5062 "%c%c$", set_type, locality);
5063 (void) nvlist_add_boolean(top_nvl, who_buf);
5068 static int
5069 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5071 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5072 nomem();
5074 if (opts->set) {
5075 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5076 opts->descend, opts->who, opts->perms, *nvlp);
5077 } else if (opts->create) {
5078 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5079 opts->descend, NULL, opts->perms, *nvlp);
5080 } else if (opts->everyone) {
5081 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5082 opts->descend, NULL, opts->perms, *nvlp);
5083 } else {
5084 char *curr = opts->who;
5085 char *end = curr + strlen(curr);
5087 while (curr < end) {
5088 const char *who;
5089 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5090 char *endch;
5091 char *delim = strchr(curr, ',');
5092 char errbuf[256];
5093 char id[64];
5094 struct passwd *p = NULL;
5095 struct group *g = NULL;
5097 uid_t rid;
5098 if (delim == NULL)
5099 delim = end;
5100 else
5101 *delim = '\0';
5103 rid = (uid_t)strtol(curr, &endch, 0);
5104 if (opts->user) {
5105 who_type = ZFS_DELEG_USER;
5106 if (*endch != '\0')
5107 p = getpwnam(curr);
5108 else
5109 p = getpwuid(rid);
5111 if (p != NULL)
5112 rid = p->pw_uid;
5113 else {
5114 (void) snprintf(errbuf, 256, gettext(
5115 "invalid user %s"), curr);
5116 allow_usage(un, B_TRUE, errbuf);
5118 } else if (opts->group) {
5119 who_type = ZFS_DELEG_GROUP;
5120 if (*endch != '\0')
5121 g = getgrnam(curr);
5122 else
5123 g = getgrgid(rid);
5125 if (g != NULL)
5126 rid = g->gr_gid;
5127 else {
5128 (void) snprintf(errbuf, 256, gettext(
5129 "invalid group %s"), curr);
5130 allow_usage(un, B_TRUE, errbuf);
5132 } else {
5133 if (*endch != '\0') {
5134 p = getpwnam(curr);
5135 } else {
5136 p = getpwuid(rid);
5139 if (p == NULL) {
5140 if (*endch != '\0') {
5141 g = getgrnam(curr);
5142 } else {
5143 g = getgrgid(rid);
5147 if (p != NULL) {
5148 who_type = ZFS_DELEG_USER;
5149 rid = p->pw_uid;
5150 } else if (g != NULL) {
5151 who_type = ZFS_DELEG_GROUP;
5152 rid = g->gr_gid;
5153 } else {
5154 (void) snprintf(errbuf, 256, gettext(
5155 "invalid user/group %s"), curr);
5156 allow_usage(un, B_TRUE, errbuf);
5160 (void) sprintf(id, "%u", rid);
5161 who = id;
5163 store_allow_perm(who_type, opts->local,
5164 opts->descend, who, opts->perms, *nvlp);
5165 curr = delim + 1;
5169 return (0);
5172 static void
5173 print_set_creat_perms(uu_avl_t *who_avl)
5175 const char *sc_title[] = {
5176 gettext("Permission sets:\n"),
5177 gettext("Create time permissions:\n"),
5178 NULL
5180 const char **title_ptr = sc_title;
5181 who_perm_node_t *who_node = NULL;
5182 int prev_weight = -1;
5184 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5185 who_node = uu_avl_next(who_avl, who_node)) {
5186 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5187 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5188 const char *who_name = who_node->who_perm.who_name;
5189 int weight = who_type2weight(who_type);
5190 boolean_t first = B_TRUE;
5191 deleg_perm_node_t *deleg_node;
5193 if (prev_weight != weight) {
5194 (void) printf(*title_ptr++);
5195 prev_weight = weight;
5198 if (who_name == NULL || strnlen(who_name, 1) == 0)
5199 (void) printf("\t");
5200 else
5201 (void) printf("\t%s ", who_name);
5203 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5204 deleg_node = uu_avl_next(avl, deleg_node)) {
5205 if (first) {
5206 (void) printf("%s",
5207 deleg_node->dpn_perm.dp_name);
5208 first = B_FALSE;
5209 } else
5210 (void) printf(",%s",
5211 deleg_node->dpn_perm.dp_name);
5214 (void) printf("\n");
5218 static void
5219 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5220 const char *title)
5222 who_perm_node_t *who_node = NULL;
5223 boolean_t prt_title = B_TRUE;
5224 uu_avl_walk_t *walk;
5226 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5227 nomem();
5229 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5230 const char *who_name = who_node->who_perm.who_name;
5231 const char *nice_who_name = who_node->who_perm.who_ug_name;
5232 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5233 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5234 char delim = ' ';
5235 deleg_perm_node_t *deleg_node;
5236 boolean_t prt_who = B_TRUE;
5238 for (deleg_node = uu_avl_first(avl);
5239 deleg_node != NULL;
5240 deleg_node = uu_avl_next(avl, deleg_node)) {
5241 if (local != deleg_node->dpn_perm.dp_local ||
5242 descend != deleg_node->dpn_perm.dp_descend)
5243 continue;
5245 if (prt_who) {
5246 const char *who = NULL;
5247 if (prt_title) {
5248 prt_title = B_FALSE;
5249 (void) printf(title);
5252 switch (who_type) {
5253 case ZFS_DELEG_USER_SETS:
5254 case ZFS_DELEG_USER:
5255 who = gettext("user");
5256 if (nice_who_name)
5257 who_name = nice_who_name;
5258 break;
5259 case ZFS_DELEG_GROUP_SETS:
5260 case ZFS_DELEG_GROUP:
5261 who = gettext("group");
5262 if (nice_who_name)
5263 who_name = nice_who_name;
5264 break;
5265 case ZFS_DELEG_EVERYONE_SETS:
5266 case ZFS_DELEG_EVERYONE:
5267 who = gettext("everyone");
5268 who_name = NULL;
5269 break;
5271 default:
5272 assert(who != NULL);
5275 prt_who = B_FALSE;
5276 if (who_name == NULL)
5277 (void) printf("\t%s", who);
5278 else
5279 (void) printf("\t%s %s", who, who_name);
5282 (void) printf("%c%s", delim,
5283 deleg_node->dpn_perm.dp_name);
5284 delim = ',';
5287 if (!prt_who)
5288 (void) printf("\n");
5291 uu_avl_walk_end(walk);
5294 static void
5295 print_fs_perms(fs_perm_set_t *fspset)
5297 fs_perm_node_t *node = NULL;
5298 char buf[MAXNAMELEN + 32];
5299 const char *dsname = buf;
5301 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5302 node = uu_list_next(fspset->fsps_list, node)) {
5303 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5304 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5305 int left = 0;
5307 (void) snprintf(buf, sizeof (buf),
5308 gettext("---- Permissions on %s "),
5309 node->fspn_fsperm.fsp_name);
5310 (void) printf(dsname);
5311 left = 70 - strlen(buf);
5312 while (left-- > 0)
5313 (void) printf("-");
5314 (void) printf("\n");
5316 print_set_creat_perms(sc_avl);
5317 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5318 gettext("Local permissions:\n"));
5319 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5320 gettext("Descendent permissions:\n"));
5321 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5322 gettext("Local+Descendent permissions:\n"));
5326 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5328 struct deleg_perms {
5329 boolean_t un;
5330 nvlist_t *nvl;
5333 static int
5334 set_deleg_perms(zfs_handle_t *zhp, void *data)
5336 struct deleg_perms *perms = (struct deleg_perms *)data;
5337 zfs_type_t zfs_type = zfs_get_type(zhp);
5339 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5340 return (0);
5342 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5345 static int
5346 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5348 zfs_handle_t *zhp;
5349 nvlist_t *perm_nvl = NULL;
5350 nvlist_t *update_perm_nvl = NULL;
5351 int error = 1;
5352 int c;
5353 struct allow_opts opts = { 0 };
5355 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5357 /* check opts */
5358 while ((c = getopt(argc, argv, optstr)) != -1) {
5359 switch (c) {
5360 case 'l':
5361 opts.local = B_TRUE;
5362 break;
5363 case 'd':
5364 opts.descend = B_TRUE;
5365 break;
5366 case 'u':
5367 opts.user = B_TRUE;
5368 break;
5369 case 'g':
5370 opts.group = B_TRUE;
5371 break;
5372 case 'e':
5373 opts.everyone = B_TRUE;
5374 break;
5375 case 's':
5376 opts.set = B_TRUE;
5377 break;
5378 case 'c':
5379 opts.create = B_TRUE;
5380 break;
5381 case 'r':
5382 opts.recursive = B_TRUE;
5383 break;
5384 case ':':
5385 (void) fprintf(stderr, gettext("missing argument for "
5386 "'%c' option\n"), optopt);
5387 usage(B_FALSE);
5388 break;
5389 case 'h':
5390 opts.prt_usage = B_TRUE;
5391 break;
5392 case '?':
5393 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5394 optopt);
5395 usage(B_FALSE);
5399 argc -= optind;
5400 argv += optind;
5402 /* check arguments */
5403 parse_allow_args(argc, argv, un, &opts);
5405 /* try to open the dataset */
5406 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5407 ZFS_TYPE_VOLUME)) == NULL) {
5408 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5409 opts.dataset);
5410 return (-1);
5413 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5414 goto cleanup2;
5416 fs_perm_set_init(&fs_perm_set);
5417 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5418 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5419 goto cleanup1;
5422 if (opts.prt_perms)
5423 print_fs_perms(&fs_perm_set);
5424 else {
5425 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5426 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5427 goto cleanup0;
5429 if (un && opts.recursive) {
5430 struct deleg_perms data = { un, update_perm_nvl };
5431 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5432 &data) != 0)
5433 goto cleanup0;
5437 error = 0;
5439 cleanup0:
5440 nvlist_free(perm_nvl);
5441 nvlist_free(update_perm_nvl);
5442 cleanup1:
5443 fs_perm_set_fini(&fs_perm_set);
5444 cleanup2:
5445 zfs_close(zhp);
5447 return (error);
5450 static int
5451 zfs_do_allow(int argc, char **argv)
5453 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5456 static int
5457 zfs_do_unallow(int argc, char **argv)
5459 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5462 static int
5463 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5465 int errors = 0;
5466 int i;
5467 const char *tag;
5468 boolean_t recursive = B_FALSE;
5469 const char *opts = holding ? "rt" : "r";
5470 int c;
5472 /* check options */
5473 while ((c = getopt(argc, argv, opts)) != -1) {
5474 switch (c) {
5475 case 'r':
5476 recursive = B_TRUE;
5477 break;
5478 case '?':
5479 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5480 optopt);
5481 usage(B_FALSE);
5485 argc -= optind;
5486 argv += optind;
5488 /* check number of arguments */
5489 if (argc < 2)
5490 usage(B_FALSE);
5492 tag = argv[0];
5493 --argc;
5494 ++argv;
5496 if (holding && tag[0] == '.') {
5497 /* tags starting with '.' are reserved for libzfs */
5498 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5499 usage(B_FALSE);
5502 for (i = 0; i < argc; ++i) {
5503 zfs_handle_t *zhp;
5504 char parent[ZFS_MAX_DATASET_NAME_LEN];
5505 const char *delim;
5506 char *path = argv[i];
5508 delim = strchr(path, '@');
5509 if (delim == NULL) {
5510 (void) fprintf(stderr,
5511 gettext("'%s' is not a snapshot\n"), path);
5512 ++errors;
5513 continue;
5515 (void) strncpy(parent, path, delim - path);
5516 parent[delim - path] = '\0';
5518 zhp = zfs_open(g_zfs, parent,
5519 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5520 if (zhp == NULL) {
5521 ++errors;
5522 continue;
5524 if (holding) {
5525 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5526 ++errors;
5527 } else {
5528 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5529 ++errors;
5531 zfs_close(zhp);
5534 return (errors != 0);
5538 * zfs hold [-r] [-t] <tag> <snap> ...
5540 * -r Recursively hold
5542 * Apply a user-hold with the given tag to the list of snapshots.
5544 static int
5545 zfs_do_hold(int argc, char **argv)
5547 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5551 * zfs release [-r] <tag> <snap> ...
5553 * -r Recursively release
5555 * Release a user-hold with the given tag from the list of snapshots.
5557 static int
5558 zfs_do_release(int argc, char **argv)
5560 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5563 typedef struct holds_cbdata {
5564 boolean_t cb_recursive;
5565 const char *cb_snapname;
5566 nvlist_t **cb_nvlp;
5567 size_t cb_max_namelen;
5568 size_t cb_max_taglen;
5569 } holds_cbdata_t;
5571 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5572 #define DATETIME_BUF_LEN (32)
5576 static void
5577 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5579 int i;
5580 nvpair_t *nvp = NULL;
5581 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5582 const char *col;
5584 if (!scripted) {
5585 for (i = 0; i < 3; i++) {
5586 col = gettext(hdr_cols[i]);
5587 if (i < 2)
5588 (void) printf("%-*s ", i ? tagwidth : nwidth,
5589 col);
5590 else
5591 (void) printf("%s\n", col);
5595 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5596 char *zname = nvpair_name(nvp);
5597 nvlist_t *nvl2;
5598 nvpair_t *nvp2 = NULL;
5599 (void) nvpair_value_nvlist(nvp, &nvl2);
5600 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5601 char tsbuf[DATETIME_BUF_LEN];
5602 char *tagname = nvpair_name(nvp2);
5603 uint64_t val = 0;
5604 time_t time;
5605 struct tm t;
5607 (void) nvpair_value_uint64(nvp2, &val);
5608 time = (time_t)val;
5609 (void) localtime_r(&time, &t);
5610 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5611 gettext(STRFTIME_FMT_STR), &t);
5613 if (scripted) {
5614 (void) printf("%s\t%s\t%s\n", zname,
5615 tagname, tsbuf);
5616 } else {
5617 (void) printf("%-*s %-*s %s\n", nwidth,
5618 zname, tagwidth, tagname, tsbuf);
5625 * Generic callback function to list a dataset or snapshot.
5627 static int
5628 holds_callback(zfs_handle_t *zhp, void *data)
5630 holds_cbdata_t *cbp = data;
5631 nvlist_t *top_nvl = *cbp->cb_nvlp;
5632 nvlist_t *nvl = NULL;
5633 nvpair_t *nvp = NULL;
5634 const char *zname = zfs_get_name(zhp);
5635 size_t znamelen = strlen(zname);
5637 if (cbp->cb_recursive) {
5638 const char *snapname;
5639 char *delim = strchr(zname, '@');
5640 if (delim == NULL)
5641 return (0);
5643 snapname = delim + 1;
5644 if (strcmp(cbp->cb_snapname, snapname))
5645 return (0);
5648 if (zfs_get_holds(zhp, &nvl) != 0)
5649 return (-1);
5651 if (znamelen > cbp->cb_max_namelen)
5652 cbp->cb_max_namelen = znamelen;
5654 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5655 const char *tag = nvpair_name(nvp);
5656 size_t taglen = strlen(tag);
5657 if (taglen > cbp->cb_max_taglen)
5658 cbp->cb_max_taglen = taglen;
5661 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5665 * zfs holds [-r] <snap> ...
5667 * -r Recursively hold
5669 static int
5670 zfs_do_holds(int argc, char **argv)
5672 int errors = 0;
5673 int c;
5674 int i;
5675 boolean_t scripted = B_FALSE;
5676 boolean_t recursive = B_FALSE;
5677 const char *opts = "rH";
5678 nvlist_t *nvl;
5680 int types = ZFS_TYPE_SNAPSHOT;
5681 holds_cbdata_t cb = { 0 };
5683 int limit = 0;
5684 int ret = 0;
5685 int flags = 0;
5687 /* check options */
5688 while ((c = getopt(argc, argv, opts)) != -1) {
5689 switch (c) {
5690 case 'r':
5691 recursive = B_TRUE;
5692 break;
5693 case 'H':
5694 scripted = B_TRUE;
5695 break;
5696 case '?':
5697 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5698 optopt);
5699 usage(B_FALSE);
5703 if (recursive) {
5704 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5705 flags |= ZFS_ITER_RECURSE;
5708 argc -= optind;
5709 argv += optind;
5711 /* check number of arguments */
5712 if (argc < 1)
5713 usage(B_FALSE);
5715 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5716 nomem();
5718 for (i = 0; i < argc; ++i) {
5719 char *snapshot = argv[i];
5720 const char *delim;
5721 const char *snapname;
5723 delim = strchr(snapshot, '@');
5724 if (delim == NULL) {
5725 (void) fprintf(stderr,
5726 gettext("'%s' is not a snapshot\n"), snapshot);
5727 ++errors;
5728 continue;
5730 snapname = delim + 1;
5731 if (recursive)
5732 snapshot[delim - snapshot] = '\0';
5734 cb.cb_recursive = recursive;
5735 cb.cb_snapname = snapname;
5736 cb.cb_nvlp = &nvl;
5739 * 1. collect holds data, set format options
5741 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5742 holds_callback, &cb);
5743 if (ret != 0)
5744 ++errors;
5748 * 2. print holds data
5750 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5752 if (nvlist_empty(nvl))
5753 (void) printf(gettext("no datasets available\n"));
5755 nvlist_free(nvl);
5757 return (0 != errors);
5760 #define CHECK_SPINNER 30
5761 #define SPINNER_TIME 3 /* seconds */
5762 #define MOUNT_TIME 5 /* seconds */
5764 static int
5765 get_one_dataset(zfs_handle_t *zhp, void *data)
5767 static char *spin[] = { "-", "\\", "|", "/" };
5768 static int spinval = 0;
5769 static int spincheck = 0;
5770 static time_t last_spin_time = (time_t)0;
5771 get_all_cb_t *cbp = data;
5772 zfs_type_t type = zfs_get_type(zhp);
5774 if (cbp->cb_verbose) {
5775 if (--spincheck < 0) {
5776 time_t now = time(NULL);
5777 if (last_spin_time + SPINNER_TIME < now) {
5778 update_progress(spin[spinval++ % 4]);
5779 last_spin_time = now;
5781 spincheck = CHECK_SPINNER;
5786 * Interate over any nested datasets.
5788 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5789 zfs_close(zhp);
5790 return (1);
5794 * Skip any datasets whose type does not match.
5796 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5797 zfs_close(zhp);
5798 return (0);
5800 libzfs_add_handle(cbp, zhp);
5801 assert(cbp->cb_used <= cbp->cb_alloc);
5803 return (0);
5806 static void
5807 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5809 get_all_cb_t cb = { 0 };
5810 cb.cb_verbose = verbose;
5811 cb.cb_getone = get_one_dataset;
5813 if (verbose)
5814 set_progress_header(gettext("Reading ZFS config"));
5815 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5817 *dslist = cb.cb_handles;
5818 *count = cb.cb_used;
5820 if (verbose)
5821 finish_progress(gettext("done."));
5825 * Generic callback for sharing or mounting filesystems. Because the code is so
5826 * similar, we have a common function with an extra parameter to determine which
5827 * mode we are using.
5829 #define OP_SHARE 0x1
5830 #define OP_MOUNT 0x2
5833 * Share or mount a dataset.
5835 static int
5836 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5837 boolean_t explicit, const char *options)
5839 char mountpoint[ZFS_MAXPROPLEN];
5840 char shareopts[ZFS_MAXPROPLEN];
5841 char smbshareopts[ZFS_MAXPROPLEN];
5842 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5843 struct mnttab mnt;
5844 uint64_t zoned, canmount;
5845 boolean_t shared_nfs, shared_smb;
5847 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5850 * Check to make sure we can mount/share this dataset. If we
5851 * are in the global zone and the filesystem is exported to a
5852 * local zone, or if we are in a local zone and the
5853 * filesystem is not exported, then it is an error.
5855 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5857 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5858 if (!explicit)
5859 return (0);
5861 (void) fprintf(stderr, gettext("cannot %s '%s': "
5862 "dataset is exported to a local zone\n"), cmdname,
5863 zfs_get_name(zhp));
5864 return (1);
5866 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5867 if (!explicit)
5868 return (0);
5870 (void) fprintf(stderr, gettext("cannot %s '%s': "
5871 "permission denied\n"), cmdname,
5872 zfs_get_name(zhp));
5873 return (1);
5877 * Ignore any filesystems which don't apply to us. This
5878 * includes those with a legacy mountpoint, or those with
5879 * legacy share options.
5881 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5882 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5883 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5884 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5885 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5886 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5888 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5889 strcmp(smbshareopts, "off") == 0) {
5890 if (!explicit)
5891 return (0);
5893 (void) fprintf(stderr, gettext("cannot share '%s': "
5894 "legacy share\n"), zfs_get_name(zhp));
5895 (void) fprintf(stderr, gettext("use share(1M) to "
5896 "share this filesystem, or set "
5897 "sharenfs property on\n"));
5898 return (1);
5902 * We cannot share or mount legacy filesystems. If the
5903 * shareopts is non-legacy but the mountpoint is legacy, we
5904 * treat it as a legacy share.
5906 if (strcmp(mountpoint, "legacy") == 0) {
5907 if (!explicit)
5908 return (0);
5910 (void) fprintf(stderr, gettext("cannot %s '%s': "
5911 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5912 (void) fprintf(stderr, gettext("use %s(1M) to "
5913 "%s this filesystem\n"), cmdname, cmdname);
5914 return (1);
5917 if (strcmp(mountpoint, "none") == 0) {
5918 if (!explicit)
5919 return (0);
5921 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5922 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5923 return (1);
5927 * canmount explicit outcome
5928 * on no pass through
5929 * on yes pass through
5930 * off no return 0
5931 * off yes display error, return 1
5932 * noauto no return 0
5933 * noauto yes pass through
5935 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5936 if (canmount == ZFS_CANMOUNT_OFF) {
5937 if (!explicit)
5938 return (0);
5940 (void) fprintf(stderr, gettext("cannot %s '%s': "
5941 "'canmount' property is set to 'off'\n"), cmdname,
5942 zfs_get_name(zhp));
5943 return (1);
5944 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5945 return (0);
5949 * If this filesystem is inconsistent and has a receive resume
5950 * token, we can not mount it.
5952 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5953 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5954 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5955 if (!explicit)
5956 return (0);
5958 (void) fprintf(stderr, gettext("cannot %s '%s': "
5959 "Contains partially-completed state from "
5960 "\"zfs receive -r\", which can be resumed with "
5961 "\"zfs send -t\"\n"),
5962 cmdname, zfs_get_name(zhp));
5963 return (1);
5967 * At this point, we have verified that the mountpoint and/or
5968 * shareopts are appropriate for auto management. If the
5969 * filesystem is already mounted or shared, return (failing
5970 * for explicit requests); otherwise mount or share the
5971 * filesystem.
5973 switch (op) {
5974 case OP_SHARE:
5976 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5977 shared_smb = zfs_is_shared_smb(zhp, NULL);
5979 if ((shared_nfs && shared_smb) ||
5980 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5981 strcmp(smbshareopts, "off") == 0) ||
5982 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5983 strcmp(shareopts, "off") == 0)) {
5984 if (!explicit)
5985 return (0);
5987 (void) fprintf(stderr, gettext("cannot share "
5988 "'%s': filesystem already shared\n"),
5989 zfs_get_name(zhp));
5990 return (1);
5993 if (!zfs_is_mounted(zhp, NULL) &&
5994 zfs_mount(zhp, NULL, 0) != 0)
5995 return (1);
5997 if (protocol == NULL) {
5998 if (zfs_shareall(zhp) != 0)
5999 return (1);
6000 } else if (strcmp(protocol, "nfs") == 0) {
6001 if (zfs_share_nfs(zhp))
6002 return (1);
6003 } else if (strcmp(protocol, "smb") == 0) {
6004 if (zfs_share_smb(zhp))
6005 return (1);
6006 } else {
6007 (void) fprintf(stderr, gettext("cannot share "
6008 "'%s': invalid share type '%s' "
6009 "specified\n"),
6010 zfs_get_name(zhp), protocol);
6011 return (1);
6014 break;
6016 case OP_MOUNT:
6017 if (options == NULL)
6018 mnt.mnt_mntopts = "";
6019 else
6020 mnt.mnt_mntopts = (char *)options;
6022 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6023 zfs_is_mounted(zhp, NULL)) {
6024 if (!explicit)
6025 return (0);
6027 (void) fprintf(stderr, gettext("cannot mount "
6028 "'%s': filesystem already mounted\n"),
6029 zfs_get_name(zhp));
6030 return (1);
6033 if (zfs_mount(zhp, options, flags) != 0)
6034 return (1);
6035 break;
6038 return (0);
6042 * Reports progress in the form "(current/total)". Not thread-safe.
6044 static void
6045 report_mount_progress(int current, int total)
6047 static time_t last_progress_time = 0;
6048 time_t now = time(NULL);
6049 char info[32];
6051 /* report 1..n instead of 0..n-1 */
6052 ++current;
6054 /* display header if we're here for the first time */
6055 if (current == 1) {
6056 set_progress_header(gettext("Mounting ZFS filesystems"));
6057 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6058 /* too soon to report again */
6059 return;
6062 last_progress_time = now;
6064 (void) sprintf(info, "(%d/%d)", current, total);
6066 if (current == total)
6067 finish_progress(info);
6068 else
6069 update_progress(info);
6072 static void
6073 append_options(char *mntopts, char *newopts)
6075 int len = strlen(mntopts);
6077 /* original length plus new string to append plus 1 for the comma */
6078 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6079 (void) fprintf(stderr, gettext("the opts argument for "
6080 "'%c' option is too long (more than %d chars)\n"),
6081 "-o", MNT_LINE_MAX);
6082 usage(B_FALSE);
6085 if (*mntopts)
6086 mntopts[len++] = ',';
6088 (void) strcpy(&mntopts[len], newopts);
6091 static int
6092 share_mount(int op, int argc, char **argv)
6094 int do_all = 0;
6095 boolean_t verbose = B_FALSE;
6096 int c, ret = 0;
6097 char *options = NULL;
6098 int flags = 0;
6100 /* check options */
6101 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6102 != -1) {
6103 switch (c) {
6104 case 'a':
6105 do_all = 1;
6106 break;
6107 case 'v':
6108 verbose = B_TRUE;
6109 break;
6110 case 'o':
6111 if (*optarg == '\0') {
6112 (void) fprintf(stderr, gettext("empty mount "
6113 "options (-o) specified\n"));
6114 usage(B_FALSE);
6117 if (options == NULL)
6118 options = safe_malloc(MNT_LINE_MAX + 1);
6120 /* option validation is done later */
6121 append_options(options, optarg);
6122 break;
6124 case 'O':
6125 flags |= MS_OVERLAY;
6126 break;
6127 case ':':
6128 (void) fprintf(stderr, gettext("missing argument for "
6129 "'%c' option\n"), optopt);
6130 usage(B_FALSE);
6131 break;
6132 case '?':
6133 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6134 optopt);
6135 usage(B_FALSE);
6139 argc -= optind;
6140 argv += optind;
6142 /* check number of arguments */
6143 if (do_all) {
6144 zfs_handle_t **dslist = NULL;
6145 size_t i, count = 0;
6146 char *protocol = NULL;
6148 if (op == OP_SHARE && argc > 0) {
6149 if (strcmp(argv[0], "nfs") != 0 &&
6150 strcmp(argv[0], "smb") != 0) {
6151 (void) fprintf(stderr, gettext("share type "
6152 "must be 'nfs' or 'smb'\n"));
6153 usage(B_FALSE);
6155 protocol = argv[0];
6156 argc--;
6157 argv++;
6160 if (argc != 0) {
6161 (void) fprintf(stderr, gettext("too many arguments\n"));
6162 usage(B_FALSE);
6165 start_progress_timer();
6166 get_all_datasets(&dslist, &count, verbose);
6168 if (count == 0)
6169 return (0);
6171 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6172 sa_init_selective_arg_t sharearg;
6173 sharearg.zhandle_arr = dslist;
6174 sharearg.zhandle_len = count;
6175 if ((ret = zfs_init_libshare_arg(zfs_get_handle(dslist[0]),
6176 SA_INIT_SHARE_API_SELECTIVE, &sharearg)) != SA_OK) {
6177 (void) fprintf(stderr,
6178 gettext("Could not initialize libshare, %d"), ret);
6179 return (ret);
6182 for (i = 0; i < count; i++) {
6183 if (verbose)
6184 report_mount_progress(i, count);
6186 if (share_mount_one(dslist[i], op, flags, protocol,
6187 B_FALSE, options) != 0)
6188 ret = 1;
6189 zfs_close(dslist[i]);
6192 free(dslist);
6193 } else if (argc == 0) {
6194 struct mnttab entry;
6196 if ((op == OP_SHARE) || (options != NULL)) {
6197 (void) fprintf(stderr, gettext("missing filesystem "
6198 "argument (specify -a for all)\n"));
6199 usage(B_FALSE);
6203 * When mount is given no arguments, go through /etc/mnttab and
6204 * display any active ZFS mounts. We hide any snapshots, since
6205 * they are controlled automatically.
6207 rewind(mnttab_file);
6208 while (getmntent(mnttab_file, &entry) == 0) {
6209 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6210 strchr(entry.mnt_special, '@') != NULL)
6211 continue;
6213 (void) printf("%-30s %s\n", entry.mnt_special,
6214 entry.mnt_mountp);
6217 } else {
6218 zfs_handle_t *zhp;
6220 if (argc > 1) {
6221 (void) fprintf(stderr,
6222 gettext("too many arguments\n"));
6223 usage(B_FALSE);
6226 if ((zhp = zfs_open(g_zfs, argv[0],
6227 ZFS_TYPE_FILESYSTEM)) == NULL) {
6228 ret = 1;
6229 } else {
6230 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6231 options);
6232 zfs_close(zhp);
6236 return (ret);
6240 * zfs mount -a [nfs]
6241 * zfs mount filesystem
6243 * Mount all filesystems, or mount the given filesystem.
6245 static int
6246 zfs_do_mount(int argc, char **argv)
6248 return (share_mount(OP_MOUNT, argc, argv));
6252 * zfs share -a [nfs | smb]
6253 * zfs share filesystem
6255 * Share all filesystems, or share the given filesystem.
6257 static int
6258 zfs_do_share(int argc, char **argv)
6260 return (share_mount(OP_SHARE, argc, argv));
6263 typedef struct unshare_unmount_node {
6264 zfs_handle_t *un_zhp;
6265 char *un_mountp;
6266 uu_avl_node_t un_avlnode;
6267 } unshare_unmount_node_t;
6269 /* ARGSUSED */
6270 static int
6271 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6273 const unshare_unmount_node_t *l = larg;
6274 const unshare_unmount_node_t *r = rarg;
6276 return (strcmp(l->un_mountp, r->un_mountp));
6280 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6281 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6282 * and unmount it appropriately.
6284 static int
6285 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6287 zfs_handle_t *zhp;
6288 int ret = 0;
6289 struct stat64 statbuf;
6290 struct extmnttab entry;
6291 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6292 ino_t path_inode;
6295 * Search for the path in /etc/mnttab. Rather than looking for the
6296 * specific path, which can be fooled by non-standard paths (i.e. ".."
6297 * or "//"), we stat() the path and search for the corresponding
6298 * (major,minor) device pair.
6300 if (stat64(path, &statbuf) != 0) {
6301 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6302 cmdname, path, strerror(errno));
6303 return (1);
6305 path_inode = statbuf.st_ino;
6308 * Search for the given (major,minor) pair in the mount table.
6310 rewind(mnttab_file);
6311 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6312 if (entry.mnt_major == major(statbuf.st_dev) &&
6313 entry.mnt_minor == minor(statbuf.st_dev))
6314 break;
6316 if (ret != 0) {
6317 if (op == OP_SHARE) {
6318 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6319 "currently mounted\n"), cmdname, path);
6320 return (1);
6322 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6323 path);
6324 if ((ret = umount2(path, flags)) != 0)
6325 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6326 strerror(errno));
6327 return (ret != 0);
6330 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6331 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6332 "filesystem\n"), cmdname, path);
6333 return (1);
6336 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6337 ZFS_TYPE_FILESYSTEM)) == NULL)
6338 return (1);
6340 ret = 1;
6341 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6342 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6343 cmdname, path, strerror(errno));
6344 goto out;
6345 } else if (statbuf.st_ino != path_inode) {
6346 (void) fprintf(stderr, gettext("cannot "
6347 "%s '%s': not a mountpoint\n"), cmdname, path);
6348 goto out;
6351 if (op == OP_SHARE) {
6352 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6353 char smbshare_prop[ZFS_MAXPROPLEN];
6355 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6356 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6357 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6358 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6360 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6361 strcmp(smbshare_prop, "off") == 0) {
6362 (void) fprintf(stderr, gettext("cannot unshare "
6363 "'%s': legacy share\n"), path);
6364 (void) fprintf(stderr, gettext("use "
6365 "unshare(1M) to unshare this filesystem\n"));
6366 } else if (!zfs_is_shared(zhp)) {
6367 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6368 "not currently shared\n"), path);
6369 } else {
6370 ret = zfs_unshareall_bypath(zhp, path);
6372 } else {
6373 char mtpt_prop[ZFS_MAXPROPLEN];
6375 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6376 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6378 if (is_manual) {
6379 ret = zfs_unmount(zhp, NULL, flags);
6380 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6381 (void) fprintf(stderr, gettext("cannot unmount "
6382 "'%s': legacy mountpoint\n"),
6383 zfs_get_name(zhp));
6384 (void) fprintf(stderr, gettext("use umount(1M) "
6385 "to unmount this filesystem\n"));
6386 } else {
6387 ret = zfs_unmountall(zhp, flags);
6391 out:
6392 zfs_close(zhp);
6394 return (ret != 0);
6398 * Generic callback for unsharing or unmounting a filesystem.
6400 static int
6401 unshare_unmount(int op, int argc, char **argv)
6403 int do_all = 0;
6404 int flags = 0;
6405 int ret = 0;
6406 int c;
6407 zfs_handle_t *zhp;
6408 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6409 char sharesmb[ZFS_MAXPROPLEN];
6411 /* check options */
6412 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6413 switch (c) {
6414 case 'a':
6415 do_all = 1;
6416 break;
6417 case 'f':
6418 flags = MS_FORCE;
6419 break;
6420 case '?':
6421 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6422 optopt);
6423 usage(B_FALSE);
6427 argc -= optind;
6428 argv += optind;
6430 if (do_all) {
6432 * We could make use of zfs_for_each() to walk all datasets in
6433 * the system, but this would be very inefficient, especially
6434 * since we would have to linearly search /etc/mnttab for each
6435 * one. Instead, do one pass through /etc/mnttab looking for
6436 * zfs entries and call zfs_unmount() for each one.
6438 * Things get a little tricky if the administrator has created
6439 * mountpoints beneath other ZFS filesystems. In this case, we
6440 * have to unmount the deepest filesystems first. To accomplish
6441 * this, we place all the mountpoints in an AVL tree sorted by
6442 * the special type (dataset name), and walk the result in
6443 * reverse to make sure to get any snapshots first.
6445 struct mnttab entry;
6446 uu_avl_pool_t *pool;
6447 uu_avl_t *tree = NULL;
6448 unshare_unmount_node_t *node;
6449 uu_avl_index_t idx;
6450 uu_avl_walk_t *walk;
6452 if (argc != 0) {
6453 (void) fprintf(stderr, gettext("too many arguments\n"));
6454 usage(B_FALSE);
6457 if (((pool = uu_avl_pool_create("unmount_pool",
6458 sizeof (unshare_unmount_node_t),
6459 offsetof(unshare_unmount_node_t, un_avlnode),
6460 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6461 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6462 nomem();
6464 rewind(mnttab_file);
6465 while (getmntent(mnttab_file, &entry) == 0) {
6467 /* ignore non-ZFS entries */
6468 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6469 continue;
6471 /* ignore snapshots */
6472 if (strchr(entry.mnt_special, '@') != NULL)
6473 continue;
6475 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6476 ZFS_TYPE_FILESYSTEM)) == NULL) {
6477 ret = 1;
6478 continue;
6482 * Ignore datasets that are excluded/restricted by
6483 * parent pool name.
6485 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6486 zfs_close(zhp);
6487 continue;
6490 switch (op) {
6491 case OP_SHARE:
6492 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6493 nfs_mnt_prop,
6494 sizeof (nfs_mnt_prop),
6495 NULL, NULL, 0, B_FALSE) == 0);
6496 if (strcmp(nfs_mnt_prop, "off") != 0)
6497 break;
6498 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6499 nfs_mnt_prop,
6500 sizeof (nfs_mnt_prop),
6501 NULL, NULL, 0, B_FALSE) == 0);
6502 if (strcmp(nfs_mnt_prop, "off") == 0)
6503 continue;
6504 break;
6505 case OP_MOUNT:
6506 /* Ignore legacy mounts */
6507 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6508 nfs_mnt_prop,
6509 sizeof (nfs_mnt_prop),
6510 NULL, NULL, 0, B_FALSE) == 0);
6511 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6512 continue;
6513 /* Ignore canmount=noauto mounts */
6514 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6515 ZFS_CANMOUNT_NOAUTO)
6516 continue;
6517 default:
6518 break;
6521 node = safe_malloc(sizeof (unshare_unmount_node_t));
6522 node->un_zhp = zhp;
6523 node->un_mountp = safe_strdup(entry.mnt_mountp);
6525 uu_avl_node_init(node, &node->un_avlnode, pool);
6527 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6528 uu_avl_insert(tree, node, idx);
6529 } else {
6530 zfs_close(node->un_zhp);
6531 free(node->un_mountp);
6532 free(node);
6537 * Walk the AVL tree in reverse, unmounting each filesystem and
6538 * removing it from the AVL tree in the process.
6540 if ((walk = uu_avl_walk_start(tree,
6541 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6542 nomem();
6544 while ((node = uu_avl_walk_next(walk)) != NULL) {
6545 uu_avl_remove(tree, node);
6547 switch (op) {
6548 case OP_SHARE:
6549 if (zfs_unshareall_bypath(node->un_zhp,
6550 node->un_mountp) != 0)
6551 ret = 1;
6552 break;
6554 case OP_MOUNT:
6555 if (zfs_unmount(node->un_zhp,
6556 node->un_mountp, flags) != 0)
6557 ret = 1;
6558 break;
6561 zfs_close(node->un_zhp);
6562 free(node->un_mountp);
6563 free(node);
6566 uu_avl_walk_end(walk);
6567 uu_avl_destroy(tree);
6568 uu_avl_pool_destroy(pool);
6570 } else {
6571 if (argc != 1) {
6572 if (argc == 0)
6573 (void) fprintf(stderr,
6574 gettext("missing filesystem argument\n"));
6575 else
6576 (void) fprintf(stderr,
6577 gettext("too many arguments\n"));
6578 usage(B_FALSE);
6582 * We have an argument, but it may be a full path or a ZFS
6583 * filesystem. Pass full paths off to unmount_path() (shared by
6584 * manual_unmount), otherwise open the filesystem and pass to
6585 * zfs_unmount().
6587 if (argv[0][0] == '/')
6588 return (unshare_unmount_path(op, argv[0],
6589 flags, B_FALSE));
6591 if ((zhp = zfs_open(g_zfs, argv[0],
6592 ZFS_TYPE_FILESYSTEM)) == NULL)
6593 return (1);
6595 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6596 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6597 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6598 NULL, 0, B_FALSE) == 0);
6600 switch (op) {
6601 case OP_SHARE:
6602 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6603 nfs_mnt_prop,
6604 sizeof (nfs_mnt_prop),
6605 NULL, NULL, 0, B_FALSE) == 0);
6606 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6607 sharesmb, sizeof (sharesmb), NULL, NULL,
6608 0, B_FALSE) == 0);
6610 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6611 strcmp(sharesmb, "off") == 0) {
6612 (void) fprintf(stderr, gettext("cannot "
6613 "unshare '%s': legacy share\n"),
6614 zfs_get_name(zhp));
6615 (void) fprintf(stderr, gettext("use "
6616 "unshare(1M) to unshare this "
6617 "filesystem\n"));
6618 ret = 1;
6619 } else if (!zfs_is_shared(zhp)) {
6620 (void) fprintf(stderr, gettext("cannot "
6621 "unshare '%s': not currently "
6622 "shared\n"), zfs_get_name(zhp));
6623 ret = 1;
6624 } else if (zfs_unshareall(zhp) != 0) {
6625 ret = 1;
6627 break;
6629 case OP_MOUNT:
6630 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6631 (void) fprintf(stderr, gettext("cannot "
6632 "unmount '%s': legacy "
6633 "mountpoint\n"), zfs_get_name(zhp));
6634 (void) fprintf(stderr, gettext("use "
6635 "umount(1M) to unmount this "
6636 "filesystem\n"));
6637 ret = 1;
6638 } else if (!zfs_is_mounted(zhp, NULL)) {
6639 (void) fprintf(stderr, gettext("cannot "
6640 "unmount '%s': not currently "
6641 "mounted\n"),
6642 zfs_get_name(zhp));
6643 ret = 1;
6644 } else if (zfs_unmountall(zhp, flags) != 0) {
6645 ret = 1;
6647 break;
6650 zfs_close(zhp);
6653 return (ret);
6657 * zfs unmount -a
6658 * zfs unmount filesystem
6660 * Unmount all filesystems, or a specific ZFS filesystem.
6662 static int
6663 zfs_do_unmount(int argc, char **argv)
6665 return (unshare_unmount(OP_MOUNT, argc, argv));
6669 * zfs unshare -a
6670 * zfs unshare filesystem
6672 * Unshare all filesystems, or a specific ZFS filesystem.
6674 static int
6675 zfs_do_unshare(int argc, char **argv)
6677 return (unshare_unmount(OP_SHARE, argc, argv));
6681 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6682 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6684 static int
6685 manual_mount(int argc, char **argv)
6687 zfs_handle_t *zhp;
6688 char mountpoint[ZFS_MAXPROPLEN];
6689 char mntopts[MNT_LINE_MAX] = { '\0' };
6690 int ret = 0;
6691 int c;
6692 int flags = 0;
6693 char *dataset, *path;
6695 /* check options */
6696 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6697 switch (c) {
6698 case 'o':
6699 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6700 break;
6701 case 'O':
6702 flags |= MS_OVERLAY;
6703 break;
6704 case 'm':
6705 flags |= MS_NOMNTTAB;
6706 break;
6707 case ':':
6708 (void) fprintf(stderr, gettext("missing argument for "
6709 "'%c' option\n"), optopt);
6710 usage(B_FALSE);
6711 break;
6712 case '?':
6713 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6714 optopt);
6715 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6716 "<path>\n"));
6717 return (2);
6721 argc -= optind;
6722 argv += optind;
6724 /* check that we only have two arguments */
6725 if (argc != 2) {
6726 if (argc == 0)
6727 (void) fprintf(stderr, gettext("missing dataset "
6728 "argument\n"));
6729 else if (argc == 1)
6730 (void) fprintf(stderr,
6731 gettext("missing mountpoint argument\n"));
6732 else
6733 (void) fprintf(stderr, gettext("too many arguments\n"));
6734 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6735 return (2);
6738 dataset = argv[0];
6739 path = argv[1];
6741 /* try to open the dataset */
6742 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6743 return (1);
6745 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6746 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6748 /* check for legacy mountpoint and complain appropriately */
6749 ret = 0;
6750 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6751 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
6752 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6753 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6754 strerror(errno));
6755 ret = 1;
6757 } else {
6758 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6759 "mounted using 'mount -F zfs'\n"), dataset);
6760 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6761 "instead.\n"), path);
6762 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6763 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6764 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6765 "information.\n"));
6766 ret = 1;
6769 return (ret);
6773 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6774 * unmounts of non-legacy filesystems, as this is the dominant administrative
6775 * interface.
6777 static int
6778 manual_unmount(int argc, char **argv)
6780 int flags = 0;
6781 int c;
6783 /* check options */
6784 while ((c = getopt(argc, argv, "f")) != -1) {
6785 switch (c) {
6786 case 'f':
6787 flags = MS_FORCE;
6788 break;
6789 case '?':
6790 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6791 optopt);
6792 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6793 "<path>\n"));
6794 return (2);
6798 argc -= optind;
6799 argv += optind;
6801 /* check arguments */
6802 if (argc != 1) {
6803 if (argc == 0)
6804 (void) fprintf(stderr, gettext("missing path "
6805 "argument\n"));
6806 else
6807 (void) fprintf(stderr, gettext("too many arguments\n"));
6808 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6809 return (2);
6812 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6815 static int
6816 find_command_idx(char *command, int *idx)
6818 int i;
6820 for (i = 0; i < NCOMMAND; i++) {
6821 if (command_table[i].name == NULL)
6822 continue;
6824 if (strcmp(command, command_table[i].name) == 0) {
6825 *idx = i;
6826 return (0);
6829 return (1);
6832 static int
6833 zfs_do_diff(int argc, char **argv)
6835 zfs_handle_t *zhp;
6836 int flags = 0;
6837 char *tosnap = NULL;
6838 char *fromsnap = NULL;
6839 char *atp, *copy;
6840 int err = 0;
6841 int c;
6843 while ((c = getopt(argc, argv, "FHt")) != -1) {
6844 switch (c) {
6845 case 'F':
6846 flags |= ZFS_DIFF_CLASSIFY;
6847 break;
6848 case 'H':
6849 flags |= ZFS_DIFF_PARSEABLE;
6850 break;
6851 case 't':
6852 flags |= ZFS_DIFF_TIMESTAMP;
6853 break;
6854 default:
6855 (void) fprintf(stderr,
6856 gettext("invalid option '%c'\n"), optopt);
6857 usage(B_FALSE);
6861 argc -= optind;
6862 argv += optind;
6864 if (argc < 1) {
6865 (void) fprintf(stderr,
6866 gettext("must provide at least one snapshot name\n"));
6867 usage(B_FALSE);
6870 if (argc > 2) {
6871 (void) fprintf(stderr, gettext("too many arguments\n"));
6872 usage(B_FALSE);
6875 fromsnap = argv[0];
6876 tosnap = (argc == 2) ? argv[1] : NULL;
6878 copy = NULL;
6879 if (*fromsnap != '@')
6880 copy = strdup(fromsnap);
6881 else if (tosnap)
6882 copy = strdup(tosnap);
6883 if (copy == NULL)
6884 usage(B_FALSE);
6886 if ((atp = strchr(copy, '@')) != NULL)
6887 *atp = '\0';
6889 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6890 return (1);
6892 free(copy);
6895 * Ignore SIGPIPE so that the library can give us
6896 * information on any failure
6898 (void) sigignore(SIGPIPE);
6900 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6902 zfs_close(zhp);
6904 return (err != 0);
6907 static int
6908 zfs_do_remap(int argc, char **argv)
6910 const char *fsname;
6911 int err = 0;
6912 if (argc != 2) {
6913 (void) fprintf(stderr, gettext("wrong number of arguments\n"));
6914 usage(B_FALSE);
6917 fsname = argv[1];
6918 err = zfs_remap_indirects(g_zfs, fsname);
6920 return (err);
6924 * zfs bookmark <fs@snap> <fs#bmark>
6926 * Creates a bookmark with the given name from the given snapshot.
6928 static int
6929 zfs_do_bookmark(int argc, char **argv)
6931 char snapname[ZFS_MAX_DATASET_NAME_LEN];
6932 zfs_handle_t *zhp;
6933 nvlist_t *nvl;
6934 int ret = 0;
6935 int c;
6937 /* check options */
6938 while ((c = getopt(argc, argv, "")) != -1) {
6939 switch (c) {
6940 case '?':
6941 (void) fprintf(stderr,
6942 gettext("invalid option '%c'\n"), optopt);
6943 goto usage;
6947 argc -= optind;
6948 argv += optind;
6950 /* check number of arguments */
6951 if (argc < 1) {
6952 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6953 goto usage;
6955 if (argc < 2) {
6956 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6957 goto usage;
6960 if (strchr(argv[1], '#') == NULL) {
6961 (void) fprintf(stderr,
6962 gettext("invalid bookmark name '%s' -- "
6963 "must contain a '#'\n"), argv[1]);
6964 goto usage;
6967 if (argv[0][0] == '@') {
6969 * Snapshot name begins with @.
6970 * Default to same fs as bookmark.
6972 (void) strncpy(snapname, argv[1], sizeof (snapname));
6973 *strchr(snapname, '#') = '\0';
6974 (void) strlcat(snapname, argv[0], sizeof (snapname));
6975 } else {
6976 (void) strncpy(snapname, argv[0], sizeof (snapname));
6978 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6979 if (zhp == NULL)
6980 goto usage;
6981 zfs_close(zhp);
6984 nvl = fnvlist_alloc();
6985 fnvlist_add_string(nvl, argv[1], snapname);
6986 ret = lzc_bookmark(nvl, NULL);
6987 fnvlist_free(nvl);
6989 if (ret != 0) {
6990 const char *err_msg;
6991 char errbuf[1024];
6993 (void) snprintf(errbuf, sizeof (errbuf),
6994 dgettext(TEXT_DOMAIN,
6995 "cannot create bookmark '%s'"), argv[1]);
6997 switch (ret) {
6998 case EXDEV:
6999 err_msg = "bookmark is in a different pool";
7000 break;
7001 case EEXIST:
7002 err_msg = "bookmark exists";
7003 break;
7004 case EINVAL:
7005 err_msg = "invalid argument";
7006 break;
7007 case ENOTSUP:
7008 err_msg = "bookmark feature not enabled";
7009 break;
7010 case ENOSPC:
7011 err_msg = "out of space";
7012 break;
7013 default:
7014 err_msg = "unknown error";
7015 break;
7017 (void) fprintf(stderr, "%s: %s\n", errbuf,
7018 dgettext(TEXT_DOMAIN, err_msg));
7021 return (ret != 0);
7023 usage:
7024 usage(B_FALSE);
7025 return (-1);
7028 static int
7029 zfs_do_channel_program(int argc, char **argv)
7031 int ret, fd;
7032 char c;
7033 char *progbuf, *filename, *poolname;
7034 size_t progsize, progread;
7035 nvlist_t *outnvl;
7036 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7037 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7038 boolean_t sync_flag = B_TRUE;
7039 zpool_handle_t *zhp;
7041 /* check options */
7042 while (-1 !=
7043 (c = getopt(argc, argv, "nt:(instr-limit)m:(memory-limit)"))) {
7044 switch (c) {
7045 case 't':
7046 case 'm': {
7047 uint64_t arg;
7048 char *endp;
7050 errno = 0;
7051 arg = strtoull(optarg, &endp, 0);
7052 if (errno != 0 || *endp != '\0') {
7053 (void) fprintf(stderr, gettext(
7054 "invalid argument "
7055 "'%s': expected integer\n"), optarg);
7056 goto usage;
7059 if (c == 't') {
7060 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7061 (void) fprintf(stderr, gettext(
7062 "Invalid instruction limit: "
7063 "%s\n"), optarg);
7064 return (1);
7065 } else {
7066 instrlimit = arg;
7068 } else {
7069 ASSERT3U(c, ==, 'm');
7070 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7071 (void) fprintf(stderr, gettext(
7072 "Invalid memory limit: "
7073 "%s\n"), optarg);
7074 return (1);
7075 } else {
7076 memlimit = arg;
7079 break;
7081 case 'n': {
7082 sync_flag = B_FALSE;
7083 break;
7085 case '?':
7086 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7087 optopt);
7088 goto usage;
7092 argc -= optind;
7093 argv += optind;
7095 if (argc < 2) {
7096 (void) fprintf(stderr,
7097 gettext("invalid number of arguments\n"));
7098 goto usage;
7101 poolname = argv[0];
7102 filename = argv[1];
7103 if (strcmp(filename, "-") == 0) {
7104 fd = 0;
7105 filename = "standard input";
7106 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7107 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7108 filename, strerror(errno));
7109 return (1);
7112 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7113 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7114 poolname);
7115 return (1);
7117 zpool_close(zhp);
7120 * Read in the channel program, expanding the program buffer as
7121 * necessary.
7123 progread = 0;
7124 progsize = 1024;
7125 progbuf = safe_malloc(progsize);
7126 do {
7127 ret = read(fd, progbuf + progread, progsize - progread);
7128 progread += ret;
7129 if (progread == progsize && ret > 0) {
7130 progsize *= 2;
7131 progbuf = safe_realloc(progbuf, progsize);
7133 } while (ret > 0);
7135 if (fd != 0)
7136 (void) close(fd);
7137 if (ret < 0) {
7138 free(progbuf);
7139 (void) fprintf(stderr,
7140 gettext("cannot read '%s': %s\n"),
7141 filename, strerror(errno));
7142 return (1);
7144 progbuf[progread] = '\0';
7147 * Any remaining arguments are passed as arguments to the lua script as
7148 * a string array:
7150 * "argv" -> [ "arg 1", ... "arg n" ],
7153 nvlist_t *argnvl = fnvlist_alloc();
7154 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7156 if (sync_flag) {
7157 ret = lzc_channel_program(poolname, progbuf,
7158 instrlimit, memlimit, argnvl, &outnvl);
7159 } else {
7160 ret = lzc_channel_program_nosync(poolname, progbuf,
7161 instrlimit, memlimit, argnvl, &outnvl);
7164 if (ret != 0) {
7166 * On error, report the error message handed back by lua if one
7167 * exists. Otherwise, generate an appropriate error message,
7168 * falling back on strerror() for an unexpected return code.
7170 char *errstring = NULL;
7171 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7172 (void) nvlist_lookup_string(outnvl,
7173 ZCP_RET_ERROR, &errstring);
7174 if (errstring == NULL)
7175 errstring = strerror(ret);
7176 } else {
7177 switch (ret) {
7178 case EINVAL:
7179 errstring =
7180 "Invalid instruction or memory limit.";
7181 break;
7182 case ENOMEM:
7183 errstring = "Return value too large.";
7184 break;
7185 case ENOSPC:
7186 errstring = "Memory limit exhausted.";
7187 break;
7188 case ETIME:
7189 errstring = "Timed out.";
7190 break;
7191 case EPERM:
7192 errstring = "Permission denied. Channel "
7193 "programs must be run as root.";
7194 break;
7195 default:
7196 errstring = strerror(ret);
7199 (void) fprintf(stderr,
7200 gettext("Channel program execution failed:\n%s\n"),
7201 errstring);
7202 } else {
7203 (void) printf("Channel program fully executed ");
7204 if (nvlist_empty(outnvl)) {
7205 (void) printf("with no return value.\n");
7206 } else {
7207 (void) printf("with return value:\n");
7208 dump_nvlist(outnvl, 4);
7212 free(progbuf);
7213 fnvlist_free(outnvl);
7214 fnvlist_free(argnvl);
7215 return (ret != 0);
7217 usage:
7218 usage(B_FALSE);
7219 return (-1);
7223 main(int argc, char **argv)
7225 int ret = 0;
7226 int i;
7227 char *progname;
7228 char *cmdname;
7230 (void) setlocale(LC_ALL, "");
7231 (void) textdomain(TEXT_DOMAIN);
7233 opterr = 0;
7235 if ((g_zfs = libzfs_init()) == NULL) {
7236 (void) fprintf(stderr, gettext("internal error: failed to "
7237 "initialize ZFS library\n"));
7238 return (1);
7241 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7243 libzfs_print_on_error(g_zfs, B_TRUE);
7245 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7246 (void) fprintf(stderr, gettext("internal error: unable to "
7247 "open %s\n"), MNTTAB);
7248 return (1);
7252 * This command also doubles as the /etc/fs mount and unmount program.
7253 * Determine if we should take this behavior based on argv[0].
7255 progname = basename(argv[0]);
7256 if (strcmp(progname, "mount") == 0) {
7257 ret = manual_mount(argc, argv);
7258 } else if (strcmp(progname, "umount") == 0) {
7259 ret = manual_unmount(argc, argv);
7260 } else {
7262 * Make sure the user has specified some command.
7264 if (argc < 2) {
7265 (void) fprintf(stderr, gettext("missing command\n"));
7266 usage(B_FALSE);
7269 cmdname = argv[1];
7272 * The 'umount' command is an alias for 'unmount'
7274 if (strcmp(cmdname, "umount") == 0)
7275 cmdname = "unmount";
7278 * The 'recv' command is an alias for 'receive'
7280 if (strcmp(cmdname, "recv") == 0)
7281 cmdname = "receive";
7284 * The 'snap' command is an alias for 'snapshot'
7286 if (strcmp(cmdname, "snap") == 0)
7287 cmdname = "snapshot";
7290 * Special case '-?'
7292 if (strcmp(cmdname, "-?") == 0)
7293 usage(B_TRUE);
7296 * Run the appropriate command.
7298 libzfs_mnttab_cache(g_zfs, B_TRUE);
7299 if (find_command_idx(cmdname, &i) == 0) {
7300 current_command = &command_table[i];
7301 ret = command_table[i].func(argc - 1, argv + 1);
7302 } else if (strchr(cmdname, '=') != NULL) {
7303 verify(find_command_idx("set", &i) == 0);
7304 current_command = &command_table[i];
7305 ret = command_table[i].func(argc, argv);
7306 } else {
7307 (void) fprintf(stderr, gettext("unrecognized "
7308 "command '%s'\n"), cmdname);
7309 usage(B_FALSE);
7311 libzfs_mnttab_cache(g_zfs, B_FALSE);
7314 (void) fclose(mnttab_file);
7316 if (ret == 0 && log_history)
7317 (void) zpool_log_history(g_zfs, history_str);
7319 libzfs_fini(g_zfs);
7322 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7323 * for the purposes of running ::findleaks.
7325 if (getenv("ZFS_ABORT") != NULL) {
7326 (void) printf("dumping core by request\n");
7327 abort();
7330 return (ret);