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