]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libzfs/libzfs_dataset.c
Illumos 5027 - zfs large block support
[mirror_zfs.git] / lib / libzfs / libzfs_dataset.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) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
31 */
32
33 #include <ctype.h>
34 #include <errno.h>
35 #include <libintl.h>
36 #include <math.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <strings.h>
40 #include <unistd.h>
41 #include <stddef.h>
42 #include <zone.h>
43 #include <fcntl.h>
44 #include <sys/mntent.h>
45 #include <sys/mount.h>
46 #include <priv.h>
47 #include <pwd.h>
48 #include <grp.h>
49 #include <stddef.h>
50 #include <ucred.h>
51 #ifdef HAVE_IDMAP
52 #include <idmap.h>
53 #include <aclutils.h>
54 #include <directory.h>
55 #endif /* HAVE_IDMAP */
56
57 #include <sys/dnode.h>
58 #include <sys/spa.h>
59 #include <sys/zap.h>
60 #include <libzfs.h>
61
62 #include "zfs_namecheck.h"
63 #include "zfs_prop.h"
64 #include "libzfs_impl.h"
65 #include "zfs_deleg.h"
66
67 static int userquota_propname_decode(const char *propname, boolean_t zoned,
68 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
69
70 /*
71 * Given a single type (not a mask of types), return the type in a human
72 * readable form.
73 */
74 const char *
75 zfs_type_to_name(zfs_type_t type)
76 {
77 switch (type) {
78 case ZFS_TYPE_FILESYSTEM:
79 return (dgettext(TEXT_DOMAIN, "filesystem"));
80 case ZFS_TYPE_SNAPSHOT:
81 return (dgettext(TEXT_DOMAIN, "snapshot"));
82 case ZFS_TYPE_VOLUME:
83 return (dgettext(TEXT_DOMAIN, "volume"));
84 default:
85 break;
86 }
87
88 return (NULL);
89 }
90
91 /*
92 * Validate a ZFS path. This is used even before trying to open the dataset, to
93 * provide a more meaningful error message. We call zfs_error_aux() to
94 * explain exactly why the name was not valid.
95 */
96 int
97 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
98 boolean_t modifying)
99 {
100 namecheck_err_t why;
101 char what;
102
103 (void) zfs_prop_get_table();
104 if (dataset_namecheck(path, &why, &what) != 0) {
105 if (hdl != NULL) {
106 switch (why) {
107 case NAME_ERR_TOOLONG:
108 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109 "name is too long"));
110 break;
111
112 case NAME_ERR_LEADING_SLASH:
113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
114 "leading slash in name"));
115 break;
116
117 case NAME_ERR_EMPTY_COMPONENT:
118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
119 "empty component in name"));
120 break;
121
122 case NAME_ERR_TRAILING_SLASH:
123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
124 "trailing slash in name"));
125 break;
126
127 case NAME_ERR_INVALCHAR:
128 zfs_error_aux(hdl,
129 dgettext(TEXT_DOMAIN, "invalid character "
130 "'%c' in name"), what);
131 break;
132
133 case NAME_ERR_MULTIPLE_AT:
134 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135 "multiple '@' delimiters in name"));
136 break;
137
138 case NAME_ERR_NOLETTER:
139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
140 "pool doesn't begin with a letter"));
141 break;
142
143 case NAME_ERR_RESERVED:
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "name is reserved"));
146 break;
147
148 case NAME_ERR_DISKLIKE:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "reserved disk name"));
151 break;
152 default:
153 break;
154 }
155 }
156
157 return (0);
158 }
159
160 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
161 if (hdl != NULL)
162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
163 "snapshot delimiter '@' in filesystem name"));
164 return (0);
165 }
166
167 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
168 if (hdl != NULL)
169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170 "missing '@' delimiter in snapshot name"));
171 return (0);
172 }
173
174 if (modifying && strchr(path, '%') != NULL) {
175 if (hdl != NULL)
176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177 "invalid character %c in name"), '%');
178 return (0);
179 }
180
181 return (-1);
182 }
183
184 int
185 zfs_name_valid(const char *name, zfs_type_t type)
186 {
187 if (type == ZFS_TYPE_POOL)
188 return (zpool_name_valid(NULL, B_FALSE, name));
189 return (zfs_validate_name(NULL, name, type, B_FALSE));
190 }
191
192 /*
193 * This function takes the raw DSL properties, and filters out the user-defined
194 * properties into a separate nvlist.
195 */
196 static nvlist_t *
197 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
198 {
199 libzfs_handle_t *hdl = zhp->zfs_hdl;
200 nvpair_t *elem;
201 nvlist_t *propval;
202 nvlist_t *nvl;
203
204 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
205 (void) no_memory(hdl);
206 return (NULL);
207 }
208
209 elem = NULL;
210 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
211 if (!zfs_prop_user(nvpair_name(elem)))
212 continue;
213
214 verify(nvpair_value_nvlist(elem, &propval) == 0);
215 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
216 nvlist_free(nvl);
217 (void) no_memory(hdl);
218 return (NULL);
219 }
220 }
221
222 return (nvl);
223 }
224
225 static zpool_handle_t *
226 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
227 {
228 libzfs_handle_t *hdl = zhp->zfs_hdl;
229 zpool_handle_t *zph;
230
231 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
232 if (hdl->libzfs_pool_handles != NULL)
233 zph->zpool_next = hdl->libzfs_pool_handles;
234 hdl->libzfs_pool_handles = zph;
235 }
236 return (zph);
237 }
238
239 static zpool_handle_t *
240 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
241 {
242 libzfs_handle_t *hdl = zhp->zfs_hdl;
243 zpool_handle_t *zph = hdl->libzfs_pool_handles;
244
245 while ((zph != NULL) &&
246 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
247 zph = zph->zpool_next;
248 return (zph);
249 }
250
251 /*
252 * Returns a handle to the pool that contains the provided dataset.
253 * If a handle to that pool already exists then that handle is returned.
254 * Otherwise, a new handle is created and added to the list of handles.
255 */
256 static zpool_handle_t *
257 zpool_handle(zfs_handle_t *zhp)
258 {
259 char *pool_name;
260 int len;
261 zpool_handle_t *zph;
262
263 len = strcspn(zhp->zfs_name, "/@#") + 1;
264 pool_name = zfs_alloc(zhp->zfs_hdl, len);
265 (void) strlcpy(pool_name, zhp->zfs_name, len);
266
267 zph = zpool_find_handle(zhp, pool_name, len);
268 if (zph == NULL)
269 zph = zpool_add_handle(zhp, pool_name);
270
271 free(pool_name);
272 return (zph);
273 }
274
275 void
276 zpool_free_handles(libzfs_handle_t *hdl)
277 {
278 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
279
280 while (zph != NULL) {
281 next = zph->zpool_next;
282 zpool_close(zph);
283 zph = next;
284 }
285 hdl->libzfs_pool_handles = NULL;
286 }
287
288 /*
289 * Utility function to gather stats (objset and zpl) for the given object.
290 */
291 static int
292 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
293 {
294 libzfs_handle_t *hdl = zhp->zfs_hdl;
295
296 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
297
298 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
299 if (errno == ENOMEM) {
300 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
301 return (-1);
302 }
303 } else {
304 return (-1);
305 }
306 }
307 return (0);
308 }
309
310 /*
311 * Utility function to get the received properties of the given object.
312 */
313 static int
314 get_recvd_props_ioctl(zfs_handle_t *zhp)
315 {
316 libzfs_handle_t *hdl = zhp->zfs_hdl;
317 nvlist_t *recvdprops;
318 zfs_cmd_t zc = {"\0"};
319 int err;
320
321 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
322 return (-1);
323
324 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
325
326 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
327 if (errno == ENOMEM) {
328 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
329 return (-1);
330 }
331 } else {
332 zcmd_free_nvlists(&zc);
333 return (-1);
334 }
335 }
336
337 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
338 zcmd_free_nvlists(&zc);
339 if (err != 0)
340 return (-1);
341
342 nvlist_free(zhp->zfs_recvd_props);
343 zhp->zfs_recvd_props = recvdprops;
344
345 return (0);
346 }
347
348 static int
349 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
350 {
351 nvlist_t *allprops, *userprops;
352
353 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
354
355 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
356 return (-1);
357 }
358
359 /*
360 * XXX Why do we store the user props separately, in addition to
361 * storing them in zfs_props?
362 */
363 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
364 nvlist_free(allprops);
365 return (-1);
366 }
367
368 nvlist_free(zhp->zfs_props);
369 nvlist_free(zhp->zfs_user_props);
370
371 zhp->zfs_props = allprops;
372 zhp->zfs_user_props = userprops;
373
374 return (0);
375 }
376
377 static int
378 get_stats(zfs_handle_t *zhp)
379 {
380 int rc = 0;
381 zfs_cmd_t zc = {"\0"};
382
383 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
384 return (-1);
385 if (get_stats_ioctl(zhp, &zc) != 0)
386 rc = -1;
387 else if (put_stats_zhdl(zhp, &zc) != 0)
388 rc = -1;
389 zcmd_free_nvlists(&zc);
390 return (rc);
391 }
392
393 /*
394 * Refresh the properties currently stored in the handle.
395 */
396 void
397 zfs_refresh_properties(zfs_handle_t *zhp)
398 {
399 (void) get_stats(zhp);
400 }
401
402 /*
403 * Makes a handle from the given dataset name. Used by zfs_open() and
404 * zfs_iter_* to create child handles on the fly.
405 */
406 static int
407 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
408 {
409 if (put_stats_zhdl(zhp, zc) != 0)
410 return (-1);
411
412 /*
413 * We've managed to open the dataset and gather statistics. Determine
414 * the high-level type.
415 */
416 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
417 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
418 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
419 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
420 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
421 return (-1);
422 else
423 abort();
424
425 if (zhp->zfs_dmustats.dds_is_snapshot)
426 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
427 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
428 zhp->zfs_type = ZFS_TYPE_VOLUME;
429 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
430 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
431 else
432 abort(); /* we should never see any other types */
433
434 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
435 return (-1);
436
437 return (0);
438 }
439
440 zfs_handle_t *
441 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
442 {
443 zfs_cmd_t zc = {"\0"};
444
445 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
446
447 if (zhp == NULL)
448 return (NULL);
449
450 zhp->zfs_hdl = hdl;
451 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
452 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
453 free(zhp);
454 return (NULL);
455 }
456 if (get_stats_ioctl(zhp, &zc) == -1) {
457 zcmd_free_nvlists(&zc);
458 free(zhp);
459 return (NULL);
460 }
461 if (make_dataset_handle_common(zhp, &zc) == -1) {
462 free(zhp);
463 zhp = NULL;
464 }
465 zcmd_free_nvlists(&zc);
466 return (zhp);
467 }
468
469 zfs_handle_t *
470 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
471 {
472 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
473
474 if (zhp == NULL)
475 return (NULL);
476
477 zhp->zfs_hdl = hdl;
478 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
479 if (make_dataset_handle_common(zhp, zc) == -1) {
480 free(zhp);
481 return (NULL);
482 }
483 return (zhp);
484 }
485
486 zfs_handle_t *
487 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
488 {
489 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
490
491 if (zhp == NULL)
492 return (NULL);
493
494 zhp->zfs_hdl = pzhp->zfs_hdl;
495 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
496 zhp->zfs_head_type = pzhp->zfs_type;
497 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
498 zhp->zpool_hdl = zpool_handle(zhp);
499
500 return (zhp);
501 }
502
503 zfs_handle_t *
504 zfs_handle_dup(zfs_handle_t *zhp_orig)
505 {
506 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
507
508 if (zhp == NULL)
509 return (NULL);
510
511 zhp->zfs_hdl = zhp_orig->zfs_hdl;
512 zhp->zpool_hdl = zhp_orig->zpool_hdl;
513 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
514 sizeof (zhp->zfs_name));
515 zhp->zfs_type = zhp_orig->zfs_type;
516 zhp->zfs_head_type = zhp_orig->zfs_head_type;
517 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
518 if (zhp_orig->zfs_props != NULL) {
519 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
520 (void) no_memory(zhp->zfs_hdl);
521 zfs_close(zhp);
522 return (NULL);
523 }
524 }
525 if (zhp_orig->zfs_user_props != NULL) {
526 if (nvlist_dup(zhp_orig->zfs_user_props,
527 &zhp->zfs_user_props, 0) != 0) {
528 (void) no_memory(zhp->zfs_hdl);
529 zfs_close(zhp);
530 return (NULL);
531 }
532 }
533 if (zhp_orig->zfs_recvd_props != NULL) {
534 if (nvlist_dup(zhp_orig->zfs_recvd_props,
535 &zhp->zfs_recvd_props, 0)) {
536 (void) no_memory(zhp->zfs_hdl);
537 zfs_close(zhp);
538 return (NULL);
539 }
540 }
541 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
542 if (zhp_orig->zfs_mntopts != NULL) {
543 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
544 zhp_orig->zfs_mntopts);
545 }
546 zhp->zfs_props_table = zhp_orig->zfs_props_table;
547 return (zhp);
548 }
549
550 boolean_t
551 zfs_bookmark_exists(const char *path)
552 {
553 nvlist_t *bmarks;
554 nvlist_t *props;
555 char fsname[ZFS_MAXNAMELEN];
556 char *bmark_name;
557 char *pound;
558 int err;
559 boolean_t rv;
560
561
562 (void) strlcpy(fsname, path, sizeof (fsname));
563 pound = strchr(fsname, '#');
564 if (pound == NULL)
565 return (B_FALSE);
566
567 *pound = '\0';
568 bmark_name = pound + 1;
569 props = fnvlist_alloc();
570 err = lzc_get_bookmarks(fsname, props, &bmarks);
571 nvlist_free(props);
572 if (err != 0) {
573 nvlist_free(bmarks);
574 return (B_FALSE);
575 }
576
577 rv = nvlist_exists(bmarks, bmark_name);
578 nvlist_free(bmarks);
579 return (rv);
580 }
581
582 zfs_handle_t *
583 make_bookmark_handle(zfs_handle_t *parent, const char *path,
584 nvlist_t *bmark_props)
585 {
586 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
587
588 if (zhp == NULL)
589 return (NULL);
590
591 /* Fill in the name. */
592 zhp->zfs_hdl = parent->zfs_hdl;
593 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
594
595 /* Set the property lists. */
596 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
597 free(zhp);
598 return (NULL);
599 }
600
601 /* Set the types. */
602 zhp->zfs_head_type = parent->zfs_head_type;
603 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
604
605 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
606 nvlist_free(zhp->zfs_props);
607 free(zhp);
608 return (NULL);
609 }
610
611 return (zhp);
612 }
613
614 /*
615 * Opens the given snapshot, filesystem, or volume. The 'types'
616 * argument is a mask of acceptable types. The function will print an
617 * appropriate error message and return NULL if it can't be opened.
618 */
619 zfs_handle_t *
620 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
621 {
622 zfs_handle_t *zhp;
623 char errbuf[1024];
624
625 (void) snprintf(errbuf, sizeof (errbuf),
626 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
627
628 /*
629 * Validate the name before we even try to open it.
630 */
631 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
633 "invalid dataset name"));
634 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
635 return (NULL);
636 }
637
638 /*
639 * Try to get stats for the dataset, which will tell us if it exists.
640 */
641 errno = 0;
642 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
643 (void) zfs_standard_error(hdl, errno, errbuf);
644 return (NULL);
645 }
646
647 if (!(types & zhp->zfs_type)) {
648 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
649 zfs_close(zhp);
650 return (NULL);
651 }
652
653 return (zhp);
654 }
655
656 /*
657 * Release a ZFS handle. Nothing to do but free the associated memory.
658 */
659 void
660 zfs_close(zfs_handle_t *zhp)
661 {
662 if (zhp->zfs_mntopts)
663 free(zhp->zfs_mntopts);
664 nvlist_free(zhp->zfs_props);
665 nvlist_free(zhp->zfs_user_props);
666 nvlist_free(zhp->zfs_recvd_props);
667 free(zhp);
668 }
669
670 typedef struct mnttab_node {
671 struct mnttab mtn_mt;
672 avl_node_t mtn_node;
673 } mnttab_node_t;
674
675 static int
676 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
677 {
678 const mnttab_node_t *mtn1 = arg1;
679 const mnttab_node_t *mtn2 = arg2;
680 int rv;
681
682 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
683
684 if (rv == 0)
685 return (0);
686 return (rv > 0 ? 1 : -1);
687 }
688
689 void
690 libzfs_mnttab_init(libzfs_handle_t *hdl)
691 {
692 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
693 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
694 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
695 }
696
697 int
698 libzfs_mnttab_update(libzfs_handle_t *hdl)
699 {
700 struct mnttab entry;
701
702 /* Reopen MNTTAB to prevent reading stale data from open file */
703 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
704 return (ENOENT);
705
706 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
707 mnttab_node_t *mtn;
708 avl_index_t where;
709
710 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
711 continue;
712
713 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
714 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
715 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
716 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
717 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
718
719 /* Exclude duplicate mounts */
720 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) {
721 free(mtn->mtn_mt.mnt_special);
722 free(mtn->mtn_mt.mnt_mountp);
723 free(mtn->mtn_mt.mnt_fstype);
724 free(mtn->mtn_mt.mnt_mntopts);
725 free(mtn);
726 continue;
727 }
728
729 avl_add(&hdl->libzfs_mnttab_cache, mtn);
730 }
731
732 return (0);
733 }
734
735 void
736 libzfs_mnttab_fini(libzfs_handle_t *hdl)
737 {
738 void *cookie = NULL;
739 mnttab_node_t *mtn;
740
741 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
742 free(mtn->mtn_mt.mnt_special);
743 free(mtn->mtn_mt.mnt_mountp);
744 free(mtn->mtn_mt.mnt_fstype);
745 free(mtn->mtn_mt.mnt_mntopts);
746 free(mtn);
747 }
748 avl_destroy(&hdl->libzfs_mnttab_cache);
749 }
750
751 void
752 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
753 {
754 hdl->libzfs_mnttab_enable = enable;
755 }
756
757 int
758 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
759 struct mnttab *entry)
760 {
761 mnttab_node_t find;
762 mnttab_node_t *mtn;
763 int error;
764
765 if (!hdl->libzfs_mnttab_enable) {
766 struct mnttab srch = { 0 };
767
768 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
769 libzfs_mnttab_fini(hdl);
770
771 /* Reopen MNTTAB to prevent reading stale data from open file */
772 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
773 return (ENOENT);
774
775 srch.mnt_special = (char *)fsname;
776 srch.mnt_fstype = MNTTYPE_ZFS;
777 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
778 return (0);
779 else
780 return (ENOENT);
781 }
782
783 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
784 if ((error = libzfs_mnttab_update(hdl)) != 0)
785 return (error);
786
787 find.mtn_mt.mnt_special = (char *)fsname;
788 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
789 if (mtn) {
790 *entry = mtn->mtn_mt;
791 return (0);
792 }
793 return (ENOENT);
794 }
795
796 void
797 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
798 const char *mountp, const char *mntopts)
799 {
800 mnttab_node_t *mtn;
801
802 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
803 return;
804 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
805 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
806 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
807 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
808 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
809 avl_add(&hdl->libzfs_mnttab_cache, mtn);
810 }
811
812 void
813 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
814 {
815 mnttab_node_t find;
816 mnttab_node_t *ret;
817
818 find.mtn_mt.mnt_special = (char *)fsname;
819 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
820 avl_remove(&hdl->libzfs_mnttab_cache, ret);
821 free(ret->mtn_mt.mnt_special);
822 free(ret->mtn_mt.mnt_mountp);
823 free(ret->mtn_mt.mnt_fstype);
824 free(ret->mtn_mt.mnt_mntopts);
825 free(ret);
826 }
827 }
828
829 int
830 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
831 {
832 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
833
834 if (zpool_handle == NULL)
835 return (-1);
836
837 *spa_version = zpool_get_prop_int(zpool_handle,
838 ZPOOL_PROP_VERSION, NULL);
839 return (0);
840 }
841
842 /*
843 * The choice of reservation property depends on the SPA version.
844 */
845 static int
846 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
847 {
848 int spa_version;
849
850 if (zfs_spa_version(zhp, &spa_version) < 0)
851 return (-1);
852
853 if (spa_version >= SPA_VERSION_REFRESERVATION)
854 *resv_prop = ZFS_PROP_REFRESERVATION;
855 else
856 *resv_prop = ZFS_PROP_RESERVATION;
857
858 return (0);
859 }
860
861 /*
862 * Given an nvlist of properties to set, validates that they are correct, and
863 * parses any numeric properties (index, boolean, etc) if they are specified as
864 * strings.
865 */
866 nvlist_t *
867 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
868 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
869 {
870 nvpair_t *elem;
871 uint64_t intval;
872 char *strval;
873 zfs_prop_t prop;
874 nvlist_t *ret;
875 int chosen_normal = -1;
876 int chosen_utf = -1;
877
878 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
879 (void) no_memory(hdl);
880 return (NULL);
881 }
882
883 /*
884 * Make sure this property is valid and applies to this type.
885 */
886
887 elem = NULL;
888 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
889 const char *propname = nvpair_name(elem);
890
891 prop = zfs_name_to_prop(propname);
892 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
893 /*
894 * This is a user property: make sure it's a
895 * string, and that it's less than ZAP_MAXNAMELEN.
896 */
897 if (nvpair_type(elem) != DATA_TYPE_STRING) {
898 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
899 "'%s' must be a string"), propname);
900 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
901 goto error;
902 }
903
904 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
905 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
906 "property name '%s' is too long"),
907 propname);
908 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
909 goto error;
910 }
911
912 (void) nvpair_value_string(elem, &strval);
913 if (nvlist_add_string(ret, propname, strval) != 0) {
914 (void) no_memory(hdl);
915 goto error;
916 }
917 continue;
918 }
919
920 /*
921 * Currently, only user properties can be modified on
922 * snapshots.
923 */
924 if (type == ZFS_TYPE_SNAPSHOT) {
925 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
926 "this property can not be modified for snapshots"));
927 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
928 goto error;
929 }
930
931 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
932 zfs_userquota_prop_t uqtype;
933 char newpropname[128];
934 char domain[128];
935 uint64_t rid;
936 uint64_t valary[3];
937
938 if (userquota_propname_decode(propname, zoned,
939 &uqtype, domain, sizeof (domain), &rid) != 0) {
940 zfs_error_aux(hdl,
941 dgettext(TEXT_DOMAIN,
942 "'%s' has an invalid user/group name"),
943 propname);
944 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
945 goto error;
946 }
947
948 if (uqtype != ZFS_PROP_USERQUOTA &&
949 uqtype != ZFS_PROP_GROUPQUOTA) {
950 zfs_error_aux(hdl,
951 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
952 propname);
953 (void) zfs_error(hdl, EZFS_PROPREADONLY,
954 errbuf);
955 goto error;
956 }
957
958 if (nvpair_type(elem) == DATA_TYPE_STRING) {
959 (void) nvpair_value_string(elem, &strval);
960 if (strcmp(strval, "none") == 0) {
961 intval = 0;
962 } else if (zfs_nicestrtonum(hdl,
963 strval, &intval) != 0) {
964 (void) zfs_error(hdl,
965 EZFS_BADPROP, errbuf);
966 goto error;
967 }
968 } else if (nvpair_type(elem) ==
969 DATA_TYPE_UINT64) {
970 (void) nvpair_value_uint64(elem, &intval);
971 if (intval == 0) {
972 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
973 "use 'none' to disable "
974 "userquota/groupquota"));
975 goto error;
976 }
977 } else {
978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
979 "'%s' must be a number"), propname);
980 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
981 goto error;
982 }
983
984 /*
985 * Encode the prop name as
986 * userquota@<hex-rid>-domain, to make it easy
987 * for the kernel to decode.
988 */
989 (void) snprintf(newpropname, sizeof (newpropname),
990 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
991 (longlong_t)rid, domain);
992 valary[0] = uqtype;
993 valary[1] = rid;
994 valary[2] = intval;
995 if (nvlist_add_uint64_array(ret, newpropname,
996 valary, 3) != 0) {
997 (void) no_memory(hdl);
998 goto error;
999 }
1000 continue;
1001 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003 "'%s' is readonly"),
1004 propname);
1005 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1006 goto error;
1007 }
1008
1009 if (prop == ZPROP_INVAL) {
1010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1011 "invalid property '%s'"), propname);
1012 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1013 goto error;
1014 }
1015
1016 if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
1017 zfs_error_aux(hdl,
1018 dgettext(TEXT_DOMAIN, "'%s' does not "
1019 "apply to datasets of this type"), propname);
1020 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1021 goto error;
1022 }
1023
1024 if (zfs_prop_readonly(prop) &&
1025 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1026 zfs_error_aux(hdl,
1027 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1028 propname);
1029 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1030 goto error;
1031 }
1032
1033 if (zprop_parse_value(hdl, elem, prop, type, ret,
1034 &strval, &intval, errbuf) != 0)
1035 goto error;
1036
1037 /*
1038 * Perform some additional checks for specific properties.
1039 */
1040 switch (prop) {
1041 case ZFS_PROP_VERSION:
1042 {
1043 int version;
1044
1045 if (zhp == NULL)
1046 break;
1047 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1048 if (intval < version) {
1049 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1050 "Can not downgrade; already at version %u"),
1051 version);
1052 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1053 goto error;
1054 }
1055 break;
1056 }
1057
1058 case ZFS_PROP_VOLBLOCKSIZE:
1059 case ZFS_PROP_RECORDSIZE:
1060 {
1061 int maxbs = SPA_MAXBLOCKSIZE;
1062 if (zhp != NULL) {
1063 maxbs = zpool_get_prop_int(zhp->zpool_hdl,
1064 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1065 }
1066 /*
1067 * The value must be a power of two between
1068 * SPA_MINBLOCKSIZE and maxbs.
1069 */
1070 if (intval < SPA_MINBLOCKSIZE ||
1071 intval > maxbs || !ISP2(intval)) {
1072 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1073 "'%s' must be power of 2 from 512B "
1074 "to %uKB"), propname, maxbs >> 10);
1075 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1076 goto error;
1077 }
1078 break;
1079 }
1080 case ZFS_PROP_MLSLABEL:
1081 {
1082 #ifdef HAVE_MLSLABEL
1083 /*
1084 * Verify the mlslabel string and convert to
1085 * internal hex label string.
1086 */
1087
1088 m_label_t *new_sl;
1089 char *hex = NULL; /* internal label string */
1090
1091 /* Default value is already OK. */
1092 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1093 break;
1094
1095 /* Verify the label can be converted to binary form */
1096 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1097 (str_to_label(strval, &new_sl, MAC_LABEL,
1098 L_NO_CORRECTION, NULL) == -1)) {
1099 goto badlabel;
1100 }
1101
1102 /* Now translate to hex internal label string */
1103 if (label_to_str(new_sl, &hex, M_INTERNAL,
1104 DEF_NAMES) != 0) {
1105 if (hex)
1106 free(hex);
1107 goto badlabel;
1108 }
1109 m_label_free(new_sl);
1110
1111 /* If string is already in internal form, we're done. */
1112 if (strcmp(strval, hex) == 0) {
1113 free(hex);
1114 break;
1115 }
1116
1117 /* Replace the label string with the internal form. */
1118 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1119 DATA_TYPE_STRING);
1120 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1121 hex) == 0);
1122 free(hex);
1123
1124 break;
1125
1126 badlabel:
1127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1128 "invalid mlslabel '%s'"), strval);
1129 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1130 m_label_free(new_sl); /* OK if null */
1131 goto error;
1132 #else
1133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1134 "mlslabels are unsupported"));
1135 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1136 goto error;
1137 #endif /* HAVE_MLSLABEL */
1138 }
1139
1140 case ZFS_PROP_MOUNTPOINT:
1141 {
1142 namecheck_err_t why;
1143
1144 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1145 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1146 break;
1147
1148 if (mountpoint_namecheck(strval, &why)) {
1149 switch (why) {
1150 case NAME_ERR_LEADING_SLASH:
1151 zfs_error_aux(hdl,
1152 dgettext(TEXT_DOMAIN,
1153 "'%s' must be an absolute path, "
1154 "'none', or 'legacy'"), propname);
1155 break;
1156 case NAME_ERR_TOOLONG:
1157 zfs_error_aux(hdl,
1158 dgettext(TEXT_DOMAIN,
1159 "component of '%s' is too long"),
1160 propname);
1161 break;
1162 default:
1163 break;
1164 }
1165 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1166 goto error;
1167 }
1168 }
1169
1170 /*FALLTHRU*/
1171
1172 case ZFS_PROP_SHARESMB:
1173 case ZFS_PROP_SHARENFS:
1174 /*
1175 * For the mountpoint and sharenfs or sharesmb
1176 * properties, check if it can be set in a
1177 * global/non-global zone based on
1178 * the zoned property value:
1179 *
1180 * global zone non-global zone
1181 * --------------------------------------------------
1182 * zoned=on mountpoint (no) mountpoint (yes)
1183 * sharenfs (no) sharenfs (no)
1184 * sharesmb (no) sharesmb (no)
1185 *
1186 * zoned=off mountpoint (yes) N/A
1187 * sharenfs (yes)
1188 * sharesmb (yes)
1189 */
1190 if (zoned) {
1191 if (getzoneid() == GLOBAL_ZONEID) {
1192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1193 "'%s' cannot be set on "
1194 "dataset in a non-global zone"),
1195 propname);
1196 (void) zfs_error(hdl, EZFS_ZONED,
1197 errbuf);
1198 goto error;
1199 } else if (prop == ZFS_PROP_SHARENFS ||
1200 prop == ZFS_PROP_SHARESMB) {
1201 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1202 "'%s' cannot be set in "
1203 "a non-global zone"), propname);
1204 (void) zfs_error(hdl, EZFS_ZONED,
1205 errbuf);
1206 goto error;
1207 }
1208 } else if (getzoneid() != GLOBAL_ZONEID) {
1209 /*
1210 * If zoned property is 'off', this must be in
1211 * a global zone. If not, something is wrong.
1212 */
1213 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1214 "'%s' cannot be set while dataset "
1215 "'zoned' property is set"), propname);
1216 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1217 goto error;
1218 }
1219
1220 /*
1221 * At this point, it is legitimate to set the
1222 * property. Now we want to make sure that the
1223 * property value is valid if it is sharenfs.
1224 */
1225 if ((prop == ZFS_PROP_SHARENFS ||
1226 prop == ZFS_PROP_SHARESMB) &&
1227 strcmp(strval, "on") != 0 &&
1228 strcmp(strval, "off") != 0) {
1229 zfs_share_proto_t proto;
1230
1231 if (prop == ZFS_PROP_SHARESMB)
1232 proto = PROTO_SMB;
1233 else
1234 proto = PROTO_NFS;
1235
1236 /*
1237 * Must be an valid sharing protocol
1238 * option string so init the libshare
1239 * in order to enable the parser and
1240 * then parse the options. We use the
1241 * control API since we don't care about
1242 * the current configuration and don't
1243 * want the overhead of loading it
1244 * until we actually do something.
1245 */
1246
1247 if (zfs_init_libshare(hdl,
1248 SA_INIT_CONTROL_API) != SA_OK) {
1249 /*
1250 * An error occurred so we can't do
1251 * anything
1252 */
1253 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1254 "'%s' cannot be set: problem "
1255 "in share initialization"),
1256 propname);
1257 (void) zfs_error(hdl, EZFS_BADPROP,
1258 errbuf);
1259 goto error;
1260 }
1261
1262 if (zfs_parse_options(strval, proto) != SA_OK) {
1263 /*
1264 * There was an error in parsing so
1265 * deal with it by issuing an error
1266 * message and leaving after
1267 * uninitializing the the libshare
1268 * interface.
1269 */
1270 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1271 "'%s' cannot be set to invalid "
1272 "options"), propname);
1273 (void) zfs_error(hdl, EZFS_BADPROP,
1274 errbuf);
1275 zfs_uninit_libshare(hdl);
1276 goto error;
1277 }
1278 zfs_uninit_libshare(hdl);
1279 }
1280
1281 break;
1282 case ZFS_PROP_UTF8ONLY:
1283 chosen_utf = (int)intval;
1284 break;
1285 case ZFS_PROP_NORMALIZE:
1286 chosen_normal = (int)intval;
1287 break;
1288 default:
1289 break;
1290 }
1291
1292 /*
1293 * For changes to existing volumes, we have some additional
1294 * checks to enforce.
1295 */
1296 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1297 uint64_t volsize = zfs_prop_get_int(zhp,
1298 ZFS_PROP_VOLSIZE);
1299 uint64_t blocksize = zfs_prop_get_int(zhp,
1300 ZFS_PROP_VOLBLOCKSIZE);
1301 char buf[64];
1302
1303 switch (prop) {
1304 case ZFS_PROP_RESERVATION:
1305 case ZFS_PROP_REFRESERVATION:
1306 if (intval > volsize) {
1307 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1308 "'%s' is greater than current "
1309 "volume size"), propname);
1310 (void) zfs_error(hdl, EZFS_BADPROP,
1311 errbuf);
1312 goto error;
1313 }
1314 break;
1315
1316 case ZFS_PROP_VOLSIZE:
1317 if (intval % blocksize != 0) {
1318 zfs_nicenum(blocksize, buf,
1319 sizeof (buf));
1320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1321 "'%s' must be a multiple of "
1322 "volume block size (%s)"),
1323 propname, buf);
1324 (void) zfs_error(hdl, EZFS_BADPROP,
1325 errbuf);
1326 goto error;
1327 }
1328
1329 if (intval == 0) {
1330 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1331 "'%s' cannot be zero"),
1332 propname);
1333 (void) zfs_error(hdl, EZFS_BADPROP,
1334 errbuf);
1335 goto error;
1336 }
1337 break;
1338 default:
1339 break;
1340 }
1341 }
1342 }
1343
1344 /*
1345 * If normalization was chosen, but no UTF8 choice was made,
1346 * enforce rejection of non-UTF8 names.
1347 *
1348 * If normalization was chosen, but rejecting non-UTF8 names
1349 * was explicitly not chosen, it is an error.
1350 */
1351 if (chosen_normal > 0 && chosen_utf < 0) {
1352 if (nvlist_add_uint64(ret,
1353 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1354 (void) no_memory(hdl);
1355 goto error;
1356 }
1357 } else if (chosen_normal > 0 && chosen_utf == 0) {
1358 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1359 "'%s' must be set 'on' if normalization chosen"),
1360 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1361 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1362 goto error;
1363 }
1364 return (ret);
1365
1366 error:
1367 nvlist_free(ret);
1368 return (NULL);
1369 }
1370
1371 int
1372 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1373 {
1374 uint64_t old_volsize;
1375 uint64_t new_volsize;
1376 uint64_t old_reservation;
1377 uint64_t new_reservation;
1378 zfs_prop_t resv_prop;
1379
1380 /*
1381 * If this is an existing volume, and someone is setting the volsize,
1382 * make sure that it matches the reservation, or add it if necessary.
1383 */
1384 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1385 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1386 return (-1);
1387 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1388 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1389 old_reservation) || nvlist_lookup_uint64(nvl,
1390 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1391 return (0);
1392 }
1393 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1394 &new_volsize) != 0)
1395 return (-1);
1396 new_reservation = zvol_volsize_to_reservation(new_volsize,
1397 zhp->zfs_props);
1398 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1399 new_reservation) != 0) {
1400 (void) no_memory(zhp->zfs_hdl);
1401 return (-1);
1402 }
1403 return (1);
1404 }
1405
1406 void
1407 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1408 char *errbuf)
1409 {
1410 switch (err) {
1411
1412 case ENOSPC:
1413 /*
1414 * For quotas and reservations, ENOSPC indicates
1415 * something different; setting a quota or reservation
1416 * doesn't use any disk space.
1417 */
1418 switch (prop) {
1419 case ZFS_PROP_QUOTA:
1420 case ZFS_PROP_REFQUOTA:
1421 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1422 "size is less than current used or "
1423 "reserved space"));
1424 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1425 break;
1426
1427 case ZFS_PROP_RESERVATION:
1428 case ZFS_PROP_REFRESERVATION:
1429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1430 "size is greater than available space"));
1431 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1432 break;
1433
1434 default:
1435 (void) zfs_standard_error(hdl, err, errbuf);
1436 break;
1437 }
1438 break;
1439
1440 case EBUSY:
1441 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1442 break;
1443
1444 case EROFS:
1445 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1446 break;
1447
1448 case ENOTSUP:
1449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1450 "pool and or dataset must be upgraded to set this "
1451 "property or value"));
1452 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1453 break;
1454
1455 case ERANGE:
1456 if (prop == ZFS_PROP_COMPRESSION ||
1457 prop == ZFS_PROP_RECORDSIZE) {
1458 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1459 "property setting is not allowed on "
1460 "bootable datasets"));
1461 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1462 } else {
1463 (void) zfs_standard_error(hdl, err, errbuf);
1464 }
1465 break;
1466
1467 case EINVAL:
1468 if (prop == ZPROP_INVAL) {
1469 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1470 } else {
1471 (void) zfs_standard_error(hdl, err, errbuf);
1472 }
1473 break;
1474
1475 case EOVERFLOW:
1476 /*
1477 * This platform can't address a volume this big.
1478 */
1479 #ifdef _ILP32
1480 if (prop == ZFS_PROP_VOLSIZE) {
1481 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1482 break;
1483 }
1484 #endif
1485 /* FALLTHROUGH */
1486 default:
1487 (void) zfs_standard_error(hdl, err, errbuf);
1488 }
1489 }
1490
1491 static boolean_t
1492 zfs_is_namespace_prop(zfs_prop_t prop)
1493 {
1494 switch (prop) {
1495
1496 case ZFS_PROP_ATIME:
1497 case ZFS_PROP_RELATIME:
1498 case ZFS_PROP_DEVICES:
1499 case ZFS_PROP_EXEC:
1500 case ZFS_PROP_SETUID:
1501 case ZFS_PROP_READONLY:
1502 case ZFS_PROP_XATTR:
1503 case ZFS_PROP_NBMAND:
1504 return (B_TRUE);
1505
1506 default:
1507 return (B_FALSE);
1508 }
1509 }
1510
1511 /*
1512 * Given a property name and value, set the property for the given dataset.
1513 */
1514 int
1515 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1516 {
1517 zfs_cmd_t zc = {"\0"};
1518 int ret = -1;
1519 prop_changelist_t *cl = NULL;
1520 char errbuf[1024];
1521 libzfs_handle_t *hdl = zhp->zfs_hdl;
1522 nvlist_t *nvl = NULL, *realprops;
1523 zfs_prop_t prop;
1524 boolean_t do_prefix = B_TRUE;
1525 int added_resv = 0;
1526
1527 (void) snprintf(errbuf, sizeof (errbuf),
1528 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1529 zhp->zfs_name);
1530
1531 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1532 nvlist_add_string(nvl, propname, propval) != 0) {
1533 (void) no_memory(hdl);
1534 goto error;
1535 }
1536
1537 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1538 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1539 goto error;
1540
1541 nvlist_free(nvl);
1542 nvl = realprops;
1543
1544 prop = zfs_name_to_prop(propname);
1545
1546 if (prop == ZFS_PROP_VOLSIZE) {
1547 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1548 goto error;
1549 }
1550
1551 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1552 goto error;
1553
1554 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1555 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1556 "child dataset with inherited mountpoint is used "
1557 "in a non-global zone"));
1558 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1559 goto error;
1560 }
1561
1562 /*
1563 * We don't want to unmount & remount the dataset when changing
1564 * its canmount property to 'on' or 'noauto'. We only use
1565 * the changelist logic to unmount when setting canmount=off.
1566 */
1567 if (prop == ZFS_PROP_CANMOUNT) {
1568 uint64_t idx;
1569 int err = zprop_string_to_index(prop, propval, &idx,
1570 ZFS_TYPE_DATASET);
1571 if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1572 do_prefix = B_FALSE;
1573 }
1574
1575 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1576 goto error;
1577
1578 /*
1579 * Execute the corresponding ioctl() to set this property.
1580 */
1581 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1582
1583 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1584 goto error;
1585
1586 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1587
1588 if (ret != 0) {
1589 zfs_setprop_error(hdl, prop, errno, errbuf);
1590 if (added_resv && errno == ENOSPC) {
1591 /* clean up the volsize property we tried to set */
1592 uint64_t old_volsize = zfs_prop_get_int(zhp,
1593 ZFS_PROP_VOLSIZE);
1594 nvlist_free(nvl);
1595 zcmd_free_nvlists(&zc);
1596 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1597 goto error;
1598 if (nvlist_add_uint64(nvl,
1599 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1600 old_volsize) != 0)
1601 goto error;
1602 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1603 goto error;
1604 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1605 }
1606 } else {
1607 if (do_prefix)
1608 ret = changelist_postfix(cl);
1609
1610 if (ret == 0) {
1611 /*
1612 * Refresh the statistics so the new property
1613 * value is reflected.
1614 */
1615 (void) get_stats(zhp);
1616
1617 /*
1618 * Remount the filesystem to propagate the change
1619 * if one of the options handled by the generic
1620 * Linux namespace layer has been modified.
1621 */
1622 if (zfs_is_namespace_prop(prop) &&
1623 zfs_is_mounted(zhp, NULL))
1624 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1625 }
1626 }
1627
1628 error:
1629 nvlist_free(nvl);
1630 zcmd_free_nvlists(&zc);
1631 if (cl)
1632 changelist_free(cl);
1633 return (ret);
1634 }
1635
1636 /*
1637 * Given a property, inherit the value from the parent dataset, or if received
1638 * is TRUE, revert to the received value, if any.
1639 */
1640 int
1641 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1642 {
1643 zfs_cmd_t zc = {"\0"};
1644 int ret;
1645 prop_changelist_t *cl;
1646 libzfs_handle_t *hdl = zhp->zfs_hdl;
1647 char errbuf[1024];
1648 zfs_prop_t prop;
1649
1650 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1651 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1652
1653 zc.zc_cookie = received;
1654 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1655 /*
1656 * For user properties, the amount of work we have to do is very
1657 * small, so just do it here.
1658 */
1659 if (!zfs_prop_user(propname)) {
1660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1661 "invalid property"));
1662 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1663 }
1664
1665 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1666 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1667
1668 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1669 return (zfs_standard_error(hdl, errno, errbuf));
1670
1671 return (0);
1672 }
1673
1674 /*
1675 * Verify that this property is inheritable.
1676 */
1677 if (zfs_prop_readonly(prop))
1678 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1679
1680 if (!zfs_prop_inheritable(prop) && !received)
1681 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1682
1683 /*
1684 * Check to see if the value applies to this type
1685 */
1686 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
1687 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1688
1689 /*
1690 * Normalize the name, to get rid of shorthand abbreviations.
1691 */
1692 propname = zfs_prop_to_name(prop);
1693 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1694 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1695
1696 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1697 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1698 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1699 "dataset is used in a non-global zone"));
1700 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1701 }
1702
1703 /*
1704 * Determine datasets which will be affected by this change, if any.
1705 */
1706 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1707 return (-1);
1708
1709 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1711 "child dataset with inherited mountpoint is used "
1712 "in a non-global zone"));
1713 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1714 goto error;
1715 }
1716
1717 if ((ret = changelist_prefix(cl)) != 0)
1718 goto error;
1719
1720 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1721 return (zfs_standard_error(hdl, errno, errbuf));
1722 } else {
1723
1724 if ((ret = changelist_postfix(cl)) != 0)
1725 goto error;
1726
1727 /*
1728 * Refresh the statistics so the new property is reflected.
1729 */
1730 (void) get_stats(zhp);
1731
1732 /*
1733 * Remount the filesystem to propagate the change
1734 * if one of the options handled by the generic
1735 * Linux namespace layer has been modified.
1736 */
1737 if (zfs_is_namespace_prop(prop) &&
1738 zfs_is_mounted(zhp, NULL))
1739 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1740 }
1741
1742 error:
1743 changelist_free(cl);
1744 return (ret);
1745 }
1746
1747 /*
1748 * True DSL properties are stored in an nvlist. The following two functions
1749 * extract them appropriately.
1750 */
1751 uint64_t
1752 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1753 {
1754 nvlist_t *nv;
1755 uint64_t value;
1756
1757 *source = NULL;
1758 if (nvlist_lookup_nvlist(zhp->zfs_props,
1759 zfs_prop_to_name(prop), &nv) == 0) {
1760 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1761 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1762 } else {
1763 verify(!zhp->zfs_props_table ||
1764 zhp->zfs_props_table[prop] == B_TRUE);
1765 value = zfs_prop_default_numeric(prop);
1766 *source = "";
1767 }
1768
1769 return (value);
1770 }
1771
1772 static char *
1773 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1774 {
1775 nvlist_t *nv;
1776 char *value;
1777
1778 *source = NULL;
1779 if (nvlist_lookup_nvlist(zhp->zfs_props,
1780 zfs_prop_to_name(prop), &nv) == 0) {
1781 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1782 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1783 } else {
1784 verify(!zhp->zfs_props_table ||
1785 zhp->zfs_props_table[prop] == B_TRUE);
1786 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1787 value = "";
1788 *source = "";
1789 }
1790
1791 return (value);
1792 }
1793
1794 static boolean_t
1795 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1796 {
1797 return (zhp->zfs_props == zhp->zfs_recvd_props);
1798 }
1799
1800 static void
1801 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1802 {
1803 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1804 zhp->zfs_props = zhp->zfs_recvd_props;
1805 }
1806
1807 static void
1808 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1809 {
1810 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1811 *cookie = 0;
1812 }
1813
1814 /*
1815 * Internal function for getting a numeric property. Both zfs_prop_get() and
1816 * zfs_prop_get_int() are built using this interface.
1817 *
1818 * Certain properties can be overridden using 'mount -o'. In this case, scan
1819 * the contents of the /etc/mtab entry, searching for the appropriate options.
1820 * If they differ from the on-disk values, report the current values and mark
1821 * the source "temporary".
1822 */
1823 static int
1824 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1825 char **source, uint64_t *val)
1826 {
1827 zfs_cmd_t zc = {"\0"};
1828 nvlist_t *zplprops = NULL;
1829 struct mnttab mnt;
1830 char *mntopt_on = NULL;
1831 char *mntopt_off = NULL;
1832 boolean_t received = zfs_is_recvd_props_mode(zhp);
1833
1834 *source = NULL;
1835
1836 /*
1837 * If the property is being fetched for a snapshot, check whether
1838 * the property is valid for the snapshot's head dataset type.
1839 */
1840 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
1841 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
1842 *val = zfs_prop_default_numeric(prop);
1843 return (-1);
1844 }
1845
1846 switch (prop) {
1847 case ZFS_PROP_ATIME:
1848 mntopt_on = MNTOPT_ATIME;
1849 mntopt_off = MNTOPT_NOATIME;
1850 break;
1851
1852 case ZFS_PROP_RELATIME:
1853 mntopt_on = MNTOPT_RELATIME;
1854 mntopt_off = MNTOPT_NORELATIME;
1855 break;
1856
1857 case ZFS_PROP_DEVICES:
1858 mntopt_on = MNTOPT_DEVICES;
1859 mntopt_off = MNTOPT_NODEVICES;
1860 break;
1861
1862 case ZFS_PROP_EXEC:
1863 mntopt_on = MNTOPT_EXEC;
1864 mntopt_off = MNTOPT_NOEXEC;
1865 break;
1866
1867 case ZFS_PROP_READONLY:
1868 mntopt_on = MNTOPT_RO;
1869 mntopt_off = MNTOPT_RW;
1870 break;
1871
1872 case ZFS_PROP_SETUID:
1873 mntopt_on = MNTOPT_SETUID;
1874 mntopt_off = MNTOPT_NOSETUID;
1875 break;
1876
1877 case ZFS_PROP_XATTR:
1878 mntopt_on = MNTOPT_XATTR;
1879 mntopt_off = MNTOPT_NOXATTR;
1880 break;
1881
1882 case ZFS_PROP_NBMAND:
1883 mntopt_on = MNTOPT_NBMAND;
1884 mntopt_off = MNTOPT_NONBMAND;
1885 break;
1886 default:
1887 break;
1888 }
1889
1890 /*
1891 * Because looking up the mount options is potentially expensive
1892 * (iterating over all of /etc/mtab), we defer its calculation until
1893 * we're looking up a property which requires its presence.
1894 */
1895 if (!zhp->zfs_mntcheck &&
1896 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1897 libzfs_handle_t *hdl = zhp->zfs_hdl;
1898 struct mnttab entry;
1899
1900 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1901 zhp->zfs_mntopts = zfs_strdup(hdl,
1902 entry.mnt_mntopts);
1903 if (zhp->zfs_mntopts == NULL)
1904 return (-1);
1905 }
1906
1907 zhp->zfs_mntcheck = B_TRUE;
1908 }
1909
1910 if (zhp->zfs_mntopts == NULL)
1911 mnt.mnt_mntopts = "";
1912 else
1913 mnt.mnt_mntopts = zhp->zfs_mntopts;
1914
1915 switch (prop) {
1916 case ZFS_PROP_DEVICES:
1917 case ZFS_PROP_EXEC:
1918 case ZFS_PROP_READONLY:
1919 case ZFS_PROP_SETUID:
1920 case ZFS_PROP_XATTR:
1921 case ZFS_PROP_NBMAND:
1922 *val = getprop_uint64(zhp, prop, source);
1923
1924 if (received)
1925 break;
1926
1927 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1928 *val = B_TRUE;
1929 if (src)
1930 *src = ZPROP_SRC_TEMPORARY;
1931 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1932 *val = B_FALSE;
1933 if (src)
1934 *src = ZPROP_SRC_TEMPORARY;
1935 }
1936 break;
1937
1938 case ZFS_PROP_ATIME:
1939 case ZFS_PROP_RELATIME:
1940 case ZFS_PROP_CANMOUNT:
1941 case ZFS_PROP_VOLSIZE:
1942 case ZFS_PROP_QUOTA:
1943 case ZFS_PROP_REFQUOTA:
1944 case ZFS_PROP_RESERVATION:
1945 case ZFS_PROP_REFRESERVATION:
1946 case ZFS_PROP_FILESYSTEM_LIMIT:
1947 case ZFS_PROP_SNAPSHOT_LIMIT:
1948 case ZFS_PROP_FILESYSTEM_COUNT:
1949 case ZFS_PROP_SNAPSHOT_COUNT:
1950 *val = getprop_uint64(zhp, prop, source);
1951
1952 if (*source == NULL) {
1953 /* not default, must be local */
1954 *source = zhp->zfs_name;
1955 }
1956 break;
1957
1958 case ZFS_PROP_MOUNTED:
1959 *val = (zhp->zfs_mntopts != NULL);
1960 break;
1961
1962 case ZFS_PROP_NUMCLONES:
1963 *val = zhp->zfs_dmustats.dds_num_clones;
1964 break;
1965
1966 case ZFS_PROP_VERSION:
1967 case ZFS_PROP_NORMALIZE:
1968 case ZFS_PROP_UTF8ONLY:
1969 case ZFS_PROP_CASE:
1970 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1971 return (-1);
1972 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1973 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1974 zcmd_free_nvlists(&zc);
1975 if (prop == ZFS_PROP_VERSION &&
1976 zhp->zfs_type == ZFS_TYPE_VOLUME)
1977 *val = zfs_prop_default_numeric(prop);
1978 return (-1);
1979 }
1980 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1981 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1982 val) != 0) {
1983 zcmd_free_nvlists(&zc);
1984 return (-1);
1985 }
1986 if (zplprops)
1987 nvlist_free(zplprops);
1988 zcmd_free_nvlists(&zc);
1989 break;
1990
1991 case ZFS_PROP_INCONSISTENT:
1992 *val = zhp->zfs_dmustats.dds_inconsistent;
1993 break;
1994
1995 default:
1996 switch (zfs_prop_get_type(prop)) {
1997 case PROP_TYPE_NUMBER:
1998 case PROP_TYPE_INDEX:
1999 *val = getprop_uint64(zhp, prop, source);
2000 /*
2001 * If we tried to use a default value for a
2002 * readonly property, it means that it was not
2003 * present.
2004 */
2005 if (zfs_prop_readonly(prop) &&
2006 *source != NULL && (*source)[0] == '\0') {
2007 *source = NULL;
2008 }
2009 break;
2010
2011 case PROP_TYPE_STRING:
2012 default:
2013 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2014 "cannot get non-numeric property"));
2015 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2016 dgettext(TEXT_DOMAIN, "internal error")));
2017 }
2018 }
2019
2020 return (0);
2021 }
2022
2023 /*
2024 * Calculate the source type, given the raw source string.
2025 */
2026 static void
2027 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2028 char *statbuf, size_t statlen)
2029 {
2030 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2031 return;
2032
2033 if (source == NULL) {
2034 *srctype = ZPROP_SRC_NONE;
2035 } else if (source[0] == '\0') {
2036 *srctype = ZPROP_SRC_DEFAULT;
2037 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2038 *srctype = ZPROP_SRC_RECEIVED;
2039 } else {
2040 if (strcmp(source, zhp->zfs_name) == 0) {
2041 *srctype = ZPROP_SRC_LOCAL;
2042 } else {
2043 (void) strlcpy(statbuf, source, statlen);
2044 *srctype = ZPROP_SRC_INHERITED;
2045 }
2046 }
2047
2048 }
2049
2050 int
2051 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2052 size_t proplen, boolean_t literal)
2053 {
2054 zfs_prop_t prop;
2055 int err = 0;
2056
2057 if (zhp->zfs_recvd_props == NULL)
2058 if (get_recvd_props_ioctl(zhp) != 0)
2059 return (-1);
2060
2061 prop = zfs_name_to_prop(propname);
2062
2063 if (prop != ZPROP_INVAL) {
2064 uint64_t cookie;
2065 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2066 return (-1);
2067 zfs_set_recvd_props_mode(zhp, &cookie);
2068 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2069 NULL, NULL, 0, literal);
2070 zfs_unset_recvd_props_mode(zhp, &cookie);
2071 } else {
2072 nvlist_t *propval;
2073 char *recvdval;
2074 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2075 propname, &propval) != 0)
2076 return (-1);
2077 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2078 &recvdval) == 0);
2079 (void) strlcpy(propbuf, recvdval, proplen);
2080 }
2081
2082 return (err == 0 ? 0 : -1);
2083 }
2084
2085 static int
2086 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2087 {
2088 nvlist_t *value;
2089 nvpair_t *pair;
2090
2091 value = zfs_get_clones_nvl(zhp);
2092 if (value == NULL)
2093 return (-1);
2094
2095 propbuf[0] = '\0';
2096 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2097 pair = nvlist_next_nvpair(value, pair)) {
2098 if (propbuf[0] != '\0')
2099 (void) strlcat(propbuf, ",", proplen);
2100 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2101 }
2102
2103 return (0);
2104 }
2105
2106 struct get_clones_arg {
2107 uint64_t numclones;
2108 nvlist_t *value;
2109 const char *origin;
2110 char buf[ZFS_MAXNAMELEN];
2111 };
2112
2113 int
2114 get_clones_cb(zfs_handle_t *zhp, void *arg)
2115 {
2116 struct get_clones_arg *gca = arg;
2117
2118 if (gca->numclones == 0) {
2119 zfs_close(zhp);
2120 return (0);
2121 }
2122
2123 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2124 NULL, NULL, 0, B_TRUE) != 0)
2125 goto out;
2126 if (strcmp(gca->buf, gca->origin) == 0) {
2127 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2128 gca->numclones--;
2129 }
2130
2131 out:
2132 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2133 zfs_close(zhp);
2134 return (0);
2135 }
2136
2137 nvlist_t *
2138 zfs_get_clones_nvl(zfs_handle_t *zhp)
2139 {
2140 nvlist_t *nv, *value;
2141
2142 if (nvlist_lookup_nvlist(zhp->zfs_props,
2143 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2144 struct get_clones_arg gca;
2145
2146 /*
2147 * if this is a snapshot, then the kernel wasn't able
2148 * to get the clones. Do it by slowly iterating.
2149 */
2150 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2151 return (NULL);
2152 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2153 return (NULL);
2154 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2155 nvlist_free(nv);
2156 return (NULL);
2157 }
2158
2159 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2160 gca.value = value;
2161 gca.origin = zhp->zfs_name;
2162
2163 if (gca.numclones != 0) {
2164 zfs_handle_t *root;
2165 char pool[ZFS_MAXNAMELEN];
2166 char *cp = pool;
2167
2168 /* get the pool name */
2169 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2170 (void) strsep(&cp, "/@");
2171 root = zfs_open(zhp->zfs_hdl, pool,
2172 ZFS_TYPE_FILESYSTEM);
2173
2174 (void) get_clones_cb(root, &gca);
2175 }
2176
2177 if (gca.numclones != 0 ||
2178 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2179 nvlist_add_nvlist(zhp->zfs_props,
2180 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2181 nvlist_free(nv);
2182 nvlist_free(value);
2183 return (NULL);
2184 }
2185 nvlist_free(nv);
2186 nvlist_free(value);
2187 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2188 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2189 }
2190
2191 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2192
2193 return (value);
2194 }
2195
2196 /*
2197 * Retrieve a property from the given object. If 'literal' is specified, then
2198 * numbers are left as exact values. Otherwise, numbers are converted to a
2199 * human-readable form.
2200 *
2201 * Returns 0 on success, or -1 on error.
2202 */
2203 int
2204 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2205 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2206 {
2207 char *source = NULL;
2208 uint64_t val;
2209 char *str;
2210 const char *strval;
2211 boolean_t received = zfs_is_recvd_props_mode(zhp);
2212
2213 /*
2214 * Check to see if this property applies to our object
2215 */
2216 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2217 return (-1);
2218
2219 if (received && zfs_prop_readonly(prop))
2220 return (-1);
2221
2222 if (src)
2223 *src = ZPROP_SRC_NONE;
2224
2225 switch (prop) {
2226 case ZFS_PROP_CREATION:
2227 /*
2228 * 'creation' is a time_t stored in the statistics. We convert
2229 * this into a string unless 'literal' is specified.
2230 */
2231 {
2232 val = getprop_uint64(zhp, prop, &source);
2233 time_t time = (time_t)val;
2234 struct tm t;
2235
2236 if (literal ||
2237 localtime_r(&time, &t) == NULL ||
2238 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2239 &t) == 0)
2240 (void) snprintf(propbuf, proplen, "%llu",
2241 (u_longlong_t) val);
2242 }
2243 break;
2244
2245 case ZFS_PROP_MOUNTPOINT:
2246 /*
2247 * Getting the precise mountpoint can be tricky.
2248 *
2249 * - for 'none' or 'legacy', return those values.
2250 * - for inherited mountpoints, we want to take everything
2251 * after our ancestor and append it to the inherited value.
2252 *
2253 * If the pool has an alternate root, we want to prepend that
2254 * root to any values we return.
2255 */
2256
2257 str = getprop_string(zhp, prop, &source);
2258
2259 if (str[0] == '/') {
2260 char buf[MAXPATHLEN];
2261 char *root = buf;
2262 const char *relpath;
2263
2264 /*
2265 * If we inherit the mountpoint, even from a dataset
2266 * with a received value, the source will be the path of
2267 * the dataset we inherit from. If source is
2268 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2269 * inherited.
2270 */
2271 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2272 relpath = "";
2273 } else {
2274 relpath = zhp->zfs_name + strlen(source);
2275 if (relpath[0] == '/')
2276 relpath++;
2277 }
2278
2279 if ((zpool_get_prop(zhp->zpool_hdl,
2280 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2281 (strcmp(root, "-") == 0))
2282 root[0] = '\0';
2283 /*
2284 * Special case an alternate root of '/'. This will
2285 * avoid having multiple leading slashes in the
2286 * mountpoint path.
2287 */
2288 if (strcmp(root, "/") == 0)
2289 root++;
2290
2291 /*
2292 * If the mountpoint is '/' then skip over this
2293 * if we are obtaining either an alternate root or
2294 * an inherited mountpoint.
2295 */
2296 if (str[1] == '\0' && (root[0] != '\0' ||
2297 relpath[0] != '\0'))
2298 str++;
2299
2300 if (relpath[0] == '\0')
2301 (void) snprintf(propbuf, proplen, "%s%s",
2302 root, str);
2303 else
2304 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2305 root, str, relpath[0] == '@' ? "" : "/",
2306 relpath);
2307 } else {
2308 /* 'legacy' or 'none' */
2309 (void) strlcpy(propbuf, str, proplen);
2310 }
2311
2312 break;
2313
2314 case ZFS_PROP_ORIGIN:
2315 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2316 proplen);
2317 /*
2318 * If there is no parent at all, return failure to indicate that
2319 * it doesn't apply to this dataset.
2320 */
2321 if (propbuf[0] == '\0')
2322 return (-1);
2323 break;
2324
2325 case ZFS_PROP_CLONES:
2326 if (get_clones_string(zhp, propbuf, proplen) != 0)
2327 return (-1);
2328 break;
2329
2330 case ZFS_PROP_QUOTA:
2331 case ZFS_PROP_REFQUOTA:
2332 case ZFS_PROP_RESERVATION:
2333 case ZFS_PROP_REFRESERVATION:
2334
2335 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2336 return (-1);
2337
2338 /*
2339 * If quota or reservation is 0, we translate this into 'none'
2340 * (unless literal is set), and indicate that it's the default
2341 * value. Otherwise, we print the number nicely and indicate
2342 * that its set locally.
2343 */
2344 if (val == 0) {
2345 if (literal)
2346 (void) strlcpy(propbuf, "0", proplen);
2347 else
2348 (void) strlcpy(propbuf, "none", proplen);
2349 } else {
2350 if (literal)
2351 (void) snprintf(propbuf, proplen, "%llu",
2352 (u_longlong_t)val);
2353 else
2354 zfs_nicenum(val, propbuf, proplen);
2355 }
2356 break;
2357
2358 case ZFS_PROP_FILESYSTEM_LIMIT:
2359 case ZFS_PROP_SNAPSHOT_LIMIT:
2360 case ZFS_PROP_FILESYSTEM_COUNT:
2361 case ZFS_PROP_SNAPSHOT_COUNT:
2362
2363 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2364 return (-1);
2365
2366 /*
2367 * If limit is UINT64_MAX, we translate this into 'none' (unless
2368 * literal is set), and indicate that it's the default value.
2369 * Otherwise, we print the number nicely and indicate that it's
2370 * set locally.
2371 */
2372 if (literal) {
2373 (void) snprintf(propbuf, proplen, "%llu",
2374 (u_longlong_t)val);
2375 } else if (val == UINT64_MAX) {
2376 (void) strlcpy(propbuf, "none", proplen);
2377 } else {
2378 zfs_nicenum(val, propbuf, proplen);
2379 }
2380 break;
2381
2382 case ZFS_PROP_REFRATIO:
2383 case ZFS_PROP_COMPRESSRATIO:
2384 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2385 return (-1);
2386 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2387 (u_longlong_t)(val / 100),
2388 (u_longlong_t)(val % 100));
2389 break;
2390
2391 case ZFS_PROP_TYPE:
2392 switch (zhp->zfs_type) {
2393 case ZFS_TYPE_FILESYSTEM:
2394 str = "filesystem";
2395 break;
2396 case ZFS_TYPE_VOLUME:
2397 str = "volume";
2398 break;
2399 case ZFS_TYPE_SNAPSHOT:
2400 str = "snapshot";
2401 break;
2402 case ZFS_TYPE_BOOKMARK:
2403 str = "bookmark";
2404 break;
2405 default:
2406 abort();
2407 }
2408 (void) snprintf(propbuf, proplen, "%s", str);
2409 break;
2410
2411 case ZFS_PROP_MOUNTED:
2412 /*
2413 * The 'mounted' property is a pseudo-property that described
2414 * whether the filesystem is currently mounted. Even though
2415 * it's a boolean value, the typical values of "on" and "off"
2416 * don't make sense, so we translate to "yes" and "no".
2417 */
2418 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2419 src, &source, &val) != 0)
2420 return (-1);
2421 if (val)
2422 (void) strlcpy(propbuf, "yes", proplen);
2423 else
2424 (void) strlcpy(propbuf, "no", proplen);
2425 break;
2426
2427 case ZFS_PROP_NAME:
2428 /*
2429 * The 'name' property is a pseudo-property derived from the
2430 * dataset name. It is presented as a real property to simplify
2431 * consumers.
2432 */
2433 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2434 break;
2435
2436 case ZFS_PROP_MLSLABEL:
2437 {
2438 #ifdef HAVE_MLSLABEL
2439 m_label_t *new_sl = NULL;
2440 char *ascii = NULL; /* human readable label */
2441
2442 (void) strlcpy(propbuf,
2443 getprop_string(zhp, prop, &source), proplen);
2444
2445 if (literal || (strcasecmp(propbuf,
2446 ZFS_MLSLABEL_DEFAULT) == 0))
2447 break;
2448
2449 /*
2450 * Try to translate the internal hex string to
2451 * human-readable output. If there are any
2452 * problems just use the hex string.
2453 */
2454
2455 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2456 L_NO_CORRECTION, NULL) == -1) {
2457 m_label_free(new_sl);
2458 break;
2459 }
2460
2461 if (label_to_str(new_sl, &ascii, M_LABEL,
2462 DEF_NAMES) != 0) {
2463 if (ascii)
2464 free(ascii);
2465 m_label_free(new_sl);
2466 break;
2467 }
2468 m_label_free(new_sl);
2469
2470 (void) strlcpy(propbuf, ascii, proplen);
2471 free(ascii);
2472 #else
2473 (void) strlcpy(propbuf,
2474 getprop_string(zhp, prop, &source), proplen);
2475 #endif /* HAVE_MLSLABEL */
2476 }
2477 break;
2478
2479 case ZFS_PROP_GUID:
2480 /*
2481 * GUIDs are stored as numbers, but they are identifiers.
2482 * We don't want them to be pretty printed, because pretty
2483 * printing mangles the ID into a truncated and useless value.
2484 */
2485 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2486 return (-1);
2487 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2488 break;
2489
2490 default:
2491 switch (zfs_prop_get_type(prop)) {
2492 case PROP_TYPE_NUMBER:
2493 if (get_numeric_property(zhp, prop, src,
2494 &source, &val) != 0)
2495 return (-1);
2496 if (literal)
2497 (void) snprintf(propbuf, proplen, "%llu",
2498 (u_longlong_t)val);
2499 else
2500 zfs_nicenum(val, propbuf, proplen);
2501 break;
2502
2503 case PROP_TYPE_STRING:
2504 (void) strlcpy(propbuf,
2505 getprop_string(zhp, prop, &source), proplen);
2506 break;
2507
2508 case PROP_TYPE_INDEX:
2509 if (get_numeric_property(zhp, prop, src,
2510 &source, &val) != 0)
2511 return (-1);
2512 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2513 return (-1);
2514 (void) strlcpy(propbuf, strval, proplen);
2515 break;
2516
2517 default:
2518 abort();
2519 }
2520 }
2521
2522 get_source(zhp, src, source, statbuf, statlen);
2523
2524 return (0);
2525 }
2526
2527 /*
2528 * Utility function to get the given numeric property. Does no validation that
2529 * the given property is the appropriate type; should only be used with
2530 * hard-coded property types.
2531 */
2532 uint64_t
2533 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2534 {
2535 char *source;
2536 uint64_t val;
2537
2538 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2539
2540 return (val);
2541 }
2542
2543 int
2544 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2545 {
2546 char buf[64];
2547
2548 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2549 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2550 }
2551
2552 /*
2553 * Similar to zfs_prop_get(), but returns the value as an integer.
2554 */
2555 int
2556 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2557 zprop_source_t *src, char *statbuf, size_t statlen)
2558 {
2559 char *source;
2560
2561 /*
2562 * Check to see if this property applies to our object
2563 */
2564 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
2565 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2566 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2567 zfs_prop_to_name(prop)));
2568 }
2569
2570 if (src)
2571 *src = ZPROP_SRC_NONE;
2572
2573 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2574 return (-1);
2575
2576 get_source(zhp, src, source, statbuf, statlen);
2577
2578 return (0);
2579 }
2580
2581 #ifdef HAVE_IDMAP
2582 static int
2583 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2584 char **domainp, idmap_rid_t *ridp)
2585 {
2586 idmap_get_handle_t *get_hdl = NULL;
2587 idmap_stat status;
2588 int err = EINVAL;
2589
2590 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2591 goto out;
2592
2593 if (isuser) {
2594 err = idmap_get_sidbyuid(get_hdl, id,
2595 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2596 } else {
2597 err = idmap_get_sidbygid(get_hdl, id,
2598 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2599 }
2600 if (err == IDMAP_SUCCESS &&
2601 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2602 status == IDMAP_SUCCESS)
2603 err = 0;
2604 else
2605 err = EINVAL;
2606 out:
2607 if (get_hdl)
2608 idmap_get_destroy(get_hdl);
2609 return (err);
2610 }
2611 #endif /* HAVE_IDMAP */
2612
2613 /*
2614 * convert the propname into parameters needed by kernel
2615 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2616 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2617 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2618 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2619 */
2620 static int
2621 userquota_propname_decode(const char *propname, boolean_t zoned,
2622 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2623 {
2624 zfs_userquota_prop_t type;
2625 char *cp;
2626 boolean_t isuser;
2627 boolean_t isgroup;
2628 struct passwd *pw;
2629 struct group *gr;
2630
2631 domain[0] = '\0';
2632
2633 /* Figure out the property type ({user|group}{quota|space}) */
2634 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2635 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2636 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2637 break;
2638 }
2639 if (type == ZFS_NUM_USERQUOTA_PROPS)
2640 return (EINVAL);
2641 *typep = type;
2642
2643 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2644 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2645
2646 cp = strchr(propname, '@') + 1;
2647
2648 if (isuser && (pw = getpwnam(cp)) != NULL) {
2649 if (zoned && getzoneid() == GLOBAL_ZONEID)
2650 return (ENOENT);
2651 *ridp = pw->pw_uid;
2652 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2653 if (zoned && getzoneid() == GLOBAL_ZONEID)
2654 return (ENOENT);
2655 *ridp = gr->gr_gid;
2656 } else if (strchr(cp, '@')) {
2657 #ifdef HAVE_IDMAP
2658 /*
2659 * It's a SID name (eg "user@domain") that needs to be
2660 * turned into S-1-domainID-RID.
2661 */
2662 directory_error_t e;
2663 char *numericsid = NULL;
2664 char *end;
2665
2666 if (zoned && getzoneid() == GLOBAL_ZONEID)
2667 return (ENOENT);
2668 if (isuser) {
2669 e = directory_sid_from_user_name(NULL,
2670 cp, &numericsid);
2671 } else {
2672 e = directory_sid_from_group_name(NULL,
2673 cp, &numericsid);
2674 }
2675 if (e != NULL) {
2676 directory_error_free(e);
2677 return (ENOENT);
2678 }
2679 if (numericsid == NULL)
2680 return (ENOENT);
2681 cp = numericsid;
2682 (void) strlcpy(domain, cp, domainlen);
2683 cp = strrchr(domain, '-');
2684 *cp = '\0';
2685 cp++;
2686
2687 errno = 0;
2688 *ridp = strtoull(cp, &end, 10);
2689 free(numericsid);
2690
2691 if (errno != 0 || *end != '\0')
2692 return (EINVAL);
2693 #else
2694 return (ENOSYS);
2695 #endif /* HAVE_IDMAP */
2696 } else {
2697 /* It's a user/group ID (eg "12345"). */
2698 uid_t id;
2699 char *end;
2700 id = strtoul(cp, &end, 10);
2701 if (*end != '\0')
2702 return (EINVAL);
2703 if (id > MAXUID) {
2704 #ifdef HAVE_IDMAP
2705 /* It's an ephemeral ID. */
2706 idmap_rid_t rid;
2707 char *mapdomain;
2708
2709 if (idmap_id_to_numeric_domain_rid(id, isuser,
2710 &mapdomain, &rid) != 0)
2711 return (ENOENT);
2712 (void) strlcpy(domain, mapdomain, domainlen);
2713 *ridp = rid;
2714 #else
2715 return (ENOSYS);
2716 #endif /* HAVE_IDMAP */
2717 } else {
2718 *ridp = id;
2719 }
2720 }
2721
2722 return (0);
2723 }
2724
2725 static int
2726 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2727 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2728 {
2729 int err;
2730 zfs_cmd_t zc = {"\0"};
2731
2732 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2733
2734 err = userquota_propname_decode(propname,
2735 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2736 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2737 zc.zc_objset_type = *typep;
2738 if (err)
2739 return (err);
2740
2741 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2742 if (err)
2743 return (err);
2744
2745 *propvalue = zc.zc_cookie;
2746 return (0);
2747 }
2748
2749 int
2750 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2751 uint64_t *propvalue)
2752 {
2753 zfs_userquota_prop_t type;
2754
2755 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2756 &type));
2757 }
2758
2759 int
2760 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2761 char *propbuf, int proplen, boolean_t literal)
2762 {
2763 int err;
2764 uint64_t propvalue;
2765 zfs_userquota_prop_t type;
2766
2767 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2768 &type);
2769
2770 if (err)
2771 return (err);
2772
2773 if (literal) {
2774 (void) snprintf(propbuf, proplen, "%llu",
2775 (u_longlong_t)propvalue);
2776 } else if (propvalue == 0 &&
2777 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2778 (void) strlcpy(propbuf, "none", proplen);
2779 } else {
2780 zfs_nicenum(propvalue, propbuf, proplen);
2781 }
2782 return (0);
2783 }
2784
2785 int
2786 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2787 uint64_t *propvalue)
2788 {
2789 int err;
2790 zfs_cmd_t zc = {"\0"};
2791 const char *snapname;
2792
2793 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2794
2795 snapname = strchr(propname, '@') + 1;
2796 if (strchr(snapname, '@')) {
2797 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2798 } else {
2799 /* snapname is the short name, append it to zhp's fsname */
2800 char *cp;
2801
2802 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2803 sizeof (zc.zc_value));
2804 cp = strchr(zc.zc_value, '@');
2805 if (cp != NULL)
2806 *cp = '\0';
2807 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2808 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2809 }
2810
2811 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2812 if (err)
2813 return (err);
2814
2815 *propvalue = zc.zc_cookie;
2816 return (0);
2817 }
2818
2819 int
2820 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2821 char *propbuf, int proplen, boolean_t literal)
2822 {
2823 int err;
2824 uint64_t propvalue;
2825
2826 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2827
2828 if (err)
2829 return (err);
2830
2831 if (literal) {
2832 (void) snprintf(propbuf, proplen, "%llu",
2833 (u_longlong_t)propvalue);
2834 } else {
2835 zfs_nicenum(propvalue, propbuf, proplen);
2836 }
2837
2838 return (0);
2839 }
2840
2841 /*
2842 * Returns the name of the given zfs handle.
2843 */
2844 const char *
2845 zfs_get_name(const zfs_handle_t *zhp)
2846 {
2847 return (zhp->zfs_name);
2848 }
2849
2850 /*
2851 * Returns the type of the given zfs handle.
2852 */
2853 zfs_type_t
2854 zfs_get_type(const zfs_handle_t *zhp)
2855 {
2856 return (zhp->zfs_type);
2857 }
2858
2859 /*
2860 * Is one dataset name a child dataset of another?
2861 *
2862 * Needs to handle these cases:
2863 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2864 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2865 * Descendant? No. No. No. Yes.
2866 */
2867 static boolean_t
2868 is_descendant(const char *ds1, const char *ds2)
2869 {
2870 size_t d1len = strlen(ds1);
2871
2872 /* ds2 can't be a descendant if it's smaller */
2873 if (strlen(ds2) < d1len)
2874 return (B_FALSE);
2875
2876 /* otherwise, compare strings and verify that there's a '/' char */
2877 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2878 }
2879
2880 /*
2881 * Given a complete name, return just the portion that refers to the parent.
2882 * Will return -1 if there is no parent (path is just the name of the
2883 * pool).
2884 */
2885 static int
2886 parent_name(const char *path, char *buf, size_t buflen)
2887 {
2888 char *slashp;
2889
2890 (void) strlcpy(buf, path, buflen);
2891
2892 if ((slashp = strrchr(buf, '/')) == NULL)
2893 return (-1);
2894 *slashp = '\0';
2895
2896 return (0);
2897 }
2898
2899 /*
2900 * If accept_ancestor is false, then check to make sure that the given path has
2901 * a parent, and that it exists. If accept_ancestor is true, then find the
2902 * closest existing ancestor for the given path. In prefixlen return the
2903 * length of already existing prefix of the given path. We also fetch the
2904 * 'zoned' property, which is used to validate property settings when creating
2905 * new datasets.
2906 */
2907 static int
2908 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2909 boolean_t accept_ancestor, int *prefixlen)
2910 {
2911 zfs_cmd_t zc = {"\0"};
2912 char parent[ZFS_MAXNAMELEN];
2913 char *slash;
2914 zfs_handle_t *zhp;
2915 char errbuf[1024];
2916 uint64_t is_zoned;
2917
2918 (void) snprintf(errbuf, sizeof (errbuf),
2919 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2920
2921 /* get parent, and check to see if this is just a pool */
2922 if (parent_name(path, parent, sizeof (parent)) != 0) {
2923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2924 "missing dataset name"));
2925 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2926 }
2927
2928 /* check to see if the pool exists */
2929 if ((slash = strchr(parent, '/')) == NULL)
2930 slash = parent + strlen(parent);
2931 (void) strncpy(zc.zc_name, parent, slash - parent);
2932 zc.zc_name[slash - parent] = '\0';
2933 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2934 errno == ENOENT) {
2935 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2936 "no such pool '%s'"), zc.zc_name);
2937 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2938 }
2939
2940 /* check to see if the parent dataset exists */
2941 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2942 if (errno == ENOENT && accept_ancestor) {
2943 /*
2944 * Go deeper to find an ancestor, give up on top level.
2945 */
2946 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2948 "no such pool '%s'"), zc.zc_name);
2949 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2950 }
2951 } else if (errno == ENOENT) {
2952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2953 "parent does not exist"));
2954 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2955 } else
2956 return (zfs_standard_error(hdl, errno, errbuf));
2957 }
2958
2959 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2960 if (zoned != NULL)
2961 *zoned = is_zoned;
2962
2963 /* we are in a non-global zone, but parent is in the global zone */
2964 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2965 (void) zfs_standard_error(hdl, EPERM, errbuf);
2966 zfs_close(zhp);
2967 return (-1);
2968 }
2969
2970 /* make sure parent is a filesystem */
2971 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2972 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2973 "parent is not a filesystem"));
2974 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2975 zfs_close(zhp);
2976 return (-1);
2977 }
2978
2979 zfs_close(zhp);
2980 if (prefixlen != NULL)
2981 *prefixlen = strlen(parent);
2982 return (0);
2983 }
2984
2985 /*
2986 * Finds whether the dataset of the given type(s) exists.
2987 */
2988 boolean_t
2989 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2990 {
2991 zfs_handle_t *zhp;
2992
2993 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2994 return (B_FALSE);
2995
2996 /*
2997 * Try to get stats for the dataset, which will tell us if it exists.
2998 */
2999 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3000 int ds_type = zhp->zfs_type;
3001
3002 zfs_close(zhp);
3003 if (types & ds_type)
3004 return (B_TRUE);
3005 }
3006 return (B_FALSE);
3007 }
3008
3009 /*
3010 * Given a path to 'target', create all the ancestors between
3011 * the prefixlen portion of the path, and the target itself.
3012 * Fail if the initial prefixlen-ancestor does not already exist.
3013 */
3014 int
3015 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3016 {
3017 zfs_handle_t *h;
3018 char *cp;
3019 const char *opname;
3020
3021 /* make sure prefix exists */
3022 cp = target + prefixlen;
3023 if (*cp != '/') {
3024 assert(strchr(cp, '/') == NULL);
3025 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3026 } else {
3027 *cp = '\0';
3028 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3029 *cp = '/';
3030 }
3031 if (h == NULL)
3032 return (-1);
3033 zfs_close(h);
3034
3035 /*
3036 * Attempt to create, mount, and share any ancestor filesystems,
3037 * up to the prefixlen-long one.
3038 */
3039 for (cp = target + prefixlen + 1;
3040 (cp = strchr(cp, '/')); *cp = '/', cp++) {
3041
3042 *cp = '\0';
3043
3044 h = make_dataset_handle(hdl, target);
3045 if (h) {
3046 /* it already exists, nothing to do here */
3047 zfs_close(h);
3048 continue;
3049 }
3050
3051 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3052 NULL) != 0) {
3053 opname = dgettext(TEXT_DOMAIN, "create");
3054 goto ancestorerr;
3055 }
3056
3057 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3058 if (h == NULL) {
3059 opname = dgettext(TEXT_DOMAIN, "open");
3060 goto ancestorerr;
3061 }
3062
3063 if (zfs_mount(h, NULL, 0) != 0) {
3064 opname = dgettext(TEXT_DOMAIN, "mount");
3065 goto ancestorerr;
3066 }
3067
3068 if (zfs_share(h) != 0) {
3069 opname = dgettext(TEXT_DOMAIN, "share");
3070 goto ancestorerr;
3071 }
3072
3073 zfs_close(h);
3074 }
3075
3076 return (0);
3077
3078 ancestorerr:
3079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3080 "failed to %s ancestor '%s'"), opname, target);
3081 return (-1);
3082 }
3083
3084 /*
3085 * Creates non-existing ancestors of the given path.
3086 */
3087 int
3088 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3089 {
3090 int prefix;
3091 char *path_copy;
3092 int rc = 0;
3093
3094 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3095 return (-1);
3096
3097 if ((path_copy = strdup(path)) != NULL) {
3098 rc = create_parents(hdl, path_copy, prefix);
3099 free(path_copy);
3100 }
3101 if (path_copy == NULL || rc != 0)
3102 return (-1);
3103
3104 return (0);
3105 }
3106
3107 /*
3108 * Create a new filesystem or volume.
3109 */
3110 int
3111 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3112 nvlist_t *props)
3113 {
3114 int ret;
3115 uint64_t size = 0;
3116 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3117 char errbuf[1024];
3118 uint64_t zoned;
3119 dmu_objset_type_t ost;
3120
3121 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3122 "cannot create '%s'"), path);
3123
3124 /* validate the path, taking care to note the extended error message */
3125 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3126 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3127
3128 /* validate parents exist */
3129 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3130 return (-1);
3131
3132 /*
3133 * The failure modes when creating a dataset of a different type over
3134 * one that already exists is a little strange. In particular, if you
3135 * try to create a dataset on top of an existing dataset, the ioctl()
3136 * will return ENOENT, not EEXIST. To prevent this from happening, we
3137 * first try to see if the dataset exists.
3138 */
3139 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3141 "dataset already exists"));
3142 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3143 }
3144
3145 if (type == ZFS_TYPE_VOLUME)
3146 ost = DMU_OST_ZVOL;
3147 else
3148 ost = DMU_OST_ZFS;
3149
3150 if (props && (props = zfs_valid_proplist(hdl, type, props,
3151 zoned, NULL, errbuf)) == 0)
3152 return (-1);
3153
3154 if (type == ZFS_TYPE_VOLUME) {
3155 /*
3156 * If we are creating a volume, the size and block size must
3157 * satisfy a few restraints. First, the blocksize must be a
3158 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3159 * volsize must be a multiple of the block size, and cannot be
3160 * zero.
3161 */
3162 if (props == NULL || nvlist_lookup_uint64(props,
3163 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3164 nvlist_free(props);
3165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3166 "missing volume size"));
3167 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3168 }
3169
3170 if ((ret = nvlist_lookup_uint64(props,
3171 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3172 &blocksize)) != 0) {
3173 if (ret == ENOENT) {
3174 blocksize = zfs_prop_default_numeric(
3175 ZFS_PROP_VOLBLOCKSIZE);
3176 } else {
3177 nvlist_free(props);
3178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3179 "missing volume block size"));
3180 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3181 }
3182 }
3183
3184 if (size == 0) {
3185 nvlist_free(props);
3186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3187 "volume size cannot be zero"));
3188 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3189 }
3190
3191 if (size % blocksize != 0) {
3192 nvlist_free(props);
3193 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3194 "volume size must be a multiple of volume block "
3195 "size"));
3196 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3197 }
3198 }
3199
3200 /* create the dataset */
3201 ret = lzc_create(path, ost, props);
3202 nvlist_free(props);
3203
3204 /* check for failure */
3205 if (ret != 0) {
3206 char parent[ZFS_MAXNAMELEN];
3207 (void) parent_name(path, parent, sizeof (parent));
3208
3209 switch (errno) {
3210 case ENOENT:
3211 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3212 "no such parent '%s'"), parent);
3213 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3214
3215 case EINVAL:
3216 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3217 "parent '%s' is not a filesystem"), parent);
3218 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3219
3220 case EDOM:
3221 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3222 "volume block size must be power of 2 from "
3223 "512B to %uKB"), zfs_max_recordsize >> 10);
3224
3225 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3226
3227 case ENOTSUP:
3228 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3229 "pool must be upgraded to set this "
3230 "property or value"));
3231 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3232 #ifdef _ILP32
3233 case EOVERFLOW:
3234 /*
3235 * This platform can't address a volume this big.
3236 */
3237 if (type == ZFS_TYPE_VOLUME)
3238 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3239 errbuf));
3240 #endif
3241 /* FALLTHROUGH */
3242 default:
3243 return (zfs_standard_error(hdl, errno, errbuf));
3244 }
3245 }
3246
3247 return (0);
3248 }
3249
3250 /*
3251 * Destroys the given dataset. The caller must make sure that the filesystem
3252 * isn't mounted, and that there are no active dependents. If the file system
3253 * does not exist this function does nothing.
3254 */
3255 int
3256 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3257 {
3258 zfs_cmd_t zc = {"\0"};
3259
3260 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3261 nvlist_t *nv = fnvlist_alloc();
3262 fnvlist_add_boolean(nv, zhp->zfs_name);
3263 int error = lzc_destroy_bookmarks(nv, NULL);
3264 fnvlist_free(nv);
3265 if (error != 0) {
3266 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3267 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3268 zhp->zfs_name));
3269 }
3270 return (0);
3271 }
3272
3273 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3274
3275 if (ZFS_IS_VOLUME(zhp)) {
3276 zc.zc_objset_type = DMU_OST_ZVOL;
3277 } else {
3278 zc.zc_objset_type = DMU_OST_ZFS;
3279 }
3280
3281 zc.zc_defer_destroy = defer;
3282 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3283 errno != ENOENT) {
3284 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3285 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3286 zhp->zfs_name));
3287 }
3288
3289 remove_mountpoint(zhp);
3290
3291 return (0);
3292 }
3293
3294 struct destroydata {
3295 nvlist_t *nvl;
3296 const char *snapname;
3297 };
3298
3299 static int
3300 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3301 {
3302 struct destroydata *dd = arg;
3303 char name[ZFS_MAXNAMELEN];
3304 int rv = 0;
3305
3306 (void) snprintf(name, sizeof (name),
3307 "%s@%s", zhp->zfs_name, dd->snapname);
3308
3309 if (lzc_exists(name))
3310 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3311
3312 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3313 zfs_close(zhp);
3314 return (rv);
3315 }
3316
3317 /*
3318 * Destroys all snapshots with the given name in zhp & descendants.
3319 */
3320 int
3321 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3322 {
3323 int ret;
3324 struct destroydata dd = { 0 };
3325
3326 dd.snapname = snapname;
3327 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3328 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3329
3330 if (nvlist_empty(dd.nvl)) {
3331 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3332 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3333 zhp->zfs_name, snapname);
3334 } else {
3335 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3336 }
3337 nvlist_free(dd.nvl);
3338 return (ret);
3339 }
3340
3341 /*
3342 * Destroys all the snapshots named in the nvlist.
3343 */
3344 int
3345 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3346 {
3347 int ret;
3348 nvlist_t *errlist;
3349 nvpair_t *pair;
3350
3351 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3352
3353 if (ret == 0)
3354 return (0);
3355
3356 if (nvlist_empty(errlist)) {
3357 char errbuf[1024];
3358 (void) snprintf(errbuf, sizeof (errbuf),
3359 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3360
3361 ret = zfs_standard_error(hdl, ret, errbuf);
3362 }
3363 for (pair = nvlist_next_nvpair(errlist, NULL);
3364 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3365 char errbuf[1024];
3366 (void) snprintf(errbuf, sizeof (errbuf),
3367 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3368 nvpair_name(pair));
3369
3370 switch (fnvpair_value_int32(pair)) {
3371 case EEXIST:
3372 zfs_error_aux(hdl,
3373 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3374 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3375 break;
3376 default:
3377 ret = zfs_standard_error(hdl, errno, errbuf);
3378 break;
3379 }
3380 }
3381
3382 return (ret);
3383 }
3384
3385 /*
3386 * Clones the given dataset. The target must be of the same type as the source.
3387 */
3388 int
3389 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3390 {
3391 char parent[ZFS_MAXNAMELEN];
3392 int ret;
3393 char errbuf[1024];
3394 libzfs_handle_t *hdl = zhp->zfs_hdl;
3395 uint64_t zoned;
3396
3397 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3398
3399 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3400 "cannot create '%s'"), target);
3401
3402 /* validate the target/clone name */
3403 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3404 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3405
3406 /* validate parents exist */
3407 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3408 return (-1);
3409
3410 (void) parent_name(target, parent, sizeof (parent));
3411
3412 /* do the clone */
3413
3414 if (props) {
3415 zfs_type_t type;
3416 if (ZFS_IS_VOLUME(zhp)) {
3417 type = ZFS_TYPE_VOLUME;
3418 } else {
3419 type = ZFS_TYPE_FILESYSTEM;
3420 }
3421 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3422 zhp, errbuf)) == NULL)
3423 return (-1);
3424 }
3425
3426 ret = lzc_clone(target, zhp->zfs_name, props);
3427 nvlist_free(props);
3428
3429 if (ret != 0) {
3430 switch (errno) {
3431
3432 case ENOENT:
3433 /*
3434 * The parent doesn't exist. We should have caught this
3435 * above, but there may a race condition that has since
3436 * destroyed the parent.
3437 *
3438 * At this point, we don't know whether it's the source
3439 * that doesn't exist anymore, or whether the target
3440 * dataset doesn't exist.
3441 */
3442 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3443 "no such parent '%s'"), parent);
3444 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3445
3446 case EXDEV:
3447 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3448 "source and target pools differ"));
3449 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3450 errbuf));
3451
3452 default:
3453 return (zfs_standard_error(zhp->zfs_hdl, errno,
3454 errbuf));
3455 }
3456 }
3457
3458 return (ret);
3459 }
3460
3461 /*
3462 * Promotes the given clone fs to be the clone parent.
3463 */
3464 int
3465 zfs_promote(zfs_handle_t *zhp)
3466 {
3467 libzfs_handle_t *hdl = zhp->zfs_hdl;
3468 zfs_cmd_t zc = {"\0"};
3469 char parent[MAXPATHLEN];
3470 int ret;
3471 char errbuf[1024];
3472
3473 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3474 "cannot promote '%s'"), zhp->zfs_name);
3475
3476 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3478 "snapshots can not be promoted"));
3479 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3480 }
3481
3482 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3483 if (parent[0] == '\0') {
3484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3485 "not a cloned filesystem"));
3486 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3487 }
3488
3489 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3490 sizeof (zc.zc_value));
3491 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3492 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3493
3494 if (ret != 0) {
3495 int save_errno = errno;
3496
3497 switch (save_errno) {
3498 case EEXIST:
3499 /* There is a conflicting snapshot name. */
3500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3501 "conflicting snapshot '%s' from parent '%s'"),
3502 zc.zc_string, parent);
3503 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3504
3505 default:
3506 return (zfs_standard_error(hdl, save_errno, errbuf));
3507 }
3508 }
3509 return (ret);
3510 }
3511
3512 typedef struct snapdata {
3513 nvlist_t *sd_nvl;
3514 const char *sd_snapname;
3515 } snapdata_t;
3516
3517 static int
3518 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3519 {
3520 snapdata_t *sd = arg;
3521 char name[ZFS_MAXNAMELEN];
3522 int rv = 0;
3523
3524 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3525 (void) snprintf(name, sizeof (name),
3526 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3527
3528 fnvlist_add_boolean(sd->sd_nvl, name);
3529
3530 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3531 }
3532 zfs_close(zhp);
3533
3534 return (rv);
3535 }
3536
3537 /*
3538 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3539 * created.
3540 */
3541 int
3542 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3543 {
3544 int ret;
3545 char errbuf[1024];
3546 nvpair_t *elem;
3547 nvlist_t *errors;
3548
3549 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3550 "cannot create snapshots "));
3551
3552 elem = NULL;
3553 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3554 const char *snapname = nvpair_name(elem);
3555
3556 /* validate the target name */
3557 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3558 B_TRUE)) {
3559 (void) snprintf(errbuf, sizeof (errbuf),
3560 dgettext(TEXT_DOMAIN,
3561 "cannot create snapshot '%s'"), snapname);
3562 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3563 }
3564 }
3565
3566 if (props != NULL &&
3567 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3568 props, B_FALSE, NULL, errbuf)) == NULL) {
3569 return (-1);
3570 }
3571
3572 ret = lzc_snapshot(snaps, props, &errors);
3573
3574 if (ret != 0) {
3575 boolean_t printed = B_FALSE;
3576 for (elem = nvlist_next_nvpair(errors, NULL);
3577 elem != NULL;
3578 elem = nvlist_next_nvpair(errors, elem)) {
3579 (void) snprintf(errbuf, sizeof (errbuf),
3580 dgettext(TEXT_DOMAIN,
3581 "cannot create snapshot '%s'"), nvpair_name(elem));
3582 (void) zfs_standard_error(hdl,
3583 fnvpair_value_int32(elem), errbuf);
3584 printed = B_TRUE;
3585 }
3586 if (!printed) {
3587 switch (ret) {
3588 case EXDEV:
3589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3590 "multiple snapshots of same "
3591 "fs not allowed"));
3592 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3593
3594 break;
3595 default:
3596 (void) zfs_standard_error(hdl, ret, errbuf);
3597 }
3598 }
3599 }
3600
3601 nvlist_free(props);
3602 nvlist_free(errors);
3603 return (ret);
3604 }
3605
3606 int
3607 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3608 nvlist_t *props)
3609 {
3610 int ret;
3611 snapdata_t sd = { 0 };
3612 char fsname[ZFS_MAXNAMELEN];
3613 char *cp;
3614 zfs_handle_t *zhp;
3615 char errbuf[1024];
3616
3617 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3618 "cannot snapshot %s"), path);
3619
3620 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3621 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3622
3623 (void) strlcpy(fsname, path, sizeof (fsname));
3624 cp = strchr(fsname, '@');
3625 *cp = '\0';
3626 sd.sd_snapname = cp + 1;
3627
3628 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3629 ZFS_TYPE_VOLUME)) == NULL) {
3630 return (-1);
3631 }
3632
3633 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3634 if (recursive) {
3635 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3636 } else {
3637 fnvlist_add_boolean(sd.sd_nvl, path);
3638 }
3639
3640 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3641 nvlist_free(sd.sd_nvl);
3642 zfs_close(zhp);
3643 return (ret);
3644 }
3645
3646 /*
3647 * Destroy any more recent snapshots. We invoke this callback on any dependents
3648 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3649 * is a dependent and we should just destroy it without checking the transaction
3650 * group.
3651 */
3652 typedef struct rollback_data {
3653 const char *cb_target; /* the snapshot */
3654 uint64_t cb_create; /* creation time reference */
3655 boolean_t cb_error;
3656 boolean_t cb_force;
3657 } rollback_data_t;
3658
3659 static int
3660 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3661 {
3662 rollback_data_t *cbp = data;
3663 prop_changelist_t *clp;
3664
3665 /* We must destroy this clone; first unmount it */
3666 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3667 cbp->cb_force ? MS_FORCE: 0);
3668 if (clp == NULL || changelist_prefix(clp) != 0) {
3669 cbp->cb_error = B_TRUE;
3670 zfs_close(zhp);
3671 return (0);
3672 }
3673 if (zfs_destroy(zhp, B_FALSE) != 0)
3674 cbp->cb_error = B_TRUE;
3675 else
3676 changelist_remove(clp, zhp->zfs_name);
3677 (void) changelist_postfix(clp);
3678 changelist_free(clp);
3679
3680 zfs_close(zhp);
3681 return (0);
3682 }
3683
3684 static int
3685 rollback_destroy(zfs_handle_t *zhp, void *data)
3686 {
3687 rollback_data_t *cbp = data;
3688
3689 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3690 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3691 rollback_destroy_dependent, cbp);
3692
3693 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3694 }
3695
3696 zfs_close(zhp);
3697 return (0);
3698 }
3699
3700 /*
3701 * Given a dataset, rollback to a specific snapshot, discarding any
3702 * data changes since then and making it the active dataset.
3703 *
3704 * Any snapshots and bookmarks more recent than the target are
3705 * destroyed, along with their dependents (i.e. clones).
3706 */
3707 int
3708 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3709 {
3710 rollback_data_t cb = { 0 };
3711 int err;
3712 boolean_t restore_resv = 0;
3713 uint64_t old_volsize = 0, new_volsize;
3714 zfs_prop_t resv_prop = { 0 };
3715
3716 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3717 zhp->zfs_type == ZFS_TYPE_VOLUME);
3718
3719 /*
3720 * Destroy all recent snapshots and their dependents.
3721 */
3722 cb.cb_force = force;
3723 cb.cb_target = snap->zfs_name;
3724 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3725 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3726 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3727
3728 if (cb.cb_error)
3729 return (-1);
3730
3731 /*
3732 * Now that we have verified that the snapshot is the latest,
3733 * rollback to the given snapshot.
3734 */
3735
3736 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3737 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3738 return (-1);
3739 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3740 restore_resv =
3741 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3742 }
3743
3744 /*
3745 * We rely on zfs_iter_children() to verify that there are no
3746 * newer snapshots for the given dataset. Therefore, we can
3747 * simply pass the name on to the ioctl() call. There is still
3748 * an unlikely race condition where the user has taken a
3749 * snapshot since we verified that this was the most recent.
3750 */
3751 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3752 if (err != 0) {
3753 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3754 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3755 zhp->zfs_name);
3756 return (err);
3757 }
3758
3759 /*
3760 * For volumes, if the pre-rollback volsize matched the pre-
3761 * rollback reservation and the volsize has changed then set
3762 * the reservation property to the post-rollback volsize.
3763 * Make a new handle since the rollback closed the dataset.
3764 */
3765 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3766 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3767 if (restore_resv) {
3768 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3769 if (old_volsize != new_volsize)
3770 err = zfs_prop_set_int(zhp, resv_prop,
3771 new_volsize);
3772 }
3773 zfs_close(zhp);
3774 }
3775 return (err);
3776 }
3777
3778 /*
3779 * Renames the given dataset.
3780 */
3781 int
3782 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3783 boolean_t force_unmount)
3784 {
3785 int ret;
3786 zfs_cmd_t zc = {"\0"};
3787 char *delim;
3788 prop_changelist_t *cl = NULL;
3789 zfs_handle_t *zhrp = NULL;
3790 char *parentname = NULL;
3791 char parent[ZFS_MAXNAMELEN];
3792 libzfs_handle_t *hdl = zhp->zfs_hdl;
3793 char errbuf[1024];
3794
3795 /* if we have the same exact name, just return success */
3796 if (strcmp(zhp->zfs_name, target) == 0)
3797 return (0);
3798
3799 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3800 "cannot rename to '%s'"), target);
3801
3802 /*
3803 * Make sure the target name is valid
3804 */
3805 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3806 if ((strchr(target, '@') == NULL) ||
3807 *target == '@') {
3808 /*
3809 * Snapshot target name is abbreviated,
3810 * reconstruct full dataset name
3811 */
3812 (void) strlcpy(parent, zhp->zfs_name,
3813 sizeof (parent));
3814 delim = strchr(parent, '@');
3815 if (strchr(target, '@') == NULL)
3816 *(++delim) = '\0';
3817 else
3818 *delim = '\0';
3819 (void) strlcat(parent, target, sizeof (parent));
3820 target = parent;
3821 } else {
3822 /*
3823 * Make sure we're renaming within the same dataset.
3824 */
3825 delim = strchr(target, '@');
3826 if (strncmp(zhp->zfs_name, target, delim - target)
3827 != 0 || zhp->zfs_name[delim - target] != '@') {
3828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3829 "snapshots must be part of same "
3830 "dataset"));
3831 return (zfs_error(hdl, EZFS_CROSSTARGET,
3832 errbuf));
3833 }
3834 }
3835 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3836 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3837 } else {
3838 if (recursive) {
3839 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3840 "recursive rename must be a snapshot"));
3841 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3842 }
3843
3844 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3845 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3846
3847 /* validate parents */
3848 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3849 return (-1);
3850
3851 /* make sure we're in the same pool */
3852 verify((delim = strchr(target, '/')) != NULL);
3853 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3854 zhp->zfs_name[delim - target] != '/') {
3855 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3856 "datasets must be within same pool"));
3857 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3858 }
3859
3860 /* new name cannot be a child of the current dataset name */
3861 if (is_descendant(zhp->zfs_name, target)) {
3862 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3863 "New dataset name cannot be a descendant of "
3864 "current dataset name"));
3865 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3866 }
3867 }
3868
3869 (void) snprintf(errbuf, sizeof (errbuf),
3870 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3871
3872 if (getzoneid() == GLOBAL_ZONEID &&
3873 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3875 "dataset is used in a non-global zone"));
3876 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3877 }
3878
3879 if (recursive) {
3880
3881 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3882 if (parentname == NULL) {
3883 ret = -1;
3884 goto error;
3885 }
3886 delim = strchr(parentname, '@');
3887 *delim = '\0';
3888 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3889 if (zhrp == NULL) {
3890 ret = -1;
3891 goto error;
3892 }
3893
3894 } else {
3895 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3896 force_unmount ? MS_FORCE : 0)) == NULL)
3897 return (-1);
3898
3899 if (changelist_haszonedchild(cl)) {
3900 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3901 "child dataset with inherited mountpoint is used "
3902 "in a non-global zone"));
3903 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3904 ret = -1;
3905 goto error;
3906 }
3907
3908 if ((ret = changelist_prefix(cl)) != 0)
3909 goto error;
3910 }
3911
3912 if (ZFS_IS_VOLUME(zhp))
3913 zc.zc_objset_type = DMU_OST_ZVOL;
3914 else
3915 zc.zc_objset_type = DMU_OST_ZFS;
3916
3917 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3918 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3919
3920 zc.zc_cookie = recursive;
3921
3922 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3923 /*
3924 * if it was recursive, the one that actually failed will
3925 * be in zc.zc_name
3926 */
3927 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3928 "cannot rename '%s'"), zc.zc_name);
3929
3930 if (recursive && errno == EEXIST) {
3931 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3932 "a child dataset already has a snapshot "
3933 "with the new name"));
3934 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3935 } else {
3936 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3937 }
3938
3939 /*
3940 * On failure, we still want to remount any filesystems that
3941 * were previously mounted, so we don't alter the system state.
3942 */
3943 if (!recursive)
3944 (void) changelist_postfix(cl);
3945 } else {
3946 if (!recursive) {
3947 changelist_rename(cl, zfs_get_name(zhp), target);
3948 ret = changelist_postfix(cl);
3949 }
3950 }
3951
3952 error:
3953 if (parentname) {
3954 free(parentname);
3955 }
3956 if (zhrp) {
3957 zfs_close(zhrp);
3958 }
3959 if (cl) {
3960 changelist_free(cl);
3961 }
3962 return (ret);
3963 }
3964
3965 nvlist_t *
3966 zfs_get_user_props(zfs_handle_t *zhp)
3967 {
3968 return (zhp->zfs_user_props);
3969 }
3970
3971 /*
3972 * This function is used by 'zfs list' to determine the exact set of columns to
3973 * display, and their maximum widths. This does two main things:
3974 *
3975 * - If this is a list of all properties, then expand the list to include
3976 * all native properties, and set a flag so that for each dataset we look
3977 * for new unique user properties and add them to the list.
3978 *
3979 * - For non fixed-width properties, keep track of the maximum width seen
3980 * so that we can size the column appropriately. If the user has
3981 * requested received property values, we also need to compute the width
3982 * of the RECEIVED column.
3983 */
3984 int
3985 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
3986 boolean_t literal)
3987 {
3988 libzfs_handle_t *hdl = zhp->zfs_hdl;
3989 zprop_list_t *entry;
3990 zprop_list_t **last, **start;
3991 nvlist_t *userprops, *propval;
3992 nvpair_t *elem;
3993 char *strval;
3994 char buf[ZFS_MAXPROPLEN];
3995
3996 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3997 return (-1);
3998
3999 userprops = zfs_get_user_props(zhp);
4000
4001 entry = *plp;
4002 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4003 /*
4004 * Go through and add any user properties as necessary. We
4005 * start by incrementing our list pointer to the first
4006 * non-native property.
4007 */
4008 start = plp;
4009 while (*start != NULL) {
4010 if ((*start)->pl_prop == ZPROP_INVAL)
4011 break;
4012 start = &(*start)->pl_next;
4013 }
4014
4015 elem = NULL;
4016 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4017 /*
4018 * See if we've already found this property in our list.
4019 */
4020 for (last = start; *last != NULL;
4021 last = &(*last)->pl_next) {
4022 if (strcmp((*last)->pl_user_prop,
4023 nvpair_name(elem)) == 0)
4024 break;
4025 }
4026
4027 if (*last == NULL) {
4028 if ((entry = zfs_alloc(hdl,
4029 sizeof (zprop_list_t))) == NULL ||
4030 ((entry->pl_user_prop = zfs_strdup(hdl,
4031 nvpair_name(elem)))) == NULL) {
4032 free(entry);
4033 return (-1);
4034 }
4035
4036 entry->pl_prop = ZPROP_INVAL;
4037 entry->pl_width = strlen(nvpair_name(elem));
4038 entry->pl_all = B_TRUE;
4039 *last = entry;
4040 }
4041 }
4042 }
4043
4044 /*
4045 * Now go through and check the width of any non-fixed columns
4046 */
4047 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4048 if (entry->pl_fixed && !literal)
4049 continue;
4050
4051 if (entry->pl_prop != ZPROP_INVAL) {
4052 if (zfs_prop_get(zhp, entry->pl_prop,
4053 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4054 if (strlen(buf) > entry->pl_width)
4055 entry->pl_width = strlen(buf);
4056 }
4057 if (received && zfs_prop_get_recvd(zhp,
4058 zfs_prop_to_name(entry->pl_prop),
4059 buf, sizeof (buf), literal) == 0)
4060 if (strlen(buf) > entry->pl_recvd_width)
4061 entry->pl_recvd_width = strlen(buf);
4062 } else {
4063 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4064 &propval) == 0) {
4065 verify(nvlist_lookup_string(propval,
4066 ZPROP_VALUE, &strval) == 0);
4067 if (strlen(strval) > entry->pl_width)
4068 entry->pl_width = strlen(strval);
4069 }
4070 if (received && zfs_prop_get_recvd(zhp,
4071 entry->pl_user_prop,
4072 buf, sizeof (buf), literal) == 0)
4073 if (strlen(buf) > entry->pl_recvd_width)
4074 entry->pl_recvd_width = strlen(buf);
4075 }
4076 }
4077
4078 return (0);
4079 }
4080
4081 void
4082 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4083 {
4084 nvpair_t *curr;
4085
4086 /*
4087 * Keep a reference to the props-table against which we prune the
4088 * properties.
4089 */
4090 zhp->zfs_props_table = props;
4091
4092 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4093
4094 while (curr) {
4095 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4096 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4097
4098 /*
4099 * User properties will result in ZPROP_INVAL, and since we
4100 * only know how to prune standard ZFS properties, we always
4101 * leave these in the list. This can also happen if we
4102 * encounter an unknown DSL property (when running older
4103 * software, for example).
4104 */
4105 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4106 (void) nvlist_remove(zhp->zfs_props,
4107 nvpair_name(curr), nvpair_type(curr));
4108 curr = next;
4109 }
4110 }
4111
4112 static int
4113 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4114 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4115 {
4116 zfs_cmd_t zc = {"\0"};
4117 nvlist_t *nvlist = NULL;
4118 int error;
4119
4120 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4121 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4122 zc.zc_cookie = (uint64_t)cmd;
4123
4124 if (cmd == ZFS_SMB_ACL_RENAME) {
4125 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4126 (void) no_memory(hdl);
4127 return (-1);
4128 }
4129 }
4130
4131 switch (cmd) {
4132 case ZFS_SMB_ACL_ADD:
4133 case ZFS_SMB_ACL_REMOVE:
4134 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4135 break;
4136 case ZFS_SMB_ACL_RENAME:
4137 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4138 resource1) != 0) {
4139 (void) no_memory(hdl);
4140 return (-1);
4141 }
4142 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4143 resource2) != 0) {
4144 (void) no_memory(hdl);
4145 return (-1);
4146 }
4147 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4148 nvlist_free(nvlist);
4149 return (-1);
4150 }
4151 break;
4152 case ZFS_SMB_ACL_PURGE:
4153 break;
4154 default:
4155 return (-1);
4156 }
4157 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4158 if (nvlist)
4159 nvlist_free(nvlist);
4160 return (error);
4161 }
4162
4163 int
4164 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4165 char *path, char *resource)
4166 {
4167 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4168 resource, NULL));
4169 }
4170
4171 int
4172 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4173 char *path, char *resource)
4174 {
4175 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4176 resource, NULL));
4177 }
4178
4179 int
4180 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4181 {
4182 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4183 NULL, NULL));
4184 }
4185
4186 int
4187 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4188 char *oldname, char *newname)
4189 {
4190 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4191 oldname, newname));
4192 }
4193
4194 int
4195 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4196 zfs_userspace_cb_t func, void *arg)
4197 {
4198 zfs_cmd_t zc = {"\0"};
4199 zfs_useracct_t buf[100];
4200 libzfs_handle_t *hdl = zhp->zfs_hdl;
4201 int ret;
4202
4203 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4204
4205 zc.zc_objset_type = type;
4206 zc.zc_nvlist_dst = (uintptr_t)buf;
4207
4208 for (;;) {
4209 zfs_useracct_t *zua = buf;
4210
4211 zc.zc_nvlist_dst_size = sizeof (buf);
4212 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4213 char errbuf[ZFS_MAXNAMELEN + 32];
4214
4215 (void) snprintf(errbuf, sizeof (errbuf),
4216 dgettext(TEXT_DOMAIN,
4217 "cannot get used/quota for %s"), zc.zc_name);
4218 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4219 }
4220 if (zc.zc_nvlist_dst_size == 0)
4221 break;
4222
4223 while (zc.zc_nvlist_dst_size > 0) {
4224 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4225 zua->zu_space)) != 0)
4226 return (ret);
4227 zua++;
4228 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4229 }
4230 }
4231
4232 return (0);
4233 }
4234
4235 struct holdarg {
4236 nvlist_t *nvl;
4237 const char *snapname;
4238 const char *tag;
4239 boolean_t recursive;
4240 int error;
4241 };
4242
4243 static int
4244 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4245 {
4246 struct holdarg *ha = arg;
4247 char name[ZFS_MAXNAMELEN];
4248 int rv = 0;
4249
4250 (void) snprintf(name, sizeof (name),
4251 "%s@%s", zhp->zfs_name, ha->snapname);
4252
4253 if (lzc_exists(name))
4254 fnvlist_add_string(ha->nvl, name, ha->tag);
4255
4256 if (ha->recursive)
4257 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4258 zfs_close(zhp);
4259 return (rv);
4260 }
4261
4262 int
4263 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4264 boolean_t recursive, int cleanup_fd)
4265 {
4266 int ret;
4267 struct holdarg ha;
4268
4269 ha.nvl = fnvlist_alloc();
4270 ha.snapname = snapname;
4271 ha.tag = tag;
4272 ha.recursive = recursive;
4273 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4274
4275 if (nvlist_empty(ha.nvl)) {
4276 char errbuf[1024];
4277
4278 fnvlist_free(ha.nvl);
4279 ret = ENOENT;
4280 (void) snprintf(errbuf, sizeof (errbuf),
4281 dgettext(TEXT_DOMAIN,
4282 "cannot hold snapshot '%s@%s'"),
4283 zhp->zfs_name, snapname);
4284 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4285 return (ret);
4286 }
4287
4288 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4289 fnvlist_free(ha.nvl);
4290
4291 return (ret);
4292 }
4293
4294 int
4295 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4296 {
4297 int ret;
4298 nvlist_t *errors;
4299 libzfs_handle_t *hdl = zhp->zfs_hdl;
4300 char errbuf[1024];
4301 nvpair_t *elem;
4302
4303 errors = NULL;
4304 ret = lzc_hold(holds, cleanup_fd, &errors);
4305
4306 if (ret == 0) {
4307 /* There may be errors even in the success case. */
4308 fnvlist_free(errors);
4309 return (0);
4310 }
4311
4312 if (nvlist_empty(errors)) {
4313 /* no hold-specific errors */
4314 (void) snprintf(errbuf, sizeof (errbuf),
4315 dgettext(TEXT_DOMAIN, "cannot hold"));
4316 switch (ret) {
4317 case ENOTSUP:
4318 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4319 "pool must be upgraded"));
4320 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4321 break;
4322 case EINVAL:
4323 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4324 break;
4325 default:
4326 (void) zfs_standard_error(hdl, ret, errbuf);
4327 }
4328 }
4329
4330 for (elem = nvlist_next_nvpair(errors, NULL);
4331 elem != NULL;
4332 elem = nvlist_next_nvpair(errors, elem)) {
4333 (void) snprintf(errbuf, sizeof (errbuf),
4334 dgettext(TEXT_DOMAIN,
4335 "cannot hold snapshot '%s'"), nvpair_name(elem));
4336 switch (fnvpair_value_int32(elem)) {
4337 case E2BIG:
4338 /*
4339 * Temporary tags wind up having the ds object id
4340 * prepended. So even if we passed the length check
4341 * above, it's still possible for the tag to wind
4342 * up being slightly too long.
4343 */
4344 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4345 break;
4346 case EINVAL:
4347 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4348 break;
4349 case EEXIST:
4350 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4351 break;
4352 default:
4353 (void) zfs_standard_error(hdl,
4354 fnvpair_value_int32(elem), errbuf);
4355 }
4356 }
4357
4358 fnvlist_free(errors);
4359 return (ret);
4360 }
4361
4362 static int
4363 zfs_release_one(zfs_handle_t *zhp, void *arg)
4364 {
4365 struct holdarg *ha = arg;
4366 char name[ZFS_MAXNAMELEN];
4367 int rv = 0;
4368 nvlist_t *existing_holds;
4369
4370 (void) snprintf(name, sizeof (name),
4371 "%s@%s", zhp->zfs_name, ha->snapname);
4372
4373 if (lzc_get_holds(name, &existing_holds) != 0) {
4374 ha->error = ENOENT;
4375 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4376 ha->error = ESRCH;
4377 } else {
4378 nvlist_t *torelease = fnvlist_alloc();
4379 fnvlist_add_boolean(torelease, ha->tag);
4380 fnvlist_add_nvlist(ha->nvl, name, torelease);
4381 fnvlist_free(torelease);
4382 }
4383
4384 if (ha->recursive)
4385 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4386 zfs_close(zhp);
4387 return (rv);
4388 }
4389
4390 int
4391 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4392 boolean_t recursive)
4393 {
4394 int ret;
4395 struct holdarg ha;
4396 nvlist_t *errors = NULL;
4397 nvpair_t *elem;
4398 libzfs_handle_t *hdl = zhp->zfs_hdl;
4399 char errbuf[1024];
4400
4401 ha.nvl = fnvlist_alloc();
4402 ha.snapname = snapname;
4403 ha.tag = tag;
4404 ha.recursive = recursive;
4405 ha.error = 0;
4406 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4407
4408 if (nvlist_empty(ha.nvl)) {
4409 fnvlist_free(ha.nvl);
4410 ret = ha.error;
4411 (void) snprintf(errbuf, sizeof (errbuf),
4412 dgettext(TEXT_DOMAIN,
4413 "cannot release hold from snapshot '%s@%s'"),
4414 zhp->zfs_name, snapname);
4415 if (ret == ESRCH) {
4416 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4417 } else {
4418 (void) zfs_standard_error(hdl, ret, errbuf);
4419 }
4420 return (ret);
4421 }
4422
4423 ret = lzc_release(ha.nvl, &errors);
4424 fnvlist_free(ha.nvl);
4425
4426 if (ret == 0) {
4427 /* There may be errors even in the success case. */
4428 fnvlist_free(errors);
4429 return (0);
4430 }
4431
4432 if (nvlist_empty(errors)) {
4433 /* no hold-specific errors */
4434 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4435 "cannot release"));
4436 switch (errno) {
4437 case ENOTSUP:
4438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4439 "pool must be upgraded"));
4440 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4441 break;
4442 default:
4443 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4444 }
4445 }
4446
4447 for (elem = nvlist_next_nvpair(errors, NULL);
4448 elem != NULL;
4449 elem = nvlist_next_nvpair(errors, elem)) {
4450 (void) snprintf(errbuf, sizeof (errbuf),
4451 dgettext(TEXT_DOMAIN,
4452 "cannot release hold from snapshot '%s'"),
4453 nvpair_name(elem));
4454 switch (fnvpair_value_int32(elem)) {
4455 case ESRCH:
4456 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4457 break;
4458 case EINVAL:
4459 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4460 break;
4461 default:
4462 (void) zfs_standard_error_fmt(hdl,
4463 fnvpair_value_int32(elem), errbuf);
4464 }
4465 }
4466
4467 fnvlist_free(errors);
4468 return (ret);
4469 }
4470
4471 int
4472 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4473 {
4474 zfs_cmd_t zc = {"\0"};
4475 libzfs_handle_t *hdl = zhp->zfs_hdl;
4476 int nvsz = 2048;
4477 void *nvbuf;
4478 int err = 0;
4479 char errbuf[1024];
4480
4481 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4482 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4483
4484 tryagain:
4485
4486 nvbuf = malloc(nvsz);
4487 if (nvbuf == NULL) {
4488 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4489 goto out;
4490 }
4491
4492 zc.zc_nvlist_dst_size = nvsz;
4493 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4494
4495 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4496
4497 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4498 (void) snprintf(errbuf, sizeof (errbuf),
4499 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4500 zc.zc_name);
4501 switch (errno) {
4502 case ENOMEM:
4503 free(nvbuf);
4504 nvsz = zc.zc_nvlist_dst_size;
4505 goto tryagain;
4506
4507 case ENOTSUP:
4508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4509 "pool must be upgraded"));
4510 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4511 break;
4512 case EINVAL:
4513 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4514 break;
4515 case ENOENT:
4516 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4517 break;
4518 default:
4519 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4520 break;
4521 }
4522 } else {
4523 /* success */
4524 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4525 if (rc) {
4526 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4527 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4528 zc.zc_name);
4529 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4530 }
4531 }
4532
4533 free(nvbuf);
4534 out:
4535 return (err);
4536 }
4537
4538 int
4539 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4540 {
4541 zfs_cmd_t zc = {"\0"};
4542 libzfs_handle_t *hdl = zhp->zfs_hdl;
4543 char *nvbuf;
4544 char errbuf[1024];
4545 size_t nvsz;
4546 int err;
4547
4548 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4549 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4550
4551 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4552 assert(err == 0);
4553
4554 nvbuf = malloc(nvsz);
4555
4556 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4557 assert(err == 0);
4558
4559 zc.zc_nvlist_src_size = nvsz;
4560 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4561 zc.zc_perm_action = un;
4562
4563 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4564
4565 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4566 (void) snprintf(errbuf, sizeof (errbuf),
4567 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4568 zc.zc_name);
4569 switch (errno) {
4570 case ENOTSUP:
4571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4572 "pool must be upgraded"));
4573 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4574 break;
4575 case EINVAL:
4576 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4577 break;
4578 case ENOENT:
4579 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4580 break;
4581 default:
4582 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4583 break;
4584 }
4585 }
4586
4587 free(nvbuf);
4588
4589 return (err);
4590 }
4591
4592 int
4593 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4594 {
4595 int err;
4596 char errbuf[1024];
4597
4598 err = lzc_get_holds(zhp->zfs_name, nvl);
4599
4600 if (err != 0) {
4601 libzfs_handle_t *hdl = zhp->zfs_hdl;
4602
4603 (void) snprintf(errbuf, sizeof (errbuf),
4604 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4605 zhp->zfs_name);
4606 switch (err) {
4607 case ENOTSUP:
4608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4609 "pool must be upgraded"));
4610 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4611 break;
4612 case EINVAL:
4613 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4614 break;
4615 case ENOENT:
4616 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4617 break;
4618 default:
4619 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4620 break;
4621 }
4622 }
4623
4624 return (err);
4625 }
4626
4627 /*
4628 * Convert the zvol's volume size to an appropriate reservation.
4629 * Note: If this routine is updated, it is necessary to update the ZFS test
4630 * suite's shell version in reservation.kshlib.
4631 */
4632 uint64_t
4633 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4634 {
4635 uint64_t numdb;
4636 uint64_t nblocks, volblocksize;
4637 int ncopies;
4638 char *strval;
4639
4640 if (nvlist_lookup_string(props,
4641 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4642 ncopies = atoi(strval);
4643 else
4644 ncopies = 1;
4645 if (nvlist_lookup_uint64(props,
4646 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4647 &volblocksize) != 0)
4648 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4649 nblocks = volsize/volblocksize;
4650 /* start with metadnode L0-L6 */
4651 numdb = 7;
4652 /* calculate number of indirects */
4653 while (nblocks > 1) {
4654 nblocks += DNODES_PER_LEVEL - 1;
4655 nblocks /= DNODES_PER_LEVEL;
4656 numdb += nblocks;
4657 }
4658 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4659 volsize *= ncopies;
4660 /*
4661 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4662 * compressed, but in practice they compress down to about
4663 * 1100 bytes
4664 */
4665 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4666 volsize += numdb;
4667 return (volsize);
4668 }