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