]> git.proxmox.com Git - mirror_zfs.git/blob - cmd/zfs/zfs_main.c
OpenZFS 2605, 6980, 6902
[mirror_zfs.git] / cmd / zfs / zfs_main.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 */
30
31 #include <assert.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <libgen.h>
35 #include <libintl.h>
36 #include <libuutil.h>
37 #include <libnvpair.h>
38 #include <locale.h>
39 #include <stddef.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <strings.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <zone.h>
46 #include <grp.h>
47 #include <pwd.h>
48 #include <signal.h>
49 #include <sys/list.h>
50 #include <sys/mkdev.h>
51 #include <sys/mntent.h>
52 #include <sys/mnttab.h>
53 #include <sys/mount.h>
54 #include <sys/stat.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/types.h>
57 #include <time.h>
58
59 #include <libzfs.h>
60 #include <libzfs_core.h>
61 #include <zfs_prop.h>
62 #include <zfs_deleg.h>
63 #include <libuutil.h>
64 #ifdef HAVE_IDMAP
65 #include <aclutils.h>
66 #include <directory.h>
67 #endif /* HAVE_IDMAP */
68
69 #include "zfs_iter.h"
70 #include "zfs_util.h"
71 #include "zfs_comutil.h"
72 #include "libzfs_impl.h"
73
74 libzfs_handle_t *g_zfs;
75
76 static FILE *mnttab_file;
77 static char history_str[HIS_MAX_RECORD_LEN];
78 static boolean_t log_history = B_TRUE;
79
80 static int zfs_do_clone(int argc, char **argv);
81 static int zfs_do_create(int argc, char **argv);
82 static int zfs_do_destroy(int argc, char **argv);
83 static int zfs_do_get(int argc, char **argv);
84 static int zfs_do_inherit(int argc, char **argv);
85 static int zfs_do_list(int argc, char **argv);
86 static int zfs_do_mount(int argc, char **argv);
87 static int zfs_do_rename(int argc, char **argv);
88 static int zfs_do_rollback(int argc, char **argv);
89 static int zfs_do_set(int argc, char **argv);
90 static int zfs_do_upgrade(int argc, char **argv);
91 static int zfs_do_snapshot(int argc, char **argv);
92 static int zfs_do_unmount(int argc, char **argv);
93 static int zfs_do_share(int argc, char **argv);
94 static int zfs_do_unshare(int argc, char **argv);
95 static int zfs_do_send(int argc, char **argv);
96 static int zfs_do_receive(int argc, char **argv);
97 static int zfs_do_promote(int argc, char **argv);
98 static int zfs_do_userspace(int argc, char **argv);
99 static int zfs_do_allow(int argc, char **argv);
100 static int zfs_do_unallow(int argc, char **argv);
101 static int zfs_do_hold(int argc, char **argv);
102 static int zfs_do_holds(int argc, char **argv);
103 static int zfs_do_release(int argc, char **argv);
104 static int zfs_do_diff(int argc, char **argv);
105 static int zfs_do_bookmark(int argc, char **argv);
106
107 /*
108 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
109 */
110
111 #ifdef DEBUG
112 const char *
113 _umem_debug_init(void)
114 {
115 return ("default,verbose"); /* $UMEM_DEBUG setting */
116 }
117
118 const char *
119 _umem_logging_init(void)
120 {
121 return ("fail,contents"); /* $UMEM_LOGGING setting */
122 }
123 #endif
124
125 typedef enum {
126 HELP_CLONE,
127 HELP_CREATE,
128 HELP_DESTROY,
129 HELP_GET,
130 HELP_INHERIT,
131 HELP_UPGRADE,
132 HELP_LIST,
133 HELP_MOUNT,
134 HELP_PROMOTE,
135 HELP_RECEIVE,
136 HELP_RENAME,
137 HELP_ROLLBACK,
138 HELP_SEND,
139 HELP_SET,
140 HELP_SHARE,
141 HELP_SNAPSHOT,
142 HELP_UNMOUNT,
143 HELP_UNSHARE,
144 HELP_ALLOW,
145 HELP_UNALLOW,
146 HELP_USERSPACE,
147 HELP_GROUPSPACE,
148 HELP_HOLD,
149 HELP_HOLDS,
150 HELP_RELEASE,
151 HELP_DIFF,
152 HELP_BOOKMARK,
153 } zfs_help_t;
154
155 typedef struct zfs_command {
156 const char *name;
157 int (*func)(int argc, char **argv);
158 zfs_help_t usage;
159 } zfs_command_t;
160
161 /*
162 * Master command table. Each ZFS command has a name, associated function, and
163 * usage message. The usage messages need to be internationalized, so we have
164 * to have a function to return the usage message based on a command index.
165 *
166 * These commands are organized according to how they are displayed in the usage
167 * message. An empty command (one with a NULL name) indicates an empty line in
168 * the generic usage message.
169 */
170 static zfs_command_t command_table[] = {
171 { "create", zfs_do_create, HELP_CREATE },
172 { "destroy", zfs_do_destroy, HELP_DESTROY },
173 { NULL },
174 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
175 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
176 { "clone", zfs_do_clone, HELP_CLONE },
177 { "promote", zfs_do_promote, HELP_PROMOTE },
178 { "rename", zfs_do_rename, HELP_RENAME },
179 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
180 { NULL },
181 { "list", zfs_do_list, HELP_LIST },
182 { NULL },
183 { "set", zfs_do_set, HELP_SET },
184 { "get", zfs_do_get, HELP_GET },
185 { "inherit", zfs_do_inherit, HELP_INHERIT },
186 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
187 { "userspace", zfs_do_userspace, HELP_USERSPACE },
188 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
189 { NULL },
190 { "mount", zfs_do_mount, HELP_MOUNT },
191 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
192 { "share", zfs_do_share, HELP_SHARE },
193 { "unshare", zfs_do_unshare, HELP_UNSHARE },
194 { NULL },
195 { "send", zfs_do_send, HELP_SEND },
196 { "receive", zfs_do_receive, HELP_RECEIVE },
197 { NULL },
198 { "allow", zfs_do_allow, HELP_ALLOW },
199 { NULL },
200 { "unallow", zfs_do_unallow, HELP_UNALLOW },
201 { NULL },
202 { "hold", zfs_do_hold, HELP_HOLD },
203 { "holds", zfs_do_holds, HELP_HOLDS },
204 { "release", zfs_do_release, HELP_RELEASE },
205 { "diff", zfs_do_diff, HELP_DIFF },
206 };
207
208 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
209
210 zfs_command_t *current_command;
211
212 static const char *
213 get_usage(zfs_help_t idx)
214 {
215 switch (idx) {
216 case HELP_CLONE:
217 return (gettext("\tclone [-p] [-o property=value] ... "
218 "<snapshot> <filesystem|volume>\n"));
219 case HELP_CREATE:
220 return (gettext("\tcreate [-p] [-o property=value] ... "
221 "<filesystem>\n"
222 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
223 "-V <size> <volume>\n"));
224 case HELP_DESTROY:
225 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
226 "\tdestroy [-dnpRrv] "
227 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
228 "\tdestroy <filesystem|volume>#<bookmark>\n"));
229 case HELP_GET:
230 return (gettext("\tget [-rHp] [-d max] "
231 "[-o \"all\" | field[,...]]\n"
232 "\t [-t type[,...]] [-s source[,...]]\n"
233 "\t <\"all\" | property[,...]> "
234 "[filesystem|volume|snapshot] ...\n"));
235 case HELP_INHERIT:
236 return (gettext("\tinherit [-rS] <property> "
237 "<filesystem|volume|snapshot> ...\n"));
238 case HELP_UPGRADE:
239 return (gettext("\tupgrade [-v]\n"
240 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
241 case HELP_LIST:
242 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
243 "[-s property]...\n\t [-S property]... [-t type[,...]] "
244 "[filesystem|volume|snapshot] ...\n"));
245 case HELP_MOUNT:
246 return (gettext("\tmount\n"
247 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
248 case HELP_PROMOTE:
249 return (gettext("\tpromote <clone-filesystem>\n"));
250 case HELP_RECEIVE:
251 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
252 "snapshot>\n"
253 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
254 "<filesystem>\n"
255 "\treceive -A <filesystem|volume>\n"));
256 case HELP_RENAME:
257 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
258 "<filesystem|volume|snapshot>\n"
259 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
260 "\trename -r <snapshot> <snapshot>\n"));
261 case HELP_ROLLBACK:
262 return (gettext("\trollback [-rRf] <snapshot>\n"));
263 case HELP_SEND:
264 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
265 "<snapshot>\n"
266 "\tsend [-Le] [-i snapshot|bookmark] "
267 "<filesystem|volume|snapshot>\n"
268 "\tsend [-nvPe] -t <receive_resume_token>\n"));
269 case HELP_SET:
270 return (gettext("\tset <property=value> ... "
271 "<filesystem|volume|snapshot> ...\n"));
272 case HELP_SHARE:
273 return (gettext("\tshare <-a | filesystem>\n"));
274 case HELP_SNAPSHOT:
275 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
276 "<filesystem|volume>@<snap> ...\n"));
277 case HELP_UNMOUNT:
278 return (gettext("\tunmount [-f] "
279 "<-a | filesystem|mountpoint>\n"));
280 case HELP_UNSHARE:
281 return (gettext("\tunshare "
282 "<-a | filesystem|mountpoint>\n"));
283 case HELP_ALLOW:
284 return (gettext("\tallow <filesystem|volume>\n"
285 "\tallow [-ldug] "
286 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
287 "\t <filesystem|volume>\n"
288 "\tallow [-ld] -e <perm|@setname>[,...] "
289 "<filesystem|volume>\n"
290 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
291 "\tallow -s @setname <perm|@setname>[,...] "
292 "<filesystem|volume>\n"));
293 case HELP_UNALLOW:
294 return (gettext("\tunallow [-rldug] "
295 "<\"everyone\"|user|group>[,...]\n"
296 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
297 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
298 "<filesystem|volume>\n"
299 "\tunallow [-r] -c [<perm|@setname>[,...]] "
300 "<filesystem|volume>\n"
301 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
302 "<filesystem|volume>\n"));
303 case HELP_USERSPACE:
304 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
305 "[-s field] ...\n"
306 "\t [-S field] ... [-t type[,...]] "
307 "<filesystem|snapshot>\n"));
308 case HELP_GROUPSPACE:
309 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
310 "[-s field] ...\n"
311 "\t [-S field] ... [-t type[,...]] "
312 "<filesystem|snapshot>\n"));
313 case HELP_HOLD:
314 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
315 case HELP_HOLDS:
316 return (gettext("\tholds [-r] <snapshot> ...\n"));
317 case HELP_RELEASE:
318 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
319 case HELP_DIFF:
320 return (gettext("\tdiff [-FHt] <snapshot> "
321 "[snapshot|filesystem]\n"));
322 case HELP_BOOKMARK:
323 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
324 }
325
326 abort();
327 /* NOTREACHED */
328 }
329
330 void
331 nomem(void)
332 {
333 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
334 exit(1);
335 }
336
337 /*
338 * Utility function to guarantee malloc() success.
339 */
340
341 void *
342 safe_malloc(size_t size)
343 {
344 void *data;
345
346 if ((data = calloc(1, size)) == NULL)
347 nomem();
348
349 return (data);
350 }
351
352 static char *
353 safe_strdup(char *str)
354 {
355 char *dupstr = strdup(str);
356
357 if (dupstr == NULL)
358 nomem();
359
360 return (dupstr);
361 }
362
363 /*
364 * Callback routine that will print out information for each of
365 * the properties.
366 */
367 static int
368 usage_prop_cb(int prop, void *cb)
369 {
370 FILE *fp = cb;
371
372 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
373
374 if (zfs_prop_readonly(prop))
375 (void) fprintf(fp, " NO ");
376 else
377 (void) fprintf(fp, "YES ");
378
379 if (zfs_prop_inheritable(prop))
380 (void) fprintf(fp, " YES ");
381 else
382 (void) fprintf(fp, " NO ");
383
384 if (zfs_prop_values(prop) == NULL)
385 (void) fprintf(fp, "-\n");
386 else
387 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
388
389 return (ZPROP_CONT);
390 }
391
392 /*
393 * Display usage message. If we're inside a command, display only the usage for
394 * that command. Otherwise, iterate over the entire command table and display
395 * a complete usage message.
396 */
397 static void
398 usage(boolean_t requested)
399 {
400 int i;
401 boolean_t show_properties = B_FALSE;
402 FILE *fp = requested ? stdout : stderr;
403
404 if (current_command == NULL) {
405
406 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
407 (void) fprintf(fp,
408 gettext("where 'command' is one of the following:\n\n"));
409
410 for (i = 0; i < NCOMMAND; i++) {
411 if (command_table[i].name == NULL)
412 (void) fprintf(fp, "\n");
413 else
414 (void) fprintf(fp, "%s",
415 get_usage(command_table[i].usage));
416 }
417
418 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
419 "pool/[dataset/]*dataset[@name]\n"));
420 } else {
421 (void) fprintf(fp, gettext("usage:\n"));
422 (void) fprintf(fp, "%s", get_usage(current_command->usage));
423 }
424
425 if (current_command != NULL &&
426 (strcmp(current_command->name, "set") == 0 ||
427 strcmp(current_command->name, "get") == 0 ||
428 strcmp(current_command->name, "inherit") == 0 ||
429 strcmp(current_command->name, "list") == 0))
430 show_properties = B_TRUE;
431
432 if (show_properties) {
433 (void) fprintf(fp,
434 gettext("\nThe following properties are supported:\n"));
435
436 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
437 "PROPERTY", "EDIT", "INHERIT", "VALUES");
438
439 /* Iterate over all properties */
440 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
441 ZFS_TYPE_DATASET);
442
443 (void) fprintf(fp, "\t%-15s ", "userused@...");
444 (void) fprintf(fp, " NO NO <size>\n");
445 (void) fprintf(fp, "\t%-15s ", "groupused@...");
446 (void) fprintf(fp, " NO NO <size>\n");
447 (void) fprintf(fp, "\t%-15s ", "userquota@...");
448 (void) fprintf(fp, "YES NO <size> | none\n");
449 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
450 (void) fprintf(fp, "YES NO <size> | none\n");
451 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
452 (void) fprintf(fp, " NO NO <size>\n");
453
454 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
455 "with standard units such as K, M, G, etc.\n"));
456 (void) fprintf(fp, gettext("\nUser-defined properties can "
457 "be specified by using a name containing a colon (:).\n"));
458 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
459 "properties must be appended with\n"
460 "a user or group specifier of one of these forms:\n"
461 " POSIX name (eg: \"matt\")\n"
462 " POSIX id (eg: \"126829\")\n"
463 " SMB name@domain (eg: \"matt@sun\")\n"
464 " SMB SID (eg: \"S-1-234-567-89\")\n"));
465 } else {
466 (void) fprintf(fp,
467 gettext("\nFor the property list, run: %s\n"),
468 "zfs set|get");
469 (void) fprintf(fp,
470 gettext("\nFor the delegated permission list, run: %s\n"),
471 "zfs allow|unallow");
472 }
473
474 /*
475 * See comments at end of main().
476 */
477 if (getenv("ZFS_ABORT") != NULL) {
478 (void) printf("dumping core by request\n");
479 abort();
480 }
481
482 exit(requested ? 0 : 2);
483 }
484
485 /*
486 * Take a property=value argument string and add it to the given nvlist.
487 * Modifies the argument inplace.
488 */
489 static int
490 parseprop(nvlist_t *props, char *propname)
491 {
492 char *propval, *strval;
493
494 if ((propval = strchr(propname, '=')) == NULL) {
495 (void) fprintf(stderr, gettext("missing "
496 "'=' for property=value argument\n"));
497 return (-1);
498 }
499 *propval = '\0';
500 propval++;
501 if (nvlist_lookup_string(props, propname, &strval) == 0) {
502 (void) fprintf(stderr, gettext("property '%s' "
503 "specified multiple times\n"), propname);
504 return (-1);
505 }
506 if (nvlist_add_string(props, propname, propval) != 0)
507 nomem();
508 return (0);
509 }
510
511 static int
512 parse_depth(char *opt, int *flags)
513 {
514 char *tmp;
515 int depth;
516
517 depth = (int)strtol(opt, &tmp, 0);
518 if (*tmp) {
519 (void) fprintf(stderr,
520 gettext("%s is not an integer\n"), optarg);
521 usage(B_FALSE);
522 }
523 if (depth < 0) {
524 (void) fprintf(stderr,
525 gettext("Depth can not be negative.\n"));
526 usage(B_FALSE);
527 }
528 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
529 return (depth);
530 }
531
532 #define PROGRESS_DELAY 2 /* seconds */
533
534 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";
535 static time_t pt_begin;
536 static char *pt_header = NULL;
537 static boolean_t pt_shown;
538
539 static void
540 start_progress_timer(void)
541 {
542 pt_begin = time(NULL) + PROGRESS_DELAY;
543 pt_shown = B_FALSE;
544 }
545
546 static void
547 set_progress_header(char *header)
548 {
549 assert(pt_header == NULL);
550 pt_header = safe_strdup(header);
551 if (pt_shown) {
552 (void) printf("%s: ", header);
553 (void) fflush(stdout);
554 }
555 }
556
557 static void
558 update_progress(char *update)
559 {
560 if (!pt_shown && time(NULL) > pt_begin) {
561 int len = strlen(update);
562
563 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
564 pt_reverse);
565 (void) fflush(stdout);
566 pt_shown = B_TRUE;
567 } else if (pt_shown) {
568 int len = strlen(update);
569
570 (void) printf("%s%*.*s", update, len, len, pt_reverse);
571 (void) fflush(stdout);
572 }
573 }
574
575 static void
576 finish_progress(char *done)
577 {
578 if (pt_shown) {
579 (void) printf("%s\n", done);
580 (void) fflush(stdout);
581 }
582 free(pt_header);
583 pt_header = NULL;
584 }
585
586 static int
587 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
588 {
589 zfs_handle_t *zhp = NULL;
590 int ret = 0;
591
592 zhp = zfs_open(hdl, dataset, type);
593 if (zhp == NULL)
594 return (1);
595
596 /*
597 * Volumes may neither be mounted or shared. Potentially in the
598 * future filesystems detected on these volumes could be mounted.
599 */
600 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
601 zfs_close(zhp);
602 return (0);
603 }
604
605 /*
606 * Mount and/or share the new filesystem as appropriate. We provide a
607 * verbose error message to let the user know that their filesystem was
608 * in fact created, even if we failed to mount or share it.
609 *
610 * If the user doesn't want the dataset automatically mounted, then
611 * skip the mount/share step
612 */
613 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
614 zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
615 if (geteuid() != 0) {
616 (void) fprintf(stderr, gettext("filesystem "
617 "successfully created, but it may only be "
618 "mounted by root\n"));
619 ret = 1;
620 } else if (zfs_mount(zhp, NULL, 0) != 0) {
621 (void) fprintf(stderr, gettext("filesystem "
622 "successfully created, but not mounted\n"));
623 ret = 1;
624 } else if (zfs_share(zhp) != 0) {
625 (void) fprintf(stderr, gettext("filesystem "
626 "successfully created, but not shared\n"));
627 ret = 1;
628 }
629 }
630
631 zfs_close(zhp);
632
633 return (ret);
634 }
635
636 /*
637 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
638 *
639 * Given an existing dataset, create a writable copy whose initial contents
640 * are the same as the source. The newly created dataset maintains a
641 * dependency on the original; the original cannot be destroyed so long as
642 * the clone exists.
643 *
644 * The '-p' flag creates all the non-existing ancestors of the target first.
645 */
646 static int
647 zfs_do_clone(int argc, char **argv)
648 {
649 zfs_handle_t *zhp = NULL;
650 boolean_t parents = B_FALSE;
651 nvlist_t *props;
652 int ret = 0;
653 int c;
654
655 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
656 nomem();
657
658 /* check options */
659 while ((c = getopt(argc, argv, "o:p")) != -1) {
660 switch (c) {
661 case 'o':
662 if (parseprop(props, optarg) != 0)
663 return (1);
664 break;
665 case 'p':
666 parents = B_TRUE;
667 break;
668 case '?':
669 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
670 optopt);
671 goto usage;
672 }
673 }
674
675 argc -= optind;
676 argv += optind;
677
678 /* check number of arguments */
679 if (argc < 1) {
680 (void) fprintf(stderr, gettext("missing source dataset "
681 "argument\n"));
682 goto usage;
683 }
684 if (argc < 2) {
685 (void) fprintf(stderr, gettext("missing target dataset "
686 "argument\n"));
687 goto usage;
688 }
689 if (argc > 2) {
690 (void) fprintf(stderr, gettext("too many arguments\n"));
691 goto usage;
692 }
693
694 /* open the source dataset */
695 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
696 return (1);
697
698 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
699 ZFS_TYPE_VOLUME)) {
700 /*
701 * Now create the ancestors of the target dataset. If the
702 * target already exists and '-p' option was used we should not
703 * complain.
704 */
705 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
706 ZFS_TYPE_VOLUME))
707 return (0);
708 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
709 return (1);
710 }
711
712 /* pass to libzfs */
713 ret = zfs_clone(zhp, argv[1], props);
714
715 /* create the mountpoint if necessary */
716 if (ret == 0) {
717 if (log_history) {
718 (void) zpool_log_history(g_zfs, history_str);
719 log_history = B_FALSE;
720 }
721
722 ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
723 }
724
725 zfs_close(zhp);
726 nvlist_free(props);
727
728 return (!!ret);
729
730 usage:
731 if (zhp)
732 zfs_close(zhp);
733 nvlist_free(props);
734 usage(B_FALSE);
735 return (-1);
736 }
737
738 /*
739 * zfs create [-p] [-o prop=value] ... fs
740 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
741 *
742 * Create a new dataset. This command can be used to create filesystems
743 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
744 * For volumes, the user must specify a size to be used.
745 *
746 * The '-s' flag applies only to volumes, and indicates that we should not try
747 * to set the reservation for this volume. By default we set a reservation
748 * equal to the size for any volume. For pools with SPA_VERSION >=
749 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
750 *
751 * The '-p' flag creates all the non-existing ancestors of the target first.
752 */
753 static int
754 zfs_do_create(int argc, char **argv)
755 {
756 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
757 uint64_t volsize = 0;
758 int c;
759 boolean_t noreserve = B_FALSE;
760 boolean_t bflag = B_FALSE;
761 boolean_t parents = B_FALSE;
762 int ret = 1;
763 nvlist_t *props;
764 uint64_t intval;
765
766 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
767 nomem();
768
769 /* check options */
770 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
771 switch (c) {
772 case 'V':
773 type = ZFS_TYPE_VOLUME;
774 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
775 (void) fprintf(stderr, gettext("bad volume "
776 "size '%s': %s\n"), optarg,
777 libzfs_error_description(g_zfs));
778 goto error;
779 }
780
781 if (nvlist_add_uint64(props,
782 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
783 nomem();
784 volsize = intval;
785 break;
786 case 'p':
787 parents = B_TRUE;
788 break;
789 case 'b':
790 bflag = B_TRUE;
791 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
792 (void) fprintf(stderr, gettext("bad volume "
793 "block size '%s': %s\n"), optarg,
794 libzfs_error_description(g_zfs));
795 goto error;
796 }
797
798 if (nvlist_add_uint64(props,
799 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
800 intval) != 0)
801 nomem();
802 break;
803 case 'o':
804 if (parseprop(props, optarg) != 0)
805 goto error;
806 break;
807 case 's':
808 noreserve = B_TRUE;
809 break;
810 case ':':
811 (void) fprintf(stderr, gettext("missing size "
812 "argument\n"));
813 goto badusage;
814 case '?':
815 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
816 optopt);
817 goto badusage;
818 }
819 }
820
821 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
822 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
823 "used when creating a volume\n"));
824 goto badusage;
825 }
826
827 argc -= optind;
828 argv += optind;
829
830 /* check number of arguments */
831 if (argc == 0) {
832 (void) fprintf(stderr, gettext("missing %s argument\n"),
833 zfs_type_to_name(type));
834 goto badusage;
835 }
836 if (argc > 1) {
837 (void) fprintf(stderr, gettext("too many arguments\n"));
838 goto badusage;
839 }
840
841 if (type == ZFS_TYPE_VOLUME && !noreserve) {
842 zpool_handle_t *zpool_handle;
843 nvlist_t *real_props = NULL;
844 uint64_t spa_version;
845 char *p;
846 zfs_prop_t resv_prop;
847 char *strval;
848 char msg[1024];
849
850 if ((p = strchr(argv[0], '/')))
851 *p = '\0';
852 zpool_handle = zpool_open(g_zfs, argv[0]);
853 if (p != NULL)
854 *p = '/';
855 if (zpool_handle == NULL)
856 goto error;
857 spa_version = zpool_get_prop_int(zpool_handle,
858 ZPOOL_PROP_VERSION, NULL);
859 if (spa_version >= SPA_VERSION_REFRESERVATION)
860 resv_prop = ZFS_PROP_REFRESERVATION;
861 else
862 resv_prop = ZFS_PROP_RESERVATION;
863
864 (void) snprintf(msg, sizeof (msg),
865 gettext("cannot create '%s'"), argv[0]);
866 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
867 props, 0, NULL, zpool_handle, msg)) == NULL) {
868 zpool_close(zpool_handle);
869 goto error;
870 }
871 zpool_close(zpool_handle);
872
873 volsize = zvol_volsize_to_reservation(volsize, real_props);
874 nvlist_free(real_props);
875
876 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
877 &strval) != 0) {
878 if (nvlist_add_uint64(props,
879 zfs_prop_to_name(resv_prop), volsize) != 0) {
880 nvlist_free(props);
881 nomem();
882 }
883 }
884 }
885
886 if (parents && zfs_name_valid(argv[0], type)) {
887 /*
888 * Now create the ancestors of target dataset. If the target
889 * already exists and '-p' option was used we should not
890 * complain.
891 */
892 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
893 ret = 0;
894 goto error;
895 }
896 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
897 goto error;
898 }
899
900 /* pass to libzfs */
901 if (zfs_create(g_zfs, argv[0], type, props) != 0)
902 goto error;
903
904 if (log_history) {
905 (void) zpool_log_history(g_zfs, history_str);
906 log_history = B_FALSE;
907 }
908
909 ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
910 error:
911 nvlist_free(props);
912 return (ret);
913 badusage:
914 nvlist_free(props);
915 usage(B_FALSE);
916 return (2);
917 }
918
919 /*
920 * zfs destroy [-rRf] <fs, vol>
921 * zfs destroy [-rRd] <snap>
922 *
923 * -r Recursively destroy all children
924 * -R Recursively destroy all dependents, including clones
925 * -f Force unmounting of any dependents
926 * -d If we can't destroy now, mark for deferred destruction
927 *
928 * Destroys the given dataset. By default, it will unmount any filesystems,
929 * and refuse to destroy a dataset that has any dependents. A dependent can
930 * either be a child, or a clone of a child.
931 */
932 typedef struct destroy_cbdata {
933 boolean_t cb_first;
934 boolean_t cb_force;
935 boolean_t cb_recurse;
936 boolean_t cb_error;
937 boolean_t cb_doclones;
938 zfs_handle_t *cb_target;
939 boolean_t cb_defer_destroy;
940 boolean_t cb_verbose;
941 boolean_t cb_parsable;
942 boolean_t cb_dryrun;
943 nvlist_t *cb_nvl;
944 nvlist_t *cb_batchedsnaps;
945
946 /* first snap in contiguous run */
947 char *cb_firstsnap;
948 /* previous snap in contiguous run */
949 char *cb_prevsnap;
950 int64_t cb_snapused;
951 char *cb_snapspec;
952 char *cb_bookmark;
953 } destroy_cbdata_t;
954
955 /*
956 * Check for any dependents based on the '-r' or '-R' flags.
957 */
958 static int
959 destroy_check_dependent(zfs_handle_t *zhp, void *data)
960 {
961 destroy_cbdata_t *cbp = data;
962 const char *tname = zfs_get_name(cbp->cb_target);
963 const char *name = zfs_get_name(zhp);
964
965 if (strncmp(tname, name, strlen(tname)) == 0 &&
966 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
967 /*
968 * This is a direct descendant, not a clone somewhere else in
969 * the hierarchy.
970 */
971 if (cbp->cb_recurse)
972 goto out;
973
974 if (cbp->cb_first) {
975 (void) fprintf(stderr, gettext("cannot destroy '%s': "
976 "%s has children\n"),
977 zfs_get_name(cbp->cb_target),
978 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
979 (void) fprintf(stderr, gettext("use '-r' to destroy "
980 "the following datasets:\n"));
981 cbp->cb_first = B_FALSE;
982 cbp->cb_error = B_TRUE;
983 }
984
985 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
986 } else {
987 /*
988 * This is a clone. We only want to report this if the '-r'
989 * wasn't specified, or the target is a snapshot.
990 */
991 if (!cbp->cb_recurse &&
992 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
993 goto out;
994
995 if (cbp->cb_first) {
996 (void) fprintf(stderr, gettext("cannot destroy '%s': "
997 "%s has dependent clones\n"),
998 zfs_get_name(cbp->cb_target),
999 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1000 (void) fprintf(stderr, gettext("use '-R' to destroy "
1001 "the following datasets:\n"));
1002 cbp->cb_first = B_FALSE;
1003 cbp->cb_error = B_TRUE;
1004 cbp->cb_dryrun = B_TRUE;
1005 }
1006
1007 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1008 }
1009
1010 out:
1011 zfs_close(zhp);
1012 return (0);
1013 }
1014
1015 static int
1016 destroy_callback(zfs_handle_t *zhp, void *data)
1017 {
1018 destroy_cbdata_t *cb = data;
1019 const char *name = zfs_get_name(zhp);
1020
1021 if (cb->cb_verbose) {
1022 if (cb->cb_parsable) {
1023 (void) printf("destroy\t%s\n", name);
1024 } else if (cb->cb_dryrun) {
1025 (void) printf(gettext("would destroy %s\n"),
1026 name);
1027 } else {
1028 (void) printf(gettext("will destroy %s\n"),
1029 name);
1030 }
1031 }
1032
1033 /*
1034 * Ignore pools (which we've already flagged as an error before getting
1035 * here).
1036 */
1037 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1038 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1039 zfs_close(zhp);
1040 return (0);
1041 }
1042 if (cb->cb_dryrun) {
1043 zfs_close(zhp);
1044 return (0);
1045 }
1046
1047 /*
1048 * We batch up all contiguous snapshots (even of different
1049 * filesystems) and destroy them with one ioctl. We can't
1050 * simply do all snap deletions and then all fs deletions,
1051 * because we must delete a clone before its origin.
1052 */
1053 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1054 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1055 } else {
1056 int error = zfs_destroy_snaps_nvl(g_zfs,
1057 cb->cb_batchedsnaps, B_FALSE);
1058 fnvlist_free(cb->cb_batchedsnaps);
1059 cb->cb_batchedsnaps = fnvlist_alloc();
1060
1061 if (error != 0 ||
1062 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1063 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1064 zfs_close(zhp);
1065 return (-1);
1066 }
1067 }
1068
1069 zfs_close(zhp);
1070 return (0);
1071 }
1072
1073 static int
1074 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1075 {
1076 destroy_cbdata_t *cb = arg;
1077 const char *name = zfs_get_name(zhp);
1078 int err = 0;
1079
1080 if (nvlist_exists(cb->cb_nvl, name)) {
1081 if (cb->cb_firstsnap == NULL)
1082 cb->cb_firstsnap = strdup(name);
1083 if (cb->cb_prevsnap != NULL)
1084 free(cb->cb_prevsnap);
1085 /* this snap continues the current range */
1086 cb->cb_prevsnap = strdup(name);
1087 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1088 nomem();
1089 if (cb->cb_verbose) {
1090 if (cb->cb_parsable) {
1091 (void) printf("destroy\t%s\n", name);
1092 } else if (cb->cb_dryrun) {
1093 (void) printf(gettext("would destroy %s\n"),
1094 name);
1095 } else {
1096 (void) printf(gettext("will destroy %s\n"),
1097 name);
1098 }
1099 }
1100 } else if (cb->cb_firstsnap != NULL) {
1101 /* end of this range */
1102 uint64_t used = 0;
1103 err = lzc_snaprange_space(cb->cb_firstsnap,
1104 cb->cb_prevsnap, &used);
1105 cb->cb_snapused += used;
1106 free(cb->cb_firstsnap);
1107 cb->cb_firstsnap = NULL;
1108 free(cb->cb_prevsnap);
1109 cb->cb_prevsnap = NULL;
1110 }
1111 zfs_close(zhp);
1112 return (err);
1113 }
1114
1115 static int
1116 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1117 {
1118 int err;
1119 assert(cb->cb_firstsnap == NULL);
1120 assert(cb->cb_prevsnap == NULL);
1121 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1122 if (cb->cb_firstsnap != NULL) {
1123 uint64_t used = 0;
1124 if (err == 0) {
1125 err = lzc_snaprange_space(cb->cb_firstsnap,
1126 cb->cb_prevsnap, &used);
1127 }
1128 cb->cb_snapused += used;
1129 free(cb->cb_firstsnap);
1130 cb->cb_firstsnap = NULL;
1131 free(cb->cb_prevsnap);
1132 cb->cb_prevsnap = NULL;
1133 }
1134 return (err);
1135 }
1136
1137 static int
1138 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1139 {
1140 destroy_cbdata_t *cb = arg;
1141 int err = 0;
1142
1143 /* Check for clones. */
1144 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1145 cb->cb_target = zhp;
1146 cb->cb_first = B_TRUE;
1147 err = zfs_iter_dependents(zhp, B_TRUE,
1148 destroy_check_dependent, cb);
1149 }
1150
1151 if (err == 0) {
1152 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1153 nomem();
1154 }
1155 zfs_close(zhp);
1156 return (err);
1157 }
1158
1159 static int
1160 gather_snapshots(zfs_handle_t *zhp, void *arg)
1161 {
1162 destroy_cbdata_t *cb = arg;
1163 int err = 0;
1164
1165 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1166 if (err == ENOENT)
1167 err = 0;
1168 if (err != 0)
1169 goto out;
1170
1171 if (cb->cb_verbose) {
1172 err = destroy_print_snapshots(zhp, cb);
1173 if (err != 0)
1174 goto out;
1175 }
1176
1177 if (cb->cb_recurse)
1178 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1179
1180 out:
1181 zfs_close(zhp);
1182 return (err);
1183 }
1184
1185 static int
1186 destroy_clones(destroy_cbdata_t *cb)
1187 {
1188 nvpair_t *pair;
1189 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1190 pair != NULL;
1191 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1192 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1193 ZFS_TYPE_SNAPSHOT);
1194 if (zhp != NULL) {
1195 boolean_t defer = cb->cb_defer_destroy;
1196 int err;
1197
1198 /*
1199 * We can't defer destroy non-snapshots, so set it to
1200 * false while destroying the clones.
1201 */
1202 cb->cb_defer_destroy = B_FALSE;
1203 err = zfs_iter_dependents(zhp, B_FALSE,
1204 destroy_callback, cb);
1205 cb->cb_defer_destroy = defer;
1206 zfs_close(zhp);
1207 if (err != 0)
1208 return (err);
1209 }
1210 }
1211 return (0);
1212 }
1213
1214 static int
1215 zfs_do_destroy(int argc, char **argv)
1216 {
1217 destroy_cbdata_t cb = { 0 };
1218 int rv = 0;
1219 int err = 0;
1220 int c;
1221 zfs_handle_t *zhp = NULL;
1222 char *at, *pound;
1223 zfs_type_t type = ZFS_TYPE_DATASET;
1224
1225 /* check options */
1226 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1227 switch (c) {
1228 case 'v':
1229 cb.cb_verbose = B_TRUE;
1230 break;
1231 case 'p':
1232 cb.cb_verbose = B_TRUE;
1233 cb.cb_parsable = B_TRUE;
1234 break;
1235 case 'n':
1236 cb.cb_dryrun = B_TRUE;
1237 break;
1238 case 'd':
1239 cb.cb_defer_destroy = B_TRUE;
1240 type = ZFS_TYPE_SNAPSHOT;
1241 break;
1242 case 'f':
1243 cb.cb_force = B_TRUE;
1244 break;
1245 case 'r':
1246 cb.cb_recurse = B_TRUE;
1247 break;
1248 case 'R':
1249 cb.cb_recurse = B_TRUE;
1250 cb.cb_doclones = B_TRUE;
1251 break;
1252 case '?':
1253 default:
1254 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1255 optopt);
1256 usage(B_FALSE);
1257 }
1258 }
1259
1260 argc -= optind;
1261 argv += optind;
1262
1263 /* check number of arguments */
1264 if (argc == 0) {
1265 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1266 usage(B_FALSE);
1267 }
1268 if (argc > 1) {
1269 (void) fprintf(stderr, gettext("too many arguments\n"));
1270 usage(B_FALSE);
1271 }
1272
1273 at = strchr(argv[0], '@');
1274 pound = strchr(argv[0], '#');
1275 if (at != NULL) {
1276
1277 /* Build the list of snaps to destroy in cb_nvl. */
1278 cb.cb_nvl = fnvlist_alloc();
1279
1280 *at = '\0';
1281 zhp = zfs_open(g_zfs, argv[0],
1282 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1283 if (zhp == NULL)
1284 return (1);
1285
1286 cb.cb_snapspec = at + 1;
1287 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1288 cb.cb_error) {
1289 rv = 1;
1290 goto out;
1291 }
1292
1293 if (nvlist_empty(cb.cb_nvl)) {
1294 (void) fprintf(stderr, gettext("could not find any "
1295 "snapshots to destroy; check snapshot names.\n"));
1296 rv = 1;
1297 goto out;
1298 }
1299
1300 if (cb.cb_verbose) {
1301 char buf[16];
1302 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1303 if (cb.cb_parsable) {
1304 (void) printf("reclaim\t%llu\n",
1305 (u_longlong_t)cb.cb_snapused);
1306 } else if (cb.cb_dryrun) {
1307 (void) printf(gettext("would reclaim %s\n"),
1308 buf);
1309 } else {
1310 (void) printf(gettext("will reclaim %s\n"),
1311 buf);
1312 }
1313 }
1314
1315 if (!cb.cb_dryrun) {
1316 if (cb.cb_doclones) {
1317 cb.cb_batchedsnaps = fnvlist_alloc();
1318 err = destroy_clones(&cb);
1319 if (err == 0) {
1320 err = zfs_destroy_snaps_nvl(g_zfs,
1321 cb.cb_batchedsnaps, B_FALSE);
1322 }
1323 if (err != 0) {
1324 rv = 1;
1325 goto out;
1326 }
1327 }
1328 if (err == 0) {
1329 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1330 cb.cb_defer_destroy);
1331 }
1332 }
1333
1334 if (err != 0)
1335 rv = 1;
1336 } else if (pound != NULL) {
1337 int err;
1338 nvlist_t *nvl;
1339
1340 if (cb.cb_dryrun) {
1341 (void) fprintf(stderr,
1342 "dryrun is not supported with bookmark\n");
1343 return (-1);
1344 }
1345
1346 if (cb.cb_defer_destroy) {
1347 (void) fprintf(stderr,
1348 "defer destroy is not supported with bookmark\n");
1349 return (-1);
1350 }
1351
1352 if (cb.cb_recurse) {
1353 (void) fprintf(stderr,
1354 "recursive is not supported with bookmark\n");
1355 return (-1);
1356 }
1357
1358 if (!zfs_bookmark_exists(argv[0])) {
1359 (void) fprintf(stderr, gettext("bookmark '%s' "
1360 "does not exist.\n"), argv[0]);
1361 return (1);
1362 }
1363
1364 nvl = fnvlist_alloc();
1365 fnvlist_add_boolean(nvl, argv[0]);
1366
1367 err = lzc_destroy_bookmarks(nvl, NULL);
1368 if (err != 0) {
1369 (void) zfs_standard_error(g_zfs, err,
1370 "cannot destroy bookmark");
1371 }
1372
1373 nvlist_free(cb.cb_nvl);
1374
1375 return (err);
1376 } else {
1377 /* Open the given dataset */
1378 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1379 return (1);
1380
1381 cb.cb_target = zhp;
1382
1383 /*
1384 * Perform an explicit check for pools before going any further.
1385 */
1386 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1387 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1388 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1389 "operation does not apply to pools\n"),
1390 zfs_get_name(zhp));
1391 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1392 "%s' to destroy all datasets in the pool\n"),
1393 zfs_get_name(zhp));
1394 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1395 "to destroy the pool itself\n"), zfs_get_name(zhp));
1396 rv = 1;
1397 goto out;
1398 }
1399
1400 /*
1401 * Check for any dependents and/or clones.
1402 */
1403 cb.cb_first = B_TRUE;
1404 if (!cb.cb_doclones &&
1405 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1406 &cb) != 0) {
1407 rv = 1;
1408 goto out;
1409 }
1410
1411 if (cb.cb_error) {
1412 rv = 1;
1413 goto out;
1414 }
1415
1416 cb.cb_batchedsnaps = fnvlist_alloc();
1417 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1418 &cb) != 0) {
1419 rv = 1;
1420 goto out;
1421 }
1422
1423 /*
1424 * Do the real thing. The callback will close the
1425 * handle regardless of whether it succeeds or not.
1426 */
1427 err = destroy_callback(zhp, &cb);
1428 zhp = NULL;
1429 if (err == 0) {
1430 err = zfs_destroy_snaps_nvl(g_zfs,
1431 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1432 }
1433 if (err != 0)
1434 rv = 1;
1435 }
1436
1437 out:
1438 fnvlist_free(cb.cb_batchedsnaps);
1439 fnvlist_free(cb.cb_nvl);
1440 if (zhp != NULL)
1441 zfs_close(zhp);
1442 return (rv);
1443 }
1444
1445 static boolean_t
1446 is_recvd_column(zprop_get_cbdata_t *cbp)
1447 {
1448 int i;
1449 zfs_get_column_t col;
1450
1451 for (i = 0; i < ZFS_GET_NCOLS &&
1452 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1453 if (col == GET_COL_RECVD)
1454 return (B_TRUE);
1455 return (B_FALSE);
1456 }
1457
1458 /*
1459 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1460 * < all | property[,property]... > < fs | snap | vol > ...
1461 *
1462 * -r recurse over any child datasets
1463 * -H scripted mode. Headers are stripped, and fields are separated
1464 * by tabs instead of spaces.
1465 * -o Set of fields to display. One of "name,property,value,
1466 * received,source". Default is "name,property,value,source".
1467 * "all" is an alias for all five.
1468 * -s Set of sources to allow. One of
1469 * "local,default,inherited,received,temporary,none". Default is
1470 * all six.
1471 * -p Display values in parsable (literal) format.
1472 *
1473 * Prints properties for the given datasets. The user can control which
1474 * columns to display as well as which property types to allow.
1475 */
1476
1477 /*
1478 * Invoked to display the properties for a single dataset.
1479 */
1480 static int
1481 get_callback(zfs_handle_t *zhp, void *data)
1482 {
1483 char buf[ZFS_MAXPROPLEN];
1484 char rbuf[ZFS_MAXPROPLEN];
1485 zprop_source_t sourcetype;
1486 char source[ZFS_MAXNAMELEN];
1487 zprop_get_cbdata_t *cbp = data;
1488 nvlist_t *user_props = zfs_get_user_props(zhp);
1489 zprop_list_t *pl = cbp->cb_proplist;
1490 nvlist_t *propval;
1491 char *strval;
1492 char *sourceval;
1493 boolean_t received = is_recvd_column(cbp);
1494
1495 for (; pl != NULL; pl = pl->pl_next) {
1496 char *recvdval = NULL;
1497 /*
1498 * Skip the special fake placeholder. This will also skip over
1499 * the name property when 'all' is specified.
1500 */
1501 if (pl->pl_prop == ZFS_PROP_NAME &&
1502 pl == cbp->cb_proplist)
1503 continue;
1504
1505 if (pl->pl_prop != ZPROP_INVAL) {
1506 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1507 sizeof (buf), &sourcetype, source,
1508 sizeof (source),
1509 cbp->cb_literal) != 0) {
1510 if (pl->pl_all)
1511 continue;
1512 if (!zfs_prop_valid_for_type(pl->pl_prop,
1513 ZFS_TYPE_DATASET, B_FALSE)) {
1514 (void) fprintf(stderr,
1515 gettext("No such property '%s'\n"),
1516 zfs_prop_to_name(pl->pl_prop));
1517 continue;
1518 }
1519 sourcetype = ZPROP_SRC_NONE;
1520 (void) strlcpy(buf, "-", sizeof (buf));
1521 }
1522
1523 if (received && (zfs_prop_get_recvd(zhp,
1524 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1525 cbp->cb_literal) == 0))
1526 recvdval = rbuf;
1527
1528 zprop_print_one_property(zfs_get_name(zhp), cbp,
1529 zfs_prop_to_name(pl->pl_prop),
1530 buf, sourcetype, source, recvdval);
1531 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1532 sourcetype = ZPROP_SRC_LOCAL;
1533
1534 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1535 buf, sizeof (buf), cbp->cb_literal) != 0) {
1536 sourcetype = ZPROP_SRC_NONE;
1537 (void) strlcpy(buf, "-", sizeof (buf));
1538 }
1539
1540 zprop_print_one_property(zfs_get_name(zhp), cbp,
1541 pl->pl_user_prop, buf, sourcetype, source, NULL);
1542 } else if (zfs_prop_written(pl->pl_user_prop)) {
1543 sourcetype = ZPROP_SRC_LOCAL;
1544
1545 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1546 buf, sizeof (buf), cbp->cb_literal) != 0) {
1547 sourcetype = ZPROP_SRC_NONE;
1548 (void) strlcpy(buf, "-", sizeof (buf));
1549 }
1550
1551 zprop_print_one_property(zfs_get_name(zhp), cbp,
1552 pl->pl_user_prop, buf, sourcetype, source, NULL);
1553 } else {
1554 if (nvlist_lookup_nvlist(user_props,
1555 pl->pl_user_prop, &propval) != 0) {
1556 if (pl->pl_all)
1557 continue;
1558 sourcetype = ZPROP_SRC_NONE;
1559 strval = "-";
1560 } else {
1561 verify(nvlist_lookup_string(propval,
1562 ZPROP_VALUE, &strval) == 0);
1563 verify(nvlist_lookup_string(propval,
1564 ZPROP_SOURCE, &sourceval) == 0);
1565
1566 if (strcmp(sourceval,
1567 zfs_get_name(zhp)) == 0) {
1568 sourcetype = ZPROP_SRC_LOCAL;
1569 } else if (strcmp(sourceval,
1570 ZPROP_SOURCE_VAL_RECVD) == 0) {
1571 sourcetype = ZPROP_SRC_RECEIVED;
1572 } else {
1573 sourcetype = ZPROP_SRC_INHERITED;
1574 (void) strlcpy(source,
1575 sourceval, sizeof (source));
1576 }
1577 }
1578
1579 if (received && (zfs_prop_get_recvd(zhp,
1580 pl->pl_user_prop, rbuf, sizeof (rbuf),
1581 cbp->cb_literal) == 0))
1582 recvdval = rbuf;
1583
1584 zprop_print_one_property(zfs_get_name(zhp), cbp,
1585 pl->pl_user_prop, strval, sourcetype,
1586 source, recvdval);
1587 }
1588 }
1589
1590 return (0);
1591 }
1592
1593 static int
1594 zfs_do_get(int argc, char **argv)
1595 {
1596 zprop_get_cbdata_t cb = { 0 };
1597 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1598 int types = ZFS_TYPE_DATASET;
1599 char *value, *fields;
1600 int ret = 0;
1601 int limit = 0;
1602 zprop_list_t fake_name = { 0 };
1603
1604 /*
1605 * Set up default columns and sources.
1606 */
1607 cb.cb_sources = ZPROP_SRC_ALL;
1608 cb.cb_columns[0] = GET_COL_NAME;
1609 cb.cb_columns[1] = GET_COL_PROPERTY;
1610 cb.cb_columns[2] = GET_COL_VALUE;
1611 cb.cb_columns[3] = GET_COL_SOURCE;
1612 cb.cb_type = ZFS_TYPE_DATASET;
1613
1614 /* check options */
1615 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1616 switch (c) {
1617 case 'p':
1618 cb.cb_literal = B_TRUE;
1619 break;
1620 case 'd':
1621 limit = parse_depth(optarg, &flags);
1622 break;
1623 case 'r':
1624 flags |= ZFS_ITER_RECURSE;
1625 break;
1626 case 'H':
1627 cb.cb_scripted = B_TRUE;
1628 break;
1629 case ':':
1630 (void) fprintf(stderr, gettext("missing argument for "
1631 "'%c' option\n"), optopt);
1632 usage(B_FALSE);
1633 break;
1634 case 'o':
1635 /*
1636 * Process the set of columns to display. We zero out
1637 * the structure to give us a blank slate.
1638 */
1639 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1640 i = 0;
1641 while (*optarg != '\0') {
1642 static char *col_subopts[] =
1643 { "name", "property", "value", "received",
1644 "source", "all", NULL };
1645
1646 if (i == ZFS_GET_NCOLS) {
1647 (void) fprintf(stderr, gettext("too "
1648 "many fields given to -o "
1649 "option\n"));
1650 usage(B_FALSE);
1651 }
1652
1653 switch (getsubopt(&optarg, col_subopts,
1654 &value)) {
1655 case 0:
1656 cb.cb_columns[i++] = GET_COL_NAME;
1657 break;
1658 case 1:
1659 cb.cb_columns[i++] = GET_COL_PROPERTY;
1660 break;
1661 case 2:
1662 cb.cb_columns[i++] = GET_COL_VALUE;
1663 break;
1664 case 3:
1665 cb.cb_columns[i++] = GET_COL_RECVD;
1666 flags |= ZFS_ITER_RECVD_PROPS;
1667 break;
1668 case 4:
1669 cb.cb_columns[i++] = GET_COL_SOURCE;
1670 break;
1671 case 5:
1672 if (i > 0) {
1673 (void) fprintf(stderr,
1674 gettext("\"all\" conflicts "
1675 "with specific fields "
1676 "given to -o option\n"));
1677 usage(B_FALSE);
1678 }
1679 cb.cb_columns[0] = GET_COL_NAME;
1680 cb.cb_columns[1] = GET_COL_PROPERTY;
1681 cb.cb_columns[2] = GET_COL_VALUE;
1682 cb.cb_columns[3] = GET_COL_RECVD;
1683 cb.cb_columns[4] = GET_COL_SOURCE;
1684 flags |= ZFS_ITER_RECVD_PROPS;
1685 i = ZFS_GET_NCOLS;
1686 break;
1687 default:
1688 (void) fprintf(stderr,
1689 gettext("invalid column name "
1690 "'%s'\n"), value);
1691 usage(B_FALSE);
1692 }
1693 }
1694 break;
1695
1696 case 's':
1697 cb.cb_sources = 0;
1698 while (*optarg != '\0') {
1699 static char *source_subopts[] = {
1700 "local", "default", "inherited",
1701 "received", "temporary", "none",
1702 NULL };
1703
1704 switch (getsubopt(&optarg, source_subopts,
1705 &value)) {
1706 case 0:
1707 cb.cb_sources |= ZPROP_SRC_LOCAL;
1708 break;
1709 case 1:
1710 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1711 break;
1712 case 2:
1713 cb.cb_sources |= ZPROP_SRC_INHERITED;
1714 break;
1715 case 3:
1716 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1717 break;
1718 case 4:
1719 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1720 break;
1721 case 5:
1722 cb.cb_sources |= ZPROP_SRC_NONE;
1723 break;
1724 default:
1725 (void) fprintf(stderr,
1726 gettext("invalid source "
1727 "'%s'\n"), value);
1728 usage(B_FALSE);
1729 }
1730 }
1731 break;
1732
1733 case 't':
1734 types = 0;
1735 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1736 while (*optarg != '\0') {
1737 static char *type_subopts[] = { "filesystem",
1738 "volume", "snapshot", "bookmark",
1739 "all", NULL };
1740
1741 switch (getsubopt(&optarg, type_subopts,
1742 &value)) {
1743 case 0:
1744 types |= ZFS_TYPE_FILESYSTEM;
1745 break;
1746 case 1:
1747 types |= ZFS_TYPE_VOLUME;
1748 break;
1749 case 2:
1750 types |= ZFS_TYPE_SNAPSHOT;
1751 break;
1752 case 3:
1753 types |= ZFS_TYPE_BOOKMARK;
1754 break;
1755 case 4:
1756 types = ZFS_TYPE_DATASET |
1757 ZFS_TYPE_BOOKMARK;
1758 break;
1759
1760 default:
1761 (void) fprintf(stderr,
1762 gettext("invalid type '%s'\n"),
1763 value);
1764 usage(B_FALSE);
1765 }
1766 }
1767 break;
1768
1769 case '?':
1770 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1771 optopt);
1772 usage(B_FALSE);
1773 }
1774 }
1775
1776 argc -= optind;
1777 argv += optind;
1778
1779 if (argc < 1) {
1780 (void) fprintf(stderr, gettext("missing property "
1781 "argument\n"));
1782 usage(B_FALSE);
1783 }
1784
1785 fields = argv[0];
1786
1787 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1788 != 0)
1789 usage(B_FALSE);
1790
1791 argc--;
1792 argv++;
1793
1794 /*
1795 * As part of zfs_expand_proplist(), we keep track of the maximum column
1796 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1797 * need to know the maximum name length. However, the user likely did
1798 * not specify 'name' as one of the properties to fetch, so we need to
1799 * make sure we always include at least this property for
1800 * print_get_headers() to work properly.
1801 */
1802 if (cb.cb_proplist != NULL) {
1803 fake_name.pl_prop = ZFS_PROP_NAME;
1804 fake_name.pl_width = strlen(gettext("NAME"));
1805 fake_name.pl_next = cb.cb_proplist;
1806 cb.cb_proplist = &fake_name;
1807 }
1808
1809 cb.cb_first = B_TRUE;
1810
1811 /* run for each object */
1812 ret = zfs_for_each(argc, argv, flags, types, NULL,
1813 &cb.cb_proplist, limit, get_callback, &cb);
1814
1815 if (cb.cb_proplist == &fake_name)
1816 zprop_free_list(fake_name.pl_next);
1817 else
1818 zprop_free_list(cb.cb_proplist);
1819
1820 return (ret);
1821 }
1822
1823 /*
1824 * inherit [-rS] <property> <fs|vol> ...
1825 *
1826 * -r Recurse over all children
1827 * -S Revert to received value, if any
1828 *
1829 * For each dataset specified on the command line, inherit the given property
1830 * from its parent. Inheriting a property at the pool level will cause it to
1831 * use the default value. The '-r' flag will recurse over all children, and is
1832 * useful for setting a property on a hierarchy-wide basis, regardless of any
1833 * local modifications for each dataset.
1834 */
1835
1836 typedef struct inherit_cbdata {
1837 const char *cb_propname;
1838 boolean_t cb_received;
1839 } inherit_cbdata_t;
1840
1841 static int
1842 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1843 {
1844 inherit_cbdata_t *cb = data;
1845 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1846
1847 /*
1848 * If we're doing it recursively, then ignore properties that
1849 * are not valid for this type of dataset.
1850 */
1851 if (prop != ZPROP_INVAL &&
1852 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
1853 return (0);
1854
1855 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1856 }
1857
1858 static int
1859 inherit_cb(zfs_handle_t *zhp, void *data)
1860 {
1861 inherit_cbdata_t *cb = data;
1862
1863 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1864 }
1865
1866 static int
1867 zfs_do_inherit(int argc, char **argv)
1868 {
1869 int c;
1870 zfs_prop_t prop;
1871 inherit_cbdata_t cb = { 0 };
1872 char *propname;
1873 int ret = 0;
1874 int flags = 0;
1875 boolean_t received = B_FALSE;
1876
1877 /* check options */
1878 while ((c = getopt(argc, argv, "rS")) != -1) {
1879 switch (c) {
1880 case 'r':
1881 flags |= ZFS_ITER_RECURSE;
1882 break;
1883 case 'S':
1884 received = B_TRUE;
1885 break;
1886 case '?':
1887 default:
1888 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1889 optopt);
1890 usage(B_FALSE);
1891 }
1892 }
1893
1894 argc -= optind;
1895 argv += optind;
1896
1897 /* check number of arguments */
1898 if (argc < 1) {
1899 (void) fprintf(stderr, gettext("missing property argument\n"));
1900 usage(B_FALSE);
1901 }
1902 if (argc < 2) {
1903 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1904 usage(B_FALSE);
1905 }
1906
1907 propname = argv[0];
1908 argc--;
1909 argv++;
1910
1911 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1912 if (zfs_prop_readonly(prop)) {
1913 (void) fprintf(stderr, gettext(
1914 "%s property is read-only\n"),
1915 propname);
1916 return (1);
1917 }
1918 if (!zfs_prop_inheritable(prop) && !received) {
1919 (void) fprintf(stderr, gettext("'%s' property cannot "
1920 "be inherited\n"), propname);
1921 if (prop == ZFS_PROP_QUOTA ||
1922 prop == ZFS_PROP_RESERVATION ||
1923 prop == ZFS_PROP_REFQUOTA ||
1924 prop == ZFS_PROP_REFRESERVATION) {
1925 (void) fprintf(stderr, gettext("use 'zfs set "
1926 "%s=none' to clear\n"), propname);
1927 (void) fprintf(stderr, gettext("use 'zfs "
1928 "inherit -S %s' to revert to received "
1929 "value\n"), propname);
1930 }
1931 return (1);
1932 }
1933 if (received && (prop == ZFS_PROP_VOLSIZE ||
1934 prop == ZFS_PROP_VERSION)) {
1935 (void) fprintf(stderr, gettext("'%s' property cannot "
1936 "be reverted to a received value\n"), propname);
1937 return (1);
1938 }
1939 } else if (!zfs_prop_user(propname)) {
1940 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1941 propname);
1942 usage(B_FALSE);
1943 }
1944
1945 cb.cb_propname = propname;
1946 cb.cb_received = received;
1947
1948 if (flags & ZFS_ITER_RECURSE) {
1949 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1950 NULL, NULL, 0, inherit_recurse_cb, &cb);
1951 } else {
1952 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1953 NULL, NULL, 0, inherit_cb, &cb);
1954 }
1955
1956 return (ret);
1957 }
1958
1959 typedef struct upgrade_cbdata {
1960 uint64_t cb_numupgraded;
1961 uint64_t cb_numsamegraded;
1962 uint64_t cb_numfailed;
1963 uint64_t cb_version;
1964 boolean_t cb_newer;
1965 boolean_t cb_foundone;
1966 char cb_lastfs[ZFS_MAXNAMELEN];
1967 } upgrade_cbdata_t;
1968
1969 static int
1970 same_pool(zfs_handle_t *zhp, const char *name)
1971 {
1972 int len1 = strcspn(name, "/@");
1973 const char *zhname = zfs_get_name(zhp);
1974 int len2 = strcspn(zhname, "/@");
1975
1976 if (len1 != len2)
1977 return (B_FALSE);
1978 return (strncmp(name, zhname, len1) == 0);
1979 }
1980
1981 static int
1982 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1983 {
1984 upgrade_cbdata_t *cb = data;
1985 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1986
1987 /* list if it's old/new */
1988 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1989 (cb->cb_newer && version > ZPL_VERSION)) {
1990 char *str;
1991 if (cb->cb_newer) {
1992 str = gettext("The following filesystems are "
1993 "formatted using a newer software version and\n"
1994 "cannot be accessed on the current system.\n\n");
1995 } else {
1996 str = gettext("The following filesystems are "
1997 "out of date, and can be upgraded. After being\n"
1998 "upgraded, these filesystems (and any 'zfs send' "
1999 "streams generated from\n"
2000 "subsequent snapshots) will no longer be "
2001 "accessible by older software versions.\n\n");
2002 }
2003
2004 if (!cb->cb_foundone) {
2005 (void) puts(str);
2006 (void) printf(gettext("VER FILESYSTEM\n"));
2007 (void) printf(gettext("--- ------------\n"));
2008 cb->cb_foundone = B_TRUE;
2009 }
2010
2011 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2012 }
2013
2014 return (0);
2015 }
2016
2017 static int
2018 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2019 {
2020 upgrade_cbdata_t *cb = data;
2021 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2022 int needed_spa_version;
2023 int spa_version;
2024
2025 if (zfs_spa_version(zhp, &spa_version) < 0)
2026 return (-1);
2027
2028 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2029
2030 if (needed_spa_version < 0)
2031 return (-1);
2032
2033 if (spa_version < needed_spa_version) {
2034 /* can't upgrade */
2035 (void) printf(gettext("%s: can not be "
2036 "upgraded; the pool version needs to first "
2037 "be upgraded\nto version %d\n\n"),
2038 zfs_get_name(zhp), needed_spa_version);
2039 cb->cb_numfailed++;
2040 return (0);
2041 }
2042
2043 /* upgrade */
2044 if (version < cb->cb_version) {
2045 char verstr[16];
2046 (void) snprintf(verstr, sizeof (verstr),
2047 "%llu", (u_longlong_t)cb->cb_version);
2048 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2049 /*
2050 * If they did "zfs upgrade -a", then we could
2051 * be doing ioctls to different pools. We need
2052 * to log this history once to each pool, and bypass
2053 * the normal history logging that happens in main().
2054 */
2055 (void) zpool_log_history(g_zfs, history_str);
2056 log_history = B_FALSE;
2057 }
2058 if (zfs_prop_set(zhp, "version", verstr) == 0)
2059 cb->cb_numupgraded++;
2060 else
2061 cb->cb_numfailed++;
2062 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2063 } else if (version > cb->cb_version) {
2064 /* can't downgrade */
2065 (void) printf(gettext("%s: can not be downgraded; "
2066 "it is already at version %u\n"),
2067 zfs_get_name(zhp), version);
2068 cb->cb_numfailed++;
2069 } else {
2070 cb->cb_numsamegraded++;
2071 }
2072 return (0);
2073 }
2074
2075 /*
2076 * zfs upgrade
2077 * zfs upgrade -v
2078 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2079 */
2080 static int
2081 zfs_do_upgrade(int argc, char **argv)
2082 {
2083 boolean_t all = B_FALSE;
2084 boolean_t showversions = B_FALSE;
2085 int ret = 0;
2086 upgrade_cbdata_t cb = { 0 };
2087 signed char c;
2088 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2089
2090 /* check options */
2091 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2092 switch (c) {
2093 case 'r':
2094 flags |= ZFS_ITER_RECURSE;
2095 break;
2096 case 'v':
2097 showversions = B_TRUE;
2098 break;
2099 case 'V':
2100 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2101 optarg, &cb.cb_version) != 0) {
2102 (void) fprintf(stderr,
2103 gettext("invalid version %s\n"), optarg);
2104 usage(B_FALSE);
2105 }
2106 break;
2107 case 'a':
2108 all = B_TRUE;
2109 break;
2110 case '?':
2111 default:
2112 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2113 optopt);
2114 usage(B_FALSE);
2115 }
2116 }
2117
2118 argc -= optind;
2119 argv += optind;
2120
2121 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2122 usage(B_FALSE);
2123 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2124 cb.cb_version || argc))
2125 usage(B_FALSE);
2126 if ((all || argc) && (showversions))
2127 usage(B_FALSE);
2128 if (all && argc)
2129 usage(B_FALSE);
2130
2131 if (showversions) {
2132 /* Show info on available versions. */
2133 (void) printf(gettext("The following filesystem versions are "
2134 "supported:\n\n"));
2135 (void) printf(gettext("VER DESCRIPTION\n"));
2136 (void) printf("--- -----------------------------------------"
2137 "---------------\n");
2138 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2139 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2140 (void) printf(gettext(" 3 Case insensitive and filesystem "
2141 "user identifier (FUID)\n"));
2142 (void) printf(gettext(" 4 userquota, groupquota "
2143 "properties\n"));
2144 (void) printf(gettext(" 5 System attributes\n"));
2145 (void) printf(gettext("\nFor more information on a particular "
2146 "version, including supported releases,\n"));
2147 (void) printf("see the ZFS Administration Guide.\n\n");
2148 ret = 0;
2149 } else if (argc || all) {
2150 /* Upgrade filesystems */
2151 if (cb.cb_version == 0)
2152 cb.cb_version = ZPL_VERSION;
2153 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2154 NULL, NULL, 0, upgrade_set_callback, &cb);
2155 (void) printf(gettext("%llu filesystems upgraded\n"),
2156 (u_longlong_t)cb.cb_numupgraded);
2157 if (cb.cb_numsamegraded) {
2158 (void) printf(gettext("%llu filesystems already at "
2159 "this version\n"),
2160 (u_longlong_t)cb.cb_numsamegraded);
2161 }
2162 if (cb.cb_numfailed != 0)
2163 ret = 1;
2164 } else {
2165 /* List old-version filesytems */
2166 boolean_t found;
2167 (void) printf(gettext("This system is currently running "
2168 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2169
2170 flags |= ZFS_ITER_RECURSE;
2171 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2172 NULL, NULL, 0, upgrade_list_callback, &cb);
2173
2174 found = cb.cb_foundone;
2175 cb.cb_foundone = B_FALSE;
2176 cb.cb_newer = B_TRUE;
2177
2178 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2179 NULL, NULL, 0, upgrade_list_callback, &cb);
2180
2181 if (!cb.cb_foundone && !found) {
2182 (void) printf(gettext("All filesystems are "
2183 "formatted with the current version.\n"));
2184 }
2185 }
2186
2187 return (ret);
2188 }
2189
2190 /*
2191 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2192 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2193 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2194 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2195 *
2196 * -H Scripted mode; elide headers and separate columns by tabs.
2197 * -i Translate SID to POSIX ID.
2198 * -n Print numeric ID instead of user/group name.
2199 * -o Control which fields to display.
2200 * -p Use exact (parsable) numeric output.
2201 * -s Specify sort columns, descending order.
2202 * -S Specify sort columns, ascending order.
2203 * -t Control which object types to display.
2204 *
2205 * Displays space consumed by, and quotas on, each user in the specified
2206 * filesystem or snapshot.
2207 */
2208
2209 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2210 enum us_field_types {
2211 USFIELD_TYPE,
2212 USFIELD_NAME,
2213 USFIELD_USED,
2214 USFIELD_QUOTA
2215 };
2216 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2217 static char *us_field_names[] = { "type", "name", "used", "quota" };
2218 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2219
2220 #define USTYPE_PSX_GRP (1 << 0)
2221 #define USTYPE_PSX_USR (1 << 1)
2222 #define USTYPE_SMB_GRP (1 << 2)
2223 #define USTYPE_SMB_USR (1 << 3)
2224 #define USTYPE_ALL \
2225 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2226
2227 static int us_type_bits[] = {
2228 USTYPE_PSX_GRP,
2229 USTYPE_PSX_USR,
2230 USTYPE_SMB_GRP,
2231 USTYPE_SMB_USR,
2232 USTYPE_ALL
2233 };
2234 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2235 "smbuser", "all" };
2236
2237 typedef struct us_node {
2238 nvlist_t *usn_nvl;
2239 uu_avl_node_t usn_avlnode;
2240 uu_list_node_t usn_listnode;
2241 } us_node_t;
2242
2243 typedef struct us_cbdata {
2244 nvlist_t **cb_nvlp;
2245 uu_avl_pool_t *cb_avl_pool;
2246 uu_avl_t *cb_avl;
2247 boolean_t cb_numname;
2248 boolean_t cb_nicenum;
2249 boolean_t cb_sid2posix;
2250 zfs_userquota_prop_t cb_prop;
2251 zfs_sort_column_t *cb_sortcol;
2252 size_t cb_width[USFIELD_LAST];
2253 } us_cbdata_t;
2254
2255 static boolean_t us_populated = B_FALSE;
2256
2257 typedef struct {
2258 zfs_sort_column_t *si_sortcol;
2259 boolean_t si_numname;
2260 } us_sort_info_t;
2261
2262 static int
2263 us_field_index(char *field)
2264 {
2265 int i;
2266
2267 for (i = 0; i < USFIELD_LAST; i++) {
2268 if (strcmp(field, us_field_names[i]) == 0)
2269 return (i);
2270 }
2271
2272 return (-1);
2273 }
2274
2275 static int
2276 us_compare(const void *larg, const void *rarg, void *unused)
2277 {
2278 const us_node_t *l = larg;
2279 const us_node_t *r = rarg;
2280 us_sort_info_t *si = (us_sort_info_t *)unused;
2281 zfs_sort_column_t *sortcol = si->si_sortcol;
2282 boolean_t numname = si->si_numname;
2283 nvlist_t *lnvl = l->usn_nvl;
2284 nvlist_t *rnvl = r->usn_nvl;
2285 int rc = 0;
2286 boolean_t lvb, rvb;
2287
2288 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2289 char *lvstr = "";
2290 char *rvstr = "";
2291 uint32_t lv32 = 0;
2292 uint32_t rv32 = 0;
2293 uint64_t lv64 = 0;
2294 uint64_t rv64 = 0;
2295 zfs_prop_t prop = sortcol->sc_prop;
2296 const char *propname = NULL;
2297 boolean_t reverse = sortcol->sc_reverse;
2298
2299 switch (prop) {
2300 case ZFS_PROP_TYPE:
2301 propname = "type";
2302 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2303 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2304 if (rv32 != lv32)
2305 rc = (rv32 < lv32) ? 1 : -1;
2306 break;
2307 case ZFS_PROP_NAME:
2308 propname = "name";
2309 if (numname) {
2310 compare_nums:
2311 (void) nvlist_lookup_uint64(lnvl, propname,
2312 &lv64);
2313 (void) nvlist_lookup_uint64(rnvl, propname,
2314 &rv64);
2315 if (rv64 != lv64)
2316 rc = (rv64 < lv64) ? 1 : -1;
2317 } else {
2318 if ((nvlist_lookup_string(lnvl, propname,
2319 &lvstr) == ENOENT) ||
2320 (nvlist_lookup_string(rnvl, propname,
2321 &rvstr) == ENOENT)) {
2322 goto compare_nums;
2323 }
2324 rc = strcmp(lvstr, rvstr);
2325 }
2326 break;
2327 case ZFS_PROP_USED:
2328 case ZFS_PROP_QUOTA:
2329 if (!us_populated)
2330 break;
2331 if (prop == ZFS_PROP_USED)
2332 propname = "used";
2333 else
2334 propname = "quota";
2335 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2336 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2337 if (rv64 != lv64)
2338 rc = (rv64 < lv64) ? 1 : -1;
2339 break;
2340 default:
2341 break;
2342 }
2343
2344 if (rc != 0) {
2345 if (rc < 0)
2346 return (reverse ? 1 : -1);
2347 else
2348 return (reverse ? -1 : 1);
2349 }
2350 }
2351
2352 /*
2353 * If entries still seem to be the same, check if they are of the same
2354 * type (smbentity is added only if we are doing SID to POSIX ID
2355 * translation where we can have duplicate type/name combinations).
2356 */
2357 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2358 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2359 lvb != rvb)
2360 return (lvb < rvb ? -1 : 1);
2361
2362 return (0);
2363 }
2364
2365 static inline const char *
2366 us_type2str(unsigned field_type)
2367 {
2368 switch (field_type) {
2369 case USTYPE_PSX_USR:
2370 return ("POSIX User");
2371 case USTYPE_PSX_GRP:
2372 return ("POSIX Group");
2373 case USTYPE_SMB_USR:
2374 return ("SMB User");
2375 case USTYPE_SMB_GRP:
2376 return ("SMB Group");
2377 default:
2378 return ("Undefined");
2379 }
2380 }
2381
2382 static int
2383 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2384 {
2385 us_cbdata_t *cb = (us_cbdata_t *)arg;
2386 zfs_userquota_prop_t prop = cb->cb_prop;
2387 char *name = NULL;
2388 char *propname;
2389 char sizebuf[32];
2390 us_node_t *node;
2391 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2392 uu_avl_t *avl = cb->cb_avl;
2393 uu_avl_index_t idx;
2394 nvlist_t *props;
2395 us_node_t *n;
2396 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2397 unsigned type = 0;
2398 const char *typestr;
2399 size_t namelen;
2400 size_t typelen;
2401 size_t sizelen;
2402 int typeidx, nameidx, sizeidx;
2403 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2404 boolean_t smbentity = B_FALSE;
2405
2406 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2407 nomem();
2408 node = safe_malloc(sizeof (us_node_t));
2409 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2410 node->usn_nvl = props;
2411
2412 if (domain != NULL && domain[0] != '\0') {
2413 #ifdef HAVE_IDMAP
2414 /* SMB */
2415 char sid[ZFS_MAXNAMELEN + 32];
2416 uid_t id;
2417 uint64_t classes;
2418 int err;
2419 directory_error_t e;
2420
2421 smbentity = B_TRUE;
2422
2423 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2424
2425 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2426 type = USTYPE_SMB_GRP;
2427 err = sid_to_id(sid, B_FALSE, &id);
2428 } else {
2429 type = USTYPE_SMB_USR;
2430 err = sid_to_id(sid, B_TRUE, &id);
2431 }
2432
2433 if (err == 0) {
2434 rid = id;
2435 if (!cb->cb_sid2posix) {
2436 e = directory_name_from_sid(NULL, sid, &name,
2437 &classes);
2438 if (e != NULL)
2439 directory_error_free(e);
2440 if (name == NULL)
2441 name = sid;
2442 }
2443 }
2444 #else
2445 nvlist_free(props);
2446 free(node);
2447
2448 return (-1);
2449 #endif /* HAVE_IDMAP */
2450 }
2451
2452 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2453 /* POSIX or -i */
2454 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2455 type = USTYPE_PSX_GRP;
2456 if (!cb->cb_numname) {
2457 struct group *g;
2458
2459 if ((g = getgrgid(rid)) != NULL)
2460 name = g->gr_name;
2461 }
2462 } else {
2463 type = USTYPE_PSX_USR;
2464 if (!cb->cb_numname) {
2465 struct passwd *p;
2466
2467 if ((p = getpwuid(rid)) != NULL)
2468 name = p->pw_name;
2469 }
2470 }
2471 }
2472
2473 /*
2474 * Make sure that the type/name combination is unique when doing
2475 * SID to POSIX ID translation (hence changing the type from SMB to
2476 * POSIX).
2477 */
2478 if (cb->cb_sid2posix &&
2479 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2480 nomem();
2481
2482 /* Calculate/update width of TYPE field */
2483 typestr = us_type2str(type);
2484 typelen = strlen(gettext(typestr));
2485 typeidx = us_field_index("type");
2486 if (typelen > cb->cb_width[typeidx])
2487 cb->cb_width[typeidx] = typelen;
2488 if (nvlist_add_uint32(props, "type", type) != 0)
2489 nomem();
2490
2491 /* Calculate/update width of NAME field */
2492 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2493 if (nvlist_add_uint64(props, "name", rid) != 0)
2494 nomem();
2495 namelen = snprintf(NULL, 0, "%u", rid);
2496 } else {
2497 if (nvlist_add_string(props, "name", name) != 0)
2498 nomem();
2499 namelen = strlen(name);
2500 }
2501 nameidx = us_field_index("name");
2502 if (namelen > cb->cb_width[nameidx])
2503 cb->cb_width[nameidx] = namelen;
2504
2505 /*
2506 * Check if this type/name combination is in the list and update it;
2507 * otherwise add new node to the list.
2508 */
2509 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2510 uu_avl_insert(avl, node, idx);
2511 } else {
2512 nvlist_free(props);
2513 free(node);
2514 node = n;
2515 props = node->usn_nvl;
2516 }
2517
2518 /* Calculate/update width of USED/QUOTA fields */
2519 if (cb->cb_nicenum)
2520 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2521 else
2522 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2523 (u_longlong_t)space);
2524 sizelen = strlen(sizebuf);
2525 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2526 propname = "used";
2527 if (!nvlist_exists(props, "quota"))
2528 (void) nvlist_add_uint64(props, "quota", 0);
2529 } else {
2530 propname = "quota";
2531 if (!nvlist_exists(props, "used"))
2532 (void) nvlist_add_uint64(props, "used", 0);
2533 }
2534 sizeidx = us_field_index(propname);
2535 if (sizelen > cb->cb_width[sizeidx])
2536 cb->cb_width[sizeidx] = sizelen;
2537
2538 if (nvlist_add_uint64(props, propname, space) != 0)
2539 nomem();
2540
2541 return (0);
2542 }
2543
2544 static void
2545 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2546 size_t *width, us_node_t *node)
2547 {
2548 nvlist_t *nvl = node->usn_nvl;
2549 char valstr[ZFS_MAXNAMELEN];
2550 boolean_t first = B_TRUE;
2551 int cfield = 0;
2552 int field;
2553 uint32_t ustype;
2554
2555 /* Check type */
2556 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2557 if (!(ustype & types))
2558 return;
2559
2560 while ((field = fields[cfield]) != USFIELD_LAST) {
2561 nvpair_t *nvp = NULL;
2562 data_type_t type;
2563 uint32_t val32;
2564 uint64_t val64;
2565 char *strval = NULL;
2566
2567 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2568 if (strcmp(nvpair_name(nvp),
2569 us_field_names[field]) == 0)
2570 break;
2571 }
2572
2573 type = nvpair_type(nvp);
2574 switch (type) {
2575 case DATA_TYPE_UINT32:
2576 (void) nvpair_value_uint32(nvp, &val32);
2577 break;
2578 case DATA_TYPE_UINT64:
2579 (void) nvpair_value_uint64(nvp, &val64);
2580 break;
2581 case DATA_TYPE_STRING:
2582 (void) nvpair_value_string(nvp, &strval);
2583 break;
2584 default:
2585 (void) fprintf(stderr, "invalid data type\n");
2586 }
2587
2588 switch (field) {
2589 case USFIELD_TYPE:
2590 strval = (char *)us_type2str(val32);
2591 break;
2592 case USFIELD_NAME:
2593 if (type == DATA_TYPE_UINT64) {
2594 (void) sprintf(valstr, "%llu",
2595 (u_longlong_t) val64);
2596 strval = valstr;
2597 }
2598 break;
2599 case USFIELD_USED:
2600 case USFIELD_QUOTA:
2601 if (type == DATA_TYPE_UINT64) {
2602 if (parsable) {
2603 (void) sprintf(valstr, "%llu",
2604 (u_longlong_t) val64);
2605 } else {
2606 zfs_nicenum(val64, valstr,
2607 sizeof (valstr));
2608 }
2609 if (field == USFIELD_QUOTA &&
2610 strcmp(valstr, "0") == 0)
2611 strval = "none";
2612 else
2613 strval = valstr;
2614 }
2615 break;
2616 }
2617
2618 if (!first) {
2619 if (scripted)
2620 (void) printf("\t");
2621 else
2622 (void) printf(" ");
2623 }
2624 if (scripted)
2625 (void) printf("%s", strval);
2626 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2627 (void) printf("%-*s", (int) width[field], strval);
2628 else
2629 (void) printf("%*s", (int) width[field], strval);
2630
2631 first = B_FALSE;
2632 cfield++;
2633 }
2634
2635 (void) printf("\n");
2636 }
2637
2638 static void
2639 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2640 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2641 {
2642 us_node_t *node;
2643 const char *col;
2644 int cfield = 0;
2645 int field;
2646
2647 if (!scripted) {
2648 boolean_t first = B_TRUE;
2649
2650 while ((field = fields[cfield]) != USFIELD_LAST) {
2651 col = gettext(us_field_hdr[field]);
2652 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2653 (void) printf(first ? "%-*s" : " %-*s",
2654 (int) width[field], col);
2655 } else {
2656 (void) printf(first ? "%*s" : " %*s",
2657 (int) width[field], col);
2658 }
2659 first = B_FALSE;
2660 cfield++;
2661 }
2662 (void) printf("\n");
2663 }
2664
2665 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2666 print_us_node(scripted, parsable, fields, types, width, node);
2667 if (rmnode)
2668 nvlist_free(node->usn_nvl);
2669 }
2670 }
2671
2672 static int
2673 zfs_do_userspace(int argc, char **argv)
2674 {
2675 zfs_handle_t *zhp;
2676 zfs_userquota_prop_t p;
2677 uu_avl_pool_t *avl_pool;
2678 uu_avl_t *avl_tree;
2679 uu_avl_walk_t *walk;
2680 char *delim;
2681 char deffields[] = "type,name,used,quota";
2682 char *ofield = NULL;
2683 char *tfield = NULL;
2684 int cfield = 0;
2685 int fields[256];
2686 int i;
2687 boolean_t scripted = B_FALSE;
2688 boolean_t prtnum = B_FALSE;
2689 boolean_t parsable = B_FALSE;
2690 boolean_t sid2posix = B_FALSE;
2691 int ret = 0;
2692 int c;
2693 zfs_sort_column_t *sortcol = NULL;
2694 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2695 us_cbdata_t cb;
2696 us_node_t *node;
2697 us_node_t *rmnode;
2698 uu_list_pool_t *listpool;
2699 uu_list_t *list;
2700 uu_avl_index_t idx = 0;
2701 uu_list_index_t idx2 = 0;
2702
2703 if (argc < 2)
2704 usage(B_FALSE);
2705
2706 if (strcmp(argv[0], "groupspace") == 0)
2707 /* Toggle default group types */
2708 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2709
2710 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2711 switch (c) {
2712 case 'n':
2713 prtnum = B_TRUE;
2714 break;
2715 case 'H':
2716 scripted = B_TRUE;
2717 break;
2718 case 'p':
2719 parsable = B_TRUE;
2720 break;
2721 case 'o':
2722 ofield = optarg;
2723 break;
2724 case 's':
2725 case 'S':
2726 if (zfs_add_sort_column(&sortcol, optarg,
2727 c == 's' ? B_FALSE : B_TRUE) != 0) {
2728 (void) fprintf(stderr,
2729 gettext("invalid field '%s'\n"), optarg);
2730 usage(B_FALSE);
2731 }
2732 break;
2733 case 't':
2734 tfield = optarg;
2735 break;
2736 case 'i':
2737 sid2posix = B_TRUE;
2738 break;
2739 case ':':
2740 (void) fprintf(stderr, gettext("missing argument for "
2741 "'%c' option\n"), optopt);
2742 usage(B_FALSE);
2743 break;
2744 case '?':
2745 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2746 optopt);
2747 usage(B_FALSE);
2748 }
2749 }
2750
2751 argc -= optind;
2752 argv += optind;
2753
2754 if (argc < 1) {
2755 (void) fprintf(stderr, gettext("missing dataset name\n"));
2756 usage(B_FALSE);
2757 }
2758 if (argc > 1) {
2759 (void) fprintf(stderr, gettext("too many arguments\n"));
2760 usage(B_FALSE);
2761 }
2762
2763 /* Use default output fields if not specified using -o */
2764 if (ofield == NULL)
2765 ofield = deffields;
2766 do {
2767 if ((delim = strchr(ofield, ',')) != NULL)
2768 *delim = '\0';
2769 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2770 (void) fprintf(stderr, gettext("invalid type '%s' "
2771 "for -o option\n"), ofield);
2772 return (-1);
2773 }
2774 if (delim != NULL)
2775 ofield = delim + 1;
2776 } while (delim != NULL);
2777 fields[cfield] = USFIELD_LAST;
2778
2779 /* Override output types (-t option) */
2780 if (tfield != NULL) {
2781 types = 0;
2782
2783 do {
2784 boolean_t found = B_FALSE;
2785
2786 if ((delim = strchr(tfield, ',')) != NULL)
2787 *delim = '\0';
2788 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2789 i++) {
2790 if (strcmp(tfield, us_type_names[i]) == 0) {
2791 found = B_TRUE;
2792 types |= us_type_bits[i];
2793 break;
2794 }
2795 }
2796 if (!found) {
2797 (void) fprintf(stderr, gettext("invalid type "
2798 "'%s' for -t option\n"), tfield);
2799 return (-1);
2800 }
2801 if (delim != NULL)
2802 tfield = delim + 1;
2803 } while (delim != NULL);
2804 }
2805
2806 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2807 return (1);
2808
2809 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2810 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2811 nomem();
2812 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2813 nomem();
2814
2815 /* Always add default sorting columns */
2816 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2817 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2818
2819 cb.cb_sortcol = sortcol;
2820 cb.cb_numname = prtnum;
2821 cb.cb_nicenum = !parsable;
2822 cb.cb_avl_pool = avl_pool;
2823 cb.cb_avl = avl_tree;
2824 cb.cb_sid2posix = sid2posix;
2825
2826 for (i = 0; i < USFIELD_LAST; i++)
2827 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2828
2829 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2830 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2831 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2832 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2833 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2834 continue;
2835 cb.cb_prop = p;
2836 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2837 return (ret);
2838 }
2839
2840 /* Sort the list */
2841 if ((node = uu_avl_first(avl_tree)) == NULL)
2842 return (0);
2843
2844 us_populated = B_TRUE;
2845
2846 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2847 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2848 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2849 uu_list_node_init(node, &node->usn_listnode, listpool);
2850
2851 while (node != NULL) {
2852 rmnode = node;
2853 node = uu_avl_next(avl_tree, node);
2854 uu_avl_remove(avl_tree, rmnode);
2855 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2856 uu_list_insert(list, rmnode, idx2);
2857 }
2858
2859 for (node = uu_list_first(list); node != NULL;
2860 node = uu_list_next(list, node)) {
2861 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2862
2863 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2864 uu_avl_insert(avl_tree, node, idx);
2865 }
2866
2867 uu_list_destroy(list);
2868 uu_list_pool_destroy(listpool);
2869
2870 /* Print and free node nvlist memory */
2871 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2872 cb.cb_avl);
2873
2874 zfs_free_sort_columns(sortcol);
2875
2876 /* Clean up the AVL tree */
2877 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2878 nomem();
2879
2880 while ((node = uu_avl_walk_next(walk)) != NULL) {
2881 uu_avl_remove(cb.cb_avl, node);
2882 free(node);
2883 }
2884
2885 uu_avl_walk_end(walk);
2886 uu_avl_destroy(avl_tree);
2887 uu_avl_pool_destroy(avl_pool);
2888
2889 return (ret);
2890 }
2891
2892 /*
2893 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2894 * [-t type[,...]] [filesystem|volume|snapshot] ...
2895 *
2896 * -H Scripted mode; elide headers and separate columns by tabs
2897 * -p Display values in parsable (literal) format.
2898 * -r Recurse over all children
2899 * -d Limit recursion by depth.
2900 * -o Control which fields to display.
2901 * -s Specify sort columns, descending order.
2902 * -S Specify sort columns, ascending order.
2903 * -t Control which object types to display.
2904 *
2905 * When given no arguments, list all filesystems in the system.
2906 * Otherwise, list the specified datasets, optionally recursing down them if
2907 * '-r' is specified.
2908 */
2909 typedef struct list_cbdata {
2910 boolean_t cb_first;
2911 boolean_t cb_literal;
2912 boolean_t cb_scripted;
2913 zprop_list_t *cb_proplist;
2914 } list_cbdata_t;
2915
2916 /*
2917 * Given a list of columns to display, output appropriate headers for each one.
2918 */
2919 static void
2920 print_header(list_cbdata_t *cb)
2921 {
2922 zprop_list_t *pl = cb->cb_proplist;
2923 char headerbuf[ZFS_MAXPROPLEN];
2924 const char *header;
2925 int i;
2926 boolean_t first = B_TRUE;
2927 boolean_t right_justify;
2928
2929 for (; pl != NULL; pl = pl->pl_next) {
2930 if (!first) {
2931 (void) printf(" ");
2932 } else {
2933 first = B_FALSE;
2934 }
2935
2936 right_justify = B_FALSE;
2937 if (pl->pl_prop != ZPROP_INVAL) {
2938 header = zfs_prop_column_name(pl->pl_prop);
2939 right_justify = zfs_prop_align_right(pl->pl_prop);
2940 } else {
2941 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2942 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2943 headerbuf[i] = '\0';
2944 header = headerbuf;
2945 }
2946
2947 if (pl->pl_next == NULL && !right_justify)
2948 (void) printf("%s", header);
2949 else if (right_justify)
2950 (void) printf("%*s", (int)pl->pl_width, header);
2951 else
2952 (void) printf("%-*s", (int)pl->pl_width, header);
2953 }
2954
2955 (void) printf("\n");
2956 }
2957
2958 /*
2959 * Given a dataset and a list of fields, print out all the properties according
2960 * to the described layout.
2961 */
2962 static void
2963 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2964 {
2965 zprop_list_t *pl = cb->cb_proplist;
2966 boolean_t first = B_TRUE;
2967 char property[ZFS_MAXPROPLEN];
2968 nvlist_t *userprops = zfs_get_user_props(zhp);
2969 nvlist_t *propval;
2970 char *propstr;
2971 boolean_t right_justify;
2972
2973 for (; pl != NULL; pl = pl->pl_next) {
2974 if (!first) {
2975 if (cb->cb_scripted)
2976 (void) printf("\t");
2977 else
2978 (void) printf(" ");
2979 } else {
2980 first = B_FALSE;
2981 }
2982
2983 if (pl->pl_prop == ZFS_PROP_NAME) {
2984 (void) strlcpy(property, zfs_get_name(zhp),
2985 sizeof (property));
2986 propstr = property;
2987 right_justify = zfs_prop_align_right(pl->pl_prop);
2988 } else if (pl->pl_prop != ZPROP_INVAL) {
2989 if (zfs_prop_get(zhp, pl->pl_prop, property,
2990 sizeof (property), NULL, NULL, 0,
2991 cb->cb_literal) != 0)
2992 propstr = "-";
2993 else
2994 propstr = property;
2995 right_justify = zfs_prop_align_right(pl->pl_prop);
2996 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2997 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2998 property, sizeof (property), cb->cb_literal) != 0)
2999 propstr = "-";
3000 else
3001 propstr = property;
3002 right_justify = B_TRUE;
3003 } else if (zfs_prop_written(pl->pl_user_prop)) {
3004 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3005 property, sizeof (property), cb->cb_literal) != 0)
3006 propstr = "-";
3007 else
3008 propstr = property;
3009 right_justify = B_TRUE;
3010 } else {
3011 if (nvlist_lookup_nvlist(userprops,
3012 pl->pl_user_prop, &propval) != 0)
3013 propstr = "-";
3014 else
3015 verify(nvlist_lookup_string(propval,
3016 ZPROP_VALUE, &propstr) == 0);
3017 right_justify = B_FALSE;
3018 }
3019
3020 /*
3021 * If this is being called in scripted mode, or if this is the
3022 * last column and it is left-justified, don't include a width
3023 * format specifier.
3024 */
3025 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3026 (void) printf("%s", propstr);
3027 else if (right_justify)
3028 (void) printf("%*s", (int)pl->pl_width, propstr);
3029 else
3030 (void) printf("%-*s", (int)pl->pl_width, propstr);
3031 }
3032
3033 (void) printf("\n");
3034 }
3035
3036 /*
3037 * Generic callback function to list a dataset or snapshot.
3038 */
3039 static int
3040 list_callback(zfs_handle_t *zhp, void *data)
3041 {
3042 list_cbdata_t *cbp = data;
3043
3044 if (cbp->cb_first) {
3045 if (!cbp->cb_scripted)
3046 print_header(cbp);
3047 cbp->cb_first = B_FALSE;
3048 }
3049
3050 print_dataset(zhp, cbp);
3051
3052 return (0);
3053 }
3054
3055 static int
3056 zfs_do_list(int argc, char **argv)
3057 {
3058 int c;
3059 static char default_fields[] =
3060 "name,used,available,referenced,mountpoint";
3061 int types = ZFS_TYPE_DATASET;
3062 boolean_t types_specified = B_FALSE;
3063 char *fields = NULL;
3064 list_cbdata_t cb = { 0 };
3065 char *value;
3066 int limit = 0;
3067 int ret = 0;
3068 zfs_sort_column_t *sortcol = NULL;
3069 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3070
3071 /* check options */
3072 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3073 switch (c) {
3074 case 'o':
3075 fields = optarg;
3076 break;
3077 case 'p':
3078 cb.cb_literal = B_TRUE;
3079 flags |= ZFS_ITER_LITERAL_PROPS;
3080 break;
3081 case 'd':
3082 limit = parse_depth(optarg, &flags);
3083 break;
3084 case 'r':
3085 flags |= ZFS_ITER_RECURSE;
3086 break;
3087 case 'H':
3088 cb.cb_scripted = B_TRUE;
3089 break;
3090 case 's':
3091 if (zfs_add_sort_column(&sortcol, optarg,
3092 B_FALSE) != 0) {
3093 (void) fprintf(stderr,
3094 gettext("invalid property '%s'\n"), optarg);
3095 usage(B_FALSE);
3096 }
3097 break;
3098 case 'S':
3099 if (zfs_add_sort_column(&sortcol, optarg,
3100 B_TRUE) != 0) {
3101 (void) fprintf(stderr,
3102 gettext("invalid property '%s'\n"), optarg);
3103 usage(B_FALSE);
3104 }
3105 break;
3106 case 't':
3107 types = 0;
3108 types_specified = B_TRUE;
3109 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3110 while (*optarg != '\0') {
3111 static char *type_subopts[] = { "filesystem",
3112 "volume", "snapshot", "snap", "bookmark",
3113 "all", NULL };
3114
3115 switch (getsubopt(&optarg, type_subopts,
3116 &value)) {
3117 case 0:
3118 types |= ZFS_TYPE_FILESYSTEM;
3119 break;
3120 case 1:
3121 types |= ZFS_TYPE_VOLUME;
3122 break;
3123 case 2:
3124 case 3:
3125 types |= ZFS_TYPE_SNAPSHOT;
3126 break;
3127 case 4:
3128 types |= ZFS_TYPE_BOOKMARK;
3129 break;
3130 case 5:
3131 types = ZFS_TYPE_DATASET |
3132 ZFS_TYPE_BOOKMARK;
3133 break;
3134 default:
3135 (void) fprintf(stderr,
3136 gettext("invalid type '%s'\n"),
3137 value);
3138 usage(B_FALSE);
3139 }
3140 }
3141 break;
3142 case ':':
3143 (void) fprintf(stderr, gettext("missing argument for "
3144 "'%c' option\n"), optopt);
3145 usage(B_FALSE);
3146 break;
3147 case '?':
3148 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3149 optopt);
3150 usage(B_FALSE);
3151 }
3152 }
3153
3154 argc -= optind;
3155 argv += optind;
3156
3157 if (fields == NULL)
3158 fields = default_fields;
3159
3160 /*
3161 * If we are only going to list snapshot names and sort by name,
3162 * then we can use faster version.
3163 */
3164 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3165 flags |= ZFS_ITER_SIMPLE;
3166
3167 /*
3168 * If "-o space" and no types were specified, don't display snapshots.
3169 */
3170 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3171 types &= ~ZFS_TYPE_SNAPSHOT;
3172
3173 /*
3174 * If the user specifies '-o all', the zprop_get_list() doesn't
3175 * normally include the name of the dataset. For 'zfs list', we always
3176 * want this property to be first.
3177 */
3178 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3179 != 0)
3180 usage(B_FALSE);
3181
3182 cb.cb_first = B_TRUE;
3183
3184 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3185 limit, list_callback, &cb);
3186
3187 zprop_free_list(cb.cb_proplist);
3188 zfs_free_sort_columns(sortcol);
3189
3190 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3191 (void) fprintf(stderr, gettext("no datasets available\n"));
3192
3193 return (ret);
3194 }
3195
3196 /*
3197 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3198 * zfs rename [-f] -p <fs | vol> <fs | vol>
3199 * zfs rename -r <snap> <snap>
3200 *
3201 * Renames the given dataset to another of the same type.
3202 *
3203 * The '-p' flag creates all the non-existing ancestors of the target first.
3204 */
3205 /* ARGSUSED */
3206 static int
3207 zfs_do_rename(int argc, char **argv)
3208 {
3209 zfs_handle_t *zhp;
3210 int c;
3211 int ret = 0;
3212 boolean_t recurse = B_FALSE;
3213 boolean_t parents = B_FALSE;
3214 boolean_t force_unmount = B_FALSE;
3215
3216 /* check options */
3217 while ((c = getopt(argc, argv, "prf")) != -1) {
3218 switch (c) {
3219 case 'p':
3220 parents = B_TRUE;
3221 break;
3222 case 'r':
3223 recurse = B_TRUE;
3224 break;
3225 case 'f':
3226 force_unmount = B_TRUE;
3227 break;
3228 case '?':
3229 default:
3230 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3231 optopt);
3232 usage(B_FALSE);
3233 }
3234 }
3235
3236 argc -= optind;
3237 argv += optind;
3238
3239 /* check number of arguments */
3240 if (argc < 1) {
3241 (void) fprintf(stderr, gettext("missing source dataset "
3242 "argument\n"));
3243 usage(B_FALSE);
3244 }
3245 if (argc < 2) {
3246 (void) fprintf(stderr, gettext("missing target dataset "
3247 "argument\n"));
3248 usage(B_FALSE);
3249 }
3250 if (argc > 2) {
3251 (void) fprintf(stderr, gettext("too many arguments\n"));
3252 usage(B_FALSE);
3253 }
3254
3255 if (recurse && parents) {
3256 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3257 "exclusive\n"));
3258 usage(B_FALSE);
3259 }
3260
3261 if (recurse && strchr(argv[0], '@') == 0) {
3262 (void) fprintf(stderr, gettext("source dataset for recursive "
3263 "rename must be a snapshot\n"));
3264 usage(B_FALSE);
3265 }
3266
3267 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3268 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3269 return (1);
3270
3271 /* If we were asked and the name looks good, try to create ancestors. */
3272 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3273 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3274 zfs_close(zhp);
3275 return (1);
3276 }
3277
3278 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3279
3280 zfs_close(zhp);
3281 return (ret);
3282 }
3283
3284 /*
3285 * zfs promote <fs>
3286 *
3287 * Promotes the given clone fs to be the parent
3288 */
3289 /* ARGSUSED */
3290 static int
3291 zfs_do_promote(int argc, char **argv)
3292 {
3293 zfs_handle_t *zhp;
3294 int ret = 0;
3295
3296 /* check options */
3297 if (argc > 1 && argv[1][0] == '-') {
3298 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3299 argv[1][1]);
3300 usage(B_FALSE);
3301 }
3302
3303 /* check number of arguments */
3304 if (argc < 2) {
3305 (void) fprintf(stderr, gettext("missing clone filesystem"
3306 " argument\n"));
3307 usage(B_FALSE);
3308 }
3309 if (argc > 2) {
3310 (void) fprintf(stderr, gettext("too many arguments\n"));
3311 usage(B_FALSE);
3312 }
3313
3314 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3315 if (zhp == NULL)
3316 return (1);
3317
3318 ret = (zfs_promote(zhp) != 0);
3319
3320
3321 zfs_close(zhp);
3322 return (ret);
3323 }
3324
3325 /*
3326 * zfs rollback [-rRf] <snapshot>
3327 *
3328 * -r Delete any intervening snapshots before doing rollback
3329 * -R Delete any snapshots and their clones
3330 * -f ignored for backwards compatability
3331 *
3332 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3333 * since then and making it the active dataset. If more recent snapshots exist,
3334 * the command will complain unless the '-r' flag is given.
3335 */
3336 typedef struct rollback_cbdata {
3337 uint64_t cb_create;
3338 boolean_t cb_first;
3339 int cb_doclones;
3340 char *cb_target;
3341 int cb_error;
3342 boolean_t cb_recurse;
3343 } rollback_cbdata_t;
3344
3345 static int
3346 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3347 {
3348 rollback_cbdata_t *cbp = data;
3349
3350 if (cbp->cb_first && cbp->cb_recurse) {
3351 (void) fprintf(stderr, gettext("cannot rollback to "
3352 "'%s': clones of previous snapshots exist\n"),
3353 cbp->cb_target);
3354 (void) fprintf(stderr, gettext("use '-R' to "
3355 "force deletion of the following clones and "
3356 "dependents:\n"));
3357 cbp->cb_first = 0;
3358 cbp->cb_error = 1;
3359 }
3360
3361 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3362
3363 zfs_close(zhp);
3364 return (0);
3365 }
3366
3367
3368 /*
3369 * Report any snapshots more recent than the one specified. Used when '-r' is
3370 * not specified. We reuse this same callback for the snapshot dependents - if
3371 * 'cb_dependent' is set, then this is a dependent and we should report it
3372 * without checking the transaction group.
3373 */
3374 static int
3375 rollback_check(zfs_handle_t *zhp, void *data)
3376 {
3377 rollback_cbdata_t *cbp = data;
3378
3379 if (cbp->cb_doclones) {
3380 zfs_close(zhp);
3381 return (0);
3382 }
3383
3384 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3385 if (cbp->cb_first && !cbp->cb_recurse) {
3386 (void) fprintf(stderr, gettext("cannot "
3387 "rollback to '%s': more recent snapshots "
3388 "or bookmarks exist\n"),
3389 cbp->cb_target);
3390 (void) fprintf(stderr, gettext("use '-r' to "
3391 "force deletion of the following "
3392 "snapshots and bookmarks:\n"));
3393 cbp->cb_first = 0;
3394 cbp->cb_error = 1;
3395 }
3396
3397 if (cbp->cb_recurse) {
3398 if (zfs_iter_dependents(zhp, B_TRUE,
3399 rollback_check_dependent, cbp) != 0) {
3400 zfs_close(zhp);
3401 return (-1);
3402 }
3403 } else {
3404 (void) fprintf(stderr, "%s\n",
3405 zfs_get_name(zhp));
3406 }
3407 }
3408 zfs_close(zhp);
3409 return (0);
3410 }
3411
3412 static int
3413 zfs_do_rollback(int argc, char **argv)
3414 {
3415 int ret = 0;
3416 int c;
3417 boolean_t force = B_FALSE;
3418 rollback_cbdata_t cb = { 0 };
3419 zfs_handle_t *zhp, *snap;
3420 char parentname[ZFS_MAXNAMELEN];
3421 char *delim;
3422
3423 /* check options */
3424 while ((c = getopt(argc, argv, "rRf")) != -1) {
3425 switch (c) {
3426 case 'r':
3427 cb.cb_recurse = 1;
3428 break;
3429 case 'R':
3430 cb.cb_recurse = 1;
3431 cb.cb_doclones = 1;
3432 break;
3433 case 'f':
3434 force = B_TRUE;
3435 break;
3436 case '?':
3437 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3438 optopt);
3439 usage(B_FALSE);
3440 }
3441 }
3442
3443 argc -= optind;
3444 argv += optind;
3445
3446 /* check number of arguments */
3447 if (argc < 1) {
3448 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3449 usage(B_FALSE);
3450 }
3451 if (argc > 1) {
3452 (void) fprintf(stderr, gettext("too many arguments\n"));
3453 usage(B_FALSE);
3454 }
3455
3456 /* open the snapshot */
3457 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3458 return (1);
3459
3460 /* open the parent dataset */
3461 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3462 verify((delim = strrchr(parentname, '@')) != NULL);
3463 *delim = '\0';
3464 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3465 zfs_close(snap);
3466 return (1);
3467 }
3468
3469 /*
3470 * Check for more recent snapshots and/or clones based on the presence
3471 * of '-r' and '-R'.
3472 */
3473 cb.cb_target = argv[0];
3474 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3475 cb.cb_first = B_TRUE;
3476 cb.cb_error = 0;
3477 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3478 goto out;
3479 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3480 goto out;
3481
3482 if ((ret = cb.cb_error) != 0)
3483 goto out;
3484
3485 /*
3486 * Rollback parent to the given snapshot.
3487 */
3488 ret = zfs_rollback(zhp, snap, force);
3489
3490 out:
3491 zfs_close(snap);
3492 zfs_close(zhp);
3493
3494 if (ret == 0)
3495 return (0);
3496 else
3497 return (1);
3498 }
3499
3500 /*
3501 * zfs set property=value ... { fs | snap | vol } ...
3502 *
3503 * Sets the given properties for all datasets specified on the command line.
3504 */
3505
3506 static int
3507 set_callback(zfs_handle_t *zhp, void *data)
3508 {
3509 nvlist_t *props = data;
3510
3511 if (zfs_prop_set_list(zhp, props) != 0) {
3512 switch (libzfs_errno(g_zfs)) {
3513 case EZFS_MOUNTFAILED:
3514 (void) fprintf(stderr, gettext("property may be set "
3515 "but unable to remount filesystem\n"));
3516 break;
3517 case EZFS_SHARENFSFAILED:
3518 (void) fprintf(stderr, gettext("property may be set "
3519 "but unable to reshare filesystem\n"));
3520 break;
3521 }
3522 return (1);
3523 }
3524 return (0);
3525 }
3526
3527 static int
3528 zfs_do_set(int argc, char **argv)
3529 {
3530 nvlist_t *props = NULL;
3531 int ds_start = -1; /* argv idx of first dataset arg */
3532 int ret = 0;
3533 int i;
3534
3535 /* check for options */
3536 if (argc > 1 && argv[1][0] == '-') {
3537 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3538 argv[1][1]);
3539 usage(B_FALSE);
3540 }
3541
3542 /* check number of arguments */
3543 if (argc < 2) {
3544 (void) fprintf(stderr, gettext("missing arguments\n"));
3545 usage(B_FALSE);
3546 }
3547 if (argc < 3) {
3548 if (strchr(argv[1], '=') == NULL) {
3549 (void) fprintf(stderr, gettext("missing property=value "
3550 "argument(s)\n"));
3551 } else {
3552 (void) fprintf(stderr, gettext("missing dataset "
3553 "name(s)\n"));
3554 }
3555 usage(B_FALSE);
3556 }
3557
3558 /* validate argument order: prop=val args followed by dataset args */
3559 for (i = 1; i < argc; i++) {
3560 if (strchr(argv[i], '=') != NULL) {
3561 if (ds_start > 0) {
3562 /* out-of-order prop=val argument */
3563 (void) fprintf(stderr, gettext("invalid "
3564 "argument order\n"));
3565 usage(B_FALSE);
3566 }
3567 } else if (ds_start < 0) {
3568 ds_start = i;
3569 }
3570 }
3571 if (ds_start < 0) {
3572 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3573 usage(B_FALSE);
3574 }
3575
3576 /* Populate a list of property settings */
3577 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3578 nomem();
3579 for (i = 1; i < ds_start; i++) {
3580 if ((ret = parseprop(props, argv[i])) != 0)
3581 goto error;
3582 }
3583
3584 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3585 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3586
3587 error:
3588 nvlist_free(props);
3589 return (ret);
3590 }
3591
3592 typedef struct snap_cbdata {
3593 nvlist_t *sd_nvl;
3594 boolean_t sd_recursive;
3595 const char *sd_snapname;
3596 } snap_cbdata_t;
3597
3598 static int
3599 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3600 {
3601 snap_cbdata_t *sd = arg;
3602 char *name;
3603 int rv = 0;
3604 int error;
3605
3606 if (sd->sd_recursive &&
3607 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3608 zfs_close(zhp);
3609 return (0);
3610 }
3611
3612 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3613 if (error == -1)
3614 nomem();
3615 fnvlist_add_boolean(sd->sd_nvl, name);
3616 free(name);
3617
3618 if (sd->sd_recursive)
3619 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3620 zfs_close(zhp);
3621 return (rv);
3622 }
3623
3624 /*
3625 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3626 *
3627 * Creates a snapshot with the given name. While functionally equivalent to
3628 * 'zfs create', it is a separate command to differentiate intent.
3629 */
3630 static int
3631 zfs_do_snapshot(int argc, char **argv)
3632 {
3633 int ret = 0;
3634 signed char c;
3635 nvlist_t *props;
3636 snap_cbdata_t sd = { 0 };
3637 boolean_t multiple_snaps = B_FALSE;
3638
3639 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3640 nomem();
3641 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3642 nomem();
3643
3644 /* check options */
3645 while ((c = getopt(argc, argv, "ro:")) != -1) {
3646 switch (c) {
3647 case 'o':
3648 if (parseprop(props, optarg) != 0)
3649 return (1);
3650 break;
3651 case 'r':
3652 sd.sd_recursive = B_TRUE;
3653 multiple_snaps = B_TRUE;
3654 break;
3655 case '?':
3656 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3657 optopt);
3658 goto usage;
3659 }
3660 }
3661
3662 argc -= optind;
3663 argv += optind;
3664
3665 /* check number of arguments */
3666 if (argc < 1) {
3667 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3668 goto usage;
3669 }
3670
3671 if (argc > 1)
3672 multiple_snaps = B_TRUE;
3673 for (; argc > 0; argc--, argv++) {
3674 char *atp;
3675 zfs_handle_t *zhp;
3676
3677 atp = strchr(argv[0], '@');
3678 if (atp == NULL)
3679 goto usage;
3680 *atp = '\0';
3681 sd.sd_snapname = atp + 1;
3682 zhp = zfs_open(g_zfs, argv[0],
3683 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3684 if (zhp == NULL)
3685 goto usage;
3686 if (zfs_snapshot_cb(zhp, &sd) != 0)
3687 goto usage;
3688 }
3689
3690 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3691 nvlist_free(sd.sd_nvl);
3692 nvlist_free(props);
3693 if (ret != 0 && multiple_snaps)
3694 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3695 return (ret != 0);
3696
3697 usage:
3698 nvlist_free(sd.sd_nvl);
3699 nvlist_free(props);
3700 usage(B_FALSE);
3701 return (-1);
3702 }
3703
3704 /*
3705 * Send a backup stream to stdout.
3706 */
3707 static int
3708 zfs_do_send(int argc, char **argv)
3709 {
3710 char *fromname = NULL;
3711 char *toname = NULL;
3712 char *resume_token = NULL;
3713 char *cp;
3714 zfs_handle_t *zhp;
3715 sendflags_t flags = { 0 };
3716 int c, err;
3717 nvlist_t *dbgnv = NULL;
3718 boolean_t extraverbose = B_FALSE;
3719
3720 /* check options */
3721 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) {
3722 switch (c) {
3723 case 'i':
3724 if (fromname)
3725 usage(B_FALSE);
3726 fromname = optarg;
3727 break;
3728 case 'I':
3729 if (fromname)
3730 usage(B_FALSE);
3731 fromname = optarg;
3732 flags.doall = B_TRUE;
3733 break;
3734 case 'R':
3735 flags.replicate = B_TRUE;
3736 break;
3737 case 'p':
3738 flags.props = B_TRUE;
3739 break;
3740 case 'P':
3741 flags.parsable = B_TRUE;
3742 flags.verbose = B_TRUE;
3743 break;
3744 case 'v':
3745 if (flags.verbose)
3746 extraverbose = B_TRUE;
3747 flags.verbose = B_TRUE;
3748 flags.progress = B_TRUE;
3749 break;
3750 case 'D':
3751 flags.dedup = B_TRUE;
3752 break;
3753 case 'n':
3754 flags.dryrun = B_TRUE;
3755 break;
3756 case 'L':
3757 flags.largeblock = B_TRUE;
3758 break;
3759 case 'e':
3760 flags.embed_data = B_TRUE;
3761 break;
3762 case 't':
3763 resume_token = optarg;
3764 break;
3765 case ':':
3766 (void) fprintf(stderr, gettext("missing argument for "
3767 "'%c' option\n"), optopt);
3768 usage(B_FALSE);
3769 break;
3770 case '?':
3771 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3772 optopt);
3773 usage(B_FALSE);
3774 }
3775 }
3776
3777 argc -= optind;
3778 argv += optind;
3779
3780 if (resume_token != NULL) {
3781 if (fromname != NULL || flags.replicate || flags.props ||
3782 flags.dedup) {
3783 (void) fprintf(stderr,
3784 gettext("invalid flags combined with -t\n"));
3785 usage(B_FALSE);
3786 }
3787 if (argc != 0) {
3788 (void) fprintf(stderr, gettext("no additional "
3789 "arguments are permitted with -t\n"));
3790 usage(B_FALSE);
3791 }
3792 } else {
3793 if (argc < 1) {
3794 (void) fprintf(stderr,
3795 gettext("missing snapshot argument\n"));
3796 usage(B_FALSE);
3797 }
3798 if (argc > 1) {
3799 (void) fprintf(stderr, gettext("too many arguments\n"));
3800 usage(B_FALSE);
3801 }
3802 }
3803
3804 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3805 (void) fprintf(stderr,
3806 gettext("Error: Stream can not be written to a terminal.\n"
3807 "You must redirect standard output.\n"));
3808 return (1);
3809 }
3810
3811 if (resume_token != NULL) {
3812 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3813 resume_token));
3814 }
3815
3816 /*
3817 * Special case sending a filesystem, or from a bookmark.
3818 */
3819 if (strchr(argv[0], '@') == NULL ||
3820 (fromname && strchr(fromname, '#') != NULL)) {
3821 char frombuf[ZFS_MAXNAMELEN];
3822 enum lzc_send_flags lzc_flags = 0;
3823
3824 if (flags.replicate || flags.doall || flags.props ||
3825 flags.dedup || flags.dryrun || flags.verbose ||
3826 flags.progress) {
3827 (void) fprintf(stderr,
3828 gettext("Error: "
3829 "Unsupported flag with filesystem or bookmark.\n"));
3830 return (1);
3831 }
3832
3833 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3834 if (zhp == NULL)
3835 return (1);
3836
3837 if (flags.largeblock)
3838 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3839 if (flags.embed_data)
3840 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3841
3842 if (fromname != NULL &&
3843 (fromname[0] == '#' || fromname[0] == '@')) {
3844 /*
3845 * Incremental source name begins with # or @.
3846 * Default to same fs as target.
3847 */
3848 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3849 cp = strchr(frombuf, '@');
3850 if (cp != NULL)
3851 *cp = '\0';
3852 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3853 fromname = frombuf;
3854 }
3855 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3856 zfs_close(zhp);
3857 return (err != 0);
3858 }
3859
3860 cp = strchr(argv[0], '@');
3861 *cp = '\0';
3862 toname = cp + 1;
3863 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3864 if (zhp == NULL)
3865 return (1);
3866
3867 /*
3868 * If they specified the full path to the snapshot, chop off
3869 * everything except the short name of the snapshot, but special
3870 * case if they specify the origin.
3871 */
3872 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3873 char origin[ZFS_MAXNAMELEN];
3874 zprop_source_t src;
3875
3876 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3877 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3878
3879 if (strcmp(origin, fromname) == 0) {
3880 fromname = NULL;
3881 flags.fromorigin = B_TRUE;
3882 } else {
3883 *cp = '\0';
3884 if (cp != fromname && strcmp(argv[0], fromname)) {
3885 (void) fprintf(stderr,
3886 gettext("incremental source must be "
3887 "in same filesystem\n"));
3888 usage(B_FALSE);
3889 }
3890 fromname = cp + 1;
3891 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3892 (void) fprintf(stderr,
3893 gettext("invalid incremental source\n"));
3894 usage(B_FALSE);
3895 }
3896 }
3897 }
3898
3899 if (flags.replicate && fromname == NULL)
3900 flags.doall = B_TRUE;
3901
3902 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3903 extraverbose ? &dbgnv : NULL);
3904
3905 if (extraverbose && dbgnv != NULL) {
3906 /*
3907 * dump_nvlist prints to stdout, but that's been
3908 * redirected to a file. Make it print to stderr
3909 * instead.
3910 */
3911 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3912 dump_nvlist(dbgnv, 0);
3913 nvlist_free(dbgnv);
3914 }
3915 zfs_close(zhp);
3916
3917 return (err != 0);
3918 }
3919
3920 /*
3921 * Restore a backup stream from stdin.
3922 */
3923 static int
3924 zfs_do_receive(int argc, char **argv)
3925 {
3926 int c, err;
3927 recvflags_t flags = { 0 };
3928 boolean_t abort_resumable = B_FALSE;
3929
3930 nvlist_t *props;
3931 nvpair_t *nvp = NULL;
3932
3933 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3934 nomem();
3935
3936 /* check options */
3937 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3938 switch (c) {
3939 case 'o':
3940 if (parseprop(props, optarg) != 0)
3941 return (1);
3942 break;
3943 case 'd':
3944 flags.isprefix = B_TRUE;
3945 break;
3946 case 'e':
3947 flags.isprefix = B_TRUE;
3948 flags.istail = B_TRUE;
3949 break;
3950 case 'n':
3951 flags.dryrun = B_TRUE;
3952 break;
3953 case 'u':
3954 flags.nomount = B_TRUE;
3955 break;
3956 case 'v':
3957 flags.verbose = B_TRUE;
3958 break;
3959 case 's':
3960 flags.resumable = B_TRUE;
3961 break;
3962 case 'F':
3963 flags.force = B_TRUE;
3964 break;
3965 case 'A':
3966 abort_resumable = B_TRUE;
3967 break;
3968 case ':':
3969 (void) fprintf(stderr, gettext("missing argument for "
3970 "'%c' option\n"), optopt);
3971 usage(B_FALSE);
3972 break;
3973 case '?':
3974 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3975 optopt);
3976 usage(B_FALSE);
3977 }
3978 }
3979
3980 argc -= optind;
3981 argv += optind;
3982
3983 /* check number of arguments */
3984 if (argc < 1) {
3985 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3986 usage(B_FALSE);
3987 }
3988 if (argc > 1) {
3989 (void) fprintf(stderr, gettext("too many arguments\n"));
3990 usage(B_FALSE);
3991 }
3992
3993 while ((nvp = nvlist_next_nvpair(props, nvp))) {
3994 if (strcmp(nvpair_name(nvp), "origin") != 0) {
3995 (void) fprintf(stderr, gettext("invalid option"));
3996 usage(B_FALSE);
3997 }
3998 }
3999
4000 if (abort_resumable) {
4001 if (flags.isprefix || flags.istail || flags.dryrun ||
4002 flags.resumable || flags.nomount) {
4003 (void) fprintf(stderr, gettext("invalid option"));
4004 usage(B_FALSE);
4005 }
4006
4007 char namebuf[ZFS_MAXNAMELEN];
4008 (void) snprintf(namebuf, sizeof (namebuf),
4009 "%s/%%recv", argv[0]);
4010
4011 if (zfs_dataset_exists(g_zfs, namebuf,
4012 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4013 zfs_handle_t *zhp = zfs_open(g_zfs,
4014 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4015 if (zhp == NULL)
4016 return (1);
4017 err = zfs_destroy(zhp, B_FALSE);
4018 } else {
4019 zfs_handle_t *zhp = zfs_open(g_zfs,
4020 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4021 if (zhp == NULL)
4022 usage(B_FALSE);
4023 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4024 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4025 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4026 (void) fprintf(stderr,
4027 gettext("'%s' does not have any "
4028 "resumable receive state to abort\n"),
4029 argv[0]);
4030 return (1);
4031 }
4032 err = zfs_destroy(zhp, B_FALSE);
4033 }
4034
4035 return (err != 0);
4036 }
4037
4038 if (isatty(STDIN_FILENO)) {
4039 (void) fprintf(stderr,
4040 gettext("Error: Backup stream can not be read "
4041 "from a terminal.\n"
4042 "You must redirect standard input.\n"));
4043 return (1);
4044 }
4045 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4046
4047 return (err != 0);
4048 }
4049
4050 /*
4051 * allow/unallow stuff
4052 */
4053 /* copied from zfs/sys/dsl_deleg.h */
4054 #define ZFS_DELEG_PERM_CREATE "create"
4055 #define ZFS_DELEG_PERM_DESTROY "destroy"
4056 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4057 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4058 #define ZFS_DELEG_PERM_CLONE "clone"
4059 #define ZFS_DELEG_PERM_PROMOTE "promote"
4060 #define ZFS_DELEG_PERM_RENAME "rename"
4061 #define ZFS_DELEG_PERM_MOUNT "mount"
4062 #define ZFS_DELEG_PERM_SHARE "share"
4063 #define ZFS_DELEG_PERM_SEND "send"
4064 #define ZFS_DELEG_PERM_RECEIVE "receive"
4065 #define ZFS_DELEG_PERM_ALLOW "allow"
4066 #define ZFS_DELEG_PERM_USERPROP "userprop"
4067 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4068 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4069 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4070 #define ZFS_DELEG_PERM_USERUSED "userused"
4071 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4072 #define ZFS_DELEG_PERM_HOLD "hold"
4073 #define ZFS_DELEG_PERM_RELEASE "release"
4074 #define ZFS_DELEG_PERM_DIFF "diff"
4075 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4076
4077 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4078
4079 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4080 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4081 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4082 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4083 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4084 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4085 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4086 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4087 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4088 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4089 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4090 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4091 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4092 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4093 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4094 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4095 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4096
4097 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4098 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4099 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4100 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4101 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4102 { NULL, ZFS_DELEG_NOTE_NONE }
4103 };
4104
4105 /* permission structure */
4106 typedef struct deleg_perm {
4107 zfs_deleg_who_type_t dp_who_type;
4108 const char *dp_name;
4109 boolean_t dp_local;
4110 boolean_t dp_descend;
4111 } deleg_perm_t;
4112
4113 /* */
4114 typedef struct deleg_perm_node {
4115 deleg_perm_t dpn_perm;
4116
4117 uu_avl_node_t dpn_avl_node;
4118 } deleg_perm_node_t;
4119
4120 typedef struct fs_perm fs_perm_t;
4121
4122 /* permissions set */
4123 typedef struct who_perm {
4124 zfs_deleg_who_type_t who_type;
4125 const char *who_name; /* id */
4126 char who_ug_name[256]; /* user/group name */
4127 fs_perm_t *who_fsperm; /* uplink */
4128
4129 uu_avl_t *who_deleg_perm_avl; /* permissions */
4130 } who_perm_t;
4131
4132 /* */
4133 typedef struct who_perm_node {
4134 who_perm_t who_perm;
4135 uu_avl_node_t who_avl_node;
4136 } who_perm_node_t;
4137
4138 typedef struct fs_perm_set fs_perm_set_t;
4139 /* fs permissions */
4140 struct fs_perm {
4141 const char *fsp_name;
4142
4143 uu_avl_t *fsp_sc_avl; /* sets,create */
4144 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4145
4146 fs_perm_set_t *fsp_set; /* uplink */
4147 };
4148
4149 /* */
4150 typedef struct fs_perm_node {
4151 fs_perm_t fspn_fsperm;
4152 uu_avl_t *fspn_avl;
4153
4154 uu_list_node_t fspn_list_node;
4155 } fs_perm_node_t;
4156
4157 /* top level structure */
4158 struct fs_perm_set {
4159 uu_list_pool_t *fsps_list_pool;
4160 uu_list_t *fsps_list; /* list of fs_perms */
4161
4162 uu_avl_pool_t *fsps_named_set_avl_pool;
4163 uu_avl_pool_t *fsps_who_perm_avl_pool;
4164 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4165 };
4166
4167 static inline const char *
4168 deleg_perm_type(zfs_deleg_note_t note)
4169 {
4170 /* subcommands */
4171 switch (note) {
4172 /* SUBCOMMANDS */
4173 /* OTHER */
4174 case ZFS_DELEG_NOTE_GROUPQUOTA:
4175 case ZFS_DELEG_NOTE_GROUPUSED:
4176 case ZFS_DELEG_NOTE_USERPROP:
4177 case ZFS_DELEG_NOTE_USERQUOTA:
4178 case ZFS_DELEG_NOTE_USERUSED:
4179 /* other */
4180 return (gettext("other"));
4181 default:
4182 return (gettext("subcommand"));
4183 }
4184 }
4185
4186 static int inline
4187 who_type2weight(zfs_deleg_who_type_t who_type)
4188 {
4189 int res;
4190 switch (who_type) {
4191 case ZFS_DELEG_NAMED_SET_SETS:
4192 case ZFS_DELEG_NAMED_SET:
4193 res = 0;
4194 break;
4195 case ZFS_DELEG_CREATE_SETS:
4196 case ZFS_DELEG_CREATE:
4197 res = 1;
4198 break;
4199 case ZFS_DELEG_USER_SETS:
4200 case ZFS_DELEG_USER:
4201 res = 2;
4202 break;
4203 case ZFS_DELEG_GROUP_SETS:
4204 case ZFS_DELEG_GROUP:
4205 res = 3;
4206 break;
4207 case ZFS_DELEG_EVERYONE_SETS:
4208 case ZFS_DELEG_EVERYONE:
4209 res = 4;
4210 break;
4211 default:
4212 res = -1;
4213 }
4214
4215 return (res);
4216 }
4217
4218 /* ARGSUSED */
4219 static int
4220 who_perm_compare(const void *larg, const void *rarg, void *unused)
4221 {
4222 const who_perm_node_t *l = larg;
4223 const who_perm_node_t *r = rarg;
4224 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4225 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4226 int lweight = who_type2weight(ltype);
4227 int rweight = who_type2weight(rtype);
4228 int res = lweight - rweight;
4229 if (res == 0)
4230 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4231 ZFS_MAX_DELEG_NAME-1);
4232
4233 if (res == 0)
4234 return (0);
4235 if (res > 0)
4236 return (1);
4237 else
4238 return (-1);
4239 }
4240
4241 /* ARGSUSED */
4242 static int
4243 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4244 {
4245 const deleg_perm_node_t *l = larg;
4246 const deleg_perm_node_t *r = rarg;
4247 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4248 ZFS_MAX_DELEG_NAME-1);
4249
4250 if (res == 0)
4251 return (0);
4252
4253 if (res > 0)
4254 return (1);
4255 else
4256 return (-1);
4257 }
4258
4259 static inline void
4260 fs_perm_set_init(fs_perm_set_t *fspset)
4261 {
4262 bzero(fspset, sizeof (fs_perm_set_t));
4263
4264 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4265 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4266 NULL, UU_DEFAULT)) == NULL)
4267 nomem();
4268 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4269 UU_DEFAULT)) == NULL)
4270 nomem();
4271
4272 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4273 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4274 who_perm_node_t, who_avl_node), who_perm_compare,
4275 UU_DEFAULT)) == NULL)
4276 nomem();
4277
4278 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4279 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4280 who_perm_node_t, who_avl_node), who_perm_compare,
4281 UU_DEFAULT)) == NULL)
4282 nomem();
4283
4284 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4285 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4286 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4287 == NULL)
4288 nomem();
4289 }
4290
4291 static inline void fs_perm_fini(fs_perm_t *);
4292 static inline void who_perm_fini(who_perm_t *);
4293
4294 static inline void
4295 fs_perm_set_fini(fs_perm_set_t *fspset)
4296 {
4297 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4298
4299 while (node != NULL) {
4300 fs_perm_node_t *next_node =
4301 uu_list_next(fspset->fsps_list, node);
4302 fs_perm_t *fsperm = &node->fspn_fsperm;
4303 fs_perm_fini(fsperm);
4304 uu_list_remove(fspset->fsps_list, node);
4305 free(node);
4306 node = next_node;
4307 }
4308
4309 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4310 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4311 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4312 }
4313
4314 static inline void
4315 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4316 const char *name)
4317 {
4318 deleg_perm->dp_who_type = type;
4319 deleg_perm->dp_name = name;
4320 }
4321
4322 static inline void
4323 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4324 zfs_deleg_who_type_t type, const char *name)
4325 {
4326 uu_avl_pool_t *pool;
4327 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4328
4329 bzero(who_perm, sizeof (who_perm_t));
4330
4331 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4332 UU_DEFAULT)) == NULL)
4333 nomem();
4334
4335 who_perm->who_type = type;
4336 who_perm->who_name = name;
4337 who_perm->who_fsperm = fsperm;
4338 }
4339
4340 static inline void
4341 who_perm_fini(who_perm_t *who_perm)
4342 {
4343 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4344
4345 while (node != NULL) {
4346 deleg_perm_node_t *next_node =
4347 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4348
4349 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4350 free(node);
4351 node = next_node;
4352 }
4353
4354 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4355 }
4356
4357 static inline void
4358 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4359 {
4360 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4361 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4362
4363 bzero(fsperm, sizeof (fs_perm_t));
4364
4365 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4366 == NULL)
4367 nomem();
4368
4369 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4370 == NULL)
4371 nomem();
4372
4373 fsperm->fsp_set = fspset;
4374 fsperm->fsp_name = fsname;
4375 }
4376
4377 static inline void
4378 fs_perm_fini(fs_perm_t *fsperm)
4379 {
4380 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4381 while (node != NULL) {
4382 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4383 node);
4384 who_perm_t *who_perm = &node->who_perm;
4385 who_perm_fini(who_perm);
4386 uu_avl_remove(fsperm->fsp_sc_avl, node);
4387 free(node);
4388 node = next_node;
4389 }
4390
4391 node = uu_avl_first(fsperm->fsp_uge_avl);
4392 while (node != NULL) {
4393 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4394 node);
4395 who_perm_t *who_perm = &node->who_perm;
4396 who_perm_fini(who_perm);
4397 uu_avl_remove(fsperm->fsp_uge_avl, node);
4398 free(node);
4399 node = next_node;
4400 }
4401
4402 uu_avl_destroy(fsperm->fsp_sc_avl);
4403 uu_avl_destroy(fsperm->fsp_uge_avl);
4404 }
4405
4406 static void inline
4407 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4408 zfs_deleg_who_type_t who_type, const char *name, char locality)
4409 {
4410 uu_avl_index_t idx = 0;
4411
4412 deleg_perm_node_t *found_node = NULL;
4413 deleg_perm_t *deleg_perm = &node->dpn_perm;
4414
4415 deleg_perm_init(deleg_perm, who_type, name);
4416
4417 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4418 == NULL)
4419 uu_avl_insert(avl, node, idx);
4420 else {
4421 node = found_node;
4422 deleg_perm = &node->dpn_perm;
4423 }
4424
4425
4426 switch (locality) {
4427 case ZFS_DELEG_LOCAL:
4428 deleg_perm->dp_local = B_TRUE;
4429 break;
4430 case ZFS_DELEG_DESCENDENT:
4431 deleg_perm->dp_descend = B_TRUE;
4432 break;
4433 case ZFS_DELEG_NA:
4434 break;
4435 default:
4436 assert(B_FALSE); /* invalid locality */
4437 }
4438 }
4439
4440 static inline int
4441 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4442 {
4443 nvpair_t *nvp = NULL;
4444 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4445 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4446 zfs_deleg_who_type_t who_type = who_perm->who_type;
4447
4448 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4449 const char *name = nvpair_name(nvp);
4450 data_type_t type = nvpair_type(nvp);
4451 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4452 deleg_perm_node_t *node =
4453 safe_malloc(sizeof (deleg_perm_node_t));
4454
4455 VERIFY(type == DATA_TYPE_BOOLEAN);
4456
4457 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4458 set_deleg_perm_node(avl, node, who_type, name, locality);
4459 }
4460
4461 return (0);
4462 }
4463
4464 static inline int
4465 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4466 {
4467 nvpair_t *nvp = NULL;
4468 fs_perm_set_t *fspset = fsperm->fsp_set;
4469
4470 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4471 nvlist_t *nvl2 = NULL;
4472 const char *name = nvpair_name(nvp);
4473 uu_avl_t *avl = NULL;
4474 uu_avl_pool_t *avl_pool = NULL;
4475 zfs_deleg_who_type_t perm_type = name[0];
4476 char perm_locality = name[1];
4477 const char *perm_name = name + 3;
4478 boolean_t is_set = B_TRUE;
4479 who_perm_t *who_perm = NULL;
4480
4481 assert('$' == name[2]);
4482
4483 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4484 return (-1);
4485
4486 switch (perm_type) {
4487 case ZFS_DELEG_CREATE:
4488 case ZFS_DELEG_CREATE_SETS:
4489 case ZFS_DELEG_NAMED_SET:
4490 case ZFS_DELEG_NAMED_SET_SETS:
4491 avl_pool = fspset->fsps_named_set_avl_pool;
4492 avl = fsperm->fsp_sc_avl;
4493 break;
4494 case ZFS_DELEG_USER:
4495 case ZFS_DELEG_USER_SETS:
4496 case ZFS_DELEG_GROUP:
4497 case ZFS_DELEG_GROUP_SETS:
4498 case ZFS_DELEG_EVERYONE:
4499 case ZFS_DELEG_EVERYONE_SETS:
4500 avl_pool = fspset->fsps_who_perm_avl_pool;
4501 avl = fsperm->fsp_uge_avl;
4502 break;
4503 default:
4504 break;
4505 }
4506
4507 if (is_set) {
4508 who_perm_node_t *found_node = NULL;
4509 who_perm_node_t *node = safe_malloc(
4510 sizeof (who_perm_node_t));
4511 who_perm = &node->who_perm;
4512 uu_avl_index_t idx = 0;
4513
4514 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4515 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4516
4517 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4518 == NULL) {
4519 if (avl == fsperm->fsp_uge_avl) {
4520 uid_t rid = 0;
4521 struct passwd *p = NULL;
4522 struct group *g = NULL;
4523 const char *nice_name = NULL;
4524
4525 switch (perm_type) {
4526 case ZFS_DELEG_USER_SETS:
4527 case ZFS_DELEG_USER:
4528 rid = atoi(perm_name);
4529 p = getpwuid(rid);
4530 if (p)
4531 nice_name = p->pw_name;
4532 break;
4533 case ZFS_DELEG_GROUP_SETS:
4534 case ZFS_DELEG_GROUP:
4535 rid = atoi(perm_name);
4536 g = getgrgid(rid);
4537 if (g)
4538 nice_name = g->gr_name;
4539 break;
4540 default:
4541 break;
4542 }
4543
4544 if (nice_name != NULL)
4545 (void) strlcpy(
4546 node->who_perm.who_ug_name,
4547 nice_name, 256);
4548 }
4549
4550 uu_avl_insert(avl, node, idx);
4551 } else {
4552 node = found_node;
4553 who_perm = &node->who_perm;
4554 }
4555 }
4556
4557 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4558 }
4559
4560 return (0);
4561 }
4562
4563 static inline int
4564 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4565 {
4566 nvpair_t *nvp = NULL;
4567 uu_avl_index_t idx = 0;
4568
4569 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4570 nvlist_t *nvl2 = NULL;
4571 const char *fsname = nvpair_name(nvp);
4572 data_type_t type = nvpair_type(nvp);
4573 fs_perm_t *fsperm = NULL;
4574 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4575 if (node == NULL)
4576 nomem();
4577
4578 fsperm = &node->fspn_fsperm;
4579
4580 VERIFY(DATA_TYPE_NVLIST == type);
4581
4582 uu_list_node_init(node, &node->fspn_list_node,
4583 fspset->fsps_list_pool);
4584
4585 idx = uu_list_numnodes(fspset->fsps_list);
4586 fs_perm_init(fsperm, fspset, fsname);
4587
4588 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4589 return (-1);
4590
4591 (void) parse_fs_perm(fsperm, nvl2);
4592
4593 uu_list_insert(fspset->fsps_list, node, idx);
4594 }
4595
4596 return (0);
4597 }
4598
4599 static inline const char *
4600 deleg_perm_comment(zfs_deleg_note_t note)
4601 {
4602 const char *str = "";
4603
4604 /* subcommands */
4605 switch (note) {
4606 /* SUBCOMMANDS */
4607 case ZFS_DELEG_NOTE_ALLOW:
4608 str = gettext("Must also have the permission that is being"
4609 "\n\t\t\t\tallowed");
4610 break;
4611 case ZFS_DELEG_NOTE_CLONE:
4612 str = gettext("Must also have the 'create' ability and 'mount'"
4613 "\n\t\t\t\tability in the origin file system");
4614 break;
4615 case ZFS_DELEG_NOTE_CREATE:
4616 str = gettext("Must also have the 'mount' ability");
4617 break;
4618 case ZFS_DELEG_NOTE_DESTROY:
4619 str = gettext("Must also have the 'mount' ability");
4620 break;
4621 case ZFS_DELEG_NOTE_DIFF:
4622 str = gettext("Allows lookup of paths within a dataset;"
4623 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4624 "\n\t\t\t\tin order to use zfs diff");
4625 break;
4626 case ZFS_DELEG_NOTE_HOLD:
4627 str = gettext("Allows adding a user hold to a snapshot");
4628 break;
4629 case ZFS_DELEG_NOTE_MOUNT:
4630 str = gettext("Allows mount/umount of ZFS datasets");
4631 break;
4632 case ZFS_DELEG_NOTE_PROMOTE:
4633 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4634 " 'promote' ability in the origin file system");
4635 break;
4636 case ZFS_DELEG_NOTE_RECEIVE:
4637 str = gettext("Must also have the 'mount' and 'create'"
4638 " ability");
4639 break;
4640 case ZFS_DELEG_NOTE_RELEASE:
4641 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4642 "might destroy the snapshot");
4643 break;
4644 case ZFS_DELEG_NOTE_RENAME:
4645 str = gettext("Must also have the 'mount' and 'create'"
4646 "\n\t\t\t\tability in the new parent");
4647 break;
4648 case ZFS_DELEG_NOTE_ROLLBACK:
4649 str = gettext("");
4650 break;
4651 case ZFS_DELEG_NOTE_SEND:
4652 str = gettext("");
4653 break;
4654 case ZFS_DELEG_NOTE_SHARE:
4655 str = gettext("Allows sharing file systems over NFS or SMB"
4656 "\n\t\t\t\tprotocols");
4657 break;
4658 case ZFS_DELEG_NOTE_SNAPSHOT:
4659 str = gettext("");
4660 break;
4661 /*
4662 * case ZFS_DELEG_NOTE_VSCAN:
4663 * str = gettext("");
4664 * break;
4665 */
4666 /* OTHER */
4667 case ZFS_DELEG_NOTE_GROUPQUOTA:
4668 str = gettext("Allows accessing any groupquota@... property");
4669 break;
4670 case ZFS_DELEG_NOTE_GROUPUSED:
4671 str = gettext("Allows reading any groupused@... property");
4672 break;
4673 case ZFS_DELEG_NOTE_USERPROP:
4674 str = gettext("Allows changing any user property");
4675 break;
4676 case ZFS_DELEG_NOTE_USERQUOTA:
4677 str = gettext("Allows accessing any userquota@... property");
4678 break;
4679 case ZFS_DELEG_NOTE_USERUSED:
4680 str = gettext("Allows reading any userused@... property");
4681 break;
4682 /* other */
4683 default:
4684 str = "";
4685 }
4686
4687 return (str);
4688 }
4689
4690 struct allow_opts {
4691 boolean_t local;
4692 boolean_t descend;
4693 boolean_t user;
4694 boolean_t group;
4695 boolean_t everyone;
4696 boolean_t create;
4697 boolean_t set;
4698 boolean_t recursive; /* unallow only */
4699 boolean_t prt_usage;
4700
4701 boolean_t prt_perms;
4702 char *who;
4703 char *perms;
4704 const char *dataset;
4705 };
4706
4707 static inline int
4708 prop_cmp(const void *a, const void *b)
4709 {
4710 const char *str1 = *(const char **)a;
4711 const char *str2 = *(const char **)b;
4712 return (strcmp(str1, str2));
4713 }
4714
4715 static void
4716 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4717 {
4718 const char *opt_desc[] = {
4719 "-h", gettext("show this help message and exit"),
4720 "-l", gettext("set permission locally"),
4721 "-d", gettext("set permission for descents"),
4722 "-u", gettext("set permission for user"),
4723 "-g", gettext("set permission for group"),
4724 "-e", gettext("set permission for everyone"),
4725 "-c", gettext("set create time permission"),
4726 "-s", gettext("define permission set"),
4727 /* unallow only */
4728 "-r", gettext("remove permissions recursively"),
4729 };
4730 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4731 size_t allow_size = unallow_size - 2;
4732 const char *props[ZFS_NUM_PROPS];
4733 int i;
4734 size_t count = 0;
4735 FILE *fp = requested ? stdout : stderr;
4736 zprop_desc_t *pdtbl = zfs_prop_get_table();
4737 const char *fmt = gettext("%-16s %-14s\t%s\n");
4738
4739 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4740 HELP_ALLOW));
4741 (void) fprintf(fp, gettext("Options:\n"));
4742 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4743 const char *opt = opt_desc[i++];
4744 const char *optdsc = opt_desc[i];
4745 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4746 }
4747
4748 (void) fprintf(fp, gettext("\nThe following permissions are "
4749 "supported:\n\n"));
4750 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4751 gettext("NOTES"));
4752 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4753 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4754 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4755 const char *perm_type = deleg_perm_type(perm_note);
4756 const char *perm_comment = deleg_perm_comment(perm_note);
4757 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4758 }
4759
4760 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4761 zprop_desc_t *pd = &pdtbl[i];
4762 if (pd->pd_visible != B_TRUE)
4763 continue;
4764
4765 if (pd->pd_attr == PROP_READONLY)
4766 continue;
4767
4768 props[count++] = pd->pd_name;
4769 }
4770 props[count] = NULL;
4771
4772 qsort(props, count, sizeof (char *), prop_cmp);
4773
4774 for (i = 0; i < count; i++)
4775 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4776
4777 if (msg != NULL)
4778 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4779
4780 exit(requested ? 0 : 2);
4781 }
4782
4783 static inline const char *
4784 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4785 char **permsp)
4786 {
4787 if (un && argc == expected_argc - 1)
4788 *permsp = NULL;
4789 else if (argc == expected_argc)
4790 *permsp = argv[argc - 2];
4791 else
4792 allow_usage(un, B_FALSE,
4793 gettext("wrong number of parameters\n"));
4794
4795 return (argv[argc - 1]);
4796 }
4797
4798 static void
4799 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4800 {
4801 int uge_sum = opts->user + opts->group + opts->everyone;
4802 int csuge_sum = opts->create + opts->set + uge_sum;
4803 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4804 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4805
4806 if (uge_sum > 1)
4807 allow_usage(un, B_FALSE,
4808 gettext("-u, -g, and -e are mutually exclusive\n"));
4809
4810 if (opts->prt_usage) {
4811 if (argc == 0 && all_sum == 0)
4812 allow_usage(un, B_TRUE, NULL);
4813 else
4814 usage(B_FALSE);
4815 }
4816
4817 if (opts->set) {
4818 if (csuge_sum > 1)
4819 allow_usage(un, B_FALSE,
4820 gettext("invalid options combined with -s\n"));
4821
4822 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4823 if (argv[0][0] != '@')
4824 allow_usage(un, B_FALSE,
4825 gettext("invalid set name: missing '@' prefix\n"));
4826 opts->who = argv[0];
4827 } else if (opts->create) {
4828 if (ldcsuge_sum > 1)
4829 allow_usage(un, B_FALSE,
4830 gettext("invalid options combined with -c\n"));
4831 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4832 } else if (opts->everyone) {
4833 if (csuge_sum > 1)
4834 allow_usage(un, B_FALSE,
4835 gettext("invalid options combined with -e\n"));
4836 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4837 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4838 == 0) {
4839 opts->everyone = B_TRUE;
4840 argc--;
4841 argv++;
4842 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4843 } else if (argc == 1 && !un) {
4844 opts->prt_perms = B_TRUE;
4845 opts->dataset = argv[argc-1];
4846 } else {
4847 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4848 opts->who = argv[0];
4849 }
4850
4851 if (!opts->local && !opts->descend) {
4852 opts->local = B_TRUE;
4853 opts->descend = B_TRUE;
4854 }
4855 }
4856
4857 static void
4858 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4859 const char *who, char *perms, nvlist_t *top_nvl)
4860 {
4861 int i;
4862 char ld[2] = { '\0', '\0' };
4863 char who_buf[ZFS_MAXNAMELEN+32];
4864 char base_type = ZFS_DELEG_WHO_UNKNOWN;
4865 char set_type = ZFS_DELEG_WHO_UNKNOWN;
4866 nvlist_t *base_nvl = NULL;
4867 nvlist_t *set_nvl = NULL;
4868 nvlist_t *nvl;
4869
4870 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4871 nomem();
4872 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4873 nomem();
4874
4875 switch (type) {
4876 case ZFS_DELEG_NAMED_SET_SETS:
4877 case ZFS_DELEG_NAMED_SET:
4878 set_type = ZFS_DELEG_NAMED_SET_SETS;
4879 base_type = ZFS_DELEG_NAMED_SET;
4880 ld[0] = ZFS_DELEG_NA;
4881 break;
4882 case ZFS_DELEG_CREATE_SETS:
4883 case ZFS_DELEG_CREATE:
4884 set_type = ZFS_DELEG_CREATE_SETS;
4885 base_type = ZFS_DELEG_CREATE;
4886 ld[0] = ZFS_DELEG_NA;
4887 break;
4888 case ZFS_DELEG_USER_SETS:
4889 case ZFS_DELEG_USER:
4890 set_type = ZFS_DELEG_USER_SETS;
4891 base_type = ZFS_DELEG_USER;
4892 if (local)
4893 ld[0] = ZFS_DELEG_LOCAL;
4894 if (descend)
4895 ld[1] = ZFS_DELEG_DESCENDENT;
4896 break;
4897 case ZFS_DELEG_GROUP_SETS:
4898 case ZFS_DELEG_GROUP:
4899 set_type = ZFS_DELEG_GROUP_SETS;
4900 base_type = ZFS_DELEG_GROUP;
4901 if (local)
4902 ld[0] = ZFS_DELEG_LOCAL;
4903 if (descend)
4904 ld[1] = ZFS_DELEG_DESCENDENT;
4905 break;
4906 case ZFS_DELEG_EVERYONE_SETS:
4907 case ZFS_DELEG_EVERYONE:
4908 set_type = ZFS_DELEG_EVERYONE_SETS;
4909 base_type = ZFS_DELEG_EVERYONE;
4910 if (local)
4911 ld[0] = ZFS_DELEG_LOCAL;
4912 if (descend)
4913 ld[1] = ZFS_DELEG_DESCENDENT;
4914 default:
4915 break;
4916 }
4917
4918 if (perms != NULL) {
4919 char *curr = perms;
4920 char *end = curr + strlen(perms);
4921
4922 while (curr < end) {
4923 char *delim = strchr(curr, ',');
4924 if (delim == NULL)
4925 delim = end;
4926 else
4927 *delim = '\0';
4928
4929 if (curr[0] == '@')
4930 nvl = set_nvl;
4931 else
4932 nvl = base_nvl;
4933
4934 (void) nvlist_add_boolean(nvl, curr);
4935 if (delim != end)
4936 *delim = ',';
4937 curr = delim + 1;
4938 }
4939
4940 for (i = 0; i < 2; i++) {
4941 char locality = ld[i];
4942 if (locality == 0)
4943 continue;
4944
4945 if (!nvlist_empty(base_nvl)) {
4946 if (who != NULL)
4947 (void) snprintf(who_buf,
4948 sizeof (who_buf), "%c%c$%s",
4949 base_type, locality, who);
4950 else
4951 (void) snprintf(who_buf,
4952 sizeof (who_buf), "%c%c$",
4953 base_type, locality);
4954
4955 (void) nvlist_add_nvlist(top_nvl, who_buf,
4956 base_nvl);
4957 }
4958
4959
4960 if (!nvlist_empty(set_nvl)) {
4961 if (who != NULL)
4962 (void) snprintf(who_buf,
4963 sizeof (who_buf), "%c%c$%s",
4964 set_type, locality, who);
4965 else
4966 (void) snprintf(who_buf,
4967 sizeof (who_buf), "%c%c$",
4968 set_type, locality);
4969
4970 (void) nvlist_add_nvlist(top_nvl, who_buf,
4971 set_nvl);
4972 }
4973 }
4974 } else {
4975 for (i = 0; i < 2; i++) {
4976 char locality = ld[i];
4977 if (locality == 0)
4978 continue;
4979
4980 if (who != NULL)
4981 (void) snprintf(who_buf, sizeof (who_buf),
4982 "%c%c$%s", base_type, locality, who);
4983 else
4984 (void) snprintf(who_buf, sizeof (who_buf),
4985 "%c%c$", base_type, locality);
4986 (void) nvlist_add_boolean(top_nvl, who_buf);
4987
4988 if (who != NULL)
4989 (void) snprintf(who_buf, sizeof (who_buf),
4990 "%c%c$%s", set_type, locality, who);
4991 else
4992 (void) snprintf(who_buf, sizeof (who_buf),
4993 "%c%c$", set_type, locality);
4994 (void) nvlist_add_boolean(top_nvl, who_buf);
4995 }
4996 }
4997 }
4998
4999 static int
5000 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5001 {
5002 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5003 nomem();
5004
5005 if (opts->set) {
5006 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5007 opts->descend, opts->who, opts->perms, *nvlp);
5008 } else if (opts->create) {
5009 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5010 opts->descend, NULL, opts->perms, *nvlp);
5011 } else if (opts->everyone) {
5012 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5013 opts->descend, NULL, opts->perms, *nvlp);
5014 } else {
5015 char *curr = opts->who;
5016 char *end = curr + strlen(curr);
5017
5018 while (curr < end) {
5019 const char *who;
5020 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5021 char *endch;
5022 char *delim = strchr(curr, ',');
5023 char errbuf[256];
5024 char id[64];
5025 struct passwd *p = NULL;
5026 struct group *g = NULL;
5027
5028 uid_t rid;
5029 if (delim == NULL)
5030 delim = end;
5031 else
5032 *delim = '\0';
5033
5034 rid = (uid_t)strtol(curr, &endch, 0);
5035 if (opts->user) {
5036 who_type = ZFS_DELEG_USER;
5037 if (*endch != '\0')
5038 p = getpwnam(curr);
5039 else
5040 p = getpwuid(rid);
5041
5042 if (p != NULL)
5043 rid = p->pw_uid;
5044 else {
5045 (void) snprintf(errbuf, 256, gettext(
5046 "invalid user %s"), curr);
5047 allow_usage(un, B_TRUE, errbuf);
5048 }
5049 } else if (opts->group) {
5050 who_type = ZFS_DELEG_GROUP;
5051 if (*endch != '\0')
5052 g = getgrnam(curr);
5053 else
5054 g = getgrgid(rid);
5055
5056 if (g != NULL)
5057 rid = g->gr_gid;
5058 else {
5059 (void) snprintf(errbuf, 256, gettext(
5060 "invalid group %s"), curr);
5061 allow_usage(un, B_TRUE, errbuf);
5062 }
5063 } else {
5064 if (*endch != '\0') {
5065 p = getpwnam(curr);
5066 } else {
5067 p = getpwuid(rid);
5068 }
5069
5070 if (p == NULL) {
5071 if (*endch != '\0') {
5072 g = getgrnam(curr);
5073 } else {
5074 g = getgrgid(rid);
5075 }
5076 }
5077
5078 if (p != NULL) {
5079 who_type = ZFS_DELEG_USER;
5080 rid = p->pw_uid;
5081 } else if (g != NULL) {
5082 who_type = ZFS_DELEG_GROUP;
5083 rid = g->gr_gid;
5084 } else {
5085 (void) snprintf(errbuf, 256, gettext(
5086 "invalid user/group %s"), curr);
5087 allow_usage(un, B_TRUE, errbuf);
5088 }
5089 }
5090
5091 (void) sprintf(id, "%u", rid);
5092 who = id;
5093
5094 store_allow_perm(who_type, opts->local,
5095 opts->descend, who, opts->perms, *nvlp);
5096 curr = delim + 1;
5097 }
5098 }
5099
5100 return (0);
5101 }
5102
5103 static void
5104 print_set_creat_perms(uu_avl_t *who_avl)
5105 {
5106 const char *sc_title[] = {
5107 gettext("Permission sets:\n"),
5108 gettext("Create time permissions:\n"),
5109 NULL
5110 };
5111 const char **title_ptr = sc_title;
5112 who_perm_node_t *who_node = NULL;
5113 int prev_weight = -1;
5114
5115 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5116 who_node = uu_avl_next(who_avl, who_node)) {
5117 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5118 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5119 const char *who_name = who_node->who_perm.who_name;
5120 int weight = who_type2weight(who_type);
5121 boolean_t first = B_TRUE;
5122 deleg_perm_node_t *deleg_node;
5123
5124 if (prev_weight != weight) {
5125 (void) printf("%s", *title_ptr++);
5126 prev_weight = weight;
5127 }
5128
5129 if (who_name == NULL || strnlen(who_name, 1) == 0)
5130 (void) printf("\t");
5131 else
5132 (void) printf("\t%s ", who_name);
5133
5134 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5135 deleg_node = uu_avl_next(avl, deleg_node)) {
5136 if (first) {
5137 (void) printf("%s",
5138 deleg_node->dpn_perm.dp_name);
5139 first = B_FALSE;
5140 } else
5141 (void) printf(",%s",
5142 deleg_node->dpn_perm.dp_name);
5143 }
5144
5145 (void) printf("\n");
5146 }
5147 }
5148
5149 static void inline
5150 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5151 const char *title)
5152 {
5153 who_perm_node_t *who_node = NULL;
5154 boolean_t prt_title = B_TRUE;
5155 uu_avl_walk_t *walk;
5156
5157 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5158 nomem();
5159
5160 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5161 const char *who_name = who_node->who_perm.who_name;
5162 const char *nice_who_name = who_node->who_perm.who_ug_name;
5163 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5164 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5165 char delim = ' ';
5166 deleg_perm_node_t *deleg_node;
5167 boolean_t prt_who = B_TRUE;
5168
5169 for (deleg_node = uu_avl_first(avl);
5170 deleg_node != NULL;
5171 deleg_node = uu_avl_next(avl, deleg_node)) {
5172 if (local != deleg_node->dpn_perm.dp_local ||
5173 descend != deleg_node->dpn_perm.dp_descend)
5174 continue;
5175
5176 if (prt_who) {
5177 const char *who = NULL;
5178 if (prt_title) {
5179 prt_title = B_FALSE;
5180 (void) printf("%s", title);
5181 }
5182
5183 switch (who_type) {
5184 case ZFS_DELEG_USER_SETS:
5185 case ZFS_DELEG_USER:
5186 who = gettext("user");
5187 if (nice_who_name)
5188 who_name = nice_who_name;
5189 break;
5190 case ZFS_DELEG_GROUP_SETS:
5191 case ZFS_DELEG_GROUP:
5192 who = gettext("group");
5193 if (nice_who_name)
5194 who_name = nice_who_name;
5195 break;
5196 case ZFS_DELEG_EVERYONE_SETS:
5197 case ZFS_DELEG_EVERYONE:
5198 who = gettext("everyone");
5199 who_name = NULL;
5200 default:
5201 break;
5202 }
5203
5204 prt_who = B_FALSE;
5205 if (who_name == NULL)
5206 (void) printf("\t%s", who);
5207 else
5208 (void) printf("\t%s %s", who, who_name);
5209 }
5210
5211 (void) printf("%c%s", delim,
5212 deleg_node->dpn_perm.dp_name);
5213 delim = ',';
5214 }
5215
5216 if (!prt_who)
5217 (void) printf("\n");
5218 }
5219
5220 uu_avl_walk_end(walk);
5221 }
5222
5223 static void
5224 print_fs_perms(fs_perm_set_t *fspset)
5225 {
5226 fs_perm_node_t *node = NULL;
5227 char buf[ZFS_MAXNAMELEN+32];
5228 const char *dsname = buf;
5229
5230 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5231 node = uu_list_next(fspset->fsps_list, node)) {
5232 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5233 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5234 int left = 0;
5235
5236 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5237 gettext("---- Permissions on %s "),
5238 node->fspn_fsperm.fsp_name);
5239 (void) printf("%s", dsname);
5240 left = 70 - strlen(buf);
5241 while (left-- > 0)
5242 (void) printf("-");
5243 (void) printf("\n");
5244
5245 print_set_creat_perms(sc_avl);
5246 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5247 gettext("Local permissions:\n"));
5248 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5249 gettext("Descendent permissions:\n"));
5250 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5251 gettext("Local+Descendent permissions:\n"));
5252 }
5253 }
5254
5255 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5256
5257 struct deleg_perms {
5258 boolean_t un;
5259 nvlist_t *nvl;
5260 };
5261
5262 static int
5263 set_deleg_perms(zfs_handle_t *zhp, void *data)
5264 {
5265 struct deleg_perms *perms = (struct deleg_perms *)data;
5266 zfs_type_t zfs_type = zfs_get_type(zhp);
5267
5268 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5269 return (0);
5270
5271 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5272 }
5273
5274 static int
5275 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5276 {
5277 zfs_handle_t *zhp;
5278 nvlist_t *perm_nvl = NULL;
5279 nvlist_t *update_perm_nvl = NULL;
5280 int error = 1;
5281 int c;
5282 struct allow_opts opts = { 0 };
5283
5284 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5285
5286 /* check opts */
5287 while ((c = getopt(argc, argv, optstr)) != -1) {
5288 switch (c) {
5289 case 'l':
5290 opts.local = B_TRUE;
5291 break;
5292 case 'd':
5293 opts.descend = B_TRUE;
5294 break;
5295 case 'u':
5296 opts.user = B_TRUE;
5297 break;
5298 case 'g':
5299 opts.group = B_TRUE;
5300 break;
5301 case 'e':
5302 opts.everyone = B_TRUE;
5303 break;
5304 case 's':
5305 opts.set = B_TRUE;
5306 break;
5307 case 'c':
5308 opts.create = B_TRUE;
5309 break;
5310 case 'r':
5311 opts.recursive = B_TRUE;
5312 break;
5313 case ':':
5314 (void) fprintf(stderr, gettext("missing argument for "
5315 "'%c' option\n"), optopt);
5316 usage(B_FALSE);
5317 break;
5318 case 'h':
5319 opts.prt_usage = B_TRUE;
5320 break;
5321 case '?':
5322 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5323 optopt);
5324 usage(B_FALSE);
5325 }
5326 }
5327
5328 argc -= optind;
5329 argv += optind;
5330
5331 /* check arguments */
5332 parse_allow_args(argc, argv, un, &opts);
5333
5334 /* try to open the dataset */
5335 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5336 ZFS_TYPE_VOLUME)) == NULL) {
5337 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5338 opts.dataset);
5339 return (-1);
5340 }
5341
5342 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5343 goto cleanup2;
5344
5345 fs_perm_set_init(&fs_perm_set);
5346 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5347 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5348 goto cleanup1;
5349 }
5350
5351 if (opts.prt_perms)
5352 print_fs_perms(&fs_perm_set);
5353 else {
5354 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5355 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5356 goto cleanup0;
5357
5358 if (un && opts.recursive) {
5359 struct deleg_perms data = { un, update_perm_nvl };
5360 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5361 &data) != 0)
5362 goto cleanup0;
5363 }
5364 }
5365
5366 error = 0;
5367
5368 cleanup0:
5369 nvlist_free(perm_nvl);
5370 nvlist_free(update_perm_nvl);
5371 cleanup1:
5372 fs_perm_set_fini(&fs_perm_set);
5373 cleanup2:
5374 zfs_close(zhp);
5375
5376 return (error);
5377 }
5378
5379 static int
5380 zfs_do_allow(int argc, char **argv)
5381 {
5382 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5383 }
5384
5385 static int
5386 zfs_do_unallow(int argc, char **argv)
5387 {
5388 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5389 }
5390
5391 static int
5392 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5393 {
5394 int errors = 0;
5395 int i;
5396 const char *tag;
5397 boolean_t recursive = B_FALSE;
5398 const char *opts = holding ? "rt" : "r";
5399 int c;
5400
5401 /* check options */
5402 while ((c = getopt(argc, argv, opts)) != -1) {
5403 switch (c) {
5404 case 'r':
5405 recursive = B_TRUE;
5406 break;
5407 case '?':
5408 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5409 optopt);
5410 usage(B_FALSE);
5411 }
5412 }
5413
5414 argc -= optind;
5415 argv += optind;
5416
5417 /* check number of arguments */
5418 if (argc < 2)
5419 usage(B_FALSE);
5420
5421 tag = argv[0];
5422 --argc;
5423 ++argv;
5424
5425 if (holding && tag[0] == '.') {
5426 /* tags starting with '.' are reserved for libzfs */
5427 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5428 usage(B_FALSE);
5429 }
5430
5431 for (i = 0; i < argc; ++i) {
5432 zfs_handle_t *zhp;
5433 char parent[ZFS_MAXNAMELEN];
5434 const char *delim;
5435 char *path = argv[i];
5436
5437 delim = strchr(path, '@');
5438 if (delim == NULL) {
5439 (void) fprintf(stderr,
5440 gettext("'%s' is not a snapshot\n"), path);
5441 ++errors;
5442 continue;
5443 }
5444 (void) strncpy(parent, path, delim - path);
5445 parent[delim - path] = '\0';
5446
5447 zhp = zfs_open(g_zfs, parent,
5448 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5449 if (zhp == NULL) {
5450 ++errors;
5451 continue;
5452 }
5453 if (holding) {
5454 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5455 ++errors;
5456 } else {
5457 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5458 ++errors;
5459 }
5460 zfs_close(zhp);
5461 }
5462
5463 return (errors != 0);
5464 }
5465
5466 /*
5467 * zfs hold [-r] [-t] <tag> <snap> ...
5468 *
5469 * -r Recursively hold
5470 *
5471 * Apply a user-hold with the given tag to the list of snapshots.
5472 */
5473 static int
5474 zfs_do_hold(int argc, char **argv)
5475 {
5476 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5477 }
5478
5479 /*
5480 * zfs release [-r] <tag> <snap> ...
5481 *
5482 * -r Recursively release
5483 *
5484 * Release a user-hold with the given tag from the list of snapshots.
5485 */
5486 static int
5487 zfs_do_release(int argc, char **argv)
5488 {
5489 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5490 }
5491
5492 typedef struct holds_cbdata {
5493 boolean_t cb_recursive;
5494 const char *cb_snapname;
5495 nvlist_t **cb_nvlp;
5496 size_t cb_max_namelen;
5497 size_t cb_max_taglen;
5498 } holds_cbdata_t;
5499
5500 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5501 #define DATETIME_BUF_LEN (32)
5502 /*
5503 *
5504 */
5505 static void
5506 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5507 {
5508 int i;
5509 nvpair_t *nvp = NULL;
5510 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5511 const char *col;
5512
5513 if (!scripted) {
5514 for (i = 0; i < 3; i++) {
5515 col = gettext(hdr_cols[i]);
5516 if (i < 2)
5517 (void) printf("%-*s ", i ? tagwidth : nwidth,
5518 col);
5519 else
5520 (void) printf("%s\n", col);
5521 }
5522 }
5523
5524 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5525 char *zname = nvpair_name(nvp);
5526 nvlist_t *nvl2;
5527 nvpair_t *nvp2 = NULL;
5528 (void) nvpair_value_nvlist(nvp, &nvl2);
5529 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5530 char tsbuf[DATETIME_BUF_LEN];
5531 char *tagname = nvpair_name(nvp2);
5532 uint64_t val = 0;
5533 time_t time;
5534 struct tm t;
5535 char sep = scripted ? '\t' : ' ';
5536 int sepnum = scripted ? 1 : 2;
5537
5538 (void) nvpair_value_uint64(nvp2, &val);
5539 time = (time_t)val;
5540 (void) localtime_r(&time, &t);
5541 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5542 gettext(STRFTIME_FMT_STR), &t);
5543
5544 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5545 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5546 }
5547 }
5548 }
5549
5550 /*
5551 * Generic callback function to list a dataset or snapshot.
5552 */
5553 static int
5554 holds_callback(zfs_handle_t *zhp, void *data)
5555 {
5556 holds_cbdata_t *cbp = data;
5557 nvlist_t *top_nvl = *cbp->cb_nvlp;
5558 nvlist_t *nvl = NULL;
5559 nvpair_t *nvp = NULL;
5560 const char *zname = zfs_get_name(zhp);
5561 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5562
5563 if (cbp->cb_recursive) {
5564 const char *snapname;
5565 char *delim = strchr(zname, '@');
5566 if (delim == NULL)
5567 return (0);
5568
5569 snapname = delim + 1;
5570 if (strcmp(cbp->cb_snapname, snapname))
5571 return (0);
5572 }
5573
5574 if (zfs_get_holds(zhp, &nvl) != 0)
5575 return (-1);
5576
5577 if (znamelen > cbp->cb_max_namelen)
5578 cbp->cb_max_namelen = znamelen;
5579
5580 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5581 const char *tag = nvpair_name(nvp);
5582 size_t taglen = strnlen(tag, MAXNAMELEN);
5583 if (taglen > cbp->cb_max_taglen)
5584 cbp->cb_max_taglen = taglen;
5585 }
5586
5587 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5588 }
5589
5590 /*
5591 * zfs holds [-r] <snap> ...
5592 *
5593 * -r Recursively hold
5594 */
5595 static int
5596 zfs_do_holds(int argc, char **argv)
5597 {
5598 int errors = 0;
5599 int c;
5600 int i;
5601 boolean_t scripted = B_FALSE;
5602 boolean_t recursive = B_FALSE;
5603 const char *opts = "rH";
5604 nvlist_t *nvl;
5605
5606 int types = ZFS_TYPE_SNAPSHOT;
5607 holds_cbdata_t cb = { 0 };
5608
5609 int limit = 0;
5610 int ret = 0;
5611 int flags = 0;
5612
5613 /* check options */
5614 while ((c = getopt(argc, argv, opts)) != -1) {
5615 switch (c) {
5616 case 'r':
5617 recursive = B_TRUE;
5618 break;
5619 case 'H':
5620 scripted = B_TRUE;
5621 break;
5622 case '?':
5623 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5624 optopt);
5625 usage(B_FALSE);
5626 }
5627 }
5628
5629 if (recursive) {
5630 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5631 flags |= ZFS_ITER_RECURSE;
5632 }
5633
5634 argc -= optind;
5635 argv += optind;
5636
5637 /* check number of arguments */
5638 if (argc < 1)
5639 usage(B_FALSE);
5640
5641 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5642 nomem();
5643
5644 for (i = 0; i < argc; ++i) {
5645 char *snapshot = argv[i];
5646 const char *delim;
5647 const char *snapname;
5648
5649 delim = strchr(snapshot, '@');
5650 if (delim == NULL) {
5651 (void) fprintf(stderr,
5652 gettext("'%s' is not a snapshot\n"), snapshot);
5653 ++errors;
5654 continue;
5655 }
5656 snapname = delim + 1;
5657 if (recursive)
5658 snapshot[delim - snapshot] = '\0';
5659
5660 cb.cb_recursive = recursive;
5661 cb.cb_snapname = snapname;
5662 cb.cb_nvlp = &nvl;
5663
5664 /*
5665 * 1. collect holds data, set format options
5666 */
5667 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5668 holds_callback, &cb);
5669 if (ret != 0)
5670 ++errors;
5671 }
5672
5673 /*
5674 * 2. print holds data
5675 */
5676 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5677
5678 if (nvlist_empty(nvl))
5679 (void) fprintf(stderr, gettext("no datasets available\n"));
5680
5681 nvlist_free(nvl);
5682
5683 return (0 != errors);
5684 }
5685
5686 #define CHECK_SPINNER 30
5687 #define SPINNER_TIME 3 /* seconds */
5688 #define MOUNT_TIME 5 /* seconds */
5689
5690 static int
5691 get_one_dataset(zfs_handle_t *zhp, void *data)
5692 {
5693 static char *spin[] = { "-", "\\", "|", "/" };
5694 static int spinval = 0;
5695 static int spincheck = 0;
5696 static time_t last_spin_time = (time_t)0;
5697 get_all_cb_t *cbp = data;
5698 zfs_type_t type = zfs_get_type(zhp);
5699
5700 if (cbp->cb_verbose) {
5701 if (--spincheck < 0) {
5702 time_t now = time(NULL);
5703 if (last_spin_time + SPINNER_TIME < now) {
5704 update_progress(spin[spinval++ % 4]);
5705 last_spin_time = now;
5706 }
5707 spincheck = CHECK_SPINNER;
5708 }
5709 }
5710
5711 /*
5712 * Iterate over any nested datasets.
5713 */
5714 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5715 zfs_close(zhp);
5716 return (1);
5717 }
5718
5719 /*
5720 * Skip any datasets whose type does not match.
5721 */
5722 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5723 zfs_close(zhp);
5724 return (0);
5725 }
5726 libzfs_add_handle(cbp, zhp);
5727 assert(cbp->cb_used <= cbp->cb_alloc);
5728
5729 return (0);
5730 }
5731
5732 static void
5733 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5734 {
5735 get_all_cb_t cb = { 0 };
5736 cb.cb_verbose = verbose;
5737 cb.cb_getone = get_one_dataset;
5738
5739 if (verbose)
5740 set_progress_header(gettext("Reading ZFS config"));
5741 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5742
5743 *dslist = cb.cb_handles;
5744 *count = cb.cb_used;
5745
5746 if (verbose)
5747 finish_progress(gettext("done."));
5748 }
5749
5750 /*
5751 * Generic callback for sharing or mounting filesystems. Because the code is so
5752 * similar, we have a common function with an extra parameter to determine which
5753 * mode we are using.
5754 */
5755 #define OP_SHARE 0x1
5756 #define OP_MOUNT 0x2
5757
5758 /*
5759 * Share or mount a dataset.
5760 */
5761 static int
5762 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5763 boolean_t explicit, const char *options)
5764 {
5765 char mountpoint[ZFS_MAXPROPLEN];
5766 char shareopts[ZFS_MAXPROPLEN];
5767 char smbshareopts[ZFS_MAXPROPLEN];
5768 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5769 struct mnttab mnt;
5770 uint64_t zoned, canmount;
5771 boolean_t shared_nfs, shared_smb;
5772
5773 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5774
5775 /*
5776 * Check to make sure we can mount/share this dataset. If we
5777 * are in the global zone and the filesystem is exported to a
5778 * local zone, or if we are in a local zone and the
5779 * filesystem is not exported, then it is an error.
5780 */
5781 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5782
5783 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5784 if (!explicit)
5785 return (0);
5786
5787 (void) fprintf(stderr, gettext("cannot %s '%s': "
5788 "dataset is exported to a local zone\n"), cmdname,
5789 zfs_get_name(zhp));
5790 return (1);
5791
5792 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5793 if (!explicit)
5794 return (0);
5795
5796 (void) fprintf(stderr, gettext("cannot %s '%s': "
5797 "permission denied\n"), cmdname,
5798 zfs_get_name(zhp));
5799 return (1);
5800 }
5801
5802 /*
5803 * Ignore any filesystems which don't apply to us. This
5804 * includes those with a legacy mountpoint, or those with
5805 * legacy share options.
5806 */
5807 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5808 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5809 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5810 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5811 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5812 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5813
5814 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5815 strcmp(smbshareopts, "off") == 0) {
5816 if (!explicit)
5817 return (0);
5818
5819 (void) fprintf(stderr, gettext("cannot share '%s': "
5820 "legacy share\n"), zfs_get_name(zhp));
5821 (void) fprintf(stderr, gettext("use share(1M) to "
5822 "share this filesystem, or set "
5823 "sharenfs property on\n"));
5824 return (1);
5825 }
5826
5827 /*
5828 * We cannot share or mount legacy filesystems. If the
5829 * shareopts is non-legacy but the mountpoint is legacy, we
5830 * treat it as a legacy share.
5831 */
5832 if (strcmp(mountpoint, "legacy") == 0) {
5833 if (!explicit)
5834 return (0);
5835
5836 (void) fprintf(stderr, gettext("cannot %s '%s': "
5837 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5838 (void) fprintf(stderr, gettext("use %s(1M) to "
5839 "%s this filesystem\n"), cmdname, cmdname);
5840 return (1);
5841 }
5842
5843 if (strcmp(mountpoint, "none") == 0) {
5844 if (!explicit)
5845 return (0);
5846
5847 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5848 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5849 return (1);
5850 }
5851
5852 /*
5853 * canmount explicit outcome
5854 * on no pass through
5855 * on yes pass through
5856 * off no return 0
5857 * off yes display error, return 1
5858 * noauto no return 0
5859 * noauto yes pass through
5860 */
5861 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5862 if (canmount == ZFS_CANMOUNT_OFF) {
5863 if (!explicit)
5864 return (0);
5865
5866 (void) fprintf(stderr, gettext("cannot %s '%s': "
5867 "'canmount' property is set to 'off'\n"), cmdname,
5868 zfs_get_name(zhp));
5869 return (1);
5870 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5871 return (0);
5872 }
5873
5874 /*
5875 * If this filesystem is inconsistent and has a receive resume
5876 * token, we can not mount it.
5877 */
5878 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5879 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5880 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5881 if (!explicit)
5882 return (0);
5883
5884 (void) fprintf(stderr, gettext("cannot %s '%s': "
5885 "Contains partially-completed state from "
5886 "\"zfs receive -r\", which can be resumed with "
5887 "\"zfs send -t\"\n"),
5888 cmdname, zfs_get_name(zhp));
5889 return (1);
5890 }
5891
5892 /*
5893 * At this point, we have verified that the mountpoint and/or
5894 * shareopts are appropriate for auto management. If the
5895 * filesystem is already mounted or shared, return (failing
5896 * for explicit requests); otherwise mount or share the
5897 * filesystem.
5898 */
5899 switch (op) {
5900 case OP_SHARE:
5901
5902 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5903 shared_smb = zfs_is_shared_smb(zhp, NULL);
5904
5905 if ((shared_nfs && shared_smb) ||
5906 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
5907 (strcmp(smbshareopts, "off") == 0)) ||
5908 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
5909 (strcmp(shareopts, "off") == 0))) {
5910 if (!explicit)
5911 return (0);
5912
5913 (void) fprintf(stderr, gettext("cannot share "
5914 "'%s': filesystem already shared\n"),
5915 zfs_get_name(zhp));
5916 return (1);
5917 }
5918
5919 if (!zfs_is_mounted(zhp, NULL) &&
5920 zfs_mount(zhp, NULL, 0) != 0)
5921 return (1);
5922
5923 if (protocol == NULL) {
5924 if (zfs_shareall(zhp) != 0)
5925 return (1);
5926 } else if (strcmp(protocol, "nfs") == 0) {
5927 if (zfs_share_nfs(zhp))
5928 return (1);
5929 } else if (strcmp(protocol, "smb") == 0) {
5930 if (zfs_share_smb(zhp))
5931 return (1);
5932 } else {
5933 (void) fprintf(stderr, gettext("cannot share "
5934 "'%s': invalid share type '%s' "
5935 "specified\n"),
5936 zfs_get_name(zhp), protocol);
5937 return (1);
5938 }
5939
5940 break;
5941
5942 case OP_MOUNT:
5943 if (options == NULL)
5944 mnt.mnt_mntopts = "";
5945 else
5946 mnt.mnt_mntopts = (char *)options;
5947
5948 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5949 zfs_is_mounted(zhp, NULL)) {
5950 if (!explicit)
5951 return (0);
5952
5953 (void) fprintf(stderr, gettext("cannot mount "
5954 "'%s': filesystem already mounted\n"),
5955 zfs_get_name(zhp));
5956 return (1);
5957 }
5958
5959 if (zfs_mount(zhp, options, flags) != 0)
5960 return (1);
5961 break;
5962 }
5963
5964 return (0);
5965 }
5966
5967 /*
5968 * Reports progress in the form "(current/total)". Not thread-safe.
5969 */
5970 static void
5971 report_mount_progress(int current, int total)
5972 {
5973 static time_t last_progress_time = 0;
5974 time_t now = time(NULL);
5975 char info[32];
5976
5977 /* report 1..n instead of 0..n-1 */
5978 ++current;
5979
5980 /* display header if we're here for the first time */
5981 if (current == 1) {
5982 set_progress_header(gettext("Mounting ZFS filesystems"));
5983 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5984 /* too soon to report again */
5985 return;
5986 }
5987
5988 last_progress_time = now;
5989
5990 (void) sprintf(info, "(%d/%d)", current, total);
5991
5992 if (current == total)
5993 finish_progress(info);
5994 else
5995 update_progress(info);
5996 }
5997
5998 static void
5999 append_options(char *mntopts, char *newopts)
6000 {
6001 int len = strlen(mntopts);
6002
6003 /* original length plus new string to append plus 1 for the comma */
6004 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6005 (void) fprintf(stderr, gettext("the opts argument for "
6006 "'%s' option is too long (more than %d chars)\n"),
6007 "-o", MNT_LINE_MAX);
6008 usage(B_FALSE);
6009 }
6010
6011 if (*mntopts)
6012 mntopts[len++] = ',';
6013
6014 (void) strcpy(&mntopts[len], newopts);
6015 }
6016
6017 static int
6018 share_mount(int op, int argc, char **argv)
6019 {
6020 int do_all = 0;
6021 boolean_t verbose = B_FALSE;
6022 int c, ret = 0;
6023 char *options = NULL;
6024 int flags = 0;
6025
6026 /* check options */
6027 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6028 != -1) {
6029 switch (c) {
6030 case 'a':
6031 do_all = 1;
6032 break;
6033 case 'v':
6034 verbose = B_TRUE;
6035 break;
6036 case 'o':
6037 if (*optarg == '\0') {
6038 (void) fprintf(stderr, gettext("empty mount "
6039 "options (-o) specified\n"));
6040 usage(B_FALSE);
6041 }
6042
6043 if (options == NULL)
6044 options = safe_malloc(MNT_LINE_MAX + 1);
6045
6046 /* option validation is done later */
6047 append_options(options, optarg);
6048 break;
6049 case 'O':
6050 flags |= MS_OVERLAY;
6051 break;
6052 case ':':
6053 (void) fprintf(stderr, gettext("missing argument for "
6054 "'%c' option\n"), optopt);
6055 usage(B_FALSE);
6056 break;
6057 case '?':
6058 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6059 optopt);
6060 usage(B_FALSE);
6061 }
6062 }
6063
6064 argc -= optind;
6065 argv += optind;
6066
6067 /* check number of arguments */
6068 if (do_all) {
6069 zfs_handle_t **dslist = NULL;
6070 size_t i, count = 0;
6071 char *protocol = NULL;
6072
6073 if (op == OP_SHARE && argc > 0) {
6074 if (strcmp(argv[0], "nfs") != 0 &&
6075 strcmp(argv[0], "smb") != 0) {
6076 (void) fprintf(stderr, gettext("share type "
6077 "must be 'nfs' or 'smb'\n"));
6078 usage(B_FALSE);
6079 }
6080 protocol = argv[0];
6081 argc--;
6082 argv++;
6083 }
6084
6085 if (argc != 0) {
6086 (void) fprintf(stderr, gettext("too many arguments\n"));
6087 usage(B_FALSE);
6088 }
6089
6090 start_progress_timer();
6091 get_all_datasets(&dslist, &count, verbose);
6092
6093 if (count == 0)
6094 return (0);
6095
6096 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6097
6098 for (i = 0; i < count; i++) {
6099 if (verbose)
6100 report_mount_progress(i, count);
6101
6102 if (share_mount_one(dslist[i], op, flags, protocol,
6103 B_FALSE, options) != 0)
6104 ret = 1;
6105 zfs_close(dslist[i]);
6106 }
6107
6108 free(dslist);
6109 } else if (argc == 0) {
6110 struct mnttab entry;
6111
6112 if ((op == OP_SHARE) || (options != NULL)) {
6113 (void) fprintf(stderr, gettext("missing filesystem "
6114 "argument (specify -a for all)\n"));
6115 usage(B_FALSE);
6116 }
6117
6118 /*
6119 * When mount is given no arguments, go through /etc/mtab and
6120 * display any active ZFS mounts. We hide any snapshots, since
6121 * they are controlled automatically.
6122 */
6123
6124 /* Reopen MNTTAB to prevent reading stale data from open file */
6125 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6126 return (ENOENT);
6127
6128 while (getmntent(mnttab_file, &entry) == 0) {
6129 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6130 strchr(entry.mnt_special, '@') != NULL)
6131 continue;
6132
6133 (void) printf("%-30s %s\n", entry.mnt_special,
6134 entry.mnt_mountp);
6135 }
6136
6137 } else {
6138 zfs_handle_t *zhp;
6139
6140 if (argc > 1) {
6141 (void) fprintf(stderr,
6142 gettext("too many arguments\n"));
6143 usage(B_FALSE);
6144 }
6145
6146 if ((zhp = zfs_open(g_zfs, argv[0],
6147 ZFS_TYPE_FILESYSTEM)) == NULL) {
6148 ret = 1;
6149 } else {
6150 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6151 options);
6152 zfs_close(zhp);
6153 }
6154 }
6155
6156 return (ret);
6157 }
6158
6159 /*
6160 * zfs mount -a [nfs]
6161 * zfs mount filesystem
6162 *
6163 * Mount all filesystems, or mount the given filesystem.
6164 */
6165 static int
6166 zfs_do_mount(int argc, char **argv)
6167 {
6168 return (share_mount(OP_MOUNT, argc, argv));
6169 }
6170
6171 /*
6172 * zfs share -a [nfs | smb]
6173 * zfs share filesystem
6174 *
6175 * Share all filesystems, or share the given filesystem.
6176 */
6177 static int
6178 zfs_do_share(int argc, char **argv)
6179 {
6180 return (share_mount(OP_SHARE, argc, argv));
6181 }
6182
6183 typedef struct unshare_unmount_node {
6184 zfs_handle_t *un_zhp;
6185 char *un_mountp;
6186 uu_avl_node_t un_avlnode;
6187 } unshare_unmount_node_t;
6188
6189 /* ARGSUSED */
6190 static int
6191 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6192 {
6193 const unshare_unmount_node_t *l = larg;
6194 const unshare_unmount_node_t *r = rarg;
6195
6196 return (strcmp(l->un_mountp, r->un_mountp));
6197 }
6198
6199 /*
6200 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6201 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
6202 * and unmount it appropriately.
6203 */
6204 static int
6205 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6206 {
6207 zfs_handle_t *zhp;
6208 int ret = 0;
6209 struct stat64 statbuf;
6210 struct extmnttab entry;
6211 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6212 ino_t path_inode;
6213
6214 /*
6215 * Search for the path in /etc/mtab. Rather than looking for the
6216 * specific path, which can be fooled by non-standard paths (i.e. ".."
6217 * or "//"), we stat() the path and search for the corresponding
6218 * (major,minor) device pair.
6219 */
6220 if (stat64(path, &statbuf) != 0) {
6221 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6222 cmdname, path, strerror(errno));
6223 return (1);
6224 }
6225 path_inode = statbuf.st_ino;
6226
6227 /*
6228 * Search for the given (major,minor) pair in the mount table.
6229 */
6230
6231 /* Reopen MNTTAB to prevent reading stale data from open file */
6232 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6233 return (ENOENT);
6234
6235 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6236 if (entry.mnt_major == major(statbuf.st_dev) &&
6237 entry.mnt_minor == minor(statbuf.st_dev))
6238 break;
6239 }
6240 if (ret != 0) {
6241 if (op == OP_SHARE) {
6242 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6243 "currently mounted\n"), cmdname, path);
6244 return (1);
6245 }
6246 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
6247 path);
6248 if ((ret = umount2(path, flags)) != 0)
6249 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6250 strerror(errno));
6251 return (ret != 0);
6252 }
6253
6254 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6255 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6256 "filesystem\n"), cmdname, path);
6257 return (1);
6258 }
6259
6260 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6261 ZFS_TYPE_FILESYSTEM)) == NULL)
6262 return (1);
6263
6264 ret = 1;
6265 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6266 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6267 cmdname, path, strerror(errno));
6268 goto out;
6269 } else if (statbuf.st_ino != path_inode) {
6270 (void) fprintf(stderr, gettext("cannot "
6271 "%s '%s': not a mountpoint\n"), cmdname, path);
6272 goto out;
6273 }
6274
6275 if (op == OP_SHARE) {
6276 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6277 char smbshare_prop[ZFS_MAXPROPLEN];
6278
6279 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6280 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6281 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6282 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6283
6284 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6285 strcmp(smbshare_prop, "off") == 0) {
6286 (void) fprintf(stderr, gettext("cannot unshare "
6287 "'%s': legacy share\n"), path);
6288 (void) fprintf(stderr, gettext("use exportfs(8) "
6289 "or smbcontrol(1) to unshare this filesystem\n"));
6290 } else if (!zfs_is_shared(zhp)) {
6291 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6292 "not currently shared\n"), path);
6293 } else {
6294 ret = zfs_unshareall_bypath(zhp, path);
6295 }
6296 } else {
6297 char mtpt_prop[ZFS_MAXPROPLEN];
6298
6299 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6300 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6301
6302 if (is_manual) {
6303 ret = zfs_unmount(zhp, NULL, flags);
6304 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6305 (void) fprintf(stderr, gettext("cannot unmount "
6306 "'%s': legacy mountpoint\n"),
6307 zfs_get_name(zhp));
6308 (void) fprintf(stderr, gettext("use umount(8) "
6309 "to unmount this filesystem\n"));
6310 } else {
6311 ret = zfs_unmountall(zhp, flags);
6312 }
6313 }
6314
6315 out:
6316 zfs_close(zhp);
6317
6318 return (ret != 0);
6319 }
6320
6321 /*
6322 * Generic callback for unsharing or unmounting a filesystem.
6323 */
6324 static int
6325 unshare_unmount(int op, int argc, char **argv)
6326 {
6327 int do_all = 0;
6328 int flags = 0;
6329 int ret = 0;
6330 int c;
6331 zfs_handle_t *zhp;
6332 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6333 char sharesmb[ZFS_MAXPROPLEN];
6334
6335 /* check options */
6336 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6337 switch (c) {
6338 case 'a':
6339 do_all = 1;
6340 break;
6341 case 'f':
6342 flags = MS_FORCE;
6343 break;
6344 case '?':
6345 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6346 optopt);
6347 usage(B_FALSE);
6348 }
6349 }
6350
6351 argc -= optind;
6352 argv += optind;
6353
6354 if (do_all) {
6355 /*
6356 * We could make use of zfs_for_each() to walk all datasets in
6357 * the system, but this would be very inefficient, especially
6358 * since we would have to linearly search /etc/mtab for each
6359 * one. Instead, do one pass through /etc/mtab looking for
6360 * zfs entries and call zfs_unmount() for each one.
6361 *
6362 * Things get a little tricky if the administrator has created
6363 * mountpoints beneath other ZFS filesystems. In this case, we
6364 * have to unmount the deepest filesystems first. To accomplish
6365 * this, we place all the mountpoints in an AVL tree sorted by
6366 * the special type (dataset name), and walk the result in
6367 * reverse to make sure to get any snapshots first.
6368 */
6369 struct mnttab entry;
6370 uu_avl_pool_t *pool;
6371 uu_avl_t *tree = NULL;
6372 unshare_unmount_node_t *node;
6373 uu_avl_index_t idx;
6374 uu_avl_walk_t *walk;
6375
6376 if (argc != 0) {
6377 (void) fprintf(stderr, gettext("too many arguments\n"));
6378 usage(B_FALSE);
6379 }
6380
6381 if (((pool = uu_avl_pool_create("unmount_pool",
6382 sizeof (unshare_unmount_node_t),
6383 offsetof(unshare_unmount_node_t, un_avlnode),
6384 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6385 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6386 nomem();
6387
6388 /* Reopen MNTTAB to prevent reading stale data from open file */
6389 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6390 return (ENOENT);
6391
6392 while (getmntent(mnttab_file, &entry) == 0) {
6393
6394 /* ignore non-ZFS entries */
6395 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6396 continue;
6397
6398 /* ignore snapshots */
6399 if (strchr(entry.mnt_special, '@') != NULL)
6400 continue;
6401
6402 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6403 ZFS_TYPE_FILESYSTEM)) == NULL) {
6404 ret = 1;
6405 continue;
6406 }
6407
6408 switch (op) {
6409 case OP_SHARE:
6410 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6411 nfs_mnt_prop,
6412 sizeof (nfs_mnt_prop),
6413 NULL, NULL, 0, B_FALSE) == 0);
6414 if (strcmp(nfs_mnt_prop, "off") != 0)
6415 break;
6416 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6417 nfs_mnt_prop,
6418 sizeof (nfs_mnt_prop),
6419 NULL, NULL, 0, B_FALSE) == 0);
6420 if (strcmp(nfs_mnt_prop, "off") == 0)
6421 continue;
6422 break;
6423 case OP_MOUNT:
6424 /* Ignore legacy mounts */
6425 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6426 nfs_mnt_prop,
6427 sizeof (nfs_mnt_prop),
6428 NULL, NULL, 0, B_FALSE) == 0);
6429 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6430 continue;
6431 /* Ignore canmount=noauto mounts */
6432 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6433 ZFS_CANMOUNT_NOAUTO)
6434 continue;
6435 default:
6436 break;
6437 }
6438
6439 node = safe_malloc(sizeof (unshare_unmount_node_t));
6440 node->un_zhp = zhp;
6441 node->un_mountp = safe_strdup(entry.mnt_mountp);
6442
6443 uu_avl_node_init(node, &node->un_avlnode, pool);
6444
6445 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6446 uu_avl_insert(tree, node, idx);
6447 } else {
6448 zfs_close(node->un_zhp);
6449 free(node->un_mountp);
6450 free(node);
6451 }
6452 }
6453
6454 /*
6455 * Walk the AVL tree in reverse, unmounting each filesystem and
6456 * removing it from the AVL tree in the process.
6457 */
6458 if ((walk = uu_avl_walk_start(tree,
6459 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6460 nomem();
6461
6462 while ((node = uu_avl_walk_next(walk)) != NULL) {
6463 uu_avl_remove(tree, node);
6464
6465 switch (op) {
6466 case OP_SHARE:
6467 if (zfs_unshareall_bypath(node->un_zhp,
6468 node->un_mountp) != 0)
6469 ret = 1;
6470 break;
6471
6472 case OP_MOUNT:
6473 if (zfs_unmount(node->un_zhp,
6474 node->un_mountp, flags) != 0)
6475 ret = 1;
6476 break;
6477 }
6478
6479 zfs_close(node->un_zhp);
6480 free(node->un_mountp);
6481 free(node);
6482 }
6483
6484 uu_avl_walk_end(walk);
6485 uu_avl_destroy(tree);
6486 uu_avl_pool_destroy(pool);
6487
6488 } else {
6489 if (argc != 1) {
6490 if (argc == 0)
6491 (void) fprintf(stderr,
6492 gettext("missing filesystem argument\n"));
6493 else
6494 (void) fprintf(stderr,
6495 gettext("too many arguments\n"));
6496 usage(B_FALSE);
6497 }
6498
6499 /*
6500 * We have an argument, but it may be a full path or a ZFS
6501 * filesystem. Pass full paths off to unmount_path() (shared by
6502 * manual_unmount), otherwise open the filesystem and pass to
6503 * zfs_unmount().
6504 */
6505 if (argv[0][0] == '/')
6506 return (unshare_unmount_path(op, argv[0],
6507 flags, B_FALSE));
6508
6509 if ((zhp = zfs_open(g_zfs, argv[0],
6510 ZFS_TYPE_FILESYSTEM)) == NULL)
6511 return (1);
6512
6513 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6514 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6515 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6516 NULL, 0, B_FALSE) == 0);
6517
6518 switch (op) {
6519 case OP_SHARE:
6520 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6521 nfs_mnt_prop,
6522 sizeof (nfs_mnt_prop),
6523 NULL, NULL, 0, B_FALSE) == 0);
6524 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6525 sharesmb, sizeof (sharesmb), NULL, NULL,
6526 0, B_FALSE) == 0);
6527
6528 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6529 strcmp(sharesmb, "off") == 0) {
6530 (void) fprintf(stderr, gettext("cannot "
6531 "unshare '%s': legacy share\n"),
6532 zfs_get_name(zhp));
6533 (void) fprintf(stderr, gettext("use "
6534 "unshare(1M) to unshare this "
6535 "filesystem\n"));
6536 ret = 1;
6537 } else if (!zfs_is_shared(zhp)) {
6538 (void) fprintf(stderr, gettext("cannot "
6539 "unshare '%s': not currently "
6540 "shared\n"), zfs_get_name(zhp));
6541 ret = 1;
6542 } else if (zfs_unshareall(zhp) != 0) {
6543 ret = 1;
6544 }
6545 break;
6546
6547 case OP_MOUNT:
6548 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6549 (void) fprintf(stderr, gettext("cannot "
6550 "unmount '%s': legacy "
6551 "mountpoint\n"), zfs_get_name(zhp));
6552 (void) fprintf(stderr, gettext("use "
6553 "umount(1M) to unmount this "
6554 "filesystem\n"));
6555 ret = 1;
6556 } else if (!zfs_is_mounted(zhp, NULL)) {
6557 (void) fprintf(stderr, gettext("cannot "
6558 "unmount '%s': not currently "
6559 "mounted\n"),
6560 zfs_get_name(zhp));
6561 ret = 1;
6562 } else if (zfs_unmountall(zhp, flags) != 0) {
6563 ret = 1;
6564 }
6565 break;
6566 }
6567
6568 zfs_close(zhp);
6569 }
6570
6571 return (ret);
6572 }
6573
6574 /*
6575 * zfs unmount -a
6576 * zfs unmount filesystem
6577 *
6578 * Unmount all filesystems, or a specific ZFS filesystem.
6579 */
6580 static int
6581 zfs_do_unmount(int argc, char **argv)
6582 {
6583 return (unshare_unmount(OP_MOUNT, argc, argv));
6584 }
6585
6586 /*
6587 * zfs unshare -a
6588 * zfs unshare filesystem
6589 *
6590 * Unshare all filesystems, or a specific ZFS filesystem.
6591 */
6592 static int
6593 zfs_do_unshare(int argc, char **argv)
6594 {
6595 return (unshare_unmount(OP_SHARE, argc, argv));
6596 }
6597
6598 static int
6599 find_command_idx(char *command, int *idx)
6600 {
6601 int i;
6602
6603 for (i = 0; i < NCOMMAND; i++) {
6604 if (command_table[i].name == NULL)
6605 continue;
6606
6607 if (strcmp(command, command_table[i].name) == 0) {
6608 *idx = i;
6609 return (0);
6610 }
6611 }
6612 return (1);
6613 }
6614
6615 static int
6616 zfs_do_diff(int argc, char **argv)
6617 {
6618 zfs_handle_t *zhp;
6619 int flags = 0;
6620 char *tosnap = NULL;
6621 char *fromsnap = NULL;
6622 char *atp, *copy;
6623 int err = 0;
6624 int c;
6625
6626 while ((c = getopt(argc, argv, "FHt")) != -1) {
6627 switch (c) {
6628 case 'F':
6629 flags |= ZFS_DIFF_CLASSIFY;
6630 break;
6631 case 'H':
6632 flags |= ZFS_DIFF_PARSEABLE;
6633 break;
6634 case 't':
6635 flags |= ZFS_DIFF_TIMESTAMP;
6636 break;
6637 default:
6638 (void) fprintf(stderr,
6639 gettext("invalid option '%c'\n"), optopt);
6640 usage(B_FALSE);
6641 }
6642 }
6643
6644 argc -= optind;
6645 argv += optind;
6646
6647 if (argc < 1) {
6648 (void) fprintf(stderr,
6649 gettext("must provide at least one snapshot name\n"));
6650 usage(B_FALSE);
6651 }
6652
6653 if (argc > 2) {
6654 (void) fprintf(stderr, gettext("too many arguments\n"));
6655 usage(B_FALSE);
6656 }
6657
6658 fromsnap = argv[0];
6659 tosnap = (argc == 2) ? argv[1] : NULL;
6660
6661 copy = NULL;
6662 if (*fromsnap != '@')
6663 copy = strdup(fromsnap);
6664 else if (tosnap)
6665 copy = strdup(tosnap);
6666 if (copy == NULL)
6667 usage(B_FALSE);
6668
6669 if ((atp = strchr(copy, '@')))
6670 *atp = '\0';
6671
6672 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6673 return (1);
6674
6675 free(copy);
6676
6677 /*
6678 * Ignore SIGPIPE so that the library can give us
6679 * information on any failure
6680 */
6681 (void) sigignore(SIGPIPE);
6682
6683 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6684
6685 zfs_close(zhp);
6686
6687 return (err != 0);
6688 }
6689
6690 /*
6691 * zfs bookmark <fs@snap> <fs#bmark>
6692 *
6693 * Creates a bookmark with the given name from the given snapshot.
6694 */
6695 static int
6696 zfs_do_bookmark(int argc, char **argv)
6697 {
6698 char snapname[ZFS_MAXNAMELEN];
6699 zfs_handle_t *zhp;
6700 nvlist_t *nvl;
6701 int ret = 0;
6702 int c;
6703
6704 /* check options */
6705 while ((c = getopt(argc, argv, "")) != -1) {
6706 switch (c) {
6707 case '?':
6708 (void) fprintf(stderr,
6709 gettext("invalid option '%c'\n"), optopt);
6710 goto usage;
6711 }
6712 }
6713
6714 argc -= optind;
6715 argv += optind;
6716
6717 /* check number of arguments */
6718 if (argc < 1) {
6719 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6720 goto usage;
6721 }
6722 if (argc < 2) {
6723 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6724 goto usage;
6725 }
6726
6727 if (strchr(argv[1], '#') == NULL) {
6728 (void) fprintf(stderr,
6729 gettext("invalid bookmark name '%s' -- "
6730 "must contain a '#'\n"), argv[1]);
6731 goto usage;
6732 }
6733
6734 if (argv[0][0] == '@') {
6735 /*
6736 * Snapshot name begins with @.
6737 * Default to same fs as bookmark.
6738 */
6739 (void) strncpy(snapname, argv[1], sizeof (snapname));
6740 *strchr(snapname, '#') = '\0';
6741 (void) strlcat(snapname, argv[0], sizeof (snapname));
6742 } else {
6743 (void) strncpy(snapname, argv[0], sizeof (snapname));
6744 }
6745 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6746 if (zhp == NULL)
6747 goto usage;
6748 zfs_close(zhp);
6749
6750
6751 nvl = fnvlist_alloc();
6752 fnvlist_add_string(nvl, argv[1], snapname);
6753 ret = lzc_bookmark(nvl, NULL);
6754 fnvlist_free(nvl);
6755
6756 if (ret != 0) {
6757 const char *err_msg;
6758 char errbuf[1024];
6759
6760 (void) snprintf(errbuf, sizeof (errbuf),
6761 dgettext(TEXT_DOMAIN,
6762 "cannot create bookmark '%s'"), argv[1]);
6763
6764 switch (ret) {
6765 case EXDEV:
6766 err_msg = "bookmark is in a different pool";
6767 break;
6768 case EEXIST:
6769 err_msg = "bookmark exists";
6770 break;
6771 case EINVAL:
6772 err_msg = "invalid argument";
6773 break;
6774 case ENOTSUP:
6775 err_msg = "bookmark feature not enabled";
6776 break;
6777 case ENOSPC:
6778 err_msg = "out of space";
6779 break;
6780 default:
6781 err_msg = "unknown error";
6782 break;
6783 }
6784 (void) fprintf(stderr, "%s: %s\n", errbuf,
6785 dgettext(TEXT_DOMAIN, err_msg));
6786 }
6787
6788 return (ret != 0);
6789
6790 usage:
6791 usage(B_FALSE);
6792 return (-1);
6793 }
6794
6795 int
6796 main(int argc, char **argv)
6797 {
6798 int ret = 0;
6799 int i = 0;
6800 char *cmdname;
6801
6802 (void) setlocale(LC_ALL, "");
6803 (void) textdomain(TEXT_DOMAIN);
6804
6805 dprintf_setup(&argc, argv);
6806
6807 opterr = 0;
6808
6809 /*
6810 * Make sure the user has specified some command.
6811 */
6812 if (argc < 2) {
6813 (void) fprintf(stderr, gettext("missing command\n"));
6814 usage(B_FALSE);
6815 }
6816
6817 cmdname = argv[1];
6818
6819 /*
6820 * The 'umount' command is an alias for 'unmount'
6821 */
6822 if (strcmp(cmdname, "umount") == 0)
6823 cmdname = "unmount";
6824
6825 /*
6826 * The 'recv' command is an alias for 'receive'
6827 */
6828 if (strcmp(cmdname, "recv") == 0)
6829 cmdname = "receive";
6830
6831 /*
6832 * The 'snap' command is an alias for 'snapshot'
6833 */
6834 if (strcmp(cmdname, "snap") == 0)
6835 cmdname = "snapshot";
6836
6837 /*
6838 * Special case '-?'
6839 */
6840 if ((strcmp(cmdname, "-?") == 0) ||
6841 (strcmp(cmdname, "--help") == 0))
6842 usage(B_TRUE);
6843
6844 if ((g_zfs = libzfs_init()) == NULL) {
6845 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
6846 return (1);
6847 }
6848
6849 mnttab_file = g_zfs->libzfs_mnttab;
6850
6851 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6852
6853 libzfs_print_on_error(g_zfs, B_TRUE);
6854
6855 /*
6856 * Run the appropriate command.
6857 */
6858 libzfs_mnttab_cache(g_zfs, B_TRUE);
6859 if (find_command_idx(cmdname, &i) == 0) {
6860 current_command = &command_table[i];
6861 ret = command_table[i].func(argc - 1, argv + 1);
6862 } else if (strchr(cmdname, '=') != NULL) {
6863 verify(find_command_idx("set", &i) == 0);
6864 current_command = &command_table[i];
6865 ret = command_table[i].func(argc, argv);
6866 } else {
6867 (void) fprintf(stderr, gettext("unrecognized "
6868 "command '%s'\n"), cmdname);
6869 usage(B_FALSE);
6870 ret = 1;
6871 }
6872
6873 if (ret == 0 && log_history)
6874 (void) zpool_log_history(g_zfs, history_str);
6875
6876 libzfs_fini(g_zfs);
6877
6878 /*
6879 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6880 * for the purposes of running ::findleaks.
6881 */
6882 if (getenv("ZFS_ABORT") != NULL) {
6883 (void) printf("dumping core by request\n");
6884 abort();
6885 }
6886
6887 return (ret);
6888 }