]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
9ae529ec | 21 | |
34dc7c2f | 22 | /* |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
788eb90c | 24 | * Copyright (c) 2013, Joyent, Inc. All rights reserved. |
23de906c | 25 | * Copyright (c) 2011, 2014 by Delphix. All rights reserved. |
34dc7c2f BB |
26 | */ |
27 | ||
34dc7c2f BB |
28 | /* |
29 | * Internal utility routines for the ZFS library. | |
30 | */ | |
31 | ||
32 | #include <errno.h> | |
33 | #include <fcntl.h> | |
34 | #include <libintl.h> | |
35 | #include <stdarg.h> | |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <strings.h> | |
39 | #include <unistd.h> | |
40 | #include <ctype.h> | |
41 | #include <math.h> | |
d603ed6c | 42 | #include <sys/stat.h> |
34dc7c2f BB |
43 | #include <sys/mnttab.h> |
44 | #include <sys/mntent.h> | |
45 | #include <sys/types.h> | |
4b1abce9 | 46 | #include <wait.h> |
34dc7c2f BB |
47 | |
48 | #include <libzfs.h> | |
6f1ffb06 | 49 | #include <libzfs_core.h> |
34dc7c2f BB |
50 | |
51 | #include "libzfs_impl.h" | |
52 | #include "zfs_prop.h" | |
9ae529ec | 53 | #include "zfeature_common.h" |
34dc7c2f BB |
54 | |
55 | int | |
56 | libzfs_errno(libzfs_handle_t *hdl) | |
57 | { | |
58 | return (hdl->libzfs_error); | |
59 | } | |
60 | ||
65037d9b BB |
61 | const char * |
62 | libzfs_error_init(int error) | |
63 | { | |
64 | switch (error) { | |
65 | case ENXIO: | |
66 | return (dgettext(TEXT_DOMAIN, "The ZFS modules are not " | |
67 | "loaded.\nTry running '/sbin/modprobe zfs' as root " | |
68 | "to load them.\n")); | |
69 | case ENOENT: | |
70 | return (dgettext(TEXT_DOMAIN, "The /dev/zfs device is " | |
71 | "missing and must be created.\nTry running 'udevadm " | |
72 | "trigger' as root to create it.\n")); | |
73 | case ENOEXEC: | |
74 | return (dgettext(TEXT_DOMAIN, "The ZFS modules cannot be " | |
75 | "auto-loaded.\nTry running '/sbin/modprobe zfs' as " | |
76 | "root to manually load them.\n")); | |
77 | case EACCES: | |
78 | return (dgettext(TEXT_DOMAIN, "Permission denied the " | |
79 | "ZFS utilities must be run as root.\n")); | |
80 | default: | |
81 | return (dgettext(TEXT_DOMAIN, "Failed to initialize the " | |
82 | "libzfs library.\n")); | |
83 | } | |
84 | } | |
85 | ||
34dc7c2f BB |
86 | const char * |
87 | libzfs_error_action(libzfs_handle_t *hdl) | |
88 | { | |
89 | return (hdl->libzfs_action); | |
90 | } | |
91 | ||
92 | const char * | |
93 | libzfs_error_description(libzfs_handle_t *hdl) | |
94 | { | |
95 | if (hdl->libzfs_desc[0] != '\0') | |
96 | return (hdl->libzfs_desc); | |
97 | ||
98 | switch (hdl->libzfs_error) { | |
99 | case EZFS_NOMEM: | |
100 | return (dgettext(TEXT_DOMAIN, "out of memory")); | |
101 | case EZFS_BADPROP: | |
102 | return (dgettext(TEXT_DOMAIN, "invalid property value")); | |
103 | case EZFS_PROPREADONLY: | |
572e2857 | 104 | return (dgettext(TEXT_DOMAIN, "read-only property")); |
34dc7c2f BB |
105 | case EZFS_PROPTYPE: |
106 | return (dgettext(TEXT_DOMAIN, "property doesn't apply to " | |
107 | "datasets of this type")); | |
108 | case EZFS_PROPNONINHERIT: | |
109 | return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); | |
110 | case EZFS_PROPSPACE: | |
111 | return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); | |
112 | case EZFS_BADTYPE: | |
113 | return (dgettext(TEXT_DOMAIN, "operation not applicable to " | |
114 | "datasets of this type")); | |
115 | case EZFS_BUSY: | |
116 | return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); | |
117 | case EZFS_EXISTS: | |
118 | return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); | |
119 | case EZFS_NOENT: | |
120 | return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); | |
121 | case EZFS_BADSTREAM: | |
122 | return (dgettext(TEXT_DOMAIN, "invalid backup stream")); | |
123 | case EZFS_DSREADONLY: | |
572e2857 | 124 | return (dgettext(TEXT_DOMAIN, "dataset is read-only")); |
34dc7c2f BB |
125 | case EZFS_VOLTOOBIG: |
126 | return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " | |
127 | "this system")); | |
34dc7c2f BB |
128 | case EZFS_INVALIDNAME: |
129 | return (dgettext(TEXT_DOMAIN, "invalid name")); | |
130 | case EZFS_BADRESTORE: | |
131 | return (dgettext(TEXT_DOMAIN, "unable to restore to " | |
132 | "destination")); | |
133 | case EZFS_BADBACKUP: | |
134 | return (dgettext(TEXT_DOMAIN, "backup failed")); | |
135 | case EZFS_BADTARGET: | |
136 | return (dgettext(TEXT_DOMAIN, "invalid target vdev")); | |
137 | case EZFS_NODEVICE: | |
138 | return (dgettext(TEXT_DOMAIN, "no such device in pool")); | |
139 | case EZFS_BADDEV: | |
140 | return (dgettext(TEXT_DOMAIN, "invalid device")); | |
141 | case EZFS_NOREPLICAS: | |
142 | return (dgettext(TEXT_DOMAIN, "no valid replicas")); | |
143 | case EZFS_RESILVERING: | |
144 | return (dgettext(TEXT_DOMAIN, "currently resilvering")); | |
145 | case EZFS_BADVERSION: | |
9ae529ec CS |
146 | return (dgettext(TEXT_DOMAIN, "unsupported version or " |
147 | "feature")); | |
34dc7c2f BB |
148 | case EZFS_POOLUNAVAIL: |
149 | return (dgettext(TEXT_DOMAIN, "pool is unavailable")); | |
150 | case EZFS_DEVOVERFLOW: | |
151 | return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); | |
152 | case EZFS_BADPATH: | |
153 | return (dgettext(TEXT_DOMAIN, "must be an absolute path")); | |
154 | case EZFS_CROSSTARGET: | |
155 | return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " | |
156 | "pools")); | |
157 | case EZFS_ZONED: | |
158 | return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); | |
159 | case EZFS_MOUNTFAILED: | |
160 | return (dgettext(TEXT_DOMAIN, "mount failed")); | |
161 | case EZFS_UMOUNTFAILED: | |
162 | return (dgettext(TEXT_DOMAIN, "umount failed")); | |
163 | case EZFS_UNSHARENFSFAILED: | |
164 | return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); | |
165 | case EZFS_SHARENFSFAILED: | |
166 | return (dgettext(TEXT_DOMAIN, "share(1M) failed")); | |
167 | case EZFS_UNSHARESMBFAILED: | |
168 | return (dgettext(TEXT_DOMAIN, "smb remove share failed")); | |
169 | case EZFS_SHARESMBFAILED: | |
170 | return (dgettext(TEXT_DOMAIN, "smb add share failed")); | |
34dc7c2f BB |
171 | case EZFS_PERM: |
172 | return (dgettext(TEXT_DOMAIN, "permission denied")); | |
173 | case EZFS_NOSPC: | |
174 | return (dgettext(TEXT_DOMAIN, "out of space")); | |
428870ff BB |
175 | case EZFS_FAULT: |
176 | return (dgettext(TEXT_DOMAIN, "bad address")); | |
34dc7c2f BB |
177 | case EZFS_IO: |
178 | return (dgettext(TEXT_DOMAIN, "I/O error")); | |
179 | case EZFS_INTR: | |
180 | return (dgettext(TEXT_DOMAIN, "signal received")); | |
181 | case EZFS_ISSPARE: | |
182 | return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " | |
183 | "spare")); | |
184 | case EZFS_INVALCONFIG: | |
185 | return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); | |
186 | case EZFS_RECURSIVE: | |
187 | return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); | |
188 | case EZFS_NOHISTORY: | |
189 | return (dgettext(TEXT_DOMAIN, "no history available")); | |
34dc7c2f BB |
190 | case EZFS_POOLPROPS: |
191 | return (dgettext(TEXT_DOMAIN, "failed to retrieve " | |
192 | "pool properties")); | |
193 | case EZFS_POOL_NOTSUP: | |
194 | return (dgettext(TEXT_DOMAIN, "operation not supported " | |
195 | "on this type of pool")); | |
196 | case EZFS_POOL_INVALARG: | |
197 | return (dgettext(TEXT_DOMAIN, "invalid argument for " | |
198 | "this pool operation")); | |
199 | case EZFS_NAMETOOLONG: | |
200 | return (dgettext(TEXT_DOMAIN, "dataset name is too long")); | |
201 | case EZFS_OPENFAILED: | |
202 | return (dgettext(TEXT_DOMAIN, "open failed")); | |
203 | case EZFS_NOCAP: | |
204 | return (dgettext(TEXT_DOMAIN, | |
205 | "disk capacity information could not be retrieved")); | |
206 | case EZFS_LABELFAILED: | |
207 | return (dgettext(TEXT_DOMAIN, "write of label failed")); | |
208 | case EZFS_BADWHO: | |
209 | return (dgettext(TEXT_DOMAIN, "invalid user/group")); | |
210 | case EZFS_BADPERM: | |
211 | return (dgettext(TEXT_DOMAIN, "invalid permission")); | |
212 | case EZFS_BADPERMSET: | |
213 | return (dgettext(TEXT_DOMAIN, "invalid permission set name")); | |
214 | case EZFS_NODELEGATION: | |
215 | return (dgettext(TEXT_DOMAIN, "delegated administration is " | |
216 | "disabled on pool")); | |
34dc7c2f BB |
217 | case EZFS_BADCACHE: |
218 | return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); | |
219 | case EZFS_ISL2CACHE: | |
220 | return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); | |
221 | case EZFS_VDEVNOTSUP: | |
222 | return (dgettext(TEXT_DOMAIN, "vdev specification is not " | |
223 | "supported")); | |
b128c09f BB |
224 | case EZFS_NOTSUP: |
225 | return (dgettext(TEXT_DOMAIN, "operation not supported " | |
226 | "on this dataset")); | |
227 | case EZFS_ACTIVE_SPARE: | |
228 | return (dgettext(TEXT_DOMAIN, "pool has active shared spare " | |
229 | "device")); | |
9babb374 BB |
230 | case EZFS_UNPLAYED_LOGS: |
231 | return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " | |
232 | "logs")); | |
45d1cae3 BB |
233 | case EZFS_REFTAG_RELE: |
234 | return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); | |
235 | case EZFS_REFTAG_HOLD: | |
236 | return (dgettext(TEXT_DOMAIN, "tag already exists on this " | |
237 | "dataset")); | |
428870ff BB |
238 | case EZFS_TAGTOOLONG: |
239 | return (dgettext(TEXT_DOMAIN, "tag too long")); | |
240 | case EZFS_PIPEFAILED: | |
241 | return (dgettext(TEXT_DOMAIN, "pipe create failed")); | |
242 | case EZFS_THREADCREATEFAILED: | |
243 | return (dgettext(TEXT_DOMAIN, "thread create failed")); | |
244 | case EZFS_POSTSPLIT_ONLINE: | |
245 | return (dgettext(TEXT_DOMAIN, "disk was split from this pool " | |
246 | "into a new one")); | |
247 | case EZFS_SCRUBBING: | |
248 | return (dgettext(TEXT_DOMAIN, "currently scrubbing; " | |
249 | "use 'zpool scrub -s' to cancel current scrub")); | |
250 | case EZFS_NO_SCRUB: | |
251 | return (dgettext(TEXT_DOMAIN, "there is no active scrub")); | |
572e2857 BB |
252 | case EZFS_DIFF: |
253 | return (dgettext(TEXT_DOMAIN, "unable to generate diffs")); | |
254 | case EZFS_DIFFDATA: | |
255 | return (dgettext(TEXT_DOMAIN, "invalid diff data")); | |
256 | case EZFS_POOLREADONLY: | |
257 | return (dgettext(TEXT_DOMAIN, "pool is read-only")); | |
34dc7c2f BB |
258 | case EZFS_UNKNOWN: |
259 | return (dgettext(TEXT_DOMAIN, "unknown error")); | |
260 | default: | |
261 | assert(hdl->libzfs_error == 0); | |
262 | return (dgettext(TEXT_DOMAIN, "no error")); | |
263 | } | |
264 | } | |
265 | ||
266 | /*PRINTFLIKE2*/ | |
267 | void | |
268 | zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) | |
269 | { | |
270 | va_list ap; | |
271 | ||
272 | va_start(ap, fmt); | |
273 | ||
274 | (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), | |
275 | fmt, ap); | |
276 | hdl->libzfs_desc_active = 1; | |
277 | ||
278 | va_end(ap); | |
279 | } | |
280 | ||
281 | static void | |
282 | zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) | |
283 | { | |
284 | (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), | |
285 | fmt, ap); | |
286 | hdl->libzfs_error = error; | |
287 | ||
288 | if (hdl->libzfs_desc_active) | |
289 | hdl->libzfs_desc_active = 0; | |
290 | else | |
291 | hdl->libzfs_desc[0] = '\0'; | |
292 | ||
293 | if (hdl->libzfs_printerr) { | |
294 | if (error == EZFS_UNKNOWN) { | |
295 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " | |
296 | "error: %s\n"), libzfs_error_description(hdl)); | |
297 | abort(); | |
298 | } | |
299 | ||
300 | (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, | |
301 | libzfs_error_description(hdl)); | |
302 | if (error == EZFS_NOMEM) | |
303 | exit(1); | |
304 | } | |
305 | } | |
306 | ||
307 | int | |
308 | zfs_error(libzfs_handle_t *hdl, int error, const char *msg) | |
309 | { | |
310 | return (zfs_error_fmt(hdl, error, "%s", msg)); | |
311 | } | |
312 | ||
313 | /*PRINTFLIKE3*/ | |
314 | int | |
315 | zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) | |
316 | { | |
317 | va_list ap; | |
318 | ||
319 | va_start(ap, fmt); | |
320 | ||
321 | zfs_verror(hdl, error, fmt, ap); | |
322 | ||
323 | va_end(ap); | |
324 | ||
325 | return (-1); | |
326 | } | |
327 | ||
328 | static int | |
329 | zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, | |
330 | va_list ap) | |
331 | { | |
332 | switch (error) { | |
333 | case EPERM: | |
334 | case EACCES: | |
335 | zfs_verror(hdl, EZFS_PERM, fmt, ap); | |
336 | return (-1); | |
337 | ||
338 | case ECANCELED: | |
339 | zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); | |
340 | return (-1); | |
341 | ||
342 | case EIO: | |
343 | zfs_verror(hdl, EZFS_IO, fmt, ap); | |
344 | return (-1); | |
345 | ||
428870ff BB |
346 | case EFAULT: |
347 | zfs_verror(hdl, EZFS_FAULT, fmt, ap); | |
348 | return (-1); | |
349 | ||
34dc7c2f BB |
350 | case EINTR: |
351 | zfs_verror(hdl, EZFS_INTR, fmt, ap); | |
352 | return (-1); | |
353 | } | |
354 | ||
355 | return (0); | |
356 | } | |
357 | ||
358 | int | |
359 | zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) | |
360 | { | |
361 | return (zfs_standard_error_fmt(hdl, error, "%s", msg)); | |
362 | } | |
363 | ||
364 | /*PRINTFLIKE3*/ | |
365 | int | |
366 | zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) | |
367 | { | |
368 | va_list ap; | |
369 | ||
370 | va_start(ap, fmt); | |
371 | ||
372 | if (zfs_common_error(hdl, error, fmt, ap) != 0) { | |
373 | va_end(ap); | |
374 | return (-1); | |
375 | } | |
376 | ||
377 | switch (error) { | |
378 | case ENXIO: | |
379 | case ENODEV: | |
330d06f9 | 380 | case EPIPE: |
34dc7c2f BB |
381 | zfs_verror(hdl, EZFS_IO, fmt, ap); |
382 | break; | |
383 | ||
384 | case ENOENT: | |
385 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
386 | "dataset does not exist")); | |
387 | zfs_verror(hdl, EZFS_NOENT, fmt, ap); | |
388 | break; | |
389 | ||
390 | case ENOSPC: | |
391 | case EDQUOT: | |
392 | zfs_verror(hdl, EZFS_NOSPC, fmt, ap); | |
393 | return (-1); | |
394 | ||
395 | case EEXIST: | |
396 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
397 | "dataset already exists")); | |
398 | zfs_verror(hdl, EZFS_EXISTS, fmt, ap); | |
399 | break; | |
400 | ||
401 | case EBUSY: | |
402 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
403 | "dataset is busy")); | |
404 | zfs_verror(hdl, EZFS_BUSY, fmt, ap); | |
405 | break; | |
406 | case EROFS: | |
572e2857 | 407 | zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); |
34dc7c2f BB |
408 | break; |
409 | case ENAMETOOLONG: | |
410 | zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); | |
411 | break; | |
412 | case ENOTSUP: | |
413 | zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); | |
414 | break; | |
9babb374 BB |
415 | case EAGAIN: |
416 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
417 | "pool I/O is currently suspended")); | |
418 | zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); | |
419 | break; | |
34dc7c2f | 420 | default: |
428870ff | 421 | zfs_error_aux(hdl, strerror(error)); |
34dc7c2f BB |
422 | zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); |
423 | break; | |
424 | } | |
425 | ||
426 | va_end(ap); | |
427 | return (-1); | |
428 | } | |
429 | ||
430 | int | |
431 | zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) | |
432 | { | |
433 | return (zpool_standard_error_fmt(hdl, error, "%s", msg)); | |
434 | } | |
435 | ||
436 | /*PRINTFLIKE3*/ | |
437 | int | |
438 | zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) | |
439 | { | |
440 | va_list ap; | |
441 | ||
442 | va_start(ap, fmt); | |
443 | ||
444 | if (zfs_common_error(hdl, error, fmt, ap) != 0) { | |
445 | va_end(ap); | |
446 | return (-1); | |
447 | } | |
448 | ||
449 | switch (error) { | |
450 | case ENODEV: | |
451 | zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); | |
452 | break; | |
453 | ||
454 | case ENOENT: | |
455 | zfs_error_aux(hdl, | |
456 | dgettext(TEXT_DOMAIN, "no such pool or dataset")); | |
457 | zfs_verror(hdl, EZFS_NOENT, fmt, ap); | |
458 | break; | |
459 | ||
460 | case EEXIST: | |
461 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
462 | "pool already exists")); | |
463 | zfs_verror(hdl, EZFS_EXISTS, fmt, ap); | |
464 | break; | |
465 | ||
466 | case EBUSY: | |
467 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); | |
b128c09f | 468 | zfs_verror(hdl, EZFS_BUSY, fmt, ap); |
34dc7c2f BB |
469 | break; |
470 | ||
471 | case ENXIO: | |
472 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
473 | "one or more devices is currently unavailable")); | |
474 | zfs_verror(hdl, EZFS_BADDEV, fmt, ap); | |
475 | break; | |
476 | ||
477 | case ENAMETOOLONG: | |
478 | zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); | |
479 | break; | |
480 | ||
481 | case ENOTSUP: | |
482 | zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); | |
483 | break; | |
484 | ||
485 | case EINVAL: | |
486 | zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); | |
487 | break; | |
488 | ||
489 | case ENOSPC: | |
490 | case EDQUOT: | |
491 | zfs_verror(hdl, EZFS_NOSPC, fmt, ap); | |
492 | return (-1); | |
572e2857 | 493 | |
9babb374 BB |
494 | case EAGAIN: |
495 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
496 | "pool I/O is currently suspended")); | |
497 | zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); | |
498 | break; | |
34dc7c2f | 499 | |
572e2857 BB |
500 | case EROFS: |
501 | zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); | |
502 | break; | |
385f9691 D |
503 | case EDOM: |
504 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
505 | "block size out of range or does not match")); | |
506 | zfs_verror(hdl, EZFS_BADPROP, fmt, ap); | |
507 | break; | |
572e2857 | 508 | |
34dc7c2f BB |
509 | default: |
510 | zfs_error_aux(hdl, strerror(error)); | |
511 | zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); | |
512 | } | |
513 | ||
514 | va_end(ap); | |
515 | return (-1); | |
516 | } | |
517 | ||
518 | /* | |
519 | * Display an out of memory error message and abort the current program. | |
520 | */ | |
521 | int | |
522 | no_memory(libzfs_handle_t *hdl) | |
523 | { | |
524 | return (zfs_error(hdl, EZFS_NOMEM, "internal error")); | |
525 | } | |
526 | ||
527 | /* | |
528 | * A safe form of malloc() which will die if the allocation fails. | |
529 | */ | |
530 | void * | |
531 | zfs_alloc(libzfs_handle_t *hdl, size_t size) | |
532 | { | |
533 | void *data; | |
534 | ||
535 | if ((data = calloc(1, size)) == NULL) | |
536 | (void) no_memory(hdl); | |
537 | ||
538 | return (data); | |
539 | } | |
540 | ||
572e2857 BB |
541 | /* |
542 | * A safe form of asprintf() which will die if the allocation fails. | |
543 | */ | |
544 | /*PRINTFLIKE2*/ | |
545 | char * | |
546 | zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...) | |
547 | { | |
548 | va_list ap; | |
549 | char *ret; | |
550 | int err; | |
551 | ||
552 | va_start(ap, fmt); | |
553 | ||
554 | err = vasprintf(&ret, fmt, ap); | |
555 | ||
556 | va_end(ap); | |
557 | ||
558 | if (err < 0) | |
559 | (void) no_memory(hdl); | |
560 | ||
561 | return (ret); | |
562 | } | |
563 | ||
34dc7c2f BB |
564 | /* |
565 | * A safe form of realloc(), which also zeroes newly allocated space. | |
566 | */ | |
567 | void * | |
568 | zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) | |
569 | { | |
570 | void *ret; | |
571 | ||
572 | if ((ret = realloc(ptr, newsize)) == NULL) { | |
573 | (void) no_memory(hdl); | |
34dc7c2f BB |
574 | return (NULL); |
575 | } | |
576 | ||
577 | bzero((char *)ret + oldsize, (newsize - oldsize)); | |
578 | return (ret); | |
579 | } | |
580 | ||
581 | /* | |
582 | * A safe form of strdup() which will die if the allocation fails. | |
583 | */ | |
584 | char * | |
585 | zfs_strdup(libzfs_handle_t *hdl, const char *str) | |
586 | { | |
587 | char *ret; | |
588 | ||
589 | if ((ret = strdup(str)) == NULL) | |
590 | (void) no_memory(hdl); | |
591 | ||
592 | return (ret); | |
593 | } | |
594 | ||
595 | /* | |
596 | * Convert a number to an appropriately human-readable output. | |
597 | */ | |
598 | void | |
599 | zfs_nicenum(uint64_t num, char *buf, size_t buflen) | |
600 | { | |
601 | uint64_t n = num; | |
602 | int index = 0; | |
603 | char u; | |
604 | ||
8bdcfb53 | 605 | while (n >= 1024 && index < 6) { |
34dc7c2f BB |
606 | n /= 1024; |
607 | index++; | |
608 | } | |
609 | ||
610 | u = " KMGTPE"[index]; | |
611 | ||
612 | if (index == 0) { | |
b8864a23 | 613 | (void) snprintf(buf, buflen, "%llu", (u_longlong_t) n); |
34dc7c2f BB |
614 | } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { |
615 | /* | |
616 | * If this is an even multiple of the base, always display | |
617 | * without any decimal precision. | |
618 | */ | |
b8864a23 | 619 | (void) snprintf(buf, buflen, "%llu%c", (u_longlong_t) n, u); |
34dc7c2f BB |
620 | } else { |
621 | /* | |
622 | * We want to choose a precision that reflects the best choice | |
623 | * for fitting in 5 characters. This can get rather tricky when | |
624 | * we have numbers that are very close to an order of magnitude. | |
625 | * For example, when displaying 10239 (which is really 9.999K), | |
626 | * we want only a single place of precision for 10.0K. We could | |
627 | * develop some complex heuristics for this, but it's much | |
628 | * easier just to try each combination in turn. | |
629 | */ | |
630 | int i; | |
631 | for (i = 2; i >= 0; i--) { | |
632 | if (snprintf(buf, buflen, "%.*f%c", i, | |
633 | (double)num / (1ULL << 10 * index), u) <= 5) | |
634 | break; | |
635 | } | |
636 | } | |
637 | } | |
638 | ||
639 | void | |
640 | libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) | |
641 | { | |
642 | hdl->libzfs_printerr = printerr; | |
643 | } | |
644 | ||
4b1abce9 NB |
645 | static int |
646 | libzfs_module_loaded(const char *module) | |
647 | { | |
f09398ce ED |
648 | const char path_prefix[] = "/sys/module/"; |
649 | char path[256]; | |
650 | ||
d1d7e268 MK |
651 | memcpy(path, path_prefix, sizeof (path_prefix) - 1); |
652 | strcpy(path + sizeof (path_prefix) - 1, module); | |
4b1abce9 | 653 | |
f09398ce | 654 | return (access(path, F_OK) == 0); |
4b1abce9 NB |
655 | } |
656 | ||
feb46b92 | 657 | int |
9ac97c2a | 658 | libzfs_run_process(const char *path, char *argv[], int flags) |
4b1abce9 NB |
659 | { |
660 | pid_t pid; | |
65037d9b | 661 | int error, devnull_fd; |
4b1abce9 NB |
662 | |
663 | pid = vfork(); | |
664 | if (pid == 0) { | |
3132cb39 GB |
665 | devnull_fd = open("/dev/null", O_WRONLY); |
666 | ||
667 | if (devnull_fd < 0) | |
668 | _exit(-1); | |
669 | ||
9ac97c2a | 670 | if (!(flags & STDOUT_VERBOSE)) |
3132cb39 | 671 | (void) dup2(devnull_fd, STDOUT_FILENO); |
9ac97c2a BB |
672 | |
673 | if (!(flags & STDERR_VERBOSE)) | |
3132cb39 GB |
674 | (void) dup2(devnull_fd, STDERR_FILENO); |
675 | ||
676 | close(devnull_fd); | |
9ac97c2a | 677 | |
4b1abce9 NB |
678 | (void) execvp(path, argv); |
679 | _exit(-1); | |
680 | } else if (pid > 0) { | |
681 | int status; | |
682 | ||
65037d9b | 683 | while ((error = waitpid(pid, &status, 0)) == -1 && |
4b1abce9 | 684 | errno == EINTR); |
65037d9b | 685 | if (error < 0 || !WIFEXITED(status)) |
d1d7e268 | 686 | return (-1); |
4b1abce9 | 687 | |
d1d7e268 | 688 | return (WEXITSTATUS(status)); |
4b1abce9 NB |
689 | } |
690 | ||
d1d7e268 | 691 | return (-1); |
4b1abce9 NB |
692 | } |
693 | ||
87abfcba BB |
694 | /* |
695 | * Verify the required ZFS_DEV device is available and optionally attempt | |
696 | * to load the ZFS modules. Under normal circumstances the modules | |
697 | * should already have been loaded by some external mechanism. | |
698 | * | |
699 | * Environment variables: | |
700 | * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules. | |
701 | * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV | |
702 | */ | |
65037d9b | 703 | static int |
4b1abce9 NB |
704 | libzfs_load_module(const char *module) |
705 | { | |
706 | char *argv[4] = {"/sbin/modprobe", "-q", (char *)module, (char *)0}; | |
87abfcba BB |
707 | char *load_str, *timeout_str; |
708 | long timeout = 10; /* seconds */ | |
709 | long busy_timeout = 10; /* milliseconds */ | |
710 | int load = 0, fd; | |
711 | hrtime_t start; | |
712 | ||
713 | /* Optionally request module loading */ | |
714 | if (!libzfs_module_loaded(module)) { | |
715 | load_str = getenv("ZFS_MODULE_LOADING"); | |
716 | if (load_str) { | |
717 | if (!strncasecmp(load_str, "YES", strlen("YES")) || | |
718 | !strncasecmp(load_str, "ON", strlen("ON"))) | |
719 | load = 1; | |
720 | else | |
721 | load = 0; | |
722 | } | |
4b1abce9 | 723 | |
87abfcba BB |
724 | if (load && libzfs_run_process("/sbin/modprobe", argv, 0)) |
725 | return (ENOEXEC); | |
726 | } | |
727 | ||
728 | /* Module loading is synchronous it must be available */ | |
729 | if (!libzfs_module_loaded(module)) | |
730 | return (ENXIO); | |
731 | ||
732 | /* | |
733 | * Device creation by udev is asynchronous and waiting may be | |
734 | * required. Busy wait for 10ms and then fall back to polling every | |
735 | * 10ms for the allowed timeout (default 10s, max 10m). This is | |
736 | * done to optimize for the common case where the device is | |
737 | * immediately available and to avoid penalizing the possible | |
738 | * case where udev is slow or unable to create the device. | |
739 | */ | |
740 | timeout_str = getenv("ZFS_MODULE_TIMEOUT"); | |
741 | if (timeout_str) { | |
742 | timeout = strtol(timeout_str, NULL, 0); | |
743 | timeout = MAX(MIN(timeout, (10 * 60)), 0); /* 0 <= N <= 600 */ | |
744 | } | |
745 | ||
746 | start = gethrtime(); | |
747 | do { | |
748 | fd = open(ZFS_DEV, O_RDWR); | |
749 | if (fd >= 0) { | |
750 | (void) close(fd); | |
751 | return (0); | |
752 | } else if (errno != ENOENT) { | |
753 | return (errno); | |
754 | } else if (NSEC2MSEC(gethrtime() - start) < busy_timeout) { | |
755 | sched_yield(); | |
756 | } else { | |
757 | usleep(10 * MILLISEC); | |
758 | } | |
759 | } while (NSEC2MSEC(gethrtime() - start) < (timeout * MILLISEC)); | |
feb46b92 | 760 | |
87abfcba | 761 | return (ENOENT); |
4b1abce9 NB |
762 | } |
763 | ||
34dc7c2f BB |
764 | libzfs_handle_t * |
765 | libzfs_init(void) | |
766 | { | |
767 | libzfs_handle_t *hdl; | |
87abfcba | 768 | int error; |
34dc7c2f | 769 | |
87abfcba BB |
770 | error = libzfs_load_module(ZFS_DRIVER); |
771 | if (error) { | |
65037d9b | 772 | errno = error; |
4b1abce9 NB |
773 | return (NULL); |
774 | } | |
775 | ||
572e2857 | 776 | if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) { |
34dc7c2f BB |
777 | return (NULL); |
778 | } | |
779 | ||
780 | if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { | |
781 | free(hdl); | |
782 | return (NULL); | |
783 | } | |
784 | ||
2eadf037 BB |
785 | #ifdef HAVE_SETMNTENT |
786 | if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) { | |
787 | #else | |
34dc7c2f | 788 | if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { |
2eadf037 | 789 | #endif |
34dc7c2f BB |
790 | (void) close(hdl->libzfs_fd); |
791 | free(hdl); | |
792 | return (NULL); | |
793 | } | |
794 | ||
795 | hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); | |
796 | ||
6f1ffb06 MA |
797 | if (libzfs_core_init() != 0) { |
798 | (void) close(hdl->libzfs_fd); | |
799 | (void) fclose(hdl->libzfs_mnttab); | |
800 | (void) fclose(hdl->libzfs_sharetab); | |
801 | free(hdl); | |
802 | return (NULL); | |
803 | } | |
804 | ||
34dc7c2f BB |
805 | zfs_prop_init(); |
806 | zpool_prop_init(); | |
9ae529ec | 807 | zpool_feature_init(); |
9babb374 | 808 | libzfs_mnttab_init(hdl); |
34dc7c2f BB |
809 | |
810 | return (hdl); | |
811 | } | |
812 | ||
813 | void | |
814 | libzfs_fini(libzfs_handle_t *hdl) | |
815 | { | |
816 | (void) close(hdl->libzfs_fd); | |
817 | if (hdl->libzfs_mnttab) | |
2eadf037 BB |
818 | #ifdef HAVE_SETMNTENT |
819 | (void) endmntent(hdl->libzfs_mnttab); | |
820 | #else | |
34dc7c2f | 821 | (void) fclose(hdl->libzfs_mnttab); |
2eadf037 | 822 | #endif |
34dc7c2f BB |
823 | if (hdl->libzfs_sharetab) |
824 | (void) fclose(hdl->libzfs_sharetab); | |
825 | zfs_uninit_libshare(hdl); | |
b128c09f | 826 | zpool_free_handles(hdl); |
428870ff | 827 | libzfs_fru_clear(hdl, B_TRUE); |
34dc7c2f | 828 | namespace_clear(hdl); |
9babb374 | 829 | libzfs_mnttab_fini(hdl); |
6f1ffb06 | 830 | libzfs_core_fini(); |
34dc7c2f BB |
831 | free(hdl); |
832 | } | |
833 | ||
834 | libzfs_handle_t * | |
835 | zpool_get_handle(zpool_handle_t *zhp) | |
836 | { | |
837 | return (zhp->zpool_hdl); | |
838 | } | |
839 | ||
840 | libzfs_handle_t * | |
841 | zfs_get_handle(zfs_handle_t *zhp) | |
842 | { | |
843 | return (zhp->zfs_hdl); | |
844 | } | |
845 | ||
b128c09f BB |
846 | zpool_handle_t * |
847 | zfs_get_pool_handle(const zfs_handle_t *zhp) | |
848 | { | |
849 | return (zhp->zpool_hdl); | |
850 | } | |
851 | ||
34dc7c2f BB |
852 | /* |
853 | * Given a name, determine whether or not it's a valid path | |
854 | * (starts with '/' or "./"). If so, walk the mnttab trying | |
855 | * to match the device number. If not, treat the path as an | |
856 | * fs/vol/snap name. | |
857 | */ | |
858 | zfs_handle_t * | |
859 | zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) | |
860 | { | |
861 | struct stat64 statbuf; | |
862 | struct extmnttab entry; | |
863 | int ret; | |
864 | ||
865 | if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { | |
866 | /* | |
867 | * It's not a valid path, assume it's a name of type 'argtype'. | |
868 | */ | |
869 | return (zfs_open(hdl, path, argtype)); | |
870 | } | |
871 | ||
872 | if (stat64(path, &statbuf) != 0) { | |
873 | (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); | |
874 | return (NULL); | |
875 | } | |
876 | ||
cbca6076 JL |
877 | /* Reopen MNTTAB to prevent reading stale data from open file */ |
878 | if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL) | |
879 | return (NULL); | |
880 | ||
34dc7c2f BB |
881 | while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { |
882 | if (makedevice(entry.mnt_major, entry.mnt_minor) == | |
883 | statbuf.st_dev) { | |
884 | break; | |
885 | } | |
886 | } | |
887 | if (ret != 0) { | |
888 | return (NULL); | |
889 | } | |
890 | ||
891 | if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { | |
892 | (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), | |
893 | path); | |
894 | return (NULL); | |
895 | } | |
896 | ||
897 | return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); | |
898 | } | |
899 | ||
79e7242a | 900 | /* |
eac47204 BB |
901 | * Append partition suffix to an otherwise fully qualified device path. |
902 | * This is used to generate the name the full path as its stored in | |
903 | * ZPOOL_CONFIG_PATH for whole disk devices. On success the new length | |
904 | * of 'path' will be returned on error a negative value is returned. | |
79e7242a NB |
905 | */ |
906 | int | |
eac47204 BB |
907 | zfs_append_partition(char *path, size_t max_len) |
908 | { | |
909 | int len = strlen(path); | |
910 | ||
6bb24f4d BB |
911 | if ((strncmp(path, UDISK_ROOT, strlen(UDISK_ROOT)) == 0) || |
912 | (strncmp(path, ZVOL_ROOT, strlen(ZVOL_ROOT)) == 0)) { | |
eac47204 BB |
913 | if (len + 6 >= max_len) |
914 | return (-1); | |
915 | ||
916 | (void) strcat(path, "-part1"); | |
917 | len += 6; | |
918 | } else { | |
919 | if (len + 2 >= max_len) | |
920 | return (-1); | |
921 | ||
922 | if (isdigit(path[len-1])) { | |
923 | (void) strcat(path, "p1"); | |
924 | len += 2; | |
925 | } else { | |
926 | (void) strcat(path, "1"); | |
927 | len += 1; | |
928 | } | |
79e7242a | 929 | } |
b04c9fc0 | 930 | |
eac47204 | 931 | return (len); |
79e7242a NB |
932 | } |
933 | ||
934 | /* | |
eac47204 BB |
935 | * Given a shorthand device name check if a file by that name exists in any |
936 | * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If | |
937 | * one is found, store its fully qualified path in the 'path' buffer passed | |
938 | * by the caller and return 0, otherwise return an error. | |
79e7242a | 939 | */ |
eac47204 BB |
940 | int |
941 | zfs_resolve_shortname(const char *name, char *path, size_t len) | |
79e7242a | 942 | { |
eac47204 BB |
943 | int i, error = -1; |
944 | char *dir, *env, *envdup; | |
945 | ||
946 | env = getenv("ZPOOL_IMPORT_PATH"); | |
947 | errno = ENOENT; | |
948 | ||
949 | if (env) { | |
950 | envdup = strdup(env); | |
951 | dir = strtok(envdup, ":"); | |
952 | while (dir && error) { | |
953 | (void) snprintf(path, len, "%s/%s", dir, name); | |
954 | error = access(path, F_OK); | |
955 | dir = strtok(NULL, ":"); | |
956 | } | |
957 | free(envdup); | |
958 | } else { | |
959 | for (i = 0; i < DEFAULT_IMPORT_PATH_SIZE && error < 0; i++) { | |
960 | (void) snprintf(path, len, "%s/%s", | |
961 | zpool_default_import_path[i], name); | |
962 | error = access(path, F_OK); | |
963 | } | |
964 | } | |
965 | ||
966 | return (error ? ENOENT : 0); | |
967 | } | |
968 | ||
969 | /* | |
970 | * Given a shorthand device name look for a match against 'cmp_name'. This | |
971 | * is done by checking all prefix expansions using either the default | |
972 | * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment | |
973 | * variable. Proper partition suffixes will be appended if this is a | |
974 | * whole disk. When a match is found 0 is returned otherwise ENOENT. | |
975 | */ | |
976 | static int | |
977 | zfs_strcmp_shortname(char *name, char *cmp_name, int wholedisk) | |
978 | { | |
979 | int path_len, cmp_len, i = 0, error = ENOENT; | |
980 | char *dir, *env, *envdup = NULL; | |
981 | char path_name[MAXPATHLEN]; | |
982 | ||
983 | cmp_len = strlen(cmp_name); | |
984 | env = getenv("ZPOOL_IMPORT_PATH"); | |
985 | ||
986 | if (env) { | |
987 | envdup = strdup(env); | |
988 | dir = strtok(envdup, ":"); | |
989 | } else { | |
990 | dir = zpool_default_import_path[i]; | |
991 | } | |
992 | ||
993 | while (dir) { | |
994 | /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */ | |
995 | while (dir[strlen(dir)-1] == '/') | |
996 | dir[strlen(dir)-1] = '\0'; | |
997 | ||
998 | path_len = snprintf(path_name, MAXPATHLEN, "%s/%s", dir, name); | |
999 | if (wholedisk) | |
1000 | path_len = zfs_append_partition(path_name, MAXPATHLEN); | |
1001 | ||
d1d7e268 | 1002 | if ((path_len == cmp_len) && strcmp(path_name, cmp_name) == 0) { |
eac47204 BB |
1003 | error = 0; |
1004 | break; | |
1005 | } | |
1006 | ||
1007 | if (env) { | |
1008 | dir = strtok(NULL, ":"); | |
1009 | } else if (++i < DEFAULT_IMPORT_PATH_SIZE) { | |
1010 | dir = zpool_default_import_path[i]; | |
1011 | } else { | |
1012 | dir = NULL; | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | if (env) | |
1017 | free(envdup); | |
1018 | ||
1019 | return (error); | |
1020 | } | |
1021 | ||
1022 | /* | |
1023 | * Given either a shorthand or fully qualified path name look for a match | |
1024 | * against 'cmp'. The passed name will be expanded as needed for comparison | |
1025 | * purposes and redundant slashes stripped to ensure an accurate match. | |
1026 | */ | |
1027 | int | |
1028 | zfs_strcmp_pathname(char *name, char *cmp, int wholedisk) | |
1029 | { | |
1030 | int path_len, cmp_len; | |
1031 | char path_name[MAXPATHLEN]; | |
1032 | char cmp_name[MAXPATHLEN]; | |
eea93094 | 1033 | char *dir, *dup; |
eac47204 BB |
1034 | |
1035 | /* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */ | |
1036 | memset(cmp_name, 0, MAXPATHLEN); | |
eea93094 BB |
1037 | dup = strdup(cmp); |
1038 | dir = strtok(dup, "/"); | |
eac47204 BB |
1039 | while (dir) { |
1040 | strcat(cmp_name, "/"); | |
1041 | strcat(cmp_name, dir); | |
1042 | dir = strtok(NULL, "/"); | |
1043 | } | |
eea93094 | 1044 | free(dup); |
eac47204 BB |
1045 | |
1046 | if (name[0] != '/') | |
d1d7e268 | 1047 | return (zfs_strcmp_shortname(name, cmp_name, wholedisk)); |
eac47204 | 1048 | |
928ee9fe | 1049 | (void) strlcpy(path_name, name, MAXPATHLEN); |
eac47204 BB |
1050 | path_len = strlen(path_name); |
1051 | cmp_len = strlen(cmp_name); | |
1052 | ||
1053 | if (wholedisk) { | |
1054 | path_len = zfs_append_partition(path_name, MAXPATHLEN); | |
1055 | if (path_len == -1) | |
1056 | return (ENOMEM); | |
1057 | } | |
1058 | ||
1059 | if ((path_len != cmp_len) || strcmp(path_name, cmp_name)) | |
1060 | return (ENOENT); | |
1061 | ||
1062 | return (0); | |
79e7242a NB |
1063 | } |
1064 | ||
34dc7c2f BB |
1065 | /* |
1066 | * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from | |
1067 | * an ioctl(). | |
1068 | */ | |
1069 | int | |
1070 | zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) | |
1071 | { | |
1072 | if (len == 0) | |
572e2857 | 1073 | len = 16 * 1024; |
34dc7c2f | 1074 | zc->zc_nvlist_dst_size = len; |
23de906c CW |
1075 | zc->zc_nvlist_dst = |
1076 | (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); | |
1077 | if (zc->zc_nvlist_dst == 0) | |
34dc7c2f BB |
1078 | return (-1); |
1079 | ||
1080 | return (0); | |
1081 | } | |
1082 | ||
1083 | /* | |
1084 | * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will | |
1085 | * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was | |
1086 | * filled in by the kernel to indicate the actual required size. | |
1087 | */ | |
1088 | int | |
1089 | zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) | |
1090 | { | |
1091 | free((void *)(uintptr_t)zc->zc_nvlist_dst); | |
23de906c CW |
1092 | zc->zc_nvlist_dst = |
1093 | (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); | |
1094 | if (zc->zc_nvlist_dst == 0) | |
34dc7c2f BB |
1095 | return (-1); |
1096 | ||
1097 | return (0); | |
1098 | } | |
1099 | ||
1100 | /* | |
1101 | * Called to free the src and dst nvlists stored in the command structure. | |
1102 | */ | |
1103 | void | |
1104 | zcmd_free_nvlists(zfs_cmd_t *zc) | |
1105 | { | |
1106 | free((void *)(uintptr_t)zc->zc_nvlist_conf); | |
1107 | free((void *)(uintptr_t)zc->zc_nvlist_src); | |
1108 | free((void *)(uintptr_t)zc->zc_nvlist_dst); | |
23de906c CW |
1109 | zc->zc_nvlist_conf = 0; |
1110 | zc->zc_nvlist_src = 0; | |
1111 | zc->zc_nvlist_dst = 0; | |
34dc7c2f BB |
1112 | } |
1113 | ||
1114 | static int | |
1115 | zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, | |
1116 | nvlist_t *nvl) | |
1117 | { | |
1118 | char *packed; | |
1119 | size_t len; | |
1120 | ||
1121 | verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); | |
1122 | ||
1123 | if ((packed = zfs_alloc(hdl, len)) == NULL) | |
1124 | return (-1); | |
1125 | ||
1126 | verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); | |
1127 | ||
1128 | *outnv = (uint64_t)(uintptr_t)packed; | |
1129 | *outlen = len; | |
1130 | ||
1131 | return (0); | |
1132 | } | |
1133 | ||
1134 | int | |
1135 | zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) | |
1136 | { | |
1137 | return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, | |
1138 | &zc->zc_nvlist_conf_size, nvl)); | |
1139 | } | |
1140 | ||
1141 | int | |
1142 | zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) | |
1143 | { | |
1144 | return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, | |
1145 | &zc->zc_nvlist_src_size, nvl)); | |
1146 | } | |
1147 | ||
1148 | /* | |
1149 | * Unpacks an nvlist from the ZFS ioctl command structure. | |
1150 | */ | |
1151 | int | |
1152 | zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) | |
1153 | { | |
1154 | if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, | |
1155 | zc->zc_nvlist_dst_size, nvlp, 0) != 0) | |
1156 | return (no_memory(hdl)); | |
1157 | ||
1158 | return (0); | |
1159 | } | |
1160 | ||
1161 | int | |
1162 | zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) | |
1163 | { | |
6f1ffb06 | 1164 | return (ioctl(hdl->libzfs_fd, request, zc)); |
34dc7c2f BB |
1165 | } |
1166 | ||
1167 | /* | |
1168 | * ================================================================ | |
1169 | * API shared by zfs and zpool property management | |
1170 | * ================================================================ | |
1171 | */ | |
1172 | ||
1173 | static void | |
1174 | zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) | |
1175 | { | |
1176 | zprop_list_t *pl = cbp->cb_proplist; | |
1177 | int i; | |
1178 | char *title; | |
1179 | size_t len; | |
1180 | ||
1181 | cbp->cb_first = B_FALSE; | |
1182 | if (cbp->cb_scripted) | |
1183 | return; | |
1184 | ||
1185 | /* | |
1186 | * Start with the length of the column headers. | |
1187 | */ | |
1188 | cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); | |
1189 | cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, | |
1190 | "PROPERTY")); | |
1191 | cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, | |
1192 | "VALUE")); | |
428870ff BB |
1193 | cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN, |
1194 | "RECEIVED")); | |
34dc7c2f BB |
1195 | cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, |
1196 | "SOURCE")); | |
1197 | ||
fb5f0bc8 BB |
1198 | /* first property is always NAME */ |
1199 | assert(cbp->cb_proplist->pl_prop == | |
1200 | ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); | |
1201 | ||
34dc7c2f BB |
1202 | /* |
1203 | * Go through and calculate the widths for each column. For the | |
1204 | * 'source' column, we kludge it up by taking the worst-case scenario of | |
1205 | * inheriting from the longest name. This is acceptable because in the | |
1206 | * majority of cases 'SOURCE' is the last column displayed, and we don't | |
1207 | * use the width anyway. Note that the 'VALUE' column can be oversized, | |
428870ff | 1208 | * if the name of the property is much longer than any values we find. |
34dc7c2f BB |
1209 | */ |
1210 | for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { | |
1211 | /* | |
1212 | * 'PROPERTY' column | |
1213 | */ | |
1214 | if (pl->pl_prop != ZPROP_INVAL) { | |
1215 | const char *propname = (type == ZFS_TYPE_POOL) ? | |
1216 | zpool_prop_to_name(pl->pl_prop) : | |
1217 | zfs_prop_to_name(pl->pl_prop); | |
1218 | ||
1219 | len = strlen(propname); | |
1220 | if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) | |
1221 | cbp->cb_colwidths[GET_COL_PROPERTY] = len; | |
1222 | } else { | |
1223 | len = strlen(pl->pl_user_prop); | |
1224 | if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) | |
1225 | cbp->cb_colwidths[GET_COL_PROPERTY] = len; | |
1226 | } | |
1227 | ||
1228 | /* | |
fb5f0bc8 BB |
1229 | * 'VALUE' column. The first property is always the 'name' |
1230 | * property that was tacked on either by /sbin/zfs's | |
1231 | * zfs_do_get() or when calling zprop_expand_list(), so we | |
1232 | * ignore its width. If the user specified the name property | |
1233 | * to display, then it will be later in the list in any case. | |
34dc7c2f | 1234 | */ |
fb5f0bc8 | 1235 | if (pl != cbp->cb_proplist && |
34dc7c2f BB |
1236 | pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) |
1237 | cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; | |
1238 | ||
428870ff BB |
1239 | /* 'RECEIVED' column. */ |
1240 | if (pl != cbp->cb_proplist && | |
1241 | pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) | |
1242 | cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; | |
1243 | ||
34dc7c2f BB |
1244 | /* |
1245 | * 'NAME' and 'SOURCE' columns | |
1246 | */ | |
1247 | if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : | |
1248 | ZFS_PROP_NAME) && | |
1249 | pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { | |
1250 | cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; | |
1251 | cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + | |
1252 | strlen(dgettext(TEXT_DOMAIN, "inherited from")); | |
1253 | } | |
1254 | } | |
1255 | ||
1256 | /* | |
1257 | * Now go through and print the headers. | |
1258 | */ | |
428870ff | 1259 | for (i = 0; i < ZFS_GET_NCOLS; i++) { |
34dc7c2f BB |
1260 | switch (cbp->cb_columns[i]) { |
1261 | case GET_COL_NAME: | |
1262 | title = dgettext(TEXT_DOMAIN, "NAME"); | |
1263 | break; | |
1264 | case GET_COL_PROPERTY: | |
1265 | title = dgettext(TEXT_DOMAIN, "PROPERTY"); | |
1266 | break; | |
1267 | case GET_COL_VALUE: | |
1268 | title = dgettext(TEXT_DOMAIN, "VALUE"); | |
1269 | break; | |
428870ff BB |
1270 | case GET_COL_RECVD: |
1271 | title = dgettext(TEXT_DOMAIN, "RECEIVED"); | |
1272 | break; | |
34dc7c2f BB |
1273 | case GET_COL_SOURCE: |
1274 | title = dgettext(TEXT_DOMAIN, "SOURCE"); | |
1275 | break; | |
1276 | default: | |
1277 | title = NULL; | |
1278 | } | |
1279 | ||
1280 | if (title != NULL) { | |
428870ff BB |
1281 | if (i == (ZFS_GET_NCOLS - 1) || |
1282 | cbp->cb_columns[i + 1] == GET_COL_NONE) | |
34dc7c2f BB |
1283 | (void) printf("%s", title); |
1284 | else | |
1285 | (void) printf("%-*s ", | |
1286 | cbp->cb_colwidths[cbp->cb_columns[i]], | |
1287 | title); | |
1288 | } | |
1289 | } | |
1290 | (void) printf("\n"); | |
1291 | } | |
1292 | ||
1293 | /* | |
1294 | * Display a single line of output, according to the settings in the callback | |
1295 | * structure. | |
1296 | */ | |
1297 | void | |
1298 | zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, | |
1299 | const char *propname, const char *value, zprop_source_t sourcetype, | |
428870ff | 1300 | const char *source, const char *recvd_value) |
34dc7c2f BB |
1301 | { |
1302 | int i; | |
d4ed6673 | 1303 | const char *str = NULL; |
34dc7c2f BB |
1304 | char buf[128]; |
1305 | ||
1306 | /* | |
1307 | * Ignore those source types that the user has chosen to ignore. | |
1308 | */ | |
1309 | if ((sourcetype & cbp->cb_sources) == 0) | |
1310 | return; | |
1311 | ||
1312 | if (cbp->cb_first) | |
1313 | zprop_print_headers(cbp, cbp->cb_type); | |
1314 | ||
428870ff | 1315 | for (i = 0; i < ZFS_GET_NCOLS; i++) { |
34dc7c2f BB |
1316 | switch (cbp->cb_columns[i]) { |
1317 | case GET_COL_NAME: | |
1318 | str = name; | |
1319 | break; | |
1320 | ||
1321 | case GET_COL_PROPERTY: | |
1322 | str = propname; | |
1323 | break; | |
1324 | ||
1325 | case GET_COL_VALUE: | |
1326 | str = value; | |
1327 | break; | |
1328 | ||
1329 | case GET_COL_SOURCE: | |
1330 | switch (sourcetype) { | |
1331 | case ZPROP_SRC_NONE: | |
1332 | str = "-"; | |
1333 | break; | |
1334 | ||
1335 | case ZPROP_SRC_DEFAULT: | |
1336 | str = "default"; | |
1337 | break; | |
1338 | ||
1339 | case ZPROP_SRC_LOCAL: | |
1340 | str = "local"; | |
1341 | break; | |
1342 | ||
1343 | case ZPROP_SRC_TEMPORARY: | |
1344 | str = "temporary"; | |
1345 | break; | |
1346 | ||
1347 | case ZPROP_SRC_INHERITED: | |
1348 | (void) snprintf(buf, sizeof (buf), | |
1349 | "inherited from %s", source); | |
1350 | str = buf; | |
1351 | break; | |
428870ff BB |
1352 | case ZPROP_SRC_RECEIVED: |
1353 | str = "received"; | |
1354 | break; | |
34dc7c2f BB |
1355 | } |
1356 | break; | |
1357 | ||
428870ff BB |
1358 | case GET_COL_RECVD: |
1359 | str = (recvd_value == NULL ? "-" : recvd_value); | |
1360 | break; | |
1361 | ||
34dc7c2f BB |
1362 | default: |
1363 | continue; | |
1364 | } | |
1365 | ||
428870ff | 1366 | if (cbp->cb_columns[i + 1] == GET_COL_NONE) |
34dc7c2f BB |
1367 | (void) printf("%s", str); |
1368 | else if (cbp->cb_scripted) | |
1369 | (void) printf("%s\t", str); | |
1370 | else | |
1371 | (void) printf("%-*s ", | |
1372 | cbp->cb_colwidths[cbp->cb_columns[i]], | |
1373 | str); | |
34dc7c2f BB |
1374 | } |
1375 | ||
1376 | (void) printf("\n"); | |
1377 | } | |
1378 | ||
1379 | /* | |
1380 | * Given a numeric suffix, convert the value into a number of bits that the | |
1381 | * resulting value must be shifted. | |
1382 | */ | |
1383 | static int | |
1384 | str2shift(libzfs_handle_t *hdl, const char *buf) | |
1385 | { | |
1386 | const char *ends = "BKMGTPEZ"; | |
1387 | int i; | |
1388 | ||
1389 | if (buf[0] == '\0') | |
1390 | return (0); | |
1391 | for (i = 0; i < strlen(ends); i++) { | |
1392 | if (toupper(buf[0]) == ends[i]) | |
1393 | break; | |
1394 | } | |
1395 | if (i == strlen(ends)) { | |
53c2ec1d JL |
1396 | if (hdl) |
1397 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1398 | "invalid numeric suffix '%s'"), buf); | |
34dc7c2f BB |
1399 | return (-1); |
1400 | } | |
1401 | ||
1402 | /* | |
826ab7ad RL |
1403 | * Allow 'G' = 'GB' = 'GiB', case-insensitively. |
1404 | * However, 'BB' and 'BiB' are disallowed. | |
34dc7c2f | 1405 | */ |
826ab7ad RL |
1406 | if (buf[1] == '\0' || |
1407 | (toupper(buf[0]) != 'B' && | |
d1d7e268 MK |
1408 | ((toupper(buf[1]) == 'B' && buf[2] == '\0') || |
1409 | (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' && | |
1410 | buf[3] == '\0')))) | |
1411 | return (10 * i); | |
34dc7c2f | 1412 | |
53c2ec1d JL |
1413 | if (hdl) |
1414 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1415 | "invalid numeric suffix '%s'"), buf); | |
34dc7c2f BB |
1416 | return (-1); |
1417 | } | |
1418 | ||
1419 | /* | |
1420 | * Convert a string of the form '100G' into a real number. Used when setting | |
1421 | * properties or creating a volume. 'buf' is used to place an extended error | |
1422 | * message for the caller to use. | |
1423 | */ | |
1424 | int | |
1425 | zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) | |
1426 | { | |
1427 | char *end; | |
1428 | int shift; | |
1429 | ||
1430 | *num = 0; | |
1431 | ||
1432 | /* Check to see if this looks like a number. */ | |
1433 | if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { | |
1434 | if (hdl) | |
1435 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1436 | "bad numeric value '%s'"), value); | |
1437 | return (-1); | |
1438 | } | |
1439 | ||
428870ff | 1440 | /* Rely on strtoull() to process the numeric portion. */ |
34dc7c2f | 1441 | errno = 0; |
d164b209 | 1442 | *num = strtoull(value, &end, 10); |
34dc7c2f BB |
1443 | |
1444 | /* | |
1445 | * Check for ERANGE, which indicates that the value is too large to fit | |
1446 | * in a 64-bit value. | |
1447 | */ | |
1448 | if (errno == ERANGE) { | |
1449 | if (hdl) | |
1450 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1451 | "numeric value is too large")); | |
1452 | return (-1); | |
1453 | } | |
1454 | ||
1455 | /* | |
1456 | * If we have a decimal value, then do the computation with floating | |
1457 | * point arithmetic. Otherwise, use standard arithmetic. | |
1458 | */ | |
1459 | if (*end == '.') { | |
1460 | double fval = strtod(value, &end); | |
1461 | ||
1462 | if ((shift = str2shift(hdl, end)) == -1) | |
1463 | return (-1); | |
1464 | ||
1465 | fval *= pow(2, shift); | |
1466 | ||
1467 | if (fval > UINT64_MAX) { | |
1468 | if (hdl) | |
1469 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1470 | "numeric value is too large")); | |
1471 | return (-1); | |
1472 | } | |
1473 | ||
1474 | *num = (uint64_t)fval; | |
1475 | } else { | |
1476 | if ((shift = str2shift(hdl, end)) == -1) | |
1477 | return (-1); | |
1478 | ||
1479 | /* Check for overflow */ | |
1480 | if (shift >= 64 || (*num << shift) >> shift != *num) { | |
1481 | if (hdl) | |
1482 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1483 | "numeric value is too large")); | |
1484 | return (-1); | |
1485 | } | |
1486 | ||
1487 | *num <<= shift; | |
1488 | } | |
1489 | ||
1490 | return (0); | |
1491 | } | |
1492 | ||
1493 | /* | |
1494 | * Given a propname=value nvpair to set, parse any numeric properties | |
1495 | * (index, boolean, etc) if they are specified as strings and add the | |
1496 | * resulting nvpair to the returned nvlist. | |
1497 | * | |
1498 | * At the DSL layer, all properties are either 64-bit numbers or strings. | |
1499 | * We want the user to be able to ignore this fact and specify properties | |
1500 | * as native values (numbers, for example) or as strings (to simplify | |
1501 | * command line utilities). This also handles converting index types | |
1502 | * (compression, checksum, etc) from strings to their on-disk index. | |
1503 | */ | |
1504 | int | |
1505 | zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, | |
1506 | zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, | |
1507 | const char *errbuf) | |
1508 | { | |
1509 | data_type_t datatype = nvpair_type(elem); | |
1510 | zprop_type_t proptype; | |
1511 | const char *propname; | |
1512 | char *value; | |
1513 | boolean_t isnone = B_FALSE; | |
1514 | ||
1515 | if (type == ZFS_TYPE_POOL) { | |
1516 | proptype = zpool_prop_get_type(prop); | |
1517 | propname = zpool_prop_to_name(prop); | |
1518 | } else { | |
1519 | proptype = zfs_prop_get_type(prop); | |
1520 | propname = zfs_prop_to_name(prop); | |
1521 | } | |
1522 | ||
1523 | /* | |
1524 | * Convert any properties to the internal DSL value types. | |
1525 | */ | |
1526 | *svalp = NULL; | |
1527 | *ivalp = 0; | |
1528 | ||
1529 | switch (proptype) { | |
1530 | case PROP_TYPE_STRING: | |
1531 | if (datatype != DATA_TYPE_STRING) { | |
1532 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1533 | "'%s' must be a string"), nvpair_name(elem)); | |
1534 | goto error; | |
1535 | } | |
1536 | (void) nvpair_value_string(elem, svalp); | |
1537 | if (strlen(*svalp) >= ZFS_MAXPROPLEN) { | |
1538 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1539 | "'%s' is too long"), nvpair_name(elem)); | |
1540 | goto error; | |
1541 | } | |
1542 | break; | |
1543 | ||
1544 | case PROP_TYPE_NUMBER: | |
1545 | if (datatype == DATA_TYPE_STRING) { | |
1546 | (void) nvpair_value_string(elem, &value); | |
1547 | if (strcmp(value, "none") == 0) { | |
1548 | isnone = B_TRUE; | |
1549 | } else if (zfs_nicestrtonum(hdl, value, ivalp) | |
1550 | != 0) { | |
1551 | goto error; | |
1552 | } | |
1553 | } else if (datatype == DATA_TYPE_UINT64) { | |
1554 | (void) nvpair_value_uint64(elem, ivalp); | |
1555 | } else { | |
1556 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1557 | "'%s' must be a number"), nvpair_name(elem)); | |
1558 | goto error; | |
1559 | } | |
1560 | ||
1561 | /* | |
1562 | * Quota special: force 'none' and don't allow 0. | |
1563 | */ | |
1564 | if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && | |
1565 | (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { | |
1566 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1567 | "use 'none' to disable quota/refquota")); | |
1568 | goto error; | |
1569 | } | |
788eb90c JJ |
1570 | |
1571 | /* | |
1572 | * Special handling for "*_limit=none". In this case it's not | |
1573 | * 0 but UINT64_MAX. | |
1574 | */ | |
1575 | if ((type & ZFS_TYPE_DATASET) && isnone && | |
1576 | (prop == ZFS_PROP_FILESYSTEM_LIMIT || | |
1577 | prop == ZFS_PROP_SNAPSHOT_LIMIT)) { | |
1578 | *ivalp = UINT64_MAX; | |
1579 | } | |
34dc7c2f BB |
1580 | break; |
1581 | ||
1582 | case PROP_TYPE_INDEX: | |
1583 | if (datatype != DATA_TYPE_STRING) { | |
1584 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1585 | "'%s' must be a string"), nvpair_name(elem)); | |
1586 | goto error; | |
1587 | } | |
1588 | ||
1589 | (void) nvpair_value_string(elem, &value); | |
1590 | ||
1591 | if (zprop_string_to_index(prop, value, ivalp, type) != 0) { | |
1592 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1593 | "'%s' must be one of '%s'"), propname, | |
1594 | zprop_values(prop, type)); | |
1595 | goto error; | |
1596 | } | |
1597 | break; | |
1598 | ||
1599 | default: | |
1600 | abort(); | |
1601 | } | |
1602 | ||
1603 | /* | |
1604 | * Add the result to our return set of properties. | |
1605 | */ | |
1606 | if (*svalp != NULL) { | |
1607 | if (nvlist_add_string(ret, propname, *svalp) != 0) { | |
1608 | (void) no_memory(hdl); | |
1609 | return (-1); | |
1610 | } | |
1611 | } else { | |
1612 | if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { | |
1613 | (void) no_memory(hdl); | |
1614 | return (-1); | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | return (0); | |
1619 | error: | |
1620 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1621 | return (-1); | |
1622 | } | |
1623 | ||
b128c09f BB |
1624 | static int |
1625 | addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, | |
1626 | zfs_type_t type) | |
1627 | { | |
1628 | int prop; | |
1629 | zprop_list_t *entry; | |
1630 | ||
1631 | prop = zprop_name_to_prop(propname, type); | |
1632 | ||
962d5242 | 1633 | if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE)) |
b128c09f BB |
1634 | prop = ZPROP_INVAL; |
1635 | ||
1636 | /* | |
1637 | * When no property table entry can be found, return failure if | |
1638 | * this is a pool property or if this isn't a user-defined | |
1639 | * dataset property, | |
1640 | */ | |
9ae529ec CS |
1641 | if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL && |
1642 | !zpool_prop_feature(propname) && | |
1643 | !zpool_prop_unsupported(propname)) || | |
1644 | (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) && | |
1645 | !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) { | |
b128c09f BB |
1646 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1647 | "invalid property '%s'"), propname); | |
1648 | return (zfs_error(hdl, EZFS_BADPROP, | |
1649 | dgettext(TEXT_DOMAIN, "bad property list"))); | |
1650 | } | |
1651 | ||
1652 | if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) | |
1653 | return (-1); | |
1654 | ||
1655 | entry->pl_prop = prop; | |
1656 | if (prop == ZPROP_INVAL) { | |
9ae529ec CS |
1657 | if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == |
1658 | NULL) { | |
b128c09f BB |
1659 | free(entry); |
1660 | return (-1); | |
1661 | } | |
1662 | entry->pl_width = strlen(propname); | |
1663 | } else { | |
1664 | entry->pl_width = zprop_width(prop, &entry->pl_fixed, | |
1665 | type); | |
1666 | } | |
1667 | ||
1668 | *listp = entry; | |
1669 | ||
1670 | return (0); | |
1671 | } | |
1672 | ||
34dc7c2f BB |
1673 | /* |
1674 | * Given a comma-separated list of properties, construct a property list | |
1675 | * containing both user-defined and native properties. This function will | |
1676 | * return a NULL list if 'all' is specified, which can later be expanded | |
1677 | * by zprop_expand_list(). | |
1678 | */ | |
1679 | int | |
1680 | zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, | |
1681 | zfs_type_t type) | |
1682 | { | |
34dc7c2f | 1683 | *listp = NULL; |
34dc7c2f BB |
1684 | |
1685 | /* | |
1686 | * If 'all' is specified, return a NULL list. | |
1687 | */ | |
1688 | if (strcmp(props, "all") == 0) | |
1689 | return (0); | |
1690 | ||
1691 | /* | |
1692 | * If no props were specified, return an error. | |
1693 | */ | |
1694 | if (props[0] == '\0') { | |
1695 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1696 | "no properties specified")); | |
1697 | return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, | |
1698 | "bad property list"))); | |
1699 | } | |
1700 | ||
1701 | /* | |
1702 | * It would be nice to use getsubopt() here, but the inclusion of column | |
1703 | * aliases makes this more effort than it's worth. | |
1704 | */ | |
b128c09f BB |
1705 | while (*props != '\0') { |
1706 | size_t len; | |
1707 | char *p; | |
1708 | char c; | |
1709 | ||
1710 | if ((p = strchr(props, ',')) == NULL) { | |
1711 | len = strlen(props); | |
1712 | p = props + len; | |
34dc7c2f | 1713 | } else { |
b128c09f | 1714 | len = p - props; |
34dc7c2f BB |
1715 | } |
1716 | ||
1717 | /* | |
1718 | * Check for empty options. | |
1719 | */ | |
1720 | if (len == 0) { | |
1721 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1722 | "empty property name")); | |
1723 | return (zfs_error(hdl, EZFS_BADPROP, | |
1724 | dgettext(TEXT_DOMAIN, "bad property list"))); | |
1725 | } | |
1726 | ||
1727 | /* | |
1728 | * Check all regular property names. | |
1729 | */ | |
b128c09f BB |
1730 | c = props[len]; |
1731 | props[len] = '\0'; | |
1732 | ||
1733 | if (strcmp(props, "space") == 0) { | |
1734 | static char *spaceprops[] = { | |
1735 | "name", "avail", "used", "usedbysnapshots", | |
1736 | "usedbydataset", "usedbyrefreservation", | |
1737 | "usedbychildren", NULL | |
1738 | }; | |
1739 | int i; | |
1740 | ||
1741 | for (i = 0; spaceprops[i]; i++) { | |
1742 | if (addlist(hdl, spaceprops[i], listp, type)) | |
1743 | return (-1); | |
1744 | listp = &(*listp)->pl_next; | |
34dc7c2f | 1745 | } |
34dc7c2f | 1746 | } else { |
b128c09f BB |
1747 | if (addlist(hdl, props, listp, type)) |
1748 | return (-1); | |
1749 | listp = &(*listp)->pl_next; | |
34dc7c2f BB |
1750 | } |
1751 | ||
b128c09f | 1752 | props = p; |
34dc7c2f | 1753 | if (c == ',') |
b128c09f | 1754 | props++; |
34dc7c2f BB |
1755 | } |
1756 | ||
1757 | return (0); | |
1758 | } | |
1759 | ||
1760 | void | |
1761 | zprop_free_list(zprop_list_t *pl) | |
1762 | { | |
1763 | zprop_list_t *next; | |
1764 | ||
1765 | while (pl != NULL) { | |
1766 | next = pl->pl_next; | |
1767 | free(pl->pl_user_prop); | |
1768 | free(pl); | |
1769 | pl = next; | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | typedef struct expand_data { | |
1774 | zprop_list_t **last; | |
1775 | libzfs_handle_t *hdl; | |
1776 | zfs_type_t type; | |
1777 | } expand_data_t; | |
1778 | ||
1779 | int | |
1780 | zprop_expand_list_cb(int prop, void *cb) | |
1781 | { | |
1782 | zprop_list_t *entry; | |
1783 | expand_data_t *edp = cb; | |
1784 | ||
1785 | if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) | |
1786 | return (ZPROP_INVAL); | |
1787 | ||
1788 | entry->pl_prop = prop; | |
1789 | entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); | |
1790 | entry->pl_all = B_TRUE; | |
1791 | ||
1792 | *(edp->last) = entry; | |
1793 | edp->last = &entry->pl_next; | |
1794 | ||
1795 | return (ZPROP_CONT); | |
1796 | } | |
1797 | ||
1798 | int | |
1799 | zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) | |
1800 | { | |
1801 | zprop_list_t *entry; | |
1802 | zprop_list_t **last; | |
1803 | expand_data_t exp; | |
1804 | ||
1805 | if (*plp == NULL) { | |
1806 | /* | |
1807 | * If this is the very first time we've been called for an 'all' | |
1808 | * specification, expand the list to include all native | |
1809 | * properties. | |
1810 | */ | |
1811 | last = plp; | |
1812 | ||
1813 | exp.last = last; | |
1814 | exp.hdl = hdl; | |
1815 | exp.type = type; | |
1816 | ||
1817 | if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, | |
1818 | B_FALSE, type) == ZPROP_INVAL) | |
1819 | return (-1); | |
1820 | ||
1821 | /* | |
1822 | * Add 'name' to the beginning of the list, which is handled | |
1823 | * specially. | |
1824 | */ | |
1825 | if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) | |
1826 | return (-1); | |
1827 | ||
1828 | entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : | |
1829 | ZFS_PROP_NAME; | |
1830 | entry->pl_width = zprop_width(entry->pl_prop, | |
1831 | &entry->pl_fixed, type); | |
1832 | entry->pl_all = B_TRUE; | |
1833 | entry->pl_next = *plp; | |
1834 | *plp = entry; | |
1835 | } | |
1836 | return (0); | |
1837 | } | |
1838 | ||
1839 | int | |
1840 | zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, | |
1841 | zfs_type_t type) | |
1842 | { | |
1843 | return (zprop_iter_common(func, cb, show_all, ordered, type)); | |
1844 | } |