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