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