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