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