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.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
33 #include <libnvpair.h>
45 #include <sys/mkdev.h>
46 #include <sys/mntent.h>
47 #include <sys/mnttab.h>
48 #include <sys/mount.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/types.h>
59 #include "zfs_comutil.h"
61 libzfs_handle_t
*g_zfs
;
63 static FILE *mnttab_file
;
64 static char history_str
[HIS_MAX_RECORD_LEN
];
65 const char *pypath
= "/usr/lib/zfs/pyzfs.py";
67 static int zfs_do_clone(int argc
, char **argv
);
68 static int zfs_do_create(int argc
, char **argv
);
69 static int zfs_do_destroy(int argc
, char **argv
);
70 static int zfs_do_get(int argc
, char **argv
);
71 static int zfs_do_inherit(int argc
, char **argv
);
72 static int zfs_do_list(int argc
, char **argv
);
73 static int zfs_do_mount(int argc
, char **argv
);
74 static int zfs_do_rename(int argc
, char **argv
);
75 static int zfs_do_rollback(int argc
, char **argv
);
76 static int zfs_do_set(int argc
, char **argv
);
77 static int zfs_do_upgrade(int argc
, char **argv
);
78 static int zfs_do_snapshot(int argc
, char **argv
);
79 static int zfs_do_unmount(int argc
, char **argv
);
80 static int zfs_do_share(int argc
, char **argv
);
81 static int zfs_do_unshare(int argc
, char **argv
);
82 static int zfs_do_send(int argc
, char **argv
);
83 static int zfs_do_receive(int argc
, char **argv
);
84 static int zfs_do_promote(int argc
, char **argv
);
85 static int zfs_do_userspace(int argc
, char **argv
);
86 static int zfs_do_python(int argc
, char **argv
);
87 static int zfs_do_hold(int argc
, char **argv
);
88 static int zfs_do_release(int argc
, char **argv
);
89 static int zfs_do_diff(int argc
, char **argv
);
92 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
97 _umem_debug_init(void)
99 return ("default,verbose"); /* $UMEM_DEBUG setting */
103 _umem_logging_init(void)
105 return ("fail,contents"); /* $UMEM_LOGGING setting */
138 typedef struct zfs_command
{
140 int (*func
)(int argc
, char **argv
);
145 * Master command table. Each ZFS command has a name, associated function, and
146 * usage message. The usage messages need to be internationalized, so we have
147 * to have a function to return the usage message based on a command index.
149 * These commands are organized according to how they are displayed in the usage
150 * message. An empty command (one with a NULL name) indicates an empty line in
151 * the generic usage message.
153 static zfs_command_t command_table
[] = {
154 { "create", zfs_do_create
, HELP_CREATE
},
155 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
157 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
158 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
159 { "clone", zfs_do_clone
, HELP_CLONE
},
160 { "promote", zfs_do_promote
, HELP_PROMOTE
},
161 { "rename", zfs_do_rename
, HELP_RENAME
},
163 { "list", zfs_do_list
, HELP_LIST
},
165 { "set", zfs_do_set
, HELP_SET
},
166 { "get", zfs_do_get
, HELP_GET
},
167 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
168 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
169 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
170 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
172 { "mount", zfs_do_mount
, HELP_MOUNT
},
173 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
174 { "share", zfs_do_share
, HELP_SHARE
},
175 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
177 { "send", zfs_do_send
, HELP_SEND
},
178 { "receive", zfs_do_receive
, HELP_RECEIVE
},
180 { "allow", zfs_do_python
, HELP_ALLOW
},
182 { "unallow", zfs_do_python
, HELP_UNALLOW
},
184 { "hold", zfs_do_hold
, HELP_HOLD
},
185 { "holds", zfs_do_python
, HELP_HOLDS
},
186 { "release", zfs_do_release
, HELP_RELEASE
},
187 { "diff", zfs_do_diff
, HELP_DIFF
},
190 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
192 zfs_command_t
*current_command
;
195 get_usage(zfs_help_t idx
)
199 return (gettext("\tclone [-p] [-o property=value] ... "
200 "<snapshot> <filesystem|volume>\n"));
202 return (gettext("\tcreate [-p] [-o property=value] ... "
204 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
205 "-V <size> <volume>\n"));
207 return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
208 "\tdestroy [-rRd] <snapshot>\n"));
210 return (gettext("\tget [-rHp] [-d max] "
211 "[-o \"all\" | field[,...]] [-s source[,...]]\n"
212 "\t <\"all\" | property[,...]> "
213 "[filesystem|volume|snapshot] ...\n"));
215 return (gettext("\tinherit [-rS] <property> "
216 "<filesystem|volume|snapshot> ...\n"));
218 return (gettext("\tupgrade [-v]\n"
219 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
221 return (gettext("\tlist [-rH][-d max] "
222 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
223 "\t [-S property] ... "
224 "[filesystem|volume|snapshot] ...\n"));
226 return (gettext("\tmount\n"
227 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
229 return (gettext("\tpromote <clone-filesystem>\n"));
231 return (gettext("\treceive [-vnFu] <filesystem|volume|"
233 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
235 return (gettext("\trename <filesystem|volume|snapshot> "
236 "<filesystem|volume|snapshot>\n"
237 "\trename -p <filesystem|volume> <filesystem|volume>\n"
238 "\trename -r <snapshot> <snapshot>"));
240 return (gettext("\trollback [-rRf] <snapshot>\n"));
242 return (gettext("\tsend [-vRDp] [-[iI] snapshot] <snapshot>\n"));
244 return (gettext("\tset <property=value> "
245 "<filesystem|volume|snapshot> ...\n"));
247 return (gettext("\tshare <-a | filesystem>\n"));
249 return (gettext("\tsnapshot [-r] [-o property=value] ... "
250 "<filesystem@snapname|volume@snapname>\n"));
252 return (gettext("\tunmount [-f] "
253 "<-a | filesystem|mountpoint>\n"));
255 return (gettext("\tunshare "
256 "<-a | filesystem|mountpoint>\n"));
258 return (gettext("\tallow <filesystem|volume>\n"
260 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
261 "\t <filesystem|volume>\n"
262 "\tallow [-ld] -e <perm|@setname>[,...] "
263 "<filesystem|volume>\n"
264 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
265 "\tallow -s @setname <perm|@setname>[,...] "
266 "<filesystem|volume>\n"));
268 return (gettext("\tunallow [-rldug] "
269 "<\"everyone\"|user|group>[,...]\n"
270 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
271 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
272 "<filesystem|volume>\n"
273 "\tunallow [-r] -c [<perm|@setname>[,...]] "
274 "<filesystem|volume>\n"
275 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
276 "<filesystem|volume>\n"));
278 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
279 "[-sS field] ... [-t type[,...]]\n"
280 "\t <filesystem|snapshot>\n"));
281 case HELP_GROUPSPACE
:
282 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
283 "[-sS field] ... [-t type[,...]]\n"
284 "\t <filesystem|snapshot>\n"));
286 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
288 return (gettext("\tholds [-r] <snapshot> ...\n"));
290 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
292 return (gettext("\tdiff [-FHt] <snapshot> "
293 "[snapshot|filesystem]\n"));
303 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
308 * Utility function to guarantee malloc() success.
312 safe_malloc(size_t size
)
316 if ((data
= calloc(1, size
)) == NULL
)
323 safe_strdup(char *str
)
325 char *dupstr
= strdup(str
);
334 * Callback routine that will print out information for each of
338 usage_prop_cb(int prop
, void *cb
)
342 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
344 if (zfs_prop_readonly(prop
))
345 (void) fprintf(fp
, " NO ");
347 (void) fprintf(fp
, "YES ");
349 if (zfs_prop_inheritable(prop
))
350 (void) fprintf(fp
, " YES ");
352 (void) fprintf(fp
, " NO ");
354 if (zfs_prop_values(prop
) == NULL
)
355 (void) fprintf(fp
, "-\n");
357 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
363 * Display usage message. If we're inside a command, display only the usage for
364 * that command. Otherwise, iterate over the entire command table and display
365 * a complete usage message.
368 usage(boolean_t requested
)
371 boolean_t show_properties
= B_FALSE
;
372 FILE *fp
= requested
? stdout
: stderr
;
374 if (current_command
== NULL
) {
376 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
378 gettext("where 'command' is one of the following:\n\n"));
380 for (i
= 0; i
< NCOMMAND
; i
++) {
381 if (command_table
[i
].name
== NULL
)
382 (void) fprintf(fp
, "\n");
384 (void) fprintf(fp
, "%s",
385 get_usage(command_table
[i
].usage
));
388 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
389 "pool/[dataset/]*dataset[@name]\n"));
391 (void) fprintf(fp
, gettext("usage:\n"));
392 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
395 if (current_command
!= NULL
&&
396 (strcmp(current_command
->name
, "set") == 0 ||
397 strcmp(current_command
->name
, "get") == 0 ||
398 strcmp(current_command
->name
, "inherit") == 0 ||
399 strcmp(current_command
->name
, "list") == 0))
400 show_properties
= B_TRUE
;
402 if (show_properties
) {
404 gettext("\nThe following properties are supported:\n"));
406 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
407 "PROPERTY", "EDIT", "INHERIT", "VALUES");
409 /* Iterate over all properties */
410 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
413 (void) fprintf(fp
, "\t%-15s ", "userused@...");
414 (void) fprintf(fp
, " NO NO <size>\n");
415 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
416 (void) fprintf(fp
, " NO NO <size>\n");
417 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
418 (void) fprintf(fp
, "YES NO <size> | none\n");
419 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
420 (void) fprintf(fp
, "YES NO <size> | none\n");
422 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
423 "with standard units such as K, M, G, etc.\n"));
424 (void) fprintf(fp
, gettext("\nUser-defined properties can "
425 "be specified by using a name containing a colon (:).\n"));
426 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
427 "properties must be appended with\n"
428 "a user or group specifier of one of these forms:\n"
429 " POSIX name (eg: \"matt\")\n"
430 " POSIX id (eg: \"126829\")\n"
431 " SMB name@domain (eg: \"matt@sun\")\n"
432 " SMB SID (eg: \"S-1-234-567-89\")\n"));
435 gettext("\nFor the property list, run: %s\n"),
438 gettext("\nFor the delegated permission list, run: %s\n"),
439 "zfs allow|unallow");
443 * See comments at end of main().
445 if (getenv("ZFS_ABORT") != NULL
) {
446 (void) printf("dumping core by request\n");
450 exit(requested
? 0 : 2);
454 parseprop(nvlist_t
*props
)
456 char *propname
= optarg
;
457 char *propval
, *strval
;
459 if ((propval
= strchr(propname
, '=')) == NULL
) {
460 (void) fprintf(stderr
, gettext("missing "
461 "'=' for -o option\n"));
466 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
467 (void) fprintf(stderr
, gettext("property '%s' "
468 "specified multiple times\n"), propname
);
471 if (nvlist_add_string(props
, propname
, propval
) != 0)
477 parse_depth(char *opt
, int *flags
)
482 depth
= (int)strtol(opt
, &tmp
, 0);
484 (void) fprintf(stderr
,
485 gettext("%s is not an integer\n"), optarg
);
489 (void) fprintf(stderr
,
490 gettext("Depth can not be negative.\n"));
493 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
497 #define PROGRESS_DELAY 2 /* seconds */
499 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";
500 static time_t pt_begin
;
501 static char *pt_header
= NULL
;
502 static boolean_t pt_shown
;
505 start_progress_timer(void)
507 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
512 set_progress_header(char *header
)
514 assert(pt_header
== NULL
);
515 pt_header
= safe_strdup(header
);
517 (void) printf("%s: ", header
);
518 (void) fflush(stdout
);
523 update_progress(char *update
)
525 if (!pt_shown
&& time(NULL
) > pt_begin
) {
526 int len
= strlen(update
);
528 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
530 (void) fflush(stdout
);
532 } else if (pt_shown
) {
533 int len
= strlen(update
);
535 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
536 (void) fflush(stdout
);
541 finish_progress(char *done
)
544 (void) printf("%s\n", done
);
545 (void) fflush(stdout
);
552 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
554 * Given an existing dataset, create a writable copy whose initial contents
555 * are the same as the source. The newly created dataset maintains a
556 * dependency on the original; the original cannot be destroyed so long as
559 * The '-p' flag creates all the non-existing ancestors of the target first.
562 zfs_do_clone(int argc
, char **argv
)
564 zfs_handle_t
*zhp
= NULL
;
565 boolean_t parents
= B_FALSE
;
570 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
574 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
577 if (parseprop(props
))
584 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
593 /* check number of arguments */
595 (void) fprintf(stderr
, gettext("missing source dataset "
600 (void) fprintf(stderr
, gettext("missing target dataset "
605 (void) fprintf(stderr
, gettext("too many arguments\n"));
609 /* open the source dataset */
610 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
613 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
616 * Now create the ancestors of the target dataset. If the
617 * target already exists and '-p' option was used we should not
620 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
623 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
628 ret
= zfs_clone(zhp
, argv
[1], props
);
630 /* create the mountpoint if necessary */
634 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
636 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
)
637 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
638 ret
= zfs_share(clone
);
657 * zfs create [-p] [-o prop=value] ... fs
658 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
660 * Create a new dataset. This command can be used to create filesystems
661 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
662 * For volumes, the user must specify a size to be used.
664 * The '-s' flag applies only to volumes, and indicates that we should not try
665 * to set the reservation for this volume. By default we set a reservation
666 * equal to the size for any volume. For pools with SPA_VERSION >=
667 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
669 * The '-p' flag creates all the non-existing ancestors of the target first.
672 zfs_do_create(int argc
, char **argv
)
674 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
675 zfs_handle_t
*zhp
= NULL
;
676 uint64_t volsize
= 0;
678 boolean_t noreserve
= B_FALSE
;
679 boolean_t bflag
= B_FALSE
;
680 boolean_t parents
= B_FALSE
;
684 int canmount
= ZFS_CANMOUNT_OFF
;
686 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
690 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
693 type
= ZFS_TYPE_VOLUME
;
694 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
695 (void) fprintf(stderr
, gettext("bad volume "
696 "size '%s': %s\n"), optarg
,
697 libzfs_error_description(g_zfs
));
701 if (nvlist_add_uint64(props
,
702 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
711 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
712 (void) fprintf(stderr
, gettext("bad volume "
713 "block size '%s': %s\n"), optarg
,
714 libzfs_error_description(g_zfs
));
718 if (nvlist_add_uint64(props
,
719 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
724 if (parseprop(props
))
731 (void) fprintf(stderr
, gettext("missing size "
736 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
742 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
743 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
744 "used when creating a volume\n"));
751 /* check number of arguments */
753 (void) fprintf(stderr
, gettext("missing %s argument\n"),
754 zfs_type_to_name(type
));
758 (void) fprintf(stderr
, gettext("too many arguments\n"));
762 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
763 zpool_handle_t
*zpool_handle
;
764 uint64_t spa_version
;
766 zfs_prop_t resv_prop
;
769 if ((p
= strchr(argv
[0], '/')))
771 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
774 if (zpool_handle
== NULL
)
776 spa_version
= zpool_get_prop_int(zpool_handle
,
777 ZPOOL_PROP_VERSION
, NULL
);
778 zpool_close(zpool_handle
);
779 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
780 resv_prop
= ZFS_PROP_REFRESERVATION
;
782 resv_prop
= ZFS_PROP_RESERVATION
;
783 volsize
= zvol_volsize_to_reservation(volsize
, props
);
785 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
787 if (nvlist_add_uint64(props
,
788 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
795 if (parents
&& zfs_name_valid(argv
[0], type
)) {
797 * Now create the ancestors of target dataset. If the target
798 * already exists and '-p' option was used we should not
801 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
805 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
810 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
813 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
818 * if the user doesn't want the dataset automatically mounted,
819 * then skip the mount/share step
821 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
))
822 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
825 * Mount and/or share the new filesystem as appropriate. We provide a
826 * verbose error message to let the user know that their filesystem was
827 * in fact created, even if we failed to mount or share it.
829 if (canmount
== ZFS_CANMOUNT_ON
) {
830 if (zfs_mount(zhp
, NULL
, 0) != 0) {
831 (void) fprintf(stderr
, gettext("filesystem "
832 "successfully created, but not mounted\n"));
834 } else if (zfs_share(zhp
) != 0) {
835 (void) fprintf(stderr
, gettext("filesystem "
836 "successfully created, but not shared\n"));
853 * zfs destroy [-rRf] <fs, vol>
854 * zfs destroy [-rRd] <snap>
856 * -r Recursively destroy all children
857 * -R Recursively destroy all dependents, including clones
858 * -f Force unmounting of any dependents
859 * -d If we can't destroy now, mark for deferred destruction
861 * Destroys the given dataset. By default, it will unmount any filesystems,
862 * and refuse to destroy a dataset that has any dependents. A dependent can
863 * either be a child, or a clone of a child.
865 typedef struct destroy_cbdata
{
872 boolean_t cb_closezhp
;
873 zfs_handle_t
*cb_target
;
875 boolean_t cb_defer_destroy
;
879 * Check for any dependents based on the '-r' or '-R' flags.
882 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
884 destroy_cbdata_t
*cbp
= data
;
885 const char *tname
= zfs_get_name(cbp
->cb_target
);
886 const char *name
= zfs_get_name(zhp
);
888 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
889 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
891 * This is a direct descendant, not a clone somewhere else in
898 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
899 "%s has children\n"),
900 zfs_get_name(cbp
->cb_target
),
901 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
902 (void) fprintf(stderr
, gettext("use '-r' to destroy "
903 "the following datasets:\n"));
904 cbp
->cb_first
= B_FALSE
;
908 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
911 * This is a clone. We only want to report this if the '-r'
912 * wasn't specified, or the target is a snapshot.
914 if (!cbp
->cb_recurse
&&
915 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
919 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
920 "%s has dependent clones\n"),
921 zfs_get_name(cbp
->cb_target
),
922 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
923 (void) fprintf(stderr
, gettext("use '-R' to destroy "
924 "the following datasets:\n"));
925 cbp
->cb_first
= B_FALSE
;
929 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
938 destroy_callback(zfs_handle_t
*zhp
, void *data
)
940 destroy_cbdata_t
*cbp
= data
;
943 * Ignore pools (which we've already flagged as an error before getting
946 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
947 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
953 * Bail out on the first error.
955 if (zfs_unmount(zhp
, NULL
, cbp
->cb_force
? MS_FORCE
: 0) != 0 ||
956 zfs_destroy(zhp
, cbp
->cb_defer_destroy
) != 0) {
966 destroy_snap_clones(zfs_handle_t
*zhp
, void *arg
)
968 destroy_cbdata_t
*cbp
= arg
;
969 char thissnap
[MAXPATHLEN
];
971 boolean_t closezhp
= cbp
->cb_closezhp
;
974 (void) snprintf(thissnap
, sizeof (thissnap
),
975 "%s@%s", zfs_get_name(zhp
), cbp
->cb_snapname
);
977 libzfs_print_on_error(g_zfs
, B_FALSE
);
978 szhp
= zfs_open(g_zfs
, thissnap
, ZFS_TYPE_SNAPSHOT
);
979 libzfs_print_on_error(g_zfs
, B_TRUE
);
982 * Destroy any clones of this snapshot
984 if (zfs_iter_dependents(szhp
, B_FALSE
, destroy_callback
,
994 cbp
->cb_closezhp
= B_TRUE
;
995 rv
= zfs_iter_filesystems(zhp
, destroy_snap_clones
, arg
);
1002 zfs_do_destroy(int argc
, char **argv
)
1004 destroy_cbdata_t cb
= { 0 };
1008 zfs_type_t type
= ZFS_TYPE_DATASET
;
1011 while ((c
= getopt(argc
, argv
, "dfrR")) != -1) {
1014 cb
.cb_defer_destroy
= B_TRUE
;
1015 type
= ZFS_TYPE_SNAPSHOT
;
1029 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1038 /* check number of arguments */
1040 (void) fprintf(stderr
, gettext("missing path argument\n"));
1044 (void) fprintf(stderr
, gettext("too many arguments\n"));
1049 * If we are doing recursive destroy of a snapshot, then the
1050 * named snapshot may not exist. Go straight to libzfs.
1052 if (cb
.cb_recurse
&& (cp
= strchr(argv
[0], '@'))) {
1056 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
1061 if (cb
.cb_doclones
) {
1062 boolean_t defer
= cb
.cb_defer_destroy
;
1065 * Temporarily ignore the defer_destroy setting since
1066 * it's not supported for clones.
1068 cb
.cb_defer_destroy
= B_FALSE
;
1069 cb
.cb_snapname
= cp
;
1070 if (destroy_snap_clones(zhp
, &cb
) != 0) {
1074 cb
.cb_defer_destroy
= defer
;
1077 ret
= zfs_destroy_snaps(zhp
, cp
, cb
.cb_defer_destroy
);
1080 (void) fprintf(stderr
,
1081 gettext("no snapshots destroyed\n"));
1086 /* Open the given dataset */
1087 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1093 * Perform an explicit check for pools before going any further.
1095 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1096 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1097 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1098 "operation does not apply to pools\n"),
1100 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1101 "%s' to destroy all datasets in the pool\n"),
1103 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1104 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1110 * Check for any dependents and/or clones.
1112 cb
.cb_first
= B_TRUE
;
1113 if (!cb
.cb_doclones
&& !cb
.cb_defer_destroy
&&
1114 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1120 if (cb
.cb_error
|| (!cb
.cb_defer_destroy
&&
1121 (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
, &cb
) != 0))) {
1127 * Do the real thing. The callback will close the handle regardless of
1128 * whether it succeeds or not.
1131 if (destroy_callback(zhp
, &cb
) != 0)
1138 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1141 zfs_get_column_t col
;
1143 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1144 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1145 if (col
== GET_COL_RECVD
)
1151 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1152 * < all | property[,property]... > < fs | snap | vol > ...
1154 * -r recurse over any child datasets
1155 * -H scripted mode. Headers are stripped, and fields are separated
1156 * by tabs instead of spaces.
1157 * -o Set of fields to display. One of "name,property,value,
1158 * received,source". Default is "name,property,value,source".
1159 * "all" is an alias for all five.
1160 * -s Set of sources to allow. One of
1161 * "local,default,inherited,received,temporary,none". Default is
1163 * -p Display values in parsable (literal) format.
1165 * Prints properties for the given datasets. The user can control which
1166 * columns to display as well as which property types to allow.
1170 * Invoked to display the properties for a single dataset.
1173 get_callback(zfs_handle_t
*zhp
, void *data
)
1175 char buf
[ZFS_MAXPROPLEN
];
1176 char rbuf
[ZFS_MAXPROPLEN
];
1177 zprop_source_t sourcetype
;
1178 char source
[ZFS_MAXNAMELEN
];
1179 zprop_get_cbdata_t
*cbp
= data
;
1180 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1181 zprop_list_t
*pl
= cbp
->cb_proplist
;
1185 boolean_t received
= is_recvd_column(cbp
);
1187 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1188 char *recvdval
= NULL
;
1190 * Skip the special fake placeholder. This will also skip over
1191 * the name property when 'all' is specified.
1193 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1194 pl
== cbp
->cb_proplist
)
1197 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1198 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1199 sizeof (buf
), &sourcetype
, source
,
1201 cbp
->cb_literal
) != 0) {
1204 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1205 ZFS_TYPE_DATASET
)) {
1206 (void) fprintf(stderr
,
1207 gettext("No such property '%s'\n"),
1208 zfs_prop_to_name(pl
->pl_prop
));
1211 sourcetype
= ZPROP_SRC_NONE
;
1212 (void) strlcpy(buf
, "-", sizeof (buf
));
1215 if (received
&& (zfs_prop_get_recvd(zhp
,
1216 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1217 cbp
->cb_literal
) == 0))
1220 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1221 zfs_prop_to_name(pl
->pl_prop
),
1222 buf
, sourcetype
, source
, recvdval
);
1223 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1224 sourcetype
= ZPROP_SRC_LOCAL
;
1226 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1227 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1228 sourcetype
= ZPROP_SRC_NONE
;
1229 (void) strlcpy(buf
, "-", sizeof (buf
));
1232 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1233 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1235 if (nvlist_lookup_nvlist(user_props
,
1236 pl
->pl_user_prop
, &propval
) != 0) {
1239 sourcetype
= ZPROP_SRC_NONE
;
1242 verify(nvlist_lookup_string(propval
,
1243 ZPROP_VALUE
, &strval
) == 0);
1244 verify(nvlist_lookup_string(propval
,
1245 ZPROP_SOURCE
, &sourceval
) == 0);
1247 if (strcmp(sourceval
,
1248 zfs_get_name(zhp
)) == 0) {
1249 sourcetype
= ZPROP_SRC_LOCAL
;
1250 } else if (strcmp(sourceval
,
1251 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1252 sourcetype
= ZPROP_SRC_RECEIVED
;
1254 sourcetype
= ZPROP_SRC_INHERITED
;
1255 (void) strlcpy(source
,
1256 sourceval
, sizeof (source
));
1260 if (received
&& (zfs_prop_get_recvd(zhp
,
1261 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1262 cbp
->cb_literal
) == 0))
1265 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1266 pl
->pl_user_prop
, strval
, sourcetype
,
1275 zfs_do_get(int argc
, char **argv
)
1277 zprop_get_cbdata_t cb
= { 0 };
1278 int i
, c
, flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1279 char *value
, *fields
;
1282 zprop_list_t fake_name
= { 0 };
1285 * Set up default columns and sources.
1287 cb
.cb_sources
= ZPROP_SRC_ALL
;
1288 cb
.cb_columns
[0] = GET_COL_NAME
;
1289 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1290 cb
.cb_columns
[2] = GET_COL_VALUE
;
1291 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1292 cb
.cb_type
= ZFS_TYPE_DATASET
;
1295 while ((c
= getopt(argc
, argv
, ":d:o:s:rHp")) != -1) {
1298 cb
.cb_literal
= B_TRUE
;
1301 limit
= parse_depth(optarg
, &flags
);
1304 flags
|= ZFS_ITER_RECURSE
;
1307 cb
.cb_scripted
= B_TRUE
;
1310 (void) fprintf(stderr
, gettext("missing argument for "
1311 "'%c' option\n"), optopt
);
1316 * Process the set of columns to display. We zero out
1317 * the structure to give us a blank slate.
1319 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1321 while (*optarg
!= '\0') {
1322 static char *col_subopts
[] =
1323 { "name", "property", "value", "received",
1324 "source", "all", NULL
};
1326 if (i
== ZFS_GET_NCOLS
) {
1327 (void) fprintf(stderr
, gettext("too "
1328 "many fields given to -o "
1333 switch (getsubopt(&optarg
, col_subopts
,
1336 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1339 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1342 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1345 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1346 flags
|= ZFS_ITER_RECVD_PROPS
;
1349 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1353 (void) fprintf(stderr
,
1354 gettext("\"all\" conflicts "
1355 "with specific fields "
1356 "given to -o option\n"));
1359 cb
.cb_columns
[0] = GET_COL_NAME
;
1360 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1361 cb
.cb_columns
[2] = GET_COL_VALUE
;
1362 cb
.cb_columns
[3] = GET_COL_RECVD
;
1363 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1364 flags
|= ZFS_ITER_RECVD_PROPS
;
1368 (void) fprintf(stderr
,
1369 gettext("invalid column name "
1378 while (*optarg
!= '\0') {
1379 static char *source_subopts
[] = {
1380 "local", "default", "inherited",
1381 "received", "temporary", "none",
1384 switch (getsubopt(&optarg
, source_subopts
,
1387 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1390 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1393 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1396 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1399 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1402 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1405 (void) fprintf(stderr
,
1406 gettext("invalid source "
1414 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1424 (void) fprintf(stderr
, gettext("missing property "
1431 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1439 * As part of zfs_expand_proplist(), we keep track of the maximum column
1440 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1441 * need to know the maximum name length. However, the user likely did
1442 * not specify 'name' as one of the properties to fetch, so we need to
1443 * make sure we always include at least this property for
1444 * print_get_headers() to work properly.
1446 if (cb
.cb_proplist
!= NULL
) {
1447 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1448 fake_name
.pl_width
= strlen(gettext("NAME"));
1449 fake_name
.pl_next
= cb
.cb_proplist
;
1450 cb
.cb_proplist
= &fake_name
;
1453 cb
.cb_first
= B_TRUE
;
1455 /* run for each object */
1456 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
, NULL
,
1457 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1459 if (cb
.cb_proplist
== &fake_name
)
1460 zprop_free_list(fake_name
.pl_next
);
1462 zprop_free_list(cb
.cb_proplist
);
1468 * inherit [-rS] <property> <fs|vol> ...
1470 * -r Recurse over all children
1471 * -S Revert to received value, if any
1473 * For each dataset specified on the command line, inherit the given property
1474 * from its parent. Inheriting a property at the pool level will cause it to
1475 * use the default value. The '-r' flag will recurse over all children, and is
1476 * useful for setting a property on a hierarchy-wide basis, regardless of any
1477 * local modifications for each dataset.
1480 typedef struct inherit_cbdata
{
1481 const char *cb_propname
;
1482 boolean_t cb_received
;
1486 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1488 inherit_cbdata_t
*cb
= data
;
1489 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1492 * If we're doing it recursively, then ignore properties that
1493 * are not valid for this type of dataset.
1495 if (prop
!= ZPROP_INVAL
&&
1496 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1499 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1503 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1505 inherit_cbdata_t
*cb
= data
;
1507 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1511 zfs_do_inherit(int argc
, char **argv
)
1515 inherit_cbdata_t cb
= { 0 };
1519 boolean_t received
= B_FALSE
;
1522 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1525 flags
|= ZFS_ITER_RECURSE
;
1532 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1541 /* check number of arguments */
1543 (void) fprintf(stderr
, gettext("missing property argument\n"));
1547 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1555 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1556 if (zfs_prop_readonly(prop
)) {
1557 (void) fprintf(stderr
, gettext(
1558 "%s property is read-only\n"),
1562 if (!zfs_prop_inheritable(prop
) && !received
) {
1563 (void) fprintf(stderr
, gettext("'%s' property cannot "
1564 "be inherited\n"), propname
);
1565 if (prop
== ZFS_PROP_QUOTA
||
1566 prop
== ZFS_PROP_RESERVATION
||
1567 prop
== ZFS_PROP_REFQUOTA
||
1568 prop
== ZFS_PROP_REFRESERVATION
)
1569 (void) fprintf(stderr
, gettext("use 'zfs set "
1570 "%s=none' to clear\n"), propname
);
1573 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1574 prop
== ZFS_PROP_VERSION
)) {
1575 (void) fprintf(stderr
, gettext("'%s' property cannot "
1576 "be reverted to a received value\n"), propname
);
1579 } else if (!zfs_prop_user(propname
)) {
1580 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1585 cb
.cb_propname
= propname
;
1586 cb
.cb_received
= received
;
1588 if (flags
& ZFS_ITER_RECURSE
) {
1589 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1590 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1592 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1593 NULL
, NULL
, 0, inherit_cb
, &cb
);
1599 typedef struct upgrade_cbdata
{
1600 uint64_t cb_numupgraded
;
1601 uint64_t cb_numsamegraded
;
1602 uint64_t cb_numfailed
;
1603 uint64_t cb_version
;
1605 boolean_t cb_foundone
;
1606 char cb_lastfs
[ZFS_MAXNAMELEN
];
1610 same_pool(zfs_handle_t
*zhp
, const char *name
)
1612 int len1
= strcspn(name
, "/@");
1613 const char *zhname
= zfs_get_name(zhp
);
1614 int len2
= strcspn(zhname
, "/@");
1618 return (strncmp(name
, zhname
, len1
) == 0);
1622 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1624 upgrade_cbdata_t
*cb
= data
;
1625 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1627 /* list if it's old/new */
1628 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1629 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1632 str
= gettext("The following filesystems are "
1633 "formatted using a newer software version and\n"
1634 "cannot be accessed on the current system.\n\n");
1636 str
= gettext("The following filesystems are "
1637 "out of date, and can be upgraded. After being\n"
1638 "upgraded, these filesystems (and any 'zfs send' "
1639 "streams generated from\n"
1640 "subsequent snapshots) will no longer be "
1641 "accessible by older software versions.\n\n");
1644 if (!cb
->cb_foundone
) {
1646 (void) printf(gettext("VER FILESYSTEM\n"));
1647 (void) printf(gettext("--- ------------\n"));
1648 cb
->cb_foundone
= B_TRUE
;
1651 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1658 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1660 upgrade_cbdata_t
*cb
= data
;
1661 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1662 int needed_spa_version
;
1665 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1668 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
1670 if (needed_spa_version
< 0)
1673 if (spa_version
< needed_spa_version
) {
1675 (void) printf(gettext("%s: can not be "
1676 "upgraded; the pool version needs to first "
1677 "be upgraded\nto version %d\n\n"),
1678 zfs_get_name(zhp
), needed_spa_version
);
1684 if (version
< cb
->cb_version
) {
1686 (void) snprintf(verstr
, sizeof (verstr
),
1687 "%llu", (u_longlong_t
)cb
->cb_version
);
1688 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1690 * If they did "zfs upgrade -a", then we could
1691 * be doing ioctls to different pools. We need
1692 * to log this history once to each pool.
1694 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
1696 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1697 cb
->cb_numupgraded
++;
1700 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1701 } else if (version
> cb
->cb_version
) {
1702 /* can't downgrade */
1703 (void) printf(gettext("%s: can not be downgraded; "
1704 "it is already at version %u\n"),
1705 zfs_get_name(zhp
), version
);
1708 cb
->cb_numsamegraded
++;
1716 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1719 zfs_do_upgrade(int argc
, char **argv
)
1721 boolean_t all
= B_FALSE
;
1722 boolean_t showversions
= B_FALSE
;
1724 upgrade_cbdata_t cb
= { 0 };
1726 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1729 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1732 flags
|= ZFS_ITER_RECURSE
;
1735 showversions
= B_TRUE
;
1738 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
1739 optarg
, &cb
.cb_version
) != 0) {
1740 (void) fprintf(stderr
,
1741 gettext("invalid version %s\n"), optarg
);
1750 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1759 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
1761 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
1762 cb
.cb_version
|| argc
))
1764 if ((all
|| argc
) && (showversions
))
1770 /* Show info on available versions. */
1771 (void) printf(gettext("The following filesystem versions are "
1773 (void) printf(gettext("VER DESCRIPTION\n"));
1774 (void) printf("--- -----------------------------------------"
1775 "---------------\n");
1776 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1777 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1778 (void) printf(gettext(" 3 Case insensitive and File system "
1779 "unique identifier (FUID)\n"));
1780 (void) printf(gettext(" 4 userquota, groupquota "
1782 (void) printf(gettext(" 5 System attributes\n"));
1783 (void) printf(gettext("\nFor more information on a particular "
1784 "version, including supported releases,\n"));
1785 (void) printf("see the ZFS Administration Guide.\n\n");
1787 } else if (argc
|| all
) {
1788 /* Upgrade filesystems */
1789 if (cb
.cb_version
== 0)
1790 cb
.cb_version
= ZPL_VERSION
;
1791 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
1792 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
1793 (void) printf(gettext("%llu filesystems upgraded\n"),
1794 (u_longlong_t
)cb
.cb_numupgraded
);
1795 if (cb
.cb_numsamegraded
) {
1796 (void) printf(gettext("%llu filesystems already at "
1798 (u_longlong_t
)cb
.cb_numsamegraded
);
1800 if (cb
.cb_numfailed
!= 0)
1803 /* List old-version filesytems */
1805 (void) printf(gettext("This system is currently running "
1806 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
1808 flags
|= ZFS_ITER_RECURSE
;
1809 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1810 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
1812 found
= cb
.cb_foundone
;
1813 cb
.cb_foundone
= B_FALSE
;
1814 cb
.cb_newer
= B_TRUE
;
1816 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1817 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
1819 if (!cb
.cb_foundone
&& !found
) {
1820 (void) printf(gettext("All filesystems are "
1821 "formatted with the current version.\n"));
1832 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
1834 zfs_userquota_prop_t
*typep
= arg
;
1835 zfs_userquota_prop_t p
= *typep
;
1837 char *ug
, *propname
;
1841 if (domain
== NULL
|| domain
[0] == '\0') {
1842 if (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) {
1843 struct group
*g
= getgrgid(rid
);
1847 struct passwd
*p
= getpwuid(rid
);
1853 if (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
)
1858 if (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_GROUPUSED
)
1864 (void) snprintf(namebuf
, sizeof (namebuf
),
1865 "%llu", (longlong_t
)rid
);
1868 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
1870 (void) printf("%s %s %s%c%s %s\n", propname
, ug
, domain
,
1871 domain
[0] ? '-' : ' ', name
, sizebuf
);
1877 zfs_do_userspace(int argc
, char **argv
)
1880 zfs_userquota_prop_t p
;
1883 if ((zhp
= zfs_open(g_zfs
, argv
[argc
-1], ZFS_TYPE_DATASET
)) == NULL
)
1886 (void) printf("PROP TYPE NAME VALUE\n");
1888 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
1889 error
= zfs_userspace(zhp
, p
, userspace_cb
, &p
);
1897 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
1898 * [-s property [-s property]...] [-S property [-S property]...]
1901 * -r Recurse over all children
1902 * -d Limit recursion by depth.
1903 * -H Scripted mode; elide headers and separate columns by tabs
1904 * -o Control which fields to display.
1905 * -t Control which object types to display.
1906 * -s Specify sort columns, descending order.
1907 * -S Specify sort columns, ascending order.
1909 * When given no arguments, lists all filesystems in the system.
1910 * Otherwise, list the specified datasets, optionally recursing down them if
1911 * '-r' is specified.
1913 typedef struct list_cbdata
{
1915 boolean_t cb_scripted
;
1916 zprop_list_t
*cb_proplist
;
1920 * Given a list of columns to display, output appropriate headers for each one.
1923 print_header(zprop_list_t
*pl
)
1925 char headerbuf
[ZFS_MAXPROPLEN
];
1928 boolean_t first
= B_TRUE
;
1929 boolean_t right_justify
;
1931 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1938 right_justify
= B_FALSE
;
1939 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1940 header
= zfs_prop_column_name(pl
->pl_prop
);
1941 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1943 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
1944 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
1945 headerbuf
[i
] = '\0';
1949 if (pl
->pl_next
== NULL
&& !right_justify
)
1950 (void) printf("%s", header
);
1951 else if (right_justify
)
1952 (void) printf("%*s", (int)pl
->pl_width
, header
);
1954 (void) printf("%-*s", (int)pl
->pl_width
, header
);
1957 (void) printf("\n");
1961 * Given a dataset and a list of fields, print out all the properties according
1962 * to the described layout.
1965 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
1967 boolean_t first
= B_TRUE
;
1968 char property
[ZFS_MAXPROPLEN
];
1969 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
1972 boolean_t right_justify
;
1975 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1978 (void) printf("\t");
1985 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1986 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
1987 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
1992 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1993 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1994 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1995 property
, sizeof (property
), B_FALSE
) != 0)
1999 right_justify
= B_TRUE
;
2001 if (nvlist_lookup_nvlist(userprops
,
2002 pl
->pl_user_prop
, &propval
) != 0)
2005 verify(nvlist_lookup_string(propval
,
2006 ZPROP_VALUE
, &propstr
) == 0);
2007 right_justify
= B_FALSE
;
2010 width
= pl
->pl_width
;
2013 * If this is being called in scripted mode, or if this is the
2014 * last column and it is left-justified, don't include a width
2017 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2018 (void) printf("%s", propstr
);
2019 else if (right_justify
)
2020 (void) printf("%*s", width
, propstr
);
2022 (void) printf("%-*s", width
, propstr
);
2025 (void) printf("\n");
2029 * Generic callback function to list a dataset or snapshot.
2032 list_callback(zfs_handle_t
*zhp
, void *data
)
2034 list_cbdata_t
*cbp
= data
;
2036 if (cbp
->cb_first
) {
2037 if (!cbp
->cb_scripted
)
2038 print_header(cbp
->cb_proplist
);
2039 cbp
->cb_first
= B_FALSE
;
2042 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
2048 zfs_do_list(int argc
, char **argv
)
2051 boolean_t scripted
= B_FALSE
;
2052 static char default_fields
[] =
2053 "name,used,available,referenced,mountpoint";
2054 int types
= ZFS_TYPE_DATASET
;
2055 boolean_t types_specified
= B_FALSE
;
2056 char *fields
= NULL
;
2057 list_cbdata_t cb
= { 0 };
2061 zfs_sort_column_t
*sortcol
= NULL
;
2062 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
2065 while ((c
= getopt(argc
, argv
, ":d:o:rt:Hs:S:")) != -1) {
2071 limit
= parse_depth(optarg
, &flags
);
2074 flags
|= ZFS_ITER_RECURSE
;
2080 if (zfs_add_sort_column(&sortcol
, optarg
,
2082 (void) fprintf(stderr
,
2083 gettext("invalid property '%s'\n"), optarg
);
2088 if (zfs_add_sort_column(&sortcol
, optarg
,
2090 (void) fprintf(stderr
,
2091 gettext("invalid property '%s'\n"), optarg
);
2097 types_specified
= B_TRUE
;
2098 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2099 while (*optarg
!= '\0') {
2100 static char *type_subopts
[] = { "filesystem",
2101 "volume", "snapshot", "all", NULL
};
2103 switch (getsubopt(&optarg
, type_subopts
,
2106 types
|= ZFS_TYPE_FILESYSTEM
;
2109 types
|= ZFS_TYPE_VOLUME
;
2112 types
|= ZFS_TYPE_SNAPSHOT
;
2115 types
= ZFS_TYPE_DATASET
;
2119 (void) fprintf(stderr
,
2120 gettext("invalid type '%s'\n"),
2127 (void) fprintf(stderr
, gettext("missing argument for "
2128 "'%c' option\n"), optopt
);
2132 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2142 fields
= default_fields
;
2145 * If "-o space" and no types were specified, don't display snapshots.
2147 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
2148 types
&= ~ZFS_TYPE_SNAPSHOT
;
2151 * If the user specifies '-o all', the zprop_get_list() doesn't
2152 * normally include the name of the dataset. For 'zfs list', we always
2153 * want this property to be first.
2155 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
2159 cb
.cb_scripted
= scripted
;
2160 cb
.cb_first
= B_TRUE
;
2162 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
2163 limit
, list_callback
, &cb
);
2165 zprop_free_list(cb
.cb_proplist
);
2166 zfs_free_sort_columns(sortcol
);
2168 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
2169 (void) printf(gettext("no datasets available\n"));
2175 * zfs rename <fs | snap | vol> <fs | snap | vol>
2176 * zfs rename -p <fs | vol> <fs | vol>
2177 * zfs rename -r <snap> <snap>
2179 * Renames the given dataset to another of the same type.
2181 * The '-p' flag creates all the non-existing ancestors of the target first.
2185 zfs_do_rename(int argc
, char **argv
)
2190 boolean_t recurse
= B_FALSE
;
2191 boolean_t parents
= B_FALSE
;
2194 while ((c
= getopt(argc
, argv
, "pr")) != -1) {
2204 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2213 /* check number of arguments */
2215 (void) fprintf(stderr
, gettext("missing source dataset "
2220 (void) fprintf(stderr
, gettext("missing target dataset "
2225 (void) fprintf(stderr
, gettext("too many arguments\n"));
2229 if (recurse
&& parents
) {
2230 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
2235 if (recurse
&& strchr(argv
[0], '@') == 0) {
2236 (void) fprintf(stderr
, gettext("source dataset for recursive "
2237 "rename must be a snapshot\n"));
2241 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
2242 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
2245 /* If we were asked and the name looks good, try to create ancestors. */
2246 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
2247 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
2252 ret
= (zfs_rename(zhp
, argv
[1], recurse
) != 0);
2261 * Promotes the given clone fs to be the parent
2265 zfs_do_promote(int argc
, char **argv
)
2271 if (argc
> 1 && argv
[1][0] == '-') {
2272 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2277 /* check number of arguments */
2279 (void) fprintf(stderr
, gettext("missing clone filesystem"
2284 (void) fprintf(stderr
, gettext("too many arguments\n"));
2288 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2292 ret
= (zfs_promote(zhp
) != 0);
2300 * zfs rollback [-rRf] <snapshot>
2302 * -r Delete any intervening snapshots before doing rollback
2303 * -R Delete any snapshots and their clones
2304 * -f ignored for backwards compatability
2306 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2307 * since then and making it the active dataset. If more recent snapshots exist,
2308 * the command will complain unless the '-r' flag is given.
2310 typedef struct rollback_cbdata
{
2316 boolean_t cb_recurse
;
2317 boolean_t cb_dependent
;
2318 } rollback_cbdata_t
;
2321 * Report any snapshots more recent than the one specified. Used when '-r' is
2322 * not specified. We reuse this same callback for the snapshot dependents - if
2323 * 'cb_dependent' is set, then this is a dependent and we should report it
2324 * without checking the transaction group.
2327 rollback_check(zfs_handle_t
*zhp
, void *data
)
2329 rollback_cbdata_t
*cbp
= data
;
2331 if (cbp
->cb_doclones
) {
2336 if (!cbp
->cb_dependent
) {
2337 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
2338 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2339 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
2342 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
2343 (void) fprintf(stderr
, gettext("cannot "
2344 "rollback to '%s': more recent snapshots "
2347 (void) fprintf(stderr
, gettext("use '-r' to "
2348 "force deletion of the following "
2354 if (cbp
->cb_recurse
) {
2355 cbp
->cb_dependent
= B_TRUE
;
2356 if (zfs_iter_dependents(zhp
, B_TRUE
,
2357 rollback_check
, cbp
) != 0) {
2361 cbp
->cb_dependent
= B_FALSE
;
2363 (void) fprintf(stderr
, "%s\n",
2368 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
2369 (void) fprintf(stderr
, gettext("cannot rollback to "
2370 "'%s': clones of previous snapshots exist\n"),
2372 (void) fprintf(stderr
, gettext("use '-R' to "
2373 "force deletion of the following clones and "
2379 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
2387 zfs_do_rollback(int argc
, char **argv
)
2391 boolean_t force
= B_FALSE
;
2392 rollback_cbdata_t cb
= { 0 };
2393 zfs_handle_t
*zhp
, *snap
;
2394 char parentname
[ZFS_MAXNAMELEN
];
2398 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
2411 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2420 /* check number of arguments */
2422 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2426 (void) fprintf(stderr
, gettext("too many arguments\n"));
2430 /* open the snapshot */
2431 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
2434 /* open the parent dataset */
2435 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
2436 verify((delim
= strrchr(parentname
, '@')) != NULL
);
2438 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
2444 * Check for more recent snapshots and/or clones based on the presence
2447 cb
.cb_target
= argv
[0];
2448 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
2449 cb
.cb_first
= B_TRUE
;
2451 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
2454 if ((ret
= cb
.cb_error
) != 0)
2458 * Rollback parent to the given snapshot.
2460 ret
= zfs_rollback(zhp
, snap
, force
);
2473 * zfs set property=value { fs | snap | vol } ...
2475 * Sets the given property for all datasets specified on the command line.
2477 typedef struct set_cbdata
{
2483 set_callback(zfs_handle_t
*zhp
, void *data
)
2485 set_cbdata_t
*cbp
= data
;
2487 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
2488 switch (libzfs_errno(g_zfs
)) {
2489 case EZFS_MOUNTFAILED
:
2490 (void) fprintf(stderr
, gettext("property may be set "
2491 "but unable to remount filesystem\n"));
2493 case EZFS_SHARENFSFAILED
:
2494 (void) fprintf(stderr
, gettext("property may be set "
2495 "but unable to reshare filesystem\n"));
2504 zfs_do_set(int argc
, char **argv
)
2509 /* check for options */
2510 if (argc
> 1 && argv
[1][0] == '-') {
2511 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2516 /* check number of arguments */
2518 (void) fprintf(stderr
, gettext("missing property=value "
2523 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2527 /* validate property=value argument */
2528 cb
.cb_propname
= argv
[1];
2529 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
2530 (cb
.cb_value
[1] == '\0')) {
2531 (void) fprintf(stderr
, gettext("missing value in "
2532 "property=value argument\n"));
2536 *cb
.cb_value
= '\0';
2539 if (*cb
.cb_propname
== '\0') {
2540 (void) fprintf(stderr
,
2541 gettext("missing property in property=value argument\n"));
2545 ret
= zfs_for_each(argc
- 2, argv
+ 2, 0,
2546 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
2552 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2554 * Creates a snapshot with the given name. While functionally equivalent to
2555 * 'zfs create', it is a separate command to differentiate intent.
2558 zfs_do_snapshot(int argc
, char **argv
)
2560 boolean_t recursive
= B_FALSE
;
2565 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2569 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
2572 if (parseprop(props
))
2579 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2588 /* check number of arguments */
2590 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2594 (void) fprintf(stderr
, gettext("too many arguments\n"));
2598 ret
= zfs_snapshot(g_zfs
, argv
[0], recursive
, props
);
2600 if (ret
&& recursive
)
2601 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
2611 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
2612 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
2614 * Send a backup stream to stdout.
2617 zfs_do_send(int argc
, char **argv
)
2619 char *fromname
= NULL
;
2620 char *toname
= NULL
;
2623 sendflags_t flags
= { 0 };
2626 boolean_t extraverbose
= B_FALSE
;
2629 while ((c
= getopt(argc
, argv
, ":i:I:RDpv")) != -1) {
2640 flags
.doall
= B_TRUE
;
2643 flags
.replicate
= B_TRUE
;
2646 flags
.props
= B_TRUE
;
2650 extraverbose
= B_TRUE
;
2651 flags
.verbose
= B_TRUE
;
2654 flags
.dedup
= B_TRUE
;
2657 (void) fprintf(stderr
, gettext("missing argument for "
2658 "'%c' option\n"), optopt
);
2662 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2671 /* check number of arguments */
2673 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2677 (void) fprintf(stderr
, gettext("too many arguments\n"));
2681 if (isatty(STDOUT_FILENO
)) {
2682 (void) fprintf(stderr
,
2683 gettext("Error: Stream can not be written to a terminal.\n"
2684 "You must redirect standard output.\n"));
2688 cp
= strchr(argv
[0], '@');
2690 (void) fprintf(stderr
,
2691 gettext("argument must be a snapshot\n"));
2696 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2701 * If they specified the full path to the snapshot, chop off
2702 * everything except the short name of the snapshot, but special
2703 * case if they specify the origin.
2705 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
2706 char origin
[ZFS_MAXNAMELEN
];
2709 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
2710 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
2712 if (strcmp(origin
, fromname
) == 0) {
2714 flags
.fromorigin
= B_TRUE
;
2717 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
2718 (void) fprintf(stderr
,
2719 gettext("incremental source must be "
2720 "in same filesystem\n"));
2724 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
2725 (void) fprintf(stderr
,
2726 gettext("invalid incremental source\n"));
2732 if (flags
.replicate
&& fromname
== NULL
)
2733 flags
.doall
= B_TRUE
;
2735 err
= zfs_send(zhp
, fromname
, toname
, flags
, STDOUT_FILENO
, NULL
, 0,
2736 extraverbose
? &dbgnv
: NULL
);
2740 * dump_nvlist prints to stdout, but that's been
2741 * redirected to a file. Make it print to stderr
2744 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
2745 dump_nvlist(dbgnv
, 0);
2754 * zfs receive [-vnFu] [-d | -e] <fs@snap>
2756 * Restore a backup stream from stdin.
2759 zfs_do_receive(int argc
, char **argv
)
2762 recvflags_t flags
= { 0 };
2765 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
2768 flags
.isprefix
= B_TRUE
;
2771 flags
.isprefix
= B_TRUE
;
2772 flags
.istail
= B_TRUE
;
2775 flags
.dryrun
= B_TRUE
;
2778 flags
.nomount
= B_TRUE
;
2781 flags
.verbose
= B_TRUE
;
2784 flags
.force
= B_TRUE
;
2787 (void) fprintf(stderr
, gettext("missing argument for "
2788 "'%c' option\n"), optopt
);
2792 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2801 /* check number of arguments */
2803 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2807 (void) fprintf(stderr
, gettext("too many arguments\n"));
2811 if (isatty(STDIN_FILENO
)) {
2812 (void) fprintf(stderr
,
2813 gettext("Error: Backup stream can not be read "
2814 "from a terminal.\n"
2815 "You must redirect standard input.\n"));
2819 err
= zfs_receive(g_zfs
, argv
[0], flags
, STDIN_FILENO
, NULL
);
2825 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
2830 boolean_t recursive
= B_FALSE
;
2831 boolean_t temphold
= B_FALSE
;
2832 const char *opts
= holding
? "rt" : "r";
2836 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
2845 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2854 /* check number of arguments */
2862 if (holding
&& tag
[0] == '.') {
2863 /* tags starting with '.' are reserved for libzfs */
2864 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
2868 for (i
= 0; i
< argc
; ++i
) {
2870 char parent
[ZFS_MAXNAMELEN
];
2872 char *path
= argv
[i
];
2874 delim
= strchr(path
, '@');
2875 if (delim
== NULL
) {
2876 (void) fprintf(stderr
,
2877 gettext("'%s' is not a snapshot\n"), path
);
2881 (void) strncpy(parent
, path
, delim
- path
);
2882 parent
[delim
- path
] = '\0';
2884 zhp
= zfs_open(g_zfs
, parent
,
2885 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2891 if (zfs_hold(zhp
, delim
+1, tag
, recursive
,
2892 temphold
, B_FALSE
, -1, 0, 0) != 0)
2895 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
2901 return (errors
!= 0);
2905 * zfs hold [-r] [-t] <tag> <snap> ...
2907 * -r Recursively hold
2908 * -t Temporary hold (hidden option)
2910 * Apply a user-hold with the given tag to the list of snapshots.
2913 zfs_do_hold(int argc
, char **argv
)
2915 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
2919 * zfs release [-r] <tag> <snap> ...
2921 * -r Recursively release
2923 * Release a user-hold with the given tag from the list of snapshots.
2926 zfs_do_release(int argc
, char **argv
)
2928 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
2931 #define CHECK_SPINNER 30
2932 #define SPINNER_TIME 3 /* seconds */
2933 #define MOUNT_TIME 5 /* seconds */
2936 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
2938 static char *spin
[] = { "-", "\\", "|", "/" };
2939 static int spinval
= 0;
2940 static int spincheck
= 0;
2941 static time_t last_spin_time
= (time_t)0;
2942 get_all_cb_t
*cbp
= data
;
2943 zfs_type_t type
= zfs_get_type(zhp
);
2945 if (cbp
->cb_verbose
) {
2946 if (--spincheck
< 0) {
2947 time_t now
= time(NULL
);
2948 if (last_spin_time
+ SPINNER_TIME
< now
) {
2949 update_progress(spin
[spinval
++ % 4]);
2950 last_spin_time
= now
;
2952 spincheck
= CHECK_SPINNER
;
2957 * Interate over any nested datasets.
2959 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
2965 * Skip any datasets whose type does not match.
2967 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
2971 libzfs_add_handle(cbp
, zhp
);
2972 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
2978 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
2980 get_all_cb_t cb
= { 0 };
2981 cb
.cb_verbose
= verbose
;
2982 cb
.cb_getone
= get_one_dataset
;
2985 set_progress_header(gettext("Reading ZFS config"));
2986 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
2988 *dslist
= cb
.cb_handles
;
2989 *count
= cb
.cb_used
;
2992 finish_progress(gettext("done."));
2996 * Generic callback for sharing or mounting filesystems. Because the code is so
2997 * similar, we have a common function with an extra parameter to determine which
2998 * mode we are using.
3000 #define OP_SHARE 0x1
3001 #define OP_MOUNT 0x2
3004 * Share or mount a dataset.
3007 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
3008 boolean_t
explicit, const char *options
)
3010 char mountpoint
[ZFS_MAXPROPLEN
];
3011 char shareopts
[ZFS_MAXPROPLEN
];
3012 char smbshareopts
[ZFS_MAXPROPLEN
];
3013 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
3015 uint64_t zoned
, canmount
;
3016 boolean_t shared_nfs
, shared_smb
;
3018 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
3021 * Check to make sure we can mount/share this dataset. If we
3022 * are in the global zone and the filesystem is exported to a
3023 * local zone, or if we are in a local zone and the
3024 * filesystem is not exported, then it is an error.
3026 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3028 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
3032 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3033 "dataset is exported to a local zone\n"), cmdname
,
3037 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
3041 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3042 "permission denied\n"), cmdname
,
3048 * Ignore any filesystems which don't apply to us. This
3049 * includes those with a legacy mountpoint, or those with
3050 * legacy share options.
3052 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
3053 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
3054 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
3055 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3056 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
3057 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3059 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
3060 strcmp(smbshareopts
, "off") == 0) {
3064 (void) fprintf(stderr
, gettext("cannot share '%s': "
3065 "legacy share\n"), zfs_get_name(zhp
));
3066 (void) fprintf(stderr
, gettext("use share(1M) to "
3067 "share this filesystem, or set "
3068 "sharenfs property on\n"));
3073 * We cannot share or mount legacy filesystems. If the
3074 * shareopts is non-legacy but the mountpoint is legacy, we
3075 * treat it as a legacy share.
3077 if (strcmp(mountpoint
, "legacy") == 0) {
3081 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3082 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
3083 (void) fprintf(stderr
, gettext("use %s(1M) to "
3084 "%s this filesystem\n"), cmdname
, cmdname
);
3088 if (strcmp(mountpoint
, "none") == 0) {
3092 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
3093 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
3098 * canmount explicit outcome
3099 * on no pass through
3100 * on yes pass through
3102 * off yes display error, return 1
3103 * noauto no return 0
3104 * noauto yes pass through
3106 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
3107 if (canmount
== ZFS_CANMOUNT_OFF
) {
3111 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3112 "'canmount' property is set to 'off'\n"), cmdname
,
3115 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
3120 * At this point, we have verified that the mountpoint and/or
3121 * shareopts are appropriate for auto management. If the
3122 * filesystem is already mounted or shared, return (failing
3123 * for explicit requests); otherwise mount or share the
3129 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
3130 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
3132 if ((shared_nfs
&& shared_smb
) ||
3133 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
3134 (strcmp(smbshareopts
, "off") == 0)) ||
3135 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
3136 (strcmp(shareopts
, "off") == 0))) {
3140 (void) fprintf(stderr
, gettext("cannot share "
3141 "'%s': filesystem already shared\n"),
3146 if (!zfs_is_mounted(zhp
, NULL
) &&
3147 zfs_mount(zhp
, NULL
, 0) != 0)
3150 if (protocol
== NULL
) {
3151 if (zfs_shareall(zhp
) != 0)
3153 } else if (strcmp(protocol
, "nfs") == 0) {
3154 if (zfs_share_nfs(zhp
))
3156 } else if (strcmp(protocol
, "smb") == 0) {
3157 if (zfs_share_smb(zhp
))
3160 (void) fprintf(stderr
, gettext("cannot share "
3161 "'%s': invalid share type '%s' "
3163 zfs_get_name(zhp
), protocol
);
3170 if (options
== NULL
)
3171 mnt
.mnt_mntopts
= "";
3173 mnt
.mnt_mntopts
= (char *)options
;
3175 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
3176 zfs_is_mounted(zhp
, NULL
)) {
3180 (void) fprintf(stderr
, gettext("cannot mount "
3181 "'%s': filesystem already mounted\n"),
3186 if (zfs_mount(zhp
, options
, flags
) != 0)
3195 * Reports progress in the form "(current/total)". Not thread-safe.
3198 report_mount_progress(int current
, int total
)
3200 static time_t last_progress_time
= 0;
3201 time_t now
= time(NULL
);
3204 /* report 1..n instead of 0..n-1 */
3207 /* display header if we're here for the first time */
3209 set_progress_header(gettext("Mounting ZFS filesystems"));
3210 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
3211 /* too soon to report again */
3215 last_progress_time
= now
;
3217 (void) sprintf(info
, "(%d/%d)", current
, total
);
3219 if (current
== total
)
3220 finish_progress(info
);
3222 update_progress(info
);
3226 append_options(char *mntopts
, char *newopts
)
3228 int len
= strlen(mntopts
);
3230 /* original length plus new string to append plus 1 for the comma */
3231 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
3232 (void) fprintf(stderr
, gettext("the opts argument for "
3233 "'%s' option is too long (more than %d chars)\n"),
3234 "-o", MNT_LINE_MAX
);
3239 mntopts
[len
++] = ',';
3241 (void) strcpy(&mntopts
[len
], newopts
);
3245 share_mount(int op
, int argc
, char **argv
)
3248 boolean_t verbose
= B_FALSE
;
3250 char *options
= NULL
;
3254 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:" : "a"))
3264 if (*optarg
== '\0') {
3265 (void) fprintf(stderr
, gettext("empty mount "
3266 "options (-o) specified\n"));
3270 if (options
== NULL
)
3271 options
= safe_malloc(MNT_LINE_MAX
+ 1);
3273 /* option validation is done later */
3274 append_options(options
, optarg
);
3278 (void) fprintf(stderr
, gettext("missing argument for "
3279 "'%c' option\n"), optopt
);
3283 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3292 /* check number of arguments */
3294 zfs_handle_t
**dslist
= NULL
;
3295 size_t i
, count
= 0;
3296 char *protocol
= NULL
;
3298 if (op
== OP_SHARE
&& argc
> 0) {
3299 if (strcmp(argv
[0], "nfs") != 0 &&
3300 strcmp(argv
[0], "smb") != 0) {
3301 (void) fprintf(stderr
, gettext("share type "
3302 "must be 'nfs' or 'smb'\n"));
3311 (void) fprintf(stderr
, gettext("too many arguments\n"));
3315 start_progress_timer();
3316 get_all_datasets(&dslist
, &count
, verbose
);
3321 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
3323 for (i
= 0; i
< count
; i
++) {
3325 report_mount_progress(i
, count
);
3327 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
3328 B_FALSE
, options
) != 0)
3330 zfs_close(dslist
[i
]);
3334 } else if (argc
== 0) {
3335 struct mnttab entry
;
3337 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
3338 (void) fprintf(stderr
, gettext("missing filesystem "
3339 "argument (specify -a for all)\n"));
3344 * When mount is given no arguments, go through /etc/mtab and
3345 * display any active ZFS mounts. We hide any snapshots, since
3346 * they are controlled automatically.
3348 rewind(mnttab_file
);
3349 while (getmntent(mnttab_file
, &entry
) == 0) {
3350 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
3351 strchr(entry
.mnt_special
, '@') != NULL
)
3354 (void) printf("%-30s %s\n", entry
.mnt_special
,
3362 (void) fprintf(stderr
,
3363 gettext("too many arguments\n"));
3367 if ((zhp
= zfs_open(g_zfs
, argv
[0],
3368 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3371 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
3381 * zfs mount -a [nfs]
3382 * zfs mount filesystem
3384 * Mount all filesystems, or mount the given filesystem.
3387 zfs_do_mount(int argc
, char **argv
)
3389 return (share_mount(OP_MOUNT
, argc
, argv
));
3393 * zfs share -a [nfs | smb]
3394 * zfs share filesystem
3396 * Share all filesystems, or share the given filesystem.
3399 zfs_do_share(int argc
, char **argv
)
3401 return (share_mount(OP_SHARE
, argc
, argv
));
3404 typedef struct unshare_unmount_node
{
3405 zfs_handle_t
*un_zhp
;
3407 uu_avl_node_t un_avlnode
;
3408 } unshare_unmount_node_t
;
3412 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
3414 const unshare_unmount_node_t
*l
= larg
;
3415 const unshare_unmount_node_t
*r
= rarg
;
3417 return (strcmp(l
->un_mountp
, r
->un_mountp
));
3421 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3422 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
3423 * and unmount it appropriately.
3426 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
3430 struct stat64 statbuf
;
3431 struct extmnttab entry
;
3432 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
3436 * Search for the path in /etc/mtab. Rather than looking for the
3437 * specific path, which can be fooled by non-standard paths (i.e. ".."
3438 * or "//"), we stat() the path and search for the corresponding
3439 * (major,minor) device pair.
3441 if (stat64(path
, &statbuf
) != 0) {
3442 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3443 cmdname
, path
, strerror(errno
));
3446 path_inode
= statbuf
.st_ino
;
3449 * Search for the given (major,minor) pair in the mount table.
3451 rewind(mnttab_file
);
3452 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
3453 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
3454 entry
.mnt_minor
== minor(statbuf
.st_dev
))
3458 if (op
== OP_SHARE
) {
3459 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
3460 "currently mounted\n"), cmdname
, path
);
3463 (void) fprintf(stderr
, gettext("warning: %s not in mtab\n"),
3465 if ((ret
= umount2(path
, flags
)) != 0)
3466 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
3471 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
3472 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
3473 "filesystem\n"), cmdname
, path
);
3477 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3478 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3482 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
3483 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3484 cmdname
, path
, strerror(errno
));
3486 } else if (statbuf
.st_ino
!= path_inode
) {
3487 (void) fprintf(stderr
, gettext("cannot "
3488 "%s '%s': not a mountpoint\n"), cmdname
, path
);
3492 if (op
== OP_SHARE
) {
3493 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3494 char smbshare_prop
[ZFS_MAXPROPLEN
];
3496 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
3497 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3498 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
3499 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3501 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3502 strcmp(smbshare_prop
, "off") == 0) {
3503 (void) fprintf(stderr
, gettext("cannot unshare "
3504 "'%s': legacy share\n"), path
);
3505 (void) fprintf(stderr
, gettext("use exportfs(8) "
3506 "or smbcontrol(1) to unshare this filesystem\n"));
3507 } else if (!zfs_is_shared(zhp
)) {
3508 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
3509 "not currently shared\n"), path
);
3511 ret
= zfs_unshareall_bypath(zhp
, path
);
3514 char mtpt_prop
[ZFS_MAXPROPLEN
];
3516 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
3517 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3520 ret
= zfs_unmount(zhp
, NULL
, flags
);
3521 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
3522 (void) fprintf(stderr
, gettext("cannot unmount "
3523 "'%s': legacy mountpoint\n"),
3525 (void) fprintf(stderr
, gettext("use umount(8) "
3526 "to unmount this filesystem\n"));
3528 ret
= zfs_unmountall(zhp
, flags
);
3539 * Generic callback for unsharing or unmounting a filesystem.
3542 unshare_unmount(int op
, int argc
, char **argv
)
3549 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3550 char sharesmb
[ZFS_MAXPROPLEN
];
3553 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
3562 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3573 * We could make use of zfs_for_each() to walk all datasets in
3574 * the system, but this would be very inefficient, especially
3575 * since we would have to linearly search /etc/mtab for each
3576 * one. Instead, do one pass through /etc/mtab looking for
3577 * zfs entries and call zfs_unmount() for each one.
3579 * Things get a little tricky if the administrator has created
3580 * mountpoints beneath other ZFS filesystems. In this case, we
3581 * have to unmount the deepest filesystems first. To accomplish
3582 * this, we place all the mountpoints in an AVL tree sorted by
3583 * the special type (dataset name), and walk the result in
3584 * reverse to make sure to get any snapshots first.
3586 struct mnttab entry
;
3587 uu_avl_pool_t
*pool
;
3588 uu_avl_t
*tree
= NULL
;
3589 unshare_unmount_node_t
*node
;
3591 uu_avl_walk_t
*walk
;
3594 (void) fprintf(stderr
, gettext("too many arguments\n"));
3598 if (((pool
= uu_avl_pool_create("unmount_pool",
3599 sizeof (unshare_unmount_node_t
),
3600 offsetof(unshare_unmount_node_t
, un_avlnode
),
3601 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
3602 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
3605 rewind(mnttab_file
);
3606 while (getmntent(mnttab_file
, &entry
) == 0) {
3608 /* ignore non-ZFS entries */
3609 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
3612 /* ignore snapshots */
3613 if (strchr(entry
.mnt_special
, '@') != NULL
)
3616 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3617 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3624 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3626 sizeof (nfs_mnt_prop
),
3627 NULL
, NULL
, 0, B_FALSE
) == 0);
3628 if (strcmp(nfs_mnt_prop
, "off") != 0)
3630 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3632 sizeof (nfs_mnt_prop
),
3633 NULL
, NULL
, 0, B_FALSE
) == 0);
3634 if (strcmp(nfs_mnt_prop
, "off") == 0)
3638 /* Ignore legacy mounts */
3639 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
3641 sizeof (nfs_mnt_prop
),
3642 NULL
, NULL
, 0, B_FALSE
) == 0);
3643 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
3645 /* Ignore canmount=noauto mounts */
3646 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
3647 ZFS_CANMOUNT_NOAUTO
)
3653 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
3655 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
3657 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
3659 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
3660 uu_avl_insert(tree
, node
, idx
);
3662 zfs_close(node
->un_zhp
);
3663 free(node
->un_mountp
);
3669 * Walk the AVL tree in reverse, unmounting each filesystem and
3670 * removing it from the AVL tree in the process.
3672 if ((walk
= uu_avl_walk_start(tree
,
3673 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
3676 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3677 uu_avl_remove(tree
, node
);
3681 if (zfs_unshareall_bypath(node
->un_zhp
,
3682 node
->un_mountp
) != 0)
3687 if (zfs_unmount(node
->un_zhp
,
3688 node
->un_mountp
, flags
) != 0)
3693 zfs_close(node
->un_zhp
);
3694 free(node
->un_mountp
);
3698 uu_avl_walk_end(walk
);
3699 uu_avl_destroy(tree
);
3700 uu_avl_pool_destroy(pool
);
3705 (void) fprintf(stderr
,
3706 gettext("missing filesystem argument\n"));
3708 (void) fprintf(stderr
,
3709 gettext("too many arguments\n"));
3714 * We have an argument, but it may be a full path or a ZFS
3715 * filesystem. Pass full paths off to unmount_path() (shared by
3716 * manual_unmount), otherwise open the filesystem and pass to
3719 if (argv
[0][0] == '/')
3720 return (unshare_unmount_path(op
, argv
[0],
3723 if ((zhp
= zfs_open(g_zfs
, argv
[0],
3724 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3727 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
3728 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
3729 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
3730 NULL
, 0, B_FALSE
) == 0);
3734 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3736 sizeof (nfs_mnt_prop
),
3737 NULL
, NULL
, 0, B_FALSE
) == 0);
3738 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3739 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
3742 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3743 strcmp(sharesmb
, "off") == 0) {
3744 (void) fprintf(stderr
, gettext("cannot "
3745 "unshare '%s': legacy share\n"),
3747 (void) fprintf(stderr
, gettext("use "
3748 "unshare(1M) to unshare this "
3751 } else if (!zfs_is_shared(zhp
)) {
3752 (void) fprintf(stderr
, gettext("cannot "
3753 "unshare '%s': not currently "
3754 "shared\n"), zfs_get_name(zhp
));
3756 } else if (zfs_unshareall(zhp
) != 0) {
3762 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
3763 (void) fprintf(stderr
, gettext("cannot "
3764 "unmount '%s': legacy "
3765 "mountpoint\n"), zfs_get_name(zhp
));
3766 (void) fprintf(stderr
, gettext("use "
3767 "umount(1M) to unmount this "
3770 } else if (!zfs_is_mounted(zhp
, NULL
)) {
3771 (void) fprintf(stderr
, gettext("cannot "
3772 "unmount '%s': not currently "
3776 } else if (zfs_unmountall(zhp
, flags
) != 0) {
3790 * zfs unmount filesystem
3792 * Unmount all filesystems, or a specific ZFS filesystem.
3795 zfs_do_unmount(int argc
, char **argv
)
3797 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
3802 * zfs unshare filesystem
3804 * Unshare all filesystems, or a specific ZFS filesystem.
3807 zfs_do_unshare(int argc
, char **argv
)
3809 return (unshare_unmount(OP_SHARE
, argc
, argv
));
3814 zfs_do_python(int argc
, char **argv
)
3816 (void) execv(pypath
, argv
-1);
3817 (void) printf("internal error: %s not found\n", pypath
);
3822 find_command_idx(char *command
, int *idx
)
3826 for (i
= 0; i
< NCOMMAND
; i
++) {
3827 if (command_table
[i
].name
== NULL
)
3830 if (strcmp(command
, command_table
[i
].name
) == 0) {
3839 zfs_do_diff(int argc
, char **argv
)
3843 char *tosnap
= NULL
;
3844 char *fromsnap
= NULL
;
3849 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
3852 flags
|= ZFS_DIFF_CLASSIFY
;
3855 flags
|= ZFS_DIFF_PARSEABLE
;
3858 flags
|= ZFS_DIFF_TIMESTAMP
;
3861 (void) fprintf(stderr
,
3862 gettext("invalid option '%c'\n"), optopt
);
3871 (void) fprintf(stderr
,
3872 gettext("must provide at least one snapshot name\n"));
3877 (void) fprintf(stderr
, gettext("too many arguments\n"));
3882 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
3885 if (*fromsnap
!= '@')
3886 copy
= strdup(fromsnap
);
3888 copy
= strdup(tosnap
);
3892 if ((atp
= strchr(copy
, '@')))
3895 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
3901 * Ignore SIGPIPE so that the library can give us
3902 * information on any failure
3904 (void) sigignore(SIGPIPE
);
3906 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
3914 main(int argc
, char **argv
)
3920 (void) setlocale(LC_ALL
, "");
3921 (void) textdomain(TEXT_DOMAIN
);
3925 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
3926 (void) fprintf(stderr
, gettext("internal error: unable to "
3927 "open %s\n"), MNTTAB
);
3932 * Make sure the user has specified some command.
3935 (void) fprintf(stderr
, gettext("missing command\n"));
3942 * The 'umount' command is an alias for 'unmount'
3944 if (strcmp(cmdname
, "umount") == 0)
3945 cmdname
= "unmount";
3948 * The 'recv' command is an alias for 'receive'
3950 if (strcmp(cmdname
, "recv") == 0)
3951 cmdname
= "receive";
3956 if ((strcmp(cmdname
, "-?") == 0) ||
3957 (strcmp(cmdname
, "--help") == 0))
3960 if ((g_zfs
= libzfs_init()) == NULL
)
3963 zpool_set_history_str("zfs", argc
, argv
, history_str
);
3964 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
3966 libzfs_print_on_error(g_zfs
, B_TRUE
);
3969 * Run the appropriate command.
3971 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
3972 if (find_command_idx(cmdname
, &i
) == 0) {
3973 current_command
= &command_table
[i
];
3974 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
3975 } else if (strchr(cmdname
, '=') != NULL
) {
3976 verify(find_command_idx("set", &i
) == 0);
3977 current_command
= &command_table
[i
];
3978 ret
= command_table
[i
].func(argc
, argv
);
3980 (void) fprintf(stderr
, gettext("unrecognized "
3981 "command '%s'\n"), cmdname
);
3985 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
3988 (void) fclose(mnttab_file
);
3991 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3992 * for the purposes of running ::findleaks.
3994 if (getenv("ZFS_ABORT") != NULL
) {
3995 (void) printf("dumping core by request\n");