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