]> git.proxmox.com Git - mirror_zfs.git/blame - zfs/zcmd/zfs/zfs_main.c
Initial Linux ZFS GIT Repo
[mirror_zfs.git] / zfs / zcmd / zfs / zfs_main.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident "@(#)zfs_main.c 1.58 08/03/24 SMI"
28
29#include <assert.h>
30#include <ctype.h>
31#include <errno.h>
32#include <libgen.h>
33#include <libintl.h>
34#include <libuutil.h>
35#include <libnvpair.h>
36#include <locale.h>
37#include <stddef.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <strings.h>
41#include <unistd.h>
42#include <fcntl.h>
43#include <zone.h>
44#include <sys/mkdev.h>
45#include <sys/mntent.h>
46#include <sys/mnttab.h>
47#include <sys/mount.h>
48#include <sys/stat.h>
49#include <sys/avl.h>
50
51#include <libzfs.h>
52#include <libuutil.h>
53
54#include "zfs_iter.h"
55#include "zfs_util.h"
56
57libzfs_handle_t *g_zfs;
58
59static FILE *mnttab_file;
60static char history_str[HIS_MAX_RECORD_LEN];
61
62static int zfs_do_clone(int argc, char **argv);
63static int zfs_do_create(int argc, char **argv);
64static int zfs_do_destroy(int argc, char **argv);
65static int zfs_do_get(int argc, char **argv);
66static int zfs_do_inherit(int argc, char **argv);
67static int zfs_do_list(int argc, char **argv);
68static int zfs_do_mount(int argc, char **argv);
69static int zfs_do_rename(int argc, char **argv);
70static int zfs_do_rollback(int argc, char **argv);
71static int zfs_do_set(int argc, char **argv);
72static int zfs_do_upgrade(int argc, char **argv);
73static int zfs_do_snapshot(int argc, char **argv);
74static int zfs_do_unmount(int argc, char **argv);
75static int zfs_do_share(int argc, char **argv);
76static int zfs_do_unshare(int argc, char **argv);
77static int zfs_do_send(int argc, char **argv);
78static int zfs_do_receive(int argc, char **argv);
79static int zfs_do_promote(int argc, char **argv);
80static int zfs_do_allow(int argc, char **argv);
81static int zfs_do_unallow(int argc, char **argv);
82
83/*
84 * These libumem hooks provide a reasonable set of defaults for the allocator's
85 * debugging facilities.
86 */
87const char *
88_umem_debug_init(void)
89{
90 return ("default,verbose"); /* $UMEM_DEBUG setting */
91}
92
93const char *
94_umem_logging_init(void)
95{
96 return ("fail,contents"); /* $UMEM_LOGGING setting */
97}
98
99typedef enum {
100 HELP_CLONE,
101 HELP_CREATE,
102 HELP_DESTROY,
103 HELP_GET,
104 HELP_INHERIT,
105 HELP_UPGRADE,
106 HELP_LIST,
107 HELP_MOUNT,
108 HELP_PROMOTE,
109 HELP_RECEIVE,
110 HELP_RENAME,
111 HELP_ROLLBACK,
112 HELP_SEND,
113 HELP_SET,
114 HELP_SHARE,
115 HELP_SNAPSHOT,
116 HELP_UNMOUNT,
117 HELP_UNSHARE,
118 HELP_ALLOW,
119 HELP_UNALLOW
120} zfs_help_t;
121
122typedef struct zfs_command {
123 const char *name;
124 int (*func)(int argc, char **argv);
125 zfs_help_t usage;
126} zfs_command_t;
127
128/*
129 * Master command table. Each ZFS command has a name, associated function, and
130 * usage message. The usage messages need to be internationalized, so we have
131 * to have a function to return the usage message based on a command index.
132 *
133 * These commands are organized according to how they are displayed in the usage
134 * message. An empty command (one with a NULL name) indicates an empty line in
135 * the generic usage message.
136 */
137static zfs_command_t command_table[] = {
138 { "create", zfs_do_create, HELP_CREATE },
139 { "destroy", zfs_do_destroy, HELP_DESTROY },
140 { NULL },
141 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
142 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
143 { "clone", zfs_do_clone, HELP_CLONE },
144 { "promote", zfs_do_promote, HELP_PROMOTE },
145 { "rename", zfs_do_rename, HELP_RENAME },
146 { NULL },
147 { "list", zfs_do_list, HELP_LIST },
148 { NULL },
149 { "set", zfs_do_set, HELP_SET },
150 { "get", zfs_do_get, HELP_GET },
151 { "inherit", zfs_do_inherit, HELP_INHERIT },
152 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
153 { NULL },
154 { "mount", zfs_do_mount, HELP_MOUNT },
155 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
156 { "share", zfs_do_share, HELP_SHARE },
157 { "unshare", zfs_do_unshare, HELP_UNSHARE },
158 { NULL },
159 { "send", zfs_do_send, HELP_SEND },
160 { "receive", zfs_do_receive, HELP_RECEIVE },
161 { NULL },
162 { "allow", zfs_do_allow, HELP_ALLOW },
163 { NULL },
164 { "unallow", zfs_do_unallow, HELP_UNALLOW },
165};
166
167#define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
168
169zfs_command_t *current_command;
170
171static const char *
172get_usage(zfs_help_t idx)
173{
174 switch (idx) {
175 case HELP_CLONE:
176 return (gettext("\tclone [-p] <snapshot> "
177 "<filesystem|volume>\n"));
178 case HELP_CREATE:
179 return (gettext("\tcreate [-p] [-o property=value] ... "
180 "<filesystem>\n"
181 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
182 "-V <size> <volume>\n"));
183 case HELP_DESTROY:
184 return (gettext("\tdestroy [-rRf] "
185 "<filesystem|volume|snapshot>\n"));
186 case HELP_GET:
187 return (gettext("\tget [-rHp] [-o field[,...]] "
188 "[-s source[,...]]\n"
189 "\t <\"all\" | property[,...]> "
190 "[filesystem|volume|snapshot] ...\n"));
191 case HELP_INHERIT:
192 return (gettext("\tinherit [-r] <property> "
193 "<filesystem|volume> ...\n"));
194 case HELP_UPGRADE:
195 return (gettext("\tupgrade [-v]\n"
196 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
197 case HELP_LIST:
198 return (gettext("\tlist [-rH] [-o property[,...]] "
199 "[-t type[,...]] [-s property] ...\n"
200 "\t [-S property] ... "
201 "[filesystem|volume|snapshot] ...\n"));
202 case HELP_MOUNT:
203 return (gettext("\tmount\n"
204 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
205 case HELP_PROMOTE:
206 return (gettext("\tpromote <clone-filesystem>\n"));
207 case HELP_RECEIVE:
208 return (gettext("\treceive [-vnF] <filesystem|volume|"
209 "snapshot>\n"
210 "\treceive [-vnF] -d <filesystem>\n"));
211 case HELP_RENAME:
212 return (gettext("\trename <filesystem|volume|snapshot> "
213 "<filesystem|volume|snapshot>\n"
214 "\trename -p <filesystem|volume> <filesystem|volume>\n"
215 "\trename -r <snapshot> <snapshot>"));
216 case HELP_ROLLBACK:
217 return (gettext("\trollback [-rRf] <snapshot>\n"));
218 case HELP_SEND:
219 return (gettext("\tsend [-R] [-[iI] snapshot] <snapshot>\n"));
220 case HELP_SET:
221 return (gettext("\tset <property=value> "
222 "<filesystem|volume> ...\n"));
223 case HELP_SHARE:
224 return (gettext("\tshare <-a | filesystem>\n"));
225 case HELP_SNAPSHOT:
226 return (gettext("\tsnapshot [-r] "
227 "<filesystem@snapname|volume@snapname>\n"));
228 case HELP_UNMOUNT:
229 return (gettext("\tunmount [-f] "
230 "<-a | filesystem|mountpoint>\n"));
231 case HELP_UNSHARE:
232 return (gettext("\tunshare [-f] "
233 "<-a | filesystem|mountpoint>\n"));
234 case HELP_ALLOW:
235 return (gettext("\tallow [-ldug] "
236 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
237 "\t <filesystem|volume>\n"
238 "\tallow [-ld] -e <perm|@setname>[,...] "
239 "<filesystem|volume>\n"
240 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
241 "\tallow -s @setname <perm|@setname>[,...] "
242 "<filesystem|volume>\n"));
243 case HELP_UNALLOW:
244 return (gettext("\tunallow [-rldug] "
245 "<\"everyone\"|user|group>[,...]\n"
246 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
247 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
248 "<filesystem|volume>\n"
249 "\tunallow [-r] -c [<perm|@setname>[,...]] "
250 "<filesystem|volume>\n"
251 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
252 "<filesystem|volume>\n"));
253 }
254
255 abort();
256 /* NOTREACHED */
257}
258
259/*
260 * Utility function to guarantee malloc() success.
261 */
262void *
263safe_malloc(size_t size)
264{
265 void *data;
266
267 if ((data = calloc(1, size)) == NULL) {
268 (void) fprintf(stderr, "internal error: out of memory\n");
269 exit(1);
270 }
271
272 return (data);
273}
274
275/*
276 * Callback routine that will print out information for each of
277 * the properties.
278 */
279static int
280usage_prop_cb(int prop, void *cb)
281{
282 FILE *fp = cb;
283
284 (void) fprintf(fp, "\t%-14s ", zfs_prop_to_name(prop));
285
286 if (prop == ZFS_PROP_CASE)
287 (void) fprintf(fp, "NO ");
288 else if (zfs_prop_readonly(prop))
289 (void) fprintf(fp, " NO ");
290 else
291 (void) fprintf(fp, " YES ");
292
293 if (zfs_prop_inheritable(prop))
294 (void) fprintf(fp, " YES ");
295 else
296 (void) fprintf(fp, " NO ");
297
298 if (zfs_prop_values(prop) == NULL)
299 (void) fprintf(fp, "-\n");
300 else
301 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
302
303 return (ZPROP_CONT);
304}
305
306/*
307 * Display usage message. If we're inside a command, display only the usage for
308 * that command. Otherwise, iterate over the entire command table and display
309 * a complete usage message.
310 */
311static void
312usage(boolean_t requested)
313{
314 int i;
315 boolean_t show_properties = B_FALSE;
316 boolean_t show_permissions = B_FALSE;
317 FILE *fp = requested ? stdout : stderr;
318
319 if (current_command == NULL) {
320
321 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
322 (void) fprintf(fp,
323 gettext("where 'command' is one of the following:\n\n"));
324
325 for (i = 0; i < NCOMMAND; i++) {
326 if (command_table[i].name == NULL)
327 (void) fprintf(fp, "\n");
328 else
329 (void) fprintf(fp, "%s",
330 get_usage(command_table[i].usage));
331 }
332
333 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
334 "pool/[dataset/]*dataset[@name]\n"));
335 } else {
336 (void) fprintf(fp, gettext("usage:\n"));
337 (void) fprintf(fp, "%s", get_usage(current_command->usage));
338 }
339
340 if (current_command != NULL &&
341 (strcmp(current_command->name, "set") == 0 ||
342 strcmp(current_command->name, "get") == 0 ||
343 strcmp(current_command->name, "inherit") == 0 ||
344 strcmp(current_command->name, "list") == 0))
345 show_properties = B_TRUE;
346
347 if (current_command != NULL &&
348 (strcmp(current_command->name, "allow") == 0 ||
349 strcmp(current_command->name, "unallow") == 0))
350 show_permissions = B_TRUE;
351
352 if (show_properties) {
353
354 (void) fprintf(fp,
355 gettext("\nThe following properties are supported:\n"));
356
357 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
358 "PROPERTY", "EDIT", "INHERIT", "VALUES");
359
360 /* Iterate over all properties */
361 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
362 ZFS_TYPE_DATASET);
363
364 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
365 "with standard units such as K, M, G, etc.\n"));
366 (void) fprintf(fp, gettext("\n\nUser-defined properties can "
367 "be specified by using a name containing a colon (:).\n"));
368
369 } else if (show_permissions) {
370 (void) fprintf(fp,
371 gettext("\nThe following permissions are supported:\n"));
372
373 zfs_deleg_permissions();
374 } else {
375 /*
376 * TRANSLATION NOTE:
377 * "zfs set|get" must not be localised this is the
378 * command name and arguments.
379 */
380
381 (void) fprintf(fp,
382 gettext("\nFor the property list, run: zfs set|get\n"));
383
384 (void) fprintf(fp,
385 gettext("\nFor the delegated permission list, run:"
386 " zfs allow|unallow\n"));
387 }
388
389 /*
390 * See comments at end of main().
391 */
392 if (getenv("ZFS_ABORT") != NULL) {
393 (void) printf("dumping core by request\n");
394 abort();
395 }
396
397 exit(requested ? 0 : 2);
398}
399
400/*
401 * zfs clone [-p] <snap> <fs | vol>
402 *
403 * Given an existing dataset, create a writable copy whose initial contents
404 * are the same as the source. The newly created dataset maintains a
405 * dependency on the original; the original cannot be destroyed so long as
406 * the clone exists.
407 *
408 * The '-p' flag creates all the non-existing ancestors of the target first.
409 */
410static int
411zfs_do_clone(int argc, char **argv)
412{
413 zfs_handle_t *zhp;
414 boolean_t parents = B_FALSE;
415 int ret;
416 int c;
417
418 /* check options */
419 while ((c = getopt(argc, argv, "p")) != -1) {
420 switch (c) {
421 case 'p':
422 parents = B_TRUE;
423 break;
424 case '?':
425 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
426 optopt);
427 usage(B_FALSE);
428 }
429 }
430
431 argc -= optind;
432 argv += optind;
433
434 /* check number of arguments */
435 if (argc < 1) {
436 (void) fprintf(stderr, gettext("missing source dataset "
437 "argument\n"));
438 usage(B_FALSE);
439 }
440 if (argc < 2) {
441 (void) fprintf(stderr, gettext("missing target dataset "
442 "argument\n"));
443 usage(B_FALSE);
444 }
445 if (argc > 2) {
446 (void) fprintf(stderr, gettext("too many arguments\n"));
447 usage(B_FALSE);
448 }
449
450 /* open the source dataset */
451 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
452 return (1);
453
454 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
455 ZFS_TYPE_VOLUME)) {
456 /*
457 * Now create the ancestors of the target dataset. If the
458 * target already exists and '-p' option was used we should not
459 * complain.
460 */
461 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
462 ZFS_TYPE_VOLUME))
463 return (0);
464 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
465 return (1);
466 }
467
468 /* pass to libzfs */
469 ret = zfs_clone(zhp, argv[1], NULL);
470
471 /* create the mountpoint if necessary */
472 if (ret == 0) {
473 zfs_handle_t *clone;
474
475 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
476 if (clone != NULL) {
477 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
478 ret = zfs_share(clone);
479 zfs_close(clone);
480 }
481 }
482
483 zfs_close(zhp);
484
485 return (ret == 0 ? 0 : 1);
486}
487
488/*
489 * zfs create [-p] [-o prop=value] ... fs
490 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
491 *
492 * Create a new dataset. This command can be used to create filesystems
493 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
494 * For volumes, the user must specify a size to be used.
495 *
496 * The '-s' flag applies only to volumes, and indicates that we should not try
497 * to set the reservation for this volume. By default we set a reservation
498 * equal to the size for any volume. For pools with SPA_VERSION >=
499 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
500 *
501 * The '-p' flag creates all the non-existing ancestors of the target first.
502 */
503static int
504zfs_do_create(int argc, char **argv)
505{
506 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
507 zfs_handle_t *zhp = NULL;
508 uint64_t volsize;
509 int c;
510 boolean_t noreserve = B_FALSE;
511 boolean_t bflag = B_FALSE;
512 boolean_t parents = B_FALSE;
513 int ret = 1;
514 nvlist_t *props = NULL;
515 uint64_t intval;
516 char *propname;
517 char *propval = NULL;
518 char *strval;
519 int canmount;
520
521 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
522 (void) fprintf(stderr, gettext("internal error: "
523 "out of memory\n"));
524 return (1);
525 }
526
527 /* check options */
528 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
529 switch (c) {
530 case 'V':
531 type = ZFS_TYPE_VOLUME;
532 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
533 (void) fprintf(stderr, gettext("bad volume "
534 "size '%s': %s\n"), optarg,
535 libzfs_error_description(g_zfs));
536 goto error;
537 }
538
539 if (nvlist_add_uint64(props,
540 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
541 intval) != 0) {
542 (void) fprintf(stderr, gettext("internal "
543 "error: out of memory\n"));
544 goto error;
545 }
546 volsize = intval;
547 break;
548 case 'p':
549 parents = B_TRUE;
550 break;
551 case 'b':
552 bflag = B_TRUE;
553 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
554 (void) fprintf(stderr, gettext("bad volume "
555 "block size '%s': %s\n"), optarg,
556 libzfs_error_description(g_zfs));
557 goto error;
558 }
559
560 if (nvlist_add_uint64(props,
561 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
562 intval) != 0) {
563 (void) fprintf(stderr, gettext("internal "
564 "error: out of memory\n"));
565 goto error;
566 }
567 break;
568 case 'o':
569 propname = optarg;
570 if ((propval = strchr(propname, '=')) == NULL) {
571 (void) fprintf(stderr, gettext("missing "
572 "'=' for -o option\n"));
573 goto error;
574 }
575 *propval = '\0';
576 propval++;
577 if (nvlist_lookup_string(props, propname,
578 &strval) == 0) {
579 (void) fprintf(stderr, gettext("property '%s' "
580 "specified multiple times\n"), propname);
581 goto error;
582 }
583 if (nvlist_add_string(props, propname, propval) != 0) {
584 (void) fprintf(stderr, gettext("internal "
585 "error: out of memory\n"));
586 goto error;
587 }
588 break;
589 case 's':
590 noreserve = B_TRUE;
591 break;
592 case ':':
593 (void) fprintf(stderr, gettext("missing size "
594 "argument\n"));
595 goto badusage;
596 break;
597 case '?':
598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
599 optopt);
600 goto badusage;
601 }
602 }
603
604 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
605 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
606 "used when creating a volume\n"));
607 goto badusage;
608 }
609
610 argc -= optind;
611 argv += optind;
612
613 /* check number of arguments */
614 if (argc == 0) {
615 (void) fprintf(stderr, gettext("missing %s argument\n"),
616 zfs_type_to_name(type));
617 goto badusage;
618 }
619 if (argc > 1) {
620 (void) fprintf(stderr, gettext("too many arguments\n"));
621 goto badusage;
622 }
623
624 if (type == ZFS_TYPE_VOLUME && !noreserve) {
625 zpool_handle_t *zpool_handle;
626 uint64_t spa_version;
627 char *p;
628 zfs_prop_t resv_prop;
629
630 if (p = strchr(argv[0], '/'))
631 *p = '\0';
632 zpool_handle = zpool_open(g_zfs, argv[0]);
633 if (p != NULL)
634 *p = '/';
635 if (zpool_handle == NULL)
636 goto error;
637 spa_version = zpool_get_prop_int(zpool_handle,
638 ZPOOL_PROP_VERSION, NULL);
639 zpool_close(zpool_handle);
640 if (spa_version >= SPA_VERSION_REFRESERVATION)
641 resv_prop = ZFS_PROP_REFRESERVATION;
642 else
643 resv_prop = ZFS_PROP_RESERVATION;
644
645 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
646 &strval) != 0) {
647 if (nvlist_add_uint64(props,
648 zfs_prop_to_name(resv_prop), volsize) != 0) {
649 (void) fprintf(stderr, gettext("internal "
650 "error: out of memory\n"));
651 nvlist_free(props);
652 return (1);
653 }
654 }
655 }
656
657 if (parents && zfs_name_valid(argv[0], type)) {
658 /*
659 * Now create the ancestors of target dataset. If the target
660 * already exists and '-p' option was used we should not
661 * complain.
662 */
663 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
664 ret = 0;
665 goto error;
666 }
667 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
668 goto error;
669 }
670
671 /* pass to libzfs */
672 if (zfs_create(g_zfs, argv[0], type, props) != 0)
673 goto error;
674
675 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
676 goto error;
677 /*
678 * if the user doesn't want the dataset automatically mounted,
679 * then skip the mount/share step
680 */
681
682 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
683
684 /*
685 * Mount and/or share the new filesystem as appropriate. We provide a
686 * verbose error message to let the user know that their filesystem was
687 * in fact created, even if we failed to mount or share it.
688 */
689 ret = 0;
690 if (canmount == ZFS_CANMOUNT_ON) {
691 if (zfs_mount(zhp, NULL, 0) != 0) {
692 (void) fprintf(stderr, gettext("filesystem "
693 "successfully created, but not mounted\n"));
694 ret = 1;
695 } else if (zfs_share(zhp) != 0) {
696 (void) fprintf(stderr, gettext("filesystem "
697 "successfully created, but not shared\n"));
698 ret = 1;
699 }
700 }
701
702error:
703 if (zhp)
704 zfs_close(zhp);
705 nvlist_free(props);
706 return (ret);
707badusage:
708 nvlist_free(props);
709 usage(B_FALSE);
710 return (2);
711}
712
713/*
714 * zfs destroy [-rf] <fs, snap, vol>
715 *
716 * -r Recursively destroy all children
717 * -R Recursively destroy all dependents, including clones
718 * -f Force unmounting of any dependents
719 *
720 * Destroys the given dataset. By default, it will unmount any filesystems,
721 * and refuse to destroy a dataset that has any dependents. A dependent can
722 * either be a child, or a clone of a child.
723 */
724typedef struct destroy_cbdata {
725 boolean_t cb_first;
726 int cb_force;
727 int cb_recurse;
728 int cb_error;
729 int cb_needforce;
730 int cb_doclones;
731 boolean_t cb_closezhp;
732 zfs_handle_t *cb_target;
733 char *cb_snapname;
734} destroy_cbdata_t;
735
736/*
737 * Check for any dependents based on the '-r' or '-R' flags.
738 */
739static int
740destroy_check_dependent(zfs_handle_t *zhp, void *data)
741{
742 destroy_cbdata_t *cbp = data;
743 const char *tname = zfs_get_name(cbp->cb_target);
744 const char *name = zfs_get_name(zhp);
745
746 if (strncmp(tname, name, strlen(tname)) == 0 &&
747 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
748 /*
749 * This is a direct descendant, not a clone somewhere else in
750 * the hierarchy.
751 */
752 if (cbp->cb_recurse)
753 goto out;
754
755 if (cbp->cb_first) {
756 (void) fprintf(stderr, gettext("cannot destroy '%s': "
757 "%s has children\n"),
758 zfs_get_name(cbp->cb_target),
759 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
760 (void) fprintf(stderr, gettext("use '-r' to destroy "
761 "the following datasets:\n"));
762 cbp->cb_first = B_FALSE;
763 cbp->cb_error = 1;
764 }
765
766 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
767 } else {
768 /*
769 * This is a clone. We only want to report this if the '-r'
770 * wasn't specified, or the target is a snapshot.
771 */
772 if (!cbp->cb_recurse &&
773 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
774 goto out;
775
776 if (cbp->cb_first) {
777 (void) fprintf(stderr, gettext("cannot destroy '%s': "
778 "%s has dependent clones\n"),
779 zfs_get_name(cbp->cb_target),
780 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
781 (void) fprintf(stderr, gettext("use '-R' to destroy "
782 "the following datasets:\n"));
783 cbp->cb_first = B_FALSE;
784 cbp->cb_error = 1;
785 }
786
787 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
788 }
789
790out:
791 zfs_close(zhp);
792 return (0);
793}
794
795static int
796destroy_callback(zfs_handle_t *zhp, void *data)
797{
798 destroy_cbdata_t *cbp = data;
799
800 /*
801 * Ignore pools (which we've already flagged as an error before getting
802 * here.
803 */
804 if (strchr(zfs_get_name(zhp), '/') == NULL &&
805 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
806 zfs_close(zhp);
807 return (0);
808 }
809
810 /*
811 * Bail out on the first error.
812 */
813 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
814 zfs_destroy(zhp) != 0) {
815 zfs_close(zhp);
816 return (-1);
817 }
818
819 zfs_close(zhp);
820 return (0);
821}
822
823static int
824destroy_snap_clones(zfs_handle_t *zhp, void *arg)
825{
826 destroy_cbdata_t *cbp = arg;
827 char thissnap[MAXPATHLEN];
828 zfs_handle_t *szhp;
829 boolean_t closezhp = cbp->cb_closezhp;
830 int rv;
831
832 (void) snprintf(thissnap, sizeof (thissnap),
833 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
834
835 libzfs_print_on_error(g_zfs, B_FALSE);
836 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
837 libzfs_print_on_error(g_zfs, B_TRUE);
838 if (szhp) {
839 /*
840 * Destroy any clones of this snapshot
841 */
842 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
843 cbp) != 0) {
844 zfs_close(szhp);
845 if (closezhp)
846 zfs_close(zhp);
847 return (-1);
848 }
849 zfs_close(szhp);
850 }
851
852 cbp->cb_closezhp = B_TRUE;
853 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
854 if (closezhp)
855 zfs_close(zhp);
856 return (rv);
857}
858
859static int
860zfs_do_destroy(int argc, char **argv)
861{
862 destroy_cbdata_t cb = { 0 };
863 int c;
864 zfs_handle_t *zhp;
865 char *cp;
866
867 /* check options */
868 while ((c = getopt(argc, argv, "frR")) != -1) {
869 switch (c) {
870 case 'f':
871 cb.cb_force = 1;
872 break;
873 case 'r':
874 cb.cb_recurse = 1;
875 break;
876 case 'R':
877 cb.cb_recurse = 1;
878 cb.cb_doclones = 1;
879 break;
880 case '?':
881 default:
882 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
883 optopt);
884 usage(B_FALSE);
885 }
886 }
887
888 argc -= optind;
889 argv += optind;
890
891 /* check number of arguments */
892 if (argc == 0) {
893 (void) fprintf(stderr, gettext("missing path argument\n"));
894 usage(B_FALSE);
895 }
896 if (argc > 1) {
897 (void) fprintf(stderr, gettext("too many arguments\n"));
898 usage(B_FALSE);
899 }
900
901 /*
902 * If we are doing recursive destroy of a snapshot, then the
903 * named snapshot may not exist. Go straight to libzfs.
904 */
905 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
906 int ret;
907
908 *cp = '\0';
909 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
910 return (1);
911 *cp = '@';
912 cp++;
913
914 if (cb.cb_doclones) {
915 cb.cb_snapname = cp;
916 if (destroy_snap_clones(zhp, &cb) != 0) {
917 zfs_close(zhp);
918 return (1);
919 }
920 }
921
922 ret = zfs_destroy_snaps(zhp, cp);
923 zfs_close(zhp);
924 if (ret) {
925 (void) fprintf(stderr,
926 gettext("no snapshots destroyed\n"));
927 }
928 return (ret != 0);
929 }
930
931
932 /* Open the given dataset */
933 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
934 return (1);
935
936 cb.cb_target = zhp;
937
938 /*
939 * Perform an explicit check for pools before going any further.
940 */
941 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
942 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
943 (void) fprintf(stderr, gettext("cannot destroy '%s': "
944 "operation does not apply to pools\n"),
945 zfs_get_name(zhp));
946 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
947 "%s' to destroy all datasets in the pool\n"),
948 zfs_get_name(zhp));
949 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
950 "to destroy the pool itself\n"), zfs_get_name(zhp));
951 zfs_close(zhp);
952 return (1);
953 }
954
955 /*
956 * Check for any dependents and/or clones.
957 */
958 cb.cb_first = B_TRUE;
959 if (!cb.cb_doclones &&
960 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
961 &cb) != 0) {
962 zfs_close(zhp);
963 return (1);
964 }
965
966 if (cb.cb_error ||
967 zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
968 zfs_close(zhp);
969 return (1);
970 }
971
972 /*
973 * Do the real thing. The callback will close the handle regardless of
974 * whether it succeeds or not.
975 */
976
977 if (destroy_callback(zhp, &cb) != 0)
978 return (1);
979
980
981 return (0);
982}
983
984/*
985 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
986 * < all | property[,property]... > < fs | snap | vol > ...
987 *
988 * -r recurse over any child datasets
989 * -H scripted mode. Headers are stripped, and fields are separated
990 * by tabs instead of spaces.
991 * -o Set of fields to display. One of "name,property,value,source".
992 * Default is all four.
993 * -s Set of sources to allow. One of
994 * "local,default,inherited,temporary,none". Default is all
995 * five.
996 * -p Display values in parsable (literal) format.
997 *
998 * Prints properties for the given datasets. The user can control which
999 * columns to display as well as which property types to allow.
1000 */
1001
1002/*
1003 * Invoked to display the properties for a single dataset.
1004 */
1005static int
1006get_callback(zfs_handle_t *zhp, void *data)
1007{
1008 char buf[ZFS_MAXPROPLEN];
1009 zprop_source_t sourcetype;
1010 char source[ZFS_MAXNAMELEN];
1011 zprop_get_cbdata_t *cbp = data;
1012 nvlist_t *userprop = zfs_get_user_props(zhp);
1013 zprop_list_t *pl = cbp->cb_proplist;
1014 nvlist_t *propval;
1015 char *strval;
1016 char *sourceval;
1017
1018 for (; pl != NULL; pl = pl->pl_next) {
1019 /*
1020 * Skip the special fake placeholder. This will also skip over
1021 * the name property when 'all' is specified.
1022 */
1023 if (pl->pl_prop == ZFS_PROP_NAME &&
1024 pl == cbp->cb_proplist)
1025 continue;
1026
1027 if (pl->pl_prop != ZPROP_INVAL) {
1028 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1029 sizeof (buf), &sourcetype, source,
1030 sizeof (source),
1031 cbp->cb_literal) != 0) {
1032 if (pl->pl_all)
1033 continue;
1034 if (!zfs_prop_valid_for_type(pl->pl_prop,
1035 ZFS_TYPE_DATASET)) {
1036 (void) fprintf(stderr,
1037 gettext("No such property '%s'\n"),
1038 zfs_prop_to_name(pl->pl_prop));
1039 continue;
1040 }
1041 sourcetype = ZPROP_SRC_NONE;
1042 (void) strlcpy(buf, "-", sizeof (buf));
1043 }
1044
1045 zprop_print_one_property(zfs_get_name(zhp), cbp,
1046 zfs_prop_to_name(pl->pl_prop),
1047 buf, sourcetype, source);
1048 } else {
1049 if (nvlist_lookup_nvlist(userprop,
1050 pl->pl_user_prop, &propval) != 0) {
1051 if (pl->pl_all)
1052 continue;
1053 sourcetype = ZPROP_SRC_NONE;
1054 strval = "-";
1055 } else {
1056 verify(nvlist_lookup_string(propval,
1057 ZPROP_VALUE, &strval) == 0);
1058 verify(nvlist_lookup_string(propval,
1059 ZPROP_SOURCE, &sourceval) == 0);
1060
1061 if (strcmp(sourceval,
1062 zfs_get_name(zhp)) == 0) {
1063 sourcetype = ZPROP_SRC_LOCAL;
1064 } else {
1065 sourcetype = ZPROP_SRC_INHERITED;
1066 (void) strlcpy(source,
1067 sourceval, sizeof (source));
1068 }
1069 }
1070
1071 zprop_print_one_property(zfs_get_name(zhp), cbp,
1072 pl->pl_user_prop, strval, sourcetype,
1073 source);
1074 }
1075 }
1076
1077 return (0);
1078}
1079
1080static int
1081zfs_do_get(int argc, char **argv)
1082{
1083 zprop_get_cbdata_t cb = { 0 };
1084 boolean_t recurse = B_FALSE;
1085 int i, c;
1086 char *value, *fields;
1087 int ret;
1088 zprop_list_t fake_name = { 0 };
1089
1090 /*
1091 * Set up default columns and sources.
1092 */
1093 cb.cb_sources = ZPROP_SRC_ALL;
1094 cb.cb_columns[0] = GET_COL_NAME;
1095 cb.cb_columns[1] = GET_COL_PROPERTY;
1096 cb.cb_columns[2] = GET_COL_VALUE;
1097 cb.cb_columns[3] = GET_COL_SOURCE;
1098 cb.cb_type = ZFS_TYPE_DATASET;
1099
1100 /* check options */
1101 while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
1102 switch (c) {
1103 case 'p':
1104 cb.cb_literal = B_TRUE;
1105 break;
1106 case 'r':
1107 recurse = B_TRUE;
1108 break;
1109 case 'H':
1110 cb.cb_scripted = B_TRUE;
1111 break;
1112 case ':':
1113 (void) fprintf(stderr, gettext("missing argument for "
1114 "'%c' option\n"), optopt);
1115 usage(B_FALSE);
1116 break;
1117 case 'o':
1118 /*
1119 * Process the set of columns to display. We zero out
1120 * the structure to give us a blank slate.
1121 */
1122 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1123 i = 0;
1124 while (*optarg != '\0') {
1125 static char *col_subopts[] =
1126 { "name", "property", "value", "source",
1127 NULL };
1128
1129 if (i == 4) {
1130 (void) fprintf(stderr, gettext("too "
1131 "many fields given to -o "
1132 "option\n"));
1133 usage(B_FALSE);
1134 }
1135
1136 switch (getsubopt(&optarg, col_subopts,
1137 &value)) {
1138 case 0:
1139 cb.cb_columns[i++] = GET_COL_NAME;
1140 break;
1141 case 1:
1142 cb.cb_columns[i++] = GET_COL_PROPERTY;
1143 break;
1144 case 2:
1145 cb.cb_columns[i++] = GET_COL_VALUE;
1146 break;
1147 case 3:
1148 cb.cb_columns[i++] = GET_COL_SOURCE;
1149 break;
1150 default:
1151 (void) fprintf(stderr,
1152 gettext("invalid column name "
1153 "'%s'\n"), value);
1154 usage(B_FALSE);
1155 }
1156 }
1157 break;
1158
1159 case 's':
1160 cb.cb_sources = 0;
1161 while (*optarg != '\0') {
1162 static char *source_subopts[] = {
1163 "local", "default", "inherited",
1164 "temporary", "none", NULL };
1165
1166 switch (getsubopt(&optarg, source_subopts,
1167 &value)) {
1168 case 0:
1169 cb.cb_sources |= ZPROP_SRC_LOCAL;
1170 break;
1171 case 1:
1172 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1173 break;
1174 case 2:
1175 cb.cb_sources |= ZPROP_SRC_INHERITED;
1176 break;
1177 case 3:
1178 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1179 break;
1180 case 4:
1181 cb.cb_sources |= ZPROP_SRC_NONE;
1182 break;
1183 default:
1184 (void) fprintf(stderr,
1185 gettext("invalid source "
1186 "'%s'\n"), value);
1187 usage(B_FALSE);
1188 }
1189 }
1190 break;
1191
1192 case '?':
1193 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1194 optopt);
1195 usage(B_FALSE);
1196 }
1197 }
1198
1199 argc -= optind;
1200 argv += optind;
1201
1202 if (argc < 1) {
1203 (void) fprintf(stderr, gettext("missing property "
1204 "argument\n"));
1205 usage(B_FALSE);
1206 }
1207
1208 fields = argv[0];
1209
1210 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1211 != 0)
1212 usage(B_FALSE);
1213
1214 argc--;
1215 argv++;
1216
1217 /*
1218 * As part of zfs_expand_proplist(), we keep track of the maximum column
1219 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1220 * need to know the maximum name length. However, the user likely did
1221 * not specify 'name' as one of the properties to fetch, so we need to
1222 * make sure we always include at least this property for
1223 * print_get_headers() to work properly.
1224 */
1225 if (cb.cb_proplist != NULL) {
1226 fake_name.pl_prop = ZFS_PROP_NAME;
1227 fake_name.pl_width = strlen(gettext("NAME"));
1228 fake_name.pl_next = cb.cb_proplist;
1229 cb.cb_proplist = &fake_name;
1230 }
1231
1232 cb.cb_first = B_TRUE;
1233
1234 /* run for each object */
1235 ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_DATASET, NULL,
1236 &cb.cb_proplist, get_callback, &cb, B_FALSE);
1237
1238 if (cb.cb_proplist == &fake_name)
1239 zprop_free_list(fake_name.pl_next);
1240 else
1241 zprop_free_list(cb.cb_proplist);
1242
1243 return (ret);
1244}
1245
1246/*
1247 * inherit [-r] <property> <fs|vol> ...
1248 *
1249 * -r Recurse over all children
1250 *
1251 * For each dataset specified on the command line, inherit the given property
1252 * from its parent. Inheriting a property at the pool level will cause it to
1253 * use the default value. The '-r' flag will recurse over all children, and is
1254 * useful for setting a property on a hierarchy-wide basis, regardless of any
1255 * local modifications for each dataset.
1256 */
1257
1258static int
1259inherit_callback(zfs_handle_t *zhp, void *data)
1260{
1261 char *propname = data;
1262 int ret;
1263
1264 ret = zfs_prop_inherit(zhp, propname);
1265 return (ret != 0);
1266}
1267
1268static int
1269zfs_do_inherit(int argc, char **argv)
1270{
1271 boolean_t recurse = B_FALSE;
1272 int c;
1273 zfs_prop_t prop;
1274 char *propname;
1275 int ret;
1276
1277 /* check options */
1278 while ((c = getopt(argc, argv, "r")) != -1) {
1279 switch (c) {
1280 case 'r':
1281 recurse = B_TRUE;
1282 break;
1283 case '?':
1284 default:
1285 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1286 optopt);
1287 usage(B_FALSE);
1288 }
1289 }
1290
1291 argc -= optind;
1292 argv += optind;
1293
1294 /* check number of arguments */
1295 if (argc < 1) {
1296 (void) fprintf(stderr, gettext("missing property argument\n"));
1297 usage(B_FALSE);
1298 }
1299 if (argc < 2) {
1300 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1301 usage(B_FALSE);
1302 }
1303
1304 propname = argv[0];
1305 argc--;
1306 argv++;
1307
1308 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1309 if (zfs_prop_readonly(prop)) {
1310 (void) fprintf(stderr, gettext(
1311 "%s property is read-only\n"),
1312 propname);
1313 return (1);
1314 }
1315 if (!zfs_prop_inheritable(prop)) {
1316 (void) fprintf(stderr, gettext("'%s' property cannot "
1317 "be inherited\n"), propname);
1318 if (prop == ZFS_PROP_QUOTA ||
1319 prop == ZFS_PROP_RESERVATION ||
1320 prop == ZFS_PROP_REFQUOTA ||
1321 prop == ZFS_PROP_REFRESERVATION)
1322 (void) fprintf(stderr, gettext("use 'zfs set "
1323 "%s=none' to clear\n"), propname);
1324 return (1);
1325 }
1326 } else if (!zfs_prop_user(propname)) {
1327 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1328 propname);
1329 usage(B_FALSE);
1330 }
1331
1332 ret = zfs_for_each(argc, argv, recurse,
1333 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL,
1334 inherit_callback, propname, B_FALSE);
1335
1336 return (ret);
1337}
1338
1339typedef struct upgrade_cbdata {
1340 uint64_t cb_numupgraded;
1341 uint64_t cb_numsamegraded;
1342 uint64_t cb_numfailed;
1343 uint64_t cb_version;
1344 boolean_t cb_newer;
1345 boolean_t cb_foundone;
1346 char cb_lastfs[ZFS_MAXNAMELEN];
1347} upgrade_cbdata_t;
1348
1349static int
1350same_pool(zfs_handle_t *zhp, const char *name)
1351{
1352 int len1 = strcspn(name, "/@");
1353 const char *zhname = zfs_get_name(zhp);
1354 int len2 = strcspn(zhname, "/@");
1355
1356 if (len1 != len2)
1357 return (B_FALSE);
1358 return (strncmp(name, zhname, len1) == 0);
1359}
1360
1361static int
1362upgrade_list_callback(zfs_handle_t *zhp, void *data)
1363{
1364 upgrade_cbdata_t *cb = data;
1365 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1366
1367 /* list if it's old/new */
1368 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1369 (cb->cb_newer && version > ZPL_VERSION)) {
1370 char *str;
1371 if (cb->cb_newer) {
1372 str = gettext("The following filesystems are "
1373 "formatted using a newer software version and\n"
1374 "cannot be accessed on the current system.\n\n");
1375 } else {
1376 str = gettext("The following filesystems are "
1377 "out of date, and can be upgraded. After being\n"
1378 "upgraded, these filesystems (and any 'zfs send' "
1379 "streams generated from\n"
1380 "subsequent snapshots) will no longer be "
1381 "accessible by older software versions.\n\n");
1382 }
1383
1384 if (!cb->cb_foundone) {
1385 (void) puts(str);
1386 (void) printf(gettext("VER FILESYSTEM\n"));
1387 (void) printf(gettext("--- ------------\n"));
1388 cb->cb_foundone = B_TRUE;
1389 }
1390
1391 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1392 }
1393
1394 return (0);
1395}
1396
1397static int
1398upgrade_set_callback(zfs_handle_t *zhp, void *data)
1399{
1400 upgrade_cbdata_t *cb = data;
1401 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1402
1403 if (cb->cb_version >= ZPL_VERSION_FUID) {
1404 int spa_version;
1405
1406 if (zfs_spa_version(zhp, &spa_version) < 0)
1407 return (-1);
1408
1409 if (spa_version < SPA_VERSION_FUID) {
1410 /* can't upgrade */
1411 (void) printf(gettext("%s: can not be upgraded; "
1412 "the pool version needs to first be upgraded\nto "
1413 "version %d\n\n"),
1414 zfs_get_name(zhp), SPA_VERSION_FUID);
1415 cb->cb_numfailed++;
1416 return (0);
1417 }
1418 }
1419
1420 /* upgrade */
1421 if (version < cb->cb_version) {
1422 char verstr[16];
1423 (void) snprintf(verstr, sizeof (verstr),
1424 "%llu", cb->cb_version);
1425 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1426 /*
1427 * If they did "zfs upgrade -a", then we could
1428 * be doing ioctls to different pools. We need
1429 * to log this history once to each pool.
1430 */
1431 verify(zpool_stage_history(g_zfs, history_str) == 0);
1432 }
1433 if (zfs_prop_set(zhp, "version", verstr) == 0)
1434 cb->cb_numupgraded++;
1435 else
1436 cb->cb_numfailed++;
1437 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1438 } else if (version > cb->cb_version) {
1439 /* can't downgrade */
1440 (void) printf(gettext("%s: can not be downgraded; "
1441 "it is already at version %u\n"),
1442 zfs_get_name(zhp), version);
1443 cb->cb_numfailed++;
1444 } else {
1445 cb->cb_numsamegraded++;
1446 }
1447 return (0);
1448}
1449
1450/*
1451 * zfs upgrade
1452 * zfs upgrade -v
1453 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1454 */
1455static int
1456zfs_do_upgrade(int argc, char **argv)
1457{
1458 boolean_t recurse = B_FALSE;
1459 boolean_t all = B_FALSE;
1460 boolean_t showversions = B_FALSE;
1461 int ret;
1462 upgrade_cbdata_t cb = { 0 };
1463 char c;
1464
1465 /* check options */
1466 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1467 switch (c) {
1468 case 'r':
1469 recurse = B_TRUE;
1470 break;
1471 case 'v':
1472 showversions = B_TRUE;
1473 break;
1474 case 'V':
1475 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1476 optarg, &cb.cb_version) != 0) {
1477 (void) fprintf(stderr,
1478 gettext("invalid version %s\n"), optarg);
1479 usage(B_FALSE);
1480 }
1481 break;
1482 case 'a':
1483 all = B_TRUE;
1484 break;
1485 case '?':
1486 default:
1487 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1488 optopt);
1489 usage(B_FALSE);
1490 }
1491 }
1492
1493 argc -= optind;
1494 argv += optind;
1495
1496 if ((!all && !argc) && (recurse | cb.cb_version))
1497 usage(B_FALSE);
1498 if (showversions && (recurse || all || cb.cb_version || argc))
1499 usage(B_FALSE);
1500 if ((all || argc) && (showversions))
1501 usage(B_FALSE);
1502 if (all && argc)
1503 usage(B_FALSE);
1504
1505 if (showversions) {
1506 /* Show info on available versions. */
1507 (void) printf(gettext("The following filesystem versions are "
1508 "supported:\n\n"));
1509 (void) printf(gettext("VER DESCRIPTION\n"));
1510 (void) printf("--- -----------------------------------------"
1511 "---------------\n");
1512 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1513 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1514 (void) printf(gettext(" 3 Case insensitive and File system "
1515 "unique identifer (FUID)\n"));
1516 (void) printf(gettext("\nFor more information on a particular "
1517 "version, including supported releases, see:\n\n"));
1518 (void) printf("http://www.opensolaris.org/os/community/zfs/"
1519 "version/zpl/N\n\n");
1520 (void) printf(gettext("Where 'N' is the version number.\n"));
1521 ret = 0;
1522 } else if (argc || all) {
1523 /* Upgrade filesystems */
1524 if (cb.cb_version == 0)
1525 cb.cb_version = ZPL_VERSION;
1526 ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_FILESYSTEM,
1527 NULL, NULL, upgrade_set_callback, &cb, B_TRUE);
1528 (void) printf(gettext("%llu filesystems upgraded\n"),
1529 cb.cb_numupgraded);
1530 if (cb.cb_numsamegraded) {
1531 (void) printf(gettext("%llu filesystems already at "
1532 "this version\n"),
1533 cb.cb_numsamegraded);
1534 }
1535 if (cb.cb_numfailed != 0)
1536 ret = 1;
1537 } else {
1538 /* List old-version filesytems */
1539 boolean_t found;
1540 (void) printf(gettext("This system is currently running "
1541 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1542
1543 ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
1544 NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
1545
1546 found = cb.cb_foundone;
1547 cb.cb_foundone = B_FALSE;
1548 cb.cb_newer = B_TRUE;
1549
1550 ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
1551 NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
1552
1553 if (!cb.cb_foundone && !found) {
1554 (void) printf(gettext("All filesystems are "
1555 "formatted with the current version.\n"));
1556 }
1557 }
1558
1559 return (ret);
1560}
1561
1562/*
1563 * list [-rH] [-o property[,property]...] [-t type[,type]...]
1564 * [-s property [-s property]...] [-S property [-S property]...]
1565 * <dataset> ...
1566 *
1567 * -r Recurse over all children
1568 * -H Scripted mode; elide headers and separate columns by tabs
1569 * -o Control which fields to display.
1570 * -t Control which object types to display.
1571 * -s Specify sort columns, descending order.
1572 * -S Specify sort columns, ascending order.
1573 *
1574 * When given no arguments, lists all filesystems in the system.
1575 * Otherwise, list the specified datasets, optionally recursing down them if
1576 * '-r' is specified.
1577 */
1578typedef struct list_cbdata {
1579 boolean_t cb_first;
1580 boolean_t cb_scripted;
1581 zprop_list_t *cb_proplist;
1582} list_cbdata_t;
1583
1584/*
1585 * Given a list of columns to display, output appropriate headers for each one.
1586 */
1587static void
1588print_header(zprop_list_t *pl)
1589{
1590 char headerbuf[ZFS_MAXPROPLEN];
1591 const char *header;
1592 int i;
1593 boolean_t first = B_TRUE;
1594 boolean_t right_justify;
1595
1596 for (; pl != NULL; pl = pl->pl_next) {
1597 if (!first) {
1598 (void) printf(" ");
1599 } else {
1600 first = B_FALSE;
1601 }
1602
1603 right_justify = B_FALSE;
1604 if (pl->pl_prop != ZPROP_INVAL) {
1605 header = zfs_prop_column_name(pl->pl_prop);
1606 right_justify = zfs_prop_align_right(pl->pl_prop);
1607 } else {
1608 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1609 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1610 headerbuf[i] = '\0';
1611 header = headerbuf;
1612 }
1613
1614 if (pl->pl_next == NULL && !right_justify)
1615 (void) printf("%s", header);
1616 else if (right_justify)
1617 (void) printf("%*s", pl->pl_width, header);
1618 else
1619 (void) printf("%-*s", pl->pl_width, header);
1620 }
1621
1622 (void) printf("\n");
1623}
1624
1625/*
1626 * Given a dataset and a list of fields, print out all the properties according
1627 * to the described layout.
1628 */
1629static void
1630print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1631{
1632 boolean_t first = B_TRUE;
1633 char property[ZFS_MAXPROPLEN];
1634 nvlist_t *userprops = zfs_get_user_props(zhp);
1635 nvlist_t *propval;
1636 char *propstr;
1637 boolean_t right_justify;
1638 int width;
1639
1640 for (; pl != NULL; pl = pl->pl_next) {
1641 if (!first) {
1642 if (scripted)
1643 (void) printf("\t");
1644 else
1645 (void) printf(" ");
1646 } else {
1647 first = B_FALSE;
1648 }
1649
1650 right_justify = B_FALSE;
1651 if (pl->pl_prop != ZPROP_INVAL) {
1652 if (zfs_prop_get(zhp, pl->pl_prop, property,
1653 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1654 propstr = "-";
1655 else
1656 propstr = property;
1657
1658 right_justify = zfs_prop_align_right(pl->pl_prop);
1659 } else {
1660 if (nvlist_lookup_nvlist(userprops,
1661 pl->pl_user_prop, &propval) != 0)
1662 propstr = "-";
1663 else
1664 verify(nvlist_lookup_string(propval,
1665 ZPROP_VALUE, &propstr) == 0);
1666 }
1667
1668 width = pl->pl_width;
1669
1670 /*
1671 * If this is being called in scripted mode, or if this is the
1672 * last column and it is left-justified, don't include a width
1673 * format specifier.
1674 */
1675 if (scripted || (pl->pl_next == NULL && !right_justify))
1676 (void) printf("%s", propstr);
1677 else if (right_justify)
1678 (void) printf("%*s", width, propstr);
1679 else
1680 (void) printf("%-*s", width, propstr);
1681 }
1682
1683 (void) printf("\n");
1684}
1685
1686/*
1687 * Generic callback function to list a dataset or snapshot.
1688 */
1689static int
1690list_callback(zfs_handle_t *zhp, void *data)
1691{
1692 list_cbdata_t *cbp = data;
1693
1694 if (cbp->cb_first) {
1695 if (!cbp->cb_scripted)
1696 print_header(cbp->cb_proplist);
1697 cbp->cb_first = B_FALSE;
1698 }
1699
1700 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
1701
1702 return (0);
1703}
1704
1705static int
1706zfs_do_list(int argc, char **argv)
1707{
1708 int c;
1709 boolean_t recurse = B_FALSE;
1710 boolean_t scripted = B_FALSE;
1711 static char default_fields[] =
1712 "name,used,available,referenced,mountpoint";
1713 int types = ZFS_TYPE_DATASET;
1714 char *fields = NULL;
1715 char *basic_fields = default_fields;
1716 list_cbdata_t cb = { 0 };
1717 char *value;
1718 int ret;
1719 char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL };
1720 zfs_sort_column_t *sortcol = NULL;
1721
1722 /* check options */
1723 while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
1724 switch (c) {
1725 case 'o':
1726 fields = optarg;
1727 break;
1728 case 'r':
1729 recurse = B_TRUE;
1730 break;
1731 case 'H':
1732 scripted = B_TRUE;
1733 break;
1734 case 's':
1735 if (zfs_add_sort_column(&sortcol, optarg,
1736 B_FALSE) != 0) {
1737 (void) fprintf(stderr,
1738 gettext("invalid property '%s'\n"), optarg);
1739 usage(B_FALSE);
1740 }
1741 break;
1742 case 'S':
1743 if (zfs_add_sort_column(&sortcol, optarg,
1744 B_TRUE) != 0) {
1745 (void) fprintf(stderr,
1746 gettext("invalid property '%s'\n"), optarg);
1747 usage(B_FALSE);
1748 }
1749 break;
1750 case 't':
1751 types = 0;
1752 while (*optarg != '\0') {
1753 switch (getsubopt(&optarg, type_subopts,
1754 &value)) {
1755 case 0:
1756 types |= ZFS_TYPE_FILESYSTEM;
1757 break;
1758 case 1:
1759 types |= ZFS_TYPE_VOLUME;
1760 break;
1761 case 2:
1762 types |= ZFS_TYPE_SNAPSHOT;
1763 break;
1764 default:
1765 (void) fprintf(stderr,
1766 gettext("invalid type '%s'\n"),
1767 value);
1768 usage(B_FALSE);
1769 }
1770 }
1771 break;
1772 case ':':
1773 (void) fprintf(stderr, gettext("missing argument for "
1774 "'%c' option\n"), optopt);
1775 usage(B_FALSE);
1776 break;
1777 case '?':
1778 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1779 optopt);
1780 usage(B_FALSE);
1781 }
1782 }
1783
1784 argc -= optind;
1785 argv += optind;
1786
1787 if (fields == NULL)
1788 fields = basic_fields;
1789
1790 /*
1791 * If the user specifies '-o all', the zprop_get_list() doesn't
1792 * normally include the name of the dataset. For 'zfs list', we always
1793 * want this property to be first.
1794 */
1795 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1796 != 0)
1797 usage(B_FALSE);
1798
1799 cb.cb_scripted = scripted;
1800 cb.cb_first = B_TRUE;
1801
1802 ret = zfs_for_each(argc, argv, recurse, types, sortcol, &cb.cb_proplist,
1803 list_callback, &cb, B_TRUE);
1804
1805 zprop_free_list(cb.cb_proplist);
1806 zfs_free_sort_columns(sortcol);
1807
1808 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
1809 (void) printf(gettext("no datasets available\n"));
1810
1811 return (ret);
1812}
1813
1814/*
1815 * zfs rename <fs | snap | vol> <fs | snap | vol>
1816 * zfs rename -p <fs | vol> <fs | vol>
1817 * zfs rename -r <snap> <snap>
1818 *
1819 * Renames the given dataset to another of the same type.
1820 *
1821 * The '-p' flag creates all the non-existing ancestors of the target first.
1822 */
1823/* ARGSUSED */
1824static int
1825zfs_do_rename(int argc, char **argv)
1826{
1827 zfs_handle_t *zhp;
1828 int c;
1829 int ret;
1830 boolean_t recurse = B_FALSE;
1831 boolean_t parents = B_FALSE;
1832
1833 /* check options */
1834 while ((c = getopt(argc, argv, "pr")) != -1) {
1835 switch (c) {
1836 case 'p':
1837 parents = B_TRUE;
1838 break;
1839 case 'r':
1840 recurse = B_TRUE;
1841 break;
1842 case '?':
1843 default:
1844 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1845 optopt);
1846 usage(B_FALSE);
1847 }
1848 }
1849
1850 argc -= optind;
1851 argv += optind;
1852
1853 /* check number of arguments */
1854 if (argc < 1) {
1855 (void) fprintf(stderr, gettext("missing source dataset "
1856 "argument\n"));
1857 usage(B_FALSE);
1858 }
1859 if (argc < 2) {
1860 (void) fprintf(stderr, gettext("missing target dataset "
1861 "argument\n"));
1862 usage(B_FALSE);
1863 }
1864 if (argc > 2) {
1865 (void) fprintf(stderr, gettext("too many arguments\n"));
1866 usage(B_FALSE);
1867 }
1868
1869 if (recurse && parents) {
1870 (void) fprintf(stderr, gettext("-p and -r options are mutually "
1871 "exclusive\n"));
1872 usage(B_FALSE);
1873 }
1874
1875 if (recurse && strchr(argv[0], '@') == 0) {
1876 (void) fprintf(stderr, gettext("source dataset for recursive "
1877 "rename must be a snapshot\n"));
1878 usage(B_FALSE);
1879 }
1880
1881 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
1882 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
1883 return (1);
1884
1885 /* If we were asked and the name looks good, try to create ancestors. */
1886 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
1887 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
1888 zfs_close(zhp);
1889 return (1);
1890 }
1891
1892 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
1893
1894 zfs_close(zhp);
1895 return (ret);
1896}
1897
1898/*
1899 * zfs promote <fs>
1900 *
1901 * Promotes the given clone fs to be the parent
1902 */
1903/* ARGSUSED */
1904static int
1905zfs_do_promote(int argc, char **argv)
1906{
1907 zfs_handle_t *zhp;
1908 int ret;
1909
1910 /* check options */
1911 if (argc > 1 && argv[1][0] == '-') {
1912 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1913 argv[1][1]);
1914 usage(B_FALSE);
1915 }
1916
1917 /* check number of arguments */
1918 if (argc < 2) {
1919 (void) fprintf(stderr, gettext("missing clone filesystem"
1920 " argument\n"));
1921 usage(B_FALSE);
1922 }
1923 if (argc > 2) {
1924 (void) fprintf(stderr, gettext("too many arguments\n"));
1925 usage(B_FALSE);
1926 }
1927
1928 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1929 if (zhp == NULL)
1930 return (1);
1931
1932 ret = (zfs_promote(zhp) != 0);
1933
1934
1935 zfs_close(zhp);
1936 return (ret);
1937}
1938
1939/*
1940 * zfs rollback [-rRf] <snapshot>
1941 *
1942 * -r Delete any intervening snapshots before doing rollback
1943 * -R Delete any snapshots and their clones
1944 * -f ignored for backwards compatability
1945 *
1946 * Given a filesystem, rollback to a specific snapshot, discarding any changes
1947 * since then and making it the active dataset. If more recent snapshots exist,
1948 * the command will complain unless the '-r' flag is given.
1949 */
1950typedef struct rollback_cbdata {
1951 uint64_t cb_create;
1952 boolean_t cb_first;
1953 int cb_doclones;
1954 char *cb_target;
1955 int cb_error;
1956 boolean_t cb_recurse;
1957 boolean_t cb_dependent;
1958} rollback_cbdata_t;
1959
1960/*
1961 * Report any snapshots more recent than the one specified. Used when '-r' is
1962 * not specified. We reuse this same callback for the snapshot dependents - if
1963 * 'cb_dependent' is set, then this is a dependent and we should report it
1964 * without checking the transaction group.
1965 */
1966static int
1967rollback_check(zfs_handle_t *zhp, void *data)
1968{
1969 rollback_cbdata_t *cbp = data;
1970
1971 if (cbp->cb_doclones) {
1972 zfs_close(zhp);
1973 return (0);
1974 }
1975
1976 if (!cbp->cb_dependent) {
1977 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
1978 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1979 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
1980 cbp->cb_create) {
1981
1982 if (cbp->cb_first && !cbp->cb_recurse) {
1983 (void) fprintf(stderr, gettext("cannot "
1984 "rollback to '%s': more recent snapshots "
1985 "exist\n"),
1986 cbp->cb_target);
1987 (void) fprintf(stderr, gettext("use '-r' to "
1988 "force deletion of the following "
1989 "snapshots:\n"));
1990 cbp->cb_first = 0;
1991 cbp->cb_error = 1;
1992 }
1993
1994 if (cbp->cb_recurse) {
1995 cbp->cb_dependent = B_TRUE;
1996 if (zfs_iter_dependents(zhp, B_TRUE,
1997 rollback_check, cbp) != 0) {
1998 zfs_close(zhp);
1999 return (-1);
2000 }
2001 cbp->cb_dependent = B_FALSE;
2002 } else {
2003 (void) fprintf(stderr, "%s\n",
2004 zfs_get_name(zhp));
2005 }
2006 }
2007 } else {
2008 if (cbp->cb_first && cbp->cb_recurse) {
2009 (void) fprintf(stderr, gettext("cannot rollback to "
2010 "'%s': clones of previous snapshots exist\n"),
2011 cbp->cb_target);
2012 (void) fprintf(stderr, gettext("use '-R' to "
2013 "force deletion of the following clones and "
2014 "dependents:\n"));
2015 cbp->cb_first = 0;
2016 cbp->cb_error = 1;
2017 }
2018
2019 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2020 }
2021
2022 zfs_close(zhp);
2023 return (0);
2024}
2025
2026static int
2027zfs_do_rollback(int argc, char **argv)
2028{
2029 int ret;
2030 int c;
2031 boolean_t force = B_FALSE;
2032 rollback_cbdata_t cb = { 0 };
2033 zfs_handle_t *zhp, *snap;
2034 char parentname[ZFS_MAXNAMELEN];
2035 char *delim;
2036
2037 /* check options */
2038 while ((c = getopt(argc, argv, "rRf")) != -1) {
2039 switch (c) {
2040 case 'r':
2041 cb.cb_recurse = 1;
2042 break;
2043 case 'R':
2044 cb.cb_recurse = 1;
2045 cb.cb_doclones = 1;
2046 break;
2047 case 'f':
2048 force = B_TRUE;
2049 break;
2050 case '?':
2051 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2052 optopt);
2053 usage(B_FALSE);
2054 }
2055 }
2056
2057 argc -= optind;
2058 argv += optind;
2059
2060 /* check number of arguments */
2061 if (argc < 1) {
2062 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2063 usage(B_FALSE);
2064 }
2065 if (argc > 1) {
2066 (void) fprintf(stderr, gettext("too many arguments\n"));
2067 usage(B_FALSE);
2068 }
2069
2070 /* open the snapshot */
2071 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2072 return (1);
2073
2074 /* open the parent dataset */
2075 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2076 verify((delim = strrchr(parentname, '@')) != NULL);
2077 *delim = '\0';
2078 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2079 zfs_close(snap);
2080 return (1);
2081 }
2082
2083 /*
2084 * Check for more recent snapshots and/or clones based on the presence
2085 * of '-r' and '-R'.
2086 */
2087 cb.cb_target = argv[0];
2088 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2089 cb.cb_first = B_TRUE;
2090 cb.cb_error = 0;
2091 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2092 goto out;
2093
2094 if ((ret = cb.cb_error) != 0)
2095 goto out;
2096
2097 /*
2098 * Rollback parent to the given snapshot.
2099 */
2100 ret = zfs_rollback(zhp, snap, force);
2101
2102out:
2103 zfs_close(snap);
2104 zfs_close(zhp);
2105
2106 if (ret == 0)
2107 return (0);
2108 else
2109 return (1);
2110}
2111
2112/*
2113 * zfs set property=value { fs | snap | vol } ...
2114 *
2115 * Sets the given property for all datasets specified on the command line.
2116 */
2117typedef struct set_cbdata {
2118 char *cb_propname;
2119 char *cb_value;
2120} set_cbdata_t;
2121
2122static int
2123set_callback(zfs_handle_t *zhp, void *data)
2124{
2125 set_cbdata_t *cbp = data;
2126
2127 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2128 switch (libzfs_errno(g_zfs)) {
2129 case EZFS_MOUNTFAILED:
2130 (void) fprintf(stderr, gettext("property may be set "
2131 "but unable to remount filesystem\n"));
2132 break;
2133 case EZFS_SHARENFSFAILED:
2134 (void) fprintf(stderr, gettext("property may be set "
2135 "but unable to reshare filesystem\n"));
2136 break;
2137 }
2138 return (1);
2139 }
2140 return (0);
2141}
2142
2143static int
2144zfs_do_set(int argc, char **argv)
2145{
2146 set_cbdata_t cb;
2147 int ret;
2148
2149 /* check for options */
2150 if (argc > 1 && argv[1][0] == '-') {
2151 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2152 argv[1][1]);
2153 usage(B_FALSE);
2154 }
2155
2156 /* check number of arguments */
2157 if (argc < 2) {
2158 (void) fprintf(stderr, gettext("missing property=value "
2159 "argument\n"));
2160 usage(B_FALSE);
2161 }
2162 if (argc < 3) {
2163 (void) fprintf(stderr, gettext("missing dataset name\n"));
2164 usage(B_FALSE);
2165 }
2166
2167 /* validate property=value argument */
2168 cb.cb_propname = argv[1];
2169 if ((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) {
2170 (void) fprintf(stderr, gettext("missing value in "
2171 "property=value argument\n"));
2172 usage(B_FALSE);
2173 }
2174
2175 *cb.cb_value = '\0';
2176 cb.cb_value++;
2177
2178 if (*cb.cb_propname == '\0') {
2179 (void) fprintf(stderr,
2180 gettext("missing property in property=value argument\n"));
2181 usage(B_FALSE);
2182 }
2183
2184
2185 ret = zfs_for_each(argc - 2, argv + 2, B_FALSE,
2186 ZFS_TYPE_DATASET, NULL, NULL, set_callback, &cb, B_FALSE);
2187
2188 return (ret);
2189}
2190
2191/*
2192 * zfs snapshot [-r] <fs@snap>
2193 *
2194 * Creates a snapshot with the given name. While functionally equivalent to
2195 * 'zfs create', it is a separate command to differentiate intent.
2196 */
2197static int
2198zfs_do_snapshot(int argc, char **argv)
2199{
2200 boolean_t recursive = B_FALSE;
2201 int ret;
2202 char c;
2203
2204 /* check options */
2205 while ((c = getopt(argc, argv, ":r")) != -1) {
2206 switch (c) {
2207 case 'r':
2208 recursive = B_TRUE;
2209 break;
2210 case '?':
2211 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2212 optopt);
2213 usage(B_FALSE);
2214 }
2215 }
2216
2217 argc -= optind;
2218 argv += optind;
2219
2220 /* check number of arguments */
2221 if (argc < 1) {
2222 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2223 usage(B_FALSE);
2224 }
2225 if (argc > 1) {
2226 (void) fprintf(stderr, gettext("too many arguments\n"));
2227 usage(B_FALSE);
2228 }
2229
2230 ret = zfs_snapshot(g_zfs, argv[0], recursive);
2231 if (ret && recursive)
2232 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2233 return (ret != 0);
2234}
2235
2236/*
2237 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2238 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2239 *
2240 * Send a backup stream to stdout.
2241 */
2242static int
2243zfs_do_send(int argc, char **argv)
2244{
2245 char *fromname = NULL;
2246 char *toname = NULL;
2247 char *cp;
2248 zfs_handle_t *zhp;
2249 boolean_t doall = B_FALSE;
2250 boolean_t replicate = B_FALSE;
2251 boolean_t fromorigin = B_FALSE;
2252 boolean_t verbose = B_FALSE;
2253 int c, err;
2254
2255 /* check options */
2256 while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
2257 switch (c) {
2258 case 'i':
2259 if (fromname)
2260 usage(B_FALSE);
2261 fromname = optarg;
2262 break;
2263 case 'I':
2264 if (fromname)
2265 usage(B_FALSE);
2266 fromname = optarg;
2267 doall = B_TRUE;
2268 break;
2269 case 'R':
2270 replicate = B_TRUE;
2271 break;
2272 case 'v':
2273 verbose = B_TRUE;
2274 break;
2275 case ':':
2276 (void) fprintf(stderr, gettext("missing argument for "
2277 "'%c' option\n"), optopt);
2278 usage(B_FALSE);
2279 break;
2280 case '?':
2281 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2282 optopt);
2283 usage(B_FALSE);
2284 }
2285 }
2286
2287 argc -= optind;
2288 argv += optind;
2289
2290 /* check number of arguments */
2291 if (argc < 1) {
2292 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2293 usage(B_FALSE);
2294 }
2295 if (argc > 1) {
2296 (void) fprintf(stderr, gettext("too many arguments\n"));
2297 usage(B_FALSE);
2298 }
2299
2300 if (isatty(STDOUT_FILENO)) {
2301 (void) fprintf(stderr,
2302 gettext("Error: Stream can not be written to a terminal.\n"
2303 "You must redirect standard output.\n"));
2304 return (1);
2305 }
2306
2307 cp = strchr(argv[0], '@');
2308 if (cp == NULL) {
2309 (void) fprintf(stderr,
2310 gettext("argument must be a snapshot\n"));
2311 usage(B_FALSE);
2312 }
2313 *cp = '\0';
2314 toname = cp + 1;
2315 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2316 if (zhp == NULL)
2317 return (1);
2318
2319 /*
2320 * If they specified the full path to the snapshot, chop off
2321 * everything except the short name of the snapshot, but special
2322 * case if they specify the origin.
2323 */
2324 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2325 char origin[ZFS_MAXNAMELEN];
2326 zprop_source_t src;
2327
2328 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2329 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2330
2331 if (strcmp(origin, fromname) == 0) {
2332 fromname = NULL;
2333 fromorigin = B_TRUE;
2334 } else {
2335 *cp = '\0';
2336 if (cp != fromname && strcmp(argv[0], fromname)) {
2337 (void) fprintf(stderr,
2338 gettext("incremental source must be "
2339 "in same filesystem\n"));
2340 usage(B_FALSE);
2341 }
2342 fromname = cp + 1;
2343 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2344 (void) fprintf(stderr,
2345 gettext("invalid incremental source\n"));
2346 usage(B_FALSE);
2347 }
2348 }
2349 }
2350
2351 if (replicate && fromname == NULL)
2352 doall = B_TRUE;
2353
2354 err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
2355 verbose, STDOUT_FILENO);
2356 zfs_close(zhp);
2357
2358 return (err != 0);
2359}
2360
2361/*
2362 * zfs receive [-dnvF] <fs@snap>
2363 *
2364 * Restore a backup stream from stdin.
2365 */
2366static int
2367zfs_do_receive(int argc, char **argv)
2368{
2369 int c, err;
2370 recvflags_t flags;
2371
2372 bzero(&flags, sizeof (recvflags_t));
2373 /* check options */
2374 while ((c = getopt(argc, argv, ":dnvF")) != -1) {
2375 switch (c) {
2376 case 'd':
2377 flags.isprefix = B_TRUE;
2378 break;
2379 case 'n':
2380 flags.dryrun = B_TRUE;
2381 break;
2382 case 'v':
2383 flags.verbose = B_TRUE;
2384 break;
2385 case 'F':
2386 flags.force = B_TRUE;
2387 break;
2388 case ':':
2389 (void) fprintf(stderr, gettext("missing argument for "
2390 "'%c' option\n"), optopt);
2391 usage(B_FALSE);
2392 break;
2393 case '?':
2394 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2395 optopt);
2396 usage(B_FALSE);
2397 }
2398 }
2399
2400 argc -= optind;
2401 argv += optind;
2402
2403 /* check number of arguments */
2404 if (argc < 1) {
2405 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2406 usage(B_FALSE);
2407 }
2408 if (argc > 1) {
2409 (void) fprintf(stderr, gettext("too many arguments\n"));
2410 usage(B_FALSE);
2411 }
2412
2413 if (isatty(STDIN_FILENO)) {
2414 (void) fprintf(stderr,
2415 gettext("Error: Backup stream can not be read "
2416 "from a terminal.\n"
2417 "You must redirect standard input.\n"));
2418 return (1);
2419 }
2420
2421 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2422
2423 return (err != 0);
2424}
2425
2426typedef struct allow_cb {
2427 int a_permcnt;
2428 size_t a_treeoffset;
2429} allow_cb_t;
2430
2431static void
2432zfs_print_perms(avl_tree_t *tree)
2433{
2434 zfs_perm_node_t *permnode;
2435
2436 permnode = avl_first(tree);
2437 while (permnode != NULL) {
2438 (void) printf("%s", permnode->z_pname);
2439 permnode = AVL_NEXT(tree, permnode);
2440 if (permnode)
2441 (void) printf(",");
2442 else
2443 (void) printf("\n");
2444 }
2445}
2446
2447/*
2448 * Iterate over user/groups/everyone/... and the call perm_iter
2449 * function to print actual permission when tree has >0 nodes.
2450 */
2451static void
2452zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb)
2453{
2454 zfs_allow_node_t *item;
2455 avl_tree_t *ptree;
2456
2457 item = avl_first(tree);
2458 while (item) {
2459 ptree = (void *)((char *)item + cb->a_treeoffset);
2460 if (avl_numnodes(ptree)) {
2461 if (cb->a_permcnt++ == 0)
2462 (void) printf("%s\n", banner);
2463 (void) printf("\t%s", item->z_key);
2464 /*
2465 * Avoid an extra space being printed
2466 * for "everyone" which is keyed with a null
2467 * string
2468 */
2469 if (item->z_key[0] != '\0')
2470 (void) printf(" ");
2471 zfs_print_perms(ptree);
2472 }
2473 item = AVL_NEXT(tree, item);
2474 }
2475}
2476
2477#define LINES "-------------------------------------------------------------\n"
2478static int
2479zfs_print_allows(char *ds)
2480{
2481 zfs_allow_t *curperms, *perms;
2482 zfs_handle_t *zhp;
2483 allow_cb_t allowcb = { 0 };
2484 char banner[MAXPATHLEN];
2485
2486 if (ds[0] == '-')
2487 usage(B_FALSE);
2488
2489 if (strrchr(ds, '@')) {
2490 (void) fprintf(stderr, gettext("Snapshots don't have 'allow'"
2491 " permissions\n"));
2492 return (1);
2493 }
2494 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2495 return (1);
2496
2497 if (zfs_perm_get(zhp, &perms)) {
2498 (void) fprintf(stderr,
2499 gettext("Failed to retrieve 'allows' on %s\n"), ds);
2500 zfs_close(zhp);
2501 return (1);
2502 }
2503
2504 zfs_close(zhp);
2505
2506 if (perms != NULL)
2507 (void) printf("%s", LINES);
2508 for (curperms = perms; curperms; curperms = curperms->z_next) {
2509
2510 (void) snprintf(banner, sizeof (banner),
2511 "Permission sets on (%s)", curperms->z_setpoint);
2512 allowcb.a_treeoffset =
2513 offsetof(zfs_allow_node_t, z_localdescend);
2514 allowcb.a_permcnt = 0;
2515 zfs_iter_perms(&curperms->z_sets, banner, &allowcb);
2516
2517 (void) snprintf(banner, sizeof (banner),
2518 "Create time permissions on (%s)", curperms->z_setpoint);
2519 allowcb.a_treeoffset =
2520 offsetof(zfs_allow_node_t, z_localdescend);
2521 allowcb.a_permcnt = 0;
2522 zfs_iter_perms(&curperms->z_crperms, banner, &allowcb);
2523
2524
2525 (void) snprintf(banner, sizeof (banner),
2526 "Local permissions on (%s)", curperms->z_setpoint);
2527 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local);
2528 allowcb.a_permcnt = 0;
2529 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2530 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2531 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2532
2533 (void) snprintf(banner, sizeof (banner),
2534 "Descendent permissions on (%s)", curperms->z_setpoint);
2535 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend);
2536 allowcb.a_permcnt = 0;
2537 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2538 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2539 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2540
2541 (void) snprintf(banner, sizeof (banner),
2542 "Local+Descendent permissions on (%s)",
2543 curperms->z_setpoint);
2544 allowcb.a_treeoffset =
2545 offsetof(zfs_allow_node_t, z_localdescend);
2546 allowcb.a_permcnt = 0;
2547 zfs_iter_perms(&curperms->z_user, banner, &allowcb);
2548 zfs_iter_perms(&curperms->z_group, banner, &allowcb);
2549 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
2550
2551 (void) printf("%s", LINES);
2552 }
2553 zfs_free_allows(perms);
2554 return (0);
2555}
2556
2557#define ALLOWOPTIONS "ldcsu:g:e"
2558#define UNALLOWOPTIONS "ldcsu:g:er"
2559
2560/*
2561 * Validate options, and build necessary datastructure to display/remove/add
2562 * permissions.
2563 * Returns 0 - If permissions should be added/removed
2564 * Returns 1 - If permissions should be displayed.
2565 * Returns -1 - on failure
2566 */
2567int
2568parse_allow_args(int *argc, char **argv[], boolean_t unallow,
2569 char **ds, int *recurse, nvlist_t **zperms)
2570{
2571 int c;
2572 char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS;
2573 zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE;
2574 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
2575 char *who = NULL;
2576 char *perms = NULL;
2577 zfs_handle_t *zhp;
2578
2579 while ((c = getopt(*argc, *argv, options)) != -1) {
2580 switch (c) {
2581 case 'l':
2582 if (who_type == ZFS_DELEG_CREATE ||
2583 who_type == ZFS_DELEG_NAMED_SET)
2584 usage(B_FALSE);
2585
2586 deleg_type |= ZFS_DELEG_PERM_LOCAL;
2587 break;
2588 case 'd':
2589 if (who_type == ZFS_DELEG_CREATE ||
2590 who_type == ZFS_DELEG_NAMED_SET)
2591 usage(B_FALSE);
2592
2593 deleg_type |= ZFS_DELEG_PERM_DESCENDENT;
2594 break;
2595 case 'r':
2596 *recurse = B_TRUE;
2597 break;
2598 case 'c':
2599 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2600 usage(B_FALSE);
2601 if (deleg_type)
2602 usage(B_FALSE);
2603 who_type = ZFS_DELEG_CREATE;
2604 break;
2605 case 's':
2606 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2607 usage(B_FALSE);
2608 if (deleg_type)
2609 usage(B_FALSE);
2610 who_type = ZFS_DELEG_NAMED_SET;
2611 break;
2612 case 'u':
2613 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2614 usage(B_FALSE);
2615 who_type = ZFS_DELEG_USER;
2616 who = optarg;
2617 break;
2618 case 'g':
2619 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2620 usage(B_FALSE);
2621 who_type = ZFS_DELEG_GROUP;
2622 who = optarg;
2623 break;
2624 case 'e':
2625 if (who_type != ZFS_DELEG_WHO_UNKNOWN)
2626 usage(B_FALSE);
2627 who_type = ZFS_DELEG_EVERYONE;
2628 break;
2629 default:
2630 usage(B_FALSE);
2631 break;
2632 }
2633 }
2634
2635 if (deleg_type == 0)
2636 deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT;
2637
2638 *argc -= optind;
2639 *argv += optind;
2640
2641 if (unallow == B_FALSE && *argc == 1) {
2642 /*
2643 * Only print permissions if no options were processed
2644 */
2645 if (optind == 1)
2646 return (1);
2647 else
2648 usage(B_FALSE);
2649 }
2650
2651 /*
2652 * initialize variables for zfs_build_perms based on number
2653 * of arguments.
2654 * 3 arguments ==> zfs [un]allow joe perm,perm,perm <dataset> or
2655 * zfs [un]allow -s @set1 perm,perm <dataset>
2656 * 2 arguments ==> zfs [un]allow -c perm,perm <dataset> or
2657 * zfs [un]allow -u|-g <name> perm <dataset> or
2658 * zfs [un]allow -e perm,perm <dataset>
2659 * zfs unallow joe <dataset>
2660 * zfs unallow -s @set1 <dataset>
2661 * 1 argument ==> zfs [un]allow -e <dataset> or
2662 * zfs [un]allow -c <dataset>
2663 */
2664
2665 switch (*argc) {
2666 case 3:
2667 perms = (*argv)[1];
2668 who = (*argv)[0];
2669 *ds = (*argv)[2];
2670
2671 /*
2672 * advance argc/argv for do_allow cases.
2673 * for do_allow case make sure who have a know who type
2674 * and its not a permission set.
2675 */
2676 if (unallow == B_TRUE) {
2677 *argc -= 2;
2678 *argv += 2;
2679 } else if (who_type != ZFS_DELEG_WHO_UNKNOWN &&
2680 who_type != ZFS_DELEG_NAMED_SET)
2681 usage(B_FALSE);
2682 break;
2683
2684 case 2:
2685 if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE ||
2686 who_type == ZFS_DELEG_CREATE || who != NULL)) {
2687 perms = (*argv)[0];
2688 *ds = (*argv)[1];
2689 } else {
2690 if (unallow == B_FALSE &&
2691 (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2692 who_type == ZFS_DELEG_NAMED_SET))
2693 usage(B_FALSE);
2694 else if (who_type == ZFS_DELEG_WHO_UNKNOWN ||
2695 who_type == ZFS_DELEG_NAMED_SET)
2696 who = (*argv)[0];
2697 else if (who_type != ZFS_DELEG_NAMED_SET)
2698 perms = (*argv)[0];
2699 *ds = (*argv)[1];
2700 }
2701 if (unallow == B_TRUE) {
2702 (*argc)--;
2703 (*argv)++;
2704 }
2705 break;
2706
2707 case 1:
2708 if (unallow == B_FALSE)
2709 usage(B_FALSE);
2710 if (who == NULL && who_type != ZFS_DELEG_CREATE &&
2711 who_type != ZFS_DELEG_EVERYONE)
2712 usage(B_FALSE);
2713 *ds = (*argv)[0];
2714 break;
2715
2716 default:
2717 usage(B_FALSE);
2718 }
2719
2720 if (strrchr(*ds, '@')) {
2721 (void) fprintf(stderr,
2722 gettext("Can't set or remove 'allow' permissions "
2723 "on snapshots.\n"));
2724 return (-1);
2725 }
2726
2727 if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_DATASET)) == NULL)
2728 return (-1);
2729
2730 if ((zfs_build_perms(zhp, who, perms,
2731 who_type, deleg_type, zperms)) != 0) {
2732 zfs_close(zhp);
2733 return (-1);
2734 }
2735 zfs_close(zhp);
2736 return (0);
2737}
2738
2739static int
2740zfs_do_allow(int argc, char **argv)
2741{
2742 char *ds;
2743 nvlist_t *zperms = NULL;
2744 zfs_handle_t *zhp;
2745 int unused;
2746 int ret;
2747
2748 if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds,
2749 &unused, &zperms)) == -1)
2750 return (1);
2751
2752 if (ret == 1)
2753 return (zfs_print_allows(argv[0]));
2754
2755 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
2756 return (1);
2757
2758 if (zfs_perm_set(zhp, zperms)) {
2759 zfs_close(zhp);
2760 nvlist_free(zperms);
2761 return (1);
2762 }
2763 nvlist_free(zperms);
2764 zfs_close(zhp);
2765
2766 return (0);
2767}
2768
2769static int
2770unallow_callback(zfs_handle_t *zhp, void *data)
2771{
2772 nvlist_t *nvp = (nvlist_t *)data;
2773 int error;
2774
2775 error = zfs_perm_remove(zhp, nvp);
2776 if (error) {
2777 (void) fprintf(stderr, gettext("Failed to remove permissions "
2778 "on %s\n"), zfs_get_name(zhp));
2779 }
2780 return (error);
2781}
2782
2783static int
2784zfs_do_unallow(int argc, char **argv)
2785{
2786 int recurse = B_FALSE;
2787 char *ds;
2788 int error;
2789 nvlist_t *zperms = NULL;
2790
2791 if (parse_allow_args(&argc, &argv, B_TRUE,
2792 &ds, &recurse, &zperms) == -1)
2793 return (1);
2794
2795 error = zfs_for_each(argc, argv, recurse,
2796 ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
2797 NULL, unallow_callback, (void *)zperms, B_FALSE);
2798
2799 if (zperms)
2800 nvlist_free(zperms);
2801
2802 return (error);
2803}
2804
2805typedef struct get_all_cbdata {
2806 zfs_handle_t **cb_handles;
2807 size_t cb_alloc;
2808 size_t cb_used;
2809 uint_t cb_types;
2810 boolean_t cb_verbose;
2811} get_all_cbdata_t;
2812
2813#define CHECK_SPINNER 30
2814#define SPINNER_TIME 3 /* seconds */
2815#define MOUNT_TIME 5 /* seconds */
2816
2817static int
2818get_one_dataset(zfs_handle_t *zhp, void *data)
2819{
2820 static char spin[] = { '-', '\\', '|', '/' };
2821 static int spinval = 0;
2822 static int spincheck = 0;
2823 static time_t last_spin_time = (time_t)0;
2824 get_all_cbdata_t *cbp = data;
2825 zfs_type_t type = zfs_get_type(zhp);
2826
2827 if (cbp->cb_verbose) {
2828 if (--spincheck < 0) {
2829 time_t now = time(NULL);
2830 if (last_spin_time + SPINNER_TIME < now) {
2831 (void) printf("\b%c", spin[spinval++ % 4]);
2832 (void) fflush(stdout);
2833 last_spin_time = now;
2834 }
2835 spincheck = CHECK_SPINNER;
2836 }
2837 }
2838
2839 /*
2840 * Interate over any nested datasets.
2841 */
2842 if (type == ZFS_TYPE_FILESYSTEM &&
2843 zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
2844 zfs_close(zhp);
2845 return (1);
2846 }
2847
2848 /*
2849 * Skip any datasets whose type does not match.
2850 */
2851 if ((type & cbp->cb_types) == 0) {
2852 zfs_close(zhp);
2853 return (0);
2854 }
2855
2856 if (cbp->cb_alloc == cbp->cb_used) {
2857 zfs_handle_t **handles;
2858
2859 if (cbp->cb_alloc == 0)
2860 cbp->cb_alloc = 64;
2861 else
2862 cbp->cb_alloc *= 2;
2863
2864 handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
2865
2866 if (cbp->cb_handles) {
2867 bcopy(cbp->cb_handles, handles,
2868 cbp->cb_used * sizeof (void *));
2869 free(cbp->cb_handles);
2870 }
2871
2872 cbp->cb_handles = handles;
2873 }
2874
2875 cbp->cb_handles[cbp->cb_used++] = zhp;
2876
2877 return (0);
2878}
2879
2880static void
2881get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
2882 boolean_t verbose)
2883{
2884 get_all_cbdata_t cb = { 0 };
2885 cb.cb_types = types;
2886 cb.cb_verbose = verbose;
2887
2888 if (verbose) {
2889 (void) printf("%s: *", gettext("Reading ZFS config"));
2890 (void) fflush(stdout);
2891 }
2892
2893 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2894
2895 *dslist = cb.cb_handles;
2896 *count = cb.cb_used;
2897
2898 if (verbose) {
2899 (void) printf("\b%s\n", gettext("done."));
2900 }
2901}
2902
2903static int
2904dataset_cmp(const void *a, const void *b)
2905{
2906 zfs_handle_t **za = (zfs_handle_t **)a;
2907 zfs_handle_t **zb = (zfs_handle_t **)b;
2908 char mounta[MAXPATHLEN];
2909 char mountb[MAXPATHLEN];
2910 boolean_t gota, gotb;
2911
2912 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
2913 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2914 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2915 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
2916 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2917 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2918
2919 if (gota && gotb)
2920 return (strcmp(mounta, mountb));
2921
2922 if (gota)
2923 return (-1);
2924 if (gotb)
2925 return (1);
2926
2927 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
2928}
2929
2930/*
2931 * Generic callback for sharing or mounting filesystems. Because the code is so
2932 * similar, we have a common function with an extra parameter to determine which
2933 * mode we are using.
2934 */
2935#define OP_SHARE 0x1
2936#define OP_MOUNT 0x2
2937
2938/*
2939 * Share or mount a dataset.
2940 */
2941static int
2942share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
2943 boolean_t explicit, const char *options)
2944{
2945 char mountpoint[ZFS_MAXPROPLEN];
2946 char shareopts[ZFS_MAXPROPLEN];
2947 char smbshareopts[ZFS_MAXPROPLEN];
2948 const char *cmdname = op == OP_SHARE ? "share" : "mount";
2949 struct mnttab mnt;
2950 uint64_t zoned, canmount;
2951 zfs_type_t type = zfs_get_type(zhp);
2952 boolean_t shared_nfs, shared_smb;
2953
2954 assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
2955
2956 if (type == ZFS_TYPE_FILESYSTEM) {
2957 /*
2958 * Check to make sure we can mount/share this dataset. If we
2959 * are in the global zone and the filesystem is exported to a
2960 * local zone, or if we are in a local zone and the
2961 * filesystem is not exported, then it is an error.
2962 */
2963 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2964
2965 if (zoned && getzoneid() == GLOBAL_ZONEID) {
2966 if (!explicit)
2967 return (0);
2968
2969 (void) fprintf(stderr, gettext("cannot %s '%s': "
2970 "dataset is exported to a local zone\n"), cmdname,
2971 zfs_get_name(zhp));
2972 return (1);
2973
2974 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
2975 if (!explicit)
2976 return (0);
2977
2978 (void) fprintf(stderr, gettext("cannot %s '%s': "
2979 "permission denied\n"), cmdname,
2980 zfs_get_name(zhp));
2981 return (1);
2982 }
2983
2984 /*
2985 * Ignore any filesystems which don't apply to us. This
2986 * includes those with a legacy mountpoint, or those with
2987 * legacy share options.
2988 */
2989 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2990 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
2991 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
2992 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
2993 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
2994 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
2995 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
2996
2997 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
2998 strcmp(smbshareopts, "off") == 0) {
2999 if (!explicit)
3000 return (0);
3001
3002 (void) fprintf(stderr, gettext("cannot share '%s': "
3003 "legacy share\n"), zfs_get_name(zhp));
3004 (void) fprintf(stderr, gettext("use share(1M) to "
3005 "share this filesystem\n"));
3006 return (1);
3007 }
3008
3009 /*
3010 * We cannot share or mount legacy filesystems. If the
3011 * shareopts is non-legacy but the mountpoint is legacy, we
3012 * treat it as a legacy share.
3013 */
3014 if (strcmp(mountpoint, "legacy") == 0) {
3015 if (!explicit)
3016 return (0);
3017
3018 (void) fprintf(stderr, gettext("cannot %s '%s': "
3019 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3020 (void) fprintf(stderr, gettext("use %s(1M) to "
3021 "%s this filesystem\n"), cmdname, cmdname);
3022 return (1);
3023 }
3024
3025 if (strcmp(mountpoint, "none") == 0) {
3026 if (!explicit)
3027 return (0);
3028
3029 (void) fprintf(stderr, gettext("cannot %s '%s': no "
3030 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3031 return (1);
3032 }
3033
3034 /*
3035 * canmount explicit outcome
3036 * on no pass through
3037 * on yes pass through
3038 * off no return 0
3039 * off yes display error, return 1
3040 * noauto no return 0
3041 * noauto yes pass through
3042 */
3043 if (canmount == ZFS_CANMOUNT_OFF) {
3044 if (!explicit)
3045 return (0);
3046
3047 (void) fprintf(stderr, gettext("cannot %s '%s': "
3048 "'canmount' property is set to 'off'\n"), cmdname,
3049 zfs_get_name(zhp));
3050 return (1);
3051 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3052 return (0);
3053 }
3054
3055 /*
3056 * At this point, we have verified that the mountpoint and/or
3057 * shareopts are appropriate for auto management. If the
3058 * filesystem is already mounted or shared, return (failing
3059 * for explicit requests); otherwise mount or share the
3060 * filesystem.
3061 */
3062 switch (op) {
3063 case OP_SHARE:
3064
3065 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3066 shared_smb = zfs_is_shared_smb(zhp, NULL);
3067
3068 if (shared_nfs && shared_smb ||
3069 (shared_nfs && strcmp(shareopts, "on") == 0 &&
3070 strcmp(smbshareopts, "off") == 0) ||
3071 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3072 strcmp(shareopts, "off") == 0)) {
3073 if (!explicit)
3074 return (0);
3075
3076 (void) fprintf(stderr, gettext("cannot share "
3077 "'%s': filesystem already shared\n"),
3078 zfs_get_name(zhp));
3079 return (1);
3080 }
3081
3082 if (!zfs_is_mounted(zhp, NULL) &&
3083 zfs_mount(zhp, NULL, 0) != 0)
3084 return (1);
3085
3086 if (protocol == NULL) {
3087 if (zfs_shareall(zhp) != 0)
3088 return (1);
3089 } else if (strcmp(protocol, "nfs") == 0) {
3090 if (zfs_share_nfs(zhp))
3091 return (1);
3092 } else if (strcmp(protocol, "smb") == 0) {
3093 if (zfs_share_smb(zhp))
3094 return (1);
3095 } else {
3096 (void) fprintf(stderr, gettext("cannot share "
3097 "'%s': invalid share type '%s' "
3098 "specified\n"),
3099 zfs_get_name(zhp), protocol);
3100 return (1);
3101 }
3102
3103 break;
3104
3105 case OP_MOUNT:
3106 if (options == NULL)
3107 mnt.mnt_mntopts = "";
3108 else
3109 mnt.mnt_mntopts = (char *)options;
3110
3111 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3112 zfs_is_mounted(zhp, NULL)) {
3113 if (!explicit)
3114 return (0);
3115
3116 (void) fprintf(stderr, gettext("cannot mount "
3117 "'%s': filesystem already mounted\n"),
3118 zfs_get_name(zhp));
3119 return (1);
3120 }
3121
3122 if (zfs_mount(zhp, options, flags) != 0)
3123 return (1);
3124 break;
3125 }
3126 } else {
3127 assert(op == OP_SHARE);
3128
3129 /*
3130 * Ignore any volumes that aren't shared.
3131 */
3132 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
3133 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3134
3135 if (strcmp(shareopts, "off") == 0) {
3136 if (!explicit)
3137 return (0);
3138
3139 (void) fprintf(stderr, gettext("cannot share '%s': "
3140 "'shareiscsi' property not set\n"),
3141 zfs_get_name(zhp));
3142 (void) fprintf(stderr, gettext("set 'shareiscsi' "
3143 "property or use iscsitadm(1M) to share this "
3144 "volume\n"));
3145 return (1);
3146 }
3147
3148 if (zfs_is_shared_iscsi(zhp)) {
3149 if (!explicit)
3150 return (0);
3151
3152 (void) fprintf(stderr, gettext("cannot share "
3153 "'%s': volume already shared\n"),
3154 zfs_get_name(zhp));
3155 return (1);
3156 }
3157
3158 if (zfs_share_iscsi(zhp) != 0)
3159 return (1);
3160 }
3161
3162 return (0);
3163}
3164
3165/*
3166 * Reports progress in the form "(current/total)". Not thread-safe.
3167 */
3168static void
3169report_mount_progress(int current, int total)
3170{
3171 static int len;
3172 static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3173 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3174 static time_t last_progress_time;
3175 time_t now = time(NULL);
3176
3177 /* report 1..n instead of 0..n-1 */
3178 ++current;
3179
3180 /* display header if we're here for the first time */
3181 if (current == 1) {
3182 (void) printf(gettext("Mounting ZFS filesystems: "));
3183 len = 0;
3184 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3185 /* too soon to report again */
3186 return;
3187 }
3188
3189 last_progress_time = now;
3190
3191 /* back up to prepare for overwriting */
3192 if (len)
3193 (void) printf("%*.*s", len, len, reverse);
3194
3195 /* We put a newline at the end if this is the last one. */
3196 len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
3197 (void) fflush(stdout);
3198}
3199
3200static void
3201append_options(char *mntopts, char *newopts)
3202{
3203 int len = strlen(mntopts);
3204
3205 /* original length plus new string to append plus 1 for the comma */
3206 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3207 (void) fprintf(stderr, gettext("the opts argument for "
3208 "'%c' option is too long (more than %d chars)\n"),
3209 "-o", MNT_LINE_MAX);
3210 usage(B_FALSE);
3211 }
3212
3213 if (*mntopts)
3214 mntopts[len++] = ',';
3215
3216 (void) strcpy(&mntopts[len], newopts);
3217}
3218
3219static int
3220share_mount(int op, int argc, char **argv)
3221{
3222 int do_all = 0;
3223 boolean_t verbose = B_FALSE;
3224 int c, ret = 0;
3225 char *options = NULL;
3226 int types, flags = 0;
3227
3228 /* check options */
3229 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
3230 != -1) {
3231 switch (c) {
3232 case 'a':
3233 do_all = 1;
3234 break;
3235 case 'v':
3236 verbose = B_TRUE;
3237 break;
3238 case 'o':
3239 if (*optarg == '\0') {
3240 (void) fprintf(stderr, gettext("empty mount "
3241 "options (-o) specified\n"));
3242 usage(B_FALSE);
3243 }
3244
3245 if (options == NULL)
3246 options = safe_malloc(MNT_LINE_MAX + 1);
3247
3248 /* option validation is done later */
3249 append_options(options, optarg);
3250 break;
3251
3252 case 'O':
3253 flags |= MS_OVERLAY;
3254 break;
3255 case ':':
3256 (void) fprintf(stderr, gettext("missing argument for "
3257 "'%c' option\n"), optopt);
3258 usage(B_FALSE);
3259 break;
3260 case '?':
3261 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3262 optopt);
3263 usage(B_FALSE);
3264 }
3265 }
3266
3267 argc -= optind;
3268 argv += optind;
3269
3270 /* check number of arguments */
3271 if (do_all) {
3272 zfs_handle_t **dslist = NULL;
3273 size_t i, count = 0;
3274 char *protocol = NULL;
3275
3276 if (op == OP_MOUNT) {
3277 types = ZFS_TYPE_FILESYSTEM;
3278 } else if (argc > 0) {
3279 if (strcmp(argv[0], "nfs") == 0 ||
3280 strcmp(argv[0], "smb") == 0) {
3281 types = ZFS_TYPE_FILESYSTEM;
3282 } else if (strcmp(argv[0], "iscsi") == 0) {
3283 types = ZFS_TYPE_VOLUME;
3284 } else {
3285 (void) fprintf(stderr, gettext("share type "
3286 "must be 'nfs', 'smb' or 'iscsi'\n"));
3287 usage(B_FALSE);
3288 }
3289 protocol = argv[0];
3290 argc--;
3291 argv++;
3292 } else {
3293 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3294 }
3295
3296 if (argc != 0) {
3297 (void) fprintf(stderr, gettext("too many arguments\n"));
3298 usage(B_FALSE);
3299 }
3300
3301 get_all_datasets(types, &dslist, &count, verbose);
3302
3303 if (count == 0)
3304 return (0);
3305
3306 qsort(dslist, count, sizeof (void *), dataset_cmp);
3307
3308 for (i = 0; i < count; i++) {
3309 if (verbose)
3310 report_mount_progress(i, count);
3311
3312 if (share_mount_one(dslist[i], op, flags, protocol,
3313 B_FALSE, options) != 0)
3314 ret = 1;
3315 zfs_close(dslist[i]);
3316 }
3317
3318 free(dslist);
3319 } else if (argc == 0) {
3320 struct mnttab entry;
3321
3322 if ((op == OP_SHARE) || (options != NULL)) {
3323 (void) fprintf(stderr, gettext("missing filesystem "
3324 "argument (specify -a for all)\n"));
3325 usage(B_FALSE);
3326 }
3327
3328 /*
3329 * When mount is given no arguments, go through /etc/mnttab and
3330 * display any active ZFS mounts. We hide any snapshots, since
3331 * they are controlled automatically.
3332 */
3333 rewind(mnttab_file);
3334 while (getmntent(mnttab_file, &entry) == 0) {
3335 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3336 strchr(entry.mnt_special, '@') != NULL)
3337 continue;
3338
3339 (void) printf("%-30s %s\n", entry.mnt_special,
3340 entry.mnt_mountp);
3341 }
3342
3343 } else {
3344 zfs_handle_t *zhp;
3345
3346 types = ZFS_TYPE_FILESYSTEM;
3347 if (op == OP_SHARE)
3348 types |= ZFS_TYPE_VOLUME;
3349
3350 if (argc > 1) {
3351 (void) fprintf(stderr,
3352 gettext("too many arguments\n"));
3353 usage(B_FALSE);
3354 }
3355
3356 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
3357 ret = 1;
3358 } else {
3359 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3360 options);
3361 zfs_close(zhp);
3362 }
3363 }
3364
3365 return (ret);
3366}
3367
3368/*
3369 * zfs mount -a [nfs | iscsi]
3370 * zfs mount filesystem
3371 *
3372 * Mount all filesystems, or mount the given filesystem.
3373 */
3374static int
3375zfs_do_mount(int argc, char **argv)
3376{
3377 return (share_mount(OP_MOUNT, argc, argv));
3378}
3379
3380/*
3381 * zfs share -a [nfs | iscsi | smb]
3382 * zfs share filesystem
3383 *
3384 * Share all filesystems, or share the given filesystem.
3385 */
3386static int
3387zfs_do_share(int argc, char **argv)
3388{
3389 return (share_mount(OP_SHARE, argc, argv));
3390}
3391
3392typedef struct unshare_unmount_node {
3393 zfs_handle_t *un_zhp;
3394 char *un_mountp;
3395 uu_avl_node_t un_avlnode;
3396} unshare_unmount_node_t;
3397
3398/* ARGSUSED */
3399static int
3400unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3401{
3402 const unshare_unmount_node_t *l = larg;
3403 const unshare_unmount_node_t *r = rarg;
3404
3405 return (strcmp(l->un_mountp, r->un_mountp));
3406}
3407
3408/*
3409 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3410 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3411 * and unmount it appropriately.
3412 */
3413static int
3414unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3415{
3416 zfs_handle_t *zhp;
3417 int ret;
3418 struct stat64 statbuf;
3419 struct extmnttab entry;
3420 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3421 ino_t path_inode;
3422
3423 /*
3424 * Search for the path in /etc/mnttab. Rather than looking for the
3425 * specific path, which can be fooled by non-standard paths (i.e. ".."
3426 * or "//"), we stat() the path and search for the corresponding
3427 * (major,minor) device pair.
3428 */
3429 if (stat64(path, &statbuf) != 0) {
3430 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3431 cmdname, path, strerror(errno));
3432 return (1);
3433 }
3434 path_inode = statbuf.st_ino;
3435
3436 /*
3437 * Search for the given (major,minor) pair in the mount table.
3438 */
3439 rewind(mnttab_file);
3440 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3441 if (entry.mnt_major == major(statbuf.st_dev) &&
3442 entry.mnt_minor == minor(statbuf.st_dev))
3443 break;
3444 }
3445 if (ret != 0) {
3446 if (op == OP_SHARE) {
3447 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3448 "currently mounted\n"), cmdname, path);
3449 return (1);
3450 }
3451 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
3452 path);
3453 if ((ret = umount2(path, flags)) != 0)
3454 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3455 strerror(errno));
3456 return (ret != 0);
3457 }
3458
3459 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3460 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3461 "filesystem\n"), cmdname, path);
3462 return (1);
3463 }
3464
3465 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3466 ZFS_TYPE_FILESYSTEM)) == NULL)
3467 return (1);
3468
3469
3470 ret = 1;
3471 if (op == OP_SHARE) {
3472 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3473 char smbshare_prop[ZFS_MAXPROPLEN];
3474
3475 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3476 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3477 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3478 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3479
3480 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3481 strcmp(smbshare_prop, "off") == 0) {
3482 (void) fprintf(stderr, gettext("cannot unshare "
3483 "'%s': legacy share\n"), path);
3484 (void) fprintf(stderr, gettext("use "
3485 "unshare(1M) to unshare this filesystem\n"));
3486 } else if (!zfs_is_shared(zhp)) {
3487 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3488 "not currently shared\n"), path);
3489 } else {
3490 ret = zfs_unshareall_bypath(zhp, path);
3491 }
3492 } else {
3493 char mtpt_prop[ZFS_MAXPROPLEN];
3494
3495 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3496 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3497
3498 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3499 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3500 cmdname, path, strerror(errno));
3501 } else if (statbuf.st_ino != path_inode) {
3502 (void) fprintf(stderr, gettext("cannot "
3503 "unmount '%s': not a mountpoint\n"), path);
3504 } else if (is_manual) {
3505 ret = zfs_unmount(zhp, NULL, flags);
3506 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3507 (void) fprintf(stderr, gettext("cannot unmount "
3508 "'%s': legacy mountpoint\n"),
3509 zfs_get_name(zhp));
3510 (void) fprintf(stderr, gettext("use umount(1M) "
3511 "to unmount this filesystem\n"));
3512 } else {
3513 ret = zfs_unmountall(zhp, flags);
3514 }
3515 }
3516
3517 zfs_close(zhp);
3518
3519 return (ret != 0);
3520}
3521
3522/*
3523 * Generic callback for unsharing or unmounting a filesystem.
3524 */
3525static int
3526unshare_unmount(int op, int argc, char **argv)
3527{
3528 int do_all = 0;
3529 int flags = 0;
3530 int ret = 0;
3531 int types, c;
3532 zfs_handle_t *zhp;
3533 char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3534 char sharesmb[ZFS_MAXPROPLEN];
3535
3536 /* check options */
3537 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3538 switch (c) {
3539 case 'a':
3540 do_all = 1;
3541 break;
3542 case 'f':
3543 flags = MS_FORCE;
3544 break;
3545 case '?':
3546 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3547 optopt);
3548 usage(B_FALSE);
3549 }
3550 }
3551
3552 argc -= optind;
3553 argv += optind;
3554
3555 if (do_all) {
3556 /*
3557 * We could make use of zfs_for_each() to walk all datasets in
3558 * the system, but this would be very inefficient, especially
3559 * since we would have to linearly search /etc/mnttab for each
3560 * one. Instead, do one pass through /etc/mnttab looking for
3561 * zfs entries and call zfs_unmount() for each one.
3562 *
3563 * Things get a little tricky if the administrator has created
3564 * mountpoints beneath other ZFS filesystems. In this case, we
3565 * have to unmount the deepest filesystems first. To accomplish
3566 * this, we place all the mountpoints in an AVL tree sorted by
3567 * the special type (dataset name), and walk the result in
3568 * reverse to make sure to get any snapshots first.
3569 */
3570 struct mnttab entry;
3571 uu_avl_pool_t *pool;
3572 uu_avl_t *tree;
3573 unshare_unmount_node_t *node;
3574 uu_avl_index_t idx;
3575 uu_avl_walk_t *walk;
3576
3577 if (argc != 0) {
3578 (void) fprintf(stderr, gettext("too many arguments\n"));
3579 usage(B_FALSE);
3580 }
3581
3582 if ((pool = uu_avl_pool_create("unmount_pool",
3583 sizeof (unshare_unmount_node_t),
3584 offsetof(unshare_unmount_node_t, un_avlnode),
3585 unshare_unmount_compare,
3586 UU_DEFAULT)) == NULL) {
3587 (void) fprintf(stderr, gettext("internal error: "
3588 "out of memory\n"));
3589 exit(1);
3590 }
3591
3592 if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
3593 (void) fprintf(stderr, gettext("internal error: "
3594 "out of memory\n"));
3595 exit(1);
3596 }
3597
3598 rewind(mnttab_file);
3599 while (getmntent(mnttab_file, &entry) == 0) {
3600
3601 /* ignore non-ZFS entries */
3602 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3603 continue;
3604
3605 /* ignore snapshots */
3606 if (strchr(entry.mnt_special, '@') != NULL)
3607 continue;
3608
3609 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3610 ZFS_TYPE_FILESYSTEM)) == NULL) {
3611 ret = 1;
3612 continue;
3613 }
3614
3615 switch (op) {
3616 case OP_SHARE:
3617 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3618 nfsiscsi_mnt_prop,
3619 sizeof (nfsiscsi_mnt_prop),
3620 NULL, NULL, 0, B_FALSE) == 0);
3621 if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3622 break;
3623 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3624 nfsiscsi_mnt_prop,
3625 sizeof (nfsiscsi_mnt_prop),
3626 NULL, NULL, 0, B_FALSE) == 0);
3627 if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3628 continue;
3629 break;
3630 case OP_MOUNT:
3631 /* Ignore legacy mounts */
3632 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3633 nfsiscsi_mnt_prop,
3634 sizeof (nfsiscsi_mnt_prop),
3635 NULL, NULL, 0, B_FALSE) == 0);
3636 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3637 continue;
3638 /* Ignore canmount=noauto mounts */
3639 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3640 ZFS_CANMOUNT_NOAUTO)
3641 continue;
3642 default:
3643 break;
3644 }
3645
3646 node = safe_malloc(sizeof (unshare_unmount_node_t));
3647 node->un_zhp = zhp;
3648
3649 if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
3650 NULL) {
3651 (void) fprintf(stderr, gettext("internal error:"
3652 " out of memory\n"));
3653 exit(1);
3654 }
3655
3656 uu_avl_node_init(node, &node->un_avlnode, pool);
3657
3658 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3659 uu_avl_insert(tree, node, idx);
3660 } else {
3661 zfs_close(node->un_zhp);
3662 free(node->un_mountp);
3663 free(node);
3664 }
3665 }
3666
3667 /*
3668 * Walk the AVL tree in reverse, unmounting each filesystem and
3669 * removing it from the AVL tree in the process.
3670 */
3671 if ((walk = uu_avl_walk_start(tree,
3672 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
3673 (void) fprintf(stderr,
3674 gettext("internal error: out of memory"));
3675 exit(1);
3676 }
3677
3678 while ((node = uu_avl_walk_next(walk)) != NULL) {
3679 uu_avl_remove(tree, node);
3680
3681 switch (op) {
3682 case OP_SHARE:
3683 if (zfs_unshareall_bypath(node->un_zhp,
3684 node->un_mountp) != 0)
3685 ret = 1;
3686 break;
3687
3688 case OP_MOUNT:
3689 if (zfs_unmount(node->un_zhp,
3690 node->un_mountp, flags) != 0)
3691 ret = 1;
3692 break;
3693 }
3694
3695 zfs_close(node->un_zhp);
3696 free(node->un_mountp);
3697 free(node);
3698 }
3699
3700 uu_avl_walk_end(walk);
3701 uu_avl_destroy(tree);
3702 uu_avl_pool_destroy(pool);
3703
3704 if (op == OP_SHARE) {
3705 /*
3706 * Finally, unshare any volumes shared via iSCSI.
3707 */
3708 zfs_handle_t **dslist = NULL;
3709 size_t i, count = 0;
3710
3711 get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
3712 B_FALSE);
3713
3714 if (count != 0) {
3715 qsort(dslist, count, sizeof (void *),
3716 dataset_cmp);
3717
3718 for (i = 0; i < count; i++) {
3719 if (zfs_unshare_iscsi(dslist[i]) != 0)
3720 ret = 1;
3721 zfs_close(dslist[i]);
3722 }
3723
3724 free(dslist);
3725 }
3726 }
3727 } else {
3728 if (argc != 1) {
3729 if (argc == 0)
3730 (void) fprintf(stderr,
3731 gettext("missing filesystem argument\n"));
3732 else
3733 (void) fprintf(stderr,
3734 gettext("too many arguments\n"));
3735 usage(B_FALSE);
3736 }
3737
3738 /*
3739 * We have an argument, but it may be a full path or a ZFS
3740 * filesystem. Pass full paths off to unmount_path() (shared by
3741 * manual_unmount), otherwise open the filesystem and pass to
3742 * zfs_unmount().
3743 */
3744 if (argv[0][0] == '/')
3745 return (unshare_unmount_path(op, argv[0],
3746 flags, B_FALSE));
3747
3748 types = ZFS_TYPE_FILESYSTEM;
3749 if (op == OP_SHARE)
3750 types |= ZFS_TYPE_VOLUME;
3751
3752 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3753 return (1);
3754
3755 if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
3756 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3757 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3758 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3759 NULL, 0, B_FALSE) == 0);
3760
3761 switch (op) {
3762 case OP_SHARE:
3763 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3764 nfsiscsi_mnt_prop,
3765 sizeof (nfsiscsi_mnt_prop),
3766 NULL, NULL, 0, B_FALSE) == 0);
3767 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3768 sharesmb, sizeof (sharesmb), NULL, NULL,
3769 0, B_FALSE) == 0);
3770
3771 if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3772 strcmp(sharesmb, "off") == 0) {
3773 (void) fprintf(stderr, gettext("cannot "
3774 "unshare '%s': legacy share\n"),
3775 zfs_get_name(zhp));
3776 (void) fprintf(stderr, gettext("use "
3777 "unshare(1M) to unshare this "
3778 "filesystem\n"));
3779 ret = 1;
3780 } else if (!zfs_is_shared(zhp)) {
3781 (void) fprintf(stderr, gettext("cannot "
3782 "unshare '%s': not currently "
3783 "shared\n"), zfs_get_name(zhp));
3784 ret = 1;
3785 } else if (zfs_unshareall(zhp) != 0) {
3786 ret = 1;
3787 }
3788 break;
3789
3790 case OP_MOUNT:
3791 if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3792 (void) fprintf(stderr, gettext("cannot "
3793 "unmount '%s': legacy "
3794 "mountpoint\n"), zfs_get_name(zhp));
3795 (void) fprintf(stderr, gettext("use "
3796 "umount(1M) to unmount this "
3797 "filesystem\n"));
3798 ret = 1;
3799 } else if (!zfs_is_mounted(zhp, NULL)) {
3800 (void) fprintf(stderr, gettext("cannot "
3801 "unmount '%s': not currently "
3802 "mounted\n"),
3803 zfs_get_name(zhp));
3804 ret = 1;
3805 } else if (zfs_unmountall(zhp, flags) != 0) {
3806 ret = 1;
3807 }
3808 break;
3809 }
3810 } else {
3811 assert(op == OP_SHARE);
3812
3813 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3814 nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3815 NULL, NULL, 0, B_FALSE) == 0);
3816
3817 if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
3818 (void) fprintf(stderr, gettext("cannot unshare "
3819 "'%s': 'shareiscsi' property not set\n"),
3820 zfs_get_name(zhp));
3821 (void) fprintf(stderr, gettext("set "
3822 "'shareiscsi' property or use "
3823 "iscsitadm(1M) to share this volume\n"));
3824 ret = 1;
3825 } else if (!zfs_is_shared_iscsi(zhp)) {
3826 (void) fprintf(stderr, gettext("cannot "
3827 "unshare '%s': not currently shared\n"),
3828 zfs_get_name(zhp));
3829 ret = 1;
3830 } else if (zfs_unshare_iscsi(zhp) != 0) {
3831 ret = 1;
3832 }
3833 }
3834
3835 zfs_close(zhp);
3836 }
3837
3838 return (ret);
3839}
3840
3841/*
3842 * zfs unmount -a
3843 * zfs unmount filesystem
3844 *
3845 * Unmount all filesystems, or a specific ZFS filesystem.
3846 */
3847static int
3848zfs_do_unmount(int argc, char **argv)
3849{
3850 return (unshare_unmount(OP_MOUNT, argc, argv));
3851}
3852
3853/*
3854 * zfs unshare -a
3855 * zfs unshare filesystem
3856 *
3857 * Unshare all filesystems, or a specific ZFS filesystem.
3858 */
3859static int
3860zfs_do_unshare(int argc, char **argv)
3861{
3862 return (unshare_unmount(OP_SHARE, argc, argv));
3863}
3864
3865/*
3866 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
3867 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
3868 */
3869static int
3870manual_mount(int argc, char **argv)
3871{
3872 zfs_handle_t *zhp;
3873 char mountpoint[ZFS_MAXPROPLEN];
3874 char mntopts[MNT_LINE_MAX] = { '\0' };
3875 int ret;
3876 int c;
3877 int flags = 0;
3878 char *dataset, *path;
3879
3880 /* check options */
3881 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
3882 switch (c) {
3883 case 'o':
3884 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
3885 break;
3886 case 'O':
3887 flags |= MS_OVERLAY;
3888 break;
3889 case 'm':
3890 flags |= MS_NOMNTTAB;
3891 break;
3892 case ':':
3893 (void) fprintf(stderr, gettext("missing argument for "
3894 "'%c' option\n"), optopt);
3895 usage(B_FALSE);
3896 break;
3897 case '?':
3898 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3899 optopt);
3900 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
3901 "<path>\n"));
3902 return (2);
3903 }
3904 }
3905
3906 argc -= optind;
3907 argv += optind;
3908
3909 /* check that we only have two arguments */
3910 if (argc != 2) {
3911 if (argc == 0)
3912 (void) fprintf(stderr, gettext("missing dataset "
3913 "argument\n"));
3914 else if (argc == 1)
3915 (void) fprintf(stderr,
3916 gettext("missing mountpoint argument\n"));
3917 else
3918 (void) fprintf(stderr, gettext("too many arguments\n"));
3919 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
3920 return (2);
3921 }
3922
3923 dataset = argv[0];
3924 path = argv[1];
3925
3926 /* try to open the dataset */
3927 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
3928 return (1);
3929
3930 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3931 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
3932
3933 /* check for legacy mountpoint and complain appropriately */
3934 ret = 0;
3935 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
3936 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
3937 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
3938 (void) fprintf(stderr, gettext("mount failed: %s\n"),
3939 strerror(errno));
3940 ret = 1;
3941 }
3942 } else {
3943 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
3944 "mounted using 'mount -F zfs'\n"), dataset);
3945 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
3946 "instead.\n"), path);
3947 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
3948 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
3949 (void) fprintf(stderr, gettext("See zfs(1M) for more "
3950 "information.\n"));
3951 ret = 1;
3952 }
3953
3954 return (ret);
3955}
3956
3957/*
3958 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
3959 * unmounts of non-legacy filesystems, as this is the dominant administrative
3960 * interface.
3961 */
3962static int
3963manual_unmount(int argc, char **argv)
3964{
3965 int flags = 0;
3966 int c;
3967
3968 /* check options */
3969 while ((c = getopt(argc, argv, "f")) != -1) {
3970 switch (c) {
3971 case 'f':
3972 flags = MS_FORCE;
3973 break;
3974 case '?':
3975 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3976 optopt);
3977 (void) fprintf(stderr, gettext("usage: unmount [-f] "
3978 "<path>\n"));
3979 return (2);
3980 }
3981 }
3982
3983 argc -= optind;
3984 argv += optind;
3985
3986 /* check arguments */
3987 if (argc != 1) {
3988 if (argc == 0)
3989 (void) fprintf(stderr, gettext("missing path "
3990 "argument\n"));
3991 else
3992 (void) fprintf(stderr, gettext("too many arguments\n"));
3993 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
3994 return (2);
3995 }
3996
3997 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
3998}
3999
4000static int
4001volcheck(zpool_handle_t *zhp, void *data)
4002{
4003 boolean_t isinit = *((boolean_t *)data);
4004
4005 if (isinit)
4006 return (zpool_create_zvol_links(zhp));
4007 else
4008 return (zpool_remove_zvol_links(zhp));
4009}
4010
4011/*
4012 * Iterate over all pools in the system and either create or destroy /dev/zvol
4013 * links, depending on the value of 'isinit'.
4014 */
4015static int
4016do_volcheck(boolean_t isinit)
4017{
4018 return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
4019}
4020
4021static int
4022find_command_idx(char *command, int *idx)
4023{
4024 int i;
4025
4026 for (i = 0; i < NCOMMAND; i++) {
4027 if (command_table[i].name == NULL)
4028 continue;
4029
4030 if (strcmp(command, command_table[i].name) == 0) {
4031 *idx = i;
4032 return (0);
4033 }
4034 }
4035 return (1);
4036}
4037
4038int
4039main(int argc, char **argv)
4040{
4041 int ret;
4042 int i;
4043 char *progname;
4044 char *cmdname;
4045
4046 (void) setlocale(LC_ALL, "");
4047 (void) textdomain(TEXT_DOMAIN);
4048
4049 opterr = 0;
4050
4051 if ((g_zfs = libzfs_init()) == NULL) {
4052 (void) fprintf(stderr, gettext("internal error: failed to "
4053 "initialize ZFS library\n"));
4054 return (1);
4055 }
4056
4057 zpool_set_history_str("zfs", argc, argv, history_str);
4058 verify(zpool_stage_history(g_zfs, history_str) == 0);
4059
4060 libzfs_print_on_error(g_zfs, B_TRUE);
4061
4062 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
4063 (void) fprintf(stderr, gettext("internal error: unable to "
4064 "open %s\n"), MNTTAB);
4065 return (1);
4066 }
4067
4068 /*
4069 * This command also doubles as the /etc/fs mount and unmount program.
4070 * Determine if we should take this behavior based on argv[0].
4071 */
4072 progname = basename(argv[0]);
4073 if (strcmp(progname, "mount") == 0) {
4074 ret = manual_mount(argc, argv);
4075 } else if (strcmp(progname, "umount") == 0) {
4076 ret = manual_unmount(argc, argv);
4077 } else {
4078 /*
4079 * Make sure the user has specified some command.
4080 */
4081 if (argc < 2) {
4082 (void) fprintf(stderr, gettext("missing command\n"));
4083 usage(B_FALSE);
4084 }
4085
4086 cmdname = argv[1];
4087
4088 /*
4089 * The 'umount' command is an alias for 'unmount'
4090 */
4091 if (strcmp(cmdname, "umount") == 0)
4092 cmdname = "unmount";
4093
4094 /*
4095 * The 'recv' command is an alias for 'receive'
4096 */
4097 if (strcmp(cmdname, "recv") == 0)
4098 cmdname = "receive";
4099
4100 /*
4101 * Special case '-?'
4102 */
4103 if (strcmp(cmdname, "-?") == 0)
4104 usage(B_TRUE);
4105
4106 /*
4107 * 'volinit' and 'volfini' do not appear in the usage message,
4108 * so we have to special case them here.
4109 */
4110 if (strcmp(cmdname, "volinit") == 0)
4111 return (do_volcheck(B_TRUE));
4112 else if (strcmp(cmdname, "volfini") == 0)
4113 return (do_volcheck(B_FALSE));
4114
4115 /*
4116 * Run the appropriate command.
4117 */
4118 if (find_command_idx(cmdname, &i) == 0) {
4119 current_command = &command_table[i];
4120 ret = command_table[i].func(argc - 1, argv + 1);
4121 } else if (strchr(cmdname, '=') != NULL) {
4122 verify(find_command_idx("set", &i) == 0);
4123 current_command = &command_table[i];
4124 ret = command_table[i].func(argc, argv);
4125 } else {
4126 (void) fprintf(stderr, gettext("unrecognized "
4127 "command '%s'\n"), cmdname);
4128 usage(B_FALSE);
4129 }
4130 }
4131
4132 (void) fclose(mnttab_file);
4133
4134 libzfs_fini(g_zfs);
4135
4136 /*
4137 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4138 * for the purposes of running ::findleaks.
4139 */
4140 if (getenv("ZFS_ABORT") != NULL) {
4141 (void) printf("dumping core by request\n");
4142 abort();
4143 }
4144
4145 return (ret);
4146}