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 2008 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();
375 * "zfs set|get" must not be localised this is the
376 * command name and arguments.
380 gettext("\nFor the property list, run: zfs set|get\n"));
383 gettext("\nFor the delegated permission list, run:"
384 " zfs allow|unallow\n"));
388 * See comments at end of main().
390 if (getenv("ZFS_ABORT") != NULL
) {
391 (void) printf("dumping core by request\n");
395 exit(requested
? 0 : 2);
399 parseprop(nvlist_t
*props
)
401 char *propname
= optarg
;
402 char *propval
, *strval
;
404 if ((propval
= strchr(propname
, '=')) == NULL
) {
405 (void) fprintf(stderr
, gettext("missing "
406 "'=' for -o option\n"));
411 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
412 (void) fprintf(stderr
, gettext("property '%s' "
413 "specified multiple times\n"), propname
);
416 if (nvlist_add_string(props
, propname
, propval
) != 0) {
417 (void) fprintf(stderr
, gettext("internal "
418 "error: out of memory\n"));
426 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
428 * Given an existing dataset, create a writable copy whose initial contents
429 * are the same as the source. The newly created dataset maintains a
430 * dependency on the original; the original cannot be destroyed so long as
433 * The '-p' flag creates all the non-existing ancestors of the target first.
436 zfs_do_clone(int argc
, char **argv
)
438 zfs_handle_t
*zhp
= NULL
;
439 boolean_t parents
= B_FALSE
;
444 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
445 (void) fprintf(stderr
, gettext("internal error: "
451 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
454 if (parseprop(props
))
461 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
470 /* check number of arguments */
472 (void) fprintf(stderr
, gettext("missing source dataset "
477 (void) fprintf(stderr
, gettext("missing target dataset "
482 (void) fprintf(stderr
, gettext("too many arguments\n"));
486 /* open the source dataset */
487 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
490 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
493 * Now create the ancestors of the target dataset. If the
494 * target already exists and '-p' option was used we should not
497 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
500 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
505 ret
= zfs_clone(zhp
, argv
[1], props
);
507 /* create the mountpoint if necessary */
511 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
513 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
514 ret
= zfs_share(clone
);
533 * zfs create [-p] [-o prop=value] ... fs
534 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
536 * Create a new dataset. This command can be used to create filesystems
537 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
538 * For volumes, the user must specify a size to be used.
540 * The '-s' flag applies only to volumes, and indicates that we should not try
541 * to set the reservation for this volume. By default we set a reservation
542 * equal to the size for any volume. For pools with SPA_VERSION >=
543 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
545 * The '-p' flag creates all the non-existing ancestors of the target first.
548 zfs_do_create(int argc
, char **argv
)
550 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
551 zfs_handle_t
*zhp
= NULL
;
554 boolean_t noreserve
= B_FALSE
;
555 boolean_t bflag
= B_FALSE
;
556 boolean_t parents
= B_FALSE
;
562 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
563 (void) fprintf(stderr
, gettext("internal error: "
569 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
572 type
= ZFS_TYPE_VOLUME
;
573 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
574 (void) fprintf(stderr
, gettext("bad volume "
575 "size '%s': %s\n"), optarg
,
576 libzfs_error_description(g_zfs
));
580 if (nvlist_add_uint64(props
,
581 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
583 (void) fprintf(stderr
, gettext("internal "
584 "error: out of memory\n"));
594 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
595 (void) fprintf(stderr
, gettext("bad volume "
596 "block size '%s': %s\n"), optarg
,
597 libzfs_error_description(g_zfs
));
601 if (nvlist_add_uint64(props
,
602 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
604 (void) fprintf(stderr
, gettext("internal "
605 "error: out of memory\n"));
610 if (parseprop(props
))
617 (void) fprintf(stderr
, gettext("missing size "
622 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
628 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
629 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
630 "used when creating a volume\n"));
637 /* check number of arguments */
639 (void) fprintf(stderr
, gettext("missing %s argument\n"),
640 zfs_type_to_name(type
));
644 (void) fprintf(stderr
, gettext("too many arguments\n"));
648 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
649 zpool_handle_t
*zpool_handle
;
650 uint64_t spa_version
;
652 zfs_prop_t resv_prop
;
655 if (p
= strchr(argv
[0], '/'))
657 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
660 if (zpool_handle
== NULL
)
662 spa_version
= zpool_get_prop_int(zpool_handle
,
663 ZPOOL_PROP_VERSION
, NULL
);
664 zpool_close(zpool_handle
);
665 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
666 resv_prop
= ZFS_PROP_REFRESERVATION
;
668 resv_prop
= ZFS_PROP_RESERVATION
;
670 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
672 if (nvlist_add_uint64(props
,
673 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
674 (void) fprintf(stderr
, gettext("internal "
675 "error: out of memory\n"));
682 if (parents
&& zfs_name_valid(argv
[0], type
)) {
684 * Now create the ancestors of target dataset. If the target
685 * already exists and '-p' option was used we should not
688 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
692 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
697 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
700 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
703 * if the user doesn't want the dataset automatically mounted,
704 * then skip the mount/share step
707 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
710 * Mount and/or share the new filesystem as appropriate. We provide a
711 * verbose error message to let the user know that their filesystem was
712 * in fact created, even if we failed to mount or share it.
715 if (canmount
== ZFS_CANMOUNT_ON
) {
716 if (zfs_mount(zhp
, NULL
, 0) != 0) {
717 (void) fprintf(stderr
, gettext("filesystem "
718 "successfully created, but not mounted\n"));
720 } else if (zfs_share(zhp
) != 0) {
721 (void) fprintf(stderr
, gettext("filesystem "
722 "successfully created, but not shared\n"));
739 * zfs destroy [-rf] <fs, snap, vol>
741 * -r Recursively destroy all children
742 * -R Recursively destroy all dependents, including clones
743 * -f Force unmounting of any dependents
745 * Destroys the given dataset. By default, it will unmount any filesystems,
746 * and refuse to destroy a dataset that has any dependents. A dependent can
747 * either be a child, or a clone of a child.
749 typedef struct destroy_cbdata
{
756 boolean_t cb_closezhp
;
757 zfs_handle_t
*cb_target
;
762 * Check for any dependents based on the '-r' or '-R' flags.
765 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
767 destroy_cbdata_t
*cbp
= data
;
768 const char *tname
= zfs_get_name(cbp
->cb_target
);
769 const char *name
= zfs_get_name(zhp
);
771 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
772 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
774 * This is a direct descendant, not a clone somewhere else in
781 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
782 "%s has children\n"),
783 zfs_get_name(cbp
->cb_target
),
784 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
785 (void) fprintf(stderr
, gettext("use '-r' to destroy "
786 "the following datasets:\n"));
787 cbp
->cb_first
= B_FALSE
;
791 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
794 * This is a clone. We only want to report this if the '-r'
795 * wasn't specified, or the target is a snapshot.
797 if (!cbp
->cb_recurse
&&
798 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
802 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
803 "%s has dependent clones\n"),
804 zfs_get_name(cbp
->cb_target
),
805 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
806 (void) fprintf(stderr
, gettext("use '-R' to destroy "
807 "the following datasets:\n"));
808 cbp
->cb_first
= B_FALSE
;
812 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
821 destroy_callback(zfs_handle_t
*zhp
, void *data
)
823 destroy_cbdata_t
*cbp
= data
;
826 * Ignore pools (which we've already flagged as an error before getting
829 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
830 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
836 * Bail out on the first error.
838 if (zfs_unmount(zhp
, NULL
, cbp
->cb_force
? MS_FORCE
: 0) != 0 ||
839 zfs_destroy(zhp
) != 0) {
849 destroy_snap_clones(zfs_handle_t
*zhp
, void *arg
)
851 destroy_cbdata_t
*cbp
= arg
;
852 char thissnap
[MAXPATHLEN
];
854 boolean_t closezhp
= cbp
->cb_closezhp
;
857 (void) snprintf(thissnap
, sizeof (thissnap
),
858 "%s@%s", zfs_get_name(zhp
), cbp
->cb_snapname
);
860 libzfs_print_on_error(g_zfs
, B_FALSE
);
861 szhp
= zfs_open(g_zfs
, thissnap
, ZFS_TYPE_SNAPSHOT
);
862 libzfs_print_on_error(g_zfs
, B_TRUE
);
865 * Destroy any clones of this snapshot
867 if (zfs_iter_dependents(szhp
, B_FALSE
, destroy_callback
,
877 cbp
->cb_closezhp
= B_TRUE
;
878 rv
= zfs_iter_filesystems(zhp
, destroy_snap_clones
, arg
);
885 zfs_do_destroy(int argc
, char **argv
)
887 destroy_cbdata_t cb
= { 0 };
893 while ((c
= getopt(argc
, argv
, "frR")) != -1) {
907 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
916 /* check number of arguments */
918 (void) fprintf(stderr
, gettext("missing path argument\n"));
922 (void) fprintf(stderr
, gettext("too many arguments\n"));
927 * If we are doing recursive destroy of a snapshot, then the
928 * named snapshot may not exist. Go straight to libzfs.
930 if (cb
.cb_recurse
&& (cp
= strchr(argv
[0], '@'))) {
934 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
939 if (cb
.cb_doclones
) {
941 if (destroy_snap_clones(zhp
, &cb
) != 0) {
947 ret
= zfs_destroy_snaps(zhp
, cp
);
950 (void) fprintf(stderr
,
951 gettext("no snapshots destroyed\n"));
957 /* Open the given dataset */
958 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
964 * Perform an explicit check for pools before going any further.
966 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
967 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
968 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
969 "operation does not apply to pools\n"),
971 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
972 "%s' to destroy all datasets in the pool\n"),
974 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
975 "to destroy the pool itself\n"), zfs_get_name(zhp
));
981 * Check for any dependents and/or clones.
983 cb
.cb_first
= B_TRUE
;
984 if (!cb
.cb_doclones
&&
985 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
992 zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
, &cb
) != 0) {
998 * Do the real thing. The callback will close the handle regardless of
999 * whether it succeeds or not.
1002 if (destroy_callback(zhp
, &cb
) != 0)
1010 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
1011 * < all | property[,property]... > < fs | snap | vol > ...
1013 * -r recurse over any child datasets
1014 * -H scripted mode. Headers are stripped, and fields are separated
1015 * by tabs instead of spaces.
1016 * -o Set of fields to display. One of "name,property,value,source".
1017 * Default is all four.
1018 * -s Set of sources to allow. One of
1019 * "local,default,inherited,temporary,none". Default is all
1021 * -p Display values in parsable (literal) format.
1023 * Prints properties for the given datasets. The user can control which
1024 * columns to display as well as which property types to allow.
1028 * Invoked to display the properties for a single dataset.
1031 get_callback(zfs_handle_t
*zhp
, void *data
)
1033 char buf
[ZFS_MAXPROPLEN
];
1034 zprop_source_t sourcetype
;
1035 char source
[ZFS_MAXNAMELEN
];
1036 zprop_get_cbdata_t
*cbp
= data
;
1037 nvlist_t
*userprop
= zfs_get_user_props(zhp
);
1038 zprop_list_t
*pl
= cbp
->cb_proplist
;
1043 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1045 * Skip the special fake placeholder. This will also skip over
1046 * the name property when 'all' is specified.
1048 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1049 pl
== cbp
->cb_proplist
)
1052 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1053 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1054 sizeof (buf
), &sourcetype
, source
,
1056 cbp
->cb_literal
) != 0) {
1059 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1060 ZFS_TYPE_DATASET
)) {
1061 (void) fprintf(stderr
,
1062 gettext("No such property '%s'\n"),
1063 zfs_prop_to_name(pl
->pl_prop
));
1066 sourcetype
= ZPROP_SRC_NONE
;
1067 (void) strlcpy(buf
, "-", sizeof (buf
));
1070 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1071 zfs_prop_to_name(pl
->pl_prop
),
1072 buf
, sourcetype
, source
);
1074 if (nvlist_lookup_nvlist(userprop
,
1075 pl
->pl_user_prop
, &propval
) != 0) {
1078 sourcetype
= ZPROP_SRC_NONE
;
1081 verify(nvlist_lookup_string(propval
,
1082 ZPROP_VALUE
, &strval
) == 0);
1083 verify(nvlist_lookup_string(propval
,
1084 ZPROP_SOURCE
, &sourceval
) == 0);
1086 if (strcmp(sourceval
,
1087 zfs_get_name(zhp
)) == 0) {
1088 sourcetype
= ZPROP_SRC_LOCAL
;
1090 sourcetype
= ZPROP_SRC_INHERITED
;
1091 (void) strlcpy(source
,
1092 sourceval
, sizeof (source
));
1096 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1097 pl
->pl_user_prop
, strval
, sourcetype
,
1106 zfs_do_get(int argc
, char **argv
)
1108 zprop_get_cbdata_t cb
= { 0 };
1109 int i
, c
, flags
= 0;
1110 char *value
, *fields
;
1112 zprop_list_t fake_name
= { 0 };
1115 * Set up default columns and sources.
1117 cb
.cb_sources
= ZPROP_SRC_ALL
;
1118 cb
.cb_columns
[0] = GET_COL_NAME
;
1119 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1120 cb
.cb_columns
[2] = GET_COL_VALUE
;
1121 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1122 cb
.cb_type
= ZFS_TYPE_DATASET
;
1125 while ((c
= getopt(argc
, argv
, ":o:s:rHp")) != -1) {
1128 cb
.cb_literal
= B_TRUE
;
1131 flags
|= ZFS_ITER_RECURSE
;
1134 cb
.cb_scripted
= B_TRUE
;
1137 (void) fprintf(stderr
, gettext("missing argument for "
1138 "'%c' option\n"), optopt
);
1143 * Process the set of columns to display. We zero out
1144 * the structure to give us a blank slate.
1146 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1148 while (*optarg
!= '\0') {
1149 static char *col_subopts
[] =
1150 { "name", "property", "value", "source",
1154 (void) fprintf(stderr
, gettext("too "
1155 "many fields given to -o "
1160 switch (getsubopt(&optarg
, col_subopts
,
1163 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1166 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1169 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1172 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1175 (void) fprintf(stderr
,
1176 gettext("invalid column name "
1185 while (*optarg
!= '\0') {
1186 static char *source_subopts
[] = {
1187 "local", "default", "inherited",
1188 "temporary", "none", NULL
};
1190 switch (getsubopt(&optarg
, source_subopts
,
1193 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1196 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1199 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1202 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1205 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1208 (void) fprintf(stderr
,
1209 gettext("invalid source "
1217 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1227 (void) fprintf(stderr
, gettext("missing property "
1234 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1242 * As part of zfs_expand_proplist(), we keep track of the maximum column
1243 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1244 * need to know the maximum name length. However, the user likely did
1245 * not specify 'name' as one of the properties to fetch, so we need to
1246 * make sure we always include at least this property for
1247 * print_get_headers() to work properly.
1249 if (cb
.cb_proplist
!= NULL
) {
1250 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1251 fake_name
.pl_width
= strlen(gettext("NAME"));
1252 fake_name
.pl_next
= cb
.cb_proplist
;
1253 cb
.cb_proplist
= &fake_name
;
1256 cb
.cb_first
= B_TRUE
;
1258 /* run for each object */
1259 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
, NULL
,
1260 &cb
.cb_proplist
, get_callback
, &cb
);
1262 if (cb
.cb_proplist
== &fake_name
)
1263 zprop_free_list(fake_name
.pl_next
);
1265 zprop_free_list(cb
.cb_proplist
);
1271 * inherit [-r] <property> <fs|vol> ...
1273 * -r Recurse over all children
1275 * For each dataset specified on the command line, inherit the given property
1276 * from its parent. Inheriting a property at the pool level will cause it to
1277 * use the default value. The '-r' flag will recurse over all children, and is
1278 * useful for setting a property on a hierarchy-wide basis, regardless of any
1279 * local modifications for each dataset.
1283 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1285 char *propname
= data
;
1286 zfs_prop_t prop
= zfs_name_to_prop(propname
);
1289 * If we're doing it recursively, then ignore properties that
1290 * are not valid for this type of dataset.
1292 if (prop
!= ZPROP_INVAL
&&
1293 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1296 return (zfs_prop_inherit(zhp
, propname
) != 0);
1300 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1302 char *propname
= data
;
1304 return (zfs_prop_inherit(zhp
, propname
) != 0);
1308 zfs_do_inherit(int argc
, char **argv
)
1317 while ((c
= getopt(argc
, argv
, "r")) != -1) {
1320 flags
|= ZFS_ITER_RECURSE
;
1324 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1333 /* check number of arguments */
1335 (void) fprintf(stderr
, gettext("missing property argument\n"));
1339 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1347 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1348 if (zfs_prop_readonly(prop
)) {
1349 (void) fprintf(stderr
, gettext(
1350 "%s property is read-only\n"),
1354 if (!zfs_prop_inheritable(prop
)) {
1355 (void) fprintf(stderr
, gettext("'%s' property cannot "
1356 "be inherited\n"), propname
);
1357 if (prop
== ZFS_PROP_QUOTA
||
1358 prop
== ZFS_PROP_RESERVATION
||
1359 prop
== ZFS_PROP_REFQUOTA
||
1360 prop
== ZFS_PROP_REFRESERVATION
)
1361 (void) fprintf(stderr
, gettext("use 'zfs set "
1362 "%s=none' to clear\n"), propname
);
1365 } else if (!zfs_prop_user(propname
)) {
1366 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1371 if (flags
& ZFS_ITER_RECURSE
) {
1372 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1373 NULL
, NULL
, inherit_recurse_cb
, propname
);
1375 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1376 NULL
, NULL
, inherit_cb
, propname
);
1382 typedef struct upgrade_cbdata
{
1383 uint64_t cb_numupgraded
;
1384 uint64_t cb_numsamegraded
;
1385 uint64_t cb_numfailed
;
1386 uint64_t cb_version
;
1388 boolean_t cb_foundone
;
1389 char cb_lastfs
[ZFS_MAXNAMELEN
];
1393 same_pool(zfs_handle_t
*zhp
, const char *name
)
1395 int len1
= strcspn(name
, "/@");
1396 const char *zhname
= zfs_get_name(zhp
);
1397 int len2
= strcspn(zhname
, "/@");
1401 return (strncmp(name
, zhname
, len1
) == 0);
1405 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1407 upgrade_cbdata_t
*cb
= data
;
1408 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1410 /* list if it's old/new */
1411 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1412 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1415 str
= gettext("The following filesystems are "
1416 "formatted using a newer software version and\n"
1417 "cannot be accessed on the current system.\n\n");
1419 str
= gettext("The following filesystems are "
1420 "out of date, and can be upgraded. After being\n"
1421 "upgraded, these filesystems (and any 'zfs send' "
1422 "streams generated from\n"
1423 "subsequent snapshots) will no longer be "
1424 "accessible by older software versions.\n\n");
1427 if (!cb
->cb_foundone
) {
1429 (void) printf(gettext("VER FILESYSTEM\n"));
1430 (void) printf(gettext("--- ------------\n"));
1431 cb
->cb_foundone
= B_TRUE
;
1434 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1441 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1443 upgrade_cbdata_t
*cb
= data
;
1444 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1446 if (cb
->cb_version
>= ZPL_VERSION_FUID
) {
1449 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1452 if (spa_version
< SPA_VERSION_FUID
) {
1454 (void) printf(gettext("%s: can not be upgraded; "
1455 "the pool version needs to first be upgraded\nto "
1457 zfs_get_name(zhp
), SPA_VERSION_FUID
);
1464 if (version
< cb
->cb_version
) {
1466 (void) snprintf(verstr
, sizeof (verstr
),
1467 "%llu", cb
->cb_version
);
1468 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1470 * If they did "zfs upgrade -a", then we could
1471 * be doing ioctls to different pools. We need
1472 * to log this history once to each pool.
1474 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
1476 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1477 cb
->cb_numupgraded
++;
1480 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1481 } else if (version
> cb
->cb_version
) {
1482 /* can't downgrade */
1483 (void) printf(gettext("%s: can not be downgraded; "
1484 "it is already at version %u\n"),
1485 zfs_get_name(zhp
), version
);
1488 cb
->cb_numsamegraded
++;
1496 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1499 zfs_do_upgrade(int argc
, char **argv
)
1501 boolean_t all
= B_FALSE
;
1502 boolean_t showversions
= B_FALSE
;
1504 upgrade_cbdata_t cb
= { 0 };
1506 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1509 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1512 flags
|= ZFS_ITER_RECURSE
;
1515 showversions
= B_TRUE
;
1518 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
1519 optarg
, &cb
.cb_version
) != 0) {
1520 (void) fprintf(stderr
,
1521 gettext("invalid version %s\n"), optarg
);
1530 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1539 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
1541 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
1542 cb
.cb_version
|| argc
))
1544 if ((all
|| argc
) && (showversions
))
1550 /* Show info on available versions. */
1551 (void) printf(gettext("The following filesystem versions are "
1553 (void) printf(gettext("VER DESCRIPTION\n"));
1554 (void) printf("--- -----------------------------------------"
1555 "---------------\n");
1556 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1557 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1558 (void) printf(gettext(" 3 Case insensitive and File system "
1559 "unique identifer (FUID)\n"));
1560 (void) printf(gettext("\nFor more information on a particular "
1561 "version, including supported releases, see:\n\n"));
1562 (void) printf("http://www.opensolaris.org/os/community/zfs/"
1563 "version/zpl/N\n\n");
1564 (void) printf(gettext("Where 'N' is the version number.\n"));
1566 } else if (argc
|| all
) {
1567 /* Upgrade filesystems */
1568 if (cb
.cb_version
== 0)
1569 cb
.cb_version
= ZPL_VERSION
;
1570 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
1571 NULL
, NULL
, upgrade_set_callback
, &cb
);
1572 (void) printf(gettext("%llu filesystems upgraded\n"),
1574 if (cb
.cb_numsamegraded
) {
1575 (void) printf(gettext("%llu filesystems already at "
1577 cb
.cb_numsamegraded
);
1579 if (cb
.cb_numfailed
!= 0)
1582 /* List old-version filesytems */
1584 (void) printf(gettext("This system is currently running "
1585 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
1587 flags
|= ZFS_ITER_RECURSE
;
1588 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1589 NULL
, NULL
, upgrade_list_callback
, &cb
);
1591 found
= cb
.cb_foundone
;
1592 cb
.cb_foundone
= B_FALSE
;
1593 cb
.cb_newer
= B_TRUE
;
1595 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1596 NULL
, NULL
, upgrade_list_callback
, &cb
);
1598 if (!cb
.cb_foundone
&& !found
) {
1599 (void) printf(gettext("All filesystems are "
1600 "formatted with the current version.\n"));
1608 * list [-rH] [-o property[,property]...] [-t type[,type]...]
1609 * [-s property [-s property]...] [-S property [-S property]...]
1612 * -r Recurse over all children
1613 * -H Scripted mode; elide headers and separate columns by tabs
1614 * -o Control which fields to display.
1615 * -t Control which object types to display.
1616 * -s Specify sort columns, descending order.
1617 * -S Specify sort columns, ascending order.
1619 * When given no arguments, lists all filesystems in the system.
1620 * Otherwise, list the specified datasets, optionally recursing down them if
1621 * '-r' is specified.
1623 typedef struct list_cbdata
{
1625 boolean_t cb_scripted
;
1626 zprop_list_t
*cb_proplist
;
1630 * Given a list of columns to display, output appropriate headers for each one.
1633 print_header(zprop_list_t
*pl
)
1635 char headerbuf
[ZFS_MAXPROPLEN
];
1638 boolean_t first
= B_TRUE
;
1639 boolean_t right_justify
;
1641 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1648 right_justify
= B_FALSE
;
1649 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1650 header
= zfs_prop_column_name(pl
->pl_prop
);
1651 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1653 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
1654 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
1655 headerbuf
[i
] = '\0';
1659 if (pl
->pl_next
== NULL
&& !right_justify
)
1660 (void) printf("%s", header
);
1661 else if (right_justify
)
1662 (void) printf("%*s", pl
->pl_width
, header
);
1664 (void) printf("%-*s", pl
->pl_width
, header
);
1667 (void) printf("\n");
1671 * Given a dataset and a list of fields, print out all the properties according
1672 * to the described layout.
1675 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
1677 boolean_t first
= B_TRUE
;
1678 char property
[ZFS_MAXPROPLEN
];
1679 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
1682 boolean_t right_justify
;
1685 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1688 (void) printf("\t");
1695 right_justify
= B_FALSE
;
1696 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1697 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
1698 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
1703 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1705 if (nvlist_lookup_nvlist(userprops
,
1706 pl
->pl_user_prop
, &propval
) != 0)
1709 verify(nvlist_lookup_string(propval
,
1710 ZPROP_VALUE
, &propstr
) == 0);
1713 width
= pl
->pl_width
;
1716 * If this is being called in scripted mode, or if this is the
1717 * last column and it is left-justified, don't include a width
1720 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
1721 (void) printf("%s", propstr
);
1722 else if (right_justify
)
1723 (void) printf("%*s", width
, propstr
);
1725 (void) printf("%-*s", width
, propstr
);
1728 (void) printf("\n");
1732 * Generic callback function to list a dataset or snapshot.
1735 list_callback(zfs_handle_t
*zhp
, void *data
)
1737 list_cbdata_t
*cbp
= data
;
1739 if (cbp
->cb_first
) {
1740 if (!cbp
->cb_scripted
)
1741 print_header(cbp
->cb_proplist
);
1742 cbp
->cb_first
= B_FALSE
;
1745 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
1751 zfs_do_list(int argc
, char **argv
)
1754 boolean_t scripted
= B_FALSE
;
1755 static char default_fields
[] =
1756 "name,used,available,referenced,mountpoint";
1757 int types
= ZFS_TYPE_DATASET
;
1758 boolean_t types_specified
= B_FALSE
;
1759 char *fields
= NULL
;
1760 list_cbdata_t cb
= { 0 };
1763 zfs_sort_column_t
*sortcol
= NULL
;
1764 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
1767 while ((c
= getopt(argc
, argv
, ":o:rt:Hs:S:")) != -1) {
1773 flags
|= ZFS_ITER_RECURSE
;
1779 if (zfs_add_sort_column(&sortcol
, optarg
,
1781 (void) fprintf(stderr
,
1782 gettext("invalid property '%s'\n"), optarg
);
1787 if (zfs_add_sort_column(&sortcol
, optarg
,
1789 (void) fprintf(stderr
,
1790 gettext("invalid property '%s'\n"), optarg
);
1796 types_specified
= B_TRUE
;
1797 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
1798 while (*optarg
!= '\0') {
1799 static char *type_subopts
[] = { "filesystem",
1800 "volume", "snapshot", "all", NULL
};
1802 switch (getsubopt(&optarg
, type_subopts
,
1805 types
|= ZFS_TYPE_FILESYSTEM
;
1808 types
|= ZFS_TYPE_VOLUME
;
1811 types
|= ZFS_TYPE_SNAPSHOT
;
1814 types
= ZFS_TYPE_DATASET
;
1818 (void) fprintf(stderr
,
1819 gettext("invalid type '%s'\n"),
1826 (void) fprintf(stderr
, gettext("missing argument for "
1827 "'%c' option\n"), optopt
);
1831 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1841 fields
= default_fields
;
1844 * If "-o space" and no types were specified, don't display snapshots.
1846 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
1847 types
&= ~ZFS_TYPE_SNAPSHOT
;
1850 * If the user specifies '-o all', the zprop_get_list() doesn't
1851 * normally include the name of the dataset. For 'zfs list', we always
1852 * want this property to be first.
1854 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1858 cb
.cb_scripted
= scripted
;
1859 cb
.cb_first
= B_TRUE
;
1861 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
1862 list_callback
, &cb
);
1864 zprop_free_list(cb
.cb_proplist
);
1865 zfs_free_sort_columns(sortcol
);
1867 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
1868 (void) printf(gettext("no datasets available\n"));
1874 * zfs rename <fs | snap | vol> <fs | snap | vol>
1875 * zfs rename -p <fs | vol> <fs | vol>
1876 * zfs rename -r <snap> <snap>
1878 * Renames the given dataset to another of the same type.
1880 * The '-p' flag creates all the non-existing ancestors of the target first.
1884 zfs_do_rename(int argc
, char **argv
)
1889 boolean_t recurse
= B_FALSE
;
1890 boolean_t parents
= B_FALSE
;
1893 while ((c
= getopt(argc
, argv
, "pr")) != -1) {
1903 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1912 /* check number of arguments */
1914 (void) fprintf(stderr
, gettext("missing source dataset "
1919 (void) fprintf(stderr
, gettext("missing target dataset "
1924 (void) fprintf(stderr
, gettext("too many arguments\n"));
1928 if (recurse
&& parents
) {
1929 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
1934 if (recurse
&& strchr(argv
[0], '@') == 0) {
1935 (void) fprintf(stderr
, gettext("source dataset for recursive "
1936 "rename must be a snapshot\n"));
1940 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
1941 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
1944 /* If we were asked and the name looks good, try to create ancestors. */
1945 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
1946 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
1951 ret
= (zfs_rename(zhp
, argv
[1], recurse
) != 0);
1960 * Promotes the given clone fs to be the parent
1964 zfs_do_promote(int argc
, char **argv
)
1970 if (argc
> 1 && argv
[1][0] == '-') {
1971 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1976 /* check number of arguments */
1978 (void) fprintf(stderr
, gettext("missing clone filesystem"
1983 (void) fprintf(stderr
, gettext("too many arguments\n"));
1987 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
1991 ret
= (zfs_promote(zhp
) != 0);
1999 * zfs rollback [-rRf] <snapshot>
2001 * -r Delete any intervening snapshots before doing rollback
2002 * -R Delete any snapshots and their clones
2003 * -f ignored for backwards compatability
2005 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2006 * since then and making it the active dataset. If more recent snapshots exist,
2007 * the command will complain unless the '-r' flag is given.
2009 typedef struct rollback_cbdata
{
2015 boolean_t cb_recurse
;
2016 boolean_t cb_dependent
;
2017 } rollback_cbdata_t
;
2020 * Report any snapshots more recent than the one specified. Used when '-r' is
2021 * not specified. We reuse this same callback for the snapshot dependents - if
2022 * 'cb_dependent' is set, then this is a dependent and we should report it
2023 * without checking the transaction group.
2026 rollback_check(zfs_handle_t
*zhp
, void *data
)
2028 rollback_cbdata_t
*cbp
= data
;
2030 if (cbp
->cb_doclones
) {
2035 if (!cbp
->cb_dependent
) {
2036 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
2037 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2038 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
2041 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
2042 (void) fprintf(stderr
, gettext("cannot "
2043 "rollback to '%s': more recent snapshots "
2046 (void) fprintf(stderr
, gettext("use '-r' to "
2047 "force deletion of the following "
2053 if (cbp
->cb_recurse
) {
2054 cbp
->cb_dependent
= B_TRUE
;
2055 if (zfs_iter_dependents(zhp
, B_TRUE
,
2056 rollback_check
, cbp
) != 0) {
2060 cbp
->cb_dependent
= B_FALSE
;
2062 (void) fprintf(stderr
, "%s\n",
2067 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
2068 (void) fprintf(stderr
, gettext("cannot rollback to "
2069 "'%s': clones of previous snapshots exist\n"),
2071 (void) fprintf(stderr
, gettext("use '-R' to "
2072 "force deletion of the following clones and "
2078 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
2086 zfs_do_rollback(int argc
, char **argv
)
2090 boolean_t force
= B_FALSE
;
2091 rollback_cbdata_t cb
= { 0 };
2092 zfs_handle_t
*zhp
, *snap
;
2093 char parentname
[ZFS_MAXNAMELEN
];
2097 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
2110 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2119 /* check number of arguments */
2121 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2125 (void) fprintf(stderr
, gettext("too many arguments\n"));
2129 /* open the snapshot */
2130 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
2133 /* open the parent dataset */
2134 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
2135 verify((delim
= strrchr(parentname
, '@')) != NULL
);
2137 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
2143 * Check for more recent snapshots and/or clones based on the presence
2146 cb
.cb_target
= argv
[0];
2147 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
2148 cb
.cb_first
= B_TRUE
;
2150 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
2153 if ((ret
= cb
.cb_error
) != 0)
2157 * Rollback parent to the given snapshot.
2159 ret
= zfs_rollback(zhp
, snap
, force
);
2172 * zfs set property=value { fs | snap | vol } ...
2174 * Sets the given property for all datasets specified on the command line.
2176 typedef struct set_cbdata
{
2182 set_callback(zfs_handle_t
*zhp
, void *data
)
2184 set_cbdata_t
*cbp
= data
;
2186 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
2187 switch (libzfs_errno(g_zfs
)) {
2188 case EZFS_MOUNTFAILED
:
2189 (void) fprintf(stderr
, gettext("property may be set "
2190 "but unable to remount filesystem\n"));
2192 case EZFS_SHARENFSFAILED
:
2193 (void) fprintf(stderr
, gettext("property may be set "
2194 "but unable to reshare filesystem\n"));
2203 zfs_do_set(int argc
, char **argv
)
2208 /* check for options */
2209 if (argc
> 1 && argv
[1][0] == '-') {
2210 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2215 /* check number of arguments */
2217 (void) fprintf(stderr
, gettext("missing property=value "
2222 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2226 /* validate property=value argument */
2227 cb
.cb_propname
= argv
[1];
2228 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
2229 (cb
.cb_value
[1] == '\0')) {
2230 (void) fprintf(stderr
, gettext("missing value in "
2231 "property=value argument\n"));
2235 *cb
.cb_value
= '\0';
2238 if (*cb
.cb_propname
== '\0') {
2239 (void) fprintf(stderr
,
2240 gettext("missing property in property=value argument\n"));
2244 ret
= zfs_for_each(argc
- 2, argv
+ 2, NULL
,
2245 ZFS_TYPE_DATASET
, NULL
, NULL
, set_callback
, &cb
);
2251 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2253 * Creates a snapshot with the given name. While functionally equivalent to
2254 * 'zfs create', it is a separate command to differentiate intent.
2257 zfs_do_snapshot(int argc
, char **argv
)
2259 boolean_t recursive
= B_FALSE
;
2264 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
2265 (void) fprintf(stderr
, gettext("internal error: "
2266 "out of memory\n"));
2271 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
2274 if (parseprop(props
))
2281 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2290 /* check number of arguments */
2292 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2296 (void) fprintf(stderr
, gettext("too many arguments\n"));
2300 ret
= zfs_snapshot(g_zfs
, argv
[0], recursive
, props
);
2302 if (ret
&& recursive
)
2303 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
2313 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
2314 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
2316 * Send a backup stream to stdout.
2319 zfs_do_send(int argc
, char **argv
)
2321 char *fromname
= NULL
;
2322 char *toname
= NULL
;
2325 boolean_t doall
= B_FALSE
;
2326 boolean_t replicate
= B_FALSE
;
2327 boolean_t fromorigin
= B_FALSE
;
2328 boolean_t verbose
= B_FALSE
;
2332 while ((c
= getopt(argc
, argv
, ":i:I:Rv")) != -1) {
2352 (void) fprintf(stderr
, gettext("missing argument for "
2353 "'%c' option\n"), optopt
);
2357 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2366 /* check number of arguments */
2368 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2372 (void) fprintf(stderr
, gettext("too many arguments\n"));
2376 if (isatty(STDOUT_FILENO
)) {
2377 (void) fprintf(stderr
,
2378 gettext("Error: Stream can not be written to a terminal.\n"
2379 "You must redirect standard output.\n"));
2383 cp
= strchr(argv
[0], '@');
2385 (void) fprintf(stderr
,
2386 gettext("argument must be a snapshot\n"));
2391 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2396 * If they specified the full path to the snapshot, chop off
2397 * everything except the short name of the snapshot, but special
2398 * case if they specify the origin.
2400 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
2401 char origin
[ZFS_MAXNAMELEN
];
2404 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
2405 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
2407 if (strcmp(origin
, fromname
) == 0) {
2409 fromorigin
= B_TRUE
;
2412 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
2413 (void) fprintf(stderr
,
2414 gettext("incremental source must be "
2415 "in same filesystem\n"));
2419 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
2420 (void) fprintf(stderr
,
2421 gettext("invalid incremental source\n"));
2427 if (replicate
&& fromname
== NULL
)
2430 err
= zfs_send(zhp
, fromname
, toname
, replicate
, doall
, fromorigin
,
2431 verbose
, STDOUT_FILENO
);
2438 * zfs receive [-dnvF] <fs@snap>
2440 * Restore a backup stream from stdin.
2443 zfs_do_receive(int argc
, char **argv
)
2448 bzero(&flags
, sizeof (recvflags_t
));
2450 while ((c
= getopt(argc
, argv
, ":dnvF")) != -1) {
2453 flags
.isprefix
= B_TRUE
;
2456 flags
.dryrun
= B_TRUE
;
2459 flags
.verbose
= B_TRUE
;
2462 flags
.force
= B_TRUE
;
2465 (void) fprintf(stderr
, gettext("missing argument for "
2466 "'%c' option\n"), optopt
);
2470 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2479 /* check number of arguments */
2481 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2485 (void) fprintf(stderr
, gettext("too many arguments\n"));
2489 if (isatty(STDIN_FILENO
)) {
2490 (void) fprintf(stderr
,
2491 gettext("Error: Backup stream can not be read "
2492 "from a terminal.\n"
2493 "You must redirect standard input.\n"));
2497 err
= zfs_receive(g_zfs
, argv
[0], flags
, STDIN_FILENO
, NULL
);
2502 typedef struct allow_cb
{
2504 size_t a_treeoffset
;
2508 zfs_print_perms(avl_tree_t
*tree
)
2510 zfs_perm_node_t
*permnode
;
2512 permnode
= avl_first(tree
);
2513 while (permnode
!= NULL
) {
2514 (void) printf("%s", permnode
->z_pname
);
2515 permnode
= AVL_NEXT(tree
, permnode
);
2519 (void) printf("\n");
2524 * Iterate over user/groups/everyone/... and the call perm_iter
2525 * function to print actual permission when tree has >0 nodes.
2528 zfs_iter_perms(avl_tree_t
*tree
, const char *banner
, allow_cb_t
*cb
)
2530 zfs_allow_node_t
*item
;
2533 item
= avl_first(tree
);
2535 ptree
= (void *)((char *)item
+ cb
->a_treeoffset
);
2536 if (avl_numnodes(ptree
)) {
2537 if (cb
->a_permcnt
++ == 0)
2538 (void) printf("%s\n", banner
);
2539 (void) printf("\t%s", item
->z_key
);
2541 * Avoid an extra space being printed
2542 * for "everyone" which is keyed with a null
2545 if (item
->z_key
[0] != '\0')
2547 zfs_print_perms(ptree
);
2549 item
= AVL_NEXT(tree
, item
);
2553 #define LINES "-------------------------------------------------------------\n"
2555 zfs_print_allows(char *ds
)
2557 zfs_allow_t
*curperms
, *perms
;
2559 allow_cb_t allowcb
= { 0 };
2560 char banner
[MAXPATHLEN
];
2565 if (strrchr(ds
, '@')) {
2566 (void) fprintf(stderr
, gettext("Snapshots don't have 'allow'"
2570 if ((zhp
= zfs_open(g_zfs
, ds
, ZFS_TYPE_DATASET
)) == NULL
)
2573 if (zfs_perm_get(zhp
, &perms
)) {
2574 (void) fprintf(stderr
,
2575 gettext("Failed to retrieve 'allows' on %s\n"), ds
);
2583 (void) printf("%s", LINES
);
2584 for (curperms
= perms
; curperms
; curperms
= curperms
->z_next
) {
2586 (void) snprintf(banner
, sizeof (banner
),
2587 "Permission sets on (%s)", curperms
->z_setpoint
);
2588 allowcb
.a_treeoffset
=
2589 offsetof(zfs_allow_node_t
, z_localdescend
);
2590 allowcb
.a_permcnt
= 0;
2591 zfs_iter_perms(&curperms
->z_sets
, banner
, &allowcb
);
2593 (void) snprintf(banner
, sizeof (banner
),
2594 "Create time permissions on (%s)", curperms
->z_setpoint
);
2595 allowcb
.a_treeoffset
=
2596 offsetof(zfs_allow_node_t
, z_localdescend
);
2597 allowcb
.a_permcnt
= 0;
2598 zfs_iter_perms(&curperms
->z_crperms
, banner
, &allowcb
);
2601 (void) snprintf(banner
, sizeof (banner
),
2602 "Local permissions on (%s)", curperms
->z_setpoint
);
2603 allowcb
.a_treeoffset
= offsetof(zfs_allow_node_t
, z_local
);
2604 allowcb
.a_permcnt
= 0;
2605 zfs_iter_perms(&curperms
->z_user
, banner
, &allowcb
);
2606 zfs_iter_perms(&curperms
->z_group
, banner
, &allowcb
);
2607 zfs_iter_perms(&curperms
->z_everyone
, banner
, &allowcb
);
2609 (void) snprintf(banner
, sizeof (banner
),
2610 "Descendent permissions on (%s)", curperms
->z_setpoint
);
2611 allowcb
.a_treeoffset
= offsetof(zfs_allow_node_t
, z_descend
);
2612 allowcb
.a_permcnt
= 0;
2613 zfs_iter_perms(&curperms
->z_user
, banner
, &allowcb
);
2614 zfs_iter_perms(&curperms
->z_group
, banner
, &allowcb
);
2615 zfs_iter_perms(&curperms
->z_everyone
, banner
, &allowcb
);
2617 (void) snprintf(banner
, sizeof (banner
),
2618 "Local+Descendent permissions on (%s)",
2619 curperms
->z_setpoint
);
2620 allowcb
.a_treeoffset
=
2621 offsetof(zfs_allow_node_t
, z_localdescend
);
2622 allowcb
.a_permcnt
= 0;
2623 zfs_iter_perms(&curperms
->z_user
, banner
, &allowcb
);
2624 zfs_iter_perms(&curperms
->z_group
, banner
, &allowcb
);
2625 zfs_iter_perms(&curperms
->z_everyone
, banner
, &allowcb
);
2627 (void) printf("%s", LINES
);
2629 zfs_free_allows(perms
);
2633 #define ALLOWOPTIONS "ldcsu:g:e"
2634 #define UNALLOWOPTIONS "ldcsu:g:er"
2637 * Validate options, and build necessary datastructure to display/remove/add
2639 * Returns 0 - If permissions should be added/removed
2640 * Returns 1 - If permissions should be displayed.
2641 * Returns -1 - on failure
2644 parse_allow_args(int *argc
, char **argv
[], boolean_t unallow
,
2645 char **ds
, int *recurse
, nvlist_t
**zperms
)
2648 char *options
= unallow
? UNALLOWOPTIONS
: ALLOWOPTIONS
;
2649 zfs_deleg_inherit_t deleg_type
= ZFS_DELEG_NONE
;
2650 zfs_deleg_who_type_t who_type
= ZFS_DELEG_WHO_UNKNOWN
;
2655 while ((c
= getopt(*argc
, *argv
, options
)) != -1) {
2658 if (who_type
== ZFS_DELEG_CREATE
||
2659 who_type
== ZFS_DELEG_NAMED_SET
)
2662 deleg_type
|= ZFS_DELEG_PERM_LOCAL
;
2665 if (who_type
== ZFS_DELEG_CREATE
||
2666 who_type
== ZFS_DELEG_NAMED_SET
)
2669 deleg_type
|= ZFS_DELEG_PERM_DESCENDENT
;
2675 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2679 who_type
= ZFS_DELEG_CREATE
;
2682 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2686 who_type
= ZFS_DELEG_NAMED_SET
;
2689 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2691 who_type
= ZFS_DELEG_USER
;
2695 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2697 who_type
= ZFS_DELEG_GROUP
;
2701 if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
)
2703 who_type
= ZFS_DELEG_EVERYONE
;
2711 if (deleg_type
== 0)
2712 deleg_type
= ZFS_DELEG_PERM_LOCALDESCENDENT
;
2717 if (unallow
== B_FALSE
&& *argc
== 1) {
2719 * Only print permissions if no options were processed
2728 * initialize variables for zfs_build_perms based on number
2730 * 3 arguments ==> zfs [un]allow joe perm,perm,perm <dataset> or
2731 * zfs [un]allow -s @set1 perm,perm <dataset>
2732 * 2 arguments ==> zfs [un]allow -c perm,perm <dataset> or
2733 * zfs [un]allow -u|-g <name> perm <dataset> or
2734 * zfs [un]allow -e perm,perm <dataset>
2735 * zfs unallow joe <dataset>
2736 * zfs unallow -s @set1 <dataset>
2737 * 1 argument ==> zfs [un]allow -e <dataset> or
2738 * zfs [un]allow -c <dataset>
2748 * advance argc/argv for do_allow cases.
2749 * for do_allow case make sure who have a know who type
2750 * and its not a permission set.
2752 if (unallow
== B_TRUE
) {
2755 } else if (who_type
!= ZFS_DELEG_WHO_UNKNOWN
&&
2756 who_type
!= ZFS_DELEG_NAMED_SET
)
2761 if (unallow
== B_TRUE
&& (who_type
== ZFS_DELEG_EVERYONE
||
2762 who_type
== ZFS_DELEG_CREATE
|| who
!= NULL
)) {
2766 if (unallow
== B_FALSE
&&
2767 (who_type
== ZFS_DELEG_WHO_UNKNOWN
||
2768 who_type
== ZFS_DELEG_NAMED_SET
))
2770 else if (who_type
== ZFS_DELEG_WHO_UNKNOWN
||
2771 who_type
== ZFS_DELEG_NAMED_SET
)
2773 else if (who_type
!= ZFS_DELEG_NAMED_SET
)
2777 if (unallow
== B_TRUE
) {
2784 if (unallow
== B_FALSE
)
2786 if (who
== NULL
&& who_type
!= ZFS_DELEG_CREATE
&&
2787 who_type
!= ZFS_DELEG_EVERYONE
)
2796 if (strrchr(*ds
, '@')) {
2797 (void) fprintf(stderr
,
2798 gettext("Can't set or remove 'allow' permissions "
2799 "on snapshots.\n"));
2803 if ((zhp
= zfs_open(g_zfs
, *ds
, ZFS_TYPE_DATASET
)) == NULL
)
2806 if ((zfs_build_perms(zhp
, who
, perms
,
2807 who_type
, deleg_type
, zperms
)) != 0) {
2816 zfs_do_allow(int argc
, char **argv
)
2819 nvlist_t
*zperms
= NULL
;
2824 if ((ret
= parse_allow_args(&argc
, &argv
, B_FALSE
, &ds
,
2825 &unused
, &zperms
)) == -1)
2829 return (zfs_print_allows(argv
[0]));
2831 if ((zhp
= zfs_open(g_zfs
, ds
, ZFS_TYPE_DATASET
)) == NULL
)
2834 if (zfs_perm_set(zhp
, zperms
)) {
2836 nvlist_free(zperms
);
2839 nvlist_free(zperms
);
2846 unallow_callback(zfs_handle_t
*zhp
, void *data
)
2848 nvlist_t
*nvp
= (nvlist_t
*)data
;
2851 error
= zfs_perm_remove(zhp
, nvp
);
2853 (void) fprintf(stderr
, gettext("Failed to remove permissions "
2854 "on %s\n"), zfs_get_name(zhp
));
2860 zfs_do_unallow(int argc
, char **argv
)
2862 int recurse
= B_FALSE
;
2865 nvlist_t
*zperms
= NULL
;
2868 if (parse_allow_args(&argc
, &argv
, B_TRUE
,
2869 &ds
, &recurse
, &zperms
) == -1)
2873 flags
|= ZFS_ITER_RECURSE
;
2874 error
= zfs_for_each(argc
, argv
, flags
,
2875 ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_VOLUME
, NULL
,
2876 NULL
, unallow_callback
, (void *)zperms
);
2879 nvlist_free(zperms
);
2884 typedef struct get_all_cbdata
{
2885 zfs_handle_t
**cb_handles
;
2889 boolean_t cb_verbose
;
2892 #define CHECK_SPINNER 30
2893 #define SPINNER_TIME 3 /* seconds */
2894 #define MOUNT_TIME 5 /* seconds */
2897 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
2899 static char spin
[] = { '-', '\\', '|', '/' };
2900 static int spinval
= 0;
2901 static int spincheck
= 0;
2902 static time_t last_spin_time
= (time_t)0;
2903 get_all_cbdata_t
*cbp
= data
;
2904 zfs_type_t type
= zfs_get_type(zhp
);
2906 if (cbp
->cb_verbose
) {
2907 if (--spincheck
< 0) {
2908 time_t now
= time(NULL
);
2909 if (last_spin_time
+ SPINNER_TIME
< now
) {
2910 (void) printf("\b%c", spin
[spinval
++ % 4]);
2911 (void) fflush(stdout
);
2912 last_spin_time
= now
;
2914 spincheck
= CHECK_SPINNER
;
2919 * Interate over any nested datasets.
2921 if (type
== ZFS_TYPE_FILESYSTEM
&&
2922 zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
2928 * Skip any datasets whose type does not match.
2930 if ((type
& cbp
->cb_types
) == 0) {
2935 if (cbp
->cb_alloc
== cbp
->cb_used
) {
2936 zfs_handle_t
**handles
;
2938 if (cbp
->cb_alloc
== 0)
2943 handles
= safe_malloc(cbp
->cb_alloc
* sizeof (void *));
2945 if (cbp
->cb_handles
) {
2946 bcopy(cbp
->cb_handles
, handles
,
2947 cbp
->cb_used
* sizeof (void *));
2948 free(cbp
->cb_handles
);
2951 cbp
->cb_handles
= handles
;
2954 cbp
->cb_handles
[cbp
->cb_used
++] = zhp
;
2960 get_all_datasets(uint_t types
, zfs_handle_t
***dslist
, size_t *count
,
2963 get_all_cbdata_t cb
= { 0 };
2964 cb
.cb_types
= types
;
2965 cb
.cb_verbose
= verbose
;
2968 (void) printf("%s: *", gettext("Reading ZFS config"));
2969 (void) fflush(stdout
);
2972 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
2974 *dslist
= cb
.cb_handles
;
2975 *count
= cb
.cb_used
;
2978 (void) printf("\b%s\n", gettext("done."));
2983 dataset_cmp(const void *a
, const void *b
)
2985 zfs_handle_t
**za
= (zfs_handle_t
**)a
;
2986 zfs_handle_t
**zb
= (zfs_handle_t
**)b
;
2987 char mounta
[MAXPATHLEN
];
2988 char mountb
[MAXPATHLEN
];
2989 boolean_t gota
, gotb
;
2991 if ((gota
= (zfs_get_type(*za
) == ZFS_TYPE_FILESYSTEM
)) != 0)
2992 verify(zfs_prop_get(*za
, ZFS_PROP_MOUNTPOINT
, mounta
,
2993 sizeof (mounta
), NULL
, NULL
, 0, B_FALSE
) == 0);
2994 if ((gotb
= (zfs_get_type(*zb
) == ZFS_TYPE_FILESYSTEM
)) != 0)
2995 verify(zfs_prop_get(*zb
, ZFS_PROP_MOUNTPOINT
, mountb
,
2996 sizeof (mountb
), NULL
, NULL
, 0, B_FALSE
) == 0);
2999 return (strcmp(mounta
, mountb
));
3006 return (strcmp(zfs_get_name(a
), zfs_get_name(b
)));
3010 * Generic callback for sharing or mounting filesystems. Because the code is so
3011 * similar, we have a common function with an extra parameter to determine which
3012 * mode we are using.
3014 #define OP_SHARE 0x1
3015 #define OP_MOUNT 0x2
3018 * Share or mount a dataset.
3021 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
3022 boolean_t
explicit, const char *options
)
3024 char mountpoint
[ZFS_MAXPROPLEN
];
3025 char shareopts
[ZFS_MAXPROPLEN
];
3026 char smbshareopts
[ZFS_MAXPROPLEN
];
3027 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
3029 uint64_t zoned
, canmount
;
3030 zfs_type_t type
= zfs_get_type(zhp
);
3031 boolean_t shared_nfs
, shared_smb
;
3033 assert(type
& (ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
));
3035 if (type
== ZFS_TYPE_FILESYSTEM
) {
3037 * Check to make sure we can mount/share this dataset. If we
3038 * are in the global zone and the filesystem is exported to a
3039 * local zone, or if we are in a local zone and the
3040 * filesystem is not exported, then it is an error.
3042 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3044 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
3048 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3049 "dataset is exported to a local zone\n"), cmdname
,
3053 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
3057 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3058 "permission denied\n"), cmdname
,
3064 * Ignore any filesystems which don't apply to us. This
3065 * includes those with a legacy mountpoint, or those with
3066 * legacy share options.
3068 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
3069 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
3070 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
3071 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3072 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
3073 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3074 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
3076 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
3077 strcmp(smbshareopts
, "off") == 0) {
3081 (void) fprintf(stderr
, gettext("cannot share '%s': "
3082 "legacy share\n"), zfs_get_name(zhp
));
3083 (void) fprintf(stderr
, gettext("use share(1M) to "
3084 "share this filesystem\n"));
3089 * We cannot share or mount legacy filesystems. If the
3090 * shareopts is non-legacy but the mountpoint is legacy, we
3091 * treat it as a legacy share.
3093 if (strcmp(mountpoint
, "legacy") == 0) {
3097 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3098 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
3099 (void) fprintf(stderr
, gettext("use %s(1M) to "
3100 "%s this filesystem\n"), cmdname
, cmdname
);
3104 if (strcmp(mountpoint
, "none") == 0) {
3108 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
3109 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
3114 * canmount explicit outcome
3115 * on no pass through
3116 * on yes pass through
3118 * off yes display error, return 1
3119 * noauto no return 0
3120 * noauto yes pass through
3122 if (canmount
== ZFS_CANMOUNT_OFF
) {
3126 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3127 "'canmount' property is set to 'off'\n"), cmdname
,
3130 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
3135 * At this point, we have verified that the mountpoint and/or
3136 * shareopts are appropriate for auto management. If the
3137 * filesystem is already mounted or shared, return (failing
3138 * for explicit requests); otherwise mount or share the
3144 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
3145 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
3147 if (shared_nfs
&& shared_smb
||
3148 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
3149 strcmp(smbshareopts
, "off") == 0) ||
3150 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
3151 strcmp(shareopts
, "off") == 0)) {
3155 (void) fprintf(stderr
, gettext("cannot share "
3156 "'%s': filesystem already shared\n"),
3161 if (!zfs_is_mounted(zhp
, NULL
) &&
3162 zfs_mount(zhp
, NULL
, 0) != 0)
3165 if (protocol
== NULL
) {
3166 if (zfs_shareall(zhp
) != 0)
3168 } else if (strcmp(protocol
, "nfs") == 0) {
3169 if (zfs_share_nfs(zhp
))
3171 } else if (strcmp(protocol
, "smb") == 0) {
3172 if (zfs_share_smb(zhp
))
3175 (void) fprintf(stderr
, gettext("cannot share "
3176 "'%s': invalid share type '%s' "
3178 zfs_get_name(zhp
), protocol
);
3185 if (options
== NULL
)
3186 mnt
.mnt_mntopts
= "";
3188 mnt
.mnt_mntopts
= (char *)options
;
3190 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
3191 zfs_is_mounted(zhp
, NULL
)) {
3195 (void) fprintf(stderr
, gettext("cannot mount "
3196 "'%s': filesystem already mounted\n"),
3201 if (zfs_mount(zhp
, options
, flags
) != 0)
3206 assert(op
== OP_SHARE
);
3209 * Ignore any volumes that aren't shared.
3211 verify(zfs_prop_get(zhp
, ZFS_PROP_SHAREISCSI
, shareopts
,
3212 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3214 if (strcmp(shareopts
, "off") == 0) {
3218 (void) fprintf(stderr
, gettext("cannot share '%s': "
3219 "'shareiscsi' property not set\n"),
3221 (void) fprintf(stderr
, gettext("set 'shareiscsi' "
3222 "property or use iscsitadm(1M) to share this "
3227 if (zfs_is_shared_iscsi(zhp
)) {
3231 (void) fprintf(stderr
, gettext("cannot share "
3232 "'%s': volume already shared\n"),
3237 if (zfs_share_iscsi(zhp
) != 0)
3245 * Reports progress in the form "(current/total)". Not thread-safe.
3248 report_mount_progress(int current
, int total
)
3251 static char *reverse
= "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
3252 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
3253 static time_t last_progress_time
;
3254 time_t now
= time(NULL
);
3256 /* report 1..n instead of 0..n-1 */
3259 /* display header if we're here for the first time */
3261 (void) printf(gettext("Mounting ZFS filesystems: "));
3263 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
3264 /* too soon to report again */
3268 last_progress_time
= now
;
3270 /* back up to prepare for overwriting */
3272 (void) printf("%*.*s", len
, len
, reverse
);
3274 /* We put a newline at the end if this is the last one. */
3275 len
= printf("(%d/%d)%s", current
, total
, current
== total
? "\n" : "");
3276 (void) fflush(stdout
);
3280 append_options(char *mntopts
, char *newopts
)
3282 int len
= strlen(mntopts
);
3284 /* original length plus new string to append plus 1 for the comma */
3285 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
3286 (void) fprintf(stderr
, gettext("the opts argument for "
3287 "'%c' option is too long (more than %d chars)\n"),
3288 "-o", MNT_LINE_MAX
);
3293 mntopts
[len
++] = ',';
3295 (void) strcpy(&mntopts
[len
], newopts
);
3299 share_mount(int op
, int argc
, char **argv
)
3302 boolean_t verbose
= B_FALSE
;
3304 char *options
= NULL
;
3305 int types
, flags
= 0;
3308 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
3318 if (*optarg
== '\0') {
3319 (void) fprintf(stderr
, gettext("empty mount "
3320 "options (-o) specified\n"));
3324 if (options
== NULL
)
3325 options
= safe_malloc(MNT_LINE_MAX
+ 1);
3327 /* option validation is done later */
3328 append_options(options
, optarg
);
3332 flags
|= MS_OVERLAY
;
3335 (void) fprintf(stderr
, gettext("missing argument for "
3336 "'%c' option\n"), optopt
);
3340 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3349 /* check number of arguments */
3351 zfs_handle_t
**dslist
= NULL
;
3352 size_t i
, count
= 0;
3353 char *protocol
= NULL
;
3355 if (op
== OP_MOUNT
) {
3356 types
= ZFS_TYPE_FILESYSTEM
;
3357 } else if (argc
> 0) {
3358 if (strcmp(argv
[0], "nfs") == 0 ||
3359 strcmp(argv
[0], "smb") == 0) {
3360 types
= ZFS_TYPE_FILESYSTEM
;
3361 } else if (strcmp(argv
[0], "iscsi") == 0) {
3362 types
= ZFS_TYPE_VOLUME
;
3364 (void) fprintf(stderr
, gettext("share type "
3365 "must be 'nfs', 'smb' or 'iscsi'\n"));
3372 types
= ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
;
3376 (void) fprintf(stderr
, gettext("too many arguments\n"));
3380 get_all_datasets(types
, &dslist
, &count
, verbose
);
3385 qsort(dslist
, count
, sizeof (void *), dataset_cmp
);
3387 for (i
= 0; i
< count
; i
++) {
3389 report_mount_progress(i
, count
);
3391 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
3392 B_FALSE
, options
) != 0)
3394 zfs_close(dslist
[i
]);
3398 } else if (argc
== 0) {
3399 struct statvfs
*sfs
;
3402 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
3403 (void) fprintf(stderr
, gettext("missing filesystem "
3404 "argument (specify -a for all)\n"));
3409 * When mount is given no arguments, go through /etc/mnttab and
3410 * display any active ZFS mounts. We hide any snapshots, since
3411 * they are controlled automatically.
3413 if ((n
= getmntinfo(&sfs
, MNT_WAIT
)) == 0) {
3414 fprintf(stderr
, "getmntinfo(): %s\n", strerror(errno
));
3417 for (i
= 0; i
< n
; i
++) {
3418 if (strcmp(sfs
[i
].f_fstypename
, MNTTYPE_ZFS
) != 0 ||
3419 strchr(sfs
[i
].f_mntfromname
, '@') != NULL
)
3422 (void) printf("%-30s %s\n", sfs
[i
].f_mntfromname
,
3423 sfs
[i
].f_mntonname
);
3429 types
= ZFS_TYPE_FILESYSTEM
;
3431 types
|= ZFS_TYPE_VOLUME
;
3434 (void) fprintf(stderr
,
3435 gettext("too many arguments\n"));
3439 if ((zhp
= zfs_open(g_zfs
, argv
[0], types
)) == NULL
) {
3442 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
3452 * zfs mount -a [nfs | iscsi]
3453 * zfs mount filesystem
3455 * Mount all filesystems, or mount the given filesystem.
3458 zfs_do_mount(int argc
, char **argv
)
3460 return (share_mount(OP_MOUNT
, argc
, argv
));
3464 * zfs share -a [nfs | iscsi | smb]
3465 * zfs share filesystem
3467 * Share all filesystems, or share the given filesystem.
3470 zfs_do_share(int argc
, char **argv
)
3472 return (share_mount(OP_SHARE
, argc
, argv
));
3475 typedef struct unshare_unmount_node
{
3476 zfs_handle_t
*un_zhp
;
3478 uu_avl_node_t un_avlnode
;
3479 } unshare_unmount_node_t
;
3483 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
3485 const unshare_unmount_node_t
*l
= larg
;
3486 const unshare_unmount_node_t
*r
= rarg
;
3488 return (strcmp(l
->un_mountp
, r
->un_mountp
));
3492 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3493 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3494 * and unmount it appropriately.
3497 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
3501 struct stat64 statbuf
;
3502 struct mnttab entry
, search
= { 0 };
3503 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
3507 * Search for the path in /etc/mnttab. Rather than looking for the
3508 * specific path, which can be fooled by non-standard paths (i.e. ".."
3509 * or "//"), we stat() the path and search for the corresponding
3510 * (major,minor) device pair.
3512 if (stat64(path
, &statbuf
) != 0) {
3513 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3514 cmdname
, path
, strerror(errno
));
3517 path_inode
= statbuf
.st_ino
;
3520 * Search for the given (major,minor) pair in the mount table.
3522 rewind(mnttab_file
);
3523 search
.mnt_mountp
= path
;
3524 if ((ret
= getmntany(mnttab_file
, &entry
, &search
)) == 0) {
3525 if (op
== OP_SHARE
) {
3526 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
3527 "currently mounted\n"), cmdname
, path
);
3530 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
3532 if ((ret
= umount2(path
, flags
)) != 0)
3533 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
3538 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
3539 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
3540 "filesystem\n"), cmdname
, path
);
3544 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3545 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3549 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
3550 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3551 cmdname
, path
, strerror(errno
));
3553 } else if (statbuf
.st_ino
!= path_inode
) {
3554 (void) fprintf(stderr
, gettext("cannot "
3555 "%s '%s': not a mountpoint\n"), cmdname
, path
);
3559 if (op
== OP_SHARE
) {
3560 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3561 char smbshare_prop
[ZFS_MAXPROPLEN
];
3563 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
3564 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3565 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
3566 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3568 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3569 strcmp(smbshare_prop
, "off") == 0) {
3570 (void) fprintf(stderr
, gettext("cannot unshare "
3571 "'%s': legacy share\n"), path
);
3572 (void) fprintf(stderr
, gettext("use "
3573 "unshare(1M) to unshare this filesystem\n"));
3574 } else if (!zfs_is_shared(zhp
)) {
3575 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
3576 "not currently shared\n"), path
);
3578 ret
= zfs_unshareall_bypath(zhp
, path
);
3581 char mtpt_prop
[ZFS_MAXPROPLEN
];
3583 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
3584 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3587 ret
= zfs_unmount(zhp
, NULL
, flags
);
3588 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
3589 (void) fprintf(stderr
, gettext("cannot unmount "
3590 "'%s': legacy mountpoint\n"),
3592 (void) fprintf(stderr
, gettext("use umount(1M) "
3593 "to unmount this filesystem\n"));
3595 ret
= zfs_unmountall(zhp
, flags
);
3606 * Generic callback for unsharing or unmounting a filesystem.
3609 unshare_unmount(int op
, int argc
, char **argv
)
3616 char nfsiscsi_mnt_prop
[ZFS_MAXPROPLEN
];
3617 char sharesmb
[ZFS_MAXPROPLEN
];
3620 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
3629 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3640 * We could make use of zfs_for_each() to walk all datasets in
3641 * the system, but this would be very inefficient, especially
3642 * since we would have to linearly search /etc/mnttab for each
3643 * one. Instead, do one pass through /etc/mnttab looking for
3644 * zfs entries and call zfs_unmount() for each one.
3646 * Things get a little tricky if the administrator has created
3647 * mountpoints beneath other ZFS filesystems. In this case, we
3648 * have to unmount the deepest filesystems first. To accomplish
3649 * this, we place all the mountpoints in an AVL tree sorted by
3650 * the special type (dataset name), and walk the result in
3651 * reverse to make sure to get any snapshots first.
3653 struct statvfs
*sfs
;
3655 uu_avl_pool_t
*pool
;
3657 unshare_unmount_node_t
*node
;
3659 uu_avl_walk_t
*walk
;
3662 (void) fprintf(stderr
, gettext("too many arguments\n"));
3666 if ((pool
= uu_avl_pool_create("unmount_pool",
3667 sizeof (unshare_unmount_node_t
),
3668 offsetof(unshare_unmount_node_t
, un_avlnode
),
3669 unshare_unmount_compare
,
3670 UU_DEFAULT
)) == NULL
) {
3671 (void) fprintf(stderr
, gettext("internal error: "
3672 "out of memory\n"));
3676 if ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
) {
3677 (void) fprintf(stderr
, gettext("internal error: "
3678 "out of memory\n"));
3682 rewind(mnttab_file
);
3683 if ((n
= getmntinfo(&sfs
, MNT_WAIT
)) == 0) {
3684 (void) fprintf(stderr
, gettext("internal error: "
3685 "getmntinfo() failed\n"));
3688 for (i
= 0; i
< n
; i
++) {
3690 /* ignore non-ZFS entries */
3691 if (strcmp(sfs
[i
].f_fstypename
, MNTTYPE_ZFS
) != 0)
3694 /* ignore snapshots */
3695 if (strchr(sfs
[i
].f_mntfromname
, '@') != NULL
)
3698 if ((zhp
= zfs_open(g_zfs
, sfs
[i
].f_mntfromname
,
3699 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3706 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3708 sizeof (nfsiscsi_mnt_prop
),
3709 NULL
, NULL
, 0, B_FALSE
) == 0);
3710 if (strcmp(nfsiscsi_mnt_prop
, "off") != 0)
3712 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3714 sizeof (nfsiscsi_mnt_prop
),
3715 NULL
, NULL
, 0, B_FALSE
) == 0);
3716 if (strcmp(nfsiscsi_mnt_prop
, "off") == 0)
3720 /* Ignore legacy mounts */
3721 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
3723 sizeof (nfsiscsi_mnt_prop
),
3724 NULL
, NULL
, 0, B_FALSE
) == 0);
3725 if (strcmp(nfsiscsi_mnt_prop
, "legacy") == 0)
3727 /* Ignore canmount=noauto mounts */
3728 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
3729 ZFS_CANMOUNT_NOAUTO
)
3735 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
3738 if ((node
->un_mountp
= strdup(sfs
[i
].f_mntonname
)) ==
3740 (void) fprintf(stderr
, gettext("internal error:"
3741 " out of memory\n"));
3745 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
3747 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
3748 uu_avl_insert(tree
, node
, idx
);
3750 zfs_close(node
->un_zhp
);
3751 free(node
->un_mountp
);
3757 * Walk the AVL tree in reverse, unmounting each filesystem and
3758 * removing it from the AVL tree in the process.
3760 if ((walk
= uu_avl_walk_start(tree
,
3761 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
) {
3762 (void) fprintf(stderr
,
3763 gettext("internal error: out of memory"));
3767 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3768 uu_avl_remove(tree
, node
);
3772 if (zfs_unshareall_bypath(node
->un_zhp
,
3773 node
->un_mountp
) != 0)
3778 if (zfs_unmount(node
->un_zhp
,
3779 node
->un_mountp
, flags
) != 0)
3784 zfs_close(node
->un_zhp
);
3785 free(node
->un_mountp
);
3789 uu_avl_walk_end(walk
);
3790 uu_avl_destroy(tree
);
3791 uu_avl_pool_destroy(pool
);
3793 if (op
== OP_SHARE
) {
3795 * Finally, unshare any volumes shared via iSCSI.
3797 zfs_handle_t
**dslist
= NULL
;
3798 size_t i
, count
= 0;
3800 get_all_datasets(ZFS_TYPE_VOLUME
, &dslist
, &count
,
3804 qsort(dslist
, count
, sizeof (void *),
3807 for (i
= 0; i
< count
; i
++) {
3808 if (zfs_unshare_iscsi(dslist
[i
]) != 0)
3810 zfs_close(dslist
[i
]);
3819 (void) fprintf(stderr
,
3820 gettext("missing filesystem argument\n"));
3822 (void) fprintf(stderr
,
3823 gettext("too many arguments\n"));
3828 * We have an argument, but it may be a full path or a ZFS
3829 * filesystem. Pass full paths off to unmount_path() (shared by
3830 * manual_unmount), otherwise open the filesystem and pass to
3833 if (argv
[0][0] == '/')
3834 return (unshare_unmount_path(op
, argv
[0],
3837 types
= ZFS_TYPE_FILESYSTEM
;
3839 types
|= ZFS_TYPE_VOLUME
;
3841 if ((zhp
= zfs_open(g_zfs
, argv
[0], types
)) == NULL
)
3844 if (zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
3845 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
3846 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
3847 nfsiscsi_mnt_prop
, sizeof (nfsiscsi_mnt_prop
), NULL
,
3848 NULL
, 0, B_FALSE
) == 0);
3852 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3854 sizeof (nfsiscsi_mnt_prop
),
3855 NULL
, NULL
, 0, B_FALSE
) == 0);
3856 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3857 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
3860 if (strcmp(nfsiscsi_mnt_prop
, "off") == 0 &&
3861 strcmp(sharesmb
, "off") == 0) {
3862 (void) fprintf(stderr
, gettext("cannot "
3863 "unshare '%s': legacy share\n"),
3865 (void) fprintf(stderr
, gettext("use "
3866 "unshare(1M) to unshare this "
3869 } else if (!zfs_is_shared(zhp
)) {
3870 (void) fprintf(stderr
, gettext("cannot "
3871 "unshare '%s': not currently "
3872 "shared\n"), zfs_get_name(zhp
));
3874 } else if (zfs_unshareall(zhp
) != 0) {
3880 if (strcmp(nfsiscsi_mnt_prop
, "legacy") == 0) {
3881 (void) fprintf(stderr
, gettext("cannot "
3882 "unmount '%s': legacy "
3883 "mountpoint\n"), zfs_get_name(zhp
));
3884 (void) fprintf(stderr
, gettext("use "
3885 "umount(1M) to unmount this "
3888 } else if (!zfs_is_mounted(zhp
, NULL
)) {
3889 (void) fprintf(stderr
, gettext("cannot "
3890 "unmount '%s': not currently "
3894 } else if (zfs_unmountall(zhp
, flags
) != 0) {
3900 assert(op
== OP_SHARE
);
3902 verify(zfs_prop_get(zhp
, ZFS_PROP_SHAREISCSI
,
3903 nfsiscsi_mnt_prop
, sizeof (nfsiscsi_mnt_prop
),
3904 NULL
, NULL
, 0, B_FALSE
) == 0);
3906 if (strcmp(nfsiscsi_mnt_prop
, "off") == 0) {
3907 (void) fprintf(stderr
, gettext("cannot unshare "
3908 "'%s': 'shareiscsi' property not set\n"),
3910 (void) fprintf(stderr
, gettext("set "
3911 "'shareiscsi' property or use "
3912 "iscsitadm(1M) to share this volume\n"));
3914 } else if (!zfs_is_shared_iscsi(zhp
)) {
3915 (void) fprintf(stderr
, gettext("cannot "
3916 "unshare '%s': not currently shared\n"),
3919 } else if (zfs_unshare_iscsi(zhp
) != 0) {
3932 * zfs unmount filesystem
3934 * Unmount all filesystems, or a specific ZFS filesystem.
3937 zfs_do_unmount(int argc
, char **argv
)
3939 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
3944 * zfs unshare filesystem
3946 * Unshare all filesystems, or a specific ZFS filesystem.
3949 zfs_do_unshare(int argc
, char **argv
)
3951 return (unshare_unmount(OP_SHARE
, argc
, argv
));
3955 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
3956 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
3959 manual_mount(int argc
, char **argv
)
3962 char mountpoint
[ZFS_MAXPROPLEN
];
3963 char mntopts
[MNT_LINE_MAX
] = { '\0' };
3967 char *dataset
, *path
;
3970 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
3973 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
3976 flags
|= MS_OVERLAY
;
3979 flags
|= MS_NOMNTTAB
;
3982 (void) fprintf(stderr
, gettext("missing argument for "
3983 "'%c' option\n"), optopt
);
3987 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3989 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
3998 /* check that we only have two arguments */
4001 (void) fprintf(stderr
, gettext("missing dataset "
4004 (void) fprintf(stderr
,
4005 gettext("missing mountpoint argument\n"));
4007 (void) fprintf(stderr
, gettext("too many arguments\n"));
4008 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
4015 /* try to open the dataset */
4016 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
4019 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
4020 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
4022 /* check for legacy mountpoint and complain appropriately */
4024 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
4025 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
4026 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
4027 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
4032 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
4033 "mounted using 'mount -F zfs'\n"), dataset
);
4034 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
4035 "instead.\n"), path
);
4036 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
4037 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
4038 (void) fprintf(stderr
, gettext("See zfs(1M) for more "
4047 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
4048 * unmounts of non-legacy filesystems, as this is the dominant administrative
4052 manual_unmount(int argc
, char **argv
)
4058 while ((c
= getopt(argc
, argv
, "f")) != -1) {
4064 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4066 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
4075 /* check arguments */
4078 (void) fprintf(stderr
, gettext("missing path "
4081 (void) fprintf(stderr
, gettext("too many arguments\n"));
4082 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
4086 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
4090 volcheck(zpool_handle_t
*zhp
, void *data
)
4092 boolean_t isinit
= *((boolean_t
*)data
);
4095 return (zpool_create_zvol_links(zhp
));
4097 return (zpool_remove_zvol_links(zhp
));
4101 * Iterate over all pools in the system and either create or destroy /dev/zvol
4102 * links, depending on the value of 'isinit'.
4105 do_volcheck(boolean_t isinit
)
4107 return (zpool_iter(g_zfs
, volcheck
, &isinit
) ? 1 : 0);
4111 find_command_idx(char *command
, int *idx
)
4115 for (i
= 0; i
< NCOMMAND
; i
++) {
4116 if (command_table
[i
].name
== NULL
)
4119 if (strcmp(command
, command_table
[i
].name
) == 0) {
4128 main(int argc
, char **argv
)
4135 (void) setlocale(LC_ALL
, "");
4136 (void) textdomain(TEXT_DOMAIN
);
4140 if ((g_zfs
= libzfs_init()) == NULL
) {
4141 (void) fprintf(stderr
, gettext("internal error: failed to "
4142 "initialize ZFS library\n"));
4146 zpool_set_history_str("zfs", argc
, argv
, history_str
);
4147 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
4149 libzfs_print_on_error(g_zfs
, B_TRUE
);
4151 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
4152 (void) fprintf(stderr
, gettext("internal error: unable to "
4153 "open %s\n"), MNTTAB
);
4158 * This command also doubles as the /etc/fs mount and unmount program.
4159 * Determine if we should take this behavior based on argv[0].
4161 progname
= basename(argv
[0]);
4162 if (strcmp(progname
, "mount") == 0) {
4163 ret
= manual_mount(argc
, argv
);
4164 } else if (strcmp(progname
, "umount") == 0) {
4165 ret
= manual_unmount(argc
, argv
);
4168 * Make sure the user has specified some command.
4171 (void) fprintf(stderr
, gettext("missing command\n"));
4178 * The 'umount' command is an alias for 'unmount'
4180 if (strcmp(cmdname
, "umount") == 0)
4181 cmdname
= "unmount";
4184 * The 'recv' command is an alias for 'receive'
4186 if (strcmp(cmdname
, "recv") == 0)
4187 cmdname
= "receive";
4192 if (strcmp(cmdname
, "-?") == 0)
4196 * 'volinit' and 'volfini' do not appear in the usage message,
4197 * so we have to special case them here.
4199 if (strcmp(cmdname
, "volinit") == 0)
4200 return (do_volcheck(B_TRUE
));
4201 else if (strcmp(cmdname
, "volfini") == 0)
4202 return (do_volcheck(B_FALSE
));
4205 * Run the appropriate command.
4207 if (find_command_idx(cmdname
, &i
) == 0) {
4208 current_command
= &command_table
[i
];
4209 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
4210 } else if (strchr(cmdname
, '=') != NULL
) {
4211 verify(find_command_idx("set", &i
) == 0);
4212 current_command
= &command_table
[i
];
4213 ret
= command_table
[i
].func(argc
, argv
);
4215 (void) fprintf(stderr
, gettext("unrecognized "
4216 "command '%s'\n"), cmdname
);
4221 (void) fclose(mnttab_file
);
4226 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4227 * for the purposes of running ::findleaks.
4229 if (getenv("ZFS_ABORT") != NULL
) {
4230 (void) printf("dumping core by request\n");