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