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 /* ARGSUSED */ 3764 static int 3765 zfs_do_rename(int argc, char **argv) 3766 { 3767 zfs_handle_t *zhp; 3768 renameflags_t flags = { 0 }; 3769 int c; 3770 int ret = 0; 3771 int types; 3772 boolean_t parents = B_FALSE; 3773 3774 /* check options */ 3775 while ((c = getopt(argc, argv, "pruf")) != -1) { 3776 switch (c) { 3777 case 'p': 3778 parents = B_TRUE; 3779 break; 3780 case 'r': 3781 flags.recursive = B_TRUE; 3782 break; 3783 case 'u': 3784 flags.nounmount = B_TRUE; 3785 break; 3786 case 'f': 3787 flags.forceunmount = B_TRUE; 3788 break; 3789 case '?': 3790 default: 3791 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3792 optopt); 3793 usage(B_FALSE); 3794 } 3795 } 3796 3797 argc -= optind; 3798 argv += optind; 3799 3800 /* check number of arguments */ 3801 if (argc < 1) { 3802 (void) fprintf(stderr, gettext("missing source dataset " 3803 "argument\n")); 3804 usage(B_FALSE); 3805 } 3806 if (argc < 2) { 3807 (void) fprintf(stderr, gettext("missing target dataset " 3808 "argument\n")); 3809 usage(B_FALSE); 3810 } 3811 if (argc > 2) { 3812 (void) fprintf(stderr, gettext("too many arguments\n")); 3813 usage(B_FALSE); 3814 } 3815 3816 if (flags.recursive && parents) { 3817 (void) fprintf(stderr, gettext("-p and -r options are mutually " 3818 "exclusive\n")); 3819 usage(B_FALSE); 3820 } 3821 3822 if (flags.nounmount && parents) { 3823 (void) fprintf(stderr, gettext("-u and -p options are mutually " 3824 "exclusive\n")); 3825 usage(B_FALSE); 3826 } 3827 3828 if (flags.recursive && strchr(argv[0], '@') == 0) { 3829 (void) fprintf(stderr, gettext("source dataset for recursive " 3830 "rename must be a snapshot\n")); 3831 usage(B_FALSE); 3832 } 3833 3834 if (flags.nounmount) 3835 types = ZFS_TYPE_FILESYSTEM; 3836 else if (parents) 3837 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 3838 else 3839 types = ZFS_TYPE_DATASET; 3840 3841 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) 3842 return (1); 3843 3844 /* If we were asked and the name looks good, try to create ancestors. */ 3845 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) && 3846 zfs_create_ancestors(g_zfs, argv[1]) != 0) { 3847 zfs_close(zhp); 3848 return (1); 3849 } 3850 3851 ret = (zfs_rename(zhp, argv[1], flags) != 0); 3852 3853 zfs_close(zhp); 3854 return (ret); 3855 } 3856 3857 /* 3858 * zfs promote <fs> 3859 * 3860 * Promotes the given clone fs to be the parent 3861 */ 3862 /* ARGSUSED */ 3863 static int 3864 zfs_do_promote(int argc, char **argv) 3865 { 3866 zfs_handle_t *zhp; 3867 int ret = 0; 3868 3869 /* check options */ 3870 if (argc > 1 && argv[1][0] == '-') { 3871 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3872 argv[1][1]); 3873 usage(B_FALSE); 3874 } 3875 3876 /* check number of arguments */ 3877 if (argc < 2) { 3878 (void) fprintf(stderr, gettext("missing clone filesystem" 3879 " argument\n")); 3880 usage(B_FALSE); 3881 } 3882 if (argc > 2) { 3883 (void) fprintf(stderr, gettext("too many arguments\n")); 3884 usage(B_FALSE); 3885 } 3886 3887 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3888 if (zhp == NULL) 3889 return (1); 3890 3891 ret = (zfs_promote(zhp) != 0); 3892 3893 3894 zfs_close(zhp); 3895 return (ret); 3896 } 3897 3898 static int 3899 zfs_do_redact(int argc, char **argv) 3900 { 3901 char *snap = NULL; 3902 char *bookname = NULL; 3903 char **rsnaps = NULL; 3904 int numrsnaps = 0; 3905 argv++; 3906 argc--; 3907 if (argc < 3) { 3908 (void) fprintf(stderr, gettext("too few arguments\n")); 3909 usage(B_FALSE); 3910 } 3911 3912 snap = argv[0]; 3913 bookname = argv[1]; 3914 rsnaps = argv + 2; 3915 numrsnaps = argc - 2; 3916 3917 nvlist_t *rsnapnv = fnvlist_alloc(); 3918 3919 for (int i = 0; i < numrsnaps; i++) { 3920 fnvlist_add_boolean(rsnapnv, rsnaps[i]); 3921 } 3922 3923 int err = lzc_redact(snap, bookname, rsnapnv); 3924 fnvlist_free(rsnapnv); 3925 3926 switch (err) { 3927 case 0: 3928 break; 3929 case ENOENT: 3930 (void) fprintf(stderr, 3931 gettext("provided snapshot %s does not exist\n"), snap); 3932 break; 3933 case EEXIST: 3934 (void) fprintf(stderr, gettext("specified redaction bookmark " 3935 "(%s) provided already exists\n"), bookname); 3936 break; 3937 case ENAMETOOLONG: 3938 (void) fprintf(stderr, gettext("provided bookmark name cannot " 3939 "be used, final name would be too long\n")); 3940 break; 3941 case E2BIG: 3942 (void) fprintf(stderr, gettext("too many redaction snapshots " 3943 "specified\n")); 3944 break; 3945 case EINVAL: 3946 if (strchr(bookname, '#') != NULL) 3947 (void) fprintf(stderr, gettext( 3948 "redaction bookmark name must not contain '#'\n")); 3949 else 3950 (void) fprintf(stderr, gettext( 3951 "redaction snapshot must be descendent of " 3952 "snapshot being redacted\n")); 3953 break; 3954 case EALREADY: 3955 (void) fprintf(stderr, gettext("attempted to redact redacted " 3956 "dataset or with respect to redacted dataset\n")); 3957 break; 3958 case ENOTSUP: 3959 (void) fprintf(stderr, gettext("redaction bookmarks feature " 3960 "not enabled\n")); 3961 break; 3962 case EXDEV: 3963 (void) fprintf(stderr, gettext("potentially invalid redaction " 3964 "snapshot; full dataset names required\n")); 3965 break; 3966 default: 3967 (void) fprintf(stderr, gettext("internal error: %s\n"), 3968 strerror(errno)); 3969 } 3970 3971 return (err); 3972 } 3973 3974 /* 3975 * zfs rollback [-rRf] <snapshot> 3976 * 3977 * -r Delete any intervening snapshots before doing rollback 3978 * -R Delete any snapshots and their clones 3979 * -f ignored for backwards compatibility 3980 * 3981 * Given a filesystem, rollback to a specific snapshot, discarding any changes 3982 * since then and making it the active dataset. If more recent snapshots exist, 3983 * the command will complain unless the '-r' flag is given. 3984 */ 3985 typedef struct rollback_cbdata { 3986 uint64_t cb_create; 3987 uint8_t cb_younger_ds_printed; 3988 boolean_t cb_first; 3989 int cb_doclones; 3990 char *cb_target; 3991 int cb_error; 3992 boolean_t cb_recurse; 3993 } rollback_cbdata_t; 3994 3995 static int 3996 rollback_check_dependent(zfs_handle_t *zhp, void *data) 3997 { 3998 rollback_cbdata_t *cbp = data; 3999 4000 if (cbp->cb_first && cbp->cb_recurse) { 4001 (void) fprintf(stderr, gettext("cannot rollback to " 4002 "'%s': clones of previous snapshots exist\n"), 4003 cbp->cb_target); 4004 (void) fprintf(stderr, gettext("use '-R' to " 4005 "force deletion of the following clones and " 4006 "dependents:\n")); 4007 cbp->cb_first = 0; 4008 cbp->cb_error = 1; 4009 } 4010 4011 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 4012 4013 zfs_close(zhp); 4014 return (0); 4015 } 4016 4017 4018 /* 4019 * Report some snapshots/bookmarks more recent than the one specified. 4020 * Used when '-r' is not specified. We reuse this same callback for the 4021 * snapshot dependents - if 'cb_dependent' is set, then this is a 4022 * dependent and we should report it without checking the transaction group. 4023 */ 4024 static int 4025 rollback_check(zfs_handle_t *zhp, void *data) 4026 { 4027 rollback_cbdata_t *cbp = data; 4028 /* 4029 * Max number of younger snapshots and/or bookmarks to display before 4030 * we stop the iteration. 4031 */ 4032 const uint8_t max_younger = 32; 4033 4034 if (cbp->cb_doclones) { 4035 zfs_close(zhp); 4036 return (0); 4037 } 4038 4039 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 4040 if (cbp->cb_first && !cbp->cb_recurse) { 4041 (void) fprintf(stderr, gettext("cannot " 4042 "rollback to '%s': more recent snapshots " 4043 "or bookmarks exist\n"), 4044 cbp->cb_target); 4045 (void) fprintf(stderr, gettext("use '-r' to " 4046 "force deletion of the following " 4047 "snapshots and bookmarks:\n")); 4048 cbp->cb_first = 0; 4049 cbp->cb_error = 1; 4050 } 4051 4052 if (cbp->cb_recurse) { 4053 if (zfs_iter_dependents(zhp, B_TRUE, 4054 rollback_check_dependent, cbp) != 0) { 4055 zfs_close(zhp); 4056 return (-1); 4057 } 4058 } else { 4059 (void) fprintf(stderr, "%s\n", 4060 zfs_get_name(zhp)); 4061 cbp->cb_younger_ds_printed++; 4062 } 4063 } 4064 zfs_close(zhp); 4065 4066 if (cbp->cb_younger_ds_printed == max_younger) { 4067 /* 4068 * This non-recursive rollback is going to fail due to the 4069 * presence of snapshots and/or bookmarks that are younger than 4070 * the rollback target. 4071 * We printed some of the offending objects, now we stop 4072 * zfs_iter_snapshot/bookmark iteration so we can fail fast and 4073 * avoid iterating over the rest of the younger objects 4074 */ 4075 (void) fprintf(stderr, gettext("Output limited to %d " 4076 "snapshots/bookmarks\n"), max_younger); 4077 return (-1); 4078 } 4079 return (0); 4080 } 4081 4082 static int 4083 zfs_do_rollback(int argc, char **argv) 4084 { 4085 int ret = 0; 4086 int c; 4087 boolean_t force = B_FALSE; 4088 rollback_cbdata_t cb = { 0 }; 4089 zfs_handle_t *zhp, *snap; 4090 char parentname[ZFS_MAX_DATASET_NAME_LEN]; 4091 char *delim; 4092 uint64_t min_txg = 0; 4093 4094 /* check options */ 4095 while ((c = getopt(argc, argv, "rRf")) != -1) { 4096 switch (c) { 4097 case 'r': 4098 cb.cb_recurse = 1; 4099 break; 4100 case 'R': 4101 cb.cb_recurse = 1; 4102 cb.cb_doclones = 1; 4103 break; 4104 case 'f': 4105 force = B_TRUE; 4106 break; 4107 case '?': 4108 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4109 optopt); 4110 usage(B_FALSE); 4111 } 4112 } 4113 4114 argc -= optind; 4115 argv += optind; 4116 4117 /* check number of arguments */ 4118 if (argc < 1) { 4119 (void) fprintf(stderr, gettext("missing dataset argument\n")); 4120 usage(B_FALSE); 4121 } 4122 if (argc > 1) { 4123 (void) fprintf(stderr, gettext("too many arguments\n")); 4124 usage(B_FALSE); 4125 } 4126 4127 /* open the snapshot */ 4128 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 4129 return (1); 4130 4131 /* open the parent dataset */ 4132 (void) strlcpy(parentname, argv[0], sizeof (parentname)); 4133 verify((delim = strrchr(parentname, '@')) != NULL); 4134 *delim = '\0'; 4135 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) { 4136 zfs_close(snap); 4137 return (1); 4138 } 4139 4140 /* 4141 * Check for more recent snapshots and/or clones based on the presence 4142 * of '-r' and '-R'. 4143 */ 4144 cb.cb_target = argv[0]; 4145 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 4146 cb.cb_first = B_TRUE; 4147 cb.cb_error = 0; 4148 4149 if (cb.cb_create > 0) 4150 min_txg = cb.cb_create; 4151 4152 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb, 4153 min_txg, 0)) != 0) 4154 goto out; 4155 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0) 4156 goto out; 4157 4158 if ((ret = cb.cb_error) != 0) 4159 goto out; 4160 4161 /* 4162 * Rollback parent to the given snapshot. 4163 */ 4164 ret = zfs_rollback(zhp, snap, force); 4165 4166 out: 4167 zfs_close(snap); 4168 zfs_close(zhp); 4169 4170 if (ret == 0) 4171 return (0); 4172 else 4173 return (1); 4174 } 4175 4176 /* 4177 * zfs set property=value ... { fs | snap | vol } ... 4178 * 4179 * Sets the given properties for all datasets specified on the command line. 4180 */ 4181 4182 static int 4183 set_callback(zfs_handle_t *zhp, void *data) 4184 { 4185 nvlist_t *props = data; 4186 4187 if (zfs_prop_set_list(zhp, props) != 0) { 4188 switch (libzfs_errno(g_zfs)) { 4189 case EZFS_MOUNTFAILED: 4190 (void) fprintf(stderr, gettext("property may be set " 4191 "but unable to remount filesystem\n")); 4192 break; 4193 case EZFS_SHARENFSFAILED: 4194 (void) fprintf(stderr, gettext("property may be set " 4195 "but unable to reshare filesystem\n")); 4196 break; 4197 } 4198 return (1); 4199 } 4200 return (0); 4201 } 4202 4203 static int 4204 zfs_do_set(int argc, char **argv) 4205 { 4206 nvlist_t *props = NULL; 4207 int ds_start = -1; /* argv idx of first dataset arg */ 4208 int ret = 0; 4209 int i; 4210 4211 /* check for options */ 4212 if (argc > 1 && argv[1][0] == '-') { 4213 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4214 argv[1][1]); 4215 usage(B_FALSE); 4216 } 4217 4218 /* check number of arguments */ 4219 if (argc < 2) { 4220 (void) fprintf(stderr, gettext("missing arguments\n")); 4221 usage(B_FALSE); 4222 } 4223 if (argc < 3) { 4224 if (strchr(argv[1], '=') == NULL) { 4225 (void) fprintf(stderr, gettext("missing property=value " 4226 "argument(s)\n")); 4227 } else { 4228 (void) fprintf(stderr, gettext("missing dataset " 4229 "name(s)\n")); 4230 } 4231 usage(B_FALSE); 4232 } 4233 4234 /* validate argument order: prop=val args followed by dataset args */ 4235 for (i = 1; i < argc; i++) { 4236 if (strchr(argv[i], '=') != NULL) { 4237 if (ds_start > 0) { 4238 /* out-of-order prop=val argument */ 4239 (void) fprintf(stderr, gettext("invalid " 4240 "argument order\n")); 4241 usage(B_FALSE); 4242 } 4243 } else if (ds_start < 0) { 4244 ds_start = i; 4245 } 4246 } 4247 if (ds_start < 0) { 4248 (void) fprintf(stderr, gettext("missing dataset name(s)\n")); 4249 usage(B_FALSE); 4250 } 4251 4252 /* Populate a list of property settings */ 4253 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 4254 nomem(); 4255 for (i = 1; i < ds_start; i++) { 4256 if (!parseprop(props, argv[i])) { 4257 ret = -1; 4258 goto error; 4259 } 4260 } 4261 4262 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0, 4263 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props); 4264 4265 error: 4266 nvlist_free(props); 4267 return (ret); 4268 } 4269 4270 typedef struct snap_cbdata { 4271 nvlist_t *sd_nvl; 4272 boolean_t sd_recursive; 4273 const char *sd_snapname; 4274 } snap_cbdata_t; 4275 4276 static int 4277 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 4278 { 4279 snap_cbdata_t *sd = arg; 4280 char *name; 4281 int rv = 0; 4282 int error; 4283 4284 if (sd->sd_recursive && 4285 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) { 4286 zfs_close(zhp); 4287 return (0); 4288 } 4289 4290 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 4291 if (error == -1) 4292 nomem(); 4293 fnvlist_add_boolean(sd->sd_nvl, name); 4294 free(name); 4295 4296 if (sd->sd_recursive) 4297 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 4298 zfs_close(zhp); 4299 return (rv); 4300 } 4301 4302 /* 4303 * zfs snapshot [-r] [-o prop=value] ... <fs@snap> 4304 * 4305 * Creates a snapshot with the given name. While functionally equivalent to 4306 * 'zfs create', it is a separate command to differentiate intent. 4307 */ 4308 static int 4309 zfs_do_snapshot(int argc, char **argv) 4310 { 4311 int ret = 0; 4312 int c; 4313 nvlist_t *props; 4314 snap_cbdata_t sd = { 0 }; 4315 boolean_t multiple_snaps = B_FALSE; 4316 4317 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 4318 nomem(); 4319 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0) 4320 nomem(); 4321 4322 /* check options */ 4323 while ((c = getopt(argc, argv, "ro:")) != -1) { 4324 switch (c) { 4325 case 'o': 4326 if (!parseprop(props, optarg)) { 4327 nvlist_free(sd.sd_nvl); 4328 nvlist_free(props); 4329 return (1); 4330 } 4331 break; 4332 case 'r': 4333 sd.sd_recursive = B_TRUE; 4334 multiple_snaps = B_TRUE; 4335 break; 4336 case '?': 4337 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4338 optopt); 4339 goto usage; 4340 } 4341 } 4342 4343 argc -= optind; 4344 argv += optind; 4345 4346 /* check number of arguments */ 4347 if (argc < 1) { 4348 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 4349 goto usage; 4350 } 4351 4352 if (argc > 1) 4353 multiple_snaps = B_TRUE; 4354 for (; argc > 0; argc--, argv++) { 4355 char *atp; 4356 zfs_handle_t *zhp; 4357 4358 atp = strchr(argv[0], '@'); 4359 if (atp == NULL) 4360 goto usage; 4361 *atp = '\0'; 4362 sd.sd_snapname = atp + 1; 4363 zhp = zfs_open(g_zfs, argv[0], 4364 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4365 if (zhp == NULL) 4366 goto usage; 4367 if (zfs_snapshot_cb(zhp, &sd) != 0) 4368 goto usage; 4369 } 4370 4371 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props); 4372 nvlist_free(sd.sd_nvl); 4373 nvlist_free(props); 4374 if (ret != 0 && multiple_snaps) 4375 (void) fprintf(stderr, gettext("no snapshots were created\n")); 4376 return (ret != 0); 4377 4378 usage: 4379 nvlist_free(sd.sd_nvl); 4380 nvlist_free(props); 4381 usage(B_FALSE); 4382 return (-1); 4383 } 4384 4385 4386 /* 4387 * Send a backup stream to stdout. 4388 */ 4389 static int 4390 zfs_do_send(int argc, char **argv) 4391 { 4392 char *fromname = NULL; 4393 char *toname = NULL; 4394 char *resume_token = NULL; 4395 char *cp; 4396 zfs_handle_t *zhp; 4397 sendflags_t flags = { 0 }; 4398 int c, err; 4399 nvlist_t *dbgnv = NULL; 4400 char *redactbook = NULL; 4401 4402 struct option long_options[] = { 4403 {"replicate", no_argument, NULL, 'R'}, 4404 {"skip-missing", no_argument, NULL, 's'}, 4405 {"redact", required_argument, NULL, 'd'}, 4406 {"props", no_argument, NULL, 'p'}, 4407 {"parsable", no_argument, NULL, 'P'}, 4408 {"dedup", no_argument, NULL, 'D'}, 4409 {"verbose", no_argument, NULL, 'v'}, 4410 {"dryrun", no_argument, NULL, 'n'}, 4411 {"large-block", no_argument, NULL, 'L'}, 4412 {"embed", no_argument, NULL, 'e'}, 4413 {"resume", required_argument, NULL, 't'}, 4414 {"compressed", no_argument, NULL, 'c'}, 4415 {"raw", no_argument, NULL, 'w'}, 4416 {"backup", no_argument, NULL, 'b'}, 4417 {"holds", no_argument, NULL, 'h'}, 4418 {"saved", no_argument, NULL, 'S'}, 4419 {0, 0, 0, 0} 4420 }; 4421 4422 /* check options */ 4423 while ((c = getopt_long(argc, argv, ":i:I:RsDpvnPLeht:cwbd:S", 4424 long_options, NULL)) != -1) { 4425 switch (c) { 4426 case 'i': 4427 if (fromname) 4428 usage(B_FALSE); 4429 fromname = optarg; 4430 break; 4431 case 'I': 4432 if (fromname) 4433 usage(B_FALSE); 4434 fromname = optarg; 4435 flags.doall = B_TRUE; 4436 break; 4437 case 'R': 4438 flags.replicate = B_TRUE; 4439 break; 4440 case 's': 4441 flags.skipmissing = B_TRUE; 4442 break; 4443 case 'd': 4444 redactbook = optarg; 4445 break; 4446 case 'p': 4447 flags.props = B_TRUE; 4448 break; 4449 case 'b': 4450 flags.backup = B_TRUE; 4451 break; 4452 case 'h': 4453 flags.holds = B_TRUE; 4454 break; 4455 case 'P': 4456 flags.parsable = B_TRUE; 4457 break; 4458 case 'v': 4459 flags.verbosity++; 4460 flags.progress = B_TRUE; 4461 break; 4462 case 'D': 4463 (void) fprintf(stderr, 4464 gettext("WARNING: deduplicated send is no " 4465 "longer supported. A regular,\n" 4466 "non-deduplicated stream will be generated.\n\n")); 4467 break; 4468 case 'n': 4469 flags.dryrun = B_TRUE; 4470 break; 4471 case 'L': 4472 flags.largeblock = B_TRUE; 4473 break; 4474 case 'e': 4475 flags.embed_data = B_TRUE; 4476 break; 4477 case 't': 4478 resume_token = optarg; 4479 break; 4480 case 'c': 4481 flags.compress = B_TRUE; 4482 break; 4483 case 'w': 4484 flags.raw = B_TRUE; 4485 flags.compress = B_TRUE; 4486 flags.embed_data = B_TRUE; 4487 flags.largeblock = B_TRUE; 4488 break; 4489 case 'S': 4490 flags.saved = B_TRUE; 4491 break; 4492 case ':': 4493 /* 4494 * If a parameter was not passed, optopt contains the 4495 * value that would normally lead us into the 4496 * appropriate case statement. If it's > 256, then this 4497 * must be a longopt and we should look at argv to get 4498 * the string. Otherwise it's just the character, so we 4499 * should use it directly. 4500 */ 4501 if (optopt <= UINT8_MAX) { 4502 (void) fprintf(stderr, 4503 gettext("missing argument for '%c' " 4504 "option\n"), optopt); 4505 } else { 4506 (void) fprintf(stderr, 4507 gettext("missing argument for '%s' " 4508 "option\n"), argv[optind - 1]); 4509 } 4510 usage(B_FALSE); 4511 break; 4512 case '?': 4513 default: 4514 /* 4515 * If an invalid flag was passed, optopt contains the 4516 * character if it was a short flag, or 0 if it was a 4517 * longopt. 4518 */ 4519 if (optopt != 0) { 4520 (void) fprintf(stderr, 4521 gettext("invalid option '%c'\n"), optopt); 4522 } else { 4523 (void) fprintf(stderr, 4524 gettext("invalid option '%s'\n"), 4525 argv[optind - 1]); 4526 4527 } 4528 usage(B_FALSE); 4529 } 4530 } 4531 4532 if (flags.parsable && flags.verbosity == 0) 4533 flags.verbosity = 1; 4534 4535 argc -= optind; 4536 argv += optind; 4537 4538 if (resume_token != NULL) { 4539 if (fromname != NULL || flags.replicate || flags.props || 4540 flags.backup || flags.holds || 4541 flags.saved || redactbook != NULL) { 4542 (void) fprintf(stderr, 4543 gettext("invalid flags combined with -t\n")); 4544 usage(B_FALSE); 4545 } 4546 if (argc > 0) { 4547 (void) fprintf(stderr, gettext("too many arguments\n")); 4548 usage(B_FALSE); 4549 } 4550 } else { 4551 if (argc < 1) { 4552 (void) fprintf(stderr, 4553 gettext("missing snapshot argument\n")); 4554 usage(B_FALSE); 4555 } 4556 if (argc > 1) { 4557 (void) fprintf(stderr, gettext("too many arguments\n")); 4558 usage(B_FALSE); 4559 } 4560 } 4561 4562 if (flags.saved) { 4563 if (fromname != NULL || flags.replicate || flags.props || 4564 flags.doall || flags.backup || 4565 flags.holds || flags.largeblock || flags.embed_data || 4566 flags.compress || flags.raw || redactbook != NULL) { 4567 (void) fprintf(stderr, gettext("incompatible flags " 4568 "combined with saved send flag\n")); 4569 usage(B_FALSE); 4570 } 4571 if (strchr(argv[0], '@') != NULL) { 4572 (void) fprintf(stderr, gettext("saved send must " 4573 "specify the dataset with partially-received " 4574 "state\n")); 4575 usage(B_FALSE); 4576 } 4577 } 4578 4579 if (flags.raw && redactbook != NULL) { 4580 (void) fprintf(stderr, 4581 gettext("Error: raw sends may not be redacted.\n")); 4582 return (1); 4583 } 4584 4585 if (!flags.dryrun && isatty(STDOUT_FILENO)) { 4586 (void) fprintf(stderr, 4587 gettext("Error: Stream can not be written to a terminal.\n" 4588 "You must redirect standard output.\n")); 4589 return (1); 4590 } 4591 4592 if (flags.saved) { 4593 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET); 4594 if (zhp == NULL) 4595 return (1); 4596 4597 err = zfs_send_saved(zhp, &flags, STDOUT_FILENO, 4598 resume_token); 4599 if (err != 0) 4600 note_dev_error(errno, STDOUT_FILENO); 4601 zfs_close(zhp); 4602 return (err != 0); 4603 } else if (resume_token != NULL) { 4604 err = zfs_send_resume(g_zfs, &flags, STDOUT_FILENO, 4605 resume_token); 4606 if (err != 0) 4607 note_dev_error(errno, STDOUT_FILENO); 4608 return (err); 4609 } 4610 4611 if (flags.skipmissing && !flags.replicate) { 4612 (void) fprintf(stderr, 4613 gettext("skip-missing flag can only be used in " 4614 "conjunction with replicate\n")); 4615 usage(B_FALSE); 4616 } 4617 4618 /* 4619 * For everything except -R and -I, use the new, cleaner code path. 4620 */ 4621 if (!(flags.replicate || flags.doall)) { 4622 char frombuf[ZFS_MAX_DATASET_NAME_LEN]; 4623 4624 if (fromname != NULL && (strchr(fromname, '#') == NULL && 4625 strchr(fromname, '@') == NULL)) { 4626 /* 4627 * Neither bookmark or snapshot was specified. Print a 4628 * warning, and assume snapshot. 4629 */ 4630 (void) fprintf(stderr, "Warning: incremental source " 4631 "didn't specify type, assuming snapshot. Use '@' " 4632 "or '#' prefix to avoid ambiguity.\n"); 4633 (void) snprintf(frombuf, sizeof (frombuf), "@%s", 4634 fromname); 4635 fromname = frombuf; 4636 } 4637 if (fromname != NULL && 4638 (fromname[0] == '#' || fromname[0] == '@')) { 4639 /* 4640 * Incremental source name begins with # or @. 4641 * Default to same fs as target. 4642 */ 4643 char tmpbuf[ZFS_MAX_DATASET_NAME_LEN]; 4644 (void) strlcpy(tmpbuf, fromname, sizeof (tmpbuf)); 4645 (void) strlcpy(frombuf, argv[0], sizeof (frombuf)); 4646 cp = strchr(frombuf, '@'); 4647 if (cp != NULL) 4648 *cp = '\0'; 4649 (void) strlcat(frombuf, tmpbuf, sizeof (frombuf)); 4650 fromname = frombuf; 4651 } 4652 4653 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET); 4654 if (zhp == NULL) 4655 return (1); 4656 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, &flags, 4657 redactbook); 4658 zfs_close(zhp); 4659 if (err != 0) 4660 note_dev_error(errno, STDOUT_FILENO); 4661 return (err != 0); 4662 } 4663 4664 if (fromname != NULL && strchr(fromname, '#')) { 4665 (void) fprintf(stderr, 4666 gettext("Error: multiple snapshots cannot be " 4667 "sent from a bookmark.\n")); 4668 return (1); 4669 } 4670 4671 if (redactbook != NULL) { 4672 (void) fprintf(stderr, gettext("Error: multiple snapshots " 4673 "cannot be sent redacted.\n")); 4674 return (1); 4675 } 4676 4677 if ((cp = strchr(argv[0], '@')) == NULL) { 4678 (void) fprintf(stderr, gettext("Error: " 4679 "Unsupported flag with filesystem or bookmark.\n")); 4680 return (1); 4681 } 4682 *cp = '\0'; 4683 toname = cp + 1; 4684 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4685 if (zhp == NULL) 4686 return (1); 4687 4688 /* 4689 * If they specified the full path to the snapshot, chop off 4690 * everything except the short name of the snapshot, but special 4691 * case if they specify the origin. 4692 */ 4693 if (fromname && (cp = strchr(fromname, '@')) != NULL) { 4694 char origin[ZFS_MAX_DATASET_NAME_LEN]; 4695 zprop_source_t src; 4696 4697 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN, 4698 origin, sizeof (origin), &src, NULL, 0, B_FALSE); 4699 4700 if (strcmp(origin, fromname) == 0) { 4701 fromname = NULL; 4702 flags.fromorigin = B_TRUE; 4703 } else { 4704 *cp = '\0'; 4705 if (cp != fromname && strcmp(argv[0], fromname)) { 4706 (void) fprintf(stderr, 4707 gettext("incremental source must be " 4708 "in same filesystem\n")); 4709 usage(B_FALSE); 4710 } 4711 fromname = cp + 1; 4712 if (strchr(fromname, '@') || strchr(fromname, '/')) { 4713 (void) fprintf(stderr, 4714 gettext("invalid incremental source\n")); 4715 usage(B_FALSE); 4716 } 4717 } 4718 } 4719 4720 if (flags.replicate && fromname == NULL) 4721 flags.doall = B_TRUE; 4722 4723 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0, 4724 flags.verbosity >= 3 ? &dbgnv : NULL); 4725 4726 if (flags.verbosity >= 3 && dbgnv != NULL) { 4727 /* 4728 * dump_nvlist prints to stdout, but that's been 4729 * redirected to a file. Make it print to stderr 4730 * instead. 4731 */ 4732 (void) dup2(STDERR_FILENO, STDOUT_FILENO); 4733 dump_nvlist(dbgnv, 0); 4734 nvlist_free(dbgnv); 4735 } 4736 zfs_close(zhp); 4737 note_dev_error(errno, STDOUT_FILENO); 4738 4739 return (err != 0); 4740 } 4741 4742 /* 4743 * Restore a backup stream from stdin. 4744 */ 4745 static int 4746 zfs_do_receive(int argc, char **argv) 4747 { 4748 int c, err = 0; 4749 recvflags_t flags = { 0 }; 4750 boolean_t abort_resumable = B_FALSE; 4751 nvlist_t *props; 4752 4753 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 4754 nomem(); 4755 4756 /* check options */ 4757 while ((c = getopt(argc, argv, ":o:x:dehMnuvFsA")) != -1) { 4758 switch (c) { 4759 case 'o': 4760 if (!parseprop(props, optarg)) { 4761 nvlist_free(props); 4762 usage(B_FALSE); 4763 } 4764 break; 4765 case 'x': 4766 if (!parsepropname(props, optarg)) { 4767 nvlist_free(props); 4768 usage(B_FALSE); 4769 } 4770 break; 4771 case 'd': 4772 if (flags.istail) { 4773 (void) fprintf(stderr, gettext("invalid option " 4774 "combination: -d and -e are mutually " 4775 "exclusive\n")); 4776 usage(B_FALSE); 4777 } 4778 flags.isprefix = B_TRUE; 4779 break; 4780 case 'e': 4781 if (flags.isprefix) { 4782 (void) fprintf(stderr, gettext("invalid option " 4783 "combination: -d and -e are mutually " 4784 "exclusive\n")); 4785 usage(B_FALSE); 4786 } 4787 flags.istail = B_TRUE; 4788 break; 4789 case 'h': 4790 flags.skipholds = B_TRUE; 4791 break; 4792 case 'M': 4793 flags.forceunmount = B_TRUE; 4794 break; 4795 case 'n': 4796 flags.dryrun = B_TRUE; 4797 break; 4798 case 'u': 4799 flags.nomount = B_TRUE; 4800 break; 4801 case 'v': 4802 flags.verbose = B_TRUE; 4803 break; 4804 case 's': 4805 flags.resumable = B_TRUE; 4806 break; 4807 case 'F': 4808 flags.force = B_TRUE; 4809 break; 4810 case 'A': 4811 abort_resumable = B_TRUE; 4812 break; 4813 case ':': 4814 (void) fprintf(stderr, gettext("missing argument for " 4815 "'%c' option\n"), optopt); 4816 usage(B_FALSE); 4817 break; 4818 case '?': 4819 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4820 optopt); 4821 usage(B_FALSE); 4822 } 4823 } 4824 4825 argc -= optind; 4826 argv += optind; 4827 4828 /* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */ 4829 if (flags.istail) 4830 flags.isprefix = B_TRUE; 4831 4832 /* check number of arguments */ 4833 if (argc < 1) { 4834 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 4835 usage(B_FALSE); 4836 } 4837 if (argc > 1) { 4838 (void) fprintf(stderr, gettext("too many arguments\n")); 4839 usage(B_FALSE); 4840 } 4841 4842 if (abort_resumable) { 4843 if (flags.isprefix || flags.istail || flags.dryrun || 4844 flags.resumable || flags.nomount) { 4845 (void) fprintf(stderr, gettext("invalid option\n")); 4846 usage(B_FALSE); 4847 } 4848 4849 char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 4850 (void) snprintf(namebuf, sizeof (namebuf), 4851 "%s/%%recv", argv[0]); 4852 4853 if (zfs_dataset_exists(g_zfs, namebuf, 4854 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) { 4855 zfs_handle_t *zhp = zfs_open(g_zfs, 4856 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4857 if (zhp == NULL) { 4858 nvlist_free(props); 4859 return (1); 4860 } 4861 err = zfs_destroy(zhp, B_FALSE); 4862 zfs_close(zhp); 4863 } else { 4864 zfs_handle_t *zhp = zfs_open(g_zfs, 4865 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4866 if (zhp == NULL) 4867 usage(B_FALSE); 4868 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) || 4869 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 4870 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) { 4871 (void) fprintf(stderr, 4872 gettext("'%s' does not have any " 4873 "resumable receive state to abort\n"), 4874 argv[0]); 4875 nvlist_free(props); 4876 zfs_close(zhp); 4877 return (1); 4878 } 4879 err = zfs_destroy(zhp, B_FALSE); 4880 zfs_close(zhp); 4881 } 4882 nvlist_free(props); 4883 return (err != 0); 4884 } 4885 4886 if (isatty(STDIN_FILENO)) { 4887 (void) fprintf(stderr, 4888 gettext("Error: Backup stream can not be read " 4889 "from a terminal.\n" 4890 "You must redirect standard input.\n")); 4891 nvlist_free(props); 4892 return (1); 4893 } 4894 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL); 4895 nvlist_free(props); 4896 4897 return (err != 0); 4898 } 4899 4900 /* 4901 * allow/unallow stuff 4902 */ 4903 /* copied from zfs/sys/dsl_deleg.h */ 4904 #define ZFS_DELEG_PERM_CREATE "create" 4905 #define ZFS_DELEG_PERM_DESTROY "destroy" 4906 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot" 4907 #define ZFS_DELEG_PERM_ROLLBACK "rollback" 4908 #define ZFS_DELEG_PERM_CLONE "clone" 4909 #define ZFS_DELEG_PERM_PROMOTE "promote" 4910 #define ZFS_DELEG_PERM_RENAME "rename" 4911 #define ZFS_DELEG_PERM_MOUNT "mount" 4912 #define ZFS_DELEG_PERM_SHARE "share" 4913 #define ZFS_DELEG_PERM_SEND "send" 4914 #define ZFS_DELEG_PERM_RECEIVE "receive" 4915 #define ZFS_DELEG_PERM_ALLOW "allow" 4916 #define ZFS_DELEG_PERM_USERPROP "userprop" 4917 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */ 4918 #define ZFS_DELEG_PERM_USERQUOTA "userquota" 4919 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota" 4920 #define ZFS_DELEG_PERM_USERUSED "userused" 4921 #define ZFS_DELEG_PERM_GROUPUSED "groupused" 4922 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota" 4923 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota" 4924 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused" 4925 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused" 4926 4927 #define ZFS_DELEG_PERM_HOLD "hold" 4928 #define ZFS_DELEG_PERM_RELEASE "release" 4929 #define ZFS_DELEG_PERM_DIFF "diff" 4930 #define ZFS_DELEG_PERM_BOOKMARK "bookmark" 4931 #define ZFS_DELEG_PERM_LOAD_KEY "load-key" 4932 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key" 4933 4934 #define ZFS_DELEG_PERM_PROJECTUSED "projectused" 4935 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota" 4936 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused" 4937 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota" 4938 4939 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE 4940 4941 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = { 4942 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW }, 4943 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE }, 4944 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE }, 4945 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY }, 4946 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF}, 4947 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD }, 4948 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT }, 4949 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE }, 4950 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE }, 4951 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE }, 4952 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME }, 4953 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK }, 4954 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND }, 4955 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE }, 4956 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT }, 4957 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK }, 4958 { ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY }, 4959 { ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY }, 4960 4961 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA }, 4962 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED }, 4963 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP }, 4964 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA }, 4965 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED }, 4966 { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA }, 4967 { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED }, 4968 { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA }, 4969 { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED }, 4970 { ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED }, 4971 { ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA }, 4972 { ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED }, 4973 { ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA }, 4974 { NULL, ZFS_DELEG_NOTE_NONE } 4975 }; 4976 4977 /* permission structure */ 4978 typedef struct deleg_perm { 4979 zfs_deleg_who_type_t dp_who_type; 4980 const char *dp_name; 4981 boolean_t dp_local; 4982 boolean_t dp_descend; 4983 } deleg_perm_t; 4984 4985 /* */ 4986 typedef struct deleg_perm_node { 4987 deleg_perm_t dpn_perm; 4988 4989 uu_avl_node_t dpn_avl_node; 4990 } deleg_perm_node_t; 4991 4992 typedef struct fs_perm fs_perm_t; 4993 4994 /* permissions set */ 4995 typedef struct who_perm { 4996 zfs_deleg_who_type_t who_type; 4997 const char *who_name; /* id */ 4998 char who_ug_name[256]; /* user/group name */ 4999 fs_perm_t *who_fsperm; /* uplink */ 5000 5001 uu_avl_t *who_deleg_perm_avl; /* permissions */ 5002 } who_perm_t; 5003 5004 /* */ 5005 typedef struct who_perm_node { 5006 who_perm_t who_perm; 5007 uu_avl_node_t who_avl_node; 5008 } who_perm_node_t; 5009 5010 typedef struct fs_perm_set fs_perm_set_t; 5011 /* fs permissions */ 5012 struct fs_perm { 5013 const char *fsp_name; 5014 5015 uu_avl_t *fsp_sc_avl; /* sets,create */ 5016 uu_avl_t *fsp_uge_avl; /* user,group,everyone */ 5017 5018 fs_perm_set_t *fsp_set; /* uplink */ 5019 }; 5020 5021 /* */ 5022 typedef struct fs_perm_node { 5023 fs_perm_t fspn_fsperm; 5024 uu_avl_t *fspn_avl; 5025 5026 uu_list_node_t fspn_list_node; 5027 } fs_perm_node_t; 5028 5029 /* top level structure */ 5030 struct fs_perm_set { 5031 uu_list_pool_t *fsps_list_pool; 5032 uu_list_t *fsps_list; /* list of fs_perms */ 5033 5034 uu_avl_pool_t *fsps_named_set_avl_pool; 5035 uu_avl_pool_t *fsps_who_perm_avl_pool; 5036 uu_avl_pool_t *fsps_deleg_perm_avl_pool; 5037 }; 5038 5039 static inline const char * 5040 deleg_perm_type(zfs_deleg_note_t note) 5041 { 5042 /* subcommands */ 5043 switch (note) { 5044 /* SUBCOMMANDS */ 5045 /* OTHER */ 5046 case ZFS_DELEG_NOTE_GROUPQUOTA: 5047 case ZFS_DELEG_NOTE_GROUPUSED: 5048 case ZFS_DELEG_NOTE_USERPROP: 5049 case ZFS_DELEG_NOTE_USERQUOTA: 5050 case ZFS_DELEG_NOTE_USERUSED: 5051 case ZFS_DELEG_NOTE_USEROBJQUOTA: 5052 case ZFS_DELEG_NOTE_USEROBJUSED: 5053 case ZFS_DELEG_NOTE_GROUPOBJQUOTA: 5054 case ZFS_DELEG_NOTE_GROUPOBJUSED: 5055 case ZFS_DELEG_NOTE_PROJECTUSED: 5056 case ZFS_DELEG_NOTE_PROJECTQUOTA: 5057 case ZFS_DELEG_NOTE_PROJECTOBJUSED: 5058 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA: 5059 /* other */ 5060 return (gettext("other")); 5061 default: 5062 return (gettext("subcommand")); 5063 } 5064 } 5065 5066 static int 5067 who_type2weight(zfs_deleg_who_type_t who_type) 5068 { 5069 int res; 5070 switch (who_type) { 5071 case ZFS_DELEG_NAMED_SET_SETS: 5072 case ZFS_DELEG_NAMED_SET: 5073 res = 0; 5074 break; 5075 case ZFS_DELEG_CREATE_SETS: 5076 case ZFS_DELEG_CREATE: 5077 res = 1; 5078 break; 5079 case ZFS_DELEG_USER_SETS: 5080 case ZFS_DELEG_USER: 5081 res = 2; 5082 break; 5083 case ZFS_DELEG_GROUP_SETS: 5084 case ZFS_DELEG_GROUP: 5085 res = 3; 5086 break; 5087 case ZFS_DELEG_EVERYONE_SETS: 5088 case ZFS_DELEG_EVERYONE: 5089 res = 4; 5090 break; 5091 default: 5092 res = -1; 5093 } 5094 5095 return (res); 5096 } 5097 5098 /* ARGSUSED */ 5099 static int 5100 who_perm_compare(const void *larg, const void *rarg, void *unused) 5101 { 5102 const who_perm_node_t *l = larg; 5103 const who_perm_node_t *r = rarg; 5104 zfs_deleg_who_type_t ltype = l->who_perm.who_type; 5105 zfs_deleg_who_type_t rtype = r->who_perm.who_type; 5106 int lweight = who_type2weight(ltype); 5107 int rweight = who_type2weight(rtype); 5108 int res = lweight - rweight; 5109 if (res == 0) 5110 res = strncmp(l->who_perm.who_name, r->who_perm.who_name, 5111 ZFS_MAX_DELEG_NAME-1); 5112 5113 if (res == 0) 5114 return (0); 5115 if (res > 0) 5116 return (1); 5117 else 5118 return (-1); 5119 } 5120 5121 /* ARGSUSED */ 5122 static int 5123 deleg_perm_compare(const void *larg, const void *rarg, void *unused) 5124 { 5125 const deleg_perm_node_t *l = larg; 5126 const deleg_perm_node_t *r = rarg; 5127 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name, 5128 ZFS_MAX_DELEG_NAME-1); 5129 5130 if (res == 0) 5131 return (0); 5132 5133 if (res > 0) 5134 return (1); 5135 else 5136 return (-1); 5137 } 5138 5139 static inline void 5140 fs_perm_set_init(fs_perm_set_t *fspset) 5141 { 5142 bzero(fspset, sizeof (fs_perm_set_t)); 5143 5144 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool", 5145 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node), 5146 NULL, UU_DEFAULT)) == NULL) 5147 nomem(); 5148 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL, 5149 UU_DEFAULT)) == NULL) 5150 nomem(); 5151 5152 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create( 5153 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof( 5154 who_perm_node_t, who_avl_node), who_perm_compare, 5155 UU_DEFAULT)) == NULL) 5156 nomem(); 5157 5158 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create( 5159 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof( 5160 who_perm_node_t, who_avl_node), who_perm_compare, 5161 UU_DEFAULT)) == NULL) 5162 nomem(); 5163 5164 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create( 5165 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof( 5166 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT)) 5167 == NULL) 5168 nomem(); 5169 } 5170 5171 static inline void fs_perm_fini(fs_perm_t *); 5172 static inline void who_perm_fini(who_perm_t *); 5173 5174 static inline void 5175 fs_perm_set_fini(fs_perm_set_t *fspset) 5176 { 5177 fs_perm_node_t *node = uu_list_first(fspset->fsps_list); 5178 5179 while (node != NULL) { 5180 fs_perm_node_t *next_node = 5181 uu_list_next(fspset->fsps_list, node); 5182 fs_perm_t *fsperm = &node->fspn_fsperm; 5183 fs_perm_fini(fsperm); 5184 uu_list_remove(fspset->fsps_list, node); 5185 free(node); 5186 node = next_node; 5187 } 5188 5189 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool); 5190 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool); 5191 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool); 5192 } 5193 5194 static inline void 5195 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type, 5196 const char *name) 5197 { 5198 deleg_perm->dp_who_type = type; 5199 deleg_perm->dp_name = name; 5200 } 5201 5202 static inline void 5203 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm, 5204 zfs_deleg_who_type_t type, const char *name) 5205 { 5206 uu_avl_pool_t *pool; 5207 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool; 5208 5209 bzero(who_perm, sizeof (who_perm_t)); 5210 5211 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL, 5212 UU_DEFAULT)) == NULL) 5213 nomem(); 5214 5215 who_perm->who_type = type; 5216 who_perm->who_name = name; 5217 who_perm->who_fsperm = fsperm; 5218 } 5219 5220 static inline void 5221 who_perm_fini(who_perm_t *who_perm) 5222 { 5223 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl); 5224 5225 while (node != NULL) { 5226 deleg_perm_node_t *next_node = 5227 uu_avl_next(who_perm->who_deleg_perm_avl, node); 5228 5229 uu_avl_remove(who_perm->who_deleg_perm_avl, node); 5230 free(node); 5231 node = next_node; 5232 } 5233 5234 uu_avl_destroy(who_perm->who_deleg_perm_avl); 5235 } 5236 5237 static inline void 5238 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname) 5239 { 5240 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool; 5241 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool; 5242 5243 bzero(fsperm, sizeof (fs_perm_t)); 5244 5245 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT)) 5246 == NULL) 5247 nomem(); 5248 5249 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT)) 5250 == NULL) 5251 nomem(); 5252 5253 fsperm->fsp_set = fspset; 5254 fsperm->fsp_name = fsname; 5255 } 5256 5257 static inline void 5258 fs_perm_fini(fs_perm_t *fsperm) 5259 { 5260 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl); 5261 while (node != NULL) { 5262 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl, 5263 node); 5264 who_perm_t *who_perm = &node->who_perm; 5265 who_perm_fini(who_perm); 5266 uu_avl_remove(fsperm->fsp_sc_avl, node); 5267 free(node); 5268 node = next_node; 5269 } 5270 5271 node = uu_avl_first(fsperm->fsp_uge_avl); 5272 while (node != NULL) { 5273 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl, 5274 node); 5275 who_perm_t *who_perm = &node->who_perm; 5276 who_perm_fini(who_perm); 5277 uu_avl_remove(fsperm->fsp_uge_avl, node); 5278 free(node); 5279 node = next_node; 5280 } 5281 5282 uu_avl_destroy(fsperm->fsp_sc_avl); 5283 uu_avl_destroy(fsperm->fsp_uge_avl); 5284 } 5285 5286 static void 5287 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node, 5288 zfs_deleg_who_type_t who_type, const char *name, char locality) 5289 { 5290 uu_avl_index_t idx = 0; 5291 5292 deleg_perm_node_t *found_node = NULL; 5293 deleg_perm_t *deleg_perm = &node->dpn_perm; 5294 5295 deleg_perm_init(deleg_perm, who_type, name); 5296 5297 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 5298 == NULL) 5299 uu_avl_insert(avl, node, idx); 5300 else { 5301 node = found_node; 5302 deleg_perm = &node->dpn_perm; 5303 } 5304 5305 5306 switch (locality) { 5307 case ZFS_DELEG_LOCAL: 5308 deleg_perm->dp_local = B_TRUE; 5309 break; 5310 case ZFS_DELEG_DESCENDENT: 5311 deleg_perm->dp_descend = B_TRUE; 5312 break; 5313 case ZFS_DELEG_NA: 5314 break; 5315 default: 5316 assert(B_FALSE); /* invalid locality */ 5317 } 5318 } 5319 5320 static inline int 5321 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality) 5322 { 5323 nvpair_t *nvp = NULL; 5324 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set; 5325 uu_avl_t *avl = who_perm->who_deleg_perm_avl; 5326 zfs_deleg_who_type_t who_type = who_perm->who_type; 5327 5328 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5329 const char *name = nvpair_name(nvp); 5330 data_type_t type = nvpair_type(nvp); 5331 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool; 5332 deleg_perm_node_t *node = 5333 safe_malloc(sizeof (deleg_perm_node_t)); 5334 5335 VERIFY(type == DATA_TYPE_BOOLEAN); 5336 5337 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool); 5338 set_deleg_perm_node(avl, node, who_type, name, locality); 5339 } 5340 5341 return (0); 5342 } 5343 5344 static inline int 5345 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl) 5346 { 5347 nvpair_t *nvp = NULL; 5348 fs_perm_set_t *fspset = fsperm->fsp_set; 5349 5350 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5351 nvlist_t *nvl2 = NULL; 5352 const char *name = nvpair_name(nvp); 5353 uu_avl_t *avl = NULL; 5354 uu_avl_pool_t *avl_pool = NULL; 5355 zfs_deleg_who_type_t perm_type = name[0]; 5356 char perm_locality = name[1]; 5357 const char *perm_name = name + 3; 5358 who_perm_t *who_perm = NULL; 5359 5360 assert('$' == name[2]); 5361 5362 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 5363 return (-1); 5364 5365 switch (perm_type) { 5366 case ZFS_DELEG_CREATE: 5367 case ZFS_DELEG_CREATE_SETS: 5368 case ZFS_DELEG_NAMED_SET: 5369 case ZFS_DELEG_NAMED_SET_SETS: 5370 avl_pool = fspset->fsps_named_set_avl_pool; 5371 avl = fsperm->fsp_sc_avl; 5372 break; 5373 case ZFS_DELEG_USER: 5374 case ZFS_DELEG_USER_SETS: 5375 case ZFS_DELEG_GROUP: 5376 case ZFS_DELEG_GROUP_SETS: 5377 case ZFS_DELEG_EVERYONE: 5378 case ZFS_DELEG_EVERYONE_SETS: 5379 avl_pool = fspset->fsps_who_perm_avl_pool; 5380 avl = fsperm->fsp_uge_avl; 5381 break; 5382 5383 default: 5384 assert(!"unhandled zfs_deleg_who_type_t"); 5385 } 5386 5387 who_perm_node_t *found_node = NULL; 5388 who_perm_node_t *node = safe_malloc( 5389 sizeof (who_perm_node_t)); 5390 who_perm = &node->who_perm; 5391 uu_avl_index_t idx = 0; 5392 5393 uu_avl_node_init(node, &node->who_avl_node, avl_pool); 5394 who_perm_init(who_perm, fsperm, perm_type, perm_name); 5395 5396 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 5397 == NULL) { 5398 if (avl == fsperm->fsp_uge_avl) { 5399 uid_t rid = 0; 5400 struct passwd *p = NULL; 5401 struct group *g = NULL; 5402 const char *nice_name = NULL; 5403 5404 switch (perm_type) { 5405 case ZFS_DELEG_USER_SETS: 5406 case ZFS_DELEG_USER: 5407 rid = atoi(perm_name); 5408 p = getpwuid(rid); 5409 if (p) 5410 nice_name = p->pw_name; 5411 break; 5412 case ZFS_DELEG_GROUP_SETS: 5413 case ZFS_DELEG_GROUP: 5414 rid = atoi(perm_name); 5415 g = getgrgid(rid); 5416 if (g) 5417 nice_name = g->gr_name; 5418 break; 5419 5420 default: 5421 break; 5422 } 5423 5424 if (nice_name != NULL) { 5425 (void) strlcpy( 5426 node->who_perm.who_ug_name, 5427 nice_name, 256); 5428 } else { 5429 /* User or group unknown */ 5430 (void) snprintf( 5431 node->who_perm.who_ug_name, 5432 sizeof (node->who_perm.who_ug_name), 5433 "(unknown: %d)", rid); 5434 } 5435 } 5436 5437 uu_avl_insert(avl, node, idx); 5438 } else { 5439 node = found_node; 5440 who_perm = &node->who_perm; 5441 } 5442 5443 assert(who_perm != NULL); 5444 (void) parse_who_perm(who_perm, nvl2, perm_locality); 5445 } 5446 5447 return (0); 5448 } 5449 5450 static inline int 5451 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl) 5452 { 5453 nvpair_t *nvp = NULL; 5454 uu_avl_index_t idx = 0; 5455 5456 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5457 nvlist_t *nvl2 = NULL; 5458 const char *fsname = nvpair_name(nvp); 5459 data_type_t type = nvpair_type(nvp); 5460 fs_perm_t *fsperm = NULL; 5461 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t)); 5462 if (node == NULL) 5463 nomem(); 5464 5465 fsperm = &node->fspn_fsperm; 5466 5467 VERIFY(DATA_TYPE_NVLIST == type); 5468 5469 uu_list_node_init(node, &node->fspn_list_node, 5470 fspset->fsps_list_pool); 5471 5472 idx = uu_list_numnodes(fspset->fsps_list); 5473 fs_perm_init(fsperm, fspset, fsname); 5474 5475 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 5476 return (-1); 5477 5478 (void) parse_fs_perm(fsperm, nvl2); 5479 5480 uu_list_insert(fspset->fsps_list, node, idx); 5481 } 5482 5483 return (0); 5484 } 5485 5486 static inline const char * 5487 deleg_perm_comment(zfs_deleg_note_t note) 5488 { 5489 const char *str = ""; 5490 5491 /* subcommands */ 5492 switch (note) { 5493 /* SUBCOMMANDS */ 5494 case ZFS_DELEG_NOTE_ALLOW: 5495 str = gettext("Must also have the permission that is being" 5496 "\n\t\t\t\tallowed"); 5497 break; 5498 case ZFS_DELEG_NOTE_CLONE: 5499 str = gettext("Must also have the 'create' ability and 'mount'" 5500 "\n\t\t\t\tability in the origin file system"); 5501 break; 5502 case ZFS_DELEG_NOTE_CREATE: 5503 str = gettext("Must also have the 'mount' ability"); 5504 break; 5505 case ZFS_DELEG_NOTE_DESTROY: 5506 str = gettext("Must also have the 'mount' ability"); 5507 break; 5508 case ZFS_DELEG_NOTE_DIFF: 5509 str = gettext("Allows lookup of paths within a dataset;" 5510 "\n\t\t\t\tgiven an object number. Ordinary users need this" 5511 "\n\t\t\t\tin order to use zfs diff"); 5512 break; 5513 case ZFS_DELEG_NOTE_HOLD: 5514 str = gettext("Allows adding a user hold to a snapshot"); 5515 break; 5516 case ZFS_DELEG_NOTE_MOUNT: 5517 str = gettext("Allows mount/umount of ZFS datasets"); 5518 break; 5519 case ZFS_DELEG_NOTE_PROMOTE: 5520 str = gettext("Must also have the 'mount'\n\t\t\t\tand" 5521 " 'promote' ability in the origin file system"); 5522 break; 5523 case ZFS_DELEG_NOTE_RECEIVE: 5524 str = gettext("Must also have the 'mount' and 'create'" 5525 " ability"); 5526 break; 5527 case ZFS_DELEG_NOTE_RELEASE: 5528 str = gettext("Allows releasing a user hold which\n\t\t\t\t" 5529 "might destroy the snapshot"); 5530 break; 5531 case ZFS_DELEG_NOTE_RENAME: 5532 str = gettext("Must also have the 'mount' and 'create'" 5533 "\n\t\t\t\tability in the new parent"); 5534 break; 5535 case ZFS_DELEG_NOTE_ROLLBACK: 5536 str = gettext(""); 5537 break; 5538 case ZFS_DELEG_NOTE_SEND: 5539 str = gettext(""); 5540 break; 5541 case ZFS_DELEG_NOTE_SHARE: 5542 str = gettext("Allows sharing file systems over NFS or SMB" 5543 "\n\t\t\t\tprotocols"); 5544 break; 5545 case ZFS_DELEG_NOTE_SNAPSHOT: 5546 str = gettext(""); 5547 break; 5548 case ZFS_DELEG_NOTE_LOAD_KEY: 5549 str = gettext("Allows loading or unloading an encryption key"); 5550 break; 5551 case ZFS_DELEG_NOTE_CHANGE_KEY: 5552 str = gettext("Allows changing or adding an encryption key"); 5553 break; 5554 /* 5555 * case ZFS_DELEG_NOTE_VSCAN: 5556 * str = gettext(""); 5557 * break; 5558 */ 5559 /* OTHER */ 5560 case ZFS_DELEG_NOTE_GROUPQUOTA: 5561 str = gettext("Allows accessing any groupquota@... property"); 5562 break; 5563 case ZFS_DELEG_NOTE_GROUPUSED: 5564 str = gettext("Allows reading any groupused@... property"); 5565 break; 5566 case ZFS_DELEG_NOTE_USERPROP: 5567 str = gettext("Allows changing any user property"); 5568 break; 5569 case ZFS_DELEG_NOTE_USERQUOTA: 5570 str = gettext("Allows accessing any userquota@... property"); 5571 break; 5572 case ZFS_DELEG_NOTE_USERUSED: 5573 str = gettext("Allows reading any userused@... property"); 5574 break; 5575 case ZFS_DELEG_NOTE_USEROBJQUOTA: 5576 str = gettext("Allows accessing any userobjquota@... property"); 5577 break; 5578 case ZFS_DELEG_NOTE_GROUPOBJQUOTA: 5579 str = gettext("Allows accessing any \n\t\t\t\t" 5580 "groupobjquota@... property"); 5581 break; 5582 case ZFS_DELEG_NOTE_GROUPOBJUSED: 5583 str = gettext("Allows reading any groupobjused@... property"); 5584 break; 5585 case ZFS_DELEG_NOTE_USEROBJUSED: 5586 str = gettext("Allows reading any userobjused@... property"); 5587 break; 5588 case ZFS_DELEG_NOTE_PROJECTQUOTA: 5589 str = gettext("Allows accessing any projectquota@... property"); 5590 break; 5591 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA: 5592 str = gettext("Allows accessing any \n\t\t\t\t" 5593 "projectobjquota@... property"); 5594 break; 5595 case ZFS_DELEG_NOTE_PROJECTUSED: 5596 str = gettext("Allows reading any projectused@... property"); 5597 break; 5598 case ZFS_DELEG_NOTE_PROJECTOBJUSED: 5599 str = gettext("Allows accessing any \n\t\t\t\t" 5600 "projectobjused@... property"); 5601 break; 5602 /* other */ 5603 default: 5604 str = ""; 5605 } 5606 5607 return (str); 5608 } 5609 5610 struct allow_opts { 5611 boolean_t local; 5612 boolean_t descend; 5613 boolean_t user; 5614 boolean_t group; 5615 boolean_t everyone; 5616 boolean_t create; 5617 boolean_t set; 5618 boolean_t recursive; /* unallow only */ 5619 boolean_t prt_usage; 5620 5621 boolean_t prt_perms; 5622 char *who; 5623 char *perms; 5624 const char *dataset; 5625 }; 5626 5627 static inline int 5628 prop_cmp(const void *a, const void *b) 5629 { 5630 const char *str1 = *(const char **)a; 5631 const char *str2 = *(const char **)b; 5632 return (strcmp(str1, str2)); 5633 } 5634 5635 static void 5636 allow_usage(boolean_t un, boolean_t requested, const char *msg) 5637 { 5638 const char *opt_desc[] = { 5639 "-h", gettext("show this help message and exit"), 5640 "-l", gettext("set permission locally"), 5641 "-d", gettext("set permission for descents"), 5642 "-u", gettext("set permission for user"), 5643 "-g", gettext("set permission for group"), 5644 "-e", gettext("set permission for everyone"), 5645 "-c", gettext("set create time permission"), 5646 "-s", gettext("define permission set"), 5647 /* unallow only */ 5648 "-r", gettext("remove permissions recursively"), 5649 }; 5650 size_t unallow_size = sizeof (opt_desc) / sizeof (char *); 5651 size_t allow_size = unallow_size - 2; 5652 const char *props[ZFS_NUM_PROPS]; 5653 int i; 5654 size_t count = 0; 5655 FILE *fp = requested ? stdout : stderr; 5656 zprop_desc_t *pdtbl = zfs_prop_get_table(); 5657 const char *fmt = gettext("%-16s %-14s\t%s\n"); 5658 5659 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW : 5660 HELP_ALLOW)); 5661 (void) fprintf(fp, gettext("Options:\n")); 5662 for (i = 0; i < (un ? unallow_size : allow_size); i += 2) { 5663 const char *opt = opt_desc[i]; 5664 const char *optdsc = opt_desc[i + 1]; 5665 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc); 5666 } 5667 5668 (void) fprintf(fp, gettext("\nThe following permissions are " 5669 "supported:\n\n")); 5670 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"), 5671 gettext("NOTES")); 5672 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) { 5673 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm; 5674 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note; 5675 const char *perm_type = deleg_perm_type(perm_note); 5676 const char *perm_comment = deleg_perm_comment(perm_note); 5677 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment); 5678 } 5679 5680 for (i = 0; i < ZFS_NUM_PROPS; i++) { 5681 zprop_desc_t *pd = &pdtbl[i]; 5682 if (pd->pd_visible != B_TRUE) 5683 continue; 5684 5685 if (pd->pd_attr == PROP_READONLY) 5686 continue; 5687 5688 props[count++] = pd->pd_name; 5689 } 5690 props[count] = NULL; 5691 5692 qsort(props, count, sizeof (char *), prop_cmp); 5693 5694 for (i = 0; i < count; i++) 5695 (void) fprintf(fp, fmt, props[i], gettext("property"), ""); 5696 5697 if (msg != NULL) 5698 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg); 5699 5700 exit(requested ? 0 : 2); 5701 } 5702 5703 static inline const char * 5704 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc, 5705 char **permsp) 5706 { 5707 if (un && argc == expected_argc - 1) 5708 *permsp = NULL; 5709 else if (argc == expected_argc) 5710 *permsp = argv[argc - 2]; 5711 else 5712 allow_usage(un, B_FALSE, 5713 gettext("wrong number of parameters\n")); 5714 5715 return (argv[argc - 1]); 5716 } 5717 5718 static void 5719 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts) 5720 { 5721 int uge_sum = opts->user + opts->group + opts->everyone; 5722 int csuge_sum = opts->create + opts->set + uge_sum; 5723 int ldcsuge_sum = csuge_sum + opts->local + opts->descend; 5724 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum; 5725 5726 if (uge_sum > 1) 5727 allow_usage(un, B_FALSE, 5728 gettext("-u, -g, and -e are mutually exclusive\n")); 5729 5730 if (opts->prt_usage) { 5731 if (argc == 0 && all_sum == 0) 5732 allow_usage(un, B_TRUE, NULL); 5733 else 5734 usage(B_FALSE); 5735 } 5736 5737 if (opts->set) { 5738 if (csuge_sum > 1) 5739 allow_usage(un, B_FALSE, 5740 gettext("invalid options combined with -s\n")); 5741 5742 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 5743 if (argv[0][0] != '@') 5744 allow_usage(un, B_FALSE, 5745 gettext("invalid set name: missing '@' prefix\n")); 5746 opts->who = argv[0]; 5747 } else if (opts->create) { 5748 if (ldcsuge_sum > 1) 5749 allow_usage(un, B_FALSE, 5750 gettext("invalid options combined with -c\n")); 5751 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 5752 } else if (opts->everyone) { 5753 if (csuge_sum > 1) 5754 allow_usage(un, B_FALSE, 5755 gettext("invalid options combined with -e\n")); 5756 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 5757 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone") 5758 == 0) { 5759 opts->everyone = B_TRUE; 5760 argc--; 5761 argv++; 5762 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 5763 } else if (argc == 1 && !un) { 5764 opts->prt_perms = B_TRUE; 5765 opts->dataset = argv[argc-1]; 5766 } else { 5767 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 5768 opts->who = argv[0]; 5769 } 5770 5771 if (!opts->local && !opts->descend) { 5772 opts->local = B_TRUE; 5773 opts->descend = B_TRUE; 5774 } 5775 } 5776 5777 static void 5778 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend, 5779 const char *who, char *perms, nvlist_t *top_nvl) 5780 { 5781 int i; 5782 char ld[2] = { '\0', '\0' }; 5783 char who_buf[MAXNAMELEN + 32]; 5784 char base_type = '\0'; 5785 char set_type = '\0'; 5786 nvlist_t *base_nvl = NULL; 5787 nvlist_t *set_nvl = NULL; 5788 nvlist_t *nvl; 5789 5790 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0) 5791 nomem(); 5792 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0) 5793 nomem(); 5794 5795 switch (type) { 5796 case ZFS_DELEG_NAMED_SET_SETS: 5797 case ZFS_DELEG_NAMED_SET: 5798 set_type = ZFS_DELEG_NAMED_SET_SETS; 5799 base_type = ZFS_DELEG_NAMED_SET; 5800 ld[0] = ZFS_DELEG_NA; 5801 break; 5802 case ZFS_DELEG_CREATE_SETS: 5803 case ZFS_DELEG_CREATE: 5804 set_type = ZFS_DELEG_CREATE_SETS; 5805 base_type = ZFS_DELEG_CREATE; 5806 ld[0] = ZFS_DELEG_NA; 5807 break; 5808 case ZFS_DELEG_USER_SETS: 5809 case ZFS_DELEG_USER: 5810 set_type = ZFS_DELEG_USER_SETS; 5811 base_type = ZFS_DELEG_USER; 5812 if (local) 5813 ld[0] = ZFS_DELEG_LOCAL; 5814 if (descend) 5815 ld[1] = ZFS_DELEG_DESCENDENT; 5816 break; 5817 case ZFS_DELEG_GROUP_SETS: 5818 case ZFS_DELEG_GROUP: 5819 set_type = ZFS_DELEG_GROUP_SETS; 5820 base_type = ZFS_DELEG_GROUP; 5821 if (local) 5822 ld[0] = ZFS_DELEG_LOCAL; 5823 if (descend) 5824 ld[1] = ZFS_DELEG_DESCENDENT; 5825 break; 5826 case ZFS_DELEG_EVERYONE_SETS: 5827 case ZFS_DELEG_EVERYONE: 5828 set_type = ZFS_DELEG_EVERYONE_SETS; 5829 base_type = ZFS_DELEG_EVERYONE; 5830 if (local) 5831 ld[0] = ZFS_DELEG_LOCAL; 5832 if (descend) 5833 ld[1] = ZFS_DELEG_DESCENDENT; 5834 break; 5835 5836 default: 5837 assert(set_type != '\0' && base_type != '\0'); 5838 } 5839 5840 if (perms != NULL) { 5841 char *curr = perms; 5842 char *end = curr + strlen(perms); 5843 5844 while (curr < end) { 5845 char *delim = strchr(curr, ','); 5846 if (delim == NULL) 5847 delim = end; 5848 else 5849 *delim = '\0'; 5850 5851 if (curr[0] == '@') 5852 nvl = set_nvl; 5853 else 5854 nvl = base_nvl; 5855 5856 (void) nvlist_add_boolean(nvl, curr); 5857 if (delim != end) 5858 *delim = ','; 5859 curr = delim + 1; 5860 } 5861 5862 for (i = 0; i < 2; i++) { 5863 char locality = ld[i]; 5864 if (locality == 0) 5865 continue; 5866 5867 if (!nvlist_empty(base_nvl)) { 5868 if (who != NULL) 5869 (void) snprintf(who_buf, 5870 sizeof (who_buf), "%c%c$%s", 5871 base_type, locality, who); 5872 else 5873 (void) snprintf(who_buf, 5874 sizeof (who_buf), "%c%c$", 5875 base_type, locality); 5876 5877 (void) nvlist_add_nvlist(top_nvl, who_buf, 5878 base_nvl); 5879 } 5880 5881 5882 if (!nvlist_empty(set_nvl)) { 5883 if (who != NULL) 5884 (void) snprintf(who_buf, 5885 sizeof (who_buf), "%c%c$%s", 5886 set_type, locality, who); 5887 else 5888 (void) snprintf(who_buf, 5889 sizeof (who_buf), "%c%c$", 5890 set_type, locality); 5891 5892 (void) nvlist_add_nvlist(top_nvl, who_buf, 5893 set_nvl); 5894 } 5895 } 5896 } else { 5897 for (i = 0; i < 2; i++) { 5898 char locality = ld[i]; 5899 if (locality == 0) 5900 continue; 5901 5902 if (who != NULL) 5903 (void) snprintf(who_buf, sizeof (who_buf), 5904 "%c%c$%s", base_type, locality, who); 5905 else 5906 (void) snprintf(who_buf, sizeof (who_buf), 5907 "%c%c$", base_type, locality); 5908 (void) nvlist_add_boolean(top_nvl, who_buf); 5909 5910 if (who != NULL) 5911 (void) snprintf(who_buf, sizeof (who_buf), 5912 "%c%c$%s", set_type, locality, who); 5913 else 5914 (void) snprintf(who_buf, sizeof (who_buf), 5915 "%c%c$", set_type, locality); 5916 (void) nvlist_add_boolean(top_nvl, who_buf); 5917 } 5918 } 5919 } 5920 5921 static int 5922 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp) 5923 { 5924 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) 5925 nomem(); 5926 5927 if (opts->set) { 5928 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local, 5929 opts->descend, opts->who, opts->perms, *nvlp); 5930 } else if (opts->create) { 5931 store_allow_perm(ZFS_DELEG_CREATE, opts->local, 5932 opts->descend, NULL, opts->perms, *nvlp); 5933 } else if (opts->everyone) { 5934 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local, 5935 opts->descend, NULL, opts->perms, *nvlp); 5936 } else { 5937 char *curr = opts->who; 5938 char *end = curr + strlen(curr); 5939 5940 while (curr < end) { 5941 const char *who; 5942 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN; 5943 char *endch; 5944 char *delim = strchr(curr, ','); 5945 char errbuf[256]; 5946 char id[64]; 5947 struct passwd *p = NULL; 5948 struct group *g = NULL; 5949 5950 uid_t rid; 5951 if (delim == NULL) 5952 delim = end; 5953 else 5954 *delim = '\0'; 5955 5956 rid = (uid_t)strtol(curr, &endch, 0); 5957 if (opts->user) { 5958 who_type = ZFS_DELEG_USER; 5959 if (*endch != '\0') 5960 p = getpwnam(curr); 5961 else 5962 p = getpwuid(rid); 5963 5964 if (p != NULL) 5965 rid = p->pw_uid; 5966 else if (*endch != '\0') { 5967 (void) snprintf(errbuf, 256, gettext( 5968 "invalid user %s\n"), curr); 5969 allow_usage(un, B_TRUE, errbuf); 5970 } 5971 } else if (opts->group) { 5972 who_type = ZFS_DELEG_GROUP; 5973 if (*endch != '\0') 5974 g = getgrnam(curr); 5975 else 5976 g = getgrgid(rid); 5977 5978 if (g != NULL) 5979 rid = g->gr_gid; 5980 else if (*endch != '\0') { 5981 (void) snprintf(errbuf, 256, gettext( 5982 "invalid group %s\n"), curr); 5983 allow_usage(un, B_TRUE, errbuf); 5984 } 5985 } else { 5986 if (*endch != '\0') { 5987 p = getpwnam(curr); 5988 } else { 5989 p = getpwuid(rid); 5990 } 5991 5992 if (p == NULL) { 5993 if (*endch != '\0') { 5994 g = getgrnam(curr); 5995 } else { 5996 g = getgrgid(rid); 5997 } 5998 } 5999 6000 if (p != NULL) { 6001 who_type = ZFS_DELEG_USER; 6002 rid = p->pw_uid; 6003 } else if (g != NULL) { 6004 who_type = ZFS_DELEG_GROUP; 6005 rid = g->gr_gid; 6006 } else { 6007 (void) snprintf(errbuf, 256, gettext( 6008 "invalid user/group %s\n"), curr); 6009 allow_usage(un, B_TRUE, errbuf); 6010 } 6011 } 6012 6013 (void) sprintf(id, "%u", rid); 6014 who = id; 6015 6016 store_allow_perm(who_type, opts->local, 6017 opts->descend, who, opts->perms, *nvlp); 6018 curr = delim + 1; 6019 } 6020 } 6021 6022 return (0); 6023 } 6024 6025 static void 6026 print_set_creat_perms(uu_avl_t *who_avl) 6027 { 6028 const char *sc_title[] = { 6029 gettext("Permission sets:\n"), 6030 gettext("Create time permissions:\n"), 6031 NULL 6032 }; 6033 who_perm_node_t *who_node = NULL; 6034 int prev_weight = -1; 6035 6036 for (who_node = uu_avl_first(who_avl); who_node != NULL; 6037 who_node = uu_avl_next(who_avl, who_node)) { 6038 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 6039 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 6040 const char *who_name = who_node->who_perm.who_name; 6041 int weight = who_type2weight(who_type); 6042 boolean_t first = B_TRUE; 6043 deleg_perm_node_t *deleg_node; 6044 6045 if (prev_weight != weight) { 6046 (void) printf("%s", sc_title[weight]); 6047 prev_weight = weight; 6048 } 6049 6050 if (who_name == NULL || strnlen(who_name, 1) == 0) 6051 (void) printf("\t"); 6052 else 6053 (void) printf("\t%s ", who_name); 6054 6055 for (deleg_node = uu_avl_first(avl); deleg_node != NULL; 6056 deleg_node = uu_avl_next(avl, deleg_node)) { 6057 if (first) { 6058 (void) printf("%s", 6059 deleg_node->dpn_perm.dp_name); 6060 first = B_FALSE; 6061 } else 6062 (void) printf(",%s", 6063 deleg_node->dpn_perm.dp_name); 6064 } 6065 6066 (void) printf("\n"); 6067 } 6068 } 6069 6070 static void 6071 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend, 6072 const char *title) 6073 { 6074 who_perm_node_t *who_node = NULL; 6075 boolean_t prt_title = B_TRUE; 6076 uu_avl_walk_t *walk; 6077 6078 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL) 6079 nomem(); 6080 6081 while ((who_node = uu_avl_walk_next(walk)) != NULL) { 6082 const char *who_name = who_node->who_perm.who_name; 6083 const char *nice_who_name = who_node->who_perm.who_ug_name; 6084 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 6085 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 6086 char delim = ' '; 6087 deleg_perm_node_t *deleg_node; 6088 boolean_t prt_who = B_TRUE; 6089 6090 for (deleg_node = uu_avl_first(avl); 6091 deleg_node != NULL; 6092 deleg_node = uu_avl_next(avl, deleg_node)) { 6093 if (local != deleg_node->dpn_perm.dp_local || 6094 descend != deleg_node->dpn_perm.dp_descend) 6095 continue; 6096 6097 if (prt_who) { 6098 const char *who = NULL; 6099 if (prt_title) { 6100 prt_title = B_FALSE; 6101 (void) printf("%s", title); 6102 } 6103 6104 switch (who_type) { 6105 case ZFS_DELEG_USER_SETS: 6106 case ZFS_DELEG_USER: 6107 who = gettext("user"); 6108 if (nice_who_name) 6109 who_name = nice_who_name; 6110 break; 6111 case ZFS_DELEG_GROUP_SETS: 6112 case ZFS_DELEG_GROUP: 6113 who = gettext("group"); 6114 if (nice_who_name) 6115 who_name = nice_who_name; 6116 break; 6117 case ZFS_DELEG_EVERYONE_SETS: 6118 case ZFS_DELEG_EVERYONE: 6119 who = gettext("everyone"); 6120 who_name = NULL; 6121 break; 6122 6123 default: 6124 assert(who != NULL); 6125 } 6126 6127 prt_who = B_FALSE; 6128 if (who_name == NULL) 6129 (void) printf("\t%s", who); 6130 else 6131 (void) printf("\t%s %s", who, who_name); 6132 } 6133 6134 (void) printf("%c%s", delim, 6135 deleg_node->dpn_perm.dp_name); 6136 delim = ','; 6137 } 6138 6139 if (!prt_who) 6140 (void) printf("\n"); 6141 } 6142 6143 uu_avl_walk_end(walk); 6144 } 6145 6146 static void 6147 print_fs_perms(fs_perm_set_t *fspset) 6148 { 6149 fs_perm_node_t *node = NULL; 6150 char buf[MAXNAMELEN + 32]; 6151 const char *dsname = buf; 6152 6153 for (node = uu_list_first(fspset->fsps_list); node != NULL; 6154 node = uu_list_next(fspset->fsps_list, node)) { 6155 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl; 6156 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl; 6157 int left = 0; 6158 6159 (void) snprintf(buf, sizeof (buf), 6160 gettext("---- Permissions on %s "), 6161 node->fspn_fsperm.fsp_name); 6162 (void) printf("%s", dsname); 6163 left = 70 - strlen(buf); 6164 while (left-- > 0) 6165 (void) printf("-"); 6166 (void) printf("\n"); 6167 6168 print_set_creat_perms(sc_avl); 6169 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE, 6170 gettext("Local permissions:\n")); 6171 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE, 6172 gettext("Descendent permissions:\n")); 6173 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE, 6174 gettext("Local+Descendent permissions:\n")); 6175 } 6176 } 6177 6178 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL }; 6179 6180 struct deleg_perms { 6181 boolean_t un; 6182 nvlist_t *nvl; 6183 }; 6184 6185 static int 6186 set_deleg_perms(zfs_handle_t *zhp, void *data) 6187 { 6188 struct deleg_perms *perms = (struct deleg_perms *)data; 6189 zfs_type_t zfs_type = zfs_get_type(zhp); 6190 6191 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME) 6192 return (0); 6193 6194 return (zfs_set_fsacl(zhp, perms->un, perms->nvl)); 6195 } 6196 6197 static int 6198 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un) 6199 { 6200 zfs_handle_t *zhp; 6201 nvlist_t *perm_nvl = NULL; 6202 nvlist_t *update_perm_nvl = NULL; 6203 int error = 1; 6204 int c; 6205 struct allow_opts opts = { 0 }; 6206 6207 const char *optstr = un ? "ldugecsrh" : "ldugecsh"; 6208 6209 /* check opts */ 6210 while ((c = getopt(argc, argv, optstr)) != -1) { 6211 switch (c) { 6212 case 'l': 6213 opts.local = B_TRUE; 6214 break; 6215 case 'd': 6216 opts.descend = B_TRUE; 6217 break; 6218 case 'u': 6219 opts.user = B_TRUE; 6220 break; 6221 case 'g': 6222 opts.group = B_TRUE; 6223 break; 6224 case 'e': 6225 opts.everyone = B_TRUE; 6226 break; 6227 case 's': 6228 opts.set = B_TRUE; 6229 break; 6230 case 'c': 6231 opts.create = B_TRUE; 6232 break; 6233 case 'r': 6234 opts.recursive = B_TRUE; 6235 break; 6236 case ':': 6237 (void) fprintf(stderr, gettext("missing argument for " 6238 "'%c' option\n"), optopt); 6239 usage(B_FALSE); 6240 break; 6241 case 'h': 6242 opts.prt_usage = B_TRUE; 6243 break; 6244 case '?': 6245 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6246 optopt); 6247 usage(B_FALSE); 6248 } 6249 } 6250 6251 argc -= optind; 6252 argv += optind; 6253 6254 /* check arguments */ 6255 parse_allow_args(argc, argv, un, &opts); 6256 6257 /* try to open the dataset */ 6258 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM | 6259 ZFS_TYPE_VOLUME)) == NULL) { 6260 (void) fprintf(stderr, "Failed to open dataset: %s\n", 6261 opts.dataset); 6262 return (-1); 6263 } 6264 6265 if (zfs_get_fsacl(zhp, &perm_nvl) != 0) 6266 goto cleanup2; 6267 6268 fs_perm_set_init(&fs_perm_set); 6269 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) { 6270 (void) fprintf(stderr, "Failed to parse fsacl permissions\n"); 6271 goto cleanup1; 6272 } 6273 6274 if (opts.prt_perms) 6275 print_fs_perms(&fs_perm_set); 6276 else { 6277 (void) construct_fsacl_list(un, &opts, &update_perm_nvl); 6278 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0) 6279 goto cleanup0; 6280 6281 if (un && opts.recursive) { 6282 struct deleg_perms data = { un, update_perm_nvl }; 6283 if (zfs_iter_filesystems(zhp, set_deleg_perms, 6284 &data) != 0) 6285 goto cleanup0; 6286 } 6287 } 6288 6289 error = 0; 6290 6291 cleanup0: 6292 nvlist_free(perm_nvl); 6293 nvlist_free(update_perm_nvl); 6294 cleanup1: 6295 fs_perm_set_fini(&fs_perm_set); 6296 cleanup2: 6297 zfs_close(zhp); 6298 6299 return (error); 6300 } 6301 6302 static int 6303 zfs_do_allow(int argc, char **argv) 6304 { 6305 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE)); 6306 } 6307 6308 static int 6309 zfs_do_unallow(int argc, char **argv) 6310 { 6311 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE)); 6312 } 6313 6314 static int 6315 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding) 6316 { 6317 int errors = 0; 6318 int i; 6319 const char *tag; 6320 boolean_t recursive = B_FALSE; 6321 const char *opts = holding ? "rt" : "r"; 6322 int c; 6323 6324 /* check options */ 6325 while ((c = getopt(argc, argv, opts)) != -1) { 6326 switch (c) { 6327 case 'r': 6328 recursive = B_TRUE; 6329 break; 6330 case '?': 6331 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6332 optopt); 6333 usage(B_FALSE); 6334 } 6335 } 6336 6337 argc -= optind; 6338 argv += optind; 6339 6340 /* check number of arguments */ 6341 if (argc < 2) 6342 usage(B_FALSE); 6343 6344 tag = argv[0]; 6345 --argc; 6346 ++argv; 6347 6348 if (holding && tag[0] == '.') { 6349 /* tags starting with '.' are reserved for libzfs */ 6350 (void) fprintf(stderr, gettext("tag may not start with '.'\n")); 6351 usage(B_FALSE); 6352 } 6353 6354 for (i = 0; i < argc; ++i) { 6355 zfs_handle_t *zhp; 6356 char parent[ZFS_MAX_DATASET_NAME_LEN]; 6357 const char *delim; 6358 char *path = argv[i]; 6359 6360 delim = strchr(path, '@'); 6361 if (delim == NULL) { 6362 (void) fprintf(stderr, 6363 gettext("'%s' is not a snapshot\n"), path); 6364 ++errors; 6365 continue; 6366 } 6367 (void) strncpy(parent, path, delim - path); 6368 parent[delim - path] = '\0'; 6369 6370 zhp = zfs_open(g_zfs, parent, 6371 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 6372 if (zhp == NULL) { 6373 ++errors; 6374 continue; 6375 } 6376 if (holding) { 6377 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0) 6378 ++errors; 6379 } else { 6380 if (zfs_release(zhp, delim+1, tag, recursive) != 0) 6381 ++errors; 6382 } 6383 zfs_close(zhp); 6384 } 6385 6386 return (errors != 0); 6387 } 6388 6389 /* 6390 * zfs hold [-r] [-t] <tag> <snap> ... 6391 * 6392 * -r Recursively hold 6393 * 6394 * Apply a user-hold with the given tag to the list of snapshots. 6395 */ 6396 static int 6397 zfs_do_hold(int argc, char **argv) 6398 { 6399 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE)); 6400 } 6401 6402 /* 6403 * zfs release [-r] <tag> <snap> ... 6404 * 6405 * -r Recursively release 6406 * 6407 * Release a user-hold with the given tag from the list of snapshots. 6408 */ 6409 static int 6410 zfs_do_release(int argc, char **argv) 6411 { 6412 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE)); 6413 } 6414 6415 typedef struct holds_cbdata { 6416 boolean_t cb_recursive; 6417 const char *cb_snapname; 6418 nvlist_t **cb_nvlp; 6419 size_t cb_max_namelen; 6420 size_t cb_max_taglen; 6421 } holds_cbdata_t; 6422 6423 #define STRFTIME_FMT_STR "%a %b %e %H:%M %Y" 6424 #define DATETIME_BUF_LEN (32) 6425 /* 6426 * 6427 */ 6428 static void 6429 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl) 6430 { 6431 int i; 6432 nvpair_t *nvp = NULL; 6433 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" }; 6434 const char *col; 6435 6436 if (!scripted) { 6437 for (i = 0; i < 3; i++) { 6438 col = gettext(hdr_cols[i]); 6439 if (i < 2) 6440 (void) printf("%-*s ", i ? tagwidth : nwidth, 6441 col); 6442 else 6443 (void) printf("%s\n", col); 6444 } 6445 } 6446 6447 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 6448 char *zname = nvpair_name(nvp); 6449 nvlist_t *nvl2; 6450 nvpair_t *nvp2 = NULL; 6451 (void) nvpair_value_nvlist(nvp, &nvl2); 6452 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) { 6453 char tsbuf[DATETIME_BUF_LEN]; 6454 char *tagname = nvpair_name(nvp2); 6455 uint64_t val = 0; 6456 time_t time; 6457 struct tm t; 6458 6459 (void) nvpair_value_uint64(nvp2, &val); 6460 time = (time_t)val; 6461 (void) localtime_r(&time, &t); 6462 (void) strftime(tsbuf, DATETIME_BUF_LEN, 6463 gettext(STRFTIME_FMT_STR), &t); 6464 6465 if (scripted) { 6466 (void) printf("%s\t%s\t%s\n", zname, 6467 tagname, tsbuf); 6468 } else { 6469 (void) printf("%-*s %-*s %s\n", nwidth, 6470 zname, tagwidth, tagname, tsbuf); 6471 } 6472 } 6473 } 6474 } 6475 6476 /* 6477 * Generic callback function to list a dataset or snapshot. 6478 */ 6479 static int 6480 holds_callback(zfs_handle_t *zhp, void *data) 6481 { 6482 holds_cbdata_t *cbp = data; 6483 nvlist_t *top_nvl = *cbp->cb_nvlp; 6484 nvlist_t *nvl = NULL; 6485 nvpair_t *nvp = NULL; 6486 const char *zname = zfs_get_name(zhp); 6487 size_t znamelen = strlen(zname); 6488 6489 if (cbp->cb_recursive) { 6490 const char *snapname; 6491 char *delim = strchr(zname, '@'); 6492 if (delim == NULL) 6493 return (0); 6494 6495 snapname = delim + 1; 6496 if (strcmp(cbp->cb_snapname, snapname)) 6497 return (0); 6498 } 6499 6500 if (zfs_get_holds(zhp, &nvl) != 0) 6501 return (-1); 6502 6503 if (znamelen > cbp->cb_max_namelen) 6504 cbp->cb_max_namelen = znamelen; 6505 6506 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 6507 const char *tag = nvpair_name(nvp); 6508 size_t taglen = strlen(tag); 6509 if (taglen > cbp->cb_max_taglen) 6510 cbp->cb_max_taglen = taglen; 6511 } 6512 6513 return (nvlist_add_nvlist(top_nvl, zname, nvl)); 6514 } 6515 6516 /* 6517 * zfs holds [-rH] <snap> ... 6518 * 6519 * -r Lists holds that are set on the named snapshots recursively. 6520 * -H Scripted mode; elide headers and separate columns by tabs. 6521 */ 6522 static int 6523 zfs_do_holds(int argc, char **argv) 6524 { 6525 int errors = 0; 6526 int c; 6527 int i; 6528 boolean_t scripted = B_FALSE; 6529 boolean_t recursive = B_FALSE; 6530 const char *opts = "rH"; 6531 nvlist_t *nvl; 6532 6533 int types = ZFS_TYPE_SNAPSHOT; 6534 holds_cbdata_t cb = { 0 }; 6535 6536 int limit = 0; 6537 int ret = 0; 6538 int flags = 0; 6539 6540 /* check options */ 6541 while ((c = getopt(argc, argv, opts)) != -1) { 6542 switch (c) { 6543 case 'r': 6544 recursive = B_TRUE; 6545 break; 6546 case 'H': 6547 scripted = B_TRUE; 6548 break; 6549 case '?': 6550 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6551 optopt); 6552 usage(B_FALSE); 6553 } 6554 } 6555 6556 if (recursive) { 6557 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 6558 flags |= ZFS_ITER_RECURSE; 6559 } 6560 6561 argc -= optind; 6562 argv += optind; 6563 6564 /* check number of arguments */ 6565 if (argc < 1) 6566 usage(B_FALSE); 6567 6568 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 6569 nomem(); 6570 6571 for (i = 0; i < argc; ++i) { 6572 char *snapshot = argv[i]; 6573 const char *delim; 6574 const char *snapname; 6575 6576 delim = strchr(snapshot, '@'); 6577 if (delim == NULL) { 6578 (void) fprintf(stderr, 6579 gettext("'%s' is not a snapshot\n"), snapshot); 6580 ++errors; 6581 continue; 6582 } 6583 snapname = delim + 1; 6584 if (recursive) 6585 snapshot[delim - snapshot] = '\0'; 6586 6587 cb.cb_recursive = recursive; 6588 cb.cb_snapname = snapname; 6589 cb.cb_nvlp = &nvl; 6590 6591 /* 6592 * 1. collect holds data, set format options 6593 */ 6594 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit, 6595 holds_callback, &cb); 6596 if (ret != 0) 6597 ++errors; 6598 } 6599 6600 /* 6601 * 2. print holds data 6602 */ 6603 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl); 6604 6605 if (nvlist_empty(nvl)) 6606 (void) fprintf(stderr, gettext("no datasets available\n")); 6607 6608 nvlist_free(nvl); 6609 6610 return (0 != errors); 6611 } 6612 6613 #define CHECK_SPINNER 30 6614 #define SPINNER_TIME 3 /* seconds */ 6615 #define MOUNT_TIME 1 /* seconds */ 6616 6617 typedef struct get_all_state { 6618 boolean_t ga_verbose; 6619 get_all_cb_t *ga_cbp; 6620 } get_all_state_t; 6621 6622 static int 6623 get_one_dataset(zfs_handle_t *zhp, void *data) 6624 { 6625 static char *spin[] = { "-", "\\", "|", "/" }; 6626 static int spinval = 0; 6627 static int spincheck = 0; 6628 static time_t last_spin_time = (time_t)0; 6629 get_all_state_t *state = data; 6630 zfs_type_t type = zfs_get_type(zhp); 6631 6632 if (state->ga_verbose) { 6633 if (--spincheck < 0) { 6634 time_t now = time(NULL); 6635 if (last_spin_time + SPINNER_TIME < now) { 6636 update_progress(spin[spinval++ % 4]); 6637 last_spin_time = now; 6638 } 6639 spincheck = CHECK_SPINNER; 6640 } 6641 } 6642 6643 /* 6644 * Iterate over any nested datasets. 6645 */ 6646 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) { 6647 zfs_close(zhp); 6648 return (1); 6649 } 6650 6651 /* 6652 * Skip any datasets whose type does not match. 6653 */ 6654 if ((type & ZFS_TYPE_FILESYSTEM) == 0) { 6655 zfs_close(zhp); 6656 return (0); 6657 } 6658 libzfs_add_handle(state->ga_cbp, zhp); 6659 assert(state->ga_cbp->cb_used <= state->ga_cbp->cb_alloc); 6660 6661 return (0); 6662 } 6663 6664 static void 6665 get_all_datasets(get_all_cb_t *cbp, boolean_t verbose) 6666 { 6667 get_all_state_t state = { 6668 .ga_verbose = verbose, 6669 .ga_cbp = cbp 6670 }; 6671 6672 if (verbose) 6673 set_progress_header(gettext("Reading ZFS config")); 6674 (void) zfs_iter_root(g_zfs, get_one_dataset, &state); 6675 6676 if (verbose) 6677 finish_progress(gettext("done.")); 6678 } 6679 6680 /* 6681 * Generic callback for sharing or mounting filesystems. Because the code is so 6682 * similar, we have a common function with an extra parameter to determine which 6683 * mode we are using. 6684 */ 6685 typedef enum { OP_SHARE, OP_MOUNT } share_mount_op_t; 6686 6687 typedef struct share_mount_state { 6688 share_mount_op_t sm_op; 6689 boolean_t sm_verbose; 6690 int sm_flags; 6691 char *sm_options; 6692 char *sm_proto; /* only valid for OP_SHARE */ 6693 pthread_mutex_t sm_lock; /* protects the remaining fields */ 6694 uint_t sm_total; /* number of filesystems to process */ 6695 uint_t sm_done; /* number of filesystems processed */ 6696 int sm_status; /* -1 if any of the share/mount operations failed */ 6697 } share_mount_state_t; 6698 6699 /* 6700 * Share or mount a dataset. 6701 */ 6702 static int 6703 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, 6704 boolean_t explicit, const char *options) 6705 { 6706 char mountpoint[ZFS_MAXPROPLEN]; 6707 char shareopts[ZFS_MAXPROPLEN]; 6708 char smbshareopts[ZFS_MAXPROPLEN]; 6709 const char *cmdname = op == OP_SHARE ? "share" : "mount"; 6710 struct mnttab mnt; 6711 uint64_t zoned, canmount; 6712 boolean_t shared_nfs, shared_smb; 6713 6714 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM); 6715 6716 /* 6717 * Check to make sure we can mount/share this dataset. If we 6718 * are in the global zone and the filesystem is exported to a 6719 * local zone, or if we are in a local zone and the 6720 * filesystem is not exported, then it is an error. 6721 */ 6722 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 6723 6724 if (zoned && getzoneid() == GLOBAL_ZONEID) { 6725 if (!explicit) 6726 return (0); 6727 6728 (void) fprintf(stderr, gettext("cannot %s '%s': " 6729 "dataset is exported to a local zone\n"), cmdname, 6730 zfs_get_name(zhp)); 6731 return (1); 6732 6733 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 6734 if (!explicit) 6735 return (0); 6736 6737 (void) fprintf(stderr, gettext("cannot %s '%s': " 6738 "permission denied\n"), cmdname, 6739 zfs_get_name(zhp)); 6740 return (1); 6741 } 6742 6743 /* 6744 * Ignore any filesystems which don't apply to us. This 6745 * includes those with a legacy mountpoint, or those with 6746 * legacy share options. 6747 */ 6748 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 6749 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 6750 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 6751 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 6752 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts, 6753 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0); 6754 6755 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 && 6756 strcmp(smbshareopts, "off") == 0) { 6757 if (!explicit) 6758 return (0); 6759 6760 (void) fprintf(stderr, gettext("cannot share '%s': " 6761 "legacy share\n"), zfs_get_name(zhp)); 6762 (void) fprintf(stderr, gettext("use exports(5) or " 6763 "smb.conf(5) to share this filesystem, or set " 6764 "the sharenfs or sharesmb property\n")); 6765 return (1); 6766 } 6767 6768 /* 6769 * We cannot share or mount legacy filesystems. If the 6770 * shareopts is non-legacy but the mountpoint is legacy, we 6771 * treat it as a legacy share. 6772 */ 6773 if (strcmp(mountpoint, "legacy") == 0) { 6774 if (!explicit) 6775 return (0); 6776 6777 (void) fprintf(stderr, gettext("cannot %s '%s': " 6778 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 6779 (void) fprintf(stderr, gettext("use %s(8) to " 6780 "%s this filesystem\n"), cmdname, cmdname); 6781 return (1); 6782 } 6783 6784 if (strcmp(mountpoint, "none") == 0) { 6785 if (!explicit) 6786 return (0); 6787 6788 (void) fprintf(stderr, gettext("cannot %s '%s': no " 6789 "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 6790 return (1); 6791 } 6792 6793 /* 6794 * canmount explicit outcome 6795 * on no pass through 6796 * on yes pass through 6797 * off no return 0 6798 * off yes display error, return 1 6799 * noauto no return 0 6800 * noauto yes pass through 6801 */ 6802 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 6803 if (canmount == ZFS_CANMOUNT_OFF) { 6804 if (!explicit) 6805 return (0); 6806 6807 (void) fprintf(stderr, gettext("cannot %s '%s': " 6808 "'canmount' property is set to 'off'\n"), cmdname, 6809 zfs_get_name(zhp)); 6810 return (1); 6811 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) { 6812 /* 6813 * When performing a 'zfs mount -a', we skip any mounts for 6814 * datasets that have 'noauto' set. Sharing a dataset with 6815 * 'noauto' set is only allowed if it's mounted. 6816 */ 6817 if (op == OP_MOUNT) 6818 return (0); 6819 if (op == OP_SHARE && !zfs_is_mounted(zhp, NULL)) { 6820 /* also purge it from existing exports */ 6821 zfs_unshareall_bypath(zhp, mountpoint); 6822 return (0); 6823 } 6824 } 6825 6826 /* 6827 * If this filesystem is encrypted and does not have 6828 * a loaded key, we can not mount it. 6829 */ 6830 if ((flags & MS_CRYPT) == 0 && 6831 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF && 6832 zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) == 6833 ZFS_KEYSTATUS_UNAVAILABLE) { 6834 if (!explicit) 6835 return (0); 6836 6837 (void) fprintf(stderr, gettext("cannot %s '%s': " 6838 "encryption key not loaded\n"), cmdname, zfs_get_name(zhp)); 6839 return (1); 6840 } 6841 6842 /* 6843 * If this filesystem is inconsistent and has a receive resume 6844 * token, we can not mount it. 6845 */ 6846 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) && 6847 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 6848 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) { 6849 if (!explicit) 6850 return (0); 6851 6852 (void) fprintf(stderr, gettext("cannot %s '%s': " 6853 "Contains partially-completed state from " 6854 "\"zfs receive -s\", which can be resumed with " 6855 "\"zfs send -t\"\n"), 6856 cmdname, zfs_get_name(zhp)); 6857 return (1); 6858 } 6859 6860 if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE)) { 6861 if (!explicit) 6862 return (0); 6863 6864 (void) fprintf(stderr, gettext("cannot %s '%s': " 6865 "Dataset is not complete, was created by receiving " 6866 "a redacted zfs send stream.\n"), cmdname, 6867 zfs_get_name(zhp)); 6868 return (1); 6869 } 6870 6871 /* 6872 * At this point, we have verified that the mountpoint and/or 6873 * shareopts are appropriate for auto management. If the 6874 * filesystem is already mounted or shared, return (failing 6875 * for explicit requests); otherwise mount or share the 6876 * filesystem. 6877 */ 6878 switch (op) { 6879 case OP_SHARE: 6880 6881 shared_nfs = zfs_is_shared_nfs(zhp, NULL); 6882 shared_smb = zfs_is_shared_smb(zhp, NULL); 6883 6884 if ((shared_nfs && shared_smb) || 6885 (shared_nfs && strcmp(shareopts, "on") == 0 && 6886 strcmp(smbshareopts, "off") == 0) || 6887 (shared_smb && strcmp(smbshareopts, "on") == 0 && 6888 strcmp(shareopts, "off") == 0)) { 6889 if (!explicit) 6890 return (0); 6891 6892 (void) fprintf(stderr, gettext("cannot share " 6893 "'%s': filesystem already shared\n"), 6894 zfs_get_name(zhp)); 6895 return (1); 6896 } 6897 6898 if (!zfs_is_mounted(zhp, NULL) && 6899 zfs_mount(zhp, NULL, flags) != 0) 6900 return (1); 6901 6902 if (protocol == NULL) { 6903 if (zfs_shareall(zhp) != 0) 6904 return (1); 6905 } else if (strcmp(protocol, "nfs") == 0) { 6906 if (zfs_share_nfs(zhp)) 6907 return (1); 6908 } else if (strcmp(protocol, "smb") == 0) { 6909 if (zfs_share_smb(zhp)) 6910 return (1); 6911 } else { 6912 (void) fprintf(stderr, gettext("cannot share " 6913 "'%s': invalid share type '%s' " 6914 "specified\n"), 6915 zfs_get_name(zhp), protocol); 6916 return (1); 6917 } 6918 6919 break; 6920 6921 case OP_MOUNT: 6922 if (options == NULL) 6923 mnt.mnt_mntopts = ""; 6924 else 6925 mnt.mnt_mntopts = (char *)options; 6926 6927 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 6928 zfs_is_mounted(zhp, NULL)) { 6929 if (!explicit) 6930 return (0); 6931 6932 (void) fprintf(stderr, gettext("cannot mount " 6933 "'%s': filesystem already mounted\n"), 6934 zfs_get_name(zhp)); 6935 return (1); 6936 } 6937 6938 if (zfs_mount(zhp, options, flags) != 0) 6939 return (1); 6940 break; 6941 } 6942 6943 return (0); 6944 } 6945 6946 /* 6947 * Reports progress in the form "(current/total)". Not thread-safe. 6948 */ 6949 static void 6950 report_mount_progress(int current, int total) 6951 { 6952 static time_t last_progress_time = 0; 6953 time_t now = time(NULL); 6954 char info[32]; 6955 6956 /* display header if we're here for the first time */ 6957 if (current == 1) { 6958 set_progress_header(gettext("Mounting ZFS filesystems")); 6959 } else if (current != total && last_progress_time + MOUNT_TIME >= now) { 6960 /* too soon to report again */ 6961 return; 6962 } 6963 6964 last_progress_time = now; 6965 6966 (void) sprintf(info, "(%d/%d)", current, total); 6967 6968 if (current == total) 6969 finish_progress(info); 6970 else 6971 update_progress(info); 6972 } 6973 6974 /* 6975 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and 6976 * updates the progress meter. 6977 */ 6978 static int 6979 share_mount_one_cb(zfs_handle_t *zhp, void *arg) 6980 { 6981 share_mount_state_t *sms = arg; 6982 int ret; 6983 6984 ret = share_mount_one(zhp, sms->sm_op, sms->sm_flags, sms->sm_proto, 6985 B_FALSE, sms->sm_options); 6986 6987 pthread_mutex_lock(&sms->sm_lock); 6988 if (ret != 0) 6989 sms->sm_status = ret; 6990 sms->sm_done++; 6991 if (sms->sm_verbose) 6992 report_mount_progress(sms->sm_done, sms->sm_total); 6993 pthread_mutex_unlock(&sms->sm_lock); 6994 return (ret); 6995 } 6996 6997 static void 6998 append_options(char *mntopts, char *newopts) 6999 { 7000 int len = strlen(mntopts); 7001 7002 /* original length plus new string to append plus 1 for the comma */ 7003 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) { 7004 (void) fprintf(stderr, gettext("the opts argument for " 7005 "'%s' option is too long (more than %d chars)\n"), 7006 "-o", MNT_LINE_MAX); 7007 usage(B_FALSE); 7008 } 7009 7010 if (*mntopts) 7011 mntopts[len++] = ','; 7012 7013 (void) strcpy(&mntopts[len], newopts); 7014 } 7015 7016 static int 7017 share_mount(int op, int argc, char **argv) 7018 { 7019 int do_all = 0; 7020 boolean_t verbose = B_FALSE; 7021 int c, ret = 0; 7022 char *options = NULL; 7023 int flags = 0; 7024 7025 /* check options */ 7026 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:Of" : "al")) 7027 != -1) { 7028 switch (c) { 7029 case 'a': 7030 do_all = 1; 7031 break; 7032 case 'v': 7033 verbose = B_TRUE; 7034 break; 7035 case 'l': 7036 flags |= MS_CRYPT; 7037 break; 7038 case 'o': 7039 if (*optarg == '\0') { 7040 (void) fprintf(stderr, gettext("empty mount " 7041 "options (-o) specified\n")); 7042 usage(B_FALSE); 7043 } 7044 7045 if (options == NULL) 7046 options = safe_malloc(MNT_LINE_MAX + 1); 7047 7048 /* option validation is done later */ 7049 append_options(options, optarg); 7050 break; 7051 case 'O': 7052 flags |= MS_OVERLAY; 7053 break; 7054 case 'f': 7055 flags |= MS_FORCE; 7056 break; 7057 case ':': 7058 (void) fprintf(stderr, gettext("missing argument for " 7059 "'%c' option\n"), optopt); 7060 usage(B_FALSE); 7061 break; 7062 case '?': 7063 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7064 optopt); 7065 usage(B_FALSE); 7066 } 7067 } 7068 7069 argc -= optind; 7070 argv += optind; 7071 7072 /* check number of arguments */ 7073 if (do_all) { 7074 char *protocol = NULL; 7075 7076 if (op == OP_SHARE && argc > 0) { 7077 if (strcmp(argv[0], "nfs") != 0 && 7078 strcmp(argv[0], "smb") != 0) { 7079 (void) fprintf(stderr, gettext("share type " 7080 "must be 'nfs' or 'smb'\n")); 7081 usage(B_FALSE); 7082 } 7083 protocol = argv[0]; 7084 argc--; 7085 argv++; 7086 } 7087 7088 if (argc != 0) { 7089 (void) fprintf(stderr, gettext("too many arguments\n")); 7090 usage(B_FALSE); 7091 } 7092 7093 start_progress_timer(); 7094 get_all_cb_t cb = { 0 }; 7095 get_all_datasets(&cb, verbose); 7096 7097 if (cb.cb_used == 0) { 7098 free(options); 7099 return (0); 7100 } 7101 7102 share_mount_state_t share_mount_state = { 0 }; 7103 share_mount_state.sm_op = op; 7104 share_mount_state.sm_verbose = verbose; 7105 share_mount_state.sm_flags = flags; 7106 share_mount_state.sm_options = options; 7107 share_mount_state.sm_proto = protocol; 7108 share_mount_state.sm_total = cb.cb_used; 7109 pthread_mutex_init(&share_mount_state.sm_lock, NULL); 7110 7111 /* 7112 * libshare isn't mt-safe, so only do the operation in parallel 7113 * if we're mounting. Additionally, the key-loading option must 7114 * be serialized so that we can prompt the user for their keys 7115 * in a consistent manner. 7116 */ 7117 zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used, 7118 share_mount_one_cb, &share_mount_state, 7119 op == OP_MOUNT && !(flags & MS_CRYPT)); 7120 zfs_commit_all_shares(); 7121 7122 ret = share_mount_state.sm_status; 7123 7124 for (int i = 0; i < cb.cb_used; i++) 7125 zfs_close(cb.cb_handles[i]); 7126 free(cb.cb_handles); 7127 } else if (argc == 0) { 7128 FILE *mnttab; 7129 struct mnttab entry; 7130 7131 if ((op == OP_SHARE) || (options != NULL)) { 7132 (void) fprintf(stderr, gettext("missing filesystem " 7133 "argument (specify -a for all)\n")); 7134 usage(B_FALSE); 7135 } 7136 7137 /* 7138 * When mount is given no arguments, go through 7139 * /proc/self/mounts and display any active ZFS mounts. 7140 * We hide any snapshots, since they are controlled 7141 * automatically. 7142 */ 7143 7144 if ((mnttab = fopen(MNTTAB, "re")) == NULL) { 7145 free(options); 7146 return (ENOENT); 7147 } 7148 7149 while (getmntent(mnttab, &entry) == 0) { 7150 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 7151 strchr(entry.mnt_special, '@') != NULL) 7152 continue; 7153 7154 (void) printf("%-30s %s\n", entry.mnt_special, 7155 entry.mnt_mountp); 7156 } 7157 7158 (void) fclose(mnttab); 7159 } else { 7160 zfs_handle_t *zhp; 7161 7162 if (argc > 1) { 7163 (void) fprintf(stderr, 7164 gettext("too many arguments\n")); 7165 usage(B_FALSE); 7166 } 7167 7168 if ((zhp = zfs_open(g_zfs, argv[0], 7169 ZFS_TYPE_FILESYSTEM)) == NULL) { 7170 ret = 1; 7171 } else { 7172 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE, 7173 options); 7174 zfs_commit_all_shares(); 7175 zfs_close(zhp); 7176 } 7177 } 7178 7179 free(options); 7180 return (ret); 7181 } 7182 7183 /* 7184 * zfs mount -a [nfs] 7185 * zfs mount filesystem 7186 * 7187 * Mount all filesystems, or mount the given filesystem. 7188 */ 7189 static int 7190 zfs_do_mount(int argc, char **argv) 7191 { 7192 return (share_mount(OP_MOUNT, argc, argv)); 7193 } 7194 7195 /* 7196 * zfs share -a [nfs | smb] 7197 * zfs share filesystem 7198 * 7199 * Share all filesystems, or share the given filesystem. 7200 */ 7201 static int 7202 zfs_do_share(int argc, char **argv) 7203 { 7204 return (share_mount(OP_SHARE, argc, argv)); 7205 } 7206 7207 typedef struct unshare_unmount_node { 7208 zfs_handle_t *un_zhp; 7209 char *un_mountp; 7210 uu_avl_node_t un_avlnode; 7211 } unshare_unmount_node_t; 7212 7213 /* ARGSUSED */ 7214 static int 7215 unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 7216 { 7217 const unshare_unmount_node_t *l = larg; 7218 const unshare_unmount_node_t *r = rarg; 7219 7220 return (strcmp(l->un_mountp, r->un_mountp)); 7221 } 7222 7223 /* 7224 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 7225 * absolute path, find the entry /proc/self/mounts, verify that it's a 7226 * ZFS filesystem, and unmount it appropriately. 7227 */ 7228 static int 7229 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) 7230 { 7231 zfs_handle_t *zhp; 7232 int ret = 0; 7233 struct stat64 statbuf; 7234 struct extmnttab entry; 7235 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount"; 7236 ino_t path_inode; 7237 7238 /* 7239 * Search for the given (major,minor) pair in the mount table. 7240 */ 7241 7242 if (getextmntent(path, &entry, &statbuf) != 0) { 7243 if (op == OP_SHARE) { 7244 (void) fprintf(stderr, gettext("cannot %s '%s': not " 7245 "currently mounted\n"), cmdname, path); 7246 return (1); 7247 } 7248 (void) fprintf(stderr, gettext("warning: %s not in" 7249 "/proc/self/mounts\n"), path); 7250 if ((ret = umount2(path, flags)) != 0) 7251 (void) fprintf(stderr, gettext("%s: %s\n"), path, 7252 strerror(errno)); 7253 return (ret != 0); 7254 } 7255 path_inode = statbuf.st_ino; 7256 7257 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 7258 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 7259 "filesystem\n"), cmdname, path); 7260 return (1); 7261 } 7262 7263 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 7264 ZFS_TYPE_FILESYSTEM)) == NULL) 7265 return (1); 7266 7267 ret = 1; 7268 if (stat64(entry.mnt_mountp, &statbuf) != 0) { 7269 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 7270 cmdname, path, strerror(errno)); 7271 goto out; 7272 } else if (statbuf.st_ino != path_inode) { 7273 (void) fprintf(stderr, gettext("cannot " 7274 "%s '%s': not a mountpoint\n"), cmdname, path); 7275 goto out; 7276 } 7277 7278 if (op == OP_SHARE) { 7279 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 7280 char smbshare_prop[ZFS_MAXPROPLEN]; 7281 7282 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop, 7283 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0); 7284 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop, 7285 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0); 7286 7287 if (strcmp(nfs_mnt_prop, "off") == 0 && 7288 strcmp(smbshare_prop, "off") == 0) { 7289 (void) fprintf(stderr, gettext("cannot unshare " 7290 "'%s': legacy share\n"), path); 7291 (void) fprintf(stderr, gettext("use exportfs(8) " 7292 "or smbcontrol(1) to unshare this filesystem\n")); 7293 } else if (!zfs_is_shared(zhp)) { 7294 (void) fprintf(stderr, gettext("cannot unshare '%s': " 7295 "not currently shared\n"), path); 7296 } else { 7297 ret = zfs_unshareall_bypath(zhp, path); 7298 zfs_commit_all_shares(); 7299 } 7300 } else { 7301 char mtpt_prop[ZFS_MAXPROPLEN]; 7302 7303 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop, 7304 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0); 7305 7306 if (is_manual) { 7307 ret = zfs_unmount(zhp, NULL, flags); 7308 } else if (strcmp(mtpt_prop, "legacy") == 0) { 7309 (void) fprintf(stderr, gettext("cannot unmount " 7310 "'%s': legacy mountpoint\n"), 7311 zfs_get_name(zhp)); 7312 (void) fprintf(stderr, gettext("use umount(8) " 7313 "to unmount this filesystem\n")); 7314 } else { 7315 ret = zfs_unmountall(zhp, flags); 7316 } 7317 } 7318 7319 out: 7320 zfs_close(zhp); 7321 7322 return (ret != 0); 7323 } 7324 7325 /* 7326 * Generic callback for unsharing or unmounting a filesystem. 7327 */ 7328 static int 7329 unshare_unmount(int op, int argc, char **argv) 7330 { 7331 int do_all = 0; 7332 int flags = 0; 7333 int ret = 0; 7334 int c; 7335 zfs_handle_t *zhp; 7336 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 7337 char sharesmb[ZFS_MAXPROPLEN]; 7338 7339 /* check options */ 7340 while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "afu")) != -1) { 7341 switch (c) { 7342 case 'a': 7343 do_all = 1; 7344 break; 7345 case 'f': 7346 flags |= MS_FORCE; 7347 break; 7348 case 'u': 7349 flags |= MS_CRYPT; 7350 break; 7351 case ':': 7352 (void) fprintf(stderr, gettext("missing argument for " 7353 "'%c' option\n"), optopt); 7354 usage(B_FALSE); 7355 break; 7356 case '?': 7357 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7358 optopt); 7359 usage(B_FALSE); 7360 } 7361 } 7362 7363 argc -= optind; 7364 argv += optind; 7365 7366 if (do_all) { 7367 /* 7368 * We could make use of zfs_for_each() to walk all datasets in 7369 * the system, but this would be very inefficient, especially 7370 * since we would have to linearly search /proc/self/mounts for 7371 * each one. Instead, do one pass through /proc/self/mounts 7372 * looking for zfs entries and call zfs_unmount() for each one. 7373 * 7374 * Things get a little tricky if the administrator has created 7375 * mountpoints beneath other ZFS filesystems. In this case, we 7376 * have to unmount the deepest filesystems first. To accomplish 7377 * this, we place all the mountpoints in an AVL tree sorted by 7378 * the special type (dataset name), and walk the result in 7379 * reverse to make sure to get any snapshots first. 7380 */ 7381 FILE *mnttab; 7382 struct mnttab entry; 7383 uu_avl_pool_t *pool; 7384 uu_avl_t *tree = NULL; 7385 unshare_unmount_node_t *node; 7386 uu_avl_index_t idx; 7387 uu_avl_walk_t *walk; 7388 char *protocol = NULL; 7389 7390 if (op == OP_SHARE && argc > 0) { 7391 if (strcmp(argv[0], "nfs") != 0 && 7392 strcmp(argv[0], "smb") != 0) { 7393 (void) fprintf(stderr, gettext("share type " 7394 "must be 'nfs' or 'smb'\n")); 7395 usage(B_FALSE); 7396 } 7397 protocol = argv[0]; 7398 argc--; 7399 argv++; 7400 } 7401 7402 if (argc != 0) { 7403 (void) fprintf(stderr, gettext("too many arguments\n")); 7404 usage(B_FALSE); 7405 } 7406 7407 if (((pool = uu_avl_pool_create("unmount_pool", 7408 sizeof (unshare_unmount_node_t), 7409 offsetof(unshare_unmount_node_t, un_avlnode), 7410 unshare_unmount_compare, UU_DEFAULT)) == NULL) || 7411 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL)) 7412 nomem(); 7413 7414 if ((mnttab = fopen(MNTTAB, "re")) == NULL) 7415 return (ENOENT); 7416 7417 while (getmntent(mnttab, &entry) == 0) { 7418 7419 /* ignore non-ZFS entries */ 7420 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 7421 continue; 7422 7423 /* ignore snapshots */ 7424 if (strchr(entry.mnt_special, '@') != NULL) 7425 continue; 7426 7427 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 7428 ZFS_TYPE_FILESYSTEM)) == NULL) { 7429 ret = 1; 7430 continue; 7431 } 7432 7433 /* 7434 * Ignore datasets that are excluded/restricted by 7435 * parent pool name. 7436 */ 7437 if (zpool_skip_pool(zfs_get_pool_name(zhp))) { 7438 zfs_close(zhp); 7439 continue; 7440 } 7441 7442 switch (op) { 7443 case OP_SHARE: 7444 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 7445 nfs_mnt_prop, 7446 sizeof (nfs_mnt_prop), 7447 NULL, NULL, 0, B_FALSE) == 0); 7448 if (strcmp(nfs_mnt_prop, "off") != 0) 7449 break; 7450 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 7451 nfs_mnt_prop, 7452 sizeof (nfs_mnt_prop), 7453 NULL, NULL, 0, B_FALSE) == 0); 7454 if (strcmp(nfs_mnt_prop, "off") == 0) 7455 continue; 7456 break; 7457 case OP_MOUNT: 7458 /* Ignore legacy mounts */ 7459 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, 7460 nfs_mnt_prop, 7461 sizeof (nfs_mnt_prop), 7462 NULL, NULL, 0, B_FALSE) == 0); 7463 if (strcmp(nfs_mnt_prop, "legacy") == 0) 7464 continue; 7465 /* Ignore canmount=noauto mounts */ 7466 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == 7467 ZFS_CANMOUNT_NOAUTO) 7468 continue; 7469 break; 7470 default: 7471 break; 7472 } 7473 7474 node = safe_malloc(sizeof (unshare_unmount_node_t)); 7475 node->un_zhp = zhp; 7476 node->un_mountp = safe_strdup(entry.mnt_mountp); 7477 7478 uu_avl_node_init(node, &node->un_avlnode, pool); 7479 7480 if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 7481 uu_avl_insert(tree, node, idx); 7482 } else { 7483 zfs_close(node->un_zhp); 7484 free(node->un_mountp); 7485 free(node); 7486 } 7487 } 7488 (void) fclose(mnttab); 7489 7490 /* 7491 * Walk the AVL tree in reverse, unmounting each filesystem and 7492 * removing it from the AVL tree in the process. 7493 */ 7494 if ((walk = uu_avl_walk_start(tree, 7495 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) 7496 nomem(); 7497 7498 while ((node = uu_avl_walk_next(walk)) != NULL) { 7499 const char *mntarg = NULL; 7500 7501 uu_avl_remove(tree, node); 7502 switch (op) { 7503 case OP_SHARE: 7504 if (zfs_unshareall_bytype(node->un_zhp, 7505 node->un_mountp, protocol) != 0) 7506 ret = 1; 7507 break; 7508 7509 case OP_MOUNT: 7510 if (zfs_unmount(node->un_zhp, 7511 mntarg, flags) != 0) 7512 ret = 1; 7513 break; 7514 } 7515 7516 zfs_close(node->un_zhp); 7517 free(node->un_mountp); 7518 free(node); 7519 } 7520 7521 if (op == OP_SHARE) 7522 zfs_commit_shares(protocol); 7523 7524 uu_avl_walk_end(walk); 7525 uu_avl_destroy(tree); 7526 uu_avl_pool_destroy(pool); 7527 7528 } else { 7529 if (argc != 1) { 7530 if (argc == 0) 7531 (void) fprintf(stderr, 7532 gettext("missing filesystem argument\n")); 7533 else 7534 (void) fprintf(stderr, 7535 gettext("too many arguments\n")); 7536 usage(B_FALSE); 7537 } 7538 7539 /* 7540 * We have an argument, but it may be a full path or a ZFS 7541 * filesystem. Pass full paths off to unmount_path() (shared by 7542 * manual_unmount), otherwise open the filesystem and pass to 7543 * zfs_unmount(). 7544 */ 7545 if (argv[0][0] == '/') 7546 return (unshare_unmount_path(op, argv[0], 7547 flags, B_FALSE)); 7548 7549 if ((zhp = zfs_open(g_zfs, argv[0], 7550 ZFS_TYPE_FILESYSTEM)) == NULL) 7551 return (1); 7552 7553 verify(zfs_prop_get(zhp, op == OP_SHARE ? 7554 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 7555 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL, 7556 NULL, 0, B_FALSE) == 0); 7557 7558 switch (op) { 7559 case OP_SHARE: 7560 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 7561 nfs_mnt_prop, 7562 sizeof (nfs_mnt_prop), 7563 NULL, NULL, 0, B_FALSE) == 0); 7564 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 7565 sharesmb, sizeof (sharesmb), NULL, NULL, 7566 0, B_FALSE) == 0); 7567 7568 if (strcmp(nfs_mnt_prop, "off") == 0 && 7569 strcmp(sharesmb, "off") == 0) { 7570 (void) fprintf(stderr, gettext("cannot " 7571 "unshare '%s': legacy share\n"), 7572 zfs_get_name(zhp)); 7573 (void) fprintf(stderr, gettext("use " 7574 "exports(5) or smb.conf(5) to unshare " 7575 "this filesystem\n")); 7576 ret = 1; 7577 } else if (!zfs_is_shared(zhp)) { 7578 (void) fprintf(stderr, gettext("cannot " 7579 "unshare '%s': not currently " 7580 "shared\n"), zfs_get_name(zhp)); 7581 ret = 1; 7582 } else if (zfs_unshareall(zhp) != 0) { 7583 ret = 1; 7584 } 7585 break; 7586 7587 case OP_MOUNT: 7588 if (strcmp(nfs_mnt_prop, "legacy") == 0) { 7589 (void) fprintf(stderr, gettext("cannot " 7590 "unmount '%s': legacy " 7591 "mountpoint\n"), zfs_get_name(zhp)); 7592 (void) fprintf(stderr, gettext("use " 7593 "umount(8) to unmount this " 7594 "filesystem\n")); 7595 ret = 1; 7596 } else if (!zfs_is_mounted(zhp, NULL)) { 7597 (void) fprintf(stderr, gettext("cannot " 7598 "unmount '%s': not currently " 7599 "mounted\n"), 7600 zfs_get_name(zhp)); 7601 ret = 1; 7602 } else if (zfs_unmountall(zhp, flags) != 0) { 7603 ret = 1; 7604 } 7605 break; 7606 } 7607 7608 zfs_close(zhp); 7609 } 7610 7611 return (ret); 7612 } 7613 7614 /* 7615 * zfs unmount [-fu] -a 7616 * zfs unmount [-fu] filesystem 7617 * 7618 * Unmount all filesystems, or a specific ZFS filesystem. 7619 */ 7620 static int 7621 zfs_do_unmount(int argc, char **argv) 7622 { 7623 return (unshare_unmount(OP_MOUNT, argc, argv)); 7624 } 7625 7626 /* 7627 * zfs unshare -a 7628 * zfs unshare filesystem 7629 * 7630 * Unshare all filesystems, or a specific ZFS filesystem. 7631 */ 7632 static int 7633 zfs_do_unshare(int argc, char **argv) 7634 { 7635 return (unshare_unmount(OP_SHARE, argc, argv)); 7636 } 7637 7638 static int 7639 find_command_idx(char *command, int *idx) 7640 { 7641 int i; 7642 7643 for (i = 0; i < NCOMMAND; i++) { 7644 if (command_table[i].name == NULL) 7645 continue; 7646 7647 if (strcmp(command, command_table[i].name) == 0) { 7648 *idx = i; 7649 return (0); 7650 } 7651 } 7652 return (1); 7653 } 7654 7655 static int 7656 zfs_do_diff(int argc, char **argv) 7657 { 7658 zfs_handle_t *zhp; 7659 int flags = 0; 7660 char *tosnap = NULL; 7661 char *fromsnap = NULL; 7662 char *atp, *copy; 7663 int err = 0; 7664 int c; 7665 struct sigaction sa; 7666 7667 while ((c = getopt(argc, argv, "FHt")) != -1) { 7668 switch (c) { 7669 case 'F': 7670 flags |= ZFS_DIFF_CLASSIFY; 7671 break; 7672 case 'H': 7673 flags |= ZFS_DIFF_PARSEABLE; 7674 break; 7675 case 't': 7676 flags |= ZFS_DIFF_TIMESTAMP; 7677 break; 7678 default: 7679 (void) fprintf(stderr, 7680 gettext("invalid option '%c'\n"), optopt); 7681 usage(B_FALSE); 7682 } 7683 } 7684 7685 argc -= optind; 7686 argv += optind; 7687 7688 if (argc < 1) { 7689 (void) fprintf(stderr, 7690 gettext("must provide at least one snapshot name\n")); 7691 usage(B_FALSE); 7692 } 7693 7694 if (argc > 2) { 7695 (void) fprintf(stderr, gettext("too many arguments\n")); 7696 usage(B_FALSE); 7697 } 7698 7699 fromsnap = argv[0]; 7700 tosnap = (argc == 2) ? argv[1] : NULL; 7701 7702 copy = NULL; 7703 if (*fromsnap != '@') 7704 copy = strdup(fromsnap); 7705 else if (tosnap) 7706 copy = strdup(tosnap); 7707 if (copy == NULL) 7708 usage(B_FALSE); 7709 7710 if ((atp = strchr(copy, '@')) != NULL) 7711 *atp = '\0'; 7712 7713 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) { 7714 free(copy); 7715 return (1); 7716 } 7717 free(copy); 7718 7719 /* 7720 * Ignore SIGPIPE so that the library can give us 7721 * information on any failure 7722 */ 7723 if (sigemptyset(&sa.sa_mask) == -1) { 7724 err = errno; 7725 goto out; 7726 } 7727 sa.sa_flags = 0; 7728 sa.sa_handler = SIG_IGN; 7729 if (sigaction(SIGPIPE, &sa, NULL) == -1) { 7730 err = errno; 7731 goto out; 7732 } 7733 7734 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags); 7735 out: 7736 zfs_close(zhp); 7737 7738 return (err != 0); 7739 } 7740 7741 /* 7742 * zfs bookmark <fs@source>|<fs#source> <fs#bookmark> 7743 * 7744 * Creates a bookmark with the given name from the source snapshot 7745 * or creates a copy of an existing source bookmark. 7746 */ 7747 static int 7748 zfs_do_bookmark(int argc, char **argv) 7749 { 7750 char *source, *bookname; 7751 char expbuf[ZFS_MAX_DATASET_NAME_LEN]; 7752 int source_type; 7753 nvlist_t *nvl; 7754 int ret = 0; 7755 int c; 7756 7757 /* check options */ 7758 while ((c = getopt(argc, argv, "")) != -1) { 7759 switch (c) { 7760 case '?': 7761 (void) fprintf(stderr, 7762 gettext("invalid option '%c'\n"), optopt); 7763 goto usage; 7764 } 7765 } 7766 7767 argc -= optind; 7768 argv += optind; 7769 7770 /* check number of arguments */ 7771 if (argc < 1) { 7772 (void) fprintf(stderr, gettext("missing source argument\n")); 7773 goto usage; 7774 } 7775 if (argc < 2) { 7776 (void) fprintf(stderr, gettext("missing bookmark argument\n")); 7777 goto usage; 7778 } 7779 7780 source = argv[0]; 7781 bookname = argv[1]; 7782 7783 if (strchr(source, '@') == NULL && strchr(source, '#') == NULL) { 7784 (void) fprintf(stderr, 7785 gettext("invalid source name '%s': " 7786 "must contain a '@' or '#'\n"), source); 7787 goto usage; 7788 } 7789 if (strchr(bookname, '#') == NULL) { 7790 (void) fprintf(stderr, 7791 gettext("invalid bookmark name '%s': " 7792 "must contain a '#'\n"), bookname); 7793 goto usage; 7794 } 7795 7796 /* 7797 * expand source or bookname to full path: 7798 * one of them may be specified as short name 7799 */ 7800 { 7801 char **expand; 7802 char *source_short, *bookname_short; 7803 source_short = strpbrk(source, "@#"); 7804 bookname_short = strpbrk(bookname, "#"); 7805 if (source_short == source && 7806 bookname_short == bookname) { 7807 (void) fprintf(stderr, gettext( 7808 "either source or bookmark must be specified as " 7809 "full dataset paths")); 7810 goto usage; 7811 } else if (source_short != source && 7812 bookname_short != bookname) { 7813 expand = NULL; 7814 } else if (source_short != source) { 7815 strlcpy(expbuf, source, sizeof (expbuf)); 7816 expand = &bookname; 7817 } else if (bookname_short != bookname) { 7818 strlcpy(expbuf, bookname, sizeof (expbuf)); 7819 expand = &source; 7820 } else { 7821 abort(); 7822 } 7823 if (expand != NULL) { 7824 *strpbrk(expbuf, "@#") = '\0'; /* dataset name in buf */ 7825 (void) strlcat(expbuf, *expand, sizeof (expbuf)); 7826 *expand = expbuf; 7827 } 7828 } 7829 7830 /* determine source type */ 7831 switch (*strpbrk(source, "@#")) { 7832 case '@': source_type = ZFS_TYPE_SNAPSHOT; break; 7833 case '#': source_type = ZFS_TYPE_BOOKMARK; break; 7834 default: abort(); 7835 } 7836 7837 /* test the source exists */ 7838 zfs_handle_t *zhp; 7839 zhp = zfs_open(g_zfs, source, source_type); 7840 if (zhp == NULL) 7841 goto usage; 7842 zfs_close(zhp); 7843 7844 nvl = fnvlist_alloc(); 7845 fnvlist_add_string(nvl, bookname, source); 7846 ret = lzc_bookmark(nvl, NULL); 7847 fnvlist_free(nvl); 7848 7849 if (ret != 0) { 7850 const char *err_msg = NULL; 7851 char errbuf[1024]; 7852 7853 (void) snprintf(errbuf, sizeof (errbuf), 7854 dgettext(TEXT_DOMAIN, 7855 "cannot create bookmark '%s'"), bookname); 7856 7857 switch (ret) { 7858 case EXDEV: 7859 err_msg = "bookmark is in a different pool"; 7860 break; 7861 case ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR: 7862 err_msg = "source is not an ancestor of the " 7863 "new bookmark's dataset"; 7864 break; 7865 case EEXIST: 7866 err_msg = "bookmark exists"; 7867 break; 7868 case EINVAL: 7869 err_msg = "invalid argument"; 7870 break; 7871 case ENOTSUP: 7872 err_msg = "bookmark feature not enabled"; 7873 break; 7874 case ENOSPC: 7875 err_msg = "out of space"; 7876 break; 7877 case ENOENT: 7878 err_msg = "dataset does not exist"; 7879 break; 7880 default: 7881 (void) zfs_standard_error(g_zfs, ret, errbuf); 7882 break; 7883 } 7884 if (err_msg != NULL) { 7885 (void) fprintf(stderr, "%s: %s\n", errbuf, 7886 dgettext(TEXT_DOMAIN, err_msg)); 7887 } 7888 } 7889 7890 return (ret != 0); 7891 7892 usage: 7893 usage(B_FALSE); 7894 return (-1); 7895 } 7896 7897 static int 7898 zfs_do_channel_program(int argc, char **argv) 7899 { 7900 int ret, fd, c; 7901 char *progbuf, *filename, *poolname; 7902 size_t progsize, progread; 7903 nvlist_t *outnvl = NULL; 7904 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT; 7905 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT; 7906 boolean_t sync_flag = B_TRUE, json_output = B_FALSE; 7907 zpool_handle_t *zhp; 7908 7909 /* check options */ 7910 while ((c = getopt(argc, argv, "nt:m:j")) != -1) { 7911 switch (c) { 7912 case 't': 7913 case 'm': { 7914 uint64_t arg; 7915 char *endp; 7916 7917 errno = 0; 7918 arg = strtoull(optarg, &endp, 0); 7919 if (errno != 0 || *endp != '\0') { 7920 (void) fprintf(stderr, gettext( 7921 "invalid argument " 7922 "'%s': expected integer\n"), optarg); 7923 goto usage; 7924 } 7925 7926 if (c == 't') { 7927 instrlimit = arg; 7928 } else { 7929 ASSERT3U(c, ==, 'm'); 7930 memlimit = arg; 7931 } 7932 break; 7933 } 7934 case 'n': { 7935 sync_flag = B_FALSE; 7936 break; 7937 } 7938 case 'j': { 7939 json_output = B_TRUE; 7940 break; 7941 } 7942 case '?': 7943 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 7944 optopt); 7945 goto usage; 7946 } 7947 } 7948 7949 argc -= optind; 7950 argv += optind; 7951 7952 if (argc < 2) { 7953 (void) fprintf(stderr, 7954 gettext("invalid number of arguments\n")); 7955 goto usage; 7956 } 7957 7958 poolname = argv[0]; 7959 filename = argv[1]; 7960 if (strcmp(filename, "-") == 0) { 7961 fd = 0; 7962 filename = "standard input"; 7963 } else if ((fd = open(filename, O_RDONLY)) < 0) { 7964 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"), 7965 filename, strerror(errno)); 7966 return (1); 7967 } 7968 7969 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) { 7970 (void) fprintf(stderr, gettext("cannot open pool '%s'\n"), 7971 poolname); 7972 if (fd != 0) 7973 (void) close(fd); 7974 return (1); 7975 } 7976 zpool_close(zhp); 7977 7978 /* 7979 * Read in the channel program, expanding the program buffer as 7980 * necessary. 7981 */ 7982 progread = 0; 7983 progsize = 1024; 7984 progbuf = safe_malloc(progsize); 7985 do { 7986 ret = read(fd, progbuf + progread, progsize - progread); 7987 progread += ret; 7988 if (progread == progsize && ret > 0) { 7989 progsize *= 2; 7990 progbuf = safe_realloc(progbuf, progsize); 7991 } 7992 } while (ret > 0); 7993 7994 if (fd != 0) 7995 (void) close(fd); 7996 if (ret < 0) { 7997 free(progbuf); 7998 (void) fprintf(stderr, 7999 gettext("cannot read '%s': %s\n"), 8000 filename, strerror(errno)); 8001 return (1); 8002 } 8003 progbuf[progread] = '\0'; 8004 8005 /* 8006 * Any remaining arguments are passed as arguments to the lua script as 8007 * a string array: 8008 * { 8009 * "argv" -> [ "arg 1", ... "arg n" ], 8010 * } 8011 */ 8012 nvlist_t *argnvl = fnvlist_alloc(); 8013 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2); 8014 8015 if (sync_flag) { 8016 ret = lzc_channel_program(poolname, progbuf, 8017 instrlimit, memlimit, argnvl, &outnvl); 8018 } else { 8019 ret = lzc_channel_program_nosync(poolname, progbuf, 8020 instrlimit, memlimit, argnvl, &outnvl); 8021 } 8022 8023 if (ret != 0) { 8024 /* 8025 * On error, report the error message handed back by lua if one 8026 * exists. Otherwise, generate an appropriate error message, 8027 * falling back on strerror() for an unexpected return code. 8028 */ 8029 char *errstring = NULL; 8030 const char *msg = gettext("Channel program execution failed"); 8031 uint64_t instructions = 0; 8032 if (outnvl != NULL && nvlist_exists(outnvl, ZCP_RET_ERROR)) { 8033 (void) nvlist_lookup_string(outnvl, 8034 ZCP_RET_ERROR, &errstring); 8035 if (errstring == NULL) 8036 errstring = strerror(ret); 8037 if (ret == ETIME) { 8038 (void) nvlist_lookup_uint64(outnvl, 8039 ZCP_ARG_INSTRLIMIT, &instructions); 8040 } 8041 } else { 8042 switch (ret) { 8043 case EINVAL: 8044 errstring = 8045 "Invalid instruction or memory limit."; 8046 break; 8047 case ENOMEM: 8048 errstring = "Return value too large."; 8049 break; 8050 case ENOSPC: 8051 errstring = "Memory limit exhausted."; 8052 break; 8053 case ETIME: 8054 errstring = "Timed out."; 8055 break; 8056 case EPERM: 8057 errstring = "Permission denied. Channel " 8058 "programs must be run as root."; 8059 break; 8060 default: 8061 (void) zfs_standard_error(g_zfs, ret, msg); 8062 } 8063 } 8064 if (errstring != NULL) 8065 (void) fprintf(stderr, "%s:\n%s\n", msg, errstring); 8066 8067 if (ret == ETIME && instructions != 0) 8068 (void) fprintf(stderr, 8069 gettext("%llu Lua instructions\n"), 8070 (u_longlong_t)instructions); 8071 } else { 8072 if (json_output) { 8073 (void) nvlist_print_json(stdout, outnvl); 8074 } else if (nvlist_empty(outnvl)) { 8075 (void) fprintf(stdout, gettext("Channel program fully " 8076 "executed and did not produce output.\n")); 8077 } else { 8078 (void) fprintf(stdout, gettext("Channel program fully " 8079 "executed and produced output:\n")); 8080 dump_nvlist(outnvl, 4); 8081 } 8082 } 8083 8084 free(progbuf); 8085 fnvlist_free(outnvl); 8086 fnvlist_free(argnvl); 8087 return (ret != 0); 8088 8089 usage: 8090 usage(B_FALSE); 8091 return (-1); 8092 } 8093 8094 8095 typedef struct loadkey_cbdata { 8096 boolean_t cb_loadkey; 8097 boolean_t cb_recursive; 8098 boolean_t cb_noop; 8099 char *cb_keylocation; 8100 uint64_t cb_numfailed; 8101 uint64_t cb_numattempted; 8102 } loadkey_cbdata_t; 8103 8104 static int 8105 load_key_callback(zfs_handle_t *zhp, void *data) 8106 { 8107 int ret; 8108 boolean_t is_encroot; 8109 loadkey_cbdata_t *cb = data; 8110 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS); 8111 8112 /* 8113 * If we are working recursively, we want to skip loading / unloading 8114 * keys for non-encryption roots and datasets whose keys are already 8115 * in the desired end-state. 8116 */ 8117 if (cb->cb_recursive) { 8118 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL); 8119 if (ret != 0) 8120 return (ret); 8121 if (!is_encroot) 8122 return (0); 8123 8124 if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) || 8125 (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE)) 8126 return (0); 8127 } 8128 8129 cb->cb_numattempted++; 8130 8131 if (cb->cb_loadkey) 8132 ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation); 8133 else 8134 ret = zfs_crypto_unload_key(zhp); 8135 8136 if (ret != 0) { 8137 cb->cb_numfailed++; 8138 return (ret); 8139 } 8140 8141 return (0); 8142 } 8143 8144 static int 8145 load_unload_keys(int argc, char **argv, boolean_t loadkey) 8146 { 8147 int c, ret = 0, flags = 0; 8148 boolean_t do_all = B_FALSE; 8149 loadkey_cbdata_t cb = { 0 }; 8150 8151 cb.cb_loadkey = loadkey; 8152 8153 while ((c = getopt(argc, argv, "anrL:")) != -1) { 8154 /* noop and alternate keylocations only apply to zfs load-key */ 8155 if (loadkey) { 8156 switch (c) { 8157 case 'n': 8158 cb.cb_noop = B_TRUE; 8159 continue; 8160 case 'L': 8161 cb.cb_keylocation = optarg; 8162 continue; 8163 default: 8164 break; 8165 } 8166 } 8167 8168 switch (c) { 8169 case 'a': 8170 do_all = B_TRUE; 8171 cb.cb_recursive = B_TRUE; 8172 break; 8173 case 'r': 8174 flags |= ZFS_ITER_RECURSE; 8175 cb.cb_recursive = B_TRUE; 8176 break; 8177 default: 8178 (void) fprintf(stderr, 8179 gettext("invalid option '%c'\n"), optopt); 8180 usage(B_FALSE); 8181 } 8182 } 8183 8184 argc -= optind; 8185 argv += optind; 8186 8187 if (!do_all && argc == 0) { 8188 (void) fprintf(stderr, 8189 gettext("Missing dataset argument or -a option\n")); 8190 usage(B_FALSE); 8191 } 8192 8193 if (do_all && argc != 0) { 8194 (void) fprintf(stderr, 8195 gettext("Cannot specify dataset with -a option\n")); 8196 usage(B_FALSE); 8197 } 8198 8199 if (cb.cb_recursive && cb.cb_keylocation != NULL && 8200 strcmp(cb.cb_keylocation, "prompt") != 0) { 8201 (void) fprintf(stderr, gettext("alternate keylocation may only " 8202 "be 'prompt' with -r or -a\n")); 8203 usage(B_FALSE); 8204 } 8205 8206 ret = zfs_for_each(argc, argv, flags, 8207 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0, 8208 load_key_callback, &cb); 8209 8210 if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) { 8211 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"), 8212 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed), 8213 (u_longlong_t)cb.cb_numattempted, 8214 loadkey ? (cb.cb_noop ? "verified" : "loaded") : 8215 "unloaded"); 8216 } 8217 8218 if (cb.cb_numfailed != 0) 8219 ret = -1; 8220 8221 return (ret); 8222 } 8223 8224 static int 8225 zfs_do_load_key(int argc, char **argv) 8226 { 8227 return (load_unload_keys(argc, argv, B_TRUE)); 8228 } 8229 8230 8231 static int 8232 zfs_do_unload_key(int argc, char **argv) 8233 { 8234 return (load_unload_keys(argc, argv, B_FALSE)); 8235 } 8236 8237 static int 8238 zfs_do_change_key(int argc, char **argv) 8239 { 8240 int c, ret; 8241 uint64_t keystatus; 8242 boolean_t loadkey = B_FALSE, inheritkey = B_FALSE; 8243 zfs_handle_t *zhp = NULL; 8244 nvlist_t *props = fnvlist_alloc(); 8245 8246 while ((c = getopt(argc, argv, "lio:")) != -1) { 8247 switch (c) { 8248 case 'l': 8249 loadkey = B_TRUE; 8250 break; 8251 case 'i': 8252 inheritkey = B_TRUE; 8253 break; 8254 case 'o': 8255 if (!parseprop(props, optarg)) { 8256 nvlist_free(props); 8257 return (1); 8258 } 8259 break; 8260 default: 8261 (void) fprintf(stderr, 8262 gettext("invalid option '%c'\n"), optopt); 8263 usage(B_FALSE); 8264 } 8265 } 8266 8267 if (inheritkey && !nvlist_empty(props)) { 8268 (void) fprintf(stderr, 8269 gettext("Properties not allowed for inheriting\n")); 8270 usage(B_FALSE); 8271 } 8272 8273 argc -= optind; 8274 argv += optind; 8275 8276 if (argc < 1) { 8277 (void) fprintf(stderr, gettext("Missing dataset argument\n")); 8278 usage(B_FALSE); 8279 } 8280 8281 if (argc > 1) { 8282 (void) fprintf(stderr, gettext("Too many arguments\n")); 8283 usage(B_FALSE); 8284 } 8285 8286 zhp = zfs_open(g_zfs, argv[argc - 1], 8287 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 8288 if (zhp == NULL) 8289 usage(B_FALSE); 8290 8291 if (loadkey) { 8292 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS); 8293 if (keystatus != ZFS_KEYSTATUS_AVAILABLE) { 8294 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL); 8295 if (ret != 0) { 8296 nvlist_free(props); 8297 zfs_close(zhp); 8298 return (-1); 8299 } 8300 } 8301 8302 /* refresh the properties so the new keystatus is visible */ 8303 zfs_refresh_properties(zhp); 8304 } 8305 8306 ret = zfs_crypto_rewrap(zhp, props, inheritkey); 8307 if (ret != 0) { 8308 nvlist_free(props); 8309 zfs_close(zhp); 8310 return (-1); 8311 } 8312 8313 nvlist_free(props); 8314 zfs_close(zhp); 8315 return (0); 8316 } 8317 8318 /* 8319 * 1) zfs project [-d|-r] <file|directory ...> 8320 * List project ID and inherit flag of file(s) or directories. 8321 * -d: List the directory itself, not its children. 8322 * -r: List subdirectories recursively. 8323 * 8324 * 2) zfs project -C [-k] [-r] <file|directory ...> 8325 * Clear project inherit flag and/or ID on the file(s) or directories. 8326 * -k: Keep the project ID unchanged. If not specified, the project ID 8327 * will be reset as zero. 8328 * -r: Clear on subdirectories recursively. 8329 * 8330 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...> 8331 * Check project ID and inherit flag on the file(s) or directories, 8332 * report the outliers. 8333 * -0: Print file name followed by a NUL instead of newline. 8334 * -d: Check the directory itself, not its children. 8335 * -p: Specify the referenced ID for comparing with the target file(s) 8336 * or directories' project IDs. If not specified, the target (top) 8337 * directory's project ID will be used as the referenced one. 8338 * -r: Check subdirectories recursively. 8339 * 8340 * 4) zfs project [-p id] [-r] [-s] <file|directory ...> 8341 * Set project ID and/or inherit flag on the file(s) or directories. 8342 * -p: Set the project ID as the given id. 8343 * -r: Set on subdirectories recursively. If not specify "-p" option, 8344 * it will use top-level directory's project ID as the given id, 8345 * then set both project ID and inherit flag on all descendants 8346 * of the top-level directory. 8347 * -s: Set project inherit flag. 8348 */ 8349 static int 8350 zfs_do_project(int argc, char **argv) 8351 { 8352 zfs_project_control_t zpc = { 8353 .zpc_expected_projid = ZFS_INVALID_PROJID, 8354 .zpc_op = ZFS_PROJECT_OP_DEFAULT, 8355 .zpc_dironly = B_FALSE, 8356 .zpc_keep_projid = B_FALSE, 8357 .zpc_newline = B_TRUE, 8358 .zpc_recursive = B_FALSE, 8359 .zpc_set_flag = B_FALSE, 8360 }; 8361 int ret = 0, c; 8362 8363 if (argc < 2) 8364 usage(B_FALSE); 8365 8366 while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) { 8367 switch (c) { 8368 case '0': 8369 zpc.zpc_newline = B_FALSE; 8370 break; 8371 case 'C': 8372 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) { 8373 (void) fprintf(stderr, gettext("cannot " 8374 "specify '-C' '-c' '-s' together\n")); 8375 usage(B_FALSE); 8376 } 8377 8378 zpc.zpc_op = ZFS_PROJECT_OP_CLEAR; 8379 break; 8380 case 'c': 8381 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) { 8382 (void) fprintf(stderr, gettext("cannot " 8383 "specify '-C' '-c' '-s' together\n")); 8384 usage(B_FALSE); 8385 } 8386 8387 zpc.zpc_op = ZFS_PROJECT_OP_CHECK; 8388 break; 8389 case 'd': 8390 zpc.zpc_dironly = B_TRUE; 8391 /* overwrite "-r" option */ 8392 zpc.zpc_recursive = B_FALSE; 8393 break; 8394 case 'k': 8395 zpc.zpc_keep_projid = B_TRUE; 8396 break; 8397 case 'p': { 8398 char *endptr; 8399 8400 errno = 0; 8401 zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0); 8402 if (errno != 0 || *endptr != '\0') { 8403 (void) fprintf(stderr, 8404 gettext("project ID must be less than " 8405 "%u\n"), UINT32_MAX); 8406 usage(B_FALSE); 8407 } 8408 if (zpc.zpc_expected_projid >= UINT32_MAX) { 8409 (void) fprintf(stderr, 8410 gettext("invalid project ID\n")); 8411 usage(B_FALSE); 8412 } 8413 break; 8414 } 8415 case 'r': 8416 zpc.zpc_recursive = B_TRUE; 8417 /* overwrite "-d" option */ 8418 zpc.zpc_dironly = B_FALSE; 8419 break; 8420 case 's': 8421 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) { 8422 (void) fprintf(stderr, gettext("cannot " 8423 "specify '-C' '-c' '-s' together\n")); 8424 usage(B_FALSE); 8425 } 8426 8427 zpc.zpc_set_flag = B_TRUE; 8428 zpc.zpc_op = ZFS_PROJECT_OP_SET; 8429 break; 8430 default: 8431 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8432 optopt); 8433 usage(B_FALSE); 8434 } 8435 } 8436 8437 if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) { 8438 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) 8439 zpc.zpc_op = ZFS_PROJECT_OP_SET; 8440 else 8441 zpc.zpc_op = ZFS_PROJECT_OP_LIST; 8442 } 8443 8444 switch (zpc.zpc_op) { 8445 case ZFS_PROJECT_OP_LIST: 8446 if (zpc.zpc_keep_projid) { 8447 (void) fprintf(stderr, 8448 gettext("'-k' is only valid together with '-C'\n")); 8449 usage(B_FALSE); 8450 } 8451 if (!zpc.zpc_newline) { 8452 (void) fprintf(stderr, 8453 gettext("'-0' is only valid together with '-c'\n")); 8454 usage(B_FALSE); 8455 } 8456 break; 8457 case ZFS_PROJECT_OP_CHECK: 8458 if (zpc.zpc_keep_projid) { 8459 (void) fprintf(stderr, 8460 gettext("'-k' is only valid together with '-C'\n")); 8461 usage(B_FALSE); 8462 } 8463 break; 8464 case ZFS_PROJECT_OP_CLEAR: 8465 if (zpc.zpc_dironly) { 8466 (void) fprintf(stderr, 8467 gettext("'-d' is useless together with '-C'\n")); 8468 usage(B_FALSE); 8469 } 8470 if (!zpc.zpc_newline) { 8471 (void) fprintf(stderr, 8472 gettext("'-0' is only valid together with '-c'\n")); 8473 usage(B_FALSE); 8474 } 8475 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) { 8476 (void) fprintf(stderr, 8477 gettext("'-p' is useless together with '-C'\n")); 8478 usage(B_FALSE); 8479 } 8480 break; 8481 case ZFS_PROJECT_OP_SET: 8482 if (zpc.zpc_dironly) { 8483 (void) fprintf(stderr, 8484 gettext("'-d' is useless for set project ID and/or " 8485 "inherit flag\n")); 8486 usage(B_FALSE); 8487 } 8488 if (zpc.zpc_keep_projid) { 8489 (void) fprintf(stderr, 8490 gettext("'-k' is only valid together with '-C'\n")); 8491 usage(B_FALSE); 8492 } 8493 if (!zpc.zpc_newline) { 8494 (void) fprintf(stderr, 8495 gettext("'-0' is only valid together with '-c'\n")); 8496 usage(B_FALSE); 8497 } 8498 break; 8499 default: 8500 ASSERT(0); 8501 break; 8502 } 8503 8504 argv += optind; 8505 argc -= optind; 8506 if (argc == 0) { 8507 (void) fprintf(stderr, 8508 gettext("missing file or directory target(s)\n")); 8509 usage(B_FALSE); 8510 } 8511 8512 for (int i = 0; i < argc; i++) { 8513 int err; 8514 8515 err = zfs_project_handle(argv[i], &zpc); 8516 if (err && !ret) 8517 ret = err; 8518 } 8519 8520 return (ret); 8521 } 8522 8523 static int 8524 zfs_do_wait(int argc, char **argv) 8525 { 8526 boolean_t enabled[ZFS_WAIT_NUM_ACTIVITIES]; 8527 int error, i; 8528 int c; 8529 8530 /* By default, wait for all types of activity. */ 8531 for (i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++) 8532 enabled[i] = B_TRUE; 8533 8534 while ((c = getopt(argc, argv, "t:")) != -1) { 8535 switch (c) { 8536 case 't': 8537 { 8538 static char *col_subopts[] = { "deleteq", NULL }; 8539 char *value; 8540 8541 /* Reset activities array */ 8542 bzero(&enabled, sizeof (enabled)); 8543 while (*optarg != '\0') { 8544 int activity = getsubopt(&optarg, col_subopts, 8545 &value); 8546 8547 if (activity < 0) { 8548 (void) fprintf(stderr, 8549 gettext("invalid activity '%s'\n"), 8550 value); 8551 usage(B_FALSE); 8552 } 8553 8554 enabled[activity] = B_TRUE; 8555 } 8556 break; 8557 } 8558 case '?': 8559 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 8560 optopt); 8561 usage(B_FALSE); 8562 } 8563 } 8564 8565 argv += optind; 8566 argc -= optind; 8567 if (argc < 1) { 8568 (void) fprintf(stderr, gettext("missing 'filesystem' " 8569 "argument\n")); 8570 usage(B_FALSE); 8571 } 8572 if (argc > 1) { 8573 (void) fprintf(stderr, gettext("too many arguments\n")); 8574 usage(B_FALSE); 8575 } 8576 8577 zfs_handle_t *zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM); 8578 if (zhp == NULL) 8579 return (1); 8580 8581 for (;;) { 8582 boolean_t missing = B_FALSE; 8583 boolean_t any_waited = B_FALSE; 8584 8585 for (int i = 0; i < ZFS_WAIT_NUM_ACTIVITIES; i++) { 8586 boolean_t waited; 8587 8588 if (!enabled[i]) 8589 continue; 8590 8591 error = zfs_wait_status(zhp, i, &missing, &waited); 8592 if (error != 0 || missing) 8593 break; 8594 8595 any_waited = (any_waited || waited); 8596 } 8597 8598 if (error != 0 || missing || !any_waited) 8599 break; 8600 } 8601 8602 zfs_close(zhp); 8603 8604 return (error); 8605 } 8606 8607 /* 8608 * Display version message 8609 */ 8610 static int 8611 zfs_do_version(int argc, char **argv) 8612 { 8613 if (zfs_version_print() == -1) 8614 return (1); 8615 8616 return (0); 8617 } 8618 8619 int 8620 main(int argc, char **argv) 8621 { 8622 int ret = 0; 8623 int i = 0; 8624 char *cmdname; 8625 char **newargv; 8626 8627 (void) setlocale(LC_ALL, ""); 8628 (void) setlocale(LC_NUMERIC, "C"); 8629 (void) textdomain(TEXT_DOMAIN); 8630 8631 opterr = 0; 8632 8633 /* 8634 * Make sure the user has specified some command. 8635 */ 8636 if (argc < 2) { 8637 (void) fprintf(stderr, gettext("missing command\n")); 8638 usage(B_FALSE); 8639 } 8640 8641 cmdname = argv[1]; 8642 8643 /* 8644 * The 'umount' command is an alias for 'unmount' 8645 */ 8646 if (strcmp(cmdname, "umount") == 0) 8647 cmdname = "unmount"; 8648 8649 /* 8650 * The 'recv' command is an alias for 'receive' 8651 */ 8652 if (strcmp(cmdname, "recv") == 0) 8653 cmdname = "receive"; 8654 8655 /* 8656 * The 'snap' command is an alias for 'snapshot' 8657 */ 8658 if (strcmp(cmdname, "snap") == 0) 8659 cmdname = "snapshot"; 8660 8661 /* 8662 * Special case '-?' 8663 */ 8664 if ((strcmp(cmdname, "-?") == 0) || 8665 (strcmp(cmdname, "--help") == 0)) 8666 usage(B_TRUE); 8667 8668 /* 8669 * Special case '-V|--version' 8670 */ 8671 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0)) 8672 return (zfs_do_version(argc, argv)); 8673 8674 if ((g_zfs = libzfs_init()) == NULL) { 8675 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno)); 8676 return (1); 8677 } 8678 8679 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 8680 8681 libzfs_print_on_error(g_zfs, B_TRUE); 8682 8683 /* 8684 * Many commands modify input strings for string parsing reasons. 8685 * We create a copy to protect the original argv. 8686 */ 8687 newargv = malloc((argc + 1) * sizeof (newargv[0])); 8688 for (i = 0; i < argc; i++) 8689 newargv[i] = strdup(argv[i]); 8690 newargv[argc] = NULL; 8691 8692 /* 8693 * Run the appropriate command. 8694 */ 8695 libzfs_mnttab_cache(g_zfs, B_TRUE); 8696 if (find_command_idx(cmdname, &i) == 0) { 8697 current_command = &command_table[i]; 8698 ret = command_table[i].func(argc - 1, newargv + 1); 8699 } else if (strchr(cmdname, '=') != NULL) { 8700 verify(find_command_idx("set", &i) == 0); 8701 current_command = &command_table[i]; 8702 ret = command_table[i].func(argc, newargv); 8703 } else { 8704 (void) fprintf(stderr, gettext("unrecognized " 8705 "command '%s'\n"), cmdname); 8706 usage(B_FALSE); 8707 ret = 1; 8708 } 8709 8710 for (i = 0; i < argc; i++) 8711 free(newargv[i]); 8712 free(newargv); 8713 8714 if (ret == 0 && log_history) 8715 (void) zpool_log_history(g_zfs, history_str); 8716 8717 libzfs_fini(g_zfs); 8718 8719 /* 8720 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 8721 * for the purposes of running ::findleaks. 8722 */ 8723 if (getenv("ZFS_ABORT") != NULL) { 8724 (void) printf("dumping core by request\n"); 8725 abort(); 8726 } 8727 8728 return (ret); 8729 } 8730 8731 #ifdef __FreeBSD__ 8732 #include <sys/jail.h> 8733 #include <jail.h> 8734 /* 8735 * Attach/detach the given dataset to/from the given jail 8736 */ 8737 /* ARGSUSED */ 8738 static int 8739 zfs_do_jail_impl(int argc, char **argv, boolean_t attach) 8740 { 8741 zfs_handle_t *zhp; 8742 int jailid, ret; 8743 8744 /* check number of arguments */ 8745 if (argc < 3) { 8746 (void) fprintf(stderr, gettext("missing argument(s)\n")); 8747 usage(B_FALSE); 8748 } 8749 if (argc > 3) { 8750 (void) fprintf(stderr, gettext("too many arguments\n")); 8751 usage(B_FALSE); 8752 } 8753 8754 jailid = jail_getid(argv[1]); 8755 if (jailid < 0) { 8756 (void) fprintf(stderr, gettext("invalid jail id or name\n")); 8757 usage(B_FALSE); 8758 } 8759 8760 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM); 8761 if (zhp == NULL) 8762 return (1); 8763 8764 ret = (zfs_jail(zhp, jailid, attach) != 0); 8765 8766 zfs_close(zhp); 8767 return (ret); 8768 } 8769 8770 /* 8771 * zfs jail jailid filesystem 8772 * 8773 * Attach the given dataset to the given jail 8774 */ 8775 /* ARGSUSED */ 8776 static int 8777 zfs_do_jail(int argc, char **argv) 8778 { 8779 return (zfs_do_jail_impl(argc, argv, B_TRUE)); 8780 } 8781 8782 /* 8783 * zfs unjail jailid filesystem 8784 * 8785 * Detach the given dataset from the given jail 8786 */ 8787 /* ARGSUSED */ 8788 static int 8789 zfs_do_unjail(int argc, char **argv) 8790 { 8791 return (zfs_do_jail_impl(argc, argv, B_FALSE)); 8792 } 8793 #endif 8794