2d795d33aa216217a0ad392ca399e5b62407cc2f
[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.
3122  */
3123 int
3124 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3125 {
3126         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3127
3128         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3129
3130         if (ZFS_IS_VOLUME(zhp)) {
3131                 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3132                         return (-1);
3133
3134                 zc.zc_objset_type = DMU_OST_ZVOL;
3135         } else {
3136                 zc.zc_objset_type = DMU_OST_ZFS;
3137         }
3138
3139         zc.zc_defer_destroy = defer;
3140         if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3141                 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3142                     dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3143                     zhp->zfs_name));
3144         }
3145
3146         remove_mountpoint(zhp);
3147
3148         return (0);
3149 }
3150
3151 struct destroydata {
3152         nvlist_t *nvl;
3153         const char *snapname;
3154 };
3155
3156 static int
3157 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3158 {
3159         struct destroydata *dd = arg;
3160         zfs_handle_t *szhp;
3161         char name[ZFS_MAXNAMELEN];
3162         int rv = 0;
3163
3164         (void) snprintf(name, sizeof (name),
3165             "%s@%s", zhp->zfs_name, dd->snapname);
3166
3167         szhp = make_dataset_handle(zhp->zfs_hdl, name);
3168         if (szhp) {
3169                 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3170                 zfs_close(szhp);
3171         }
3172
3173         if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3174                 (void) zvol_remove_link(zhp->zfs_hdl, name);
3175                 /*
3176                  * NB: this is simply a best-effort.  We don't want to
3177                  * return an error, because then we wouldn't visit all
3178                  * the volumes.
3179                  */
3180         }
3181
3182         rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3183         zfs_close(zhp);
3184         return (rv);
3185 }
3186
3187 /*
3188  * Destroys all snapshots with the given name in zhp & descendants.
3189  */
3190 int
3191 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3192 {
3193         int ret;
3194         struct destroydata dd = { 0 };
3195
3196         dd.snapname = snapname;
3197         verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3198         (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3199
3200         if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3201                 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3202                     dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3203                     zhp->zfs_name, snapname);
3204         } else {
3205                 ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
3206         }
3207         nvlist_free(dd.nvl);
3208         return (ret);
3209 }
3210
3211 /*
3212  * Destroys all the snapshots named in the nvlist.  They must be underneath
3213  * the zhp (either snapshots of it, or snapshots of its descendants).
3214  */
3215 int
3216 zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
3217 {
3218         int ret;
3219         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3220
3221         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3222         if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, snaps) != 0)
3223                 return (-1);
3224         zc.zc_defer_destroy = defer;
3225
3226         ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS_NVL, &zc);
3227         if (ret != 0) {
3228                 char errbuf[1024];
3229
3230                 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3231                     "cannot destroy snapshots in %s"), zc.zc_name);
3232
3233                 switch (errno) {
3234                 case EEXIST:
3235                         zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3236                             "snapshot is cloned"));
3237                         return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3238
3239                 default:
3240                         return (zfs_standard_error(zhp->zfs_hdl, errno,
3241                             errbuf));
3242                 }
3243         }
3244
3245         return (0);
3246 }
3247
3248 /*
3249  * Clones the given dataset.  The target must be of the same type as the source.
3250  */
3251 int
3252 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3253 {
3254         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3255         char parent[ZFS_MAXNAMELEN];
3256         int ret;
3257         char errbuf[1024];
3258         libzfs_handle_t *hdl = zhp->zfs_hdl;
3259         zfs_type_t type;
3260         uint64_t zoned;
3261
3262         assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3263
3264         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3265             "cannot create '%s'"), target);
3266
3267         /* validate the target/clone name */
3268         if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3269                 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3270
3271         /* validate parents exist */
3272         if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3273                 return (-1);
3274
3275         (void) parent_name(target, parent, sizeof (parent));
3276
3277         /* do the clone */
3278         if (ZFS_IS_VOLUME(zhp)) {
3279                 zc.zc_objset_type = DMU_OST_ZVOL;
3280                 type = ZFS_TYPE_VOLUME;
3281         } else {
3282                 zc.zc_objset_type = DMU_OST_ZFS;
3283                 type = ZFS_TYPE_FILESYSTEM;
3284         }
3285
3286         if (props) {
3287                 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3288                     zhp, errbuf)) == NULL)
3289                         return (-1);
3290
3291                 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3292                         nvlist_free(props);
3293                         return (-1);
3294                 }
3295
3296                 nvlist_free(props);
3297         }
3298
3299         (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3300         (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3301         ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3302
3303         zcmd_free_nvlists(&zc);
3304
3305         if (ret != 0) {
3306                 switch (errno) {
3307
3308                 case ENOENT:
3309                         /*
3310                          * The parent doesn't exist.  We should have caught this
3311                          * above, but there may a race condition that has since
3312                          * destroyed the parent.
3313                          *
3314                          * At this point, we don't know whether it's the source
3315                          * that doesn't exist anymore, or whether the target
3316                          * dataset doesn't exist.
3317                          */
3318                         zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3319                             "no such parent '%s'"), parent);
3320                         return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3321
3322                 case EXDEV:
3323                         zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3324                             "source and target pools differ"));
3325                         return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3326                             errbuf));
3327
3328                 default:
3329                         return (zfs_standard_error(zhp->zfs_hdl, errno,
3330                             errbuf));
3331                 }
3332         } else if (ZFS_IS_VOLUME(zhp)) {
3333                 ret = zvol_create_link(zhp->zfs_hdl, target);
3334         }
3335
3336         return (ret);
3337 }
3338
3339 typedef struct promote_data {
3340         char cb_mountpoint[MAXPATHLEN];
3341         const char *cb_target;
3342         const char *cb_errbuf;
3343         uint64_t cb_pivot_txg;
3344 } promote_data_t;
3345
3346 static int
3347 promote_snap_cb(zfs_handle_t *zhp, void *data)
3348 {
3349         promote_data_t *pd = data;
3350         zfs_handle_t *szhp;
3351         char snapname[MAXPATHLEN];
3352         int rv = 0;
3353
3354         /* We don't care about snapshots after the pivot point */
3355         if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3356                 zfs_close(zhp);
3357                 return (0);
3358         }
3359
3360         /* Remove the device link if it's a zvol. */
3361         if (ZFS_IS_VOLUME(zhp))
3362                 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3363
3364         /* Check for conflicting names */
3365         (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3366         (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3367         szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3368         if (szhp != NULL) {
3369                 zfs_close(szhp);
3370                 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3371                     "snapshot name '%s' from origin \n"
3372                     "conflicts with '%s' from target"),
3373                     zhp->zfs_name, snapname);
3374                 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3375         }
3376         zfs_close(zhp);
3377         return (rv);
3378 }
3379
3380 static int
3381 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3382 {
3383         promote_data_t *pd = data;
3384
3385         /* We don't care about snapshots after the pivot point */
3386         if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3387                 /* Create the device link if it's a zvol. */
3388                 if (ZFS_IS_VOLUME(zhp))
3389                         (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3390         }
3391
3392         zfs_close(zhp);
3393         return (0);
3394 }
3395
3396 /*
3397  * Promotes the given clone fs to be the clone parent.
3398  */
3399 int
3400 zfs_promote(zfs_handle_t *zhp)
3401 {
3402         libzfs_handle_t *hdl = zhp->zfs_hdl;
3403         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3404         char parent[MAXPATHLEN];
3405         char *cp;
3406         int ret;
3407         zfs_handle_t *pzhp;
3408         promote_data_t pd;
3409         char errbuf[1024];
3410
3411         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3412             "cannot promote '%s'"), zhp->zfs_name);
3413
3414         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3415                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3416                     "snapshots can not be promoted"));
3417                 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3418         }
3419
3420         (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3421         if (parent[0] == '\0') {
3422                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3423                     "not a cloned filesystem"));
3424                 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3425         }
3426         cp = strchr(parent, '@');
3427         *cp = '\0';
3428
3429         /* Walk the snapshots we will be moving */
3430         pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3431         if (pzhp == NULL)
3432                 return (-1);
3433         pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3434         zfs_close(pzhp);
3435         pd.cb_target = zhp->zfs_name;
3436         pd.cb_errbuf = errbuf;
3437         pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3438         if (pzhp == NULL)
3439                 return (-1);
3440         (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3441             sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3442         ret = zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_cb, &pd);
3443         if (ret != 0) {
3444                 zfs_close(pzhp);
3445                 return (-1);
3446         }
3447
3448         /* issue the ioctl */
3449         (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3450             sizeof (zc.zc_value));
3451         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3452         ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3453
3454         if (ret != 0) {
3455                 int save_errno = errno;
3456
3457                 (void) zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_done_cb,
3458                     &pd);
3459                 zfs_close(pzhp);
3460
3461                 switch (save_errno) {
3462                 case EEXIST:
3463                         /*
3464                          * There is a conflicting snapshot name.  We
3465                          * should have caught this above, but they could
3466                          * have renamed something in the mean time.
3467                          */
3468                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3469                             "conflicting snapshot '%s' from parent '%s'"),
3470                             zc.zc_string, parent);
3471                         return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3472
3473                 default:
3474                         return (zfs_standard_error(hdl, save_errno, errbuf));
3475                 }
3476         } else {
3477                 (void) zfs_iter_snapshots(zhp, B_FALSE, promote_snap_done_cb,
3478                     &pd);
3479         }
3480
3481         zfs_close(pzhp);
3482         return (ret);
3483 }
3484
3485 struct createdata {
3486         const char *cd_snapname;
3487         int cd_ifexists;
3488 };
3489
3490 static int
3491 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3492 {
3493         struct createdata *cd = arg;
3494         int ret;
3495
3496         if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3497                 char name[MAXPATHLEN];
3498
3499                 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3500                 (void) strlcat(name, "@", sizeof (name));
3501                 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3502                 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3503                     cd->cd_ifexists);
3504                 /*
3505                  * NB: this is simply a best-effort.  We don't want to
3506                  * return an error, because then we wouldn't visit all
3507                  * the volumes.
3508                  */
3509         }
3510
3511         ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3512
3513         zfs_close(zhp);
3514
3515         return (ret);
3516 }
3517
3518 /*
3519  * Takes a snapshot of the given dataset.
3520  */
3521 int
3522 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3523     nvlist_t *props)
3524 {
3525         const char *delim;
3526         char parent[ZFS_MAXNAMELEN];
3527         zfs_handle_t *zhp;
3528         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3529         int ret;
3530         char errbuf[1024];
3531
3532         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3533             "cannot snapshot '%s'"), path);
3534
3535         /* validate the target name */
3536         if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3537                 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3538
3539         if (props) {
3540                 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3541                     props, B_FALSE, NULL, errbuf)) == NULL)
3542                         return (-1);
3543
3544                 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3545                         nvlist_free(props);
3546                         return (-1);
3547                 }
3548
3549                 nvlist_free(props);
3550         }
3551
3552         /* make sure the parent exists and is of the appropriate type */
3553         delim = strchr(path, '@');
3554         (void) strncpy(parent, path, delim - path);
3555         parent[delim - path] = '\0';
3556
3557         if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3558             ZFS_TYPE_VOLUME)) == NULL) {
3559                 zcmd_free_nvlists(&zc);
3560                 return (-1);
3561         }
3562
3563         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3564         (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3565         if (ZFS_IS_VOLUME(zhp))
3566                 zc.zc_objset_type = DMU_OST_ZVOL;
3567         else
3568                 zc.zc_objset_type = DMU_OST_ZFS;
3569         zc.zc_cookie = recursive;
3570         ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3571
3572         zcmd_free_nvlists(&zc);
3573
3574         /*
3575          * if it was recursive, the one that actually failed will be in
3576          * zc.zc_name.
3577          */
3578         if (ret != 0)
3579                 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3580                     "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3581
3582         if (ret == 0 && recursive) {
3583                 struct createdata cd;
3584
3585                 cd.cd_snapname = delim + 1;
3586                 cd.cd_ifexists = B_FALSE;
3587                 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3588         }
3589         if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3590                 ret = zvol_create_link(zhp->zfs_hdl, path);
3591                 if (ret != 0) {
3592                         (void) zfs_standard_error(hdl, errno,
3593                             dgettext(TEXT_DOMAIN,
3594                             "Volume successfully snapshotted, but device links "
3595                             "were not created"));
3596                         zfs_close(zhp);
3597                         return (-1);
3598                 }
3599         }
3600
3601         if (ret != 0)
3602                 (void) zfs_standard_error(hdl, errno, errbuf);
3603
3604         zfs_close(zhp);
3605
3606         return (ret);
3607 }
3608
3609 /*
3610  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3611  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3612  * is a dependent and we should just destroy it without checking the transaction
3613  * group.
3614  */
3615 typedef struct rollback_data {
3616         const char      *cb_target;             /* the snapshot */
3617         uint64_t        cb_create;              /* creation time reference */
3618         boolean_t       cb_error;
3619         boolean_t       cb_dependent;
3620         boolean_t       cb_force;
3621 } rollback_data_t;
3622
3623 static int
3624 rollback_destroy(zfs_handle_t *zhp, void *data)
3625 {
3626         rollback_data_t *cbp = data;
3627
3628         if (!cbp->cb_dependent) {
3629                 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3630                     zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3631                     zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3632                     cbp->cb_create) {
3633                         char *logstr;
3634
3635                         cbp->cb_dependent = B_TRUE;
3636                         cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3637                             rollback_destroy, cbp);
3638                         cbp->cb_dependent = B_FALSE;
3639
3640                         logstr = zhp->zfs_hdl->libzfs_log_str;
3641                         zhp->zfs_hdl->libzfs_log_str = NULL;
3642                         cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3643                         zhp->zfs_hdl->libzfs_log_str = logstr;
3644                 }
3645         } else {
3646                 /* We must destroy this clone; first unmount it */
3647                 prop_changelist_t *clp;
3648
3649                 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3650                     cbp->cb_force ? MS_FORCE: 0);
3651                 if (clp == NULL || changelist_prefix(clp) != 0) {
3652                         cbp->cb_error = B_TRUE;
3653                         zfs_close(zhp);
3654                         return (0);
3655                 }
3656                 if (zfs_destroy(zhp, B_FALSE) != 0)
3657                         cbp->cb_error = B_TRUE;
3658                 else
3659                         changelist_remove(clp, zhp->zfs_name);
3660                 (void) changelist_postfix(clp);
3661                 changelist_free(clp);
3662         }
3663
3664         zfs_close(zhp);
3665         return (0);
3666 }
3667
3668 /*
3669  * Given a dataset, rollback to a specific snapshot, discarding any
3670  * data changes since then and making it the active dataset.
3671  *
3672  * Any snapshots more recent than the target are destroyed, along with
3673  * their dependents.
3674  */
3675 int
3676 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3677 {
3678         rollback_data_t cb = { 0 };
3679         int err;
3680         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3681         boolean_t restore_resv = 0;
3682         uint64_t old_volsize = 0, new_volsize;
3683         zfs_prop_t resv_prop = { 0 };
3684
3685         assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3686             zhp->zfs_type == ZFS_TYPE_VOLUME);
3687
3688         /*
3689          * Destroy all recent snapshots and its dependends.
3690          */
3691         cb.cb_force = force;
3692         cb.cb_target = snap->zfs_name;
3693         cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3694         (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3695
3696         if (cb.cb_error)
3697                 return (-1);
3698
3699         /*
3700          * Now that we have verified that the snapshot is the latest,
3701          * rollback to the given snapshot.
3702          */
3703
3704         if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3705                 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3706                         return (-1);
3707                 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3708                         return (-1);
3709                 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3710                 restore_resv =
3711                     (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3712         }
3713
3714         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3715
3716         if (ZFS_IS_VOLUME(zhp))
3717                 zc.zc_objset_type = DMU_OST_ZVOL;
3718         else
3719                 zc.zc_objset_type = DMU_OST_ZFS;
3720
3721         /*
3722          * We rely on zfs_iter_children() to verify that there are no
3723          * newer snapshots for the given dataset.  Therefore, we can
3724          * simply pass the name on to the ioctl() call.  There is still
3725          * an unlikely race condition where the user has taken a
3726          * snapshot since we verified that this was the most recent.
3727          *
3728          */
3729         if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3730                 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3731                     dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3732                     zhp->zfs_name);
3733                 return (err);
3734         }
3735
3736         /*
3737          * For volumes, if the pre-rollback volsize matched the pre-
3738          * rollback reservation and the volsize has changed then set
3739          * the reservation property to the post-rollback volsize.
3740          * Make a new handle since the rollback closed the dataset.
3741          */
3742         if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3743             (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3744                 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3745                         zfs_close(zhp);
3746                         return (err);
3747                 }
3748                 if (restore_resv) {
3749                         new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3750                         if (old_volsize != new_volsize)
3751                                 err = zfs_prop_set_int(zhp, resv_prop,
3752                                     new_volsize);
3753                 }
3754                 zfs_close(zhp);
3755         }
3756         return (err);
3757 }
3758
3759 /*
3760  * Renames the given dataset.
3761  */
3762 int
3763 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3764 {
3765         int ret;
3766         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3767         char *delim;
3768         prop_changelist_t *cl = NULL;
3769         zfs_handle_t *zhrp = NULL;
3770         char *parentname = NULL;
3771         char parent[ZFS_MAXNAMELEN];
3772         libzfs_handle_t *hdl = zhp->zfs_hdl;
3773         char errbuf[1024];
3774
3775         /* if we have the same exact name, just return success */
3776         if (strcmp(zhp->zfs_name, target) == 0)
3777                 return (0);
3778
3779         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3780             "cannot rename to '%s'"), target);
3781
3782         /*
3783          * Make sure the target name is valid
3784          */
3785         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3786                 if ((strchr(target, '@') == NULL) ||
3787                     *target == '@') {
3788                         /*
3789                          * Snapshot target name is abbreviated,
3790                          * reconstruct full dataset name
3791                          */
3792                         (void) strlcpy(parent, zhp->zfs_name,
3793                             sizeof (parent));
3794                         delim = strchr(parent, '@');
3795                         if (strchr(target, '@') == NULL)
3796                                 *(++delim) = '\0';
3797                         else
3798                                 *delim = '\0';
3799                         (void) strlcat(parent, target, sizeof (parent));
3800                         target = parent;
3801                 } else {
3802                         /*
3803                          * Make sure we're renaming within the same dataset.
3804                          */
3805                         delim = strchr(target, '@');
3806                         if (strncmp(zhp->zfs_name, target, delim - target)
3807                             != 0 || zhp->zfs_name[delim - target] != '@') {
3808                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3809                                     "snapshots must be part of same "
3810                                     "dataset"));
3811                                 return (zfs_error(hdl, EZFS_CROSSTARGET,
3812                                     errbuf));
3813                         }
3814                 }
3815                 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3816                         return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3817         } else {
3818                 if (recursive) {
3819                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3820                             "recursive rename must be a snapshot"));
3821                         return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3822                 }
3823
3824                 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3825                         return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3826
3827                 /* validate parents */
3828                 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3829                         return (-1);
3830
3831                 /* make sure we're in the same pool */
3832                 verify((delim = strchr(target, '/')) != NULL);
3833                 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3834                     zhp->zfs_name[delim - target] != '/') {
3835                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3836                             "datasets must be within same pool"));
3837                         return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3838                 }
3839
3840                 /* new name cannot be a child of the current dataset name */
3841                 if (is_descendant(zhp->zfs_name, target)) {
3842                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3843                             "New dataset name cannot be a descendant of "
3844                             "current dataset name"));
3845                         return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3846                 }
3847         }
3848
3849         (void) snprintf(errbuf, sizeof (errbuf),
3850             dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3851
3852         if (getzoneid() == GLOBAL_ZONEID &&
3853             zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3854                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3855                     "dataset is used in a non-global zone"));
3856                 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3857         }
3858
3859         if (recursive) {
3860                 struct destroydata dd;
3861
3862                 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3863                 if (parentname == NULL) {
3864                         ret = -1;
3865                         goto error;
3866                 }
3867                 delim = strchr(parentname, '@');
3868                 *delim = '\0';
3869                 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3870                 if (zhrp == NULL) {
3871                         ret = -1;
3872                         goto error;
3873                 }
3874
3875                 dd.snapname = delim + 1;
3876
3877                 /* We remove any zvol links prior to renaming them */
3878                 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3879                 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3880                 nvlist_free(dd.nvl);
3881                 if (ret) {
3882                         goto error;
3883                 }
3884         } else {
3885                 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3886                         return (-1);
3887
3888                 if (changelist_haszonedchild(cl)) {
3889                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3890                             "child dataset with inherited mountpoint is used "
3891                             "in a non-global zone"));
3892                         (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3893                         ret = -1;
3894                         goto error;
3895                 }
3896
3897                 if ((ret = changelist_prefix(cl)) != 0)
3898                         goto error;
3899         }
3900
3901         if (ZFS_IS_VOLUME(zhp))
3902                 zc.zc_objset_type = DMU_OST_ZVOL;
3903         else
3904                 zc.zc_objset_type = DMU_OST_ZFS;
3905
3906         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3907         (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3908
3909         zc.zc_cookie = recursive;
3910
3911         if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3912                 /*
3913                  * if it was recursive, the one that actually failed will
3914                  * be in zc.zc_name
3915                  */
3916                 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3917                     "cannot rename '%s'"), zc.zc_name);
3918
3919                 if (recursive && errno == EEXIST) {
3920                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3921                             "a child dataset already has a snapshot "
3922                             "with the new name"));
3923                         (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3924                 } else {
3925                         (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3926                 }
3927
3928                 /*
3929                  * On failure, we still want to remount any filesystems that
3930                  * were previously mounted, so we don't alter the system state.
3931                  */
3932                 if (recursive) {
3933                         struct createdata cd;
3934
3935                         /* only create links for datasets that had existed */
3936                         cd.cd_snapname = delim + 1;
3937                         cd.cd_ifexists = B_TRUE;
3938                         (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3939                             &cd);
3940                 } else {
3941                         (void) changelist_postfix(cl);
3942                 }
3943         } else {
3944                 if (recursive) {
3945                         struct createdata cd;
3946
3947                         /* only create links for datasets that had existed */
3948                         cd.cd_snapname = strchr(target, '@') + 1;
3949                         cd.cd_ifexists = B_TRUE;
3950                         ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3951                             &cd);
3952                 } else {
3953                         changelist_rename(cl, zfs_get_name(zhp), target);
3954                         ret = changelist_postfix(cl);
3955                 }
3956         }
3957
3958 error:
3959         if (parentname) {
3960                 free(parentname);
3961         }
3962         if (zhrp) {
3963                 zfs_close(zhrp);
3964         }
3965         if (cl) {
3966                 changelist_free(cl);
3967         }
3968         return (ret);
3969 }
3970
3971 /*
3972  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3973  * and wait briefly for udev to create the /dev link.
3974  */
3975 int
3976 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3977 {
3978         return (zvol_create_link_common(hdl, dataset, B_FALSE));
3979 }
3980
3981 static int
3982 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3983 {
3984         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3985         char path[MAXPATHLEN];
3986         int error;
3987
3988         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3989
3990         /*
3991          * Issue the appropriate ioctl.
3992          */
3993         if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3994                 switch (errno) {
3995                 case EEXIST:
3996                         /*
3997                          * Silently ignore the case where the link already
3998                          * exists.  This allows 'zfs volinit' to be run multiple
3999                          * times without errors.
4000                          */
4001                         return (0);
4002
4003                 case ENOENT:
4004                         /*
4005                          * Dataset does not exist in the kernel.  If we
4006                          * don't care (see zfs_rename), then ignore the
4007                          * error quietly.
4008                          */
4009                         if (ifexists) {
4010                                 return (0);
4011                         }
4012
4013                         /* FALLTHROUGH */
4014
4015                 default:
4016                         return (zfs_standard_error_fmt(hdl, errno,
4017                             dgettext(TEXT_DOMAIN, "cannot create device links "
4018                             "for '%s'"), dataset));
4019                 }
4020         }
4021
4022         /*
4023          * Wait up to 10 seconds for udev to create the device.
4024          */
4025         (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
4026         error = zpool_label_disk_wait(path, 10000);
4027         if (error)
4028                 (void) printf(gettext("%s may not be immediately "
4029                     "available\n"), path);
4030
4031         return (0);
4032 }
4033
4034 /*
4035  * Remove a minor node for the given zvol and the associated /dev links.
4036  */
4037 int
4038 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4039 {
4040         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4041         int timeout = 3000; /* in milliseconds */
4042         int error = 0;
4043         int i;
4044
4045         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4046
4047         /*
4048          * Due to concurrent updates by udev the device may be reported as
4049          * busy.  In this case don't immediately fail.  Instead briefly delay
4050          * and retry the ioctl() which is now likely to succeed.  If unable
4051          * remove the link after timeout milliseconds return the failure.
4052          */
4053         for (i = 0; i < timeout; i++) {
4054                 error = ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
4055                 if (error && errno == EBUSY) {
4056                         usleep(1000);
4057                         continue;
4058                 } else {
4059                         break;
4060                 }
4061         }
4062
4063         if (error) {
4064                 switch (errno) {
4065                 case ENXIO:
4066                         /*
4067                          * Silently ignore the case where the link no longer
4068                          * exists, so that 'zfs volfini' can be run multiple
4069                          * times without errors.
4070                          */
4071                         return (0);
4072
4073                 default:
4074                         return (zfs_standard_error_fmt(hdl, errno,
4075                             dgettext(TEXT_DOMAIN, "cannot remove device "
4076                             "links for '%s': %s"), dataset, strerror(errno)));
4077                 }
4078         }
4079
4080         return (0);
4081 }
4082
4083 nvlist_t *
4084 zfs_get_user_props(zfs_handle_t *zhp)
4085 {
4086         return (zhp->zfs_user_props);
4087 }
4088
4089 /*
4090  * This function is used by 'zfs list' to determine the exact set of columns to
4091  * display, and their maximum widths.  This does two main things:
4092  *
4093  *      - If this is a list of all properties, then expand the list to include
4094  *        all native properties, and set a flag so that for each dataset we look
4095  *        for new unique user properties and add them to the list.
4096  *
4097  *      - For non fixed-width properties, keep track of the maximum width seen
4098  *        so that we can size the column appropriately. If the user has
4099  *        requested received property values, we also need to compute the width
4100  *        of the RECEIVED column.
4101  */
4102 int
4103 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
4104 {
4105         libzfs_handle_t *hdl = zhp->zfs_hdl;
4106         zprop_list_t *entry;
4107         zprop_list_t **last, **start;
4108         nvlist_t *userprops, *propval;
4109         nvpair_t *elem;
4110         char *strval;
4111         char buf[ZFS_MAXPROPLEN];
4112
4113         if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4114                 return (-1);
4115
4116         userprops = zfs_get_user_props(zhp);
4117
4118         entry = *plp;
4119         if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4120                 /*
4121                  * Go through and add any user properties as necessary.  We
4122                  * start by incrementing our list pointer to the first
4123                  * non-native property.
4124                  */
4125                 start = plp;
4126                 while (*start != NULL) {
4127                         if ((*start)->pl_prop == ZPROP_INVAL)
4128                                 break;
4129                         start = &(*start)->pl_next;
4130                 }
4131
4132                 elem = NULL;
4133                 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4134                         /*
4135                          * See if we've already found this property in our list.
4136                          */
4137                         for (last = start; *last != NULL;
4138                             last = &(*last)->pl_next) {
4139                                 if (strcmp((*last)->pl_user_prop,
4140                                     nvpair_name(elem)) == 0)
4141                                         break;
4142                         }
4143
4144                         if (*last == NULL) {
4145                                 if ((entry = zfs_alloc(hdl,
4146                                     sizeof (zprop_list_t))) == NULL ||
4147                                     ((entry->pl_user_prop = zfs_strdup(hdl,
4148                                     nvpair_name(elem)))) == NULL) {
4149                                         free(entry);
4150                                         return (-1);
4151                                 }
4152
4153                                 entry->pl_prop = ZPROP_INVAL;
4154                                 entry->pl_width = strlen(nvpair_name(elem));
4155                                 entry->pl_all = B_TRUE;
4156                                 *last = entry;
4157                         }
4158                 }
4159         }
4160
4161         /*
4162          * Now go through and check the width of any non-fixed columns
4163          */
4164         for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4165                 if (entry->pl_fixed)
4166                         continue;
4167
4168                 if (entry->pl_prop != ZPROP_INVAL) {
4169                         if (zfs_prop_get(zhp, entry->pl_prop,
4170                             buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4171                                 if (strlen(buf) > entry->pl_width)
4172                                         entry->pl_width = strlen(buf);
4173                         }
4174                         if (received && zfs_prop_get_recvd(zhp,
4175                             zfs_prop_to_name(entry->pl_prop),
4176                             buf, sizeof (buf), B_FALSE) == 0)
4177                                 if (strlen(buf) > entry->pl_recvd_width)
4178                                         entry->pl_recvd_width = strlen(buf);
4179                 } else {
4180                         if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4181                             &propval) == 0) {
4182                                 verify(nvlist_lookup_string(propval,
4183                                     ZPROP_VALUE, &strval) == 0);
4184                                 if (strlen(strval) > entry->pl_width)
4185                                         entry->pl_width = strlen(strval);
4186                         }
4187                         if (received && zfs_prop_get_recvd(zhp,
4188                             entry->pl_user_prop,
4189                             buf, sizeof (buf), B_FALSE) == 0)
4190                                 if (strlen(buf) > entry->pl_recvd_width)
4191                                         entry->pl_recvd_width = strlen(buf);
4192                 }
4193         }
4194
4195         return (0);
4196 }
4197
4198 void
4199 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4200 {
4201         nvpair_t *curr;
4202
4203         /*
4204          * Keep a reference to the props-table against which we prune the
4205          * properties.
4206          */
4207         zhp->zfs_props_table = props;
4208
4209         curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4210
4211         while (curr) {
4212                 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4213                 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4214
4215                 /*
4216                  * User properties will result in ZPROP_INVAL, and since we
4217                  * only know how to prune standard ZFS properties, we always
4218                  * leave these in the list.  This can also happen if we
4219                  * encounter an unknown DSL property (when running older
4220                  * software, for example).
4221                  */
4222                 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4223                         (void) nvlist_remove(zhp->zfs_props,
4224                             nvpair_name(curr), nvpair_type(curr));
4225                 curr = next;
4226         }
4227 }
4228
4229 static int
4230 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4231     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4232 {
4233         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4234         nvlist_t *nvlist = NULL;
4235         int error;
4236
4237         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4238         (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4239         zc.zc_cookie = (uint64_t)cmd;
4240
4241         if (cmd == ZFS_SMB_ACL_RENAME) {
4242                 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4243                         (void) no_memory(hdl);
4244                         return (-1);
4245                 }
4246         }
4247
4248         switch (cmd) {
4249         case ZFS_SMB_ACL_ADD:
4250         case ZFS_SMB_ACL_REMOVE:
4251                 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4252                 break;
4253         case ZFS_SMB_ACL_RENAME:
4254                 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4255                     resource1) != 0) {
4256                                 (void) no_memory(hdl);
4257                                 return (-1);
4258                 }
4259                 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4260                     resource2) != 0) {
4261                                 (void) no_memory(hdl);
4262                                 return (-1);
4263                 }
4264                 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4265                         nvlist_free(nvlist);
4266                         return (-1);
4267                 }
4268                 break;
4269         case ZFS_SMB_ACL_PURGE:
4270                 break;
4271         default:
4272                 return (-1);
4273         }
4274         error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4275         if (nvlist)
4276                 nvlist_free(nvlist);
4277         return (error);
4278 }
4279
4280 int
4281 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4282     char *path, char *resource)
4283 {
4284         return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4285             resource, NULL));
4286 }
4287
4288 int
4289 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4290     char *path, char *resource)
4291 {
4292         return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4293             resource, NULL));
4294 }
4295
4296 int
4297 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4298 {
4299         return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4300             NULL, NULL));
4301 }
4302
4303 int
4304 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4305     char *oldname, char *newname)
4306 {
4307         return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4308             oldname, newname));
4309 }
4310
4311 int
4312 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4313     zfs_userspace_cb_t func, void *arg)
4314 {
4315         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4316         int error;
4317         zfs_useracct_t buf[100];
4318
4319         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4320
4321         zc.zc_objset_type = type;
4322         zc.zc_nvlist_dst = (uintptr_t)buf;
4323
4324         /* CONSTCOND */
4325         while (1) {
4326                 zfs_useracct_t *zua = buf;
4327
4328                 zc.zc_nvlist_dst_size = sizeof (buf);
4329                 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4330                     ZFS_IOC_USERSPACE_MANY, &zc);
4331                 if (error || zc.zc_nvlist_dst_size == 0)
4332                         break;
4333
4334                 while (zc.zc_nvlist_dst_size > 0) {
4335                         error = func(arg, zua->zu_domain, zua->zu_rid,
4336                             zua->zu_space);
4337                         if (error != 0)
4338                                 return (error);
4339                         zua++;
4340                         zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4341                 }
4342         }
4343
4344         return (error);
4345 }
4346
4347 int
4348 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4349     boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4350     int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4351 {
4352         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4353         libzfs_handle_t *hdl = zhp->zfs_hdl;
4354
4355         ASSERT(!recursive || dsobj == 0);
4356
4357         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4358         (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4359         if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4360             >= sizeof (zc.zc_string))
4361                 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4362         zc.zc_cookie = recursive;
4363         zc.zc_temphold = temphold;
4364         zc.zc_cleanup_fd = cleanup_fd;
4365         zc.zc_sendobj = dsobj;
4366         zc.zc_createtxg = createtxg;
4367
4368         if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4369                 char errbuf[ZFS_MAXNAMELEN+32];
4370
4371                 /*
4372                  * if it was recursive, the one that actually failed will be in
4373                  * zc.zc_name.
4374                  */
4375                 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4376                     "cannot hold '%s@%s'"), zc.zc_name, snapname);
4377                 switch (errno) {
4378                 case E2BIG:
4379                         /*
4380                          * Temporary tags wind up having the ds object id
4381                          * prepended. So even if we passed the length check
4382                          * above, it's still possible for the tag to wind
4383                          * up being slightly too long.
4384                          */
4385                         return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4386                 case ENOTSUP:
4387                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4388                             "pool must be upgraded"));
4389                         return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4390                 case EINVAL:
4391                         return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4392                 case EEXIST:
4393                         return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4394                 case ENOENT:
4395                         if (enoent_ok)
4396                                 return (ENOENT);
4397                         /* FALLTHROUGH */
4398                 default:
4399                         return (zfs_standard_error_fmt(hdl, errno, errbuf));
4400                 }
4401         }
4402
4403         return (0);
4404 }
4405
4406 int
4407 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4408     boolean_t recursive)
4409 {
4410         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4411         libzfs_handle_t *hdl = zhp->zfs_hdl;
4412
4413         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4414         (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4415         if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4416             >= sizeof (zc.zc_string))
4417                 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4418         zc.zc_cookie = recursive;
4419
4420         if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4421                 char errbuf[ZFS_MAXNAMELEN+32];
4422
4423                 /*
4424                  * if it was recursive, the one that actually failed will be in
4425                  * zc.zc_name.
4426                  */
4427                 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4428                     "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4429                     snapname);
4430                 switch (errno) {
4431                 case ESRCH:
4432                         return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4433                 case ENOTSUP:
4434                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4435                             "pool must be upgraded"));
4436                         return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4437                 case EINVAL:
4438                         return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4439                 default:
4440                         return (zfs_standard_error_fmt(hdl, errno, errbuf));
4441                 }
4442         }
4443
4444         return (0);
4445 }
4446
4447 int
4448 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4449 {
4450         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4451         libzfs_handle_t *hdl = zhp->zfs_hdl;
4452         int nvsz = 2048;
4453         void *nvbuf;
4454         int err = 0;
4455         char errbuf[ZFS_MAXNAMELEN+32];
4456
4457         assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4458             zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4459
4460 tryagain:
4461
4462         nvbuf = malloc(nvsz);
4463         if (nvbuf == NULL) {
4464                 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4465                 goto out;
4466         }
4467
4468         zc.zc_nvlist_dst_size = nvsz;
4469         zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4470
4471         (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4472
4473         if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4474                 (void) snprintf(errbuf, sizeof (errbuf),
4475                     dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4476                     zc.zc_name);
4477                 switch (errno) {
4478                 case ENOMEM:
4479                         free(nvbuf);
4480                         nvsz = zc.zc_nvlist_dst_size;
4481                         goto tryagain;
4482
4483                 case ENOTSUP:
4484                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4485                             "pool must be upgraded"));
4486                         err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4487                         break;
4488                 case EINVAL:
4489                         err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4490                         break;
4491                 case ENOENT:
4492                         err = zfs_error(hdl, EZFS_NOENT, errbuf);
4493                         break;
4494                 default:
4495                         err = zfs_standard_error_fmt(hdl, errno, errbuf);
4496                         break;
4497                 }
4498         } else {
4499                 /* success */
4500                 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4501                 if (rc) {
4502                         (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4503                             TEXT_DOMAIN, "cannot get permissions on '%s'"),
4504                             zc.zc_name);
4505                         err = zfs_standard_error_fmt(hdl, rc, errbuf);
4506                 }
4507         }
4508
4509         free(nvbuf);
4510 out:
4511         return (err);
4512 }
4513
4514 int
4515 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4516 {
4517         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4518         libzfs_handle_t *hdl = zhp->zfs_hdl;
4519         char *nvbuf;
4520         char errbuf[ZFS_MAXNAMELEN+32];
4521         size_t nvsz;
4522         int err;
4523
4524         assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4525             zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4526
4527         err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4528         assert(err == 0);
4529
4530         nvbuf = malloc(nvsz);
4531
4532         err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4533         assert(err == 0);
4534
4535         zc.zc_nvlist_src_size = nvsz;
4536         zc.zc_nvlist_src = (uintptr_t)nvbuf;
4537         zc.zc_perm_action = un;
4538
4539         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4540
4541         if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4542                 (void) snprintf(errbuf, sizeof (errbuf),
4543                     dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4544                     zc.zc_name);
4545                 switch (errno) {
4546                 case ENOTSUP:
4547                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4548                             "pool must be upgraded"));
4549                         err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4550                         break;
4551                 case EINVAL:
4552                         err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4553                         break;
4554                 case ENOENT:
4555                         err = zfs_error(hdl, EZFS_NOENT, errbuf);
4556                         break;
4557                 default:
4558                         err = zfs_standard_error_fmt(hdl, errno, errbuf);
4559                         break;
4560                 }
4561         }
4562
4563         free(nvbuf);
4564
4565         return (err);
4566 }
4567
4568 int
4569 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4570 {
4571         zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4572         libzfs_handle_t *hdl = zhp->zfs_hdl;
4573         int nvsz = 2048;
4574         void *nvbuf;
4575         int err = 0;
4576         char errbuf[ZFS_MAXNAMELEN+32];
4577
4578         assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4579
4580 tryagain:
4581
4582         nvbuf = malloc(nvsz);
4583         if (nvbuf == NULL) {
4584                 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4585                 goto out;
4586         }
4587
4588         zc.zc_nvlist_dst_size = nvsz;
4589         zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4590
4591         (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4592
4593         if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4594                 (void) snprintf(errbuf, sizeof (errbuf),
4595                     dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4596                     zc.zc_name);
4597                 switch (errno) {
4598                 case ENOMEM:
4599                         free(nvbuf);
4600                         nvsz = zc.zc_nvlist_dst_size;
4601                         goto tryagain;
4602
4603                 case ENOTSUP:
4604                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4605                             "pool must be upgraded"));
4606                         err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4607                         break;
4608                 case EINVAL:
4609                         err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4610                         break;
4611                 case ENOENT:
4612                         err = zfs_error(hdl, EZFS_NOENT, errbuf);
4613                         break;
4614                 default:
4615                         err = zfs_standard_error_fmt(hdl, errno, errbuf);
4616                         break;
4617                 }
4618         } else {
4619                 /* success */
4620                 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4621                 if (rc) {
4622                         (void) snprintf(errbuf, sizeof (errbuf),
4623                             dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4624                             zc.zc_name);
4625                         err = zfs_standard_error_fmt(hdl, rc, errbuf);
4626                 }
4627         }
4628
4629         free(nvbuf);
4630 out:
4631         return (err);
4632 }
4633
4634 uint64_t
4635 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4636 {
4637         uint64_t numdb;
4638         uint64_t nblocks, volblocksize;
4639         int ncopies;
4640         char *strval;
4641
4642         if (nvlist_lookup_string(props,
4643             zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4644                 ncopies = atoi(strval);
4645         else
4646                 ncopies = 1;
4647         if (nvlist_lookup_uint64(props,
4648             zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4649             &volblocksize) != 0)
4650                 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4651         nblocks = volsize/volblocksize;
4652         /* start with metadnode L0-L6 */
4653         numdb = 7;
4654         /* calculate number of indirects */
4655         while (nblocks > 1) {
4656                 nblocks += DNODES_PER_LEVEL - 1;
4657                 nblocks /= DNODES_PER_LEVEL;
4658                 numdb += nblocks;
4659         }
4660         numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4661         volsize *= ncopies;
4662         /*
4663          * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4664          * compressed, but in practice they compress down to about
4665          * 1100 bytes
4666          */
4667         numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4668         volsize += numdb;
4669         return (volsize);
4670 }