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