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