4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
32 #include <libnvpair.h>
44 #include <sys/mkdev.h>
45 #include <sys/mntent.h>
46 #include <sys/mnttab.h>
47 #include <sys/mount.h>
49 #include <sys/fs/zfs.h>
50 #include <sys/types.h>
58 #include "zfs_comutil.h"
60 libzfs_handle_t
*g_zfs
;
62 static FILE *mnttab_file
;
63 static char history_str
[HIS_MAX_RECORD_LEN
];
64 const char *pypath
= "/usr/lib/zfs/pyzfs.py";
66 static int zfs_do_clone(int argc
, char **argv
);
67 static int zfs_do_create(int argc
, char **argv
);
68 static int zfs_do_destroy(int argc
, char **argv
);
69 static int zfs_do_get(int argc
, char **argv
);
70 static int zfs_do_inherit(int argc
, char **argv
);
71 static int zfs_do_list(int argc
, char **argv
);
72 static int zfs_do_mount(int argc
, char **argv
);
73 static int zfs_do_rename(int argc
, char **argv
);
74 static int zfs_do_rollback(int argc
, char **argv
);
75 static int zfs_do_set(int argc
, char **argv
);
76 static int zfs_do_upgrade(int argc
, char **argv
);
77 static int zfs_do_snapshot(int argc
, char **argv
);
78 static int zfs_do_unmount(int argc
, char **argv
);
79 static int zfs_do_share(int argc
, char **argv
);
80 static int zfs_do_unshare(int argc
, char **argv
);
81 static int zfs_do_send(int argc
, char **argv
);
82 static int zfs_do_receive(int argc
, char **argv
);
83 static int zfs_do_promote(int argc
, char **argv
);
84 static int zfs_do_userspace(int argc
, char **argv
);
85 static int zfs_do_python(int argc
, char **argv
);
86 static int zfs_do_hold(int argc
, char **argv
);
87 static int zfs_do_release(int argc
, char **argv
);
88 static int zfs_do_diff(int argc
, char **argv
);
91 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
96 _umem_debug_init(void)
98 return ("default,verbose"); /* $UMEM_DEBUG setting */
102 _umem_logging_init(void)
104 return ("fail,contents"); /* $UMEM_LOGGING setting */
137 typedef struct zfs_command
{
139 int (*func
)(int argc
, char **argv
);
144 * Master command table. Each ZFS command has a name, associated function, and
145 * usage message. The usage messages need to be internationalized, so we have
146 * to have a function to return the usage message based on a command index.
148 * These commands are organized according to how they are displayed in the usage
149 * message. An empty command (one with a NULL name) indicates an empty line in
150 * the generic usage message.
152 static zfs_command_t command_table
[] = {
153 { "create", zfs_do_create
, HELP_CREATE
},
154 { "destroy", zfs_do_destroy
, HELP_DESTROY
},
156 { "snapshot", zfs_do_snapshot
, HELP_SNAPSHOT
},
157 { "rollback", zfs_do_rollback
, HELP_ROLLBACK
},
158 { "clone", zfs_do_clone
, HELP_CLONE
},
159 { "promote", zfs_do_promote
, HELP_PROMOTE
},
160 { "rename", zfs_do_rename
, HELP_RENAME
},
162 { "list", zfs_do_list
, HELP_LIST
},
164 { "set", zfs_do_set
, HELP_SET
},
165 { "get", zfs_do_get
, HELP_GET
},
166 { "inherit", zfs_do_inherit
, HELP_INHERIT
},
167 { "upgrade", zfs_do_upgrade
, HELP_UPGRADE
},
168 { "userspace", zfs_do_userspace
, HELP_USERSPACE
},
169 { "groupspace", zfs_do_userspace
, HELP_GROUPSPACE
},
171 { "mount", zfs_do_mount
, HELP_MOUNT
},
172 { "unmount", zfs_do_unmount
, HELP_UNMOUNT
},
173 { "share", zfs_do_share
, HELP_SHARE
},
174 { "unshare", zfs_do_unshare
, HELP_UNSHARE
},
176 { "send", zfs_do_send
, HELP_SEND
},
177 { "receive", zfs_do_receive
, HELP_RECEIVE
},
179 { "allow", zfs_do_python
, HELP_ALLOW
},
181 { "unallow", zfs_do_python
, HELP_UNALLOW
},
183 { "hold", zfs_do_hold
, HELP_HOLD
},
184 { "holds", zfs_do_python
, HELP_HOLDS
},
185 { "release", zfs_do_release
, HELP_RELEASE
},
186 { "diff", zfs_do_diff
, HELP_DIFF
},
189 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
191 zfs_command_t
*current_command
;
194 get_usage(zfs_help_t idx
)
198 return (gettext("\tclone [-p] [-o property=value] ... "
199 "<snapshot> <filesystem|volume>\n"));
201 return (gettext("\tcreate [-p] [-o property=value] ... "
203 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
204 "-V <size> <volume>\n"));
206 return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
207 "\tdestroy [-rRd] <snapshot>\n"));
209 return (gettext("\tget [-rHp] [-d max] "
210 "[-o \"all\" | field[,...]] [-s source[,...]]\n"
211 "\t <\"all\" | property[,...]> "
212 "[filesystem|volume|snapshot] ...\n"));
214 return (gettext("\tinherit [-rS] <property> "
215 "<filesystem|volume|snapshot> ...\n"));
217 return (gettext("\tupgrade [-v]\n"
218 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
220 return (gettext("\tlist [-rH][-d max] "
221 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
222 "\t [-S property] ... "
223 "[filesystem|volume|snapshot] ...\n"));
225 return (gettext("\tmount\n"
226 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
228 return (gettext("\tpromote <clone-filesystem>\n"));
230 return (gettext("\treceive [-vnFu] <filesystem|volume|"
232 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
234 return (gettext("\trename <filesystem|volume|snapshot> "
235 "<filesystem|volume|snapshot>\n"
236 "\trename -p <filesystem|volume> <filesystem|volume>\n"
237 "\trename -r <snapshot> <snapshot>"));
239 return (gettext("\trollback [-rRf] <snapshot>\n"));
241 return (gettext("\tsend [-RDp] [-[iI] snapshot] <snapshot>\n"));
243 return (gettext("\tset <property=value> "
244 "<filesystem|volume|snapshot> ...\n"));
246 return (gettext("\tshare <-a | filesystem>\n"));
248 return (gettext("\tsnapshot [-r] [-o property=value] ... "
249 "<filesystem@snapname|volume@snapname>\n"));
251 return (gettext("\tunmount [-f] "
252 "<-a | filesystem|mountpoint>\n"));
254 return (gettext("\tunshare "
255 "<-a | filesystem|mountpoint>\n"));
257 return (gettext("\tallow <filesystem|volume>\n"
259 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
260 "\t <filesystem|volume>\n"
261 "\tallow [-ld] -e <perm|@setname>[,...] "
262 "<filesystem|volume>\n"
263 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
264 "\tallow -s @setname <perm|@setname>[,...] "
265 "<filesystem|volume>\n"));
267 return (gettext("\tunallow [-rldug] "
268 "<\"everyone\"|user|group>[,...]\n"
269 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
270 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
271 "<filesystem|volume>\n"
272 "\tunallow [-r] -c [<perm|@setname>[,...]] "
273 "<filesystem|volume>\n"
274 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
275 "<filesystem|volume>\n"));
277 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
278 "[-sS field] ... [-t type[,...]]\n"
279 "\t <filesystem|snapshot>\n"));
280 case HELP_GROUPSPACE
:
281 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
282 "[-sS field] ... [-t type[,...]]\n"
283 "\t <filesystem|snapshot>\n"));
285 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
287 return (gettext("\tholds [-r] <snapshot> ...\n"));
289 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
291 return (gettext("\tdiff [-FHt] <snapshot> "
292 "[snapshot|filesystem]\n"));
302 (void) fprintf(stderr
, gettext("internal error: out of memory\n"));
307 * Utility function to guarantee malloc() success.
311 safe_malloc(size_t size
)
315 if ((data
= calloc(1, size
)) == NULL
)
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
);
550 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
552 * Given an existing dataset, create a writable copy whose initial contents
553 * are the same as the source. The newly created dataset maintains a
554 * dependency on the original; the original cannot be destroyed so long as
557 * The '-p' flag creates all the non-existing ancestors of the target first.
560 zfs_do_clone(int argc
, char **argv
)
562 zfs_handle_t
*zhp
= NULL
;
563 boolean_t parents
= B_FALSE
;
568 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
572 while ((c
= getopt(argc
, argv
, "o:p")) != -1) {
575 if (parseprop(props
))
582 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
591 /* check number of arguments */
593 (void) fprintf(stderr
, gettext("missing source dataset "
598 (void) fprintf(stderr
, gettext("missing target dataset "
603 (void) fprintf(stderr
, gettext("too many arguments\n"));
607 /* open the source dataset */
608 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
611 if (parents
&& zfs_name_valid(argv
[1], ZFS_TYPE_FILESYSTEM
|
614 * Now create the ancestors of the target dataset. If the
615 * target already exists and '-p' option was used we should not
618 if (zfs_dataset_exists(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
|
621 if (zfs_create_ancestors(g_zfs
, argv
[1]) != 0)
626 ret
= zfs_clone(zhp
, argv
[1], props
);
628 /* create the mountpoint if necessary */
632 clone
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_DATASET
);
634 if (zfs_get_type(clone
) != ZFS_TYPE_VOLUME
)
635 if ((ret
= zfs_mount(clone
, NULL
, 0)) == 0)
636 ret
= zfs_share(clone
);
655 * zfs create [-p] [-o prop=value] ... fs
656 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
658 * Create a new dataset. This command can be used to create filesystems
659 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
660 * For volumes, the user must specify a size to be used.
662 * The '-s' flag applies only to volumes, and indicates that we should not try
663 * to set the reservation for this volume. By default we set a reservation
664 * equal to the size for any volume. For pools with SPA_VERSION >=
665 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
667 * The '-p' flag creates all the non-existing ancestors of the target first.
670 zfs_do_create(int argc
, char **argv
)
672 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
673 zfs_handle_t
*zhp
= NULL
;
676 boolean_t noreserve
= B_FALSE
;
677 boolean_t bflag
= B_FALSE
;
678 boolean_t parents
= B_FALSE
;
682 int canmount
= ZFS_CANMOUNT_OFF
;
684 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
688 while ((c
= getopt(argc
, argv
, ":V:b:so:p")) != -1) {
691 type
= ZFS_TYPE_VOLUME
;
692 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
693 (void) fprintf(stderr
, gettext("bad volume "
694 "size '%s': %s\n"), optarg
,
695 libzfs_error_description(g_zfs
));
699 if (nvlist_add_uint64(props
,
700 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), intval
) != 0)
709 if (zfs_nicestrtonum(g_zfs
, optarg
, &intval
) != 0) {
710 (void) fprintf(stderr
, gettext("bad volume "
711 "block size '%s': %s\n"), optarg
,
712 libzfs_error_description(g_zfs
));
716 if (nvlist_add_uint64(props
,
717 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
722 if (parseprop(props
))
729 (void) fprintf(stderr
, gettext("missing size "
734 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
740 if ((bflag
|| noreserve
) && type
!= ZFS_TYPE_VOLUME
) {
741 (void) fprintf(stderr
, gettext("'-s' and '-b' can only be "
742 "used when creating a volume\n"));
749 /* check number of arguments */
751 (void) fprintf(stderr
, gettext("missing %s argument\n"),
752 zfs_type_to_name(type
));
756 (void) fprintf(stderr
, gettext("too many arguments\n"));
760 if (type
== ZFS_TYPE_VOLUME
&& !noreserve
) {
761 zpool_handle_t
*zpool_handle
;
762 uint64_t spa_version
;
764 zfs_prop_t resv_prop
;
767 if (p
= strchr(argv
[0], '/'))
769 zpool_handle
= zpool_open(g_zfs
, argv
[0]);
772 if (zpool_handle
== NULL
)
774 spa_version
= zpool_get_prop_int(zpool_handle
,
775 ZPOOL_PROP_VERSION
, NULL
);
776 zpool_close(zpool_handle
);
777 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
778 resv_prop
= ZFS_PROP_REFRESERVATION
;
780 resv_prop
= ZFS_PROP_RESERVATION
;
781 volsize
= zvol_volsize_to_reservation(volsize
, props
);
783 if (nvlist_lookup_string(props
, zfs_prop_to_name(resv_prop
),
785 if (nvlist_add_uint64(props
,
786 zfs_prop_to_name(resv_prop
), volsize
) != 0) {
793 if (parents
&& zfs_name_valid(argv
[0], type
)) {
795 * Now create the ancestors of target dataset. If the target
796 * already exists and '-p' option was used we should not
799 if (zfs_dataset_exists(g_zfs
, argv
[0], type
)) {
803 if (zfs_create_ancestors(g_zfs
, argv
[0]) != 0)
808 if (zfs_create(g_zfs
, argv
[0], type
, props
) != 0)
811 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
816 * if the user doesn't want the dataset automatically mounted,
817 * then skip the mount/share step
819 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT
, type
))
820 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
823 * Mount and/or share the new filesystem as appropriate. We provide a
824 * verbose error message to let the user know that their filesystem was
825 * in fact created, even if we failed to mount or share it.
827 if (canmount
== ZFS_CANMOUNT_ON
) {
828 if (zfs_mount(zhp
, NULL
, 0) != 0) {
829 (void) fprintf(stderr
, gettext("filesystem "
830 "successfully created, but not mounted\n"));
832 } else if (zfs_share(zhp
) != 0) {
833 (void) fprintf(stderr
, gettext("filesystem "
834 "successfully created, but not shared\n"));
851 * zfs destroy [-rRf] <fs, vol>
852 * zfs destroy [-rRd] <snap>
854 * -r Recursively destroy all children
855 * -R Recursively destroy all dependents, including clones
856 * -f Force unmounting of any dependents
857 * -d If we can't destroy now, mark for deferred destruction
859 * Destroys the given dataset. By default, it will unmount any filesystems,
860 * and refuse to destroy a dataset that has any dependents. A dependent can
861 * either be a child, or a clone of a child.
863 typedef struct destroy_cbdata
{
870 boolean_t cb_closezhp
;
871 zfs_handle_t
*cb_target
;
873 boolean_t cb_defer_destroy
;
877 * Check for any dependents based on the '-r' or '-R' flags.
880 destroy_check_dependent(zfs_handle_t
*zhp
, void *data
)
882 destroy_cbdata_t
*cbp
= data
;
883 const char *tname
= zfs_get_name(cbp
->cb_target
);
884 const char *name
= zfs_get_name(zhp
);
886 if (strncmp(tname
, name
, strlen(tname
)) == 0 &&
887 (name
[strlen(tname
)] == '/' || name
[strlen(tname
)] == '@')) {
889 * This is a direct descendant, not a clone somewhere else in
896 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
897 "%s has children\n"),
898 zfs_get_name(cbp
->cb_target
),
899 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
900 (void) fprintf(stderr
, gettext("use '-r' to destroy "
901 "the following datasets:\n"));
902 cbp
->cb_first
= B_FALSE
;
906 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
909 * This is a clone. We only want to report this if the '-r'
910 * wasn't specified, or the target is a snapshot.
912 if (!cbp
->cb_recurse
&&
913 zfs_get_type(cbp
->cb_target
) != ZFS_TYPE_SNAPSHOT
)
917 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
918 "%s has dependent clones\n"),
919 zfs_get_name(cbp
->cb_target
),
920 zfs_type_to_name(zfs_get_type(cbp
->cb_target
)));
921 (void) fprintf(stderr
, gettext("use '-R' to destroy "
922 "the following datasets:\n"));
923 cbp
->cb_first
= B_FALSE
;
927 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
936 destroy_callback(zfs_handle_t
*zhp
, void *data
)
938 destroy_cbdata_t
*cbp
= data
;
941 * Ignore pools (which we've already flagged as an error before getting
944 if (strchr(zfs_get_name(zhp
), '/') == NULL
&&
945 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
951 * Bail out on the first error.
953 if (zfs_unmount(zhp
, NULL
, cbp
->cb_force
? MS_FORCE
: 0) != 0 ||
954 zfs_destroy(zhp
, cbp
->cb_defer_destroy
) != 0) {
964 destroy_snap_clones(zfs_handle_t
*zhp
, void *arg
)
966 destroy_cbdata_t
*cbp
= arg
;
967 char thissnap
[MAXPATHLEN
];
969 boolean_t closezhp
= cbp
->cb_closezhp
;
972 (void) snprintf(thissnap
, sizeof (thissnap
),
973 "%s@%s", zfs_get_name(zhp
), cbp
->cb_snapname
);
975 libzfs_print_on_error(g_zfs
, B_FALSE
);
976 szhp
= zfs_open(g_zfs
, thissnap
, ZFS_TYPE_SNAPSHOT
);
977 libzfs_print_on_error(g_zfs
, B_TRUE
);
980 * Destroy any clones of this snapshot
982 if (zfs_iter_dependents(szhp
, B_FALSE
, destroy_callback
,
992 cbp
->cb_closezhp
= B_TRUE
;
993 rv
= zfs_iter_filesystems(zhp
, destroy_snap_clones
, arg
);
1000 zfs_do_destroy(int argc
, char **argv
)
1002 destroy_cbdata_t cb
= { 0 };
1006 zfs_type_t type
= ZFS_TYPE_DATASET
;
1009 while ((c
= getopt(argc
, argv
, "dfrR")) != -1) {
1012 cb
.cb_defer_destroy
= B_TRUE
;
1013 type
= ZFS_TYPE_SNAPSHOT
;
1027 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1036 /* check number of arguments */
1038 (void) fprintf(stderr
, gettext("missing path argument\n"));
1042 (void) fprintf(stderr
, gettext("too many arguments\n"));
1047 * If we are doing recursive destroy of a snapshot, then the
1048 * named snapshot may not exist. Go straight to libzfs.
1050 if (cb
.cb_recurse
&& (cp
= strchr(argv
[0], '@'))) {
1054 if ((zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_DATASET
)) == NULL
)
1059 if (cb
.cb_doclones
) {
1060 boolean_t defer
= cb
.cb_defer_destroy
;
1063 * Temporarily ignore the defer_destroy setting since
1064 * it's not supported for clones.
1066 cb
.cb_defer_destroy
= B_FALSE
;
1067 cb
.cb_snapname
= cp
;
1068 if (destroy_snap_clones(zhp
, &cb
) != 0) {
1072 cb
.cb_defer_destroy
= defer
;
1075 ret
= zfs_destroy_snaps(zhp
, cp
, cb
.cb_defer_destroy
);
1078 (void) fprintf(stderr
,
1079 gettext("no snapshots destroyed\n"));
1084 /* Open the given dataset */
1085 if ((zhp
= zfs_open(g_zfs
, argv
[0], type
)) == NULL
)
1091 * Perform an explicit check for pools before going any further.
1093 if (!cb
.cb_recurse
&& strchr(zfs_get_name(zhp
), '/') == NULL
&&
1094 zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) {
1095 (void) fprintf(stderr
, gettext("cannot destroy '%s': "
1096 "operation does not apply to pools\n"),
1098 (void) fprintf(stderr
, gettext("use 'zfs destroy -r "
1099 "%s' to destroy all datasets in the pool\n"),
1101 (void) fprintf(stderr
, gettext("use 'zpool destroy %s' "
1102 "to destroy the pool itself\n"), zfs_get_name(zhp
));
1108 * Check for any dependents and/or clones.
1110 cb
.cb_first
= B_TRUE
;
1111 if (!cb
.cb_doclones
&& !cb
.cb_defer_destroy
&&
1112 zfs_iter_dependents(zhp
, B_TRUE
, destroy_check_dependent
,
1118 if (cb
.cb_error
|| (!cb
.cb_defer_destroy
&&
1119 (zfs_iter_dependents(zhp
, B_FALSE
, destroy_callback
, &cb
) != 0))) {
1125 * Do the real thing. The callback will close the handle regardless of
1126 * whether it succeeds or not.
1129 if (destroy_callback(zhp
, &cb
) != 0)
1136 is_recvd_column(zprop_get_cbdata_t
*cbp
)
1139 zfs_get_column_t col
;
1141 for (i
= 0; i
< ZFS_GET_NCOLS
&&
1142 (col
= cbp
->cb_columns
[i
]) != GET_COL_NONE
; i
++)
1143 if (col
== GET_COL_RECVD
)
1149 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1150 * < all | property[,property]... > < fs | snap | vol > ...
1152 * -r recurse over any child datasets
1153 * -H scripted mode. Headers are stripped, and fields are separated
1154 * by tabs instead of spaces.
1155 * -o Set of fields to display. One of "name,property,value,
1156 * received,source". Default is "name,property,value,source".
1157 * "all" is an alias for all five.
1158 * -s Set of sources to allow. One of
1159 * "local,default,inherited,received,temporary,none". Default is
1161 * -p Display values in parsable (literal) format.
1163 * Prints properties for the given datasets. The user can control which
1164 * columns to display as well as which property types to allow.
1168 * Invoked to display the properties for a single dataset.
1171 get_callback(zfs_handle_t
*zhp
, void *data
)
1173 char buf
[ZFS_MAXPROPLEN
];
1174 char rbuf
[ZFS_MAXPROPLEN
];
1175 zprop_source_t sourcetype
;
1176 char source
[ZFS_MAXNAMELEN
];
1177 zprop_get_cbdata_t
*cbp
= data
;
1178 nvlist_t
*user_props
= zfs_get_user_props(zhp
);
1179 zprop_list_t
*pl
= cbp
->cb_proplist
;
1183 boolean_t received
= is_recvd_column(cbp
);
1185 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1186 char *recvdval
= NULL
;
1188 * Skip the special fake placeholder. This will also skip over
1189 * the name property when 'all' is specified.
1191 if (pl
->pl_prop
== ZFS_PROP_NAME
&&
1192 pl
== cbp
->cb_proplist
)
1195 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1196 if (zfs_prop_get(zhp
, pl
->pl_prop
, buf
,
1197 sizeof (buf
), &sourcetype
, source
,
1199 cbp
->cb_literal
) != 0) {
1202 if (!zfs_prop_valid_for_type(pl
->pl_prop
,
1203 ZFS_TYPE_DATASET
)) {
1204 (void) fprintf(stderr
,
1205 gettext("No such property '%s'\n"),
1206 zfs_prop_to_name(pl
->pl_prop
));
1209 sourcetype
= ZPROP_SRC_NONE
;
1210 (void) strlcpy(buf
, "-", sizeof (buf
));
1213 if (received
&& (zfs_prop_get_recvd(zhp
,
1214 zfs_prop_to_name(pl
->pl_prop
), rbuf
, sizeof (rbuf
),
1215 cbp
->cb_literal
) == 0))
1218 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1219 zfs_prop_to_name(pl
->pl_prop
),
1220 buf
, sourcetype
, source
, recvdval
);
1221 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
1222 sourcetype
= ZPROP_SRC_LOCAL
;
1224 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
1225 buf
, sizeof (buf
), cbp
->cb_literal
) != 0) {
1226 sourcetype
= ZPROP_SRC_NONE
;
1227 (void) strlcpy(buf
, "-", sizeof (buf
));
1230 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1231 pl
->pl_user_prop
, buf
, sourcetype
, source
, NULL
);
1233 if (nvlist_lookup_nvlist(user_props
,
1234 pl
->pl_user_prop
, &propval
) != 0) {
1237 sourcetype
= ZPROP_SRC_NONE
;
1240 verify(nvlist_lookup_string(propval
,
1241 ZPROP_VALUE
, &strval
) == 0);
1242 verify(nvlist_lookup_string(propval
,
1243 ZPROP_SOURCE
, &sourceval
) == 0);
1245 if (strcmp(sourceval
,
1246 zfs_get_name(zhp
)) == 0) {
1247 sourcetype
= ZPROP_SRC_LOCAL
;
1248 } else if (strcmp(sourceval
,
1249 ZPROP_SOURCE_VAL_RECVD
) == 0) {
1250 sourcetype
= ZPROP_SRC_RECEIVED
;
1252 sourcetype
= ZPROP_SRC_INHERITED
;
1253 (void) strlcpy(source
,
1254 sourceval
, sizeof (source
));
1258 if (received
&& (zfs_prop_get_recvd(zhp
,
1259 pl
->pl_user_prop
, rbuf
, sizeof (rbuf
),
1260 cbp
->cb_literal
) == 0))
1263 zprop_print_one_property(zfs_get_name(zhp
), cbp
,
1264 pl
->pl_user_prop
, strval
, sourcetype
,
1273 zfs_do_get(int argc
, char **argv
)
1275 zprop_get_cbdata_t cb
= { 0 };
1276 int i
, c
, flags
= 0;
1277 char *value
, *fields
;
1280 zprop_list_t fake_name
= { 0 };
1283 * Set up default columns and sources.
1285 cb
.cb_sources
= ZPROP_SRC_ALL
;
1286 cb
.cb_columns
[0] = GET_COL_NAME
;
1287 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1288 cb
.cb_columns
[2] = GET_COL_VALUE
;
1289 cb
.cb_columns
[3] = GET_COL_SOURCE
;
1290 cb
.cb_type
= ZFS_TYPE_DATASET
;
1293 while ((c
= getopt(argc
, argv
, ":d:o:s:rHp")) != -1) {
1296 cb
.cb_literal
= B_TRUE
;
1299 limit
= parse_depth(optarg
, &flags
);
1302 flags
|= ZFS_ITER_RECURSE
;
1305 cb
.cb_scripted
= B_TRUE
;
1308 (void) fprintf(stderr
, gettext("missing argument for "
1309 "'%c' option\n"), optopt
);
1314 * Process the set of columns to display. We zero out
1315 * the structure to give us a blank slate.
1317 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
1319 while (*optarg
!= '\0') {
1320 static char *col_subopts
[] =
1321 { "name", "property", "value", "received",
1322 "source", "all", NULL
};
1324 if (i
== ZFS_GET_NCOLS
) {
1325 (void) fprintf(stderr
, gettext("too "
1326 "many fields given to -o "
1331 switch (getsubopt(&optarg
, col_subopts
,
1334 cb
.cb_columns
[i
++] = GET_COL_NAME
;
1337 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
1340 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
1343 cb
.cb_columns
[i
++] = GET_COL_RECVD
;
1344 flags
|= ZFS_ITER_RECVD_PROPS
;
1347 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
1351 (void) fprintf(stderr
,
1352 gettext("\"all\" conflicts "
1353 "with specific fields "
1354 "given to -o option\n"));
1357 cb
.cb_columns
[0] = GET_COL_NAME
;
1358 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
1359 cb
.cb_columns
[2] = GET_COL_VALUE
;
1360 cb
.cb_columns
[3] = GET_COL_RECVD
;
1361 cb
.cb_columns
[4] = GET_COL_SOURCE
;
1362 flags
|= ZFS_ITER_RECVD_PROPS
;
1366 (void) fprintf(stderr
,
1367 gettext("invalid column name "
1376 while (*optarg
!= '\0') {
1377 static char *source_subopts
[] = {
1378 "local", "default", "inherited",
1379 "received", "temporary", "none",
1382 switch (getsubopt(&optarg
, source_subopts
,
1385 cb
.cb_sources
|= ZPROP_SRC_LOCAL
;
1388 cb
.cb_sources
|= ZPROP_SRC_DEFAULT
;
1391 cb
.cb_sources
|= ZPROP_SRC_INHERITED
;
1394 cb
.cb_sources
|= ZPROP_SRC_RECEIVED
;
1397 cb
.cb_sources
|= ZPROP_SRC_TEMPORARY
;
1400 cb
.cb_sources
|= ZPROP_SRC_NONE
;
1403 (void) fprintf(stderr
,
1404 gettext("invalid source "
1412 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1422 (void) fprintf(stderr
, gettext("missing property "
1429 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
1437 * As part of zfs_expand_proplist(), we keep track of the maximum column
1438 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1439 * need to know the maximum name length. However, the user likely did
1440 * not specify 'name' as one of the properties to fetch, so we need to
1441 * make sure we always include at least this property for
1442 * print_get_headers() to work properly.
1444 if (cb
.cb_proplist
!= NULL
) {
1445 fake_name
.pl_prop
= ZFS_PROP_NAME
;
1446 fake_name
.pl_width
= strlen(gettext("NAME"));
1447 fake_name
.pl_next
= cb
.cb_proplist
;
1448 cb
.cb_proplist
= &fake_name
;
1451 cb
.cb_first
= B_TRUE
;
1453 /* run for each object */
1454 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
, NULL
,
1455 &cb
.cb_proplist
, limit
, get_callback
, &cb
);
1457 if (cb
.cb_proplist
== &fake_name
)
1458 zprop_free_list(fake_name
.pl_next
);
1460 zprop_free_list(cb
.cb_proplist
);
1466 * inherit [-rS] <property> <fs|vol> ...
1468 * -r Recurse over all children
1469 * -S Revert to received value, if any
1471 * For each dataset specified on the command line, inherit the given property
1472 * from its parent. Inheriting a property at the pool level will cause it to
1473 * use the default value. The '-r' flag will recurse over all children, and is
1474 * useful for setting a property on a hierarchy-wide basis, regardless of any
1475 * local modifications for each dataset.
1478 typedef struct inherit_cbdata
{
1479 const char *cb_propname
;
1480 boolean_t cb_received
;
1484 inherit_recurse_cb(zfs_handle_t
*zhp
, void *data
)
1486 inherit_cbdata_t
*cb
= data
;
1487 zfs_prop_t prop
= zfs_name_to_prop(cb
->cb_propname
);
1490 * If we're doing it recursively, then ignore properties that
1491 * are not valid for this type of dataset.
1493 if (prop
!= ZPROP_INVAL
&&
1494 !zfs_prop_valid_for_type(prop
, zfs_get_type(zhp
)))
1497 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1501 inherit_cb(zfs_handle_t
*zhp
, void *data
)
1503 inherit_cbdata_t
*cb
= data
;
1505 return (zfs_prop_inherit(zhp
, cb
->cb_propname
, cb
->cb_received
) != 0);
1509 zfs_do_inherit(int argc
, char **argv
)
1513 inherit_cbdata_t cb
= { 0 };
1517 boolean_t received
= B_FALSE
;
1520 while ((c
= getopt(argc
, argv
, "rS")) != -1) {
1523 flags
|= ZFS_ITER_RECURSE
;
1530 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1539 /* check number of arguments */
1541 (void) fprintf(stderr
, gettext("missing property argument\n"));
1545 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
1553 if ((prop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
1554 if (zfs_prop_readonly(prop
)) {
1555 (void) fprintf(stderr
, gettext(
1556 "%s property is read-only\n"),
1560 if (!zfs_prop_inheritable(prop
) && !received
) {
1561 (void) fprintf(stderr
, gettext("'%s' property cannot "
1562 "be inherited\n"), propname
);
1563 if (prop
== ZFS_PROP_QUOTA
||
1564 prop
== ZFS_PROP_RESERVATION
||
1565 prop
== ZFS_PROP_REFQUOTA
||
1566 prop
== ZFS_PROP_REFRESERVATION
)
1567 (void) fprintf(stderr
, gettext("use 'zfs set "
1568 "%s=none' to clear\n"), propname
);
1571 if (received
&& (prop
== ZFS_PROP_VOLSIZE
||
1572 prop
== ZFS_PROP_VERSION
)) {
1573 (void) fprintf(stderr
, gettext("'%s' property cannot "
1574 "be reverted to a received value\n"), propname
);
1577 } else if (!zfs_prop_user(propname
)) {
1578 (void) fprintf(stderr
, gettext("invalid property '%s'\n"),
1583 cb
.cb_propname
= propname
;
1584 cb
.cb_received
= received
;
1586 if (flags
& ZFS_ITER_RECURSE
) {
1587 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1588 NULL
, NULL
, 0, inherit_recurse_cb
, &cb
);
1590 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_DATASET
,
1591 NULL
, NULL
, 0, inherit_cb
, &cb
);
1597 typedef struct upgrade_cbdata
{
1598 uint64_t cb_numupgraded
;
1599 uint64_t cb_numsamegraded
;
1600 uint64_t cb_numfailed
;
1601 uint64_t cb_version
;
1603 boolean_t cb_foundone
;
1604 char cb_lastfs
[ZFS_MAXNAMELEN
];
1608 same_pool(zfs_handle_t
*zhp
, const char *name
)
1610 int len1
= strcspn(name
, "/@");
1611 const char *zhname
= zfs_get_name(zhp
);
1612 int len2
= strcspn(zhname
, "/@");
1616 return (strncmp(name
, zhname
, len1
) == 0);
1620 upgrade_list_callback(zfs_handle_t
*zhp
, void *data
)
1622 upgrade_cbdata_t
*cb
= data
;
1623 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1625 /* list if it's old/new */
1626 if ((!cb
->cb_newer
&& version
< ZPL_VERSION
) ||
1627 (cb
->cb_newer
&& version
> ZPL_VERSION
)) {
1630 str
= gettext("The following filesystems are "
1631 "formatted using a newer software version and\n"
1632 "cannot be accessed on the current system.\n\n");
1634 str
= gettext("The following filesystems are "
1635 "out of date, and can be upgraded. After being\n"
1636 "upgraded, these filesystems (and any 'zfs send' "
1637 "streams generated from\n"
1638 "subsequent snapshots) will no longer be "
1639 "accessible by older software versions.\n\n");
1642 if (!cb
->cb_foundone
) {
1644 (void) printf(gettext("VER FILESYSTEM\n"));
1645 (void) printf(gettext("--- ------------\n"));
1646 cb
->cb_foundone
= B_TRUE
;
1649 (void) printf("%2u %s\n", version
, zfs_get_name(zhp
));
1656 upgrade_set_callback(zfs_handle_t
*zhp
, void *data
)
1658 upgrade_cbdata_t
*cb
= data
;
1659 int version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1660 int needed_spa_version
;
1663 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1666 needed_spa_version
= zfs_spa_version_map(cb
->cb_version
);
1668 if (needed_spa_version
< 0)
1671 if (spa_version
< needed_spa_version
) {
1673 (void) printf(gettext("%s: can not be "
1674 "upgraded; the pool version needs to first "
1675 "be upgraded\nto version %d\n\n"),
1676 zfs_get_name(zhp
), needed_spa_version
);
1682 if (version
< cb
->cb_version
) {
1684 (void) snprintf(verstr
, sizeof (verstr
),
1685 "%llu", cb
->cb_version
);
1686 if (cb
->cb_lastfs
[0] && !same_pool(zhp
, cb
->cb_lastfs
)) {
1688 * If they did "zfs upgrade -a", then we could
1689 * be doing ioctls to different pools. We need
1690 * to log this history once to each pool.
1692 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
1694 if (zfs_prop_set(zhp
, "version", verstr
) == 0)
1695 cb
->cb_numupgraded
++;
1698 (void) strcpy(cb
->cb_lastfs
, zfs_get_name(zhp
));
1699 } else if (version
> cb
->cb_version
) {
1700 /* can't downgrade */
1701 (void) printf(gettext("%s: can not be downgraded; "
1702 "it is already at version %u\n"),
1703 zfs_get_name(zhp
), version
);
1706 cb
->cb_numsamegraded
++;
1714 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1717 zfs_do_upgrade(int argc
, char **argv
)
1719 boolean_t all
= B_FALSE
;
1720 boolean_t showversions
= B_FALSE
;
1722 upgrade_cbdata_t cb
= { 0 };
1724 int flags
= ZFS_ITER_ARGS_CAN_BE_PATHS
;
1727 while ((c
= getopt(argc
, argv
, "rvV:a")) != -1) {
1730 flags
|= ZFS_ITER_RECURSE
;
1733 showversions
= B_TRUE
;
1736 if (zfs_prop_string_to_index(ZFS_PROP_VERSION
,
1737 optarg
, &cb
.cb_version
) != 0) {
1738 (void) fprintf(stderr
,
1739 gettext("invalid version %s\n"), optarg
);
1748 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1757 if ((!all
&& !argc
) && ((flags
& ZFS_ITER_RECURSE
) | cb
.cb_version
))
1759 if (showversions
&& (flags
& ZFS_ITER_RECURSE
|| all
||
1760 cb
.cb_version
|| argc
))
1762 if ((all
|| argc
) && (showversions
))
1768 /* Show info on available versions. */
1769 (void) printf(gettext("The following filesystem versions are "
1771 (void) printf(gettext("VER DESCRIPTION\n"));
1772 (void) printf("--- -----------------------------------------"
1773 "---------------\n");
1774 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1775 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1776 (void) printf(gettext(" 3 Case insensitive and File system "
1777 "unique identifier (FUID)\n"));
1778 (void) printf(gettext(" 4 userquota, groupquota "
1780 (void) printf(gettext(" 5 System attributes\n"));
1781 (void) printf(gettext("\nFor more information on a particular "
1782 "version, including supported releases,\n"));
1783 (void) printf("see the ZFS Administration Guide.\n\n");
1785 } else if (argc
|| all
) {
1786 /* Upgrade filesystems */
1787 if (cb
.cb_version
== 0)
1788 cb
.cb_version
= ZPL_VERSION
;
1789 ret
= zfs_for_each(argc
, argv
, flags
, ZFS_TYPE_FILESYSTEM
,
1790 NULL
, NULL
, 0, upgrade_set_callback
, &cb
);
1791 (void) printf(gettext("%llu filesystems upgraded\n"),
1793 if (cb
.cb_numsamegraded
) {
1794 (void) printf(gettext("%llu filesystems already at "
1796 cb
.cb_numsamegraded
);
1798 if (cb
.cb_numfailed
!= 0)
1801 /* List old-version filesytems */
1803 (void) printf(gettext("This system is currently running "
1804 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION
);
1806 flags
|= ZFS_ITER_RECURSE
;
1807 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1808 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
1810 found
= cb
.cb_foundone
;
1811 cb
.cb_foundone
= B_FALSE
;
1812 cb
.cb_newer
= B_TRUE
;
1814 ret
= zfs_for_each(0, NULL
, flags
, ZFS_TYPE_FILESYSTEM
,
1815 NULL
, NULL
, 0, upgrade_list_callback
, &cb
);
1817 if (!cb
.cb_foundone
&& !found
) {
1818 (void) printf(gettext("All filesystems are "
1819 "formatted with the current version.\n"));
1830 userspace_cb(void *arg
, const char *domain
, uid_t rid
, uint64_t space
)
1832 zfs_userquota_prop_t
*typep
= arg
;
1833 zfs_userquota_prop_t p
= *typep
;
1835 char *ug
, *propname
;
1839 if (domain
== NULL
|| domain
[0] == '\0') {
1840 if (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
) {
1841 struct group
*g
= getgrgid(rid
);
1845 struct passwd
*p
= getpwuid(rid
);
1851 if (p
== ZFS_PROP_GROUPUSED
|| p
== ZFS_PROP_GROUPQUOTA
)
1856 if (p
== ZFS_PROP_USERUSED
|| p
== ZFS_PROP_GROUPUSED
)
1862 (void) snprintf(namebuf
, sizeof (namebuf
),
1863 "%llu", (longlong_t
)rid
);
1866 zfs_nicenum(space
, sizebuf
, sizeof (sizebuf
));
1868 (void) printf("%s %s %s%c%s %s\n", propname
, ug
, domain
,
1869 domain
[0] ? '-' : ' ', name
, sizebuf
);
1875 zfs_do_userspace(int argc
, char **argv
)
1878 zfs_userquota_prop_t p
;
1882 * Try the python version. If the execv fails, we'll continue
1883 * and do a simplistic implementation.
1885 (void) execv(pypath
, argv
-1);
1887 (void) printf("internal error: %s not found\n"
1888 "falling back on built-in implementation, "
1889 "some features will not work\n", pypath
);
1891 if ((zhp
= zfs_open(g_zfs
, argv
[argc
-1], ZFS_TYPE_DATASET
)) == NULL
)
1894 (void) printf("PROP TYPE NAME VALUE\n");
1896 for (p
= 0; p
< ZFS_NUM_USERQUOTA_PROPS
; p
++) {
1897 error
= zfs_userspace(zhp
, p
, userspace_cb
, &p
);
1905 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
1906 * [-s property [-s property]...] [-S property [-S property]...]
1909 * -r Recurse over all children
1910 * -d Limit recursion by depth.
1911 * -H Scripted mode; elide headers and separate columns by tabs
1912 * -o Control which fields to display.
1913 * -t Control which object types to display.
1914 * -s Specify sort columns, descending order.
1915 * -S Specify sort columns, ascending order.
1917 * When given no arguments, lists all filesystems in the system.
1918 * Otherwise, list the specified datasets, optionally recursing down them if
1919 * '-r' is specified.
1921 typedef struct list_cbdata
{
1923 boolean_t cb_scripted
;
1924 zprop_list_t
*cb_proplist
;
1928 * Given a list of columns to display, output appropriate headers for each one.
1931 print_header(zprop_list_t
*pl
)
1933 char headerbuf
[ZFS_MAXPROPLEN
];
1936 boolean_t first
= B_TRUE
;
1937 boolean_t right_justify
;
1939 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1946 right_justify
= B_FALSE
;
1947 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1948 header
= zfs_prop_column_name(pl
->pl_prop
);
1949 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
1951 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
1952 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
1953 headerbuf
[i
] = '\0';
1957 if (pl
->pl_next
== NULL
&& !right_justify
)
1958 (void) printf("%s", header
);
1959 else if (right_justify
)
1960 (void) printf("%*s", pl
->pl_width
, header
);
1962 (void) printf("%-*s", pl
->pl_width
, header
);
1965 (void) printf("\n");
1969 * Given a dataset and a list of fields, print out all the properties according
1970 * to the described layout.
1973 print_dataset(zfs_handle_t
*zhp
, zprop_list_t
*pl
, boolean_t scripted
)
1975 boolean_t first
= B_TRUE
;
1976 char property
[ZFS_MAXPROPLEN
];
1977 nvlist_t
*userprops
= zfs_get_user_props(zhp
);
1980 boolean_t right_justify
;
1983 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
1986 (void) printf("\t");
1993 if (pl
->pl_prop
!= ZPROP_INVAL
) {
1994 if (zfs_prop_get(zhp
, pl
->pl_prop
, property
,
1995 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) != 0)
2000 right_justify
= zfs_prop_align_right(pl
->pl_prop
);
2001 } else if (zfs_prop_userquota(pl
->pl_user_prop
)) {
2002 if (zfs_prop_get_userquota(zhp
, pl
->pl_user_prop
,
2003 property
, sizeof (property
), B_FALSE
) != 0)
2007 right_justify
= B_TRUE
;
2009 if (nvlist_lookup_nvlist(userprops
,
2010 pl
->pl_user_prop
, &propval
) != 0)
2013 verify(nvlist_lookup_string(propval
,
2014 ZPROP_VALUE
, &propstr
) == 0);
2015 right_justify
= B_FALSE
;
2018 width
= pl
->pl_width
;
2021 * If this is being called in scripted mode, or if this is the
2022 * last column and it is left-justified, don't include a width
2025 if (scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2026 (void) printf("%s", propstr
);
2027 else if (right_justify
)
2028 (void) printf("%*s", width
, propstr
);
2030 (void) printf("%-*s", width
, propstr
);
2033 (void) printf("\n");
2037 * Generic callback function to list a dataset or snapshot.
2040 list_callback(zfs_handle_t
*zhp
, void *data
)
2042 list_cbdata_t
*cbp
= data
;
2044 if (cbp
->cb_first
) {
2045 if (!cbp
->cb_scripted
)
2046 print_header(cbp
->cb_proplist
);
2047 cbp
->cb_first
= B_FALSE
;
2050 print_dataset(zhp
, cbp
->cb_proplist
, cbp
->cb_scripted
);
2056 zfs_do_list(int argc
, char **argv
)
2059 boolean_t scripted
= B_FALSE
;
2060 static char default_fields
[] =
2061 "name,used,available,referenced,mountpoint";
2062 int types
= ZFS_TYPE_DATASET
;
2063 boolean_t types_specified
= B_FALSE
;
2064 char *fields
= NULL
;
2065 list_cbdata_t cb
= { 0 };
2069 zfs_sort_column_t
*sortcol
= NULL
;
2070 int flags
= ZFS_ITER_PROP_LISTSNAPS
| ZFS_ITER_ARGS_CAN_BE_PATHS
;
2073 while ((c
= getopt(argc
, argv
, ":d:o:rt:Hs:S:")) != -1) {
2079 limit
= parse_depth(optarg
, &flags
);
2082 flags
|= ZFS_ITER_RECURSE
;
2088 if (zfs_add_sort_column(&sortcol
, optarg
,
2090 (void) fprintf(stderr
,
2091 gettext("invalid property '%s'\n"), optarg
);
2096 if (zfs_add_sort_column(&sortcol
, optarg
,
2098 (void) fprintf(stderr
,
2099 gettext("invalid property '%s'\n"), optarg
);
2105 types_specified
= B_TRUE
;
2106 flags
&= ~ZFS_ITER_PROP_LISTSNAPS
;
2107 while (*optarg
!= '\0') {
2108 static char *type_subopts
[] = { "filesystem",
2109 "volume", "snapshot", "all", NULL
};
2111 switch (getsubopt(&optarg
, type_subopts
,
2114 types
|= ZFS_TYPE_FILESYSTEM
;
2117 types
|= ZFS_TYPE_VOLUME
;
2120 types
|= ZFS_TYPE_SNAPSHOT
;
2123 types
= ZFS_TYPE_DATASET
;
2127 (void) fprintf(stderr
,
2128 gettext("invalid type '%s'\n"),
2135 (void) fprintf(stderr
, gettext("missing argument for "
2136 "'%c' option\n"), optopt
);
2140 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2150 fields
= default_fields
;
2153 * If "-o space" and no types were specified, don't display snapshots.
2155 if (strcmp(fields
, "space") == 0 && types_specified
== B_FALSE
)
2156 types
&= ~ZFS_TYPE_SNAPSHOT
;
2159 * If the user specifies '-o all', the zprop_get_list() doesn't
2160 * normally include the name of the dataset. For 'zfs list', we always
2161 * want this property to be first.
2163 if (zprop_get_list(g_zfs
, fields
, &cb
.cb_proplist
, ZFS_TYPE_DATASET
)
2167 cb
.cb_scripted
= scripted
;
2168 cb
.cb_first
= B_TRUE
;
2170 ret
= zfs_for_each(argc
, argv
, flags
, types
, sortcol
, &cb
.cb_proplist
,
2171 limit
, list_callback
, &cb
);
2173 zprop_free_list(cb
.cb_proplist
);
2174 zfs_free_sort_columns(sortcol
);
2176 if (ret
== 0 && cb
.cb_first
&& !cb
.cb_scripted
)
2177 (void) printf(gettext("no datasets available\n"));
2183 * zfs rename <fs | snap | vol> <fs | snap | vol>
2184 * zfs rename -p <fs | vol> <fs | vol>
2185 * zfs rename -r <snap> <snap>
2187 * Renames the given dataset to another of the same type.
2189 * The '-p' flag creates all the non-existing ancestors of the target first.
2193 zfs_do_rename(int argc
, char **argv
)
2198 boolean_t recurse
= B_FALSE
;
2199 boolean_t parents
= B_FALSE
;
2202 while ((c
= getopt(argc
, argv
, "pr")) != -1) {
2212 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2221 /* check number of arguments */
2223 (void) fprintf(stderr
, gettext("missing source dataset "
2228 (void) fprintf(stderr
, gettext("missing target dataset "
2233 (void) fprintf(stderr
, gettext("too many arguments\n"));
2237 if (recurse
&& parents
) {
2238 (void) fprintf(stderr
, gettext("-p and -r options are mutually "
2243 if (recurse
&& strchr(argv
[0], '@') == 0) {
2244 (void) fprintf(stderr
, gettext("source dataset for recursive "
2245 "rename must be a snapshot\n"));
2249 if ((zhp
= zfs_open(g_zfs
, argv
[0], parents
? ZFS_TYPE_FILESYSTEM
|
2250 ZFS_TYPE_VOLUME
: ZFS_TYPE_DATASET
)) == NULL
)
2253 /* If we were asked and the name looks good, try to create ancestors. */
2254 if (parents
&& zfs_name_valid(argv
[1], zfs_get_type(zhp
)) &&
2255 zfs_create_ancestors(g_zfs
, argv
[1]) != 0) {
2260 ret
= (zfs_rename(zhp
, argv
[1], recurse
) != 0);
2269 * Promotes the given clone fs to be the parent
2273 zfs_do_promote(int argc
, char **argv
)
2279 if (argc
> 1 && argv
[1][0] == '-') {
2280 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2285 /* check number of arguments */
2287 (void) fprintf(stderr
, gettext("missing clone filesystem"
2292 (void) fprintf(stderr
, gettext("too many arguments\n"));
2296 zhp
= zfs_open(g_zfs
, argv
[1], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2300 ret
= (zfs_promote(zhp
) != 0);
2308 * zfs rollback [-rRf] <snapshot>
2310 * -r Delete any intervening snapshots before doing rollback
2311 * -R Delete any snapshots and their clones
2312 * -f ignored for backwards compatability
2314 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2315 * since then and making it the active dataset. If more recent snapshots exist,
2316 * the command will complain unless the '-r' flag is given.
2318 typedef struct rollback_cbdata
{
2324 boolean_t cb_recurse
;
2325 boolean_t cb_dependent
;
2326 } rollback_cbdata_t
;
2329 * Report any snapshots more recent than the one specified. Used when '-r' is
2330 * not specified. We reuse this same callback for the snapshot dependents - if
2331 * 'cb_dependent' is set, then this is a dependent and we should report it
2332 * without checking the transaction group.
2335 rollback_check(zfs_handle_t
*zhp
, void *data
)
2337 rollback_cbdata_t
*cbp
= data
;
2339 if (cbp
->cb_doclones
) {
2344 if (!cbp
->cb_dependent
) {
2345 if (strcmp(zfs_get_name(zhp
), cbp
->cb_target
) != 0 &&
2346 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2347 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
2350 if (cbp
->cb_first
&& !cbp
->cb_recurse
) {
2351 (void) fprintf(stderr
, gettext("cannot "
2352 "rollback to '%s': more recent snapshots "
2355 (void) fprintf(stderr
, gettext("use '-r' to "
2356 "force deletion of the following "
2362 if (cbp
->cb_recurse
) {
2363 cbp
->cb_dependent
= B_TRUE
;
2364 if (zfs_iter_dependents(zhp
, B_TRUE
,
2365 rollback_check
, cbp
) != 0) {
2369 cbp
->cb_dependent
= B_FALSE
;
2371 (void) fprintf(stderr
, "%s\n",
2376 if (cbp
->cb_first
&& cbp
->cb_recurse
) {
2377 (void) fprintf(stderr
, gettext("cannot rollback to "
2378 "'%s': clones of previous snapshots exist\n"),
2380 (void) fprintf(stderr
, gettext("use '-R' to "
2381 "force deletion of the following clones and "
2387 (void) fprintf(stderr
, "%s\n", zfs_get_name(zhp
));
2395 zfs_do_rollback(int argc
, char **argv
)
2399 boolean_t force
= B_FALSE
;
2400 rollback_cbdata_t cb
= { 0 };
2401 zfs_handle_t
*zhp
, *snap
;
2402 char parentname
[ZFS_MAXNAMELEN
];
2406 while ((c
= getopt(argc
, argv
, "rRf")) != -1) {
2419 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2428 /* check number of arguments */
2430 (void) fprintf(stderr
, gettext("missing dataset argument\n"));
2434 (void) fprintf(stderr
, gettext("too many arguments\n"));
2438 /* open the snapshot */
2439 if ((snap
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_SNAPSHOT
)) == NULL
)
2442 /* open the parent dataset */
2443 (void) strlcpy(parentname
, argv
[0], sizeof (parentname
));
2444 verify((delim
= strrchr(parentname
, '@')) != NULL
);
2446 if ((zhp
= zfs_open(g_zfs
, parentname
, ZFS_TYPE_DATASET
)) == NULL
) {
2452 * Check for more recent snapshots and/or clones based on the presence
2455 cb
.cb_target
= argv
[0];
2456 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
2457 cb
.cb_first
= B_TRUE
;
2459 if ((ret
= zfs_iter_children(zhp
, rollback_check
, &cb
)) != 0)
2462 if ((ret
= cb
.cb_error
) != 0)
2466 * Rollback parent to the given snapshot.
2468 ret
= zfs_rollback(zhp
, snap
, force
);
2481 * zfs set property=value { fs | snap | vol } ...
2483 * Sets the given property for all datasets specified on the command line.
2485 typedef struct set_cbdata
{
2491 set_callback(zfs_handle_t
*zhp
, void *data
)
2493 set_cbdata_t
*cbp
= data
;
2495 if (zfs_prop_set(zhp
, cbp
->cb_propname
, cbp
->cb_value
) != 0) {
2496 switch (libzfs_errno(g_zfs
)) {
2497 case EZFS_MOUNTFAILED
:
2498 (void) fprintf(stderr
, gettext("property may be set "
2499 "but unable to remount filesystem\n"));
2501 case EZFS_SHARENFSFAILED
:
2502 (void) fprintf(stderr
, gettext("property may be set "
2503 "but unable to reshare filesystem\n"));
2512 zfs_do_set(int argc
, char **argv
)
2517 /* check for options */
2518 if (argc
> 1 && argv
[1][0] == '-') {
2519 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2524 /* check number of arguments */
2526 (void) fprintf(stderr
, gettext("missing property=value "
2531 (void) fprintf(stderr
, gettext("missing dataset name\n"));
2535 /* validate property=value argument */
2536 cb
.cb_propname
= argv
[1];
2537 if (((cb
.cb_value
= strchr(cb
.cb_propname
, '=')) == NULL
) ||
2538 (cb
.cb_value
[1] == '\0')) {
2539 (void) fprintf(stderr
, gettext("missing value in "
2540 "property=value argument\n"));
2544 *cb
.cb_value
= '\0';
2547 if (*cb
.cb_propname
== '\0') {
2548 (void) fprintf(stderr
,
2549 gettext("missing property in property=value argument\n"));
2553 ret
= zfs_for_each(argc
- 2, argv
+ 2, NULL
,
2554 ZFS_TYPE_DATASET
, NULL
, NULL
, 0, set_callback
, &cb
);
2560 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
2562 * Creates a snapshot with the given name. While functionally equivalent to
2563 * 'zfs create', it is a separate command to differentiate intent.
2566 zfs_do_snapshot(int argc
, char **argv
)
2568 boolean_t recursive
= B_FALSE
;
2573 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
2577 while ((c
= getopt(argc
, argv
, "ro:")) != -1) {
2580 if (parseprop(props
))
2587 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2596 /* check number of arguments */
2598 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2602 (void) fprintf(stderr
, gettext("too many arguments\n"));
2606 ret
= zfs_snapshot(g_zfs
, argv
[0], recursive
, props
);
2608 if (ret
&& recursive
)
2609 (void) fprintf(stderr
, gettext("no snapshots were created\n"));
2619 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
2620 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
2622 * Send a backup stream to stdout.
2625 zfs_do_send(int argc
, char **argv
)
2627 char *fromname
= NULL
;
2628 char *toname
= NULL
;
2631 sendflags_t flags
= { 0 };
2634 boolean_t extraverbose
= B_FALSE
;
2637 while ((c
= getopt(argc
, argv
, ":i:I:RDpv")) != -1) {
2648 flags
.doall
= B_TRUE
;
2651 flags
.replicate
= B_TRUE
;
2654 flags
.props
= B_TRUE
;
2658 extraverbose
= B_TRUE
;
2659 flags
.verbose
= B_TRUE
;
2662 flags
.dedup
= B_TRUE
;
2665 (void) fprintf(stderr
, gettext("missing argument for "
2666 "'%c' option\n"), optopt
);
2670 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2679 /* check number of arguments */
2681 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2685 (void) fprintf(stderr
, gettext("too many arguments\n"));
2689 if (isatty(STDOUT_FILENO
)) {
2690 (void) fprintf(stderr
,
2691 gettext("Error: Stream can not be written to a terminal.\n"
2692 "You must redirect standard output.\n"));
2696 cp
= strchr(argv
[0], '@');
2698 (void) fprintf(stderr
,
2699 gettext("argument must be a snapshot\n"));
2704 zhp
= zfs_open(g_zfs
, argv
[0], ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2709 * If they specified the full path to the snapshot, chop off
2710 * everything except the short name of the snapshot, but special
2711 * case if they specify the origin.
2713 if (fromname
&& (cp
= strchr(fromname
, '@')) != NULL
) {
2714 char origin
[ZFS_MAXNAMELEN
];
2717 (void) zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
,
2718 origin
, sizeof (origin
), &src
, NULL
, 0, B_FALSE
);
2720 if (strcmp(origin
, fromname
) == 0) {
2722 flags
.fromorigin
= B_TRUE
;
2725 if (cp
!= fromname
&& strcmp(argv
[0], fromname
)) {
2726 (void) fprintf(stderr
,
2727 gettext("incremental source must be "
2728 "in same filesystem\n"));
2732 if (strchr(fromname
, '@') || strchr(fromname
, '/')) {
2733 (void) fprintf(stderr
,
2734 gettext("invalid incremental source\n"));
2740 if (flags
.replicate
&& fromname
== NULL
)
2741 flags
.doall
= B_TRUE
;
2743 err
= zfs_send(zhp
, fromname
, toname
, flags
, STDOUT_FILENO
, NULL
, 0,
2744 extraverbose
? &dbgnv
: NULL
);
2748 * dump_nvlist prints to stdout, but that's been
2749 * redirected to a file. Make it print to stderr
2752 (void) dup2(STDERR_FILENO
, STDOUT_FILENO
);
2753 dump_nvlist(dbgnv
, 0);
2762 * zfs receive [-vnFu] [-d | -e] <fs@snap>
2764 * Restore a backup stream from stdin.
2767 zfs_do_receive(int argc
, char **argv
)
2770 recvflags_t flags
= { 0 };
2773 while ((c
= getopt(argc
, argv
, ":denuvF")) != -1) {
2776 flags
.isprefix
= B_TRUE
;
2779 flags
.isprefix
= B_TRUE
;
2780 flags
.istail
= B_TRUE
;
2783 flags
.dryrun
= B_TRUE
;
2786 flags
.nomount
= B_TRUE
;
2789 flags
.verbose
= B_TRUE
;
2792 flags
.force
= B_TRUE
;
2795 (void) fprintf(stderr
, gettext("missing argument for "
2796 "'%c' option\n"), optopt
);
2800 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2809 /* check number of arguments */
2811 (void) fprintf(stderr
, gettext("missing snapshot argument\n"));
2815 (void) fprintf(stderr
, gettext("too many arguments\n"));
2819 if (isatty(STDIN_FILENO
)) {
2820 (void) fprintf(stderr
,
2821 gettext("Error: Backup stream can not be read "
2822 "from a terminal.\n"
2823 "You must redirect standard input.\n"));
2827 err
= zfs_receive(g_zfs
, argv
[0], flags
, STDIN_FILENO
, NULL
);
2833 zfs_do_hold_rele_impl(int argc
, char **argv
, boolean_t holding
)
2838 boolean_t recursive
= B_FALSE
;
2839 boolean_t temphold
= B_FALSE
;
2840 const char *opts
= holding
? "rt" : "r";
2844 while ((c
= getopt(argc
, argv
, opts
)) != -1) {
2853 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2862 /* check number of arguments */
2870 if (holding
&& tag
[0] == '.') {
2871 /* tags starting with '.' are reserved for libzfs */
2872 (void) fprintf(stderr
, gettext("tag may not start with '.'\n"));
2876 for (i
= 0; i
< argc
; ++i
) {
2878 char parent
[ZFS_MAXNAMELEN
];
2880 char *path
= argv
[i
];
2882 delim
= strchr(path
, '@');
2883 if (delim
== NULL
) {
2884 (void) fprintf(stderr
,
2885 gettext("'%s' is not a snapshot\n"), path
);
2889 (void) strncpy(parent
, path
, delim
- path
);
2890 parent
[delim
- path
] = '\0';
2892 zhp
= zfs_open(g_zfs
, parent
,
2893 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2899 if (zfs_hold(zhp
, delim
+1, tag
, recursive
,
2900 temphold
, B_FALSE
, -1, 0, 0) != 0)
2903 if (zfs_release(zhp
, delim
+1, tag
, recursive
) != 0)
2909 return (errors
!= 0);
2913 * zfs hold [-r] [-t] <tag> <snap> ...
2915 * -r Recursively hold
2916 * -t Temporary hold (hidden option)
2918 * Apply a user-hold with the given tag to the list of snapshots.
2921 zfs_do_hold(int argc
, char **argv
)
2923 return (zfs_do_hold_rele_impl(argc
, argv
, B_TRUE
));
2927 * zfs release [-r] <tag> <snap> ...
2929 * -r Recursively release
2931 * Release a user-hold with the given tag from the list of snapshots.
2934 zfs_do_release(int argc
, char **argv
)
2936 return (zfs_do_hold_rele_impl(argc
, argv
, B_FALSE
));
2939 #define CHECK_SPINNER 30
2940 #define SPINNER_TIME 3 /* seconds */
2941 #define MOUNT_TIME 5 /* seconds */
2944 get_one_dataset(zfs_handle_t
*zhp
, void *data
)
2946 static char *spin
[] = { "-", "\\", "|", "/" };
2947 static int spinval
= 0;
2948 static int spincheck
= 0;
2949 static time_t last_spin_time
= (time_t)0;
2950 get_all_cb_t
*cbp
= data
;
2951 zfs_type_t type
= zfs_get_type(zhp
);
2953 if (cbp
->cb_verbose
) {
2954 if (--spincheck
< 0) {
2955 time_t now
= time(NULL
);
2956 if (last_spin_time
+ SPINNER_TIME
< now
) {
2957 update_progress(spin
[spinval
++ % 4]);
2958 last_spin_time
= now
;
2960 spincheck
= CHECK_SPINNER
;
2965 * Interate over any nested datasets.
2967 if (zfs_iter_filesystems(zhp
, get_one_dataset
, data
) != 0) {
2973 * Skip any datasets whose type does not match.
2975 if ((type
& ZFS_TYPE_FILESYSTEM
) == 0) {
2979 libzfs_add_handle(cbp
, zhp
);
2980 assert(cbp
->cb_used
<= cbp
->cb_alloc
);
2986 get_all_datasets(zfs_handle_t
***dslist
, size_t *count
, boolean_t verbose
)
2988 get_all_cb_t cb
= { 0 };
2989 cb
.cb_verbose
= verbose
;
2990 cb
.cb_getone
= get_one_dataset
;
2993 set_progress_header(gettext("Reading ZFS config"));
2994 (void) zfs_iter_root(g_zfs
, get_one_dataset
, &cb
);
2996 *dslist
= cb
.cb_handles
;
2997 *count
= cb
.cb_used
;
3000 finish_progress(gettext("done."));
3004 * Generic callback for sharing or mounting filesystems. Because the code is so
3005 * similar, we have a common function with an extra parameter to determine which
3006 * mode we are using.
3008 #define OP_SHARE 0x1
3009 #define OP_MOUNT 0x2
3012 * Share or mount a dataset.
3015 share_mount_one(zfs_handle_t
*zhp
, int op
, int flags
, char *protocol
,
3016 boolean_t
explicit, const char *options
)
3018 char mountpoint
[ZFS_MAXPROPLEN
];
3019 char shareopts
[ZFS_MAXPROPLEN
];
3020 char smbshareopts
[ZFS_MAXPROPLEN
];
3021 const char *cmdname
= op
== OP_SHARE
? "share" : "mount";
3023 uint64_t zoned
, canmount
;
3024 boolean_t shared_nfs
, shared_smb
;
3026 assert(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
);
3029 * Check to make sure we can mount/share this dataset. If we
3030 * are in the global zone and the filesystem is exported to a
3031 * local zone, or if we are in a local zone and the
3032 * filesystem is not exported, then it is an error.
3034 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3036 if (zoned
&& getzoneid() == GLOBAL_ZONEID
) {
3040 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3041 "dataset is exported to a local zone\n"), cmdname
,
3045 } else if (!zoned
&& getzoneid() != GLOBAL_ZONEID
) {
3049 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3050 "permission denied\n"), cmdname
,
3056 * Ignore any filesystems which don't apply to us. This
3057 * includes those with a legacy mountpoint, or those with
3058 * legacy share options.
3060 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
3061 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
) == 0);
3062 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, shareopts
,
3063 sizeof (shareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3064 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshareopts
,
3065 sizeof (smbshareopts
), NULL
, NULL
, 0, B_FALSE
) == 0);
3067 if (op
== OP_SHARE
&& strcmp(shareopts
, "off") == 0 &&
3068 strcmp(smbshareopts
, "off") == 0) {
3072 (void) fprintf(stderr
, gettext("cannot share '%s': "
3073 "legacy share\n"), zfs_get_name(zhp
));
3074 (void) fprintf(stderr
, gettext("use share(1M) to "
3075 "share this filesystem, or set "
3076 "sharenfs property on\n"));
3081 * We cannot share or mount legacy filesystems. If the
3082 * shareopts is non-legacy but the mountpoint is legacy, we
3083 * treat it as a legacy share.
3085 if (strcmp(mountpoint
, "legacy") == 0) {
3089 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3090 "legacy mountpoint\n"), cmdname
, zfs_get_name(zhp
));
3091 (void) fprintf(stderr
, gettext("use %s(1M) to "
3092 "%s this filesystem\n"), cmdname
, cmdname
);
3096 if (strcmp(mountpoint
, "none") == 0) {
3100 (void) fprintf(stderr
, gettext("cannot %s '%s': no "
3101 "mountpoint set\n"), cmdname
, zfs_get_name(zhp
));
3106 * canmount explicit outcome
3107 * on no pass through
3108 * on yes pass through
3110 * off yes display error, return 1
3111 * noauto no return 0
3112 * noauto yes pass through
3114 canmount
= zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
);
3115 if (canmount
== ZFS_CANMOUNT_OFF
) {
3119 (void) fprintf(stderr
, gettext("cannot %s '%s': "
3120 "'canmount' property is set to 'off'\n"), cmdname
,
3123 } else if (canmount
== ZFS_CANMOUNT_NOAUTO
&& !explicit) {
3128 * At this point, we have verified that the mountpoint and/or
3129 * shareopts are appropriate for auto management. If the
3130 * filesystem is already mounted or shared, return (failing
3131 * for explicit requests); otherwise mount or share the
3137 shared_nfs
= zfs_is_shared_nfs(zhp
, NULL
);
3138 shared_smb
= zfs_is_shared_smb(zhp
, NULL
);
3140 if (shared_nfs
&& shared_smb
||
3141 (shared_nfs
&& strcmp(shareopts
, "on") == 0 &&
3142 strcmp(smbshareopts
, "off") == 0) ||
3143 (shared_smb
&& strcmp(smbshareopts
, "on") == 0 &&
3144 strcmp(shareopts
, "off") == 0)) {
3148 (void) fprintf(stderr
, gettext("cannot share "
3149 "'%s': filesystem already shared\n"),
3154 if (!zfs_is_mounted(zhp
, NULL
) &&
3155 zfs_mount(zhp
, NULL
, 0) != 0)
3158 if (protocol
== NULL
) {
3159 if (zfs_shareall(zhp
) != 0)
3161 } else if (strcmp(protocol
, "nfs") == 0) {
3162 if (zfs_share_nfs(zhp
))
3164 } else if (strcmp(protocol
, "smb") == 0) {
3165 if (zfs_share_smb(zhp
))
3168 (void) fprintf(stderr
, gettext("cannot share "
3169 "'%s': invalid share type '%s' "
3171 zfs_get_name(zhp
), protocol
);
3178 if (options
== NULL
)
3179 mnt
.mnt_mntopts
= "";
3181 mnt
.mnt_mntopts
= (char *)options
;
3183 if (!hasmntopt(&mnt
, MNTOPT_REMOUNT
) &&
3184 zfs_is_mounted(zhp
, NULL
)) {
3188 (void) fprintf(stderr
, gettext("cannot mount "
3189 "'%s': filesystem already mounted\n"),
3194 if (zfs_mount(zhp
, options
, flags
) != 0)
3203 * Reports progress in the form "(current/total)". Not thread-safe.
3206 report_mount_progress(int current
, int total
)
3208 static time_t last_progress_time
= 0;
3209 time_t now
= time(NULL
);
3212 /* report 1..n instead of 0..n-1 */
3215 /* display header if we're here for the first time */
3217 set_progress_header(gettext("Mounting ZFS filesystems"));
3218 } else if (current
!= total
&& last_progress_time
+ MOUNT_TIME
>= now
) {
3219 /* too soon to report again */
3223 last_progress_time
= now
;
3225 (void) sprintf(info
, "(%d/%d)", current
, total
);
3227 if (current
== total
)
3228 finish_progress(info
);
3230 update_progress(info
);
3234 append_options(char *mntopts
, char *newopts
)
3236 int len
= strlen(mntopts
);
3238 /* original length plus new string to append plus 1 for the comma */
3239 if (len
+ 1 + strlen(newopts
) >= MNT_LINE_MAX
) {
3240 (void) fprintf(stderr
, gettext("the opts argument for "
3241 "'%s' option is too long (more than %d chars)\n"),
3242 "-o", MNT_LINE_MAX
);
3247 mntopts
[len
++] = ',';
3249 (void) strcpy(&mntopts
[len
], newopts
);
3253 share_mount(int op
, int argc
, char **argv
)
3256 boolean_t verbose
= B_FALSE
;
3258 char *options
= NULL
;
3262 while ((c
= getopt(argc
, argv
, op
== OP_MOUNT
? ":avo:O" : "a"))
3272 if (*optarg
== '\0') {
3273 (void) fprintf(stderr
, gettext("empty mount "
3274 "options (-o) specified\n"));
3278 if (options
== NULL
)
3279 options
= safe_malloc(MNT_LINE_MAX
+ 1);
3281 /* option validation is done later */
3282 append_options(options
, optarg
);
3286 flags
|= MS_OVERLAY
;
3289 (void) fprintf(stderr
, gettext("missing argument for "
3290 "'%c' option\n"), optopt
);
3294 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3303 /* check number of arguments */
3305 zfs_handle_t
**dslist
= NULL
;
3306 size_t i
, count
= 0;
3307 char *protocol
= NULL
;
3309 if (op
== OP_SHARE
&& argc
> 0) {
3310 if (strcmp(argv
[0], "nfs") != 0 &&
3311 strcmp(argv
[0], "smb") != 0) {
3312 (void) fprintf(stderr
, gettext("share type "
3313 "must be 'nfs' or 'smb'\n"));
3322 (void) fprintf(stderr
, gettext("too many arguments\n"));
3326 start_progress_timer();
3327 get_all_datasets(&dslist
, &count
, verbose
);
3332 qsort(dslist
, count
, sizeof (void *), libzfs_dataset_cmp
);
3334 for (i
= 0; i
< count
; i
++) {
3336 report_mount_progress(i
, count
);
3338 if (share_mount_one(dslist
[i
], op
, flags
, protocol
,
3339 B_FALSE
, options
) != 0)
3341 zfs_close(dslist
[i
]);
3345 } else if (argc
== 0) {
3346 struct mnttab entry
;
3348 if ((op
== OP_SHARE
) || (options
!= NULL
)) {
3349 (void) fprintf(stderr
, gettext("missing filesystem "
3350 "argument (specify -a for all)\n"));
3355 * When mount is given no arguments, go through /etc/mnttab and
3356 * display any active ZFS mounts. We hide any snapshots, since
3357 * they are controlled automatically.
3359 rewind(mnttab_file
);
3360 while (getmntent(mnttab_file
, &entry
) == 0) {
3361 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0 ||
3362 strchr(entry
.mnt_special
, '@') != NULL
)
3365 (void) printf("%-30s %s\n", entry
.mnt_special
,
3373 (void) fprintf(stderr
,
3374 gettext("too many arguments\n"));
3378 if ((zhp
= zfs_open(g_zfs
, argv
[0],
3379 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3382 ret
= share_mount_one(zhp
, op
, flags
, NULL
, B_TRUE
,
3392 * zfs mount -a [nfs]
3393 * zfs mount filesystem
3395 * Mount all filesystems, or mount the given filesystem.
3398 zfs_do_mount(int argc
, char **argv
)
3400 return (share_mount(OP_MOUNT
, argc
, argv
));
3404 * zfs share -a [nfs | smb]
3405 * zfs share filesystem
3407 * Share all filesystems, or share the given filesystem.
3410 zfs_do_share(int argc
, char **argv
)
3412 return (share_mount(OP_SHARE
, argc
, argv
));
3415 typedef struct unshare_unmount_node
{
3416 zfs_handle_t
*un_zhp
;
3418 uu_avl_node_t un_avlnode
;
3419 } unshare_unmount_node_t
;
3423 unshare_unmount_compare(const void *larg
, const void *rarg
, void *unused
)
3425 const unshare_unmount_node_t
*l
= larg
;
3426 const unshare_unmount_node_t
*r
= rarg
;
3428 return (strcmp(l
->un_mountp
, r
->un_mountp
));
3432 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
3433 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
3434 * and unmount it appropriately.
3437 unshare_unmount_path(int op
, char *path
, int flags
, boolean_t is_manual
)
3441 struct stat64 statbuf
;
3442 struct extmnttab entry
;
3443 const char *cmdname
= (op
== OP_SHARE
) ? "unshare" : "unmount";
3447 * Search for the path in /etc/mnttab. Rather than looking for the
3448 * specific path, which can be fooled by non-standard paths (i.e. ".."
3449 * or "//"), we stat() the path and search for the corresponding
3450 * (major,minor) device pair.
3452 if (stat64(path
, &statbuf
) != 0) {
3453 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3454 cmdname
, path
, strerror(errno
));
3457 path_inode
= statbuf
.st_ino
;
3460 * Search for the given (major,minor) pair in the mount table.
3462 rewind(mnttab_file
);
3463 while ((ret
= getextmntent(mnttab_file
, &entry
, 0)) == 0) {
3464 if (entry
.mnt_major
== major(statbuf
.st_dev
) &&
3465 entry
.mnt_minor
== minor(statbuf
.st_dev
))
3469 if (op
== OP_SHARE
) {
3470 (void) fprintf(stderr
, gettext("cannot %s '%s': not "
3471 "currently mounted\n"), cmdname
, path
);
3474 (void) fprintf(stderr
, gettext("warning: %s not in mnttab\n"),
3476 if ((ret
= umount2(path
, flags
)) != 0)
3477 (void) fprintf(stderr
, gettext("%s: %s\n"), path
,
3482 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
3483 (void) fprintf(stderr
, gettext("cannot %s '%s': not a ZFS "
3484 "filesystem\n"), cmdname
, path
);
3488 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3489 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3493 if (stat64(entry
.mnt_mountp
, &statbuf
) != 0) {
3494 (void) fprintf(stderr
, gettext("cannot %s '%s': %s\n"),
3495 cmdname
, path
, strerror(errno
));
3497 } else if (statbuf
.st_ino
!= path_inode
) {
3498 (void) fprintf(stderr
, gettext("cannot "
3499 "%s '%s': not a mountpoint\n"), cmdname
, path
);
3503 if (op
== OP_SHARE
) {
3504 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3505 char smbshare_prop
[ZFS_MAXPROPLEN
];
3507 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
, nfs_mnt_prop
,
3508 sizeof (nfs_mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3509 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
, smbshare_prop
,
3510 sizeof (smbshare_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3512 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3513 strcmp(smbshare_prop
, "off") == 0) {
3514 (void) fprintf(stderr
, gettext("cannot unshare "
3515 "'%s': legacy share\n"), path
);
3516 (void) fprintf(stderr
, gettext("use "
3517 "unshare(1M) to unshare this filesystem\n"));
3518 } else if (!zfs_is_shared(zhp
)) {
3519 (void) fprintf(stderr
, gettext("cannot unshare '%s': "
3520 "not currently shared\n"), path
);
3522 ret
= zfs_unshareall_bypath(zhp
, path
);
3525 char mtpt_prop
[ZFS_MAXPROPLEN
];
3527 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mtpt_prop
,
3528 sizeof (mtpt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
3531 ret
= zfs_unmount(zhp
, NULL
, flags
);
3532 } else if (strcmp(mtpt_prop
, "legacy") == 0) {
3533 (void) fprintf(stderr
, gettext("cannot unmount "
3534 "'%s': legacy mountpoint\n"),
3536 (void) fprintf(stderr
, gettext("use umount(1M) "
3537 "to unmount this filesystem\n"));
3539 ret
= zfs_unmountall(zhp
, flags
);
3550 * Generic callback for unsharing or unmounting a filesystem.
3553 unshare_unmount(int op
, int argc
, char **argv
)
3560 char nfs_mnt_prop
[ZFS_MAXPROPLEN
];
3561 char sharesmb
[ZFS_MAXPROPLEN
];
3564 while ((c
= getopt(argc
, argv
, op
== OP_SHARE
? "a" : "af")) != -1) {
3573 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3584 * We could make use of zfs_for_each() to walk all datasets in
3585 * the system, but this would be very inefficient, especially
3586 * since we would have to linearly search /etc/mnttab for each
3587 * one. Instead, do one pass through /etc/mnttab looking for
3588 * zfs entries and call zfs_unmount() for each one.
3590 * Things get a little tricky if the administrator has created
3591 * mountpoints beneath other ZFS filesystems. In this case, we
3592 * have to unmount the deepest filesystems first. To accomplish
3593 * this, we place all the mountpoints in an AVL tree sorted by
3594 * the special type (dataset name), and walk the result in
3595 * reverse to make sure to get any snapshots first.
3597 struct mnttab entry
;
3598 uu_avl_pool_t
*pool
;
3600 unshare_unmount_node_t
*node
;
3602 uu_avl_walk_t
*walk
;
3605 (void) fprintf(stderr
, gettext("too many arguments\n"));
3609 if (((pool
= uu_avl_pool_create("unmount_pool",
3610 sizeof (unshare_unmount_node_t
),
3611 offsetof(unshare_unmount_node_t
, un_avlnode
),
3612 unshare_unmount_compare
, UU_DEFAULT
)) == NULL
) ||
3613 ((tree
= uu_avl_create(pool
, NULL
, UU_DEFAULT
)) == NULL
))
3616 rewind(mnttab_file
);
3617 while (getmntent(mnttab_file
, &entry
) == 0) {
3619 /* ignore non-ZFS entries */
3620 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
3623 /* ignore snapshots */
3624 if (strchr(entry
.mnt_special
, '@') != NULL
)
3627 if ((zhp
= zfs_open(g_zfs
, entry
.mnt_special
,
3628 ZFS_TYPE_FILESYSTEM
)) == NULL
) {
3635 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3637 sizeof (nfs_mnt_prop
),
3638 NULL
, NULL
, 0, B_FALSE
) == 0);
3639 if (strcmp(nfs_mnt_prop
, "off") != 0)
3641 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3643 sizeof (nfs_mnt_prop
),
3644 NULL
, NULL
, 0, B_FALSE
) == 0);
3645 if (strcmp(nfs_mnt_prop
, "off") == 0)
3649 /* Ignore legacy mounts */
3650 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
3652 sizeof (nfs_mnt_prop
),
3653 NULL
, NULL
, 0, B_FALSE
) == 0);
3654 if (strcmp(nfs_mnt_prop
, "legacy") == 0)
3656 /* Ignore canmount=noauto mounts */
3657 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) ==
3658 ZFS_CANMOUNT_NOAUTO
)
3664 node
= safe_malloc(sizeof (unshare_unmount_node_t
));
3666 node
->un_mountp
= safe_strdup(entry
.mnt_mountp
);
3668 uu_avl_node_init(node
, &node
->un_avlnode
, pool
);
3670 if (uu_avl_find(tree
, node
, NULL
, &idx
) == NULL
) {
3671 uu_avl_insert(tree
, node
, idx
);
3673 zfs_close(node
->un_zhp
);
3674 free(node
->un_mountp
);
3680 * Walk the AVL tree in reverse, unmounting each filesystem and
3681 * removing it from the AVL tree in the process.
3683 if ((walk
= uu_avl_walk_start(tree
,
3684 UU_WALK_REVERSE
| UU_WALK_ROBUST
)) == NULL
)
3687 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
3688 uu_avl_remove(tree
, node
);
3692 if (zfs_unshareall_bypath(node
->un_zhp
,
3693 node
->un_mountp
) != 0)
3698 if (zfs_unmount(node
->un_zhp
,
3699 node
->un_mountp
, flags
) != 0)
3704 zfs_close(node
->un_zhp
);
3705 free(node
->un_mountp
);
3709 uu_avl_walk_end(walk
);
3710 uu_avl_destroy(tree
);
3711 uu_avl_pool_destroy(pool
);
3716 (void) fprintf(stderr
,
3717 gettext("missing filesystem argument\n"));
3719 (void) fprintf(stderr
,
3720 gettext("too many arguments\n"));
3725 * We have an argument, but it may be a full path or a ZFS
3726 * filesystem. Pass full paths off to unmount_path() (shared by
3727 * manual_unmount), otherwise open the filesystem and pass to
3730 if (argv
[0][0] == '/')
3731 return (unshare_unmount_path(op
, argv
[0],
3734 if ((zhp
= zfs_open(g_zfs
, argv
[0],
3735 ZFS_TYPE_FILESYSTEM
)) == NULL
)
3738 verify(zfs_prop_get(zhp
, op
== OP_SHARE
?
3739 ZFS_PROP_SHARENFS
: ZFS_PROP_MOUNTPOINT
,
3740 nfs_mnt_prop
, sizeof (nfs_mnt_prop
), NULL
,
3741 NULL
, 0, B_FALSE
) == 0);
3745 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARENFS
,
3747 sizeof (nfs_mnt_prop
),
3748 NULL
, NULL
, 0, B_FALSE
) == 0);
3749 verify(zfs_prop_get(zhp
, ZFS_PROP_SHARESMB
,
3750 sharesmb
, sizeof (sharesmb
), NULL
, NULL
,
3753 if (strcmp(nfs_mnt_prop
, "off") == 0 &&
3754 strcmp(sharesmb
, "off") == 0) {
3755 (void) fprintf(stderr
, gettext("cannot "
3756 "unshare '%s': legacy share\n"),
3758 (void) fprintf(stderr
, gettext("use "
3759 "unshare(1M) to unshare this "
3762 } else if (!zfs_is_shared(zhp
)) {
3763 (void) fprintf(stderr
, gettext("cannot "
3764 "unshare '%s': not currently "
3765 "shared\n"), zfs_get_name(zhp
));
3767 } else if (zfs_unshareall(zhp
) != 0) {
3773 if (strcmp(nfs_mnt_prop
, "legacy") == 0) {
3774 (void) fprintf(stderr
, gettext("cannot "
3775 "unmount '%s': legacy "
3776 "mountpoint\n"), zfs_get_name(zhp
));
3777 (void) fprintf(stderr
, gettext("use "
3778 "umount(1M) to unmount this "
3781 } else if (!zfs_is_mounted(zhp
, NULL
)) {
3782 (void) fprintf(stderr
, gettext("cannot "
3783 "unmount '%s': not currently "
3787 } else if (zfs_unmountall(zhp
, flags
) != 0) {
3801 * zfs unmount filesystem
3803 * Unmount all filesystems, or a specific ZFS filesystem.
3806 zfs_do_unmount(int argc
, char **argv
)
3808 return (unshare_unmount(OP_MOUNT
, argc
, argv
));
3813 * zfs unshare filesystem
3815 * Unshare all filesystems, or a specific ZFS filesystem.
3818 zfs_do_unshare(int argc
, char **argv
)
3820 return (unshare_unmount(OP_SHARE
, argc
, argv
));
3825 zfs_do_python(int argc
, char **argv
)
3827 (void) execv(pypath
, argv
-1);
3828 (void) printf("internal error: %s not found\n", pypath
);
3833 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
3834 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
3837 manual_mount(int argc
, char **argv
)
3840 char mountpoint
[ZFS_MAXPROPLEN
];
3841 char mntopts
[MNT_LINE_MAX
] = { '\0' };
3845 char *dataset
, *path
;
3848 while ((c
= getopt(argc
, argv
, ":mo:O")) != -1) {
3851 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
3854 flags
|= MS_OVERLAY
;
3857 flags
|= MS_NOMNTTAB
;
3860 (void) fprintf(stderr
, gettext("missing argument for "
3861 "'%c' option\n"), optopt
);
3865 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3867 (void) fprintf(stderr
, gettext("usage: mount [-o opts] "
3876 /* check that we only have two arguments */
3879 (void) fprintf(stderr
, gettext("missing dataset "
3882 (void) fprintf(stderr
,
3883 gettext("missing mountpoint argument\n"));
3885 (void) fprintf(stderr
, gettext("too many arguments\n"));
3886 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
3893 /* try to open the dataset */
3894 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
3897 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, mountpoint
,
3898 sizeof (mountpoint
), NULL
, NULL
, 0, B_FALSE
);
3900 /* check for legacy mountpoint and complain appropriately */
3902 if (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) == 0) {
3903 if (mount(dataset
, path
, MS_OPTIONSTR
| flags
, MNTTYPE_ZFS
,
3904 NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
3905 (void) fprintf(stderr
, gettext("mount failed: %s\n"),
3910 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
3911 "mounted using 'mount -F zfs'\n"), dataset
);
3912 (void) fprintf(stderr
, gettext("Use 'zfs set mountpoint=%s' "
3913 "instead.\n"), path
);
3914 (void) fprintf(stderr
, gettext("If you must use 'mount -F zfs' "
3915 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
3916 (void) fprintf(stderr
, gettext("See zfs(1M) for more "
3925 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
3926 * unmounts of non-legacy filesystems, as this is the dominant administrative
3930 manual_unmount(int argc
, char **argv
)
3936 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3942 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3944 (void) fprintf(stderr
, gettext("usage: unmount [-f] "
3953 /* check arguments */
3956 (void) fprintf(stderr
, gettext("missing path "
3959 (void) fprintf(stderr
, gettext("too many arguments\n"));
3960 (void) fprintf(stderr
, gettext("usage: unmount [-f] <path>\n"));
3964 return (unshare_unmount_path(OP_MOUNT
, argv
[0], flags
, B_TRUE
));
3968 find_command_idx(char *command
, int *idx
)
3972 for (i
= 0; i
< NCOMMAND
; i
++) {
3973 if (command_table
[i
].name
== NULL
)
3976 if (strcmp(command
, command_table
[i
].name
) == 0) {
3985 zfs_do_diff(int argc
, char **argv
)
3989 char *tosnap
= NULL
;
3990 char *fromsnap
= NULL
;
3995 while ((c
= getopt(argc
, argv
, "FHt")) != -1) {
3998 flags
|= ZFS_DIFF_CLASSIFY
;
4001 flags
|= ZFS_DIFF_PARSEABLE
;
4004 flags
|= ZFS_DIFF_TIMESTAMP
;
4007 (void) fprintf(stderr
,
4008 gettext("invalid option '%c'\n"), optopt
);
4017 (void) fprintf(stderr
,
4018 gettext("must provide at least one snapshot name\n"));
4023 (void) fprintf(stderr
, gettext("too many arguments\n"));
4028 tosnap
= (argc
== 2) ? argv
[1] : NULL
;
4031 if (*fromsnap
!= '@')
4032 copy
= strdup(fromsnap
);
4034 copy
= strdup(tosnap
);
4038 if (atp
= strchr(copy
, '@'))
4041 if ((zhp
= zfs_open(g_zfs
, copy
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
4047 * Ignore SIGPIPE so that the library can give us
4048 * information on any failure
4050 (void) sigignore(SIGPIPE
);
4052 err
= zfs_show_diffs(zhp
, STDOUT_FILENO
, fromsnap
, tosnap
, flags
);
4060 main(int argc
, char **argv
)
4067 (void) setlocale(LC_ALL
, "");
4068 (void) textdomain(TEXT_DOMAIN
);
4072 if ((g_zfs
= libzfs_init()) == NULL
) {
4073 (void) fprintf(stderr
, gettext("internal error: failed to "
4074 "initialize ZFS library\n"));
4078 zpool_set_history_str("zfs", argc
, argv
, history_str
);
4079 verify(zpool_stage_history(g_zfs
, history_str
) == 0);
4081 libzfs_print_on_error(g_zfs
, B_TRUE
);
4083 if ((mnttab_file
= fopen(MNTTAB
, "r")) == NULL
) {
4084 (void) fprintf(stderr
, gettext("internal error: unable to "
4085 "open %s\n"), MNTTAB
);
4090 * This command also doubles as the /etc/fs mount and unmount program.
4091 * Determine if we should take this behavior based on argv[0].
4093 progname
= basename(argv
[0]);
4094 if (strcmp(progname
, "mount") == 0) {
4095 ret
= manual_mount(argc
, argv
);
4096 } else if (strcmp(progname
, "umount") == 0) {
4097 ret
= manual_unmount(argc
, argv
);
4100 * Make sure the user has specified some command.
4103 (void) fprintf(stderr
, gettext("missing command\n"));
4110 * The 'umount' command is an alias for 'unmount'
4112 if (strcmp(cmdname
, "umount") == 0)
4113 cmdname
= "unmount";
4116 * The 'recv' command is an alias for 'receive'
4118 if (strcmp(cmdname
, "recv") == 0)
4119 cmdname
= "receive";
4124 if (strcmp(cmdname
, "-?") == 0)
4128 * Run the appropriate command.
4130 libzfs_mnttab_cache(g_zfs
, B_TRUE
);
4131 if (find_command_idx(cmdname
, &i
) == 0) {
4132 current_command
= &command_table
[i
];
4133 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
4134 } else if (strchr(cmdname
, '=') != NULL
) {
4135 verify(find_command_idx("set", &i
) == 0);
4136 current_command
= &command_table
[i
];
4137 ret
= command_table
[i
].func(argc
, argv
);
4139 (void) fprintf(stderr
, gettext("unrecognized "
4140 "command '%s'\n"), cmdname
);
4143 libzfs_mnttab_cache(g_zfs
, B_FALSE
);
4146 (void) fclose(mnttab_file
);
4151 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4152 * for the purposes of running ::findleaks.
4154 if (getenv("ZFS_ABORT") != NULL
) {
4155 (void) printf("dumping core by request\n");