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