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