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