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