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