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