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