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