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