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