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