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