zfs_main.c revision 329484
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22/* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 25 * Copyright 2012 Milan Jurik. All rights reserved. 26 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved. 28 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 29 * Copyright (c) 2013 Steven Hartland. All rights reserved. 30 * Copyright (c) 2014 Integros [integros.com] 31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>. 32 * Copyright 2016 Nexenta Systems, Inc. 33 */ 34 35#include <assert.h> 36#include <ctype.h> 37#include <errno.h> 38#include <getopt.h> 39#include <libgen.h> 40#include <libintl.h> 41#include <libuutil.h> 42#include <libnvpair.h> 43#include <locale.h> 44#include <stddef.h> 45#include <stdio.h> 46#include <stdlib.h> 47#include <strings.h> 48#include <unistd.h> 49#include <fcntl.h> 50#include <zone.h> 51#include <grp.h> 52#include <pwd.h> 53#include <signal.h> 54#include <sys/debug.h> 55#include <sys/list.h> 56#include <sys/mntent.h> 57#include <sys/mnttab.h> 58#include <sys/mount.h> 59#include <sys/stat.h> 60#include <sys/fs/zfs.h> 61#include <sys/types.h> 62#include <time.h> 63#include <err.h> 64#include <jail.h> 65 66#include <libzfs.h> 67#include <libzfs_core.h> 68#include <zfs_prop.h> 69#include <zfs_deleg.h> 70#include <libuutil.h> 71#ifdef illumos 72#include <aclutils.h> 73#include <directory.h> 74#include <idmap.h> 75#endif 76 77#include "zfs_iter.h" 78#include "zfs_util.h" 79#include "zfs_comutil.h" 80 81libzfs_handle_t *g_zfs; 82 83static FILE *mnttab_file; 84static char history_str[HIS_MAX_RECORD_LEN]; 85static boolean_t log_history = B_TRUE; 86 87static int zfs_do_clone(int argc, char **argv); 88static int zfs_do_create(int argc, char **argv); 89static int zfs_do_destroy(int argc, char **argv); 90static int zfs_do_get(int argc, char **argv); 91static int zfs_do_inherit(int argc, char **argv); 92static int zfs_do_list(int argc, char **argv); 93static int zfs_do_mount(int argc, char **argv); 94static int zfs_do_rename(int argc, char **argv); 95static int zfs_do_rollback(int argc, char **argv); 96static int zfs_do_set(int argc, char **argv); 97static int zfs_do_upgrade(int argc, char **argv); 98static int zfs_do_snapshot(int argc, char **argv); 99static int zfs_do_unmount(int argc, char **argv); 100static int zfs_do_share(int argc, char **argv); 101static int zfs_do_unshare(int argc, char **argv); 102static int zfs_do_send(int argc, char **argv); 103static int zfs_do_receive(int argc, char **argv); 104static int zfs_do_promote(int argc, char **argv); 105static int zfs_do_userspace(int argc, char **argv); 106static int zfs_do_allow(int argc, char **argv); 107static int zfs_do_unallow(int argc, char **argv); 108static int zfs_do_hold(int argc, char **argv); 109static int zfs_do_holds(int argc, char **argv); 110static int zfs_do_release(int argc, char **argv); 111static int zfs_do_diff(int argc, char **argv); 112static int zfs_do_jail(int argc, char **argv); 113static int zfs_do_unjail(int argc, char **argv); 114static int zfs_do_bookmark(int argc, char **argv); 115static int zfs_do_channel_program(int argc, char **argv); 116 117/* 118 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds. 119 */ 120 121#ifdef DEBUG 122const char * 123_umem_debug_init(void) 124{ 125 return ("default,verbose"); /* $UMEM_DEBUG setting */ 126} 127 128const char * 129_umem_logging_init(void) 130{ 131 return ("fail,contents"); /* $UMEM_LOGGING setting */ 132} 133#endif 134 135typedef enum { 136 HELP_CLONE, 137 HELP_CREATE, 138 HELP_DESTROY, 139 HELP_GET, 140 HELP_INHERIT, 141 HELP_UPGRADE, 142 HELP_JAIL, 143 HELP_UNJAIL, 144 HELP_LIST, 145 HELP_MOUNT, 146 HELP_PROMOTE, 147 HELP_RECEIVE, 148 HELP_RENAME, 149 HELP_ROLLBACK, 150 HELP_SEND, 151 HELP_SET, 152 HELP_SHARE, 153 HELP_SNAPSHOT, 154 HELP_UNMOUNT, 155 HELP_UNSHARE, 156 HELP_ALLOW, 157 HELP_UNALLOW, 158 HELP_USERSPACE, 159 HELP_GROUPSPACE, 160 HELP_HOLD, 161 HELP_HOLDS, 162 HELP_RELEASE, 163 HELP_DIFF, 164 HELP_BOOKMARK, 165 HELP_CHANNEL_PROGRAM, 166} zfs_help_t; 167 168typedef struct zfs_command { 169 const char *name; 170 int (*func)(int argc, char **argv); 171 zfs_help_t usage; 172} zfs_command_t; 173 174/* 175 * Master command table. Each ZFS command has a name, associated function, and 176 * usage message. The usage messages need to be internationalized, so we have 177 * to have a function to return the usage message based on a command index. 178 * 179 * These commands are organized according to how they are displayed in the usage 180 * message. An empty command (one with a NULL name) indicates an empty line in 181 * the generic usage message. 182 */ 183static zfs_command_t command_table[] = { 184 { "create", zfs_do_create, HELP_CREATE }, 185 { "destroy", zfs_do_destroy, HELP_DESTROY }, 186 { NULL }, 187 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT }, 188 { "rollback", zfs_do_rollback, HELP_ROLLBACK }, 189 { "clone", zfs_do_clone, HELP_CLONE }, 190 { "promote", zfs_do_promote, HELP_PROMOTE }, 191 { "rename", zfs_do_rename, HELP_RENAME }, 192 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK }, 193 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM }, 194 { NULL }, 195 { "list", zfs_do_list, HELP_LIST }, 196 { NULL }, 197 { "set", zfs_do_set, HELP_SET }, 198 { "get", zfs_do_get, HELP_GET }, 199 { "inherit", zfs_do_inherit, HELP_INHERIT }, 200 { "upgrade", zfs_do_upgrade, HELP_UPGRADE }, 201 { "userspace", zfs_do_userspace, HELP_USERSPACE }, 202 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE }, 203 { NULL }, 204 { "mount", zfs_do_mount, HELP_MOUNT }, 205 { "unmount", zfs_do_unmount, HELP_UNMOUNT }, 206 { "share", zfs_do_share, HELP_SHARE }, 207 { "unshare", zfs_do_unshare, HELP_UNSHARE }, 208 { NULL }, 209 { "send", zfs_do_send, HELP_SEND }, 210 { "receive", zfs_do_receive, HELP_RECEIVE }, 211 { NULL }, 212 { "allow", zfs_do_allow, HELP_ALLOW }, 213 { NULL }, 214 { "unallow", zfs_do_unallow, HELP_UNALLOW }, 215 { NULL }, 216 { "hold", zfs_do_hold, HELP_HOLD }, 217 { "holds", zfs_do_holds, HELP_HOLDS }, 218 { "release", zfs_do_release, HELP_RELEASE }, 219 { "diff", zfs_do_diff, HELP_DIFF }, 220 { NULL }, 221 { "jail", zfs_do_jail, HELP_JAIL }, 222 { "unjail", zfs_do_unjail, HELP_UNJAIL }, 223}; 224 225#define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 226 227zfs_command_t *current_command; 228 229static const char * 230get_usage(zfs_help_t idx) 231{ 232 switch (idx) { 233 case HELP_CLONE: 234 return (gettext("\tclone [-p] [-o property=value] ... " 235 "<snapshot> <filesystem|volume>\n")); 236 case HELP_CREATE: 237 return (gettext("\tcreate [-pu] [-o property=value] ... " 238 "<filesystem>\n" 239 "\tcreate [-ps] [-b blocksize] [-o property=value] ... " 240 "-V <size> <volume>\n")); 241 case HELP_DESTROY: 242 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n" 243 "\tdestroy [-dnpRrv] " 244 "<filesystem|volume>@<snap>[%<snap>][,...]\n" 245 "\tdestroy <filesystem|volume>#<bookmark>\n")); 246 case HELP_GET: 247 return (gettext("\tget [-rHp] [-d max] " 248 "[-o \"all\" | field[,...]]\n" 249 "\t [-t type[,...]] [-s source[,...]]\n" 250 "\t <\"all\" | property[,...]> " 251 "[filesystem|volume|snapshot|bookmark] ...\n")); 252 case HELP_INHERIT: 253 return (gettext("\tinherit [-rS] <property> " 254 "<filesystem|volume|snapshot> ...\n")); 255 case HELP_UPGRADE: 256 return (gettext("\tupgrade [-v]\n" 257 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n")); 258 case HELP_JAIL: 259 return (gettext("\tjail <jailid|jailname> <filesystem>\n")); 260 case HELP_UNJAIL: 261 return (gettext("\tunjail <jailid|jailname> <filesystem>\n")); 262 case HELP_LIST: 263 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] " 264 "[-s property]...\n\t [-S property]... [-t type[,...]] " 265 "[filesystem|volume|snapshot] ...\n")); 266 case HELP_MOUNT: 267 return (gettext("\tmount\n" 268 "\tmount [-vO] [-o opts] <-a | filesystem>\n")); 269 case HELP_PROMOTE: 270 return (gettext("\tpromote <clone-filesystem>\n")); 271 case HELP_RECEIVE: 272 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|" 273 "snapshot>\n" 274 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] " 275 "<filesystem>\n" 276 "\treceive|recv -A <filesystem|volume>\n")); 277 case HELP_RENAME: 278 return (gettext("\trename [-f] <filesystem|volume|snapshot> " 279 "<filesystem|volume|snapshot>\n" 280 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n" 281 "\trename -r <snapshot> <snapshot>\n" 282 "\trename -u [-p] <filesystem> <filesystem>")); 283 case HELP_ROLLBACK: 284 return (gettext("\trollback [-rRf] <snapshot>\n")); 285 case HELP_SEND: 286 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] " 287 "<snapshot>\n" 288 "\tsend [-Le] [-i snapshot|bookmark] " 289 "<filesystem|volume|snapshot>\n" 290 "\tsend [-nvPe] -t <receive_resume_token>\n")); 291 case HELP_SET: 292 return (gettext("\tset <property=value> ... " 293 "<filesystem|volume|snapshot> ...\n")); 294 case HELP_SHARE: 295 return (gettext("\tshare <-a | filesystem>\n")); 296 case HELP_SNAPSHOT: 297 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... " 298 "<filesystem|volume>@<snap> ...\n")); 299 case HELP_UNMOUNT: 300 return (gettext("\tunmount|umount [-f] " 301 "<-a | filesystem|mountpoint>\n")); 302 case HELP_UNSHARE: 303 return (gettext("\tunshare " 304 "<-a | filesystem|mountpoint>\n")); 305 case HELP_ALLOW: 306 return (gettext("\tallow <filesystem|volume>\n" 307 "\tallow [-ldug] " 308 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n" 309 "\t <filesystem|volume>\n" 310 "\tallow [-ld] -e <perm|@setname>[,...] " 311 "<filesystem|volume>\n" 312 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n" 313 "\tallow -s @setname <perm|@setname>[,...] " 314 "<filesystem|volume>\n")); 315 case HELP_UNALLOW: 316 return (gettext("\tunallow [-rldug] " 317 "<\"everyone\"|user|group>[,...]\n" 318 "\t [<perm|@setname>[,...]] <filesystem|volume>\n" 319 "\tunallow [-rld] -e [<perm|@setname>[,...]] " 320 "<filesystem|volume>\n" 321 "\tunallow [-r] -c [<perm|@setname>[,...]] " 322 "<filesystem|volume>\n" 323 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] " 324 "<filesystem|volume>\n")); 325 case HELP_USERSPACE: 326 return (gettext("\tuserspace [-Hinp] [-o field[,...]] " 327 "[-s field] ...\n" 328 "\t [-S field] ... [-t type[,...]] " 329 "<filesystem|snapshot>\n")); 330 case HELP_GROUPSPACE: 331 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] " 332 "[-s field] ...\n" 333 "\t [-S field] ... [-t type[,...]] " 334 "<filesystem|snapshot>\n")); 335 case HELP_HOLD: 336 return (gettext("\thold [-r] <tag> <snapshot> ...\n")); 337 case HELP_HOLDS: 338 return (gettext("\tholds [-Hp] [-r|-d depth] " 339 "<filesystem|volume|snapshot> ...\n")); 340 case HELP_RELEASE: 341 return (gettext("\trelease [-r] <tag> <snapshot> ...\n")); 342 case HELP_DIFF: 343 return (gettext("\tdiff [-FHt] <snapshot> " 344 "[snapshot|filesystem]\n")); 345 case HELP_BOOKMARK: 346 return (gettext("\tbookmark <snapshot> <bookmark>\n")); 347 case HELP_CHANNEL_PROGRAM: 348 return (gettext("\tprogram [-n] [-t <instruction limit>] " 349 "[-m <memory limit (b)>] <pool> <program file> " 350 "[lua args...]\n")); 351 } 352 353 abort(); 354 /* NOTREACHED */ 355} 356 357void 358nomem(void) 359{ 360 (void) fprintf(stderr, gettext("internal error: out of memory\n")); 361 exit(1); 362} 363 364/* 365 * Utility function to guarantee malloc() success. 366 */ 367 368void * 369safe_malloc(size_t size) 370{ 371 void *data; 372 373 if ((data = calloc(1, size)) == NULL) 374 nomem(); 375 376 return (data); 377} 378 379void * 380safe_realloc(void *data, size_t size) 381{ 382 void *newp; 383 if ((newp = realloc(data, size)) == NULL) { 384 free(data); 385 nomem(); 386 } 387 388 return (newp); 389} 390 391static char * 392safe_strdup(char *str) 393{ 394 char *dupstr = strdup(str); 395 396 if (dupstr == NULL) 397 nomem(); 398 399 return (dupstr); 400} 401 402/* 403 * Callback routine that will print out information for each of 404 * the properties. 405 */ 406static int 407usage_prop_cb(int prop, void *cb) 408{ 409 FILE *fp = cb; 410 411 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop)); 412 413 if (zfs_prop_readonly(prop)) 414 (void) fprintf(fp, " NO "); 415 else 416 (void) fprintf(fp, "YES "); 417 418 if (zfs_prop_inheritable(prop)) 419 (void) fprintf(fp, " YES "); 420 else 421 (void) fprintf(fp, " NO "); 422 423 if (zfs_prop_values(prop) == NULL) 424 (void) fprintf(fp, "-\n"); 425 else 426 (void) fprintf(fp, "%s\n", zfs_prop_values(prop)); 427 428 return (ZPROP_CONT); 429} 430 431/* 432 * Display usage message. If we're inside a command, display only the usage for 433 * that command. Otherwise, iterate over the entire command table and display 434 * a complete usage message. 435 */ 436static void 437usage(boolean_t requested) 438{ 439 int i; 440 boolean_t show_properties = B_FALSE; 441 FILE *fp = requested ? stdout : stderr; 442 443 if (current_command == NULL) { 444 445 (void) fprintf(fp, gettext("usage: zfs command args ...\n")); 446 (void) fprintf(fp, 447 gettext("where 'command' is one of the following:\n\n")); 448 449 for (i = 0; i < NCOMMAND; i++) { 450 if (command_table[i].name == NULL) 451 (void) fprintf(fp, "\n"); 452 else 453 (void) fprintf(fp, "%s", 454 get_usage(command_table[i].usage)); 455 } 456 457 (void) fprintf(fp, gettext("\nEach dataset is of the form: " 458 "pool/[dataset/]*dataset[@name]\n")); 459 } else { 460 (void) fprintf(fp, gettext("usage:\n")); 461 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 462 } 463 464 if (current_command != NULL && 465 (strcmp(current_command->name, "set") == 0 || 466 strcmp(current_command->name, "get") == 0 || 467 strcmp(current_command->name, "inherit") == 0 || 468 strcmp(current_command->name, "list") == 0)) 469 show_properties = B_TRUE; 470 471 if (show_properties) { 472 (void) fprintf(fp, 473 gettext("\nThe following properties are supported:\n")); 474 475 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n", 476 "PROPERTY", "EDIT", "INHERIT", "VALUES"); 477 478 /* Iterate over all properties */ 479 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE, 480 ZFS_TYPE_DATASET); 481 482 (void) fprintf(fp, "\t%-15s ", "userused@..."); 483 (void) fprintf(fp, " NO NO <size>\n"); 484 (void) fprintf(fp, "\t%-15s ", "groupused@..."); 485 (void) fprintf(fp, " NO NO <size>\n"); 486 (void) fprintf(fp, "\t%-15s ", "userquota@..."); 487 (void) fprintf(fp, "YES NO <size> | none\n"); 488 (void) fprintf(fp, "\t%-15s ", "groupquota@..."); 489 (void) fprintf(fp, "YES NO <size> | none\n"); 490 (void) fprintf(fp, "\t%-15s ", "written@<snap>"); 491 (void) fprintf(fp, " NO NO <size>\n"); 492 493 (void) fprintf(fp, gettext("\nSizes are specified in bytes " 494 "with standard units such as K, M, G, etc.\n")); 495 (void) fprintf(fp, gettext("\nUser-defined properties can " 496 "be specified by using a name containing a colon (:).\n")); 497 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ " 498 "properties must be appended with\n" 499 "a user or group specifier of one of these forms:\n" 500 " POSIX name (eg: \"matt\")\n" 501 " POSIX id (eg: \"126829\")\n" 502 " SMB name@domain (eg: \"matt@sun\")\n" 503 " SMB SID (eg: \"S-1-234-567-89\")\n")); 504 } else { 505 (void) fprintf(fp, 506 gettext("\nFor the property list, run: %s\n"), 507 "zfs set|get"); 508 (void) fprintf(fp, 509 gettext("\nFor the delegated permission list, run: %s\n"), 510 "zfs allow|unallow"); 511 } 512 513 /* 514 * See comments at end of main(). 515 */ 516 if (getenv("ZFS_ABORT") != NULL) { 517 (void) printf("dumping core by request\n"); 518 abort(); 519 } 520 521 exit(requested ? 0 : 2); 522} 523 524/* 525 * Take a property=value argument string and add it to the given nvlist. 526 * Modifies the argument inplace. 527 */ 528static int 529parseprop(nvlist_t *props, char *propname) 530{ 531 char *propval, *strval; 532 533 if ((propval = strchr(propname, '=')) == NULL) { 534 (void) fprintf(stderr, gettext("missing " 535 "'=' for property=value argument\n")); 536 return (-1); 537 } 538 *propval = '\0'; 539 propval++; 540 if (nvlist_lookup_string(props, propname, &strval) == 0) { 541 (void) fprintf(stderr, gettext("property '%s' " 542 "specified multiple times\n"), propname); 543 return (-1); 544 } 545 if (nvlist_add_string(props, propname, propval) != 0) 546 nomem(); 547 return (0); 548} 549 550static int 551parse_depth(char *opt, int *flags) 552{ 553 char *tmp; 554 int depth; 555 556 depth = (int)strtol(opt, &tmp, 0); 557 if (*tmp) { 558 (void) fprintf(stderr, 559 gettext("%s is not an integer\n"), opt); 560 usage(B_FALSE); 561 } 562 if (depth < 0) { 563 (void) fprintf(stderr, 564 gettext("Depth can not be negative.\n")); 565 usage(B_FALSE); 566 } 567 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE); 568 return (depth); 569} 570 571#define PROGRESS_DELAY 2 /* seconds */ 572 573static char *pt_reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; 574static time_t pt_begin; 575static char *pt_header = NULL; 576static boolean_t pt_shown; 577 578static void 579start_progress_timer(void) 580{ 581 pt_begin = time(NULL) + PROGRESS_DELAY; 582 pt_shown = B_FALSE; 583} 584 585static void 586set_progress_header(char *header) 587{ 588 assert(pt_header == NULL); 589 pt_header = safe_strdup(header); 590 if (pt_shown) { 591 (void) printf("%s: ", header); 592 (void) fflush(stdout); 593 } 594} 595 596static void 597update_progress(char *update) 598{ 599 if (!pt_shown && time(NULL) > pt_begin) { 600 int len = strlen(update); 601 602 (void) printf("%s: %s%*.*s", pt_header, update, len, len, 603 pt_reverse); 604 (void) fflush(stdout); 605 pt_shown = B_TRUE; 606 } else if (pt_shown) { 607 int len = strlen(update); 608 609 (void) printf("%s%*.*s", update, len, len, pt_reverse); 610 (void) fflush(stdout); 611 } 612} 613 614static void 615finish_progress(char *done) 616{ 617 if (pt_shown) { 618 (void) printf("%s\n", done); 619 (void) fflush(stdout); 620 } 621 free(pt_header); 622 pt_header = NULL; 623} 624 625/* 626 * Check if the dataset is mountable and should be automatically mounted. 627 */ 628static boolean_t 629should_auto_mount(zfs_handle_t *zhp) 630{ 631 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp))) 632 return (B_FALSE); 633 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON); 634} 635 636/* 637 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol> 638 * 639 * Given an existing dataset, create a writable copy whose initial contents 640 * are the same as the source. The newly created dataset maintains a 641 * dependency on the original; the original cannot be destroyed so long as 642 * the clone exists. 643 * 644 * The '-p' flag creates all the non-existing ancestors of the target first. 645 */ 646static int 647zfs_do_clone(int argc, char **argv) 648{ 649 zfs_handle_t *zhp = NULL; 650 boolean_t parents = B_FALSE; 651 nvlist_t *props; 652 int ret = 0; 653 int c; 654 655 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 656 nomem(); 657 658 /* check options */ 659 while ((c = getopt(argc, argv, "o:p")) != -1) { 660 switch (c) { 661 case 'o': 662 if (parseprop(props, optarg) != 0) 663 return (1); 664 break; 665 case 'p': 666 parents = B_TRUE; 667 break; 668 case '?': 669 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 670 optopt); 671 goto usage; 672 } 673 } 674 675 argc -= optind; 676 argv += optind; 677 678 /* check number of arguments */ 679 if (argc < 1) { 680 (void) fprintf(stderr, gettext("missing source dataset " 681 "argument\n")); 682 goto usage; 683 } 684 if (argc < 2) { 685 (void) fprintf(stderr, gettext("missing target dataset " 686 "argument\n")); 687 goto usage; 688 } 689 if (argc > 2) { 690 (void) fprintf(stderr, gettext("too many arguments\n")); 691 goto usage; 692 } 693 694 /* open the source dataset */ 695 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 696 return (1); 697 698 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM | 699 ZFS_TYPE_VOLUME)) { 700 /* 701 * Now create the ancestors of the target dataset. If the 702 * target already exists and '-p' option was used we should not 703 * complain. 704 */ 705 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | 706 ZFS_TYPE_VOLUME)) 707 return (0); 708 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) 709 return (1); 710 } 711 712 /* pass to libzfs */ 713 ret = zfs_clone(zhp, argv[1], props); 714 715 /* create the mountpoint if necessary */ 716 if (ret == 0) { 717 zfs_handle_t *clone; 718 719 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET); 720 if (clone != NULL) { 721 /* 722 * If the user doesn't want the dataset 723 * automatically mounted, then skip the mount/share 724 * step. 725 */ 726 if (should_auto_mount(clone)) { 727 if ((ret = zfs_mount(clone, NULL, 0)) != 0) { 728 (void) fprintf(stderr, gettext("clone " 729 "successfully created, " 730 "but not mounted\n")); 731 } else if ((ret = zfs_share(clone)) != 0) { 732 (void) fprintf(stderr, gettext("clone " 733 "successfully created, " 734 "but not shared\n")); 735 } 736 } 737 zfs_close(clone); 738 } 739 } 740 741 zfs_close(zhp); 742 nvlist_free(props); 743 744 return (!!ret); 745 746usage: 747 if (zhp) 748 zfs_close(zhp); 749 nvlist_free(props); 750 usage(B_FALSE); 751 return (-1); 752} 753 754/* 755 * zfs create [-pu] [-o prop=value] ... fs 756 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size 757 * 758 * Create a new dataset. This command can be used to create filesystems 759 * and volumes. Snapshot creation is handled by 'zfs snapshot'. 760 * For volumes, the user must specify a size to be used. 761 * 762 * The '-s' flag applies only to volumes, and indicates that we should not try 763 * to set the reservation for this volume. By default we set a reservation 764 * equal to the size for any volume. For pools with SPA_VERSION >= 765 * SPA_VERSION_REFRESERVATION, we set a refreservation instead. 766 * 767 * The '-p' flag creates all the non-existing ancestors of the target first. 768 * 769 * The '-u' flag prevents mounting of newly created file system. 770 */ 771static int 772zfs_do_create(int argc, char **argv) 773{ 774 zfs_type_t type = ZFS_TYPE_FILESYSTEM; 775 zfs_handle_t *zhp = NULL; 776 uint64_t volsize = 0; 777 int c; 778 boolean_t noreserve = B_FALSE; 779 boolean_t bflag = B_FALSE; 780 boolean_t parents = B_FALSE; 781 boolean_t nomount = B_FALSE; 782 int ret = 1; 783 nvlist_t *props; 784 uint64_t intval; 785 786 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 787 nomem(); 788 789 /* check options */ 790 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) { 791 switch (c) { 792 case 'V': 793 type = ZFS_TYPE_VOLUME; 794 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 795 (void) fprintf(stderr, gettext("bad volume " 796 "size '%s': %s\n"), optarg, 797 libzfs_error_description(g_zfs)); 798 goto error; 799 } 800 801 if (nvlist_add_uint64(props, 802 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0) 803 nomem(); 804 volsize = intval; 805 break; 806 case 'p': 807 parents = B_TRUE; 808 break; 809 case 'b': 810 bflag = B_TRUE; 811 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 812 (void) fprintf(stderr, gettext("bad volume " 813 "block size '%s': %s\n"), optarg, 814 libzfs_error_description(g_zfs)); 815 goto error; 816 } 817 818 if (nvlist_add_uint64(props, 819 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 820 intval) != 0) 821 nomem(); 822 break; 823 case 'o': 824 if (parseprop(props, optarg) != 0) 825 goto error; 826 break; 827 case 's': 828 noreserve = B_TRUE; 829 break; 830 case 'u': 831 nomount = B_TRUE; 832 break; 833 case ':': 834 (void) fprintf(stderr, gettext("missing size " 835 "argument\n")); 836 goto badusage; 837 case '?': 838 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 839 optopt); 840 goto badusage; 841 } 842 } 843 844 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) { 845 (void) fprintf(stderr, gettext("'-s' and '-b' can only be " 846 "used when creating a volume\n")); 847 goto badusage; 848 } 849 if (nomount && type != ZFS_TYPE_FILESYSTEM) { 850 (void) fprintf(stderr, gettext("'-u' can only be " 851 "used when creating a file system\n")); 852 goto badusage; 853 } 854 855 argc -= optind; 856 argv += optind; 857 858 /* check number of arguments */ 859 if (argc == 0) { 860 (void) fprintf(stderr, gettext("missing %s argument\n"), 861 zfs_type_to_name(type)); 862 goto badusage; 863 } 864 if (argc > 1) { 865 (void) fprintf(stderr, gettext("too many arguments\n")); 866 goto badusage; 867 } 868 869 if (type == ZFS_TYPE_VOLUME && !noreserve) { 870 zpool_handle_t *zpool_handle; 871 nvlist_t *real_props = NULL; 872 uint64_t spa_version; 873 char *p; 874 zfs_prop_t resv_prop; 875 char *strval; 876 char msg[1024]; 877 878 if ((p = strchr(argv[0], '/')) != NULL) 879 *p = '\0'; 880 zpool_handle = zpool_open(g_zfs, argv[0]); 881 if (p != NULL) 882 *p = '/'; 883 if (zpool_handle == NULL) 884 goto error; 885 spa_version = zpool_get_prop_int(zpool_handle, 886 ZPOOL_PROP_VERSION, NULL); 887 if (spa_version >= SPA_VERSION_REFRESERVATION) 888 resv_prop = ZFS_PROP_REFRESERVATION; 889 else 890 resv_prop = ZFS_PROP_RESERVATION; 891 892 (void) snprintf(msg, sizeof (msg), 893 gettext("cannot create '%s'"), argv[0]); 894 if (props && (real_props = zfs_valid_proplist(g_zfs, type, 895 props, 0, NULL, zpool_handle, msg)) == NULL) { 896 zpool_close(zpool_handle); 897 goto error; 898 } 899 zpool_close(zpool_handle); 900 901 volsize = zvol_volsize_to_reservation(volsize, real_props); 902 nvlist_free(real_props); 903 904 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop), 905 &strval) != 0) { 906 if (nvlist_add_uint64(props, 907 zfs_prop_to_name(resv_prop), volsize) != 0) { 908 nvlist_free(props); 909 nomem(); 910 } 911 } 912 } 913 914 if (parents && zfs_name_valid(argv[0], type)) { 915 /* 916 * Now create the ancestors of target dataset. If the target 917 * already exists and '-p' option was used we should not 918 * complain. 919 */ 920 if (zfs_dataset_exists(g_zfs, argv[0], type)) { 921 ret = 0; 922 goto error; 923 } 924 if (zfs_create_ancestors(g_zfs, argv[0]) != 0) 925 goto error; 926 } 927 928 /* pass to libzfs */ 929 if (zfs_create(g_zfs, argv[0], type, props) != 0) 930 goto error; 931 932 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 933 goto error; 934 935 ret = 0; 936 937 /* 938 * Mount and/or share the new filesystem as appropriate. We provide a 939 * verbose error message to let the user know that their filesystem was 940 * in fact created, even if we failed to mount or share it. 941 * If the user doesn't want the dataset automatically mounted, 942 * then skip the mount/share step altogether. 943 */ 944 if (!nomount && should_auto_mount(zhp)) { 945 if (zfs_mount(zhp, NULL, 0) != 0) { 946 (void) fprintf(stderr, gettext("filesystem " 947 "successfully created, but not mounted\n")); 948 ret = 1; 949 } else if (zfs_share(zhp) != 0) { 950 (void) fprintf(stderr, gettext("filesystem " 951 "successfully created, but not shared\n")); 952 ret = 1; 953 } 954 } 955 956error: 957 if (zhp) 958 zfs_close(zhp); 959 nvlist_free(props); 960 return (ret); 961badusage: 962 nvlist_free(props); 963 usage(B_FALSE); 964 return (2); 965} 966 967/* 968 * zfs destroy [-rRf] <fs, vol> 969 * zfs destroy [-rRd] <snap> 970 * 971 * -r Recursively destroy all children 972 * -R Recursively destroy all dependents, including clones 973 * -f Force unmounting of any dependents 974 * -d If we can't destroy now, mark for deferred destruction 975 * 976 * Destroys the given dataset. By default, it will unmount any filesystems, 977 * and refuse to destroy a dataset that has any dependents. A dependent can 978 * either be a child, or a clone of a child. 979 */ 980typedef struct destroy_cbdata { 981 boolean_t cb_first; 982 boolean_t cb_force; 983 boolean_t cb_recurse; 984 boolean_t cb_error; 985 boolean_t cb_doclones; 986 zfs_handle_t *cb_target; 987 boolean_t cb_defer_destroy; 988 boolean_t cb_verbose; 989 boolean_t cb_parsable; 990 boolean_t cb_dryrun; 991 nvlist_t *cb_nvl; 992 nvlist_t *cb_batchedsnaps; 993 994 /* first snap in contiguous run */ 995 char *cb_firstsnap; 996 /* previous snap in contiguous run */ 997 char *cb_prevsnap; 998 int64_t cb_snapused; 999 char *cb_snapspec; 1000 char *cb_bookmark; 1001} destroy_cbdata_t; 1002 1003/* 1004 * Check for any dependents based on the '-r' or '-R' flags. 1005 */ 1006static int 1007destroy_check_dependent(zfs_handle_t *zhp, void *data) 1008{ 1009 destroy_cbdata_t *cbp = data; 1010 const char *tname = zfs_get_name(cbp->cb_target); 1011 const char *name = zfs_get_name(zhp); 1012 1013 if (strncmp(tname, name, strlen(tname)) == 0 && 1014 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) { 1015 /* 1016 * This is a direct descendant, not a clone somewhere else in 1017 * the hierarchy. 1018 */ 1019 if (cbp->cb_recurse) 1020 goto out; 1021 1022 if (cbp->cb_first) { 1023 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1024 "%s has children\n"), 1025 zfs_get_name(cbp->cb_target), 1026 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 1027 (void) fprintf(stderr, gettext("use '-r' to destroy " 1028 "the following datasets:\n")); 1029 cbp->cb_first = B_FALSE; 1030 cbp->cb_error = B_TRUE; 1031 } 1032 1033 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 1034 } else { 1035 /* 1036 * This is a clone. We only want to report this if the '-r' 1037 * wasn't specified, or the target is a snapshot. 1038 */ 1039 if (!cbp->cb_recurse && 1040 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT) 1041 goto out; 1042 1043 if (cbp->cb_first) { 1044 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1045 "%s has dependent clones\n"), 1046 zfs_get_name(cbp->cb_target), 1047 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 1048 (void) fprintf(stderr, gettext("use '-R' to destroy " 1049 "the following datasets:\n")); 1050 cbp->cb_first = B_FALSE; 1051 cbp->cb_error = B_TRUE; 1052 cbp->cb_dryrun = B_TRUE; 1053 } 1054 1055 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 1056 } 1057 1058out: 1059 zfs_close(zhp); 1060 return (0); 1061} 1062 1063static int 1064destroy_callback(zfs_handle_t *zhp, void *data) 1065{ 1066 destroy_cbdata_t *cb = data; 1067 const char *name = zfs_get_name(zhp); 1068 1069 if (cb->cb_verbose) { 1070 if (cb->cb_parsable) { 1071 (void) printf("destroy\t%s\n", name); 1072 } else if (cb->cb_dryrun) { 1073 (void) printf(gettext("would destroy %s\n"), 1074 name); 1075 } else { 1076 (void) printf(gettext("will destroy %s\n"), 1077 name); 1078 } 1079 } 1080 1081 /* 1082 * Ignore pools (which we've already flagged as an error before getting 1083 * here). 1084 */ 1085 if (strchr(zfs_get_name(zhp), '/') == NULL && 1086 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1087 zfs_close(zhp); 1088 return (0); 1089 } 1090 if (cb->cb_dryrun) { 1091 zfs_close(zhp); 1092 return (0); 1093 } 1094 1095 /* 1096 * We batch up all contiguous snapshots (even of different 1097 * filesystems) and destroy them with one ioctl. We can't 1098 * simply do all snap deletions and then all fs deletions, 1099 * because we must delete a clone before its origin. 1100 */ 1101 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) { 1102 fnvlist_add_boolean(cb->cb_batchedsnaps, name); 1103 } else { 1104 int error = zfs_destroy_snaps_nvl(g_zfs, 1105 cb->cb_batchedsnaps, B_FALSE); 1106 fnvlist_free(cb->cb_batchedsnaps); 1107 cb->cb_batchedsnaps = fnvlist_alloc(); 1108 1109 if (error != 0 || 1110 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 || 1111 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) { 1112 zfs_close(zhp); 1113 return (-1); 1114 } 1115 } 1116 1117 zfs_close(zhp); 1118 return (0); 1119} 1120 1121static int 1122destroy_print_cb(zfs_handle_t *zhp, void *arg) 1123{ 1124 destroy_cbdata_t *cb = arg; 1125 const char *name = zfs_get_name(zhp); 1126 int err = 0; 1127 1128 if (nvlist_exists(cb->cb_nvl, name)) { 1129 if (cb->cb_firstsnap == NULL) 1130 cb->cb_firstsnap = strdup(name); 1131 if (cb->cb_prevsnap != NULL) 1132 free(cb->cb_prevsnap); 1133 /* this snap continues the current range */ 1134 cb->cb_prevsnap = strdup(name); 1135 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL) 1136 nomem(); 1137 if (cb->cb_verbose) { 1138 if (cb->cb_parsable) { 1139 (void) printf("destroy\t%s\n", name); 1140 } else if (cb->cb_dryrun) { 1141 (void) printf(gettext("would destroy %s\n"), 1142 name); 1143 } else { 1144 (void) printf(gettext("will destroy %s\n"), 1145 name); 1146 } 1147 } 1148 } else if (cb->cb_firstsnap != NULL) { 1149 /* end of this range */ 1150 uint64_t used = 0; 1151 err = lzc_snaprange_space(cb->cb_firstsnap, 1152 cb->cb_prevsnap, &used); 1153 cb->cb_snapused += used; 1154 free(cb->cb_firstsnap); 1155 cb->cb_firstsnap = NULL; 1156 free(cb->cb_prevsnap); 1157 cb->cb_prevsnap = NULL; 1158 } 1159 zfs_close(zhp); 1160 return (err); 1161} 1162 1163static int 1164destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb) 1165{ 1166 int err = 0; 1167 assert(cb->cb_firstsnap == NULL); 1168 assert(cb->cb_prevsnap == NULL); 1169 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb); 1170 if (cb->cb_firstsnap != NULL) { 1171 uint64_t used = 0; 1172 if (err == 0) { 1173 err = lzc_snaprange_space(cb->cb_firstsnap, 1174 cb->cb_prevsnap, &used); 1175 } 1176 cb->cb_snapused += used; 1177 free(cb->cb_firstsnap); 1178 cb->cb_firstsnap = NULL; 1179 free(cb->cb_prevsnap); 1180 cb->cb_prevsnap = NULL; 1181 } 1182 return (err); 1183} 1184 1185static int 1186snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg) 1187{ 1188 destroy_cbdata_t *cb = arg; 1189 int err = 0; 1190 1191 /* Check for clones. */ 1192 if (!cb->cb_doclones && !cb->cb_defer_destroy) { 1193 cb->cb_target = zhp; 1194 cb->cb_first = B_TRUE; 1195 err = zfs_iter_dependents(zhp, B_TRUE, 1196 destroy_check_dependent, cb); 1197 } 1198 1199 if (err == 0) { 1200 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp))) 1201 nomem(); 1202 } 1203 zfs_close(zhp); 1204 return (err); 1205} 1206 1207static int 1208gather_snapshots(zfs_handle_t *zhp, void *arg) 1209{ 1210 destroy_cbdata_t *cb = arg; 1211 int err = 0; 1212 1213 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb); 1214 if (err == ENOENT) 1215 err = 0; 1216 if (err != 0) 1217 goto out; 1218 1219 if (cb->cb_verbose) { 1220 err = destroy_print_snapshots(zhp, cb); 1221 if (err != 0) 1222 goto out; 1223 } 1224 1225 if (cb->cb_recurse) 1226 err = zfs_iter_filesystems(zhp, gather_snapshots, cb); 1227 1228out: 1229 zfs_close(zhp); 1230 return (err); 1231} 1232 1233static int 1234destroy_clones(destroy_cbdata_t *cb) 1235{ 1236 nvpair_t *pair; 1237 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL); 1238 pair != NULL; 1239 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) { 1240 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair), 1241 ZFS_TYPE_SNAPSHOT); 1242 if (zhp != NULL) { 1243 boolean_t defer = cb->cb_defer_destroy; 1244 int err = 0; 1245 1246 /* 1247 * We can't defer destroy non-snapshots, so set it to 1248 * false while destroying the clones. 1249 */ 1250 cb->cb_defer_destroy = B_FALSE; 1251 err = zfs_iter_dependents(zhp, B_FALSE, 1252 destroy_callback, cb); 1253 cb->cb_defer_destroy = defer; 1254 zfs_close(zhp); 1255 if (err != 0) 1256 return (err); 1257 } 1258 } 1259 return (0); 1260} 1261 1262static int 1263zfs_do_destroy(int argc, char **argv) 1264{ 1265 destroy_cbdata_t cb = { 0 }; 1266 int rv = 0; 1267 int err = 0; 1268 int c; 1269 zfs_handle_t *zhp = NULL; 1270 char *at, *pound; 1271 zfs_type_t type = ZFS_TYPE_DATASET; 1272 1273 /* check options */ 1274 while ((c = getopt(argc, argv, "vpndfrR")) != -1) { 1275 switch (c) { 1276 case 'v': 1277 cb.cb_verbose = B_TRUE; 1278 break; 1279 case 'p': 1280 cb.cb_verbose = B_TRUE; 1281 cb.cb_parsable = B_TRUE; 1282 break; 1283 case 'n': 1284 cb.cb_dryrun = B_TRUE; 1285 break; 1286 case 'd': 1287 cb.cb_defer_destroy = B_TRUE; 1288 type = ZFS_TYPE_SNAPSHOT; 1289 break; 1290 case 'f': 1291 cb.cb_force = B_TRUE; 1292 break; 1293 case 'r': 1294 cb.cb_recurse = B_TRUE; 1295 break; 1296 case 'R': 1297 cb.cb_recurse = B_TRUE; 1298 cb.cb_doclones = B_TRUE; 1299 break; 1300 case '?': 1301 default: 1302 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1303 optopt); 1304 usage(B_FALSE); 1305 } 1306 } 1307 1308 argc -= optind; 1309 argv += optind; 1310 1311 /* check number of arguments */ 1312 if (argc == 0) { 1313 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1314 usage(B_FALSE); 1315 } 1316 if (argc > 1) { 1317 (void) fprintf(stderr, gettext("too many arguments\n")); 1318 usage(B_FALSE); 1319 } 1320 1321 at = strchr(argv[0], '@'); 1322 pound = strchr(argv[0], '#'); 1323 if (at != NULL) { 1324 1325 /* Build the list of snaps to destroy in cb_nvl. */ 1326 cb.cb_nvl = fnvlist_alloc(); 1327 1328 *at = '\0'; 1329 zhp = zfs_open(g_zfs, argv[0], 1330 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 1331 if (zhp == NULL) 1332 return (1); 1333 1334 cb.cb_snapspec = at + 1; 1335 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 || 1336 cb.cb_error) { 1337 rv = 1; 1338 goto out; 1339 } 1340 1341 if (nvlist_empty(cb.cb_nvl)) { 1342 (void) fprintf(stderr, gettext("could not find any " 1343 "snapshots to destroy; check snapshot names.\n")); 1344 rv = 1; 1345 goto out; 1346 } 1347 1348 if (cb.cb_verbose) { 1349 char buf[16]; 1350 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf)); 1351 if (cb.cb_parsable) { 1352 (void) printf("reclaim\t%llu\n", 1353 cb.cb_snapused); 1354 } else if (cb.cb_dryrun) { 1355 (void) printf(gettext("would reclaim %s\n"), 1356 buf); 1357 } else { 1358 (void) printf(gettext("will reclaim %s\n"), 1359 buf); 1360 } 1361 } 1362 1363 if (!cb.cb_dryrun) { 1364 if (cb.cb_doclones) { 1365 cb.cb_batchedsnaps = fnvlist_alloc(); 1366 err = destroy_clones(&cb); 1367 if (err == 0) { 1368 err = zfs_destroy_snaps_nvl(g_zfs, 1369 cb.cb_batchedsnaps, B_FALSE); 1370 } 1371 if (err != 0) { 1372 rv = 1; 1373 goto out; 1374 } 1375 } 1376 if (err == 0) { 1377 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl, 1378 cb.cb_defer_destroy); 1379 } 1380 } 1381 1382 if (err != 0) 1383 rv = 1; 1384 } else if (pound != NULL) { 1385 int err; 1386 nvlist_t *nvl; 1387 1388 if (cb.cb_dryrun) { 1389 (void) fprintf(stderr, 1390 "dryrun is not supported with bookmark\n"); 1391 return (-1); 1392 } 1393 1394 if (cb.cb_defer_destroy) { 1395 (void) fprintf(stderr, 1396 "defer destroy is not supported with bookmark\n"); 1397 return (-1); 1398 } 1399 1400 if (cb.cb_recurse) { 1401 (void) fprintf(stderr, 1402 "recursive is not supported with bookmark\n"); 1403 return (-1); 1404 } 1405 1406 if (!zfs_bookmark_exists(argv[0])) { 1407 (void) fprintf(stderr, gettext("bookmark '%s' " 1408 "does not exist.\n"), argv[0]); 1409 return (1); 1410 } 1411 1412 nvl = fnvlist_alloc(); 1413 fnvlist_add_boolean(nvl, argv[0]); 1414 1415 err = lzc_destroy_bookmarks(nvl, NULL); 1416 if (err != 0) { 1417 (void) zfs_standard_error(g_zfs, err, 1418 "cannot destroy bookmark"); 1419 } 1420 1421 nvlist_free(cb.cb_nvl); 1422 1423 return (err); 1424 } else { 1425 /* Open the given dataset */ 1426 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL) 1427 return (1); 1428 1429 cb.cb_target = zhp; 1430 1431 /* 1432 * Perform an explicit check for pools before going any further. 1433 */ 1434 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL && 1435 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1436 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1437 "operation does not apply to pools\n"), 1438 zfs_get_name(zhp)); 1439 (void) fprintf(stderr, gettext("use 'zfs destroy -r " 1440 "%s' to destroy all datasets in the pool\n"), 1441 zfs_get_name(zhp)); 1442 (void) fprintf(stderr, gettext("use 'zpool destroy %s' " 1443 "to destroy the pool itself\n"), zfs_get_name(zhp)); 1444 rv = 1; 1445 goto out; 1446 } 1447 1448 /* 1449 * Check for any dependents and/or clones. 1450 */ 1451 cb.cb_first = B_TRUE; 1452 if (!cb.cb_doclones && 1453 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent, 1454 &cb) != 0) { 1455 rv = 1; 1456 goto out; 1457 } 1458 1459 if (cb.cb_error) { 1460 rv = 1; 1461 goto out; 1462 } 1463 1464 cb.cb_batchedsnaps = fnvlist_alloc(); 1465 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, 1466 &cb) != 0) { 1467 rv = 1; 1468 goto out; 1469 } 1470 1471 /* 1472 * Do the real thing. The callback will close the 1473 * handle regardless of whether it succeeds or not. 1474 */ 1475 err = destroy_callback(zhp, &cb); 1476 zhp = NULL; 1477 if (err == 0) { 1478 err = zfs_destroy_snaps_nvl(g_zfs, 1479 cb.cb_batchedsnaps, cb.cb_defer_destroy); 1480 } 1481 if (err != 0) 1482 rv = 1; 1483 } 1484 1485out: 1486 fnvlist_free(cb.cb_batchedsnaps); 1487 fnvlist_free(cb.cb_nvl); 1488 if (zhp != NULL) 1489 zfs_close(zhp); 1490 return (rv); 1491} 1492 1493static boolean_t 1494is_recvd_column(zprop_get_cbdata_t *cbp) 1495{ 1496 int i; 1497 zfs_get_column_t col; 1498 1499 for (i = 0; i < ZFS_GET_NCOLS && 1500 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++) 1501 if (col == GET_COL_RECVD) 1502 return (B_TRUE); 1503 return (B_FALSE); 1504} 1505 1506/* 1507 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...] 1508 * < all | property[,property]... > < fs | snap | vol > ... 1509 * 1510 * -r recurse over any child datasets 1511 * -H scripted mode. Headers are stripped, and fields are separated 1512 * by tabs instead of spaces. 1513 * -o Set of fields to display. One of "name,property,value, 1514 * received,source". Default is "name,property,value,source". 1515 * "all" is an alias for all five. 1516 * -s Set of sources to allow. One of 1517 * "local,default,inherited,received,temporary,none". Default is 1518 * all six. 1519 * -p Display values in parsable (literal) format. 1520 * 1521 * Prints properties for the given datasets. The user can control which 1522 * columns to display as well as which property types to allow. 1523 */ 1524 1525/* 1526 * Invoked to display the properties for a single dataset. 1527 */ 1528static int 1529get_callback(zfs_handle_t *zhp, void *data) 1530{ 1531 char buf[ZFS_MAXPROPLEN]; 1532 char rbuf[ZFS_MAXPROPLEN]; 1533 zprop_source_t sourcetype; 1534 char source[ZFS_MAX_DATASET_NAME_LEN]; 1535 zprop_get_cbdata_t *cbp = data; 1536 nvlist_t *user_props = zfs_get_user_props(zhp); 1537 zprop_list_t *pl = cbp->cb_proplist; 1538 nvlist_t *propval; 1539 char *strval; 1540 char *sourceval; 1541 boolean_t received = is_recvd_column(cbp); 1542 1543 for (; pl != NULL; pl = pl->pl_next) { 1544 char *recvdval = NULL; 1545 /* 1546 * Skip the special fake placeholder. This will also skip over 1547 * the name property when 'all' is specified. 1548 */ 1549 if (pl->pl_prop == ZFS_PROP_NAME && 1550 pl == cbp->cb_proplist) 1551 continue; 1552 1553 if (pl->pl_prop != ZPROP_INVAL) { 1554 if (zfs_prop_get(zhp, pl->pl_prop, buf, 1555 sizeof (buf), &sourcetype, source, 1556 sizeof (source), 1557 cbp->cb_literal) != 0) { 1558 if (pl->pl_all) 1559 continue; 1560 if (!zfs_prop_valid_for_type(pl->pl_prop, 1561 ZFS_TYPE_DATASET)) { 1562 (void) fprintf(stderr, 1563 gettext("No such property '%s'\n"), 1564 zfs_prop_to_name(pl->pl_prop)); 1565 continue; 1566 } 1567 sourcetype = ZPROP_SRC_NONE; 1568 (void) strlcpy(buf, "-", sizeof (buf)); 1569 } 1570 1571 if (received && (zfs_prop_get_recvd(zhp, 1572 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf), 1573 cbp->cb_literal) == 0)) 1574 recvdval = rbuf; 1575 1576 zprop_print_one_property(zfs_get_name(zhp), cbp, 1577 zfs_prop_to_name(pl->pl_prop), 1578 buf, sourcetype, source, recvdval); 1579 } else if (zfs_prop_userquota(pl->pl_user_prop)) { 1580 sourcetype = ZPROP_SRC_LOCAL; 1581 1582 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 1583 buf, sizeof (buf), cbp->cb_literal) != 0) { 1584 sourcetype = ZPROP_SRC_NONE; 1585 (void) strlcpy(buf, "-", sizeof (buf)); 1586 } 1587 1588 zprop_print_one_property(zfs_get_name(zhp), cbp, 1589 pl->pl_user_prop, buf, sourcetype, source, NULL); 1590 } else if (zfs_prop_written(pl->pl_user_prop)) { 1591 sourcetype = ZPROP_SRC_LOCAL; 1592 1593 if (zfs_prop_get_written(zhp, pl->pl_user_prop, 1594 buf, sizeof (buf), cbp->cb_literal) != 0) { 1595 sourcetype = ZPROP_SRC_NONE; 1596 (void) strlcpy(buf, "-", sizeof (buf)); 1597 } 1598 1599 zprop_print_one_property(zfs_get_name(zhp), cbp, 1600 pl->pl_user_prop, buf, sourcetype, source, NULL); 1601 } else { 1602 if (nvlist_lookup_nvlist(user_props, 1603 pl->pl_user_prop, &propval) != 0) { 1604 if (pl->pl_all) 1605 continue; 1606 sourcetype = ZPROP_SRC_NONE; 1607 strval = "-"; 1608 } else { 1609 verify(nvlist_lookup_string(propval, 1610 ZPROP_VALUE, &strval) == 0); 1611 verify(nvlist_lookup_string(propval, 1612 ZPROP_SOURCE, &sourceval) == 0); 1613 1614 if (strcmp(sourceval, 1615 zfs_get_name(zhp)) == 0) { 1616 sourcetype = ZPROP_SRC_LOCAL; 1617 } else if (strcmp(sourceval, 1618 ZPROP_SOURCE_VAL_RECVD) == 0) { 1619 sourcetype = ZPROP_SRC_RECEIVED; 1620 } else { 1621 sourcetype = ZPROP_SRC_INHERITED; 1622 (void) strlcpy(source, 1623 sourceval, sizeof (source)); 1624 } 1625 } 1626 1627 if (received && (zfs_prop_get_recvd(zhp, 1628 pl->pl_user_prop, rbuf, sizeof (rbuf), 1629 cbp->cb_literal) == 0)) 1630 recvdval = rbuf; 1631 1632 zprop_print_one_property(zfs_get_name(zhp), cbp, 1633 pl->pl_user_prop, strval, sourcetype, 1634 source, recvdval); 1635 } 1636 } 1637 1638 return (0); 1639} 1640 1641static int 1642zfs_do_get(int argc, char **argv) 1643{ 1644 zprop_get_cbdata_t cb = { 0 }; 1645 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 1646 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK; 1647 char *value, *fields; 1648 int ret = 0; 1649 int limit = 0; 1650 zprop_list_t fake_name = { 0 }; 1651 1652 /* 1653 * Set up default columns and sources. 1654 */ 1655 cb.cb_sources = ZPROP_SRC_ALL; 1656 cb.cb_columns[0] = GET_COL_NAME; 1657 cb.cb_columns[1] = GET_COL_PROPERTY; 1658 cb.cb_columns[2] = GET_COL_VALUE; 1659 cb.cb_columns[3] = GET_COL_SOURCE; 1660 cb.cb_type = ZFS_TYPE_DATASET; 1661 1662 /* check options */ 1663 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) { 1664 switch (c) { 1665 case 'p': 1666 cb.cb_literal = B_TRUE; 1667 break; 1668 case 'd': 1669 limit = parse_depth(optarg, &flags); 1670 break; 1671 case 'r': 1672 flags |= ZFS_ITER_RECURSE; 1673 break; 1674 case 'H': 1675 cb.cb_scripted = B_TRUE; 1676 break; 1677 case ':': 1678 (void) fprintf(stderr, gettext("missing argument for " 1679 "'%c' option\n"), optopt); 1680 usage(B_FALSE); 1681 break; 1682 case 'o': 1683 /* 1684 * Process the set of columns to display. We zero out 1685 * the structure to give us a blank slate. 1686 */ 1687 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 1688 i = 0; 1689 while (*optarg != '\0') { 1690 static char *col_subopts[] = 1691 { "name", "property", "value", "received", 1692 "source", "all", NULL }; 1693 1694 if (i == ZFS_GET_NCOLS) { 1695 (void) fprintf(stderr, gettext("too " 1696 "many fields given to -o " 1697 "option\n")); 1698 usage(B_FALSE); 1699 } 1700 1701 switch (getsubopt(&optarg, col_subopts, 1702 &value)) { 1703 case 0: 1704 cb.cb_columns[i++] = GET_COL_NAME; 1705 break; 1706 case 1: 1707 cb.cb_columns[i++] = GET_COL_PROPERTY; 1708 break; 1709 case 2: 1710 cb.cb_columns[i++] = GET_COL_VALUE; 1711 break; 1712 case 3: 1713 cb.cb_columns[i++] = GET_COL_RECVD; 1714 flags |= ZFS_ITER_RECVD_PROPS; 1715 break; 1716 case 4: 1717 cb.cb_columns[i++] = GET_COL_SOURCE; 1718 break; 1719 case 5: 1720 if (i > 0) { 1721 (void) fprintf(stderr, 1722 gettext("\"all\" conflicts " 1723 "with specific fields " 1724 "given to -o option\n")); 1725 usage(B_FALSE); 1726 } 1727 cb.cb_columns[0] = GET_COL_NAME; 1728 cb.cb_columns[1] = GET_COL_PROPERTY; 1729 cb.cb_columns[2] = GET_COL_VALUE; 1730 cb.cb_columns[3] = GET_COL_RECVD; 1731 cb.cb_columns[4] = GET_COL_SOURCE; 1732 flags |= ZFS_ITER_RECVD_PROPS; 1733 i = ZFS_GET_NCOLS; 1734 break; 1735 default: 1736 (void) fprintf(stderr, 1737 gettext("invalid column name " 1738 "'%s'\n"), suboptarg); 1739 usage(B_FALSE); 1740 } 1741 } 1742 break; 1743 1744 case 's': 1745 cb.cb_sources = 0; 1746 while (*optarg != '\0') { 1747 static char *source_subopts[] = { 1748 "local", "default", "inherited", 1749 "received", "temporary", "none", 1750 NULL }; 1751 1752 switch (getsubopt(&optarg, source_subopts, 1753 &value)) { 1754 case 0: 1755 cb.cb_sources |= ZPROP_SRC_LOCAL; 1756 break; 1757 case 1: 1758 cb.cb_sources |= ZPROP_SRC_DEFAULT; 1759 break; 1760 case 2: 1761 cb.cb_sources |= ZPROP_SRC_INHERITED; 1762 break; 1763 case 3: 1764 cb.cb_sources |= ZPROP_SRC_RECEIVED; 1765 break; 1766 case 4: 1767 cb.cb_sources |= ZPROP_SRC_TEMPORARY; 1768 break; 1769 case 5: 1770 cb.cb_sources |= ZPROP_SRC_NONE; 1771 break; 1772 default: 1773 (void) fprintf(stderr, 1774 gettext("invalid source " 1775 "'%s'\n"), suboptarg); 1776 usage(B_FALSE); 1777 } 1778 } 1779 break; 1780 1781 case 't': 1782 types = 0; 1783 flags &= ~ZFS_ITER_PROP_LISTSNAPS; 1784 while (*optarg != '\0') { 1785 static char *type_subopts[] = { "filesystem", 1786 "volume", "snapshot", "bookmark", 1787 "all", NULL }; 1788 1789 switch (getsubopt(&optarg, type_subopts, 1790 &value)) { 1791 case 0: 1792 types |= ZFS_TYPE_FILESYSTEM; 1793 break; 1794 case 1: 1795 types |= ZFS_TYPE_VOLUME; 1796 break; 1797 case 2: 1798 types |= ZFS_TYPE_SNAPSHOT; 1799 break; 1800 case 3: 1801 types |= ZFS_TYPE_BOOKMARK; 1802 break; 1803 case 4: 1804 types = ZFS_TYPE_DATASET | 1805 ZFS_TYPE_BOOKMARK; 1806 break; 1807 1808 default: 1809 (void) fprintf(stderr, 1810 gettext("invalid type '%s'\n"), 1811 suboptarg); 1812 usage(B_FALSE); 1813 } 1814 } 1815 break; 1816 1817 case '?': 1818 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1819 optopt); 1820 usage(B_FALSE); 1821 } 1822 } 1823 1824 argc -= optind; 1825 argv += optind; 1826 1827 if (argc < 1) { 1828 (void) fprintf(stderr, gettext("missing property " 1829 "argument\n")); 1830 usage(B_FALSE); 1831 } 1832 1833 fields = argv[0]; 1834 1835 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 1836 != 0) 1837 usage(B_FALSE); 1838 1839 argc--; 1840 argv++; 1841 1842 /* 1843 * As part of zfs_expand_proplist(), we keep track of the maximum column 1844 * width for each property. For the 'NAME' (and 'SOURCE') columns, we 1845 * need to know the maximum name length. However, the user likely did 1846 * not specify 'name' as one of the properties to fetch, so we need to 1847 * make sure we always include at least this property for 1848 * print_get_headers() to work properly. 1849 */ 1850 if (cb.cb_proplist != NULL) { 1851 fake_name.pl_prop = ZFS_PROP_NAME; 1852 fake_name.pl_width = strlen(gettext("NAME")); 1853 fake_name.pl_next = cb.cb_proplist; 1854 cb.cb_proplist = &fake_name; 1855 } 1856 1857 cb.cb_first = B_TRUE; 1858 1859 /* run for each object */ 1860 ret = zfs_for_each(argc, argv, flags, types, NULL, 1861 &cb.cb_proplist, limit, get_callback, &cb); 1862 1863 if (cb.cb_proplist == &fake_name) 1864 zprop_free_list(fake_name.pl_next); 1865 else 1866 zprop_free_list(cb.cb_proplist); 1867 1868 return (ret); 1869} 1870 1871/* 1872 * inherit [-rS] <property> <fs|vol> ... 1873 * 1874 * -r Recurse over all children 1875 * -S Revert to received value, if any 1876 * 1877 * For each dataset specified on the command line, inherit the given property 1878 * from its parent. Inheriting a property at the pool level will cause it to 1879 * use the default value. The '-r' flag will recurse over all children, and is 1880 * useful for setting a property on a hierarchy-wide basis, regardless of any 1881 * local modifications for each dataset. 1882 */ 1883 1884typedef struct inherit_cbdata { 1885 const char *cb_propname; 1886 boolean_t cb_received; 1887} inherit_cbdata_t; 1888 1889static int 1890inherit_recurse_cb(zfs_handle_t *zhp, void *data) 1891{ 1892 inherit_cbdata_t *cb = data; 1893 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname); 1894 1895 /* 1896 * If we're doing it recursively, then ignore properties that 1897 * are not valid for this type of dataset. 1898 */ 1899 if (prop != ZPROP_INVAL && 1900 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp))) 1901 return (0); 1902 1903 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1904} 1905 1906static int 1907inherit_cb(zfs_handle_t *zhp, void *data) 1908{ 1909 inherit_cbdata_t *cb = data; 1910 1911 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1912} 1913 1914static int 1915zfs_do_inherit(int argc, char **argv) 1916{ 1917 int c; 1918 zfs_prop_t prop; 1919 inherit_cbdata_t cb = { 0 }; 1920 char *propname; 1921 int ret = 0; 1922 int flags = 0; 1923 boolean_t received = B_FALSE; 1924 1925 /* check options */ 1926 while ((c = getopt(argc, argv, "rS")) != -1) { 1927 switch (c) { 1928 case 'r': 1929 flags |= ZFS_ITER_RECURSE; 1930 break; 1931 case 'S': 1932 received = B_TRUE; 1933 break; 1934 case '?': 1935 default: 1936 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1937 optopt); 1938 usage(B_FALSE); 1939 } 1940 } 1941 1942 argc -= optind; 1943 argv += optind; 1944 1945 /* check number of arguments */ 1946 if (argc < 1) { 1947 (void) fprintf(stderr, gettext("missing property argument\n")); 1948 usage(B_FALSE); 1949 } 1950 if (argc < 2) { 1951 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1952 usage(B_FALSE); 1953 } 1954 1955 propname = argv[0]; 1956 argc--; 1957 argv++; 1958 1959 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 1960 if (zfs_prop_readonly(prop)) { 1961 (void) fprintf(stderr, gettext( 1962 "%s property is read-only\n"), 1963 propname); 1964 return (1); 1965 } 1966 if (!zfs_prop_inheritable(prop) && !received) { 1967 (void) fprintf(stderr, gettext("'%s' property cannot " 1968 "be inherited\n"), propname); 1969 if (prop == ZFS_PROP_QUOTA || 1970 prop == ZFS_PROP_RESERVATION || 1971 prop == ZFS_PROP_REFQUOTA || 1972 prop == ZFS_PROP_REFRESERVATION) { 1973 (void) fprintf(stderr, gettext("use 'zfs set " 1974 "%s=none' to clear\n"), propname); 1975 (void) fprintf(stderr, gettext("use 'zfs " 1976 "inherit -S %s' to revert to received " 1977 "value\n"), propname); 1978 } 1979 return (1); 1980 } 1981 if (received && (prop == ZFS_PROP_VOLSIZE || 1982 prop == ZFS_PROP_VERSION)) { 1983 (void) fprintf(stderr, gettext("'%s' property cannot " 1984 "be reverted to a received value\n"), propname); 1985 return (1); 1986 } 1987 } else if (!zfs_prop_user(propname)) { 1988 (void) fprintf(stderr, gettext("invalid property '%s'\n"), 1989 propname); 1990 usage(B_FALSE); 1991 } 1992 1993 cb.cb_propname = propname; 1994 cb.cb_received = received; 1995 1996 if (flags & ZFS_ITER_RECURSE) { 1997 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 1998 NULL, NULL, 0, inherit_recurse_cb, &cb); 1999 } else { 2000 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 2001 NULL, NULL, 0, inherit_cb, &cb); 2002 } 2003 2004 return (ret); 2005} 2006 2007typedef struct upgrade_cbdata { 2008 uint64_t cb_numupgraded; 2009 uint64_t cb_numsamegraded; 2010 uint64_t cb_numfailed; 2011 uint64_t cb_version; 2012 boolean_t cb_newer; 2013 boolean_t cb_foundone; 2014 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN]; 2015} upgrade_cbdata_t; 2016 2017static int 2018same_pool(zfs_handle_t *zhp, const char *name) 2019{ 2020 int len1 = strcspn(name, "/@"); 2021 const char *zhname = zfs_get_name(zhp); 2022 int len2 = strcspn(zhname, "/@"); 2023 2024 if (len1 != len2) 2025 return (B_FALSE); 2026 return (strncmp(name, zhname, len1) == 0); 2027} 2028 2029static int 2030upgrade_list_callback(zfs_handle_t *zhp, void *data) 2031{ 2032 upgrade_cbdata_t *cb = data; 2033 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 2034 2035 /* list if it's old/new */ 2036 if ((!cb->cb_newer && version < ZPL_VERSION) || 2037 (cb->cb_newer && version > ZPL_VERSION)) { 2038 char *str; 2039 if (cb->cb_newer) { 2040 str = gettext("The following filesystems are " 2041 "formatted using a newer software version and\n" 2042 "cannot be accessed on the current system.\n\n"); 2043 } else { 2044 str = gettext("The following filesystems are " 2045 "out of date, and can be upgraded. After being\n" 2046 "upgraded, these filesystems (and any 'zfs send' " 2047 "streams generated from\n" 2048 "subsequent snapshots) will no longer be " 2049 "accessible by older software versions.\n\n"); 2050 } 2051 2052 if (!cb->cb_foundone) { 2053 (void) puts(str); 2054 (void) printf(gettext("VER FILESYSTEM\n")); 2055 (void) printf(gettext("--- ------------\n")); 2056 cb->cb_foundone = B_TRUE; 2057 } 2058 2059 (void) printf("%2u %s\n", version, zfs_get_name(zhp)); 2060 } 2061 2062 return (0); 2063} 2064 2065static int 2066upgrade_set_callback(zfs_handle_t *zhp, void *data) 2067{ 2068 upgrade_cbdata_t *cb = data; 2069 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 2070 int needed_spa_version; 2071 int spa_version; 2072 2073 if (zfs_spa_version(zhp, &spa_version) < 0) 2074 return (-1); 2075 2076 needed_spa_version = zfs_spa_version_map(cb->cb_version); 2077 2078 if (needed_spa_version < 0) 2079 return (-1); 2080 2081 if (spa_version < needed_spa_version) { 2082 /* can't upgrade */ 2083 (void) printf(gettext("%s: can not be " 2084 "upgraded; the pool version needs to first " 2085 "be upgraded\nto version %d\n\n"), 2086 zfs_get_name(zhp), needed_spa_version); 2087 cb->cb_numfailed++; 2088 return (0); 2089 } 2090 2091 /* upgrade */ 2092 if (version < cb->cb_version) { 2093 char verstr[16]; 2094 (void) snprintf(verstr, sizeof (verstr), 2095 "%llu", cb->cb_version); 2096 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) { 2097 /* 2098 * If they did "zfs upgrade -a", then we could 2099 * be doing ioctls to different pools. We need 2100 * to log this history once to each pool, and bypass 2101 * the normal history logging that happens in main(). 2102 */ 2103 (void) zpool_log_history(g_zfs, history_str); 2104 log_history = B_FALSE; 2105 } 2106 if (zfs_prop_set(zhp, "version", verstr) == 0) 2107 cb->cb_numupgraded++; 2108 else 2109 cb->cb_numfailed++; 2110 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp)); 2111 } else if (version > cb->cb_version) { 2112 /* can't downgrade */ 2113 (void) printf(gettext("%s: can not be downgraded; " 2114 "it is already at version %u\n"), 2115 zfs_get_name(zhp), version); 2116 cb->cb_numfailed++; 2117 } else { 2118 cb->cb_numsamegraded++; 2119 } 2120 return (0); 2121} 2122 2123/* 2124 * zfs upgrade 2125 * zfs upgrade -v 2126 * zfs upgrade [-r] [-V <version>] <-a | filesystem> 2127 */ 2128static int 2129zfs_do_upgrade(int argc, char **argv) 2130{ 2131 boolean_t all = B_FALSE; 2132 boolean_t showversions = B_FALSE; 2133 int ret = 0; 2134 upgrade_cbdata_t cb = { 0 }; 2135 int c; 2136 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 2137 2138 /* check options */ 2139 while ((c = getopt(argc, argv, "rvV:a")) != -1) { 2140 switch (c) { 2141 case 'r': 2142 flags |= ZFS_ITER_RECURSE; 2143 break; 2144 case 'v': 2145 showversions = B_TRUE; 2146 break; 2147 case 'V': 2148 if (zfs_prop_string_to_index(ZFS_PROP_VERSION, 2149 optarg, &cb.cb_version) != 0) { 2150 (void) fprintf(stderr, 2151 gettext("invalid version %s\n"), optarg); 2152 usage(B_FALSE); 2153 } 2154 break; 2155 case 'a': 2156 all = B_TRUE; 2157 break; 2158 case '?': 2159 default: 2160 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2161 optopt); 2162 usage(B_FALSE); 2163 } 2164 } 2165 2166 argc -= optind; 2167 argv += optind; 2168 2169 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version)) 2170 usage(B_FALSE); 2171 if (showversions && (flags & ZFS_ITER_RECURSE || all || 2172 cb.cb_version || argc)) 2173 usage(B_FALSE); 2174 if ((all || argc) && (showversions)) 2175 usage(B_FALSE); 2176 if (all && argc) 2177 usage(B_FALSE); 2178 2179 if (showversions) { 2180 /* Show info on available versions. */ 2181 (void) printf(gettext("The following filesystem versions are " 2182 "supported:\n\n")); 2183 (void) printf(gettext("VER DESCRIPTION\n")); 2184 (void) printf("--- -----------------------------------------" 2185 "---------------\n"); 2186 (void) printf(gettext(" 1 Initial ZFS filesystem version\n")); 2187 (void) printf(gettext(" 2 Enhanced directory entries\n")); 2188 (void) printf(gettext(" 3 Case insensitive and filesystem " 2189 "user identifier (FUID)\n")); 2190 (void) printf(gettext(" 4 userquota, groupquota " 2191 "properties\n")); 2192 (void) printf(gettext(" 5 System attributes\n")); 2193 (void) printf(gettext("\nFor more information on a particular " 2194 "version, including supported releases,\n")); 2195 (void) printf("see the ZFS Administration Guide.\n\n"); 2196 ret = 0; 2197 } else if (argc || all) { 2198 /* Upgrade filesystems */ 2199 if (cb.cb_version == 0) 2200 cb.cb_version = ZPL_VERSION; 2201 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM, 2202 NULL, NULL, 0, upgrade_set_callback, &cb); 2203 (void) printf(gettext("%llu filesystems upgraded\n"), 2204 cb.cb_numupgraded); 2205 if (cb.cb_numsamegraded) { 2206 (void) printf(gettext("%llu filesystems already at " 2207 "this version\n"), 2208 cb.cb_numsamegraded); 2209 } 2210 if (cb.cb_numfailed != 0) 2211 ret = 1; 2212 } else { 2213 /* List old-version filesytems */ 2214 boolean_t found; 2215 (void) printf(gettext("This system is currently running " 2216 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION); 2217 2218 flags |= ZFS_ITER_RECURSE; 2219 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 2220 NULL, NULL, 0, upgrade_list_callback, &cb); 2221 2222 found = cb.cb_foundone; 2223 cb.cb_foundone = B_FALSE; 2224 cb.cb_newer = B_TRUE; 2225 2226 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 2227 NULL, NULL, 0, upgrade_list_callback, &cb); 2228 2229 if (!cb.cb_foundone && !found) { 2230 (void) printf(gettext("All filesystems are " 2231 "formatted with the current version.\n")); 2232 } 2233 } 2234 2235 return (ret); 2236} 2237 2238/* 2239 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...] 2240 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot 2241 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...] 2242 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot 2243 * 2244 * -H Scripted mode; elide headers and separate columns by tabs. 2245 * -i Translate SID to POSIX ID. 2246 * -n Print numeric ID instead of user/group name. 2247 * -o Control which fields to display. 2248 * -p Use exact (parsable) numeric output. 2249 * -s Specify sort columns, descending order. 2250 * -S Specify sort columns, ascending order. 2251 * -t Control which object types to display. 2252 * 2253 * Displays space consumed by, and quotas on, each user in the specified 2254 * filesystem or snapshot. 2255 */ 2256 2257/* us_field_types, us_field_hdr and us_field_names should be kept in sync */ 2258enum us_field_types { 2259 USFIELD_TYPE, 2260 USFIELD_NAME, 2261 USFIELD_USED, 2262 USFIELD_QUOTA 2263}; 2264static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" }; 2265static char *us_field_names[] = { "type", "name", "used", "quota" }; 2266#define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *)) 2267 2268#define USTYPE_PSX_GRP (1 << 0) 2269#define USTYPE_PSX_USR (1 << 1) 2270#define USTYPE_SMB_GRP (1 << 2) 2271#define USTYPE_SMB_USR (1 << 3) 2272#define USTYPE_ALL \ 2273 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR) 2274 2275static int us_type_bits[] = { 2276 USTYPE_PSX_GRP, 2277 USTYPE_PSX_USR, 2278 USTYPE_SMB_GRP, 2279 USTYPE_SMB_USR, 2280 USTYPE_ALL 2281}; 2282static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup", 2283 "smbuser", "all" }; 2284 2285typedef struct us_node { 2286 nvlist_t *usn_nvl; 2287 uu_avl_node_t usn_avlnode; 2288 uu_list_node_t usn_listnode; 2289} us_node_t; 2290 2291typedef struct us_cbdata { 2292 nvlist_t **cb_nvlp; 2293 uu_avl_pool_t *cb_avl_pool; 2294 uu_avl_t *cb_avl; 2295 boolean_t cb_numname; 2296 boolean_t cb_nicenum; 2297 boolean_t cb_sid2posix; 2298 zfs_userquota_prop_t cb_prop; 2299 zfs_sort_column_t *cb_sortcol; 2300 size_t cb_width[USFIELD_LAST]; 2301} us_cbdata_t; 2302 2303static boolean_t us_populated = B_FALSE; 2304 2305typedef struct { 2306 zfs_sort_column_t *si_sortcol; 2307 boolean_t si_numname; 2308} us_sort_info_t; 2309 2310static int 2311us_field_index(char *field) 2312{ 2313 int i; 2314 2315 for (i = 0; i < USFIELD_LAST; i++) { 2316 if (strcmp(field, us_field_names[i]) == 0) 2317 return (i); 2318 } 2319 2320 return (-1); 2321} 2322 2323static int 2324us_compare(const void *larg, const void *rarg, void *unused) 2325{ 2326 const us_node_t *l = larg; 2327 const us_node_t *r = rarg; 2328 us_sort_info_t *si = (us_sort_info_t *)unused; 2329 zfs_sort_column_t *sortcol = si->si_sortcol; 2330 boolean_t numname = si->si_numname; 2331 nvlist_t *lnvl = l->usn_nvl; 2332 nvlist_t *rnvl = r->usn_nvl; 2333 int rc = 0; 2334 boolean_t lvb, rvb; 2335 2336 for (; sortcol != NULL; sortcol = sortcol->sc_next) { 2337 char *lvstr = ""; 2338 char *rvstr = ""; 2339 uint32_t lv32 = 0; 2340 uint32_t rv32 = 0; 2341 uint64_t lv64 = 0; 2342 uint64_t rv64 = 0; 2343 zfs_prop_t prop = sortcol->sc_prop; 2344 const char *propname = NULL; 2345 boolean_t reverse = sortcol->sc_reverse; 2346 2347 switch (prop) { 2348 case ZFS_PROP_TYPE: 2349 propname = "type"; 2350 (void) nvlist_lookup_uint32(lnvl, propname, &lv32); 2351 (void) nvlist_lookup_uint32(rnvl, propname, &rv32); 2352 if (rv32 != lv32) 2353 rc = (rv32 < lv32) ? 1 : -1; 2354 break; 2355 case ZFS_PROP_NAME: 2356 propname = "name"; 2357 if (numname) { 2358 (void) nvlist_lookup_uint64(lnvl, propname, 2359 &lv64); 2360 (void) nvlist_lookup_uint64(rnvl, propname, 2361 &rv64); 2362 if (rv64 != lv64) 2363 rc = (rv64 < lv64) ? 1 : -1; 2364 } else { 2365 (void) nvlist_lookup_string(lnvl, propname, 2366 &lvstr); 2367 (void) nvlist_lookup_string(rnvl, propname, 2368 &rvstr); 2369 rc = strcmp(lvstr, rvstr); 2370 } 2371 break; 2372 case ZFS_PROP_USED: 2373 case ZFS_PROP_QUOTA: 2374 if (!us_populated) 2375 break; 2376 if (prop == ZFS_PROP_USED) 2377 propname = "used"; 2378 else 2379 propname = "quota"; 2380 (void) nvlist_lookup_uint64(lnvl, propname, &lv64); 2381 (void) nvlist_lookup_uint64(rnvl, propname, &rv64); 2382 if (rv64 != lv64) 2383 rc = (rv64 < lv64) ? 1 : -1; 2384 break; 2385 2386 default: 2387 break; 2388 } 2389 2390 if (rc != 0) { 2391 if (rc < 0) 2392 return (reverse ? 1 : -1); 2393 else 2394 return (reverse ? -1 : 1); 2395 } 2396 } 2397 2398 /* 2399 * If entries still seem to be the same, check if they are of the same 2400 * type (smbentity is added only if we are doing SID to POSIX ID 2401 * translation where we can have duplicate type/name combinations). 2402 */ 2403 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 && 2404 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 && 2405 lvb != rvb) 2406 return (lvb < rvb ? -1 : 1); 2407 2408 return (0); 2409} 2410 2411static inline const char * 2412us_type2str(unsigned field_type) 2413{ 2414 switch (field_type) { 2415 case USTYPE_PSX_USR: 2416 return ("POSIX User"); 2417 case USTYPE_PSX_GRP: 2418 return ("POSIX Group"); 2419 case USTYPE_SMB_USR: 2420 return ("SMB User"); 2421 case USTYPE_SMB_GRP: 2422 return ("SMB Group"); 2423 default: 2424 return ("Undefined"); 2425 } 2426} 2427 2428static int 2429userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space) 2430{ 2431 us_cbdata_t *cb = (us_cbdata_t *)arg; 2432 zfs_userquota_prop_t prop = cb->cb_prop; 2433 char *name = NULL; 2434 char *propname; 2435 char sizebuf[32]; 2436 us_node_t *node; 2437 uu_avl_pool_t *avl_pool = cb->cb_avl_pool; 2438 uu_avl_t *avl = cb->cb_avl; 2439 uu_avl_index_t idx; 2440 nvlist_t *props; 2441 us_node_t *n; 2442 zfs_sort_column_t *sortcol = cb->cb_sortcol; 2443 unsigned type = 0; 2444 const char *typestr; 2445 size_t namelen; 2446 size_t typelen; 2447 size_t sizelen; 2448 int typeidx, nameidx, sizeidx; 2449 us_sort_info_t sortinfo = { sortcol, cb->cb_numname }; 2450 boolean_t smbentity = B_FALSE; 2451 2452 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 2453 nomem(); 2454 node = safe_malloc(sizeof (us_node_t)); 2455 uu_avl_node_init(node, &node->usn_avlnode, avl_pool); 2456 node->usn_nvl = props; 2457 2458 if (domain != NULL && domain[0] != '\0') { 2459 /* SMB */ 2460 char sid[MAXNAMELEN + 32]; 2461 uid_t id; 2462#ifdef illumos 2463 int err; 2464 int flag = IDMAP_REQ_FLG_USE_CACHE; 2465#endif 2466 2467 smbentity = B_TRUE; 2468 2469 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid); 2470 2471 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) { 2472 type = USTYPE_SMB_GRP; 2473#ifdef illumos 2474 err = sid_to_id(sid, B_FALSE, &id); 2475#endif 2476 } else { 2477 type = USTYPE_SMB_USR; 2478#ifdef illumos 2479 err = sid_to_id(sid, B_TRUE, &id); 2480#endif 2481 } 2482 2483#ifdef illumos 2484 if (err == 0) { 2485 rid = id; 2486 if (!cb->cb_sid2posix) { 2487 if (type == USTYPE_SMB_USR) { 2488 (void) idmap_getwinnamebyuid(rid, flag, 2489 &name, NULL); 2490 } else { 2491 (void) idmap_getwinnamebygid(rid, flag, 2492 &name, NULL); 2493 } 2494 if (name == NULL) 2495 name = sid; 2496 } 2497 } 2498#endif 2499 } 2500 2501 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') { 2502 /* POSIX or -i */ 2503 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) { 2504 type = USTYPE_PSX_GRP; 2505 if (!cb->cb_numname) { 2506 struct group *g; 2507 2508 if ((g = getgrgid(rid)) != NULL) 2509 name = g->gr_name; 2510 } 2511 } else { 2512 type = USTYPE_PSX_USR; 2513 if (!cb->cb_numname) { 2514 struct passwd *p; 2515 2516 if ((p = getpwuid(rid)) != NULL) 2517 name = p->pw_name; 2518 } 2519 } 2520 } 2521 2522 /* 2523 * Make sure that the type/name combination is unique when doing 2524 * SID to POSIX ID translation (hence changing the type from SMB to 2525 * POSIX). 2526 */ 2527 if (cb->cb_sid2posix && 2528 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0) 2529 nomem(); 2530 2531 /* Calculate/update width of TYPE field */ 2532 typestr = us_type2str(type); 2533 typelen = strlen(gettext(typestr)); 2534 typeidx = us_field_index("type"); 2535 if (typelen > cb->cb_width[typeidx]) 2536 cb->cb_width[typeidx] = typelen; 2537 if (nvlist_add_uint32(props, "type", type) != 0) 2538 nomem(); 2539 2540 /* Calculate/update width of NAME field */ 2541 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) { 2542 if (nvlist_add_uint64(props, "name", rid) != 0) 2543 nomem(); 2544 namelen = snprintf(NULL, 0, "%u", rid); 2545 } else { 2546 if (nvlist_add_string(props, "name", name) != 0) 2547 nomem(); 2548 namelen = strlen(name); 2549 } 2550 nameidx = us_field_index("name"); 2551 if (namelen > cb->cb_width[nameidx]) 2552 cb->cb_width[nameidx] = namelen; 2553 2554 /* 2555 * Check if this type/name combination is in the list and update it; 2556 * otherwise add new node to the list. 2557 */ 2558 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) { 2559 uu_avl_insert(avl, node, idx); 2560 } else { 2561 nvlist_free(props); 2562 free(node); 2563 node = n; 2564 props = node->usn_nvl; 2565 } 2566 2567 /* Calculate/update width of USED/QUOTA fields */ 2568 if (cb->cb_nicenum) 2569 zfs_nicenum(space, sizebuf, sizeof (sizebuf)); 2570 else 2571 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space); 2572 sizelen = strlen(sizebuf); 2573 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) { 2574 propname = "used"; 2575 if (!nvlist_exists(props, "quota")) 2576 (void) nvlist_add_uint64(props, "quota", 0); 2577 } else { 2578 propname = "quota"; 2579 if (!nvlist_exists(props, "used")) 2580 (void) nvlist_add_uint64(props, "used", 0); 2581 } 2582 sizeidx = us_field_index(propname); 2583 if (sizelen > cb->cb_width[sizeidx]) 2584 cb->cb_width[sizeidx] = sizelen; 2585 2586 if (nvlist_add_uint64(props, propname, space) != 0) 2587 nomem(); 2588 2589 return (0); 2590} 2591 2592static void 2593print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types, 2594 size_t *width, us_node_t *node) 2595{ 2596 nvlist_t *nvl = node->usn_nvl; 2597 char valstr[MAXNAMELEN]; 2598 boolean_t first = B_TRUE; 2599 int cfield = 0; 2600 int field; 2601 uint32_t ustype; 2602 2603 /* Check type */ 2604 (void) nvlist_lookup_uint32(nvl, "type", &ustype); 2605 if (!(ustype & types)) 2606 return; 2607 2608 while ((field = fields[cfield]) != USFIELD_LAST) { 2609 nvpair_t *nvp = NULL; 2610 data_type_t type; 2611 uint32_t val32; 2612 uint64_t val64; 2613 char *strval = NULL; 2614 2615 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 2616 if (strcmp(nvpair_name(nvp), 2617 us_field_names[field]) == 0) 2618 break; 2619 } 2620 2621 type = nvpair_type(nvp); 2622 switch (type) { 2623 case DATA_TYPE_UINT32: 2624 (void) nvpair_value_uint32(nvp, &val32); 2625 break; 2626 case DATA_TYPE_UINT64: 2627 (void) nvpair_value_uint64(nvp, &val64); 2628 break; 2629 case DATA_TYPE_STRING: 2630 (void) nvpair_value_string(nvp, &strval); 2631 break; 2632 default: 2633 (void) fprintf(stderr, "invalid data type\n"); 2634 } 2635 2636 switch (field) { 2637 case USFIELD_TYPE: 2638 strval = (char *)us_type2str(val32); 2639 break; 2640 case USFIELD_NAME: 2641 if (type == DATA_TYPE_UINT64) { 2642 (void) sprintf(valstr, "%llu", val64); 2643 strval = valstr; 2644 } 2645 break; 2646 case USFIELD_USED: 2647 case USFIELD_QUOTA: 2648 if (type == DATA_TYPE_UINT64) { 2649 if (parsable) { 2650 (void) sprintf(valstr, "%llu", val64); 2651 } else { 2652 zfs_nicenum(val64, valstr, 2653 sizeof (valstr)); 2654 } 2655 if (field == USFIELD_QUOTA && 2656 strcmp(valstr, "0") == 0) 2657 strval = "none"; 2658 else 2659 strval = valstr; 2660 } 2661 break; 2662 } 2663 2664 if (!first) { 2665 if (scripted) 2666 (void) printf("\t"); 2667 else 2668 (void) printf(" "); 2669 } 2670 if (scripted) 2671 (void) printf("%s", strval); 2672 else if (field == USFIELD_TYPE || field == USFIELD_NAME) 2673 (void) printf("%-*s", width[field], strval); 2674 else 2675 (void) printf("%*s", width[field], strval); 2676 2677 first = B_FALSE; 2678 cfield++; 2679 } 2680 2681 (void) printf("\n"); 2682} 2683 2684static void 2685print_us(boolean_t scripted, boolean_t parsable, int *fields, int types, 2686 size_t *width, boolean_t rmnode, uu_avl_t *avl) 2687{ 2688 us_node_t *node; 2689 const char *col; 2690 int cfield = 0; 2691 int field; 2692 2693 if (!scripted) { 2694 boolean_t first = B_TRUE; 2695 2696 while ((field = fields[cfield]) != USFIELD_LAST) { 2697 col = gettext(us_field_hdr[field]); 2698 if (field == USFIELD_TYPE || field == USFIELD_NAME) { 2699 (void) printf(first ? "%-*s" : " %-*s", 2700 width[field], col); 2701 } else { 2702 (void) printf(first ? "%*s" : " %*s", 2703 width[field], col); 2704 } 2705 first = B_FALSE; 2706 cfield++; 2707 } 2708 (void) printf("\n"); 2709 } 2710 2711 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) { 2712 print_us_node(scripted, parsable, fields, types, width, node); 2713 if (rmnode) 2714 nvlist_free(node->usn_nvl); 2715 } 2716} 2717 2718static int 2719zfs_do_userspace(int argc, char **argv) 2720{ 2721 zfs_handle_t *zhp; 2722 zfs_userquota_prop_t p; 2723 2724 uu_avl_pool_t *avl_pool; 2725 uu_avl_t *avl_tree; 2726 uu_avl_walk_t *walk; 2727 char *delim; 2728 char deffields[] = "type,name,used,quota"; 2729 char *ofield = NULL; 2730 char *tfield = NULL; 2731 int cfield = 0; 2732 int fields[256]; 2733 int i; 2734 boolean_t scripted = B_FALSE; 2735 boolean_t prtnum = B_FALSE; 2736 boolean_t parsable = B_FALSE; 2737 boolean_t sid2posix = B_FALSE; 2738 int ret = 0; 2739 int c; 2740 zfs_sort_column_t *sortcol = NULL; 2741 int types = USTYPE_PSX_USR | USTYPE_SMB_USR; 2742 us_cbdata_t cb; 2743 us_node_t *node; 2744 us_node_t *rmnode; 2745 uu_list_pool_t *listpool; 2746 uu_list_t *list; 2747 uu_avl_index_t idx = 0; 2748 uu_list_index_t idx2 = 0; 2749 2750 if (argc < 2) 2751 usage(B_FALSE); 2752 2753 if (strcmp(argv[0], "groupspace") == 0) 2754 /* Toggle default group types */ 2755 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP; 2756 2757 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) { 2758 switch (c) { 2759 case 'n': 2760 prtnum = B_TRUE; 2761 break; 2762 case 'H': 2763 scripted = B_TRUE; 2764 break; 2765 case 'p': 2766 parsable = B_TRUE; 2767 break; 2768 case 'o': 2769 ofield = optarg; 2770 break; 2771 case 's': 2772 case 'S': 2773 if (zfs_add_sort_column(&sortcol, optarg, 2774 c == 's' ? B_FALSE : B_TRUE) != 0) { 2775 (void) fprintf(stderr, 2776 gettext("invalid field '%s'\n"), optarg); 2777 usage(B_FALSE); 2778 } 2779 break; 2780 case 't': 2781 tfield = optarg; 2782 break; 2783 case 'i': 2784 sid2posix = B_TRUE; 2785 break; 2786 case ':': 2787 (void) fprintf(stderr, gettext("missing argument for " 2788 "'%c' option\n"), optopt); 2789 usage(B_FALSE); 2790 break; 2791 case '?': 2792 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2793 optopt); 2794 usage(B_FALSE); 2795 } 2796 } 2797 2798 argc -= optind; 2799 argv += optind; 2800 2801 if (argc < 1) { 2802 (void) fprintf(stderr, gettext("missing dataset name\n")); 2803 usage(B_FALSE); 2804 } 2805 if (argc > 1) { 2806 (void) fprintf(stderr, gettext("too many arguments\n")); 2807 usage(B_FALSE); 2808 } 2809 2810 /* Use default output fields if not specified using -o */ 2811 if (ofield == NULL) 2812 ofield = deffields; 2813 do { 2814 if ((delim = strchr(ofield, ',')) != NULL) 2815 *delim = '\0'; 2816 if ((fields[cfield++] = us_field_index(ofield)) == -1) { 2817 (void) fprintf(stderr, gettext("invalid type '%s' " 2818 "for -o option\n"), ofield); 2819 return (-1); 2820 } 2821 if (delim != NULL) 2822 ofield = delim + 1; 2823 } while (delim != NULL); 2824 fields[cfield] = USFIELD_LAST; 2825 2826 /* Override output types (-t option) */ 2827 if (tfield != NULL) { 2828 types = 0; 2829 2830 do { 2831 boolean_t found = B_FALSE; 2832 2833 if ((delim = strchr(tfield, ',')) != NULL) 2834 *delim = '\0'; 2835 for (i = 0; i < sizeof (us_type_bits) / sizeof (int); 2836 i++) { 2837 if (strcmp(tfield, us_type_names[i]) == 0) { 2838 found = B_TRUE; 2839 types |= us_type_bits[i]; 2840 break; 2841 } 2842 } 2843 if (!found) { 2844 (void) fprintf(stderr, gettext("invalid type " 2845 "'%s' for -t option\n"), tfield); 2846 return (-1); 2847 } 2848 if (delim != NULL) 2849 tfield = delim + 1; 2850 } while (delim != NULL); 2851 } 2852 2853 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 2854 return (1); 2855 2856 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t), 2857 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL) 2858 nomem(); 2859 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) 2860 nomem(); 2861 2862 /* Always add default sorting columns */ 2863 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE); 2864 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE); 2865 2866 cb.cb_sortcol = sortcol; 2867 cb.cb_numname = prtnum; 2868 cb.cb_nicenum = !parsable; 2869 cb.cb_avl_pool = avl_pool; 2870 cb.cb_avl = avl_tree; 2871 cb.cb_sid2posix = sid2posix; 2872 2873 for (i = 0; i < USFIELD_LAST; i++) 2874 cb.cb_width[i] = strlen(gettext(us_field_hdr[i])); 2875 2876 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) { 2877 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) && 2878 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) || 2879 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) && 2880 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP)))) 2881 continue; 2882 cb.cb_prop = p; 2883 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0) 2884 return (ret); 2885 } 2886 2887 /* Sort the list */ 2888 if ((node = uu_avl_first(avl_tree)) == NULL) 2889 return (0); 2890 2891 us_populated = B_TRUE; 2892 2893 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t), 2894 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT); 2895 list = uu_list_create(listpool, NULL, UU_DEFAULT); 2896 uu_list_node_init(node, &node->usn_listnode, listpool); 2897 2898 while (node != NULL) { 2899 rmnode = node; 2900 node = uu_avl_next(avl_tree, node); 2901 uu_avl_remove(avl_tree, rmnode); 2902 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) 2903 uu_list_insert(list, rmnode, idx2); 2904 } 2905 2906 for (node = uu_list_first(list); node != NULL; 2907 node = uu_list_next(list, node)) { 2908 us_sort_info_t sortinfo = { sortcol, cb.cb_numname }; 2909 2910 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL) 2911 uu_avl_insert(avl_tree, node, idx); 2912 } 2913 2914 uu_list_destroy(list); 2915 uu_list_pool_destroy(listpool); 2916 2917 /* Print and free node nvlist memory */ 2918 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE, 2919 cb.cb_avl); 2920 2921 zfs_free_sort_columns(sortcol); 2922 2923 /* Clean up the AVL tree */ 2924 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) 2925 nomem(); 2926 2927 while ((node = uu_avl_walk_next(walk)) != NULL) { 2928 uu_avl_remove(cb.cb_avl, node); 2929 free(node); 2930 } 2931 2932 uu_avl_walk_end(walk); 2933 uu_avl_destroy(avl_tree); 2934 uu_avl_pool_destroy(avl_pool); 2935 2936 return (ret); 2937} 2938 2939/* 2940 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ... 2941 * [-t type[,...]] [filesystem|volume|snapshot] ... 2942 * 2943 * -H Scripted mode; elide headers and separate columns by tabs. 2944 * -p Display values in parsable (literal) format. 2945 * -r Recurse over all children. 2946 * -d Limit recursion by depth. 2947 * -o Control which fields to display. 2948 * -s Specify sort columns, descending order. 2949 * -S Specify sort columns, ascending order. 2950 * -t Control which object types to display. 2951 * 2952 * When given no arguments, list all filesystems in the system. 2953 * Otherwise, list the specified datasets, optionally recursing down them if 2954 * '-r' is specified. 2955 */ 2956typedef struct list_cbdata { 2957 boolean_t cb_first; 2958 boolean_t cb_literal; 2959 boolean_t cb_scripted; 2960 zprop_list_t *cb_proplist; 2961} list_cbdata_t; 2962 2963/* 2964 * Given a list of columns to display, output appropriate headers for each one. 2965 */ 2966static void 2967print_header(list_cbdata_t *cb) 2968{ 2969 zprop_list_t *pl = cb->cb_proplist; 2970 char headerbuf[ZFS_MAXPROPLEN]; 2971 const char *header; 2972 int i; 2973 boolean_t first = B_TRUE; 2974 boolean_t right_justify; 2975 2976 for (; pl != NULL; pl = pl->pl_next) { 2977 if (!first) { 2978 (void) printf(" "); 2979 } else { 2980 first = B_FALSE; 2981 } 2982 2983 right_justify = B_FALSE; 2984 if (pl->pl_prop != ZPROP_INVAL) { 2985 header = zfs_prop_column_name(pl->pl_prop); 2986 right_justify = zfs_prop_align_right(pl->pl_prop); 2987 } else { 2988 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 2989 headerbuf[i] = toupper(pl->pl_user_prop[i]); 2990 headerbuf[i] = '\0'; 2991 header = headerbuf; 2992 } 2993 2994 if (pl->pl_next == NULL && !right_justify) 2995 (void) printf("%s", header); 2996 else if (right_justify) 2997 (void) printf("%*s", pl->pl_width, header); 2998 else 2999 (void) printf("%-*s", pl->pl_width, header); 3000 } 3001 3002 (void) printf("\n"); 3003} 3004 3005/* 3006 * Given a dataset and a list of fields, print out all the properties according 3007 * to the described layout. 3008 */ 3009static void 3010print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb) 3011{ 3012 zprop_list_t *pl = cb->cb_proplist; 3013 boolean_t first = B_TRUE; 3014 char property[ZFS_MAXPROPLEN]; 3015 nvlist_t *userprops = zfs_get_user_props(zhp); 3016 nvlist_t *propval; 3017 char *propstr; 3018 boolean_t right_justify; 3019 3020 for (; pl != NULL; pl = pl->pl_next) { 3021 if (!first) { 3022 if (cb->cb_scripted) 3023 (void) printf("\t"); 3024 else 3025 (void) printf(" "); 3026 } else { 3027 first = B_FALSE; 3028 } 3029 3030 if (pl->pl_prop == ZFS_PROP_NAME) { 3031 (void) strlcpy(property, zfs_get_name(zhp), 3032 sizeof (property)); 3033 propstr = property; 3034 right_justify = zfs_prop_align_right(pl->pl_prop); 3035 } else if (pl->pl_prop != ZPROP_INVAL) { 3036 if (zfs_prop_get(zhp, pl->pl_prop, property, 3037 sizeof (property), NULL, NULL, 0, 3038 cb->cb_literal) != 0) 3039 propstr = "-"; 3040 else 3041 propstr = property; 3042 right_justify = zfs_prop_align_right(pl->pl_prop); 3043 } else if (zfs_prop_userquota(pl->pl_user_prop)) { 3044 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 3045 property, sizeof (property), cb->cb_literal) != 0) 3046 propstr = "-"; 3047 else 3048 propstr = property; 3049 right_justify = B_TRUE; 3050 } else if (zfs_prop_written(pl->pl_user_prop)) { 3051 if (zfs_prop_get_written(zhp, pl->pl_user_prop, 3052 property, sizeof (property), cb->cb_literal) != 0) 3053 propstr = "-"; 3054 else 3055 propstr = property; 3056 right_justify = B_TRUE; 3057 } else { 3058 if (nvlist_lookup_nvlist(userprops, 3059 pl->pl_user_prop, &propval) != 0) 3060 propstr = "-"; 3061 else 3062 verify(nvlist_lookup_string(propval, 3063 ZPROP_VALUE, &propstr) == 0); 3064 right_justify = B_FALSE; 3065 } 3066 3067 /* 3068 * If this is being called in scripted mode, or if this is the 3069 * last column and it is left-justified, don't include a width 3070 * format specifier. 3071 */ 3072 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 3073 (void) printf("%s", propstr); 3074 else if (right_justify) 3075 (void) printf("%*s", pl->pl_width, propstr); 3076 else 3077 (void) printf("%-*s", pl->pl_width, propstr); 3078 } 3079 3080 (void) printf("\n"); 3081} 3082 3083/* 3084 * Generic callback function to list a dataset or snapshot. 3085 */ 3086static int 3087list_callback(zfs_handle_t *zhp, void *data) 3088{ 3089 list_cbdata_t *cbp = data; 3090 3091 if (cbp->cb_first) { 3092 if (!cbp->cb_scripted) 3093 print_header(cbp); 3094 cbp->cb_first = B_FALSE; 3095 } 3096 3097 print_dataset(zhp, cbp); 3098 3099 return (0); 3100} 3101 3102static int 3103zfs_do_list(int argc, char **argv) 3104{ 3105 int c; 3106 static char default_fields[] = 3107 "name,used,available,referenced,mountpoint"; 3108 int types = ZFS_TYPE_DATASET; 3109 boolean_t types_specified = B_FALSE; 3110 char *fields = NULL; 3111 list_cbdata_t cb = { 0 }; 3112 char *value; 3113 int limit = 0; 3114 int ret = 0; 3115 zfs_sort_column_t *sortcol = NULL; 3116 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS; 3117 3118 /* check options */ 3119 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) { 3120 switch (c) { 3121 case 'o': 3122 fields = optarg; 3123 break; 3124 case 'p': 3125 cb.cb_literal = B_TRUE; 3126 flags |= ZFS_ITER_LITERAL_PROPS; 3127 break; 3128 case 'd': 3129 limit = parse_depth(optarg, &flags); 3130 break; 3131 case 'r': 3132 flags |= ZFS_ITER_RECURSE; 3133 break; 3134 case 'H': 3135 cb.cb_scripted = B_TRUE; 3136 break; 3137 case 's': 3138 if (zfs_add_sort_column(&sortcol, optarg, 3139 B_FALSE) != 0) { 3140 (void) fprintf(stderr, 3141 gettext("invalid property '%s'\n"), optarg); 3142 usage(B_FALSE); 3143 } 3144 break; 3145 case 'S': 3146 if (zfs_add_sort_column(&sortcol, optarg, 3147 B_TRUE) != 0) { 3148 (void) fprintf(stderr, 3149 gettext("invalid property '%s'\n"), optarg); 3150 usage(B_FALSE); 3151 } 3152 break; 3153 case 't': 3154 types = 0; 3155 types_specified = B_TRUE; 3156 flags &= ~ZFS_ITER_PROP_LISTSNAPS; 3157 while (*optarg != '\0') { 3158 static char *type_subopts[] = { "filesystem", 3159 "volume", "snapshot", "snap", "bookmark", 3160 "all", NULL }; 3161 3162 switch (getsubopt(&optarg, type_subopts, 3163 &value)) { 3164 case 0: 3165 types |= ZFS_TYPE_FILESYSTEM; 3166 break; 3167 case 1: 3168 types |= ZFS_TYPE_VOLUME; 3169 break; 3170 case 2: 3171 case 3: 3172 types |= ZFS_TYPE_SNAPSHOT; 3173 break; 3174 case 4: 3175 types |= ZFS_TYPE_BOOKMARK; 3176 break; 3177 case 5: 3178 types = ZFS_TYPE_DATASET | 3179 ZFS_TYPE_BOOKMARK; 3180 break; 3181 default: 3182 (void) fprintf(stderr, 3183 gettext("invalid type '%s'\n"), 3184 suboptarg); 3185 usage(B_FALSE); 3186 } 3187 } 3188 break; 3189 case ':': 3190 (void) fprintf(stderr, gettext("missing argument for " 3191 "'%c' option\n"), optopt); 3192 usage(B_FALSE); 3193 break; 3194 case '?': 3195 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3196 optopt); 3197 usage(B_FALSE); 3198 } 3199 } 3200 3201 argc -= optind; 3202 argv += optind; 3203 3204 if (fields == NULL) 3205 fields = default_fields; 3206 3207 /* 3208 * If we are only going to list snapshot names and sort by name, 3209 * then we can use faster version. 3210 */ 3211 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol)) 3212 flags |= ZFS_ITER_SIMPLE; 3213 3214 /* 3215 * If "-o space" and no types were specified, don't display snapshots. 3216 */ 3217 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE) 3218 types &= ~ZFS_TYPE_SNAPSHOT; 3219 3220 /* 3221 * If the user specifies '-o all', the zprop_get_list() doesn't 3222 * normally include the name of the dataset. For 'zfs list', we always 3223 * want this property to be first. 3224 */ 3225 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 3226 != 0) 3227 usage(B_FALSE); 3228 3229 cb.cb_first = B_TRUE; 3230 3231 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist, 3232 limit, list_callback, &cb); 3233 3234 zprop_free_list(cb.cb_proplist); 3235 zfs_free_sort_columns(sortcol); 3236 3237 if (ret == 0 && cb.cb_first && !cb.cb_scripted) 3238 (void) printf(gettext("no datasets available\n")); 3239 3240 return (ret); 3241} 3242 3243/* 3244 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol> 3245 * zfs rename [-f] -p <fs | vol> <fs | vol> 3246 * zfs rename -r <snap> <snap> 3247 * zfs rename -u [-p] <fs> <fs> 3248 * 3249 * Renames the given dataset to another of the same type. 3250 * 3251 * The '-p' flag creates all the non-existing ancestors of the target first. 3252 */ 3253/* ARGSUSED */ 3254static int 3255zfs_do_rename(int argc, char **argv) 3256{ 3257 zfs_handle_t *zhp; 3258 renameflags_t flags = { 0 }; 3259 int c; 3260 int ret = 0; 3261 int types; 3262 boolean_t parents = B_FALSE; 3263 char *snapshot = NULL; 3264 3265 /* check options */ 3266 while ((c = getopt(argc, argv, "fpru")) != -1) { 3267 switch (c) { 3268 case 'p': 3269 parents = B_TRUE; 3270 break; 3271 case 'r': 3272 flags.recurse = B_TRUE; 3273 break; 3274 case 'u': 3275 flags.nounmount = B_TRUE; 3276 break; 3277 case 'f': 3278 flags.forceunmount = B_TRUE; 3279 break; 3280 case '?': 3281 default: 3282 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3283 optopt); 3284 usage(B_FALSE); 3285 } 3286 } 3287 3288 argc -= optind; 3289 argv += optind; 3290 3291 /* check number of arguments */ 3292 if (argc < 1) { 3293 (void) fprintf(stderr, gettext("missing source dataset " 3294 "argument\n")); 3295 usage(B_FALSE); 3296 } 3297 if (argc < 2) { 3298 (void) fprintf(stderr, gettext("missing target dataset " 3299 "argument\n")); 3300 usage(B_FALSE); 3301 } 3302 if (argc > 2) { 3303 (void) fprintf(stderr, gettext("too many arguments\n")); 3304 usage(B_FALSE); 3305 } 3306 3307 if (flags.recurse && parents) { 3308 (void) fprintf(stderr, gettext("-p and -r options are mutually " 3309 "exclusive\n")); 3310 usage(B_FALSE); 3311 } 3312 3313 if (flags.recurse && strchr(argv[0], '@') == 0) { 3314 (void) fprintf(stderr, gettext("source dataset for recursive " 3315 "rename must be a snapshot\n")); 3316 usage(B_FALSE); 3317 } 3318 3319 if (flags.nounmount && parents) { 3320 (void) fprintf(stderr, gettext("-u and -p options are mutually " 3321 "exclusive\n")); 3322 usage(B_FALSE); 3323 } 3324 3325 if (flags.nounmount) 3326 types = ZFS_TYPE_FILESYSTEM; 3327 else if (parents) 3328 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 3329 else 3330 types = ZFS_TYPE_DATASET; 3331 3332 if (flags.recurse) { 3333 /* 3334 * When we do recursive rename we are fine when the given 3335 * snapshot for the given dataset doesn't exist - it can 3336 * still exists below. 3337 */ 3338 3339 snapshot = strchr(argv[0], '@'); 3340 assert(snapshot != NULL); 3341 *snapshot = '\0'; 3342 snapshot++; 3343 } 3344 3345 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) 3346 return (1); 3347 3348 /* If we were asked and the name looks good, try to create ancestors. */ 3349 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) && 3350 zfs_create_ancestors(g_zfs, argv[1]) != 0) { 3351 zfs_close(zhp); 3352 return (1); 3353 } 3354 3355 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0); 3356 3357 zfs_close(zhp); 3358 return (ret); 3359} 3360 3361/* 3362 * zfs promote <fs> 3363 * 3364 * Promotes the given clone fs to be the parent 3365 */ 3366/* ARGSUSED */ 3367static int 3368zfs_do_promote(int argc, char **argv) 3369{ 3370 zfs_handle_t *zhp; 3371 int ret = 0; 3372 3373 /* check options */ 3374 if (argc > 1 && argv[1][0] == '-') { 3375 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3376 argv[1][1]); 3377 usage(B_FALSE); 3378 } 3379 3380 /* check number of arguments */ 3381 if (argc < 2) { 3382 (void) fprintf(stderr, gettext("missing clone filesystem" 3383 " argument\n")); 3384 usage(B_FALSE); 3385 } 3386 if (argc > 2) { 3387 (void) fprintf(stderr, gettext("too many arguments\n")); 3388 usage(B_FALSE); 3389 } 3390 3391 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3392 if (zhp == NULL) 3393 return (1); 3394 3395 ret = (zfs_promote(zhp) != 0); 3396 3397 3398 zfs_close(zhp); 3399 return (ret); 3400} 3401 3402/* 3403 * zfs rollback [-rRf] <snapshot> 3404 * 3405 * -r Delete any intervening snapshots before doing rollback 3406 * -R Delete any snapshots and their clones 3407 * -f ignored for backwards compatability 3408 * 3409 * Given a filesystem, rollback to a specific snapshot, discarding any changes 3410 * since then and making it the active dataset. If more recent snapshots exist, 3411 * the command will complain unless the '-r' flag is given. 3412 */ 3413typedef struct rollback_cbdata { 3414 uint64_t cb_create; 3415 boolean_t cb_first; 3416 int cb_doclones; 3417 char *cb_target; 3418 int cb_error; 3419 boolean_t cb_recurse; 3420} rollback_cbdata_t; 3421 3422static int 3423rollback_check_dependent(zfs_handle_t *zhp, void *data) 3424{ 3425 rollback_cbdata_t *cbp = data; 3426 3427 if (cbp->cb_first && cbp->cb_recurse) { 3428 (void) fprintf(stderr, gettext("cannot rollback to " 3429 "'%s': clones of previous snapshots exist\n"), 3430 cbp->cb_target); 3431 (void) fprintf(stderr, gettext("use '-R' to " 3432 "force deletion of the following clones and " 3433 "dependents:\n")); 3434 cbp->cb_first = 0; 3435 cbp->cb_error = 1; 3436 } 3437 3438 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 3439 3440 zfs_close(zhp); 3441 return (0); 3442} 3443 3444/* 3445 * Report any snapshots more recent than the one specified. Used when '-r' is 3446 * not specified. We reuse this same callback for the snapshot dependents - if 3447 * 'cb_dependent' is set, then this is a dependent and we should report it 3448 * without checking the transaction group. 3449 */ 3450static int 3451rollback_check(zfs_handle_t *zhp, void *data) 3452{ 3453 rollback_cbdata_t *cbp = data; 3454 3455 if (cbp->cb_doclones) { 3456 zfs_close(zhp); 3457 return (0); 3458 } 3459 3460 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 3461 if (cbp->cb_first && !cbp->cb_recurse) { 3462 (void) fprintf(stderr, gettext("cannot " 3463 "rollback to '%s': more recent snapshots " 3464 "or bookmarks exist\n"), 3465 cbp->cb_target); 3466 (void) fprintf(stderr, gettext("use '-r' to " 3467 "force deletion of the following " 3468 "snapshots and bookmarks:\n")); 3469 cbp->cb_first = 0; 3470 cbp->cb_error = 1; 3471 } 3472 3473 if (cbp->cb_recurse) { 3474 if (zfs_iter_dependents(zhp, B_TRUE, 3475 rollback_check_dependent, cbp) != 0) { 3476 zfs_close(zhp); 3477 return (-1); 3478 } 3479 } else { 3480 (void) fprintf(stderr, "%s\n", 3481 zfs_get_name(zhp)); 3482 } 3483 } 3484 zfs_close(zhp); 3485 return (0); 3486} 3487 3488static int 3489zfs_do_rollback(int argc, char **argv) 3490{ 3491 int ret = 0; 3492 int c; 3493 boolean_t force = B_FALSE; 3494 rollback_cbdata_t cb = { 0 }; 3495 zfs_handle_t *zhp, *snap; 3496 char parentname[ZFS_MAX_DATASET_NAME_LEN]; 3497 char *delim; 3498 3499 /* check options */ 3500 while ((c = getopt(argc, argv, "rRf")) != -1) { 3501 switch (c) { 3502 case 'r': 3503 cb.cb_recurse = 1; 3504 break; 3505 case 'R': 3506 cb.cb_recurse = 1; 3507 cb.cb_doclones = 1; 3508 break; 3509 case 'f': 3510 force = B_TRUE; 3511 break; 3512 case '?': 3513 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3514 optopt); 3515 usage(B_FALSE); 3516 } 3517 } 3518 3519 argc -= optind; 3520 argv += optind; 3521 3522 /* check number of arguments */ 3523 if (argc < 1) { 3524 (void) fprintf(stderr, gettext("missing dataset argument\n")); 3525 usage(B_FALSE); 3526 } 3527 if (argc > 1) { 3528 (void) fprintf(stderr, gettext("too many arguments\n")); 3529 usage(B_FALSE); 3530 } 3531 3532 /* open the snapshot */ 3533 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 3534 return (1); 3535 3536 /* open the parent dataset */ 3537 (void) strlcpy(parentname, argv[0], sizeof (parentname)); 3538 verify((delim = strrchr(parentname, '@')) != NULL); 3539 *delim = '\0'; 3540 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) { 3541 zfs_close(snap); 3542 return (1); 3543 } 3544 3545 /* 3546 * Check for more recent snapshots and/or clones based on the presence 3547 * of '-r' and '-R'. 3548 */ 3549 cb.cb_target = argv[0]; 3550 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 3551 cb.cb_first = B_TRUE; 3552 cb.cb_error = 0; 3553 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0) 3554 goto out; 3555 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0) 3556 goto out; 3557 3558 if ((ret = cb.cb_error) != 0) 3559 goto out; 3560 3561 /* 3562 * Rollback parent to the given snapshot. 3563 */ 3564 ret = zfs_rollback(zhp, snap, force); 3565 3566out: 3567 zfs_close(snap); 3568 zfs_close(zhp); 3569 3570 if (ret == 0) 3571 return (0); 3572 else 3573 return (1); 3574} 3575 3576/* 3577 * zfs set property=value ... { fs | snap | vol } ... 3578 * 3579 * Sets the given properties for all datasets specified on the command line. 3580 */ 3581 3582static int 3583set_callback(zfs_handle_t *zhp, void *data) 3584{ 3585 nvlist_t *props = data; 3586 3587 if (zfs_prop_set_list(zhp, props) != 0) { 3588 switch (libzfs_errno(g_zfs)) { 3589 case EZFS_MOUNTFAILED: 3590 (void) fprintf(stderr, gettext("property may be set " 3591 "but unable to remount filesystem\n")); 3592 break; 3593 case EZFS_SHARENFSFAILED: 3594 (void) fprintf(stderr, gettext("property may be set " 3595 "but unable to reshare filesystem\n")); 3596 break; 3597 } 3598 return (1); 3599 } 3600 return (0); 3601} 3602 3603static int 3604zfs_do_set(int argc, char **argv) 3605{ 3606 nvlist_t *props = NULL; 3607 int ds_start = -1; /* argv idx of first dataset arg */ 3608 int ret = 0; 3609 3610 /* check for options */ 3611 if (argc > 1 && argv[1][0] == '-') { 3612 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3613 argv[1][1]); 3614 usage(B_FALSE); 3615 } 3616 3617 /* check number of arguments */ 3618 if (argc < 2) { 3619 (void) fprintf(stderr, gettext("missing arguments\n")); 3620 usage(B_FALSE); 3621 } 3622 if (argc < 3) { 3623 if (strchr(argv[1], '=') == NULL) { 3624 (void) fprintf(stderr, gettext("missing property=value " 3625 "argument(s)\n")); 3626 } else { 3627 (void) fprintf(stderr, gettext("missing dataset " 3628 "name(s)\n")); 3629 } 3630 usage(B_FALSE); 3631 } 3632 3633 /* validate argument order: prop=val args followed by dataset args */ 3634 for (int i = 1; i < argc; i++) { 3635 if (strchr(argv[i], '=') != NULL) { 3636 if (ds_start > 0) { 3637 /* out-of-order prop=val argument */ 3638 (void) fprintf(stderr, gettext("invalid " 3639 "argument order\n"), i); 3640 usage(B_FALSE); 3641 } 3642 } else if (ds_start < 0) { 3643 ds_start = i; 3644 } 3645 } 3646 if (ds_start < 0) { 3647 (void) fprintf(stderr, gettext("missing dataset name(s)\n")); 3648 usage(B_FALSE); 3649 } 3650 3651 /* Populate a list of property settings */ 3652 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3653 nomem(); 3654 for (int i = 1; i < ds_start; i++) { 3655 if ((ret = parseprop(props, argv[i])) != 0) 3656 goto error; 3657 } 3658 3659 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0, 3660 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props); 3661 3662error: 3663 nvlist_free(props); 3664 return (ret); 3665} 3666 3667typedef struct snap_cbdata { 3668 nvlist_t *sd_nvl; 3669 boolean_t sd_recursive; 3670 const char *sd_snapname; 3671} snap_cbdata_t; 3672 3673static int 3674zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 3675{ 3676 snap_cbdata_t *sd = arg; 3677 char *name; 3678 int rv = 0; 3679 int error; 3680 3681 if (sd->sd_recursive && 3682 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) { 3683 zfs_close(zhp); 3684 return (0); 3685 } 3686 3687 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 3688 if (error == -1) 3689 nomem(); 3690 fnvlist_add_boolean(sd->sd_nvl, name); 3691 free(name); 3692 3693 if (sd->sd_recursive) 3694 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3695 zfs_close(zhp); 3696 return (rv); 3697} 3698 3699/* 3700 * zfs snapshot [-r] [-o prop=value] ... <fs@snap> 3701 * 3702 * Creates a snapshot with the given name. While functionally equivalent to 3703 * 'zfs create', it is a separate command to differentiate intent. 3704 */ 3705static int 3706zfs_do_snapshot(int argc, char **argv) 3707{ 3708 int ret = 0; 3709 int c; 3710 nvlist_t *props; 3711 snap_cbdata_t sd = { 0 }; 3712 boolean_t multiple_snaps = B_FALSE; 3713 3714 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3715 nomem(); 3716 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0) 3717 nomem(); 3718 3719 /* check options */ 3720 while ((c = getopt(argc, argv, "ro:")) != -1) { 3721 switch (c) { 3722 case 'o': 3723 if (parseprop(props, optarg) != 0) 3724 return (1); 3725 break; 3726 case 'r': 3727 sd.sd_recursive = B_TRUE; 3728 multiple_snaps = B_TRUE; 3729 break; 3730 case '?': 3731 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3732 optopt); 3733 goto usage; 3734 } 3735 } 3736 3737 argc -= optind; 3738 argv += optind; 3739 3740 /* check number of arguments */ 3741 if (argc < 1) { 3742 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 3743 goto usage; 3744 } 3745 3746 if (argc > 1) 3747 multiple_snaps = B_TRUE; 3748 for (; argc > 0; argc--, argv++) { 3749 char *atp; 3750 zfs_handle_t *zhp; 3751 3752 atp = strchr(argv[0], '@'); 3753 if (atp == NULL) 3754 goto usage; 3755 *atp = '\0'; 3756 sd.sd_snapname = atp + 1; 3757 zhp = zfs_open(g_zfs, argv[0], 3758 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3759 if (zhp == NULL) 3760 goto usage; 3761 if (zfs_snapshot_cb(zhp, &sd) != 0) 3762 goto usage; 3763 } 3764 3765 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props); 3766 nvlist_free(sd.sd_nvl); 3767 nvlist_free(props); 3768 if (ret != 0 && multiple_snaps) 3769 (void) fprintf(stderr, gettext("no snapshots were created\n")); 3770 return (ret != 0); 3771 3772usage: 3773 nvlist_free(sd.sd_nvl); 3774 nvlist_free(props); 3775 usage(B_FALSE); 3776 return (-1); 3777} 3778 3779/* 3780 * Send a backup stream to stdout. 3781 */ 3782static int 3783zfs_do_send(int argc, char **argv) 3784{ 3785 char *fromname = NULL; 3786 char *toname = NULL; 3787 char *resume_token = NULL; 3788 char *cp; 3789 zfs_handle_t *zhp; 3790 sendflags_t flags = { 0 }; 3791 int c, err; 3792 nvlist_t *dbgnv = NULL; 3793 boolean_t extraverbose = B_FALSE; 3794 3795 struct option long_options[] = { 3796 {"replicate", no_argument, NULL, 'R'}, 3797 {"props", no_argument, NULL, 'p'}, 3798 {"parsable", no_argument, NULL, 'P'}, 3799 {"dedup", no_argument, NULL, 'D'}, 3800 {"verbose", no_argument, NULL, 'v'}, 3801 {"dryrun", no_argument, NULL, 'n'}, 3802 {"large-block", no_argument, NULL, 'L'}, 3803 {"embed", no_argument, NULL, 'e'}, 3804 {"resume", required_argument, NULL, 't'}, 3805 {"compressed", no_argument, NULL, 'c'}, 3806 {0, 0, 0, 0} 3807 }; 3808 3809 /* check options */ 3810 while ((c = getopt_long(argc, argv, ":i:I:RbDpvnPLet:c", long_options, 3811 NULL)) != -1) { 3812 switch (c) { 3813 case 'i': 3814 if (fromname) 3815 usage(B_FALSE); 3816 fromname = optarg; 3817 break; 3818 case 'I': 3819 if (fromname) 3820 usage(B_FALSE); 3821 fromname = optarg; 3822 flags.doall = B_TRUE; 3823 break; 3824 case 'R': 3825 flags.replicate = B_TRUE; 3826 break; 3827 case 'p': 3828 flags.props = B_TRUE; 3829 break; 3830 case 'P': 3831 flags.parsable = B_TRUE; 3832 flags.verbose = B_TRUE; 3833 break; 3834 case 'v': 3835 if (flags.verbose) 3836 extraverbose = B_TRUE; 3837 flags.verbose = B_TRUE; 3838 flags.progress = B_TRUE; 3839 break; 3840 case 'D': 3841 flags.dedup = B_TRUE; 3842 break; 3843 case 'n': 3844 flags.dryrun = B_TRUE; 3845 break; 3846 case 'L': 3847 flags.largeblock = B_TRUE; 3848 break; 3849 case 'e': 3850 flags.embed_data = B_TRUE; 3851 break; 3852 case 't': 3853 resume_token = optarg; 3854 break; 3855 case 'c': 3856 flags.compress = B_TRUE; 3857 break; 3858 case ':': 3859 (void) fprintf(stderr, gettext("missing argument for " 3860 "'%c' option\n"), optopt); 3861 usage(B_FALSE); 3862 break; 3863 case '?': 3864 /*FALLTHROUGH*/ 3865 default: 3866 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3867 optopt); 3868 usage(B_FALSE); 3869 } 3870 } 3871 3872 argc -= optind; 3873 argv += optind; 3874 3875 if (resume_token != NULL) { 3876 if (fromname != NULL || flags.replicate || flags.props || 3877 flags.dedup) { 3878 (void) fprintf(stderr, 3879 gettext("invalid flags combined with -t\n")); 3880 usage(B_FALSE); 3881 } 3882 if (argc != 0) { 3883 (void) fprintf(stderr, gettext("no additional " 3884 "arguments are permitted with -t\n")); 3885 usage(B_FALSE); 3886 } 3887 } else { 3888 if (argc < 1) { 3889 (void) fprintf(stderr, 3890 gettext("missing snapshot argument\n")); 3891 usage(B_FALSE); 3892 } 3893 if (argc > 1) { 3894 (void) fprintf(stderr, gettext("too many arguments\n")); 3895 usage(B_FALSE); 3896 } 3897 } 3898 3899 if (!flags.dryrun && isatty(STDOUT_FILENO)) { 3900 (void) fprintf(stderr, 3901 gettext("Error: Stream can not be written to a terminal.\n" 3902 "You must redirect standard output.\n")); 3903 return (1); 3904 } 3905 3906 if (resume_token != NULL) { 3907 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO, 3908 resume_token)); 3909 } 3910 3911 /* 3912 * Special case sending a filesystem, or from a bookmark. 3913 */ 3914 if (strchr(argv[0], '@') == NULL || 3915 (fromname && strchr(fromname, '#') != NULL)) { 3916 char frombuf[ZFS_MAX_DATASET_NAME_LEN]; 3917 enum lzc_send_flags lzc_flags = 0; 3918 3919 if (flags.replicate || flags.doall || flags.props || 3920 flags.dedup || flags.dryrun || flags.verbose || 3921 flags.progress) { 3922 (void) fprintf(stderr, 3923 gettext("Error: " 3924 "Unsupported flag with filesystem or bookmark.\n")); 3925 return (1); 3926 } 3927 3928 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET); 3929 if (zhp == NULL) 3930 return (1); 3931 3932 if (flags.largeblock) 3933 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 3934 if (flags.embed_data) 3935 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 3936 if (flags.compress) 3937 lzc_flags |= LZC_SEND_FLAG_COMPRESS; 3938 3939 if (fromname != NULL && 3940 (fromname[0] == '#' || fromname[0] == '@')) { 3941 /* 3942 * Incremental source name begins with # or @. 3943 * Default to same fs as target. 3944 */ 3945 (void) strncpy(frombuf, argv[0], sizeof (frombuf)); 3946 cp = strchr(frombuf, '@'); 3947 if (cp != NULL) 3948 *cp = '\0'; 3949 (void) strlcat(frombuf, fromname, sizeof (frombuf)); 3950 fromname = frombuf; 3951 } 3952 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags); 3953 zfs_close(zhp); 3954 return (err != 0); 3955 } 3956 3957 cp = strchr(argv[0], '@'); 3958 *cp = '\0'; 3959 toname = cp + 1; 3960 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3961 if (zhp == NULL) 3962 return (1); 3963 3964 /* 3965 * If they specified the full path to the snapshot, chop off 3966 * everything except the short name of the snapshot, but special 3967 * case if they specify the origin. 3968 */ 3969 if (fromname && (cp = strchr(fromname, '@')) != NULL) { 3970 char origin[ZFS_MAX_DATASET_NAME_LEN]; 3971 zprop_source_t src; 3972 3973 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN, 3974 origin, sizeof (origin), &src, NULL, 0, B_FALSE); 3975 3976 if (strcmp(origin, fromname) == 0) { 3977 fromname = NULL; 3978 flags.fromorigin = B_TRUE; 3979 } else { 3980 *cp = '\0'; 3981 if (cp != fromname && strcmp(argv[0], fromname)) { 3982 (void) fprintf(stderr, 3983 gettext("incremental source must be " 3984 "in same filesystem\n")); 3985 usage(B_FALSE); 3986 } 3987 fromname = cp + 1; 3988 if (strchr(fromname, '@') || strchr(fromname, '/')) { 3989 (void) fprintf(stderr, 3990 gettext("invalid incremental source\n")); 3991 usage(B_FALSE); 3992 } 3993 } 3994 } 3995 3996 if (flags.replicate && fromname == NULL) 3997 flags.doall = B_TRUE; 3998 3999 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0, 4000 extraverbose ? &dbgnv : NULL); 4001 4002 if (extraverbose && dbgnv != NULL) { 4003 /* 4004 * dump_nvlist prints to stdout, but that's been 4005 * redirected to a file. Make it print to stderr 4006 * instead. 4007 */ 4008 (void) dup2(STDERR_FILENO, STDOUT_FILENO); 4009 dump_nvlist(dbgnv, 0); 4010 nvlist_free(dbgnv); 4011 } 4012 zfs_close(zhp); 4013 4014 return (err != 0); 4015} 4016 4017/* 4018 * Restore a backup stream from stdin. 4019 */ 4020static int 4021zfs_do_receive(int argc, char **argv) 4022{ 4023 int c, err = 0; 4024 recvflags_t flags = { 0 }; 4025 boolean_t abort_resumable = B_FALSE; 4026 4027 nvlist_t *props; 4028 nvpair_t *nvp = NULL; 4029 4030 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 4031 nomem(); 4032 4033 /* check options */ 4034 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) { 4035 switch (c) { 4036 case 'o': 4037 if (parseprop(props, optarg) != 0) 4038 return (1); 4039 break; 4040 case 'd': 4041 flags.isprefix = B_TRUE; 4042 break; 4043 case 'e': 4044 flags.isprefix = B_TRUE; 4045 flags.istail = B_TRUE; 4046 break; 4047 case 'n': 4048 flags.dryrun = B_TRUE; 4049 break; 4050 case 'u': 4051 flags.nomount = B_TRUE; 4052 break; 4053 case 'v': 4054 flags.verbose = B_TRUE; 4055 break; 4056 case 's': 4057 flags.resumable = B_TRUE; 4058 break; 4059 case 'F': 4060 flags.force = B_TRUE; 4061 break; 4062 case 'A': 4063 abort_resumable = B_TRUE; 4064 break; 4065 case ':': 4066 (void) fprintf(stderr, gettext("missing argument for " 4067 "'%c' option\n"), optopt); 4068 usage(B_FALSE); 4069 break; 4070 case '?': 4071 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4072 optopt); 4073 usage(B_FALSE); 4074 } 4075 } 4076 4077 argc -= optind; 4078 argv += optind; 4079 4080 /* check number of arguments */ 4081 if (argc < 1) { 4082 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 4083 usage(B_FALSE); 4084 } 4085 if (argc > 1) { 4086 (void) fprintf(stderr, gettext("too many arguments\n")); 4087 usage(B_FALSE); 4088 } 4089 4090 while ((nvp = nvlist_next_nvpair(props, nvp))) { 4091 if (strcmp(nvpair_name(nvp), "origin") != 0) { 4092 (void) fprintf(stderr, gettext("invalid option")); 4093 usage(B_FALSE); 4094 } 4095 } 4096 4097 if (abort_resumable) { 4098 if (flags.isprefix || flags.istail || flags.dryrun || 4099 flags.resumable || flags.nomount) { 4100 (void) fprintf(stderr, gettext("invalid option")); 4101 usage(B_FALSE); 4102 } 4103 4104 char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 4105 (void) snprintf(namebuf, sizeof (namebuf), 4106 "%s/%%recv", argv[0]); 4107 4108 if (zfs_dataset_exists(g_zfs, namebuf, 4109 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) { 4110 zfs_handle_t *zhp = zfs_open(g_zfs, 4111 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4112 if (zhp == NULL) 4113 return (1); 4114 err = zfs_destroy(zhp, B_FALSE); 4115 } else { 4116 zfs_handle_t *zhp = zfs_open(g_zfs, 4117 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4118 if (zhp == NULL) 4119 usage(B_FALSE); 4120 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) || 4121 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 4122 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) { 4123 (void) fprintf(stderr, 4124 gettext("'%s' does not have any " 4125 "resumable receive state to abort\n"), 4126 argv[0]); 4127 return (1); 4128 } 4129 err = zfs_destroy(zhp, B_FALSE); 4130 } 4131 4132 return (err != 0); 4133 } 4134 4135 if (isatty(STDIN_FILENO)) { 4136 (void) fprintf(stderr, 4137 gettext("Error: Backup stream can not be read " 4138 "from a terminal.\n" 4139 "You must redirect standard input.\n")); 4140 return (1); 4141 } 4142 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL); 4143 4144 return (err != 0); 4145} 4146 4147/* 4148 * allow/unallow stuff 4149 */ 4150/* copied from zfs/sys/dsl_deleg.h */ 4151#define ZFS_DELEG_PERM_CREATE "create" 4152#define ZFS_DELEG_PERM_DESTROY "destroy" 4153#define ZFS_DELEG_PERM_SNAPSHOT "snapshot" 4154#define ZFS_DELEG_PERM_ROLLBACK "rollback" 4155#define ZFS_DELEG_PERM_CLONE "clone" 4156#define ZFS_DELEG_PERM_PROMOTE "promote" 4157#define ZFS_DELEG_PERM_RENAME "rename" 4158#define ZFS_DELEG_PERM_MOUNT "mount" 4159#define ZFS_DELEG_PERM_SHARE "share" 4160#define ZFS_DELEG_PERM_SEND "send" 4161#define ZFS_DELEG_PERM_RECEIVE "receive" 4162#define ZFS_DELEG_PERM_ALLOW "allow" 4163#define ZFS_DELEG_PERM_USERPROP "userprop" 4164#define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */ 4165#define ZFS_DELEG_PERM_USERQUOTA "userquota" 4166#define ZFS_DELEG_PERM_GROUPQUOTA "groupquota" 4167#define ZFS_DELEG_PERM_USERUSED "userused" 4168#define ZFS_DELEG_PERM_GROUPUSED "groupused" 4169#define ZFS_DELEG_PERM_HOLD "hold" 4170#define ZFS_DELEG_PERM_RELEASE "release" 4171#define ZFS_DELEG_PERM_DIFF "diff" 4172#define ZFS_DELEG_PERM_BOOKMARK "bookmark" 4173 4174#define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE 4175 4176static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = { 4177 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW }, 4178 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE }, 4179 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE }, 4180 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY }, 4181 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF}, 4182 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD }, 4183 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT }, 4184 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE }, 4185 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE }, 4186 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE }, 4187 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME }, 4188 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK }, 4189 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND }, 4190 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE }, 4191 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT }, 4192 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK }, 4193 4194 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA }, 4195 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED }, 4196 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP }, 4197 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA }, 4198 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED }, 4199 { NULL, ZFS_DELEG_NOTE_NONE } 4200}; 4201 4202/* permission structure */ 4203typedef struct deleg_perm { 4204 zfs_deleg_who_type_t dp_who_type; 4205 const char *dp_name; 4206 boolean_t dp_local; 4207 boolean_t dp_descend; 4208} deleg_perm_t; 4209 4210/* */ 4211typedef struct deleg_perm_node { 4212 deleg_perm_t dpn_perm; 4213 4214 uu_avl_node_t dpn_avl_node; 4215} deleg_perm_node_t; 4216 4217typedef struct fs_perm fs_perm_t; 4218 4219/* permissions set */ 4220typedef struct who_perm { 4221 zfs_deleg_who_type_t who_type; 4222 const char *who_name; /* id */ 4223 char who_ug_name[256]; /* user/group name */ 4224 fs_perm_t *who_fsperm; /* uplink */ 4225 4226 uu_avl_t *who_deleg_perm_avl; /* permissions */ 4227} who_perm_t; 4228 4229/* */ 4230typedef struct who_perm_node { 4231 who_perm_t who_perm; 4232 uu_avl_node_t who_avl_node; 4233} who_perm_node_t; 4234 4235typedef struct fs_perm_set fs_perm_set_t; 4236/* fs permissions */ 4237struct fs_perm { 4238 const char *fsp_name; 4239 4240 uu_avl_t *fsp_sc_avl; /* sets,create */ 4241 uu_avl_t *fsp_uge_avl; /* user,group,everyone */ 4242 4243 fs_perm_set_t *fsp_set; /* uplink */ 4244}; 4245 4246/* */ 4247typedef struct fs_perm_node { 4248 fs_perm_t fspn_fsperm; 4249 uu_avl_t *fspn_avl; 4250 4251 uu_list_node_t fspn_list_node; 4252} fs_perm_node_t; 4253 4254/* top level structure */ 4255struct fs_perm_set { 4256 uu_list_pool_t *fsps_list_pool; 4257 uu_list_t *fsps_list; /* list of fs_perms */ 4258 4259 uu_avl_pool_t *fsps_named_set_avl_pool; 4260 uu_avl_pool_t *fsps_who_perm_avl_pool; 4261 uu_avl_pool_t *fsps_deleg_perm_avl_pool; 4262}; 4263 4264static inline const char * 4265deleg_perm_type(zfs_deleg_note_t note) 4266{ 4267 /* subcommands */ 4268 switch (note) { 4269 /* SUBCOMMANDS */ 4270 /* OTHER */ 4271 case ZFS_DELEG_NOTE_GROUPQUOTA: 4272 case ZFS_DELEG_NOTE_GROUPUSED: 4273 case ZFS_DELEG_NOTE_USERPROP: 4274 case ZFS_DELEG_NOTE_USERQUOTA: 4275 case ZFS_DELEG_NOTE_USERUSED: 4276 /* other */ 4277 return (gettext("other")); 4278 default: 4279 return (gettext("subcommand")); 4280 } 4281} 4282 4283static int 4284who_type2weight(zfs_deleg_who_type_t who_type) 4285{ 4286 int res; 4287 switch (who_type) { 4288 case ZFS_DELEG_NAMED_SET_SETS: 4289 case ZFS_DELEG_NAMED_SET: 4290 res = 0; 4291 break; 4292 case ZFS_DELEG_CREATE_SETS: 4293 case ZFS_DELEG_CREATE: 4294 res = 1; 4295 break; 4296 case ZFS_DELEG_USER_SETS: 4297 case ZFS_DELEG_USER: 4298 res = 2; 4299 break; 4300 case ZFS_DELEG_GROUP_SETS: 4301 case ZFS_DELEG_GROUP: 4302 res = 3; 4303 break; 4304 case ZFS_DELEG_EVERYONE_SETS: 4305 case ZFS_DELEG_EVERYONE: 4306 res = 4; 4307 break; 4308 default: 4309 res = -1; 4310 } 4311 4312 return (res); 4313} 4314 4315/* ARGSUSED */ 4316static int 4317who_perm_compare(const void *larg, const void *rarg, void *unused) 4318{ 4319 const who_perm_node_t *l = larg; 4320 const who_perm_node_t *r = rarg; 4321 zfs_deleg_who_type_t ltype = l->who_perm.who_type; 4322 zfs_deleg_who_type_t rtype = r->who_perm.who_type; 4323 int lweight = who_type2weight(ltype); 4324 int rweight = who_type2weight(rtype); 4325 int res = lweight - rweight; 4326 if (res == 0) 4327 res = strncmp(l->who_perm.who_name, r->who_perm.who_name, 4328 ZFS_MAX_DELEG_NAME-1); 4329 4330 if (res == 0) 4331 return (0); 4332 if (res > 0) 4333 return (1); 4334 else 4335 return (-1); 4336} 4337 4338/* ARGSUSED */ 4339static int 4340deleg_perm_compare(const void *larg, const void *rarg, void *unused) 4341{ 4342 const deleg_perm_node_t *l = larg; 4343 const deleg_perm_node_t *r = rarg; 4344 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name, 4345 ZFS_MAX_DELEG_NAME-1); 4346 4347 if (res == 0) 4348 return (0); 4349 4350 if (res > 0) 4351 return (1); 4352 else 4353 return (-1); 4354} 4355 4356static inline void 4357fs_perm_set_init(fs_perm_set_t *fspset) 4358{ 4359 bzero(fspset, sizeof (fs_perm_set_t)); 4360 4361 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool", 4362 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node), 4363 NULL, UU_DEFAULT)) == NULL) 4364 nomem(); 4365 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL, 4366 UU_DEFAULT)) == NULL) 4367 nomem(); 4368 4369 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create( 4370 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof( 4371 who_perm_node_t, who_avl_node), who_perm_compare, 4372 UU_DEFAULT)) == NULL) 4373 nomem(); 4374 4375 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create( 4376 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof( 4377 who_perm_node_t, who_avl_node), who_perm_compare, 4378 UU_DEFAULT)) == NULL) 4379 nomem(); 4380 4381 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create( 4382 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof( 4383 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT)) 4384 == NULL) 4385 nomem(); 4386} 4387 4388static inline void fs_perm_fini(fs_perm_t *); 4389static inline void who_perm_fini(who_perm_t *); 4390 4391static inline void 4392fs_perm_set_fini(fs_perm_set_t *fspset) 4393{ 4394 fs_perm_node_t *node = uu_list_first(fspset->fsps_list); 4395 4396 while (node != NULL) { 4397 fs_perm_node_t *next_node = 4398 uu_list_next(fspset->fsps_list, node); 4399 fs_perm_t *fsperm = &node->fspn_fsperm; 4400 fs_perm_fini(fsperm); 4401 uu_list_remove(fspset->fsps_list, node); 4402 free(node); 4403 node = next_node; 4404 } 4405 4406 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool); 4407 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool); 4408 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool); 4409} 4410 4411static inline void 4412deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type, 4413 const char *name) 4414{ 4415 deleg_perm->dp_who_type = type; 4416 deleg_perm->dp_name = name; 4417} 4418 4419static inline void 4420who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm, 4421 zfs_deleg_who_type_t type, const char *name) 4422{ 4423 uu_avl_pool_t *pool; 4424 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool; 4425 4426 bzero(who_perm, sizeof (who_perm_t)); 4427 4428 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL, 4429 UU_DEFAULT)) == NULL) 4430 nomem(); 4431 4432 who_perm->who_type = type; 4433 who_perm->who_name = name; 4434 who_perm->who_fsperm = fsperm; 4435} 4436 4437static inline void 4438who_perm_fini(who_perm_t *who_perm) 4439{ 4440 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl); 4441 4442 while (node != NULL) { 4443 deleg_perm_node_t *next_node = 4444 uu_avl_next(who_perm->who_deleg_perm_avl, node); 4445 4446 uu_avl_remove(who_perm->who_deleg_perm_avl, node); 4447 free(node); 4448 node = next_node; 4449 } 4450 4451 uu_avl_destroy(who_perm->who_deleg_perm_avl); 4452} 4453 4454static inline void 4455fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname) 4456{ 4457 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool; 4458 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool; 4459 4460 bzero(fsperm, sizeof (fs_perm_t)); 4461 4462 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT)) 4463 == NULL) 4464 nomem(); 4465 4466 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT)) 4467 == NULL) 4468 nomem(); 4469 4470 fsperm->fsp_set = fspset; 4471 fsperm->fsp_name = fsname; 4472} 4473 4474static inline void 4475fs_perm_fini(fs_perm_t *fsperm) 4476{ 4477 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl); 4478 while (node != NULL) { 4479 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl, 4480 node); 4481 who_perm_t *who_perm = &node->who_perm; 4482 who_perm_fini(who_perm); 4483 uu_avl_remove(fsperm->fsp_sc_avl, node); 4484 free(node); 4485 node = next_node; 4486 } 4487 4488 node = uu_avl_first(fsperm->fsp_uge_avl); 4489 while (node != NULL) { 4490 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl, 4491 node); 4492 who_perm_t *who_perm = &node->who_perm; 4493 who_perm_fini(who_perm); 4494 uu_avl_remove(fsperm->fsp_uge_avl, node); 4495 free(node); 4496 node = next_node; 4497 } 4498 4499 uu_avl_destroy(fsperm->fsp_sc_avl); 4500 uu_avl_destroy(fsperm->fsp_uge_avl); 4501} 4502 4503static void 4504set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node, 4505 zfs_deleg_who_type_t who_type, const char *name, char locality) 4506{ 4507 uu_avl_index_t idx = 0; 4508 4509 deleg_perm_node_t *found_node = NULL; 4510 deleg_perm_t *deleg_perm = &node->dpn_perm; 4511 4512 deleg_perm_init(deleg_perm, who_type, name); 4513 4514 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4515 == NULL) 4516 uu_avl_insert(avl, node, idx); 4517 else { 4518 node = found_node; 4519 deleg_perm = &node->dpn_perm; 4520 } 4521 4522 4523 switch (locality) { 4524 case ZFS_DELEG_LOCAL: 4525 deleg_perm->dp_local = B_TRUE; 4526 break; 4527 case ZFS_DELEG_DESCENDENT: 4528 deleg_perm->dp_descend = B_TRUE; 4529 break; 4530 case ZFS_DELEG_NA: 4531 break; 4532 default: 4533 assert(B_FALSE); /* invalid locality */ 4534 } 4535} 4536 4537static inline int 4538parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality) 4539{ 4540 nvpair_t *nvp = NULL; 4541 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set; 4542 uu_avl_t *avl = who_perm->who_deleg_perm_avl; 4543 zfs_deleg_who_type_t who_type = who_perm->who_type; 4544 4545 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4546 const char *name = nvpair_name(nvp); 4547 data_type_t type = nvpair_type(nvp); 4548 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool; 4549 deleg_perm_node_t *node = 4550 safe_malloc(sizeof (deleg_perm_node_t)); 4551 4552 assert(type == DATA_TYPE_BOOLEAN); 4553 4554 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool); 4555 set_deleg_perm_node(avl, node, who_type, name, locality); 4556 } 4557 4558 return (0); 4559} 4560 4561static inline int 4562parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl) 4563{ 4564 nvpair_t *nvp = NULL; 4565 fs_perm_set_t *fspset = fsperm->fsp_set; 4566 4567 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4568 nvlist_t *nvl2 = NULL; 4569 const char *name = nvpair_name(nvp); 4570 uu_avl_t *avl = NULL; 4571 uu_avl_pool_t *avl_pool = NULL; 4572 zfs_deleg_who_type_t perm_type = name[0]; 4573 char perm_locality = name[1]; 4574 const char *perm_name = name + 3; 4575 boolean_t is_set = B_TRUE; 4576 who_perm_t *who_perm = NULL; 4577 4578 assert('$' == name[2]); 4579 4580 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4581 return (-1); 4582 4583 switch (perm_type) { 4584 case ZFS_DELEG_CREATE: 4585 case ZFS_DELEG_CREATE_SETS: 4586 case ZFS_DELEG_NAMED_SET: 4587 case ZFS_DELEG_NAMED_SET_SETS: 4588 avl_pool = fspset->fsps_named_set_avl_pool; 4589 avl = fsperm->fsp_sc_avl; 4590 break; 4591 case ZFS_DELEG_USER: 4592 case ZFS_DELEG_USER_SETS: 4593 case ZFS_DELEG_GROUP: 4594 case ZFS_DELEG_GROUP_SETS: 4595 case ZFS_DELEG_EVERYONE: 4596 case ZFS_DELEG_EVERYONE_SETS: 4597 avl_pool = fspset->fsps_who_perm_avl_pool; 4598 avl = fsperm->fsp_uge_avl; 4599 break; 4600 4601 default: 4602 assert(!"unhandled zfs_deleg_who_type_t"); 4603 } 4604 4605 if (is_set) { 4606 who_perm_node_t *found_node = NULL; 4607 who_perm_node_t *node = safe_malloc( 4608 sizeof (who_perm_node_t)); 4609 who_perm = &node->who_perm; 4610 uu_avl_index_t idx = 0; 4611 4612 uu_avl_node_init(node, &node->who_avl_node, avl_pool); 4613 who_perm_init(who_perm, fsperm, perm_type, perm_name); 4614 4615 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4616 == NULL) { 4617 if (avl == fsperm->fsp_uge_avl) { 4618 uid_t rid = 0; 4619 struct passwd *p = NULL; 4620 struct group *g = NULL; 4621 const char *nice_name = NULL; 4622 4623 switch (perm_type) { 4624 case ZFS_DELEG_USER_SETS: 4625 case ZFS_DELEG_USER: 4626 rid = atoi(perm_name); 4627 p = getpwuid(rid); 4628 if (p) 4629 nice_name = p->pw_name; 4630 break; 4631 case ZFS_DELEG_GROUP_SETS: 4632 case ZFS_DELEG_GROUP: 4633 rid = atoi(perm_name); 4634 g = getgrgid(rid); 4635 if (g) 4636 nice_name = g->gr_name; 4637 break; 4638 4639 default: 4640 break; 4641 } 4642 4643 if (nice_name != NULL) 4644 (void) strlcpy( 4645 node->who_perm.who_ug_name, 4646 nice_name, 256); 4647 } 4648 4649 uu_avl_insert(avl, node, idx); 4650 } else { 4651 node = found_node; 4652 who_perm = &node->who_perm; 4653 } 4654 } 4655 4656 (void) parse_who_perm(who_perm, nvl2, perm_locality); 4657 } 4658 4659 return (0); 4660} 4661 4662static inline int 4663parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl) 4664{ 4665 nvpair_t *nvp = NULL; 4666 uu_avl_index_t idx = 0; 4667 4668 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4669 nvlist_t *nvl2 = NULL; 4670 const char *fsname = nvpair_name(nvp); 4671 data_type_t type = nvpair_type(nvp); 4672 fs_perm_t *fsperm = NULL; 4673 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t)); 4674 if (node == NULL) 4675 nomem(); 4676 4677 fsperm = &node->fspn_fsperm; 4678 4679 assert(DATA_TYPE_NVLIST == type); 4680 4681 uu_list_node_init(node, &node->fspn_list_node, 4682 fspset->fsps_list_pool); 4683 4684 idx = uu_list_numnodes(fspset->fsps_list); 4685 fs_perm_init(fsperm, fspset, fsname); 4686 4687 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4688 return (-1); 4689 4690 (void) parse_fs_perm(fsperm, nvl2); 4691 4692 uu_list_insert(fspset->fsps_list, node, idx); 4693 } 4694 4695 return (0); 4696} 4697 4698static inline const char * 4699deleg_perm_comment(zfs_deleg_note_t note) 4700{ 4701 const char *str = ""; 4702 4703 /* subcommands */ 4704 switch (note) { 4705 /* SUBCOMMANDS */ 4706 case ZFS_DELEG_NOTE_ALLOW: 4707 str = gettext("Must also have the permission that is being" 4708 "\n\t\t\t\tallowed"); 4709 break; 4710 case ZFS_DELEG_NOTE_CLONE: 4711 str = gettext("Must also have the 'create' ability and 'mount'" 4712 "\n\t\t\t\tability in the origin file system"); 4713 break; 4714 case ZFS_DELEG_NOTE_CREATE: 4715 str = gettext("Must also have the 'mount' ability"); 4716 break; 4717 case ZFS_DELEG_NOTE_DESTROY: 4718 str = gettext("Must also have the 'mount' ability"); 4719 break; 4720 case ZFS_DELEG_NOTE_DIFF: 4721 str = gettext("Allows lookup of paths within a dataset;" 4722 "\n\t\t\t\tgiven an object number. Ordinary users need this" 4723 "\n\t\t\t\tin order to use zfs diff"); 4724 break; 4725 case ZFS_DELEG_NOTE_HOLD: 4726 str = gettext("Allows adding a user hold to a snapshot"); 4727 break; 4728 case ZFS_DELEG_NOTE_MOUNT: 4729 str = gettext("Allows mount/umount of ZFS datasets"); 4730 break; 4731 case ZFS_DELEG_NOTE_PROMOTE: 4732 str = gettext("Must also have the 'mount'\n\t\t\t\tand" 4733 " 'promote' ability in the origin file system"); 4734 break; 4735 case ZFS_DELEG_NOTE_RECEIVE: 4736 str = gettext("Must also have the 'mount' and 'create'" 4737 " ability"); 4738 break; 4739 case ZFS_DELEG_NOTE_RELEASE: 4740 str = gettext("Allows releasing a user hold which\n\t\t\t\t" 4741 "might destroy the snapshot"); 4742 break; 4743 case ZFS_DELEG_NOTE_RENAME: 4744 str = gettext("Must also have the 'mount' and 'create'" 4745 "\n\t\t\t\tability in the new parent"); 4746 break; 4747 case ZFS_DELEG_NOTE_ROLLBACK: 4748 str = gettext(""); 4749 break; 4750 case ZFS_DELEG_NOTE_SEND: 4751 str = gettext(""); 4752 break; 4753 case ZFS_DELEG_NOTE_SHARE: 4754 str = gettext("Allows sharing file systems over NFS or SMB" 4755 "\n\t\t\t\tprotocols"); 4756 break; 4757 case ZFS_DELEG_NOTE_SNAPSHOT: 4758 str = gettext(""); 4759 break; 4760/* 4761 * case ZFS_DELEG_NOTE_VSCAN: 4762 * str = gettext(""); 4763 * break; 4764 */ 4765 /* OTHER */ 4766 case ZFS_DELEG_NOTE_GROUPQUOTA: 4767 str = gettext("Allows accessing any groupquota@... property"); 4768 break; 4769 case ZFS_DELEG_NOTE_GROUPUSED: 4770 str = gettext("Allows reading any groupused@... property"); 4771 break; 4772 case ZFS_DELEG_NOTE_USERPROP: 4773 str = gettext("Allows changing any user property"); 4774 break; 4775 case ZFS_DELEG_NOTE_USERQUOTA: 4776 str = gettext("Allows accessing any userquota@... property"); 4777 break; 4778 case ZFS_DELEG_NOTE_USERUSED: 4779 str = gettext("Allows reading any userused@... property"); 4780 break; 4781 /* other */ 4782 default: 4783 str = ""; 4784 } 4785 4786 return (str); 4787} 4788 4789struct allow_opts { 4790 boolean_t local; 4791 boolean_t descend; 4792 boolean_t user; 4793 boolean_t group; 4794 boolean_t everyone; 4795 boolean_t create; 4796 boolean_t set; 4797 boolean_t recursive; /* unallow only */ 4798 boolean_t prt_usage; 4799 4800 boolean_t prt_perms; 4801 char *who; 4802 char *perms; 4803 const char *dataset; 4804}; 4805 4806static inline int 4807prop_cmp(const void *a, const void *b) 4808{ 4809 const char *str1 = *(const char **)a; 4810 const char *str2 = *(const char **)b; 4811 return (strcmp(str1, str2)); 4812} 4813 4814static void 4815allow_usage(boolean_t un, boolean_t requested, const char *msg) 4816{ 4817 const char *opt_desc[] = { 4818 "-h", gettext("show this help message and exit"), 4819 "-l", gettext("set permission locally"), 4820 "-d", gettext("set permission for descents"), 4821 "-u", gettext("set permission for user"), 4822 "-g", gettext("set permission for group"), 4823 "-e", gettext("set permission for everyone"), 4824 "-c", gettext("set create time permission"), 4825 "-s", gettext("define permission set"), 4826 /* unallow only */ 4827 "-r", gettext("remove permissions recursively"), 4828 }; 4829 size_t unallow_size = sizeof (opt_desc) / sizeof (char *); 4830 size_t allow_size = unallow_size - 2; 4831 const char *props[ZFS_NUM_PROPS]; 4832 int i; 4833 size_t count = 0; 4834 FILE *fp = requested ? stdout : stderr; 4835 zprop_desc_t *pdtbl = zfs_prop_get_table(); 4836 const char *fmt = gettext("%-16s %-14s\t%s\n"); 4837 4838 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW : 4839 HELP_ALLOW)); 4840 (void) fprintf(fp, gettext("Options:\n")); 4841 for (i = 0; i < (un ? unallow_size : allow_size); i++) { 4842 const char *opt = opt_desc[i++]; 4843 const char *optdsc = opt_desc[i]; 4844 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc); 4845 } 4846 4847 (void) fprintf(fp, gettext("\nThe following permissions are " 4848 "supported:\n\n")); 4849 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"), 4850 gettext("NOTES")); 4851 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) { 4852 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm; 4853 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note; 4854 const char *perm_type = deleg_perm_type(perm_note); 4855 const char *perm_comment = deleg_perm_comment(perm_note); 4856 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment); 4857 } 4858 4859 for (i = 0; i < ZFS_NUM_PROPS; i++) { 4860 zprop_desc_t *pd = &pdtbl[i]; 4861 if (pd->pd_visible != B_TRUE) 4862 continue; 4863 4864 if (pd->pd_attr == PROP_READONLY) 4865 continue; 4866 4867 props[count++] = pd->pd_name; 4868 } 4869 props[count] = NULL; 4870 4871 qsort(props, count, sizeof (char *), prop_cmp); 4872 4873 for (i = 0; i < count; i++) 4874 (void) fprintf(fp, fmt, props[i], gettext("property"), ""); 4875 4876 if (msg != NULL) 4877 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg); 4878 4879 exit(requested ? 0 : 2); 4880} 4881 4882static inline const char * 4883munge_args(int argc, char **argv, boolean_t un, size_t expected_argc, 4884 char **permsp) 4885{ 4886 if (un && argc == expected_argc - 1) 4887 *permsp = NULL; 4888 else if (argc == expected_argc) 4889 *permsp = argv[argc - 2]; 4890 else 4891 allow_usage(un, B_FALSE, 4892 gettext("wrong number of parameters\n")); 4893 4894 return (argv[argc - 1]); 4895} 4896 4897static void 4898parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts) 4899{ 4900 int uge_sum = opts->user + opts->group + opts->everyone; 4901 int csuge_sum = opts->create + opts->set + uge_sum; 4902 int ldcsuge_sum = csuge_sum + opts->local + opts->descend; 4903 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum; 4904 4905 if (uge_sum > 1) 4906 allow_usage(un, B_FALSE, 4907 gettext("-u, -g, and -e are mutually exclusive\n")); 4908 4909 if (opts->prt_usage) { 4910 if (argc == 0 && all_sum == 0) 4911 allow_usage(un, B_TRUE, NULL); 4912 else 4913 usage(B_FALSE); 4914 } 4915 4916 if (opts->set) { 4917 if (csuge_sum > 1) 4918 allow_usage(un, B_FALSE, 4919 gettext("invalid options combined with -s\n")); 4920 4921 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4922 if (argv[0][0] != '@') 4923 allow_usage(un, B_FALSE, 4924 gettext("invalid set name: missing '@' prefix\n")); 4925 opts->who = argv[0]; 4926 } else if (opts->create) { 4927 if (ldcsuge_sum > 1) 4928 allow_usage(un, B_FALSE, 4929 gettext("invalid options combined with -c\n")); 4930 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4931 } else if (opts->everyone) { 4932 if (csuge_sum > 1) 4933 allow_usage(un, B_FALSE, 4934 gettext("invalid options combined with -e\n")); 4935 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4936 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone") 4937 == 0) { 4938 opts->everyone = B_TRUE; 4939 argc--; 4940 argv++; 4941 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4942 } else if (argc == 1 && !un) { 4943 opts->prt_perms = B_TRUE; 4944 opts->dataset = argv[argc-1]; 4945 } else { 4946 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4947 opts->who = argv[0]; 4948 } 4949 4950 if (!opts->local && !opts->descend) { 4951 opts->local = B_TRUE; 4952 opts->descend = B_TRUE; 4953 } 4954} 4955 4956static void 4957store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend, 4958 const char *who, char *perms, nvlist_t *top_nvl) 4959{ 4960 int i; 4961 char ld[2] = { '\0', '\0' }; 4962 char who_buf[MAXNAMELEN + 32]; 4963 char base_type = '\0'; 4964 char set_type = '\0'; 4965 nvlist_t *base_nvl = NULL; 4966 nvlist_t *set_nvl = NULL; 4967 nvlist_t *nvl; 4968 4969 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0) 4970 nomem(); 4971 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0) 4972 nomem(); 4973 4974 switch (type) { 4975 case ZFS_DELEG_NAMED_SET_SETS: 4976 case ZFS_DELEG_NAMED_SET: 4977 set_type = ZFS_DELEG_NAMED_SET_SETS; 4978 base_type = ZFS_DELEG_NAMED_SET; 4979 ld[0] = ZFS_DELEG_NA; 4980 break; 4981 case ZFS_DELEG_CREATE_SETS: 4982 case ZFS_DELEG_CREATE: 4983 set_type = ZFS_DELEG_CREATE_SETS; 4984 base_type = ZFS_DELEG_CREATE; 4985 ld[0] = ZFS_DELEG_NA; 4986 break; 4987 case ZFS_DELEG_USER_SETS: 4988 case ZFS_DELEG_USER: 4989 set_type = ZFS_DELEG_USER_SETS; 4990 base_type = ZFS_DELEG_USER; 4991 if (local) 4992 ld[0] = ZFS_DELEG_LOCAL; 4993 if (descend) 4994 ld[1] = ZFS_DELEG_DESCENDENT; 4995 break; 4996 case ZFS_DELEG_GROUP_SETS: 4997 case ZFS_DELEG_GROUP: 4998 set_type = ZFS_DELEG_GROUP_SETS; 4999 base_type = ZFS_DELEG_GROUP; 5000 if (local) 5001 ld[0] = ZFS_DELEG_LOCAL; 5002 if (descend) 5003 ld[1] = ZFS_DELEG_DESCENDENT; 5004 break; 5005 case ZFS_DELEG_EVERYONE_SETS: 5006 case ZFS_DELEG_EVERYONE: 5007 set_type = ZFS_DELEG_EVERYONE_SETS; 5008 base_type = ZFS_DELEG_EVERYONE; 5009 if (local) 5010 ld[0] = ZFS_DELEG_LOCAL; 5011 if (descend) 5012 ld[1] = ZFS_DELEG_DESCENDENT; 5013 break; 5014 5015 default: 5016 assert(set_type != '\0' && base_type != '\0'); 5017 } 5018 5019 if (perms != NULL) { 5020 char *curr = perms; 5021 char *end = curr + strlen(perms); 5022 5023 while (curr < end) { 5024 char *delim = strchr(curr, ','); 5025 if (delim == NULL) 5026 delim = end; 5027 else 5028 *delim = '\0'; 5029 5030 if (curr[0] == '@') 5031 nvl = set_nvl; 5032 else 5033 nvl = base_nvl; 5034 5035 (void) nvlist_add_boolean(nvl, curr); 5036 if (delim != end) 5037 *delim = ','; 5038 curr = delim + 1; 5039 } 5040 5041 for (i = 0; i < 2; i++) { 5042 char locality = ld[i]; 5043 if (locality == 0) 5044 continue; 5045 5046 if (!nvlist_empty(base_nvl)) { 5047 if (who != NULL) 5048 (void) snprintf(who_buf, 5049 sizeof (who_buf), "%c%c$%s", 5050 base_type, locality, who); 5051 else 5052 (void) snprintf(who_buf, 5053 sizeof (who_buf), "%c%c$", 5054 base_type, locality); 5055 5056 (void) nvlist_add_nvlist(top_nvl, who_buf, 5057 base_nvl); 5058 } 5059 5060 5061 if (!nvlist_empty(set_nvl)) { 5062 if (who != NULL) 5063 (void) snprintf(who_buf, 5064 sizeof (who_buf), "%c%c$%s", 5065 set_type, locality, who); 5066 else 5067 (void) snprintf(who_buf, 5068 sizeof (who_buf), "%c%c$", 5069 set_type, locality); 5070 5071 (void) nvlist_add_nvlist(top_nvl, who_buf, 5072 set_nvl); 5073 } 5074 } 5075 } else { 5076 for (i = 0; i < 2; i++) { 5077 char locality = ld[i]; 5078 if (locality == 0) 5079 continue; 5080 5081 if (who != NULL) 5082 (void) snprintf(who_buf, sizeof (who_buf), 5083 "%c%c$%s", base_type, locality, who); 5084 else 5085 (void) snprintf(who_buf, sizeof (who_buf), 5086 "%c%c$", base_type, locality); 5087 (void) nvlist_add_boolean(top_nvl, who_buf); 5088 5089 if (who != NULL) 5090 (void) snprintf(who_buf, sizeof (who_buf), 5091 "%c%c$%s", set_type, locality, who); 5092 else 5093 (void) snprintf(who_buf, sizeof (who_buf), 5094 "%c%c$", set_type, locality); 5095 (void) nvlist_add_boolean(top_nvl, who_buf); 5096 } 5097 } 5098} 5099 5100static int 5101construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp) 5102{ 5103 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) 5104 nomem(); 5105 5106 if (opts->set) { 5107 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local, 5108 opts->descend, opts->who, opts->perms, *nvlp); 5109 } else if (opts->create) { 5110 store_allow_perm(ZFS_DELEG_CREATE, opts->local, 5111 opts->descend, NULL, opts->perms, *nvlp); 5112 } else if (opts->everyone) { 5113 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local, 5114 opts->descend, NULL, opts->perms, *nvlp); 5115 } else { 5116 char *curr = opts->who; 5117 char *end = curr + strlen(curr); 5118 5119 while (curr < end) { 5120 const char *who; 5121 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN; 5122 char *endch; 5123 char *delim = strchr(curr, ','); 5124 char errbuf[256]; 5125 char id[64]; 5126 struct passwd *p = NULL; 5127 struct group *g = NULL; 5128 5129 uid_t rid; 5130 if (delim == NULL) 5131 delim = end; 5132 else 5133 *delim = '\0'; 5134 5135 rid = (uid_t)strtol(curr, &endch, 0); 5136 if (opts->user) { 5137 who_type = ZFS_DELEG_USER; 5138 if (*endch != '\0') 5139 p = getpwnam(curr); 5140 else 5141 p = getpwuid(rid); 5142 5143 if (p != NULL) 5144 rid = p->pw_uid; 5145 else { 5146 (void) snprintf(errbuf, 256, gettext( 5147 "invalid user %s"), curr); 5148 allow_usage(un, B_TRUE, errbuf); 5149 } 5150 } else if (opts->group) { 5151 who_type = ZFS_DELEG_GROUP; 5152 if (*endch != '\0') 5153 g = getgrnam(curr); 5154 else 5155 g = getgrgid(rid); 5156 5157 if (g != NULL) 5158 rid = g->gr_gid; 5159 else { 5160 (void) snprintf(errbuf, 256, gettext( 5161 "invalid group %s"), curr); 5162 allow_usage(un, B_TRUE, errbuf); 5163 } 5164 } else { 5165 if (*endch != '\0') { 5166 p = getpwnam(curr); 5167 } else { 5168 p = getpwuid(rid); 5169 } 5170 5171 if (p == NULL) { 5172 if (*endch != '\0') { 5173 g = getgrnam(curr); 5174 } else { 5175 g = getgrgid(rid); 5176 } 5177 } 5178 5179 if (p != NULL) { 5180 who_type = ZFS_DELEG_USER; 5181 rid = p->pw_uid; 5182 } else if (g != NULL) { 5183 who_type = ZFS_DELEG_GROUP; 5184 rid = g->gr_gid; 5185 } else { 5186 (void) snprintf(errbuf, 256, gettext( 5187 "invalid user/group %s"), curr); 5188 allow_usage(un, B_TRUE, errbuf); 5189 } 5190 } 5191 5192 (void) sprintf(id, "%u", rid); 5193 who = id; 5194 5195 store_allow_perm(who_type, opts->local, 5196 opts->descend, who, opts->perms, *nvlp); 5197 curr = delim + 1; 5198 } 5199 } 5200 5201 return (0); 5202} 5203 5204static void 5205print_set_creat_perms(uu_avl_t *who_avl) 5206{ 5207 const char *sc_title[] = { 5208 gettext("Permission sets:\n"), 5209 gettext("Create time permissions:\n"), 5210 NULL 5211 }; 5212 const char **title_ptr = sc_title; 5213 who_perm_node_t *who_node = NULL; 5214 int prev_weight = -1; 5215 5216 for (who_node = uu_avl_first(who_avl); who_node != NULL; 5217 who_node = uu_avl_next(who_avl, who_node)) { 5218 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 5219 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 5220 const char *who_name = who_node->who_perm.who_name; 5221 int weight = who_type2weight(who_type); 5222 boolean_t first = B_TRUE; 5223 deleg_perm_node_t *deleg_node; 5224 5225 if (prev_weight != weight) { 5226 (void) printf(*title_ptr++); 5227 prev_weight = weight; 5228 } 5229 5230 if (who_name == NULL || strnlen(who_name, 1) == 0) 5231 (void) printf("\t"); 5232 else 5233 (void) printf("\t%s ", who_name); 5234 5235 for (deleg_node = uu_avl_first(avl); deleg_node != NULL; 5236 deleg_node = uu_avl_next(avl, deleg_node)) { 5237 if (first) { 5238 (void) printf("%s", 5239 deleg_node->dpn_perm.dp_name); 5240 first = B_FALSE; 5241 } else 5242 (void) printf(",%s", 5243 deleg_node->dpn_perm.dp_name); 5244 } 5245 5246 (void) printf("\n"); 5247 } 5248} 5249 5250static void 5251print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend, 5252 const char *title) 5253{ 5254 who_perm_node_t *who_node = NULL; 5255 boolean_t prt_title = B_TRUE; 5256 uu_avl_walk_t *walk; 5257 5258 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL) 5259 nomem(); 5260 5261 while ((who_node = uu_avl_walk_next(walk)) != NULL) { 5262 const char *who_name = who_node->who_perm.who_name; 5263 const char *nice_who_name = who_node->who_perm.who_ug_name; 5264 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 5265 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 5266 char delim = ' '; 5267 deleg_perm_node_t *deleg_node; 5268 boolean_t prt_who = B_TRUE; 5269 5270 for (deleg_node = uu_avl_first(avl); 5271 deleg_node != NULL; 5272 deleg_node = uu_avl_next(avl, deleg_node)) { 5273 if (local != deleg_node->dpn_perm.dp_local || 5274 descend != deleg_node->dpn_perm.dp_descend) 5275 continue; 5276 5277 if (prt_who) { 5278 const char *who = NULL; 5279 if (prt_title) { 5280 prt_title = B_FALSE; 5281 (void) printf(title); 5282 } 5283 5284 switch (who_type) { 5285 case ZFS_DELEG_USER_SETS: 5286 case ZFS_DELEG_USER: 5287 who = gettext("user"); 5288 if (nice_who_name) 5289 who_name = nice_who_name; 5290 break; 5291 case ZFS_DELEG_GROUP_SETS: 5292 case ZFS_DELEG_GROUP: 5293 who = gettext("group"); 5294 if (nice_who_name) 5295 who_name = nice_who_name; 5296 break; 5297 case ZFS_DELEG_EVERYONE_SETS: 5298 case ZFS_DELEG_EVERYONE: 5299 who = gettext("everyone"); 5300 who_name = NULL; 5301 break; 5302 5303 default: 5304 assert(who != NULL); 5305 } 5306 5307 prt_who = B_FALSE; 5308 if (who_name == NULL) 5309 (void) printf("\t%s", who); 5310 else 5311 (void) printf("\t%s %s", who, who_name); 5312 } 5313 5314 (void) printf("%c%s", delim, 5315 deleg_node->dpn_perm.dp_name); 5316 delim = ','; 5317 } 5318 5319 if (!prt_who) 5320 (void) printf("\n"); 5321 } 5322 5323 uu_avl_walk_end(walk); 5324} 5325 5326static void 5327print_fs_perms(fs_perm_set_t *fspset) 5328{ 5329 fs_perm_node_t *node = NULL; 5330 char buf[MAXNAMELEN + 32]; 5331 const char *dsname = buf; 5332 5333 for (node = uu_list_first(fspset->fsps_list); node != NULL; 5334 node = uu_list_next(fspset->fsps_list, node)) { 5335 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl; 5336 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl; 5337 int left = 0; 5338 5339 (void) snprintf(buf, sizeof (buf), 5340 gettext("---- Permissions on %s "), 5341 node->fspn_fsperm.fsp_name); 5342 (void) printf(dsname); 5343 left = 70 - strlen(buf); 5344 while (left-- > 0) 5345 (void) printf("-"); 5346 (void) printf("\n"); 5347 5348 print_set_creat_perms(sc_avl); 5349 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE, 5350 gettext("Local permissions:\n")); 5351 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE, 5352 gettext("Descendent permissions:\n")); 5353 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE, 5354 gettext("Local+Descendent permissions:\n")); 5355 } 5356} 5357 5358static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL }; 5359 5360struct deleg_perms { 5361 boolean_t un; 5362 nvlist_t *nvl; 5363}; 5364 5365static int 5366set_deleg_perms(zfs_handle_t *zhp, void *data) 5367{ 5368 struct deleg_perms *perms = (struct deleg_perms *)data; 5369 zfs_type_t zfs_type = zfs_get_type(zhp); 5370 5371 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME) 5372 return (0); 5373 5374 return (zfs_set_fsacl(zhp, perms->un, perms->nvl)); 5375} 5376 5377static int 5378zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un) 5379{ 5380 zfs_handle_t *zhp; 5381 nvlist_t *perm_nvl = NULL; 5382 nvlist_t *update_perm_nvl = NULL; 5383 int error = 1; 5384 int c; 5385 struct allow_opts opts = { 0 }; 5386 5387 const char *optstr = un ? "ldugecsrh" : "ldugecsh"; 5388 5389 /* check opts */ 5390 while ((c = getopt(argc, argv, optstr)) != -1) { 5391 switch (c) { 5392 case 'l': 5393 opts.local = B_TRUE; 5394 break; 5395 case 'd': 5396 opts.descend = B_TRUE; 5397 break; 5398 case 'u': 5399 opts.user = B_TRUE; 5400 break; 5401 case 'g': 5402 opts.group = B_TRUE; 5403 break; 5404 case 'e': 5405 opts.everyone = B_TRUE; 5406 break; 5407 case 's': 5408 opts.set = B_TRUE; 5409 break; 5410 case 'c': 5411 opts.create = B_TRUE; 5412 break; 5413 case 'r': 5414 opts.recursive = B_TRUE; 5415 break; 5416 case ':': 5417 (void) fprintf(stderr, gettext("missing argument for " 5418 "'%c' option\n"), optopt); 5419 usage(B_FALSE); 5420 break; 5421 case 'h': 5422 opts.prt_usage = B_TRUE; 5423 break; 5424 case '?': 5425 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5426 optopt); 5427 usage(B_FALSE); 5428 } 5429 } 5430 5431 argc -= optind; 5432 argv += optind; 5433 5434 /* check arguments */ 5435 parse_allow_args(argc, argv, un, &opts); 5436 5437 /* try to open the dataset */ 5438 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM | 5439 ZFS_TYPE_VOLUME)) == NULL) { 5440 (void) fprintf(stderr, "Failed to open dataset: %s\n", 5441 opts.dataset); 5442 return (-1); 5443 } 5444 5445 if (zfs_get_fsacl(zhp, &perm_nvl) != 0) 5446 goto cleanup2; 5447 5448 fs_perm_set_init(&fs_perm_set); 5449 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) { 5450 (void) fprintf(stderr, "Failed to parse fsacl permissions\n"); 5451 goto cleanup1; 5452 } 5453 5454 if (opts.prt_perms) 5455 print_fs_perms(&fs_perm_set); 5456 else { 5457 (void) construct_fsacl_list(un, &opts, &update_perm_nvl); 5458 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0) 5459 goto cleanup0; 5460 5461 if (un && opts.recursive) { 5462 struct deleg_perms data = { un, update_perm_nvl }; 5463 if (zfs_iter_filesystems(zhp, set_deleg_perms, 5464 &data) != 0) 5465 goto cleanup0; 5466 } 5467 } 5468 5469 error = 0; 5470 5471cleanup0: 5472 nvlist_free(perm_nvl); 5473 nvlist_free(update_perm_nvl); 5474cleanup1: 5475 fs_perm_set_fini(&fs_perm_set); 5476cleanup2: 5477 zfs_close(zhp); 5478 5479 return (error); 5480} 5481 5482static int 5483zfs_do_allow(int argc, char **argv) 5484{ 5485 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE)); 5486} 5487 5488static int 5489zfs_do_unallow(int argc, char **argv) 5490{ 5491 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE)); 5492} 5493 5494static int 5495zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding) 5496{ 5497 int errors = 0; 5498 int i; 5499 const char *tag; 5500 boolean_t recursive = B_FALSE; 5501 const char *opts = holding ? "rt" : "r"; 5502 int c; 5503 5504 /* check options */ 5505 while ((c = getopt(argc, argv, opts)) != -1) { 5506 switch (c) { 5507 case 'r': 5508 recursive = B_TRUE; 5509 break; 5510 case '?': 5511 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5512 optopt); 5513 usage(B_FALSE); 5514 } 5515 } 5516 5517 argc -= optind; 5518 argv += optind; 5519 5520 /* check number of arguments */ 5521 if (argc < 2) 5522 usage(B_FALSE); 5523 5524 tag = argv[0]; 5525 --argc; 5526 ++argv; 5527 5528 if (holding && tag[0] == '.') { 5529 /* tags starting with '.' are reserved for libzfs */ 5530 (void) fprintf(stderr, gettext("tag may not start with '.'\n")); 5531 usage(B_FALSE); 5532 } 5533 5534 for (i = 0; i < argc; ++i) { 5535 zfs_handle_t *zhp; 5536 char parent[ZFS_MAX_DATASET_NAME_LEN]; 5537 const char *delim; 5538 char *path = argv[i]; 5539 5540 delim = strchr(path, '@'); 5541 if (delim == NULL) { 5542 (void) fprintf(stderr, 5543 gettext("'%s' is not a snapshot\n"), path); 5544 ++errors; 5545 continue; 5546 } 5547 (void) strncpy(parent, path, delim - path); 5548 parent[delim - path] = '\0'; 5549 5550 zhp = zfs_open(g_zfs, parent, 5551 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 5552 if (zhp == NULL) { 5553 ++errors; 5554 continue; 5555 } 5556 if (holding) { 5557 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0) 5558 ++errors; 5559 } else { 5560 if (zfs_release(zhp, delim+1, tag, recursive) != 0) 5561 ++errors; 5562 } 5563 zfs_close(zhp); 5564 } 5565 5566 return (errors != 0); 5567} 5568 5569/* 5570 * zfs hold [-r] [-t] <tag> <snap> ... 5571 * 5572 * -r Recursively hold 5573 * 5574 * Apply a user-hold with the given tag to the list of snapshots. 5575 */ 5576static int 5577zfs_do_hold(int argc, char **argv) 5578{ 5579 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE)); 5580} 5581 5582/* 5583 * zfs release [-r] <tag> <snap> ... 5584 * 5585 * -r Recursively release 5586 * 5587 * Release a user-hold with the given tag from the list of snapshots. 5588 */ 5589static int 5590zfs_do_release(int argc, char **argv) 5591{ 5592 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE)); 5593} 5594 5595typedef struct holds_cbdata { 5596 boolean_t cb_recursive; 5597 const char *cb_snapname; 5598 nvlist_t **cb_nvlp; 5599 size_t cb_max_namelen; 5600 size_t cb_max_taglen; 5601} holds_cbdata_t; 5602 5603#define STRFTIME_FMT_STR "%a %b %e %k:%M %Y" 5604#define DATETIME_BUF_LEN (32) 5605/* 5606 * 5607 */ 5608static void 5609print_holds(boolean_t scripted, boolean_t literal, size_t nwidth, 5610 size_t tagwidth, nvlist_t *nvl) 5611{ 5612 int i; 5613 nvpair_t *nvp = NULL; 5614 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" }; 5615 const char *col; 5616 5617 if (!scripted) { 5618 for (i = 0; i < 3; i++) { 5619 col = gettext(hdr_cols[i]); 5620 if (i < 2) 5621 (void) printf("%-*s ", i ? tagwidth : nwidth, 5622 col); 5623 else 5624 (void) printf("%s\n", col); 5625 } 5626 } 5627 5628 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5629 char *zname = nvpair_name(nvp); 5630 nvlist_t *nvl2; 5631 nvpair_t *nvp2 = NULL; 5632 (void) nvpair_value_nvlist(nvp, &nvl2); 5633 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) { 5634 char tsbuf[DATETIME_BUF_LEN]; 5635 char *tagname = nvpair_name(nvp2); 5636 uint64_t val = 0; 5637 time_t time; 5638 struct tm t; 5639 char sep = scripted ? '\t' : ' '; 5640 size_t sepnum = scripted ? 1 : 2; 5641 5642 (void) nvpair_value_uint64(nvp2, &val); 5643 if (literal) 5644 snprintf(tsbuf, DATETIME_BUF_LEN, "%llu", val); 5645 else { 5646 time = (time_t)val; 5647 (void) localtime_r(&time, &t); 5648 (void) strftime(tsbuf, DATETIME_BUF_LEN, 5649 gettext(STRFTIME_FMT_STR), &t); 5650 } 5651 5652 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname, 5653 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf); 5654 } 5655 } 5656} 5657 5658/* 5659 * Generic callback function to list a dataset or snapshot. 5660 */ 5661static int 5662holds_callback(zfs_handle_t *zhp, void *data) 5663{ 5664 holds_cbdata_t *cbp = data; 5665 nvlist_t *top_nvl = *cbp->cb_nvlp; 5666 nvlist_t *nvl = NULL; 5667 nvpair_t *nvp = NULL; 5668 const char *zname = zfs_get_name(zhp); 5669 size_t znamelen = strlen(zname); 5670 5671 if (cbp->cb_recursive && cbp->cb_snapname != NULL) { 5672 const char *snapname; 5673 char *delim = strchr(zname, '@'); 5674 if (delim == NULL) 5675 return (0); 5676 5677 snapname = delim + 1; 5678 if (strcmp(cbp->cb_snapname, snapname)) 5679 return (0); 5680 } 5681 5682 if (zfs_get_holds(zhp, &nvl) != 0) 5683 return (-1); 5684 5685 if (znamelen > cbp->cb_max_namelen) 5686 cbp->cb_max_namelen = znamelen; 5687 5688 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5689 const char *tag = nvpair_name(nvp); 5690 size_t taglen = strlen(tag); 5691 if (taglen > cbp->cb_max_taglen) 5692 cbp->cb_max_taglen = taglen; 5693 } 5694 5695 return (nvlist_add_nvlist(top_nvl, zname, nvl)); 5696} 5697 5698/* 5699 * zfs holds [-Hp] [-r | -d max] <dataset|snap> ... 5700 * 5701 * -H Suppress header output 5702 * -p Output literal values 5703 * -r Recursively search for holds 5704 * -d max Limit depth of recursive search 5705 */ 5706static int 5707zfs_do_holds(int argc, char **argv) 5708{ 5709 int errors = 0; 5710 int c; 5711 int i; 5712 boolean_t scripted = B_FALSE; 5713 boolean_t literal = B_FALSE; 5714 boolean_t recursive = B_FALSE; 5715 const char *opts = "d:rHp"; 5716 nvlist_t *nvl; 5717 5718 int types = ZFS_TYPE_SNAPSHOT; 5719 holds_cbdata_t cb = { 0 }; 5720 5721 int limit = 0; 5722 int ret = 0; 5723 int flags = 0; 5724 5725 /* check options */ 5726 while ((c = getopt(argc, argv, opts)) != -1) { 5727 switch (c) { 5728 case 'd': 5729 limit = parse_depth(optarg, &flags); 5730 recursive = B_TRUE; 5731 break; 5732 case 'r': 5733 recursive = B_TRUE; 5734 break; 5735 case 'H': 5736 scripted = B_TRUE; 5737 break; 5738 case 'p': 5739 literal = B_TRUE; 5740 break; 5741 case '?': 5742 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5743 optopt); 5744 usage(B_FALSE); 5745 } 5746 } 5747 5748 if (recursive) { 5749 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 5750 flags |= ZFS_ITER_RECURSE; 5751 } 5752 5753 argc -= optind; 5754 argv += optind; 5755 5756 /* check number of arguments */ 5757 if (argc < 1) 5758 usage(B_FALSE); 5759 5760 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5761 nomem(); 5762 5763 for (i = 0; i < argc; ++i) { 5764 char *snapshot = argv[i]; 5765 const char *delim; 5766 const char *snapname = NULL; 5767 5768 delim = strchr(snapshot, '@'); 5769 if (delim != NULL) { 5770 snapname = delim + 1; 5771 if (recursive) 5772 snapshot[delim - snapshot] = '\0'; 5773 } 5774 5775 cb.cb_recursive = recursive; 5776 cb.cb_snapname = snapname; 5777 cb.cb_nvlp = &nvl; 5778 5779 /* 5780 * 1. collect holds data, set format options 5781 */ 5782 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit, 5783 holds_callback, &cb); 5784 if (ret != 0) 5785 ++errors; 5786 } 5787 5788 /* 5789 * 2. print holds data 5790 */ 5791 print_holds(scripted, literal, cb.cb_max_namelen, cb.cb_max_taglen, 5792 nvl); 5793 5794 if (nvlist_empty(nvl)) 5795 (void) printf(gettext("no datasets available\n")); 5796 5797 nvlist_free(nvl); 5798 5799 return (0 != errors); 5800} 5801 5802#define CHECK_SPINNER 30 5803#define SPINNER_TIME 3 /* seconds */ 5804#define MOUNT_TIME 5 /* seconds */ 5805 5806static int 5807get_one_dataset(zfs_handle_t *zhp, void *data) 5808{ 5809 static char *spin[] = { "-", "\\", "|", "/" }; 5810 static int spinval = 0; 5811 static int spincheck = 0; 5812 static time_t last_spin_time = (time_t)0; 5813 get_all_cb_t *cbp = data; 5814 zfs_type_t type = zfs_get_type(zhp); 5815 5816 if (cbp->cb_verbose) { 5817 if (--spincheck < 0) { 5818 time_t now = time(NULL); 5819 if (last_spin_time + SPINNER_TIME < now) { 5820 update_progress(spin[spinval++ % 4]); 5821 last_spin_time = now; 5822 } 5823 spincheck = CHECK_SPINNER; 5824 } 5825 } 5826 5827 /* 5828 * Interate over any nested datasets. 5829 */ 5830 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) { 5831 zfs_close(zhp); 5832 return (1); 5833 } 5834 5835 /* 5836 * Skip any datasets whose type does not match. 5837 */ 5838 if ((type & ZFS_TYPE_FILESYSTEM) == 0) { 5839 zfs_close(zhp); 5840 return (0); 5841 } 5842 libzfs_add_handle(cbp, zhp); 5843 assert(cbp->cb_used <= cbp->cb_alloc); 5844 5845 return (0); 5846} 5847 5848static void 5849get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose) 5850{ 5851 get_all_cb_t cb = { 0 }; 5852 cb.cb_verbose = verbose; 5853 cb.cb_getone = get_one_dataset; 5854 5855 if (verbose) 5856 set_progress_header(gettext("Reading ZFS config")); 5857 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb); 5858 5859 *dslist = cb.cb_handles; 5860 *count = cb.cb_used; 5861 5862 if (verbose) 5863 finish_progress(gettext("done.")); 5864} 5865 5866/* 5867 * Generic callback for sharing or mounting filesystems. Because the code is so 5868 * similar, we have a common function with an extra parameter to determine which 5869 * mode we are using. 5870 */ 5871#define OP_SHARE 0x1 5872#define OP_MOUNT 0x2 5873 5874/* 5875 * Share or mount a dataset. 5876 */ 5877static int 5878share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, 5879 boolean_t explicit, const char *options) 5880{ 5881 char mountpoint[ZFS_MAXPROPLEN]; 5882 char shareopts[ZFS_MAXPROPLEN]; 5883 char smbshareopts[ZFS_MAXPROPLEN]; 5884 const char *cmdname = op == OP_SHARE ? "share" : "mount"; 5885 struct mnttab mnt; 5886 uint64_t zoned, canmount; 5887 boolean_t shared_nfs, shared_smb; 5888 5889 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM); 5890 5891 /* 5892 * Check to make sure we can mount/share this dataset. If we 5893 * are in the global zone and the filesystem is exported to a 5894 * local zone, or if we are in a local zone and the 5895 * filesystem is not exported, then it is an error. 5896 */ 5897 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 5898 5899 if (zoned && getzoneid() == GLOBAL_ZONEID) { 5900 if (!explicit) 5901 return (0); 5902 5903 (void) fprintf(stderr, gettext("cannot %s '%s': " 5904 "dataset is exported to a local zone\n"), cmdname, 5905 zfs_get_name(zhp)); 5906 return (1); 5907 5908 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 5909 if (!explicit) 5910 return (0); 5911 5912 (void) fprintf(stderr, gettext("cannot %s '%s': " 5913 "permission denied\n"), cmdname, 5914 zfs_get_name(zhp)); 5915 return (1); 5916 } 5917 5918 /* 5919 * Ignore any filesystems which don't apply to us. This 5920 * includes those with a legacy mountpoint, or those with 5921 * legacy share options. 5922 */ 5923 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 5924 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 5925 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 5926 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 5927 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts, 5928 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0); 5929 5930 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 && 5931 strcmp(smbshareopts, "off") == 0) { 5932 if (!explicit) 5933 return (0); 5934 5935 (void) fprintf(stderr, gettext("cannot share '%s': " 5936 "legacy share\n"), zfs_get_name(zhp)); 5937 (void) fprintf(stderr, gettext("to " 5938 "share this filesystem set " 5939 "sharenfs property on\n")); 5940 return (1); 5941 } 5942 5943 /* 5944 * We cannot share or mount legacy filesystems. If the 5945 * shareopts is non-legacy but the mountpoint is legacy, we 5946 * treat it as a legacy share. 5947 */ 5948 if (strcmp(mountpoint, "legacy") == 0) { 5949 if (!explicit) 5950 return (0); 5951 5952 (void) fprintf(stderr, gettext("cannot %s '%s': " 5953 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 5954 (void) fprintf(stderr, gettext("use %s(8) to " 5955 "%s this filesystem\n"), cmdname, cmdname); 5956 return (1); 5957 } 5958 5959 if (strcmp(mountpoint, "none") == 0) { 5960 if (!explicit) 5961 return (0); 5962 5963 (void) fprintf(stderr, gettext("cannot %s '%s': no " 5964 "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 5965 return (1); 5966 } 5967 5968 /* 5969 * canmount explicit outcome 5970 * on no pass through 5971 * on yes pass through 5972 * off no return 0 5973 * off yes display error, return 1 5974 * noauto no return 0 5975 * noauto yes pass through 5976 */ 5977 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 5978 if (canmount == ZFS_CANMOUNT_OFF) { 5979 if (!explicit) 5980 return (0); 5981 5982 (void) fprintf(stderr, gettext("cannot %s '%s': " 5983 "'canmount' property is set to 'off'\n"), cmdname, 5984 zfs_get_name(zhp)); 5985 return (1); 5986 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) { 5987 return (0); 5988 } 5989 5990 /* 5991 * If this filesystem is inconsistent and has a receive resume 5992 * token, we can not mount it. 5993 */ 5994 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) && 5995 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 5996 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) { 5997 if (!explicit) 5998 return (0); 5999 6000 (void) fprintf(stderr, gettext("cannot %s '%s': " 6001 "Contains partially-completed state from " 6002 "\"zfs receive -r\", which can be resumed with " 6003 "\"zfs send -t\"\n"), 6004 cmdname, zfs_get_name(zhp)); 6005 return (1); 6006 } 6007 6008 /* 6009 * At this point, we have verified that the mountpoint and/or 6010 * shareopts are appropriate for auto management. If the 6011 * filesystem is already mounted or shared, return (failing 6012 * for explicit requests); otherwise mount or share the 6013 * filesystem. 6014 */ 6015 switch (op) { 6016 case OP_SHARE: 6017 6018 shared_nfs = zfs_is_shared_nfs(zhp, NULL); 6019 shared_smb = zfs_is_shared_smb(zhp, NULL); 6020 6021 if ((shared_nfs && shared_smb) || 6022 (shared_nfs && strcmp(shareopts, "on") == 0 && 6023 strcmp(smbshareopts, "off") == 0) || 6024 (shared_smb && strcmp(smbshareopts, "on") == 0 && 6025 strcmp(shareopts, "off") == 0)) { 6026 if (!explicit) 6027 return (0); 6028 6029 (void) fprintf(stderr, gettext("cannot share " 6030 "'%s': filesystem already shared\n"), 6031 zfs_get_name(zhp)); 6032 return (1); 6033 } 6034 6035 if (!zfs_is_mounted(zhp, NULL) && 6036 zfs_mount(zhp, NULL, 0) != 0) 6037 return (1); 6038 6039 if (protocol == NULL) { 6040 if (zfs_shareall(zhp) != 0) 6041 return (1); 6042 } else if (strcmp(protocol, "nfs") == 0) { 6043 if (zfs_share_nfs(zhp)) 6044 return (1); 6045 } else if (strcmp(protocol, "smb") == 0) { 6046 if (zfs_share_smb(zhp)) 6047 return (1); 6048 } else { 6049 (void) fprintf(stderr, gettext("cannot share " 6050 "'%s': invalid share type '%s' " 6051 "specified\n"), 6052 zfs_get_name(zhp), protocol); 6053 return (1); 6054 } 6055 6056 break; 6057 6058 case OP_MOUNT: 6059 if (options == NULL) 6060 mnt.mnt_mntopts = ""; 6061 else 6062 mnt.mnt_mntopts = (char *)options; 6063 6064 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 6065 zfs_is_mounted(zhp, NULL)) { 6066 if (!explicit) 6067 return (0); 6068 6069 (void) fprintf(stderr, gettext("cannot mount " 6070 "'%s': filesystem already mounted\n"), 6071 zfs_get_name(zhp)); 6072 return (1); 6073 } 6074 6075 if (zfs_mount(zhp, options, flags) != 0) 6076 return (1); 6077 break; 6078 } 6079 6080 return (0); 6081} 6082 6083/* 6084 * Reports progress in the form "(current/total)". Not thread-safe. 6085 */ 6086static void 6087report_mount_progress(int current, int total) 6088{ 6089 static time_t last_progress_time = 0; 6090 time_t now = time(NULL); 6091 char info[32]; 6092 6093 /* report 1..n instead of 0..n-1 */ 6094 ++current; 6095 6096 /* display header if we're here for the first time */ 6097 if (current == 1) { 6098 set_progress_header(gettext("Mounting ZFS filesystems")); 6099 } else if (current != total && last_progress_time + MOUNT_TIME >= now) { 6100 /* too soon to report again */ 6101 return; 6102 } 6103 6104 last_progress_time = now; 6105 6106 (void) sprintf(info, "(%d/%d)", current, total); 6107 6108 if (current == total) 6109 finish_progress(info); 6110 else 6111 update_progress(info); 6112} 6113 6114static void 6115append_options(char *mntopts, char *newopts) 6116{ 6117 int len = strlen(mntopts); 6118 6119 /* original length plus new string to append plus 1 for the comma */ 6120 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) { 6121 (void) fprintf(stderr, gettext("the opts argument for " 6122 "'%c' option is too long (more than %d chars)\n"), 6123 "-o", MNT_LINE_MAX); 6124 usage(B_FALSE); 6125 } 6126 6127 if (*mntopts) 6128 mntopts[len++] = ','; 6129 6130 (void) strcpy(&mntopts[len], newopts); 6131} 6132 6133static int 6134share_mount(int op, int argc, char **argv) 6135{ 6136 int do_all = 0; 6137 boolean_t verbose = B_FALSE; 6138 int c, ret = 0; 6139 char *options = NULL; 6140 int flags = 0; 6141 6142 /* check options */ 6143 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a")) 6144 != -1) { 6145 switch (c) { 6146 case 'a': 6147 do_all = 1; 6148 break; 6149 case 'v': 6150 verbose = B_TRUE; 6151 break; 6152 case 'o': 6153 if (*optarg == '\0') { 6154 (void) fprintf(stderr, gettext("empty mount " 6155 "options (-o) specified\n")); 6156 usage(B_FALSE); 6157 } 6158 6159 if (options == NULL) 6160 options = safe_malloc(MNT_LINE_MAX + 1); 6161 6162 /* option validation is done later */ 6163 append_options(options, optarg); 6164 break; 6165 6166 case 'O': 6167 warnx("no overlay mounts support on FreeBSD, ignoring"); 6168 break; 6169 case ':': 6170 (void) fprintf(stderr, gettext("missing argument for " 6171 "'%c' option\n"), optopt); 6172 usage(B_FALSE); 6173 break; 6174 case '?': 6175 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6176 optopt); 6177 usage(B_FALSE); 6178 } 6179 } 6180 6181 argc -= optind; 6182 argv += optind; 6183 6184 /* check number of arguments */ 6185 if (do_all) { 6186 zfs_handle_t **dslist = NULL; 6187 size_t i, count = 0; 6188 char *protocol = NULL; 6189 6190 if (op == OP_SHARE && argc > 0) { 6191 if (strcmp(argv[0], "nfs") != 0 && 6192 strcmp(argv[0], "smb") != 0) { 6193 (void) fprintf(stderr, gettext("share type " 6194 "must be 'nfs' or 'smb'\n")); 6195 usage(B_FALSE); 6196 } 6197 protocol = argv[0]; 6198 argc--; 6199 argv++; 6200 } 6201 6202 if (argc != 0) { 6203 (void) fprintf(stderr, gettext("too many arguments\n")); 6204 usage(B_FALSE); 6205 } 6206 6207 start_progress_timer(); 6208 get_all_datasets(&dslist, &count, verbose); 6209 6210 if (count == 0) 6211 return (0); 6212 6213 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp); 6214 6215 for (i = 0; i < count; i++) { 6216 if (verbose) 6217 report_mount_progress(i, count); 6218 6219 if (share_mount_one(dslist[i], op, flags, protocol, 6220 B_FALSE, options) != 0) 6221 ret = 1; 6222 zfs_close(dslist[i]); 6223 } 6224 6225 free(dslist); 6226 } else if (argc == 0) { 6227 struct mnttab entry; 6228 6229 if ((op == OP_SHARE) || (options != NULL)) { 6230 (void) fprintf(stderr, gettext("missing filesystem " 6231 "argument (specify -a for all)\n")); 6232 usage(B_FALSE); 6233 } 6234 6235 /* 6236 * When mount is given no arguments, go through /etc/mnttab and 6237 * display any active ZFS mounts. We hide any snapshots, since 6238 * they are controlled automatically. 6239 */ 6240 rewind(mnttab_file); 6241 while (getmntent(mnttab_file, &entry) == 0) { 6242 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 6243 strchr(entry.mnt_special, '@') != NULL) 6244 continue; 6245 6246 (void) printf("%-30s %s\n", entry.mnt_special, 6247 entry.mnt_mountp); 6248 } 6249 6250 } else { 6251 zfs_handle_t *zhp; 6252 6253 if (argc > 1) { 6254 (void) fprintf(stderr, 6255 gettext("too many arguments\n")); 6256 usage(B_FALSE); 6257 } 6258 6259 if ((zhp = zfs_open(g_zfs, argv[0], 6260 ZFS_TYPE_FILESYSTEM)) == NULL) { 6261 ret = 1; 6262 } else { 6263 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE, 6264 options); 6265 zfs_close(zhp); 6266 } 6267 } 6268 6269 return (ret); 6270} 6271 6272/* 6273 * zfs mount -a [nfs] 6274 * zfs mount filesystem 6275 * 6276 * Mount all filesystems, or mount the given filesystem. 6277 */ 6278static int 6279zfs_do_mount(int argc, char **argv) 6280{ 6281 return (share_mount(OP_MOUNT, argc, argv)); 6282} 6283 6284/* 6285 * zfs share -a [nfs | smb] 6286 * zfs share filesystem 6287 * 6288 * Share all filesystems, or share the given filesystem. 6289 */ 6290static int 6291zfs_do_share(int argc, char **argv) 6292{ 6293 return (share_mount(OP_SHARE, argc, argv)); 6294} 6295 6296typedef struct unshare_unmount_node { 6297 zfs_handle_t *un_zhp; 6298 char *un_mountp; 6299 uu_avl_node_t un_avlnode; 6300} unshare_unmount_node_t; 6301 6302/* ARGSUSED */ 6303static int 6304unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 6305{ 6306 const unshare_unmount_node_t *l = larg; 6307 const unshare_unmount_node_t *r = rarg; 6308 6309 return (strcmp(l->un_mountp, r->un_mountp)); 6310} 6311 6312/* 6313 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 6314 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem, 6315 * and unmount it appropriately. 6316 */ 6317static int 6318unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) 6319{ 6320 zfs_handle_t *zhp; 6321 int ret = 0; 6322 struct stat64 statbuf; 6323 struct extmnttab entry; 6324 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount"; 6325 ino_t path_inode; 6326 6327 /* 6328 * Search for the path in /etc/mnttab. Rather than looking for the 6329 * specific path, which can be fooled by non-standard paths (i.e. ".." 6330 * or "//"), we stat() the path and search for the corresponding 6331 * (major,minor) device pair. 6332 */ 6333 if (stat64(path, &statbuf) != 0) { 6334 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 6335 cmdname, path, strerror(errno)); 6336 return (1); 6337 } 6338 path_inode = statbuf.st_ino; 6339 6340 /* 6341 * Search for the given (major,minor) pair in the mount table. 6342 */ 6343#ifdef illumos 6344 rewind(mnttab_file); 6345 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) { 6346 if (entry.mnt_major == major(statbuf.st_dev) && 6347 entry.mnt_minor == minor(statbuf.st_dev)) 6348 break; 6349 } 6350#else 6351 { 6352 struct statfs sfs; 6353 6354 if (statfs(path, &sfs) != 0) { 6355 (void) fprintf(stderr, "%s: %s\n", path, 6356 strerror(errno)); 6357 ret = -1; 6358 } 6359 statfs2mnttab(&sfs, &entry); 6360 } 6361#endif 6362 if (ret != 0) { 6363 if (op == OP_SHARE) { 6364 (void) fprintf(stderr, gettext("cannot %s '%s': not " 6365 "currently mounted\n"), cmdname, path); 6366 return (1); 6367 } 6368 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"), 6369 path); 6370 if ((ret = umount2(path, flags)) != 0) 6371 (void) fprintf(stderr, gettext("%s: %s\n"), path, 6372 strerror(errno)); 6373 return (ret != 0); 6374 } 6375 6376 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6377 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 6378 "filesystem\n"), cmdname, path); 6379 return (1); 6380 } 6381 6382 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 6383 ZFS_TYPE_FILESYSTEM)) == NULL) 6384 return (1); 6385 6386 ret = 1; 6387 if (stat64(entry.mnt_mountp, &statbuf) != 0) { 6388 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 6389 cmdname, path, strerror(errno)); 6390 goto out; 6391 } else if (statbuf.st_ino != path_inode) { 6392 (void) fprintf(stderr, gettext("cannot " 6393 "%s '%s': not a mountpoint\n"), cmdname, path); 6394 goto out; 6395 } 6396 6397 if (op == OP_SHARE) { 6398 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 6399 char smbshare_prop[ZFS_MAXPROPLEN]; 6400 6401 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop, 6402 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0); 6403 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop, 6404 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0); 6405 6406 if (strcmp(nfs_mnt_prop, "off") == 0 && 6407 strcmp(smbshare_prop, "off") == 0) { 6408 (void) fprintf(stderr, gettext("cannot unshare " 6409 "'%s': legacy share\n"), path); 6410#ifdef illumos 6411 (void) fprintf(stderr, gettext("use " 6412 "unshare(1M) to unshare this filesystem\n")); 6413#endif 6414 } else if (!zfs_is_shared(zhp)) { 6415 (void) fprintf(stderr, gettext("cannot unshare '%s': " 6416 "not currently shared\n"), path); 6417 } else { 6418 ret = zfs_unshareall_bypath(zhp, path); 6419 } 6420 } else { 6421 char mtpt_prop[ZFS_MAXPROPLEN]; 6422 6423 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop, 6424 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0); 6425 6426 if (is_manual) { 6427 ret = zfs_unmount(zhp, NULL, flags); 6428 } else if (strcmp(mtpt_prop, "legacy") == 0) { 6429 (void) fprintf(stderr, gettext("cannot unmount " 6430 "'%s': legacy mountpoint\n"), 6431 zfs_get_name(zhp)); 6432 (void) fprintf(stderr, gettext("use umount(8) " 6433 "to unmount this filesystem\n")); 6434 } else { 6435 ret = zfs_unmountall(zhp, flags); 6436 } 6437 } 6438 6439out: 6440 zfs_close(zhp); 6441 6442 return (ret != 0); 6443} 6444 6445/* 6446 * Generic callback for unsharing or unmounting a filesystem. 6447 */ 6448static int 6449unshare_unmount(int op, int argc, char **argv) 6450{ 6451 int do_all = 0; 6452 int flags = 0; 6453 int ret = 0; 6454 int c; 6455 zfs_handle_t *zhp; 6456 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 6457 char sharesmb[ZFS_MAXPROPLEN]; 6458 6459 /* check options */ 6460 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) { 6461 switch (c) { 6462 case 'a': 6463 do_all = 1; 6464 break; 6465 case 'f': 6466 flags = MS_FORCE; 6467 break; 6468 case '?': 6469 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6470 optopt); 6471 usage(B_FALSE); 6472 } 6473 } 6474 6475 argc -= optind; 6476 argv += optind; 6477 6478 if (do_all) { 6479 /* 6480 * We could make use of zfs_for_each() to walk all datasets in 6481 * the system, but this would be very inefficient, especially 6482 * since we would have to linearly search /etc/mnttab for each 6483 * one. Instead, do one pass through /etc/mnttab looking for 6484 * zfs entries and call zfs_unmount() for each one. 6485 * 6486 * Things get a little tricky if the administrator has created 6487 * mountpoints beneath other ZFS filesystems. In this case, we 6488 * have to unmount the deepest filesystems first. To accomplish 6489 * this, we place all the mountpoints in an AVL tree sorted by 6490 * the special type (dataset name), and walk the result in 6491 * reverse to make sure to get any snapshots first. 6492 */ 6493 struct mnttab entry; 6494 uu_avl_pool_t *pool; 6495 uu_avl_t *tree = NULL; 6496 unshare_unmount_node_t *node; 6497 uu_avl_index_t idx; 6498 uu_avl_walk_t *walk; 6499 6500 if (argc != 0) { 6501 (void) fprintf(stderr, gettext("too many arguments\n")); 6502 usage(B_FALSE); 6503 } 6504 6505 if (((pool = uu_avl_pool_create("unmount_pool", 6506 sizeof (unshare_unmount_node_t), 6507 offsetof(unshare_unmount_node_t, un_avlnode), 6508 unshare_unmount_compare, UU_DEFAULT)) == NULL) || 6509 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL)) 6510 nomem(); 6511 6512 rewind(mnttab_file); 6513 while (getmntent(mnttab_file, &entry) == 0) { 6514 6515 /* ignore non-ZFS entries */ 6516 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 6517 continue; 6518 6519 /* ignore snapshots */ 6520 if (strchr(entry.mnt_special, '@') != NULL) 6521 continue; 6522 6523 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 6524 ZFS_TYPE_FILESYSTEM)) == NULL) { 6525 ret = 1; 6526 continue; 6527 } 6528 6529 /* 6530 * Ignore datasets that are excluded/restricted by 6531 * parent pool name. 6532 */ 6533 if (zpool_skip_pool(zfs_get_pool_name(zhp))) { 6534 zfs_close(zhp); 6535 continue; 6536 } 6537 6538 switch (op) { 6539 case OP_SHARE: 6540 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 6541 nfs_mnt_prop, 6542 sizeof (nfs_mnt_prop), 6543 NULL, NULL, 0, B_FALSE) == 0); 6544 if (strcmp(nfs_mnt_prop, "off") != 0) 6545 break; 6546 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 6547 nfs_mnt_prop, 6548 sizeof (nfs_mnt_prop), 6549 NULL, NULL, 0, B_FALSE) == 0); 6550 if (strcmp(nfs_mnt_prop, "off") == 0) 6551 continue; 6552 break; 6553 case OP_MOUNT: 6554 /* Ignore legacy mounts */ 6555 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, 6556 nfs_mnt_prop, 6557 sizeof (nfs_mnt_prop), 6558 NULL, NULL, 0, B_FALSE) == 0); 6559 if (strcmp(nfs_mnt_prop, "legacy") == 0) 6560 continue; 6561 /* Ignore canmount=noauto mounts */ 6562 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == 6563 ZFS_CANMOUNT_NOAUTO) 6564 continue; 6565 default: 6566 break; 6567 } 6568 6569 node = safe_malloc(sizeof (unshare_unmount_node_t)); 6570 node->un_zhp = zhp; 6571 node->un_mountp = safe_strdup(entry.mnt_mountp); 6572 6573 uu_avl_node_init(node, &node->un_avlnode, pool); 6574 6575 if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 6576 uu_avl_insert(tree, node, idx); 6577 } else { 6578 zfs_close(node->un_zhp); 6579 free(node->un_mountp); 6580 free(node); 6581 } 6582 } 6583 6584 /* 6585 * Walk the AVL tree in reverse, unmounting each filesystem and 6586 * removing it from the AVL tree in the process. 6587 */ 6588 if ((walk = uu_avl_walk_start(tree, 6589 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) 6590 nomem(); 6591 6592 while ((node = uu_avl_walk_next(walk)) != NULL) { 6593 uu_avl_remove(tree, node); 6594 6595 switch (op) { 6596 case OP_SHARE: 6597 if (zfs_unshareall_bypath(node->un_zhp, 6598 node->un_mountp) != 0) 6599 ret = 1; 6600 break; 6601 6602 case OP_MOUNT: 6603 if (zfs_unmount(node->un_zhp, 6604 node->un_mountp, flags) != 0) 6605 ret = 1; 6606 break; 6607 } 6608 6609 zfs_close(node->un_zhp); 6610 free(node->un_mountp); 6611 free(node); 6612 } 6613 6614 uu_avl_walk_end(walk); 6615 uu_avl_destroy(tree); 6616 uu_avl_pool_destroy(pool); 6617 6618 } else { 6619 if (argc != 1) { 6620 if (argc == 0) 6621 (void) fprintf(stderr, 6622 gettext("missing filesystem argument\n")); 6623 else 6624 (void) fprintf(stderr, 6625 gettext("too many arguments\n")); 6626 usage(B_FALSE); 6627 } 6628 6629 /* 6630 * We have an argument, but it may be a full path or a ZFS 6631 * filesystem. Pass full paths off to unmount_path() (shared by 6632 * manual_unmount), otherwise open the filesystem and pass to 6633 * zfs_unmount(). 6634 */ 6635 if (argv[0][0] == '/') 6636 return (unshare_unmount_path(op, argv[0], 6637 flags, B_FALSE)); 6638 6639 if ((zhp = zfs_open(g_zfs, argv[0], 6640 ZFS_TYPE_FILESYSTEM)) == NULL) 6641 return (1); 6642 6643 verify(zfs_prop_get(zhp, op == OP_SHARE ? 6644 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 6645 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL, 6646 NULL, 0, B_FALSE) == 0); 6647 6648 switch (op) { 6649 case OP_SHARE: 6650 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 6651 nfs_mnt_prop, 6652 sizeof (nfs_mnt_prop), 6653 NULL, NULL, 0, B_FALSE) == 0); 6654 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 6655 sharesmb, sizeof (sharesmb), NULL, NULL, 6656 0, B_FALSE) == 0); 6657 6658 if (strcmp(nfs_mnt_prop, "off") == 0 && 6659 strcmp(sharesmb, "off") == 0) { 6660 (void) fprintf(stderr, gettext("cannot " 6661 "unshare '%s': legacy share\n"), 6662 zfs_get_name(zhp)); 6663#ifdef illumos 6664 (void) fprintf(stderr, gettext("use " 6665 "unshare(1M) to unshare this " 6666 "filesystem\n")); 6667#endif 6668 ret = 1; 6669 } else if (!zfs_is_shared(zhp)) { 6670 (void) fprintf(stderr, gettext("cannot " 6671 "unshare '%s': not currently " 6672 "shared\n"), zfs_get_name(zhp)); 6673 ret = 1; 6674 } else if (zfs_unshareall(zhp) != 0) { 6675 ret = 1; 6676 } 6677 break; 6678 6679 case OP_MOUNT: 6680 if (strcmp(nfs_mnt_prop, "legacy") == 0) { 6681 (void) fprintf(stderr, gettext("cannot " 6682 "unmount '%s': legacy " 6683 "mountpoint\n"), zfs_get_name(zhp)); 6684 (void) fprintf(stderr, gettext("use " 6685 "umount(8) to unmount this " 6686 "filesystem\n")); 6687 ret = 1; 6688 } else if (!zfs_is_mounted(zhp, NULL)) { 6689 (void) fprintf(stderr, gettext("cannot " 6690 "unmount '%s': not currently " 6691 "mounted\n"), 6692 zfs_get_name(zhp)); 6693 ret = 1; 6694 } else if (zfs_unmountall(zhp, flags) != 0) { 6695 ret = 1; 6696 } 6697 break; 6698 } 6699 6700 zfs_close(zhp); 6701 } 6702 6703 return (ret); 6704} 6705 6706/* 6707 * zfs unmount -a 6708 * zfs unmount filesystem 6709 * 6710 * Unmount all filesystems, or a specific ZFS filesystem. 6711 */ 6712static int 6713zfs_do_unmount(int argc, char **argv) 6714{ 6715 return (unshare_unmount(OP_MOUNT, argc, argv)); 6716} 6717 6718/* 6719 * zfs unshare -a 6720 * zfs unshare filesystem 6721 * 6722 * Unshare all filesystems, or a specific ZFS filesystem. 6723 */ 6724static int 6725zfs_do_unshare(int argc, char **argv) 6726{ 6727 return (unshare_unmount(OP_SHARE, argc, argv)); 6728} 6729 6730/* 6731 * Attach/detach the given dataset to/from the given jail 6732 */ 6733/* ARGSUSED */ 6734static int 6735do_jail(int argc, char **argv, int attach) 6736{ 6737 zfs_handle_t *zhp; 6738 int jailid, ret; 6739 6740 /* check number of arguments */ 6741 if (argc < 3) { 6742 (void) fprintf(stderr, gettext("missing argument(s)\n")); 6743 usage(B_FALSE); 6744 } 6745 if (argc > 3) { 6746 (void) fprintf(stderr, gettext("too many arguments\n")); 6747 usage(B_FALSE); 6748 } 6749 6750 jailid = jail_getid(argv[1]); 6751 if (jailid < 0) { 6752 (void) fprintf(stderr, gettext("invalid jail id or name\n")); 6753 usage(B_FALSE); 6754 } 6755 6756 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM); 6757 if (zhp == NULL) 6758 return (1); 6759 6760 ret = (zfs_jail(zhp, jailid, attach) != 0); 6761 6762 zfs_close(zhp); 6763 return (ret); 6764} 6765 6766/* 6767 * zfs jail jailid filesystem 6768 * 6769 * Attach the given dataset to the given jail 6770 */ 6771/* ARGSUSED */ 6772static int 6773zfs_do_jail(int argc, char **argv) 6774{ 6775 6776 return (do_jail(argc, argv, 1)); 6777} 6778 6779/* 6780 * zfs unjail jailid filesystem 6781 * 6782 * Detach the given dataset from the given jail 6783 */ 6784/* ARGSUSED */ 6785static int 6786zfs_do_unjail(int argc, char **argv) 6787{ 6788 6789 return (do_jail(argc, argv, 0)); 6790} 6791 6792/* 6793 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is 6794 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'. 6795 */ 6796static int 6797manual_mount(int argc, char **argv) 6798{ 6799 zfs_handle_t *zhp; 6800 char mountpoint[ZFS_MAXPROPLEN]; 6801 char mntopts[MNT_LINE_MAX] = { '\0' }; 6802 int ret = 0; 6803 int c; 6804 int flags = 0; 6805 char *dataset, *path; 6806 6807 /* check options */ 6808 while ((c = getopt(argc, argv, ":mo:O")) != -1) { 6809 switch (c) { 6810 case 'o': 6811 (void) strlcpy(mntopts, optarg, sizeof (mntopts)); 6812 break; 6813 case 'O': 6814 flags |= MS_OVERLAY; 6815 break; 6816 case 'm': 6817 flags |= MS_NOMNTTAB; 6818 break; 6819 case ':': 6820 (void) fprintf(stderr, gettext("missing argument for " 6821 "'%c' option\n"), optopt); 6822 usage(B_FALSE); 6823 break; 6824 case '?': 6825 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6826 optopt); 6827 (void) fprintf(stderr, gettext("usage: mount [-o opts] " 6828 "<path>\n")); 6829 return (2); 6830 } 6831 } 6832 6833 argc -= optind; 6834 argv += optind; 6835 6836 /* check that we only have two arguments */ 6837 if (argc != 2) { 6838 if (argc == 0) 6839 (void) fprintf(stderr, gettext("missing dataset " 6840 "argument\n")); 6841 else if (argc == 1) 6842 (void) fprintf(stderr, 6843 gettext("missing mountpoint argument\n")); 6844 else 6845 (void) fprintf(stderr, gettext("too many arguments\n")); 6846 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n"); 6847 return (2); 6848 } 6849 6850 dataset = argv[0]; 6851 path = argv[1]; 6852 6853 /* try to open the dataset */ 6854 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) 6855 return (1); 6856 6857 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 6858 sizeof (mountpoint), NULL, NULL, 0, B_FALSE); 6859 6860 /* check for legacy mountpoint and complain appropriately */ 6861 ret = 0; 6862 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 6863 if (zmount(dataset, path, flags, MNTTYPE_ZFS, 6864 NULL, 0, mntopts, sizeof (mntopts)) != 0) { 6865 (void) fprintf(stderr, gettext("mount failed: %s\n"), 6866 strerror(errno)); 6867 ret = 1; 6868 } 6869 } else { 6870 (void) fprintf(stderr, gettext("filesystem '%s' cannot be " 6871 "mounted using 'mount -t zfs'\n"), dataset); 6872 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' " 6873 "instead.\n"), path); 6874 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' " 6875 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n")); 6876 (void) fprintf(stderr, gettext("See zfs(8) for more " 6877 "information.\n")); 6878 ret = 1; 6879 } 6880 6881 return (ret); 6882} 6883 6884/* 6885 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow 6886 * unmounts of non-legacy filesystems, as this is the dominant administrative 6887 * interface. 6888 */ 6889static int 6890manual_unmount(int argc, char **argv) 6891{ 6892 int flags = 0; 6893 int c; 6894 6895 /* check options */ 6896 while ((c = getopt(argc, argv, "f")) != -1) { 6897 switch (c) { 6898 case 'f': 6899 flags = MS_FORCE; 6900 break; 6901 case '?': 6902 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6903 optopt); 6904 (void) fprintf(stderr, gettext("usage: unmount [-f] " 6905 "<path>\n")); 6906 return (2); 6907 } 6908 } 6909 6910 argc -= optind; 6911 argv += optind; 6912 6913 /* check arguments */ 6914 if (argc != 1) { 6915 if (argc == 0) 6916 (void) fprintf(stderr, gettext("missing path " 6917 "argument\n")); 6918 else 6919 (void) fprintf(stderr, gettext("too many arguments\n")); 6920 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n")); 6921 return (2); 6922 } 6923 6924 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE)); 6925} 6926 6927static int 6928find_command_idx(char *command, int *idx) 6929{ 6930 int i; 6931 6932 for (i = 0; i < NCOMMAND; i++) { 6933 if (command_table[i].name == NULL) 6934 continue; 6935 6936 if (strcmp(command, command_table[i].name) == 0) { 6937 *idx = i; 6938 return (0); 6939 } 6940 } 6941 return (1); 6942} 6943 6944static int 6945zfs_do_diff(int argc, char **argv) 6946{ 6947 zfs_handle_t *zhp; 6948 int flags = 0; 6949 char *tosnap = NULL; 6950 char *fromsnap = NULL; 6951 char *atp, *copy; 6952 int err = 0; 6953 int c; 6954 6955 while ((c = getopt(argc, argv, "FHt")) != -1) { 6956 switch (c) { 6957 case 'F': 6958 flags |= ZFS_DIFF_CLASSIFY; 6959 break; 6960 case 'H': 6961 flags |= ZFS_DIFF_PARSEABLE; 6962 break; 6963 case 't': 6964 flags |= ZFS_DIFF_TIMESTAMP; 6965 break; 6966 default: 6967 (void) fprintf(stderr, 6968 gettext("invalid option '%c'\n"), optopt); 6969 usage(B_FALSE); 6970 } 6971 } 6972 6973 argc -= optind; 6974 argv += optind; 6975 6976 if (argc < 1) { 6977 (void) fprintf(stderr, 6978 gettext("must provide at least one snapshot name\n")); 6979 usage(B_FALSE); 6980 } 6981 6982 if (argc > 2) { 6983 (void) fprintf(stderr, gettext("too many arguments\n")); 6984 usage(B_FALSE); 6985 } 6986 6987 fromsnap = argv[0]; 6988 tosnap = (argc == 2) ? argv[1] : NULL; 6989 6990 copy = NULL; 6991 if (*fromsnap != '@') 6992 copy = strdup(fromsnap); 6993 else if (tosnap) 6994 copy = strdup(tosnap); 6995 if (copy == NULL) 6996 usage(B_FALSE); 6997 6998 if ((atp = strchr(copy, '@')) != NULL) 6999 *atp = '\0'; 7000 7001 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) 7002 return (1); 7003 7004 free(copy); 7005 7006 /* 7007 * Ignore SIGPIPE so that the library can give us 7008 * information on any failure 7009 */ 7010 (void) sigignore(SIGPIPE); 7011 7012 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags); 7013 7014 zfs_close(zhp); 7015 7016 return (err != 0); 7017} 7018 7019/* 7020 * zfs bookmark <fs@snap> <fs#bmark> 7021 * 7022 * Creates a bookmark with the given name from the given snapshot. 7023 */ 7024static int 7025zfs_do_bookmark(int argc, char **argv) 7026{ 7027 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 7028 zfs_handle_t *zhp; 7029 nvlist_t *nvl; 7030 int ret = 0; 7031 int c; 7032 7033 /* check options */ 7034 while ((c = getopt(argc, argv, "")) != -1) { 7035 switch (c) { 7036 case '?': 7037 (void) fprintf(stderr, 7038 gettext("invalid option '%c'\n"), optopt); 7039 goto usage; 7040 } 7041 } 7042 7043 argc -= optind; 7044 argv += optind; 7045 7046 /* check number of arguments */ 7047 if (argc < 1) { 7048 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 7049 goto usage; 7050 } 7051 if (argc < 2) { 7052 (void) fprintf(stderr, gettext("missing bookmark argument\n")); 7053 goto usage; 7054 } 7055 7056 if (strchr(argv[1], '#') == NULL) { 7057 (void) fprintf(stderr, 7058 gettext("invalid bookmark name '%s' -- " 7059 "must contain a '#'\n"), argv[1]); 7060 goto usage; 7061 } 7062 7063 if (argv[0][0] == '@') { 7064 /* 7065 * Snapshot name begins with @. 7066 * Default to same fs as bookmark. 7067 */ 7068 (void) strncpy(snapname, argv[1], sizeof (snapname)); 7069 *strchr(snapname, '#') = '\0'; 7070 (void) strlcat(snapname, argv[0], sizeof (snapname)); 7071 } else { 7072 (void) strncpy(snapname, argv[0], sizeof (snapname)); 7073 } 7074 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT); 7075 if (zhp == NULL) 7076 goto usage; 7077 zfs_close(zhp); 7078 7079 7080 nvl = fnvlist_alloc(); 7081 fnvlist_add_string(nvl, argv[1], snapname); 7082 ret = lzc_bookmark(nvl, NULL); 7083 fnvlist_free(nvl); 7084 7085 if (ret != 0) { 7086 const char *err_msg; 7087 char errbuf[1024]; 7088 7089 (void) snprintf(errbuf, sizeof (errbuf), 7090 dgettext(TEXT_DOMAIN, 7091 "cannot create bookmark '%s'"), argv[1]); 7092 7093 switch (ret) { 7094 case EXDEV: 7095 err_msg = "bookmark is in a different pool"; 7096 break; 7097 case EEXIST: 7098 err_msg = "bookmark exists"; 7099 break; 7100 case EINVAL: 7101 err_msg = "invalid argument"; 7102 break; 7103 case ENOTSUP: 7104 err_msg = "bookmark feature not enabled"; 7105 break; 7106 case ENOSPC: 7107 err_msg = "out of space"; 7108 break; 7109 default: 7110 err_msg = "unknown error"; 7111 break; 7112 } 7113 (void) fprintf(stderr, "%s: %s\n", errbuf, 7114 dgettext(TEXT_DOMAIN, err_msg)); 7115 } 7116 7117 return (ret != 0); 7118 7119usage: 7120 usage(B_FALSE); 7121 return (-1); 7122} 7123 7124static int 7125zfs_do_channel_program(int argc, char **argv) 7126{ 7127 int ret, fd; 7128 char c; 7129 char *progbuf, *filename, *poolname; 7130 size_t progsize, progread; 7131 nvlist_t *outnvl; 7132 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT; 7133 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT; 7134 boolean_t sync_flag = B_TRUE; 7135 zpool_handle_t *zhp; 7136 7137 /* check options */ 7138 while (-1 != 7139 (c = getopt(argc, argv, "nt:(instr-limit)m:(memory-limit)"))) { 7140 switch (c) { 7141 case 't': 7142 case 'm': { 7143 uint64_t arg; 7144 char *endp; 7145 7146 errno = 0; 7147 arg = strtoull(optarg, &endp, 0); 7148 if (errno != 0 || *endp != '\0') { 7149 (void) fprintf(stderr, gettext( 7150 "invalid argument " 7151 "'%s': expected integer\n"), optarg); 7152 goto usage; 7153 } 7154 7155 if (c == 't') { 7156 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) { 7157 (void) fprintf(stderr, gettext( 7158 "Invalid instruction limit: " 7159 "%s\n"), optarg); 7160 return (1); 7161 } else { 7162 instrlimit = arg; 7163 } 7164 } else { 7165 ASSERT3U(c, ==, 'm'); 7166 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) { 7167 (void) fprintf(stderr, gettext( 7168 "Invalid memory limit: " 7169 "%s\n"), optarg); 7170 return (1); 7171 } else { 7172 memlimit = arg; 7173 } 7174 } 7175 break; 7176 } 7177 case 'n': { 7178 sync_flag = B_FALSE; 7179 break; 7180 } 7181 case '?': 7182 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7183 optopt); 7184 goto usage; 7185 } 7186 } 7187 7188 argc -= optind; 7189 argv += optind; 7190 7191 if (argc < 2) { 7192 (void) fprintf(stderr, 7193 gettext("invalid number of arguments\n")); 7194 goto usage; 7195 } 7196 7197 poolname = argv[0]; 7198 filename = argv[1]; 7199 if (strcmp(filename, "-") == 0) { 7200 fd = 0; 7201 filename = "standard input"; 7202 } else if ((fd = open(filename, O_RDONLY)) < 0) { 7203 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"), 7204 filename, strerror(errno)); 7205 return (1); 7206 } 7207 7208 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 7209 (void) fprintf(stderr, gettext("cannot open pool '%s'"), 7210 poolname); 7211 return (1); 7212 } 7213 zpool_close(zhp); 7214 7215 /* 7216 * Read in the channel program, expanding the program buffer as 7217 * necessary. 7218 */ 7219 progread = 0; 7220 progsize = 1024; 7221 progbuf = safe_malloc(progsize); 7222 do { 7223 ret = read(fd, progbuf + progread, progsize - progread); 7224 progread += ret; 7225 if (progread == progsize && ret > 0) { 7226 progsize *= 2; 7227 progbuf = safe_realloc(progbuf, progsize); 7228 } 7229 } while (ret > 0); 7230 7231 if (fd != 0) 7232 (void) close(fd); 7233 if (ret < 0) { 7234 free(progbuf); 7235 (void) fprintf(stderr, 7236 gettext("cannot read '%s': %s\n"), 7237 filename, strerror(errno)); 7238 return (1); 7239 } 7240 progbuf[progread] = '\0'; 7241 7242 /* 7243 * Any remaining arguments are passed as arguments to the lua script as 7244 * a string array: 7245 * { 7246 * "argv" -> [ "arg 1", ... "arg n" ], 7247 * } 7248 */ 7249 nvlist_t *argnvl = fnvlist_alloc(); 7250 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2); 7251 7252 if (sync_flag) { 7253 ret = lzc_channel_program(poolname, progbuf, 7254 instrlimit, memlimit, argnvl, &outnvl); 7255 } else { 7256 ret = lzc_channel_program_nosync(poolname, progbuf, 7257 instrlimit, memlimit, argnvl, &outnvl); 7258 } 7259 7260 if (ret != 0) { 7261 /* 7262 * On error, report the error message handed back by lua if one 7263 * exists. Otherwise, generate an appropriate error message, 7264 * falling back on strerror() for an unexpected return code. 7265 */ 7266 char *errstring = NULL; 7267 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) { 7268 (void) nvlist_lookup_string(outnvl, 7269 ZCP_RET_ERROR, &errstring); 7270 if (errstring == NULL) 7271 errstring = strerror(ret); 7272 } else { 7273 switch (ret) { 7274 case EINVAL: 7275 errstring = 7276 "Invalid instruction or memory limit."; 7277 break; 7278 case ENOMEM: 7279 errstring = "Return value too large."; 7280 break; 7281 case ENOSPC: 7282 errstring = "Memory limit exhausted."; 7283 break; 7284#ifdef illumos 7285 case ETIME: 7286#else 7287 case ETIMEDOUT: 7288#endif 7289 errstring = "Timed out."; 7290 break; 7291 case EPERM: 7292 errstring = "Permission denied. Channel " 7293 "programs must be run as root."; 7294 break; 7295 default: 7296 errstring = strerror(ret); 7297 } 7298 } 7299 (void) fprintf(stderr, 7300 gettext("Channel program execution failed:\n%s\n"), 7301 errstring); 7302 } else { 7303 (void) printf("Channel program fully executed "); 7304 if (nvlist_empty(outnvl)) { 7305 (void) printf("with no return value.\n"); 7306 } else { 7307 (void) printf("with return value:\n"); 7308 dump_nvlist(outnvl, 4); 7309 } 7310 } 7311 7312 free(progbuf); 7313 fnvlist_free(outnvl); 7314 fnvlist_free(argnvl); 7315 return (ret != 0); 7316 7317usage: 7318 usage(B_FALSE); 7319 return (-1); 7320} 7321 7322int 7323main(int argc, char **argv) 7324{ 7325 int ret = 0; 7326 int i; 7327 char *progname; 7328 char *cmdname; 7329 7330 (void) setlocale(LC_ALL, ""); 7331 (void) textdomain(TEXT_DOMAIN); 7332 7333 opterr = 0; 7334 7335 if ((g_zfs = libzfs_init()) == NULL) { 7336 (void) fprintf(stderr, gettext("internal error: failed to " 7337 "initialize ZFS library\n")); 7338 return (1); 7339 } 7340 7341 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 7342 7343 libzfs_print_on_error(g_zfs, B_TRUE); 7344 7345 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) { 7346 (void) fprintf(stderr, gettext("internal error: unable to " 7347 "open %s\n"), MNTTAB); 7348 return (1); 7349 } 7350 7351 /* 7352 * This command also doubles as the /etc/fs mount and unmount program. 7353 * Determine if we should take this behavior based on argv[0]. 7354 */ 7355 progname = basename(argv[0]); 7356 if (strcmp(progname, "mount") == 0) { 7357 ret = manual_mount(argc, argv); 7358 } else if (strcmp(progname, "umount") == 0) { 7359 ret = manual_unmount(argc, argv); 7360 } else { 7361 /* 7362 * Make sure the user has specified some command. 7363 */ 7364 if (argc < 2) { 7365 (void) fprintf(stderr, gettext("missing command\n")); 7366 usage(B_FALSE); 7367 } 7368 7369 cmdname = argv[1]; 7370 7371 /* 7372 * The 'umount' command is an alias for 'unmount' 7373 */ 7374 if (strcmp(cmdname, "umount") == 0) 7375 cmdname = "unmount"; 7376 7377 /* 7378 * The 'recv' command is an alias for 'receive' 7379 */ 7380 if (strcmp(cmdname, "recv") == 0) 7381 cmdname = "receive"; 7382 7383 /* 7384 * The 'snap' command is an alias for 'snapshot' 7385 */ 7386 if (strcmp(cmdname, "snap") == 0) 7387 cmdname = "snapshot"; 7388 7389 /* 7390 * Special case '-?' 7391 */ 7392 if (strcmp(cmdname, "-?") == 0) 7393 usage(B_TRUE); 7394 7395 /* 7396 * Run the appropriate command. 7397 */ 7398 libzfs_mnttab_cache(g_zfs, B_TRUE); 7399 if (find_command_idx(cmdname, &i) == 0) { 7400 current_command = &command_table[i]; 7401 ret = command_table[i].func(argc - 1, argv + 1); 7402 } else if (strchr(cmdname, '=') != NULL) { 7403 verify(find_command_idx("set", &i) == 0); 7404 current_command = &command_table[i]; 7405 ret = command_table[i].func(argc, argv); 7406 } else { 7407 (void) fprintf(stderr, gettext("unrecognized " 7408 "command '%s'\n"), cmdname); 7409 usage(B_FALSE); 7410 } 7411 libzfs_mnttab_cache(g_zfs, B_FALSE); 7412 } 7413 7414 (void) fclose(mnttab_file); 7415 7416 if (ret == 0 && log_history) 7417 (void) zpool_log_history(g_zfs, history_str); 7418 7419 libzfs_fini(g_zfs); 7420 7421 /* 7422 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 7423 * for the purposes of running ::findleaks. 7424 */ 7425 if (getenv("ZFS_ABORT") != NULL) { 7426 (void) printf("dumping core by request\n"); 7427 abort(); 7428 } 7429 7430 return (ret); 7431} 7432