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