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