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