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