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