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