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