Print this page
patch tsoome-feedback
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/zfs/zfs_main.c
+++ new/usr/src/cmd/zfs/zfs_main.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 25 * Copyright 2012 Milan Jurik. All rights reserved.
26 26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 29 * Copyright (c) 2014 Integros [integros.com]
30 30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
31 31 */
32 32
33 33 #include <assert.h>
34 34 #include <ctype.h>
35 35 #include <errno.h>
36 36 #include <libgen.h>
37 37 #include <libintl.h>
38 38 #include <libuutil.h>
39 39 #include <libnvpair.h>
40 40 #include <locale.h>
41 41 #include <stddef.h>
42 42 #include <stdio.h>
43 43 #include <stdlib.h>
44 44 #include <strings.h>
45 45 #include <unistd.h>
46 46 #include <fcntl.h>
47 47 #include <zone.h>
48 48 #include <grp.h>
49 49 #include <pwd.h>
50 50 #include <signal.h>
51 51 #include <sys/list.h>
52 52 #include <sys/mkdev.h>
53 53 #include <sys/mntent.h>
54 54 #include <sys/mnttab.h>
55 55 #include <sys/mount.h>
56 56 #include <sys/stat.h>
57 57 #include <sys/fs/zfs.h>
58 58 #include <sys/types.h>
59 59 #include <time.h>
60 60
61 61 #include <libzfs.h>
62 62 #include <libzfs_core.h>
63 63 #include <zfs_prop.h>
64 64 #include <zfs_deleg.h>
65 65 #include <libuutil.h>
66 66 #include <aclutils.h>
67 67 #include <directory.h>
68 68 #include <idmap.h>
69 69
70 70 #include "zfs_iter.h"
71 71 #include "zfs_util.h"
72 72 #include "zfs_comutil.h"
73 73
74 74 libzfs_handle_t *g_zfs;
75 75
76 76 static FILE *mnttab_file;
77 77 static char history_str[HIS_MAX_RECORD_LEN];
78 78 static boolean_t log_history = B_TRUE;
79 79
80 80 static int zfs_do_clone(int argc, char **argv);
81 81 static int zfs_do_create(int argc, char **argv);
82 82 static int zfs_do_destroy(int argc, char **argv);
83 83 static int zfs_do_get(int argc, char **argv);
84 84 static int zfs_do_inherit(int argc, char **argv);
85 85 static int zfs_do_list(int argc, char **argv);
86 86 static int zfs_do_mount(int argc, char **argv);
87 87 static int zfs_do_rename(int argc, char **argv);
88 88 static int zfs_do_rollback(int argc, char **argv);
89 89 static int zfs_do_set(int argc, char **argv);
90 90 static int zfs_do_upgrade(int argc, char **argv);
91 91 static int zfs_do_snapshot(int argc, char **argv);
92 92 static int zfs_do_unmount(int argc, char **argv);
93 93 static int zfs_do_share(int argc, char **argv);
94 94 static int zfs_do_unshare(int argc, char **argv);
95 95 static int zfs_do_send(int argc, char **argv);
96 96 static int zfs_do_receive(int argc, char **argv);
97 97 static int zfs_do_promote(int argc, char **argv);
98 98 static int zfs_do_userspace(int argc, char **argv);
99 99 static int zfs_do_allow(int argc, char **argv);
100 100 static int zfs_do_unallow(int argc, char **argv);
101 101 static int zfs_do_hold(int argc, char **argv);
102 102 static int zfs_do_holds(int argc, char **argv);
103 103 static int zfs_do_release(int argc, char **argv);
104 104 static int zfs_do_diff(int argc, char **argv);
105 105 static int zfs_do_bookmark(int argc, char **argv);
106 106
107 107 /*
108 108 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
109 109 */
110 110
111 111 #ifdef DEBUG
112 112 const char *
113 113 _umem_debug_init(void)
114 114 {
115 115 return ("default,verbose"); /* $UMEM_DEBUG setting */
116 116 }
117 117
118 118 const char *
119 119 _umem_logging_init(void)
120 120 {
121 121 return ("fail,contents"); /* $UMEM_LOGGING setting */
122 122 }
123 123 #endif
124 124
125 125 typedef enum {
126 126 HELP_CLONE,
127 127 HELP_CREATE,
128 128 HELP_DESTROY,
129 129 HELP_GET,
130 130 HELP_INHERIT,
131 131 HELP_UPGRADE,
132 132 HELP_LIST,
133 133 HELP_MOUNT,
134 134 HELP_PROMOTE,
135 135 HELP_RECEIVE,
136 136 HELP_RENAME,
137 137 HELP_ROLLBACK,
138 138 HELP_SEND,
139 139 HELP_SET,
140 140 HELP_SHARE,
141 141 HELP_SNAPSHOT,
142 142 HELP_UNMOUNT,
143 143 HELP_UNSHARE,
144 144 HELP_ALLOW,
145 145 HELP_UNALLOW,
146 146 HELP_USERSPACE,
147 147 HELP_GROUPSPACE,
148 148 HELP_HOLD,
149 149 HELP_HOLDS,
150 150 HELP_RELEASE,
151 151 HELP_DIFF,
152 152 HELP_BOOKMARK,
153 153 } zfs_help_t;
154 154
155 155 typedef struct zfs_command {
156 156 const char *name;
157 157 int (*func)(int argc, char **argv);
158 158 zfs_help_t usage;
159 159 } zfs_command_t;
160 160
161 161 /*
162 162 * Master command table. Each ZFS command has a name, associated function, and
163 163 * usage message. The usage messages need to be internationalized, so we have
164 164 * to have a function to return the usage message based on a command index.
165 165 *
166 166 * These commands are organized according to how they are displayed in the usage
167 167 * message. An empty command (one with a NULL name) indicates an empty line in
168 168 * the generic usage message.
169 169 */
170 170 static zfs_command_t command_table[] = {
171 171 { "create", zfs_do_create, HELP_CREATE },
172 172 { "destroy", zfs_do_destroy, HELP_DESTROY },
173 173 { NULL },
174 174 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
175 175 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
176 176 { "clone", zfs_do_clone, HELP_CLONE },
177 177 { "promote", zfs_do_promote, HELP_PROMOTE },
178 178 { "rename", zfs_do_rename, HELP_RENAME },
179 179 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
180 180 { NULL },
181 181 { "list", zfs_do_list, HELP_LIST },
182 182 { NULL },
183 183 { "set", zfs_do_set, HELP_SET },
184 184 { "get", zfs_do_get, HELP_GET },
185 185 { "inherit", zfs_do_inherit, HELP_INHERIT },
186 186 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
187 187 { "userspace", zfs_do_userspace, HELP_USERSPACE },
188 188 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
189 189 { NULL },
190 190 { "mount", zfs_do_mount, HELP_MOUNT },
191 191 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
192 192 { "share", zfs_do_share, HELP_SHARE },
193 193 { "unshare", zfs_do_unshare, HELP_UNSHARE },
194 194 { NULL },
195 195 { "send", zfs_do_send, HELP_SEND },
196 196 { "receive", zfs_do_receive, HELP_RECEIVE },
197 197 { NULL },
198 198 { "allow", zfs_do_allow, HELP_ALLOW },
199 199 { NULL },
200 200 { "unallow", zfs_do_unallow, HELP_UNALLOW },
201 201 { NULL },
202 202 { "hold", zfs_do_hold, HELP_HOLD },
203 203 { "holds", zfs_do_holds, HELP_HOLDS },
204 204 { "release", zfs_do_release, HELP_RELEASE },
205 205 { "diff", zfs_do_diff, HELP_DIFF },
206 206 };
207 207
208 208 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
209 209
210 210 zfs_command_t *current_command;
211 211
212 212 static const char *
213 213 get_usage(zfs_help_t idx)
214 214 {
215 215 switch (idx) {
216 216 case HELP_CLONE:
217 217 return (gettext("\tclone [-p] [-o property=value] ... "
218 218 "<snapshot> <filesystem|volume>\n"));
219 219 case HELP_CREATE:
220 220 return (gettext("\tcreate [-p] [-o property=value] ... "
221 221 "<filesystem>\n"
222 222 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
223 223 "-V <size> <volume>\n"));
224 224 case HELP_DESTROY:
225 225 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
226 226 "\tdestroy [-dnpRrv] "
227 227 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
228 228 "\tdestroy <filesystem|volume>#<bookmark>\n"));
229 229 case HELP_GET:
230 230 return (gettext("\tget [-rHp] [-d max] "
231 231 "[-o \"all\" | field[,...]]\n"
232 232 "\t [-t type[,...]] [-s source[,...]]\n"
233 233 "\t <\"all\" | property[,...]> "
234 234 "[filesystem|volume|snapshot] ...\n"));
235 235 case HELP_INHERIT:
236 236 return (gettext("\tinherit [-rS] <property> "
237 237 "<filesystem|volume|snapshot> ...\n"));
238 238 case HELP_UPGRADE:
239 239 return (gettext("\tupgrade [-v]\n"
240 240 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
241 241 case HELP_LIST:
242 242 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
243 243 "[-s property]...\n\t [-S property]... [-t type[,...]] "
244 244 "[filesystem|volume|snapshot] ...\n"));
245 245 case HELP_MOUNT:
246 246 return (gettext("\tmount\n"
247 247 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
248 248 case HELP_PROMOTE:
249 249 return (gettext("\tpromote <clone-filesystem>\n"));
250 250 case HELP_RECEIVE:
251 251 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
252 252 "snapshot>\n"
253 253 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
254 254 "<filesystem>\n"
255 255 "\treceive -A <filesystem|volume>\n"));
256 256 case HELP_RENAME:
257 257 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
258 258 "<filesystem|volume|snapshot>\n"
259 259 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
260 260 "\trename -r <snapshot> <snapshot>\n"));
261 261 case HELP_ROLLBACK:
262 262 return (gettext("\trollback [-rRf] <snapshot>\n"));
263 263 case HELP_SEND:
264 264 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
265 265 "<snapshot>\n"
266 266 "\tsend [-Le] [-i snapshot|bookmark] "
267 267 "<filesystem|volume|snapshot>\n"
268 268 "\tsend [-nvPe] -t <receive_resume_token>\n"));
269 269 case HELP_SET:
270 270 return (gettext("\tset <property=value> ... "
271 271 "<filesystem|volume|snapshot> ...\n"));
272 272 case HELP_SHARE:
273 273 return (gettext("\tshare <-a | filesystem>\n"));
274 274 case HELP_SNAPSHOT:
275 275 return (gettext("\tsnapshot [-r] [-o property=value] ... "
276 276 "<filesystem|volume>@<snap> ...\n"));
277 277 case HELP_UNMOUNT:
278 278 return (gettext("\tunmount [-f] "
279 279 "<-a | filesystem|mountpoint>\n"));
280 280 case HELP_UNSHARE:
281 281 return (gettext("\tunshare "
282 282 "<-a | filesystem|mountpoint>\n"));
283 283 case HELP_ALLOW:
284 284 return (gettext("\tallow <filesystem|volume>\n"
285 285 "\tallow [-ldug] "
286 286 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
287 287 "\t <filesystem|volume>\n"
288 288 "\tallow [-ld] -e <perm|@setname>[,...] "
289 289 "<filesystem|volume>\n"
290 290 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
291 291 "\tallow -s @setname <perm|@setname>[,...] "
292 292 "<filesystem|volume>\n"));
293 293 case HELP_UNALLOW:
294 294 return (gettext("\tunallow [-rldug] "
295 295 "<\"everyone\"|user|group>[,...]\n"
296 296 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
297 297 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
298 298 "<filesystem|volume>\n"
299 299 "\tunallow [-r] -c [<perm|@setname>[,...]] "
300 300 "<filesystem|volume>\n"
301 301 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
302 302 "<filesystem|volume>\n"));
303 303 case HELP_USERSPACE:
304 304 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
305 305 "[-s field] ...\n"
306 306 "\t [-S field] ... [-t type[,...]] "
307 307 "<filesystem|snapshot>\n"));
308 308 case HELP_GROUPSPACE:
309 309 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
310 310 "[-s field] ...\n"
311 311 "\t [-S field] ... [-t type[,...]] "
312 312 "<filesystem|snapshot>\n"));
313 313 case HELP_HOLD:
314 314 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
315 315 case HELP_HOLDS:
316 316 return (gettext("\tholds [-r] <snapshot> ...\n"));
317 317 case HELP_RELEASE:
318 318 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
319 319 case HELP_DIFF:
320 320 return (gettext("\tdiff [-FHt] <snapshot> "
321 321 "[snapshot|filesystem]\n"));
322 322 case HELP_BOOKMARK:
323 323 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
324 324 }
325 325
326 326 abort();
327 327 /* NOTREACHED */
328 328 }
329 329
330 330 void
331 331 nomem(void)
332 332 {
333 333 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
334 334 exit(1);
335 335 }
336 336
337 337 /*
338 338 * Utility function to guarantee malloc() success.
339 339 */
340 340
341 341 void *
342 342 safe_malloc(size_t size)
343 343 {
344 344 void *data;
345 345
346 346 if ((data = calloc(1, size)) == NULL)
347 347 nomem();
348 348
349 349 return (data);
350 350 }
351 351
352 352 static char *
353 353 safe_strdup(char *str)
354 354 {
355 355 char *dupstr = strdup(str);
356 356
357 357 if (dupstr == NULL)
358 358 nomem();
359 359
360 360 return (dupstr);
361 361 }
362 362
363 363 /*
364 364 * Callback routine that will print out information for each of
365 365 * the properties.
366 366 */
367 367 static int
368 368 usage_prop_cb(int prop, void *cb)
369 369 {
370 370 FILE *fp = cb;
371 371
372 372 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
373 373
374 374 if (zfs_prop_readonly(prop))
375 375 (void) fprintf(fp, " NO ");
376 376 else
377 377 (void) fprintf(fp, "YES ");
378 378
379 379 if (zfs_prop_inheritable(prop))
380 380 (void) fprintf(fp, " YES ");
381 381 else
382 382 (void) fprintf(fp, " NO ");
383 383
384 384 if (zfs_prop_values(prop) == NULL)
385 385 (void) fprintf(fp, "-\n");
386 386 else
387 387 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
388 388
389 389 return (ZPROP_CONT);
390 390 }
391 391
392 392 /*
393 393 * Display usage message. If we're inside a command, display only the usage for
394 394 * that command. Otherwise, iterate over the entire command table and display
395 395 * a complete usage message.
396 396 */
397 397 static void
398 398 usage(boolean_t requested)
399 399 {
400 400 int i;
401 401 boolean_t show_properties = B_FALSE;
402 402 FILE *fp = requested ? stdout : stderr;
403 403
404 404 if (current_command == NULL) {
405 405
406 406 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
407 407 (void) fprintf(fp,
408 408 gettext("where 'command' is one of the following:\n\n"));
409 409
410 410 for (i = 0; i < NCOMMAND; i++) {
411 411 if (command_table[i].name == NULL)
412 412 (void) fprintf(fp, "\n");
413 413 else
414 414 (void) fprintf(fp, "%s",
415 415 get_usage(command_table[i].usage));
416 416 }
417 417
418 418 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
419 419 "pool/[dataset/]*dataset[@name]\n"));
420 420 } else {
421 421 (void) fprintf(fp, gettext("usage:\n"));
422 422 (void) fprintf(fp, "%s", get_usage(current_command->usage));
423 423 }
424 424
425 425 if (current_command != NULL &&
426 426 (strcmp(current_command->name, "set") == 0 ||
427 427 strcmp(current_command->name, "get") == 0 ||
428 428 strcmp(current_command->name, "inherit") == 0 ||
429 429 strcmp(current_command->name, "list") == 0))
430 430 show_properties = B_TRUE;
431 431
432 432 if (show_properties) {
433 433 (void) fprintf(fp,
434 434 gettext("\nThe following properties are supported:\n"));
435 435
436 436 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
437 437 "PROPERTY", "EDIT", "INHERIT", "VALUES");
438 438
439 439 /* Iterate over all properties */
440 440 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
441 441 ZFS_TYPE_DATASET);
442 442
443 443 (void) fprintf(fp, "\t%-15s ", "userused@...");
444 444 (void) fprintf(fp, " NO NO <size>\n");
445 445 (void) fprintf(fp, "\t%-15s ", "groupused@...");
446 446 (void) fprintf(fp, " NO NO <size>\n");
447 447 (void) fprintf(fp, "\t%-15s ", "userquota@...");
448 448 (void) fprintf(fp, "YES NO <size> | none\n");
449 449 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
450 450 (void) fprintf(fp, "YES NO <size> | none\n");
451 451 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
452 452 (void) fprintf(fp, " NO NO <size>\n");
453 453
454 454 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
455 455 "with standard units such as K, M, G, etc.\n"));
456 456 (void) fprintf(fp, gettext("\nUser-defined properties can "
457 457 "be specified by using a name containing a colon (:).\n"));
458 458 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
459 459 "properties must be appended with\n"
460 460 "a user or group specifier of one of these forms:\n"
461 461 " POSIX name (eg: \"matt\")\n"
462 462 " POSIX id (eg: \"126829\")\n"
463 463 " SMB name@domain (eg: \"matt@sun\")\n"
464 464 " SMB SID (eg: \"S-1-234-567-89\")\n"));
465 465 } else {
466 466 (void) fprintf(fp,
467 467 gettext("\nFor the property list, run: %s\n"),
468 468 "zfs set|get");
469 469 (void) fprintf(fp,
470 470 gettext("\nFor the delegated permission list, run: %s\n"),
471 471 "zfs allow|unallow");
472 472 }
473 473
474 474 /*
475 475 * See comments at end of main().
476 476 */
477 477 if (getenv("ZFS_ABORT") != NULL) {
478 478 (void) printf("dumping core by request\n");
479 479 abort();
480 480 }
481 481
482 482 exit(requested ? 0 : 2);
483 483 }
484 484
485 485 /*
486 486 * Take a property=value argument string and add it to the given nvlist.
487 487 * Modifies the argument inplace.
488 488 */
489 489 static int
490 490 parseprop(nvlist_t *props, char *propname)
491 491 {
492 492 char *propval, *strval;
493 493
494 494 if ((propval = strchr(propname, '=')) == NULL) {
495 495 (void) fprintf(stderr, gettext("missing "
496 496 "'=' for property=value argument\n"));
497 497 return (-1);
498 498 }
499 499 *propval = '\0';
500 500 propval++;
501 501 if (nvlist_lookup_string(props, propname, &strval) == 0) {
502 502 (void) fprintf(stderr, gettext("property '%s' "
503 503 "specified multiple times\n"), propname);
504 504 return (-1);
505 505 }
506 506 if (nvlist_add_string(props, propname, propval) != 0)
507 507 nomem();
508 508 return (0);
509 509 }
510 510
511 511 static int
512 512 parse_depth(char *opt, int *flags)
513 513 {
514 514 char *tmp;
515 515 int depth;
516 516
517 517 depth = (int)strtol(opt, &tmp, 0);
518 518 if (*tmp) {
519 519 (void) fprintf(stderr,
520 520 gettext("%s is not an integer\n"), optarg);
521 521 usage(B_FALSE);
522 522 }
523 523 if (depth < 0) {
524 524 (void) fprintf(stderr,
525 525 gettext("Depth can not be negative.\n"));
526 526 usage(B_FALSE);
527 527 }
528 528 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
529 529 return (depth);
530 530 }
531 531
532 532 #define PROGRESS_DELAY 2 /* seconds */
533 533
534 534 static char *pt_reverse = "\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";
535 535 static time_t pt_begin;
536 536 static char *pt_header = NULL;
537 537 static boolean_t pt_shown;
538 538
539 539 static void
540 540 start_progress_timer(void)
541 541 {
542 542 pt_begin = time(NULL) + PROGRESS_DELAY;
543 543 pt_shown = B_FALSE;
544 544 }
545 545
546 546 static void
547 547 set_progress_header(char *header)
548 548 {
549 549 assert(pt_header == NULL);
550 550 pt_header = safe_strdup(header);
551 551 if (pt_shown) {
552 552 (void) printf("%s: ", header);
553 553 (void) fflush(stdout);
554 554 }
555 555 }
556 556
557 557 static void
558 558 update_progress(char *update)
559 559 {
560 560 if (!pt_shown && time(NULL) > pt_begin) {
561 561 int len = strlen(update);
562 562
563 563 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
564 564 pt_reverse);
565 565 (void) fflush(stdout);
566 566 pt_shown = B_TRUE;
567 567 } else if (pt_shown) {
568 568 int len = strlen(update);
569 569
570 570 (void) printf("%s%*.*s", update, len, len, pt_reverse);
571 571 (void) fflush(stdout);
572 572 }
573 573 }
574 574
575 575 static void
576 576 finish_progress(char *done)
577 577 {
578 578 if (pt_shown) {
579 579 (void) printf("%s\n", done);
580 580 (void) fflush(stdout);
581 581 }
582 582 free(pt_header);
583 583 pt_header = NULL;
584 584 }
585 585
586 586 /*
587 587 * Check if the dataset is mountable and should be automatically mounted.
588 588 */
589 589 static boolean_t
590 590 should_auto_mount(zfs_handle_t *zhp)
591 591 {
592 592 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
593 593 return (B_FALSE);
594 594 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
595 595 }
596 596
597 597 /*
598 598 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
599 599 *
600 600 * Given an existing dataset, create a writable copy whose initial contents
601 601 * are the same as the source. The newly created dataset maintains a
602 602 * dependency on the original; the original cannot be destroyed so long as
603 603 * the clone exists.
604 604 *
605 605 * The '-p' flag creates all the non-existing ancestors of the target first.
606 606 */
607 607 static int
608 608 zfs_do_clone(int argc, char **argv)
609 609 {
610 610 zfs_handle_t *zhp = NULL;
611 611 boolean_t parents = B_FALSE;
612 612 nvlist_t *props;
613 613 int ret = 0;
614 614 int c;
615 615
616 616 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
617 617 nomem();
618 618
619 619 /* check options */
620 620 while ((c = getopt(argc, argv, "o:p")) != -1) {
621 621 switch (c) {
622 622 case 'o':
623 623 if (parseprop(props, optarg) != 0)
624 624 return (1);
625 625 break;
626 626 case 'p':
627 627 parents = B_TRUE;
628 628 break;
629 629 case '?':
630 630 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
631 631 optopt);
632 632 goto usage;
633 633 }
634 634 }
635 635
636 636 argc -= optind;
637 637 argv += optind;
638 638
639 639 /* check number of arguments */
640 640 if (argc < 1) {
641 641 (void) fprintf(stderr, gettext("missing source dataset "
642 642 "argument\n"));
643 643 goto usage;
644 644 }
645 645 if (argc < 2) {
646 646 (void) fprintf(stderr, gettext("missing target dataset "
647 647 "argument\n"));
648 648 goto usage;
649 649 }
650 650 if (argc > 2) {
651 651 (void) fprintf(stderr, gettext("too many arguments\n"));
652 652 goto usage;
653 653 }
654 654
655 655 /* open the source dataset */
656 656 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
657 657 return (1);
658 658
659 659 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
660 660 ZFS_TYPE_VOLUME)) {
661 661 /*
662 662 * Now create the ancestors of the target dataset. If the
663 663 * target already exists and '-p' option was used we should not
664 664 * complain.
665 665 */
666 666 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
667 667 ZFS_TYPE_VOLUME))
668 668 return (0);
669 669 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
670 670 return (1);
671 671 }
672 672
673 673 /* pass to libzfs */
674 674 ret = zfs_clone(zhp, argv[1], props);
675 675
676 676 /* create the mountpoint if necessary */
677 677 if (ret == 0) {
678 678 zfs_handle_t *clone;
679 679
680 680 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
681 681 if (clone != NULL) {
682 682 /*
683 683 * If the user doesn't want the dataset
684 684 * automatically mounted, then skip the mount/share
685 685 * step.
686 686 */
687 687 if (should_auto_mount(clone)) {
688 688 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
689 689 (void) fprintf(stderr, gettext("clone "
690 690 "successfully created, "
691 691 "but not mounted\n"));
692 692 } else if ((ret = zfs_share(clone)) != 0) {
693 693 (void) fprintf(stderr, gettext("clone "
694 694 "successfully created, "
695 695 "but not shared\n"));
696 696 }
697 697 }
698 698 zfs_close(clone);
699 699 }
700 700 }
701 701
702 702 zfs_close(zhp);
703 703 nvlist_free(props);
704 704
705 705 return (!!ret);
706 706
707 707 usage:
708 708 if (zhp)
709 709 zfs_close(zhp);
710 710 nvlist_free(props);
711 711 usage(B_FALSE);
712 712 return (-1);
713 713 }
714 714
715 715 /*
716 716 * zfs create [-p] [-o prop=value] ... fs
717 717 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
718 718 *
719 719 * Create a new dataset. This command can be used to create filesystems
720 720 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
721 721 * For volumes, the user must specify a size to be used.
722 722 *
723 723 * The '-s' flag applies only to volumes, and indicates that we should not try
724 724 * to set the reservation for this volume. By default we set a reservation
725 725 * equal to the size for any volume. For pools with SPA_VERSION >=
726 726 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
727 727 *
728 728 * The '-p' flag creates all the non-existing ancestors of the target first.
729 729 */
730 730 static int
731 731 zfs_do_create(int argc, char **argv)
732 732 {
733 733 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
734 734 zfs_handle_t *zhp = NULL;
735 735 uint64_t volsize = 0;
736 736 int c;
737 737 boolean_t noreserve = B_FALSE;
738 738 boolean_t bflag = B_FALSE;
739 739 boolean_t parents = B_FALSE;
740 740 int ret = 1;
741 741 nvlist_t *props;
742 742 uint64_t intval;
743 743
744 744 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
745 745 nomem();
746 746
747 747 /* check options */
748 748 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
749 749 switch (c) {
750 750 case 'V':
751 751 type = ZFS_TYPE_VOLUME;
752 752 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
753 753 (void) fprintf(stderr, gettext("bad volume "
754 754 "size '%s': %s\n"), optarg,
755 755 libzfs_error_description(g_zfs));
756 756 goto error;
757 757 }
758 758
759 759 if (nvlist_add_uint64(props,
760 760 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
761 761 nomem();
762 762 volsize = intval;
763 763 break;
764 764 case 'p':
765 765 parents = B_TRUE;
766 766 break;
767 767 case 'b':
768 768 bflag = B_TRUE;
769 769 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
770 770 (void) fprintf(stderr, gettext("bad volume "
771 771 "block size '%s': %s\n"), optarg,
772 772 libzfs_error_description(g_zfs));
773 773 goto error;
774 774 }
775 775
776 776 if (nvlist_add_uint64(props,
777 777 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
778 778 intval) != 0)
779 779 nomem();
780 780 break;
781 781 case 'o':
782 782 if (parseprop(props, optarg) != 0)
783 783 goto error;
784 784 break;
785 785 case 's':
786 786 noreserve = B_TRUE;
787 787 break;
788 788 case ':':
789 789 (void) fprintf(stderr, gettext("missing size "
790 790 "argument\n"));
791 791 goto badusage;
792 792 case '?':
793 793 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
794 794 optopt);
795 795 goto badusage;
796 796 }
797 797 }
798 798
799 799 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
800 800 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
801 801 "used when creating a volume\n"));
802 802 goto badusage;
803 803 }
804 804
805 805 argc -= optind;
806 806 argv += optind;
807 807
808 808 /* check number of arguments */
809 809 if (argc == 0) {
810 810 (void) fprintf(stderr, gettext("missing %s argument\n"),
811 811 zfs_type_to_name(type));
812 812 goto badusage;
813 813 }
814 814 if (argc > 1) {
815 815 (void) fprintf(stderr, gettext("too many arguments\n"));
816 816 goto badusage;
817 817 }
818 818
819 819 if (type == ZFS_TYPE_VOLUME && !noreserve) {
820 820 zpool_handle_t *zpool_handle;
821 821 nvlist_t *real_props = NULL;
822 822 uint64_t spa_version;
823 823 char *p;
824 824 zfs_prop_t resv_prop;
825 825 char *strval;
826 826 char msg[1024];
827 827
828 828 if ((p = strchr(argv[0], '/')) != NULL)
829 829 *p = '\0';
830 830 zpool_handle = zpool_open(g_zfs, argv[0]);
831 831 if (p != NULL)
832 832 *p = '/';
833 833 if (zpool_handle == NULL)
834 834 goto error;
835 835 spa_version = zpool_get_prop_int(zpool_handle,
836 836 ZPOOL_PROP_VERSION, NULL);
837 837 if (spa_version >= SPA_VERSION_REFRESERVATION)
838 838 resv_prop = ZFS_PROP_REFRESERVATION;
839 839 else
840 840 resv_prop = ZFS_PROP_RESERVATION;
841 841
842 842 (void) snprintf(msg, sizeof (msg),
843 843 gettext("cannot create '%s'"), argv[0]);
844 844 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
845 845 props, 0, NULL, zpool_handle, msg)) == NULL) {
846 846 zpool_close(zpool_handle);
847 847 goto error;
848 848 }
849 849 zpool_close(zpool_handle);
850 850
851 851 volsize = zvol_volsize_to_reservation(volsize, real_props);
852 852 nvlist_free(real_props);
853 853
854 854 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
855 855 &strval) != 0) {
856 856 if (nvlist_add_uint64(props,
857 857 zfs_prop_to_name(resv_prop), volsize) != 0) {
858 858 nvlist_free(props);
859 859 nomem();
860 860 }
861 861 }
862 862 }
863 863
864 864 if (parents && zfs_name_valid(argv[0], type)) {
865 865 /*
866 866 * Now create the ancestors of target dataset. If the target
867 867 * already exists and '-p' option was used we should not
868 868 * complain.
869 869 */
870 870 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
871 871 ret = 0;
872 872 goto error;
873 873 }
874 874 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
875 875 goto error;
876 876 }
877 877
878 878 /* pass to libzfs */
879 879 if (zfs_create(g_zfs, argv[0], type, props) != 0)
880 880 goto error;
881 881
882 882 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
883 883 goto error;
884 884
885 885 ret = 0;
886 886
887 887 /*
888 888 * Mount and/or share the new filesystem as appropriate. We provide a
889 889 * verbose error message to let the user know that their filesystem was
890 890 * in fact created, even if we failed to mount or share it.
891 891 * If the user doesn't want the dataset automatically mounted,
892 892 * then skip the mount/share step altogether.
893 893 */
894 894 if (should_auto_mount(zhp)) {
895 895 if (zfs_mount(zhp, NULL, 0) != 0) {
896 896 (void) fprintf(stderr, gettext("filesystem "
897 897 "successfully created, but not mounted\n"));
898 898 ret = 1;
899 899 } else if (zfs_share(zhp) != 0) {
900 900 (void) fprintf(stderr, gettext("filesystem "
901 901 "successfully created, but not shared\n"));
902 902 ret = 1;
903 903 }
904 904 }
905 905
906 906 error:
907 907 if (zhp)
908 908 zfs_close(zhp);
909 909 nvlist_free(props);
910 910 return (ret);
911 911 badusage:
912 912 nvlist_free(props);
913 913 usage(B_FALSE);
914 914 return (2);
915 915 }
916 916
917 917 /*
918 918 * zfs destroy [-rRf] <fs, vol>
919 919 * zfs destroy [-rRd] <snap>
920 920 *
921 921 * -r Recursively destroy all children
922 922 * -R Recursively destroy all dependents, including clones
923 923 * -f Force unmounting of any dependents
924 924 * -d If we can't destroy now, mark for deferred destruction
925 925 *
926 926 * Destroys the given dataset. By default, it will unmount any filesystems,
927 927 * and refuse to destroy a dataset that has any dependents. A dependent can
928 928 * either be a child, or a clone of a child.
929 929 */
930 930 typedef struct destroy_cbdata {
931 931 boolean_t cb_first;
932 932 boolean_t cb_force;
933 933 boolean_t cb_recurse;
934 934 boolean_t cb_error;
935 935 boolean_t cb_doclones;
936 936 zfs_handle_t *cb_target;
937 937 boolean_t cb_defer_destroy;
938 938 boolean_t cb_verbose;
939 939 boolean_t cb_parsable;
940 940 boolean_t cb_dryrun;
941 941 nvlist_t *cb_nvl;
942 942 nvlist_t *cb_batchedsnaps;
943 943
944 944 /* first snap in contiguous run */
945 945 char *cb_firstsnap;
946 946 /* previous snap in contiguous run */
947 947 char *cb_prevsnap;
948 948 int64_t cb_snapused;
949 949 char *cb_snapspec;
950 950 char *cb_bookmark;
951 951 } destroy_cbdata_t;
952 952
953 953 /*
954 954 * Check for any dependents based on the '-r' or '-R' flags.
955 955 */
956 956 static int
957 957 destroy_check_dependent(zfs_handle_t *zhp, void *data)
958 958 {
959 959 destroy_cbdata_t *cbp = data;
960 960 const char *tname = zfs_get_name(cbp->cb_target);
961 961 const char *name = zfs_get_name(zhp);
962 962
963 963 if (strncmp(tname, name, strlen(tname)) == 0 &&
964 964 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
965 965 /*
966 966 * This is a direct descendant, not a clone somewhere else in
967 967 * the hierarchy.
968 968 */
969 969 if (cbp->cb_recurse)
970 970 goto out;
971 971
972 972 if (cbp->cb_first) {
973 973 (void) fprintf(stderr, gettext("cannot destroy '%s': "
974 974 "%s has children\n"),
975 975 zfs_get_name(cbp->cb_target),
976 976 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
977 977 (void) fprintf(stderr, gettext("use '-r' to destroy "
978 978 "the following datasets:\n"));
979 979 cbp->cb_first = B_FALSE;
980 980 cbp->cb_error = B_TRUE;
981 981 }
982 982
983 983 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
984 984 } else {
985 985 /*
986 986 * This is a clone. We only want to report this if the '-r'
987 987 * wasn't specified, or the target is a snapshot.
988 988 */
989 989 if (!cbp->cb_recurse &&
990 990 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
991 991 goto out;
992 992
993 993 if (cbp->cb_first) {
994 994 (void) fprintf(stderr, gettext("cannot destroy '%s': "
995 995 "%s has dependent clones\n"),
996 996 zfs_get_name(cbp->cb_target),
997 997 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
998 998 (void) fprintf(stderr, gettext("use '-R' to destroy "
999 999 "the following datasets:\n"));
1000 1000 cbp->cb_first = B_FALSE;
1001 1001 cbp->cb_error = B_TRUE;
1002 1002 cbp->cb_dryrun = B_TRUE;
1003 1003 }
1004 1004
1005 1005 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1006 1006 }
1007 1007
1008 1008 out:
1009 1009 zfs_close(zhp);
1010 1010 return (0);
1011 1011 }
1012 1012
1013 1013 static int
1014 1014 destroy_callback(zfs_handle_t *zhp, void *data)
1015 1015 {
1016 1016 destroy_cbdata_t *cb = data;
1017 1017 const char *name = zfs_get_name(zhp);
1018 1018
1019 1019 if (cb->cb_verbose) {
1020 1020 if (cb->cb_parsable) {
1021 1021 (void) printf("destroy\t%s\n", name);
1022 1022 } else if (cb->cb_dryrun) {
1023 1023 (void) printf(gettext("would destroy %s\n"),
1024 1024 name);
1025 1025 } else {
1026 1026 (void) printf(gettext("will destroy %s\n"),
1027 1027 name);
1028 1028 }
1029 1029 }
1030 1030
1031 1031 /*
1032 1032 * Ignore pools (which we've already flagged as an error before getting
1033 1033 * here).
1034 1034 */
1035 1035 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1036 1036 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1037 1037 zfs_close(zhp);
1038 1038 return (0);
1039 1039 }
1040 1040 if (cb->cb_dryrun) {
1041 1041 zfs_close(zhp);
1042 1042 return (0);
1043 1043 }
1044 1044
1045 1045 /*
1046 1046 * We batch up all contiguous snapshots (even of different
1047 1047 * filesystems) and destroy them with one ioctl. We can't
1048 1048 * simply do all snap deletions and then all fs deletions,
1049 1049 * because we must delete a clone before its origin.
1050 1050 */
1051 1051 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1052 1052 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1053 1053 } else {
1054 1054 int error = zfs_destroy_snaps_nvl(g_zfs,
1055 1055 cb->cb_batchedsnaps, B_FALSE);
1056 1056 fnvlist_free(cb->cb_batchedsnaps);
1057 1057 cb->cb_batchedsnaps = fnvlist_alloc();
1058 1058
1059 1059 if (error != 0 ||
1060 1060 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1061 1061 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1062 1062 zfs_close(zhp);
1063 1063 return (-1);
1064 1064 }
1065 1065 }
1066 1066
1067 1067 zfs_close(zhp);
1068 1068 return (0);
1069 1069 }
1070 1070
1071 1071 static int
1072 1072 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1073 1073 {
1074 1074 destroy_cbdata_t *cb = arg;
1075 1075 const char *name = zfs_get_name(zhp);
1076 1076 int err = 0;
1077 1077
1078 1078 if (nvlist_exists(cb->cb_nvl, name)) {
1079 1079 if (cb->cb_firstsnap == NULL)
1080 1080 cb->cb_firstsnap = strdup(name);
1081 1081 if (cb->cb_prevsnap != NULL)
1082 1082 free(cb->cb_prevsnap);
1083 1083 /* this snap continues the current range */
1084 1084 cb->cb_prevsnap = strdup(name);
1085 1085 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1086 1086 nomem();
1087 1087 if (cb->cb_verbose) {
1088 1088 if (cb->cb_parsable) {
1089 1089 (void) printf("destroy\t%s\n", name);
1090 1090 } else if (cb->cb_dryrun) {
1091 1091 (void) printf(gettext("would destroy %s\n"),
1092 1092 name);
1093 1093 } else {
1094 1094 (void) printf(gettext("will destroy %s\n"),
1095 1095 name);
1096 1096 }
1097 1097 }
1098 1098 } else if (cb->cb_firstsnap != NULL) {
1099 1099 /* end of this range */
1100 1100 uint64_t used = 0;
1101 1101 err = lzc_snaprange_space(cb->cb_firstsnap,
1102 1102 cb->cb_prevsnap, &used);
1103 1103 cb->cb_snapused += used;
1104 1104 free(cb->cb_firstsnap);
1105 1105 cb->cb_firstsnap = NULL;
1106 1106 free(cb->cb_prevsnap);
1107 1107 cb->cb_prevsnap = NULL;
1108 1108 }
1109 1109 zfs_close(zhp);
1110 1110 return (err);
1111 1111 }
1112 1112
1113 1113 static int
1114 1114 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1115 1115 {
1116 1116 int err = 0;
1117 1117 assert(cb->cb_firstsnap == NULL);
1118 1118 assert(cb->cb_prevsnap == NULL);
1119 1119 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1120 1120 if (cb->cb_firstsnap != NULL) {
1121 1121 uint64_t used = 0;
1122 1122 if (err == 0) {
1123 1123 err = lzc_snaprange_space(cb->cb_firstsnap,
1124 1124 cb->cb_prevsnap, &used);
1125 1125 }
1126 1126 cb->cb_snapused += used;
1127 1127 free(cb->cb_firstsnap);
1128 1128 cb->cb_firstsnap = NULL;
1129 1129 free(cb->cb_prevsnap);
1130 1130 cb->cb_prevsnap = NULL;
1131 1131 }
1132 1132 return (err);
1133 1133 }
1134 1134
1135 1135 static int
1136 1136 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1137 1137 {
1138 1138 destroy_cbdata_t *cb = arg;
1139 1139 int err = 0;
1140 1140
1141 1141 /* Check for clones. */
1142 1142 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1143 1143 cb->cb_target = zhp;
1144 1144 cb->cb_first = B_TRUE;
1145 1145 err = zfs_iter_dependents(zhp, B_TRUE,
1146 1146 destroy_check_dependent, cb);
1147 1147 }
1148 1148
1149 1149 if (err == 0) {
1150 1150 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1151 1151 nomem();
1152 1152 }
1153 1153 zfs_close(zhp);
1154 1154 return (err);
1155 1155 }
1156 1156
1157 1157 static int
1158 1158 gather_snapshots(zfs_handle_t *zhp, void *arg)
1159 1159 {
1160 1160 destroy_cbdata_t *cb = arg;
1161 1161 int err = 0;
1162 1162
1163 1163 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1164 1164 if (err == ENOENT)
1165 1165 err = 0;
1166 1166 if (err != 0)
1167 1167 goto out;
1168 1168
1169 1169 if (cb->cb_verbose) {
1170 1170 err = destroy_print_snapshots(zhp, cb);
1171 1171 if (err != 0)
1172 1172 goto out;
1173 1173 }
1174 1174
1175 1175 if (cb->cb_recurse)
1176 1176 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1177 1177
1178 1178 out:
1179 1179 zfs_close(zhp);
1180 1180 return (err);
1181 1181 }
1182 1182
1183 1183 static int
1184 1184 destroy_clones(destroy_cbdata_t *cb)
1185 1185 {
1186 1186 nvpair_t *pair;
1187 1187 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1188 1188 pair != NULL;
1189 1189 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1190 1190 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1191 1191 ZFS_TYPE_SNAPSHOT);
1192 1192 if (zhp != NULL) {
1193 1193 boolean_t defer = cb->cb_defer_destroy;
1194 1194 int err = 0;
1195 1195
1196 1196 /*
1197 1197 * We can't defer destroy non-snapshots, so set it to
1198 1198 * false while destroying the clones.
1199 1199 */
1200 1200 cb->cb_defer_destroy = B_FALSE;
1201 1201 err = zfs_iter_dependents(zhp, B_FALSE,
1202 1202 destroy_callback, cb);
1203 1203 cb->cb_defer_destroy = defer;
1204 1204 zfs_close(zhp);
1205 1205 if (err != 0)
1206 1206 return (err);
1207 1207 }
1208 1208 }
1209 1209 return (0);
1210 1210 }
1211 1211
1212 1212 static int
1213 1213 zfs_do_destroy(int argc, char **argv)
1214 1214 {
1215 1215 destroy_cbdata_t cb = { 0 };
1216 1216 int rv = 0;
1217 1217 int err = 0;
1218 1218 int c;
1219 1219 zfs_handle_t *zhp = NULL;
1220 1220 char *at, *pound;
1221 1221 zfs_type_t type = ZFS_TYPE_DATASET;
1222 1222
1223 1223 /* check options */
1224 1224 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1225 1225 switch (c) {
1226 1226 case 'v':
1227 1227 cb.cb_verbose = B_TRUE;
1228 1228 break;
1229 1229 case 'p':
1230 1230 cb.cb_verbose = B_TRUE;
1231 1231 cb.cb_parsable = B_TRUE;
1232 1232 break;
1233 1233 case 'n':
1234 1234 cb.cb_dryrun = B_TRUE;
1235 1235 break;
1236 1236 case 'd':
1237 1237 cb.cb_defer_destroy = B_TRUE;
1238 1238 type = ZFS_TYPE_SNAPSHOT;
1239 1239 break;
1240 1240 case 'f':
1241 1241 cb.cb_force = B_TRUE;
1242 1242 break;
1243 1243 case 'r':
1244 1244 cb.cb_recurse = B_TRUE;
1245 1245 break;
1246 1246 case 'R':
1247 1247 cb.cb_recurse = B_TRUE;
1248 1248 cb.cb_doclones = B_TRUE;
1249 1249 break;
1250 1250 case '?':
1251 1251 default:
1252 1252 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1253 1253 optopt);
1254 1254 usage(B_FALSE);
1255 1255 }
1256 1256 }
1257 1257
1258 1258 argc -= optind;
1259 1259 argv += optind;
1260 1260
1261 1261 /* check number of arguments */
1262 1262 if (argc == 0) {
1263 1263 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1264 1264 usage(B_FALSE);
1265 1265 }
1266 1266 if (argc > 1) {
1267 1267 (void) fprintf(stderr, gettext("too many arguments\n"));
1268 1268 usage(B_FALSE);
1269 1269 }
1270 1270
1271 1271 at = strchr(argv[0], '@');
1272 1272 pound = strchr(argv[0], '#');
1273 1273 if (at != NULL) {
1274 1274
1275 1275 /* Build the list of snaps to destroy in cb_nvl. */
1276 1276 cb.cb_nvl = fnvlist_alloc();
1277 1277
1278 1278 *at = '\0';
1279 1279 zhp = zfs_open(g_zfs, argv[0],
1280 1280 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1281 1281 if (zhp == NULL)
1282 1282 return (1);
1283 1283
1284 1284 cb.cb_snapspec = at + 1;
1285 1285 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1286 1286 cb.cb_error) {
1287 1287 rv = 1;
1288 1288 goto out;
1289 1289 }
1290 1290
1291 1291 if (nvlist_empty(cb.cb_nvl)) {
1292 1292 (void) fprintf(stderr, gettext("could not find any "
1293 1293 "snapshots to destroy; check snapshot names.\n"));
1294 1294 rv = 1;
1295 1295 goto out;
1296 1296 }
1297 1297
1298 1298 if (cb.cb_verbose) {
1299 1299 char buf[16];
1300 1300 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1301 1301 if (cb.cb_parsable) {
1302 1302 (void) printf("reclaim\t%llu\n",
1303 1303 cb.cb_snapused);
1304 1304 } else if (cb.cb_dryrun) {
1305 1305 (void) printf(gettext("would reclaim %s\n"),
1306 1306 buf);
1307 1307 } else {
1308 1308 (void) printf(gettext("will reclaim %s\n"),
1309 1309 buf);
1310 1310 }
1311 1311 }
1312 1312
1313 1313 if (!cb.cb_dryrun) {
1314 1314 if (cb.cb_doclones) {
1315 1315 cb.cb_batchedsnaps = fnvlist_alloc();
1316 1316 err = destroy_clones(&cb);
1317 1317 if (err == 0) {
1318 1318 err = zfs_destroy_snaps_nvl(g_zfs,
1319 1319 cb.cb_batchedsnaps, B_FALSE);
1320 1320 }
1321 1321 if (err != 0) {
1322 1322 rv = 1;
1323 1323 goto out;
1324 1324 }
1325 1325 }
1326 1326 if (err == 0) {
1327 1327 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1328 1328 cb.cb_defer_destroy);
1329 1329 }
1330 1330 }
1331 1331
1332 1332 if (err != 0)
1333 1333 rv = 1;
1334 1334 } else if (pound != NULL) {
1335 1335 int err;
1336 1336 nvlist_t *nvl;
1337 1337
1338 1338 if (cb.cb_dryrun) {
1339 1339 (void) fprintf(stderr,
1340 1340 "dryrun is not supported with bookmark\n");
1341 1341 return (-1);
1342 1342 }
1343 1343
1344 1344 if (cb.cb_defer_destroy) {
1345 1345 (void) fprintf(stderr,
1346 1346 "defer destroy is not supported with bookmark\n");
1347 1347 return (-1);
1348 1348 }
1349 1349
1350 1350 if (cb.cb_recurse) {
1351 1351 (void) fprintf(stderr,
1352 1352 "recursive is not supported with bookmark\n");
1353 1353 return (-1);
1354 1354 }
1355 1355
1356 1356 if (!zfs_bookmark_exists(argv[0])) {
1357 1357 (void) fprintf(stderr, gettext("bookmark '%s' "
1358 1358 "does not exist.\n"), argv[0]);
1359 1359 return (1);
1360 1360 }
1361 1361
1362 1362 nvl = fnvlist_alloc();
1363 1363 fnvlist_add_boolean(nvl, argv[0]);
1364 1364
1365 1365 err = lzc_destroy_bookmarks(nvl, NULL);
1366 1366 if (err != 0) {
1367 1367 (void) zfs_standard_error(g_zfs, err,
1368 1368 "cannot destroy bookmark");
1369 1369 }
1370 1370
1371 1371 nvlist_free(cb.cb_nvl);
1372 1372
1373 1373 return (err);
1374 1374 } else {
1375 1375 /* Open the given dataset */
1376 1376 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1377 1377 return (1);
1378 1378
1379 1379 cb.cb_target = zhp;
1380 1380
1381 1381 /*
1382 1382 * Perform an explicit check for pools before going any further.
1383 1383 */
1384 1384 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1385 1385 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1386 1386 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1387 1387 "operation does not apply to pools\n"),
1388 1388 zfs_get_name(zhp));
1389 1389 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1390 1390 "%s' to destroy all datasets in the pool\n"),
1391 1391 zfs_get_name(zhp));
1392 1392 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1393 1393 "to destroy the pool itself\n"), zfs_get_name(zhp));
1394 1394 rv = 1;
1395 1395 goto out;
1396 1396 }
1397 1397
1398 1398 /*
1399 1399 * Check for any dependents and/or clones.
1400 1400 */
1401 1401 cb.cb_first = B_TRUE;
1402 1402 if (!cb.cb_doclones &&
1403 1403 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1404 1404 &cb) != 0) {
1405 1405 rv = 1;
1406 1406 goto out;
1407 1407 }
1408 1408
1409 1409 if (cb.cb_error) {
1410 1410 rv = 1;
1411 1411 goto out;
1412 1412 }
1413 1413
1414 1414 cb.cb_batchedsnaps = fnvlist_alloc();
1415 1415 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1416 1416 &cb) != 0) {
1417 1417 rv = 1;
1418 1418 goto out;
1419 1419 }
1420 1420
1421 1421 /*
1422 1422 * Do the real thing. The callback will close the
1423 1423 * handle regardless of whether it succeeds or not.
1424 1424 */
1425 1425 err = destroy_callback(zhp, &cb);
1426 1426 zhp = NULL;
1427 1427 if (err == 0) {
1428 1428 err = zfs_destroy_snaps_nvl(g_zfs,
1429 1429 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1430 1430 }
1431 1431 if (err != 0)
1432 1432 rv = 1;
1433 1433 }
1434 1434
1435 1435 out:
1436 1436 fnvlist_free(cb.cb_batchedsnaps);
1437 1437 fnvlist_free(cb.cb_nvl);
1438 1438 if (zhp != NULL)
1439 1439 zfs_close(zhp);
1440 1440 return (rv);
1441 1441 }
1442 1442
1443 1443 static boolean_t
1444 1444 is_recvd_column(zprop_get_cbdata_t *cbp)
1445 1445 {
1446 1446 int i;
1447 1447 zfs_get_column_t col;
1448 1448
1449 1449 for (i = 0; i < ZFS_GET_NCOLS &&
1450 1450 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1451 1451 if (col == GET_COL_RECVD)
1452 1452 return (B_TRUE);
1453 1453 return (B_FALSE);
1454 1454 }
1455 1455
1456 1456 /*
1457 1457 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1458 1458 * < all | property[,property]... > < fs | snap | vol > ...
1459 1459 *
1460 1460 * -r recurse over any child datasets
1461 1461 * -H scripted mode. Headers are stripped, and fields are separated
1462 1462 * by tabs instead of spaces.
1463 1463 * -o Set of fields to display. One of "name,property,value,
1464 1464 * received,source". Default is "name,property,value,source".
1465 1465 * "all" is an alias for all five.
1466 1466 * -s Set of sources to allow. One of
1467 1467 * "local,default,inherited,received,temporary,none". Default is
1468 1468 * all six.
1469 1469 * -p Display values in parsable (literal) format.
1470 1470 *
1471 1471 * Prints properties for the given datasets. The user can control which
1472 1472 * columns to display as well as which property types to allow.
1473 1473 */
1474 1474
1475 1475 /*
1476 1476 * Invoked to display the properties for a single dataset.
1477 1477 */
1478 1478 static int
1479 1479 get_callback(zfs_handle_t *zhp, void *data)
1480 1480 {
1481 1481 char buf[ZFS_MAXPROPLEN];
1482 1482 char rbuf[ZFS_MAXPROPLEN];
1483 1483 zprop_source_t sourcetype;
1484 1484 char source[ZFS_MAXNAMELEN];
1485 1485 zprop_get_cbdata_t *cbp = data;
1486 1486 nvlist_t *user_props = zfs_get_user_props(zhp);
1487 1487 zprop_list_t *pl = cbp->cb_proplist;
1488 1488 nvlist_t *propval;
1489 1489 char *strval;
1490 1490 char *sourceval;
1491 1491 boolean_t received = is_recvd_column(cbp);
1492 1492
1493 1493 for (; pl != NULL; pl = pl->pl_next) {
1494 1494 char *recvdval = NULL;
1495 1495 /*
1496 1496 * Skip the special fake placeholder. This will also skip over
1497 1497 * the name property when 'all' is specified.
1498 1498 */
1499 1499 if (pl->pl_prop == ZFS_PROP_NAME &&
1500 1500 pl == cbp->cb_proplist)
1501 1501 continue;
1502 1502
1503 1503 if (pl->pl_prop != ZPROP_INVAL) {
1504 1504 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1505 1505 sizeof (buf), &sourcetype, source,
1506 1506 sizeof (source),
1507 1507 cbp->cb_literal) != 0) {
1508 1508 if (pl->pl_all)
1509 1509 continue;
1510 1510 if (!zfs_prop_valid_for_type(pl->pl_prop,
1511 1511 ZFS_TYPE_DATASET)) {
1512 1512 (void) fprintf(stderr,
1513 1513 gettext("No such property '%s'\n"),
1514 1514 zfs_prop_to_name(pl->pl_prop));
1515 1515 continue;
1516 1516 }
1517 1517 sourcetype = ZPROP_SRC_NONE;
1518 1518 (void) strlcpy(buf, "-", sizeof (buf));
1519 1519 }
1520 1520
1521 1521 if (received && (zfs_prop_get_recvd(zhp,
1522 1522 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1523 1523 cbp->cb_literal) == 0))
1524 1524 recvdval = rbuf;
1525 1525
1526 1526 zprop_print_one_property(zfs_get_name(zhp), cbp,
1527 1527 zfs_prop_to_name(pl->pl_prop),
1528 1528 buf, sourcetype, source, recvdval);
1529 1529 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1530 1530 sourcetype = ZPROP_SRC_LOCAL;
1531 1531
1532 1532 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1533 1533 buf, sizeof (buf), cbp->cb_literal) != 0) {
1534 1534 sourcetype = ZPROP_SRC_NONE;
1535 1535 (void) strlcpy(buf, "-", sizeof (buf));
1536 1536 }
1537 1537
1538 1538 zprop_print_one_property(zfs_get_name(zhp), cbp,
1539 1539 pl->pl_user_prop, buf, sourcetype, source, NULL);
1540 1540 } else if (zfs_prop_written(pl->pl_user_prop)) {
1541 1541 sourcetype = ZPROP_SRC_LOCAL;
1542 1542
1543 1543 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1544 1544 buf, sizeof (buf), cbp->cb_literal) != 0) {
1545 1545 sourcetype = ZPROP_SRC_NONE;
1546 1546 (void) strlcpy(buf, "-", sizeof (buf));
1547 1547 }
1548 1548
1549 1549 zprop_print_one_property(zfs_get_name(zhp), cbp,
1550 1550 pl->pl_user_prop, buf, sourcetype, source, NULL);
1551 1551 } else {
1552 1552 if (nvlist_lookup_nvlist(user_props,
1553 1553 pl->pl_user_prop, &propval) != 0) {
1554 1554 if (pl->pl_all)
1555 1555 continue;
1556 1556 sourcetype = ZPROP_SRC_NONE;
1557 1557 strval = "-";
1558 1558 } else {
1559 1559 verify(nvlist_lookup_string(propval,
1560 1560 ZPROP_VALUE, &strval) == 0);
1561 1561 verify(nvlist_lookup_string(propval,
1562 1562 ZPROP_SOURCE, &sourceval) == 0);
1563 1563
1564 1564 if (strcmp(sourceval,
1565 1565 zfs_get_name(zhp)) == 0) {
1566 1566 sourcetype = ZPROP_SRC_LOCAL;
1567 1567 } else if (strcmp(sourceval,
1568 1568 ZPROP_SOURCE_VAL_RECVD) == 0) {
1569 1569 sourcetype = ZPROP_SRC_RECEIVED;
1570 1570 } else {
1571 1571 sourcetype = ZPROP_SRC_INHERITED;
1572 1572 (void) strlcpy(source,
1573 1573 sourceval, sizeof (source));
1574 1574 }
1575 1575 }
1576 1576
1577 1577 if (received && (zfs_prop_get_recvd(zhp,
1578 1578 pl->pl_user_prop, rbuf, sizeof (rbuf),
1579 1579 cbp->cb_literal) == 0))
1580 1580 recvdval = rbuf;
1581 1581
1582 1582 zprop_print_one_property(zfs_get_name(zhp), cbp,
1583 1583 pl->pl_user_prop, strval, sourcetype,
1584 1584 source, recvdval);
1585 1585 }
1586 1586 }
1587 1587
1588 1588 return (0);
1589 1589 }
1590 1590
1591 1591 static int
1592 1592 zfs_do_get(int argc, char **argv)
1593 1593 {
1594 1594 zprop_get_cbdata_t cb = { 0 };
1595 1595 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1596 1596 int types = ZFS_TYPE_DATASET;
1597 1597 char *value, *fields;
1598 1598 int ret = 0;
1599 1599 int limit = 0;
1600 1600 zprop_list_t fake_name = { 0 };
1601 1601
1602 1602 /*
1603 1603 * Set up default columns and sources.
1604 1604 */
1605 1605 cb.cb_sources = ZPROP_SRC_ALL;
1606 1606 cb.cb_columns[0] = GET_COL_NAME;
1607 1607 cb.cb_columns[1] = GET_COL_PROPERTY;
1608 1608 cb.cb_columns[2] = GET_COL_VALUE;
1609 1609 cb.cb_columns[3] = GET_COL_SOURCE;
1610 1610 cb.cb_type = ZFS_TYPE_DATASET;
1611 1611
1612 1612 /* check options */
1613 1613 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1614 1614 switch (c) {
1615 1615 case 'p':
1616 1616 cb.cb_literal = B_TRUE;
1617 1617 break;
1618 1618 case 'd':
1619 1619 limit = parse_depth(optarg, &flags);
1620 1620 break;
1621 1621 case 'r':
1622 1622 flags |= ZFS_ITER_RECURSE;
1623 1623 break;
1624 1624 case 'H':
1625 1625 cb.cb_scripted = B_TRUE;
1626 1626 break;
1627 1627 case ':':
1628 1628 (void) fprintf(stderr, gettext("missing argument for "
1629 1629 "'%c' option\n"), optopt);
1630 1630 usage(B_FALSE);
1631 1631 break;
1632 1632 case 'o':
1633 1633 /*
1634 1634 * Process the set of columns to display. We zero out
1635 1635 * the structure to give us a blank slate.
1636 1636 */
1637 1637 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1638 1638 i = 0;
1639 1639 while (*optarg != '\0') {
1640 1640 static char *col_subopts[] =
1641 1641 { "name", "property", "value", "received",
1642 1642 "source", "all", NULL };
1643 1643
1644 1644 if (i == ZFS_GET_NCOLS) {
1645 1645 (void) fprintf(stderr, gettext("too "
1646 1646 "many fields given to -o "
1647 1647 "option\n"));
1648 1648 usage(B_FALSE);
1649 1649 }
1650 1650
1651 1651 switch (getsubopt(&optarg, col_subopts,
1652 1652 &value)) {
1653 1653 case 0:
1654 1654 cb.cb_columns[i++] = GET_COL_NAME;
1655 1655 break;
1656 1656 case 1:
1657 1657 cb.cb_columns[i++] = GET_COL_PROPERTY;
1658 1658 break;
1659 1659 case 2:
1660 1660 cb.cb_columns[i++] = GET_COL_VALUE;
1661 1661 break;
1662 1662 case 3:
1663 1663 cb.cb_columns[i++] = GET_COL_RECVD;
1664 1664 flags |= ZFS_ITER_RECVD_PROPS;
1665 1665 break;
1666 1666 case 4:
1667 1667 cb.cb_columns[i++] = GET_COL_SOURCE;
1668 1668 break;
1669 1669 case 5:
1670 1670 if (i > 0) {
1671 1671 (void) fprintf(stderr,
1672 1672 gettext("\"all\" conflicts "
1673 1673 "with specific fields "
1674 1674 "given to -o option\n"));
1675 1675 usage(B_FALSE);
1676 1676 }
1677 1677 cb.cb_columns[0] = GET_COL_NAME;
1678 1678 cb.cb_columns[1] = GET_COL_PROPERTY;
1679 1679 cb.cb_columns[2] = GET_COL_VALUE;
1680 1680 cb.cb_columns[3] = GET_COL_RECVD;
1681 1681 cb.cb_columns[4] = GET_COL_SOURCE;
1682 1682 flags |= ZFS_ITER_RECVD_PROPS;
1683 1683 i = ZFS_GET_NCOLS;
1684 1684 break;
1685 1685 default:
1686 1686 (void) fprintf(stderr,
1687 1687 gettext("invalid column name "
1688 1688 "'%s'\n"), value);
1689 1689 usage(B_FALSE);
1690 1690 }
1691 1691 }
1692 1692 break;
1693 1693
1694 1694 case 's':
1695 1695 cb.cb_sources = 0;
1696 1696 while (*optarg != '\0') {
1697 1697 static char *source_subopts[] = {
1698 1698 "local", "default", "inherited",
1699 1699 "received", "temporary", "none",
1700 1700 NULL };
1701 1701
1702 1702 switch (getsubopt(&optarg, source_subopts,
1703 1703 &value)) {
1704 1704 case 0:
1705 1705 cb.cb_sources |= ZPROP_SRC_LOCAL;
1706 1706 break;
1707 1707 case 1:
1708 1708 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1709 1709 break;
1710 1710 case 2:
1711 1711 cb.cb_sources |= ZPROP_SRC_INHERITED;
1712 1712 break;
1713 1713 case 3:
1714 1714 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1715 1715 break;
1716 1716 case 4:
1717 1717 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1718 1718 break;
1719 1719 case 5:
1720 1720 cb.cb_sources |= ZPROP_SRC_NONE;
1721 1721 break;
1722 1722 default:
1723 1723 (void) fprintf(stderr,
1724 1724 gettext("invalid source "
1725 1725 "'%s'\n"), value);
1726 1726 usage(B_FALSE);
1727 1727 }
1728 1728 }
1729 1729 break;
1730 1730
1731 1731 case 't':
1732 1732 types = 0;
1733 1733 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1734 1734 while (*optarg != '\0') {
1735 1735 static char *type_subopts[] = { "filesystem",
1736 1736 "volume", "snapshot", "bookmark",
1737 1737 "all", NULL };
1738 1738
1739 1739 switch (getsubopt(&optarg, type_subopts,
1740 1740 &value)) {
1741 1741 case 0:
1742 1742 types |= ZFS_TYPE_FILESYSTEM;
1743 1743 break;
1744 1744 case 1:
1745 1745 types |= ZFS_TYPE_VOLUME;
1746 1746 break;
1747 1747 case 2:
1748 1748 types |= ZFS_TYPE_SNAPSHOT;
1749 1749 break;
1750 1750 case 3:
1751 1751 types |= ZFS_TYPE_BOOKMARK;
1752 1752 break;
1753 1753 case 4:
1754 1754 types = ZFS_TYPE_DATASET |
1755 1755 ZFS_TYPE_BOOKMARK;
1756 1756 break;
1757 1757
1758 1758 default:
1759 1759 (void) fprintf(stderr,
1760 1760 gettext("invalid type '%s'\n"),
1761 1761 value);
1762 1762 usage(B_FALSE);
1763 1763 }
1764 1764 }
1765 1765 break;
1766 1766
1767 1767 case '?':
1768 1768 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1769 1769 optopt);
1770 1770 usage(B_FALSE);
1771 1771 }
1772 1772 }
1773 1773
1774 1774 argc -= optind;
1775 1775 argv += optind;
1776 1776
1777 1777 if (argc < 1) {
1778 1778 (void) fprintf(stderr, gettext("missing property "
1779 1779 "argument\n"));
1780 1780 usage(B_FALSE);
1781 1781 }
1782 1782
1783 1783 fields = argv[0];
1784 1784
1785 1785 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1786 1786 != 0)
1787 1787 usage(B_FALSE);
1788 1788
1789 1789 argc--;
1790 1790 argv++;
1791 1791
1792 1792 /*
1793 1793 * As part of zfs_expand_proplist(), we keep track of the maximum column
1794 1794 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1795 1795 * need to know the maximum name length. However, the user likely did
1796 1796 * not specify 'name' as one of the properties to fetch, so we need to
1797 1797 * make sure we always include at least this property for
1798 1798 * print_get_headers() to work properly.
1799 1799 */
1800 1800 if (cb.cb_proplist != NULL) {
1801 1801 fake_name.pl_prop = ZFS_PROP_NAME;
1802 1802 fake_name.pl_width = strlen(gettext("NAME"));
1803 1803 fake_name.pl_next = cb.cb_proplist;
1804 1804 cb.cb_proplist = &fake_name;
1805 1805 }
1806 1806
1807 1807 cb.cb_first = B_TRUE;
1808 1808
1809 1809 /* run for each object */
1810 1810 ret = zfs_for_each(argc, argv, flags, types, NULL,
1811 1811 &cb.cb_proplist, limit, get_callback, &cb);
1812 1812
1813 1813 if (cb.cb_proplist == &fake_name)
1814 1814 zprop_free_list(fake_name.pl_next);
1815 1815 else
1816 1816 zprop_free_list(cb.cb_proplist);
1817 1817
1818 1818 return (ret);
1819 1819 }
1820 1820
1821 1821 /*
1822 1822 * inherit [-rS] <property> <fs|vol> ...
1823 1823 *
1824 1824 * -r Recurse over all children
1825 1825 * -S Revert to received value, if any
1826 1826 *
1827 1827 * For each dataset specified on the command line, inherit the given property
1828 1828 * from its parent. Inheriting a property at the pool level will cause it to
1829 1829 * use the default value. The '-r' flag will recurse over all children, and is
1830 1830 * useful for setting a property on a hierarchy-wide basis, regardless of any
1831 1831 * local modifications for each dataset.
1832 1832 */
1833 1833
1834 1834 typedef struct inherit_cbdata {
1835 1835 const char *cb_propname;
1836 1836 boolean_t cb_received;
1837 1837 } inherit_cbdata_t;
1838 1838
1839 1839 static int
1840 1840 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1841 1841 {
1842 1842 inherit_cbdata_t *cb = data;
1843 1843 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1844 1844
1845 1845 /*
1846 1846 * If we're doing it recursively, then ignore properties that
1847 1847 * are not valid for this type of dataset.
1848 1848 */
1849 1849 if (prop != ZPROP_INVAL &&
1850 1850 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1851 1851 return (0);
1852 1852
1853 1853 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1854 1854 }
1855 1855
1856 1856 static int
1857 1857 inherit_cb(zfs_handle_t *zhp, void *data)
1858 1858 {
1859 1859 inherit_cbdata_t *cb = data;
1860 1860
1861 1861 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1862 1862 }
1863 1863
1864 1864 static int
1865 1865 zfs_do_inherit(int argc, char **argv)
1866 1866 {
1867 1867 int c;
1868 1868 zfs_prop_t prop;
1869 1869 inherit_cbdata_t cb = { 0 };
1870 1870 char *propname;
1871 1871 int ret = 0;
1872 1872 int flags = 0;
1873 1873 boolean_t received = B_FALSE;
1874 1874
1875 1875 /* check options */
1876 1876 while ((c = getopt(argc, argv, "rS")) != -1) {
1877 1877 switch (c) {
1878 1878 case 'r':
1879 1879 flags |= ZFS_ITER_RECURSE;
1880 1880 break;
1881 1881 case 'S':
1882 1882 received = B_TRUE;
1883 1883 break;
1884 1884 case '?':
1885 1885 default:
1886 1886 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1887 1887 optopt);
1888 1888 usage(B_FALSE);
1889 1889 }
1890 1890 }
1891 1891
1892 1892 argc -= optind;
1893 1893 argv += optind;
1894 1894
1895 1895 /* check number of arguments */
1896 1896 if (argc < 1) {
1897 1897 (void) fprintf(stderr, gettext("missing property argument\n"));
1898 1898 usage(B_FALSE);
1899 1899 }
1900 1900 if (argc < 2) {
1901 1901 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1902 1902 usage(B_FALSE);
1903 1903 }
1904 1904
1905 1905 propname = argv[0];
1906 1906 argc--;
1907 1907 argv++;
1908 1908
1909 1909 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1910 1910 if (zfs_prop_readonly(prop)) {
1911 1911 (void) fprintf(stderr, gettext(
1912 1912 "%s property is read-only\n"),
1913 1913 propname);
1914 1914 return (1);
1915 1915 }
1916 1916 if (!zfs_prop_inheritable(prop) && !received) {
1917 1917 (void) fprintf(stderr, gettext("'%s' property cannot "
1918 1918 "be inherited\n"), propname);
1919 1919 if (prop == ZFS_PROP_QUOTA ||
1920 1920 prop == ZFS_PROP_RESERVATION ||
1921 1921 prop == ZFS_PROP_REFQUOTA ||
1922 1922 prop == ZFS_PROP_REFRESERVATION) {
1923 1923 (void) fprintf(stderr, gettext("use 'zfs set "
1924 1924 "%s=none' to clear\n"), propname);
1925 1925 (void) fprintf(stderr, gettext("use 'zfs "
1926 1926 "inherit -S %s' to revert to received "
1927 1927 "value\n"), propname);
1928 1928 }
1929 1929 return (1);
1930 1930 }
1931 1931 if (received && (prop == ZFS_PROP_VOLSIZE ||
1932 1932 prop == ZFS_PROP_VERSION)) {
1933 1933 (void) fprintf(stderr, gettext("'%s' property cannot "
1934 1934 "be reverted to a received value\n"), propname);
1935 1935 return (1);
1936 1936 }
1937 1937 } else if (!zfs_prop_user(propname)) {
1938 1938 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1939 1939 propname);
1940 1940 usage(B_FALSE);
1941 1941 }
1942 1942
1943 1943 cb.cb_propname = propname;
1944 1944 cb.cb_received = received;
1945 1945
1946 1946 if (flags & ZFS_ITER_RECURSE) {
1947 1947 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1948 1948 NULL, NULL, 0, inherit_recurse_cb, &cb);
1949 1949 } else {
1950 1950 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1951 1951 NULL, NULL, 0, inherit_cb, &cb);
1952 1952 }
1953 1953
1954 1954 return (ret);
1955 1955 }
1956 1956
1957 1957 typedef struct upgrade_cbdata {
1958 1958 uint64_t cb_numupgraded;
1959 1959 uint64_t cb_numsamegraded;
1960 1960 uint64_t cb_numfailed;
1961 1961 uint64_t cb_version;
1962 1962 boolean_t cb_newer;
1963 1963 boolean_t cb_foundone;
1964 1964 char cb_lastfs[ZFS_MAXNAMELEN];
1965 1965 } upgrade_cbdata_t;
1966 1966
1967 1967 static int
1968 1968 same_pool(zfs_handle_t *zhp, const char *name)
1969 1969 {
1970 1970 int len1 = strcspn(name, "/@");
1971 1971 const char *zhname = zfs_get_name(zhp);
1972 1972 int len2 = strcspn(zhname, "/@");
1973 1973
1974 1974 if (len1 != len2)
1975 1975 return (B_FALSE);
1976 1976 return (strncmp(name, zhname, len1) == 0);
1977 1977 }
1978 1978
1979 1979 static int
1980 1980 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1981 1981 {
1982 1982 upgrade_cbdata_t *cb = data;
1983 1983 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1984 1984
1985 1985 /* list if it's old/new */
1986 1986 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1987 1987 (cb->cb_newer && version > ZPL_VERSION)) {
1988 1988 char *str;
1989 1989 if (cb->cb_newer) {
1990 1990 str = gettext("The following filesystems are "
1991 1991 "formatted using a newer software version and\n"
1992 1992 "cannot be accessed on the current system.\n\n");
1993 1993 } else {
1994 1994 str = gettext("The following filesystems are "
1995 1995 "out of date, and can be upgraded. After being\n"
1996 1996 "upgraded, these filesystems (and any 'zfs send' "
1997 1997 "streams generated from\n"
1998 1998 "subsequent snapshots) will no longer be "
1999 1999 "accessible by older software versions.\n\n");
2000 2000 }
2001 2001
2002 2002 if (!cb->cb_foundone) {
2003 2003 (void) puts(str);
2004 2004 (void) printf(gettext("VER FILESYSTEM\n"));
2005 2005 (void) printf(gettext("--- ------------\n"));
2006 2006 cb->cb_foundone = B_TRUE;
2007 2007 }
2008 2008
2009 2009 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2010 2010 }
2011 2011
2012 2012 return (0);
2013 2013 }
2014 2014
2015 2015 static int
2016 2016 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2017 2017 {
2018 2018 upgrade_cbdata_t *cb = data;
2019 2019 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2020 2020 int needed_spa_version;
2021 2021 int spa_version;
2022 2022
2023 2023 if (zfs_spa_version(zhp, &spa_version) < 0)
2024 2024 return (-1);
2025 2025
2026 2026 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2027 2027
2028 2028 if (needed_spa_version < 0)
2029 2029 return (-1);
2030 2030
2031 2031 if (spa_version < needed_spa_version) {
2032 2032 /* can't upgrade */
2033 2033 (void) printf(gettext("%s: can not be "
2034 2034 "upgraded; the pool version needs to first "
2035 2035 "be upgraded\nto version %d\n\n"),
2036 2036 zfs_get_name(zhp), needed_spa_version);
2037 2037 cb->cb_numfailed++;
2038 2038 return (0);
2039 2039 }
2040 2040
2041 2041 /* upgrade */
2042 2042 if (version < cb->cb_version) {
2043 2043 char verstr[16];
2044 2044 (void) snprintf(verstr, sizeof (verstr),
2045 2045 "%llu", cb->cb_version);
2046 2046 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2047 2047 /*
2048 2048 * If they did "zfs upgrade -a", then we could
2049 2049 * be doing ioctls to different pools. We need
2050 2050 * to log this history once to each pool, and bypass
2051 2051 * the normal history logging that happens in main().
2052 2052 */
2053 2053 (void) zpool_log_history(g_zfs, history_str);
2054 2054 log_history = B_FALSE;
2055 2055 }
2056 2056 if (zfs_prop_set(zhp, "version", verstr) == 0)
2057 2057 cb->cb_numupgraded++;
2058 2058 else
2059 2059 cb->cb_numfailed++;
2060 2060 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2061 2061 } else if (version > cb->cb_version) {
2062 2062 /* can't downgrade */
2063 2063 (void) printf(gettext("%s: can not be downgraded; "
2064 2064 "it is already at version %u\n"),
2065 2065 zfs_get_name(zhp), version);
2066 2066 cb->cb_numfailed++;
2067 2067 } else {
2068 2068 cb->cb_numsamegraded++;
2069 2069 }
2070 2070 return (0);
2071 2071 }
2072 2072
2073 2073 /*
2074 2074 * zfs upgrade
2075 2075 * zfs upgrade -v
2076 2076 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2077 2077 */
2078 2078 static int
2079 2079 zfs_do_upgrade(int argc, char **argv)
2080 2080 {
2081 2081 boolean_t all = B_FALSE;
2082 2082 boolean_t showversions = B_FALSE;
2083 2083 int ret = 0;
2084 2084 upgrade_cbdata_t cb = { 0 };
2085 2085 char c;
2086 2086 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2087 2087
2088 2088 /* check options */
2089 2089 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2090 2090 switch (c) {
2091 2091 case 'r':
2092 2092 flags |= ZFS_ITER_RECURSE;
2093 2093 break;
2094 2094 case 'v':
2095 2095 showversions = B_TRUE;
2096 2096 break;
2097 2097 case 'V':
2098 2098 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2099 2099 optarg, &cb.cb_version) != 0) {
2100 2100 (void) fprintf(stderr,
2101 2101 gettext("invalid version %s\n"), optarg);
2102 2102 usage(B_FALSE);
2103 2103 }
2104 2104 break;
2105 2105 case 'a':
2106 2106 all = B_TRUE;
2107 2107 break;
2108 2108 case '?':
2109 2109 default:
2110 2110 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2111 2111 optopt);
2112 2112 usage(B_FALSE);
2113 2113 }
2114 2114 }
2115 2115
2116 2116 argc -= optind;
2117 2117 argv += optind;
2118 2118
2119 2119 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2120 2120 usage(B_FALSE);
2121 2121 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2122 2122 cb.cb_version || argc))
2123 2123 usage(B_FALSE);
2124 2124 if ((all || argc) && (showversions))
2125 2125 usage(B_FALSE);
2126 2126 if (all && argc)
2127 2127 usage(B_FALSE);
2128 2128
2129 2129 if (showversions) {
2130 2130 /* Show info on available versions. */
2131 2131 (void) printf(gettext("The following filesystem versions are "
2132 2132 "supported:\n\n"));
2133 2133 (void) printf(gettext("VER DESCRIPTION\n"));
2134 2134 (void) printf("--- -----------------------------------------"
2135 2135 "---------------\n");
2136 2136 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2137 2137 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2138 2138 (void) printf(gettext(" 3 Case insensitive and filesystem "
2139 2139 "user identifier (FUID)\n"));
2140 2140 (void) printf(gettext(" 4 userquota, groupquota "
2141 2141 "properties\n"));
2142 2142 (void) printf(gettext(" 5 System attributes\n"));
2143 2143 (void) printf(gettext("\nFor more information on a particular "
2144 2144 "version, including supported releases,\n"));
2145 2145 (void) printf("see the ZFS Administration Guide.\n\n");
2146 2146 ret = 0;
2147 2147 } else if (argc || all) {
2148 2148 /* Upgrade filesystems */
2149 2149 if (cb.cb_version == 0)
2150 2150 cb.cb_version = ZPL_VERSION;
2151 2151 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2152 2152 NULL, NULL, 0, upgrade_set_callback, &cb);
2153 2153 (void) printf(gettext("%llu filesystems upgraded\n"),
2154 2154 cb.cb_numupgraded);
2155 2155 if (cb.cb_numsamegraded) {
2156 2156 (void) printf(gettext("%llu filesystems already at "
2157 2157 "this version\n"),
2158 2158 cb.cb_numsamegraded);
2159 2159 }
2160 2160 if (cb.cb_numfailed != 0)
2161 2161 ret = 1;
2162 2162 } else {
2163 2163 /* List old-version filesytems */
2164 2164 boolean_t found;
2165 2165 (void) printf(gettext("This system is currently running "
2166 2166 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2167 2167
2168 2168 flags |= ZFS_ITER_RECURSE;
2169 2169 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2170 2170 NULL, NULL, 0, upgrade_list_callback, &cb);
2171 2171
2172 2172 found = cb.cb_foundone;
2173 2173 cb.cb_foundone = B_FALSE;
2174 2174 cb.cb_newer = B_TRUE;
2175 2175
2176 2176 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2177 2177 NULL, NULL, 0, upgrade_list_callback, &cb);
2178 2178
2179 2179 if (!cb.cb_foundone && !found) {
2180 2180 (void) printf(gettext("All filesystems are "
2181 2181 "formatted with the current version.\n"));
2182 2182 }
2183 2183 }
2184 2184
2185 2185 return (ret);
2186 2186 }
2187 2187
2188 2188 /*
2189 2189 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2190 2190 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2191 2191 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2192 2192 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2193 2193 *
2194 2194 * -H Scripted mode; elide headers and separate columns by tabs.
2195 2195 * -i Translate SID to POSIX ID.
2196 2196 * -n Print numeric ID instead of user/group name.
2197 2197 * -o Control which fields to display.
2198 2198 * -p Use exact (parsable) numeric output.
2199 2199 * -s Specify sort columns, descending order.
2200 2200 * -S Specify sort columns, ascending order.
2201 2201 * -t Control which object types to display.
2202 2202 *
2203 2203 * Displays space consumed by, and quotas on, each user in the specified
2204 2204 * filesystem or snapshot.
2205 2205 */
2206 2206
2207 2207 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2208 2208 enum us_field_types {
2209 2209 USFIELD_TYPE,
2210 2210 USFIELD_NAME,
2211 2211 USFIELD_USED,
2212 2212 USFIELD_QUOTA
2213 2213 };
2214 2214 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2215 2215 static char *us_field_names[] = { "type", "name", "used", "quota" };
2216 2216 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2217 2217
2218 2218 #define USTYPE_PSX_GRP (1 << 0)
2219 2219 #define USTYPE_PSX_USR (1 << 1)
2220 2220 #define USTYPE_SMB_GRP (1 << 2)
2221 2221 #define USTYPE_SMB_USR (1 << 3)
2222 2222 #define USTYPE_ALL \
2223 2223 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2224 2224
2225 2225 static int us_type_bits[] = {
2226 2226 USTYPE_PSX_GRP,
2227 2227 USTYPE_PSX_USR,
2228 2228 USTYPE_SMB_GRP,
2229 2229 USTYPE_SMB_USR,
2230 2230 USTYPE_ALL
2231 2231 };
2232 2232 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2233 2233 "smbuser", "all" };
2234 2234
2235 2235 typedef struct us_node {
2236 2236 nvlist_t *usn_nvl;
2237 2237 uu_avl_node_t usn_avlnode;
2238 2238 uu_list_node_t usn_listnode;
2239 2239 } us_node_t;
2240 2240
2241 2241 typedef struct us_cbdata {
2242 2242 nvlist_t **cb_nvlp;
2243 2243 uu_avl_pool_t *cb_avl_pool;
2244 2244 uu_avl_t *cb_avl;
2245 2245 boolean_t cb_numname;
2246 2246 boolean_t cb_nicenum;
2247 2247 boolean_t cb_sid2posix;
2248 2248 zfs_userquota_prop_t cb_prop;
2249 2249 zfs_sort_column_t *cb_sortcol;
2250 2250 size_t cb_width[USFIELD_LAST];
2251 2251 } us_cbdata_t;
2252 2252
2253 2253 static boolean_t us_populated = B_FALSE;
2254 2254
2255 2255 typedef struct {
2256 2256 zfs_sort_column_t *si_sortcol;
2257 2257 boolean_t si_numname;
2258 2258 } us_sort_info_t;
2259 2259
2260 2260 static int
2261 2261 us_field_index(char *field)
2262 2262 {
2263 2263 int i;
2264 2264
2265 2265 for (i = 0; i < USFIELD_LAST; i++) {
2266 2266 if (strcmp(field, us_field_names[i]) == 0)
2267 2267 return (i);
2268 2268 }
2269 2269
2270 2270 return (-1);
2271 2271 }
2272 2272
2273 2273 static int
2274 2274 us_compare(const void *larg, const void *rarg, void *unused)
2275 2275 {
2276 2276 const us_node_t *l = larg;
2277 2277 const us_node_t *r = rarg;
2278 2278 us_sort_info_t *si = (us_sort_info_t *)unused;
2279 2279 zfs_sort_column_t *sortcol = si->si_sortcol;
2280 2280 boolean_t numname = si->si_numname;
2281 2281 nvlist_t *lnvl = l->usn_nvl;
2282 2282 nvlist_t *rnvl = r->usn_nvl;
2283 2283 int rc = 0;
2284 2284 boolean_t lvb, rvb;
2285 2285
2286 2286 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2287 2287 char *lvstr = "";
2288 2288 char *rvstr = "";
2289 2289 uint32_t lv32 = 0;
2290 2290 uint32_t rv32 = 0;
2291 2291 uint64_t lv64 = 0;
2292 2292 uint64_t rv64 = 0;
2293 2293 zfs_prop_t prop = sortcol->sc_prop;
2294 2294 const char *propname = NULL;
2295 2295 boolean_t reverse = sortcol->sc_reverse;
2296 2296
2297 2297 switch (prop) {
2298 2298 case ZFS_PROP_TYPE:
2299 2299 propname = "type";
2300 2300 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2301 2301 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2302 2302 if (rv32 != lv32)
2303 2303 rc = (rv32 < lv32) ? 1 : -1;
2304 2304 break;
2305 2305 case ZFS_PROP_NAME:
2306 2306 propname = "name";
2307 2307 if (numname) {
2308 2308 (void) nvlist_lookup_uint64(lnvl, propname,
2309 2309 &lv64);
2310 2310 (void) nvlist_lookup_uint64(rnvl, propname,
2311 2311 &rv64);
2312 2312 if (rv64 != lv64)
2313 2313 rc = (rv64 < lv64) ? 1 : -1;
2314 2314 } else {
2315 2315 (void) nvlist_lookup_string(lnvl, propname,
2316 2316 &lvstr);
2317 2317 (void) nvlist_lookup_string(rnvl, propname,
2318 2318 &rvstr);
2319 2319 rc = strcmp(lvstr, rvstr);
2320 2320 }
2321 2321 break;
2322 2322 case ZFS_PROP_USED:
2323 2323 case ZFS_PROP_QUOTA:
2324 2324 if (!us_populated)
2325 2325 break;
2326 2326 if (prop == ZFS_PROP_USED)
2327 2327 propname = "used";
2328 2328 else
2329 2329 propname = "quota";
2330 2330 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2331 2331 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2332 2332 if (rv64 != lv64)
2333 2333 rc = (rv64 < lv64) ? 1 : -1;
2334 2334 break;
2335 2335
2336 2336 default:
2337 2337 break;
2338 2338 }
2339 2339
2340 2340 if (rc != 0) {
2341 2341 if (rc < 0)
2342 2342 return (reverse ? 1 : -1);
2343 2343 else
2344 2344 return (reverse ? -1 : 1);
2345 2345 }
2346 2346 }
2347 2347
2348 2348 /*
2349 2349 * If entries still seem to be the same, check if they are of the same
2350 2350 * type (smbentity is added only if we are doing SID to POSIX ID
2351 2351 * translation where we can have duplicate type/name combinations).
2352 2352 */
2353 2353 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2354 2354 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2355 2355 lvb != rvb)
2356 2356 return (lvb < rvb ? -1 : 1);
2357 2357
2358 2358 return (0);
2359 2359 }
2360 2360
2361 2361 static inline const char *
2362 2362 us_type2str(unsigned field_type)
2363 2363 {
2364 2364 switch (field_type) {
2365 2365 case USTYPE_PSX_USR:
2366 2366 return ("POSIX User");
2367 2367 case USTYPE_PSX_GRP:
2368 2368 return ("POSIX Group");
2369 2369 case USTYPE_SMB_USR:
2370 2370 return ("SMB User");
2371 2371 case USTYPE_SMB_GRP:
2372 2372 return ("SMB Group");
2373 2373 default:
2374 2374 return ("Undefined");
2375 2375 }
2376 2376 }
2377 2377
2378 2378 static int
2379 2379 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2380 2380 {
2381 2381 us_cbdata_t *cb = (us_cbdata_t *)arg;
2382 2382 zfs_userquota_prop_t prop = cb->cb_prop;
2383 2383 char *name = NULL;
2384 2384 char *propname;
2385 2385 char sizebuf[32];
2386 2386 us_node_t *node;
2387 2387 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2388 2388 uu_avl_t *avl = cb->cb_avl;
2389 2389 uu_avl_index_t idx;
2390 2390 nvlist_t *props;
2391 2391 us_node_t *n;
2392 2392 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2393 2393 unsigned type = 0;
2394 2394 const char *typestr;
2395 2395 size_t namelen;
2396 2396 size_t typelen;
2397 2397 size_t sizelen;
2398 2398 int typeidx, nameidx, sizeidx;
2399 2399 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2400 2400 boolean_t smbentity = B_FALSE;
2401 2401
2402 2402 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2403 2403 nomem();
2404 2404 node = safe_malloc(sizeof (us_node_t));
2405 2405 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2406 2406 node->usn_nvl = props;
2407 2407
2408 2408 if (domain != NULL && domain[0] != '\0') {
2409 2409 /* SMB */
2410 2410 char sid[ZFS_MAXNAMELEN + 32];
2411 2411 uid_t id;
2412 2412 int err;
2413 2413 int flag = IDMAP_REQ_FLG_USE_CACHE;
2414 2414
2415 2415 smbentity = B_TRUE;
2416 2416
2417 2417 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2418 2418
2419 2419 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2420 2420 type = USTYPE_SMB_GRP;
2421 2421 err = sid_to_id(sid, B_FALSE, &id);
2422 2422 } else {
2423 2423 type = USTYPE_SMB_USR;
2424 2424 err = sid_to_id(sid, B_TRUE, &id);
2425 2425 }
2426 2426
2427 2427 if (err == 0) {
2428 2428 rid = id;
2429 2429 if (!cb->cb_sid2posix) {
2430 2430 if (type == USTYPE_SMB_USR) {
2431 2431 (void) idmap_getwinnamebyuid(rid, flag,
2432 2432 &name, NULL);
2433 2433 } else {
2434 2434 (void) idmap_getwinnamebygid(rid, flag,
2435 2435 &name, NULL);
2436 2436 }
2437 2437 if (name == NULL)
2438 2438 name = sid;
2439 2439 }
2440 2440 }
2441 2441 }
2442 2442
2443 2443 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2444 2444 /* POSIX or -i */
2445 2445 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2446 2446 type = USTYPE_PSX_GRP;
2447 2447 if (!cb->cb_numname) {
2448 2448 struct group *g;
2449 2449
2450 2450 if ((g = getgrgid(rid)) != NULL)
2451 2451 name = g->gr_name;
2452 2452 }
2453 2453 } else {
2454 2454 type = USTYPE_PSX_USR;
2455 2455 if (!cb->cb_numname) {
2456 2456 struct passwd *p;
2457 2457
2458 2458 if ((p = getpwuid(rid)) != NULL)
2459 2459 name = p->pw_name;
2460 2460 }
2461 2461 }
2462 2462 }
2463 2463
2464 2464 /*
2465 2465 * Make sure that the type/name combination is unique when doing
2466 2466 * SID to POSIX ID translation (hence changing the type from SMB to
2467 2467 * POSIX).
2468 2468 */
2469 2469 if (cb->cb_sid2posix &&
2470 2470 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2471 2471 nomem();
2472 2472
2473 2473 /* Calculate/update width of TYPE field */
2474 2474 typestr = us_type2str(type);
2475 2475 typelen = strlen(gettext(typestr));
2476 2476 typeidx = us_field_index("type");
2477 2477 if (typelen > cb->cb_width[typeidx])
2478 2478 cb->cb_width[typeidx] = typelen;
2479 2479 if (nvlist_add_uint32(props, "type", type) != 0)
2480 2480 nomem();
2481 2481
2482 2482 /* Calculate/update width of NAME field */
2483 2483 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2484 2484 if (nvlist_add_uint64(props, "name", rid) != 0)
2485 2485 nomem();
2486 2486 namelen = snprintf(NULL, 0, "%u", rid);
2487 2487 } else {
2488 2488 if (nvlist_add_string(props, "name", name) != 0)
2489 2489 nomem();
2490 2490 namelen = strlen(name);
2491 2491 }
2492 2492 nameidx = us_field_index("name");
2493 2493 if (namelen > cb->cb_width[nameidx])
2494 2494 cb->cb_width[nameidx] = namelen;
2495 2495
2496 2496 /*
2497 2497 * Check if this type/name combination is in the list and update it;
2498 2498 * otherwise add new node to the list.
2499 2499 */
2500 2500 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2501 2501 uu_avl_insert(avl, node, idx);
2502 2502 } else {
2503 2503 nvlist_free(props);
2504 2504 free(node);
2505 2505 node = n;
2506 2506 props = node->usn_nvl;
2507 2507 }
2508 2508
2509 2509 /* Calculate/update width of USED/QUOTA fields */
2510 2510 if (cb->cb_nicenum)
2511 2511 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2512 2512 else
2513 2513 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2514 2514 sizelen = strlen(sizebuf);
2515 2515 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2516 2516 propname = "used";
2517 2517 if (!nvlist_exists(props, "quota"))
2518 2518 (void) nvlist_add_uint64(props, "quota", 0);
2519 2519 } else {
2520 2520 propname = "quota";
2521 2521 if (!nvlist_exists(props, "used"))
2522 2522 (void) nvlist_add_uint64(props, "used", 0);
2523 2523 }
2524 2524 sizeidx = us_field_index(propname);
2525 2525 if (sizelen > cb->cb_width[sizeidx])
2526 2526 cb->cb_width[sizeidx] = sizelen;
2527 2527
2528 2528 if (nvlist_add_uint64(props, propname, space) != 0)
2529 2529 nomem();
2530 2530
2531 2531 return (0);
2532 2532 }
2533 2533
2534 2534 static void
2535 2535 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2536 2536 size_t *width, us_node_t *node)
2537 2537 {
2538 2538 nvlist_t *nvl = node->usn_nvl;
2539 2539 char valstr[ZFS_MAXNAMELEN];
2540 2540 boolean_t first = B_TRUE;
2541 2541 int cfield = 0;
2542 2542 int field;
2543 2543 uint32_t ustype;
2544 2544
2545 2545 /* Check type */
2546 2546 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2547 2547 if (!(ustype & types))
2548 2548 return;
2549 2549
2550 2550 while ((field = fields[cfield]) != USFIELD_LAST) {
2551 2551 nvpair_t *nvp = NULL;
2552 2552 data_type_t type;
2553 2553 uint32_t val32;
2554 2554 uint64_t val64;
2555 2555 char *strval = NULL;
2556 2556
2557 2557 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2558 2558 if (strcmp(nvpair_name(nvp),
2559 2559 us_field_names[field]) == 0)
2560 2560 break;
2561 2561 }
2562 2562
2563 2563 type = nvpair_type(nvp);
2564 2564 switch (type) {
2565 2565 case DATA_TYPE_UINT32:
2566 2566 (void) nvpair_value_uint32(nvp, &val32);
2567 2567 break;
2568 2568 case DATA_TYPE_UINT64:
2569 2569 (void) nvpair_value_uint64(nvp, &val64);
2570 2570 break;
2571 2571 case DATA_TYPE_STRING:
2572 2572 (void) nvpair_value_string(nvp, &strval);
2573 2573 break;
2574 2574 default:
2575 2575 (void) fprintf(stderr, "invalid data type\n");
2576 2576 }
2577 2577
2578 2578 switch (field) {
2579 2579 case USFIELD_TYPE:
2580 2580 strval = (char *)us_type2str(val32);
2581 2581 break;
2582 2582 case USFIELD_NAME:
2583 2583 if (type == DATA_TYPE_UINT64) {
2584 2584 (void) sprintf(valstr, "%llu", val64);
2585 2585 strval = valstr;
2586 2586 }
2587 2587 break;
2588 2588 case USFIELD_USED:
2589 2589 case USFIELD_QUOTA:
2590 2590 if (type == DATA_TYPE_UINT64) {
2591 2591 if (parsable) {
2592 2592 (void) sprintf(valstr, "%llu", val64);
2593 2593 } else {
2594 2594 zfs_nicenum(val64, valstr,
2595 2595 sizeof (valstr));
2596 2596 }
2597 2597 if (field == USFIELD_QUOTA &&
2598 2598 strcmp(valstr, "0") == 0)
2599 2599 strval = "none";
2600 2600 else
2601 2601 strval = valstr;
2602 2602 }
2603 2603 break;
2604 2604 }
2605 2605
2606 2606 if (!first) {
2607 2607 if (scripted)
2608 2608 (void) printf("\t");
2609 2609 else
2610 2610 (void) printf(" ");
2611 2611 }
2612 2612 if (scripted)
2613 2613 (void) printf("%s", strval);
2614 2614 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2615 2615 (void) printf("%-*s", width[field], strval);
2616 2616 else
2617 2617 (void) printf("%*s", width[field], strval);
2618 2618
2619 2619 first = B_FALSE;
2620 2620 cfield++;
2621 2621 }
2622 2622
2623 2623 (void) printf("\n");
2624 2624 }
2625 2625
2626 2626 static void
2627 2627 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2628 2628 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2629 2629 {
2630 2630 us_node_t *node;
2631 2631 const char *col;
2632 2632 int cfield = 0;
2633 2633 int field;
2634 2634
2635 2635 if (!scripted) {
2636 2636 boolean_t first = B_TRUE;
2637 2637
2638 2638 while ((field = fields[cfield]) != USFIELD_LAST) {
2639 2639 col = gettext(us_field_hdr[field]);
2640 2640 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2641 2641 (void) printf(first ? "%-*s" : " %-*s",
2642 2642 width[field], col);
2643 2643 } else {
2644 2644 (void) printf(first ? "%*s" : " %*s",
2645 2645 width[field], col);
2646 2646 }
2647 2647 first = B_FALSE;
2648 2648 cfield++;
2649 2649 }
2650 2650 (void) printf("\n");
2651 2651 }
2652 2652
2653 2653 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2654 2654 print_us_node(scripted, parsable, fields, types, width, node);
2655 2655 if (rmnode)
2656 2656 nvlist_free(node->usn_nvl);
2657 2657 }
2658 2658 }
2659 2659
2660 2660 static int
2661 2661 zfs_do_userspace(int argc, char **argv)
2662 2662 {
2663 2663 zfs_handle_t *zhp;
2664 2664 zfs_userquota_prop_t p;
2665 2665 uu_avl_pool_t *avl_pool;
2666 2666 uu_avl_t *avl_tree;
2667 2667 uu_avl_walk_t *walk;
2668 2668 char *delim;
2669 2669 char deffields[] = "type,name,used,quota";
2670 2670 char *ofield = NULL;
2671 2671 char *tfield = NULL;
2672 2672 int cfield = 0;
2673 2673 int fields[256];
2674 2674 int i;
2675 2675 boolean_t scripted = B_FALSE;
2676 2676 boolean_t prtnum = B_FALSE;
2677 2677 boolean_t parsable = B_FALSE;
2678 2678 boolean_t sid2posix = B_FALSE;
2679 2679 int ret = 0;
2680 2680 int c;
2681 2681 zfs_sort_column_t *sortcol = NULL;
2682 2682 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2683 2683 us_cbdata_t cb;
2684 2684 us_node_t *node;
2685 2685 us_node_t *rmnode;
2686 2686 uu_list_pool_t *listpool;
2687 2687 uu_list_t *list;
2688 2688 uu_avl_index_t idx = 0;
2689 2689 uu_list_index_t idx2 = 0;
2690 2690
2691 2691 if (argc < 2)
2692 2692 usage(B_FALSE);
2693 2693
2694 2694 if (strcmp(argv[0], "groupspace") == 0)
2695 2695 /* Toggle default group types */
2696 2696 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2697 2697
2698 2698 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2699 2699 switch (c) {
2700 2700 case 'n':
2701 2701 prtnum = B_TRUE;
2702 2702 break;
2703 2703 case 'H':
2704 2704 scripted = B_TRUE;
2705 2705 break;
2706 2706 case 'p':
2707 2707 parsable = B_TRUE;
2708 2708 break;
2709 2709 case 'o':
2710 2710 ofield = optarg;
2711 2711 break;
2712 2712 case 's':
2713 2713 case 'S':
2714 2714 if (zfs_add_sort_column(&sortcol, optarg,
2715 2715 c == 's' ? B_FALSE : B_TRUE) != 0) {
2716 2716 (void) fprintf(stderr,
2717 2717 gettext("invalid field '%s'\n"), optarg);
2718 2718 usage(B_FALSE);
2719 2719 }
2720 2720 break;
2721 2721 case 't':
2722 2722 tfield = optarg;
2723 2723 break;
2724 2724 case 'i':
2725 2725 sid2posix = B_TRUE;
2726 2726 break;
2727 2727 case ':':
2728 2728 (void) fprintf(stderr, gettext("missing argument for "
2729 2729 "'%c' option\n"), optopt);
2730 2730 usage(B_FALSE);
2731 2731 break;
2732 2732 case '?':
2733 2733 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2734 2734 optopt);
2735 2735 usage(B_FALSE);
2736 2736 }
2737 2737 }
2738 2738
2739 2739 argc -= optind;
2740 2740 argv += optind;
2741 2741
2742 2742 if (argc < 1) {
2743 2743 (void) fprintf(stderr, gettext("missing dataset name\n"));
2744 2744 usage(B_FALSE);
2745 2745 }
2746 2746 if (argc > 1) {
2747 2747 (void) fprintf(stderr, gettext("too many arguments\n"));
2748 2748 usage(B_FALSE);
2749 2749 }
2750 2750
2751 2751 /* Use default output fields if not specified using -o */
2752 2752 if (ofield == NULL)
2753 2753 ofield = deffields;
2754 2754 do {
2755 2755 if ((delim = strchr(ofield, ',')) != NULL)
2756 2756 *delim = '\0';
2757 2757 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2758 2758 (void) fprintf(stderr, gettext("invalid type '%s' "
2759 2759 "for -o option\n"), ofield);
2760 2760 return (-1);
2761 2761 }
2762 2762 if (delim != NULL)
2763 2763 ofield = delim + 1;
2764 2764 } while (delim != NULL);
2765 2765 fields[cfield] = USFIELD_LAST;
2766 2766
2767 2767 /* Override output types (-t option) */
2768 2768 if (tfield != NULL) {
2769 2769 types = 0;
2770 2770
2771 2771 do {
2772 2772 boolean_t found = B_FALSE;
2773 2773
2774 2774 if ((delim = strchr(tfield, ',')) != NULL)
2775 2775 *delim = '\0';
2776 2776 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2777 2777 i++) {
2778 2778 if (strcmp(tfield, us_type_names[i]) == 0) {
2779 2779 found = B_TRUE;
2780 2780 types |= us_type_bits[i];
2781 2781 break;
2782 2782 }
2783 2783 }
2784 2784 if (!found) {
2785 2785 (void) fprintf(stderr, gettext("invalid type "
2786 2786 "'%s' for -t option\n"), tfield);
2787 2787 return (-1);
2788 2788 }
2789 2789 if (delim != NULL)
2790 2790 tfield = delim + 1;
2791 2791 } while (delim != NULL);
2792 2792 }
2793 2793
2794 2794 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2795 2795 return (1);
2796 2796
2797 2797 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2798 2798 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2799 2799 nomem();
2800 2800 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2801 2801 nomem();
2802 2802
2803 2803 /* Always add default sorting columns */
2804 2804 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2805 2805 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2806 2806
2807 2807 cb.cb_sortcol = sortcol;
2808 2808 cb.cb_numname = prtnum;
2809 2809 cb.cb_nicenum = !parsable;
2810 2810 cb.cb_avl_pool = avl_pool;
2811 2811 cb.cb_avl = avl_tree;
2812 2812 cb.cb_sid2posix = sid2posix;
2813 2813
2814 2814 for (i = 0; i < USFIELD_LAST; i++)
2815 2815 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2816 2816
2817 2817 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2818 2818 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2819 2819 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2820 2820 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2821 2821 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2822 2822 continue;
2823 2823 cb.cb_prop = p;
2824 2824 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2825 2825 return (ret);
2826 2826 }
2827 2827
2828 2828 /* Sort the list */
2829 2829 if ((node = uu_avl_first(avl_tree)) == NULL)
2830 2830 return (0);
2831 2831
2832 2832 us_populated = B_TRUE;
2833 2833
2834 2834 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2835 2835 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2836 2836 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2837 2837 uu_list_node_init(node, &node->usn_listnode, listpool);
2838 2838
2839 2839 while (node != NULL) {
2840 2840 rmnode = node;
2841 2841 node = uu_avl_next(avl_tree, node);
2842 2842 uu_avl_remove(avl_tree, rmnode);
2843 2843 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2844 2844 uu_list_insert(list, rmnode, idx2);
2845 2845 }
2846 2846
2847 2847 for (node = uu_list_first(list); node != NULL;
2848 2848 node = uu_list_next(list, node)) {
2849 2849 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2850 2850
2851 2851 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2852 2852 uu_avl_insert(avl_tree, node, idx);
2853 2853 }
2854 2854
2855 2855 uu_list_destroy(list);
2856 2856 uu_list_pool_destroy(listpool);
2857 2857
2858 2858 /* Print and free node nvlist memory */
2859 2859 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2860 2860 cb.cb_avl);
2861 2861
2862 2862 zfs_free_sort_columns(sortcol);
2863 2863
2864 2864 /* Clean up the AVL tree */
2865 2865 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2866 2866 nomem();
2867 2867
2868 2868 while ((node = uu_avl_walk_next(walk)) != NULL) {
2869 2869 uu_avl_remove(cb.cb_avl, node);
2870 2870 free(node);
2871 2871 }
2872 2872
2873 2873 uu_avl_walk_end(walk);
2874 2874 uu_avl_destroy(avl_tree);
2875 2875 uu_avl_pool_destroy(avl_pool);
2876 2876
2877 2877 return (ret);
2878 2878 }
2879 2879
2880 2880 /*
2881 2881 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2882 2882 * [-t type[,...]] [filesystem|volume|snapshot] ...
2883 2883 *
2884 2884 * -H Scripted mode; elide headers and separate columns by tabs.
2885 2885 * -p Display values in parsable (literal) format.
2886 2886 * -r Recurse over all children.
2887 2887 * -d Limit recursion by depth.
2888 2888 * -o Control which fields to display.
2889 2889 * -s Specify sort columns, descending order.
2890 2890 * -S Specify sort columns, ascending order.
2891 2891 * -t Control which object types to display.
2892 2892 *
2893 2893 * When given no arguments, list all filesystems in the system.
2894 2894 * Otherwise, list the specified datasets, optionally recursing down them if
2895 2895 * '-r' is specified.
2896 2896 */
2897 2897 typedef struct list_cbdata {
2898 2898 boolean_t cb_first;
2899 2899 boolean_t cb_literal;
2900 2900 boolean_t cb_scripted;
2901 2901 zprop_list_t *cb_proplist;
2902 2902 } list_cbdata_t;
2903 2903
2904 2904 /*
2905 2905 * Given a list of columns to display, output appropriate headers for each one.
2906 2906 */
2907 2907 static void
2908 2908 print_header(list_cbdata_t *cb)
2909 2909 {
2910 2910 zprop_list_t *pl = cb->cb_proplist;
2911 2911 char headerbuf[ZFS_MAXPROPLEN];
2912 2912 const char *header;
2913 2913 int i;
2914 2914 boolean_t first = B_TRUE;
2915 2915 boolean_t right_justify;
2916 2916
2917 2917 for (; pl != NULL; pl = pl->pl_next) {
2918 2918 if (!first) {
2919 2919 (void) printf(" ");
2920 2920 } else {
2921 2921 first = B_FALSE;
2922 2922 }
2923 2923
2924 2924 right_justify = B_FALSE;
2925 2925 if (pl->pl_prop != ZPROP_INVAL) {
2926 2926 header = zfs_prop_column_name(pl->pl_prop);
2927 2927 right_justify = zfs_prop_align_right(pl->pl_prop);
2928 2928 } else {
2929 2929 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2930 2930 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2931 2931 headerbuf[i] = '\0';
2932 2932 header = headerbuf;
2933 2933 }
2934 2934
2935 2935 if (pl->pl_next == NULL && !right_justify)
2936 2936 (void) printf("%s", header);
2937 2937 else if (right_justify)
2938 2938 (void) printf("%*s", pl->pl_width, header);
2939 2939 else
2940 2940 (void) printf("%-*s", pl->pl_width, header);
2941 2941 }
2942 2942
2943 2943 (void) printf("\n");
2944 2944 }
2945 2945
2946 2946 /*
2947 2947 * Given a dataset and a list of fields, print out all the properties according
2948 2948 * to the described layout.
2949 2949 */
2950 2950 static void
2951 2951 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2952 2952 {
2953 2953 zprop_list_t *pl = cb->cb_proplist;
2954 2954 boolean_t first = B_TRUE;
2955 2955 char property[ZFS_MAXPROPLEN];
2956 2956 nvlist_t *userprops = zfs_get_user_props(zhp);
2957 2957 nvlist_t *propval;
2958 2958 char *propstr;
2959 2959 boolean_t right_justify;
2960 2960
2961 2961 for (; pl != NULL; pl = pl->pl_next) {
2962 2962 if (!first) {
2963 2963 if (cb->cb_scripted)
2964 2964 (void) printf("\t");
2965 2965 else
2966 2966 (void) printf(" ");
2967 2967 } else {
2968 2968 first = B_FALSE;
2969 2969 }
2970 2970
2971 2971 if (pl->pl_prop != ZPROP_INVAL) {
2972 2972 if (zfs_prop_get(zhp, pl->pl_prop, property,
2973 2973 sizeof (property), NULL, NULL, 0,
2974 2974 cb->cb_literal) != 0)
2975 2975 propstr = "-";
2976 2976 else
2977 2977 propstr = property;
2978 2978 right_justify = zfs_prop_align_right(pl->pl_prop);
2979 2979 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2980 2980 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2981 2981 property, sizeof (property), cb->cb_literal) != 0)
2982 2982 propstr = "-";
2983 2983 else
2984 2984 propstr = property;
2985 2985 right_justify = B_TRUE;
2986 2986 } else if (zfs_prop_written(pl->pl_user_prop)) {
2987 2987 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2988 2988 property, sizeof (property), cb->cb_literal) != 0)
2989 2989 propstr = "-";
2990 2990 else
2991 2991 propstr = property;
2992 2992 right_justify = B_TRUE;
2993 2993 } else {
2994 2994 if (nvlist_lookup_nvlist(userprops,
2995 2995 pl->pl_user_prop, &propval) != 0)
2996 2996 propstr = "-";
2997 2997 else
2998 2998 verify(nvlist_lookup_string(propval,
2999 2999 ZPROP_VALUE, &propstr) == 0);
3000 3000 right_justify = B_FALSE;
3001 3001 }
3002 3002
3003 3003 /*
3004 3004 * If this is being called in scripted mode, or if this is the
3005 3005 * last column and it is left-justified, don't include a width
3006 3006 * format specifier.
3007 3007 */
3008 3008 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3009 3009 (void) printf("%s", propstr);
3010 3010 else if (right_justify)
3011 3011 (void) printf("%*s", pl->pl_width, propstr);
3012 3012 else
3013 3013 (void) printf("%-*s", pl->pl_width, propstr);
3014 3014 }
3015 3015
3016 3016 (void) printf("\n");
3017 3017 }
3018 3018
3019 3019 /*
3020 3020 * Generic callback function to list a dataset or snapshot.
3021 3021 */
3022 3022 static int
3023 3023 list_callback(zfs_handle_t *zhp, void *data)
3024 3024 {
3025 3025 list_cbdata_t *cbp = data;
3026 3026
3027 3027 if (cbp->cb_first) {
3028 3028 if (!cbp->cb_scripted)
3029 3029 print_header(cbp);
3030 3030 cbp->cb_first = B_FALSE;
3031 3031 }
3032 3032
3033 3033 print_dataset(zhp, cbp);
3034 3034
3035 3035 return (0);
3036 3036 }
3037 3037
3038 3038 static int
3039 3039 zfs_do_list(int argc, char **argv)
3040 3040 {
3041 3041 int c;
3042 3042 static char default_fields[] =
3043 3043 "name,used,available,referenced,mountpoint";
3044 3044 int types = ZFS_TYPE_DATASET;
3045 3045 boolean_t types_specified = B_FALSE;
3046 3046 char *fields = NULL;
3047 3047 list_cbdata_t cb = { 0 };
3048 3048 char *value;
3049 3049 int limit = 0;
3050 3050 int ret = 0;
3051 3051 zfs_sort_column_t *sortcol = NULL;
3052 3052 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3053 3053
3054 3054 /* check options */
3055 3055 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3056 3056 switch (c) {
3057 3057 case 'o':
3058 3058 fields = optarg;
3059 3059 break;
3060 3060 case 'p':
3061 3061 cb.cb_literal = B_TRUE;
3062 3062 flags |= ZFS_ITER_LITERAL_PROPS;
3063 3063 break;
3064 3064 case 'd':
3065 3065 limit = parse_depth(optarg, &flags);
3066 3066 break;
3067 3067 case 'r':
3068 3068 flags |= ZFS_ITER_RECURSE;
3069 3069 break;
3070 3070 case 'H':
3071 3071 cb.cb_scripted = B_TRUE;
3072 3072 break;
3073 3073 case 's':
3074 3074 if (zfs_add_sort_column(&sortcol, optarg,
3075 3075 B_FALSE) != 0) {
3076 3076 (void) fprintf(stderr,
3077 3077 gettext("invalid property '%s'\n"), optarg);
3078 3078 usage(B_FALSE);
3079 3079 }
3080 3080 break;
3081 3081 case 'S':
3082 3082 if (zfs_add_sort_column(&sortcol, optarg,
3083 3083 B_TRUE) != 0) {
3084 3084 (void) fprintf(stderr,
3085 3085 gettext("invalid property '%s'\n"), optarg);
3086 3086 usage(B_FALSE);
3087 3087 }
3088 3088 break;
3089 3089 case 't':
3090 3090 types = 0;
3091 3091 types_specified = B_TRUE;
3092 3092 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3093 3093 while (*optarg != '\0') {
3094 3094 static char *type_subopts[] = { "filesystem",
3095 3095 "volume", "snapshot", "snap", "bookmark",
3096 3096 "all", NULL };
3097 3097
3098 3098 switch (getsubopt(&optarg, type_subopts,
3099 3099 &value)) {
3100 3100 case 0:
3101 3101 types |= ZFS_TYPE_FILESYSTEM;
3102 3102 break;
3103 3103 case 1:
3104 3104 types |= ZFS_TYPE_VOLUME;
3105 3105 break;
3106 3106 case 2:
3107 3107 case 3:
3108 3108 types |= ZFS_TYPE_SNAPSHOT;
3109 3109 break;
3110 3110 case 4:
3111 3111 types |= ZFS_TYPE_BOOKMARK;
3112 3112 break;
3113 3113 case 5:
3114 3114 types = ZFS_TYPE_DATASET |
3115 3115 ZFS_TYPE_BOOKMARK;
3116 3116 break;
3117 3117 default:
3118 3118 (void) fprintf(stderr,
3119 3119 gettext("invalid type '%s'\n"),
3120 3120 value);
3121 3121 usage(B_FALSE);
3122 3122 }
3123 3123 }
3124 3124 break;
3125 3125 case ':':
3126 3126 (void) fprintf(stderr, gettext("missing argument for "
3127 3127 "'%c' option\n"), optopt);
3128 3128 usage(B_FALSE);
3129 3129 break;
3130 3130 case '?':
3131 3131 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3132 3132 optopt);
3133 3133 usage(B_FALSE);
3134 3134 }
3135 3135 }
3136 3136
3137 3137 argc -= optind;
3138 3138 argv += optind;
3139 3139
3140 3140 if (fields == NULL)
3141 3141 fields = default_fields;
3142 3142
3143 3143 /*
3144 3144 * If "-o space" and no types were specified, don't display snapshots.
3145 3145 */
3146 3146 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3147 3147 types &= ~ZFS_TYPE_SNAPSHOT;
3148 3148
3149 3149 /*
3150 3150 * If the user specifies '-o all', the zprop_get_list() doesn't
3151 3151 * normally include the name of the dataset. For 'zfs list', we always
3152 3152 * want this property to be first.
3153 3153 */
3154 3154 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3155 3155 != 0)
3156 3156 usage(B_FALSE);
3157 3157
3158 3158 cb.cb_first = B_TRUE;
3159 3159
3160 3160 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3161 3161 limit, list_callback, &cb);
3162 3162
3163 3163 zprop_free_list(cb.cb_proplist);
3164 3164 zfs_free_sort_columns(sortcol);
3165 3165
3166 3166 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3167 3167 (void) printf(gettext("no datasets available\n"));
3168 3168
3169 3169 return (ret);
3170 3170 }
3171 3171
3172 3172 /*
3173 3173 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3174 3174 * zfs rename [-f] -p <fs | vol> <fs | vol>
3175 3175 * zfs rename -r <snap> <snap>
3176 3176 *
3177 3177 * Renames the given dataset to another of the same type.
3178 3178 *
3179 3179 * The '-p' flag creates all the non-existing ancestors of the target first.
3180 3180 */
3181 3181 /* ARGSUSED */
3182 3182 static int
3183 3183 zfs_do_rename(int argc, char **argv)
3184 3184 {
3185 3185 zfs_handle_t *zhp;
3186 3186 int c;
3187 3187 int ret = 0;
3188 3188 boolean_t recurse = B_FALSE;
3189 3189 boolean_t parents = B_FALSE;
3190 3190 boolean_t force_unmount = B_FALSE;
3191 3191
3192 3192 /* check options */
3193 3193 while ((c = getopt(argc, argv, "prf")) != -1) {
3194 3194 switch (c) {
3195 3195 case 'p':
3196 3196 parents = B_TRUE;
3197 3197 break;
3198 3198 case 'r':
3199 3199 recurse = B_TRUE;
3200 3200 break;
3201 3201 case 'f':
3202 3202 force_unmount = B_TRUE;
3203 3203 break;
3204 3204 case '?':
3205 3205 default:
3206 3206 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3207 3207 optopt);
3208 3208 usage(B_FALSE);
3209 3209 }
3210 3210 }
3211 3211
3212 3212 argc -= optind;
3213 3213 argv += optind;
3214 3214
3215 3215 /* check number of arguments */
3216 3216 if (argc < 1) {
3217 3217 (void) fprintf(stderr, gettext("missing source dataset "
3218 3218 "argument\n"));
3219 3219 usage(B_FALSE);
3220 3220 }
3221 3221 if (argc < 2) {
3222 3222 (void) fprintf(stderr, gettext("missing target dataset "
3223 3223 "argument\n"));
3224 3224 usage(B_FALSE);
3225 3225 }
3226 3226 if (argc > 2) {
3227 3227 (void) fprintf(stderr, gettext("too many arguments\n"));
3228 3228 usage(B_FALSE);
3229 3229 }
3230 3230
3231 3231 if (recurse && parents) {
3232 3232 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3233 3233 "exclusive\n"));
3234 3234 usage(B_FALSE);
3235 3235 }
3236 3236
3237 3237 if (recurse && strchr(argv[0], '@') == 0) {
3238 3238 (void) fprintf(stderr, gettext("source dataset for recursive "
3239 3239 "rename must be a snapshot\n"));
3240 3240 usage(B_FALSE);
3241 3241 }
3242 3242
3243 3243 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3244 3244 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3245 3245 return (1);
3246 3246
3247 3247 /* If we were asked and the name looks good, try to create ancestors. */
3248 3248 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3249 3249 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3250 3250 zfs_close(zhp);
3251 3251 return (1);
3252 3252 }
3253 3253
3254 3254 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3255 3255
3256 3256 zfs_close(zhp);
3257 3257 return (ret);
3258 3258 }
3259 3259
3260 3260 /*
3261 3261 * zfs promote <fs>
3262 3262 *
3263 3263 * Promotes the given clone fs to be the parent
3264 3264 */
3265 3265 /* ARGSUSED */
3266 3266 static int
3267 3267 zfs_do_promote(int argc, char **argv)
3268 3268 {
3269 3269 zfs_handle_t *zhp;
3270 3270 int ret = 0;
3271 3271
3272 3272 /* check options */
3273 3273 if (argc > 1 && argv[1][0] == '-') {
3274 3274 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3275 3275 argv[1][1]);
3276 3276 usage(B_FALSE);
3277 3277 }
3278 3278
3279 3279 /* check number of arguments */
3280 3280 if (argc < 2) {
3281 3281 (void) fprintf(stderr, gettext("missing clone filesystem"
3282 3282 " argument\n"));
3283 3283 usage(B_FALSE);
3284 3284 }
3285 3285 if (argc > 2) {
3286 3286 (void) fprintf(stderr, gettext("too many arguments\n"));
3287 3287 usage(B_FALSE);
3288 3288 }
3289 3289
3290 3290 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3291 3291 if (zhp == NULL)
3292 3292 return (1);
3293 3293
3294 3294 ret = (zfs_promote(zhp) != 0);
3295 3295
3296 3296
3297 3297 zfs_close(zhp);
3298 3298 return (ret);
3299 3299 }
3300 3300
3301 3301 /*
3302 3302 * zfs rollback [-rRf] <snapshot>
3303 3303 *
3304 3304 * -r Delete any intervening snapshots before doing rollback
3305 3305 * -R Delete any snapshots and their clones
3306 3306 * -f ignored for backwards compatability
3307 3307 *
3308 3308 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3309 3309 * since then and making it the active dataset. If more recent snapshots exist,
3310 3310 * the command will complain unless the '-r' flag is given.
3311 3311 */
3312 3312 typedef struct rollback_cbdata {
3313 3313 uint64_t cb_create;
3314 3314 boolean_t cb_first;
3315 3315 int cb_doclones;
3316 3316 char *cb_target;
3317 3317 int cb_error;
3318 3318 boolean_t cb_recurse;
3319 3319 } rollback_cbdata_t;
3320 3320
3321 3321 static int
3322 3322 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3323 3323 {
3324 3324 rollback_cbdata_t *cbp = data;
3325 3325
3326 3326 if (cbp->cb_first && cbp->cb_recurse) {
3327 3327 (void) fprintf(stderr, gettext("cannot rollback to "
3328 3328 "'%s': clones of previous snapshots exist\n"),
3329 3329 cbp->cb_target);
3330 3330 (void) fprintf(stderr, gettext("use '-R' to "
3331 3331 "force deletion of the following clones and "
3332 3332 "dependents:\n"));
3333 3333 cbp->cb_first = 0;
3334 3334 cbp->cb_error = 1;
3335 3335 }
3336 3336
3337 3337 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3338 3338
3339 3339 zfs_close(zhp);
3340 3340 return (0);
3341 3341 }
3342 3342
3343 3343 /*
3344 3344 * Report any snapshots more recent than the one specified. Used when '-r' is
3345 3345 * not specified. We reuse this same callback for the snapshot dependents - if
3346 3346 * 'cb_dependent' is set, then this is a dependent and we should report it
3347 3347 * without checking the transaction group.
3348 3348 */
3349 3349 static int
3350 3350 rollback_check(zfs_handle_t *zhp, void *data)
3351 3351 {
3352 3352 rollback_cbdata_t *cbp = data;
3353 3353
3354 3354 if (cbp->cb_doclones) {
3355 3355 zfs_close(zhp);
3356 3356 return (0);
3357 3357 }
3358 3358
3359 3359 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3360 3360 if (cbp->cb_first && !cbp->cb_recurse) {
3361 3361 (void) fprintf(stderr, gettext("cannot "
3362 3362 "rollback to '%s': more recent snapshots "
3363 3363 "or bookmarks exist\n"),
3364 3364 cbp->cb_target);
3365 3365 (void) fprintf(stderr, gettext("use '-r' to "
3366 3366 "force deletion of the following "
3367 3367 "snapshots and bookmarks:\n"));
3368 3368 cbp->cb_first = 0;
3369 3369 cbp->cb_error = 1;
3370 3370 }
3371 3371
3372 3372 if (cbp->cb_recurse) {
3373 3373 if (zfs_iter_dependents(zhp, B_TRUE,
3374 3374 rollback_check_dependent, cbp) != 0) {
3375 3375 zfs_close(zhp);
3376 3376 return (-1);
3377 3377 }
3378 3378 } else {
3379 3379 (void) fprintf(stderr, "%s\n",
3380 3380 zfs_get_name(zhp));
3381 3381 }
3382 3382 }
3383 3383 zfs_close(zhp);
3384 3384 return (0);
3385 3385 }
3386 3386
3387 3387 static int
3388 3388 zfs_do_rollback(int argc, char **argv)
3389 3389 {
3390 3390 int ret = 0;
3391 3391 int c;
3392 3392 boolean_t force = B_FALSE;
3393 3393 rollback_cbdata_t cb = { 0 };
3394 3394 zfs_handle_t *zhp, *snap;
3395 3395 char parentname[ZFS_MAXNAMELEN];
3396 3396 char *delim;
3397 3397
3398 3398 /* check options */
3399 3399 while ((c = getopt(argc, argv, "rRf")) != -1) {
3400 3400 switch (c) {
3401 3401 case 'r':
3402 3402 cb.cb_recurse = 1;
3403 3403 break;
3404 3404 case 'R':
3405 3405 cb.cb_recurse = 1;
3406 3406 cb.cb_doclones = 1;
3407 3407 break;
3408 3408 case 'f':
3409 3409 force = B_TRUE;
3410 3410 break;
3411 3411 case '?':
3412 3412 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3413 3413 optopt);
3414 3414 usage(B_FALSE);
3415 3415 }
3416 3416 }
3417 3417
3418 3418 argc -= optind;
3419 3419 argv += optind;
3420 3420
3421 3421 /* check number of arguments */
3422 3422 if (argc < 1) {
3423 3423 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3424 3424 usage(B_FALSE);
3425 3425 }
3426 3426 if (argc > 1) {
3427 3427 (void) fprintf(stderr, gettext("too many arguments\n"));
3428 3428 usage(B_FALSE);
3429 3429 }
3430 3430
3431 3431 /* open the snapshot */
3432 3432 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3433 3433 return (1);
3434 3434
3435 3435 /* open the parent dataset */
3436 3436 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3437 3437 verify((delim = strrchr(parentname, '@')) != NULL);
3438 3438 *delim = '\0';
3439 3439 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3440 3440 zfs_close(snap);
3441 3441 return (1);
3442 3442 }
3443 3443
3444 3444 /*
3445 3445 * Check for more recent snapshots and/or clones based on the presence
3446 3446 * of '-r' and '-R'.
3447 3447 */
3448 3448 cb.cb_target = argv[0];
3449 3449 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3450 3450 cb.cb_first = B_TRUE;
3451 3451 cb.cb_error = 0;
3452 3452 if ((ret = zfs_iter_snapshots(zhp, rollback_check, &cb)) != 0)
3453 3453 goto out;
3454 3454 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3455 3455 goto out;
3456 3456
3457 3457 if ((ret = cb.cb_error) != 0)
3458 3458 goto out;
3459 3459
3460 3460 /*
3461 3461 * Rollback parent to the given snapshot.
3462 3462 */
3463 3463 ret = zfs_rollback(zhp, snap, force);
3464 3464
3465 3465 out:
3466 3466 zfs_close(snap);
3467 3467 zfs_close(zhp);
3468 3468
3469 3469 if (ret == 0)
3470 3470 return (0);
3471 3471 else
3472 3472 return (1);
3473 3473 }
3474 3474
3475 3475 /*
3476 3476 * zfs set property=value ... { fs | snap | vol } ...
3477 3477 *
3478 3478 * Sets the given properties for all datasets specified on the command line.
3479 3479 */
3480 3480
3481 3481 static int
3482 3482 set_callback(zfs_handle_t *zhp, void *data)
3483 3483 {
3484 3484 nvlist_t *props = data;
3485 3485
3486 3486 if (zfs_prop_set_list(zhp, props) != 0) {
3487 3487 switch (libzfs_errno(g_zfs)) {
3488 3488 case EZFS_MOUNTFAILED:
3489 3489 (void) fprintf(stderr, gettext("property may be set "
3490 3490 "but unable to remount filesystem\n"));
3491 3491 break;
3492 3492 case EZFS_SHARENFSFAILED:
3493 3493 (void) fprintf(stderr, gettext("property may be set "
3494 3494 "but unable to reshare filesystem\n"));
3495 3495 break;
3496 3496 }
3497 3497 return (1);
3498 3498 }
3499 3499 return (0);
3500 3500 }
3501 3501
3502 3502 static int
3503 3503 zfs_do_set(int argc, char **argv)
3504 3504 {
3505 3505 nvlist_t *props = NULL;
3506 3506 int ds_start = -1; /* argv idx of first dataset arg */
3507 3507 int ret = 0;
3508 3508
3509 3509 /* check for options */
3510 3510 if (argc > 1 && argv[1][0] == '-') {
3511 3511 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3512 3512 argv[1][1]);
3513 3513 usage(B_FALSE);
3514 3514 }
3515 3515
3516 3516 /* check number of arguments */
3517 3517 if (argc < 2) {
3518 3518 (void) fprintf(stderr, gettext("missing arguments\n"));
3519 3519 usage(B_FALSE);
3520 3520 }
3521 3521 if (argc < 3) {
3522 3522 if (strchr(argv[1], '=') == NULL) {
3523 3523 (void) fprintf(stderr, gettext("missing property=value "
3524 3524 "argument(s)\n"));
3525 3525 } else {
3526 3526 (void) fprintf(stderr, gettext("missing dataset "
3527 3527 "name(s)\n"));
3528 3528 }
3529 3529 usage(B_FALSE);
3530 3530 }
3531 3531
3532 3532 /* validate argument order: prop=val args followed by dataset args */
3533 3533 for (int i = 1; i < argc; i++) {
3534 3534 if (strchr(argv[i], '=') != NULL) {
3535 3535 if (ds_start > 0) {
3536 3536 /* out-of-order prop=val argument */
3537 3537 (void) fprintf(stderr, gettext("invalid "
3538 3538 "argument order\n"), i);
3539 3539 usage(B_FALSE);
3540 3540 }
3541 3541 } else if (ds_start < 0) {
3542 3542 ds_start = i;
3543 3543 }
3544 3544 }
3545 3545 if (ds_start < 0) {
3546 3546 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3547 3547 usage(B_FALSE);
3548 3548 }
3549 3549
3550 3550 /* Populate a list of property settings */
3551 3551 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3552 3552 nomem();
3553 3553 for (int i = 1; i < ds_start; i++) {
3554 3554 if ((ret = parseprop(props, argv[i])) != 0)
3555 3555 goto error;
3556 3556 }
3557 3557
3558 3558 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3559 3559 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3560 3560
3561 3561 error:
3562 3562 nvlist_free(props);
3563 3563 return (ret);
3564 3564 }
3565 3565
3566 3566 typedef struct snap_cbdata {
3567 3567 nvlist_t *sd_nvl;
3568 3568 boolean_t sd_recursive;
3569 3569 const char *sd_snapname;
3570 3570 } snap_cbdata_t;
3571 3571
3572 3572 static int
3573 3573 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3574 3574 {
3575 3575 snap_cbdata_t *sd = arg;
3576 3576 char *name;
3577 3577 int rv = 0;
3578 3578 int error;
3579 3579
3580 3580 if (sd->sd_recursive &&
3581 3581 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3582 3582 zfs_close(zhp);
3583 3583 return (0);
3584 3584 }
3585 3585
3586 3586 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3587 3587 if (error == -1)
3588 3588 nomem();
3589 3589 fnvlist_add_boolean(sd->sd_nvl, name);
3590 3590 free(name);
3591 3591
3592 3592 if (sd->sd_recursive)
3593 3593 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3594 3594 zfs_close(zhp);
3595 3595 return (rv);
3596 3596 }
3597 3597
3598 3598 /*
3599 3599 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3600 3600 *
3601 3601 * Creates a snapshot with the given name. While functionally equivalent to
3602 3602 * 'zfs create', it is a separate command to differentiate intent.
3603 3603 */
3604 3604 static int
3605 3605 zfs_do_snapshot(int argc, char **argv)
3606 3606 {
3607 3607 int ret = 0;
3608 3608 char c;
3609 3609 nvlist_t *props;
3610 3610 snap_cbdata_t sd = { 0 };
3611 3611 boolean_t multiple_snaps = B_FALSE;
3612 3612
3613 3613 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3614 3614 nomem();
3615 3615 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3616 3616 nomem();
3617 3617
3618 3618 /* check options */
3619 3619 while ((c = getopt(argc, argv, "ro:")) != -1) {
3620 3620 switch (c) {
3621 3621 case 'o':
3622 3622 if (parseprop(props, optarg) != 0)
3623 3623 return (1);
3624 3624 break;
3625 3625 case 'r':
3626 3626 sd.sd_recursive = B_TRUE;
3627 3627 multiple_snaps = B_TRUE;
3628 3628 break;
3629 3629 case '?':
3630 3630 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3631 3631 optopt);
3632 3632 goto usage;
3633 3633 }
3634 3634 }
3635 3635
3636 3636 argc -= optind;
3637 3637 argv += optind;
3638 3638
3639 3639 /* check number of arguments */
3640 3640 if (argc < 1) {
3641 3641 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3642 3642 goto usage;
3643 3643 }
3644 3644
3645 3645 if (argc > 1)
3646 3646 multiple_snaps = B_TRUE;
3647 3647 for (; argc > 0; argc--, argv++) {
3648 3648 char *atp;
3649 3649 zfs_handle_t *zhp;
3650 3650
3651 3651 atp = strchr(argv[0], '@');
3652 3652 if (atp == NULL)
3653 3653 goto usage;
3654 3654 *atp = '\0';
3655 3655 sd.sd_snapname = atp + 1;
3656 3656 zhp = zfs_open(g_zfs, argv[0],
3657 3657 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3658 3658 if (zhp == NULL)
3659 3659 goto usage;
3660 3660 if (zfs_snapshot_cb(zhp, &sd) != 0)
3661 3661 goto usage;
3662 3662 }
3663 3663
3664 3664 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3665 3665 nvlist_free(sd.sd_nvl);
3666 3666 nvlist_free(props);
3667 3667 if (ret != 0 && multiple_snaps)
3668 3668 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3669 3669 return (ret != 0);
3670 3670
3671 3671 usage:
3672 3672 nvlist_free(sd.sd_nvl);
3673 3673 nvlist_free(props);
3674 3674 usage(B_FALSE);
3675 3675 return (-1);
3676 3676 }
3677 3677
3678 3678 /*
3679 3679 * Send a backup stream to stdout.
3680 3680 */
3681 3681 static int
3682 3682 zfs_do_send(int argc, char **argv)
3683 3683 {
3684 3684 char *fromname = NULL;
3685 3685 char *toname = NULL;
3686 3686 char *resume_token = NULL;
3687 3687 char *cp;
3688 3688 zfs_handle_t *zhp;
3689 3689 sendflags_t flags = { 0 };
3690 3690 int c, err;
3691 3691 nvlist_t *dbgnv = NULL;
3692 3692 boolean_t extraverbose = B_FALSE;
3693 3693
3694 3694 /* check options */
3695 3695 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) {
3696 3696 switch (c) {
3697 3697 case 'i':
3698 3698 if (fromname)
3699 3699 usage(B_FALSE);
3700 3700 fromname = optarg;
3701 3701 break;
3702 3702 case 'I':
3703 3703 if (fromname)
3704 3704 usage(B_FALSE);
3705 3705 fromname = optarg;
3706 3706 flags.doall = B_TRUE;
3707 3707 break;
3708 3708 case 'R':
3709 3709 flags.replicate = B_TRUE;
3710 3710 break;
3711 3711 case 'p':
3712 3712 flags.props = B_TRUE;
3713 3713 break;
3714 3714 case 'P':
3715 3715 flags.parsable = B_TRUE;
3716 3716 flags.verbose = B_TRUE;
3717 3717 break;
3718 3718 case 'v':
3719 3719 if (flags.verbose)
3720 3720 extraverbose = B_TRUE;
3721 3721 flags.verbose = B_TRUE;
3722 3722 flags.progress = B_TRUE;
3723 3723 break;
3724 3724 case 'D':
3725 3725 flags.dedup = B_TRUE;
3726 3726 break;
3727 3727 case 'n':
3728 3728 flags.dryrun = B_TRUE;
3729 3729 break;
3730 3730 case 'L':
3731 3731 flags.largeblock = B_TRUE;
3732 3732 break;
3733 3733 case 'e':
3734 3734 flags.embed_data = B_TRUE;
3735 3735 break;
3736 3736 case 't':
3737 3737 resume_token = optarg;
3738 3738 break;
3739 3739 case ':':
3740 3740 (void) fprintf(stderr, gettext("missing argument for "
3741 3741 "'%c' option\n"), optopt);
3742 3742 usage(B_FALSE);
3743 3743 break;
3744 3744 case '?':
3745 3745 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3746 3746 optopt);
3747 3747 usage(B_FALSE);
3748 3748 }
3749 3749 }
3750 3750
3751 3751 argc -= optind;
3752 3752 argv += optind;
3753 3753
3754 3754 if (resume_token != NULL) {
3755 3755 if (fromname != NULL || flags.replicate || flags.props ||
3756 3756 flags.dedup) {
3757 3757 (void) fprintf(stderr,
3758 3758 gettext("invalid flags combined with -t\n"));
3759 3759 usage(B_FALSE);
3760 3760 }
3761 3761 if (argc != 0) {
3762 3762 (void) fprintf(stderr, gettext("no additional "
3763 3763 "arguments are permitted with -t\n"));
3764 3764 usage(B_FALSE);
3765 3765 }
3766 3766 } else {
3767 3767 if (argc < 1) {
3768 3768 (void) fprintf(stderr,
3769 3769 gettext("missing snapshot argument\n"));
3770 3770 usage(B_FALSE);
3771 3771 }
3772 3772 if (argc > 1) {
3773 3773 (void) fprintf(stderr, gettext("too many arguments\n"));
3774 3774 usage(B_FALSE);
3775 3775 }
3776 3776 }
3777 3777
3778 3778 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3779 3779 (void) fprintf(stderr,
3780 3780 gettext("Error: Stream can not be written to a terminal.\n"
3781 3781 "You must redirect standard output.\n"));
3782 3782 return (1);
3783 3783 }
3784 3784
3785 3785 if (resume_token != NULL) {
3786 3786 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3787 3787 resume_token));
3788 3788 }
3789 3789
3790 3790 /*
3791 3791 * Special case sending a filesystem, or from a bookmark.
3792 3792 */
3793 3793 if (strchr(argv[0], '@') == NULL ||
3794 3794 (fromname && strchr(fromname, '#') != NULL)) {
3795 3795 char frombuf[ZFS_MAXNAMELEN];
3796 3796 enum lzc_send_flags lzc_flags = 0;
3797 3797
3798 3798 if (flags.replicate || flags.doall || flags.props ||
3799 3799 flags.dedup || flags.dryrun || flags.verbose ||
3800 3800 flags.progress) {
3801 3801 (void) fprintf(stderr,
3802 3802 gettext("Error: "
3803 3803 "Unsupported flag with filesystem or bookmark.\n"));
3804 3804 return (1);
3805 3805 }
3806 3806
3807 3807 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3808 3808 if (zhp == NULL)
3809 3809 return (1);
3810 3810
3811 3811 if (flags.largeblock)
3812 3812 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3813 3813 if (flags.embed_data)
3814 3814 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3815 3815
3816 3816 if (fromname != NULL &&
3817 3817 (fromname[0] == '#' || fromname[0] == '@')) {
3818 3818 /*
3819 3819 * Incremental source name begins with # or @.
3820 3820 * Default to same fs as target.
3821 3821 */
3822 3822 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3823 3823 cp = strchr(frombuf, '@');
3824 3824 if (cp != NULL)
3825 3825 *cp = '\0';
3826 3826 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3827 3827 fromname = frombuf;
3828 3828 }
3829 3829 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3830 3830 zfs_close(zhp);
3831 3831 return (err != 0);
3832 3832 }
3833 3833
3834 3834 cp = strchr(argv[0], '@');
3835 3835 *cp = '\0';
3836 3836 toname = cp + 1;
3837 3837 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3838 3838 if (zhp == NULL)
3839 3839 return (1);
3840 3840
3841 3841 /*
3842 3842 * If they specified the full path to the snapshot, chop off
3843 3843 * everything except the short name of the snapshot, but special
3844 3844 * case if they specify the origin.
3845 3845 */
3846 3846 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3847 3847 char origin[ZFS_MAXNAMELEN];
3848 3848 zprop_source_t src;
3849 3849
3850 3850 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3851 3851 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3852 3852
3853 3853 if (strcmp(origin, fromname) == 0) {
3854 3854 fromname = NULL;
3855 3855 flags.fromorigin = B_TRUE;
3856 3856 } else {
3857 3857 *cp = '\0';
3858 3858 if (cp != fromname && strcmp(argv[0], fromname)) {
3859 3859 (void) fprintf(stderr,
3860 3860 gettext("incremental source must be "
3861 3861 "in same filesystem\n"));
3862 3862 usage(B_FALSE);
3863 3863 }
3864 3864 fromname = cp + 1;
3865 3865 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3866 3866 (void) fprintf(stderr,
3867 3867 gettext("invalid incremental source\n"));
3868 3868 usage(B_FALSE);
3869 3869 }
3870 3870 }
3871 3871 }
3872 3872
3873 3873 if (flags.replicate && fromname == NULL)
3874 3874 flags.doall = B_TRUE;
3875 3875
3876 3876 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3877 3877 extraverbose ? &dbgnv : NULL);
3878 3878
3879 3879 if (extraverbose && dbgnv != NULL) {
3880 3880 /*
3881 3881 * dump_nvlist prints to stdout, but that's been
3882 3882 * redirected to a file. Make it print to stderr
3883 3883 * instead.
3884 3884 */
3885 3885 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3886 3886 dump_nvlist(dbgnv, 0);
3887 3887 nvlist_free(dbgnv);
3888 3888 }
3889 3889 zfs_close(zhp);
3890 3890
3891 3891 return (err != 0);
3892 3892 }
3893 3893
3894 3894 /*
3895 3895 * Restore a backup stream from stdin.
3896 3896 */
3897 3897 static int
3898 3898 zfs_do_receive(int argc, char **argv)
3899 3899 {
3900 3900 int c, err = 0;
3901 3901 recvflags_t flags = { 0 };
3902 3902 boolean_t abort_resumable = B_FALSE;
3903 3903
3904 3904 nvlist_t *props;
3905 3905 nvpair_t *nvp = NULL;
3906 3906
3907 3907 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3908 3908 nomem();
3909 3909
3910 3910 /* check options */
3911 3911 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3912 3912 switch (c) {
3913 3913 case 'o':
3914 3914 if (parseprop(props, optarg) != 0)
3915 3915 return (1);
3916 3916 break;
3917 3917 case 'd':
3918 3918 flags.isprefix = B_TRUE;
3919 3919 break;
3920 3920 case 'e':
3921 3921 flags.isprefix = B_TRUE;
3922 3922 flags.istail = B_TRUE;
3923 3923 break;
3924 3924 case 'n':
3925 3925 flags.dryrun = B_TRUE;
3926 3926 break;
3927 3927 case 'u':
3928 3928 flags.nomount = B_TRUE;
3929 3929 break;
3930 3930 case 'v':
3931 3931 flags.verbose = B_TRUE;
3932 3932 break;
3933 3933 case 's':
3934 3934 flags.resumable = B_TRUE;
3935 3935 break;
3936 3936 case 'F':
3937 3937 flags.force = B_TRUE;
3938 3938 break;
3939 3939 case 'A':
3940 3940 abort_resumable = B_TRUE;
3941 3941 break;
3942 3942 case ':':
3943 3943 (void) fprintf(stderr, gettext("missing argument for "
3944 3944 "'%c' option\n"), optopt);
3945 3945 usage(B_FALSE);
3946 3946 break;
3947 3947 case '?':
3948 3948 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3949 3949 optopt);
3950 3950 usage(B_FALSE);
3951 3951 }
3952 3952 }
3953 3953
3954 3954 argc -= optind;
3955 3955 argv += optind;
3956 3956
3957 3957 /* check number of arguments */
3958 3958 if (argc < 1) {
3959 3959 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3960 3960 usage(B_FALSE);
3961 3961 }
3962 3962 if (argc > 1) {
3963 3963 (void) fprintf(stderr, gettext("too many arguments\n"));
3964 3964 usage(B_FALSE);
3965 3965 }
3966 3966
3967 3967 while ((nvp = nvlist_next_nvpair(props, nvp))) {
3968 3968 if (strcmp(nvpair_name(nvp), "origin") != 0) {
3969 3969 (void) fprintf(stderr, gettext("invalid option"));
3970 3970 usage(B_FALSE);
3971 3971 }
3972 3972 }
3973 3973
3974 3974 if (abort_resumable) {
3975 3975 if (flags.isprefix || flags.istail || flags.dryrun ||
3976 3976 flags.resumable || flags.nomount) {
3977 3977 (void) fprintf(stderr, gettext("invalid option"));
3978 3978 usage(B_FALSE);
3979 3979 }
3980 3980
3981 3981 char namebuf[ZFS_MAXNAMELEN];
3982 3982 (void) snprintf(namebuf, sizeof (namebuf),
3983 3983 "%s/%%recv", argv[0]);
3984 3984
3985 3985 if (zfs_dataset_exists(g_zfs, namebuf,
3986 3986 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
3987 3987 zfs_handle_t *zhp = zfs_open(g_zfs,
3988 3988 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3989 3989 if (zhp == NULL)
3990 3990 return (1);
3991 3991 err = zfs_destroy(zhp, B_FALSE);
3992 3992 } else {
3993 3993 zfs_handle_t *zhp = zfs_open(g_zfs,
3994 3994 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3995 3995 if (zhp == NULL)
3996 3996 usage(B_FALSE);
3997 3997 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
3998 3998 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3999 3999 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4000 4000 (void) fprintf(stderr,
4001 4001 gettext("'%s' does not have any "
4002 4002 "resumable receive state to abort\n"),
4003 4003 argv[0]);
4004 4004 return (1);
4005 4005 }
4006 4006 err = zfs_destroy(zhp, B_FALSE);
4007 4007 }
4008 4008
4009 4009 return (err != 0);
4010 4010 }
4011 4011
4012 4012 if (isatty(STDIN_FILENO)) {
4013 4013 (void) fprintf(stderr,
4014 4014 gettext("Error: Backup stream can not be read "
4015 4015 "from a terminal.\n"
4016 4016 "You must redirect standard input.\n"));
4017 4017 return (1);
4018 4018 }
4019 4019 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4020 4020
4021 4021 return (err != 0);
4022 4022 }
4023 4023
4024 4024 /*
4025 4025 * allow/unallow stuff
4026 4026 */
4027 4027 /* copied from zfs/sys/dsl_deleg.h */
4028 4028 #define ZFS_DELEG_PERM_CREATE "create"
4029 4029 #define ZFS_DELEG_PERM_DESTROY "destroy"
4030 4030 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4031 4031 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4032 4032 #define ZFS_DELEG_PERM_CLONE "clone"
4033 4033 #define ZFS_DELEG_PERM_PROMOTE "promote"
4034 4034 #define ZFS_DELEG_PERM_RENAME "rename"
4035 4035 #define ZFS_DELEG_PERM_MOUNT "mount"
4036 4036 #define ZFS_DELEG_PERM_SHARE "share"
4037 4037 #define ZFS_DELEG_PERM_SEND "send"
4038 4038 #define ZFS_DELEG_PERM_RECEIVE "receive"
4039 4039 #define ZFS_DELEG_PERM_ALLOW "allow"
4040 4040 #define ZFS_DELEG_PERM_USERPROP "userprop"
4041 4041 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4042 4042 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4043 4043 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4044 4044 #define ZFS_DELEG_PERM_USERUSED "userused"
4045 4045 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4046 4046 #define ZFS_DELEG_PERM_HOLD "hold"
4047 4047 #define ZFS_DELEG_PERM_RELEASE "release"
4048 4048 #define ZFS_DELEG_PERM_DIFF "diff"
4049 4049 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4050 4050
4051 4051 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4052 4052
4053 4053 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4054 4054 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4055 4055 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4056 4056 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4057 4057 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4058 4058 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4059 4059 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4060 4060 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4061 4061 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4062 4062 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4063 4063 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4064 4064 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4065 4065 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4066 4066 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4067 4067 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4068 4068 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4069 4069 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4070 4070
4071 4071 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4072 4072 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4073 4073 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4074 4074 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4075 4075 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4076 4076 { NULL, ZFS_DELEG_NOTE_NONE }
4077 4077 };
4078 4078
4079 4079 /* permission structure */
4080 4080 typedef struct deleg_perm {
4081 4081 zfs_deleg_who_type_t dp_who_type;
4082 4082 const char *dp_name;
4083 4083 boolean_t dp_local;
4084 4084 boolean_t dp_descend;
4085 4085 } deleg_perm_t;
4086 4086
4087 4087 /* */
4088 4088 typedef struct deleg_perm_node {
4089 4089 deleg_perm_t dpn_perm;
4090 4090
4091 4091 uu_avl_node_t dpn_avl_node;
4092 4092 } deleg_perm_node_t;
4093 4093
4094 4094 typedef struct fs_perm fs_perm_t;
4095 4095
4096 4096 /* permissions set */
4097 4097 typedef struct who_perm {
4098 4098 zfs_deleg_who_type_t who_type;
4099 4099 const char *who_name; /* id */
4100 4100 char who_ug_name[256]; /* user/group name */
4101 4101 fs_perm_t *who_fsperm; /* uplink */
4102 4102
4103 4103 uu_avl_t *who_deleg_perm_avl; /* permissions */
4104 4104 } who_perm_t;
4105 4105
4106 4106 /* */
4107 4107 typedef struct who_perm_node {
4108 4108 who_perm_t who_perm;
4109 4109 uu_avl_node_t who_avl_node;
4110 4110 } who_perm_node_t;
4111 4111
4112 4112 typedef struct fs_perm_set fs_perm_set_t;
4113 4113 /* fs permissions */
4114 4114 struct fs_perm {
4115 4115 const char *fsp_name;
4116 4116
4117 4117 uu_avl_t *fsp_sc_avl; /* sets,create */
4118 4118 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4119 4119
4120 4120 fs_perm_set_t *fsp_set; /* uplink */
4121 4121 };
4122 4122
4123 4123 /* */
4124 4124 typedef struct fs_perm_node {
4125 4125 fs_perm_t fspn_fsperm;
4126 4126 uu_avl_t *fspn_avl;
4127 4127
4128 4128 uu_list_node_t fspn_list_node;
4129 4129 } fs_perm_node_t;
4130 4130
4131 4131 /* top level structure */
4132 4132 struct fs_perm_set {
4133 4133 uu_list_pool_t *fsps_list_pool;
4134 4134 uu_list_t *fsps_list; /* list of fs_perms */
4135 4135
4136 4136 uu_avl_pool_t *fsps_named_set_avl_pool;
4137 4137 uu_avl_pool_t *fsps_who_perm_avl_pool;
4138 4138 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4139 4139 };
4140 4140
4141 4141 static inline const char *
4142 4142 deleg_perm_type(zfs_deleg_note_t note)
4143 4143 {
4144 4144 /* subcommands */
4145 4145 switch (note) {
4146 4146 /* SUBCOMMANDS */
4147 4147 /* OTHER */
4148 4148 case ZFS_DELEG_NOTE_GROUPQUOTA:
4149 4149 case ZFS_DELEG_NOTE_GROUPUSED:
4150 4150 case ZFS_DELEG_NOTE_USERPROP:
4151 4151 case ZFS_DELEG_NOTE_USERQUOTA:
4152 4152 case ZFS_DELEG_NOTE_USERUSED:
4153 4153 /* other */
4154 4154 return (gettext("other"));
4155 4155 default:
4156 4156 return (gettext("subcommand"));
4157 4157 }
4158 4158 }
4159 4159
4160 4160 static int
4161 4161 who_type2weight(zfs_deleg_who_type_t who_type)
4162 4162 {
4163 4163 int res;
4164 4164 switch (who_type) {
4165 4165 case ZFS_DELEG_NAMED_SET_SETS:
4166 4166 case ZFS_DELEG_NAMED_SET:
4167 4167 res = 0;
4168 4168 break;
4169 4169 case ZFS_DELEG_CREATE_SETS:
4170 4170 case ZFS_DELEG_CREATE:
4171 4171 res = 1;
4172 4172 break;
4173 4173 case ZFS_DELEG_USER_SETS:
4174 4174 case ZFS_DELEG_USER:
4175 4175 res = 2;
4176 4176 break;
4177 4177 case ZFS_DELEG_GROUP_SETS:
4178 4178 case ZFS_DELEG_GROUP:
4179 4179 res = 3;
4180 4180 break;
4181 4181 case ZFS_DELEG_EVERYONE_SETS:
4182 4182 case ZFS_DELEG_EVERYONE:
4183 4183 res = 4;
4184 4184 break;
4185 4185 default:
4186 4186 res = -1;
4187 4187 }
4188 4188
4189 4189 return (res);
4190 4190 }
4191 4191
4192 4192 /* ARGSUSED */
4193 4193 static int
4194 4194 who_perm_compare(const void *larg, const void *rarg, void *unused)
4195 4195 {
4196 4196 const who_perm_node_t *l = larg;
4197 4197 const who_perm_node_t *r = rarg;
4198 4198 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4199 4199 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4200 4200 int lweight = who_type2weight(ltype);
4201 4201 int rweight = who_type2weight(rtype);
4202 4202 int res = lweight - rweight;
4203 4203 if (res == 0)
4204 4204 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4205 4205 ZFS_MAX_DELEG_NAME-1);
4206 4206
4207 4207 if (res == 0)
4208 4208 return (0);
4209 4209 if (res > 0)
4210 4210 return (1);
4211 4211 else
4212 4212 return (-1);
4213 4213 }
4214 4214
4215 4215 /* ARGSUSED */
4216 4216 static int
4217 4217 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4218 4218 {
4219 4219 const deleg_perm_node_t *l = larg;
4220 4220 const deleg_perm_node_t *r = rarg;
4221 4221 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4222 4222 ZFS_MAX_DELEG_NAME-1);
4223 4223
4224 4224 if (res == 0)
4225 4225 return (0);
4226 4226
4227 4227 if (res > 0)
4228 4228 return (1);
4229 4229 else
4230 4230 return (-1);
4231 4231 }
4232 4232
4233 4233 static inline void
4234 4234 fs_perm_set_init(fs_perm_set_t *fspset)
4235 4235 {
4236 4236 bzero(fspset, sizeof (fs_perm_set_t));
4237 4237
4238 4238 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4239 4239 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4240 4240 NULL, UU_DEFAULT)) == NULL)
4241 4241 nomem();
4242 4242 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4243 4243 UU_DEFAULT)) == NULL)
4244 4244 nomem();
4245 4245
4246 4246 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4247 4247 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4248 4248 who_perm_node_t, who_avl_node), who_perm_compare,
4249 4249 UU_DEFAULT)) == NULL)
4250 4250 nomem();
4251 4251
4252 4252 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4253 4253 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4254 4254 who_perm_node_t, who_avl_node), who_perm_compare,
4255 4255 UU_DEFAULT)) == NULL)
4256 4256 nomem();
4257 4257
4258 4258 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4259 4259 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4260 4260 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4261 4261 == NULL)
4262 4262 nomem();
4263 4263 }
4264 4264
4265 4265 static inline void fs_perm_fini(fs_perm_t *);
4266 4266 static inline void who_perm_fini(who_perm_t *);
4267 4267
4268 4268 static inline void
4269 4269 fs_perm_set_fini(fs_perm_set_t *fspset)
4270 4270 {
4271 4271 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4272 4272
4273 4273 while (node != NULL) {
4274 4274 fs_perm_node_t *next_node =
4275 4275 uu_list_next(fspset->fsps_list, node);
4276 4276 fs_perm_t *fsperm = &node->fspn_fsperm;
4277 4277 fs_perm_fini(fsperm);
4278 4278 uu_list_remove(fspset->fsps_list, node);
4279 4279 free(node);
4280 4280 node = next_node;
4281 4281 }
4282 4282
4283 4283 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4284 4284 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4285 4285 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4286 4286 }
4287 4287
4288 4288 static inline void
4289 4289 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4290 4290 const char *name)
4291 4291 {
4292 4292 deleg_perm->dp_who_type = type;
4293 4293 deleg_perm->dp_name = name;
4294 4294 }
4295 4295
4296 4296 static inline void
4297 4297 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4298 4298 zfs_deleg_who_type_t type, const char *name)
4299 4299 {
4300 4300 uu_avl_pool_t *pool;
4301 4301 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4302 4302
4303 4303 bzero(who_perm, sizeof (who_perm_t));
4304 4304
4305 4305 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4306 4306 UU_DEFAULT)) == NULL)
4307 4307 nomem();
4308 4308
4309 4309 who_perm->who_type = type;
4310 4310 who_perm->who_name = name;
4311 4311 who_perm->who_fsperm = fsperm;
4312 4312 }
4313 4313
4314 4314 static inline void
4315 4315 who_perm_fini(who_perm_t *who_perm)
4316 4316 {
4317 4317 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4318 4318
4319 4319 while (node != NULL) {
4320 4320 deleg_perm_node_t *next_node =
4321 4321 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4322 4322
4323 4323 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4324 4324 free(node);
4325 4325 node = next_node;
4326 4326 }
4327 4327
4328 4328 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4329 4329 }
4330 4330
4331 4331 static inline void
4332 4332 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4333 4333 {
4334 4334 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4335 4335 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4336 4336
4337 4337 bzero(fsperm, sizeof (fs_perm_t));
4338 4338
4339 4339 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4340 4340 == NULL)
4341 4341 nomem();
4342 4342
4343 4343 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4344 4344 == NULL)
4345 4345 nomem();
4346 4346
4347 4347 fsperm->fsp_set = fspset;
4348 4348 fsperm->fsp_name = fsname;
4349 4349 }
4350 4350
4351 4351 static inline void
4352 4352 fs_perm_fini(fs_perm_t *fsperm)
4353 4353 {
4354 4354 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4355 4355 while (node != NULL) {
4356 4356 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4357 4357 node);
4358 4358 who_perm_t *who_perm = &node->who_perm;
4359 4359 who_perm_fini(who_perm);
4360 4360 uu_avl_remove(fsperm->fsp_sc_avl, node);
4361 4361 free(node);
4362 4362 node = next_node;
4363 4363 }
4364 4364
4365 4365 node = uu_avl_first(fsperm->fsp_uge_avl);
4366 4366 while (node != NULL) {
4367 4367 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4368 4368 node);
4369 4369 who_perm_t *who_perm = &node->who_perm;
4370 4370 who_perm_fini(who_perm);
4371 4371 uu_avl_remove(fsperm->fsp_uge_avl, node);
4372 4372 free(node);
4373 4373 node = next_node;
4374 4374 }
4375 4375
4376 4376 uu_avl_destroy(fsperm->fsp_sc_avl);
4377 4377 uu_avl_destroy(fsperm->fsp_uge_avl);
4378 4378 }
4379 4379
4380 4380 static void
4381 4381 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4382 4382 zfs_deleg_who_type_t who_type, const char *name, char locality)
4383 4383 {
4384 4384 uu_avl_index_t idx = 0;
4385 4385
4386 4386 deleg_perm_node_t *found_node = NULL;
4387 4387 deleg_perm_t *deleg_perm = &node->dpn_perm;
4388 4388
4389 4389 deleg_perm_init(deleg_perm, who_type, name);
4390 4390
4391 4391 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4392 4392 == NULL)
4393 4393 uu_avl_insert(avl, node, idx);
4394 4394 else {
4395 4395 node = found_node;
4396 4396 deleg_perm = &node->dpn_perm;
4397 4397 }
4398 4398
4399 4399
4400 4400 switch (locality) {
4401 4401 case ZFS_DELEG_LOCAL:
4402 4402 deleg_perm->dp_local = B_TRUE;
4403 4403 break;
4404 4404 case ZFS_DELEG_DESCENDENT:
4405 4405 deleg_perm->dp_descend = B_TRUE;
4406 4406 break;
4407 4407 case ZFS_DELEG_NA:
4408 4408 break;
4409 4409 default:
4410 4410 assert(B_FALSE); /* invalid locality */
4411 4411 }
4412 4412 }
4413 4413
4414 4414 static inline int
4415 4415 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4416 4416 {
4417 4417 nvpair_t *nvp = NULL;
4418 4418 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4419 4419 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4420 4420 zfs_deleg_who_type_t who_type = who_perm->who_type;
4421 4421
4422 4422 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4423 4423 const char *name = nvpair_name(nvp);
4424 4424 data_type_t type = nvpair_type(nvp);
4425 4425 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4426 4426 deleg_perm_node_t *node =
4427 4427 safe_malloc(sizeof (deleg_perm_node_t));
4428 4428
4429 4429 assert(type == DATA_TYPE_BOOLEAN);
4430 4430
4431 4431 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4432 4432 set_deleg_perm_node(avl, node, who_type, name, locality);
4433 4433 }
4434 4434
4435 4435 return (0);
4436 4436 }
4437 4437
4438 4438 static inline int
4439 4439 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4440 4440 {
4441 4441 nvpair_t *nvp = NULL;
4442 4442 fs_perm_set_t *fspset = fsperm->fsp_set;
4443 4443
4444 4444 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4445 4445 nvlist_t *nvl2 = NULL;
4446 4446 const char *name = nvpair_name(nvp);
4447 4447 uu_avl_t *avl = NULL;
4448 4448 uu_avl_pool_t *avl_pool = NULL;
4449 4449 zfs_deleg_who_type_t perm_type = name[0];
4450 4450 char perm_locality = name[1];
4451 4451 const char *perm_name = name + 3;
4452 4452 boolean_t is_set = B_TRUE;
4453 4453 who_perm_t *who_perm = NULL;
4454 4454
4455 4455 assert('$' == name[2]);
4456 4456
4457 4457 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4458 4458 return (-1);
4459 4459
4460 4460 switch (perm_type) {
4461 4461 case ZFS_DELEG_CREATE:
4462 4462 case ZFS_DELEG_CREATE_SETS:
4463 4463 case ZFS_DELEG_NAMED_SET:
4464 4464 case ZFS_DELEG_NAMED_SET_SETS:
4465 4465 avl_pool = fspset->fsps_named_set_avl_pool;
4466 4466 avl = fsperm->fsp_sc_avl;
4467 4467 break;
4468 4468 case ZFS_DELEG_USER:
4469 4469 case ZFS_DELEG_USER_SETS:
4470 4470 case ZFS_DELEG_GROUP:
4471 4471 case ZFS_DELEG_GROUP_SETS:
4472 4472 case ZFS_DELEG_EVERYONE:
4473 4473 case ZFS_DELEG_EVERYONE_SETS:
4474 4474 avl_pool = fspset->fsps_who_perm_avl_pool;
4475 4475 avl = fsperm->fsp_uge_avl;
4476 4476 break;
4477 4477
4478 4478 default:
4479 4479 assert(!"unhandled zfs_deleg_who_type_t");
4480 4480 }
4481 4481
4482 4482 if (is_set) {
4483 4483 who_perm_node_t *found_node = NULL;
4484 4484 who_perm_node_t *node = safe_malloc(
4485 4485 sizeof (who_perm_node_t));
4486 4486 who_perm = &node->who_perm;
4487 4487 uu_avl_index_t idx = 0;
4488 4488
4489 4489 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4490 4490 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4491 4491
4492 4492 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4493 4493 == NULL) {
4494 4494 if (avl == fsperm->fsp_uge_avl) {
4495 4495 uid_t rid = 0;
4496 4496 struct passwd *p = NULL;
4497 4497 struct group *g = NULL;
4498 4498 const char *nice_name = NULL;
4499 4499
4500 4500 switch (perm_type) {
4501 4501 case ZFS_DELEG_USER_SETS:
4502 4502 case ZFS_DELEG_USER:
4503 4503 rid = atoi(perm_name);
4504 4504 p = getpwuid(rid);
4505 4505 if (p)
4506 4506 nice_name = p->pw_name;
4507 4507 break;
4508 4508 case ZFS_DELEG_GROUP_SETS:
4509 4509 case ZFS_DELEG_GROUP:
4510 4510 rid = atoi(perm_name);
4511 4511 g = getgrgid(rid);
4512 4512 if (g)
4513 4513 nice_name = g->gr_name;
4514 4514 break;
4515 4515
4516 4516 default:
4517 4517 break;
4518 4518 }
4519 4519
4520 4520 if (nice_name != NULL)
4521 4521 (void) strlcpy(
4522 4522 node->who_perm.who_ug_name,
4523 4523 nice_name, 256);
4524 4524 }
4525 4525
4526 4526 uu_avl_insert(avl, node, idx);
4527 4527 } else {
4528 4528 node = found_node;
4529 4529 who_perm = &node->who_perm;
4530 4530 }
4531 4531 }
4532 4532
4533 4533 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4534 4534 }
4535 4535
4536 4536 return (0);
4537 4537 }
4538 4538
4539 4539 static inline int
4540 4540 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4541 4541 {
4542 4542 nvpair_t *nvp = NULL;
4543 4543 uu_avl_index_t idx = 0;
4544 4544
4545 4545 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4546 4546 nvlist_t *nvl2 = NULL;
4547 4547 const char *fsname = nvpair_name(nvp);
4548 4548 data_type_t type = nvpair_type(nvp);
4549 4549 fs_perm_t *fsperm = NULL;
4550 4550 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4551 4551 if (node == NULL)
4552 4552 nomem();
4553 4553
4554 4554 fsperm = &node->fspn_fsperm;
4555 4555
4556 4556 assert(DATA_TYPE_NVLIST == type);
4557 4557
4558 4558 uu_list_node_init(node, &node->fspn_list_node,
4559 4559 fspset->fsps_list_pool);
4560 4560
4561 4561 idx = uu_list_numnodes(fspset->fsps_list);
4562 4562 fs_perm_init(fsperm, fspset, fsname);
4563 4563
4564 4564 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4565 4565 return (-1);
4566 4566
4567 4567 (void) parse_fs_perm(fsperm, nvl2);
4568 4568
4569 4569 uu_list_insert(fspset->fsps_list, node, idx);
4570 4570 }
4571 4571
4572 4572 return (0);
4573 4573 }
4574 4574
4575 4575 static inline const char *
4576 4576 deleg_perm_comment(zfs_deleg_note_t note)
4577 4577 {
4578 4578 const char *str = "";
4579 4579
4580 4580 /* subcommands */
4581 4581 switch (note) {
4582 4582 /* SUBCOMMANDS */
4583 4583 case ZFS_DELEG_NOTE_ALLOW:
4584 4584 str = gettext("Must also have the permission that is being"
4585 4585 "\n\t\t\t\tallowed");
4586 4586 break;
4587 4587 case ZFS_DELEG_NOTE_CLONE:
4588 4588 str = gettext("Must also have the 'create' ability and 'mount'"
4589 4589 "\n\t\t\t\tability in the origin file system");
4590 4590 break;
4591 4591 case ZFS_DELEG_NOTE_CREATE:
4592 4592 str = gettext("Must also have the 'mount' ability");
4593 4593 break;
4594 4594 case ZFS_DELEG_NOTE_DESTROY:
4595 4595 str = gettext("Must also have the 'mount' ability");
4596 4596 break;
4597 4597 case ZFS_DELEG_NOTE_DIFF:
4598 4598 str = gettext("Allows lookup of paths within a dataset;"
4599 4599 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4600 4600 "\n\t\t\t\tin order to use zfs diff");
4601 4601 break;
4602 4602 case ZFS_DELEG_NOTE_HOLD:
4603 4603 str = gettext("Allows adding a user hold to a snapshot");
4604 4604 break;
4605 4605 case ZFS_DELEG_NOTE_MOUNT:
4606 4606 str = gettext("Allows mount/umount of ZFS datasets");
4607 4607 break;
4608 4608 case ZFS_DELEG_NOTE_PROMOTE:
4609 4609 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4610 4610 " 'promote' ability in the origin file system");
4611 4611 break;
4612 4612 case ZFS_DELEG_NOTE_RECEIVE:
4613 4613 str = gettext("Must also have the 'mount' and 'create'"
4614 4614 " ability");
4615 4615 break;
4616 4616 case ZFS_DELEG_NOTE_RELEASE:
4617 4617 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4618 4618 "might destroy the snapshot");
4619 4619 break;
4620 4620 case ZFS_DELEG_NOTE_RENAME:
4621 4621 str = gettext("Must also have the 'mount' and 'create'"
4622 4622 "\n\t\t\t\tability in the new parent");
4623 4623 break;
4624 4624 case ZFS_DELEG_NOTE_ROLLBACK:
4625 4625 str = gettext("");
4626 4626 break;
4627 4627 case ZFS_DELEG_NOTE_SEND:
4628 4628 str = gettext("");
4629 4629 break;
4630 4630 case ZFS_DELEG_NOTE_SHARE:
4631 4631 str = gettext("Allows sharing file systems over NFS or SMB"
4632 4632 "\n\t\t\t\tprotocols");
4633 4633 break;
4634 4634 case ZFS_DELEG_NOTE_SNAPSHOT:
4635 4635 str = gettext("");
4636 4636 break;
4637 4637 /*
4638 4638 * case ZFS_DELEG_NOTE_VSCAN:
4639 4639 * str = gettext("");
4640 4640 * break;
4641 4641 */
4642 4642 /* OTHER */
4643 4643 case ZFS_DELEG_NOTE_GROUPQUOTA:
4644 4644 str = gettext("Allows accessing any groupquota@... property");
4645 4645 break;
4646 4646 case ZFS_DELEG_NOTE_GROUPUSED:
4647 4647 str = gettext("Allows reading any groupused@... property");
4648 4648 break;
4649 4649 case ZFS_DELEG_NOTE_USERPROP:
4650 4650 str = gettext("Allows changing any user property");
4651 4651 break;
4652 4652 case ZFS_DELEG_NOTE_USERQUOTA:
4653 4653 str = gettext("Allows accessing any userquota@... property");
4654 4654 break;
4655 4655 case ZFS_DELEG_NOTE_USERUSED:
4656 4656 str = gettext("Allows reading any userused@... property");
4657 4657 break;
4658 4658 /* other */
4659 4659 default:
4660 4660 str = "";
4661 4661 }
4662 4662
4663 4663 return (str);
4664 4664 }
4665 4665
4666 4666 struct allow_opts {
4667 4667 boolean_t local;
4668 4668 boolean_t descend;
4669 4669 boolean_t user;
4670 4670 boolean_t group;
4671 4671 boolean_t everyone;
4672 4672 boolean_t create;
4673 4673 boolean_t set;
4674 4674 boolean_t recursive; /* unallow only */
4675 4675 boolean_t prt_usage;
4676 4676
4677 4677 boolean_t prt_perms;
4678 4678 char *who;
4679 4679 char *perms;
4680 4680 const char *dataset;
4681 4681 };
4682 4682
4683 4683 static inline int
4684 4684 prop_cmp(const void *a, const void *b)
4685 4685 {
4686 4686 const char *str1 = *(const char **)a;
4687 4687 const char *str2 = *(const char **)b;
4688 4688 return (strcmp(str1, str2));
4689 4689 }
4690 4690
4691 4691 static void
4692 4692 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4693 4693 {
4694 4694 const char *opt_desc[] = {
4695 4695 "-h", gettext("show this help message and exit"),
4696 4696 "-l", gettext("set permission locally"),
4697 4697 "-d", gettext("set permission for descents"),
4698 4698 "-u", gettext("set permission for user"),
4699 4699 "-g", gettext("set permission for group"),
4700 4700 "-e", gettext("set permission for everyone"),
4701 4701 "-c", gettext("set create time permission"),
4702 4702 "-s", gettext("define permission set"),
4703 4703 /* unallow only */
4704 4704 "-r", gettext("remove permissions recursively"),
4705 4705 };
4706 4706 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4707 4707 size_t allow_size = unallow_size - 2;
4708 4708 const char *props[ZFS_NUM_PROPS];
4709 4709 int i;
4710 4710 size_t count = 0;
4711 4711 FILE *fp = requested ? stdout : stderr;
4712 4712 zprop_desc_t *pdtbl = zfs_prop_get_table();
4713 4713 const char *fmt = gettext("%-16s %-14s\t%s\n");
4714 4714
4715 4715 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4716 4716 HELP_ALLOW));
4717 4717 (void) fprintf(fp, gettext("Options:\n"));
4718 4718 for (int i = 0; i < (un ? unallow_size : allow_size); i++) {
4719 4719 const char *opt = opt_desc[i++];
4720 4720 const char *optdsc = opt_desc[i];
4721 4721 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4722 4722 }
4723 4723
4724 4724 (void) fprintf(fp, gettext("\nThe following permissions are "
4725 4725 "supported:\n\n"));
4726 4726 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4727 4727 gettext("NOTES"));
4728 4728 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4729 4729 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4730 4730 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4731 4731 const char *perm_type = deleg_perm_type(perm_note);
4732 4732 const char *perm_comment = deleg_perm_comment(perm_note);
4733 4733 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4734 4734 }
4735 4735
4736 4736 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4737 4737 zprop_desc_t *pd = &pdtbl[i];
4738 4738 if (pd->pd_visible != B_TRUE)
4739 4739 continue;
4740 4740
4741 4741 if (pd->pd_attr == PROP_READONLY)
4742 4742 continue;
4743 4743
4744 4744 props[count++] = pd->pd_name;
4745 4745 }
4746 4746 props[count] = NULL;
4747 4747
4748 4748 qsort(props, count, sizeof (char *), prop_cmp);
4749 4749
4750 4750 for (i = 0; i < count; i++)
4751 4751 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4752 4752
4753 4753 if (msg != NULL)
4754 4754 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4755 4755
4756 4756 exit(requested ? 0 : 2);
4757 4757 }
4758 4758
4759 4759 static inline const char *
4760 4760 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4761 4761 char **permsp)
4762 4762 {
4763 4763 if (un && argc == expected_argc - 1)
4764 4764 *permsp = NULL;
4765 4765 else if (argc == expected_argc)
4766 4766 *permsp = argv[argc - 2];
4767 4767 else
4768 4768 allow_usage(un, B_FALSE,
4769 4769 gettext("wrong number of parameters\n"));
4770 4770
4771 4771 return (argv[argc - 1]);
4772 4772 }
4773 4773
4774 4774 static void
4775 4775 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4776 4776 {
4777 4777 int uge_sum = opts->user + opts->group + opts->everyone;
4778 4778 int csuge_sum = opts->create + opts->set + uge_sum;
4779 4779 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4780 4780 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4781 4781
4782 4782 if (uge_sum > 1)
4783 4783 allow_usage(un, B_FALSE,
4784 4784 gettext("-u, -g, and -e are mutually exclusive\n"));
4785 4785
4786 4786 if (opts->prt_usage) {
4787 4787 if (argc == 0 && all_sum == 0)
4788 4788 allow_usage(un, B_TRUE, NULL);
4789 4789 else
4790 4790 usage(B_FALSE);
4791 4791 }
4792 4792
4793 4793 if (opts->set) {
4794 4794 if (csuge_sum > 1)
4795 4795 allow_usage(un, B_FALSE,
4796 4796 gettext("invalid options combined with -s\n"));
4797 4797
4798 4798 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4799 4799 if (argv[0][0] != '@')
4800 4800 allow_usage(un, B_FALSE,
4801 4801 gettext("invalid set name: missing '@' prefix\n"));
4802 4802 opts->who = argv[0];
4803 4803 } else if (opts->create) {
4804 4804 if (ldcsuge_sum > 1)
4805 4805 allow_usage(un, B_FALSE,
4806 4806 gettext("invalid options combined with -c\n"));
4807 4807 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4808 4808 } else if (opts->everyone) {
4809 4809 if (csuge_sum > 1)
4810 4810 allow_usage(un, B_FALSE,
4811 4811 gettext("invalid options combined with -e\n"));
4812 4812 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4813 4813 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4814 4814 == 0) {
4815 4815 opts->everyone = B_TRUE;
4816 4816 argc--;
4817 4817 argv++;
4818 4818 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4819 4819 } else if (argc == 1 && !un) {
4820 4820 opts->prt_perms = B_TRUE;
4821 4821 opts->dataset = argv[argc-1];
4822 4822 } else {
4823 4823 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4824 4824 opts->who = argv[0];
4825 4825 }
4826 4826
4827 4827 if (!opts->local && !opts->descend) {
4828 4828 opts->local = B_TRUE;
4829 4829 opts->descend = B_TRUE;
4830 4830 }
4831 4831 }
4832 4832
4833 4833 static void
4834 4834 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4835 4835 const char *who, char *perms, nvlist_t *top_nvl)
4836 4836 {
4837 4837 int i;
4838 4838 char ld[2] = { '\0', '\0' };
4839 4839 char who_buf[ZFS_MAXNAMELEN+32];
4840 4840 char base_type = '\0';
4841 4841 char set_type = '\0';
4842 4842 nvlist_t *base_nvl = NULL;
4843 4843 nvlist_t *set_nvl = NULL;
4844 4844 nvlist_t *nvl;
4845 4845
4846 4846 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4847 4847 nomem();
4848 4848 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4849 4849 nomem();
4850 4850
4851 4851 switch (type) {
4852 4852 case ZFS_DELEG_NAMED_SET_SETS:
4853 4853 case ZFS_DELEG_NAMED_SET:
4854 4854 set_type = ZFS_DELEG_NAMED_SET_SETS;
4855 4855 base_type = ZFS_DELEG_NAMED_SET;
4856 4856 ld[0] = ZFS_DELEG_NA;
4857 4857 break;
4858 4858 case ZFS_DELEG_CREATE_SETS:
4859 4859 case ZFS_DELEG_CREATE:
4860 4860 set_type = ZFS_DELEG_CREATE_SETS;
4861 4861 base_type = ZFS_DELEG_CREATE;
4862 4862 ld[0] = ZFS_DELEG_NA;
4863 4863 break;
4864 4864 case ZFS_DELEG_USER_SETS:
4865 4865 case ZFS_DELEG_USER:
4866 4866 set_type = ZFS_DELEG_USER_SETS;
4867 4867 base_type = ZFS_DELEG_USER;
4868 4868 if (local)
4869 4869 ld[0] = ZFS_DELEG_LOCAL;
4870 4870 if (descend)
4871 4871 ld[1] = ZFS_DELEG_DESCENDENT;
4872 4872 break;
4873 4873 case ZFS_DELEG_GROUP_SETS:
4874 4874 case ZFS_DELEG_GROUP:
4875 4875 set_type = ZFS_DELEG_GROUP_SETS;
4876 4876 base_type = ZFS_DELEG_GROUP;
4877 4877 if (local)
4878 4878 ld[0] = ZFS_DELEG_LOCAL;
4879 4879 if (descend)
4880 4880 ld[1] = ZFS_DELEG_DESCENDENT;
4881 4881 break;
4882 4882 case ZFS_DELEG_EVERYONE_SETS:
4883 4883 case ZFS_DELEG_EVERYONE:
4884 4884 set_type = ZFS_DELEG_EVERYONE_SETS;
4885 4885 base_type = ZFS_DELEG_EVERYONE;
4886 4886 if (local)
4887 4887 ld[0] = ZFS_DELEG_LOCAL;
4888 4888 if (descend)
4889 4889 ld[1] = ZFS_DELEG_DESCENDENT;
4890 4890 break;
4891 4891
4892 4892 default:
4893 4893 assert(set_type != '\0' && base_type != '\0');
4894 4894 }
4895 4895
4896 4896 if (perms != NULL) {
4897 4897 char *curr = perms;
4898 4898 char *end = curr + strlen(perms);
4899 4899
4900 4900 while (curr < end) {
4901 4901 char *delim = strchr(curr, ',');
4902 4902 if (delim == NULL)
4903 4903 delim = end;
4904 4904 else
4905 4905 *delim = '\0';
4906 4906
4907 4907 if (curr[0] == '@')
4908 4908 nvl = set_nvl;
4909 4909 else
4910 4910 nvl = base_nvl;
4911 4911
4912 4912 (void) nvlist_add_boolean(nvl, curr);
4913 4913 if (delim != end)
4914 4914 *delim = ',';
4915 4915 curr = delim + 1;
4916 4916 }
4917 4917
4918 4918 for (i = 0; i < 2; i++) {
4919 4919 char locality = ld[i];
4920 4920 if (locality == 0)
4921 4921 continue;
4922 4922
4923 4923 if (!nvlist_empty(base_nvl)) {
4924 4924 if (who != NULL)
4925 4925 (void) snprintf(who_buf,
4926 4926 sizeof (who_buf), "%c%c$%s",
4927 4927 base_type, locality, who);
4928 4928 else
4929 4929 (void) snprintf(who_buf,
4930 4930 sizeof (who_buf), "%c%c$",
4931 4931 base_type, locality);
4932 4932
4933 4933 (void) nvlist_add_nvlist(top_nvl, who_buf,
4934 4934 base_nvl);
4935 4935 }
4936 4936
4937 4937
4938 4938 if (!nvlist_empty(set_nvl)) {
4939 4939 if (who != NULL)
4940 4940 (void) snprintf(who_buf,
4941 4941 sizeof (who_buf), "%c%c$%s",
4942 4942 set_type, locality, who);
4943 4943 else
4944 4944 (void) snprintf(who_buf,
4945 4945 sizeof (who_buf), "%c%c$",
4946 4946 set_type, locality);
4947 4947
4948 4948 (void) nvlist_add_nvlist(top_nvl, who_buf,
4949 4949 set_nvl);
4950 4950 }
4951 4951 }
4952 4952 } else {
4953 4953 for (i = 0; i < 2; i++) {
4954 4954 char locality = ld[i];
4955 4955 if (locality == 0)
4956 4956 continue;
4957 4957
4958 4958 if (who != NULL)
4959 4959 (void) snprintf(who_buf, sizeof (who_buf),
4960 4960 "%c%c$%s", base_type, locality, who);
4961 4961 else
4962 4962 (void) snprintf(who_buf, sizeof (who_buf),
4963 4963 "%c%c$", base_type, locality);
4964 4964 (void) nvlist_add_boolean(top_nvl, who_buf);
4965 4965
4966 4966 if (who != NULL)
4967 4967 (void) snprintf(who_buf, sizeof (who_buf),
4968 4968 "%c%c$%s", set_type, locality, who);
4969 4969 else
4970 4970 (void) snprintf(who_buf, sizeof (who_buf),
4971 4971 "%c%c$", set_type, locality);
4972 4972 (void) nvlist_add_boolean(top_nvl, who_buf);
4973 4973 }
4974 4974 }
4975 4975 }
4976 4976
4977 4977 static int
4978 4978 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4979 4979 {
4980 4980 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4981 4981 nomem();
4982 4982
4983 4983 if (opts->set) {
4984 4984 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4985 4985 opts->descend, opts->who, opts->perms, *nvlp);
4986 4986 } else if (opts->create) {
4987 4987 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4988 4988 opts->descend, NULL, opts->perms, *nvlp);
4989 4989 } else if (opts->everyone) {
4990 4990 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4991 4991 opts->descend, NULL, opts->perms, *nvlp);
4992 4992 } else {
4993 4993 char *curr = opts->who;
4994 4994 char *end = curr + strlen(curr);
4995 4995
4996 4996 while (curr < end) {
4997 4997 const char *who;
4998 4998 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
4999 4999 char *endch;
5000 5000 char *delim = strchr(curr, ',');
5001 5001 char errbuf[256];
5002 5002 char id[64];
5003 5003 struct passwd *p = NULL;
5004 5004 struct group *g = NULL;
5005 5005
5006 5006 uid_t rid;
5007 5007 if (delim == NULL)
5008 5008 delim = end;
5009 5009 else
5010 5010 *delim = '\0';
5011 5011
5012 5012 rid = (uid_t)strtol(curr, &endch, 0);
5013 5013 if (opts->user) {
5014 5014 who_type = ZFS_DELEG_USER;
5015 5015 if (*endch != '\0')
5016 5016 p = getpwnam(curr);
5017 5017 else
5018 5018 p = getpwuid(rid);
5019 5019
5020 5020 if (p != NULL)
5021 5021 rid = p->pw_uid;
5022 5022 else {
5023 5023 (void) snprintf(errbuf, 256, gettext(
5024 5024 "invalid user %s"), curr);
5025 5025 allow_usage(un, B_TRUE, errbuf);
5026 5026 }
5027 5027 } else if (opts->group) {
5028 5028 who_type = ZFS_DELEG_GROUP;
5029 5029 if (*endch != '\0')
5030 5030 g = getgrnam(curr);
5031 5031 else
5032 5032 g = getgrgid(rid);
5033 5033
5034 5034 if (g != NULL)
5035 5035 rid = g->gr_gid;
5036 5036 else {
5037 5037 (void) snprintf(errbuf, 256, gettext(
5038 5038 "invalid group %s"), curr);
5039 5039 allow_usage(un, B_TRUE, errbuf);
5040 5040 }
5041 5041 } else {
5042 5042 if (*endch != '\0') {
5043 5043 p = getpwnam(curr);
5044 5044 } else {
5045 5045 p = getpwuid(rid);
5046 5046 }
5047 5047
5048 5048 if (p == NULL) {
5049 5049 if (*endch != '\0') {
5050 5050 g = getgrnam(curr);
5051 5051 } else {
5052 5052 g = getgrgid(rid);
5053 5053 }
5054 5054 }
5055 5055
5056 5056 if (p != NULL) {
5057 5057 who_type = ZFS_DELEG_USER;
5058 5058 rid = p->pw_uid;
5059 5059 } else if (g != NULL) {
5060 5060 who_type = ZFS_DELEG_GROUP;
5061 5061 rid = g->gr_gid;
5062 5062 } else {
5063 5063 (void) snprintf(errbuf, 256, gettext(
5064 5064 "invalid user/group %s"), curr);
5065 5065 allow_usage(un, B_TRUE, errbuf);
5066 5066 }
5067 5067 }
5068 5068
5069 5069 (void) sprintf(id, "%u", rid);
5070 5070 who = id;
5071 5071
5072 5072 store_allow_perm(who_type, opts->local,
5073 5073 opts->descend, who, opts->perms, *nvlp);
5074 5074 curr = delim + 1;
5075 5075 }
5076 5076 }
5077 5077
5078 5078 return (0);
5079 5079 }
5080 5080
5081 5081 static void
5082 5082 print_set_creat_perms(uu_avl_t *who_avl)
5083 5083 {
5084 5084 const char *sc_title[] = {
5085 5085 gettext("Permission sets:\n"),
5086 5086 gettext("Create time permissions:\n"),
5087 5087 NULL
5088 5088 };
5089 5089 const char **title_ptr = sc_title;
5090 5090 who_perm_node_t *who_node = NULL;
5091 5091 int prev_weight = -1;
5092 5092
5093 5093 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5094 5094 who_node = uu_avl_next(who_avl, who_node)) {
5095 5095 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5096 5096 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5097 5097 const char *who_name = who_node->who_perm.who_name;
5098 5098 int weight = who_type2weight(who_type);
5099 5099 boolean_t first = B_TRUE;
5100 5100 deleg_perm_node_t *deleg_node;
5101 5101
5102 5102 if (prev_weight != weight) {
5103 5103 (void) printf(*title_ptr++);
5104 5104 prev_weight = weight;
5105 5105 }
5106 5106
5107 5107 if (who_name == NULL || strnlen(who_name, 1) == 0)
5108 5108 (void) printf("\t");
5109 5109 else
5110 5110 (void) printf("\t%s ", who_name);
5111 5111
5112 5112 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5113 5113 deleg_node = uu_avl_next(avl, deleg_node)) {
5114 5114 if (first) {
5115 5115 (void) printf("%s",
5116 5116 deleg_node->dpn_perm.dp_name);
5117 5117 first = B_FALSE;
5118 5118 } else
5119 5119 (void) printf(",%s",
5120 5120 deleg_node->dpn_perm.dp_name);
5121 5121 }
5122 5122
5123 5123 (void) printf("\n");
5124 5124 }
5125 5125 }
5126 5126
5127 5127 static void
5128 5128 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5129 5129 const char *title)
5130 5130 {
5131 5131 who_perm_node_t *who_node = NULL;
5132 5132 boolean_t prt_title = B_TRUE;
5133 5133 uu_avl_walk_t *walk;
5134 5134
5135 5135 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5136 5136 nomem();
5137 5137
5138 5138 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5139 5139 const char *who_name = who_node->who_perm.who_name;
5140 5140 const char *nice_who_name = who_node->who_perm.who_ug_name;
5141 5141 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5142 5142 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5143 5143 char delim = ' ';
5144 5144 deleg_perm_node_t *deleg_node;
5145 5145 boolean_t prt_who = B_TRUE;
5146 5146
5147 5147 for (deleg_node = uu_avl_first(avl);
5148 5148 deleg_node != NULL;
5149 5149 deleg_node = uu_avl_next(avl, deleg_node)) {
5150 5150 if (local != deleg_node->dpn_perm.dp_local ||
5151 5151 descend != deleg_node->dpn_perm.dp_descend)
5152 5152 continue;
5153 5153
5154 5154 if (prt_who) {
5155 5155 const char *who = NULL;
5156 5156 if (prt_title) {
5157 5157 prt_title = B_FALSE;
5158 5158 (void) printf(title);
5159 5159 }
5160 5160
5161 5161 switch (who_type) {
5162 5162 case ZFS_DELEG_USER_SETS:
5163 5163 case ZFS_DELEG_USER:
5164 5164 who = gettext("user");
5165 5165 if (nice_who_name)
5166 5166 who_name = nice_who_name;
5167 5167 break;
5168 5168 case ZFS_DELEG_GROUP_SETS:
5169 5169 case ZFS_DELEG_GROUP:
5170 5170 who = gettext("group");
5171 5171 if (nice_who_name)
5172 5172 who_name = nice_who_name;
5173 5173 break;
5174 5174 case ZFS_DELEG_EVERYONE_SETS:
5175 5175 case ZFS_DELEG_EVERYONE:
5176 5176 who = gettext("everyone");
5177 5177 who_name = NULL;
5178 5178 break;
5179 5179
5180 5180 default:
5181 5181 assert(who != NULL);
5182 5182 }
5183 5183
5184 5184 prt_who = B_FALSE;
5185 5185 if (who_name == NULL)
5186 5186 (void) printf("\t%s", who);
5187 5187 else
5188 5188 (void) printf("\t%s %s", who, who_name);
5189 5189 }
5190 5190
5191 5191 (void) printf("%c%s", delim,
5192 5192 deleg_node->dpn_perm.dp_name);
5193 5193 delim = ',';
5194 5194 }
5195 5195
5196 5196 if (!prt_who)
5197 5197 (void) printf("\n");
5198 5198 }
5199 5199
5200 5200 uu_avl_walk_end(walk);
5201 5201 }
5202 5202
5203 5203 static void
5204 5204 print_fs_perms(fs_perm_set_t *fspset)
5205 5205 {
5206 5206 fs_perm_node_t *node = NULL;
5207 5207 char buf[ZFS_MAXNAMELEN+32];
5208 5208 const char *dsname = buf;
5209 5209
5210 5210 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5211 5211 node = uu_list_next(fspset->fsps_list, node)) {
5212 5212 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5213 5213 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5214 5214 int left = 0;
5215 5215
5216 5216 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5217 5217 gettext("---- Permissions on %s "),
5218 5218 node->fspn_fsperm.fsp_name);
5219 5219 (void) printf(dsname);
5220 5220 left = 70 - strlen(buf);
5221 5221 while (left-- > 0)
5222 5222 (void) printf("-");
5223 5223 (void) printf("\n");
5224 5224
5225 5225 print_set_creat_perms(sc_avl);
5226 5226 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5227 5227 gettext("Local permissions:\n"));
5228 5228 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5229 5229 gettext("Descendent permissions:\n"));
5230 5230 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5231 5231 gettext("Local+Descendent permissions:\n"));
5232 5232 }
5233 5233 }
5234 5234
5235 5235 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5236 5236
5237 5237 struct deleg_perms {
5238 5238 boolean_t un;
5239 5239 nvlist_t *nvl;
5240 5240 };
5241 5241
5242 5242 static int
5243 5243 set_deleg_perms(zfs_handle_t *zhp, void *data)
5244 5244 {
5245 5245 struct deleg_perms *perms = (struct deleg_perms *)data;
5246 5246 zfs_type_t zfs_type = zfs_get_type(zhp);
5247 5247
5248 5248 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5249 5249 return (0);
5250 5250
5251 5251 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5252 5252 }
5253 5253
5254 5254 static int
5255 5255 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5256 5256 {
5257 5257 zfs_handle_t *zhp;
5258 5258 nvlist_t *perm_nvl = NULL;
5259 5259 nvlist_t *update_perm_nvl = NULL;
5260 5260 int error = 1;
5261 5261 int c;
5262 5262 struct allow_opts opts = { 0 };
5263 5263
5264 5264 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5265 5265
5266 5266 /* check opts */
5267 5267 while ((c = getopt(argc, argv, optstr)) != -1) {
5268 5268 switch (c) {
5269 5269 case 'l':
5270 5270 opts.local = B_TRUE;
5271 5271 break;
5272 5272 case 'd':
5273 5273 opts.descend = B_TRUE;
5274 5274 break;
5275 5275 case 'u':
5276 5276 opts.user = B_TRUE;
5277 5277 break;
5278 5278 case 'g':
5279 5279 opts.group = B_TRUE;
5280 5280 break;
5281 5281 case 'e':
5282 5282 opts.everyone = B_TRUE;
5283 5283 break;
5284 5284 case 's':
5285 5285 opts.set = B_TRUE;
5286 5286 break;
5287 5287 case 'c':
5288 5288 opts.create = B_TRUE;
5289 5289 break;
5290 5290 case 'r':
5291 5291 opts.recursive = B_TRUE;
5292 5292 break;
5293 5293 case ':':
5294 5294 (void) fprintf(stderr, gettext("missing argument for "
5295 5295 "'%c' option\n"), optopt);
5296 5296 usage(B_FALSE);
5297 5297 break;
5298 5298 case 'h':
5299 5299 opts.prt_usage = B_TRUE;
5300 5300 break;
5301 5301 case '?':
5302 5302 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5303 5303 optopt);
5304 5304 usage(B_FALSE);
5305 5305 }
5306 5306 }
5307 5307
5308 5308 argc -= optind;
5309 5309 argv += optind;
5310 5310
5311 5311 /* check arguments */
5312 5312 parse_allow_args(argc, argv, un, &opts);
5313 5313
5314 5314 /* try to open the dataset */
5315 5315 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5316 5316 ZFS_TYPE_VOLUME)) == NULL) {
5317 5317 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5318 5318 opts.dataset);
5319 5319 return (-1);
5320 5320 }
5321 5321
5322 5322 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5323 5323 goto cleanup2;
5324 5324
5325 5325 fs_perm_set_init(&fs_perm_set);
5326 5326 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5327 5327 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5328 5328 goto cleanup1;
5329 5329 }
5330 5330
5331 5331 if (opts.prt_perms)
5332 5332 print_fs_perms(&fs_perm_set);
5333 5333 else {
5334 5334 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5335 5335 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5336 5336 goto cleanup0;
5337 5337
5338 5338 if (un && opts.recursive) {
5339 5339 struct deleg_perms data = { un, update_perm_nvl };
↓ open down ↓ |
5339 lines elided |
↑ open up ↑ |
5340 5340 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5341 5341 &data) != 0)
5342 5342 goto cleanup0;
5343 5343 }
5344 5344 }
5345 5345
5346 5346 error = 0;
5347 5347
5348 5348 cleanup0:
5349 5349 nvlist_free(perm_nvl);
5350 - if (update_perm_nvl != NULL)
5351 - nvlist_free(update_perm_nvl);
5350 + nvlist_free(update_perm_nvl);
5352 5351 cleanup1:
5353 5352 fs_perm_set_fini(&fs_perm_set);
5354 5353 cleanup2:
5355 5354 zfs_close(zhp);
5356 5355
5357 5356 return (error);
5358 5357 }
5359 5358
5360 5359 static int
5361 5360 zfs_do_allow(int argc, char **argv)
5362 5361 {
5363 5362 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5364 5363 }
5365 5364
5366 5365 static int
5367 5366 zfs_do_unallow(int argc, char **argv)
5368 5367 {
5369 5368 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5370 5369 }
5371 5370
5372 5371 static int
5373 5372 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5374 5373 {
5375 5374 int errors = 0;
5376 5375 int i;
5377 5376 const char *tag;
5378 5377 boolean_t recursive = B_FALSE;
5379 5378 const char *opts = holding ? "rt" : "r";
5380 5379 int c;
5381 5380
5382 5381 /* check options */
5383 5382 while ((c = getopt(argc, argv, opts)) != -1) {
5384 5383 switch (c) {
5385 5384 case 'r':
5386 5385 recursive = B_TRUE;
5387 5386 break;
5388 5387 case '?':
5389 5388 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5390 5389 optopt);
5391 5390 usage(B_FALSE);
5392 5391 }
5393 5392 }
5394 5393
5395 5394 argc -= optind;
5396 5395 argv += optind;
5397 5396
5398 5397 /* check number of arguments */
5399 5398 if (argc < 2)
5400 5399 usage(B_FALSE);
5401 5400
5402 5401 tag = argv[0];
5403 5402 --argc;
5404 5403 ++argv;
5405 5404
5406 5405 if (holding && tag[0] == '.') {
5407 5406 /* tags starting with '.' are reserved for libzfs */
5408 5407 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5409 5408 usage(B_FALSE);
5410 5409 }
5411 5410
5412 5411 for (i = 0; i < argc; ++i) {
5413 5412 zfs_handle_t *zhp;
5414 5413 char parent[ZFS_MAXNAMELEN];
5415 5414 const char *delim;
5416 5415 char *path = argv[i];
5417 5416
5418 5417 delim = strchr(path, '@');
5419 5418 if (delim == NULL) {
5420 5419 (void) fprintf(stderr,
5421 5420 gettext("'%s' is not a snapshot\n"), path);
5422 5421 ++errors;
5423 5422 continue;
5424 5423 }
5425 5424 (void) strncpy(parent, path, delim - path);
5426 5425 parent[delim - path] = '\0';
5427 5426
5428 5427 zhp = zfs_open(g_zfs, parent,
5429 5428 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5430 5429 if (zhp == NULL) {
5431 5430 ++errors;
5432 5431 continue;
5433 5432 }
5434 5433 if (holding) {
5435 5434 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5436 5435 ++errors;
5437 5436 } else {
5438 5437 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5439 5438 ++errors;
5440 5439 }
5441 5440 zfs_close(zhp);
5442 5441 }
5443 5442
5444 5443 return (errors != 0);
5445 5444 }
5446 5445
5447 5446 /*
5448 5447 * zfs hold [-r] [-t] <tag> <snap> ...
5449 5448 *
5450 5449 * -r Recursively hold
5451 5450 *
5452 5451 * Apply a user-hold with the given tag to the list of snapshots.
5453 5452 */
5454 5453 static int
5455 5454 zfs_do_hold(int argc, char **argv)
5456 5455 {
5457 5456 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5458 5457 }
5459 5458
5460 5459 /*
5461 5460 * zfs release [-r] <tag> <snap> ...
5462 5461 *
5463 5462 * -r Recursively release
5464 5463 *
5465 5464 * Release a user-hold with the given tag from the list of snapshots.
5466 5465 */
5467 5466 static int
5468 5467 zfs_do_release(int argc, char **argv)
5469 5468 {
5470 5469 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5471 5470 }
5472 5471
5473 5472 typedef struct holds_cbdata {
5474 5473 boolean_t cb_recursive;
5475 5474 const char *cb_snapname;
5476 5475 nvlist_t **cb_nvlp;
5477 5476 size_t cb_max_namelen;
5478 5477 size_t cb_max_taglen;
5479 5478 } holds_cbdata_t;
5480 5479
5481 5480 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5482 5481 #define DATETIME_BUF_LEN (32)
5483 5482 /*
5484 5483 *
5485 5484 */
5486 5485 static void
5487 5486 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5488 5487 {
5489 5488 int i;
5490 5489 nvpair_t *nvp = NULL;
5491 5490 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5492 5491 const char *col;
5493 5492
5494 5493 if (!scripted) {
5495 5494 for (i = 0; i < 3; i++) {
5496 5495 col = gettext(hdr_cols[i]);
5497 5496 if (i < 2)
5498 5497 (void) printf("%-*s ", i ? tagwidth : nwidth,
5499 5498 col);
5500 5499 else
5501 5500 (void) printf("%s\n", col);
5502 5501 }
5503 5502 }
5504 5503
5505 5504 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5506 5505 char *zname = nvpair_name(nvp);
5507 5506 nvlist_t *nvl2;
5508 5507 nvpair_t *nvp2 = NULL;
5509 5508 (void) nvpair_value_nvlist(nvp, &nvl2);
5510 5509 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5511 5510 char tsbuf[DATETIME_BUF_LEN];
5512 5511 char *tagname = nvpair_name(nvp2);
5513 5512 uint64_t val = 0;
5514 5513 time_t time;
5515 5514 struct tm t;
5516 5515 char sep = scripted ? '\t' : ' ';
5517 5516 size_t sepnum = scripted ? 1 : 2;
5518 5517
5519 5518 (void) nvpair_value_uint64(nvp2, &val);
5520 5519 time = (time_t)val;
5521 5520 (void) localtime_r(&time, &t);
5522 5521 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5523 5522 gettext(STRFTIME_FMT_STR), &t);
5524 5523
5525 5524 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5526 5525 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5527 5526 }
5528 5527 }
5529 5528 }
5530 5529
5531 5530 /*
5532 5531 * Generic callback function to list a dataset or snapshot.
5533 5532 */
5534 5533 static int
5535 5534 holds_callback(zfs_handle_t *zhp, void *data)
5536 5535 {
5537 5536 holds_cbdata_t *cbp = data;
5538 5537 nvlist_t *top_nvl = *cbp->cb_nvlp;
5539 5538 nvlist_t *nvl = NULL;
5540 5539 nvpair_t *nvp = NULL;
5541 5540 const char *zname = zfs_get_name(zhp);
5542 5541 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5543 5542
5544 5543 if (cbp->cb_recursive) {
5545 5544 const char *snapname;
5546 5545 char *delim = strchr(zname, '@');
5547 5546 if (delim == NULL)
5548 5547 return (0);
5549 5548
5550 5549 snapname = delim + 1;
5551 5550 if (strcmp(cbp->cb_snapname, snapname))
5552 5551 return (0);
5553 5552 }
5554 5553
5555 5554 if (zfs_get_holds(zhp, &nvl) != 0)
5556 5555 return (-1);
5557 5556
5558 5557 if (znamelen > cbp->cb_max_namelen)
5559 5558 cbp->cb_max_namelen = znamelen;
5560 5559
5561 5560 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5562 5561 const char *tag = nvpair_name(nvp);
5563 5562 size_t taglen = strnlen(tag, MAXNAMELEN);
5564 5563 if (taglen > cbp->cb_max_taglen)
5565 5564 cbp->cb_max_taglen = taglen;
5566 5565 }
5567 5566
5568 5567 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5569 5568 }
5570 5569
5571 5570 /*
5572 5571 * zfs holds [-r] <snap> ...
5573 5572 *
5574 5573 * -r Recursively hold
5575 5574 */
5576 5575 static int
5577 5576 zfs_do_holds(int argc, char **argv)
5578 5577 {
5579 5578 int errors = 0;
5580 5579 int c;
5581 5580 int i;
5582 5581 boolean_t scripted = B_FALSE;
5583 5582 boolean_t recursive = B_FALSE;
5584 5583 const char *opts = "rH";
5585 5584 nvlist_t *nvl;
5586 5585
5587 5586 int types = ZFS_TYPE_SNAPSHOT;
5588 5587 holds_cbdata_t cb = { 0 };
5589 5588
5590 5589 int limit = 0;
5591 5590 int ret = 0;
5592 5591 int flags = 0;
5593 5592
5594 5593 /* check options */
5595 5594 while ((c = getopt(argc, argv, opts)) != -1) {
5596 5595 switch (c) {
5597 5596 case 'r':
5598 5597 recursive = B_TRUE;
5599 5598 break;
5600 5599 case 'H':
5601 5600 scripted = B_TRUE;
5602 5601 break;
5603 5602 case '?':
5604 5603 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5605 5604 optopt);
5606 5605 usage(B_FALSE);
5607 5606 }
5608 5607 }
5609 5608
5610 5609 if (recursive) {
5611 5610 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5612 5611 flags |= ZFS_ITER_RECURSE;
5613 5612 }
5614 5613
5615 5614 argc -= optind;
5616 5615 argv += optind;
5617 5616
5618 5617 /* check number of arguments */
5619 5618 if (argc < 1)
5620 5619 usage(B_FALSE);
5621 5620
5622 5621 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5623 5622 nomem();
5624 5623
5625 5624 for (i = 0; i < argc; ++i) {
5626 5625 char *snapshot = argv[i];
5627 5626 const char *delim;
5628 5627 const char *snapname;
5629 5628
5630 5629 delim = strchr(snapshot, '@');
5631 5630 if (delim == NULL) {
5632 5631 (void) fprintf(stderr,
5633 5632 gettext("'%s' is not a snapshot\n"), snapshot);
5634 5633 ++errors;
5635 5634 continue;
5636 5635 }
5637 5636 snapname = delim + 1;
5638 5637 if (recursive)
5639 5638 snapshot[delim - snapshot] = '\0';
5640 5639
5641 5640 cb.cb_recursive = recursive;
5642 5641 cb.cb_snapname = snapname;
5643 5642 cb.cb_nvlp = &nvl;
5644 5643
5645 5644 /*
5646 5645 * 1. collect holds data, set format options
5647 5646 */
5648 5647 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5649 5648 holds_callback, &cb);
5650 5649 if (ret != 0)
5651 5650 ++errors;
5652 5651 }
5653 5652
5654 5653 /*
5655 5654 * 2. print holds data
5656 5655 */
5657 5656 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5658 5657
5659 5658 if (nvlist_empty(nvl))
5660 5659 (void) printf(gettext("no datasets available\n"));
5661 5660
5662 5661 nvlist_free(nvl);
5663 5662
5664 5663 return (0 != errors);
5665 5664 }
5666 5665
5667 5666 #define CHECK_SPINNER 30
5668 5667 #define SPINNER_TIME 3 /* seconds */
5669 5668 #define MOUNT_TIME 5 /* seconds */
5670 5669
5671 5670 static int
5672 5671 get_one_dataset(zfs_handle_t *zhp, void *data)
5673 5672 {
5674 5673 static char *spin[] = { "-", "\\", "|", "/" };
5675 5674 static int spinval = 0;
5676 5675 static int spincheck = 0;
5677 5676 static time_t last_spin_time = (time_t)0;
5678 5677 get_all_cb_t *cbp = data;
5679 5678 zfs_type_t type = zfs_get_type(zhp);
5680 5679
5681 5680 if (cbp->cb_verbose) {
5682 5681 if (--spincheck < 0) {
5683 5682 time_t now = time(NULL);
5684 5683 if (last_spin_time + SPINNER_TIME < now) {
5685 5684 update_progress(spin[spinval++ % 4]);
5686 5685 last_spin_time = now;
5687 5686 }
5688 5687 spincheck = CHECK_SPINNER;
5689 5688 }
5690 5689 }
5691 5690
5692 5691 /*
5693 5692 * Interate over any nested datasets.
5694 5693 */
5695 5694 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5696 5695 zfs_close(zhp);
5697 5696 return (1);
5698 5697 }
5699 5698
5700 5699 /*
5701 5700 * Skip any datasets whose type does not match.
5702 5701 */
5703 5702 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5704 5703 zfs_close(zhp);
5705 5704 return (0);
5706 5705 }
5707 5706 libzfs_add_handle(cbp, zhp);
5708 5707 assert(cbp->cb_used <= cbp->cb_alloc);
5709 5708
5710 5709 return (0);
5711 5710 }
5712 5711
5713 5712 static void
5714 5713 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5715 5714 {
5716 5715 get_all_cb_t cb = { 0 };
5717 5716 cb.cb_verbose = verbose;
5718 5717 cb.cb_getone = get_one_dataset;
5719 5718
5720 5719 if (verbose)
5721 5720 set_progress_header(gettext("Reading ZFS config"));
5722 5721 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5723 5722
5724 5723 *dslist = cb.cb_handles;
5725 5724 *count = cb.cb_used;
5726 5725
5727 5726 if (verbose)
5728 5727 finish_progress(gettext("done."));
5729 5728 }
5730 5729
5731 5730 /*
5732 5731 * Generic callback for sharing or mounting filesystems. Because the code is so
5733 5732 * similar, we have a common function with an extra parameter to determine which
5734 5733 * mode we are using.
5735 5734 */
5736 5735 #define OP_SHARE 0x1
5737 5736 #define OP_MOUNT 0x2
5738 5737
5739 5738 /*
5740 5739 * Share or mount a dataset.
5741 5740 */
5742 5741 static int
5743 5742 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5744 5743 boolean_t explicit, const char *options)
5745 5744 {
5746 5745 char mountpoint[ZFS_MAXPROPLEN];
5747 5746 char shareopts[ZFS_MAXPROPLEN];
5748 5747 char smbshareopts[ZFS_MAXPROPLEN];
5749 5748 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5750 5749 struct mnttab mnt;
5751 5750 uint64_t zoned, canmount;
5752 5751 boolean_t shared_nfs, shared_smb;
5753 5752
5754 5753 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5755 5754
5756 5755 /*
5757 5756 * Check to make sure we can mount/share this dataset. If we
5758 5757 * are in the global zone and the filesystem is exported to a
5759 5758 * local zone, or if we are in a local zone and the
5760 5759 * filesystem is not exported, then it is an error.
5761 5760 */
5762 5761 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5763 5762
5764 5763 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5765 5764 if (!explicit)
5766 5765 return (0);
5767 5766
5768 5767 (void) fprintf(stderr, gettext("cannot %s '%s': "
5769 5768 "dataset is exported to a local zone\n"), cmdname,
5770 5769 zfs_get_name(zhp));
5771 5770 return (1);
5772 5771
5773 5772 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5774 5773 if (!explicit)
5775 5774 return (0);
5776 5775
5777 5776 (void) fprintf(stderr, gettext("cannot %s '%s': "
5778 5777 "permission denied\n"), cmdname,
5779 5778 zfs_get_name(zhp));
5780 5779 return (1);
5781 5780 }
5782 5781
5783 5782 /*
5784 5783 * Ignore any filesystems which don't apply to us. This
5785 5784 * includes those with a legacy mountpoint, or those with
5786 5785 * legacy share options.
5787 5786 */
5788 5787 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5789 5788 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5790 5789 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5791 5790 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5792 5791 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5793 5792 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5794 5793
5795 5794 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5796 5795 strcmp(smbshareopts, "off") == 0) {
5797 5796 if (!explicit)
5798 5797 return (0);
5799 5798
5800 5799 (void) fprintf(stderr, gettext("cannot share '%s': "
5801 5800 "legacy share\n"), zfs_get_name(zhp));
5802 5801 (void) fprintf(stderr, gettext("use share(1M) to "
5803 5802 "share this filesystem, or set "
5804 5803 "sharenfs property on\n"));
5805 5804 return (1);
5806 5805 }
5807 5806
5808 5807 /*
5809 5808 * We cannot share or mount legacy filesystems. If the
5810 5809 * shareopts is non-legacy but the mountpoint is legacy, we
5811 5810 * treat it as a legacy share.
5812 5811 */
5813 5812 if (strcmp(mountpoint, "legacy") == 0) {
5814 5813 if (!explicit)
5815 5814 return (0);
5816 5815
5817 5816 (void) fprintf(stderr, gettext("cannot %s '%s': "
5818 5817 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5819 5818 (void) fprintf(stderr, gettext("use %s(1M) to "
5820 5819 "%s this filesystem\n"), cmdname, cmdname);
5821 5820 return (1);
5822 5821 }
5823 5822
5824 5823 if (strcmp(mountpoint, "none") == 0) {
5825 5824 if (!explicit)
5826 5825 return (0);
5827 5826
5828 5827 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5829 5828 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5830 5829 return (1);
5831 5830 }
5832 5831
5833 5832 /*
5834 5833 * canmount explicit outcome
5835 5834 * on no pass through
5836 5835 * on yes pass through
5837 5836 * off no return 0
5838 5837 * off yes display error, return 1
5839 5838 * noauto no return 0
5840 5839 * noauto yes pass through
5841 5840 */
5842 5841 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5843 5842 if (canmount == ZFS_CANMOUNT_OFF) {
5844 5843 if (!explicit)
5845 5844 return (0);
5846 5845
5847 5846 (void) fprintf(stderr, gettext("cannot %s '%s': "
5848 5847 "'canmount' property is set to 'off'\n"), cmdname,
5849 5848 zfs_get_name(zhp));
5850 5849 return (1);
5851 5850 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5852 5851 return (0);
5853 5852 }
5854 5853
5855 5854 /*
5856 5855 * If this filesystem is inconsistent and has a receive resume
5857 5856 * token, we can not mount it.
5858 5857 */
5859 5858 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5860 5859 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5861 5860 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5862 5861 if (!explicit)
5863 5862 return (0);
5864 5863
5865 5864 (void) fprintf(stderr, gettext("cannot %s '%s': "
5866 5865 "Contains partially-completed state from "
5867 5866 "\"zfs receive -r\", which can be resumed with "
5868 5867 "\"zfs send -t\"\n"),
5869 5868 cmdname, zfs_get_name(zhp));
5870 5869 return (1);
5871 5870 }
5872 5871
5873 5872 /*
5874 5873 * At this point, we have verified that the mountpoint and/or
5875 5874 * shareopts are appropriate for auto management. If the
5876 5875 * filesystem is already mounted or shared, return (failing
5877 5876 * for explicit requests); otherwise mount or share the
5878 5877 * filesystem.
5879 5878 */
5880 5879 switch (op) {
5881 5880 case OP_SHARE:
5882 5881
5883 5882 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5884 5883 shared_smb = zfs_is_shared_smb(zhp, NULL);
5885 5884
5886 5885 if ((shared_nfs && shared_smb) ||
5887 5886 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5888 5887 strcmp(smbshareopts, "off") == 0) ||
5889 5888 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5890 5889 strcmp(shareopts, "off") == 0)) {
5891 5890 if (!explicit)
5892 5891 return (0);
5893 5892
5894 5893 (void) fprintf(stderr, gettext("cannot share "
5895 5894 "'%s': filesystem already shared\n"),
5896 5895 zfs_get_name(zhp));
5897 5896 return (1);
5898 5897 }
5899 5898
5900 5899 if (!zfs_is_mounted(zhp, NULL) &&
5901 5900 zfs_mount(zhp, NULL, 0) != 0)
5902 5901 return (1);
5903 5902
5904 5903 if (protocol == NULL) {
5905 5904 if (zfs_shareall(zhp) != 0)
5906 5905 return (1);
5907 5906 } else if (strcmp(protocol, "nfs") == 0) {
5908 5907 if (zfs_share_nfs(zhp))
5909 5908 return (1);
5910 5909 } else if (strcmp(protocol, "smb") == 0) {
5911 5910 if (zfs_share_smb(zhp))
5912 5911 return (1);
5913 5912 } else {
5914 5913 (void) fprintf(stderr, gettext("cannot share "
5915 5914 "'%s': invalid share type '%s' "
5916 5915 "specified\n"),
5917 5916 zfs_get_name(zhp), protocol);
5918 5917 return (1);
5919 5918 }
5920 5919
5921 5920 break;
5922 5921
5923 5922 case OP_MOUNT:
5924 5923 if (options == NULL)
5925 5924 mnt.mnt_mntopts = "";
5926 5925 else
5927 5926 mnt.mnt_mntopts = (char *)options;
5928 5927
5929 5928 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5930 5929 zfs_is_mounted(zhp, NULL)) {
5931 5930 if (!explicit)
5932 5931 return (0);
5933 5932
5934 5933 (void) fprintf(stderr, gettext("cannot mount "
5935 5934 "'%s': filesystem already mounted\n"),
5936 5935 zfs_get_name(zhp));
5937 5936 return (1);
5938 5937 }
5939 5938
5940 5939 if (zfs_mount(zhp, options, flags) != 0)
5941 5940 return (1);
5942 5941 break;
5943 5942 }
5944 5943
5945 5944 return (0);
5946 5945 }
5947 5946
5948 5947 /*
5949 5948 * Reports progress in the form "(current/total)". Not thread-safe.
5950 5949 */
5951 5950 static void
5952 5951 report_mount_progress(int current, int total)
5953 5952 {
5954 5953 static time_t last_progress_time = 0;
5955 5954 time_t now = time(NULL);
5956 5955 char info[32];
5957 5956
5958 5957 /* report 1..n instead of 0..n-1 */
5959 5958 ++current;
5960 5959
5961 5960 /* display header if we're here for the first time */
5962 5961 if (current == 1) {
5963 5962 set_progress_header(gettext("Mounting ZFS filesystems"));
5964 5963 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5965 5964 /* too soon to report again */
5966 5965 return;
5967 5966 }
5968 5967
5969 5968 last_progress_time = now;
5970 5969
5971 5970 (void) sprintf(info, "(%d/%d)", current, total);
5972 5971
5973 5972 if (current == total)
5974 5973 finish_progress(info);
5975 5974 else
5976 5975 update_progress(info);
5977 5976 }
5978 5977
5979 5978 static void
5980 5979 append_options(char *mntopts, char *newopts)
5981 5980 {
5982 5981 int len = strlen(mntopts);
5983 5982
5984 5983 /* original length plus new string to append plus 1 for the comma */
5985 5984 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5986 5985 (void) fprintf(stderr, gettext("the opts argument for "
5987 5986 "'%c' option is too long (more than %d chars)\n"),
5988 5987 "-o", MNT_LINE_MAX);
5989 5988 usage(B_FALSE);
5990 5989 }
5991 5990
5992 5991 if (*mntopts)
5993 5992 mntopts[len++] = ',';
5994 5993
5995 5994 (void) strcpy(&mntopts[len], newopts);
5996 5995 }
5997 5996
5998 5997 static int
5999 5998 share_mount(int op, int argc, char **argv)
6000 5999 {
6001 6000 int do_all = 0;
6002 6001 boolean_t verbose = B_FALSE;
6003 6002 int c, ret = 0;
6004 6003 char *options = NULL;
6005 6004 int flags = 0;
6006 6005
6007 6006 /* check options */
6008 6007 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6009 6008 != -1) {
6010 6009 switch (c) {
6011 6010 case 'a':
6012 6011 do_all = 1;
6013 6012 break;
6014 6013 case 'v':
6015 6014 verbose = B_TRUE;
6016 6015 break;
6017 6016 case 'o':
6018 6017 if (*optarg == '\0') {
6019 6018 (void) fprintf(stderr, gettext("empty mount "
6020 6019 "options (-o) specified\n"));
6021 6020 usage(B_FALSE);
6022 6021 }
6023 6022
6024 6023 if (options == NULL)
6025 6024 options = safe_malloc(MNT_LINE_MAX + 1);
6026 6025
6027 6026 /* option validation is done later */
6028 6027 append_options(options, optarg);
6029 6028 break;
6030 6029
6031 6030 case 'O':
6032 6031 flags |= MS_OVERLAY;
6033 6032 break;
6034 6033 case ':':
6035 6034 (void) fprintf(stderr, gettext("missing argument for "
6036 6035 "'%c' option\n"), optopt);
6037 6036 usage(B_FALSE);
6038 6037 break;
6039 6038 case '?':
6040 6039 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6041 6040 optopt);
6042 6041 usage(B_FALSE);
6043 6042 }
6044 6043 }
6045 6044
6046 6045 argc -= optind;
6047 6046 argv += optind;
6048 6047
6049 6048 /* check number of arguments */
6050 6049 if (do_all) {
6051 6050 zfs_handle_t **dslist = NULL;
6052 6051 size_t i, count = 0;
6053 6052 char *protocol = NULL;
6054 6053
6055 6054 if (op == OP_SHARE && argc > 0) {
6056 6055 if (strcmp(argv[0], "nfs") != 0 &&
6057 6056 strcmp(argv[0], "smb") != 0) {
6058 6057 (void) fprintf(stderr, gettext("share type "
6059 6058 "must be 'nfs' or 'smb'\n"));
6060 6059 usage(B_FALSE);
6061 6060 }
6062 6061 protocol = argv[0];
6063 6062 argc--;
6064 6063 argv++;
6065 6064 }
6066 6065
6067 6066 if (argc != 0) {
6068 6067 (void) fprintf(stderr, gettext("too many arguments\n"));
6069 6068 usage(B_FALSE);
6070 6069 }
6071 6070
6072 6071 start_progress_timer();
6073 6072 get_all_datasets(&dslist, &count, verbose);
6074 6073
6075 6074 if (count == 0)
6076 6075 return (0);
6077 6076
6078 6077 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6079 6078
6080 6079 for (i = 0; i < count; i++) {
6081 6080 if (verbose)
6082 6081 report_mount_progress(i, count);
6083 6082
6084 6083 if (share_mount_one(dslist[i], op, flags, protocol,
6085 6084 B_FALSE, options) != 0)
6086 6085 ret = 1;
6087 6086 zfs_close(dslist[i]);
6088 6087 }
6089 6088
6090 6089 free(dslist);
6091 6090 } else if (argc == 0) {
6092 6091 struct mnttab entry;
6093 6092
6094 6093 if ((op == OP_SHARE) || (options != NULL)) {
6095 6094 (void) fprintf(stderr, gettext("missing filesystem "
6096 6095 "argument (specify -a for all)\n"));
6097 6096 usage(B_FALSE);
6098 6097 }
6099 6098
6100 6099 /*
6101 6100 * When mount is given no arguments, go through /etc/mnttab and
6102 6101 * display any active ZFS mounts. We hide any snapshots, since
6103 6102 * they are controlled automatically.
6104 6103 */
6105 6104 rewind(mnttab_file);
6106 6105 while (getmntent(mnttab_file, &entry) == 0) {
6107 6106 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6108 6107 strchr(entry.mnt_special, '@') != NULL)
6109 6108 continue;
6110 6109
6111 6110 (void) printf("%-30s %s\n", entry.mnt_special,
6112 6111 entry.mnt_mountp);
6113 6112 }
6114 6113
6115 6114 } else {
6116 6115 zfs_handle_t *zhp;
6117 6116
6118 6117 if (argc > 1) {
6119 6118 (void) fprintf(stderr,
6120 6119 gettext("too many arguments\n"));
6121 6120 usage(B_FALSE);
6122 6121 }
6123 6122
6124 6123 if ((zhp = zfs_open(g_zfs, argv[0],
6125 6124 ZFS_TYPE_FILESYSTEM)) == NULL) {
6126 6125 ret = 1;
6127 6126 } else {
6128 6127 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6129 6128 options);
6130 6129 zfs_close(zhp);
6131 6130 }
6132 6131 }
6133 6132
6134 6133 return (ret);
6135 6134 }
6136 6135
6137 6136 /*
6138 6137 * zfs mount -a [nfs]
6139 6138 * zfs mount filesystem
6140 6139 *
6141 6140 * Mount all filesystems, or mount the given filesystem.
6142 6141 */
6143 6142 static int
6144 6143 zfs_do_mount(int argc, char **argv)
6145 6144 {
6146 6145 return (share_mount(OP_MOUNT, argc, argv));
6147 6146 }
6148 6147
6149 6148 /*
6150 6149 * zfs share -a [nfs | smb]
6151 6150 * zfs share filesystem
6152 6151 *
6153 6152 * Share all filesystems, or share the given filesystem.
6154 6153 */
6155 6154 static int
6156 6155 zfs_do_share(int argc, char **argv)
6157 6156 {
6158 6157 return (share_mount(OP_SHARE, argc, argv));
6159 6158 }
6160 6159
6161 6160 typedef struct unshare_unmount_node {
6162 6161 zfs_handle_t *un_zhp;
6163 6162 char *un_mountp;
6164 6163 uu_avl_node_t un_avlnode;
6165 6164 } unshare_unmount_node_t;
6166 6165
6167 6166 /* ARGSUSED */
6168 6167 static int
6169 6168 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6170 6169 {
6171 6170 const unshare_unmount_node_t *l = larg;
6172 6171 const unshare_unmount_node_t *r = rarg;
6173 6172
6174 6173 return (strcmp(l->un_mountp, r->un_mountp));
6175 6174 }
6176 6175
6177 6176 /*
6178 6177 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6179 6178 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6180 6179 * and unmount it appropriately.
6181 6180 */
6182 6181 static int
6183 6182 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6184 6183 {
6185 6184 zfs_handle_t *zhp;
6186 6185 int ret = 0;
6187 6186 struct stat64 statbuf;
6188 6187 struct extmnttab entry;
6189 6188 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6190 6189 ino_t path_inode;
6191 6190
6192 6191 /*
6193 6192 * Search for the path in /etc/mnttab. Rather than looking for the
6194 6193 * specific path, which can be fooled by non-standard paths (i.e. ".."
6195 6194 * or "//"), we stat() the path and search for the corresponding
6196 6195 * (major,minor) device pair.
6197 6196 */
6198 6197 if (stat64(path, &statbuf) != 0) {
6199 6198 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6200 6199 cmdname, path, strerror(errno));
6201 6200 return (1);
6202 6201 }
6203 6202 path_inode = statbuf.st_ino;
6204 6203
6205 6204 /*
6206 6205 * Search for the given (major,minor) pair in the mount table.
6207 6206 */
6208 6207 rewind(mnttab_file);
6209 6208 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6210 6209 if (entry.mnt_major == major(statbuf.st_dev) &&
6211 6210 entry.mnt_minor == minor(statbuf.st_dev))
6212 6211 break;
6213 6212 }
6214 6213 if (ret != 0) {
6215 6214 if (op == OP_SHARE) {
6216 6215 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6217 6216 "currently mounted\n"), cmdname, path);
6218 6217 return (1);
6219 6218 }
6220 6219 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6221 6220 path);
6222 6221 if ((ret = umount2(path, flags)) != 0)
6223 6222 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6224 6223 strerror(errno));
6225 6224 return (ret != 0);
6226 6225 }
6227 6226
6228 6227 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6229 6228 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6230 6229 "filesystem\n"), cmdname, path);
6231 6230 return (1);
6232 6231 }
6233 6232
6234 6233 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6235 6234 ZFS_TYPE_FILESYSTEM)) == NULL)
6236 6235 return (1);
6237 6236
6238 6237 ret = 1;
6239 6238 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6240 6239 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6241 6240 cmdname, path, strerror(errno));
6242 6241 goto out;
6243 6242 } else if (statbuf.st_ino != path_inode) {
6244 6243 (void) fprintf(stderr, gettext("cannot "
6245 6244 "%s '%s': not a mountpoint\n"), cmdname, path);
6246 6245 goto out;
6247 6246 }
6248 6247
6249 6248 if (op == OP_SHARE) {
6250 6249 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6251 6250 char smbshare_prop[ZFS_MAXPROPLEN];
6252 6251
6253 6252 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6254 6253 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6255 6254 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6256 6255 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6257 6256
6258 6257 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6259 6258 strcmp(smbshare_prop, "off") == 0) {
6260 6259 (void) fprintf(stderr, gettext("cannot unshare "
6261 6260 "'%s': legacy share\n"), path);
6262 6261 (void) fprintf(stderr, gettext("use "
6263 6262 "unshare(1M) to unshare this filesystem\n"));
6264 6263 } else if (!zfs_is_shared(zhp)) {
6265 6264 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6266 6265 "not currently shared\n"), path);
6267 6266 } else {
6268 6267 ret = zfs_unshareall_bypath(zhp, path);
6269 6268 }
6270 6269 } else {
6271 6270 char mtpt_prop[ZFS_MAXPROPLEN];
6272 6271
6273 6272 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6274 6273 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6275 6274
6276 6275 if (is_manual) {
6277 6276 ret = zfs_unmount(zhp, NULL, flags);
6278 6277 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6279 6278 (void) fprintf(stderr, gettext("cannot unmount "
6280 6279 "'%s': legacy mountpoint\n"),
6281 6280 zfs_get_name(zhp));
6282 6281 (void) fprintf(stderr, gettext("use umount(1M) "
6283 6282 "to unmount this filesystem\n"));
6284 6283 } else {
6285 6284 ret = zfs_unmountall(zhp, flags);
6286 6285 }
6287 6286 }
6288 6287
6289 6288 out:
6290 6289 zfs_close(zhp);
6291 6290
6292 6291 return (ret != 0);
6293 6292 }
6294 6293
6295 6294 /*
6296 6295 * Generic callback for unsharing or unmounting a filesystem.
6297 6296 */
6298 6297 static int
6299 6298 unshare_unmount(int op, int argc, char **argv)
6300 6299 {
6301 6300 int do_all = 0;
6302 6301 int flags = 0;
6303 6302 int ret = 0;
6304 6303 int c;
6305 6304 zfs_handle_t *zhp;
6306 6305 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6307 6306 char sharesmb[ZFS_MAXPROPLEN];
6308 6307
6309 6308 /* check options */
6310 6309 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6311 6310 switch (c) {
6312 6311 case 'a':
6313 6312 do_all = 1;
6314 6313 break;
6315 6314 case 'f':
6316 6315 flags = MS_FORCE;
6317 6316 break;
6318 6317 case '?':
6319 6318 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6320 6319 optopt);
6321 6320 usage(B_FALSE);
6322 6321 }
6323 6322 }
6324 6323
6325 6324 argc -= optind;
6326 6325 argv += optind;
6327 6326
6328 6327 if (do_all) {
6329 6328 /*
6330 6329 * We could make use of zfs_for_each() to walk all datasets in
6331 6330 * the system, but this would be very inefficient, especially
6332 6331 * since we would have to linearly search /etc/mnttab for each
6333 6332 * one. Instead, do one pass through /etc/mnttab looking for
6334 6333 * zfs entries and call zfs_unmount() for each one.
6335 6334 *
6336 6335 * Things get a little tricky if the administrator has created
6337 6336 * mountpoints beneath other ZFS filesystems. In this case, we
6338 6337 * have to unmount the deepest filesystems first. To accomplish
6339 6338 * this, we place all the mountpoints in an AVL tree sorted by
6340 6339 * the special type (dataset name), and walk the result in
6341 6340 * reverse to make sure to get any snapshots first.
6342 6341 */
6343 6342 struct mnttab entry;
6344 6343 uu_avl_pool_t *pool;
6345 6344 uu_avl_t *tree = NULL;
6346 6345 unshare_unmount_node_t *node;
6347 6346 uu_avl_index_t idx;
6348 6347 uu_avl_walk_t *walk;
6349 6348
6350 6349 if (argc != 0) {
6351 6350 (void) fprintf(stderr, gettext("too many arguments\n"));
6352 6351 usage(B_FALSE);
6353 6352 }
6354 6353
6355 6354 if (((pool = uu_avl_pool_create("unmount_pool",
6356 6355 sizeof (unshare_unmount_node_t),
6357 6356 offsetof(unshare_unmount_node_t, un_avlnode),
6358 6357 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6359 6358 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6360 6359 nomem();
6361 6360
6362 6361 rewind(mnttab_file);
6363 6362 while (getmntent(mnttab_file, &entry) == 0) {
6364 6363
6365 6364 /* ignore non-ZFS entries */
6366 6365 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6367 6366 continue;
6368 6367
6369 6368 /* ignore snapshots */
6370 6369 if (strchr(entry.mnt_special, '@') != NULL)
6371 6370 continue;
6372 6371
6373 6372 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6374 6373 ZFS_TYPE_FILESYSTEM)) == NULL) {
6375 6374 ret = 1;
6376 6375 continue;
6377 6376 }
6378 6377
6379 6378 switch (op) {
6380 6379 case OP_SHARE:
6381 6380 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6382 6381 nfs_mnt_prop,
6383 6382 sizeof (nfs_mnt_prop),
6384 6383 NULL, NULL, 0, B_FALSE) == 0);
6385 6384 if (strcmp(nfs_mnt_prop, "off") != 0)
6386 6385 break;
6387 6386 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6388 6387 nfs_mnt_prop,
6389 6388 sizeof (nfs_mnt_prop),
6390 6389 NULL, NULL, 0, B_FALSE) == 0);
6391 6390 if (strcmp(nfs_mnt_prop, "off") == 0)
6392 6391 continue;
6393 6392 break;
6394 6393 case OP_MOUNT:
6395 6394 /* Ignore legacy mounts */
6396 6395 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6397 6396 nfs_mnt_prop,
6398 6397 sizeof (nfs_mnt_prop),
6399 6398 NULL, NULL, 0, B_FALSE) == 0);
6400 6399 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6401 6400 continue;
6402 6401 /* Ignore canmount=noauto mounts */
6403 6402 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6404 6403 ZFS_CANMOUNT_NOAUTO)
6405 6404 continue;
6406 6405 default:
6407 6406 break;
6408 6407 }
6409 6408
6410 6409 node = safe_malloc(sizeof (unshare_unmount_node_t));
6411 6410 node->un_zhp = zhp;
6412 6411 node->un_mountp = safe_strdup(entry.mnt_mountp);
6413 6412
6414 6413 uu_avl_node_init(node, &node->un_avlnode, pool);
6415 6414
6416 6415 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6417 6416 uu_avl_insert(tree, node, idx);
6418 6417 } else {
6419 6418 zfs_close(node->un_zhp);
6420 6419 free(node->un_mountp);
6421 6420 free(node);
6422 6421 }
6423 6422 }
6424 6423
6425 6424 /*
6426 6425 * Walk the AVL tree in reverse, unmounting each filesystem and
6427 6426 * removing it from the AVL tree in the process.
6428 6427 */
6429 6428 if ((walk = uu_avl_walk_start(tree,
6430 6429 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6431 6430 nomem();
6432 6431
6433 6432 while ((node = uu_avl_walk_next(walk)) != NULL) {
6434 6433 uu_avl_remove(tree, node);
6435 6434
6436 6435 switch (op) {
6437 6436 case OP_SHARE:
6438 6437 if (zfs_unshareall_bypath(node->un_zhp,
6439 6438 node->un_mountp) != 0)
6440 6439 ret = 1;
6441 6440 break;
6442 6441
6443 6442 case OP_MOUNT:
6444 6443 if (zfs_unmount(node->un_zhp,
6445 6444 node->un_mountp, flags) != 0)
6446 6445 ret = 1;
6447 6446 break;
6448 6447 }
6449 6448
6450 6449 zfs_close(node->un_zhp);
6451 6450 free(node->un_mountp);
6452 6451 free(node);
6453 6452 }
6454 6453
6455 6454 uu_avl_walk_end(walk);
6456 6455 uu_avl_destroy(tree);
6457 6456 uu_avl_pool_destroy(pool);
6458 6457
6459 6458 } else {
6460 6459 if (argc != 1) {
6461 6460 if (argc == 0)
6462 6461 (void) fprintf(stderr,
6463 6462 gettext("missing filesystem argument\n"));
6464 6463 else
6465 6464 (void) fprintf(stderr,
6466 6465 gettext("too many arguments\n"));
6467 6466 usage(B_FALSE);
6468 6467 }
6469 6468
6470 6469 /*
6471 6470 * We have an argument, but it may be a full path or a ZFS
6472 6471 * filesystem. Pass full paths off to unmount_path() (shared by
6473 6472 * manual_unmount), otherwise open the filesystem and pass to
6474 6473 * zfs_unmount().
6475 6474 */
6476 6475 if (argv[0][0] == '/')
6477 6476 return (unshare_unmount_path(op, argv[0],
6478 6477 flags, B_FALSE));
6479 6478
6480 6479 if ((zhp = zfs_open(g_zfs, argv[0],
6481 6480 ZFS_TYPE_FILESYSTEM)) == NULL)
6482 6481 return (1);
6483 6482
6484 6483 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6485 6484 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6486 6485 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6487 6486 NULL, 0, B_FALSE) == 0);
6488 6487
6489 6488 switch (op) {
6490 6489 case OP_SHARE:
6491 6490 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6492 6491 nfs_mnt_prop,
6493 6492 sizeof (nfs_mnt_prop),
6494 6493 NULL, NULL, 0, B_FALSE) == 0);
6495 6494 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6496 6495 sharesmb, sizeof (sharesmb), NULL, NULL,
6497 6496 0, B_FALSE) == 0);
6498 6497
6499 6498 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6500 6499 strcmp(sharesmb, "off") == 0) {
6501 6500 (void) fprintf(stderr, gettext("cannot "
6502 6501 "unshare '%s': legacy share\n"),
6503 6502 zfs_get_name(zhp));
6504 6503 (void) fprintf(stderr, gettext("use "
6505 6504 "unshare(1M) to unshare this "
6506 6505 "filesystem\n"));
6507 6506 ret = 1;
6508 6507 } else if (!zfs_is_shared(zhp)) {
6509 6508 (void) fprintf(stderr, gettext("cannot "
6510 6509 "unshare '%s': not currently "
6511 6510 "shared\n"), zfs_get_name(zhp));
6512 6511 ret = 1;
6513 6512 } else if (zfs_unshareall(zhp) != 0) {
6514 6513 ret = 1;
6515 6514 }
6516 6515 break;
6517 6516
6518 6517 case OP_MOUNT:
6519 6518 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6520 6519 (void) fprintf(stderr, gettext("cannot "
6521 6520 "unmount '%s': legacy "
6522 6521 "mountpoint\n"), zfs_get_name(zhp));
6523 6522 (void) fprintf(stderr, gettext("use "
6524 6523 "umount(1M) to unmount this "
6525 6524 "filesystem\n"));
6526 6525 ret = 1;
6527 6526 } else if (!zfs_is_mounted(zhp, NULL)) {
6528 6527 (void) fprintf(stderr, gettext("cannot "
6529 6528 "unmount '%s': not currently "
6530 6529 "mounted\n"),
6531 6530 zfs_get_name(zhp));
6532 6531 ret = 1;
6533 6532 } else if (zfs_unmountall(zhp, flags) != 0) {
6534 6533 ret = 1;
6535 6534 }
6536 6535 break;
6537 6536 }
6538 6537
6539 6538 zfs_close(zhp);
6540 6539 }
6541 6540
6542 6541 return (ret);
6543 6542 }
6544 6543
6545 6544 /*
6546 6545 * zfs unmount -a
6547 6546 * zfs unmount filesystem
6548 6547 *
6549 6548 * Unmount all filesystems, or a specific ZFS filesystem.
6550 6549 */
6551 6550 static int
6552 6551 zfs_do_unmount(int argc, char **argv)
6553 6552 {
6554 6553 return (unshare_unmount(OP_MOUNT, argc, argv));
6555 6554 }
6556 6555
6557 6556 /*
6558 6557 * zfs unshare -a
6559 6558 * zfs unshare filesystem
6560 6559 *
6561 6560 * Unshare all filesystems, or a specific ZFS filesystem.
6562 6561 */
6563 6562 static int
6564 6563 zfs_do_unshare(int argc, char **argv)
6565 6564 {
6566 6565 return (unshare_unmount(OP_SHARE, argc, argv));
6567 6566 }
6568 6567
6569 6568 /*
6570 6569 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6571 6570 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6572 6571 */
6573 6572 static int
6574 6573 manual_mount(int argc, char **argv)
6575 6574 {
6576 6575 zfs_handle_t *zhp;
6577 6576 char mountpoint[ZFS_MAXPROPLEN];
6578 6577 char mntopts[MNT_LINE_MAX] = { '\0' };
6579 6578 int ret = 0;
6580 6579 int c;
6581 6580 int flags = 0;
6582 6581 char *dataset, *path;
6583 6582
6584 6583 /* check options */
6585 6584 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6586 6585 switch (c) {
6587 6586 case 'o':
6588 6587 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6589 6588 break;
6590 6589 case 'O':
6591 6590 flags |= MS_OVERLAY;
6592 6591 break;
6593 6592 case 'm':
6594 6593 flags |= MS_NOMNTTAB;
6595 6594 break;
6596 6595 case ':':
6597 6596 (void) fprintf(stderr, gettext("missing argument for "
6598 6597 "'%c' option\n"), optopt);
6599 6598 usage(B_FALSE);
6600 6599 break;
6601 6600 case '?':
6602 6601 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6603 6602 optopt);
6604 6603 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6605 6604 "<path>\n"));
6606 6605 return (2);
6607 6606 }
6608 6607 }
6609 6608
6610 6609 argc -= optind;
6611 6610 argv += optind;
6612 6611
6613 6612 /* check that we only have two arguments */
6614 6613 if (argc != 2) {
6615 6614 if (argc == 0)
6616 6615 (void) fprintf(stderr, gettext("missing dataset "
6617 6616 "argument\n"));
6618 6617 else if (argc == 1)
6619 6618 (void) fprintf(stderr,
6620 6619 gettext("missing mountpoint argument\n"));
6621 6620 else
6622 6621 (void) fprintf(stderr, gettext("too many arguments\n"));
6623 6622 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6624 6623 return (2);
6625 6624 }
6626 6625
6627 6626 dataset = argv[0];
6628 6627 path = argv[1];
6629 6628
6630 6629 /* try to open the dataset */
6631 6630 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6632 6631 return (1);
6633 6632
6634 6633 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6635 6634 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6636 6635
6637 6636 /* check for legacy mountpoint and complain appropriately */
6638 6637 ret = 0;
6639 6638 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6640 6639 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
6641 6640 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6642 6641 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6643 6642 strerror(errno));
6644 6643 ret = 1;
6645 6644 }
6646 6645 } else {
6647 6646 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6648 6647 "mounted using 'mount -F zfs'\n"), dataset);
6649 6648 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6650 6649 "instead.\n"), path);
6651 6650 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6652 6651 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6653 6652 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6654 6653 "information.\n"));
6655 6654 ret = 1;
6656 6655 }
6657 6656
6658 6657 return (ret);
6659 6658 }
6660 6659
6661 6660 /*
6662 6661 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6663 6662 * unmounts of non-legacy filesystems, as this is the dominant administrative
6664 6663 * interface.
6665 6664 */
6666 6665 static int
6667 6666 manual_unmount(int argc, char **argv)
6668 6667 {
6669 6668 int flags = 0;
6670 6669 int c;
6671 6670
6672 6671 /* check options */
6673 6672 while ((c = getopt(argc, argv, "f")) != -1) {
6674 6673 switch (c) {
6675 6674 case 'f':
6676 6675 flags = MS_FORCE;
6677 6676 break;
6678 6677 case '?':
6679 6678 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6680 6679 optopt);
6681 6680 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6682 6681 "<path>\n"));
6683 6682 return (2);
6684 6683 }
6685 6684 }
6686 6685
6687 6686 argc -= optind;
6688 6687 argv += optind;
6689 6688
6690 6689 /* check arguments */
6691 6690 if (argc != 1) {
6692 6691 if (argc == 0)
6693 6692 (void) fprintf(stderr, gettext("missing path "
6694 6693 "argument\n"));
6695 6694 else
6696 6695 (void) fprintf(stderr, gettext("too many arguments\n"));
6697 6696 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6698 6697 return (2);
6699 6698 }
6700 6699
6701 6700 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6702 6701 }
6703 6702
6704 6703 static int
6705 6704 find_command_idx(char *command, int *idx)
6706 6705 {
6707 6706 int i;
6708 6707
6709 6708 for (i = 0; i < NCOMMAND; i++) {
6710 6709 if (command_table[i].name == NULL)
6711 6710 continue;
6712 6711
6713 6712 if (strcmp(command, command_table[i].name) == 0) {
6714 6713 *idx = i;
6715 6714 return (0);
6716 6715 }
6717 6716 }
6718 6717 return (1);
6719 6718 }
6720 6719
6721 6720 static int
6722 6721 zfs_do_diff(int argc, char **argv)
6723 6722 {
6724 6723 zfs_handle_t *zhp;
6725 6724 int flags = 0;
6726 6725 char *tosnap = NULL;
6727 6726 char *fromsnap = NULL;
6728 6727 char *atp, *copy;
6729 6728 int err = 0;
6730 6729 int c;
6731 6730
6732 6731 while ((c = getopt(argc, argv, "FHt")) != -1) {
6733 6732 switch (c) {
6734 6733 case 'F':
6735 6734 flags |= ZFS_DIFF_CLASSIFY;
6736 6735 break;
6737 6736 case 'H':
6738 6737 flags |= ZFS_DIFF_PARSEABLE;
6739 6738 break;
6740 6739 case 't':
6741 6740 flags |= ZFS_DIFF_TIMESTAMP;
6742 6741 break;
6743 6742 default:
6744 6743 (void) fprintf(stderr,
6745 6744 gettext("invalid option '%c'\n"), optopt);
6746 6745 usage(B_FALSE);
6747 6746 }
6748 6747 }
6749 6748
6750 6749 argc -= optind;
6751 6750 argv += optind;
6752 6751
6753 6752 if (argc < 1) {
6754 6753 (void) fprintf(stderr,
6755 6754 gettext("must provide at least one snapshot name\n"));
6756 6755 usage(B_FALSE);
6757 6756 }
6758 6757
6759 6758 if (argc > 2) {
6760 6759 (void) fprintf(stderr, gettext("too many arguments\n"));
6761 6760 usage(B_FALSE);
6762 6761 }
6763 6762
6764 6763 fromsnap = argv[0];
6765 6764 tosnap = (argc == 2) ? argv[1] : NULL;
6766 6765
6767 6766 copy = NULL;
6768 6767 if (*fromsnap != '@')
6769 6768 copy = strdup(fromsnap);
6770 6769 else if (tosnap)
6771 6770 copy = strdup(tosnap);
6772 6771 if (copy == NULL)
6773 6772 usage(B_FALSE);
6774 6773
6775 6774 if ((atp = strchr(copy, '@')) != NULL)
6776 6775 *atp = '\0';
6777 6776
6778 6777 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6779 6778 return (1);
6780 6779
6781 6780 free(copy);
6782 6781
6783 6782 /*
6784 6783 * Ignore SIGPIPE so that the library can give us
6785 6784 * information on any failure
6786 6785 */
6787 6786 (void) sigignore(SIGPIPE);
6788 6787
6789 6788 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6790 6789
6791 6790 zfs_close(zhp);
6792 6791
6793 6792 return (err != 0);
6794 6793 }
6795 6794
6796 6795 /*
6797 6796 * zfs bookmark <fs@snap> <fs#bmark>
6798 6797 *
6799 6798 * Creates a bookmark with the given name from the given snapshot.
6800 6799 */
6801 6800 static int
6802 6801 zfs_do_bookmark(int argc, char **argv)
6803 6802 {
6804 6803 char snapname[ZFS_MAXNAMELEN];
6805 6804 zfs_handle_t *zhp;
6806 6805 nvlist_t *nvl;
6807 6806 int ret = 0;
6808 6807 int c;
6809 6808
6810 6809 /* check options */
6811 6810 while ((c = getopt(argc, argv, "")) != -1) {
6812 6811 switch (c) {
6813 6812 case '?':
6814 6813 (void) fprintf(stderr,
6815 6814 gettext("invalid option '%c'\n"), optopt);
6816 6815 goto usage;
6817 6816 }
6818 6817 }
6819 6818
6820 6819 argc -= optind;
6821 6820 argv += optind;
6822 6821
6823 6822 /* check number of arguments */
6824 6823 if (argc < 1) {
6825 6824 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6826 6825 goto usage;
6827 6826 }
6828 6827 if (argc < 2) {
6829 6828 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6830 6829 goto usage;
6831 6830 }
6832 6831
6833 6832 if (strchr(argv[1], '#') == NULL) {
6834 6833 (void) fprintf(stderr,
6835 6834 gettext("invalid bookmark name '%s' -- "
6836 6835 "must contain a '#'\n"), argv[1]);
6837 6836 goto usage;
6838 6837 }
6839 6838
6840 6839 if (argv[0][0] == '@') {
6841 6840 /*
6842 6841 * Snapshot name begins with @.
6843 6842 * Default to same fs as bookmark.
6844 6843 */
6845 6844 (void) strncpy(snapname, argv[1], sizeof (snapname));
6846 6845 *strchr(snapname, '#') = '\0';
6847 6846 (void) strlcat(snapname, argv[0], sizeof (snapname));
6848 6847 } else {
6849 6848 (void) strncpy(snapname, argv[0], sizeof (snapname));
6850 6849 }
6851 6850 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6852 6851 if (zhp == NULL)
6853 6852 goto usage;
6854 6853 zfs_close(zhp);
6855 6854
6856 6855
6857 6856 nvl = fnvlist_alloc();
6858 6857 fnvlist_add_string(nvl, argv[1], snapname);
6859 6858 ret = lzc_bookmark(nvl, NULL);
6860 6859 fnvlist_free(nvl);
6861 6860
6862 6861 if (ret != 0) {
6863 6862 const char *err_msg;
6864 6863 char errbuf[1024];
6865 6864
6866 6865 (void) snprintf(errbuf, sizeof (errbuf),
6867 6866 dgettext(TEXT_DOMAIN,
6868 6867 "cannot create bookmark '%s'"), argv[1]);
6869 6868
6870 6869 switch (ret) {
6871 6870 case EXDEV:
6872 6871 err_msg = "bookmark is in a different pool";
6873 6872 break;
6874 6873 case EEXIST:
6875 6874 err_msg = "bookmark exists";
6876 6875 break;
6877 6876 case EINVAL:
6878 6877 err_msg = "invalid argument";
6879 6878 break;
6880 6879 case ENOTSUP:
6881 6880 err_msg = "bookmark feature not enabled";
6882 6881 break;
6883 6882 case ENOSPC:
6884 6883 err_msg = "out of space";
6885 6884 break;
6886 6885 default:
6887 6886 err_msg = "unknown error";
6888 6887 break;
6889 6888 }
6890 6889 (void) fprintf(stderr, "%s: %s\n", errbuf,
6891 6890 dgettext(TEXT_DOMAIN, err_msg));
6892 6891 }
6893 6892
6894 6893 return (ret != 0);
6895 6894
6896 6895 usage:
6897 6896 usage(B_FALSE);
6898 6897 return (-1);
6899 6898 }
6900 6899
6901 6900 int
6902 6901 main(int argc, char **argv)
6903 6902 {
6904 6903 int ret = 0;
6905 6904 int i;
6906 6905 char *progname;
6907 6906 char *cmdname;
6908 6907
6909 6908 (void) setlocale(LC_ALL, "");
6910 6909 (void) textdomain(TEXT_DOMAIN);
6911 6910
6912 6911 opterr = 0;
6913 6912
6914 6913 if ((g_zfs = libzfs_init()) == NULL) {
6915 6914 (void) fprintf(stderr, gettext("internal error: failed to "
6916 6915 "initialize ZFS library\n"));
6917 6916 return (1);
6918 6917 }
6919 6918
6920 6919 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6921 6920
6922 6921 libzfs_print_on_error(g_zfs, B_TRUE);
6923 6922
6924 6923 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6925 6924 (void) fprintf(stderr, gettext("internal error: unable to "
6926 6925 "open %s\n"), MNTTAB);
6927 6926 return (1);
6928 6927 }
6929 6928
6930 6929 /*
6931 6930 * This command also doubles as the /etc/fs mount and unmount program.
6932 6931 * Determine if we should take this behavior based on argv[0].
6933 6932 */
6934 6933 progname = basename(argv[0]);
6935 6934 if (strcmp(progname, "mount") == 0) {
6936 6935 ret = manual_mount(argc, argv);
6937 6936 } else if (strcmp(progname, "umount") == 0) {
6938 6937 ret = manual_unmount(argc, argv);
6939 6938 } else {
6940 6939 /*
6941 6940 * Make sure the user has specified some command.
6942 6941 */
6943 6942 if (argc < 2) {
6944 6943 (void) fprintf(stderr, gettext("missing command\n"));
6945 6944 usage(B_FALSE);
6946 6945 }
6947 6946
6948 6947 cmdname = argv[1];
6949 6948
6950 6949 /*
6951 6950 * The 'umount' command is an alias for 'unmount'
6952 6951 */
6953 6952 if (strcmp(cmdname, "umount") == 0)
6954 6953 cmdname = "unmount";
6955 6954
6956 6955 /*
6957 6956 * The 'recv' command is an alias for 'receive'
6958 6957 */
6959 6958 if (strcmp(cmdname, "recv") == 0)
6960 6959 cmdname = "receive";
6961 6960
6962 6961 /*
6963 6962 * The 'snap' command is an alias for 'snapshot'
6964 6963 */
6965 6964 if (strcmp(cmdname, "snap") == 0)
6966 6965 cmdname = "snapshot";
6967 6966
6968 6967 /*
6969 6968 * Special case '-?'
6970 6969 */
6971 6970 if (strcmp(cmdname, "-?") == 0)
6972 6971 usage(B_TRUE);
6973 6972
6974 6973 /*
6975 6974 * Run the appropriate command.
6976 6975 */
6977 6976 libzfs_mnttab_cache(g_zfs, B_TRUE);
6978 6977 if (find_command_idx(cmdname, &i) == 0) {
6979 6978 current_command = &command_table[i];
6980 6979 ret = command_table[i].func(argc - 1, argv + 1);
6981 6980 } else if (strchr(cmdname, '=') != NULL) {
6982 6981 verify(find_command_idx("set", &i) == 0);
6983 6982 current_command = &command_table[i];
6984 6983 ret = command_table[i].func(argc, argv);
6985 6984 } else {
6986 6985 (void) fprintf(stderr, gettext("unrecognized "
6987 6986 "command '%s'\n"), cmdname);
6988 6987 usage(B_FALSE);
6989 6988 }
6990 6989 libzfs_mnttab_cache(g_zfs, B_FALSE);
6991 6990 }
6992 6991
6993 6992 (void) fclose(mnttab_file);
6994 6993
6995 6994 if (ret == 0 && log_history)
6996 6995 (void) zpool_log_history(g_zfs, history_str);
6997 6996
6998 6997 libzfs_fini(g_zfs);
6999 6998
7000 6999 /*
7001 7000 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7002 7001 * for the purposes of running ::findleaks.
7003 7002 */
7004 7003 if (getenv("ZFS_ABORT") != NULL) {
7005 7004 (void) printf("dumping core by request\n");
7006 7005 abort();
7007 7006 }
7008 7007
7009 7008 return (ret);
7010 7009 }
↓ open down ↓ |
1649 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX