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