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