]> git.proxmox.com Git - mirror_zfs-debian.git/blame - cmd/zfs/zfs_main.c
Illumos #1043: Recursive zfs snapshot destroy fails
[mirror_zfs-debian.git] / cmd / zfs / zfs_main.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
bb939d10 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27#include <assert.h>
28#include <ctype.h>
29#include <errno.h>
30#include <libgen.h>
31#include <libintl.h>
32#include <libuutil.h>
33#include <libnvpair.h>
34#include <locale.h>
35#include <stddef.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <strings.h>
39#include <unistd.h>
40#include <fcntl.h>
41#include <zone.h>
9babb374
BB
42#include <grp.h>
43#include <pwd.h>
572e2857 44#include <signal.h>
34dc7c2f
BB
45#include <sys/mkdev.h>
46#include <sys/mntent.h>
47#include <sys/mnttab.h>
48#include <sys/mount.h>
49#include <sys/stat.h>
9babb374 50#include <sys/fs/zfs.h>
428870ff
BB
51#include <sys/types.h>
52#include <time.h>
34dc7c2f
BB
53
54#include <libzfs.h>
55#include <libuutil.h>
56
57#include "zfs_iter.h"
58#include "zfs_util.h"
428870ff 59#include "zfs_comutil.h"
34dc7c2f
BB
60
61libzfs_handle_t *g_zfs;
62
63static FILE *mnttab_file;
64static char history_str[HIS_MAX_RECORD_LEN];
9babb374 65const char *pypath = "/usr/lib/zfs/pyzfs.py";
34dc7c2f
BB
66
67static int zfs_do_clone(int argc, char **argv);
68static int zfs_do_create(int argc, char **argv);
69static int zfs_do_destroy(int argc, char **argv);
70static int zfs_do_get(int argc, char **argv);
71static int zfs_do_inherit(int argc, char **argv);
72static int zfs_do_list(int argc, char **argv);
73static int zfs_do_mount(int argc, char **argv);
74static int zfs_do_rename(int argc, char **argv);
75static int zfs_do_rollback(int argc, char **argv);
76static int zfs_do_set(int argc, char **argv);
77static int zfs_do_upgrade(int argc, char **argv);
78static int zfs_do_snapshot(int argc, char **argv);
79static int zfs_do_unmount(int argc, char **argv);
80static int zfs_do_share(int argc, char **argv);
81static int zfs_do_unshare(int argc, char **argv);
82static int zfs_do_send(int argc, char **argv);
83static int zfs_do_receive(int argc, char **argv);
84static int zfs_do_promote(int argc, char **argv);
9babb374
BB
85static int zfs_do_userspace(int argc, char **argv);
86static int zfs_do_python(int argc, char **argv);
45d1cae3
BB
87static int zfs_do_hold(int argc, char **argv);
88static int zfs_do_release(int argc, char **argv);
572e2857 89static int zfs_do_diff(int argc, char **argv);
34dc7c2f
BB
90
91/*
b128c09f 92 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
34dc7c2f 93 */
b128c09f
BB
94
95#ifdef DEBUG
34dc7c2f
BB
96const char *
97_umem_debug_init(void)
98{
99 return ("default,verbose"); /* $UMEM_DEBUG setting */
100}
101
102const char *
103_umem_logging_init(void)
104{
105 return ("fail,contents"); /* $UMEM_LOGGING setting */
106}
b128c09f 107#endif
34dc7c2f
BB
108
109typedef enum {
110 HELP_CLONE,
111 HELP_CREATE,
112 HELP_DESTROY,
113 HELP_GET,
114 HELP_INHERIT,
115 HELP_UPGRADE,
116 HELP_LIST,
117 HELP_MOUNT,
118 HELP_PROMOTE,
119 HELP_RECEIVE,
120 HELP_RENAME,
121 HELP_ROLLBACK,
122 HELP_SEND,
123 HELP_SET,
124 HELP_SHARE,
125 HELP_SNAPSHOT,
126 HELP_UNMOUNT,
127 HELP_UNSHARE,
128 HELP_ALLOW,
9babb374
BB
129 HELP_UNALLOW,
130 HELP_USERSPACE,
45d1cae3
BB
131 HELP_GROUPSPACE,
132 HELP_HOLD,
133 HELP_HOLDS,
572e2857
BB
134 HELP_RELEASE,
135 HELP_DIFF
34dc7c2f
BB
136} zfs_help_t;
137
138typedef struct zfs_command {
139 const char *name;
140 int (*func)(int argc, char **argv);
141 zfs_help_t usage;
142} zfs_command_t;
143
144/*
145 * Master command table. Each ZFS command has a name, associated function, and
146 * usage message. The usage messages need to be internationalized, so we have
147 * to have a function to return the usage message based on a command index.
148 *
149 * These commands are organized according to how they are displayed in the usage
150 * message. An empty command (one with a NULL name) indicates an empty line in
151 * the generic usage message.
152 */
153static zfs_command_t command_table[] = {
154 { "create", zfs_do_create, HELP_CREATE },
155 { "destroy", zfs_do_destroy, HELP_DESTROY },
156 { NULL },
157 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
158 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
159 { "clone", zfs_do_clone, HELP_CLONE },
160 { "promote", zfs_do_promote, HELP_PROMOTE },
161 { "rename", zfs_do_rename, HELP_RENAME },
162 { NULL },
163 { "list", zfs_do_list, HELP_LIST },
164 { NULL },
165 { "set", zfs_do_set, HELP_SET },
428870ff 166 { "get", zfs_do_get, HELP_GET },
34dc7c2f
BB
167 { "inherit", zfs_do_inherit, HELP_INHERIT },
168 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
9babb374
BB
169 { "userspace", zfs_do_userspace, HELP_USERSPACE },
170 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
34dc7c2f
BB
171 { NULL },
172 { "mount", zfs_do_mount, HELP_MOUNT },
173 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
174 { "share", zfs_do_share, HELP_SHARE },
175 { "unshare", zfs_do_unshare, HELP_UNSHARE },
176 { NULL },
177 { "send", zfs_do_send, HELP_SEND },
178 { "receive", zfs_do_receive, HELP_RECEIVE },
179 { NULL },
9babb374 180 { "allow", zfs_do_python, HELP_ALLOW },
34dc7c2f 181 { NULL },
9babb374 182 { "unallow", zfs_do_python, HELP_UNALLOW },
45d1cae3
BB
183 { NULL },
184 { "hold", zfs_do_hold, HELP_HOLD },
185 { "holds", zfs_do_python, HELP_HOLDS },
186 { "release", zfs_do_release, HELP_RELEASE },
572e2857 187 { "diff", zfs_do_diff, HELP_DIFF },
34dc7c2f
BB
188};
189
190#define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
191
192zfs_command_t *current_command;
193
194static const char *
195get_usage(zfs_help_t idx)
196{
197 switch (idx) {
198 case HELP_CLONE:
b128c09f
BB
199 return (gettext("\tclone [-p] [-o property=value] ... "
200 "<snapshot> <filesystem|volume>\n"));
34dc7c2f
BB
201 case HELP_CREATE:
202 return (gettext("\tcreate [-p] [-o property=value] ... "
203 "<filesystem>\n"
204 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
205 "-V <size> <volume>\n"));
206 case HELP_DESTROY:
428870ff
BB
207 return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
208 "\tdestroy [-rRd] <snapshot>\n"));
34dc7c2f 209 case HELP_GET:
9babb374 210 return (gettext("\tget [-rHp] [-d max] "
428870ff 211 "[-o \"all\" | field[,...]] [-s source[,...]]\n"
34dc7c2f
BB
212 "\t <\"all\" | property[,...]> "
213 "[filesystem|volume|snapshot] ...\n"));
214 case HELP_INHERIT:
428870ff 215 return (gettext("\tinherit [-rS] <property> "
b128c09f 216 "<filesystem|volume|snapshot> ...\n"));
34dc7c2f
BB
217 case HELP_UPGRADE:
218 return (gettext("\tupgrade [-v]\n"
219 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
220 case HELP_LIST:
9babb374
BB
221 return (gettext("\tlist [-rH][-d max] "
222 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
34dc7c2f
BB
223 "\t [-S property] ... "
224 "[filesystem|volume|snapshot] ...\n"));
225 case HELP_MOUNT:
226 return (gettext("\tmount\n"
227 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
228 case HELP_PROMOTE:
229 return (gettext("\tpromote <clone-filesystem>\n"));
230 case HELP_RECEIVE:
428870ff 231 return (gettext("\treceive [-vnFu] <filesystem|volume|"
34dc7c2f 232 "snapshot>\n"
428870ff 233 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
34dc7c2f
BB
234 case HELP_RENAME:
235 return (gettext("\trename <filesystem|volume|snapshot> "
236 "<filesystem|volume|snapshot>\n"
237 "\trename -p <filesystem|volume> <filesystem|volume>\n"
238 "\trename -r <snapshot> <snapshot>"));
239 case HELP_ROLLBACK:
240 return (gettext("\trollback [-rRf] <snapshot>\n"));
241 case HELP_SEND:
e0f86c98 242 return (gettext("\tsend [-vRDp] [-[iI] snapshot] <snapshot>\n"));
34dc7c2f
BB
243 case HELP_SET:
244 return (gettext("\tset <property=value> "
b128c09f 245 "<filesystem|volume|snapshot> ...\n"));
34dc7c2f
BB
246 case HELP_SHARE:
247 return (gettext("\tshare <-a | filesystem>\n"));
248 case HELP_SNAPSHOT:
b128c09f 249 return (gettext("\tsnapshot [-r] [-o property=value] ... "
34dc7c2f
BB
250 "<filesystem@snapname|volume@snapname>\n"));
251 case HELP_UNMOUNT:
252 return (gettext("\tunmount [-f] "
253 "<-a | filesystem|mountpoint>\n"));
254 case HELP_UNSHARE:
45d1cae3 255 return (gettext("\tunshare "
34dc7c2f
BB
256 "<-a | filesystem|mountpoint>\n"));
257 case HELP_ALLOW:
9babb374
BB
258 return (gettext("\tallow <filesystem|volume>\n"
259 "\tallow [-ldug] "
34dc7c2f
BB
260 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
261 "\t <filesystem|volume>\n"
262 "\tallow [-ld] -e <perm|@setname>[,...] "
263 "<filesystem|volume>\n"
264 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
265 "\tallow -s @setname <perm|@setname>[,...] "
266 "<filesystem|volume>\n"));
267 case HELP_UNALLOW:
268 return (gettext("\tunallow [-rldug] "
269 "<\"everyone\"|user|group>[,...]\n"
270 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
271 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
272 "<filesystem|volume>\n"
273 "\tunallow [-r] -c [<perm|@setname>[,...]] "
274 "<filesystem|volume>\n"
275 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
276 "<filesystem|volume>\n"));
9babb374
BB
277 case HELP_USERSPACE:
278 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
279 "[-sS field] ... [-t type[,...]]\n"
280 "\t <filesystem|snapshot>\n"));
281 case HELP_GROUPSPACE:
282 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
283 "[-sS field] ... [-t type[,...]]\n"
284 "\t <filesystem|snapshot>\n"));
45d1cae3
BB
285 case HELP_HOLD:
286 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
287 case HELP_HOLDS:
288 return (gettext("\tholds [-r] <snapshot> ...\n"));
289 case HELP_RELEASE:
290 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
572e2857
BB
291 case HELP_DIFF:
292 return (gettext("\tdiff [-FHt] <snapshot> "
293 "[snapshot|filesystem]\n"));
34dc7c2f
BB
294 }
295
296 abort();
297 /* NOTREACHED */
298}
299
428870ff
BB
300void
301nomem(void)
302{
303 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
304 exit(1);
305}
306
34dc7c2f
BB
307/*
308 * Utility function to guarantee malloc() success.
309 */
428870ff 310
34dc7c2f
BB
311void *
312safe_malloc(size_t size)
313{
314 void *data;
315
428870ff
BB
316 if ((data = calloc(1, size)) == NULL)
317 nomem();
34dc7c2f
BB
318
319 return (data);
320}
321
428870ff
BB
322static char *
323safe_strdup(char *str)
324{
325 char *dupstr = strdup(str);
326
327 if (dupstr == NULL)
328 nomem();
329
330 return (dupstr);
331}
332
34dc7c2f
BB
333/*
334 * Callback routine that will print out information for each of
335 * the properties.
336 */
337static int
338usage_prop_cb(int prop, void *cb)
339{
340 FILE *fp = cb;
341
b128c09f 342 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
34dc7c2f 343
b128c09f
BB
344 if (zfs_prop_readonly(prop))
345 (void) fprintf(fp, " NO ");
34dc7c2f 346 else
b128c09f 347 (void) fprintf(fp, "YES ");
34dc7c2f
BB
348
349 if (zfs_prop_inheritable(prop))
350 (void) fprintf(fp, " YES ");
351 else
352 (void) fprintf(fp, " NO ");
353
354 if (zfs_prop_values(prop) == NULL)
355 (void) fprintf(fp, "-\n");
356 else
357 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
358
359 return (ZPROP_CONT);
360}
361
362/*
363 * Display usage message. If we're inside a command, display only the usage for
364 * that command. Otherwise, iterate over the entire command table and display
365 * a complete usage message.
366 */
367static void
368usage(boolean_t requested)
369{
370 int i;
371 boolean_t show_properties = B_FALSE;
34dc7c2f
BB
372 FILE *fp = requested ? stdout : stderr;
373
374 if (current_command == NULL) {
375
376 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
377 (void) fprintf(fp,
378 gettext("where 'command' is one of the following:\n\n"));
379
380 for (i = 0; i < NCOMMAND; i++) {
381 if (command_table[i].name == NULL)
382 (void) fprintf(fp, "\n");
383 else
384 (void) fprintf(fp, "%s",
385 get_usage(command_table[i].usage));
386 }
387
388 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
389 "pool/[dataset/]*dataset[@name]\n"));
390 } else {
391 (void) fprintf(fp, gettext("usage:\n"));
392 (void) fprintf(fp, "%s", get_usage(current_command->usage));
393 }
394
395 if (current_command != NULL &&
396 (strcmp(current_command->name, "set") == 0 ||
397 strcmp(current_command->name, "get") == 0 ||
398 strcmp(current_command->name, "inherit") == 0 ||
399 strcmp(current_command->name, "list") == 0))
400 show_properties = B_TRUE;
401
34dc7c2f 402 if (show_properties) {
34dc7c2f
BB
403 (void) fprintf(fp,
404 gettext("\nThe following properties are supported:\n"));
405
406 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
407 "PROPERTY", "EDIT", "INHERIT", "VALUES");
408
409 /* Iterate over all properties */
410 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
411 ZFS_TYPE_DATASET);
412
9babb374
BB
413 (void) fprintf(fp, "\t%-15s ", "userused@...");
414 (void) fprintf(fp, " NO NO <size>\n");
415 (void) fprintf(fp, "\t%-15s ", "groupused@...");
416 (void) fprintf(fp, " NO NO <size>\n");
417 (void) fprintf(fp, "\t%-15s ", "userquota@...");
418 (void) fprintf(fp, "YES NO <size> | none\n");
419 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
420 (void) fprintf(fp, "YES NO <size> | none\n");
421
34dc7c2f
BB
422 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
423 "with standard units such as K, M, G, etc.\n"));
b128c09f 424 (void) fprintf(fp, gettext("\nUser-defined properties can "
34dc7c2f 425 "be specified by using a name containing a colon (:).\n"));
9babb374
BB
426 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
427 "properties must be appended with\n"
428 "a user or group specifier of one of these forms:\n"
429 " POSIX name (eg: \"matt\")\n"
430 " POSIX id (eg: \"126829\")\n"
431 " SMB name@domain (eg: \"matt@sun\")\n"
432 " SMB SID (eg: \"S-1-234-567-89\")\n"));
34dc7c2f 433 } else {
34dc7c2f 434 (void) fprintf(fp,
fb5f0bc8
BB
435 gettext("\nFor the property list, run: %s\n"),
436 "zfs set|get");
34dc7c2f 437 (void) fprintf(fp,
fb5f0bc8
BB
438 gettext("\nFor the delegated permission list, run: %s\n"),
439 "zfs allow|unallow");
34dc7c2f
BB
440 }
441
442 /*
443 * See comments at end of main().
444 */
445 if (getenv("ZFS_ABORT") != NULL) {
446 (void) printf("dumping core by request\n");
447 abort();
448 }
449
450 exit(requested ? 0 : 2);
451}
452
b128c09f
BB
453static int
454parseprop(nvlist_t *props)
455{
456 char *propname = optarg;
457 char *propval, *strval;
458
459 if ((propval = strchr(propname, '=')) == NULL) {
460 (void) fprintf(stderr, gettext("missing "
461 "'=' for -o option\n"));
462 return (-1);
463 }
464 *propval = '\0';
465 propval++;
466 if (nvlist_lookup_string(props, propname, &strval) == 0) {
467 (void) fprintf(stderr, gettext("property '%s' "
468 "specified multiple times\n"), propname);
469 return (-1);
470 }
428870ff
BB
471 if (nvlist_add_string(props, propname, propval) != 0)
472 nomem();
b128c09f 473 return (0);
b128c09f
BB
474}
475
9babb374
BB
476static int
477parse_depth(char *opt, int *flags)
478{
479 char *tmp;
480 int depth;
481
482 depth = (int)strtol(opt, &tmp, 0);
483 if (*tmp) {
484 (void) fprintf(stderr,
485 gettext("%s is not an integer\n"), optarg);
486 usage(B_FALSE);
487 }
488 if (depth < 0) {
489 (void) fprintf(stderr,
490 gettext("Depth can not be negative.\n"));
491 usage(B_FALSE);
492 }
493 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
494 return (depth);
495}
496
428870ff
BB
497#define PROGRESS_DELAY 2 /* seconds */
498
499static 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";
500static time_t pt_begin;
501static char *pt_header = NULL;
502static boolean_t pt_shown;
503
504static void
505start_progress_timer(void)
506{
507 pt_begin = time(NULL) + PROGRESS_DELAY;
508 pt_shown = B_FALSE;
509}
510
511static void
512set_progress_header(char *header)
513{
514 assert(pt_header == NULL);
515 pt_header = safe_strdup(header);
516 if (pt_shown) {
517 (void) printf("%s: ", header);
518 (void) fflush(stdout);
519 }
520}
521
522static void
523update_progress(char *update)
524{
525 if (!pt_shown && time(NULL) > pt_begin) {
526 int len = strlen(update);
527
528 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
529 pt_reverse);
530 (void) fflush(stdout);
531 pt_shown = B_TRUE;
532 } else if (pt_shown) {
533 int len = strlen(update);
534
535 (void) printf("%s%*.*s", update, len, len, pt_reverse);
536 (void) fflush(stdout);
537 }
538}
539
540static void
541finish_progress(char *done)
542{
543 if (pt_shown) {
544 (void) printf("%s\n", done);
545 (void) fflush(stdout);
546 }
547 free(pt_header);
548 pt_header = NULL;
549}
d603ed6c 550
34dc7c2f 551/*
b128c09f 552 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
34dc7c2f
BB
553 *
554 * Given an existing dataset, create a writable copy whose initial contents
555 * are the same as the source. The newly created dataset maintains a
556 * dependency on the original; the original cannot be destroyed so long as
557 * the clone exists.
558 *
559 * The '-p' flag creates all the non-existing ancestors of the target first.
560 */
561static int
562zfs_do_clone(int argc, char **argv)
563{
b128c09f 564 zfs_handle_t *zhp = NULL;
34dc7c2f 565 boolean_t parents = B_FALSE;
b128c09f 566 nvlist_t *props;
34dc7c2f
BB
567 int ret;
568 int c;
569
428870ff
BB
570 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
571 nomem();
b128c09f 572
34dc7c2f 573 /* check options */
b128c09f 574 while ((c = getopt(argc, argv, "o:p")) != -1) {
34dc7c2f 575 switch (c) {
b128c09f
BB
576 case 'o':
577 if (parseprop(props))
578 return (1);
579 break;
34dc7c2f
BB
580 case 'p':
581 parents = B_TRUE;
582 break;
583 case '?':
584 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
585 optopt);
b128c09f 586 goto usage;
34dc7c2f
BB
587 }
588 }
589
590 argc -= optind;
591 argv += optind;
592
593 /* check number of arguments */
594 if (argc < 1) {
595 (void) fprintf(stderr, gettext("missing source dataset "
596 "argument\n"));
b128c09f 597 goto usage;
34dc7c2f
BB
598 }
599 if (argc < 2) {
600 (void) fprintf(stderr, gettext("missing target dataset "
601 "argument\n"));
b128c09f 602 goto usage;
34dc7c2f
BB
603 }
604 if (argc > 2) {
605 (void) fprintf(stderr, gettext("too many arguments\n"));
b128c09f 606 goto usage;
34dc7c2f
BB
607 }
608
609 /* open the source dataset */
610 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
611 return (1);
612
613 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
614 ZFS_TYPE_VOLUME)) {
615 /*
616 * Now create the ancestors of the target dataset. If the
617 * target already exists and '-p' option was used we should not
618 * complain.
619 */
620 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
621 ZFS_TYPE_VOLUME))
622 return (0);
623 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
624 return (1);
625 }
626
627 /* pass to libzfs */
b128c09f 628 ret = zfs_clone(zhp, argv[1], props);
34dc7c2f
BB
629
630 /* create the mountpoint if necessary */
631 if (ret == 0) {
632 zfs_handle_t *clone;
633
634 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
635 if (clone != NULL) {
572e2857
BB
636 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
637 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
638 ret = zfs_share(clone);
34dc7c2f
BB
639 zfs_close(clone);
640 }
641 }
642
643 zfs_close(zhp);
b128c09f 644 nvlist_free(props);
34dc7c2f 645
b128c09f
BB
646 return (!!ret);
647
648usage:
649 if (zhp)
650 zfs_close(zhp);
651 nvlist_free(props);
652 usage(B_FALSE);
653 return (-1);
34dc7c2f
BB
654}
655
656/*
657 * zfs create [-p] [-o prop=value] ... fs
658 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
659 *
660 * Create a new dataset. This command can be used to create filesystems
661 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
662 * For volumes, the user must specify a size to be used.
663 *
664 * The '-s' flag applies only to volumes, and indicates that we should not try
665 * to set the reservation for this volume. By default we set a reservation
666 * equal to the size for any volume. For pools with SPA_VERSION >=
667 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
668 *
669 * The '-p' flag creates all the non-existing ancestors of the target first.
670 */
671static int
672zfs_do_create(int argc, char **argv)
673{
674 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
675 zfs_handle_t *zhp = NULL;
d4ed6673 676 uint64_t volsize = 0;
34dc7c2f
BB
677 int c;
678 boolean_t noreserve = B_FALSE;
679 boolean_t bflag = B_FALSE;
680 boolean_t parents = B_FALSE;
681 int ret = 1;
b128c09f 682 nvlist_t *props;
34dc7c2f 683 uint64_t intval;
572e2857 684 int canmount = ZFS_CANMOUNT_OFF;
34dc7c2f 685
428870ff
BB
686 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
687 nomem();
34dc7c2f
BB
688
689 /* check options */
690 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
691 switch (c) {
692 case 'V':
693 type = ZFS_TYPE_VOLUME;
694 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
695 (void) fprintf(stderr, gettext("bad volume "
696 "size '%s': %s\n"), optarg,
697 libzfs_error_description(g_zfs));
698 goto error;
699 }
700
701 if (nvlist_add_uint64(props,
428870ff
BB
702 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
703 nomem();
34dc7c2f
BB
704 volsize = intval;
705 break;
706 case 'p':
707 parents = B_TRUE;
708 break;
709 case 'b':
710 bflag = B_TRUE;
711 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
712 (void) fprintf(stderr, gettext("bad volume "
713 "block size '%s': %s\n"), optarg,
714 libzfs_error_description(g_zfs));
715 goto error;
716 }
717
718 if (nvlist_add_uint64(props,
719 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
428870ff
BB
720 intval) != 0)
721 nomem();
34dc7c2f
BB
722 break;
723 case 'o':
b128c09f 724 if (parseprop(props))
34dc7c2f 725 goto error;
34dc7c2f
BB
726 break;
727 case 's':
728 noreserve = B_TRUE;
729 break;
730 case ':':
731 (void) fprintf(stderr, gettext("missing size "
732 "argument\n"));
733 goto badusage;
734 break;
735 case '?':
736 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
737 optopt);
738 goto badusage;
739 }
740 }
741
742 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
743 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
744 "used when creating a volume\n"));
745 goto badusage;
746 }
747
748 argc -= optind;
749 argv += optind;
750
751 /* check number of arguments */
752 if (argc == 0) {
753 (void) fprintf(stderr, gettext("missing %s argument\n"),
754 zfs_type_to_name(type));
755 goto badusage;
756 }
757 if (argc > 1) {
758 (void) fprintf(stderr, gettext("too many arguments\n"));
759 goto badusage;
760 }
761
762 if (type == ZFS_TYPE_VOLUME && !noreserve) {
763 zpool_handle_t *zpool_handle;
764 uint64_t spa_version;
765 char *p;
766 zfs_prop_t resv_prop;
b128c09f 767 char *strval;
34dc7c2f 768
c65aa5b2 769 if ((p = strchr(argv[0], '/')))
34dc7c2f
BB
770 *p = '\0';
771 zpool_handle = zpool_open(g_zfs, argv[0]);
772 if (p != NULL)
773 *p = '/';
774 if (zpool_handle == NULL)
775 goto error;
776 spa_version = zpool_get_prop_int(zpool_handle,
777 ZPOOL_PROP_VERSION, NULL);
778 zpool_close(zpool_handle);
779 if (spa_version >= SPA_VERSION_REFRESERVATION)
780 resv_prop = ZFS_PROP_REFRESERVATION;
781 else
782 resv_prop = ZFS_PROP_RESERVATION;
428870ff 783 volsize = zvol_volsize_to_reservation(volsize, props);
34dc7c2f
BB
784
785 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
786 &strval) != 0) {
787 if (nvlist_add_uint64(props,
788 zfs_prop_to_name(resv_prop), volsize) != 0) {
34dc7c2f 789 nvlist_free(props);
428870ff 790 nomem();
34dc7c2f
BB
791 }
792 }
793 }
794
795 if (parents && zfs_name_valid(argv[0], type)) {
796 /*
797 * Now create the ancestors of target dataset. If the target
798 * already exists and '-p' option was used we should not
799 * complain.
800 */
801 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
802 ret = 0;
803 goto error;
804 }
805 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
806 goto error;
807 }
808
809 /* pass to libzfs */
810 if (zfs_create(g_zfs, argv[0], type, props) != 0)
811 goto error;
812
813 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
814 goto error;
572e2857
BB
815
816 ret = 0;
34dc7c2f
BB
817 /*
818 * if the user doesn't want the dataset automatically mounted,
819 * then skip the mount/share step
820 */
572e2857
BB
821 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
822 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
34dc7c2f
BB
823
824 /*
825 * Mount and/or share the new filesystem as appropriate. We provide a
826 * verbose error message to let the user know that their filesystem was
827 * in fact created, even if we failed to mount or share it.
828 */
34dc7c2f
BB
829 if (canmount == ZFS_CANMOUNT_ON) {
830 if (zfs_mount(zhp, NULL, 0) != 0) {
831 (void) fprintf(stderr, gettext("filesystem "
832 "successfully created, but not mounted\n"));
833 ret = 1;
834 } else if (zfs_share(zhp) != 0) {
835 (void) fprintf(stderr, gettext("filesystem "
836 "successfully created, but not shared\n"));
837 ret = 1;
838 }
839 }
840
841error:
842 if (zhp)
843 zfs_close(zhp);
844 nvlist_free(props);
845 return (ret);
846badusage:
847 nvlist_free(props);
848 usage(B_FALSE);
849 return (2);
850}
851
852/*
428870ff
BB
853 * zfs destroy [-rRf] <fs, vol>
854 * zfs destroy [-rRd] <snap>
34dc7c2f 855 *
428870ff
BB
856 * -r Recursively destroy all children
857 * -R Recursively destroy all dependents, including clones
858 * -f Force unmounting of any dependents
45d1cae3 859 * -d If we can't destroy now, mark for deferred destruction
34dc7c2f
BB
860 *
861 * Destroys the given dataset. By default, it will unmount any filesystems,
862 * and refuse to destroy a dataset that has any dependents. A dependent can
863 * either be a child, or a clone of a child.
864 */
865typedef struct destroy_cbdata {
866 boolean_t cb_first;
867 int cb_force;
868 int cb_recurse;
869 int cb_error;
870 int cb_needforce;
871 int cb_doclones;
872 boolean_t cb_closezhp;
873 zfs_handle_t *cb_target;
874 char *cb_snapname;
45d1cae3 875 boolean_t cb_defer_destroy;
34dc7c2f
BB
876} destroy_cbdata_t;
877
878/*
879 * Check for any dependents based on the '-r' or '-R' flags.
880 */
881static int
882destroy_check_dependent(zfs_handle_t *zhp, void *data)
883{
884 destroy_cbdata_t *cbp = data;
885 const char *tname = zfs_get_name(cbp->cb_target);
886 const char *name = zfs_get_name(zhp);
887
888 if (strncmp(tname, name, strlen(tname)) == 0 &&
889 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
890 /*
891 * This is a direct descendant, not a clone somewhere else in
892 * the hierarchy.
893 */
894 if (cbp->cb_recurse)
895 goto out;
896
897 if (cbp->cb_first) {
898 (void) fprintf(stderr, gettext("cannot destroy '%s': "
899 "%s has children\n"),
900 zfs_get_name(cbp->cb_target),
901 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
902 (void) fprintf(stderr, gettext("use '-r' to destroy "
903 "the following datasets:\n"));
904 cbp->cb_first = B_FALSE;
905 cbp->cb_error = 1;
906 }
907
908 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
909 } else {
910 /*
911 * This is a clone. We only want to report this if the '-r'
912 * wasn't specified, or the target is a snapshot.
913 */
914 if (!cbp->cb_recurse &&
915 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
916 goto out;
917
918 if (cbp->cb_first) {
919 (void) fprintf(stderr, gettext("cannot destroy '%s': "
920 "%s has dependent clones\n"),
921 zfs_get_name(cbp->cb_target),
922 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
923 (void) fprintf(stderr, gettext("use '-R' to destroy "
924 "the following datasets:\n"));
925 cbp->cb_first = B_FALSE;
926 cbp->cb_error = 1;
927 }
928
929 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
930 }
931
932out:
933 zfs_close(zhp);
934 return (0);
935}
936
937static int
938destroy_callback(zfs_handle_t *zhp, void *data)
939{
940 destroy_cbdata_t *cbp = data;
941
942 /*
943 * Ignore pools (which we've already flagged as an error before getting
428870ff 944 * here).
34dc7c2f
BB
945 */
946 if (strchr(zfs_get_name(zhp), '/') == NULL &&
947 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
948 zfs_close(zhp);
949 return (0);
950 }
951
952 /*
953 * Bail out on the first error.
954 */
955 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
45d1cae3 956 zfs_destroy(zhp, cbp->cb_defer_destroy) != 0) {
34dc7c2f
BB
957 zfs_close(zhp);
958 return (-1);
959 }
960
961 zfs_close(zhp);
962 return (0);
963}
964
965static int
966destroy_snap_clones(zfs_handle_t *zhp, void *arg)
967{
968 destroy_cbdata_t *cbp = arg;
969 char thissnap[MAXPATHLEN];
970 zfs_handle_t *szhp;
971 boolean_t closezhp = cbp->cb_closezhp;
972 int rv;
973
974 (void) snprintf(thissnap, sizeof (thissnap),
975 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
976
977 libzfs_print_on_error(g_zfs, B_FALSE);
978 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
979 libzfs_print_on_error(g_zfs, B_TRUE);
980 if (szhp) {
981 /*
982 * Destroy any clones of this snapshot
983 */
984 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
985 cbp) != 0) {
986 zfs_close(szhp);
987 if (closezhp)
988 zfs_close(zhp);
989 return (-1);
990 }
991 zfs_close(szhp);
992 }
993
994 cbp->cb_closezhp = B_TRUE;
995 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
996 if (closezhp)
997 zfs_close(zhp);
998 return (rv);
999}
1000
1001static int
1002zfs_do_destroy(int argc, char **argv)
1003{
1004 destroy_cbdata_t cb = { 0 };
1005 int c;
1006 zfs_handle_t *zhp;
1007 char *cp;
428870ff 1008 zfs_type_t type = ZFS_TYPE_DATASET;
34dc7c2f
BB
1009
1010 /* check options */
45d1cae3 1011 while ((c = getopt(argc, argv, "dfrR")) != -1) {
34dc7c2f 1012 switch (c) {
45d1cae3
BB
1013 case 'd':
1014 cb.cb_defer_destroy = B_TRUE;
428870ff 1015 type = ZFS_TYPE_SNAPSHOT;
45d1cae3 1016 break;
34dc7c2f
BB
1017 case 'f':
1018 cb.cb_force = 1;
1019 break;
1020 case 'r':
1021 cb.cb_recurse = 1;
1022 break;
1023 case 'R':
1024 cb.cb_recurse = 1;
1025 cb.cb_doclones = 1;
1026 break;
1027 case '?':
1028 default:
1029 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1030 optopt);
1031 usage(B_FALSE);
1032 }
1033 }
1034
1035 argc -= optind;
1036 argv += optind;
1037
1038 /* check number of arguments */
1039 if (argc == 0) {
1040 (void) fprintf(stderr, gettext("missing path argument\n"));
1041 usage(B_FALSE);
1042 }
1043 if (argc > 1) {
1044 (void) fprintf(stderr, gettext("too many arguments\n"));
1045 usage(B_FALSE);
1046 }
1047
1048 /*
1049 * If we are doing recursive destroy of a snapshot, then the
1050 * named snapshot may not exist. Go straight to libzfs.
1051 */
1052 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
1053 int ret;
1054
1055 *cp = '\0';
1056 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
1057 return (1);
1058 *cp = '@';
1059 cp++;
1060
1061 if (cb.cb_doclones) {
428870ff
BB
1062 boolean_t defer = cb.cb_defer_destroy;
1063
1064 /*
1065 * Temporarily ignore the defer_destroy setting since
1066 * it's not supported for clones.
1067 */
1068 cb.cb_defer_destroy = B_FALSE;
34dc7c2f
BB
1069 cb.cb_snapname = cp;
1070 if (destroy_snap_clones(zhp, &cb) != 0) {
1071 zfs_close(zhp);
1072 return (1);
1073 }
428870ff 1074 cb.cb_defer_destroy = defer;
34dc7c2f
BB
1075 }
1076
45d1cae3 1077 ret = zfs_destroy_snaps(zhp, cp, cb.cb_defer_destroy);
34dc7c2f
BB
1078 zfs_close(zhp);
1079 if (ret) {
1080 (void) fprintf(stderr,
1081 gettext("no snapshots destroyed\n"));
1082 }
1083 return (ret != 0);
1084 }
1085
34dc7c2f 1086 /* Open the given dataset */
428870ff 1087 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
34dc7c2f
BB
1088 return (1);
1089
1090 cb.cb_target = zhp;
1091
1092 /*
1093 * Perform an explicit check for pools before going any further.
1094 */
1095 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1096 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1097 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1098 "operation does not apply to pools\n"),
1099 zfs_get_name(zhp));
1100 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1101 "%s' to destroy all datasets in the pool\n"),
1102 zfs_get_name(zhp));
1103 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1104 "to destroy the pool itself\n"), zfs_get_name(zhp));
1105 zfs_close(zhp);
1106 return (1);
1107 }
1108
1109 /*
1110 * Check for any dependents and/or clones.
1111 */
1112 cb.cb_first = B_TRUE;
45d1cae3 1113 if (!cb.cb_doclones && !cb.cb_defer_destroy &&
34dc7c2f
BB
1114 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1115 &cb) != 0) {
1116 zfs_close(zhp);
1117 return (1);
1118 }
1119
45d1cae3
BB
1120 if (cb.cb_error || (!cb.cb_defer_destroy &&
1121 (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0))) {
34dc7c2f
BB
1122 zfs_close(zhp);
1123 return (1);
1124 }
1125
1126 /*
1127 * Do the real thing. The callback will close the handle regardless of
1128 * whether it succeeds or not.
1129 */
1130
1131 if (destroy_callback(zhp, &cb) != 0)
1132 return (1);
1133
34dc7c2f
BB
1134 return (0);
1135}
1136
428870ff
BB
1137static boolean_t
1138is_recvd_column(zprop_get_cbdata_t *cbp)
1139{
1140 int i;
1141 zfs_get_column_t col;
1142
1143 for (i = 0; i < ZFS_GET_NCOLS &&
1144 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1145 if (col == GET_COL_RECVD)
1146 return (B_TRUE);
1147 return (B_FALSE);
1148}
1149
34dc7c2f 1150/*
428870ff
BB
1151 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1152 * < all | property[,property]... > < fs | snap | vol > ...
34dc7c2f
BB
1153 *
1154 * -r recurse over any child datasets
1155 * -H scripted mode. Headers are stripped, and fields are separated
1156 * by tabs instead of spaces.
428870ff
BB
1157 * -o Set of fields to display. One of "name,property,value,
1158 * received,source". Default is "name,property,value,source".
1159 * "all" is an alias for all five.
34dc7c2f 1160 * -s Set of sources to allow. One of
428870ff
BB
1161 * "local,default,inherited,received,temporary,none". Default is
1162 * all six.
34dc7c2f
BB
1163 * -p Display values in parsable (literal) format.
1164 *
1165 * Prints properties for the given datasets. The user can control which
1166 * columns to display as well as which property types to allow.
1167 */
1168
1169/*
1170 * Invoked to display the properties for a single dataset.
1171 */
1172static int
1173get_callback(zfs_handle_t *zhp, void *data)
1174{
1175 char buf[ZFS_MAXPROPLEN];
428870ff 1176 char rbuf[ZFS_MAXPROPLEN];
34dc7c2f
BB
1177 zprop_source_t sourcetype;
1178 char source[ZFS_MAXNAMELEN];
1179 zprop_get_cbdata_t *cbp = data;
428870ff 1180 nvlist_t *user_props = zfs_get_user_props(zhp);
34dc7c2f
BB
1181 zprop_list_t *pl = cbp->cb_proplist;
1182 nvlist_t *propval;
1183 char *strval;
1184 char *sourceval;
428870ff 1185 boolean_t received = is_recvd_column(cbp);
34dc7c2f
BB
1186
1187 for (; pl != NULL; pl = pl->pl_next) {
428870ff 1188 char *recvdval = NULL;
34dc7c2f
BB
1189 /*
1190 * Skip the special fake placeholder. This will also skip over
1191 * the name property when 'all' is specified.
1192 */
1193 if (pl->pl_prop == ZFS_PROP_NAME &&
1194 pl == cbp->cb_proplist)
1195 continue;
1196
1197 if (pl->pl_prop != ZPROP_INVAL) {
1198 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1199 sizeof (buf), &sourcetype, source,
1200 sizeof (source),
1201 cbp->cb_literal) != 0) {
1202 if (pl->pl_all)
1203 continue;
1204 if (!zfs_prop_valid_for_type(pl->pl_prop,
1205 ZFS_TYPE_DATASET)) {
1206 (void) fprintf(stderr,
1207 gettext("No such property '%s'\n"),
1208 zfs_prop_to_name(pl->pl_prop));
1209 continue;
1210 }
1211 sourcetype = ZPROP_SRC_NONE;
1212 (void) strlcpy(buf, "-", sizeof (buf));
1213 }
1214
428870ff
BB
1215 if (received && (zfs_prop_get_recvd(zhp,
1216 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1217 cbp->cb_literal) == 0))
1218 recvdval = rbuf;
1219
34dc7c2f
BB
1220 zprop_print_one_property(zfs_get_name(zhp), cbp,
1221 zfs_prop_to_name(pl->pl_prop),
428870ff 1222 buf, sourcetype, source, recvdval);
9babb374
BB
1223 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1224 sourcetype = ZPROP_SRC_LOCAL;
1225
1226 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1227 buf, sizeof (buf), cbp->cb_literal) != 0) {
1228 sourcetype = ZPROP_SRC_NONE;
1229 (void) strlcpy(buf, "-", sizeof (buf));
1230 }
1231
1232 zprop_print_one_property(zfs_get_name(zhp), cbp,
428870ff 1233 pl->pl_user_prop, buf, sourcetype, source, NULL);
34dc7c2f 1234 } else {
428870ff 1235 if (nvlist_lookup_nvlist(user_props,
34dc7c2f
BB
1236 pl->pl_user_prop, &propval) != 0) {
1237 if (pl->pl_all)
1238 continue;
1239 sourcetype = ZPROP_SRC_NONE;
1240 strval = "-";
1241 } else {
1242 verify(nvlist_lookup_string(propval,
1243 ZPROP_VALUE, &strval) == 0);
1244 verify(nvlist_lookup_string(propval,
1245 ZPROP_SOURCE, &sourceval) == 0);
1246
1247 if (strcmp(sourceval,
1248 zfs_get_name(zhp)) == 0) {
1249 sourcetype = ZPROP_SRC_LOCAL;
428870ff
BB
1250 } else if (strcmp(sourceval,
1251 ZPROP_SOURCE_VAL_RECVD) == 0) {
1252 sourcetype = ZPROP_SRC_RECEIVED;
34dc7c2f
BB
1253 } else {
1254 sourcetype = ZPROP_SRC_INHERITED;
1255 (void) strlcpy(source,
1256 sourceval, sizeof (source));
1257 }
1258 }
1259
428870ff
BB
1260 if (received && (zfs_prop_get_recvd(zhp,
1261 pl->pl_user_prop, rbuf, sizeof (rbuf),
1262 cbp->cb_literal) == 0))
1263 recvdval = rbuf;
1264
34dc7c2f
BB
1265 zprop_print_one_property(zfs_get_name(zhp), cbp,
1266 pl->pl_user_prop, strval, sourcetype,
428870ff 1267 source, recvdval);
34dc7c2f
BB
1268 }
1269 }
1270
1271 return (0);
1272}
1273
1274static int
1275zfs_do_get(int argc, char **argv)
1276{
1277 zprop_get_cbdata_t cb = { 0 };
bb939d10 1278 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
34dc7c2f
BB
1279 char *value, *fields;
1280 int ret;
9babb374 1281 int limit = 0;
34dc7c2f
BB
1282 zprop_list_t fake_name = { 0 };
1283
1284 /*
1285 * Set up default columns and sources.
1286 */
1287 cb.cb_sources = ZPROP_SRC_ALL;
1288 cb.cb_columns[0] = GET_COL_NAME;
1289 cb.cb_columns[1] = GET_COL_PROPERTY;
1290 cb.cb_columns[2] = GET_COL_VALUE;
1291 cb.cb_columns[3] = GET_COL_SOURCE;
1292 cb.cb_type = ZFS_TYPE_DATASET;
1293
1294 /* check options */
9babb374 1295 while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
34dc7c2f
BB
1296 switch (c) {
1297 case 'p':
1298 cb.cb_literal = B_TRUE;
1299 break;
9babb374
BB
1300 case 'd':
1301 limit = parse_depth(optarg, &flags);
1302 break;
34dc7c2f 1303 case 'r':
b128c09f 1304 flags |= ZFS_ITER_RECURSE;
34dc7c2f
BB
1305 break;
1306 case 'H':
1307 cb.cb_scripted = B_TRUE;
1308 break;
1309 case ':':
1310 (void) fprintf(stderr, gettext("missing argument for "
1311 "'%c' option\n"), optopt);
1312 usage(B_FALSE);
1313 break;
1314 case 'o':
1315 /*
1316 * Process the set of columns to display. We zero out
1317 * the structure to give us a blank slate.
1318 */
1319 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1320 i = 0;
1321 while (*optarg != '\0') {
1322 static char *col_subopts[] =
428870ff
BB
1323 { "name", "property", "value", "received",
1324 "source", "all", NULL };
34dc7c2f 1325
428870ff 1326 if (i == ZFS_GET_NCOLS) {
34dc7c2f
BB
1327 (void) fprintf(stderr, gettext("too "
1328 "many fields given to -o "
1329 "option\n"));
1330 usage(B_FALSE);
1331 }
1332
1333 switch (getsubopt(&optarg, col_subopts,
1334 &value)) {
1335 case 0:
1336 cb.cb_columns[i++] = GET_COL_NAME;
1337 break;
1338 case 1:
1339 cb.cb_columns[i++] = GET_COL_PROPERTY;
1340 break;
1341 case 2:
1342 cb.cb_columns[i++] = GET_COL_VALUE;
1343 break;
1344 case 3:
428870ff
BB
1345 cb.cb_columns[i++] = GET_COL_RECVD;
1346 flags |= ZFS_ITER_RECVD_PROPS;
1347 break;
1348 case 4:
34dc7c2f
BB
1349 cb.cb_columns[i++] = GET_COL_SOURCE;
1350 break;
428870ff
BB
1351 case 5:
1352 if (i > 0) {
1353 (void) fprintf(stderr,
1354 gettext("\"all\" conflicts "
1355 "with specific fields "
1356 "given to -o option\n"));
1357 usage(B_FALSE);
1358 }
1359 cb.cb_columns[0] = GET_COL_NAME;
1360 cb.cb_columns[1] = GET_COL_PROPERTY;
1361 cb.cb_columns[2] = GET_COL_VALUE;
1362 cb.cb_columns[3] = GET_COL_RECVD;
1363 cb.cb_columns[4] = GET_COL_SOURCE;
1364 flags |= ZFS_ITER_RECVD_PROPS;
1365 i = ZFS_GET_NCOLS;
1366 break;
34dc7c2f
BB
1367 default:
1368 (void) fprintf(stderr,
1369 gettext("invalid column name "
1370 "'%s'\n"), value);
1371 usage(B_FALSE);
1372 }
1373 }
1374 break;
1375
1376 case 's':
1377 cb.cb_sources = 0;
1378 while (*optarg != '\0') {
1379 static char *source_subopts[] = {
1380 "local", "default", "inherited",
428870ff
BB
1381 "received", "temporary", "none",
1382 NULL };
34dc7c2f
BB
1383
1384 switch (getsubopt(&optarg, source_subopts,
1385 &value)) {
1386 case 0:
1387 cb.cb_sources |= ZPROP_SRC_LOCAL;
1388 break;
1389 case 1:
1390 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1391 break;
1392 case 2:
1393 cb.cb_sources |= ZPROP_SRC_INHERITED;
1394 break;
1395 case 3:
428870ff 1396 cb.cb_sources |= ZPROP_SRC_RECEIVED;
34dc7c2f
BB
1397 break;
1398 case 4:
428870ff
BB
1399 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1400 break;
1401 case 5:
34dc7c2f
BB
1402 cb.cb_sources |= ZPROP_SRC_NONE;
1403 break;
1404 default:
1405 (void) fprintf(stderr,
1406 gettext("invalid source "
1407 "'%s'\n"), value);
1408 usage(B_FALSE);
1409 }
1410 }
1411 break;
1412
1413 case '?':
1414 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1415 optopt);
1416 usage(B_FALSE);
1417 }
1418 }
1419
1420 argc -= optind;
1421 argv += optind;
1422
1423 if (argc < 1) {
1424 (void) fprintf(stderr, gettext("missing property "
1425 "argument\n"));
1426 usage(B_FALSE);
1427 }
1428
1429 fields = argv[0];
1430
1431 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1432 != 0)
1433 usage(B_FALSE);
1434
1435 argc--;
1436 argv++;
1437
1438 /*
1439 * As part of zfs_expand_proplist(), we keep track of the maximum column
1440 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1441 * need to know the maximum name length. However, the user likely did
1442 * not specify 'name' as one of the properties to fetch, so we need to
1443 * make sure we always include at least this property for
1444 * print_get_headers() to work properly.
1445 */
1446 if (cb.cb_proplist != NULL) {
1447 fake_name.pl_prop = ZFS_PROP_NAME;
1448 fake_name.pl_width = strlen(gettext("NAME"));
1449 fake_name.pl_next = cb.cb_proplist;
1450 cb.cb_proplist = &fake_name;
1451 }
1452
1453 cb.cb_first = B_TRUE;
1454
1455 /* run for each object */
b128c09f 1456 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
9babb374 1457 &cb.cb_proplist, limit, get_callback, &cb);
34dc7c2f
BB
1458
1459 if (cb.cb_proplist == &fake_name)
1460 zprop_free_list(fake_name.pl_next);
1461 else
1462 zprop_free_list(cb.cb_proplist);
1463
1464 return (ret);
1465}
1466
1467/*
428870ff 1468 * inherit [-rS] <property> <fs|vol> ...
34dc7c2f 1469 *
428870ff
BB
1470 * -r Recurse over all children
1471 * -S Revert to received value, if any
34dc7c2f
BB
1472 *
1473 * For each dataset specified on the command line, inherit the given property
1474 * from its parent. Inheriting a property at the pool level will cause it to
1475 * use the default value. The '-r' flag will recurse over all children, and is
1476 * useful for setting a property on a hierarchy-wide basis, regardless of any
1477 * local modifications for each dataset.
1478 */
1479
428870ff
BB
1480typedef struct inherit_cbdata {
1481 const char *cb_propname;
1482 boolean_t cb_received;
1483} inherit_cbdata_t;
1484
34dc7c2f 1485static int
b128c09f 1486inherit_recurse_cb(zfs_handle_t *zhp, void *data)
34dc7c2f 1487{
428870ff
BB
1488 inherit_cbdata_t *cb = data;
1489 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
34dc7c2f 1490
b128c09f
BB
1491 /*
1492 * If we're doing it recursively, then ignore properties that
1493 * are not valid for this type of dataset.
1494 */
1495 if (prop != ZPROP_INVAL &&
1496 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1497 return (0);
1498
428870ff 1499 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
b128c09f
BB
1500}
1501
1502static int
1503inherit_cb(zfs_handle_t *zhp, void *data)
1504{
428870ff 1505 inherit_cbdata_t *cb = data;
b128c09f 1506
428870ff 1507 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
34dc7c2f
BB
1508}
1509
1510static int
1511zfs_do_inherit(int argc, char **argv)
1512{
34dc7c2f
BB
1513 int c;
1514 zfs_prop_t prop;
428870ff 1515 inherit_cbdata_t cb = { 0 };
34dc7c2f
BB
1516 char *propname;
1517 int ret;
b128c09f 1518 int flags = 0;
428870ff 1519 boolean_t received = B_FALSE;
34dc7c2f
BB
1520
1521 /* check options */
428870ff 1522 while ((c = getopt(argc, argv, "rS")) != -1) {
34dc7c2f
BB
1523 switch (c) {
1524 case 'r':
b128c09f 1525 flags |= ZFS_ITER_RECURSE;
34dc7c2f 1526 break;
428870ff
BB
1527 case 'S':
1528 received = B_TRUE;
1529 break;
34dc7c2f
BB
1530 case '?':
1531 default:
1532 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1533 optopt);
1534 usage(B_FALSE);
1535 }
1536 }
1537
1538 argc -= optind;
1539 argv += optind;
1540
1541 /* check number of arguments */
1542 if (argc < 1) {
1543 (void) fprintf(stderr, gettext("missing property argument\n"));
1544 usage(B_FALSE);
1545 }
1546 if (argc < 2) {
1547 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1548 usage(B_FALSE);
1549 }
1550
1551 propname = argv[0];
1552 argc--;
1553 argv++;
1554
1555 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1556 if (zfs_prop_readonly(prop)) {
1557 (void) fprintf(stderr, gettext(
1558 "%s property is read-only\n"),
1559 propname);
1560 return (1);
1561 }
428870ff 1562 if (!zfs_prop_inheritable(prop) && !received) {
34dc7c2f
BB
1563 (void) fprintf(stderr, gettext("'%s' property cannot "
1564 "be inherited\n"), propname);
1565 if (prop == ZFS_PROP_QUOTA ||
1566 prop == ZFS_PROP_RESERVATION ||
1567 prop == ZFS_PROP_REFQUOTA ||
1568 prop == ZFS_PROP_REFRESERVATION)
1569 (void) fprintf(stderr, gettext("use 'zfs set "
1570 "%s=none' to clear\n"), propname);
1571 return (1);
1572 }
428870ff
BB
1573 if (received && (prop == ZFS_PROP_VOLSIZE ||
1574 prop == ZFS_PROP_VERSION)) {
1575 (void) fprintf(stderr, gettext("'%s' property cannot "
1576 "be reverted to a received value\n"), propname);
1577 return (1);
1578 }
34dc7c2f
BB
1579 } else if (!zfs_prop_user(propname)) {
1580 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1581 propname);
1582 usage(B_FALSE);
1583 }
1584
428870ff
BB
1585 cb.cb_propname = propname;
1586 cb.cb_received = received;
1587
b128c09f
BB
1588 if (flags & ZFS_ITER_RECURSE) {
1589 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
428870ff 1590 NULL, NULL, 0, inherit_recurse_cb, &cb);
b128c09f
BB
1591 } else {
1592 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
428870ff 1593 NULL, NULL, 0, inherit_cb, &cb);
b128c09f 1594 }
34dc7c2f
BB
1595
1596 return (ret);
1597}
1598
1599typedef struct upgrade_cbdata {
1600 uint64_t cb_numupgraded;
1601 uint64_t cb_numsamegraded;
1602 uint64_t cb_numfailed;
1603 uint64_t cb_version;
1604 boolean_t cb_newer;
1605 boolean_t cb_foundone;
1606 char cb_lastfs[ZFS_MAXNAMELEN];
1607} upgrade_cbdata_t;
1608
1609static int
1610same_pool(zfs_handle_t *zhp, const char *name)
1611{
1612 int len1 = strcspn(name, "/@");
1613 const char *zhname = zfs_get_name(zhp);
1614 int len2 = strcspn(zhname, "/@");
1615
1616 if (len1 != len2)
1617 return (B_FALSE);
1618 return (strncmp(name, zhname, len1) == 0);
1619}
1620
1621static int
1622upgrade_list_callback(zfs_handle_t *zhp, void *data)
1623{
1624 upgrade_cbdata_t *cb = data;
1625 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1626
1627 /* list if it's old/new */
1628 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1629 (cb->cb_newer && version > ZPL_VERSION)) {
1630 char *str;
1631 if (cb->cb_newer) {
1632 str = gettext("The following filesystems are "
1633 "formatted using a newer software version and\n"
1634 "cannot be accessed on the current system.\n\n");
1635 } else {
1636 str = gettext("The following filesystems are "
1637 "out of date, and can be upgraded. After being\n"
1638 "upgraded, these filesystems (and any 'zfs send' "
1639 "streams generated from\n"
1640 "subsequent snapshots) will no longer be "
1641 "accessible by older software versions.\n\n");
1642 }
1643
1644 if (!cb->cb_foundone) {
1645 (void) puts(str);
1646 (void) printf(gettext("VER FILESYSTEM\n"));
1647 (void) printf(gettext("--- ------------\n"));
1648 cb->cb_foundone = B_TRUE;
1649 }
1650
1651 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1652 }
1653
1654 return (0);
1655}
1656
1657static int
1658upgrade_set_callback(zfs_handle_t *zhp, void *data)
1659{
1660 upgrade_cbdata_t *cb = data;
1661 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
428870ff
BB
1662 int needed_spa_version;
1663 int spa_version;
1664
1665 if (zfs_spa_version(zhp, &spa_version) < 0)
1666 return (-1);
1667
1668 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1669
1670 if (needed_spa_version < 0)
1671 return (-1);
1672
1673 if (spa_version < needed_spa_version) {
1674 /* can't upgrade */
1675 (void) printf(gettext("%s: can not be "
1676 "upgraded; the pool version needs to first "
1677 "be upgraded\nto version %d\n\n"),
1678 zfs_get_name(zhp), needed_spa_version);
1679 cb->cb_numfailed++;
1680 return (0);
34dc7c2f
BB
1681 }
1682
1683 /* upgrade */
1684 if (version < cb->cb_version) {
1685 char verstr[16];
1686 (void) snprintf(verstr, sizeof (verstr),
b8864a23 1687 "%llu", (u_longlong_t)cb->cb_version);
34dc7c2f
BB
1688 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1689 /*
1690 * If they did "zfs upgrade -a", then we could
1691 * be doing ioctls to different pools. We need
1692 * to log this history once to each pool.
1693 */
1694 verify(zpool_stage_history(g_zfs, history_str) == 0);
1695 }
1696 if (zfs_prop_set(zhp, "version", verstr) == 0)
1697 cb->cb_numupgraded++;
1698 else
1699 cb->cb_numfailed++;
1700 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1701 } else if (version > cb->cb_version) {
1702 /* can't downgrade */
1703 (void) printf(gettext("%s: can not be downgraded; "
1704 "it is already at version %u\n"),
1705 zfs_get_name(zhp), version);
1706 cb->cb_numfailed++;
1707 } else {
1708 cb->cb_numsamegraded++;
1709 }
1710 return (0);
1711}
1712
1713/*
1714 * zfs upgrade
1715 * zfs upgrade -v
1716 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1717 */
1718static int
1719zfs_do_upgrade(int argc, char **argv)
1720{
34dc7c2f
BB
1721 boolean_t all = B_FALSE;
1722 boolean_t showversions = B_FALSE;
1723 int ret;
1724 upgrade_cbdata_t cb = { 0 };
b8864a23 1725 signed char c;
b128c09f 1726 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
34dc7c2f
BB
1727
1728 /* check options */
1729 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1730 switch (c) {
1731 case 'r':
b128c09f 1732 flags |= ZFS_ITER_RECURSE;
34dc7c2f
BB
1733 break;
1734 case 'v':
1735 showversions = B_TRUE;
1736 break;
1737 case 'V':
1738 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1739 optarg, &cb.cb_version) != 0) {
1740 (void) fprintf(stderr,
1741 gettext("invalid version %s\n"), optarg);
1742 usage(B_FALSE);
1743 }
1744 break;
1745 case 'a':
1746 all = B_TRUE;
1747 break;
1748 case '?':
1749 default:
1750 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1751 optopt);
1752 usage(B_FALSE);
1753 }
1754 }
1755
1756 argc -= optind;
1757 argv += optind;
1758
b128c09f 1759 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
34dc7c2f 1760 usage(B_FALSE);
b128c09f
BB
1761 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1762 cb.cb_version || argc))
34dc7c2f
BB
1763 usage(B_FALSE);
1764 if ((all || argc) && (showversions))
1765 usage(B_FALSE);
1766 if (all && argc)
1767 usage(B_FALSE);
1768
1769 if (showversions) {
1770 /* Show info on available versions. */
1771 (void) printf(gettext("The following filesystem versions are "
1772 "supported:\n\n"));
1773 (void) printf(gettext("VER DESCRIPTION\n"));
1774 (void) printf("--- -----------------------------------------"
1775 "---------------\n");
1776 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1777 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1778 (void) printf(gettext(" 3 Case insensitive and File system "
45d1cae3 1779 "unique identifier (FUID)\n"));
9babb374
BB
1780 (void) printf(gettext(" 4 userquota, groupquota "
1781 "properties\n"));
428870ff 1782 (void) printf(gettext(" 5 System attributes\n"));
34dc7c2f 1783 (void) printf(gettext("\nFor more information on a particular "
428870ff
BB
1784 "version, including supported releases,\n"));
1785 (void) printf("see the ZFS Administration Guide.\n\n");
34dc7c2f
BB
1786 ret = 0;
1787 } else if (argc || all) {
1788 /* Upgrade filesystems */
1789 if (cb.cb_version == 0)
1790 cb.cb_version = ZPL_VERSION;
b128c09f 1791 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
9babb374 1792 NULL, NULL, 0, upgrade_set_callback, &cb);
34dc7c2f 1793 (void) printf(gettext("%llu filesystems upgraded\n"),
b8864a23 1794 (u_longlong_t)cb.cb_numupgraded);
34dc7c2f
BB
1795 if (cb.cb_numsamegraded) {
1796 (void) printf(gettext("%llu filesystems already at "
1797 "this version\n"),
b8864a23 1798 (u_longlong_t)cb.cb_numsamegraded);
34dc7c2f
BB
1799 }
1800 if (cb.cb_numfailed != 0)
1801 ret = 1;
1802 } else {
1803 /* List old-version filesytems */
1804 boolean_t found;
1805 (void) printf(gettext("This system is currently running "
1806 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1807
b128c09f
BB
1808 flags |= ZFS_ITER_RECURSE;
1809 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
9babb374 1810 NULL, NULL, 0, upgrade_list_callback, &cb);
34dc7c2f
BB
1811
1812 found = cb.cb_foundone;
1813 cb.cb_foundone = B_FALSE;
1814 cb.cb_newer = B_TRUE;
1815
b128c09f 1816 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
9babb374 1817 NULL, NULL, 0, upgrade_list_callback, &cb);
34dc7c2f
BB
1818
1819 if (!cb.cb_foundone && !found) {
1820 (void) printf(gettext("All filesystems are "
1821 "formatted with the current version.\n"));
1822 }
1823 }
1824
1825 return (ret);
1826}
1827
1828/*
9babb374
BB
1829 * zfs userspace
1830 */
1831static int
1832userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
1833{
1834 zfs_userquota_prop_t *typep = arg;
1835 zfs_userquota_prop_t p = *typep;
1836 char *name = NULL;
1837 char *ug, *propname;
1838 char namebuf[32];
1839 char sizebuf[32];
1840
1841 if (domain == NULL || domain[0] == '\0') {
1842 if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) {
1843 struct group *g = getgrgid(rid);
1844 if (g)
1845 name = g->gr_name;
1846 } else {
1847 struct passwd *p = getpwuid(rid);
1848 if (p)
1849 name = p->pw_name;
1850 }
1851 }
1852
1853 if (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA)
1854 ug = "group";
1855 else
1856 ug = "user";
1857
1858 if (p == ZFS_PROP_USERUSED || p == ZFS_PROP_GROUPUSED)
1859 propname = "used";
1860 else
1861 propname = "quota";
1862
1863 if (name == NULL) {
1864 (void) snprintf(namebuf, sizeof (namebuf),
1865 "%llu", (longlong_t)rid);
1866 name = namebuf;
1867 }
1868 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
1869
1870 (void) printf("%s %s %s%c%s %s\n", propname, ug, domain,
1871 domain[0] ? '-' : ' ', name, sizebuf);
1872
1873 return (0);
1874}
1875
1876static int
1877zfs_do_userspace(int argc, char **argv)
1878{
1879 zfs_handle_t *zhp;
1880 zfs_userquota_prop_t p;
1881 int error;
1882
9babb374
BB
1883 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
1884 return (1);
1885
1886 (void) printf("PROP TYPE NAME VALUE\n");
1887
1888 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
1889 error = zfs_userspace(zhp, p, userspace_cb, &p);
1890 if (error)
1891 break;
1892 }
1893 return (error);
1894}
1895
1896/*
1897 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
34dc7c2f
BB
1898 * [-s property [-s property]...] [-S property [-S property]...]
1899 * <dataset> ...
1900 *
428870ff
BB
1901 * -r Recurse over all children
1902 * -d Limit recursion by depth.
1903 * -H Scripted mode; elide headers and separate columns by tabs
1904 * -o Control which fields to display.
1905 * -t Control which object types to display.
34dc7c2f
BB
1906 * -s Specify sort columns, descending order.
1907 * -S Specify sort columns, ascending order.
1908 *
1909 * When given no arguments, lists all filesystems in the system.
1910 * Otherwise, list the specified datasets, optionally recursing down them if
1911 * '-r' is specified.
1912 */
1913typedef struct list_cbdata {
1914 boolean_t cb_first;
1915 boolean_t cb_scripted;
1916 zprop_list_t *cb_proplist;
1917} list_cbdata_t;
1918
1919/*
1920 * Given a list of columns to display, output appropriate headers for each one.
1921 */
1922static void
1923print_header(zprop_list_t *pl)
1924{
1925 char headerbuf[ZFS_MAXPROPLEN];
1926 const char *header;
1927 int i;
1928 boolean_t first = B_TRUE;
1929 boolean_t right_justify;
1930
1931 for (; pl != NULL; pl = pl->pl_next) {
1932 if (!first) {
1933 (void) printf(" ");
1934 } else {
1935 first = B_FALSE;
1936 }
1937
1938 right_justify = B_FALSE;
1939 if (pl->pl_prop != ZPROP_INVAL) {
1940 header = zfs_prop_column_name(pl->pl_prop);
1941 right_justify = zfs_prop_align_right(pl->pl_prop);
1942 } else {
1943 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
1944 headerbuf[i] = toupper(pl->pl_user_prop[i]);
1945 headerbuf[i] = '\0';
1946 header = headerbuf;
1947 }
1948
1949 if (pl->pl_next == NULL && !right_justify)
1950 (void) printf("%s", header);
1951 else if (right_justify)
b8864a23 1952 (void) printf("%*s", (int)pl->pl_width, header);
34dc7c2f 1953 else
b8864a23 1954 (void) printf("%-*s", (int)pl->pl_width, header);
34dc7c2f
BB
1955 }
1956
1957 (void) printf("\n");
1958}
1959
1960/*
1961 * Given a dataset and a list of fields, print out all the properties according
1962 * to the described layout.
1963 */
1964static void
1965print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
1966{
1967 boolean_t first = B_TRUE;
1968 char property[ZFS_MAXPROPLEN];
1969 nvlist_t *userprops = zfs_get_user_props(zhp);
1970 nvlist_t *propval;
1971 char *propstr;
1972 boolean_t right_justify;
1973 int width;
1974
1975 for (; pl != NULL; pl = pl->pl_next) {
1976 if (!first) {
1977 if (scripted)
1978 (void) printf("\t");
1979 else
1980 (void) printf(" ");
1981 } else {
1982 first = B_FALSE;
1983 }
1984
34dc7c2f
BB
1985 if (pl->pl_prop != ZPROP_INVAL) {
1986 if (zfs_prop_get(zhp, pl->pl_prop, property,
1987 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
1988 propstr = "-";
1989 else
1990 propstr = property;
1991
1992 right_justify = zfs_prop_align_right(pl->pl_prop);
9babb374
BB
1993 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1994 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1995 property, sizeof (property), B_FALSE) != 0)
1996 propstr = "-";
1997 else
1998 propstr = property;
1999 right_justify = B_TRUE;
34dc7c2f
BB
2000 } else {
2001 if (nvlist_lookup_nvlist(userprops,
2002 pl->pl_user_prop, &propval) != 0)
2003 propstr = "-";
2004 else
2005 verify(nvlist_lookup_string(propval,
2006 ZPROP_VALUE, &propstr) == 0);
9babb374 2007 right_justify = B_FALSE;
34dc7c2f
BB
2008 }
2009
2010 width = pl->pl_width;
2011
2012 /*
2013 * If this is being called in scripted mode, or if this is the
2014 * last column and it is left-justified, don't include a width
2015 * format specifier.
2016 */
2017 if (scripted || (pl->pl_next == NULL && !right_justify))
2018 (void) printf("%s", propstr);
2019 else if (right_justify)
2020 (void) printf("%*s", width, propstr);
2021 else
2022 (void) printf("%-*s", width, propstr);
2023 }
2024
2025 (void) printf("\n");
2026}
2027
2028/*
2029 * Generic callback function to list a dataset or snapshot.
2030 */
2031static int
2032list_callback(zfs_handle_t *zhp, void *data)
2033{
2034 list_cbdata_t *cbp = data;
2035
2036 if (cbp->cb_first) {
2037 if (!cbp->cb_scripted)
2038 print_header(cbp->cb_proplist);
2039 cbp->cb_first = B_FALSE;
2040 }
2041
2042 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2043
2044 return (0);
2045}
2046
2047static int
2048zfs_do_list(int argc, char **argv)
2049{
2050 int c;
34dc7c2f
BB
2051 boolean_t scripted = B_FALSE;
2052 static char default_fields[] =
2053 "name,used,available,referenced,mountpoint";
d164b209 2054 int types = ZFS_TYPE_DATASET;
b128c09f 2055 boolean_t types_specified = B_FALSE;
34dc7c2f 2056 char *fields = NULL;
34dc7c2f
BB
2057 list_cbdata_t cb = { 0 };
2058 char *value;
9babb374 2059 int limit = 0;
34dc7c2f 2060 int ret;
34dc7c2f 2061 zfs_sort_column_t *sortcol = NULL;
b128c09f 2062 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
34dc7c2f
BB
2063
2064 /* check options */
9babb374 2065 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
34dc7c2f
BB
2066 switch (c) {
2067 case 'o':
2068 fields = optarg;
2069 break;
9babb374
BB
2070 case 'd':
2071 limit = parse_depth(optarg, &flags);
2072 break;
34dc7c2f 2073 case 'r':
b128c09f 2074 flags |= ZFS_ITER_RECURSE;
34dc7c2f
BB
2075 break;
2076 case 'H':
2077 scripted = B_TRUE;
2078 break;
2079 case 's':
2080 if (zfs_add_sort_column(&sortcol, optarg,
2081 B_FALSE) != 0) {
2082 (void) fprintf(stderr,
2083 gettext("invalid property '%s'\n"), optarg);
2084 usage(B_FALSE);
2085 }
2086 break;
2087 case 'S':
2088 if (zfs_add_sort_column(&sortcol, optarg,
2089 B_TRUE) != 0) {
2090 (void) fprintf(stderr,
2091 gettext("invalid property '%s'\n"), optarg);
2092 usage(B_FALSE);
2093 }
2094 break;
2095 case 't':
2096 types = 0;
b128c09f
BB
2097 types_specified = B_TRUE;
2098 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
34dc7c2f 2099 while (*optarg != '\0') {
b128c09f
BB
2100 static char *type_subopts[] = { "filesystem",
2101 "volume", "snapshot", "all", NULL };
2102
34dc7c2f
BB
2103 switch (getsubopt(&optarg, type_subopts,
2104 &value)) {
2105 case 0:
2106 types |= ZFS_TYPE_FILESYSTEM;
2107 break;
2108 case 1:
2109 types |= ZFS_TYPE_VOLUME;
2110 break;
2111 case 2:
2112 types |= ZFS_TYPE_SNAPSHOT;
2113 break;
b128c09f
BB
2114 case 3:
2115 types = ZFS_TYPE_DATASET;
2116 break;
2117
34dc7c2f
BB
2118 default:
2119 (void) fprintf(stderr,
2120 gettext("invalid type '%s'\n"),
2121 value);
2122 usage(B_FALSE);
2123 }
2124 }
2125 break;
2126 case ':':
2127 (void) fprintf(stderr, gettext("missing argument for "
2128 "'%c' option\n"), optopt);
2129 usage(B_FALSE);
2130 break;
2131 case '?':
2132 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2133 optopt);
2134 usage(B_FALSE);
2135 }
2136 }
2137
2138 argc -= optind;
2139 argv += optind;
2140
2141 if (fields == NULL)
b128c09f
BB
2142 fields = default_fields;
2143
2144 /*
2145 * If "-o space" and no types were specified, don't display snapshots.
2146 */
2147 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
2148 types &= ~ZFS_TYPE_SNAPSHOT;
34dc7c2f
BB
2149
2150 /*
2151 * If the user specifies '-o all', the zprop_get_list() doesn't
2152 * normally include the name of the dataset. For 'zfs list', we always
2153 * want this property to be first.
2154 */
2155 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2156 != 0)
2157 usage(B_FALSE);
2158
2159 cb.cb_scripted = scripted;
2160 cb.cb_first = B_TRUE;
2161
b128c09f 2162 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
9babb374 2163 limit, list_callback, &cb);
34dc7c2f
BB
2164
2165 zprop_free_list(cb.cb_proplist);
2166 zfs_free_sort_columns(sortcol);
2167
2168 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
2169 (void) printf(gettext("no datasets available\n"));
2170
2171 return (ret);
2172}
2173
2174/*
2175 * zfs rename <fs | snap | vol> <fs | snap | vol>
2176 * zfs rename -p <fs | vol> <fs | vol>
2177 * zfs rename -r <snap> <snap>
2178 *
2179 * Renames the given dataset to another of the same type.
2180 *
2181 * The '-p' flag creates all the non-existing ancestors of the target first.
2182 */
2183/* ARGSUSED */
2184static int
2185zfs_do_rename(int argc, char **argv)
2186{
2187 zfs_handle_t *zhp;
2188 int c;
2189 int ret;
2190 boolean_t recurse = B_FALSE;
2191 boolean_t parents = B_FALSE;
2192
2193 /* check options */
2194 while ((c = getopt(argc, argv, "pr")) != -1) {
2195 switch (c) {
2196 case 'p':
2197 parents = B_TRUE;
2198 break;
2199 case 'r':
2200 recurse = B_TRUE;
2201 break;
2202 case '?':
2203 default:
2204 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2205 optopt);
2206 usage(B_FALSE);
2207 }
2208 }
2209
2210 argc -= optind;
2211 argv += optind;
2212
2213 /* check number of arguments */
2214 if (argc < 1) {
2215 (void) fprintf(stderr, gettext("missing source dataset "
2216 "argument\n"));
2217 usage(B_FALSE);
2218 }
2219 if (argc < 2) {
2220 (void) fprintf(stderr, gettext("missing target dataset "
2221 "argument\n"));
2222 usage(B_FALSE);
2223 }
2224 if (argc > 2) {
2225 (void) fprintf(stderr, gettext("too many arguments\n"));
2226 usage(B_FALSE);
2227 }
2228
2229 if (recurse && parents) {
2230 (void) fprintf(stderr, gettext("-p and -r options are mutually "
2231 "exclusive\n"));
2232 usage(B_FALSE);
2233 }
2234
2235 if (recurse && strchr(argv[0], '@') == 0) {
2236 (void) fprintf(stderr, gettext("source dataset for recursive "
2237 "rename must be a snapshot\n"));
2238 usage(B_FALSE);
2239 }
2240
2241 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
2242 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
2243 return (1);
2244
2245 /* If we were asked and the name looks good, try to create ancestors. */
2246 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
2247 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
2248 zfs_close(zhp);
2249 return (1);
2250 }
2251
2252 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
2253
2254 zfs_close(zhp);
2255 return (ret);
2256}
2257
2258/*
2259 * zfs promote <fs>
2260 *
2261 * Promotes the given clone fs to be the parent
2262 */
2263/* ARGSUSED */
2264static int
2265zfs_do_promote(int argc, char **argv)
2266{
2267 zfs_handle_t *zhp;
2268 int ret;
2269
2270 /* check options */
2271 if (argc > 1 && argv[1][0] == '-') {
2272 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2273 argv[1][1]);
2274 usage(B_FALSE);
2275 }
2276
2277 /* check number of arguments */
2278 if (argc < 2) {
2279 (void) fprintf(stderr, gettext("missing clone filesystem"
2280 " argument\n"));
2281 usage(B_FALSE);
2282 }
2283 if (argc > 2) {
2284 (void) fprintf(stderr, gettext("too many arguments\n"));
2285 usage(B_FALSE);
2286 }
2287
2288 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2289 if (zhp == NULL)
2290 return (1);
2291
2292 ret = (zfs_promote(zhp) != 0);
2293
2294
2295 zfs_close(zhp);
2296 return (ret);
2297}
2298
2299/*
2300 * zfs rollback [-rRf] <snapshot>
2301 *
428870ff
BB
2302 * -r Delete any intervening snapshots before doing rollback
2303 * -R Delete any snapshots and their clones
2304 * -f ignored for backwards compatability
34dc7c2f
BB
2305 *
2306 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2307 * since then and making it the active dataset. If more recent snapshots exist,
2308 * the command will complain unless the '-r' flag is given.
2309 */
2310typedef struct rollback_cbdata {
2311 uint64_t cb_create;
2312 boolean_t cb_first;
2313 int cb_doclones;
2314 char *cb_target;
2315 int cb_error;
2316 boolean_t cb_recurse;
2317 boolean_t cb_dependent;
2318} rollback_cbdata_t;
2319
2320/*
2321 * Report any snapshots more recent than the one specified. Used when '-r' is
2322 * not specified. We reuse this same callback for the snapshot dependents - if
2323 * 'cb_dependent' is set, then this is a dependent and we should report it
2324 * without checking the transaction group.
2325 */
2326static int
2327rollback_check(zfs_handle_t *zhp, void *data)
2328{
2329 rollback_cbdata_t *cbp = data;
2330
2331 if (cbp->cb_doclones) {
2332 zfs_close(zhp);
2333 return (0);
2334 }
2335
2336 if (!cbp->cb_dependent) {
2337 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
2338 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2339 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
2340 cbp->cb_create) {
2341
2342 if (cbp->cb_first && !cbp->cb_recurse) {
2343 (void) fprintf(stderr, gettext("cannot "
2344 "rollback to '%s': more recent snapshots "
2345 "exist\n"),
2346 cbp->cb_target);
2347 (void) fprintf(stderr, gettext("use '-r' to "
2348 "force deletion of the following "
2349 "snapshots:\n"));
2350 cbp->cb_first = 0;
2351 cbp->cb_error = 1;
2352 }
2353
2354 if (cbp->cb_recurse) {
2355 cbp->cb_dependent = B_TRUE;
2356 if (zfs_iter_dependents(zhp, B_TRUE,
2357 rollback_check, cbp) != 0) {
2358 zfs_close(zhp);
2359 return (-1);
2360 }
2361 cbp->cb_dependent = B_FALSE;
2362 } else {
2363 (void) fprintf(stderr, "%s\n",
2364 zfs_get_name(zhp));
2365 }
2366 }
2367 } else {
2368 if (cbp->cb_first && cbp->cb_recurse) {
2369 (void) fprintf(stderr, gettext("cannot rollback to "
2370 "'%s': clones of previous snapshots exist\n"),
2371 cbp->cb_target);
2372 (void) fprintf(stderr, gettext("use '-R' to "
2373 "force deletion of the following clones and "
2374 "dependents:\n"));
2375 cbp->cb_first = 0;
2376 cbp->cb_error = 1;
2377 }
2378
2379 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
2380 }
2381
2382 zfs_close(zhp);
2383 return (0);
2384}
2385
2386static int
2387zfs_do_rollback(int argc, char **argv)
2388{
2389 int ret;
2390 int c;
2391 boolean_t force = B_FALSE;
2392 rollback_cbdata_t cb = { 0 };
2393 zfs_handle_t *zhp, *snap;
2394 char parentname[ZFS_MAXNAMELEN];
2395 char *delim;
2396
2397 /* check options */
2398 while ((c = getopt(argc, argv, "rRf")) != -1) {
2399 switch (c) {
2400 case 'r':
2401 cb.cb_recurse = 1;
2402 break;
2403 case 'R':
2404 cb.cb_recurse = 1;
2405 cb.cb_doclones = 1;
2406 break;
2407 case 'f':
2408 force = B_TRUE;
2409 break;
2410 case '?':
2411 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2412 optopt);
2413 usage(B_FALSE);
2414 }
2415 }
2416
2417 argc -= optind;
2418 argv += optind;
2419
2420 /* check number of arguments */
2421 if (argc < 1) {
2422 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2423 usage(B_FALSE);
2424 }
2425 if (argc > 1) {
2426 (void) fprintf(stderr, gettext("too many arguments\n"));
2427 usage(B_FALSE);
2428 }
2429
2430 /* open the snapshot */
2431 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
2432 return (1);
2433
2434 /* open the parent dataset */
2435 (void) strlcpy(parentname, argv[0], sizeof (parentname));
2436 verify((delim = strrchr(parentname, '@')) != NULL);
2437 *delim = '\0';
2438 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
2439 zfs_close(snap);
2440 return (1);
2441 }
2442
2443 /*
2444 * Check for more recent snapshots and/or clones based on the presence
2445 * of '-r' and '-R'.
2446 */
2447 cb.cb_target = argv[0];
2448 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2449 cb.cb_first = B_TRUE;
2450 cb.cb_error = 0;
2451 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
2452 goto out;
2453
2454 if ((ret = cb.cb_error) != 0)
2455 goto out;
2456
2457 /*
2458 * Rollback parent to the given snapshot.
2459 */
2460 ret = zfs_rollback(zhp, snap, force);
2461
2462out:
2463 zfs_close(snap);
2464 zfs_close(zhp);
2465
2466 if (ret == 0)
2467 return (0);
2468 else
2469 return (1);
2470}
2471
2472/*
2473 * zfs set property=value { fs | snap | vol } ...
2474 *
2475 * Sets the given property for all datasets specified on the command line.
2476 */
2477typedef struct set_cbdata {
2478 char *cb_propname;
2479 char *cb_value;
2480} set_cbdata_t;
2481
2482static int
2483set_callback(zfs_handle_t *zhp, void *data)
2484{
2485 set_cbdata_t *cbp = data;
2486
2487 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2488 switch (libzfs_errno(g_zfs)) {
2489 case EZFS_MOUNTFAILED:
2490 (void) fprintf(stderr, gettext("property may be set "
2491 "but unable to remount filesystem\n"));
2492 break;
2493 case EZFS_SHARENFSFAILED:
2494 (void) fprintf(stderr, gettext("property may be set "
2495 "but unable to reshare filesystem\n"));
2496 break;
2497 }
2498 return (1);
2499 }
2500 return (0);
2501}
2502
2503static int
2504zfs_do_set(int argc, char **argv)
2505{
2506 set_cbdata_t cb;
2507 int ret;
2508
2509 /* check for options */
2510 if (argc > 1 && argv[1][0] == '-') {
2511 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2512 argv[1][1]);
2513 usage(B_FALSE);
2514 }
2515
2516 /* check number of arguments */
2517 if (argc < 2) {
2518 (void) fprintf(stderr, gettext("missing property=value "
2519 "argument\n"));
2520 usage(B_FALSE);
2521 }
2522 if (argc < 3) {
2523 (void) fprintf(stderr, gettext("missing dataset name\n"));
2524 usage(B_FALSE);
2525 }
2526
2527 /* validate property=value argument */
2528 cb.cb_propname = argv[1];
b128c09f
BB
2529 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
2530 (cb.cb_value[1] == '\0')) {
34dc7c2f
BB
2531 (void) fprintf(stderr, gettext("missing value in "
2532 "property=value argument\n"));
2533 usage(B_FALSE);
2534 }
2535
2536 *cb.cb_value = '\0';
2537 cb.cb_value++;
2538
2539 if (*cb.cb_propname == '\0') {
2540 (void) fprintf(stderr,
2541 gettext("missing property in property=value argument\n"));
2542 usage(B_FALSE);
2543 }
2544
b8864a23 2545 ret = zfs_for_each(argc - 2, argv + 2, 0,
9babb374 2546 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
34dc7c2f
BB
2547
2548 return (ret);
2549}
2550
2551/*
b128c09f 2552 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
34dc7c2f
BB
2553 *
2554 * Creates a snapshot with the given name. While functionally equivalent to
2555 * 'zfs create', it is a separate command to differentiate intent.
2556 */
2557static int
2558zfs_do_snapshot(int argc, char **argv)
2559{
2560 boolean_t recursive = B_FALSE;
2561 int ret;
b8864a23 2562 signed char c;
b128c09f
BB
2563 nvlist_t *props;
2564
428870ff
BB
2565 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2566 nomem();
34dc7c2f
BB
2567
2568 /* check options */
b128c09f 2569 while ((c = getopt(argc, argv, "ro:")) != -1) {
34dc7c2f 2570 switch (c) {
b128c09f
BB
2571 case 'o':
2572 if (parseprop(props))
2573 return (1);
2574 break;
34dc7c2f
BB
2575 case 'r':
2576 recursive = B_TRUE;
2577 break;
2578 case '?':
2579 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2580 optopt);
b128c09f 2581 goto usage;
34dc7c2f
BB
2582 }
2583 }
2584
2585 argc -= optind;
2586 argv += optind;
2587
2588 /* check number of arguments */
2589 if (argc < 1) {
2590 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
b128c09f 2591 goto usage;
34dc7c2f
BB
2592 }
2593 if (argc > 1) {
2594 (void) fprintf(stderr, gettext("too many arguments\n"));
b128c09f 2595 goto usage;
34dc7c2f
BB
2596 }
2597
b128c09f
BB
2598 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
2599 nvlist_free(props);
34dc7c2f
BB
2600 if (ret && recursive)
2601 (void) fprintf(stderr, gettext("no snapshots were created\n"));
2602 return (ret != 0);
b128c09f
BB
2603
2604usage:
2605 nvlist_free(props);
2606 usage(B_FALSE);
2607 return (-1);
34dc7c2f
BB
2608}
2609
2610/*
428870ff
BB
2611 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
2612 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
34dc7c2f
BB
2613 *
2614 * Send a backup stream to stdout.
2615 */
2616static int
2617zfs_do_send(int argc, char **argv)
2618{
2619 char *fromname = NULL;
2620 char *toname = NULL;
2621 char *cp;
2622 zfs_handle_t *zhp;
428870ff 2623 sendflags_t flags = { 0 };
34dc7c2f 2624 int c, err;
428870ff
BB
2625 nvlist_t *dbgnv;
2626 boolean_t extraverbose = B_FALSE;
34dc7c2f
BB
2627
2628 /* check options */
428870ff 2629 while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) {
34dc7c2f
BB
2630 switch (c) {
2631 case 'i':
2632 if (fromname)
2633 usage(B_FALSE);
2634 fromname = optarg;
2635 break;
2636 case 'I':
2637 if (fromname)
2638 usage(B_FALSE);
2639 fromname = optarg;
428870ff 2640 flags.doall = B_TRUE;
34dc7c2f
BB
2641 break;
2642 case 'R':
428870ff
BB
2643 flags.replicate = B_TRUE;
2644 break;
2645 case 'p':
2646 flags.props = B_TRUE;
34dc7c2f
BB
2647 break;
2648 case 'v':
428870ff
BB
2649 if (flags.verbose)
2650 extraverbose = B_TRUE;
2651 flags.verbose = B_TRUE;
2652 break;
2653 case 'D':
2654 flags.dedup = B_TRUE;
34dc7c2f
BB
2655 break;
2656 case ':':
2657 (void) fprintf(stderr, gettext("missing argument for "
2658 "'%c' option\n"), optopt);
2659 usage(B_FALSE);
2660 break;
2661 case '?':
2662 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2663 optopt);
2664 usage(B_FALSE);
2665 }
2666 }
2667
2668 argc -= optind;
2669 argv += optind;
2670
2671 /* check number of arguments */
2672 if (argc < 1) {
2673 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2674 usage(B_FALSE);
2675 }
2676 if (argc > 1) {
2677 (void) fprintf(stderr, gettext("too many arguments\n"));
2678 usage(B_FALSE);
2679 }
2680
2681 if (isatty(STDOUT_FILENO)) {
2682 (void) fprintf(stderr,
2683 gettext("Error: Stream can not be written to a terminal.\n"
2684 "You must redirect standard output.\n"));
2685 return (1);
2686 }
2687
2688 cp = strchr(argv[0], '@');
2689 if (cp == NULL) {
2690 (void) fprintf(stderr,
2691 gettext("argument must be a snapshot\n"));
2692 usage(B_FALSE);
2693 }
2694 *cp = '\0';
2695 toname = cp + 1;
2696 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2697 if (zhp == NULL)
2698 return (1);
2699
2700 /*
2701 * If they specified the full path to the snapshot, chop off
2702 * everything except the short name of the snapshot, but special
2703 * case if they specify the origin.
2704 */
2705 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
2706 char origin[ZFS_MAXNAMELEN];
2707 zprop_source_t src;
2708
2709 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
2710 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
2711
2712 if (strcmp(origin, fromname) == 0) {
2713 fromname = NULL;
428870ff 2714 flags.fromorigin = B_TRUE;
34dc7c2f
BB
2715 } else {
2716 *cp = '\0';
2717 if (cp != fromname && strcmp(argv[0], fromname)) {
2718 (void) fprintf(stderr,
2719 gettext("incremental source must be "
2720 "in same filesystem\n"));
2721 usage(B_FALSE);
2722 }
2723 fromname = cp + 1;
2724 if (strchr(fromname, '@') || strchr(fromname, '/')) {
2725 (void) fprintf(stderr,
2726 gettext("invalid incremental source\n"));
2727 usage(B_FALSE);
2728 }
2729 }
2730 }
2731
428870ff
BB
2732 if (flags.replicate && fromname == NULL)
2733 flags.doall = B_TRUE;
2734
2735 err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0,
2736 extraverbose ? &dbgnv : NULL);
34dc7c2f 2737
428870ff
BB
2738 if (extraverbose) {
2739 /*
2740 * dump_nvlist prints to stdout, but that's been
2741 * redirected to a file. Make it print to stderr
2742 * instead.
2743 */
2744 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
2745 dump_nvlist(dbgnv, 0);
2746 nvlist_free(dbgnv);
2747 }
34dc7c2f
BB
2748 zfs_close(zhp);
2749
2750 return (err != 0);
2751}
2752
2753/*
428870ff 2754 * zfs receive [-vnFu] [-d | -e] <fs@snap>
34dc7c2f
BB
2755 *
2756 * Restore a backup stream from stdin.
2757 */
2758static int
2759zfs_do_receive(int argc, char **argv)
2760{
2761 int c, err;
428870ff 2762 recvflags_t flags = { 0 };
34dc7c2f 2763
34dc7c2f 2764 /* check options */
428870ff 2765 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
34dc7c2f
BB
2766 switch (c) {
2767 case 'd':
2768 flags.isprefix = B_TRUE;
2769 break;
428870ff
BB
2770 case 'e':
2771 flags.isprefix = B_TRUE;
2772 flags.istail = B_TRUE;
2773 break;
34dc7c2f
BB
2774 case 'n':
2775 flags.dryrun = B_TRUE;
2776 break;
d164b209
BB
2777 case 'u':
2778 flags.nomount = B_TRUE;
2779 break;
34dc7c2f
BB
2780 case 'v':
2781 flags.verbose = B_TRUE;
2782 break;
2783 case 'F':
2784 flags.force = B_TRUE;
2785 break;
2786 case ':':
2787 (void) fprintf(stderr, gettext("missing argument for "
2788 "'%c' option\n"), optopt);
2789 usage(B_FALSE);
2790 break;
2791 case '?':
2792 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2793 optopt);
2794 usage(B_FALSE);
2795 }
2796 }
2797
2798 argc -= optind;
2799 argv += optind;
2800
2801 /* check number of arguments */
2802 if (argc < 1) {
2803 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
2804 usage(B_FALSE);
2805 }
2806 if (argc > 1) {
2807 (void) fprintf(stderr, gettext("too many arguments\n"));
2808 usage(B_FALSE);
2809 }
2810
2811 if (isatty(STDIN_FILENO)) {
2812 (void) fprintf(stderr,
2813 gettext("Error: Backup stream can not be read "
2814 "from a terminal.\n"
2815 "You must redirect standard input.\n"));
2816 return (1);
2817 }
2818
2819 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2820
2821 return (err != 0);
2822}
2823
45d1cae3
BB
2824static int
2825zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
2826{
2827 int errors = 0;
2828 int i;
2829 const char *tag;
2830 boolean_t recursive = B_FALSE;
428870ff
BB
2831 boolean_t temphold = B_FALSE;
2832 const char *opts = holding ? "rt" : "r";
45d1cae3 2833 int c;
45d1cae3
BB
2834
2835 /* check options */
428870ff 2836 while ((c = getopt(argc, argv, opts)) != -1) {
45d1cae3
BB
2837 switch (c) {
2838 case 'r':
2839 recursive = B_TRUE;
2840 break;
428870ff
BB
2841 case 't':
2842 temphold = B_TRUE;
2843 break;
45d1cae3
BB
2844 case '?':
2845 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2846 optopt);
2847 usage(B_FALSE);
2848 }
2849 }
2850
2851 argc -= optind;
2852 argv += optind;
2853
2854 /* check number of arguments */
2855 if (argc < 2)
2856 usage(B_FALSE);
2857
2858 tag = argv[0];
2859 --argc;
2860 ++argv;
2861
428870ff
BB
2862 if (holding && tag[0] == '.') {
2863 /* tags starting with '.' are reserved for libzfs */
2864 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
2865 usage(B_FALSE);
45d1cae3
BB
2866 }
2867
2868 for (i = 0; i < argc; ++i) {
2869 zfs_handle_t *zhp;
2870 char parent[ZFS_MAXNAMELEN];
2871 const char *delim;
2872 char *path = argv[i];
2873
2874 delim = strchr(path, '@');
2875 if (delim == NULL) {
2876 (void) fprintf(stderr,
2877 gettext("'%s' is not a snapshot\n"), path);
2878 ++errors;
2879 continue;
2880 }
2881 (void) strncpy(parent, path, delim - path);
2882 parent[delim - path] = '\0';
2883
2884 zhp = zfs_open(g_zfs, parent,
2885 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2886 if (zhp == NULL) {
2887 ++errors;
2888 continue;
2889 }
428870ff
BB
2890 if (holding) {
2891 if (zfs_hold(zhp, delim+1, tag, recursive,
572e2857 2892 temphold, B_FALSE, -1, 0, 0) != 0)
428870ff
BB
2893 ++errors;
2894 } else {
2895 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
2896 ++errors;
2897 }
45d1cae3
BB
2898 zfs_close(zhp);
2899 }
2900
2901 return (errors != 0);
2902}
2903
2904/*
428870ff 2905 * zfs hold [-r] [-t] <tag> <snap> ...
45d1cae3 2906 *
428870ff
BB
2907 * -r Recursively hold
2908 * -t Temporary hold (hidden option)
45d1cae3
BB
2909 *
2910 * Apply a user-hold with the given tag to the list of snapshots.
2911 */
2912static int
2913zfs_do_hold(int argc, char **argv)
2914{
2915 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
2916}
2917
2918/*
2919 * zfs release [-r] <tag> <snap> ...
2920 *
428870ff 2921 * -r Recursively release
45d1cae3
BB
2922 *
2923 * Release a user-hold with the given tag from the list of snapshots.
2924 */
2925static int
2926zfs_do_release(int argc, char **argv)
2927{
2928 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
2929}
2930
34dc7c2f
BB
2931#define CHECK_SPINNER 30
2932#define SPINNER_TIME 3 /* seconds */
2933#define MOUNT_TIME 5 /* seconds */
2934
2935static int
2936get_one_dataset(zfs_handle_t *zhp, void *data)
2937{
428870ff 2938 static char *spin[] = { "-", "\\", "|", "/" };
34dc7c2f
BB
2939 static int spinval = 0;
2940 static int spincheck = 0;
2941 static time_t last_spin_time = (time_t)0;
572e2857 2942 get_all_cb_t *cbp = data;
34dc7c2f
BB
2943 zfs_type_t type = zfs_get_type(zhp);
2944
2945 if (cbp->cb_verbose) {
2946 if (--spincheck < 0) {
2947 time_t now = time(NULL);
2948 if (last_spin_time + SPINNER_TIME < now) {
428870ff 2949 update_progress(spin[spinval++ % 4]);
34dc7c2f
BB
2950 last_spin_time = now;
2951 }
2952 spincheck = CHECK_SPINNER;
2953 }
2954 }
2955
2956 /*
2957 * Interate over any nested datasets.
2958 */
572e2857 2959 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
34dc7c2f
BB
2960 zfs_close(zhp);
2961 return (1);
2962 }
2963
2964 /*
2965 * Skip any datasets whose type does not match.
2966 */
572e2857 2967 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
34dc7c2f
BB
2968 zfs_close(zhp);
2969 return (0);
2970 }
572e2857
BB
2971 libzfs_add_handle(cbp, zhp);
2972 assert(cbp->cb_used <= cbp->cb_alloc);
34dc7c2f
BB
2973
2974 return (0);
2975}
2976
2977static void
572e2857 2978get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
34dc7c2f 2979{
572e2857 2980 get_all_cb_t cb = { 0 };
34dc7c2f 2981 cb.cb_verbose = verbose;
572e2857 2982 cb.cb_getone = get_one_dataset;
34dc7c2f 2983
428870ff
BB
2984 if (verbose)
2985 set_progress_header(gettext("Reading ZFS config"));
34dc7c2f
BB
2986 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
2987
2988 *dslist = cb.cb_handles;
2989 *count = cb.cb_used;
2990
428870ff
BB
2991 if (verbose)
2992 finish_progress(gettext("done."));
34dc7c2f
BB
2993}
2994
34dc7c2f
BB
2995/*
2996 * Generic callback for sharing or mounting filesystems. Because the code is so
2997 * similar, we have a common function with an extra parameter to determine which
2998 * mode we are using.
2999 */
3000#define OP_SHARE 0x1
3001#define OP_MOUNT 0x2
3002
3003/*
3004 * Share or mount a dataset.
3005 */
3006static int
3007share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3008 boolean_t explicit, const char *options)
3009{
3010 char mountpoint[ZFS_MAXPROPLEN];
3011 char shareopts[ZFS_MAXPROPLEN];
3012 char smbshareopts[ZFS_MAXPROPLEN];
3013 const char *cmdname = op == OP_SHARE ? "share" : "mount";
3014 struct mnttab mnt;
3015 uint64_t zoned, canmount;
34dc7c2f
BB
3016 boolean_t shared_nfs, shared_smb;
3017
572e2857 3018 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
34dc7c2f 3019
572e2857
BB
3020 /*
3021 * Check to make sure we can mount/share this dataset. If we
3022 * are in the global zone and the filesystem is exported to a
3023 * local zone, or if we are in a local zone and the
3024 * filesystem is not exported, then it is an error.
3025 */
3026 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
34dc7c2f 3027
572e2857
BB
3028 if (zoned && getzoneid() == GLOBAL_ZONEID) {
3029 if (!explicit)
3030 return (0);
34dc7c2f 3031
572e2857
BB
3032 (void) fprintf(stderr, gettext("cannot %s '%s': "
3033 "dataset is exported to a local zone\n"), cmdname,
3034 zfs_get_name(zhp));
3035 return (1);
34dc7c2f 3036
572e2857
BB
3037 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
3038 if (!explicit)
3039 return (0);
34dc7c2f 3040
572e2857
BB
3041 (void) fprintf(stderr, gettext("cannot %s '%s': "
3042 "permission denied\n"), cmdname,
3043 zfs_get_name(zhp));
3044 return (1);
3045 }
34dc7c2f 3046
572e2857
BB
3047 /*
3048 * Ignore any filesystems which don't apply to us. This
3049 * includes those with a legacy mountpoint, or those with
3050 * legacy share options.
3051 */
3052 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
3053 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
3054 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
3055 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
3056 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3057 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3058
3059 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3060 strcmp(smbshareopts, "off") == 0) {
3061 if (!explicit)
3062 return (0);
34dc7c2f 3063
572e2857
BB
3064 (void) fprintf(stderr, gettext("cannot share '%s': "
3065 "legacy share\n"), zfs_get_name(zhp));
3066 (void) fprintf(stderr, gettext("use share(1M) to "
3067 "share this filesystem, or set "
3068 "sharenfs property on\n"));
3069 return (1);
3070 }
34dc7c2f 3071
572e2857
BB
3072 /*
3073 * We cannot share or mount legacy filesystems. If the
3074 * shareopts is non-legacy but the mountpoint is legacy, we
3075 * treat it as a legacy share.
3076 */
3077 if (strcmp(mountpoint, "legacy") == 0) {
3078 if (!explicit)
3079 return (0);
34dc7c2f 3080
572e2857
BB
3081 (void) fprintf(stderr, gettext("cannot %s '%s': "
3082 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
3083 (void) fprintf(stderr, gettext("use %s(1M) to "
3084 "%s this filesystem\n"), cmdname, cmdname);
3085 return (1);
3086 }
34dc7c2f 3087
572e2857
BB
3088 if (strcmp(mountpoint, "none") == 0) {
3089 if (!explicit)
3090 return (0);
34dc7c2f 3091
572e2857
BB
3092 (void) fprintf(stderr, gettext("cannot %s '%s': no "
3093 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
3094 return (1);
3095 }
34dc7c2f 3096
572e2857
BB
3097 /*
3098 * canmount explicit outcome
3099 * on no pass through
3100 * on yes pass through
3101 * off no return 0
3102 * off yes display error, return 1
3103 * noauto no return 0
3104 * noauto yes pass through
3105 */
3106 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
3107 if (canmount == ZFS_CANMOUNT_OFF) {
3108 if (!explicit)
3109 return (0);
3110
3111 (void) fprintf(stderr, gettext("cannot %s '%s': "
3112 "'canmount' property is set to 'off'\n"), cmdname,
3113 zfs_get_name(zhp));
3114 return (1);
3115 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
3116 return (0);
3117 }
3118
3119 /*
3120 * At this point, we have verified that the mountpoint and/or
3121 * shareopts are appropriate for auto management. If the
3122 * filesystem is already mounted or shared, return (failing
3123 * for explicit requests); otherwise mount or share the
3124 * filesystem.
3125 */
3126 switch (op) {
3127 case OP_SHARE:
3128
3129 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3130 shared_smb = zfs_is_shared_smb(zhp, NULL);
3131
149e873a
BB
3132 if ((shared_nfs && shared_smb) ||
3133 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
3134 (strcmp(smbshareopts, "off") == 0)) ||
3135 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
3136 (strcmp(shareopts, "off") == 0))) {
34dc7c2f
BB
3137 if (!explicit)
3138 return (0);
3139
572e2857
BB
3140 (void) fprintf(stderr, gettext("cannot share "
3141 "'%s': filesystem already shared\n"),
34dc7c2f
BB
3142 zfs_get_name(zhp));
3143 return (1);
34dc7c2f
BB
3144 }
3145
572e2857
BB
3146 if (!zfs_is_mounted(zhp, NULL) &&
3147 zfs_mount(zhp, NULL, 0) != 0)
3148 return (1);
34dc7c2f 3149
572e2857
BB
3150 if (protocol == NULL) {
3151 if (zfs_shareall(zhp) != 0)
34dc7c2f 3152 return (1);
572e2857
BB
3153 } else if (strcmp(protocol, "nfs") == 0) {
3154 if (zfs_share_nfs(zhp))
34dc7c2f 3155 return (1);
572e2857
BB
3156 } else if (strcmp(protocol, "smb") == 0) {
3157 if (zfs_share_smb(zhp))
34dc7c2f 3158 return (1);
572e2857
BB
3159 } else {
3160 (void) fprintf(stderr, gettext("cannot share "
3161 "'%s': invalid share type '%s' "
3162 "specified\n"),
3163 zfs_get_name(zhp), protocol);
3164 return (1);
3165 }
34dc7c2f 3166
572e2857 3167 break;
34dc7c2f 3168
572e2857
BB
3169 case OP_MOUNT:
3170 if (options == NULL)
3171 mnt.mnt_mntopts = "";
3172 else
3173 mnt.mnt_mntopts = (char *)options;
34dc7c2f 3174
572e2857
BB
3175 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
3176 zfs_is_mounted(zhp, NULL)) {
3177 if (!explicit)
3178 return (0);
34dc7c2f 3179
572e2857
BB
3180 (void) fprintf(stderr, gettext("cannot mount "
3181 "'%s': filesystem already mounted\n"),
3182 zfs_get_name(zhp));
3183 return (1);
34dc7c2f 3184 }
572e2857
BB
3185
3186 if (zfs_mount(zhp, options, flags) != 0)
3187 return (1);
3188 break;
3189 }
34dc7c2f 3190
34dc7c2f
BB
3191 return (0);
3192}
3193
3194/*
3195 * Reports progress in the form "(current/total)". Not thread-safe.
3196 */
3197static void
3198report_mount_progress(int current, int total)
3199{
428870ff 3200 static time_t last_progress_time = 0;
34dc7c2f 3201 time_t now = time(NULL);
428870ff 3202 char info[32];
34dc7c2f
BB
3203
3204 /* report 1..n instead of 0..n-1 */
3205 ++current;
3206
3207 /* display header if we're here for the first time */
3208 if (current == 1) {
428870ff 3209 set_progress_header(gettext("Mounting ZFS filesystems"));
34dc7c2f
BB
3210 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
3211 /* too soon to report again */
3212 return;
3213 }
3214
3215 last_progress_time = now;
3216
428870ff 3217 (void) sprintf(info, "(%d/%d)", current, total);
34dc7c2f 3218
428870ff
BB
3219 if (current == total)
3220 finish_progress(info);
3221 else
3222 update_progress(info);
34dc7c2f
BB
3223}
3224
3225static void
3226append_options(char *mntopts, char *newopts)
3227{
3228 int len = strlen(mntopts);
3229
3230 /* original length plus new string to append plus 1 for the comma */
3231 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
3232 (void) fprintf(stderr, gettext("the opts argument for "
d6320ddb 3233 "'%s' option is too long (more than %d chars)\n"),
34dc7c2f
BB
3234 "-o", MNT_LINE_MAX);
3235 usage(B_FALSE);
3236 }
3237
3238 if (*mntopts)
3239 mntopts[len++] = ',';
3240
3241 (void) strcpy(&mntopts[len], newopts);
3242}
3243
3244static int
3245share_mount(int op, int argc, char **argv)
3246{
3247 int do_all = 0;
3248 boolean_t verbose = B_FALSE;
3249 int c, ret = 0;
3250 char *options = NULL;
572e2857 3251 int flags = 0;
34dc7c2f
BB
3252
3253 /* check options */
3fb1fcde 3254 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:" : "a"))
34dc7c2f
BB
3255 != -1) {
3256 switch (c) {
3257 case 'a':
3258 do_all = 1;
3259 break;
3260 case 'v':
3261 verbose = B_TRUE;
3262 break;
3263 case 'o':
3264 if (*optarg == '\0') {
3265 (void) fprintf(stderr, gettext("empty mount "
3266 "options (-o) specified\n"));
3267 usage(B_FALSE);
3268 }
3269
3270 if (options == NULL)
3271 options = safe_malloc(MNT_LINE_MAX + 1);
3272
3273 /* option validation is done later */
3274 append_options(options, optarg);
3275 break;
3276
34dc7c2f
BB
3277 case ':':
3278 (void) fprintf(stderr, gettext("missing argument for "
3279 "'%c' option\n"), optopt);
3280 usage(B_FALSE);
3281 break;
3282 case '?':
3283 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3284 optopt);
3285 usage(B_FALSE);
3286 }
3287 }
3288
3289 argc -= optind;
3290 argv += optind;
3291
3292 /* check number of arguments */
3293 if (do_all) {
3294 zfs_handle_t **dslist = NULL;
3295 size_t i, count = 0;
3296 char *protocol = NULL;
3297
572e2857
BB
3298 if (op == OP_SHARE && argc > 0) {
3299 if (strcmp(argv[0], "nfs") != 0 &&
3300 strcmp(argv[0], "smb") != 0) {
34dc7c2f 3301 (void) fprintf(stderr, gettext("share type "
428870ff 3302 "must be 'nfs' or 'smb'\n"));
34dc7c2f
BB
3303 usage(B_FALSE);
3304 }
3305 protocol = argv[0];
3306 argc--;
3307 argv++;
34dc7c2f
BB
3308 }
3309
3310 if (argc != 0) {
3311 (void) fprintf(stderr, gettext("too many arguments\n"));
3312 usage(B_FALSE);
3313 }
3314
428870ff 3315 start_progress_timer();
572e2857 3316 get_all_datasets(&dslist, &count, verbose);
34dc7c2f
BB
3317
3318 if (count == 0)
3319 return (0);
3320
572e2857 3321 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
34dc7c2f
BB
3322
3323 for (i = 0; i < count; i++) {
3324 if (verbose)
3325 report_mount_progress(i, count);
3326
3327 if (share_mount_one(dslist[i], op, flags, protocol,
3328 B_FALSE, options) != 0)
3329 ret = 1;
3330 zfs_close(dslist[i]);
3331 }
3332
3333 free(dslist);
3334 } else if (argc == 0) {
3335 struct mnttab entry;
3336
3337 if ((op == OP_SHARE) || (options != NULL)) {
3338 (void) fprintf(stderr, gettext("missing filesystem "
3339 "argument (specify -a for all)\n"));
3340 usage(B_FALSE);
3341 }
3342
3343 /*
9a616b5d 3344 * When mount is given no arguments, go through /etc/mtab and
34dc7c2f
BB
3345 * display any active ZFS mounts. We hide any snapshots, since
3346 * they are controlled automatically.
3347 */
3348 rewind(mnttab_file);
3349 while (getmntent(mnttab_file, &entry) == 0) {
3350 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
3351 strchr(entry.mnt_special, '@') != NULL)
3352 continue;
3353
3354 (void) printf("%-30s %s\n", entry.mnt_special,
3355 entry.mnt_mountp);
3356 }
3357
3358 } else {
3359 zfs_handle_t *zhp;
3360
34dc7c2f
BB
3361 if (argc > 1) {
3362 (void) fprintf(stderr,
3363 gettext("too many arguments\n"));
3364 usage(B_FALSE);
3365 }
3366
572e2857
BB
3367 if ((zhp = zfs_open(g_zfs, argv[0],
3368 ZFS_TYPE_FILESYSTEM)) == NULL) {
34dc7c2f
BB
3369 ret = 1;
3370 } else {
3371 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3372 options);
3373 zfs_close(zhp);
3374 }
3375 }
3376
3377 return (ret);
3378}
3379
3380/*
428870ff 3381 * zfs mount -a [nfs]
34dc7c2f
BB
3382 * zfs mount filesystem
3383 *
3384 * Mount all filesystems, or mount the given filesystem.
3385 */
3386static int
3387zfs_do_mount(int argc, char **argv)
3388{
3389 return (share_mount(OP_MOUNT, argc, argv));
3390}
3391
3392/*
428870ff 3393 * zfs share -a [nfs | smb]
34dc7c2f
BB
3394 * zfs share filesystem
3395 *
3396 * Share all filesystems, or share the given filesystem.
3397 */
3398static int
3399zfs_do_share(int argc, char **argv)
3400{
3401 return (share_mount(OP_SHARE, argc, argv));
3402}
3403
3404typedef struct unshare_unmount_node {
3405 zfs_handle_t *un_zhp;
3406 char *un_mountp;
3407 uu_avl_node_t un_avlnode;
3408} unshare_unmount_node_t;
3409
3410/* ARGSUSED */
3411static int
3412unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
3413{
3414 const unshare_unmount_node_t *l = larg;
3415 const unshare_unmount_node_t *r = rarg;
3416
3417 return (strcmp(l->un_mountp, r->un_mountp));
3418}
3419
3420/*
3421 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
9a616b5d 3422 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
34dc7c2f
BB
3423 * and unmount it appropriately.
3424 */
3425static int
3426unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
3427{
3428 zfs_handle_t *zhp;
3429 int ret;
3430 struct stat64 statbuf;
3431 struct extmnttab entry;
3432 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
3433 ino_t path_inode;
3434
3435 /*
9a616b5d 3436 * Search for the path in /etc/mtab. Rather than looking for the
34dc7c2f
BB
3437 * specific path, which can be fooled by non-standard paths (i.e. ".."
3438 * or "//"), we stat() the path and search for the corresponding
3439 * (major,minor) device pair.
3440 */
3441 if (stat64(path, &statbuf) != 0) {
3442 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3443 cmdname, path, strerror(errno));
3444 return (1);
3445 }
3446 path_inode = statbuf.st_ino;
3447
3448 /*
3449 * Search for the given (major,minor) pair in the mount table.
3450 */
3451 rewind(mnttab_file);
3452 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
3453 if (entry.mnt_major == major(statbuf.st_dev) &&
3454 entry.mnt_minor == minor(statbuf.st_dev))
3455 break;
3456 }
3457 if (ret != 0) {
3458 if (op == OP_SHARE) {
3459 (void) fprintf(stderr, gettext("cannot %s '%s': not "
3460 "currently mounted\n"), cmdname, path);
3461 return (1);
3462 }
9a616b5d 3463 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
34dc7c2f
BB
3464 path);
3465 if ((ret = umount2(path, flags)) != 0)
3466 (void) fprintf(stderr, gettext("%s: %s\n"), path,
3467 strerror(errno));
3468 return (ret != 0);
3469 }
3470
3471 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
3472 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
3473 "filesystem\n"), cmdname, path);
3474 return (1);
3475 }
3476
3477 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3478 ZFS_TYPE_FILESYSTEM)) == NULL)
3479 return (1);
3480
34dc7c2f 3481 ret = 1;
b128c09f
BB
3482 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
3483 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
3484 cmdname, path, strerror(errno));
3485 goto out;
3486 } else if (statbuf.st_ino != path_inode) {
3487 (void) fprintf(stderr, gettext("cannot "
3488 "%s '%s': not a mountpoint\n"), cmdname, path);
3489 goto out;
3490 }
3491
34dc7c2f
BB
3492 if (op == OP_SHARE) {
3493 char nfs_mnt_prop[ZFS_MAXPROPLEN];
3494 char smbshare_prop[ZFS_MAXPROPLEN];
3495
3496 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
3497 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3498 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
3499 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
3500
3501 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3502 strcmp(smbshare_prop, "off") == 0) {
3503 (void) fprintf(stderr, gettext("cannot unshare "
3504 "'%s': legacy share\n"), path);
9a616b5d
BB
3505 (void) fprintf(stderr, gettext("use exportfs(8) "
3506 "or smbcontrol(1) to unshare this filesystem\n"));
34dc7c2f
BB
3507 } else if (!zfs_is_shared(zhp)) {
3508 (void) fprintf(stderr, gettext("cannot unshare '%s': "
3509 "not currently shared\n"), path);
3510 } else {
3511 ret = zfs_unshareall_bypath(zhp, path);
3512 }
3513 } else {
3514 char mtpt_prop[ZFS_MAXPROPLEN];
3515
3516 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
3517 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
3518
b128c09f 3519 if (is_manual) {
34dc7c2f
BB
3520 ret = zfs_unmount(zhp, NULL, flags);
3521 } else if (strcmp(mtpt_prop, "legacy") == 0) {
3522 (void) fprintf(stderr, gettext("cannot unmount "
3523 "'%s': legacy mountpoint\n"),
3524 zfs_get_name(zhp));
9a616b5d 3525 (void) fprintf(stderr, gettext("use umount(8) "
34dc7c2f
BB
3526 "to unmount this filesystem\n"));
3527 } else {
3528 ret = zfs_unmountall(zhp, flags);
3529 }
3530 }
3531
b128c09f 3532out:
34dc7c2f
BB
3533 zfs_close(zhp);
3534
3535 return (ret != 0);
3536}
3537
3538/*
3539 * Generic callback for unsharing or unmounting a filesystem.
3540 */
3541static int
3542unshare_unmount(int op, int argc, char **argv)
3543{
3544 int do_all = 0;
3545 int flags = 0;
3546 int ret = 0;
572e2857 3547 int c;
34dc7c2f 3548 zfs_handle_t *zhp;
428870ff 3549 char nfs_mnt_prop[ZFS_MAXPROPLEN];
34dc7c2f
BB
3550 char sharesmb[ZFS_MAXPROPLEN];
3551
3552 /* check options */
3553 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
3554 switch (c) {
3555 case 'a':
3556 do_all = 1;
3557 break;
3558 case 'f':
3559 flags = MS_FORCE;
3560 break;
3561 case '?':
3562 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3563 optopt);
3564 usage(B_FALSE);
3565 }
3566 }
3567
3568 argc -= optind;
3569 argv += optind;
3570
3571 if (do_all) {
3572 /*
3573 * We could make use of zfs_for_each() to walk all datasets in
3574 * the system, but this would be very inefficient, especially
9a616b5d
BB
3575 * since we would have to linearly search /etc/mtab for each
3576 * one. Instead, do one pass through /etc/mtab looking for
34dc7c2f
BB
3577 * zfs entries and call zfs_unmount() for each one.
3578 *
3579 * Things get a little tricky if the administrator has created
3580 * mountpoints beneath other ZFS filesystems. In this case, we
3581 * have to unmount the deepest filesystems first. To accomplish
3582 * this, we place all the mountpoints in an AVL tree sorted by
3583 * the special type (dataset name), and walk the result in
3584 * reverse to make sure to get any snapshots first.
3585 */
3586 struct mnttab entry;
3587 uu_avl_pool_t *pool;
149e873a 3588 uu_avl_t *tree = NULL;
34dc7c2f
BB
3589 unshare_unmount_node_t *node;
3590 uu_avl_index_t idx;
3591 uu_avl_walk_t *walk;
3592
3593 if (argc != 0) {
3594 (void) fprintf(stderr, gettext("too many arguments\n"));
3595 usage(B_FALSE);
3596 }
3597
428870ff 3598 if (((pool = uu_avl_pool_create("unmount_pool",
34dc7c2f
BB
3599 sizeof (unshare_unmount_node_t),
3600 offsetof(unshare_unmount_node_t, un_avlnode),
428870ff
BB
3601 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
3602 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
3603 nomem();
34dc7c2f
BB
3604
3605 rewind(mnttab_file);
3606 while (getmntent(mnttab_file, &entry) == 0) {
3607
3608 /* ignore non-ZFS entries */
3609 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
3610 continue;
3611
3612 /* ignore snapshots */
3613 if (strchr(entry.mnt_special, '@') != NULL)
3614 continue;
3615
3616 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
3617 ZFS_TYPE_FILESYSTEM)) == NULL) {
3618 ret = 1;
3619 continue;
3620 }
3621
3622 switch (op) {
3623 case OP_SHARE:
3624 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
428870ff
BB
3625 nfs_mnt_prop,
3626 sizeof (nfs_mnt_prop),
34dc7c2f 3627 NULL, NULL, 0, B_FALSE) == 0);
428870ff 3628 if (strcmp(nfs_mnt_prop, "off") != 0)
34dc7c2f
BB
3629 break;
3630 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
428870ff
BB
3631 nfs_mnt_prop,
3632 sizeof (nfs_mnt_prop),
34dc7c2f 3633 NULL, NULL, 0, B_FALSE) == 0);
428870ff 3634 if (strcmp(nfs_mnt_prop, "off") == 0)
34dc7c2f
BB
3635 continue;
3636 break;
3637 case OP_MOUNT:
3638 /* Ignore legacy mounts */
3639 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
428870ff
BB
3640 nfs_mnt_prop,
3641 sizeof (nfs_mnt_prop),
34dc7c2f 3642 NULL, NULL, 0, B_FALSE) == 0);
428870ff 3643 if (strcmp(nfs_mnt_prop, "legacy") == 0)
34dc7c2f
BB
3644 continue;
3645 /* Ignore canmount=noauto mounts */
3646 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
3647 ZFS_CANMOUNT_NOAUTO)
3648 continue;
3649 default:
3650 break;
3651 }
3652
3653 node = safe_malloc(sizeof (unshare_unmount_node_t));
3654 node->un_zhp = zhp;
428870ff 3655 node->un_mountp = safe_strdup(entry.mnt_mountp);
34dc7c2f
BB
3656
3657 uu_avl_node_init(node, &node->un_avlnode, pool);
3658
3659 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
3660 uu_avl_insert(tree, node, idx);
3661 } else {
3662 zfs_close(node->un_zhp);
3663 free(node->un_mountp);
3664 free(node);
3665 }
3666 }
3667
3668 /*
3669 * Walk the AVL tree in reverse, unmounting each filesystem and
3670 * removing it from the AVL tree in the process.
3671 */
3672 if ((walk = uu_avl_walk_start(tree,
428870ff
BB
3673 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
3674 nomem();
34dc7c2f
BB
3675
3676 while ((node = uu_avl_walk_next(walk)) != NULL) {
3677 uu_avl_remove(tree, node);
3678
3679 switch (op) {
3680 case OP_SHARE:
3681 if (zfs_unshareall_bypath(node->un_zhp,
3682 node->un_mountp) != 0)
3683 ret = 1;
3684 break;
3685
3686 case OP_MOUNT:
3687 if (zfs_unmount(node->un_zhp,
3688 node->un_mountp, flags) != 0)
3689 ret = 1;
3690 break;
3691 }
3692
3693 zfs_close(node->un_zhp);
3694 free(node->un_mountp);
3695 free(node);
3696 }
3697
3698 uu_avl_walk_end(walk);
3699 uu_avl_destroy(tree);
3700 uu_avl_pool_destroy(pool);
3701
34dc7c2f
BB
3702 } else {
3703 if (argc != 1) {
3704 if (argc == 0)
3705 (void) fprintf(stderr,
3706 gettext("missing filesystem argument\n"));
3707 else
3708 (void) fprintf(stderr,
3709 gettext("too many arguments\n"));
3710 usage(B_FALSE);
3711 }
3712
3713 /*
3714 * We have an argument, but it may be a full path or a ZFS
3715 * filesystem. Pass full paths off to unmount_path() (shared by
3716 * manual_unmount), otherwise open the filesystem and pass to
3717 * zfs_unmount().
3718 */
3719 if (argv[0][0] == '/')
3720 return (unshare_unmount_path(op, argv[0],
3721 flags, B_FALSE));
3722
572e2857
BB
3723 if ((zhp = zfs_open(g_zfs, argv[0],
3724 ZFS_TYPE_FILESYSTEM)) == NULL)
34dc7c2f
BB
3725 return (1);
3726
572e2857
BB
3727 verify(zfs_prop_get(zhp, op == OP_SHARE ?
3728 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3729 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
3730 NULL, 0, B_FALSE) == 0);
34dc7c2f 3731
572e2857
BB
3732 switch (op) {
3733 case OP_SHARE:
3734 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3735 nfs_mnt_prop,
3736 sizeof (nfs_mnt_prop),
3737 NULL, NULL, 0, B_FALSE) == 0);
3738 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3739 sharesmb, sizeof (sharesmb), NULL, NULL,
3740 0, B_FALSE) == 0);
3741
3742 if (strcmp(nfs_mnt_prop, "off") == 0 &&
3743 strcmp(sharesmb, "off") == 0) {
3744 (void) fprintf(stderr, gettext("cannot "
3745 "unshare '%s': legacy share\n"),
3746 zfs_get_name(zhp));
3747 (void) fprintf(stderr, gettext("use "
3748 "unshare(1M) to unshare this "
3749 "filesystem\n"));
3750 ret = 1;
3751 } else if (!zfs_is_shared(zhp)) {
3752 (void) fprintf(stderr, gettext("cannot "
3753 "unshare '%s': not currently "
3754 "shared\n"), zfs_get_name(zhp));
3755 ret = 1;
3756 } else if (zfs_unshareall(zhp) != 0) {
3757 ret = 1;
3758 }
3759 break;
34dc7c2f 3760
572e2857
BB
3761 case OP_MOUNT:
3762 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
3763 (void) fprintf(stderr, gettext("cannot "
3764 "unmount '%s': legacy "
3765 "mountpoint\n"), zfs_get_name(zhp));
3766 (void) fprintf(stderr, gettext("use "
3767 "umount(1M) to unmount this "
3768 "filesystem\n"));
3769 ret = 1;
3770 } else if (!zfs_is_mounted(zhp, NULL)) {
3771 (void) fprintf(stderr, gettext("cannot "
3772 "unmount '%s': not currently "
3773 "mounted\n"),
3774 zfs_get_name(zhp));
3775 ret = 1;
3776 } else if (zfs_unmountall(zhp, flags) != 0) {
3777 ret = 1;
34dc7c2f 3778 }
572e2857 3779 break;
34dc7c2f
BB
3780 }
3781
3782 zfs_close(zhp);
3783 }
3784
3785 return (ret);
3786}
3787
3788/*
3789 * zfs unmount -a
3790 * zfs unmount filesystem
3791 *
3792 * Unmount all filesystems, or a specific ZFS filesystem.
3793 */
3794static int
3795zfs_do_unmount(int argc, char **argv)
3796{
3797 return (unshare_unmount(OP_MOUNT, argc, argv));
3798}
3799
3800/*
3801 * zfs unshare -a
3802 * zfs unshare filesystem
3803 *
3804 * Unshare all filesystems, or a specific ZFS filesystem.
3805 */
3806static int
3807zfs_do_unshare(int argc, char **argv)
3808{
3809 return (unshare_unmount(OP_SHARE, argc, argv));
3810}
3811
9babb374
BB
3812/* ARGSUSED */
3813static int
3814zfs_do_python(int argc, char **argv)
3815{
3816 (void) execv(pypath, argv-1);
3817 (void) printf("internal error: %s not found\n", pypath);
3818 return (-1);
3819}
3820
34dc7c2f
BB
3821static int
3822find_command_idx(char *command, int *idx)
3823{
3824 int i;
3825
3826 for (i = 0; i < NCOMMAND; i++) {
3827 if (command_table[i].name == NULL)
3828 continue;
3829
3830 if (strcmp(command, command_table[i].name) == 0) {
3831 *idx = i;
3832 return (0);
3833 }
3834 }
3835 return (1);
3836}
3837
572e2857
BB
3838static int
3839zfs_do_diff(int argc, char **argv)
3840{
3841 zfs_handle_t *zhp;
3842 int flags = 0;
3843 char *tosnap = NULL;
3844 char *fromsnap = NULL;
3845 char *atp, *copy;
3846 int err;
3847 int c;
3848
3849 while ((c = getopt(argc, argv, "FHt")) != -1) {
3850 switch (c) {
3851 case 'F':
3852 flags |= ZFS_DIFF_CLASSIFY;
3853 break;
3854 case 'H':
3855 flags |= ZFS_DIFF_PARSEABLE;
3856 break;
3857 case 't':
3858 flags |= ZFS_DIFF_TIMESTAMP;
3859 break;
3860 default:
3861 (void) fprintf(stderr,
3862 gettext("invalid option '%c'\n"), optopt);
3863 usage(B_FALSE);
3864 }
3865 }
3866
3867 argc -= optind;
3868 argv += optind;
3869
3870 if (argc < 1) {
3871 (void) fprintf(stderr,
3872 gettext("must provide at least one snapshot name\n"));
3873 usage(B_FALSE);
3874 }
3875
3876 if (argc > 2) {
3877 (void) fprintf(stderr, gettext("too many arguments\n"));
3878 usage(B_FALSE);
3879 }
3880
3881 fromsnap = argv[0];
3882 tosnap = (argc == 2) ? argv[1] : NULL;
3883
3884 copy = NULL;
3885 if (*fromsnap != '@')
3886 copy = strdup(fromsnap);
3887 else if (tosnap)
3888 copy = strdup(tosnap);
3889 if (copy == NULL)
3890 usage(B_FALSE);
3891
c65aa5b2 3892 if ((atp = strchr(copy, '@')))
572e2857
BB
3893 *atp = '\0';
3894
3895 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
3896 return (1);
3897
3898 free(copy);
3899
3900 /*
3901 * Ignore SIGPIPE so that the library can give us
3902 * information on any failure
3903 */
3904 (void) sigignore(SIGPIPE);
3905
3906 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
3907
3908 zfs_close(zhp);
3909
3910 return (err != 0);
3911}
3912
34dc7c2f
BB
3913int
3914main(int argc, char **argv)
3915{
3916 int ret;
d4ed6673 3917 int i = 0;
34dc7c2f
BB
3918 char *cmdname;
3919
3920 (void) setlocale(LC_ALL, "");
3921 (void) textdomain(TEXT_DOMAIN);
3922
3923 opterr = 0;
3924
34dc7c2f
BB
3925 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
3926 (void) fprintf(stderr, gettext("internal error: unable to "
3927 "open %s\n"), MNTTAB);
3928 return (1);
3929 }
3930
3931 /*
d53368f6 3932 * Make sure the user has specified some command.
34dc7c2f 3933 */
d53368f6
BB
3934 if (argc < 2) {
3935 (void) fprintf(stderr, gettext("missing command\n"));
3936 usage(B_FALSE);
3937 }
34dc7c2f 3938
d53368f6 3939 cmdname = argv[1];
34dc7c2f 3940
d53368f6
BB
3941 /*
3942 * The 'umount' command is an alias for 'unmount'
3943 */
3944 if (strcmp(cmdname, "umount") == 0)
3945 cmdname = "unmount";
34dc7c2f 3946
d53368f6
BB
3947 /*
3948 * The 'recv' command is an alias for 'receive'
3949 */
3950 if (strcmp(cmdname, "recv") == 0)
3951 cmdname = "receive";
34dc7c2f 3952
d53368f6
BB
3953 /*
3954 * Special case '-?'
3955 */
3956 if ((strcmp(cmdname, "-?") == 0) ||
3957 (strcmp(cmdname, "--help") == 0))
3958 usage(B_TRUE);
34dc7c2f 3959
d53368f6
BB
3960 if ((g_zfs = libzfs_init()) == NULL)
3961 return (1);
9b020fd9 3962
d53368f6
BB
3963 zpool_set_history_str("zfs", argc, argv, history_str);
3964 verify(zpool_stage_history(g_zfs, history_str) == 0);
9b020fd9 3965
d53368f6 3966 libzfs_print_on_error(g_zfs, B_TRUE);
9b020fd9 3967
d53368f6
BB
3968 /*
3969 * Run the appropriate command.
3970 */
3971 libzfs_mnttab_cache(g_zfs, B_TRUE);
3972 if (find_command_idx(cmdname, &i) == 0) {
3973 current_command = &command_table[i];
3974 ret = command_table[i].func(argc - 1, argv + 1);
3975 } else if (strchr(cmdname, '=') != NULL) {
3976 verify(find_command_idx("set", &i) == 0);
3977 current_command = &command_table[i];
3978 ret = command_table[i].func(argc, argv);
3979 } else {
3980 (void) fprintf(stderr, gettext("unrecognized "
3981 "command '%s'\n"), cmdname);
3982 usage(B_FALSE);
3983 ret = 1;
34dc7c2f 3984 }
d53368f6
BB
3985 libzfs_mnttab_cache(g_zfs, B_FALSE);
3986 libzfs_fini(g_zfs);
34dc7c2f
BB
3987
3988 (void) fclose(mnttab_file);
3989
34dc7c2f
BB
3990 /*
3991 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3992 * for the purposes of running ::findleaks.
3993 */
3994 if (getenv("ZFS_ABORT") != NULL) {
3995 (void) printf("dumping core by request\n");
3996 abort();
3997 }
3998
3999 return (ret);
4000}