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