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