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