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