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