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