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