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