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