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