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