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