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