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