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