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