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