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