]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libzfs/libzfs_dataset.c
Illumos 5813 - zfs_setprop_error(): Handle errno value E2BIG.
[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 E2BIG:
1449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1450 "property value too long"));
1451 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1452 break;
1453
1454 case ENOTSUP:
1455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1456 "pool and or dataset must be upgraded to set this "
1457 "property or value"));
1458 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1459 break;
1460
1461 case ERANGE:
1462 if (prop == ZFS_PROP_COMPRESSION ||
1463 prop == ZFS_PROP_RECORDSIZE) {
1464 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1465 "property setting is not allowed on "
1466 "bootable datasets"));
1467 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1468 } else {
1469 (void) zfs_standard_error(hdl, err, errbuf);
1470 }
1471 break;
1472
1473 case EINVAL:
1474 if (prop == ZPROP_INVAL) {
1475 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1476 } else {
1477 (void) zfs_standard_error(hdl, err, errbuf);
1478 }
1479 break;
1480
1481 case EOVERFLOW:
1482 /*
1483 * This platform can't address a volume this big.
1484 */
1485 #ifdef _ILP32
1486 if (prop == ZFS_PROP_VOLSIZE) {
1487 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1488 break;
1489 }
1490 #endif
1491 /* FALLTHROUGH */
1492 default:
1493 (void) zfs_standard_error(hdl, err, errbuf);
1494 }
1495 }
1496
1497 static boolean_t
1498 zfs_is_namespace_prop(zfs_prop_t prop)
1499 {
1500 switch (prop) {
1501
1502 case ZFS_PROP_ATIME:
1503 case ZFS_PROP_RELATIME:
1504 case ZFS_PROP_DEVICES:
1505 case ZFS_PROP_EXEC:
1506 case ZFS_PROP_SETUID:
1507 case ZFS_PROP_READONLY:
1508 case ZFS_PROP_XATTR:
1509 case ZFS_PROP_NBMAND:
1510 return (B_TRUE);
1511
1512 default:
1513 return (B_FALSE);
1514 }
1515 }
1516
1517 /*
1518 * Given a property name and value, set the property for the given dataset.
1519 */
1520 int
1521 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1522 {
1523 zfs_cmd_t zc = {"\0"};
1524 int ret = -1;
1525 prop_changelist_t *cl = NULL;
1526 char errbuf[1024];
1527 libzfs_handle_t *hdl = zhp->zfs_hdl;
1528 nvlist_t *nvl = NULL, *realprops;
1529 zfs_prop_t prop;
1530 boolean_t do_prefix = B_TRUE;
1531 int added_resv = 0;
1532
1533 (void) snprintf(errbuf, sizeof (errbuf),
1534 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1535 zhp->zfs_name);
1536
1537 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1538 nvlist_add_string(nvl, propname, propval) != 0) {
1539 (void) no_memory(hdl);
1540 goto error;
1541 }
1542
1543 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1544 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1545 goto error;
1546
1547 nvlist_free(nvl);
1548 nvl = realprops;
1549
1550 prop = zfs_name_to_prop(propname);
1551
1552 if (prop == ZFS_PROP_VOLSIZE) {
1553 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1554 goto error;
1555 }
1556
1557 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1558 goto error;
1559
1560 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1562 "child dataset with inherited mountpoint is used "
1563 "in a non-global zone"));
1564 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1565 goto error;
1566 }
1567
1568 /*
1569 * We don't want to unmount & remount the dataset when changing
1570 * its canmount property to 'on' or 'noauto'. We only use
1571 * the changelist logic to unmount when setting canmount=off.
1572 */
1573 if (prop == ZFS_PROP_CANMOUNT) {
1574 uint64_t idx;
1575 int err = zprop_string_to_index(prop, propval, &idx,
1576 ZFS_TYPE_DATASET);
1577 if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1578 do_prefix = B_FALSE;
1579 }
1580
1581 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1582 goto error;
1583
1584 /*
1585 * Execute the corresponding ioctl() to set this property.
1586 */
1587 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1588
1589 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1590 goto error;
1591
1592 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1593
1594 if (ret != 0) {
1595 zfs_setprop_error(hdl, prop, errno, errbuf);
1596 if (added_resv && errno == ENOSPC) {
1597 /* clean up the volsize property we tried to set */
1598 uint64_t old_volsize = zfs_prop_get_int(zhp,
1599 ZFS_PROP_VOLSIZE);
1600 nvlist_free(nvl);
1601 zcmd_free_nvlists(&zc);
1602 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1603 goto error;
1604 if (nvlist_add_uint64(nvl,
1605 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1606 old_volsize) != 0)
1607 goto error;
1608 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1609 goto error;
1610 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1611 }
1612 } else {
1613 if (do_prefix)
1614 ret = changelist_postfix(cl);
1615
1616 if (ret == 0) {
1617 /*
1618 * Refresh the statistics so the new property
1619 * value is reflected.
1620 */
1621 (void) get_stats(zhp);
1622
1623 /*
1624 * Remount the filesystem to propagate the change
1625 * if one of the options handled by the generic
1626 * Linux namespace layer has been modified.
1627 */
1628 if (zfs_is_namespace_prop(prop) &&
1629 zfs_is_mounted(zhp, NULL))
1630 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1631 }
1632 }
1633
1634 error:
1635 nvlist_free(nvl);
1636 zcmd_free_nvlists(&zc);
1637 if (cl)
1638 changelist_free(cl);
1639 return (ret);
1640 }
1641
1642 /*
1643 * Given a property, inherit the value from the parent dataset, or if received
1644 * is TRUE, revert to the received value, if any.
1645 */
1646 int
1647 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1648 {
1649 zfs_cmd_t zc = {"\0"};
1650 int ret;
1651 prop_changelist_t *cl;
1652 libzfs_handle_t *hdl = zhp->zfs_hdl;
1653 char errbuf[1024];
1654 zfs_prop_t prop;
1655
1656 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1657 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1658
1659 zc.zc_cookie = received;
1660 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1661 /*
1662 * For user properties, the amount of work we have to do is very
1663 * small, so just do it here.
1664 */
1665 if (!zfs_prop_user(propname)) {
1666 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1667 "invalid property"));
1668 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1669 }
1670
1671 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1672 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1673
1674 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1675 return (zfs_standard_error(hdl, errno, errbuf));
1676
1677 return (0);
1678 }
1679
1680 /*
1681 * Verify that this property is inheritable.
1682 */
1683 if (zfs_prop_readonly(prop))
1684 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1685
1686 if (!zfs_prop_inheritable(prop) && !received)
1687 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1688
1689 /*
1690 * Check to see if the value applies to this type
1691 */
1692 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
1693 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1694
1695 /*
1696 * Normalize the name, to get rid of shorthand abbreviations.
1697 */
1698 propname = zfs_prop_to_name(prop);
1699 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1700 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1701
1702 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1703 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1705 "dataset is used in a non-global zone"));
1706 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1707 }
1708
1709 /*
1710 * Determine datasets which will be affected by this change, if any.
1711 */
1712 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1713 return (-1);
1714
1715 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1716 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1717 "child dataset with inherited mountpoint is used "
1718 "in a non-global zone"));
1719 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1720 goto error;
1721 }
1722
1723 if ((ret = changelist_prefix(cl)) != 0)
1724 goto error;
1725
1726 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1727 return (zfs_standard_error(hdl, errno, errbuf));
1728 } else {
1729
1730 if ((ret = changelist_postfix(cl)) != 0)
1731 goto error;
1732
1733 /*
1734 * Refresh the statistics so the new property is reflected.
1735 */
1736 (void) get_stats(zhp);
1737
1738 /*
1739 * Remount the filesystem to propagate the change
1740 * if one of the options handled by the generic
1741 * Linux namespace layer has been modified.
1742 */
1743 if (zfs_is_namespace_prop(prop) &&
1744 zfs_is_mounted(zhp, NULL))
1745 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1746 }
1747
1748 error:
1749 changelist_free(cl);
1750 return (ret);
1751 }
1752
1753 /*
1754 * True DSL properties are stored in an nvlist. The following two functions
1755 * extract them appropriately.
1756 */
1757 uint64_t
1758 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1759 {
1760 nvlist_t *nv;
1761 uint64_t value;
1762
1763 *source = NULL;
1764 if (nvlist_lookup_nvlist(zhp->zfs_props,
1765 zfs_prop_to_name(prop), &nv) == 0) {
1766 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1767 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1768 } else {
1769 verify(!zhp->zfs_props_table ||
1770 zhp->zfs_props_table[prop] == B_TRUE);
1771 value = zfs_prop_default_numeric(prop);
1772 *source = "";
1773 }
1774
1775 return (value);
1776 }
1777
1778 static char *
1779 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1780 {
1781 nvlist_t *nv;
1782 char *value;
1783
1784 *source = NULL;
1785 if (nvlist_lookup_nvlist(zhp->zfs_props,
1786 zfs_prop_to_name(prop), &nv) == 0) {
1787 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1788 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1789 } else {
1790 verify(!zhp->zfs_props_table ||
1791 zhp->zfs_props_table[prop] == B_TRUE);
1792 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1793 value = "";
1794 *source = "";
1795 }
1796
1797 return (value);
1798 }
1799
1800 static boolean_t
1801 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1802 {
1803 return (zhp->zfs_props == zhp->zfs_recvd_props);
1804 }
1805
1806 static void
1807 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1808 {
1809 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1810 zhp->zfs_props = zhp->zfs_recvd_props;
1811 }
1812
1813 static void
1814 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1815 {
1816 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1817 *cookie = 0;
1818 }
1819
1820 /*
1821 * Internal function for getting a numeric property. Both zfs_prop_get() and
1822 * zfs_prop_get_int() are built using this interface.
1823 *
1824 * Certain properties can be overridden using 'mount -o'. In this case, scan
1825 * the contents of the /etc/mtab entry, searching for the appropriate options.
1826 * If they differ from the on-disk values, report the current values and mark
1827 * the source "temporary".
1828 */
1829 static int
1830 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1831 char **source, uint64_t *val)
1832 {
1833 zfs_cmd_t zc = {"\0"};
1834 nvlist_t *zplprops = NULL;
1835 struct mnttab mnt;
1836 char *mntopt_on = NULL;
1837 char *mntopt_off = NULL;
1838 boolean_t received = zfs_is_recvd_props_mode(zhp);
1839
1840 *source = NULL;
1841
1842 /*
1843 * If the property is being fetched for a snapshot, check whether
1844 * the property is valid for the snapshot's head dataset type.
1845 */
1846 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
1847 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
1848 *val = zfs_prop_default_numeric(prop);
1849 return (-1);
1850 }
1851
1852 switch (prop) {
1853 case ZFS_PROP_ATIME:
1854 mntopt_on = MNTOPT_ATIME;
1855 mntopt_off = MNTOPT_NOATIME;
1856 break;
1857
1858 case ZFS_PROP_RELATIME:
1859 mntopt_on = MNTOPT_RELATIME;
1860 mntopt_off = MNTOPT_NORELATIME;
1861 break;
1862
1863 case ZFS_PROP_DEVICES:
1864 mntopt_on = MNTOPT_DEVICES;
1865 mntopt_off = MNTOPT_NODEVICES;
1866 break;
1867
1868 case ZFS_PROP_EXEC:
1869 mntopt_on = MNTOPT_EXEC;
1870 mntopt_off = MNTOPT_NOEXEC;
1871 break;
1872
1873 case ZFS_PROP_READONLY:
1874 mntopt_on = MNTOPT_RO;
1875 mntopt_off = MNTOPT_RW;
1876 break;
1877
1878 case ZFS_PROP_SETUID:
1879 mntopt_on = MNTOPT_SETUID;
1880 mntopt_off = MNTOPT_NOSETUID;
1881 break;
1882
1883 case ZFS_PROP_XATTR:
1884 mntopt_on = MNTOPT_XATTR;
1885 mntopt_off = MNTOPT_NOXATTR;
1886 break;
1887
1888 case ZFS_PROP_NBMAND:
1889 mntopt_on = MNTOPT_NBMAND;
1890 mntopt_off = MNTOPT_NONBMAND;
1891 break;
1892 default:
1893 break;
1894 }
1895
1896 /*
1897 * Because looking up the mount options is potentially expensive
1898 * (iterating over all of /etc/mtab), we defer its calculation until
1899 * we're looking up a property which requires its presence.
1900 */
1901 if (!zhp->zfs_mntcheck &&
1902 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1903 libzfs_handle_t *hdl = zhp->zfs_hdl;
1904 struct mnttab entry;
1905
1906 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1907 zhp->zfs_mntopts = zfs_strdup(hdl,
1908 entry.mnt_mntopts);
1909 if (zhp->zfs_mntopts == NULL)
1910 return (-1);
1911 }
1912
1913 zhp->zfs_mntcheck = B_TRUE;
1914 }
1915
1916 if (zhp->zfs_mntopts == NULL)
1917 mnt.mnt_mntopts = "";
1918 else
1919 mnt.mnt_mntopts = zhp->zfs_mntopts;
1920
1921 switch (prop) {
1922 case ZFS_PROP_DEVICES:
1923 case ZFS_PROP_EXEC:
1924 case ZFS_PROP_READONLY:
1925 case ZFS_PROP_SETUID:
1926 case ZFS_PROP_XATTR:
1927 case ZFS_PROP_NBMAND:
1928 *val = getprop_uint64(zhp, prop, source);
1929
1930 if (received)
1931 break;
1932
1933 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1934 *val = B_TRUE;
1935 if (src)
1936 *src = ZPROP_SRC_TEMPORARY;
1937 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1938 *val = B_FALSE;
1939 if (src)
1940 *src = ZPROP_SRC_TEMPORARY;
1941 }
1942 break;
1943
1944 case ZFS_PROP_ATIME:
1945 case ZFS_PROP_RELATIME:
1946 case ZFS_PROP_CANMOUNT:
1947 case ZFS_PROP_VOLSIZE:
1948 case ZFS_PROP_QUOTA:
1949 case ZFS_PROP_REFQUOTA:
1950 case ZFS_PROP_RESERVATION:
1951 case ZFS_PROP_REFRESERVATION:
1952 case ZFS_PROP_FILESYSTEM_LIMIT:
1953 case ZFS_PROP_SNAPSHOT_LIMIT:
1954 case ZFS_PROP_FILESYSTEM_COUNT:
1955 case ZFS_PROP_SNAPSHOT_COUNT:
1956 *val = getprop_uint64(zhp, prop, source);
1957
1958 if (*source == NULL) {
1959 /* not default, must be local */
1960 *source = zhp->zfs_name;
1961 }
1962 break;
1963
1964 case ZFS_PROP_MOUNTED:
1965 *val = (zhp->zfs_mntopts != NULL);
1966 break;
1967
1968 case ZFS_PROP_NUMCLONES:
1969 *val = zhp->zfs_dmustats.dds_num_clones;
1970 break;
1971
1972 case ZFS_PROP_VERSION:
1973 case ZFS_PROP_NORMALIZE:
1974 case ZFS_PROP_UTF8ONLY:
1975 case ZFS_PROP_CASE:
1976 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1977 return (-1);
1978 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1979 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1980 zcmd_free_nvlists(&zc);
1981 if (prop == ZFS_PROP_VERSION &&
1982 zhp->zfs_type == ZFS_TYPE_VOLUME)
1983 *val = zfs_prop_default_numeric(prop);
1984 return (-1);
1985 }
1986 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1987 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1988 val) != 0) {
1989 zcmd_free_nvlists(&zc);
1990 return (-1);
1991 }
1992 if (zplprops)
1993 nvlist_free(zplprops);
1994 zcmd_free_nvlists(&zc);
1995 break;
1996
1997 case ZFS_PROP_INCONSISTENT:
1998 *val = zhp->zfs_dmustats.dds_inconsistent;
1999 break;
2000
2001 default:
2002 switch (zfs_prop_get_type(prop)) {
2003 case PROP_TYPE_NUMBER:
2004 case PROP_TYPE_INDEX:
2005 *val = getprop_uint64(zhp, prop, source);
2006 /*
2007 * If we tried to use a default value for a
2008 * readonly property, it means that it was not
2009 * present.
2010 */
2011 if (zfs_prop_readonly(prop) &&
2012 *source != NULL && (*source)[0] == '\0') {
2013 *source = NULL;
2014 }
2015 break;
2016
2017 case PROP_TYPE_STRING:
2018 default:
2019 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2020 "cannot get non-numeric property"));
2021 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2022 dgettext(TEXT_DOMAIN, "internal error")));
2023 }
2024 }
2025
2026 return (0);
2027 }
2028
2029 /*
2030 * Calculate the source type, given the raw source string.
2031 */
2032 static void
2033 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2034 char *statbuf, size_t statlen)
2035 {
2036 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2037 return;
2038
2039 if (source == NULL) {
2040 *srctype = ZPROP_SRC_NONE;
2041 } else if (source[0] == '\0') {
2042 *srctype = ZPROP_SRC_DEFAULT;
2043 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2044 *srctype = ZPROP_SRC_RECEIVED;
2045 } else {
2046 if (strcmp(source, zhp->zfs_name) == 0) {
2047 *srctype = ZPROP_SRC_LOCAL;
2048 } else {
2049 (void) strlcpy(statbuf, source, statlen);
2050 *srctype = ZPROP_SRC_INHERITED;
2051 }
2052 }
2053
2054 }
2055
2056 int
2057 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2058 size_t proplen, boolean_t literal)
2059 {
2060 zfs_prop_t prop;
2061 int err = 0;
2062
2063 if (zhp->zfs_recvd_props == NULL)
2064 if (get_recvd_props_ioctl(zhp) != 0)
2065 return (-1);
2066
2067 prop = zfs_name_to_prop(propname);
2068
2069 if (prop != ZPROP_INVAL) {
2070 uint64_t cookie;
2071 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2072 return (-1);
2073 zfs_set_recvd_props_mode(zhp, &cookie);
2074 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2075 NULL, NULL, 0, literal);
2076 zfs_unset_recvd_props_mode(zhp, &cookie);
2077 } else {
2078 nvlist_t *propval;
2079 char *recvdval;
2080 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2081 propname, &propval) != 0)
2082 return (-1);
2083 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2084 &recvdval) == 0);
2085 (void) strlcpy(propbuf, recvdval, proplen);
2086 }
2087
2088 return (err == 0 ? 0 : -1);
2089 }
2090
2091 static int
2092 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2093 {
2094 nvlist_t *value;
2095 nvpair_t *pair;
2096
2097 value = zfs_get_clones_nvl(zhp);
2098 if (value == NULL)
2099 return (-1);
2100
2101 propbuf[0] = '\0';
2102 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2103 pair = nvlist_next_nvpair(value, pair)) {
2104 if (propbuf[0] != '\0')
2105 (void) strlcat(propbuf, ",", proplen);
2106 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2107 }
2108
2109 return (0);
2110 }
2111
2112 struct get_clones_arg {
2113 uint64_t numclones;
2114 nvlist_t *value;
2115 const char *origin;
2116 char buf[ZFS_MAXNAMELEN];
2117 };
2118
2119 int
2120 get_clones_cb(zfs_handle_t *zhp, void *arg)
2121 {
2122 struct get_clones_arg *gca = arg;
2123
2124 if (gca->numclones == 0) {
2125 zfs_close(zhp);
2126 return (0);
2127 }
2128
2129 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2130 NULL, NULL, 0, B_TRUE) != 0)
2131 goto out;
2132 if (strcmp(gca->buf, gca->origin) == 0) {
2133 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2134 gca->numclones--;
2135 }
2136
2137 out:
2138 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2139 zfs_close(zhp);
2140 return (0);
2141 }
2142
2143 nvlist_t *
2144 zfs_get_clones_nvl(zfs_handle_t *zhp)
2145 {
2146 nvlist_t *nv, *value;
2147
2148 if (nvlist_lookup_nvlist(zhp->zfs_props,
2149 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2150 struct get_clones_arg gca;
2151
2152 /*
2153 * if this is a snapshot, then the kernel wasn't able
2154 * to get the clones. Do it by slowly iterating.
2155 */
2156 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2157 return (NULL);
2158 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2159 return (NULL);
2160 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2161 nvlist_free(nv);
2162 return (NULL);
2163 }
2164
2165 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2166 gca.value = value;
2167 gca.origin = zhp->zfs_name;
2168
2169 if (gca.numclones != 0) {
2170 zfs_handle_t *root;
2171 char pool[ZFS_MAXNAMELEN];
2172 char *cp = pool;
2173
2174 /* get the pool name */
2175 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2176 (void) strsep(&cp, "/@");
2177 root = zfs_open(zhp->zfs_hdl, pool,
2178 ZFS_TYPE_FILESYSTEM);
2179
2180 (void) get_clones_cb(root, &gca);
2181 }
2182
2183 if (gca.numclones != 0 ||
2184 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2185 nvlist_add_nvlist(zhp->zfs_props,
2186 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2187 nvlist_free(nv);
2188 nvlist_free(value);
2189 return (NULL);
2190 }
2191 nvlist_free(nv);
2192 nvlist_free(value);
2193 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2194 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2195 }
2196
2197 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2198
2199 return (value);
2200 }
2201
2202 /*
2203 * Retrieve a property from the given object. If 'literal' is specified, then
2204 * numbers are left as exact values. Otherwise, numbers are converted to a
2205 * human-readable form.
2206 *
2207 * Returns 0 on success, or -1 on error.
2208 */
2209 int
2210 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2211 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2212 {
2213 char *source = NULL;
2214 uint64_t val;
2215 char *str;
2216 const char *strval;
2217 boolean_t received = zfs_is_recvd_props_mode(zhp);
2218
2219 /*
2220 * Check to see if this property applies to our object
2221 */
2222 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2223 return (-1);
2224
2225 if (received && zfs_prop_readonly(prop))
2226 return (-1);
2227
2228 if (src)
2229 *src = ZPROP_SRC_NONE;
2230
2231 switch (prop) {
2232 case ZFS_PROP_CREATION:
2233 /*
2234 * 'creation' is a time_t stored in the statistics. We convert
2235 * this into a string unless 'literal' is specified.
2236 */
2237 {
2238 val = getprop_uint64(zhp, prop, &source);
2239 time_t time = (time_t)val;
2240 struct tm t;
2241
2242 if (literal ||
2243 localtime_r(&time, &t) == NULL ||
2244 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2245 &t) == 0)
2246 (void) snprintf(propbuf, proplen, "%llu",
2247 (u_longlong_t) val);
2248 }
2249 break;
2250
2251 case ZFS_PROP_MOUNTPOINT:
2252 /*
2253 * Getting the precise mountpoint can be tricky.
2254 *
2255 * - for 'none' or 'legacy', return those values.
2256 * - for inherited mountpoints, we want to take everything
2257 * after our ancestor and append it to the inherited value.
2258 *
2259 * If the pool has an alternate root, we want to prepend that
2260 * root to any values we return.
2261 */
2262
2263 str = getprop_string(zhp, prop, &source);
2264
2265 if (str[0] == '/') {
2266 char buf[MAXPATHLEN];
2267 char *root = buf;
2268 const char *relpath;
2269
2270 /*
2271 * If we inherit the mountpoint, even from a dataset
2272 * with a received value, the source will be the path of
2273 * the dataset we inherit from. If source is
2274 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2275 * inherited.
2276 */
2277 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2278 relpath = "";
2279 } else {
2280 relpath = zhp->zfs_name + strlen(source);
2281 if (relpath[0] == '/')
2282 relpath++;
2283 }
2284
2285 if ((zpool_get_prop(zhp->zpool_hdl,
2286 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2287 (strcmp(root, "-") == 0))
2288 root[0] = '\0';
2289 /*
2290 * Special case an alternate root of '/'. This will
2291 * avoid having multiple leading slashes in the
2292 * mountpoint path.
2293 */
2294 if (strcmp(root, "/") == 0)
2295 root++;
2296
2297 /*
2298 * If the mountpoint is '/' then skip over this
2299 * if we are obtaining either an alternate root or
2300 * an inherited mountpoint.
2301 */
2302 if (str[1] == '\0' && (root[0] != '\0' ||
2303 relpath[0] != '\0'))
2304 str++;
2305
2306 if (relpath[0] == '\0')
2307 (void) snprintf(propbuf, proplen, "%s%s",
2308 root, str);
2309 else
2310 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2311 root, str, relpath[0] == '@' ? "" : "/",
2312 relpath);
2313 } else {
2314 /* 'legacy' or 'none' */
2315 (void) strlcpy(propbuf, str, proplen);
2316 }
2317
2318 break;
2319
2320 case ZFS_PROP_ORIGIN:
2321 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2322 proplen);
2323 /*
2324 * If there is no parent at all, return failure to indicate that
2325 * it doesn't apply to this dataset.
2326 */
2327 if (propbuf[0] == '\0')
2328 return (-1);
2329 break;
2330
2331 case ZFS_PROP_CLONES:
2332 if (get_clones_string(zhp, propbuf, proplen) != 0)
2333 return (-1);
2334 break;
2335
2336 case ZFS_PROP_QUOTA:
2337 case ZFS_PROP_REFQUOTA:
2338 case ZFS_PROP_RESERVATION:
2339 case ZFS_PROP_REFRESERVATION:
2340
2341 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2342 return (-1);
2343
2344 /*
2345 * If quota or reservation is 0, we translate this into 'none'
2346 * (unless literal is set), and indicate that it's the default
2347 * value. Otherwise, we print the number nicely and indicate
2348 * that its set locally.
2349 */
2350 if (val == 0) {
2351 if (literal)
2352 (void) strlcpy(propbuf, "0", proplen);
2353 else
2354 (void) strlcpy(propbuf, "none", proplen);
2355 } else {
2356 if (literal)
2357 (void) snprintf(propbuf, proplen, "%llu",
2358 (u_longlong_t)val);
2359 else
2360 zfs_nicenum(val, propbuf, proplen);
2361 }
2362 break;
2363
2364 case ZFS_PROP_FILESYSTEM_LIMIT:
2365 case ZFS_PROP_SNAPSHOT_LIMIT:
2366 case ZFS_PROP_FILESYSTEM_COUNT:
2367 case ZFS_PROP_SNAPSHOT_COUNT:
2368
2369 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2370 return (-1);
2371
2372 /*
2373 * If limit is UINT64_MAX, we translate this into 'none' (unless
2374 * literal is set), and indicate that it's the default value.
2375 * Otherwise, we print the number nicely and indicate that it's
2376 * set locally.
2377 */
2378 if (literal) {
2379 (void) snprintf(propbuf, proplen, "%llu",
2380 (u_longlong_t)val);
2381 } else if (val == UINT64_MAX) {
2382 (void) strlcpy(propbuf, "none", proplen);
2383 } else {
2384 zfs_nicenum(val, propbuf, proplen);
2385 }
2386 break;
2387
2388 case ZFS_PROP_REFRATIO:
2389 case ZFS_PROP_COMPRESSRATIO:
2390 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2391 return (-1);
2392 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2393 (u_longlong_t)(val / 100),
2394 (u_longlong_t)(val % 100));
2395 break;
2396
2397 case ZFS_PROP_TYPE:
2398 switch (zhp->zfs_type) {
2399 case ZFS_TYPE_FILESYSTEM:
2400 str = "filesystem";
2401 break;
2402 case ZFS_TYPE_VOLUME:
2403 str = "volume";
2404 break;
2405 case ZFS_TYPE_SNAPSHOT:
2406 str = "snapshot";
2407 break;
2408 case ZFS_TYPE_BOOKMARK:
2409 str = "bookmark";
2410 break;
2411 default:
2412 abort();
2413 }
2414 (void) snprintf(propbuf, proplen, "%s", str);
2415 break;
2416
2417 case ZFS_PROP_MOUNTED:
2418 /*
2419 * The 'mounted' property is a pseudo-property that described
2420 * whether the filesystem is currently mounted. Even though
2421 * it's a boolean value, the typical values of "on" and "off"
2422 * don't make sense, so we translate to "yes" and "no".
2423 */
2424 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2425 src, &source, &val) != 0)
2426 return (-1);
2427 if (val)
2428 (void) strlcpy(propbuf, "yes", proplen);
2429 else
2430 (void) strlcpy(propbuf, "no", proplen);
2431 break;
2432
2433 case ZFS_PROP_NAME:
2434 /*
2435 * The 'name' property is a pseudo-property derived from the
2436 * dataset name. It is presented as a real property to simplify
2437 * consumers.
2438 */
2439 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2440 break;
2441
2442 case ZFS_PROP_MLSLABEL:
2443 {
2444 #ifdef HAVE_MLSLABEL
2445 m_label_t *new_sl = NULL;
2446 char *ascii = NULL; /* human readable label */
2447
2448 (void) strlcpy(propbuf,
2449 getprop_string(zhp, prop, &source), proplen);
2450
2451 if (literal || (strcasecmp(propbuf,
2452 ZFS_MLSLABEL_DEFAULT) == 0))
2453 break;
2454
2455 /*
2456 * Try to translate the internal hex string to
2457 * human-readable output. If there are any
2458 * problems just use the hex string.
2459 */
2460
2461 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2462 L_NO_CORRECTION, NULL) == -1) {
2463 m_label_free(new_sl);
2464 break;
2465 }
2466
2467 if (label_to_str(new_sl, &ascii, M_LABEL,
2468 DEF_NAMES) != 0) {
2469 if (ascii)
2470 free(ascii);
2471 m_label_free(new_sl);
2472 break;
2473 }
2474 m_label_free(new_sl);
2475
2476 (void) strlcpy(propbuf, ascii, proplen);
2477 free(ascii);
2478 #else
2479 (void) strlcpy(propbuf,
2480 getprop_string(zhp, prop, &source), proplen);
2481 #endif /* HAVE_MLSLABEL */
2482 }
2483 break;
2484
2485 case ZFS_PROP_GUID:
2486 /*
2487 * GUIDs are stored as numbers, but they are identifiers.
2488 * We don't want them to be pretty printed, because pretty
2489 * printing mangles the ID into a truncated and useless value.
2490 */
2491 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2492 return (-1);
2493 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2494 break;
2495
2496 default:
2497 switch (zfs_prop_get_type(prop)) {
2498 case PROP_TYPE_NUMBER:
2499 if (get_numeric_property(zhp, prop, src,
2500 &source, &val) != 0)
2501 return (-1);
2502 if (literal)
2503 (void) snprintf(propbuf, proplen, "%llu",
2504 (u_longlong_t)val);
2505 else
2506 zfs_nicenum(val, propbuf, proplen);
2507 break;
2508
2509 case PROP_TYPE_STRING:
2510 (void) strlcpy(propbuf,
2511 getprop_string(zhp, prop, &source), proplen);
2512 break;
2513
2514 case PROP_TYPE_INDEX:
2515 if (get_numeric_property(zhp, prop, src,
2516 &source, &val) != 0)
2517 return (-1);
2518 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2519 return (-1);
2520 (void) strlcpy(propbuf, strval, proplen);
2521 break;
2522
2523 default:
2524 abort();
2525 }
2526 }
2527
2528 get_source(zhp, src, source, statbuf, statlen);
2529
2530 return (0);
2531 }
2532
2533 /*
2534 * Utility function to get the given numeric property. Does no validation that
2535 * the given property is the appropriate type; should only be used with
2536 * hard-coded property types.
2537 */
2538 uint64_t
2539 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2540 {
2541 char *source;
2542 uint64_t val;
2543
2544 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2545
2546 return (val);
2547 }
2548
2549 int
2550 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2551 {
2552 char buf[64];
2553
2554 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2555 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2556 }
2557
2558 /*
2559 * Similar to zfs_prop_get(), but returns the value as an integer.
2560 */
2561 int
2562 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2563 zprop_source_t *src, char *statbuf, size_t statlen)
2564 {
2565 char *source;
2566
2567 /*
2568 * Check to see if this property applies to our object
2569 */
2570 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
2571 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2572 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2573 zfs_prop_to_name(prop)));
2574 }
2575
2576 if (src)
2577 *src = ZPROP_SRC_NONE;
2578
2579 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2580 return (-1);
2581
2582 get_source(zhp, src, source, statbuf, statlen);
2583
2584 return (0);
2585 }
2586
2587 #ifdef HAVE_IDMAP
2588 static int
2589 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2590 char **domainp, idmap_rid_t *ridp)
2591 {
2592 idmap_get_handle_t *get_hdl = NULL;
2593 idmap_stat status;
2594 int err = EINVAL;
2595
2596 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2597 goto out;
2598
2599 if (isuser) {
2600 err = idmap_get_sidbyuid(get_hdl, id,
2601 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2602 } else {
2603 err = idmap_get_sidbygid(get_hdl, id,
2604 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2605 }
2606 if (err == IDMAP_SUCCESS &&
2607 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2608 status == IDMAP_SUCCESS)
2609 err = 0;
2610 else
2611 err = EINVAL;
2612 out:
2613 if (get_hdl)
2614 idmap_get_destroy(get_hdl);
2615 return (err);
2616 }
2617 #endif /* HAVE_IDMAP */
2618
2619 /*
2620 * convert the propname into parameters needed by kernel
2621 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2622 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2623 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2624 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2625 */
2626 static int
2627 userquota_propname_decode(const char *propname, boolean_t zoned,
2628 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2629 {
2630 zfs_userquota_prop_t type;
2631 char *cp;
2632 boolean_t isuser;
2633 boolean_t isgroup;
2634 struct passwd *pw;
2635 struct group *gr;
2636
2637 domain[0] = '\0';
2638
2639 /* Figure out the property type ({user|group}{quota|space}) */
2640 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2641 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2642 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2643 break;
2644 }
2645 if (type == ZFS_NUM_USERQUOTA_PROPS)
2646 return (EINVAL);
2647 *typep = type;
2648
2649 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2650 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2651
2652 cp = strchr(propname, '@') + 1;
2653
2654 if (isuser && (pw = getpwnam(cp)) != NULL) {
2655 if (zoned && getzoneid() == GLOBAL_ZONEID)
2656 return (ENOENT);
2657 *ridp = pw->pw_uid;
2658 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2659 if (zoned && getzoneid() == GLOBAL_ZONEID)
2660 return (ENOENT);
2661 *ridp = gr->gr_gid;
2662 } else if (strchr(cp, '@')) {
2663 #ifdef HAVE_IDMAP
2664 /*
2665 * It's a SID name (eg "user@domain") that needs to be
2666 * turned into S-1-domainID-RID.
2667 */
2668 directory_error_t e;
2669 char *numericsid = NULL;
2670 char *end;
2671
2672 if (zoned && getzoneid() == GLOBAL_ZONEID)
2673 return (ENOENT);
2674 if (isuser) {
2675 e = directory_sid_from_user_name(NULL,
2676 cp, &numericsid);
2677 } else {
2678 e = directory_sid_from_group_name(NULL,
2679 cp, &numericsid);
2680 }
2681 if (e != NULL) {
2682 directory_error_free(e);
2683 return (ENOENT);
2684 }
2685 if (numericsid == NULL)
2686 return (ENOENT);
2687 cp = numericsid;
2688 (void) strlcpy(domain, cp, domainlen);
2689 cp = strrchr(domain, '-');
2690 *cp = '\0';
2691 cp++;
2692
2693 errno = 0;
2694 *ridp = strtoull(cp, &end, 10);
2695 free(numericsid);
2696
2697 if (errno != 0 || *end != '\0')
2698 return (EINVAL);
2699 #else
2700 return (ENOSYS);
2701 #endif /* HAVE_IDMAP */
2702 } else {
2703 /* It's a user/group ID (eg "12345"). */
2704 uid_t id;
2705 char *end;
2706 id = strtoul(cp, &end, 10);
2707 if (*end != '\0')
2708 return (EINVAL);
2709 if (id > MAXUID) {
2710 #ifdef HAVE_IDMAP
2711 /* It's an ephemeral ID. */
2712 idmap_rid_t rid;
2713 char *mapdomain;
2714
2715 if (idmap_id_to_numeric_domain_rid(id, isuser,
2716 &mapdomain, &rid) != 0)
2717 return (ENOENT);
2718 (void) strlcpy(domain, mapdomain, domainlen);
2719 *ridp = rid;
2720 #else
2721 return (ENOSYS);
2722 #endif /* HAVE_IDMAP */
2723 } else {
2724 *ridp = id;
2725 }
2726 }
2727
2728 return (0);
2729 }
2730
2731 static int
2732 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2733 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2734 {
2735 int err;
2736 zfs_cmd_t zc = {"\0"};
2737
2738 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2739
2740 err = userquota_propname_decode(propname,
2741 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2742 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2743 zc.zc_objset_type = *typep;
2744 if (err)
2745 return (err);
2746
2747 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2748 if (err)
2749 return (err);
2750
2751 *propvalue = zc.zc_cookie;
2752 return (0);
2753 }
2754
2755 int
2756 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2757 uint64_t *propvalue)
2758 {
2759 zfs_userquota_prop_t type;
2760
2761 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2762 &type));
2763 }
2764
2765 int
2766 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2767 char *propbuf, int proplen, boolean_t literal)
2768 {
2769 int err;
2770 uint64_t propvalue;
2771 zfs_userquota_prop_t type;
2772
2773 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2774 &type);
2775
2776 if (err)
2777 return (err);
2778
2779 if (literal) {
2780 (void) snprintf(propbuf, proplen, "%llu",
2781 (u_longlong_t)propvalue);
2782 } else if (propvalue == 0 &&
2783 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2784 (void) strlcpy(propbuf, "none", proplen);
2785 } else {
2786 zfs_nicenum(propvalue, propbuf, proplen);
2787 }
2788 return (0);
2789 }
2790
2791 int
2792 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2793 uint64_t *propvalue)
2794 {
2795 int err;
2796 zfs_cmd_t zc = {"\0"};
2797 const char *snapname;
2798
2799 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2800
2801 snapname = strchr(propname, '@') + 1;
2802 if (strchr(snapname, '@')) {
2803 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2804 } else {
2805 /* snapname is the short name, append it to zhp's fsname */
2806 char *cp;
2807
2808 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2809 sizeof (zc.zc_value));
2810 cp = strchr(zc.zc_value, '@');
2811 if (cp != NULL)
2812 *cp = '\0';
2813 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2814 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2815 }
2816
2817 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2818 if (err)
2819 return (err);
2820
2821 *propvalue = zc.zc_cookie;
2822 return (0);
2823 }
2824
2825 int
2826 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2827 char *propbuf, int proplen, boolean_t literal)
2828 {
2829 int err;
2830 uint64_t propvalue;
2831
2832 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2833
2834 if (err)
2835 return (err);
2836
2837 if (literal) {
2838 (void) snprintf(propbuf, proplen, "%llu",
2839 (u_longlong_t)propvalue);
2840 } else {
2841 zfs_nicenum(propvalue, propbuf, proplen);
2842 }
2843
2844 return (0);
2845 }
2846
2847 /*
2848 * Returns the name of the given zfs handle.
2849 */
2850 const char *
2851 zfs_get_name(const zfs_handle_t *zhp)
2852 {
2853 return (zhp->zfs_name);
2854 }
2855
2856 /*
2857 * Returns the type of the given zfs handle.
2858 */
2859 zfs_type_t
2860 zfs_get_type(const zfs_handle_t *zhp)
2861 {
2862 return (zhp->zfs_type);
2863 }
2864
2865 /*
2866 * Is one dataset name a child dataset of another?
2867 *
2868 * Needs to handle these cases:
2869 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2870 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2871 * Descendant? No. No. No. Yes.
2872 */
2873 static boolean_t
2874 is_descendant(const char *ds1, const char *ds2)
2875 {
2876 size_t d1len = strlen(ds1);
2877
2878 /* ds2 can't be a descendant if it's smaller */
2879 if (strlen(ds2) < d1len)
2880 return (B_FALSE);
2881
2882 /* otherwise, compare strings and verify that there's a '/' char */
2883 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2884 }
2885
2886 /*
2887 * Given a complete name, return just the portion that refers to the parent.
2888 * Will return -1 if there is no parent (path is just the name of the
2889 * pool).
2890 */
2891 static int
2892 parent_name(const char *path, char *buf, size_t buflen)
2893 {
2894 char *slashp;
2895
2896 (void) strlcpy(buf, path, buflen);
2897
2898 if ((slashp = strrchr(buf, '/')) == NULL)
2899 return (-1);
2900 *slashp = '\0';
2901
2902 return (0);
2903 }
2904
2905 /*
2906 * If accept_ancestor is false, then check to make sure that the given path has
2907 * a parent, and that it exists. If accept_ancestor is true, then find the
2908 * closest existing ancestor for the given path. In prefixlen return the
2909 * length of already existing prefix of the given path. We also fetch the
2910 * 'zoned' property, which is used to validate property settings when creating
2911 * new datasets.
2912 */
2913 static int
2914 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2915 boolean_t accept_ancestor, int *prefixlen)
2916 {
2917 zfs_cmd_t zc = {"\0"};
2918 char parent[ZFS_MAXNAMELEN];
2919 char *slash;
2920 zfs_handle_t *zhp;
2921 char errbuf[1024];
2922 uint64_t is_zoned;
2923
2924 (void) snprintf(errbuf, sizeof (errbuf),
2925 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2926
2927 /* get parent, and check to see if this is just a pool */
2928 if (parent_name(path, parent, sizeof (parent)) != 0) {
2929 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2930 "missing dataset name"));
2931 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2932 }
2933
2934 /* check to see if the pool exists */
2935 if ((slash = strchr(parent, '/')) == NULL)
2936 slash = parent + strlen(parent);
2937 (void) strncpy(zc.zc_name, parent, slash - parent);
2938 zc.zc_name[slash - parent] = '\0';
2939 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2940 errno == ENOENT) {
2941 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2942 "no such pool '%s'"), zc.zc_name);
2943 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2944 }
2945
2946 /* check to see if the parent dataset exists */
2947 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2948 if (errno == ENOENT && accept_ancestor) {
2949 /*
2950 * Go deeper to find an ancestor, give up on top level.
2951 */
2952 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2954 "no such pool '%s'"), zc.zc_name);
2955 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2956 }
2957 } else if (errno == ENOENT) {
2958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2959 "parent does not exist"));
2960 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2961 } else
2962 return (zfs_standard_error(hdl, errno, errbuf));
2963 }
2964
2965 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2966 if (zoned != NULL)
2967 *zoned = is_zoned;
2968
2969 /* we are in a non-global zone, but parent is in the global zone */
2970 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2971 (void) zfs_standard_error(hdl, EPERM, errbuf);
2972 zfs_close(zhp);
2973 return (-1);
2974 }
2975
2976 /* make sure parent is a filesystem */
2977 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2979 "parent is not a filesystem"));
2980 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2981 zfs_close(zhp);
2982 return (-1);
2983 }
2984
2985 zfs_close(zhp);
2986 if (prefixlen != NULL)
2987 *prefixlen = strlen(parent);
2988 return (0);
2989 }
2990
2991 /*
2992 * Finds whether the dataset of the given type(s) exists.
2993 */
2994 boolean_t
2995 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2996 {
2997 zfs_handle_t *zhp;
2998
2999 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3000 return (B_FALSE);
3001
3002 /*
3003 * Try to get stats for the dataset, which will tell us if it exists.
3004 */
3005 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3006 int ds_type = zhp->zfs_type;
3007
3008 zfs_close(zhp);
3009 if (types & ds_type)
3010 return (B_TRUE);
3011 }
3012 return (B_FALSE);
3013 }
3014
3015 /*
3016 * Given a path to 'target', create all the ancestors between
3017 * the prefixlen portion of the path, and the target itself.
3018 * Fail if the initial prefixlen-ancestor does not already exist.
3019 */
3020 int
3021 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3022 {
3023 zfs_handle_t *h;
3024 char *cp;
3025 const char *opname;
3026
3027 /* make sure prefix exists */
3028 cp = target + prefixlen;
3029 if (*cp != '/') {
3030 assert(strchr(cp, '/') == NULL);
3031 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3032 } else {
3033 *cp = '\0';
3034 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3035 *cp = '/';
3036 }
3037 if (h == NULL)
3038 return (-1);
3039 zfs_close(h);
3040
3041 /*
3042 * Attempt to create, mount, and share any ancestor filesystems,
3043 * up to the prefixlen-long one.
3044 */
3045 for (cp = target + prefixlen + 1;
3046 (cp = strchr(cp, '/')); *cp = '/', cp++) {
3047
3048 *cp = '\0';
3049
3050 h = make_dataset_handle(hdl, target);
3051 if (h) {
3052 /* it already exists, nothing to do here */
3053 zfs_close(h);
3054 continue;
3055 }
3056
3057 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3058 NULL) != 0) {
3059 opname = dgettext(TEXT_DOMAIN, "create");
3060 goto ancestorerr;
3061 }
3062
3063 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3064 if (h == NULL) {
3065 opname = dgettext(TEXT_DOMAIN, "open");
3066 goto ancestorerr;
3067 }
3068
3069 if (zfs_mount(h, NULL, 0) != 0) {
3070 opname = dgettext(TEXT_DOMAIN, "mount");
3071 goto ancestorerr;
3072 }
3073
3074 if (zfs_share(h) != 0) {
3075 opname = dgettext(TEXT_DOMAIN, "share");
3076 goto ancestorerr;
3077 }
3078
3079 zfs_close(h);
3080 }
3081
3082 return (0);
3083
3084 ancestorerr:
3085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3086 "failed to %s ancestor '%s'"), opname, target);
3087 return (-1);
3088 }
3089
3090 /*
3091 * Creates non-existing ancestors of the given path.
3092 */
3093 int
3094 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3095 {
3096 int prefix;
3097 char *path_copy;
3098 int rc = 0;
3099
3100 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3101 return (-1);
3102
3103 if ((path_copy = strdup(path)) != NULL) {
3104 rc = create_parents(hdl, path_copy, prefix);
3105 free(path_copy);
3106 }
3107 if (path_copy == NULL || rc != 0)
3108 return (-1);
3109
3110 return (0);
3111 }
3112
3113 /*
3114 * Create a new filesystem or volume.
3115 */
3116 int
3117 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3118 nvlist_t *props)
3119 {
3120 int ret;
3121 uint64_t size = 0;
3122 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3123 char errbuf[1024];
3124 uint64_t zoned;
3125 dmu_objset_type_t ost;
3126
3127 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3128 "cannot create '%s'"), path);
3129
3130 /* validate the path, taking care to note the extended error message */
3131 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3132 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3133
3134 /* validate parents exist */
3135 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3136 return (-1);
3137
3138 /*
3139 * The failure modes when creating a dataset of a different type over
3140 * one that already exists is a little strange. In particular, if you
3141 * try to create a dataset on top of an existing dataset, the ioctl()
3142 * will return ENOENT, not EEXIST. To prevent this from happening, we
3143 * first try to see if the dataset exists.
3144 */
3145 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3146 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3147 "dataset already exists"));
3148 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3149 }
3150
3151 if (type == ZFS_TYPE_VOLUME)
3152 ost = DMU_OST_ZVOL;
3153 else
3154 ost = DMU_OST_ZFS;
3155
3156 if (props && (props = zfs_valid_proplist(hdl, type, props,
3157 zoned, NULL, errbuf)) == 0)
3158 return (-1);
3159
3160 if (type == ZFS_TYPE_VOLUME) {
3161 /*
3162 * If we are creating a volume, the size and block size must
3163 * satisfy a few restraints. First, the blocksize must be a
3164 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3165 * volsize must be a multiple of the block size, and cannot be
3166 * zero.
3167 */
3168 if (props == NULL || nvlist_lookup_uint64(props,
3169 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3170 nvlist_free(props);
3171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3172 "missing volume size"));
3173 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3174 }
3175
3176 if ((ret = nvlist_lookup_uint64(props,
3177 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3178 &blocksize)) != 0) {
3179 if (ret == ENOENT) {
3180 blocksize = zfs_prop_default_numeric(
3181 ZFS_PROP_VOLBLOCKSIZE);
3182 } else {
3183 nvlist_free(props);
3184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3185 "missing volume block size"));
3186 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3187 }
3188 }
3189
3190 if (size == 0) {
3191 nvlist_free(props);
3192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3193 "volume size cannot be zero"));
3194 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3195 }
3196
3197 if (size % blocksize != 0) {
3198 nvlist_free(props);
3199 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3200 "volume size must be a multiple of volume block "
3201 "size"));
3202 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3203 }
3204 }
3205
3206 /* create the dataset */
3207 ret = lzc_create(path, ost, props);
3208 nvlist_free(props);
3209
3210 /* check for failure */
3211 if (ret != 0) {
3212 char parent[ZFS_MAXNAMELEN];
3213 (void) parent_name(path, parent, sizeof (parent));
3214
3215 switch (errno) {
3216 case ENOENT:
3217 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3218 "no such parent '%s'"), parent);
3219 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3220
3221 case EINVAL:
3222 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3223 "parent '%s' is not a filesystem"), parent);
3224 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3225
3226 case EDOM:
3227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3228 "volume block size must be power of 2 from "
3229 "512B to %uKB"), zfs_max_recordsize >> 10);
3230
3231 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3232
3233 case ENOTSUP:
3234 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3235 "pool must be upgraded to set this "
3236 "property or value"));
3237 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3238 #ifdef _ILP32
3239 case EOVERFLOW:
3240 /*
3241 * This platform can't address a volume this big.
3242 */
3243 if (type == ZFS_TYPE_VOLUME)
3244 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3245 errbuf));
3246 #endif
3247 /* FALLTHROUGH */
3248 default:
3249 return (zfs_standard_error(hdl, errno, errbuf));
3250 }
3251 }
3252
3253 return (0);
3254 }
3255
3256 /*
3257 * Destroys the given dataset. The caller must make sure that the filesystem
3258 * isn't mounted, and that there are no active dependents. If the file system
3259 * does not exist this function does nothing.
3260 */
3261 int
3262 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3263 {
3264 zfs_cmd_t zc = {"\0"};
3265
3266 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3267 nvlist_t *nv = fnvlist_alloc();
3268 fnvlist_add_boolean(nv, zhp->zfs_name);
3269 int error = lzc_destroy_bookmarks(nv, NULL);
3270 fnvlist_free(nv);
3271 if (error != 0) {
3272 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3273 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3274 zhp->zfs_name));
3275 }
3276 return (0);
3277 }
3278
3279 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3280
3281 if (ZFS_IS_VOLUME(zhp)) {
3282 zc.zc_objset_type = DMU_OST_ZVOL;
3283 } else {
3284 zc.zc_objset_type = DMU_OST_ZFS;
3285 }
3286
3287 zc.zc_defer_destroy = defer;
3288 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3289 errno != ENOENT) {
3290 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3291 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3292 zhp->zfs_name));
3293 }
3294
3295 remove_mountpoint(zhp);
3296
3297 return (0);
3298 }
3299
3300 struct destroydata {
3301 nvlist_t *nvl;
3302 const char *snapname;
3303 };
3304
3305 static int
3306 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3307 {
3308 struct destroydata *dd = arg;
3309 char name[ZFS_MAXNAMELEN];
3310 int rv = 0;
3311
3312 (void) snprintf(name, sizeof (name),
3313 "%s@%s", zhp->zfs_name, dd->snapname);
3314
3315 if (lzc_exists(name))
3316 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3317
3318 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3319 zfs_close(zhp);
3320 return (rv);
3321 }
3322
3323 /*
3324 * Destroys all snapshots with the given name in zhp & descendants.
3325 */
3326 int
3327 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3328 {
3329 int ret;
3330 struct destroydata dd = { 0 };
3331
3332 dd.snapname = snapname;
3333 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3334 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3335
3336 if (nvlist_empty(dd.nvl)) {
3337 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3338 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3339 zhp->zfs_name, snapname);
3340 } else {
3341 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3342 }
3343 nvlist_free(dd.nvl);
3344 return (ret);
3345 }
3346
3347 /*
3348 * Destroys all the snapshots named in the nvlist.
3349 */
3350 int
3351 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3352 {
3353 int ret;
3354 nvlist_t *errlist;
3355 nvpair_t *pair;
3356
3357 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3358
3359 if (ret == 0)
3360 return (0);
3361
3362 if (nvlist_empty(errlist)) {
3363 char errbuf[1024];
3364 (void) snprintf(errbuf, sizeof (errbuf),
3365 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3366
3367 ret = zfs_standard_error(hdl, ret, errbuf);
3368 }
3369 for (pair = nvlist_next_nvpair(errlist, NULL);
3370 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3371 char errbuf[1024];
3372 (void) snprintf(errbuf, sizeof (errbuf),
3373 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3374 nvpair_name(pair));
3375
3376 switch (fnvpair_value_int32(pair)) {
3377 case EEXIST:
3378 zfs_error_aux(hdl,
3379 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3380 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3381 break;
3382 default:
3383 ret = zfs_standard_error(hdl, errno, errbuf);
3384 break;
3385 }
3386 }
3387
3388 return (ret);
3389 }
3390
3391 /*
3392 * Clones the given dataset. The target must be of the same type as the source.
3393 */
3394 int
3395 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3396 {
3397 char parent[ZFS_MAXNAMELEN];
3398 int ret;
3399 char errbuf[1024];
3400 libzfs_handle_t *hdl = zhp->zfs_hdl;
3401 uint64_t zoned;
3402
3403 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3404
3405 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3406 "cannot create '%s'"), target);
3407
3408 /* validate the target/clone name */
3409 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3410 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3411
3412 /* validate parents exist */
3413 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3414 return (-1);
3415
3416 (void) parent_name(target, parent, sizeof (parent));
3417
3418 /* do the clone */
3419
3420 if (props) {
3421 zfs_type_t type;
3422 if (ZFS_IS_VOLUME(zhp)) {
3423 type = ZFS_TYPE_VOLUME;
3424 } else {
3425 type = ZFS_TYPE_FILESYSTEM;
3426 }
3427 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3428 zhp, errbuf)) == NULL)
3429 return (-1);
3430 }
3431
3432 ret = lzc_clone(target, zhp->zfs_name, props);
3433 nvlist_free(props);
3434
3435 if (ret != 0) {
3436 switch (errno) {
3437
3438 case ENOENT:
3439 /*
3440 * The parent doesn't exist. We should have caught this
3441 * above, but there may a race condition that has since
3442 * destroyed the parent.
3443 *
3444 * At this point, we don't know whether it's the source
3445 * that doesn't exist anymore, or whether the target
3446 * dataset doesn't exist.
3447 */
3448 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3449 "no such parent '%s'"), parent);
3450 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3451
3452 case EXDEV:
3453 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3454 "source and target pools differ"));
3455 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3456 errbuf));
3457
3458 default:
3459 return (zfs_standard_error(zhp->zfs_hdl, errno,
3460 errbuf));
3461 }
3462 }
3463
3464 return (ret);
3465 }
3466
3467 /*
3468 * Promotes the given clone fs to be the clone parent.
3469 */
3470 int
3471 zfs_promote(zfs_handle_t *zhp)
3472 {
3473 libzfs_handle_t *hdl = zhp->zfs_hdl;
3474 zfs_cmd_t zc = {"\0"};
3475 char parent[MAXPATHLEN];
3476 int ret;
3477 char errbuf[1024];
3478
3479 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3480 "cannot promote '%s'"), zhp->zfs_name);
3481
3482 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3484 "snapshots can not be promoted"));
3485 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3486 }
3487
3488 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3489 if (parent[0] == '\0') {
3490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3491 "not a cloned filesystem"));
3492 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3493 }
3494
3495 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3496 sizeof (zc.zc_value));
3497 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3498 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3499
3500 if (ret != 0) {
3501 int save_errno = errno;
3502
3503 switch (save_errno) {
3504 case EEXIST:
3505 /* There is a conflicting snapshot name. */
3506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3507 "conflicting snapshot '%s' from parent '%s'"),
3508 zc.zc_string, parent);
3509 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3510
3511 default:
3512 return (zfs_standard_error(hdl, save_errno, errbuf));
3513 }
3514 }
3515 return (ret);
3516 }
3517
3518 typedef struct snapdata {
3519 nvlist_t *sd_nvl;
3520 const char *sd_snapname;
3521 } snapdata_t;
3522
3523 static int
3524 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3525 {
3526 snapdata_t *sd = arg;
3527 char name[ZFS_MAXNAMELEN];
3528 int rv = 0;
3529
3530 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3531 (void) snprintf(name, sizeof (name),
3532 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3533
3534 fnvlist_add_boolean(sd->sd_nvl, name);
3535
3536 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3537 }
3538 zfs_close(zhp);
3539
3540 return (rv);
3541 }
3542
3543 /*
3544 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3545 * created.
3546 */
3547 int
3548 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3549 {
3550 int ret;
3551 char errbuf[1024];
3552 nvpair_t *elem;
3553 nvlist_t *errors;
3554
3555 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3556 "cannot create snapshots "));
3557
3558 elem = NULL;
3559 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3560 const char *snapname = nvpair_name(elem);
3561
3562 /* validate the target name */
3563 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3564 B_TRUE)) {
3565 (void) snprintf(errbuf, sizeof (errbuf),
3566 dgettext(TEXT_DOMAIN,
3567 "cannot create snapshot '%s'"), snapname);
3568 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3569 }
3570 }
3571
3572 if (props != NULL &&
3573 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3574 props, B_FALSE, NULL, errbuf)) == NULL) {
3575 return (-1);
3576 }
3577
3578 ret = lzc_snapshot(snaps, props, &errors);
3579
3580 if (ret != 0) {
3581 boolean_t printed = B_FALSE;
3582 for (elem = nvlist_next_nvpair(errors, NULL);
3583 elem != NULL;
3584 elem = nvlist_next_nvpair(errors, elem)) {
3585 (void) snprintf(errbuf, sizeof (errbuf),
3586 dgettext(TEXT_DOMAIN,
3587 "cannot create snapshot '%s'"), nvpair_name(elem));
3588 (void) zfs_standard_error(hdl,
3589 fnvpair_value_int32(elem), errbuf);
3590 printed = B_TRUE;
3591 }
3592 if (!printed) {
3593 switch (ret) {
3594 case EXDEV:
3595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3596 "multiple snapshots of same "
3597 "fs not allowed"));
3598 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3599
3600 break;
3601 default:
3602 (void) zfs_standard_error(hdl, ret, errbuf);
3603 }
3604 }
3605 }
3606
3607 nvlist_free(props);
3608 nvlist_free(errors);
3609 return (ret);
3610 }
3611
3612 int
3613 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3614 nvlist_t *props)
3615 {
3616 int ret;
3617 snapdata_t sd = { 0 };
3618 char fsname[ZFS_MAXNAMELEN];
3619 char *cp;
3620 zfs_handle_t *zhp;
3621 char errbuf[1024];
3622
3623 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3624 "cannot snapshot %s"), path);
3625
3626 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3627 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3628
3629 (void) strlcpy(fsname, path, sizeof (fsname));
3630 cp = strchr(fsname, '@');
3631 *cp = '\0';
3632 sd.sd_snapname = cp + 1;
3633
3634 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3635 ZFS_TYPE_VOLUME)) == NULL) {
3636 return (-1);
3637 }
3638
3639 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3640 if (recursive) {
3641 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3642 } else {
3643 fnvlist_add_boolean(sd.sd_nvl, path);
3644 }
3645
3646 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3647 nvlist_free(sd.sd_nvl);
3648 zfs_close(zhp);
3649 return (ret);
3650 }
3651
3652 /*
3653 * Destroy any more recent snapshots. We invoke this callback on any dependents
3654 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3655 * is a dependent and we should just destroy it without checking the transaction
3656 * group.
3657 */
3658 typedef struct rollback_data {
3659 const char *cb_target; /* the snapshot */
3660 uint64_t cb_create; /* creation time reference */
3661 boolean_t cb_error;
3662 boolean_t cb_force;
3663 } rollback_data_t;
3664
3665 static int
3666 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3667 {
3668 rollback_data_t *cbp = data;
3669 prop_changelist_t *clp;
3670
3671 /* We must destroy this clone; first unmount it */
3672 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3673 cbp->cb_force ? MS_FORCE: 0);
3674 if (clp == NULL || changelist_prefix(clp) != 0) {
3675 cbp->cb_error = B_TRUE;
3676 zfs_close(zhp);
3677 return (0);
3678 }
3679 if (zfs_destroy(zhp, B_FALSE) != 0)
3680 cbp->cb_error = B_TRUE;
3681 else
3682 changelist_remove(clp, zhp->zfs_name);
3683 (void) changelist_postfix(clp);
3684 changelist_free(clp);
3685
3686 zfs_close(zhp);
3687 return (0);
3688 }
3689
3690 static int
3691 rollback_destroy(zfs_handle_t *zhp, void *data)
3692 {
3693 rollback_data_t *cbp = data;
3694
3695 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3696 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3697 rollback_destroy_dependent, cbp);
3698
3699 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3700 }
3701
3702 zfs_close(zhp);
3703 return (0);
3704 }
3705
3706 /*
3707 * Given a dataset, rollback to a specific snapshot, discarding any
3708 * data changes since then and making it the active dataset.
3709 *
3710 * Any snapshots and bookmarks more recent than the target are
3711 * destroyed, along with their dependents (i.e. clones).
3712 */
3713 int
3714 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3715 {
3716 rollback_data_t cb = { 0 };
3717 int err;
3718 boolean_t restore_resv = 0;
3719 uint64_t old_volsize = 0, new_volsize;
3720 zfs_prop_t resv_prop = { 0 };
3721
3722 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3723 zhp->zfs_type == ZFS_TYPE_VOLUME);
3724
3725 /*
3726 * Destroy all recent snapshots and their dependents.
3727 */
3728 cb.cb_force = force;
3729 cb.cb_target = snap->zfs_name;
3730 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3731 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3732 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3733
3734 if (cb.cb_error)
3735 return (-1);
3736
3737 /*
3738 * Now that we have verified that the snapshot is the latest,
3739 * rollback to the given snapshot.
3740 */
3741
3742 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3743 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3744 return (-1);
3745 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3746 restore_resv =
3747 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3748 }
3749
3750 /*
3751 * We rely on zfs_iter_children() to verify that there are no
3752 * newer snapshots for the given dataset. Therefore, we can
3753 * simply pass the name on to the ioctl() call. There is still
3754 * an unlikely race condition where the user has taken a
3755 * snapshot since we verified that this was the most recent.
3756 */
3757 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3758 if (err != 0) {
3759 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3760 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3761 zhp->zfs_name);
3762 return (err);
3763 }
3764
3765 /*
3766 * For volumes, if the pre-rollback volsize matched the pre-
3767 * rollback reservation and the volsize has changed then set
3768 * the reservation property to the post-rollback volsize.
3769 * Make a new handle since the rollback closed the dataset.
3770 */
3771 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3772 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3773 if (restore_resv) {
3774 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3775 if (old_volsize != new_volsize)
3776 err = zfs_prop_set_int(zhp, resv_prop,
3777 new_volsize);
3778 }
3779 zfs_close(zhp);
3780 }
3781 return (err);
3782 }
3783
3784 /*
3785 * Renames the given dataset.
3786 */
3787 int
3788 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3789 boolean_t force_unmount)
3790 {
3791 int ret;
3792 zfs_cmd_t zc = {"\0"};
3793 char *delim;
3794 prop_changelist_t *cl = NULL;
3795 zfs_handle_t *zhrp = NULL;
3796 char *parentname = NULL;
3797 char parent[ZFS_MAXNAMELEN];
3798 libzfs_handle_t *hdl = zhp->zfs_hdl;
3799 char errbuf[1024];
3800
3801 /* if we have the same exact name, just return success */
3802 if (strcmp(zhp->zfs_name, target) == 0)
3803 return (0);
3804
3805 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3806 "cannot rename to '%s'"), target);
3807
3808 /*
3809 * Make sure the target name is valid
3810 */
3811 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3812 if ((strchr(target, '@') == NULL) ||
3813 *target == '@') {
3814 /*
3815 * Snapshot target name is abbreviated,
3816 * reconstruct full dataset name
3817 */
3818 (void) strlcpy(parent, zhp->zfs_name,
3819 sizeof (parent));
3820 delim = strchr(parent, '@');
3821 if (strchr(target, '@') == NULL)
3822 *(++delim) = '\0';
3823 else
3824 *delim = '\0';
3825 (void) strlcat(parent, target, sizeof (parent));
3826 target = parent;
3827 } else {
3828 /*
3829 * Make sure we're renaming within the same dataset.
3830 */
3831 delim = strchr(target, '@');
3832 if (strncmp(zhp->zfs_name, target, delim - target)
3833 != 0 || zhp->zfs_name[delim - target] != '@') {
3834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3835 "snapshots must be part of same "
3836 "dataset"));
3837 return (zfs_error(hdl, EZFS_CROSSTARGET,
3838 errbuf));
3839 }
3840 }
3841 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3842 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3843 } else {
3844 if (recursive) {
3845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3846 "recursive rename must be a snapshot"));
3847 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3848 }
3849
3850 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3851 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3852
3853 /* validate parents */
3854 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3855 return (-1);
3856
3857 /* make sure we're in the same pool */
3858 verify((delim = strchr(target, '/')) != NULL);
3859 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3860 zhp->zfs_name[delim - target] != '/') {
3861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3862 "datasets must be within same pool"));
3863 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3864 }
3865
3866 /* new name cannot be a child of the current dataset name */
3867 if (is_descendant(zhp->zfs_name, target)) {
3868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3869 "New dataset name cannot be a descendant of "
3870 "current dataset name"));
3871 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3872 }
3873 }
3874
3875 (void) snprintf(errbuf, sizeof (errbuf),
3876 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3877
3878 if (getzoneid() == GLOBAL_ZONEID &&
3879 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3880 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3881 "dataset is used in a non-global zone"));
3882 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3883 }
3884
3885 if (recursive) {
3886
3887 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3888 if (parentname == NULL) {
3889 ret = -1;
3890 goto error;
3891 }
3892 delim = strchr(parentname, '@');
3893 *delim = '\0';
3894 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3895 if (zhrp == NULL) {
3896 ret = -1;
3897 goto error;
3898 }
3899
3900 } else {
3901 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3902 force_unmount ? MS_FORCE : 0)) == NULL)
3903 return (-1);
3904
3905 if (changelist_haszonedchild(cl)) {
3906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3907 "child dataset with inherited mountpoint is used "
3908 "in a non-global zone"));
3909 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3910 ret = -1;
3911 goto error;
3912 }
3913
3914 if ((ret = changelist_prefix(cl)) != 0)
3915 goto error;
3916 }
3917
3918 if (ZFS_IS_VOLUME(zhp))
3919 zc.zc_objset_type = DMU_OST_ZVOL;
3920 else
3921 zc.zc_objset_type = DMU_OST_ZFS;
3922
3923 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3924 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3925
3926 zc.zc_cookie = recursive;
3927
3928 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3929 /*
3930 * if it was recursive, the one that actually failed will
3931 * be in zc.zc_name
3932 */
3933 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3934 "cannot rename '%s'"), zc.zc_name);
3935
3936 if (recursive && errno == EEXIST) {
3937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3938 "a child dataset already has a snapshot "
3939 "with the new name"));
3940 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3941 } else {
3942 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3943 }
3944
3945 /*
3946 * On failure, we still want to remount any filesystems that
3947 * were previously mounted, so we don't alter the system state.
3948 */
3949 if (!recursive)
3950 (void) changelist_postfix(cl);
3951 } else {
3952 if (!recursive) {
3953 changelist_rename(cl, zfs_get_name(zhp), target);
3954 ret = changelist_postfix(cl);
3955 }
3956 }
3957
3958 error:
3959 if (parentname) {
3960 free(parentname);
3961 }
3962 if (zhrp) {
3963 zfs_close(zhrp);
3964 }
3965 if (cl) {
3966 changelist_free(cl);
3967 }
3968 return (ret);
3969 }
3970
3971 nvlist_t *
3972 zfs_get_user_props(zfs_handle_t *zhp)
3973 {
3974 return (zhp->zfs_user_props);
3975 }
3976
3977 /*
3978 * This function is used by 'zfs list' to determine the exact set of columns to
3979 * display, and their maximum widths. This does two main things:
3980 *
3981 * - If this is a list of all properties, then expand the list to include
3982 * all native properties, and set a flag so that for each dataset we look
3983 * for new unique user properties and add them to the list.
3984 *
3985 * - For non fixed-width properties, keep track of the maximum width seen
3986 * so that we can size the column appropriately. If the user has
3987 * requested received property values, we also need to compute the width
3988 * of the RECEIVED column.
3989 */
3990 int
3991 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
3992 boolean_t literal)
3993 {
3994 libzfs_handle_t *hdl = zhp->zfs_hdl;
3995 zprop_list_t *entry;
3996 zprop_list_t **last, **start;
3997 nvlist_t *userprops, *propval;
3998 nvpair_t *elem;
3999 char *strval;
4000 char buf[ZFS_MAXPROPLEN];
4001
4002 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4003 return (-1);
4004
4005 userprops = zfs_get_user_props(zhp);
4006
4007 entry = *plp;
4008 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4009 /*
4010 * Go through and add any user properties as necessary. We
4011 * start by incrementing our list pointer to the first
4012 * non-native property.
4013 */
4014 start = plp;
4015 while (*start != NULL) {
4016 if ((*start)->pl_prop == ZPROP_INVAL)
4017 break;
4018 start = &(*start)->pl_next;
4019 }
4020
4021 elem = NULL;
4022 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4023 /*
4024 * See if we've already found this property in our list.
4025 */
4026 for (last = start; *last != NULL;
4027 last = &(*last)->pl_next) {
4028 if (strcmp((*last)->pl_user_prop,
4029 nvpair_name(elem)) == 0)
4030 break;
4031 }
4032
4033 if (*last == NULL) {
4034 if ((entry = zfs_alloc(hdl,
4035 sizeof (zprop_list_t))) == NULL ||
4036 ((entry->pl_user_prop = zfs_strdup(hdl,
4037 nvpair_name(elem)))) == NULL) {
4038 free(entry);
4039 return (-1);
4040 }
4041
4042 entry->pl_prop = ZPROP_INVAL;
4043 entry->pl_width = strlen(nvpair_name(elem));
4044 entry->pl_all = B_TRUE;
4045 *last = entry;
4046 }
4047 }
4048 }
4049
4050 /*
4051 * Now go through and check the width of any non-fixed columns
4052 */
4053 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4054 if (entry->pl_fixed && !literal)
4055 continue;
4056
4057 if (entry->pl_prop != ZPROP_INVAL) {
4058 if (zfs_prop_get(zhp, entry->pl_prop,
4059 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4060 if (strlen(buf) > entry->pl_width)
4061 entry->pl_width = strlen(buf);
4062 }
4063 if (received && zfs_prop_get_recvd(zhp,
4064 zfs_prop_to_name(entry->pl_prop),
4065 buf, sizeof (buf), literal) == 0)
4066 if (strlen(buf) > entry->pl_recvd_width)
4067 entry->pl_recvd_width = strlen(buf);
4068 } else {
4069 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4070 &propval) == 0) {
4071 verify(nvlist_lookup_string(propval,
4072 ZPROP_VALUE, &strval) == 0);
4073 if (strlen(strval) > entry->pl_width)
4074 entry->pl_width = strlen(strval);
4075 }
4076 if (received && zfs_prop_get_recvd(zhp,
4077 entry->pl_user_prop,
4078 buf, sizeof (buf), literal) == 0)
4079 if (strlen(buf) > entry->pl_recvd_width)
4080 entry->pl_recvd_width = strlen(buf);
4081 }
4082 }
4083
4084 return (0);
4085 }
4086
4087 void
4088 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4089 {
4090 nvpair_t *curr;
4091
4092 /*
4093 * Keep a reference to the props-table against which we prune the
4094 * properties.
4095 */
4096 zhp->zfs_props_table = props;
4097
4098 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4099
4100 while (curr) {
4101 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4102 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4103
4104 /*
4105 * User properties will result in ZPROP_INVAL, and since we
4106 * only know how to prune standard ZFS properties, we always
4107 * leave these in the list. This can also happen if we
4108 * encounter an unknown DSL property (when running older
4109 * software, for example).
4110 */
4111 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4112 (void) nvlist_remove(zhp->zfs_props,
4113 nvpair_name(curr), nvpair_type(curr));
4114 curr = next;
4115 }
4116 }
4117
4118 static int
4119 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4120 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4121 {
4122 zfs_cmd_t zc = {"\0"};
4123 nvlist_t *nvlist = NULL;
4124 int error;
4125
4126 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4127 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4128 zc.zc_cookie = (uint64_t)cmd;
4129
4130 if (cmd == ZFS_SMB_ACL_RENAME) {
4131 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4132 (void) no_memory(hdl);
4133 return (-1);
4134 }
4135 }
4136
4137 switch (cmd) {
4138 case ZFS_SMB_ACL_ADD:
4139 case ZFS_SMB_ACL_REMOVE:
4140 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4141 break;
4142 case ZFS_SMB_ACL_RENAME:
4143 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4144 resource1) != 0) {
4145 (void) no_memory(hdl);
4146 return (-1);
4147 }
4148 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4149 resource2) != 0) {
4150 (void) no_memory(hdl);
4151 return (-1);
4152 }
4153 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4154 nvlist_free(nvlist);
4155 return (-1);
4156 }
4157 break;
4158 case ZFS_SMB_ACL_PURGE:
4159 break;
4160 default:
4161 return (-1);
4162 }
4163 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4164 if (nvlist)
4165 nvlist_free(nvlist);
4166 return (error);
4167 }
4168
4169 int
4170 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4171 char *path, char *resource)
4172 {
4173 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4174 resource, NULL));
4175 }
4176
4177 int
4178 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4179 char *path, char *resource)
4180 {
4181 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4182 resource, NULL));
4183 }
4184
4185 int
4186 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4187 {
4188 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4189 NULL, NULL));
4190 }
4191
4192 int
4193 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4194 char *oldname, char *newname)
4195 {
4196 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4197 oldname, newname));
4198 }
4199
4200 int
4201 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4202 zfs_userspace_cb_t func, void *arg)
4203 {
4204 zfs_cmd_t zc = {"\0"};
4205 zfs_useracct_t buf[100];
4206 libzfs_handle_t *hdl = zhp->zfs_hdl;
4207 int ret;
4208
4209 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4210
4211 zc.zc_objset_type = type;
4212 zc.zc_nvlist_dst = (uintptr_t)buf;
4213
4214 for (;;) {
4215 zfs_useracct_t *zua = buf;
4216
4217 zc.zc_nvlist_dst_size = sizeof (buf);
4218 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4219 char errbuf[ZFS_MAXNAMELEN + 32];
4220
4221 (void) snprintf(errbuf, sizeof (errbuf),
4222 dgettext(TEXT_DOMAIN,
4223 "cannot get used/quota for %s"), zc.zc_name);
4224 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4225 }
4226 if (zc.zc_nvlist_dst_size == 0)
4227 break;
4228
4229 while (zc.zc_nvlist_dst_size > 0) {
4230 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4231 zua->zu_space)) != 0)
4232 return (ret);
4233 zua++;
4234 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4235 }
4236 }
4237
4238 return (0);
4239 }
4240
4241 struct holdarg {
4242 nvlist_t *nvl;
4243 const char *snapname;
4244 const char *tag;
4245 boolean_t recursive;
4246 int error;
4247 };
4248
4249 static int
4250 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4251 {
4252 struct holdarg *ha = arg;
4253 char name[ZFS_MAXNAMELEN];
4254 int rv = 0;
4255
4256 (void) snprintf(name, sizeof (name),
4257 "%s@%s", zhp->zfs_name, ha->snapname);
4258
4259 if (lzc_exists(name))
4260 fnvlist_add_string(ha->nvl, name, ha->tag);
4261
4262 if (ha->recursive)
4263 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4264 zfs_close(zhp);
4265 return (rv);
4266 }
4267
4268 int
4269 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4270 boolean_t recursive, int cleanup_fd)
4271 {
4272 int ret;
4273 struct holdarg ha;
4274
4275 ha.nvl = fnvlist_alloc();
4276 ha.snapname = snapname;
4277 ha.tag = tag;
4278 ha.recursive = recursive;
4279 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4280
4281 if (nvlist_empty(ha.nvl)) {
4282 char errbuf[1024];
4283
4284 fnvlist_free(ha.nvl);
4285 ret = ENOENT;
4286 (void) snprintf(errbuf, sizeof (errbuf),
4287 dgettext(TEXT_DOMAIN,
4288 "cannot hold snapshot '%s@%s'"),
4289 zhp->zfs_name, snapname);
4290 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4291 return (ret);
4292 }
4293
4294 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4295 fnvlist_free(ha.nvl);
4296
4297 return (ret);
4298 }
4299
4300 int
4301 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4302 {
4303 int ret;
4304 nvlist_t *errors;
4305 libzfs_handle_t *hdl = zhp->zfs_hdl;
4306 char errbuf[1024];
4307 nvpair_t *elem;
4308
4309 errors = NULL;
4310 ret = lzc_hold(holds, cleanup_fd, &errors);
4311
4312 if (ret == 0) {
4313 /* There may be errors even in the success case. */
4314 fnvlist_free(errors);
4315 return (0);
4316 }
4317
4318 if (nvlist_empty(errors)) {
4319 /* no hold-specific errors */
4320 (void) snprintf(errbuf, sizeof (errbuf),
4321 dgettext(TEXT_DOMAIN, "cannot hold"));
4322 switch (ret) {
4323 case ENOTSUP:
4324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4325 "pool must be upgraded"));
4326 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4327 break;
4328 case EINVAL:
4329 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4330 break;
4331 default:
4332 (void) zfs_standard_error(hdl, ret, errbuf);
4333 }
4334 }
4335
4336 for (elem = nvlist_next_nvpair(errors, NULL);
4337 elem != NULL;
4338 elem = nvlist_next_nvpair(errors, elem)) {
4339 (void) snprintf(errbuf, sizeof (errbuf),
4340 dgettext(TEXT_DOMAIN,
4341 "cannot hold snapshot '%s'"), nvpair_name(elem));
4342 switch (fnvpair_value_int32(elem)) {
4343 case E2BIG:
4344 /*
4345 * Temporary tags wind up having the ds object id
4346 * prepended. So even if we passed the length check
4347 * above, it's still possible for the tag to wind
4348 * up being slightly too long.
4349 */
4350 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4351 break;
4352 case EINVAL:
4353 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4354 break;
4355 case EEXIST:
4356 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4357 break;
4358 default:
4359 (void) zfs_standard_error(hdl,
4360 fnvpair_value_int32(elem), errbuf);
4361 }
4362 }
4363
4364 fnvlist_free(errors);
4365 return (ret);
4366 }
4367
4368 static int
4369 zfs_release_one(zfs_handle_t *zhp, void *arg)
4370 {
4371 struct holdarg *ha = arg;
4372 char name[ZFS_MAXNAMELEN];
4373 int rv = 0;
4374 nvlist_t *existing_holds;
4375
4376 (void) snprintf(name, sizeof (name),
4377 "%s@%s", zhp->zfs_name, ha->snapname);
4378
4379 if (lzc_get_holds(name, &existing_holds) != 0) {
4380 ha->error = ENOENT;
4381 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4382 ha->error = ESRCH;
4383 } else {
4384 nvlist_t *torelease = fnvlist_alloc();
4385 fnvlist_add_boolean(torelease, ha->tag);
4386 fnvlist_add_nvlist(ha->nvl, name, torelease);
4387 fnvlist_free(torelease);
4388 }
4389
4390 if (ha->recursive)
4391 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4392 zfs_close(zhp);
4393 return (rv);
4394 }
4395
4396 int
4397 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4398 boolean_t recursive)
4399 {
4400 int ret;
4401 struct holdarg ha;
4402 nvlist_t *errors = NULL;
4403 nvpair_t *elem;
4404 libzfs_handle_t *hdl = zhp->zfs_hdl;
4405 char errbuf[1024];
4406
4407 ha.nvl = fnvlist_alloc();
4408 ha.snapname = snapname;
4409 ha.tag = tag;
4410 ha.recursive = recursive;
4411 ha.error = 0;
4412 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4413
4414 if (nvlist_empty(ha.nvl)) {
4415 fnvlist_free(ha.nvl);
4416 ret = ha.error;
4417 (void) snprintf(errbuf, sizeof (errbuf),
4418 dgettext(TEXT_DOMAIN,
4419 "cannot release hold from snapshot '%s@%s'"),
4420 zhp->zfs_name, snapname);
4421 if (ret == ESRCH) {
4422 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4423 } else {
4424 (void) zfs_standard_error(hdl, ret, errbuf);
4425 }
4426 return (ret);
4427 }
4428
4429 ret = lzc_release(ha.nvl, &errors);
4430 fnvlist_free(ha.nvl);
4431
4432 if (ret == 0) {
4433 /* There may be errors even in the success case. */
4434 fnvlist_free(errors);
4435 return (0);
4436 }
4437
4438 if (nvlist_empty(errors)) {
4439 /* no hold-specific errors */
4440 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4441 "cannot release"));
4442 switch (errno) {
4443 case ENOTSUP:
4444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4445 "pool must be upgraded"));
4446 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4447 break;
4448 default:
4449 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4450 }
4451 }
4452
4453 for (elem = nvlist_next_nvpair(errors, NULL);
4454 elem != NULL;
4455 elem = nvlist_next_nvpair(errors, elem)) {
4456 (void) snprintf(errbuf, sizeof (errbuf),
4457 dgettext(TEXT_DOMAIN,
4458 "cannot release hold from snapshot '%s'"),
4459 nvpair_name(elem));
4460 switch (fnvpair_value_int32(elem)) {
4461 case ESRCH:
4462 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4463 break;
4464 case EINVAL:
4465 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4466 break;
4467 default:
4468 (void) zfs_standard_error_fmt(hdl,
4469 fnvpair_value_int32(elem), errbuf);
4470 }
4471 }
4472
4473 fnvlist_free(errors);
4474 return (ret);
4475 }
4476
4477 int
4478 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4479 {
4480 zfs_cmd_t zc = {"\0"};
4481 libzfs_handle_t *hdl = zhp->zfs_hdl;
4482 int nvsz = 2048;
4483 void *nvbuf;
4484 int err = 0;
4485 char errbuf[1024];
4486
4487 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4488 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4489
4490 tryagain:
4491
4492 nvbuf = malloc(nvsz);
4493 if (nvbuf == NULL) {
4494 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4495 goto out;
4496 }
4497
4498 zc.zc_nvlist_dst_size = nvsz;
4499 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4500
4501 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4502
4503 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4504 (void) snprintf(errbuf, sizeof (errbuf),
4505 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4506 zc.zc_name);
4507 switch (errno) {
4508 case ENOMEM:
4509 free(nvbuf);
4510 nvsz = zc.zc_nvlist_dst_size;
4511 goto tryagain;
4512
4513 case ENOTSUP:
4514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4515 "pool must be upgraded"));
4516 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4517 break;
4518 case EINVAL:
4519 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4520 break;
4521 case ENOENT:
4522 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4523 break;
4524 default:
4525 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4526 break;
4527 }
4528 } else {
4529 /* success */
4530 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4531 if (rc) {
4532 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4533 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4534 zc.zc_name);
4535 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4536 }
4537 }
4538
4539 free(nvbuf);
4540 out:
4541 return (err);
4542 }
4543
4544 int
4545 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4546 {
4547 zfs_cmd_t zc = {"\0"};
4548 libzfs_handle_t *hdl = zhp->zfs_hdl;
4549 char *nvbuf;
4550 char errbuf[1024];
4551 size_t nvsz;
4552 int err;
4553
4554 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4555 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4556
4557 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4558 assert(err == 0);
4559
4560 nvbuf = malloc(nvsz);
4561
4562 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4563 assert(err == 0);
4564
4565 zc.zc_nvlist_src_size = nvsz;
4566 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4567 zc.zc_perm_action = un;
4568
4569 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4570
4571 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4572 (void) snprintf(errbuf, sizeof (errbuf),
4573 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4574 zc.zc_name);
4575 switch (errno) {
4576 case ENOTSUP:
4577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4578 "pool must be upgraded"));
4579 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4580 break;
4581 case EINVAL:
4582 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4583 break;
4584 case ENOENT:
4585 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4586 break;
4587 default:
4588 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4589 break;
4590 }
4591 }
4592
4593 free(nvbuf);
4594
4595 return (err);
4596 }
4597
4598 int
4599 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4600 {
4601 int err;
4602 char errbuf[1024];
4603
4604 err = lzc_get_holds(zhp->zfs_name, nvl);
4605
4606 if (err != 0) {
4607 libzfs_handle_t *hdl = zhp->zfs_hdl;
4608
4609 (void) snprintf(errbuf, sizeof (errbuf),
4610 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4611 zhp->zfs_name);
4612 switch (err) {
4613 case ENOTSUP:
4614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4615 "pool must be upgraded"));
4616 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4617 break;
4618 case EINVAL:
4619 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4620 break;
4621 case ENOENT:
4622 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4623 break;
4624 default:
4625 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4626 break;
4627 }
4628 }
4629
4630 return (err);
4631 }
4632
4633 /*
4634 * Convert the zvol's volume size to an appropriate reservation.
4635 * Note: If this routine is updated, it is necessary to update the ZFS test
4636 * suite's shell version in reservation.kshlib.
4637 */
4638 uint64_t
4639 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4640 {
4641 uint64_t numdb;
4642 uint64_t nblocks, volblocksize;
4643 int ncopies;
4644 char *strval;
4645
4646 if (nvlist_lookup_string(props,
4647 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4648 ncopies = atoi(strval);
4649 else
4650 ncopies = 1;
4651 if (nvlist_lookup_uint64(props,
4652 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4653 &volblocksize) != 0)
4654 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4655 nblocks = volsize/volblocksize;
4656 /* start with metadnode L0-L6 */
4657 numdb = 7;
4658 /* calculate number of indirects */
4659 while (nblocks > 1) {
4660 nblocks += DNODES_PER_LEVEL - 1;
4661 nblocks /= DNODES_PER_LEVEL;
4662 numdb += nblocks;
4663 }
4664 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4665 volsize *= ncopies;
4666 /*
4667 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4668 * compressed, but in practice they compress down to about
4669 * 1100 bytes
4670 */
4671 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4672 volsize += numdb;
4673 return (volsize);
4674 }