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