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