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