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 [-vRDp] [-[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
)
322 safe_strdup(char *str
)
324 char *dupstr
= strdup(str
);
333 * Callback routine that will print out information for each of
337 usage_prop_cb(int prop
, void *cb
)
341 (void) fprintf(fp
, "\t%-15s ", zfs_prop_to_name(prop
));
343 if (zfs_prop_readonly(prop
))
344 (void) fprintf(fp
, " NO ");
346 (void) fprintf(fp
, "YES ");
348 if (zfs_prop_inheritable(prop
))
349 (void) fprintf(fp
, " YES ");
351 (void) fprintf(fp
, " NO ");
353 if (zfs_prop_values(prop
) == NULL
)
354 (void) fprintf(fp
, "-\n");
356 (void) fprintf(fp
, "%s\n", zfs_prop_values(prop
));
362 * Display usage message. If we're inside a command, display only the usage for
363 * that command. Otherwise, iterate over the entire command table and display
364 * a complete usage message.
367 usage(boolean_t requested
)
370 boolean_t show_properties
= B_FALSE
;
371 FILE *fp
= requested
? stdout
: stderr
;
373 if (current_command
== NULL
) {
375 (void) fprintf(fp
, gettext("usage: zfs command args ...\n"));
377 gettext("where 'command' is one of the following:\n\n"));
379 for (i
= 0; i
< NCOMMAND
; i
++) {
380 if (command_table
[i
].name
== NULL
)
381 (void) fprintf(fp
, "\n");
383 (void) fprintf(fp
, "%s",
384 get_usage(command_table
[i
].usage
));
387 (void) fprintf(fp
, gettext("\nEach dataset is of the form: "
388 "pool/[dataset/]*dataset[@name]\n"));
390 (void) fprintf(fp
, gettext("usage:\n"));
391 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
394 if (current_command
!= NULL
&&
395 (strcmp(current_command
->name
, "set") == 0 ||
396 strcmp(current_command
->name
, "get") == 0 ||
397 strcmp(current_command
->name
, "inherit") == 0 ||
398 strcmp(current_command
->name
, "list") == 0))
399 show_properties
= B_TRUE
;
401 if (show_properties
) {
403 gettext("\nThe following properties are supported:\n"));
405 (void) fprintf(fp
, "\n\t%-14s %s %s %s\n\n",
406 "PROPERTY", "EDIT", "INHERIT", "VALUES");
408 /* Iterate over all properties */
409 (void) zprop_iter(usage_prop_cb
, fp
, B_FALSE
, B_TRUE
,
412 (void) fprintf(fp
, "\t%-15s ", "userused@...");
413 (void) fprintf(fp
, " NO NO <size>\n");
414 (void) fprintf(fp
, "\t%-15s ", "groupused@...");
415 (void) fprintf(fp
, " NO NO <size>\n");
416 (void) fprintf(fp
, "\t%-15s ", "userquota@...");
417 (void) fprintf(fp
, "YES NO <size> | none\n");
418 (void) fprintf(fp
, "\t%-15s ", "groupquota@...");
419 (void) fprintf(fp
, "YES NO <size> | none\n");
421 (void) fprintf(fp
, gettext("\nSizes are specified in bytes "
422 "with standard units such as K, M, G, etc.\n"));
423 (void) fprintf(fp
, gettext("\nUser-defined properties can "
424 "be specified by using a name containing a colon (:).\n"));
425 (void) fprintf(fp
, gettext("\nThe {user|group}{used|quota}@ "
426 "properties must be appended with\n"
427 "a user or group specifier of one of these forms:\n"
428 " POSIX name (eg: \"matt\")\n"
429 " POSIX id (eg: \"126829\")\n"
430 " SMB name@domain (eg: \"matt@sun\")\n"
431 " SMB SID (eg: \"S-1-234-567-89\")\n"));
434 gettext("\nFor the property list, run: %s\n"),
437 gettext("\nFor the delegated permission list, run: %s\n"),
438 "zfs allow|unallow");
442 * See comments at end of main().
444 if (getenv("ZFS_ABORT") != NULL
) {
445 (void) printf("dumping core by request\n");
449 exit(requested
? 0 : 2);
453 parseprop(nvlist_t
*props
)
455 char *propname
= optarg
;
456 char *propval
, *strval
;
458 if ((propval
= strchr(propname
, '=')) == NULL
) {
459 (void) fprintf(stderr
, gettext("missing "
460 "'=' for -o option\n"));
465 if (nvlist_lookup_string(props
, propname
, &strval
) == 0) {
466 (void) fprintf(stderr
, gettext("property '%s' "
467 "specified multiple times\n"), propname
);
470 if (nvlist_add_string(props
, propname
, propval
) != 0)
476 parse_depth(char *opt
, int *flags
)
481 depth
= (int)strtol(opt
, &tmp
, 0);
483 (void) fprintf(stderr
,
484 gettext("%s is not an integer\n"), optarg
);
488 (void) fprintf(stderr
,
489 gettext("Depth can not be negative.\n"));
492 *flags
|= (ZFS_ITER_DEPTH_LIMIT
|ZFS_ITER_RECURSE
);
496 #define PROGRESS_DELAY 2 /* seconds */
498 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";
499 static time_t pt_begin
;
500 static char *pt_header
= NULL
;
501 static boolean_t pt_shown
;
504 start_progress_timer(void)
506 pt_begin
= time(NULL
) + PROGRESS_DELAY
;
511 set_progress_header(char *header
)
513 assert(pt_header
== NULL
);
514 pt_header
= safe_strdup(header
);
516 (void) printf("%s: ", header
);
517 (void) fflush(stdout
);
522 update_progress(char *update
)
524 if (!pt_shown
&& time(NULL
) > pt_begin
) {
525 int len
= strlen(update
);
527 (void) printf("%s: %s%*.*s", pt_header
, update
, len
, len
,
529 (void) fflush(stdout
);
531 } else if (pt_shown
) {
532 int len
= strlen(update
);
534 (void) printf("%s%*.*s", update
, len
, len
, pt_reverse
);
535 (void) fflush(stdout
);
540 finish_progress(char *done
)
543 (void) printf("%s\n", done
);
544 (void) fflush(stdout
);
551 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
553 * Given an existing dataset, create a writable copy whose initial contents
554 * are the same as the source. The newly created dataset maintains a
555 * dependency on the original; the original cannot be destroyed so long as
558 * The '-p' flag creates all the non-existing ancestors of the target first.
561 zfs_do_clone(int argc
, char **argv
)
563 zfs_handle_t
*zhp
= NULL
;
564 boolean_t parents
= B_FALSE
;
569 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
573 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
576 if (parseprop(props
))
583 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
592 /* check number of arguments */
594 (void) fprintf(stderr
, gettext("missing source dataset "
599 (void) fprintf(stderr
, gettext("missing target dataset "
604 (void) fprintf(stderr
, gettext("too many arguments\n"));
608 /* open the source dataset */
609 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
612 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
615 * Now create the ancestors of the target dataset. If the
616 * target already exists and '-p' option was used we should not
619 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
622 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
627 ret
= zfs_clone(zhp
, argv
[1], props
);
629 /* create the mountpoint if necessary */
633 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
635 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
)
636 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
637 ret
= zfs_share(clone
);
656 * zfs create [-p] [-o prop=value] ... fs
657 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
659 * Create a new dataset. This command can be used to create filesystems
660 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
661 * For volumes, the user must specify a size to be used.
663 * The '-s' flag applies only to volumes, and indicates that we should not try
664 * to set the reservation for this volume. By default we set a reservation
665 * equal to the size for any volume. For pools with SPA_VERSION >=
666 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
668 * The '-p' flag creates all the non-existing ancestors of the target first.
671 zfs_do_create(int argc
, char **argv
)
673 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
674 zfs_handle_t
*zhp
= NULL
;
675 uint64_t volsize
= 0;
677 boolean_t noreserve
= B_FALSE
;
678 boolean_t bflag
= B_FALSE
;
679 boolean_t parents
= B_FALSE
;
683 int canmount
= ZFS_CANMOUNT_OFF
;
685 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
689 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
692 type
= ZFS_TYPE_VOLUME
;
693 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
694 (void) fprintf(stderr
, gettext("bad volume "
695 "size '%s': %s\n"), optarg
,
696 libzfs_error_description(g_zfs
));
700 if (nvlist_add_uint64(props
,
701 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
710 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
711 (void) fprintf(stderr
, gettext("bad volume "
712 "block size '%s': %s\n"), optarg
,
713 libzfs_error_description(g_zfs
));
717 if (nvlist_add_uint64(props
,
718 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
723 if (parseprop(props
))
730 (void) fprintf(stderr
, gettext("missing size "
735 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
741 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
742 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
743 "used when creating a volume\n"));
750 /* check number of arguments */
752 (void) fprintf(stderr
, gettext("missing %s argument\n"),
753 zfs_type_to_name(type
));
757 (void) fprintf(stderr
, gettext("too many arguments\n"));
761 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
762 zpool_handle_t
*zpool_handle
;
763 uint64_t spa_version
;
765 zfs_prop_t resv_prop
;
768 if ((p
= strchr(argv
[0], '/')))
770 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
773 if (zpool_handle
== NULL
)
775 spa_version
= zpool_get_prop_int(zpool_handle
,
776 ZPOOL_PROP_VERSION
, NULL
);
777 zpool_close(zpool_handle
);
778 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
779 resv_prop
= ZFS_PROP_REFRESERVATION
;
781 resv_prop
= ZFS_PROP_RESERVATION
;
782 volsize
= zvol_volsize_to_reservation(volsize
, props
);
784 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
786 if (nvlist_add_uint64(props
,
787 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
794 if (parents
&& zfs_name_valid(argv
[0], type
)) {
796 * Now create the ancestors of target dataset. If the target
797 * already exists and '-p' option was used we should not
800 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
804 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
809 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
812 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
817 * if the user doesn't want the dataset automatically mounted,
818 * then skip the mount/share step
820 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
))
821 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
824 * Mount and/or share the new filesystem as appropriate. We provide a
825 * verbose error message to let the user know that their filesystem was
826 * in fact created, even if we failed to mount or share it.
828 if (canmount
== ZFS_CANMOUNT_ON
) {
829 if (zfs_mount(zhp
, NULL
, 0) != 0) {
830 (void) fprintf(stderr
, gettext("filesystem "
831 "successfully created, but not mounted\n"));
833 } else if (zfs_share(zhp
) != 0) {
834 (void) fprintf(stderr
, gettext("filesystem "
835 "successfully created, but not shared\n"));
852 * zfs destroy [-rRf] <fs, vol>
853 * zfs destroy [-rRd] <snap>
855 * -r Recursively destroy all children
856 * -R Recursively destroy all dependents, including clones
857 * -f Force unmounting of any dependents
858 * -d If we can't destroy now, mark for deferred destruction
860 * Destroys the given dataset. By default, it will unmount any filesystems,
861 * and refuse to destroy a dataset that has any dependents. A dependent can
862 * either be a child, or a clone of a child.
864 typedef struct destroy_cbdata
{
871 boolean_t cb_closezhp
;
872 zfs_handle_t
*cb_target
;
874 boolean_t cb_defer_destroy
;
878 * Check for any dependents based on the '-r' or '-R' flags.
881 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
883 destroy_cbdata_t
*cbp
= data
;
884 const char *tname
= zfs_get_name(cbp
->cb_target
);
885 const char *name
= zfs_get_name(zhp
);
887 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
888 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
890 * This is a direct descendant, not a clone somewhere else in
897 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
898 "%s has children\n"),
899 zfs_get_name(cbp
->cb_target
),
900 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
901 (void) fprintf(stderr
, gettext("use '-r' to destroy "
902 "the following datasets:\n"));
903 cbp
->cb_first
= B_FALSE
;
907 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
910 * This is a clone. We only want to report this if the '-r'
911 * wasn't specified, or the target is a snapshot.
913 if (!cbp
->cb_recurse
&&
914 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
918 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
919 "%s has dependent clones\n"),
920 zfs_get_name(cbp
->cb_target
),
921 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
922 (void) fprintf(stderr
, gettext("use '-R' to destroy "
923 "the following datasets:\n"));
924 cbp
->cb_first
= B_FALSE
;
928 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
937 destroy_callback(zfs_handle_t
*zhp
, void *data
)
939 destroy_cbdata_t
*cbp
= data
;
942 * Ignore pools (which we've already flagged as an error before getting
945 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
946 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
952 * Bail out on the first error.
954 if (zfs_unmount(zhp
, NULL
, cbp
->cb_force
? MS_FORCE
: 0) != 0 ||
955 zfs_destroy(zhp
, cbp
->cb_defer_destroy
) != 0) {
965 destroy_snap_clones(zfs_handle_t
*zhp
, void *arg
)
967 destroy_cbdata_t
*cbp
= arg
;
968 char thissnap
[MAXPATHLEN
];
970 boolean_t closezhp
= cbp
->cb_closezhp
;
973 (void) snprintf(thissnap
, sizeof (thissnap
),
974 "%s@%s", zfs_get_name(zhp
), cbp
->cb_snapname
);
976 libzfs_print_on_error(g_zfs
, B_FALSE
);
977 szhp
= zfs_open(g_zfs
, thissnap
, ZFS_TYPE_SNAPSHOT
);
978 libzfs_print_on_error(g_zfs
, B_TRUE
);
981 * Destroy any clones of this snapshot
983 if (zfs_iter_dependents(szhp
, B_FALSE
, destroy_callback
,
993 cbp
->cb_closezhp
= B_TRUE
;
994 rv
= zfs_iter_filesystems(zhp
, destroy_snap_clones
, arg
);
1001 zfs_do_destroy(int argc
, char **argv
)
1003 destroy_cbdata_t cb
= { 0 };
1007 zfs_type_t type
= ZFS_TYPE_DATASET
;
1010 while ((c
= getopt(argc
, argv
, "dfrR")) != -1) {
1013 cb
.cb_defer_destroy
= B_TRUE
;
1014 type
= ZFS_TYPE_SNAPSHOT
;
1028 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1037 /* check number of arguments */
1039 (void) fprintf(stderr
, gettext("missing path argument\n"));
1043 (void) fprintf(stderr
, gettext("too many arguments\n"));
1048 * If we are doing recursive destroy of a snapshot, then the
1049 * named snapshot may not exist. Go straight to libzfs.
1051 if (cb
.cb_recurse
&& (cp
= strchr(argv
[0], '@'))) {
1055 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
1060 if (cb
.cb_doclones
) {
1061 boolean_t defer
= cb
.cb_defer_destroy
;
1064 * Temporarily ignore the defer_destroy setting since
1065 * it's not supported for clones.
1067 cb
.cb_defer_destroy
= B_FALSE
;
1068 cb
.cb_snapname
= cp
;
1069 if (destroy_snap_clones(zhp
, &cb
) != 0) {
1073 cb
.cb_defer_destroy
= defer
;
1076 ret
= zfs_destroy_snaps(zhp
, cp
, cb
.cb_defer_destroy
);
1079 (void) fprintf(stderr
,
1080 gettext("no snapshots destroyed\n"));
1085 /* Open the given dataset */
1086 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1092 * Perform an explicit check for pools before going any further.
1094 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1095 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1096 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1097 "operation does not apply to pools\n"),
1099 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1100 "%s' to destroy all datasets in the pool\n"),
1102 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1103 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1109 * Check for any dependents and/or clones.
1111 cb
.cb_first
= B_TRUE
;
1112 if (!cb
.cb_doclones
&& !cb
.cb_defer_destroy
&&
1113 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1119 if (cb
.cb_error
|| (!cb
.cb_defer_destroy
&&
1120 (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
, &cb
) != 0))) {
1126 * Do the real thing. The callback will close the handle regardless of
1127 * whether it succeeds or not.
1130 if (destroy_callback(zhp
, &cb
) != 0)
1137 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1140 zfs_get_column_t col
;
1142 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1143 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1144 if (col
== GET_COL_RECVD
)
1150 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1151 * < all | property[,property]... > < fs | snap | vol > ...
1153 * -r recurse over any child datasets
1154 * -H scripted mode. Headers are stripped, and fields are separated
1155 * by tabs instead of spaces.
1156 * -o Set of fields to display. One of "name,property,value,
1157 * received,source". Default is "name,property,value,source".
1158 * "all" is an alias for all five.
1159 * -s Set of sources to allow. One of
1160 * "local,default,inherited,received,temporary,none". Default is
1162 * -p Display values in parsable (literal) format.
1164 * Prints properties for the given datasets. The user can control which
1165 * columns to display as well as which property types to allow.
1169 * Invoked to display the properties for a single dataset.
1172 get_callback(zfs_handle_t
*zhp
, void *data
)
1174 char buf
[ZFS_MAXPROPLEN
];
1175 char rbuf
[ZFS_MAXPROPLEN
];
1176 zprop_source_t sourcetype
;
1177 char source
[ZFS_MAXNAMELEN
];
1178 zprop_get_cbdata_t
*cbp
= data
;
1179 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1180 zprop_list_t
*pl
= cbp
->cb_proplist
;
1184 boolean_t received
= is_recvd_column(cbp
);
1186 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1187 char *recvdval
= NULL
;
1189 * Skip the special fake placeholder. This will also skip over
1190 * the name property when 'all' is specified.
1192 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1193 pl
== cbp
->cb_proplist
)
1196 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1197 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1198 sizeof (buf
), &sourcetype
, source
,
1200 cbp
->cb_literal
) != 0) {
1203 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1204 ZFS_TYPE_DATASET
)) {
1205 (void) fprintf(stderr
,
1206 gettext("No such property '%s'\n"),
1207 zfs_prop_to_name(pl
->pl_prop
));
1210 sourcetype
= ZPROP_SRC_NONE
;
1211 (void) strlcpy(buf
, "-", sizeof (buf
));
1214 if (received
&& (zfs_prop_get_recvd(zhp
,
1215 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1216 cbp
->cb_literal
) == 0))
1219 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1220 zfs_prop_to_name(pl
->pl_prop
),
1221 buf
, sourcetype
, source
, recvdval
);
1222 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1223 sourcetype
= ZPROP_SRC_LOCAL
;
1225 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1226 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1227 sourcetype
= ZPROP_SRC_NONE
;
1228 (void) strlcpy(buf
, "-", sizeof (buf
));
1231 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1232 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1234 if (nvlist_lookup_nvlist(user_props
,
1235 pl
->pl_user_prop
, &propval
) != 0) {
1238 sourcetype
= ZPROP_SRC_NONE
;
1241 verify(nvlist_lookup_string(propval
,
1242 ZPROP_VALUE
, &strval
) == 0);
1243 verify(nvlist_lookup_string(propval
,
1244 ZPROP_SOURCE
, &sourceval
) == 0);
1246 if (strcmp(sourceval
,
1247 zfs_get_name(zhp
)) == 0) {
1248 sourcetype
= ZPROP_SRC_LOCAL
;
1249 } else if (strcmp(sourceval
,
1250 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1251 sourcetype
= ZPROP_SRC_RECEIVED
;
1253 sourcetype
= ZPROP_SRC_INHERITED
;
1254 (void) strlcpy(source
,
1255 sourceval
, sizeof (source
));
1259 if (received
&& (zfs_prop_get_recvd(zhp
,
1260 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1261 cbp
->cb_literal
) == 0))
1264 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1265 pl
->pl_user_prop
, strval
, sourcetype
,
1274 zfs_do_get(int argc
, char **argv
)
1276 zprop_get_cbdata_t cb
= { 0 };
1277 int i
, c
, flags
= 0;
1278 char *value
, *fields
;
1281 zprop_list_t fake_name
= { 0 };
1284 * Set up default columns and sources.
1286 cb
.cb_sources
= ZPROP_SRC_ALL
;
1287 cb
.cb_columns
[0] = GET_COL_NAME
;
1288 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1289 cb
.cb_columns
[2] = GET_COL_VALUE
;
1290 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1291 cb
.cb_type
= ZFS_TYPE_DATASET
;
1294 while ((c
= getopt(argc
, argv
, ":d:o:s:rHp")) != -1) {
1297 cb
.cb_literal
= B_TRUE
;
1300 limit
= parse_depth(optarg
, &flags
);
1303 flags
|= ZFS_ITER_RECURSE
;
1306 cb
.cb_scripted
= B_TRUE
;
1309 (void) fprintf(stderr
, gettext("missing argument for "
1310 "'%c' option\n"), optopt
);
1315 * Process the set of columns to display. We zero out
1316 * the structure to give us a blank slate.
1318 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1320 while (*optarg
!= '\0') {
1321 static char *col_subopts
[] =
1322 { "name", "property", "value", "received",
1323 "source", "all", NULL
};
1325 if (i
== ZFS_GET_NCOLS
) {
1326 (void) fprintf(stderr
, gettext("too "
1327 "many fields given to -o "
1332 switch (getsubopt(&optarg
, col_subopts
,
1335 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1338 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1341 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1344 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1345 flags
|= ZFS_ITER_RECVD_PROPS
;
1348 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1352 (void) fprintf(stderr
,
1353 gettext("\"all\" conflicts "
1354 "with specific fields "
1355 "given to -o option\n"));
1358 cb
.cb_columns
[0] = GET_COL_NAME
;
1359 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1360 cb
.cb_columns
[2] = GET_COL_VALUE
;
1361 cb
.cb_columns
[3] = GET_COL_RECVD
;
1362 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1363 flags
|= ZFS_ITER_RECVD_PROPS
;
1367 (void) fprintf(stderr
,
1368 gettext("invalid column name "
1377 while (*optarg
!= '\0') {
1378 static char *source_subopts
[] = {
1379 "local", "default", "inherited",
1380 "received", "temporary", "none",
1383 switch (getsubopt(&optarg
, source_subopts
,
1386 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1389 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1392 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1395 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1398 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1401 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1404 (void) fprintf(stderr
,
1405 gettext("invalid source "
1413 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1423 (void) fprintf(stderr
, gettext("missing property "
1430 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1438 * As part of zfs_expand_proplist(), we keep track of the maximum column
1439 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1440 * need to know the maximum name length. However, the user likely did
1441 * not specify 'name' as one of the properties to fetch, so we need to
1442 * make sure we always include at least this property for
1443 * print_get_headers() to work properly.
1445 if (cb
.cb_proplist
!= NULL
) {
1446 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1447 fake_name
.pl_width
= strlen(gettext("NAME"));
1448 fake_name
.pl_next
= cb
.cb_proplist
;
1449 cb
.cb_proplist
= &fake_name
;
1452 cb
.cb_first
= B_TRUE
;
1454 /* run for each object */
1455 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
, NULL
,
1456 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1458 if (cb
.cb_proplist
== &fake_name
)
1459 zprop_free_list(fake_name
.pl_next
);
1461 zprop_free_list(cb
.cb_proplist
);
1467 * inherit [-rS] <property> <fs|vol> ...
1469 * -r Recurse over all children
1470 * -S Revert to received value, if any
1472 * For each dataset specified on the command line, inherit the given property
1473 * from its parent. Inheriting a property at the pool level will cause it to
1474 * use the default value. The '-r' flag will recurse over all children, and is
1475 * useful for setting a property on a hierarchy-wide basis, regardless of any
1476 * local modifications for each dataset.
1479 typedef struct inherit_cbdata
{
1480 const char *cb_propname
;
1481 boolean_t cb_received
;
1485 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1487 inherit_cbdata_t
*cb
= data
;
1488 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1491 * If we're doing it recursively, then ignore properties that
1492 * are not valid for this type of dataset.
1494 if (prop
!= ZPROP_INVAL
&&
1495 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1498 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1502 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1504 inherit_cbdata_t
*cb
= data
;
1506 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1510 zfs_do_inherit(int argc
, char **argv
)
1514 inherit_cbdata_t cb
= { 0 };
1518 boolean_t received
= B_FALSE
;
1521 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1524 flags
|= ZFS_ITER_RECURSE
;
1531 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1540 /* check number of arguments */
1542 (void) fprintf(stderr
, gettext("missing property argument\n"));
1546 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1554 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1555 if (zfs_prop_readonly(prop
)) {
1556 (void) fprintf(stderr
, gettext(
1557 "%s property is read-only\n"),
1561 if (!zfs_prop_inheritable(prop
) && !received
) {
1562 (void) fprintf(stderr
, gettext("'%s' property cannot "
1563 "be inherited\n"), propname
);
1564 if (prop
== ZFS_PROP_QUOTA
||
1565 prop
== ZFS_PROP_RESERVATION
||
1566 prop
== ZFS_PROP_REFQUOTA
||
1567 prop
== ZFS_PROP_REFRESERVATION
)
1568 (void) fprintf(stderr
, gettext("use 'zfs set "
1569 "%s=none' to clear\n"), propname
);
1572 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1573 prop
== ZFS_PROP_VERSION
)) {
1574 (void) fprintf(stderr
, gettext("'%s' property cannot "
1575 "be reverted to a received value\n"), propname
);
1578 } else if (!zfs_prop_user(propname
)) {
1579 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1584 cb
.cb_propname
= propname
;
1585 cb
.cb_received
= received
;
1587 if (flags
& ZFS_ITER_RECURSE
) {
1588 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1589 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1591 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1592 NULL
, NULL
, 0, inherit_cb
, &cb
);
1598 typedef struct upgrade_cbdata
{
1599 uint64_t cb_numupgraded
;
1600 uint64_t cb_numsamegraded
;
1601 uint64_t cb_numfailed
;
1602 uint64_t cb_version
;
1604 boolean_t cb_foundone
;
1605 char cb_lastfs
[ZFS_MAXNAMELEN
];
1609 same_pool(zfs_handle_t
*zhp
, const char *name
)
1611 int len1
= strcspn(name
, "/@");
1612 const char *zhname
= zfs_get_name(zhp
);
1613 int len2
= strcspn(zhname
, "/@");
1617 return (strncmp(name
, zhname
, len1
) == 0);
1621 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1623 upgrade_cbdata_t
*cb
= data
;
1624 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1626 /* list if it's old/new */
1627 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1628 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1631 str
= gettext("The following filesystems are "
1632 "formatted using a newer software version and\n"
1633 "cannot be accessed on the current system.\n\n");
1635 str
= gettext("The following filesystems are "
1636 "out of date, and can be upgraded. After being\n"
1637 "upgraded, these filesystems (and any 'zfs send' "
1638 "streams generated from\n"
1639 "subsequent snapshots) will no longer be "
1640 "accessible by older software versions.\n\n");
1643 if (!cb
->cb_foundone
) {
1645 (void) printf(gettext("VER FILESYSTEM\n"));
1646 (void) printf(gettext("--- ------------\n"));
1647 cb
->cb_foundone
= B_TRUE
;
1650 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1657 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1659 upgrade_cbdata_t
*cb
= data
;
1660 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1661 int needed_spa_version
;
1664 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1667 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
1669 if (needed_spa_version
< 0)
1672 if (spa_version
< needed_spa_version
) {
1674 (void) printf(gettext("%s: can not be "
1675 "upgraded; the pool version needs to first "
1676 "be upgraded\nto version %d\n\n"),
1677 zfs_get_name(zhp
), needed_spa_version
);
1683 if (version
< cb
->cb_version
) {
1685 (void) snprintf(verstr
, sizeof (verstr
),
1686 "%llu", (u_longlong_t
)cb
->cb_version
);
1687 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1689 * If they did "zfs upgrade -a", then we could
1690 * be doing ioctls to different pools. We need
1691 * to log this history once to each pool.
1693 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
1695 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1696 cb
->cb_numupgraded
++;
1699 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1700 } else if (version
> cb
->cb_version
) {
1701 /* can't downgrade */
1702 (void) printf(gettext("%s: can not be downgraded; "
1703 "it is already at version %u\n"),
1704 zfs_get_name(zhp
), version
);
1707 cb
->cb_numsamegraded
++;
1715 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1718 zfs_do_upgrade(int argc
, char **argv
)
1720 boolean_t all
= B_FALSE
;
1721 boolean_t showversions
= B_FALSE
;
1723 upgrade_cbdata_t cb
= { 0 };
1725 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1728 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1731 flags
|= ZFS_ITER_RECURSE
;
1734 showversions
= B_TRUE
;
1737 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
1738 optarg
, &cb
.cb_version
) != 0) {
1739 (void) fprintf(stderr
,
1740 gettext("invalid version %s\n"), optarg
);
1749 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1758 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
1760 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
1761 cb
.cb_version
|| argc
))
1763 if ((all
|| argc
) && (showversions
))
1769 /* Show info on available versions. */
1770 (void) printf(gettext("The following filesystem versions are "
1772 (void) printf(gettext("VER DESCRIPTION\n"));
1773 (void) printf("--- -----------------------------------------"
1774 "---------------\n");
1775 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1776 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1777 (void) printf(gettext(" 3 Case insensitive and File system "
1778 "unique identifier (FUID)\n"));
1779 (void) printf(gettext(" 4 userquota, groupquota "
1781 (void) printf(gettext(" 5 System attributes\n"));
1782 (void) printf(gettext("\nFor more information on a particular "
1783 "version, including supported releases,\n"));
1784 (void) printf("see the ZFS Administration Guide.\n\n");
1786 } else if (argc
|| all
) {
1787 /* Upgrade filesystems */
1788 if (cb
.cb_version
== 0)
1789 cb
.cb_version
= ZPL_VERSION
;
1790 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
1791 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
1792 (void) printf(gettext("%llu filesystems upgraded\n"),
1793 (u_longlong_t
)cb
.cb_numupgraded
);
1794 if (cb
.cb_numsamegraded
) {
1795 (void) printf(gettext("%llu filesystems already at "
1797 (u_longlong_t
)cb
.cb_numsamegraded
);
1799 if (cb
.cb_numfailed
!= 0)
1802 /* List old-version filesytems */
1804 (void) printf(gettext("This system is currently running "
1805 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
1807 flags
|= ZFS_ITER_RECURSE
;
1808 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1809 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
1811 found
= cb
.cb_foundone
;
1812 cb
.cb_foundone
= B_FALSE
;
1813 cb
.cb_newer
= B_TRUE
;
1815 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1816 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
1818 if (!cb
.cb_foundone
&& !found
) {
1819 (void) printf(gettext("All filesystems are "
1820 "formatted with the current version.\n"));
1831 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
1833 zfs_userquota_prop_t
*typep
= arg
;
1834 zfs_userquota_prop_t p
= *typep
;
1836 char *ug
, *propname
;
1840 if (domain
== NULL
|| domain
[0] == '\0') {
1841 if (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) {
1842 struct group
*g
= getgrgid(rid
);
1846 struct passwd
*p
= getpwuid(rid
);
1852 if (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
)
1857 if (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_GROUPUSED
)
1863 (void) snprintf(namebuf
, sizeof (namebuf
),
1864 "%llu", (longlong_t
)rid
);
1867 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
1869 (void) printf("%s %s %s%c%s %s\n", propname
, ug
, domain
,
1870 domain
[0] ? '-' : ' ', name
, sizebuf
);
1876 zfs_do_userspace(int argc
, char **argv
)
1879 zfs_userquota_prop_t p
;
1882 if ((zhp
= zfs_open(g_zfs
, argv
[argc
-1], ZFS_TYPE_DATASET
)) == NULL
)
1885 (void) printf("PROP TYPE NAME VALUE\n");
1887 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
1888 error
= zfs_userspace(zhp
, p
, userspace_cb
, &p
);
1896 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
1897 * [-s property [-s property]...] [-S property [-S property]...]
1900 * -r Recurse over all children
1901 * -d Limit recursion by depth.
1902 * -H Scripted mode; elide headers and separate columns by tabs
1903 * -o Control which fields to display.
1904 * -t Control which object types to display.
1905 * -s Specify sort columns, descending order.
1906 * -S Specify sort columns, ascending order.
1908 * When given no arguments, lists all filesystems in the system.
1909 * Otherwise, list the specified datasets, optionally recursing down them if
1910 * '-r' is specified.
1912 typedef struct list_cbdata
{
1914 boolean_t cb_scripted
;
1915 zprop_list_t
*cb_proplist
;
1919 * Given a list of columns to display, output appropriate headers for each one.
1922 print_header(zprop_list_t
*pl
)
1924 char headerbuf
[ZFS_MAXPROPLEN
];
1927 boolean_t first
= B_TRUE
;
1928 boolean_t right_justify
;
1930 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1937 right_justify
= B_FALSE
;
1938 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1939 header
= zfs_prop_column_name(pl
->pl_prop
);
1940 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1942 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
1943 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
1944 headerbuf
[i
] = '\0';
1948 if (pl
->pl_next
== NULL
&& !right_justify
)
1949 (void) printf("%s", header
);
1950 else if (right_justify
)
1951 (void) printf("%*s", (int)pl
->pl_width
, header
);
1953 (void) printf("%-*s", (int)pl
->pl_width
, header
);
1956 (void) printf("\n");
1960 * Given a dataset and a list of fields, print out all the properties according
1961 * to the described layout.
1964 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
1966 boolean_t first
= B_TRUE
;
1967 char property
[ZFS_MAXPROPLEN
];
1968 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
1971 boolean_t right_justify
;
1974 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1977 (void) printf("\t");
1984 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1985 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
1986 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
1991 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1992 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1993 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1994 property
, sizeof (property
), B_FALSE
) != 0)
1998 right_justify
= B_TRUE
;
2000 if (nvlist_lookup_nvlist(userprops
,
2001 pl
->pl_user_prop
, &propval
) != 0)
2004 verify(nvlist_lookup_string(propval
,
2005 ZPROP_VALUE
, &propstr
) == 0);
2006 right_justify
= B_FALSE
;
2009 width
= pl
->pl_width
;
2012 * If this is being called in scripted mode, or if this is the
2013 * last column and it is left-justified, don't include a width
2016 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2017 (void) printf("%s", propstr
);
2018 else if (right_justify
)
2019 (void) printf("%*s", width
, propstr
);
2021 (void) printf("%-*s", width
, propstr
);
2024 (void) printf("\n");
2028 * Generic callback function to list a dataset or snapshot.
2031 list_callback(zfs_handle_t
*zhp
, void *data
)
2033 list_cbdata_t
*cbp
= data
;
2035 if (cbp
->cb_first
) {
2036 if (!cbp
->cb_scripted
)
2037 print_header(cbp
->cb_proplist
);
2038 cbp
->cb_first
= B_FALSE
;
2041 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
2047 zfs_do_list(int argc
, char **argv
)
2050 boolean_t scripted
= B_FALSE
;
2051 static char default_fields
[] =
2052 "name,used,available,referenced,mountpoint";
2053 int types
= ZFS_TYPE_DATASET
;
2054 boolean_t types_specified
= B_FALSE
;
2055 char *fields
= NULL
;
2056 list_cbdata_t cb
= { 0 };
2060 zfs_sort_column_t
*sortcol
= NULL
;
2061 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
2064 while ((c
= getopt(argc
, argv
, ":d:o:rt:Hs:S:")) != -1) {
2070 limit
= parse_depth(optarg
, &flags
);
2073 flags
|= ZFS_ITER_RECURSE
;
2079 if (zfs_add_sort_column(&sortcol
, optarg
,
2081 (void) fprintf(stderr
,
2082 gettext("invalid property '%s'\n"), optarg
);
2087 if (zfs_add_sort_column(&sortcol
, optarg
,
2089 (void) fprintf(stderr
,
2090 gettext("invalid property '%s'\n"), optarg
);
2096 types_specified
= B_TRUE
;
2097 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2098 while (*optarg
!= '\0') {
2099 static char *type_subopts
[] = { "filesystem",
2100 "volume", "snapshot", "all", NULL
};
2102 switch (getsubopt(&optarg
, type_subopts
,
2105 types
|= ZFS_TYPE_FILESYSTEM
;
2108 types
|= ZFS_TYPE_VOLUME
;
2111 types
|= ZFS_TYPE_SNAPSHOT
;
2114 types
= ZFS_TYPE_DATASET
;
2118 (void) fprintf(stderr
,
2119 gettext("invalid type '%s'\n"),
2126 (void) fprintf(stderr
, gettext("missing argument for "
2127 "'%c' option\n"), optopt
);
2131 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2141 fields
= default_fields
;
2144 * If "-o space" and no types were specified, don't display snapshots.
2146 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
2147 types
&= ~ZFS_TYPE_SNAPSHOT
;
2150 * If the user specifies '-o all', the zprop_get_list() doesn't
2151 * normally include the name of the dataset. For 'zfs list', we always
2152 * want this property to be first.
2154 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
2158 cb
.cb_scripted
= scripted
;
2159 cb
.cb_first
= B_TRUE
;
2161 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
2162 limit
, list_callback
, &cb
);
2164 zprop_free_list(cb
.cb_proplist
);
2165 zfs_free_sort_columns(sortcol
);
2167 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
2168 (void) printf(gettext("no datasets available\n"));
2174 * zfs rename <fs | snap | vol> <fs | snap | vol>
2175 * zfs rename -p <fs | vol> <fs | vol>
2176 * zfs rename -r <snap> <snap>
2178 * Renames the given dataset to another of the same type.
2180 * The '-p' flag creates all the non-existing ancestors of the target first.
2184 zfs_do_rename(int argc
, char **argv
)
2189 boolean_t recurse
= B_FALSE
;
2190 boolean_t parents
= B_FALSE
;
2193 while ((c
= getopt(argc
, argv
, "pr")) != -1) {
2203 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2212 /* check number of arguments */
2214 (void) fprintf(stderr
, gettext("missing source dataset "
2219 (void) fprintf(stderr
, gettext("missing target dataset "
2224 (void) fprintf(stderr
, gettext("too many arguments\n"));
2228 if (recurse
&& parents
) {
2229 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
2234 if (recurse
&& strchr(argv
[0], '@') == 0) {
2235 (void) fprintf(stderr
, gettext("source dataset for recursive "
2236 "rename must be a snapshot\n"));
2240 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
2241 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
2244 /* If we were asked and the name looks good, try to create ancestors. */
2245 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
2246 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
2251 ret
= (zfs_rename(zhp
, argv
[1], recurse
) != 0);
2260 * Promotes the given clone fs to be the parent
2264 zfs_do_promote(int argc
, char **argv
)
2270 if (argc
> 1 && argv
[1][0] == '-') {
2271 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2276 /* check number of arguments */
2278 (void) fprintf(stderr
, gettext("missing clone filesystem"
2283 (void) fprintf(stderr
, gettext("too many arguments\n"));
2287 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2291 ret
= (zfs_promote(zhp
) != 0);
2299 * zfs rollback [-rRf] <snapshot>
2301 * -r Delete any intervening snapshots before doing rollback
2302 * -R Delete any snapshots and their clones
2303 * -f ignored for backwards compatability
2305 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2306 * since then and making it the active dataset. If more recent snapshots exist,
2307 * the command will complain unless the '-r' flag is given.
2309 typedef struct rollback_cbdata
{
2315 boolean_t cb_recurse
;
2316 boolean_t cb_dependent
;
2317 } rollback_cbdata_t
;
2320 * Report any snapshots more recent than the one specified. Used when '-r' is
2321 * not specified. We reuse this same callback for the snapshot dependents - if
2322 * 'cb_dependent' is set, then this is a dependent and we should report it
2323 * without checking the transaction group.
2326 rollback_check(zfs_handle_t
*zhp
, void *data
)
2328 rollback_cbdata_t
*cbp
= data
;
2330 if (cbp
->cb_doclones
) {
2335 if (!cbp
->cb_dependent
) {
2336 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
2337 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2338 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
2341 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
2342 (void) fprintf(stderr
, gettext("cannot "
2343 "rollback to '%s': more recent snapshots "
2346 (void) fprintf(stderr
, gettext("use '-r' to "
2347 "force deletion of the following "
2353 if (cbp
->cb_recurse
) {
2354 cbp
->cb_dependent
= B_TRUE
;
2355 if (zfs_iter_dependents(zhp
, B_TRUE
,
2356 rollback_check
, cbp
) != 0) {
2360 cbp
->cb_dependent
= B_FALSE
;
2362 (void) fprintf(stderr
, "%s\n",
2367 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
2368 (void) fprintf(stderr
, gettext("cannot rollback to "
2369 "'%s': clones of previous snapshots exist\n"),
2371 (void) fprintf(stderr
, gettext("use '-R' to "
2372 "force deletion of the following clones and "
2378 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
2386 zfs_do_rollback(int argc
, char **argv
)
2390 boolean_t force
= B_FALSE
;
2391 rollback_cbdata_t cb
= { 0 };
2392 zfs_handle_t
*zhp
, *snap
;
2393 char parentname
[ZFS_MAXNAMELEN
];
2397 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
2410 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2419 /* check number of arguments */
2421 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2425 (void) fprintf(stderr
, gettext("too many arguments\n"));
2429 /* open the snapshot */
2430 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
2433 /* open the parent dataset */
2434 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
2435 verify((delim
= strrchr(parentname
, '@')) != NULL
);
2437 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
2443 * Check for more recent snapshots and/or clones based on the presence
2446 cb
.cb_target
= argv
[0];
2447 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
2448 cb
.cb_first
= B_TRUE
;
2450 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
2453 if ((ret
= cb
.cb_error
) != 0)
2457 * Rollback parent to the given snapshot.
2459 ret
= zfs_rollback(zhp
, snap
, force
);
2472 * zfs set property=value { fs | snap | vol } ...
2474 * Sets the given property for all datasets specified on the command line.
2476 typedef struct set_cbdata
{
2482 set_callback(zfs_handle_t
*zhp
, void *data
)
2484 set_cbdata_t
*cbp
= data
;
2486 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
2487 switch (libzfs_errno(g_zfs
)) {
2488 case EZFS_MOUNTFAILED
:
2489 (void) fprintf(stderr
, gettext("property may be set "
2490 "but unable to remount filesystem\n"));
2492 case EZFS_SHARENFSFAILED
:
2493 (void) fprintf(stderr
, gettext("property may be set "
2494 "but unable to reshare filesystem\n"));
2503 zfs_do_set(int argc
, char **argv
)
2508 /* check for options */
2509 if (argc
> 1 && argv
[1][0] == '-') {
2510 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2515 /* check number of arguments */
2517 (void) fprintf(stderr
, gettext("missing property=value "
2522 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2526 /* validate property=value argument */
2527 cb
.cb_propname
= argv
[1];
2528 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
2529 (cb
.cb_value
[1] == '\0')) {
2530 (void) fprintf(stderr
, gettext("missing value in "
2531 "property=value argument\n"));
2535 *cb
.cb_value
= '\0';
2538 if (*cb
.cb_propname
== '\0') {
2539 (void) fprintf(stderr
,
2540 gettext("missing property in property=value argument\n"));
2544 ret
= zfs_for_each(argc
- 2, argv
+ 2, 0,
2545 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
2551 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2553 * Creates a snapshot with the given name. While functionally equivalent to
2554 * 'zfs create', it is a separate command to differentiate intent.
2557 zfs_do_snapshot(int argc
, char **argv
)
2559 boolean_t recursive
= B_FALSE
;
2564 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2568 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
2571 if (parseprop(props
))
2578 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2587 /* check number of arguments */
2589 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2593 (void) fprintf(stderr
, gettext("too many arguments\n"));
2597 ret
= zfs_snapshot(g_zfs
, argv
[0], recursive
, props
);
2599 if (ret
&& recursive
)
2600 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
2610 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
2611 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
2613 * Send a backup stream to stdout.
2616 zfs_do_send(int argc
, char **argv
)
2618 char *fromname
= NULL
;
2619 char *toname
= NULL
;
2622 sendflags_t flags
= { 0 };
2625 boolean_t extraverbose
= B_FALSE
;
2628 while ((c
= getopt(argc
, argv
, ":i:I:RDpv")) != -1) {
2639 flags
.doall
= B_TRUE
;
2642 flags
.replicate
= B_TRUE
;
2645 flags
.props
= B_TRUE
;
2649 extraverbose
= B_TRUE
;
2650 flags
.verbose
= B_TRUE
;
2653 flags
.dedup
= B_TRUE
;
2656 (void) fprintf(stderr
, gettext("missing argument for "
2657 "'%c' option\n"), optopt
);
2661 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2670 /* check number of arguments */
2672 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2676 (void) fprintf(stderr
, gettext("too many arguments\n"));
2680 if (isatty(STDOUT_FILENO
)) {
2681 (void) fprintf(stderr
,
2682 gettext("Error: Stream can not be written to a terminal.\n"
2683 "You must redirect standard output.\n"));
2687 cp
= strchr(argv
[0], '@');
2689 (void) fprintf(stderr
,
2690 gettext("argument must be a snapshot\n"));
2695 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2700 * If they specified the full path to the snapshot, chop off
2701 * everything except the short name of the snapshot, but special
2702 * case if they specify the origin.
2704 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
2705 char origin
[ZFS_MAXNAMELEN
];
2708 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
2709 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
2711 if (strcmp(origin
, fromname
) == 0) {
2713 flags
.fromorigin
= B_TRUE
;
2716 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
2717 (void) fprintf(stderr
,
2718 gettext("incremental source must be "
2719 "in same filesystem\n"));
2723 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
2724 (void) fprintf(stderr
,
2725 gettext("invalid incremental source\n"));
2731 if (flags
.replicate
&& fromname
== NULL
)
2732 flags
.doall
= B_TRUE
;
2734 err
= zfs_send(zhp
, fromname
, toname
, flags
, STDOUT_FILENO
, NULL
, 0,
2735 extraverbose
? &dbgnv
: NULL
);
2739 * dump_nvlist prints to stdout, but that's been
2740 * redirected to a file. Make it print to stderr
2743 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
2744 dump_nvlist(dbgnv
, 0);
2753 * zfs receive [-vnFu] [-d | -e] <fs@snap>
2755 * Restore a backup stream from stdin.
2758 zfs_do_receive(int argc
, char **argv
)
2761 recvflags_t flags
= { 0 };
2764 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
2767 flags
.isprefix
= B_TRUE
;
2770 flags
.isprefix
= B_TRUE
;
2771 flags
.istail
= B_TRUE
;
2774 flags
.dryrun
= B_TRUE
;
2777 flags
.nomount
= B_TRUE
;
2780 flags
.verbose
= B_TRUE
;
2783 flags
.force
= B_TRUE
;
2786 (void) fprintf(stderr
, gettext("missing argument for "
2787 "'%c' option\n"), optopt
);
2791 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2800 /* check number of arguments */
2802 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2806 (void) fprintf(stderr
, gettext("too many arguments\n"));
2810 if (isatty(STDIN_FILENO
)) {
2811 (void) fprintf(stderr
,
2812 gettext("Error: Backup stream can not be read "
2813 "from a terminal.\n"
2814 "You must redirect standard input.\n"));
2818 err
= zfs_receive(g_zfs
, argv
[0], flags
, STDIN_FILENO
, NULL
);
2824 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
2829 boolean_t recursive
= B_FALSE
;
2830 boolean_t temphold
= B_FALSE
;
2831 const char *opts
= holding
? "rt" : "r";
2835 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
2844 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2853 /* check number of arguments */
2861 if (holding
&& tag
[0] == '.') {
2862 /* tags starting with '.' are reserved for libzfs */
2863 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
2867 for (i
= 0; i
< argc
; ++i
) {
2869 char parent
[ZFS_MAXNAMELEN
];
2871 char *path
= argv
[i
];
2873 delim
= strchr(path
, '@');
2874 if (delim
== NULL
) {
2875 (void) fprintf(stderr
,
2876 gettext("'%s' is not a snapshot\n"), path
);
2880 (void) strncpy(parent
, path
, delim
- path
);
2881 parent
[delim
- path
] = '\0';
2883 zhp
= zfs_open(g_zfs
, parent
,
2884 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2890 if (zfs_hold(zhp
, delim
+1, tag
, recursive
,
2891 temphold
, B_FALSE
, -1, 0, 0) != 0)
2894 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
2900 return (errors
!= 0);
2904 * zfs hold [-r] [-t] <tag> <snap> ...
2906 * -r Recursively hold
2907 * -t Temporary hold (hidden option)
2909 * Apply a user-hold with the given tag to the list of snapshots.
2912 zfs_do_hold(int argc
, char **argv
)
2914 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
2918 * zfs release [-r] <tag> <snap> ...
2920 * -r Recursively release
2922 * Release a user-hold with the given tag from the list of snapshots.
2925 zfs_do_release(int argc
, char **argv
)
2927 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
2930 #define CHECK_SPINNER 30
2931 #define SPINNER_TIME 3 /* seconds */
2932 #define MOUNT_TIME 5 /* seconds */
2935 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
2937 static char *spin
[] = { "-", "\\", "|", "/" };
2938 static int spinval
= 0;
2939 static int spincheck
= 0;
2940 static time_t last_spin_time
= (time_t)0;
2941 get_all_cb_t
*cbp
= data
;
2942 zfs_type_t type
= zfs_get_type(zhp
);
2944 if (cbp
->cb_verbose
) {
2945 if (--spincheck
< 0) {
2946 time_t now
= time(NULL
);
2947 if (last_spin_time
+ SPINNER_TIME
< now
) {
2948 update_progress(spin
[spinval
++ % 4]);
2949 last_spin_time
= now
;
2951 spincheck
= CHECK_SPINNER
;
2956 * Interate over any nested datasets.
2958 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
2964 * Skip any datasets whose type does not match.
2966 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
2970 libzfs_add_handle(cbp
, zhp
);
2971 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
2977 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
2979 get_all_cb_t cb
= { 0 };
2980 cb
.cb_verbose
= verbose
;
2981 cb
.cb_getone
= get_one_dataset
;
2984 set_progress_header(gettext("Reading ZFS config"));
2985 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
2987 *dslist
= cb
.cb_handles
;
2988 *count
= cb
.cb_used
;
2991 finish_progress(gettext("done."));
2995 * Generic callback for sharing or mounting filesystems. Because the code is so
2996 * similar, we have a common function with an extra parameter to determine which
2997 * mode we are using.
2999 #define OP_SHARE 0x1
3000 #define OP_MOUNT 0x2
3003 * Share or mount a dataset.
3006 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
3007 boolean_t
explicit, const char *options
)
3009 char mountpoint
[ZFS_MAXPROPLEN
];
3010 char shareopts
[ZFS_MAXPROPLEN
];
3011 char smbshareopts
[ZFS_MAXPROPLEN
];
3012 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
3014 uint64_t zoned
, canmount
;
3015 boolean_t shared_nfs
, shared_smb
;
3017 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
3020 * Check to make sure we can mount/share this dataset. If we
3021 * are in the global zone and the filesystem is exported to a
3022 * local zone, or if we are in a local zone and the
3023 * filesystem is not exported, then it is an error.
3025 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3027 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
3031 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3032 "dataset is exported to a local zone\n"), cmdname
,
3036 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
3040 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3041 "permission denied\n"), cmdname
,
3047 * Ignore any filesystems which don't apply to us. This
3048 * includes those with a legacy mountpoint, or those with
3049 * legacy share options.
3051 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
3052 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
3053 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
3054 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3055 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
3056 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3058 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
3059 strcmp(smbshareopts
, "off") == 0) {
3063 (void) fprintf(stderr
, gettext("cannot share '%s': "
3064 "legacy share\n"), zfs_get_name(zhp
));
3065 (void) fprintf(stderr
, gettext("use share(1M) to "
3066 "share this filesystem, or set "
3067 "sharenfs property on\n"));
3072 * We cannot share or mount legacy filesystems. If the
3073 * shareopts is non-legacy but the mountpoint is legacy, we
3074 * treat it as a legacy share.
3076 if (strcmp(mountpoint
, "legacy") == 0) {
3080 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3081 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
3082 (void) fprintf(stderr
, gettext("use %s(1M) to "
3083 "%s this filesystem\n"), cmdname
, cmdname
);
3087 if (strcmp(mountpoint
, "none") == 0) {
3091 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
3092 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
3097 * canmount explicit outcome
3098 * on no pass through
3099 * on yes pass through
3101 * off yes display error, return 1
3102 * noauto no return 0
3103 * noauto yes pass through
3105 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
3106 if (canmount
== ZFS_CANMOUNT_OFF
) {
3110 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3111 "'canmount' property is set to 'off'\n"), cmdname
,
3114 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
3119 * At this point, we have verified that the mountpoint and/or
3120 * shareopts are appropriate for auto management. If the
3121 * filesystem is already mounted or shared, return (failing
3122 * for explicit requests); otherwise mount or share the
3128 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
3129 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
3131 if ((shared_nfs
&& shared_smb
) ||
3132 ((shared_nfs
&& strcmp(shareopts
, "on") == 0) &&
3133 (strcmp(smbshareopts
, "off") == 0)) ||
3134 ((shared_smb
&& strcmp(smbshareopts
, "on") == 0) &&
3135 (strcmp(shareopts
, "off") == 0))) {
3139 (void) fprintf(stderr
, gettext("cannot share "
3140 "'%s': filesystem already shared\n"),
3145 if (!zfs_is_mounted(zhp
, NULL
) &&
3146 zfs_mount(zhp
, NULL
, 0) != 0)
3149 if (protocol
== NULL
) {
3150 if (zfs_shareall(zhp
) != 0)
3152 } else if (strcmp(protocol
, "nfs") == 0) {
3153 if (zfs_share_nfs(zhp
))
3155 } else if (strcmp(protocol
, "smb") == 0) {
3156 if (zfs_share_smb(zhp
))
3159 (void) fprintf(stderr
, gettext("cannot share "
3160 "'%s': invalid share type '%s' "
3162 zfs_get_name(zhp
), protocol
);
3169 if (options
== NULL
)
3170 mnt
.mnt_mntopts
= "";
3172 mnt
.mnt_mntopts
= (char *)options
;
3174 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
3175 zfs_is_mounted(zhp
, NULL
)) {
3179 (void) fprintf(stderr
, gettext("cannot mount "
3180 "'%s': filesystem already mounted\n"),
3185 if (zfs_mount(zhp
, options
, flags
) != 0)
3194 * Reports progress in the form "(current/total)". Not thread-safe.
3197 report_mount_progress(int current
, int total
)
3199 static time_t last_progress_time
= 0;
3200 time_t now
= time(NULL
);
3203 /* report 1..n instead of 0..n-1 */
3206 /* display header if we're here for the first time */
3208 set_progress_header(gettext("Mounting ZFS filesystems"));
3209 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
3210 /* too soon to report again */
3214 last_progress_time
= now
;
3216 (void) sprintf(info
, "(%d/%d)", current
, total
);
3218 if (current
== total
)
3219 finish_progress(info
);
3221 update_progress(info
);
3225 append_options(char *mntopts
, char *newopts
)
3227 int len
= strlen(mntopts
);
3229 /* original length plus new string to append plus 1 for the comma */
3230 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
3231 (void) fprintf(stderr
, gettext("the opts argument for "
3232 "'%s' option is too long (more than %d chars)\n"),
3233 "-o", MNT_LINE_MAX
);
3238 mntopts
[len
++] = ',';
3240 (void) strcpy(&mntopts
[len
], newopts
);
3244 share_mount(int op
, int argc
, char **argv
)
3247 boolean_t verbose
= B_FALSE
;
3249 char *options
= NULL
;
3253 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:" : "a"))
3263 if (*optarg
== '\0') {
3264 (void) fprintf(stderr
, gettext("empty mount "
3265 "options (-o) specified\n"));
3269 if (options
== NULL
)
3270 options
= safe_malloc(MNT_LINE_MAX
+ 1);
3272 /* option validation is done later */
3273 append_options(options
, optarg
);
3277 (void) fprintf(stderr
, gettext("missing argument for "
3278 "'%c' option\n"), optopt
);
3282 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3291 /* check number of arguments */
3293 zfs_handle_t
**dslist
= NULL
;
3294 size_t i
, count
= 0;
3295 char *protocol
= NULL
;
3297 if (op
== OP_SHARE
&& argc
> 0) {
3298 if (strcmp(argv
[0], "nfs") != 0 &&
3299 strcmp(argv
[0], "smb") != 0) {
3300 (void) fprintf(stderr
, gettext("share type "
3301 "must be 'nfs' or 'smb'\n"));
3310 (void) fprintf(stderr
, gettext("too many arguments\n"));
3314 start_progress_timer();
3315 get_all_datasets(&dslist
, &count
, verbose
);
3320 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
3322 for (i
= 0; i
< count
; i
++) {
3324 report_mount_progress(i
, count
);
3326 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
3327 B_FALSE
, options
) != 0)
3329 zfs_close(dslist
[i
]);
3333 } else if (argc
== 0) {
3334 struct mnttab entry
;
3336 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
3337 (void) fprintf(stderr
, gettext("missing filesystem "
3338 "argument (specify -a for all)\n"));
3343 * When mount is given no arguments, go through /etc/mtab and
3344 * display any active ZFS mounts. We hide any snapshots, since
3345 * they are controlled automatically.
3347 rewind(mnttab_file
);
3348 while (getmntent(mnttab_file
, &entry
) == 0) {
3349 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
3350 strchr(entry
.mnt_special
, '@') != NULL
)
3353 (void) printf("%-30s %s\n", entry
.mnt_special
,
3361 (void) fprintf(stderr
,
3362 gettext("too many arguments\n"));
3366 if ((zhp
= zfs_open(g_zfs
, argv
[0],
3367 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3370 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
3380 * zfs mount -a [nfs]
3381 * zfs mount filesystem
3383 * Mount all filesystems, or mount the given filesystem.
3386 zfs_do_mount(int argc
, char **argv
)
3388 return (share_mount(OP_MOUNT
, argc
, argv
));
3392 * zfs share -a [nfs | smb]
3393 * zfs share filesystem
3395 * Share all filesystems, or share the given filesystem.
3398 zfs_do_share(int argc
, char **argv
)
3400 return (share_mount(OP_SHARE
, argc
, argv
));
3403 typedef struct unshare_unmount_node
{
3404 zfs_handle_t
*un_zhp
;
3406 uu_avl_node_t un_avlnode
;
3407 } unshare_unmount_node_t
;
3411 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
3413 const unshare_unmount_node_t
*l
= larg
;
3414 const unshare_unmount_node_t
*r
= rarg
;
3416 return (strcmp(l
->un_mountp
, r
->un_mountp
));
3420 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3421 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
3422 * and unmount it appropriately.
3425 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
3429 struct stat64 statbuf
;
3430 struct extmnttab entry
;
3431 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
3435 * Search for the path in /etc/mtab. Rather than looking for the
3436 * specific path, which can be fooled by non-standard paths (i.e. ".."
3437 * or "//"), we stat() the path and search for the corresponding
3438 * (major,minor) device pair.
3440 if (stat64(path
, &statbuf
) != 0) {
3441 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3442 cmdname
, path
, strerror(errno
));
3445 path_inode
= statbuf
.st_ino
;
3448 * Search for the given (major,minor) pair in the mount table.
3450 rewind(mnttab_file
);
3451 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
3452 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
3453 entry
.mnt_minor
== minor(statbuf
.st_dev
))
3457 if (op
== OP_SHARE
) {
3458 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
3459 "currently mounted\n"), cmdname
, path
);
3462 (void) fprintf(stderr
, gettext("warning: %s not in mtab\n"),
3464 if ((ret
= umount2(path
, flags
)) != 0)
3465 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
3470 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
3471 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
3472 "filesystem\n"), cmdname
, path
);
3476 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3477 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3481 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
3482 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3483 cmdname
, path
, strerror(errno
));
3485 } else if (statbuf
.st_ino
!= path_inode
) {
3486 (void) fprintf(stderr
, gettext("cannot "
3487 "%s '%s': not a mountpoint\n"), cmdname
, path
);
3491 if (op
== OP_SHARE
) {
3492 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3493 char smbshare_prop
[ZFS_MAXPROPLEN
];
3495 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
3496 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3497 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
3498 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3500 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3501 strcmp(smbshare_prop
, "off") == 0) {
3502 (void) fprintf(stderr
, gettext("cannot unshare "
3503 "'%s': legacy share\n"), path
);
3504 (void) fprintf(stderr
, gettext("use exportfs(8) "
3505 "or smbcontrol(1) to unshare this filesystem\n"));
3506 } else if (!zfs_is_shared(zhp
)) {
3507 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
3508 "not currently shared\n"), path
);
3510 ret
= zfs_unshareall_bypath(zhp
, path
);
3513 char mtpt_prop
[ZFS_MAXPROPLEN
];
3515 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
3516 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3519 ret
= zfs_unmount(zhp
, NULL
, flags
);
3520 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
3521 (void) fprintf(stderr
, gettext("cannot unmount "
3522 "'%s': legacy mountpoint\n"),
3524 (void) fprintf(stderr
, gettext("use umount(8) "
3525 "to unmount this filesystem\n"));
3527 ret
= zfs_unmountall(zhp
, flags
);
3538 * Generic callback for unsharing or unmounting a filesystem.
3541 unshare_unmount(int op
, int argc
, char **argv
)
3548 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3549 char sharesmb
[ZFS_MAXPROPLEN
];
3552 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
3561 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3572 * We could make use of zfs_for_each() to walk all datasets in
3573 * the system, but this would be very inefficient, especially
3574 * since we would have to linearly search /etc/mtab for each
3575 * one. Instead, do one pass through /etc/mtab looking for
3576 * zfs entries and call zfs_unmount() for each one.
3578 * Things get a little tricky if the administrator has created
3579 * mountpoints beneath other ZFS filesystems. In this case, we
3580 * have to unmount the deepest filesystems first. To accomplish
3581 * this, we place all the mountpoints in an AVL tree sorted by
3582 * the special type (dataset name), and walk the result in
3583 * reverse to make sure to get any snapshots first.
3585 struct mnttab entry
;
3586 uu_avl_pool_t
*pool
;
3587 uu_avl_t
*tree
= NULL
;
3588 unshare_unmount_node_t
*node
;
3590 uu_avl_walk_t
*walk
;
3593 (void) fprintf(stderr
, gettext("too many arguments\n"));
3597 if (((pool
= uu_avl_pool_create("unmount_pool",
3598 sizeof (unshare_unmount_node_t
),
3599 offsetof(unshare_unmount_node_t
, un_avlnode
),
3600 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
3601 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
3604 rewind(mnttab_file
);
3605 while (getmntent(mnttab_file
, &entry
) == 0) {
3607 /* ignore non-ZFS entries */
3608 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
3611 /* ignore snapshots */
3612 if (strchr(entry
.mnt_special
, '@') != NULL
)
3615 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3616 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3623 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3625 sizeof (nfs_mnt_prop
),
3626 NULL
, NULL
, 0, B_FALSE
) == 0);
3627 if (strcmp(nfs_mnt_prop
, "off") != 0)
3629 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3631 sizeof (nfs_mnt_prop
),
3632 NULL
, NULL
, 0, B_FALSE
) == 0);
3633 if (strcmp(nfs_mnt_prop
, "off") == 0)
3637 /* Ignore legacy mounts */
3638 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
3640 sizeof (nfs_mnt_prop
),
3641 NULL
, NULL
, 0, B_FALSE
) == 0);
3642 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
3644 /* Ignore canmount=noauto mounts */
3645 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
3646 ZFS_CANMOUNT_NOAUTO
)
3652 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
3654 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
3656 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
3658 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
3659 uu_avl_insert(tree
, node
, idx
);
3661 zfs_close(node
->un_zhp
);
3662 free(node
->un_mountp
);
3668 * Walk the AVL tree in reverse, unmounting each filesystem and
3669 * removing it from the AVL tree in the process.
3671 if ((walk
= uu_avl_walk_start(tree
,
3672 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
3675 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3676 uu_avl_remove(tree
, node
);
3680 if (zfs_unshareall_bypath(node
->un_zhp
,
3681 node
->un_mountp
) != 0)
3686 if (zfs_unmount(node
->un_zhp
,
3687 node
->un_mountp
, flags
) != 0)
3692 zfs_close(node
->un_zhp
);
3693 free(node
->un_mountp
);
3697 uu_avl_walk_end(walk
);
3698 uu_avl_destroy(tree
);
3699 uu_avl_pool_destroy(pool
);
3704 (void) fprintf(stderr
,
3705 gettext("missing filesystem argument\n"));
3707 (void) fprintf(stderr
,
3708 gettext("too many arguments\n"));
3713 * We have an argument, but it may be a full path or a ZFS
3714 * filesystem. Pass full paths off to unmount_path() (shared by
3715 * manual_unmount), otherwise open the filesystem and pass to
3718 if (argv
[0][0] == '/')
3719 return (unshare_unmount_path(op
, argv
[0],
3722 if ((zhp
= zfs_open(g_zfs
, argv
[0],
3723 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3726 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
3727 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
3728 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
3729 NULL
, 0, B_FALSE
) == 0);
3733 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3735 sizeof (nfs_mnt_prop
),
3736 NULL
, NULL
, 0, B_FALSE
) == 0);
3737 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3738 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
3741 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3742 strcmp(sharesmb
, "off") == 0) {
3743 (void) fprintf(stderr
, gettext("cannot "
3744 "unshare '%s': legacy share\n"),
3746 (void) fprintf(stderr
, gettext("use "
3747 "unshare(1M) to unshare this "
3750 } else if (!zfs_is_shared(zhp
)) {
3751 (void) fprintf(stderr
, gettext("cannot "
3752 "unshare '%s': not currently "
3753 "shared\n"), zfs_get_name(zhp
));
3755 } else if (zfs_unshareall(zhp
) != 0) {
3761 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
3762 (void) fprintf(stderr
, gettext("cannot "
3763 "unmount '%s': legacy "
3764 "mountpoint\n"), zfs_get_name(zhp
));
3765 (void) fprintf(stderr
, gettext("use "
3766 "umount(1M) to unmount this "
3769 } else if (!zfs_is_mounted(zhp
, NULL
)) {
3770 (void) fprintf(stderr
, gettext("cannot "
3771 "unmount '%s': not currently "
3775 } else if (zfs_unmountall(zhp
, flags
) != 0) {
3789 * zfs unmount filesystem
3791 * Unmount all filesystems, or a specific ZFS filesystem.
3794 zfs_do_unmount(int argc
, char **argv
)
3796 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
3801 * zfs unshare filesystem
3803 * Unshare all filesystems, or a specific ZFS filesystem.
3806 zfs_do_unshare(int argc
, char **argv
)
3808 return (unshare_unmount(OP_SHARE
, argc
, argv
));
3813 zfs_do_python(int argc
, char **argv
)
3815 (void) execv(pypath
, argv
-1);
3816 (void) printf("internal error: %s not found\n", pypath
);
3821 find_command_idx(char *command
, int *idx
)
3825 for (i
= 0; i
< NCOMMAND
; i
++) {
3826 if (command_table
[i
].name
== NULL
)
3829 if (strcmp(command
, command_table
[i
].name
) == 0) {
3838 zfs_do_diff(int argc
, char **argv
)
3842 char *tosnap
= NULL
;
3843 char *fromsnap
= NULL
;
3848 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
3851 flags
|= ZFS_DIFF_CLASSIFY
;
3854 flags
|= ZFS_DIFF_PARSEABLE
;
3857 flags
|= ZFS_DIFF_TIMESTAMP
;
3860 (void) fprintf(stderr
,
3861 gettext("invalid option '%c'\n"), optopt
);
3870 (void) fprintf(stderr
,
3871 gettext("must provide at least one snapshot name\n"));
3876 (void) fprintf(stderr
, gettext("too many arguments\n"));
3881 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
3884 if (*fromsnap
!= '@')
3885 copy
= strdup(fromsnap
);
3887 copy
= strdup(tosnap
);
3891 if ((atp
= strchr(copy
, '@')))
3894 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
3900 * Ignore SIGPIPE so that the library can give us
3901 * information on any failure
3903 (void) sigignore(SIGPIPE
);
3905 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
3913 main(int argc
, char **argv
)
3919 (void) setlocale(LC_ALL
, "");
3920 (void) textdomain(TEXT_DOMAIN
);
3924 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
3925 (void) fprintf(stderr
, gettext("internal error: unable to "
3926 "open %s\n"), MNTTAB
);
3931 * Make sure the user has specified some command.
3934 (void) fprintf(stderr
, gettext("missing command\n"));
3941 * The 'umount' command is an alias for 'unmount'
3943 if (strcmp(cmdname
, "umount") == 0)
3944 cmdname
= "unmount";
3947 * The 'recv' command is an alias for 'receive'
3949 if (strcmp(cmdname
, "recv") == 0)
3950 cmdname
= "receive";
3955 if ((strcmp(cmdname
, "-?") == 0) ||
3956 (strcmp(cmdname
, "--help") == 0))
3959 if ((g_zfs
= libzfs_init()) == NULL
)
3962 zpool_set_history_str("zfs", argc
, argv
, history_str
);
3963 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
3965 libzfs_print_on_error(g_zfs
, B_TRUE
);
3968 * Run the appropriate command.
3970 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
3971 if (find_command_idx(cmdname
, &i
) == 0) {
3972 current_command
= &command_table
[i
];
3973 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
3974 } else if (strchr(cmdname
, '=') != NULL
) {
3975 verify(find_command_idx("set", &i
) == 0);
3976 current_command
= &command_table
[i
];
3977 ret
= command_table
[i
].func(argc
, argv
);
3979 (void) fprintf(stderr
, gettext("unrecognized "
3980 "command '%s'\n"), cmdname
);
3984 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
3987 (void) fclose(mnttab_file
);
3990 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3991 * for the purposes of running ::findleaks.
3993 if (getenv("ZFS_ABORT") != NULL
) {
3994 (void) printf("dumping core by request\n");