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