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