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