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