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