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