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