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