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