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