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