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