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