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.
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.
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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
33 #include <libnvpair.h>
42 #include <sys/mkdev.h>
43 #include <sys/mntent.h>
44 #include <sys/mnttab.h>
45 #include <sys/mount.h>
55 libzfs_handle_t
*g_zfs
;
57 static FILE *mnttab_file
;
58 static char history_str
[HIS_MAX_RECORD_LEN
];
60 static int zfs_do_clone(int argc
, char **argv
);
61 static int zfs_do_create(int argc
, char **argv
);
62 static int zfs_do_destroy(int argc
, char **argv
);
63 static int zfs_do_get(int argc
, char **argv
);
64 static int zfs_do_inherit(int argc
, char **argv
);
65 static int zfs_do_list(int argc
, char **argv
);
66 static int zfs_do_mount(int argc
, char **argv
);
67 static int zfs_do_rename(int argc
, char **argv
);
68 static int zfs_do_rollback(int argc
, char **argv
);
69 static int zfs_do_set(int argc
, char **argv
);
70 static int zfs_do_upgrade(int argc
, char **argv
);
71 static int zfs_do_snapshot(int argc
, char **argv
);
72 static int zfs_do_unmount(int argc
, char **argv
);
73 static int zfs_do_share(int argc
, char **argv
);
74 static int zfs_do_unshare(int argc
, char **argv
);
75 static int zfs_do_send(int argc
, char **argv
);
76 static int zfs_do_receive(int argc
, char **argv
);
77 static int zfs_do_promote(int argc
, char **argv
);
78 static int zfs_do_allow(int argc
, char **argv
);
79 static int zfs_do_unallow(int argc
, char **argv
);
82 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
87 _umem_debug_init(void)
89 return ("default,verbose"); /* $UMEM_DEBUG setting */
93 _umem_logging_init(void)
95 return ("fail,contents"); /* $UMEM_LOGGING setting */
122 typedef struct zfs_command
{
124 int (*func
)(int argc
, char **argv
);
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.
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.
137 static zfs_command_t command_table
[] = {
138 { "create", zfs_do_create
, HELP_CREATE
},
139 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
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
},
147 { "list", zfs_do_list
, HELP_LIST
},
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
},
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
},
159 { "send", zfs_do_send
, HELP_SEND
},
160 { "receive", zfs_do_receive
, HELP_RECEIVE
},
162 { "allow", zfs_do_allow
, HELP_ALLOW
},
164 { "unallow", zfs_do_unallow
, HELP_UNALLOW
},
167 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
169 zfs_command_t
*current_command
;
172 get_usage(zfs_help_t idx
)
176 return (gettext("\tclone [-p] [-o property=value] ... "
177 "<snapshot> <filesystem|volume>\n"));
179 return (gettext("\tcreate [-p] [-o property=value] ... "
181 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
182 "-V <size> <volume>\n"));
184 return (gettext("\tdestroy [-rRf] "
185 "<filesystem|volume|snapshot>\n"));
187 return (gettext("\tget [-rHp] [-o field[,...]] "
188 "[-s source[,...]]\n"
189 "\t <\"all\" | property[,...]> "
190 "[filesystem|volume|snapshot] ...\n"));
192 return (gettext("\tinherit [-r] <property> "
193 "<filesystem|volume|snapshot> ...\n"));
195 return (gettext("\tupgrade [-v]\n"
196 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
198 return (gettext("\tlist [-rH] [-o property[,...]] "
199 "[-t type[,...]] [-s property] ...\n"
200 "\t [-S property] ... "
201 "[filesystem|volume|snapshot] ...\n"));
203 return (gettext("\tmount\n"
204 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
206 return (gettext("\tpromote <clone-filesystem>\n"));
208 return (gettext("\treceive [-vnF] <filesystem|volume|"
210 "\treceive [-vnF] -d <filesystem>\n"));
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>"));
217 return (gettext("\trollback [-rRf] <snapshot>\n"));
219 return (gettext("\tsend [-R] [-[iI] snapshot] <snapshot>\n"));
221 return (gettext("\tset <property=value> "
222 "<filesystem|volume|snapshot> ...\n"));
224 return (gettext("\tshare <-a | filesystem>\n"));
226 return (gettext("\tsnapshot [-r] [-o property=value] ... "
227 "<filesystem@snapname|volume@snapname>\n"));
229 return (gettext("\tunmount [-f] "
230 "<-a | filesystem|mountpoint>\n"));
232 return (gettext("\tunshare [-f] "
233 "<-a | filesystem|mountpoint>\n"));
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"));
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"));
260 * Utility function to guarantee malloc() success.
263 safe_malloc(size_t size
)
267 if ((data
= calloc(1, size
)) == NULL
) {
268 (void) fprintf(stderr
, "internal error: out of memory\n");
276 * Callback routine that will print out information for each of
280 usage_prop_cb(int prop
, void *cb
)
284 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
286 if (zfs_prop_readonly(prop
))
287 (void) fprintf(fp
, " NO ");
289 (void) fprintf(fp
, "YES ");
291 if (zfs_prop_inheritable(prop
))
292 (void) fprintf(fp
, " YES ");
294 (void) fprintf(fp
, " NO ");
296 if (zfs_prop_values(prop
) == NULL
)
297 (void) fprintf(fp
, "-\n");
299 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
305 * Display usage message. If we're inside a command, display only the usage for
306 * that command. Otherwise, iterate over the entire command table and display
307 * a complete usage message.
310 usage(boolean_t requested
)
313 boolean_t show_properties
= B_FALSE
;
314 boolean_t show_permissions
= B_FALSE
;
315 FILE *fp
= requested
? stdout
: stderr
;
317 if (current_command
== NULL
) {
319 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
321 gettext("where 'command' is one of the following:\n\n"));
323 for (i
= 0; i
< NCOMMAND
; i
++) {
324 if (command_table
[i
].name
== NULL
)
325 (void) fprintf(fp
, "\n");
327 (void) fprintf(fp
, "%s",
328 get_usage(command_table
[i
].usage
));
331 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
332 "pool/[dataset/]*dataset[@name]\n"));
334 (void) fprintf(fp
, gettext("usage:\n"));
335 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
338 if (current_command
!= NULL
&&
339 (strcmp(current_command
->name
, "set") == 0 ||
340 strcmp(current_command
->name
, "get") == 0 ||
341 strcmp(current_command
->name
, "inherit") == 0 ||
342 strcmp(current_command
->name
, "list") == 0))
343 show_properties
= B_TRUE
;
345 if (current_command
!= NULL
&&
346 (strcmp(current_command
->name
, "allow") == 0 ||
347 strcmp(current_command
->name
, "unallow") == 0))
348 show_permissions
= B_TRUE
;
350 if (show_properties
) {
353 gettext("\nThe following properties are supported:\n"));
355 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
356 "PROPERTY", "EDIT", "INHERIT", "VALUES");
358 /* Iterate over all properties */
359 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
362 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
363 "with standard units such as K, M, G, etc.\n"));
364 (void) fprintf(fp
, gettext("\nUser-defined properties can "
365 "be specified by using a name containing a colon (:).\n"));
367 } else if (show_permissions
) {
369 gettext("\nThe following permissions are supported:\n"));
371 zfs_deleg_permissions();
374 gettext("\nFor the property list, run: %s\n"),
377 gettext("\nFor the delegated permission list, run: %s\n"),
378 "zfs allow|unallow");
382 * See comments at end of main().
384 if (getenv("ZFS_ABORT") != NULL
) {
385 (void) printf("dumping core by request\n");
389 exit(requested
? 0 : 2);
393 parseprop(nvlist_t
*props
)
395 char *propname
= optarg
;
396 char *propval
, *strval
;
398 if ((propval
= strchr(propname
, '=')) == NULL
) {
399 (void) fprintf(stderr
, gettext("missing "
400 "'=' for -o option\n"));
405 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
406 (void) fprintf(stderr
, gettext("property '%s' "
407 "specified multiple times\n"), propname
);
410 if (nvlist_add_string(props
, propname
, propval
) != 0) {
411 (void) fprintf(stderr
, gettext("internal "
412 "error: out of memory\n"));
419 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
421 * Given an existing dataset, create a writable copy whose initial contents
422 * are the same as the source. The newly created dataset maintains a
423 * dependency on the original; the original cannot be destroyed so long as
426 * The '-p' flag creates all the non-existing ancestors of the target first.
429 zfs_do_clone(int argc
, char **argv
)
431 zfs_handle_t
*zhp
= NULL
;
432 boolean_t parents
= B_FALSE
;
437 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
438 (void) fprintf(stderr
, gettext("internal error: "
444 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
447 if (parseprop(props
))
454 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
463 /* check number of arguments */
465 (void) fprintf(stderr
, gettext("missing source dataset "
470 (void) fprintf(stderr
, gettext("missing target dataset "
475 (void) fprintf(stderr
, gettext("too many arguments\n"));
479 /* open the source dataset */
480 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
483 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
486 * Now create the ancestors of the target dataset. If the
487 * target already exists and '-p' option was used we should not
490 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
493 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
498 ret
= zfs_clone(zhp
, argv
[1], props
);
500 /* create the mountpoint if necessary */
504 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
506 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
507 ret
= zfs_share(clone
);
526 * zfs create [-p] [-o prop=value] ... fs
527 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
529 * Create a new dataset. This command can be used to create filesystems
530 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
531 * For volumes, the user must specify a size to be used.
533 * The '-s' flag applies only to volumes, and indicates that we should not try
534 * to set the reservation for this volume. By default we set a reservation
535 * equal to the size for any volume. For pools with SPA_VERSION >=
536 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
538 * The '-p' flag creates all the non-existing ancestors of the target first.
541 zfs_do_create(int argc
, char **argv
)
543 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
544 zfs_handle_t
*zhp
= NULL
;
547 boolean_t noreserve
= B_FALSE
;
548 boolean_t bflag
= B_FALSE
;
549 boolean_t parents
= B_FALSE
;
555 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
556 (void) fprintf(stderr
, gettext("internal error: "
562 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
565 type
= ZFS_TYPE_VOLUME
;
566 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
567 (void) fprintf(stderr
, gettext("bad volume "
568 "size '%s': %s\n"), optarg
,
569 libzfs_error_description(g_zfs
));
573 if (nvlist_add_uint64(props
,
574 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
576 (void) fprintf(stderr
, gettext("internal "
577 "error: out of memory\n"));
587 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
588 (void) fprintf(stderr
, gettext("bad volume "
589 "block size '%s': %s\n"), optarg
,
590 libzfs_error_description(g_zfs
));
594 if (nvlist_add_uint64(props
,
595 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
597 (void) fprintf(stderr
, gettext("internal "
598 "error: out of memory\n"));
603 if (parseprop(props
))
610 (void) fprintf(stderr
, gettext("missing size "
615 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
621 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
622 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
623 "used when creating a volume\n"));
630 /* check number of arguments */
632 (void) fprintf(stderr
, gettext("missing %s argument\n"),
633 zfs_type_to_name(type
));
637 (void) fprintf(stderr
, gettext("too many arguments\n"));
641 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
642 zpool_handle_t
*zpool_handle
;
643 uint64_t spa_version
;
645 zfs_prop_t resv_prop
;
648 if (p
= strchr(argv
[0], '/'))
650 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
653 if (zpool_handle
== NULL
)
655 spa_version
= zpool_get_prop_int(zpool_handle
,
656 ZPOOL_PROP_VERSION
, NULL
);
657 zpool_close(zpool_handle
);
658 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
659 resv_prop
= ZFS_PROP_REFRESERVATION
;
661 resv_prop
= ZFS_PROP_RESERVATION
;
663 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
665 if (nvlist_add_uint64(props
,
666 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
667 (void) fprintf(stderr
, gettext("internal "
668 "error: out of memory\n"));
675 if (parents
&& zfs_name_valid(argv
[0], type
)) {
677 * Now create the ancestors of target dataset. If the target
678 * already exists and '-p' option was used we should not
681 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
685 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
690 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
693 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
696 * if the user doesn't want the dataset automatically mounted,
697 * then skip the mount/share step
700 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
703 * Mount and/or share the new filesystem as appropriate. We provide a
704 * verbose error message to let the user know that their filesystem was
705 * in fact created, even if we failed to mount or share it.
708 if (canmount
== ZFS_CANMOUNT_ON
) {
709 if (zfs_mount(zhp
, NULL
, 0) != 0) {
710 (void) fprintf(stderr
, gettext("filesystem "
711 "successfully created, but not mounted\n"));
713 } else if (zfs_share(zhp
) != 0) {
714 (void) fprintf(stderr
, gettext("filesystem "
715 "successfully created, but not shared\n"));
732 * zfs destroy [-rf] <fs, snap, vol>
734 * -r Recursively destroy all children
735 * -R Recursively destroy all dependents, including clones
736 * -f Force unmounting of any dependents
738 * Destroys the given dataset. By default, it will unmount any filesystems,
739 * and refuse to destroy a dataset that has any dependents. A dependent can
740 * either be a child, or a clone of a child.
742 typedef struct destroy_cbdata
{
749 boolean_t cb_closezhp
;
750 zfs_handle_t
*cb_target
;
755 * Check for any dependents based on the '-r' or '-R' flags.
758 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
760 destroy_cbdata_t
*cbp
= data
;
761 const char *tname
= zfs_get_name(cbp
->cb_target
);
762 const char *name
= zfs_get_name(zhp
);
764 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
765 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
767 * This is a direct descendant, not a clone somewhere else in
774 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
775 "%s has children\n"),
776 zfs_get_name(cbp
->cb_target
),
777 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
778 (void) fprintf(stderr
, gettext("use '-r' to destroy "
779 "the following datasets:\n"));
780 cbp
->cb_first
= B_FALSE
;
784 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
787 * This is a clone. We only want to report this if the '-r'
788 * wasn't specified, or the target is a snapshot.
790 if (!cbp
->cb_recurse
&&
791 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
795 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
796 "%s has dependent clones\n"),
797 zfs_get_name(cbp
->cb_target
),
798 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
799 (void) fprintf(stderr
, gettext("use '-R' to destroy "
800 "the following datasets:\n"));
801 cbp
->cb_first
= B_FALSE
;
805 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
814 destroy_callback(zfs_handle_t
*zhp
, void *data
)
816 destroy_cbdata_t
*cbp
= data
;
819 * Ignore pools (which we've already flagged as an error before getting
822 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
823 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
829 * Bail out on the first error.
831 if (zfs_unmount(zhp
, NULL
, cbp
->cb_force
? MS_FORCE
: 0) != 0 ||
832 zfs_destroy(zhp
) != 0) {
842 destroy_snap_clones(zfs_handle_t
*zhp
, void *arg
)
844 destroy_cbdata_t
*cbp
= arg
;
845 char thissnap
[MAXPATHLEN
];
847 boolean_t closezhp
= cbp
->cb_closezhp
;
850 (void) snprintf(thissnap
, sizeof (thissnap
),
851 "%s@%s", zfs_get_name(zhp
), cbp
->cb_snapname
);
853 libzfs_print_on_error(g_zfs
, B_FALSE
);
854 szhp
= zfs_open(g_zfs
, thissnap
, ZFS_TYPE_SNAPSHOT
);
855 libzfs_print_on_error(g_zfs
, B_TRUE
);
858 * Destroy any clones of this snapshot
860 if (zfs_iter_dependents(szhp
, B_FALSE
, destroy_callback
,
870 cbp
->cb_closezhp
= B_TRUE
;
871 rv
= zfs_iter_filesystems(zhp
, destroy_snap_clones
, arg
);
878 zfs_do_destroy(int argc
, char **argv
)
880 destroy_cbdata_t cb
= { 0 };
886 while ((c
= getopt(argc
, argv
, "frR")) != -1) {
900 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
909 /* check number of arguments */
911 (void) fprintf(stderr
, gettext("missing path argument\n"));
915 (void) fprintf(stderr
, gettext("too many arguments\n"));
920 * If we are doing recursive destroy of a snapshot, then the
921 * named snapshot may not exist. Go straight to libzfs.
923 if (cb
.cb_recurse
&& (cp
= strchr(argv
[0], '@'))) {
927 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
932 if (cb
.cb_doclones
) {
934 if (destroy_snap_clones(zhp
, &cb
) != 0) {
940 ret
= zfs_destroy_snaps(zhp
, cp
);
943 (void) fprintf(stderr
,
944 gettext("no snapshots destroyed\n"));
950 /* Open the given dataset */
951 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
957 * Perform an explicit check for pools before going any further.
959 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
960 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
961 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
962 "operation does not apply to pools\n"),
964 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
965 "%s' to destroy all datasets in the pool\n"),
967 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
968 "to destroy the pool itself\n"), zfs_get_name(zhp
));
974 * Check for any dependents and/or clones.
976 cb
.cb_first
= B_TRUE
;
977 if (!cb
.cb_doclones
&&
978 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
985 zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
, &cb
) != 0) {
991 * Do the real thing. The callback will close the handle regardless of
992 * whether it succeeds or not.
995 if (destroy_callback(zhp
, &cb
) != 0)
1003 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
1004 * < all | property[,property]... > < fs | snap | vol > ...
1006 * -r recurse over any child datasets
1007 * -H scripted mode. Headers are stripped, and fields are separated
1008 * by tabs instead of spaces.
1009 * -o Set of fields to display. One of "name,property,value,source".
1010 * Default is all four.
1011 * -s Set of sources to allow. One of
1012 * "local,default,inherited,temporary,none". Default is all
1014 * -p Display values in parsable (literal) format.
1016 * Prints properties for the given datasets. The user can control which
1017 * columns to display as well as which property types to allow.
1021 * Invoked to display the properties for a single dataset.
1024 get_callback(zfs_handle_t
*zhp
, void *data
)
1026 char buf
[ZFS_MAXPROPLEN
];
1027 zprop_source_t sourcetype
;
1028 char source
[ZFS_MAXNAMELEN
];
1029 zprop_get_cbdata_t
*cbp
= data
;
1030 nvlist_t
*userprop
= zfs_get_user_props(zhp
);
1031 zprop_list_t
*pl
= cbp
->cb_proplist
;
1036 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1038 * Skip the special fake placeholder. This will also skip over
1039 * the name property when 'all' is specified.
1041 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1042 pl
== cbp
->cb_proplist
)
1045 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1046 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1047 sizeof (buf
), &sourcetype
, source
,
1049 cbp
->cb_literal
) != 0) {
1052 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1053 ZFS_TYPE_DATASET
)) {
1054 (void) fprintf(stderr
,
1055 gettext("No such property '%s'\n"),
1056 zfs_prop_to_name(pl
->pl_prop
));
1059 sourcetype
= ZPROP_SRC_NONE
;
1060 (void) strlcpy(buf
, "-", sizeof (buf
));
1063 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1064 zfs_prop_to_name(pl
->pl_prop
),
1065 buf
, sourcetype
, source
);
1067 if (nvlist_lookup_nvlist(userprop
,
1068 pl
->pl_user_prop
, &propval
) != 0) {
1071 sourcetype
= ZPROP_SRC_NONE
;
1074 verify(nvlist_lookup_string(propval
,
1075 ZPROP_VALUE
, &strval
) == 0);
1076 verify(nvlist_lookup_string(propval
,
1077 ZPROP_SOURCE
, &sourceval
) == 0);
1079 if (strcmp(sourceval
,
1080 zfs_get_name(zhp
)) == 0) {
1081 sourcetype
= ZPROP_SRC_LOCAL
;
1083 sourcetype
= ZPROP_SRC_INHERITED
;
1084 (void) strlcpy(source
,
1085 sourceval
, sizeof (source
));
1089 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1090 pl
->pl_user_prop
, strval
, sourcetype
,
1099 zfs_do_get(int argc
, char **argv
)
1101 zprop_get_cbdata_t cb
= { 0 };
1102 int i
, c
, flags
= 0;
1103 char *value
, *fields
;
1105 zprop_list_t fake_name
= { 0 };
1108 * Set up default columns and sources.
1110 cb
.cb_sources
= ZPROP_SRC_ALL
;
1111 cb
.cb_columns
[0] = GET_COL_NAME
;
1112 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1113 cb
.cb_columns
[2] = GET_COL_VALUE
;
1114 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1115 cb
.cb_type
= ZFS_TYPE_DATASET
;
1118 while ((c
= getopt(argc
, argv
, ":o:s:rHp")) != -1) {
1121 cb
.cb_literal
= B_TRUE
;
1124 flags
|= ZFS_ITER_RECURSE
;
1127 cb
.cb_scripted
= B_TRUE
;
1130 (void) fprintf(stderr
, gettext("missing argument for "
1131 "'%c' option\n"), optopt
);
1136 * Process the set of columns to display. We zero out
1137 * the structure to give us a blank slate.
1139 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1141 while (*optarg
!= '\0') {
1142 static char *col_subopts
[] =
1143 { "name", "property", "value", "source",
1147 (void) fprintf(stderr
, gettext("too "
1148 "many fields given to -o "
1153 switch (getsubopt(&optarg
, col_subopts
,
1156 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1159 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1162 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1165 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1168 (void) fprintf(stderr
,
1169 gettext("invalid column name "
1178 while (*optarg
!= '\0') {
1179 static char *source_subopts
[] = {
1180 "local", "default", "inherited",
1181 "temporary", "none", NULL
};
1183 switch (getsubopt(&optarg
, source_subopts
,
1186 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1189 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1192 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1195 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1198 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1201 (void) fprintf(stderr
,
1202 gettext("invalid source "
1210 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1220 (void) fprintf(stderr
, gettext("missing property "
1227 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1235 * As part of zfs_expand_proplist(), we keep track of the maximum column
1236 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1237 * need to know the maximum name length. However, the user likely did
1238 * not specify 'name' as one of the properties to fetch, so we need to
1239 * make sure we always include at least this property for
1240 * print_get_headers() to work properly.
1242 if (cb
.cb_proplist
!= NULL
) {
1243 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1244 fake_name
.pl_width
= strlen(gettext("NAME"));
1245 fake_name
.pl_next
= cb
.cb_proplist
;
1246 cb
.cb_proplist
= &fake_name
;
1249 cb
.cb_first
= B_TRUE
;
1251 /* run for each object */
1252 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
, NULL
,
1253 &cb
.cb_proplist
, get_callback
, &cb
);
1255 if (cb
.cb_proplist
== &fake_name
)
1256 zprop_free_list(fake_name
.pl_next
);
1258 zprop_free_list(cb
.cb_proplist
);
1264 * inherit [-r] <property> <fs|vol> ...
1266 * -r Recurse over all children
1268 * For each dataset specified on the command line, inherit the given property
1269 * from its parent. Inheriting a property at the pool level will cause it to
1270 * use the default value. The '-r' flag will recurse over all children, and is
1271 * useful for setting a property on a hierarchy-wide basis, regardless of any
1272 * local modifications for each dataset.
1276 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1278 char *propname
= data
;
1279 zfs_prop_t prop
= zfs_name_to_prop(propname
);
1282 * If we're doing it recursively, then ignore properties that
1283 * are not valid for this type of dataset.
1285 if (prop
!= ZPROP_INVAL
&&
1286 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1289 return (zfs_prop_inherit(zhp
, propname
) != 0);
1293 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1295 char *propname
= data
;
1297 return (zfs_prop_inherit(zhp
, propname
) != 0);
1301 zfs_do_inherit(int argc
, char **argv
)
1310 while ((c
= getopt(argc
, argv
, "r")) != -1) {
1313 flags
|= ZFS_ITER_RECURSE
;
1317 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1326 /* check number of arguments */
1328 (void) fprintf(stderr
, gettext("missing property argument\n"));
1332 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1340 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1341 if (zfs_prop_readonly(prop
)) {
1342 (void) fprintf(stderr
, gettext(
1343 "%s property is read-only\n"),
1347 if (!zfs_prop_inheritable(prop
)) {
1348 (void) fprintf(stderr
, gettext("'%s' property cannot "
1349 "be inherited\n"), propname
);
1350 if (prop
== ZFS_PROP_QUOTA
||
1351 prop
== ZFS_PROP_RESERVATION
||
1352 prop
== ZFS_PROP_REFQUOTA
||
1353 prop
== ZFS_PROP_REFRESERVATION
)
1354 (void) fprintf(stderr
, gettext("use 'zfs set "
1355 "%s=none' to clear\n"), propname
);
1358 } else if (!zfs_prop_user(propname
)) {
1359 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1364 if (flags
& ZFS_ITER_RECURSE
) {
1365 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1366 NULL
, NULL
, inherit_recurse_cb
, propname
);
1368 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1369 NULL
, NULL
, inherit_cb
, propname
);
1375 typedef struct upgrade_cbdata
{
1376 uint64_t cb_numupgraded
;
1377 uint64_t cb_numsamegraded
;
1378 uint64_t cb_numfailed
;
1379 uint64_t cb_version
;
1381 boolean_t cb_foundone
;
1382 char cb_lastfs
[ZFS_MAXNAMELEN
];
1386 same_pool(zfs_handle_t
*zhp
, const char *name
)
1388 int len1
= strcspn(name
, "/@");
1389 const char *zhname
= zfs_get_name(zhp
);
1390 int len2
= strcspn(zhname
, "/@");
1394 return (strncmp(name
, zhname
, len1
) == 0);
1398 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1400 upgrade_cbdata_t
*cb
= data
;
1401 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1403 /* list if it's old/new */
1404 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1405 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1408 str
= gettext("The following filesystems are "
1409 "formatted using a newer software version and\n"
1410 "cannot be accessed on the current system.\n\n");
1412 str
= gettext("The following filesystems are "
1413 "out of date, and can be upgraded. After being\n"
1414 "upgraded, these filesystems (and any 'zfs send' "
1415 "streams generated from\n"
1416 "subsequent snapshots) will no longer be "
1417 "accessible by older software versions.\n\n");
1420 if (!cb
->cb_foundone
) {
1422 (void) printf(gettext("VER FILESYSTEM\n"));
1423 (void) printf(gettext("--- ------------\n"));
1424 cb
->cb_foundone
= B_TRUE
;
1427 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1434 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1436 upgrade_cbdata_t
*cb
= data
;
1437 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1439 if (cb
->cb_version
>= ZPL_VERSION_FUID
) {
1442 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1445 if (spa_version
< SPA_VERSION_FUID
) {
1447 (void) printf(gettext("%s: can not be upgraded; "
1448 "the pool version needs to first be upgraded\nto "
1450 zfs_get_name(zhp
), SPA_VERSION_FUID
);
1457 if (version
< cb
->cb_version
) {
1459 (void) snprintf(verstr
, sizeof (verstr
),
1460 "%llu", cb
->cb_version
);
1461 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1463 * If they did "zfs upgrade -a", then we could
1464 * be doing ioctls to different pools. We need
1465 * to log this history once to each pool.
1467 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
1469 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1470 cb
->cb_numupgraded
++;
1473 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1474 } else if (version
> cb
->cb_version
) {
1475 /* can't downgrade */
1476 (void) printf(gettext("%s: can not be downgraded; "
1477 "it is already at version %u\n"),
1478 zfs_get_name(zhp
), version
);
1481 cb
->cb_numsamegraded
++;
1489 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1492 zfs_do_upgrade(int argc
, char **argv
)
1494 boolean_t all
= B_FALSE
;
1495 boolean_t showversions
= B_FALSE
;
1497 upgrade_cbdata_t cb
= { 0 };
1499 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1502 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1505 flags
|= ZFS_ITER_RECURSE
;
1508 showversions
= B_TRUE
;
1511 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
1512 optarg
, &cb
.cb_version
) != 0) {
1513 (void) fprintf(stderr
,
1514 gettext("invalid version %s\n"), optarg
);
1523 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1532 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
1534 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
1535 cb
.cb_version
|| argc
))
1537 if ((all
|| argc
) && (showversions
))
1543 /* Show info on available versions. */
1544 (void) printf(gettext("The following filesystem versions are "
1546 (void) printf(gettext("VER DESCRIPTION\n"));
1547 (void) printf("--- -----------------------------------------"
1548 "---------------\n");
1549 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1550 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1551 (void) printf(gettext(" 3 Case insensitive and File system "
1552 "unique identifer (FUID)\n"));
1553 (void) printf(gettext("\nFor more information on a particular "
1554 "version, including supported releases, see:\n\n"));
1555 (void) printf("http://www.opensolaris.org/os/community/zfs/"
1556 "version/zpl/N\n\n");
1557 (void) printf(gettext("Where 'N' is the version number.\n"));
1559 } else if (argc
|| all
) {
1560 /* Upgrade filesystems */
1561 if (cb
.cb_version
== 0)
1562 cb
.cb_version
= ZPL_VERSION
;
1563 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
1564 NULL
, NULL
, upgrade_set_callback
, &cb
);
1565 (void) printf(gettext("%llu filesystems upgraded\n"),
1567 if (cb
.cb_numsamegraded
) {
1568 (void) printf(gettext("%llu filesystems already at "
1570 cb
.cb_numsamegraded
);
1572 if (cb
.cb_numfailed
!= 0)
1575 /* List old-version filesytems */
1577 (void) printf(gettext("This system is currently running "
1578 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
1580 flags
|= ZFS_ITER_RECURSE
;
1581 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1582 NULL
, NULL
, upgrade_list_callback
, &cb
);
1584 found
= cb
.cb_foundone
;
1585 cb
.cb_foundone
= B_FALSE
;
1586 cb
.cb_newer
= B_TRUE
;
1588 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1589 NULL
, NULL
, upgrade_list_callback
, &cb
);
1591 if (!cb
.cb_foundone
&& !found
) {
1592 (void) printf(gettext("All filesystems are "
1593 "formatted with the current version.\n"));
1601 * list [-rH] [-o property[,property]...] [-t type[,type]...]
1602 * [-s property [-s property]...] [-S property [-S property]...]
1605 * -r Recurse over all children
1606 * -H Scripted mode; elide headers and separate columns by tabs
1607 * -o Control which fields to display.
1608 * -t Control which object types to display.
1609 * -s Specify sort columns, descending order.
1610 * -S Specify sort columns, ascending order.
1612 * When given no arguments, lists all filesystems in the system.
1613 * Otherwise, list the specified datasets, optionally recursing down them if
1614 * '-r' is specified.
1616 typedef struct list_cbdata
{
1618 boolean_t cb_scripted
;
1619 zprop_list_t
*cb_proplist
;
1623 * Given a list of columns to display, output appropriate headers for each one.
1626 print_header(zprop_list_t
*pl
)
1628 char headerbuf
[ZFS_MAXPROPLEN
];
1631 boolean_t first
= B_TRUE
;
1632 boolean_t right_justify
;
1634 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1641 right_justify
= B_FALSE
;
1642 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1643 header
= zfs_prop_column_name(pl
->pl_prop
);
1644 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1646 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
1647 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
1648 headerbuf
[i
] = '\0';
1652 if (pl
->pl_next
== NULL
&& !right_justify
)
1653 (void) printf("%s", header
);
1654 else if (right_justify
)
1655 (void) printf("%*s", pl
->pl_width
, header
);
1657 (void) printf("%-*s", pl
->pl_width
, header
);
1660 (void) printf("\n");
1664 * Given a dataset and a list of fields, print out all the properties according
1665 * to the described layout.
1668 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
1670 boolean_t first
= B_TRUE
;
1671 char property
[ZFS_MAXPROPLEN
];
1672 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
1675 boolean_t right_justify
;
1678 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1681 (void) printf("\t");
1688 right_justify
= B_FALSE
;
1689 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1690 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
1691 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
1696 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1698 if (nvlist_lookup_nvlist(userprops
,
1699 pl
->pl_user_prop
, &propval
) != 0)
1702 verify(nvlist_lookup_string(propval
,
1703 ZPROP_VALUE
, &propstr
) == 0);
1706 width
= pl
->pl_width
;
1709 * If this is being called in scripted mode, or if this is the
1710 * last column and it is left-justified, don't include a width
1713 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
1714 (void) printf("%s", propstr
);
1715 else if (right_justify
)
1716 (void) printf("%*s", width
, propstr
);
1718 (void) printf("%-*s", width
, propstr
);
1721 (void) printf("\n");
1725 * Generic callback function to list a dataset or snapshot.
1728 list_callback(zfs_handle_t
*zhp
, void *data
)
1730 list_cbdata_t
*cbp
= data
;
1732 if (cbp
->cb_first
) {
1733 if (!cbp
->cb_scripted
)
1734 print_header(cbp
->cb_proplist
);
1735 cbp
->cb_first
= B_FALSE
;
1738 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
1744 zfs_do_list(int argc
, char **argv
)
1747 boolean_t scripted
= B_FALSE
;
1748 static char default_fields
[] =
1749 "name,used,available,referenced,mountpoint";
1750 int types
= ZFS_TYPE_DATASET
;
1751 boolean_t types_specified
= B_FALSE
;
1752 char *fields
= NULL
;
1753 list_cbdata_t cb
= { 0 };
1756 zfs_sort_column_t
*sortcol
= NULL
;
1757 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
1760 while ((c
= getopt(argc
, argv
, ":o:rt:Hs:S:")) != -1) {
1766 flags
|= ZFS_ITER_RECURSE
;
1772 if (zfs_add_sort_column(&sortcol
, optarg
,
1774 (void) fprintf(stderr
,
1775 gettext("invalid property '%s'\n"), optarg
);
1780 if (zfs_add_sort_column(&sortcol
, optarg
,
1782 (void) fprintf(stderr
,
1783 gettext("invalid property '%s'\n"), optarg
);
1789 types_specified
= B_TRUE
;
1790 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1791 while (*optarg
!= '\0') {
1792 static char *type_subopts
[] = { "filesystem",
1793 "volume", "snapshot", "all", NULL
};
1795 switch (getsubopt(&optarg
, type_subopts
,
1798 types
|= ZFS_TYPE_FILESYSTEM
;
1801 types
|= ZFS_TYPE_VOLUME
;
1804 types
|= ZFS_TYPE_SNAPSHOT
;
1807 types
= ZFS_TYPE_DATASET
;
1811 (void) fprintf(stderr
,
1812 gettext("invalid type '%s'\n"),
1819 (void) fprintf(stderr
, gettext("missing argument for "
1820 "'%c' option\n"), optopt
);
1824 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1834 fields
= default_fields
;
1837 * If "-o space" and no types were specified, don't display snapshots.
1839 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
1840 types
&= ~ZFS_TYPE_SNAPSHOT
;
1843 * If the user specifies '-o all', the zprop_get_list() doesn't
1844 * normally include the name of the dataset. For 'zfs list', we always
1845 * want this property to be first.
1847 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1851 cb
.cb_scripted
= scripted
;
1852 cb
.cb_first
= B_TRUE
;
1854 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
1855 list_callback
, &cb
);
1857 zprop_free_list(cb
.cb_proplist
);
1858 zfs_free_sort_columns(sortcol
);
1860 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
1861 (void) printf(gettext("no datasets available\n"));
1867 * zfs rename <fs | snap | vol> <fs | snap | vol>
1868 * zfs rename -p <fs | vol> <fs | vol>
1869 * zfs rename -r <snap> <snap>
1871 * Renames the given dataset to another of the same type.
1873 * The '-p' flag creates all the non-existing ancestors of the target first.
1877 zfs_do_rename(int argc
, char **argv
)
1882 boolean_t recurse
= B_FALSE
;
1883 boolean_t parents
= B_FALSE
;
1886 while ((c
= getopt(argc
, argv
, "pr")) != -1) {
1896 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1905 /* check number of arguments */
1907 (void) fprintf(stderr
, gettext("missing source dataset "
1912 (void) fprintf(stderr
, gettext("missing target dataset "
1917 (void) fprintf(stderr
, gettext("too many arguments\n"));
1921 if (recurse
&& parents
) {
1922 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
1927 if (recurse
&& strchr(argv
[0], '@') == 0) {
1928 (void) fprintf(stderr
, gettext("source dataset for recursive "
1929 "rename must be a snapshot\n"));
1933 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
1934 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
1937 /* If we were asked and the name looks good, try to create ancestors. */
1938 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
1939 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
1944 ret
= (zfs_rename(zhp
, argv
[1], recurse
) != 0);
1953 * Promotes the given clone fs to be the parent
1957 zfs_do_promote(int argc
, char **argv
)
1963 if (argc
> 1 && argv
[1][0] == '-') {
1964 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1969 /* check number of arguments */
1971 (void) fprintf(stderr
, gettext("missing clone filesystem"
1976 (void) fprintf(stderr
, gettext("too many arguments\n"));
1980 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1984 ret
= (zfs_promote(zhp
) != 0);
1992 * zfs rollback [-rRf] <snapshot>
1994 * -r Delete any intervening snapshots before doing rollback
1995 * -R Delete any snapshots and their clones
1996 * -f ignored for backwards compatability
1998 * Given a filesystem, rollback to a specific snapshot, discarding any changes
1999 * since then and making it the active dataset. If more recent snapshots exist,
2000 * the command will complain unless the '-r' flag is given.
2002 typedef struct rollback_cbdata
{
2008 boolean_t cb_recurse
;
2009 boolean_t cb_dependent
;
2010 } rollback_cbdata_t
;
2013 * Report any snapshots more recent than the one specified. Used when '-r' is
2014 * not specified. We reuse this same callback for the snapshot dependents - if
2015 * 'cb_dependent' is set, then this is a dependent and we should report it
2016 * without checking the transaction group.
2019 rollback_check(zfs_handle_t
*zhp
, void *data
)
2021 rollback_cbdata_t
*cbp
= data
;
2023 if (cbp
->cb_doclones
) {
2028 if (!cbp
->cb_dependent
) {
2029 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
2030 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2031 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
2034 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
2035 (void) fprintf(stderr
, gettext("cannot "
2036 "rollback to '%s': more recent snapshots "
2039 (void) fprintf(stderr
, gettext("use '-r' to "
2040 "force deletion of the following "
2046 if (cbp
->cb_recurse
) {
2047 cbp
->cb_dependent
= B_TRUE
;
2048 if (zfs_iter_dependents(zhp
, B_TRUE
,
2049 rollback_check
, cbp
) != 0) {
2053 cbp
->cb_dependent
= B_FALSE
;
2055 (void) fprintf(stderr
, "%s\n",
2060 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
2061 (void) fprintf(stderr
, gettext("cannot rollback to "
2062 "'%s': clones of previous snapshots exist\n"),
2064 (void) fprintf(stderr
, gettext("use '-R' to "
2065 "force deletion of the following clones and "
2071 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
2079 zfs_do_rollback(int argc
, char **argv
)
2083 boolean_t force
= B_FALSE
;
2084 rollback_cbdata_t cb
= { 0 };
2085 zfs_handle_t
*zhp
, *snap
;
2086 char parentname
[ZFS_MAXNAMELEN
];
2090 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
2103 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2112 /* check number of arguments */
2114 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2118 (void) fprintf(stderr
, gettext("too many arguments\n"));
2122 /* open the snapshot */
2123 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
2126 /* open the parent dataset */
2127 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
2128 verify((delim
= strrchr(parentname
, '@')) != NULL
);
2130 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
2136 * Check for more recent snapshots and/or clones based on the presence
2139 cb
.cb_target
= argv
[0];
2140 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
2141 cb
.cb_first
= B_TRUE
;
2143 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
2146 if ((ret
= cb
.cb_error
) != 0)
2150 * Rollback parent to the given snapshot.
2152 ret
= zfs_rollback(zhp
, snap
, force
);
2165 * zfs set property=value { fs | snap | vol } ...
2167 * Sets the given property for all datasets specified on the command line.
2169 typedef struct set_cbdata
{
2175 set_callback(zfs_handle_t
*zhp
, void *data
)
2177 set_cbdata_t
*cbp
= data
;
2179 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
2180 switch (libzfs_errno(g_zfs
)) {
2181 case EZFS_MOUNTFAILED
:
2182 (void) fprintf(stderr
, gettext("property may be set "
2183 "but unable to remount filesystem\n"));
2185 case EZFS_SHARENFSFAILED
:
2186 (void) fprintf(stderr
, gettext("property may be set "
2187 "but unable to reshare filesystem\n"));
2196 zfs_do_set(int argc
, char **argv
)
2201 /* check for options */
2202 if (argc
> 1 && argv
[1][0] == '-') {
2203 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2208 /* check number of arguments */
2210 (void) fprintf(stderr
, gettext("missing property=value "
2215 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2219 /* validate property=value argument */
2220 cb
.cb_propname
= argv
[1];
2221 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
2222 (cb
.cb_value
[1] == '\0')) {
2223 (void) fprintf(stderr
, gettext("missing value in "
2224 "property=value argument\n"));
2228 *cb
.cb_value
= '\0';
2231 if (*cb
.cb_propname
== '\0') {
2232 (void) fprintf(stderr
,
2233 gettext("missing property in property=value argument\n"));
2237 ret
= zfs_for_each(argc
- 2, argv
+ 2, NULL
,
2238 ZFS_TYPE_DATASET
, NULL
, NULL
, set_callback
, &cb
);
2244 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2246 * Creates a snapshot with the given name. While functionally equivalent to
2247 * 'zfs create', it is a separate command to differentiate intent.
2250 zfs_do_snapshot(int argc
, char **argv
)
2252 boolean_t recursive
= B_FALSE
;
2257 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
2258 (void) fprintf(stderr
, gettext("internal error: "
2259 "out of memory\n"));
2264 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
2267 if (parseprop(props
))
2274 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2283 /* check number of arguments */
2285 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2289 (void) fprintf(stderr
, gettext("too many arguments\n"));
2293 ret
= zfs_snapshot(g_zfs
, argv
[0], recursive
, props
);
2295 if (ret
&& recursive
)
2296 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
2306 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2307 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2309 * Send a backup stream to stdout.
2312 zfs_do_send(int argc
, char **argv
)
2314 char *fromname
= NULL
;
2315 char *toname
= NULL
;
2318 boolean_t doall
= B_FALSE
;
2319 boolean_t replicate
= B_FALSE
;
2320 boolean_t fromorigin
= B_FALSE
;
2321 boolean_t verbose
= B_FALSE
;
2325 while ((c
= getopt(argc
, argv
, ":i:I:Rv")) != -1) {
2345 (void) fprintf(stderr
, gettext("missing argument for "
2346 "'%c' option\n"), optopt
);
2350 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2359 /* check number of arguments */
2361 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2365 (void) fprintf(stderr
, gettext("too many arguments\n"));
2369 if (isatty(STDOUT_FILENO
)) {
2370 (void) fprintf(stderr
,
2371 gettext("Error: Stream can not be written to a terminal.\n"
2372 "You must redirect standard output.\n"));
2376 cp
= strchr(argv
[0], '@');
2378 (void) fprintf(stderr
,
2379 gettext("argument must be a snapshot\n"));
2384 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2389 * If they specified the full path to the snapshot, chop off
2390 * everything except the short name of the snapshot, but special
2391 * case if they specify the origin.
2393 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
2394 char origin
[ZFS_MAXNAMELEN
];
2397 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
2398 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
2400 if (strcmp(origin
, fromname
) == 0) {
2402 fromorigin
= B_TRUE
;
2405 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
2406 (void) fprintf(stderr
,
2407 gettext("incremental source must be "
2408 "in same filesystem\n"));
2412 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
2413 (void) fprintf(stderr
,
2414 gettext("invalid incremental source\n"));
2420 if (replicate
&& fromname
== NULL
)
2423 err
= zfs_send(zhp
, fromname
, toname
, replicate
, doall
, fromorigin
,
2424 verbose
, STDOUT_FILENO
);
2431 * zfs receive [-dnvF] <fs@snap>
2433 * Restore a backup stream from stdin.
2436 zfs_do_receive(int argc
, char **argv
)
2441 bzero(&flags
, sizeof (recvflags_t
));
2443 while ((c
= getopt(argc
, argv
, ":dnuvF")) != -1) {
2446 flags
.isprefix
= B_TRUE
;
2449 flags
.dryrun
= B_TRUE
;
2452 flags
.nomount
= B_TRUE
;
2455 flags
.verbose
= B_TRUE
;
2458 flags
.force
= B_TRUE
;
2461 (void) fprintf(stderr
, gettext("missing argument for "
2462 "'%c' option\n"), optopt
);
2466 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2475 /* check number of arguments */
2477 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2481 (void) fprintf(stderr
, gettext("too many arguments\n"));
2485 if (isatty(STDIN_FILENO
)) {
2486 (void) fprintf(stderr
,
2487 gettext("Error: Backup stream can not be read "
2488 "from a terminal.\n"
2489 "You must redirect standard input.\n"));
2493 err
= zfs_receive(g_zfs
, argv
[0], flags
, STDIN_FILENO
, NULL
);
2498 typedef struct allow_cb
{
2500 size_t a_treeoffset
;
2504 zfs_print_perms(avl_tree_t
*tree
)
2506 zfs_perm_node_t
*permnode
;
2508 permnode
= avl_first(tree
);
2509 while (permnode
!= NULL
) {
2510 (void) printf("%s", permnode
->z_pname
);
2511 permnode
= AVL_NEXT(tree
, permnode
);
2515 (void) printf("\n");
2520 * Iterate over user/groups/everyone/... and the call perm_iter
2521 * function to print actual permission when tree has >0 nodes.
2524 zfs_iter_perms(avl_tree_t
*tree
, const char *banner
, allow_cb_t
*cb
)
2526 zfs_allow_node_t
*item
;
2529 item
= avl_first(tree
);
2531 ptree
= (void *)((char *)item
+ cb
->a_treeoffset
);
2532 if (avl_numnodes(ptree
)) {
2533 if (cb
->a_permcnt
++ == 0)
2534 (void) printf("%s\n", banner
);
2535 (void) printf("\t%s", item
->z_key
);
2537 * Avoid an extra space being printed
2538 * for "everyone" which is keyed with a null
2541 if (item
->z_key
[0] != '\0')
2543 zfs_print_perms(ptree
);
2545 item
= AVL_NEXT(tree
, item
);
2549 #define LINES "-------------------------------------------------------------\n"
2551 zfs_print_allows(char *ds
)
2553 zfs_allow_t
*curperms
, *perms
;
2555 allow_cb_t allowcb
= { 0 };
2556 char banner
[MAXPATHLEN
];
2561 if (strrchr(ds
, '@')) {
2562 (void) fprintf(stderr
, gettext("Snapshots don't have 'allow'"
2566 if ((zhp
= zfs_open(g_zfs
, ds
, ZFS_TYPE_DATASET
)) == NULL
)
2569 if (zfs_perm_get(zhp
, &perms
)) {
2570 (void) fprintf(stderr
,
2571 gettext("Failed to retrieve 'allows' on %s\n"), ds
);
2579 (void) printf("%s", LINES
);
2580 for (curperms
= perms
; curperms
; curperms
= curperms
->z_next
) {
2582 (void) snprintf(banner
, sizeof (banner
),
2583 gettext("Permission sets on (%s)"), curperms
->z_setpoint
);
2584 allowcb
.a_treeoffset
=
2585 offsetof(zfs_allow_node_t
, z_localdescend
);
2586 allowcb
.a_permcnt
= 0;
2587 zfs_iter_perms(&curperms
->z_sets
, banner
, &allowcb
);
2589 (void) snprintf(banner
, sizeof (banner
),
2590 gettext("Create time permissions on (%s)"),
2591 curperms
->z_setpoint
);
2592 allowcb
.a_treeoffset
=
2593 offsetof(zfs_allow_node_t
, z_localdescend
);
2594 allowcb
.a_permcnt
= 0;
2595 zfs_iter_perms(&curperms
->z_crperms
, banner
, &allowcb
);
2598 (void) snprintf(banner
, sizeof (banner
),
2599 gettext("Local permissions on (%s)"), curperms
->z_setpoint
);
2600 allowcb
.a_treeoffset
= offsetof(zfs_allow_node_t
, z_local
);
2601 allowcb
.a_permcnt
= 0;
2602 zfs_iter_perms(&curperms
->z_user
, banner
, &allowcb
);
2603 zfs_iter_perms(&curperms
->z_group
, banner
, &allowcb
);
2604 zfs_iter_perms(&curperms
->z_everyone
, banner
, &allowcb
);
2606 (void) snprintf(banner
, sizeof (banner
),
2607 gettext("Descendent permissions on (%s)"),
2608 curperms
->z_setpoint
);
2609 allowcb
.a_treeoffset
= offsetof(zfs_allow_node_t
, z_descend
);
2610 allowcb
.a_permcnt
= 0;
2611 zfs_iter_perms(&curperms
->z_user
, banner
, &allowcb
);
2612 zfs_iter_perms(&curperms
->z_group
, banner
, &allowcb
);
2613 zfs_iter_perms(&curperms
->z_everyone
, banner
, &allowcb
);
2615 (void) snprintf(banner
, sizeof (banner
),
2616 gettext("Local+Descendent permissions on (%s)"),
2617 curperms
->z_setpoint
);
2618 allowcb
.a_treeoffset
=
2619 offsetof(zfs_allow_node_t
, z_localdescend
);
2620 allowcb
.a_permcnt
= 0;
2621 zfs_iter_perms(&curperms
->z_user
, banner
, &allowcb
);
2622 zfs_iter_perms(&curperms
->z_group
, banner
, &allowcb
);
2623 zfs_iter_perms(&curperms
->z_everyone
, banner
, &allowcb
);
2625 (void) printf("%s", LINES
);
2627 zfs_free_allows(perms
);
2631 #define ALLOWOPTIONS "ldcsu:g:e"
2632 #define UNALLOWOPTIONS "ldcsu:g:er"
2635 * Validate options, and build necessary datastructure to display/remove/add
2637 * Returns 0 - If permissions should be added/removed
2638 * Returns 1 - If permissions should be displayed.
2639 * Returns -1 - on failure
2642 parse_allow_args(int *argc
, char **argv
[], boolean_t unallow
,
2643 char **ds
, int *recurse
, nvlist_t
**zperms
)
2646 char *options
= unallow
? UNALLOWOPTIONS
: ALLOWOPTIONS
;
2647 zfs_deleg_inherit_t deleg_type
= ZFS_DELEG_NONE
;
2648 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
2653 while ((c
= getopt(*argc
, *argv
, options
)) != -1) {
2656 if (who_type
== ZFS_DELEG_CREATE
||
2657 who_type
== ZFS_DELEG_NAMED_SET
)
2660 deleg_type
|= ZFS_DELEG_PERM_LOCAL
;
2663 if (who_type
== ZFS_DELEG_CREATE
||
2664 who_type
== ZFS_DELEG_NAMED_SET
)
2667 deleg_type
|= ZFS_DELEG_PERM_DESCENDENT
;
2673 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2677 who_type
= ZFS_DELEG_CREATE
;
2680 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2684 who_type
= ZFS_DELEG_NAMED_SET
;
2687 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2689 who_type
= ZFS_DELEG_USER
;
2693 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2695 who_type
= ZFS_DELEG_GROUP
;
2699 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2701 who_type
= ZFS_DELEG_EVERYONE
;
2709 if (deleg_type
== 0)
2710 deleg_type
= ZFS_DELEG_PERM_LOCALDESCENDENT
;
2715 if (unallow
== B_FALSE
&& *argc
== 1) {
2717 * Only print permissions if no options were processed
2726 * initialize variables for zfs_build_perms based on number
2728 * 3 arguments ==> zfs [un]allow joe perm,perm,perm <dataset> or
2729 * zfs [un]allow -s @set1 perm,perm <dataset>
2730 * 2 arguments ==> zfs [un]allow -c perm,perm <dataset> or
2731 * zfs [un]allow -u|-g <name> perm <dataset> or
2732 * zfs [un]allow -e perm,perm <dataset>
2733 * zfs unallow joe <dataset>
2734 * zfs unallow -s @set1 <dataset>
2735 * 1 argument ==> zfs [un]allow -e <dataset> or
2736 * zfs [un]allow -c <dataset>
2746 * advance argc/argv for do_allow cases.
2747 * for do_allow case make sure who have a know who type
2748 * and its not a permission set.
2750 if (unallow
== B_TRUE
) {
2753 } else if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
&&
2754 who_type
!= ZFS_DELEG_NAMED_SET
)
2759 if (unallow
== B_TRUE
&& (who_type
== ZFS_DELEG_EVERYONE
||
2760 who_type
== ZFS_DELEG_CREATE
|| who
!= NULL
)) {
2764 if (unallow
== B_FALSE
&&
2765 (who_type
== ZFS_DELEG_WHO_UNKNOWN
||
2766 who_type
== ZFS_DELEG_NAMED_SET
))
2768 else if (who_type
== ZFS_DELEG_WHO_UNKNOWN
||
2769 who_type
== ZFS_DELEG_NAMED_SET
)
2771 else if (who_type
!= ZFS_DELEG_NAMED_SET
)
2775 if (unallow
== B_TRUE
) {
2782 if (unallow
== B_FALSE
)
2784 if (who
== NULL
&& who_type
!= ZFS_DELEG_CREATE
&&
2785 who_type
!= ZFS_DELEG_EVERYONE
)
2794 if (strrchr(*ds
, '@')) {
2795 (void) fprintf(stderr
,
2796 gettext("Can't set or remove 'allow' permissions "
2797 "on snapshots.\n"));
2801 if ((zhp
= zfs_open(g_zfs
, *ds
, ZFS_TYPE_DATASET
)) == NULL
)
2804 if ((zfs_build_perms(zhp
, who
, perms
,
2805 who_type
, deleg_type
, zperms
)) != 0) {
2814 zfs_do_allow(int argc
, char **argv
)
2817 nvlist_t
*zperms
= NULL
;
2822 if ((ret
= parse_allow_args(&argc
, &argv
, B_FALSE
, &ds
,
2823 &unused
, &zperms
)) == -1)
2827 return (zfs_print_allows(argv
[0]));
2829 if ((zhp
= zfs_open(g_zfs
, ds
, ZFS_TYPE_DATASET
)) == NULL
)
2832 if (zfs_perm_set(zhp
, zperms
)) {
2834 nvlist_free(zperms
);
2837 nvlist_free(zperms
);
2844 unallow_callback(zfs_handle_t
*zhp
, void *data
)
2846 nvlist_t
*nvp
= (nvlist_t
*)data
;
2849 error
= zfs_perm_remove(zhp
, nvp
);
2851 (void) fprintf(stderr
, gettext("Failed to remove permissions "
2852 "on %s\n"), zfs_get_name(zhp
));
2858 zfs_do_unallow(int argc
, char **argv
)
2860 int recurse
= B_FALSE
;
2863 nvlist_t
*zperms
= NULL
;
2866 if (parse_allow_args(&argc
, &argv
, B_TRUE
,
2867 &ds
, &recurse
, &zperms
) == -1)
2871 flags
|= ZFS_ITER_RECURSE
;
2872 error
= zfs_for_each(argc
, argv
, flags
,
2873 ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_VOLUME
, NULL
,
2874 NULL
, unallow_callback
, (void *)zperms
);
2877 nvlist_free(zperms
);
2882 typedef struct get_all_cbdata
{
2883 zfs_handle_t
**cb_handles
;
2887 boolean_t cb_verbose
;
2890 #define CHECK_SPINNER 30
2891 #define SPINNER_TIME 3 /* seconds */
2892 #define MOUNT_TIME 5 /* seconds */
2895 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
2897 static char spin
[] = { '-', '\\', '|', '/' };
2898 static int spinval
= 0;
2899 static int spincheck
= 0;
2900 static time_t last_spin_time
= (time_t)0;
2901 get_all_cbdata_t
*cbp
= data
;
2902 zfs_type_t type
= zfs_get_type(zhp
);
2904 if (cbp
->cb_verbose
) {
2905 if (--spincheck
< 0) {
2906 time_t now
= time(NULL
);
2907 if (last_spin_time
+ SPINNER_TIME
< now
) {
2908 (void) printf("\b%c", spin
[spinval
++ % 4]);
2909 (void) fflush(stdout
);
2910 last_spin_time
= now
;
2912 spincheck
= CHECK_SPINNER
;
2917 * Interate over any nested datasets.
2919 if (type
== ZFS_TYPE_FILESYSTEM
&&
2920 zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
2926 * Skip any datasets whose type does not match.
2928 if ((type
& cbp
->cb_types
) == 0) {
2933 if (cbp
->cb_alloc
== cbp
->cb_used
) {
2934 zfs_handle_t
**handles
;
2936 if (cbp
->cb_alloc
== 0)
2941 handles
= safe_malloc(cbp
->cb_alloc
* sizeof (void *));
2943 if (cbp
->cb_handles
) {
2944 bcopy(cbp
->cb_handles
, handles
,
2945 cbp
->cb_used
* sizeof (void *));
2946 free(cbp
->cb_handles
);
2949 cbp
->cb_handles
= handles
;
2952 cbp
->cb_handles
[cbp
->cb_used
++] = zhp
;
2958 get_all_datasets(uint_t types
, zfs_handle_t
***dslist
, size_t *count
,
2961 get_all_cbdata_t cb
= { 0 };
2962 cb
.cb_types
= types
;
2963 cb
.cb_verbose
= verbose
;
2966 (void) printf("%s: *", gettext("Reading ZFS config"));
2967 (void) fflush(stdout
);
2970 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
2972 *dslist
= cb
.cb_handles
;
2973 *count
= cb
.cb_used
;
2976 (void) printf("\b%s\n", gettext("done."));
2981 dataset_cmp(const void *a
, const void *b
)
2983 zfs_handle_t
**za
= (zfs_handle_t
**)a
;
2984 zfs_handle_t
**zb
= (zfs_handle_t
**)b
;
2985 char mounta
[MAXPATHLEN
];
2986 char mountb
[MAXPATHLEN
];
2987 boolean_t gota
, gotb
;
2989 if ((gota
= (zfs_get_type(*za
) == ZFS_TYPE_FILESYSTEM
)) != 0)
2990 verify(zfs_prop_get(*za
, ZFS_PROP_MOUNTPOINT
, mounta
,
2991 sizeof (mounta
), NULL
, NULL
, 0, B_FALSE
) == 0);
2992 if ((gotb
= (zfs_get_type(*zb
) == ZFS_TYPE_FILESYSTEM
)) != 0)
2993 verify(zfs_prop_get(*zb
, ZFS_PROP_MOUNTPOINT
, mountb
,
2994 sizeof (mountb
), NULL
, NULL
, 0, B_FALSE
) == 0);
2997 return (strcmp(mounta
, mountb
));
3004 return (strcmp(zfs_get_name(a
), zfs_get_name(b
)));
3008 * Generic callback for sharing or mounting filesystems. Because the code is so
3009 * similar, we have a common function with an extra parameter to determine which
3010 * mode we are using.
3012 #define OP_SHARE 0x1
3013 #define OP_MOUNT 0x2
3016 * Share or mount a dataset.
3019 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
3020 boolean_t
explicit, const char *options
)
3022 char mountpoint
[ZFS_MAXPROPLEN
];
3023 char shareopts
[ZFS_MAXPROPLEN
];
3024 char smbshareopts
[ZFS_MAXPROPLEN
];
3025 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
3027 uint64_t zoned
, canmount
;
3028 zfs_type_t type
= zfs_get_type(zhp
);
3029 boolean_t shared_nfs
, shared_smb
;
3031 assert(type
& (ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
));
3033 if (type
== ZFS_TYPE_FILESYSTEM
) {
3035 * Check to make sure we can mount/share this dataset. If we
3036 * are in the global zone and the filesystem is exported to a
3037 * local zone, or if we are in a local zone and the
3038 * filesystem is not exported, then it is an error.
3040 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3042 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
3046 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3047 "dataset is exported to a local zone\n"), cmdname
,
3051 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
3055 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3056 "permission denied\n"), cmdname
,
3062 * Ignore any filesystems which don't apply to us. This
3063 * includes those with a legacy mountpoint, or those with
3064 * legacy share options.
3066 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
3067 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
3068 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
3069 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3070 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
3071 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3073 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
3074 strcmp(smbshareopts
, "off") == 0) {
3078 (void) fprintf(stderr
, gettext("cannot share '%s': "
3079 "legacy share\n"), zfs_get_name(zhp
));
3080 (void) fprintf(stderr
, gettext("use share(1M) to "
3081 "share this filesystem, or set "
3082 "sharenfs property on\n"));
3087 * We cannot share or mount legacy filesystems. If the
3088 * shareopts is non-legacy but the mountpoint is legacy, we
3089 * treat it as a legacy share.
3091 if (strcmp(mountpoint
, "legacy") == 0) {
3095 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3096 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
3097 (void) fprintf(stderr
, gettext("use %s(1M) to "
3098 "%s this filesystem\n"), cmdname
, cmdname
);
3102 if (strcmp(mountpoint
, "none") == 0) {
3106 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
3107 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
3112 * canmount explicit outcome
3113 * on no pass through
3114 * on yes pass through
3116 * off yes display error, return 1
3117 * noauto no return 0
3118 * noauto yes pass through
3120 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
3121 if (canmount
== ZFS_CANMOUNT_OFF
) {
3125 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3126 "'canmount' property is set to 'off'\n"), cmdname
,
3129 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
3134 * At this point, we have verified that the mountpoint and/or
3135 * shareopts are appropriate for auto management. If the
3136 * filesystem is already mounted or shared, return (failing
3137 * for explicit requests); otherwise mount or share the
3143 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
3144 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
3146 if (shared_nfs
&& shared_smb
||
3147 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
3148 strcmp(smbshareopts
, "off") == 0) ||
3149 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
3150 strcmp(shareopts
, "off") == 0)) {
3154 (void) fprintf(stderr
, gettext("cannot share "
3155 "'%s': filesystem already shared\n"),
3160 if (!zfs_is_mounted(zhp
, NULL
) &&
3161 zfs_mount(zhp
, NULL
, 0) != 0)
3164 if (protocol
== NULL
) {
3165 if (zfs_shareall(zhp
) != 0)
3167 } else if (strcmp(protocol
, "nfs") == 0) {
3168 if (zfs_share_nfs(zhp
))
3170 } else if (strcmp(protocol
, "smb") == 0) {
3171 if (zfs_share_smb(zhp
))
3174 (void) fprintf(stderr
, gettext("cannot share "
3175 "'%s': invalid share type '%s' "
3177 zfs_get_name(zhp
), protocol
);
3184 if (options
== NULL
)
3185 mnt
.mnt_mntopts
= "";
3187 mnt
.mnt_mntopts
= (char *)options
;
3189 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
3190 zfs_is_mounted(zhp
, NULL
)) {
3194 (void) fprintf(stderr
, gettext("cannot mount "
3195 "'%s': filesystem already mounted\n"),
3200 if (zfs_mount(zhp
, options
, flags
) != 0)
3205 assert(op
== OP_SHARE
);
3208 * Ignore any volumes that aren't shared.
3210 verify(zfs_prop_get(zhp
, ZFS_PROP_SHAREISCSI
, shareopts
,
3211 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3213 if (strcmp(shareopts
, "off") == 0) {
3217 (void) fprintf(stderr
, gettext("cannot share '%s': "
3218 "'shareiscsi' property not set\n"),
3220 (void) fprintf(stderr
, gettext("set 'shareiscsi' "
3221 "property or use iscsitadm(1M) to share this "
3226 if (zfs_is_shared_iscsi(zhp
)) {
3230 (void) fprintf(stderr
, gettext("cannot share "
3231 "'%s': volume already shared\n"),
3236 if (zfs_share_iscsi(zhp
) != 0)
3244 * Reports progress in the form "(current/total)". Not thread-safe.
3247 report_mount_progress(int current
, int total
)
3250 static char *reverse
= "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3251 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3252 static time_t last_progress_time
;
3253 time_t now
= time(NULL
);
3255 /* report 1..n instead of 0..n-1 */
3258 /* display header if we're here for the first time */
3260 (void) printf(gettext("Mounting ZFS filesystems: "));
3262 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
3263 /* too soon to report again */
3267 last_progress_time
= now
;
3269 /* back up to prepare for overwriting */
3271 (void) printf("%*.*s", len
, len
, reverse
);
3273 /* We put a newline at the end if this is the last one. */
3274 len
= printf("(%d/%d)%s", current
, total
, current
== total
? "\n" : "");
3275 (void) fflush(stdout
);
3279 append_options(char *mntopts
, char *newopts
)
3281 int len
= strlen(mntopts
);
3283 /* original length plus new string to append plus 1 for the comma */
3284 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
3285 (void) fprintf(stderr
, gettext("the opts argument for "
3286 "'%c' option is too long (more than %d chars)\n"),
3287 "-o", MNT_LINE_MAX
);
3292 mntopts
[len
++] = ',';
3294 (void) strcpy(&mntopts
[len
], newopts
);
3298 share_mount(int op
, int argc
, char **argv
)
3301 boolean_t verbose
= B_FALSE
;
3303 char *options
= NULL
;
3304 int types
, flags
= 0;
3307 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
3317 if (*optarg
== '\0') {
3318 (void) fprintf(stderr
, gettext("empty mount "
3319 "options (-o) specified\n"));
3323 if (options
== NULL
)
3324 options
= safe_malloc(MNT_LINE_MAX
+ 1);
3326 /* option validation is done later */
3327 append_options(options
, optarg
);
3331 flags
|= MS_OVERLAY
;
3334 (void) fprintf(stderr
, gettext("missing argument for "
3335 "'%c' option\n"), optopt
);
3339 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3348 /* check number of arguments */
3350 zfs_handle_t
**dslist
= NULL
;
3351 size_t i
, count
= 0;
3352 char *protocol
= NULL
;
3354 if (op
== OP_MOUNT
) {
3355 types
= ZFS_TYPE_FILESYSTEM
;
3356 } else if (argc
> 0) {
3357 if (strcmp(argv
[0], "nfs") == 0 ||
3358 strcmp(argv
[0], "smb") == 0) {
3359 types
= ZFS_TYPE_FILESYSTEM
;
3360 } else if (strcmp(argv
[0], "iscsi") == 0) {
3361 types
= ZFS_TYPE_VOLUME
;
3363 (void) fprintf(stderr
, gettext("share type "
3364 "must be 'nfs', 'smb' or 'iscsi'\n"));
3371 types
= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
3375 (void) fprintf(stderr
, gettext("too many arguments\n"));
3379 get_all_datasets(types
, &dslist
, &count
, verbose
);
3384 qsort(dslist
, count
, sizeof (void *), dataset_cmp
);
3386 for (i
= 0; i
< count
; i
++) {
3388 report_mount_progress(i
, count
);
3390 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
3391 B_FALSE
, options
) != 0)
3393 zfs_close(dslist
[i
]);
3397 } else if (argc
== 0) {
3398 struct mnttab entry
;
3400 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
3401 (void) fprintf(stderr
, gettext("missing filesystem "
3402 "argument (specify -a for all)\n"));
3407 * When mount is given no arguments, go through /etc/mnttab and
3408 * display any active ZFS mounts. We hide any snapshots, since
3409 * they are controlled automatically.
3411 rewind(mnttab_file
);
3412 while (getmntent(mnttab_file
, &entry
) == 0) {
3413 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
3414 strchr(entry
.mnt_special
, '@') != NULL
)
3417 (void) printf("%-30s %s\n", entry
.mnt_special
,
3424 types
= ZFS_TYPE_FILESYSTEM
;
3426 types
|= ZFS_TYPE_VOLUME
;
3429 (void) fprintf(stderr
,
3430 gettext("too many arguments\n"));
3434 if ((zhp
= zfs_open(g_zfs
, argv
[0], types
)) == NULL
) {
3437 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
3447 * zfs mount -a [nfs | iscsi]
3448 * zfs mount filesystem
3450 * Mount all filesystems, or mount the given filesystem.
3453 zfs_do_mount(int argc
, char **argv
)
3455 return (share_mount(OP_MOUNT
, argc
, argv
));
3459 * zfs share -a [nfs | iscsi | smb]
3460 * zfs share filesystem
3462 * Share all filesystems, or share the given filesystem.
3465 zfs_do_share(int argc
, char **argv
)
3467 return (share_mount(OP_SHARE
, argc
, argv
));
3470 typedef struct unshare_unmount_node
{
3471 zfs_handle_t
*un_zhp
;
3473 uu_avl_node_t un_avlnode
;
3474 } unshare_unmount_node_t
;
3478 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
3480 const unshare_unmount_node_t
*l
= larg
;
3481 const unshare_unmount_node_t
*r
= rarg
;
3483 return (strcmp(l
->un_mountp
, r
->un_mountp
));
3487 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3488 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3489 * and unmount it appropriately.
3492 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
3496 struct stat64 statbuf
;
3497 struct extmnttab entry
;
3498 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
3502 * Search for the path in /etc/mnttab. Rather than looking for the
3503 * specific path, which can be fooled by non-standard paths (i.e. ".."
3504 * or "//"), we stat() the path and search for the corresponding
3505 * (major,minor) device pair.
3507 if (stat64(path
, &statbuf
) != 0) {
3508 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3509 cmdname
, path
, strerror(errno
));
3512 path_inode
= statbuf
.st_ino
;
3515 * Search for the given (major,minor) pair in the mount table.
3517 rewind(mnttab_file
);
3518 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
3519 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
3520 entry
.mnt_minor
== minor(statbuf
.st_dev
))
3524 if (op
== OP_SHARE
) {
3525 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
3526 "currently mounted\n"), cmdname
, path
);
3529 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
3531 if ((ret
= umount2(path
, flags
)) != 0)
3532 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
3537 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
3538 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
3539 "filesystem\n"), cmdname
, path
);
3543 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3544 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3548 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
3549 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3550 cmdname
, path
, strerror(errno
));
3552 } else if (statbuf
.st_ino
!= path_inode
) {
3553 (void) fprintf(stderr
, gettext("cannot "
3554 "%s '%s': not a mountpoint\n"), cmdname
, path
);
3558 if (op
== OP_SHARE
) {
3559 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3560 char smbshare_prop
[ZFS_MAXPROPLEN
];
3562 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
3563 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3564 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
3565 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3567 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3568 strcmp(smbshare_prop
, "off") == 0) {
3569 (void) fprintf(stderr
, gettext("cannot unshare "
3570 "'%s': legacy share\n"), path
);
3571 (void) fprintf(stderr
, gettext("use "
3572 "unshare(1M) to unshare this filesystem\n"));
3573 } else if (!zfs_is_shared(zhp
)) {
3574 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
3575 "not currently shared\n"), path
);
3577 ret
= zfs_unshareall_bypath(zhp
, path
);
3580 char mtpt_prop
[ZFS_MAXPROPLEN
];
3582 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
3583 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3586 ret
= zfs_unmount(zhp
, NULL
, flags
);
3587 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
3588 (void) fprintf(stderr
, gettext("cannot unmount "
3589 "'%s': legacy mountpoint\n"),
3591 (void) fprintf(stderr
, gettext("use umount(1M) "
3592 "to unmount this filesystem\n"));
3594 ret
= zfs_unmountall(zhp
, flags
);
3605 * Generic callback for unsharing or unmounting a filesystem.
3608 unshare_unmount(int op
, int argc
, char **argv
)
3615 char nfsiscsi_mnt_prop
[ZFS_MAXPROPLEN
];
3616 char sharesmb
[ZFS_MAXPROPLEN
];
3619 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
3628 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3639 * We could make use of zfs_for_each() to walk all datasets in
3640 * the system, but this would be very inefficient, especially
3641 * since we would have to linearly search /etc/mnttab for each
3642 * one. Instead, do one pass through /etc/mnttab looking for
3643 * zfs entries and call zfs_unmount() for each one.
3645 * Things get a little tricky if the administrator has created
3646 * mountpoints beneath other ZFS filesystems. In this case, we
3647 * have to unmount the deepest filesystems first. To accomplish
3648 * this, we place all the mountpoints in an AVL tree sorted by
3649 * the special type (dataset name), and walk the result in
3650 * reverse to make sure to get any snapshots first.
3652 struct mnttab entry
;
3653 uu_avl_pool_t
*pool
;
3655 unshare_unmount_node_t
*node
;
3657 uu_avl_walk_t
*walk
;
3660 (void) fprintf(stderr
, gettext("too many arguments\n"));
3664 if ((pool
= uu_avl_pool_create("unmount_pool",
3665 sizeof (unshare_unmount_node_t
),
3666 offsetof(unshare_unmount_node_t
, un_avlnode
),
3667 unshare_unmount_compare
,
3668 UU_DEFAULT
)) == NULL
) {
3669 (void) fprintf(stderr
, gettext("internal error: "
3670 "out of memory\n"));
3674 if ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
) {
3675 (void) fprintf(stderr
, gettext("internal error: "
3676 "out of memory\n"));
3680 rewind(mnttab_file
);
3681 while (getmntent(mnttab_file
, &entry
) == 0) {
3683 /* ignore non-ZFS entries */
3684 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
3687 /* ignore snapshots */
3688 if (strchr(entry
.mnt_special
, '@') != NULL
)
3691 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3692 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3699 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3701 sizeof (nfsiscsi_mnt_prop
),
3702 NULL
, NULL
, 0, B_FALSE
) == 0);
3703 if (strcmp(nfsiscsi_mnt_prop
, "off") != 0)
3705 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3707 sizeof (nfsiscsi_mnt_prop
),
3708 NULL
, NULL
, 0, B_FALSE
) == 0);
3709 if (strcmp(nfsiscsi_mnt_prop
, "off") == 0)
3713 /* Ignore legacy mounts */
3714 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
3716 sizeof (nfsiscsi_mnt_prop
),
3717 NULL
, NULL
, 0, B_FALSE
) == 0);
3718 if (strcmp(nfsiscsi_mnt_prop
, "legacy") == 0)
3720 /* Ignore canmount=noauto mounts */
3721 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
3722 ZFS_CANMOUNT_NOAUTO
)
3728 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
3731 if ((node
->un_mountp
= strdup(entry
.mnt_mountp
)) ==
3733 (void) fprintf(stderr
, gettext("internal error:"
3734 " out of memory\n"));
3738 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
3740 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
3741 uu_avl_insert(tree
, node
, idx
);
3743 zfs_close(node
->un_zhp
);
3744 free(node
->un_mountp
);
3750 * Walk the AVL tree in reverse, unmounting each filesystem and
3751 * removing it from the AVL tree in the process.
3753 if ((walk
= uu_avl_walk_start(tree
,
3754 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
) {
3755 (void) fprintf(stderr
,
3756 gettext("internal error: out of memory"));
3760 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3761 uu_avl_remove(tree
, node
);
3765 if (zfs_unshareall_bypath(node
->un_zhp
,
3766 node
->un_mountp
) != 0)
3771 if (zfs_unmount(node
->un_zhp
,
3772 node
->un_mountp
, flags
) != 0)
3777 zfs_close(node
->un_zhp
);
3778 free(node
->un_mountp
);
3782 uu_avl_walk_end(walk
);
3783 uu_avl_destroy(tree
);
3784 uu_avl_pool_destroy(pool
);
3786 if (op
== OP_SHARE
) {
3788 * Finally, unshare any volumes shared via iSCSI.
3790 zfs_handle_t
**dslist
= NULL
;
3791 size_t i
, count
= 0;
3793 get_all_datasets(ZFS_TYPE_VOLUME
, &dslist
, &count
,
3797 qsort(dslist
, count
, sizeof (void *),
3800 for (i
= 0; i
< count
; i
++) {
3801 if (zfs_unshare_iscsi(dslist
[i
]) != 0)
3803 zfs_close(dslist
[i
]);
3812 (void) fprintf(stderr
,
3813 gettext("missing filesystem argument\n"));
3815 (void) fprintf(stderr
,
3816 gettext("too many arguments\n"));
3821 * We have an argument, but it may be a full path or a ZFS
3822 * filesystem. Pass full paths off to unmount_path() (shared by
3823 * manual_unmount), otherwise open the filesystem and pass to
3826 if (argv
[0][0] == '/')
3827 return (unshare_unmount_path(op
, argv
[0],
3830 types
= ZFS_TYPE_FILESYSTEM
;
3832 types
|= ZFS_TYPE_VOLUME
;
3834 if ((zhp
= zfs_open(g_zfs
, argv
[0], types
)) == NULL
)
3837 if (zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
3838 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
3839 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
3840 nfsiscsi_mnt_prop
, sizeof (nfsiscsi_mnt_prop
), NULL
,
3841 NULL
, 0, B_FALSE
) == 0);
3845 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3847 sizeof (nfsiscsi_mnt_prop
),
3848 NULL
, NULL
, 0, B_FALSE
) == 0);
3849 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3850 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
3853 if (strcmp(nfsiscsi_mnt_prop
, "off") == 0 &&
3854 strcmp(sharesmb
, "off") == 0) {
3855 (void) fprintf(stderr
, gettext("cannot "
3856 "unshare '%s': legacy share\n"),
3858 (void) fprintf(stderr
, gettext("use "
3859 "unshare(1M) to unshare this "
3862 } else if (!zfs_is_shared(zhp
)) {
3863 (void) fprintf(stderr
, gettext("cannot "
3864 "unshare '%s': not currently "
3865 "shared\n"), zfs_get_name(zhp
));
3867 } else if (zfs_unshareall(zhp
) != 0) {
3873 if (strcmp(nfsiscsi_mnt_prop
, "legacy") == 0) {
3874 (void) fprintf(stderr
, gettext("cannot "
3875 "unmount '%s': legacy "
3876 "mountpoint\n"), zfs_get_name(zhp
));
3877 (void) fprintf(stderr
, gettext("use "
3878 "umount(1M) to unmount this "
3881 } else if (!zfs_is_mounted(zhp
, NULL
)) {
3882 (void) fprintf(stderr
, gettext("cannot "
3883 "unmount '%s': not currently "
3887 } else if (zfs_unmountall(zhp
, flags
) != 0) {
3893 assert(op
== OP_SHARE
);
3895 verify(zfs_prop_get(zhp
, ZFS_PROP_SHAREISCSI
,
3896 nfsiscsi_mnt_prop
, sizeof (nfsiscsi_mnt_prop
),
3897 NULL
, NULL
, 0, B_FALSE
) == 0);
3899 if (strcmp(nfsiscsi_mnt_prop
, "off") == 0) {
3900 (void) fprintf(stderr
, gettext("cannot unshare "
3901 "'%s': 'shareiscsi' property not set\n"),
3903 (void) fprintf(stderr
, gettext("set "
3904 "'shareiscsi' property or use "
3905 "iscsitadm(1M) to share this volume\n"));
3907 } else if (!zfs_is_shared_iscsi(zhp
)) {
3908 (void) fprintf(stderr
, gettext("cannot "
3909 "unshare '%s': not currently shared\n"),
3912 } else if (zfs_unshare_iscsi(zhp
) != 0) {
3925 * zfs unmount filesystem
3927 * Unmount all filesystems, or a specific ZFS filesystem.
3930 zfs_do_unmount(int argc
, char **argv
)
3932 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
3937 * zfs unshare filesystem
3939 * Unshare all filesystems, or a specific ZFS filesystem.
3942 zfs_do_unshare(int argc
, char **argv
)
3944 return (unshare_unmount(OP_SHARE
, argc
, argv
));
3948 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
3949 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
3952 manual_mount(int argc
, char **argv
)
3955 char mountpoint
[ZFS_MAXPROPLEN
];
3956 char mntopts
[MNT_LINE_MAX
] = { '\0' };
3960 char *dataset
, *path
;
3963 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
3966 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
3969 flags
|= MS_OVERLAY
;
3972 flags
|= MS_NOMNTTAB
;
3975 (void) fprintf(stderr
, gettext("missing argument for "
3976 "'%c' option\n"), optopt
);
3980 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3982 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
3991 /* check that we only have two arguments */
3994 (void) fprintf(stderr
, gettext("missing dataset "
3997 (void) fprintf(stderr
,
3998 gettext("missing mountpoint argument\n"));
4000 (void) fprintf(stderr
, gettext("too many arguments\n"));
4001 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
4008 /* try to open the dataset */
4009 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
4012 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
4013 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
4015 /* check for legacy mountpoint and complain appropriately */
4017 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
4018 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
4019 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
4020 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
4025 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
4026 "mounted using 'mount -F zfs'\n"), dataset
);
4027 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
4028 "instead.\n"), path
);
4029 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
4030 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
4031 (void) fprintf(stderr
, gettext("See zfs(1M) for more "
4040 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
4041 * unmounts of non-legacy filesystems, as this is the dominant administrative
4045 manual_unmount(int argc
, char **argv
)
4051 while ((c
= getopt(argc
, argv
, "f")) != -1) {
4057 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4059 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
4068 /* check arguments */
4071 (void) fprintf(stderr
, gettext("missing path "
4074 (void) fprintf(stderr
, gettext("too many arguments\n"));
4075 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
4079 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
4083 volcheck(zpool_handle_t
*zhp
, void *data
)
4085 boolean_t isinit
= *((boolean_t
*)data
);
4088 return (zpool_create_zvol_links(zhp
));
4090 return (zpool_remove_zvol_links(zhp
));
4094 * Iterate over all pools in the system and either create or destroy /dev/zvol
4095 * links, depending on the value of 'isinit'.
4098 do_volcheck(boolean_t isinit
)
4100 return (zpool_iter(g_zfs
, volcheck
, &isinit
) ? 1 : 0);
4104 find_command_idx(char *command
, int *idx
)
4108 for (i
= 0; i
< NCOMMAND
; i
++) {
4109 if (command_table
[i
].name
== NULL
)
4112 if (strcmp(command
, command_table
[i
].name
) == 0) {
4121 main(int argc
, char **argv
)
4128 (void) setlocale(LC_ALL
, "");
4129 (void) textdomain(TEXT_DOMAIN
);
4133 if ((g_zfs
= libzfs_init()) == NULL
) {
4134 (void) fprintf(stderr
, gettext("internal error: failed to "
4135 "initialize ZFS library\n"));
4139 zpool_set_history_str("zfs", argc
, argv
, history_str
);
4140 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
4142 libzfs_print_on_error(g_zfs
, B_TRUE
);
4144 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
4145 (void) fprintf(stderr
, gettext("internal error: unable to "
4146 "open %s\n"), MNTTAB
);
4151 * This command also doubles as the /etc/fs mount and unmount program.
4152 * Determine if we should take this behavior based on argv[0].
4154 progname
= basename(argv
[0]);
4155 if (strcmp(progname
, "mount") == 0) {
4156 ret
= manual_mount(argc
, argv
);
4157 } else if (strcmp(progname
, "umount") == 0) {
4158 ret
= manual_unmount(argc
, argv
);
4161 * Make sure the user has specified some command.
4164 (void) fprintf(stderr
, gettext("missing command\n"));
4171 * The 'umount' command is an alias for 'unmount'
4173 if (strcmp(cmdname
, "umount") == 0)
4174 cmdname
= "unmount";
4177 * The 'recv' command is an alias for 'receive'
4179 if (strcmp(cmdname
, "recv") == 0)
4180 cmdname
= "receive";
4185 if (strcmp(cmdname
, "-?") == 0)
4189 * 'volinit' and 'volfini' do not appear in the usage message,
4190 * so we have to special case them here.
4192 if (strcmp(cmdname
, "volinit") == 0)
4193 return (do_volcheck(B_TRUE
));
4194 else if (strcmp(cmdname
, "volfini") == 0)
4195 return (do_volcheck(B_FALSE
));
4198 * Run the appropriate command.
4200 if (find_command_idx(cmdname
, &i
) == 0) {
4201 current_command
= &command_table
[i
];
4202 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
4203 } else if (strchr(cmdname
, '=') != NULL
) {
4204 verify(find_command_idx("set", &i
) == 0);
4205 current_command
= &command_table
[i
];
4206 ret
= command_table
[i
].func(argc
, argv
);
4208 (void) fprintf(stderr
, gettext("unrecognized "
4209 "command '%s'\n"), cmdname
);
4214 (void) fclose(mnttab_file
);
4219 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4220 * for the purposes of running ::findleaks.
4222 if (getenv("ZFS_ABORT") != NULL
) {
4223 (void) printf("dumping core by request\n");