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