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