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