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