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