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