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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
32 #include <libnvpair.h>
44 #include <sys/mkdev.h>
45 #include <sys/mntent.h>
46 #include <sys/mnttab.h>
47 #include <sys/mount.h>
49 #include <sys/fs/zfs.h>
50 #include <sys/types.h>
58 #include "zfs_comutil.h"
60 libzfs_handle_t
*g_zfs
;
62 static FILE *mnttab_file
;
63 static char history_str
[HIS_MAX_RECORD_LEN
];
64 const char *pypath
= "/usr/lib/zfs/pyzfs.py";
66 static int zfs_do_clone(int argc
, char **argv
);
67 static int zfs_do_create(int argc
, char **argv
);
68 static int zfs_do_destroy(int argc
, char **argv
);
69 static int zfs_do_get(int argc
, char **argv
);
70 static int zfs_do_inherit(int argc
, char **argv
);
71 static int zfs_do_list(int argc
, char **argv
);
72 static int zfs_do_mount(int argc
, char **argv
);
73 static int zfs_do_rename(int argc
, char **argv
);
74 static int zfs_do_rollback(int argc
, char **argv
);
75 static int zfs_do_set(int argc
, char **argv
);
76 static int zfs_do_upgrade(int argc
, char **argv
);
77 static int zfs_do_snapshot(int argc
, char **argv
);
78 static int zfs_do_unmount(int argc
, char **argv
);
79 static int zfs_do_share(int argc
, char **argv
);
80 static int zfs_do_unshare(int argc
, char **argv
);
81 static int zfs_do_send(int argc
, char **argv
);
82 static int zfs_do_receive(int argc
, char **argv
);
83 static int zfs_do_promote(int argc
, char **argv
);
84 static int zfs_do_userspace(int argc
, char **argv
);
85 static int zfs_do_python(int argc
, char **argv
);
86 static int zfs_do_hold(int argc
, char **argv
);
87 static int zfs_do_release(int argc
, char **argv
);
88 static int zfs_do_diff(int argc
, char **argv
);
91 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
96 _umem_debug_init(void)
98 return ("default,verbose"); /* $UMEM_DEBUG setting */
102 _umem_logging_init(void)
104 return ("fail,contents"); /* $UMEM_LOGGING setting */
137 typedef struct zfs_command
{
139 int (*func
)(int argc
, char **argv
);
144 * Master command table. Each ZFS command has a name, associated function, and
145 * usage message. The usage messages need to be internationalized, so we have
146 * to have a function to return the usage message based on a command index.
148 * These commands are organized according to how they are displayed in the usage
149 * message. An empty command (one with a NULL name) indicates an empty line in
150 * the generic usage message.
152 static zfs_command_t command_table
[] = {
153 { "create", zfs_do_create
, HELP_CREATE
},
154 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
156 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
157 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
158 { "clone", zfs_do_clone
, HELP_CLONE
},
159 { "promote", zfs_do_promote
, HELP_PROMOTE
},
160 { "rename", zfs_do_rename
, HELP_RENAME
},
162 { "list", zfs_do_list
, HELP_LIST
},
164 { "set", zfs_do_set
, HELP_SET
},
165 { "get", zfs_do_get
, HELP_GET
},
166 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
167 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
168 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
169 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
171 { "mount", zfs_do_mount
, HELP_MOUNT
},
172 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
173 { "share", zfs_do_share
, HELP_SHARE
},
174 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
176 { "send", zfs_do_send
, HELP_SEND
},
177 { "receive", zfs_do_receive
, HELP_RECEIVE
},
179 { "allow", zfs_do_python
, HELP_ALLOW
},
181 { "unallow", zfs_do_python
, HELP_UNALLOW
},
183 { "hold", zfs_do_hold
, HELP_HOLD
},
184 { "holds", zfs_do_python
, HELP_HOLDS
},
185 { "release", zfs_do_release
, HELP_RELEASE
},
186 { "diff", zfs_do_diff
, HELP_DIFF
},
189 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
191 zfs_command_t
*current_command
;
194 get_usage(zfs_help_t idx
)
198 return (gettext("\tclone [-p] [-o property=value] ... "
199 "<snapshot> <filesystem|volume>\n"));
201 return (gettext("\tcreate [-p] [-o property=value] ... "
203 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
204 "-V <size> <volume>\n"));
206 return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
207 "\tdestroy [-rRd] <snapshot>\n"));
209 return (gettext("\tget [-rHp] [-d max] "
210 "[-o \"all\" | field[,...]] [-s source[,...]]\n"
211 "\t <\"all\" | property[,...]> "
212 "[filesystem|volume|snapshot] ...\n"));
214 return (gettext("\tinherit [-rS] <property> "
215 "<filesystem|volume|snapshot> ...\n"));
217 return (gettext("\tupgrade [-v]\n"
218 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
220 return (gettext("\tlist [-rH][-d max] "
221 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
222 "\t [-S property] ... "
223 "[filesystem|volume|snapshot] ...\n"));
225 return (gettext("\tmount\n"
226 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
228 return (gettext("\tpromote <clone-filesystem>\n"));
230 return (gettext("\treceive [-vnFu] <filesystem|volume|"
232 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
234 return (gettext("\trename <filesystem|volume|snapshot> "
235 "<filesystem|volume|snapshot>\n"
236 "\trename -p <filesystem|volume> <filesystem|volume>\n"
237 "\trename -r <snapshot> <snapshot>"));
239 return (gettext("\trollback [-rRf] <snapshot>\n"));
241 return (gettext("\tsend [-RDp] [-[iI] snapshot] <snapshot>\n"));
243 return (gettext("\tset <property=value> "
244 "<filesystem|volume|snapshot> ...\n"));
246 return (gettext("\tshare <-a | filesystem>\n"));
248 return (gettext("\tsnapshot [-r] [-o property=value] ... "
249 "<filesystem@snapname|volume@snapname>\n"));
251 return (gettext("\tunmount [-f] "
252 "<-a | filesystem|mountpoint>\n"));
254 return (gettext("\tunshare "
255 "<-a | filesystem|mountpoint>\n"));
257 return (gettext("\tallow <filesystem|volume>\n"
259 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
260 "\t <filesystem|volume>\n"
261 "\tallow [-ld] -e <perm|@setname>[,...] "
262 "<filesystem|volume>\n"
263 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
264 "\tallow -s @setname <perm|@setname>[,...] "
265 "<filesystem|volume>\n"));
267 return (gettext("\tunallow [-rldug] "
268 "<\"everyone\"|user|group>[,...]\n"
269 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
270 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
271 "<filesystem|volume>\n"
272 "\tunallow [-r] -c [<perm|@setname>[,...]] "
273 "<filesystem|volume>\n"
274 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
275 "<filesystem|volume>\n"));
277 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
278 "[-sS field] ... [-t type[,...]]\n"
279 "\t <filesystem|snapshot>\n"));
280 case HELP_GROUPSPACE
:
281 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
282 "[-sS field] ... [-t type[,...]]\n"
283 "\t <filesystem|snapshot>\n"));
285 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
287 return (gettext("\tholds [-r] <snapshot> ...\n"));
289 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
291 return (gettext("\tdiff [-FHt] <snapshot> "
292 "[snapshot|filesystem]\n"));
302 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
307 * Utility function to guarantee malloc() success.
311 safe_malloc(size_t size
)
315 if ((data
= calloc(1, size
)) == NULL
)
323 safe_strdup(char *str
)
325 char *dupstr
= strdup(str
);
332 #endif /* HAVE_ZPL */
335 * Callback routine that will print out information for each of
339 usage_prop_cb(int prop
, void *cb
)
343 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
345 if (zfs_prop_readonly(prop
))
346 (void) fprintf(fp
, " NO ");
348 (void) fprintf(fp
, "YES ");
350 if (zfs_prop_inheritable(prop
))
351 (void) fprintf(fp
, " YES ");
353 (void) fprintf(fp
, " NO ");
355 if (zfs_prop_values(prop
) == NULL
)
356 (void) fprintf(fp
, "-\n");
358 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
364 * Display usage message. If we're inside a command, display only the usage for
365 * that command. Otherwise, iterate over the entire command table and display
366 * a complete usage message.
369 usage(boolean_t requested
)
372 boolean_t show_properties
= B_FALSE
;
373 FILE *fp
= requested
? stdout
: stderr
;
375 if (current_command
== NULL
) {
377 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
379 gettext("where 'command' is one of the following:\n\n"));
381 for (i
= 0; i
< NCOMMAND
; i
++) {
382 if (command_table
[i
].name
== NULL
)
383 (void) fprintf(fp
, "\n");
385 (void) fprintf(fp
, "%s",
386 get_usage(command_table
[i
].usage
));
389 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
390 "pool/[dataset/]*dataset[@name]\n"));
392 (void) fprintf(fp
, gettext("usage:\n"));
393 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
396 if (current_command
!= NULL
&&
397 (strcmp(current_command
->name
, "set") == 0 ||
398 strcmp(current_command
->name
, "get") == 0 ||
399 strcmp(current_command
->name
, "inherit") == 0 ||
400 strcmp(current_command
->name
, "list") == 0))
401 show_properties
= B_TRUE
;
403 if (show_properties
) {
405 gettext("\nThe following properties are supported:\n"));
407 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
408 "PROPERTY", "EDIT", "INHERIT", "VALUES");
410 /* Iterate over all properties */
411 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
414 (void) fprintf(fp
, "\t%-15s ", "userused@...");
415 (void) fprintf(fp
, " NO NO <size>\n");
416 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
417 (void) fprintf(fp
, " NO NO <size>\n");
418 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
419 (void) fprintf(fp
, "YES NO <size> | none\n");
420 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
421 (void) fprintf(fp
, "YES NO <size> | none\n");
423 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
424 "with standard units such as K, M, G, etc.\n"));
425 (void) fprintf(fp
, gettext("\nUser-defined properties can "
426 "be specified by using a name containing a colon (:).\n"));
427 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
428 "properties must be appended with\n"
429 "a user or group specifier of one of these forms:\n"
430 " POSIX name (eg: \"matt\")\n"
431 " POSIX id (eg: \"126829\")\n"
432 " SMB name@domain (eg: \"matt@sun\")\n"
433 " SMB SID (eg: \"S-1-234-567-89\")\n"));
436 gettext("\nFor the property list, run: %s\n"),
439 gettext("\nFor the delegated permission list, run: %s\n"),
440 "zfs allow|unallow");
444 * See comments at end of main().
446 if (getenv("ZFS_ABORT") != NULL
) {
447 (void) printf("dumping core by request\n");
451 exit(requested
? 0 : 2);
455 parseprop(nvlist_t
*props
)
457 char *propname
= optarg
;
458 char *propval
, *strval
;
460 if ((propval
= strchr(propname
, '=')) == NULL
) {
461 (void) fprintf(stderr
, gettext("missing "
462 "'=' for -o option\n"));
467 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
468 (void) fprintf(stderr
, gettext("property '%s' "
469 "specified multiple times\n"), propname
);
472 if (nvlist_add_string(props
, propname
, propval
) != 0)
478 parse_depth(char *opt
, int *flags
)
483 depth
= (int)strtol(opt
, &tmp
, 0);
485 (void) fprintf(stderr
,
486 gettext("%s is not an integer\n"), optarg
);
490 (void) fprintf(stderr
,
491 gettext("Depth can not be negative.\n"));
494 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
498 #define PROGRESS_DELAY 2 /* seconds */
501 static char *pt_reverse
= "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
502 static time_t pt_begin
;
503 static char *pt_header
= NULL
;
504 static boolean_t pt_shown
;
507 start_progress_timer(void)
509 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
514 set_progress_header(char *header
)
516 assert(pt_header
== NULL
);
517 pt_header
= safe_strdup(header
);
519 (void) printf("%s: ", header
);
520 (void) fflush(stdout
);
525 update_progress(char *update
)
527 if (!pt_shown
&& time(NULL
) > pt_begin
) {
528 int len
= strlen(update
);
530 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
532 (void) fflush(stdout
);
534 } else if (pt_shown
) {
535 int len
= strlen(update
);
537 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
538 (void) fflush(stdout
);
543 finish_progress(char *done
)
546 (void) printf("%s\n", done
);
547 (void) fflush(stdout
);
552 #endif /* HAVE_ZPL */
555 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
557 * Given an existing dataset, create a writable copy whose initial contents
558 * are the same as the source. The newly created dataset maintains a
559 * dependency on the original; the original cannot be destroyed so long as
562 * The '-p' flag creates all the non-existing ancestors of the target first.
565 zfs_do_clone(int argc
, char **argv
)
567 zfs_handle_t
*zhp
= NULL
;
568 boolean_t parents
= B_FALSE
;
573 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
577 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
580 if (parseprop(props
))
587 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
596 /* check number of arguments */
598 (void) fprintf(stderr
, gettext("missing source dataset "
603 (void) fprintf(stderr
, gettext("missing target dataset "
608 (void) fprintf(stderr
, gettext("too many arguments\n"));
612 /* open the source dataset */
613 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
616 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
619 * Now create the ancestors of the target dataset. If the
620 * target already exists and '-p' option was used we should not
623 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
626 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
631 ret
= zfs_clone(zhp
, argv
[1], props
);
633 /* create the mountpoint if necessary */
638 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
640 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
)
641 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
642 ret
= zfs_share(clone
);
646 #endif /* HAVE_ZPL */
662 * zfs create [-p] [-o prop=value] ... fs
663 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
665 * Create a new dataset. This command can be used to create filesystems
666 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
667 * For volumes, the user must specify a size to be used.
669 * The '-s' flag applies only to volumes, and indicates that we should not try
670 * to set the reservation for this volume. By default we set a reservation
671 * equal to the size for any volume. For pools with SPA_VERSION >=
672 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
674 * The '-p' flag creates all the non-existing ancestors of the target first.
677 zfs_do_create(int argc
, char **argv
)
679 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
680 zfs_handle_t
*zhp
= NULL
;
681 uint64_t volsize
= 0;
683 boolean_t noreserve
= B_FALSE
;
684 boolean_t bflag
= B_FALSE
;
685 boolean_t parents
= B_FALSE
;
689 int canmount
= ZFS_CANMOUNT_OFF
;
691 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
695 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
698 type
= ZFS_TYPE_VOLUME
;
699 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
700 (void) fprintf(stderr
, gettext("bad volume "
701 "size '%s': %s\n"), optarg
,
702 libzfs_error_description(g_zfs
));
706 if (nvlist_add_uint64(props
,
707 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
716 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
717 (void) fprintf(stderr
, gettext("bad volume "
718 "block size '%s': %s\n"), optarg
,
719 libzfs_error_description(g_zfs
));
723 if (nvlist_add_uint64(props
,
724 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
729 if (parseprop(props
))
736 (void) fprintf(stderr
, gettext("missing size "
741 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
747 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
748 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
749 "used when creating a volume\n"));
756 /* check number of arguments */
758 (void) fprintf(stderr
, gettext("missing %s argument\n"),
759 zfs_type_to_name(type
));
763 (void) fprintf(stderr
, gettext("too many arguments\n"));
767 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
768 zpool_handle_t
*zpool_handle
;
769 uint64_t spa_version
;
771 zfs_prop_t resv_prop
;
774 if ((p
= strchr(argv
[0], '/')))
776 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
779 if (zpool_handle
== NULL
)
781 spa_version
= zpool_get_prop_int(zpool_handle
,
782 ZPOOL_PROP_VERSION
, NULL
);
783 zpool_close(zpool_handle
);
784 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
785 resv_prop
= ZFS_PROP_REFRESERVATION
;
787 resv_prop
= ZFS_PROP_RESERVATION
;
788 volsize
= zvol_volsize_to_reservation(volsize
, props
);
790 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
792 if (nvlist_add_uint64(props
,
793 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
800 if (parents
&& zfs_name_valid(argv
[0], type
)) {
802 * Now create the ancestors of target dataset. If the target
803 * already exists and '-p' option was used we should not
806 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
810 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
815 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
818 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
823 * if the user doesn't want the dataset automatically mounted,
824 * then skip the mount/share step
826 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
))
827 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
830 * Mount and/or share the new filesystem as appropriate. We provide a
831 * verbose error message to let the user know that their filesystem was
832 * in fact created, even if we failed to mount or share it.
835 if (canmount
== ZFS_CANMOUNT_ON
) {
836 if (zfs_mount(zhp
, NULL
, 0) != 0) {
837 (void) fprintf(stderr
, gettext("filesystem "
838 "successfully created, but not mounted\n"));
840 } else if (zfs_share(zhp
) != 0) {
841 (void) fprintf(stderr
, gettext("filesystem "
842 "successfully created, but not shared\n"));
846 #endif /* HAVE_ZPL */
860 * zfs destroy [-rRf] <fs, vol>
861 * zfs destroy [-rRd] <snap>
863 * -r Recursively destroy all children
864 * -R Recursively destroy all dependents, including clones
865 * -f Force unmounting of any dependents
866 * -d If we can't destroy now, mark for deferred destruction
868 * Destroys the given dataset. By default, it will unmount any filesystems,
869 * and refuse to destroy a dataset that has any dependents. A dependent can
870 * either be a child, or a clone of a child.
872 typedef struct destroy_cbdata
{
879 boolean_t cb_closezhp
;
880 zfs_handle_t
*cb_target
;
882 boolean_t cb_defer_destroy
;
886 * Check for any dependents based on the '-r' or '-R' flags.
889 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
891 destroy_cbdata_t
*cbp
= data
;
892 const char *tname
= zfs_get_name(cbp
->cb_target
);
893 const char *name
= zfs_get_name(zhp
);
895 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
896 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
898 * This is a direct descendant, not a clone somewhere else in
905 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
906 "%s has children\n"),
907 zfs_get_name(cbp
->cb_target
),
908 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
909 (void) fprintf(stderr
, gettext("use '-r' to destroy "
910 "the following datasets:\n"));
911 cbp
->cb_first
= B_FALSE
;
915 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
918 * This is a clone. We only want to report this if the '-r'
919 * wasn't specified, or the target is a snapshot.
921 if (!cbp
->cb_recurse
&&
922 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
926 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
927 "%s has dependent clones\n"),
928 zfs_get_name(cbp
->cb_target
),
929 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
930 (void) fprintf(stderr
, gettext("use '-R' to destroy "
931 "the following datasets:\n"));
932 cbp
->cb_first
= B_FALSE
;
936 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
945 destroy_callback(zfs_handle_t
*zhp
, void *data
)
947 destroy_cbdata_t
*cbp
= data
;
950 * Ignore pools (which we've already flagged as an error before getting
953 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
954 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
960 * Bail out on the first error.
962 if (zfs_unmount(zhp
, NULL
, cbp
->cb_force
? MS_FORCE
: 0) != 0 ||
963 zfs_destroy(zhp
, cbp
->cb_defer_destroy
) != 0) {
973 destroy_snap_clones(zfs_handle_t
*zhp
, void *arg
)
975 destroy_cbdata_t
*cbp
= arg
;
976 char thissnap
[MAXPATHLEN
];
978 boolean_t closezhp
= cbp
->cb_closezhp
;
981 (void) snprintf(thissnap
, sizeof (thissnap
),
982 "%s@%s", zfs_get_name(zhp
), cbp
->cb_snapname
);
984 libzfs_print_on_error(g_zfs
, B_FALSE
);
985 szhp
= zfs_open(g_zfs
, thissnap
, ZFS_TYPE_SNAPSHOT
);
986 libzfs_print_on_error(g_zfs
, B_TRUE
);
989 * Destroy any clones of this snapshot
991 if (zfs_iter_dependents(szhp
, B_FALSE
, destroy_callback
,
1001 cbp
->cb_closezhp
= B_TRUE
;
1002 rv
= zfs_iter_filesystems(zhp
, destroy_snap_clones
, arg
);
1009 zfs_do_destroy(int argc
, char **argv
)
1011 destroy_cbdata_t cb
= { 0 };
1015 zfs_type_t type
= ZFS_TYPE_DATASET
;
1018 while ((c
= getopt(argc
, argv
, "dfrR")) != -1) {
1021 cb
.cb_defer_destroy
= B_TRUE
;
1022 type
= ZFS_TYPE_SNAPSHOT
;
1036 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1045 /* check number of arguments */
1047 (void) fprintf(stderr
, gettext("missing path argument\n"));
1051 (void) fprintf(stderr
, gettext("too many arguments\n"));
1056 * If we are doing recursive destroy of a snapshot, then the
1057 * named snapshot may not exist. Go straight to libzfs.
1059 if (cb
.cb_recurse
&& (cp
= strchr(argv
[0], '@'))) {
1063 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
1068 if (cb
.cb_doclones
) {
1069 boolean_t defer
= cb
.cb_defer_destroy
;
1072 * Temporarily ignore the defer_destroy setting since
1073 * it's not supported for clones.
1075 cb
.cb_defer_destroy
= B_FALSE
;
1076 cb
.cb_snapname
= cp
;
1077 if (destroy_snap_clones(zhp
, &cb
) != 0) {
1081 cb
.cb_defer_destroy
= defer
;
1084 ret
= zfs_destroy_snaps(zhp
, cp
, cb
.cb_defer_destroy
);
1087 (void) fprintf(stderr
,
1088 gettext("no snapshots destroyed\n"));
1093 /* Open the given dataset */
1094 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1100 * Perform an explicit check for pools before going any further.
1102 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1103 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1104 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1105 "operation does not apply to pools\n"),
1107 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1108 "%s' to destroy all datasets in the pool\n"),
1110 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1111 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1117 * Check for any dependents and/or clones.
1119 cb
.cb_first
= B_TRUE
;
1120 if (!cb
.cb_doclones
&& !cb
.cb_defer_destroy
&&
1121 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1127 if (cb
.cb_error
|| (!cb
.cb_defer_destroy
&&
1128 (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
, &cb
) != 0))) {
1134 * Do the real thing. The callback will close the handle regardless of
1135 * whether it succeeds or not.
1138 if (destroy_callback(zhp
, &cb
) != 0)
1145 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1148 zfs_get_column_t col
;
1150 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1151 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1152 if (col
== GET_COL_RECVD
)
1158 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1159 * < all | property[,property]... > < fs | snap | vol > ...
1161 * -r recurse over any child datasets
1162 * -H scripted mode. Headers are stripped, and fields are separated
1163 * by tabs instead of spaces.
1164 * -o Set of fields to display. One of "name,property,value,
1165 * received,source". Default is "name,property,value,source".
1166 * "all" is an alias for all five.
1167 * -s Set of sources to allow. One of
1168 * "local,default,inherited,received,temporary,none". Default is
1170 * -p Display values in parsable (literal) format.
1172 * Prints properties for the given datasets. The user can control which
1173 * columns to display as well as which property types to allow.
1177 * Invoked to display the properties for a single dataset.
1180 get_callback(zfs_handle_t
*zhp
, void *data
)
1182 char buf
[ZFS_MAXPROPLEN
];
1183 char rbuf
[ZFS_MAXPROPLEN
];
1184 zprop_source_t sourcetype
;
1185 char source
[ZFS_MAXNAMELEN
];
1186 zprop_get_cbdata_t
*cbp
= data
;
1187 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1188 zprop_list_t
*pl
= cbp
->cb_proplist
;
1192 boolean_t received
= is_recvd_column(cbp
);
1194 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1195 char *recvdval
= NULL
;
1197 * Skip the special fake placeholder. This will also skip over
1198 * the name property when 'all' is specified.
1200 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1201 pl
== cbp
->cb_proplist
)
1204 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1205 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1206 sizeof (buf
), &sourcetype
, source
,
1208 cbp
->cb_literal
) != 0) {
1211 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1212 ZFS_TYPE_DATASET
)) {
1213 (void) fprintf(stderr
,
1214 gettext("No such property '%s'\n"),
1215 zfs_prop_to_name(pl
->pl_prop
));
1218 sourcetype
= ZPROP_SRC_NONE
;
1219 (void) strlcpy(buf
, "-", sizeof (buf
));
1222 if (received
&& (zfs_prop_get_recvd(zhp
,
1223 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1224 cbp
->cb_literal
) == 0))
1227 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1228 zfs_prop_to_name(pl
->pl_prop
),
1229 buf
, sourcetype
, source
, recvdval
);
1230 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1231 sourcetype
= ZPROP_SRC_LOCAL
;
1233 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1234 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1235 sourcetype
= ZPROP_SRC_NONE
;
1236 (void) strlcpy(buf
, "-", sizeof (buf
));
1239 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1240 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1242 if (nvlist_lookup_nvlist(user_props
,
1243 pl
->pl_user_prop
, &propval
) != 0) {
1246 sourcetype
= ZPROP_SRC_NONE
;
1249 verify(nvlist_lookup_string(propval
,
1250 ZPROP_VALUE
, &strval
) == 0);
1251 verify(nvlist_lookup_string(propval
,
1252 ZPROP_SOURCE
, &sourceval
) == 0);
1254 if (strcmp(sourceval
,
1255 zfs_get_name(zhp
)) == 0) {
1256 sourcetype
= ZPROP_SRC_LOCAL
;
1257 } else if (strcmp(sourceval
,
1258 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1259 sourcetype
= ZPROP_SRC_RECEIVED
;
1261 sourcetype
= ZPROP_SRC_INHERITED
;
1262 (void) strlcpy(source
,
1263 sourceval
, sizeof (source
));
1267 if (received
&& (zfs_prop_get_recvd(zhp
,
1268 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1269 cbp
->cb_literal
) == 0))
1272 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1273 pl
->pl_user_prop
, strval
, sourcetype
,
1282 zfs_do_get(int argc
, char **argv
)
1284 zprop_get_cbdata_t cb
= { 0 };
1285 int i
, c
, flags
= 0;
1286 char *value
, *fields
;
1289 zprop_list_t fake_name
= { 0 };
1292 * Set up default columns and sources.
1294 cb
.cb_sources
= ZPROP_SRC_ALL
;
1295 cb
.cb_columns
[0] = GET_COL_NAME
;
1296 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1297 cb
.cb_columns
[2] = GET_COL_VALUE
;
1298 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1299 cb
.cb_type
= ZFS_TYPE_DATASET
;
1302 while ((c
= getopt(argc
, argv
, ":d:o:s:rHp")) != -1) {
1305 cb
.cb_literal
= B_TRUE
;
1308 limit
= parse_depth(optarg
, &flags
);
1311 flags
|= ZFS_ITER_RECURSE
;
1314 cb
.cb_scripted
= B_TRUE
;
1317 (void) fprintf(stderr
, gettext("missing argument for "
1318 "'%c' option\n"), optopt
);
1323 * Process the set of columns to display. We zero out
1324 * the structure to give us a blank slate.
1326 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1328 while (*optarg
!= '\0') {
1329 static char *col_subopts
[] =
1330 { "name", "property", "value", "received",
1331 "source", "all", NULL
};
1333 if (i
== ZFS_GET_NCOLS
) {
1334 (void) fprintf(stderr
, gettext("too "
1335 "many fields given to -o "
1340 switch (getsubopt(&optarg
, col_subopts
,
1343 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1346 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1349 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1352 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1353 flags
|= ZFS_ITER_RECVD_PROPS
;
1356 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1360 (void) fprintf(stderr
,
1361 gettext("\"all\" conflicts "
1362 "with specific fields "
1363 "given to -o option\n"));
1366 cb
.cb_columns
[0] = GET_COL_NAME
;
1367 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1368 cb
.cb_columns
[2] = GET_COL_VALUE
;
1369 cb
.cb_columns
[3] = GET_COL_RECVD
;
1370 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1371 flags
|= ZFS_ITER_RECVD_PROPS
;
1375 (void) fprintf(stderr
,
1376 gettext("invalid column name "
1385 while (*optarg
!= '\0') {
1386 static char *source_subopts
[] = {
1387 "local", "default", "inherited",
1388 "received", "temporary", "none",
1391 switch (getsubopt(&optarg
, source_subopts
,
1394 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1397 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1400 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1403 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1406 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1409 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1412 (void) fprintf(stderr
,
1413 gettext("invalid source "
1421 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1431 (void) fprintf(stderr
, gettext("missing property "
1438 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1446 * As part of zfs_expand_proplist(), we keep track of the maximum column
1447 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1448 * need to know the maximum name length. However, the user likely did
1449 * not specify 'name' as one of the properties to fetch, so we need to
1450 * make sure we always include at least this property for
1451 * print_get_headers() to work properly.
1453 if (cb
.cb_proplist
!= NULL
) {
1454 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1455 fake_name
.pl_width
= strlen(gettext("NAME"));
1456 fake_name
.pl_next
= cb
.cb_proplist
;
1457 cb
.cb_proplist
= &fake_name
;
1460 cb
.cb_first
= B_TRUE
;
1462 /* run for each object */
1463 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
, NULL
,
1464 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1466 if (cb
.cb_proplist
== &fake_name
)
1467 zprop_free_list(fake_name
.pl_next
);
1469 zprop_free_list(cb
.cb_proplist
);
1475 * inherit [-rS] <property> <fs|vol> ...
1477 * -r Recurse over all children
1478 * -S Revert to received value, if any
1480 * For each dataset specified on the command line, inherit the given property
1481 * from its parent. Inheriting a property at the pool level will cause it to
1482 * use the default value. The '-r' flag will recurse over all children, and is
1483 * useful for setting a property on a hierarchy-wide basis, regardless of any
1484 * local modifications for each dataset.
1487 typedef struct inherit_cbdata
{
1488 const char *cb_propname
;
1489 boolean_t cb_received
;
1493 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1495 inherit_cbdata_t
*cb
= data
;
1496 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1499 * If we're doing it recursively, then ignore properties that
1500 * are not valid for this type of dataset.
1502 if (prop
!= ZPROP_INVAL
&&
1503 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1506 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1510 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1512 inherit_cbdata_t
*cb
= data
;
1514 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1518 zfs_do_inherit(int argc
, char **argv
)
1522 inherit_cbdata_t cb
= { 0 };
1526 boolean_t received
= B_FALSE
;
1529 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1532 flags
|= ZFS_ITER_RECURSE
;
1539 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1548 /* check number of arguments */
1550 (void) fprintf(stderr
, gettext("missing property argument\n"));
1554 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1562 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1563 if (zfs_prop_readonly(prop
)) {
1564 (void) fprintf(stderr
, gettext(
1565 "%s property is read-only\n"),
1569 if (!zfs_prop_inheritable(prop
) && !received
) {
1570 (void) fprintf(stderr
, gettext("'%s' property cannot "
1571 "be inherited\n"), propname
);
1572 if (prop
== ZFS_PROP_QUOTA
||
1573 prop
== ZFS_PROP_RESERVATION
||
1574 prop
== ZFS_PROP_REFQUOTA
||
1575 prop
== ZFS_PROP_REFRESERVATION
)
1576 (void) fprintf(stderr
, gettext("use 'zfs set "
1577 "%s=none' to clear\n"), propname
);
1580 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1581 prop
== ZFS_PROP_VERSION
)) {
1582 (void) fprintf(stderr
, gettext("'%s' property cannot "
1583 "be reverted to a received value\n"), propname
);
1586 } else if (!zfs_prop_user(propname
)) {
1587 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1592 cb
.cb_propname
= propname
;
1593 cb
.cb_received
= received
;
1595 if (flags
& ZFS_ITER_RECURSE
) {
1596 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1597 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1599 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1600 NULL
, NULL
, 0, inherit_cb
, &cb
);
1606 typedef struct upgrade_cbdata
{
1607 uint64_t cb_numupgraded
;
1608 uint64_t cb_numsamegraded
;
1609 uint64_t cb_numfailed
;
1610 uint64_t cb_version
;
1612 boolean_t cb_foundone
;
1613 char cb_lastfs
[ZFS_MAXNAMELEN
];
1617 same_pool(zfs_handle_t
*zhp
, const char *name
)
1619 int len1
= strcspn(name
, "/@");
1620 const char *zhname
= zfs_get_name(zhp
);
1621 int len2
= strcspn(zhname
, "/@");
1625 return (strncmp(name
, zhname
, len1
) == 0);
1629 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1631 upgrade_cbdata_t
*cb
= data
;
1632 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1634 /* list if it's old/new */
1635 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1636 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1639 str
= gettext("The following filesystems are "
1640 "formatted using a newer software version and\n"
1641 "cannot be accessed on the current system.\n\n");
1643 str
= gettext("The following filesystems are "
1644 "out of date, and can be upgraded. After being\n"
1645 "upgraded, these filesystems (and any 'zfs send' "
1646 "streams generated from\n"
1647 "subsequent snapshots) will no longer be "
1648 "accessible by older software versions.\n\n");
1651 if (!cb
->cb_foundone
) {
1653 (void) printf(gettext("VER FILESYSTEM\n"));
1654 (void) printf(gettext("--- ------------\n"));
1655 cb
->cb_foundone
= B_TRUE
;
1658 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1665 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1667 upgrade_cbdata_t
*cb
= data
;
1668 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1669 int needed_spa_version
;
1672 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1675 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
1677 if (needed_spa_version
< 0)
1680 if (spa_version
< needed_spa_version
) {
1682 (void) printf(gettext("%s: can not be "
1683 "upgraded; the pool version needs to first "
1684 "be upgraded\nto version %d\n\n"),
1685 zfs_get_name(zhp
), needed_spa_version
);
1691 if (version
< cb
->cb_version
) {
1693 (void) snprintf(verstr
, sizeof (verstr
),
1694 "%llu", (u_longlong_t
)cb
->cb_version
);
1695 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1697 * If they did "zfs upgrade -a", then we could
1698 * be doing ioctls to different pools. We need
1699 * to log this history once to each pool.
1701 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
1703 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1704 cb
->cb_numupgraded
++;
1707 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1708 } else if (version
> cb
->cb_version
) {
1709 /* can't downgrade */
1710 (void) printf(gettext("%s: can not be downgraded; "
1711 "it is already at version %u\n"),
1712 zfs_get_name(zhp
), version
);
1715 cb
->cb_numsamegraded
++;
1723 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1726 zfs_do_upgrade(int argc
, char **argv
)
1728 boolean_t all
= B_FALSE
;
1729 boolean_t showversions
= B_FALSE
;
1731 upgrade_cbdata_t cb
= { 0 };
1733 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1736 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1739 flags
|= ZFS_ITER_RECURSE
;
1742 showversions
= B_TRUE
;
1745 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
1746 optarg
, &cb
.cb_version
) != 0) {
1747 (void) fprintf(stderr
,
1748 gettext("invalid version %s\n"), optarg
);
1757 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1766 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
1768 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
1769 cb
.cb_version
|| argc
))
1771 if ((all
|| argc
) && (showversions
))
1777 /* Show info on available versions. */
1778 (void) printf(gettext("The following filesystem versions are "
1780 (void) printf(gettext("VER DESCRIPTION\n"));
1781 (void) printf("--- -----------------------------------------"
1782 "---------------\n");
1783 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1784 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1785 (void) printf(gettext(" 3 Case insensitive and File system "
1786 "unique identifier (FUID)\n"));
1787 (void) printf(gettext(" 4 userquota, groupquota "
1789 (void) printf(gettext(" 5 System attributes\n"));
1790 (void) printf(gettext("\nFor more information on a particular "
1791 "version, including supported releases,\n"));
1792 (void) printf("see the ZFS Administration Guide.\n\n");
1794 } else if (argc
|| all
) {
1795 /* Upgrade filesystems */
1796 if (cb
.cb_version
== 0)
1797 cb
.cb_version
= ZPL_VERSION
;
1798 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
1799 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
1800 (void) printf(gettext("%llu filesystems upgraded\n"),
1801 (u_longlong_t
)cb
.cb_numupgraded
);
1802 if (cb
.cb_numsamegraded
) {
1803 (void) printf(gettext("%llu filesystems already at "
1805 (u_longlong_t
)cb
.cb_numsamegraded
);
1807 if (cb
.cb_numfailed
!= 0)
1810 /* List old-version filesytems */
1812 (void) printf(gettext("This system is currently running "
1813 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
1815 flags
|= ZFS_ITER_RECURSE
;
1816 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1817 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
1819 found
= cb
.cb_foundone
;
1820 cb
.cb_foundone
= B_FALSE
;
1821 cb
.cb_newer
= B_TRUE
;
1823 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1824 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
1826 if (!cb
.cb_foundone
&& !found
) {
1827 (void) printf(gettext("All filesystems are "
1828 "formatted with the current version.\n"));
1839 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
1841 zfs_userquota_prop_t
*typep
= arg
;
1842 zfs_userquota_prop_t p
= *typep
;
1844 char *ug
, *propname
;
1848 if (domain
== NULL
|| domain
[0] == '\0') {
1849 if (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) {
1850 struct group
*g
= getgrgid(rid
);
1854 struct passwd
*p
= getpwuid(rid
);
1860 if (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
)
1865 if (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_GROUPUSED
)
1871 (void) snprintf(namebuf
, sizeof (namebuf
),
1872 "%llu", (longlong_t
)rid
);
1875 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
1877 (void) printf("%s %s %s%c%s %s\n", propname
, ug
, domain
,
1878 domain
[0] ? '-' : ' ', name
, sizebuf
);
1884 zfs_do_userspace(int argc
, char **argv
)
1887 zfs_userquota_prop_t p
;
1891 * Try the python version. If the execv fails, we'll continue
1892 * and do a simplistic implementation.
1894 (void) execv(pypath
, argv
-1);
1896 (void) printf("internal error: %s not found\n"
1897 "falling back on built-in implementation, "
1898 "some features will not work\n", pypath
);
1900 if ((zhp
= zfs_open(g_zfs
, argv
[argc
-1], ZFS_TYPE_DATASET
)) == NULL
)
1903 (void) printf("PROP TYPE NAME VALUE\n");
1905 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
1906 error
= zfs_userspace(zhp
, p
, userspace_cb
, &p
);
1914 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
1915 * [-s property [-s property]...] [-S property [-S property]...]
1918 * -r Recurse over all children
1919 * -d Limit recursion by depth.
1920 * -H Scripted mode; elide headers and separate columns by tabs
1921 * -o Control which fields to display.
1922 * -t Control which object types to display.
1923 * -s Specify sort columns, descending order.
1924 * -S Specify sort columns, ascending order.
1926 * When given no arguments, lists all filesystems in the system.
1927 * Otherwise, list the specified datasets, optionally recursing down them if
1928 * '-r' is specified.
1930 typedef struct list_cbdata
{
1932 boolean_t cb_scripted
;
1933 zprop_list_t
*cb_proplist
;
1937 * Given a list of columns to display, output appropriate headers for each one.
1940 print_header(zprop_list_t
*pl
)
1942 char headerbuf
[ZFS_MAXPROPLEN
];
1945 boolean_t first
= B_TRUE
;
1946 boolean_t right_justify
;
1948 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1955 right_justify
= B_FALSE
;
1956 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1957 header
= zfs_prop_column_name(pl
->pl_prop
);
1958 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1960 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
1961 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
1962 headerbuf
[i
] = '\0';
1966 if (pl
->pl_next
== NULL
&& !right_justify
)
1967 (void) printf("%s", header
);
1968 else if (right_justify
)
1969 (void) printf("%*s", (int)pl
->pl_width
, header
);
1971 (void) printf("%-*s", (int)pl
->pl_width
, header
);
1974 (void) printf("\n");
1978 * Given a dataset and a list of fields, print out all the properties according
1979 * to the described layout.
1982 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
1984 boolean_t first
= B_TRUE
;
1985 char property
[ZFS_MAXPROPLEN
];
1986 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
1989 boolean_t right_justify
;
1992 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1995 (void) printf("\t");
2002 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2003 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
2004 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
2009 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2010 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2011 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2012 property
, sizeof (property
), B_FALSE
) != 0)
2016 right_justify
= B_TRUE
;
2018 if (nvlist_lookup_nvlist(userprops
,
2019 pl
->pl_user_prop
, &propval
) != 0)
2022 verify(nvlist_lookup_string(propval
,
2023 ZPROP_VALUE
, &propstr
) == 0);
2024 right_justify
= B_FALSE
;
2027 width
= pl
->pl_width
;
2030 * If this is being called in scripted mode, or if this is the
2031 * last column and it is left-justified, don't include a width
2034 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2035 (void) printf("%s", propstr
);
2036 else if (right_justify
)
2037 (void) printf("%*s", width
, propstr
);
2039 (void) printf("%-*s", width
, propstr
);
2042 (void) printf("\n");
2046 * Generic callback function to list a dataset or snapshot.
2049 list_callback(zfs_handle_t
*zhp
, void *data
)
2051 list_cbdata_t
*cbp
= data
;
2053 if (cbp
->cb_first
) {
2054 if (!cbp
->cb_scripted
)
2055 print_header(cbp
->cb_proplist
);
2056 cbp
->cb_first
= B_FALSE
;
2059 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
2065 zfs_do_list(int argc
, char **argv
)
2068 boolean_t scripted
= B_FALSE
;
2069 static char default_fields
[] =
2070 "name,used,available,referenced,mountpoint";
2071 int types
= ZFS_TYPE_DATASET
;
2072 boolean_t types_specified
= B_FALSE
;
2073 char *fields
= NULL
;
2074 list_cbdata_t cb
= { 0 };
2078 zfs_sort_column_t
*sortcol
= NULL
;
2079 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
2082 while ((c
= getopt(argc
, argv
, ":d:o:rt:Hs:S:")) != -1) {
2088 limit
= parse_depth(optarg
, &flags
);
2091 flags
|= ZFS_ITER_RECURSE
;
2097 if (zfs_add_sort_column(&sortcol
, optarg
,
2099 (void) fprintf(stderr
,
2100 gettext("invalid property '%s'\n"), optarg
);
2105 if (zfs_add_sort_column(&sortcol
, optarg
,
2107 (void) fprintf(stderr
,
2108 gettext("invalid property '%s'\n"), optarg
);
2114 types_specified
= B_TRUE
;
2115 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2116 while (*optarg
!= '\0') {
2117 static char *type_subopts
[] = { "filesystem",
2118 "volume", "snapshot", "all", NULL
};
2120 switch (getsubopt(&optarg
, type_subopts
,
2123 types
|= ZFS_TYPE_FILESYSTEM
;
2126 types
|= ZFS_TYPE_VOLUME
;
2129 types
|= ZFS_TYPE_SNAPSHOT
;
2132 types
= ZFS_TYPE_DATASET
;
2136 (void) fprintf(stderr
,
2137 gettext("invalid type '%s'\n"),
2144 (void) fprintf(stderr
, gettext("missing argument for "
2145 "'%c' option\n"), optopt
);
2149 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2159 fields
= default_fields
;
2162 * If "-o space" and no types were specified, don't display snapshots.
2164 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
2165 types
&= ~ZFS_TYPE_SNAPSHOT
;
2168 * If the user specifies '-o all', the zprop_get_list() doesn't
2169 * normally include the name of the dataset. For 'zfs list', we always
2170 * want this property to be first.
2172 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
2176 cb
.cb_scripted
= scripted
;
2177 cb
.cb_first
= B_TRUE
;
2179 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
2180 limit
, list_callback
, &cb
);
2182 zprop_free_list(cb
.cb_proplist
);
2183 zfs_free_sort_columns(sortcol
);
2185 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
2186 (void) printf(gettext("no datasets available\n"));
2192 * zfs rename <fs | snap | vol> <fs | snap | vol>
2193 * zfs rename -p <fs | vol> <fs | vol>
2194 * zfs rename -r <snap> <snap>
2196 * Renames the given dataset to another of the same type.
2198 * The '-p' flag creates all the non-existing ancestors of the target first.
2202 zfs_do_rename(int argc
, char **argv
)
2207 boolean_t recurse
= B_FALSE
;
2208 boolean_t parents
= B_FALSE
;
2211 while ((c
= getopt(argc
, argv
, "pr")) != -1) {
2221 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2230 /* check number of arguments */
2232 (void) fprintf(stderr
, gettext("missing source dataset "
2237 (void) fprintf(stderr
, gettext("missing target dataset "
2242 (void) fprintf(stderr
, gettext("too many arguments\n"));
2246 if (recurse
&& parents
) {
2247 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
2252 if (recurse
&& strchr(argv
[0], '@') == 0) {
2253 (void) fprintf(stderr
, gettext("source dataset for recursive "
2254 "rename must be a snapshot\n"));
2258 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
2259 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
2262 /* If we were asked and the name looks good, try to create ancestors. */
2263 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
2264 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
2269 ret
= (zfs_rename(zhp
, argv
[1], recurse
) != 0);
2278 * Promotes the given clone fs to be the parent
2282 zfs_do_promote(int argc
, char **argv
)
2288 if (argc
> 1 && argv
[1][0] == '-') {
2289 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2294 /* check number of arguments */
2296 (void) fprintf(stderr
, gettext("missing clone filesystem"
2301 (void) fprintf(stderr
, gettext("too many arguments\n"));
2305 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2309 ret
= (zfs_promote(zhp
) != 0);
2317 * zfs rollback [-rRf] <snapshot>
2319 * -r Delete any intervening snapshots before doing rollback
2320 * -R Delete any snapshots and their clones
2321 * -f ignored for backwards compatability
2323 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2324 * since then and making it the active dataset. If more recent snapshots exist,
2325 * the command will complain unless the '-r' flag is given.
2327 typedef struct rollback_cbdata
{
2333 boolean_t cb_recurse
;
2334 boolean_t cb_dependent
;
2335 } rollback_cbdata_t
;
2338 * Report any snapshots more recent than the one specified. Used when '-r' is
2339 * not specified. We reuse this same callback for the snapshot dependents - if
2340 * 'cb_dependent' is set, then this is a dependent and we should report it
2341 * without checking the transaction group.
2345 rollback_check(zfs_handle_t
*zhp
, void *data
)
2347 rollback_cbdata_t
*cbp
= data
;
2349 if (cbp
->cb_doclones
) {
2354 if (!cbp
->cb_dependent
) {
2355 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
2356 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2357 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
2360 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
2361 (void) fprintf(stderr
, gettext("cannot "
2362 "rollback to '%s': more recent snapshots "
2365 (void) fprintf(stderr
, gettext("use '-r' to "
2366 "force deletion of the following "
2372 if (cbp
->cb_recurse
) {
2373 cbp
->cb_dependent
= B_TRUE
;
2374 if (zfs_iter_dependents(zhp
, B_TRUE
,
2375 rollback_check
, cbp
) != 0) {
2379 cbp
->cb_dependent
= B_FALSE
;
2381 (void) fprintf(stderr
, "%s\n",
2386 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
2387 (void) fprintf(stderr
, gettext("cannot rollback to "
2388 "'%s': clones of previous snapshots exist\n"),
2390 (void) fprintf(stderr
, gettext("use '-R' to "
2391 "force deletion of the following clones and "
2397 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
2403 #endif /* HAVE_ZPL */
2406 zfs_do_rollback(int argc
, char **argv
)
2411 boolean_t force
= B_FALSE
;
2412 rollback_cbdata_t cb
= { 0 };
2413 zfs_handle_t
*zhp
, *snap
;
2414 char parentname
[ZFS_MAXNAMELEN
];
2418 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
2431 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2440 /* check number of arguments */
2442 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2446 (void) fprintf(stderr
, gettext("too many arguments\n"));
2450 /* open the snapshot */
2451 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
2454 /* open the parent dataset */
2455 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
2456 verify((delim
= strrchr(parentname
, '@')) != NULL
);
2458 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
2464 * Check for more recent snapshots and/or clones based on the presence
2467 cb
.cb_target
= argv
[0];
2468 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
2469 cb
.cb_first
= B_TRUE
;
2471 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
2474 if ((ret
= cb
.cb_error
) != 0)
2478 * Rollback parent to the given snapshot.
2480 ret
= zfs_rollback(zhp
, snap
, force
);
2496 * zfs set property=value { fs | snap | vol } ...
2498 * Sets the given property for all datasets specified on the command line.
2500 typedef struct set_cbdata
{
2506 set_callback(zfs_handle_t
*zhp
, void *data
)
2508 set_cbdata_t
*cbp
= data
;
2510 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
2511 switch (libzfs_errno(g_zfs
)) {
2512 case EZFS_MOUNTFAILED
:
2513 (void) fprintf(stderr
, gettext("property may be set "
2514 "but unable to remount filesystem\n"));
2516 case EZFS_SHARENFSFAILED
:
2517 (void) fprintf(stderr
, gettext("property may be set "
2518 "but unable to reshare filesystem\n"));
2527 zfs_do_set(int argc
, char **argv
)
2532 /* check for options */
2533 if (argc
> 1 && argv
[1][0] == '-') {
2534 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2539 /* check number of arguments */
2541 (void) fprintf(stderr
, gettext("missing property=value "
2546 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2550 /* validate property=value argument */
2551 cb
.cb_propname
= argv
[1];
2552 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
2553 (cb
.cb_value
[1] == '\0')) {
2554 (void) fprintf(stderr
, gettext("missing value in "
2555 "property=value argument\n"));
2559 *cb
.cb_value
= '\0';
2562 if (*cb
.cb_propname
== '\0') {
2563 (void) fprintf(stderr
,
2564 gettext("missing property in property=value argument\n"));
2568 ret
= zfs_for_each(argc
- 2, argv
+ 2, 0,
2569 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
2575 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2577 * Creates a snapshot with the given name. While functionally equivalent to
2578 * 'zfs create', it is a separate command to differentiate intent.
2581 zfs_do_snapshot(int argc
, char **argv
)
2583 boolean_t recursive
= B_FALSE
;
2588 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2592 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
2595 if (parseprop(props
))
2602 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2611 /* check number of arguments */
2613 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2617 (void) fprintf(stderr
, gettext("too many arguments\n"));
2621 ret
= zfs_snapshot(g_zfs
, argv
[0], recursive
, props
);
2623 if (ret
&& recursive
)
2624 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
2634 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
2635 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
2637 * Send a backup stream to stdout.
2640 zfs_do_send(int argc
, char **argv
)
2642 char *fromname
= NULL
;
2643 char *toname
= NULL
;
2646 sendflags_t flags
= { 0 };
2649 boolean_t extraverbose
= B_FALSE
;
2652 while ((c
= getopt(argc
, argv
, ":i:I:RDpv")) != -1) {
2663 flags
.doall
= B_TRUE
;
2666 flags
.replicate
= B_TRUE
;
2669 flags
.props
= B_TRUE
;
2673 extraverbose
= B_TRUE
;
2674 flags
.verbose
= B_TRUE
;
2677 flags
.dedup
= B_TRUE
;
2680 (void) fprintf(stderr
, gettext("missing argument for "
2681 "'%c' option\n"), optopt
);
2685 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2694 /* check number of arguments */
2696 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2700 (void) fprintf(stderr
, gettext("too many arguments\n"));
2704 if (isatty(STDOUT_FILENO
)) {
2705 (void) fprintf(stderr
,
2706 gettext("Error: Stream can not be written to a terminal.\n"
2707 "You must redirect standard output.\n"));
2711 cp
= strchr(argv
[0], '@');
2713 (void) fprintf(stderr
,
2714 gettext("argument must be a snapshot\n"));
2719 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2724 * If they specified the full path to the snapshot, chop off
2725 * everything except the short name of the snapshot, but special
2726 * case if they specify the origin.
2728 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
2729 char origin
[ZFS_MAXNAMELEN
];
2732 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
2733 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
2735 if (strcmp(origin
, fromname
) == 0) {
2737 flags
.fromorigin
= B_TRUE
;
2740 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
2741 (void) fprintf(stderr
,
2742 gettext("incremental source must be "
2743 "in same filesystem\n"));
2747 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
2748 (void) fprintf(stderr
,
2749 gettext("invalid incremental source\n"));
2755 if (flags
.replicate
&& fromname
== NULL
)
2756 flags
.doall
= B_TRUE
;
2758 err
= zfs_send(zhp
, fromname
, toname
, flags
, STDOUT_FILENO
, NULL
, 0,
2759 extraverbose
? &dbgnv
: NULL
);
2763 * dump_nvlist prints to stdout, but that's been
2764 * redirected to a file. Make it print to stderr
2767 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
2768 dump_nvlist(dbgnv
, 0);
2777 * zfs receive [-vnFu] [-d | -e] <fs@snap>
2779 * Restore a backup stream from stdin.
2782 zfs_do_receive(int argc
, char **argv
)
2785 recvflags_t flags
= { 0 };
2788 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
2791 flags
.isprefix
= B_TRUE
;
2794 flags
.isprefix
= B_TRUE
;
2795 flags
.istail
= B_TRUE
;
2798 flags
.dryrun
= B_TRUE
;
2801 flags
.nomount
= B_TRUE
;
2804 flags
.verbose
= B_TRUE
;
2807 flags
.force
= B_TRUE
;
2810 (void) fprintf(stderr
, gettext("missing argument for "
2811 "'%c' option\n"), optopt
);
2815 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2824 /* check number of arguments */
2826 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2830 (void) fprintf(stderr
, gettext("too many arguments\n"));
2834 if (isatty(STDIN_FILENO
)) {
2835 (void) fprintf(stderr
,
2836 gettext("Error: Backup stream can not be read "
2837 "from a terminal.\n"
2838 "You must redirect standard input.\n"));
2842 err
= zfs_receive(g_zfs
, argv
[0], flags
, STDIN_FILENO
, NULL
);
2848 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
2853 boolean_t recursive
= B_FALSE
;
2854 boolean_t temphold
= B_FALSE
;
2855 const char *opts
= holding
? "rt" : "r";
2859 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
2868 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2877 /* check number of arguments */
2885 if (holding
&& tag
[0] == '.') {
2886 /* tags starting with '.' are reserved for libzfs */
2887 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
2891 for (i
= 0; i
< argc
; ++i
) {
2893 char parent
[ZFS_MAXNAMELEN
];
2895 char *path
= argv
[i
];
2897 delim
= strchr(path
, '@');
2898 if (delim
== NULL
) {
2899 (void) fprintf(stderr
,
2900 gettext("'%s' is not a snapshot\n"), path
);
2904 (void) strncpy(parent
, path
, delim
- path
);
2905 parent
[delim
- path
] = '\0';
2907 zhp
= zfs_open(g_zfs
, parent
,
2908 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2914 if (zfs_hold(zhp
, delim
+1, tag
, recursive
,
2915 temphold
, B_FALSE
, -1, 0, 0) != 0)
2918 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
2924 return (errors
!= 0);
2928 * zfs hold [-r] [-t] <tag> <snap> ...
2930 * -r Recursively hold
2931 * -t Temporary hold (hidden option)
2933 * Apply a user-hold with the given tag to the list of snapshots.
2936 zfs_do_hold(int argc
, char **argv
)
2938 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
2942 * zfs release [-r] <tag> <snap> ...
2944 * -r Recursively release
2946 * Release a user-hold with the given tag from the list of snapshots.
2949 zfs_do_release(int argc
, char **argv
)
2951 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
2954 #define CHECK_SPINNER 30
2955 #define SPINNER_TIME 3 /* seconds */
2956 #define MOUNT_TIME 5 /* seconds */
2960 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
2962 static char *spin
[] = { "-", "\\", "|", "/" };
2963 static int spinval
= 0;
2964 static int spincheck
= 0;
2965 static time_t last_spin_time
= (time_t)0;
2966 get_all_cb_t
*cbp
= data
;
2967 zfs_type_t type
= zfs_get_type(zhp
);
2969 if (cbp
->cb_verbose
) {
2970 if (--spincheck
< 0) {
2971 time_t now
= time(NULL
);
2972 if (last_spin_time
+ SPINNER_TIME
< now
) {
2973 update_progress(spin
[spinval
++ % 4]);
2974 last_spin_time
= now
;
2976 spincheck
= CHECK_SPINNER
;
2981 * Interate over any nested datasets.
2983 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
2989 * Skip any datasets whose type does not match.
2991 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
2995 libzfs_add_handle(cbp
, zhp
);
2996 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
3002 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
3004 get_all_cb_t cb
= { 0 };
3005 cb
.cb_verbose
= verbose
;
3006 cb
.cb_getone
= get_one_dataset
;
3009 set_progress_header(gettext("Reading ZFS config"));
3010 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
3012 *dslist
= cb
.cb_handles
;
3013 *count
= cb
.cb_used
;
3016 finish_progress(gettext("done."));
3020 * Generic callback for sharing or mounting filesystems. Because the code is so
3021 * similar, we have a common function with an extra parameter to determine which
3022 * mode we are using.
3024 #define OP_SHARE 0x1
3025 #define OP_MOUNT 0x2
3028 * Share or mount a dataset.
3031 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
3032 boolean_t
explicit, const char *options
)
3034 char mountpoint
[ZFS_MAXPROPLEN
];
3035 char shareopts
[ZFS_MAXPROPLEN
];
3036 char smbshareopts
[ZFS_MAXPROPLEN
];
3037 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
3039 uint64_t zoned
, canmount
;
3040 boolean_t shared_nfs
, shared_smb
;
3042 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
3045 * Check to make sure we can mount/share this dataset. If we
3046 * are in the global zone and the filesystem is exported to a
3047 * local zone, or if we are in a local zone and the
3048 * filesystem is not exported, then it is an error.
3050 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3052 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
3056 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3057 "dataset is exported to a local zone\n"), cmdname
,
3061 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
3065 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3066 "permission denied\n"), cmdname
,
3072 * Ignore any filesystems which don't apply to us. This
3073 * includes those with a legacy mountpoint, or those with
3074 * legacy share options.
3076 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
3077 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
3078 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
3079 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3080 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
3081 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3083 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
3084 strcmp(smbshareopts
, "off") == 0) {
3088 (void) fprintf(stderr
, gettext("cannot share '%s': "
3089 "legacy share\n"), zfs_get_name(zhp
));
3090 (void) fprintf(stderr
, gettext("use share(1M) to "
3091 "share this filesystem, or set "
3092 "sharenfs property on\n"));
3097 * We cannot share or mount legacy filesystems. If the
3098 * shareopts is non-legacy but the mountpoint is legacy, we
3099 * treat it as a legacy share.
3101 if (strcmp(mountpoint
, "legacy") == 0) {
3105 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3106 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
3107 (void) fprintf(stderr
, gettext("use %s(1M) to "
3108 "%s this filesystem\n"), cmdname
, cmdname
);
3112 if (strcmp(mountpoint
, "none") == 0) {
3116 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
3117 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
3122 * canmount explicit outcome
3123 * on no pass through
3124 * on yes pass through
3126 * off yes display error, return 1
3127 * noauto no return 0
3128 * noauto yes pass through
3130 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
3131 if (canmount
== ZFS_CANMOUNT_OFF
) {
3135 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3136 "'canmount' property is set to 'off'\n"), cmdname
,
3139 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
3144 * At this point, we have verified that the mountpoint and/or
3145 * shareopts are appropriate for auto management. If the
3146 * filesystem is already mounted or shared, return (failing
3147 * for explicit requests); otherwise mount or share the
3153 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
3154 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
3156 if ((shared_nfs
&& shared_smb
) ||
3157 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
3158 (strcmp(smbshareopts
, "off") == 0)) ||
3159 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
3160 (strcmp(shareopts
, "off") == 0))) {
3164 (void) fprintf(stderr
, gettext("cannot share "
3165 "'%s': filesystem already shared\n"),
3170 if (!zfs_is_mounted(zhp
, NULL
) &&
3171 zfs_mount(zhp
, NULL
, 0) != 0)
3174 if (protocol
== NULL
) {
3175 if (zfs_shareall(zhp
) != 0)
3177 } else if (strcmp(protocol
, "nfs") == 0) {
3178 if (zfs_share_nfs(zhp
))
3180 } else if (strcmp(protocol
, "smb") == 0) {
3181 if (zfs_share_smb(zhp
))
3184 (void) fprintf(stderr
, gettext("cannot share "
3185 "'%s': invalid share type '%s' "
3187 zfs_get_name(zhp
), protocol
);
3194 if (options
== NULL
)
3195 mnt
.mnt_mntopts
= "";
3197 mnt
.mnt_mntopts
= (char *)options
;
3199 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
3200 zfs_is_mounted(zhp
, NULL
)) {
3204 (void) fprintf(stderr
, gettext("cannot mount "
3205 "'%s': filesystem already mounted\n"),
3210 if (zfs_mount(zhp
, options
, flags
) != 0)
3219 * Reports progress in the form "(current/total)". Not thread-safe.
3222 report_mount_progress(int current
, int total
)
3224 static time_t last_progress_time
= 0;
3225 time_t now
= time(NULL
);
3228 /* report 1..n instead of 0..n-1 */
3231 /* display header if we're here for the first time */
3233 set_progress_header(gettext("Mounting ZFS filesystems"));
3234 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
3235 /* too soon to report again */
3239 last_progress_time
= now
;
3241 (void) sprintf(info
, "(%d/%d)", current
, total
);
3243 if (current
== total
)
3244 finish_progress(info
);
3246 update_progress(info
);
3250 append_options(char *mntopts
, char *newopts
)
3252 int len
= strlen(mntopts
);
3254 /* original length plus new string to append plus 1 for the comma */
3255 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
3256 (void) fprintf(stderr
, gettext("the opts argument for "
3257 "'%s' option is too long (more than %d chars)\n"),
3258 "-o", MNT_LINE_MAX
);
3263 mntopts
[len
++] = ',';
3265 (void) strcpy(&mntopts
[len
], newopts
);
3269 share_mount(int op
, int argc
, char **argv
)
3272 boolean_t verbose
= B_FALSE
;
3274 char *options
= NULL
;
3278 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
3288 if (*optarg
== '\0') {
3289 (void) fprintf(stderr
, gettext("empty mount "
3290 "options (-o) specified\n"));
3294 if (options
== NULL
)
3295 options
= safe_malloc(MNT_LINE_MAX
+ 1);
3297 /* option validation is done later */
3298 append_options(options
, optarg
);
3302 flags
|= MS_OVERLAY
;
3305 (void) fprintf(stderr
, gettext("missing argument for "
3306 "'%c' option\n"), optopt
);
3310 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3319 /* check number of arguments */
3321 zfs_handle_t
**dslist
= NULL
;
3322 size_t i
, count
= 0;
3323 char *protocol
= NULL
;
3325 if (op
== OP_SHARE
&& argc
> 0) {
3326 if (strcmp(argv
[0], "nfs") != 0 &&
3327 strcmp(argv
[0], "smb") != 0) {
3328 (void) fprintf(stderr
, gettext("share type "
3329 "must be 'nfs' or 'smb'\n"));
3338 (void) fprintf(stderr
, gettext("too many arguments\n"));
3342 start_progress_timer();
3343 get_all_datasets(&dslist
, &count
, verbose
);
3348 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
3350 for (i
= 0; i
< count
; i
++) {
3352 report_mount_progress(i
, count
);
3354 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
3355 B_FALSE
, options
) != 0)
3357 zfs_close(dslist
[i
]);
3361 } else if (argc
== 0) {
3362 struct mnttab entry
;
3364 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
3365 (void) fprintf(stderr
, gettext("missing filesystem "
3366 "argument (specify -a for all)\n"));
3371 * When mount is given no arguments, go through /etc/mnttab and
3372 * display any active ZFS mounts. We hide any snapshots, since
3373 * they are controlled automatically.
3375 rewind(mnttab_file
);
3376 while (getmntent(mnttab_file
, &entry
) == 0) {
3377 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
3378 strchr(entry
.mnt_special
, '@') != NULL
)
3381 (void) printf("%-30s %s\n", entry
.mnt_special
,
3389 (void) fprintf(stderr
,
3390 gettext("too many arguments\n"));
3394 if ((zhp
= zfs_open(g_zfs
, argv
[0],
3395 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3398 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
3406 #endif /* HAVE_ZPL */
3409 * zfs mount -a [nfs]
3410 * zfs mount filesystem
3412 * Mount all filesystems, or mount the given filesystem.
3415 zfs_do_mount(int argc
, char **argv
)
3418 return (share_mount(OP_MOUNT
, argc
, argv
));
3421 #endif /* HAVE_ZPL */
3425 * zfs share -a [nfs | smb]
3426 * zfs share filesystem
3428 * Share all filesystems, or share the given filesystem.
3431 zfs_do_share(int argc
, char **argv
)
3434 return (share_mount(OP_SHARE
, argc
, argv
));
3437 #endif /* HAVE_ZPL */
3441 typedef struct unshare_unmount_node
{
3442 zfs_handle_t
*un_zhp
;
3444 uu_avl_node_t un_avlnode
;
3445 } unshare_unmount_node_t
;
3449 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
3451 const unshare_unmount_node_t
*l
= larg
;
3452 const unshare_unmount_node_t
*r
= rarg
;
3454 return (strcmp(l
->un_mountp
, r
->un_mountp
));
3458 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3459 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3460 * and unmount it appropriately.
3463 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
3467 struct stat64 statbuf
;
3468 struct extmnttab entry
;
3469 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
3473 * Search for the path in /etc/mnttab. Rather than looking for the
3474 * specific path, which can be fooled by non-standard paths (i.e. ".."
3475 * or "//"), we stat() the path and search for the corresponding
3476 * (major,minor) device pair.
3478 if (stat64(path
, &statbuf
) != 0) {
3479 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3480 cmdname
, path
, strerror(errno
));
3483 path_inode
= statbuf
.st_ino
;
3486 * Search for the given (major,minor) pair in the mount table.
3488 rewind(mnttab_file
);
3489 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
3490 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
3491 entry
.mnt_minor
== minor(statbuf
.st_dev
))
3495 if (op
== OP_SHARE
) {
3496 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
3497 "currently mounted\n"), cmdname
, path
);
3500 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
3502 if ((ret
= umount2(path
, flags
)) != 0)
3503 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
3508 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
3509 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
3510 "filesystem\n"), cmdname
, path
);
3514 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3515 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3519 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
3520 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3521 cmdname
, path
, strerror(errno
));
3523 } else if (statbuf
.st_ino
!= path_inode
) {
3524 (void) fprintf(stderr
, gettext("cannot "
3525 "%s '%s': not a mountpoint\n"), cmdname
, path
);
3529 if (op
== OP_SHARE
) {
3530 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3531 char smbshare_prop
[ZFS_MAXPROPLEN
];
3533 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
3534 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3535 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
3536 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3538 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3539 strcmp(smbshare_prop
, "off") == 0) {
3540 (void) fprintf(stderr
, gettext("cannot unshare "
3541 "'%s': legacy share\n"), path
);
3542 (void) fprintf(stderr
, gettext("use "
3543 "unshare(1M) to unshare this filesystem\n"));
3544 } else if (!zfs_is_shared(zhp
)) {
3545 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
3546 "not currently shared\n"), path
);
3548 ret
= zfs_unshareall_bypath(zhp
, path
);
3551 char mtpt_prop
[ZFS_MAXPROPLEN
];
3553 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
3554 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3557 ret
= zfs_unmount(zhp
, NULL
, flags
);
3558 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
3559 (void) fprintf(stderr
, gettext("cannot unmount "
3560 "'%s': legacy mountpoint\n"),
3562 (void) fprintf(stderr
, gettext("use umount(1M) "
3563 "to unmount this filesystem\n"));
3565 ret
= zfs_unmountall(zhp
, flags
);
3576 * Generic callback for unsharing or unmounting a filesystem.
3579 unshare_unmount(int op
, int argc
, char **argv
)
3586 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3587 char sharesmb
[ZFS_MAXPROPLEN
];
3590 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
3599 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3610 * We could make use of zfs_for_each() to walk all datasets in
3611 * the system, but this would be very inefficient, especially
3612 * since we would have to linearly search /etc/mnttab for each
3613 * one. Instead, do one pass through /etc/mnttab looking for
3614 * zfs entries and call zfs_unmount() for each one.
3616 * Things get a little tricky if the administrator has created
3617 * mountpoints beneath other ZFS filesystems. In this case, we
3618 * have to unmount the deepest filesystems first. To accomplish
3619 * this, we place all the mountpoints in an AVL tree sorted by
3620 * the special type (dataset name), and walk the result in
3621 * reverse to make sure to get any snapshots first.
3623 struct mnttab entry
;
3624 uu_avl_pool_t
*pool
;
3625 uu_avl_t
*tree
= NULL
;
3626 unshare_unmount_node_t
*node
;
3628 uu_avl_walk_t
*walk
;
3631 (void) fprintf(stderr
, gettext("too many arguments\n"));
3635 if (((pool
= uu_avl_pool_create("unmount_pool",
3636 sizeof (unshare_unmount_node_t
),
3637 offsetof(unshare_unmount_node_t
, un_avlnode
),
3638 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
3639 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
3642 rewind(mnttab_file
);
3643 while (getmntent(mnttab_file
, &entry
) == 0) {
3645 /* ignore non-ZFS entries */
3646 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
3649 /* ignore snapshots */
3650 if (strchr(entry
.mnt_special
, '@') != NULL
)
3653 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3654 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3661 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3663 sizeof (nfs_mnt_prop
),
3664 NULL
, NULL
, 0, B_FALSE
) == 0);
3665 if (strcmp(nfs_mnt_prop
, "off") != 0)
3667 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3669 sizeof (nfs_mnt_prop
),
3670 NULL
, NULL
, 0, B_FALSE
) == 0);
3671 if (strcmp(nfs_mnt_prop
, "off") == 0)
3675 /* Ignore legacy mounts */
3676 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
3678 sizeof (nfs_mnt_prop
),
3679 NULL
, NULL
, 0, B_FALSE
) == 0);
3680 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
3682 /* Ignore canmount=noauto mounts */
3683 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
3684 ZFS_CANMOUNT_NOAUTO
)
3690 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
3692 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
3694 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
3696 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
3697 uu_avl_insert(tree
, node
, idx
);
3699 zfs_close(node
->un_zhp
);
3700 free(node
->un_mountp
);
3706 * Walk the AVL tree in reverse, unmounting each filesystem and
3707 * removing it from the AVL tree in the process.
3709 if ((walk
= uu_avl_walk_start(tree
,
3710 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
3713 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3714 uu_avl_remove(tree
, node
);
3718 if (zfs_unshareall_bypath(node
->un_zhp
,
3719 node
->un_mountp
) != 0)
3724 if (zfs_unmount(node
->un_zhp
,
3725 node
->un_mountp
, flags
) != 0)
3730 zfs_close(node
->un_zhp
);
3731 free(node
->un_mountp
);
3735 uu_avl_walk_end(walk
);
3736 uu_avl_destroy(tree
);
3737 uu_avl_pool_destroy(pool
);
3742 (void) fprintf(stderr
,
3743 gettext("missing filesystem argument\n"));
3745 (void) fprintf(stderr
,
3746 gettext("too many arguments\n"));
3751 * We have an argument, but it may be a full path or a ZFS
3752 * filesystem. Pass full paths off to unmount_path() (shared by
3753 * manual_unmount), otherwise open the filesystem and pass to
3756 if (argv
[0][0] == '/')
3757 return (unshare_unmount_path(op
, argv
[0],
3760 if ((zhp
= zfs_open(g_zfs
, argv
[0],
3761 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3764 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
3765 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
3766 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
3767 NULL
, 0, B_FALSE
) == 0);
3771 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3773 sizeof (nfs_mnt_prop
),
3774 NULL
, NULL
, 0, B_FALSE
) == 0);
3775 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3776 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
3779 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3780 strcmp(sharesmb
, "off") == 0) {
3781 (void) fprintf(stderr
, gettext("cannot "
3782 "unshare '%s': legacy share\n"),
3784 (void) fprintf(stderr
, gettext("use "
3785 "unshare(1M) to unshare this "
3788 } else if (!zfs_is_shared(zhp
)) {
3789 (void) fprintf(stderr
, gettext("cannot "
3790 "unshare '%s': not currently "
3791 "shared\n"), zfs_get_name(zhp
));
3793 } else if (zfs_unshareall(zhp
) != 0) {
3799 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
3800 (void) fprintf(stderr
, gettext("cannot "
3801 "unmount '%s': legacy "
3802 "mountpoint\n"), zfs_get_name(zhp
));
3803 (void) fprintf(stderr
, gettext("use "
3804 "umount(1M) to unmount this "
3807 } else if (!zfs_is_mounted(zhp
, NULL
)) {
3808 (void) fprintf(stderr
, gettext("cannot "
3809 "unmount '%s': not currently "
3813 } else if (zfs_unmountall(zhp
, flags
) != 0) {
3824 #endif /* HAVE_ZPL */
3828 * zfs unmount filesystem
3830 * Unmount all filesystems, or a specific ZFS filesystem.
3833 zfs_do_unmount(int argc
, char **argv
)
3836 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
3839 #endif /* HAVE_ZPL */
3844 * zfs unshare filesystem
3846 * Unshare all filesystems, or a specific ZFS filesystem.
3849 zfs_do_unshare(int argc
, char **argv
)
3852 return (unshare_unmount(OP_SHARE
, argc
, argv
));
3855 #endif /* HAVE_ZPL */
3860 zfs_do_python(int argc
, char **argv
)
3862 (void) execv(pypath
, argv
-1);
3863 (void) printf("internal error: %s not found\n", pypath
);
3867 typedef struct option_map
{
3872 static const option_map_t option_map
[] = {
3873 /* Canonicalized filesystem independent options from mount(8) */
3874 { MNTOPT_NOAUTO
, MS_COMMENT
},
3875 { MNTOPT_DEFAULTS
, MS_COMMENT
},
3876 { MNTOPT_NODEVICES
, MS_NODEV
},
3877 { MNTOPT_DIRSYNC
, MS_DIRSYNC
},
3878 { MNTOPT_NOEXEC
, MS_NOEXEC
},
3879 { MNTOPT_GROUP
, MS_GROUP
},
3880 { MNTOPT_NETDEV
, MS_COMMENT
},
3881 { MNTOPT_NOFAIL
, MS_COMMENT
},
3882 { MNTOPT_NOSUID
, MS_NOSUID
},
3883 { MNTOPT_OWNER
, MS_OWNER
},
3884 { MNTOPT_REMOUNT
, MS_REMOUNT
},
3885 { MNTOPT_RO
, MS_RDONLY
},
3886 { MNTOPT_SYNC
, MS_SYNCHRONOUS
},
3887 { MNTOPT_USER
, MS_USERS
},
3888 { MNTOPT_USERS
, MS_USERS
},
3890 { MNTOPT_NOATIME
, MS_NOATIME
},
3892 #ifdef MS_NODIRATIME
3893 { MNTOPT_NODIRATIME
, MS_NODIRATIME
},
3896 { MNTOPT_RELATIME
, MS_RELATIME
},
3898 #ifdef MS_STRICTATIME
3899 { MNTOPT_DFRATIME
, MS_STRICTATIME
},
3902 { MNTOPT_CONTEXT
, MS_COMMENT
},
3903 { MNTOPT_FSCONTEXT
, MS_COMMENT
},
3904 { MNTOPT_DEFCONTEXT
, MS_COMMENT
},
3905 { MNTOPT_ROOTCONTEXT
, MS_COMMENT
},
3908 { MNTOPT_IVERSION
, MS_I_VERSION
},
3911 { MNTOPT_NBMAND
, MS_MANDLOCK
},
3913 /* Valid options not found in mount(8) */
3914 { MNTOPT_BIND
, MS_BIND
},
3915 { MNTOPT_RBIND
, MS_BIND
|MS_REC
},
3916 { MNTOPT_COMMENT
, MS_COMMENT
},
3917 { MNTOPT_BOOTWAIT
, MS_COMMENT
},
3918 { MNTOPT_NOBOOTWAIT
, MS_COMMENT
},
3919 { MNTOPT_OPTIONAL
, MS_COMMENT
},
3920 { MNTOPT_SHOWTHROUGH
, MS_COMMENT
},
3922 { MNTOPT_NOSUB
, MS_NOSUB
},
3925 { MNTOPT_QUIET
, MS_SILENT
},
3927 /* Custom zfs options */
3928 { MNTOPT_NOXATTR
, MS_COMMENT
},
3932 * Break the mount option in to a name/value pair. The name is
3933 * validated against the option map and mount flags set accordingly.
3936 parse_option(char *mntopt
, unsigned long *mntflags
, int sloppy
)
3938 const option_map_t
*opt
;
3939 char *ptr
, *name
, *value
= NULL
;
3942 name
= strdup(mntopt
);
3946 for (ptr
= name
; ptr
&& *ptr
; ptr
++) {
3954 for (opt
= option_map
; opt
->name
!= NULL
; opt
++) {
3955 if (strncmp(name
, opt
->name
, strlen(name
)) == 0) {
3956 *mntflags
|= opt
->mask
;
3958 /* MS_USERS implies default user options */
3959 if (opt
->mask
& (MS_USERS
))
3960 *mntflags
|= (MS_NOEXEC
|MS_NOSUID
|MS_NODEV
);
3962 /* MS_OWNER|MS_GROUP imply default owner options */
3963 if (opt
->mask
& (MS_OWNER
| MS_GROUP
))
3964 *mntflags
|= (MS_NOSUID
|MS_NODEV
);
3974 /* If required further process on the value may be done here */
3980 * Translate the mount option string in to MS_* mount flags for the
3981 * kernel vfs. When sloppy is non-zero unknown options will be ignored
3982 * otherwise they are considered fatal are copied in to badopt.
3985 parse_options(char *mntopts
, unsigned long *mntflags
, int sloppy
, char *badopt
)
3987 int rc
= 0, quote
= 0;
3988 char *ptr
, *opt
, *opts
;
3990 opts
= strdup(mntopts
);
3998 * Scan through all mount options which must be comma delimited.
3999 * We must be careful to notice regions which are double quoted
4000 * and skip commas in these regions. Each option is then checked
4001 * to determine if it is a known option.
4003 for (ptr
= opts
; ptr
&& *ptr
; ptr
++) {
4013 if ((*ptr
== ',') || (*ptr
== '\0')) {
4015 rc
= parse_option(opt
, mntflags
, sloppy
);
4017 strcpy(badopt
, opt
);
4030 * Called when invoked as /sbin/mount.zfs, mount helper for mount(8).
4033 manual_mount(int argc
, char **argv
)
4036 char legacy
[ZFS_MAXPROPLEN
];
4037 char mntopts
[MNT_LINE_MAX
] = { '\0' };
4038 char badopt
[MNT_LINE_MAX
] = { '\0' };
4039 char *dataset
, *mntpoint
;
4040 unsigned long mntflags
;
4041 int sloppy
= 0, fake
= 0, verbose
= 0;
4045 while ((c
= getopt(argc
, argv
, "sfnvo:h?")) != -1) {
4054 /* Ignored, handled by mount(8) */
4060 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
4064 (void) fprintf(stderr
, gettext("Invalid option '%c'\n"),
4066 (void) fprintf(stderr
, gettext("Usage: mount.zfs "
4067 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
4068 return (MOUNT_USAGE
);
4075 /* check that we only have two arguments */
4078 (void) fprintf(stderr
, gettext("missing dataset "
4081 (void) fprintf(stderr
,
4082 gettext("missing mountpoint argument\n"));
4084 (void) fprintf(stderr
, gettext("too many arguments\n"));
4085 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
4086 return (MOUNT_USAGE
);
4092 /* try to open the dataset to access the mount point */
4093 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
) {
4094 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
4095 "mounted, unable to open the dataset\n"), dataset
);
4096 return (MOUNT_USAGE
);
4099 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, legacy
,
4100 sizeof (legacy
), NULL
, NULL
, 0, B_FALSE
);
4104 /* check for legacy mountpoint or util mount option */
4105 if ((!strcmp(legacy
, ZFS_MOUNTPOINT_LEGACY
) == 0) &&
4106 (strstr(mntopts
, MNTOPT_ZFSUTIL
) == NULL
)) {
4107 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
4108 "mounted using 'mount -a -t zfs'\n"), dataset
);
4109 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
4110 "instead.\n"), mntpoint
);
4111 (void) fprintf(stderr
, gettext("If you must use 'mount -a -t "
4112 "zfs' or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
4113 (void) fprintf(stderr
, gettext("See zfs(8) for more "
4115 return (MOUNT_USAGE
);
4118 /* validate mount options and set mntflags */
4119 rc
= parse_options(mntopts
, &mntflags
, sloppy
, badopt
);
4123 (void) fprintf(stderr
, gettext("filesystem '%s' "
4124 "cannot be mounted due to a memory allocation "
4125 "failure\n"), dataset
);
4126 return (MOUNT_SYSERR
);
4128 (void) fprintf(stderr
, gettext("filesystem '%s' "
4129 "cannot be mounted of due to the invalid option "
4130 "'%s'\n"), dataset
, badopt
);
4131 (void) fprintf(stderr
, gettext("Use the '-s' option "
4132 "to ignore the bad mount option.\n"));
4133 return (MOUNT_USAGE
);
4135 (void) fprintf(stderr
, gettext("filesystem '%s' "
4136 "cannot be mounted due to internal error %d\n"),
4138 return (MOUNT_SOFTWARE
);
4143 printf("mount.zfs: dataset: \"%s\", mountpoint: \"%s\" "
4144 "mountflags: 0x%lx, mountopts: \"%s\"\n", dataset
,
4145 mntpoint
, mntflags
, mntopts
);
4147 /* load the zfs posix layer module (zpl) */
4148 if (libzfs_load_module("zpl")) {
4149 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
4150 "mounted without the zpl kernel module\n"), dataset
);
4151 (void) fprintf(stderr
, gettext("Use 'dmesg' to determine why "
4152 "the module could not be loaded.\n"));
4153 return (MOUNT_SYSERR
);
4157 rc
= mount(dataset
, mntpoint
, MNTTYPE_ZFS
, mntflags
, mntopts
);
4159 (void) fprintf(stderr
, gettext("filesystem '%s' can"
4160 "not be mounted due to error %d\n"), dataset
, rc
);
4161 return (MOUNT_USAGE
);
4165 return (MOUNT_SUCCESS
);
4168 #ifdef HAVE_UNMOUNT_HELPER
4170 * Called when invoked as /sbin/umount.zfs, mount helper for mount(8).
4171 * Unlike a manual mount, we allow unmounts of non-legacy filesystems,
4172 * as this is the dominant administrative interface.
4175 manual_unmount(int argc
, char **argv
)
4177 int verbose
= 0, flags
= 0;
4181 while ((c
= getopt(argc
, argv
, "nlfvrh?")) != -1) {
4184 /* Ignored, handled by mount(8) */
4196 /* Remount read-only on umount failure, unsupported */
4197 (void) fprintf(stderr
, gettext("Unsupported option "
4199 return (MOUNT_USAGE
);
4202 (void) fprintf(stderr
, gettext("Invalid option '%c'\n"),
4204 (void) fprintf(stderr
, gettext("Usage: umount.zfs "
4205 "[-nlfvr] <mountpoint>\n"));
4206 return (MOUNT_USAGE
);
4213 /* check that we only have one argument */
4216 (void) fprintf(stderr
, gettext("missing mountpoint "
4219 (void) fprintf(stderr
, gettext("too many arguments\n"));
4221 (void) fprintf(stderr
, gettext("Usage: umount.zfs [-nlfvr] "
4223 return (MOUNT_USAGE
);
4226 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
4228 #endif /* HAVE_UNMOUNT_HELPER */
4231 find_command_idx(char *command
, int *idx
)
4235 for (i
= 0; i
< NCOMMAND
; i
++) {
4236 if (command_table
[i
].name
== NULL
)
4239 if (strcmp(command
, command_table
[i
].name
) == 0) {
4248 zfs_do_diff(int argc
, char **argv
)
4252 char *tosnap
= NULL
;
4253 char *fromsnap
= NULL
;
4258 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
4261 flags
|= ZFS_DIFF_CLASSIFY
;
4264 flags
|= ZFS_DIFF_PARSEABLE
;
4267 flags
|= ZFS_DIFF_TIMESTAMP
;
4270 (void) fprintf(stderr
,
4271 gettext("invalid option '%c'\n"), optopt
);
4280 (void) fprintf(stderr
,
4281 gettext("must provide at least one snapshot name\n"));
4286 (void) fprintf(stderr
, gettext("too many arguments\n"));
4291 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
4294 if (*fromsnap
!= '@')
4295 copy
= strdup(fromsnap
);
4297 copy
= strdup(tosnap
);
4301 if ((atp
= strchr(copy
, '@')))
4304 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
4310 * Ignore SIGPIPE so that the library can give us
4311 * information on any failure
4313 (void) sigignore(SIGPIPE
);
4315 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
4323 main(int argc
, char **argv
)
4330 (void) setlocale(LC_ALL
, "");
4331 (void) textdomain(TEXT_DOMAIN
);
4335 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
4336 (void) fprintf(stderr
, gettext("internal error: unable to "
4337 "open %s\n"), MNTTAB
);
4342 * This command also doubles as the /etc/fs mount and unmount program.
4343 * Determine if we should take this behavior based on argv[0].
4345 progname
= basename(argv
[0]);
4346 if (strcmp(progname
, "mount.zfs") == 0) {
4347 ret
= manual_mount(argc
, argv
);
4348 #ifdef HAVE_UNMOUNT_HELPER
4349 } else if (strcmp(progname
, "umount.zfs") == 0) {
4350 ret
= manual_unmount(argc
, argv
);
4351 #endif /* HAVE_UNMOUNT_HELPER */
4354 * Make sure the user has specified some command.
4357 (void) fprintf(stderr
, gettext("missing command\n"));
4364 * The 'umount' command is an alias for 'unmount'
4366 if (strcmp(cmdname
, "umount") == 0)
4367 cmdname
= "unmount";
4370 * The 'recv' command is an alias for 'receive'
4372 if (strcmp(cmdname
, "recv") == 0)
4373 cmdname
= "receive";
4378 if ((strcmp(cmdname
, "-?") == 0) ||
4379 (strcmp(cmdname
, "--help") == 0))
4382 if ((g_zfs
= libzfs_init()) == NULL
)
4385 zpool_set_history_str("zfs", argc
, argv
, history_str
);
4386 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
4388 libzfs_print_on_error(g_zfs
, B_TRUE
);
4391 * Run the appropriate command.
4393 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
4394 if (find_command_idx(cmdname
, &i
) == 0) {
4395 current_command
= &command_table
[i
];
4396 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
4397 } else if (strchr(cmdname
, '=') != NULL
) {
4398 verify(find_command_idx("set", &i
) == 0);
4399 current_command
= &command_table
[i
];
4400 ret
= command_table
[i
].func(argc
, argv
);
4402 (void) fprintf(stderr
, gettext("unrecognized "
4403 "command '%s'\n"), cmdname
);
4407 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
4410 (void) fclose(mnttab_file
);
4415 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4416 * for the purposes of running ::findleaks.
4418 if (getenv("ZFS_ABORT") != NULL
) {
4419 (void) printf("dumping core by request\n");