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