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