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